Coverage Report

Created: 2026-12-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_hd.c
Line
Count
Source
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2013 Tatsuhiro Tsujikawa
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
#include "nghttp2_hd.h"
26
27
#include <string.h>
28
#include <assert.h>
29
#include <stdio.h>
30
31
#include "nghttp2_helper.h"
32
#include "nghttp2_int.h"
33
#include "nghttp2_debug.h"
34
35
/* Make scalar initialization form of nghttp2_hd_entry */
36
#define MAKE_STATIC_ENT(N, V, T, H)                                            \
37
  {                                                                            \
38
    .name =                                                                    \
39
      {                                                                        \
40
        .base = (uint8_t *)(N),                                                \
41
        .len = nghttp2_strlen_lit((N)),                                        \
42
        .ref = -1,                                                             \
43
      },                                                                       \
44
    .value =                                                                   \
45
      {                                                                        \
46
        .base = (uint8_t *)(V),                                                \
47
        .len = nghttp2_strlen_lit((V)),                                        \
48
        .ref = -1,                                                             \
49
      },                                                                       \
50
    .cnv =                                                                     \
51
      {                                                                        \
52
        .name = (uint8_t *)(N),                                                \
53
        .value = (uint8_t *)(V),                                               \
54
        .namelen = nghttp2_strlen_lit((N)),                                    \
55
        .valuelen = nghttp2_strlen_lit((V)),                                   \
56
      },                                                                       \
57
    .token = T,                                                                \
58
    .hash = H,                                                                 \
59
  }
60
61
/* Generated by mkstatictbl.py */
62
/* 3rd parameter is nghttp2_token value for header field name.  We use
63
   first enum value if same header names are repeated (e.g.,
64
   :status). */
65
static const nghttp2_hd_static_entry static_table[] = {
66
  MAKE_STATIC_ENT(":authority", "", 0, 3153725150u),
67
  MAKE_STATIC_ENT(":method", "GET", 1, 695666056u),
68
  MAKE_STATIC_ENT(":method", "POST", 1, 695666056u),
69
  MAKE_STATIC_ENT(":path", "/", 3, 3292848686u),
70
  MAKE_STATIC_ENT(":path", "/index.html", 3, 3292848686u),
71
  MAKE_STATIC_ENT(":scheme", "http", 5, 2510477674u),
72
  MAKE_STATIC_ENT(":scheme", "https", 5, 2510477674u),
73
  MAKE_STATIC_ENT(":status", "200", 7, 4000288983u),
74
  MAKE_STATIC_ENT(":status", "204", 7, 4000288983u),
75
  MAKE_STATIC_ENT(":status", "206", 7, 4000288983u),
76
  MAKE_STATIC_ENT(":status", "304", 7, 4000288983u),
77
  MAKE_STATIC_ENT(":status", "400", 7, 4000288983u),
78
  MAKE_STATIC_ENT(":status", "404", 7, 4000288983u),
79
  MAKE_STATIC_ENT(":status", "500", 7, 4000288983u),
80
  MAKE_STATIC_ENT("accept-charset", "", 14, 3664010344u),
81
  MAKE_STATIC_ENT("accept-encoding", "gzip, deflate", 15, 3379649177u),
82
  MAKE_STATIC_ENT("accept-language", "", 16, 1979086614u),
83
  MAKE_STATIC_ENT("accept-ranges", "", 17, 1713753958u),
84
  MAKE_STATIC_ENT("accept", "", 18, 136609321u),
85
  MAKE_STATIC_ENT("access-control-allow-origin", "", 19, 2710797292u),
86
  MAKE_STATIC_ENT("age", "", 20, 742476188u),
87
  MAKE_STATIC_ENT("allow", "", 21, 2930878514u),
88
  MAKE_STATIC_ENT("authorization", "", 22, 2436257726u),
89
  MAKE_STATIC_ENT("cache-control", "", 23, 1355326669u),
90
  MAKE_STATIC_ENT("content-disposition", "", 24, 3889184348u),
91
  MAKE_STATIC_ENT("content-encoding", "", 25, 65203592u),
92
  MAKE_STATIC_ENT("content-language", "", 26, 24973587u),
93
  MAKE_STATIC_ENT("content-length", "", 27, 1308181789u),
94
  MAKE_STATIC_ENT("content-location", "", 28, 2302364718u),
95
  MAKE_STATIC_ENT("content-range", "", 29, 3555523146u),
96
  MAKE_STATIC_ENT("content-type", "", 30, 4244048277u),
97
  MAKE_STATIC_ENT("cookie", "", 31, 2007449791u),
98
  MAKE_STATIC_ENT("date", "", 32, 3564297305u),
99
  MAKE_STATIC_ENT("etag", "", 33, 113792960u),
100
  MAKE_STATIC_ENT("expect", "", 34, 2530896728u),
101
  MAKE_STATIC_ENT("expires", "", 35, 1049544579u),
102
  MAKE_STATIC_ENT("from", "", 36, 2513272949u),
103
  MAKE_STATIC_ENT("host", "", 37, 2952701295u),
104
  MAKE_STATIC_ENT("if-match", "", 38, 3597694698u),
105
  MAKE_STATIC_ENT("if-modified-since", "", 39, 2213050793u),
106
  MAKE_STATIC_ENT("if-none-match", "", 40, 2536202615u),
107
  MAKE_STATIC_ENT("if-range", "", 41, 2340978238u),
108
  MAKE_STATIC_ENT("if-unmodified-since", "", 42, 3794814858u),
109
  MAKE_STATIC_ENT("last-modified", "", 43, 3226950251u),
110
  MAKE_STATIC_ENT("link", "", 44, 232457833u),
111
  MAKE_STATIC_ENT("location", "", 45, 200649126u),
112
  MAKE_STATIC_ENT("max-forwards", "", 46, 1826162134u),
113
  MAKE_STATIC_ENT("proxy-authenticate", "", 47, 2709445359u),
114
  MAKE_STATIC_ENT("proxy-authorization", "", 48, 2686392507u),
115
  MAKE_STATIC_ENT("range", "", 49, 4208725202u),
116
  MAKE_STATIC_ENT("referer", "", 50, 3969579366u),
117
  MAKE_STATIC_ENT("refresh", "", 51, 3572655668u),
118
  MAKE_STATIC_ENT("retry-after", "", 52, 3336180598u),
119
  MAKE_STATIC_ENT("server", "", 53, 1085029842u),
120
  MAKE_STATIC_ENT("set-cookie", "", 54, 1848371000u),
121
  MAKE_STATIC_ENT("strict-transport-security", "", 55, 4138147361u),
122
  MAKE_STATIC_ENT("transfer-encoding", "", 56, 3719590988u),
123
  MAKE_STATIC_ENT("user-agent", "", 57, 606444526u),
124
  MAKE_STATIC_ENT("vary", "", 58, 1085005381u),
125
  MAKE_STATIC_ENT("via", "", 59, 1762798611u),
126
  MAKE_STATIC_ENT("www-authenticate", "", 60, 779865858u),
127
};
128
129
13.6k
static int memeq(const void *s1, const void *s2, size_t n) {
130
13.6k
  return memcmp(s1, s2, n) == 0;
131
13.6k
}
132
133
/*
134
 * This function was generated by genlibtokenlookup.py.  Inspired by
135
 * h2o header lookup.  https://github.com/h2o/h2o
136
 */
