1use crate::error::DecodeError;
10
11const TABLE: [(u32, u8); 257] = [
13 (0x1ff8, 13),
14 (0x7fffd8, 23),
15 (0xfffffe2, 28),
16 (0xfffffe3, 28),
17 (0xfffffe4, 28),
18 (0xfffffe5, 28),
19 (0xfffffe6, 28),
20 (0xfffffe7, 28),
21 (0xfffffe8, 28),
22 (0xffffea, 24),
23 (0x3ffffffc, 30),
24 (0xfffffe9, 28),
25 (0xfffffea, 28),
26 (0x3ffffffd, 30),
27 (0xfffffeb, 28),
28 (0xfffffec, 28),
29 (0xfffffed, 28),
30 (0xfffffee, 28),
31 (0xfffffef, 28),
32 (0xffffff0, 28),
33 (0xffffff1, 28),
34 (0xffffff2, 28),
35 (0x3ffffffe, 30),
36 (0xffffff3, 28),
37 (0xffffff4, 28),
38 (0xffffff5, 28),
39 (0xffffff6, 28),
40 (0xffffff7, 28),
41 (0xffffff8, 28),
42 (0xffffff9, 28),
43 (0xffffffa, 28),
44 (0xffffffb, 28),
45 (0x14, 6),
46 (0x3f8, 10),
47 (0x3f9, 10),
48 (0xffa, 12),
49 (0x1ff9, 13),
50 (0x15, 6),
51 (0xf8, 8),
52 (0x7fa, 11),
53 (0x3fa, 10),
54 (0x3fb, 10),
55 (0xf9, 8),
56 (0x7fb, 11),
57 (0xfa, 8),
58 (0x16, 6),
59 (0x17, 6),
60 (0x18, 6),
61 (0x0, 5),
62 (0x1, 5),
63 (0x2, 5),
64 (0x19, 6),
65 (0x1a, 6),
66 (0x1b, 6),
67 (0x1c, 6),
68 (0x1d, 6),
69 (0x1e, 6),
70 (0x1f, 6),
71 (0x5c, 7),
72 (0xfb, 8),
73 (0x7ffc, 15),
74 (0x20, 6),
75 (0xffb, 12),
76 (0x3fc, 10),
77 (0x1ffa, 13),
78 (0x21, 6),
79 (0x5d, 7),
80 (0x5e, 7),
81 (0x5f, 7),
82 (0x60, 7),
83 (0x61, 7),
84 (0x62, 7),
85 (0x63, 7),
86 (0x64, 7),
87 (0x65, 7),
88 (0x66, 7),
89 (0x67, 7),
90 (0x68, 7),
91 (0x69, 7),
92 (0x6a, 7),
93 (0x6b, 7),
94 (0x6c, 7),
95 (0x6d, 7),
96 (0x6e, 7),
97 (0x6f, 7),
98 (0x70, 7),
99 (0x71, 7),
100 (0x72, 7),
101 (0xfc, 8),
102 (0x73, 7),
103 (0xfd, 8),
104 (0x1ffb, 13),
105 (0x7fff0, 19),
106 (0x1ffc, 13),
107 (0x3ffc, 14),
108 (0x22, 6),
109 (0x7ffd, 15),
110 (0x3, 5),
111 (0x23, 6),
112 (0x4, 5),
113 (0x24, 6),
114 (0x5, 5),
115 (0x25, 6),
116 (0x26, 6),
117 (0x27, 6),
118 (0x6, 5),
119 (0x74, 7),
120 (0x75, 7),
121 (0x28, 6),
122 (0x29, 6),
123 (0x2a, 6),
124 (0x7, 5),
125 (0x2b, 6),
126 (0x76, 7),
127 (0x2c, 6),
128 (0x8, 5),
129 (0x9, 5),
130 (0x2d, 6),
131 (0x77, 7),
132 (0x78, 7),
133 (0x79, 7),
134 (0x7a, 7),
135 (0x7b, 7),
136 (0x7ffe, 15),
137 (0x7fc, 11),
138 (0x3ffd, 14),
139 (0x1ffd, 13),
140 (0xffffffc, 28),
141 (0xfffe6, 20),
142 (0x3fffd2, 22),
143 (0xfffe7, 20),
144 (0xfffe8, 20),
145 (0x3fffd3, 22),
146 (0x3fffd4, 22),
147 (0x3fffd5, 22),
148 (0x7fffd9, 23),
149 (0x3fffd6, 22),
150 (0x7fffda, 23),
151 (0x7fffdb, 23),
152 (0x7fffdc, 23),
153 (0x7fffdd, 23),
154 (0x7fffde, 23),
155 (0xffffeb, 24),
156 (0x7fffdf, 23),
157 (0xffffec, 24),
158 (0xffffed, 24),
159 (0x3fffd7, 22),
160 (0x7fffe0, 23),
161 (0xffffee, 24),
162 (0x7fffe1, 23),
163 (0x7fffe2, 23),
164 (0x7fffe3, 23),
165 (0x7fffe4, 23),
166 (0x1fffdc, 21),
167 (0x3fffd8, 22),
168 (0x7fffe5, 23),
169 (0x3fffd9, 22),
170 (0x7fffe6, 23),
171 (0x7fffe7, 23),
172 (0xffffef, 24),
173 (0x3fffda, 22),
174 (0x1fffdd, 21),
175 (0xfffe9, 20),
176 (0x3fffdb, 22),
177 (0x3fffdc, 22),
178 (0x7fffe8, 23),
179 (0x7fffe9, 23),
180 (0x1fffde, 21),
181 (0x7fffea, 23),
182 (0x3fffdd, 22),
183 (0x3fffde, 22),
184 (0xfffff0, 24),
185 (0x1fffdf, 21),
186 (0x3fffdf, 22),
187 (0x7fffeb, 23),
188 (0x7fffec, 23),
189 (0x1fffe0, 21),
190 (0x1fffe1, 21),
191 (0x3fffe0, 22),
192 (0x1fffe2, 21),
193 (0x7fffed, 23),
194 (0x3fffe1, 22),
195 (0x7fffee, 23),
196 (0x7fffef, 23),
197 (0xfffea, 20),
198 (0x3fffe2, 22),
199 (0x3fffe3, 22),
200 (0x3fffe4, 22),
201 (0x7ffff0, 23),
202 (0x3fffe5, 22),
203 (0x3fffe6, 22),
204 (0x7ffff1, 23),
205 (0x3ffffe0, 26),
206 (0x3ffffe1, 26),
207 (0xfffeb, 20),
208 (0x7fff1, 19),
209 (0x3fffe7, 22),
210 (0x7ffff2, 23),
211 (0x3fffe8, 22),
212 (0x1ffffec, 25),
213 (0x3ffffe2, 26),
214 (0x3ffffe3, 26),
215 (0x3ffffe4, 26),
216 (0x7ffffde, 27),
217 (0x7ffffdf, 27),
218 (0x3ffffe5, 26),
219 (0xfffff1, 24),
220 (0x1ffffed, 25),
221 (0x7fff2, 19),
222 (0x1fffe3, 21),
223 (0x3ffffe6, 26),
224 (0x7ffffe0, 27),
225 (0x7ffffe1, 27),
226 (0x3ffffe7, 26),
227 (0x7ffffe2, 27),
228 (0xfffff2, 24),
229 (0x1fffe4, 21),
230 (0x1fffe5, 21),
231 (0x3ffffe8, 26),
232 (0x3ffffe9, 26),
233 (0xffffffd, 28),
234 (0x7ffffe3, 27),
235 (0x7ffffe4, 27),
236 (0x7ffffe5, 27),
237 (0xfffec, 20),
238 (0xfffff3, 24),
239 (0xfffed, 20),
240 (0x1fffe6, 21),
241 (0x3fffe9, 22),
242 (0x1fffe7, 21),
243 (0x1fffe8, 21),
244 (0x7ffff3, 23),
245 (0x3fffea, 22),
246 (0x3fffeb, 22),
247 (0x1ffffee, 25),
248 (0x1ffffef, 25),
249 (0xfffff4, 24),
250 (0xfffff5, 24),
251 (0x3ffffea, 26),
252 (0x7ffff4, 23),
253 (0x3ffffeb, 26),
254 (0x7ffffe6, 27),
255 (0x3ffffec, 26),
256 (0x3ffffed, 26),
257 (0x7ffffe7, 27),
258 (0x7ffffe8, 27),
259 (0x7ffffe9, 27),
260 (0x7ffffea, 27),
261 (0x7ffffeb, 27),
262 (0xffffffe, 28),
263 (0x7ffffec, 27),
264 (0x7ffffed, 27),
265 (0x7ffffee, 27),
266 (0x7ffffef, 27),
267 (0x7fffff0, 27),
268 (0x3ffffee, 26),
269 (0x3fffffff, 30),
270];
271
272const EOS: u16 = 256;
273const LEAF: u16 = 0x8000;
274
275static TREE: [[u16; 2]; 256] = build_tree();
279
280const fn build_tree() -> [[u16; 2]; 256] {
281 let mut tree = [[0u16; 2]; 256];
282 let mut next = 1u16;
283 let mut sym = 0;
284 while sym < TABLE.len() {
285 let (code, bits) = TABLE[sym];
286 let mut node = 0usize;
287 let mut i = bits;
288 while i > 1 {
289 i -= 1;
290 let bit = ((code >> i) & 1) as usize;
291 if tree[node][bit] == 0 {
292 tree[node][bit] = next;
293 next += 1;
294 }
295 node = tree[node][bit] as usize;
296 }
297 tree[node][(code & 1) as usize] = LEAF | sym as u16;
298 sym += 1;
299 }
300 tree
301}
302
303#[must_use]
305pub fn encoded_len(s: &[u8]) -> usize {
306 let bits: usize = s
307 .iter()
308 .map(|&b| usize::from(TABLE[usize::from(b)].1))
309 .sum();
310 bits.div_ceil(8)
311}
312
313pub fn encode(s: &[u8], out: &mut Vec<u8>) {
316 let mut acc: u64 = 0;
317 let mut n: u32 = 0;
318 for &b in s {
319 let (code, bits) = TABLE[usize::from(b)];
320 acc = (acc << bits) | u64::from(code);
321 n += u32::from(bits);
322 while n >= 8 {
323 n -= 8;
324 out.push((acc >> n) as u8);
325 }
326 acc &= (1u64 << n) - 1;
327 }
328 if n > 0 {
329 out.push(((acc << (8 - n)) as u8) | (0xff >> n));
330 }
331}
332
333pub fn decode(input: &[u8], out: &mut Vec<u8>, cap: usize) -> Result<(), DecodeError> {
342 let start = out.len();
343 let mut node = 0usize;
344 let mut pad_bits = 0u32;
345 let mut pad_ones = true;
346 for &byte in input {
347 for shift in (0..8).rev() {
348 let bit = usize::from((byte >> shift) & 1);
349 let next = TREE[node][bit];
350 if next & LEAF != 0 {
351 let sym = next & !LEAF;
352 if sym == EOS {
353 return Err(DecodeError::Malformed {
354 what: "EOS in a Huffman string",
355 });
356 }
357 if out.len() - start >= cap {
358 return Err(DecodeError::TooLarge {
359 what: "string",
360 declared: (out.len() - start + 1) as u64,
361 cap: cap as u64,
362 });
363 }
364 out.push(sym as u8);
365 node = 0;
366 pad_bits = 0;
367 pad_ones = true;
368 } else {
369 node = usize::from(next);
370 pad_bits += 1;
371 pad_ones &= bit == 1;
372 }
373 }
374 }
375 if pad_bits > 7 || !pad_ones {
376 return Err(DecodeError::Malformed {
377 what: "Huffman padding",
378 });
379 }
380 Ok(())
381}
382
383#[cfg(test)]
384mod tests {
385 use super::*;
386
387 fn round_trip(s: &[u8]) {
388 let mut coded = Vec::new();
389 encode(s, &mut coded);
390 assert_eq!(coded.len(), encoded_len(s));
391 let mut back = Vec::new();
392 decode(&coded, &mut back, usize::MAX).unwrap();
393 assert_eq!(back, s);
394 }
395
396 #[test]
397 fn every_symbol_round_trips() {
398 let all: Vec<u8> = (0..=255).collect();
399 round_trip(&all);
400 for b in 0..=255u8 {
401 round_trip(&[b]);
402 round_trip(&[b, b, b]);
403 }
404 }
405
406 #[test]
407 fn rfc7541_c4_1_authority() {
408 let mut coded = Vec::new();
410 encode(b"www.example.com", &mut coded);
411 assert_eq!(
412 coded,
413 [
414 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff
415 ]
416 );
417 }
418
419 #[test]
420 fn padding_must_be_short_and_all_ones() {
421 let mut out = Vec::new();
422 decode(&[0x07], &mut out, 8).unwrap();
424 assert_eq!(out, b"0");
425 assert_eq!(
427 decode(&[0x00], &mut Vec::new(), 8),
428 Err(DecodeError::Malformed {
429 what: "Huffman padding"
430 })
431 );
432 assert_eq!(
434 decode(&[0x07, 0xff], &mut Vec::new(), 8),
435 Err(DecodeError::Malformed {
436 what: "Huffman padding"
437 })
438 );
439 assert_eq!(
441 decode(&[0xff, 0xff, 0xff, 0xff], &mut Vec::new(), 8),
442 Err(DecodeError::Malformed {
443 what: "EOS in a Huffman string"
444 })
445 );
446 }
447
448 #[test]
449 fn the_cap_bounds_the_decoded_length() {
450 let mut coded = Vec::new();
451 encode(b"aaaa", &mut coded);
452 assert!(matches!(
453 decode(&coded, &mut Vec::new(), 3),
454 Err(DecodeError::TooLarge { what: "string", .. })
455 ));
456 }
457}