nx_lzma_lib.nx
buildroot/runtime/nx_lzma_lib.nx
about
nx_lzma_lib.nx -- THE LZMA AND LZMA2 DECODER (/compare/modding MD29, 2026-09-06), written from the mirrored specification
(knowledge/fetched/cmp_modding_lzma-specification.txt, Igor Pavlov 2015-06-14, pin 6a4d441c) and the LZMA2 chunk rules of
the public-domain SDK decoder (cmp_modding_Lzma2Dec.c, pin 41303267), both READ before a line of this was written.
It is the decode-to-one-buffer variant the specification names: the OUTPUT BUFFER IS THE SLIDING WINDOW, so a match at
distance d reads out[pos - d] and a dictionary reset simply moves the base the distance check measures from. The
probability counters are i64 slots (11-bit values), the range coder keeps Range and Code as 32-bit values inside i64
(masked after every shift, exactly as the specification's note on wider integers requires), and every corruption the
specification lets the reference decoder IGNORE is likewise ignored here so the output matches the reference byte for byte;
the conditions the specification calls errors (distance past the window or dictionary, a literal past the unpack size,
a match longer than what remains, a non-zero first byte, a marker with a non-zero code) are REFUSALS, named by code.
LZMA2 (the 7z and xz container coder): a control byte per chunk -- 0 ends the stream; 1 and 2 copy an uncompressed chunk
(1 resets the dictionary); 0x80 and above carry an LZMA chunk whose low five bits are the unpack size's high bits, then
two big-endian bytes each of unpack size minus one and pack size minus one, then a properties byte when bit 6 is set;
bits 5 and 6 select the reset: 0 nothing, 1 state, 2 state plus new properties, 3 state plus properties plus dictionary.
Every LZMA chunk re-initialises the range coder from its own first five bytes (the SDK sets needFlush on every chunk).
license_tier: ORIGINAL No hw writes (Rule 26).
dependencies 1 imports · 2 importers
imports: nx_syscalls.nx
imported by: nx_sevenz_lib.nxnx_sevenz_selected_candidate_t144.nx
structs
| none |
consts
| 19 | const LZ_NUM_STATES: i64 = 12 |
| 20 | const LZ_POS_BITS_MAX: i64 = 4 |
| 21 | const LZ_NUM_POS_STATES_MAX: i64 = 16 |
| 22 | const LZ_NUM_LEN_TO_POS_STATES: i64 = 4 |
| 23 | const LZ_END_POS_MODEL_INDEX: i64 = 14 |
| 24 | const LZ_NUM_FULL_DISTANCES: i64 = 128 |
| 25 | const LZ_NUM_ALIGN_BITS: i64 = 4 |
| 26 | const LZ_MATCH_MIN_LEN: i64 = 2 |
| 27 | const LZ_PROB_TOTAL: i64 = 2048 |
| 28 | const LZ_PROB_INIT: i64 = 1024 |
| 29 | const LZ_MOVE_DIV: i64 = 32 |
| 30 | const LZ_TOP: i64 = 16777216 |
| 31 | const LZ_MASK32: i64 = 4294967295 |
| 32 | const LZ_DIC_MIN: i64 = 4096 |
| 33 | const LZ_LIT_TABLE: i64 = 768 |
| 34 | const LZ_MARKER: i64 = 4294967295 |
| 35 | const LZ_LCLP_MAX: i64 = 4 |
| 36 | const LZ_PROPS_MAX_BYTE: i64 = 225 |
| 38 | const LZ_P_ISMATCH: i64 = 0 |
| 39 | const LZ_P_ISREP: i64 = 192 |
| 40 | const LZ_P_ISREPG0: i64 = 204 |
| 41 | const LZ_P_ISREPG1: i64 = 216 |
| 42 | const LZ_P_ISREPG2: i64 = 228 |
| 43 | const LZ_P_ISREP0LONG: i64 = 240 |
| 44 | const LZ_P_POSSLOT: i64 = 432 |
| 45 | const LZ_P_POSDEC: i64 = 688 |
| 46 | const LZ_P_ALIGN: i64 = 803 |
| 47 | const LZ_P_LEN: i64 = 819 |
| 48 | const LZ_P_REPLEN: i64 = 1333 |
| 49 | const LZ_P_LIT: i64 = 1847 |
| 50 | const LZ_LEN_CHOICE: i64 = 0 |
| 51 | const LZ_LEN_CHOICE2: i64 = 1 |
| 52 | const LZ_LEN_LOW: i64 = 2 |
| 53 | const LZ_LEN_MID: i64 = 130 |
| 54 | const LZ_LEN_HIGH: i64 = 258 |
| 55 | const LZ_LEN_WORDS: i64 = 514 |
| 57 | const LZ_D_IN: i64 = 0 |
| 58 | const LZ_D_INPOS: i64 = 1 |
| 59 | const LZ_D_INEND: i64 = 2 |
| 60 | const LZ_D_OUT: i64 = 3 |
| 61 | const LZ_D_OUTPOS: i64 = 4 |
| 62 | const LZ_D_OUTEND: i64 = 5 |
| 63 | const LZ_D_BASE: i64 = 6 |
| 64 | const LZ_D_RANGE: i64 = 7 |
| 65 | const LZ_D_CODE: i64 = 8 |
| 66 | const LZ_D_LC: i64 = 9 |
| 67 | const LZ_D_LP: i64 = 10 |
| 68 | const LZ_D_PB: i64 = 11 |
| 69 | const LZ_D_DICT: i64 = 12 |
| 70 | const LZ_D_STATE: i64 = 13 |
| 71 | const LZ_D_REP0: i64 = 14 |
| 72 | const LZ_D_REP1: i64 = 15 |
| 73 | const LZ_D_REP2: i64 = 16 |
| 74 | const LZ_D_REP3: i64 = 17 |
| 75 | const LZ_D_PROBS: i64 = 18 |
| 76 | const LZ_D_NPROBS: i64 = 19 |
| 77 | const LZ_D_CORRUPT: i64 = 20 |
| 78 | const LZ_D_INOVER: i64 = 21 |
| 79 | const LZ_D_CHUNKS: i64 = 22 |
| 80 | const LZ_D_N: i64 = 32 |
| 82 | const LZ_OK: i64 = 0 |
| 83 | const LZ_ERR_PROPS: i64 = 0 - 1 |
| 84 | const LZ_ERR_FIRST_BYTE: i64 = 0 - 2 |
| 85 | const LZ_ERR_INPUT_SHORT: i64 = 0 - 3 |
| 86 | const LZ_ERR_DISTANCE: i64 = 0 - 4 |
| 87 | const LZ_ERR_OUTPUT_OVER: i64 = 0 - 5 |
| 88 | const LZ_ERR_MARKER_CODE: i64 = 0 - 6 |
| 89 | const LZ_ERR_CONTROL: i64 = 0 - 7 |
| 90 | const LZ_ERR_UNPACK_MISMATCH: i64 = 0 - 8 |
| 91 | const LZ_ERR_WINDOW_EMPTY: i64 = 0 - 9 |
| 454 | const LZ_BCJ_OP_MASK: i64 = 254 |
| 455 | const LZ_BCJ_OP: i64 = 232 // E8 (CALL) and E9 (JMP) both match under the 0xFE mask |
| 456 | const LZ_BCJ_TAIL: i64 = 4 |
| 457 | const LZ_BCJ_HEAD: i64 = 5 |
functions
| 93 | func lz_err_name(e: i64) -> *u8 |
| 105 | func lz_pow2(k: i64) -> i64 { var v: i64 = 1; var i: i64 = 0; while i < k { v = v * 2; i = i + 1 } return v } |
| 106 | func lz_probs_count(lc: i64, lp: i64) -> i64 { return LZ_P_LIT + LZ_LIT_TABLE * lz_pow2(lc + lp) } |
| 108 | func lz_in(d: *i64) -> i64 |
| 116 | func lz_rc_init(d: *i64) -> i64 |
| 127 | func lz_rc_norm(d: *i64) -> i64 |
| 134 | func lz_bit(d: *i64, pi: i64) -> i64 |
| 153 | func lz_direct(d: *i64, nbits: i64) -> i64 |
| 168 | func lz_tree(d: *i64, base: i64, nbits: i64) -> i64 |
| 174 | func lz_tree_rev(d: *i64, base: i64, nbits: i64) -> i64 |
| 186 | func lz_len(d: *i64, base: i64, pos_state: i64) -> i64 |
| 191 | func lz_dist(d: *i64, len: i64) -> i64 |
| 206 | func lz_state_lit(s: i64) -> i64 { if s < 4 { return 0 } if s < 10 { return s - 3 } return s - 6 } called by 1: lz_decode |
| 207 | func lz_state_match(s: i64) -> i64 { if s < 7 { return 7 } return 10 } called by 1: lz_decode |
| 208 | func lz_state_rep(s: i64) -> i64 { if s < 7 { return 8 } return 11 } called by 1: lz_decode |
| 209 | func lz_state_shortrep(s: i64) -> i64 { if s < 7 { return 9 } return 11 } called by 1: lz_decode |
| 211 | func lz_init_probs(d: *i64) -> i64 |
| 218 | func lz_reset_state(d: *i64) -> i64 |
| 225 | func lz_set_props_byte(d: *i64, pbyte: i64, lclp_max: i64) -> i64 |
| 236 | func lz_new(out: *u8, outcap: i64, lc: i64, lp: i64, pb: i64, dict: i64) -> *i64 |
| 257 | func lz_decode(d: *i64, src: *u8, srclen: i64, unpack: i64) -> i64 called by 2: lz_lzma_decodelz_lzma2_decode calls 9: lz_rc_initlz_pow2lz_bitlz_state_litlz_lenlz_state_match+3 |
| 372 | func lz_lzma_props_dict(props: *u8) -> i64 called by 1: lz_lzma_decode |
| 375 | func lz_lzma_decode(src: *u8, srclen: i64, props: *u8, out: *u8, unpack: i64) -> i64 called by 2: sz_decode_foldersz_decode_folder calls 5: sys_mmaplz_set_props_bytelz_newlz_lzma_props_dictlz_decode |
| 387 | func lz_lzma2_dict(p: i64) -> i64 |
| 392 | func lz_lzma2_decode(src: *u8, srclen: i64, dict_prop: i64, out: *u8, unpack: i64) -> i64 called by 2: sz_decode_foldersz_decode_folder calls 5: lz_lzma2_dictlz_newlz_set_props_bytelz_reset_statelz_decode |
| 458 | func lz_test86(b: i64) -> i64 { if ((b + 1) & LZ_BCJ_OP_MASK) == 0 { return 1 } return 0 } called by 1: lz_bcj_x86 |
| 459 | func lz_bcj_x86(data: *u8, size0: i64, ip0: i64, statebox: *i64, encoding: i64) -> i64 |