Skip to content

Commit

Permalink
Fixed exporting 3 byte DataNumbers (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: mikemiles-dev <michaelmileusnich@Michaels-MacBook-Air-2.local>
  • Loading branch information
mikemiles-dev and mikemiles-dev authored Jun 17, 2024
1 parent 6784097 commit 8a31a2f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "netflow_parser"
description = "Parser for Netflow Cisco V5, V7, V9, IPFIX"
version = "0.3.4"
version = "0.3.5"
edition = "2021"
author = "michael.mileusnich@gmail.com"
license = "MIT OR Apache-2.0"
Expand All @@ -10,6 +10,7 @@ readme = "README.md"
repository = "https://github.com/mikemiles-dev/netflow_parser/"

[dependencies]
byteorder = "1.5.0"
nom = "7.1.3"
nom-derive = "0.10.1"
mac_address = "1.1.5"
Expand Down
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.3.5
* 3 Byte Data Numbers now correctly converts back to be_bytes.

# 0.3.4
* Added 3 byte DataNumber support.

Expand Down
1 change: 1 addition & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

| Version | Supported |
| ------- | ------------------ |
| 0.3.5 | :white_check_mark: |
| 0.3.4 | :white_check_mark: |
| 0.3.3 | :white_check_mark: |
| 0.3.2 | :white_check_mark: |
Expand Down
33 changes: 29 additions & 4 deletions src/variable_versions/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::protocol::ProtocolTypes;

use byteorder::{BigEndian, WriteBytesExt};
use nom::bytes::complete::take;
use nom::error::{Error as NomError, ErrorKind};
use nom::number::complete::{be_i24, be_u24};
use nom::number::complete::{be_u128, be_u32};
use nom::number::complete::{be_i24, be_u128, be_u24, be_u32};
use nom::Err as NomErr;
use nom::IResult;
use nom_derive::*;
Expand All @@ -19,6 +19,8 @@ use std::time::Duration;
pub enum DataNumber {
U8(u8),
U16(u16),
U24(u32),
I24(i32),
U32(u32),
U64(u64),
U128(u128),
Expand Down Expand Up @@ -47,8 +49,8 @@ impl DataNumber {
match field_length {
1 if !signed => Ok(u8::parse(i)?).map(|(i, j)| (i, Self::U8(j))),
2 if !signed => Ok(u16::parse(i)?).map(|(i, j)| (i, Self::U16(j))),
3 if !signed => Ok(be_u24(i).map(|(i, j)| (i, Self::U32(j)))?),
3 if signed => Ok(be_i24(i).map(|(i, j)| (i, Self::I32(j)))?),
3 if !signed => Ok(be_u24(i).map(|(i, j)| (i, Self::U24(j)))?),
3 if signed => Ok(be_i24(i).map(|(i, j)| (i, Self::I24(j)))?),
4 if signed => Ok(i32::parse(i)?).map(|(i, j)| (i, Self::I32(j))),
4 if !signed => Ok(u32::parse(i)?).map(|(i, j)| (i, Self::U32(j))),
8 if !signed => Ok(u64::parse(i)?).map(|(i, j)| (i, Self::U64(j))),
Expand All @@ -61,6 +63,16 @@ impl DataNumber {
match self {
DataNumber::U8(n) => n.to_be_bytes().to_vec(),
DataNumber::U16(n) => n.to_be_bytes().to_vec(),
DataNumber::U24(n) => {
let mut wtr = Vec::new();
wtr.write_u24::<BigEndian>(*n).unwrap();
wtr
}
DataNumber::I24(n) => {
let mut wtr = Vec::new();
wtr.write_i24::<BigEndian>(*n).unwrap();
wtr
}
DataNumber::U32(n) => n.to_be_bytes().to_vec(),
DataNumber::U64(n) => n.to_be_bytes().to_vec(),
DataNumber::U128(n) => n.to_be_bytes().to_vec(),
Expand Down Expand Up @@ -167,6 +179,8 @@ impl From<DataNumber> for usize {
fn from(val: DataNumber) -> Self {
match val {
DataNumber::U8(i) => i as usize,
DataNumber::I24(i) => i as usize,
DataNumber::U24(i) => i as usize,
DataNumber::U32(i) => i as usize,
DataNumber::I32(i) => i as usize,
DataNumber::U16(i) => i as usize,
Expand Down Expand Up @@ -222,3 +236,14 @@ pub enum FieldDataType {
ProtocolType,
Unknown,
}

#[cfg(test)]
mod common_tests {
#[test]
fn it_tests_3_byte_data_number_exports() {
use super::DataNumber;
let data = DataNumber::parse(&[1, 246, 118], 3, false).unwrap().1;
println!("{:?}", data);
assert_eq!(data.to_be_bytes(), vec![1, 246, 118]);
}
}

0 comments on commit 8a31a2f

Please sign in to comment.