mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Add a buf1.closed field.
This commit is contained in:
1 parent
84e999c14e
commit
b3c12e05cf
9 files changed
+44
-29
No files matched your search
@@ -38,6 +38,7 @@ typedef struct {
|
|||||||
size_t len; // Length.
|
size_t len; // Length.
|
||||||
size_t wi; // Write index. Invariant: wi <= len.
|
size_t wi; // Write index. Invariant: wi <= len.
|
||||||
size_t ri; // Read index. Invariant: ri <= wi.
|
size_t ri; // Read index. Invariant: ri <= wi.
|
||||||
|
bool closed; // No further writes are expected.
|
||||||
} puffs_base_buf1;
|
} puffs_base_buf1;
|
||||||
|
|
||||||
#endif // PUFFS_BASE_HEADER_H
|
#endif // PUFFS_BASE_HEADER_H
|
||||||
@@ -7,7 +7,7 @@ package main
|
|||||||
|
|
||||||
const baseHeader = "" +
|
const baseHeader = "" +
|
||||||
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)" +
|
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)" +
|
||||||
"\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus\n// additional indexes into that buffer.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n} puffs_base_buf1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
"\n\n// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length) plus\n// additional indexes into that buffer.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n} puffs_base_buf1;\n\n#endif // PUFFS_BASE_HEADER_H\n" +
|
||||||
""
|
""
|
||||||
|
|
||||||
const baseImpl = "" +
|
const baseImpl = "" +
|
||||||
|
|||||||
+13
-7
@@ -77,9 +77,10 @@ var builtInStatuses = [...]string{
|
|||||||
"error bad receiver",
|
"error bad receiver",
|
||||||
"error bad argument",
|
"error bad argument",
|
||||||
"error constructor not called",
|
"error constructor not called",
|
||||||
"error unexpected EOF", // Used if read_foo passed final == true.
|
"error unexpected EOF", // Used if reading when closed == true.
|
||||||
"status short read", // Used if read_foo passed final == false.
|
"status short read", // Used if reading when closed == false.
|
||||||
"status short write",
|
"status short write",
|
||||||
|
"error closed for writes",
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -945,11 +946,9 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
|||||||
// TODO: delete these hacks that only matches "in.src.read_u8?()" etc.
|
// TODO: delete these hacks that only matches "in.src.read_u8?()" etc.
|
||||||
if isInSrcReadU8(g.tm, n) {
|
if isInSrcReadU8(g.tm, n) {
|
||||||
// TODO: suspend coroutine state.
|
// TODO: suspend coroutine state.
|
||||||
//
|
g.printf("if (%ssrc->ri >= %ssrc->wi) { status = "+
|
||||||
// TODO: choose error_unexpected_eof versus status_short_read based on
|
"%ssrc->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
|
||||||
// src_final.
|
aPrefix, aPrefix, aPrefix, g.pkgName, g.pkgName)
|
||||||
g.printf("if (%ssrc->ri >= %ssrc->wi) { status = puffs_%s_status_short_read;",
|
|
||||||
aPrefix, aPrefix, g.pkgName)
|
|
||||||
if g.perFunc.public && g.perFunc.suspendible {
|
if g.perFunc.public && g.perFunc.suspendible {
|
||||||
g.writes("goto cleanup0;")
|
g.writes("goto cleanup0;")
|
||||||
} else {
|
} else {
|
||||||
@@ -965,6 +964,13 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
|||||||
// TODO: suspend coroutine state.
|
// TODO: suspend coroutine state.
|
||||||
//
|
//
|
||||||
// TODO: don't assume that the argument is "this.stack[s:]".
|
// TODO: don't assume that the argument is "this.stack[s:]".
|
||||||
|
g.printf("if (%sdst->closed) { status = puffs_%s_error_closed_for_writes;", aPrefix, g.pkgName)
|
||||||
|
if g.perFunc.public && g.perFunc.suspendible {
|
||||||
|
g.writes("goto cleanup0;")
|
||||||
|
} else {
|
||||||
|
g.writes("return status;")
|
||||||
|
}
|
||||||
|
g.writes("}\n")
|
||||||
g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {", aPrefix, aPrefix)
|
g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {", aPrefix, aPrefix)
|
||||||
g.printf("status = puffs_%s_status_short_write;", g.pkgName)
|
g.printf("status = puffs_%s_status_short_write;", g.pkgName)
|
||||||
if g.perFunc.public && g.perFunc.suspendible {
|
if g.perFunc.public && g.perFunc.suspendible {
|
||||||
|
|||||||
+15
-9
@@ -41,6 +41,7 @@ typedef struct {
|
|||||||
size_t len; // Length.
|
size_t len; // Length.
|
||||||
size_t wi; // Write index. Invariant: wi <= len.
|
size_t wi; // Write index. Invariant: wi <= len.
|
||||||
size_t ri; // Read index. Invariant: ri <= wi.
|
size_t ri; // Read index. Invariant: ri <= wi.
|
||||||
|
bool closed; // No further writes are expected.
|
||||||
} puffs_base_buf1;
|
} puffs_base_buf1;
|
||||||
|
|
||||||
#endif // PUFFS_BASE_HEADER_H
|
#endif // PUFFS_BASE_HEADER_H
|
||||||
@@ -63,6 +64,7 @@ typedef enum {
|
|||||||
puffs_gif_error_unexpected_eof = -10 + 1,
|
puffs_gif_error_unexpected_eof = -10 + 1,
|
||||||
puffs_gif_status_short_read = -12,
|
puffs_gif_status_short_read = -12,
|
||||||
puffs_gif_status_short_write = -14,
|
puffs_gif_status_short_write = -14,
|
||||||
|
puffs_gif_error_closed_for_writes = -16 + 1,
|
||||||
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
||||||
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
@@ -111,8 +113,7 @@ void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
|||||||
|
|
||||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||||
puffs_base_buf1* a_dst,
|
puffs_base_buf1* a_dst,
|
||||||
puffs_base_buf1* a_src,
|
puffs_base_buf1* a_src);
|
||||||
bool a_src_final);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
@@ -135,7 +136,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s) {
|
|||||||
return s & 1;
|
return s & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* puffs_gif_status_strings[10] = {
|
const char* puffs_gif_status_strings[11] = {
|
||||||
"gif: ok",
|
"gif: ok",
|
||||||
"gif: bad version",
|
"gif: bad version",
|
||||||
"gif: bad receiver",
|
"gif: bad receiver",
|
||||||
@@ -144,6 +145,7 @@ const char* puffs_gif_status_strings[10] = {
|
|||||||
"gif: unexpected EOF",
|
"gif: unexpected EOF",
|
||||||
"gif: short read",
|
"gif: short read",
|
||||||
"gif: short write",
|
"gif: short write",
|
||||||
|
"gif: closed for writes",
|
||||||
"gif: LZW code is out of range",
|
"gif: LZW code is out of range",
|
||||||
"gif: LZW prefix chain is cyclical",
|
"gif: LZW prefix chain is cyclical",
|
||||||
};
|
};
|
||||||
@@ -151,11 +153,11 @@ const char* puffs_gif_status_strings[10] = {
|
|||||||
const char* puffs_gif_status_string(puffs_gif_status s) {
|
const char* puffs_gif_status_string(puffs_gif_status s) {
|
||||||
s = -(s >> 1);
|
s = -(s >> 1);
|
||||||
if (0 <= s) {
|
if (0 <= s) {
|
||||||
if (s < 8) {
|
if (s < 9) {
|
||||||
return puffs_gif_status_strings[s];
|
return puffs_gif_status_strings[s];
|
||||||
}
|
}
|
||||||
s -= 120;
|
s -= 119;
|
||||||
if ((8 <= s) && (s < 10)) {
|
if ((9 <= s) && (s < 11)) {
|
||||||
return puffs_gif_status_strings[s];
|
return puffs_gif_status_strings[s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,8 +233,7 @@ void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
|||||||
|
|
||||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||||
puffs_base_buf1* a_dst,
|
puffs_base_buf1* a_dst,
|
||||||
puffs_base_buf1* a_src,
|
puffs_base_buf1* a_src) {
|
||||||
bool a_src_final) {
|
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return puffs_gif_error_bad_receiver;
|
return puffs_gif_error_bad_receiver;
|
||||||
}
|
}
|
||||||
@@ -273,7 +274,8 @@ label_0_continue:;
|
|||||||
while (true) {
|
while (true) {
|
||||||
while (v_n_bits < v_width) {
|
while (v_n_bits < v_width) {
|
||||||
if (a_src->ri >= a_src->wi) {
|
if (a_src->ri >= a_src->wi) {
|
||||||
status = puffs_gif_status_short_read;
|
status = a_src->closed ? puffs_gif_error_unexpected_eof
|
||||||
|
: puffs_gif_status_short_read;
|
||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
}
|
}
|
||||||
uint8_t t_0 = a_src->ptr[a_src->ri++];
|
uint8_t t_0 = a_src->ptr[a_src->ri++];
|
||||||
@@ -322,6 +324,10 @@ label_0_continue:;
|
|||||||
if ((v_code == v_save_code) && v_use_save_code) {
|
if ((v_code == v_save_code) && v_use_save_code) {
|
||||||
self->private_impl.f_stack[4095] = ((uint8_t)(v_c));
|
self->private_impl.f_stack[4095] = ((uint8_t)(v_c));
|
||||||
}
|
}
|
||||||
|
if (a_dst->closed) {
|
||||||
|
status = puffs_gif_error_closed_for_writes;
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
if ((a_dst->len - a_dst->wi) <
|
if ((a_dst->len - a_dst->wi) <
|
||||||
(sizeof(self->private_impl.f_stack) - v_s)) {
|
(sizeof(self->private_impl.f_stack) - v_s)) {
|
||||||
status = puffs_gif_status_short_write;
|
status = puffs_gif_status_short_write;
|
||||||
|
|||||||
+3
-2
@@ -41,6 +41,7 @@ typedef struct {
|
|||||||
size_t len; // Length.
|
size_t len; // Length.
|
||||||
size_t wi; // Write index. Invariant: wi <= len.
|
size_t wi; // Write index. Invariant: wi <= len.
|
||||||
size_t ri; // Read index. Invariant: ri <= wi.
|
size_t ri; // Read index. Invariant: ri <= wi.
|
||||||
|
bool closed; // No further writes are expected.
|
||||||
} puffs_base_buf1;
|
} puffs_base_buf1;
|
||||||
|
|
||||||
#endif // PUFFS_BASE_HEADER_H
|
#endif // PUFFS_BASE_HEADER_H
|
||||||
@@ -63,6 +64,7 @@ typedef enum {
|
|||||||
puffs_gif_error_unexpected_eof = -10 + 1,
|
puffs_gif_error_unexpected_eof = -10 + 1,
|
||||||
puffs_gif_status_short_read = -12,
|
puffs_gif_status_short_read = -12,
|
||||||
puffs_gif_status_short_write = -14,
|
puffs_gif_status_short_write = -14,
|
||||||
|
puffs_gif_error_closed_for_writes = -16 + 1,
|
||||||
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
puffs_gif_error_lzw_code_is_out_of_range = -256 + 1,
|
||||||
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
puffs_gif_error_lzw_prefix_chain_is_cyclical = -258 + 1,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
@@ -111,8 +113,7 @@ void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
|||||||
|
|
||||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||||
puffs_base_buf1* a_dst,
|
puffs_base_buf1* a_dst,
|
||||||
puffs_base_buf1* a_src,
|
puffs_base_buf1* a_src);
|
||||||
bool a_src_final);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ pub func lzw_decoder.set_literal_width(lw u32[2..8] = 8)() {
|
|||||||
this.literal_width = in.lw
|
this.literal_width = in.lw
|
||||||
}
|
}
|
||||||
|
|
||||||
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1)() {
|
||||||
// These variables don't change over the lifetime of this func.
|
// These variables don't change over the lifetime of this func.
|
||||||
var clear_code u32[4..256] = (1 as u32) << this.literal_width
|
var clear_code u32[4..256] = (1 as u32) << this.literal_width
|
||||||
var end_code u32[5..257] = clear_code + 1
|
var end_code u32[5..257] = clear_code + 1
|
||||||
@@ -41,7 +41,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
|||||||
post n_bits >= width,
|
post n_bits >= width,
|
||||||
{
|
{
|
||||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||||
bits |= (in.src.read_u8?() as u32) << n_bits // TODO: read_u8 should take src_final.
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
n_bits += 8
|
n_bits += 8
|
||||||
}
|
}
|
||||||
var code u32[..4095] = bits.low_bits(n:width)
|
var code u32[..4095] = bits.low_bits(n:width)
|
||||||
|
|||||||
+3
-6
@@ -22,8 +22,7 @@ void test_constructor_not_called() {
|
|||||||
puffs_gif_lzw_decoder dec;
|
puffs_gif_lzw_decoder dec;
|
||||||
puffs_base_buf1 dst = {0};
|
puffs_base_buf1 dst = {0};
|
||||||
puffs_base_buf1 src = {0};
|
puffs_base_buf1 src = {0};
|
||||||
puffs_gif_status status =
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, &dst, &src);
|
||||||
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
|
|
||||||
if (status != puffs_gif_error_constructor_not_called) {
|
if (status != puffs_gif_error_constructor_not_called) {
|
||||||
FAIL("status: got %d, want %d", status,
|
FAIL("status: got %d, want %d", status,
|
||||||
puffs_gif_error_constructor_not_called);
|
puffs_gif_error_constructor_not_called);
|
||||||
@@ -34,8 +33,7 @@ void test_bad_argument_null() {
|
|||||||
test_funcname = __func__;
|
test_funcname = __func__;
|
||||||
puffs_gif_lzw_decoder dec;
|
puffs_gif_lzw_decoder dec;
|
||||||
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||||
puffs_gif_status status =
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, NULL, NULL);
|
||||||
puffs_gif_lzw_decoder_decode(&dec, NULL, NULL, false);
|
|
||||||
if (status != puffs_gif_error_bad_argument) {
|
if (status != puffs_gif_error_bad_argument) {
|
||||||
FAIL("status: got %d, want %d", status, puffs_gif_error_bad_argument);
|
FAIL("status: got %d, want %d", status, puffs_gif_error_bad_argument);
|
||||||
}
|
}
|
||||||
@@ -72,8 +70,7 @@ void test_bad_receiver() {
|
|||||||
test_funcname = __func__;
|
test_funcname = __func__;
|
||||||
puffs_base_buf1 dst = {0};
|
puffs_base_buf1 dst = {0};
|
||||||
puffs_base_buf1 src = {0};
|
puffs_base_buf1 src = {0};
|
||||||
puffs_gif_status status =
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(NULL, &dst, &src);
|
||||||
puffs_gif_lzw_decoder_decode(NULL, &dst, &src, false);
|
|
||||||
if (status != puffs_gif_error_bad_receiver) {
|
if (status != puffs_gif_error_bad_receiver) {
|
||||||
FAIL("status: got %d, want %d", status, puffs_gif_error_bad_receiver);
|
FAIL("status: got %d, want %d", status, puffs_gif_error_bad_receiver);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -58,8 +58,7 @@ void test_lzw_decode() {
|
|||||||
puffs_gif_lzw_decoder dec;
|
puffs_gif_lzw_decoder dec;
|
||||||
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||||
puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width);
|
puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width);
|
||||||
puffs_gif_status status =
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, &got, &src);
|
||||||
puffs_gif_lzw_decoder_decode(&dec, &got, &src, false);
|
|
||||||
if (status != puffs_gif_status_ok) {
|
if (status != puffs_gif_status_ok) {
|
||||||
FAIL("status: got %d, want %d", status, puffs_gif_status_ok);
|
FAIL("status: got %d, want %d", status, puffs_gif_status_ok);
|
||||||
goto cleanup1;
|
goto cleanup1;
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ bool read_file(puffs_base_buf1* dst, const char* path) {
|
|||||||
FAIL("read_file: NULL argument");
|
FAIL("read_file: NULL argument");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (dst->closed) {
|
||||||
|
FAIL("read_file: dst buffer closed for writes");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
FILE* f = fopen(path, "r");
|
FILE* f = fopen(path, "r");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno), errno);
|
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno), errno);
|
||||||
@@ -83,6 +87,7 @@ bool read_file(puffs_base_buf1* dst, const char* path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
dst->closed = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user