Coverage Report

Created: 2026-06-07 07:05

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.35M
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
3.35M
        t: &'a [u8],
21
3.35M
    ) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22
5.47M
        move |i: &'a [u8]| {
23
5.47M
            let (i, res) = take_until(t)(i)?;
24
5.13M
            let (i, _) = tag(t)(i)?;
25
5.13M
            Ok((i, res))
26
5.47M
        }
suricata::common::nom7::take_until_and_consume::<nom::error::Error<&[u8]>>::{closure#0}
Line
Count
Source
22
5.13M
        move |i: &'a [u8]| {
23
5.13M
            let (i, res) = take_until(t)(i)?;
24
4.84M
            let (i, _) = tag(t)(i)?;
25
4.84M
            Ok((i, res))
26
5.13M
        }
suricata::common::nom7::take_until_and_consume::<suricata::smb::error::SmbError>::{closure#0}
Line
Count
Source
22
311k
        move |i: &'a [u8]| {
23
311k
            let (i, res) = take_until(t)(i)?;
24
255k
            let (i, _) = tag(t)(i)?;
25
255k
            Ok((i, res))
26
311k
        }
suricata::common::nom7::take_until_and_consume::<suricata::rdp::error::RdpError>::{closure#0}
Line
Count
Source
22
31.7k
        move |i: &'a [u8]| {
23
31.7k
            let (i, res) = take_until(t)(i)?;
24
28.0k
            let (i, _) = tag(t)(i)?;
25
28.0k
            Ok((i, res))
26
31.7k
        }
27
3.35M
    }
suricata::common::nom7::take_until_and_consume::<nom::error::Error<&[u8]>>
Line
Count
Source
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
        move |i: &'a [u8]| {
23
            let (i, res) = take_until(t)(i)?;
24
            let (i, _) = tag(t)(i)?;
25
            Ok((i, res))
26
        }
27
3.00M
    }
suricata::common::nom7::take_until_and_consume::<suricata::smb::error::SmbError>
Line
Count
Source
19
311k
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
311k
        t: &'a [u8],
21
311k
    ) -> 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
311k
    }
suricata::common::nom7::take_until_and_consume::<suricata::rdp::error::RdpError>
Line
Count
Source
19
31.7k
    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
20
31.7k
        t: &'a [u8],
21
31.7k
    ) -> 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
31.7k
    }
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
82.8M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
82.8M
    where
39
82.8M
        E: ParseError<&'a [u8]>,
40
82.8M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
82.8M
        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
82.8M
        nom7::bits::bits(parser)
45
82.8M
    }
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
6.16M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
6.16M
    where
39
6.16M
        E: ParseError<&'a [u8]>,
40
6.16M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
6.16M
        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
6.16M
        nom7::bits::bits(parser)
45
6.16M
    }
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
40.4M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
40.4M
    where
39
40.4M
        E: ParseError<&'a [u8]>,
40
40.4M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
40.4M
        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
40.4M
        nom7::bits::bits(parser)
45
40.4M
    }
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
35.6k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
35.6k
    where
39
35.6k
        E: ParseError<&'a [u8]>,
40
35.6k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
35.6k
        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
35.6k
        nom7::bits::bits(parser)
45
35.6k
    }
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
143k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
143k
    where
39
143k
        E: ParseError<&'a [u8]>,
40
143k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
143k
        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
143k
        nom7::bits::bits(parser)
45
143k
    }
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
143k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
143k
    where
39
143k
        E: ParseError<&'a [u8]>,
40
143k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
143k
        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
143k
        nom7::bits::bits(parser)
45
143k
    }
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
569k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
569k
    where
39
569k
        E: ParseError<&'a [u8]>,
40
569k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
569k
        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
569k
        nom7::bits::bits(parser)
45
569k
    }
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
18.2M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
18.2M
    where
39
18.2M
        E: ParseError<&'a [u8]>,
40
18.2M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
18.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
18.2M
        nom7::bits::bits(parser)
45
18.2M
    }
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
1.85M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
1.85M
    where
39
1.85M
        E: ParseError<&'a [u8]>,
40
1.85M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
1.85M
        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
1.85M
        nom7::bits::bits(parser)
45
1.85M
    }
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
2.37M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
2.37M
    where