137
16.7k
static int32_t lookup_token(const uint8_t *name, size_t namelen) {
138
16.7k
  switch (namelen) {
139
760
  case 2:
140
760
    switch (name[1]) {
141
554
    case 'e':
142
554
      if (memeq("t", name, 1)) {
143
342
        return NGHTTP2_TOKEN_TE;
144
342
      }
145
212
      break;
146
760
    }
147
418
    break;
148
1.07k
  case 3:
149
1.07k
    switch (name[2]) {
150
440
    case 'a':
151
440
      if (memeq("vi", name, 2)) {
152
198
        return NGHTTP2_TOKEN_VIA;
153
198
      }
154
242
      break;
155
428
    case 'e':
156
428
      if (memeq("ag", name, 2)) {
157
195
        return NGHTTP2_TOKEN_AGE;
158
195
      }
159
233
      break;
160
1.07k
    }
161
679
    break;
162
2.77k
  case 4:
163
2.77k
    switch (name[3]) {
164
436
    case 'e':
165
436
      if (memeq("dat", name, 3)) {
166
196
        return NGHTTP2_TOKEN_DATE;
167
196
      }
168
240
      break;
169
439
    case 'g':
170
439
      if (memeq("eta", name, 3)) {
171
196
        return NGHTTP2_TOKEN_ETAG;
172
196
      }
173
243
      break;
174
437
    case 'k':
175
437
      if (memeq("lin", name, 3)) {
176
194
        return NGHTTP2_TOKEN_LINK;
177
194
      }
178
243
      break;
179
471
    case 'm':
180
471
      if (memeq("fro", name, 3)) {
181
198
        return NGHTTP2_TOKEN_FROM;
182
198
      }
183
273
      break;
184
398
    case 't':
185
398
      if (memeq("hos", name, 3)) {
186
194
        return NGHTTP2_TOKEN_HOST;
187
194
      }
188
204
      break;
189
400
    case 'y':
190
400
      if (memeq("var", name, 3)) {
191
194
        return NGHTTP2_TOKEN_VARY;
192
194
      }
193
206
      break;
194
2.77k
    }
195
1.60k
    break;
196
1.60k
  case 5:
197
1.00k
    switch (name[4]) {
198
267
    case 'e':
199
267
      if (memeq("rang", name, 4)) {
200
66
        return NGHTTP2_TOKEN_RANGE;
201
66
      }
202
201
      break;
203
274
    case 'h':
204
274
      if (memeq(":pat", name, 4)) {
205
66
        return NGHTTP2_TOKEN__PATH;
206
66
      }
207
208
      break;
208
264
    case 'w':
209
264
      if (memeq("allo", name, 4)) {
210
66
        return NGHTTP2_TOKEN_ALLOW;
211
66
      }
212
198
      break;
213
1.00k
    }
214
806
    break;
215
954
  case 6:
216
954
    switch (name[5]) {
217
146
    case 'e':
218
146
      if (memeq("cooki", name, 5)) {
219
66
        return NGHTTP2_TOKEN_COOKIE;
220
66
      }
221
80
      break;
222
268
    case 'r':
223
268
      if (memeq("serve", name, 5)) {
224
66
        return NGHTTP2_TOKEN_SERVER;
225
66
      }
226
202
      break;
227
346
    case 't':
228
346
      if (memeq("accep", name, 5)) {
229
66
        return NGHTTP2_TOKEN_ACCEPT;
230
66
      }
231
280
      if (memeq("expec", name, 5)) {
232
66
        return NGHTTP2_TOKEN_EXPECT;
233
66
      }
234
214
      break;
235
954
    }
236
690
    break;
237
935
  case 7:
238
935
    switch (name[6]) {
239
145
    case 'd':
240
145
      if (memeq(":metho", name, 6)) {
241
66
        return NGHTTP2_TOKEN__METHOD;
242
66
      }
243
79
      break;
244
235
    case 'e':
245
235
      if (memeq(":schem", name, 6)) {
246
72
        return NGHTTP2_TOKEN__SCHEME;
247
72
      }
248
163
      if (memeq("upgrad", name, 6)) {
249
72
        return NGHTTP2_TOKEN_UPGRADE;
250
72
      }
251
91
      break;
252
141
    case 'h':
253
141
      if (memeq("refres", name, 6)) {
254
67
        return NGHTTP2_TOKEN_REFRESH;
255
67
      }
256
74
      break;
257
142
    case 'r':
258
142
      if (memeq("refere", name, 6)) {
259
70
        return NGHTTP2_TOKEN_REFERER;
260
70
      }
261
72
      break;
262
202
    case 's':
263
202
      if (memeq(":statu", name, 6)) {
264
66
        return NGHTTP2_TOKEN__STATUS;
265
66
      }
266
136
      if (memeq("expire", name, 6)) {
267
66
        return NGHTTP2_TOKEN_EXPIRES;
268
66
      }
269
70
      break;
270
935
    }
271
456
    break;
272
1.73k
  case 8:
273
1.73k
    switch (name[7]) {
274
141
    case 'e':
275
141
      if (memeq("if-rang", name, 7)) {
276
66
        return NGHTTP2_TOKEN_IF_RANGE;
277
66
      }
278
75
      break;
279
147
    case 'h':
280
147
      if (memeq("if-matc", name, 7)) {
281
66
        return NGHTTP2_TOKEN_IF_MATCH;
282
66
      }
283
81
      break;
284
147
    case 'n':
285
147
      if (memeq("locatio", name, 7)) {
286
66
        return NGHTTP2_TOKEN_LOCATION;
287
66
      }
288
81
      break;
289
1.22k
    case 'y':
290
1.22k
      if (memeq("priorit", name, 7)) {
291
1.11k
        return NGHTTP2_TOKEN_PRIORITY;
292
1.11k
      }
293
112
      break;
294
1.73k
    }
295
425
    break;
296
425
  case 9:
297
207
    switch (name[8]) {
298
140
    case 'l':
299
140
      if (memeq(":protoco", name, 8)) {
300
72
        return NGHTTP2_TOKEN__PROTOCOL;
301
72
      }
302
68
      break;
303
207
    }
304
135
    break;
305
726
  case 10:
306
726
    switch (name[9]) {
307
215
    case 'e':
308
215
      if (memeq("keep-aliv", name, 9)) {
309
72
        return NGHTTP2_TOKEN_KEEP_ALIVE;
310
72
      }
311
143
      if (memeq("set-cooki", name, 9)) {
312
66
        return NGHTTP2_TOKEN_SET_COOKIE;
313
66
      }
314
77
      break;
315
155
    case 'n':
316
155
      if (memeq("connectio", name, 9)) {
317
72
        return NGHTTP2_TOKEN_CONNECTION;
318
72
      }
319
83
      break;
320
147
    case 't':
321
147
      if (memeq("user-agen", name, 9)) {
322
66
        return NGHTTP2_TOKEN_USER_AGENT;
323
66
      }
324
81
      break;
325
140
    case 'y':
326
140
      if (memeq(":authorit", name, 9)) {
327
66
        return NGHTTP2_TOKEN__AUTHORITY;
328
66
      }
329
74
      break;
330
726
    }
331
384
    break;
332
384
  case 11:
333
222
    switch (name[10]) {
334
147
    case 'r':
335
147
      if (memeq("retry-afte", name, 10)) {
336
66
        return NGHTTP2_TOKEN_RETRY_AFTER;
337
66
      }
338
81
      break;
339
222
    }
340
156
    break;
341
355
  case 12:
342
355
    switch (name[11]) {
343
144
    case 'e':
344
144
      if (memeq("content-typ", name, 11)) {
345
66
        return NGHTTP2_TOKEN_CONTENT_TYPE;
346
66
      }
347
78
      break;
348
143
    case 's':
349
143
      if (memeq("max-forward", name, 11)) {
350
66
        return NGHTTP2_TOKEN_MAX_FORWARDS;
351
66
      }
352
77
      break;
353
355
    }
354
223
    break;
355
979
  case 13:
356
979
    switch (name[12]) {
357
150
    case 'd':
358
150
      if (memeq("last-modifie", name, 12)) {
359
66
        return NGHTTP2_TOKEN_LAST_MODIFIED;
360
66
      }
361
84
      break;
362
143
    case 'e':
363
143
      if (memeq("content-rang", name, 12)) {
364
67
        return NGHTTP2_TOKEN_CONTENT_RANGE;
365
67
      }
366
76
      break;
367
147
    case 'h':
368
147
      if (memeq("if-none-matc", name, 12)) {
369
66
        return NGHTTP2_TOKEN_IF_NONE_MATCH;
370
66
      }
371
81
      break;
372
143
    case 'l':
373
143
      if (memeq("cache-contro", name, 12)) {
374
66
        return NGHTTP2_TOKEN_CACHE_CONTROL;
375
66
      }
376
77
      break;
377
145
    case 'n':
378
145
      if (memeq("authorizatio", name, 12)) {
379
70
        return NGHTTP2_TOKEN_AUTHORIZATION;
380
70
      }
381
75
      break;
382
180
    case 's':
383
180
      if (memeq("accept-range", name, 12)) {
384
88
        return NGHTTP2_TOKEN_ACCEPT_RANGES;
385
88
      }
386
92
      break;
387
979
    }
388
556
    break;
389
556
  case 14:
390
351
    switch (name[13]) {
391
145
    case 'h':
392
145
      if (memeq("content-lengt", name, 13)) {
393
66
        return NGHTTP2_TOKEN_CONTENT_LENGTH;
394
66
      }
395
79
      break;
396
140
    case 't':
397
140
      if (memeq("accept-charse", name, 13)) {
398
66
        return NGHTTP2_TOKEN_ACCEPT_CHARSET;
399
66
      }
400
74
      break;
401
351
    }
402
219
    break;
403
364
  case 15:
404
364
    switch (name[14]) {
405
150
    case 'e':
406
150
      if (memeq("accept-languag", name, 14)) {
407
67
        return NGHTTP2_TOKEN_ACCEPT_LANGUAGE;
408
67
      }
409
83
      break;
410
148
    case 'g':
411
148
      if (memeq("accept-encodin", name, 14)) {
412
66
        return NGHTTP2_TOKEN_ACCEPT_ENCODING;
413
66
      }
414
82
      break;
415
364
    }
416
231
    break;
417
662
  case 16:
418
662
    switch (name[15]) {
419
223
    case 'e':
420
223
      if (memeq("content-languag", name, 15)) {
421
67
        return NGHTTP2_TOKEN_CONTENT_LANGUAGE;
422
67
      }
423
156
      if (memeq("www-authenticat", name, 15)) {
424
66
        return NGHTTP2_TOKEN_WWW_AUTHENTICATE;
425
66
      }
426
90
      break;
427
145
    case 'g':
428
145
      if (memeq("content-encodin", name, 15)) {
429
67
        return NGHTTP2_TOKEN_CONTENT_ENCODING;
430
67
      }
431
78
      break;
432
226
    case 'n':
433
226
      if (memeq("content-locatio", name, 15)) {
434
66
        return NGHTTP2_TOKEN_CONTENT_LOCATION;
435
66
      }
436
160
      if (memeq("proxy-connectio", name, 15)) {
437
72
        return NGHTTP2_TOKEN_PROXY_CONNECTION;
438
72
      }
439
88
      break;
440
662
    }
441
324
    break;
442
353
  case 17:
443
353
    switch (name[16]) {
444
146
    case 'e':
445
146
      if (memeq("if-modified-sinc", name, 16)) {
446
68
        return NGHTTP2_TOKEN_IF_MODIFIED_SINCE;
447
68
      }
448
78
      break;
449
140
    case 'g':
450
140
      if (memeq("transfer-encodin", name, 16)) {
451
66
        return NGHTTP2_TOKEN_TRANSFER_ENCODING;
452
66
      }
453
74
      break;
454
353
    }
455
219
    break;
456
224
  case 18:
457
224
    switch (name[17]) {
458
145
    case 'e':
459
145
      if (memeq("proxy-authenticat", name, 17)) {
460
67
        return NGHTTP2_TOKEN_PROXY_AUTHENTICATE;
461
67
      }
462
78
      break;
463
224
    }
464
157
    break;
465
426
  case 19:
466
426
    switch (name[18]) {
467
144
    case 'e':
468
144
      if (memeq("if-unmodified-sinc", name, 18)) {
469
66
        return NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE;
470
66
      }
471
78
      break;
472
214
    case 'n':
473
214
      if (memeq("content-dispositio", name, 18)) {
474
68
        return NGHTTP2_TOKEN_CONTENT_DISPOSITION;
475
68
      }
476
146
      if (memeq("proxy-authorizatio", name, 18)) {
477
66
        return NGHTTP2_TOKEN_PROXY_AUTHORIZATION;
478
66
      }
479
80
      break;
480
426
    }
481
226
    break;
482
226
  case 25:
483
211
    switch (name[24]) {
484
142
    case 'y':
485
142
      if (memeq("strict-transport-securit", name, 24)) {
486
66
        return NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY;
487
66
      }
488
76
      break;
489
211
    }
490
145
    break;
491
219
  case 27:
492
219
    switch (name[26]) {
493
145
    case 'n':
494
145
      if (memeq("access-control-allow-origi", name, 26)) {
495
66
        return NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN;
496
66
      }
497
79
      break;
498
219
    }
499
153
    break;
500
16.7k
  }
501
10.3k
  return -1;
502
16.7k
}
503
504
12.3k
void nghttp2_hd_entry_init(nghttp2_hd_entry *ent, nghttp2_hd_nv *nv) {
505
12.3k
  *ent = (nghttp2_hd_entry){
506
12.3k
    .nv = *nv,
507
12.3k
    .cnv =
508
12.3k
      {
509
12.3k
        .name = nv->name->base,
510
12.3k
        .value = nv->value->base,
511
12.3k
        .namelen = nv->name->len,
512
12.3k
        .valuelen = nv->value->len,
513
12.3k
        .flags = nv->flags,
514
12.3k
      },
515
12.3k
  };
516
517
12.3k
  nghttp2_rcbuf_incref(ent->nv.name);
518
12.3k
  nghttp2_rcbuf_incref(ent->nv.value);
519
12.3k
}
520
521
12.3k
void nghttp2_hd_entry_free(nghttp2_hd_entry *ent) {
522
12.3k
  nghttp2_rcbuf_decref(ent->nv.value);
523
12.3k
  nghttp2_rcbuf_decref(ent->nv.name);
524
12.3k
}
525
526
0
static int name_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
527
0
  return a->name->len == b->namelen &&
528
0
         memeq(a->name->base, b->name, b->namelen);
529
0
}
530
531
0
static int value_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
532
0
  return a->value->len == b->valuelen &&
533
0
         memeq(a->value->base, b->value, b->valuelen);
