From ce652511bd9343457956ccf3347690fbdd4a9e28 Mon Sep 17 00:00:00 2001 From: Pierre Chifflier Date: Mon, 1 Nov 2021 14:34:34 +0100 Subject: [PATCH] rust/tftp: convert parser to nom7 functions --- rust/src/tftp/tftp.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index e64d4843b9..b78281c657 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -21,7 +21,10 @@ extern crate nom; use std::str; use std; -use nom::*; +use nom7::IResult; +use nom7::combinator::map_res; +use nom7::bytes::streaming::{tag, take_while}; +use nom7::number::streaming::be_u8; use crate::applayer::AppLayerTxData; @@ -63,8 +66,8 @@ impl TFTPState { impl TFTPTransaction { pub fn new(opcode : u8, filename : String, mode : String) -> TFTPTransaction { TFTPTransaction { - opcode : opcode, - filename : filename, + opcode, + filename, mode : mode.to_lowercase(), id : 0, tx_data: AppLayerTxData::new(), @@ -116,23 +119,23 @@ pub extern "C" fn rs_tftp_get_tx_cnt(state: &mut TFTPState) -> u64 { return state.tx_id as u64; } -named!(getstr<&str>, map_res!( - take_while!(call!(|c| c != 0)), +fn getstr(i: &[u8]) -> IResult<&[u8], &str> { + map_res( + take_while(|c| c != 0), str::from_utf8 - ) -); + )(i) +} fn tftp_request<'a>(slice: &'a [u8]) -> IResult<&[u8], TFTPTransaction> { - do_parse!(slice, - tag!([0]) >> - opcode: take!(1) >> - filename: getstr >> - tag!([0]) >> - mode: getstr >> - ( - TFTPTransaction::new(opcode[0], String::from(filename), String::from(mode)) - ) + let (i, _) = tag([0])(slice)?; + let (i, opcode) = be_u8(i)?; + let (i, filename) = getstr(i)?; + let (i, _) = tag([0])(i)?; + let (i, mode) = getstr(i)?; + Ok((i, + TFTPTransaction::new(opcode, String::from(filename), String::from(mode)) ) + ) } fn parse_tftp_request(input: &[u8]) -> Option {