| | varnish-cache/bin/varnishd/hpack/vhp_decode.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2016 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Martin Blix Grydeland <martin@varnish-software.com> |
5 |
|
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
* |
30 |
|
*/ |
31 |
|
|
32 |
|
#include "config.h" |
33 |
|
|
34 |
|
#include <stdio.h> |
35 |
|
#include <string.h> |
36 |
|
#include <limits.h> |
37 |
|
#include <stdlib.h> |
38 |
|
|
39 |
|
#include "vdef.h" |
40 |
|
#include "vas.h" |
41 |
|
#include "miniobj.h" |
42 |
|
|
43 |
|
#include "hpack/vhp.h" |
44 |
|
|
45 |
|
#include "vhp_hufdec.h" |
46 |
|
|
47 |
|
struct vhd_ctx { |
48 |
|
struct vhd_decode *d; |
49 |
|
struct vht_table *tbl; |
50 |
|
const uint8_t *in; |
51 |
|
const uint8_t *in_e; |
52 |
|
char *out; |
53 |
|
char *out_e; |
54 |
|
}; |
55 |
|
|
56 |
|
typedef enum vhd_ret_e vhd_state_f(struct vhd_ctx *ctx, unsigned first); |
57 |
|
|
58 |
|
/* Function flags */ |
59 |
|
#define VHD_INCREMENTAL (1U << 0) |
60 |
|
|
61 |
|
/* Functions */ |
62 |
|
enum vhd_func_e { |
63 |
|
#define VHD_FSM_FUNC(NAME, func) \ |
64 |
|
VHD_F_##NAME, |
65 |
|
#include "tbl/vhd_fsm_funcs.h" |
66 |
|
VHD_F__MAX, |
67 |
|
}; |
68 |
|
#define VHD_FSM_FUNC(NAME, func) \ |
69 |
|
static vhd_state_f func; |
70 |
|
#include "tbl/vhd_fsm_funcs.h" |
71 |
|
|
72 |
|
/* States */ |
73 |
|
enum vhd_state_e { |
74 |
|
VHD_S__MIN = -1, |
75 |
|
#define VHD_FSM(STATE, FUNC, arg1, arg2) \ |
76 |
|
VHD_S_##STATE, |
77 |
|
#include "tbl/vhd_fsm.h" |
78 |
|
VHD_S__MAX, |
79 |
|
}; |
80 |
|
static const struct vhd_state { |
81 |
|
const char *name; |
82 |
|
enum vhd_func_e func; |
83 |
|
unsigned arg1; |
84 |
|
unsigned arg2; |
85 |
|
} vhd_states[VHD_S__MAX] = { |
86 |
|
#define VHD_FSM(STATE, FUNC, arg1, arg2) \ |
87 |
|
[VHD_S_##STATE] = { #STATE, VHD_F_##FUNC, arg1, arg2 }, |
88 |
|
#include "tbl/vhd_fsm.h" |
89 |
|
}; |
90 |
|
|
91 |
|
/* Utility functions */ |
92 |
|
static void |
93 |
440360 |
vhd_set_state(struct vhd_decode *d, enum vhd_state_e state) |
94 |
|
{ |
95 |
440360 |
AN(d); |
96 |
440360 |
assert(state > VHD_S__MIN && state < VHD_S__MAX); |
97 |
440360 |
d->state = state; |
98 |
440360 |
d->first = 1; |
99 |
440360 |
} |
100 |
|
|
101 |
|
static void |
102 |
307920 |
vhd_next_state(struct vhd_decode *d) |
103 |
|
{ |
104 |
307920 |
AN(d); |
105 |
307920 |
assert(d->state + 1 < VHD_S__MAX); |
106 |
307920 |
vhd_set_state(d, d->state + 1); |
107 |
307920 |
} |
108 |
|
|
109 |
|
/* State functions */ |
110 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
111 |
79600 |
vhd_skip(struct vhd_ctx *ctx, unsigned first) |
112 |
|
{ |
113 |
79600 |
AN(ctx); |
114 |
79600 |
AN(first); |
115 |
79600 |
vhd_next_state(ctx->d); |
116 |
79600 |
return (VHD_AGAIN); |
117 |
|
} |
118 |
|
|
119 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
120 |
75200 |
vhd_goto(struct vhd_ctx *ctx, unsigned first) |
121 |
|
{ |
122 |
|
const struct vhd_state *s; |
123 |
|
|
124 |
75200 |
AN(ctx); |
125 |
75200 |
AN(first); |
126 |
75200 |
assert(ctx->d->state < VHD_S__MAX); |
127 |
75200 |
s = &vhd_states[ctx->d->state]; |
128 |
75200 |
assert(s->arg1 < VHD_S__MAX); |
129 |
75200 |
vhd_set_state(ctx->d, s->arg1); |
130 |
75200 |
return (VHD_AGAIN); |
131 |
|
} |
132 |
|
|
133 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
134 |
40160 |
vhd_idle(struct vhd_ctx *ctx, unsigned first) |
135 |
|
{ |
136 |
|
uint8_t c; |
137 |
|
|
138 |
40160 |
AN(ctx); |
139 |
40160 |
(void)first; |
140 |
|
|
141 |
40160 |
while (ctx->in < ctx->in_e) { |
142 |
31240 |
c = *ctx->in; |
143 |
31240 |
if ((c & 0x80) == 0x80) |
144 |
6000 |
vhd_set_state(ctx->d, VHD_S_HP61_START); |
145 |
25240 |
else if ((c & 0xc0) == 0x40) |
146 |
3840 |
vhd_set_state(ctx->d, VHD_S_HP621_START); |
147 |
21400 |
else if ((c & 0xf0) == 0x00) |
148 |
21200 |
vhd_set_state(ctx->d, VHD_S_HP622_START); |
149 |
200 |
else if ((c & 0xf0) == 0x10) |
150 |
200 |
vhd_set_state(ctx->d, VHD_S_HP623_START); |
151 |
0 |
else if ((c & 0xe0) == 0x20) |
152 |
0 |
vhd_set_state(ctx->d, VHD_S_HP63_START); |
153 |
|
else |
154 |
0 |
return (VHD_ERR_ARG); |
155 |
31240 |
return (VHD_AGAIN); |
156 |
|
} |
157 |
|
|
158 |
8920 |
return (VHD_OK); |
159 |
40160 |
} |
160 |
|
|
161 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
162 |
81560 |
vhd_integer(struct vhd_ctx *ctx, unsigned first) |
163 |
|
{ |
164 |
|
const struct vhd_state *s; |
165 |
|
struct vhd_int *i; |
166 |
|
uint8_t c; |
167 |
|
unsigned mask; |
168 |
|
|
169 |
81560 |
assert(UINT_MAX >= UINT32_MAX); |
170 |
|
|
171 |
81560 |
AN(ctx); |
172 |
81560 |
assert(ctx->d->state < VHD_S__MAX); |
173 |
81560 |
s = &vhd_states[ctx->d->state]; |
174 |
81560 |
i = ctx->d->integer; |
175 |
|
|
176 |
81560 |
if (first) { |
177 |
80600 |
INIT_OBJ(i, VHD_INT_MAGIC); |
178 |
80600 |
i->pfx = s->arg1; |
179 |
80600 |
assert(i->pfx >= 4 && i->pfx <= 7); |
180 |
80600 |
} |
181 |
81560 |
CHECK_OBJ_NOTNULL(i, VHD_INT_MAGIC); |
182 |
|
|
183 |
84880 |
while (ctx->in < ctx->in_e) { |
184 |
83920 |
c = *ctx->in; |
185 |
83920 |
ctx->in++; |
186 |
83920 |
if (i->pfx) { |
187 |
80600 |
mask = (1U << i->pfx) - 1; |
188 |
80600 |
i->pfx = 0; |
189 |
80600 |
i->v = c & mask; |
190 |
80600 |
if (i->v < mask) { |
191 |
79520 |
vhd_next_state(ctx->d); |
192 |
79520 |
return (VHD_AGAIN); |
193 |
|
} |
194 |
1080 |
} else { |
195 |
3320 |
if ((i->m == 28 && (c & 0x78)) || i->m > 28) |
196 |
240 |
return (VHD_ERR_INT); |
197 |
3080 |
i->v += (c & 0x7f) * ((uint32_t)1 << i->m); |
198 |
3080 |
i->m += 7; |
199 |
3080 |
if (!(c & 0x80)) { |
200 |
840 |
vhd_next_state(ctx->d); |
201 |
840 |
return (VHD_AGAIN); |
202 |
|
} |
203 |
|
} |
204 |
|
} |
205 |
960 |
return (VHD_MORE); |
206 |
81560 |
} |
207 |
|
|
208 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
209 |
0 |
vhd_set_max(struct vhd_ctx *ctx, unsigned first) |
210 |
|
{ |
211 |
0 |
AN(ctx); |
212 |
0 |
AN(first); |
213 |
0 |
CHECK_OBJ_NOTNULL(ctx->d->integer, VHD_INT_MAGIC); |
214 |
0 |
if (ctx->tbl == NULL) |
215 |
0 |
return (VHD_ERR_UPD); |
216 |
0 |
if (VHT_SetMaxTableSize(ctx->tbl, ctx->d->integer->v)) |
217 |
0 |
return (VHD_ERR_UPD); |
218 |
0 |
vhd_next_state(ctx->d); |
219 |
0 |
return (VHD_AGAIN); |
220 |
0 |
} |
221 |
|
|
222 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
223 |
31160 |
vhd_set_idx(struct vhd_ctx *ctx, unsigned first) |
224 |
|
{ |
225 |
31160 |
AN(ctx); |
226 |
31160 |
AN(first); |
227 |
31160 |
CHECK_OBJ_NOTNULL(ctx->d->integer, VHD_INT_MAGIC); |
228 |
31160 |
ctx->d->index = ctx->d->integer->v; |
229 |
31160 |
vhd_next_state(ctx->d); |
230 |
31160 |
return (VHD_AGAIN); |
231 |
|
} |
232 |
|
|
233 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
234 |
69920 |
vhd_lookup(struct vhd_ctx *ctx, unsigned first) |
235 |
|
{ |
236 |
|
const struct vhd_state *s; |
237 |
|
struct vhd_lookup *lu; |
238 |
|
const char *p; |
239 |
|
size_t l; |
240 |
|
|
241 |
69920 |
AN(ctx); |
242 |
69920 |
assert(ctx->d->state < VHD_S__MAX); |
243 |
69920 |
s = &vhd_states[ctx->d->state]; |
244 |
69920 |
lu = ctx->d->lookup; |
245 |
|
|
246 |
69920 |
if (first) |
247 |
15360 |
INIT_OBJ(lu, VHD_LOOKUP_MAGIC); |
248 |
69920 |
CHECK_OBJ_NOTNULL(lu, VHD_LOOKUP_MAGIC); |
249 |
|
|
250 |
69920 |
switch (s->arg1) { |
251 |
|
case VHD_NAME: |
252 |
|
case VHD_NAME_SEC: |
253 |
41440 |
p = VHT_LookupName(ctx->tbl, ctx->d->index, &l); |
254 |
41440 |
break; |
255 |
|
case VHD_VALUE: |
256 |
|
case VHD_VALUE_SEC: |
257 |
28480 |
p = VHT_LookupValue(ctx->tbl, ctx->d->index, &l); |
258 |
28480 |
break; |
259 |
|
default: |
260 |
0 |
WRONG("vhd_lookup wrong arg1"); |
261 |
0 |
break; |
262 |
|
} |
263 |
69920 |
if (first && p == NULL) |
264 |
0 |
return (VHD_ERR_IDX); |
265 |
69920 |
AN(p); |
266 |
69920 |
assert(l <= UINT_MAX); |
267 |
69920 |
if (first) |
268 |
15360 |
lu->l = l; |
269 |
|
|
270 |
69920 |
assert(lu->l <= l); |
271 |
69920 |
p += l - lu->l; |
272 |
69920 |
l = vmin_t(size_t, lu->l, ctx->out_e - ctx->out); |
273 |
69920 |
memcpy(ctx->out, p, l); |
274 |
69920 |
ctx->out += l; |
275 |
69920 |
lu->l -= l; |
276 |
|
|
277 |
69920 |
if (lu->l == 0) { |
278 |
15360 |
vhd_next_state(ctx->d); |
279 |
15360 |
return (s->arg1); |
280 |
|
} |
281 |
54560 |
assert(ctx->out == ctx->out_e); |
282 |
54560 |
return (VHD_BUF); |
283 |
69920 |
} |
284 |
|
|
285 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
286 |
520 |
vhd_new(struct vhd_ctx *ctx, unsigned first) |
287 |
|
{ |
288 |
520 |
AN(ctx); |
289 |
520 |
AN(first); |
290 |
520 |
if (ctx->tbl != NULL) |
291 |
360 |
VHT_NewEntry(ctx->tbl); |
292 |
520 |
vhd_next_state(ctx->d); |
293 |
520 |
return (VHD_AGAIN); |
294 |
|
} |
295 |
|
|
296 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
297 |
3320 |
vhd_new_idx(struct vhd_ctx *ctx, unsigned first) |
298 |
|
{ |
299 |
3320 |
AN(ctx); |
300 |
3320 |
AN(first); |
301 |
3320 |
if (ctx->tbl != NULL) { |
302 |
3320 |
if (VHT_NewEntry_Indexed(ctx->tbl, ctx->d->index)) |
303 |
0 |
return (VHD_ERR_IDX); |
304 |
3320 |
} |
305 |
3320 |
vhd_next_state(ctx->d); |
306 |
3320 |
return (VHD_AGAIN); |
307 |
3320 |
} |
308 |
|
|
309 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
310 |
25240 |
vhd_branch_zidx(struct vhd_ctx *ctx, unsigned first) |
311 |
|
{ |
312 |
|
const struct vhd_state *s; |
313 |
|
|
314 |
25240 |
AN(ctx); |
315 |
25240 |
(void)first; |
316 |
25240 |
assert(ctx->d->state < VHD_S__MAX); |
317 |
25240 |
s = &vhd_states[ctx->d->state]; |
318 |
25240 |
assert(s->arg1 < VHD_S__MAX); |
319 |
|
|
320 |
25240 |
if (ctx->d->index == 0) |
321 |
21720 |
vhd_set_state(ctx->d, s->arg1); |
322 |
|
else |
323 |
3520 |
vhd_next_state(ctx->d); |
324 |
25240 |
return (VHD_AGAIN); |
325 |
|
} |
326 |
|
|
327 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
328 |
49040 |
vhd_branch_bit0(struct vhd_ctx *ctx, unsigned first) |
329 |
|
{ |
330 |
|
const struct vhd_state *s; |
331 |
|
|
332 |
49040 |
AN(ctx); |
333 |
49040 |
(void)first; |
334 |
49040 |
assert(ctx->d->state < VHD_S__MAX); |
335 |
49040 |
s = &vhd_states[ctx->d->state]; |
336 |
49040 |
assert(s->arg1 < VHD_S__MAX); |
337 |
|
|
338 |
49040 |
if (ctx->in == ctx->in_e) |
339 |
320 |
return (VHD_MORE); |
340 |
|
|
341 |
48720 |
if (*ctx->in & 0x80) |
342 |
2680 |
vhd_set_state(ctx->d, s->arg1); |
343 |
|
else |
344 |
46040 |
vhd_next_state(ctx->d); |
345 |
48720 |
return (VHD_AGAIN); |
346 |
49040 |
} |
347 |
|
|
348 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
349 |
78160 |
vhd_raw(struct vhd_ctx *ctx, unsigned first) |
350 |
|
{ |
351 |
|
const struct vhd_state *s; |
352 |
|
struct vhd_raw *raw; |
353 |
|
size_t l2; |
354 |
|
|
355 |
78160 |
AN(ctx); |
356 |
78160 |
assert(ctx->d->state < VHD_S__MAX); |
357 |
78160 |
s = &vhd_states[ctx->d->state]; |
358 |
|
|
359 |
78160 |
raw = ctx->d->raw; |
360 |
78160 |
if (first) { |
361 |
46040 |
CHECK_OBJ_NOTNULL(ctx->d->integer, VHD_INT_MAGIC); |
362 |
46040 |
l2 = ctx->d->integer->v; |
363 |
46040 |
INIT_OBJ(raw, VHD_RAW_MAGIC); |
364 |
46040 |
raw->l = l2; |
365 |
46040 |
} |
366 |
78160 |
CHECK_OBJ_NOTNULL(raw, VHD_RAW_MAGIC); |
367 |
|
|
368 |
154040 |
while (raw->l > 0) { |
369 |
108200 |
l2 = raw->l; |
370 |
108200 |
if (l2 > (ctx->in_e - ctx->in)) |
371 |
42400 |
l2 = ctx->in_e - ctx->in; |
372 |
108200 |
if (l2 == 0) |
373 |
22200 |
return (VHD_MORE); |
374 |
86000 |
if (l2 > (ctx->out_e - ctx->out)) |
375 |
20240 |
l2 = ctx->out_e - ctx->out; |
376 |
86000 |
if (l2 == 0) |
377 |
10120 |
return (VHD_BUF); |
378 |
75880 |
memcpy(ctx->out, ctx->in, l2); |
379 |
75880 |
ctx->in += l2; |
380 |
75880 |
if (ctx->tbl != NULL && (s->arg2 & VHD_INCREMENTAL)) { |
381 |
24440 |
switch (s->arg1) { |
382 |
|
case VHD_NAME: |
383 |
1240 |
VHT_AppendName(ctx->tbl, ctx->out, l2); |
384 |
1240 |
break; |
385 |
|
case VHD_VALUE: |
386 |
23200 |
VHT_AppendValue(ctx->tbl, ctx->out, l2); |
387 |
23200 |
break; |
388 |
|
default: |
389 |
0 |
WRONG("vhd_raw wrong arg1"); |
390 |
0 |
break; |
391 |
|
} |
392 |
24440 |
} |
393 |
75880 |
ctx->out += l2; |
394 |
75880 |
raw->l -= l2; |
395 |
|
} |
396 |
45840 |
vhd_next_state(ctx->d); |
397 |
45840 |
return (s->arg1); |
398 |
78160 |
} |
399 |
|
|
400 |
|
static enum vhd_ret_e v_matchproto_(vhd_state_f) |
401 |
26320 |
vhd_huffman(struct vhd_ctx *ctx, unsigned first) |
402 |
|
{ |
403 |
|
const struct vhd_state *s; |
404 |
|
struct vhd_huffman *huf; |
405 |
|
enum vhd_ret_e r; |
406 |
|
unsigned u, l; |
407 |
|
|
408 |
26320 |
AN(ctx); |
409 |
26320 |
assert(ctx->d->state < VHD_S__MAX); |
410 |
26320 |
s = &vhd_states[ctx->d->state]; |
411 |
|
|
412 |
26320 |
huf = ctx->d->huffman; |
413 |
26320 |
if (first) { |
414 |
2680 |
CHECK_OBJ_NOTNULL(ctx->d->integer, VHD_INT_MAGIC); |
415 |
2680 |
l = ctx->d->integer->v; |
416 |
2680 |
INIT_OBJ(huf, VHD_HUFFMAN_MAGIC); |
417 |
2680 |
huf->len = l; |
418 |
2680 |
} |
419 |
26320 |
CHECK_OBJ_NOTNULL(huf, VHD_HUFFMAN_MAGIC); |
420 |
|
|
421 |
26320 |
r = VHD_OK; |
422 |
26320 |
l = 0; |
423 |
63120 |
while (1) { |
424 |
63120 |
assert(huf->pos < HUFDEC_LEN); |
425 |
63120 |
assert(hufdec[huf->pos].mask > 0); |
426 |
63120 |
assert(hufdec[huf->pos].mask <= 8); |
427 |
|
|
428 |
63120 |
if (huf->len > 0 && huf->blen < hufdec[huf->pos].mask) { |
429 |
|
/* Refill from input */ |
430 |
37400 |
if (ctx->in == ctx->in_e) { |
431 |
9120 |
r = VHD_MORE; |
432 |
9120 |
break; |
433 |
|
} |
434 |
28280 |
huf->bits = (huf->bits << 8) | *ctx->in; |
435 |
28280 |
huf->blen += 8; |
436 |
28280 |
huf->len--; |
437 |
28280 |
ctx->in++; |
438 |
28280 |
} |
439 |
|
|
440 |
54000 |
if (huf->len == 0 && huf->pos == 0 && huf->blen <= 7 && |
441 |
3400 |
huf->bits == (1U << huf->blen) - 1U) { |
442 |
|
/* End of stream */ |
443 |
2200 |
r = s->arg1; |
444 |
2200 |
vhd_next_state(ctx->d); |
445 |
2200 |
break; |
446 |
|
} |
447 |
|
|
448 |
51800 |
if (ctx->out + l == ctx->out_e) { |
449 |
14680 |
r = VHD_BUF; |
450 |
14680 |
break; |
451 |
|
} |
452 |
|
|
453 |
37120 |
if (huf->blen >= hufdec[huf->pos].mask) |
454 |
36160 |
u = huf->bits >> (huf->blen - hufdec[huf->pos].mask); |
455 |
|
else |
456 |
960 |
u = huf->bits << (hufdec[huf->pos].mask - huf->blen); |
457 |
37120 |
huf->pos += u; |
458 |
37120 |
assert(huf->pos < HUFDEC_LEN); |
459 |
|
|
460 |
37120 |
if (hufdec[huf->pos].len == 0 || |
461 |
36960 |
hufdec[huf->pos].len > huf->blen) { |
462 |
|
/* Invalid or incomplete code */ |
463 |
320 |
r = VHD_ERR_HUF; |
464 |
320 |
break; |
465 |
|
} |
466 |
|
|
467 |
36800 |
huf->blen -= hufdec[huf->pos].len; |
468 |
36800 |
huf->bits &= (1U << huf->blen) - 1U; |
469 |
|
|
470 |
36800 |
if (hufdec[huf->pos].jump) { |
471 |
1440 |
huf->pos += hufdec[huf->pos].jump; |
472 |
1440 |
assert(huf->pos < HUFDEC_LEN); |
473 |
1440 |
} else { |
474 |
35360 |
ctx->out[l++] = hufdec[huf->pos].chr; |
475 |
35360 |
huf->pos = 0; |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
26320 |
if (l > 0 && ctx->tbl != NULL && (s->arg2 & VHD_INCREMENTAL)) { |
480 |
22680 |
switch (s->arg1) { |
481 |
|
case VHD_NAME: |
482 |
1200 |
VHT_AppendName(ctx->tbl, ctx->out, l); |
483 |
1200 |
break; |
484 |
|
case VHD_VALUE: |
485 |
21480 |
VHT_AppendValue(ctx->tbl, ctx->out, l); |
486 |
21480 |
break; |
487 |
|
default: |
488 |
0 |
WRONG("vhd_raw wrong arg1"); |
489 |
0 |
break; |
490 |
|
} |
491 |
22680 |
} |
492 |
26320 |
ctx->out += l; |
493 |
|
|
494 |
26320 |
assert(r != VHD_OK); |
495 |
26320 |
return (r); |
496 |
|
} |
497 |
|
|
498 |
|
/* Public interface */ |
499 |
|
|
500 |
|
const char * |
501 |
1480 |
VHD_Error(enum vhd_ret_e r) |
502 |
|
{ |
503 |
1480 |
switch (r) { |
504 |
|
#define VHD_RET(NAME, VAL, DESC) \ |
505 |
|
case VHD_##NAME: \ |
506 |
|
return ("VHD_" #NAME " (" DESC ")"); |
507 |
|
#include "tbl/vhd_return.h" |
508 |
|
default: |
509 |
|
return ("VHD_UNKNOWN"); |
510 |
|
} |
511 |
1480 |
} |
512 |
|
|
513 |
|
enum vhd_ret_e |
514 |
184840 |
VHD_Decode(struct vhd_decode *d, struct vht_table *tbl, |
515 |
|
const uint8_t *in, size_t inlen, size_t *p_inused, |
516 |
|
char *out, size_t outlen, size_t *p_outused) |
517 |
|
{ |
518 |
|
const struct vhd_state *s; |
519 |
|
struct vhd_ctx ctx[1]; |
520 |
|
enum vhd_ret_e ret; |
521 |
|
unsigned first; |
522 |
|
|
523 |
184840 |
CHECK_OBJ_NOTNULL(d, VHD_DECODE_MAGIC); |
524 |
184840 |
CHECK_OBJ_ORNULL(tbl, VHT_TABLE_MAGIC); |
525 |
184840 |
AN(in); |
526 |
184840 |
AN(p_inused); |
527 |
184840 |
AN(out); |
528 |
184840 |
AN(p_outused); |
529 |
|
|
530 |
184840 |
if (d->error < 0) |
531 |
0 |
return (d->error); |
532 |
|
|
533 |
184840 |
assert(*p_inused <= inlen); |
534 |
184840 |
assert(*p_outused <= outlen); |
535 |
|
|
536 |
184840 |
ctx->d = d; |
537 |
184840 |
ctx->tbl = tbl; |
538 |
184840 |
ctx->in = in + *p_inused; |
539 |
184840 |
ctx->in_e = in + inlen; |
540 |
184840 |
ctx->out = out + *p_outused; |
541 |
184840 |
ctx->out_e = out + outlen; |
542 |
|
|
543 |
184840 |
do { |
544 |
560200 |
first = d->first; |
545 |
560200 |
d->first = 0; |
546 |
560200 |
assert(d->state < VHD_S__MAX); |
547 |
560200 |
s = &vhd_states[d->state]; |
548 |
560200 |
switch (s->func) { |
549 |
|
#define VHD_FSM_FUNC(NAME, func) \ |
550 |
|
case VHD_F_##NAME: \ |
551 |
|
ret = func(ctx, first); \ |
552 |
|
break; |
553 |
|
#include "tbl/vhd_fsm_funcs.h" |
554 |
|
default: |
555 |
0 |
WRONG("Undefined vhd function"); |
556 |
|
break; |
557 |
|
} |
558 |
560200 |
} while (ret == VHD_AGAIN); |
559 |
|
|
560 |
184840 |
if (ret < 0) |
561 |
560 |
d->error = ret; |
562 |
|
|
563 |
184840 |
assert(in + *p_inused <= ctx->in); |
564 |
184840 |
*p_inused += ctx->in - (in + *p_inused); |
565 |
184840 |
assert(out + *p_outused <= ctx->out); |
566 |
184840 |
*p_outused += ctx->out - (out + *p_outused); |
567 |
|
|
568 |
184840 |
return (ret); |
569 |
184840 |
} |
570 |
|
|
571 |
|
void |
572 |
8800 |
VHD_Init(struct vhd_decode *d) |
573 |
|
{ |
574 |
|
|
575 |
8800 |
AN(d); |
576 |
8800 |
assert(VHD_S__MAX <= UINT16_MAX); |
577 |
8800 |
assert(HUFDEC_LEN <= UINT16_MAX); |
578 |
8800 |
INIT_OBJ(d, VHD_DECODE_MAGIC); |
579 |
8800 |
d->state = VHD_S_IDLE; |
580 |
8800 |
d->first = 1; |
581 |
8800 |
} |
582 |
|
|
583 |
|
/* Test driver */ |
584 |
|
|
585 |
|
#ifdef DECODE_TEST_DRIVER |
586 |
|
|
587 |
|
#include <ctype.h> |
588 |
|
#include <stdarg.h> |
589 |
|
|
590 |
|
static int verbose = 0; |
591 |
|
|
592 |
|
static size_t |
593 |
4160 |
hexbuf(uint8_t *buf, size_t buflen, const char *h) |
594 |
|
{ |
595 |
|
size_t l; |
596 |
|
uint8_t u; |
597 |
|
|
598 |
4160 |
AN(h); |
599 |
4160 |
AN(buf); |
600 |
|
|
601 |
4160 |
l = 0; |
602 |
223840 |
for (; *h != '\0'; h++) { |
603 |
219680 |
if (l == buflen * 2) |
604 |
0 |
WRONG("Too small buffer"); |
605 |
219680 |
if (isspace(*h)) |
606 |
39840 |
continue; |
607 |
179840 |
if (*h >= '0' && *h <= '9') |
608 |
135200 |
u = *h - '0'; |
609 |
44640 |
else if (*h >= 'a' && *h <= 'f') |
610 |
44640 |
u = 0xa + *h - 'a'; |
611 |
0 |
else if (*h >= 'A' && *h <= 'F') |
612 |
0 |
u = 0xa + *h - 'A'; |
613 |
|
else |
614 |
0 |
WRONG("Bad input character"); |
615 |
179840 |
assert(u <= 0xf); |
616 |
179840 |
if (l % 2 == 0) { |
617 |
89920 |
u <<= 4; |
618 |
89920 |
buf[l / 2] = u; |
619 |
89920 |
} else { |
620 |
89920 |
buf[l / 2] |= u; |
621 |
|
} |
622 |
179840 |
l++; |
623 |
179840 |
} |
624 |
4160 |
AZ(l % 2); |
625 |
4160 |
return (l / 2); |
626 |
|
} |
627 |
|
|
628 |
|
static int |
629 |
2880 |
match(const char *b, size_t l, ...) |
630 |
|
{ |
631 |
|
va_list ap; |
632 |
|
const char *e; |
633 |
|
const char *m; |
634 |
2880 |
int r = 0; |
635 |
|
|
636 |
2880 |
va_start(ap, l); |
637 |
2880 |
e = b + l; |
638 |
22720 |
while (1) { |
639 |
22720 |
m = va_arg(ap, const char *); |
640 |
22720 |
if (m == NULL) |
641 |
2880 |
break; |
642 |
19840 |
l = strlen(m); |
643 |
19840 |
if (e - b <= l || b[l] != '\0' || strncmp(b, m, l)) { |
644 |
0 |
printf("%.*s != %s\n", (int)(e - b), b, m); |
645 |
0 |
r = -1; |
646 |
0 |
break; |
647 |
19840 |
} else if (verbose) { |
648 |
0 |
printf("%s == %s\n", b, m); |
649 |
0 |
} |
650 |
19840 |
b += l + 1; |
651 |
|
} |
652 |
2880 |
va_end(ap); |
653 |
2880 |
return (r); |
654 |
|
} |
655 |
|
|
656 |
|
#define M_1IN (1U << 0) |
657 |
|
#define M_1OUT (1U << 1) |
658 |
|
|
659 |
|
static enum vhd_ret_e |
660 |
4160 |
decode(struct vhd_decode *d, struct vht_table *tbl, uint8_t *in, size_t in_l, |
661 |
|
char *out, size_t out_l, unsigned m) |
662 |
|
{ |
663 |
|
size_t in_u, out_u; |
664 |
|
enum vhd_ret_e r; |
665 |
|
|
666 |
4160 |
CHECK_OBJ_NOTNULL(d, VHD_DECODE_MAGIC); |
667 |
4160 |
AN(in); |
668 |
4160 |
AN(out); |
669 |
|
|
670 |
4160 |
in_u = 0; |
671 |
4160 |
out_u = 0; |
672 |
|
|
673 |
136040 |
while (1) { |
674 |
272080 |
r = VHD_Decode(d, tbl, in, |
675 |
136040 |
(m & M_1IN ? (in_l > in_u ? in_u + 1 : in_u) : in_l), |
676 |
|
&in_u, |
677 |
136040 |
out, |
678 |
136040 |
(m & M_1OUT ? (out_l > out_u ? out_u + 1 : out_u) : out_l), |
679 |
|
&out_u); |
680 |
136040 |
assert(in_u <= in_l); |
681 |
136040 |
assert(out_u <= out_l); |
682 |
136040 |
if (r < VHD_OK) |
683 |
480 |
return (r); |
684 |
|
|
685 |
135560 |
switch (r) { |
686 |
|
case VHD_OK: |
687 |
3360 |
return (r); |
688 |
|
|
689 |
|
case VHD_MORE: |
690 |
32560 |
if (in_u == in_l) |
691 |
320 |
return (r); |
692 |
32240 |
break; |
693 |
|
|
694 |
|
case VHD_BUF: |
695 |
79320 |
if (out_u == out_l) |
696 |
0 |
return (r); |
697 |
79320 |
break; |
698 |
|
|
699 |
|
case VHD_NAME: |
700 |
|
case VHD_NAME_SEC: |
701 |
10400 |
assert(out_l - out_u > 0); |
702 |
10400 |
out[out_u++] = '\0'; |
703 |
10400 |
if (verbose) |
704 |
0 |
printf("Name%s: '%s'\n", |
705 |
0 |
(r == VHD_NAME_SEC ? " (sec)" : ""), |
706 |
0 |
out); |
707 |
10400 |
out += out_u; |
708 |
10400 |
out_l -= out_u; |
709 |
10400 |
out_u = 0; |
710 |
10400 |
break; |
711 |
|
|
712 |
|
case VHD_VALUE: |
713 |
|
case VHD_VALUE_SEC: |
714 |
9920 |
assert(out_l - out_u > 0); |
715 |
9920 |
out[out_u++] = '\0'; |
716 |
9920 |
if (verbose) |
717 |
0 |
printf("Value%s: '%s'\n", |
718 |
0 |
(r == VHD_VALUE_SEC ? " (sec)" : ""), |
719 |
0 |
out); |
720 |
9920 |
out += out_u; |
721 |
9920 |
out_l -= out_u; |
722 |
9920 |
out_u = 0; |
723 |
9920 |
break; |
724 |
|
|
725 |
|
default: |
726 |
0 |
WRONG("Wrong return code"); |
727 |
0 |
break; |
728 |
|
} |
729 |
|
} |
730 |
|
|
731 |
|
NEEDLESS(return (VHD_OK)); |
732 |
4160 |
} |
733 |
|
|
734 |
|
#define CHECK_RET(r, e) \ |
735 |
|
do { \ |
736 |
|
if (verbose || r != e) { \ |
737 |
|
printf("%s %s %s\n", \ |
738 |
|
VHD_Error(r), \ |
739 |
|
(r == e ? "==" : "!="), \ |
740 |
|
VHD_Error(e)); \ |
741 |
|
} \ |
742 |
|
assert(r == e); \ |
743 |
|
} while (0) |
744 |
|
|
745 |
|
#define CHECK_INT(d, u) \ |
746 |
|
do { \ |
747 |
|
CHECK_OBJ_NOTNULL(d->integer, VHD_INT_MAGIC); \ |
748 |
|
if (verbose || d->integer->v != u) { \ |
749 |
|
printf("%u %s %u\n", d->integer->v, \ |
750 |
|
(d->integer->v == u ? "==" : "!="), \ |
751 |
|
u); \ |
752 |
|
} \ |
753 |
|
assert(d->integer->v == u); \ |
754 |
|
} while (0) |
755 |
|
|
756 |
|
static void |
757 |
160 |
test_integer(unsigned mode) |
758 |
|
{ |
759 |
|
struct vhd_decode d[1]; |
760 |
|
uint8_t in[128]; |
761 |
|
size_t in_l; |
762 |
|
char out[128]; |
763 |
|
enum vhd_ret_e r; |
764 |
|
|
765 |
|
/* Test single byte decoding */ |
766 |
160 |
VHD_Init(d); |
767 |
160 |
vhd_set_state(d, VHD_S_TEST_INT5); |
768 |
160 |
in_l = hexbuf(in, sizeof in, "1e"); |
769 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
770 |
160 |
CHECK_RET(r, VHD_OK); |
771 |
160 |
CHECK_INT(d, 30); |
772 |
|
|
773 |
|
/* Test multibyte decoding */ |
774 |
160 |
VHD_Init(d); |
775 |
160 |
vhd_set_state(d, VHD_S_TEST_INT5); |
776 |
160 |
in_l = hexbuf(in, sizeof in, "ff 9a 0a"); |
777 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
778 |
160 |
CHECK_RET(r, VHD_OK); |
779 |
160 |
CHECK_INT(d, 1337); |
780 |
|
|
781 |
|
/* Test max size we allow */ |
782 |
160 |
VHD_Init(d); |
783 |
160 |
vhd_set_state(d, VHD_S_TEST_INT5); |
784 |
160 |
in_l = hexbuf(in, sizeof in, "1f ff ff ff ff 07"); |
785 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
786 |
160 |
CHECK_RET(r, VHD_OK); |
787 |
160 |
CHECK_INT(d, 0x8000001E); |
788 |
|
|
789 |
|
/* Test overflow */ |
790 |
160 |
VHD_Init(d); |
791 |
160 |
vhd_set_state(d, VHD_S_TEST_INT5); |
792 |
160 |
in_l = hexbuf(in, sizeof in, "1f ff ff ff ff 08"); |
793 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
794 |
160 |
CHECK_RET(r, VHD_ERR_INT); |
795 |
160 |
} |
796 |
|
|
797 |
|
static void |
798 |
160 |
test_raw(unsigned mode) |
799 |
|
{ |
800 |
|
struct vhd_decode d[1]; |
801 |
|
uint8_t in[128]; |
802 |
|
size_t in_l; |
803 |
|
char out[128]; |
804 |
|
enum vhd_ret_e r; |
805 |
|
|
806 |
|
/* Test raw encoding */ |
807 |
160 |
VHD_Init(d); |
808 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
809 |
160 |
in_l = hexbuf(in, sizeof in, |
810 |
|
"0a63 7573 746f 6d2d 6b65 790d 6375 7374 6f6d 2d68 6561 6465 72"); |
811 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
812 |
160 |
CHECK_RET(r, VHD_OK); |
813 |
160 |
AZ(match(out, sizeof out, "custom-key", "custom-header", NULL)); |
814 |
|
|
815 |
|
/* Test too short input */ |
816 |
160 |
VHD_Init(d); |
817 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
818 |
160 |
in_l = hexbuf(in, sizeof in, |
819 |
|
"02"); |
820 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
821 |
160 |
CHECK_RET(r, VHD_MORE); |
822 |
160 |
} |
823 |
|
|
824 |
|
static void |
825 |
160 |
test_huffman(unsigned mode) |
826 |
|
{ |
827 |
|
struct vhd_decode d[1]; |
828 |
|
uint8_t in[256]; |
829 |
|
size_t in_l; |
830 |
|
char out[256]; |
831 |
|
enum vhd_ret_e r; |
832 |
|
|
833 |
|
/* Decode a huffman encoded value */ |
834 |
160 |
VHD_Init(d); |
835 |
160 |
in_l = hexbuf(in, sizeof in, |
836 |
|
"0141 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ff"); |
837 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
838 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
839 |
160 |
CHECK_RET(r, VHD_OK); |
840 |
160 |
AZ(match(out, sizeof out, "A", "www.example.com", NULL)); |
841 |
|
|
842 |
|
/* Decode an incomplete input buffer */ |
843 |
160 |
VHD_Init(d); |
844 |
160 |
in_l = hexbuf(in, sizeof in, |
845 |
|
"0141 81"); |
846 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
847 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
848 |
160 |
CHECK_RET(r, VHD_MORE); |
849 |
|
|
850 |
|
/* Decode an incomplete huffman code */ |
851 |
160 |
VHD_Init(d); |
852 |
160 |
in_l = hexbuf(in, sizeof in, |
853 |
|
"0141 81 fe"); |
854 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
855 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
856 |
160 |
CHECK_RET(r, VHD_ERR_HUF); |
857 |
|
|
858 |
|
/* Decode an invalid huffman code */ |
859 |
160 |
VHD_Init(d); |
860 |
160 |
in_l = hexbuf(in, sizeof in, |
861 |
|
"0141 84 ff ff ff ff"); |
862 |
160 |
vhd_set_state(d, VHD_S_TEST_LITERAL); |
863 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
864 |
160 |
CHECK_RET(r, VHD_ERR_HUF); |
865 |
160 |
} |
866 |
|
|
867 |
|
static void |
868 |
160 |
test_c2(unsigned mode) |
869 |
|
{ |
870 |
|
struct vhd_decode d[1]; |
871 |
|
uint8_t in[256]; |
872 |
|
size_t in_l; |
873 |
|
char out[256]; |
874 |
|
enum vhd_ret_e r; |
875 |
|
|
876 |
|
/* See RFC 7541 Appendix C.2 */ |
877 |
|
|
878 |
160 |
VHD_Init(d); |
879 |
|
|
880 |
|
/* C.2.1 */ |
881 |
160 |
in_l = hexbuf(in, sizeof in, |
882 |
|
"400a 6375 7374 6f6d 2d6b 6579 0d63 7573" |
883 |
|
"746f 6d2d 6865 6164 6572"); |
884 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
885 |
160 |
CHECK_RET(r, VHD_OK); |
886 |
160 |
AZ(match(out, sizeof out, |
887 |
|
"custom-key", "custom-header", |
888 |
|
NULL)); |
889 |
|
|
890 |
|
/* C.2.2 */ |
891 |
160 |
in_l = hexbuf(in, sizeof in, |
892 |
|
"040c 2f73 616d 706c 652f 7061 7468"); |
893 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
894 |
160 |
CHECK_RET(r, VHD_OK); |
895 |
160 |
AZ(match(out, sizeof out, |
896 |
|
":path", "/sample/path", |
897 |
|
NULL)); |
898 |
|
|
899 |
|
/* C.2.3 */ |
900 |
160 |
in_l = hexbuf(in, sizeof in, |
901 |
|
"1008 7061 7373 776f 7264 0673 6563 7265" |
902 |
|
"74"); |
903 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
904 |
160 |
CHECK_RET(r, VHD_OK); |
905 |
160 |
AZ(match(out, sizeof out, |
906 |
|
"password", "secret", |
907 |
|
NULL)); |
908 |
|
|
909 |
|
/* C.2.4 */ |
910 |
160 |
in_l = hexbuf(in, sizeof in, |
911 |
|
"82"); |
912 |
160 |
r = decode(d, NULL, in, in_l, out, sizeof out, mode); |
913 |
160 |
CHECK_RET(r, VHD_OK); |
914 |
160 |
AZ(match(out, sizeof out, |
915 |
|
":method", "GET", |
916 |
|
NULL)); |
917 |
160 |
} |
918 |
|
|
919 |
|
static void |
920 |
160 |
test_c3(unsigned mode) |
921 |
|
{ |
922 |
|
struct vht_table t[1]; |
923 |
|
struct vhd_decode d[1]; |
924 |
|
uint8_t in[256]; |
925 |
|
size_t in_l; |
926 |
|
char out[256]; |
927 |
|
enum vhd_ret_e r; |
928 |
|
|
929 |
|
/* See RFC 7541 Appendix C.3 */ |
930 |
|
|
931 |
160 |
AZ(VHT_Init(t, 4096)); |
932 |
160 |
VHD_Init(d); |
933 |
|
|
934 |
|
/* C.3.1 */ |
935 |
160 |
in_l = hexbuf(in, sizeof in, |
936 |
|
"8286 8441 0f77 7777 2e65 7861 6d70 6c65" |
937 |
|
"2e63 6f6d"); |
938 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
939 |
160 |
CHECK_RET(r, VHD_OK); |
940 |
160 |
AZ(match(out, sizeof out, |
941 |
|
":method", "GET", |
942 |
|
":scheme", "http", |
943 |
|
":path", "/", |
944 |
|
":authority", "www.example.com", |
945 |
|
NULL)); |
946 |
|
|
947 |
|
/* C.3.2 */ |
948 |
160 |
in_l = hexbuf(in, sizeof in, |
949 |
|
"8286 84be 5808 6e6f 2d63 6163 6865"); |
950 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
951 |
160 |
CHECK_RET(r, VHD_OK); |
952 |
160 |
AZ(match(out, sizeof out, |
953 |
|
":method", "GET", |
954 |
|
":scheme", "http", |
955 |
|
":path", "/", |
956 |
|
":authority", "www.example.com", |
957 |
|
"cache-control", "no-cache", |
958 |
|
NULL)); |
959 |
|
|
960 |
|
/* C.3.3 */ |
961 |
160 |
in_l = hexbuf(in, sizeof in, |
962 |
|
"8287 85bf 400a 6375 7374 6f6d 2d6b 6579" |
963 |
|
"0c63 7573 746f 6d2d 7661 6c75 65"); |
964 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
965 |
160 |
CHECK_RET(r, VHD_OK); |
966 |
160 |
AZ(match(out, sizeof out, |
967 |
|
":method", "GET", |
968 |
|
":scheme", "https", |
969 |
|
":path", "/index.html", |
970 |
|
":authority", "www.example.com", |
971 |
|
"custom-key", "custom-value", |
972 |
|
NULL)); |
973 |
|
|
974 |
160 |
VHT_Fini(t); |
975 |
160 |
} |
976 |
|
|
977 |
|
static void |
978 |
160 |
test_c4(unsigned mode) |
979 |
|
{ |
980 |
|
struct vht_table t[1]; |
981 |
|
struct vhd_decode d[1]; |
982 |
|
uint8_t in[256]; |
983 |
|
size_t in_l; |
984 |
|
char out[256]; |
985 |
|
enum vhd_ret_e r; |
986 |
|
|
987 |
|
/* See RFC 7541 Appendix C.4 */ |
988 |
|
|
989 |
160 |
AZ(VHT_Init(t, 4096)); |
990 |
160 |
VHD_Init(d); |
991 |
|
|
992 |
|
/* C.4.1 */ |
993 |
160 |
in_l = hexbuf(in, sizeof in, |
994 |
|
"8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ff"); |
995 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
996 |
160 |
CHECK_RET(r, VHD_OK); |
997 |
160 |
AZ(match(out, sizeof out, |
998 |
|
":method", "GET", |
999 |
|
":scheme", "http", |
1000 |
|
":path", "/", |
1001 |
|
":authority", "www.example.com", |
1002 |
|
NULL)); |
1003 |
|
|
1004 |
|
/* C.4.2 */ |
1005 |
160 |
in_l = hexbuf(in, sizeof in, |
1006 |
|
"8286 84be 5886 a8eb 1064 9cbf"); |
1007 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1008 |
160 |
CHECK_RET(r, VHD_OK); |
1009 |
160 |
AZ(match(out, sizeof out, |
1010 |
|
":method", "GET", |
1011 |
|
":scheme", "http", |
1012 |
|
":path", "/", |
1013 |
|
":authority", "www.example.com", |
1014 |
|
"cache-control", "no-cache", |
1015 |
|
NULL)); |
1016 |
|
|
1017 |
|
/* C.4.3 */ |
1018 |
160 |
in_l = hexbuf(in, sizeof in, |
1019 |
|
"8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" |
1020 |
|
"a849 e95b b8e8 b4bf"); |
1021 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1022 |
160 |
CHECK_RET(r, VHD_OK); |
1023 |
160 |
AZ(match(out, sizeof out, |
1024 |
|
":method", "GET", |
1025 |
|
":scheme", "https", |
1026 |
|
":path", "/index.html", |
1027 |
|
":authority", "www.example.com", |
1028 |
|
"custom-key", "custom-value", |
1029 |
|
NULL)); |
1030 |
|
|
1031 |
160 |
VHT_Fini(t); |
1032 |
160 |
} |
1033 |
|
|
1034 |
|
static void |
1035 |
160 |
test_c5(unsigned mode) |
1036 |
|
{ |
1037 |
|
struct vht_table t[1]; |
1038 |
|
struct vhd_decode d[1]; |
1039 |
|
uint8_t in[256]; |
1040 |
|
size_t in_l; |
1041 |
|
char out[256]; |
1042 |
|
enum vhd_ret_e r; |
1043 |
|
|
1044 |
|
/* See RFC 7541 Appendix C.5 */ |
1045 |
|
|
1046 |
160 |
AZ(VHT_Init(t, 256)); |
1047 |
160 |
VHD_Init(d); |
1048 |
|
|
1049 |
|
/* C.5.1 */ |
1050 |
160 |
in_l = hexbuf(in, sizeof in, |
1051 |
|
"4803 3330 3258 0770 7269 7661 7465 611d" |
1052 |
|
"4d6f 6e2c 2032 3120 4f63 7420 3230 3133" |
1053 |
|
"2032 303a 3133 3a32 3120 474d 546e 1768" |
1054 |
|
"7474 7073 3a2f 2f77 7777 2e65 7861 6d70" |
1055 |
|
"6c65 2e63 6f6d"); |
1056 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1057 |
160 |
CHECK_RET(r, VHD_OK); |
1058 |
160 |
AZ(match(out, sizeof out, |
1059 |
|
":status", "302", |
1060 |
|
"cache-control", "private", |
1061 |
|
"date", "Mon, 21 Oct 2013 20:13:21 GMT", |
1062 |
|
"location", "https://www.example.com", |
1063 |
|
NULL)); |
1064 |
|
|
1065 |
|
/* C.5.2 */ |
1066 |
160 |
in_l = hexbuf(in, sizeof in, |
1067 |
|
"4803 3330 37c1 c0bf"); |
1068 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1069 |
160 |
CHECK_RET(r, VHD_OK); |
1070 |
160 |
AZ(match(out, sizeof out, |
1071 |
|
":status", "307", |
1072 |
|
"cache-control", "private", |
1073 |
|
"date", "Mon, 21 Oct 2013 20:13:21 GMT", |
1074 |
|
"location", "https://www.example.com", |
1075 |
|
NULL)); |
1076 |
|
|
1077 |
|
/* C.5.3 */ |
1078 |
160 |
in_l = hexbuf(in, sizeof in, |
1079 |
|
"88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" |
1080 |
|
"3230 3133 2032 303a 3133 3a32 3220 474d" |
1081 |
|
"54c0 5a04 677a 6970 7738 666f 6f3d 4153" |
1082 |
|
"444a 4b48 514b 425a 584f 5157 454f 5049" |
1083 |
|
"5541 5851 5745 4f49 553b 206d 6178 2d61" |
1084 |
|
"6765 3d33 3630 303b 2076 6572 7369 6f6e" |
1085 |
|
"3d31"); |
1086 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1087 |
160 |
CHECK_RET(r, VHD_OK); |
1088 |
160 |
AZ(match(out, sizeof out, |
1089 |
|
":status", "200", |
1090 |
|
"cache-control", "private", |
1091 |
|
"date", "Mon, 21 Oct 2013 20:13:22 GMT", |
1092 |
|
"location", "https://www.example.com", |
1093 |
|
"content-encoding", "gzip", |
1094 |
|
"set-cookie", |
1095 |
|
"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
1096 |
|
NULL)); |
1097 |
|
|
1098 |
160 |
VHT_Fini(t); |
1099 |
160 |
} |
1100 |
|
|
1101 |
|
static void |
1102 |
160 |
test_c6(unsigned mode) |
1103 |
|
{ |
1104 |
|
struct vht_table t[1]; |
1105 |
|
struct vhd_decode d[1]; |
1106 |
|
uint8_t in[256]; |
1107 |
|
size_t in_l; |
1108 |
|
char out[256]; |
1109 |
|
enum vhd_ret_e r; |
1110 |
|
|
1111 |
|
/* See RFC 7541 Appendix C.6 */ |
1112 |
|
|
1113 |
160 |
AZ(VHT_Init(t, 256)); |
1114 |
160 |
VHD_Init(d); |
1115 |
|
|
1116 |
|
/* C.6.1 */ |
1117 |
160 |
in_l = hexbuf(in, sizeof in, |
1118 |
|
"4882 6402 5885 aec3 771a 4b61 96d0 7abe" |
1119 |
|
"9410 54d4 44a8 2005 9504 0b81 66e0 82a6" |
1120 |
|
"2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" |
1121 |
|
"e9ae 82ae 43d3"); |
1122 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1123 |
160 |
CHECK_RET(r, VHD_OK); |
1124 |
160 |
AZ(match(out, sizeof out, |
1125 |
|
":status", "302", |
1126 |
|
"cache-control", "private", |
1127 |
|
"date", "Mon, 21 Oct 2013 20:13:21 GMT", |
1128 |
|
"location", "https://www.example.com", |
1129 |
|
NULL)); |
1130 |
|
|
1131 |
|
/* C.6.2 */ |
1132 |
160 |
in_l = hexbuf(in, sizeof in, |
1133 |
|
"4883 640e ffc1 c0bf"); |
1134 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1135 |
160 |
CHECK_RET(r, VHD_OK); |
1136 |
160 |
AZ(match(out, sizeof out, |
1137 |
|
":status", "307", |
1138 |
|
"cache-control", "private", |
1139 |
|
"date", "Mon, 21 Oct 2013 20:13:21 GMT", |
1140 |
|
"location", "https://www.example.com", |
1141 |
|
NULL)); |
1142 |
|
|
1143 |
|
/* C.6.3 */ |
1144 |
160 |
in_l = hexbuf(in, sizeof in, |
1145 |
|
"88c1 6196 d07a be94 1054 d444 a820 0595" |
1146 |
|
"040b 8166 e084 a62d 1bff c05a 839b d9ab" |
1147 |
|
"77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" |
1148 |
|
"3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" |
1149 |
|
"9587 3160 65c0 03ed 4ee5 b106 3d50 07"); |
1150 |
160 |
r = decode(d, t, in, in_l, out, sizeof out, mode); |
1151 |
160 |
CHECK_RET(r, VHD_OK); |
1152 |
160 |
AZ(match(out, sizeof out, |
1153 |
|
":status", "200", |
1154 |
|
"cache-control", "private", |
1155 |
|
"date", "Mon, 21 Oct 2013 20:13:22 GMT", |
1156 |
|
"location", "https://www.example.com", |
1157 |
|
"content-encoding", "gzip", |
1158 |
|
"set-cookie", |
1159 |
|
"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
1160 |
|
NULL)); |
1161 |
|
|
1162 |
160 |
VHT_Fini(t); |
1163 |
160 |
} |
1164 |
|
|
1165 |
|
#define do_test(name) \ |
1166 |
|
do { \ |
1167 |
|
printf("Doing test: %s\n", #name); \ |
1168 |
|
name(0); \ |
1169 |
|
printf("Doing test: %s 1IN\n", #name); \ |
1170 |
|
name(M_1IN); \ |
1171 |
|
printf("Doing test: %s 1OUT\n", #name); \ |
1172 |
|
name(M_1OUT); \ |
1173 |
|
printf("Doing test: %s 1IN|1OUT\n", #name); \ |
1174 |
|
name(M_1IN|M_1OUT); \ |
1175 |
|
printf("Test finished: %s\n\n", #name); \ |
1176 |
|
} while (0) |
1177 |
|
|
1178 |
|
int |
1179 |
40 |
main(int argc, char **argv) |
1180 |
|
{ |
1181 |
40 |
if (argc == 2 && !strcmp(argv[1], "-v")) |
1182 |
0 |
verbose = 1; |
1183 |
40 |
else if (argc != 1) { |
1184 |
0 |
fprintf(stderr, "Usage: %s [-v]\n", argv[0]); |
1185 |
0 |
return (1); |
1186 |
|
} |
1187 |
|
|
1188 |
40 |
if (verbose) { |
1189 |
0 |
printf("sizeof (struct vhd_int)=%zu\n", |
1190 |
|
sizeof (struct vhd_int)); |
1191 |
0 |
printf("sizeof (struct vhd_lookup)=%zu\n", |
1192 |
|
sizeof (struct vhd_lookup)); |
1193 |
0 |
printf("sizeof (struct vhd_raw)=%zu\n", |
1194 |
|
sizeof (struct vhd_raw)); |
1195 |
0 |
printf("sizeof (struct vhd_huffman)=%zu\n", |
1196 |
|
sizeof (struct vhd_huffman)); |
1197 |
0 |
printf("sizeof (struct vhd_decode)=%zu\n", |
1198 |
|
sizeof (struct vhd_decode)); |
1199 |
0 |
} |
1200 |
|
|
1201 |
40 |
do_test(test_integer); |
1202 |
40 |
do_test(test_raw); |
1203 |
40 |
do_test(test_huffman); |
1204 |
|
|
1205 |
40 |
do_test(test_c2); |
1206 |
40 |
do_test(test_c3); |
1207 |
40 |
do_test(test_c4); |
1208 |
40 |
do_test(test_c5); |
1209 |
40 |
do_test(test_c6); |
1210 |
|
|
1211 |
40 |
return (0); |
1212 |
40 |
} |
1213 |
|
|
1214 |
|
#endif /* DECODE_TEST_DRIVER */ |