534
0
}
535
536
0
static uint32_t name_hash(const nghttp2_nv *nv) {
537
  /* 32 bit FNV-1a: http://isthe.com/chongo/tech/comp/fnv/ */
538
0
  uint32_t h = 2166136261u;
539
0
  size_t i;
540
541
0
  for (i = 0; i < nv->namelen; ++i) {
542
0
    h ^= nv->name[i];
543
0
    h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
544
0
  }
545
546
0
  return h;
547
0
}
548
549
8.91k
static void hd_map_init(nghttp2_hd_map *map) { *map = (nghttp2_hd_map){0}; }
550
551
0
static void hd_map_insert(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
552
0
  nghttp2_hd_entry **bucket;
553
554
0
  bucket = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
555
556
0
  if (*bucket == NULL) {
557
0
    *bucket = ent;
558
0
    return;
559
0
  }
560
561
  /* lower index is linked near the root */
562
0
  ent->next = *bucket;
563
0
  *bucket = ent;
564
0
}
565
566
static nghttp2_hd_entry *hd_map_find(nghttp2_hd_map *map, int *exact_match,
567
                                     const nghttp2_nv *nv, int32_t token,
568
0
                                     uint32_t hash, int name_only) {
569
0
  nghttp2_hd_entry *p;
570
0
  nghttp2_hd_entry *res = NULL;
571
572
0
  *exact_match = 0;
573
574
0
  for (p = map->table[hash & (HD_MAP_SIZE - 1)]; p; p = p->next) {
575
0
    if (token != p->nv.token ||
576
0
        (token == -1 && (hash != p->hash || !name_eq(&p->nv, nv)))) {
577
0
      continue;
578
0
    }
579
0
    if (!res) {
580
0
      res = p;
581
0
      if (name_only) {
582
0
        break;
583
0
      }
584
0
    }
585
0
    if (value_eq(&p->nv, nv)) {
586
0
      res = p;
587
0
      *exact_match = 1;
588
0
      break;
589
0
    }
590
0
  }
591
592
0
  return res;
593
0
}
594
595
0
static void hd_map_remove(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
596
0
  nghttp2_hd_entry **dst;
597
598
0
  dst = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
599
600
0
  for (; *dst; dst = &(*dst)->next) {
601
0
    if (*dst != ent) {
602
0
      continue;
603
0
    }
604
605
0
    *dst = ent->next;
606
0
    ent->next = NULL;
607
0
    return;
608
0
  }
609
0
}
610
611
static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
612
17.8k
                           nghttp2_mem *mem) {
613
17.8k
  size_t size;
614
17.8k
  const size_t max_size = SIZE_MAX / sizeof(nghttp2_hd_entry *);
615
616
17.8k
  if (bufsize > max_size) {
617
0
    return NGHTTP2_ERR_NOMEM;
618
0
  }
619
620
142k
  for (size = 1; size < bufsize; size <<= 1)
621
124k
    ;
622
623
17.8k
  if (size > max_size) {
624
0
    return NGHTTP2_ERR_NOMEM;
625
0
  }
626
627
17.8k
  ringbuf->buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
628
17.8k
  if (ringbuf->buffer == NULL) {
629
0
    return NGHTTP2_ERR_NOMEM;
630
0
  }
631
17.8k
  ringbuf->mask = size - 1;
632
17.8k
  ringbuf->first = 0;
633
17.8k
  ringbuf->len = 0;
634
17.8k
  return 0;
635
17.8k
}
636
637
static nghttp2_hd_entry *hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
638
35.0k
                                        size_t idx) {
639
35.0k
  assert(idx < ringbuf->len);
640
35.0k
  return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
641
35.0k
}
642
643
static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
644
12.3k
                              nghttp2_mem *mem) {
645
12.3k
  size_t i;
646
12.3k
  size_t size;
647
12.3k
  nghttp2_hd_entry **buffer;
648
649
12.3k
  if (ringbuf->mask + 1 >= bufsize) {
650
12.3k
    return 0;
651
12.3k
  }
652
0
  for (size = 1; size < bufsize; size <<= 1)
653
0
    ;
654
0
  buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
655
0
  if (buffer == NULL) {
656
0
    return NGHTTP2_ERR_NOMEM;
657
0
  }
658
0
  for (i = 0; i < ringbuf->len; ++i) {
659
0
    buffer[i] = hd_ringbuf_get(ringbuf, i);
660
0
  }
661
0
  nghttp2_mem_free(mem, ringbuf->buffer);
662
0
  ringbuf->buffer = buffer;
663
0
  ringbuf->mask = size - 1;
664
0
  ringbuf->first = 0;
665
0
  return 0;
666
0
}
667
668
17.8k
static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf, nghttp2_mem *mem) {
669
17.8k
  size_t i;
670
17.8k
  if (ringbuf == NULL) {
671
0
    return;
672
0
  }
673
28.4k
  for (i = 0; i < ringbuf->len; ++i) {
674
10.6k
    nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
675
676
10.6k
    nghttp2_hd_entry_free(ent);
677
10.6k
    nghttp2_mem_free(mem, ent);
678
10.6k
  }
679
17.8k
  nghttp2_mem_free(mem, ringbuf->buffer);
680
17.8k
}
681
682
static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
683
12.3k
                                 nghttp2_hd_entry *ent, nghttp2_mem *mem) {
684
12.3k
  int rv;
685
686
12.3k
  rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem);
687
688
12.3k
  if (rv != 0) {
689
0
    return rv;
690
0
  }
691
692
12.3k
  ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
693
12.3k
  ++ringbuf->len;
694
695
12.3k
  return 0;
696
12.3k
}
697
698
1.72k
static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) {
699
1.72k
  assert(ringbuf->len > 0);
700
1.72k
  --ringbuf->len;
701
1.72k
}
702
703
17.8k
static int hd_context_init(nghttp2_hd_context *context, nghttp2_mem *mem) {
704
17.8k
  int rv;
705
17.8k
  context->mem = mem;
706
17.8k
  context->bad = 0;
707
17.8k
  context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
708
17.8k
  rv = hd_ringbuf_init(
709
17.8k
    &context->hd_table,
710
17.8k
    context->hd_table_bufsize_max / NGHTTP2_HD_ENTRY_OVERHEAD, mem);
711
17.8k
  if (rv != 0) {
712
0
    return rv;
713
0
  }
714
715
17.8k
  context->hd_table_bufsize = 0;
716
17.8k
  context->next_seq = 0;
717
718
17.8k
  return 0;
719
17.8k
}
720
721
17.8k
static void hd_context_free(nghttp2_hd_context *context) {
722
17.8k
  hd_ringbuf_free(&context->hd_table, context->mem);
723
17.8k
}
724
725
0
int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) {
726
0
  return nghttp2_hd_deflate_init2(
727
0
    deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem);
728
0
}
729
730
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
731
                             size_t max_deflate_dynamic_table_size,
732
8.91k
                             nghttp2_mem *mem) {
733
8.91k
  int rv;
734
8.91k
  rv = hd_context_init(&deflater->ctx, mem);
735
8.91k
  if (rv != 0) {
736
0
    return rv;
737
0
  }
738
739
8.91k
  hd_map_init(&deflater->map);
740
741
8.91k
  if (max_deflate_dynamic_table_size < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
742
0
    deflater->notify_table_size_change = 1;
743
0
    deflater->ctx.hd_table_bufsize_max = max_deflate_dynamic_table_size;
744
8.91k
  } else {
745
8.91k
    deflater->notify_table_size_change = 0;
746
8.91k
  }
747
748
8.91k
  deflater->deflate_hd_table_bufsize_max = max_deflate_dynamic_table_size;
749
8.91k
  deflater->min_hd_table_bufsize_max = UINT32_MAX;
750
751
8.91k
  return 0;
752
8.91k
}
753
754
8.91k
int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem) {
755
8.91k
  int rv;
756
757
8.91k
  rv = hd_context_init(&inflater->ctx, mem);
758
8.91k
  if (rv != 0) {
759
0
    goto fail;
760
0
  }
761
762
8.91k
  inflater->settings_hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
763
8.91k
  inflater->min_hd_table_bufsize_max = UINT32_MAX;
764
765
8.91k
  inflater->nv_name_keep = NULL;
766
8.91k
  inflater->nv_value_keep = NULL;
767
768
8.91k
  inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
769
8.91k
  inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
770
771
8.91k
  nghttp2_buf_init(&inflater->namebuf);
772
8.91k
  nghttp2_buf_init(&inflater->valuebuf);
773
774
8.91k
  inflater->namercbuf = NULL;
775
8.91k
  inflater->valuercbuf = NULL;
776
777
8.91k
  inflater->huffman_encoded = 0;
778
8.91k
  inflater->index = 0;
779
8.91k
  inflater->left = 0;
780
8.91k
  inflater->shift = 0;
781
8.91k
  inflater->index_required = 0;
782
8.91k
  inflater->no_index = 0;
783
784
8.91k
  return 0;
785
786
0
fail:
787
0
  return rv;
788
8.91k
}
789
790
94.3k
static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) {
791
94.3k
  nghttp2_rcbuf_decref(inflater->nv_value_keep);
792
94.3k
  nghttp2_rcbuf_decref(inflater->nv_name_keep);
793
794
94.3k
  inflater->nv_value_keep = NULL;
795
94.3k
  inflater->nv_name_keep = NULL;
796
94.3k
}
797
798
8.91k
void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater) {
799
8.91k
  hd_context_free(&deflater->ctx);
800
8.91k
}
801
802
8.91k
void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater) {
803
8.91k
  hd_inflate_keep_free(inflater);
804
805
8.91k
  nghttp2_rcbuf_decref(inflater->valuercbuf);
806
8.91k
  nghttp2_rcbuf_decref(inflater->namercbuf);
807
808
8.91k
  hd_context_free(&inflater->ctx);
809
8.91k
}
810
811
14.5k
static size_t entry_room(size_t namelen, size_t valuelen) {
812
14.5k
  return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
813
14.5k
}
814
815
52.2k
static void emit_header(nghttp2_hd_nv *nv_out, nghttp2_hd_nv *nv) {
816
52.2k
  DEBUGF("inflatehd: header emission: %s: %s\n", nv->name->base,
817
52.2k
         nv->value->base);
818
  /* ent->ref may be 0. This happens if the encoder emits literal
819
     block larger than header table capacity with indexing. */
820
52.2k
  *nv_out = *nv;
821
52.2k
}
822
823
0
static size_t count_encoded_length(size_t n, size_t prefix) {
824
0
  size_t k = (size_t)((1 << prefix) - 1);
825
0
  size_t len = 0;
826
827
0
  if (n < k) {
828
0
    return 1;
829
0
  }
830
831
0
  n -= k;
832
0
  ++len;
833
834
0
  for (; n >= 128; n >>= 7, ++len)
835
0
    ;
836
837
0
  return len + 1;
838
0
}
839
840
0
static size_t encode_length(uint8_t *buf, size_t n, size_t prefix) {
841
0
  size_t k = (size_t)((1 << prefix) - 1);
842
0
  uint8_t *begin = buf;
843
844
0
  *buf = (uint8_t)(*buf & ~k);
845
846
0
  if (n < k) {
847
0
    *buf = (uint8_t)(*buf | n);
848
0
    return 1;
849
0
  }
850
851
0
  *buf = (uint8_t)(*buf | k);
852
0
  ++buf;
853
854
0
  n -= k;
855
856
0
  for (; n >= 128; n >>= 7) {
857
0
    *buf++ = (uint8_t)((1 << 7) | (n & 0x7F));
858
0
  }
859
860
0
  *buf++ = (uint8_t)n;
861
862
0
  return (size_t)(buf - begin);
863
0
}
864
865
/*
866
 * Decodes |prefix| prefixed integer stored from |in|.  The |last|
867
 * represents the 1 beyond the last of the valid contiguous memory
868
 * region from |in|.  The decoded integer must be less than or equal
869
 * to UINT32_MAX.
870
 *
871
 * If the |initial| is nonzero, it is used as a initial value, this
872
 * function assumes the |in| starts with intermediate data.
873
 *
874
 * An entire integer is decoded successfully, decoded, the |*fin| is
875
 * set to nonzero.
876
 *
877
 * This function stores the decoded integer in |*res| if it succeed,
878
 * including partial decoding (in this case, number of shift to make
879
 * in the next call will be stored in |*shift_ptr|) and returns number
880
 * of bytes processed, or returns -1, indicating decoding error.
881
 */
882
static nghttp2_ssize decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
883
                                   uint32_t initial, size_t shift,
884
                                   const uint8_t *in, const uint8_t *last,
885
84.0k
                                   size_t prefix) {
886
84.0k
  uint32_t k = (uint8_t)((1 << prefix) - 1);
887
84.0k
  uint32_t n = initial;
888
84.0k
  const uint8_t *start = in;
889
890
84.0k
  *shift_ptr = 0;
891
84.0k
  *fin = 0;
892
893
84.0k
  if (n == 0) {
894
82.8k
    if ((*in & k) != k) {
895
78.4k
      *res = (*in) & k;
896
78.4k
      *fin = 1;
897
78.4k
      return 1;
898
78.4k
    }
899
900
4.41k
    n = k;
901
902
4.41k
    if (++in == last) {
903
331
      *res = n;
904
331
      return (nghttp2_ssize)(in - start);
905
331
    }
906
4.41k
  }
907
908
13.7k
  for (; in != last; ++in, shift += 7) {
909
12.2k
    uint32_t add = *in & 0x7F;
910
911
12.2k
    if (shift >= 32) {
912
24
      DEBUGF("inflate: shift exponent overflow\n");
913
24
      return -1;
914
24
    }
915
916
12.2k
    if ((UINT32_MAX >> shift) < add) {
917
30
      DEBUGF("inflate: integer overflow on shift\n");
918
30
      return -1;
919
30
    }
920
921
12.1k
    add <<= shift;
922
923
12.1k
    if (UINT32_MAX - add < n) {
924
44
      DEBUGF("inflate: integer overflow on addition\n");
925
44
      return -1;
926
44
    }
927
928
12.1k
    n += add;
929
930
12.1k
    if ((*in & (1 << 7)) == 0) {
931
3.72k
      break;
932
3.72k
    }
933
12.1k
  }
934
935
5.17k
  *shift_ptr = shift;
936
937
5.17k
  if (in == last) {
938
1.45k
    *res = n;
939
1.45k
    return (nghttp2_ssize)(in - start);
940
1.45k
  }
941
942
3.72k
  *res = n;
943
3.72k
  *fin = 1;
944
3.72k
  return (nghttp2_ssize)(in + 1 - start);
945
5.17k
}
946
947
0
static int emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
948
0
  int rv;
