std/lzma: add QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE

This commit is contained in:
Nigel Tao committed 2024-03-01 21:11:28 +11:00
1 parent 27407b5332
commit 13c72db350
5 files changed
+41 -9

No files matched your search

+6
View File
@@ -106,8 +106,14 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
}
// Decode LZMA.
//
// Re lzma.QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE, as exercised by
// test/3pdata/xzsuite/lzip-testsuite/fox6_mark.lz, the LZMA spec says
// that the decoder must check that the byte is zero, but the Lzip spec
// says that the byte "must be ignored by the range decoder".
this.ssize_have = 0
this.dsize_have = 0
this.lzma.set_quirk!(key: lzma.QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE, value: 1)
while true {
dmark = args.dst.mark()
smark = args.src.mark()
+13 -3
View File
@@ -60,6 +60,8 @@ pub struct decoder? implements base.io_transformer(
prev_lzma2_chunk_was_uncompressed : base.bool,
allow_non_zero_initial_byte : base.bool,
stashed_bytes : array[2] base.u8, // 0 is prev_byte, 1 is match_byte.
stashed_bits : base.u32,
stashed_range : base.u32,
@@ -265,7 +267,12 @@ pri func decoder.add_history!(hist: slice base.u8, workbuf: slice base.u8) base.
}
pub func decoder.get_quirk(key: base.u32) base.u64 {
if args.key == QUIRK_FORMAT_EXTENSION {
if args.key == QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE {
if this.allow_non_zero_initial_byte {
return 1
}
} else if args.key == QUIRK_FORMAT_EXTENSION {
return this.format_extension as base.u64
}
return 0
@@ -275,7 +282,10 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
var v : base.u32
var n : base.u32
if args.key == QUIRK_FORMAT_EXTENSION {
if args.key == QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE {
this.allow_non_zero_initial_byte = args.value > 0
} else if args.key == QUIRK_FORMAT_EXTENSION {
if args.value == 0 {
this.format_extension = 0
return ok
@@ -493,7 +503,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
}
c8 = args.src.read_u8?()
if c8 <> 0x00 {
if (c8 <> 0x00) and not this.allow_non_zero_initial_byte {
return "#bad code"
}
this.stashed_bits = args.src.read_u32be?()
+7 -1
View File
@@ -18,6 +18,12 @@ pri const QUIRKS_BASE : base.u32 = 0x5058_E000
// --------
// When this quirk is set, a positive or zero value ignores or enforces the "If
// initial byte [after properties or other headers] is not equal to ZERO, the
// LZMA Decoder must stop decoding and report error" from the spec
// https://raw.githubusercontent.com/jljusten/LZMA-SDK/781863cdf592da3e97420f50de5dac056ad352a5/DOC/lzma-specification.txt
pub const QUIRK_ALLOW_NON_ZERO_INITIAL_BYTE : base.u32 = 0x5058_E000 | 0x00
// When this quirk is set, a positive value indicates an extension to the LZMA
// format. Zero means to use the default LZMA format.
//
@@ -33,4 +39,4 @@ pri const QUIRKS_BASE : base.u32 = 0x5058_E000
// For LZMA2, the high 7 bytes (shifted right by 8) must be less than or equal
// to 40 and indicates the dictionary size. See section "5.3.1. LZMA2" of
// https://tukaani.org/xz/xz-file-format.txt
pub const QUIRK_FORMAT_EXTENSION : base.u32 = 0x5058_E000 | 0x00
pub const QUIRK_FORMAT_EXTENSION : base.u32 = 0x5058_E000 | 0x01