flat_rs/decode/
decoder.rs

1use crate::{decode::Decode, zigzag};
2
3use super::Error;
4
5#[derive(Debug)]
6pub struct Decoder<'b> {
7    pub buffer: &'b [u8],
8    pub used_bits: i64,
9    pub pos: usize,
10}
11
12impl<'b> Decoder<'b> {
13    pub fn new(bytes: &'b [u8]) -> Decoder {
14        Decoder {
15            buffer: bytes,
16            pos: 0,
17            used_bits: 0,
18        }
19    }
20
21    /// Decode any type that implements [`Decode`].
22    pub fn decode<T: Decode<'b>>(&mut self) -> Result<T, Error> {
23        T::decode(self)
24    }
25
26    /// Decode an integer of any size.
27    /// This is byte alignment agnostic.
28    /// First we decode the next 8 bits of the buffer.
29    /// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
30    /// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
31    /// filling in the next 7 least significant bits of the unsigned integer and so on.
32    /// If the most significant bit was instead 0 we stop decoding any more bits.
33    /// Finally we use zigzag to convert the unsigned integer back to a signed integer.
34    pub fn integer(&mut self) -> Result<isize, Error> {
35        Ok(zigzag::to_isize(self.word()?))
36    }
37
38    /// Decode an integer of 128 bits size.
39    /// This is byte alignment agnostic.
40    /// First we decode the next 8 bits of the buffer.
41    /// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
42    /// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
43    /// filling in the next 7 least significant bits of the unsigned integer and so on.
44    /// If the most significant bit was instead 0 we stop decoding any more bits.
45    /// Finally we use zigzag to convert the unsigned integer back to a signed integer.
46    pub fn big_integer(&mut self) -> Result<i128, Error> {
47        Ok(zigzag::to_i128(self.big_word()?))
48    }
49
50    /// Decode a single bit of the buffer to get a bool.
51    /// We mask out a single bit of the buffer based on used bits.
52    /// and check if it is 0 for false or 1 for true.
53    // TODO: use bit() instead of this custom implementation.
54    pub fn bool(&mut self) -> Result<bool, Error> {
55        let current_byte = self.buffer[self.pos];
56        let b = 0 != (current_byte & (128 >> self.used_bits));
57        self.increment_buffer_by_bit();
58        Ok(b)
59    }
60
61    /// Decode a byte from the buffer.
62    /// This byte alignment agnostic.
63    /// We use the next 8 bits in the buffer and return the resulting byte.
64    pub fn u8(&mut self) -> Result<u8, Error> {
65        self.bits8(8)
66    }
67
68    /// Decode a byte array.
69    /// Decodes a filler to byte align the buffer,
70    /// then decodes the next byte to get the array length up to a max of 255.
71    /// We decode bytes equal to the array length to form the byte array.
72    /// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
73    /// We stop once we hit a byte array length of 0.
74    /// If array length is 0 for first byte array length the we return a empty array.
75    pub fn bytes(&mut self) -> Result<Vec<u8>, Error> {
76        self.filler()?;
77        self.byte_array()
78    }
79
80    /// Decode a 32 bit char.
81    /// This is byte alignment agnostic.
82    /// First we decode the next 8 bits of the buffer.
83    /// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
84    /// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
85    /// filling in the next 7 least significant bits of the unsigned integer and so on.
86    /// If the most significant bit was instead 0 we stop decoding any more bits.
87    pub fn char(&mut self) -> Result<char, Error> {
88        let character = self.word()? as u32;
89
90        char::from_u32(character).ok_or(Error::DecodeChar(character))
91    }
92
93    // TODO: Do we need this?
94    pub fn string(&mut self) -> Result<String, Error> {
95        let mut s = String::new();
96        while self.bit()? {
97            s += &self.char()?.to_string();
98        }
99        Ok(s)
100    }
101
102    /// Decode a string.
103    /// Convert to byte array and then use byte array decoding.
104    /// Decodes a filler to byte align the buffer,
105    /// then decodes the next byte to get the array length up to a max of 255.
106    /// We decode bytes equal to the array length to form the byte array.
107    /// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
108    /// We stop once we hit a byte array length of 0.
109    /// If array length is 0 for first byte array length the we return a empty array.
110    pub fn utf8(&mut self) -> Result<String, Error> {
111        // TODO: Better Error Handling
112        String::from_utf8(Vec::<u8>::decode(self)?).map_err(Error::from)
113    }
114
115    /// Decodes a filler of max one byte size.
116    /// Decodes bits until we hit a bit that is 1.
117    /// Expects that the 1 is at the end of the current byte in the buffer.
118    pub fn filler(&mut self) -> Result<(), Error> {
119        while self.zero()? {}
120        Ok(())
121    }
122
123    /// Decode a word of any size.
124    /// This is byte alignment agnostic.
125    /// First we decode the next 8 bits of the buffer.
126    /// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
127    /// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
128    /// filling in the next 7 least significant bits of the unsigned integer and so on.
129    /// If the most significant bit was instead 0 we stop decoding any more bits.
130    pub fn word(&mut self) -> Result<usize, Error> {
131        let mut leading_bit = 1;
132        let mut final_word: usize = 0;
133        let mut shl: usize = 0;
134        // continue looping if lead bit is 1 which is 128 as a u8 otherwise exit
135        while leading_bit > 0 {
136            let word8 = self.bits8(8)?;
137            let word7 = word8 & 127;
138            final_word |= (word7 as usize) << shl;
139            shl += 7;
140            leading_bit = word8 & 128;
141        }
142        Ok(final_word)
143    }
144
145    /// Decode a word of 128 bits size.
146    /// This is byte alignment agnostic.
147    /// First we decode the next 8 bits of the buffer.
148    /// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
149    /// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
150    /// filling in the next 7 least significant bits of the unsigned integer and so on.
151    /// If the most significant bit was instead 0 we stop decoding any more bits.
152    pub fn big_word(&mut self) -> Result<u128, Error> {
153        let mut leading_bit = 1;
154        let mut final_word: u128 = 0;
155        let mut shl: u128 = 0;
156        // continue looping if lead bit is 1 which is 128 as a u8 otherwise exit
157        while leading_bit > 0 {
158            let word8 = self.bits8(8)?;
159            let word7 = word8 & 127;
160            final_word |= (word7 as u128) << shl;
161            shl += 7;
162            leading_bit = word8 & 128;
163        }
164        Ok(final_word)
165    }
166
167    /// Decode a list of items with a decoder function.
168    /// This is byte alignment agnostic.
169    /// Decode a bit from the buffer.
170    /// If 0 then stop.
171    /// Otherwise we decode an item in the list with the decoder function passed in.
172    /// Then decode the next bit in the buffer and repeat above.
173    /// Returns a list of items decoded with the decoder function.
174    pub fn decode_list_with<T: Decode<'b>, F>(&mut self, decoder_func: F) -> Result<Vec<T>, Error>
175    where
176        F: Copy + FnOnce(&mut Decoder) -> Result<T, Error>,
177    {
178        let mut vec_array: Vec<T> = Vec::new();
179        while self.bit()? {
180            vec_array.push(decoder_func(self)?)
181        }
182        Ok(vec_array)
183    }
184
185    /// Decode the next bit in the buffer.
186    /// If the bit was 0 then return true.
187    /// Otherwise return false.
188    /// Throws EndOfBuffer error if used at the end of the array.
189    fn zero(&mut self) -> Result<bool, Error> {
190        let current_bit = self.bit()?;
191
192        Ok(!current_bit)
193    }
194
195    /// Decode the next bit in the buffer.
196    /// If the bit was 1 then return true.
197    /// Otherwise return false.
198    /// Throws EndOfBuffer error if used at the end of the array.
199    fn bit(&mut self) -> Result<bool, Error> {
200        if self.pos >= self.buffer.len() {
201            return Err(Error::EndOfBuffer);
202        }
203
204        let b = self.buffer[self.pos] & (128 >> self.used_bits) > 0;
205
206        self.increment_buffer_by_bit();
207
208        Ok(b)
209    }
210
211    /// Decode a byte array.
212    /// Throws a BufferNotByteAligned error if the buffer is not byte aligned
213    /// Decodes the next byte to get the array length up to a max of 255.
214    /// We decode bytes equal to the array length to form the byte array.
215    /// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
216    /// We stop once we hit a byte array length of 0.
217    /// If array length is 0 for first byte array length the we return a empty array.
218    fn byte_array(&mut self) -> Result<Vec<u8>, Error> {
219        if self.used_bits != 0 {
220            return Err(Error::BufferNotByteAligned);
221        }
222
223        self.ensure_bytes(1)?;
224
225        let mut blk_len = self.buffer[self.pos];
226
227        self.pos += 1;
228
229        let mut blk_array: Vec<u8> = Vec::new();
230
231        while blk_len != 0 {
232            self.ensure_bytes(blk_len as usize + 1)?;
233
234            let decoded_array = &self.buffer[self.pos..self.pos + blk_len as usize];
235
236            blk_array.extend(decoded_array);
237
238            self.pos += blk_len as usize;
239
240            blk_len = self.buffer[self.pos];
241
242            self.pos += 1
243        }
244
245        Ok(blk_array)
246    }
247
248    /// Decode up to 8 bits.
249    /// This is byte alignment agnostic.
250    /// If num_bits is greater than the 8 we throw an IncorrectNumBits error.
251    /// First we decode the next num_bits of bits in the buffer.
252    /// If there are less unused bits in the current byte in the buffer than num_bits,
253    /// then we decode the remaining bits from the most significant bits in the next byte in the buffer.
254    /// Otherwise we decode the unused bits from the current byte.
255    /// Returns the decoded value up to a byte in size.
256    pub fn bits8(&mut self, num_bits: usize) -> Result<u8, Error> {
257        if num_bits > 8 {
258            return Err(Error::IncorrectNumBits);
259        }
260
261        self.ensure_bits(num_bits)?;
262
263        let unused_bits = 8 - self.used_bits as usize;
264        let leading_zeroes = 8 - num_bits;
265        let r = (self.buffer[self.pos] << self.used_bits as usize) >> leading_zeroes;
266
267        let x = if num_bits > unused_bits {
268            r | (self.buffer[self.pos + 1] >> (unused_bits + leading_zeroes))
269        } else {
270            r
271        };
272
273        self.drop_bits(num_bits);
274
275        Ok(x)
276    }
277
278    /// Ensures the buffer has the required bytes passed in by required_bytes.
279    /// Throws a NotEnoughBytes error if there are less bytes remaining in the buffer than required_bytes.
280    fn ensure_bytes(&mut self, required_bytes: usize) -> Result<(), Error> {
281        if required_bytes as isize > self.buffer.len() as isize - self.pos as isize {
282            Err(Error::NotEnoughBytes(required_bytes))
283        } else {
284            Ok(())
285        }
286    }
287
288    /// Ensures the buffer has the required bits passed in by required_bits.
289    /// Throws a NotEnoughBits error if there are less bits remaining in the buffer than required_bits.
290    fn ensure_bits(&mut self, required_bits: usize) -> Result<(), Error> {
291        if required_bits as isize
292            > (self.buffer.len() as isize - self.pos as isize) * 8 - self.used_bits as isize
293        {
294            Err(Error::NotEnoughBits(required_bits))
295        } else {
296            Ok(())
297        }
298    }
299
300    /// Increment buffer by num_bits.
301    /// If num_bits + used bits is greater than 8,
302    /// then increment position by (num_bits + used bits) / 8
303    /// Use the left over remainder as the new amount of used bits.
304    fn drop_bits(&mut self, num_bits: usize) {
305        let all_used_bits = num_bits as i64 + self.used_bits;
306        self.used_bits = all_used_bits % 8;
307        self.pos += all_used_bits as usize / 8;
308    }
309
310    /// Increment used bits by 1.
311    /// If all 8 bits are used then increment buffer position by 1.
312    fn increment_buffer_by_bit(&mut self) {
313        if self.used_bits == 7 {
314            self.pos += 1;
315            self.used_bits = 0;
316        } else {
317            self.used_bits += 1;
318        }
319    }
320}