gen_hpack_tables.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* generates constant tables for hpack.cc */
20 
21 #include <assert.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <grpc/support/log.h>
28 
29 /*
30  * Huffman decoder table generation
31  */
32 
33 #define MAXHUFFSTATES 1024
34 
35 /* represents a set of symbols as an array of booleans indicating inclusion */
36 typedef struct { char included[GRPC_CHTTP2_NUM_HUFFSYMS]; } symset;
37 /* represents a lookup table indexed by a nibble */
38 typedef struct { unsigned values[16]; } nibblelut;
39 
40 #define NOT_SET (~(unsigned)0)
41 
42 /* returns a symset that includes all possible symbols */
43 static symset symset_all(void) {
44  symset x;
45  memset(x.included, 1, sizeof(x.included));
46  return x;
47 }
48 
49 /* returns a symset that includes no symbols */
50 static symset symset_none(void) {
51  symset x;
52  memset(x.included, 0, sizeof(x.included));
53  return x;
54 }
55 
56 /* returns an empty nibblelut */
57 static nibblelut nibblelut_empty(void) {
58  nibblelut x;
59  int i;
60  for (i = 0; i < 16; i++) {
61  x.values[i] = NOT_SET;
62  }
63  return x;
64 }
65 
66 /* counts symbols in a symset - only used for debug builds */
67 #ifndef NDEBUG
68 static int nsyms(symset s) {
69  int i;
70  int c = 0;
71  for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) {
72  c += s.included[i] != 0;
73  }
74  return c;
75 }
76 #endif
77 
78 /* global table of discovered huffman decoding states */
79 static struct {
80  /* the bit offset that this state starts at */
81  unsigned bitofs;
82  /* the set of symbols that this state started with */
84 
85  /* lookup table for the next state */
87  /* lookup table for what to emit */
90 static unsigned nhuffstates = 0;
91 
92 /* given a number of decoded bits and a set of symbols that are live,
93  return the index into the decoder table for this state.
94  set isnew to 1 if this state was previously undiscovered */
95 static unsigned state_index(unsigned bitofs, symset syms, unsigned *isnew) {
96  unsigned i;
97  for (i = 0; i < nhuffstates; i++) {
98  if (huffstates[i].bitofs != bitofs) continue;
99  if (0 != memcmp(huffstates[i].syms.included, syms.included,
101  continue;
102  *isnew = 0;
103  return i;
104  }
106 
107  i = nhuffstates;
108  nhuffstates++;
109 
110  huffstates[i].bitofs = bitofs;
111  huffstates[i].syms = syms;
112  huffstates[i].next = nibblelut_empty();
113  huffstates[i].emit = nibblelut_empty();
114  *isnew = 1;
115  return i;
116 }
117 
118 /* recursively build a decoding table
119 
120  state - the huffman state that we are trying to fill in
121  nibble - the current nibble
122  nibbits - the number of bits in the nibble that have been filled in
123  bitofs - the number of bits of symbol that have been decoded
124  emit - the symbol to emit on this nibble (or -1 if no symbol has been
125  found)
126  syms - the set of symbols that could be matched */
127 static void build_dec_tbl(unsigned state, unsigned nibble, int nibbits,
128  unsigned bitofs, unsigned emit, symset syms) {
129  unsigned i;
130  unsigned bit;
131 
132  /* If we have four bits in the nibble we're looking at, then we can fill in
133  a slot in the lookup tables. */
134  if (nibbits == 4) {
135  unsigned isnew;
136  /* Find the state that we are in: this may be a new state, in which case
137  we recurse to fill it in, or we may have already seen this state, in
138  which case the recursion terminates */
139  unsigned st = state_index(bitofs, syms, &isnew);
141  huffstates[state].next.values[nibble] = st;
142  huffstates[state].emit.values[nibble] = emit;
143  if (isnew) {
144  build_dec_tbl(st, 0, 0, bitofs, NOT_SET, syms);
145  }
146  return;
147  }
148 
149  assert(nsyms(syms));
150 
151  /* A bit can be 0 or 1 */
152  for (bit = 0; bit < 2; bit++) {
153  /* walk over active symbols and see if they have this bit set */
154  symset nextsyms = symset_none();
155  for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) {
156  if (!syms.included[i]) continue; /* disregard inactive symbols */
157  if (((grpc_chttp2_huffsyms[i].bits >>
158  (grpc_chttp2_huffsyms[i].length - bitofs - 1)) &
159  1) == bit) {
160  /* the bit is set, include it in the next recursive set */
161  if (grpc_chttp2_huffsyms[i].length == bitofs + 1) {
162  /* additionally, we've gotten to the end of a symbol - this is a
163  special recursion step: re-activate all the symbols, reset
164  bitofs to zero, and recurse */
165  build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, 0, i,
166  symset_all());
167  /* skip the remainder of this loop */
168  goto next;
169  }
170  nextsyms.included[i] = 1;
171  }
172  }
173  /* recurse down for this bit */
174  build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, bitofs + 1, emit,
175  nextsyms);
176  next:;
177  }
178 }
179 
181 static int nctbl;
182 
183 static int ctbl_idx(nibblelut x) {
184  int i;
185  for (i = 0; i < nctbl; i++) {
186  if (0 == memcmp(&x, ctbl + i, sizeof(nibblelut))) return i;
187  }
188  ctbl[i] = x;
189  nctbl++;
190  return i;
191 }
192 
193 static void dump_ctbl(const char *name) {
194  int i, j;
195  printf("static const gpr_int16 %s[%d*16] = {\n", name, nctbl);
196  for (i = 0; i < nctbl; i++) {
197  for (j = 0; j < 16; j++) {
198  printf("%d,", ctbl[i].values[j]);
199  }
200  printf("\n");
201  }
202  printf("};\n");
203 }
204 
205 static void generate_huff_tables(void) {
206  unsigned i;
207  build_dec_tbl(state_index(0, symset_all(), &i), 0, 0, 0, NOT_SET,
208  symset_all());
209 
210  nctbl = 0;
211  printf("static const gpr_uint8 next_tbl[%d] = {", nhuffstates);
212  for (i = 0; i < nhuffstates; i++) {
213  printf("%d,", ctbl_idx(huffstates[i].next));
214  }
215  printf("};\n");
216  dump_ctbl("next_sub_tbl");
217 
218  nctbl = 0;
219  printf("static const gpr_uint16 emit_tbl[%d] = {", nhuffstates);
220  for (i = 0; i < nhuffstates; i++) {
221  printf("%d,", ctbl_idx(huffstates[i].emit));
222  }
223  printf("};\n");
224  dump_ctbl("emit_sub_tbl");
225 }
226 
228  static const char alphabet[] =
229  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
230  int i;
231 
232  printf(
233  "static const struct { gpr_uint16 bits, gpr_uint8 length } "
234  "base64_syms[64] = {\n");
235  for (i = 0; i < 64; i++) {
236  printf("{0x%x, %d},", grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].bits,
237  grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].length);
238  }
239  printf("};\n");
240 }
241 
242 int main(void) {
245 
246  return 0;
247 }
nibblelut::values
unsigned values[16]
Definition: gen_hpack_tables.cc:38
log.h
nhuffstates
static unsigned nhuffstates
Definition: gen_hpack_tables.cc:90
memset
return memset(p, 0, total)
syms
symset syms
Definition: gen_hpack_tables.cc:83
nibblelut_empty
static nibblelut nibblelut_empty(void)
Definition: gen_hpack_tables.cc:57
string.h
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
huffstates
static struct @718 huffstates[MAXHUFFSTATES]
nctbl
static int nctbl
Definition: gen_hpack_tables.cc:181
setup.name
name
Definition: setup.py:542
alphabet
static const char alphabet[]
Definition: bin_encoder.cc:30
state_index
static unsigned state_index(unsigned bitofs, symset syms, unsigned *isnew)
Definition: gen_hpack_tables.cc:95
GRPC_CHTTP2_NUM_HUFFSYMS
#define GRPC_CHTTP2_NUM_HUFFSYMS
Definition: huffsyms.h:24
build_dec_tbl
static void build_dec_tbl(unsigned state, unsigned nibble, int nibbits, unsigned bitofs, unsigned emit, symset syms)
Definition: gen_hpack_tables.cc:127
dump_ctbl
static void dump_ctbl(const char *name)
Definition: gen_hpack_tables.cc:193
ctbl
static nibblelut ctbl[MAXHUFFSTATES]
Definition: gen_hpack_tables.cc:180
MAXHUFFSTATES
#define MAXHUFFSTATES
Definition: gen_hpack_tables.cc:33
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
nibblelut
Definition: gen_hpack_tables.cc:38
grpc_chttp2_huffsyms
const grpc_chttp2_huffsym grpc_chttp2_huffsyms[GRPC_CHTTP2_NUM_HUFFSYMS]
Definition: huffsyms.cc:26
symset::included
char included[GRPC_CHTTP2_NUM_HUFFSYMS]
Definition: gen_hpack_tables.cc:36
symset_all
static symset symset_all(void)
Definition: gen_hpack_tables.cc:43
emit
nibblelut emit
Definition: gen_hpack_tables.cc:88
generate_base64_huff_encoder_table
static void generate_base64_huff_encoder_table(void)
Definition: gen_hpack_tables.cc:227
symset
Definition: gen_hpack_tables.cc:36
generate_huff_tables
static void generate_huff_tables(void)
Definition: gen_hpack_tables.cc:205
nsyms
static int nsyms(symset s)
Definition: gen_hpack_tables.cc:68
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
main
int main(void)
Definition: gen_hpack_tables.cc:242
bitofs
unsigned bitofs
Definition: gen_hpack_tables.cc:81
huffsyms.h
next
nibblelut next
Definition: gen_hpack_tables.cc:86
symset_none
static symset symset_none(void)
Definition: gen_hpack_tables.cc:50
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
ctbl_idx
static int ctbl_idx(nibblelut x)
Definition: gen_hpack_tables.cc:183
NOT_SET
#define NOT_SET
Definition: gen_hpack_tables.cc:40
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:25