1 //! Bit level parsers
2 //!
3 
4 use crate::error::{ErrorKind, ParseError};
5 use crate::internal::{Err, IResult, Needed};
6 use crate::lib::std::ops::{AddAssign, Div, RangeFrom, Shl, Shr};
7 use crate::traits::{InputIter, InputLength, Slice, ToUsize};
8 
9 /// Generates a parser taking `count` bits
take<I, O, C, E: ParseError<(I, usize)>>( count: C, ) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength, C: ToUsize, O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,10 pub fn take<I, O, C, E: ParseError<(I, usize)>>(
11   count: C,
12 ) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
13 where
14   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
15   C: ToUsize,
16   O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
17 {
18   let count = count.to_usize();
19   move |(input, bit_offset): (I, usize)| {
20     if count == 0 {
21       Ok(((input, bit_offset), 0u8.into()))
22     } else {
23       let cnt = (count + bit_offset).div(8);
24       if input.input_len() * 8 < count + bit_offset {
25         Err(Err::Incomplete(Needed::new(count as usize)))
26       } else {
27         let mut acc: O = (0 as u8).into();
28         let mut offset: usize = bit_offset;
29         let mut remaining: usize = count;
30         let mut end_offset: usize = 0;
31 
32         for byte in input.iter_elements().take(cnt + 1) {
33           if remaining == 0 {
34             break;
35           }
36           let val: O = if offset == 0 {
37             byte.into()
38           } else {
39             ((byte << offset) as u8 >> offset).into()
40           };
41 
42           if remaining < 8 - offset {
43             acc += val >> (8 - offset - remaining);
44             end_offset = remaining + offset;
45             break;
46           } else {
47             acc += val << (remaining - (8 - offset));
48             remaining -= 8 - offset;
49             offset = 0;
50           }
51         }
52         Ok(((input.slice(cnt..), end_offset), acc))
53       }
54     }
55   }
56 }
57 
58 /// Generates a parser taking `count` bits and comparing them to `pattern`
tag<I, O, C, E: ParseError<(I, usize)>>( pattern: O, count: C, ) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone, C: ToUsize, O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,59 pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
60   pattern: O,
61   count: C,
62 ) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
63 where
64   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + Clone,
65   C: ToUsize,
66   O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
67 {
68   let count = count.to_usize();
69   move |input: (I, usize)| {
70     let inp = input.clone();
71 
72     take(count)(input).and_then(|(i, o)| {
73       if pattern == o {
74         Ok((i, o))
75       } else {
76         Err(Err::Error(error_position!(inp, ErrorKind::TagBits)))
77       }
78     })
79   }
80 }
81