mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 01:01:35 +01:00
Refactor std/flate's decoder.decode_dynamic.
This commit is contained in:
1 parent
f7fb991619
commit
74400247f2
6 files changed
+143
-92
No files matched your search
+10
-2
@@ -969,8 +969,16 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
|||||||
}
|
}
|
||||||
g.writes("if (status) { goto suspend; }\n")
|
g.writes("if (status) { goto suspend; }\n")
|
||||||
|
|
||||||
} else if isThisMethod(g.tm, n, "init_huffman", 2) {
|
} else if isThisMethod(g.tm, n, "init_huffs", 1) {
|
||||||
g.printf("status = %s%s_init_huffman(self,",
|
g.printf("status = %s%s_init_huffs(self, %ssrc);\n",
|
||||||
|
g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm), aPrefix)
|
||||||
|
if err := g.writeLoadExprDerivedVars(n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g.writes("if (status) { goto suspend; }\n")
|
||||||
|
|
||||||
|
} else if isThisMethod(g.tm, n, "init_huff", 2) {
|
||||||
|
g.printf("status = %s%s_init_huff(self,",
|
||||||
g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm))
|
g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm))
|
||||||
if err := g.writeExpr(n.Args()[0].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
|
if err := g.writeExpr(n.Args()[0].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+90
-60
@@ -108,7 +108,7 @@ typedef int32_t puffs_flate_status;
|
|||||||
-1157040126 // 0xbb08f802
|
-1157040126 // 0xbb08f802
|
||||||
#define PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH \
|
#define PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH \
|
||||||
-1157040125 // 0xbb08f803
|
-1157040125 // 0xbb08f803
|
||||||
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFFMAN_STATE \
|
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFF_STATE \
|
||||||
-1157040124 // 0xbb08f804
|
-1157040124 // 0xbb08f804
|
||||||
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS \
|
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS \
|
||||||
-1157040123 // 0xbb08f805
|
-1157040123 // 0xbb08f805
|
||||||
@@ -166,22 +166,25 @@ typedef struct {
|
|||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_n;
|
uint32_t v_n;
|
||||||
} c_decode_uncompressed[1];
|
} c_decode_uncompressed[1];
|
||||||
|
struct {
|
||||||
|
uint32_t coro_susp_point;
|
||||||
|
} c_decode_dynamic[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_bits;
|
uint32_t v_bits;
|
||||||
uint32_t v_n_bits;
|
uint32_t v_n_bits;
|
||||||
uint32_t v_hlit;
|
uint32_t v_n_lit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_n_dist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_n_clen;
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
} c_decode_dynamic[1];
|
} c_init_huffs[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
uint16_t v_offsets[16];
|
uint16_t v_offsets[16];
|
||||||
uint16_t v_total;
|
uint16_t v_total;
|
||||||
uint16_t v_count;
|
uint16_t v_count;
|
||||||
} c_init_huffman[1];
|
} c_init_huff[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_decoder;
|
} puffs_flate_decoder;
|
||||||
|
|
||||||
@@ -269,7 +272,7 @@ const char* puffs_flate_status_strings1[8] = {
|
|||||||
"flate: bad flate block",
|
"flate: bad flate block",
|
||||||
"flate: bad literal/length code count",
|
"flate: bad literal/length code count",
|
||||||
"flate: inconsistent stored block length",
|
"flate: inconsistent stored block length",
|
||||||
"flate: internal error: inconsistent init_huffman state",
|
"flate: internal error: inconsistent init_huff state",
|
||||||
"flate: internal error: inconsistent n_bits",
|
"flate: internal error: inconsistent n_bits",
|
||||||
"flate: no Huffman codes",
|
"flate: no Huffman codes",
|
||||||
"flate: TODO: fixed Huffman blocks",
|
"flate: TODO: fixed Huffman blocks",
|
||||||
@@ -311,9 +314,12 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(puffs_flate_decoder* self,
|
|||||||
puffs_base_writer1 a_dst,
|
puffs_base_writer1 a_dst,
|
||||||
puffs_base_reader1 a_src);
|
puffs_base_reader1 a_src);
|
||||||
|
|
||||||
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self,
|
||||||
uint32_t a_which,
|
puffs_base_reader1 a_src);
|
||||||
uint32_t a_n_codes);
|
|
||||||
|
puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self,
|
||||||
|
uint32_t a_which,
|
||||||
|
uint32_t a_n_codes);
|
||||||
|
|
||||||
// ---------------- Constructor and Destructor Implementations
|
// ---------------- Constructor and Destructor Implementations
|
||||||
|
|
||||||
@@ -661,11 +667,39 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
puffs_base_reader1 a_src) {
|
puffs_base_reader1 a_src) {
|
||||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||||
|
|
||||||
|
uint32_t coro_susp_point =
|
||||||
|
self->private_impl.c_decode_dynamic[0].coro_susp_point;
|
||||||
|
if (coro_susp_point) {
|
||||||
|
}
|
||||||
|
switch (coro_susp_point) {
|
||||||
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||||
|
|
||||||
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
||||||
|
status = puffs_flate_decoder_init_huffs(self, a_src);
|
||||||
|
if (status) {
|
||||||
|
goto suspend;
|
||||||
|
}
|
||||||
|
self->private_impl.c_decode_dynamic[0].coro_susp_point = 0;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
goto suspend;
|
||||||
|
suspend:
|
||||||
|
self->private_impl.c_decode_dynamic[0].coro_susp_point = coro_susp_point;
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self,
|
||||||
|
puffs_base_reader1 a_src) {
|
||||||
|
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||||
|
|
||||||
uint32_t v_bits;
|
uint32_t v_bits;
|
||||||
uint32_t v_n_bits;
|
uint32_t v_n_bits;
|
||||||
uint32_t v_hlit;
|
uint32_t v_n_lit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_n_dist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_n_clen;
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
|
|
||||||
uint8_t* b_rptr_src = NULL;
|
uint8_t* b_rptr_src = NULL;
|
||||||
@@ -682,15 +716,14 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
b_rend_src = b_rptr_src + len;
|
b_rend_src = b_rptr_src + len;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t coro_susp_point =
|
uint32_t coro_susp_point = self->private_impl.c_init_huffs[0].coro_susp_point;
|
||||||
self->private_impl.c_decode_dynamic[0].coro_susp_point;
|
|
||||||
if (coro_susp_point) {
|
if (coro_susp_point) {
|
||||||
v_bits = self->private_impl.c_decode_dynamic[0].v_bits;
|
v_bits = self->private_impl.c_init_huffs[0].v_bits;
|
||||||
v_n_bits = self->private_impl.c_decode_dynamic[0].v_n_bits;
|
v_n_bits = self->private_impl.c_init_huffs[0].v_n_bits;
|
||||||
v_hlit = self->private_impl.c_decode_dynamic[0].v_hlit;
|
v_n_lit = self->private_impl.c_init_huffs[0].v_n_lit;
|
||||||
v_hdist = self->private_impl.c_decode_dynamic[0].v_hdist;
|
v_n_dist = self->private_impl.c_init_huffs[0].v_n_dist;
|
||||||
v_hclen = self->private_impl.c_decode_dynamic[0].v_hclen;
|
v_n_clen = self->private_impl.c_init_huffs[0].v_n_clen;
|
||||||
v_i = self->private_impl.c_decode_dynamic[0].v_i;
|
v_i = self->private_impl.c_init_huffs[0].v_i;
|
||||||
}
|
}
|
||||||
switch (coro_susp_point) {
|
switch (coro_susp_point) {
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||||
@@ -706,23 +739,23 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
|
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
|
||||||
v_n_bits += 8;
|
v_n_bits += 8;
|
||||||
}
|
}
|
||||||
v_hlit = (((v_bits) & ((1 << (5)) - 1)) + 257);
|
v_n_lit = (((v_bits) & ((1 << (5)) - 1)) + 257);
|
||||||
if (v_hlit > 286) {
|
if (v_n_lit > 286) {
|
||||||
status = PUFFS_FLATE_ERROR_BAD_LITERAL_LENGTH_CODE_COUNT;
|
status = PUFFS_FLATE_ERROR_BAD_LITERAL_LENGTH_CODE_COUNT;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
v_bits >>= 5;
|
v_bits >>= 5;
|
||||||
v_hdist = (((v_bits) & ((1 << (5)) - 1)) + 1);
|
v_n_dist = (((v_bits) & ((1 << (5)) - 1)) + 1);
|
||||||
if (v_hdist > 30) {
|
if (v_n_dist > 30) {
|
||||||
status = PUFFS_FLATE_ERROR_BAD_DISTANCE_CODE_COUNT;
|
status = PUFFS_FLATE_ERROR_BAD_DISTANCE_CODE_COUNT;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
v_bits >>= 5;
|
v_bits >>= 5;
|
||||||
v_hclen = (((v_bits) & ((1 << (4)) - 1)) + 4);
|
v_n_clen = (((v_bits) & ((1 << (4)) - 1)) + 4);
|
||||||
v_bits >>= 4;
|
v_bits >>= 4;
|
||||||
v_n_bits -= 14;
|
v_n_bits -= 14;
|
||||||
v_i = 0;
|
v_i = 0;
|
||||||
while (v_i < v_hclen) {
|
while (v_i < v_n_clen) {
|
||||||
while (v_n_bits < 3) {
|
while (v_n_bits < 3) {
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
||||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||||
@@ -742,28 +775,28 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
self->private_impl.f_code_lengths[puffs_flate_code_order[v_i]] = 0;
|
self->private_impl.f_code_lengths[puffs_flate_code_order[v_i]] = 0;
|
||||||
v_i += 1;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
self->private_impl.f_bits = v_bits;
|
|
||||||
self->private_impl.f_n_bits = v_n_bits;
|
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||||
status = puffs_flate_decoder_init_huffman(self, 0, 19);
|
status = puffs_flate_decoder_init_huff(self, 0, 19);
|
||||||
if (status) {
|
if (status) {
|
||||||
goto suspend;
|
goto suspend;
|
||||||
}
|
}
|
||||||
self->private_impl.f_wip0 = v_hlit;
|
self->private_impl.f_wip0 = v_n_lit;
|
||||||
self->private_impl.f_wip1 = v_hdist;
|
self->private_impl.f_wip1 = v_n_dist;
|
||||||
self->private_impl.c_decode_dynamic[0].coro_susp_point = 0;
|
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;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
goto suspend;
|
goto suspend;
|
||||||
suspend:
|
suspend:
|
||||||
self->private_impl.c_decode_dynamic[0].coro_susp_point = coro_susp_point;
|
self->private_impl.c_init_huffs[0].coro_susp_point = coro_susp_point;
|
||||||
self->private_impl.c_decode_dynamic[0].v_bits = v_bits;
|
self->private_impl.c_init_huffs[0].v_bits = v_bits;
|
||||||
self->private_impl.c_decode_dynamic[0].v_n_bits = v_n_bits;
|
self->private_impl.c_init_huffs[0].v_n_bits = v_n_bits;
|
||||||
self->private_impl.c_decode_dynamic[0].v_hlit = v_hlit;
|
self->private_impl.c_init_huffs[0].v_n_lit = v_n_lit;
|
||||||
self->private_impl.c_decode_dynamic[0].v_hdist = v_hdist;
|
self->private_impl.c_init_huffs[0].v_n_dist = v_n_dist;
|
||||||
self->private_impl.c_decode_dynamic[0].v_hclen = v_hclen;
|
self->private_impl.c_init_huffs[0].v_n_clen = v_n_clen;
|
||||||
self->private_impl.c_decode_dynamic[0].v_i = v_i;
|
self->private_impl.c_init_huffs[0].v_i = v_i;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (a_src.buf) {
|
if (a_src.buf) {
|
||||||
@@ -791,9 +824,9 @@ short_read_src:
|
|||||||
goto suspend;
|
goto suspend;
|
||||||
}
|
}
|
||||||
|
|
||||||
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self,
|
||||||
uint32_t a_which,
|
uint32_t a_which,
|
||||||
uint32_t a_n_codes) {
|
uint32_t a_n_codes) {
|
||||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||||
|
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
@@ -801,13 +834,12 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
|||||||
uint16_t v_total;
|
uint16_t v_total;
|
||||||
uint16_t v_count;
|
uint16_t v_count;
|
||||||
|
|
||||||
uint32_t coro_susp_point =
|
uint32_t coro_susp_point = self->private_impl.c_init_huff[0].coro_susp_point;
|
||||||
self->private_impl.c_init_huffman[0].coro_susp_point;
|
|
||||||
if (coro_susp_point) {
|
if (coro_susp_point) {
|
||||||
v_i = self->private_impl.c_init_huffman[0].v_i;
|
v_i = self->private_impl.c_init_huff[0].v_i;
|
||||||
memcpy(v_offsets, self->private_impl.c_init_huffman[0].v_offsets, 2 * 16);
|
memcpy(v_offsets, self->private_impl.c_init_huff[0].v_offsets, 2 * 16);
|
||||||
v_total = self->private_impl.c_init_huffman[0].v_total;
|
v_total = self->private_impl.c_init_huff[0].v_total;
|
||||||
v_count = self->private_impl.c_init_huffman[0].v_count;
|
v_count = self->private_impl.c_init_huff[0].v_count;
|
||||||
}
|
}
|
||||||
switch (coro_susp_point) {
|
switch (coro_susp_point) {
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||||
@@ -822,8 +854,7 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
|||||||
if (self->private_impl.f_huffs[a_which]
|
if (self->private_impl.f_huffs[a_which]
|
||||||
.private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >=
|
.private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >=
|
||||||
289) {
|
289) {
|
||||||
status =
|
status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFF_STATE;
|
||||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFFMAN_STATE;
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
self->private_impl.f_huffs[a_which]
|
self->private_impl.f_huffs[a_which]
|
||||||
@@ -848,8 +879,7 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
|||||||
v_offsets[v_i] = v_total;
|
v_offsets[v_i] = v_total;
|
||||||
v_count = self->private_impl.f_huffs[a_which].private_impl.f_counts[v_i];
|
v_count = self->private_impl.f_huffs[a_which].private_impl.f_counts[v_i];
|
||||||
if (v_total > (289 - v_count)) {
|
if (v_total > (289 - v_count)) {
|
||||||
status =
|
status = PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFF_STATE;
|
||||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFFMAN_STATE;
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
v_total = (v_total + v_count);
|
v_total = (v_total + v_count);
|
||||||
@@ -864,24 +894,24 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
|||||||
((uint16_t)(v_i));
|
((uint16_t)(v_i));
|
||||||
if (v_offsets[self->private_impl.f_code_lengths[v_i]] >= 289) {
|
if (v_offsets[self->private_impl.f_code_lengths[v_i]] >= 289) {
|
||||||
status =
|
status =
|
||||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFFMAN_STATE;
|
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFF_STATE;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
v_offsets[self->private_impl.f_code_lengths[v_i]] += 1;
|
v_offsets[self->private_impl.f_code_lengths[v_i]] += 1;
|
||||||
}
|
}
|
||||||
v_i += 1;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
self->private_impl.c_init_huffman[0].coro_susp_point = 0;
|
self->private_impl.c_init_huff[0].coro_susp_point = 0;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
goto suspend;
|
goto suspend;
|
||||||
suspend:
|
suspend:
|
||||||
self->private_impl.c_init_huffman[0].coro_susp_point = coro_susp_point;
|
self->private_impl.c_init_huff[0].coro_susp_point = coro_susp_point;
|
||||||
self->private_impl.c_init_huffman[0].v_i = v_i;
|
self->private_impl.c_init_huff[0].v_i = v_i;
|
||||||
memcpy(self->private_impl.c_init_huffman[0].v_offsets, v_offsets, 2 * 16);
|
memcpy(self->private_impl.c_init_huff[0].v_offsets, v_offsets, 2 * 16);
|
||||||
self->private_impl.c_init_huffman[0].v_total = v_total;
|
self->private_impl.c_init_huff[0].v_total = v_total;
|
||||||
self->private_impl.c_init_huffman[0].v_count = v_count;
|
self->private_impl.c_init_huff[0].v_count = v_count;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
+9
-6
@@ -108,7 +108,7 @@ typedef int32_t puffs_flate_status;
|
|||||||
-1157040126 // 0xbb08f802
|
-1157040126 // 0xbb08f802
|
||||||
#define PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH \
|
#define PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH \
|
||||||
-1157040125 // 0xbb08f803
|
-1157040125 // 0xbb08f803
|
||||||
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFFMAN_STATE \
|
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_INIT_HUFF_STATE \
|
||||||
-1157040124 // 0xbb08f804
|
-1157040124 // 0xbb08f804
|
||||||
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS \
|
#define PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS \
|
||||||
-1157040123 // 0xbb08f805
|
-1157040123 // 0xbb08f805
|
||||||
@@ -166,22 +166,25 @@ typedef struct {
|
|||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_n;
|
uint32_t v_n;
|
||||||
} c_decode_uncompressed[1];
|
} c_decode_uncompressed[1];
|
||||||
|
struct {
|
||||||
|
uint32_t coro_susp_point;
|
||||||
|
} c_decode_dynamic[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_bits;
|
uint32_t v_bits;
|
||||||
uint32_t v_n_bits;
|
uint32_t v_n_bits;
|
||||||
uint32_t v_hlit;
|
uint32_t v_n_lit;
|
||||||
uint32_t v_hdist;
|
uint32_t v_n_dist;
|
||||||
uint32_t v_hclen;
|
uint32_t v_n_clen;
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
} c_decode_dynamic[1];
|
} c_init_huffs[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
uint16_t v_offsets[16];
|
uint16_t v_offsets[16];
|
||||||
uint16_t v_total;
|
uint16_t v_total;
|
||||||
uint16_t v_count;
|
uint16_t v_count;
|
||||||
} c_init_huffman[1];
|
} c_init_huff[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_decoder;
|
} puffs_flate_decoder;
|
||||||
|
|
||||||
|
|||||||
@@ -625,7 +625,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
|||||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
||||||
isThisMethod(q.tm, n, "init_huffman", 2) {
|
isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 2) {
|
||||||
|
|
||||||
for _, o := range n.Args() {
|
for _, o := range n.Args() {
|
||||||
// TODO: check that the arg range at the caller and the
|
// TODO: check that the arg range at the caller and the
|
||||||
|
|||||||
+1
-1
@@ -350,7 +350,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
||||||
isThisMethod(q.tm, n, "init_huffman", 2) {
|
isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 2) {
|
||||||
|
|
||||||
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ pub error "bad distance code count"
|
|||||||
pub error "bad flate block"
|
pub error "bad flate block"
|
||||||
pub error "bad literal/length code count"
|
pub error "bad literal/length code count"
|
||||||
pub error "inconsistent stored block length"
|
pub error "inconsistent stored block length"
|
||||||
pub error "internal error: inconsistent init_huffman state"
|
pub error "internal error: inconsistent init_huff state"
|
||||||
pub error "internal error: inconsistent n_bits"
|
pub error "internal error: inconsistent n_bits"
|
||||||
pub error "no Huffman codes"
|
pub error "no Huffman codes"
|
||||||
|
|
||||||
@@ -20,7 +20,12 @@ pub struct decoder?(
|
|||||||
bits u32,
|
bits u32,
|
||||||
n_bits u32,
|
n_bits u32,
|
||||||
|
|
||||||
|
// There are up to 2 Huffman tables active at any one time. As per this
|
||||||
|
// package's README.md:
|
||||||
|
// - huffs[0] is used for clcode and lcode.
|
||||||
|
// - huffs[1] is used for dcode.
|
||||||
huffs[2] huffman_decoder,
|
huffs[2] huffman_decoder,
|
||||||
|
|
||||||
code_lengths[289] u8[..15],
|
code_lengths[289] u8[..15],
|
||||||
|
|
||||||
// These temporary fields are used to debug work-in-progress code.
|
// These temporary fields are used to debug work-in-progress code.
|
||||||
@@ -72,6 +77,10 @@ pri func decoder.decode_uncompressed?(dst writer1, src reader1)() {
|
|||||||
|
|
||||||
// decode_dynamic decodes dynamic Huffman codes as per the RFC section 3.2.7.
|
// decode_dynamic decodes dynamic Huffman codes as per the RFC section 3.2.7.
|
||||||
pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
||||||
|
this.init_huffs?(src:in.src)
|
||||||
|
}
|
||||||
|
|
||||||
|
pri func decoder.init_huffs?(src reader1)() {
|
||||||
var bits u32 = this.bits
|
var bits u32 = this.bits
|
||||||
var n_bits u32 = this.n_bits
|
var n_bits u32 = this.n_bits
|
||||||
while n_bits < 14,
|
while n_bits < 14,
|
||||||
@@ -80,31 +89,31 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
|||||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
n_bits += 8
|
n_bits += 8
|
||||||
}
|
}
|
||||||
var hlit u32[257..288] = bits.low_bits(n:5) + 257
|
var n_lit u32[257..288] = bits.low_bits(n:5) + 257
|
||||||
if hlit > 286 {
|
if n_lit > 286 {
|
||||||
return error "bad literal/length code count"
|
return error "bad literal/length code count"
|
||||||
}
|
}
|
||||||
bits >>= 5
|
bits >>= 5
|
||||||
var hdist u32[1..32] = bits.low_bits(n:5) + 1
|
var n_dist u32[1..32] = bits.low_bits(n:5) + 1
|
||||||
if hdist > 30 {
|
if n_dist > 30 {
|
||||||
return error "bad distance code count"
|
return error "bad distance code count"
|
||||||
}
|
}
|
||||||
bits >>= 5
|
bits >>= 5
|
||||||
var hclen u32[4..19] = bits.low_bits(n:4) + 4
|
var n_clen u32[4..19] = bits.low_bits(n:4) + 4
|
||||||
bits >>= 4
|
bits >>= 4
|
||||||
n_bits -= 14
|
n_bits -= 14
|
||||||
|
|
||||||
// Read the code length code lengths. (This is not a typo).
|
// Read the clcode Huffman table.
|
||||||
var i u32 = 0
|
var i u32 = 0
|
||||||
while i < hclen {
|
while i < n_clen {
|
||||||
while n_bits < 3,
|
while n_bits < 3,
|
||||||
inv i < hclen,
|
inv i < n_clen,
|
||||||
post n_bits >= 3,
|
post n_bits >= 3,
|
||||||
{
|
{
|
||||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
n_bits += 8
|
n_bits += 8
|
||||||
}
|
}
|
||||||
assert i < 19 via "a < b: a < c; c <= b"(c:hclen)
|
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 u8
|
||||||
bits >>= 3
|
bits >>= 3
|
||||||
n_bits -= 3
|
n_bits -= 3
|
||||||
@@ -114,25 +123,26 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
|||||||
this.code_lengths[code_order[i]] = 0
|
this.code_lengths[code_order[i]] = 0
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
this.bits = bits
|
this.init_huff?(which:0, n_codes:19)
|
||||||
this.n_bits = n_bits
|
|
||||||
|
|
||||||
this.init_huffman?(which:0, n_codes:19)
|
|
||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
this.wip0 = hlit
|
|
||||||
this.wip1 = hdist
|
this.wip0 = n_lit
|
||||||
|
this.wip1 = n_dist
|
||||||
|
|
||||||
|
this.bits = bits
|
||||||
|
this.n_bits = n_bits
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make init_huffman a huffman_decoder method instead of a decoder
|
// TODO: make init_huff a huffman_decoder method instead of a decoder
|
||||||
// method?
|
// method?
|
||||||
|
|
||||||
// TODO: make named constants for 15, 289, etc.
|
// TODO: make named constants for 15, 19, 289, etc.
|
||||||
|
|
||||||
// TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function,
|
// TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function,
|
||||||
// and its callers, as it can only return errors, not suspensions.
|
// and its callers, as it can only return errors, not suspensions.
|
||||||
|
|
||||||
pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
|
pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() {
|
||||||
// For the clcode example in this package's README.md:
|
// For the clcode example in this package's README.md:
|
||||||
// - n_codes = 19
|
// - n_codes = 19
|
||||||
// - code_lengths[ 0] = 3
|
// - code_lengths[ 0] = 3
|
||||||
@@ -166,7 +176,7 @@ pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
|
|||||||
// TODO: this if should be unnecessary. Have some way to assert that,
|
// 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.
|
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
|
||||||
if this.huffs[in.which].counts[this.code_lengths[i]] >= 289 {
|
if this.huffs[in.which].counts[this.code_lengths[i]] >= 289 {
|
||||||
return error "internal error: inconsistent init_huffman state"
|
return error "internal error: inconsistent init_huff state"
|
||||||
}
|
}
|
||||||
this.huffs[in.which].counts[this.code_lengths[i]] += 1
|
this.huffs[in.which].counts[this.code_lengths[i]] += 1
|
||||||
i += 1
|
i += 1
|
||||||
@@ -193,7 +203,7 @@ pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
|
|||||||
offsets[i] = total
|
offsets[i] = total
|
||||||
var count u16[..289] = this.huffs[in.which].counts[i]
|
var count u16[..289] = this.huffs[in.which].counts[i]
|
||||||
if total > (289 - count) {
|
if total > (289 - count) {
|
||||||
return error "internal error: inconsistent init_huffman state"
|
return error "internal error: inconsistent init_huff state"
|
||||||
}
|
}
|
||||||
assert (total + count) <= 289 via "(a + b) <= c: a <= (c - b)"()
|
assert (total + count) <= 289 via "(a + b) <= c: a <= (c - b)"()
|
||||||
// TODO: change this to total += count, once the proof engine's bounds
|
// TODO: change this to total += count, once the proof engine's bounds
|
||||||
@@ -218,7 +228,7 @@ pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
|
|||||||
if this.code_lengths[i] != 0 {
|
if this.code_lengths[i] != 0 {
|
||||||
this.huffs[in.which].symbols[offsets[this.code_lengths[i]]] = i as u16
|
this.huffs[in.which].symbols[offsets[this.code_lengths[i]]] = i as u16
|
||||||
if offsets[this.code_lengths[i]] >= 289 {
|
if offsets[this.code_lengths[i]] >= 289 {
|
||||||
return error "internal error: inconsistent init_huffman state"
|
return error "internal error: inconsistent init_huff state"
|
||||||
}
|
}
|
||||||
offsets[this.code_lengths[i]] += 1
|
offsets[this.code_lengths[i]] += 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user