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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use bstr::ByteSlice;
use hex;
#[cfg(target_family = "wasm")]
use serde::{Deserialize, Serialize};

use crate::Operation;
use crate::OperationError;

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub struct HexDecode {
    prefix: Option<String>,
    delimiter: Option<String>,
}

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub enum HexFormat {
    Upper,
    Lower,
}

impl Operation for HexDecode {
    fn execute(&self, input: &[u8]) -> Result<Vec<u8>, OperationError> {
        let mut input = input.to_vec();
        if let Some(p) = &self.prefix {
            input = input.replace(p, "");
        };
        if let Some(d) = &self.delimiter {
            input = input.replace(d, "");
        };
        Ok(hex::decode(input)?)
    }
}

impl HexDecode {
    pub fn new(prefix: Option<String>, delimiter: Option<String>) -> Self {
        HexDecode { prefix, delimiter }
    }
}

#[derive(Clone)]
#[cfg_attr(target_family = "wasm", derive(Serialize, Deserialize))]
pub struct HexEncode {
    format: HexFormat,
    prefix: Option<String>,
    delimiter: Option<String>,
}

impl Operation for HexEncode {
    fn execute(&self, input: &[u8]) -> Result<Vec<u8>, OperationError> {
        let hex_string = match self.format {
            HexFormat::Lower => hex::encode(input),
            HexFormat::Upper => hex::encode_upper(input),
        };
        let mut output = vec![];
        let delimiter = self
            .delimiter
            .clone()
            .and_then(|d| if d.is_empty() { None } else { Some(d) });
        let prefix = self
            .prefix
            .clone()
            .and_then(|p| if p.is_empty() { None } else { Some(p) });
        let mut chunks = hex_string.as_bytes().chunks(2).peekable();
        while let Some(chunk) = chunks.next() {
            if let Some(p) = &prefix {
                output.extend_from_slice(p.as_bytes());
            }
            output.extend_from_slice(chunk);
            if let Some(d) = &delimiter {
                if chunks.peek().is_some() {
                    output.extend_from_slice(d.as_bytes());
                }
            }
        }
        Ok(output)
    }
}

impl HexEncode {
    pub fn new(format: HexFormat, prefix: Option<String>, delimiter: Option<String>) -> Self {
        HexEncode {
            format,
            prefix,
            delimiter,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hex_decode_no_prefix_no_delimiter() {
        let encoder = HexDecode::new(None, None);
        let actual = encoder.execute("636169646f".as_bytes()).unwrap();
        let expected = "caido".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_decode_prefix() {
        let encoder = HexDecode::new(Some("\\x".to_string()), None);
        let actual = encoder
            .execute("\\x63\\x61\\x69\\x64\\x6f".as_bytes())
            .unwrap();
        let expected = "caido".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_decode_delimiter() {
        let encoder = HexDecode::new(None, Some(",".to_string()));
        let actual = encoder.execute("63,61,69,64,6f".as_bytes()).unwrap();
        let expected = "caido".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_encode_prefix_upper() {
        let encoder = HexEncode::new(HexFormat::Upper, Some("\\x".to_string()), None);
        let actual = encoder.execute("caido".as_bytes()).unwrap();
        let expected = "\\x63\\x61\\x69\\x64\\x6F".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_encode_prefix_lower() {
        let encoder = HexEncode::new(HexFormat::Lower, Some("0x".to_string()), None);
        let actual = encoder.execute("caido".as_bytes()).unwrap();
        let expected = "0x630x610x690x640x6f".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_encode_delimiter_lower() {
        let encoder = HexEncode::new(HexFormat::Lower, None, Some("\n".to_string()));
        let actual = encoder.execute("caido".as_bytes()).unwrap();
        let expected = "63\n61\n69\n64\n6f".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }

    #[test]
    fn hex_encode_delimiter_upper() {
        let encoder = HexEncode::new(HexFormat::Upper, None, Some("\n".to_string()));
        let actual = encoder.execute("caido".as_bytes()).unwrap();
        let expected = "63\n61\n69\n64\n6F".as_bytes().to_vec();
        assert_eq!(actual, expected);
    }
}