From ef899b04925e491aff420534df16cfaefda97419 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 28 Mar 2018 12:36:31 +1100 Subject: [PATCH] Make gzip checksum check length, not just CRC32 --- gen/c/std/gzip.c | 22 +++++++++++++++++++--- gen/h/std/gzip.h | 1 + std/gzip/decode_gzip.wuffs | 9 +++++---- test/c/std/gzip.c | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/gen/c/std/gzip.c b/gen/c/std/gzip.c index 83826b05..9889780d 100644 --- a/gen/c/std/gzip.c +++ b/gen/c/std/gzip.c @@ -519,6 +519,7 @@ typedef struct { uint8_t v_c; uint16_t v_xlen; uint32_t v_checksum_got; + uint32_t v_decoded_length_got; wuffs_gzip__status v_z; uint32_t v_checksum_want; uint32_t v_decoded_length_want; @@ -1025,6 +1026,7 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, uint8_t v_c; uint16_t v_xlen; uint32_t v_checksum_got; + uint32_t v_decoded_length_got; wuffs_gzip__status v_z; uint32_t v_checksum_want; uint32_t v_decoded_length_want; @@ -1069,6 +1071,7 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, v_c = self->private_impl.c_decode[0].v_c; v_xlen = self->private_impl.c_decode[0].v_xlen; v_checksum_got = self->private_impl.c_decode[0].v_checksum_got; + v_decoded_length_got = self->private_impl.c_decode[0].v_decoded_length_got; v_z = self->private_impl.c_decode[0].v_z; v_checksum_want = self->private_impl.c_decode[0].v_checksum_want; v_decoded_length_want = @@ -1210,6 +1213,7 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, goto exit; } v_checksum_got = 0; + v_decoded_length_got = 0; while (true) { wuffs_base__writer1__mark(&a_dst, b_wptr_dst); { @@ -1253,6 +1257,18 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, ? (size_t)(b_wptr_dst - a_dst.private_impl.mark) : 0, })); + v_decoded_length_got = + (v_decoded_length_got + + ((uint32_t)(( + ((uint64_t)(((wuffs_base__slice_u8){ + .ptr = a_dst.private_impl.mark, + .len = a_dst.private_impl.mark + ? (size_t)(b_wptr_dst - + a_dst.private_impl.mark) + : 0, + }) + .len)) & + 4294967295)))); } if (v_z == 0) { goto label_2_break; @@ -1318,12 +1334,11 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, v_decoded_length_want = t_12; } if (!self->private_impl.f_ignore_checksum && - (v_checksum_got != v_checksum_want)) { + ((v_checksum_got != v_checksum_want) || + (v_decoded_length_got != v_decoded_length_want))) { status = WUFFS_GZIP__ERROR_CHECKSUM_MISMATCH; goto exit; } - if (v_decoded_length_want != 0) { - } goto ok; ok: @@ -1338,6 +1353,7 @@ suspend: self->private_impl.c_decode[0].v_c = v_c; self->private_impl.c_decode[0].v_xlen = v_xlen; self->private_impl.c_decode[0].v_checksum_got = v_checksum_got; + self->private_impl.c_decode[0].v_decoded_length_got = v_decoded_length_got; self->private_impl.c_decode[0].v_z = v_z; self->private_impl.c_decode[0].v_checksum_want = v_checksum_want; self->private_impl.c_decode[0].v_decoded_length_want = v_decoded_length_want; diff --git a/gen/h/std/gzip.h b/gen/h/std/gzip.h index 019b5948..c3b0e6c5 100644 --- a/gen/h/std/gzip.h +++ b/gen/h/std/gzip.h @@ -519,6 +519,7 @@ typedef struct { uint8_t v_c; uint16_t v_xlen; uint32_t v_checksum_got; + uint32_t v_decoded_length_got; wuffs_gzip__status v_z; uint32_t v_checksum_want; uint32_t v_decoded_length_want; diff --git a/std/gzip/decode_gzip.wuffs b/std/gzip/decode_gzip.wuffs index 640ede20..5bc02d47 100644 --- a/std/gzip/decode_gzip.wuffs +++ b/std/gzip/decode_gzip.wuffs @@ -90,11 +90,14 @@ pub func decoder.decode?(dst writer1, src reader1)() { // Decode and checksum the DEFLATE-encoded payload. var checksum_got u32 + var decoded_length_got u32 while true { in.dst.mark() var z status = try this.flate.decode?(dst:in.dst, src:in.src) if not this.ignore_checksum { checksum_got = this.checksum.update(x:in.dst.since_mark()) + decoded_length_got = decoded_length_got ~+ + ((in.dst.since_mark().length() & 0xFFFFFFFF) as u32) } if z.is_ok() { break @@ -103,10 +106,8 @@ pub func decoder.decode?(dst writer1, src reader1)() { } var checksum_want u32 = in.src.read_u32le?() var decoded_length_want u32 = in.src.read_u32le?() - if (not this.ignore_checksum) and (checksum_got != checksum_want) { + if (not this.ignore_checksum) and + ((checksum_got != checksum_want) or (decoded_length_got != decoded_length_want)) { return error "checksum mismatch" } - if decoded_length_want != 0 { - // TODO: compare with decoded_length_got. - } } diff --git a/test/c/std/gzip.c b/test/c/std/gzip.c index f091a76f..20942c4b 100644 --- a/test/c/std/gzip.c +++ b/test/c/std/gzip.c @@ -245,7 +245,7 @@ void bench_mimic_gzip_decode_100k() { proc tests[] = { test_wuffs_gzip_checksum_ignore, // - // test_wuffs_gzip_checksum_verify_bad1, // TODO: uncomment + test_wuffs_gzip_checksum_verify_bad1, // test_wuffs_gzip_checksum_verify_bad7, // test_wuffs_gzip_checksum_verify_good, // test_wuffs_gzip_decode_midsummer, //