Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert byte_extract keyword parser to Rust #10881

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
473 changes: 473 additions & 0 deletions rust/src/detect/byte_extract.rs

Large diffs are not rendered by default.

419 changes: 176 additions & 243 deletions rust/src/detect/byte_math.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rust/src/detect/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use nom7::error::{ErrorKind, ParseError};
#[derive(Debug, PartialEq, Eq)]
pub enum RuleParseError<I> {
InvalidByteMath(String),
InvalidByteExtract(String),

Nom(I, ErrorKind),
}
Expand Down
44 changes: 43 additions & 1 deletion rust/src/detect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

//! Module for rule parsing.

pub mod byte_extract;
pub mod byte_math;
pub mod error;
pub mod iprep;
pub mod parser;
pub mod requires;
pub mod stream_size;
pub mod uint;
pub mod uri;
pub mod requires;
pub mod tojson;

/// EnumString trait that will be implemented on enums that
Expand All @@ -43,6 +44,47 @@ pub trait EnumString<T> {
fn from_str(s: &str) -> Option<Self> where Self: Sized;
}

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
// endian <big|little|dce>
pub enum ByteEndian {
_EndianNone = 0,
BigEndian = 1,
LittleEndian = 2,
EndianDCE = 3,
}

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ByteBase {
_BaseNone = 0,
BaseOct = 8,
BaseDec = 10,
BaseHex = 16,
}

fn get_string_value(value: &str) -> Option<ByteBase> {
let res = match value {
"hex" => Some(ByteBase::BaseHex),
"oct" => Some(ByteBase::BaseOct),
"dec" => Some(ByteBase::BaseDec),
_ => None,
};

res
}

fn get_endian_value(value: &str) -> Option<ByteEndian> {
let res = match value {
"big" => Some(ByteEndian::BigEndian),
"little" => Some(ByteEndian::LittleEndian),
"dce" => Some(ByteEndian::EndianDCE),
_ => None,
};

res
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading
Loading