Commit 79bde28a authored by Guillaume Quintard's avatar Guillaume Quintard Committed by Dridi Boukelmoune

[varnishtest] generate the decoding tables

parent a33aa048
......@@ -76,7 +76,8 @@ EXTRA_DIST = $(top_srcdir)/bin/varnishtest/tests/*.vtc \
$(top_srcdir)/bin/varnishtest/tests/README \
$(top_srcdir)/bin/varnishtest/gensequences \
$(top_srcdir)/bin/varnishtest/sequences \
$(top_srcdir)/bin/varnishtest/teken.3
$(top_srcdir)/bin/varnishtest/teken.3 \
huffman_gen.py
teken.c: teken_state.h
......@@ -84,4 +85,8 @@ teken_state.h: $(srcdir)/sequences $(srcdir)/gensequences
awk -f $(srcdir)/gensequences $(srcdir)/sequences \
> $(builddir)/teken_state.h
vtc_h2_dectbl.h: huffman_gen.py $(top_srcdir)/include/tbl/vhp_huffman.h
$(srcdir)/huffman_gen.py $(top_srcdir)/include/tbl/vhp_huffman.h > $@_
mv $@_ $@
CLEANFILES = $(builddir)/teken_state.h
#!/usr/bin/env python
import re
import sys
#HPH(0x30, 0x00000000, 5)
regex = re.compile("^HPH\((.{4}), (.{10}), +(.{1,3})\)")
if len(sys.argv) != 2:
print("{} takes one and only one argument".format(sys.argv[0]))
sys.exit(2)
class sym:
def __init__(self, bigval, bigvall, chr = 0, esc = None):
self.vall = bigvall % 8 if bigvall % 8 else 8
self.val = bigval & ((1 << self.vall) - 1)
self.pfx = (bigval >> self.vall)# & 0xff
self.chr = chr
self.esc = esc
tbls = {}
msl = {} # max sym length
f = open(sys.argv[1])
for l in f:
grp = 1
match = regex.match(l)
if not match:
continue
chr = int(match.group(grp), 16)
grp += 1
val = int(match.group(grp), 16)
grp += 1
vall = int(match.group(grp))
s = sym(val, vall, chr)
if s.pfx not in tbls:
tbls[s.pfx] = {}
if (s.val in tbls[s.pfx]):
assert(tbls[s.pfx][s.val].e)
tbls[s.pfx][s.val] = s
# add the escape entry in the "previous" table
if s.pfx:
pp = s.pfx >> 8
pv = s.pfx & 0xff
if pp not in tbls:
tbls[pp] = {}
tbls[pp][pv] = sym(pv, 8, 0, "&tbl_{:x}".format(s.pfx))
f.close()
# add the EOS case
s = sym(63, 6, 0)
tbls[0xffffff][63] = s
print('''/* NB: This file is machine generated, DO NOT EDIT!
* edit bin/varnishtest/huffman_input instead
*/
struct stbl;
struct ssym {
uint8_t csm; /* bits consumed */
uint8_t chr; /* character */
struct stbl *nxt; /* next table */
};
struct stbl {
int msk;
struct ssym *syms;
};
''')
for pfx in sorted(tbls.keys(), reverse=True):
msl = max([ x.vall for x in tbls[pfx].values() ])
for s in tbls[pfx].values():
s.val = s.val << (msl - s.vall)
tbl = sorted(tbls[pfx].values(), key= lambda x: x.val)
print("\nstatic struct ssym sym_{:x}_array[] = {{".format(pfx))
for s in tbl:
for j in range(2 ** (msl - s.vall)):
print(" {} {{{}, {:3d}, {}}},".format(
" "*13 if j else "/* idx {:3d} */".format(s.val + j),
s.vall, s.chr % 256,
s.esc if s.esc else "NULL"))
print('''}};
static struct stbl tbl_{:x} = {{
{},
sym_{:x}_array
}};'''.format(pfx, msl, pfx))
This diff is collapsed.
This diff is collapsed.
......@@ -61,7 +61,7 @@ huff_decode(char *str, int nm, struct hpk_iter *iter, int ilen)
int l = 0;
uint64_t pack = 0;
unsigned pl = 0; /* pack length*/
struct stbl *tbl = &byte0;
struct stbl *tbl = &tbl_0;
struct ssym *sym;
(void)nm;
......@@ -71,7 +71,7 @@ huff_decode(char *str, int nm, struct hpk_iter *iter, int ilen)
if (ilen == 0) {
if (pl == 0 || (MASK(pack, pl) ==
(unsigned)((1U << pl) - 1U))) {
assert(tbl == &byte0);
assert(tbl == &tbl_0);
return (l);
}
}
......@@ -102,7 +102,7 @@ huff_decode(char *str, int nm, struct hpk_iter *iter, int ilen)
continue;
}
str[l++] = sym->chr;
tbl = &byte0;
tbl = &tbl_0;
}
return (l);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment