]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: simplify bits parser annotations 6814/head
authorPierre Chifflier <chifflier@wzdftpd.net>
Tue, 18 Jan 2022 21:38:19 +0000 (22:38 +0100)
committerPierre Chifflier <chifflier@wzdftpd.net>
Wed, 19 Jan 2022 08:13:11 +0000 (09:13 +0100)
rust/src/http2/parser.rs
rust/src/mqtt/parser.rs
rust/src/nfs/rpc_records.rs

index 77caa5684c95791dca94a76c4cf424e3d1a97636..f228edfa5bdb60a33bd415afee75f1a76e1f1338 100644 (file)
  */
 
 use super::huffman;
+use crate::common::nom7::bits;
 use crate::http2::http2::{HTTP2DynTable, HTTP2_MAX_TABLESIZE};
-use nom7::bits::{bits, streaming::take as take_bits};
+use nom7::bits::streaming::take as take_bits;
 use nom7::branch::alt;
 use nom7::bytes::streaming::{is_a, is_not, tag, take, take_while};
 use nom7::character::complete::digit1;
 use nom7::combinator::{complete, cond, map_opt, opt, rest, verify};
-use nom7::error::{make_error, Error, ErrorKind};
+use nom7::error::{make_error, ErrorKind};
 use nom7::multi::many0;
 use nom7::number::streaming::{be_u16, be_u24, be_u32, be_u8};
 use nom7::sequence::tuple;
@@ -357,7 +358,7 @@ fn http2_parse_headers_block_indexed<'a>(
     input: &'a [u8], dyn_headers: &HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FrameHeaderBlock> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(complete(tuple((
+        bits(complete(tuple((
             verify(take_bits(1u8), |&x| x == 1),
             take_bits(7u8),
         ))))(input)
@@ -375,8 +376,7 @@ fn http2_parse_headers_block_indexed<'a>(
 
 fn http2_parse_headers_block_string(input: &[u8]) -> IResult<&[u8], Vec<u8>> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        let (i, b) = be_u8(input)?;
-        Ok((i, (b >> 7, b & 0x7f)))
+        bits(tuple((take_bits(1u8), take_bits(7u8))))(input)
     }
     let (i1, huffslen) = parser(input)?;
     let (i2, stringlen) = http2_parse_var_uint(i1, huffslen.1 as u64, 0x7F)?;
@@ -387,8 +387,7 @@ fn http2_parse_headers_block_string(input: &[u8]) -> IResult<&[u8], Vec<u8>> {
     if huffslen.0 == 0 {
         return Ok((i3, data.to_vec()));
     } else {
-        let (_, val) =
-            bits::<_, _, Error<(&[u8], usize)>, _, _>(many0(huffman::http2_decode_huffman))(data)?;
+        let (_, val) = bits(many0(huffman::http2_decode_huffman))(data)?;
         return Ok((i3, val));
     }
 }
@@ -431,7 +430,7 @@ fn http2_parse_headers_block_literal_incindex<'a>(
     input: &'a [u8], dyn_headers: &mut HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FrameHeaderBlock> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(complete(tuple((
+        bits(complete(tuple((
             verify(take_bits(2u8), |&x| x == 1),
             take_bits(6u8),
         ))))(input)
@@ -485,7 +484,7 @@ fn http2_parse_headers_block_literal_noindex<'a>(
     input: &'a [u8], dyn_headers: &HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FrameHeaderBlock> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(complete(tuple((
+        bits(complete(tuple((
             verify(take_bits(4u8), |&x| x == 0),
             take_bits(4u8),
         ))))(input)
@@ -503,7 +502,7 @@ fn http2_parse_headers_block_literal_neverindex<'a>(
     input: &'a [u8], dyn_headers: &HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FrameHeaderBlock> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(complete(tuple((
+        bits(complete(tuple((
             verify(take_bits(4u8), |&x| x == 1),
             take_bits(4u8),
         ))))(input)
@@ -543,7 +542,7 @@ fn http2_parse_headers_block_dynamic_size<'a>(
     input: &'a [u8], dyn_headers: &mut HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FrameHeaderBlock> {
     fn parser(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(complete(tuple((
+        bits(complete(tuple((
             verify(take_bits(3u8), |&x| x == 1),
             take_bits(5u8),
         ))))(input)
@@ -660,8 +659,7 @@ pub fn http2_parse_frame_push_promise<'a>(
     input: &'a [u8], flags: u8, dyn_headers: &mut HTTP2DynTable,
 ) -> IResult<&'a [u8], HTTP2FramePushPromise> {
     let (i2, padlength) = cond(flags & HTTP2_FLAG_HEADER_PADDED != 0, be_u8)(input)?;
-    let (mut i3, stream_id) =
-        bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((take_bits(1u8), take_bits(31u32))))(i2)?;
+    let (mut i3, stream_id) = bits(tuple((take_bits(1u8), take_bits(31u32))))(i2)?;
     let mut blocks = Vec::new();
     while i3.len() > 0 {
         match http2_parse_headers_block(i3, dyn_headers) {
index fd3937ec5d4193a9163e0a5c7ae137f968bcbb0b..8e6900ef4ea3a2f87208217cc695b611d59d91c6 100644 (file)
 
 // written by Sascha Steinbiss <sascha@steinbiss.name>
 
+use crate::common::nom7::bits;
 use crate::mqtt::mqtt_message::*;
 use crate::mqtt::mqtt_property::*;
-use nom7::bits::{bits, streaming::take as take_bits};
+use nom7::bits::streaming::take as take_bits;
 use nom7::bytes::streaming::take_while_m_n;
 use nom7::combinator::{complete, cond, verify};
-use nom7::error::Error;
 use nom7::multi::{length_data, many0, many1};
 use nom7::number::streaming::*;
 use nom7::sequence::tuple;
@@ -132,7 +132,7 @@ fn parse_properties(input: &[u8], precond: bool) -> IResult<&[u8], Option<Vec<MQ
 
 #[inline]
 fn parse_fixed_header_flags(i: &[u8]) -> IResult<&[u8], (u8, u8, u8, u8)> {
-    bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((
+    bits(tuple((
         take_bits(4u8),
         take_bits(1u8),
         take_bits(2u8),
@@ -176,7 +176,7 @@ pub fn parse_fixed_header(i: &[u8]) -> IResult<&[u8], FixedHeader> {
 
 #[inline]
 fn parse_connect_variable_flags(i: &[u8]) -> IResult<&[u8], (u8, u8, u8, u8, u8, u8, u8)> {
-    bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((
+    bits(tuple((
         take_bits(1u8),
         take_bits(1u8),
         take_bits(1u8),
index 933d2e0146239bd0cbda0b4e0526c48711791bf3..ff142f18b52a985390c7669c0822b860787f9e7a 100644 (file)
 
 //! Nom parsers for RPCv2
 
-use nom7::bits::{bits, streaming::take as take_bits};
+use crate::common::nom7::bits;
+use nom7::bits::streaming::take as take_bits;
 use nom7::bytes::streaming::take;
 use nom7::combinator::cond;
-use nom7::error::Error;
 use nom7::multi::length_data;
 use nom7::number::streaming::be_u32;
 use nom7::sequence::tuple;
@@ -121,7 +121,7 @@ pub struct RpcPacketHeader {
 }
 
 fn parse_bits(i: &[u8]) -> IResult<&[u8], (u8, u32)> {
-    bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((
+    bits(tuple((
         take_bits(1u8),   // is_last
         take_bits(31u32), // len
     )))(i)