Coverage Report

Created: 2025-12-31 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/rust/src/common.rs
Line
Count
Source
1
//! Utility library module for commonly used strings, hexadecimals and other elements.
2
3
use super::build_slice;
4
use crate::jsonbuilder::HEX;
5
use std::ffi::CString;
6
use std::os::raw::c_char;
7
8
pub mod nom7 {
9
    use nom7::bytes::streaming::{tag, take_until};
10
    use nom7::error::{Error, ParseError};
11
    use nom7::ErrorConvert;
12
    use nom7::IResult;
13
14
    /// Reimplementation of `take_until_and_consume` for nom 7
15
    ///
16
    /// `take_until` does not consume the matched tag, and
17
    /// `take_until_and_consume` was removed in nom 7. This function
18
    /// provides an implementation (specialized for `&[u8]`).
19
3.00M
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
3.00M
        t: &'a [u8],
21
3.00M
    ) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22
3.43M
        move |i: &'a [u8]| {
23
3.43M
            let (i, res) = take_until(t)(i)?;
24
3.08M
            let (i, _) = tag(t)(i)?;
25
3.08M
            Ok((i, res))
26
3.43M
        }
suricata::common::nom7::take_until_and_consume::<nom::error::Error<&[u8]>>::{closure#0}
Line
Count
Source
22
3.07M
        move |i: &'a [u8]| {
23
3.07M
            let (i, res) = take_until(t)(i)?;
24
2.80M
            let (i, _) = tag(t)(i)?;
25
2.80M
            Ok((i, res))
26
3.07M
        }
suricata::common::nom7::take_until_and_consume::<suricata::smb::error::SmbError>::{closure#0}
Line
Count
Source
22
334k
        move |i: &'a [u8]| {
23
334k
            let (i, res) = take_until(t)(i)?;
24
261k
            let (i, _) = tag(t)(i)?;
25
261k
            Ok((i, res))
26
334k
        }
suricata::common::nom7::take_until_and_consume::<suricata::rdp::error::RdpError>::{closure#0}
Line
Count
Source
22
29.3k
        move |i: &'a [u8]| {
23
29.3k
            let (i, res) = take_until(t)(i)?;
24
26.0k
            let (i, _) = tag(t)(i)?;
25
26.0k
            Ok((i, res))
26
29.3k
        }
27
3.00M
    }
suricata::common::nom7::take_until_and_consume::<nom::error::Error<&[u8]>>
Line
Count
Source
19
2.64M
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
2.64M
        t: &'a [u8],
21
2.64M
    ) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22
        move |i: &'a [u8]| {
23
            let (i, res) = take_until(t)(i)?;
24
            let (i, _) = tag(t)(i)?;
25
            Ok((i, res))
26
        }
27
2.64M
    }
suricata::common::nom7::take_until_and_consume::<suricata::smb::error::SmbError>
Line
Count
Source
19
334k
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
334k
        t: &'a [u8],
21
334k
    ) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22
        move |i: &'a [u8]| {
23
            let (i, res) = take_until(t)(i)?;
24
            let (i, _) = tag(t)(i)?;
25
            Ok((i, res))
26
        }
27
334k
    }
suricata::common::nom7::take_until_and_consume::<suricata::rdp::error::RdpError>
Line
Count
Source
19
29.3k
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
29.3k
        t: &'a [u8],
21
29.3k
    ) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22
        move |i: &'a [u8]| {
23
            let (i, res) = take_until(t)(i)?;
24
            let (i, _) = tag(t)(i)?;
25
            Ok((i, res))
26
        }
27
29.3k
    }
28
29
    /// Specialized version of the nom 7 `bits` combinator
30
    ///
31
    /// The `bits combinator has trouble inferring the transient error type
32
    /// used by the tuple parser, because the function is generic and any
33
    /// error type would be valid.
34
    /// Use an explicit error type (as described in
35
    /// https://docs.rs/nom/7.1.0/nom/bits/fn.bits.html) to solve this problem, and
36
    /// specialize this function for `&[u8]`.
37
98.2M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
98.2M
    where
39
98.2M
        E: ParseError<&'a [u8]>,
40
98.2M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
98.2M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
98.2M
        nom7::bits::bits(parser)
45
98.2M
    }
suricata::common::nom7::bits::<(u8, u8, u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8, u8, u8), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
34.1M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
34.1M
    where
39
34.1M
        E: ParseError<&'a [u8]>,
40
34.1M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
34.1M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
34.1M
        nom7::bits::bits(parser)
45
34.1M
    }
suricata::common::nom7::bits::<(u8, u8, u8, u8, u8, u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8, u8, u8, u8, u8, u8), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
34.0k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
34.0k
    where
39
34.0k
        E: ParseError<&'a [u8]>,
