From 793ab36569198d7d67f85c4e669953fc497a5835 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 1 Mar 2024 09:19:38 +1100 Subject: [PATCH] std/lzip: add new package --- doc/changelog.md | 1 + doc/std/README.md | 1 + doc/std/compression-decoders.md | 6 +- example/mzcat/mzcat.c | 12 + internal/cgen/base/magic-submodule.c | 1 + lang/builtin/builtin.go | 1 + release/c/wuffs-unsupported-snapshot.c | 551 +++++++++++++++++++++ std/lzip/decode_lzip.wuffs | 65 +++ test/3pdata/mzcat-checksums-of-xzsuite.txt | 17 + test/c/std/lzip.c | 135 +++++ test/data/romeo.txt.lz | Bin 0 -> 609 bytes test/mzcat-checksums-of-data.txt | 1 + 12 files changed, 790 insertions(+), 1 deletion(-) create mode 100644 std/lzip/decode_lzip.wuffs create mode 100644 test/c/std/lzip.c create mode 100644 test/data/romeo.txt.lz diff --git a/doc/changelog.md b/doc/changelog.md index 529122ea..3425e48e 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -17,6 +17,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license - Added `get_quirk(key: u32) u64`. - Added `std/crc64`. - Added `std/jpeg`. +- Added `std/lzip`. - Added `std/lzma`. - Added `std/netpbm`. - Added `std/sha256`. diff --git a/doc/std/README.md b/doc/std/README.md index e670b486..d9cca114 100644 --- a/doc/std/README.md +++ b/doc/std/README.md @@ -56,6 +56,7 @@ to enable. - `GZIP: BASE, CRC32, DEFLATE` - `JPEG: BASE` - `JSON: BASE` +- `LZIP: BASE, CRC32, LZMA` - `LZMA: BASE` - `LZW: BASE` - `NETPBM: BASE` diff --git a/doc/std/compression-decoders.md b/doc/std/compression-decoders.md index 84b0d676..609926b7 100644 --- a/doc/std/compression-decoders.md +++ b/doc/std/compression-decoders.md @@ -37,6 +37,7 @@ In Wuffs syntax, the `base.io_transformer` methods are: - [std/bzip2](/std/bzip2) - [std/deflate](/std/deflate) - [std/gzip](/std/gzip) +- [std/lzip](/std/lzip) - [std/lzma](/std/lzma) - [std/lzw](/std/lzw) - [std/xz](/std/xz) @@ -45,13 +46,16 @@ In Wuffs syntax, the `base.io_transformer` methods are: ## Examples -- [example/bzcat](/example/bzcat) +- [example/mzcat](/example/mzcat) - [example/toy-genlib](/example/toy-genlib) - [example/zcat](/example/zcat) ## [Quirks](/doc/note/quirks.md) +- [LZMA decoder quirks](/std/lzma/decode_quirks.wuffs) +- [LZW decoder quirks](/std/lzw/decode_quirks.wuffs) +- [XZ decoder quirks](/std/xz/decode_quirks.wuffs) - [Zlib decoder quirks](/std/zlib/decode_quirks.wuffs) diff --git a/example/mzcat/mzcat.c b/example/mzcat/mzcat.c index 3fce5043..ab5afc83 100644 --- a/example/mzcat/mzcat.c +++ b/example/mzcat/mzcat.c @@ -23,6 +23,7 @@ for a C compiler $CC, such as clang or gcc. Supported compression formats: - bzip2 - gzip +- lzip - lzma - xz - zlib @@ -64,6 +65,7 @@ Supported compression formats: #define WUFFS_CONFIG__MODULE__CRC64 #define WUFFS_CONFIG__MODULE__DEFLATE #define WUFFS_CONFIG__MODULE__GZIP +#define WUFFS_CONFIG__MODULE__LZIP #define WUFFS_CONFIG__MODULE__LZMA #define WUFFS_CONFIG__MODULE__SHA256 #define WUFFS_CONFIG__MODULE__XZ @@ -107,6 +109,7 @@ wuffs_base__io_transformer* g_io_transformer = NULL; union { wuffs_bzip2__decoder bzip2; wuffs_gzip__decoder gzip; + wuffs_lzip__decoder lzip; wuffs_lzma__decoder lzma; wuffs_xz__decoder xz; wuffs_zlib__decoder zlib; @@ -197,6 +200,15 @@ initialize_io_transformer(uint8_t input_first_byte) { &g_potential_decoders.bzip2); break; + case 0x4C: + status = wuffs_lzip__decoder__initialize( + &g_potential_decoders.lzip, sizeof g_potential_decoders.lzip, + WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS); + io_transformer = + wuffs_lzip__decoder__upcast_as__wuffs_base__io_transformer( + &g_potential_decoders.lzip); + break; + case 0x5D: status = wuffs_lzma__decoder__initialize( &g_potential_decoders.lzma, sizeof g_potential_decoders.lzma, diff --git a/internal/cgen/base/magic-submodule.c b/internal/cgen/base/magic-submodule.c index 76184604..1aa1aa31 100644 --- a/internal/cgen/base/magic-submodule.c +++ b/internal/cgen/base/magic-submodule.c @@ -152,6 +152,7 @@ wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix_data, {+0x424D5020, "\x01\x42\x4D"}, // BMP {+0x47494620, "\x03\x47\x49\x46\x38"}, // GIF {+0x54494646, "\x03\x49\x49\x2A\x00"}, // TIFF (little-endian) + {+0x4C5A4950, "\x04\x4C\x5A\x49\x50\x01"}, // LZIP {+0x54494646, "\x03\x4D\x4D\x00\x2A"}, // TIFF (big-endian) {+0x4E50424D, "\x02\x50\x35\x0A"}, // NPBM (P5; *.pgm) {+0x4E50424D, "\x02\x50\x36\x0A"}, // NPBM (P6; *.ppm) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index e1ba4963..5aee7984 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -51,6 +51,7 @@ var FourCCs = [...][2]string{ {"KVPK", "Key-Value Pair (Key)"}, {"KVPV", "Key-Value Pair (Value)"}, {"LZ4 ", "Lempel–Ziv 4"}, + {"LZIP", "Lzip"}, {"LZMA", "Lempel–Ziv Markov-chain Algorithm"}, {"MD ", "Markdown"}, {"MTIM", "Modification Time"}, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index cd3ca1fc..fbe67e72 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -782,6 +782,9 @@ typedef struct wuffs_base__transform__output__struct { // Lempel–Ziv 4. #define WUFFS_BASE__FOURCC__LZ4 0x4C5A3420 +// Lzip. +#define WUFFS_BASE__FOURCC__LZIP 0x4C5A4950 + // Lempel–Ziv Markov-chain Algorithm. #define WUFFS_BASE__FOURCC__LZMA 0x4C5A4D41 @@ -10651,6 +10654,240 @@ struct wuffs_lzma__decoder__struct { #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) || defined(WUFFS_NONMONOLITHIC) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZIP) || defined(WUFFS_NONMONOLITHIC) + +// ---------------- Status Codes + +extern const char wuffs_lzip__error__truncated_input[]; + +// ---------------- Public Consts + +#define WUFFS_LZIP__DECODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE 0u + +#define WUFFS_LZIP__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 1u + +// ---------------- Struct Declarations + +typedef struct wuffs_lzip__decoder__struct wuffs_lzip__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_lzip__decoder__initialize( + wuffs_lzip__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options); + +size_t +sizeof__wuffs_lzip__decoder(void); + +// ---------------- 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_lzip__decoder* +wuffs_lzip__decoder__alloc(void); + +static inline wuffs_base__io_transformer* +wuffs_lzip__decoder__alloc_as__wuffs_base__io_transformer(void) { + return (wuffs_base__io_transformer*)(wuffs_lzip__decoder__alloc()); +} + +// ---------------- Upcasts + +static inline wuffs_base__io_transformer* +wuffs_lzip__decoder__upcast_as__wuffs_base__io_transformer( + wuffs_lzip__decoder* p) { + return (wuffs_base__io_transformer*)p; +} + +// ---------------- Public Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_lzip__decoder__get_quirk( + const wuffs_lzip__decoder* self, + uint32_t a_key); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzip__decoder__set_quirk( + wuffs_lzip__decoder* self, + uint32_t a_key, + uint64_t a_value); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63 +wuffs_lzip__decoder__dst_history_retain_length( + const wuffs_lzip__decoder* self); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_lzip__decoder__workbuf_len( + const wuffs_lzip__decoder* self); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzip__decoder__transform_io( + wuffs_lzip__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_lzip__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; + + bool f_ignore_checksum; + + uint32_t p_transform_io; + uint32_t p_do_transform_io; + } private_impl; + + struct { + struct { + uint64_t scratch; + } s_do_transform_io; + } 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_lzip__decoder__alloc()); + } + + static inline wuffs_base__io_transformer::unique_ptr + alloc_as__wuffs_base__io_transformer() { + return wuffs_base__io_transformer::unique_ptr( + wuffs_lzip__decoder__alloc_as__wuffs_base__io_transformer()); + } +#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_lzip__decoder__struct() = delete; + wuffs_lzip__decoder__struct(const wuffs_lzip__decoder__struct&) = delete; + wuffs_lzip__decoder__struct& operator=( + const wuffs_lzip__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_lzip__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 uint64_t + get_quirk( + uint32_t a_key) const { + return wuffs_lzip__decoder__get_quirk(this, a_key); + } + + inline wuffs_base__status + set_quirk( + uint32_t a_key, + uint64_t a_value) { + return wuffs_lzip__decoder__set_quirk(this, a_key, a_value); + } + + inline wuffs_base__optional_u63 + dst_history_retain_length() const { + return wuffs_lzip__decoder__dst_history_retain_length(this); + } + + inline wuffs_base__range_ii_u64 + workbuf_len() const { + return wuffs_lzip__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_lzip__decoder__transform_io(this, a_dst, a_src, a_workbuf); + } + +#endif // __cplusplus +}; // struct wuffs_lzip__decoder__struct + +#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZIP) || defined(WUFFS_NONMONOLITHIC) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZW) || defined(WUFFS_NONMONOLITHIC) // ---------------- Status Codes @@ -20588,6 +20825,7 @@ wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix_data, {+0x424D5020, "\x01\x42\x4D"}, // BMP {+0x47494620, "\x03\x47\x49\x46\x38"}, // GIF {+0x54494646, "\x03\x49\x49\x2A\x00"}, // TIFF (little-endian) + {+0x4C5A4950, "\x04\x4C\x5A\x49\x50\x01"}, // LZIP {+0x54494646, "\x03\x4D\x4D\x00\x2A"}, // TIFF (big-endian) {+0x4E50424D, "\x02\x50\x35\x0A"}, // NPBM (P5; *.pgm) {+0x4E50424D, "\x02\x50\x36\x0A"}, // NPBM (P6; *.ppm) @@ -53294,6 +53532,319 @@ wuffs_lzma__decoder__initialize_probs( #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZIP) + +// ---------------- Status Codes Implementations + +const char wuffs_lzip__error__truncated_input[] = "#lzip: truncated input"; + +// ---------------- Private Consts + +// ---------------- Private Initializer Prototypes + +// ---------------- Private Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzip__decoder__do_transform_io( + wuffs_lzip__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf); + +// ---------------- VTables + +const wuffs_base__io_transformer__func_ptrs +wuffs_lzip__decoder__func_ptrs_for__wuffs_base__io_transformer = { + (wuffs_base__optional_u63(*)(const void*))(&wuffs_lzip__decoder__dst_history_retain_length), + (uint64_t(*)(const void*, + uint32_t))(&wuffs_lzip__decoder__get_quirk), + (wuffs_base__status(*)(void*, + uint32_t, + uint64_t))(&wuffs_lzip__decoder__set_quirk), + (wuffs_base__status(*)(void*, + wuffs_base__io_buffer*, + wuffs_base__io_buffer*, + wuffs_base__slice_u8))(&wuffs_lzip__decoder__transform_io), + (wuffs_base__range_ii_u64(*)(const void*))(&wuffs_lzip__decoder__workbuf_len), +}; + +// ---------------- Initializer Implementations + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_lzip__decoder__initialize( + wuffs_lzip__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_lzip__decoder__func_ptrs_for__wuffs_base__io_transformer); + return wuffs_base__make_status(NULL); +} + +wuffs_lzip__decoder* +wuffs_lzip__decoder__alloc(void) { + wuffs_lzip__decoder* x = + (wuffs_lzip__decoder*)(calloc(sizeof(wuffs_lzip__decoder), 1)); + if (!x) { + return NULL; + } + if (wuffs_lzip__decoder__initialize( + x, sizeof(wuffs_lzip__decoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) { + free(x); + return NULL; + } + return x; +} + +size_t +sizeof__wuffs_lzip__decoder(void) { + return sizeof(wuffs_lzip__decoder); +} + +// ---------------- Function Implementations + +// -------- func lzip.decoder.get_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_lzip__decoder__get_quirk( + const wuffs_lzip__decoder* self, + uint32_t a_key) { + if (!self) { + return 0; + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return 0; + } + + if ((a_key == 1u) && self->private_impl.f_ignore_checksum) { + return 1u; + } + return 0u; +} + +// -------- func lzip.decoder.set_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzip__decoder__set_quirk( + wuffs_lzip__decoder* self, + uint32_t a_key, + uint64_t a_value) { + 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_key == 1u) { + self->private_impl.f_ignore_checksum = (a_value > 0u); + return wuffs_base__make_status(NULL); + } + return wuffs_base__make_status(wuffs_base__error__unsupported_option); +} + +// -------- func lzip.decoder.dst_history_retain_length + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63 +wuffs_lzip__decoder__dst_history_retain_length( + const wuffs_lzip__decoder* self) { + if (!self) { + return wuffs_base__utility__make_optional_u63(false, 0u); + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return wuffs_base__utility__make_optional_u63(false, 0u); + } + + return wuffs_base__utility__make_optional_u63(true, 0u); +} + +// -------- func lzip.decoder.workbuf_len + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_lzip__decoder__workbuf_len( + const wuffs_lzip__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(1u, 1u); +} + +// -------- func lzip.decoder.transform_io + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzip__decoder__transform_io( + wuffs_lzip__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); + + wuffs_base__status v_status = wuffs_base__make_status(NULL); + + uint32_t coro_susp_point = self->private_impl.p_transform_io; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + while (true) { + { + wuffs_base__status t_0 = wuffs_lzip__decoder__do_transform_io(self, a_dst, a_src, a_workbuf); + v_status = t_0; + } + if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) { + status = wuffs_base__make_status(wuffs_lzip__error__truncated_input); + goto exit; + } + status = v_status; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1); + } + + ok: + self->private_impl.p_transform_io = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_transform_io = 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 (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +// -------- func lzip.decoder.do_transform_io + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzip__decoder__do_transform_io( + wuffs_lzip__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf) { + 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 && a_dst->data.ptr) { + 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; + } + } + + uint32_t coro_susp_point = self->private_impl.p_do_transform_io; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + self->private_data.s_do_transform_io.scratch = 120u; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + 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_do_transform_io.scratch)); + + goto ok; + ok: + self->private_impl.p_do_transform_io = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_do_transform_io = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + goto exit; + exit: + if (a_dst && a_dst->data.ptr) { + a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr)); + } + + return status; +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZIP) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZW) // ---------------- Status Codes Implementations diff --git a/std/lzip/decode_lzip.wuffs b/std/lzip/decode_lzip.wuffs new file mode 100644 index 00000000..66d5c230 --- /dev/null +++ b/std/lzip/decode_lzip.wuffs @@ -0,0 +1,65 @@ +// Copyright 2024 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use "std/crc32" +use "std/lzma" + +pub status "#truncated input" + +pub const DECODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE : base.u64 = 0 + +pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 1 + +pub struct decoder? implements base.io_transformer( + ignore_checksum : base.bool, + + util : base.utility, +) + +pub func decoder.get_quirk(key: base.u32) base.u64 { + if (args.key == base.QUIRK_IGNORE_CHECKSUM) and this.ignore_checksum { + return 1 + } + return 0 +} + +pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status { + if args.key == base.QUIRK_IGNORE_CHECKSUM { + this.ignore_checksum = args.value > 0 + return ok + } + return base."#unsupported option" +} + +pub func decoder.dst_history_retain_length() base.optional_u63 { + return this.util.make_optional_u63(has_value: true, value: 0) +} + +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) { + var status : base.status + + while true { + status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf) + if (status == base."$short read") and args.src.is_closed() { + return "#truncated input" + } + yield? status + } endwhile +} + +pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) { + args.dst.write_u8?(a: 'x') +} diff --git a/test/3pdata/mzcat-checksums-of-xzsuite.txt b/test/3pdata/mzcat-checksums-of-xzsuite.txt index da51d8be..f06c99fe 100644 --- a/test/3pdata/mzcat-checksums-of-xzsuite.txt +++ b/test/3pdata/mzcat-checksums-of-xzsuite.txt @@ -17,6 +17,14 @@ BAD 7759aecd test/3pdata/xzsuite/bad-1-lzma2-8.xz BAD 3b80e9ac test/3pdata/xzsuite/bad-1-lzma2-9.xz BAD 15a2a343 test/3pdata/xzsuite/bad-1-stream_flags-1.xz BAD 15a2a343 test/3pdata/xzsuite/bad-1-stream_flags-3.xz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v0-uncomp-size.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-crc32.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-dict-1.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-dict-2.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-magic-2.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-member-size.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-trailing-magic.lz +OK. 8cdc1683 test/3pdata/xzsuite/bad-1-v1-uncomp-size.lz BAD 15a2a343 test/3pdata/xzsuite/bad-1-vli-2.xz BAD 31963516 test/3pdata/xzsuite/bad-2-compressed_data_padding.xz BAD 15a2a343 test/3pdata/xzsuite/bad-2-index-1.xz @@ -54,8 +62,17 @@ OK. 00000000 test/3pdata/xzsuite/good-1-lzma2-5.xz OK. 9d269cdc test/3pdata/xzsuite/good-1-riscv-lzma2-1.xz OK. 8d115715 test/3pdata/xzsuite/good-1-riscv-lzma2-2.xz OK. cc270da1 test/3pdata/xzsuite/good-1-sparc-lzma2.xz +OK. 8cdc1683 test/3pdata/xzsuite/good-1-v0-trailing-1.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-1-v0.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-1-v1-trailing-1.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-1-v1-trailing-2.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-1-v1.lz OK. a3679091 test/3pdata/xzsuite/good-1-x86-lzma2.xz OK. 15a2a343 test/3pdata/xzsuite/good-2-lzma2.xz +OK. 8cdc1683 test/3pdata/xzsuite/good-2-v0-v1.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-2-v1-v0.lz +OK. 8cdc1683 test/3pdata/xzsuite/good-2-v1-v1.lz OK. 15a2a343 test/3pdata/xzsuite/good-known_size-with_eopm.lzma OK. 15a2a343 test/3pdata/xzsuite/good-known_size-without_eopm.lzma OK. 15a2a343 test/3pdata/xzsuite/good-unknown_size-with_eopm.lzma +OK. 8cdc1683 test/3pdata/xzsuite/unsupported-1-v234.lz diff --git a/test/c/std/lzip.c b/test/c/std/lzip.c new file mode 100644 index 00000000..81016a67 --- /dev/null +++ b/test/c/std/lzip.c @@ -0,0 +1,135 @@ +// Copyright 2024 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +// ---------------- + +/* +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 lzip.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__CRC32 +#define WUFFS_CONFIG__MODULE__LZIP +#define WUFFS_CONFIG__MODULE__LZMA + +// 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 + +// ---------------- Lzip Tests + +const char* // +test_wuffs_lzip_decode_interface() { + CHECK_FOCUS(__func__); + wuffs_lzip__decoder dec; + CHECK_STATUS("initialize", + wuffs_lzip__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + return do_test__wuffs_base__io_transformer( + wuffs_lzip__decoder__upcast_as__wuffs_base__io_transformer(&dec), + "test/data/romeo.txt.lz", 0, SIZE_MAX, 1, 0x78); +} + +// ---------------- Mimic Tests + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + +// ---------------- Lzip Benches + +// No Lzip benches. + +// ---------------- Mimic Benches + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + +// ---------------- Manifest + +proc g_tests[] = { + + test_wuffs_lzip_decode_interface, + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + + NULL, +}; + +proc g_benches[] = { + +// No Lzip benches. + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + + NULL, +}; + +int // +main(int argc, char** argv) { + g_proc_package_name = "std/lzip"; + return test_main(argc, argv, g_tests, g_benches); +} diff --git a/test/data/romeo.txt.lz b/test/data/romeo.txt.lz new file mode 100644 index 0000000000000000000000000000000000000000..ac42c117d7092b9cba46d49d505ba3880dcf93b0 GIT binary patch literal 609 zcmeZ?@(f_)VbGL5xh%`x(k5U^AivUDm&g|)|4#gzZF*S1>#p@Ws|~TsCkaaUC|;X2 z{n~MUMx7`JMrDHye`22=JCw8Q=&XYH2A$nXt=3a_-JINd=~4*e9cIm`9Iq8UHcX$_ z@~p`~q(VmYmKYWTy8>lQ3cHsFo;VZa(AdQ~jYAWg*EE^+-g(@XAWr(ZbQ ze%p=Z(yu9MuA9>({_HD|tnF%yI98!z%lGHmCj-7!Q6}$0);P^f3*TQAkZUtrLRLa! z<UcGQ$fB&XOtc}~XrC0ry zZ+*#Bre&f&$I&@ipzC>B+r_Z1lFYn1d z3*R|EHb@J+=fC4+nxCTi$Gak=`i=FhmLs}-bN6nV!fjM-EN>`Skixuu>hIRjepMNF literal 0 HcmV?d00001 diff --git a/test/mzcat-checksums-of-data.txt b/test/mzcat-checksums-of-data.txt index fc5dc86b..92e8efc4 100644 --- a/test/mzcat-checksums-of-data.txt +++ b/test/mzcat-checksums-of-data.txt @@ -29,6 +29,7 @@ OK. abe507ef test/data/romeo.txt.bz2 OK. abe507ef test/data/romeo.txt.delta1.xz OK. abe507ef test/data/romeo.txt.gz OK. abe507ef test/data/romeo.txt.litonlylzma +OK. 8cdc1683 test/data/romeo.txt.lz OK. abe507ef test/data/romeo.txt.lzma OK. abe507ef test/data/romeo.txt.xz OK. abe507ef test/data/romeo.txt.zlib