mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
std/lzip: actually implement the format
This commit is contained in:
1 parent
793ab36569
commit
a9739a8921
7 files changed
+464
-62
No files matched your search
+111
-7
@@ -8,19 +8,32 @@
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// The Lzip file format specification is at
|
||||
// https://www.nongnu.org/lzip/manual/lzip_manual.html#File-format
|
||||
|
||||
use "std/crc32"
|
||||
use "std/lzma"
|
||||
|
||||
pub status "#bad checksum"
|
||||
pub status "#bad footer"
|
||||
pub status "#bad header"
|
||||
pub status "#truncated input"
|
||||
|
||||
pub const DECODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 1
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0xFFFF_FFFF + 273
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
ignore_checksum : base.bool,
|
||||
|
||||
dsize_have : base.u64,
|
||||
ssize_have : base.u64,
|
||||
|
||||
util : base.utility,
|
||||
) + (
|
||||
crc32 : crc32.ieee_hasher,
|
||||
lzma : lzma.decoder,
|
||||
)
|
||||
|
||||
pub func decoder.get_quirk(key: base.u32) base.u64 {
|
||||
@@ -39,13 +52,11 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
|
||||
}
|
||||
|
||||
pub func decoder.dst_history_retain_length() base.optional_u63 {
|
||||
return this.util.make_optional_u63(has_value: true, value: 0)
|
||||
return this.lzma.dst_history_retain_length()
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.lzma.workbuf_len()
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
@@ -61,5 +72,98 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
}
|
||||
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
args.dst.write_u8?(a: 'x')
|
||||
var c8 : base.u8
|
||||
var c32 : base.u32
|
||||
var c64 : base.u64
|
||||
var dmark : base.u64
|
||||
var smark : base.u64
|
||||
var status : base.status
|
||||
var checksum_want : base.u32
|
||||
var checksum_have : base.u32
|
||||
var size_want : base.u64
|
||||
|
||||
while.outer true {
|
||||
// Read the magic number and version number.
|
||||
//
|
||||
// Some of the test/3pdata/xzsuite/good*v0*.lz files have a 0x00
|
||||
// version number (instead of 0x01). We reject those, just like
|
||||
// /usr/bin/lzip does and as per the Lzip spec.
|
||||
c64 = args.src.read_u40le_as_u64?()
|
||||
if c64 <> 'LZIP\x01'le {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
// Read the dictionary size.
|
||||
c8 = args.src.read_u8?()
|
||||
status = this.lzma.set_quirk!(
|
||||
key: lzma.QUIRK_FORMAT_EXTENSION,
|
||||
value: 0x01 | ((c8 as base.u64) << 8))
|
||||
if not status.is_ok() {
|
||||
if status == base."#bad argument" {
|
||||
return "#bad header"
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
// Decode LZMA.
|
||||
this.ssize_have = 0
|
||||
this.dsize_have = 0
|
||||
while true {
|
||||
dmark = args.dst.mark()
|
||||
smark = args.src.mark()
|
||||
status =? this.lzma.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
this.ssize_have ~mod+= args.src.count_since(mark: smark)
|
||||
this.dsize_have ~mod+= args.dst.count_since(mark: dmark)
|
||||
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.update!(x: args.dst.since(mark: dmark))
|
||||
}
|
||||
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
|
||||
// Check the checksum.
|
||||
checksum_want = args.src.read_u32le?()
|
||||
if not this.ignore_checksum {
|
||||
checksum_have = this.crc32.checksum_u32()
|
||||
if checksum_have <> checksum_want {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
|
||||
// Check the uncompressed size.
|
||||
size_want = args.src.read_u64le?()
|
||||
if this.dsize_have <> size_want {
|
||||
return "#bad footer"
|
||||
}
|
||||
|
||||
// Check the compressed size, which includes 6 bytes of header and 20
|
||||
// bytes of footer. Per the spec, it's also capped at 2 PiB.
|
||||
size_want = args.src.read_u64le?()
|
||||
if (size_want < 26) or (0x8_0000_0000_0000 < size_want) {
|
||||
return "#bad footer"
|
||||
} else if this.ssize_have <> (size_want - 26) {
|
||||
return "#bad footer"
|
||||
}
|
||||
|
||||
// Reset state and continue the outer loop, if not at EOF and it looks
|
||||
// like there's another LZIP-encoded chunk.
|
||||
while args.src.length() < 4,
|
||||
post args.src.length() >= 4,
|
||||
{
|
||||
if args.src.is_closed() {
|
||||
break.outer
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
c32 = args.src.peek_u32le()
|
||||
if c32 <> 'LZIP'le {
|
||||
break.outer
|
||||
}
|
||||
this.crc32.reset!()
|
||||
this.lzma.reset!()
|
||||
} endwhile.outer
|
||||
}
|
||||
+38
-13
@@ -273,22 +273,40 @@ pub func decoder.get_quirk(key: base.u32) base.u64 {
|
||||
|
||||
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.value == 0 {
|
||||
// No-op.
|
||||
} else if ((args.value & 0xFF) <> 0x02) or ((args.value >> 8) > 40) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.format_extension = (args.value & 0xFFFF_FFFF) as base.u32
|
||||
this.format_extension = 0
|
||||
return ok
|
||||
|
||||
v = this.format_extension >> 8
|
||||
if v < 40 {
|
||||
this.dict_size = (2 | (v & 1)) << ((v >> 1) + 11)
|
||||
} else {
|
||||
this.dict_size = 0xFFFF_FFFF
|
||||
} else if (args.value & 0xFF) == 1 {
|
||||
if (args.value >> 8) <= 0xFF {
|
||||
this.format_extension = (args.value & 0xFFFF_FFFF) as base.u32
|
||||
v = this.format_extension >> 8
|
||||
n = (1 as base.u32) << (v & 0x1F)
|
||||
n ~sat-= (n >> 4) * ((v >> 5) & 7)
|
||||
if (n < (1 << 12)) or ((1 << 29) < n) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.dict_size = n
|
||||
return ok
|
||||
}
|
||||
|
||||
} else if (args.value & 0xFF) == 2 {
|
||||
if (args.value >> 8) <= 40 {
|
||||
this.format_extension = (args.value & 0xFFFF_FFFF) as base.u32
|
||||
v = this.format_extension >> 8
|
||||
if v < 40 {
|
||||
this.dict_size = (2 | (v & 1)) << ((v >> 1) + 11)
|
||||
} else {
|
||||
this.dict_size = 0xFFFF_FFFF
|
||||
}
|
||||
return ok
|
||||
}
|
||||
}
|
||||
return ok
|
||||
|
||||
return base."#bad argument"
|
||||
}
|
||||
return base."#unsupported option"
|
||||
}
|
||||
@@ -352,7 +370,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
// header, see
|
||||
// https://github.com/jljusten/LZMA-SDK/blob/781863cdf592da3e97420f50de5dac056ad352a5/C/Lzma2Dec.c#L17-L23
|
||||
while true {
|
||||
if this.format_extension == 0 { // LZMA1.
|
||||
if (this.format_extension & 0xFF) == 0 { // LZMA1.
|
||||
prop_byte = args.src.read_u8?()
|
||||
if prop_byte >= 225 {
|
||||
return "#bad header"
|
||||
@@ -379,6 +397,13 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
|
||||
this.initialize_probs!()
|
||||
|
||||
} else if (this.format_extension & 0xFF) == 1 { // Lzip.
|
||||
this.lc = 3
|
||||
this.lp = 0
|
||||
this.pb = 2
|
||||
this.decoded_length = 0xFFFF_FFFF_FFFF_FFFF
|
||||
this.initialize_probs!()
|
||||
|
||||
} else { // LZMA2.
|
||||
while args.src.length() <= 0,
|
||||
post args.src.length() > 0,
|
||||
@@ -514,7 +539,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
}
|
||||
}
|
||||
|
||||
if this.format_extension == 0 {
|
||||
if (this.format_extension & 0xFF) < 2 {
|
||||
break
|
||||
} else if this.lzma2_encoded_length_have <> this.lzma2_encoded_length_want {
|
||||
return "#bad LZMA2 header"
|
||||
|
||||
@@ -22,7 +22,13 @@ pri const QUIRKS_BASE : base.u32 = 0x5058_E000
|
||||
// format. Zero means to use the default LZMA format.
|
||||
//
|
||||
// When positive, the low byte of the u64 value indicates the extension.
|
||||
// Currently, the only valid extension is 0x02, indicating LZMA2.
|
||||
// Currently, the only valid extensions are:
|
||||
// - 0x01 indicates Lzip.
|
||||
// - 0x02 indicates LZMA2.
|
||||
//
|
||||
// For Lzip, the high 7 bytes (shifted right by 8) must be less than or equal
|
||||
// to 0xFF and indicates the dictionary size. See section "6 File format" of
|
||||
// https://www.nongnu.org/lzip/manual/lzip_manual.html
|
||||
//
|
||||
// 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
|
||||
|
||||
Reference in new issue
Block a user