mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
std/lzma: accept optional End of Stream code
This commit is contained in:
1 parent
22b4ff4842
commit
1c0b07f1aa
5 files changed
+172
-26
No files matched your search
@@ -10485,12 +10485,14 @@ struct wuffs_lzma__decoder__struct {
|
||||
uint32_t f_stashed_rep2;
|
||||
uint32_t f_stashed_rep3;
|
||||
uint64_t f_stashed_pos;
|
||||
uint64_t f_stashed_pos_end;
|
||||
|
||||
uint32_t p_decode_bitstream_slow;
|
||||
uint32_t p_transform_io;
|
||||
uint32_t p_do_transform_io;
|
||||
uint32_t p_decode_bitstream;
|
||||
uint32_t p_update_stashed_bytes;
|
||||
uint32_t p_decode_optional_end_of_stream;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
@@ -51001,6 +51003,13 @@ wuffs_lzma__decoder__update_stashed_bytes(
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_lzma__decoder__decode_optional_end_of_stream(
|
||||
wuffs_lzma__decoder* self,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_lzma__decoder__initialize_dict(
|
||||
@@ -51219,10 +51228,10 @@ wuffs_lzma__decoder__decode_bitstream_slow(
|
||||
v_rep2 = self->private_impl.f_stashed_rep2;
|
||||
v_rep3 = self->private_impl.f_stashed_rep3;
|
||||
v_pos = self->private_impl.f_stashed_pos;
|
||||
v_pos_end = self->private_impl.f_stashed_pos_end;
|
||||
v_lc = self->private_impl.f_lc;
|
||||
v_lp_mask = ((((uint64_t)(1u)) << self->private_impl.f_lp) - 1u);
|
||||
v_pb_mask = ((((uint64_t)(1u)) << self->private_impl.f_pb) - 1u);
|
||||
v_pos_end = wuffs_base__u64__sat_add(v_pos, self->private_impl.f_decoded_length);
|
||||
while (v_pos < v_pos_end) {
|
||||
v_index_ao00 = ((v_state << 4u) | ((uint32_t)((v_pos & v_pb_mask))));
|
||||
v_prob = ((uint32_t)(self->private_data.f_probs_ao00[v_index_ao00]));
|
||||
@@ -52141,13 +52150,6 @@ wuffs_lzma__decoder__decode_bitstream_slow(
|
||||
v_prev_byte = ((uint8_t)(v_match_cusp));
|
||||
}
|
||||
label__outer__break:;
|
||||
if (v_bits != 0u) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_bitstream_trailer);
|
||||
goto exit;
|
||||
} else if ((self->private_impl.f_decoded_length != 18446744073709551615u) && (v_pos != v_pos_end)) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_decoded_length);
|
||||
goto exit;
|
||||
}
|
||||
self->private_impl.f_stashed_bytes[0u] = v_prev_byte;
|
||||
self->private_impl.f_stashed_bytes[1u] = v_match_byte;
|
||||
self->private_impl.f_stashed_bits = v_bits;
|
||||
@@ -52158,6 +52160,7 @@ wuffs_lzma__decoder__decode_bitstream_slow(
|
||||
self->private_impl.f_stashed_rep2 = v_rep2;
|
||||
self->private_impl.f_stashed_rep3 = v_rep3;
|
||||
self->private_impl.f_stashed_pos = v_pos;
|
||||
self->private_impl.f_stashed_pos_end = v_pos_end;
|
||||
|
||||
ok:
|
||||
self->private_impl.p_decode_bitstream_slow = 0;
|
||||
@@ -52871,6 +52874,11 @@ wuffs_lzma__decoder__do_transform_io(
|
||||
goto exit;
|
||||
}
|
||||
self->private_impl.f_stashed_range = 4294967295u;
|
||||
self->private_impl.f_stashed_pos_end = wuffs_base__u64__sat_add(self->private_impl.f_stashed_pos, self->private_impl.f_decoded_length);
|
||||
if ((self->private_impl.f_stashed_pos_end == 18446744073709551615u) && (self->private_impl.f_decoded_length != 18446744073709551615u)) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__unsupported_decoded_length);
|
||||
goto exit;
|
||||
}
|
||||
self->private_impl.f_lzma2_encoded_length_have = 5u;
|
||||
while (((uint64_t)(a_workbuf.len)) < (((uint64_t)(self->private_impl.f_dict_size)) + 273u)) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_workbuf);
|
||||
@@ -52901,6 +52909,31 @@ wuffs_lzma__decoder__do_transform_io(
|
||||
status = v_status;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(22);
|
||||
}
|
||||
if (self->private_impl.f_decoded_length == 18446744073709551615u) {
|
||||
if (self->private_impl.f_stashed_bits != 0u) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_bitstream_trailer);
|
||||
goto exit;
|
||||
}
|
||||
} else if (self->private_impl.f_stashed_pos != self->private_impl.f_stashed_pos_end) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_decoded_length);
|
||||
goto exit;
|
||||
} else if (self->private_impl.f_stashed_bits != 0u) {
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(23);
|
||||
status = wuffs_lzma__decoder__decode_optional_end_of_stream(self, a_src, a_workbuf);
|
||||
if (a_src) {
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
}
|
||||
if (status.repr) {
|
||||
goto suspend;
|
||||
}
|
||||
if (self->private_impl.f_stashed_bits != 0u) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_bitstream_trailer);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
if (self->private_impl.f_format_extension == 0u) {
|
||||
break;
|
||||
} else if (self->private_impl.f_lzma2_encoded_length_have != self->private_impl.f_lzma2_encoded_length_want) {
|
||||
@@ -53051,6 +53084,79 @@ wuffs_lzma__decoder__update_stashed_bytes(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func lzma.decoder.decode_optional_end_of_stream
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_lzma__decoder__decode_optional_end_of_stream(
|
||||
wuffs_lzma__decoder* self,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
wuffs_base__io_buffer u_w = wuffs_base__empty_io_buffer();
|
||||
wuffs_base__io_buffer* v_w = &u_w;
|
||||
uint8_t* iop_v_w WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
uint8_t* io0_v_w WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
uint8_t* io1_v_w WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
uint8_t* io2_v_w WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
wuffs_base__status v_status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_decode_optional_end_of_stream;
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
self->private_impl.f_stashed_pos_end = 18446744073709551615u;
|
||||
while (true) {
|
||||
{
|
||||
wuffs_base__io_buffer* o_0_v_w = v_w;
|
||||
uint8_t* o_0_iop_v_w = iop_v_w;
|
||||
uint8_t* o_0_io0_v_w = io0_v_w;
|
||||
uint8_t* o_0_io1_v_w = io1_v_w;
|
||||
uint8_t* o_0_io2_v_w = io2_v_w;
|
||||
v_w = wuffs_private_impl__io_writer__set(
|
||||
&u_w,
|
||||
&iop_v_w,
|
||||
&io0_v_w,
|
||||
&io1_v_w,
|
||||
&io2_v_w,
|
||||
wuffs_base__utility__empty_slice_u8(),
|
||||
0u);
|
||||
{
|
||||
wuffs_base__status t_0 = wuffs_lzma__decoder__decode_bitstream_slow(self, v_w, a_src, a_workbuf);
|
||||
v_status = t_0;
|
||||
}
|
||||
v_w = o_0_v_w;
|
||||
iop_v_w = o_0_iop_v_w;
|
||||
io0_v_w = o_0_io0_v_w;
|
||||
io1_v_w = o_0_io1_v_w;
|
||||
io2_v_w = o_0_io2_v_w;
|
||||
}
|
||||
if (wuffs_base__status__is_ok(&v_status)) {
|
||||
break;
|
||||
} else if (v_status.repr == wuffs_base__suspension__short_write) {
|
||||
status = wuffs_base__make_status(wuffs_lzma__error__bad_bitstream_trailer);
|
||||
goto exit;
|
||||
}
|
||||
status = v_status;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
|
||||
}
|
||||
self->private_impl.f_stashed_pos_end = self->private_impl.f_stashed_pos;
|
||||
|
||||
ok:
|
||||
self->private_impl.p_decode_optional_end_of_stream = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_decode_optional_end_of_stream = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func lzma.decoder.initialize_dict
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ conceptually like this:
|
||||
AlgOve23 slot = decodeSlot(min(len-2, 3))
|
||||
AlgOve24 distBiasedBy1 = decodeDistBiasedBy1(slot)
|
||||
AlgOve25 if distBiasedBy1 == 0xFFFF_FFFF {
|
||||
AlgOve26 break
|
||||
AlgOve26 break // End of Stream.
|
||||
AlgOve27 }
|
||||
AlgOve28 mrud = (1 + distBiasedBy1, mrud[0], mrud[1], mrud[2])
|
||||
AlgOve29 goto labelDoTheLZCopy
|
||||
|
||||
@@ -90,13 +90,13 @@ pri func decoder.decode_bitstream_slow?(dst: base.io_writer, src: base.io_reader
|
||||
rep2 = this.stashed_rep2
|
||||
rep3 = this.stashed_rep3
|
||||
pos = this.stashed_pos
|
||||
pos_end = this.stashed_pos_end
|
||||
|
||||
lc = this.lc
|
||||
lp_mask = ((1 as base.u64) << this.lp) - 1
|
||||
pb_mask = ((1 as base.u64) << this.pb) - 1
|
||||
|
||||
// "outer" is the core loop discussed in README.md's "Algorithm Overview".
|
||||
pos_end = pos ~sat+ this.decoded_length
|
||||
while.outer pos < pos_end {
|
||||
// AlgOve00 if decodeTheNextBym() == 0
|
||||
index_ao00 = (state << 4) | ((pos & pb_mask) as base.u32)
|
||||
@@ -904,12 +904,6 @@ pri func decoder.decode_bitstream_slow?(dst: base.io_writer, src: base.io_reader
|
||||
prev_byte = (match_cusp & 0xFF) as base.u8
|
||||
} endwhile.outer
|
||||
|
||||
if bits <> 0 {
|
||||
return "#bad bitstream trailer"
|
||||
} else if (this.decoded_length <> 0xFFFF_FFFF_FFFF_FFFF) and (pos <> pos_end) {
|
||||
return "#bad decoded length"
|
||||
}
|
||||
|
||||
this.stashed_bytes[0] = prev_byte
|
||||
this.stashed_bytes[1] = match_byte
|
||||
this.stashed_bits = bits
|
||||
@@ -920,4 +914,5 @@ pri func decoder.decode_bitstream_slow?(dst: base.io_writer, src: base.io_reader
|
||||
this.stashed_rep2 = rep2
|
||||
this.stashed_rep3 = rep3
|
||||
this.stashed_pos = pos
|
||||
this.stashed_pos_end = pos_end
|
||||
}
|
||||
@@ -60,15 +60,16 @@ pub struct decoder? implements base.io_transformer(
|
||||
|
||||
prev_lzma2_chunk_was_uncompressed : base.bool,
|
||||
|
||||
stashed_bytes : array[2] base.u8, // 0 is prev_byte, 1 is match_byte.
|
||||
stashed_bits : base.u32,
|
||||
stashed_range : base.u32,
|
||||
stashed_state : base.u32[..= 11],
|
||||
stashed_rep0 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep1 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep2 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep3 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_pos : base.u64,
|
||||
stashed_bytes : array[2] base.u8, // 0 is prev_byte, 1 is match_byte.
|
||||
stashed_bits : base.u32,
|
||||
stashed_range : base.u32,
|
||||
stashed_state : base.u32[..= 11],
|
||||
stashed_rep0 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep1 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep2 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_rep3 : base.u32[..= 0xFFFF_FFFE],
|
||||
stashed_pos : base.u64,
|
||||
stashed_pos_end : base.u64,
|
||||
|
||||
util : base.utility,
|
||||
) + (
|
||||
@@ -475,6 +476,11 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
return "#bad code"
|
||||
}
|
||||
this.stashed_range = 0xFFFF_FFFF
|
||||
this.stashed_pos_end = this.stashed_pos ~sat+ this.decoded_length
|
||||
if (this.stashed_pos_end == 0xFFFF_FFFF_FFFF_FFFF) and
|
||||
(this.decoded_length <> 0xFFFF_FFFF_FFFF_FFFF) {
|
||||
return "#unsupported decoded length"
|
||||
}
|
||||
this.lzma2_encoded_length_have = 5
|
||||
|
||||
while args.workbuf.length() < ((this.dict_size as base.u64) + 273) {
|
||||
@@ -491,6 +497,23 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, work
|
||||
yield? status
|
||||
} endwhile
|
||||
|
||||
if this.decoded_length == 0xFFFF_FFFF_FFFF_FFFF {
|
||||
if this.stashed_bits <> 0 {
|
||||
return "#bad bitstream trailer"
|
||||
}
|
||||
} else if this.stashed_pos <> this.stashed_pos_end {
|
||||
return "#bad decoded length"
|
||||
} else if this.stashed_bits <> 0 {
|
||||
// Almost all LZMA chunks have either an explicit decoded_length or
|
||||
// an End of Stream marker. It is legitimate, though, for a chunk
|
||||
// to have both. We cater for that here.
|
||||
this.decode_optional_end_of_stream?(src: args.src, workbuf: args.workbuf)
|
||||
|
||||
if this.stashed_bits <> 0 {
|
||||
return "#bad bitstream trailer"
|
||||
}
|
||||
}
|
||||
|
||||
if this.format_extension == 0 {
|
||||
break
|
||||
} else if this.lzma2_encoded_length_have <> this.lzma2_encoded_length_want {
|
||||
@@ -543,6 +566,28 @@ pri func decoder.update_stashed_bytes?(dst: base.io_writer, workbuf: roslice bas
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// Preconditions:
|
||||
// - this.stashed_pos == this.stashed_pos_end
|
||||
// - this.stashed_pos_end < 0xFFFF_FFFF_FFFF_FFFF
|
||||
pri func decoder.decode_optional_end_of_stream?(src: base.io_reader, workbuf: roslice base.u8) {
|
||||
var w : base.io_writer
|
||||
var status : base.status
|
||||
|
||||
this.stashed_pos_end = 0xFFFF_FFFF_FFFF_FFFF
|
||||
while true {
|
||||
io_bind (io: w, data: this.util.empty_slice_u8(), history_position: 0) {
|
||||
status =? this.decode_bitstream_slow?(dst: w, src: args.src, workbuf: args.workbuf)
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status == base."$short write" {
|
||||
return "#bad bitstream trailer"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
this.stashed_pos_end = this.stashed_pos
|
||||
}
|
||||
|
||||
pri func decoder.initialize_dict!() {
|
||||
this.dict_workbuf_index = 0
|
||||
this.dict_seen = 0
|
||||
|
||||
@@ -56,6 +56,6 @@ OK. 8d115715 test/3pdata/xzsuite/good-1-riscv-lzma2-2.xz
|
||||
OK. cc270da1 test/3pdata/xzsuite/good-1-sparc-lzma2.xz
|
||||
OK. a3679091 test/3pdata/xzsuite/good-1-x86-lzma2.xz
|
||||
OK. 15a2a343 test/3pdata/xzsuite/good-2-lzma2.xz
|
||||
BAD 15a2a343 test/3pdata/xzsuite/good-known_size-with_eopm.lzma
|
||||
OK. 15a2a343 test/3pdata/xzsuite/good-known_size-with_eopm.lzma
|
||||
OK. 15a2a343 test/3pdata/xzsuite/good-known_size-without_eopm.lzma
|
||||
OK. 15a2a343 test/3pdata/xzsuite/good-unknown_size-with_eopm.lzma
|
||||
Reference in new issue
Block a user