@@ -50,51 +50,38 @@ macro_rules! try_read_vlq {
5050 } ;
5151}
5252
53- pub const fn parse_dedup_len ( mut data : & [ u8 ] ) -> Result < usize , Error > {
54- read_vlq ( & mut data)
53+ /// Blobby file header
54+ pub struct Header {
55+ /// Number of blobs stored in the file
56+ pub items_len : usize ,
57+ /// Number of deduplicated blobs
58+ pub dedup_len : usize ,
5559}
5660
57- pub const fn parse_items_len ( mut data : & [ u8 ] ) -> Result < usize , Error > {
58- let dedup_index_len = try_read_vlq ! ( data) ;
59-
60- let mut i = 0 ;
61- while i < dedup_index_len {
62- let m = try_read_vlq ! ( data) ;
63- let split = data. split_at ( m) ;
64- data = split. 1 ;
65- i += 1 ;
66- }
67-
68- let mut i = 0 ;
69- loop {
70- if data. is_empty ( ) {
71- return Ok ( i) ;
61+ impl Header {
62+ /// Parse blobby header
63+ pub const fn parse ( data : & mut & [ u8 ] ) -> Result < Self , Error > {
64+ match ( read_vlq ( data) , read_vlq ( data) ) {
65+ ( Ok ( items_len) , Ok ( dedup_len) ) => Ok ( Header {
66+ items_len,
67+ dedup_len,
68+ } ) ,
69+ ( Err ( err) , _) | ( Ok ( _) , Err ( err) ) => Err ( err) ,
7270 }
73- let val = try_read_vlq ! ( data) ;
74- // the least significant bit is used as a flag
75- let is_ref = ( val & 1 ) != 0 ;
76- let val = val >> 1 ;
77- if is_ref {
78- if val >= dedup_index_len {
79- return Err ( Error :: InvalidIndex ) ;
80- }
81- } else {
82- if val > data. len ( ) {
83- return Err ( Error :: UnexpectedEnd ) ;
84- }
85- let split = data. split_at ( val) ;
86- data = split. 1 ;
87- } ;
88- i += 1 ;
8971 }
9072}
9173
9274/// Parse blobby data into an array.
93- pub const fn parse_into_array < const ITEMS : usize , const DEDUP_LEN : usize > (
75+ pub const fn parse_into_array < const ITEMS_LEN : usize , const DEDUP_LEN : usize > (
9476 mut data : & [ u8 ] ,
95- ) -> Result < [ & [ u8 ] ; ITEMS ] , Error > {
96- if try_read_vlq ! ( data) != DEDUP_LEN {
97- return Err ( Error :: BadArrayLen ) ;
77+ ) -> Result < [ & [ u8 ] ; ITEMS_LEN ] , Error > {
78+ match Header :: parse ( & mut data) {
79+ Ok ( header) => {
80+ if header. items_len != ITEMS_LEN || header. dedup_len != DEDUP_LEN {
81+ return Err ( Error :: BadArrayLen ) ;
82+ }
83+ }
84+ Err ( err) => return Err ( err) ,
9885 }
9986
10087 let mut dedup_index: [ & [ u8 ] ; DEDUP_LEN ] = [ & [ ] ; DEDUP_LEN ] ;
@@ -108,7 +95,7 @@ pub const fn parse_into_array<const ITEMS: usize, const DEDUP_LEN: usize>(
10895 i += 1 ;
10996 }
11097
111- let mut res: [ & [ u8 ] ; ITEMS ] = [ & [ ] ; ITEMS ] ;
98+ let mut res: [ & [ u8 ] ; ITEMS_LEN ] = [ & [ ] ; ITEMS_LEN ] ;
11299
113100 let mut i = 0 ;
114101 while i < res. len ( ) {
@@ -144,7 +131,10 @@ pub const fn parse_into_array<const ITEMS: usize, const DEDUP_LEN: usize>(
144131pub fn parse_into_vec ( mut data : & [ u8 ] ) -> Result < alloc:: vec:: Vec < & [ u8 ] > , Error > {
145132 use alloc:: { vec, vec:: Vec } ;
146133
147- let dedup_len = try_read_vlq ! ( data) ;
134+ let Header {
135+ items_len,
136+ dedup_len,
137+ } = Header :: parse ( & mut data) ?;
148138
149139 let mut dedup_index: Vec < & [ u8 ] > = vec ! [ & [ ] ; dedup_len] ;
150140
@@ -157,7 +147,6 @@ pub fn parse_into_vec(mut data: &[u8]) -> Result<alloc::vec::Vec<&[u8]>, Error>
157147 i += 1 ;
158148 }
159149
160- let items_len = parse_items_len ( data) ?;
161150 let mut res: Vec < & [ u8 ] > = vec ! [ & [ ] ; items_len] ;
162151
163152 let mut i = 0 ;
@@ -189,20 +178,15 @@ pub fn parse_into_vec(mut data: &[u8]) -> Result<alloc::vec::Vec<&[u8]>, Error>
189178#[ macro_export]
190179macro_rules! parse_into_slice {
191180 ( $data: expr) => { {
192- const ITEMS_LEN : usize = {
193- match $crate:: parse_items_len( $data) {
181+ const HEADER : $crate:: Header = {
182+ let mut data: & [ u8 ] = $data;
183+ match $crate:: Header :: parse( & mut data) {
194184 Ok ( v) => v,
195185 Err ( _) => panic!( "Failed to parse items len" ) ,
196186 }
197187 } ;
198- const DEDUP_LEN : usize = {
199- match $crate:: parse_dedup_len( $data) {
200- Ok ( v) => v,
201- Err ( _) => panic!( "Failed to parse dedup len" ) ,
202- }
203- } ;
204- const ITEMS : [ & [ u8 ] ; ITEMS_LEN ] = {
205- match $crate:: parse_into_array:: <ITEMS_LEN , DEDUP_LEN >( $data) {
188+ const ITEMS : [ & [ u8 ] ; { HEADER . items_len } ] = {
189+ match $crate:: parse_into_array:: <{ HEADER . items_len } , { HEADER . dedup_len } >( $data) {
206190 Ok ( v) => v,
207191 Err ( _) => panic!( "Failed to parse items" ) ,
208192 }
0 commit comments