949
0
  uint8_t *bufp;
950
0
  size_t blocklen;
951
0
  uint8_t sb[16];
952
953
0
  DEBUGF("deflatehd: emit table_size=%zu\n", table_size);
954
955
0
  blocklen = count_encoded_length(table_size, 5);
956
957
0
  if (sizeof(sb) < blocklen) {
958
0
    return NGHTTP2_ERR_HEADER_COMP;
959
0
  }
960
961
0
  bufp = sb;
962
963
0
  *bufp = 0x20U;
964
965
0
  encode_length(bufp, table_size, 5);
966
967
0
  rv = nghttp2_bufs_add(bufs, sb, blocklen);
968
0
  if (rv != 0) {
969
0
    return rv;
970
0
  }
971
972
0
  return 0;
973
0
}
974
975
0
static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx) {
976
0
  int rv;
977
0
  size_t blocklen;
978
0
  uint8_t sb[16];
979
0
  uint8_t *bufp;
980
981
0
  blocklen = count_encoded_length(idx + 1, 7);
982
983
0
  DEBUGF("deflatehd: emit indexed index=%zu, %zu bytes\n", idx, blocklen);
984
985
0
  if (sizeof(sb) < blocklen) {
986
0
    return NGHTTP2_ERR_HEADER_COMP;
987
0
  }
988
989
0
  bufp = sb;
990
0
  *bufp = 0x80U;
991
0
  encode_length(bufp, idx + 1, 7);
992
993
0
  rv = nghttp2_bufs_add(bufs, sb, blocklen);
994
0
  if (rv != 0) {
995
0
    return rv;
996
0
  }
997
998
0
  return 0;
999
0
}
1000
1001
0
static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len) {
1002
0
  int rv;
1003
0
  uint8_t sb[16];
1004
0
  uint8_t *bufp;
1005
0
  size_t blocklen;
1006
0
  size_t enclen;
1007
0
  int huffman = 0;
1008
1009
0
  enclen = nghttp2_hd_huff_encode_count(str, len);
1010
1011
0
  if (enclen < len) {
1012
0
    huffman = 1;
1013
0
  } else {
1014
0
    enclen = len;
1015
0
  }
1016
1017
0
  blocklen = count_encoded_length(enclen, 7);
1018
1019
0
  DEBUGF("deflatehd: emit string str=%.*s, length=%zu, huffman=%d, "
1020
0
         "encoded_length=%zu\n",
1021
0
         (int)len, (const char *)str, len, huffman, enclen);
1022
1023
0
  if (sizeof(sb) < blocklen) {
1024
0
    return NGHTTP2_ERR_HEADER_COMP;
1025
0
  }
1026
1027
0
  bufp = sb;
1028
0
  *bufp = huffman ? 1 << 7 : 0;
1029
0
  encode_length(bufp, enclen, 7);
1030
1031
0
  rv = nghttp2_bufs_add(bufs, sb, blocklen);
1032
0
  if (rv != 0) {
1033
0
    return rv;
1034
0
  }
1035
1036
0
  if (huffman) {
1037
0
    rv = nghttp2_hd_huff_encode(bufs, str, len);
1038
0
  } else {
1039
0
    assert(enclen == len);
1040
0
    rv = nghttp2_bufs_add(bufs, str, len);
1041
0
  }
1042
1043
0
  return rv;
1044
0
}
1045
1046
0
static uint8_t pack_first_byte(int indexing_mode) {
1047
0
  switch (indexing_mode) {
1048
0
  case NGHTTP2_HD_WITH_INDEXING:
1049
0
    return 0x40U;
1050
0
  case NGHTTP2_HD_WITHOUT_INDEXING:
1051
0
    return 0;
1052
0
  case NGHTTP2_HD_NEVER_INDEXING:
1053
0
    return 0x10U;
1054
0
  default:
1055
0
    assert(0);
1056
0
  }
1057
  /* This is required to compile with android NDK r10d +
1058
     --enable-werror */
1059
0
  return 0;
1060
0
}
1061
1062
static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
1063
0
                              const nghttp2_nv *nv, int indexing_mode) {
1064
0
  int rv;
1065
0
  uint8_t *bufp;
1066
0
  size_t blocklen;
1067
0
  uint8_t sb[16];
1068
0
  size_t prefixlen;
1069
1070
0
  if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
1071
0
    prefixlen = 6;
1072
0
  } else {
1073
0
    prefixlen = 4;
1074
0
  }
1075
1076
0
  DEBUGF("deflatehd: emit indname index=%zu, valuelen=%zu, indexing_mode=%d\n",
1077
0
         idx, nv->valuelen, indexing_mode);
1078
1079
0
  blocklen = count_encoded_length(idx + 1, prefixlen);
1080
1081
0
  if (sizeof(sb) < blocklen) {
1082
0
    return NGHTTP2_ERR_HEADER_COMP;
1083
0
  }
1084
1085
0
  bufp = sb;
1086
1087
0
  *bufp = pack_first_byte(indexing_mode);
1088
1089
0
  encode_length(bufp, idx + 1, prefixlen);
1090
1091
0
  rv = nghttp2_bufs_add(bufs, sb, blocklen);
1092
0
  if (rv != 0) {
1093
0
    return rv;
1094
0
  }
1095
1096
0
  rv = emit_string(bufs, nv->value, nv->valuelen);
1097
0
  if (rv != 0) {
1098
0
    return rv;
1099
0
  }
1100
1101
0
  return 0;
1102
0
}
1103
1104
static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
1105
0
                              int indexing_mode) {
1106
0
  int rv;
1107
1108
0
  DEBUGF(
1109
0
    "deflatehd: emit newname namelen=%zu, valuelen=%zu, indexing_mode=%d\n",
1110
0
    nv->namelen, nv->valuelen, indexing_mode);
1111
1112
0
  rv = nghttp2_bufs_addb(bufs, pack_first_byte(indexing_mode));
1113
0
  if (rv != 0) {
1114
0
    return rv;
1115
0
  }
1116
1117
0
  rv = emit_string(bufs, nv->name, nv->namelen);
1118
0
  if (rv != 0) {
1119
0
    return rv;
1120
0
  }
1121
1122
0
  rv = emit_string(bufs, nv->value, nv->valuelen);
1123
0
  if (rv != 0) {
1124
0
    return rv;
1125
0
  }
1126
1127
0
  return 0;
1128
0
}
1129
1130
static int add_hd_table_incremental(nghttp2_hd_context *context,
1131
                                    nghttp2_hd_nv *nv, nghttp2_hd_map *map,
1132
12.7k
                                    uint32_t hash) {
1133
12.7k
  int rv;
1134
12.7k
  nghttp2_hd_entry *new_ent;
1135
12.7k
  size_t room;
1136
12.7k
  nghttp2_mem *mem;
1137
1138
12.7k
  mem = context->mem;
1139
12.7k
  room = entry_room(nv->name->len, nv->value->len);
1140
1141
14.0k
  while (context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
1142
1.70k
         context->hd_table.len > 0) {
1143
1.29k
    size_t idx = context->hd_table.len - 1;
1144
1.29k
    nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
1145
1146
1.29k
    context->hd_table_bufsize -=
1147
1.29k
      entry_room(ent->nv.name->len, ent->nv.value->len);
1148
1149
1.29k
    DEBUGF("hpack: remove item from header table: %s: %s\n",
1150
1.29k
           (char *)ent->nv.name->base, (char *)ent->nv.value->base);
1151
1152
1.29k
    hd_ringbuf_pop_back(&context->hd_table);
1153
1.29k
    if (map) {
1154
0
      hd_map_remove(map, ent);
1155
0
    }
1156
1157
1.29k
    nghttp2_hd_entry_free(ent);
1158
1.29k
    nghttp2_mem_free(mem, ent);
1159
1.29k
  }
1160
1161
12.7k
  if (room > context->hd_table_bufsize_max) {
1162
    /* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
1163
       immediately evicted.  So we don't allocate memory for it. */
1164
411
    return 0;
1165
411
  }
1166
1167
12.3k
  new_ent = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry));
1168
12.3k
  if (new_ent == NULL) {
1169
0
    return NGHTTP2_ERR_NOMEM;
1170
0
  }
1171
1172
12.3k
  nghttp2_hd_entry_init(new_ent, nv);
1173
1174
12.3k
  rv = hd_ringbuf_push_front(&context->hd_table, new_ent, mem);
1175
1176
12.3k
  if (rv != 0) {
1177
0
    nghttp2_hd_entry_free(new_ent);
1178
0
    nghttp2_mem_free(mem, new_ent);
1179
1180
0
    return rv;
1181
0
  }
1182
1183
12.3k
  new_ent->seq = context->next_seq++;
1184
12.3k
  new_ent->hash = hash;
1185
1186
12.3k
  if (map) {
1187
0
    hd_map_insert(map, new_ent);
1188
0
  }
1189
1190
12.3k
  context->hd_table_bufsize += room;
1191
1192
12.3k
  return 0;
