diff --git a/doc/changelog.md b/doc/changelog.md index 46572ed4..e924a8a5 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -23,6 +23,7 @@ - Added `example/sdl-imageviewer`. - Added `slice base.u8 peek/poke` methods. - Added `std/bmp`. +- Added `std/bzip2`. - Added `std/cbor`. - Added `std/json`. - Added `std/nie`. diff --git a/example/bzcat/bzcat.c b/example/bzcat/bzcat.c new file mode 100644 index 00000000..9e3aef49 --- /dev/null +++ b/example/bzcat/bzcat.c @@ -0,0 +1,265 @@ +// Copyright 2022 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- + +/* +bzcat decodes bzip2'ed data to stdout. It is similar to the standard /bin/bzcat +program, except that this example program only reads from stdin. On Linux, it +also self-imposes a SECCOMP_MODE_STRICT sandbox. To run: + +$CC bzcat.c && ./a.out < ../../test/data/romeo.txt.bz2; rm -f a.out + +for a C compiler $CC, such as clang or gcc. +*/ + +#include +#include + +// Wuffs ships as a "single file C library" or "header file library" as per +// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt +// +// To use that single file as a "foo.c"-like implementation, instead of a +// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or +// compiling it. +#define WUFFS_IMPLEMENTATION + +// Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when +// combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs' +// functions have static storage. +// +// This can help the compiler ignore or discard unused code, which can produce +// faster compiles and smaller binaries. Other motivations are discussed in the +// "ALLOW STATIC IMPLEMENTATION" section of +// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt +#define WUFFS_CONFIG__STATIC_FUNCTIONS + +// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of +// release/c/etc.c choose which parts of Wuffs to build. That file contains the +// entire Wuffs standard library, implementing a variety of codecs and file +// formats. Without this macro definition, an optimizing compiler or linker may +// very well discard Wuffs code for unused codecs, but listing the Wuffs +// modules we use makes that process explicit. Preprocessing means that such +// code simply isn't compiled. +#define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__BASE +#define WUFFS_CONFIG__MODULE__BZIP2 + +// If building this program in an environment that doesn't easily accommodate +// relative includes, you can use the script/inline-c-relative-includes.go +// program to generate a stand-alone C file. +#include "../../release/c/wuffs-unsupported-snapshot.c" + +#if defined(__linux__) +#include +#include +#include +#include +#define WUFFS_EXAMPLE_USE_SECCOMP +#endif + +#ifndef DST_BUFFER_ARRAY_SIZE +#define DST_BUFFER_ARRAY_SIZE (128 * 1024) +#endif + +#ifndef SRC_BUFFER_ARRAY_SIZE +#define SRC_BUFFER_ARRAY_SIZE (128 * 1024) +#endif + +#define WORK_BUFFER_ARRAY_SIZE \ + WUFFS_GZIP__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE + +uint8_t g_dst_buffer_array[DST_BUFFER_ARRAY_SIZE]; +uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE]; +#if WORK_BUFFER_ARRAY_SIZE > 0 +uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; +#else +// Not all C/C++ compilers support 0-length arrays. +uint8_t g_work_buffer_array[1]; +#endif + +// ---- + +static bool g_sandboxed = false; + +struct { + int remaining_argc; + char** remaining_argv; + + bool fail_if_unsandboxed; +} g_flags = {0}; + +const char* // +parse_flags(int argc, char** argv) { + int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. + for (; c < argc; c++) { + char* arg = argv[c]; + if (*arg++ != '-') { + break; + } + + // A double-dash "--foo" is equivalent to a single-dash "-foo". As special + // cases, a bare "-" is not a flag (some programs may interpret it as + // stdin) and a bare "--" means to stop parsing flags. + if (*arg == '\x00') { + break; + } else if (*arg == '-') { + arg++; + if (*arg == '\x00') { + c++; + break; + } + } + + if (!strcmp(arg, "fail-if-unsandboxed")) { + g_flags.fail_if_unsandboxed = true; + continue; + } + + return "main: unrecognized flag argument"; + } + + g_flags.remaining_argc = argc - c; + g_flags.remaining_argv = argv + c; + return NULL; +} + +// ---- + +// ignore_return_value suppresses errors from -Wall -Werror. +static void // +ignore_return_value(int ignored) {} + +const char* // +main1(int argc, char** argv) { + const char* z = parse_flags(argc, argv); + if (z) { + return z; + } + if (g_flags.fail_if_unsandboxed && !g_sandboxed) { + return "main: unsandboxed"; + } + + wuffs_bzip2__decoder dec; + wuffs_base__status status = + wuffs_bzip2__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0); + if (!wuffs_base__status__is_ok(&status)) { + return wuffs_base__status__message(&status); + } + + wuffs_base__io_buffer dst; + dst.data.ptr = g_dst_buffer_array; + dst.data.len = DST_BUFFER_ARRAY_SIZE; + dst.meta.wi = 0; + dst.meta.ri = 0; + dst.meta.pos = 0; + dst.meta.closed = false; + + wuffs_base__io_buffer src; + src.data.ptr = g_src_buffer_array; + src.data.len = SRC_BUFFER_ARRAY_SIZE; + src.meta.wi = 0; + src.meta.ri = 0; + src.meta.pos = 0; + src.meta.closed = false; + + while (true) { + const int stdin_fd = 0; + ssize_t n = + read(stdin_fd, src.data.ptr + src.meta.wi, src.data.len - src.meta.wi); + if (n < 0) { + if (errno != EINTR) { + return strerror(errno); + } + continue; + } + src.meta.wi += n; + if (n == 0) { + src.meta.closed = true; + } + + while (true) { + status = wuffs_bzip2__decoder__transform_io( + &dec, &dst, &src, + wuffs_base__make_slice_u8(g_work_buffer_array, + WORK_BUFFER_ARRAY_SIZE)); + + if (dst.meta.wi) { + // TODO: handle EINTR and other write errors; see "man 2 write". + const int stdout_fd = 1; + ignore_return_value(write(stdout_fd, g_dst_buffer_array, dst.meta.wi)); + dst.meta.ri = dst.meta.wi; + wuffs_base__io_buffer__compact(&dst); + } + + if (status.repr == wuffs_base__suspension__short_read) { + break; + } + if (status.repr == wuffs_base__suspension__short_write) { + continue; + } + return wuffs_base__status__message(&status); + } + + wuffs_base__io_buffer__compact(&src); + if (src.meta.wi == src.data.len) { + return "main: internal error: no I/O progress possible"; + } + } +} + +int // +compute_exit_code(const char* status_msg) { + if (!status_msg) { + return 0; + } + size_t n = strnlen(status_msg, 2047); + if (n >= 2047) { + status_msg = "main: internal error: error message is too long"; + n = strnlen(status_msg, 2047); + } + const int stderr_fd = 2; + ignore_return_value(write(stderr_fd, status_msg, n)); + ignore_return_value(write(stderr_fd, "\n", 1)); + // Return an exit code of 1 for regular (foreseen) errors, e.g. badly + // formatted or unsupported input. + // + // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive + // run-time checks found that an internal invariant did not hold. + // + // Automated testing, including badly formatted inputs, can therefore + // discriminate between expected failure (exit code 1) and unexpected failure + // (other non-zero exit codes). Specifically, exit code 2 for internal + // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 + // linux) for a segmentation fault (e.g. null pointer dereference). + return strstr(status_msg, "internal error:") ? 2 : 1; +} + +int // +main(int argc, char** argv) { +#if defined(WUFFS_EXAMPLE_USE_SECCOMP) + prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); + g_sandboxed = true; +#endif + + int exit_code = compute_exit_code(main1(argc, argv)); + +#if defined(WUFFS_EXAMPLE_USE_SECCOMP) + // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by + // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows + // only SYS_exit. + syscall(SYS_exit, exit_code); +#endif + return exit_code; +} diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 89de1fcd..d2166a02 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -6789,6 +6789,205 @@ struct wuffs_bmp__decoder__struct { // ---------------- Status Codes +// ---------------- Public Consts + +#define WUFFS_BZIP2__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 0 + +// ---------------- Struct Declarations + +typedef struct wuffs_bzip2__decoder__struct wuffs_bzip2__decoder; + +#ifdef __cplusplus +extern "C" { +#endif + +// ---------------- Public Initializer Prototypes + +// For any given "wuffs_foo__bar* self", "wuffs_foo__bar__initialize(self, +// etc)" should be called before any other "wuffs_foo__bar__xxx(self, etc)". +// +// Pass sizeof(*self) and WUFFS_VERSION for sizeof_star_self and wuffs_version. +// Pass 0 (or some combination of WUFFS_INITIALIZE__XXX) for options. + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_bzip2__decoder__initialize( + wuffs_bzip2__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options); + +size_t +sizeof__wuffs_bzip2__decoder(); + +// ---------------- Allocs + +// These functions allocate and initialize Wuffs structs. They return NULL if +// memory allocation fails. If they return non-NULL, there is no need to call +// wuffs_foo__bar__initialize, but the caller is responsible for eventually +// calling free on the returned pointer. That pointer is effectively a C++ +// std::unique_ptr. + +wuffs_bzip2__decoder* +wuffs_bzip2__decoder__alloc(); + +static inline wuffs_base__io_transformer* +wuffs_bzip2__decoder__alloc_as__wuffs_base__io_transformer() { + return (wuffs_base__io_transformer*)(wuffs_bzip2__decoder__alloc()); +} + +// ---------------- Upcasts + +static inline wuffs_base__io_transformer* +wuffs_bzip2__decoder__upcast_as__wuffs_base__io_transformer( + wuffs_bzip2__decoder* p) { + return (wuffs_base__io_transformer*)p; +} + +// ---------------- Public Function Prototypes + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_bzip2__decoder__set_quirk_enabled( + wuffs_bzip2__decoder* self, + uint32_t a_quirk, + bool a_enabled); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_bzip2__decoder__workbuf_len( + const wuffs_bzip2__decoder* self); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_bzip2__decoder__transform_io( + wuffs_bzip2__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf); + +#ifdef __cplusplus +} // extern "C" +#endif + +// ---------------- Struct Definitions + +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#if defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +struct wuffs_bzip2__decoder__struct { + // Do not access the private_impl's or private_data's fields directly. There + // is no API/ABI compatibility or safety guarantee if you do so. Instead, use + // the wuffs_foo__bar__baz functions. + // + // It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct + // can be stack allocated when WUFFS_IMPLEMENTATION is defined. + + struct { + uint32_t magic; + uint32_t active_coroutine; + wuffs_base__vtable vtable_for__wuffs_base__io_transformer; + wuffs_base__vtable null_vtable; + + + uint32_t p_transform_io[1]; + } private_impl; + + struct { + struct { + uint64_t scratch; + } s_transform_io[1]; + } private_data; + +#ifdef __cplusplus +#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR) + using unique_ptr = std::unique_ptr; + + // On failure, the alloc_etc functions return nullptr. They don't throw. + + static inline unique_ptr + alloc() { + return unique_ptr(wuffs_bzip2__decoder__alloc(), &free); + } + + static inline wuffs_base__io_transformer::unique_ptr + alloc_as__wuffs_base__io_transformer() { + return wuffs_base__io_transformer::unique_ptr( + wuffs_bzip2__decoder__alloc_as__wuffs_base__io_transformer(), &free); + } +#endif // defined(WUFFS_BASE__HAVE_UNIQUE_PTR) + +#if defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION) + // Disallow constructing or copying an object via standard C++ mechanisms, + // e.g. the "new" operator, as this struct is intentionally opaque. Its total + // size and field layout is not part of the public, stable, memory-safe API. + // Use malloc or memcpy and the sizeof__wuffs_foo__bar function instead, and + // call wuffs_foo__bar__baz methods (which all take a "this"-like pointer as + // their first argument) rather than tweaking bar.private_impl.qux fields. + // + // In C, we can just leave wuffs_foo__bar as an incomplete type (unless + // WUFFS_IMPLEMENTATION is #define'd). In C++, we define a complete type in + // order to provide convenience methods. These forward on "this", so that you + // can write "bar->baz(etc)" instead of "wuffs_foo__bar__baz(bar, etc)". + wuffs_bzip2__decoder__struct() = delete; + wuffs_bzip2__decoder__struct(const wuffs_bzip2__decoder__struct&) = delete; + wuffs_bzip2__decoder__struct& operator=( + const wuffs_bzip2__decoder__struct&) = delete; +#endif // defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION) + +#if !defined(WUFFS_IMPLEMENTATION) + // As above, the size of the struct is not part of the public API, and unless + // WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap + // allocated, not stack allocated. Its size is not intended to be known at + // compile time, but it is unfortunately divulged as a side effect of + // defining C++ convenience methods. Use "sizeof__T()", calling the function, + // instead of "sizeof T", invoking the operator. To make the two values + // different, so that passing the latter will be rejected by the initialize + // function, we add an arbitrary amount of dead weight. + uint8_t dead_weight[123000000]; // 123 MB. +#endif // !defined(WUFFS_IMPLEMENTATION) + + inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT + initialize( + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options) { + return wuffs_bzip2__decoder__initialize( + this, sizeof_star_self, wuffs_version, options); + } + + inline wuffs_base__io_transformer* + upcast_as__wuffs_base__io_transformer() { + return (wuffs_base__io_transformer*)this; + } + + inline wuffs_base__empty_struct + set_quirk_enabled( + uint32_t a_quirk, + bool a_enabled) { + return wuffs_bzip2__decoder__set_quirk_enabled(this, a_quirk, a_enabled); + } + + inline wuffs_base__range_ii_u64 + workbuf_len() const { + return wuffs_bzip2__decoder__workbuf_len(this); + } + + inline wuffs_base__status + transform_io( + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf) { + return wuffs_bzip2__decoder__transform_io(this, a_dst, a_src, a_workbuf); + } + +#endif // __cplusplus +}; // struct wuffs_bzip2__decoder__struct + +#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +// ---------------- Status Codes + extern const char wuffs_cbor__error__bad_input[]; extern const char wuffs_cbor__error__unsupported_recursion_depth[]; @@ -24553,6 +24752,238 @@ wuffs_bmp__decoder__process_masks( #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BMP) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BZIP2) + +// ---------------- Status Codes Implementations + +// ---------------- Private Consts + +// ---------------- Private Initializer Prototypes + +// ---------------- Private Function Prototypes + +// ---------------- VTables + +const wuffs_base__io_transformer__func_ptrs +wuffs_bzip2__decoder__func_ptrs_for__wuffs_base__io_transformer = { + (wuffs_base__empty_struct(*)(void*, + uint32_t, + bool))(&wuffs_bzip2__decoder__set_quirk_enabled), + (wuffs_base__status(*)(void*, + wuffs_base__io_buffer*, + wuffs_base__io_buffer*, + wuffs_base__slice_u8))(&wuffs_bzip2__decoder__transform_io), + (wuffs_base__range_ii_u64(*)(const void*))(&wuffs_bzip2__decoder__workbuf_len), +}; + +// ---------------- Initializer Implementations + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_bzip2__decoder__initialize( + wuffs_bzip2__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options){ + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (sizeof(*self) != sizeof_star_self) { + return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver); + } + if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) || + (((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) { + return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version); + } + + if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) { + // The whole point of this if-check is to detect an uninitialized *self. + // We disable the warning on GCC. Clang-5.0 does not have this warning. +#if !defined(__clang__) && defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + if (self->private_impl.magic != 0) { + return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed); + } +#if !defined(__clang__) && defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + } else { + if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) { + memset(self, 0, sizeof(*self)); + options |= WUFFS_INITIALIZE__ALREADY_ZEROED; + } else { + memset(&(self->private_impl), 0, sizeof(self->private_impl)); + } + } + + self->private_impl.magic = WUFFS_BASE__MAGIC; + self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name = + wuffs_base__io_transformer__vtable_name; + self->private_impl.vtable_for__wuffs_base__io_transformer.function_pointers = + (const void*)(&wuffs_bzip2__decoder__func_ptrs_for__wuffs_base__io_transformer); + return wuffs_base__make_status(NULL); +} + +wuffs_bzip2__decoder* +wuffs_bzip2__decoder__alloc() { + wuffs_bzip2__decoder* x = + (wuffs_bzip2__decoder*)(calloc(sizeof(wuffs_bzip2__decoder), 1)); + if (!x) { + return NULL; + } + if (wuffs_bzip2__decoder__initialize( + x, sizeof(wuffs_bzip2__decoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) { + free(x); + return NULL; + } + return x; +} + +size_t +sizeof__wuffs_bzip2__decoder() { + return sizeof(wuffs_bzip2__decoder); +} + +// ---------------- Function Implementations + +// -------- func bzip2.decoder.set_quirk_enabled + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_bzip2__decoder__set_quirk_enabled( + wuffs_bzip2__decoder* self, + uint32_t a_quirk, + bool a_enabled) { + return wuffs_base__make_empty_struct(); +} + +// -------- func bzip2.decoder.workbuf_len + +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_bzip2__decoder__workbuf_len( + const wuffs_bzip2__decoder* self) { + if (!self) { + return wuffs_base__utility__empty_range_ii_u64(); + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return wuffs_base__utility__empty_range_ii_u64(); + } + + return wuffs_base__utility__make_range_ii_u64(0, 0); +} + +// -------- func bzip2.decoder.transform_io + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_bzip2__decoder__transform_io( + wuffs_bzip2__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + if (!a_dst || !a_src) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } + if ((self->private_impl.active_coroutine != 0) && + (self->private_impl.active_coroutine != 1)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls); + } + self->private_impl.active_coroutine = 0; + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint8_t* iop_a_dst = NULL; + uint8_t* io0_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + uint8_t* io1_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + uint8_t* io2_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_dst) { + io0_a_dst = a_dst->data.ptr; + io1_a_dst = io0_a_dst + a_dst->meta.wi; + iop_a_dst = io1_a_dst; + io2_a_dst = io0_a_dst + a_dst->data.len; + if (a_dst->meta.closed) { + io2_a_dst = iop_a_dst; + } + } + const uint8_t* iop_a_src = NULL; + const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_src) { + io0_a_src = a_src->data.ptr; + io1_a_src = io0_a_src + a_src->meta.ri; + iop_a_src = io1_a_src; + io2_a_src = io0_a_src + a_src->meta.wi; + } + + uint32_t coro_susp_point = self->private_impl.p_transform_io[0]; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + while (true) { + if (((uint64_t)(io2_a_src - iop_a_src)) > 0) { + self->private_data.s_transform_io[0].scratch = ((uint64_t)(io2_a_src - iop_a_src)); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (self->private_data.s_transform_io[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_transform_io[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_transform_io[0].scratch; + } + if (a_src && a_src->meta.closed) { + goto label__0__break; + } + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2); + } + label__0__break:; + self->private_data.s_transform_io[0].scratch = 66; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + if (iop_a_dst == io2_a_dst) { + status = wuffs_base__make_status(wuffs_base__suspension__short_write); + goto suspend; + } + *iop_a_dst++ = ((uint8_t)(self->private_data.s_transform_io[0].scratch)); + + ok: + self->private_impl.p_transform_io[0] = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_transform_io[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0; + + goto exit; + exit: + if (a_dst) { + a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr)); + } + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + if (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BZIP2) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__CBOR) // ---------------- Status Codes Implementations diff --git a/std/bzip2/decode_bzip2.wuffs b/std/bzip2/decode_bzip2.wuffs new file mode 100644 index 00000000..550673d5 --- /dev/null +++ b/std/bzip2/decode_bzip2.wuffs @@ -0,0 +1,45 @@ +// Copyright 2022 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 + +pub struct decoder? implements base.io_transformer( + util : base.utility, +) + +pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) { + // TODO: support base.QUIRK_IGNORE_CHECKSUM. +} + +pub func decoder.workbuf_len() base.range_ii_u64 { + return this.util.make_range_ii_u64( + min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE, + max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE) +} + +pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) { + // TODO: implement. + + while true { + if args.src.length() > 0 { + args.src.skip?(n: args.src.length()) + } + if args.src.is_closed() { + break + } + yield? base."$short read" + } endwhile + + args.dst.write_u8?(a: 0x42) +} diff --git a/test/c/std/bzip2.c b/test/c/std/bzip2.c new file mode 100644 index 00000000..5c622315 --- /dev/null +++ b/test/c/std/bzip2.c @@ -0,0 +1,197 @@ +// Copyright 2022 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- + +/* +This test program is typically run indirectly, by the "wuffs test" or "wuffs +bench" commands. These commands take an optional "-mimic" flag to check that +Wuffs' output mimics (i.e. exactly matches) other libraries' output, such as +giflib for GIF, libpng for PNG, etc. + +To manually run this test: + +for CC in clang gcc; do + $CC -std=c99 -Wall -Werror bzip2.c && ./a.out + rm -f a.out +done + +Each edition should print "PASS", amongst other information, and exit(0). + +Add the "wuffs mimic cflags" (everything after the colon below) to the C +compiler flags (after the .c file) to run the mimic tests. + +To manually run the benchmarks, replace "-Wall -Werror" with "-O3" and replace +the first "./a.out" with "./a.out -bench". Combine these changes with the +"wuffs mimic cflags" to run the mimic benchmarks. +*/ + +// ¿ wuffs mimic cflags: -DWUFFS_MIMIC -ldeflate -lz + +// Wuffs ships as a "single file C library" or "header file library" as per +// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt +// +// To use that single file as a "foo.c"-like implementation, instead of a +// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or +// compiling it. +#define WUFFS_IMPLEMENTATION + +// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of +// release/c/etc.c choose which parts of Wuffs to build. That file contains the +// entire Wuffs standard library, implementing a variety of codecs and file +// formats. Without this macro definition, an optimizing compiler or linker may +// very well discard Wuffs code for unused codecs, but listing the Wuffs +// modules we use makes that process explicit. Preprocessing means that such +// code simply isn't compiled. +#define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__BASE +#define WUFFS_CONFIG__MODULE__BZIP2 + +// If building this program in an environment that doesn't easily accommodate +// relative includes, you can use the script/inline-c-relative-includes.go +// program to generate a stand-alone C file. +#include "../../../release/c/wuffs-unsupported-snapshot.c" +#include "../testlib/testlib.c" +#ifdef WUFFS_MIMIC +// No mimic library. +#endif + +// ---------------- Golden Tests + +golden_test g_bzip2_midsummer_gt = { + .want_filename = "test/data/midsummer.txt", + .src_filename = "test/data/midsummer.txt.bz2", +}; + +golden_test g_bzip2_pi_gt = { + .want_filename = "test/data/pi.txt", + .src_filename = "test/data/pi.txt.bz2", +}; + +// ---------------- Bzip2 Tests + +const char* // +test_wuffs_bzip2_decode_interface() { + CHECK_FOCUS(__func__); + wuffs_bzip2__decoder dec; + CHECK_STATUS("initialize", + wuffs_bzip2__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + return do_test__wuffs_base__io_transformer( + wuffs_bzip2__decoder__upcast_as__wuffs_base__io_transformer(&dec), + "test/data/romeo.txt.bz2", 0, SIZE_MAX, 1, 0x42); +} + +const char* // +wuffs_bzip2_decode(wuffs_base__io_buffer* dst, + wuffs_base__io_buffer* src, + uint32_t wuffs_initialize_flags, + uint64_t wlimit, + uint64_t rlimit) { + wuffs_bzip2__decoder dec; + CHECK_STATUS("initialize", + wuffs_bzip2__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, + wuffs_initialize_flags)); + + while (true) { + wuffs_base__io_buffer limited_dst = make_limited_writer(*dst, wlimit); + wuffs_base__io_buffer limited_src = make_limited_reader(*src, rlimit); + + wuffs_base__status status = wuffs_bzip2__decoder__transform_io( + &dec, &limited_dst, &limited_src, g_work_slice_u8); + + dst->meta.wi += limited_dst.meta.wi; + src->meta.ri += limited_src.meta.ri; + + if (((wlimit < UINT64_MAX) && + (status.repr == wuffs_base__suspension__short_write)) || + ((rlimit < UINT64_MAX) && + (status.repr == wuffs_base__suspension__short_read))) { + continue; + } + return status.repr; + } +} + +// ---------------- Mimic Tests + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + +// ---------------- Bzip2 Benches + +const char* // +bench_wuffs_bzip2_decode_10k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + wuffs_bzip2_decode, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, tcounter_dst, + &g_bzip2_midsummer_gt, UINT64_MAX, UINT64_MAX, 300); +} + +const char* // +bench_wuffs_bzip2_decode_100k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + wuffs_bzip2_decode, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, tcounter_dst, + &g_bzip2_pi_gt, UINT64_MAX, UINT64_MAX, 30); +} + +// ---------------- Mimic Benches + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + +// ---------------- Manifest + +proc g_tests[] = { + + test_wuffs_bzip2_decode_interface, + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + + NULL, +}; + +proc g_benches[] = { + + bench_wuffs_bzip2_decode_10k, + bench_wuffs_bzip2_decode_100k, + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + + NULL, +}; + +int // +main(int argc, char** argv) { + g_proc_package_name = "std/bzip2"; + return test_main(argc, argv, g_tests, g_benches); +}