Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/transport/chttp2/hpack_parser.h" |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 35 | #include "src/core/transport/chttp2/internal.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 37 | #include <assert.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <stddef.h> |
| 39 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 41 | /* This is here for grpc_is_binary_header |
| 42 | * TODO(murgatroid99): Remove this |
| 43 | */ |
| 44 | #include <grpc/grpc.h> |
| 45 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc/support/alloc.h> |
| 47 | #include <grpc/support/log.h> |
| 48 | #include <grpc/support/port_platform.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc/support/useful.h> |
| 50 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 51 | #include "src/core/profiling/timers.h" |
| 52 | #include "src/core/support/string.h" |
| 53 | #include "src/core/transport/chttp2/bin_encoder.h" |
| 54 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | typedef enum { |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 56 | NOT_BINARY, |
| 57 | B64_BYTE0, |
| 58 | B64_BYTE1, |
| 59 | B64_BYTE2, |
| 60 | B64_BYTE3 |
| 61 | } binary_state; |
| 62 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | /* How parsing works: |
| 64 | |
| 65 | The parser object keeps track of a function pointer which represents the |
| 66 | current parse state. |
| 67 | |
| 68 | Each time new bytes are presented, we call into the current state, which |
| 69 | recursively parses until all bytes in the given chunk are exhausted. |
| 70 | |
| 71 | The parse state that terminates then saves its function pointer to be the |
| 72 | current state so that it can resume when more bytes are available. |
| 73 | |
| 74 | It's expected that most optimizing compilers will turn this code into |
| 75 | a set of indirect jumps, and so not waste stack space. */ |
| 76 | |
| 77 | /* forward declarations for parsing states */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 78 | static int parse_begin(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 79 | const uint8_t *end); |
| 80 | static int parse_error(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 81 | const uint8_t *end); |
| 82 | static int parse_illegal_op(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 83 | const uint8_t *end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 85 | static int parse_string_prefix(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 86 | const uint8_t *end); |
| 87 | static int parse_key_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 88 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | static int parse_value_string_with_indexed_key(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 90 | const uint8_t *cur, |
| 91 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | static int parse_value_string_with_literal_key(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 93 | const uint8_t *cur, |
| 94 | const uint8_t *end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 96 | static int parse_value0(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 97 | const uint8_t *end); |
| 98 | static int parse_value1(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 99 | const uint8_t *end); |
| 100 | static int parse_value2(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 101 | const uint8_t *end); |
| 102 | static int parse_value3(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 103 | const uint8_t *end); |
| 104 | static int parse_value4(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 105 | const uint8_t *end); |
| 106 | static int parse_value5up(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 107 | const uint8_t *end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 109 | static int parse_indexed_field(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 110 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | static int parse_indexed_field_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 112 | const uint8_t *cur, const uint8_t *end); |
| 113 | static int parse_lithdr_incidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 114 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | static int parse_lithdr_incidx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 116 | const uint8_t *cur, const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static int parse_lithdr_incidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 118 | const uint8_t *cur, const uint8_t *end); |
| 119 | static int parse_lithdr_notidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 120 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | static int parse_lithdr_notidx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 122 | const uint8_t *cur, const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | static int parse_lithdr_notidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 124 | const uint8_t *cur, const uint8_t *end); |
| 125 | static int parse_lithdr_nvridx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 126 | const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | static int parse_lithdr_nvridx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 128 | const uint8_t *cur, const uint8_t *end); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | static int parse_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 130 | const uint8_t *cur, const uint8_t *end); |
| 131 | static int parse_max_tbl_size(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 132 | const uint8_t *end); |
| 133 | static int parse_max_tbl_size_x(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 134 | const uint8_t *end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | |
| 136 | /* we translate the first byte of a hpack field into one of these decoding |
| 137 | cases, then use a lookup table to jump directly to the appropriate parser. |
| 138 | |
| 139 | _X => the integer index is all ones, meaning we need to do varint decoding |
| 140 | _V => the integer index is all zeros, meaning we need to decode an additional |
| 141 | string value */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | typedef enum { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | INDEXED_FIELD, |
| 144 | INDEXED_FIELD_X, |
| 145 | LITHDR_INCIDX, |
| 146 | LITHDR_INCIDX_X, |
| 147 | LITHDR_INCIDX_V, |
| 148 | LITHDR_NOTIDX, |
| 149 | LITHDR_NOTIDX_X, |
| 150 | LITHDR_NOTIDX_V, |
| 151 | LITHDR_NVRIDX, |
| 152 | LITHDR_NVRIDX_X, |
| 153 | LITHDR_NVRIDX_V, |
| 154 | MAX_TBL_SIZE, |
| 155 | MAX_TBL_SIZE_X, |
| 156 | ILLEGAL |
| 157 | } first_byte_type; |
| 158 | |
| 159 | /* jump table of parse state functions -- order must match first_byte_type |
| 160 | above */ |
| 161 | static const grpc_chttp2_hpack_parser_state first_byte_action[] = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 162 | parse_indexed_field, parse_indexed_field_x, parse_lithdr_incidx, |
| 163 | parse_lithdr_incidx_x, parse_lithdr_incidx_v, parse_lithdr_notidx, |
| 164 | parse_lithdr_notidx_x, parse_lithdr_notidx_v, parse_lithdr_nvridx, |
| 165 | parse_lithdr_nvridx_x, parse_lithdr_nvridx_v, parse_max_tbl_size, |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 166 | parse_max_tbl_size_x, parse_illegal_op}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | |
| 168 | /* indexes the first byte to a parse state function - generated by |
| 169 | gen_hpack_tables.c */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 170 | static const uint8_t first_byte_lut[256] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | LITHDR_NOTIDX_V, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, |
| 172 | LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, |
| 173 | LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, |
| 174 | LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX_X, |
| 175 | LITHDR_NVRIDX_V, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, |
| 176 | LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, |
| 177 | LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, |
| 178 | LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX_X, |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 179 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 181 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 182 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 183 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 184 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 185 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, |
| 186 | MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE_X, |
| 187 | LITHDR_INCIDX_V, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 188 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 189 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 190 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 191 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 192 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 193 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 194 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 195 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 196 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 197 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 198 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 199 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 200 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 201 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, |
| 202 | LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX_X, |
| 203 | ILLEGAL, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 204 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 205 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 206 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 207 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 208 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 209 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 210 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 211 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 212 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 213 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 214 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 215 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 216 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 217 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 218 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 219 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 220 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 221 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 222 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 223 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 224 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 225 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 226 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 227 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 228 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 229 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 230 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 231 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 232 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 233 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, |
| 234 | INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD_X, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 235 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | |
| 237 | /* state table for huffman decoding: given a state, gives an index/16 into |
| 238 | next_sub_tbl. Taking that index and adding the value of the nibble being |
| 239 | considered returns the next state. |
| 240 | |
| 241 | generated by gen_hpack_tables.c */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 242 | static const uint8_t next_tbl[256] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | 0, 1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 3, 3, 9, 10, 11, 1, 1, |
| 244 | 1, 12, 1, 2, 13, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, |
| 245 | 14, 1, 15, 16, 1, 17, 1, 15, 2, 7, 3, 18, 19, 1, 1, 1, 1, 20, 1, 1, |
| 246 | 1, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 7, 21, 1, 22, 1, 1, 1, 1, 1, |
| 247 | 1, 1, 1, 15, 2, 2, 2, 2, 2, 2, 23, 24, 25, 1, 1, 1, 1, 2, 2, 2, |
| 248 | 26, 3, 3, 27, 10, 28, 1, 1, 1, 1, 1, 1, 2, 3, 29, 10, 30, 1, 1, 1, |
| 249 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 31, 1, 1, 1, 1, 1, 1, 1, 2, |
| 250 | 2, 2, 2, 2, 2, 2, 2, 32, 1, 1, 15, 33, 1, 34, 35, 9, 36, 1, 1, 1, |
| 251 | 1, 1, 1, 1, 37, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 26, 9, |
| 252 | 38, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 2, 2, 26, 3, 3, 39, 1, 1, 1, |
| 253 | 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 7, 3, 3, 3, 40, 2, |
| 254 | 41, 1, 1, 1, 42, 43, 1, 1, 44, 1, 1, 1, 1, 15, 2, 2, 2, 2, 2, 2, |
| 255 | 3, 3, 3, 45, 46, 1, 1, 2, 2, 2, 35, 3, 3, 18, 47, 2, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 256 | }; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 257 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | /* next state, based upon current state and the current nibble: see above. |
| 259 | generated by gen_hpack_tables.c */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 260 | static const int16_t next_sub_tbl[48 * 16] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | 1, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, |
| 262 | 218, 2, 6, 10, 13, 14, 15, 16, 17, 2, 6, 10, 13, 14, 15, |
| 263 | 16, 17, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11, 24, 3, |
| 264 | 7, 11, 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, |
| 265 | 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, |
| 266 | 199, 200, 201, 202, 203, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, |
| 267 | 0, 0, 0, 0, 0, 0, 9, 133, 134, 135, 136, 137, 138, 139, 140, |
| 268 | 141, 142, 143, 144, 145, 146, 147, 3, 7, 11, 24, 3, 7, 11, 24, |
| 269 | 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 12, 132, 4, 8, 4, 8, 4, 8, |
| 271 | 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, 18, 19, 20, 21, 4, 8, 4, |
| 274 | 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 22, 23, 91, 25, 26, |
| 275 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 3, |
| 276 | 7, 11, 24, 3, 7, 11, 24, 0, 0, 0, 0, 0, 41, 42, 43, |
| 277 | 2, 6, 10, 13, 14, 15, 16, 17, 3, 7, 11, 24, 3, 7, 11, |
| 278 | 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, |
| 279 | 44, 45, 2, 6, 10, 13, 14, 15, 16, 17, 46, 47, 48, 49, 50, |
| 280 | 51, 52, 57, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | 0, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, |
| 283 | 68, 69, 70, 71, 72, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 73, 75, 76, 77, 78, 79, 80, 81, 82, |
| 285 | 83, 84, 85, 86, 87, 88, 89, 90, 3, 7, 11, 24, 3, 7, 11, |
| 286 | 24, 3, 7, 11, 24, 0, 0, 0, 0, 3, 7, 11, 24, 3, 7, |
| 287 | 11, 24, 4, 8, 4, 8, 0, 0, 0, 92, 0, 0, 0, 93, 94, |
| 288 | 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 3, 7, 11, 24, |
| 289 | 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, |
| 290 | 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 4, |
| 292 | 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, |
| 293 | 0, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, |
| 294 | 131, 2, 6, 10, 13, 14, 15, 16, 17, 4, 8, 4, 8, 4, 8, |
| 295 | 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 148, |
| 296 | 149, 150, 151, 3, 7, 11, 24, 4, 8, 4, 8, 0, 0, 0, 0, |
| 297 | 0, 0, 152, 153, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11, |
| 298 | 24, 154, 155, 156, 164, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, |
| 299 | 11, 24, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 300 | 157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 169, 170, 171, 172, |
| 301 | 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, |
| 302 | 188, 189, 190, 191, 192, 193, 194, 195, 196, 4, 8, 4, 8, 4, 8, |
| 303 | 4, 8, 4, 8, 4, 8, 4, 8, 197, 198, 4, 8, 4, 8, 4, |
| 304 | 8, 4, 8, 0, 0, 0, 0, 0, 0, 219, 220, 3, 7, 11, 24, |
| 305 | 4, 8, 4, 8, 4, 8, 0, 0, 221, 222, 223, 224, 3, 7, 11, |
| 306 | 24, 3, 7, 11, 24, 4, 8, 4, 8, 4, 8, 225, 228, 4, 8, |
| 307 | 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 226, 227, 229, |
| 308 | 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, |
| 309 | 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, |
| 310 | 0, 0, 0, 0, 0, 0, 0, 245, 246, 247, 248, 249, 250, 251, 252, |
| 311 | 253, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 312 | 0, 0, 255, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 313 | }; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 314 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | /* emission table: indexed like next_tbl, ultimately gives the byte to be |
| 316 | emitted, or -1 for no byte, or 256 for end of stream |
| 317 | |
| 318 | generated by gen_hpack_tables.c */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 319 | static const uint16_t emit_tbl[256] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 8, 9, 10, 11, 12, 13, |
| 321 | 14, 15, 16, 17, 18, 19, 20, 21, 22, 0, 23, 24, 25, 26, 27, |
| 322 | 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, |
| 323 | 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 0, 55, 56, |
| 324 | 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 0, |
| 325 | 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, |
| 326 | 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, |
| 327 | 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, |
| 328 | 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, |
| 329 | 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, |
| 330 | 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 0, |
| 331 | 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, |
| 332 | 0, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, |
| 333 | 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, |
| 334 | 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, |
| 335 | 219, 220, 221, 0, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, |
| 336 | 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 337 | 248, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 338 | }; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 339 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 340 | /* generated by gen_hpack_tables.c */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 341 | static const int16_t emit_sub_tbl[249 * 16] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 343 | -1, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, |
| 344 | 49, 49, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 97, |
| 345 | 97, 97, 97, 48, 48, 49, 49, 50, 50, 97, 97, 99, 99, 101, 101, |
| 346 | 105, 105, 111, 111, 48, 49, 50, 97, 99, 101, 105, 111, 115, 116, -1, |
| 347 | -1, -1, -1, -1, -1, 32, 32, 32, 32, 32, 32, 32, 32, 37, 37, |
| 348 | 37, 37, 37, 37, 37, 37, 99, 99, 99, 99, 101, 101, 101, 101, 105, |
| 349 | 105, 105, 105, 111, 111, 111, 111, 115, 115, 116, 116, 32, 37, 45, 46, |
| 350 | 47, 51, 52, 53, 54, 55, 56, 57, 61, 61, 61, 61, 61, 61, 61, |
| 351 | 61, 65, 65, 65, 65, 65, 65, 65, 65, 115, 115, 115, 115, 116, 116, |
| 352 | 116, 116, 32, 32, 37, 37, 45, 45, 46, 46, 61, 65, 95, 98, 100, |
| 353 | 102, 103, 104, 108, 109, 110, 112, 114, 117, -1, -1, 58, 58, 58, 58, |
| 354 | 58, 58, 58, 58, 66, 66, 66, 66, 66, 66, 66, 66, 47, 47, 51, |
| 355 | 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 61, 61, |
| 356 | 65, 65, 95, 95, 98, 98, 100, 100, 102, 102, 103, 103, 104, 104, 108, |
| 357 | 108, 109, 109, 110, 110, 112, 112, 114, 114, 117, 117, 58, 66, 67, 68, |
| 358 | 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, |
| 359 | 84, 85, 86, 87, 89, 106, 107, 113, 118, 119, 120, 121, 122, -1, -1, |
| 360 | -1, -1, 38, 38, 38, 38, 38, 38, 38, 38, 42, 42, 42, 42, 42, |
| 361 | 42, 42, 42, 44, 44, 44, 44, 44, 44, 44, 44, 59, 59, 59, 59, |
| 362 | 59, 59, 59, 59, 88, 88, 88, 88, 88, 88, 88, 88, 90, 90, 90, |
| 363 | 90, 90, 90, 90, 90, 33, 33, 34, 34, 40, 40, 41, 41, 63, 63, |
| 364 | 39, 43, 124, -1, -1, -1, 35, 35, 35, 35, 35, 35, 35, 35, 62, |
| 365 | 62, 62, 62, 62, 62, 62, 62, 0, 0, 0, 0, 36, 36, 36, 36, |
| 366 | 64, 64, 64, 64, 91, 91, 91, 91, 69, 69, 69, 69, 69, 69, 69, |
| 367 | 69, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, |
| 368 | 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73, 73, 73, |
| 369 | 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, |
| 370 | 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77, |
| 371 | 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79, |
| 372 | 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 81, |
| 373 | 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82, |
| 374 | 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84, |
| 375 | 84, 85, 85, 85, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 86, |
| 376 | 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 89, 89, 89, 89, 89, |
| 377 | 89, 89, 89, 106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, |
| 378 | 107, 107, 107, 107, 113, 113, 113, 113, 113, 113, 113, 113, 118, 118, 118, |
| 379 | 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 119, 120, 120, |
| 380 | 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 122, |
| 381 | 122, 122, 122, 122, 122, 122, 122, 38, 38, 38, 38, 42, 42, 42, 42, |
| 382 | 44, 44, 44, 44, 59, 59, 59, 59, 88, 88, 88, 88, 90, 90, 90, |
| 383 | 90, 33, 34, 40, 41, 63, -1, -1, -1, 39, 39, 39, 39, 39, 39, |
| 384 | 39, 39, 43, 43, 43, 43, 43, 43, 43, 43, 124, 124, 124, 124, 124, |
| 385 | 124, 124, 124, 35, 35, 35, 35, 62, 62, 62, 62, 0, 0, 36, 36, |
| 386 | 64, 64, 91, 91, 93, 93, 126, 126, 94, 125, -1, -1, 60, 60, 60, |
| 387 | 60, 60, 60, 60, 60, 96, 96, 96, 96, 96, 96, 96, 96, 123, 123, |
| 388 | 123, 123, 123, 123, 123, 123, -1, -1, -1, -1, -1, -1, -1, -1, 92, |
| 389 | 92, 92, 92, 92, 92, 92, 92, 195, 195, 195, 195, 195, 195, 195, 195, |
| 390 | 208, 208, 208, 208, 208, 208, 208, 208, 128, 128, 128, 128, 130, 130, 130, |
| 391 | 130, 131, 131, 131, 131, 162, 162, 162, 162, 184, 184, 184, 184, 194, 194, |
| 392 | 194, 194, 224, 224, 224, 224, 226, 226, 226, 226, 153, 153, 161, 161, 167, |
| 393 | 167, 172, 172, 176, 176, 177, 177, 179, 179, 209, 209, 216, 216, 217, 217, |
| 394 | 227, 227, 229, 229, 230, 230, 129, 132, 133, 134, 136, 146, 154, 156, 160, |
| 395 | 163, 164, 169, 170, 173, 178, 181, 185, 186, 187, 189, 190, 196, 198, 228, |
| 396 | 232, 233, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 135, |
| 397 | 135, 135, 135, 135, 135, 135, 135, 137, 137, 137, 137, 137, 137, 137, 137, |
| 398 | 138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139, |
| 399 | 139, 140, 140, 140, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141, |
| 400 | 141, 141, 143, 143, 143, 143, 143, 143, 143, 143, 147, 147, 147, 147, 147, |
| 401 | 147, 147, 147, 149, 149, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, |
| 402 | 150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152, |
| 403 | 152, 152, 152, 152, 152, 155, 155, 155, 155, 155, 155, 155, 155, 157, 157, |
| 404 | 157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 158, 165, |
| 405 | 165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 166, 166, 166, |
| 406 | 168, 168, 168, 168, 168, 168, 168, 168, 174, 174, 174, 174, 174, 174, 174, |
| 407 | 174, 175, 175, 175, 175, 175, 175, 175, 175, 180, 180, 180, 180, 180, 180, |
| 408 | 180, 180, 182, 182, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183, |
| 409 | 183, 183, 183, 188, 188, 188, 188, 188, 188, 188, 188, 191, 191, 191, 191, |
| 410 | 191, 191, 191, 191, 197, 197, 197, 197, 197, 197, 197, 197, 231, 231, 231, |
| 411 | 231, 231, 231, 231, 231, 239, 239, 239, 239, 239, 239, 239, 239, 9, 9, |
| 412 | 9, 9, 142, 142, 142, 142, 144, 144, 144, 144, 145, 145, 145, 145, 148, |
| 413 | 148, 148, 148, 159, 159, 159, 159, 171, 171, 171, 171, 206, 206, 206, 206, |
| 414 | 215, 215, 215, 215, 225, 225, 225, 225, 236, 236, 236, 236, 237, 237, 237, |
| 415 | 237, 199, 199, 207, 207, 234, 234, 235, 235, 192, 193, 200, 201, 202, 205, |
| 416 | 210, 213, 218, 219, 238, 240, 242, 243, 255, -1, 203, 203, 203, 203, 203, |
| 417 | 203, 203, 203, 204, 204, 204, 204, 204, 204, 204, 204, 211, 211, 211, 211, |
| 418 | 211, 211, 211, 211, 212, 212, 212, 212, 212, 212, 212, 212, 214, 214, 214, |
| 419 | 214, 214, 214, 214, 214, 221, 221, 221, 221, 221, 221, 221, 221, 222, 222, |
| 420 | 222, 222, 222, 222, 222, 222, 223, 223, 223, 223, 223, 223, 223, 223, 241, |
| 421 | 241, 241, 241, 241, 241, 241, 241, 244, 244, 244, 244, 244, 244, 244, 244, |
| 422 | 245, 245, 245, 245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 246, 246, |
| 423 | 246, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 248, 248, |
| 424 | 248, 248, 250, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251, |
| 425 | 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, 253, |
| 426 | 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 2, 2, 2, |
| 427 | 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, |
| 428 | 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 11, 11, 11, 11, 12, |
| 429 | 12, 12, 12, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, |
| 430 | 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, |
| 431 | 20, 21, 21, 21, 21, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, |
| 432 | 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, |
| 433 | 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 127, 127, 127, 127, |
| 434 | 220, 220, 220, 220, 249, 249, 249, 249, 10, 13, 22, 256, 93, 93, 93, |
| 435 | 93, 126, 126, 126, 126, 94, 94, 125, 125, 60, 96, 123, -1, 92, 195, |
| 436 | 208, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 128, |
| 437 | 128, 128, 128, 128, 128, 128, 128, 130, 130, 130, 130, 130, 130, 130, 130, |
| 438 | 131, 131, 131, 131, 131, 131, 131, 131, 162, 162, 162, 162, 162, 162, 162, |
| 439 | 162, 184, 184, 184, 184, 184, 184, 184, 184, 194, 194, 194, 194, 194, 194, |
| 440 | 194, 194, 224, 224, 224, 224, 224, 224, 224, 224, 226, 226, 226, 226, 226, |
| 441 | 226, 226, 226, 153, 153, 153, 153, 161, 161, 161, 161, 167, 167, 167, 167, |
| 442 | 172, 172, 172, 172, 176, 176, 176, 176, 177, 177, 177, 177, 179, 179, 179, |
| 443 | 179, 209, 209, 209, 209, 216, 216, 216, 216, 217, 217, 217, 217, 227, 227, |
| 444 | 227, 227, 229, 229, 229, 229, 230, 230, 230, 230, 129, 129, 132, 132, 133, |
| 445 | 133, 134, 134, 136, 136, 146, 146, 154, 154, 156, 156, 160, 160, 163, 163, |
| 446 | 164, 164, 169, 169, 170, 170, 173, 173, 178, 178, 181, 181, 185, 185, 186, |
| 447 | 186, 187, 187, 189, 189, 190, 190, 196, 196, 198, 198, 228, 228, 232, 232, |
| 448 | 233, 233, 1, 135, 137, 138, 139, 140, 141, 143, 147, 149, 150, 151, 152, |
| 449 | 155, 157, 158, 165, 166, 168, 174, 175, 180, 182, 183, 188, 191, 197, 231, |
| 450 | 239, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 9, 9, |
| 451 | 9, 9, 9, 9, 9, 142, 142, 142, 142, 142, 142, 142, 142, 144, 144, |
| 452 | 144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 148, |
| 453 | 148, 148, 148, 148, 148, 148, 148, 159, 159, 159, 159, 159, 159, 159, 159, |
| 454 | 171, 171, 171, 171, 171, 171, 171, 171, 206, 206, 206, 206, 206, 206, 206, |
| 455 | 206, 215, 215, 215, 215, 215, 215, 215, 215, 225, 225, 225, 225, 225, 225, |
| 456 | 225, 225, 236, 236, 236, 236, 236, 236, 236, 236, 237, 237, 237, 237, 237, |
| 457 | 237, 237, 237, 199, 199, 199, 199, 207, 207, 207, 207, 234, 234, 234, 234, |
| 458 | 235, 235, 235, 235, 192, 192, 193, 193, 200, 200, 201, 201, 202, 202, 205, |
| 459 | 205, 210, 210, 213, 213, 218, 218, 219, 219, 238, 238, 240, 240, 242, 242, |
| 460 | 243, 243, 255, 255, 203, 204, 211, 212, 214, 221, 222, 223, 241, 244, 245, |
| 461 | 246, 247, 248, 250, 251, 252, 253, 254, -1, -1, -1, -1, -1, -1, -1, |
| 462 | -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, |
| 463 | 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, |
| 464 | 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, |
| 465 | 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, |
| 466 | 8, 8, 8, 8, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, |
| 467 | 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, |
| 468 | 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17, |
| 469 | 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, |
| 470 | 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, |
| 471 | 20, 21, 21, 21, 21, 21, 21, 21, 21, 23, 23, 23, 23, 23, 23, |
| 472 | 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, |
| 473 | 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, |
| 474 | 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, |
| 475 | 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, |
| 476 | 31, 31, 31, 31, 31, 31, 127, 127, 127, 127, 127, 127, 127, 127, 220, |
| 477 | 220, 220, 220, 220, 220, 220, 220, 249, 249, 249, 249, 249, 249, 249, 249, |
| 478 | 10, 10, 13, 13, 22, 22, 256, 256, 67, 67, 67, 67, 67, 67, 67, |
| 479 | 67, 68, 68, 68, 68, 68, 68, 68, 68, 95, 95, 95, 95, 95, 95, |
| 480 | 95, 95, 98, 98, 98, 98, 98, 98, 98, 98, 100, 100, 100, 100, 100, |
| 481 | 100, 100, 100, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, |
| 482 | 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 108, 108, 108, |
| 483 | 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 110, 110, |
| 484 | 110, 110, 110, 110, 110, 110, 112, 112, 112, 112, 112, 112, 112, 112, 114, |
| 485 | 114, 114, 114, 114, 114, 114, 114, 117, 117, 117, 117, 117, 117, 117, 117, |
| 486 | 58, 58, 58, 58, 66, 66, 66, 66, 67, 67, 67, 67, 68, 68, 68, |
| 487 | 68, 69, 69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 71, 72, 72, |
| 488 | 72, 72, 73, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 75, 76, |
| 489 | 76, 76, 76, 77, 77, 77, 77, 78, 78, 78, 78, 79, 79, 79, 79, |
| 490 | 80, 80, 80, 80, 81, 81, 81, 81, 82, 82, 82, 82, 83, 83, 83, |
| 491 | 83, 84, 84, 84, 84, 85, 85, 85, 85, 86, 86, 86, 86, 87, 87, |
| 492 | 87, 87, 89, 89, 89, 89, 106, 106, 106, 106, 107, 107, 107, 107, 113, |
| 493 | 113, 113, 113, 118, 118, 118, 118, 119, 119, 119, 119, 120, 120, 120, 120, |
| 494 | 121, 121, 121, 121, 122, 122, 122, 122, 38, 38, 42, 42, 44, 44, 59, |
| 495 | 59, 88, 88, 90, 90, -1, -1, -1, -1, 33, 33, 33, 33, 33, 33, |
| 496 | 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 40, 40, 40, 40, 40, |
| 497 | 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 63, 63, 63, 63, |
| 498 | 63, 63, 63, 63, 39, 39, 39, 39, 43, 43, 43, 43, 124, 124, 124, |
| 499 | 124, 35, 35, 62, 62, 0, 36, 64, 91, 93, 126, -1, -1, 94, 94, |
| 500 | 94, 94, 94, 94, 94, 94, 125, 125, 125, 125, 125, 125, 125, 125, 60, |
| 501 | 60, 60, 60, 96, 96, 96, 96, 123, 123, 123, 123, -1, -1, -1, -1, |
| 502 | 92, 92, 92, 92, 195, 195, 195, 195, 208, 208, 208, 208, 128, 128, 130, |
| 503 | 130, 131, 131, 162, 162, 184, 184, 194, 194, 224, 224, 226, 226, 153, 161, |
| 504 | 167, 172, 176, 177, 179, 209, 216, 217, 227, 229, 230, -1, -1, -1, -1, |
| 505 | -1, -1, -1, 129, 129, 129, 129, 129, 129, 129, 129, 132, 132, 132, 132, |
| 506 | 132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134, |
| 507 | 134, 134, 134, 134, 134, 136, 136, 136, 136, 136, 136, 136, 136, 146, 146, |
| 508 | 146, 146, 146, 146, 146, 146, 154, 154, 154, 154, 154, 154, 154, 154, 156, |
| 509 | 156, 156, 156, 156, 156, 156, 156, 160, 160, 160, 160, 160, 160, 160, 160, |
| 510 | 163, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164, 164, |
| 511 | 164, 169, 169, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 170, 170, |
| 512 | 170, 170, 173, 173, 173, 173, 173, 173, 173, 173, 178, 178, 178, 178, 178, |
| 513 | 178, 178, 178, 181, 181, 181, 181, 181, 181, 181, 181, 185, 185, 185, 185, |
| 514 | 185, 185, 185, 185, 186, 186, 186, 186, 186, 186, 186, 186, 187, 187, 187, |
| 515 | 187, 187, 187, 187, 187, 189, 189, 189, 189, 189, 189, 189, 189, 190, 190, |
| 516 | 190, 190, 190, 190, 190, 190, 196, 196, 196, 196, 196, 196, 196, 196, 198, |
| 517 | 198, 198, 198, 198, 198, 198, 198, 228, 228, 228, 228, 228, 228, 228, 228, |
| 518 | 232, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 233, 233, 233, |
| 519 | 233, 1, 1, 1, 1, 135, 135, 135, 135, 137, 137, 137, 137, 138, 138, |
| 520 | 138, 138, 139, 139, 139, 139, 140, 140, 140, 140, 141, 141, 141, 141, 143, |
| 521 | 143, 143, 143, 147, 147, 147, 147, 149, 149, 149, 149, 150, 150, 150, 150, |
| 522 | 151, 151, 151, 151, 152, 152, 152, 152, 155, 155, 155, 155, 157, 157, 157, |
| 523 | 157, 158, 158, 158, 158, 165, 165, 165, 165, 166, 166, 166, 166, 168, 168, |
| 524 | 168, 168, 174, 174, 174, 174, 175, 175, 175, 175, 180, 180, 180, 180, 182, |
| 525 | 182, 182, 182, 183, 183, 183, 183, 188, 188, 188, 188, 191, 191, 191, 191, |
| 526 | 197, 197, 197, 197, 231, 231, 231, 231, 239, 239, 239, 239, 9, 9, 142, |
| 527 | 142, 144, 144, 145, 145, 148, 148, 159, 159, 171, 171, 206, 206, 215, 215, |
| 528 | 225, 225, 236, 236, 237, 237, 199, 207, 234, 235, 192, 192, 192, 192, 192, |
| 529 | 192, 192, 192, 193, 193, 193, 193, 193, 193, 193, 193, 200, 200, 200, 200, |
| 530 | 200, 200, 200, 200, 201, 201, 201, 201, 201, 201, 201, 201, 202, 202, 202, |
| 531 | 202, 202, 202, 202, 202, 205, 205, 205, 205, 205, 205, 205, 205, 210, 210, |
| 532 | 210, 210, 210, 210, 210, 210, 213, 213, 213, 213, 213, 213, 213, 213, 218, |
| 533 | 218, 218, 218, 218, 218, 218, 218, 219, 219, 219, 219, 219, 219, 219, 219, |
| 534 | 238, 238, 238, 238, 238, 238, 238, 238, 240, 240, 240, 240, 240, 240, 240, |
| 535 | 240, 242, 242, 242, 242, 242, 242, 242, 242, 243, 243, 243, 243, 243, 243, |
| 536 | 243, 243, 255, 255, 255, 255, 255, 255, 255, 255, 203, 203, 203, 203, 204, |
| 537 | 204, 204, 204, 211, 211, 211, 211, 212, 212, 212, 212, 214, 214, 214, 214, |
| 538 | 221, 221, 221, 221, 222, 222, 222, 222, 223, 223, 223, 223, 241, 241, 241, |
| 539 | 241, 244, 244, 244, 244, 245, 245, 245, 245, 246, 246, 246, 246, 247, 247, |
| 540 | 247, 247, 248, 248, 248, 248, 250, 250, 250, 250, 251, 251, 251, 251, 252, |
| 541 | 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 2, 2, 3, 3, |
| 542 | 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 11, 11, 12, 12, 14, |
| 543 | 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, |
| 544 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, |
| 545 | 30, 31, 31, 127, 127, 220, 220, 249, 249, -1, -1, 10, 10, 10, 10, |
| 546 | 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13, 22, 22, 22, |
| 547 | 22, 22, 22, 22, 22, 256, 256, 256, 256, 256, 256, 256, 256, 45, 45, |
| 548 | 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47, |
| 549 | 47, 47, 47, 47, 47, 47, 47, 51, 51, 51, 51, 51, 51, 51, 51, |
| 550 | 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, |
| 551 | 53, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, |
| 552 | 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, |
| 553 | 57, 57, 57, 50, 50, 50, 50, 50, 50, 50, 50, 97, 97, 97, 97, |
| 554 | 97, 97, 97, 97, 99, 99, 99, 99, 99, 99, 99, 99, 101, 101, 101, |
| 555 | 101, 101, 101, 101, 101, 105, 105, 105, 105, 105, 105, 105, 105, 111, 111, |
| 556 | 111, 111, 111, 111, 111, 111, 115, 115, 115, 115, 115, 115, 115, 115, 116, |
| 557 | 116, 116, 116, 116, 116, 116, 116, 32, 32, 32, 32, 37, 37, 37, 37, |
| 558 | 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 51, 51, 51, |
| 559 | 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, |
| 560 | 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 61, 61, 61, 61, 65, |
| 561 | 65, 65, 65, 95, 95, 95, 95, 98, 98, 98, 98, 100, 100, 100, 100, |
| 562 | 102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 104, 104, 108, 108, 108, |
| 563 | 108, 109, 109, 109, 109, 110, 110, 110, 110, 112, 112, 112, 112, 114, 114, |
| 564 | 114, 114, 117, 117, 117, 117, 58, 58, 66, 66, 67, 67, 68, 68, 69, |
| 565 | 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, |
| 566 | 77, 77, 78, 78, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84, |
| 567 | 84, 85, 85, 86, 86, 87, 87, 89, 89, 106, 106, 107, 107, 113, 113, |
| 568 | 118, 118, 119, 119, 120, 120, 121, 121, 122, 122, 38, 42, 44, 59, 88, |
| 569 | 90, -1, -1, 33, 33, 33, 33, 34, 34, 34, 34, 40, 40, 40, 40, |
| 570 | 41, 41, 41, 41, 63, 63, 63, 63, 39, 39, 43, 43, 124, 124, 35, |
| 571 | 62, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 36, 36, |
| 572 | 36, 36, 36, 36, 36, 36, 64, 64, 64, 64, 64, 64, 64, 64, 91, |
| 573 | 91, 91, 91, 91, 91, 91, 91, 93, 93, 93, 93, 93, 93, 93, 93, |
| 574 | 126, 126, 126, 126, 126, 126, 126, 126, 94, 94, 94, 94, 125, 125, 125, |
| 575 | 125, 60, 60, 96, 96, 123, 123, -1, -1, 92, 92, 195, 195, 208, 208, |
| 576 | 128, 130, 131, 162, 184, 194, 224, 226, -1, -1, 153, 153, 153, 153, 153, |
| 577 | 153, 153, 153, 161, 161, 161, 161, 161, 161, 161, 161, 167, 167, 167, 167, |
| 578 | 167, 167, 167, 167, 172, 172, 172, 172, 172, 172, 172, 172, 176, 176, 176, |
| 579 | 176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 177, 177, 179, 179, |
| 580 | 179, 179, 179, 179, 179, 179, 209, 209, 209, 209, 209, 209, 209, 209, 216, |
| 581 | 216, 216, 216, 216, 216, 216, 216, 217, 217, 217, 217, 217, 217, 217, 217, |
| 582 | 227, 227, 227, 227, 227, 227, 227, 227, 229, 229, 229, 229, 229, 229, 229, |
| 583 | 229, 230, 230, 230, 230, 230, 230, 230, 230, 129, 129, 129, 129, 132, 132, |
| 584 | 132, 132, 133, 133, 133, 133, 134, 134, 134, 134, 136, 136, 136, 136, 146, |
| 585 | 146, 146, 146, 154, 154, 154, 154, 156, 156, 156, 156, 160, 160, 160, 160, |
| 586 | 163, 163, 163, 163, 164, 164, 164, 164, 169, 169, 169, 169, 170, 170, 170, |
| 587 | 170, 173, 173, 173, 173, 178, 178, 178, 178, 181, 181, 181, 181, 185, 185, |
| 588 | 185, 185, 186, 186, 186, 186, 187, 187, 187, 187, 189, 189, 189, 189, 190, |
| 589 | 190, 190, 190, 196, 196, 196, 196, 198, 198, 198, 198, 228, 228, 228, 228, |
| 590 | 232, 232, 232, 232, 233, 233, 233, 233, 1, 1, 135, 135, 137, 137, 138, |
| 591 | 138, 139, 139, 140, 140, 141, 141, 143, 143, 147, 147, 149, 149, 150, 150, |
| 592 | 151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 165, 165, 166, 166, 168, |
| 593 | 168, 174, 174, 175, 175, 180, 180, 182, 182, 183, 183, 188, 188, 191, 191, |
| 594 | 197, 197, 231, 231, 239, 239, 9, 142, 144, 145, 148, 159, 171, 206, 215, |
| 595 | 225, 236, 237, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 199, 199, |
| 596 | 199, 199, 199, 199, 199, 199, 207, 207, 207, 207, 207, 207, 207, 207, 234, |
| 597 | 234, 234, 234, 234, 234, 234, 234, 235, 235, 235, 235, 235, 235, 235, 235, |
| 598 | 192, 192, 192, 192, 193, 193, 193, 193, 200, 200, 200, 200, 201, 201, 201, |
| 599 | 201, 202, 202, 202, 202, 205, 205, 205, 205, 210, 210, 210, 210, 213, 213, |
| 600 | 213, 213, 218, 218, 218, 218, 219, 219, 219, 219, 238, 238, 238, 238, 240, |
| 601 | 240, 240, 240, 242, 242, 242, 242, 243, 243, 243, 243, 255, 255, 255, 255, |
| 602 | 203, 203, 204, 204, 211, 211, 212, 212, 214, 214, 221, 221, 222, 222, 223, |
| 603 | 223, 241, 241, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 250, 250, |
| 604 | 251, 251, 252, 252, 253, 253, 254, 254, 2, 3, 4, 5, 6, 7, 8, |
| 605 | 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, |
| 606 | 28, 29, 30, 31, 127, 220, 249, -1, 10, 10, 10, 10, 13, 13, 13, |
| 607 | 13, 22, 22, 22, 22, 256, 256, 256, 256, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 608 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 609 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 610 | static const uint8_t inverse_base64[256] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 611 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 612 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 613 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, |
| 614 | 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 615 | 255, 64, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 616 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 617 | 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, |
| 618 | 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 619 | 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 620 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 621 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 622 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 623 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 624 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 625 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 626 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 627 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 628 | 255, |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 629 | }; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 630 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 631 | /* emission helpers */ |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 632 | static int on_hdr(grpc_chttp2_hpack_parser *p, grpc_mdelem *md, |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 633 | int add_to_table) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 634 | if (add_to_table) { |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 635 | if (!grpc_chttp2_hptbl_add(&p->table, md)) { |
| 636 | return 0; |
| 637 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 638 | } |
| 639 | p->on_header(p->on_header_user_data, md); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 640 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 643 | static grpc_mdstr *take_string(grpc_chttp2_hpack_parser *p, |
| 644 | grpc_chttp2_hpack_parser_string *str) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 645 | grpc_mdstr *s = grpc_mdstr_from_buffer((uint8_t *)str->str, str->length); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 646 | str->length = 0; |
| 647 | return s; |
| 648 | } |
| 649 | |
| 650 | /* jump to the next state */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 651 | static int parse_next(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 652 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 653 | p->state = *p->next_state++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 654 | return p->state(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* begin parsing a header: all functionality is encoded into lookup tables |
| 658 | above */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 659 | static int parse_begin(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 660 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 661 | if (cur == end) { |
| 662 | p->state = parse_begin; |
| 663 | return 1; |
| 664 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 665 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 666 | return first_byte_action[first_byte_lut[*cur]](p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* stream dependency and prioritization data: we just skip it */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 670 | static int parse_stream_weight(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 671 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 672 | if (cur == end) { |
| 673 | p->state = parse_stream_weight; |
| 674 | return 1; |
| 675 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 676 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 677 | return p->after_prioritization(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 680 | static int parse_stream_dep3(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 681 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 682 | if (cur == end) { |
| 683 | p->state = parse_stream_dep3; |
| 684 | return 1; |
| 685 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 686 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 687 | return parse_stream_weight(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 690 | static int parse_stream_dep2(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 691 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 692 | if (cur == end) { |
| 693 | p->state = parse_stream_dep2; |
| 694 | return 1; |
| 695 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 696 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 697 | return parse_stream_dep3(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 700 | static int parse_stream_dep1(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 701 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 702 | if (cur == end) { |
| 703 | p->state = parse_stream_dep1; |
| 704 | return 1; |
| 705 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 706 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 707 | return parse_stream_dep2(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 710 | static int parse_stream_dep0(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 711 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 712 | if (cur == end) { |
| 713 | p->state = parse_stream_dep0; |
| 714 | return 1; |
| 715 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 716 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 717 | return parse_stream_dep1(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* emit an indexed field; for now just logs it to console; jumps to |
| 721 | begin the next field on completion */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 722 | static int finish_indexed_field(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 723 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 724 | grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 725 | if (md == NULL) { |
| 726 | gpr_log(GPR_ERROR, "Invalid HPACK index received: %d", p->index); |
| 727 | return 0; |
| 728 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 729 | GRPC_MDELEM_REF(md); |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 730 | return on_hdr(p, md, 0) && parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* parse an indexed field with index < 127 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 734 | static int parse_indexed_field(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 735 | const uint8_t *end) { |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 736 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 737 | p->index = (*cur) & 0x7f; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 738 | return finish_indexed_field(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* parse an indexed field with index >= 127 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 742 | static int parse_indexed_field_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 743 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 744 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 745 | finish_indexed_field}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 746 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 747 | p->next_state = and_then; |
| 748 | p->index = 0x7f; |
| 749 | p->parsing.value = &p->index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 750 | return parse_value0(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* finish a literal header with incremental indexing: just log, and jump to ' |
| 754 | begin */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 755 | static int finish_lithdr_incidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 756 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 757 | grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index); |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 758 | GPR_ASSERT(md != NULL); /* handled in string parsing */ |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 759 | return on_hdr(p, grpc_mdelem_from_metadata_strings(GRPC_MDSTR_REF(md->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 760 | take_string(p, &p->value)), |
| 761 | 1) && |
| 762 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* finish a literal header with incremental indexing with no index */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 766 | static int finish_lithdr_incidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 767 | const uint8_t *cur, const uint8_t *end) { |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 768 | return on_hdr(p, grpc_mdelem_from_metadata_strings(take_string(p, &p->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 769 | take_string(p, &p->value)), |
| 770 | 1) && |
| 771 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /* parse a literal header with incremental indexing; index < 63 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 775 | static int parse_lithdr_incidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 776 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 777 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 778 | parse_value_string_with_indexed_key, finish_lithdr_incidx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 779 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 780 | p->next_state = and_then; |
| 781 | p->index = (*cur) & 0x3f; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 782 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* parse a literal header with incremental indexing; index >= 63 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 786 | static int parse_lithdr_incidx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 787 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 788 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 789 | parse_string_prefix, parse_value_string_with_indexed_key, |
| 790 | finish_lithdr_incidx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 791 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 792 | p->next_state = and_then; |
| 793 | p->index = 0x3f; |
| 794 | p->parsing.value = &p->index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 795 | return parse_value0(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* parse a literal header with incremental indexing; index = 0 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 799 | static int parse_lithdr_incidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 800 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 801 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 802 | parse_key_string, parse_string_prefix, |
| 803 | parse_value_string_with_literal_key, finish_lithdr_incidx_v}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 804 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 805 | p->next_state = and_then; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 806 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* finish a literal header without incremental indexing */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 810 | static int finish_lithdr_notidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 811 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 812 | grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index); |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 813 | GPR_ASSERT(md != NULL); /* handled in string parsing */ |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 814 | return on_hdr(p, grpc_mdelem_from_metadata_strings(GRPC_MDSTR_REF(md->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 815 | take_string(p, &p->value)), |
| 816 | 0) && |
| 817 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* finish a literal header without incremental indexing with index = 0 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 821 | static int finish_lithdr_notidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 822 | const uint8_t *cur, const uint8_t *end) { |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 823 | return on_hdr(p, grpc_mdelem_from_metadata_strings(take_string(p, &p->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 824 | take_string(p, &p->value)), |
| 825 | 0) && |
| 826 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* parse a literal header without incremental indexing; index < 15 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 830 | static int parse_lithdr_notidx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 831 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 832 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 833 | parse_value_string_with_indexed_key, finish_lithdr_notidx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 834 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 835 | p->next_state = and_then; |
| 836 | p->index = (*cur) & 0xf; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 837 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* parse a literal header without incremental indexing; index >= 15 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 841 | static int parse_lithdr_notidx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 842 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 843 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 844 | parse_string_prefix, parse_value_string_with_indexed_key, |
| 845 | finish_lithdr_notidx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 846 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 847 | p->next_state = and_then; |
| 848 | p->index = 0xf; |
| 849 | p->parsing.value = &p->index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 850 | return parse_value0(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* parse a literal header without incremental indexing; index == 0 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 854 | static int parse_lithdr_notidx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 855 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 856 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 857 | parse_key_string, parse_string_prefix, |
| 858 | parse_value_string_with_literal_key, finish_lithdr_notidx_v}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 859 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 860 | p->next_state = and_then; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 861 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* finish a literal header that is never indexed */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 865 | static int finish_lithdr_nvridx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 866 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 867 | grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index); |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 868 | GPR_ASSERT(md != NULL); /* handled in string parsing */ |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 869 | return on_hdr(p, grpc_mdelem_from_metadata_strings(GRPC_MDSTR_REF(md->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 870 | take_string(p, &p->value)), |
| 871 | 0) && |
| 872 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* finish a literal header that is never indexed with an extra value */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 876 | static int finish_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 877 | const uint8_t *cur, const uint8_t *end) { |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 878 | return on_hdr(p, grpc_mdelem_from_metadata_strings(take_string(p, &p->key), |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 879 | take_string(p, &p->value)), |
| 880 | 0) && |
| 881 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* parse a literal header that is never indexed; index < 15 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 885 | static int parse_lithdr_nvridx(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 886 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 887 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 888 | parse_value_string_with_indexed_key, finish_lithdr_nvridx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 889 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 890 | p->next_state = and_then; |
| 891 | p->index = (*cur) & 0xf; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 892 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* parse a literal header that is never indexed; index >= 15 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 896 | static int parse_lithdr_nvridx_x(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 897 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 898 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 899 | parse_string_prefix, parse_value_string_with_indexed_key, |
| 900 | finish_lithdr_nvridx}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 901 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 902 | p->next_state = and_then; |
| 903 | p->index = 0xf; |
| 904 | p->parsing.value = &p->index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 905 | return parse_value0(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* parse a literal header that is never indexed; index == 0 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 909 | static int parse_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 910 | const uint8_t *cur, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 911 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 912 | parse_key_string, parse_string_prefix, |
| 913 | parse_value_string_with_literal_key, finish_lithdr_nvridx_v}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 914 | p->dynamic_table_update_allowed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 915 | p->next_state = and_then; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 916 | return parse_string_prefix(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* finish parsing a max table size change */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 920 | static int finish_max_tbl_size(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 921 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 922 | gpr_log(GPR_INFO, "MAX TABLE SIZE: %d", p->index); |
Craig Tiller | 3c53bb2 | 2015-11-10 14:24:36 +0000 | [diff] [blame] | 923 | return grpc_chttp2_hptbl_set_current_table_size(&p->table, p->index) && |
| 924 | parse_begin(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* parse a max table size change, max size < 15 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 928 | static int parse_max_tbl_size(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 929 | const uint8_t *end) { |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 930 | if (p->dynamic_table_update_allowed == 0) { |
| 931 | return 0; |
| 932 | } |
| 933 | p->dynamic_table_update_allowed--; |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 934 | p->index = (*cur) & 0x1f; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 935 | return finish_max_tbl_size(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* parse a max table size change, max size >= 15 */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 939 | static int parse_max_tbl_size_x(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 940 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 941 | static const grpc_chttp2_hpack_parser_state and_then[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 942 | finish_max_tbl_size}; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 943 | if (p->dynamic_table_update_allowed == 0) { |
| 944 | return 0; |
| 945 | } |
| 946 | p->dynamic_table_update_allowed--; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 947 | p->next_state = and_then; |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 948 | p->index = 0x1f; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 949 | p->parsing.value = &p->index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 950 | return parse_value0(p, cur + 1, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* a parse error: jam the parse state into parse_error, and return error */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 954 | static int parse_error(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 955 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 956 | p->state = parse_error; |
| 957 | return 0; |
| 958 | } |
| 959 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 960 | static int parse_illegal_op(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 961 | const uint8_t *end) { |
Craig Tiller | 027a74c | 2015-11-10 08:37:46 +0000 | [diff] [blame] | 962 | GPR_ASSERT(cur != end); |
| 963 | gpr_log(GPR_DEBUG, "Illegal hpack op code %d", *cur); |
| 964 | return parse_error(p, cur, end); |
| 965 | } |
| 966 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 967 | /* parse the 1st byte of a varint into p->parsing.value |
| 968 | no overflow is possible */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 969 | static int parse_value0(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 970 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 971 | if (cur == end) { |
| 972 | p->state = parse_value0; |
| 973 | return 1; |
| 974 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 975 | |
| 976 | *p->parsing.value += (*cur) & 0x7f; |
| 977 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 978 | if ((*cur) & 0x80) { |
| 979 | return parse_value1(p, cur + 1, end); |
| 980 | } else { |
| 981 | return parse_next(p, cur + 1, end); |
| 982 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* parse the 2nd byte of a varint into p->parsing.value |
| 986 | no overflow is possible */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 987 | static int parse_value1(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 988 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 989 | if (cur == end) { |
| 990 | p->state = parse_value1; |
| 991 | return 1; |
| 992 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 993 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 994 | *p->parsing.value += (((uint32_t)*cur) & 0x7f) << 7; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 995 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 996 | if ((*cur) & 0x80) { |
| 997 | return parse_value2(p, cur + 1, end); |
| 998 | } else { |
| 999 | return parse_next(p, cur + 1, end); |
| 1000 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /* parse the 3rd byte of a varint into p->parsing.value |
| 1004 | no overflow is possible */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1005 | static int parse_value2(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1006 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1007 | if (cur == end) { |
| 1008 | p->state = parse_value2; |
| 1009 | return 1; |
| 1010 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1011 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1012 | *p->parsing.value += (((uint32_t)*cur) & 0x7f) << 14; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1013 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1014 | if ((*cur) & 0x80) { |
| 1015 | return parse_value3(p, cur + 1, end); |
| 1016 | } else { |
| 1017 | return parse_next(p, cur + 1, end); |
| 1018 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* parse the 4th byte of a varint into p->parsing.value |
| 1022 | no overflow is possible */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1023 | static int parse_value3(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1024 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1025 | if (cur == end) { |
| 1026 | p->state = parse_value3; |
| 1027 | return 1; |
| 1028 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1029 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1030 | *p->parsing.value += (((uint32_t)*cur) & 0x7f) << 21; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1031 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1032 | if ((*cur) & 0x80) { |
| 1033 | return parse_value4(p, cur + 1, end); |
| 1034 | } else { |
| 1035 | return parse_next(p, cur + 1, end); |
| 1036 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /* parse the 5th byte of a varint into p->parsing.value |
| 1040 | depending on the byte, we may overflow, and care must be taken */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1041 | static int parse_value4(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1042 | const uint8_t *end) { |
| 1043 | uint8_t c; |
| 1044 | uint32_t cur_value; |
| 1045 | uint32_t add_value; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1046 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1047 | if (cur == end) { |
| 1048 | p->state = parse_value4; |
| 1049 | return 1; |
| 1050 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1051 | |
| 1052 | c = (*cur) & 0x7f; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1053 | if (c > 0xf) { |
| 1054 | goto error; |
| 1055 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1056 | |
| 1057 | cur_value = *p->parsing.value; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1058 | add_value = ((uint32_t)c) << 28; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1059 | if (add_value > 0xffffffffu - cur_value) { |
| 1060 | goto error; |
| 1061 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1062 | |
| 1063 | *p->parsing.value = cur_value + add_value; |
| 1064 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1065 | if ((*cur) & 0x80) { |
| 1066 | return parse_value5up(p, cur + 1, end); |
| 1067 | } else { |
| 1068 | return parse_next(p, cur + 1, end); |
| 1069 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1070 | |
| 1071 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1072 | gpr_log(GPR_ERROR, |
| 1073 | "integer overflow in hpack integer decoding: have 0x%08x, " |
Craig Tiller | d391b56 | 2015-12-08 08:53:09 -0800 | [diff] [blame] | 1074 | "got byte 0x%02x on byte 5", |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1075 | *p->parsing.value, *cur); |
| 1076 | return parse_error(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* parse any trailing bytes in a varint: it's possible to append an arbitrary |
| 1080 | number of 0x80's and not affect the value - a zero will terminate - and |
| 1081 | anything else will overflow */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1082 | static int parse_value5up(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1083 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1084 | while (cur != end && *cur == 0x80) { |
| 1085 | ++cur; |
| 1086 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1087 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1088 | if (cur == end) { |
| 1089 | p->state = parse_value5up; |
| 1090 | return 1; |
| 1091 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1092 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1093 | if (*cur == 0) { |
| 1094 | return parse_next(p, cur + 1, end); |
| 1095 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1096 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1097 | gpr_log(GPR_ERROR, |
| 1098 | "integer overflow in hpack integer decoding: have 0x%08x, " |
Craig Tiller | d391b56 | 2015-12-08 08:53:09 -0800 | [diff] [blame] | 1099 | "got byte 0x%02x sometime after byte 5", |
| 1100 | *p->parsing.value, *cur); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1101 | return parse_error(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* parse a string prefix */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1105 | static int parse_string_prefix(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1106 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1107 | if (cur == end) { |
| 1108 | p->state = parse_string_prefix; |
| 1109 | return 1; |
| 1110 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1111 | |
| 1112 | p->strlen = (*cur) & 0x7f; |
| 1113 | p->huff = (*cur) >> 7; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1114 | if (p->strlen == 0x7f) { |
| 1115 | p->parsing.value = &p->strlen; |
| 1116 | return parse_value0(p, cur + 1, end); |
| 1117 | } else { |
| 1118 | return parse_next(p, cur + 1, end); |
| 1119 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* append some bytes to a string */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1123 | static void append_bytes(grpc_chttp2_hpack_parser_string *str, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1124 | const uint8_t *data, size_t length) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1125 | if (length + str->length > str->capacity) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1126 | GPR_ASSERT(str->length + length <= UINT32_MAX); |
| 1127 | str->capacity = (uint32_t)(str->length + length); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1128 | str->str = gpr_realloc(str->str, str->capacity); |
| 1129 | } |
| 1130 | memcpy(str->str + str->length, data, length); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1131 | GPR_ASSERT(length <= UINT32_MAX - str->length); |
| 1132 | str->length += (uint32_t)length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1135 | static int append_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1136 | const uint8_t *end) { |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1137 | grpc_chttp2_hpack_parser_string *str = p->parsing.str; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1138 | uint32_t bits; |
| 1139 | uint8_t decoded[3]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1140 | switch ((binary_state)p->binary) { |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1141 | case NOT_BINARY: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1142 | append_bytes(str, cur, (size_t)(end - cur)); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1143 | return 1; |
| 1144 | b64_byte0: |
| 1145 | case B64_BYTE0: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1146 | if (cur == end) { |
| 1147 | p->binary = B64_BYTE0; |
| 1148 | return 1; |
| 1149 | } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1150 | bits = inverse_base64[*cur]; |
| 1151 | ++cur; |
| 1152 | if (bits == 255) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1153 | return 0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1154 | else if (bits == 64) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1155 | goto b64_byte0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1156 | p->base64_buffer = bits << 18; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1157 | /* fallthrough */ |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1158 | b64_byte1: |
| 1159 | case B64_BYTE1: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1160 | if (cur == end) { |
| 1161 | p->binary = B64_BYTE1; |
| 1162 | return 1; |
| 1163 | } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1164 | bits = inverse_base64[*cur]; |
| 1165 | ++cur; |
| 1166 | if (bits == 255) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1167 | return 0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1168 | else if (bits == 64) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1169 | goto b64_byte1; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1170 | p->base64_buffer |= bits << 12; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1171 | /* fallthrough */ |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1172 | b64_byte2: |
| 1173 | case B64_BYTE2: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1174 | if (cur == end) { |
| 1175 | p->binary = B64_BYTE2; |
| 1176 | return 1; |
| 1177 | } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1178 | bits = inverse_base64[*cur]; |
| 1179 | ++cur; |
| 1180 | if (bits == 255) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1181 | return 0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1182 | else if (bits == 64) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1183 | goto b64_byte2; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1184 | p->base64_buffer |= bits << 6; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1185 | /* fallthrough */ |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1186 | b64_byte3: |
| 1187 | case B64_BYTE3: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1188 | if (cur == end) { |
| 1189 | p->binary = B64_BYTE3; |
| 1190 | return 1; |
| 1191 | } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1192 | bits = inverse_base64[*cur]; |
| 1193 | ++cur; |
| 1194 | if (bits == 255) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1195 | return 0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1196 | else if (bits == 64) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1197 | goto b64_byte3; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1198 | p->base64_buffer |= bits; |
| 1199 | bits = p->base64_buffer; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1200 | decoded[0] = (uint8_t)(bits >> 16); |
| 1201 | decoded[1] = (uint8_t)(bits >> 8); |
| 1202 | decoded[2] = (uint8_t)(bits); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1203 | append_bytes(str, decoded, 3); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1204 | goto b64_byte0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1205 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 1206 | GPR_UNREACHABLE_CODE(return 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1209 | /* append a null terminator to a string */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1210 | static int finish_str(grpc_chttp2_hpack_parser *p) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1211 | uint8_t terminator = 0; |
| 1212 | uint8_t decoded[2]; |
| 1213 | uint32_t bits; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1214 | grpc_chttp2_hpack_parser_string *str = p->parsing.str; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1215 | switch ((binary_state)p->binary) { |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1216 | case NOT_BINARY: |
| 1217 | break; |
| 1218 | case B64_BYTE0: |
| 1219 | break; |
| 1220 | case B64_BYTE1: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1221 | gpr_log(GPR_ERROR, "illegal base64 encoding"); |
| 1222 | return 0; /* illegal encoding */ |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1223 | case B64_BYTE2: |
| 1224 | bits = p->base64_buffer; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1225 | if (bits & 0xffff) { |
| 1226 | gpr_log(GPR_ERROR, "trailing bits in base64 encoding: 0x%04x", |
| 1227 | bits & 0xffff); |
| 1228 | return 0; |
| 1229 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1230 | decoded[0] = (uint8_t)(bits >> 16); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1231 | append_bytes(str, decoded, 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1232 | break; |
| 1233 | case B64_BYTE3: |
| 1234 | bits = p->base64_buffer; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1235 | if (bits & 0xff) { |
| 1236 | gpr_log(GPR_ERROR, "trailing bits in base64 encoding: 0x%02x", |
| 1237 | bits & 0xff); |
| 1238 | return 0; |
| 1239 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1240 | decoded[0] = (uint8_t)(bits >> 16); |
| 1241 | decoded[1] = (uint8_t)(bits >> 8); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1242 | append_bytes(str, decoded, 2); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1243 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1244 | } |
| 1245 | append_bytes(str, &terminator, 1); |
| 1246 | p->parsing.str->length--; /* don't actually count the null terminator */ |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1247 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* decode a nibble from a huffman encoded stream */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1251 | static int huff_nibble(grpc_chttp2_hpack_parser *p, uint8_t nibble) { |
| 1252 | int16_t emit = emit_sub_tbl[16 * emit_tbl[p->huff_state] + nibble]; |
| 1253 | int16_t next = next_sub_tbl[16 * next_tbl[p->huff_state] + nibble]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1254 | if (emit != -1) { |
| 1255 | if (emit >= 0 && emit < 256) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1256 | uint8_t c = (uint8_t)emit; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1257 | if (!append_string(p, &c, (&c) + 1)) return 0; |
| 1258 | } else { |
| 1259 | assert(emit == 256); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1260 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1261 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1262 | p->huff_state = next; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1263 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /* decode full bytes from a huffman encoded stream */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1267 | static int add_huff_bytes(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1268 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1269 | for (; cur != end; ++cur) { |
| 1270 | if (!huff_nibble(p, *cur >> 4) || !huff_nibble(p, *cur & 0xf)) return 0; |
| 1271 | } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1272 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* decode some string bytes based on the current decoding mode |
| 1276 | (huffman or not) */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1277 | static int add_str_bytes(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1278 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1279 | if (p->huff) { |
| 1280 | return add_huff_bytes(p, cur, end); |
| 1281 | } else { |
| 1282 | return append_string(p, cur, end); |
| 1283 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* parse a string - tries to do large chunks at a time */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1287 | static int parse_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1288 | const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1289 | size_t remaining = p->strlen - p->strgot; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1290 | size_t given = (size_t)(end - cur); |
| 1291 | if (remaining <= given) { |
| 1292 | return add_str_bytes(p, cur, cur + remaining) && finish_str(p) && |
| 1293 | parse_next(p, cur + remaining, end); |
| 1294 | } else { |
| 1295 | if (!add_str_bytes(p, cur, cur + given)) return 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1296 | GPR_ASSERT(given <= UINT32_MAX - p->strgot); |
| 1297 | p->strgot += (uint32_t)given; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1298 | p->state = parse_string; |
| 1299 | return 1; |
| 1300 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | /* begin parsing a string - performs setup, calls parse_string */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1304 | static int begin_parse_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1305 | const uint8_t *end, uint8_t binary, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1306 | grpc_chttp2_hpack_parser_string *str) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1307 | p->strgot = 0; |
| 1308 | str->length = 0; |
| 1309 | p->parsing.str = str; |
| 1310 | p->huff_state = 0; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1311 | p->binary = binary; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1312 | return parse_string(p, cur, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* parse the key string */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1316 | static int parse_key_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1317 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1318 | return begin_parse_string(p, cur, end, NOT_BINARY, &p->key); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* check if a key represents a binary header or not */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1322 | typedef enum { BINARY_HEADER, PLAINTEXT_HEADER, ERROR_HEADER } is_binary_header; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1323 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1324 | static is_binary_header is_binary_literal_header(grpc_chttp2_hpack_parser *p) { |
| 1325 | return grpc_is_binary_header(p->key.str, p->key.length) ? BINARY_HEADER |
| 1326 | : PLAINTEXT_HEADER; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1329 | static is_binary_header is_binary_indexed_header(grpc_chttp2_hpack_parser *p) { |
| 1330 | grpc_mdelem *elem = grpc_chttp2_hptbl_lookup(&p->table, p->index); |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 1331 | if (!elem) { |
| 1332 | gpr_log(GPR_ERROR, "Invalid HPACK index received: %d", p->index); |
| 1333 | return ERROR_HEADER; |
| 1334 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1335 | return grpc_is_binary_header( |
| 1336 | (const char *)GPR_SLICE_START_PTR(elem->key->slice), |
| 1337 | GPR_SLICE_LENGTH(elem->key->slice)) |
| 1338 | ? BINARY_HEADER |
| 1339 | : PLAINTEXT_HEADER; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* parse the value string */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1343 | static int parse_value_string(grpc_chttp2_hpack_parser *p, const uint8_t *cur, |
| 1344 | const uint8_t *end, is_binary_header type) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1345 | switch (type) { |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1346 | case BINARY_HEADER: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1347 | return begin_parse_string(p, cur, end, B64_BYTE0, &p->value); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1348 | case PLAINTEXT_HEADER: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1349 | return begin_parse_string(p, cur, end, NOT_BINARY, &p->value); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1350 | case ERROR_HEADER: |
| 1351 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1352 | } |
vpai | 61cba0b | 2014-12-15 14:40:45 -0800 | [diff] [blame] | 1353 | /* Add code to prevent return without value error */ |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 1354 | GPR_UNREACHABLE_CODE(return 0); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1357 | static int parse_value_string_with_indexed_key(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1358 | const uint8_t *cur, |
| 1359 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1360 | return parse_value_string(p, cur, end, is_binary_indexed_header(p)); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1363 | static int parse_value_string_with_literal_key(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1364 | const uint8_t *cur, |
| 1365 | const uint8_t *end) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1366 | return parse_value_string(p, cur, end, is_binary_literal_header(p)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* PUBLIC INTERFACE */ |
| 1370 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1371 | static void on_header_not_set(void *user_data, grpc_mdelem *md) { |
Craig Tiller | 535701c | 2015-12-07 10:09:44 -0800 | [diff] [blame] | 1372 | GPR_UNREACHABLE_CODE(return ); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1375 | void grpc_chttp2_hpack_parser_init(grpc_chttp2_hpack_parser *p) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1376 | p->on_header = on_header_not_set; |
| 1377 | p->on_header_user_data = NULL; |
| 1378 | p->state = parse_begin; |
| 1379 | p->key.str = NULL; |
| 1380 | p->key.capacity = 0; |
| 1381 | p->key.length = 0; |
| 1382 | p->value.str = NULL; |
| 1383 | p->value.capacity = 0; |
| 1384 | p->value.length = 0; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 1385 | p->dynamic_table_update_allowed = 2; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1386 | grpc_chttp2_hptbl_init(&p->table); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1389 | void grpc_chttp2_hpack_parser_set_has_priority(grpc_chttp2_hpack_parser *p) { |
Craig Tiller | d1e5fe3 | 2015-05-21 14:12:23 -0700 | [diff] [blame] | 1390 | p->after_prioritization = p->state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1391 | p->state = parse_stream_dep0; |
| 1392 | } |
| 1393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1394 | void grpc_chttp2_hpack_parser_destroy(grpc_chttp2_hpack_parser *p) { |
| 1395 | grpc_chttp2_hptbl_destroy(&p->table); |
| 1396 | gpr_free(p->key.str); |
| 1397 | gpr_free(p->value.str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1400 | int grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser *p, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1401 | const uint8_t *beg, const uint8_t *end) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1402 | /* TODO(ctiller): limit the distance of end from beg, and perform multiple |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1403 | steps in the event of a large chunk of data to limit |
| 1404 | stack space usage when no tail call optimization is |
| 1405 | available */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1406 | return p->state(p, beg, end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1409 | grpc_chttp2_parse_error grpc_chttp2_header_parser_parse( |
| 1410 | grpc_exec_ctx *exec_ctx, void *hpack_parser, |
| 1411 | grpc_chttp2_transport_parsing *transport_parsing, |
| 1412 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1413 | grpc_chttp2_hpack_parser *parser = hpack_parser; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1414 | GPR_TIMER_BEGIN("grpc_chttp2_hpack_parser_parse", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1415 | if (!grpc_chttp2_hpack_parser_parse(parser, GPR_SLICE_START_PTR(slice), |
| 1416 | GPR_SLICE_END_PTR(slice))) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1417 | GPR_TIMER_END("grpc_chttp2_hpack_parser_parse", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1418 | return GRPC_CHTTP2_CONNECTION_ERROR; |
| 1419 | } |
| 1420 | if (is_last) { |
| 1421 | if (parser->is_boundary && parser->state != parse_begin) { |
| 1422 | gpr_log(GPR_ERROR, |
| 1423 | "end of header frame not aligned with a hpack record boundary"); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1424 | GPR_TIMER_END("grpc_chttp2_hpack_parser_parse", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1425 | return GRPC_CHTTP2_CONNECTION_ERROR; |
| 1426 | } |
Craig Tiller | c321814 | 2015-12-08 08:30:39 -0800 | [diff] [blame] | 1427 | /* need to check for null stream: this can occur if we receive an invalid |
| 1428 | stream id on a header */ |
| 1429 | if (stream_parsing != NULL) { |
| 1430 | if (parser->is_boundary) { |
| 1431 | stream_parsing |
| 1432 | ->got_metadata_on_parse[stream_parsing->header_frames_received] = 1; |
| 1433 | stream_parsing->header_frames_received++; |
| 1434 | grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, |
| 1435 | stream_parsing); |
| 1436 | } |
| 1437 | if (parser->is_eof) { |
| 1438 | stream_parsing->received_close = 1; |
| 1439 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1440 | } |
| 1441 | parser->on_header = on_header_not_set; |
| 1442 | parser->on_header_user_data = NULL; |
| 1443 | parser->is_boundary = 0xde; |
| 1444 | parser->is_eof = 0xde; |
Craig Tiller | 7b01b75 | 2015-12-02 16:52:54 -0800 | [diff] [blame] | 1445 | parser->dynamic_table_update_allowed = 2; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1446 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1447 | GPR_TIMER_END("grpc_chttp2_hpack_parser_parse", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1448 | return GRPC_CHTTP2_PARSE_OK; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1449 | } |