From 57f01ec3f68db807888ec4a72c805e0f9b2d37c3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 10 Dec 2023 17:09:46 +1100 Subject: [PATCH] std/lzma: add new package --- doc/changelog.md | 1 + doc/std/compression-decoders.md | 1 + lang/builtin/builtin.go | 1 + release/c/wuffs-unsupported-snapshot.c | 1964 ++++++++++++++++++++++++ std/lzma/README.md | 331 ++++ std/lzma/decode_bitstream_slow.wuffs | 835 ++++++++++ std/lzma/decode_lzma.wuffs | 324 ++++ test/c/mimiclib/lzma.c | 46 + test/c/std/lzma.c | 217 +++ 9 files changed, 3720 insertions(+) create mode 100644 std/lzma/README.md create mode 100644 std/lzma/decode_bitstream_slow.wuffs create mode 100644 std/lzma/decode_lzma.wuffs create mode 100644 test/c/mimiclib/lzma.c create mode 100644 test/c/std/lzma.c diff --git a/doc/changelog.md b/doc/changelog.md index 185497ae..dc3be484 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -19,6 +19,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license - Added `slice_var as nptr array[etc] etc` conversion. - Added `std/crc64`. - Added `std/jpeg`. +- Added `std/lzma`. - Added `std/netpbm`. - Added `std/sha256`. - Added `std/xxhash32`. diff --git a/doc/std/compression-decoders.md b/doc/std/compression-decoders.md index 7738856f..6d7dba66 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/lzma](/std/lzma) - [std/lzw](/std/lzw) - [std/zlib](/std/zlib) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 06092ba7..652ad3a5 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -263,6 +263,7 @@ var Statuses = [...]string{ `"#disabled by previous error"`, `"#initialize falsely claimed already zeroed"`, `"#initialize not called"`, + `"#insufficient history"`, `"#interleaved coroutine calls"`, `"#no more information"`, `"#not enough data"`, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ce73612d..fefa4927 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -551,6 +551,7 @@ extern const char wuffs_base__error__cannot_return_a_suspension[]; extern const char wuffs_base__error__disabled_by_previous_error[]; extern const char wuffs_base__error__initialize_falsely_claimed_already_zeroed[]; extern const char wuffs_base__error__initialize_not_called[]; +extern const char wuffs_base__error__insufficient_history[]; extern const char wuffs_base__error__interleaved_coroutine_calls[]; extern const char wuffs_base__error__no_more_information[]; extern const char wuffs_base__error__not_enough_data[]; @@ -10408,6 +10409,299 @@ struct wuffs_json__decoder__struct { #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__JSON) || defined(WUFFS_NONMONOLITHIC) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) || defined(WUFFS_NONMONOLITHIC) + +// ---------------- Status Codes + +extern const char wuffs_lzma__error__bad_bitstream_trailer[]; +extern const char wuffs_lzma__error__bad_code[]; +extern const char wuffs_lzma__error__bad_decoded_length[]; +extern const char wuffs_lzma__error__bad_distance[]; +extern const char wuffs_lzma__error__bad_header[]; +extern const char wuffs_lzma__error__truncated_input[]; +extern const char wuffs_lzma__error__unsupported_decoded_length[]; +extern const char wuffs_lzma__error__unsupported_properties[]; + +// ---------------- Public Consts + +#define WUFFS_LZMA__DECODER_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE 4294967295u + +#define WUFFS_LZMA__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 0u + +// ---------------- Struct Declarations + +typedef struct wuffs_lzma__decoder__struct wuffs_lzma__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_lzma__decoder__initialize( + wuffs_lzma__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options); + +size_t +sizeof__wuffs_lzma__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_lzma__decoder* +wuffs_lzma__decoder__alloc(void); + +static inline wuffs_base__io_transformer* +wuffs_lzma__decoder__alloc_as__wuffs_base__io_transformer(void) { + return (wuffs_base__io_transformer*)(wuffs_lzma__decoder__alloc()); +} + +// ---------------- Upcasts + +static inline wuffs_base__io_transformer* +wuffs_lzma__decoder__upcast_as__wuffs_base__io_transformer( + wuffs_lzma__decoder* p) { + return (wuffs_base__io_transformer*)p; +} + +// ---------------- Public Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_lzma__decoder__get_quirk( + const wuffs_lzma__decoder* self, + uint32_t a_key); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzma__decoder__set_quirk( + wuffs_lzma__decoder* self, + uint32_t a_key, + uint64_t a_value); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63 +wuffs_lzma__decoder__history_retain_length( + const wuffs_lzma__decoder* self); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_lzma__decoder__workbuf_len( + const wuffs_lzma__decoder* self); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzma__decoder__transform_io( + wuffs_lzma__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_lzma__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 f_lc; + uint32_t f_lp; + uint32_t f_pb; + uint32_t f_dict_size; + uint64_t f_decoded_length; + + uint32_t p_decode_bitstream_slow; + uint32_t p_transform_io; + uint32_t p_do_transform_io; + uint32_t p_decode_properties; + } private_impl; + + struct { + uint16_t f_probs_ao00[192]; + uint16_t f_probs_ao20[12]; + uint16_t f_probs_ao40[12]; + uint16_t f_probs_ao41[192]; + uint16_t f_probs_ao60[12]; + uint16_t f_probs_ao63[12]; + uint16_t f_probs_match_len_low[16][8]; + uint16_t f_probs_match_len_mid[16][8]; + uint16_t f_probs_match_len_high[1][256]; + uint16_t f_probs_longrep_len_low[16][8]; + uint16_t f_probs_longrep_len_mid[16][8]; + uint16_t f_probs_longrep_len_high[1][256]; + uint16_t f_probs_slot[4][64]; + uint16_t f_probs_small_dist[128]; + uint16_t f_probs_large_dist[16]; + uint16_t f_probs_lit[16][768]; + + struct { + uint32_t v_bits; + uint32_t v_range; + uint32_t v_state; + uint32_t v_rep0; + uint32_t v_rep1; + uint32_t v_rep2; + uint32_t v_rep3; + uint32_t v_rep; + uint64_t v_pos; + uint32_t v_lc; + uint64_t v_lp_mask; + uint64_t v_pb_mask; + uint32_t v_tree_node; + uint8_t v_prev_byte; + uint8_t v_match_byte; + uint32_t v_len_state; + uint32_t v_slot; + uint32_t v_len; + uint32_t v_num_extra_bits; + uint32_t v_dist_extra_bits; + uint32_t v_i; + uint32_t v_index_lit; + uint32_t v_index_len; + uint32_t v_index_small_dist_base; + uint32_t v_index_small_dist_extra; + uint32_t v_index_large_dist; + uint64_t scratch; + } s_decode_bitstream_slow; + struct { + uint64_t scratch; + } s_decode_properties; + } 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_lzma__decoder__alloc()); + } + + static inline wuffs_base__io_transformer::unique_ptr + alloc_as__wuffs_base__io_transformer() { + return wuffs_base__io_transformer::unique_ptr( + wuffs_lzma__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_lzma__decoder__struct() = delete; + wuffs_lzma__decoder__struct(const wuffs_lzma__decoder__struct&) = delete; + wuffs_lzma__decoder__struct& operator=( + const wuffs_lzma__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_lzma__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_lzma__decoder__get_quirk(this, a_key); + } + + inline wuffs_base__status + set_quirk( + uint32_t a_key, + uint64_t a_value) { + return wuffs_lzma__decoder__set_quirk(this, a_key, a_value); + } + + inline wuffs_base__optional_u63 + history_retain_length() const { + return wuffs_lzma__decoder__history_retain_length(this); + } + + inline wuffs_base__range_ii_u64 + workbuf_len() const { + return wuffs_lzma__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_lzma__decoder__transform_io(this, a_dst, a_src, a_workbuf); + } + +#endif // __cplusplus +}; // struct wuffs_lzma__decoder__struct + +#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) || defined(WUFFS_NONMONOLITHIC) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZW) || defined(WUFFS_NONMONOLITHIC) // ---------------- Status Codes @@ -15209,6 +15503,7 @@ const char wuffs_base__error__cannot_return_a_suspension[] = "#base: cannot retu const char wuffs_base__error__disabled_by_previous_error[] = "#base: disabled by previous error"; const char wuffs_base__error__initialize_falsely_claimed_already_zeroed[] = "#base: initialize falsely claimed already zeroed"; const char wuffs_base__error__initialize_not_called[] = "#base: initialize not called"; +const char wuffs_base__error__insufficient_history[] = "#base: insufficient history"; const char wuffs_base__error__interleaved_coroutine_calls[] = "#base: interleaved coroutine calls"; const char wuffs_base__error__no_more_information[] = "#base: no more information"; const char wuffs_base__error__not_enough_data[] = "#base: not enough data"; @@ -50487,6 +50782,1675 @@ wuffs_json__decoder__decode_trailer( #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__JSON) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) + +// ---------------- Status Codes Implementations + +const char wuffs_lzma__error__bad_bitstream_trailer[] = "#lzma: bad bitstream trailer"; +const char wuffs_lzma__error__bad_code[] = "#lzma: bad code"; +const char wuffs_lzma__error__bad_decoded_length[] = "#lzma: bad decoded length"; +const char wuffs_lzma__error__bad_distance[] = "#lzma: bad distance"; +const char wuffs_lzma__error__bad_header[] = "#lzma: bad header"; +const char wuffs_lzma__error__truncated_input[] = "#lzma: truncated input"; +const char wuffs_lzma__error__unsupported_decoded_length[] = "#lzma: unsupported decoded length"; +const char wuffs_lzma__error__unsupported_properties[] = "#lzma: unsupported properties"; + +// ---------------- Private Consts + +static const uint8_t +WUFFS_LZMA__STATE_TRANSITION_LITERAL[12] WUFFS_BASE__POTENTIALLY_UNUSED = { + 0u, 0u, 0u, 0u, 1u, 2u, 3u, 4u, + 5u, 6u, 4u, 5u, +}; + +static const uint8_t +WUFFS_LZMA__STATE_TRANSITION_MATCH[12] WUFFS_BASE__POTENTIALLY_UNUSED = { + 7u, 7u, 7u, 7u, 7u, 7u, 7u, 10u, + 10u, 10u, 10u, 10u, +}; + +static const uint8_t +WUFFS_LZMA__STATE_TRANSITION_LONGREP[12] WUFFS_BASE__POTENTIALLY_UNUSED = { + 8u, 8u, 8u, 8u, 8u, 8u, 8u, 11u, + 11u, 11u, 11u, 11u, +}; + +static const uint8_t +WUFFS_LZMA__STATE_TRANSITION_SHORTREP[12] WUFFS_BASE__POTENTIALLY_UNUSED = { + 9u, 9u, 9u, 9u, 9u, 9u, 9u, 11u, + 11u, 11u, 11u, 11u, +}; + +static const uint8_t +WUFFS_LZMA__CLAMP_NO_MORE_THAN_3[8] WUFFS_BASE__POTENTIALLY_UNUSED = { + 0u, 1u, 2u, 3u, 3u, 3u, 3u, 3u, +}; + +// ---------------- Private Initializer Prototypes + +// ---------------- Private Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__decode_bitstream_slow( + wuffs_lzma__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__do_transform_io( + wuffs_lzma__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__decode_properties( + wuffs_lzma__decoder* self, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_lzma__decoder__initialize_probs( + wuffs_lzma__decoder* self); + +// ---------------- VTables + +const wuffs_base__io_transformer__func_ptrs +wuffs_lzma__decoder__func_ptrs_for__wuffs_base__io_transformer = { + (uint64_t(*)(const void*, + uint32_t))(&wuffs_lzma__decoder__get_quirk), + (wuffs_base__optional_u63(*)(const void*))(&wuffs_lzma__decoder__history_retain_length), + (wuffs_base__status(*)(void*, + uint32_t, + uint64_t))(&wuffs_lzma__decoder__set_quirk), + (wuffs_base__status(*)(void*, + wuffs_base__io_buffer*, + wuffs_base__io_buffer*, + wuffs_base__slice_u8))(&wuffs_lzma__decoder__transform_io), + (wuffs_base__range_ii_u64(*)(const void*))(&wuffs_lzma__decoder__workbuf_len), +}; + +// ---------------- Initializer Implementations + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_lzma__decoder__initialize( + wuffs_lzma__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_lzma__decoder__func_ptrs_for__wuffs_base__io_transformer); + return wuffs_base__make_status(NULL); +} + +wuffs_lzma__decoder* +wuffs_lzma__decoder__alloc(void) { + wuffs_lzma__decoder* x = + (wuffs_lzma__decoder*)(calloc(sizeof(wuffs_lzma__decoder), 1)); + if (!x) { + return NULL; + } + if (wuffs_lzma__decoder__initialize( + x, sizeof(wuffs_lzma__decoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) { + free(x); + return NULL; + } + return x; +} + +size_t +sizeof__wuffs_lzma__decoder(void) { + return sizeof(wuffs_lzma__decoder); +} + +// ---------------- Function Implementations + +// -------- func lzma.decoder.decode_bitstream_slow + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__decode_bitstream_slow( + wuffs_lzma__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__io_buffer* a_src) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint8_t v_c8 = 0; + uint32_t v_bits = 0; + uint32_t v_range = 0; + uint32_t v_state = 0; + uint32_t v_rep0 = 0; + uint32_t v_rep1 = 0; + uint32_t v_rep2 = 0; + uint32_t v_rep3 = 0; + uint32_t v_reptmp = 0; + uint32_t v_rep = 0; + uint64_t v_pos = 0; + uint32_t v_lc = 0; + uint64_t v_lp_mask = 0; + uint64_t v_pb_mask = 0; + uint32_t v_prob = 0; + uint32_t v_threshold = 0; + uint32_t v_tree_node = 0; + uint8_t v_prev_byte = 0; + uint8_t v_match_byte = 0; + uint32_t v_match_cusp = 0; + uint32_t v_match_bit = 0; + uint32_t v_len_state = 0; + uint32_t v_slot = 0; + uint32_t v_len = 0; + uint32_t v_num_extra_bits = 0; + uint32_t v_dist_extra_bits = 0; + uint32_t v_high_bit_was_on = 0; + uint32_t v_i = 0; + uint32_t v_index_ao00 = 0; + uint32_t v_index_ao41 = 0; + uint32_t v_index_lit = 0; + uint32_t v_index_len = 0; + uint32_t v_index_small_dist_base = 0; + uint32_t v_index_small_dist_extra = 0; + uint32_t v_index_small_dist = 0; + uint32_t v_index_large_dist = 0; + + 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; + } + } + 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 && a_src->data.ptr) { + 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_decode_bitstream_slow; + if (coro_susp_point) { + v_bits = self->private_data.s_decode_bitstream_slow.v_bits; + v_range = self->private_data.s_decode_bitstream_slow.v_range; + v_state = self->private_data.s_decode_bitstream_slow.v_state; + v_rep0 = self->private_data.s_decode_bitstream_slow.v_rep0; + v_rep1 = self->private_data.s_decode_bitstream_slow.v_rep1; + v_rep2 = self->private_data.s_decode_bitstream_slow.v_rep2; + v_rep3 = self->private_data.s_decode_bitstream_slow.v_rep3; + v_rep = self->private_data.s_decode_bitstream_slow.v_rep; + v_pos = self->private_data.s_decode_bitstream_slow.v_pos; + v_lc = self->private_data.s_decode_bitstream_slow.v_lc; + v_lp_mask = self->private_data.s_decode_bitstream_slow.v_lp_mask; + v_pb_mask = self->private_data.s_decode_bitstream_slow.v_pb_mask; + v_tree_node = self->private_data.s_decode_bitstream_slow.v_tree_node; + v_prev_byte = self->private_data.s_decode_bitstream_slow.v_prev_byte; + v_match_byte = self->private_data.s_decode_bitstream_slow.v_match_byte; + v_len_state = self->private_data.s_decode_bitstream_slow.v_len_state; + v_slot = self->private_data.s_decode_bitstream_slow.v_slot; + v_len = self->private_data.s_decode_bitstream_slow.v_len; + v_num_extra_bits = self->private_data.s_decode_bitstream_slow.v_num_extra_bits; + v_dist_extra_bits = self->private_data.s_decode_bitstream_slow.v_dist_extra_bits; + v_i = self->private_data.s_decode_bitstream_slow.v_i; + v_index_lit = self->private_data.s_decode_bitstream_slow.v_index_lit; + v_index_len = self->private_data.s_decode_bitstream_slow.v_index_len; + v_index_small_dist_base = self->private_data.s_decode_bitstream_slow.v_index_small_dist_base; + v_index_small_dist_extra = self->private_data.s_decode_bitstream_slow.v_index_small_dist_extra; + v_index_large_dist = self->private_data.s_decode_bitstream_slow.v_index_large_dist; + } + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_0 = *iop_a_src++; + v_c8 = t_0; + } + if (v_c8 != 0u) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_code); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + uint32_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_1 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_bitstream_slow.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_bitstream_slow.scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFFu)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1); + if (num_bits_1 == 24) { + t_1 = ((uint32_t)(*scratch >> 32)); + break; + } + num_bits_1 += 8u; + *scratch |= ((uint64_t)(num_bits_1)); + } + } + v_bits = t_1; + } + if (v_bits == 4294967295u) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_code); + goto exit; + } + v_range = 4294967295u; + v_lc = self->private_impl.f_lc; + v_lp_mask = ((((uint64_t)(1u)) << self->private_impl.f_lp) - 1u); + v_pb_mask = ((((uint64_t)(1u)) << self->private_impl.f_pb) - 1u); + while (v_pos < self->private_impl.f_decoded_length) { + v_index_ao00 = ((v_state << 4u) | ((uint32_t)((v_pos & v_pb_mask)))); + v_prob = ((uint32_t)(self->private_data.f_probs_ao00[v_index_ao00])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao00[v_index_ao00] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_2 = *iop_a_src++; + v_c8 = t_2; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_lit = (15u & ((((uint32_t)((v_pos & v_lp_mask))) << v_lc) | (((uint32_t)(v_prev_byte)) >> (8u - v_lc)))); + v_tree_node = 1u; + if (v_state >= 7u) { + while (true) { + v_match_bit = ((uint32_t)((v_match_byte >> 7u))); + v_match_byte <<= 1u; + v_prob = ((uint32_t)(self->private_data.f_probs_lit[v_index_lit][(256u + (v_match_bit << 8u) + v_tree_node)])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_lit[v_index_lit][(256u + (v_match_bit << 8u) + v_tree_node)] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + if ((v_tree_node >= 256u) || (v_match_bit != 0u)) { + break; + } + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_lit[v_index_lit][(256u + (v_match_bit << 8u) + v_tree_node)] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + if ((v_tree_node >= 256u) || (v_match_bit == 0u)) { + break; + } + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_3 = *iop_a_src++; + v_c8 = t_3; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_4 = *iop_a_src++; + v_c8 = t_4; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + while (v_tree_node < 256u) { + v_prob = ((uint32_t)(self->private_data.f_probs_lit[v_index_lit][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_lit[v_index_lit][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_lit[v_index_lit][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_5 = *iop_a_src++; + v_c8 = t_5; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_prev_byte = ((uint8_t)(v_tree_node)); + self->private_data.s_decode_bitstream_slow.scratch = v_prev_byte; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); + 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_decode_bitstream_slow.scratch)); + v_pos += 1u; + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LITERAL[v_state])); + continue; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao00[v_index_ao00] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_6 = *iop_a_src++; + v_c8 = t_6; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + do { + v_prob = ((uint32_t)(self->private_data.f_probs_ao20[v_state])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao20[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_7 = *iop_a_src++; + v_c8 = t_7; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + do { + v_prob = ((uint32_t)(self->private_data.f_probs_match_len_low[0u][0u])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_match_len_low[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_8 = *iop_a_src++; + v_c8 = t_8; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_len = ((uint32_t)((v_pos & v_pb_mask))); + v_tree_node = 1u; + while (v_tree_node < 8u) { + v_prob = ((uint32_t)(self->private_data.f_probs_match_len_low[v_index_len][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_match_len_low[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_match_len_low[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_9 = *iop_a_src++; + v_c8 = t_9; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len_state = ((uint32_t)(WUFFS_LZMA__CLAMP_NO_MORE_THAN_3[(v_tree_node & 7u)])); + v_len = ((v_tree_node & 7u) + 2u); + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_match_len_low[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_10 = *iop_a_src++; + v_c8 = t_10; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_prob = ((uint32_t)(self->private_data.f_probs_match_len_mid[0u][0u])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_match_len_mid[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_11 = *iop_a_src++; + v_c8 = t_11; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_len = ((uint32_t)((v_pos & v_pb_mask))); + v_tree_node = 1u; + while (v_tree_node < 8u) { + v_prob = ((uint32_t)(self->private_data.f_probs_match_len_mid[v_index_len][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_match_len_mid[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_match_len_mid[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_12 = *iop_a_src++; + v_c8 = t_12; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len = ((v_tree_node & 7u) + 10u); + v_len_state = 3u; + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_match_len_mid[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(16); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_13 = *iop_a_src++; + v_c8 = t_13; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_tree_node = 1u; + while (v_tree_node < 256u) { + v_prob = ((uint32_t)(self->private_data.f_probs_match_len_high[0u][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_match_len_high[0u][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_match_len_high[0u][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(17); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_14 = *iop_a_src++; + v_c8 = t_14; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len = ((v_tree_node & 255u) + 18u); + v_len_state = 3u; + } while (0); + v_slot = 1u; + while (v_slot < 64u) { + v_prob = ((uint32_t)(self->private_data.f_probs_slot[v_len_state][v_slot])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_slot[v_len_state][v_slot] = ((uint16_t)(v_prob)); + v_slot = (v_slot << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_slot[v_len_state][v_slot] = ((uint16_t)(v_prob)); + v_slot = ((v_slot << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(18); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_15 = *iop_a_src++; + v_c8 = t_15; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_slot &= 63u; + v_rep = v_slot; + if (v_slot < 4u) { + } else if (v_slot < 14u) { + v_num_extra_bits = ((v_slot >> 1u) - 1u); + v_rep = ((2u | (v_slot & 1u)) << v_num_extra_bits); + v_index_small_dist_base = ((uint32_t)(v_rep - v_slot)); + v_index_small_dist_extra = 1u; + v_dist_extra_bits = 0u; + v_i = 0u; + while (v_i < v_num_extra_bits) { + v_index_small_dist = (((uint32_t)(v_index_small_dist_base + v_index_small_dist_extra)) & 127u); + v_prob = ((uint32_t)(self->private_data.f_probs_small_dist[v_index_small_dist])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_small_dist[v_index_small_dist] = ((uint16_t)(v_prob)); + v_index_small_dist_extra = ((uint32_t)(v_index_small_dist_extra << 1u)); + v_i += 1u; + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_small_dist[v_index_small_dist] = ((uint16_t)(v_prob)); + v_index_small_dist_extra = (((uint32_t)(v_index_small_dist_extra << 1u)) | 1u); + v_dist_extra_bits |= (((uint32_t)(1u)) << v_i); + v_i += 1u; + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(19); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_16 = *iop_a_src++; + v_c8 = t_16; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_rep += v_dist_extra_bits; + } else { + v_num_extra_bits = ((v_slot >> 1u) - 1u); + v_rep = ((2u | (v_slot & 1u)) << v_num_extra_bits); + v_dist_extra_bits = 0u; + while (true) { + v_range >>= 1u; + v_bits -= v_range; + v_high_bit_was_on = ((uint32_t)(0u - (v_bits >> 31u))); + v_bits += (v_range & v_high_bit_was_on); + v_dist_extra_bits = (((uint32_t)(v_dist_extra_bits << 1u)) | (((uint32_t)(v_high_bit_was_on + 1u)) & 1u)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(20); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_17 = *iop_a_src++; + v_c8 = t_17; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_num_extra_bits -= 1u; + if (v_num_extra_bits <= 4u) { + break; + } + } + v_dist_extra_bits <<= 4u; + v_index_large_dist = 1u; + while (true) { + v_prob = ((uint32_t)(self->private_data.f_probs_large_dist[v_index_large_dist])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_large_dist[v_index_large_dist] = ((uint16_t)(v_prob)); + v_index_large_dist = (15u & ((uint32_t)(v_index_large_dist << 1u))); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_large_dist[v_index_large_dist] = ((uint16_t)(v_prob)); + v_index_large_dist = (15u & (((uint32_t)(v_index_large_dist << 1u)) | 1u)); + v_dist_extra_bits |= (((uint32_t)(1u)) << (4u - v_num_extra_bits)); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(21); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_18 = *iop_a_src++; + v_c8 = t_18; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_num_extra_bits -= 1u; + if (v_num_extra_bits <= 0u) { + break; + } + } + v_rep += v_dist_extra_bits; + } + if (v_rep >= 4294967295u) { + goto label__outer__break; + } + v_rep3 = v_rep2; + v_rep2 = v_rep1; + v_rep1 = v_rep0; + v_rep0 = v_rep; + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_MATCH[v_state])); + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao20[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(22); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_19 = *iop_a_src++; + v_c8 = t_19; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_prob = ((uint32_t)(self->private_data.f_probs_ao40[v_state])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao40[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(23); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_20 = *iop_a_src++; + v_c8 = t_20; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_ao41 = ((v_state << 4u) | ((uint32_t)((v_pos & v_pb_mask)))); + v_prob = ((uint32_t)(self->private_data.f_probs_ao41[v_index_ao41])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao41[v_index_ao41] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(24); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_21 = *iop_a_src++; + v_c8 = t_21; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_len = 1u; + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_SHORTREP[v_state])); + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao41[v_index_ao41] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(25); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_22 = *iop_a_src++; + v_c8 = t_22; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao40[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(26); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_23 = *iop_a_src++; + v_c8 = t_23; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_prob = ((uint32_t)(self->private_data.f_probs_ao60[v_state])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao60[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(27); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_24 = *iop_a_src++; + v_c8 = t_24; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_reptmp = v_rep1; + v_rep1 = v_rep0; + v_rep0 = v_reptmp; + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao60[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(28); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_25 = *iop_a_src++; + v_c8 = t_25; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_prob = ((uint32_t)(self->private_data.f_probs_ao63[v_state])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_ao63[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(29); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_26 = *iop_a_src++; + v_c8 = t_26; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_reptmp = v_rep2; + v_rep2 = v_rep1; + v_rep1 = v_rep0; + v_rep0 = v_reptmp; + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_ao63[v_state] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(30); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_27 = *iop_a_src++; + v_c8 = t_27; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_reptmp = v_rep3; + v_rep3 = v_rep2; + v_rep2 = v_rep1; + v_rep1 = v_rep0; + v_rep0 = v_reptmp; + } + } + } + do { + v_prob = ((uint32_t)(self->private_data.f_probs_longrep_len_low[0u][0u])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_longrep_len_low[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(31); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_28 = *iop_a_src++; + v_c8 = t_28; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_len = ((uint32_t)((v_pos & v_pb_mask))); + v_tree_node = 1u; + while (v_tree_node < 8u) { + v_prob = ((uint32_t)(self->private_data.f_probs_longrep_len_low[v_index_len][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_longrep_len_low[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_longrep_len_low[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(32); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_29 = *iop_a_src++; + v_c8 = t_29; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len = ((v_tree_node & 7u) + 2u); + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LONGREP[v_state])); + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_longrep_len_low[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(33); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_30 = *iop_a_src++; + v_c8 = t_30; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_prob = ((uint32_t)(self->private_data.f_probs_longrep_len_mid[0u][0u])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_longrep_len_mid[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(34); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_31 = *iop_a_src++; + v_c8 = t_31; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_index_len = ((uint32_t)((v_pos & v_pb_mask))); + v_tree_node = 1u; + while (v_tree_node < 8u) { + v_prob = ((uint32_t)(self->private_data.f_probs_longrep_len_mid[v_index_len][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_longrep_len_mid[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_longrep_len_mid[v_index_len][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(35); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_32 = *iop_a_src++; + v_c8 = t_32; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len = ((v_tree_node & 7u) + 10u); + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LONGREP[v_state])); + break; + } + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_longrep_len_mid[0u][0u] = ((uint16_t)(v_prob)); + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(36); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_33 = *iop_a_src++; + v_c8 = t_33; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + v_tree_node = 1u; + while (v_tree_node < 256u) { + v_prob = ((uint32_t)(self->private_data.f_probs_longrep_len_high[0u][v_tree_node])); + v_threshold = ((uint32_t)((v_range >> 11u) * v_prob)); + if (v_bits < v_threshold) { + v_range = v_threshold; + v_prob += (((uint32_t)(2048u - v_prob)) >> 5u); + self->private_data.f_probs_longrep_len_high[0u][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = (v_tree_node << 1u); + } else { + v_bits -= v_threshold; + v_range -= v_threshold; + v_prob -= (v_prob >> 5u); + self->private_data.f_probs_longrep_len_high[0u][v_tree_node] = ((uint16_t)(v_prob)); + v_tree_node = ((v_tree_node << 1u) | 1u); + } + if ((v_range >> 24u) == 0u) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(37); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_34 = *iop_a_src++; + v_c8 = t_34; + } + v_bits = (((uint32_t)(v_bits << 8u)) | ((uint32_t)(v_c8))); + v_range <<= 8u; + } + } + v_len = ((v_tree_node & 255u) + 18u); + v_state = ((uint32_t)(WUFFS_LZMA__STATE_TRANSITION_LONGREP[v_state])); + } while (0); + } while (0); + if ((((uint64_t)((v_rep0 + 1u))) > v_pos) || (((uint64_t)((v_rep0 + 1u))) > ((uint64_t)(self->private_impl.f_dict_size)))) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_distance); + goto exit; + } + v_pos += ((uint64_t)(v_len)); + while (((uint64_t)(v_len)) > ((uint64_t)(io2_a_dst - iop_a_dst))) { + status = wuffs_base__make_status(wuffs_base__suspension__short_write); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(38); + } + if (((uint64_t)((v_rep0 + 1u))) > ((uint64_t)(iop_a_dst - io0_a_dst))) { + status = wuffs_base__make_status(wuffs_base__error__insufficient_history); + goto exit; + } + v_match_cusp = wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp( + &iop_a_dst, io0_a_dst, io2_a_dst, v_len, (v_rep0 + 1u)); + v_match_byte = ((uint8_t)((v_match_cusp >> 8u))); + v_prev_byte = ((uint8_t)(v_match_cusp)); + } + label__outer__break:; + if (v_bits != 0u) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_bitstream_trailer); + goto exit; + } else if ((self->private_impl.f_decoded_length != 18446744073709551615u) && (self->private_impl.f_decoded_length != v_pos)) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_decoded_length); + goto exit; + } + + ok: + self->private_impl.p_decode_bitstream_slow = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_bitstream_slow = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + self->private_data.s_decode_bitstream_slow.v_bits = v_bits; + self->private_data.s_decode_bitstream_slow.v_range = v_range; + self->private_data.s_decode_bitstream_slow.v_state = v_state; + self->private_data.s_decode_bitstream_slow.v_rep0 = v_rep0; + self->private_data.s_decode_bitstream_slow.v_rep1 = v_rep1; + self->private_data.s_decode_bitstream_slow.v_rep2 = v_rep2; + self->private_data.s_decode_bitstream_slow.v_rep3 = v_rep3; + self->private_data.s_decode_bitstream_slow.v_rep = v_rep; + self->private_data.s_decode_bitstream_slow.v_pos = v_pos; + self->private_data.s_decode_bitstream_slow.v_lc = v_lc; + self->private_data.s_decode_bitstream_slow.v_lp_mask = v_lp_mask; + self->private_data.s_decode_bitstream_slow.v_pb_mask = v_pb_mask; + self->private_data.s_decode_bitstream_slow.v_tree_node = v_tree_node; + self->private_data.s_decode_bitstream_slow.v_prev_byte = v_prev_byte; + self->private_data.s_decode_bitstream_slow.v_match_byte = v_match_byte; + self->private_data.s_decode_bitstream_slow.v_len_state = v_len_state; + self->private_data.s_decode_bitstream_slow.v_slot = v_slot; + self->private_data.s_decode_bitstream_slow.v_len = v_len; + self->private_data.s_decode_bitstream_slow.v_num_extra_bits = v_num_extra_bits; + self->private_data.s_decode_bitstream_slow.v_dist_extra_bits = v_dist_extra_bits; + self->private_data.s_decode_bitstream_slow.v_i = v_i; + self->private_data.s_decode_bitstream_slow.v_index_lit = v_index_lit; + self->private_data.s_decode_bitstream_slow.v_index_len = v_index_len; + self->private_data.s_decode_bitstream_slow.v_index_small_dist_base = v_index_small_dist_base; + self->private_data.s_decode_bitstream_slow.v_index_small_dist_extra = v_index_small_dist_extra; + self->private_data.s_decode_bitstream_slow.v_index_large_dist = v_index_large_dist; + + goto exit; + exit: + if (a_dst && a_dst->data.ptr) { + a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr)); + } + if (a_src && a_src->data.ptr) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + return status; +} + +// -------- func lzma.decoder.get_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_lzma__decoder__get_quirk( + const wuffs_lzma__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; + } + + return 0u; +} + +// -------- func lzma.decoder.set_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzma__decoder__set_quirk( + wuffs_lzma__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); + } + + return wuffs_base__make_status(wuffs_base__error__unsupported_option); +} + +// -------- func lzma.decoder.history_retain_length + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63 +wuffs_lzma__decoder__history_retain_length( + const wuffs_lzma__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); + } + + if (self->private_impl.f_dict_size == 0u) { + return wuffs_base__utility__make_optional_u63(false, 0u); + } + return wuffs_base__utility__make_optional_u63(true, ((uint64_t)(self->private_impl.f_dict_size))); +} + +// -------- func lzma.decoder.workbuf_len + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_lzma__decoder__workbuf_len( + const wuffs_lzma__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(0u, 0u); +} + +// -------- func lzma.decoder.transform_io + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_lzma__decoder__transform_io( + wuffs_lzma__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_lzma__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_lzma__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 lzma.decoder.do_transform_io + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__do_transform_io( + wuffs_lzma__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); + + uint32_t coro_susp_point = self->private_impl.p_do_transform_io; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_lzma__decoder__decode_properties(self, a_src); + if (status.repr) { + goto suspend; + } + wuffs_lzma__decoder__initialize_probs(self); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_lzma__decoder__decode_bitstream_slow(self, a_dst, a_src); + if (status.repr) { + goto suspend; + } + + 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: + return status; +} + +// -------- func lzma.decoder.decode_properties + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_lzma__decoder__decode_properties( + wuffs_lzma__decoder* self, + wuffs_base__io_buffer* a_src) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint8_t v_c8 = 0; + uint32_t v_c32 = 0; + uint32_t v_lc = 0; + uint32_t v_lp = 0; + uint32_t v_pb = 0; + + 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 && a_src->data.ptr) { + 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_decode_properties; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_0 = *iop_a_src++; + v_c8 = t_0; + } + if (v_c8 >= 225u) { + status = wuffs_base__make_status(wuffs_lzma__error__bad_header); + goto exit; + } + v_lc = ((uint32_t)((v_c8 % 9u))); + v_c8 /= 9u; + v_lp = ((uint32_t)((v_c8 % 5u))); + v_pb = ((uint32_t)((v_c8 / 5u))); + if ((v_lc + v_lp) > 4u) { + status = wuffs_base__make_status(wuffs_lzma__error__unsupported_properties); + goto exit; + } + self->private_impl.f_lc = wuffs_base__u32__min(v_lc, 4u); + self->private_impl.f_lp = v_lp; + self->private_impl.f_pb = v_pb; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + uint32_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_properties.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_properties.scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1; + if (num_bits_1 == 24) { + t_1 = ((uint32_t)(*scratch)); + break; + } + num_bits_1 += 8u; + *scratch |= ((uint64_t)(num_bits_1)) << 56; + } + } + v_c32 = t_1; + } + self->private_impl.f_dict_size = wuffs_base__u32__max(v_c32, 4096u); + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + uint64_t t_2; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 8)) { + t_2 = wuffs_base__peek_u64le__no_bounds_check(iop_a_src); + iop_a_src += 8; + } else { + self->private_data.s_decode_properties.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_properties.scratch; + uint32_t num_bits_2 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_2; + if (num_bits_2 == 56) { + t_2 = ((uint64_t)(*scratch)); + break; + } + num_bits_2 += 8u; + *scratch |= ((uint64_t)(num_bits_2)) << 56; + } + } + self->private_impl.f_decoded_length = t_2; + } + if ((self->private_impl.f_decoded_length >= 9223372036854775808u) && (self->private_impl.f_decoded_length != 18446744073709551615u)) { + status = wuffs_base__make_status(wuffs_lzma__error__unsupported_decoded_length); + goto exit; + } + + goto ok; + ok: + self->private_impl.p_decode_properties = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_properties = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + goto exit; + exit: + if (a_src && a_src->data.ptr) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + return status; +} + +// -------- func lzma.decoder.initialize_probs + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_lzma__decoder__initialize_probs( + wuffs_lzma__decoder* self) { + uint32_t v_i = 0; + uint32_t v_j = 0; + + v_i = 0u; + while (v_i < 192u) { + self->private_data.f_probs_ao00[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 12u) { + self->private_data.f_probs_ao20[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 12u) { + self->private_data.f_probs_ao40[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 192u) { + self->private_data.f_probs_ao41[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 12u) { + self->private_data.f_probs_ao60[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 12u) { + self->private_data.f_probs_ao63[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + v_j = 0u; + while (v_j < 8u) { + self->private_data.f_probs_match_len_low[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + v_j = 0u; + while (v_j < 8u) { + self->private_data.f_probs_match_len_mid[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + v_i = 0u; + while (v_i < 256u) { + self->private_data.f_probs_match_len_high[0u][v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + v_j = 0u; + while (v_j < 8u) { + self->private_data.f_probs_longrep_len_low[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + v_j = 0u; + while (v_j < 8u) { + self->private_data.f_probs_longrep_len_mid[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + v_i = 0u; + while (v_i < 256u) { + self->private_data.f_probs_longrep_len_high[0u][v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 4u) { + v_j = 0u; + while (v_j < 64u) { + self->private_data.f_probs_slot[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + v_i = 0u; + while (v_i < 128u) { + self->private_data.f_probs_small_dist[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + self->private_data.f_probs_large_dist[v_i] = 1024u; + v_i += 1u; + } + v_i = 0u; + while (v_i < 16u) { + v_j = 0u; + while (v_j < 768u) { + self->private_data.f_probs_lit[v_i][v_j] = 1024u; + v_j += 1u; + } + v_i += 1u; + } + return wuffs_base__make_empty_struct(); +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZMA) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZW) // ---------------- Status Codes Implementations diff --git a/std/lzma/README.md b/std/lzma/README.md new file mode 100644 index 00000000..916b3ff9 --- /dev/null +++ b/std/lzma/README.md @@ -0,0 +1,331 @@ +# LZMA + +Lempel–Ziv–Markov chain algorithm (LZMA) is a general purpose compression +format, using range coding and Lempel-Ziv style length-distance +back-references. Various descriptions of the file format are at: + +- [LZMA-SDK reference implementation](https://raw.githubusercontent.com/jljusten/LZMA-SDK/781863cdf592da3e97420f50de5dac056ad352a5/CPP/7zip/Bundles/LzmaSpec/LzmaSpec.cpp) +- [LZMA-SDK specification](https://raw.githubusercontent.com/jljusten/LZMA-SDK/781863cdf592da3e97420f50de5dac056ad352a5/DOC/lzma-specification.txt) +- [Lzip Manual - Stream Format](https://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format) +- [Wikipedia (LZMA)](https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm) + + +## Byms and Packets + +The compressed stream is a stream of "byms" (binary symbols, either 0 or 1; +yeah, it's an awkward term, but less ambiguous than using "bits" for both +compressed and uncompressed things). Byms are conceptually very similar to bits +(binary digits, ubiquitous in computing, a 0.5 KiB file holds 512 bytes, which +is 4096 bits), but are encoded using an adaptive binary range-coder so that N +byms usually compresses to fewer than N bits. + +The stream is divided into packets, each packet describing either a single +byte, or an LZ77 sequence with its length and distance implicitly or explicitly +encoded. Each part of each packet is modeled with independent contexts, so the +probability predictions for each bym are correlated with the values of that bym +(and related byms from the same field) in previous packets of the same type. + +There are 7 types of packets: + + Symbols Meaning + 0 ,literal LITERAL byte (8_byms) + 1,0 ,len ,dist MATCH + 1,1,0,0 SHORTREP len = 1, dist = Most Recently Used + 1,1,0,1 ,len LONGREP[0] dist = Most Recently Used + 1,1,1,0 ,len LONGREP[1] dist = 2nd Most Recently Used + 1,1,1,1,0 ,len LONGREP[2] dist = 3rd Most Recently Used + 1,1,1,1,1 ,len LONGREP[3] dist = 4th Most Recently Used + +The length encoding: + + Symbols Value + 0 ,3_byms Ranges from 2 ..= 9 + 1,0 ,3_byms Ranges from 10 ..= 17 + 1,1 ,8_byms Ranges from 18 ..= 273 + +The distance encoding starts with a 6-bym "Slot" value, which determines how +many further byms are needed. For small Slot values, there are up to 5 extra +byms, all context-encoded like the rest of LZMA. For large Slot values, there +are N extra byms. The first (N - 4) of them are encoded with a fixed 50% +probability and the remaining 4 byms are context-encoded. The largest encodable +distance-biased-by-1 is `0xFFFF_FFFF`, a (2 + 26 + 4) bit number. + + Slot (decimal) Distance (binary), biased by 1 Extra byms + 0 0 0 + 1 1 0 + 2 10 0 + 3 11 0 + 4 10 x 1 + 5 11 x 1 + 6 10 xx 2 + 7 11 xx 2 + 8 10 xxx 3 + 9 11 xxx 4 + 10 10 xxxx 5 + 11 11 xxxx 4 + 12 10 xxxxx 5 + 13 11 xxxxx 5 + 14 10 yy zzzz 2+4 + 15 11 yy zzzz 2+4 + 16 10 yyy zzzz 3+4 + 17 11 yyy zzzz 3+4 + 18 10 yyyy zzzz 4+4 + ... ... ... + 61 11 yyyyyyyyyyyyyyyyyyyyyyyyy zzzz 25+4 + 62 10 yyyyyyyyyyyyyyyyyyyyyyyyyy zzzz 26+4 + 63 11 yyyyyyyyyyyyyyyyyyyyyyyyyy zzzz 26+4 + +"xxxx" means up-to-5 byms are encoded with a "reverse" binary tree. Each Slot +has its own binary tree probabilities. + +"yyyy" means up-to-26 byms. Each has a fixed 50% probability. + +"zzzz" means four byms encoded with a "reverse" binary tree. All Slots use the +same "align" binary tree probabilities. + +"Biased by 1" means that slot=2 implies biasedDistance=2 so distance=3. A +biasedDistance of `0xFFFF_FFFF` means the End of Stream. Otherwise, the +corrected (unbiased) distance ranges in `[1 ..= 0xFFFF_FFFF]`. + +Distance is also capped by the dictionary size, stated in the LZMA header. + + +## State + +In addition to the hundreds of contextual probabilities (see below), there's +also a "State" variable with 12 possible values. The initial State is zero. +Each packet causes a state transtition. The left-most column is the current +State, the other columns hold the next State, depending on the packet: + + State LITERAL MATCH LONGREP SHORTREP + 0 0 7 8 9 + 1 0 7 8 9 + 2 0 7 8 9 + 3 0 7 8 9 + 4 1 7 8 9 + 5 2 7 8 9 + 6 3 7 8 9 + 7 4 10 11 11 + 8 5 10 11 11 + 9 6 10 11 11 + 10 4 10 11 11 + 11 5 10 11 11 + +Equivalently, but looking backwards instead of forwards, each State embodies +the 1st, 2nd, 3rd and 4th MRUP (Most Recently Used Packet). The ? question mark +means every possible packet. Some States (2, 5 and 11) have two rows - multiple +possible histories could lead to that State: + + State 1MRUP 2MRUP 3MRUP 4MRUP + 0 LITERAL LITERAL LITERAL ? + 1 LITERAL LITERAL MATCH ? + 2a LITERAL LITERAL LONGREP ? + 2b LITERAL LITERAL SHORTREP NON-LITERAL + 3 LITERAL LITERAL SHORTREP LITERAL + 4 LITERAL MATCH ? ? + 5a LITERAL LONGREP ? ? + 5b LITERAL SHORTREP NON-LITERAL ? + 6 LITERAL SHORTREP LITERAL ? + 7 MATCH LITERAL ? ? + 8 LONGREP LITERAL ? ? + 9 SHORTREP LITERAL ? ? + 10 MATCH NON-LITERAL ? ? + 11a LONGREP NON-LITERAL ? ? + 11b SHORTREP NON-LITERAL ? ? + +A State's 12 possible values can also be aggregated into whether the State is +less than or at least 7: whether the last packet was a LITERAL or a NON-LITERAL +(a Lempel-Ziv copy-from-history). When decoding a LITERAL after a NON-LITERAL +(as opposed to a LITERAL after a LITERAL, i.e. having `decodeLiteral()` branch +on `State < 7`), there is information in the next historical byte after the +previous NON-LITERAL packet's copy-source. That byte almost always does not +equal the about-to-be-decoded literal byte, because if it did equal, the copy +would have been longer. + + +## Algorithm Overview + +The core loop's body (with AlgOveNN line numbers for others to reference) looks +conceptually like this: + + AlgOve00 if decodeTheNextBym() == 0 { + AlgOve01 // Decode a LITERAL. + AlgOve02 literal = decodeLiteral() + AlgOve03 emitLiteral(literal) + AlgOve04 continue + AlgOve.. + AlgOve20 } else if decodeTheNextBym() == 0 { + AlgOve21 // Decode a MATCH. + AlgOve22 len = decodeLen() + AlgOve23 slot = decodeSlot(min(len-2, 3)) + AlgOve24 distBiasedBy1 = decodeDistBiasedBy1(slot) + AlgOve25 if distBiasedBy1 == 0xFFFF_FFFF { + AlgOve26 break + AlgOve27 } + AlgOve28 mrud = (1 + distBiasedBy1, mrud[0], mrud[1], mrud[2]) + AlgOve29 goto labelDoTheLZCopy + AlgOve.. + AlgOve40 } else if decodeTheNextBym() == 0 { + AlgOve41 if decodeTheNextBym() == 0 { + AlgOve42 // Decode a SHORTREP. + AlgOve43 len = 1 + AlgOve44 goto labelDoTheLZCopy + AlgOve45 } + AlgOve46 // Decode a LONGREP[0]. + AlgOve.. + AlgOve60 } else if decodeTheNextBym() == 0 { + AlgOve61 // Decode a LONGREP[1]. + AlgOve62 mrud = (mrud[1], mrud[0], mrud[2], mrud[3]) + AlgOve63 } else if decodeTheNextBym() == 0 { + AlgOve64 // Decode a LONGREP[2]. + AlgOve65 mrud = (mrud[2], mrud[0], mrud[1], mrud[3]) + AlgOve66 } else { + AlgOve67 // Decode a LONGREP[3]. + AlgOve68 mrud = (mrud[3], mrud[0], mrud[1], mrud[2]) + AlgOve69 } + AlgOve.. + AlgOve80 len = decodeLen() + AlgOve.. + AlgOve90 labelDoTheLZCopy: + AlgOve91 // mrud[0] has been set to what will be (after the emitCopy + AlgOve92 // call) the most recently used distance. mrud[1] is the 2nd + AlgOve93 // most recently used, mrud[2] is the 3rd, mrud[3] is the 4th. + AlgOve94 emitCopy(len, mrud[0]) + +Conceptually means that some details have been elided. The `State` variable +needs updating after each iteration, but that's not shown. `decodeTheNextBym()` +also depends not only on the `State` but also which line number you're at. The +probabilities used for "is it a LITERAL or NON-LITERAL" are different from the +probabilities used for "is it a LONGREP[2] or LONGREP[3]". + +`decodeLen` expands to: + + AlgOveNN.0 if decodeTheNextBym() == 0 { + AlgOveNN.1 // Decode a low length. + AlgOveNN.2 len = decodeMultipleByms(3) + 2 + AlgOveNN.3 } else if decodeTheNextBym() == 0 { + AlgOveNN.4 // Decode a middle length. + AlgOveNN.5 len = decodeMultipleByms(3) + 10 + AlgOveNN.6 } else { + AlgOveNN.7 // Decode a high length. + AlgOveNN.8 len = decodeMultipleByms(8) + 18 + AlgOveNN.9 } + + +## Range Coding and `decodeTheNextBym` + +The codec also tracks two `uint32` variables, called `bits` and `range`, that +represent an interval (a position and width) on the number line between zero +and one. The literal bitstream (the 0s and 1s in the file, read in MSB to LSB +order) is translated into a `bym` stream, where each `bym` is either 0 or 1. If +both `bym` values have equal (50%) probability then the `bym` stream is +basically the same as the bitstream. If symbols have unequal probability then +we can achieve compression: the literal bitstream can be shorter (in terms of +number of bits) then the `bym` stream. + +Specifically, if `prob` is the probability (scaled so that `(1 << 11) = 2048` +means 100% probability and so `1024` means 50% probability) that the next `bym` +is 0, and `range` is at least `(1 << 24)`, then (to over-simplify for now) +`decodeTheNextBym()` is: + + threshold = (range >> 11) * prob + if bits < threshold { + bym = 0 + } else { + bym = 1 + } + +The probability `prob` depends on the `State` variable as well as additional +context, such as the high bits of the most recent decoded byte and the low bits +of the number of decoded bytes. Implementations maintain an array with hundreds +of contextual probabilities (each a `uint16` in the range `1 ..= 2047`), +indexed by a `prob_index` variable (calculated from the `State` and other +factors). Expanding (marked by ¶) the over-simplified pseudo-code above: + + prob = probs_array[prob_index] // ¶ + threshold = (range >> 11) * prob + if bits < threshold { + bym = 0 + } else { + bym = 1 + } + +LZMA probabilties also adapt. Producing a 0 (or 1) `bym` increases (or +decreases) the relevant contextual probability: the prediction that, the next +time we're in the same context, that next `bym` is 0. Expanding again: + + prob = probs_array[prob_index] + threshold = (range >> 11) * prob + if bits < threshold { + bym = 0 + prob += (2048 - prob) >> 5 // ¶ + probs_array[prob_index] = prob // ¶ + } else { + bym = 1 + prob -= prob >> 5 // ¶ + probs_array[prob_index] = prob // ¶ + } + +We also need to update the `bits` and `range`. The `range` shrinks every time +we decode a `bym` and, if small enough, then grows by reading the next source +byte. Expanding one last time, `decodeTheNextBym()` (which also updates `bits`, +`range` and the relevant contextual probability) is: + + prob = probs_array[prob_index] + threshold = (range >> 11) * prob + if bits < threshold { + bym = 0 + range = threshold // ¶ + prob += (2048 - prob) >> 5 + probs_array[prob_index] = prob + } else { + bym = 1 + bits -= threshold // ¶ + range -= threshold // ¶ + prob -= prob >> 5 + probs_array[prob_index] = prob + } + if (range >> 24) == 0 { // ¶ + bits = (bits << 8) | src.read_byte() // ¶ + range <<= 8 // ¶ + } // ¶ + + +## Binary Trees and `decodeMultipleByms` + +Decoding N byms (e.g. decoding a literal, a length, a slot or a distance) is +basically N sequential calls to `decodeTheNextBym`, using `((1 << N) - 1)` +different probabilties. This can be visualized as a balanced binary tree with +`((1 << N) - 1)` branch nodes and `(1 << N)` leaf nodes. Each branch node has +its own probability for taking one or the other of its two child nodes. + +- There's 1 probability for the first bym. +- There's 2 probabilities for the second bym. The one that is used depends on + the value of the first bym. +- There's 4 probabilities for the third bym. The one that is used depends on + the values of the first two byms. +- Etc. +- There's `(1 << (N - 1))` probabilities for the Nth bym. The one that is used + depends on the values of all previous byms. +- There's `(1 << N)` leaf nodes, one for each possible N-bit value. + +These probabilities can be flattened to a `(1 << N)` element array, whose first +element is unused (or available for repurposing, such as for what the reference +implementation calls the "choice" probabilities). For example, if N is 3 then +the 8-element array could be labeled as `u1223333`, where `u` is unused, `1` +marks the probability for the first bym, `2` marks the probabilities for the +second bym, etc. + +Algorithmically, we loop N times, walking the tree from the root to a leaf. The +final N-bit value is captured in the tree node number (after applying a bit +mask or, equivalently, a subtraction): + + tree_node = 1 + while tree_node < (1 << N) { + bym = decodeTheNextBym() // Using prob_index = tree_node. + tree_node = (tree_node << 1) | bym + } + tree_node &= ((1 << N) - 1) // Equivalent to "tree_node -= (1 << N)". + +That's a "forward" binary tree, where the bits occur in MSB to LSB order. A +"reverse" binary tree uses the opposite order. diff --git a/std/lzma/decode_bitstream_slow.wuffs b/std/lzma/decode_bitstream_slow.wuffs new file mode 100644 index 00000000..2bb9be6d --- /dev/null +++ b/std/lzma/decode_bitstream_slow.wuffs @@ -0,0 +1,835 @@ +// Copyright 2023 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 + +// -------- + +// The AlgOveNN line numbers refer to the ./README.md file. +// +// Most LZMA C/C++ implementations use macros for the "decodeTheNextBym()" code +// fragment in that "Algorithm Overview" section, repeated multiple times in +// the decoding algorithm. Wuffs does not have macros (or inline functions), so +// its LZMA implementation looks relatively verbose and repetitive (even though +// it's equivalent to the C/C++ code, at the machine code level). + +pri func decoder.decode_bitstream_slow?(dst: base.io_writer, src: base.io_reader) { + var c8 : base.u8 + + var bits : base.u32 + var range : base.u32 + + var state : base.u32[..= 11] + + // repN is what the "Algorithm Overview" calls (mrud[N] - 1). The minimum + // distance is 1. + var rep0 : base.u32[..= 0xFFFF_FFFE] + var rep1 : base.u32[..= 0xFFFF_FFFE] + var rep2 : base.u32[..= 0xFFFF_FFFE] + var rep3 : base.u32[..= 0xFFFF_FFFE] + var reptmp : base.u32[..= 0xFFFF_FFFE] + var rep : base.u32 + + var pos : base.u64 + + var lc : base.u32[..= 8] + var lp_mask : base.u64[..= 15] + var pb_mask : base.u64[..= 15] + + var prob : base.u32 + var threshold : base.u32 + var tree_node : base.u32[..= 0x1FF] + var prev_byte : base.u8 + var match_byte : base.u8 + var match_cusp : base.u32[..= 0xFFFF] + var match_bit : base.u32[..= 1] + var len_state : base.u32[..= 3] // Equals min(len-2, 3) when in a MATCH. + var slot : base.u32[..= 0x7F] + var len : base.u32[..= 273] + + var num_extra_bits : base.u32[..= 30] + var dist_extra_bits : base.u32 + var high_bit_was_on : base.u32 + var i : base.u32 + + var index_ao00 : base.u32[..= (12 << 4) - 1] + var index_ao41 : base.u32[..= (12 << 4) - 1] + var index_lit : base.u32[..= (1 << 4) - 1] + var index_len : base.u32[..= (1 << 4) - 1] + + var index_small_dist_base : base.u32 + var index_small_dist_extra : base.u32 + var index_small_dist : base.u32[..= 127] + + var index_large_dist : base.u32[..= 15] + + // Initialize the bitstream. + c8 = args.src.read_u8?() + if c8 <> 0x00 { + return "#bad code" + } + bits = args.src.read_u32be?() + if bits == 0xFFFF_FFFF { + return "#bad code" + } + range = 0xFFFF_FFFF + + lc = this.lc + lp_mask = ((1 as base.u64) << this.lp) - 1 + pb_mask = ((1 as base.u64) << this.pb) - 1 + + // "outer" is the core loop discussed in README.md's "Algorithm Overview". + while.outer pos < this.decoded_length { + // AlgOve00 if decodeTheNextBym() == 0 + index_ao00 = (state << 4) | ((pos & pb_mask) as base.u32) + prob = this.probs_ao00[index_ao00] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao00[index_ao00] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve01 // Decode a LITERAL. + // AlgOve02 literal = decodeLiteral() + + index_lit = 15 & ( + (((pos & lp_mask) as base.u32) << lc) | + ((prev_byte as base.u32) >> (8 - lc))) + tree_node = 1 + + if state >= 7 { + assert tree_node < 0x100 + + while true, + pre tree_node < 0x100, + { + match_bit = (match_byte >> 7) as base.u32 + match_byte ~mod<<= 1 + prob = this.probs_lit[index_lit][0x100 + (match_bit << 8) + tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_lit[index_lit][0x100 + (match_bit << 8) + tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + if (tree_node >= 0x100) or (match_bit <> 0) { + break + } + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_lit[index_lit][0x100 + (match_bit << 8) + tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + if (tree_node >= 0x100) or (match_bit == 0) { + break + } + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } + + while tree_node < 0x100 { + prob = this.probs_lit[index_lit][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_lit[index_lit][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_lit[index_lit][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + + // AlgOve03 emitLiteral(literal) + // AlgOve04 continue + prev_byte = (tree_node & 0xFF) as base.u8 + args.dst.write_u8?(a: prev_byte) + pos ~mod+= 1 + state = STATE_TRANSITION_LITERAL[state] as base.u32 + continue.outer + } + + // AlgOve00a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao00[index_ao00] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + while.goto_do_the_lz_copy true, + post len >= 1, + {{ + // AlgOve20 if decodeTheNextBym() == 0 + prob = this.probs_ao20[state] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao20[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve21 // Decode a MATCH. + + while.goto_have_len true, + post len >= 1, + {{ + // AlgOve22 len = decodeLen() + + // AlgOve22.0 if decodeTheNextBym() == 0 + prob = this.probs_match_len_low[0][0] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_match_len_low[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve22.1 // Decode a low length. + // AlgOve22.2 len = decodeMultipleByms(3) + 2 + index_len = (pos & pb_mask) as base.u32 + tree_node = 1 + while tree_node < 0x08 { + prob = this.probs_match_len_low[index_len][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_match_len_low[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_match_len_low[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len_state = CLAMP_NO_MORE_THAN_3[tree_node & 0x07] as base.u32 + len = (tree_node & 0x07) + 2 + assert len >= 1 + break.goto_have_len // goto AlgOve23. + } + + // AlgOve22.0a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_match_len_low[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve22.3 if decodeTheNextBym() == 0 + prob = this.probs_match_len_mid[0][0] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_match_len_mid[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve22.4 // Decode a middle length. + // AlgOve22.5 len = decodeMultipleByms(3) + 10 + index_len = (pos & pb_mask) as base.u32 + tree_node = 1 + while tree_node < 0x08 { + prob = this.probs_match_len_mid[index_len][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_match_len_mid[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_match_len_mid[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len = (tree_node & 0x07) + 10 + assert len >= 1 + len_state = 3 + break.goto_have_len // goto AlgOve23. + } + + // AlgOve22.3a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_match_len_mid[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve22.7 // Decode a high length. + // AlgOve22.8 len = decodeMultipleByms(8) + 18 + tree_node = 1 + while tree_node < 0x100 { + prob = this.probs_match_len_high[0][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_match_len_high[0][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_match_len_high[0][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len = (tree_node & 0xFF) + 18 + assert len >= 1 + len_state = 3 + break.goto_have_len + }} endwhile.goto_have_len + + // AlgOve23 slot = decodeSlot(min(len-2, 3)) + slot = 1 + while slot < 0x40, + inv len >= 1, + { + prob = this.probs_slot[len_state][slot] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_slot[len_state][slot] = (prob & 0xFFFF) as base.u16 + slot = (slot << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_slot[len_state][slot] = (prob & 0xFFFF) as base.u16 + slot = (slot << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + slot &= 0x3F + + // AlgOve24 distBiasedBy1 = decodeDistBiasedBy1(slot) + rep = slot + if slot < 4 { + // No-op. + + } else if slot < 14 { + num_extra_bits = (slot >> 1) - 1 + rep = (2 | (slot & 1)) << num_extra_bits + + // Read num_extra_bits "xxxx" bits. + index_small_dist_base = rep ~mod- slot + index_small_dist_extra = 1 + dist_extra_bits = 0 + i = 0 + while i < num_extra_bits, + inv len >= 1, + { + assert i < 30 via "a < b: a < c; c <= b"(c: num_extra_bits) + index_small_dist = (index_small_dist_base ~mod+ index_small_dist_extra) & 127 + prob = this.probs_small_dist[index_small_dist] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_small_dist[index_small_dist] = (prob & 0xFFFF) as base.u16 + index_small_dist_extra = (index_small_dist_extra ~mod<< 1) + i += 1 + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_small_dist[index_small_dist] = (prob & 0xFFFF) as base.u16 + index_small_dist_extra = (index_small_dist_extra ~mod<< 1) | 1 + dist_extra_bits |= (1 as base.u32) << i + i += 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + rep ~mod+= dist_extra_bits + + } else { + num_extra_bits = (slot >> 1) - 1 + assert num_extra_bits >= 6 + assert num_extra_bits <= 30 + rep = (2 | (slot & 1)) << num_extra_bits + + // Read (num_extra_bits - 4) "yyyy" bits. + dist_extra_bits = 0 + while true, + pre num_extra_bits > 4, + inv len >= 1, + post num_extra_bits > 0, + post num_extra_bits <= 4, + { + range >>= 1 + bits ~mod-= range + high_bit_was_on = 0 ~mod- (bits >> 31) + bits ~mod+= range & high_bit_was_on + dist_extra_bits = (dist_extra_bits ~mod<< 1) | ((high_bit_was_on ~mod+ 1) & 1) + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + num_extra_bits -= 1 + if num_extra_bits <= 4 { + break + } + } endwhile + dist_extra_bits ~mod<<= 4 + + // Read 4 "zzzz" bits. + index_large_dist = 1 + while true, + pre num_extra_bits > 0, + pre num_extra_bits <= 4, + inv len >= 1, + { + prob = this.probs_large_dist[index_large_dist] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_large_dist[index_large_dist] = (prob & 0xFFFF) as base.u16 + index_large_dist = 15 & ((index_large_dist ~mod<< 1)) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_large_dist[index_large_dist] = (prob & 0xFFFF) as base.u16 + index_large_dist = 15 & ((index_large_dist ~mod<< 1) | 1) + dist_extra_bits |= (1 as base.u32) << (4 - num_extra_bits) + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + num_extra_bits -= 1 + if num_extra_bits <= 0 { + break + } + } endwhile + rep ~mod+= dist_extra_bits + } + + // AlgOve25 if distBiasedBy1 == 0xFFFF_FFFF + if rep >= 0xFFFF_FFFF { + // AlgOve26 break + break.outer + } + + // AlgOve28 mrud = (1 + distBiasedBy1, mrud[0], mrud[1], mrud[2]) + rep3 = rep2 + rep2 = rep1 + rep1 = rep0 + rep0 = rep + + assert len >= 1 + state = STATE_TRANSITION_MATCH[state] as base.u32 + break.goto_do_the_lz_copy // goto AlgOve90. + } + + // AlgOve20a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao20[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve40 if decodeTheNextBym() == 0 + prob = this.probs_ao40[state] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao40[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve41 if decodeTheNextBym() == 0 + index_ao41 = (state << 4) | ((pos & pb_mask) as base.u32) + prob = this.probs_ao41[index_ao41] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao41[index_ao41] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve42 // Decode a SHORTREP. + // AlgOve43 len = 1 + len = 1 + + assert len >= 1 + state = STATE_TRANSITION_SHORTREP[state] as base.u32 + break.goto_do_the_lz_copy // goto AlgOve90. + } + + // AlgOve41a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao41[index_ao41] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve46 // Decode a LONGREP[0]. + + } else { + // AlgOve40a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao40[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve60 if decodeTheNextBym() == 0 + prob = this.probs_ao60[state] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao60[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve61 // Decode a LONGREP[1]. + // AlgOve62 mrud = (mrud[1], mrud[0], mrud[2], mrud[3]) + reptmp = rep1 + rep1 = rep0 + rep0 = reptmp + + } else { + // AlgOve60a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao60[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve63 if decodeTheNextBym() == 0 + prob = this.probs_ao63[state] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_ao63[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve64 // Decode a LONGREP[2]. + // AlgOve65 mrud = (mrud[2], mrud[0], mrud[1], mrud[3]) + reptmp = rep2 + rep2 = rep1 + rep1 = rep0 + rep0 = reptmp + + } else { + // AlgOve63a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_ao63[state] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve67 // Decode a LONGREP[3]. + // AlgOve68 mrud = (mrud[3], mrud[0], mrud[1], mrud[2]) + reptmp = rep3 + rep3 = rep2 + rep2 = rep1 + rep1 = rep0 + rep0 = reptmp + } + } + } + + // AlgOve80 len = decodeLen() + while.goto_after_decode_len true, + post len >= 1, + {{ + // AlgOve80.0 if decodeTheNextBym() == 0 + prob = this.probs_longrep_len_low[0][0] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_longrep_len_low[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve80.1 // Decode a low length. + // AlgOve80.2 len = decodeMultipleByms(3) + 2 + index_len = (pos & pb_mask) as base.u32 + tree_node = 1 + while tree_node < 0x08 { + prob = this.probs_longrep_len_low[index_len][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_longrep_len_low[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_longrep_len_low[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len = (tree_node & 0x07) + 2 + assert len >= 1 + state = STATE_TRANSITION_LONGREP[state] as base.u32 + break.goto_after_decode_len + } + + // AlgOve80.0a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_longrep_len_low[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve80.3 if decodeTheNextBym() == 0 + prob = this.probs_longrep_len_mid[0][0] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_longrep_len_mid[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve80.4 // Decode a middle length. + // AlgOve80.5 len = decodeMultipleByms(3) + 10 + index_len = (pos & pb_mask) as base.u32 + tree_node = 1 + while tree_node < 0x08 { + prob = this.probs_longrep_len_mid[index_len][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_longrep_len_mid[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_longrep_len_mid[index_len][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len = (tree_node & 0x07) + 10 + assert len >= 1 + state = STATE_TRANSITION_LONGREP[state] as base.u32 + break.goto_after_decode_len + } + + // AlgOve80.3a if decodeTheNextBym() == 0 // If-false branch. + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_longrep_len_mid[0][0] = (prob & 0xFFFF) as base.u16 + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + + // AlgOve80.7 // Decode a high length. + // AlgOve80.8 len = decodeMultipleByms(8) + 18 + tree_node = 1 + while tree_node < 0x100 { + prob = this.probs_longrep_len_high[0][tree_node] as base.u32 + threshold = (range >> 11) ~mod* prob + if bits < threshold { + range = threshold + prob ~mod+= (2048 ~mod- prob) >> 5 + this.probs_longrep_len_high[0][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) + } else { + bits ~mod-= threshold + range ~mod-= threshold + prob ~mod-= prob >> 5 + this.probs_longrep_len_high[0][tree_node] = (prob & 0xFFFF) as base.u16 + tree_node = (tree_node << 1) | 1 + } + if (range >> 24) == 0 { + c8 = args.src.read_u8?() + bits = (bits ~mod<< 8) | (c8 as base.u32) + range ~mod<<= 8 + } + } endwhile + len = (tree_node & 0xFF) + 18 + assert len >= 1 + state = STATE_TRANSITION_LONGREP[state] as base.u32 + + break.goto_after_decode_len + }} endwhile.goto_after_decode_len + break.goto_do_the_lz_copy + }} endwhile.goto_do_the_lz_copy + + // AlgOve90 labelDoTheLZCopy: + // AlgOve94 emitCopy(len, mrud[0]) + assert len >= 1 + if (((rep0 + 1) as base.u64) > pos) or + (((rep0 + 1) as base.u64) > (this.dict_size as base.u64)) { + return "#bad distance" + } + pos ~mod+= len as base.u64 + while (len as base.u64) > args.dst.length(), + inv len >= 1, + post (len as base.u64) <= args.dst.length(), + { + yield? base."$short write" + } endwhile + if ((rep0 + 1) as base.u64) > args.dst.history_length() { + return base."#insufficient history" + } + assert (rep0 + 1) >= 1 + match_cusp = args.dst.limited_copy_u32_from_history_fast_return_cusp!( + up_to: len, distance: (rep0 + 1)) + match_byte = (match_cusp >> 8) as base.u8 + prev_byte = (match_cusp & 0xFF) as base.u8 + } endwhile.outer + + if bits <> 0 { + return "#bad bitstream trailer" + } else if (this.decoded_length <> 0xFFFF_FFFF_FFFF_FFFF) and + (this.decoded_length <> pos) { + return "#bad decoded length" + } +} diff --git a/std/lzma/decode_lzma.wuffs b/std/lzma/decode_lzma.wuffs new file mode 100644 index 00000000..06f96d5f --- /dev/null +++ b/std/lzma/decode_lzma.wuffs @@ -0,0 +1,324 @@ +// Copyright 2023 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 + +pub status "#bad bitstream trailer" +pub status "#bad code" +pub status "#bad decoded length" +pub status "#bad distance" +pub status "#bad header" +pub status "#truncated input" +pub status "#unsupported decoded length" +pub status "#unsupported properties" + +pub const DECODER_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE : base.u64 = 0xFFFF_FFFF + +pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 + +pub struct decoder? implements base.io_transformer( + // In theory, lc ranges in [0 ..= 8]. In practice, to keep the + // worst-case size of the statically allocated probs_lit array + // managable, we only support configurations where ((lc + lp) <= 4) and + // otherwise return "#unsupported properties". + // + // This matches "LZMA2's limited property condition" described at: + // https://github.com/jljusten/LZMA-SDK/blob/781863cdf592da3e97420f50de5dac056ad352a5/DOC/lzma-specification.txt#L192 + lc : base.u32[..= 4], // The number of Literal Context bits. Default: 3. + lp : base.u32[..= 4], // The number of Literal Position bits. Default: 0. + pb : base.u32[..= 4], // The number of Position Bits. Default: 2. + + dict_size : base.u32, + + decoded_length : base.u64, + + util : base.utility, +) + ( + // 12 is the number of states. 4 is the pb inclusive maximum. + // + // The AlgOveNN line numbers refer to the ./README.md file. + probs_ao00 : array[12 << 4] base.u16, // AlgOve00. + probs_ao20 : array[12] base.u16, // AlgOve20. + probs_ao40 : array[12] base.u16, // AlgOve40. + probs_ao41 : array[12 << 4] base.u16, // AlgOve41. + probs_ao60 : array[12] base.u16, // AlgOve60. + probs_ao63 : array[12] base.u16, // AlgOve63. + + // AlgOve22 and what "decodeLen expands to". + probs_match_len_low : array[1 << 4] array[1 << 3] base.u16, + probs_match_len_mid : array[1 << 4] array[1 << 3] base.u16, + probs_match_len_high : array[1 << 0] array[1 << 8] base.u16, + + // AlgOve80 and what "decodeLen expands to". + probs_longrep_len_low : array[1 << 4] array[1 << 3] base.u16, + probs_longrep_len_mid : array[1 << 4] array[1 << 3] base.u16, + probs_longrep_len_high : array[1 << 0] array[1 << 8] base.u16, + + // AlgOve23. The first index is min(len-2, 3). There are then 64 + // possible slots, so a 6-bit binary tree. + probs_slot : array[4] array[1 << 6] base.u16, + + // This holds the "xxxx" binary trees, packed per below. The first + // element and last 13 elements are unused. The index_small_dist_base + // constant is set to (Low - 1) and index_small_dist_extra variable + // starts at 1. (index_small_dist_base + index_small_dist_extra) ranges + // in Low .. High during the "decode a binary tree" loop. + // + // Slot Low .. High H-L NumberOfXByms + // 4 1 .. 2 1 1 + // 5 2 .. 3 1 1 + // 6 3 .. 6 3 2 + // 7 6 .. 9 3 2 + // 8 9 .. 16 7 3 + // 9 16 .. 23 7 3 + // 10 23 .. 38 15 4 + // 11 38 .. 53 15 4 + // 12 53 .. 84 31 5 + // 13 84 .. 115 31 5 + probs_small_dist : array[128] base.u16, + + // This holds the "zzzz" binary tree. + probs_large_dist : array[1 << 4] base.u16, + + // 4 is the (lc + lp) inclusive maximum. The 0x300 holds three 8-bit + // binary trees. The first is for (state < 7), when the previous packet + // was a LITERAL. The other two trees are for (state >= 7), when it was + // an LZ-copy. Which tree gets used depends on the bits in the next + // historical byte after the LZ-copy-source, up until the point where + // those bits don't match the currently decoded literal byte, when it + // drops back to the first tree. + probs_lit : array[1 << 4] array[0x300] base.u16, +) + +pub func decoder.get_quirk(key: base.u32) base.u64 { + return 0 +} + +pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status { + return base."#unsupported option" +} + +pub func decoder.history_retain_length() base.optional_u63 { + if this.dict_size == 0 { + return this.util.make_optional_u63(has_value: false, value: 0) + } + return this.util.make_optional_u63(has_value: true, value: this.dict_size as base.u64) +} + +pub func decoder.workbuf_len() base.range_ii_u64 { + return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0) +} + +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) { + this.decode_properties?(src: args.src) + this.initialize_probs!() + this.decode_bitstream_slow?(dst: args.dst, src: args.src) +} + +pri func decoder.decode_properties?(src: base.io_reader) { + var c8 : base.u8 + var c32 : base.u32 + + var lc : base.u32[..= 8] + var lp : base.u32[..= 4] + var pb : base.u32[..= 4] + + c8 = args.src.read_u8?() + if c8 >= 225 { + return "#bad header" + } + lc = (c8 % 9) as base.u32 + c8 /= 9 + lp = (c8 % 5) as base.u32 + pb = (c8 / 5) as base.u32 + if (lc + lp) > 4 { + return "#unsupported properties" + } + this.lc = lc.min(no_more_than: 4) + this.lp = lp + this.pb = pb + + c32 = args.src.read_u32le?() + this.dict_size = c32.max(no_less_than: 4096) + + this.decoded_length = args.src.read_u64le?() + if (this.decoded_length >= 0x8000_0000_0000_0000) and + (this.decoded_length <> 0xFFFF_FFFF_FFFF_FFFF) { + return "#unsupported decoded length" + } +} + +// initialize_probs sets all of the probs_foo[i][j] values to 1024. +pri func decoder.initialize_probs!() { + var i : base.u32 + var j : base.u32 + + i = 0 + while i < (12 << 4) { + this.probs_ao00[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 12 { + this.probs_ao20[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 12 { + this.probs_ao40[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < (12 << 4) { + this.probs_ao41[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 12 { + this.probs_ao60[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 12 { + this.probs_ao63[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 16 { + j = 0 + while j < 8, + inv i < 16, + { + this.probs_match_len_low[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile + + i = 0 + while i < 16 { + j = 0 + while j < 8, + inv i < 16, + { + this.probs_match_len_mid[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile + + i = 0 + while i < 256 { + this.probs_match_len_high[0][i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 16 { + j = 0 + while j < 8, + inv i < 16, + { + this.probs_longrep_len_low[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile + + i = 0 + while i < 16 { + j = 0 + while j < 8, + inv i < 16, + { + this.probs_longrep_len_mid[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile + + i = 0 + while i < 256 { + this.probs_longrep_len_high[0][i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 4 { + j = 0 + while j < 64, + inv i < 4, + { + this.probs_slot[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile + + i = 0 + while i < 128 { + this.probs_small_dist[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 16 { + this.probs_large_dist[i] = 1024 + i += 1 + } endwhile + + i = 0 + while i < 16 { + j = 0 + while j < 0x300, + inv i < 16, + { + this.probs_lit[i][j] = 1024 + j += 1 + } endwhile + i += 1 + } endwhile +} + +pri const STATE_TRANSITION_LITERAL : roarray[12] base.u8[..= 11] = [ + 0x0, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x4, 0x5, +] + +pri const STATE_TRANSITION_MATCH : roarray[12] base.u8[..= 11] = [ + 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0xA, 0xA, 0xA, 0xA, 0xA, +] + +pri const STATE_TRANSITION_LONGREP : roarray[12] base.u8[..= 11] = [ + 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0xB, 0xB, 0xB, 0xB, 0xB, +] + +pri const STATE_TRANSITION_SHORTREP : roarray[12] base.u8[..= 11] = [ + 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0xB, 0xB, 0xB, 0xB, 0xB, +] + +pri const CLAMP_NO_MORE_THAN_3 : roarray[8] base.u8[..= 3] = [ + 0, 1, 2, 3, 3, 3, 3, 3, +] diff --git a/test/c/mimiclib/lzma.c b/test/c/mimiclib/lzma.c new file mode 100644 index 00000000..d1bcf275 --- /dev/null +++ b/test/c/mimiclib/lzma.c @@ -0,0 +1,46 @@ +// Copyright 2023 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 + +#include "lzma.h" + +const char* // +mimic_lzma_decode(wuffs_base__io_buffer* dst, + wuffs_base__io_buffer* src, + uint32_t wuffs_initialize_flags, + uint64_t wlimit, + uint64_t rlimit) { + if (wuffs_base__io_buffer__writer_length(dst) > 0x7FFFFFFF) { + return "dst length is too large"; + } else if (wuffs_base__io_buffer__reader_length(src) > 0x7FFFFFFF) { + return "src length is too large"; + } else if ((wlimit < UINT64_MAX) || (rlimit < UINT64_MAX)) { + // It's simpler if we only assume one-shot decompression. + return "unsupported I/O limit"; + } + lzma_stream z = LZMA_STREAM_INIT; + + lzma_ret ret = lzma_alone_decoder(&z, UINT64_MAX); + if (ret != LZMA_OK) { + return "liblzma: lzma_alone_decoder failed"; + } + z.next_out = wuffs_base__io_buffer__writer_pointer(dst); + z.avail_out = wuffs_base__io_buffer__writer_length(dst); + z.next_in = wuffs_base__io_buffer__reader_pointer(src); + z.avail_in = wuffs_base__io_buffer__reader_length(src); + + ret = lzma_code(&z, LZMA_RUN); + dst->meta.wi += (size_t)z.total_out; + src->meta.ri += (size_t)z.total_in; + lzma_end(&z); + if (ret != LZMA_STREAM_END) { + return "liblzma: lzma_code failed"; + } + return NULL; +} diff --git a/test/c/std/lzma.c b/test/c/std/lzma.c new file mode 100644 index 00000000..7c7e1d46 --- /dev/null +++ b/test/c/std/lzma.c @@ -0,0 +1,217 @@ +// Copyright 2023 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 lzma.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 -llzma + +// 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__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 +#include "../mimiclib/lzma.c" +#endif + +// ---------------- Golden Tests + +golden_test g_lzma_enwik5_gt = { + .want_filename = "test/data/enwik5", + .src_filename = "test/data/enwik5.lzma", +}; + +golden_test g_lzma_romeo_gt = { + .want_filename = "test/data/romeo.txt", + .src_filename = "test/data/romeo.txt.lzma", +}; + +// ---------------- LZMA Tests + +const char* // +test_wuffs_lzma_decode_interface() { + CHECK_FOCUS(__func__); + wuffs_lzma__decoder dec; + CHECK_STATUS("initialize", + wuffs_lzma__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + return do_test__wuffs_base__io_transformer( + wuffs_lzma__decoder__upcast_as__wuffs_base__io_transformer(&dec), + "test/data/romeo.txt.litonlylzma", 0, SIZE_MAX, 942, 0x0A); +} + +const char* // +wuffs_lzma_decode(wuffs_base__io_buffer* dst, + wuffs_base__io_buffer* src, + uint32_t wuffs_initialize_flags, + uint64_t wlimit, + uint64_t rlimit) { + wuffs_lzma__decoder dec; + CHECK_STATUS("initialize", + wuffs_lzma__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_lzma__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; + } +} + +const char* // +test_wuffs_lzma_decode_enwik5() { + CHECK_FOCUS(__func__); + return do_test_io_buffers(wuffs_lzma_decode, &g_lzma_enwik5_gt, UINT64_MAX, + UINT64_MAX); +} + +const char* // +test_wuffs_lzma_decode_romeo() { + CHECK_FOCUS(__func__); + return do_test_io_buffers(wuffs_lzma_decode, &g_lzma_romeo_gt, UINT64_MAX, + UINT64_MAX); +} + +// ---------------- Mimic Tests + +#ifdef WUFFS_MIMIC + +const char* // +test_mimic_lzma_decode_enwik5() { + CHECK_FOCUS(__func__); + return do_test_io_buffers(mimic_lzma_decode, &g_lzma_enwik5_gt, UINT64_MAX, + UINT64_MAX); +} + +const char* // +test_mimic_lzma_decode_romeo() { + CHECK_FOCUS(__func__); + return do_test_io_buffers(mimic_lzma_decode, &g_lzma_romeo_gt, UINT64_MAX, + UINT64_MAX); +} + +#endif // WUFFS_MIMIC + +// ---------------- LZMA Benches + +const char* // +bench_wuffs_lzma_decode_100k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + wuffs_lzma_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, + tcounter_dst, &g_lzma_enwik5_gt, UINT64_MAX, UINT64_MAX, 5); +} + +// ---------------- Mimic Benches + +#ifdef WUFFS_MIMIC + +const char* // +bench_mimic_lzma_decode_100k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + mimic_lzma_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, + tcounter_dst, &g_lzma_enwik5_gt, UINT64_MAX, UINT64_MAX, 5); +} + +#endif // WUFFS_MIMIC + +// ---------------- Manifest + +proc g_tests[] = { + + test_wuffs_lzma_decode_enwik5, + test_wuffs_lzma_decode_interface, + test_wuffs_lzma_decode_romeo, + +#ifdef WUFFS_MIMIC + + test_mimic_lzma_decode_enwik5, + test_mimic_lzma_decode_romeo, + +#endif // WUFFS_MIMIC + + NULL, +}; + +proc g_benches[] = { + + bench_wuffs_lzma_decode_100k, + +#ifdef WUFFS_MIMIC + + bench_mimic_lzma_decode_100k, + +#endif // WUFFS_MIMIC + + NULL, +}; + +int // +main(int argc, char** argv) { + g_proc_package_name = "std/lzma"; + return test_main(argc, argv, g_tests, g_benches); +}