Factor out deflate.decoder.add_history

This commit is contained in:
Nigel Tao committed 2019-09-28 10:32:12 +10:00
1 parent 344efe7926
commit 498e1f407d
2 files changed
+105 -77

No files matched your search

+46 -43
View File
@@ -132,6 +132,49 @@ pub struct decoder?(
code_lengths array[320] base.u8,
)
pub func decoder.add_history!(hist slice base.u8) {
var s slice base.u8
var n_copied base.u64
var already_full base.u32[..0x8000]
s = args.hist
if s.length() >= 0x8000 {
// If s is longer than the ringbuffer, we can ignore the previous value
// of history_index, as we will overwrite the whole ringbuffer.
s = s.suffix(up_to:0x8000)
this.history[:].copy_from_slice!(s:s)
this.history_index = 0x8000
} else {
// Otherwise, append s to the history ringbuffer starting at the
// previous history_index (modulo 0x8000).
n_copied = this.history[this.history_index & 0x7FFF:].copy_from_slice!(s:s)
if n_copied < s.length() {
// a_slice.copy_from(s:b_slice) returns the minimum of the two
// slice lengths. If that value is less than b_slice.length(), then
// not all of b_slice was copied.
//
// In terms of the history ringbuffer, that means that we have to
// wrap around and copy the remainder of s over the start of the
// history ringbuffer.
s = s[n_copied:]
n_copied = this.history[:].copy_from_slice!(s:s)
// Set history_index (modulo 0x8000) to the length of this
// remainder. The &0x7FFF is redundant, but proves to the compiler
// that the conversion to u32 will not overflow. The +0x8000 is to
// maintain that the history ringbuffer is full if and only if
// history_index >= 0x8000.
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
} else {
// We didn't need to wrap around.
already_full = 0
if this.history_index >= 0x8000 {
already_full = 0x8000
}
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
}
}
}
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,
@@ -139,11 +182,8 @@ pub func decoder.workbuf_len() base.range_ii_u64 {
}
pub func decoder.decode_io_writer?(dst base.io_writer, src base.io_reader, workbuf slice base.u8) {
var mark base.u64
var status base.status
var written slice base.u8
var n_copied base.u64
var already_full base.u32[..0x8000]
var mark base.u64
var status base.status
while true {
mark = args.dst.mark()
@@ -154,44 +194,7 @@ pub func decoder.decode_io_writer?(dst base.io_writer, src base.io_reader, workb
// TODO: should "since" be "since!", as the return value lets you
// modify the state of args.dst, so future mutations (via the slice)
// can change the veracity of any args.dst assertions?
written = args.dst.since(mark:mark)
// Append written, the decoded output, to the history ringbuffer.
if written.length() >= 0x8000 {
// If written is longer than the ringbuffer, we can ignore the
// previous value of history_index, as we will overwrite the whole
// ringbuffer.
written = written.suffix(up_to:0x8000)
this.history[:].copy_from_slice!(s:written)
this.history_index = 0x8000
} else {
// Otherwise, append written to the history ringbuffer starting at
// the previous history_index (modulo 0x8000).
n_copied = this.history[this.history_index & 0x7FFF:].copy_from_slice!(s:written)
if n_copied < written.length() {
// a_slice.copy_from(s:b_slice) returns the minimum of the two
// slice lengths. If that value is less than b_slice.length(),
// then not all of b_slice was copied.
//
// In terms of the history ringbuffer, that means that we have
// to wrap around and copy the remainder of written over the
// start of the history ringbuffer.
written = written[n_copied:]
n_copied = this.history[:].copy_from_slice!(s:written)
// Set history_index (modulo 0x8000) to the length of this
// remainder. The &0x7FFF is redundant, but proves to the
// compiler that the conversion to u32 will not overflow. The
// +0x8000 is to maintain that the history ringbuffer is full
// if and only if history_index >= 0x8000.
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
} else {
// We didn't need to wrap around.
already_full = 0
if this.history_index >= 0x8000 {
already_full = 0x8000
}
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
}
}
this.add_history!(hist:args.dst.since(mark:mark))
yield? status
}
}