]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/ssh: finish transition to nom7
authorPierre Chifflier <chifflier@wzdftpd.net>
Wed, 19 Jan 2022 13:21:46 +0000 (14:21 +0100)
committerVictor Julien <vjulien@oisf.net>
Thu, 29 Sep 2022 08:37:50 +0000 (10:37 +0200)
rust/src/ssh/parser.rs
rust/src/ssh/ssh.rs

index fa7629de5b3405f6338300609a246269e1565f32..6dced26a41b0750d09476881aeb835a9512c7efa 100644 (file)
@@ -19,11 +19,12 @@ use digest::Digest;
 use digest::Update;
 use md5::Md5;
 use nom7::branch::alt;
-use nom7::bytes::streaming::{is_not, tag, take};
+use nom7::bytes::streaming::{is_not, tag, take, take_while};
 use nom7::character::streaming::char;
-use nom7::combinator::{complete, rest, verify};
+use nom7::combinator::{complete, eof, not, rest, verify};
 use nom7::multi::length_data;
 use nom7::number::streaming::{be_u32, be_u8};
+use nom7::sequence::terminated;
 use nom7::IResult;
 use std::fmt;
 
@@ -70,17 +71,18 @@ fn is_not_lineend(b: u8) -> bool {
 }
 
 //may leave \r at the end to be removed
-named!(pub ssh_parse_line<&[u8], &[u8]>,
-    terminated!(
-        take_while!(is_not_lineend),
-        alt!( tag!("\n") | tag!("\r\n") |
-              do_parse!(
-                    bytes: tag!("\r") >>
-                    not!(eof!()) >> (bytes)
-                )
-            )
-    )
-);
+pub fn ssh_parse_line(i: &[u8]) -> IResult<&[u8], &[u8]> {
+    fn parser(i: &[u8]) -> IResult<&[u8], &[u8]> {
+        let (i, bytes) = tag("\r")(i)?;
+        let (i, _) = not(eof)(i)?;
+        Ok((i, bytes))
+    }
+    terminated(
+        take_while(is_not_lineend),
+        alt(( tag("\n"), tag("\r\n"), parser
+            ))
+        )(i)
+}
 
 #[derive(PartialEq)]
 pub struct SshBanner<'a> {
@@ -316,7 +318,7 @@ mod tests {
             Ok((_, _)) => {
                 panic!("Expected incomplete result");
             }
-            Err(nom::Err::Incomplete(_)) => {
+            Err(Err::Incomplete(_)) => {
                 //OK
                 assert_eq!(1, 1);
             }
index 874ac9faf72a24cde250e796bf94f2a68f4b2cce..de716557d717cb163a72e1f80e0a8b5339f378e8 100644 (file)
@@ -258,7 +258,7 @@ impl SSHState {
                     }
                     return r;
                 }
-                Err(nom::Err::Incomplete(_)) => {
+                Err(Err::Incomplete(_)) => {
                     return AppLayerResult::incomplete(0 as u32, (input.len() + 1) as u32);
                 }
                 Err(_e) => {
@@ -296,7 +296,7 @@ impl SSHState {
                 }
                 return r;
             }
-            Err(nom::Err::Incomplete(_)) => {
+            Err(Err::Incomplete(_)) => {
                 if input.len() < SSH_MAX_BANNER_LEN {
                     //0 consumed, needs at least one more byte
                     return AppLayerResult::incomplete(0 as u32, (input.len() + 1) as u32);