39
2.37M
        E: ParseError<&'a [u8]>,
40
2.37M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
2.37M
        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.37M
        nom7::bits::bits(parser)
45
2.37M
    }
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
3.03M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
3.03M
    where
39
3.03M
        E: ParseError<&'a [u8]>,
40
3.03M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
3.03M
        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
3.03M
        nom7::bits::bits(parser)
45
3.03M
    }
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
177k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
177k
    where
39
177k
        E: ParseError<&'a [u8]>,
40
177k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
177k
        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
177k
        nom7::bits::bits(parser)
45
177k
    }
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
7.23M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
7.23M
    where
39
7.23M
        E: ParseError<&'a [u8]>,
40
7.23M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
7.23M
        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.23M
        nom7::bits::bits(parser)
45
7.23M
    }
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.43M
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
2.43M
    where
39
2.43M
        E: ParseError<&'a [u8]>,
40
2.43M
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
2.43M
        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.43M
        nom7::bits::bits(parser)
45
2.43M
    }
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
38.9k
    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
38
38.9k
    where
39
38.9k
        E: ParseError<&'a [u8]>,
40
38.9k
        Error<(&'a [u8], usize)>: ErrorConvert<E>,
41
38.9k
        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
38.9k
        nom7::bits::bits(parser)
45
38.9k
    }
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
6.72k
pub fn rust_string_to_c(s: String) -> *mut c_char {
89
6.72k
    CString::new(s)
90
6.72k
        .map(|c_str| c_str.into_raw())
91
6.72k
        .unwrap_or(std::ptr::null_mut())
92
6.72k
}
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
6.69k
pub unsafe extern "C" fn rs_cstring_free(s: *mut c_char) {
101
6.69k
    if s.is_null() {
102
0
        return;
103
6.69k
    }
104
6.69k
    drop(CString::from_raw(s));
105
6.69k
}
106
107
/// Convert an u8-array of data into a hexadecimal representation
108
1.35M
pub fn to_hex(input: &[u8]) -> String {
109
1.35M
    return input
110
1.35M
        .iter()
111
3.51M
        .flat_map(|b| {
112
3.51M
            vec![
113
3.51M
                char::from(HEX[(b >> 4) as usize]),
114
3.51M
                char::from(HEX[(b & 0xf) as usize]),
115
            ]
116
3.51M
        })
117
1.35M
        .collect();
118
1.35M
}
119
120
#[no_mangle]
121
66.1k
pub unsafe extern "C" fn rs_to_hex(
122
66.1k
    output: *mut u8, out_len: usize, input: *const u8, in_len: usize,
123
66.1k
) {
124
66.1k
    if out_len < 2 * in_len + 1 {
125
0
        return;
126
66.1k
    }
127
66.1k
    let islice = build_slice!(input, in_len);
128
66.1k
    let oslice = std::slice::from_raw_parts_mut(output, 2 * in_len + 1);
129
    // only used from C
130
2.11M
    for i in 0..islice.len() {
131
2.11M
        oslice[2 * i] = HEX[(islice[i] >> 4) as usize];
132
2.11M
        oslice[2 * i + 1] = HEX[(islice[i] & 0xf) as usize];
133
2.11M
    }
134
66.1k
    oslice[2 * islice.len()] = 0;
135
66.1k
}
136
137
#[no_mangle]
138
2.22k
pub unsafe extern "C" fn rs_to_hex_sep(
139
2.22k
    output: *mut u8, out_len: usize, sep: u8, input: *const u8, in_len: usize,
140
2.22k
) {
141
2.22k
    if out_len < 3 * in_len {
142
0
        return;
143
2.22k
    }
144
2.22k
    let islice = build_slice!(input, in_len);
145
2.22k
    let oslice = std::slice::from_raw_parts_mut(output, 3 * in_len);
146
    // only used from C
147
44.5k
    for i in 0..islice.len() {
148
44.5k
        oslice[3 * i] = HEX[(islice[i] >> 4) as usize];
149
44.5k
        oslice[3 * i + 1] = HEX[(islice[i] & 0xf) as usize];
150
44.5k
        oslice[3 * i + 2] = sep;
151
44.5k
    }
152
    // overwrites last separator with final null char
153
2.22k
    oslice[3 * islice.len() - 1] = 0;
154
2.22k
}