nghttp2/mkhufftbl.py

99 lines
2.5 KiB
Python
Raw Normal View History

2013-10-15 18:23:10 +02:00
#!/usr/bin/env python
import re
import sys
class Node:
def __init__(self, depth):
self.depth = depth
self.children = {}
def to_bin(s):
res = []
for i in range(0, len(s), 8):
x = s[i:i+8]
x += '0'*(8 - len(x))
a = 0
for j in range(8):
a *= 2
a += ord(x[j]) - ord('0')
res.append(a) #chr(a))
return res
nodes = []
def insert(node, sym, binpat, nbits, pidx):
if pidx == len(binpat) - 1:
#assert(binpat[pidx] not in node.children)
mx = (8 - (nbits & 0x7)) & 0x7;
#print "last", bin(binpat[pidx]), mx
for i in range(1 << mx):
node.children[binpat[pidx] + i] = sym
else:
if binpat[pidx] not in node.children:
node.children[binpat[pidx]] = -len(nodes)
nextnode = Node(pidx + 1)
nodes.append(nextnode)
else:
nextnode = nodes[-node.children[binpat[pidx]]]
insert(nextnode, sym, binpat, nbits, pidx + 1)
symbol_tbl = [(None, 0) for i in range(257)]
tables = {}
root = Node(0)
nodes.append(root)
for line in sys.stdin:
2013-12-19 14:54:34 +01:00
m = re.match(r'.*\(\s*(\d+)\) ([|01]+) \[(\d+)\]\s+(\S+).*', line)
2013-10-15 18:23:10 +02:00
if m:
#print m.group(1), m.group(2), m.group(3)
2013-12-19 14:54:34 +01:00
if len(m.group(4)) > 8:
raise Error('Code is more than 4 bytes long')
2013-10-15 18:23:10 +02:00
sym = int(m.group(1))
pat = re.sub(r'\|', '', m.group(2))
nbits = int(m.group(3))
assert(len(pat) == nbits)
binpat = to_bin(pat)
assert(len(binpat) == (nbits+7)/8)
2013-12-19 14:54:34 +01:00
symbol_tbl[sym] = (binpat, nbits, m.group(4))
2013-10-15 18:23:10 +02:00
#print "Inserting", sym
insert(root, sym, binpat, nbits, 0)
print '''\
typedef struct {
2013-12-19 14:54:34 +01:00
uint32_t nbits;
uint32_t code;
2013-10-15 18:23:10 +02:00
} nghttp2_huff_sym;
'''
print '''\
nghttp2_huff_sym huff_sym_table[] = {'''
for i in range(257):
pat = list(symbol_tbl[i][0])
pat += [0]*(4 - len(pat))
print '''\
2013-12-19 14:54:34 +01:00
{{ {}, 0x{}u }}{}\
'''.format(symbol_tbl[i][1], symbol_tbl[i][2], ',' if i < 256 else '')
2013-10-15 18:23:10 +02:00
print '};'
print ''
print '''int16_t huff_decode_table[][256] = {'''
for j in range(len(nodes)):
node = nodes[j]
print '/* {} */'.format(j)
print '{'
for i in range(256):
if i in node.children:
sys.stdout.write('''\
{}{}'''.format(node.children[i], ',' if i < 255 else ''))
else:
sys.stdout.write(''' NGHTTP2_HD_HUFF_NO_ENT,''')
if (i+1)&0x7 == 0:
print ''
sys.stdout.write('}')
if j == len(nodes) - 1:
print ''
else:
print ','
print '};'