1 //! Bit level parsers and combinators
2 //!
3 //! Bit parsing is handled by tweaking the input in most macros.
4 //! In byte level parsing, the input is generally a `&[u8]` passed from combinator
5 //! to combinator as the slices are manipulated.
6 //!
7 //! Bit parsers take a `(&[u8], usize)` as input. The first part of the tuple is a byte slice,
8 //! the second part is a bit offset in the first byte of the slice.
9 //!
10 //! By passing a pair like this, we can leverage most of the existing combinators, and avoid
11 //! transforming the whole slice to a vector of booleans. This should make it easy
12 //! to see a byte slice as a bit stream, and parse code points of arbitrary bit length.
13 //!
14 
15 /// Transforms its byte slice input into a bit stream for the underlying parser. This allows the
16 /// given bit stream parser to work on a byte slice input.
17 ///
18 /// Signature:
19 /// `bits!( parser ) => ( &[u8], (&[u8], usize) -> IResult<(&[u8], usize), T> ) -> IResult<&[u8], T>`
20 ///
21 /// ```
22 /// # #[macro_use] extern crate nom;
23 /// # use nom::{Err, Needed};
24 /// # fn main() {
25 ///  named!( take_4_bits<u8>, bits!( take_bits!( 4u8 ) ) );
26 ///
27 ///  let input = vec![0xAB, 0xCD, 0xEF, 0x12];
28 ///  let sl    = &input[..];
29 ///
30 ///  assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) ));
31 ///  assert_eq!(take_4_bits( &b""[..] ), Err(Err::Incomplete(Needed::new(1))));
32 /// # }
33 #[macro_export(local_inner_macros)]
34 macro_rules! bits (
35   ($i:expr, $submac:ident!( $($args:tt)* )) => ({
36     $crate::bits::bitsc($i, move |i| { $submac!(i, $($args)*) })
37   });
38   ($i:expr, $f:expr) => (
39     bits!($i, call!($f))
40   );
41 );
42 
43 /// Counterpart to `bits`, `bytes!` transforms its bit stream input into a byte slice for the underlying
44 /// parser, allowing byte-slice parsers to work on bit streams.
45 ///
46 /// Signature:
47 /// `bytes!( parser ) => ( (&[u8], usize), &[u8] -> IResult<&[u8], T> ) -> IResult<(&[u8], usize), T>`,
48 ///
49 /// A partial byte remaining in the input will be ignored and the given parser will start parsing
50 /// at the next full byte.
51 ///
52 /// ```
53 /// # #[macro_use] extern crate nom;
54 /// # use nom::combinator::rest;
55 /// # use nom::error::{Error, ErrorKind};
56 /// # fn main() {
57 ///
58 /// named!( parse<(u8, u8, &[u8])>,  bits!( tuple!(
59 ///    take_bits!(4u8),
60 ///    take_bits!(8u8),
61 ///    bytes!(rest::<_, Error<_>>)
62 /// )));
63 ///
64 ///  let input = &[0xde, 0xad, 0xbe, 0xaf];
65 ///
66 ///  assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) )));
67 /// # }
68 #[macro_export(local_inner_macros)]
69 macro_rules! bytes (
70   ($i:expr, $submac:ident!( $($args:tt)* )) => ({
71     $crate::bits::bytesc($i, move |i| { $submac!(i, $($args)*) })
72   });
73   ($i:expr, $f:expr) => (
74     bytes!($i, call!($f))
75   );
76 );
77 
78 /// Consumes the specified number of bits and returns them as the specified type.
79 ///
80 /// Signature:
81 /// `take_bits!(type, count) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>`
82 ///
83 /// ```
84 /// # #[macro_use] extern crate nom;
85 /// # fn main() {
86 /// named!(bits_pair<(&[u8], usize), (u8, u8)>, pair!( take_bits!(4u8), take_bits!(4u8) ) );
87 /// named!( take_pair<(u8, u8)>, bits!( bits_pair ) );
88 ///
89 /// let input = vec![0xAB, 0xCD, 0xEF];
90 /// let sl    = &input[..];
91 ///
92 /// assert_eq!(take_pair( sl ),       Ok((&sl[1..], (0xA, 0xB))) );
93 /// assert_eq!(take_pair( &sl[1..] ), Ok((&sl[2..], (0xC, 0xD))) );
94 /// # }
95 /// ```
96 #[macro_export(local_inner_macros)]
97 macro_rules! take_bits (
98   ($i:expr, $count:expr) => (
99     {
100       let res: $crate::IResult<_, _> = $crate::bits::streaming::take($count)($i);
101       res
102     }
103   );
104 );
105 
106 /// Matches the given bit pattern.
107 ///
108 /// Signature:
109 /// `tag_bits!(type, count, pattern) => ( (&[T], usize), U, usize, U) -> IResult<(&[T], usize), U>`
110 ///
111 /// The caller must specify the number of bits to consume. The matched value is included in the
112 /// result on success.
113 ///
114 /// ```
115 /// # #[macro_use] extern crate nom;
116 /// # fn main() {
117 ///  named!( take_a<u8>, bits!( tag_bits!(4usize, 0xA) ) );
118 ///
119 ///  let input = vec![0xAB, 0xCD, 0xEF];
120 ///  let sl    = &input[..];
121 ///
122 ///  assert_eq!(take_a( sl ),       Ok((&sl[1..], 0xA)) );
123 /// # }
124 /// ```
125 #[macro_export(local_inner_macros)]
126 macro_rules! tag_bits (
127   ($i:expr, $count:expr, $p: expr) => (
128     {
129       let res: $crate::IResult<_, _> = $crate::bits::streaming::tag($p, $count)($i);
130       res
131     }
132   )
133 );
134 
135 #[cfg(test)]
136 mod tests {
137   use crate::error::ErrorKind;
138   use crate::internal::{Err, IResult, Needed};
139   use crate::lib::std::ops::{AddAssign, Shl, Shr};
140 
141   #[test]
142   fn take_bits() {
143     let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11];
144     let sl = &input[..];
145 
146     assert_eq!(take_bits!((sl, 0), 0u8), Ok(((sl, 0), 0)));
147     assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170)));
148     assert_eq!(take_bits!((sl, 0), 3u8), Ok(((&sl[0..], 3), 5)));
149     assert_eq!(take_bits!((sl, 0), 6u8), Ok(((&sl[0..], 6), 42)));
150     assert_eq!(take_bits!((sl, 1), 1u8), Ok(((&sl[0..], 2), 0)));
151     assert_eq!(take_bits!((sl, 1), 2u8), Ok(((&sl[0..], 3), 1)));
152     assert_eq!(take_bits!((sl, 1), 3u8), Ok(((&sl[0..], 4), 2)));
153     assert_eq!(take_bits!((sl, 6), 3u8), Ok(((&sl[1..], 1), 5)));
154     assert_eq!(take_bits!((sl, 0), 10u8), Ok(((&sl[1..], 2), 683)));
155     assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170)));
156     assert_eq!(take_bits!((sl, 6), 10u8), Ok(((&sl[2..], 0), 752)));
157     assert_eq!(take_bits!((sl, 6), 11u8), Ok(((&sl[2..], 1), 1504)));
158     assert_eq!(take_bits!((sl, 0), 20u8), Ok(((&sl[2..], 4), 700_163)));
159     assert_eq!(take_bits!((sl, 4), 20u8), Ok(((&sl[3..], 0), 716_851)));
160     let r: IResult<_, u32> = take_bits!((sl, 4), 22u8);
161     assert_eq!(r, Err(Err::Incomplete(Needed::new(22))));
162   }
163 
164   #[test]
165   fn tag_bits() {
166     let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11];
167     let sl = &input[..];
168 
169     assert_eq!(tag_bits!((sl, 0), 3u8, 0b101), Ok(((&sl[0..], 3), 5)));
170     assert_eq!(tag_bits!((sl, 0), 4u8, 0b1010), Ok(((&sl[0..], 4), 10)));
171   }
172 
173   named!(ch<(&[u8],usize),(u8,u8)>,
174     do_parse!(
175       tag_bits!(3u8, 0b101) >>
176       x: take_bits!(4u8)    >>
177       y: take_bits!(5u8)    >>
178       (x,y)
179     )
180   );
181 
182   #[test]
183   fn chain_bits() {
184     let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11];
185     let sl = &input[..];
186     assert_eq!(ch((&input[..], 0)), Ok(((&sl[1..], 4), (5, 15))));
187     assert_eq!(ch((&input[..], 4)), Ok(((&sl[2..], 0), (7, 16))));
188     assert_eq!(ch((&input[..1], 0)), Err(Err::Incomplete(Needed::new(5))));
189   }
190 
191   named!(ch_bytes<(u8, u8)>, bits!(ch));
192   #[test]
193   fn bits_to_bytes() {
194     let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11];
195     assert_eq!(ch_bytes(&input[..]), Ok((&input[2..], (5, 15))));
196     assert_eq!(ch_bytes(&input[..1]), Err(Err::Incomplete(Needed::new(1))));
197     assert_eq!(
198       ch_bytes(&input[1..]),
199       Err(Err::Error(error_position!(&input[1..], ErrorKind::TagBits)))
200     );
201   }
202 
203   named!(
204     bits_bytes_bs,
205     bits!(bytes!(
206       crate::combinator::rest::<_, crate::error::Error<&[u8]>>
207     ))
208   );
209   #[test]
210   fn bits_bytes() {
211     let input = [0b10_10_10_10];
212     assert_eq!(
213       bits_bytes_bs(&input[..]),
214       Ok((&[][..], &[0b10_10_10_10][..]))
215     );
216   }
217 
218   #[derive(PartialEq, Debug)]
219   struct FakeUint(u32);
220 
221   impl AddAssign for FakeUint {
222     fn add_assign(&mut self, other: FakeUint) {
223       *self = FakeUint(self.0 + other.0);
224     }
225   }
226 
227   impl Shr<usize> for FakeUint {
228     type Output = FakeUint;
229 
230     fn shr(self, shift: usize) -> FakeUint {
231       FakeUint(self.0 >> shift)
232     }
233   }
234 
235   impl Shl<usize> for FakeUint {
236     type Output = FakeUint;
237 
238     fn shl(self, shift: usize) -> FakeUint {
239       FakeUint(self.0 << shift)
240     }
241   }
242 
243   impl From<u8> for FakeUint {
244     fn from(i: u8) -> FakeUint {
245       FakeUint(u32::from(i))
246     }
247   }
248 
249   #[test]
250   fn non_privitive_type() {
251     let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11];
252     let sl = &input[..];
253 
254     assert_eq!(
255       take_bits!((sl, 0), 20u8),
256       Ok(((&sl[2..], 4), FakeUint(700_163)))
257     );
258     assert_eq!(
259       take_bits!((sl, 4), 20u8),
260       Ok(((&sl[3..], 0), FakeUint(716_851)))
261     );
262     let r3: IResult<_, FakeUint> = take_bits!((sl, 4), 22u8);
263     assert_eq!(r3, Err(Err::Incomplete(Needed::new(22))));
264   }
265 }
266