40
34.0k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
34.0k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
34.0k
        nom7::bits::bits(parser)
45
34.0k
    }
suricata::common::nom7::bits::<(u8, u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8, u8), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
139k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
139k
    where
39
139k
        E: ParseError<&'a [u8]>,
40
139k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
139k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
139k
        nom7::bits::bits(parser)
45
139k
    }
suricata::common::nom7::bits::<(u8, u32), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u32), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u32, u32, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
7.63M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
7.63M
    where
39
7.63M
        E: ParseError<&'a [u8]>,
40
7.63M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
7.63M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
7.63M
        nom7::bits::bits(parser)
45
7.63M
    }
suricata::common::nom7::bits::<(u32, u32, u32), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u32, u32, u32), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u32, u32, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u32, u32, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u32, u32, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
139k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
139k
    where
39
139k
        E: ParseError<&'a [u8]>,
40
139k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
139k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
139k
        nom7::bits::bits(parser)
45
139k
    }
suricata::common::nom7::bits::<alloc::vec::Vec<u8>, nom::error::Error<&[u8]>, nom::multi::many0<(&[u8], usize), u8, nom::error::Error<(&[u8], usize)>, suricata::http2::huffman::http2_decode_huffman>::{closure#0}>
Line
Count
Source
37
467k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
467k
    where
39
467k
        E: ParseError<&'a [u8]>,
40
467k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
467k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
467k
        nom7::bits::bits(parser)
45
467k
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::combinator::complete<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::http2::parser::http2_parse_headers_block_indexed::parser::{closure#0}>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>::{closure#0}>
Line
Count
Source
37
27.7M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
27.7M
    where
39
27.7M
        E: ParseError<&'a [u8]>,
40
27.7M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
27.7M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
27.7M
        nom7::bits::bits(parser)
45
27.7M
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::combinator::complete<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::http2::parser::http2_parse_headers_block_dynamic_size::parser::{closure#0}>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>::{closure#0}>
Line
Count
Source
37
2.98M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
2.98M
    where
39
2.98M
        E: ParseError<&'a [u8]>,
40
2.98M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
2.98M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
2.98M
        nom7::bits::bits(parser)
45
2.98M
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::combinator::complete<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::http2::parser::http2_parse_headers_block_literal_noindex::parser::{closure#0}>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>::{closure#0}>
Line
Count
Source
37
4.74M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
4.74M
    where
39
4.74M
        E: ParseError<&'a [u8]>,
40
4.74M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
4.74M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
4.74M
        nom7::bits::bits(parser)
45
4.74M
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::combinator::complete<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::http2::parser::http2_parse_headers_block_literal_incindex::parser::{closure#0}>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>::{closure#0}>
Line
Count
Source
37
5.38M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
5.38M
    where
39
5.38M
        E: ParseError<&'a [u8]>,
40
5.38M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
5.38M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
5.38M
        nom7::bits::bits(parser)
45
5.38M
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::combinator::complete<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::http2::parser::http2_parse_headers_block_literal_neverindex::parser::{closure#0}>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>::{closure#0}>
Line
Count
Source
37
165k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
165k
    where
39
165k
        E: ParseError<&'a [u8]>,
40
165k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
165k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
165k
        nom7::bits::bits(parser)
45
165k
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0})>::{closure#0}>
Line
Count
Source
37
12.5M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
12.5M
    where
39
12.5M
        E: ParseError<&'a [u8]>,
40
12.5M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
12.5M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
12.5M
        nom7::bits::bits(parser)
45
12.5M
    }
suricata::common::nom7::bits::<(u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::rdp::parser::parse_class_options::{closure#0}>::{closure#0}, nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::rdp::parser::parse_class_options::{closure#1}>::{closure#0})>::{closure#0}>
Line
Count
Source
37
2.11M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
2.11M
    where
39
2.11M
        E: ParseError<&'a [u8]>,
40
2.11M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
2.11M
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
2.11M
        nom7::bits::bits(parser)
45
2.11M
    }
suricata::common::nom7::bits::<(u8, u8, u8), nom::error::Error<&[u8]>, nom::sequence::tuple<(&[u8], usize), (u8, u8, u8), nom::error::Error<(&[u8], usize)>, (nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::rdp::parser::parse_x223_data_class_0::parser::{closure#0}>::{closure#0}, nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::rdp::parser::parse_x223_data_class_0::parser::{closure#1}>::{closure#0}, nom::combinator::verify<(&[u8], usize), u8, u8, nom::error::Error<(&[u8], usize)>, nom::bits::streaming::take<&[u8], u8, u8, nom::error::Error<(&[u8], usize)>>::{closure#0}, suricata::rdp::parser::parse_x223_data_class_0::parser::{closure#2}>::{closure#0})>::{closure#0}>
Line
Count
Source
37
34.5k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
34.5k
    where
39
34.5k
        E: ParseError<&'a [u8]>,
40
34.5k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
34.5k
        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
42
    {
43
        // use full path to disambiguate nom `bits` from this current function name
44
34.5k
        nom7::bits::bits(parser)
45
34.5k
    }
46
}
47
48
#[cfg(not(feature = "debug-validate"))]
49
#[macro_export]
50
macro_rules! debug_validate_bug_on (
51
  ($item:expr) => {};
52
);
53
54
#[cfg(feature = "debug-validate")]
55
#[macro_export]
56
macro_rules! debug_validate_bug_on (
57
  ($item:expr) => {
58
    if $item {
59
        panic!("Condition check failed");
60
    }
61
  };
62
);
63
64
#[cfg(not(feature = "debug-validate"))]
65
#[macro_export]
66
macro_rules! debug_validate_fail (
67
  ($msg:expr) => {};
68
);
69
70
#[cfg(feature = "debug-validate")]
71
#[macro_export]
72
macro_rules! debug_validate_fail (
73
  ($msg:expr) => {
74
    // Wrap in a conditional to prevent unreachable code warning in caller.
75
    if true {
76
      panic!($msg);
77
    }
78
  };
79
);
80
81
/// Convert a String to C-compatible string
82
///
83
/// This function will consume the provided data and use the underlying bytes to construct a new
84
/// string, ensuring that there is a trailing 0 byte. This trailing 0 byte will be appended by this
85
/// function; the provided data should *not* contain any 0 bytes in it.
86
///
87
/// Returns a valid pointer, or NULL
88
5.25k
pub fn rust_string_to_c(s: String) -> *mut c_char {
89
5.25k
    CString::new(s)
90
5.25k
        .map(|c_str| c_str.into_raw())
91
5.25k
        .unwrap_or(std::ptr::null_mut())
92
5.25k
}
93
94
/// Free a CString allocated by Rust (for ex. using `rust_string_to_c`)
95
///
96
/// # Safety
97
///
98
/// s must be allocated by rust, using `CString::new`
99
#[no_mangle]
100
5.22k
pub unsafe extern "C" fn rs_cstring_free(s: *mut c_char) {
101
5.22k
    if s.is_null() {
102
0
        return;
103
5.22k
    }
104
5.22k
    drop(CString::from_raw(s));
105
5.22k
}
106
107
/// Convert an u8-array of data into a hexadecimal representation
108
1.74M
pub fn to_hex(input: &[u8]) -> String {
109
1.74M
    return input
110
1.74M
        .iter()
111
3.71M
        .flat_map(|b| {
112
3.71M
            vec![
113
3.71M
                char::from(HEX[(b >> 4) as usize]),
114
3.71M
                char::from(HEX[(b & 0xf) as usize]),
115
            ]
116
3.71M
        })
117
1.74M
        .collect();
118
1.74M
}
119
120
#[no_mangle]
121
51.3k
pub unsafe extern "C" fn rs_to_hex(
122
51.3k
    output: *mut u8, out_len: usize, input: *const u8, in_len: usize,
123
51.3k
) {
124
51.3k
    if out_len < 2 * in_len + 1 {
125
0
        return;
126
51.3k
    }
127
51.3k
    let islice = build_slice!(input, in_len);
128
51.3k
    let oslice = std::slice::from_raw_parts_mut(output, 2 * in_len + 1);
129
    // only used from C
130
1.64M
    for i in 0..islice.len() {
131
1.64M
        oslice[2 * i] = HEX[(islice[i] >> 4) as usize];
132
1.64M
        oslice[2 * i + 1] = HEX[(islice[i] & 0xf) as usize];
133
1.64M
    }
134
51.3k
    oslice[2 * islice.len()] = 0;
135
51.3k
}
136
137
#[no_mangle]
138
1.73k
pub unsafe extern "C" fn rs_to_hex_sep(
139
1.73k
    output: *mut u8, out_len: usize, sep: u8, input: *const u8, in_len: usize,
140
1.73k
) {
141
1.73k
    if out_len < 3 * in_len {
142
0
        return;
143
1.73k
    }
144
1.73k
    let islice = build_slice!(input, in_len);
145
1.73k
    let oslice = std::slice::from_raw_parts_mut(output, 3 * in_len);
146
    // only used from C
147
34.7k
    for i in 0..islice.len() {
148
34.7k
        oslice[3 * i] = HEX[(islice[i] >> 4) as usize];
149
34.7k
        oslice[3 * i + 1] = HEX[(islice[i] & 0xf) as usize];
150
34.7k
        oslice[3 * i + 2] = sep;
151
34.7k
    }
152
    // overwrites last separator with final null char
153
1.73k
    oslice[3 * islice.len() - 1] = 0;
154
1.73k
}