]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/mime: convert parser to nom7
authorPierre Chifflier <chifflier@wzdftpd.net>
Wed, 19 Jan 2022 10:58:49 +0000 (11:58 +0100)
committerVictor Julien <vjulien@oisf.net>
Thu, 17 Feb 2022 17:36:28 +0000 (18:36 +0100)
rust/src/mime/mod.rs

index 59799c34c4bc1e00d32da9c441933ee90e47d371..fa69da41924e07aa71c15d52f14f9ec4b5c058a6 100644 (file)
  * 02110-1301, USA.
  */
 
+use crate::common::nom7::take_until_and_consume;
+use nom7::branch::alt;
+use nom7::bytes::streaming::{tag, take_until, take_while};
+use nom7::combinator::{complete, opt, rest};
+use nom7::error::{make_error, ErrorKind};
+use nom7::{Err, IResult};
 use std;
 use std::collections::HashMap;
 
-use nom::combinator::rest;
-use nom::error::ErrorKind;
-use nom::Err;
-use nom::IResult;
-
 #[derive(Clone)]
 pub struct MIMEHeaderTokens<'a> {
     pub tokens: HashMap<&'a [u8], &'a [u8]>,
 }
 
 pub fn mime_parse_value_delimited(input: &[u8]) -> IResult<&[u8], &[u8]> {
-    let (input, _) = tag!(input, "\"")?;
-    let (input, value) = take_until!(input, "\"")?;
-    let (input, _) = tag!(input, "\"")?;
+    let (input, _) = tag("\"")(input)?;
+    let (input, value) = take_until("\"")(input)?;
+    let (input, _) = tag("\"")(input)?;
     return Ok((input, value));
 }
 
 pub fn mime_parse_header_token(input: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
     // from RFC2047 : like ch.is_ascii_whitespace but without 0x0c FORM-FEED
-    let (input, _) = take_while!(input, |ch: u8| ch == 0x20
+    let (input, _) = take_while(|ch: u8| ch == 0x20
         || ch == 0x09
         || ch == 0x0a
-        || ch == 0x0d)?;
-    let (input, name) = take_until!(input, "=")?;
-    let (input, _) = tag!(input, "=")?;
-    let (input, value) = alt!(
-        input,
-        mime_parse_value_delimited | complete!(take_until!(";")) | rest
-    )?;
-    let (input, _) = opt!(input, complete!(tag!(";")))?;
+        || ch == 0x0d)(input)?;
+    let (input, name) = take_until("=")(input)?;
+    let (input, _) = tag("=")(input)?;
+    let (input, value) = alt((
+        mime_parse_value_delimited,
+        complete(take_until(";")),
+        rest
+    ))(input)?;
+    let (input, _) = opt(complete(tag(";")))(input)?;
     return Ok((input, (name, value)));
 }
 
 fn mime_parse_header_tokens(input: &[u8]) -> IResult<&[u8], MIMEHeaderTokens> {
-    let (mut input, _) = take_until_and_consume!(input, ";")?;
+    let (mut input, _) = take_until_and_consume(b";")(input)?;
     let mut tokens = HashMap::new();
     while input.len() > 0 {
         match mime_parse_header_token(input) {
@@ -62,7 +64,7 @@ fn mime_parse_header_tokens(input: &[u8]) -> IResult<&[u8], MIMEHeaderTokens> {
                 debug_validate_bug_on!(input.len() == rem.len());
                 if input.len() == rem.len() {
                     //infinite loop
-                    return Err(Err::Error((input, ErrorKind::Eof)));
+                    return Err(Err::Error(make_error(input, ErrorKind::Eof)));
                 }
                 input = rem;
             }