Add wuffs_base__malloc_slice_uxx functions

This commit is contained in:
Nigel Tao committed 2018-09-09 21:31:37 +10:00
1 parent 399a7af6ef
commit 180ce5e481
5 files changed
+229 -103

No files matched your search

@@ -286,6 +286,8 @@ wuffs_base__u64__byte_swapped(uint64_t x) {
// WUFFS_BASE__SLICE is a 1-dimensional buffer.
//
// len measures a number of elements, not necessarily a size in bytes.
//
// A value with all fields NULL or zero is a valid, empty slice.
#define WUFFS_BASE__SLICE(T) \
struct { \
@@ -295,6 +297,9 @@ wuffs_base__u64__byte_swapped(uint64_t x) {
// WUFFS_BASE__TABLE is a 2-dimensional buffer.
//
// width height, and stride measure a number of elements, not necessarily a
// size in bytes.
//
// A value with all fields NULL or zero is a valid, empty table.
#define WUFFS_BASE__TABLE(T) \
struct { \
@@ -1086,6 +1091,77 @@ wuffs_base__io_buffer__struct::writer() {
#endif // __cplusplus
// ---------------- Memory Allocation
// The memory allocation related functions in this section aren't used by Wuffs
// per se, but they may be helpful to the code that uses Wuffs.
// wuffs_base__malloc_slice_uxx wraps calling a malloc-like function, except
// that it takes a uint64_t number of elements instead of a size_t size in
// bytes, and it returns a slice (a pointer and a length) instead of just a
// pointer.
//
// You can pass the C stdlib's malloc as the malloc_func.
//
// It returns an empty slice (containing a NULL ptr field) if (num_uxx *
// sizeof(uintxx_t)) would overflow SIZE_MAX.
static inline wuffs_base__slice_u8 //
wuffs_base__malloc_slice_u8(void* (*malloc_func)(size_t), uint64_t num_u8) {
if (malloc_func && (num_u8 <= (SIZE_MAX / sizeof(uint8_t)))) {
void* p = (*malloc_func)(num_u8 * sizeof(uint8_t));
if (p) {
return ((wuffs_base__slice_u8){
.ptr = p,
.len = num_u8,
});
}
}
return ((wuffs_base__slice_u8){});
}
static inline wuffs_base__slice_u16 //
wuffs_base__malloc_slice_u16(void* (*malloc_func)(size_t), uint64_t num_u16) {
if (malloc_func && (num_u16 <= (SIZE_MAX / sizeof(uint16_t)))) {
void* p = (*malloc_func)(num_u16 * sizeof(uint16_t));
if (p) {
return ((wuffs_base__slice_u16){
.ptr = p,
.len = num_u16,
});
}
}
return ((wuffs_base__slice_u16){});
}
static inline wuffs_base__slice_u32 //
wuffs_base__malloc_slice_u32(void* (*malloc_func)(size_t), uint64_t num_u32) {
if (malloc_func && (num_u32 <= (SIZE_MAX / sizeof(uint32_t)))) {
void* p = (*malloc_func)(num_u32 * sizeof(uint32_t));
if (p) {
return ((wuffs_base__slice_u32){
.ptr = p,
.len = num_u32,
});
}
}
return ((wuffs_base__slice_u32){});
}
static inline wuffs_base__slice_u64 //
wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
if (malloc_func && (num_u64 <= (SIZE_MAX / sizeof(uint64_t)))) {
void* p = (*malloc_func)(num_u64 * sizeof(uint64_t));
if (p) {
return ((wuffs_base__slice_u64){
.ptr = p,
.len = num_u64,
});
}
}
return ((wuffs_base__slice_u64){});
}
#ifdef __cplusplus
} // extern "C"
#endif
+7 -3
View File
@@ -33,8 +33,8 @@ const baseBasePublicH = "" +
"" +
"// --------\n\n// Clang also defines \"__GNUC__\".\n\nstatic inline uint16_t //\nwuffs_base__u16__byte_swapped(uint16_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap16(x);\n#else\n return (x >> 8) | (x << 8);\n#endif\n}\n\nstatic inline uint32_t //\nwuffs_base__u32__byte_swapped(uint32_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap32(x);\n#else\n static const uint32_t mask8 = 0x00FF00FF;\n x = ((x >> 8) & mask8) | ((x & mask8) << 8);\n return (x >> 16) | (x << 16);\n#endif\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__byte_swapped(uint64_t x) {\n#if defined(__GNUC__)\n return __builtin_bswap64(x);\n#else\n static const uint64_t mask8 = 0x00FF00FF00FF00FF;\n static const uint64_t mask16 = 0x0000FFFF0000FFFF;\n x = ((x >> 8) & mask8) | ((x & mask8) << 8);\n x = ((x >> 16) & mask16) | ((x & mask16) << 16);\n return (x >> 32) | (x << 32);\n#endif\n}\n\n" +
"" +
"// ---------------- Slices and Tables\n\n// WUFFS_BASE__SLICE is a 1-dimensional buffer.\n//\n// A value with all fields NULL or zero is a valid, empty slice.\n#define WUFFS_BASE__SLICE(T) \\\n struct { \\\n T* ptr; \\\n size_t len; \\\n }\n\n// WUFFS_BASE__TABLE is a 2-dimensional buffer.\n//\n// A value with all fields NULL or zero is a valid, empty table.\n#define WUFFS_BASE__TABLE(T) \\\n struct { \\\n T* ptr; \\\n size_t width; \\\n size_t height; \\\n size_t stride; \\\n }\n\ntypedef WUFFS_BASE__SLICE(uint8_t) wuffs_base__slice_u8;\ntypedef WUFFS_BASE__SLICE(uint16_t) wuffs_base__slice_u16;\ntypedef WUFFS_BASE__SLICE(uint32_t) wuffs_base__slice_u32;\ntypedef WUFFS_BASE__SLICE(uint64_t) wuffs_base__slice_u64;\n\ntypedef WUFFS_BASE__TABLE(uint8_t) wuffs_base__table_u8;\ntypedef WUFFS_BASE__TABLE(uint16_t) wuffs_base__table_u16;\ntypedef WUFFS_BASE__TABLE(uint32_t) wuffs_base__table_u32;\ntypedef WUFFS_BASE__TAB" +
"LE(uint64_t) wuffs_base__table_u64;\n\n" +
"// ---------------- Slices and Tables\n\n// WUFFS_BASE__SLICE is a 1-dimensional buffer.\n//\n// len measures a number of elements, not necessarily a size in bytes.\n//\n// A value with all fields NULL or zero is a valid, empty slice.\n#define WUFFS_BASE__SLICE(T) \\\n struct { \\\n T* ptr; \\\n size_t len; \\\n }\n\n// WUFFS_BASE__TABLE is a 2-dimensional buffer.\n//\n// width height, and stride measure a number of elements, not necessarily a\n// size in bytes.\n//\n// A value with all fields NULL or zero is a valid, empty table.\n#define WUFFS_BASE__TABLE(T) \\\n struct { \\\n T* ptr; \\\n size_t width; \\\n size_t height; \\\n size_t stride; \\\n }\n\ntypedef WUFFS_BASE__SLICE(uint8_t) wuffs_base__slice_u8;\ntypedef WUFFS_BASE__SLICE(uint16_t) wuffs_base__slice_u16;\ntypedef WUFFS_BASE__SLICE(uint32_t) wuffs_base__slice_u32;\ntypedef WUFFS_BASE__SLICE(uint64_t) wuffs_base__slice_u64;\n\ntypedef WUFFS_BASE__TABLE(u" +
"int8_t) wuffs_base__table_u8;\ntypedef WUFFS_BASE__TABLE(uint16_t) wuffs_base__table_u16;\ntypedef WUFFS_BASE__TABLE(uint32_t) wuffs_base__table_u32;\ntypedef WUFFS_BASE__TABLE(uint64_t) wuffs_base__table_u64;\n\n" +
"" +
"// ---------------- Ranges and Rects\n\n// Ranges are either inclusive (\"range_ii\") or exclusive (\"range_ie\") on the\n// high end. Both the \"ii\" and \"ie\" flavors are useful in practice.\n//\n// The \"ei\" and \"ee\" flavors also exist in theory, but aren't widely used. In\n// Wuffs, the low end is always inclusive.\n//\n// The \"ii\" (closed interval) flavor is useful when refining e.g. \"the set of\n// all uint32_t values\" to a contiguous subset: \"uint32_t values in the closed\n// interval [M, N]\", for uint32_t values M and N. An unrefined type (in other\n// words, the set of all uint32_t values) is not representable in the \"ie\"\n// flavor because if N equals ((1<<32) - 1) then (N + 1) will overflow.\n//\n// On the other hand, the \"ie\" (half-open interval) flavor is recommended by\n// Dijkstra's \"Why numbering should start at zero\" at\n// http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF and a further\n// discussion of motivating rationale is at\n// https://www.quora.com/Why-are-Python-ranges-half-open-exclusive-instead-of-close" +
"d-inclusive\n//\n// For example, with \"ie\", the number of elements in \"uint32_t values in the\n// half-open interval [M, N)\" is equal to max(0, N-M). Furthermore, that number\n// of elements (in one dimension, a length, in two dimensions, a width or\n// height) is itself representable as a uint32_t without overflow, again for\n// uint32_t values M and N. In the contrasting \"ii\" flavor, the length of the\n// closed interval [0, (1<<32) - 1] is 1<<32, which cannot be represented as a\n// uint32_t. In Wuffs, because of this potential overflow, the \"ie\" flavor has\n// length / width / height methods, but the \"ii\" flavor does not.\n//\n// It is valid for min > max (for range_ii) or for min >= max (for range_ie),\n// in which case the range is empty. There are multiple representations of an\n// empty range.\n\ntypedef struct wuffs_base__range_ii_u32__struct {\n uint32_t min_incl;\n uint32_t max_incl;\n\n#ifdef __cplusplus\n inline bool is_empty();\n inline bool equals(wuffs_base__range_ii_u32__struct s);\n inline bool contains(uint" +
@@ -68,7 +68,11 @@ const baseBasePublicH = "" +
"// ---------------- I/O\n\nstruct wuffs_base__io_buffer__struct;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* mark;\n uint8_t* limit;\n } private_impl;\n} wuffs_base__io_reader;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* mark;\n uint8_t* limit;\n } private_impl;\n} wuffs_base__io_writer;\n\n// wuffs_base__io_buffer is a 1-dimensio" +
"nal buffer (a pointer and length), plus\n// additional indexes into that buffer, plus a buffer position and an opened /\n// closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct wuffs_base__io_buffer__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 uint64_t pos; // Position of the buffer start relative to the stream start.\n bool closed; // No further writes are expected.\n\n#ifdef __cplusplus\n inline void compact();\n inline uint64_t io_position_reader();\n inline uint64_t io_position_writer();\n inline wuffs_base__io_reader reader();\n inline wuffs_base__io_writer writer();\n#endif // __cplusplus\n\n} wuffs_base__io_buffer;\n\n// wuffs_base__io_buffer__compact moves any written but unread bytes to the\n// start of the buffer.\nstatic inline void //\nwuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {\n if (!buf || (buf->ri == 0)) {\n " +
" return;\n }\n buf->pos = wuffs_base__u64__sat_add(buf->pos, buf->ri);\n size_t n = buf->wi - buf->ri;\n if (n != 0) {\n memmove(buf->ptr, buf->ptr + buf->ri, n);\n }\n buf->wi = n;\n buf->ri = 0;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_buffer__io_position_reader(wuffs_base__io_buffer* buf) {\n return buf ? wuffs_base__u64__sat_add(buf->pos, buf->ri) : 0;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_buffer__io_position_writer(wuffs_base__io_buffer* buf) {\n return buf ? wuffs_base__u64__sat_add(buf->pos, buf->wi) : 0;\n}\n\nstatic inline wuffs_base__io_reader //\nwuffs_base__io_buffer__reader(wuffs_base__io_buffer* buf) {\n wuffs_base__io_reader ret = ((wuffs_base__io_reader){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\nstatic inline wuffs_base__io_writer //\nwuffs_base__io_buffer__writer(wuffs_base__io_buffer* buf) {\n wuffs_base__io_writer ret = ((wuffs_base__io_writer){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__io_buffer__struct::compact() {" +
"\n wuffs_base__io_buffer__compact(this);\n}\n\ninline uint64_t //\nwuffs_base__io_buffer__struct::io_position_reader() {\n return wuffs_base__io_buffer__io_position_reader(this);\n}\n\ninline uint64_t //\nwuffs_base__io_buffer__struct::io_position_writer() {\n return wuffs_base__io_buffer__io_position_writer(this);\n}\n\ninline wuffs_base__io_reader //\nwuffs_base__io_buffer__struct::reader() {\n return wuffs_base__io_buffer__reader(this);\n}\n\ninline wuffs_base__io_writer //\nwuffs_base__io_buffer__struct::writer() {\n return wuffs_base__io_buffer__writer(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
"\n wuffs_base__io_buffer__compact(this);\n}\n\ninline uint64_t //\nwuffs_base__io_buffer__struct::io_position_reader() {\n return wuffs_base__io_buffer__io_position_reader(this);\n}\n\ninline uint64_t //\nwuffs_base__io_buffer__struct::io_position_writer() {\n return wuffs_base__io_buffer__io_position_writer(this);\n}\n\ninline wuffs_base__io_reader //\nwuffs_base__io_buffer__struct::reader() {\n return wuffs_base__io_buffer__reader(this);\n}\n\ninline wuffs_base__io_writer //\nwuffs_base__io_buffer__struct::writer() {\n return wuffs_base__io_buffer__writer(this);\n}\n\n#endif // __cplusplus\n\n" +
"" +
"// ---------------- Memory Allocation\n\n// The memory allocation related functions in this section aren't used by Wuffs\n// per se, but they may be helpful to the code that uses Wuffs.\n\n// wuffs_base__malloc_slice_uxx wraps calling a malloc-like function, except\n// that it takes a uint64_t number of elements instead of a size_t size in\n// bytes, and it returns a slice (a pointer and a length) instead of just a\n// pointer.\n//\n// You can pass the C stdlib's malloc as the malloc_func.\n//\n// It returns an empty slice (containing a NULL ptr field) if (num_uxx *\n// sizeof(uintxx_t)) would overflow SIZE_MAX.\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__malloc_slice_u8(void* (*malloc_func)(size_t), uint64_t num_u8) {\n if (malloc_func && (num_u8 <= (SIZE_MAX / sizeof(uint8_t)))) {\n void* p = (*malloc_func)(num_u8 * sizeof(uint8_t));\n if (p) {\n return ((wuffs_base__slice_u8){\n .ptr = p,\n .len = num_u8,\n });\n }\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_bas" +
"e__slice_u16 //\nwuffs_base__malloc_slice_u16(void* (*malloc_func)(size_t), uint64_t num_u16) {\n if (malloc_func && (num_u16 <= (SIZE_MAX / sizeof(uint16_t)))) {\n void* p = (*malloc_func)(num_u16 * sizeof(uint16_t));\n if (p) {\n return ((wuffs_base__slice_u16){\n .ptr = p,\n .len = num_u16,\n });\n }\n }\n return ((wuffs_base__slice_u16){});\n}\n\nstatic inline wuffs_base__slice_u32 //\nwuffs_base__malloc_slice_u32(void* (*malloc_func)(size_t), uint64_t num_u32) {\n if (malloc_func && (num_u32 <= (SIZE_MAX / sizeof(uint32_t)))) {\n void* p = (*malloc_func)(num_u32 * sizeof(uint32_t));\n if (p) {\n return ((wuffs_base__slice_u32){\n .ptr = p,\n .len = num_u32,\n });\n }\n }\n return ((wuffs_base__slice_u32){});\n}\n\nstatic inline wuffs_base__slice_u64 //\nwuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {\n if (malloc_func && (num_u64 <= (SIZE_MAX / sizeof(uint64_t)))) {\n void* p = (*malloc_func)(num_u64 * sizeof(uint64_t)" +
");\n if (p) {\n return ((wuffs_base__slice_u64){\n .ptr = p,\n .len = num_u64,\n });\n }\n }\n return ((wuffs_base__slice_u64){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
""
const baseBasePrivateH = "" +
+56 -72
View File
@@ -78,18 +78,13 @@ uint64_t micros_since_start(struct timespec* now) {
uint8_t src_buffer[SRC_BUFFER_SIZE] = {0};
size_t src_len = 0;
wuffs_base__color_u32argb* dst_buffer = NULL;
wuffs_base__color_u32argb* curr_dst_buffer = NULL;
wuffs_base__color_u32argb* prev_dst_buffer = NULL;
size_t dst_len; // Length in bytes.
uint8_t* image_buffer = NULL;
size_t image_len = 0;
uint8_t* work_buffer = NULL;
size_t work_len = 0;
uint8_t* print_buffer = NULL;
size_t print_len = 0;
wuffs_base__slice_u8 pixbuf = ((wuffs_base__slice_u8){});
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){});
wuffs_base__slice_u8 printbuf = ((wuffs_base__slice_u8){});
bool first_play = true;
uint32_t num_loops_remaining = 0;
@@ -141,7 +136,8 @@ void restore_background(wuffs_base__pixel_buffer* pb,
size_t y;
for (y = bounds.min_incl_y; y < bounds.max_excl_y; y++) {
size_t x;
wuffs_base__color_u32argb* d = dst_buffer + (y * width) + bounds.min_incl_x;
wuffs_base__color_u32argb* d =
curr_dst_buffer + (y * width) + bounds.min_incl_x;
for (x = bounds.min_incl_x; x < bounds.max_excl_x; x++) {
*d++ = 0;
}
@@ -158,7 +154,8 @@ void compose(wuffs_base__pixel_buffer* pb, wuffs_base__rect_ie_u32 bounds) {
size_t y;
for (y = bounds.min_incl_y; y < bounds.max_excl_y; y++) {
size_t x;
wuffs_base__color_u32argb* d = dst_buffer + (y * width) + bounds.min_incl_x;
wuffs_base__color_u32argb* d =
curr_dst_buffer + (y * width) + bounds.min_incl_x;
uint8_t* s = tab.ptr + (y * tab.stride) + bounds.min_incl_x;
for (x = bounds.min_incl_x; x < bounds.max_excl_x; x++) {
uint32_t index = *s++;
@@ -175,8 +172,8 @@ size_t print_ascii_art(wuffs_base__pixel_buffer* pb) {
uint32_t width = wuffs_base__pixel_config__width(&pb->pixcfg);
uint32_t height = wuffs_base__pixel_config__height(&pb->pixcfg);
wuffs_base__color_u32argb* d = dst_buffer;
uint8_t* p = print_buffer;
wuffs_base__color_u32argb* d = curr_dst_buffer;
uint8_t* p = printbuf.ptr;
*p++ = '\n';
uint32_t y;
for (y = 0; y < height; y++) {
@@ -194,15 +191,15 @@ size_t print_ascii_art(wuffs_base__pixel_buffer* pb) {
}
*p++ = '\n';
}
return p - print_buffer;
return p - printbuf.ptr;
}
size_t print_color_art(wuffs_base__pixel_buffer* pb) {
uint32_t width = wuffs_base__pixel_config__width(&pb->pixcfg);
uint32_t height = wuffs_base__pixel_config__height(&pb->pixcfg);
wuffs_base__color_u32argb* d = dst_buffer;
uint8_t* p = print_buffer;
wuffs_base__color_u32argb* d = curr_dst_buffer;
uint8_t* p = printbuf.ptr;
*p++ = '\n';
p += sprintf((char*)p, "%s", reset_color);
uint32_t y;
@@ -220,12 +217,12 @@ size_t print_color_art(wuffs_base__pixel_buffer* pb) {
*p++ = '\n';
}
p += sprintf((char*)p, "%s", reset_color);
return p - print_buffer;
return p - printbuf.ptr;
}
// ----
const char* allocate(wuffs_base__image_config* ic) {
const char* try_allocate(wuffs_base__image_config* ic) {
uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg);
uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg);
uint64_t num_pixels = ((uint64_t)width) * ((uint64_t)height);
@@ -234,64 +231,59 @@ const char* allocate(wuffs_base__image_config* ic) {
}
dst_len = num_pixels * sizeof(wuffs_base__color_u32argb);
dst_buffer = (wuffs_base__color_u32argb*)malloc(dst_len);
if (!dst_buffer) {
return "could not allocate dst buffer";
curr_dst_buffer = (wuffs_base__color_u32argb*)malloc(dst_len);
if (!curr_dst_buffer) {
return "could not allocate curr-dst buffer";
}
prev_dst_buffer = (wuffs_base__color_u32argb*)malloc(dst_len);
if (!prev_dst_buffer) {
free(dst_buffer);
dst_buffer = NULL;
return "could not allocate prev-dst buffer";
}
image_len = wuffs_base__pixel_config__pixbuf_len(&ic->pixcfg);
image_buffer = malloc(image_len);
if (!image_buffer) {
free(prev_dst_buffer);
prev_dst_buffer = NULL;
free(dst_buffer);
dst_buffer = NULL;
return "could not allocate image buffer";
pixbuf = wuffs_base__malloc_slice_u8(
malloc, wuffs_base__pixel_config__pixbuf_len(&ic->pixcfg));
if (!pixbuf.ptr) {
return "could not allocate pixel buffer";
}
uint64_t work_len_u64 = wuffs_base__image_config__workbuf_len(ic).max_incl;
if (work_len_u64 <= SIZE_MAX) {
work_buffer = malloc(work_len_u64);
work_len = work_len_u64;
}
if (!work_buffer) {
free(image_buffer);
image_buffer = NULL;
free(prev_dst_buffer);
prev_dst_buffer = NULL;
free(dst_buffer);
dst_buffer = NULL;
return "could not allocate image buffer";
workbuf = wuffs_base__malloc_slice_u8(
malloc, wuffs_base__image_config__workbuf_len(ic).max_incl);
if (!workbuf.ptr) {
return "could not allocate work buffer";
}
uint64_t plen = 1 + ((uint64_t)(width) + 1) * (uint64_t)(height);
uint64_t bytes_per_print_pixel = color_flag ? BYTES_PER_COLOR_PIXEL : 1;
if (plen <= ((uint64_t)SIZE_MAX) / bytes_per_print_pixel) {
print_len = (size_t)(plen * bytes_per_print_pixel);
print_buffer = malloc(print_len);
printbuf =
wuffs_base__malloc_slice_u8(malloc, plen * bytes_per_print_pixel);
}
if (!print_buffer) {
free(work_buffer);
work_buffer = NULL;
free(image_buffer);
image_buffer = NULL;
free(prev_dst_buffer);
prev_dst_buffer = NULL;
free(dst_buffer);
dst_buffer = NULL;
if (!printbuf.ptr) {
return "could not allocate print buffer";
}
return NULL;
}
const char* allocate(wuffs_base__image_config* ic) {
const char* z = try_allocate(ic);
if (z) {
free(printbuf.ptr);
printbuf = ((wuffs_base__slice_u8){});
free(workbuf.ptr);
workbuf = ((wuffs_base__slice_u8){});
free(pixbuf.ptr);
pixbuf = ((wuffs_base__slice_u8){});
free(prev_dst_buffer);
prev_dst_buffer = NULL;
free(curr_dst_buffer);
curr_dst_buffer = NULL;
dst_len = 0;
}
return z;
}
const char* play() {
wuffs_gif__decoder dec = ((wuffs_gif__decoder){});
wuffs_base__status z =
@@ -304,6 +296,7 @@ const char* play() {
.ptr = src_buffer,
.len = src_len,
.wi = src_len,
.ri = 0,
.pos = 0,
.closed = true,
});
@@ -328,16 +321,12 @@ const char* play() {
if (msg) {
return msg;
}
z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg,
((wuffs_base__slice_u8){
.ptr = image_buffer,
.len = image_len,
}));
z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, pixbuf);
if (z) {
return z;
}
memset(image_buffer, 0, image_len);
memset(dst_buffer, 0, dst_len);
memset(pixbuf.ptr, 0, pixbuf.len);
memset(curr_dst_buffer, 0, dst_len);
num_loops_remaining = wuffs_base__image_config__num_loops(&ic);
}
@@ -354,17 +343,12 @@ const char* play() {
switch (wuffs_base__frame_config__disposal(&fc)) {
case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS: {
memcpy(prev_dst_buffer, dst_buffer, dst_len);
memcpy(prev_dst_buffer, curr_dst_buffer, dst_len);
break;
}
}
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
((wuffs_base__slice_u8){
.ptr = work_buffer,
.len = work_len,
}),
NULL);
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL);
if (z) {
if (z == wuffs_base__warning__end_of_data) {
break;
@@ -382,8 +366,8 @@ const char* play() {
break;
}
case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS: {
wuffs_base__color_u32argb* swap = dst_buffer;
dst_buffer = prev_dst_buffer;
wuffs_base__color_u32argb* swap = curr_dst_buffer;
curr_dst_buffer = prev_dst_buffer;
prev_dst_buffer = swap;
break;
}
@@ -408,7 +392,7 @@ const char* play() {
}
#endif
ignore_return_value(write(stdout_fd, print_buffer, n));
ignore_return_value(write(stdout_fd, printbuf.ptr, n));
cumulative_delay_micros +=
(1000 * wuffs_base__frame_config__duration(&fc)) /
+14 -28
View File
@@ -48,8 +48,8 @@ It should print "PASS", amongst other information, and exit(0).
const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
const char* ret = NULL;
void* pixbuf_ptr = NULL;
void* workbuf_ptr = NULL;
wuffs_base__slice_u8 pixbuf = ((wuffs_base__slice_u8){});
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){});
// Use a {} code block so that "goto exit" doesn't trigger "jump bypasses
// variable initialization" warnings.
@@ -73,36 +73,30 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
goto exit;
}
uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl;
// Don't try to allocate more than 64 MiB.
if ((workbuf_len > 64 * 1024 * 1024) || (workbuf_len > SIZE_MAX)) {
uint64_t n = wuffs_base__image_config__workbuf_len(&ic).max_incl;
if (n > 64 * 1024 * 1024) { // Don't allocate more than 64 MiB.
ret = "image too large";
goto exit;
}
workbuf_ptr = malloc(workbuf_len);
if (!workbuf_ptr) {
workbuf = wuffs_base__malloc_slice_u8(malloc, n);
if (!workbuf.ptr) {
ret = "out of memory";
goto exit;
}
size_t pixbuf_len = wuffs_base__pixel_config__pixbuf_len(&ic.pixcfg);
// Don't try to allocate more than 64 MiB.
if (pixbuf_len > 64 * 1024 * 1024) {
n = wuffs_base__pixel_config__pixbuf_len(&ic.pixcfg);
if (n > 64 * 1024 * 1024) { // Don't allocate more than 64 MiB.
ret = "image too large";
goto exit;
}
pixbuf_ptr = malloc(pixbuf_len);
if (!pixbuf_ptr) {
pixbuf = wuffs_base__malloc_slice_u8(malloc, n);
if (!pixbuf.ptr) {
ret = "out of memory";
goto exit;
}
wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){});
z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg,
((wuffs_base__slice_u8){
.ptr = pixbuf_ptr,
.len = pixbuf_len,
}));
z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, pixbuf);
if (z) {
ret = z;
goto exit;
@@ -110,11 +104,7 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
bool seen_ok = false;
while (true) {
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader,
((wuffs_base__slice_u8){
.ptr = workbuf_ptr,
.len = workbuf_len,
}),
z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf,
NULL);
if (z) {
if ((z != wuffs_base__warning__end_of_data) || !seen_ok) {
@@ -127,11 +117,7 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
}
exit:
if (workbuf_ptr) {
free(workbuf_ptr);
}
if (pixbuf_ptr) {
free(pixbuf_ptr);
}
free(workbuf.ptr);
free(pixbuf.ptr);
return ret;
}
+76
View File
@@ -324,6 +324,8 @@ wuffs_base__u64__byte_swapped(uint64_t x) {
// WUFFS_BASE__SLICE is a 1-dimensional buffer.
//
// len measures a number of elements, not necessarily a size in bytes.
//
// A value with all fields NULL or zero is a valid, empty slice.
#define WUFFS_BASE__SLICE(T) \
struct { \
@@ -333,6 +335,9 @@ wuffs_base__u64__byte_swapped(uint64_t x) {
// WUFFS_BASE__TABLE is a 2-dimensional buffer.
//
// width height, and stride measure a number of elements, not necessarily a
// size in bytes.
//
// A value with all fields NULL or zero is a valid, empty table.
#define WUFFS_BASE__TABLE(T) \
struct { \
@@ -1124,6 +1129,77 @@ wuffs_base__io_buffer__struct::writer() {
#endif // __cplusplus
// ---------------- Memory Allocation
// The memory allocation related functions in this section aren't used by Wuffs
// per se, but they may be helpful to the code that uses Wuffs.
// wuffs_base__malloc_slice_uxx wraps calling a malloc-like function, except
// that it takes a uint64_t number of elements instead of a size_t size in
// bytes, and it returns a slice (a pointer and a length) instead of just a
// pointer.
//
// You can pass the C stdlib's malloc as the malloc_func.
//
// It returns an empty slice (containing a NULL ptr field) if (num_uxx *
// sizeof(uintxx_t)) would overflow SIZE_MAX.
static inline wuffs_base__slice_u8 //
wuffs_base__malloc_slice_u8(void* (*malloc_func)(size_t), uint64_t num_u8) {
if (malloc_func && (num_u8 <= (SIZE_MAX / sizeof(uint8_t)))) {
void* p = (*malloc_func)(num_u8 * sizeof(uint8_t));
if (p) {
return ((wuffs_base__slice_u8){
.ptr = p,
.len = num_u8,
});
}
}
return ((wuffs_base__slice_u8){});
}
static inline wuffs_base__slice_u16 //
wuffs_base__malloc_slice_u16(void* (*malloc_func)(size_t), uint64_t num_u16) {
if (malloc_func && (num_u16 <= (SIZE_MAX / sizeof(uint16_t)))) {
void* p = (*malloc_func)(num_u16 * sizeof(uint16_t));
if (p) {
return ((wuffs_base__slice_u16){
.ptr = p,
.len = num_u16,
});
}
}
return ((wuffs_base__slice_u16){});
}
static inline wuffs_base__slice_u32 //
wuffs_base__malloc_slice_u32(void* (*malloc_func)(size_t), uint64_t num_u32) {
if (malloc_func && (num_u32 <= (SIZE_MAX / sizeof(uint32_t)))) {
void* p = (*malloc_func)(num_u32 * sizeof(uint32_t));
if (p) {
return ((wuffs_base__slice_u32){
.ptr = p,
.len = num_u32,
});
}
}
return ((wuffs_base__slice_u32){});
}
static inline wuffs_base__slice_u64 //
wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
if (malloc_func && (num_u64 <= (SIZE_MAX / sizeof(uint64_t)))) {
void* p = (*malloc_func)(num_u64 * sizeof(uint64_t));
if (p) {
return ((wuffs_base__slice_u64){
.ptr = p,
.len = num_u64,
});
}
}
return ((wuffs_base__slice_u64){});
}
#ifdef __cplusplus
} // extern "C"
#endif