-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.rs
162 lines (150 loc) · 4.8 KB
/
preprocessing.rs
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
153
154
155
156
157
158
159
160
161
use std::fmt::Display;
/// # Preprocessed array of values that needs to be converted
/// This is the result of calling the `printable_array(Vec::from(preprocess_ascii_convertable()), 6);`
///
/// It shows which chars should get encoded.
///
/// - `1` lets the program know to **NOT** encode the char
/// - `0` lets the program know to encode the char
pub const PREPROCESSED_ARRAY: [u8; 256] =
[0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 1, 0,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 ];
pub const HEX_DIGITS: &[u8; 16] = b"0123456789ABCDEF";
/// # Preprocessed Hexadecimal in bytes and their values
/// This is the result of calling the `printable_array(Vec::from(from_hex_bytes_to_value_bytes()), 6);`
///
/// This "takes" a byte representing a hexadecimal char and gives its decimal value
///
/// - `-1` lets the program know that the provided byte is not hex
/// - `everything else` lets the program know the hex value of the given byte
pub const HEX_BYTE_TO_HEX_VALUE: [i16; 256] =
[-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6,
7, 8, 9, -1, -1, -1, -1,
-1, -1, -1, 10, 11, 12, 13,
14, 15, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
10, 11, 12, 13, 14, 15, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1 ];
/// This function converts bytes that represent hex values in ascii to their
/// respective values
#[allow(dead_code)]
pub fn from_hex_bytes_to_value_bytes() -> [i16; 256] {
let mut array = [-1; 256];
for index in 0..256usize {
match index as u8{
b'0'..=b'9' => array[index] = (index as u8 - b'0') as i16,
b'A'..=b'F' => array[index] = (index as u8 - b'A' + 10) as i16,
b'a'..=b'f' => array[index] = (index as u8 - b'a' + 10) as i16,
_ => continue ,
}
}
array
}
/// This prints an array in a more readable way
///
#[allow(dead_code)]
pub fn printable_array<T>(array: Vec<T>, number_before_linebreak: u8)
where T: Display{
let mut current_number: u8 = 0;
print!("[");
for (index, current) in array.iter().enumerate() {
if current_number >= number_before_linebreak {
current_number = 0;
println!();
} else {
current_number += 1;
}
print!(
"{}{} ",
current,
if index == array.len() - 1 { "" } else { "," }
);
}
print!("]");
}
/// # Returns
/// An array that shows if the ascii char should get encoded:
///
/// - `1` - Char should not get encoded
/// - `0` - Char should get encoded
///
/// Which characters **should get encoded** gets decided by: https://www.rfc-editor.org/rfc/rfc3986#section-2.3
///
#[allow(dead_code)]
pub fn preprocess_ascii_convertable() -> [u8; 256] {
let mut array = [0_u8; 256];
for index in 0..=255u8 {
match index as char {
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' | '~' => {
array[index as usize] = 1u8
}
_ => array[index as usize] = 0u8,
}
}
array
}