std/lzma: track prior decoded length

This commit is contained in:
Nigel Tao committed 2023-12-26 23:48:47 +11:00
1 parent 5a7299b5c2
commit 2afec50f62
4 files changed
+18 -6

No files matched your search

+8 -1
View File
@@ -10468,6 +10468,7 @@ struct wuffs_lzma__decoder__struct {
uint32_t f_format_extension; uint32_t f_format_extension;
uint32_t f_dict_size; uint32_t f_dict_size;
uint64_t f_decoded_length; uint64_t f_decoded_length;
uint64_t f_lzma2_prior_decoded_length;
uint64_t f_lzma2_encoded_length_have; uint64_t f_lzma2_encoded_length_have;
uint64_t f_lzma2_encoded_length_want; uint64_t f_lzma2_encoded_length_want;
bool f_lzma2_need_prob_reset; bool f_lzma2_need_prob_reset;
@@ -52015,7 +52016,7 @@ wuffs_lzma__decoder__decode_bitstream_slow(
v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LONGREP[v_state])); v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LONGREP[v_state]));
} while (0); } while (0);
} while (0); } while (0);
if ((((uint64_t)((v_rep0 + 1u))) > v_pos) || (((uint64_t)((v_rep0 + 1u))) > ((uint64_t)(self->private_impl.f_dict_size)))) { if ((((uint64_t)((v_rep0 + 1u))) > ((uint64_t)(v_pos + self->private_impl.f_lzma2_prior_decoded_length))) || (((uint64_t)((v_rep0 + 1u))) > ((uint64_t)(self->private_impl.f_dict_size)))) {
status = wuffs_base__make_status(wuffs_lzma__error__bad_distance); status = wuffs_base__make_status(wuffs_lzma__error__bad_distance);
goto exit; goto exit;
} }
@@ -52269,6 +52270,7 @@ wuffs_lzma__decoder__do_transform_io(
uint32_t v_pb = 0; uint32_t v_pb = 0;
uint32_t v_length = 0; uint32_t v_length = 0;
uint32_t v_n_copied = 0; uint32_t v_n_copied = 0;
uint64_t v_dmark = 0;
uint64_t v_smark = 0; uint64_t v_smark = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL); wuffs_base__status v_status = wuffs_base__make_status(NULL);
@@ -52423,6 +52425,7 @@ wuffs_lzma__decoder__do_transform_io(
} }
if (v_header_byte < 128u) { if (v_header_byte < 128u) {
if (v_header_byte < 2u) { if (v_header_byte < 2u) {
self->private_impl.f_lzma2_prior_decoded_length = 0u;
self->private_impl.f_lzma2_need_prob_reset = true; self->private_impl.f_lzma2_need_prob_reset = true;
self->private_impl.f_lzma2_need_properties = true; self->private_impl.f_lzma2_need_properties = true;
} else if (v_header_byte > 2u) { } else if (v_header_byte > 2u) {
@@ -52465,6 +52468,7 @@ wuffs_lzma__decoder__do_transform_io(
if (v_length <= v_n_copied) { if (v_length <= v_n_copied) {
break; break;
} }
wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_lzma2_prior_decoded_length, ((uint64_t)(v_n_copied)));
v_length -= v_n_copied; v_length -= v_n_copied;
if (((uint64_t)(io2_a_dst - iop_a_dst)) == 0u) { if (((uint64_t)(io2_a_dst - iop_a_dst)) == 0u) {
status = wuffs_base__make_status(wuffs_base__suspension__short_write); status = wuffs_base__make_status(wuffs_base__suspension__short_write);
@@ -52576,6 +52580,7 @@ wuffs_lzma__decoder__do_transform_io(
self->private_impl.f_lzma2_need_properties = false; self->private_impl.f_lzma2_need_properties = false;
} }
if (v_header_byte >= 224u) { if (v_header_byte >= 224u) {
self->private_impl.f_lzma2_prior_decoded_length = 0u;
} }
if (self->private_impl.f_lzma2_need_prob_reset || self->private_impl.f_lzma2_need_properties) { if (self->private_impl.f_lzma2_need_prob_reset || self->private_impl.f_lzma2_need_properties) {
status = wuffs_base__make_status(wuffs_lzma__error__bad_lzma2_header); status = wuffs_base__make_status(wuffs_lzma__error__bad_lzma2_header);
@@ -52584,6 +52589,7 @@ wuffs_lzma__decoder__do_transform_io(
} }
self->private_impl.f_lzma2_encoded_length_have = 0u; self->private_impl.f_lzma2_encoded_length_have = 0u;
while (true) { while (true) {
v_dmark = ((uint64_t)(iop_a_dst - io0_a_dst));
v_smark = ((uint64_t)(iop_a_src - io0_a_src)); v_smark = ((uint64_t)(iop_a_src - io0_a_src));
{ {
if (a_dst) { if (a_dst) {
@@ -52602,6 +52608,7 @@ wuffs_lzma__decoder__do_transform_io(
} }
} }
wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_lzma2_encoded_length_have, wuffs_private_impl__io__count_since(v_smark, ((uint64_t)(iop_a_src - io0_a_src)))); wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_lzma2_encoded_length_have, wuffs_private_impl__io__count_since(v_smark, ((uint64_t)(iop_a_src - io0_a_src))));
wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_lzma2_prior_decoded_length, wuffs_private_impl__io__count_since(v_dmark, ((uint64_t)(iop_a_dst - io0_a_dst))));
if (wuffs_base__status__is_ok(&v_status)) { if (wuffs_base__status__is_ok(&v_status)) {
break; break;
} }
+1 -1
View File
@@ -805,7 +805,7 @@ pri func decoder.decode_bitstream_slow?(dst: base.io_writer, src: base.io_reader
// AlgOve90 labelDoTheLZCopy: // AlgOve90 labelDoTheLZCopy:
// AlgOve94 emitCopy(len, mrud[0]) // AlgOve94 emitCopy(len, mrud[0])
assert len >= 1 assert len >= 1
if (((rep0 + 1) as base.u64) > pos) or if (((rep0 + 1) as base.u64) > (pos ~mod+ this.lzma2_prior_decoded_length)) or
(((rep0 + 1) as base.u64) > (this.dict_size as base.u64)) { (((rep0 + 1) as base.u64) > (this.dict_size as base.u64)) {
return "#bad distance" return "#bad distance"
} }
+7 -2
View File
@@ -40,6 +40,7 @@ pub struct decoder? implements base.io_transformer(
decoded_length : base.u64, decoded_length : base.u64,
lzma2_prior_decoded_length : base.u64,
lzma2_encoded_length_have : base.u64, lzma2_encoded_length_have : base.u64,
lzma2_encoded_length_want : base.u64, lzma2_encoded_length_want : base.u64,
lzma2_need_prob_reset : base.bool, lzma2_need_prob_reset : base.bool,
@@ -167,6 +168,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader) {
var length : base.u32 var length : base.u32
var n_copied : base.u32 var n_copied : base.u32
var dmark : base.u64
var smark : base.u64 var smark : base.u64
var status : base.status var status : base.status
@@ -219,7 +221,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader) {
header_byte = args.src.read_u8?() header_byte = args.src.read_u8?()
if header_byte < 0x80 { if header_byte < 0x80 {
if header_byte < 0x02 { if header_byte < 0x02 {
// TODO: enforce "reset dictionary". this.lzma2_prior_decoded_length = 0
this.lzma2_need_prob_reset = true this.lzma2_need_prob_reset = true
this.lzma2_need_properties = true this.lzma2_need_properties = true
} else if header_byte > 0x02 { } else if header_byte > 0x02 {
@@ -234,6 +236,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader) {
if length <= n_copied { if length <= n_copied {
break break
} }
this.lzma2_prior_decoded_length ~sat+= n_copied as base.u64
length -= n_copied length -= n_copied
if args.dst.length() == 0 { if args.dst.length() == 0 {
yield? base."$short write" yield? base."$short write"
@@ -275,7 +278,7 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader) {
} }
if header_byte >= 0xE0 { if header_byte >= 0xE0 {
// TODO: enforce "reset dictionary". this.lzma2_prior_decoded_length = 0
} }
if this.lzma2_need_prob_reset or this.lzma2_need_properties { if this.lzma2_need_prob_reset or this.lzma2_need_properties {
@@ -285,9 +288,11 @@ pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader) {
this.lzma2_encoded_length_have = 0 this.lzma2_encoded_length_have = 0
while true { while true {
dmark = args.dst.mark()
smark = args.src.mark() smark = args.src.mark()
status =? this.decode_bitstream?(dst: args.dst, src: args.src) status =? this.decode_bitstream?(dst: args.dst, src: args.src)
this.lzma2_encoded_length_have ~sat+= args.src.count_since(mark: smark) this.lzma2_encoded_length_have ~sat+= args.src.count_since(mark: smark)
this.lzma2_prior_decoded_length ~sat+= args.dst.count_since(mark: dmark)
if status.is_ok() { if status.is_ok() {
break break
} }
+2 -2
View File
@@ -52,8 +52,8 @@ OK. 15a2a343 test/3pdata/xzsuite/good-1-check-crc32.xz
OK. 15a2a343 test/3pdata/xzsuite/good-1-check-crc64.xz OK. 15a2a343 test/3pdata/xzsuite/good-1-check-crc64.xz
OK. 15a2a343 test/3pdata/xzsuite/good-1-check-none.xz OK. 15a2a343 test/3pdata/xzsuite/good-1-check-none.xz
OK. 15a2a343 test/3pdata/xzsuite/good-1-check-sha256.xz OK. 15a2a343 test/3pdata/xzsuite/good-1-check-sha256.xz
BAD 7759aecd test/3pdata/xzsuite/good-1-lzma2-1.xz OK. db4847fb test/3pdata/xzsuite/good-1-lzma2-1.xz
BAD 7759aecd test/3pdata/xzsuite/good-1-lzma2-2.xz OK. db4847fb test/3pdata/xzsuite/good-1-lzma2-2.xz
BAD cb518f93 test/3pdata/xzsuite/good-1-lzma2-3.xz BAD cb518f93 test/3pdata/xzsuite/good-1-lzma2-3.xz
OK. db4847fb test/3pdata/xzsuite/good-1-lzma2-4.xz OK. db4847fb test/3pdata/xzsuite/good-1-lzma2-4.xz
OK. 00000000 test/3pdata/xzsuite/good-1-lzma2-5.xz OK. 00000000 test/3pdata/xzsuite/good-1-lzma2-5.xz