-
Notifications
You must be signed in to change notification settings - Fork 3
/
qmin_enc.c
1990 lines (1701 loc) · 59 KB
/
qmin_enc.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
/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */
/*
* qmin_enc.c - QMIN encoder
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include "qmin_common.h"
#include "qmin_id_list.h"
#include "qmin_internal.h"
#include "qmin_enc.h"
enum enc_checkpoint_state
{
ECS_NEW,
ECS_PENDING,
ECS_LIVE,
ECS_DEAD,
};
struct enc_checkpoint
{
TAILQ_ENTRY(enc_checkpoint) ecp_next;
/* IDs of entries added the the dynamic table when this enc_checkpoint
* was in ECS_NEW state and those referenced when the checkpoint is in
* ECS_LIVE state:
*/
struct id_list ecp_entry_ids;
/* IDs of streams that triggered entry addition to the dynamic
* table when the checkpoit was in ECS_NEW state.
*
* When the state is ECS_LIVE or ECS_DEAD, this list also keeps a record
* of stream IDs that have taken out dynamic entry references from
* this enc_checkpoint.
*/
struct id_list ecp_stream_ids[2];
enum enc_checkpoint_state ecp_state;
};
static int
maybe_drop_checkpoints (struct qmin_enc *);
static unsigned
maybe_declare_dead_checkpoints (struct qmin_enc *);
static size_t
checkpoint_size (const struct enc_checkpoint *ckpoint, unsigned alt_max)
{
unsigned max_id = id_list_max(&ckpoint->ecp_entry_ids);
if (alt_max > max_id)
max_id = alt_max;
return QMIN_CKPOINT_OVERHEAD
+ (max_id - QMIN_STATIC_TABLE_N_ENTRIES - 1) / 8;
}
static struct enc_checkpoint *
enc_ckpoint_new (unsigned min_stream_id_server, unsigned min_stream_id_client)
{
struct enc_checkpoint *ckpoint;
ckpoint = calloc(1, sizeof(*ckpoint));
if (!ckpoint)
return NULL;
id_list_init(&ckpoint->ecp_entry_ids, QMIN_STATIC_TABLE_N_ENTRIES + 1);
id_list_init(&ckpoint->ecp_stream_ids[0], min_stream_id_server);
id_list_init(&ckpoint->ecp_stream_ids[1], min_stream_id_client);
ckpoint->ecp_state = ECS_NEW;
return ckpoint;
}
static void
enc_ckpoint_destroy (struct enc_checkpoint *ckpoint)
{
id_list_cleanup(&ckpoint->ecp_entry_ids);
id_list_cleanup(&ckpoint->ecp_stream_ids[0]);
id_list_cleanup(&ckpoint->ecp_stream_ids[1]);
free(ckpoint);
}
static size_t
enc_ckpoint_mem_used (const struct enc_checkpoint *ckpoint)
{
return sizeof(*ckpoint)
- sizeof(ckpoint->ecp_entry_ids)
+ id_list_mem_used(&ckpoint->ecp_entry_ids)
- sizeof(ckpoint->ecp_stream_ids[0])
+ id_list_mem_used(&ckpoint->ecp_stream_ids[0])
- sizeof(ckpoint->ecp_stream_ids[1])
+ id_list_mem_used(&ckpoint->ecp_stream_ids[1]);
}
#define ENC_HIST_MAX_ELEMS 1024
struct enc_hist
{
unsigned ehe_hashes[ENC_HIST_MAX_ELEMS];
unsigned ehe_hist_next;
bool ehe_wrapped;
};
enum enc_hist_add_st { EHA_ADDED, EHA_EXISTS, };
static enum enc_hist_add_st
enc_hist_add (struct enc_hist *hist, unsigned nameval_hash)
{
unsigned max, n;
max = hist->ehe_wrapped ? ENC_HIST_MAX_ELEMS : hist->ehe_hist_next;
for (n = 0; n < max; ++n)
if (hist->ehe_hashes[n] == nameval_hash)
return EHA_EXISTS;
if (!hist->ehe_wrapped && n == ENC_HIST_MAX_ELEMS)
hist->ehe_wrapped = true;
n = hist->ehe_hist_next++ % ENC_HIST_MAX_ELEMS;
hist->ehe_hashes[n] = nameval_hash;
return EHA_ADDED;
}
struct enc_table_entry;
TAILQ_HEAD(enc_head, enc_table_entry);
struct double_enc_head;
TAILQ_HEAD(checkpoint_head , enc_checkpoint);
struct qmin_enc
{
enum {
QME_TRACE = (1 << 0),
} qme_flags;
enum qmin_side qme_side;
/* List of all the dynamic entry IDs */
struct id_list qme_entry_ids;
struct {
struct id_list list;
/* All stream IDs at and below this value have been closed: */
unsigned low;
} qme_closed_stream_ids[2];
unsigned qme_max_opened_stream_id;
struct {
unsigned n_streams,
n_inserts,
added_size;
} qme_stats;
/* Dynamic table entries (struct enc_table_entry) live in two hash
* tables: name/value hash table and name hash table. These tables
* are the same size.
*/
struct double_enc_head *qme_buckets;
unsigned qme_nelem;
unsigned qme_nbits;
struct checkpoint_head qme_checkpoints;
unsigned qme_n_ckpoints;
unsigned qme_n_live_ckpoints;
/*
* In addition, the dynamic table entries are in array.
*/
struct {
struct enc_table_entry **arr;
unsigned count;
} qme_entries;
const struct qmin_ctl_out *qme_ctl_out;
const char *qme_idstr;
unsigned qme_max_capacity;
unsigned qme_cur_capacity;
size_t qme_bytes_out,
qme_bytes_in;
struct enc_hist qme_enc_hist;
};
#define TRACE(args...) do { \
if (enc->qme_flags & QME_TRACE) { \
fprintf(stderr, "ENC TRACE(%s): ", enc->qme_idstr); \
fprintf(stderr, args); \
fflush(stderr); \
} \
} while (0)
struct double_enc_head
{
struct enc_head by_name;
struct enc_head by_nameval;
};
struct enc_table_entry
{
/* An entry always lives on both lists */
TAILQ_ENTRY(enc_table_entry) ete_next_nameval,
ete_next_name;
unsigned ete_id;
unsigned ete_live_refcnt;
unsigned ete_total_refcnt;
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 N_BUCKETS(n_bits) (1U << (n_bits))
#define BUCKNO(n_bits, hash) ((hash) & (N_BUCKETS(n_bits) - 1))
struct qmin_enc *
qmin_enc_new (enum qmin_side side, unsigned max_capacity,
const struct qmin_ctl_out *ctl_out, const char *idstr)
{
struct qmin_enc *enc;
struct enc_checkpoint *enc_checkpoint;
struct double_enc_head *buckets;
const char *s;
unsigned nbits = 2;
unsigned i;
if (max_capacity < QMIN_CKPOINT_OVERHEAD)
{
errno = EINVAL;
return NULL;
}
enc = malloc(sizeof(*enc));
if (!enc)
return NULL;
enc_checkpoint = enc_ckpoint_new(0, 0);
if (!enc_checkpoint)
{
free(enc);
return NULL;
}
buckets = malloc(sizeof(buckets[0]) * N_BUCKETS(nbits));
if (!buckets)
{
enc_ckpoint_destroy(enc_checkpoint);
free(enc);
return NULL;
}
for (i = 0; i < N_BUCKETS(nbits); ++i)
{
TAILQ_INIT(&buckets[i].by_name);
TAILQ_INIT(&buckets[i].by_nameval);
}
memset(enc, 0, sizeof(*enc));
enc->qme_idstr = idstr ? idstr : "";
enc->qme_side = side;
enc->qme_ctl_out = ctl_out;
enc->qme_max_capacity = max_capacity;
enc->qme_cur_capacity = checkpoint_size(enc_checkpoint, 0);
enc->qme_buckets = buckets;
enc->qme_nbits = nbits;
enc->qme_nelem = 0;
enc->qme_entries.arr = NULL;
enc->qme_entries.count= 0;
id_list_init(&enc->qme_closed_stream_ids[0].list, 0);
id_list_init(&enc->qme_closed_stream_ids[1].list, 0);
id_list_init(&enc->qme_entry_ids, QMIN_STATIC_TABLE_N_ENTRIES + 1);
TAILQ_INIT(&enc->qme_checkpoints);
TAILQ_INSERT_HEAD(&enc->qme_checkpoints, enc_checkpoint, ecp_next);
enc->qme_n_ckpoints = 1;
enc->qme_n_live_ckpoints = 0;
s = getenv("QMIN_ENC_TRACE");
if (s && atoi(s))
enc->qme_flags |= QME_TRACE;
return enc;
}
void
qmin_enc_destroy (struct qmin_enc *enc)
{
struct enc_checkpoint *enc_checkpoint;
unsigned n;
while ((enc_checkpoint = TAILQ_FIRST(&enc->qme_checkpoints)))
{
TAILQ_REMOVE(&enc->qme_checkpoints, enc_checkpoint, ecp_next);
enc_ckpoint_destroy(enc_checkpoint);
}
for (n = 0; n < enc->qme_entries.count; ++n)
if (enc->qme_entries.arr[n])
free(enc->qme_entries.arr[n]);
id_list_cleanup(&enc->qme_closed_stream_ids[0].list);
id_list_cleanup(&enc->qme_closed_stream_ids[1].list);
id_list_cleanup(&enc->qme_entry_ids);
free(enc->qme_entries.arr);
free(enc->qme_buckets);
free(enc);
}
//not find return 0, otherwise return the index
static unsigned
qmin_enc_get_stx_tab_id (const char *name, unsigned name_len,
const char *val, unsigned val_len, bool *val_matched)
{
if (name_len < 3)
return 0;
*val_matched = false;
//check value first
int i = -1;
switch (*val)
{
case 'G':
i = 1;
break;
case 'P':
i = 2;
break;
case '/':
if (val_len == 1)
i = 3;
else if (val_len == 11)
i = 4;
break;
case 'h':
if (val_len == 4)
i = 5;
else if (val_len == 5)
i = 6;
break;
case '2':
if (val_len == 3)
{
switch (*(val + 2))
{
case '0':
i = 7;
break;
case '4':
i = 8;
break;
case '6':
i = 9;
break;
default:
break;
}
}
break;
case '3':
i = 10;
break;
case '4':
if (val_len == 3)
{
switch (*(val + 2))
{
case '0':
i = 11;
break;
case '4':
i = 12;
default:
break;
}
}
break;
case '5':
i = 13;
break;
case 'g':
i = 15;
break;
default:
break;
}
if (i > 0 && qmin_stx_tab[i].val_len == val_len
&& qmin_stx_tab[i].name_len == name_len
&& memcmp(val, qmin_stx_tab[i].val, val_len) == 0
&& memcmp(name, qmin_stx_tab[i].name, name_len) == 0)
{
*val_matched = true;
return i + 1;
}
//macth name only checking
i = -1;
switch (*name)
{
case ':':
switch (*(name + 1))
{
case 'a':
i = 0;
break;
case 'm':
i = 1;
break;
case 'p':
i = 3;
break;
case 's':
if (*(name + 2) == 'c') //:scheme
i = 5;
else
i = 7;
break;
default:
break;
}
break;
case 'a':
switch (name_len)
{
case 3:
i = 20; //age
break;
case 5:
i = 21; //allow
break;
case 6:
i = 18; //accept
break;
case 13:
if (*(name + 1) == 'u')
i = 22; //authorization
else
i = 17; //accept-ranges
break;
case 14:
i = 14; //accept-charset
break;
case 15:
if (*(name + 7) == 'l')
i = 16; //accept-language,
else
i = 15;// accept-encoding
break;
case 27:
i = 19;//access-control-allow-origin
break;
default:
break;
}
break;
case 'c':
switch (name_len)
{
case 6:
i = 31; //cookie
break;
case 12:
i = 30; //content-type
break;
case 13:
if (*(name + 1) == 'a')
i = 23; //cache-control
else
i = 29; //content-range
break;
case 14:
i = 27; //content-length
break;
case 16:
switch (*(name + 9))
{
case 'n':
i = 25 ;//content-encoding
break;
case 'a':
i = 26; //content-language
break;
case 'o':
i = 28; //content-location
default:
break;
}
break;
case 19:
i = 24; //content-disposition
break;
}
break;
case 'd':
i = 32 ;//date
break;
case 'e':
switch (name_len)
{
case 4:
i = 33; //etag
break;
case 6:
i = 34;
break;
case 7:
i = 35;
break;
default:
break;
}
break;
case 'f':
i = 36; //from
break;
case 'h':
i = 37; //host
break;
case 'i':
switch (name_len)
{
case 8:
if (*(name + 3) == 'm')
i = 38; //if-match
else
i = 41; //if-range
break;
case 13:
i = 40; //if-none-match
break;
case 17:
i = 39; //if-modified-since
break;
case 19:
i = 42; //if-unmodified-since
break;
default:
break;
}
break;
case 'l':
switch (name_len)
{
case 4:
i = 44; //link
break;
case 8:
i = 45; //location
break;
case 13:
i = 43; //last-modified
break;
default:
break;
}
break;
case 'm':
i = 46; //max-forwards
break;
case 'p':
if (name_len == 18)
i = 47; //proxy-authenticate
else
i = 48; //proxy-authorization
break;
case 'r':
if (name_len >= 5)
{
switch (*(name + 4))
{
case 'e':
if (name_len == 5)
i = 49; //range
else
i = 51; //refresh
break;
case 'r':
i = 50; //referer
break;
case 'y':
i = 52; //retry-after
break;
default:
break;
}
}
break;
case 's':
switch (name_len)
{
case 6:
i = 53; //server
break;
case 10:
i = 54; //set-cookie
break;
case 25:
i = 55; //strict-transport-security
break;
default:
break;
}
break;
case 't':
i = 56;//transfer-encoding
break;
case 'u':
i = 57; //user-agent
break;
case 'v':
if (name_len == 4)
i = 58;
else
i = 59;
break;
case 'w':
i = 60;
break;
default:
break;
}
if (i >= 0
&& qmin_stx_tab[i].name_len == name_len
&& memcmp(name, qmin_stx_tab[i].name, name_len) == 0)
return i + 1;
return 0;
}
#define DJB2_INIT 5381
/* There are better hashes, but this is good enough for this proof-of-concept.
* And it is five lines of code.
*/
static unsigned
djb2 (unsigned hash, const void *data, size_t sz)
{
const unsigned char *c = data;
const unsigned char *const end = c + sz;
for (; c < end; ++c)
hash = ((hash << 5) + hash) + *c;
return hash;
}
enum
{
FIBIT_LIVE,
FIBIT_PENDING,
FIBIT_NEW,
};
enum found_in
{
FI_LIVE = 1 << FIBIT_LIVE,
FI_PENDING = 1 << FIBIT_PENDING,
FI_NEW = 1 << FIBIT_NEW,
};
struct entry_search_result {
unsigned esr_entry_id; /* If 0, entry is not found */
unsigned esr_name_hash,
esr_nameval_hash;
bool esr_val_matched;
enum found_in
esr_found_in;
};
static struct entry_search_result
qmin_enc_find_entry (struct qmin_enc *enc, const char *name,
unsigned name_len, const char *value, unsigned value_len)
{
struct enc_checkpoint *const new_ckpoint = TAILQ_FIRST(&enc->qme_checkpoints);
struct enc_checkpoint *pend_ckpoint;
struct enc_table_entry *entry;
struct entry_search_result r;
unsigned buckno;
pend_ckpoint = TAILQ_NEXT(new_ckpoint, ecp_next);
if (pend_ckpoint && pend_ckpoint->ecp_state != ECS_PENDING)
pend_ckpoint = NULL;
memset(&r, 0, sizeof(r));
/* First, look for a match in the static table: */
r.esr_entry_id = qmin_enc_get_stx_tab_id(name, name_len, value,
value_len, &r.esr_val_matched);
if (r.esr_entry_id > 0 && r.esr_val_matched)
{
r.esr_found_in = FI_LIVE;
return r;
}
/* Search by name and value: */
r.esr_name_hash = djb2(DJB2_INIT, &name_len, sizeof(name_len));
r.esr_name_hash = djb2(r.esr_name_hash, name, name_len);
r.esr_nameval_hash = djb2(r.esr_name_hash, &value_len, sizeof(value_len));
r.esr_nameval_hash = djb2(r.esr_nameval_hash, value, value_len);
buckno = BUCKNO(enc->qme_nbits, r.esr_nameval_hash);
TAILQ_FOREACH(entry, &enc->qme_buckets[buckno].by_nameval, ete_next_nameval)
if (r.esr_nameval_hash == entry->ete_nameval_hash
&& name_len == entry->ete_name_len
&& value_len == entry->ete_val_len
&& 0 == memcmp(name, ETE_NAME(entry), name_len)
&& 0 == memcmp(value, ETE_VALUE(entry), value_len)
&& (entry->ete_live_refcnt > 0
|| (pend_ckpoint &&
id_list_exists(&pend_ckpoint->ecp_entry_ids, entry->ete_id))
|| id_list_exists(&new_ckpoint->ecp_entry_ids, entry->ete_id)))
{
r.esr_entry_id = entry->ete_id;
r.esr_val_matched = true;
r.esr_found_in =
((entry->ete_live_refcnt > 0) << FIBIT_LIVE)
|
((pend_ckpoint &&
id_list_exists(&pend_ckpoint->ecp_entry_ids, entry->ete_id)) << FIBIT_PENDING)
|
(id_list_exists(&new_ckpoint->ecp_entry_ids, entry->ete_id) << FIBIT_NEW);
return r;
}
/* Name/value match is not found, but if the caller found a matching
* static table entry, no need to continue to search:
*/
if (r.esr_entry_id > 0)
{
r.esr_found_in = FI_LIVE;
return r;
}
/* Search by name only: */
buckno = BUCKNO(enc->qme_nbits, r.esr_name_hash);
TAILQ_FOREACH(entry, &enc->qme_buckets[buckno].by_name, ete_next_name)
if (r.esr_name_hash == entry->ete_name_hash
&& name_len == entry->ete_name_len
&& 0 == memcmp(name, ETE_NAME(entry), name_len)
&& (entry->ete_live_refcnt > 0
|| (pend_ckpoint &&
id_list_exists(&pend_ckpoint->ecp_entry_ids, entry->ete_id))
|| id_list_exists(&new_ckpoint->ecp_entry_ids, entry->ete_id)))
{
r.esr_entry_id = entry->ete_id;
r.esr_val_matched = false;
r.esr_found_in =
((entry->ete_live_refcnt > 0) << FIBIT_LIVE)
|
((pend_ckpoint &&
id_list_exists(&pend_ckpoint->ecp_entry_ids, entry->ete_id)) << FIBIT_PENDING)
|
(id_list_exists(&new_ckpoint->ecp_entry_ids, entry->ete_id) << FIBIT_NEW);
return r;
}
r.esr_entry_id = 0;
return r;
}
static int
qmin_huffman_enc (const unsigned char *src, const unsigned char *const src_end,
unsigned char *dst, int dst_len)
{
const unsigned char *p_src = src;
unsigned char *p_dst = dst;
unsigned char *dst_end = p_dst + dst_len;
uint64_t bits = 0;
int bits_left = 40;
struct qmin_huff_encode cur_enc_code;
assert(dst_len > 0);
while (p_src != src_end)
{
cur_enc_code = qmin_huff_encode_tables[(int) *p_src++];
assert(bits_left >= cur_enc_code.bits); // (possible negative shift, undefined behavior)
bits |= (uint64_t)cur_enc_code.code << (bits_left - cur_enc_code.bits);
bits_left -= cur_enc_code.bits;
while (bits_left <= 32)
{
*p_dst++ = bits >> 32;
bits <<= 8;
bits_left += 8;
if (p_dst == dst_end)
return -1; //dst does not have enough space
}
}
if (bits_left != 40)
{
assert(bits_left < 40 && bits_left > 0);
bits |= ((uint64_t)1 << bits_left) - 1;
*p_dst++ = bits >> 32;
}
return p_dst - dst;
}
static int
qmin_enc_enc_str (unsigned char *const dst, size_t dst_len,
const unsigned char *str, unsigned str_len)
{
unsigned char size_buf[4];
unsigned char *p;
unsigned size_len;
int rc;
if (dst_len > 1)
/* We guess that the string size fits into a single byte -- meaning
* compressed string of size 126 and smaller -- which is the normal
* case. Thus, we immediately write compressed string to the output
* buffer. If our guess is not correct, we fix it later.
*/
rc = qmin_huffman_enc(str, str + str_len, dst + 1, dst_len - 1);
else if (dst_len == 1)
/* Here, the call can only succeed if the string to encode is empty. */
rc = 0;
else
return -1;
/*
* Check if need huffman encoding or not
* Comment: (size_t)rc <= str_len = means if same length, still use Huffman
* ^
*/
if (rc > 0 && (size_t)rc <= str_len)
{
if (rc < 127)
{
*dst = 0x80 | rc;
return 1 + rc;
}
size_buf[0] = 0x80;
str_len = rc;
str = dst + 1;
}
else if (str_len <= dst_len - 1)
{
if (str_len < 127)
{
*dst = str_len;
memcpy(dst + 1, str, str_len);
return 1 + str_len;
}
size_buf[0] = 0x00;
}
else
return -1;
/* The guess of one-byte size was incorrect. Perform necessary
* adjustments.
*/
p = qmin_encode_int(size_buf, size_buf + sizeof(size_buf), str_len, 7);
if (p == size_buf)
return -1;
size_len = p - size_buf;
assert(size_len > 1);
/* Check if there is enough room in the output buffer for both
* encoded size and the string.
*/
if (size_len + str_len > dst_len)
return -1;
memmove(dst + size_len, str, str_len);
memcpy(dst, size_buf, size_len);
return size_len + str_len;
}
static int
qmin_enc_grow_tables (struct qmin_enc *enc)
{
struct double_enc_head *new_buckets, *new[2];
struct enc_table_entry *entry;
unsigned n, old_nbits;
int idx;
old_nbits = enc->qme_nbits;
new_buckets = malloc(sizeof(enc->qme_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)];
TAILQ_INIT(&new[0]->by_name);
TAILQ_INIT(&new[1]->by_name);
TAILQ_INIT(&new[0]->by_nameval);
TAILQ_INIT(&new[1]->by_nameval);
while ((entry = TAILQ_FIRST(&enc->qme_buckets[n].by_name)))
{
TAILQ_REMOVE(&enc->qme_buckets[n].by_name, entry, ete_next_name);
idx = (BUCKNO(old_nbits + 1, entry->ete_name_hash) >> old_nbits) & 1;
TAILQ_INSERT_TAIL(&new[idx]->by_name, entry, ete_next_name);
}
while ((entry = TAILQ_FIRST(&enc->qme_buckets[n].by_nameval)))
{
TAILQ_REMOVE(&enc->qme_buckets[n].by_nameval, entry, ete_next_nameval);
idx = (BUCKNO(old_nbits + 1, entry->ete_nameval_hash) >> old_nbits) & 1;
TAILQ_INSERT_TAIL(&new[idx]->by_nameval, entry, ete_next_nameval);
}
}
free(enc->qme_buckets);
enc->qme_nbits = old_nbits + 1;
enc->qme_buckets = new_buckets;
return 0;
}
static const struct enc_table_entry *
qmin_enc_push_entry (struct qmin_enc *enc, unsigned stream_id, const char *name,
unsigned name_len, const char *value, unsigned value_len)
{
struct enc_checkpoint *const new_ckpoint = TAILQ_FIRST(&enc->qme_checkpoints);
unsigned name_hash, nameval_hash, buckno, new_entry_id, arr_idx;
struct enc_table_entry *entry;
enum id_list_add_st add_st;
size_t size;
assert(new_ckpoint->ecp_state == ECS_NEW);
add_st = id_list_add(&new_ckpoint->ecp_stream_ids[ stream_id & 1 ],
stream_id >> 1);
if (add_st == ILA_ERROR)
return NULL;
/* See SPEC, Section 4.1.1 */
new_entry_id = id_list_min_unused(&enc->qme_entry_ids);
assert(new_entry_id >= QMIN_STATIC_TABLE_N_ENTRIES + 1);
arr_idx = new_entry_id - QMIN_STATIC_TABLE_N_ENTRIES - 1;
if (arr_idx >= enc->qme_entries.count)
{
struct enc_table_entry **new_entries;
unsigned count;
if (enc->qme_entries.count)
count = enc->qme_entries.count * 2;
else
count = 8;
new_entries = realloc(enc->qme_entries.arr,
sizeof(enc->qme_entries.arr[0]) * count);
if (!new_entries)
return NULL;
memset(new_entries + enc->qme_entries.count, 0,
sizeof(new_entries[0]) * (count - enc->qme_entries.count));
enc->qme_entries.arr = new_entries;
enc->qme_entries.count = count;
}
if (enc->qme_nelem >= N_BUCKETS(enc->qme_nbits) / 2
&& 0 != qmin_enc_grow_tables(enc))
{
return NULL;
}
size = sizeof(*entry) + name_len + value_len;
entry = malloc(size);
if (!entry)
return NULL;
assert(!enc->qme_entries.arr[arr_idx]);
enc->qme_entries.arr[arr_idx] = entry;
add_st = id_list_add(&enc->qme_entry_ids, new_entry_id);
assert(add_st != ILA_EXISTS);