1 /* Copyright (C) 2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 use nom::error::ErrorKind;
19 use nom::Err;
20 use nom::IResult;
21 
http2_huffman_table_len5(n: u32) -> Option<u8>22 fn http2_huffman_table_len5(n: u32) -> Option<u8> {
23     match n {
24         0 => Some(48),
25         1 => Some(49),
26         2 => Some(50),
27         3 => Some(97),
28         4 => Some(99),
29         5 => Some(101),
30         6 => Some(105),
31         7 => Some(111),
32         8 => Some(115),
33         9 => Some(116),
34         _ => None,
35     }
36 }
37 
38 named!(http2_decode_huffman_len5<(&[u8], usize), u8>,
39     complete!( map_opt!(take_bits!(5u32), http2_huffman_table_len5) )
40 );
41 
http2_huffman_table_len6(n: u32) -> Option<u8>42 fn http2_huffman_table_len6(n: u32) -> Option<u8> {
43     match n {
44         0x14 => Some(32),
45         0x15 => Some(37),
46         0x16 => Some(45),
47         0x17 => Some(46),
48         0x18 => Some(47),
49         0x19 => Some(51),
50         0x1a => Some(52),
51         0x1b => Some(53),
52         0x1c => Some(54),
53         0x1d => Some(55),
54         0x1e => Some(56),
55         0x1f => Some(57),
56         0x20 => Some(61),
57         0x21 => Some(65),
58         0x22 => Some(95),
59         0x23 => Some(98),
60         0x24 => Some(100),
61         0x25 => Some(102),
62         0x26 => Some(103),
63         0x27 => Some(104),
64         0x28 => Some(108),
65         0x29 => Some(109),
66         0x2a => Some(110),
67         0x2b => Some(112),
68         0x2c => Some(114),
69         0x2d => Some(117),
70         _ => None,
71     }
72 }
73 
74 named!(http2_decode_huffman_len6<(&[u8], usize), u8>,
75     complete!( map_opt!(take_bits!(6u32), http2_huffman_table_len6))
76 );
77 
http2_huffman_table_len7(n: u32) -> Option<u8>78 fn http2_huffman_table_len7(n: u32) -> Option<u8> {
79     match n {
80         0x5c => Some(58),
81         0x5d => Some(66),
82         0x5e => Some(67),
83         0x5f => Some(68),
84         0x60 => Some(69),
85         0x61 => Some(70),
86         0x62 => Some(71),
87         0x63 => Some(72),
88         0x64 => Some(73),
89         0x65 => Some(74),
90         0x66 => Some(75),
91         0x67 => Some(76),
92         0x68 => Some(77),
93         0x69 => Some(78),
94         0x6a => Some(79),
95         0x6b => Some(80),
96         0x6c => Some(81),
97         0x6d => Some(82),
98         0x6e => Some(83),
99         0x6f => Some(84),
100         0x70 => Some(85),
101         0x71 => Some(86),
102         0x72 => Some(87),
103         0x73 => Some(89),
104         0x74 => Some(106),
105         0x75 => Some(107),
106         0x76 => Some(113),
107         0x77 => Some(118),
108         0x78 => Some(119),
109         0x79 => Some(120),
110         0x7a => Some(121),
111         0x7b => Some(122),
112         _ => None,
113     }
114 }
115 
116 named!(http2_decode_huffman_len7<(&[u8], usize), u8>,
117 complete!( map_opt!(take_bits!(7u32), http2_huffman_table_len7))
118 );
119 
http2_huffman_table_len8(n: u32) -> Option<u8>120 fn http2_huffman_table_len8(n: u32) -> Option<u8> {
121     match n {
122         0xf8 => Some(38),
123         0xf9 => Some(42),
124         0xfa => Some(44),
125         0xfb => Some(59),
126         0xfc => Some(88),
127         0xfd => Some(90),
128         _ => None,
129     }
130 }
131 
132 named!(http2_decode_huffman_len8<(&[u8], usize), u8>,
133 complete!( map_opt!(take_bits!(8u32), http2_huffman_table_len8))
134 );
135 
http2_huffman_table_len10(n: u32) -> Option<u8>136 fn http2_huffman_table_len10(n: u32) -> Option<u8> {
137     match n {
138         0x3f8 => Some(33),
139         0x3f9 => Some(34),
140         0x3fa => Some(40),
141         0x3fb => Some(41),
142         0x3fc => Some(63),
143         _ => None,
144     }
145 }
146 
147 named!(http2_decode_huffman_len10<(&[u8], usize), u8>,
148 complete!( map_opt!(take_bits!(10u32), http2_huffman_table_len10))
149 );
150 
http2_huffman_table_len11(n: u32) -> Option<u8>151 fn http2_huffman_table_len11(n: u32) -> Option<u8> {
152     match n {
153         0x7fa => Some(39),
154         0x7fb => Some(43),
155         0x7fc => Some(124),
156         _ => None,
157     }
158 }
159 
160 named!(http2_decode_huffman_len11<(&[u8], usize), u8>,
161 complete!( map_opt!(take_bits!(11u32), http2_huffman_table_len11))
162 );
163 
http2_huffman_table_len12(n: u32) -> Option<u8>164 fn http2_huffman_table_len12(n: u32) -> Option<u8> {
165     match n {
166         0xffa => Some(35),
167         0xffb => Some(62),
168         _ => None,
169     }
170 }
171 
172 named!(http2_decode_huffman_len12<(&[u8], usize), u8>,
173 complete!( map_opt!(take_bits!(12u32), http2_huffman_table_len12))
174 );
175 
http2_huffman_table_len13(n: u32) -> Option<u8>176 fn http2_huffman_table_len13(n: u32) -> Option<u8> {
177     match n {
178         0x1ff8 => Some(0),
179         0x1ff9 => Some(36),
180         0x1ffa => Some(64),
181         0x1ffb => Some(91),
182         0x1ffc => Some(93),
183         0x1ffd => Some(126),
184         _ => None,
185     }
186 }
187 
188 named!(http2_decode_huffman_len13<(&[u8], usize), u8>,
189 complete!( map_opt!(take_bits!(13u32), http2_huffman_table_len13))
190 );
191 
http2_huffman_table_len14(n: u32) -> Option<u8>192 fn http2_huffman_table_len14(n: u32) -> Option<u8> {
193     match n {
194         0x3ffc => Some(94),
195         0x3ffd => Some(125),
196         _ => None,
197     }
198 }
199 
200 named!(http2_decode_huffman_len14<(&[u8], usize), u8>,
201 complete!( map_opt!(take_bits!(14u32), http2_huffman_table_len14))
202 );
203 
http2_huffman_table_len15(n: u32) -> Option<u8>204 fn http2_huffman_table_len15(n: u32) -> Option<u8> {
205     match n {
206         0x7ffc => Some(60),
207         0x7ffd => Some(96),
208         0x7ffe => Some(123),
209         _ => None,
210     }
211 }
212 
213 named!(http2_decode_huffman_len15<(&[u8], usize), u8>,
214 complete!( map_opt!(take_bits!(15u32), http2_huffman_table_len15))
215 );
216 
http2_huffman_table_len19(n: u32) -> Option<u8>217 fn http2_huffman_table_len19(n: u32) -> Option<u8> {
218     match n {
219         0x7fff0 => Some(92),
220         0x7fff1 => Some(195),
221         0x7fff2 => Some(208),
222         _ => None,
223     }
224 }
225 
226 named!(http2_decode_huffman_len19<(&[u8], usize), u8>,
227 complete!( map_opt!(take_bits!(19u32), http2_huffman_table_len19))
228 );
229 
http2_huffman_table_len20(n: u32) -> Option<u8>230 fn http2_huffman_table_len20(n: u32) -> Option<u8> {
231     match n {
232         0xfffe6 => Some(128),
233         0xfffe7 => Some(130),
234         0xfffe8 => Some(131),
235         0xfffe9 => Some(162),
236         0xfffea => Some(184),
237         0xfffeb => Some(194),
238         0xfffec => Some(224),
239         0xfffed => Some(226),
240         _ => None,
241     }
242 }
243 
244 named!(http2_decode_huffman_len20<(&[u8], usize), u8>,
245 complete!( map_opt!(take_bits!(20u32), http2_huffman_table_len20))
246 );
247 
http2_huffman_table_len21(n: u32) -> Option<u8>248 fn http2_huffman_table_len21(n: u32) -> Option<u8> {
249     match n {
250         0x1fffdc => Some(153),
251         0x1fffdd => Some(161),
252         0x1fffde => Some(167),
253         0x1fffdf => Some(172),
254         0x1fffe0 => Some(176),
255         0x1fffe1 => Some(177),
256         0x1fffe2 => Some(179),
257         0x1fffe3 => Some(209),
258         0x1fffe4 => Some(216),
259         0x1fffe5 => Some(217),
260         0x1fffe6 => Some(227),
261         0x1fffe7 => Some(229),
262         0x1fffe8 => Some(230),
263         _ => None,
264     }
265 }
266 
267 named!(http2_decode_huffman_len21<(&[u8], usize), u8>,
268 complete!( map_opt!(take_bits!(21u32), http2_huffman_table_len21))
269 );
270 
http2_huffman_table_len22(n: u32) -> Option<u8>271 fn http2_huffman_table_len22(n: u32) -> Option<u8> {
272     match n {
273         0x3fffd2 => Some(129),
274         0x3fffd3 => Some(132),
275         0x3fffd4 => Some(133),
276         0x3fffd5 => Some(134),
277         0x3fffd6 => Some(136),
278         0x3fffd7 => Some(146),
279         0x3fffd8 => Some(154),
280         0x3fffd9 => Some(156),
281         0x3fffda => Some(160),
282         0x3fffdb => Some(163),
283         0x3fffdc => Some(164),
284         0x3fffdd => Some(169),
285         0x3fffde => Some(170),
286         0x3fffdf => Some(173),
287         0x3fffe0 => Some(178),
288         0x3fffe1 => Some(181),
289         0x3fffe2 => Some(185),
290         0x3fffe3 => Some(186),
291         0x3fffe4 => Some(187),
292         0x3fffe5 => Some(189),
293         0x3fffe6 => Some(190),
294         0x3fffe7 => Some(196),
295         0x3fffe8 => Some(198),
296         0x3fffe9 => Some(228),
297         0x3fffea => Some(232),
298         0x3fffeb => Some(233),
299         _ => None,
300     }
301 }
302 
303 named!(http2_decode_huffman_len22<(&[u8], usize), u8>,
304 complete!( map_opt!(take_bits!(22u32), http2_huffman_table_len22))
305 );
306 
http2_huffman_table_len23(n: u32) -> Option<u8>307 fn http2_huffman_table_len23(n: u32) -> Option<u8> {
308     match n {
309         0x7fffd8 => Some(1),
310         0x7fffd9 => Some(135),
311         0x7fffda => Some(137),
312         0x7fffdb => Some(138),
313         0x7fffdc => Some(139),
314         0x7fffdd => Some(140),
315         0x7fffde => Some(141),
316         0x7fffdf => Some(143),
317         0x7fffe0 => Some(147),
318         0x7fffe1 => Some(149),
319         0x7fffe2 => Some(150),
320         0x7fffe3 => Some(151),
321         0x7fffe4 => Some(152),
322         0x7fffe5 => Some(155),
323         0x7fffe6 => Some(157),
324         0x7fffe7 => Some(158),
325         0x7fffe8 => Some(165),
326         0x7fffe9 => Some(166),
327         0x7fffea => Some(168),
328         0x7fffeb => Some(174),
329         0x7fffec => Some(175),
330         0x7fffed => Some(180),
331         0x7fffee => Some(182),
332         0x7fffef => Some(183),
333         0x7ffff0 => Some(188),
334         0x7ffff1 => Some(191),
335         0x7ffff2 => Some(197),
336         0x7ffff3 => Some(231),
337         0x7ffff4 => Some(239),
338         _ => None,
339     }
340 }
341 
342 named!(http2_decode_huffman_len23<(&[u8], usize), u8>,
343     complete!( map_opt!(take_bits!(23u32), http2_huffman_table_len23))
344 );
345 
http2_huffman_table_len24(n: u32) -> Option<u8>346 fn http2_huffman_table_len24(n: u32) -> Option<u8> {
347     match n {
348         0xffffea => Some(9),
349         0xffffeb => Some(142),
350         0xffffec => Some(144),
351         0xffffed => Some(145),
352         0xffffee => Some(148),
353         0xffffef => Some(159),
354         0xfffff0 => Some(171),
355         0xfffff1 => Some(206),
356         0xfffff2 => Some(215),
357         0xfffff3 => Some(225),
358         0xfffff4 => Some(236),
359         0xfffff5 => Some(237),
360         _ => None,
361     }
362 }
363 
364 named!(http2_decode_huffman_len24<(&[u8], usize), u8>,
365     complete!( map_opt!(take_bits!(24u32), http2_huffman_table_len24))
366 );
367 
http2_huffman_table_len25(n: u32) -> Option<u8>368 fn http2_huffman_table_len25(n: u32) -> Option<u8> {
369     match n {
370         0x1ffffec => Some(199),
371         0x1ffffed => Some(207),
372         0x1ffffee => Some(234),
373         0x1ffffef => Some(235),
374         _ => None,
375     }
376 }
377 
378 named!(http2_decode_huffman_len25<(&[u8], usize), u8>,
379     complete!( map_opt!(take_bits!(25u32), http2_huffman_table_len25))
380 );
381 
http2_huffman_table_len26(n: u32) -> Option<u8>382 fn http2_huffman_table_len26(n: u32) -> Option<u8> {
383     match n {
384         0x3ffffe0 => Some(192),
385         0x3ffffe1 => Some(193),
386         0x3ffffe2 => Some(200),
387         0x3ffffe3 => Some(201),
388         0x3ffffe4 => Some(202),
389         0x3ffffe5 => Some(205),
390         0x3ffffe6 => Some(210),
391         0x3ffffe7 => Some(213),
392         0x3ffffe8 => Some(218),
393         0x3ffffe9 => Some(219),
394         0x3ffffea => Some(238),
395         0x3ffffeb => Some(240),
396         0x3ffffec => Some(242),
397         0x3ffffed => Some(243),
398         0x3ffffee => Some(255),
399         _ => None,
400     }
401 }
402 
403 named!(http2_decode_huffman_len26<(&[u8], usize), u8>,
404     complete!( map_opt!(take_bits!(26u32), http2_huffman_table_len26))
405 );
406 
http2_huffman_table_len27(n: u32) -> Option<u8>407 fn http2_huffman_table_len27(n: u32) -> Option<u8> {
408     match n {
409         0x7ffffde => Some(203),
410         0x7ffffdf => Some(204),
411         0x7ffffe0 => Some(211),
412         0x7ffffe1 => Some(212),
413         0x7ffffe2 => Some(214),
414         0x7ffffe3 => Some(221),
415         0x7ffffe4 => Some(222),
416         0x7ffffe5 => Some(223),
417         0x7ffffe6 => Some(241),
418         0x7ffffe7 => Some(244),
419         0x7ffffe8 => Some(245),
420         0x7ffffe9 => Some(246),
421         0x7ffffea => Some(247),
422         0x7ffffeb => Some(248),
423         0x7ffffec => Some(250),
424         0x7ffffed => Some(251),
425         0x7ffffee => Some(252),
426         0x7ffffef => Some(253),
427         0x7fffff0 => Some(254),
428         _ => None,
429     }
430 }
431 
432 named!(http2_decode_huffman_len27<(&[u8], usize), u8>,
433     complete!( map_opt!(take_bits!(27u32), http2_huffman_table_len27))
434 );
435 
http2_huffman_table_len28(n: u32) -> Option<u8>436 fn http2_huffman_table_len28(n: u32) -> Option<u8> {
437     match n {
438         0xfffffe2 => Some(2),
439         0xfffffe3 => Some(3),
440         0xfffffe4 => Some(4),
441         0xfffffe5 => Some(5),
442         0xfffffe6 => Some(6),
443         0xfffffe7 => Some(7),
444         0xfffffe8 => Some(8),
445         0xfffffe9 => Some(11),
446         0xfffffea => Some(12),
447         0xfffffeb => Some(14),
448         0xfffffec => Some(15),
449         0xfffffed => Some(16),
450         0xfffffee => Some(17),
451         0xfffffef => Some(18),
452         0xffffff0 => Some(19),
453         0xffffff1 => Some(20),
454         0xffffff2 => Some(21),
455         0xffffff3 => Some(23),
456         0xffffff4 => Some(24),
457         0xffffff5 => Some(25),
458         0xffffff6 => Some(26),
459         0xffffff7 => Some(27),
460         0xffffff8 => Some(28),
461         0xffffff9 => Some(29),
462         0xffffffa => Some(30),
463         0xffffffb => Some(31),
464         0xffffffc => Some(127),
465         0xffffffd => Some(220),
466         0xffffffe => Some(249),
467         _ => None,
468     }
469 }
470 
471 named!(http2_decode_huffman_len28<(&[u8], usize), u8>,
472     complete!( map_opt!(take_bits!(28u32), http2_huffman_table_len28))
473 );
474 
http2_huffman_table_len30(n: u32) -> Option<u8>475 fn http2_huffman_table_len30(n: u32) -> Option<u8> {
476     match n {
477         0x3ffffffc => Some(10),
478         0x3ffffffd => Some(13),
479         0x3ffffffe => Some(22),
480         // 0x3fffffff => Some(256),
481         _ => None,
482     }
483 }
484 
485 named!(http2_decode_huffman_len30<(&[u8], usize), u8>,
486     complete!( map_opt!(take_bits!(30u32), http2_huffman_table_len30))
487 );
488 
489 //hack to end many0 even if some bits are remaining
http2_decode_huffman_end(input: (&[u8], usize)) -> IResult<(&[u8], usize), u8>490 fn http2_decode_huffman_end(input: (&[u8], usize)) -> IResult<(&[u8], usize), u8> {
491     return Err(Err::Error((input, ErrorKind::Eof)));
492 }
493 
494 //we could profile and optimize performance here
495 named!(pub http2_decode_huffman<(&[u8], usize), u8>,
496     alt!(http2_decode_huffman_len5 | http2_decode_huffman_len6 | http2_decode_huffman_len7 |
497     http2_decode_huffman_len8 | http2_decode_huffman_len10 | http2_decode_huffman_len11 |
498     http2_decode_huffman_len12 | http2_decode_huffman_len13 | http2_decode_huffman_len14 |
499     http2_decode_huffman_len15 | http2_decode_huffman_len19 | http2_decode_huffman_len20 |
500     http2_decode_huffman_len21 | http2_decode_huffman_len22 | http2_decode_huffman_len23 |
501     http2_decode_huffman_len24 | http2_decode_huffman_len25 | http2_decode_huffman_len26 |
502     http2_decode_huffman_len27 | http2_decode_huffman_len28 | http2_decode_huffman_len30 |
503     http2_decode_huffman_end)
504 );
505