code wiki / _hdl_build / nx_gzip.nx
nx_gzip.nx source
↩ module page · 72 lines · 4027 B
1// nx_gzip.nx -- SOVEREIGN gzip/DEFLATE COMPRESSOR: CLI FRONT END ONLY.
2//
3// ★THE DUPLICATE IS GONE (2026-08-28). Until today this file carried its OWN copy of the entire
4// compressor -- gz_deflate, gz_compress, the CRC32, the bit writer, the Huffman tables and every
5// const -- alongside nx_gzip_lib.nx, which nx_host_router imports to compress every response the
6// estate serves. TWO copies of one DEFLATE. The library's own source had recorded the remedy and
7// nobody had taken it: nx_gzip.nx MUST be reduced to an import of nx_gzip_lib.nx.
8//
9// WHAT THE DUPLICATE COST, MEASURED TODAY: adding lazy matching had to be written TWICE, into two
10// files, and proven twice -- because a fix landing in one copy and not its sibling is half a fix,
11// and the half left undone is the one that ships. The next change already named on
12// /compare/performance as rung PF3, dynamic Huffman at a measured further 11.3 percent, would have
13// paid that tax again.
14//
15// ★A SHARED RULER THAT IS COPIED IS NOT SHARED. This file is now the CLI and nothing else: argument
16// parsing, the selftest entry, and the report line. Every byte of compression logic lives ONCE, in
17// the library the serving router already imports -- so the tool that MEASURES the estate's
18// compression and the code that PERFORMS it can no longer drift apart. That equality is not a
19// promise here, it is a property: there is only one gz_deflate now.
20//
21// Usage unchanged: nx_gzip <infile> <outfile> [mode 2 default|0 stored] | selftest
22// Correctness oracle unchanged: STANDARD gunzip byte-identical.
23import "nx_gzip_lib.nx"
24
25func main(argc: i64, argv: *i64) -> i64 {
26 if argc < 2 { gz_w("usage: nx_gzip <infile> <outfile> [mode 0|2] | selftest\n" as *u8); sys_exit(2); return 2 }
27 let v: *u8 = argv[1] as *u8
28 if v[0] == (115 as u8) { if v[1] == (101 as u8) {
29 let msg: *u8 = "the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.\x00" as *u8
30 var mn: i64 = 0
31 while msg[mn] != (0 as u8) { mn = mn + 1 }
32 let ob: *u8 = sys_mmap(mn * 2 + GZ_MAGIC_4096)
33 let olen: i64 = gz_compress(msg, mn, 2, ob)
34 gz_write("/tmp/nxgzip_selftest.gz" as *u8, ob, olen)
35 gz_w("NX-GZIP selftest in=" as *u8)
36 gz_wn(mn)
37 gz_w(" out=" as *u8)
38 gz_wn(olen)
39 gz_w(" -> /tmp/nxgzip_selftest.gz (verify: gunzip -c reproduces input)\n" as *u8)
40 sys_exit(0)
41 return 0
42 } }
43 if argc < 3 { gz_w("usage: nx_gzip <infile> <outfile> [mode]\n" as *u8); sys_exit(2); return 2 }
44 let inpath: *u8 = argv[1] as *u8
45 let outpath: *u8 = argv[2] as *u8
46 var mode: i64 = 2
47 if argc >= 4 { let ms: *u8 = argv[3] as *u8; if ms[0] == (48 as u8) { mode = 0 } }
48 let fsz: i64 = gz_filesize(inpath)
49 if fsz < 0 { gz_w("NX-GZIP FAIL cannot read infile\n" as *u8); sys_exit(3); return 3 }
50 if fsz > GZ_MAXIN { gz_w("NX-GZIP REFUSED: input exceeds GZ_MAXIN -- raise it deliberately, never truncate\n" as *u8); sys_exit(3); return 3 }
51 let sb: *u8 = sys_mmap(fsz + GZ_MAGIC_4096)
52 let n: i64 = gz_read(inpath, sb, fsz)
53 if n == (0 - 2) { gz_w("NX-GZIP REFUSED: input grew past the sized buffer -- refusing a partial read\n" as *u8); sys_exit(3); return 3 }
54 if n < 0 { gz_w("NX-GZIP FAIL cannot read infile\n" as *u8); sys_exit(3); return 3 }
55 if n != fsz { gz_w("NX-GZIP REFUSED: short read -- got fewer bytes than the file reports\n" as *u8); sys_exit(3); return 3 }
56 let ob: *u8 = sys_mmap(n * 2 + GZ_MAGIC_4096)
57 let olen: i64 = gz_compress(sb, n, mode, ob)
58 if gz_write(outpath, ob, olen) != 0 { gz_w("NX-GZIP FAIL cannot write outfile\n" as *u8); sys_exit(3); return 3 }
59 gz_w("NX-GZIP ok in=" as *u8)
60 gz_wn(n)
61 gz_w(" out=" as *u8)
62 gz_wn(olen)
63 gz_w(" mode=" as *u8)
64 gz_wn(mode)
65 var ratio: i64 = 0
66 if n > 0 { ratio = (olen * 100) / n }
67 gz_w(" ratio_pct=" as *u8)
68 gz_wn(ratio)
69 gz_w("\n" as *u8)
70 sys_exit(0)
71 return 0
72}