mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 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
|
||||
}
|
||||
Reference in new issue
Block a user