mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Add zlib QUIRK_JUST_RAW_DEFLATE
This commit is contained in:
1 parent
38b101d170
commit
8ef4c062d9
5 files changed
+150
-36
No files matched your search
@@ -61,3 +61,4 @@ Package-specific quirks:
|
||||
|
||||
- [GIF image decoder quirks](/std/gif/decode_quirks.wuffs)
|
||||
- [JSON decoder quirks](/std/json/decode_quirks.wuffs)
|
||||
- [ZLIB decoder quirks](/std/zlib/decode_quirks.wuffs)
|
||||
@@ -8930,6 +8930,8 @@ extern const char wuffs_zlib__error__incorrect_dictionary[];
|
||||
|
||||
// ---------------- Public Consts
|
||||
|
||||
#define WUFFS_ZLIB__QUIRK_JUST_RAW_DEFLATE 2113790976
|
||||
|
||||
#define WUFFS_ZLIB__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 1
|
||||
|
||||
// ---------------- Struct Declarations
|
||||
@@ -9041,6 +9043,7 @@ struct wuffs_zlib__decoder__struct {
|
||||
bool f_header_complete;
|
||||
bool f_got_dictionary;
|
||||
bool f_want_dictionary;
|
||||
bool f_quirks[1];
|
||||
bool f_ignore_checksum;
|
||||
uint32_t f_dict_id_got;
|
||||
uint32_t f_dict_id_want;
|
||||
@@ -34902,6 +34905,10 @@ const char wuffs_zlib__error__incorrect_dictionary[] = "#zlib: incorrect diction
|
||||
|
||||
// ---------------- Private Consts
|
||||
|
||||
#define WUFFS_ZLIB__QUIRKS_BASE 2113790976
|
||||
|
||||
#define WUFFS_ZLIB__QUIRKS_COUNT 1
|
||||
|
||||
// ---------------- Private Initializer Prototypes
|
||||
|
||||
// ---------------- Private Function Prototypes
|
||||
@@ -35065,8 +35072,15 @@ wuffs_zlib__decoder__set_quirk_enabled(
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
if (a_quirk == 1) {
|
||||
if (self->private_impl.f_header_complete) {
|
||||
self->private_impl.f_bad_call_sequence = true;
|
||||
} else if (a_quirk == 1) {
|
||||
self->private_impl.f_ignore_checksum = a_enabled;
|
||||
} else if (a_quirk >= 2113790976) {
|
||||
a_quirk -= 2113790976;
|
||||
if (a_quirk < 1) {
|
||||
self->private_impl.f_quirks[a_quirk] = a_enabled;
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
@@ -35156,6 +35170,7 @@ wuffs_zlib__decoder__transform_io(
|
||||
if (self->private_impl.f_bad_call_sequence) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
|
||||
goto exit;
|
||||
} else if (self->private_impl.f_quirks[0]) {
|
||||
} else if ( ! self->private_impl.f_want_dictionary) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
@@ -35263,7 +35278,7 @@ wuffs_zlib__decoder__transform_io(
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
}
|
||||
}
|
||||
if ( ! self->private_impl.f_ignore_checksum) {
|
||||
if ( ! self->private_impl.f_ignore_checksum && ! self->private_impl.f_quirks[0]) {
|
||||
v_checksum_got = wuffs_adler32__hasher__update_u32(&self->private_data.f_checksum, wuffs_base__io__since(v_mark, ((uint64_t)(iop_a_dst - io0_a_dst)), io0_a_dst));
|
||||
}
|
||||
if (wuffs_base__status__is_ok(&v_status)) {
|
||||
@@ -35273,38 +35288,40 @@ wuffs_zlib__decoder__transform_io(
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
|
||||
}
|
||||
label__0__break:;
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
|
||||
uint32_t t_3;
|
||||
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
|
||||
t_3 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
|
||||
iop_a_src += 4;
|
||||
} else {
|
||||
self->private_data.s_transform_io[0].scratch = 0;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||
while (true) {
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
if ( ! self->private_impl.f_quirks[0]) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
|
||||
uint32_t t_3;
|
||||
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) {
|
||||
t_3 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src);
|
||||
iop_a_src += 4;
|
||||
} else {
|
||||
self->private_data.s_transform_io[0].scratch = 0;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
|
||||
while (true) {
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
}
|
||||
uint64_t* scratch = &self->private_data.s_transform_io[0].scratch;
|
||||
uint32_t num_bits_3 = ((uint32_t)(*scratch & 0xFF));
|
||||
*scratch >>= 8;
|
||||
*scratch <<= 8;
|
||||
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_3);
|
||||
if (num_bits_3 == 24) {
|
||||
t_3 = ((uint32_t)(*scratch >> 32));
|
||||
break;
|
||||
}
|
||||
num_bits_3 += 8;
|
||||
*scratch |= ((uint64_t)(num_bits_3));
|
||||
}
|
||||
uint64_t* scratch = &self->private_data.s_transform_io[0].scratch;
|
||||
uint32_t num_bits_3 = ((uint32_t)(*scratch & 0xFF));
|
||||
*scratch >>= 8;
|
||||
*scratch <<= 8;
|
||||
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_3);
|
||||
if (num_bits_3 == 24) {
|
||||
t_3 = ((uint32_t)(*scratch >> 32));
|
||||
break;
|
||||
}
|
||||
num_bits_3 += 8;
|
||||
*scratch |= ((uint64_t)(num_bits_3));
|
||||
}
|
||||
v_checksum_want = t_3;
|
||||
}
|
||||
if ( ! self->private_impl.f_ignore_checksum && (v_checksum_got != v_checksum_want)) {
|
||||
status = wuffs_base__make_status(wuffs_zlib__error__bad_checksum);
|
||||
goto exit;
|
||||
}
|
||||
v_checksum_want = t_3;
|
||||
}
|
||||
if ( ! self->private_impl.f_ignore_checksum && (v_checksum_got != v_checksum_want)) {
|
||||
status = wuffs_base__make_status(wuffs_zlib__error__bad_checksum);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ok:
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2021 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// --------
|
||||
|
||||
// Quirks are discussed in (/doc/note/quirks.md).
|
||||
//
|
||||
// The base38 encoding of "zlib" is 0x1F_7F79. Left shifting by 10 gives
|
||||
// 0x7DFD_E400.
|
||||
pri const QUIRKS_BASE : base.u32 = 0x7DFD_E400
|
||||
|
||||
// --------
|
||||
|
||||
// When this quirk is enabled, the decoder speaks just raw deflate, without
|
||||
// zlib's configuration prefix and checksum suffix wrapped around that.
|
||||
//
|
||||
// A zlib.decoder basically becomes a deflate.decoder. This is useful when the
|
||||
// caller might want to choose between the two related-but-slightly-different
|
||||
// formats at run time but would rather not allocate two separate decoders.
|
||||
pub const QUIRK_JUST_RAW_DEFLATE : base.u32 = 0x7DFD_E400 | 0x00
|
||||
|
||||
pri const QUIRKS_COUNT : base.u32 = 0x01
|
||||
@@ -33,6 +33,8 @@ pub struct decoder? implements base.io_transformer(
|
||||
got_dictionary : base.bool,
|
||||
want_dictionary : base.bool,
|
||||
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
|
||||
ignore_checksum : base.bool,
|
||||
checksum : adler32.hasher,
|
||||
|
||||
@@ -60,8 +62,15 @@ pub func decoder.add_dictionary!(dict: slice base.u8) {
|
||||
}
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
if this.header_complete {
|
||||
this.bad_call_sequence = true
|
||||
} else if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
} else if args.quirk >= QUIRKS_BASE {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +89,8 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
|
||||
if this.bad_call_sequence {
|
||||
return base."#bad call sequence"
|
||||
} else if this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
// No-op.
|
||||
} else if not this.want_dictionary {
|
||||
x = args.src.read_u16be?()
|
||||
if ((x >> 8) & 0x0F) <> 0x08 {
|
||||
@@ -112,7 +123,7 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.flate.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if not this.ignore_checksum {
|
||||
if (not this.ignore_checksum) and (not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE]) {
|
||||
checksum_got = this.checksum.update_u32!(x: args.dst.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
@@ -120,9 +131,12 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_got <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
|
||||
if not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_got <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: reset state (e.g. want_dictionary), so that we can read concat'ed
|
||||
|
||||
@@ -71,6 +71,11 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
|
||||
|
||||
// ---------------- Golden Tests
|
||||
|
||||
golden_test g_deflate_romeo_gt = {
|
||||
.want_filename = "test/data/romeo.txt",
|
||||
.src_filename = "test/data/romeo.txt.deflate",
|
||||
};
|
||||
|
||||
golden_test g_zlib_midsummer_gt = {
|
||||
.want_filename = "test/data/midsummer.txt",
|
||||
.src_filename = "test/data/midsummer.txt.zlib",
|
||||
@@ -107,6 +112,42 @@ test_wuffs_zlib_decode_interface() {
|
||||
"test/data/romeo.txt.zlib", 0, SIZE_MAX, 942, 0x0A);
|
||||
}
|
||||
|
||||
const char* //
|
||||
wuffs_raw_deflate_decode(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
uint32_t wuffs_initialize_flags,
|
||||
uint64_t wlimit,
|
||||
uint64_t rlimit) {
|
||||
wuffs_zlib__decoder dec;
|
||||
CHECK_STATUS("initialize",
|
||||
wuffs_zlib__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION,
|
||||
wuffs_initialize_flags));
|
||||
|
||||
// This wuffs_zlib__decoder__set_quirk_enabled call is the only difference
|
||||
// between wuffs_raw_deflate_decode and wuffs_zlib_decode immediately below.
|
||||
wuffs_zlib__decoder__set_quirk_enabled(
|
||||
&dec, WUFFS_ZLIB__QUIRK_JUST_RAW_DEFLATE, true);
|
||||
|
||||
while (true) {
|
||||
wuffs_base__io_buffer limited_dst = make_limited_writer(*dst, wlimit);
|
||||
wuffs_base__io_buffer limited_src = make_limited_reader(*src, rlimit);
|
||||
|
||||
wuffs_base__status status = wuffs_zlib__decoder__transform_io(
|
||||
&dec, &limited_dst, &limited_src, g_work_slice_u8);
|
||||
|
||||
dst->meta.wi += limited_dst.meta.wi;
|
||||
src->meta.ri += limited_src.meta.ri;
|
||||
|
||||
if (((wlimit < UINT64_MAX) &&
|
||||
(status.repr == wuffs_base__suspension__short_write)) ||
|
||||
((rlimit < UINT64_MAX) &&
|
||||
(status.repr == wuffs_base__suspension__short_read))) {
|
||||
continue;
|
||||
}
|
||||
return status.repr;
|
||||
}
|
||||
}
|
||||
|
||||
const char* //
|
||||
wuffs_zlib_decode(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
@@ -240,6 +281,13 @@ test_wuffs_zlib_decode_pi() {
|
||||
UINT64_MAX);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_zlib_decode_raw_deflate_romeo() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_io_buffers(wuffs_raw_deflate_decode, &g_deflate_romeo_gt,
|
||||
UINT64_MAX, UINT64_MAX);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_zlib_decode_sheep() {
|
||||
CHECK_FOCUS(__func__);
|
||||
@@ -376,6 +424,7 @@ proc g_tests[] = {
|
||||
test_wuffs_zlib_decode_interface,
|
||||
test_wuffs_zlib_decode_midsummer,
|
||||
test_wuffs_zlib_decode_pi,
|
||||
test_wuffs_zlib_decode_raw_deflate_romeo,
|
||||
test_wuffs_zlib_decode_sheep,
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
Reference in new issue
Block a user