rusticata-macros-4.1.0/.cargo_vcs_info.json0000644000000001360000000000100143170ustar { "git": { "sha1": "d6b00e821e9148bada1aa4eccc29a5948dc4ab23" }, "path_in_vcs": "" }rusticata-macros-4.1.0/.github/workflows/clippy-check.yml000064400000000000000000000004460072674642500216360ustar 00000000000000on: 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 rusticata-macros-4.1.0/.github/workflows/rust.yml000064400000000000000000000037160072674642500202630ustar 00000000000000on: [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 rusticata-macros-4.1.0/.github/workflows/security-audit.yml000064400000000000000000000004500072674642500222310ustar 00000000000000name: 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 }} rusticata-macros-4.1.0/.gitignore000064400000000000000000000000310072674642500151210ustar 00000000000000target Cargo.lock .*.swp rusticata-macros-4.1.0/.travis.yml000064400000000000000000000015530072674642500152540ustar 00000000000000language: rust sudo: false matrix: include: - rust: stable env: - NAME="stable" - FEATURES='' - rust: stable env: - NAME="stable,clippy" - FEATURES='' - CLIPPY=yes - rust: stable env: - NAME="stable,fmt" - FEATURES='' - RUSTFMT=yes - rust: nightly env: - NAME="nightly" - FEATURES='' - BENCH=1 before_script: - ([ "$CLIPPY" != yes ] || rustup component add clippy) - ([ "$RUSTFMT" != yes ] || rustup component add rustfmt) script: - ([ "$CLIPPY" != yes ] || cargo clippy --all-features -- -D clippy::all) - ([ "$RUSTFMT" != yes ] || cargo fmt --all -- --check) - | cargo build --verbose --features "$FEATURES" && cargo test --verbose --features "$FEATURES" && ([ "$BENCH" != 1 ] || cargo bench --verbose --features "$FEATURES") rusticata-macros-4.1.0/Cargo.toml0000644000000016730000000000100123240ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "rusticata-macros" version = "4.1.0" authors = ["Pierre Chifflier "] description = "Helper macros for Rusticata" homepage = "https://github.com/rusticata/rusticata-macros" readme = "README.md" keywords = ["parser", "nom", "serialize"] categories = ["parsing"] license = "MIT/Apache-2.0" repository = "https://github.com/rusticata/rusticata-macros.git" [dependencies.nom] version = "7.0" features = ["std"] default-features = false rusticata-macros-4.1.0/Cargo.toml.orig000064400000000000000000000007570072674642500160370ustar 00000000000000[package] name = "rusticata-macros" version = "4.1.0" description = "Helper macros for Rusticata" license = "MIT/Apache-2.0" keywords = ["parser","nom","serialize"] homepage = "https://github.com/rusticata/rusticata-macros" repository = "https://github.com/rusticata/rusticata-macros.git" authors = ["Pierre Chifflier "] edition = "2018" categories = ["parsing"] readme = "README.md" [dependencies] nom = { version = "7.0", default-features = false, features = ["std"] } rusticata-macros-4.1.0/LICENSE-APACHE000064400000000000000000000251370072674642500150730ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. rusticata-macros-4.1.0/LICENSE-MIT000064400000000000000000000020440072674642500145730ustar 00000000000000Copyright (c) 2017 Pierre Chifflier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rusticata-macros-4.1.0/README.md000064400000000000000000000054230072674642500144220ustar 00000000000000# rusticata-macros [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE-MIT) [![Apache License 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE) [![Build Status](https://travis-ci.org/rusticata/rusticata-macros.svg?branch=master)](https://travis-ci.org/rusticata/rusticata-macros) [![Github CI](https://github.com/rusticata/rusticata-macros/workflows/Continuous%20integration/badge.svg)](https://github.com/rusticata/rusticata-macros/actions) [![Crates.io Version](https://img.shields.io/crates/v/rusticata-macros.svg)](https://crates.io/crates/rusticata-macros) # Rusticata-macros Helper macros for the [rusticata](https://github.com/rusticata) project. This crate contains some additions to [nom](https://github.com/Geal/nom). For example, the [`combinator::cond_else`] function allows to apply the first parser if the condition is true, and the second if the condition is false: ```rust use rusticata_macros::combinator::cond_else; let r: IResult<_, _, ()> = cond_else( || s.len() > 1, be_u16, map(be_u8, u16::from) )(s); ``` See the documentation for more details and examples. ## Nom versions Different versions of this crate are available, depending on nom version. - `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 Crate is documented, do running `cargo doc` will crate the offline documentation. Reference documentation can be found [here](https://docs.rs/rusticata-macros/) ## Changes ### 4.1.0 - Remove macro `slice_fixed` ### 4.0.0 - Upgrade to nom 7 - Remove macros ### 3.0.1 - Add `be_var_u64` and `le_var_u64` ### 3.0.0 - Upgrade to nom 6 ### 2.1.0 - Add common trait `Serialize` for structures serialization ### 2.0.4 - Add function version of most combinators ### 2.0.3 - Add macros `q` (quote) and `align32` ### 2.0.2 - Add `upgrade_error` and `upgrade_error_to` ### 2.0.1 - Add macro `custom_check` - Add macro `flat_take` ### 2.0.0 - Upgrade to nom 5 - Debug types: use newtypes ### 1.1.0 - Add macro `newtype_enum` ### 1.0.0 - Upgrade to nom 4.0 - Warning: this is a breaking change! - Mark `parse_uint24` as deprecated ## License Licensed under either of * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ## Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. rusticata-macros-4.1.0/benches/bench.rs000064400000000000000000000005530072674642500161760ustar 00000000000000#![feature(test)] extern crate test; use test::Bencher; extern crate rusticata_macros; 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 = be_var_u64::<()>(bytes).unwrap(); assert_eq!(res.1, 0x123456789012); }); } rusticata-macros-4.1.0/src/combinator.rs000064400000000000000000000153550072674642500164420ustar 00000000000000//! General purpose combinators use nom::bytes::streaming::take; use nom::combinator::map_parser; use nom::error::{make_error, ErrorKind, ParseError}; 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 { 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> { if input.is_empty() { return Err(nom::Err::Incomplete(Needed::new(1))); } if input.len() > 8 { return Err(nom::Err::Error(make_error(input, ErrorKind::TooLarge))); } let mut res = 0u64; for byte in input { res = (res << 8) + *byte as u64; } Ok((&b""[..], res)) } /// Read the entire slice as a little endian unsigned integer, up to 8 bytes #[inline] pub fn le_var_u64<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u64, E> { if input.is_empty() { return Err(nom::Err::Incomplete(Needed::new(1))); } if input.len() > 8 { return Err(nom::Err::Error(make_error(input, ErrorKind::TooLarge))); } let mut res = 0u64; for byte in input.iter().rev() { res = (res << 8) + *byte as u64; } Ok((&b""[..], res)) } /// Read a slice as a big-endian value. #[inline] pub fn parse_hex_to_u64(i: &[u8], size: S) -> IResult<&[u8], u64> where S: ToUsize + Copy, { map_parser(take(size.to_usize()), be_var_u64)(i) } /// Apply combinator, automatically converts between errors if the underlying type supports it pub fn upgrade_error, E2: ParseError, F>( mut f: F, ) -> impl FnMut(I) -> IResult where F: FnMut(I) -> IResult, E2: From, { move |i| f(i).map_err(nom::Err::convert) } /// Create a combinator that returns the provided value, and input unchanged pub fn pure>(val: O) -> impl Fn(I) -> IResult where O: Clone, { move |input: I| Ok((input, val.clone())) } /// Return a closure that takes `len` bytes from input, and applies `parser`. pub fn flat_take, F>( len: C, mut parser: F, ) -> impl FnMut(I) -> IResult where I: InputTake + InputLength + InputIter, C: ToUsize + Copy, F: Parser, { // 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.parse(o1)?; Ok((input, o2)) } } /// Take `len` bytes from `input`, and apply `parser`. pub fn flat_takec, C, F>(input: I, len: C, parser: F) -> IResult where C: ToUsize + Copy, F: Parser, I: InputTake + InputLength + InputIter, O: InputLength, { flat_take(len, parser)(input) } /// Helper macro for nom parsers: run first parser if condition is true, else second parser pub fn cond_else, C, F, G>( cond: C, mut first: F, mut second: G, ) -> impl FnMut(I) -> IResult where C: Fn() -> bool, F: Parser, G: Parser, { move |input: I| { if cond() { first.parse(input) } else { second.parse(input) } } } /// Align input value to the next multiple of n bytes /// Valid only if n is a power of 2 pub const fn align_n2(x: usize, n: usize) -> usize { (x + (n - 1)) & !(n - 1) } /// Align input value to the next multiple of 4 bytes pub const fn align32(x: usize) -> usize { (x + 3) & !3 } #[cfg(test)] mod tests { use super::{align32, be_var_u64, cond_else, flat_take, pure}; use nom::bytes::streaming::take; use nom::number::streaming::{be_u16, be_u32, be_u8}; use nom::{Err, IResult, Needed}; #[test] fn test_be_var_u64() { let res: IResult<&[u8], u64> = be_var_u64(b"\x12\x34\x56"); let (_, v) = res.expect("be_var_u64 failed"); assert_eq!(v, 0x123456); } #[test] fn test_flat_take() { let input = &[0x00, 0x01, 0xff]; // read first 2 bytes and use correct combinator: OK let res: IResult<&[u8], u16> = flat_take(2u8, be_u16)(input); assert_eq!(res, Ok((&input[2..], 0x0001))); // read 3 bytes and use 2: OK (some input is just lost) let res: IResult<&[u8], u16> = flat_take(3u8, be_u16)(input); assert_eq!(res, Ok((&b""[..], 0x0001))); // read 2 bytes and a combinator requiring more bytes let res: IResult<&[u8], u32> = flat_take(2u8, be_u32)(input); assert_eq!(res, Err(Err::Incomplete(Needed::new(2)))); } #[test] fn test_flat_take_str() { let input = "abcdef"; // read first 2 bytes and use correct combinator: OK let res: IResult<&str, &str> = flat_take(2u8, take(2u8))(input); assert_eq!(res, Ok(("cdef", "ab"))); // read 3 bytes and use 2: OK (some input is just lost) let res: IResult<&str, &str> = flat_take(3u8, take(2u8))(input); assert_eq!(res, Ok(("def", "ab"))); // read 2 bytes and a use combinator requiring more bytes let res: IResult<&str, &str> = flat_take(2u8, take(4u8))(input); assert_eq!(res, Err(Err::Incomplete(Needed::Unknown))); } #[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(|| a == 1, parse_u8, pure(0x02))(input), Ok((empty, 0x01)) ); assert_eq!( cond_else(|| a == 1, parse_u8, pure(0x02))(input), Ok((empty, 0x01)) ); assert_eq!( cond_else(|| a == 2, parse_u8, pure(0x02))(input), Ok((input, 0x02)) ); assert_eq!( cond_else(|| a == 1, pure(0x02), parse_u8)(input), Ok((input, 0x02)) ); let res: IResult<&[u8], u8> = cond_else(|| a == 1, parse_u8, parse_u8)(input); assert_eq!(res, Ok((empty, 0x01))); } #[test] fn test_align32() { assert_eq!(align32(3), 4); assert_eq!(align32(4), 4); assert_eq!(align32(5), 8); assert_eq!(align32(5usize), 8); } } rusticata-macros-4.1.0/src/debug.rs000064400000000000000000000030060072674642500153610ustar 00000000000000//! 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); impl fmt::Debug for HexU8 { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "0x{:02x}", self.0) } } /// Wrapper for printing value as u16 hex data pub struct HexU16(pub u16); impl fmt::Debug for HexU16 { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "0x{:04x}", self.0) } } /// Wrapper for printing slice as hex data pub struct HexSlice<'a>(pub &'a [u8]); impl<'a> fmt::Debug for HexSlice<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let s: Vec<_> = self.0.iter().map(|&i| format!("{:02x}", i)).collect(); write!(fmt, "[{}]", s.join(" ")) } } #[cfg(test)] mod tests { use crate::debug; #[test] fn debug_print_hexu8() { assert_eq!(format!("{:?}", debug::HexU8(18)), "0x12"); } #[test] fn debug_print_hexu16() { assert_eq!(format!("{:?}", debug::HexU16(32769)), "0x8001"); } #[test] fn debug_print_hexslice() { assert_eq!( format!("{:?}", debug::HexSlice(&[15, 16, 17, 18, 19, 20])), "[0f 10 11 12 13 14]" ); } } rusticata-macros-4.1.0/src/lib.rs000064400000000000000000000017720072674642500150510ustar 00000000000000//! # Rusticata-macros //! //! Helper macros for the [rusticata](https://github.com/rusticata) project. //! //! This crate contains some additions to [nom](https://github.com/Geal/nom). //! //! For example, the [`combinator::cond_else`] function allows to apply the first parser if the //! condition is true, and the second if the condition is false: //! //! ```rust //! # use nom::IResult; //! # use nom::combinator::map; //! # use nom::number::streaming::*; //! use rusticata_macros::combinator::cond_else; //! # fn parser(s:&[u8]) { //! let r: IResult<_, _, ()> = cond_else( //! || s.len() > 1, //! be_u16, //! map(be_u8, u16::from) //! )(s); //! # } //! ``` //! //! See the documentation for more details and examples. #![deny( missing_docs, unsafe_code, unstable_features, unused_import_braces, unused_qualifications )] pub mod combinator; pub mod debug; pub use macros::*; #[macro_use] pub mod macros; mod traits; pub use traits::*; // re-exports pub use nom; rusticata-macros-4.1.0/src/macros.rs000064400000000000000000000205230072674642500155620ustar 00000000000000//! Helper macros use nom::bytes::complete::take; use nom::combinator::map_res; use nom::IResult; #[doc(hidden)] pub mod export { pub use core::{fmt, mem, ptr}; } /// Helper macro for newtypes: declare associated constants and implement Display trait #[macro_export] macro_rules! newtype_enum ( (@collect_impl, $name:ident, $($key:ident = $val:expr),* $(,)*) => { $( pub const $key : $name = $name($val); )* }; (@collect_disp, $name:ident, $f:ident, $m:expr, $($key:ident = $val:expr),* $(,)*) => { match $m { $( $val => write!($f, stringify!{$key}), )* n => write!($f, "{}({} / 0x{:x})", stringify!{$name}, n, n) } }; // entry (impl $name:ident {$($body:tt)*}) => ( #[allow(non_upper_case_globals)] impl $name { newtype_enum!{@collect_impl, $name, $($body)*} } ); // entry with display (impl display $name:ident {$($body:tt)*}) => ( newtype_enum!(impl $name { $($body)* }); impl $crate::export::fmt::Display for $name { fn fmt(&self, f: &mut $crate::export::fmt::Formatter) -> $crate::export::fmt::Result { newtype_enum!(@collect_disp, $name, f, self.0, $($body)*) } } ); // entry with display and debug (impl debug $name:ident {$($body:tt)*}) => ( newtype_enum!(impl display $name { $($body)* }); impl $crate::export::fmt::Debug for $name { fn fmt(&self, f: &mut $crate::export::fmt::Formatter) -> $crate::export::fmt::Result { write!(f, "{}", self) } } ); ); /// Helper macro for nom parsers: raise error if the condition is true /// /// This macro is used when using custom errors #[macro_export] macro_rules! custom_check ( ($i:expr, $cond:expr, $err:expr) => ( { if $cond { Err(::nom::Err::Error($err)) } else { Ok(($i, ())) } } ); ); /// Helper macro for nom parsers: raise error if the condition is true /// /// This macro is used when using `ErrorKind` #[macro_export] macro_rules! error_if ( ($i:expr, $cond:expr, $err:expr) => ( { use nom::error_position; if $cond { Err(::nom::Err::Error(error_position!($i, $err))) } else { Ok(($i, ())) } } ); ); /// Helper macro for nom parsers: raise error if input is not empty /// /// Deprecated - use `nom::eof` #[macro_export] #[deprecated(since = "2.0.0")] macro_rules! empty ( ($i:expr,) => ( { use nom::eof; eof!($i,) } ); ); #[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 { 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 a slice as a big-endian value. #[macro_export] macro_rules! parse_hex_to_u64 ( ( $i:expr, $size:expr ) => { map_res(take($size as usize), $crate::combinator::be_var_u64)($i) }; ); /// Read 3 bytes as an unsigned integer #[deprecated(since = "0.5.0", note = "please use `be_u24` instead")] #[allow(deprecated)] #[inline] pub fn parse_uint24(i: &[u8]) -> IResult<&[u8], u64> { map_res(take(3usize), bytes_to_u64)(i) } //named!(parse_hex4<&[u8], u64>, parse_hex_to_u64!(4)); /// Combination and flat_map! and take! as first combinator #[macro_export] macro_rules! flat_take ( ($i:expr, $len:expr, $f:ident) => ({ if $i.len() < $len { Err(::nom::Err::Incomplete(::nom::Needed::new($len))) } else { let taken = &$i[0..$len]; let rem = &$i[$len..]; match $f(taken) { Ok((_,res)) => Ok((rem,res)), Err(e) => Err(e) } } }); ($i:expr, $len:expr, $submac:ident!( $($args:tt)*)) => ({ if $i.len() < $len { Err(::nom::Err::Incomplete(::nom::Needed::new($len))) } else { let taken = &$i[0..$len]; let rem = &$i[$len..]; match $submac!(taken, $($args)*) { Ok((_,res)) => Ok((rem,res)), Err(e) => Err(e) } } }); ); /// Apply combinator, trying to "upgrade" error to next error type (using the `Into` or `From` /// traits). #[macro_export] macro_rules! upgrade_error ( ($i:expr, $submac:ident!( $($args:tt)*) ) => ({ upgrade_error!( $submac!( $i, $($args)* ) ) }); ($i:expr, $f:expr) => ({ upgrade_error!( call!($i, $f) ) }); ($e:expr) => ({ match $e { Ok(o) => Ok(o), Err(::nom::Err::Error(e)) => Err(::nom::Err::Error(e.into())), Err(::nom::Err::Failure(e)) => Err(::nom::Err::Failure(e.into())), Err(::nom::Err::Incomplete(i)) => Err(::nom::Err::Incomplete(i)), } }); ); /// Apply combinator, trying to "upgrade" error to next error type (using the `Into` or `From` /// traits). #[macro_export] macro_rules! upgrade_error_to ( ($i:expr, $ty:ty, $submac:ident!( $($args:tt)*) ) => ({ upgrade_error_to!( $ty, $submac!( $i, $($args)* ) ) }); ($i:expr, $ty:ty, $f:expr) => ({ upgrade_error_to!( $ty, call!($i, $f) ) }); ($ty:ty, $e:expr) => ({ match $e { Ok(o) => Ok(o), Err(::nom::Err::Error(e)) => Err(::nom::Err::Error(e.into::<$ty>())), Err(::nom::Err::Failure(e)) => Err(::nom::Err::Failure(e.into::<$ty>())), Err(::nom::Err::Incomplete(i)) => Err(::nom::Err::Incomplete(i)), } }); ); /// Nom combinator that returns the given expression unchanged #[macro_export] macro_rules! q { ($i:expr, $x:expr) => {{ Ok(($i, $x)) }}; } /// Align input value to the next multiple of n bytes /// Valid only if n is a power of 2 #[macro_export] macro_rules! align_n2 { ($x:expr, $n:expr) => { ($x + ($n - 1)) & !($n - 1) }; } /// Align input value to the next multiple of 4 bytes #[macro_export] macro_rules! align32 { ($x:expr) => { $crate::align_n2!($x, 4) }; } #[cfg(test)] mod tests { use nom::error::ErrorKind; use nom::number::streaming::{be_u16, be_u32}; use nom::{error_position, Err, IResult, Needed}; #[test] fn test_error_if() { let empty = &b""[..]; let res: IResult<&[u8], ()> = error_if!(empty, true, ErrorKind::Tag); assert_eq!(res, Err(Err::Error(error_position!(empty, ErrorKind::Tag)))); } #[test] fn test_newtype_enum() { #[derive(Debug, PartialEq, Eq)] struct MyType(pub u8); newtype_enum! { impl display MyType { Val1 = 0, Val2 = 1 } } assert_eq!(MyType(0), MyType::Val1); assert_eq!(MyType(1), MyType::Val2); assert_eq!(format!("{}", MyType(0)), "Val1"); assert_eq!(format!("{}", MyType(4)), "MyType(4 / 0x4)"); } #[test] fn test_flat_take() { let input = &[0x00, 0x01, 0xff]; // read first 2 bytes and use correct combinator: OK let res: IResult<&[u8], u16> = flat_take!(input, 2, be_u16); assert_eq!(res, Ok((&input[2..], 0x0001))); // read 3 bytes and use 2: OK (some input is just lost) let res: IResult<&[u8], u16> = flat_take!(input, 3, be_u16); assert_eq!(res, Ok((&b""[..], 0x0001))); // read 2 bytes and a combinator requiring more bytes let res: IResult<&[u8], u32> = flat_take!(input, 2, be_u32); assert_eq!(res, Err(Err::Incomplete(Needed::new(2)))); // test with macro as sub-combinator let res: IResult<&[u8], u16> = flat_take!(input, 2, be_u16); assert_eq!(res, Ok((&input[2..], 0x0001))); } #[test] fn test_q() { let empty = &b""[..]; let res: IResult<&[u8], &str, ErrorKind> = q!(empty, "test"); assert_eq!(res, Ok((empty, "test"))); } #[test] fn test_align32() { assert_eq!(align32!(3), 4); assert_eq!(align32!(4), 4); assert_eq!(align32!(5), 8); assert_eq!(align32!(5u32), 8); assert_eq!(align32!(5i32), 8); assert_eq!(align32!(5usize), 8); } } rusticata-macros-4.1.0/src/traits.rs000064400000000000000000000003550072674642500156050ustar 00000000000000//! Common traits /// Common trait for structures serialization pub trait Serialize> { /// Type of serialization error type Error; /// Try to serialize object fn serialize(&self) -> Result; }