diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 88fb2c83..f475c94a 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -152,8 +152,6 @@ typedef struct { uint32_t f_huffs[2][512]; uint32_t f_n_huffs_bits[2]; uint8_t f_code_lengths[320]; - uint16_t f_counts[16]; - uint16_t f_symbols[320]; struct { uint32_t coro_susp_point; @@ -196,11 +194,13 @@ typedef struct { } c_init_dynamic_huffman[1]; struct { uint32_t coro_susp_point; + uint16_t v_counts[16]; uint32_t v_i; uint32_t v_remaining; uint16_t v_offsets[16]; uint32_t v_n_symbols; uint32_t v_count; + uint16_t v_symbols[320]; uint32_t v_max_cl; uint32_t v_prev_cl; uint32_t v_code; @@ -1341,11 +1341,13 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, uint32_t a_base_symbol) { puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + uint16_t v_counts[16]; uint32_t v_i; uint32_t v_remaining; uint16_t v_offsets[16]; uint32_t v_n_symbols; uint32_t v_count; + uint16_t v_symbols[320]; uint32_t v_max_cl; uint32_t v_prev_cl; uint32_t v_code; @@ -1358,11 +1360,13 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, uint32_t coro_susp_point = self->private_impl.c_init_huff[0].coro_susp_point; if (coro_susp_point) { + memcpy(v_counts, self->private_impl.c_init_huff[0].v_counts, 2 * 16); v_i = self->private_impl.c_init_huff[0].v_i; v_remaining = self->private_impl.c_init_huff[0].v_remaining; memcpy(v_offsets, self->private_impl.c_init_huff[0].v_offsets, 2 * 16); v_n_symbols = self->private_impl.c_init_huff[0].v_n_symbols; v_count = self->private_impl.c_init_huff[0].v_count; + memcpy(v_symbols, self->private_impl.c_init_huff[0].v_symbols, 2 * 320); v_max_cl = self->private_impl.c_init_huff[0].v_max_cl; v_prev_cl = self->private_impl.c_init_huff[0].v_prev_cl; v_code = self->private_impl.c_init_huff[0].v_code; @@ -1376,24 +1380,23 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); - v_i = 0; - while (v_i < 16) { - self->private_impl.f_counts[v_i] = 0; - v_i += 1; - } + { + size_t i; + for (i = 0; i < 16; i++) { + v_counts[i] = 0; + } + }; v_i = a_n_codes0; while (v_i < a_n_codes1) { - if (self->private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >= - 320) { + if (v_counts[self->private_impl.f_code_lengths[v_i]] >= 320) { status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } - self->private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] += 1; + v_counts[self->private_impl.f_code_lengths[v_i]] += 1; v_i += 1; } - if ((((uint32_t)(self->private_impl.f_counts[0])) + a_n_codes0) == - a_n_codes1) { + if ((((uint32_t)(v_counts[0])) + a_n_codes0) == a_n_codes1) { status = PUFFS_FLATE_ERROR_NO_HUFFMAN_CODES; goto exit; } @@ -1406,11 +1409,11 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, goto exit; } v_remaining <<= 1; - if (v_remaining < ((uint32_t)(self->private_impl.f_counts[v_i]))) { + if (v_remaining < ((uint32_t)(v_counts[v_i]))) { status = PUFFS_FLATE_ERROR_BAD_HUFFMAN_CODE_OVER_SUBSCRIBED; goto exit; } - v_remaining -= ((uint32_t)(self->private_impl.f_counts[v_i])); + v_remaining -= ((uint32_t)(v_counts[v_i])); v_i += 1; } if (v_remaining != 0) { @@ -1427,7 +1430,7 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, v_i = 1; while (v_i <= 15) { v_offsets[v_i] = ((uint16_t)(v_n_symbols)); - v_count = ((uint32_t)(self->private_impl.f_counts[v_i])); + v_count = ((uint32_t)(v_counts[v_i])); if (v_n_symbols > (320 - v_count)) { status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; @@ -1441,6 +1444,12 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } + { + size_t i; + for (i = 0; i < 320; i++) { + v_symbols[i] = 0; + } + }; v_i = a_n_codes0; while (v_i < a_n_codes1) { if (v_i < a_n_codes0) { @@ -1454,8 +1463,7 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } - self->private_impl - .f_symbols[v_offsets[self->private_impl.f_code_lengths[v_i]]] = + v_symbols[v_offsets[self->private_impl.f_code_lengths[v_i]]] = ((uint16_t)((v_i - a_n_codes0))); v_offsets[self->private_impl.f_code_lengths[v_i]] += 1; } @@ -1463,7 +1471,7 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, } v_max_cl = 15; while (true) { - if (self->private_impl.f_counts[v_max_cl] != 0) { + if (v_counts[v_max_cl] != 0) { goto label_0_break; } if (v_max_cl <= 1) { @@ -1484,26 +1492,24 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } - if ((a_n_codes0 + ((uint32_t)(self->private_impl.f_symbols[0]))) >= 320) { + if ((a_n_codes0 + ((uint32_t)(v_symbols[0]))) >= 320) { status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } v_prev_cl = ((uint32_t)( self->private_impl - .f_code_lengths[a_n_codes0 + - ((uint32_t)(self->private_impl.f_symbols[0]))])); + .f_code_lengths[a_n_codes0 + ((uint32_t)(v_symbols[0]))])); v_code = 0; while (true) { - if ((a_n_codes0 + ((uint32_t)(self->private_impl.f_symbols[v_i]))) >= - 320) { + if ((a_n_codes0 + ((uint32_t)(v_symbols[v_i]))) >= 320) { status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } v_cl = ((uint32_t)( - self->private_impl.f_code_lengths - [a_n_codes0 + ((uint32_t)(self->private_impl.f_symbols[v_i]))])); + self->private_impl + .f_code_lengths[a_n_codes0 + ((uint32_t)(v_symbols[v_i]))])); if (v_cl > v_max_cl) { status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; @@ -1521,7 +1527,7 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, ((v_code & 1) << 8)); v_reversed_code >>= (9 - v_cl); v_value = 0; - v_symbol = ((uint32_t)(self->private_impl.f_symbols[v_i])); + v_symbol = ((uint32_t)(v_symbols[v_i])); if (v_symbol == 256) { v_value = (268435456 | v_cl); } else if ((v_symbol < 256) && (a_which == 0)) { @@ -1566,11 +1572,13 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, goto suspend; suspend: self->private_impl.c_init_huff[0].coro_susp_point = coro_susp_point; + memcpy(self->private_impl.c_init_huff[0].v_counts, v_counts, 2 * 16); self->private_impl.c_init_huff[0].v_i = v_i; self->private_impl.c_init_huff[0].v_remaining = v_remaining; memcpy(self->private_impl.c_init_huff[0].v_offsets, v_offsets, 2 * 16); self->private_impl.c_init_huff[0].v_n_symbols = v_n_symbols; self->private_impl.c_init_huff[0].v_count = v_count; + memcpy(self->private_impl.c_init_huff[0].v_symbols, v_symbols, 2 * 320); self->private_impl.c_init_huff[0].v_max_cl = v_max_cl; self->private_impl.c_init_huff[0].v_prev_cl = v_prev_cl; self->private_impl.c_init_huff[0].v_code = v_code; diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 951f8911..fef1f851 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -152,8 +152,6 @@ typedef struct { uint32_t f_huffs[2][512]; uint32_t f_n_huffs_bits[2]; uint8_t f_code_lengths[320]; - uint16_t f_counts[16]; - uint16_t f_symbols[320]; struct { uint32_t coro_susp_point; @@ -196,11 +194,13 @@ typedef struct { } c_init_dynamic_huffman[1]; struct { uint32_t coro_susp_point; + uint16_t v_counts[16]; uint32_t v_i; uint32_t v_remaining; uint16_t v_offsets[16]; uint32_t v_n_symbols; uint32_t v_count; + uint16_t v_symbols[320]; uint32_t v_max_cl; uint32_t v_prev_cl; uint32_t v_code; diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index d50bd2f5..2d4be0a4 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -85,12 +85,6 @@ pub struct decoder?( // // code_lengths[in.n_codes0 + i] holds the number of bits in the i'th code. code_lengths[320] u8[..15], - - // These fields are used as init_huff scratch space. - // - // TODO: make these local variables, not fields. - counts[16] u16[..320], - symbols[320] u16[..319], ) pub func decoder.decode?(dst writer1, src reader1)() { @@ -461,23 +455,19 @@ 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 i u32 - while i < 16 { - this.counts[i] = 0 - i += 1 - } - i = in.n_codes0 + var counts[16] u16[..320] + var i 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, // for all j, counts[j] <= i, and thus counts[j]++ will not overflow. - if this.counts[this.code_lengths[i]] >= 320 { + if counts[this.code_lengths[i]] >= 320 { return error "internal error: inconsistent Huffman decoder state" } - this.counts[this.code_lengths[i]] += 1 + counts[this.code_lengths[i]] += 1 i += 1 } - if ((this.counts[0] as u32) + in.n_codes0) == in.n_codes1 { + if ((counts[0] as u32) + in.n_codes0) == in.n_codes1 { return error "no Huffman codes" } @@ -490,10 +480,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 < (this.counts[i] as u32) { + if remaining < (counts[i] as u32) { return error "bad Huffman code (over-subscribed)" } - remaining -= this.counts[i] as u32 + remaining -= counts[i] as u32 i += 1 } if remaining != 0 { @@ -518,7 +508,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. i = 1 while i <= 15 { offsets[i] = n_symbols as u16 - var count u32[..320] = this.counts[i] as u32 + var count u32[..320] = counts[i] as u32 if n_symbols > (320 - count) { return error "internal error: inconsistent Huffman decoder state" } @@ -550,6 +540,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[320] u16[..319] i = in.n_codes0 while i < in.n_codes1, inv n_symbols <= 288, @@ -563,7 +554,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" } - this.symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as u16 + symbols[offsets[this.code_lengths[i]]] = (i - in.n_codes0) as u16 offsets[this.code_lengths[i]] += 1 } i += 1 @@ -577,7 +568,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. while true, inv n_symbols <= 288, { - if this.counts[max_cl] != 0 { + if counts[max_cl] != 0 { break } if max_cl <= 1 { @@ -607,10 +598,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. if (n_symbols != (offsets[max_cl] as u32)) or (n_symbols != (offsets[15] as u32)) { return error "internal error: inconsistent Huffman decoder state" } - if (in.n_codes0 + (this.symbols[0] as u32)) >= 320 { + if (in.n_codes0 + (symbols[0] as u32)) >= 320 { return error "internal error: inconsistent Huffman decoder state" } - var prev_cl u32[..15] = this.code_lengths[in.n_codes0 + (this.symbols[0] as u32)] as u32 + var prev_cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[0] as u32)] as u32 var code u32 while true, pre code < (1 << 9), @@ -618,10 +609,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. inv max_cl <= 9, inv n_symbols <= 288, { - if (in.n_codes0 + (this.symbols[i] as u32)) >= 320 { + if (in.n_codes0 + (symbols[i] as u32)) >= 320 { return error "internal error: inconsistent Huffman decoder state" } - var cl u32[..15] = this.code_lengths[in.n_codes0 + (this.symbols[i] as u32)] as u32 + var cl u32[..15] = this.code_lengths[in.n_codes0 + (symbols[i] as u32)] as u32 if cl > max_cl { return error "internal error: inconsistent Huffman decoder state" } @@ -637,7 +628,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. reversed_code >>= 9 - cl var value u32 - var symbol u32[..319] = this.symbols[i] as u32 + var symbol u32[..319] = symbols[i] as u32 if symbol == 256 { // End-of-block. value = 0x10000000 | cl