3d6e1aa346b13c02de1ff56e61fce9cf46db7d14
[platform/upstream/grpc.git] / src / core / ext / transport / chttp2 / transport / hpack_parser.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
22 #include "src/core/ext/transport/chttp2/transport/internal.h"
23
24 #include <assert.h>
25 #include <stddef.h>
26 #include <string.h>
27
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33
34 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
35 #include "src/core/lib/debug/stats.h"
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/profiling/timers.h"
38 #include "src/core/lib/slice/slice_internal.h"
39 #include "src/core/lib/slice/slice_string_helpers.h"
40 #include "src/core/lib/surface/validate_metadata.h"
41 #include "src/core/lib/transport/http2_errors.h"
42
43 #if __cplusplus > 201103L
44 #define GRPC_HPACK_CONSTEXPR_FN constexpr
45 #define GRPC_HPACK_CONSTEXPR_VALUE constexpr
46 #else
47 #define GRPC_HPACK_CONSTEXPR_FN
48 #define GRPC_HPACK_CONSTEXPR_VALUE const
49 #endif
50
51 namespace grpc_core {
52
53 DebugOnlyTraceFlag grpc_trace_chttp2_hpack_parser(false, "chttp2_hpack_parser");
54
55 /* How parsing works:
56
57    The parser object keeps track of a function pointer which represents the
58    current parse state.
59
60    Each time new bytes are presented, we call into the current state, which
61    recursively parses until all bytes in the given chunk are exhausted.
62
63    The parse state that terminates then saves its function pointer to be the
64    current state so that it can resume when more bytes are available.
65
66    It's expected that most optimizing compilers will turn this code into
67    a set of indirect jumps, and so not waste stack space. */
68
69 /* state table for huffman decoding: given a state, gives an index/16 into
70    next_sub_tbl. Taking that index and adding the value of the nibble being
71    considered returns the next state.
72
73    generated by gen_hpack_tables.c */
74 static const uint8_t next_tbl[256] = {
75     0,  1,  2,  3,  4,  1,  2, 5,  6,  1, 7,  8,  1,  3,  3,  9,  10, 11, 1,  1,
76     1,  12, 1,  2,  13, 1,  1, 1,  1,  1, 1,  1,  1,  1,  1,  1,  1,  1,  1,  2,
77     14, 1,  15, 16, 1,  17, 1, 15, 2,  7, 3,  18, 19, 1,  1,  1,  1,  20, 1,  1,
78     1,  1,  1,  1,  1,  1,  1, 1,  15, 2, 2,  7,  21, 1,  22, 1,  1,  1,  1,  1,
79     1,  1,  1,  15, 2,  2,  2, 2,  2,  2, 23, 24, 25, 1,  1,  1,  1,  2,  2,  2,
80     26, 3,  3,  27, 10, 28, 1, 1,  1,  1, 1,  1,  2,  3,  29, 10, 30, 1,  1,  1,
81     1,  1,  1,  1,  1,  1,  1, 1,  1,  1, 1,  31, 1,  1,  1,  1,  1,  1,  1,  2,
82     2,  2,  2,  2,  2,  2,  2, 32, 1,  1, 15, 33, 1,  34, 35, 9,  36, 1,  1,  1,
83     1,  1,  1,  1,  37, 1,  1, 1,  1,  1, 1,  2,  2,  2,  2,  2,  2,  2,  26, 9,
84     38, 1,  1,  1,  1,  1,  1, 1,  15, 2, 2,  2,  2,  26, 3,  3,  39, 1,  1,  1,
85     1,  1,  1,  1,  1,  1,  1, 1,  2,  2, 2,  2,  2,  2,  7,  3,  3,  3,  40, 2,
86     41, 1,  1,  1,  42, 43, 1, 1,  44, 1, 1,  1,  1,  15, 2,  2,  2,  2,  2,  2,
87     3,  3,  3,  45, 46, 1,  1, 2,  2,  2, 35, 3,  3,  18, 47, 2,
88 };
89
90 /* next state, based upon current state and the current nibble: see above.
91    generated by gen_hpack_tables.c */
92 static const int16_t next_sub_tbl[48 * 16] = {
93     1,   204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
94     218, 2,   6,   10,  13,  14,  15,  16,  17,  2,   6,   10,  13,  14,  15,
95     16,  17,  3,   7,   11,  24,  3,   7,   11,  24,  3,   7,   11,  24,  3,
96     7,   11,  24,  4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,
97     4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   5,
98     199, 200, 201, 202, 203, 4,   8,   4,   8,   0,   0,   0,   0,   0,   0,
99     0,   0,   0,   0,   0,   0,   9,   133, 134, 135, 136, 137, 138, 139, 140,
100     141, 142, 143, 144, 145, 146, 147, 3,   7,   11,  24,  3,   7,   11,  24,
101     4,   8,   4,   8,   4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,
102     0,   0,   0,   0,   0,   0,   0,   12,  132, 4,   8,   4,   8,   4,   8,
103     4,   8,   4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   0,
104     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
105     0,   0,   0,   0,   0,   0,   0,   0,   18,  19,  20,  21,  4,   8,   4,
106     8,   4,   8,   4,   8,   4,   8,   0,   0,   0,   22,  23,  91,  25,  26,
107     27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  3,
108     7,   11,  24,  3,   7,   11,  24,  0,   0,   0,   0,   0,   41,  42,  43,
109     2,   6,   10,  13,  14,  15,  16,  17,  3,   7,   11,  24,  3,   7,   11,
110     24,  4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   0,   0,
111     44,  45,  2,   6,   10,  13,  14,  15,  16,  17,  46,  47,  48,  49,  50,
112     51,  52,  57,  4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
113     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
114     0,   53,  54,  55,  56,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,
115     68,  69,  70,  71,  72,  74,  0,   0,   0,   0,   0,   0,   0,   0,   0,
116     0,   0,   0,   0,   0,   0,   73,  75,  76,  77,  78,  79,  80,  81,  82,
117     83,  84,  85,  86,  87,  88,  89,  90,  3,   7,   11,  24,  3,   7,   11,
118     24,  3,   7,   11,  24,  0,   0,   0,   0,   3,   7,   11,  24,  3,   7,
119     11,  24,  4,   8,   4,   8,   0,   0,   0,   92,  0,   0,   0,   93,  94,
120     95,  96,  97,  98,  99,  100, 101, 102, 103, 104, 105, 3,   7,   11,  24,
121     4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,
122     8,   4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
123     0,   0,   0,   106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 4,
124     8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   0,   0,
125     0,   117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
126     131, 2,   6,   10,  13,  14,  15,  16,  17,  4,   8,   4,   8,   4,   8,
127     4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   148,
128     149, 150, 151, 3,   7,   11,  24,  4,   8,   4,   8,   0,   0,   0,   0,
129     0,   0,   152, 153, 3,   7,   11,  24,  3,   7,   11,  24,  3,   7,   11,
130     24,  154, 155, 156, 164, 3,   7,   11,  24,  3,   7,   11,  24,  3,   7,
131     11,  24,  4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   0,
132     157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 169, 170, 171, 172,
133     173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
134     188, 189, 190, 191, 192, 193, 194, 195, 196, 4,   8,   4,   8,   4,   8,
135     4,   8,   4,   8,   4,   8,   4,   8,   197, 198, 4,   8,   4,   8,   4,
136     8,   4,   8,   0,   0,   0,   0,   0,   0,   219, 220, 3,   7,   11,  24,
137     4,   8,   4,   8,   4,   8,   0,   0,   221, 222, 223, 224, 3,   7,   11,
138     24,  3,   7,   11,  24,  4,   8,   4,   8,   4,   8,   225, 228, 4,   8,
139     4,   8,   4,   8,   0,   0,   0,   0,   0,   0,   0,   0,   226, 227, 229,
140     230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
141     4,   8,   4,   8,   4,   8,   4,   8,   4,   8,   0,   0,   0,   0,   0,
142     0,   0,   0,   0,   0,   0,   0,   245, 246, 247, 248, 249, 250, 251, 252,
143     253, 254, 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
144     0,   0,   255,
145 };
146
147 /* emission table: indexed like next_tbl, ultimately gives the byte to be
148    emitted, or -1 for no byte, or 256 for end of stream
149
150    generated by gen_hpack_tables.c */
151 static const uint16_t emit_tbl[256] = {
152     0,   1,   2,   3,   4,   5,   6,   7,   0,   8,   9,   10,  11,  12,  13,
153     14,  15,  16,  17,  18,  19,  20,  21,  22,  0,   23,  24,  25,  26,  27,
154     28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
155     43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  0,   55,  56,
156     57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  0,
157     71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,
158     86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,  99,  100,
159     101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
160     116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
161     131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
162     146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 0,
163     160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
164     0,   175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
165     189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
166     204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218,
167     219, 220, 221, 0,   222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
168     233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
169     248,
170 };
171
172 /* generated by gen_hpack_tables.c */
173 static const int16_t emit_sub_tbl[249 * 16] = {
174     -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
175     -1,  48,  48,  48,  48,  48,  48,  48,  48,  49,  49,  49,  49,  49,  49,
176     49,  49,  48,  48,  48,  48,  49,  49,  49,  49,  50,  50,  50,  50,  97,
177     97,  97,  97,  48,  48,  49,  49,  50,  50,  97,  97,  99,  99,  101, 101,
178     105, 105, 111, 111, 48,  49,  50,  97,  99,  101, 105, 111, 115, 116, -1,
179     -1,  -1,  -1,  -1,  -1,  32,  32,  32,  32,  32,  32,  32,  32,  37,  37,
180     37,  37,  37,  37,  37,  37,  99,  99,  99,  99,  101, 101, 101, 101, 105,
181     105, 105, 105, 111, 111, 111, 111, 115, 115, 116, 116, 32,  37,  45,  46,
182     47,  51,  52,  53,  54,  55,  56,  57,  61,  61,  61,  61,  61,  61,  61,
183     61,  65,  65,  65,  65,  65,  65,  65,  65,  115, 115, 115, 115, 116, 116,
184     116, 116, 32,  32,  37,  37,  45,  45,  46,  46,  61,  65,  95,  98,  100,
185     102, 103, 104, 108, 109, 110, 112, 114, 117, -1,  -1,  58,  58,  58,  58,
186     58,  58,  58,  58,  66,  66,  66,  66,  66,  66,  66,  66,  47,  47,  51,
187     51,  52,  52,  53,  53,  54,  54,  55,  55,  56,  56,  57,  57,  61,  61,
188     65,  65,  95,  95,  98,  98,  100, 100, 102, 102, 103, 103, 104, 104, 108,
189     108, 109, 109, 110, 110, 112, 112, 114, 114, 117, 117, 58,  66,  67,  68,
190     69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,
191     84,  85,  86,  87,  89,  106, 107, 113, 118, 119, 120, 121, 122, -1,  -1,
192     -1,  -1,  38,  38,  38,  38,  38,  38,  38,  38,  42,  42,  42,  42,  42,
193     42,  42,  42,  44,  44,  44,  44,  44,  44,  44,  44,  59,  59,  59,  59,
194     59,  59,  59,  59,  88,  88,  88,  88,  88,  88,  88,  88,  90,  90,  90,
195     90,  90,  90,  90,  90,  33,  33,  34,  34,  40,  40,  41,  41,  63,  63,
196     39,  43,  124, -1,  -1,  -1,  35,  35,  35,  35,  35,  35,  35,  35,  62,
197     62,  62,  62,  62,  62,  62,  62,  0,   0,   0,   0,   36,  36,  36,  36,
198     64,  64,  64,  64,  91,  91,  91,  91,  69,  69,  69,  69,  69,  69,  69,
199     69,  70,  70,  70,  70,  70,  70,  70,  70,  71,  71,  71,  71,  71,  71,
200     71,  71,  72,  72,  72,  72,  72,  72,  72,  72,  73,  73,  73,  73,  73,
201     73,  73,  73,  74,  74,  74,  74,  74,  74,  74,  74,  75,  75,  75,  75,
202     75,  75,  75,  75,  76,  76,  76,  76,  76,  76,  76,  76,  77,  77,  77,
203     77,  77,  77,  77,  77,  78,  78,  78,  78,  78,  78,  78,  78,  79,  79,
204     79,  79,  79,  79,  79,  79,  80,  80,  80,  80,  80,  80,  80,  80,  81,
205     81,  81,  81,  81,  81,  81,  81,  82,  82,  82,  82,  82,  82,  82,  82,
206     83,  83,  83,  83,  83,  83,  83,  83,  84,  84,  84,  84,  84,  84,  84,
207     84,  85,  85,  85,  85,  85,  85,  85,  85,  86,  86,  86,  86,  86,  86,
208     86,  86,  87,  87,  87,  87,  87,  87,  87,  87,  89,  89,  89,  89,  89,
209     89,  89,  89,  106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
210     107, 107, 107, 107, 113, 113, 113, 113, 113, 113, 113, 113, 118, 118, 118,
211     118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 119, 120, 120,
212     120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 122,
213     122, 122, 122, 122, 122, 122, 122, 38,  38,  38,  38,  42,  42,  42,  42,
214     44,  44,  44,  44,  59,  59,  59,  59,  88,  88,  88,  88,  90,  90,  90,
215     90,  33,  34,  40,  41,  63,  -1,  -1,  -1,  39,  39,  39,  39,  39,  39,
216     39,  39,  43,  43,  43,  43,  43,  43,  43,  43,  124, 124, 124, 124, 124,
217     124, 124, 124, 35,  35,  35,  35,  62,  62,  62,  62,  0,   0,   36,  36,
218     64,  64,  91,  91,  93,  93,  126, 126, 94,  125, -1,  -1,  60,  60,  60,
219     60,  60,  60,  60,  60,  96,  96,  96,  96,  96,  96,  96,  96,  123, 123,
220     123, 123, 123, 123, 123, 123, -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  92,
221     92,  92,  92,  92,  92,  92,  92,  195, 195, 195, 195, 195, 195, 195, 195,
222     208, 208, 208, 208, 208, 208, 208, 208, 128, 128, 128, 128, 130, 130, 130,
223     130, 131, 131, 131, 131, 162, 162, 162, 162, 184, 184, 184, 184, 194, 194,
224     194, 194, 224, 224, 224, 224, 226, 226, 226, 226, 153, 153, 161, 161, 167,
225     167, 172, 172, 176, 176, 177, 177, 179, 179, 209, 209, 216, 216, 217, 217,
226     227, 227, 229, 229, 230, 230, 129, 132, 133, 134, 136, 146, 154, 156, 160,
227     163, 164, 169, 170, 173, 178, 181, 185, 186, 187, 189, 190, 196, 198, 228,
228     232, 233, -1,  -1,  -1,  -1,  1,   1,   1,   1,   1,   1,   1,   1,   135,
229     135, 135, 135, 135, 135, 135, 135, 137, 137, 137, 137, 137, 137, 137, 137,
230     138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139,
231     139, 140, 140, 140, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141,
232     141, 141, 143, 143, 143, 143, 143, 143, 143, 143, 147, 147, 147, 147, 147,
233     147, 147, 147, 149, 149, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150,
234     150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152,
235     152, 152, 152, 152, 152, 155, 155, 155, 155, 155, 155, 155, 155, 157, 157,
236     157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 158, 165,
237     165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 166, 166, 166,
238     168, 168, 168, 168, 168, 168, 168, 168, 174, 174, 174, 174, 174, 174, 174,
239     174, 175, 175, 175, 175, 175, 175, 175, 175, 180, 180, 180, 180, 180, 180,
240     180, 180, 182, 182, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183,
241     183, 183, 183, 188, 188, 188, 188, 188, 188, 188, 188, 191, 191, 191, 191,
242     191, 191, 191, 191, 197, 197, 197, 197, 197, 197, 197, 197, 231, 231, 231,
243     231, 231, 231, 231, 231, 239, 239, 239, 239, 239, 239, 239, 239, 9,   9,
244     9,   9,   142, 142, 142, 142, 144, 144, 144, 144, 145, 145, 145, 145, 148,
245     148, 148, 148, 159, 159, 159, 159, 171, 171, 171, 171, 206, 206, 206, 206,
246     215, 215, 215, 215, 225, 225, 225, 225, 236, 236, 236, 236, 237, 237, 237,
247     237, 199, 199, 207, 207, 234, 234, 235, 235, 192, 193, 200, 201, 202, 205,
248     210, 213, 218, 219, 238, 240, 242, 243, 255, -1,  203, 203, 203, 203, 203,
249     203, 203, 203, 204, 204, 204, 204, 204, 204, 204, 204, 211, 211, 211, 211,
250     211, 211, 211, 211, 212, 212, 212, 212, 212, 212, 212, 212, 214, 214, 214,
251     214, 214, 214, 214, 214, 221, 221, 221, 221, 221, 221, 221, 221, 222, 222,
252     222, 222, 222, 222, 222, 222, 223, 223, 223, 223, 223, 223, 223, 223, 241,
253     241, 241, 241, 241, 241, 241, 241, 244, 244, 244, 244, 244, 244, 244, 244,
254     245, 245, 245, 245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 246, 246,
255     246, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 248, 248,
256     248, 248, 250, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251,
257     251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, 253,
258     253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 2,   2,   2,
259     2,   3,   3,   3,   3,   4,   4,   4,   4,   5,   5,   5,   5,   6,   6,
260     6,   6,   7,   7,   7,   7,   8,   8,   8,   8,   11,  11,  11,  11,  12,
261     12,  12,  12,  14,  14,  14,  14,  15,  15,  15,  15,  16,  16,  16,  16,
262     17,  17,  17,  17,  18,  18,  18,  18,  19,  19,  19,  19,  20,  20,  20,
263     20,  21,  21,  21,  21,  23,  23,  23,  23,  24,  24,  24,  24,  25,  25,
264     25,  25,  26,  26,  26,  26,  27,  27,  27,  27,  28,  28,  28,  28,  29,
265     29,  29,  29,  30,  30,  30,  30,  31,  31,  31,  31,  127, 127, 127, 127,
266     220, 220, 220, 220, 249, 249, 249, 249, 10,  13,  22,  256, 93,  93,  93,
267     93,  126, 126, 126, 126, 94,  94,  125, 125, 60,  96,  123, -1,  92,  195,
268     208, -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  128,
269     128, 128, 128, 128, 128, 128, 128, 130, 130, 130, 130, 130, 130, 130, 130,
270     131, 131, 131, 131, 131, 131, 131, 131, 162, 162, 162, 162, 162, 162, 162,
271     162, 184, 184, 184, 184, 184, 184, 184, 184, 194, 194, 194, 194, 194, 194,
272     194, 194, 224, 224, 224, 224, 224, 224, 224, 224, 226, 226, 226, 226, 226,
273     226, 226, 226, 153, 153, 153, 153, 161, 161, 161, 161, 167, 167, 167, 167,
274     172, 172, 172, 172, 176, 176, 176, 176, 177, 177, 177, 177, 179, 179, 179,
275     179, 209, 209, 209, 209, 216, 216, 216, 216, 217, 217, 217, 217, 227, 227,
276     227, 227, 229, 229, 229, 229, 230, 230, 230, 230, 129, 129, 132, 132, 133,
277     133, 134, 134, 136, 136, 146, 146, 154, 154, 156, 156, 160, 160, 163, 163,
278     164, 164, 169, 169, 170, 170, 173, 173, 178, 178, 181, 181, 185, 185, 186,
279     186, 187, 187, 189, 189, 190, 190, 196, 196, 198, 198, 228, 228, 232, 232,
280     233, 233, 1,   135, 137, 138, 139, 140, 141, 143, 147, 149, 150, 151, 152,
281     155, 157, 158, 165, 166, 168, 174, 175, 180, 182, 183, 188, 191, 197, 231,
282     239, -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  9,   9,   9,
283     9,   9,   9,   9,   9,   142, 142, 142, 142, 142, 142, 142, 142, 144, 144,
284     144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 148,
285     148, 148, 148, 148, 148, 148, 148, 159, 159, 159, 159, 159, 159, 159, 159,
286     171, 171, 171, 171, 171, 171, 171, 171, 206, 206, 206, 206, 206, 206, 206,
287     206, 215, 215, 215, 215, 215, 215, 215, 215, 225, 225, 225, 225, 225, 225,
288     225, 225, 236, 236, 236, 236, 236, 236, 236, 236, 237, 237, 237, 237, 237,
289     237, 237, 237, 199, 199, 199, 199, 207, 207, 207, 207, 234, 234, 234, 234,
290     235, 235, 235, 235, 192, 192, 193, 193, 200, 200, 201, 201, 202, 202, 205,
291     205, 210, 210, 213, 213, 218, 218, 219, 219, 238, 238, 240, 240, 242, 242,
292     243, 243, 255, 255, 203, 204, 211, 212, 214, 221, 222, 223, 241, 244, 245,
293     246, 247, 248, 250, 251, 252, 253, 254, -1,  -1,  -1,  -1,  -1,  -1,  -1,
294     -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  2,   2,   2,   2,   2,   2,   2,
295     2,   3,   3,   3,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   4,
296     4,   4,   5,   5,   5,   5,   5,   5,   5,   5,   6,   6,   6,   6,   6,
297     6,   6,   6,   7,   7,   7,   7,   7,   7,   7,   7,   8,   8,   8,   8,
298     8,   8,   8,   8,   11,  11,  11,  11,  11,  11,  11,  11,  12,  12,  12,
299     12,  12,  12,  12,  12,  14,  14,  14,  14,  14,  14,  14,  14,  15,  15,
300     15,  15,  15,  15,  15,  15,  16,  16,  16,  16,  16,  16,  16,  16,  17,
301     17,  17,  17,  17,  17,  17,  17,  18,  18,  18,  18,  18,  18,  18,  18,
302     19,  19,  19,  19,  19,  19,  19,  19,  20,  20,  20,  20,  20,  20,  20,
303     20,  21,  21,  21,  21,  21,  21,  21,  21,  23,  23,  23,  23,  23,  23,
304     23,  23,  24,  24,  24,  24,  24,  24,  24,  24,  25,  25,  25,  25,  25,
305     25,  25,  25,  26,  26,  26,  26,  26,  26,  26,  26,  27,  27,  27,  27,
306     27,  27,  27,  27,  28,  28,  28,  28,  28,  28,  28,  28,  29,  29,  29,
307     29,  29,  29,  29,  29,  30,  30,  30,  30,  30,  30,  30,  30,  31,  31,
308     31,  31,  31,  31,  31,  31,  127, 127, 127, 127, 127, 127, 127, 127, 220,
309     220, 220, 220, 220, 220, 220, 220, 249, 249, 249, 249, 249, 249, 249, 249,
310     10,  10,  13,  13,  22,  22,  256, 256, 67,  67,  67,  67,  67,  67,  67,
311     67,  68,  68,  68,  68,  68,  68,  68,  68,  95,  95,  95,  95,  95,  95,
312     95,  95,  98,  98,  98,  98,  98,  98,  98,  98,  100, 100, 100, 100, 100,
313     100, 100, 100, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103,
314     103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 108, 108, 108,
315     108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 110, 110,
316     110, 110, 110, 110, 110, 110, 112, 112, 112, 112, 112, 112, 112, 112, 114,
317     114, 114, 114, 114, 114, 114, 114, 117, 117, 117, 117, 117, 117, 117, 117,
318     58,  58,  58,  58,  66,  66,  66,  66,  67,  67,  67,  67,  68,  68,  68,
319     68,  69,  69,  69,  69,  70,  70,  70,  70,  71,  71,  71,  71,  72,  72,
320     72,  72,  73,  73,  73,  73,  74,  74,  74,  74,  75,  75,  75,  75,  76,
321     76,  76,  76,  77,  77,  77,  77,  78,  78,  78,  78,  79,  79,  79,  79,
322     80,  80,  80,  80,  81,  81,  81,  81,  82,  82,  82,  82,  83,  83,  83,
323     83,  84,  84,  84,  84,  85,  85,  85,  85,  86,  86,  86,  86,  87,  87,
324     87,  87,  89,  89,  89,  89,  106, 106, 106, 106, 107, 107, 107, 107, 113,
325     113, 113, 113, 118, 118, 118, 118, 119, 119, 119, 119, 120, 120, 120, 120,
326     121, 121, 121, 121, 122, 122, 122, 122, 38,  38,  42,  42,  44,  44,  59,
327     59,  88,  88,  90,  90,  -1,  -1,  -1,  -1,  33,  33,  33,  33,  33,  33,
328     33,  33,  34,  34,  34,  34,  34,  34,  34,  34,  40,  40,  40,  40,  40,
329     40,  40,  40,  41,  41,  41,  41,  41,  41,  41,  41,  63,  63,  63,  63,
330     63,  63,  63,  63,  39,  39,  39,  39,  43,  43,  43,  43,  124, 124, 124,
331     124, 35,  35,  62,  62,  0,   36,  64,  91,  93,  126, -1,  -1,  94,  94,
332     94,  94,  94,  94,  94,  94,  125, 125, 125, 125, 125, 125, 125, 125, 60,
333     60,  60,  60,  96,  96,  96,  96,  123, 123, 123, 123, -1,  -1,  -1,  -1,
334     92,  92,  92,  92,  195, 195, 195, 195, 208, 208, 208, 208, 128, 128, 130,
335     130, 131, 131, 162, 162, 184, 184, 194, 194, 224, 224, 226, 226, 153, 161,
336     167, 172, 176, 177, 179, 209, 216, 217, 227, 229, 230, -1,  -1,  -1,  -1,
337     -1,  -1,  -1,  129, 129, 129, 129, 129, 129, 129, 129, 132, 132, 132, 132,
338     132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134,
339     134, 134, 134, 134, 134, 136, 136, 136, 136, 136, 136, 136, 136, 146, 146,
340     146, 146, 146, 146, 146, 146, 154, 154, 154, 154, 154, 154, 154, 154, 156,
341     156, 156, 156, 156, 156, 156, 156, 160, 160, 160, 160, 160, 160, 160, 160,
342     163, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164, 164,
343     164, 169, 169, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 170, 170,
344     170, 170, 173, 173, 173, 173, 173, 173, 173, 173, 178, 178, 178, 178, 178,
345     178, 178, 178, 181, 181, 181, 181, 181, 181, 181, 181, 185, 185, 185, 185,
346     185, 185, 185, 185, 186, 186, 186, 186, 186, 186, 186, 186, 187, 187, 187,
347     187, 187, 187, 187, 187, 189, 189, 189, 189, 189, 189, 189, 189, 190, 190,
348     190, 190, 190, 190, 190, 190, 196, 196, 196, 196, 196, 196, 196, 196, 198,
349     198, 198, 198, 198, 198, 198, 198, 228, 228, 228, 228, 228, 228, 228, 228,
350     232, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 233, 233, 233,
351     233, 1,   1,   1,   1,   135, 135, 135, 135, 137, 137, 137, 137, 138, 138,
352     138, 138, 139, 139, 139, 139, 140, 140, 140, 140, 141, 141, 141, 141, 143,
353     143, 143, 143, 147, 147, 147, 147, 149, 149, 149, 149, 150, 150, 150, 150,
354     151, 151, 151, 151, 152, 152, 152, 152, 155, 155, 155, 155, 157, 157, 157,
355     157, 158, 158, 158, 158, 165, 165, 165, 165, 166, 166, 166, 166, 168, 168,
356     168, 168, 174, 174, 174, 174, 175, 175, 175, 175, 180, 180, 180, 180, 182,
357     182, 182, 182, 183, 183, 183, 183, 188, 188, 188, 188, 191, 191, 191, 191,
358     197, 197, 197, 197, 231, 231, 231, 231, 239, 239, 239, 239, 9,   9,   142,
359     142, 144, 144, 145, 145, 148, 148, 159, 159, 171, 171, 206, 206, 215, 215,
360     225, 225, 236, 236, 237, 237, 199, 207, 234, 235, 192, 192, 192, 192, 192,
361     192, 192, 192, 193, 193, 193, 193, 193, 193, 193, 193, 200, 200, 200, 200,
362     200, 200, 200, 200, 201, 201, 201, 201, 201, 201, 201, 201, 202, 202, 202,
363     202, 202, 202, 202, 202, 205, 205, 205, 205, 205, 205, 205, 205, 210, 210,
364     210, 210, 210, 210, 210, 210, 213, 213, 213, 213, 213, 213, 213, 213, 218,
365     218, 218, 218, 218, 218, 218, 218, 219, 219, 219, 219, 219, 219, 219, 219,
366     238, 238, 238, 238, 238, 238, 238, 238, 240, 240, 240, 240, 240, 240, 240,
367     240, 242, 242, 242, 242, 242, 242, 242, 242, 243, 243, 243, 243, 243, 243,
368     243, 243, 255, 255, 255, 255, 255, 255, 255, 255, 203, 203, 203, 203, 204,
369     204, 204, 204, 211, 211, 211, 211, 212, 212, 212, 212, 214, 214, 214, 214,
370     221, 221, 221, 221, 222, 222, 222, 222, 223, 223, 223, 223, 241, 241, 241,
371     241, 244, 244, 244, 244, 245, 245, 245, 245, 246, 246, 246, 246, 247, 247,
372     247, 247, 248, 248, 248, 248, 250, 250, 250, 250, 251, 251, 251, 251, 252,
373     252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 2,   2,   3,   3,
374     4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   11,  11,  12,  12,  14,
375     14,  15,  15,  16,  16,  17,  17,  18,  18,  19,  19,  20,  20,  21,  21,
376     23,  23,  24,  24,  25,  25,  26,  26,  27,  27,  28,  28,  29,  29,  30,
377     30,  31,  31,  127, 127, 220, 220, 249, 249, -1,  -1,  10,  10,  10,  10,
378     10,  10,  10,  10,  13,  13,  13,  13,  13,  13,  13,  13,  22,  22,  22,
379     22,  22,  22,  22,  22,  256, 256, 256, 256, 256, 256, 256, 256, 45,  45,
380     45,  45,  45,  45,  45,  45,  46,  46,  46,  46,  46,  46,  46,  46,  47,
381     47,  47,  47,  47,  47,  47,  47,  51,  51,  51,  51,  51,  51,  51,  51,
382     52,  52,  52,  52,  52,  52,  52,  52,  53,  53,  53,  53,  53,  53,  53,
383     53,  54,  54,  54,  54,  54,  54,  54,  54,  55,  55,  55,  55,  55,  55,
384     55,  55,  56,  56,  56,  56,  56,  56,  56,  56,  57,  57,  57,  57,  57,
385     57,  57,  57,  50,  50,  50,  50,  50,  50,  50,  50,  97,  97,  97,  97,
386     97,  97,  97,  97,  99,  99,  99,  99,  99,  99,  99,  99,  101, 101, 101,
387     101, 101, 101, 101, 101, 105, 105, 105, 105, 105, 105, 105, 105, 111, 111,
388     111, 111, 111, 111, 111, 111, 115, 115, 115, 115, 115, 115, 115, 115, 116,
389     116, 116, 116, 116, 116, 116, 116, 32,  32,  32,  32,  37,  37,  37,  37,
390     45,  45,  45,  45,  46,  46,  46,  46,  47,  47,  47,  47,  51,  51,  51,
391     51,  52,  52,  52,  52,  53,  53,  53,  53,  54,  54,  54,  54,  55,  55,
392     55,  55,  56,  56,  56,  56,  57,  57,  57,  57,  61,  61,  61,  61,  65,
393     65,  65,  65,  95,  95,  95,  95,  98,  98,  98,  98,  100, 100, 100, 100,
394     102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 104, 104, 108, 108, 108,
395     108, 109, 109, 109, 109, 110, 110, 110, 110, 112, 112, 112, 112, 114, 114,
396     114, 114, 117, 117, 117, 117, 58,  58,  66,  66,  67,  67,  68,  68,  69,
397     69,  70,  70,  71,  71,  72,  72,  73,  73,  74,  74,  75,  75,  76,  76,
398     77,  77,  78,  78,  79,  79,  80,  80,  81,  81,  82,  82,  83,  83,  84,
399     84,  85,  85,  86,  86,  87,  87,  89,  89,  106, 106, 107, 107, 113, 113,
400     118, 118, 119, 119, 120, 120, 121, 121, 122, 122, 38,  42,  44,  59,  88,
401     90,  -1,  -1,  33,  33,  33,  33,  34,  34,  34,  34,  40,  40,  40,  40,
402     41,  41,  41,  41,  63,  63,  63,  63,  39,  39,  43,  43,  124, 124, 35,
403     62,  -1,  -1,  -1,  -1,  0,   0,   0,   0,   0,   0,   0,   0,   36,  36,
404     36,  36,  36,  36,  36,  36,  64,  64,  64,  64,  64,  64,  64,  64,  91,
405     91,  91,  91,  91,  91,  91,  91,  93,  93,  93,  93,  93,  93,  93,  93,
406     126, 126, 126, 126, 126, 126, 126, 126, 94,  94,  94,  94,  125, 125, 125,
407     125, 60,  60,  96,  96,  123, 123, -1,  -1,  92,  92,  195, 195, 208, 208,
408     128, 130, 131, 162, 184, 194, 224, 226, -1,  -1,  153, 153, 153, 153, 153,
409     153, 153, 153, 161, 161, 161, 161, 161, 161, 161, 161, 167, 167, 167, 167,
410     167, 167, 167, 167, 172, 172, 172, 172, 172, 172, 172, 172, 176, 176, 176,
411     176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 177, 177, 179, 179,
412     179, 179, 179, 179, 179, 179, 209, 209, 209, 209, 209, 209, 209, 209, 216,
413     216, 216, 216, 216, 216, 216, 216, 217, 217, 217, 217, 217, 217, 217, 217,
414     227, 227, 227, 227, 227, 227, 227, 227, 229, 229, 229, 229, 229, 229, 229,
415     229, 230, 230, 230, 230, 230, 230, 230, 230, 129, 129, 129, 129, 132, 132,
416     132, 132, 133, 133, 133, 133, 134, 134, 134, 134, 136, 136, 136, 136, 146,
417     146, 146, 146, 154, 154, 154, 154, 156, 156, 156, 156, 160, 160, 160, 160,
418     163, 163, 163, 163, 164, 164, 164, 164, 169, 169, 169, 169, 170, 170, 170,
419     170, 173, 173, 173, 173, 178, 178, 178, 178, 181, 181, 181, 181, 185, 185,
420     185, 185, 186, 186, 186, 186, 187, 187, 187, 187, 189, 189, 189, 189, 190,
421     190, 190, 190, 196, 196, 196, 196, 198, 198, 198, 198, 228, 228, 228, 228,
422     232, 232, 232, 232, 233, 233, 233, 233, 1,   1,   135, 135, 137, 137, 138,
423     138, 139, 139, 140, 140, 141, 141, 143, 143, 147, 147, 149, 149, 150, 150,
424     151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 165, 165, 166, 166, 168,
425     168, 174, 174, 175, 175, 180, 180, 182, 182, 183, 183, 188, 188, 191, 191,
426     197, 197, 231, 231, 239, 239, 9,   142, 144, 145, 148, 159, 171, 206, 215,
427     225, 236, 237, -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  199, 199,
428     199, 199, 199, 199, 199, 199, 207, 207, 207, 207, 207, 207, 207, 207, 234,
429     234, 234, 234, 234, 234, 234, 234, 235, 235, 235, 235, 235, 235, 235, 235,
430     192, 192, 192, 192, 193, 193, 193, 193, 200, 200, 200, 200, 201, 201, 201,
431     201, 202, 202, 202, 202, 205, 205, 205, 205, 210, 210, 210, 210, 213, 213,
432     213, 213, 218, 218, 218, 218, 219, 219, 219, 219, 238, 238, 238, 238, 240,
433     240, 240, 240, 242, 242, 242, 242, 243, 243, 243, 243, 255, 255, 255, 255,
434     203, 203, 204, 204, 211, 211, 212, 212, 214, 214, 221, 221, 222, 222, 223,
435     223, 241, 241, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 250, 250,
436     251, 251, 252, 252, 253, 253, 254, 254, 2,   3,   4,   5,   6,   7,   8,
437     11,  12,  14,  15,  16,  17,  18,  19,  20,  21,  23,  24,  25,  26,  27,
438     28,  29,  30,  31,  127, 220, 249, -1,  10,  10,  10,  10,  13,  13,  13,
439     13,  22,  22,  22,  22,  256, 256, 256, 256,
440 };
441
442 namespace {
443 // The alphabet used for base64 encoding binary metadata.
444 static constexpr char kBase64Alphabet[] =
445     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
446
447 // An inverted table: for each value in kBase64Alphabet, table contains the
448 // index with which it's stored, so we can quickly invert the encoding without
449 // any complicated runtime logic.
450 struct Base64InverseTable {
451   uint8_t table[256]{};
452   GRPC_HPACK_CONSTEXPR_FN Base64InverseTable() {
453     for (int i = 0; i < 256; i++) {
454       table[i] = 255;
455     }
456     for (const char* p = kBase64Alphabet; *p; p++) {
457       uint8_t idx = *p;
458       uint8_t ofs = p - kBase64Alphabet;
459       table[idx] = ofs;
460     }
461   }
462 };
463
464 static GRPC_HPACK_CONSTEXPR_VALUE Base64InverseTable kBase64InverseTable;
465 }  // namespace
466
467 void HPackParser::FinishFrame() {
468   sink_ = Sink();
469   dynamic_table_updates_allowed_ = 2;
470 }
471
472 void GPR_ATTRIBUTE_NOINLINE HPackParser::LogHeader(grpc_mdelem md) {
473   char* k = grpc_slice_to_c_string(GRPC_MDKEY(md));
474   char* v = nullptr;
475   if (grpc_is_binary_header_internal(GRPC_MDKEY(md))) {
476     v = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_HEX);
477   } else {
478     v = grpc_slice_to_c_string(GRPC_MDVALUE(md));
479   }
480   gpr_log(
481       GPR_INFO,
482       "Decode: '%s: %s', elem_interned=%d [%d], k_interned=%d, v_interned=%d",
483       k, v, GRPC_MDELEM_IS_INTERNED(md), GRPC_MDELEM_STORAGE(md),
484       grpc_slice_is_interned(GRPC_MDKEY(md)),
485       grpc_slice_is_interned(GRPC_MDVALUE(md)));
486   gpr_free(k);
487   gpr_free(v);
488 }
489
490 /* emission helpers */
491 template <HPackParser::TableAction action>
492 grpc_error_handle HPackParser::FinishHeader(grpc_mdelem md) {
493   if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_chttp2_hpack_parser)) {
494     LogHeader(md);
495   }
496   if (action == TableAction::kAddToTable) {
497     GPR_DEBUG_ASSERT(GRPC_MDELEM_STORAGE(md) == GRPC_MDELEM_STORAGE_INTERNED ||
498                      GRPC_MDELEM_STORAGE(md) == GRPC_MDELEM_STORAGE_STATIC);
499     grpc_error_handle err = grpc_chttp2_hptbl_add(&table_, md);
500     if (GPR_UNLIKELY(err != GRPC_ERROR_NONE)) return err;
501   }
502   return sink_(md);
503 }
504
505 UnmanagedMemorySlice HPackParser::String::TakeExtern() {
506   UnmanagedMemorySlice s;
507   if (!copied_) {
508     GPR_DEBUG_ASSERT(!grpc_slice_is_interned(data_.referenced));
509     s = static_cast<UnmanagedMemorySlice&>(data_.referenced);
510     copied_ = true;
511     data_.referenced = UnmanagedMemorySlice();
512   } else {
513     s = UnmanagedMemorySlice(data_.copied.str, data_.copied.length);
514   }
515   data_.copied.length = 0;
516   return s;
517 }
518
519 ManagedMemorySlice HPackParser::String::TakeIntern() {
520   ManagedMemorySlice s;
521   if (!copied_) {
522     s = ManagedMemorySlice(&data_.referenced);
523     grpc_slice_unref_internal(data_.referenced);
524     copied_ = true;
525     data_.referenced = grpc_empty_slice();
526   } else {
527     s = ManagedMemorySlice(data_.copied.str, data_.copied.length);
528   }
529   data_.copied.length = 0;
530   return s;
531 }
532
533 grpc_error_handle HPackParser::parse_next(const uint8_t* cur,
534                                           const uint8_t* end) {
535   state_ = *next_state_++;
536   return (this->*state_)(cur, end);
537 }
538
539 /* begin parsing a header: all functionality is encoded into lookup tables
540    above */
541 grpc_error_handle HPackParser::parse_begin(const uint8_t* cur,
542                                            const uint8_t* end) {
543   if (cur == end) {
544     state_ = &HPackParser::parse_begin;
545     return GRPC_ERROR_NONE;
546   }
547
548   switch (*cur >> 4) {
549       // Literal header not indexed.
550       // First byte format: 0000xxxx
551       // Where xxxx:
552       //   0000  - literal key
553       //   1111  - indexed key, varint encoded index
554       //   other - indexed key, inline encoded index
555     case 0:
556       switch (*cur & 0xf) {
557         case 0:  // literal key
558           return parse_lithdr_notidx_v(cur, end);
559         case 0xf:  // varint encoded key index
560           return parse_lithdr_notidx_x(cur, end);
561         default:  // inline encoded key index
562           return parse_lithdr_notidx(cur, end);
563       }
564       // Literal header never indexed.
565       // First byte format: 0001xxxx
566       // Where xxxx:
567       //   0000  - literal key
568       //   1111  - indexed key, varint encoded index
569       //   other - indexed key, inline encoded index
570     case 1:
571       switch (*cur & 0xf) {
572         case 0:  // literal key
573           return parse_lithdr_nvridx_v(cur, end);
574         case 0xf:  // varint encoded key index
575           return parse_lithdr_nvridx_x(cur, end);
576         default:  // inline encoded key index
577           return parse_lithdr_nvridx(cur, end);
578       }
579       // Update max table size.
580       // First byte format: 001xxxxx
581       // Where xxxxx:
582       //   11111 - max size is varint encoded
583       //   other - max size is stored inline
584     case 2:
585       // inline encoded max table size
586       return parse_max_tbl_size(cur, end);
587     case 3:
588       if (*cur == 0x3f) {
589         // varint encoded max table size
590         return parse_max_tbl_size_x(cur, end);
591       } else {
592         // inline encoded max table size
593         return parse_max_tbl_size(cur, end);
594       }
595       // Literal header with incremental indexing.
596       // First byte format: 01xxxxxx
597       // Where xxxxxx:
598       //   000000 - literal key
599       //   111111 - indexed key, varint encoded index
600       //   other  - indexed key, inline encoded index
601     case 4:
602       if (*cur == 0x40) {
603         // literal key
604         return parse_lithdr_incidx_v(cur, end);
605       }
606       ABSL_FALLTHROUGH_INTENDED;
607     case 5:
608     case 6:
609       // inline encoded key index
610       return parse_lithdr_incidx(cur, end);
611     case 7:
612       if (*cur == 0x7f) {
613         // varint encoded key index
614         return parse_lithdr_incidx_x(cur, end);
615       } else {
616         // inline encoded key index
617         return parse_lithdr_incidx(cur, end);
618       }
619       // Indexed Header Field Representation
620       // First byte format: 1xxxxxxx
621       // Where xxxxxxx:
622       //   0000000 - illegal
623       //   1111111 - varint encoded field index
624       //   other   - inline encoded field index
625     case 8:
626       if (*cur == 0x80) {
627         // illegal value.
628         return parse_illegal_op(cur, end);
629       }
630       ABSL_FALLTHROUGH_INTENDED;
631     case 9:
632     case 10:
633     case 11:
634     case 12:
635     case 13:
636     case 14:
637       // inline encoded field index
638       return parse_indexed_field(cur, end);
639     case 15:
640       if (*cur == 0xff) {
641         // varint encoded field index
642         return parse_indexed_field_x(cur, end);
643       } else {
644         // inline encoded field index
645         return parse_indexed_field(cur, end);
646       }
647   }
648   GPR_UNREACHABLE_CODE(abort());
649 }
650
651 /* stream dependency and prioritization data: we just skip it */
652 grpc_error_handle HPackParser::parse_stream_weight(const uint8_t* cur,
653                                                    const uint8_t* end) {
654   if (cur == end) {
655     state_ = &HPackParser::parse_stream_weight;
656     return GRPC_ERROR_NONE;
657   }
658
659   return (this->*after_prioritization_)(cur + 1, end);
660 }
661
662 grpc_error_handle HPackParser::parse_stream_dep3(const uint8_t* cur,
663                                                  const uint8_t* end) {
664   if (cur == end) {
665     state_ = &HPackParser::parse_stream_dep3;
666     return GRPC_ERROR_NONE;
667   }
668
669   return parse_stream_weight(cur + 1, end);
670 }
671
672 grpc_error_handle HPackParser::parse_stream_dep2(const uint8_t* cur,
673                                                  const uint8_t* end) {
674   if (cur == end) {
675     state_ = &HPackParser::parse_stream_dep2;
676     return GRPC_ERROR_NONE;
677   }
678
679   return parse_stream_dep3(cur + 1, end);
680 }
681
682 grpc_error_handle HPackParser::parse_stream_dep1(const uint8_t* cur,
683                                                  const uint8_t* end) {
684   if (cur == end) {
685     state_ = &HPackParser::parse_stream_dep1;
686     return GRPC_ERROR_NONE;
687   }
688
689   return parse_stream_dep2(cur + 1, end);
690 }
691
692 grpc_error_handle HPackParser::parse_stream_dep0(const uint8_t* cur,
693                                                  const uint8_t* end) {
694   if (cur == end) {
695     state_ = &HPackParser::parse_stream_dep0;
696     return GRPC_ERROR_NONE;
697   }
698
699   return parse_stream_dep1(cur + 1, end);
700 }
701
702 grpc_error_handle HPackParser::InvalidHPackIndexError() {
703   return grpc_error_set_int(
704       grpc_error_set_int(
705           GRPC_ERROR_CREATE_FROM_STATIC_STRING("Invalid HPACK index received"),
706           GRPC_ERROR_INT_INDEX, static_cast<intptr_t>(index_)),
707       GRPC_ERROR_INT_SIZE, static_cast<intptr_t>(table_.num_ents));
708 }
709
710 /* emit an indexed field; jumps to begin the next field on completion */
711 grpc_error_handle HPackParser::finish_indexed_field(const uint8_t* cur,
712                                                     const uint8_t* end) {
713   grpc_mdelem md = grpc_chttp2_hptbl_lookup<true>(&table_, index_);
714   if (GPR_UNLIKELY(GRPC_MDISNULL(md))) {
715     return InvalidHPackIndexError();
716   }
717   GRPC_STATS_INC_HPACK_RECV_INDEXED();
718   grpc_error_handle err = FinishHeader<TableAction::kOmitFromTable>(md);
719   if (GPR_UNLIKELY(err != GRPC_ERROR_NONE)) return err;
720   return parse_begin(cur, end);
721 }
722
723 /* parse an indexed field with index < 127 */
724 grpc_error_handle HPackParser::parse_indexed_field(const uint8_t* cur,
725                                                    const uint8_t* end) {
726   dynamic_table_updates_allowed_ = 0;
727   index_ = (*cur) & 0x7f;
728   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
729   return finish_indexed_field(cur + 1, end);
730 }
731
732 /* parse an indexed field with index >= 127 */
733 grpc_error_handle HPackParser::parse_indexed_field_x(const uint8_t* cur,
734                                                      const uint8_t* end) {
735   static const State and_then[] = {&HPackParser::finish_indexed_field};
736   dynamic_table_updates_allowed_ = 0;
737   next_state_ = and_then;
738   index_ = 0x7f;
739   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
740   parsing_.value = &index_;
741   return parse_value0(cur + 1, end);
742 }
743
744 /* When finishing with a header, get the cached md element for this index.
745    This is set in parse_value_string(). We ensure (in debug mode) that the
746    cached metadata corresponds with the index we are examining. */
747 grpc_mdelem HPackParser::GetPrecomputedMDForIndex() {
748   GPR_DEBUG_ASSERT(md_for_index_.payload != 0);
749   GPR_DEBUG_ASSERT(static_cast<int64_t>(index_) == precomputed_md_index_);
750   grpc_mdelem md = md_for_index_;
751   GPR_DEBUG_ASSERT(!GRPC_MDISNULL(md)); /* handled in string parsing */
752   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
753 #ifndef NDEBUG
754   precomputed_md_index_ = -1;
755 #endif
756   return md;
757 }
758
759 static const grpc_core::ManagedMemorySlice& get_indexed_key(grpc_mdelem md) {
760   GPR_DEBUG_ASSERT(GRPC_MDELEM_IS_INTERNED(md));
761   return static_cast<const grpc_core::ManagedMemorySlice&>(
762       grpc_slice_ref_internal(GRPC_MDKEY(md)));
763 }
764
765 /* finish a literal header with incremental indexing */
766 grpc_error_handle HPackParser::finish_lithdr_incidx(const uint8_t* cur,
767                                                     const uint8_t* end) {
768   GRPC_STATS_INC_HPACK_RECV_LITHDR_INCIDX();
769   grpc_mdelem md = GetPrecomputedMDForIndex();
770   grpc_error_handle err = FinishHeader<TableAction::kAddToTable>(
771       grpc_mdelem_from_slices(get_indexed_key(md), value_.TakeIntern()));
772   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
773   return parse_begin(cur, end);
774 }
775
776 /* finish a literal header with incremental indexing with no index */
777 grpc_error_handle HPackParser::finish_lithdr_incidx_v(const uint8_t* cur,
778                                                       const uint8_t* end) {
779   GRPC_STATS_INC_HPACK_RECV_LITHDR_INCIDX_V();
780   grpc_error_handle err = FinishHeader<TableAction::kAddToTable>(
781       grpc_mdelem_from_slices(key_.TakeIntern(), value_.TakeIntern()));
782   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
783   return parse_begin(cur, end);
784 }
785
786 /* parse a literal header with incremental indexing; index < 63 */
787 grpc_error_handle HPackParser::parse_lithdr_incidx(const uint8_t* cur,
788                                                    const uint8_t* end) {
789   static const State and_then[] = {
790       &HPackParser::parse_value_string_with_indexed_key,
791       &HPackParser::finish_lithdr_incidx};
792   dynamic_table_updates_allowed_ = 0;
793   next_state_ = and_then;
794   index_ = (*cur) & 0x3f;
795   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
796   return parse_string_prefix(cur + 1, end);
797 }
798
799 /* parse a literal header with incremental indexing; index >= 63 */
800 grpc_error_handle HPackParser::parse_lithdr_incidx_x(const uint8_t* cur,
801                                                      const uint8_t* end) {
802   static const State and_then[] = {
803       &HPackParser::parse_string_prefix,
804       &HPackParser::parse_value_string_with_indexed_key,
805       &HPackParser::finish_lithdr_incidx};
806   dynamic_table_updates_allowed_ = 0;
807   next_state_ = and_then;
808   index_ = 0x3f;
809   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
810   parsing_.value = &index_;
811   return parse_value0(cur + 1, end);
812 }
813
814 /* parse a literal header with incremental indexing; index = 0 */
815 grpc_error_handle HPackParser::parse_lithdr_incidx_v(const uint8_t* cur,
816                                                      const uint8_t* end) {
817   static const State and_then[] = {
818       &HPackParser::parse_key_string, &HPackParser::parse_string_prefix,
819       &HPackParser::parse_value_string_with_literal_key,
820       &HPackParser::finish_lithdr_incidx_v};
821   dynamic_table_updates_allowed_ = 0;
822   next_state_ = and_then;
823   return parse_string_prefix(cur + 1, end);
824 }
825
826 /* finish a literal header without incremental indexing */
827 grpc_error_handle HPackParser::finish_lithdr_notidx(const uint8_t* cur,
828                                                     const uint8_t* end) {
829   GRPC_STATS_INC_HPACK_RECV_LITHDR_NOTIDX();
830   grpc_mdelem md = GetPrecomputedMDForIndex();
831   grpc_error_handle err = FinishHeader<TableAction::kOmitFromTable>(
832       grpc_mdelem_from_slices(get_indexed_key(md), value_.TakeExtern()));
833   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
834   return parse_begin(cur, end);
835 }
836
837 /* finish a literal header without incremental indexing with index = 0 */
838 grpc_error_handle HPackParser::finish_lithdr_notidx_v(const uint8_t* cur,
839                                                       const uint8_t* end) {
840   GRPC_STATS_INC_HPACK_RECV_LITHDR_NOTIDX_V();
841   grpc_error_handle err = FinishHeader<TableAction::kOmitFromTable>(
842       grpc_mdelem_from_slices(key_.TakeIntern(), value_.TakeExtern()));
843   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
844   return parse_begin(cur, end);
845 }
846
847 /* parse a literal header without incremental indexing; index < 15 */
848 grpc_error_handle HPackParser::parse_lithdr_notidx(const uint8_t* cur,
849                                                    const uint8_t* end) {
850   static const State and_then[] = {
851       &HPackParser::parse_value_string_with_indexed_key,
852       &HPackParser::finish_lithdr_notidx};
853   dynamic_table_updates_allowed_ = 0;
854   next_state_ = and_then;
855   index_ = (*cur) & 0xf;
856   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
857   return parse_string_prefix(cur + 1, end);
858 }
859
860 /* parse a literal header without incremental indexing; index >= 15 */
861 grpc_error_handle HPackParser::parse_lithdr_notidx_x(const uint8_t* cur,
862                                                      const uint8_t* end) {
863   static const State and_then[] = {
864       &HPackParser::parse_string_prefix,
865       &HPackParser::parse_value_string_with_indexed_key,
866       &HPackParser::finish_lithdr_notidx};
867   dynamic_table_updates_allowed_ = 0;
868   next_state_ = and_then;
869   index_ = 0xf;
870   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
871   parsing_.value = &index_;
872   return parse_value0(cur + 1, end);
873 }
874
875 /* parse a literal header without incremental indexing; index == 0 */
876 grpc_error_handle HPackParser::parse_lithdr_notidx_v(const uint8_t* cur,
877                                                      const uint8_t* end) {
878   static const State and_then[] = {
879       &HPackParser::parse_key_string, &HPackParser::parse_string_prefix,
880       &HPackParser::parse_value_string_with_literal_key,
881       &HPackParser::finish_lithdr_notidx_v};
882   dynamic_table_updates_allowed_ = 0;
883   next_state_ = and_then;
884   return parse_string_prefix(cur + 1, end);
885 }
886
887 /* finish a literal header that is never indexed */
888 grpc_error_handle HPackParser::finish_lithdr_nvridx(const uint8_t* cur,
889                                                     const uint8_t* end) {
890   GRPC_STATS_INC_HPACK_RECV_LITHDR_NVRIDX();
891   grpc_mdelem md = GetPrecomputedMDForIndex();
892   grpc_error_handle err = FinishHeader<TableAction::kOmitFromTable>(
893       grpc_mdelem_from_slices(get_indexed_key(md), value_.TakeExtern()));
894   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
895   return parse_begin(cur, end);
896 }
897
898 /* finish a literal header that is never indexed with an extra value */
899 grpc_error_handle HPackParser::finish_lithdr_nvridx_v(const uint8_t* cur,
900                                                       const uint8_t* end) {
901   GRPC_STATS_INC_HPACK_RECV_LITHDR_NVRIDX_V();
902   grpc_error_handle err = FinishHeader<TableAction::kOmitFromTable>(
903       grpc_mdelem_from_slices(key_.TakeIntern(), value_.TakeExtern()));
904   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
905   return parse_begin(cur, end);
906 }
907
908 /* parse a literal header that is never indexed; index < 15 */
909 grpc_error_handle HPackParser::parse_lithdr_nvridx(const uint8_t* cur,
910                                                    const uint8_t* end) {
911   static const State and_then[] = {
912       &HPackParser::parse_value_string_with_indexed_key,
913       &HPackParser::finish_lithdr_nvridx};
914   dynamic_table_updates_allowed_ = 0;
915   next_state_ = and_then;
916   index_ = (*cur) & 0xf;
917   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
918   return parse_string_prefix(cur + 1, end);
919 }
920
921 /* parse a literal header that is never indexed; index >= 15 */
922 grpc_error_handle HPackParser::parse_lithdr_nvridx_x(const uint8_t* cur,
923                                                      const uint8_t* end) {
924   static const State and_then[] = {
925       &HPackParser::parse_string_prefix,
926       &HPackParser::parse_value_string_with_indexed_key,
927       &HPackParser::finish_lithdr_nvridx};
928   dynamic_table_updates_allowed_ = 0;
929   next_state_ = and_then;
930   index_ = 0xf;
931   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
932   parsing_.value = &index_;
933   return parse_value0(cur + 1, end);
934 }
935
936 /* parse a literal header that is never indexed; index == 0 */
937 grpc_error_handle HPackParser::parse_lithdr_nvridx_v(const uint8_t* cur,
938                                                      const uint8_t* end) {
939   static const State and_then[] = {
940       &HPackParser::parse_key_string, &HPackParser::parse_string_prefix,
941       &HPackParser::parse_value_string_with_literal_key,
942       &HPackParser::finish_lithdr_nvridx_v};
943   dynamic_table_updates_allowed_ = 0;
944   next_state_ = and_then;
945   return parse_string_prefix(cur + 1, end);
946 }
947
948 /* finish parsing a max table size change */
949 grpc_error_handle HPackParser::finish_max_tbl_size(const uint8_t* cur,
950                                                    const uint8_t* end) {
951   if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_chttp2_hpack_parser)) {
952     gpr_log(GPR_INFO, "MAX TABLE SIZE: %d", index_);
953   }
954   grpc_error_handle err =
955       grpc_chttp2_hptbl_set_current_table_size(&table_, index_);
956   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
957   return parse_begin(cur, end);
958 }
959
960 /* parse a max table size change, max size < 15 */
961 grpc_error_handle HPackParser::parse_max_tbl_size(const uint8_t* cur,
962                                                   const uint8_t* end) {
963   if (dynamic_table_updates_allowed_ == 0) {
964     return parse_error(
965         cur, end,
966         GRPC_ERROR_CREATE_FROM_STATIC_STRING(
967             "More than two max table size changes in a single frame"));
968   }
969   dynamic_table_updates_allowed_--;
970   index_ = (*cur) & 0x1f;
971   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
972   return finish_max_tbl_size(cur + 1, end);
973 }
974
975 /* parse a max table size change, max size >= 15 */
976 grpc_error_handle HPackParser::parse_max_tbl_size_x(const uint8_t* cur,
977                                                     const uint8_t* end) {
978   static const State and_then[] = {&HPackParser::finish_max_tbl_size};
979   if (dynamic_table_updates_allowed_ == 0) {
980     return parse_error(
981         cur, end,
982         GRPC_ERROR_CREATE_FROM_STATIC_STRING(
983             "More than two max table size changes in a single frame"));
984   }
985   dynamic_table_updates_allowed_--;
986   next_state_ = and_then;
987   index_ = 0x1f;
988   md_for_index_.payload = 0; /* Invalidate cached md when index changes. */
989   parsing_.value = &index_;
990   return parse_value0(cur + 1, end);
991 }
992
993 /* a parse error: jam the parse state into parse_error, and return error */
994 grpc_error_handle HPackParser::parse_error(const uint8_t* /*cur*/,
995                                            const uint8_t* /*end*/,
996                                            grpc_error_handle err) {
997   GPR_ASSERT(err != GRPC_ERROR_NONE);
998   if (last_error_ == GRPC_ERROR_NONE) {
999     last_error_ = GRPC_ERROR_REF(err);
1000   }
1001   state_ = &HPackParser::still_parse_error;
1002   return err;
1003 }
1004
1005 grpc_error_handle HPackParser::still_parse_error(const uint8_t* /*cur*/,
1006                                                  const uint8_t* /*end*/) {
1007   return GRPC_ERROR_REF(last_error_);
1008 }
1009
1010 grpc_error_handle HPackParser::parse_illegal_op(const uint8_t* cur,
1011                                                 const uint8_t* end) {
1012   GPR_ASSERT(cur != end);
1013   grpc_error_handle err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
1014       absl::StrCat("Illegal hpack op code ", *cur).c_str());
1015   return parse_error(cur, end, err);
1016 }
1017
1018 /* parse the 1st byte of a varint into parsing_.value
1019    no overflow is possible */
1020 grpc_error_handle HPackParser::parse_value0(const uint8_t* cur,
1021                                             const uint8_t* end) {
1022   if (cur == end) {
1023     state_ = &HPackParser::parse_value0;
1024     return GRPC_ERROR_NONE;
1025   }
1026
1027   *parsing_.value += (*cur) & 0x7f;
1028
1029   if ((*cur) & 0x80) {
1030     return parse_value1(cur + 1, end);
1031   } else {
1032     return parse_next(cur + 1, end);
1033   }
1034 }
1035
1036 /* parse the 2nd byte of a varint into parsing_.value
1037    no overflow is possible */
1038 grpc_error_handle HPackParser::parse_value1(const uint8_t* cur,
1039                                             const uint8_t* end) {
1040   if (cur == end) {
1041     state_ = &HPackParser::parse_value1;
1042     return GRPC_ERROR_NONE;
1043   }
1044
1045   *parsing_.value += ((static_cast<uint32_t>(*cur)) & 0x7f) << 7;
1046
1047   if ((*cur) & 0x80) {
1048     return parse_value2(cur + 1, end);
1049   } else {
1050     return parse_next(cur + 1, end);
1051   }
1052 }
1053
1054 /* parse the 3rd byte of a varint into parsing_.value
1055    no overflow is possible */
1056 grpc_error_handle HPackParser::parse_value2(const uint8_t* cur,
1057                                             const uint8_t* end) {
1058   if (cur == end) {
1059     state_ = &HPackParser::parse_value2;
1060     return GRPC_ERROR_NONE;
1061   }
1062
1063   *parsing_.value += ((static_cast<uint32_t>(*cur)) & 0x7f) << 14;
1064
1065   if ((*cur) & 0x80) {
1066     return parse_value3(cur + 1, end);
1067   } else {
1068     return parse_next(cur + 1, end);
1069   }
1070 }
1071
1072 /* parse the 4th byte of a varint into parsing_.value
1073    no overflow is possible */
1074 grpc_error_handle HPackParser::parse_value3(const uint8_t* cur,
1075                                             const uint8_t* end) {
1076   if (cur == end) {
1077     state_ = &HPackParser::parse_value3;
1078     return GRPC_ERROR_NONE;
1079   }
1080
1081   *parsing_.value += ((static_cast<uint32_t>(*cur)) & 0x7f) << 21;
1082
1083   if ((*cur) & 0x80) {
1084     return parse_value4(cur + 1, end);
1085   } else {
1086     return parse_next(cur + 1, end);
1087   }
1088 }
1089
1090 /* parse the 5th byte of a varint into parsing_.value
1091    depending on the byte, we may overflow, and care must be taken */
1092 grpc_error_handle HPackParser::parse_value4(const uint8_t* cur,
1093                                             const uint8_t* end) {
1094   uint8_t c;
1095   uint32_t cur_value;
1096   uint32_t add_value;
1097
1098   if (cur == end) {
1099     state_ = &HPackParser::parse_value4;
1100     return GRPC_ERROR_NONE;
1101   }
1102
1103   c = (*cur) & 0x7f;
1104   if (c > 0xf) {
1105     goto error;
1106   }
1107
1108   cur_value = *parsing_.value;
1109   add_value = (static_cast<uint32_t>(c)) << 28;
1110   if (add_value > 0xffffffffu - cur_value) {
1111     goto error;
1112   }
1113
1114   *parsing_.value = cur_value + add_value;
1115
1116   if ((*cur) & 0x80) {
1117     return parse_value5up(cur + 1, end);
1118   } else {
1119     return parse_next(cur + 1, end);
1120   }
1121
1122 error:
1123   grpc_error_handle err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
1124       absl::StrFormat(
1125           "integer overflow in hpack integer decoding: have 0x%08x, "
1126           "got byte 0x%02x on byte 5",
1127           *parsing_.value, *cur)
1128           .c_str());
1129   return parse_error(cur, end, err);
1130 }
1131
1132 /* parse any trailing bytes in a varint: it's possible to append an arbitrary
1133    number of 0x80's and not affect the value - a zero will terminate - and
1134    anything else will overflow */
1135 grpc_error_handle HPackParser::parse_value5up(const uint8_t* cur,
1136                                               const uint8_t* end) {
1137   while (cur != end && *cur == 0x80) {
1138     ++cur;
1139   }
1140
1141   if (cur == end) {
1142     state_ = &HPackParser::parse_value5up;
1143     return GRPC_ERROR_NONE;
1144   }
1145
1146   if (*cur == 0) {
1147     return parse_next(cur + 1, end);
1148   }
1149
1150   grpc_error_handle err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
1151       absl::StrFormat(
1152           "integer overflow in hpack integer decoding: have 0x%08x, "
1153           "got byte 0x%02x sometime after byte 5",
1154           *parsing_.value, *cur)
1155           .c_str());
1156   return parse_error(cur, end, err);
1157 }
1158
1159 /* parse a string prefix */
1160 grpc_error_handle HPackParser::parse_string_prefix(const uint8_t* cur,
1161                                                    const uint8_t* end) {
1162   if (cur == end) {
1163     state_ = &HPackParser::parse_string_prefix;
1164     return GRPC_ERROR_NONE;
1165   }
1166
1167   strlen_ = (*cur) & 0x7f;
1168   huff_ = (*cur) >> 7;
1169   if (strlen_ == 0x7f) {
1170     parsing_.value = &strlen_;
1171     return parse_value0(cur + 1, end);
1172   } else {
1173     return parse_next(cur + 1, end);
1174   }
1175 }
1176
1177 /* append some bytes to a string */
1178 void HPackParser::String::AppendBytes(const uint8_t* data, size_t length) {
1179   if (length == 0) return;
1180   if (length + data_.copied.length > data_.copied.capacity) {
1181     GPR_ASSERT(data_.copied.length + length <= UINT32_MAX);
1182     data_.copied.capacity = static_cast<uint32_t>(data_.copied.length + length);
1183     data_.copied.str = static_cast<char*>(
1184         gpr_realloc(data_.copied.str, data_.copied.capacity));
1185   }
1186   memcpy(data_.copied.str + data_.copied.length, data, length);
1187   GPR_ASSERT(length <= UINT32_MAX - data_.copied.length);
1188   data_.copied.length += static_cast<uint32_t>(length);
1189 }
1190
1191 grpc_error_handle HPackParser::AppendString(const uint8_t* cur,
1192                                             const uint8_t* end) {
1193   String* str = parsing_.str;
1194   uint32_t bits;
1195   uint8_t decoded[3];
1196   switch (binary_) {
1197     case BinaryState::kNotBinary:
1198       str->AppendBytes(cur, static_cast<size_t>(end - cur));
1199       return GRPC_ERROR_NONE;
1200     case BinaryState::kBinaryBegin:
1201       if (cur == end) {
1202         binary_ = BinaryState::kBinaryBegin;
1203         return GRPC_ERROR_NONE;
1204       }
1205       if (*cur == 0) {
1206         /* 'true-binary' case */
1207         ++cur;
1208         binary_ = BinaryState::kNotBinary;
1209         GRPC_STATS_INC_HPACK_RECV_BINARY();
1210         str->AppendBytes(cur, static_cast<size_t>(end - cur));
1211         return GRPC_ERROR_NONE;
1212       }
1213       GRPC_STATS_INC_HPACK_RECV_BINARY_BASE64();
1214     b64_byte0:
1215       ABSL_FALLTHROUGH_INTENDED;
1216     case BinaryState::kBase64Byte0:
1217       if (cur == end) {
1218         binary_ = BinaryState::kBase64Byte0;
1219         return GRPC_ERROR_NONE;
1220       }
1221       bits = kBase64InverseTable.table[*cur];
1222       ++cur;
1223       if (bits == 255) {
1224         return parse_error(
1225             cur, end,
1226             GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal base64 character"));
1227       } else if (bits == 64) {
1228         goto b64_byte0;
1229       }
1230       base64_buffer_ = bits << 18;
1231     b64_byte1:
1232       ABSL_FALLTHROUGH_INTENDED;
1233     case BinaryState::kBase64Byte1:
1234       if (cur == end) {
1235         binary_ = BinaryState::kBase64Byte1;
1236         return GRPC_ERROR_NONE;
1237       }
1238       bits = kBase64InverseTable.table[*cur];
1239       ++cur;
1240       if (bits == 255) {
1241         return parse_error(
1242             cur, end,
1243             GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal base64 character"));
1244       } else if (bits == 64) {
1245         goto b64_byte1;
1246       }
1247       base64_buffer_ |= bits << 12;
1248     b64_byte2:
1249       ABSL_FALLTHROUGH_INTENDED;
1250     case BinaryState::kBase64Byte2:
1251       if (cur == end) {
1252         binary_ = BinaryState::kBase64Byte2;
1253         return GRPC_ERROR_NONE;
1254       }
1255       bits = kBase64InverseTable.table[*cur];
1256       ++cur;
1257       if (bits == 255) {
1258         return parse_error(
1259             cur, end,
1260             GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal base64 character"));
1261       } else if (bits == 64) {
1262         goto b64_byte2;
1263       }
1264       base64_buffer_ |= bits << 6;
1265     b64_byte3:
1266       ABSL_FALLTHROUGH_INTENDED;
1267     case BinaryState::kBase64Byte3:
1268       if (cur == end) {
1269         binary_ = BinaryState::kBase64Byte3;
1270         return GRPC_ERROR_NONE;
1271       }
1272       bits = kBase64InverseTable.table[*cur];
1273       ++cur;
1274       if (bits == 255) {
1275         return parse_error(
1276             cur, end,
1277             GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal base64 character"));
1278       } else if (bits == 64) {
1279         goto b64_byte3;
1280       }
1281       base64_buffer_ |= bits;
1282       bits = base64_buffer_;
1283       decoded[0] = static_cast<uint8_t>(bits >> 16);
1284       decoded[1] = static_cast<uint8_t>(bits >> 8);
1285       decoded[2] = static_cast<uint8_t>(bits);
1286       str->AppendBytes(decoded, 3);
1287       goto b64_byte0;
1288   }
1289   GPR_UNREACHABLE_CODE(return parse_error(
1290       cur, end,
1291       GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here")));
1292 }
1293
1294 grpc_error_handle HPackParser::finish_str(const uint8_t* cur,
1295                                           const uint8_t* end) {
1296   uint8_t decoded[2];
1297   uint32_t bits;
1298   String* str = parsing_.str;
1299   switch (binary_) {
1300     case BinaryState::kNotBinary:
1301       break;
1302     case BinaryState::kBinaryBegin:
1303       break;
1304     case BinaryState::kBase64Byte0:
1305       break;
1306     case BinaryState::kBase64Byte1:
1307       return parse_error(cur, end,
1308                          GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1309                              "illegal base64 encoding")); /* illegal encoding */
1310     case BinaryState::kBase64Byte2:
1311       bits = base64_buffer_;
1312       if (bits & 0xffff) {
1313         grpc_error_handle err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
1314             absl::StrFormat("trailing bits in base64 encoding: 0x%04x",
1315                             bits & 0xffff)
1316                 .c_str());
1317         return parse_error(cur, end, err);
1318       }
1319       decoded[0] = static_cast<uint8_t>(bits >> 16);
1320       str->AppendBytes(decoded, 1);
1321       break;
1322     case BinaryState::kBase64Byte3:
1323       bits = base64_buffer_;
1324       if (bits & 0xff) {
1325         grpc_error_handle err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
1326             absl::StrFormat("trailing bits in base64 encoding: 0x%02x",
1327                             bits & 0xff)
1328                 .c_str());
1329         return parse_error(cur, end, err);
1330       }
1331       decoded[0] = static_cast<uint8_t>(bits >> 16);
1332       decoded[1] = static_cast<uint8_t>(bits >> 8);
1333       str->AppendBytes(decoded, 2);
1334       break;
1335   }
1336   return GRPC_ERROR_NONE;
1337 }
1338
1339 /* decode a nibble from a huffman encoded stream */
1340 grpc_error_handle HPackParser::AppendHuffNibble(uint8_t nibble) {
1341   int16_t emit = emit_sub_tbl[16 * emit_tbl[huff_state_] + nibble];
1342   int16_t next = next_sub_tbl[16 * next_tbl[huff_state_] + nibble];
1343   if (emit != -1) {
1344     if (emit >= 0 && emit < 256) {
1345       uint8_t c = static_cast<uint8_t>(emit);
1346       grpc_error_handle err = AppendString(&c, (&c) + 1);
1347       if (err != GRPC_ERROR_NONE) return err;
1348     } else {
1349       assert(emit == 256);
1350     }
1351   }
1352   huff_state_ = next;
1353   return GRPC_ERROR_NONE;
1354 }
1355
1356 /* decode full bytes from a huffman encoded stream */
1357 grpc_error_handle HPackParser::AppendHuffBytes(const uint8_t* cur,
1358                                                const uint8_t* end) {
1359   for (; cur != end; ++cur) {
1360     grpc_error_handle err = AppendHuffNibble(*cur >> 4);
1361     if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1362     err = AppendHuffNibble(*cur & 0xf);
1363     if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1364   }
1365   return GRPC_ERROR_NONE;
1366 }
1367
1368 /* decode some string bytes based on the current decoding mode
1369    (huffman or not) */
1370 grpc_error_handle HPackParser::AppendStrBytes(const uint8_t* cur,
1371                                               const uint8_t* end) {
1372   if (huff_) {
1373     return AppendHuffBytes(cur, end);
1374   } else {
1375     return AppendString(cur, end);
1376   }
1377 }
1378
1379 /* parse a string - tries to do large chunks at a time */
1380 grpc_error_handle HPackParser::parse_string(const uint8_t* cur,
1381                                             const uint8_t* end) {
1382   size_t remaining = strlen_ - strgot_;
1383   size_t given = static_cast<size_t>(end - cur);
1384   if (remaining <= given) {
1385     grpc_error_handle err = AppendStrBytes(cur, cur + remaining);
1386     if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1387     err = finish_str(cur + remaining, end);
1388     if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1389     return parse_next(cur + remaining, end);
1390   } else {
1391     grpc_error_handle err = AppendStrBytes(cur, cur + given);
1392     if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1393     GPR_ASSERT(given <= UINT32_MAX - strgot_);
1394     strgot_ += static_cast<uint32_t>(given);
1395     state_ = &HPackParser::parse_string;
1396     return GRPC_ERROR_NONE;
1397   }
1398 }
1399
1400 /* begin parsing a string - performs setup, calls parse_string */
1401 grpc_error_handle HPackParser::begin_parse_string(const uint8_t* cur,
1402                                                   const uint8_t* end,
1403                                                   BinaryState binary,
1404                                                   HPackParser::String* str) {
1405   if (!huff_ && binary == BinaryState::kNotBinary &&
1406       static_cast<uint32_t>(end - cur) >= strlen_ &&
1407       current_slice_refcount_ != nullptr) {
1408     GRPC_STATS_INC_HPACK_RECV_UNCOMPRESSED();
1409     str->copied_ = false;
1410     str->data_.referenced.refcount = current_slice_refcount_;
1411     str->data_.referenced.data.refcounted.bytes = const_cast<uint8_t*>(cur);
1412     str->data_.referenced.data.refcounted.length = strlen_;
1413     grpc_slice_ref_internal(str->data_.referenced);
1414     return parse_next(cur + strlen_, end);
1415   }
1416   strgot_ = 0;
1417   str->copied_ = true;
1418   str->data_.copied.length = 0;
1419   parsing_.str = str;
1420   huff_state_ = 0;
1421   binary_ = binary;
1422   switch (binary_) {
1423     case BinaryState::kNotBinary:
1424       if (huff_) {
1425         GRPC_STATS_INC_HPACK_RECV_HUFFMAN();
1426       } else {
1427         GRPC_STATS_INC_HPACK_RECV_UNCOMPRESSED();
1428       }
1429       break;
1430     case BinaryState::kBinaryBegin:
1431       /* stats incremented later: don't know true binary or not */
1432       break;
1433     default:
1434       abort();
1435   }
1436   return parse_string(cur, end);
1437 }
1438
1439 /* parse the key string */
1440 grpc_error_handle HPackParser::parse_key_string(const uint8_t* cur,
1441                                                 const uint8_t* end) {
1442   return begin_parse_string(cur, end, BinaryState::kNotBinary, &key_);
1443 }
1444
1445 /* check if a key represents a binary header or not */
1446
1447 bool HPackParser::IsBinaryLiteralHeader() {
1448   /* We know that either argument here is a reference counter slice.
1449    * 1. If it is a grpc_core::StaticSlice, the refcount is set to kNoopRefcount.
1450    * 2. If it's key_.data.referenced, then key_.copied was set to false,
1451    *    which occurs in begin_parse_string() - where the refcount is set to
1452    *    current_slice_refcount_, which is not null. */
1453   return grpc_is_refcounted_slice_binary_header(
1454       key_.copied_ ? grpc_core::ExternallyManagedSlice(key_.data_.copied.str,
1455                                                        key_.data_.copied.length)
1456                    : key_.data_.referenced);
1457 }
1458
1459 /* Cache the metadata for the given index during initial parsing. This avoids a
1460    pointless recomputation of the metadata when finishing a header. We read the
1461    cached value in get_precomputed_md_for_idx(). */
1462 void HPackParser::SetPrecomputedMDIndex(grpc_mdelem md) {
1463   GPR_DEBUG_ASSERT(md_for_index_.payload == 0);
1464   GPR_DEBUG_ASSERT(precomputed_md_index_ == -1);
1465   md_for_index_ = md;
1466 #ifndef NDEBUG
1467   precomputed_md_index_ = index_;
1468 #endif
1469 }
1470
1471 /* Determines if a metadata element key associated with the current parser index
1472    is a binary indexed header during string parsing. We'll need to revisit this
1473    metadata when we're done parsing, so we cache the metadata for this index
1474    here using set_precomputed_md_idx(). */
1475 grpc_error_handle HPackParser::IsBinaryIndexedHeader(bool* is) {
1476   grpc_mdelem elem = grpc_chttp2_hptbl_lookup(&table_, index_);
1477   if (GPR_UNLIKELY(GRPC_MDISNULL(elem))) {
1478     return InvalidHPackIndexError();
1479   }
1480   /* We know that GRPC_MDKEY(elem) points to a reference counted slice since:
1481    * 1. elem was a result of grpc_chttp2_hptbl_lookup
1482    * 2. An item in this table is either static (see entries with
1483    *    index < GRPC_CHTTP2_LAST_STATIC_ENTRY or added via
1484    *    grpc_chttp2_hptbl_add).
1485    * 3. If added via grpc_chttp2_hptbl_add, the entry is either static or
1486    *    interned.
1487    * 4. Both static and interned element slices have non-null refcounts. */
1488   *is = grpc_is_refcounted_slice_binary_header(GRPC_MDKEY(elem));
1489   SetPrecomputedMDIndex(elem);
1490   return GRPC_ERROR_NONE;
1491 }
1492
1493 /* parse the value string */
1494 grpc_error_handle HPackParser::parse_value_string(const uint8_t* cur,
1495                                                   const uint8_t* end,
1496                                                   bool is_binary) {
1497   return begin_parse_string(
1498       cur, end, is_binary ? BinaryState::kBinaryBegin : BinaryState::kNotBinary,
1499       &value_);
1500 }
1501
1502 grpc_error_handle HPackParser::parse_value_string_with_indexed_key(
1503     const uint8_t* cur, const uint8_t* end) {
1504   bool is_binary = false;
1505   grpc_error_handle err = IsBinaryIndexedHeader(&is_binary);
1506   if (err != GRPC_ERROR_NONE) return parse_error(cur, end, err);
1507   return parse_value_string(cur, end, is_binary);
1508 }
1509
1510 grpc_error_handle HPackParser::parse_value_string_with_literal_key(
1511     const uint8_t* cur, const uint8_t* end) {
1512   return parse_value_string(cur, end, IsBinaryLiteralHeader());
1513 }
1514
1515 /* PUBLIC INTERFACE */
1516
1517 HPackParser::HPackParser() {
1518   state_ = &HPackParser::parse_begin;
1519   key_.data_.referenced = grpc_empty_slice();
1520   key_.data_.copied.str = nullptr;
1521   key_.data_.copied.capacity = 0;
1522   key_.data_.copied.length = 0;
1523   value_.data_.referenced = grpc_empty_slice();
1524   value_.data_.copied.str = nullptr;
1525   value_.data_.copied.capacity = 0;
1526   value_.data_.copied.length = 0;
1527   /* Cached metadata for the current index the parser is handling. This is set
1528      to 0 initially, invalidated when the index changes, and invalidated when it
1529      is read (by get_precomputed_md_for_idx()). It is set during string parsing,
1530      by set_precomputed_md_idx() - which is called by parse_value_string().
1531      The goal here is to avoid recomputing the metadata for the index when
1532      finishing with a header as well as the initial parse. */
1533   md_for_index_.payload = 0;
1534 #ifndef NDEBUG
1535   /* In debug mode, this ensures that the cached metadata we're reading is in
1536    * fact correct for the index we are examining. */
1537   precomputed_md_index_ = -1;
1538 #endif
1539   dynamic_table_updates_allowed_ = 2;
1540   last_error_ = GRPC_ERROR_NONE;
1541 }
1542
1543 void HPackParser::BeginFrame(Sink sink, Boundary boundary, Priority priority) {
1544   sink_ = std::move(sink);
1545   boundary_ = boundary;
1546   switch (priority) {
1547     case Priority::Included:
1548       after_prioritization_ = state_;
1549       state_ = &HPackParser::parse_stream_dep0;
1550       break;
1551     case Priority::None:
1552       break;
1553   }
1554 }
1555
1556 HPackParser::~HPackParser() {
1557   grpc_chttp2_hptbl_destroy(&table_);
1558   GRPC_ERROR_UNREF(last_error_);
1559   grpc_slice_unref_internal(key_.data_.referenced);
1560   grpc_slice_unref_internal(value_.data_.referenced);
1561   gpr_free(key_.data_.copied.str);
1562   gpr_free(value_.data_.copied.str);
1563 }
1564
1565 grpc_error_handle HPackParser::Parse(const grpc_slice& slice) {
1566 /* max number of bytes to parse at a time... limits call stack depth on
1567  * compilers without TCO */
1568 #define MAX_PARSE_LENGTH 1024
1569   current_slice_refcount_ = slice.refcount;
1570   const uint8_t* start = GRPC_SLICE_START_PTR(slice);
1571   const uint8_t* end = GRPC_SLICE_END_PTR(slice);
1572   grpc_error_handle error = GRPC_ERROR_NONE;
1573   while (start != end && error == GRPC_ERROR_NONE) {
1574     const uint8_t* target = start + GPR_MIN(MAX_PARSE_LENGTH, end - start);
1575     error = (this->*state_)(start, target);
1576     start = target;
1577   }
1578   current_slice_refcount_ = nullptr;
1579   return error;
1580 }
1581
1582 }  // namespace grpc_core
1583
1584 // TODO(ctiller): this serves as an eviction notice for the remainder of this
1585 // file... it belongs elsewhere!
1586
1587 typedef void (*maybe_complete_func_type)(grpc_chttp2_transport* t,
1588                                          grpc_chttp2_stream* s);
1589 static const maybe_complete_func_type maybe_complete_funcs[] = {
1590     grpc_chttp2_maybe_complete_recv_initial_metadata,
1591     grpc_chttp2_maybe_complete_recv_trailing_metadata};
1592
1593 static void force_client_rst_stream(void* sp, grpc_error_handle /*error*/) {
1594   grpc_chttp2_stream* s = static_cast<grpc_chttp2_stream*>(sp);
1595   grpc_chttp2_transport* t = s->t;
1596   if (!s->write_closed) {
1597     grpc_chttp2_add_rst_stream_to_next_write(t, s->id, GRPC_HTTP2_NO_ERROR,
1598                                              &s->stats.outgoing);
1599     grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_FORCE_RST_STREAM);
1600     grpc_chttp2_mark_stream_closed(t, s, true, true, GRPC_ERROR_NONE);
1601   }
1602   GRPC_CHTTP2_STREAM_UNREF(s, "final_rst");
1603 }
1604
1605 static void parse_stream_compression_md(grpc_chttp2_transport* /*t*/,
1606                                         grpc_chttp2_stream* s,
1607                                         grpc_metadata_batch* initial_metadata) {
1608   if (initial_metadata->idx.named.content_encoding == nullptr ||
1609       grpc_stream_compression_method_parse(
1610           GRPC_MDVALUE(initial_metadata->idx.named.content_encoding->md), false,
1611           &s->stream_decompression_method) == 0) {
1612     s->stream_decompression_method =
1613         GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS;
1614   }
1615
1616   if (s->stream_decompression_method !=
1617       GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS) {
1618     s->stream_decompression_ctx = nullptr;
1619     grpc_slice_buffer_init(&s->decompressed_data_buffer);
1620   }
1621 }
1622
1623 grpc_error_handle grpc_chttp2_header_parser_parse(void* hpack_parser,
1624                                                   grpc_chttp2_transport* t,
1625                                                   grpc_chttp2_stream* s,
1626                                                   const grpc_slice& slice,
1627                                                   int is_last) {
1628   GPR_TIMER_SCOPE("grpc_chttp2_header_parser_parse", 0);
1629   auto* parser = static_cast<grpc_core::HPackParser*>(hpack_parser);
1630   if (s != nullptr) {
1631     s->stats.incoming.header_bytes += GRPC_SLICE_LENGTH(slice);
1632   }
1633   grpc_error_handle error = parser->Parse(slice);
1634   if (error != GRPC_ERROR_NONE) {
1635     return error;
1636   }
1637   if (is_last) {
1638     if (parser->is_boundary() && !parser->is_in_begin_state()) {
1639       return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1640           "end of header frame not aligned with a hpack record boundary");
1641     }
1642     /* need to check for null stream: this can occur if we receive an invalid
1643        stream id on a header */
1644     if (s != nullptr) {
1645       if (parser->is_boundary()) {
1646         if (s->header_frames_received == GPR_ARRAY_SIZE(s->metadata_buffer)) {
1647           return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1648               "Too many trailer frames");
1649         }
1650         /* Process stream compression md element if it exists */
1651         if (s->header_frames_received ==
1652             0) { /* Only acts on initial metadata */
1653           parse_stream_compression_md(t, s, &s->metadata_buffer[0].batch);
1654         }
1655         s->published_metadata[s->header_frames_received] =
1656             GRPC_METADATA_PUBLISHED_FROM_WIRE;
1657         maybe_complete_funcs[s->header_frames_received](t, s);
1658         s->header_frames_received++;
1659       }
1660       if (parser->is_eof()) {
1661         if (t->is_client && !s->write_closed) {
1662           /* server eof ==> complete closure; we may need to forcefully close
1663              the stream. Wait until the combiner lock is ready to be released
1664              however -- it might be that we receive a RST_STREAM following this
1665              and can avoid the extra write */
1666           GRPC_CHTTP2_STREAM_REF(s, "final_rst");
1667           t->combiner->FinallyRun(
1668               GRPC_CLOSURE_CREATE(force_client_rst_stream, s, nullptr),
1669               GRPC_ERROR_NONE);
1670         }
1671         grpc_chttp2_mark_stream_closed(t, s, true, false, GRPC_ERROR_NONE);
1672       }
1673     }
1674     parser->FinishFrame();
1675   }
1676   return GRPC_ERROR_NONE;
1677 }