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 until the slices are manipulated. 6 //! 7 //! Bit parsers take a `(&[u8], usize)` as input. The first part of the tuple is an 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 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 /// `bits!( parser ) => ( &[u8], (&[u8], usize) -> IResult<(&[u8], usize), T> ) -> IResult<&[u8], T>` 16 /// transforms its byte slice input into a bit stream for the underlying parsers 17 /// 18 /// ``` 19 /// # #[macro_use] extern crate nom; 20 /// # use nom::IResult::Done; 21 /// # fn main() { 22 /// named!( take_3_bits<u8>, bits!( take_bits!( u8, 3 ) ) ); 23 /// 24 /// let input = vec![0b10101010, 0b11110000, 0b00110011]; 25 /// let sl = &input[..]; 26 /// 27 /// assert_eq!(take_3_bits( sl ), Done(&sl[1..], 5) ); 28 /// # } 29 #[macro_export] 30 macro_rules! bits ( 31 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 32 bits_impl!($i, $submac!($($args)*)); 33 ); 34 ($i:expr, $f:expr) => ( 35 bits_impl!($i, call!($f)); 36 ); 37 ); 38 39 #[cfg(feature = "verbose-errors")] 40 /// Internal parser, do not use directly 41 #[doc(hidden)] 42 #[macro_export] 43 macro_rules! bits_impl ( 44 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 45 { 46 let input = ($i, 0usize); 47 match $submac!(input, $($args)*) { 48 $crate::IResult::Error(e) => { 49 let err = match e { 50 $crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k), 51 $crate::Err::Position(k, (i,b)) | $crate::Err::NodePosition(k, (i,b), _) => { 52 $crate::Err::Position(k, &i[b/8..]) 53 } 54 }; 55 $crate::IResult::Error(err) 56 } 57 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 58 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 59 //println!("bits parser returned Needed::Size({})", i); 60 $crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1)) 61 }, 62 $crate::IResult::Done((i, bit_index), o) => { 63 let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ; 64 //println!("bit index=={} => byte index=={}", bit_index, byte_index); 65 $crate::IResult::Done(&i[byte_index..], o) 66 } 67 } 68 } 69 ); 70 ); 71 72 #[cfg(not(feature = "verbose-errors"))] 73 /// Internal parser, do not use directly 74 #[doc(hidden)] 75 #[macro_export] 76 macro_rules! bits_impl ( 77 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 78 { 79 let input = ($i, 0usize); 80 match $submac!(input, $($args)*) { 81 $crate::IResult::Error(e) => { 82 $crate::IResult::Error(e) 83 } 84 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 85 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 86 //println!("bits parser returned Needed::Size({})", i); 87 $crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1)) 88 }, 89 $crate::IResult::Done((i, bit_index), o) => { 90 let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ; 91 //println!("bit index=={} => byte index=={}", bit_index, byte_index); 92 $crate::IResult::Done(&i[byte_index..], o) 93 } 94 } 95 } 96 ); 97 ); 98 99 /// Counterpart to bits, 100 /// `bytes!( parser ) => ( (&[u8], usize), &[u8] -> IResult<&[u8], T> ) -> IResult<(&[u8], usize), T>`, 101 /// transforms its bits stream input into a byte slice for the underlying parsers. If we start in the 102 /// middle of a byte throws away the bits until the end of the byte. 103 /// 104 /// ``` 105 /// # #[macro_use] extern crate nom; 106 /// # use nom::IResult::Done; 107 /// # use nom::rest; 108 /// # fn main() { 109 /// named!( parse<(u8, u8, &[u8])>, bits!( tuple!( 110 /// take_bits!(u8, 4), 111 /// take_bits!(u8, 8), 112 /// bytes!(rest) 113 /// ))); 114 /// 115 /// let input = &[0xde, 0xad, 0xbe, 0xaf]; 116 /// 117 /// assert_eq!(parse( input ), Done(&[][..], (0xd, 0xea, &[0xbe, 0xaf][..]))); 118 /// # } 119 #[macro_export] 120 macro_rules! bytes ( 121 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 122 bytes_impl!($i, $submac!($($args)*)); 123 ); 124 ($i:expr, $f:expr) => ( 125 bytes_impl!($i, call!($f)); 126 ); 127 ); 128 129 #[cfg(feature = "verbose-errors")] 130 /// Internal parser, do not use directly 131 #[doc(hidden)] 132 #[macro_export] 133 macro_rules! bytes_impl ( 134 ($macro_i:expr, $submac:ident!( $($args:tt)* )) => ( 135 { 136 let inp; 137 if $macro_i.1 % 8 != 0 { 138 inp = & $macro_i.0[1 + $macro_i.1 / 8 ..]; 139 } 140 else { 141 inp = & $macro_i.0[$macro_i.1 / 8 ..]; 142 } 143 144 match $submac!(inp, $($args)*) { 145 $crate::IResult::Error(e) => { 146 let err = match e { 147 $crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k), 148 $crate::Err::Position(k, i) | $crate::Err::NodePosition(k, i, _) => { 149 $crate::Err::Position(k, (i, 0)) 150 } 151 }; 152 $crate::IResult::Error(err) 153 } 154 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 155 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 156 $crate::IResult::Incomplete($crate::Needed::Size(i * 8)) 157 }, 158 $crate::IResult::Done(i, o) => { 159 $crate::IResult::Done((i, 0), o) 160 } 161 } 162 } 163 ); 164 ); 165 166 #[cfg(not(feature = "verbose-errors"))] 167 /// Internal parser, do not use directly 168 #[doc(hidden)] 169 #[macro_export] 170 macro_rules! bytes_impl ( 171 ($macro_i:expr, $submac:ident!( $($args:tt)* )) => ( 172 { 173 let inp; 174 if $macro_i.1 % 8 != 0 { 175 inp = & $macro_i.0[1 + $macro_i.1 / 8 ..]; 176 } 177 else { 178 inp = & $macro_i.0[$macro_i.1 / 8 ..]; 179 } 180 181 match $submac!(inp, $($args)*) { 182 $crate::IResult::Error(e) => { 183 $crate::IResult::Error(e) 184 } 185 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 186 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 187 $crate::IResult::Incomplete($crate::Needed::Size(i * 8)) 188 }, 189 $crate::IResult::Done(i, o) => { 190 $crate::IResult::Done((i, 0), o) 191 } 192 } 193 } 194 ); 195 ); 196 197 /// `take_bits!(type, nb) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>` 198 /// generates a parser consuming the specified number of bits. 199 /// 200 /// ``` 201 /// # #[macro_use] extern crate nom; 202 /// # use nom::IResult::Done; 203 /// # fn main() { 204 /// named!( take_pair<(u8, u8)>, bits!( pair!( take_bits!( u8, 3 ), take_bits!(u8, 5) ) ) ); 205 /// 206 /// let input = vec![0b10101010, 0b11110000, 0b00110011]; 207 /// let sl = &input[..]; 208 /// 209 /// assert_eq!(take_pair( sl ), Done(&sl[1..], (5, 10)) ); 210 /// assert_eq!(take_pair( &sl[1..] ), Done(&sl[2..], (7, 16)) ); 211 /// # } 212 /// ``` 213 #[macro_export] 214 macro_rules! take_bits ( 215 ($i:expr, $t:ty, $count:expr) => ( 216 { 217 use std::ops::Div; 218 use std::convert::Into; 219 //println!("taking {} bits from {:?}", $count, $i); 220 let (input, bit_offset) = $i; 221 let res : $crate::IResult<(&[u8],usize), $t> = if $count == 0 { 222 $crate::IResult::Done( (input, bit_offset), (0 as u8).into()) 223 } else { 224 let cnt = ($count as usize + bit_offset).div(8); 225 if input.len() * 8 < $count as usize + bit_offset { 226 //println!("returning incomplete: {}", $count as usize + bit_offset); 227 $crate::IResult::Incomplete($crate::Needed::Size($count as usize)) 228 } else { 229 let mut acc:$t = (0 as u8).into(); 230 let mut offset: usize = bit_offset; 231 let mut remaining: usize = $count; 232 let mut end_offset: usize = 0; 233 234 for byte in input.iter().take(cnt + 1) { 235 if remaining == 0 { 236 break; 237 } 238 let val: $t = if offset == 0 { 239 (*byte as u8).into() 240 } else { 241 (((*byte as u8) << offset) as u8 >> offset).into() 242 }; 243 244 if remaining < 8 - offset { 245 acc += val >> (8 - offset - remaining); 246 end_offset = remaining + offset; 247 break; 248 } else { 249 acc += val << (remaining - (8 - offset)); 250 remaining -= 8 - offset; 251 offset = 0; 252 } 253 } 254 $crate::IResult::Done( (&input[cnt..], end_offset) , acc) 255 } 256 }; 257 res 258 } 259 ); 260 ); 261 262 /// matches an integer pattern to a bitstream. The number of bits of the input to compare must be specified 263 #[macro_export] 264 macro_rules! tag_bits ( 265 ($i:expr, $t:ty, $count:expr, $p: pat) => ( 266 { 267 match take_bits!($i, $t, $count) { 268 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 269 $crate::IResult::Done(i, o) => { 270 if let $p = o { 271 let res: $crate::IResult<(&[u8],usize),$t> = $crate::IResult::Done(i, o); 272 res 273 } else { 274 $crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i)) 275 } 276 }, 277 _ => { 278 $crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i)) 279 } 280 } 281 } 282 ) 283 ); 284 285 #[cfg(test)] 286 mod tests { 287 use std::ops::{Shr,Shl,AddAssign}; 288 use internal::{IResult,Needed}; 289 use ErrorKind; 290 291 #[test] take_bits()292 fn take_bits() { 293 let input = [0b10101010, 0b11110000, 0b00110011]; 294 let sl = &input[..]; 295 296 assert_eq!(take_bits!( (sl, 0), u8, 0 ), IResult::Done((sl, 0), 0)); 297 assert_eq!(take_bits!( (sl, 0), u8, 8 ), IResult::Done((&sl[1..], 0), 170)); 298 assert_eq!(take_bits!( (sl, 0), u8, 3 ), IResult::Done((&sl[0..], 3), 5)); 299 assert_eq!(take_bits!( (sl, 0), u8, 6 ), IResult::Done((&sl[0..], 6), 42)); 300 assert_eq!(take_bits!( (sl, 1), u8, 1 ), IResult::Done((&sl[0..], 2), 0)); 301 assert_eq!(take_bits!( (sl, 1), u8, 2 ), IResult::Done((&sl[0..], 3), 1)); 302 assert_eq!(take_bits!( (sl, 1), u8, 3 ), IResult::Done((&sl[0..], 4), 2)); 303 assert_eq!(take_bits!( (sl, 6), u8, 3 ), IResult::Done((&sl[1..], 1), 5)); 304 assert_eq!(take_bits!( (sl, 0), u16, 10 ), IResult::Done((&sl[1..], 2), 683)); 305 assert_eq!(take_bits!( (sl, 0), u16, 8 ), IResult::Done((&sl[1..], 0), 170)); 306 assert_eq!(take_bits!( (sl, 6), u16, 10 ), IResult::Done((&sl[2..], 0), 752)); 307 assert_eq!(take_bits!( (sl, 6), u16, 11 ), IResult::Done((&sl[2..], 1), 1504)); 308 assert_eq!(take_bits!( (sl, 0), u32, 20 ), IResult::Done((&sl[2..], 4), 700163)); 309 assert_eq!(take_bits!( (sl, 4), u32, 20 ), IResult::Done((&sl[3..], 0), 716851)); 310 assert_eq!(take_bits!( (sl, 4), u32, 22 ), IResult::Incomplete(Needed::Size(22))); 311 } 312 313 #[test] tag_bits()314 fn tag_bits() { 315 let input = [0b10101010, 0b11110000, 0b00110011]; 316 let sl = &input[..]; 317 318 assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), IResult::Done((&sl[0..], 3), 5)); 319 assert_eq!(tag_bits!( (sl, 0), u8, 4, 0b1010), IResult::Done((&sl[0..], 4), 10)); 320 } 321 322 named!(ch<(&[u8],usize),(u8,u8)>, 323 do_parse!( 324 tag_bits!(u8, 3, 0b101) >> 325 x: take_bits!(u8, 4) >> 326 y: take_bits!(u8, 5) >> 327 (x,y) 328 ) 329 ); 330 331 #[test] chain_bits()332 fn chain_bits() { 333 let input = [0b10101010, 0b11110000, 0b00110011]; 334 let sl = &input[..]; 335 assert_eq!(ch((&input[..],0)), IResult::Done((&sl[1..], 4), (5,15))); 336 assert_eq!(ch((&input[..],4)), IResult::Done((&sl[2..], 0), (7,16))); 337 assert_eq!(ch((&input[..1],0)), IResult::Incomplete(Needed::Size(12))); 338 } 339 340 named!(ch_bytes<(u8,u8)>, bits!(ch)); 341 #[test] bits_to_bytes()342 fn bits_to_bytes() { 343 let input = [0b10101010, 0b11110000, 0b00110011]; 344 assert_eq!(ch_bytes(&input[..]), IResult::Done(&input[2..], (5,15))); 345 assert_eq!(ch_bytes(&input[..1]), IResult::Incomplete(Needed::Size(2))); 346 assert_eq!(ch_bytes(&input[1..]), IResult::Error(error_position!(ErrorKind::TagBits, &input[1..]))); 347 } 348 349 #[derive(PartialEq,Debug)] 350 struct FakeUint(u32); 351 352 impl AddAssign for FakeUint { 353 add_assign(&mut self, other: FakeUint)354 fn add_assign(&mut self, other: FakeUint) { 355 *self = FakeUint(&self.0 + other.0); 356 } 357 358 } 359 360 impl Shr<usize> for FakeUint { 361 type Output = FakeUint; 362 shr(self, shift: usize) -> FakeUint363 fn shr(self, shift: usize) -> FakeUint { 364 FakeUint(&self.0 >> shift) 365 } 366 367 } 368 369 impl Shl<usize> for FakeUint { 370 type Output = FakeUint; 371 shl(self, shift: usize) -> FakeUint372 fn shl(self, shift: usize) -> FakeUint { 373 FakeUint(&self.0 << shift) 374 } 375 376 } 377 378 impl From<u8> for FakeUint { 379 from(i: u8) -> FakeUint380 fn from(i: u8) -> FakeUint { 381 FakeUint(u32::from(i)) 382 } 383 } 384 385 #[test] non_privitive_type()386 fn non_privitive_type() { 387 let input = [0b10101010, 0b11110000, 0b00110011]; 388 let sl = &input[..]; 389 390 assert_eq!(take_bits!( (sl, 0), FakeUint, 20 ), IResult::Done((&sl[2..], 4), FakeUint(700163))); 391 assert_eq!(take_bits!( (sl, 4), FakeUint, 20 ), IResult::Done((&sl[3..], 0), FakeUint(716851))); 392 assert_eq!(take_bits!( (sl, 4), FakeUint, 22 ), IResult::Incomplete(Needed::Size(22))); 393 } 394 } 395