Make gzip checksum check length, not just CRC32

This commit is contained in:
Nigel Tao committed 2018-03-28 12:36:31 +11:00
1 parent a8e40fedc9
commit ef899b0492
4 files changed
+26 -8

No files matched your search

+19 -3
View File
@@ -519,6 +519,7 @@ typedef struct {
uint8_t v_c; uint8_t v_c;
uint16_t v_xlen; uint16_t v_xlen;
uint32_t v_checksum_got; uint32_t v_checksum_got;
uint32_t v_decoded_length_got;
wuffs_gzip__status v_z; wuffs_gzip__status v_z;
uint32_t v_checksum_want; uint32_t v_checksum_want;
uint32_t v_decoded_length_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; uint8_t v_c;
uint16_t v_xlen; uint16_t v_xlen;
uint32_t v_checksum_got; uint32_t v_checksum_got;
uint32_t v_decoded_length_got;
wuffs_gzip__status v_z; wuffs_gzip__status v_z;
uint32_t v_checksum_want; uint32_t v_checksum_want;
uint32_t v_decoded_length_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_c = self->private_impl.c_decode[0].v_c;
v_xlen = self->private_impl.c_decode[0].v_xlen; v_xlen = self->private_impl.c_decode[0].v_xlen;
v_checksum_got = self->private_impl.c_decode[0].v_checksum_got; 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_z = self->private_impl.c_decode[0].v_z;
v_checksum_want = self->private_impl.c_decode[0].v_checksum_want; v_checksum_want = self->private_impl.c_decode[0].v_checksum_want;
v_decoded_length_want = v_decoded_length_want =
@@ -1210,6 +1213,7 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self,
goto exit; goto exit;
} }
v_checksum_got = 0; v_checksum_got = 0;
v_decoded_length_got = 0;
while (true) { while (true) {
wuffs_base__writer1__mark(&a_dst, b_wptr_dst); 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) ? (size_t)(b_wptr_dst - a_dst.private_impl.mark)
: 0, : 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) { if (v_z == 0) {
goto label_2_break; 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; v_decoded_length_want = t_12;
} }
if (!self->private_impl.f_ignore_checksum && 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; status = WUFFS_GZIP__ERROR_CHECKSUM_MISMATCH;
goto exit; goto exit;
} }
if (v_decoded_length_want != 0) {
}
goto ok; goto ok;
ok: ok:
@@ -1338,6 +1353,7 @@ suspend:
self->private_impl.c_decode[0].v_c = v_c; 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_xlen = v_xlen;
self->private_impl.c_decode[0].v_checksum_got = v_checksum_got; 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_z = v_z;
self->private_impl.c_decode[0].v_checksum_want = v_checksum_want; 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; self->private_impl.c_decode[0].v_decoded_length_want = v_decoded_length_want;
+1
View File
@@ -519,6 +519,7 @@ typedef struct {
uint8_t v_c; uint8_t v_c;
uint16_t v_xlen; uint16_t v_xlen;
uint32_t v_checksum_got; uint32_t v_checksum_got;
uint32_t v_decoded_length_got;
wuffs_gzip__status v_z; wuffs_gzip__status v_z;
uint32_t v_checksum_want; uint32_t v_checksum_want;
uint32_t v_decoded_length_want; uint32_t v_decoded_length_want;
+5 -4
View File
@@ -90,11 +90,14 @@ pub func decoder.decode?(dst writer1, src reader1)() {
// Decode and checksum the DEFLATE-encoded payload. // Decode and checksum the DEFLATE-encoded payload.
var checksum_got u32 var checksum_got u32
var decoded_length_got u32
while true { while true {
in.dst.mark() in.dst.mark()
var z status = try this.flate.decode?(dst:in.dst, src:in.src) var z status = try this.flate.decode?(dst:in.dst, src:in.src)
if not this.ignore_checksum { if not this.ignore_checksum {
checksum_got = this.checksum.update(x:in.dst.since_mark()) 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() { if z.is_ok() {
break break
@@ -103,10 +106,8 @@ pub func decoder.decode?(dst writer1, src reader1)() {
} }
var checksum_want u32 = in.src.read_u32le?() var checksum_want u32 = in.src.read_u32le?()
var decoded_length_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" return error "checksum mismatch"
} }
if decoded_length_want != 0 {
// TODO: compare with decoded_length_got.
}
} }
+1 -1
View File
@@ -245,7 +245,7 @@ void bench_mimic_gzip_decode_100k() {
proc tests[] = { proc tests[] = {
test_wuffs_gzip_checksum_ignore, // 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_bad7, //
test_wuffs_gzip_checksum_verify_good, // test_wuffs_gzip_checksum_verify_good, //
test_wuffs_gzip_decode_midsummer, // test_wuffs_gzip_decode_midsummer, //