code wiki / _hdl_build / nx_huffcdic.nx

nx_huffcdic.nx

buildroot/runtime/_hdl_build/nx_huffcdic.nx

6297 B128 linesdepth 2pulls 2 transitivereach 3 importersview sourcekind library
docsdependenciesstructsconstsfunctions

about

nx_huffcdic.nx -- SOVEREIGN HUFF/CDIC decompressor for MOBI/AZW (compression type 17480), the Nishi capability equivalent of Calibre's huffcdic reference. CLEAN-ROOM: written from the documented algorithm (HUFF/CDIC record layout + the 32-bit Huffman-window decode), NOT copied -- pure NishiLang (nx_cc -> nxasm_x86, no python/gcc/sh). Newer Kindle books use HUFF/CDIC instead of PalmDOC; nx_mobi_book currently handles 1/2 (none/PalmDOC) only. HUFF record: "HUFF\x00\x00\x00\x18" | off1(u32) | off2(u32) | dict1[256]u32 (at off1) | dict2[64]u32 (at off2) dict1 entry v: codelen = v&0x1f ; term = v&0x80 ; maxcode = ((v>>8)+1 << (32-codelen)) - 1 dict2: 32 (mincode,maxcode) pairs -> mincode[cl] = raw<<(32-cl) ; maxcode[cl] = ((raw+1)<<(32-cl))-1, cl=1..32 CDIC record: "CDIC\x00\x00\x00\x10" | phrases(u32) | bits(u32) | u16 offset table -> phrases phrase j: off = be16(16+j*2) ; blen = be16(16+off) ; bytes = [18+off .. 18+off+(blen&0x7fff)) ; flag = blen&0x8000 unpack: 32-bit window; codelen/term/maxcode = dict1[code>>24]; if !term walk mincode[] up; r=(maxcode-code)>>(32-codelen) is the dictionary index; a non-terminal phrase is itself a compressed slice -> recurse, then cache as terminal. NXASM-SAFE: be64 builds the u64 via a <<8 loop (immediate shifts >=32 are mis-encoded); every <<(32-cl) is a VARIABLE (register) shift, which x86 SHR/SHL handle correctly. license_tier: ORIGINAL

dependencies 1 imports · 3 importers

nx_syscalls.nx nx_huffcdic.nx nx_huffcdic_gate.nx nx_kf8_book.nx nx_mobi_book.nx

imports: nx_syscalls.nx

imported by: nx_huffcdic_gate.nxnx_kf8_book.nxnx_mobi_book.nx

structs

none

consts

16const K_MAGIC_65536: i64 = 65536

functions

18func hc_be16(b: *u8, o: i64) -> i64 { return ((b[o] as i64)<<8)|(b[o+1] as i64) }
called by 1: cdic_load
19func hc_be32(b: *u8, o: i64) -> i64 { return ((b[o] as i64)<<24)|((b[o+1] as i64)<<16)|((b[o+2] as i64)<<8)|(b[o+3] as i64) }
called by 2: huff_loadcdic_load
20func hc_be64(b: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=(v<<8)|(b[o+i] as i64); i=i+1 } return v }
called by 1: hc_unpack
24func huff_load(huff: *u8, hlen: i64, d1cl: *i64, d1tm: *i64, d1mx: *i64, mincode: *i64, maxcode: *i64) -> i64
called by 3: maindo_kf8do_mobi calls 1: hc_be32
58func cdic_load(cdic: *u8, clen: i64, dptr: *i64, dlen: *i64, dflag: *i64, dcount: *i64) -> i64
called by 3: maindo_kf8do_mobi calls 2: hc_be32hc_be16
87func hc_unpack(data: *u8, datalen: i64, ob: *u8, obcap: i64, d1cl: *i64, d1tm: *i64, d1mx: *i64, mincode: *i64, maxcode: *i64, dptr: *i64, dlen: *i64, dflag: *i64, dcount: i64) -> i64