mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
Prepare for calculating the zlib checksum.
This commit is contained in:
1 parent
4d7faac49e
commit
62935c41d7
7 files changed
+89
-42
No files matched your search
@@ -130,7 +130,8 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.writes(")))")
|
||||
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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
// TODO: write < or == instead of > for KeyIsError or KeyIsOK.
|
||||
b.writes(" > 0")
|
||||
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")
|
||||
default:
|
||||
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther's IsXxx", key)
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
|
||||
@@ -453,6 +453,8 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: should we goto exit/suspend depending on if status < or > 0?
|
||||
// Ditto below.
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} 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 {
|
||||
return err
|
||||
}
|
||||
// TODO: check if tPrefix_temp is an error, and return?
|
||||
|
||||
} else if isThisMethod(g.tm, n, "decode_uncompressed", 2) {
|
||||
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) {
|
||||
switch g.pkgName {
|
||||
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)
|
||||
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
// TODO: check if tPrefix_temp is an error, and return?
|
||||
|
||||
case "gif":
|
||||
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))
|
||||
|
||||
+43
-33
@@ -269,6 +269,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint16_t v_x;
|
||||
puffs_flate_status v_z;
|
||||
uint32_t v_checksum;
|
||||
uint64_t scratch;
|
||||
} c_decode[1];
|
||||
@@ -2260,6 +2261,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
|
||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||
|
||||
uint16_t v_x;
|
||||
puffs_flate_status v_z;
|
||||
uint32_t v_checksum;
|
||||
|
||||
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;
|
||||
if (coro_susp_point) {
|
||||
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;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
@@ -2328,59 +2331,65 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
|
||||
status = PUFFS_FLATE_ERROR_INVALID_ZLIB_PARITY_CHECK;
|
||||
goto exit;
|
||||
}
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_src.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
while (true) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||
if (a_src.buf) {
|
||||
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
||||
a_src.buf->ri += n;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_src.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
status =
|
||||
puffs_flate_decoder_decode(&self->private_impl.f_dec, a_dst, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
size_t len = a_src.buf->wi - a_src.buf->ri;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_src.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
puffs_flate_status t_2 =
|
||||
puffs_flate_decoder_decode(&self->private_impl.f_dec, a_dst, a_src);
|
||||
if (a_src.buf) {
|
||||
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
||||
size_t len = a_src.buf->wi - a_src.buf->ri;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_src.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
}
|
||||
}
|
||||
b_rend_src = b_rptr_src + len;
|
||||
}
|
||||
b_rend_src = b_rptr_src + len;
|
||||
v_z = t_2;
|
||||
if (v_z == 0) {
|
||||
goto label_0_break;
|
||||
}
|
||||
status = v_z;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
|
||||
}
|
||||
if (status) {
|
||||
goto suspend;
|
||||
}
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
||||
uint32_t t_3;
|
||||
label_0_break:;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(5);
|
||||
uint32_t t_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;
|
||||
} else {
|
||||
self->private_impl.c_decode[0].scratch = 0;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(5);
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(6);
|
||||
while (true) {
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_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 |= ((uint64_t)(*b_rptr_src++))
|
||||
<< (64 - t_2);
|
||||
if (t_2 == 24) {
|
||||
t_3 = self->private_impl.c_decode[0].scratch >> (64 - 32);
|
||||
<< (64 - t_3);
|
||||
if (t_3 == 24) {
|
||||
t_4 = self->private_impl.c_decode[0].scratch >> (64 - 32);
|
||||
break;
|
||||
}
|
||||
t_2 += 8;
|
||||
self->private_impl.c_decode[0].scratch |= ((uint64_t)(t_2));
|
||||
t_3 += 8;
|
||||
self->private_impl.c_decode[0].scratch |= ((uint64_t)(t_3));
|
||||
}
|
||||
}
|
||||
v_checksum = t_3;
|
||||
v_checksum = t_4;
|
||||
|
||||
goto ok;
|
||||
ok:
|
||||
@@ -2392,6 +2401,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
|
||||
suspend:
|
||||
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_z = v_z;
|
||||
self->private_impl.c_decode[0].v_checksum = v_checksum;
|
||||
|
||||
exit:
|
||||
|
||||
@@ -269,6 +269,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint16_t v_x;
|
||||
puffs_flate_status v_z;
|
||||
uint32_t v_checksum;
|
||||
uint64_t scratch;
|
||||
} c_decode[1];
|
||||
|
||||
@@ -760,7 +760,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return zero, bitMask(int(aMax.Int64())), nil
|
||||
}
|
||||
// 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
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
|
||||
+9
-2
@@ -480,7 +480,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// 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()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -526,7 +528,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
if n.ID0().Key() == t.KeyTry {
|
||||
// TODO: be more principled about inferring "try etc"'s type.
|
||||
n.SetMType(typeExprStatus)
|
||||
} else {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.length(etc)".
|
||||
|
||||
@@ -37,7 +37,14 @@ pub func zlib_decoder.decode?(dst writer1, src reader1)() {
|
||||
if (x % 31) != 0 {
|
||||
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?()
|
||||
// TODO: verify the checksum.
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user