Add zlib QUIRK_JUST_RAW_DEFLATE

This commit is contained in:
Nigel Tao committed 2021-11-26 23:13:57 +11:00
1 parent 38b101d170
commit 8ef4c062d9
5 files changed
+150 -36

No files matched your search

+33
View File
@@ -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
+19 -5
View File
@@ -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