mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Rename u32, io_reader, etc to base.u32, etc
This commit is contained in:
1 parent
fee97df698
commit
f95239f6e0
24 files changed
+394
-392
No files matched your search
@@ -16,7 +16,7 @@ packageid "crc3"
|
||||
|
||||
// TODO: drop the '?' but still generate wuffs_crc32__ieee__initialize?
|
||||
pub struct ieee?(
|
||||
state u32,
|
||||
state base.u32,
|
||||
)
|
||||
|
||||
// TODO: add a ! as this function is impure.
|
||||
@@ -24,11 +24,11 @@ pub struct ieee?(
|
||||
// TODO: should it be ? instead of ! because it's public, and might need a way
|
||||
// to signal "initializer not called"? Should the return type, in the generated
|
||||
// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"?
|
||||
pub func ieee.update(x slice u8)(checksum u32) {
|
||||
var s u32 = 0xFFFFFFFF ^ this.state
|
||||
pub func ieee.update(x slice base.u8)(checksum base.u32) {
|
||||
var s base.u32 = 0xFFFFFFFF ^ this.state
|
||||
|
||||
iterate[1](p ptr u8:in.x) {
|
||||
s = ieee_table[((s & 0xFF) as u8) ^ deref p] ^ (s >> 8)
|
||||
iterate[1](p ptr base.u8:in.x) {
|
||||
s = ieee_table[((s & 0xFF) as base.u8) ^ deref p] ^ (s >> 8)
|
||||
}
|
||||
|
||||
this.state = 0xFFFFFFFF ^ s
|
||||
@@ -37,7 +37,7 @@ pub func ieee.update(x slice u8)(checksum u32) {
|
||||
|
||||
// The table below was created by script/print-crc32-magic-numbers.go.
|
||||
|
||||
pri const ieee_table array[256] u32 = $(
|
||||
pri const ieee_table array[256] base.u32 = $(
|
||||
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
|
||||
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
|
||||
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
|
||||
|
||||
@@ -13,12 +13,12 @@
|
||||
// limitations under the License.
|
||||
|
||||
// code_order is defined in the RFC section 3.2.7.
|
||||
pri const code_order array[19] u8[..18] = $(
|
||||
pri const code_order array[19] base.u8[..18] = $(
|
||||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
|
||||
)
|
||||
|
||||
// reverse8 reverses the bits in a byte.
|
||||
pri const reverse8 array[256] u8 = $(
|
||||
pri const reverse8 array[256] base.u8 = $(
|
||||
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
|
||||
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
|
||||
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
|
||||
|
||||
@@ -41,14 +41,14 @@ pri error "internal error: inconsistent n_bits"
|
||||
//
|
||||
// Some trailing elements are 0x08000000. Bit 27 indicates an invalid value.
|
||||
|
||||
pri const lcode_magic_numbers array[32] u32 = $(
|
||||
pri const lcode_magic_numbers array[32] base.u32 = $(
|
||||
0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, 0x40000800, 0x40000900, 0x40000A00,
|
||||
0x40000B10, 0x40000D10, 0x40000F10, 0x40001110, 0x40001320, 0x40001720, 0x40001B20, 0x40001F20,
|
||||
0x40002330, 0x40002B30, 0x40003330, 0x40003B30, 0x40004340, 0x40005340, 0x40006340, 0x40007340,
|
||||
0x40008350, 0x4000A350, 0x4000C350, 0x4000E350, 0x40010200, 0x08000000, 0x08000000, 0x08000000,
|
||||
)
|
||||
|
||||
pri const dcode_magic_numbers array[32] u32 = $(
|
||||
pri const dcode_magic_numbers array[32] base.u32 = $(
|
||||
0x40000000, 0x40000100, 0x40000200, 0x40000300, 0x40000410, 0x40000610, 0x40000820, 0x40000C20,
|
||||
0x40001030, 0x40001830, 0x40002040, 0x40003040, 0x40004050, 0x40006050, 0x40008060, 0x4000C060,
|
||||
0x40010070, 0x40018070, 0x40020080, 0x40030080, 0x40040090, 0x40060090, 0x400800A0, 0x400C00A0,
|
||||
@@ -57,8 +57,8 @@ pri const dcode_magic_numbers array[32] u32 = $(
|
||||
|
||||
pub struct decoder?(
|
||||
// These fields yield src's bits in Least Significant Bits order.
|
||||
bits u32,
|
||||
n_bits u32,
|
||||
bits base.u32,
|
||||
n_bits base.u32,
|
||||
|
||||
// huffs and n_huffs_bits are the lookup tables for Huffman decodings.
|
||||
//
|
||||
@@ -93,8 +93,8 @@ pub struct decoder?(
|
||||
//
|
||||
// TODO: replace the magic "big enough" 1234 with something more
|
||||
// principled, perhaps discovered via an exhaustive search.
|
||||
huffs array[2] array[1234] u32,
|
||||
n_huffs_bits array[2] u32[..9],
|
||||
huffs array[2] array[1234] base.u32,
|
||||
n_huffs_bits array[2] base.u32[..9],
|
||||
|
||||
// history holds up to the last 32KiB of decoded output, if the decoding
|
||||
// was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE)
|
||||
@@ -107,31 +107,31 @@ pub struct decoder?(
|
||||
//
|
||||
// history[history_index & 0x7FFF] is where the next byte of decoded output
|
||||
// will be written.
|
||||
history array[0x8000] u8, // 32 KiB.
|
||||
history_index u32,
|
||||
history array[0x8000] base.u8, // 32 KiB.
|
||||
history_index base.u32,
|
||||
|
||||
// code_lengths is used to pass out-of-band data to init_huff.
|
||||
//
|
||||
// code_lengths[in.n_codes0 + i] holds the number of bits in the i'th code.
|
||||
code_lengths array[320] u8[..15],
|
||||
code_lengths array[320] base.u8[..15],
|
||||
|
||||
// end_of_block is whether decode_huffman_xxx saw an end-of-block code.
|
||||
//
|
||||
// TODO: can decode_huffman_xxx signal this in band instead of out of band?
|
||||
end_of_block bool,
|
||||
end_of_block base.bool,
|
||||
)
|
||||
|
||||
pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
|
||||
while true {
|
||||
in.dst.mark()
|
||||
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
|
||||
var z base.status = try this.decode_blocks?(dst:in.dst, src:in.src)
|
||||
if not z.is_suspension() {
|
||||
return z
|
||||
}
|
||||
// TODO: should "since_mark" be "since_mark!", as the return value lets
|
||||
// you modify the state of in.dst, so future mutations (via the slice)
|
||||
// can change the veracity of any in.dst assertions?
|
||||
var written slice u8 = in.dst.since_mark()
|
||||
var written slice base.u8 = in.dst.since_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
|
||||
@@ -143,7 +143,7 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
} else {
|
||||
// Otherwise, append written to the history ringbuffer starting at
|
||||
// the previous history_index (modulo 0x8000).
|
||||
var n_copied u64 = this.history[this.history_index & 0x7FFF:].copy_from_slice(s:written)
|
||||
var n_copied base.u64 = 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(),
|
||||
@@ -159,31 +159,31 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
// 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 u32) + 0x8000
|
||||
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
|
||||
} else {
|
||||
// We didn't need to wrap around.
|
||||
var already_full u32[..0x8000]
|
||||
var already_full base.u32[..0x8000]
|
||||
if this.history_index >= 0x8000 {
|
||||
already_full = 0x8000
|
||||
}
|
||||
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as u32) + already_full
|
||||
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
|
||||
}
|
||||
}
|
||||
yield z
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_blocks?(dst io_writer, src io_reader)() {
|
||||
var final u32
|
||||
pri func decoder.decode_blocks?(dst base.io_writer, src base.io_reader)() {
|
||||
var final base.u32
|
||||
while final == 0 {
|
||||
while this.n_bits < 3,
|
||||
post this.n_bits >= 3,
|
||||
{
|
||||
this.bits |= (in.src.read_u8?() as u32) << this.n_bits
|
||||
this.bits |= (in.src.read_u8?() as base.u32) << this.n_bits
|
||||
this.n_bits += 8
|
||||
}
|
||||
final = this.bits & 0x01
|
||||
var type u32 = (this.bits >> 1) & 0x03
|
||||
var type base.u32 = (this.bits >> 1) & 0x03
|
||||
this.bits >>= 3
|
||||
this.n_bits -= 3
|
||||
|
||||
@@ -213,7 +213,7 @@ pri func decoder.decode_blocks?(dst io_writer, src io_reader)() {
|
||||
|
||||
// decode_uncompressed decodes an uncompresed block as per the RFC section
|
||||
// 3.2.4.
|
||||
pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
|
||||
pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader)() {
|
||||
// TODO: make this "if" into a function invariant?
|
||||
//
|
||||
// Ditto for decode_huffman_slow and decode_huffman_fast.
|
||||
@@ -223,13 +223,13 @@ pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
|
||||
this.n_bits = 0
|
||||
this.bits = 0
|
||||
|
||||
var length u32 = in.src.read_u32le?()
|
||||
var length base.u32 = in.src.read_u32le?()
|
||||
if (length.low_bits(n:16) + length.high_bits(n:16)) != 0xFFFF {
|
||||
return error "inconsistent stored block length"
|
||||
}
|
||||
length = length.low_bits(n:16)
|
||||
while true {
|
||||
var n_copied u32 = in.dst.copy_from_reader32(r:in.src, length:length)
|
||||
var n_copied base.u32 = in.dst.copy_from_reader32(r:in.src, length:length)
|
||||
if length <= n_copied {
|
||||
length = 0
|
||||
break
|
||||
@@ -246,7 +246,7 @@ pri func decoder.decode_uncompressed?(dst io_writer, src io_reader)() {
|
||||
|
||||
// init_fixed_huffman initializes this.huffs as per the RFC section 3.2.6.
|
||||
pri func decoder.init_fixed_huffman?()() {
|
||||
var i u32
|
||||
var i base.u32
|
||||
while i < 144 {
|
||||
this.code_lengths[i] = 8
|
||||
i += 1
|
||||
@@ -272,41 +272,41 @@ pri func decoder.init_fixed_huffman?()() {
|
||||
}
|
||||
|
||||
// init_dynamic_huffman initializes this.huffs as per the RFC section 3.2.7.
|
||||
pri func decoder.init_dynamic_huffman?(src io_reader)() {
|
||||
var bits u32 = this.bits
|
||||
var n_bits u32 = this.n_bits
|
||||
pri func decoder.init_dynamic_huffman?(src base.io_reader)() {
|
||||
var bits base.u32 = this.bits
|
||||
var n_bits base.u32 = this.n_bits
|
||||
while n_bits < 14,
|
||||
post n_bits >= 14,
|
||||
{
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
var n_lit u32[257..288] = bits.low_bits(n:5) + 257
|
||||
var n_lit base.u32[257..288] = bits.low_bits(n:5) + 257
|
||||
if n_lit > 286 {
|
||||
return error "bad literal/length code count"
|
||||
}
|
||||
bits >>= 5
|
||||
var n_dist u32[1..32] = bits.low_bits(n:5) + 1
|
||||
var n_dist base.u32[1..32] = bits.low_bits(n:5) + 1
|
||||
if n_dist > 30 {
|
||||
return error "bad distance code count"
|
||||
}
|
||||
bits >>= 5
|
||||
var n_clen u32[4..19] = bits.low_bits(n:4) + 4
|
||||
var n_clen base.u32[4..19] = bits.low_bits(n:4) + 4
|
||||
bits >>= 4
|
||||
n_bits -= 14
|
||||
|
||||
// Read the clcode Huffman table: H-CL.
|
||||
var i u32
|
||||
var i base.u32
|
||||
while i < n_clen {
|
||||
while n_bits < 3,
|
||||
inv i < n_clen,
|
||||
post n_bits >= 3,
|
||||
{
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
assert i < 19 via "a < b: a < c; c <= b"(c:n_clen)
|
||||
this.code_lengths[code_order[i]] = (bits & 0x07) as u8
|
||||
this.code_lengths[code_order[i]] = (bits & 0x07) as base.u8
|
||||
bits >>= 3
|
||||
n_bits -= 3
|
||||
i += 1
|
||||
@@ -318,25 +318,25 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
|
||||
this.init_huff?(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF)
|
||||
|
||||
// Decode the code lengths for the next two Huffman tables.
|
||||
var mask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
|
||||
var mask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
i = 0
|
||||
while i < (n_lit + n_dist) {
|
||||
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0:n_lit, c0:n_dist)
|
||||
|
||||
// Decode a clcode symbol from H-CL.
|
||||
var table_entry u32
|
||||
var table_entry base.u32
|
||||
while true,
|
||||
inv i < 320,
|
||||
{
|
||||
table_entry = this.huffs[0][bits & mask]
|
||||
var table_entry_n_bits u32[..15] = table_entry & 15
|
||||
var table_entry_n_bits base.u32[..15] = table_entry & 15
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
// For H-CL, there should be no redirections and all symbols should be
|
||||
@@ -348,15 +348,15 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
|
||||
|
||||
// Write a literal code length.
|
||||
if table_entry < 16 {
|
||||
this.code_lengths[i] = table_entry as u8
|
||||
this.code_lengths[i] = table_entry as base.u8
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
|
||||
// Write a repeated code length.
|
||||
var n_extra_bits u32[..7]
|
||||
var rep_symbol u8[..15]
|
||||
var rep_count u32
|
||||
var n_extra_bits base.u32[..7]
|
||||
var rep_symbol base.u8[..15]
|
||||
var rep_count base.u32
|
||||
if table_entry == 16 {
|
||||
n_extra_bits = 2
|
||||
if i <= 0 {
|
||||
@@ -384,7 +384,7 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
|
||||
post n_bits >= n_extra_bits,
|
||||
{
|
||||
assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
rep_count += bits.low_bits(n:n_extra_bits)
|
||||
@@ -422,7 +422,7 @@ pri func decoder.init_dynamic_huffman?(src io_reader)() {
|
||||
// TODO: skip generating WUFFS_COROUTINE_SUSPENSION_POINTs for *callers* of
|
||||
// this function, as it can only return errors, not suspensions.
|
||||
|
||||
pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..320], base_symbol u32)() {
|
||||
pri func decoder.init_huff?(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32)() {
|
||||
// For the clcode example in this package's README.md:
|
||||
// - n_codes0 = 0
|
||||
// - n_codes1 = 19
|
||||
@@ -456,8 +456,8 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// - counts[4] = 1
|
||||
// - counts[5] = 2
|
||||
// - all other counts elements are 0.
|
||||
var counts array[16] u16[..320]
|
||||
var i u32 = in.n_codes0
|
||||
var counts array[16] base.u16[..320]
|
||||
var i base.u32 = in.n_codes0
|
||||
while i < in.n_codes1 {
|
||||
assert i < 320 via "a < b: a < c; c <= b"(c:in.n_codes1)
|
||||
// TODO: this if should be unnecessary. Have some way to assert that,
|
||||
@@ -468,12 +468,12 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
counts[this.code_lengths[i]] += 1
|
||||
i += 1
|
||||
}
|
||||
if ((counts[0] as u32) + in.n_codes0) == in.n_codes1 {
|
||||
if ((counts[0] as base.u32) + in.n_codes0) == in.n_codes1 {
|
||||
return error "no Huffman codes"
|
||||
}
|
||||
|
||||
// Check that the Huffman code completely covers all possible input bits.
|
||||
var remaining u32 = 1 // There is 1 possible 0-bit code.
|
||||
var remaining base.u32 = 1 // There is 1 possible 0-bit code.
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
if remaining > (1 << 30) {
|
||||
@@ -481,10 +481,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
}
|
||||
// Each iteration doubles the number of possible remaining codes.
|
||||
remaining <<= 1
|
||||
if remaining < (counts[i] as u32) {
|
||||
if remaining < (counts[i] as base.u32) {
|
||||
return error "bad Huffman code (over-subscribed)"
|
||||
}
|
||||
remaining -= counts[i] as u32
|
||||
remaining -= counts[i] as base.u32
|
||||
i += 1
|
||||
}
|
||||
if remaining != 0 {
|
||||
@@ -504,12 +504,12 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// - offsets[6] = 10
|
||||
// - all other offsets elements are 10.
|
||||
// - n_symbols = 10
|
||||
var offsets array[16] u16[..320]
|
||||
var n_symbols u32[..320]
|
||||
var offsets array[16] base.u16[..320]
|
||||
var n_symbols base.u32[..320]
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
offsets[i] = n_symbols as u16
|
||||
var count u32[..320] = counts[i] as u32
|
||||
offsets[i] = n_symbols as base.u16
|
||||
var count base.u32[..320] = counts[i] as base.u32
|
||||
if n_symbols > (320 - count) {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
@@ -541,7 +541,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// - offsets[3] = 7, formerly 0
|
||||
// - offsets[4] = 8, formerly 7
|
||||
// - offsets[5] = 10, formerly 8
|
||||
var symbols array[320] u16[..319]
|
||||
var symbols array[320] base.u16[..319]
|
||||
i = in.n_codes0
|
||||
while i < in.n_codes1,
|
||||
inv n_symbols <= 288,
|
||||
@@ -555,7 +555,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
if offsets[this.code_lengths[i]] >= 320 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as u16
|
||||
symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as base.u16
|
||||
offsets[this.code_lengths[i]] += 1
|
||||
}
|
||||
i += 1
|
||||
@@ -566,7 +566,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// For the clcode example in this package's README.md:
|
||||
// - min_cl = 3
|
||||
// - max_cl = 5
|
||||
var min_cl u32[1..9] = 1
|
||||
var min_cl base.u32[1..9] = 1
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
@@ -578,7 +578,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
}
|
||||
min_cl += 1
|
||||
}
|
||||
var max_cl u32[1..15] = 15
|
||||
var max_cl base.u32[1..15] = 15
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
@@ -611,33 +611,33 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// - this.huffs[0][0b01111] = 0x80000305 (literal, symbols[8]=0x03, code_length=5)
|
||||
// - this.huffs[0][0b11111] = 0x80001805 (literal, symbols[9]=0x18, code_length=5)
|
||||
i = 0
|
||||
if (n_symbols != (offsets[max_cl] as u32)) or (n_symbols != (offsets[15] as u32)) {
|
||||
if (n_symbols != (offsets[max_cl] as base.u32)) or (n_symbols != (offsets[15] as base.u32)) {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (in.n_codes0 + (symbols[0] as u32)) >= 320 {
|
||||
if (in.n_codes0 + (symbols[0] as base.u32)) >= 320 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
var initial_high_bits u32 = 1 << 9
|
||||
var initial_high_bits base.u32 = 1 << 9
|
||||
if max_cl < 9 {
|
||||
initial_high_bits = (1 as u32) << max_cl
|
||||
initial_high_bits = (1 as base.u32) << max_cl
|
||||
}
|
||||
var prev_cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[0] as u32)] as u32
|
||||
var prev_redirect_key u32 = 0xFFFFFFFF
|
||||
var top u32[..1234]
|
||||
var next_top u32[..1234] = 512
|
||||
var code u32
|
||||
var key u32
|
||||
var value u32
|
||||
var prev_cl base.u32[..15] = this.code_lengths[in.n_codes0 + (symbols[0] as base.u32)] as base.u32
|
||||
var prev_redirect_key base.u32 = 0xFFFFFFFF
|
||||
var top base.u32[..1234]
|
||||
var next_top base.u32[..1234] = 512
|
||||
var code base.u32
|
||||
var key base.u32
|
||||
var value base.u32
|
||||
while true,
|
||||
pre code < (1 << 15),
|
||||
pre i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if (in.n_codes0 + (symbols[i] as u32)) >= 320 {
|
||||
if (in.n_codes0 + (symbols[i] as base.u32)) >= 320 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
var cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[i] as u32)] as u32
|
||||
var cl base.u32[..15] = this.code_lengths[in.n_codes0 + (symbols[i] as base.u32)] as base.u32
|
||||
if cl > prev_cl {
|
||||
code <<= cl - prev_cl
|
||||
if code >= (1 << 15) {
|
||||
@@ -651,11 +651,11 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
key = code
|
||||
if cl > 9 {
|
||||
// TODO: we shouldn't need a temporary variable.
|
||||
var tmp u32[..6] = cl - 9
|
||||
var tmp base.u32[..6] = cl - 9
|
||||
cl = tmp
|
||||
assert cl <= 9 via "a <= b: a == c; c <= b"(c:tmp)
|
||||
|
||||
var redirect_key u32[..511] = (key >> tmp) & 511
|
||||
var redirect_key base.u32[..511] = (key >> tmp) & 511
|
||||
key = key.low_bits(n:tmp)
|
||||
if prev_redirect_key != redirect_key {
|
||||
prev_redirect_key = redirect_key
|
||||
@@ -663,18 +663,18 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// Calculate the number of bits needed for the 2nd level table.
|
||||
// This computation is similar to "check that the Huffman code
|
||||
// completely covers all possible input bits" above.
|
||||
remaining = (1 as u32) << cl
|
||||
var j u32 = prev_cl
|
||||
remaining = (1 as base.u32) << cl
|
||||
var j base.u32 = prev_cl
|
||||
while j <= 15,
|
||||
inv cl <= 9,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if remaining <= (counts[j] as u32) {
|
||||
if remaining <= (counts[j] as base.u32) {
|
||||
break
|
||||
}
|
||||
remaining -= counts[j] as u32
|
||||
remaining -= counts[j] as base.u32
|
||||
if remaining > (1 << 30) {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
@@ -685,15 +685,15 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
tmp = j - 9
|
||||
initial_high_bits = (1 as u32) << tmp
|
||||
initial_high_bits = (1 as base.u32) << tmp
|
||||
|
||||
top = next_top
|
||||
if (top + ((1 as u32) << tmp)) > 1234 {
|
||||
if (top + ((1 as base.u32) << tmp)) > 1234 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
next_top = top + ((1 as u32) << tmp)
|
||||
next_top = top + ((1 as base.u32) << tmp)
|
||||
|
||||
redirect_key = (reverse8[redirect_key >> 1] as u32) | ((redirect_key & 1) << 8)
|
||||
redirect_key = (reverse8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8)
|
||||
this.huffs[in.which][redirect_key] = 0x10000009 | (top << 8) | (tmp << 4)
|
||||
}
|
||||
}
|
||||
@@ -702,10 +702,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
}
|
||||
counts[prev_cl] -= 1
|
||||
|
||||
var reversed_key u32[..511] = (reverse8[key >> 1] as u32) | ((key & 1) << 8)
|
||||
var reversed_key base.u32[..511] = (reverse8[key >> 1] as base.u32) | ((key & 1) << 8)
|
||||
reversed_key >>= 9 - cl
|
||||
|
||||
var symbol u32[..319] = symbols[i] as u32
|
||||
var symbol base.u32[..319] = symbols[i] as base.u32
|
||||
if symbol == 256 {
|
||||
// End-of-block.
|
||||
value = 0x20000000 | cl
|
||||
@@ -727,8 +727,8 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// The table uses log2(initial_high_bits) bits, but reversed_key only
|
||||
// has cl bits. We duplicate the key-value pair across all possible
|
||||
// values of the high (log2(initial_high_bits) - cl) bits.
|
||||
var high_bits u32 = initial_high_bits
|
||||
var delta u32 = (1 as u32) << cl
|
||||
var high_bits base.u32 = initial_high_bits
|
||||
var delta base.u32 = (1 as base.u32) << cl
|
||||
while high_bits >= delta,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
// TODO: describe how the xxx_fast version differs from the xxx_slow one, the
|
||||
// assumptions that xxx_fast makes, and how that makes it fast.
|
||||
pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
pri func decoder.decode_huffman_fast?(dst base.io_writer, src base.io_reader)() {
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
@@ -29,13 +29,13 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
return error "internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
var bits u32 = this.bits
|
||||
var n_bits u32 = this.n_bits
|
||||
var bits base.u32 = this.bits
|
||||
var n_bits base.u32 = this.n_bits
|
||||
|
||||
var table_entry u32
|
||||
var table_entry_n_bits u32[..15]
|
||||
var lmask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
|
||||
var dmask u32[..511] = ((1 as u32) << this.n_huffs_bits[1]) - 1
|
||||
var table_entry base.u32
|
||||
var table_entry_n_bits base.u32[..15]
|
||||
var lmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
var dmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (12 bytes) and write (258 bytes) as much as we need to. Doing
|
||||
@@ -70,9 +70,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
@@ -92,7 +92,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -107,9 +107,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
@@ -119,8 +119,8 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
assert in.src.available() >= 8
|
||||
assert n_bits >= 15
|
||||
|
||||
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
||||
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
var redir_top base.u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
||||
var redir_mask base.u32[..0x7FFF] = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
if (redir_top + (bits & redir_mask)) >= 1234 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
@@ -131,7 +131,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -157,15 +157,15 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
|
||||
// length = base_number + extra_bits.
|
||||
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
@@ -189,9 +189,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
@@ -212,9 +212,9 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
@@ -225,7 +225,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
assert n_bits >= 15
|
||||
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
if (redir_top + (bits & redir_mask)) >= 1234 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
@@ -251,15 +251,15 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
}
|
||||
@@ -271,7 +271,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
|
||||
var n_copied u32
|
||||
var n_copied base.u32
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fast and
|
||||
// decode_huffman_slow.
|
||||
@@ -281,13 +281,13 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
inv in.dst.is_marked(),
|
||||
{
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
|
||||
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
|
||||
// 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 =
|
||||
(((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32
|
||||
var hlen base.u32[..0x7FFF]
|
||||
var hdist base.u32 =
|
||||
(((dist_minus_1 + 1) as base.u64) - in.dst.since_mark().length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
|
||||
@@ -342,20 +342,20 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
continue:loop
|
||||
}
|
||||
|
||||
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
|
||||
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
|
||||
return error "internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert length <= 258
|
||||
assert ((dist_minus_1 + 1) as u64) <= in.dst.since_mark().length()
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= in.dst.since_mark().length()
|
||||
assert in.dst.available() >= 258
|
||||
assert in.dst.is_marked()
|
||||
|
||||
// We can therefore prove:
|
||||
assert (dist_minus_1 + 1) > 0
|
||||
assert (length as u64) <= 258
|
||||
assert (length as u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258)
|
||||
assert (length as base.u64) <= 258
|
||||
assert (length as base.u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258)
|
||||
|
||||
// Copy from in.dst.
|
||||
in.dst.copy_from_history32(distance:(dist_minus_1 + 1), length:length)
|
||||
@@ -383,7 +383,7 @@ pri func decoder.decode_huffman_fast?(dst io_writer, src io_reader)() {
|
||||
in.src.unread_u8?()
|
||||
}
|
||||
|
||||
this.bits = bits & (((1 as u32) << n_bits) - 1)
|
||||
this.bits = bits & (((1 as base.u32) << n_bits) - 1)
|
||||
this.n_bits = n_bits
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader)() {
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_fast function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
@@ -22,13 +22,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
return error "internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
var bits u32 = this.bits
|
||||
var n_bits u32 = this.n_bits
|
||||
var bits base.u32 = this.bits
|
||||
var n_bits base.u32 = this.n_bits
|
||||
|
||||
var table_entry u32
|
||||
var table_entry_n_bits u32[..15]
|
||||
var lmask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
|
||||
var dmask u32[..511] = ((1 as u32) << this.n_huffs_bits[1]) - 1
|
||||
var table_entry base.u32
|
||||
var table_entry_n_bits base.u32[..15]
|
||||
var lmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
var dmask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
while:loop true {
|
||||
// Decode an lcode symbol from H-L.
|
||||
@@ -41,13 +41,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -57,8 +57,8 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
break:loop
|
||||
} else if (table_entry >> 28) != 0 {
|
||||
// Redirect.
|
||||
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
||||
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
var redir_top base.u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
||||
var redir_mask base.u32[..0x7FFF] = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
if (redir_top + (bits & redir_mask)) >= 1234 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
@@ -71,13 +71,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
|
||||
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -100,14 +100,14 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
|
||||
// length = base_number + extra_bits.
|
||||
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
||||
@@ -125,13 +125,13 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
if (redir_top + (bits & redir_mask)) >= 1234 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
@@ -144,7 +144,7 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
}
|
||||
@@ -163,14 +163,14 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
||||
@@ -178,16 +178,16 @@ pri func decoder.decode_huffman_slow?(dst io_writer, src io_reader)() {
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
|
||||
var n_copied u32
|
||||
var n_copied base.u32
|
||||
while true {
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() {
|
||||
if ((dist_minus_1 + 1) as base.u64) > in.dst.since_mark().length() {
|
||||
// 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 =
|
||||
(((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32
|
||||
var hlen base.u32[..0x7FFF]
|
||||
var hdist base.u32 =
|
||||
(((dist_minus_1 + 1) as base.u64) - in.dst.since_mark().length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
|
||||
|
||||
+56
-56
@@ -22,45 +22,45 @@ pub error "bad LZW literal width"
|
||||
pri error "internal error: inconsistent limited read"
|
||||
|
||||
pub struct decoder?(
|
||||
width u32[..65535],
|
||||
height u32[..65535],
|
||||
width base.u32[..65535],
|
||||
height base.u32[..65535],
|
||||
|
||||
// Call sequence state transitions:
|
||||
// - 0 -> 1: via decode_config.
|
||||
// - 1 -> 1: via decode_frame.
|
||||
call_sequence u8,
|
||||
call_sequence base.u8,
|
||||
|
||||
background_color_index u8,
|
||||
background_color_index base.u8,
|
||||
|
||||
block_type u8,
|
||||
peek_block_type bool,
|
||||
block_type base.u8,
|
||||
peek_block_type base.bool,
|
||||
|
||||
have_gct bool,
|
||||
have_lct bool,
|
||||
have_gct base.bool,
|
||||
have_lct base.bool,
|
||||
|
||||
interlace bool,
|
||||
interlace base.bool,
|
||||
|
||||
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
|
||||
// animation loops is 1.
|
||||
seen_num_loops bool,
|
||||
num_loops u32 = 1,
|
||||
seen_num_loops base.bool,
|
||||
num_loops base.u32 = 1,
|
||||
|
||||
frame_top u32[..65535],
|
||||
frame_left u32[..65535],
|
||||
frame_width u32[..65535],
|
||||
frame_height u32[..65535],
|
||||
frame_top base.u32[..65535],
|
||||
frame_left base.u32[..65535],
|
||||
frame_width base.u32[..65535],
|
||||
frame_height base.u32[..65535],
|
||||
|
||||
// gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries.
|
||||
//
|
||||
// TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB?
|
||||
gct array[3 * 256] u8,
|
||||
lct array[3 * 256] u8,
|
||||
gct array[3 * 256] base.u8,
|
||||
lct array[3 * 256] base.u8,
|
||||
|
||||
lzw lzw_decoder,
|
||||
)
|
||||
|
||||
// TODO: should dst be an nptr instead of a ptr?
|
||||
pub func decoder.decode_config?(dst ptr image_config, src io_reader)() {
|
||||
pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() {
|
||||
if this.call_sequence >= 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
@@ -85,7 +85,7 @@ pub func decoder.decode_config?(dst ptr image_config, src io_reader)() {
|
||||
this.call_sequence = 1
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
|
||||
pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() {
|
||||
if this.call_sequence < 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
@@ -121,9 +121,9 @@ pub func decoder.decode_frame?(dst io_writer, src io_reader)() {
|
||||
// decode_header reads either "GIF87a" or "GIF89a".
|
||||
//
|
||||
// See the spec section 17 "Header" on page 7.
|
||||
pri func decoder.decode_header?(src io_reader)() {
|
||||
var c array[6] u8
|
||||
var i u32
|
||||
pri func decoder.decode_header?(src base.io_reader)() {
|
||||
var c array[6] base.u8
|
||||
var i base.u32
|
||||
while i < 6 {
|
||||
c[i] = in.src.read_u8?()
|
||||
i += 1
|
||||
@@ -137,22 +137,22 @@ pri func decoder.decode_header?(src io_reader)() {
|
||||
// decode_lsd reads the Logical Screen Descriptor.
|
||||
//
|
||||
// See the spec section 18 "Logical Screen Descriptor" on page 8.
|
||||
pri func decoder.decode_lsd?(src io_reader)() {
|
||||
var c array[7] u8
|
||||
var i u32
|
||||
pri func decoder.decode_lsd?(src base.io_reader)() {
|
||||
var c array[7] base.u8
|
||||
var i base.u32
|
||||
while i < 7 {
|
||||
c[i] = in.src.read_u8?()
|
||||
i += 1
|
||||
}
|
||||
// TODO: use read_u16le.
|
||||
this.width = (c[0] as u32) | ((c[1] as u32) << 8)
|
||||
this.height = (c[2] as u32) | ((c[3] as u32) << 8)
|
||||
this.width = (c[0] as base.u32) | ((c[1] as base.u32) << 8)
|
||||
this.height = (c[2] as base.u32) | ((c[3] as base.u32) << 8)
|
||||
this.background_color_index = c[5]
|
||||
|
||||
// Read the optional Global Color Table.
|
||||
this.have_gct = (c[4] & 0x80) != 0
|
||||
if this.have_gct {
|
||||
var gct_size u32[..256] = (1 as u32) << (1 + (c[4] & 0x07))
|
||||
var gct_size base.u32[..256] = (1 as base.u32) << (1 + (c[4] & 0x07))
|
||||
i = 0
|
||||
while i < gct_size {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c:gct_size)
|
||||
@@ -172,8 +172,8 @@ pri func decoder.decode_lsd?(src io_reader)() {
|
||||
// - section 24 "Comment Extension" on page 17.
|
||||
// - section 25 "Plain Text Extension" on page 18.
|
||||
// - section 26 "Application Extension" on page 21.
|
||||
pri func decoder.decode_extension?(src io_reader)() {
|
||||
var label u8 = in.src.read_u8?()
|
||||
pri func decoder.decode_extension?(src base.io_reader)() {
|
||||
var label base.u8 = in.src.read_u8?()
|
||||
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
// TODO.
|
||||
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
|
||||
@@ -185,33 +185,33 @@ pri func decoder.decode_extension?(src io_reader)() {
|
||||
this.skip_blocks?(src:in.src)
|
||||
}
|
||||
|
||||
pri func decoder.skip_blocks?(src io_reader)() {
|
||||
pri func decoder.skip_blocks?(src base.io_reader)() {
|
||||
while true {
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
var block_size base.u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
}
|
||||
}
|
||||
|
||||
// animexts1dot0 is "ANIMEXTS1.0" as bytes.
|
||||
pri const animexts1dot0 array[11] u8 = $(
|
||||
pri const animexts1dot0 array[11] base.u8 = $(
|
||||
0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30,
|
||||
)
|
||||
|
||||
// netscape2dot0 is "NETSCAPE2.0" as bytes.
|
||||
pri const netscape2dot0 array[11] u8 = $(
|
||||
pri const netscape2dot0 array[11] base.u8 = $(
|
||||
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
|
||||
)
|
||||
|
||||
// decode_ae reads an Application Extension.
|
||||
pri func decoder.decode_ae?(src io_reader)() {
|
||||
pri func decoder.decode_ae?(src base.io_reader)() {
|
||||
// This "while true" always executes exactly once, as it ends with a
|
||||
// "break", but using "break"s throughout simplifies the control flow.
|
||||
while true {
|
||||
var c u8
|
||||
var block_size u8 = in.src.read_u8?()
|
||||
var c base.u8
|
||||
var block_size base.u8 = in.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
@@ -221,11 +221,11 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
|
||||
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
|
||||
if block_size != 11 {
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
var not_animexts bool
|
||||
var not_netscape bool
|
||||
var not_animexts base.bool
|
||||
var not_netscape base.bool
|
||||
block_size = 0 // Re-purpose the block_size variable as a counter.
|
||||
while block_size < 11 {
|
||||
c = in.src.read_u8?()
|
||||
@@ -241,7 +241,7 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// count.
|
||||
block_size = in.src.read_u8?()
|
||||
if block_size != 3 {
|
||||
in.src.skip32?(n:block_size as u32)
|
||||
in.src.skip32?(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
c = in.src.read_u8?()
|
||||
@@ -249,7 +249,7 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
in.src.skip32?(n:2)
|
||||
break
|
||||
}
|
||||
this.num_loops = in.src.read_u16le?() as u32
|
||||
this.num_loops = in.src.read_u16le?() as base.u32
|
||||
this.seen_num_loops = true
|
||||
|
||||
// A loop count of N, in the wire format, actually means "repeat N
|
||||
@@ -275,22 +275,22 @@ pri func decoder.decode_ae?(src io_reader)() {
|
||||
// been read.
|
||||
//
|
||||
// See the spec section 20 "Image Descriptor" on page 11.
|
||||
pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() {
|
||||
// TODO: check that the frame rect is inside the image rect??
|
||||
this.frame_left = in.src.read_u16le?() as u32
|
||||
this.frame_top = in.src.read_u16le?() as u32
|
||||
this.frame_width = in.src.read_u16le?() as u32
|
||||
this.frame_height = in.src.read_u16le?() as u32
|
||||
this.frame_left = in.src.read_u16le?() as base.u32
|
||||
this.frame_top = in.src.read_u16le?() as base.u32
|
||||
this.frame_width = in.src.read_u16le?() as base.u32
|
||||
this.frame_height = in.src.read_u16le?() as base.u32
|
||||
|
||||
// TODO: use this.interlace.
|
||||
var flags u8 = in.src.read_u8?()
|
||||
var flags base.u8 = in.src.read_u8?()
|
||||
this.interlace = (flags & 0x40) != 0
|
||||
|
||||
// Read the optional Local Color Table.
|
||||
this.have_lct = (flags & 0x80) != 0
|
||||
if this.have_lct {
|
||||
var lct_size u32[..256] = (1 as u32) << (1 + (flags & 0x07))
|
||||
var i u32
|
||||
var lct_size base.u32[..256] = (1 as base.u32) << (1 + (flags & 0x07))
|
||||
var i base.u32
|
||||
while i < lct_size {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c:lct_size)
|
||||
this.lct[(3 * i) + 0] = in.src.read_u8?()
|
||||
@@ -300,19 +300,19 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
}
|
||||
|
||||
var lw u8 = in.src.read_u8?()
|
||||
var lw base.u8 = in.src.read_u8?()
|
||||
if (lw < 2) or (8 < lw) {
|
||||
return error "bad LZW literal width"
|
||||
}
|
||||
this.lzw.set_literal_width!(lw:lw as u32)
|
||||
this.lzw.set_literal_width!(lw:lw as base.u32)
|
||||
|
||||
while true {
|
||||
var block_size u64 = in.src.read_u8?() as u64
|
||||
var block_size base.u64 = in.src.read_u8?() as base.u64
|
||||
if block_size == 0 {
|
||||
break
|
||||
}
|
||||
while true {
|
||||
var r io_reader = in.src
|
||||
var r base.io_reader = in.src
|
||||
// TODO: should "mark" be "set_mark"? Unlike "limit", "mark" does
|
||||
// not return a different io_reader.
|
||||
r.mark()
|
||||
@@ -321,7 +321,7 @@ pri func decoder.decode_id?(dst io_writer, src io_reader)() {
|
||||
// variables.
|
||||
//
|
||||
// TODO: enforce that limit can only be called in a "foo?" call?
|
||||
var z status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
|
||||
var z base.status = try this.lzw.decode?(dst:in.dst, src:r.limit(l:block_size), dummy:in.src)
|
||||
if z.is_ok() {
|
||||
break
|
||||
}
|
||||
|
||||
+29
-29
@@ -18,33 +18,33 @@ pub error "LZW code is out of range"
|
||||
pub error "LZW prefix chain is cyclical"
|
||||
|
||||
pri struct lzw_decoder?(
|
||||
literal_width u32[2..8] = 8,
|
||||
stack array[4096] u8,
|
||||
suffixes array[4096] u8,
|
||||
prefixes array[4096] u16[..4095],
|
||||
literal_width base.u32[2..8] = 8,
|
||||
stack array[4096] base.u8,
|
||||
suffixes array[4096] base.u8,
|
||||
prefixes array[4096] base.u16[..4095],
|
||||
)
|
||||
|
||||
pri func lzw_decoder.set_literal_width!(lw u32[2..8] = 8)() {
|
||||
pri func lzw_decoder.set_literal_width!(lw base.u32[2..8] = 8)() {
|
||||
this.literal_width = in.lw
|
||||
}
|
||||
|
||||
pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
|
||||
// These variables don't change over the lifetime of this func.
|
||||
var clear_code u32[4..256] = (1 as u32) << this.literal_width
|
||||
var end_code u32[5..257] = clear_code + 1
|
||||
var clear_code base.u32[4..256] = (1 as base.u32) << this.literal_width
|
||||
var end_code base.u32[5..257] = clear_code + 1
|
||||
|
||||
// These variables do change.
|
||||
//
|
||||
// save_code is the code for which, after decoding a code, we save what the
|
||||
// next back-reference expands to. The README.md file also calls this value
|
||||
// `max`. 4096 means do not save.
|
||||
var save_code u32[..4096] = end_code
|
||||
var prev_code u32[..4095]
|
||||
var width u32[..12] = this.literal_width + 1
|
||||
var save_code base.u32[..4096] = end_code
|
||||
var prev_code base.u32[..4095]
|
||||
var width base.u32[..12] = this.literal_width + 1
|
||||
|
||||
// These variables yield src's bits in Least Significant Bits order.
|
||||
var bits u32
|
||||
var n_bits u32
|
||||
var bits base.u32
|
||||
var n_bits base.u32
|
||||
|
||||
while true,
|
||||
pre n_bits < 8,
|
||||
@@ -55,19 +55,19 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
post n_bits >= width,
|
||||
{
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
bits |= (in.src.read_u8?() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
var code u32[..4095] = bits.low_bits(n:width)
|
||||
var code base.u32[..4095] = bits.low_bits(n:width)
|
||||
bits >>= width
|
||||
n_bits -= width
|
||||
|
||||
if code < clear_code {
|
||||
assert code < 256 via "a < b: a < c; c <= b"(c:clear_code)
|
||||
in.dst.write_u8?(x:code as u8)
|
||||
in.dst.write_u8?(x:code as base.u8)
|
||||
if save_code <= 4095 {
|
||||
this.suffixes[save_code] = code as u8
|
||||
this.prefixes[save_code] = prev_code as u16
|
||||
this.suffixes[save_code] = code as base.u8
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
}
|
||||
|
||||
} else if code == clear_code {
|
||||
@@ -80,8 +80,8 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
return
|
||||
|
||||
} else if code <= save_code {
|
||||
var s u32[..4095] = 4095
|
||||
var c u32[..4095] = code
|
||||
var s base.u32[..4095] = 4095
|
||||
var c base.u32[..4095] = code
|
||||
|
||||
if code == save_code {
|
||||
s -= 1
|
||||
@@ -97,31 +97,31 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
return error "LZW prefix chain is cyclical"
|
||||
}
|
||||
s -= 1
|
||||
c = this.prefixes[c] as u32
|
||||
c = this.prefixes[c] as base.u32
|
||||
}
|
||||
this.stack[s] = c as u8
|
||||
this.stack[s] = c as base.u8
|
||||
|
||||
if code == save_code {
|
||||
this.stack[4095] = c as u8
|
||||
this.stack[4095] = c as base.u8
|
||||
}
|
||||
|
||||
while true,
|
||||
inv n_bits < 8,
|
||||
inv c < 256,
|
||||
{
|
||||
var expansion slice u8 = this.stack[s:]
|
||||
var n_copied u64 = in.dst.copy_from_slice(s:expansion)
|
||||
var expansion slice base.u8 = this.stack[s:]
|
||||
var n_copied base.u64 = in.dst.copy_from_slice(s:expansion)
|
||||
if n_copied == expansion.length() {
|
||||
break
|
||||
}
|
||||
s = (s + ((n_copied & 4095) as u32)) & 4095
|
||||
s = (s + ((n_copied & 4095) as base.u32)) & 4095
|
||||
// TODO: "closed for writes" instead?
|
||||
yield suspension "short write"
|
||||
}
|
||||
|
||||
if save_code <= 4095 {
|
||||
this.suffixes[save_code] = c as u8
|
||||
this.prefixes[save_code] = prev_code as u16
|
||||
this.suffixes[save_code] = c as base.u8
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -130,7 +130,7 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() {
|
||||
|
||||
if save_code <= 4095 {
|
||||
save_code += 1
|
||||
if (save_code == ((1 as u32) << width)) and (width < 12) {
|
||||
if (save_code == ((1 as base.u32) << width)) and (width < 12) {
|
||||
width += 1
|
||||
}
|
||||
}
|
||||
|
||||
+13
-13
@@ -25,14 +25,14 @@ pub error "invalid gzip encoding flags"
|
||||
pub struct decoder?(
|
||||
flate deflate.decoder,
|
||||
checksum crc32.ieee,
|
||||
ignore_checksum bool,
|
||||
ignore_checksum base.bool,
|
||||
)
|
||||
|
||||
pub func decoder.set_ignore_checksum!(ic bool)() {
|
||||
pub func decoder.set_ignore_checksum!(ic base.bool)() {
|
||||
this.ignore_checksum = in.ic
|
||||
}
|
||||
|
||||
pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
|
||||
// Read the header.
|
||||
if in.src.read_u8?() != 0x1F {
|
||||
return error "bad gzip header"
|
||||
@@ -43,15 +43,15 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
if in.src.read_u8?() != 0x08 {
|
||||
return error "invalid gzip compression method"
|
||||
}
|
||||
var flags u8 = in.src.read_u8?()
|
||||
var flags base.u8 = in.src.read_u8?()
|
||||
// TODO: API for returning the header's MTIME field.
|
||||
in.src.skip32?(n:6)
|
||||
var c u8
|
||||
var c base.u8
|
||||
|
||||
// Handle FEXTRA.
|
||||
if (flags & 0x04) != 0 {
|
||||
var xlen u16 = in.src.read_u16le?()
|
||||
in.src.skip32?(n:xlen as u32)
|
||||
var xlen base.u16 = in.src.read_u16le?()
|
||||
in.src.skip32?(n:xlen as base.u32)
|
||||
}
|
||||
|
||||
// Handle FNAME.
|
||||
@@ -89,23 +89,23 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
var checksum_got u32
|
||||
var decoded_length_got u32
|
||||
var checksum_got base.u32
|
||||
var decoded_length_got base.u32
|
||||
while true {
|
||||
in.dst.mark()
|
||||
var z status = try this.flate.decode?(dst:in.dst, src:in.src)
|
||||
var z base.status = try this.flate.decode?(dst:in.dst, src:in.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update(x:in.dst.since_mark())
|
||||
decoded_length_got = decoded_length_got ~+
|
||||
((in.dst.since_mark().length() & 0xFFFFFFFF) as u32)
|
||||
((in.dst.since_mark().length() & 0xFFFFFFFF) as base.u32)
|
||||
}
|
||||
if z.is_ok() {
|
||||
break
|
||||
}
|
||||
yield z
|
||||
}
|
||||
var checksum_want u32 = in.src.read_u32le?()
|
||||
var decoded_length_want u32 = in.src.read_u32le?()
|
||||
var checksum_want base.u32 = in.src.read_u32le?()
|
||||
var decoded_length_want base.u32 = in.src.read_u32le?()
|
||||
if (not this.ignore_checksum) and
|
||||
((checksum_got != checksum_want) or (decoded_length_got != decoded_length_want)) {
|
||||
return error "checksum mismatch"
|
||||
|
||||
+14
-14
@@ -26,15 +26,15 @@ pub error "TODO: unsupported zlib preset dictionary"
|
||||
pub struct decoder?(
|
||||
flate deflate.decoder,
|
||||
checksum adler32,
|
||||
ignore_checksum bool,
|
||||
ignore_checksum base.bool,
|
||||
)
|
||||
|
||||
pub func decoder.set_ignore_checksum!(ic bool)() {
|
||||
pub func decoder.set_ignore_checksum!(ic base.bool)() {
|
||||
this.ignore_checksum = in.ic
|
||||
}
|
||||
|
||||
pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
var x u16 = in.src.read_u16be?()
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
|
||||
var x base.u16 = in.src.read_u16be?()
|
||||
if ((x >> 8) & 0x0F) != 0x08 {
|
||||
return error "invalid zlib compression method"
|
||||
}
|
||||
@@ -49,10 +49,10 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
var checksum_got u32
|
||||
var checksum_got base.u32
|
||||
while true {
|
||||
in.dst.mark()
|
||||
var z status = try this.flate.decode?(dst:in.dst, src:in.src)
|
||||
var z base.status = try this.flate.decode?(dst:in.dst, src:in.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update(x:in.dst.since_mark())
|
||||
}
|
||||
@@ -61,7 +61,7 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
}
|
||||
yield z
|
||||
}
|
||||
var checksum_want u32 = in.src.read_u32be?()
|
||||
var checksum_want base.u32 = in.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_got != checksum_want) {
|
||||
return error "checksum mismatch"
|
||||
}
|
||||
@@ -69,26 +69,26 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
|
||||
|
||||
// TODO: drop the '?' but still generate wuffs_zlib__adler32__initialize?
|
||||
pri struct adler32?(
|
||||
state u32 = 1,
|
||||
state base.u32 = 1,
|
||||
)
|
||||
|
||||
// TODO: add a ! as this function is impure.
|
||||
pri func adler32.update(x slice u8)(checksum u32) {
|
||||
pri func adler32.update(x slice base.u8)(checksum base.u32) {
|
||||
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
|
||||
// RFC 1950.
|
||||
|
||||
var s1 u32 = this.state.low_bits(n:16)
|
||||
var s2 u32 = this.state.high_bits(n:16)
|
||||
var s1 base.u32 = this.state.low_bits(n:16)
|
||||
var s2 base.u32 = this.state.high_bits(n:16)
|
||||
|
||||
while in.x.length() > 0 {
|
||||
var remaining slice u8
|
||||
var remaining slice base.u8
|
||||
if in.x.length() > 5552 {
|
||||
remaining = in.x[5552:]
|
||||
in.x = in.x[:5552]
|
||||
}
|
||||
|
||||
iterate[8](p ptr u8:in.x) {
|
||||
s1 ~+= deref p as u32
|
||||
iterate[8](p ptr base.u8:in.x) {
|
||||
s1 ~+= deref p as base.u32
|
||||
s2 ~+= s1
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user