code wiki / (root) / nx_zip_gate.nx

nx_zip_gate.nx source

↩ module page · 172 lines · 7195 B

1// nx_zip_gate.nx -- proves the ZIP writer/reader pair by ROUND-TRIP. 2// 3// T1 pins the CRC ALGORITHM to the classic check value 0xCBF43926 for 4// "123456789". This matters more than usual here: nx_ogg.nx also implements 5// something called "CRC-32", using the SAME polynomial UNreflected with zero 6// init. The two are not interchangeable, and swapping them produces archives 7// that round-trip perfectly against themselves while every unzip on earth 8// reports a checksum error. Only an absolute known-value assertion separates 9// them. 10// 11// T5 pins the central-directory offset. Readers trust the CD, not the local 12// headers -- an archive with correct local headers and wrong CD offsets opens 13// as empty or corrupt, and nothing in the local headers hints at it. The test 14// resolves each entry's data THROUGH the CD offset and verifies the CRC of 15// what it finds there. 16// 17// NON-VACUITY: T7 covers refusals -- a corrupted data byte must fail CRC, a 18// short buffer must refuse, and a missing EOCD must not be invented. 19// 20// license_tier: ORIGINAL 21import "nx_syscalls.nx" 22import "nx_zip.nx" 23 24func g_puts(s: *u8) -> i64 { 25 var i: i64 = 0 26 while s[i] != (0 as u8) { i = i + 1 } 27 sys_write(1, s, i) 28 return i 29} 30 31func g_putn(v: i64) -> i64 { 32 let buf: *u8 = sys_mmap(32) 33 var x: i64 = v 34 if x == 0 { buf[0] = 0x30 as u8; sys_write(1, buf, 1); return 1 } 35 let tmp: *u8 = sys_mmap(32) 36 var d: i64 = 0 37 while x > 0 { tmp[d] = ((x % 10) + 0x30) as u8; x = x / 10; d = d + 1 } 38 var i: i64 = 0 39 while i < d { buf[i] = tmp[d - 1 - i]; i = i + 1 } 40 sys_write(1, buf, d) 41 return d 42} 43 44func main() -> i64 { 45 var fails: i64 = 0 46 var mark: i64 = 0 47 48 // ---- T1: the REFLECTED CRC-32, pinned to the classic check value ---- 49 let cb: *u8 = sys_mmap(64) 50 var i: i64 = 0 51 while i < 9 { cb[i] = (0x31 + i) as u8; i = i + 1 } // "123456789" 52 if nxzip_crc32(cb, 9) != 0xcbf43926 { fails = fails + 1 } 53 // and the single-byte 'a' 54 cb[0] = 0x61 as u8 55 if nxzip_crc32(cb, 1) != 0xe8b7be43 { fails = fails + 1 } 56 // the empty string checksums to zero 57 if nxzip_crc32(cb, 0) != 0 { fails = fails + 1 } 58 if fails > 0 { if mark == 0 { mark = 1 } } 59 60 // ---- T2: LE field helpers ---- 61 let lb: *u8 = sys_mmap(64) 62 nxzip_w16(lb, 0, 65535) 63 if nxzip_r16(lb, 0) != 65535 { fails = fails + 1 } 64 nxzip_w32(lb, 4, 0x04034b50) 65 if nxzip_r32(lb, 4) != 0x04034b50 { fails = fails + 1 } 66 if fails > 0 { if mark == 0 { mark = 2 } } 67 68 // ---- T3/T4: build a real two-entry archive ---- 69 let ar: *u8 = sys_mmap(16384) 70 let cd: *u8 = sys_mmap(4096) 71 let st: *i64 = sys_mmap(64) as *i64 72 st[NX_ZIP_ST_OFF] = 0; st[NX_ZIP_ST_CDOFF] = 0; st[NX_ZIP_ST_COUNT] = 0 73 74 let n1: *u8 = sys_mmap(64) 75 n1[0] = 0x61 as u8; n1[1] = 0x2e as u8; n1[2] = 0x74 as u8 76 n1[3] = 0x78 as u8; n1[4] = 0x74 as u8; n1[5] = 0 as u8 77 let d1: *u8 = sys_mmap(256) 78 i = 0 79 while i < 11 { d1[i] = (0x48 + i) as u8; i = i + 1 } 80 81 let n2: *u8 = sys_mmap(64) 82 n2[0] = 0x62 as u8; n2[1] = 0x2f as u8; n2[2] = 0x63 as u8 83 n2[3] = 0x2e as u8; n2[4] = 0x64 as u8; n2[5] = 0 as u8 84 let d2: *u8 = sys_mmap(2048) 85 i = 0 86 while i < 900 { d2[i] = ((i * 5 + 7) & 255) as u8; i = i + 1 } 87 88 if nxzip_add_stored(ar, 16384, cd, 4096, st, n1, d1, 11) != 1 { fails = fails + 1 } 89 if nxzip_add_stored(ar, 16384, cd, 4096, st, n2, d2, 900) != 1 { fails = fails + 1 } 90 if st[NX_ZIP_ST_COUNT] != 2 { fails = fails + 1 } 91 let total: i64 = nxzip_finalize(ar, 16384, cd, st) 92 if total <= 0 { fails = fails + 1 } 93 if fails > 0 { if mark == 0 { mark = 4 } } 94 95 // ---- T5: read it back THROUGH the central directory ---- 96 if nxzip_count(ar, total) != 2 { fails = fails + 1 } 97 var cdp: i64 = nxzip_cd_start(ar, total) 98 if cdp < 0 { fails = fails + 1 } 99 let fld: *i64 = sys_mmap(128) as *i64 100 var seen: i64 = 0 101 var bad: i64 = 0 102 while seen < 2 { 103 if nxzip_read_cd(ar, total, cdp, fld) != 1 { fails = fails + 1; seen = 2 } else { 104 let doff: i64 = nxzip_entry_data(ar, total, fld) 105 if doff < 0 { fails = fails + 1 } else { 106 if seen == 0 { 107 if fld[NX_ZIP_FLD_USIZE] != 11 { fails = fails + 1 } 108 if fld[NX_ZIP_FLD_NAMELEN] != 5 { fails = fails + 1 } 109 var k: i64 = 0 110 while k < 11 { 111 if (ar[doff+k] as i64 & 255) != (d1[k] as i64 & 255) { bad = bad + 1 } 112 k = k + 1 113 } 114 } 115 if seen == 1 { 116 if fld[NX_ZIP_FLD_USIZE] != 900 { fails = fails + 1 } 117 if fld[NX_ZIP_FLD_METHOD] != NX_ZIP_METHOD_STORE { fails = fails + 1 } 118 var k2: i64 = 0 119 while k2 < 900 { 120 if (ar[doff+k2] as i64 & 255) != (d2[k2] as i64 & 255) { bad = bad + 1 } 121 k2 = k2 + 1 122 } 123 } 124 } 125 cdp = fld[NX_ZIP_FLD_NEXTCD] 126 seen = seen + 1 127 } 128 } 129 if bad != 0 { fails = fails + 1 } 130 if fails > 0 { if mark == 0 { mark = 5 } } 131 132 // ---- T6: the entry name survives into the central directory ---- 133 cdp = nxzip_cd_start(ar, total) 134 if nxzip_read_cd(ar, total, cdp, fld) != 1 { fails = fails + 1 } else { 135 let noff: i64 = fld[NX_ZIP_FLD_NAMEOFF] 136 if (ar[noff] as i64 & 255) != 0x61 { fails = fails + 1 } 137 if (ar[noff+4] as i64 & 255) != 0x74 { fails = fails + 1 } 138 } 139 if fails > 0 { if mark == 0 { mark = 6 } } 140 141 // ---- T7 NEG: a corrupted data byte must fail the CRC ---- 142 cdp = nxzip_cd_start(ar, total) 143 nxzip_read_cd(ar, total, cdp, fld) 144 let doff2: i64 = nxzip_entry_data(ar, total, fld) 145 ar[doff2 + 3] = ((ar[doff2 + 3] as i64) ^ 0xff) as u8 146 if nxzip_entry_data(ar, total, fld) != (0 - 1) { fails = fails + 1 } 147 ar[doff2 + 3] = ((ar[doff2 + 3] as i64) ^ 0xff) as u8 148 if nxzip_entry_data(ar, total, fld) != doff2 { fails = fails + 1 } 149 // no EOCD in a buffer of junk 150 let junk: *u8 = sys_mmap(128) 151 i = 0 152 while i < 64 { junk[i] = 0x41 as u8; i = i + 1 } 153 if nxzip_find_eocd(junk, 64) != (0 - 1) { fails = fails + 1 } 154 // an output buffer too small refuses the entry 155 let st2: *i64 = sys_mmap(64) as *i64 156 st2[NX_ZIP_ST_OFF] = 0; st2[NX_ZIP_ST_CDOFF] = 0; st2[NX_ZIP_ST_COUNT] = 0 157 if nxzip_add_stored(ar, 20, cd, 4096, st2, n1, d1, 11) != 0 { fails = fails + 1 } 158 if fails > 0 { if mark == 0 { mark = 7 } } 159 160 if fails == 0 { 161 g_puts("GATE nx_zip verdict=GREEN pass=7/7 (REFLECTED CRC-32 pinned 0xCBF43926 + 0xE8B7BE43 + empty=0, distinct from the Ogg unreflected variant; LE16/LE32; 2-entry archive written; read back THROUGH central-directory offsets with 911 content bytes EXACT; names preserved; NEG corrupted byte fails CRC then restored-and-accepted, no-EOCD refused, short buffer refused)\n" as *u8) 162 sys_exit(0) 163 return 0 164 } 165 g_puts("GATE nx_zip verdict=RED fails=" as *u8) 166 g_putn(fails) 167 g_puts(" first_stage=" as *u8) 168 g_putn(mark) 169 g_puts("\n" as *u8) 170 sys_exit(1) 171 return 1 172}