code wiki / (root) / nx_lzma_lib.nx

nx_lzma_lib.nx

buildroot/runtime/nx_lzma_lib.nx

22498 B509 linesdepth 2pulls 2 transitivereach 14 importersview sourcekind library
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_lzma_lib.nx nx_sevenz_lib.nx nx_sevenz_selected_candidate_t144.

imports: nx_syscalls.nx

imported by: nx_sevenz_lib.nxnx_sevenz_selected_candidate_t144.nx

structs

none

consts

19const LZ_NUM_STATES: i64 = 12
20const LZ_POS_BITS_MAX: i64 = 4
21const LZ_NUM_POS_STATES_MAX: i64 = 16
22const LZ_NUM_LEN_TO_POS_STATES: i64 = 4
23const LZ_END_POS_MODEL_INDEX: i64 = 14
24const LZ_NUM_FULL_DISTANCES: i64 = 128
25const LZ_NUM_ALIGN_BITS: i64 = 4
26const LZ_MATCH_MIN_LEN: i64 = 2
27const LZ_PROB_TOTAL: i64 = 2048
28const LZ_PROB_INIT: i64 = 1024
29const LZ_MOVE_DIV: i64 = 32
30const LZ_TOP: i64 = 16777216
31const LZ_MASK32: i64 = 4294967295
32const LZ_DIC_MIN: i64 = 4096
33const LZ_LIT_TABLE: i64 = 768
34const LZ_MARKER: i64 = 4294967295
35const LZ_LCLP_MAX: i64 = 4
36const LZ_PROPS_MAX_BYTE: i64 = 225
38const LZ_P_ISMATCH: i64 = 0
39const LZ_P_ISREP: i64 = 192
40const LZ_P_ISREPG0: i64 = 204
41const LZ_P_ISREPG1: i64 = 216
42const LZ_P_ISREPG2: i64 = 228
43const LZ_P_ISREP0LONG: i64 = 240
44const LZ_P_POSSLOT: i64 = 432
45const LZ_P_POSDEC: i64 = 688
46const LZ_P_ALIGN: i64 = 803
47const LZ_P_LEN: i64 = 819
48const LZ_P_REPLEN: i64 = 1333
49const LZ_P_LIT: i64 = 1847
50const LZ_LEN_CHOICE: i64 = 0
51const LZ_LEN_CHOICE2: i64 = 1
52const LZ_LEN_LOW: i64 = 2
53const LZ_LEN_MID: i64 = 130
54const LZ_LEN_HIGH: i64 = 258
55const LZ_LEN_WORDS: i64 = 514
57const LZ_D_IN: i64 = 0
58const LZ_D_INPOS: i64 = 1
59const LZ_D_INEND: i64 = 2
60const LZ_D_OUT: i64 = 3
61const LZ_D_OUTPOS: i64 = 4
62const LZ_D_OUTEND: i64 = 5
63const LZ_D_BASE: i64 = 6
64const LZ_D_RANGE: i64 = 7
65const LZ_D_CODE: i64 = 8
66const LZ_D_LC: i64 = 9
67const LZ_D_LP: i64 = 10
68const LZ_D_PB: i64 = 11
69const LZ_D_DICT: i64 = 12
70const LZ_D_STATE: i64 = 13
71const LZ_D_REP0: i64 = 14
72const LZ_D_REP1: i64 = 15
73const LZ_D_REP2: i64 = 16
74const LZ_D_REP3: i64 = 17
75const LZ_D_PROBS: i64 = 18
76const LZ_D_NPROBS: i64 = 19
77const LZ_D_CORRUPT: i64 = 20
78const LZ_D_INOVER: i64 = 21
79const LZ_D_CHUNKS: i64 = 22
80const LZ_D_N: i64 = 32
82const LZ_OK: i64 = 0
83const LZ_ERR_PROPS: i64 = 0 - 1
84const LZ_ERR_FIRST_BYTE: i64 = 0 - 2
85const LZ_ERR_INPUT_SHORT: i64 = 0 - 3
86const LZ_ERR_DISTANCE: i64 = 0 - 4
87const LZ_ERR_OUTPUT_OVER: i64 = 0 - 5
88const LZ_ERR_MARKER_CODE: i64 = 0 - 6
89const LZ_ERR_CONTROL: i64 = 0 - 7
90const LZ_ERR_UNPACK_MISMATCH: i64 = 0 - 8
91const LZ_ERR_WINDOW_EMPTY: i64 = 0 - 9
454const LZ_BCJ_OP_MASK: i64 = 254
455const LZ_BCJ_OP: i64 = 232 // E8 (CALL) and E9 (JMP) both match under the 0xFE mask
456const LZ_BCJ_TAIL: i64 = 4
457const LZ_BCJ_HEAD: i64 = 5

