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
162
163
164
use super::*;
impl Decode {
/// Emits up to 3 decoded non-zero bytes from four charset symbol indices.
///
/// # Arguments
///
/// - `&mut Vec<u8>` - Destination decoded byte buffer.
/// - `usize` - First symbol index.
/// - `usize` - Second symbol index.
/// - `usize` - Third symbol index.
/// - `usize` - Fourth symbol index.
#[inline(always)]
fn push_decoded_group(decoded: &mut Vec<u8>, v0: usize, v1: usize, v2: usize, v3: usize) {
let high_bits: usize = v0 | v1;
let low_high: usize = (v2 >> 2) | (v3 >> 6);
if (high_bits | low_high) == 0 {
let b2: u8 = ((v2 << 6) | v3) as u8;
if b2 != 0 {
decoded.push(b2);
}
return;
}
let combined: usize = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3;
let b0: u8 = (combined >> 16) as u8;
let b1: u8 = (combined >> 8) as u8;
let b2: u8 = combined as u8;
if b0 != 0 {
decoded.push(b0);
}
if b1 != 0 {
decoded.push(b1);
}
if b2 != 0 {
decoded.push(b2);
}
}
/// Decodes an encoded string using specified character set.
///
/// Processes 4-character groups to reconstruct original bytes by mapping each
/// character to its position in the charset.
///
/// # Arguments
///
/// - `&str` - The character set used for decoding.
/// - `&str` - The string to decode.
///
/// # Returns
///
/// - `Result<String, DecodeError>` - Result of decoding operation.
pub fn execute(charset: &str, decode_str: &str) -> Result<String, DecodeError> {
if charset.is_ascii() {
let charset_bytes: &[u8] = charset.as_bytes();
let bytes: &[u8] = decode_str.as_bytes();
let mut decoded: Vec<u8> = Vec::with_capacity(bytes.len() / 4);
if charset_bytes.len() == CHARSET_LEN {
let lut: [u8; DECODE_LUT_LEN] =
Charset::build_ascii_decode_lut_exact(charset_bytes)
.ok_or(DecodeError::CharsetError)?;
let mut pos: usize = 0;
let out_ptr: *mut u8 = decoded.as_mut_ptr();
let mut out_len: usize = 0;
for &[b0, b1, b2_in, b3] in bytes.as_chunks::<4>().0 {
let v0: u8 = lut[b0 as usize];
let v1: u8 = lut[b1 as usize];
let v2: u8 = lut[b2_in as usize];
let v3: u8 = lut[b3 as usize];
let high_bits: u8 = v0 | v1;
let low_bad: u8 = (v2 >> 2) | (v3 & !SIX_BIT_MASK);
if (high_bits | low_bad) != 0 {
break;
}
let b2: u8 = (v2 << 6) | v3;
if b2 != 0 {
unsafe {
*out_ptr.add(out_len) = b2;
}
out_len += 1;
}
pos += 4;
}
unsafe {
decoded.set_len(out_len);
}
let mut group: [usize; 4] = [0usize; 4];
let mut group_len: usize = 0;
for &byte in &bytes[pos..] {
let val: u8 = lut[byte as usize];
if val != INVALID_DECODE_VAL {
group[group_len] = val as usize;
group_len += 1;
if group_len == 4 {
Self::push_decoded_group(
&mut decoded,
group[0],
group[1],
group[2],
group[3],
);
group_len = 0;
}
}
}
return Ok(String::from_utf8(decoded).unwrap_or_default());
}
let lut: [usize; DECODE_LUT_LEN] =
Charset::build_ascii_decode_lut_general(charset_bytes)
.ok_or(DecodeError::CharsetError)?;
let mut group: [usize; 4] = [0usize; 4];
let mut group_len: usize = 0;
for &byte in bytes {
let val: usize = lut[byte as usize];
if val != INVALID_DECODE_INDEX {
group[group_len] = val;
group_len += 1;
if group_len == 4 {
Self::push_decoded_group(
&mut decoded,
group[0],
group[1],
group[2],
group[3],
);
group_len = 0;
}
}
}
return Ok(String::from_utf8(decoded).unwrap_or_default());
}
let table: [(char, usize); UNICODE_HASH_SLOTS] =
Charset::build_unicode_decode_table(charset).ok_or(DecodeError::CharsetError)?;
let mut decoded: Vec<u8> = Vec::with_capacity(decode_str.len() / 4);
let mut group: [usize; 4] = [0usize; 4];
let mut group_len: usize = 0;
for ch in decode_str.chars() {
let mut slot: usize =
(((ch as u32).wrapping_mul(UNICODE_HASH_MULT)) >> 25) as usize & UNICODE_HASH_MASK;
loop {
let entry: (char, usize) = table[slot];
if entry.1 == UNICODE_EMPTY_SLOT {
break;
}
if entry.0 == ch {
group[group_len] = entry.1;
group_len += 1;
if group_len == 4 {
Self::push_decoded_group(
&mut decoded,
group[0],
group[1],
group[2],
group[3],
);
group_len = 0;
}
break;
}
slot = (slot + 1) & UNICODE_HASH_MASK;
}
}
Ok(String::from_utf8(decoded).unwrap_or_default())
}
}