tizen 2.4 release
[external/nghttp2.git] / mkstatichdtbl.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # This scripts reads static table entries [1] and generates
5 # nghttp2_hd_static_entry table.  This table is used in
6 # lib/nghttp2_hd.c.
7 #
8 # [1] http://http2.github.io/http2-spec/compression.html
9
10 from __future__ import unicode_literals
11 import re, sys
12
13 def hash(s):
14     h = 0
15     for c in s:
16         h = h * 31 + ord(c)
17     return h & ((1 << 32) - 1)
18
19 entries = []
20 for line in sys.stdin:
21     m = re.match(r'(\d+)\s+(\S+)\s+(\S.*)?', line)
22     val = m.group(3).strip() if m.group(3) else ''
23     entries.append((hash(m.group(2)), int(m.group(1)), m.group(2), val))
24
25 entries.sort()
26
27 print '/* Sorted by hash(name) and its table index */'
28 print 'static nghttp2_hd_static_entry static_table[] = {'
29 for ent in entries:
30     print 'MAKE_STATIC_ENT({}, "{}", "{}", {}u, {}u),'\
31         .format(ent[1] - 1, ent[2], ent[3], ent[0], hash(ent[3]))
32 print '};'
33
34 print ''
35
36 print '/* Index to the position in static_table */'
37 print 'const size_t static_table_index[] = {'
38 for i in range(len(entries)):
39     for j, ent in enumerate(entries):
40         if ent[1] - 1 == i:
41             sys.stdout.write('{: <2d},'.format(j))
42             break
43     if (i + 1) % 16 == 0:
44         sys.stdout.write('\n')
45     else:
46         sys.stdout.write(' ')
47
48 print '};'