functions

93func lz_err_name(e: i64) -> *u8
105func lz_pow2(k: i64) -> i64 { var v: i64 = 1; var i: i64 = 0; while i < k { v = v * 2; i = i + 1 } return v }
106func lz_probs_count(lc: i64, lp: i64) -> i64 { return LZ_P_LIT + LZ_LIT_TABLE * lz_pow2(lc + lp) }
called by 1: lz_init_probs calls 1: lz_pow2
108func lz_in(d: *i64) -> i64
116func lz_rc_init(d: *i64) -> i64
called by 1: lz_decode calls 1: lz_in
127func lz_rc_norm(d: *i64) -> i64
called by 2: lz_bitlz_direct calls 1: lz_in
134func lz_bit(d: *i64, pi: i64) -> i64
153func lz_direct(d: *i64, nbits: i64) -> i64
called by 1: lz_dist calls 1: lz_rc_norm
168func lz_tree(d: *i64, base: i64, nbits: i64) -> i64
called by 2: lz_lenlz_dist calls 2: lz_bitlz_pow2
174func lz_tree_rev(d: *i64, base: i64, nbits: i64) -> i64
called by 1: lz_dist calls 2: lz_bitlz_pow2
186func lz_len(d: *i64, base: i64, pos_state: i64) -> i64
called by 1: lz_decode calls 2: lz_bitlz_tree
191func lz_dist(d: *i64, len: i64) -> i64
206func 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
207func lz_state_match(s: i64) -> i64 { if s < 7 { return 7 } return 10 }
called by 1: lz_decode
208func lz_state_rep(s: i64) -> i64 { if s < 7 { return 8 } return 11 }
called by 1: lz_decode
209func lz_state_shortrep(s: i64) -> i64 { if s < 7 { return 9 } return 11 }
called by 1: lz_decode
211func lz_init_probs(d: *i64) -> i64
called by 1: lz_reset_state calls 1: lz_probs_count
218func lz_reset_state(d: *i64) -> i64
called by 2: lz_newlz_lzma2_decode calls 1: lz_init_probs
225func lz_set_props_byte(d: *i64, pbyte: i64, lclp_max: i64) -> i64
236func lz_new(out: *u8, outcap: i64, lc: i64, lp: i64, pb: i64, dict: i64) -> *i64
257func lz_decode(d: *i64, src: *u8, srclen: i64, unpack: i64) -> i64
372func lz_lzma_props_dict(props: *u8) -> i64
called by 1: lz_lzma_decode
375func lz_lzma_decode(src: *u8, srclen: i64, props: *u8, out: *u8, unpack: i64) -> i64
387func lz_lzma2_dict(p: i64) -> i64
called by 1: lz_lzma2_decode calls 1: lz_pow2
392func lz_lzma2_decode(src: *u8, srclen: i64, dict_prop: i64, out: *u8, unpack: i64) -> i64
458func lz_test86(b: i64) -> i64 { if ((b + 1) & LZ_BCJ_OP_MASK) == 0 { return 1 } return 0 }
called by 1: lz_bcj_x86
459func lz_bcj_x86(data: *u8, size0: i64, ip0: i64, statebox: *i64, encoding: i64) -> i64