From be3b9b81e7f3e97c4ebe675d91651ad728070b6e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 27 Jul 2017 14:56:23 +1000 Subject: [PATCH] Generate the lcode and dcode Huffman tables. --- gen/c/std/flate.c | 19 +++++++++++++++++-- std/flate/decode_flate.puffs | 15 +++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 5d535d04..369c6bc3 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -915,6 +915,17 @@ puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self, status = PUFFS_FLATE_ERROR_MISSING_END_OF_BLOCK_CODE; goto exit; } + PUFFS_COROUTINE_SUSPENSION_POINT(6); + status = puffs_flate_decoder_init_huff(self, 0, 0, v_n_lit); + if (status) { + goto suspend; + } + PUFFS_COROUTINE_SUSPENSION_POINT(7); + status = + puffs_flate_decoder_init_huff(self, 1, v_n_lit, (v_n_lit + v_n_dist)); + if (status) { + goto suspend; + } self->private_impl.f_bits = v_bits; self->private_impl.f_n_bits = v_n_bits; self->private_impl.c_init_huffs[0].coro_susp_point = 0; @@ -1079,7 +1090,7 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, v_prev_cl = ((uint32_t)( self->private_impl.f_code_lengths[self->private_impl.f_symbols[0]])); v_code = 0; - while (v_i < v_n_symbols) { + while (true) { v_cl = ((uint32_t)(self->private_impl .f_code_lengths[self->private_impl.f_symbols[v_i]])); @@ -1108,6 +1119,10 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, self->private_impl .f_huffs[a_which][(v_high_bits | v_reversed_code) & 511] = v_value; } + v_i += 1; + if (v_i >= v_n_symbols) { + goto label_1_break; + } v_code += 1; if (v_code >= 512) { status = @@ -1115,8 +1130,8 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, goto exit; } v_prev_cl = v_cl; - v_i += 1; } + label_1_break:; self->private_impl.f_n_huffs_bits[a_which] = v_max_cl; self->private_impl.c_init_huff[0].coro_susp_point = 0; goto exit; diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index b077de49..3328aaa6 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -266,6 +266,9 @@ pri func decoder.init_huffs?(src reader1)() { return error "missing end-of-block code" } + this.init_huff?(which:0, n_codes0:0, n_codes1:n_lit) + this.init_huff?(which:1, n_codes0:n_lit, n_codes1:n_lit + n_dist) + this.bits = bits this.n_bits = n_bits } @@ -408,11 +411,11 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..289], n_codes1 u32[.. var n_symbols u32[..289] = offsets[max_cl] as u32 var prev_cl u32[..15] = this.code_lengths[this.symbols[0]] as u32 var code u32 - while i < n_symbols, - inv code < (1 << 9), + while true, + pre code < (1 << 9), + pre i < 289, inv max_cl <= 9, { - assert i < 289 via "a < b: a < c; c <= b"(c:n_symbols) var cl u32[..15] = this.code_lengths[this.symbols[i]] as u32 if cl > max_cl { return error "internal error: inconsistent Huffman decoder state" @@ -445,12 +448,16 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..289], n_codes1 u32[.. this.huffs[in.which][(high_bits | reversed_code) & 511] = value } + i += 1 + if i >= n_symbols { + break + } + assert i < 289 via "a < b: a < c; c <= b"(c:n_symbols) code += 1 if code >= (1 << 9) { return error "internal error: inconsistent Huffman decoder state" } prev_cl = cl - i += 1 } this.n_huffs_bits[in.which] = max_cl