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
mod algorithms;
extern crate esvm_rlp as rlp;

mod m256;
mod mi256;
mod u256;
mod u512;

pub use self::m256::M256;
pub use self::u256::U256;
pub use self::mi256::MI256;
pub use self::u512::U512;

#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)]
/// Sign of an integer.
pub enum Sign {
    Minus,
    NoSign,
    Plus,
}

#[derive(Debug)]
/// Errors exhibited from `read_hex`.
pub enum ParseHexError {
    InvalidCharacter,
    TooLong,
    TooShort,
    Other
}

/// Parses a given hex string and return a list of bytes if
/// succeeded. The string can optionally start by `0x`, which
/// indicates that it is a hex representation.
pub fn read_hex(s: &str) -> Result<Vec<u8>, ParseHexError> {
    if s.starts_with("0x") {
        return read_hex(&s[2..s.len()]);
    }

    if s.len() & 1 == 1 {
        let mut new_s = "0".to_string();
        new_s.push_str(s);
        return read_hex(&new_s);
    }

    let mut res = Vec::<u8>::new();

    let mut cur = 0;
    let mut len = 0;
    for c in s.chars() {
        len += 1;
        let v_option = c.to_digit(16);
        if v_option.is_none() {
            return Err(ParseHexError::InvalidCharacter);
        }
        let v = v_option.unwrap();
        if len == 1 {
            cur += v * 16;
        } else { // len == 2
            cur += v;
        }
        if len == 2 {
            res.push(cur as u8);
            cur = 0;
            len = 0;
        }
    }

    return Ok(res);
}