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