Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | # Copyright 2015, Google Inc. |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are |
| 8 | # met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # * Redistributions in binary form must reproduce the above |
| 13 | # copyright notice, this list of conditions and the following disclaimer |
| 14 | # in the documentation and/or other materials provided with the |
| 15 | # distribution. |
| 16 | # * Neither the name of Google Inc. nor the names of its |
| 17 | # contributors may be used to endorse or promote products derived from |
| 18 | # this software without specific prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | import hashlib |
| 33 | import itertools |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 34 | import collections |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 35 | import os |
| 36 | import sys |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 37 | import subprocess |
| 38 | import re |
Craig Tiller | ddea41e | 2016-11-30 17:05:54 -0800 | [diff] [blame] | 39 | import perfection |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 40 | |
| 41 | # configuration: a list of either strings or 2-tuples of strings |
| 42 | # a single string represents a static grpc_mdstr |
| 43 | # a 2-tuple represents a static grpc_mdelem (and appropriate grpc_mdstrs will |
| 44 | # also be created) |
| 45 | |
| 46 | CONFIG = [ |
Craig Tiller | 47718a6 | 2016-11-14 14:48:42 -0800 | [diff] [blame] | 47 | # metadata strings |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 48 | 'host', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 49 | 'grpc-timeout', |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 50 | 'grpc-internal-encoding-request', |
Makdharma | 178f4bc | 2016-08-24 15:16:53 -0700 | [diff] [blame] | 51 | 'grpc-payload-bin', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 52 | ':path', |
| 53 | 'grpc-encoding', |
| 54 | 'grpc-accept-encoding', |
| 55 | 'user-agent', |
| 56 | ':authority', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 57 | 'grpc-message', |
| 58 | 'grpc-status', |
David Garcia Quintas | 8eec9ec | 2016-08-01 17:05:04 -0700 | [diff] [blame] | 59 | 'grpc-tracing-bin', |
David Garcia Quintas | 98d5f5c | 2016-09-14 14:34:22 -0700 | [diff] [blame] | 60 | 'grpc-stats-bin', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 61 | '', |
Craig Tiller | 47718a6 | 2016-11-14 14:48:42 -0800 | [diff] [blame] | 62 | # channel arg keys |
| 63 | 'grpc.wait_for_ready', |
| 64 | 'grpc.timeout', |
| 65 | 'grpc.max_request_message_bytes', |
| 66 | 'grpc.max_response_message_bytes', |
Craig Tiller | d7f1583 | 2016-11-16 17:12:55 -0800 | [diff] [blame] | 67 | # well known method names |
| 68 | '/grpc.lb.v1.LoadBalancer/BalanceLoad', |
Craig Tiller | 47718a6 | 2016-11-14 14:48:42 -0800 | [diff] [blame] | 69 | # metadata elements |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 70 | ('grpc-status', '0'), |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 71 | ('grpc-status', '1'), |
| 72 | ('grpc-status', '2'), |
| 73 | ('grpc-encoding', 'identity'), |
| 74 | ('grpc-encoding', 'gzip'), |
| 75 | ('grpc-encoding', 'deflate'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 76 | ('te', 'trailers'), |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 77 | ('content-type', 'application/grpc'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 78 | (':method', 'POST'), |
| 79 | (':status', '200'), |
| 80 | (':status', '404'), |
| 81 | (':scheme', 'http'), |
| 82 | (':scheme', 'https'), |
| 83 | (':scheme', 'grpc'), |
| 84 | (':authority', ''), |
| 85 | (':method', 'GET'), |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 86 | (':method', 'PUT'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 87 | (':path', '/'), |
| 88 | (':path', '/index.html'), |
| 89 | (':status', '204'), |
| 90 | (':status', '206'), |
| 91 | (':status', '304'), |
| 92 | (':status', '400'), |
| 93 | (':status', '500'), |
| 94 | ('accept-charset', ''), |
| 95 | ('accept-encoding', ''), |
| 96 | ('accept-encoding', 'gzip, deflate'), |
| 97 | ('accept-language', ''), |
| 98 | ('accept-ranges', ''), |
| 99 | ('accept', ''), |
| 100 | ('access-control-allow-origin', ''), |
| 101 | ('age', ''), |
| 102 | ('allow', ''), |
| 103 | ('authorization', ''), |
| 104 | ('cache-control', ''), |
| 105 | ('content-disposition', ''), |
| 106 | ('content-encoding', ''), |
| 107 | ('content-language', ''), |
| 108 | ('content-length', ''), |
| 109 | ('content-location', ''), |
| 110 | ('content-range', ''), |
| 111 | ('content-type', ''), |
| 112 | ('cookie', ''), |
| 113 | ('date', ''), |
| 114 | ('etag', ''), |
| 115 | ('expect', ''), |
| 116 | ('expires', ''), |
| 117 | ('from', ''), |
| 118 | ('host', ''), |
| 119 | ('if-match', ''), |
| 120 | ('if-modified-since', ''), |
| 121 | ('if-none-match', ''), |
| 122 | ('if-range', ''), |
| 123 | ('if-unmodified-since', ''), |
| 124 | ('last-modified', ''), |
David Garcia Quintas | a3654db | 2016-10-11 15:52:39 -0700 | [diff] [blame] | 125 | ('lb-token', ''), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 126 | ('link', ''), |
| 127 | ('location', ''), |
| 128 | ('max-forwards', ''), |
| 129 | ('proxy-authenticate', ''), |
| 130 | ('proxy-authorization', ''), |
| 131 | ('range', ''), |
| 132 | ('referer', ''), |
| 133 | ('refresh', ''), |
| 134 | ('retry-after', ''), |
| 135 | ('server', ''), |
| 136 | ('set-cookie', ''), |
| 137 | ('strict-transport-security', ''), |
| 138 | ('transfer-encoding', ''), |
| 139 | ('user-agent', ''), |
| 140 | ('vary', ''), |
| 141 | ('via', ''), |
| 142 | ('www-authenticate', ''), |
| 143 | ] |
| 144 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 145 | METADATA_BATCH_CALLOUTS = [ |
| 146 | ':path', |
| 147 | ':method', |
| 148 | ':status', |
| 149 | ':authority', |
| 150 | ':scheme', |
| 151 | 'te', |
| 152 | 'grpc-message', |
| 153 | 'grpc-status', |
| 154 | 'grpc-payload-bin', |
| 155 | 'grpc-encoding', |
| 156 | 'grpc-accept-encoding', |
| 157 | 'content-type', |
| 158 | 'grpc-internal-encoding-request', |
| 159 | 'user-agent', |
| 160 | 'host', |
| 161 | 'lb-token', |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 162 | ] |
| 163 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 164 | COMPRESSION_ALGORITHMS = [ |
| 165 | 'identity', |
| 166 | 'deflate', |
| 167 | 'gzip', |
| 168 | ] |
| 169 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 170 | # utility: mangle the name of a config |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 171 | def mangle(elem, name=None): |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 172 | xl = { |
| 173 | '-': '_', |
| 174 | ':': '', |
| 175 | '/': 'slash', |
| 176 | '.': 'dot', |
| 177 | ',': 'comma', |
| 178 | ' ': '_', |
| 179 | } |
| 180 | def m0(x): |
| 181 | if not x: return 'empty' |
| 182 | r = '' |
| 183 | for c in x: |
| 184 | put = xl.get(c, c.lower()) |
| 185 | if not put: continue |
| 186 | last_is_underscore = r[-1] == '_' if r else True |
| 187 | if last_is_underscore and put == '_': continue |
| 188 | elif len(put) > 1: |
| 189 | if not last_is_underscore: r += '_' |
| 190 | r += put |
| 191 | r += '_' |
| 192 | else: |
| 193 | r += put |
| 194 | if r[-1] == '_': r = r[:-1] |
| 195 | return r |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 196 | def n(default, name=name): |
| 197 | if name is None: return 'grpc_%s_' % default |
| 198 | if name == '': return '' |
| 199 | return 'grpc_%s_' % name |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 200 | if isinstance(elem, tuple): |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 201 | return '%s%s_%s' % (n('mdelem'), m0(elem[0]), m0(elem[1])) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 202 | else: |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 203 | return '%s%s' % (n('mdstr'), m0(elem)) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 204 | |
| 205 | # utility: generate some hash value for a string |
| 206 | def fake_hash(elem): |
| 207 | return hashlib.md5(elem).hexdigest()[0:8] |
| 208 | |
| 209 | # utility: print a big comment block into a set of files |
| 210 | def put_banner(files, banner): |
| 211 | for f in files: |
| 212 | print >>f, '/*' |
| 213 | for line in banner: |
| 214 | print >>f, ' * %s' % line |
| 215 | print >>f, ' */' |
| 216 | print >>f |
| 217 | |
| 218 | # build a list of all the strings we need |
Craig Tiller | d7f1583 | 2016-11-16 17:12:55 -0800 | [diff] [blame] | 219 | all_strs = list() |
| 220 | all_elems = list() |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 221 | static_userdata = {} |
Craig Tiller | 6ec8aa1 | 2016-11-23 09:35:25 -0800 | [diff] [blame] | 222 | # put metadata batch callouts first, to make the check of if a static metadata |
| 223 | # string is a callout trivial |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 224 | for elem in METADATA_BATCH_CALLOUTS: |
| 225 | if elem not in all_strs: |
| 226 | all_strs.append(elem) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 227 | for elem in CONFIG: |
| 228 | if isinstance(elem, tuple): |
Craig Tiller | d7f1583 | 2016-11-16 17:12:55 -0800 | [diff] [blame] | 229 | if elem[0] not in all_strs: |
| 230 | all_strs.append(elem[0]) |
| 231 | if elem[1] not in all_strs: |
| 232 | all_strs.append(elem[1]) |
| 233 | if elem not in all_elems: |
| 234 | all_elems.append(elem) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 235 | else: |
Craig Tiller | d7f1583 | 2016-11-16 17:12:55 -0800 | [diff] [blame] | 236 | if elem not in all_strs: |
| 237 | all_strs.append(elem) |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 238 | compression_elems = [] |
| 239 | for mask in range(1, 1<<len(COMPRESSION_ALGORITHMS)): |
| 240 | val = ','.join(COMPRESSION_ALGORITHMS[alg] |
| 241 | for alg in range(0, len(COMPRESSION_ALGORITHMS)) |
| 242 | if (1 << alg) & mask) |
| 243 | elem = ('grpc-accept-encoding', val) |
Craig Tiller | d7f1583 | 2016-11-16 17:12:55 -0800 | [diff] [blame] | 244 | if val not in all_strs: |
| 245 | all_strs.append(val) |
| 246 | if elem not in all_elems: |
| 247 | all_elems.append(elem) |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 248 | compression_elems.append(elem) |
David Garcia Quintas | 07503b6 | 2016-03-21 11:59:33 -0700 | [diff] [blame] | 249 | static_userdata[elem] = 1 + (mask | 1) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 250 | |
| 251 | # output configuration |
| 252 | args = sys.argv[1:] |
| 253 | H = None |
| 254 | C = None |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 255 | D = None |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 256 | if args: |
| 257 | if 'header' in args: |
| 258 | H = sys.stdout |
| 259 | else: |
| 260 | H = open('/dev/null', 'w') |
| 261 | if 'source' in args: |
| 262 | C = sys.stdout |
| 263 | else: |
| 264 | C = open('/dev/null', 'w') |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 265 | if 'dictionary' in args: |
| 266 | D = sys.stdout |
| 267 | else: |
| 268 | D = open('/dev/null', 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 269 | else: |
| 270 | H = open(os.path.join( |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 271 | os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.h'), 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 272 | C = open(os.path.join( |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 273 | os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.c'), 'w') |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 274 | D = open(os.path.join( |
| 275 | os.path.dirname(sys.argv[0]), '../../../test/core/end2end/fuzzers/hpack.dictionary'), 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 276 | |
| 277 | # copy-paste copyright notice from this file |
| 278 | with open(sys.argv[0]) as my_source: |
| 279 | copyright = [] |
| 280 | for line in my_source: |
| 281 | if line[0] != '#': break |
| 282 | for line in my_source: |
| 283 | if line[0] == '#': |
| 284 | copyright.append(line) |
| 285 | break |
| 286 | for line in my_source: |
| 287 | if line[0] != '#': |
| 288 | break |
| 289 | copyright.append(line) |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 290 | put_banner([H,C], [line[2:].rstrip() for line in copyright]) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 291 | |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 292 | |
| 293 | hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"] |
| 294 | |
| 295 | |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 296 | def esc_dict(line): |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 297 | out = "\"" |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 298 | for c in line: |
| 299 | if 32 <= c < 127: |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 300 | if c != ord('"'): |
| 301 | out += chr(c) |
| 302 | else: |
| 303 | out += "\\\"" |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 304 | else: |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 305 | out += "\\x%02X" % c |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 306 | return out + "\"" |
| 307 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 308 | put_banner([H,C], |
| 309 | """WARNING: Auto-generated code. |
| 310 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 311 | To make changes to this file, change |
| 312 | tools/codegen/core/gen_static_metadata.py, and then re-run it. |
Craig Tiller | 51ae0be | 2015-11-19 07:51:25 -0800 | [diff] [blame] | 313 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 314 | See metadata.h for an explanation of the interface here, and metadata.c for |
| 315 | an explanation of what's going on. |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 316 | """.splitlines()) |
| 317 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 318 | print >>H, '#ifndef GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' |
| 319 | print >>H, '#define GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 320 | print >>H |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 321 | print >>H, '#include "src/core/lib/transport/metadata.h"' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 322 | print >>H |
| 323 | |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 324 | print >>C, '#include "src/core/lib/transport/static_metadata.h"' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 325 | print >>C |
Craig Tiller | 1961436 | 2016-11-17 16:05:27 -0800 | [diff] [blame] | 326 | print >>C, '#include "src/core/lib/slice/slice_internal.h"' |
| 327 | print >>C |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 328 | |
Craig Tiller | dcbe70c | 2016-11-21 14:07:52 -0800 | [diff] [blame] | 329 | str_ofs = 0 |
| 330 | id2strofs = {} |
| 331 | for i, elem in enumerate(all_strs): |
| 332 | id2strofs[i] = str_ofs |
| 333 | str_ofs += len(elem); |
| 334 | def slice_def(i): |
Craig Tiller | 43a5169 | 2016-11-23 08:03:02 -0800 | [diff] [blame] | 335 | return '{.refcount = &grpc_static_metadata_refcounts[%d], .data.refcounted = {g_bytes+%d, %d}}' % (i, id2strofs[i], len(all_strs[i])) |
Craig Tiller | dcbe70c | 2016-11-21 14:07:52 -0800 | [diff] [blame] | 336 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 337 | # validate configuration |
| 338 | for elem in METADATA_BATCH_CALLOUTS: |
| 339 | assert elem in all_strs |
| 340 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 341 | print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs) |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 342 | print >>H, 'extern const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT];' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 343 | for i, elem in enumerate(all_strs): |
| 344 | print >>H, '/* "%s" */' % elem |
Craig Tiller | 5ef31ab | 2016-11-10 16:27:48 -0800 | [diff] [blame] | 345 | print >>H, '#define %s (grpc_static_slice_table[%d])' % (mangle(elem).upper(), i) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 346 | print >>H |
Craig Tiller | dcbe70c | 2016-11-21 14:07:52 -0800 | [diff] [blame] | 347 | print >>C, 'static uint8_t g_bytes[] = {%s};' % (','.join('%d' % ord(c) for c in ''.join(all_strs))) |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 348 | print >>C |
| 349 | print >>C, 'static void static_ref(void *unused) {}' |
| 350 | print >>C, 'static void static_unref(grpc_exec_ctx *exec_ctx, void *unused) {}' |
Craig Tiller | 43a5169 | 2016-11-23 08:03:02 -0800 | [diff] [blame] | 351 | print >>C, 'static const grpc_slice_refcount_vtable static_sub_vtable = {static_ref, static_unref, grpc_slice_default_eq_impl, grpc_slice_default_hash_impl};'; |
| 352 | print >>H, 'extern const grpc_slice_refcount_vtable grpc_static_metadata_vtable;'; |
| 353 | print >>C, 'const grpc_slice_refcount_vtable grpc_static_metadata_vtable = {static_ref, static_unref, grpc_static_slice_eq, grpc_static_slice_hash};'; |
| 354 | print >>C, 'static grpc_slice_refcount static_sub_refcnt = {&static_sub_vtable, &static_sub_refcnt};'; |
| 355 | print >>H, 'extern grpc_slice_refcount grpc_static_metadata_refcounts[GRPC_STATIC_MDSTR_COUNT];' |
| 356 | print >>C, 'grpc_slice_refcount grpc_static_metadata_refcounts[GRPC_STATIC_MDSTR_COUNT] = {' |
Craig Tiller | dcbe70c | 2016-11-21 14:07:52 -0800 | [diff] [blame] | 357 | for i, elem in enumerate(all_strs): |
Craig Tiller | 43a5169 | 2016-11-23 08:03:02 -0800 | [diff] [blame] | 358 | print >>C, ' {&grpc_static_metadata_vtable, &static_sub_refcnt},' |
Craig Tiller | dcbe70c | 2016-11-21 14:07:52 -0800 | [diff] [blame] | 359 | print >>C, '};' |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 360 | print >>C |
Craig Tiller | a82d2a3 | 2016-11-23 08:13:46 -0800 | [diff] [blame] | 361 | print >>H, '#define GRPC_IS_STATIC_METADATA_STRING(slice) \\' |
| 362 | print >>H, ' ((slice).refcount != NULL && (slice).refcount->vtable == &grpc_static_metadata_vtable)' |
Craig Tiller | 43a5169 | 2016-11-23 08:03:02 -0800 | [diff] [blame] | 363 | print >>H |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 364 | print >>C, 'const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = {' |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 365 | for i, elem in enumerate(all_strs): |
Craig Tiller | 7885d1a | 2016-11-17 14:07:45 -0800 | [diff] [blame] | 366 | print >>C, slice_def(i) + ',' |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 367 | print >>C, '};' |
| 368 | print >>C |
Craig Tiller | a82d2a3 | 2016-11-23 08:13:46 -0800 | [diff] [blame] | 369 | print >>H, '#define GRPC_STATIC_METADATA_INDEX(static_slice) \\' |
| 370 | print >>H, ' ((int)((static_slice).refcount - grpc_static_metadata_refcounts))' |
Craig Tiller | 43a5169 | 2016-11-23 08:03:02 -0800 | [diff] [blame] | 371 | print >>H |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 372 | |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 373 | print >>D, '# hpack fuzzing dictionary' |
| 374 | for i, elem in enumerate(all_strs): |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 375 | print >>D, '%s' % (esc_dict([len(elem)] + [ord(c) for c in elem])) |
Craig Tiller | 942568b | 2016-04-18 22:19:00 -0700 | [diff] [blame] | 376 | for i, elem in enumerate(all_elems): |
| 377 | print >>D, '%s' % (esc_dict([0, len(elem[0])] + [ord(c) for c in elem[0]] + |
| 378 | [len(elem[1])] + [ord(c) for c in elem[1]])) |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 379 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 380 | print >>H, '#define GRPC_STATIC_MDELEM_COUNT %d' % len(all_elems) |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 381 | print >>H, 'extern grpc_mdelem_data grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];' |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 382 | print >>H, 'extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT];' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 383 | for i, elem in enumerate(all_elems): |
| 384 | print >>H, '/* "%s": "%s" */' % elem |
Craig Tiller | 9ecadce | 2016-11-18 14:52:25 -0800 | [diff] [blame] | 385 | print >>H, '#define %s (GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[%d], GRPC_MDELEM_STORAGE_STATIC))' % (mangle(elem).upper(), i) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 386 | print >>H |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 387 | print >>C, 'uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] = {' |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 388 | print >>C, ' %s' % ','.join('%d' % static_userdata.get(elem, 0) for elem in all_elems) |
| 389 | print >>C, '};' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 390 | print >>C |
| 391 | |
| 392 | def str_idx(s): |
| 393 | for i, s2 in enumerate(all_strs): |
| 394 | if s == s2: |
| 395 | return i |
| 396 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 397 | def md_idx(m): |
| 398 | for i, m2 in enumerate(all_elems): |
| 399 | if m == m2: |
| 400 | return i |
| 401 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 402 | def offset_trials(mink): |
| 403 | yield 0 |
| 404 | for i in range(1, 100): |
| 405 | for mul in [-1, 1]: |
| 406 | yield mul * i |
| 407 | |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 408 | def perfect_hash(keys, name): |
Craig Tiller | ddea41e | 2016-11-30 17:05:54 -0800 | [diff] [blame] | 409 | p = perfection.hash_parameters(keys) |
| 410 | def f(i, p=p): |
| 411 | i += p.offset |
| 412 | x = i % p.t |
| 413 | y = i / p.t |
| 414 | return x + p.r[y] |
| 415 | return { |
| 416 | 'PHASHRANGE': p.t - 1 + max(p.r), |
| 417 | 'PHASHNKEYS': len(p.slots), |
| 418 | 'pyfunc': f, |
| 419 | 'code': """ |
| 420 | static const int8_t %(name)s_r[] = {%(r)s}; |
| 421 | static uint32_t %(name)s_phash(uint32_t i) { |
| 422 | i %(offset_sign)s= %(offset)d; |
| 423 | uint32_t x = i %% %(t)d; |
| 424 | uint32_t y = i / %(t)d; |
Craig Tiller | 5efdf3c | 2016-12-07 17:30:21 -0800 | [diff] [blame] | 425 | uint32_t h = x; |
| 426 | if (y < GPR_ARRAY_SIZE(%(name)s_r)) { |
| 427 | uint32_t delta = (uint32_t)%(name)s_r[y]; |
| 428 | h += delta; |
| 429 | } |
| 430 | return h; |
Craig Tiller | ddea41e | 2016-11-30 17:05:54 -0800 | [diff] [blame] | 431 | } |
| 432 | """ % { |
| 433 | 'name': name, |
| 434 | 'r': ','.join('%d' % (r if r is not None else 0) for r in p.r), |
| 435 | 't': p.t, |
| 436 | 'offset': abs(p.offset), |
| 437 | 'offset_sign': '+' if p.offset > 0 else '-' |
| 438 | } |
| 439 | } |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 440 | |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 441 | |
| 442 | elem_keys = [str_idx(elem[0]) * len(all_strs) + str_idx(elem[1]) for elem in all_elems] |
| 443 | elem_hash = perfect_hash(elem_keys, "elems") |
| 444 | print >>C, elem_hash['code'] |
| 445 | |
| 446 | keys = [0] * int(elem_hash['PHASHRANGE']) |
Craig Tiller | ddea41e | 2016-11-30 17:05:54 -0800 | [diff] [blame] | 447 | idxs = [255] * int(elem_hash['PHASHNKEYS']) |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 448 | for i, k in enumerate(elem_keys): |
| 449 | h = elem_hash['pyfunc'](k) |
| 450 | assert keys[h] == 0 |
| 451 | keys[h] = k |
| 452 | idxs[h] = i |
| 453 | print >>C, 'static const uint16_t elem_keys[] = {%s};' % ','.join('%d' % k for k in keys) |
| 454 | print >>C, 'static const uint8_t elem_idxs[] = {%s};' % ','.join('%d' % i for i in idxs) |
| 455 | print >>C |
| 456 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 457 | print >>H, 'grpc_mdelem grpc_static_mdelem_for_static_strings(int a, int b);' |
| 458 | print >>C, 'grpc_mdelem grpc_static_mdelem_for_static_strings(int a, int b) {' |
| 459 | print >>C, ' if (a == -1 || b == -1) return GRPC_MDNULL;' |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 460 | print >>C, ' uint32_t k = (uint32_t)(a * %d + b);' % len(all_strs) |
| 461 | print >>C, ' uint32_t h = elems_phash(k);' |
Craig Tiller | 0c3fdad | 2017-01-21 15:16:26 -0800 | [diff] [blame] | 462 | print >>C, ' return h < GPR_ARRAY_SIZE(elem_keys) && elem_keys[h] == k ? GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[elem_idxs[h]], GRPC_MDELEM_STORAGE_STATIC) : GRPC_MDNULL;' |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 463 | print >>C, '}' |
| 464 | print >>C |
| 465 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 466 | print >>C, 'grpc_mdelem_data grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT] = {' |
Craig Tiller | 7885d1a | 2016-11-17 14:07:45 -0800 | [diff] [blame] | 467 | for a, b in all_elems: |
| 468 | print >>C, '{%s,%s},' % (slice_def(str_idx(a)), slice_def(str_idx(b))) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 469 | print >>C, '};' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 470 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 471 | print >>H, 'typedef enum {' |
Craig Tiller | 6ec8aa1 | 2016-11-23 09:35:25 -0800 | [diff] [blame] | 472 | for elem in METADATA_BATCH_CALLOUTS: |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 473 | print >>H, ' %s,' % mangle(elem, 'batch').upper() |
| 474 | print >>H, ' GRPC_BATCH_CALLOUTS_COUNT' |
| 475 | print >>H, '} grpc_metadata_batch_callouts_index;' |
| 476 | print >>H |
| 477 | print >>H, 'typedef union {' |
| 478 | print >>H, ' struct grpc_linked_mdelem *array[GRPC_BATCH_CALLOUTS_COUNT];' |
| 479 | print >>H, ' struct {' |
Craig Tiller | 6ec8aa1 | 2016-11-23 09:35:25 -0800 | [diff] [blame] | 480 | for elem in METADATA_BATCH_CALLOUTS: |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 481 | print >>H, ' struct grpc_linked_mdelem *%s;' % mangle(elem, '').lower() |
| 482 | print >>H, ' } named;' |
| 483 | print >>H, '} grpc_metadata_batch_callouts;' |
| 484 | print >>H |
Craig Tiller | 6ec8aa1 | 2016-11-23 09:35:25 -0800 | [diff] [blame] | 485 | print >>H, '#define GRPC_BATCH_INDEX_OF(slice) \\' |
Craig Tiller | 28d776e | 2016-11-23 10:16:53 -0800 | [diff] [blame] | 486 | print >>H, ' (GRPC_IS_STATIC_METADATA_STRING((slice)) ? (grpc_metadata_batch_callouts_index)GPR_CLAMP(GRPC_STATIC_METADATA_INDEX((slice)), 0, GRPC_BATCH_CALLOUTS_COUNT) : GRPC_BATCH_CALLOUTS_COUNT)' |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 487 | print >>H |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 488 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 489 | print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS)) |
| 490 | print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS)) |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 491 | print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems) |
| 492 | print >>C, '};' |
| 493 | print >>C |
| 494 | |
Craig Tiller | 9ecadce | 2016-11-18 14:52:25 -0800 | [diff] [blame] | 495 | print >>H, '#define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) (GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]], GRPC_MDELEM_STORAGE_STATIC))' |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 496 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 497 | print >>H, '#endif /* GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H */' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 498 | |
| 499 | H.close() |
| 500 | C.close() |