Implement gif.decoder.workbuf_len

Also relax decode_frame's workbuf length check from not-equals to
less-than.
This commit is contained in:
Nigel Tao committed 2018-11-17 21:52:26 +11:00
1 parent 4023294ba2
commit fc1002cb89
5 files changed
+34 -61

No files matched your search

+6 -5
View File
@@ -223,7 +223,8 @@ size_t print_color_art(wuffs_base__pixel_buffer* pb) {
// ---- // ----
const char* try_allocate(wuffs_base__image_config* ic) { const char* try_allocate(wuffs_gif__decoder* dec,
wuffs_base__image_config* ic) {
uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg); uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg);
uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg); uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg);
uint64_t num_pixels = ((uint64_t)width) * ((uint64_t)height); uint64_t num_pixels = ((uint64_t)width) * ((uint64_t)height);
@@ -249,7 +250,7 @@ const char* try_allocate(wuffs_base__image_config* ic) {
} }
workbuf = wuffs_base__malloc_slice_u8( workbuf = wuffs_base__malloc_slice_u8(
malloc, wuffs_base__image_config__workbuf_len(ic).max_incl); malloc, wuffs_gif__decoder__workbuf_len(dec).max_incl);
if (!workbuf.ptr) { if (!workbuf.ptr) {
return "could not allocate work buffer"; return "could not allocate work buffer";
} }
@@ -267,8 +268,8 @@ const char* try_allocate(wuffs_base__image_config* ic) {
return NULL; return NULL;
} }
const char* allocate(wuffs_base__image_config* ic) { const char* allocate(wuffs_gif__decoder* dec, wuffs_base__image_config* ic) {
const char* z = try_allocate(ic); const char* z = try_allocate(dec, ic);
if (z) { if (z) {
free(printbuf.ptr); free(printbuf.ptr);
printbuf = ((wuffs_base__slice_u8){}); printbuf = ((wuffs_base__slice_u8){});
@@ -321,7 +322,7 @@ const char* play() {
if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) { if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) {
return "image dimensions are too large"; return "image dimensions are too large";
} }
const char* msg = allocate(&ic); const char* msg = allocate(&dec, &ic);
if (msg) { if (msg) {
return msg; return msg;
} }
+1 -1
View File
@@ -73,7 +73,7 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
goto exit; goto exit;
} }
uint64_t n = wuffs_base__image_config__workbuf_len(&ic).max_incl; uint64_t n = wuffs_gif__decoder__workbuf_len(&dec).max_incl;
if (n > 64 * 1024 * 1024) { // Don't allocate more than 64 MiB. if (n > 64 * 1024 * 1024) { // Don't allocate more than 64 MiB.
ret = "image too large"; ret = "image too large";
goto exit; goto exit;
+4 -3
View File
@@ -7511,8 +7511,9 @@ wuffs_gif__decoder__workbuf_len(const wuffs_gif__decoder* self) {
return ((wuffs_base__range_ii_u64){}); return ((wuffs_base__range_ii_u64){});
} }
return wuffs_base__utility__make_range_ii_u64(&self->private_impl.f_util, 0, return wuffs_base__utility__make_range_ii_u64(
0); &self->private_impl.f_util, ((uint64_t)(self->private_impl.f_width)),
((uint64_t)(self->private_impl.f_width)));
} }
// -------- func gif.decoder.restart_frame // -------- func gif.decoder.restart_frame
@@ -7776,7 +7777,7 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
switch (coro_susp_point) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (((uint64_t)(a_workbuf.len)) != if (((uint64_t)(a_workbuf.len)) <
((uint64_t)(self->private_impl.f_width))) { ((uint64_t)(self->private_impl.f_width))) {
status = wuffs_base__error__bad_workbuf_length; status = wuffs_base__error__bad_workbuf_length;
goto exit; goto exit;
+5 -2
View File
@@ -146,6 +146,7 @@ pub func decoder.decode_image_config!??(dst nptr base.image_config, src base.io_
pixsub:0, pixsub:0,
width:this.width, width:this.width,
height:this.height, height:this.height,
// TODO: workbuf_len should be an aspect of the decoder, not the image_config.
workbuf_len0:this.width as base.u64, workbuf_len0:this.width as base.u64,
workbuf_len1:this.width as base.u64, workbuf_len1:this.width as base.u64,
first_frame_io_position:this.frame_config_io_position, first_frame_io_position:this.frame_config_io_position,
@@ -179,7 +180,9 @@ pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
} }
pub func decoder.workbuf_len() base.range_ii_u64 { pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl:0, max_incl:0) return this.util.make_range_ii_u64(
min_incl:this.width as base.u64,
max_incl:this.width as base.u64)
} }
pub func decoder.restart_frame!??(index base.u64, io_position base.u64) { pub func decoder.restart_frame!??(index base.u64, io_position base.u64) {
@@ -251,7 +254,7 @@ pri func decoder.skip_frame!??(src base.io_reader) {
// TODO: honor args.opts. // TODO: honor args.opts.
pub func decoder.decode_frame!??(dst ptr base.pixel_buffer, src base.io_reader, workbuf slice base.u8, opts nptr base.decode_frame_options) { pub func decoder.decode_frame!??(dst ptr base.pixel_buffer, src base.io_reader, workbuf slice base.u8, opts nptr base.decode_frame_options) {
if args.workbuf.length() != (this.width as base.u64) { if args.workbuf.length() < (this.width as base.u64) {
return status "?bad workbuf length" return status "?bad workbuf length"
} }
if this.call_sequence != 2 { if this.call_sequence != 2 {
+18 -50
View File
@@ -211,15 +211,6 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst,
return z; return z;
} }
uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl;
if (workbuf_len > BUFFER_SIZE) {
return "work buffer size is too large";
}
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){
.ptr = global_work_array,
.len = workbuf_len,
});
while (true) { while (true) {
z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader);
if (z == wuffs_base__warning__end_of_data) { if (z == wuffs_base__warning__end_of_data) {
@@ -228,7 +219,8 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst,
return z; return z;
} }
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
global_work_slice, NULL);
if (z) { if (z) {
return z; return z;
} }
@@ -290,10 +282,6 @@ const char* do_test_wuffs_gif_decode(const char* filename,
RETURN_FAIL("height: got %" PRIu32 ", want 120", RETURN_FAIL("height: got %" PRIu32 ", want 120",
wuffs_base__pixel_config__height(&ic.pixcfg)); wuffs_base__pixel_config__height(&ic.pixcfg));
} }
if (wuffs_base__image_config__workbuf_len(&ic).max_incl != 160) {
RETURN_FAIL("workbuf_len: got %" PRIu64 ", want 160",
wuffs_base__image_config__workbuf_len(&ic).max_incl);
}
if (!wuffs_base__image_config__first_frame_is_opaque(&ic)) { if (!wuffs_base__image_config__first_frame_is_opaque(&ic)) {
RETURN_FAIL("first_frame_is_opaque: got false, want true"); RETURN_FAIL("first_frame_is_opaque: got false, want true");
} }
@@ -309,10 +297,10 @@ const char* do_test_wuffs_gif_decode(const char* filename,
} }
} }
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ if (wuffs_gif__decoder__workbuf_len(&dec).min_incl != 160) {
.ptr = global_work_array, RETURN_FAIL("workbuf_len: got %" PRIu64 ", want 160",
.len = 160, wuffs_gif__decoder__workbuf_len(&dec).min_incl);
}); }
int num_iters = 0; int num_iters = 0;
while (true) { while (true) {
@@ -349,8 +337,8 @@ const char* do_test_wuffs_gif_decode(const char* filename,
} }
size_t old_ri = src.meta.ri; size_t old_ri = src.meta.ri;
const char* z = const char* z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); global_work_slice, NULL);
if (!z) { if (!z) {
break; break;
@@ -421,7 +409,8 @@ const char* do_test_wuffs_gif_decode(const char* filename,
RETURN_FAIL("decode_frame returned \"ok\" but src was exhausted"); RETURN_FAIL("decode_frame returned \"ok\" but src was exhausted");
} }
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
global_work_slice, NULL);
if (z != wuffs_base__warning__end_of_data) { if (z != wuffs_base__warning__end_of_data) {
RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z, RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z,
wuffs_base__warning__end_of_data); wuffs_base__warning__end_of_data);
@@ -496,15 +485,6 @@ const char* do_test_wuffs_gif_decode_animated(
RETURN_FAIL("decode_image_config: got \"%s\"", z); RETURN_FAIL("decode_image_config: got \"%s\"", z);
} }
uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl;
if (workbuf_len > BUFFER_SIZE) {
return "work buffer size is too large";
}
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){
.ptr = global_work_array,
.len = workbuf_len,
});
uint32_t got_num_loops = wuffs_gif__decoder__num_animation_loops(&dec); uint32_t got_num_loops = wuffs_gif__decoder__num_animation_loops(&dec);
if (got_num_loops != want_num_loops) { if (got_num_loops != want_num_loops) {
RETURN_FAIL("num_loops: got %" PRIu32 ", want %" PRIu32, got_num_loops, RETURN_FAIL("num_loops: got %" PRIu32 ", want %" PRIu32, got_num_loops,
@@ -539,7 +519,8 @@ const char* do_test_wuffs_gif_decode_animated(
} }
} }
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
global_work_slice, NULL);
if (z) { if (z) {
RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z);
} }
@@ -548,7 +529,8 @@ const char* do_test_wuffs_gif_decode_animated(
// There should be no more frames, no matter how many times we call // There should be no more frames, no matter how many times we call
// decode_frame. // decode_frame.
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
global_work_slice, NULL);
if (z != wuffs_base__warning__end_of_data) { if (z != wuffs_base__warning__end_of_data) {
RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z, RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z,
wuffs_base__warning__end_of_data); wuffs_base__warning__end_of_data);
@@ -641,15 +623,6 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() {
RETURN_FAIL("set_from_slice: \"%s\"", z); RETURN_FAIL("set_from_slice: \"%s\"", z);
} }
uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl;
if (workbuf_len > BUFFER_SIZE) {
RETURN_FAIL("work buffer size is too large");
}
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){
.ptr = global_work_array,
.len = workbuf_len,
});
// See test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt // See test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt
// for the want_frame_config_bounds and want_pixel_indexes source. // for the want_frame_config_bounds and want_pixel_indexes source.
@@ -703,8 +676,8 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() {
} }
} }
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
NULL); global_work_slice, NULL);
if (z) { if (z) {
RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z);
} }
@@ -884,11 +857,6 @@ const char* do_test_wuffs_gif_num_decoded(bool frame_config) {
} }
} }
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){
.ptr = global_work_array,
.len = 64,
});
const char* method = frame_config ? "decode_frame_config" : "decode_frame"; const char* method = frame_config ? "decode_frame_config" : "decode_frame";
bool end_of_data = false; bool end_of_data = false;
uint64_t want = 0; uint64_t want = 0;
@@ -909,8 +877,8 @@ const char* do_test_wuffs_gif_num_decoded(bool frame_config) {
if (frame_config) { if (frame_config) {
z = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader); z = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader);
} else { } else {
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
NULL); global_work_slice, NULL);
} }
if (!z) { if (!z) {