nghttp2/mkstatichdtbl.py

49 lines
1.2 KiB
Python
Raw Normal View History

2013-10-15 18:23:10 +02:00
#!/usr/bin/env python
2014-10-25 11:12:49 +02:00
# -*- coding: utf-8 -*-
# This scripts reads static table entries [1] and generates
# nghttp2_hd_static_entry table. This table is used in
# lib/nghttp2_hd.c.
#
# [1] http://http2.github.io/http2-spec/compression.html
from __future__ import unicode_literals
2013-10-15 18:23:10 +02:00
import re, sys
def hash(s):
h = 0
for c in s:
h = h * 31 + ord(c)
return h & ((1 << 32) - 1)
2014-01-07 15:41:09 +01:00
entries = []
2013-10-15 18:23:10 +02:00
for line in sys.stdin:
2014-06-14 15:24:47 +02:00
m = re.match(r'(\d+)\s+(\S+)\s+(\S.*)?', line)
val = m.group(3).strip() if m.group(3) else ''
2014-01-07 15:41:09 +01:00
entries.append((hash(m.group(2)), int(m.group(1)), m.group(2), val))
entries.sort()
print '/* Sorted by hash(name) and its table index */'
print 'static nghttp2_hd_static_entry static_table[] = {'
2014-01-07 15:41:09 +01:00
for ent in entries:
print 'MAKE_STATIC_ENT({}, "{}", "{}", {}u, {}u),'\
.format(ent[1] - 1, ent[2], ent[3], ent[0], hash(ent[3]))
print '};'
print ''
print '/* Index to the position in static_table */'
print 'const size_t static_table_index[] = {'
for i in range(len(entries)):
for j, ent in enumerate(entries):
if ent[1] - 1 == i:
sys.stdout.write('{: <2d},'.format(j))
break
if (i + 1) % 16 == 0:
sys.stdout.write('\n')
else:
sys.stdout.write(' ')
2014-01-07 15:41:09 +01:00
print '};'