mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 16:47:28 +01:00
Implement the gzip checksum
Also fix a bug in ast.Raw.SetPackage, discovered because type checking failed to show that "[] u8" and "[] u8" are equivalent types.
This commit is contained in:
1 parent
153d1276a1
commit
a8e40fedc9
7 files changed
+184
-28
No files matched your search
+89
-2
@@ -97,6 +97,89 @@ const char* wuffs_gzip_decode(wuffs_base__buf1* dst,
|
||||
}
|
||||
}
|
||||
|
||||
bool do_test_wuffs_gzip_checksum(bool ignore_checksum, uint32_t bad_checksum) {
|
||||
wuffs_base__buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
|
||||
wuffs_base__buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
|
||||
|
||||
if (!read_file(&src, gzip_midsummer_gt.src_filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Flip a bit in the gzip checksum, which is in the last 8 bytes of the file.
|
||||
if (src.wi < 8) {
|
||||
FAIL("source file was too short");
|
||||
return false;
|
||||
}
|
||||
if (bad_checksum) {
|
||||
src.ptr[src.wi - 1 - (bad_checksum & 7)] ^= 1;
|
||||
}
|
||||
|
||||
int end_limit;
|
||||
for (end_limit = 0; end_limit < 10; end_limit++) {
|
||||
wuffs_gzip__decoder dec;
|
||||
wuffs_gzip__decoder__initialize(&dec, WUFFS_VERSION, 0);
|
||||
wuffs_gzip__decoder__set_ignore_checksum(&dec, ignore_checksum);
|
||||
got.wi = 0;
|
||||
wuffs_base__writer1 got_writer = {.buf = &got};
|
||||
src.ri = 0;
|
||||
wuffs_base__reader1 src_reader = {.buf = &src};
|
||||
|
||||
// Decode the src data in 1 or 2 chunks, depending on whether end_limit is
|
||||
// or isn't zero.
|
||||
int i;
|
||||
for (i = 0; i < 2; i++) {
|
||||
wuffs_gzip__status want = 0;
|
||||
if (i == 0) {
|
||||
if (end_limit == 0) {
|
||||
continue;
|
||||
}
|
||||
if (src.wi < end_limit) {
|
||||
FAIL("end_limit=%d: not enough source data", end_limit);
|
||||
return false;
|
||||
}
|
||||
uint64_t rlim = src.wi - (uint64_t)(end_limit);
|
||||
src_reader.private_impl.limit.ptr_to_len = &rlim;
|
||||
want = WUFFS_GZIP__SUSPENSION_SHORT_READ;
|
||||
} else {
|
||||
src_reader.private_impl.limit.ptr_to_len = NULL;
|
||||
want = (bad_checksum && !ignore_checksum)
|
||||
? WUFFS_GZIP__ERROR_CHECKSUM_MISMATCH
|
||||
: WUFFS_GZIP__STATUS_OK;
|
||||
}
|
||||
|
||||
wuffs_gzip__status status =
|
||||
wuffs_gzip__decoder__decode(&dec, got_writer, src_reader);
|
||||
if (status != want) {
|
||||
FAIL("end_limit=%d: got %" PRIi32 " (%s), want %" PRIi32 " (%s)",
|
||||
end_limit, status, wuffs_gzip__status__string(status), want,
|
||||
wuffs_gzip__status__string(want));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void test_wuffs_gzip_checksum_ignore() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_gzip_checksum(true, 1);
|
||||
}
|
||||
|
||||
void test_wuffs_gzip_checksum_verify_bad1() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_gzip_checksum(false, 1);
|
||||
}
|
||||
|
||||
void test_wuffs_gzip_checksum_verify_bad7() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_gzip_checksum(false, 7);
|
||||
}
|
||||
|
||||
void test_wuffs_gzip_checksum_verify_good() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_gzip_checksum(false, 0);
|
||||
}
|
||||
|
||||
void test_wuffs_gzip_decode_midsummer() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_buf1_buf1(wuffs_gzip_decode, &gzip_midsummer_gt, 0, 0);
|
||||
@@ -161,8 +244,12 @@ void bench_mimic_gzip_decode_100k() {
|
||||
// The empty comments forces clang-format to place one element per line.
|
||||
proc tests[] = {
|
||||
|
||||
test_wuffs_gzip_decode_midsummer, //
|
||||
test_wuffs_gzip_decode_pi, //
|
||||
test_wuffs_gzip_checksum_ignore, //
|
||||
// test_wuffs_gzip_checksum_verify_bad1, // TODO: uncomment
|
||||
test_wuffs_gzip_checksum_verify_bad7, //
|
||||
test_wuffs_gzip_checksum_verify_good, //
|
||||
test_wuffs_gzip_decode_midsummer, //
|
||||
test_wuffs_gzip_decode_pi, //
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
|
||||
+8
-8
@@ -231,17 +231,17 @@ bool do_test_wuffs_zlib_checksum(bool ignore_checksum, bool bad_checksum) {
|
||||
if (!read_file(&src, zlib_midsummer_gt.src_filename)) {
|
||||
return false;
|
||||
}
|
||||
if (src.wi == 0) {
|
||||
FAIL("source file was empty");
|
||||
// Flip a bit in the zlib checksum, which is in the last 4 bytes of the file.
|
||||
if (src.wi < 4) {
|
||||
FAIL("source file was too short");
|
||||
return false;
|
||||
}
|
||||
// Flip a bit in the zlib checksum, which comes at the end of the file.
|
||||
if (bad_checksum) {
|
||||
src.ptr[src.wi - 1] ^= 1;
|
||||
src.ptr[src.wi - 1 - (bad_checksum & 3)] ^= 1;
|
||||
}
|
||||
|
||||
int end_limit;
|
||||
for (end_limit = 0; end_limit < 8; end_limit++) {
|
||||
for (end_limit = 0; end_limit < 10; end_limit++) {
|
||||
wuffs_zlib__decoder dec;
|
||||
wuffs_zlib__decoder__initialize(&dec, WUFFS_VERSION, 0);
|
||||
wuffs_zlib__decoder__set_ignore_checksum(&dec, ignore_checksum);
|
||||
@@ -288,17 +288,17 @@ bool do_test_wuffs_zlib_checksum(bool ignore_checksum, bool bad_checksum) {
|
||||
|
||||
void test_wuffs_zlib_checksum_ignore() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_zlib_checksum(true, true);
|
||||
do_test_wuffs_zlib_checksum(true, 1);
|
||||
}
|
||||
|
||||
void test_wuffs_zlib_checksum_verify_bad() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_zlib_checksum(false, true);
|
||||
do_test_wuffs_zlib_checksum(false, 1);
|
||||
}
|
||||
|
||||
void test_wuffs_zlib_checksum_verify_good() {
|
||||
CHECK_FOCUS(__func__);
|
||||
do_test_wuffs_zlib_checksum(false, false);
|
||||
do_test_wuffs_zlib_checksum(false, 0);
|
||||
}
|
||||
|
||||
void test_wuffs_zlib_decode_midsummer() {
|
||||
|
||||
Reference in new issue
Block a user