1193
12.3k
}
1194
1195
typedef struct {
1196
  nghttp2_ssize index;
1197
  /* Nonzero if both name and value are matched. */
1198
  int name_value_match;
1199
} search_result;
1200
1201
static search_result search_static_table(const nghttp2_nv *nv, int32_t token,
1202
0
                                         int name_only) {
1203
0
  search_result res = {token, 0};
1204
0
  int i;
1205
0
  const nghttp2_hd_static_entry *ent;
1206
1207
0
  if (name_only) {
1208
0
    return res;
1209
0
  }
1210
1211
0
  for (i = token;
1212
0
       i <= NGHTTP2_TOKEN_WWW_AUTHENTICATE && static_table[i].token == token;
1213
0
       ++i) {
1214
0
    ent = &static_table[i];
1215
0
    if (ent->value.len == nv->valuelen &&
1216
0
        memcmp(ent->value.base, nv->value, nv->valuelen) == 0) {
1217
0
      res.index = i;
1218
0
      res.name_value_match = 1;
1219
0
      return res;
1220
0
    }
1221
0
  }
1222
0
  return res;
1223
0
}
1224
1225
static search_result search_hd_table(nghttp2_hd_context *context,
1226
                                     const nghttp2_nv *nv, int32_t token,
1227
                                     int indexing_mode, nghttp2_hd_map *map,
1228
0
                                     uint32_t hash) {
1229
0
  search_result res = {-1, 0};
1230
0
  const nghttp2_hd_entry *ent;
1231
0
  int exact_match;
1232
0
  int name_only = indexing_mode == NGHTTP2_HD_NEVER_INDEXING;
1233
1234
0
  exact_match = 0;
1235
0
  ent = hd_map_find(map, &exact_match, nv, token, hash, name_only);
1236
1237
0
  if (!exact_match && token >= 0 && token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
1238
0
    return search_static_table(nv, token, name_only);
1239
0
  }
1240
1241
0
  if (ent == NULL) {
1242
0
    return res;
1243
0
  }
1244
1245
0
  res.index = (nghttp2_ssize)(context->next_seq - 1 - ent->seq +
1246
0
                              NGHTTP2_STATIC_TABLE_LENGTH);
1247
0
  res.name_value_match = exact_match;
1248
1249
0
  return res;
1250
0
}
1251
1252
static void hd_context_shrink_table_size(nghttp2_hd_context *context,
1253
2.64k
                                         nghttp2_hd_map *map) {
1254
2.64k
  nghttp2_mem *mem;
1255
1256
2.64k
  mem = context->mem;
1257
1258
3.07k
  while (context->hd_table_bufsize > context->hd_table_bufsize_max &&
1259
430
         context->hd_table.len > 0) {
1260
430
    size_t idx = context->hd_table.len - 1;
1261
430
    nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
1262
430
    context->hd_table_bufsize -=
1263
430
      entry_room(ent->nv.name->len, ent->nv.value->len);
1264
430
    hd_ringbuf_pop_back(&context->hd_table);
1265
430
    if (map) {
1266
0
      hd_map_remove(map, ent);
1267
0
    }
1268
1269
430
    nghttp2_hd_entry_free(ent);
1270
430
    nghttp2_mem_free(mem, ent);
1271
430
  }
1272
2.64k
}
1273
1274
int nghttp2_hd_deflate_change_table_size(
1275
635
  nghttp2_hd_deflater *deflater, size_t settings_max_dynamic_table_size) {
1276
635
  size_t next_bufsize = nghttp2_min_size(
1277
635
    settings_max_dynamic_table_size, deflater->deflate_hd_table_bufsize_max);
1278
1279
635
  deflater->ctx.hd_table_bufsize_max = next_bufsize;
1280
1281
635
  deflater->min_hd_table_bufsize_max =
1282
635
    nghttp2_min_size(deflater->min_hd_table_bufsize_max, next_bufsize);
1283
1284
635
  deflater->notify_table_size_change = 1;
1285
1286
635
  hd_context_shrink_table_size(&deflater->ctx, &deflater->map);
1287
635
  return 0;
1288
635
}
1289
1290
int nghttp2_hd_inflate_change_table_size(
1291
0
  nghttp2_hd_inflater *inflater, size_t settings_max_dynamic_table_size) {
1292
0
  switch (inflater->state) {
1293
0
  case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
1294
0
  case NGHTTP2_HD_STATE_INFLATE_START:
1295
0
    break;
1296
0
  default:
1297
0
    return NGHTTP2_ERR_INVALID_STATE;
1298
0
  }
1299
1300
0
  inflater->settings_hd_table_bufsize_max = settings_max_dynamic_table_size;
1301
1302
  /* It seems that encoder is not required to send dynamic table size
1303
     update if the table size is not changed after applying
1304
     SETTINGS_HEADER_TABLE_SIZE.  RFC 7541 is ambiguous here, but this
1305
     is the intention of the editor.  If new maximum table size is
1306
     strictly smaller than the current negotiated maximum size,
1307
     encoder must send dynamic table size update.  In other cases, we
1308
     cannot expect it to do so. */
1309
0
  if (inflater->ctx.hd_table_bufsize_max > settings_max_dynamic_table_size) {
1310
0
    inflater->state = NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE;
1311
    /* Remember minimum value, and validate that encoder sends the
1312
       value less than or equal to this. */
1313
0
    inflater->min_hd_table_bufsize_max = settings_max_dynamic_table_size;
1314
1315
0
    inflater->ctx.hd_table_bufsize_max = settings_max_dynamic_table_size;
1316
1317
0
    hd_context_shrink_table_size(&inflater->ctx, NULL);
1318
0
  }
1319
1320
0
  return 0;
1321
0
}
1322
1323
#define INDEX_RANGE_VALID(context, idx)                                        \
1324
0
  ((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
1325
1326
36.6k
static size_t get_max_index(nghttp2_hd_context *context) {
1327
36.6k
  return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH;
1328
36.6k
}
1329
1330
35.4k
nghttp2_hd_nv nghttp2_hd_table_get(nghttp2_hd_context *context, size_t idx) {
1331
35.4k
  assert(INDEX_RANGE_VALID(context, idx));
1332
35.4k
  if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
1333
22.6k
    return hd_ringbuf_get(&context->hd_table, idx - NGHTTP2_STATIC_TABLE_LENGTH)
1334
22.6k
      ->nv;
1335
22.6k
  } else {
1336
12.8k
    const nghttp2_hd_static_entry *ent = &static_table[idx];
1337
12.8k
    return (nghttp2_hd_nv){
1338
12.8k
      .name = (nghttp2_rcbuf *)&ent->name,
1339
12.8k
      .value = (nghttp2_rcbuf *)&ent->value,
1340
12.8k
      .token = ent->token,
1341
12.8k
      .flags = NGHTTP2_NV_FLAG_NONE,
1342
12.8k
    };
1343
12.8k
  }
1344
35.4k
}
1345
1346
static const nghttp2_nv *nghttp2_hd_table_get2(nghttp2_hd_context *context,
1347
0
                                               size_t idx) {
1348
0
  assert(INDEX_RANGE_VALID(context, idx));
1349
0
  if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
1350
0
    return &hd_ringbuf_get(&context->hd_table,
1351
0
                           idx - NGHTTP2_STATIC_TABLE_LENGTH)
1352
0
              ->cnv;
1353
0
  }
1354
1355
0
  return &static_table[idx].cnv;
1356
0
}
1357
1358
static int hd_deflate_decide_indexing(nghttp2_hd_deflater *deflater,
1359
0
                                      const nghttp2_nv *nv, int32_t token) {
1360
0
  if (token == NGHTTP2_TOKEN__PATH || token == NGHTTP2_TOKEN_AGE ||
1361
0
      token == NGHTTP2_TOKEN_CONTENT_LENGTH || token == NGHTTP2_TOKEN_ETAG ||
1362
0
      token == NGHTTP2_TOKEN_IF_MODIFIED_SINCE ||
1363
0
      token == NGHTTP2_TOKEN_IF_NONE_MATCH || token == NGHTTP2_TOKEN_LOCATION ||
1364
0
      token == NGHTTP2_TOKEN_SET_COOKIE ||
1365
0
      entry_room(nv->namelen, nv->valuelen) >
1366
0
        deflater->ctx.hd_table_bufsize_max * 3 / 4) {
1367
0
    return NGHTTP2_HD_WITHOUT_INDEXING;
1368
0
  }
1369
1370
0
  return NGHTTP2_HD_WITH_INDEXING;
1371
0
}
1372
1373
static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
1374
0
                      const nghttp2_nv *nv) {
1375
0
  int rv;
1376
0
  search_result res;
1377
0
  nghttp2_ssize idx;
1378
0
  int indexing_mode;
1379
0
  int32_t token;
1380
0
  nghttp2_mem *mem;
1381
0
  uint32_t hash = 0;
1382
1383
0
  DEBUGF("deflatehd: deflating %.*s: %.*s\n", (int)nv->namelen, nv->name,
1384
0
         (int)nv->valuelen, nv->value);
1385
1386
0
  mem = deflater->ctx.mem;
1387
1388
0
  token = lookup_token(nv->name, nv->namelen);
1389
0
  if (token == -1) {
1390
0
    hash = name_hash(nv);
1391
0
  } else if (token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
1392
0
    hash = static_table[token].hash;
1393
0
  }
1394
1395
  /* Don't index authorization header field since it may contain low
1396
     entropy secret data (e.g., id/password).  Also cookie header
1397
     field with less than 20 bytes value is also never indexed.  This
1398
     is the same criteria used in Firefox codebase. */
1399
0
  indexing_mode = token == NGHTTP2_TOKEN_AUTHORIZATION ||
1400
0
                      (token == NGHTTP2_TOKEN_COOKIE && nv->valuelen < 20) ||
1401
0
                      (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX)
1402
0
                    ? NGHTTP2_HD_NEVER_INDEXING
1403
0
                    : hd_deflate_decide_indexing(deflater, nv, token);
1404
1405
0
  res = search_hd_table(&deflater->ctx, nv, token, indexing_mode,
1406
0
                        &deflater->map, hash);
1407
1408
0
  idx = res.index;
1409
1410
0
  if (res.name_value_match) {
1411
0
    DEBUGF("deflatehd: name/value match index=%td\n", idx);
1412
1413
0
    rv = emit_indexed_block(bufs, (size_t)idx);
1414
0
    if (rv != 0) {
1415
0
      return rv;
1416
0
    }
1417
1418
0
    return 0;
1419
0
  }
1420
1421
0
  if (res.index != -1) {
1422
0
    DEBUGF("deflatehd: name match index=%td\n", res.index);
1423
0
  }
1424
1425
0
  if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
1426
0
    nghttp2_hd_nv hd_nv;
1427
1428
0
    if (idx != -1) {
1429
0
      hd_nv.name = nghttp2_hd_table_get(&deflater->ctx, (size_t)idx).name;
1430
0
      nghttp2_rcbuf_incref(hd_nv.name);
1431
0
    } else {
1432
0
      rv = nghttp2_rcbuf_new2(&hd_nv.name, nv->name, nv->namelen, mem);
1433
0
      if (rv != 0) {
1434
0
        return rv;
1435
0
      }
1436
0
    }
1437
1438
0
    rv = nghttp2_rcbuf_new2(&hd_nv.value, nv->value, nv->valuelen, mem);
1439
1440
0
    if (rv != 0) {
1441
0
      nghttp2_rcbuf_decref(hd_nv.name);
1442
0
      return rv;
1443
0
    }
1444
1445
0
    hd_nv.token = token;
1446
0
    hd_nv.flags = NGHTTP2_NV_FLAG_NONE;
1447
1448
0
    rv = add_hd_table_incremental(&deflater->ctx, &hd_nv, &deflater->map, hash);
1449
1450
0
    nghttp2_rcbuf_decref(hd_nv.value);
1451
0
    nghttp2_rcbuf_decref(hd_nv.name);
1452
1453
0
    if (rv != 0) {
1454
0
      return NGHTTP2_ERR_HEADER_COMP;
1455
0
    }
1456
0
  }
1457
0
  if (idx == -1) {
1458
0
    rv = emit_newname_block(bufs, nv, indexing_mode);
1459
0
  } else {
1460
0
    rv = emit_indname_block(bufs, (size_t)idx, nv, indexing_mode);
1461
0
  }
1462
0
  if (rv != 0) {
1463
0
    return rv;
1464
0
  }
1465
1466
0
  return 0;
1467
0
}
1468
1469
int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
1470
                               nghttp2_bufs *bufs, const nghttp2_nv *nv,
1471
0
                               size_t nvlen) {
1472
0
  size_t i;
1473
0
  int rv = 0;
1474
1475
0
  if (deflater->ctx.bad) {
1476
0
    return NGHTTP2_ERR_HEADER_COMP;
1477
0
  }
1478
1479
0
  if (deflater->notify_table_size_change) {
1480
0
    size_t min_hd_table_bufsize_max;
1481
1482
0
    min_hd_table_bufsize_max = deflater->min_hd_table_bufsize_max;
1483
1484
0
    deflater->notify_table_size_change = 0;
1485
0
    deflater->min_hd_table_bufsize_max = UINT32_MAX;
1486
1487
0
    if (deflater->ctx.hd_table_bufsize_max > min_hd_table_bufsize_max) {
1488
0
      rv = emit_table_size(bufs, min_hd_table_bufsize_max);
1489
1490
0
      if (rv != 0) {
1491
0
        goto fail;
1492
0
      }
1493
0
    }
1494
1495
0
    rv = emit_table_size(bufs, deflater->ctx.hd_table_bufsize_max);
1496
1497
0
    if (rv != 0) {
1498
0
      goto fail;
1499
0
    }
1500
0
  }
1501
1502
0
  for (i = 0; i < nvlen; ++i) {
1503
0
    rv = deflate_nv(deflater, bufs, &nv[i]);
1504
0
    if (rv != 0) {
1505
0
      goto fail;
1506
0
    }
1507
0
  }
1508
1509
0
  DEBUGF("deflatehd: all input name/value pairs were deflated\n");
1510
1511
0
  return 0;
1512
0
fail:
1513
0
  DEBUGF("deflatehd: error return %d\n", rv);
1514
1515
0
  deflater->ctx.bad = 1;
1516
0
  return rv;
1517
0
}
1518
1519
ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf,
1520
                              size_t buflen, const nghttp2_nv *nv,
1521
0
                              size_t nvlen) {
1522
0
  return (ssize_t)nghttp2_hd_deflate_hd2(deflater, buf, buflen, nv, nvlen);
1523
0
}
1524
1525
nghttp2_ssize nghttp2_hd_deflate_hd2(nghttp2_hd_deflater *deflater,
1526
                                     uint8_t *buf, size_t buflen,
1527
0
                                     const nghttp2_nv *nv, size_t nvlen) {
1528
0
  nghttp2_bufs bufs;
1529
0
  int rv;
1530
0
  nghttp2_mem *mem;
1531
1532
0
  mem = deflater->ctx.mem;
1533
1534
0
  rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen, mem);
