mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
std/xz: decode concatenated streams
This commit is contained in:
1 parent
5b62289c63
commit
22b4ff4842
7 files changed
+141
-13
No files matched your search
@@ -0,0 +1,40 @@
|
||||
// Copyright 2024 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// Quirks are discussed in (/doc/note/quirks.md).
|
||||
//
|
||||
// The base38 encoding of "xz " is 0x1E1EBC. Left shifting by 10 gives
|
||||
// 0x787A_F000.
|
||||
pri const QUIRKS_BASE : base.u32 = 0x787A_F000
|
||||
|
||||
// --------
|
||||
|
||||
// When this quirk is set, a positive value means to allow one or more Streams
|
||||
// (which can have trailing Stream Padding). Zero means exactly one Stream (and
|
||||
// decoding stops at the end of that Stream; no Stream Padding is consumed).
|
||||
//
|
||||
// The XZ spec (section 2. Overall Structure of .xz File) says:
|
||||
//
|
||||
// While a typical file contains only one Stream and no Stream Padding, a
|
||||
// decoder handling standalone .xz files SHOULD support files that have more
|
||||
// than one Stream or Stream Padding.
|
||||
//
|
||||
// In contrast to standalone .xz files, when the .xz file format is used as
|
||||
// an internal part of some other file format or communication protocol, it
|
||||
// usually is expected that the decoder stops after the first Stream, and
|
||||
// doesn't look for Stream Padding or possibly other Streams.
|
||||
//
|
||||
// For Wuffs, the quirk value is zero by default, which means to decode the
|
||||
// XZ-as-part-of-something-else format, not the XZ-standalone format. Like most
|
||||
// of Wuffs' other std/foobar quirks, decoding a more lenient variant of the
|
||||
// foobar format is opt-in, not opt-out.
|
||||
pub const QUIRK_DECODE_STANDALONE_CONCATENATED_STREAMS : base.u32 = 0x787A_F000 | 0x00
|
||||
+45
-4
@@ -22,6 +22,7 @@ pub status "#bad checksum"
|
||||
pub status "#bad filter"
|
||||
pub status "#bad footer"
|
||||
pub status "#bad header"
|
||||
pub status "#bad header (concatenated stream)"
|
||||
pub status "#bad index"
|
||||
pub status "#bad padding"
|
||||
pub status "#truncated input"
|
||||
@@ -44,7 +45,8 @@ pub struct decoder? implements base.io_transformer(
|
||||
// 3: SHA-256.
|
||||
checksummer : base.u8[..= 3],
|
||||
|
||||
ignore_checksum : base.bool,
|
||||
ignore_checksum : base.bool,
|
||||
standalone_format : base.bool,
|
||||
|
||||
lzma_needs_reset : base.bool,
|
||||
|
||||
@@ -102,8 +104,14 @@ pub struct decoder? implements base.io_transformer(
|
||||
)
|
||||
|
||||
pub func decoder.get_quirk(key: base.u32) base.u64 {
|
||||
if (args.key == base.QUIRK_IGNORE_CHECKSUM) and this.ignore_checksum {
|
||||
return 1
|
||||
if args.key == base.QUIRK_IGNORE_CHECKSUM {
|
||||
if this.ignore_checksum {
|
||||
return 1
|
||||
}
|
||||
} else if args.key == QUIRK_DECODE_STANDALONE_CONCATENATED_STREAMS {
|
||||
if this.standalone_format {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -112,6 +120,9 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
|
||||
if args.key == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.value > 0
|
||||
return ok
|
||||
} else if args.key == QUIRK_DECODE_STANDALONE_CONCATENATED_STREAMS {
|
||||
this.standalone_format = args.value > 0
|
||||
return ok
|
||||
}
|
||||
return base."#unsupported option"
|
||||
}
|
||||
@@ -151,6 +162,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
var uncompressed_size : base.u64
|
||||
var hash : base.u32
|
||||
var c8 : base.u8
|
||||
var c32 : base.u32
|
||||
var footer_magic : base.u16
|
||||
|
||||
while.streams true {
|
||||
@@ -182,6 +194,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
}
|
||||
this.flags = (header_magic & 0xFFFF) as base.u16
|
||||
|
||||
this.num_actual_blocks = 0
|
||||
while.blocks true {
|
||||
if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
@@ -431,7 +444,34 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
return "#bad footer"
|
||||
}
|
||||
|
||||
break.streams
|
||||
if not this.standalone_format {
|
||||
break.streams
|
||||
}
|
||||
|
||||
// Consume any Stream Padding.
|
||||
while.stream_padding true {
|
||||
while args.src.length() < 4,
|
||||
post args.src.length() >= 4,
|
||||
{
|
||||
if args.src.is_closed() {
|
||||
if args.src.length() == 0 {
|
||||
break.streams
|
||||
} else {
|
||||
return "#truncated input"
|
||||
}
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
|
||||
c32 = args.src.peek_u32le()
|
||||
if c32 == '\xFD\x37\x7A\x58'le {
|
||||
break.stream_padding
|
||||
} else if c32 <> 0 {
|
||||
return "#bad header (concatenated stream)"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
} endwhile.stream_padding
|
||||
this.started_verify_index = false
|
||||
} endwhile.streams
|
||||
}
|
||||
|
||||
@@ -643,6 +683,7 @@ pri func decoder.verify_index?(src: base.io_reader) {
|
||||
return "#bad index"
|
||||
}
|
||||
|
||||
this.num_index_blocks = 0
|
||||
shift = 0
|
||||
while true {
|
||||
c8 = args.src.read_u8?()
|
||||
|
||||
Reference in new issue
Block a user