forked from litespeedtech/ls-qpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsqpack.c
5381 lines (4839 loc) · 175 KB
/
lsqpack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* lsqpack.c -- LiteSpeed QPACK Compression Library: encoder and decoder.
*/
/*
MIT License
Copyright (c) 2018 - 2020 LiteSpeed Technologies Inc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef LS_QPACK_USE_LARGE_TABLES
#define LS_QPACK_USE_LARGE_TABLES 1
#endif
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <inttypes.h>
#include "lsqpack.h"
#include "lsxpack_header.h"
#ifdef XXH_HEADER_NAME
#include XXH_HEADER_NAME
#else
#include <xxhash.h>
#endif
#include "huff-tables.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static unsigned char *
qenc_huffman_enc (const unsigned char *, const unsigned char *const, unsigned char *);
static unsigned
qenc_enc_str_size (const unsigned char *, unsigned);
struct static_table_entry
{
const char *name;
const char *val;
unsigned name_len;
unsigned val_len;
};
/* [draft-ietf-quic-qpack-03] Appendix A */
static const struct static_table_entry static_table[] =
{
{":authority", "", 10, 0,},
{":path", "/", 5, 1,},
{"age", "0", 3, 1,},
{"content-disposition", "", 19, 0,},
{"content-length", "0", 14, 1,},
{"cookie", "", 6, 0,},
{"date", "", 4, 0,},
{"etag", "", 4, 0,},
{"if-modified-since", "", 17, 0,},
{"if-none-match", "", 13, 0,},
{"last-modified", "", 13, 0,},
{"link", "", 4, 0,},
{"location", "", 8, 0,},
{"referer", "", 7, 0,},
{"set-cookie", "", 10, 0,},
{":method", "CONNECT", 7, 7,},
{":method", "DELETE", 7, 6,},
{":method", "GET", 7, 3,},
{":method", "HEAD", 7, 4,},
{":method", "OPTIONS", 7, 7,},
{":method", "POST", 7, 4,},
{":method", "PUT", 7, 3,},
{":scheme", "http", 7, 4,},
{":scheme", "https", 7, 5,},
{":status", "103", 7, 3,},
{":status", "200", 7, 3,},
{":status", "304", 7, 3,},
{":status", "404", 7, 3,},
{":status", "503", 7, 3,},
{"accept", "*/*", 6, 3,},
{"accept", "application/dns-message", 6, 23,},
{"accept-encoding", "gzip, deflate, br", 15, 17,},
{"accept-ranges", "bytes", 13, 5,},
{"access-control-allow-headers", "cache-control", 28, 13,},
{"access-control-allow-headers", "content-type", 28, 12,},
{"access-control-allow-origin", "*", 27, 1,},
{"cache-control", "max-age=0", 13, 9,},
{"cache-control", "max-age=2592000", 13, 15,},
{"cache-control", "max-age=604800", 13, 14,},
{"cache-control", "no-cache", 13, 8,},
{"cache-control", "no-store", 13, 8,},
{"cache-control", "public, max-age=31536000", 13, 24,},
{"content-encoding", "br", 16, 2,},
{"content-encoding", "gzip", 16, 4,},
{"content-type", "application/dns-message", 12, 23,},
{"content-type", "application/javascript", 12, 22,},
{"content-type", "application/json", 12, 16,},
{"content-type", "application/x-www-form-urlencoded", 12, 33,},
{"content-type", "image/gif", 12, 9,},
{"content-type", "image/jpeg", 12, 10,},
{"content-type", "image/png", 12, 9,},
{"content-type", "text/css", 12, 8,},
{"content-type", "text/html; charset=utf-8", 12, 24,},
{"content-type", "text/plain", 12, 10,},
{"content-type", "text/plain;charset=utf-8", 12, 24,},
{"range", "bytes=0-", 5, 8,},
{"strict-transport-security", "max-age=31536000", 25, 16,},
{"strict-transport-security", "max-age=31536000; includesubdomains",
25, 35,},
{"strict-transport-security",
"max-age=31536000; includesubdomains; preload", 25, 44,},
{"vary", "accept-encoding", 4, 15,},
{"vary", "origin", 4, 6,},
{"x-content-type-options", "nosniff", 22, 7,},
{"x-xss-protection", "1; mode=block", 16, 13,},
{":status", "100", 7, 3,},
{":status", "204", 7, 3,},
{":status", "206", 7, 3,},
{":status", "302", 7, 3,},
{":status", "400", 7, 3,},
{":status", "403", 7, 3,},
{":status", "421", 7, 3,},
{":status", "425", 7, 3,},
{":status", "500", 7, 3,},
{"accept-language", "", 15, 0,},
{"access-control-allow-credentials", "FALSE", 32, 5,},
{"access-control-allow-credentials", "TRUE", 32, 4,},
{"access-control-allow-headers", "*", 28, 1,},
{"access-control-allow-methods", "get", 28, 3,},
{"access-control-allow-methods", "get, post, options", 28, 18,},
{"access-control-allow-methods", "options", 28, 7,},
{"access-control-expose-headers", "content-length", 29, 14,},
{"access-control-request-headers", "content-type", 30, 12,},
{"access-control-request-method", "get", 29, 3,},
{"access-control-request-method", "post", 29, 4,},
{"alt-svc", "clear", 7, 5,},
{"authorization", "", 13, 0,},
{"content-security-policy",
"script-src 'none'; object-src 'none'; base-uri 'none'", 23, 53,},
{"early-data", "1", 10, 1,},
{"expect-ct", "", 9, 0,},
{"forwarded", "", 9, 0,},
{"if-range", "", 8, 0,},
{"origin", "", 6, 0,},
{"purpose", "prefetch", 7, 8,},
{"server", "", 6, 0,},
{"timing-allow-origin", "*", 19, 1,},
{"upgrade-insecure-requests", "1", 25, 1,},
{"user-agent", "", 10, 0,},
{"x-forwarded-for", "", 15, 0,},
{"x-frame-options", "deny", 15, 4,},
{"x-frame-options", "sameorigin", 15, 10,},
};
#define QPACK_STATIC_TABLE_SIZE (sizeof(static_table) / sizeof(static_table[0]))
/* RFC 7541, Section 4.1:
*
* " The size of the dynamic table is the sum of the size of its entries.
* "
* " The size of an entry is the sum of its name's length in octets (as
* " defined in Section 5.2), its value's length in octets, and 32.
*/
#define DYNAMIC_ENTRY_OVERHEAD 32u
/* Initial guess at number of header fields per list: */
#define GUESS_N_HEADER_FIELDS 12
#define MAX_QUIC_STREAM_ID ((1ull << 62) - 1)
#ifdef LSQPACK_ENC_LOGGER_HEADER
#include LSQPACK_ENC_LOGGER_HEADER
#else
#define E_LOG(prefix, ...) do { \
if (enc->qpe_logger_ctx) { \
fprintf(enc->qpe_logger_ctx, prefix); \
fprintf(enc->qpe_logger_ctx, __VA_ARGS__); \
fprintf(enc->qpe_logger_ctx, "\n"); \
} \
} while (0)
#define E_DEBUG(...) E_LOG("qenc: debug: ", __VA_ARGS__)
#define E_INFO(...) E_LOG("qenc: info: ", __VA_ARGS__)
#define E_WARN(...) E_LOG("qenc: warn: ", __VA_ARGS__)
#define E_ERROR(...) E_LOG("qenc: error: ", __VA_ARGS__)
#endif
/* Entries in the encoder's dynamic table are hashed 1) by name and 2) by
* name and value. Instead of having two arrays of buckets, the encoder
* keeps just one, but each bucket has two heads.
*/
struct lsqpack_double_enc_head
{
struct lsqpack_enc_head by_name;
struct lsqpack_enc_head by_nameval;
};
struct lsqpack_enc_table_entry
{
/* An entry always lives on all three lists */
STAILQ_ENTRY(lsqpack_enc_table_entry)
ete_next_nameval,
ete_next_name,
ete_next_all;
lsqpack_abs_id_t ete_id;
unsigned ete_when_added_used;
unsigned ete_when_added_dropped;
unsigned ete_n_reffd;
unsigned ete_nameval_hash;
unsigned ete_name_hash;
unsigned ete_name_len;
unsigned ete_val_len;
char ete_buf[0];
};
#define ETE_NAME(ete) ((ete)->ete_buf)
#define ETE_VALUE(ete) (&(ete)->ete_buf[(ete)->ete_name_len])
#define ENTRY_COST(name_len, value_len) (DYNAMIC_ENTRY_OVERHEAD + \
name_len + value_len)
#define ETE_SIZE(ete) ENTRY_COST((ete)->ete_name_len, (ete)->ete_val_len)
#define N_BUCKETS(n_bits) (1U << (n_bits))
#define BUCKNO(n_bits, hash) ((hash) & (N_BUCKETS(n_bits) - 1))
struct lsqpack_header_info
{
TAILQ_ENTRY(lsqpack_header_info) qhi_next_all;
TAILQ_ENTRY(lsqpack_header_info) qhi_next_risked;
struct lsqpack_header_info *qhi_same_stream_id; /* Circular list */
uint64_t qhi_stream_id;
unsigned qhi_seqno;
unsigned qhi_bytes_inserted;
lsqpack_abs_id_t qhi_min_id;
lsqpack_abs_id_t qhi_max_id;
};
/* Absolute index starts with 1. 0 indicates that the value is not set */
#define HINFO_IDS_SET(hinfo) ((hinfo)->qhi_max_id != 0)
/* Header info structures are kept in a list of arrays, which is faster than
* searching through a linked list whose elements may be all over the place
* in memory. This is important because we need to look up header infos by
* stream ID and also calculate the minimum absolute ID. If not sequential
* search, we would need to implement a hybrid of min-heap and a hash (or
* search tree) and that seems to be an overkill for something that is not
* likely to have more than a few hundred elements at the most.
*/
struct lsqpack_header_info_arr
{
STAILQ_ENTRY(lsqpack_header_info_arr) hia_next;
uint64_t hia_slots;
struct lsqpack_header_info hia_hinfos[64];
};
static unsigned
find_free_slot (uint64_t slots)
{
#if __GNUC__
return __builtin_ffsll(~slots) - 1;
#else
unsigned n;
slots =~ slots;
n = 0;
if (0 == (slots & ((1ULL << 32) - 1))) { n += 32; slots >>= 32; }
if (0 == (slots & ((1ULL << 16) - 1))) { n += 16; slots >>= 16; }
if (0 == (slots & ((1ULL << 8) - 1))) { n += 8; slots >>= 8; }
if (0 == (slots & ((1ULL << 4) - 1))) { n += 4; slots >>= 4; }
if (0 == (slots & ((1ULL << 2) - 1))) { n += 2; slots >>= 2; }
if (0 == (slots & ((1ULL << 1) - 1))) { n += 1; slots >>= 1; }
return n;
#endif
}
static struct lsqpack_header_info *
enc_alloc_hinfo (struct lsqpack_enc *enc)
{
struct lsqpack_header_info_arr *hiarr;
struct lsqpack_header_info *hinfo;
unsigned slot;
STAILQ_FOREACH(hiarr, &enc->qpe_hinfo_arrs, hia_next)
if (hiarr->hia_slots != ~0ULL)
break;
if (!hiarr)
{
if (0 == (enc->qpe_flags & LSQPACK_ENC_NO_MEM_GUARD)
&& enc->qpe_hinfo_arrs_count * sizeof(*hiarr)
>= enc->qpe_cur_max_capacity)
return NULL;
hiarr = malloc(sizeof(*hiarr));
if (!hiarr)
return NULL;
hiarr->hia_slots = 0;
STAILQ_INSERT_TAIL(&enc->qpe_hinfo_arrs, hiarr, hia_next);
++enc->qpe_hinfo_arrs_count;
}
slot = find_free_slot(hiarr->hia_slots);
hiarr->hia_slots |= 1ULL << slot;
hinfo = &hiarr->hia_hinfos[ slot ];
memset(hinfo, 0, sizeof(*hinfo));
hinfo->qhi_same_stream_id = hinfo;
TAILQ_INSERT_TAIL(&enc->qpe_all_hinfos, hinfo, qhi_next_all);
return hinfo;
}
static void
enc_free_hinfo (struct lsqpack_enc *enc, struct lsqpack_header_info *hinfo)
{
struct lsqpack_header_info_arr *hiarr;
unsigned slot;
STAILQ_FOREACH(hiarr, &enc->qpe_hinfo_arrs, hia_next)
if (hinfo >= hiarr->hia_hinfos && hinfo < &hiarr->hia_hinfos[64])
{
slot = hinfo - hiarr->hia_hinfos;
hiarr->hia_slots &= ~(1ULL << slot);
TAILQ_REMOVE(&enc->qpe_all_hinfos, &hiarr->hia_hinfos[slot], qhi_next_all);
return;
}
assert(0);
}
static int
enc_use_dynamic_table (const struct lsqpack_enc *enc)
{
return enc->qpe_cur_header.hinfo != NULL
&& enc->qpe_cur_header.hinfo->qhi_bytes_inserted
< enc->qpe_cur_max_capacity / 2;
}
enum he { HE_NAME, HE_NAMEVAL, N_HES };
struct lsqpack_hist_el {
unsigned he_hashes[N_HES];
};
static void
qenc_hist_update_size (struct lsqpack_enc *enc, unsigned new_size)
{
struct lsqpack_hist_el *els;
unsigned first, count, i, j;
if (new_size == enc->qpe_hist_nels)
return;
if (new_size == 0)
{
enc->qpe_hist_nels = 0;
enc->qpe_hist_idx = 0;
enc->qpe_hist_wrapped = 0;
return;
}
els = malloc(sizeof(els[0]) * (new_size + 1));
if (!els)
return;
E_DEBUG("history size change from %u to %u", enc->qpe_hist_nels, new_size);
if (enc->qpe_hist_wrapped)
{
first = (enc->qpe_hist_idx + 1) % enc->qpe_hist_nels;
count = enc->qpe_hist_nels;
}
else
{
first = 0;
count = enc->qpe_hist_idx;
}
for (i = 0, j = 0; count > 0 && j < new_size; ++i, ++j, --count)
els[j] = enc->qpe_hist_els[ (first + i) % enc->qpe_hist_nels ];
enc->qpe_hist_nels = new_size;
enc->qpe_hist_idx = j % new_size;
enc->qpe_hist_wrapped = enc->qpe_hist_idx == 0;
free(enc->qpe_hist_els);
enc->qpe_hist_els = els;
}
static void
qenc_hist_add (struct lsqpack_enc *enc, unsigned name_hash,
unsigned nameval_hash)
{
if (enc->qpe_hist_nels)
{
enc->qpe_hist_els[ enc->qpe_hist_idx ].he_hashes[HE_NAME] = name_hash;
enc->qpe_hist_els[ enc->qpe_hist_idx ].he_hashes[HE_NAMEVAL]
= nameval_hash;
enc->qpe_hist_idx = (enc->qpe_hist_idx + 1) % enc->qpe_hist_nels;
enc->qpe_hist_wrapped |= enc->qpe_hist_idx == 0;
}
}
static int
qenc_hist_seen (struct lsqpack_enc *enc, enum he he, unsigned hash)
{
const struct lsqpack_hist_el *el;
unsigned last_idx;
if (enc->qpe_hist_els)
{
if (enc->qpe_hist_wrapped)
last_idx = enc->qpe_hist_nels;
else
last_idx = enc->qpe_hist_idx;
enc->qpe_hist_els[ last_idx ].he_hashes[he] = hash;
for (el = enc->qpe_hist_els; el->he_hashes[he] != hash; ++el)
;
return el < &enc->qpe_hist_els[ last_idx ];
}
else
return 1;
}
unsigned char *
lsqpack_enc_int (unsigned char *, unsigned char *const, uint64_t, unsigned);
void
lsqpack_enc_preinit (struct lsqpack_enc *enc, void *logger_ctx)
{
memset(enc, 0, sizeof(*enc));
STAILQ_INIT(&enc->qpe_all_entries);
STAILQ_INIT(&enc->qpe_hinfo_arrs);
TAILQ_INIT(&enc->qpe_all_hinfos);
TAILQ_INIT(&enc->qpe_risked_hinfos);
enc->qpe_logger_ctx = logger_ctx;
E_DEBUG("preinitialized");
};
int
lsqpack_enc_init (struct lsqpack_enc *enc, void *logger_ctx,
unsigned max_table_size, unsigned dyn_table_size,
unsigned max_risked_streams, enum lsqpack_enc_opts enc_opts,
unsigned char *tsu_buf, size_t *tsu_buf_sz)
{
struct lsqpack_double_enc_head *buckets;
unsigned char *p;
unsigned nbits = 2;
unsigned i;
if (dyn_table_size > max_table_size)
{
errno = EINVAL;
return -1;
}
if (!(enc_opts & LSQPACK_ENC_OPT_STAGE_2))
lsqpack_enc_preinit(enc, logger_ctx);
if (dyn_table_size != LSQPACK_DEF_DYN_TABLE_SIZE)
{
if (!(tsu_buf && tsu_buf_sz && *tsu_buf_sz))
{
errno = EINVAL;
return -1;
}
p = tsu_buf;
*p = 0x20;
p = lsqpack_enc_int(p, tsu_buf + *tsu_buf_sz, dyn_table_size, 5);
if (p <= tsu_buf)
{
errno = ENOBUFS;
return -1;
}
E_DEBUG("generated TSU=%u instruction %zd byte%.*s in size",
dyn_table_size, p - tsu_buf, p - tsu_buf != 1, "s");
*tsu_buf_sz = p - tsu_buf;
}
else if (tsu_buf_sz)
*tsu_buf_sz = 0;
if (!(enc_opts & LSQPACK_ENC_OPT_IX_AGGR))
{
enc->qpe_hist_nels = MAX(
/* Initial guess at number of entries in dynamic table: */
dyn_table_size / DYNAMIC_ENTRY_OVERHEAD / 3,
GUESS_N_HEADER_FIELDS
);
enc->qpe_hist_els = malloc(sizeof(enc->qpe_hist_els[0]) * (enc->qpe_hist_nels + 1));
if (!enc->qpe_hist_els)
return -1;
}
else
{
enc->qpe_hist_nels = 0;
enc->qpe_hist_els = NULL;
}
if (max_table_size / DYNAMIC_ENTRY_OVERHEAD)
{
nbits = 2;
buckets = malloc(sizeof(buckets[0]) * N_BUCKETS(nbits));
if (!buckets)
{
free(enc->qpe_hist_els);
return -1;
}
for (i = 0; i < N_BUCKETS(nbits); ++i)
{
STAILQ_INIT(&buckets[i].by_name);
STAILQ_INIT(&buckets[i].by_nameval);
}
}
else
{
nbits = 0;
buckets = NULL;
}
enc->qpe_max_entries = max_table_size / DYNAMIC_ENTRY_OVERHEAD;
enc->qpe_real_max_capacity = max_table_size;
enc->qpe_cur_max_capacity = dyn_table_size;
enc->qpe_max_risked_streams = max_risked_streams;
enc->qpe_buckets = buckets;
enc->qpe_nbits = nbits;
enc->qpe_logger_ctx = logger_ctx;
if (!(enc_opts & LSQPACK_ENC_OPT_NO_DUP))
enc->qpe_flags |= LSQPACK_ENC_USE_DUP;
if (enc_opts & LSQPACK_ENC_OPT_NO_MEM_GUARD)
enc->qpe_flags |= LSQPACK_ENC_NO_MEM_GUARD;
E_DEBUG("initialized. opts: 0x%X; max capacity: %u; max risked "
"streams: %u.", enc_opts, enc->qpe_cur_max_capacity,
enc->qpe_max_risked_streams);
return 0;
}
void
lsqpack_enc_cleanup (struct lsqpack_enc *enc)
{
struct lsqpack_enc_table_entry *entry, *next;
struct lsqpack_header_info_arr *hiarr, *next_hiarr;
for (entry = STAILQ_FIRST(&enc->qpe_all_entries); entry; entry = next)
{
next = STAILQ_NEXT(entry, ete_next_all);
free(entry);
}
for (hiarr = STAILQ_FIRST(&enc->qpe_hinfo_arrs); hiarr; hiarr = next_hiarr)
{
next_hiarr = STAILQ_NEXT(hiarr, hia_next);
free(hiarr);
}
free(enc->qpe_buckets);
free(enc->qpe_hist_els);
E_DEBUG("cleaned up");
}
#define LSQPACK_XXH_SEED 39378473
#define XXH_NAME_WIDTH 9
#define XXH_NAME_SHIFT 0
#define XXH_NAMEVAL_WIDTH 9
#define XXH_NAMEVAL_SHIFT 0
static const unsigned char name2id_plus_one[ 1 << XXH_NAME_WIDTH ] =
{
[347] = 1, [397] = 2, [64] = 3, [144] = 4, [25] = 5,
[216] = 6, [361] = 7, [442] = 8, [190] = 9, [404] = 10,
[181] = 11, [210] = 12, [38] = 13, [51] = 14, [318] = 15,
[484] = 16, [81] = 23, [83] = 25, [248] = 30, [169] = 32,
[456] = 33, [479] = 34, [59] = 36, [498] = 37, [401] = 43,
[453] = 45, [266] = 56, [88] = 57, [317] = 60, [74] = 62,
[189] = 63, [211] = 73, [334] = 74, [365] = 77, [382] = 80,
[377] = 81, [257] = 82, [56] = 84, [321] = 85, [79] = 86,
[384] = 87, [357] = 88, [438] = 89, [84] = 90, [264] = 91,
[146] = 92, [225] = 93, [490] = 94, [305] = 95, [362] = 96,
[486] = 97, [497] = 98,
};
static const unsigned char nameval2id_plus_one[ 1 << XXH_NAMEVAL_WIDTH ] =
{
[150] = 1, [502] = 2, [353] = 3, [262] = 4, [443] = 5,
[164] = 6, [463] = 7, [84] = 8, [205] = 9, [228] = 10,
[451] = 11, [444] = 12, [176] = 13, [75] = 14, [399] = 15,
[56] = 16, [384] = 17, [21] = 18, [484] = 19, [382] = 20,
[439] = 21, [329] = 22, [360] = 23, [67] = 24, [105] = 25,
[342] = 26, [457] = 27, [161] = 28, [337] = 29, [135] = 30,
[314] = 31, [370] = 32, [404] = 33, [184] = 34, [156] = 35,
[139] = 36, [339] = 37, [508] = 38, [267] = 39, [375] = 40,
[122] = 41, [297] = 42, [144] = 43, [85] = 44, [466] = 45,
[38] = 46, [320] = 47, [273] = 48, [277] = 49, [136] = 50,
[454] = 51, [477] = 52, [91] = 53, [227] = 54, [301] = 55,
[272] = 56, [319] = 57, [142] = 58, [268] = 59, [65] = 60,
[410] = 61, [4] = 62, [373] = 63, [1] = 64, [210] = 65,
[224] = 66, [423] = 67, [222] = 68, [386] = 69, [12] = 70,
[7] = 71, [391] = 72, [73] = 73, [307] = 74, [27] = 75,
[256] = 76, [154] = 77, [204] = 78, [310] = 79, [198] = 80,
[162] = 81, [334] = 82, [438] = 83, [69] = 84, [188] = 85,
[244] = 86, [190] = 87, [465] = 88, [468] = 89, [417] = 90,
[110] = 91, [107] = 92, [368] = 93, [460] = 94, [54] = 95,
[492] = 96, [402] = 97, [196] = 98, [383] = 99,
};
static const uint32_t name_hashes[] =
{
0x653A915Bu, 0x3513518Du, 0xBEC8E440u, 0x16020A90u, 0x48F5CC19u,
0x0B486ED8u, 0x1A7AA369u, 0x6DE855BAu, 0xF2BADABEu, 0xD8CA2594u,
0x6B86C0B5u, 0xC62FECD2u, 0x8DA64A26u, 0x01F10233u, 0x8F7E493Eu,
0xC7742BE4u, 0xC7742BE4u, 0xC7742BE4u, 0xC7742BE4u, 0xC7742BE4u,
0xC7742BE4u, 0xC7742BE4u, 0xF49F1451u, 0xF49F1451u, 0x672BDA53u,
0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0x1AB214F8u,
0x1AB214F8u, 0xF93AD8A9u, 0x1DC691C8u, 0x7C21CFDFu, 0x7C21CFDFu,
0x7D3B7A3Bu, 0xC25511F2u, 0xC25511F2u, 0xC25511F2u, 0xC25511F2u,
0xC25511F2u, 0xC25511F2u, 0x48011191u, 0x48011191u, 0x085EF7C5u,
0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u,
0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u, 0x085EF7C5u,
0xB396750Au, 0x85E74C58u, 0x85E74C58u, 0x85E74C58u, 0x1A04DF3Du,
0x1A04DF3Du, 0x28686A4Au, 0x9F8BCEBDu, 0x672BDA53u, 0x672BDA53u,
0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0x672BDA53u,
0x672BDA53u, 0x672BDA53u, 0x98BD32D3u, 0x0A829D4Eu, 0x0A829D4Eu,
0x7C21CFDFu, 0x363F796Du, 0x363F796Du, 0x363F796Du, 0xD8A0B17Eu,
0xAAF9FD79u, 0x617E4501u, 0x617E4501u, 0x1E6DBE38u, 0x19D88141u,
0x3392084Fu, 0x5579EF80u, 0x8F3D7765u, 0x7EDC71B6u, 0xFBA64C54u,
0x3ECDA708u, 0xEBA96E92u, 0x82E1B4E1u, 0x5AD275EAu, 0xDD09E931u,
0x34C0456Au, 0x5EF889E6u, 0x4B1BB7F1u, 0x4B1BB7F1u,
};
static const uint32_t nameval_hashes[] =
{
0xF8614896u, 0xC8C267F6u, 0xF4617F61u, 0x8410A906u, 0xC8D109BBu,
0x51D448A4u, 0x52C167CFu, 0xFB22AA54u, 0x4F5272CDu, 0x9D4170E4u,
0x4E8C1DC3u, 0x684BDDBCu, 0xE113A2B0u, 0x5010D24Bu, 0xBCA5998Fu,
0xC8490E38u, 0x19094780u, 0x25D95A15u, 0x342283E4u, 0x15893F7Eu,
0x33968BB7u, 0x4C856F49u, 0x98573F68u, 0x16DDE443u, 0x813C3469u,
0x352A6556u, 0xD7988BC9u, 0x65E6ECA1u, 0x7EEE2551u, 0x77EBAE87u,
0xBDF5A53Au, 0x7F49F172u, 0xC06A7994u, 0xDB2FBCB8u, 0x343EA49Cu,
0xD143768Bu, 0x3E2D8753u, 0xA2EA09FCu, 0x467B5D0Bu, 0xCEB7F977u,
0x7119DC7Au, 0xDEFDA129u, 0x3F6EBC90u, 0x14E09A55u, 0x43C8B9D2u,
0xA707C426u, 0xFE372940u, 0x77591711u, 0xA6410F15u, 0xEACDE488u,
0x8B2C4DC6u, 0x8C2B11DDu, 0x9703CE5Bu, 0x0FAA28E3u, 0x13CCE32Du,
0xDCD68310u, 0x416F0B3Fu, 0x3BB4D68Eu, 0xF81F070Cu, 0xBDD89641u,
0x3915039Au, 0xF609E604u, 0x1C9DBB75u, 0x7ACD6A01u, 0xD4F462D2u,
0x125E66E0u, 0x0AD44FA7u, 0x4C3C90DEu, 0x27AD6982u, 0x0673640Cu,
0x65C03607u, 0xB05B7B87u, 0x97E01849u, 0xBA18BD33u, 0xDEF6041Bu,
0xE227F500u, 0x8A871E9Au, 0xCB120ACCu, 0x4B1B6336u, 0xEBDA42C6u,
0xFF166CA2u, 0x3A5E054Eu, 0x027207B6u, 0x04E3E645u, 0xAA95A0BCu,
0x77BFA4F4u, 0x3C95E0BEu, 0xD506A9D1u, 0x443EDFD4u, 0xD4E28BA1u,
0xA60BF66Eu, 0x46201E6Bu, 0xB2DE5570u, 0xF19F5DCCu, 0x73B6C636u,
0xDC83E7ECu, 0xAA333392u, 0x4EDB46C4u, 0xF64F937Fu,
};
/* -1 means not found */
static int
find_in_static_full (uint32_t nameval_hash, const char *name,
unsigned name_len, const char *val, unsigned val_len)
{
unsigned id;
id = nameval2id_plus_one[ (nameval_hash >> XXH_NAMEVAL_SHIFT)
& ((1 << XXH_NAMEVAL_WIDTH) - 1) ];
if (id == 0)
return -1;
--id;
if (static_table[id].name_len == name_len
&& static_table[id].val_len == val_len
&& memcmp(static_table[id].name, name, name_len) == 0
&& memcmp(static_table[id].val, val, val_len) == 0)
return id;
else
return -1;
}
#ifdef NDEBUG
static
#endif
int
lsqpack_find_in_static_headers (uint32_t name_hash, const char *name,
unsigned name_len)
{
unsigned id;
id = name2id_plus_one[ (name_hash >> XXH_NAME_SHIFT)
& ((1 << XXH_NAME_WIDTH) - 1) ];
if (id == 0)
return -1;
--id;
if (static_table[id].name_len == name_len
&& memcmp(static_table[id].name, name, name_len) == 0)
return id;
else
return -1;
}
static unsigned
lsqpack_val2len (uint64_t value, unsigned prefix_bits)
{
uint64_t mask = (1ULL << prefix_bits) - 1;
return 1
+ (value >= mask )
+ (value >= ((1ULL << 7) + mask))
+ (value >= ((1ULL << 14) + mask))
+ (value >= ((1ULL << 21) + mask))
+ (value >= ((1ULL << 28) + mask))
+ (value >= ((1ULL << 35) + mask))
+ (value >= ((1ULL << 42) + mask))
+ (value >= ((1ULL << 49) + mask))
+ (value >= ((1ULL << 56) + mask))
+ (value >= ((1ULL << 63) + mask))
;
}
unsigned char *
lsqpack_enc_int (unsigned char *dst, unsigned char *const end, uint64_t value,
unsigned prefix_bits)
{
unsigned char *const dst_orig = dst;
/* This function assumes that at least one byte is available */
assert(dst < end);
if (value < ((uint64_t)1 << prefix_bits) - 1)
*dst++ |= value;
else
{
*dst++ |= (1 << prefix_bits) - 1;
value -= (1 << prefix_bits) - 1;
while (value >= 128)
{
if (dst < end)
{
*dst++ = 0x80 | (unsigned char) value;
value >>= 7;
}
else
return dst_orig;
}
if (dst < end)
*dst++ = (unsigned char) value;
else
return dst_orig;
}
return dst;
}
static void
lsqpack_enc_int_nocheck (unsigned char *dst, uint64_t value,
unsigned prefix_bits)
{
if (value < ((uint64_t)1 << prefix_bits) - 1)
*dst++ |= value;
else
{
*dst++ |= (1 << prefix_bits) - 1;
value -= (1 << prefix_bits) - 1;
while (value >= 128)
{
*dst++ = 0x80 | (unsigned char) value;
value >>= 7;
}
*dst++ = (unsigned char) value;
}
}
int
lsqpack_enc_enc_str (unsigned prefix_bits, unsigned char *const dst,
size_t dst_len, const unsigned char *str, unsigned str_len)
{
unsigned char *p;
unsigned enc_size_bytes, len_size;
enc_size_bytes = qenc_enc_str_size(str, str_len);
if (enc_size_bytes < str_len)
{
len_size = lsqpack_val2len(enc_size_bytes, prefix_bits);
if (len_size + enc_size_bytes <= dst_len)
{
*dst &= ~((1 << (prefix_bits + 1)) - 1);
*dst |= 1 << prefix_bits;
lsqpack_enc_int_nocheck(dst, enc_size_bytes, prefix_bits);
p = qenc_huffman_enc(str, str + str_len, dst + len_size);
assert((unsigned) (p - dst) == len_size + enc_size_bytes);
return p - dst;
}
else
return -1;
}
else
{
len_size = lsqpack_val2len(str_len, prefix_bits);
if (len_size + str_len <= dst_len)
{
*dst &= ~((1 << (prefix_bits + 1)) - 1);
lsqpack_enc_int_nocheck(dst, str_len, prefix_bits);
memcpy(dst + len_size, str, str_len);
return len_size + str_len;
}
else
return -1;
}
}
static void
qenc_drop_oldest_entry (struct lsqpack_enc *enc)
{
struct lsqpack_enc_table_entry *entry;
unsigned buckno;
entry = STAILQ_FIRST(&enc->qpe_all_entries);
assert(entry);
E_DEBUG("drop entry %u (`%.*s': `%.*s'), nelem: %u; capacity: %u",
entry->ete_id, (int) entry->ete_name_len, ETE_NAME(entry),
(int) entry->ete_val_len, ETE_VALUE(entry), enc->qpe_nelem - 1,
enc->qpe_cur_bytes_used - ETE_SIZE(entry));
STAILQ_REMOVE_HEAD(&enc->qpe_all_entries, ete_next_all);
buckno = BUCKNO(enc->qpe_nbits, entry->ete_nameval_hash);
assert(entry == STAILQ_FIRST(&enc->qpe_buckets[buckno].by_nameval));
STAILQ_REMOVE_HEAD(&enc->qpe_buckets[buckno].by_nameval, ete_next_nameval);
buckno = BUCKNO(enc->qpe_nbits, entry->ete_name_hash);
assert(entry == STAILQ_FIRST(&enc->qpe_buckets[buckno].by_name));
STAILQ_REMOVE_HEAD(&enc->qpe_buckets[buckno].by_name, ete_next_name);
enc->qpe_dropped += ETE_SIZE(entry);
enc->qpe_cur_bytes_used -= ETE_SIZE(entry);
--enc->qpe_nelem;
free(entry);
}
static float
qenc_effective_fill (const struct lsqpack_enc *enc)
{
struct lsqpack_enc_table_entry *entry, *dup;
unsigned dups_size = 0;
assert(enc->qpe_cur_max_capacity);
STAILQ_FOREACH(entry, &enc->qpe_all_entries, ete_next_all)
for (dup = STAILQ_NEXT(entry, ete_next_all); dup;
dup = STAILQ_NEXT(dup, ete_next_all))
if (dup->ete_name_len == entry->ete_name_len &&
dup->ete_val_len == entry->ete_val_len &&
0 == memcmp(ETE_NAME(dup), ETE_NAME(entry),
dup->ete_name_len + dup->ete_val_len))
{
dups_size += ETE_SIZE(dup);
break;
}
return (float) (enc->qpe_cur_bytes_used - dups_size)
/ (float) enc->qpe_cur_max_capacity;
}
static void
update_ema (float *val, unsigned new)
{
if (*val)
*val = (new - *val) * 0.4 + *val;
else
*val = new;
}
static void
qenc_sample_table_size (struct lsqpack_enc *enc)
{
update_ema(&enc->qpe_table_nelem_ema, enc->qpe_nelem);
E_DEBUG("table size actual: %u; exponential moving average: %.3f",
enc->qpe_nelem, enc->qpe_table_nelem_ema);
}
static void
qenc_sample_header_count (struct lsqpack_enc *enc)
{
update_ema(&enc->qpe_header_count_ema,
enc->qpe_cur_header.n_hdr_added_to_hist);
E_DEBUG("header count actual: %u; exponential moving average: %.3f",
enc->qpe_cur_header.n_hdr_added_to_hist, enc->qpe_header_count_ema);
}
static void
qenc_remove_overflow_entries (struct lsqpack_enc *enc)
{
int dropped;
dropped = 0;
while (enc->qpe_cur_bytes_used > enc->qpe_cur_max_capacity)
{
qenc_drop_oldest_entry(enc);
++dropped;
}
if (enc->qpe_logger_ctx && enc->qpe_cur_max_capacity)
{
if (enc->qpe_flags & LSQPACK_ENC_USE_DUP)
E_DEBUG("fill: %.2f; effective fill: %.2f",
(float) enc->qpe_cur_bytes_used / (float) enc->qpe_cur_max_capacity,
qenc_effective_fill(enc));
else
E_DEBUG("fill: %.2f",
(float) enc->qpe_cur_bytes_used / (float) enc->qpe_cur_max_capacity);
}
/* It's important to sample only when entries have been dropped: that
* indicates that the table is being cycled through.
*/
if (dropped && enc->qpe_hist_els)
qenc_sample_table_size(enc);
}
static int
qenc_grow_tables (struct lsqpack_enc *enc)
{
struct lsqpack_double_enc_head *new_buckets, *new[2];
struct lsqpack_enc_table_entry *entry;
unsigned n, old_nbits;
int idx;
old_nbits = enc->qpe_nbits;
new_buckets = malloc(sizeof(enc->qpe_buckets[0])
* N_BUCKETS(old_nbits + 1));
if (!new_buckets)
return -1;
for (n = 0; n < N_BUCKETS(old_nbits); ++n)
{
new[0] = &new_buckets[n];
new[1] = &new_buckets[n + N_BUCKETS(old_nbits)];
STAILQ_INIT(&new[0]->by_name);
STAILQ_INIT(&new[1]->by_name);
STAILQ_INIT(&new[0]->by_nameval);
STAILQ_INIT(&new[1]->by_nameval);
while (entry = STAILQ_FIRST(&enc->qpe_buckets[n].by_name), entry != NULL)
{
STAILQ_REMOVE_HEAD(&enc->qpe_buckets[n].by_name, ete_next_name);
idx = (BUCKNO(old_nbits + 1, entry->ete_name_hash)
>> old_nbits) & 1;
STAILQ_INSERT_TAIL(&new[idx]->by_name, entry, ete_next_name);
}
while (entry = STAILQ_FIRST(&enc->qpe_buckets[n].by_nameval), entry != NULL)
{
STAILQ_REMOVE_HEAD(&enc->qpe_buckets[n].by_nameval,
ete_next_nameval);
idx = (BUCKNO(old_nbits + 1, entry->ete_nameval_hash)
>> old_nbits) & 1;
STAILQ_INSERT_TAIL(&new[idx]->by_nameval, entry,
ete_next_nameval);
}
}
free(enc->qpe_buckets);
enc->qpe_nbits = old_nbits + 1;
enc->qpe_buckets = new_buckets;
return 0;
}