1535
1536
0
  if (rv != 0) {
1537
0
    return rv;
1538
0
  }
1539
1540
0
  rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
1541
1542
0
  buflen = nghttp2_bufs_len(&bufs);
1543
1544
0
  nghttp2_bufs_wrap_free(&bufs);
1545
1546
0
  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
1547
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
1548
0
  }
1549
1550
0
  if (rv != 0) {
1551
0
    return rv;
1552
0
  }
1553
1554
0
  return (nghttp2_ssize)buflen;
1555
0
}
1556
1557
ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
1558
                                  const nghttp2_vec *vec, size_t veclen,
1559
0
                                  const nghttp2_nv *nv, size_t nvlen) {
1560
0
  return (ssize_t)nghttp2_hd_deflate_hd_vec2(deflater, vec, veclen, nv, nvlen);
1561
0
}
1562
1563
nghttp2_ssize nghttp2_hd_deflate_hd_vec2(nghttp2_hd_deflater *deflater,
1564
                                         const nghttp2_vec *vec, size_t veclen,
1565
0
                                         const nghttp2_nv *nv, size_t nvlen) {
1566
0
  nghttp2_bufs bufs;
1567
0
  int rv;
1568
0
  nghttp2_mem *mem;
1569
0
  size_t buflen;
1570
1571
0
  mem = deflater->ctx.mem;
1572
1573
0
  rv = nghttp2_bufs_wrap_init2(&bufs, vec, veclen, mem);
1574
1575
0
  if (rv != 0) {
1576
0
    return rv;
1577
0
  }
1578
1579
0
  rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
1580
1581
0
  buflen = nghttp2_bufs_len(&bufs);
1582
1583
0
  nghttp2_bufs_wrap_free(&bufs);
1584
1585
0
  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
1586
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
1587
0
  }
1588
1589
0
  if (rv != 0) {
1590
0
    return rv;
1591
0
  }
1592
1593
0
  return (nghttp2_ssize)buflen;
1594
0
}
1595
1596
size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater,
1597
0
                                const nghttp2_nv *nva, size_t nvlen) {
1598
0
  size_t n = 0;
1599
0
  size_t i;
1600
0
  (void)deflater;
1601
1602
  /* Possible Maximum Header Table Size Change.  Encoding (1u << 31) -
1603
     1 using 4 bit prefix requires 6 bytes.  We may emit this at most
1604
     twice. */
1605
0
  n += 12;
1606
1607
  /* Use Literal Header Field without indexing - New Name, since it is
1608
     most space consuming format.  Also we choose the less one between
1609
     non-huffman and huffman, so using literal byte count is
1610
     sufficient for upper bound.
1611
1612
     Encoding (1u << 31) - 1 using 7 bit prefix requires 6 bytes.  We
1613
     need 2 of this for |nvlen| header fields. */
1614
0
  n += 6 * 2 * nvlen;
1615
1616
0
  for (i = 0; i < nvlen; ++i) {
1617
0
    n += nva[i].namelen + nva[i].valuelen;
1618
0
  }
1619
1620
0
  return n;
1621
0
}
1622
1623
int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
1624
0
                           size_t deflate_hd_table_bufsize_max) {
1625
0
  return nghttp2_hd_deflate_new2(deflater_ptr, deflate_hd_table_bufsize_max,
1626
0
                                 NULL);
1627
0
}
1628
1629
/* nghttp2_hd_deflater allocated by nghttp2_hd_deflate_new2().  The
1630
   application may free |mem| passed to nghttp2_hd_deflate_new2()
1631
   after it returns, so keep a copy of it along with deflater. */
1632
typedef struct {
1633
  nghttp2_hd_deflater deflater;
1634
  nghttp2_mem mem;
1635
} hd_deflater_with_mem;
1636
1637
int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
1638
                            size_t deflate_hd_table_bufsize_max,
1639
0
                            nghttp2_mem *mem) {
1640
0
  int rv;
1641
0
  hd_deflater_with_mem *p;
1642
1643
0
  if (mem == NULL) {
1644
0
    mem = nghttp2_mem_default();
1645
0
  }
1646
1647
0
  p = nghttp2_mem_malloc(mem, sizeof(hd_deflater_with_mem));
1648
1649
0
  if (p == NULL) {
1650
0
    return NGHTTP2_ERR_NOMEM;
1651
0
  }
1652
1653
0
  p->mem = *mem;
1654
1655
0
  rv = nghttp2_hd_deflate_init2(&p->deflater, deflate_hd_table_bufsize_max,
1656
0
                                &p->mem);
1657
1658
0
  if (rv != 0) {
1659
0
    nghttp2_mem_free(mem, p);
1660
1661
0
    return rv;
1662
0
  }
1663
1664
0
  *deflater_ptr = &p->deflater;
1665
1666
0
  return 0;
1667
0
}
1668
1669
0
void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) {
1670
0
  nghttp2_mem mem;
1671
1672
  /* deflater->ctx.mem points to the memory being freed */
1673
0
  mem = *deflater->ctx.mem;
1674
1675
0
  nghttp2_hd_deflate_free(deflater);
1676
1677
0
  nghttp2_mem_free(&mem, deflater);
1678
0
}
1679
1680
static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater,
1681
44.2k
                                           const uint8_t *in) {
1682
44.2k
  inflater->huffman_encoded = (*in & (1 << 7)) != 0;
1683
44.2k
}
1684
1685
/*
1686
 * Decodes the integer from the range [in, last).  The result is
1687
 * assigned to |inflater->left|.  If the |inflater->left| is 0, then
1688
 * it performs variable integer decoding from scratch. Otherwise, it
1689
 * uses the |inflater->left| as the initial value and continues to
1690
 * decode assuming that [in, last) begins with intermediary sequence.
1691
 *
1692
 * This function returns the number of bytes read if it succeeds, or
1693
 * one of the following negative error codes:
1694
 *
1695
 * NGHTTP2_ERR_HEADER_COMP
1696
 *   Integer decoding failed
1697
 */
1698
static nghttp2_ssize hd_inflate_read_len(nghttp2_hd_inflater *inflater,
1699
                                         int *rfin, const uint8_t *in,
1700
                                         const uint8_t *last, size_t prefix,
1701
84.0k
                                         size_t maxlen) {
1702
84.0k
  nghttp2_ssize rv;
1703
84.0k
  uint32_t out;
1704
1705
84.0k
  *rfin = 0;
1706
1707
84.0k
  rv = decode_length(&out, &inflater->shift, rfin, (uint32_t)inflater->left,
1708
84.0k
                     inflater->shift, in, last, prefix);
1709
1710
84.0k
  if (rv == -1) {
1711
98
    DEBUGF("inflatehd: integer decoding failed\n");
1712
98
    return NGHTTP2_ERR_HEADER_COMP;
1713
98
  }
1714
1715
83.9k
  if (out > maxlen) {
1716
893
    DEBUGF("inflatehd: integer exceeded the maximum value %zu\n", maxlen);
1717
893
    return NGHTTP2_ERR_HEADER_COMP;
1718
893
  }
1719
1720
83.0k
  inflater->left = out;
1721
1722
83.0k
  DEBUGF("inflatehd: decoded integer is %u\n", out);
1723
1724
83.0k
  return rv;
1725
83.9k
}
1726
1727
/*
1728
 * Reads |inflater->left| bytes from the range [in, last) and performs
1729
 * huffman decoding against them and pushes the result into the
1730
 * |buffer|.
1731
 *
1732
 * This function returns the number of bytes read if it succeeds, or
1733
 * one of the following negative error codes:
1734
 *
1735
 * NGHTTP2_ERR_NOMEM
1736
 *   Out of memory
1737
 * NGHTTP2_ERR_HEADER_COMP
1738
 *   Huffman decoding failed
1739
 */
1740
static nghttp2_ssize hd_inflate_read_huff(nghttp2_hd_inflater *inflater,
1741
                                          nghttp2_buf *buf, const uint8_t *in,
1742
14.8k
                                          const uint8_t *last) {
1743
14.8k
  nghttp2_ssize readlen;
1744
14.8k
  int fin = 0;
1745
14.8k
  if ((size_t)(last - in) >= inflater->left) {
1746
14.6k
    last = in + inflater->left;
1747
14.6k
    fin = 1;
1748
14.6k
  }
1749
14.8k
  readlen = nghttp2_hd_huff_decode(&inflater->huff_decode_ctx, buf, in,
1750
14.8k
                                   (size_t)(last - in), fin);
1751
1752
14.8k
  if (readlen < 0) {
1753
12
    DEBUGF("inflatehd: huffman decoding failed\n");
1754
12
    return readlen;
1755
12
  }
1756
14.8k
  if (nghttp2_hd_huff_decode_failure_state(&inflater->huff_decode_ctx)) {
1757
9
    DEBUGF("inflatehd: huffman decoding failed\n");
1758
9
    return NGHTTP2_ERR_HEADER_COMP;
1759
9
  }
1760
1761
14.8k
  inflater->left -= (size_t)readlen;
1762
14.8k
  return readlen;
1763
14.8k
}
1764
1765
/*
1766
 * Reads |inflater->left| bytes from the range [in, last) and copies
1767
 * them into the |buffer|.
1768
 *
1769
 * This function returns the number of bytes read if it succeeds, or
1770
 * one of the following negative error codes:
1771
 *
1772
 * NGHTTP2_ERR_NOMEM
1773
 *   Out of memory
1774
 * NGHTTP2_ERR_HEADER_COMP
1775
 *   Header decompression failed
1776
 */
1777
static nghttp2_ssize hd_inflate_read(nghttp2_hd_inflater *inflater,
1778
                                     nghttp2_buf *buf, const uint8_t *in,
1779
28.8k
                                     const uint8_t *last) {
1780
28.8k
  size_t len = nghttp2_min_size((size_t)(last - in), inflater->left);
1781
1782
28.8k
  buf->last = nghttp2_cpymem(buf->last, in, len);
1783
1784
28.8k
  inflater->left -= len;
1785
28.8k
  return (nghttp2_ssize)len;
1786
28.8k
}
1787
1788
/*
1789
 * Finalize indexed header representation reception.  The referenced
1790
 * header is always emitted, and |*nv_out| is filled with that value.
1791
 */
1792
static void hd_inflate_commit_indexed(nghttp2_hd_inflater *inflater,
1793
25.6k
                                      nghttp2_hd_nv *nv_out) {
1794
25.6k
  nghttp2_hd_nv nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
1795
1796
25.6k
  emit_header(nv_out, &nv);
1797
25.6k
}
1798
1799
/*
1800
 * Finalize literal header representation - new name- reception. If
1801
 * header is emitted, |*nv_out| is filled with that value and 0 is
1802
 * returned.
1803
 *
1804
 * This function returns 0 if it succeeds, or one of the following
1805
 * negative error codes:
1806
 *
1807
 * NGHTTP2_ERR_NOMEM
1808
 *   Out of memory
1809
 */
1810
static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
1811
16.7k
                                     nghttp2_hd_nv *nv_out) {
1812
16.7k
  nghttp2_hd_nv nv;
1813
16.7k
  int rv;
1814
1815
16.7k
  if (inflater->no_index) {
1816
693
    nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
1817
16.0k
  } else {
1818
16.0k
    nv.flags = NGHTTP2_NV_FLAG_NONE;
1819
16.0k
  }
1820
1821
16.7k
  nv.name = inflater->namercbuf;
1822
16.7k
  nv.value = inflater->valuercbuf;
1823
16.7k
  nv.token = lookup_token(inflater->namercbuf->base, inflater->namercbuf->len);
1824
1825
16.7k
  if (inflater->index_required) {
1826
5.27k
    rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
1827
1828
5.27k
    if (rv != 0) {
1829
0
      return rv;
1830
0
    }
1831
5.27k
  }
1832
1833
16.7k
  emit_header(nv_out, &nv);
1834
1835
16.7k
  inflater->nv_name_keep = nv.name;
1836
16.7k
  inflater->nv_value_keep = nv.value;
1837
1838
16.7k
  inflater->namercbuf = NULL;
1839
16.7k
  inflater->valuercbuf = NULL;
1840
1841
16.7k
  return 0;
1842
16.7k
}
1843
1844
/*
1845
 * Finalize literal header representation - indexed name-
1846
 * reception. If header is emitted, |*nv_out| is filled with that
1847
 * value and 0 is returned.
1848
 *
1849
 * This function returns 0 if it succeeds, or one of the following
1850
 * negative error codes:
1851
 *
1852
 * NGHTTP2_ERR_NOMEM
1853
 *   Out of memory
1854
 */
