code wiki / _hdl_build / nx_sh_safeharbor.nx

nx_sh_safeharbor.nx source

↩ module page · 61 lines · 4814 B

1// nx_sh_safeharbor.nx -- LIB: the SAFE-HARBOR mechanism layer for the share-hub (operator: shift responsibility to 2// the peers; honor takedowns; don't be liable for a virus a peer uploaded; enable LEGAL content). Three append-only 3// logs (additive-only, history sacred -- Cardinal 13) keyed by content-id: 4// ATTESTATIONS cid<TAB>peer<TAB>legal<TAB>ts -- the uploader NAMES THEMSELVES the responsible party + attests legal 5// TAKEDOWNS cid -- a valid DMCA-512 notice tombstones a cid (idempotent, no re-list) 6// FLAGS cid<TAB>flagger -- peers flag abuse/malware; enough flags auto-SUPPRESS (community, no hub curation) 7// A cid is ACTIVE (discoverable/served) iff it has an attestation AND is not taken-down AND flags < threshold. 8// This is the architecture-to-fit Section-230 (user content, hub is a conduit) + DMCA-512 (act on notice) + the 9// Sony/Grokster neutrality line. ⚠NOT LEGAL ADVICE -- an attorney must review. Self-contained (own parsing). No TLS. 10// license_tier: ORIGINAL 11import "nx_syscalls.nx" 12 13func sh_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 14func sh_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o } 15func sh_wdec(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; if v==0 { dst[o]=48 as u8; return o+1 } let t:*u8=sys_mmap(24); var k:i64=0; var m:i64=v; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var j:i64=k-1; while j>=0 { dst[o]=t[j]; o=o+1; j=j-1 } return o } 16func sh_eol(b: *u8, from: i64, blen: i64) -> i64 { var e: i64=from; while e<blen { if b[e]==10 as u8 { return e } e=e+1 } return blen } 17func sh_tab(b: *u8, from: i64, limit: i64) -> i64 { var e: i64=from; while e<limit { if b[e]==9 as u8 { return e } e=e+1 } return limit } 18func sh_memeq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 19// count lines whose FIRST tab-delimited field == cid. 20func sh_count_by_cid(buf: *u8, blen: i64, cid: *u8) -> i64 { 21 let cl: i64 = sh_slen(cid); var cnt: i64=0; var ls: i64=0 22 while ls<blen { let le: i64=sh_eol(buf,ls,blen); let ft: i64=sh_tab(buf,ls,le); if ft-ls==cl { if sh_memeq((buf as i64+ls) as *u8, cid, cl)==1 { cnt=cnt+1 } } ls=le+1 } 23 return cnt 24} 25func sh_field(row: *u8, rlen: i64, idx: i64, out: *u8) -> i64 { var fs: i64=0; var f: i64=0; while f<idx { let t: i64=sh_tab(row,fs,rlen); fs=t+1; f=f+1 } let fe: i64=sh_tab(row,fs,rlen); var o: i64=0; var k: i64=fs; while k<fe { out[o]=row[k]; o=o+1; k=k+1 } out[o]=0 as u8; return o } 26 27// ATTEST: the uploader records themselves as the responsible party + attests the content is legal. Returns new len. 28func sh_attest(abuf: *u8, alen: i64, cid: *u8, peer: *u8, legal: i64, ts: i64) -> i64 { 29 var o: i64=alen; o=sh_cat(abuf,o,cid); abuf[o]=9 as u8; o=o+1 30 o=sh_cat(abuf,o,peer); abuf[o]=9 as u8; o=o+1 31 o=sh_wdec(abuf,o,legal); abuf[o]=9 as u8; o=o+1 32 o=sh_wdec(abuf,o,ts); abuf[o]=10 as u8; o=o+1 33 return o 34} 35// RESPONSIBLE: who attested for this cid (the party liability flows to) -> out_peer. Returns 1 if attested. 36func sh_responsible(abuf: *u8, alen: i64, cid: *u8, out_peer: *u8) -> i64 { 37 let cl: i64=sh_slen(cid); var ls: i64=0 38 while ls<alen { let le: i64=sh_eol(abuf,ls,alen); let ft: i64=sh_tab(abuf,ls,le) 39 if ft-ls==cl { if sh_memeq((abuf as i64+ls) as *u8, cid, cl)==1 { sh_field((abuf as i64+ls) as *u8, le-ls, 1, out_peer); return 1 } } 40 ls=le+1 } 41 out_peer[0]=0 as u8; return 0 42} 43// TAKEDOWN: honor a notice -> tombstone the cid (idempotent -- additive, never re-listed). Returns new takedown-log len. 44func sh_takedown(tbuf: *u8, tlen: i64, cid: *u8) -> i64 { 45 if sh_count_by_cid(tbuf, tlen, cid) > 0 { return tlen } 46 var o: i64=tlen; o=sh_cat(tbuf,o,cid); tbuf[o]=10 as u8; o=o+1; return o 47} 48func sh_is_taken_down(tbuf: *u8, tlen: i64, cid: *u8) -> i64 { if sh_count_by_cid(tbuf,tlen,cid)>0 { return 1 } return 0 } 49// FLAG: a peer flags abuse/malware. Returns new flag-log len. 50func sh_flag(fbuf: *u8, flen: i64, cid: *u8, flagger: *u8) -> i64 { 51 var o: i64=flen; o=sh_cat(fbuf,o,cid); fbuf[o]=9 as u8; o=o+1; o=sh_cat(fbuf,o,flagger); fbuf[o]=10 as u8; o=o+1; return o 52} 53func sh_flag_count(fbuf: *u8, flen: i64, cid: *u8) -> i64 { return sh_count_by_cid(fbuf, flen, cid) } 54// ACTIVE: discoverable/served iff attested AND not taken-down AND flags < threshold. 55func sh_active(abuf: *u8, alen: i64, tbuf: *u8, tlen: i64, fbuf: *u8, flen: i64, cid: *u8, threshold: i64) -> i64 { 56 let peer: *u8=sys_mmap(256) 57 if sh_responsible(abuf, alen, cid, peer)==0 { return 0 } 58 if sh_is_taken_down(tbuf, tlen, cid)==1 { return 0 } 59 if sh_flag_count(fbuf, flen, cid) >= threshold { return 0 } 60 return 1 61}