LCOV - code coverage report
Current view: top level - core/transport/chttp2 - hpack_parser.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 403 429 93.9 %
Date: 2015-12-10 22:15:08 Functions: 58 59 98.3 %

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

Generated by: LCOV version 1.11