1855
static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
1856
9.81k
                                     nghttp2_hd_nv *nv_out) {
1857
9.81k
  nghttp2_hd_nv nv;
1858
9.81k
  int rv;
1859
1860
9.81k
  nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
1861
1862
9.81k
  if (inflater->no_index) {
1863
416
    nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
1864
9.39k
  } else {
1865
9.39k
    nv.flags = NGHTTP2_NV_FLAG_NONE;
1866
9.39k
  }
1867
1868
9.81k
  nghttp2_rcbuf_incref(nv.name);
1869
1870
9.81k
  nv.value = inflater->valuercbuf;
1871
1872
9.81k
  if (inflater->index_required) {
1873
7.50k
    rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
1874
7.50k
    if (rv != 0) {
1875
0
      nghttp2_rcbuf_decref(nv.name);
1876
0
      return NGHTTP2_ERR_NOMEM;
1877
0
    }
1878
7.50k
  }
1879
1880
9.81k
  emit_header(nv_out, &nv);
1881
1882
9.81k
  inflater->nv_name_keep = nv.name;
1883
9.81k
  inflater->nv_value_keep = nv.value;
1884
1885
9.81k
  inflater->valuercbuf = NULL;
1886
1887
9.81k
  return 0;
1888
9.81k
}
1889
1890
ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out,
1891
                              int *inflate_flags, uint8_t *in, size_t inlen,
1892
0
                              int in_final) {
1893
0
  return nghttp2_hd_inflate_hd2(inflater, nv_out, inflate_flags, in, inlen,
1894
0
                                in_final);
1895
0
}
1896
1897
ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
1898
                               nghttp2_nv *nv_out, int *inflate_flags,
1899
0
                               const uint8_t *in, size_t inlen, int in_final) {
1900
0
  return (nghttp2_ssize)nghttp2_hd_inflate_hd3(inflater, nv_out, inflate_flags,
1901
0
                                               in, inlen, in_final);
1902
0
}
1903
1904
nghttp2_ssize nghttp2_hd_inflate_hd3(nghttp2_hd_inflater *inflater,
1905
                                     nghttp2_nv *nv_out, int *inflate_flags,
1906
                                     const uint8_t *in, size_t inlen,
1907
0
                                     int in_final) {
1908
0
  nghttp2_ssize rv;
1909
0
  nghttp2_hd_nv hd_nv;
1910
1911
0
  rv = nghttp2_hd_inflate_hd_nv(inflater, &hd_nv, inflate_flags, in, inlen,
1912
0
                                in_final);
1913
1914
0
  if (rv < 0) {
1915
0
    return rv;
1916
0
  }
1917
1918
0
  if (*inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
1919
0
    *nv_out = (nghttp2_nv){
1920
0
      .name = hd_nv.name->base,
1921
0
      .value = hd_nv.value->base,
1922
0
      .namelen = hd_nv.name->len,
1923
0
      .valuelen = hd_nv.value->len,
1924
0
      .flags = hd_nv.flags,
1925
0
    };
1926
0
  }
1927
1928
0
  return rv;
1929
0
}
1930
1931
nghttp2_ssize nghttp2_hd_inflate_hd_nv(nghttp2_hd_inflater *inflater,
1932
                                       nghttp2_hd_nv *nv_out,
1933
                                       int *inflate_flags, const uint8_t *in,
1934
71.6k
                                       size_t inlen, int in_final) {
1935
71.6k
  nghttp2_ssize rv = 0;
1936
71.6k
  const uint8_t *first = in;
1937
71.6k
  const uint8_t *last = in + inlen;
1938
71.6k
  int rfin = 0;
1939
71.6k
  int busy = 0;
1940
71.6k
  nghttp2_mem *mem;
1941
1942
71.6k
  mem = inflater->ctx.mem;
1943
1944
71.6k
  if (inflater->ctx.bad) {
1945
0
    return NGHTTP2_ERR_HEADER_COMP;
1946
0
  }
1947
1948
71.6k
  DEBUGF("inflatehd: start state=%d\n", inflater->state);
1949
71.6k
  hd_inflate_keep_free(inflater);
1950
71.6k
  *inflate_flags = NGHTTP2_HD_INFLATE_NONE;
1951
200k
  for (; in != last || busy;) {
1952
183k
    busy = 0;
1953
183k
    switch (inflater->state) {
1954
0
    case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
1955
0
      if ((*in & 0xE0U) != 0x20U) {
1956
0
        DEBUGF("inflatehd: header table size change was expected, but saw "
1957
0
               "0x%02x as first byte",
1958
0
               *in);
1959
0
        rv = NGHTTP2_ERR_HEADER_COMP;
1960
0
        goto fail;
1961
0
      }
1962
    /* fall through */
1963
17.8k
    case NGHTTP2_HD_STATE_INFLATE_START:
1964
55.9k
    case NGHTTP2_HD_STATE_OPCODE:
1965
55.9k
      if ((*in & 0xE0U) == 0x20U) {
1966
2.33k
        DEBUGF("inflatehd: header table size change\n");
1967
2.33k
        if (inflater->state == NGHTTP2_HD_STATE_OPCODE) {
1968
25
          DEBUGF("inflatehd: header table size change must appear at the head "
1969
25
                 "of header block\n");
1970
25
          rv = NGHTTP2_ERR_HEADER_COMP;
1971
25
          goto fail;
1972
25
        }
1973
2.31k
        inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
1974
2.31k
        inflater->state = NGHTTP2_HD_STATE_READ_TABLE_SIZE;
1975
53.6k
      } else if (*in & 0x80U) {
1976
25.8k
        DEBUGF("inflatehd: indexed repr\n");
1977
25.8k
        inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
1978
25.8k
        inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
1979
27.7k
      } else {
1980
27.7k
        if (*in == 0x40U || *in == 0 || *in == 0x10U) {
1981
17.2k
          DEBUGF("inflatehd: literal header repr - new name\n");
1982
17.2k
          inflater->opcode = NGHTTP2_HD_OPCODE_NEWNAME;
1983
17.2k
          inflater->state = NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN;
1984
17.2k
        } else {
1985
10.4k
          DEBUGF("inflatehd: literal header repr - indexed name\n");
1986
10.4k
          inflater->opcode = NGHTTP2_HD_OPCODE_INDNAME;
1987
10.4k
          inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
1988
10.4k
        }
1989
27.7k
        inflater->index_required = (*in & 0x40) != 0;
1990
27.7k
        inflater->no_index = (*in & 0xF0U) == 0x10U;
1991
27.7k
        DEBUGF("inflatehd: indexing required=%d, no_index=%d\n",
1992
27.7k
               inflater->index_required, inflater->no_index);
1993
27.7k
        if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
1994
17.2k
          ++in;
1995
17.2k
        }
1996
27.7k
      }
1997
55.9k
      inflater->left = 0;
1998
55.9k
      inflater->shift = 0;
1999
55.9k
      break;
2000
2.87k
    case NGHTTP2_HD_STATE_READ_TABLE_SIZE:
2001
2.87k
      rfin = 0;
2002
2.87k
      rv = hd_inflate_read_len(
2003
2.87k
        inflater, &rfin, in, last, 5,
2004
2.87k
        nghttp2_min_size(inflater->min_hd_table_bufsize_max,
2005
2.87k
                         inflater->settings_hd_table_bufsize_max));
2006
2.87k
      if (rv < 0) {
2007
235
        goto fail;
2008
235
      }
2009
2.63k
      in += rv;
2010
2.63k
      if (!rfin) {
2011
621
        goto almost_ok;
2012
621
      }
2013
2.01k
      DEBUGF("inflatehd: table_size=%zu\n", inflater->left);
2014
2.01k
      inflater->min_hd_table_bufsize_max = UINT32_MAX;
2015
2.01k
      inflater->ctx.hd_table_bufsize_max = inflater->left;
2016
2.01k
      hd_context_shrink_table_size(&inflater->ctx, NULL);
2017
2.01k
      inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
2018
2.01k
      break;
2019
36.6k
    case NGHTTP2_HD_STATE_READ_INDEX: {
2020
36.6k
      size_t prefixlen;
2021
2022
36.6k
      if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
2023
25.8k
        prefixlen = 7;
2024
25.8k
      } else if (inflater->index_required) {
2025
7.76k
        prefixlen = 6;
2026
7.76k
      } else {
2027
3.02k
        prefixlen = 4;
2028
3.02k
      }
2029
2030
36.6k
      rfin = 0;
2031
36.6k
      rv = hd_inflate_read_len(inflater, &rfin, in, last, prefixlen,
2032
36.6k
                               get_max_index(&inflater->ctx));
2033
36.6k
      if (rv < 0) {
2034
356
        goto fail;
2035
356
      }
2036
2037
36.2k
      in += rv;
2038
2039
36.2k
      if (!rfin) {
2040
340
        goto almost_ok;
2041
340
      }
2042
2043
35.9k
      if (inflater->left == 0) {
2044
12
        rv = NGHTTP2_ERR_HEADER_COMP;
2045
12
        goto fail;
2046
12
      }
2047
2048
35.9k
      DEBUGF("inflatehd: index=%zu\n", inflater->left);
2049
35.9k
      if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
2050
25.6k
        inflater->index = inflater->left;
2051
25.6k
        --inflater->index;
2052
2053
25.6k
        hd_inflate_commit_indexed(inflater, nv_out);
2054
2055
25.6k
        inflater->state = NGHTTP2_HD_STATE_OPCODE;
2056
25.6k
        *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2057
25.6k
        return (nghttp2_ssize)(in - first);
2058
25.6k
      } else {
2059
10.2k
        inflater->index = inflater->left;
2060
10.2k
        --inflater->index;
2061
2062
10.2k
        inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2063
10.2k
      }
2064
10.2k
      break;
2065
35.9k
    }
2066
17.2k
    case NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN:
2067
17.2k
      hd_inflate_set_huffman_encoded(inflater, in);
2068
17.2k
      inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN;
2069
17.2k
      inflater->left = 0;
2070
17.2k
      inflater->shift = 0;
2071
17.2k
      DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
2072
    /* Fall through */
2073
17.4k
    case NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN:
2074
17.4k
      rfin = 0;
2075
17.4k
      rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
2076
17.4k
      if (rv < 0) {
2077
209
        goto fail;
2078
209
      }
2079
17.1k
      in += rv;
2080
17.1k
      if (!rfin) {
2081
221
        DEBUGF("inflatehd: integer not fully decoded. current=%zu\n",
2082
221
               inflater->left);
2083
2084
221
        goto almost_ok;
2085
221
      }
2086
2087
16.9k
      if (inflater->huffman_encoded) {
2088
8.91k
        nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
2089
2090
8.91k
        inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF;
2091
2092
8.91k
        rv = nghttp2_rcbuf_new(
2093
8.91k
          &inflater->namercbuf,
2094
8.91k
          nghttp2_huff_estimate_decode_length(inflater->left) + 1, mem);
2095
8.91k
      } else {
2096
8.06k
        inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAME;
2097
8.06k
        rv = nghttp2_rcbuf_new(&inflater->namercbuf, inflater->left + 1, mem);
2098
8.06k
      }
2099
2100
16.9k
      if (rv != 0) {
2101
0
        goto fail;
2102
0
      }
2103
2104
16.9k
      nghttp2_buf_wrap_init(&inflater->namebuf, inflater->namercbuf->base,
2105
16.9k
                            inflater->namercbuf->len);
