code wiki / _hdl_build / nx_nv1_gate.nx

nx_nv1_gate.nx source

↩ module page · 122 lines · 4727 B

1// nx_nv1_gate.nx -- ENGINEER gate for the NishiLossless v1 container. 2// Evidence-driven, re-runnable, no network: writes a synthetic container 3// (3 PCM chunks of a known ramp + 5 JPEG-stand-in frames), validates it, 4// asserts every count/byte round-trips, then proves the validator REJECTS 5// each malformation class (truncation, bad magic, bad version, bad kind, 6// nonzero-len terminator, trailing bytes, overrun chunk). 7// license_tier: ORIGINAL 8 9import "nx_nv1.nx" 10import "nx_g_check_lib.nx" 11import "nx_g_puts_lib.nx" 12 13func main() -> i64 { 14 g_puts("nx_nv1 gate (NishiLossless v1 container)\n" as *u8) 15 var pass: i64 = 0 16 var total: i64 = 0 17 18 // ---- synthetic payloads ---- 19 let pcm: *u8 = sys_mmap(8192) // 4096 samples, ramp pattern 20 var i: i64 = 0 21 while i < 8192 { pcm[i] = ((i * 7 + 3) & 255) as u8; i = i + 1 } 22 let jpg: *u8 = sys_mmap(1000) // frame stand-in pattern 23 i = 0 24 while i < 1000 { jpg[i] = ((i * 13 + 5) & 255) as u8; i = i + 1 } 25 26 // ---- write: A V A V V A V V end ---- 27 let buf: *u8 = sys_mmap(65536) 28 var w: i64 = nv1_write_header(buf, 48000) 29 w = nv1_write_chunk(buf, w, 65, 0, pcm, 8192) 30 w = nv1_write_chunk(buf, w, 86, 10, jpg, 1000) 31 w = nv1_write_chunk(buf, w, 65, 85, pcm, 8192) 32 w = nv1_write_chunk(buf, w, 86, 110, jpg, 1000) 33 w = nv1_write_chunk(buf, w, 86, 210, jpg, 1000) 34 w = nv1_write_chunk(buf, w, 65, 170, pcm, 8192) 35 w = nv1_write_chunk(buf, w, 86, 310, jpg, 1000) 36 w = nv1_write_chunk(buf, w, 86, 410, jpg, 1000) 37 w = nv1_write_end(buf, w, 512) 38 let n: i64 = w 39 40 let info: *i64 = sys_mmap(64) as *i64 41 var rc: i64 = nv1_validate(buf, n, info) 42 pass = pass + g_check("valid container -> rc 0" as *u8, rc == 0) 43 total = total + 1 44 pass = pass + g_check("rate round-trips (48000)" as *u8, info[0] == 48000) 45 total = total + 1 46 pass = pass + g_check("3 audio chunks counted" as *u8, info[1] == 3) 47 total = total + 1 48 pass = pass + g_check("12288 samples (3 x 4096)" as *u8, info[2] == 12288) 49 total = total + 1 50 pass = pass + g_check("5 video frames counted" as *u8, info[3] == 5) 51 total = total + 1 52 pass = pass + g_check("duration = terminator t_ms (512)" as *u8, info[4] == 512) 53 total = total + 1 54 55 // ---- first audio payload is byte-identical in place ---- 56 var same: i64 = 1 57 i = 0 58 while i < 8192 { 59 if buf[24 + 9 + i] != pcm[i] { same = 0; i = 8192 } else { i = i + 1 } 60 } 61 pass = pass + g_check("PCM bytes byte-identical in container" as *u8, same) 62 total = total + 1 63 64 // ---- audio-only container is valid ---- 65 var w2: i64 = nv1_write_header(buf, 44100) 66 w2 = nv1_write_chunk(buf, w2, 65, 0, pcm, 8192) 67 w2 = nv1_write_end(buf, w2, 93) 68 rc = nv1_validate(buf, w2, info) 69 var aok: i64 = 0 70 if rc == 0 { if info[1] == 1 { if info[3] == 0 { aok = 1 } } } 71 pass = pass + g_check("audio-only container valid" as *u8, aok) 72 total = total + 1 73 74 // ---- rejection classes (rebuild the full container each time) ---- 75 w = nv1_write_header(buf, 48000) 76 w = nv1_write_chunk(buf, w, 65, 0, pcm, 8192) 77 w = nv1_write_chunk(buf, w, 86, 10, jpg, 1000) 78 w = nv1_write_end(buf, w, 512) 79 80 rc = nv1_validate(buf, w - 10, info) 81 pass = pass + g_check("truncated tail -> rejected" as *u8, rc < 0) 82 total = total + 1 83 84 buf[0] = 88 // 'X' 85 rc = nv1_validate(buf, w, info) 86 pass = pass + g_check("bad magic -> -2" as *u8, rc == 0 - 2) 87 total = total + 1 88 buf[0] = 78 89 90 buf[4] = 9 91 rc = nv1_validate(buf, w, info) 92 pass = pass + g_check("bad version -> -3" as *u8, rc == 0 - 3) 93 total = total + 1 94 buf[4] = 1 95 96 buf[24] = 90 // 'Z' kind 97 rc = nv1_validate(buf, w, info) 98 pass = pass + g_check("unknown chunk kind -> -4" as *u8, rc == 0 - 4) 99 total = total + 1 100 buf[24] = 65 101 102 buf[25] = 255; buf[26] = 255 // A len huge -> overrun 103 rc = nv1_validate(buf, w, info) 104 pass = pass + g_check("overrun chunk len -> rejected" as *u8, rc < 0) 105 total = total + 1 106 nv1_wr_u32(buf, 25, 8192) 107 108 rc = nv1_validate(buf, w + 3, info) // trailing junk 109 pass = pass + g_check("trailing bytes -> rejected" as *u8, rc < 0) 110 total = total + 1 111 112 g_puts("---- nv1 gate: passed " as *u8) 113 let d: *u8 = sys_mmap(8) 114 d[0] = (48 + pass / 10) as u8; d[1] = (48 + pass % 10) as u8 115 sys_write(1, d, 2) 116 g_puts(" / " as *u8) 117 d[0] = (48 + total / 10) as u8; d[1] = (48 + total % 10) as u8 118 sys_write(1, d, 2) 119 g_puts(" ----\n" as *u8) 120 if pass == total { return 0 } 121 return 1 122}