mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
Actually copy from the flate decoder's history.
This commit is contained in:
1 parent
4357c22e91
commit
c99fdaf63d
11 files changed
+289
-120
No files matched your search
@@ -212,6 +212,7 @@ pri func decoder.decode_uncompressed?(dst writer1, src reader1)() {
|
||||
var n u32 = in.dst.copy_from_reader32(r:in.src, length:length)
|
||||
// TODO: assert n <= length and simplify the subsequent if.
|
||||
if length == n {
|
||||
length = 0
|
||||
break
|
||||
} else if length < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
@@ -365,31 +366,71 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
|
||||
// Copy from this.history.
|
||||
if (distance as u64) > in.dst.length() {
|
||||
// Set (n, d) to be the length-distance pair to copy from
|
||||
// this.history, and (length, distance) to be the remaining
|
||||
// length-distance pair to copy from in.dst.
|
||||
var d u32[..0x7FFF] = ((distance as u64) - in.dst.length()) as u32
|
||||
if length > d {
|
||||
length -= d
|
||||
n = d
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from in.dst.
|
||||
var hlen u32[..0x7FFF]
|
||||
var hdist u32[..0x7FFF] = ((distance as u64) - in.dst.length()) as u32
|
||||
if length > hdist {
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
length = 0
|
||||
n = length
|
||||
hlen = length
|
||||
}
|
||||
if this.history_index < d {
|
||||
if this.history_index < hdist {
|
||||
return error "bad distance"
|
||||
}
|
||||
// Re-purpose the hdist variable as the this.history index to
|
||||
// start copying from.
|
||||
hdist = (this.history_index - hdist) & 0x7FFF
|
||||
|
||||
var history[] u8 = this.history[(this.history_index - d) & 0x7FFF:]
|
||||
if (n as u64) <= history.length() {
|
||||
// TODO: in.dst.write?(x:history.prefix(up_to:n as u64))
|
||||
} else {
|
||||
n = ((n as u64) - history.length()) as u32
|
||||
// TODO: in.dst.write?(x:history)
|
||||
// TODO: in.dst.write?(x:this.history[:n & 0x7FFF])
|
||||
// Copy from hdist to the end of this.history.
|
||||
while true {
|
||||
n = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
if hlen == n {
|
||||
hlen = 0
|
||||
break
|
||||
} else if hlen < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if n > 0 {
|
||||
hlen -= n
|
||||
// TODO: a "~+" tilde-operator that ignores overflow.
|
||||
hdist = (hdist + (n & 0x7FFF)) & 0x7FFF
|
||||
if hdist == 0 {
|
||||
// Wrap around the this.history ringbuffer.
|
||||
break
|
||||
}
|
||||
}
|
||||
// TODO: "closed for writes" instead?
|
||||
return suspension "short write"
|
||||
// TODO: this "assert false" should be a compiler error.
|
||||
// This might mean differentiating "return" and "raise".
|
||||
//
|
||||
// TODO: invalidate this-related facts because the
|
||||
// suspension can change the state of this and its fields.
|
||||
assert false
|
||||
}
|
||||
// Copy from the start of this.history, if we wrapped around.
|
||||
if hlen > 0 {
|
||||
while true {
|
||||
n = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
if hlen == n {
|
||||
hlen = 0
|
||||
break
|
||||
} else if hlen < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
hlen -= n
|
||||
hdist = (hdist + (n & 0x7FFF)) & 0x7FFF
|
||||
// TODO: "closed for writes" instead?
|
||||
return suspension "short write"
|
||||
}
|
||||
}
|
||||
|
||||
if length == 0 {
|
||||
// No need to copy from in.dst.
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user