diff options
author | Joel Galenson <jgalenson@google.com> | 2021-09-23 14:49:07 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-09-23 14:49:07 +0000 |
commit | c3671ee3ea5bf25f4b30190b7a2519c767e7b520 (patch) | |
tree | 285b40d1c45383cd37e4017e216c185609689b32 | |
parent | 06ce5038dd0933d772b88ae495be5f94dc738420 (diff) | |
parent | 1a0cb4be2d41b27ab0f7dfff0a06887c33221beb (diff) | |
download | rusticata-macros-c3671ee3ea5bf25f4b30190b7a2519c767e7b520.tar.gz |
Upgrade rust/crates/rusticata-macros to 4.0.0 am: dcd5c5237a am: 1a0cb4be2d
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/rusticata-macros/+/1832969
Change-Id: I4dc20250d49ccae1ae13f90d9ba7ee5c3eb2b141
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | .github/workflows/clippy-check.yml | 12 | ||||
-rw-r--r-- | .github/workflows/rust.yml | 89 | ||||
-rw-r--r-- | .github/workflows/security-audit.yml | 15 | ||||
-rw-r--r-- | Android.bp | 6 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | Cargo.toml.orig | 4 | ||||
-rw-r--r-- | METADATA | 9 | ||||
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | benches/bench.rs | 6 | ||||
-rw-r--r-- | src/combinator.rs | 57 | ||||
-rw-r--r-- | src/debug.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 27 | ||||
-rw-r--r-- | src/macros.rs | 57 |
14 files changed, 201 insertions, 107 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e5ef5a3..5809fb6 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "636cde7a9e61447c6f17f8cc665c32e7f70f319f" + "sha1": "d0e0ab4cb4e060c37bfcbb0c2eebe1325186cad6" } } diff --git a/.github/workflows/clippy-check.yml b/.github/workflows/clippy-check.yml new file mode 100644 index 0000000..6010bf2 --- /dev/null +++ b/.github/workflows/clippy-check.yml @@ -0,0 +1,12 @@ +on: push +name: Clippy check +jobs: + clippy_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - run: rustup component add clippy + - uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --all-features diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..7a261ad --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,89 @@ +on: [push, pull_request] + +name: Continuous integration + +jobs: + check: + name: Check + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + - 1.44.0 + - nightly + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.rust }} + override: true + - name: Cargo update + run: cargo update + - uses: actions-rs/cargo@v1 + with: + command: check + + test: + name: Test Suite + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + + fmt: + name: Rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: rustup component add rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: rustup component add clippy + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: -- -D warnings + + doc: + name: Build documentation + runs-on: ubuntu-latest + env: + RUSTDOCFLAGS: --cfg docsrs + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + override: true + - uses: actions-rs/cargo@v1 + with: + command: doc + args: --workspace --no-deps --all-features diff --git a/.github/workflows/security-audit.yml b/.github/workflows/security-audit.yml new file mode 100644 index 0000000..7641204 --- /dev/null +++ b/.github/workflows/security-audit.yml @@ -0,0 +1,15 @@ +name: Security audit +on: + schedule: + - cron: "0 8 * * *" + push: + paths: + - "**/Cargo.*" +jobs: + security_audit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/audit-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} @@ -1,8 +1,6 @@ // This file is generated by cargo2android.py --config cargo2android.json. // Do not modify this file as changes will be overridden on upgrade. - - package { default_applicable_licenses: [ "external_rust_crates_rusticata-macros_license", @@ -45,6 +43,8 @@ rust_library { name: "librusticata_macros", host_supported: true, crate_name: "rusticata_macros", + cargo_env_compat: true, + cargo_pkg_version: "4.0.0", srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ @@ -56,6 +56,8 @@ rust_defaults { name: "rusticata-macros_test_defaults", crate_name: "rusticata_macros", srcs: ["src/lib.rs"], + cargo_env_compat: true, + cargo_pkg_version: "4.0.0", test_suites: ["general-tests"], auto_gen_config: true, edition: "2018", @@ -13,7 +13,7 @@ [package] edition = "2018" name = "rusticata-macros" -version = "3.1.0" +version = "4.0.0" authors = ["Pierre Chifflier <chifflier@wzdftpd.net>"] description = "Helper macros for Rusticata" homepage = "https://github.com/rusticata/rusticata-macros" @@ -23,4 +23,6 @@ categories = ["parsing"] license = "MIT/Apache-2.0" repository = "https://github.com/rusticata/rusticata-macros.git" [dependencies.nom] -version = "6.0" +version = "7.0" +features = ["std"] +default-features = false diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 6c23803..983518e 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "rusticata-macros" -version = "3.1.0" +version = "4.0.0" description = "Helper macros for Rusticata" license = "MIT/Apache-2.0" keywords = ["parser","nom","serialize"] @@ -12,4 +12,4 @@ categories = ["parsing"] readme = "README.md" [dependencies] -nom = "6.0" +nom = { version = "7.0", default-features = false, features = ["std"] } @@ -7,14 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/rusticata-macros/rusticata-macros-3.1.0.crate" + value: "https://static.crates.io/crates/rusticata-macros/rusticata-macros-4.0.0.crate" } - version: "3.1.0" - # Dual-licensed, using the least restrictive per go/thirdpartylicenses#same. + version: "4.0.0" license_type: NOTICE last_upgrade_date { year: 2021 - month: 7 - day: 9 + month: 9 + day: 22 } } @@ -36,8 +36,9 @@ See the documentation for more details and examples. Different versions of this crate are available, depending on nom version. -- `rusticata-macros` 0.3.x depends on nom 6 -- `rusticata-macros` 0.2.x depends on nom 5 +- `rusticata-macros` 4.x depends on nom 7 +- `rusticata-macros` 3.x depends on nom 6 +- `rusticata-macros` 2.x depends on nom 5 ## Documentation @@ -47,6 +48,11 @@ Reference documentation can be found [here](https://docs.rs/rusticata-macros/) ## Changes +### 4.0.0 + +- Upgrade to nom 7 + - Remove macros + ### 3.0.1 - Add `be_var_u64` and `le_var_u64` diff --git a/benches/bench.rs b/benches/bench.rs index 250f5be..454625b 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -5,13 +5,13 @@ use test::Bencher; extern crate rusticata_macros; -use rusticata_macros::bytes_to_u64; +use rusticata_macros::combinator::be_var_u64; #[bench] fn bench_bytes_to_u64(b: &mut Bencher) { let bytes = &[0x12, 0x34, 0x56, 0x78, 0x90, 0x12]; b.iter(|| { - let res = bytes_to_u64(bytes); - assert_eq!(res, Ok(0x123456789012)); + let res = be_var_u64::<()>(bytes).unwrap(); + assert_eq!(res.1, 0x123456789012); }); } diff --git a/src/combinator.rs b/src/combinator.rs index f5de774..d542557 100644 --- a/src/combinator.rs +++ b/src/combinator.rs @@ -3,10 +3,32 @@ use nom::bytes::streaming::take; use nom::combinator::map_parser; pub use nom::error::{make_error, ErrorKind, ParseError}; -pub use nom::{IResult, Needed}; +pub use nom::{IResult, Needed, Parser}; use nom::{InputIter, InputTake}; use nom::{InputLength, ToUsize}; +#[deprecated(since = "3.0.1", note = "please use `be_var_u64` instead")] +/// Read an entire slice as a big-endian value. +/// +/// Returns the value as `u64`. This function checks for integer overflows, and returns a +/// `Result::Err` value if the value is too big. +pub fn bytes_to_u64(s: &[u8]) -> Result<u64, &'static str> { + let mut u: u64 = 0; + + if s.is_empty() { + return Err("empty"); + }; + if s.len() > 8 { + return Err("overflow"); + } + for &c in s { + let u1 = u << 8; + u = u1 | (c as u64); + } + + Ok(u) +} + /// Read the entire slice as a big endian unsigned integer, up to 8 bytes #[inline] pub fn be_var_u64<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u64, E> { @@ -70,29 +92,28 @@ where } /// Return a closure that takes `len` bytes from input, and applies `parser`. -pub fn flat_take<I, C, O, E: ParseError<I>, F>(len: C, parser: F) -> impl Fn(I) -> IResult<I, O, E> +pub fn flat_take<I, C, O, E: ParseError<I>, F>( + len: C, + mut parser: F, +) -> impl FnMut(I) -> IResult<I, O, E> where I: InputTake + InputLength + InputIter, C: ToUsize + Copy, - F: Fn(I) -> IResult<I, O, E>, + F: Parser<I, O, E>, { // Note: this is the same as `map_parser(take(len), parser)` move |input: I| { let (input, o1) = take(len.to_usize())(input)?; - let (_, o2) = parser(o1)?; + let (_, o2) = parser.parse(o1)?; Ok((input, o2)) } } /// Take `len` bytes from `input`, and apply `parser`. -pub fn flat_takec<I: Clone, O, E: ParseError<I>, C, F>( - input: I, - len: C, - parser: F, -) -> IResult<I, O, E> +pub fn flat_takec<I, O, E: ParseError<I>, C, F>(input: I, len: C, parser: F) -> IResult<I, O, E> where C: ToUsize + Copy, - F: Fn(I) -> IResult<I, O, E>, + F: Parser<I, O, E>, I: InputTake + InputLength + InputIter, O: InputLength, { @@ -100,21 +121,21 @@ where } /// Helper macro for nom parsers: run first parser if condition is true, else second parser -pub fn cond_else<I: Clone, O, E: ParseError<I>, C, F, G>( +pub fn cond_else<I, O, E: ParseError<I>, C, F, G>( cond: C, - first: F, - second: G, -) -> impl Fn(I) -> IResult<I, O, E> + mut first: F, + mut second: G, +) -> impl FnMut(I) -> IResult<I, O, E> where C: Fn() -> bool, - F: Fn(I) -> IResult<I, O, E>, - G: Fn(I) -> IResult<I, O, E>, + F: Parser<I, O, E>, + G: Parser<I, O, E>, { move |input: I| { if cond() { - first(input) + first.parse(input) } else { - second(input) + second.parse(input) } } } diff --git a/src/debug.rs b/src/debug.rs index eb4e821..ec8977b 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,7 +1,15 @@ //! Helper functions and structures for debugging purpose +use nom::combinator::{map, peek, rest}; +use nom::HexDisplay; +use nom::IResult; use std::fmt; +/// Dump the remaining bytes to stderr, formatted as hex +pub fn dbg_dmp_rest(i: &[u8]) -> IResult<&[u8], ()> { + map(peek(rest), |r: &[u8]| eprintln!("\n{}\n", r.to_hex(16)))(i) +} + /// Wrapper for printing value as u8 hex data pub struct HexU8(pub u8); @@ -4,24 +4,20 @@ //! //! This crate contains some additions to [nom](https://github.com/Geal/nom). //! -//! For example, the `error_if!` macro allows to test a condition and return an error from the parser if the condition -//! fails: +//! For example, the [`cond_else`] function allows to apply the first parser if the +//! condition is true, and the second if the condition is false: //! //! ```rust -//! # extern crate rusticata_macros; -//! # extern crate nom; -//! # use nom::{do_parse, take, IResult}; -//! # use nom::error::ErrorKind; -//! # use nom::number::streaming::be_u8; -//! use rusticata_macros::error_if; +//! # use nom::IResult; +//! # use nom::combinator::map; +//! # use nom::number::streaming::*; +//! use rusticata_macros::combinator::cond_else; //! # fn parser(s:&[u8]) { -//! let r : IResult<&[u8],()> = do_parse!( -//! s, -//! l: be_u8 >> -//! error_if!(l < 4, ErrorKind::Verify) >> -//! data: take!(l - 4) >> -//! (()) -//! ); +//! let r: IResult<_, _, ()> = cond_else( +//! || s.len() > 1, +//! be_u16, +//! map(be_u8, u16::from) +//! )(s); //! # } //! ``` //! @@ -35,7 +31,6 @@ unused_qualifications )] -#[macro_use] extern crate nom; extern crate core; diff --git a/src/macros.rs b/src/macros.rs index c74cb4e..6109162 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,9 +2,7 @@ use nom::bytes::complete::take; use nom::combinator::map_res; -use nom::combinator::rest; pub use nom::error::{make_error, ErrorKind, ParseError}; -use nom::HexDisplay; pub use nom::{IResult, Needed}; #[doc(hidden)] @@ -104,31 +102,6 @@ macro_rules! empty ( ); ); -/// Helper macro for nom parsers: run first parser if condition is true, else second parser -#[macro_export] -macro_rules! cond_else ( - ($i:expr, $cond:expr, $expr_then:ident!($($args_then:tt)*), $expr_else:ident!($($args_else:tt)*)) => ( - { - if $cond { $expr_then!($i, $($args_then)*) } - else { $expr_else!($i, $($args_else)*) } - } - ); - ($i:expr, $cond:expr, $expr_then:expr, $expr_else:ident!($($args_else:tt)*)) => ( - cond_else!($i, $cond, call!($expr_then), $expr_else!($($args_else)*)) - ); - ($i:expr, $cond:expr, $expr_then:ident!($($args_then:tt)*), $expr_else:expr) => ( - cond_else!($i, $cond, $expr_then!($($args_then)*), call!($expr_else)) - ); - ($i:expr, $cond:expr, $expr_then:expr, $expr_else:expr) => ( - cond_else!($i, $cond, call!($expr_then), call!($expr_else)) - ); -); - -/// Dump the remaining bytes to stderr, formatted as hex -pub fn dbg_dmp_rest(i: &[u8]) -> IResult<&[u8], ()> { - map!(i, peek!(call!(rest)), |r| eprintln!("\n{}\n", r.to_hex(16))) -} - #[deprecated(since = "3.0.1", note = "please use `be_var_u64` instead")] /// Read an entire slice as a big-endian value. /// @@ -288,7 +261,7 @@ macro_rules! align32 { mod tests { use nom::error::ErrorKind; use nom::number::streaming::{be_u16, be_u32, be_u8}; - use nom::{Err, IResult, Needed}; + use nom::{error_position, Err, IResult, Needed}; #[test] #[allow(unsafe_code)] @@ -325,34 +298,6 @@ mod tests { } #[test] - fn test_cond_else() { - let input = &[0x01][..]; - let empty = &b""[..]; - let a = 1; - fn parse_u8(i: &[u8]) -> IResult<&[u8], u8> { - be_u8(i) - } - assert_eq!( - cond_else!(input, a == 1, call!(parse_u8), value!(0x02)), - Ok((empty, 0x01)) - ); - assert_eq!( - cond_else!(input, a == 1, parse_u8, value!(0x02)), - Ok((empty, 0x01)) - ); - assert_eq!( - cond_else!(input, a == 2, parse_u8, value!(0x02)), - Ok((input, 0x02)) - ); - assert_eq!( - cond_else!(input, a == 1, value!(0x02), parse_u8), - Ok((input, 0x02)) - ); - let res: IResult<&[u8], u8> = cond_else!(input, a == 1, parse_u8, parse_u8); - assert_eq!(res, Ok((empty, 0x01))); - } - - #[test] fn test_newtype_enum() { #[derive(Debug, PartialEq, Eq)] struct MyType(pub u8); |