diff --git a/mkhufftbl.py b/mkhufftbl.py new file mode 100755 index 00000000..07611230 --- /dev/null +++ b/mkhufftbl.py @@ -0,0 +1,97 @@ +#!/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: + m = re.match(r'.*\(\s*(\d+)\) ([|01]+) \[(\d+)\] .*', line) + if m: + #print m.group(1), m.group(2), m.group(3) + 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) + symbol_tbl[sym] = (binpat, nbits) + #print "Inserting", sym + insert(root, sym, binpat, nbits, 0) + +print '''\ +typedef struct { + size_t nbits; + uint8_t code[4]; +} 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 '''\ + {{ {}, {{ {} }} }}{}\ +'''.format(symbol_tbl[i][1], ', '.join([str(k) for k in pat]), + ',' if i < 256 else '') +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 '};' diff --git a/mkstatichdtbl.py b/mkstatichdtbl.py new file mode 100755 index 00000000..495633d7 --- /dev/null +++ b/mkstatichdtbl.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python +import re, sys + +for line in sys.stdin: + m = re.match(r'(\d+)\s+(\S+)\s+(\S+)?', line) + print '/* {} */ "{}", "{}",'.format(m.group(1), + m.group(2), + m.group(3) if m.group(3) else '')