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
use crate::utils::alphabets::Alphabet;
use crate::utils::alphabets::uniffy_opcode_group;

pub struct Decode {

}

impl Decode {
    ///  Decode a vector of u8 to ascii text string.
    ///
    ///  ```
    ///  use cryptatools_core::utils::convert;
    ///  let ascii: String = convert::Decode::from_u8_to_ascii(vec![0x41, 0x41, 0x41]);
    ///  assert_eq!("AAA", ascii);
    ///  ```
    /// 
    ///  ```
    ///  use cryptatools_core::utils::convert;
    ///  let ascii: String = convert::Decode::from_u8_to_ascii(vec![0x48, 0x69, 0x21]);
    ///  assert_eq!("Hi!", ascii);
    ///  ```
    pub fn from_u8_to_ascii(u8_input: Vec<u8>) -> String {
        String::from(std::str::from_utf8(u8_input.as_slice()).unwrap())
    }

    /*pub fn decode(alphabet: HashMap<String, Vec<u8>>, encoded: Vec<u8>) -> String {

    }*/
}
pub struct Encode {

}
impl Encode {
    ///  Encode a string to a vector of u8 bytes.
    ///
    ///  ```
    ///  use cryptatools_core::utils::convert;
    ///  let bytes: Vec<u8> = convert::Encode::from_ascii_to_u8(String::from("AAA"));
    ///  assert_eq!(vec![0x41, 0x41, 0x41], bytes);
    ///  ```
    pub fn from_ascii_to_u8(ascii_input: String) -> Vec<u8> {
        let mut bytes = vec![];
        for character in ascii_input.chars() {
            bytes.push(char::from(character) as u8)
        }

        bytes
    }

    /// Encode the input argument `unencoded` to a byte according the `alphabet`.
    /// 
    /// ```
    /// use cryptatools_core::utils::convert;
    /// use cryptatools_core::utils::alphabets::{Alphabet};
    /// 
    /// let ascii_alphabet = Alphabet::new_empty().ascii_encoding();
    /// let encoded: Vec<u8> = convert::Encode::encode(&ascii_alphabet, String::from("ABCDEFGH"));
    /// assert_eq!(encoded, vec![65, 66, 67, 68, 69, 70, 71, 72]);
    /// 
    /// let pokered_alphabet = Alphabet::new_empty().pokered_charset_encoding();
    /// let pokered_encoded = convert::Encode::encode(&pokered_alphabet, String::from("<NULL><PAGE><PKMN>"));
    /// assert_eq!(pokered_encoded, vec![0, 73, 74]);
    /// ```
    pub fn encode(alphabet: &Alphabet, unecoded: String) -> Vec<u8> {
        let mut encoded: Vec<Vec<u8>> = vec![];
        let mut stack = String::from("");

        for c in unecoded.chars() {
            let char_str = String::from(c);

            if alphabet.encoding.contains_left(&char_str) {
                if let Some(encoded_byte) = alphabet.encoding.get_by_left(&char_str) {
                    encoded.push(encoded_byte.to_vec());
                }
                stack = String::from("");
            } else {
                stack.push_str(&char_str);
                if alphabet.encoding.contains_left(&stack) {
                    if let Some(encoded_byte) = alphabet.encoding.get_by_left(stack.as_str()).clone() {
                        encoded.push(encoded_byte.clone());
                    }
                    stack = String::from("");
                }
            }
        }

        uniffy_opcode_group(encoded)
    }
}