code wiki / _hdl_build / nx_room_stream.nx
nx_room_stream.nx source
↩ module page · 80 lines · 3181 B
1// nx_room_stream.nx -- the SOVEREIGN no-WebSocket media down-channel wire.
2// Operator law: "we dont want js or websocket ... nishi from the hardware rung up."
3// HTTP/1.1 chunked transfer-encoding (RFC 7230 4.1) is the mechanism a browser fetch()
4// ReadableStream consumes PROGRESSIVELY -- live frames, not buffered to EOF -- with zero
5// WebSocket and zero WebRTC. The Nishi TLS daemon emits this; a Nishi-emitted loader reads
6// it and hands each chunk's bytes to the wasm client core (vc_wire_parse). Pure buffer ops:
7// imports nothing, no syscalls (like nx_video_client_wasm) -- usable in any caller memory.
8// Proven by nx_room_stream_gate (round-trip + vc_wire frames + malformed neg-control).
9// license_tier: ORIGINAL
10
11// lowercase hex digit for value 0..15
12func rs_hexdig(v: i64) -> u8 { if v < 10 { return (48 + v) as u8 } return (87 + v) as u8 }
13// hex value of an ascii digit (0-9 a-f A-F), or -1 if not a hex digit
14func rs_hexval(c: u8) -> i64 {
15 let x: i64 = c as i64
16 if x >= 48 { if x <= 57 { return x - 48 } }
17 if x >= 97 { if x <= 102 { return x - 87 } }
18 if x >= 65 { if x <= 70 { return x - 55 } }
19 return 0 - 1
20}
21
22// encode one HTTP chunk into out at off: <hexlen> CRLF <payload> CRLF ; returns new offset.
23// plen==0 emits the terminator chunk "0 CRLF CRLF".
24func rs_chunk_encode(out: *u8, off: i64, payload: *u8, plen: i64) -> i64 {
25 var o: i64 = off
26 if plen == 0 {
27 out[o] = 48; o = o + 1
28 } else {
29 var nd: i64 = 0; var t: i64 = plen
30 while t > 0 { nd = nd + 1; t = t / 16 }
31 var pos: i64 = nd - 1
32 var dv: i64 = 0; var k: i64 = 0
33 while pos >= 0 {
34 dv = plen; k = 0
35 while k < pos { dv = dv / 16; k = k + 1 }
36 out[o] = rs_hexdig(dv % 16); o = o + 1
37 pos = pos - 1
38 }
39 }
40 out[o]=13; out[o+1]=10; o = o + 2
41 var j: i64 = 0
42 while j < plen { out[o + j] = payload[j]; j = j + 1 }
43 o = o + plen
44 out[o]=13; out[o+1]=10; o = o + 2
45 return o
46}
47
48// decode one chunk at off. On a data chunk: info[0]=payload_off info[1]=payload_len, return next off (>0).
49// On terminator (len 0): return 0. On malformed (bad hex / missing CRLF / truncated): return -1.
50func rs_chunk_decode(buf: *u8, n: i64, off: i64, info: *i64) -> i64 {
51 var o: i64 = off
52 var len: i64 = 0
53 var ndig: i64 = 0
54 var bad: i64 = 0
55 var hv: i64 = 0
56 var scan: i64 = 1
57 while scan == 1 {
58 if o >= n { bad = 1; scan = 0 } else {
59 if buf[o] == (13 as u8) { scan = 0 } else {
60 hv = rs_hexval(buf[o])
61 if hv < 0 { bad = 1; scan = 0 } else {
62 len = len * 16 + hv; ndig = ndig + 1; o = o + 1
63 }
64 }
65 }
66 }
67 if bad == 1 { return 0 - 1 }
68 if ndig == 0 { return 0 - 1 }
69 if o + 1 >= n { return 0 - 1 }
70 if buf[o] != (13 as u8) { return 0 - 1 }
71 if buf[o+1] != (10 as u8) { return 0 - 1 }
72 o = o + 2
73 if len == 0 { return 0 }
74 if o + len + 2 > n { return 0 - 1 }
75 if buf[o+len] != (13 as u8) { return 0 - 1 }
76 if buf[o+len+1] != (10 as u8) { return 0 - 1 }
77 info[0] = o
78 info[1] = len
79 return o + len + 2
80}