Prepare for calculating the zlib checksum.

This commit is contained in:
Nigel Tao committed 2017-10-21 11:55:58 +11:00
1 parent 4d7faac49e
commit 62935c41d7
7 files changed
+68 -21

No files matched your search

+11 -2
View File
@@ -130,7 +130,8 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
b.writes(")))") b.writes(")))")
return nil return nil
} }
if isThatMethod(g.tm, n, t.KeyIsSuspension, 0) { if isThatMethod(g.tm, n, t.KeyIsError, 0) || isThatMethod(g.tm, n, t.KeyIsOK, 0) ||
isThatMethod(g.tm, n, t.KeyIsSuspension, 0) {
if pp == parenthesesMandatory { if pp == parenthesesMandatory {
b.writeb('(') b.writeb('(')
} }
@@ -138,8 +139,16 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil { if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil {
return err return err
} }
// TODO: write < or == instead of > for KeyIsError or KeyIsOK. switch key := n.LHS().Expr().ID1().Key(); key {
case t.KeyIsError:
b.writes(" < 0")
case t.KeyIsOK:
b.writes(" == 0")
case t.KeyIsSuspension:
b.writes(" > 0") b.writes(" > 0")
default:
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther's IsXxx", key)
}
if pp == parenthesesMandatory { if pp == parenthesesMandatory {
b.writeb(')') b.writeb(')')
} }
+14 -2
View File
@@ -453,6 +453,8 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if err := g.writeLoadExprDerivedVars(b, n); err != nil { if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err return err
} }
// TODO: should we goto exit/suspend depending on if status < or > 0?
// Ditto below.
b.writes("if (status) { goto suspend; }\n") b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_lsd", 1) { } else if isThisMethod(g.tm, n, "decode_lsd", 1) {
@@ -493,6 +495,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if err := g.writeLoadExprDerivedVars(b, n); err != nil { if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err return err
} }
// TODO: check if tPrefix_temp is an error, and return?
} else if isThisMethod(g.tm, n, "decode_uncompressed", 2) { } else if isThisMethod(g.tm, n, "decode_uncompressed", 2) {
b.printf("status = %s%s_decode_uncompressed(self, %sdst, %ssrc);\n", b.printf("status = %s%s_decode_uncompressed(self, %sdst, %ssrc);\n",
@@ -546,12 +549,21 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
} else if isThatMethod(g.tm, n, g.tm.ByName("decode").Key(), 2) { } else if isThatMethod(g.tm, n, g.tm.ByName("decode").Key(), 2) {
switch g.pkgName { switch g.pkgName {
case "flate": case "flate":
b.printf("status = %sdecoder_decode(&self->private_impl.f_dec, %sdst, %ssrc);\n", // TODO: don't hard code being inside a try call.
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("%sstatus %s%d = %sdecoder_decode(&self->private_impl.f_dec, %sdst, %ssrc);\n",
g.pkgPrefix, tPrefix, temp,
g.pkgPrefix, aPrefix, aPrefix) g.pkgPrefix, aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil { if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err return err
} }
b.writes("if (status) { goto suspend; }\n") // TODO: check if tPrefix_temp is an error, and return?
case "gif": case "gif":
b.printf("status = %slzw_decoder_decode(&self->private_impl.f_lzw, %sdst, %s%s);\n", b.printf("status = %slzw_decoder_decode(&self->private_impl.f_lzw, %sdst, %s%s);\n",
g.pkgPrefix, aPrefix, vPrefix, n.Args()[1].Arg().Value().String(g.tm)) g.pkgPrefix, aPrefix, vPrefix, n.Args()[1].Arg().Value().String(g.tm))
+24 -14
View File
@@ -269,6 +269,7 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint16_t v_x; uint16_t v_x;
puffs_flate_status v_z;
uint32_t v_checksum; uint32_t v_checksum;
uint64_t scratch; uint64_t scratch;
} c_decode[1]; } c_decode[1];
@@ -2260,6 +2261,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
puffs_flate_status status = PUFFS_FLATE_STATUS_OK; puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
uint16_t v_x; uint16_t v_x;
puffs_flate_status v_z;
uint32_t v_checksum; uint32_t v_checksum;
uint8_t* b_rptr_src = NULL; uint8_t* b_rptr_src = NULL;
@@ -2281,6 +2283,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (coro_susp_point) { if (coro_susp_point) {
v_x = self->private_impl.c_decode[0].v_x; v_x = self->private_impl.c_decode[0].v_x;
v_z = self->private_impl.c_decode[0].v_z;
v_checksum = self->private_impl.c_decode[0].v_checksum; v_checksum = self->private_impl.c_decode[0].v_checksum;
} }
switch (coro_susp_point) { switch (coro_susp_point) {
@@ -2328,6 +2331,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
status = PUFFS_FLATE_ERROR_INVALID_ZLIB_PARITY_CHECK; status = PUFFS_FLATE_ERROR_INVALID_ZLIB_PARITY_CHECK;
goto exit; goto exit;
} }
while (true) {
PUFFS_COROUTINE_SUSPENSION_POINT(3); PUFFS_COROUTINE_SUSPENSION_POINT(3);
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);
@@ -2339,7 +2343,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
} }
} }
} }
status = puffs_flate_status t_2 =
puffs_flate_decoder_decode(&self->private_impl.f_dec, a_dst, a_src); puffs_flate_decoder_decode(&self->private_impl.f_dec, a_dst, a_src);
if (a_src.buf) { if (a_src.buf) {
b_rptr_src = a_src.buf->ptr + a_src.buf->ri; b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
@@ -2352,35 +2356,40 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
} }
b_rend_src = b_rptr_src + len; b_rend_src = b_rptr_src + len;
} }
if (status) { v_z = t_2;
goto suspend; if (v_z == 0) {
goto label_0_break;
} }
PUFFS_COROUTINE_SUSPENSION_POINT(4); status = v_z;
uint32_t t_3; PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
}
label_0_break:;
PUFFS_COROUTINE_SUSPENSION_POINT(5);
uint32_t t_4;
if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 4)) { if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 4)) {
t_3 = puffs_base_load_u32be(b_rptr_src); t_4 = puffs_base_load_u32be(b_rptr_src);
b_rptr_src += 4; b_rptr_src += 4;
} else { } else {
self->private_impl.c_decode[0].scratch = 0; self->private_impl.c_decode[0].scratch = 0;
PUFFS_COROUTINE_SUSPENSION_POINT(5); PUFFS_COROUTINE_SUSPENSION_POINT(6);
while (true) { while (true) {
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) { if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
uint32_t t_2 = self->private_impl.c_decode[0].scratch & 0xFF; uint32_t t_3 = self->private_impl.c_decode[0].scratch & 0xFF;
self->private_impl.c_decode[0].scratch >>= 8; self->private_impl.c_decode[0].scratch >>= 8;
self->private_impl.c_decode[0].scratch <<= 8; self->private_impl.c_decode[0].scratch <<= 8;
self->private_impl.c_decode[0].scratch |= ((uint64_t)(*b_rptr_src++)) self->private_impl.c_decode[0].scratch |= ((uint64_t)(*b_rptr_src++))
<< (64 - t_2); << (64 - t_3);
if (t_2 == 24) { if (t_3 == 24) {
t_3 = self->private_impl.c_decode[0].scratch >> (64 - 32); t_4 = self->private_impl.c_decode[0].scratch >> (64 - 32);
break; break;
} }
t_2 += 8; t_3 += 8;
self->private_impl.c_decode[0].scratch |= ((uint64_t)(t_2)); self->private_impl.c_decode[0].scratch |= ((uint64_t)(t_3));
} }
} }
v_checksum = t_3; v_checksum = t_4;
goto ok; goto ok;
ok: ok:
@@ -2392,6 +2401,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
suspend: suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode[0].v_x = v_x; self->private_impl.c_decode[0].v_x = v_x;
self->private_impl.c_decode[0].v_z = v_z;
self->private_impl.c_decode[0].v_checksum = v_checksum; self->private_impl.c_decode[0].v_checksum = v_checksum;
exit: exit:
+1
View File
@@ -269,6 +269,7 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint16_t v_x; uint16_t v_x;
puffs_flate_status v_z;
uint32_t v_checksum; uint32_t v_checksum;
uint64_t scratch; uint64_t scratch;
} c_decode[1]; } c_decode[1];
+2 -1
View File
@@ -760,7 +760,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return zero, bitMask(int(aMax.Int64())), nil return zero, bitMask(int(aMax.Int64())), nil
} }
// TODO: delete this hack that only matches "foo.is_suspension(etc)". // TODO: delete this hack that only matches "foo.is_suspension(etc)".
if isThatMethod(q.tm, n, t.KeyIsSuspension, 0) || isThatMethod(q.tm, n, t.KeySuffix, 1) { if isThatMethod(q.tm, n, t.KeyIsError, 0) || isThatMethod(q.tm, n, t.KeyIsOK, 0) ||
isThatMethod(q.tm, n, t.KeyIsSuspension, 0) || isThatMethod(q.tm, n, t.KeySuffix, 1) {
return nil, nil, nil return nil, nil, nil
} }
// TODO: delete this hack that only matches "foo.set_literal_width(etc)". // TODO: delete this hack that only matches "foo.set_literal_width(etc)".
+8 -1
View File
@@ -480,7 +480,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return nil return nil
} }
// TODO: delete this hack that only matches "foo.is_suspension(etc)". // TODO: delete this hack that only matches "foo.is_suspension(etc)".
if isThatMethod(q.tm, n, t.KeyIsSuspension, 0) { if isThatMethod(q.tm, n, t.KeyIsError, 0) ||
isThatMethod(q.tm, n, t.KeyIsOK, 0) ||
isThatMethod(q.tm, n, t.KeyIsSuspension, 0) {
foo := n.LHS().Expr().LHS().Expr() foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil { if err := q.tcheckExpr(foo, depth); err != nil {
return err return err
@@ -526,7 +528,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err return err
} }
} }
if n.ID0().Key() == t.KeyTry {
// TODO: be more principled about inferring "try etc"'s type.
n.SetMType(typeExprStatus)
} else {
n.SetMType(typeExprPlaceholder) // HACK. n.SetMType(typeExprPlaceholder) // HACK.
}
return nil return nil
} }
// TODO: delete this hack that only matches "foo.length(etc)". // TODO: delete this hack that only matches "foo.length(etc)".
+8 -1
View File
@@ -37,7 +37,14 @@ pub func zlib_decoder.decode?(dst writer1, src reader1)() {
if (x % 31) != 0 { if (x % 31) != 0 {
return error "invalid zlib parity check" return error "invalid zlib parity check"
} }
this.dec.decode?(dst:in.dst, src:in.src) while true {
var z status = try this.dec.decode?(dst:in.dst, src:in.src)
// TODO: calculate the checksum.
if z.is_ok() {
break
}
return z
}
var checksum u32 = in.src.read_u32be?() var checksum u32 = in.src.read_u32be?()
// TODO: verify the checksum. // TODO: verify the checksum.
} }