2106
2107
16.9k
      break;
2108
8.87k
    case NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF:
2109
8.87k
      rv = hd_inflate_read_huff(inflater, &inflater->namebuf, in, last);
2110
8.87k
      if (rv < 0) {
2111
13
        goto fail;
2112
13
      }
2113
2114
8.86k
      in += rv;
2115
2116
8.86k
      DEBUGF("inflatehd: %td bytes read\n", rv);
2117
2118
8.86k
      if (inflater->left) {
2119
56
        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
2120
2121
56
        goto almost_ok;
2122
56
      }
2123
2124
8.80k
      *inflater->namebuf.last = '\0';
2125
8.80k
      inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
2126
2127
8.80k
      inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2128
2129
8.80k
      break;
2130
8.02k
    case NGHTTP2_HD_STATE_NEWNAME_READ_NAME:
2131
8.02k
      rv = hd_inflate_read(inflater, &inflater->namebuf, in, last);
2132
8.02k
      if (rv < 0) {
2133
0
        goto fail;
2134
0
      }
2135
2136
8.02k
      in += rv;
2137
2138
8.02k
      DEBUGF("inflatehd: %td bytes read\n", rv);
2139
8.02k
      if (inflater->left) {
2140
70
        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
2141
2142
70
        goto almost_ok;
2143
70
      }
2144
2145
7.95k
      *inflater->namebuf.last = '\0';
2146
7.95k
      inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
2147
2148
7.95k
      inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2149
2150
7.95k
      break;
2151
26.9k
    case NGHTTP2_HD_STATE_CHECK_VALUELEN:
2152
26.9k
      hd_inflate_set_huffman_encoded(inflater, in);
2153
26.9k
      inflater->state = NGHTTP2_HD_STATE_READ_VALUELEN;
2154
26.9k
      inflater->left = 0;
2155
26.9k
      inflater->shift = 0;
2156
26.9k
      DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
2157
    /* Fall through */
2158
27.1k
    case NGHTTP2_HD_STATE_READ_VALUELEN:
2159
27.1k
      rfin = 0;
2160
27.1k
      rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
2161
27.1k
      if (rv < 0) {
2162
191
        goto fail;
2163
191
      }
2164
2165
26.9k
      in += rv;
2166
2167
26.9k
      if (!rfin) {
2168
201
        goto almost_ok;
2169
201
      }
2170
2171
26.7k
      DEBUGF("inflatehd: valuelen=%zu\n", inflater->left);
2172
2173
26.7k
      if (inflater->huffman_encoded) {
2174
5.94k
        nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
2175
2176
5.94k
        inflater->state = NGHTTP2_HD_STATE_READ_VALUEHUFF;
2177
2178
5.94k
        rv = nghttp2_rcbuf_new(
2179
5.94k
          &inflater->valuercbuf,
2180
5.94k
          nghttp2_huff_estimate_decode_length(inflater->left) + 1, mem);
2181
20.7k
      } else {
2182
20.7k
        inflater->state = NGHTTP2_HD_STATE_READ_VALUE;
2183
2184
20.7k
        rv = nghttp2_rcbuf_new(&inflater->valuercbuf, inflater->left + 1, mem);
2185
20.7k
      }
2186
2187
26.7k
      if (rv != 0) {
2188
0
        goto fail;
2189
0
      }
2190
2191
26.7k
      nghttp2_buf_wrap_init(&inflater->valuebuf, inflater->valuercbuf->base,
2192
26.7k
                            inflater->valuercbuf->len);
2193
2194
26.7k
      busy = 1;
2195
2196
26.7k
      break;
2197
5.95k
    case NGHTTP2_HD_STATE_READ_VALUEHUFF:
2198
5.95k
      rv = hd_inflate_read_huff(inflater, &inflater->valuebuf, in, last);
2199
5.95k
      if (rv < 0) {
2200
8
        goto fail;
2201
8
      }
2202
2203
5.95k
      in += rv;
2204
2205
5.95k
      DEBUGF("inflatehd: %td bytes read\n", rv);
2206
2207
5.95k
      if (inflater->left) {
2208
93
        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
2209
2210
93
        goto almost_ok;
2211
93
      }
2212
2213
5.85k
      *inflater->valuebuf.last = '\0';
2214
5.85k
      inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
2215
2216
5.85k
      if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
2217
1.66k
        rv = hd_inflate_commit_newname(inflater, nv_out);
2218
4.19k
      } else {
2219
4.19k
        rv = hd_inflate_commit_indname(inflater, nv_out);
2220
4.19k
      }
2221
2222
5.85k
      if (rv != 0) {
2223
0
        goto fail;
2224
0
      }
2225
2226
5.85k
      inflater->state = NGHTTP2_HD_STATE_OPCODE;
2227
5.85k
      *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2228
2229
5.85k
      return (nghttp2_ssize)(in - first);
2230
20.7k
    case NGHTTP2_HD_STATE_READ_VALUE:
2231
20.7k
      rv = hd_inflate_read(inflater, &inflater->valuebuf, in, last);
2232
20.7k
      if (rv < 0) {
2233
0
        DEBUGF("inflatehd: value read failure %td: %s\n", rv,
2234
0
               nghttp2_strerror((int)rv));
2235
0
        goto fail;
2236
0
      }
2237
2238
20.7k
      in += rv;
2239
2240
20.7k
      DEBUGF("inflatehd: %td bytes read\n", rv);
2241
2242
20.7k
      if (inflater->left) {
2243
109
        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
2244
109
        goto almost_ok;
2245
109
      }
2246
2247
20.6k
      *inflater->valuebuf.last = '\0';
2248
20.6k
      inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
2249
2250
20.6k
      if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
2251
15.0k
        rv = hd_inflate_commit_newname(inflater, nv_out);
2252
15.0k
      } else {
2253
5.61k
        rv = hd_inflate_commit_indname(inflater, nv_out);
2254
5.61k
      }
2255
2256
20.6k
      if (rv != 0) {
2257
0
        goto fail;
2258
0
      }
2259
2260
20.6k
      inflater->state = NGHTTP2_HD_STATE_OPCODE;
2261
20.6k
      *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2262
2263
20.6k
      return (nghttp2_ssize)(in - first);
2264
183k
    }
2265
183k
  }
2266
2267
71.6k
  assert(in == last);
2268
2269
16.7k
  DEBUGF("inflatehd: all input bytes were processed\n");
2270
2271
16.7k
  if (in_final) {
2272
13.7k
    DEBUGF("inflatehd: in_final set\n");
2273
2274
13.7k
    if (inflater->state != NGHTTP2_HD_STATE_OPCODE &&
2275
2.85k
        inflater->state != NGHTTP2_HD_STATE_INFLATE_START) {
2276
19
      DEBUGF("inflatehd: unacceptable state=%d\n", inflater->state);
2277
19
      rv = NGHTTP2_ERR_HEADER_COMP;
2278
2279
19
      goto fail;
2280
19
    }
2281
13.7k
    *inflate_flags |= NGHTTP2_HD_INFLATE_FINAL;
2282
13.7k
  }
2283
16.6k
  return (nghttp2_ssize)(in - first);
2284
2285
1.71k
almost_ok:
2286
1.71k
  if (in_final) {
2287
24
    DEBUGF("inflatehd: input ended prematurely\n");
2288
2289
24
    rv = NGHTTP2_ERR_HEADER_COMP;
2290
2291
24
    goto fail;
2292
24
  }
2293
1.68k
  return (nghttp2_ssize)(in - first);
2294
2295
1.09k
fail:
2296
1.09k
  DEBUGF("inflatehd: error return %td\n", rv);
2297
2298
1.09k
  inflater->ctx.bad = 1;
2299
1.09k
  return rv;
2300
1.71k
}
2301
2302
13.7k
int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) {
2303
13.7k
  hd_inflate_keep_free(inflater);
2304
13.7k
  inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
2305
13.7k
  return 0;
2306
13.7k
}
2307
2308
0
int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) {
2309
0
  return nghttp2_hd_inflate_new2(inflater_ptr, NULL);
2310
0
}
2311
2312
/* nghttp2_hd_inflater allocated by nghttp2_hd_inflate_new2().  The
2313
   application may free |mem| passed to nghttp2_hd_inflate_new2()
2314
   after it returns, so keep a copy of it along with inflater. */
2315
typedef struct {
2316
  nghttp2_hd_inflater inflater;
2317
  nghttp2_mem mem;
2318
} hd_inflater_with_mem;
2319
2320
int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
2321
0
                            nghttp2_mem *mem) {
2322
0
  int rv;
2323
0
  hd_inflater_with_mem *p;
2324
2325
0
  if (mem == NULL) {
2326
0
    mem = nghttp2_mem_default();
2327
0
  }
2328
2329
0
  p = nghttp2_mem_malloc(mem, sizeof(hd_inflater_with_mem));
2330
2331
0
  if (p == NULL) {
2332
0
    return NGHTTP2_ERR_NOMEM;
2333
0
  }
2334
2335
0
  p->mem = *mem;
2336
2337
0
  rv = nghttp2_hd_inflate_init(&p->inflater, &p->mem);
2338
2339
0
  if (rv != 0) {
2340
0
    nghttp2_mem_free(mem, p);
2341
2342
0
    return rv;
2343
0
  }
2344
2345
0
  *inflater_ptr = &p->inflater;
2346
2347
0
  return 0;
2348
0
}
2349
2350
0
void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) {
2351
0
  nghttp2_mem mem;
2352
2353
  /* inflater->ctx.mem points to the memory being freed */
2354
0
  mem = *inflater->ctx.mem;
2355
0
  nghttp2_hd_inflate_free(inflater);
2356
2357
0
  nghttp2_mem_free(&mem, inflater);
2358
0
}
2359
2360
int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,
2361
0
                                  const nghttp2_nv *nv, int indexing_mode) {
2362
0
  return emit_indname_block(bufs, idx, nv, indexing_mode);
2363
0
}
2364
2365
int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
2366
0
                                  int indexing_mode) {
2367
0
  return emit_newname_block(bufs, nv, indexing_mode);
2368
0
}
2369
2370
0
int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
2371
0
  return emit_table_size(bufs, table_size);
2372
0
}
2373
2374
nghttp2_ssize nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr,
2375
                                       int *fin, uint32_t initial, size_t shift,
2376
                                       uint8_t *in, uint8_t *last,
2377
0
                                       size_t prefix) {
2378
0
  return decode_length(res, shift_ptr, fin, initial, shift, in, last, prefix);
2379
0
}
2380
2381
static const nghttp2_nv *hd_get_table_entry(nghttp2_hd_context *context,
2382
0
                                            size_t idx) {
2383
0
  if (idx == 0) {
2384
0
    return NULL;
2385
0
  }
2386
2387
0
  --idx;
2388
2389
0
  if (!INDEX_RANGE_VALID(context, idx)) {
2390
0
    return NULL;
2391
0
  }
2392
2393
0
  return nghttp2_hd_table_get2(context, idx);
2394
0
}
2395
2396
0
size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater) {
2397
0
  return get_max_index(&deflater->ctx);
2398
0
}
2399
2400
const nghttp2_nv *
2401
0
nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx) {
2402
0
  return hd_get_table_entry(&deflater->ctx, idx);
2403
0
}
2404
2405
size_t
2406
0
nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater) {
2407
0
  return deflater->ctx.hd_table_bufsize;
2408
0
}
2409
2410
size_t
2411
0
nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater) {
2412
0
  return deflater->ctx.hd_table_bufsize_max;
2413
0
}
2414
2415
0
size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater) {
2416
0
  return get_max_index(&inflater->ctx);
2417
0
}
2418
2419
const nghttp2_nv *
2420
0
nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx) {
2421
0
  return hd_get_table_entry(&inflater->ctx, idx);
2422
0
}
2423
2424
size_t
2425
0
nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater) {
2426
0
  return inflater->ctx.hd_table_bufsize;
2427
0
}
2428
2429
size_t
2430
0
nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater) {
2431
0
  return inflater->ctx.hd_table_bufsize_max;
2432
0
}