From db9a1e17b6fc24dc285a9c1081ef6e552c04ddee Mon Sep 17 00:00:00 2001 From: Pierre Chifflier Date: Wed, 19 Jan 2022 14:21:46 +0100 Subject: [PATCH] rust/ssh: finish transition to nom7 --- rust/src/ssh/parser.rs | 30 ++++++++++++++++-------------- rust/src/ssh/ssh.rs | 4 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/rust/src/ssh/parser.rs b/rust/src/ssh/parser.rs index fa7629de5b..6dced26a41 100644 --- a/rust/src/ssh/parser.rs +++ b/rust/src/ssh/parser.rs @@ -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); } diff --git a/rust/src/ssh/ssh.rs b/rust/src/ssh/ssh.rs index 874ac9faf7..de716557d7 100644 --- a/rust/src/ssh/ssh.rs +++ b/rust/src/ssh/ssh.rs @@ -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); -- 2.47.2