Debian Codegraph @

vendor/ls-qpack/bin/interop-decode.c

raw
1 /*
2 * QPACK Interop -- decode from intermediate format and output QIF
3 *
4 * https://github.com/quicwg/base-drafts/wiki/QPACK-Offline-Interop
5 */
6
7 #include <assert.h>
8
9 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
10 #include <sys/endian.h>
11 #define bswap_16 bswap16
12 #define bswap_32 bswap32
13 #define bswap_64 bswap64
14 #elif defined(__OpenBSD__)
15 #define bswap_16 swap16
16 #define bswap_32 swap32
17 #define bswap_64 swap64
18 #elif defined(__APPLE__)
19 #include <libkern/OSByteOrder.h>
20 #define bswap_16 OSSwapInt16
21 #define bswap_32 OSSwapInt32
22 #define bswap_64 OSSwapInt64
23 #elif defined(WIN32)
24 #define bswap_16 _byteswap_ushort
25 #define bswap_32 _byteswap_ulong
26 #define bswap_64 _byteswap_uint64
27 #else
28 #include <byteswap.h>
29 #endif
30
31 #include <errno.h>
32 #include <inttypes.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #ifdef WIN32
37 #include <getopt.h>
38 #else
39 #include <unistd.h>
40 #endif
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45
46 #include "lsqpack.h"
47 #include "lsxpack_header.h"
48 #include "xxhash.h"
49 #ifndef DEBUG
50 #include "lsqpack-test.h"
51 #endif
52
53 #ifndef NDEBUG
54 struct static_table_entry
55 {
56 const char *name;
57 const char *val;
58 unsigned name_len;
59 unsigned val_len;
60 };
61
62 /* [draft-ietf-quic-qpack-03] Appendix A */
63 static const struct static_table_entry static_table[] =
64 {
65 {":authority", "", 10, 0,},
66 {":path", "/", 5, 1,},
67 {"age", "0", 3, 1,},
68 {"content-disposition", "", 19, 0,},
69 {"content-length", "0", 14, 1,},
70 {"cookie", "", 6, 0,},
71 {"date", "", 4, 0,},
72 {"etag", "", 4, 0,},
73 {"if-modified-since", "", 17, 0,},
74 {"if-none-match", "", 13, 0,},
75 {"last-modified", "", 13, 0,},
76 {"link", "", 4, 0,},
77 {"location", "", 8, 0,},
78 {"referer", "", 7, 0,},
79 {"set-cookie", "", 10, 0,},
80 {":method", "CONNECT", 7, 7,},
81 {":method", "DELETE", 7, 6,},
82 {":method", "GET", 7, 3,},
83 {":method", "HEAD", 7, 4,},
84 {":method", "OPTIONS", 7, 7,},
85 {":method", "POST", 7, 4,},
86 {":method", "PUT", 7, 3,},
87 {":scheme", "http", 7, 4,},
88 {":scheme", "https", 7, 5,},
89 {":status", "103", 7, 3,},
90 {":status", "200", 7, 3,},
91 {":status", "304", 7, 3,},
92 {":status", "404", 7, 3,},
93 {":status", "503", 7, 3,},
94 {"accept", "*/*", 6, 3,},
95 {"accept", "application/dns-message", 6, 23,},
96 {"accept-encoding", "gzip, deflate, br", 15, 17,},
97 {"accept-ranges", "bytes", 13, 5,},
98 {"access-control-allow-headers", "cache-control", 28, 13,},
99 {"access-control-allow-headers", "content-type", 28, 12,},
100 {"access-control-allow-origin", "*", 27, 1,},
101 {"cache-control", "max-age=0", 13, 9,},
102 {"cache-control", "max-age=2592000", 13, 15,},
103 {"cache-control", "max-age=604800", 13, 14,},
104 {"cache-control", "no-cache", 13, 8,},
105 {"cache-control", "no-store", 13, 8,},
106 {"cache-control", "public, max-age=31536000", 13, 24,},
107 {"content-encoding", "br", 16, 2,},
108 {"content-encoding", "gzip", 16, 4,},
109 {"content-type", "application/dns-message", 12, 23,},
110 {"content-type", "application/javascript", 12, 22,},
111 {"content-type", "application/json", 12, 16,},
112 {"content-type", "application/x-www-form-urlencoded", 12, 33,},
113 {"content-type", "image/gif", 12, 9,},
114 {"content-type", "image/jpeg", 12, 10,},
115 {"content-type", "image/png", 12, 9,},
116 {"content-type", "text/css", 12, 8,},
117 {"content-type", "text/html; charset=utf-8", 12, 24,},
118 {"content-type", "text/plain", 12, 10,},
119 {"content-type", "text/plain;charset=utf-8", 12, 24,},
120 {"range", "bytes=0-", 5, 8,},
121 {"strict-transport-security", "max-age=31536000", 25, 16,},
122 {"strict-transport-security", "max-age=31536000; includesubdomains",
123 25, 35,},
124 {"strict-transport-security",
125 "max-age=31536000; includesubdomains; preload", 25, 44,},
126 {"vary", "accept-encoding", 4, 15,},
127 {"vary", "origin", 4, 6,},
128 {"x-content-type-options", "nosniff", 22, 7,},
129 {"x-xss-protection", "1; mode=block", 16, 13,},
130 {":status", "100", 7, 3,},
131 {":status", "204", 7, 3,},
132 {":status", "206", 7, 3,},
133 {":status", "302", 7, 3,},
134 {":status", "400", 7, 3,},
135 {":status", "403", 7, 3,},
136 {":status", "421", 7, 3,},
137 {":status", "425", 7, 3,},
138 {":status", "500", 7, 3,},
139 {"accept-language", "", 15, 0,},
140 {"access-control-allow-credentials", "FALSE", 32, 5,},
141 {"access-control-allow-credentials", "TRUE", 32, 4,},
142 {"access-control-allow-headers", "*", 28, 1,},
143 {"access-control-allow-methods", "get", 28, 3,},
144 {"access-control-allow-methods", "get, post, options", 28, 18,},
145 {"access-control-allow-methods", "options", 28, 7,},
146 {"access-control-expose-headers", "content-length", 29, 14,},
147 {"access-control-request-headers", "content-type", 30, 12,},
148 {"access-control-request-method", "get", 29, 3,},
149 {"access-control-request-method", "post", 29, 4,},
150 {"alt-svc", "clear", 7, 5,},
151 {"authorization", "", 13, 0,},
152 {"content-security-policy",
153 "script-src 'none'; object-src 'none'; base-uri 'none'", 23, 53,},
154 {"early-data", "1", 10, 1,},
155 {"expect-ct", "", 9, 0,},
156 {"forwarded", "", 9, 0,},
157 {"if-range", "", 8, 0,},
158 {"origin", "", 6, 0,},
159 {"purpose", "prefetch", 7, 8,},
160 {"server", "", 6, 0,},
161 {"timing-allow-origin", "*", 19, 1,},
162 {"upgrade-insecure-requests", "1", 25, 1,},
163 {"user-agent", "", 10, 0,},
164 {"x-forwarded-for", "", 15, 0,},
165 {"x-frame-options", "deny", 15, 4,},
166 {"x-frame-options", "sameorigin", 15, 10,},
167 };
168 #endif
169
170 static size_t s_max_read_size = SIZE_MAX;
171
172 static int s_verbose;
173 static enum lsqpack_dec_opts s_dec_opts = LSQPACK_DEC_OPT_HASH_NAME
174 | LSQPACK_DEC_OPT_HASH_NAMEVAL;
175
176 static int s_check_unset_qpack_idx = 1;
177
178 static FILE *s_out;
179
180 #define MIN(a, b) ((a) < (b) ? (a) : (b))
181
182 static void
183 usage (const char *name)
184 {
185 fprintf(stderr,
186 "Usage: %s [options] [-i input] [-o output]\n"
187 "\n"
188 "Options:\n"
189 " -i FILE Input file. If not specified or set to `-', the input is\n"
190 " read from stdin.\n"
191 " -o FILE Output file. If not spepcified or set to `-', the output\n"
192 " is written to stdout.\n"
193 " -r FILE Recipe file. Without a recipe, buffers are processed in\n"
194 " order.\n"
195 " -s NUMBER Maximum number of risked streams. Defaults to %u.\n"
196 " -t NUMBER Dynamic table size. Defaults to %u.\n"
197 " -m NUMBER Maximum read size. Defaults to %zu.\n"
198 " -H [0|1] Use HTTP/1.x mode and test each header (defaults to `off').\n"
199 " -v Verbose: print headers and table state to stderr.\n"
200 " -S Don't swap encoder stream and header blocks.\n"
201 " -Q Don't check static table when LSXPACK_QPACK_IDX is not set.\n"
202 "\n"
203 " -h Print this help screen and exit\n"
204 , name, LSQPACK_DEF_MAX_RISKED_STREAMS, LSQPACK_DEF_DYN_TABLE_SIZE, SIZE_MAX);
205 }
206
207
208 struct buf
209 {
210 TAILQ_ENTRY(buf) next_buf;
211 struct lsqpack_dec *dec;
212 uint64_t stream_id; /* Zero means encoder stream */
213 size_t size;
214 size_t off;
215 size_t file_off;
216
217 /* A single header name/value pair is stored in xhdr_buf. When the
218 * header is done, the whole buffer can be used again for the next
219 * header.
220 */
221 struct lsxpack_header xhdr;
222 unsigned xhdr_off; /* Used in xhdr_buf */
223 unsigned xhdr_nalloc; /* Number of bytes allocated */ /* TODO: use it */
224
225 /* Output is written to out_buf out header at a time and the printed all
226 * at once. This logic is not very important and we use a reasonably
227 * large fixed-size buffer.
228 */
229 unsigned out_off;
230 char out_buf[0x1000];
231
232 unsigned char buf[0];
233 };
234
235
236 TAILQ_HEAD(, buf) bufs = TAILQ_HEAD_INITIALIZER(bufs);
237
238 static void
239 hblock_unblocked (void *buf_p)
240 {
241 struct buf *buf = buf_p;
242 TAILQ_INSERT_HEAD(&bufs, buf, next_buf);
243 }
244
245
246 static struct lsxpack_header *
247 prepare_decode (void *hblock_ctx, struct lsxpack_header *xhdr, size_t space)
248 {
249 struct buf *const buf = hblock_ctx;
250 char *new;
251
252 if (space > LSXPACK_MAX_STRLEN)
253 return NULL;
254
255 if (xhdr)
256 {
257 assert(xhdr == &buf->xhdr);
258 assert(space > xhdr->val_len);
259 new = realloc(xhdr->buf, space);
260 if (!new)
261 return NULL;
262 xhdr->buf = new;
263 xhdr->val_len = space;
264 return xhdr;
265 }
266 else
267 {
268 xhdr = &buf->xhdr;
269 new = malloc(space);
270 if (!new)
271 return NULL;
272 lsxpack_header_prepare_decode(xhdr, new, 0, space);
273 return xhdr;
274 }
275 }
276
277
278 static int
279 process_header (void *hblock_ctx, struct lsxpack_header *xhdr)
280 {
281 struct buf *const buf = hblock_ctx;
282 const char *p;
283 const uint32_t seed = 39378473;
284 uint32_t hash, name_hash;
285 int nw;
286
287 if (s_dec_opts & LSQPACK_DEC_OPT_HTTP1X)
288 {
289 p = lsxpack_header_get_name(xhdr) + xhdr->name_len;
290 assert(0 == memcmp(p, ": ", 2));
291 p += 2 + xhdr->val_len;
292 assert(0 == memcmp(p, "\r\n", 2));
293 assert(xhdr->dec_overhead == 4);
294 }
295 else
296 assert(xhdr->dec_overhead == 0);
297
298 if (s_dec_opts & LSQPACK_DEC_OPT_HASH_NAME)
299 {
300 assert(xhdr->flags & LSXPACK_NAME_HASH);
301 hash = XXH32(lsxpack_header_get_name(xhdr), xhdr->name_len, seed);
302 assert(hash == xhdr->name_hash);
303 }
304
305 if (s_dec_opts & LSQPACK_DEC_OPT_HASH_NAME)
306 assert(xhdr->flags & LSXPACK_NAME_HASH);
307
308 if (xhdr->flags & LSXPACK_NAME_HASH)
309 {
310 name_hash = XXH32(lsxpack_header_get_name(xhdr), xhdr->name_len, seed);
311 assert(hash == xhdr->name_hash);
312 }
313 #ifndef NDEBUG
314 else if (!(xhdr->flags & LSXPACK_QPACK_IDX))
315 /* Calculate for upcoming "not in static table check" */
316 name_hash = XXH32(lsxpack_header_get_name(xhdr), xhdr->name_len, seed);
317 #endif
318
319 if (s_dec_opts & LSQPACK_DEC_OPT_HASH_NAMEVAL)
320 {
321 /* This is not required by the API, but internally, if the library
322 * calculates nameval hash, it should also set the name hash.
323 */
324 assert(xhdr->flags & LSXPACK_NAME_HASH);
325 assert(xhdr->flags & LSXPACK_NAMEVAL_HASH);
326 }
327
328 if (xhdr->flags & LSXPACK_NAMEVAL_HASH)
329 {
330 hash = XXH32(lsxpack_header_get_name(xhdr), xhdr->name_len, seed);
331 hash = XXH32(lsxpack_header_get_value(xhdr), xhdr->val_len, hash);
332 assert(hash == xhdr->nameval_hash);
333 }
334
335 #ifndef NDEBUG
336 if (xhdr->flags & LSXPACK_QPACK_IDX)
337 {
338 assert(xhdr->qpack_index <
339 sizeof(static_table) / sizeof(static_table[0]));
340 assert(static_table[xhdr->qpack_index].name_len == xhdr->name_len);
341 assert(0 == memcmp(lsxpack_header_get_name(xhdr),
342 static_table[xhdr->qpack_index].name, xhdr->name_len));
343 }
344 else if (s_check_unset_qpack_idx)
345 {
346 /* The decoder does best effort: if the encoder did not use the
347 * static table, QPACK index is not set. However, since we are
348 * testing our decoder, we assume that the encoder always uses
349 * the static table when it can.
350 */
351 int idx = lsqpack_find_in_static_headers(name_hash,
352 lsxpack_header_get_name(xhdr), xhdr->name_len);
353 assert(idx < 0);
354 }
355 #endif
356
357 nw = snprintf(buf->out_buf + buf->out_off,
358 sizeof(buf->out_buf) - buf->out_off,
359 "%.*s\t%.*s\n",
360 (int) xhdr->name_len, lsxpack_header_get_name(xhdr),
361 (int) xhdr->val_len, lsxpack_header_get_value(xhdr));
362 free(buf->xhdr.buf);
363 memset(&buf->xhdr, 0, sizeof(buf->xhdr));
364 if (nw > 0 && (size_t) nw <= sizeof(buf->out_buf) - buf->out_off)
365 {
366 buf->out_off += (unsigned) nw;
367 return 0;
368 }
369 else
370 {
371 fprintf(stderr, "header list too long\n");
372 return -1;
373 }
374 }
375
376
377 static const struct lsqpack_dec_hset_if hset_if = {
378 .dhi_unblocked = hblock_unblocked,
379 .dhi_prepare_decode = prepare_decode,
380 .dhi_process_header = process_header,
381 };
382
383
384 static void
385 header_block_done (const struct buf *buf)
386 {
387 fprintf(s_out, "# stream %"PRIu64"\n", buf->stream_id);
388 fprintf(s_out, "# (stream ID above is used for sorting)\n");
389 fprintf(s_out, "%.*s\n", (int) buf->out_off, buf->out_buf);
390 }
391
392
393 int
394 main (int argc, char **argv)
395 {
396 FILE *in = stdin;
397 FILE *recipe = NULL;
398 int opt;
399 unsigned dyn_table_size = LSQPACK_DEF_DYN_TABLE_SIZE,
400 max_risked_streams = LSQPACK_DEF_MAX_RISKED_STREAMS;
401 struct lsqpack_dec decoder;
402 const struct lsqpack_dec_err *err;
403 const unsigned char *p;
404 ssize_t nr;
405 int r;
406 uint64_t stream_id;
407 uint32_t size;
408 size_t off; /* For debugging */
409 size_t file_off;
410 struct buf *buf;
411 unsigned lineno;
412 char *line, *end;
413 enum lsqpack_read_header_status rhs;
414 int do_swap = 1;
415 char command[0x100];
416 char line_buf[0x100];
417
418 while (-1 != (opt = getopt(argc, argv, "i:o:r:s:t:m:hvH:SQ")))
419 {
420 switch (opt)
421 {
422 case 'i':
423 if (0 != strcmp(optarg, "-"))
424 {
425 in = fopen(optarg, "rb");
426 if (!in)
427 {
428 fprintf(stderr, "cannot open `%s' for reading: %s\n",
429 optarg, strerror(errno));
430 exit(EXIT_FAILURE);
431 }
432 }
433 break;
434 case 'o':
435 if (0 != strcmp(optarg, "-"))
436 {
437 s_out = fopen(optarg, "w");
438 if (!s_out)
439 {
440 fprintf(stderr, "cannot open `%s' for writing: %s\n",
441 optarg, strerror(errno));
442 exit(EXIT_FAILURE);
443 }
444 }
445 break;
446 case 'r':
447 if (0 == strcmp(optarg, "-"))
448 recipe = stdin;
449 else
450 {
451 recipe = fopen(optarg, "r");
452 if (!recipe)
453 {
454 fprintf(stderr, "cannot open `%s' for reading: %s\n",
455 optarg, strerror(errno));
456 exit(EXIT_FAILURE);
457 }
458 }
459 break;
460 case 's':
461 max_risked_streams = atoi(optarg);
462 break;
463 case 't':
464 dyn_table_size = atoi(optarg);
465 break;
466 case 'm':
467 s_max_read_size = atoi(optarg);
468 break;
469 case 'h':
470 usage(argv[0]);
471 exit(EXIT_SUCCESS);
472 case 'v':
473 ++s_verbose;
474 break;
475 case 'S':
476 do_swap = 0;
477 break;
478 case 'H':
479 if (atoi(optarg))
480 s_dec_opts |= LSQPACK_DEC_OPT_HTTP1X;
481 else
482 s_dec_opts &= ~LSQPACK_DEC_OPT_HTTP1X;
483 break;
484 case 'Q':
485 s_check_unset_qpack_idx = 0;
486 break;
487 default:
488 exit(EXIT_FAILURE);
489 }
490 }
491
492 if (!s_out)
493 s_out = stdout;
494
495 lsqpack_dec_init(&decoder, s_verbose ? stderr : NULL, dyn_table_size,
496 max_risked_streams, &hset_if, s_dec_opts);
497
498 off = 0;
499 while (1)
500 {
501 file_off = off;
502 nr = fread(&stream_id, 1, sizeof(stream_id), in);
503 if (nr == 0)
504 break;
505 if (nr != sizeof(stream_id))
506 {
507 fprintf(stderr, "could not read %zu bytes (stream id) at "
508 "offset %zu: %s\n", sizeof(stream_id), off, strerror(errno));
509 goto read_err;
510 }
511 off += nr;
512 nr = fread(&size, 1, sizeof(size), in);
513 if (nr != sizeof(size))
514 {
515 fprintf(stderr, "could not read %zu bytes (size) at "
516 "offset %zu: %s\n", sizeof(size), off, strerror(errno));
517 goto read_err;
518 }
519 off += nr;
520 #if __BYTE_ORDER == __LITTLE_ENDIAN
521 stream_id = bswap_64(stream_id);
522 size = bswap_32(size);
523 #endif
524 if (stream_id == 0 && size == 0)
525 continue;
526 buf = malloc(sizeof(*buf) + size);
527 if (!buf)
528 {
529 perror("malloc");
530 exit(EXIT_FAILURE);
531 }
532 memset(buf, 0, sizeof(*buf));
533 nr = fread(buf->buf, 1, size, in);
534 if (nr != (ssize_t) size)
535 {
536 fprintf(stderr, "could not read %"PRIu32" bytes (buffer) at "
537 "offset %zu: %s\n", size, off, strerror(errno));
538 goto read_err;
539 }
540 off += nr;
541 buf->dec = &decoder;
542 buf->stream_id = stream_id;
543 buf->size = size;
544 buf->file_off = file_off;
545 if (buf->size == 0)
546 exit(EXIT_FAILURE);
547 TAILQ_INSERT_TAIL(&bufs, buf, next_buf);
548 }
549 (void) fclose(in);
550 in = NULL;
551
552 if (recipe)
553 {
554 lineno = 0;
555 while (line = fgets(line_buf, sizeof(line_buf), recipe), line != NULL)
556 {
557 ++lineno;
558 end = strchr(line, '\n');
559 if (!end)
560 {
561 fprintf(stderr, "no newline on line %u\n", lineno);
562 exit(EXIT_FAILURE);
563 }
564 *end = '\0';
565
566 if (*line == '#')
567 continue;
568
569 if (3 == sscanf(line, " %[s] %"PRIu64" %"PRIu32" ", command, &stream_id, &size))
570 {
571 TAILQ_FOREACH(buf, &bufs, next_buf)
572 if (stream_id == buf->stream_id)
573 break;
574 if (!buf)
575 {
576 fprintf(stderr, "stream %"PRIu64" not found (recipe line %u)\n",
577 stream_id, lineno);
578 exit(EXIT_FAILURE);
579 }
580 p = buf->buf;
581 rhs = lsqpack_dec_header_in(&decoder, buf, stream_id,
582 buf->size, &p,
583 buf->size /* FIXME: this should be `size' */,
584 NULL, NULL);
585 switch (rhs)
586 {
587 case LQRHS_DONE:
588 assert(p == buf->buf + buf->size);
589 header_block_done(buf);
590 if (s_verbose)
591 fprintf(stderr, "compression ratio: %.3f\n",
592 lsqpack_dec_ratio(&decoder));
593 TAILQ_REMOVE(&bufs, buf, next_buf);
594 free(buf);
595 break;
596 case LQRHS_BLOCKED:
597 buf->off += (unsigned) (p - buf->buf);
598 TAILQ_REMOVE(&bufs, buf, next_buf);
599 break;
600 case LQRHS_NEED:
601 buf->off += (unsigned) (p - buf->buf);
602 break;
603 default:
604 assert(rhs == LQRHS_ERROR);
605 fprintf(stderr, "recipe line %u: stream %"PRIu64": "
606 "header_in error\n", lineno, stream_id);
607 exit(EXIT_FAILURE);
608 }
609 }
610 else if (2 == sscanf(line, " %[z] %u ", command, &size))
611 {
612 s_max_read_size = size;
613 }
614 else
615 {
616 perror("sscanf");
617 exit(EXIT_FAILURE);
618 }
619 }
620 fclose(recipe);
621 }
622 else if (do_swap && max_risked_streams && dyn_table_size
623 && TAILQ_FIRST(&bufs) && TAILQ_FIRST(&bufs)->stream_id)
624 {
625 /* Swap header blocks and encoder stream bufs to exercise blocked
626 * header blocks logic.
627 */
628 struct buf *saved_hblock = NULL, *next;
629 for (buf = TAILQ_FIRST(&bufs); buf; buf = next)
630 {
631 next = TAILQ_NEXT(buf, next_buf);
632 if (buf->stream_id)
633 continue;
634 if (saved_hblock)
635 TAILQ_INSERT_BEFORE(buf, saved_hblock, next_buf);
636 saved_hblock = buf;
637 TAILQ_REMOVE(&bufs, buf, next_buf);
638 }
639 TAILQ_INSERT_TAIL(&bufs, saved_hblock, next_buf);
640 }
641
642 while (buf = TAILQ_FIRST(&bufs), buf != NULL)
643 {
644 TAILQ_REMOVE(&bufs, buf, next_buf);
645 if (buf->stream_id == 0)
646 {
647 r = lsqpack_dec_enc_in(&decoder, buf->buf, buf->size - buf->off);
648 if (r != 0)
649 {
650 err = lsqpack_dec_get_err_info(buf->dec);
651 fprintf(stderr, "encoder_in error; off %"PRIu64", line %d\n",
652 err->off, err->line);
653 exit(EXIT_FAILURE);
654 }
655 if (s_verbose)
656 lsqpack_dec_print_table(&decoder, stderr);
657 free(buf);
658 }
659 else
660 {
661 dec_header:
662 p = buf->buf + buf->off;
663 if (buf->off == 0)
664 rhs = lsqpack_dec_header_in(&decoder, buf, buf->stream_id,
665 buf->size, &p, MIN(s_max_read_size, buf->size),
666 NULL, NULL);
667 else
668 rhs = lsqpack_dec_header_read(buf->dec, buf, &p,
669 MIN(s_max_read_size, (buf->size - buf->off)),
670 NULL, NULL);
671 switch (rhs)
672 {
673 case LQRHS_DONE:
674 assert(p == buf->buf + buf->size);
675 header_block_done(buf);
676 if (s_verbose)
677 fprintf(stderr, "compression ratio: %.3f\n",
678 lsqpack_dec_ratio(&decoder));
679 free(buf);
680 break;
681 case LQRHS_BLOCKED:
682 buf->off = (unsigned) (p - buf->buf);
683 break;
684 case LQRHS_NEED:
685 buf->off = (unsigned) (p - buf->buf);
686 goto dec_header;
687 default:
688 assert(rhs == LQRHS_ERROR);
689 fprintf(stderr, "stream %"PRIu64": header block error "
690 "starting at off %zu\n", buf->stream_id, buf->off);
691 err = lsqpack_dec_get_err_info(&decoder);
692 fprintf(stderr, "encoder_in error; off %"PRIu64", line %d\n",
693 err->off, err->line);
694 exit(EXIT_FAILURE);
695 }
696 }
697 }
698
699 if (!TAILQ_EMPTY(&bufs))
700 {
701 fprintf(stderr, "some streams reamain\n");
702 exit(EXIT_FAILURE);
703 }
704 /* TODO: check if decoder has any stream references. That would be
705 * an error.
706 */
707
708 if (s_verbose)
709 lsqpack_dec_print_table(&decoder, stderr);
710
711 lsqpack_dec_cleanup(&decoder);
712
713 assert(TAILQ_EMPTY(&bufs));
714
715 if (s_out)
716 (void) fclose(s_out);
717
718 exit(EXIT_SUCCESS);
719
720 read_err:
721 if (nr < 0)
722 perror("read");
723 else if (nr == 0)
724 fprintf(stderr, "unexpected EOF\n");
725 else
726 fprintf(stderr, "not enough bytes read (%zu)\n", (size_t) nr);
727 exit(EXIT_FAILURE);
728 }