mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
Decode flate code length code lengths.
This commit is contained in:
1 parent
b78807d60a
commit
20f37a6dab
8 files changed
+95
-15
No files matched your search
@@ -1105,8 +1105,11 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
|||||||
if id1 := n.ID1(); id1.Key() == t.KeyThis {
|
if id1 := n.ID1(); id1.Key() == t.KeyThis {
|
||||||
g.writes("self->private_impl")
|
g.writes("self->private_impl")
|
||||||
} else {
|
} else {
|
||||||
// TODO: don't assume that the vPrefix is necessary.
|
if n.GlobalIdent() {
|
||||||
g.writes(vPrefix)
|
g.writes(g.pkgPrefix)
|
||||||
|
} else {
|
||||||
|
g.writes(vPrefix)
|
||||||
|
}
|
||||||
g.writes(id1.String(g.tm))
|
g.writes(id1.String(g.tm))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -47,11 +47,12 @@ const (
|
|||||||
func main() {
|
func main() {
|
||||||
generate.Main(func(pkgName string, tm *t.Map, c *check.Checker, files []*a.File) ([]byte, error) {
|
generate.Main(func(pkgName string, tm *t.Map, c *check.Checker, files []*a.File) ([]byte, error) {
|
||||||
g := &gen{
|
g := &gen{
|
||||||
pkgName: pkgName,
|
pkgPrefix: "puffs_" + pkgName + "_",
|
||||||
PKGNAME: strings.ToUpper(pkgName),
|
pkgName: pkgName,
|
||||||
tm: tm,
|
PKGNAME: strings.ToUpper(pkgName),
|
||||||
checker: c,
|
tm: tm,
|
||||||
files: files,
|
checker: c,
|
||||||
|
files: files,
|
||||||
}
|
}
|
||||||
if err := g.generate(); err != nil {
|
if err := g.generate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -140,6 +141,7 @@ type status struct {
|
|||||||
|
|
||||||
type gen struct {
|
type gen struct {
|
||||||
buffer bytes.Buffer
|
buffer bytes.Buffer
|
||||||
|
pkgPrefix string
|
||||||
pkgName string
|
pkgName string
|
||||||
PKGNAME string
|
PKGNAME string
|
||||||
tm *t.Map
|
tm *t.Map
|
||||||
|
|||||||
+28
-2
@@ -134,9 +134,9 @@ typedef struct {
|
|||||||
|
|
||||||
uint32_t f_bits;
|
uint32_t f_bits;
|
||||||
uint32_t f_n_bits;
|
uint32_t f_n_bits;
|
||||||
|
uint8_t f_code_length_code_lengths[19];
|
||||||
uint32_t f_wip0;
|
uint32_t f_wip0;
|
||||||
uint32_t f_wip1;
|
uint32_t f_wip1;
|
||||||
uint32_t f_wip2;
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
@@ -154,6 +154,7 @@ typedef struct {
|
|||||||
uint32_t v_hlit;
|
uint32_t v_hlit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_hdist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_hclen;
|
||||||
|
uint32_t v_i;
|
||||||
} c_decode_dynamic[1];
|
} c_decode_dynamic[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_decoder;
|
} puffs_flate_decoder;
|
||||||
@@ -641,6 +642,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
uint32_t v_hlit;
|
uint32_t v_hlit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_hdist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_hclen;
|
||||||
|
uint32_t v_i;
|
||||||
|
|
||||||
uint8_t* b_rptr_src = NULL;
|
uint8_t* b_rptr_src = NULL;
|
||||||
uint8_t* b_rend_src = NULL;
|
uint8_t* b_rend_src = NULL;
|
||||||
@@ -664,6 +666,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
v_hlit = self->private_impl.c_decode_dynamic[0].v_hlit;
|
v_hlit = self->private_impl.c_decode_dynamic[0].v_hlit;
|
||||||
v_hdist = self->private_impl.c_decode_dynamic[0].v_hdist;
|
v_hdist = self->private_impl.c_decode_dynamic[0].v_hdist;
|
||||||
v_hclen = self->private_impl.c_decode_dynamic[0].v_hclen;
|
v_hclen = self->private_impl.c_decode_dynamic[0].v_hclen;
|
||||||
|
v_i = self->private_impl.c_decode_dynamic[0].v_i;
|
||||||
}
|
}
|
||||||
switch (coro_susp_point) {
|
switch (coro_susp_point) {
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||||
@@ -694,9 +697,31 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
v_hclen = (((v_bits) & ((1 << (4)) - 1)) + 4);
|
v_hclen = (((v_bits) & ((1 << (4)) - 1)) + 4);
|
||||||
v_bits >>= 4;
|
v_bits >>= 4;
|
||||||
v_n_bits -= 14;
|
v_n_bits -= 14;
|
||||||
|
v_i = 0;
|
||||||
|
while (v_i < v_hclen) {
|
||||||
|
while (v_n_bits < 3) {
|
||||||
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
||||||
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||||
|
goto short_read_src;
|
||||||
|
}
|
||||||
|
uint8_t t_1 = *b_rptr_src++;
|
||||||
|
v_bits |= (((uint32_t)(t_1)) << v_n_bits);
|
||||||
|
v_n_bits += 8;
|
||||||
|
}
|
||||||
|
self->private_impl
|
||||||
|
.f_code_length_code_lengths[puffs_flate_code_order[v_i]] =
|
||||||
|
((uint8_t)((v_bits & 7)));
|
||||||
|
v_bits >>= 3;
|
||||||
|
v_n_bits -= 3;
|
||||||
|
v_i += 1;
|
||||||
|
}
|
||||||
|
while (v_i < 19) {
|
||||||
|
self->private_impl
|
||||||
|
.f_code_length_code_lengths[puffs_flate_code_order[v_i]] = 0;
|
||||||
|
v_i += 1;
|
||||||
|
}
|
||||||
self->private_impl.f_wip0 = v_hlit;
|
self->private_impl.f_wip0 = v_hlit;
|
||||||
self->private_impl.f_wip1 = v_hdist;
|
self->private_impl.f_wip1 = v_hdist;
|
||||||
self->private_impl.f_wip2 = v_hclen;
|
|
||||||
self->private_impl.f_bits = v_bits;
|
self->private_impl.f_bits = v_bits;
|
||||||
self->private_impl.f_n_bits = v_n_bits;
|
self->private_impl.f_n_bits = v_n_bits;
|
||||||
coro_susp_point = 0;
|
coro_susp_point = 0;
|
||||||
@@ -710,6 +735,7 @@ suspend:
|
|||||||
self->private_impl.c_decode_dynamic[0].v_hlit = v_hlit;
|
self->private_impl.c_decode_dynamic[0].v_hlit = v_hlit;
|
||||||
self->private_impl.c_decode_dynamic[0].v_hdist = v_hdist;
|
self->private_impl.c_decode_dynamic[0].v_hdist = v_hdist;
|
||||||
self->private_impl.c_decode_dynamic[0].v_hclen = v_hclen;
|
self->private_impl.c_decode_dynamic[0].v_hclen = v_hclen;
|
||||||
|
self->private_impl.c_decode_dynamic[0].v_i = v_i;
|
||||||
|
|
||||||
if (a_src.buf) {
|
if (a_src.buf) {
|
||||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||||
|
|||||||
+2
-1
@@ -134,9 +134,9 @@ typedef struct {
|
|||||||
|
|
||||||
uint32_t f_bits;
|
uint32_t f_bits;
|
||||||
uint32_t f_n_bits;
|
uint32_t f_n_bits;
|
||||||
|
uint8_t f_code_length_code_lengths[19];
|
||||||
uint32_t f_wip0;
|
uint32_t f_wip0;
|
||||||
uint32_t f_wip1;
|
uint32_t f_wip1;
|
||||||
uint32_t f_wip2;
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
@@ -154,6 +154,7 @@ typedef struct {
|
|||||||
uint32_t v_hlit;
|
uint32_t v_hlit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_hdist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_hclen;
|
||||||
|
uint32_t v_i;
|
||||||
} c_decode_dynamic[1];
|
} c_decode_dynamic[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_decoder;
|
} puffs_flate_decoder;
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ const (
|
|||||||
FlagsTypeChecked = Flags(0x00000020)
|
FlagsTypeChecked = Flags(0x00000020)
|
||||||
FlagsHasBreak = Flags(0x00000040)
|
FlagsHasBreak = Flags(0x00000040)
|
||||||
FlagsHasContinue = Flags(0x00000080)
|
FlagsHasContinue = Flags(0x00000080)
|
||||||
|
FlagsGlobalIdent = Flags(0x00000100)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@@ -217,6 +218,7 @@ func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 }
|
|||||||
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
|
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
|
||||||
func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 }
|
func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 }
|
||||||
func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 }
|
func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 }
|
||||||
|
func (n *Expr) GlobalIdent() bool { return n.flags&FlagsGlobalIdent != 0 }
|
||||||
func (n *Expr) ConstValue() *big.Int { return n.constValue }
|
func (n *Expr) ConstValue() *big.Int { return n.constValue }
|
||||||
func (n *Expr) MType() *TypeExpr { return n.mType }
|
func (n *Expr) MType() *TypeExpr { return n.mType }
|
||||||
func (n *Expr) ID0() t.ID { return n.id0 }
|
func (n *Expr) ID0() t.ID { return n.id0 }
|
||||||
@@ -228,6 +230,7 @@ func (n *Expr) Args() []*Node { return n.list0 }
|
|||||||
|
|
||||||
func (n *Expr) SetConstValue(x *big.Int) { n.constValue = x }
|
func (n *Expr) SetConstValue(x *big.Int) { n.constValue = x }
|
||||||
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
|
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
|
||||||
|
func (n *Expr) SetGlobalIdent() { n.flags |= FlagsGlobalIdent }
|
||||||
|
|
||||||
func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr {
|
func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr {
|
||||||
if lhs != nil {
|
if lhs != nil {
|
||||||
|
|||||||
+8
-1
@@ -312,7 +312,14 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: look for (global) names (constants, funcs, structs).
|
if c, ok := q.c.consts[id1]; ok {
|
||||||
|
// TODO: check somewhere that a global ident (i.e. a const) is
|
||||||
|
// not directly in the LHS of an assignment.
|
||||||
|
n.SetGlobalIdent()
|
||||||
|
n.SetMType(c.Const.XType())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// TODO: look for other (global) names: funcs, structs.
|
||||||
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.tm))
|
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.tm))
|
||||||
}
|
}
|
||||||
switch id1.Key() {
|
switch id1.Key() {
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ pub struct decoder?(
|
|||||||
bits u32,
|
bits u32,
|
||||||
n_bits u32,
|
n_bits u32,
|
||||||
|
|
||||||
|
code_length_code_lengths[19] u8[..7],
|
||||||
|
|
||||||
// These temporary fields are used to debug work-in-progress code.
|
// These temporary fields are used to debug work-in-progress code.
|
||||||
wip0 u32,
|
wip0 u32,
|
||||||
wip1 u32,
|
wip1 u32,
|
||||||
wip2 u32,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pub func decoder.decode?(dst writer1, src reader1)() {
|
pub func decoder.decode?(dst writer1, src reader1)() {
|
||||||
@@ -91,10 +92,30 @@ pub func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
|||||||
bits >>= 4
|
bits >>= 4
|
||||||
n_bits -= 14
|
n_bits -= 14
|
||||||
|
|
||||||
|
// Read the code length code lengths. (This is not a typo).
|
||||||
|
var i u32 = 0
|
||||||
|
while i < hclen {
|
||||||
|
while n_bits < 3,
|
||||||
|
inv i < hclen,
|
||||||
|
post n_bits >= 3,
|
||||||
|
{
|
||||||
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
|
n_bits += 8
|
||||||
|
}
|
||||||
|
assert i < 19 via "a < b: a < c; c <= b"(c:hclen)
|
||||||
|
this.code_length_code_lengths[code_order[i]] = (bits & 0x07) as u8
|
||||||
|
bits >>= 3
|
||||||
|
n_bits -= 3
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
while i < 19 {
|
||||||
|
this.code_length_code_lengths[code_order[i]] = 0
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
this.wip0 = hlit
|
this.wip0 = hlit
|
||||||
this.wip1 = hdist
|
this.wip1 = hdist
|
||||||
this.wip2 = hclen
|
|
||||||
|
|
||||||
this.bits = bits
|
this.bits = bits
|
||||||
this.n_bits = n_bits
|
this.n_bits = n_bits
|
||||||
|
|||||||
+19
-2
@@ -236,8 +236,25 @@ void test_puffs_flate_work_in_progress() {
|
|||||||
FAIL("wip1: got %" PRIu32 ", want %" PRIu32, dec.private_impl.f_wip1, 20);
|
FAIL("wip1: got %" PRIu32 ", want %" PRIu32, dec.private_impl.f_wip1, 20);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (dec.private_impl.f_wip2 != 14) {
|
|
||||||
FAIL("wip2: got %" PRIu32 ", want %" PRIu32, dec.private_impl.f_wip2, 14);
|
if (dec.private_impl.f_code_length_code_lengths[16] != 0) {
|
||||||
|
FAIL("clcl[16]: got %" PRIu8 ", want %" PRIu8,
|
||||||
|
dec.private_impl.f_code_length_code_lengths[16], 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (dec.private_impl.f_code_length_code_lengths[17] != 4) {
|
||||||
|
FAIL("clcl[17]: got %" PRIu8 ", want %" PRIu8,
|
||||||
|
dec.private_impl.f_code_length_code_lengths[17], 4);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (dec.private_impl.f_code_length_code_lengths[18] != 5) {
|
||||||
|
FAIL("clcl[18]: got %" PRIu8 ", want %" PRIu8,
|
||||||
|
dec.private_impl.f_code_length_code_lengths[18], 5);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (dec.private_impl.f_code_length_code_lengths[3] != 5) {
|
||||||
|
FAIL("clcl[3]: got %" PRIu8 ", want %" PRIu8,
|
||||||
|
dec.private_impl.f_code_length_code_lengths[3], 5);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user