Make std/gif's LZW decoder private

This commit is contained in:
Nigel Tao committed 2017-12-08 16:18:35 +11:00
1 parent 44f5b2d384
commit 2e12a41414
4 files changed
+40 -109

No files matched your search

+20 -45
View File
@@ -245,15 +245,6 @@ typedef struct {
// ---------------- Public Initializer Prototypes // ---------------- Public Initializer Prototypes
// wuffs_gif__lzw_decoder__initialize is an initializer function.
//
// It should be called before any other wuffs_gif__lzw_decoder__* function.
//
// Pass WUFFS_VERSION and 0 for wuffs_version and for_internal_use_only.
void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
uint32_t wuffs_version,
uint32_t for_internal_use_only);
// wuffs_gif__decoder__initialize is an initializer function. // wuffs_gif__decoder__initialize is an initializer function.
// //
// It should be called before any other wuffs_gif__decoder__* function. // It should be called before any other wuffs_gif__decoder__* function.
@@ -269,13 +260,6 @@ wuffs_gif__status wuffs_gif__decoder__decode(wuffs_gif__decoder* self,
wuffs_base__writer1 a_dst, wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src); wuffs_base__reader1 a_src);
void wuffs_gif__lzw_decoder__set_literal_width(wuffs_gif__lzw_decoder* self,
uint32_t a_lw);
wuffs_gif__status wuffs_gif__lzw_decoder__decode(wuffs_gif__lzw_decoder* self,
wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif
@@ -681,6 +665,10 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
// ---------------- Private Initializer Prototypes // ---------------- Private Initializer Prototypes
void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
uint32_t wuffs_version,
uint32_t for_internal_use_only);
// ---------------- Private Function Prototypes // ---------------- Private Function Prototypes
static wuffs_gif__status wuffs_gif__decoder__decode_header( static wuffs_gif__status wuffs_gif__decoder__decode_header(
@@ -700,6 +688,15 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
wuffs_base__writer1 a_dst, wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src); wuffs_base__reader1 a_src);
static void wuffs_gif__lzw_decoder__set_literal_width(
wuffs_gif__lzw_decoder* self,
uint32_t a_lw);
static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
wuffs_gif__lzw_decoder* self,
wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src);
// ---------------- Initializer Implementations // ---------------- Initializer Implementations
void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self, void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
@@ -1418,37 +1415,16 @@ short_read_src:
goto suspend; goto suspend;
} }
void wuffs_gif__lzw_decoder__set_literal_width(wuffs_gif__lzw_decoder* self, static void wuffs_gif__lzw_decoder__set_literal_width(
uint32_t a_lw) { wuffs_gif__lzw_decoder* self,
if (!self) { uint32_t a_lw) {
return;
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
self->private_impl.status = WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED;
}
if (self->private_impl.status < 0) {
return;
}
if (a_lw < 2 || a_lw > 8) {
self->private_impl.status = WUFFS_GIF__ERROR_BAD_ARGUMENT;
return;
}
self->private_impl.f_literal_width = a_lw; self->private_impl.f_literal_width = a_lw;
} }
wuffs_gif__status wuffs_gif__lzw_decoder__decode(wuffs_gif__lzw_decoder* self, static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
wuffs_base__writer1 a_dst, wuffs_gif__lzw_decoder* self,
wuffs_base__reader1 a_src) { wuffs_base__writer1 a_dst,
if (!self) { wuffs_base__reader1 a_src) {
return WUFFS_GIF__ERROR_BAD_RECEIVER;
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
self->private_impl.status = WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED;
}
if (self->private_impl.status < 0) {
return self->private_impl.status;
}
wuffs_gif__status status = WUFFS_GIF__STATUS_OK; wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint32_t v_clear_code; uint32_t v_clear_code;
@@ -1659,7 +1635,6 @@ exit:
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src); WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
} }
self->private_impl.status = status;
return status; return status;
short_read_src: short_read_src:
-16
View File
@@ -245,15 +245,6 @@ typedef struct {
// ---------------- Public Initializer Prototypes // ---------------- Public Initializer Prototypes
// wuffs_gif__lzw_decoder__initialize is an initializer function.
//
// It should be called before any other wuffs_gif__lzw_decoder__* function.
//
// Pass WUFFS_VERSION and 0 for wuffs_version and for_internal_use_only.
void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
uint32_t wuffs_version,
uint32_t for_internal_use_only);
// wuffs_gif__decoder__initialize is an initializer function. // wuffs_gif__decoder__initialize is an initializer function.
// //
// It should be called before any other wuffs_gif__decoder__* function. // It should be called before any other wuffs_gif__decoder__* function.
@@ -269,13 +260,6 @@ wuffs_gif__status wuffs_gif__decoder__decode(wuffs_gif__decoder* self,
wuffs_base__writer1 a_dst, wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src); wuffs_base__reader1 a_src);
void wuffs_gif__lzw_decoder__set_literal_width(wuffs_gif__lzw_decoder* self,
uint32_t a_lw);
wuffs_gif__status wuffs_gif__lzw_decoder__decode(wuffs_gif__lzw_decoder* self,
wuffs_base__writer1 a_dst,
wuffs_base__reader1 a_src);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif
+3 -3
View File
@@ -17,7 +17,7 @@
pub error "LZW code is out of range" pub error "LZW code is out of range"
pub error "LZW prefix chain is cyclical" pub error "LZW prefix chain is cyclical"
pub struct lzw_decoder?( pri struct lzw_decoder?(
literal_width u32[2..8] = 8, literal_width u32[2..8] = 8,
stack[4096] u8, stack[4096] u8,
suffixes[4096] u8, suffixes[4096] u8,
@@ -25,11 +25,11 @@ pub struct lzw_decoder?(
) )
// TODO: add a ! as this function is impure. // TODO: add a ! as this function is impure.
pub func lzw_decoder.set_literal_width(lw u32[2..8] = 8)() { pri 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 writer1, src reader1)() { pri func lzw_decoder.decode?(dst writer1, src reader1)() {
// 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
+17 -45
View File
@@ -48,38 +48,11 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
// ---------------- Basic Tests // ---------------- Basic Tests
void test_basic_bad_argument_out_of_range() {
CHECK_FOCUS(__func__);
wuffs_gif__lzw_decoder dec;
wuffs_gif__lzw_decoder__initialize(&dec, WUFFS_VERSION, 0);
// Setting to 8 is in the [2..8] range.
wuffs_gif__lzw_decoder__set_literal_width(&dec, 8);
if (dec.private_impl.status != WUFFS_GIF__STATUS_OK) {
FAIL("status: got %d, want %d", dec.private_impl.status,
WUFFS_GIF__STATUS_OK);
}
// Setting to 999 is out of range.
wuffs_gif__lzw_decoder__set_literal_width(&dec, 999);
if (dec.private_impl.status != WUFFS_GIF__ERROR_BAD_ARGUMENT) {
FAIL("status: got %d, want %d", dec.private_impl.status,
WUFFS_GIF__ERROR_BAD_ARGUMENT);
}
// That error status code should be sticky.
wuffs_gif__lzw_decoder__set_literal_width(&dec, 8);
if (dec.private_impl.status != WUFFS_GIF__ERROR_BAD_ARGUMENT) {
FAIL("status: got %d, want %d", dec.private_impl.status,
WUFFS_GIF__ERROR_BAD_ARGUMENT);
}
}
void test_basic_bad_receiver() { void test_basic_bad_receiver() {
CHECK_FOCUS(__func__); CHECK_FOCUS(__func__);
wuffs_base__writer1 dst = {0}; wuffs_base__writer1 dst = {0};
wuffs_base__reader1 src = {0}; wuffs_base__reader1 src = {0};
wuffs_gif__status status = wuffs_gif__lzw_decoder__decode(NULL, dst, src); wuffs_gif__status status = wuffs_gif__decoder__decode(NULL, dst, src);
if (status != WUFFS_GIF__ERROR_BAD_RECEIVER) { if (status != WUFFS_GIF__ERROR_BAD_RECEIVER) {
FAIL("status: got %d, want %d", status, WUFFS_GIF__ERROR_BAD_RECEIVER); FAIL("status: got %d, want %d", status, WUFFS_GIF__ERROR_BAD_RECEIVER);
} }
@@ -87,10 +60,10 @@ void test_basic_bad_receiver() {
void test_basic_initializer_not_called() { void test_basic_initializer_not_called() {
CHECK_FOCUS(__func__); CHECK_FOCUS(__func__);
wuffs_gif__lzw_decoder dec = {{0}}; wuffs_gif__decoder dec = {{0}};
wuffs_base__writer1 dst = {0}; wuffs_base__writer1 dst = {0};
wuffs_base__reader1 src = {0}; wuffs_base__reader1 src = {0};
wuffs_gif__status status = wuffs_gif__lzw_decoder__decode(&dec, dst, src); wuffs_gif__status status = wuffs_gif__decoder__decode(&dec, dst, src);
if (status != WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED) { if (status != WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED) {
FAIL("status: got %d, want %d", status, FAIL("status: got %d, want %d", status,
WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED); WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED);
@@ -99,8 +72,8 @@ void test_basic_initializer_not_called() {
void test_basic_wuffs_version_bad() { void test_basic_wuffs_version_bad() {
CHECK_FOCUS(__func__); CHECK_FOCUS(__func__);
wuffs_gif__lzw_decoder dec; wuffs_gif__decoder dec;
wuffs_gif__lzw_decoder__initialize(&dec, 0, 0); // 0 is not WUFFS_VERSION. wuffs_gif__decoder__initialize(&dec, 0, 0); // 0 is not WUFFS_VERSION.
if (dec.private_impl.status != WUFFS_GIF__ERROR_BAD_WUFFS_VERSION) { if (dec.private_impl.status != WUFFS_GIF__ERROR_BAD_WUFFS_VERSION) {
FAIL("status: got %d, want %d", dec.private_impl.status, FAIL("status: got %d, want %d", dec.private_impl.status,
WUFFS_GIF__ERROR_BAD_WUFFS_VERSION); WUFFS_GIF__ERROR_BAD_WUFFS_VERSION);
@@ -110,15 +83,15 @@ void test_basic_wuffs_version_bad() {
void test_basic_wuffs_version_good() { void test_basic_wuffs_version_good() {
CHECK_FOCUS(__func__); CHECK_FOCUS(__func__);
wuffs_gif__lzw_decoder dec; wuffs_gif__decoder dec;
wuffs_gif__lzw_decoder__initialize(&dec, WUFFS_VERSION, 0); wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0);
if (dec.private_impl.magic != WUFFS_BASE__MAGIC) { if (dec.private_impl.magic != WUFFS_BASE__MAGIC) {
FAIL("magic: got %u, want %u", dec.private_impl.magic, WUFFS_BASE__MAGIC); FAIL("magic: got %u, want %u", dec.private_impl.magic, WUFFS_BASE__MAGIC);
return; return;
} }
if (dec.private_impl.f_literal_width != 8) { if (dec.private_impl.f_lzw.private_impl.f_literal_width != 8) {
FAIL("f_literal_width: got %u, want %u", dec.private_impl.f_literal_width, FAIL("f_literal_width: got %u, want %u",
8); dec.private_impl.f_lzw.private_impl.f_literal_width, 8);
return; return;
} }
} }
@@ -719,14 +692,13 @@ proc tests[] = {
// These basic tests are really testing the Wuffs compiler. They aren't // These basic tests are really testing the Wuffs compiler. They aren't
// specific to the std/gif code, but putting them here is as good as any // specific to the std/gif code, but putting them here is as good as any
// other place. // other place.
test_basic_bad_argument_out_of_range, // test_basic_bad_receiver, //
test_basic_bad_receiver, // test_basic_initializer_not_called, //
test_basic_initializer_not_called, // test_basic_wuffs_version_bad, //
test_basic_wuffs_version_bad, // test_basic_wuffs_version_good, //
test_basic_wuffs_version_good, // test_basic_status_is_error, //
test_basic_status_is_error, // test_basic_status_strings, //
test_basic_status_strings, // test_basic_sub_struct_initializer, //
test_basic_sub_struct_initializer, //
test_wuffs_lzw_decode_many_big_reads, // test_wuffs_lzw_decode_many_big_reads, //
test_wuffs_lzw_decode_many_small_writes_reads, // test_wuffs_lzw_decode_many_small_writes_reads, //