Debian Codegraph @

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

raw
1 /*
2 * QPACK Interop -- encode to intermediate format
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 <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef WIN32
36 #include <getopt.h>
37 #else
38 #include <unistd.h>
39 #endif
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <inttypes.h>
44
45 #include "lsqpack.h"
46 #include "lsxpack_header.h"
47
48 static int s_verbose;
49
50 unsigned char *
51 lsqpack_enc_int (unsigned char *dst, unsigned char *const end, uint64_t value,
52 unsigned prefix_bits);
53
54 static void
55 usage (const char *name)
56 {
57 fprintf(stderr,
58 "Usage: %s [options] [-i input] [-o output]\n"
59 "\n"
60 "Options:\n"
61 " -i FILE Input file. If not specified or set to `-', the input is\n"
62 " read from stdin.\n"
63 " -o FILE Output file. If not spepcified or set to `-', the output\n"
64 " is written to stdout.\n"
65 " -s NUMBER Maximum number of risked streams. Defaults to %u.\n"
66 " -t NUMBER Dynamic table size. Defaults to %u.\n"
67 " -a MODE Header acknowledgement mode. 0 means headers are never\n"
68 " acknowledged, non-zero means header blocks are acknowledged\n"
69 " immediately. Default value is 0.\n"
70 " -n Process annotations.\n"
71 " -S Server mode.\n"
72 " -D Do not emit \"Duplicate\" instructions.\n"
73 " -A Aggressive indexing.\n"
74 " -M Turn off memory guard.\n"
75 " -f Fast: use maximum output buffers.\n"
76 " -v Verbose: print various messages to stderr.\n"
77 "\n"
78 " -h Print this help screen and exit\n"
79 , name, LSQPACK_DEF_MAX_RISKED_STREAMS, LSQPACK_DEF_DYN_TABLE_SIZE);
80 }
81
82
83 static void
84 write_enc_stream (FILE *out, const unsigned char *enc_buf, size_t enc_sz)
85 {
86 uint64_t stream_id_enc;
87 uint32_t length_enc;
88 size_t written;
89
90 if (enc_sz <= 0)
91 return;
92 stream_id_enc = 0;
93 length_enc = enc_sz;
94 #if __BYTE_ORDER == __LITTLE_ENDIAN
95 stream_id_enc = bswap_64(stream_id_enc);
96 length_enc = bswap_32(length_enc);
97 #endif
98 written = fwrite(&stream_id_enc, 1, sizeof(stream_id_enc), out);
99 if (written != sizeof(stream_id_enc))
100 {
101 perror("fwrite");
102 exit(EXIT_FAILURE);
103 }
104 written = fwrite(&length_enc, 1, sizeof(length_enc), out);
105 if (written != sizeof(length_enc))
106 {
107 perror("fwrite");
108 exit(EXIT_FAILURE);
109 }
110 written = fwrite(enc_buf, 1, enc_sz, out);
111 if (written != enc_sz)
112 {
113 perror("fwrite");
114 exit(EXIT_FAILURE);
115 }
116 }
117
118
119 static void
120 write_enc_and_header_streams (FILE *out, unsigned stream_id,
121 const unsigned char *enc_buf, size_t enc_sz,
122 const unsigned char *pref_buf, size_t pref_sz,
123 const unsigned char *hea_buf, size_t hea_sz)
124 {
125 uint64_t stream_id_enc;
126 uint32_t length_enc;
127 size_t written;
128
129 if (s_verbose)
130 fprintf(stderr, "%s: stream %"PRIu32"\n", __func__, stream_id);
131
132 if (enc_sz)
133 {
134 stream_id_enc = 0;
135 length_enc = enc_sz;
136 #if __BYTE_ORDER == __LITTLE_ENDIAN
137 stream_id_enc = bswap_64(stream_id_enc);
138 length_enc = bswap_32(length_enc);
139 #endif
140 written = fwrite(&stream_id_enc, 1, sizeof(stream_id_enc), out);
141 if (written != sizeof(stream_id_enc))
142 goto write_err;
143 written = fwrite(&length_enc, 1, sizeof(length_enc), out);
144 if (written != sizeof(length_enc))
145 goto write_err;
146 written = fwrite(enc_buf, 1, enc_sz, out);
147 if (written != enc_sz)
148 goto write_err;
149 }
150
151 stream_id_enc = stream_id;
152 length_enc = pref_sz + hea_sz;
153 #if __BYTE_ORDER == __LITTLE_ENDIAN
154 stream_id_enc = bswap_64(stream_id_enc);
155 length_enc = bswap_32(length_enc);
156 #endif
157 written = fwrite(&stream_id_enc, 1, sizeof(stream_id_enc), out);
158 if (written != sizeof(stream_id_enc))
159 goto write_err;
160 written = fwrite(&length_enc, 1, sizeof(length_enc), out);
161 if (written != sizeof(length_enc))
162 goto write_err;
163 written = fwrite(pref_buf, 1, pref_sz, out);
164 if (written != pref_sz)
165 goto write_err;
166 written = fwrite(hea_buf, 1, hea_sz, out);
167 if (written != hea_sz)
168 goto write_err;
169 return;
170
171 write_err:
172 perror("fwrite");
173 exit(EXIT_FAILURE);
174 }
175
176
177 static unsigned s_saved_ins_count;
178 static int
179 ack_last_entry_id (struct lsqpack_enc *encoder)
180 {
181 unsigned char *end_cmd;
182 unsigned char cmd[80];
183 unsigned val;
184
185 if (s_verbose)
186 fprintf(stderr, "ACK entry ID %u\n", encoder->qpe_ins_count);
187
188 cmd[0] = 0x00;
189 val = encoder->qpe_ins_count - s_saved_ins_count;
190 s_saved_ins_count = encoder->qpe_ins_count;
191 end_cmd = lsqpack_enc_int(cmd, cmd + sizeof(cmd), val, 6);
192 assert(end_cmd > cmd);
193 return lsqpack_enc_decoder_in(encoder, cmd, end_cmd - cmd);
194 }
195
196
197 static int
198 ack_stream (struct lsqpack_enc *encoder, uint64_t stream_id)
199 {
200 unsigned char *end_cmd;
201 unsigned char cmd[80];
202
203 if (s_verbose)
204 fprintf(stderr, "ACK stream ID %"PRIu64"\n", stream_id);
205
206 cmd[0] = 0x80;
207 end_cmd = lsqpack_enc_int(cmd, cmd + sizeof(cmd), stream_id, 7);
208 assert(end_cmd > cmd);
209 return lsqpack_enc_decoder_in(encoder, cmd, end_cmd - cmd);
210 }
211
212
213 static int
214 sync_table (struct lsqpack_enc *encoder, uint64_t num_inserts)
215 {
216 unsigned char *end_cmd;
217 unsigned char cmd[80];
218
219 if (s_verbose)
220 fprintf(stderr, "Sync table num inserts %"PRIu64"\n", num_inserts);
221
222 cmd[0] = 0x00;
223 end_cmd = lsqpack_enc_int(cmd, cmd + sizeof(cmd), num_inserts, 6);
224 assert(end_cmd > cmd);
225 return lsqpack_enc_decoder_in(encoder, cmd, end_cmd - cmd);
226 }
227
228
229 static int
230 cancel_stream (struct lsqpack_enc *encoder, uint64_t stream_id)
231 {
232 unsigned char *end_cmd;
233 unsigned char cmd[80];
234
235 if (s_verbose)
236 fprintf(stderr, "Cancel stream ID %"PRIu64"\n", stream_id);
237
238 cmd[0] = 0x40;
239 end_cmd = lsqpack_enc_int(cmd, cmd + sizeof(cmd), stream_id, 6);
240 assert(end_cmd > cmd);
241 return lsqpack_enc_decoder_in(encoder, cmd, end_cmd - cmd);
242 }
243
244
245 int
246 main (int argc, char **argv)
247 {
248 FILE *in = stdin;
249 FILE *out = stdout;
250 int opt;
251 unsigned dyn_table_size = LSQPACK_DEF_DYN_TABLE_SIZE,
252 max_risked_streams = LSQPACK_DEF_MAX_RISKED_STREAMS;
253 unsigned lineno, stream_id;
254 struct lsqpack_enc encoder;
255 char *line, *end, *tab;
256 ssize_t pref_sz;
257 enum lsqpack_enc_status st;
258 enum lsqpack_enc_opts enc_opts = 0;
259 size_t enc_sz, hea_sz, enc_off, hea_off;
260 int header_opened, r;
261 unsigned arg;
262 enum { ACK_NEVER, ACK_IMMEDIATE, } ack_mode = ACK_NEVER;
263 int process_annotations = 0;
264 char line_buf[0x1000];
265 unsigned char tsu_buf[LSQPACK_LONGEST_SDTC];
266 size_t tsu_buf_sz;
267 enum lsqpack_enc_header_flags hflags;
268 int fast = 0;
269 struct lsxpack_header xhdr;
270 unsigned char enc_buf[0x1000], hea_buf[0x1000], pref_buf[0x20];
271
272 while (-1 != (opt = getopt(argc, argv, "ADMSa:i:no:s:t:hvf")))
273 {
274 switch (opt)
275 {
276 case 'S':
277 enc_opts |= LSQPACK_ENC_OPT_SERVER;
278 break;
279 case 'D':
280 enc_opts |= LSQPACK_ENC_OPT_NO_DUP;
281 break;
282 case 'A':
283 enc_opts |= LSQPACK_ENC_OPT_IX_AGGR;
284 break;
285 case 'M':
286 enc_opts |= LSQPACK_ENC_OPT_NO_MEM_GUARD;
287 break;
288 case 'n':
289 ++process_annotations;
290 break;
291 case 'a':
292 ack_mode = atoi(optarg) ? ACK_IMMEDIATE : ACK_NEVER;
293 break;
294 case 'i':
295 if (0 != strcmp(optarg, "-"))
296 {
297 in = fopen(optarg, "r");
298 if (!in)
299 {
300 fprintf(stderr, "cannot open `%s' for reading: %s\n",
301 optarg, strerror(errno));
302 exit(EXIT_FAILURE);
303 }
304 }
305 break;
306 case 'o':
307 if (0 != strcmp(optarg, "-"))
308 {
309 out = fopen(optarg, "wb");
310 if (!out)
311 {
312 fprintf(stderr, "cannot open `%s' for writing: %s\n",
313 optarg, strerror(errno));
314 exit(EXIT_FAILURE);
315 }
316 }
317 break;
318 case 's':
319 max_risked_streams = atoi(optarg);
320 break;
321 case 't':
322 dyn_table_size = atoi(optarg);
323 break;
324 case 'h':
325 usage(argv[0]);
326 exit(EXIT_SUCCESS);
327 case 'f':
328 fast = 1;
329 break;
330 case 'v':
331 ++s_verbose;
332 break;
333 default:
334 exit(EXIT_FAILURE);
335 }
336 }
337
338 tsu_buf_sz = sizeof(tsu_buf);
339 if (0 != lsqpack_enc_init(&encoder, s_verbose ? stderr : NULL, dyn_table_size,
340 dyn_table_size, max_risked_streams, enc_opts, tsu_buf,
341 &tsu_buf_sz))
342 {
343 perror("lsqpack_enc_init");
344 exit(EXIT_FAILURE);
345 }
346
347 lineno = 0;
348 stream_id = 0;
349 enc_off = 0;
350 hea_off = 0;
351 header_opened = 0;
352
353 while (line = fgets(line_buf, sizeof(line_buf), in), line != NULL)
354 {
355 ++lineno;
356 end = strchr(line, '\n');
357 if (!end)
358 {
359 fprintf(stderr, "no newline on line %u\n", lineno);
360 exit(EXIT_FAILURE);
361 }
362 *end = '\0';
363
364 if (end == line)
365 {
366 if (header_opened)
367 {
368 size_t sz, pref_max = sizeof(pref_buf);
369 for (sz = (fast ? pref_max : 0); sz <= pref_max; sz++)
370 {
371 pref_sz = lsqpack_enc_end_header(&encoder, pref_buf, sz, &hflags);
372 if (pref_sz > 0)
373 {
374 if (max_risked_streams == 0)
375 assert(!(hflags & LSQECH_REF_AT_RISK));
376 break;
377 }
378 }
379 assert(pref_sz <= lsqpack_enc_header_block_prefix_size(&encoder));
380 if (pref_sz < 0)
381 {
382 fprintf(stderr, "end_header failed: %s", strerror(errno));
383 exit(EXIT_FAILURE);
384 }
385 if (ack_mode == ACK_IMMEDIATE)
386 {
387 if (!(2 == pref_sz && pref_buf[0] == 0 && pref_buf[1] == 0))
388 r = ack_stream(&encoder, stream_id);
389 else
390 r = 0;
391 if (r == 0 && encoder.qpe_ins_count > s_saved_ins_count)
392 r = ack_last_entry_id(&encoder);
393 else
394 r = 0;
395 if (r != 0)
396 {
397 fprintf(stderr, "acking stream %u failed: %s", stream_id,
398 strerror(errno));
399 exit(EXIT_FAILURE);
400 }
401 }
402 if (s_verbose)
403 fprintf(stderr, "compression ratio: %.3f\n",
404 lsqpack_enc_ratio(&encoder));
405 write_enc_and_header_streams(out, stream_id, enc_buf, enc_off,
406 pref_buf, pref_sz, hea_buf, hea_off);
407 enc_off = 0;
408 hea_off = 0;
409 header_opened = 0;
410 }
411 continue;
412 }
413
414 if (*line == '#')
415 {
416 if (!process_annotations)
417 continue;
418
419 /* Lines starting with ## are potential annotations */
420 if (ack_mode != ACK_IMMEDIATE
421 /* Ignore ACK annotations in immediate ACK mode, as we do
422 * not tolerate duplicate ACKs.
423 */
424 && 1 == sscanf(line, "## %*[a] %u ", &arg))
425 {
426 if (0 != ack_stream(&encoder, arg))
427 {
428 fprintf(stderr, "ACKing stream ID %u failed\n", arg);
429 exit(EXIT_FAILURE);
430 }
431 }
432 else if (1 == sscanf(line, "## %*[s] %u", &arg))
433 sync_table(&encoder, arg);
434 else if (1 == sscanf(line, "## %*[c] %u", &arg))
435 cancel_stream(&encoder, arg);
436 else if (1 == sscanf(line, "## %*[t] %u", &arg))
437 {
438 tsu_buf_sz = sizeof(tsu_buf);
439 if (0 != lsqpack_enc_set_max_capacity(&encoder, arg, tsu_buf,
440 &tsu_buf_sz))
441 {
442 fprintf(stderr, "cannot set capacity to %u: %s\n", arg,
443 strerror(errno));
444 exit(EXIT_FAILURE);
445 }
446 write_enc_stream(out, tsu_buf, tsu_buf_sz);
447 }
448 continue;
449 }
450
451 tab = strchr(line, '\t');
452 if (!tab)
453 {
454 fprintf(stderr, "no TAB on line %u\n", lineno);
455 exit(EXIT_FAILURE);
456 }
457
458 if (!header_opened)
459 {
460 ++stream_id;
461 if (0 != lsqpack_enc_start_header(&encoder, stream_id, 0))
462 {
463 fprintf(stderr, "start_header failed: %s\n", strerror(errno));
464 exit(EXIT_FAILURE);
465 }
466 header_opened = 1;
467 }
468 if (fast)
469 {
470 enc_sz = sizeof(enc_buf) - enc_off;
471 hea_sz = sizeof(hea_buf) - hea_off;
472 }
473 else
474 {
475 /* Increase buffers one by one to exercise error conditions */
476 enc_sz = 0;
477 hea_sz = 0;
478 }
479 while (1)
480 {
481 lsxpack_header_set_offset2(&xhdr, line, 0, tab - line,
482 tab + 1 - line, end - tab - 1);
483 st = lsqpack_enc_encode(&encoder, enc_buf + enc_off, &enc_sz,
484 hea_buf + hea_off, &hea_sz, &xhdr, 0);
485 switch (st)
486 {
487 case LQES_NOBUF_ENC:
488 if (enc_sz < sizeof(enc_buf) - enc_off)
489 ++enc_sz;
490 else
491 assert(0);
492 break;
493 case LQES_NOBUF_HEAD:
494 if (hea_sz < sizeof(hea_buf) - hea_off)
495 ++hea_sz;
496 else
497 assert(0);
498 break;
499 default:
500 assert(st == LQES_OK);
501 goto end_encode_one_header;
502 }
503 }
504 if (st != LQES_OK)
505 {
506 /* It could only run of of output space, so it's not really an
507 * error, but we make no provision in the interop encoder to
508 * grow the buffers.
509 */
510 fprintf(stderr, "Could not encode header on line %u: %u\n",
511 lineno, st);
512 exit(EXIT_FAILURE);
513 }
514 end_encode_one_header:
515 enc_off += enc_sz;
516 hea_off += hea_sz;
517 }
518
519 if (s_verbose)
520 fprintf(stderr, "exited while loop\n");
521
522 (void) fclose(in);
523
524 if (header_opened)
525 {
526 if (s_verbose)
527 fprintf(stderr, "close opened header\n");
528 pref_sz = lsqpack_enc_end_header(&encoder, pref_buf, sizeof(pref_buf),
529 &hflags);
530 if (pref_sz < 0)
531 {
532 fprintf(stderr, "end_header failed: %s", strerror(errno));
533 exit(EXIT_FAILURE);
534 }
535 if (max_risked_streams == 0)
536 assert(!(hflags & LSQECH_REF_AT_RISK));
537 if (ack_mode == ACK_IMMEDIATE
538 && !(2 == pref_sz && pref_buf[0] == 0 && pref_buf[1] == 0)
539 && 0 != ack_stream(&encoder, stream_id))
540 {
541 fprintf(stderr, "acking stream %u failed: %s", stream_id,
542 strerror(errno));
543 exit(EXIT_FAILURE);
544 }
545 if (s_verbose)
546 fprintf(stderr, "compression ratio: %.3f\n",
547 lsqpack_enc_ratio(&encoder));
548 write_enc_and_header_streams(out, stream_id, enc_buf, enc_off, pref_buf,
549 pref_sz, hea_buf, hea_off);
550 }
551
552 lsqpack_enc_cleanup(&encoder);
553
554 if (0 != fclose(out))
555 {
556 perror("fclose(out)");
557 exit(EXIT_FAILURE);
558 }
559
560 exit(EXIT_SUCCESS);
561 }