gen_hpack_tables.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /* generates constant tables for hpack.cc */
  19. #include <assert.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/ext/transport/chttp2/transport/huffsyms.h"
  25. /*
  26. * Huffman decoder table generation
  27. */
  28. #define MAXHUFFSTATES 1024
  29. /* represents a set of symbols as an array of booleans indicating inclusion */
  30. typedef struct { char included[GRPC_CHTTP2_NUM_HUFFSYMS]; } symset;
  31. /* represents a lookup table indexed by a nibble */
  32. typedef struct { unsigned values[16]; } nibblelut;
  33. #define NOT_SET (~(unsigned)0)
  34. /* returns a symset that includes all possible symbols */
  35. static symset symset_all(void) {
  36. symset x;
  37. memset(x.included, 1, sizeof(x.included));
  38. return x;
  39. }
  40. /* returns a symset that includes no symbols */
  41. static symset symset_none(void) {
  42. symset x;
  43. memset(x.included, 0, sizeof(x.included));
  44. return x;
  45. }
  46. /* returns an empty nibblelut */
  47. static nibblelut nibblelut_empty(void) {
  48. nibblelut x;
  49. int i;
  50. for (i = 0; i < 16; i++) {
  51. x.values[i] = NOT_SET;
  52. }
  53. return x;
  54. }
  55. /* counts symbols in a symset - only used for debug builds */
  56. #ifndef NDEBUG
  57. static int nsyms(symset s) {
  58. int i;
  59. int c = 0;
  60. for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) {
  61. c += s.included[i] != 0;
  62. }
  63. return c;
  64. }
  65. #endif
  66. /* global table of discovered huffman decoding states */
  67. static struct {
  68. /* the bit offset that this state starts at */
  69. unsigned bitofs;
  70. /* the set of symbols that this state started with */
  71. symset syms;
  72. /* lookup table for the next state */
  73. nibblelut next;
  74. /* lookup table for what to emit */
  75. nibblelut emit;
  76. } huffstates[MAXHUFFSTATES];
  77. static unsigned nhuffstates = 0;
  78. /* given a number of decoded bits and a set of symbols that are live,
  79. return the index into the decoder table for this state.
  80. set isnew to 1 if this state was previously undiscovered */
  81. static unsigned state_index(unsigned bitofs, symset syms, unsigned *isnew) {
  82. unsigned i;
  83. for (i = 0; i < nhuffstates; i++) {
  84. if (huffstates[i].bitofs != bitofs) continue;
  85. if (0 != memcmp(huffstates[i].syms.included, syms.included,
  86. GRPC_CHTTP2_NUM_HUFFSYMS))
  87. continue;
  88. *isnew = 0;
  89. return i;
  90. }
  91. GPR_ASSERT(nhuffstates != MAXHUFFSTATES);
  92. i = nhuffstates;
  93. nhuffstates++;
  94. huffstates[i].bitofs = bitofs;
  95. huffstates[i].syms = syms;
  96. huffstates[i].next = nibblelut_empty();
  97. huffstates[i].emit = nibblelut_empty();
  98. *isnew = 1;
  99. return i;
  100. }
  101. /* recursively build a decoding table
  102. state - the huffman state that we are trying to fill in
  103. nibble - the current nibble
  104. nibbits - the number of bits in the nibble that have been filled in
  105. bitofs - the number of bits of symbol that have been decoded
  106. emit - the symbol to emit on this nibble (or -1 if no symbol has been
  107. found)
  108. syms - the set of symbols that could be matched */
  109. static void build_dec_tbl(unsigned state, unsigned nibble, int nibbits,
  110. unsigned bitofs, unsigned emit, symset syms) {
  111. unsigned i;
  112. unsigned bit;
  113. /* If we have four bits in the nibble we're looking at, then we can fill in
  114. a slot in the lookup tables. */
  115. if (nibbits == 4) {
  116. unsigned isnew;
  117. /* Find the state that we are in: this may be a new state, in which case
  118. we recurse to fill it in, or we may have already seen this state, in
  119. which case the recursion terminates */
  120. unsigned st = state_index(bitofs, syms, &isnew);
  121. GPR_ASSERT(huffstates[state].next.values[nibble] == NOT_SET);
  122. huffstates[state].next.values[nibble] = st;
  123. huffstates[state].emit.values[nibble] = emit;
  124. if (isnew) {
  125. build_dec_tbl(st, 0, 0, bitofs, NOT_SET, syms);
  126. }
  127. return;
  128. }
  129. assert(nsyms(syms));
  130. /* A bit can be 0 or 1 */
  131. for (bit = 0; bit < 2; bit++) {
  132. /* walk over active symbols and see if they have this bit set */
  133. symset nextsyms = symset_none();
  134. for (i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) {
  135. if (!syms.included[i]) continue; /* disregard inactive symbols */
  136. if (((grpc_chttp2_huffsyms[i].bits >>
  137. (grpc_chttp2_huffsyms[i].length - bitofs - 1)) &
  138. 1) == bit) {
  139. /* the bit is set, include it in the next recursive set */
  140. if (grpc_chttp2_huffsyms[i].length == bitofs + 1) {
  141. /* additionally, we've gotten to the end of a symbol - this is a
  142. special recursion step: re-activate all the symbols, reset
  143. bitofs to zero, and recurse */
  144. build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, 0, i,
  145. symset_all());
  146. /* skip the remainder of this loop */
  147. goto next;
  148. }
  149. nextsyms.included[i] = 1;
  150. }
  151. }
  152. /* recurse down for this bit */
  153. build_dec_tbl(state, (nibble << 1) | bit, nibbits + 1, bitofs + 1, emit,
  154. nextsyms);
  155. next:;
  156. }
  157. }
  158. static nibblelut ctbl[MAXHUFFSTATES];
  159. static int nctbl;
  160. static int ctbl_idx(nibblelut x) {
  161. int i;
  162. for (i = 0; i < nctbl; i++) {
  163. if (0 == memcmp(&x, ctbl + i, sizeof(nibblelut))) return i;
  164. }
  165. ctbl[i] = x;
  166. nctbl++;
  167. return i;
  168. }
  169. static void dump_ctbl(const char *name) {
  170. int i, j;
  171. printf("static const gpr_int16 %s[%d*16] = {\n", name, nctbl);
  172. for (i = 0; i < nctbl; i++) {
  173. for (j = 0; j < 16; j++) {
  174. printf("%d,", ctbl[i].values[j]);
  175. }
  176. printf("\n");
  177. }
  178. printf("};\n");
  179. }
  180. static void generate_huff_tables(void) {
  181. unsigned i;
  182. build_dec_tbl(state_index(0, symset_all(), &i), 0, 0, 0, NOT_SET,
  183. symset_all());
  184. nctbl = 0;
  185. printf("static const gpr_uint8 next_tbl[%d] = {", nhuffstates);
  186. for (i = 0; i < nhuffstates; i++) {
  187. printf("%d,", ctbl_idx(huffstates[i].next));
  188. }
  189. printf("};\n");
  190. dump_ctbl("next_sub_tbl");
  191. nctbl = 0;
  192. printf("static const gpr_uint16 emit_tbl[%d] = {", nhuffstates);
  193. for (i = 0; i < nhuffstates; i++) {
  194. printf("%d,", ctbl_idx(huffstates[i].emit));
  195. }
  196. printf("};\n");
  197. dump_ctbl("emit_sub_tbl");
  198. }
  199. static void generate_base64_huff_encoder_table(void) {
  200. static const char alphabet[] =
  201. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  202. int i;
  203. printf(
  204. "static const struct { gpr_uint16 bits, gpr_uint8 length } "
  205. "base64_syms[64] = {\n");
  206. for (i = 0; i < 64; i++) {
  207. printf("{0x%x, %d},", grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].bits,
  208. grpc_chttp2_huffsyms[(unsigned char)alphabet[i]].length);
  209. }
  210. printf("};\n");
  211. }
  212. int main(void) {
  213. generate_huff_tables();
  214. generate_base64_huff_encoder_table();
  215. return 0;
  216. }