mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
Actually copy from the flate decoder's history.
This commit is contained in:
1 parent
4357c22e91
commit
c99fdaf63d
11 files changed
+289
-120
No files matched your search
@@ -165,14 +165,14 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
return 0;
|
||||
}
|
||||
start = ptr - distance;
|
||||
size_t l = end - ptr;
|
||||
if ((size_t)(length) > l) {
|
||||
length = l;
|
||||
size_t n = end - ptr;
|
||||
if ((size_t)(length) > n) {
|
||||
length = n;
|
||||
} else {
|
||||
l = length;
|
||||
n = length;
|
||||
}
|
||||
// TODO: is manual unrolling actually helpful?
|
||||
for (; l >= 8; l -= 8) {
|
||||
for (; n >= 8; n -= 8) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
@@ -182,7 +182,7 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; l; l--) {
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
@@ -195,19 +195,20 @@ static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = length;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > rend - rptr) {
|
||||
length = rend - rptr;
|
||||
if (n > rend - rptr) {
|
||||
n = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, rptr, n);
|
||||
*ptr_wptr += n;
|
||||
*ptr_rptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
@@ -215,15 +216,35 @@ static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t length = src.len;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = src.len;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, src.ptr, length);
|
||||
*ptr_wptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from_slice32(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t n = src.len;
|
||||
if (n > length) {
|
||||
n = length;
|
||||
}
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
@@ -29,9 +29,9 @@ const baseImpl = "" +
|
||||
"\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline uint16_t puffs_base_load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t puff" +
|
||||
"s_base_load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_base_load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t puffs_base_load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_i(\n puffs_base_slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(\n puffs_base_slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline " +
|
||||
"puffs_base_slice_u8\npuffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_slice_u8_prefix returns up to the first up_to bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last up_to bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from_slice calls memmove(dst.ptr, src.ptr, length)\n// where length is the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields" +
|
||||
" NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t puffs_base_slice_u8_copy_from_slice(\n puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t l = end - ptr;\n if ((size_t)(length) > l) {\n length = l;\n } else {\n l = length;\n }\n // TODO: is manual unrolling actually helpful?\n " +
|
||||
" for (; l >= 8; l -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; l; l--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n if (length > wend - wptr) {\n length = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (length > rend - rptr) {\n length = rend - rptr;\n }\n if (length > 0) {\n memmove(wptr, rptr, length);\n *ptr_wptr += length;\n *ptr_rptr += length;\n }\n return length;\n}\n\nstatic inline uint64_t puff" +
|
||||
"s_base_writer1_copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base_slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t length = src.len;\n if (length > wend - wptr) {\n length = wend - wptr;\n }\n if (length > 0) {\n memmove(wptr, src.ptr, length);\n *ptr_wptr += length;\n }\n return length;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
" NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t puffs_base_slice_u8_copy_from_slice(\n puffs_base_slice_u8 dst,\n puffs_base_slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: is manual unrolling actually helpful?\n " +
|
||||
" for (; n >= 8; n -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n memmove(wptr, rptr, n);\n *ptr_wptr += n;\n *ptr_rptr += n;\n }\n return n;\n}\n\nstatic inline uint64_t puffs_base_writer1_copy_from" +
|
||||
"_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base_slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n puffs_base_slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
@@ -187,6 +187,17 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.writeb(')')
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeyCopyFromSlice32, 2) {
|
||||
b.printf("puffs_base_writer1_copy_from_slice32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
|
||||
for _, o := range n.Args() {
|
||||
b.writeb(',')
|
||||
if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b.writeb(')')
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeyCopyFromSlice, 1) {
|
||||
b.printf("puffs_base_writer1_copy_from_slice(&%swptr_dst, %swend_dst,", bPrefix, bPrefix)
|
||||
a := n.Args()[0].Arg().Value()
|
||||
|
||||
@@ -604,6 +604,7 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
callSuspendible := methodName != t.KeyCopyFromReader32 &&
|
||||
methodName != t.KeyCopyFromHistory32 &&
|
||||
methodName != t.KeyCopyFromSlice32 &&
|
||||
methodName != t.KeyCopyFromSlice &&
|
||||
methodName != t.KeySlice
|
||||
// TODO: check that n.Args() is "(x:bar)".
|
||||
|
||||
+112
-44
@@ -209,7 +209,8 @@ typedef struct {
|
||||
uint32_t v_length;
|
||||
uint32_t v_distance;
|
||||
uint32_t v_n;
|
||||
uint32_t v_d;
|
||||
uint32_t v_hlen;
|
||||
uint32_t v_hdist;
|
||||
} c_decode_huffman[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
@@ -460,14 +461,14 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
return 0;
|
||||
}
|
||||
start = ptr - distance;
|
||||
size_t l = end - ptr;
|
||||
if ((size_t)(length) > l) {
|
||||
length = l;
|
||||
size_t n = end - ptr;
|
||||
if ((size_t)(length) > n) {
|
||||
length = n;
|
||||
} else {
|
||||
l = length;
|
||||
n = length;
|
||||
}
|
||||
// TODO: is manual unrolling actually helpful?
|
||||
for (; l >= 8; l -= 8) {
|
||||
for (; n >= 8; n -= 8) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
@@ -477,7 +478,7 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; l; l--) {
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
@@ -490,19 +491,20 @@ static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = length;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > rend - rptr) {
|
||||
length = rend - rptr;
|
||||
if (n > rend - rptr) {
|
||||
n = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, rptr, n);
|
||||
*ptr_wptr += n;
|
||||
*ptr_rptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
@@ -510,15 +512,35 @@ static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t length = src.len;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = src.len;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, src.ptr, length);
|
||||
*ptr_wptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from_slice32(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t n = src.len;
|
||||
if (n > length) {
|
||||
n = length;
|
||||
}
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
@@ -1132,6 +1154,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
v_n = puffs_base_writer1_copy_from_reader32(
|
||||
&b_wptr_dst, b_wend_dst, &b_rptr_src, b_rend_src, v_length);
|
||||
if (v_length == v_n) {
|
||||
v_length = 0;
|
||||
goto label_0_break;
|
||||
} else if (v_length < v_n) {
|
||||
status =
|
||||
@@ -1215,8 +1238,8 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
uint32_t v_length;
|
||||
uint32_t v_distance;
|
||||
uint32_t v_n;
|
||||
uint32_t v_d;
|
||||
puffs_base_slice_u8 v_history;
|
||||
uint32_t v_hlen;
|
||||
uint32_t v_hdist;
|
||||
|
||||
uint8_t* b_wptr_dst = NULL;
|
||||
uint8_t* b_wstart_dst = NULL;
|
||||
@@ -1267,8 +1290,8 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
v_length = self->private_impl.c_decode_huffman[0].v_length;
|
||||
v_distance = self->private_impl.c_decode_huffman[0].v_distance;
|
||||
v_n = self->private_impl.c_decode_huffman[0].v_n;
|
||||
v_d = self->private_impl.c_decode_huffman[0].v_d;
|
||||
v_history = ((puffs_base_slice_u8){});
|
||||
v_hlen = self->private_impl.c_decode_huffman[0].v_hlen;
|
||||
v_hdist = self->private_impl.c_decode_huffman[0].v_hdist;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||
@@ -1446,27 +1469,71 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
v_n = 0;
|
||||
if (((uint64_t)(v_distance)) >
|
||||
((uint64_t)(b_wptr_dst - b_wstart_dst))) {
|
||||
v_d = ((uint32_t)((((uint64_t)(v_distance)) -
|
||||
((uint64_t)(b_wptr_dst - b_wstart_dst)))));
|
||||
if (v_length > v_d) {
|
||||
v_length -= v_d;
|
||||
v_n = v_d;
|
||||
v_hlen = 0;
|
||||
v_hdist = ((uint32_t)((((uint64_t)(v_distance)) -
|
||||
((uint64_t)(b_wptr_dst - b_wstart_dst)))));
|
||||
if (v_length > v_hdist) {
|
||||
v_length -= v_hdist;
|
||||
v_hlen = v_hdist;
|
||||
} else {
|
||||
v_length = 0;
|
||||
v_n = v_length;
|
||||
v_hlen = v_length;
|
||||
}
|
||||
if (self->private_impl.f_history_index < v_d) {
|
||||
if (self->private_impl.f_history_index < v_hdist) {
|
||||
status = PUFFS_FLATE_ERROR_BAD_DISTANCE;
|
||||
goto exit;
|
||||
}
|
||||
v_history = puffs_base_slice_u8_subslice_i(
|
||||
((puffs_base_slice_u8){.ptr = self->private_impl.f_history,
|
||||
.len = 32768}),
|
||||
(self->private_impl.f_history_index - v_d) & 32767);
|
||||
if (((uint64_t)(v_n)) <= ((uint64_t)(v_history.len))) {
|
||||
} else {
|
||||
v_n =
|
||||
((uint32_t)((((uint64_t)(v_n)) - ((uint64_t)(v_history.len)))));
|
||||
v_hdist = ((self->private_impl.f_history_index - v_hdist) & 32767);
|
||||
while (true) {
|
||||
v_n = puffs_base_writer1_copy_from_slice32(
|
||||
&b_wptr_dst, b_wend_dst,
|
||||
puffs_base_slice_u8_subslice_i(
|
||||
((puffs_base_slice_u8){.ptr = self->private_impl.f_history,
|
||||
.len = 32768}),
|
||||
v_hdist),
|
||||
v_hlen);
|
||||
if (v_hlen == v_n) {
|
||||
v_hlen = 0;
|
||||
goto label_6_break;
|
||||
} else if (v_hlen < v_n) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto exit;
|
||||
}
|
||||
if (v_n > 0) {
|
||||
v_hlen -= v_n;
|
||||
v_hdist = ((v_hdist + (v_n & 32767)) & 32767);
|
||||
if (v_hdist == 0) {
|
||||
goto label_6_break;
|
||||
}
|
||||
}
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
|
||||
}
|
||||
label_6_break:;
|
||||
if (v_hlen > 0) {
|
||||
while (true) {
|
||||
v_n = puffs_base_writer1_copy_from_slice32(
|
||||
&b_wptr_dst, b_wend_dst,
|
||||
puffs_base_slice_u8_subslice_i(
|
||||
((puffs_base_slice_u8){
|
||||
.ptr = self->private_impl.f_history, .len = 32768}),
|
||||
v_hdist),
|
||||
v_hlen);
|
||||
if (v_hlen == v_n) {
|
||||
v_hlen = 0;
|
||||
goto label_7_break;
|
||||
} else if (v_hlen < v_n) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto exit;
|
||||
}
|
||||
v_hlen -= v_n;
|
||||
v_hdist = ((v_hdist + (v_n & 32767)) & 32767);
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
|
||||
}
|
||||
label_7_break:;
|
||||
}
|
||||
if (v_length == 0) {
|
||||
goto label_5_continue;
|
||||
@@ -1483,7 +1550,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
}
|
||||
v_length -= v_n;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(10);
|
||||
}
|
||||
label_5_break:;
|
||||
}
|
||||
@@ -1512,7 +1579,8 @@ suspend:
|
||||
self->private_impl.c_decode_huffman[0].v_length = v_length;
|
||||
self->private_impl.c_decode_huffman[0].v_distance = v_distance;
|
||||
self->private_impl.c_decode_huffman[0].v_n = v_n;
|
||||
self->private_impl.c_decode_huffman[0].v_d = v_d;
|
||||
self->private_impl.c_decode_huffman[0].v_hlen = v_hlen;
|
||||
self->private_impl.c_decode_huffman[0].v_hdist = v_hdist;
|
||||
|
||||
exit:
|
||||
if (a_dst.buf) {
|
||||
|
||||
+43
-22
@@ -428,14 +428,14 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
return 0;
|
||||
}
|
||||
start = ptr - distance;
|
||||
size_t l = end - ptr;
|
||||
if ((size_t)(length) > l) {
|
||||
length = l;
|
||||
size_t n = end - ptr;
|
||||
if ((size_t)(length) > n) {
|
||||
length = n;
|
||||
} else {
|
||||
l = length;
|
||||
n = length;
|
||||
}
|
||||
// TODO: is manual unrolling actually helpful?
|
||||
for (; l >= 8; l -= 8) {
|
||||
for (; n >= 8; n -= 8) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
@@ -445,7 +445,7 @@ static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; l; l--) {
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
@@ -458,19 +458,20 @@ static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = length;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > rend - rptr) {
|
||||
length = rend - rptr;
|
||||
if (n > rend - rptr) {
|
||||
n = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, rptr, n);
|
||||
*ptr_wptr += n;
|
||||
*ptr_rptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
@@ -478,15 +479,35 @@ static inline uint64_t puffs_base_writer1_copy_from_slice(
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t length = src.len;
|
||||
if (length > wend - wptr) {
|
||||
length = wend - wptr;
|
||||
size_t n = src.len;
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, src.ptr, length);
|
||||
*ptr_wptr += length;
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return length;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from_slice32(
|
||||
uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
puffs_base_slice_u8 src,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
size_t n = src.len;
|
||||
if (n > length) {
|
||||
n = length;
|
||||
}
|
||||
if (n > wend - wptr) {
|
||||
n = wend - wptr;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(wptr, src.ptr, n);
|
||||
*ptr_wptr += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
|
||||
+2
-1
@@ -209,7 +209,8 @@ typedef struct {
|
||||
uint32_t v_length;
|
||||
uint32_t v_distance;
|
||||
uint32_t v_n;
|
||||
uint32_t v_d;
|
||||
uint32_t v_hlen;
|
||||
uint32_t v_hdist;
|
||||
} c_decode_huffman[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
|
||||
@@ -663,9 +663,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
|
||||
isInDst(q.tm, n, t.KeySlice, 0) ||
|
||||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySlice, 0) ||
|
||||
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_uncompressed", 2) || isThisMethod(q.tm, n, "decode_huffman", 2) ||
|
||||
|
||||
+5
-3
@@ -373,9 +373,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
|
||||
isInDst(q.tm, n, t.KeySlice, 0) ||
|
||||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySlice, 0) ||
|
||||
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_uncompressed", 2) || isThisMethod(q.tm, n, "decode_huffman", 2) ||
|
||||
@@ -401,7 +401,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprSliceU8) // HACK.
|
||||
} else if isInDst(q.tm, n, t.KeyCopyFromSlice, 1) {
|
||||
n.SetMType(typeExprU64) // HACK.
|
||||
} else if isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) {
|
||||
} else if isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) || isInDst(q.tm, n, t.KeyCopyFromReader32, 2) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) {
|
||||
n.SetMType(typeExprU32) // HACK.
|
||||
} else {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
@@ -619,6 +620,7 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
callSuspendible := methodName != t.KeyCopyFromReader32 &&
|
||||
methodName != t.KeyCopyFromHistory32 &&
|
||||
methodName != t.KeyCopyFromSlice32 &&
|
||||
methodName != t.KeyCopyFromSlice &&
|
||||
methodName != t.KeySlice
|
||||
// TODO: check that n.Args() is "(x:bar)".
|
||||
|
||||
+10
-7
@@ -287,6 +287,7 @@ const (
|
||||
KeyCopyFromHistory32 = Key(IDCopyFromHistory32 >> KeyShift)
|
||||
KeyCopyFromReader32 = Key(IDCopyFromReader32 >> KeyShift)
|
||||
KeyCopyFromSlice = Key(IDCopyFromSlice >> KeyShift)
|
||||
KeyCopyFromSlice32 = Key(IDCopyFromSlice32 >> KeyShift)
|
||||
KeySkip32 = Key(IDSkip32 >> KeyShift)
|
||||
KeySkip64 = Key(IDSkip64 >> KeyShift)
|
||||
KeySlice = Key(IDSlice >> KeyShift)
|
||||
@@ -464,13 +465,14 @@ const (
|
||||
IDCopyFromHistory32 = ID(0xC3<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDCopyFromReader32 = ID(0xC4<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDCopyFromSlice = ID(0xC5<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSkip32 = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSkip64 = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDAvailable = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDPrefix = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSuffix = ID(0xCC<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDCopyFromSlice32 = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSkip32 = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSkip64 = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSlice = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDLength = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDAvailable = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDPrefix = ID(0xCC<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSuffix = ID(0xCD<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
)
|
||||
|
||||
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
|
||||
@@ -663,6 +665,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyCopyFromHistory32: {"copy_from_history32", IDCopyFromHistory32},
|
||||
KeyCopyFromReader32: {"copy_from_reader32", IDCopyFromReader32},
|
||||
KeyCopyFromSlice: {"copy_from_slice", IDCopyFromSlice},
|
||||
KeyCopyFromSlice32: {"copy_from_slice32", IDCopyFromSlice32},
|
||||
KeySkip32: {"skip32", IDSkip32},
|
||||
KeySkip64: {"skip64", IDSkip64},
|
||||
KeySlice: {"slice", IDSlice},
|
||||
|
||||
@@ -212,6 +212,7 @@ pri func decoder.decode_uncompressed?(dst writer1, src reader1)() {
|
||||
var n u32 = in.dst.copy_from_reader32(r:in.src, length:length)
|
||||
// TODO: assert n <= length and simplify the subsequent if.
|
||||
if length == n {
|
||||
length = 0
|
||||
break
|
||||
} else if length < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
@@ -365,31 +366,71 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
|
||||
// Copy from this.history.
|
||||
if (distance as u64) > in.dst.length() {
|
||||
// Set (n, d) to be the length-distance pair to copy from
|
||||
// this.history, and (length, distance) to be the remaining
|
||||
// length-distance pair to copy from in.dst.
|
||||
var d u32[..0x7FFF] = ((distance as u64) - in.dst.length()) as u32
|
||||
if length > d {
|
||||
length -= d
|
||||
n = d
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from in.dst.
|
||||
var hlen u32[..0x7FFF]
|
||||
var hdist u32[..0x7FFF] = ((distance as u64) - in.dst.length()) as u32
|
||||
if length > hdist {
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
length = 0
|
||||
n = length
|
||||
hlen = length
|
||||
}
|
||||
if this.history_index < d {
|
||||
if this.history_index < hdist {
|
||||
return error "bad distance"
|
||||
}
|
||||
// Re-purpose the hdist variable as the this.history index to
|
||||
// start copying from.
|
||||
hdist = (this.history_index - hdist) & 0x7FFF
|
||||
|
||||
var history[] u8 = this.history[(this.history_index - d) & 0x7FFF:]
|
||||
if (n as u64) <= history.length() {
|
||||
// TODO: in.dst.write?(x:history.prefix(up_to:n as u64))
|
||||
} else {
|
||||
n = ((n as u64) - history.length()) as u32
|
||||
// TODO: in.dst.write?(x:history)
|
||||
// TODO: in.dst.write?(x:this.history[:n & 0x7FFF])
|
||||
// Copy from hdist to the end of this.history.
|
||||
while true {
|
||||
n = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
if hlen == n {
|
||||
hlen = 0
|
||||
break
|
||||
} else if hlen < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if n > 0 {
|
||||
hlen -= n
|
||||
// TODO: a "~+" tilde-operator that ignores overflow.
|
||||
hdist = (hdist + (n & 0x7FFF)) & 0x7FFF
|
||||
if hdist == 0 {
|
||||
// Wrap around the this.history ringbuffer.
|
||||
break
|
||||
}
|
||||
}
|
||||
// TODO: "closed for writes" instead?
|
||||
return suspension "short write"
|
||||
// TODO: this "assert false" should be a compiler error.
|
||||
// This might mean differentiating "return" and "raise".
|
||||
//
|
||||
// TODO: invalidate this-related facts because the
|
||||
// suspension can change the state of this and its fields.
|
||||
assert false
|
||||
}
|
||||
// Copy from the start of this.history, if we wrapped around.
|
||||
if hlen > 0 {
|
||||
while true {
|
||||
n = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
if hlen == n {
|
||||
hlen = 0
|
||||
break
|
||||
} else if hlen < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
hlen -= n
|
||||
hdist = (hdist + (n & 0x7FFF)) & 0x7FFF
|
||||
// TODO: "closed for writes" instead?
|
||||
return suspension "short write"
|
||||
}
|
||||
}
|
||||
|
||||
if length == 0 {
|
||||
// No need to copy from in.dst.
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user