1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#[macro_export]
macro_rules! cfor {
    ($init: stmt; $cond: expr; $step: expr; $body: block) => {
        {
            let mut first = true;
            $init;
            while {
                if first {
                    first = false
                } else {
                    $step
                }

                $cond
            } $body
        }
    }
}

pub const PF_TABLE_NAME_SIZE: usize = 32;
pub const PF_RULE_LABEL_SIZE: usize = 64;
pub const MAXPATHLEN: usize = 1024;
pub const IFNAMSIZ: usize = 16;
pub const IF_NAMESIZE: usize = 16;

//libc is missing some constants for the FreeBSD
pub const AI_NUMERICHOST: libc::c_int = 0x00000004;

pub(crate)
fn sanitize_str(st: &str) -> String
{
    let mut out = String::with_capacity(st.len());

    for c in st.chars()
    {
        if c.is_ascii_alphanumeric() == true ||
            c.is_ascii_punctuation() == true ||
            c == ' '
        {
            out.push(c);
        }
        else
        {
            let mut buf = [0_u8; 4];
            c.encode_utf8(&mut buf);

            let formatted: String = 
                buf[0..c.len_utf8()].into_iter()
                    .map(|c| format!("\\x{:02x}", c))
                    .collect();

            out.push_str(&formatted);
        }
    }

    return out;
}