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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
extern crate proc_macro;
use proc_macro::{Span, TokenStream};
use syn::{parse_macro_input, Error, LitInt};

#[proc_macro]
pub fn encode_oid(input: TokenStream) -> TokenStream {
    let token_stream = input.to_string();
    let (s, relative) = if token_stream.starts_with("rel ") {
        (&token_stream[4..], true)
    } else {
        (token_stream.as_ref(), false)
    };
    let items: Result<Vec<_>, _> = s.split('.').map(|x| x.trim().parse::<u128>()).collect();
    let mut items: &[_] = match items.as_ref() {
        Ok(v) => v.as_ref(),
        Err(_) => return create_error("Could not parse OID"),
    };
    let mut v = Vec::new();
    if !relative {
        if items.len() < 2 {
            if items.len() == 1 && items[0] == 0 {
                return "[0]".parse().unwrap();
            }
            return create_error("Need at least two components for non-relative oid");
        }
        if items[0] > 2 || items[1] > 39 {
            return create_error("First components are too big");
        }
        let first_byte = (items[0] * 40 + items[1]) as u8;
        v.push(first_byte);
        items = &items[2..];
    }
    for &int in items {
        let enc = encode_base128(int);
        v.extend_from_slice(&enc);
    }
    // "fn answer() -> u32 { 42 }".parse().unwrap()
    let mut s = String::with_capacity(2 + 6 * v.len());
    s.push('[');
    for byte in v.iter() {
        s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
    }
    s.push(']');
    s.parse().unwrap()
}

#[inline]
fn create_error(msg: &str) -> TokenStream {
    let s = format!("Invalid OID({})", msg);
    Error::new(Span::call_site().into(), &s)
        .to_compile_error()
        .into()
}

// encode int as base128
fn encode_base128(int: u128) -> Vec<u8> {
    let mut val = int;
    let mut base128 = Vec::new();
    let lo = val & 0x7f;
    base128.push(lo as u8);
    val >>= 7;
    loop {
        if val == 0 {
            base128.reverse();
            return base128;
        }
        let lo = val & 0x7f;
        base128.push(lo as u8 | 0x80);
        val >>= 7;
    }
}

#[proc_macro]
pub fn encode_int(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitInt);

    match impl_encode_int(lit) {
        Ok(ts) => ts,
        Err(e) => e.to_compile_error().into(),
    }

    // let token_stream = input.to_string();
    // let items: Result<Vec<_>, _> = token_stream
    //     .split('.')
    //     .map(|x| x.trim().parse::<u64>())
    //     .collect();
    // let err = Error::new(Span::call_site().into(), "invalid OID");
    // if let Ok(items) = items {
    //     let mut v = Vec::new();
    //     if items.len() < 2 || items[0] > 2 || items[1] > 39 {
    //         return err.to_compile_error().into();
    //     }
    //     let first_byte = (items[0] * 40 + items[1]) as u8;
    //     v.push(first_byte);
    //     for &int in &items[2..] {
    //         let enc = encode_base128(int);
    //         v.extend_from_slice(&enc);
    //     }
    //     // "fn answer() -> u32 { 42 }".parse().unwrap()
    //     let mut s = String::with_capacity(2 + 6 * v.len());
    //     s.push('[');
    //     for byte in v.iter() {
    //         s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
    //     }
    //     s.push(']');
    //     s.parse().unwrap()
    // } else {
    //     eprintln!("could not parse OID '{}'", token_stream);
    //     err.to_compile_error().into()
    // }
}

fn impl_encode_int(lit: LitInt) -> Result<TokenStream, Error> {
    let value = lit.base10_parse::<u64>()?;

    let bytes = value.to_be_bytes();
    let v: Vec<_> = bytes.iter().skip_while(|&c| *c == 0).collect();

    let mut s = String::with_capacity(2 + 6 * v.len());
    s.push('[');
    for byte in v.iter() {
        s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
    }
    s.push(']');
    Ok(s.parse().unwrap())
}