interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / omega-strand-hub / main.om

7280 lines · 174643 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note omega-strand-hub: pure seeded-Omega (om2) zero-JS Helix Strand Exchange & Clip Editor
3note Fail-closed OLLMSP01 specialist pack verifier, content-addressed SHA-256 store,
4note deterministic OMSPCLIP01 clip composer/validator, HTML UI + JSON/TSV agent APIs.
5om2
6
7const HUB_DEF_PORT ulong 18491
8const HUB_IP uint 2130706433
9const HUB_DEF_MAX_UP ulong 33554432
10const HUB_HDR_OVERHEAD ulong 65536
11const HUB_MAX_HDR ulong 16384
12const HUB_MAX_REQ ulong 32768
13const HUB_MAX_RESP ulong 1048576
14const HUB_IO_SZ ulong 65536
15const HUB_IO_TMO long 15000000000
16const HUB_UP_TMO long 30000000000
17
18const HUB_MAX_STRANDS ulong 512
19const HUB_MAX_CLIPS ulong 256
20const HUB_MAX_PARTS ulong 32
21
22const OSPMAG ulong 3544421225449933903
23const FNV_OFF ulong 14695981039346656037
24const FNV_PRM ulong 1099511628211
25
26const F_SHA ulong 64
27const F_NAME ulong 80
28const F_AUTH ulong 64
29const F_LIC ulong 32
30const F_TERM ulong 128
31const F_PROV ulong 160
32const F_PT ulong 64
33const F_FN ulong 128
34const F_DESC ulong 256
35
36note Upload status slots in g_u_st
37const US_ERR ulong 0
38const US_BL ulong 1
39const US_SL ulong 2
40const US_BHASH ulong 3
41const US_NPAR ulong 4
42const US_PHASH ulong 5
43const US_POFF ulong 6
44const US_PLEN ulong 7
45const US_DUP_IDX ulong 8
46const US_HAS_PACK ulong 9
47const US_HTTP_CODE ulong 10
48const US_NEW_IDX ulong 11
49const US_BAD_FN ulong 12
50const US_BAD_PT ulong 13
51const US_BAD_EXP ulong 14
52
53note Request parse slots in g_rq_st
54const RQ_METH ulong 0
55const RQ_P0 ulong 1
56const RQ_P1 ulong 2
57const RQ_Q0 ulong 3
58const RQ_Q1 ulong 4
59const RQ_HEND ulong 5
60const RQ_CLEN ulong 6
61const RQ_HAS_CL ulong 7
62const RQ_BAD_HDR ulong 8
63const RQ_CTYPE ulong 9
64const RQ_EXPECT ulong 10
65
66note Upload error codes
67const UE_OK ulong 0
68const UE_MISSING_PACK ulong 1
69const UE_MISSING_META ulong 2
70const UE_BAD_FILENAME ulong 3
71const UE_TOO_BIG ulong 4
72const UE_TRUNCATED ulong 5
73const UE_BAD_MAGIC ulong 6
74const UE_BAD_LAYERS ulong 7
75const UE_BAD_BASE_HASH ulong 8
76const UE_BAD_PARAMS ulong 9
77const UE_SIZE_MISMATCH ulong 10
78const UE_PACK_HASH ulong 11
79const UE_NONFINITE ulong 12
80const UE_BAD_PARENT_TRUNK ulong 13
81const UE_MERKLE_MISMATCH ulong 14
82const UE_MERKLE_LINEAGE ulong 15
83const UE_SHA_MISMATCH ulong 16
84const UE_DUPLICATE ulong 17
85const UE_STORAGE_FULL ulong 18
86const UE_IO_FAIL ulong 19
87const UE_BAD_MULTIPART ulong 20
88
89note Clip error codes
90const CE_OK ulong 0
91const CE_STRAND_COUNT ulong 1
92const CE_UNKNOWN_STRAND ulong 2
93const CE_INCOMPAT_TRUNK ulong 3
94const CE_BAD_WEIGHT ulong 4
95const CE_BAD_MODE ulong 5
96const CE_BAD_TAU ulong 6
97const CE_BAD_SPLICE ulong 7
98const CE_BAD_TOKENS ulong 8
99const CE_TAMPERED ulong 9
100const CE_BAD_FORMAT ulong 10
101const CE_STORAGE_FULL ulong 11
102const CE_IO_FAIL ulong 12
103const CE_DUPLICATE_STRAND ulong 13
104
105note ---- Global state ----
106global g_dir uchar 512
107global g_dirl ulong 1
108global g_port ulong 1
109global g_max_up ulong 1
110global g_seq ulong 1
111
112global g_req uchar 32768
113global g_reql ulong 1
114global g_rq_st ulong 16
115global g_io uchar 65536
116global g_io2 uchar 65536
117global g_resp uchar 1048576
118global g_respl ulong 1
119global g_hdr uchar 2048
120global g_hdrl ulong 1
121
122global g_p1 uchar 1024
123global g_p1l ulong 1
124global g_p2 uchar 1024
125global g_p2l ulong 1
126global g_pk uchar 2100
127global g_tmp_raw uchar 1024
128global g_tmp_rawl ulong 1
129
130note Streaming FIPS 180-4 SHA-256 state
131global g_sh_k uint 64
132global g_sh_h uint 8
133global g_sh_w uint 64
134global g_sh_v uint 8
135global g_sh_buf uchar 64
136global g_sh_len ulong 1
137global g_sh_init ulong 1
138global g_sh_dig uchar 32
139global g_sh_hex uchar 64
140
141note Verified strand catalog (up to 512 records)
142global g_scnt ulong 1
143global g_c_sha uchar 32768
144global g_c_name uchar 40960
145global g_c_namel ulong 512
146global g_c_auth uchar 32768
147global g_c_authl ulong 512
148global g_c_lic uchar 16384
149global g_c_licl ulong 512
150global g_c_term uchar 65536
151global g_c_terml ulong 512
152global g_c_prov uchar 81920
153global g_c_provl ulong 512
154global g_c_pt uchar 32768
155global g_c_ptl ulong 512
156global g_c_fn uchar 65536
157global g_c_fnl ulong 512
158global g_c_desc uchar 131072
159global g_c_descl ulong 512
160global g_c_bhash ulong 512
161global g_c_bl ulong 512
162global g_c_sl ulong 512
163global g_c_npar ulong 512
164global g_c_phash ulong 512
165global g_c_sz ulong 512
166global g_c_dl ulong 512
167global g_c_ts ulong 512
168
169note Verified .spclip catalog (up to 256 records)
170global g_kcnt ulong 1
171global g_k_sha uchar 16384
172global g_k_name uchar 20480
173global g_k_namel ulong 256
174global g_k_auth uchar 16384
175global g_k_authl ulong 256
176global g_k_lic uchar 8192
177global g_k_licl ulong 256
178global g_k_bhash ulong 256
179global g_k_mode ulong 256
180global g_k_tau ulong 256
181global g_k_splice ulong 256
182global g_k_sc ulong 256
183global g_k_ts ulong 256
184global g_k_dl ulong 256
185global g_k_seed ulong 256
186global g_k_steps ulong 256
187global g_k_lr ulong 256
188global g_k_tok ulong 256
189global g_k_ssha uchar 65536
190global g_k_sw long 1024
191
192note Per-upload working buffers
193global g_u_name uchar 80
194global g_u_namel ulong 1
195global g_u_auth uchar 64
196global g_u_authl ulong 1
197global g_u_lic uchar 32
198global g_u_licl ulong 1
199global g_u_term uchar 128
200global g_u_terml ulong 1
201global g_u_prov uchar 160
202global g_u_provl ulong 1
203global g_u_pt uchar 64
204global g_u_ptl ulong 1
205global g_u_fn uchar 128
206global g_u_fnl ulong 1
207global g_u_desc uchar 256
208global g_u_descl ulong 1
209global g_u_exp uchar 64
210global g_u_expl ulong 1
211global g_u_bnd uchar 96
212global g_u_bndl ulong 1
213global g_u_delim uchar 104
214global g_u_deliml ulong 1
215global g_u_st ulong 16
216global g_u_sha uchar 64
217global g_u_fbuf uchar 256
218global g_u_fbufl ulong 1
219global g_u_pname uchar 64
220global g_u_pnamel ulong 1
221global g_u_pfn uchar 160
222global g_u_pfnl ulong 1
223global g_u_has_pfn ulong 1
224
225note Per-clip working buffers
226global g_e_name uchar 80
227global g_e_namel ulong 1
228global g_e_auth uchar 64
229global g_e_authl ulong 1
230global g_e_lic uchar 32
231global g_e_licl ulong 1
232global g_e_prompt uchar 256
233global g_e_promptl ulong 1
234global g_e_merges uchar 128
235global g_e_mergesl ulong 1
236global g_e_ssha uchar 256
237global g_e_sset ulong 4
238global g_e_sidx ulong 4
239global g_e_sw long 4
240global g_e_wset ulong 4
241global g_e_wbad ulong 4
242global g_e_vbh ulong 4
243global g_e_vbl ulong 4
244global g_e_vsl ulong 4
245global g_e_vbh_set ulong 4
246global g_e_vbl_set ulong 4
247global g_e_vsl_set ulong 4
248global g_e_st ulong 16
249global g_e_man uchar 16384
250global g_e_manl ulong 1
251global g_e_sha uchar 64
252global g_e_bsha uchar 64
253global g_e_vbuf uchar 16384
254global g_e_vbufl ulong 1
255
256note Clip editor state indices in g_e_st
257const ES_MODE ulong 0
258const ES_TAU ulong 1
259const ES_SPLICE ulong 2
260const ES_TOK ulong 3
261const ES_SEED ulong 4
262const ES_STEPS ulong 5
263const ES_LR ulong 6
264const ES_ACTION ulong 7
265const ES_ERR ulong 8
266const ES_SC ulong 9
267const ES_BHASH ulong 10
268const ES_MIN_BL ulong 11
269const ES_HAS_POST ulong 12
270const ES_BAD_NUM ulong 13
271const ES_STRICT_VERIFY ulong 14
272
273note Query filter / prefill buffers
274global g_q_trunk uchar 64
275global g_q_trunkl ulong 1
276global g_q_lic uchar 32
277global g_q_licl ulong 1
278global g_q_pt uchar 64
279global g_q_ptl ulong 1
280global g_q_clip uchar 64
281global g_q_clipl ulong 1
282global g_q_s1 uchar 64
283global g_q_s1l ulong 1
284global g_q_s2 uchar 64
285global g_q_s2l ulong 1
286global g_seen_tr ulong 512
287global g_seen_cnt ulong 1
288
289note Scratch line parser buffer for catalog loading
290global g_line uchar 4096
291global g_linel ulong 1
292global g_fld_off ulong 32
293global g_fld_len ulong 32
294global g_fld_cnt ulong 1
295global g_hex16 uchar 16
296global g_qkey uchar 64
297global g_qkeyl ulong 1
298global g_qval uchar 256
299global g_qvall ulong 1
300global g_dpath uchar 64
301
302note ---- Static constants and texts ----
303text t_def_dir "./strand-hub-data"
304text t_sub_blobs "/blobs"
305text t_sub_clips "/clips"
306text t_sub_tmp "/tmp"
307text t_sub_cat "/catalog.tsv"
308text t_sub_kcat "/clips.tsv"
309text t_tmp_raw_pre "/tmp/raw-"
310text t_tmp_sp_pre "/tmp/sp-"
311text t_tmp_cat_pre "/tmp/cat-"
312text t_tmp_kcat_pre "/tmp/kcat-"
313text t_tmp_clip_pre "/tmp/clip-"
314text t_ext_sp ".sp"
315text t_ext_spclip ".spclip"
316text t_ext_tsv ".tsv"
317text t_ext_raw ".raw"
318text t_slash "/"
319text t_dash "-"
320
321text t_listen_err "om-strand-hub: cannot bind 127.0.0.1:"
322text t_dir_err "om-strand-hub: cannot create or access data_dir="
323text t_started "om-strand-hub: listening on 127.0.0.1:"
324text t_dir_msg " data="
325text t_nl "\n"
326
327text t_http_100 "HTTP/1.1 100 Continue"
328text t_http_200 "HTTP/1.1 200 OK"
329text t_http_201 "HTTP/1.1 201 Created"
330text t_http_400 "HTTP/1.1 400 Bad Request"
331text t_http_404 "HTTP/1.1 404 Not Found"
332text t_http_405 "HTTP/1.1 405 Method Not Allowed"
333text t_http_409 "HTTP/1.1 409 Conflict"
334text t_http_411 "HTTP/1.1 411 Length Required"
335text t_http_413 "HTTP/1.1 413 Payload Too Large"
336text t_http_415 "HTTP/1.1 415 Unsupported Media Type"
337text t_http_500 "HTTP/1.1 500 Internal Server Error"
338text t_http_507 "HTTP/1.1 507 Insufficient Storage"
339
340text t_ct_html "Content-Type: text/html; charset=utf-8"
341text t_ct_css "Content-Type: text/css; charset=utf-8"
342text t_ct_txt "Content-Type: text/plain; charset=utf-8"
343text t_ct_tsv "Content-Type: text/tab-separated-values; charset=utf-8"
344text t_ct_json "Content-Type: application/json; charset=utf-8"
345text t_ct_bin "Content-Type: application/octet-stream"
346text t_cc_no "Cache-Control: no-store"
347text t_cc_css "Cache-Control: public, max-age=300"
348text t_nosniff "X-Content-Type-Options: nosniff"
349text t_conn_close "Connection: close"
350text t_cl_pre "Content-Length: "
351text t_cd_pre "Content-Disposition: attachment; filename=\""
352text t_cd_suf "\""
353text t_xsha_pre "X-Strand-SHA256: "
354text t_xbh_pre "X-Strand-Base-Hash: "
355text t_xksha_pre "X-Spclip-SHA256: "
356
357text t_ok_body "ok\n"
358text t_404_body "not found\n"
359text t_405_body "method not allowed\n"
360text t_411_body "content-length required\n"
361text t_413_body "payload exceeds bounded upload limit\n"
362text t_400_body "bad request\n"
363
364text t_m_get "GET "
365text t_m_head "HEAD "
366text t_m_post "POST "
367text t_h_cl "content-length:"
368text t_h_te "transfer-encoding:"
369text t_h_ct "content-type:"
370text t_h_expect "expect:"
371text t_h_xname "x-strand-name:"
372text t_h_xauth "x-strand-author:"
373text t_h_xlic "x-strand-license:"
374text t_h_xterm "x-strand-terms:"
375text t_h_xprov "x-strand-provenance:"
376text t_h_xpt "x-strand-parent-trunk:"
377text t_h_xdesc "x-strand-description:"
378text t_h_xfn "x-strand-filename:"
379text t_h_xexp "x-strand-sha256:"
380text t_ct_mp "multipart/form-data"
381text t_ct_form "application/x-www-form-urlencoded"
382text t_ct_oct "application/octet-stream"
383text t_ct_plain "text/plain"
384text t_bnd_eq "boundary="
385text t_cdisp "content-disposition:"
386text t_p_name "name=\""
387text t_p_fn "filename=\""
388
389text t_rt_css "/strands/hub.css"
390text t_rt_health "/strands/health"
391text t_rt_itsv "/strands/index.tsv"
392text t_rt_ijson "/strands/index.json"
393text t_rt_ktsv "/strands/clips/index.tsv"
394text t_rt_kjson "/strands/clips/index.json"
395text t_rt_upload "/strands/upload"
396text t_rt_clip_ver "/strands/clip/verify"
397text t_rt_clip_pre "/strands/clip/"
398text t_rt_clip "/strands/clip"
399text t_rt_pack_pre "/strands/pack/"
400text t_rt_strand_pre "/strands/strand/"
401text t_rt_root "/strands/"
402text t_rt_root2 "/strands"
403text t_rt_slash "/"
404
405text t_def_lic "Apache-2.0"
406text t_def_term "open-source"
407text t_def_prompt "fn main to int"
408text t_def_merges "-"
409text t_def_clip_name "custom-helix-clip"
410text t_def_author "community"
411text t_spclip_prov_pre "spclip:"
412text t_cat_hdr1 "# schema=omega.strand_hub.v1\n"
413text t_cat_hdr2 "sha256\tname\tauthor\tlicense\tterms\tprovenance\tparent_trunk\tbase_hash\tbase_l\tspec_l\tn_spec_params\tpack_hash\tsize_bytes\tdownloads\tuploaded_ns\tfilename\tdescription\n"
414text t_kcat_hdr1 "# schema=omega.spclip_hub.v1\n"
415text t_kcat_hdr2 "clip_sha256\tname\tauthor\tlicense\tbase_hash\tmode\ttau_milli\tsplice_layer\tstrand_count\tdownloads\tcreated_ns\ttokens\tft_seed\tft_steps\tft_lr_micro\ts1\tw1\ts2\tw2\ts3\tw3\ts4\tw4\n"
416
417text t_esc_amp "&"
418text t_esc_lt "<"
419text t_esc_gt ">"
420text t_esc_quot """
421text t_esc_apos "'"
422
423text t_sha_k_str "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298"
424
425note Field name constants for form and manifest parsing
426text t_f_name "name"
427text t_f_author "author"
428text t_f_desc "description"
429text t_f_lic "license"
430text t_f_terms "terms"
431text t_f_prov "provenance"
432text t_f_pt "parent_trunk"
433text t_f_exp "expected_sha256"
434text t_f_pack "pack"
435text t_f_trunk "trunk"
436text t_f_clip "clip"
437text t_f_s1 "s1"
438text t_f_s2 "s2"
439text t_f_s3 "s3"
440text t_f_s4 "s4"
441text t_f_w1 "w1"
442text t_f_w2 "w2"
443text t_f_w3 "w3"
444text t_f_w4 "w4"
445text t_f_mode "mode"
446text t_f_tau "tau"
447text t_f_splice "splice_layer"
448text t_f_tokens "tokens"
449text t_f_prompt "prompt"
450text t_f_merges "merges"
451text t_f_seed "ft_seed"
452text t_f_steps "ft_steps"
453text t_f_lr "ft_lr_micro"
454text t_f_action "action"
455text t_f_clip_name "clip_name"
456text t_v_download "download"
457text t_mf_magic "format=OMSPCLIP01"
458text t_mf_bsha "body_sha256="
459text t_mf_name "name="
460text t_mf_auth "author="
461text t_mf_lic "license="
462text t_mf_bh "base_hash="
463text t_mf_pt "parent_trunk="
464text t_mf_sp "splice_layer="
465text t_mf_mode "mode="
466text t_mf_tau "tau_milli="
467text t_mf_tok "tokens="
468text t_mf_seed "ft_seed="
469text t_mf_steps "ft_steps="
470text t_mf_lr "ft_lr_micro="
471text t_mf_sc "strand_count="
472text t_mf_s1_sha "strand.1.sha256="
473text t_mf_s2_sha "strand.2.sha256="
474text t_mf_s3_sha "strand.3.sha256="
475text t_mf_s4_sha "strand.4.sha256="
476text t_mf_s1_w "strand.1.weight_milli="
477text t_mf_s2_w "strand.2.weight_milli="
478text t_mf_s3_w "strand.3.weight_milli="
479text t_mf_s4_w "strand.4.weight_milli="
480text t_mf_s1_bh "strand.1.base_hash="
481text t_mf_s2_bh "strand.2.base_hash="
482text t_mf_s3_bh "strand.3.base_hash="
483text t_mf_s4_bh "strand.4.base_hash="
484text t_mf_s1_bl "strand.1.base_l="
485text t_mf_s2_bl "strand.2.base_l="
486text t_mf_s3_bl "strand.3.base_l="
487text t_mf_s4_bl "strand.4.base_l="
488text t_mf_s1_sl "strand.1.spec_l="
489text t_mf_s2_sl "strand.2.spec_l="
490text t_mf_s3_sl "strand.3.spec_l="
491text t_mf_s4_sl "strand.4.spec_l="
492text t_mf_s_pre "strand."
493text t_mf_sha_k ".sha256="
494text t_mf_bh_k ".base_hash="
495text t_mf_bl_k ".base_l="
496text t_mf_sl_k ".spec_l="
497text t_mf_wm_k ".weight_milli="
498text t_mf_fn_k ".filename="
499
500note ---- CSS stylesheet (/strands/hub.css) - CSP safe, zero JS, Interplane 4-strand theme ----
501text t_css1 ":root{--hub-a:var(--strand-a,#22d3ee);--hub-b:var(--strand-b,#fbbf24);--hub-c:var(--strand-c,#3b82f6);--hub-d:var(--strand-d,#34d399);--hub-bg:var(--paper,#060a12);--hub-card:var(--white,#0c1320);--hub-panel:var(--panel,#090f1a);--hub-ink:var(--ink,#f3f6fb);--hub-muted:var(--muted,#a3b1c6);--hub-line:var(--line,#1a2536);--hub-strong:var(--line-strong,#2d3d58)}html,body{overflow-x:clip}.hub-hero{position:relative;padding:44px 0 36px;border-bottom:1px solid var(--hub-line);background:radial-gradient(900px 360px at 12% 0%,#22d3ee14,transparent 65%),radial-gradient(760px 340px at 88% 10%,#34d39914,transparent 65%),var(--hub-bg)}.hub-hero h1{margin:10px 0 14px;font-size:clamp(30px,4.4vw,50px)}.hub-hero .lede{max-width:740px;font-size:17px;line-height:1.65;color:var(--hub-muted)}.hub-cta{display:flex;flex-wrap:wrap;gap:12px;margin:22px 0 26px}.hub-btn{display:inline-flex;align-items:center;gap:8px;padding:11px 18px;border-radius:8px;font-weight:600;font-size:14px;text-decoration:none;border:1px solid var(--hub-strong);cursor:pointer;background:var(--hub-card);color:var(--hub-ink);transition:border-color .15s,background .15s}.hub-btn:hover{border-color:var(--hub-a);color:var(--hub-ink)}.hub-btn-primary{background:#2563eb;border-color:#3b82f6;color:#fff}.hub-btn-primary:hover{background:#1d4ed8;border-color:#60a5fa;color:#fff}.hub-btn-emerald{background:#065f46;border-color:#10b981;color:#ecfdf5}.hub-btn-emerald:hover{background:#047857;border-color:#34d399;color:#fff}.hub-btn-sm{padding:7px 12px;font-size:13px}.hub-kpis{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin-top:8px}@media(max-width:860px){.hub-kpis{grid-template-columns:repeat(2,minmax(0,1fr))}}@media(max-width:480px){.hub-kpis{grid-template-columns:1fr}}.hub-kpi{background:var(--hub-card);border:1px solid var(--hub-line);border-radius:10px;padding:14px 16px}.hub-kpi-k{display:block;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--hub-muted)}.hub-kpi-v{display:block;margin-top:4px;font-size:20px;font-weight:700;color:var(--hub-ink)}"
502
503text t_css2 ".hub-sec{padding:36px 0;border-bottom:1px solid var(--hub-line)}.hub-sec-head{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:flex-end;gap:14px;margin-bottom:20px}.hub-sec-head h2{margin-top:6px}.hub-grid3{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px}@media(max-width:860px){.hub-grid3{grid-template-columns:1fr}}.hub-story{background:var(--hub-card);border:1px solid var(--hub-line);border-top:3px solid var(--hub-a);border-radius:12px;padding:18px 20px}.hub-story.st-b{border-top-color:var(--hub-b)}.hub-story.st-d{border-top-color:var(--hub-d)}.hub-story h3{margin-bottom:8px;font-size:17px}.hub-story p{font-size:14px;line-height:1.6}.hub-flow{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px;margin-top:18px}@media(max-width:860px){.hub-flow{grid-template-columns:1fr}}.hub-step{position:relative;background:var(--hub-panel);border:1px solid var(--hub-line);border-radius:12px;padding:18px 20px}.hub-step-num{display:inline-block;padding:2px 9px;border-radius:999px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:11px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;background:#111c30;color:var(--hub-a);border:1px solid var(--hub-strong);margin-bottom:8px}.hub-step h3{font-size:16px;margin-bottom:6px}.hub-step p{font-size:14px;line-height:1.55}.hub-details{margin-top:18px;background:var(--hub-panel);border:1px solid var(--hub-line);border-radius:10px;padding:14px 18px}.hub-details summary{cursor:pointer;font-weight:600;color:var(--hub-ink);font-size:14px}.hub-details-body{margin-top:12px;font-size:13px;color:var(--hub-muted);line-height:1.6}"
504
505text t_css3 ".hub-banner{border-radius:12px;padding:18px 20px;margin:20px 0;border:1px solid var(--hub-strong);background:var(--hub-card)}.hub-banner.ok{border-color:#10b981;background:#052e22}.hub-banner.err{border-color:#ef4444;background:#2a0f14}.hub-banner h3{margin-bottom:6px;font-size:17px}.hub-filter{display:flex;flex-wrap:wrap;gap:10px;align-items:center;margin-bottom:16px;background:var(--hub-panel);border:1px solid var(--hub-line);border-radius:10px;padding:12px 14px}.hub-in,.hub-sel,.hub-ta{width:100%;padding:10px 12px;border-radius:8px;border:1px solid var(--hub-strong);background:#070c16;color:var(--hub-ink);font-size:14px;font-family:inherit}.hub-in-grow{flex:1 1 260px;min-width:0}.hub-pills{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin:0 0 18px}.hub-pill{display:inline-flex;align-items:center;gap:6px;padding:5px 11px;border-radius:999px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:12px;text-decoration:none;border:1px solid var(--hub-strong);background:var(--hub-card);color:var(--hub-ink)}.hub-pill:hover,.hub-pill.active{border-color:var(--hub-a);background:#0e2238}.hub-cards{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px}@media(max-width:860px){.hub-cards{grid-template-columns:1fr}}.hub-card{background:var(--hub-card);border:1px solid var(--hub-line);border-radius:12px;padding:18px 20px;display:flex;flex-direction:column;gap:12px}.hub-card-top{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:flex-start;gap:8px}.hub-card-title{font-size:18px;font-weight:700;color:var(--hub-ink);text-decoration:none}.hub-card-title:hover{color:var(--hub-a)}.hub-badges{display:flex;flex-wrap:wrap;gap:6px}.hub-badge{display:inline-block;padding:2px 9px;border-radius:999px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:11px;font-weight:600;border:1px solid var(--hub-strong);background:#0f192b;color:var(--hub-ink)}.hub-badge.lic{border-color:#10b981;color:#6ee7b7;background:#06291e}.hub-badge.lay{border-color:#22d3ee;color:#67e8f9;background:#082733}.hub-badge.ok{border-color:#34d399;color:#a7f3d0;background:#06291e}.hub-badge.warn{border-color:#f59e0b;color:#fde68a;background:#2b1e07}.hub-meta{display:flex;flex-wrap:wrap;gap:8px 14px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:12px;color:var(--hub-muted)}.hub-code{margin:0;padding:12px 14px;border-radius:8px;background:#04070e;border:1px solid var(--hub-line);color:#dbeafe;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:12px;line-height:1.5;overflow-x:auto;white-space:pre-wrap;word-break:break-word}.hub-card-act{display:flex;flex-wrap:wrap;gap:8px;margin-top:auto}.hub-form-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px}@media(max-width:760px){.hub-form-grid{grid-template-columns:1fr}}.hub-fg{display:flex;flex-direction:column;gap:6px}.hub-fg.full{grid-column:1 / -1}.hub-lbl{font-size:12px;font-weight:600;letter-spacing:.04em;text-transform:uppercase;color:var(--hub-muted);font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}.hub-help{font-size:12px;color:var(--hub-muted)}.hub-slot{background:var(--hub-panel);border:1px solid var(--hub-line);border-radius:10px;padding:14px;display:grid;grid-template-columns:1fr 140px;gap:10px;align-items:end}@media(max-width:600px){.hub-slot{grid-template-columns:1fr}}.hub-empty{background:var(--hub-panel);border:1px dashed var(--hub-strong);border-radius:12px;padding:24px;text-align:center}.hub-table{width:100%;border-collapse:collapse;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:12px}.hub-table th,.hub-table td{text-align:left;padding:8px 10px;border-bottom:1px solid var(--hub-line);word-break:break-word}.hub-table th{color:var(--hub-muted);width:190px}\n"
506
507note ---- Core byte/hex/integer helpers ----
508
509fn to_low c uchar to uchar
510 if ge c 65
511 if le c 90
512 ret add c 32
513 end
514 end
515 ret c
516end
517
518fn hex_val c uchar to int
519 if ge c 48
520 if le c 57
521 ret int sub c 48
522 end
523 end
524 if ge c 97
525 if le c 102
526 ret int add sub c 97 10
527 end
528 end
529 if ge c 65
530 if le c 70
531 ret int add sub c 65 10
532 end
533 end
534 ret -1
535end
536
537fn is_hex_low c uchar to bool
538 if ge c 48
539 if le c 57
540 ret true
541 end
542 end
543 if ge c 97
544 if le c 102
545 ret true
546 end
547 end
548 ret false
549end
550
551fn hex_ch v ulong to uchar
552 d = and v 15
553 if lt d 10
554 ret uchar add 48 d
555 end
556 ret uchar add 87 d
557end
558
559fn fmt_hex16 v ulong to void
560 for i ulong 16
561 sh = mul sub 15 i 4
562 put g_hex16 i hex_ch shr v sh
563 end
564end
565
566fn read_u64_le_io off ulong to ulong
567 v = ulong 0
568 for i ulong 8
569 b = ulong get g_io add off i
570 v = or v shl b mul i 8
571 end
572 ret v
573end
574
575fn is_safe_fn_char c uchar to bool
576 if ge c 48
577 if le c 57
578 ret true
579 end
580 end
581 if ge c 65
582 if le c 90
583 ret true
584 end
585 end
586 if ge c 97
587 if le c 122
588 ret true
589 end
590 end
591 if or eq c 45 or eq c 46 eq c 95
592 ret true
593 end
594 ret false
595end
596
597note ---- Streaming FIPS 180-4 SHA-256 ----
598
599fn sha_init_k to void
600 if ne get g_sh_init 0 0
601 ret
602 end
603 put g_sh_init 0 1
604 p = ulong 0
605 for i ulong 64
606 v = ulong 0
607 while lt p len t_sha_k_str bound 8
608 if ge get t_sha_k_str p 48
609 break
610 end
611 p = add p 1
612 end
613 while lt p len t_sha_k_str bound 16
614 c = get t_sha_k_str p
615 if lt c 48
616 break
617 end
618 v = add mul v 10 ulong sub c 48
619 p = add p 1
620 end
621 put g_sh_k i uint v
622 end
623end
624
625fn sha_start to void
626 sha_init_k
627 put g_sh_h 0 0x6a09e667
628 put g_sh_h 1 0xbb67ae85
629 put g_sh_h 2 0x3c6ef372
630 put g_sh_h 3 0xa54ff53a
631 put g_sh_h 4 0x510e527f
632 put g_sh_h 5 0x9b05688c
633 put g_sh_h 6 0x1f83d9ab
634 put g_sh_h 7 0x5be0cd19
635 put g_sh_len 0 0
636end
637
638fn sha_s0 a uint to uint
639 ret xor xor rotr a 2 rotr a 13 rotr a 22
640end
641
642fn sha_s1 e uint to uint
643 ret xor xor rotr e 6 rotr e 11 rotr e 25
644end
645
646fn sha_g0 x uint to uint
647 ret xor xor rotr x 7 rotr x 18 shr x 3
648end
649
650fn sha_g1 x uint to uint
651 ret xor xor rotr x 17 rotr x 19 shr x 10
652end
653
654fn sha_rnd i ulong to void
655 e = get g_sh_v 4
656 t1 = add get g_sh_v 7 sha_s1 e
657 t1 = add t1 xor and e get g_sh_v 5 and not e get g_sh_v 6
658 t1 = add t1 add get g_sh_k i get g_sh_w i
659 a = get g_sh_v 0
660 b = get g_sh_v 1
661 c = get g_sh_v 2
662 t2 = add sha_s0 a xor xor and a b and a c and b c
663 put g_sh_v 7 get g_sh_v 6
664 put g_sh_v 6 get g_sh_v 5
665 put g_sh_v 5 e
666 put g_sh_v 4 add get g_sh_v 3 t1
667 put g_sh_v 3 c
668 put g_sh_v 2 b
669 put g_sh_v 1 a
670 put g_sh_v 0 add t1 t2
671end
672
673fn sha_compress_buf to void
674 for t ulong 16
675 q = mul t 4
676 w = shl uint get g_sh_buf q 24
677 w = or w shl uint get g_sh_buf add q 1 16
678 w = or w shl uint get g_sh_buf add q 2 8
679 w = or w uint get g_sh_buf add q 3
680 put g_sh_w t w
681 end
682 for t ulong 48
683 u = add t 16
684 x = add sha_g1 get g_sh_w sub u 2 get g_sh_w sub u 7
685 put g_sh_w u add x add sha_g0 get g_sh_w sub u 15 get g_sh_w sub u 16
686 end
687 for j ulong 8
688 put g_sh_v j get g_sh_h j
689 end
690 for i ulong 64
691 sha_rnd i
692 end
693 for j ulong 8
694 put g_sh_h j add get g_sh_h j get g_sh_v j
695 end
696end
697
698fn sha_feed_byte c uchar to void
699 n = get g_sh_len 0
700 bi = and n 63
701 put g_sh_buf bi c
702 put g_sh_len 0 add n 1
703 if eq bi 63
704 sha_compress_buf
705 end
706end
707
708fn sha_feed_io off ulong cnt ulong to void
709 for i ulong cnt bound 65536
710 sha_feed_byte get g_io add off i
711 end
712end
713
714fn sha_feed_man off ulong cnt ulong to void
715 for i ulong cnt bound 16384
716 sha_feed_byte get g_e_man add off i
717 end
718end
719
720fn sha_feed_vbuf off ulong cnt ulong to void
721 for i ulong cnt bound 16384
722 sha_feed_byte get g_e_vbuf add off i
723 end
724end
725
726fn sha_finish to void
727 tot = get g_sh_len 0
728 bits = shl tot 3
729 sha_feed_byte 128
730 while ne and get g_sh_len 0 63 56 bound 64
731 sha_feed_byte 0
732 end
733 for i ulong 8
734 sh = mul sub 7 i 8
735 sha_feed_byte uchar and shr bits sh 255
736 end
737 for j ulong 8
738 v = ulong get g_sh_h j
739 for q ulong 4
740 sh = mul sub 3 q 8
741 b = uchar and shr v sh 255
742 put g_sh_dig add mul j 4 q b
743 hi = mul add mul j 4 q 2
744 put g_sh_hex hi hex_ch shr ulong b 4
745 put g_sh_hex add hi 1 hex_ch ulong b
746 end
747 end
748end
749
750note ---- Response and HTTP header builders ----
751
752fn r_reset to void
753 put g_respl 0 0
754end
755
756fn r_b c uchar to void
757 n = get g_respl 0
758 if lt n HUB_MAX_RESP
759 put g_resp n c
760 put g_respl 0 add n 1
761 end
762end
763
764fn r_txt t view uchar * to void
765 for i ulong len t bound 16384
766 r_b get t i
767 end
768end
769
770fn r_u64 v ulong to void
771 buf = new uchar 24 0
772 p = ulong 24
773 n = v
774 if eq n 0
775 r_b 48
776 ret
777 end
778 while gt n 0 bound 24
779 p = sub p 1
780 put buf p uchar add 48 rem n 10
781 n = div n 10
782 end
783 while lt p 24 bound 24
784 r_b get buf p
785 p = add p 1
786 end
787end
788
789fn r_i64 v long to void
790 if lt v 0
791 r_b 45
792 r_u64 ulong neg v
793 ret
794 end
795 r_u64 ulong v
796end
797
798fn r_hex16 v ulong to void
799 fmt_hex16 v
800 for i ulong 16
801 r_b get g_hex16 i
802 end
803end
804
805fn r_esc_b c uchar to void
806 if eq c 38
807 r_txt t_esc_amp
808 ret
809 end
810 if eq c 60
811 r_txt t_esc_lt
812 ret
813 end
814 if eq c 62
815 r_txt t_esc_gt
816 ret
817 end
818 if eq c 34
819 r_txt t_esc_quot
820 ret
821 end
822 if eq c 39
823 r_txt t_esc_apos
824 ret
825 end
826 r_b c
827end
828
829fn r_jesc_b c uchar to void
830 if eq c 34
831 r_b 92
832 r_b 34
833 ret
834 end
835 if eq c 92
836 r_b 92
837 r_b 92
838 ret
839 end
840 if eq c 10
841 r_b 92
842 r_b 110
843 ret
844 end
845 if eq c 13
846 r_b 92
847 r_b 114
848 ret
849 end
850 if eq c 9
851 r_b 92
852 r_b 116
853 ret
854 end
855 if lt c 32
856 r_b 32
857 ret
858 end
859 r_b c
860end
861
862fn r_u_sha_all to void
863 for i ulong 64
864 r_b get g_u_sha i
865 end
866end
867
868fn r_c_sha idx ulong n ulong to void
869 base = mul idx F_SHA
870 for i ulong n bound 64
871 r_b get g_c_sha add base i
872 end
873end
874
875fn r_c_name_esc idx ulong to void
876 base = mul idx F_NAME
877 n = get g_c_namel idx
878 for i ulong n bound 80
879 r_esc_b get g_c_name add base i
880 end
881end
882
883fn r_c_name_raw idx ulong to void
884 base = mul idx F_NAME
885 n = get g_c_namel idx
886 for i ulong n bound 80
887 r_b get g_c_name add base i
888 end
889end
890
891fn r_c_name_json idx ulong to void
892 base = mul idx F_NAME
893 n = get g_c_namel idx
894 for i ulong n bound 80
895 r_jesc_b get g_c_name add base i
896 end
897end
898
899fn r_c_auth_esc idx ulong to void
900 base = mul idx F_AUTH
901 n = get g_c_authl idx
902 for i ulong n bound 64
903 r_esc_b get g_c_auth add base i
904 end
905end
906
907fn r_c_auth_raw idx ulong to void
908 base = mul idx F_AUTH
909 n = get g_c_authl idx
910 for i ulong n bound 64
911 r_b get g_c_auth add base i
912 end
913end
914
915fn r_c_auth_json idx ulong to void
916 base = mul idx F_AUTH
917 n = get g_c_authl idx
918 for i ulong n bound 64
919 r_jesc_b get g_c_auth add base i
920 end
921end
922
923fn r_c_lic_esc idx ulong to void
924 base = mul idx F_LIC
925 n = get g_c_licl idx
926 for i ulong n bound 32
927 r_esc_b get g_c_lic add base i
928 end
929end
930
931fn r_c_lic_raw idx ulong to void
932 base = mul idx F_LIC
933 n = get g_c_licl idx
934 for i ulong n bound 32
935 r_b get g_c_lic add base i
936 end
937end
938
939fn r_c_lic_json idx ulong to void
940 base = mul idx F_LIC
941 n = get g_c_licl idx
942 for i ulong n bound 32
943 r_jesc_b get g_c_lic add base i
944 end
945end
946
947fn r_c_term_esc idx ulong to void
948 base = mul idx F_TERM
949 n = get g_c_terml idx
950 for i ulong n bound 128
951 r_esc_b get g_c_term add base i
952 end
953end
954
955fn r_c_term_raw idx ulong to void
956 base = mul idx F_TERM
957 n = get g_c_terml idx
958 for i ulong n bound 128
959 r_b get g_c_term add base i
960 end
961end
962
963fn r_c_term_json idx ulong to void
964 base = mul idx F_TERM
965 n = get g_c_terml idx
966 for i ulong n bound 128
967 r_jesc_b get g_c_term add base i
968 end
969end
970
971fn r_c_prov_esc idx ulong to void
972 base = mul idx F_PROV
973 n = get g_c_provl idx
974 for i ulong n bound 160
975 r_esc_b get g_c_prov add base i
976 end
977end
978
979fn r_c_prov_raw idx ulong to void
980 base = mul idx F_PROV
981 n = get g_c_provl idx
982 for i ulong n bound 160
983 r_b get g_c_prov add base i
984 end
985end
986
987fn r_c_prov_json idx ulong to void
988 base = mul idx F_PROV
989 n = get g_c_provl idx
990 for i ulong n bound 160
991 r_jesc_b get g_c_prov add base i
992 end
993end
994
995fn r_c_pt_raw idx ulong to void
996 base = mul idx F_PT
997 n = get g_c_ptl idx
998 for i ulong n bound 64
999 r_b get g_c_pt add base i
1000 end
1001end
1002
1003fn r_c_fn_esc idx ulong to void
1004 base = mul idx F_FN
1005 n = get g_c_fnl idx
1006 for i ulong n bound 128
1007 r_esc_b get g_c_fn add base i
1008 end
1009end
1010
1011fn r_c_fn_raw idx ulong to void
1012 base = mul idx F_FN
1013 n = get g_c_fnl idx
1014 for i ulong n bound 128
1015 r_b get g_c_fn add base i
1016 end
1017end
1018
1019fn r_c_fn_json idx ulong to void
1020 base = mul idx F_FN
1021 n = get g_c_fnl idx
1022 for i ulong n bound 128
1023 r_jesc_b get g_c_fn add base i
1024 end
1025end
1026
1027fn r_c_desc_esc idx ulong to void
1028 base = mul idx F_DESC
1029 n = get g_c_descl idx
1030 for i ulong n bound 256
1031 r_esc_b get g_c_desc add base i
1032 end
1033end
1034
1035fn r_c_desc_raw idx ulong to void
1036 base = mul idx F_DESC
1037 n = get g_c_descl idx
1038 for i ulong n bound 256
1039 r_b get g_c_desc add base i
1040 end
1041end
1042
1043fn r_c_desc_json idx ulong to void
1044 base = mul idx F_DESC
1045 n = get g_c_descl idx
1046 for i ulong n bound 256
1047 r_jesc_b get g_c_desc add base i
1048 end
1049end
1050
1051fn h_reset to void
1052 put g_hdrl 0 0
1053end
1054
1055fn h_b c uchar to void
1056 n = get g_hdrl 0
1057 if lt n 2048
1058 put g_hdr n c
1059 put g_hdrl 0 add n 1
1060 end
1061end
1062
1063fn h_txt t view uchar * to void
1064 for i ulong len t bound 256
1065 h_b get t i
1066 end
1067end
1068
1069fn h_crlf to void
1070 h_b 13
1071 h_b 10
1072end
1073
1074fn h_u64 v ulong to void
1075 buf = new uchar 24 0
1076 p = ulong 24
1077 n = v
1078 if eq n 0
1079 h_b 48
1080 ret
1081 end
1082 while gt n 0 bound 24
1083 p = sub p 1
1084 put buf p uchar add 48 rem n 10
1085 n = div n 10
1086 end
1087 while lt p 24 bound 24
1088 h_b get buf p
1089 p = add p 1
1090 end
1091end
1092
1093fn h_hex16 v ulong to void
1094 fmt_hex16 v
1095 for i ulong 16
1096 h_b get g_hex16 i
1097 end
1098end
1099
1100note ---- Path builders and atomic rename ----
1101
1102fn p1_reset to void
1103 put g_p1l 0 0
1104end
1105
1106fn p1_b c uchar to void
1107 n = get g_p1l 0
1108 if lt n 1020
1109 put g_p1 n c
1110 put g_p1l 0 add n 1
1111 end
1112end
1113
1114fn p1_txt t view uchar * to void
1115 for i ulong len t bound 256
1116 p1_b get t i
1117 end
1118end
1119
1120fn p1_dir to void
1121 n = get g_dirl 0
1122 for i ulong n bound 512
1123 p1_b get g_dir i
1124 end
1125end
1126
1127fn p1_u64 v ulong to void
1128 buf = new uchar 24 0
1129 p = ulong 24
1130 n = v
1131 if eq n 0
1132 p1_b 48
1133 ret
1134 end
1135 while gt n 0 bound 24
1136 p = sub p 1
1137 put buf p uchar add 48 rem n 10
1138 n = div n 10
1139 end
1140 while lt p 24 bound 24
1141 p1_b get buf p
1142 p = add p 1
1143 end
1144end
1145
1146fn p2_reset to void
1147 put g_p2l 0 0
1148end
1149
1150fn p2_b c uchar to void
1151 n = get g_p2l 0
1152 if lt n 1020
1153 put g_p2 n c
1154 put g_p2l 0 add n 1
1155 end
1156end
1157
1158fn p2_txt t view uchar * to void
1159 for i ulong len t bound 256
1160 p2_b get t i
1161 end
1162end
1163
1164fn p2_dir to void
1165 n = get g_dirl 0
1166 for i ulong n bound 512
1167 p2_b get g_dir i
1168 end
1169end
1170
1171fn atomic_rename_p1_p2 to bool
1172 l1 = get g_p1l 0
1173 l2 = get g_p2l 0
1174 if or eq l1 0 or eq l2 0 gt add l1 l2 2040
1175 ret false
1176 end
1177 put g_pk 0 2
1178 put g_pk 1 0
1179 put g_pk 2 uchar and l1 255
1180 put g_pk 3 uchar shr l1 8
1181 for i ulong l1 bound 1024
1182 put g_pk add 4 i get g_p1 i
1183 end
1184 q = add 4 l1
1185 put g_pk q uchar and l2 255
1186 put g_pk add q 1 uchar shr l2 8
1187 for j ulong l2 bound 1024
1188 put g_pk add add q 2 j get g_p2 j
1189 end
1190 tot = add add q 2 l2
1191 r = renames g_pk 0 tot 0
1192 ret eq r 0
1193end
1194
1195fn build_tmp_p1 pre view uchar * ext view uchar * to void
1196 p1_reset
1197 p1_dir
1198 p1_txt pre
1199 p1_u64 ulong getpid
1200 p1_b 45
1201 s = add get g_seq 0 1
1202 put g_seq 0 s
1203 p1_u64 s
1204 p1_txt ext
1205end
1206
1207fn ensure_dirs to bool
1208 p1_reset
1209 p1_dir
1210 x = makedir g_p1 0 get g_p1l 0 493
1211 if ne faccess g_p1 0 get g_p1l 0 7 0
1212 ret false
1213 end
1214 p1_reset
1215 p1_dir
1216 p1_txt t_sub_blobs
1217 x = makedir g_p1 0 get g_p1l 0 493
1218 if ne faccess g_p1 0 get g_p1l 0 7 0
1219 ret false
1220 end
1221 p1_reset
1222 p1_dir
1223 p1_txt t_sub_clips
1224 x = makedir g_p1 0 get g_p1l 0 493
1225 if ne faccess g_p1 0 get g_p1l 0 7 0
1226 ret false
1227 end
1228 p1_reset
1229 p1_dir
1230 p1_txt t_sub_tmp
1231 x = makedir g_p1 0 get g_p1l 0 493
1232 if ne faccess g_p1 0 get g_p1l 0 7 0
1233 ret false
1234 end
1235 ret true
1236end
1237
1238fn write_all_resp fd int to bool
1239 n = get g_respl 0
1240 off = ulong 0
1241 for it ulong 2000000 bound 2000000
1242 if ge off n
1243 ret true
1244 end
1245 w = write fd g_resp off sub n off
1246 if le w 0
1247 if eq w -4
1248 continue
1249 end
1250 ret false
1251 end
1252 off = add off ulong w
1253 end
1254 ret ge off n
1255end
1256
1257note ---- Catalog and Clip persistence (TSV atomic save/load) ----
1258
1259fn split_line_tabs to void
1260 n = get g_linel 0
1261 cnt = ulong 0
1262 start = ulong 0
1263 for i ulong n bound 4096
1264 if eq get g_line i 9
1265 if lt cnt 32
1266 put g_fld_off cnt start
1267 put g_fld_len cnt sub i start
1268 cnt = add cnt 1
1269 end
1270 start = add i 1
1271 end
1272 end
1273 if lt cnt 32
1274 put g_fld_off cnt start
1275 put g_fld_len cnt sub n start
1276 cnt = add cnt 1
1277 end
1278 put g_fld_cnt 0 cnt
1279end
1280
1281fn parse_line_u64 fi ulong to ulong
1282 off = get g_fld_off fi
1283 fl = get g_fld_len fi
1284 if gt fl 24
1285 fl = 24
1286 end
1287 v = ulong 0
1288 for i ulong fl bound 24
1289 c = get g_line add off i
1290 if and ge c 48 le c 57
1291 v = add mul v 10 ulong sub c 48
1292 else
1293 break
1294 end
1295 end
1296 ret v
1297end
1298
1299fn parse_line_i64 fi ulong to long
1300 off = get g_fld_off fi
1301 fl = get g_fld_len fi
1302 if eq fl 0
1303 ret 0
1304 end
1305 if gt fl 24
1306 fl = 24
1307 end
1308 is_neg = false
1309 st = ulong 0
1310 if eq get g_line off 45
1311 is_neg = true
1312 st = 1
1313 end
1314 v = ulong 0
1315 i = st
1316 while lt i fl bound 24
1317 c = get g_line add off i
1318 if and ge c 48 le c 57
1319 if lt v 1000000000
1320 v = add mul v 10 ulong sub c 48
1321 end
1322 i = add i 1
1323 else
1324 break
1325 end
1326 end
1327 if is_neg
1328 ret neg long v
1329 end
1330 ret long v
1331end
1332
1333fn parse_line_hex64 fi ulong to ulong
1334 off = get g_fld_off fi
1335 fl = get g_fld_len fi
1336 if gt fl 16
1337 fl = 16
1338 end
1339 v = ulong 0
1340 for i ulong fl bound 16
1341 hv = hex_val get g_line add off i
1342 if lt hv 0
1343 break
1344 end
1345 v = or shl v 4 ulong hv
1346 end
1347 ret v
1348end
1349
1350fn load_cat_copy_sha idx ulong fi ulong to bool
1351 if ne get g_fld_len fi 64
1352 ret false
1353 end
1354 off = get g_fld_off fi
1355 base = mul idx F_SHA
1356 for i ulong 64
1357 c = get g_line add off i
1358 if not is_hex_low c
1359 ret false
1360 end
1361 put g_c_sha add base i c
1362 end
1363 ret true
1364end
1365
1366fn load_cat_fields_a idx ulong to void
1367 off = get g_fld_off 1
1368 fl = get g_fld_len 1
1369 if gt fl F_NAME
1370 fl = F_NAME
1371 end
1372 for i ulong fl bound 80
1373 put g_c_name add mul idx F_NAME i get g_line add off i
1374 end
1375 put g_c_namel idx fl
1376
1377 off = get g_fld_off 2
1378 fl = get g_fld_len 2
1379 if gt fl F_AUTH
1380 fl = F_AUTH
1381 end
1382 for i ulong fl bound 64
1383 put g_c_auth add mul idx F_AUTH i get g_line add off i
1384 end
1385 put g_c_authl idx fl
1386
1387 off = get g_fld_off 3
1388 fl = get g_fld_len 3
1389 if gt fl F_LIC
1390 fl = F_LIC
1391 end
1392 for i ulong fl bound 32
1393 put g_c_lic add mul idx F_LIC i get g_line add off i
1394 end
1395 put g_c_licl idx fl
1396end
1397
1398fn load_cat_fields_b idx ulong to void
1399 off = get g_fld_off 4
1400 fl = get g_fld_len 4
1401 if gt fl F_TERM
1402 fl = F_TERM
1403 end
1404 for i ulong fl bound 128
1405 put g_c_term add mul idx F_TERM i get g_line add off i
1406 end
1407 put g_c_terml idx fl
1408
1409 off = get g_fld_off 5
1410 fl = get g_fld_len 5
1411 if gt fl F_PROV
1412 fl = F_PROV
1413 end
1414 for i ulong fl bound 160
1415 put g_c_prov add mul idx F_PROV i get g_line add off i
1416 end
1417 put g_c_provl idx fl
1418
1419 off = get g_fld_off 6
1420 fl = get g_fld_len 6
1421 if gt fl F_PT
1422 fl = F_PT
1423 end
1424 for i ulong fl bound 64
1425 put g_c_pt add mul idx F_PT i get g_line add off i
1426 end
1427 put g_c_ptl idx fl
1428end
1429
1430fn load_cat_fields_c idx ulong to void
1431 off = get g_fld_off 15
1432 fl = get g_fld_len 15
1433 if gt fl F_FN
1434 fl = F_FN
1435 end
1436 for i ulong fl bound 128
1437 put g_c_fn add mul idx F_FN i get g_line add off i
1438 end
1439 put g_c_fnl idx fl
1440
1441 off = get g_fld_off 16
1442 fl = get g_fld_len 16
1443 if gt fl F_DESC
1444 fl = F_DESC
1445 end
1446 for i ulong fl bound 256
1447 put g_c_desc add mul idx F_DESC i get g_line add off i
1448 end
1449 put g_c_descl idx fl
1450end
1451
1452fn parse_cat_line to void
1453 n = get g_linel 0
1454 if eq n 0
1455 ret
1456 end
1457 if eq get g_line 0 35
1458 ret
1459 end
1460 if ge get g_scnt 0 HUB_MAX_STRANDS
1461 ret
1462 end
1463 split_line_tabs
1464 if lt get g_fld_cnt 0 17
1465 ret
1466 end
1467 idx = get g_scnt 0
1468 if not load_cat_copy_sha idx 0
1469 ret
1470 end
1471 load_cat_fields_a idx
1472 load_cat_fields_b idx
1473 load_cat_fields_c idx
1474 put g_c_bhash idx parse_line_hex64 7
1475 put g_c_bl idx parse_line_u64 8
1476 put g_c_sl idx parse_line_u64 9
1477 put g_c_npar idx parse_line_u64 10
1478 put g_c_phash idx parse_line_hex64 11
1479 put g_c_sz idx parse_line_u64 12
1480 put g_c_dl idx parse_line_u64 13
1481 put g_c_ts idx parse_line_u64 14
1482 put g_scnt 0 add idx 1
1483end
1484
1485fn load_catalog to void
1486 put g_scnt 0 0
1487 p1_reset
1488 p1_dir
1489 p1_txt t_sub_cat
1490 fd = fopen g_p1 0 get g_p1l 0 0 0
1491 if lt fd 0
1492 ret
1493 end
1494 put g_linel 0 0
1495 for it ulong 10000 bound 10000
1496 r = read int fd g_io 0 HUB_IO_SZ
1497 if le r 0
1498 break
1499 end
1500 for i ulong ulong r bound 65536
1501 c = get g_io i
1502 if eq c 10
1503 parse_cat_line
1504 put g_linel 0 0
1505 else
1506 if ne c 13
1507 ln = get g_linel 0
1508 if lt ln 4095
1509 put g_line ln c
1510 put g_linel 0 add ln 1
1511 end
1512 end
1513 end
1514 end
1515 end
1516 if gt get g_linel 0 0
1517 parse_cat_line
1518 end
1519 x = fclose int fd
1520end
1521
1522fn emit_cat_row_resp idx ulong to void
1523 r_c_sha idx 64
1524 r_b 9
1525 r_c_name_raw idx
1526 r_b 9
1527 r_c_auth_raw idx
1528 r_b 9
1529 r_c_lic_raw idx
1530 r_b 9
1531 r_c_term_raw idx
1532 r_b 9
1533 r_c_prov_raw idx
1534 r_b 9
1535 r_c_pt_raw idx
1536 r_b 9
1537 r_hex16 get g_c_bhash idx
1538 r_b 9
1539 r_u64 get g_c_bl idx
1540 r_b 9
1541 r_u64 get g_c_sl idx
1542 r_b 9
1543 r_u64 get g_c_npar idx
1544 r_b 9
1545 r_hex16 get g_c_phash idx
1546 r_b 9
1547 r_u64 get g_c_sz idx
1548 r_b 9
1549 r_u64 get g_c_dl idx
1550 r_b 9
1551 r_u64 get g_c_ts idx
1552 r_b 9
1553 r_c_fn_raw idx
1554 r_b 9
1555 r_c_desc_raw idx
1556 r_b 10
1557end
1558
1559fn save_catalog to bool
1560 r_reset
1561 r_txt t_cat_hdr1
1562 r_txt t_cat_hdr2
1563 cnt = get g_scnt 0
1564 for i ulong cnt bound 512
1565 emit_cat_row_resp i
1566 end
1567 build_tmp_p1 t_tmp_cat_pre t_ext_tsv
1568 fd = fopen g_p1 0 get g_p1l 0 577 420
1569 if lt fd 0
1570 ret false
1571 end
1572 ok = write_all_resp int fd
1573 if ok
1574 if ne fsync int fd 0
1575 ok = false
1576 end
1577 end
1578 x = fclose int fd
1579 if not ok
1580 u = unlink g_p1 0 get g_p1l 0
1581 ret false
1582 end
1583 p2_reset
1584 p2_dir
1585 p2_txt t_sub_cat
1586 if not atomic_rename_p1_p2
1587 u = unlink g_p1 0 get g_p1l 0
1588 ret false
1589 end
1590 ret true
1591end
1592
1593note ---- Clips catalog persistence (clips.tsv) ----
1594
1595fn load_kcat_str idx ulong to void
1596 off = get g_fld_off 1
1597 fl = get g_fld_len 1
1598 if gt fl F_NAME
1599 fl = F_NAME
1600 end
1601 for i ulong fl bound 80
1602 put g_k_name add mul idx F_NAME i get g_line add off i
1603 end
1604 put g_k_namel idx fl
1605
1606 off = get g_fld_off 2
1607 fl = get g_fld_len 2
1608 if gt fl F_AUTH
1609 fl = F_AUTH
1610 end
1611 for i ulong fl bound 64
1612 put g_k_auth add mul idx F_AUTH i get g_line add off i
1613 end
1614 put g_k_authl idx fl
1615
1616 off = get g_fld_off 3
1617 fl = get g_fld_len 3
1618 if gt fl F_LIC
1619 fl = F_LIC
1620 end
1621 for i ulong fl bound 32
1622 put g_k_lic add mul idx F_LIC i get g_line add off i
1623 end
1624 put g_k_licl idx fl
1625end
1626
1627fn load_kcat_slot idx ulong slot ulong fi_sha ulong fi_w ulong to void
1628 fl = get g_fld_len fi_sha
1629 off = get g_fld_off fi_sha
1630 base = mul add mul idx 4 slot 64
1631 if eq fl 64
1632 for i ulong 64
1633 put g_k_ssha add base i get g_line add off i
1634 end
1635 else
1636 for i ulong 64
1637 put g_k_ssha add base i 48
1638 end
1639 end
1640 put g_k_sw add mul idx 4 slot parse_line_i64 fi_w
1641end
1642
1643fn parse_kcat_line to void
1644 n = get g_linel 0
1645 if eq n 0
1646 ret
1647 end
1648 if eq get g_line 0 35
1649 ret
1650 end
1651 if ge get g_kcnt 0 HUB_MAX_CLIPS
1652 ret
1653 end
1654 split_line_tabs
1655 if lt get g_fld_cnt 0 23
1656 ret
1657 end
1658 if ne get g_fld_len 0 64
1659 ret
1660 end
1661 idx = get g_kcnt 0
1662 off0 = get g_fld_off 0
1663 base0 = mul idx 64
1664 for i ulong 64
1665 c = get g_line add off0 i
1666 if not is_hex_low c
1667 ret
1668 end
1669 put g_k_sha add base0 i c
1670 end
1671 load_kcat_str idx
1672 put g_k_bhash idx parse_line_hex64 4
1673 put g_k_mode idx parse_line_u64 5
1674 put g_k_tau idx parse_line_u64 6
1675 put g_k_splice idx parse_line_u64 7
1676 put g_k_sc idx parse_line_u64 8
1677 put g_k_dl idx parse_line_u64 9
1678 put g_k_ts idx parse_line_u64 10
1679 put g_k_tok idx parse_line_u64 11
1680 put g_k_seed idx parse_line_u64 12
1681 put g_k_steps idx parse_line_u64 13
1682 put g_k_lr idx parse_line_u64 14
1683 load_kcat_slot idx 0 15 16
1684 load_kcat_slot idx 1 17 18
1685 load_kcat_slot idx 2 19 20
1686 load_kcat_slot idx 3 21 22
1687 put g_kcnt 0 add idx 1
1688end
1689
1690fn load_clips_catalog to void
1691 put g_kcnt 0 0
1692 p1_reset
1693 p1_dir
1694 p1_txt t_sub_kcat
1695 fd = fopen g_p1 0 get g_p1l 0 0 0
1696 if lt fd 0
1697 ret
1698 end
1699 put g_linel 0 0
1700 for it ulong 10000 bound 10000
1701 r = read int fd g_io 0 HUB_IO_SZ
1702 if le r 0
1703 break
1704 end
1705 for i ulong ulong r bound 65536
1706 c = get g_io i
1707 if eq c 10
1708 parse_kcat_line
1709 put g_linel 0 0
1710 else
1711 if ne c 13
1712 ln = get g_linel 0
1713 if lt ln 4095
1714 put g_line ln c
1715 put g_linel 0 add ln 1
1716 end
1717 end
1718 end
1719 end
1720 end
1721 if gt get g_linel 0 0
1722 parse_kcat_line
1723 end
1724 x = fclose int fd
1725end
1726
1727fn r_k_sha idx ulong n ulong to void
1728 base = mul idx 64
1729 for i ulong n bound 64
1730 r_b get g_k_sha add base i
1731 end
1732end
1733
1734fn r_k_name_raw idx ulong to void
1735 base = mul idx F_NAME
1736 for i ulong get g_k_namel idx bound 80
1737 r_b get g_k_name add base i
1738 end
1739end
1740
1741fn r_k_name_esc idx ulong to void
1742 base = mul idx F_NAME
1743 for i ulong get g_k_namel idx bound 80
1744 r_esc_b get g_k_name add base i
1745 end
1746end
1747
1748fn r_k_name_json idx ulong to void
1749 base = mul idx F_NAME
1750 for i ulong get g_k_namel idx bound 80
1751 r_jesc_b get g_k_name add base i
1752 end
1753end
1754
1755fn r_k_auth_raw idx ulong to void
1756 base = mul idx F_AUTH
1757 for i ulong get g_k_authl idx bound 64
1758 r_b get g_k_auth add base i
1759 end
1760end
1761
1762fn r_k_auth_esc idx ulong to void
1763 base = mul idx F_AUTH
1764 for i ulong get g_k_authl idx bound 64
1765 r_esc_b get g_k_auth add base i
1766 end
1767end
1768
1769fn r_k_auth_json idx ulong to void
1770 base = mul idx F_AUTH
1771 for i ulong get g_k_authl idx bound 64
1772 r_jesc_b get g_k_auth add base i
1773 end
1774end
1775
1776fn r_k_lic_raw idx ulong to void
1777 base = mul idx F_LIC
1778 for i ulong get g_k_licl idx bound 32
1779 r_b get g_k_lic add base i
1780 end
1781end
1782
1783fn r_k_lic_esc idx ulong to void
1784 base = mul idx F_LIC
1785 for i ulong get g_k_licl idx bound 32
1786 r_esc_b get g_k_lic add base i
1787 end
1788end
1789
1790fn r_k_lic_json idx ulong to void
1791 base = mul idx F_LIC
1792 for i ulong get g_k_licl idx bound 32
1793 r_jesc_b get g_k_lic add base i
1794 end
1795end
1796
1797fn emit_kcat_row_resp idx ulong to void
1798 r_k_sha idx 64
1799 r_b 9
1800 r_k_name_raw idx
1801 r_b 9
1802 r_k_auth_raw idx
1803 r_b 9
1804 r_k_lic_raw idx
1805 r_b 9
1806 r_hex16 get g_k_bhash idx
1807 r_b 9
1808 r_u64 get g_k_mode idx
1809 r_b 9
1810 r_u64 get g_k_tau idx
1811 r_b 9
1812 r_u64 get g_k_splice idx
1813 r_b 9
1814 r_u64 get g_k_sc idx
1815 r_b 9
1816 r_u64 get g_k_dl idx
1817 r_b 9
1818 r_u64 get g_k_ts idx
1819 r_b 9
1820 r_u64 get g_k_tok idx
1821 r_b 9
1822 r_u64 get g_k_seed idx
1823 r_b 9
1824 r_u64 get g_k_steps idx
1825 r_b 9
1826 r_u64 get g_k_lr idx
1827 sc = get g_k_sc idx
1828 for slot ulong 4
1829 r_b 9
1830 if lt slot sc
1831 base = mul add mul idx 4 slot 64
1832 for j ulong 64
1833 r_b get g_k_ssha add base j
1834 end
1835 end
1836 r_b 9
1837 if lt slot sc
1838 r_i64 get g_k_sw add mul idx 4 slot
1839 else
1840 r_b 48
1841 end
1842 end
1843 r_b 10
1844end
1845
1846fn save_clips_catalog to bool
1847 r_reset
1848 r_txt t_kcat_hdr1
1849 r_txt t_kcat_hdr2
1850 cnt = get g_kcnt 0
1851 for i ulong cnt bound 256
1852 emit_kcat_row_resp i
1853 end
1854 build_tmp_p1 t_tmp_kcat_pre t_ext_tsv
1855 fd = fopen g_p1 0 get g_p1l 0 577 420
1856 if lt fd 0
1857 ret false
1858 end
1859 ok = write_all_resp int fd
1860 if ok
1861 if ne fsync int fd 0
1862 ok = false
1863 end
1864 end
1865 x = fclose int fd
1866 if not ok
1867 u = unlink g_p1 0 get g_p1l 0
1868 ret false
1869 end
1870 p2_reset
1871 p2_dir
1872 p2_txt t_sub_kcat
1873 if not atomic_rename_p1_p2
1874 u = unlink g_p1 0 get g_p1l 0
1875 ret false
1876 end
1877 ret true
1878end
1879
1880note ---- Catalog lookup and trunk compatibility helpers ----
1881
1882fn sha_eq_cat_u idx ulong to bool
1883 base = mul idx F_SHA
1884 for i ulong 64
1885 if ne get g_c_sha add base i get g_u_sha i
1886 ret false
1887 end
1888 end
1889 ret true
1890end
1891
1892fn find_strand_by_u_sha to long
1893 cnt = get g_scnt 0
1894 for i ulong cnt bound 512
1895 if sha_eq_cat_u i
1896 ret long i
1897 end
1898 end
1899 ret -1
1900end
1901
1902fn sha_eq_cat_pt idx ulong to bool
1903 if ne get g_u_ptl 0 64
1904 ret false
1905 end
1906 base = mul idx F_SHA
1907 for i ulong 64
1908 if ne get g_c_sha add base i get g_u_pt i
1909 ret false
1910 end
1911 end
1912 ret true
1913end
1914
1915fn find_strand_by_u_pt to long
1916 if ne get g_u_ptl 0 64
1917 ret -1
1918 end
1919 cnt = get g_scnt 0
1920 for i ulong cnt bound 512
1921 if sha_eq_cat_pt i
1922 ret long i
1923 end
1924 end
1925 ret -1
1926end
1927
1928fn sha_eq_cat_dpath idx ulong to bool
1929 base = mul idx F_SHA
1930 for i ulong 64
1931 if ne get g_c_sha add base i get g_dpath i
1932 ret false
1933 end
1934 end
1935 ret true
1936end
1937
1938fn find_strand_by_dpath to long
1939 cnt = get g_scnt 0
1940 for i ulong cnt bound 512
1941 if sha_eq_cat_dpath i
1942 ret long i
1943 end
1944 end
1945 ret -1
1946end
1947
1948fn sha_eq_cat_eslot idx ulong slot ulong to bool
1949 base = mul idx F_SHA
1950 eb = mul slot 64
1951 for i ulong 64
1952 if ne get g_c_sha add base i get g_e_ssha add eb i
1953 ret false
1954 end
1955 end
1956 ret true
1957end
1958
1959fn find_strand_by_eslot slot ulong to long
1960 cnt = get g_scnt 0
1961 for i ulong cnt bound 512
1962 if sha_eq_cat_eslot i slot
1963 ret long i
1964 end
1965 end
1966 ret -1
1967end
1968
1969fn sha_eq_kcat_dpath idx ulong to bool
1970 base = mul idx 64
1971 for i ulong 64
1972 if ne get g_k_sha add base i get g_dpath i
1973 ret false
1974 end
1975 end
1976 ret true
1977end
1978
1979fn find_clip_by_dpath to long
1980 cnt = get g_kcnt 0
1981 for i ulong cnt bound 256
1982 if sha_eq_kcat_dpath i
1983 ret long i
1984 end
1985 end
1986 ret -1
1987end
1988
1989fn sha_eq_kcat_esha idx ulong to bool
1990 base = mul idx 64
1991 for i ulong 64
1992 if ne get g_k_sha add base i get g_e_sha i
1993 ret false
1994 end
1995 end
1996 ret true
1997end
1998
1999fn find_clip_by_esha to long
2000 cnt = get g_kcnt 0
2001 for i ulong cnt bound 256
2002 if sha_eq_kcat_esha i
2003 ret long i
2004 end
2005 end
2006 ret -1
2007end
2008
2009fn strand_matches_trunk idx ulong to bool
2010 ql = get g_q_trunkl 0
2011 if eq ql 0
2012 ret true
2013 end
2014 fmt_hex16 get g_c_bhash idx
2015 if le ql 16
2016 ok1 = true
2017 for i ulong ql bound 16
2018 if ne to_low get g_q_trunk i get g_hex16 i
2019 ok1 = false
2020 break
2021 end
2022 end
2023 if ok1
2024 ret true
2025 end
2026 end
2027 ptl = get g_c_ptl idx
2028 if ge ptl ql
2029 base = mul idx F_PT
2030 ok2 = true
2031 for i ulong ql bound 64
2032 if ne to_low get g_q_trunk i to_low get g_c_pt add base i
2033 ok2 = false
2034 break
2035 end
2036 end
2037 if ok2
2038 ret true
2039 end
2040 end
2041 ret false
2042end
2043
2044fn strand_matches_filters idx ulong to bool
2045 if not strand_matches_trunk idx
2046 ret false
2047 end
2048 ll = get g_q_licl 0
2049 if eq ll 0
2050 ret true
2051 end
2052 if ne ll get g_c_licl idx
2053 ret false
2054 end
2055 base = mul idx F_LIC
2056 for i ulong ll bound 32
2057 if ne to_low get g_q_lic i to_low get g_c_lic add base i
2058 ret false
2059 end
2060 end
2061 ret true
2062end
2063
2064fn collect_distinct_trunks to ulong
2065 put g_seen_cnt 0 0
2066 cnt = get g_scnt 0
2067 for i ulong cnt bound 512
2068 bh = get g_c_bhash i
2069 sc = get g_seen_cnt 0
2070 found = false
2071 for j ulong sc bound 512
2072 if eq get g_seen_tr j bh
2073 found = true
2074 break
2075 end
2076 end
2077 if not found
2078 if lt sc 512
2079 put g_seen_tr sc bh
2080 put g_seen_cnt 0 add sc 1
2081 end
2082 end
2083 end
2084 ret get g_seen_cnt 0
2085end
2086
2087fn total_downloads to ulong
2088 tot = ulong 0
2089 cnt = get g_scnt 0
2090 for i ulong cnt bound 512
2091 tot = add tot get g_c_dl i
2092 end
2093 ret tot
2094end
2095
2096fn find_partner_strand idx ulong to long
2097 bh = get g_c_bhash idx
2098 cnt = get g_scnt 0
2099 for i ulong cnt bound 512
2100 if ne i idx
2101 if eq get g_c_bhash i bh
2102 ret long i
2103 end
2104 end
2105 end
2106 ret -1
2107end
2108
2109note ---- HTTP request parsing, query decoding, and streaming body ----
2110
2111fn req_starts off ulong pat view uchar * to bool
2112 n = get g_reql 0
2113 if gt add off len pat n
2114 ret false
2115 end
2116 for i ulong len pat bound 128
2117 if ne get g_req add off i get pat i
2118 ret false
2119 end
2120 end
2121 ret true
2122end
2123
2124fn req_istarts off ulong pat view uchar * to bool
2125 n = get g_reql 0
2126 if gt add off len pat n
2127 ret false
2128 end
2129 for i ulong len pat bound 128
2130 if ne to_low get g_req add off i get pat i
2131 ret false
2132 end
2133 end
2134 ret true
2135end
2136
2137fn req_range_eq a ulong b ulong pat view uchar * to bool
2138 if ne sub b a len pat
2139 ret false
2140 end
2141 for i ulong len pat bound 128
2142 if ne get g_req add a i get pat i
2143 ret false
2144 end
2145 end
2146 ret true
2147end
2148
2149fn req_range_starts a ulong b ulong pat view uchar * to bool
2150 if lt sub b a len pat
2151 ret false
2152 end
2153 for i ulong len pat bound 128
2154 if ne get g_req add a i get pat i
2155 ret false
2156 end
2157 end
2158 ret true
2159end
2160
2161fn find_hdr_end to ulong
2162 n = get g_reql 0
2163 if lt n 4
2164 ret 0
2165 end
2166 for i ulong sub n 3 bound 32768
2167 if eq get g_req i 13
2168 if eq get g_req add i 1 10
2169 if eq get g_req add i 2 13
2170 if eq get g_req add i 3 10
2171 ret add i 4
2172 end
2173 end
2174 end
2175 end
2176 end
2177 ret 0
2178end
2179
2180fn read_http_headers fd int to bool
2181 put g_reql 0 0
2182 dl = add long monons HUB_IO_TMO
2183 for it ulong 20000 bound 20000
2184 h = find_hdr_end
2185 if gt h 0
2186 put g_rq_st RQ_HEND h
2187 ret true
2188 end
2189 n = get g_reql 0
2190 if ge n HUB_MAX_HDR
2191 ret false
2192 end
2193 left = sub dl long monons
2194 if le left 0
2195 ret false
2196 end
2197 w = waitio fd 1 left
2198 if le w 0
2199 if eq w -4
2200 continue
2201 end
2202 ret false
2203 end
2204 r = recv fd g_req n sub HUB_MAX_HDR n 0
2205 if lt r 0
2206 if or eq r -4 eq r -11
2207 continue
2208 end
2209 ret false
2210 end
2211 if eq r 0
2212 ret false
2213 end
2214 put g_reql 0 add n ulong r
2215 end
2216 ret false
2217end
2218
2219fn reset_upload_meta to void
2220 put g_u_namel 0 0
2221 put g_u_authl 0 0
2222 put g_u_licl 0 0
2223 put g_u_terml 0 0
2224 put g_u_provl 0 0
2225 put g_u_ptl 0 0
2226 put g_u_fnl 0 0
2227 put g_u_descl 0 0
2228 put g_u_expl 0 0
2229 put g_u_bndl 0 0
2230 put g_u_deliml 0 0
2231 for i ulong 16
2232 put g_u_st i 0
2233 end
2234end
2235
2236fn parse_hdr_val_span line_s ulong line_e ulong plen ulong to ulong
2237 j = add line_s plen
2238 while lt j line_e bound 16384
2239 c = get g_req j
2240 if and ne c 32 ne c 9
2241 break
2242 end
2243 j = add j 1
2244 end
2245 ret j
2246end
2247
2248fn trim_hdr_end lo ulong hi ulong to ulong
2249 e = hi
2250 while gt e lo bound 16384
2251 c = get g_req sub e 1
2252 if or eq c 32 or eq c 9 or eq c 13 eq c 10
2253 e = sub e 1
2254 else
2255 break
2256 end
2257 end
2258 ret e
2259end
2260
2261fn copy_req_san_u fld ulong lo ulong hi ulong to void
2262 e = trim_hdr_end lo hi
2263 n = sub e lo
2264 if and eq fld 6 gt n F_PT
2265 put g_u_st US_BAD_PT 1
2266 ret
2267 end
2268 if and eq fld 8 gt n F_FN
2269 put g_u_st US_BAD_FN 1
2270 ret
2271 end
2272 if and eq fld 9 gt n 64
2273 put g_u_st US_BAD_EXP 1
2274 ret
2275 end
2276 if gt n 256
2277 n = 256
2278 end
2279 for i ulong n bound 256
2280 c = get g_req add lo i
2281 if or lt c 32 eq c 127
2282 c = 32
2283 end
2284 if eq fld 1
2285 k = get g_u_namel 0
2286 if lt k F_NAME
2287 put g_u_name k c
2288 put g_u_namel 0 add k 1
2289 end
2290 end
2291 if eq fld 2
2292 k = get g_u_authl 0
2293 if lt k F_AUTH
2294 put g_u_auth k c
2295 put g_u_authl 0 add k 1
2296 end
2297 end
2298 if eq fld 3
2299 k = get g_u_licl 0
2300 if lt k F_LIC
2301 put g_u_lic k c
2302 put g_u_licl 0 add k 1
2303 end
2304 end
2305 if eq fld 4
2306 k = get g_u_terml 0
2307 if lt k F_TERM
2308 put g_u_term k c
2309 put g_u_terml 0 add k 1
2310 end
2311 end
2312 if eq fld 5
2313 k = get g_u_provl 0
2314 if lt k F_PROV
2315 put g_u_prov k c
2316 put g_u_provl 0 add k 1
2317 end
2318 end
2319 if eq fld 6
2320 k = get g_u_ptl 0
2321 if lt k F_PT
2322 put g_u_pt k to_low c
2323 put g_u_ptl 0 add k 1
2324 end
2325 end
2326 if eq fld 7
2327 k = get g_u_descl 0
2328 if lt k F_DESC
2329 put g_u_desc k c
2330 put g_u_descl 0 add k 1
2331 end
2332 end
2333 if eq fld 8
2334 k = get g_u_fnl 0
2335 if lt k F_FN
2336 put g_u_fn k c
2337 put g_u_fnl 0 add k 1
2338 end
2339 end
2340 if eq fld 9
2341 k = get g_u_expl 0
2342 if lt k 64
2343 put g_u_exp k to_low c
2344 put g_u_expl 0 add k 1
2345 end
2346 end
2347 end
2348end
2349
2350fn is_bnd_sep c uchar to bool
2351 if eq c 59
2352 ret true
2353 end
2354 if eq c 32
2355 ret true
2356 end
2357 if eq c 9
2358 ret true
2359 end
2360 if eq c 13
2361 ret true
2362 end
2363 ret eq c 10
2364end
2365
2366fn copy_boundary_at j0 ulong hi ulong to bool
2367 j = j0
2368 quoted = false
2369 closed = false
2370 if lt j hi
2371 if eq get g_req j 34
2372 quoted = true
2373 j = add j 1
2374 end
2375 end
2376 while lt j hi bound 96
2377 c = get g_req j
2378 if quoted
2379 if eq c 34
2380 closed = true
2381 break
2382 end
2383 else
2384 if is_bnd_sep c
2385 break
2386 end
2387 end
2388 bl = get g_u_bndl 0
2389 if ge bl 70
2390 ret false
2391 end
2392 put g_u_bnd bl c
2393 put g_u_bndl 0 add bl 1
2394 j = add j 1
2395 end
2396 if and quoted not closed
2397 ret false
2398 end
2399 ret gt get g_u_bndl 0 0
2400end
2401
2402fn parse_boundary_from_ct lo ulong hi ulong to bool
2403 put g_u_bndl 0 0
2404 i = lo
2405 while lt i hi bound 16384
2406 if req_istarts i t_bnd_eq
2407 ret copy_boundary_at add i len t_bnd_eq hi
2408 end
2409 i = add i 1
2410 end
2411 ret false
2412end
2413
2414fn parse_cl_value lo ulong hi ulong to bool
2415 e = trim_hdr_end lo hi
2416 if ge lo e
2417 ret false
2418 end
2419 n = sub e lo
2420 if gt n 20
2421 ret false
2422 end
2423 val = ulong 0
2424 for i ulong n bound 20
2425 c = get g_req add lo i
2426 if or lt c 48 gt c 57
2427 ret false
2428 end
2429 if gt val 100000000000
2430 ret false
2431 end
2432 val = add mul val 10 ulong sub c 48
2433 end
2434 if ne get g_rq_st RQ_HAS_CL 0
2435 if ne val get g_rq_st RQ_CLEN
2436 ret false
2437 end
2438 end
2439 put g_rq_st RQ_HAS_CL 1
2440 put g_rq_st RQ_CLEN val
2441 ret true
2442end
2443
2444fn parse_x_strand_hdr ls ulong le_pos ulong to void
2445 if req_istarts ls t_h_xname
2446 put g_u_namel 0 0
2447 copy_req_san_u 1 parse_hdr_val_span ls le_pos len t_h_xname le_pos
2448 ret
2449 end
2450 if req_istarts ls t_h_xauth
2451 put g_u_authl 0 0
2452 copy_req_san_u 2 parse_hdr_val_span ls le_pos len t_h_xauth le_pos
2453 ret
2454 end
2455 if req_istarts ls t_h_xlic
2456 put g_u_licl 0 0
2457 copy_req_san_u 3 parse_hdr_val_span ls le_pos len t_h_xlic le_pos
2458 ret
2459 end
2460 if req_istarts ls t_h_xterm
2461 put g_u_terml 0 0
2462 copy_req_san_u 4 parse_hdr_val_span ls le_pos len t_h_xterm le_pos
2463 ret
2464 end
2465 if req_istarts ls t_h_xprov
2466 put g_u_provl 0 0
2467 copy_req_san_u 5 parse_hdr_val_span ls le_pos len t_h_xprov le_pos
2468 ret
2469 end
2470 if req_istarts ls t_h_xpt
2471 put g_u_ptl 0 0
2472 copy_req_san_u 6 parse_hdr_val_span ls le_pos len t_h_xpt le_pos
2473 ret
2474 end
2475 if req_istarts ls t_h_xdesc
2476 put g_u_descl 0 0
2477 copy_req_san_u 7 parse_hdr_val_span ls le_pos len t_h_xdesc le_pos
2478 ret
2479 end
2480 if req_istarts ls t_h_xfn
2481 put g_u_fnl 0 0
2482 copy_req_san_u 8 parse_hdr_val_span ls le_pos len t_h_xfn le_pos
2483 ret
2484 end
2485 if req_istarts ls t_h_xexp
2486 put g_u_expl 0 0
2487 copy_req_san_u 9 parse_hdr_val_span ls le_pos len t_h_xexp le_pos
2488 ret
2489 end
2490end
2491
2492fn parse_one_hdr_line ls ulong le_pos ulong to void
2493 if req_istarts ls t_h_te
2494 put g_rq_st RQ_BAD_HDR 1
2495 ret
2496 end
2497 if req_istarts ls t_h_expect
2498 put g_rq_st RQ_EXPECT 1
2499 ret
2500 end
2501 if req_istarts ls t_h_cl
2502 v0 = parse_hdr_val_span ls le_pos len t_h_cl
2503 if not parse_cl_value v0 le_pos
2504 put g_rq_st RQ_BAD_HDR 1
2505 end
2506 ret
2507 end
2508 if req_istarts ls t_h_ct
2509 v0 = parse_hdr_val_span ls le_pos len t_h_ct
2510 if req_istarts v0 t_ct_mp
2511 put g_rq_st RQ_CTYPE 1
2512 if not parse_boundary_from_ct v0 le_pos
2513 put g_rq_st RQ_BAD_HDR 1
2514 end
2515 else
2516 if req_istarts v0 t_ct_form
2517 put g_rq_st RQ_CTYPE 2
2518 else
2519 if req_istarts v0 t_ct_oct
2520 put g_rq_st RQ_CTYPE 3
2521 else
2522 if req_istarts v0 t_ct_plain
2523 put g_rq_st RQ_CTYPE 4
2524 end
2525 end
2526 end
2527 end
2528 ret
2529 end
2530 parse_x_strand_hdr ls le_pos
2531end
2532
2533fn parse_req_headers to bool
2534 hend = get g_rq_st RQ_HEND
2535 for i ulong 16
2536 if ne i RQ_HEND
2537 put g_rq_st i 0
2538 end
2539 end
2540 reset_upload_meta
2541 p0 = ulong 0
2542 if req_starts 0 t_m_get
2543 put g_rq_st RQ_METH 1
2544 p0 = 4
2545 else
2546 if req_starts 0 t_m_head
2547 put g_rq_st RQ_METH 2
2548 p0 = 5
2549 else
2550 if req_starts 0 t_m_post
2551 put g_rq_st RQ_METH 3
2552 p0 = 5
2553 else
2554 ret false
2555 end
2556 end
2557 end
2558 p1 = p0
2559 q0 = ulong 0
2560 while lt p1 hend bound 16384
2561 c = get g_req p1
2562 if or eq c 32 or eq c 13 eq c 10
2563 break
2564 end
2565 if and eq c 63 eq q0 0
2566 q0 = add p1 1
2567 end
2568 p1 = add p1 1
2569 end
2570 if gt sub p1 p0 4096
2571 put g_rq_st RQ_BAD_HDR 1
2572 ret true
2573 end
2574 if eq q0 0
2575 put g_rq_st RQ_P0 p0
2576 put g_rq_st RQ_P1 p1
2577 put g_rq_st RQ_Q0 0
2578 put g_rq_st RQ_Q1 0
2579 else
2580 put g_rq_st RQ_P0 p0
2581 put g_rq_st RQ_P1 sub q0 1
2582 put g_rq_st RQ_Q0 q0
2583 put g_rq_st RQ_Q1 p1
2584 end
2585 i = p1
2586 while lt i hend bound 16384
2587 c = get g_req i
2588 i = add i 1
2589 if eq c 10
2590 break
2591 end
2592 end
2593 while lt i hend bound 16384
2594 ls = i
2595 while lt i hend bound 16384
2596 c = get g_req i
2597 i = add i 1
2598 if eq c 10
2599 break
2600 end
2601 end
2602 le_pos = i
2603 if gt le_pos ls
2604 if eq get g_req sub le_pos 1 10
2605 le_pos = sub le_pos 1
2606 end
2607 end
2608 if gt le_pos ls
2609 if eq get g_req sub le_pos 1 13
2610 le_pos = sub le_pos 1
2611 end
2612 end
2613 if eq ls le_pos
2614 break
2615 end
2616 parse_one_hdr_line ls le_pos
2617 end
2618 ret true
2619end
2620
2621note ---- Query string parsing ----
2622
2623fn qkey_eq pat view uchar * to bool
2624 if ne get g_qkeyl 0 len pat
2625 ret false
2626 end
2627 for i ulong len pat bound 64
2628 if ne get g_qkey i get pat i
2629 ret false
2630 end
2631 end
2632 ret true
2633end
2634
2635fn apply_query_kv to void
2636 vl = get g_qvall 0
2637 if qkey_eq t_f_trunk
2638 if gt vl 64
2639 vl = 64
2640 end
2641 for i ulong vl bound 64
2642 put g_q_trunk i to_low get g_qval i
2643 end
2644 put g_q_trunkl 0 vl
2645 ret
2646 end
2647 if qkey_eq t_f_lic
2648 if gt vl F_LIC
2649 vl = F_LIC
2650 end
2651 for i ulong vl bound 32
2652 put g_q_lic i get g_qval i
2653 end
2654 put g_q_licl 0 vl
2655 ret
2656 end
2657 if qkey_eq t_f_pt
2658 if gt vl 64
2659 vl = 64
2660 end
2661 for i ulong vl bound 64
2662 put g_q_pt i to_low get g_qval i
2663 end
2664 put g_q_ptl 0 vl
2665 ret
2666 end
2667 if qkey_eq t_f_clip
2668 if gt vl 64
2669 vl = 64
2670 end
2671 for i ulong vl bound 64
2672 put g_q_clip i to_low get g_qval i
2673 end
2674 put g_q_clipl 0 vl
2675 ret
2676 end
2677 if qkey_eq t_f_s1
2678 if gt vl 64
2679 vl = 64
2680 end
2681 for i ulong vl bound 64
2682 put g_q_s1 i to_low get g_qval i
2683 end
2684 put g_q_s1l 0 vl
2685 ret
2686 end
2687 if qkey_eq t_f_s2
2688 if gt vl 64
2689 vl = 64
2690 end
2691 for i ulong vl bound 64
2692 put g_q_s2 i to_low get g_qval i
2693 end
2694 put g_q_s2l 0 vl
2695 ret
2696 end
2697end
2698
2699fn parse_query_string to void
2700 put g_q_trunkl 0 0
2701 put g_q_licl 0 0
2702 put g_q_ptl 0 0
2703 put g_q_clipl 0 0
2704 put g_q_s1l 0 0
2705 put g_q_s2l 0 0
2706 q0 = get g_rq_st RQ_Q0
2707 q1 = get g_rq_st RQ_Q1
2708 if or ge q0 q1 gt sub q1 q0 4096
2709 ret
2710 end
2711 i = q0
2712 while lt i q1 bound 4097
2713 put g_qkeyl 0 0
2714 put g_qvall 0 0
2715 in_val = false
2716 while lt i q1 bound 4097
2717 c = get g_req i
2718 if eq c 38
2719 i = add i 1
2720 break
2721 end
2722 if and not in_val eq c 61
2723 in_val = true
2724 i = add i 1
2725 continue
2726 end
2727 b = c
2728 if eq c 43
2729 b = 32
2730 i = add i 1
2731 else
2732 if eq c 37
2733 if le add i 3 q1
2734 h1 = hex_val get g_req add i 1
2735 h2 = hex_val get g_req add i 2
2736 if and ge h1 0 ge h2 0
2737 b = uchar add mul h1 16 h2
2738 i = add i 3
2739 else
2740 i = add i 1
2741 end
2742 else
2743 i = add i 1
2744 end
2745 else
2746 i = add i 1
2747 end
2748 end
2749 if in_val
2750 vl = get g_qvall 0
2751 if lt vl 256
2752 put g_qval vl b
2753 put g_qvall 0 add vl 1
2754 end
2755 else
2756 kl = get g_qkeyl 0
2757 if lt kl 64
2758 put g_qkey kl b
2759 put g_qkeyl 0 add kl 1
2760 end
2761 end
2762 end
2763 if gt get g_qkeyl 0 0
2764 apply_query_kv
2765 end
2766 end
2767end
2768
2769note ---- Stream HTTP body to staging file ----
2770
2771fn write_exact_fd fd int off ulong n ulong use_req bool to bool
2772 p = ulong 0
2773 for it ulong 200000 bound 200000
2774 if ge p n
2775 ret true
2776 end
2777 w = long 0
2778 if use_req
2779 w = write fd g_req add off p sub n p
2780 else
2781 w = write fd g_io add off p sub n p
2782 end
2783 if le w 0
2784 if eq w -4
2785 continue
2786 end
2787 ret false
2788 end
2789 p = add p ulong w
2790 end
2791 ret ge p n
2792end
2793
2794fn flush_req_tail rawfd int hend ulong clen ulong to long
2795 avail = ulong 0
2796 reql = get g_reql 0
2797 if gt reql hend
2798 avail = sub reql hend
2799 end
2800 if gt avail clen
2801 avail = clen
2802 end
2803 if gt avail 0
2804 if not write_exact_fd rawfd hend avail true
2805 ret -1
2806 end
2807 end
2808 ret long avail
2809end
2810
2811fn recv_io_step sockfd int want ulong dl long to long
2812 left = sub dl long monons
2813 if le left 0
2814 ret -2
2815 end
2816 w = waitio sockfd 1 left
2817 if le w 0
2818 if eq w -4
2819 ret -1
2820 end
2821 ret -2
2822 end
2823 r = recv sockfd g_io 0 want 0
2824 if lt r 0
2825 if or eq r -4 eq r -11
2826 ret -1
2827 end
2828 ret -2
2829 end
2830 if eq r 0
2831 ret -2
2832 end
2833 ret r
2834end
2835
2836fn stream_body_to_file sockfd int hend ulong clen ulong rawfd int to bool
2837 fl = flush_req_tail rawfd hend clen
2838 if lt fl 0
2839 ret false
2840 end
2841 done = ulong fl
2842 dl = add long monons HUB_UP_TMO
2843 for it ulong 100000000 bound 100000000
2844 if ge done clen
2845 ret true
2846 end
2847 want = sub clen done
2848 if gt want HUB_IO_SZ
2849 want = HUB_IO_SZ
2850 end
2851 r = recv_io_step sockfd want dl
2852 if eq r -1
2853 continue
2854 end
2855 if le r 0
2856 ret false
2857 end
2858 if not write_exact_fd rawfd 0 ulong r false
2859 ret false
2860 end
2861 done = add done ulong r
2862 end
2863 ret eq done clen
2864end
2865
2866note ---- Overlap-safe multipart/form-data parser over staging file ----
2867
2868fn pread_exact fd int pos ulong n ulong to bool
2869 p = ulong 0
2870 for it ulong 200000 bound 200000
2871 if ge p n
2872 ret true
2873 end
2874 r = pread fd g_io p sub n p long add pos p
2875 if le r 0
2876 if eq r -4
2877 continue
2878 end
2879 ret false
2880 end
2881 p = add p ulong r
2882 end
2883 ret ge p n
2884end
2885
2886fn build_delim with_crlf bool to void
2887 p = ulong 0
2888 if with_crlf
2889 put g_u_delim 0 13
2890 put g_u_delim 1 10
2891 p = 2
2892 end
2893 put g_u_delim p 45
2894 put g_u_delim add p 1 45
2895 p = add p 2
2896 bl = get g_u_bndl 0
2897 for i ulong bl bound 72
2898 put g_u_delim add p i get g_u_bnd i
2899 end
2900 put g_u_deliml 0 add p bl
2901end
2902
2903fn io_match_delim off ulong dlen ulong to bool
2904 for i ulong dlen bound 104
2905 if ne get g_io add off i get g_u_delim i
2906 ret false
2907 end
2908 end
2909 ret true
2910end
2911
2912fn find_delim_in_file fd int start ulong flen ulong to long
2913 dlen = get g_u_deliml 0
2914 if or eq dlen 0 gt add start dlen flen
2915 ret -1
2916 end
2917 pos = start
2918 for it ulong 200000 bound 200000
2919 if gt add pos dlen flen
2920 ret -1
2921 end
2922 want = sub flen pos
2923 if gt want HUB_IO_SZ
2924 want = HUB_IO_SZ
2925 end
2926 if not pread_exact fd pos want
2927 ret -1
2928 end
2929 lim = add sub want dlen 1
2930 for i ulong lim bound 65536
2931 if eq get g_io i get g_u_delim 0
2932 if io_match_delim i dlen
2933 ret long add pos i
2934 end
2935 end
2936 end
2937 if ge add pos want flen
2938 ret -1
2939 end
2940 pos = add pos sub want sub dlen 1
2941 end
2942 ret -1
2943end
2944
2945fn io_istarts off ulong lim ulong pat view uchar * to bool
2946 if gt add off len pat lim
2947 ret false
2948 end
2949 for i ulong len pat bound 64
2950 if ne to_low get g_io add off i get pat i
2951 ret false
2952 end
2953 end
2954 ret true
2955end
2956
2957fn is_disp_attr_start i ulong s ulong to bool
2958 if eq i s
2959 ret true
2960 end
2961 p = get g_io sub i 1
2962 if or eq p 59 or eq p 32 eq p 9
2963 ret true
2964 end
2965 ret false
2966end
2967
2968fn parse_part_disp_attrs s ulong e ulong to void
2969 i = s
2970 while lt i e bound 4096
2971 if is_disp_attr_start i s
2972 if io_istarts i e t_p_fn
2973 j = add i len t_p_fn
2974 put g_u_has_pfn 0 1
2975 put g_u_pfnl 0 0
2976 while lt j e bound 4096
2977 c = get g_io j
2978 if eq c 34
2979 break
2980 end
2981 k = get g_u_pfnl 0
2982 if lt k F_FN
2983 put g_u_pfn k c
2984 put g_u_pfnl 0 add k 1
2985 else
2986 put g_u_pfnl 0 add F_FN 1
2987 end
2988 j = add j 1
2989 end
2990 i = add j 1
2991 continue
2992 end
2993 if io_istarts i e t_p_name
2994 j = add i len t_p_name
2995 put g_u_pnamel 0 0
2996 while lt j e bound 4096
2997 c = get g_io j
2998 if eq c 34
2999 break
3000 end
3001 k = get g_u_pnamel 0
3002 if lt k 64
3003 put g_u_pname k c
3004 put g_u_pnamel 0 add k 1
3005 else
3006 put g_u_pnamel 0 65
3007 end
3008 j = add j 1
3009 end
3010 i = add j 1
3011 continue
3012 end
3013 end
3014 i = add i 1
3015 end
3016end
3017
3018fn parse_part_headers fd int pstart ulong flen ulong to long
3019 put g_u_pnamel 0 0
3020 put g_u_pfnl 0 0
3021 put g_u_has_pfn 0 0
3022 want = sub flen pstart
3023 if gt want 4096
3024 want = 4096
3025 end
3026 if lt want 4
3027 ret -1
3028 end
3029 if not pread_exact fd pstart want
3030 ret -1
3031 end
3032 hend = ulong 0
3033 for i ulong sub want 3 bound 4096
3034 if eq get g_io i 13
3035 if eq get g_io add i 1 10
3036 if eq get g_io add i 2 13
3037 if eq get g_io add i 3 10
3038 hend = add i 4
3039 break
3040 end
3041 end
3042 end
3043 end
3044 end
3045 if eq hend 0
3046 ret -1
3047 end
3048 i = ulong 0
3049 while lt i hend bound 4096
3050 ls = i
3051 while lt i hend bound 4096
3052 c = get g_io i
3053 i = add i 1
3054 if eq c 10
3055 break
3056 end
3057 end
3058 if io_istarts ls i t_cdisp
3059 parse_part_disp_attrs add ls len t_cdisp i
3060 end
3061 end
3062 ret long add pstart hend
3063end
3064
3065fn pname_eq pat view uchar * to bool
3066 if ne get g_u_pnamel 0 len pat
3067 ret false
3068 end
3069 for i ulong len pat bound 64
3070 if ne get g_u_pname i get pat i
3071 ret false
3072 end
3073 end
3074 ret true
3075end
3076
3077fn validate_upload_filename to bool
3078 n = get g_u_fnl 0
3079 if eq n 0
3080 ret true
3081 end
3082 if gt n F_FN
3083 ret false
3084 end
3085 if eq get g_u_fn 0 46
3086 ret false
3087 end
3088 for i ulong n bound 128
3089 c = get g_u_fn i
3090 if not is_safe_fn_char c
3091 ret false
3092 end
3093 if eq c 46
3094 if lt add i 1 n
3095 if eq get g_u_fn add i 1 46
3096 ret false
3097 end
3098 end
3099 end
3100 end
3101 ret true
3102end
3103
3104fn copy_io_san_u fld ulong n ulong to void
3105 for i ulong n bound 256
3106 c = get g_io i
3107 if or lt c 32 eq c 127
3108 c = 32
3109 end
3110 if eq fld 1
3111 k = get g_u_namel 0
3112 if lt k F_NAME
3113 put g_u_name k c
3114 put g_u_namel 0 add k 1
3115 end
3116 end
3117 if eq fld 2
3118 k = get g_u_authl 0
3119 if lt k F_AUTH
3120 put g_u_auth k c
3121 put g_u_authl 0 add k 1
3122 end
3123 end
3124 if eq fld 3
3125 k = get g_u_licl 0
3126 if lt k F_LIC
3127 put g_u_lic k c
3128 put g_u_licl 0 add k 1
3129 end
3130 end
3131 if eq fld 4
3132 k = get g_u_terml 0
3133 if lt k F_TERM
3134 put g_u_term k c
3135 put g_u_terml 0 add k 1
3136 end
3137 end
3138 if eq fld 5
3139 k = get g_u_provl 0
3140 if lt k F_PROV
3141 put g_u_prov k c
3142 put g_u_provl 0 add k 1
3143 end
3144 end
3145 if eq fld 6
3146 k = get g_u_ptl 0
3147 if lt k F_PT
3148 put g_u_pt k to_low c
3149 put g_u_ptl 0 add k 1
3150 end
3151 end
3152 if eq fld 7
3153 k = get g_u_descl 0
3154 if lt k F_DESC
3155 put g_u_desc k c
3156 put g_u_descl 0 add k 1
3157 end
3158 end
3159 if eq fld 9
3160 k = get g_u_expl 0
3161 if lt k 64
3162 put g_u_exp k to_low c
3163 put g_u_expl 0 add k 1
3164 end
3165 end
3166 end
3167end
3168
3169fn store_mp_text_part fd int boff ulong blen ulong to void
3170 n = blen
3171 if gt n 256
3172 n = 256
3173 end
3174 if gt n 0
3175 if not pread_exact fd boff n
3176 ret
3177 end
3178 end
3179 if pname_eq t_f_name
3180 put g_u_namel 0 0
3181 copy_io_san_u 1 n
3182 ret
3183 end
3184 if pname_eq t_f_author
3185 put g_u_authl 0 0
3186 copy_io_san_u 2 n
3187 ret
3188 end
3189 if pname_eq t_f_lic
3190 put g_u_licl 0 0
3191 copy_io_san_u 3 n
3192 ret
3193 end
3194 if pname_eq t_f_terms
3195 put g_u_terml 0 0
3196 copy_io_san_u 4 n
3197 ret
3198 end
3199 if pname_eq t_f_prov
3200 put g_u_provl 0 0
3201 copy_io_san_u 5 n
3202 ret
3203 end
3204 if pname_eq t_f_pt
3205 put g_u_ptl 0 0
3206 if gt blen F_PT
3207 put g_u_st US_BAD_PT 1
3208 ret
3209 end
3210 copy_io_san_u 6 n
3211 ret
3212 end
3213 if pname_eq t_f_desc
3214 put g_u_descl 0 0
3215 copy_io_san_u 7 n
3216 ret
3217 end
3218 if pname_eq t_f_exp
3219 put g_u_expl 0 0
3220 if gt blen 64
3221 put g_u_st US_BAD_EXP 1
3222 ret
3223 end
3224 copy_io_san_u 9 n
3225 ret
3226 end
3227end
3228
3229fn parse_multipart_file fd int flen ulong to bool
3230 build_delim false
3231 first = find_delim_in_file fd 0 flen
3232 if lt first 0
3233 put g_u_st US_ERR UE_BAD_MULTIPART
3234 ret false
3235 end
3236 pos = add ulong first get g_u_deliml 0
3237 for part_i ulong HUB_MAX_PARTS bound 32
3238 if gt add pos 2 flen
3239 put g_u_st US_ERR UE_BAD_MULTIPART
3240 ret false
3241 end
3242 if not pread_exact fd pos 2
3243 put g_u_st US_ERR UE_IO_FAIL
3244 ret false
3245 end
3246 if and eq get g_io 0 45 eq get g_io 1 45
3247 ret true
3248 end
3249 if and eq get g_io 0 13 eq get g_io 1 10
3250 pos = add pos 2
3251 else
3252 if eq get g_io 0 10
3253 pos = add pos 1
3254 else
3255 put g_u_st US_ERR UE_BAD_MULTIPART
3256 ret false
3257 end
3258 end
3259 bstart = parse_part_headers fd pos flen
3260 if lt bstart 0
3261 put g_u_st US_ERR UE_BAD_MULTIPART
3262 ret false
3263 end
3264 build_delim true
3265 next_d = find_delim_in_file fd ulong bstart flen
3266 if lt next_d 0
3267 put g_u_st US_ERR UE_BAD_MULTIPART
3268 ret false
3269 end
3270 blen = sub ulong next_d ulong bstart
3271 if pname_eq t_f_pack
3272 if ne get g_u_has_pfn 0 0
3273 pfnl = get g_u_pfnl 0
3274 if gt pfnl F_FN
3275 put g_u_st US_ERR UE_BAD_FILENAME
3276 ret false
3277 end
3278 for fi ulong pfnl bound 128
3279 put g_u_fn fi get g_u_pfn fi
3280 end
3281 put g_u_fnl 0 pfnl
3282 if not validate_upload_filename
3283 put g_u_st US_ERR UE_BAD_FILENAME
3284 ret false
3285 end
3286 end
3287 put g_u_st US_HAS_PACK 1
3288 put g_u_st US_POFF ulong bstart
3289 put g_u_st US_PLEN blen
3290 else
3291 store_mp_text_part fd ulong bstart blen
3292 end
3293 pos = add ulong next_d get g_u_deliml 0
3294 end
3295 put g_u_st US_ERR UE_BAD_MULTIPART
3296 ret false
3297end
3298
3299note ---- Fail-closed OLLMSP01 verifier and atomic content-addressed store ----
3300
3301fn validate_hex_u_pt to bool
3302 n = get g_u_ptl 0
3303 if eq n 0
3304 ret true
3305 end
3306 if and ne n 16 ne n 64
3307 ret false
3308 end
3309 for i ulong n bound 64
3310 if not is_hex_low get g_u_pt i
3311 ret false
3312 end
3313 end
3314 ret true
3315end
3316
3317fn validate_hex_u_exp to bool
3318 n = get g_u_expl 0
3319 if eq n 0
3320 ret true
3321 end
3322 if ne n 64
3323 ret false
3324 end
3325 for i ulong 64
3326 if not is_hex_low get g_u_exp i
3327 ret false
3328 end
3329 end
3330 ret true
3331end
3332
3333fn ensure_default_meta to void
3334 if eq get g_u_licl 0 0
3335 for i ulong len t_def_lic bound 32
3336 put g_u_lic i get t_def_lic i
3337 end
3338 put g_u_licl 0 len t_def_lic
3339 end
3340 if eq get g_u_terml 0 0
3341 for i ulong len t_def_term bound 128
3342 put g_u_term i get t_def_term i
3343 end
3344 put g_u_terml 0 len t_def_term
3345 end
3346 if eq get g_u_fnl 0 0
3347 nl = get g_u_namel 0
3348 p = ulong 0
3349 for i ulong nl bound 80
3350 c = get g_u_name i
3351 if and is_safe_fn_char c ne c 46
3352 if lt p 120
3353 put g_u_fn p c
3354 p = add p 1
3355 end
3356 else
3357 if and gt p 0 lt p 120
3358 put g_u_fn p 45
3359 p = add p 1
3360 end
3361 end
3362 end
3363 if eq p 0
3364 put g_u_fn 0 115
3365 put g_u_fn 1 116
3366 put g_u_fn 2 114
3367 put g_u_fn 3 97
3368 put g_u_fn 4 110
3369 put g_u_fn 5 100
3370 p = 6
3371 end
3372 put g_u_fn p 46
3373 put g_u_fn add p 1 115
3374 put g_u_fn add p 2 112
3375 put g_u_fnl 0 add p 3
3376 end
3377end
3378
3379fn find_clip_from_u_prov to long
3380 pl = get g_u_provl 0
3381 if lt pl add len t_spclip_prov_pre 64
3382 ret -1
3383 end
3384 for i ulong len t_spclip_prov_pre bound 16
3385 if ne get g_u_prov i get t_spclip_prov_pre i
3386 ret -1
3387 end
3388 end
3389 off = len t_spclip_prov_pre
3390 for j ulong 64
3391 c = to_low get g_u_prov add off j
3392 if not is_hex_low c
3393 ret -1
3394 end
3395 put g_dpath j c
3396 end
3397 ret find_clip_by_dpath
3398end
3399
3400fn check_u_pt_merkle bl ulong bh ulong to ulong
3401 ptl = get g_u_ptl 0
3402 if eq ptl 16
3403 fmt_hex16 bh
3404 for i ulong 16
3405 if ne get g_u_pt i get g_hex16 i
3406 ret UE_MERKLE_MISMATCH
3407 end
3408 end
3409 end
3410 if eq ptl 64
3411 pidx = find_strand_by_u_pt
3412 if ge pidx 0
3413 if ne get g_c_sl ulong pidx bl
3414 ret UE_MERKLE_LINEAGE
3415 end
3416 end
3417 end
3418 kidx = find_clip_from_u_prov
3419 if ge kidx 0
3420 cbh = get g_k_bhash ulong kidx
3421 csp = get g_k_splice ulong kidx
3422 if ne bh cbh
3423 if lt bl csp
3424 ret UE_MERKLE_LINEAGE
3425 end
3426 if eq bl csp
3427 ret UE_MERKLE_MISMATCH
3428 end
3429 end
3430 end
3431 ret UE_OK
3432end
3433
3434fn copy_pack_to_blob rawfd int poff ulong plen ulong to bool
3435 build_tmp_p1 t_tmp_sp_pre t_ext_sp
3436 dfd = fopen g_p1 0 get g_p1l 0 577 420
3437 if lt dfd 0
3438 ret false
3439 end
3440 done = ulong 0
3441 ok = true
3442 while lt done plen bound 200000
3443 want = sub plen done
3444 if gt want HUB_IO_SZ
3445 want = HUB_IO_SZ
3446 end
3447 if not pread_exact rawfd add poff done want
3448 ok = false
3449 break
3450 end
3451 if not write_exact_fd int dfd 0 want false
3452 ok = false
3453 break
3454 end
3455 done = add done want
3456 end
3457 if ok
3458 if ne fdsync int dfd 0
3459 ok = false
3460 end
3461 end
3462 x = fclose int dfd
3463 if not ok
3464 u = unlink g_p1 0 get g_p1l 0
3465 ret false
3466 end
3467 p2_reset
3468 p2_dir
3469 p2_txt t_sub_blobs
3470 p2_txt t_slash
3471 for i ulong 64
3472 p2_b get g_u_sha i
3473 end
3474 p2_txt t_ext_sp
3475 if not atomic_rename_p1_p2
3476 u = unlink g_p1 0 get g_p1l 0
3477 ret false
3478 end
3479 ret true
3480end
3481
3482fn append_u_to_catalog idx ulong to void
3483 for i ulong 64
3484 put g_c_sha add mul idx F_SHA i get g_u_sha i
3485 end
3486 for i ulong get g_u_namel 0 bound 80
3487 put g_c_name add mul idx F_NAME i get g_u_name i
3488 end
3489 put g_c_namel idx get g_u_namel 0
3490 for i ulong get g_u_authl 0 bound 64
3491 put g_c_auth add mul idx F_AUTH i get g_u_auth i
3492 end
3493 put g_c_authl idx get g_u_authl 0
3494 for i ulong get g_u_licl 0 bound 32
3495 put g_c_lic add mul idx F_LIC i get g_u_lic i
3496 end
3497 put g_c_licl idx get g_u_licl 0
3498 for i ulong get g_u_terml 0 bound 128
3499 put g_c_term add mul idx F_TERM i get g_u_term i
3500 end
3501 put g_c_terml idx get g_u_terml 0
3502 for i ulong get g_u_provl 0 bound 160
3503 put g_c_prov add mul idx F_PROV i get g_u_prov i
3504 end
3505 put g_c_provl idx get g_u_provl 0
3506 for i ulong get g_u_ptl 0 bound 64
3507 put g_c_pt add mul idx F_PT i get g_u_pt i
3508 end
3509 put g_c_ptl idx get g_u_ptl 0
3510 for i ulong get g_u_fnl 0 bound 128
3511 put g_c_fn add mul idx F_FN i get g_u_fn i
3512 end
3513 put g_c_fnl idx get g_u_fnl 0
3514 for i ulong get g_u_descl 0 bound 256
3515 put g_c_desc add mul idx F_DESC i get g_u_desc i
3516 end
3517 put g_c_descl idx get g_u_descl 0
3518 put g_c_bhash idx get g_u_st US_BHASH
3519 put g_c_bl idx get g_u_st US_BL
3520 put g_c_sl idx get g_u_st US_SL
3521 put g_c_npar idx get g_u_st US_NPAR
3522 put g_c_phash idx get g_u_st US_PHASH
3523 put g_c_sz idx get g_u_st US_PLEN
3524 put g_c_dl idx 0
3525 put g_c_ts idx ulong realns
3526 put g_scnt 0 add idx 1
3527end
3528
3529fn stream_verify_payload rawfd int poff ulong pay_len ulong to ulong
3530 fnv = FNV_OFF
3531 done = ulong 0
3532 while lt done pay_len bound 200000
3533 want = sub pay_len done
3534 if gt want HUB_IO_SZ
3535 want = HUB_IO_SZ
3536 end
3537 if not pread_exact rawfd add add poff 48 done want
3538 put g_u_st US_ERR UE_IO_FAIL
3539 ret 0
3540 end
3541 sha_feed_io 0 want
3542 for b ulong want bound 65536
3543 fnv = mul xor fnv ulong get g_io b FNV_PRM
3544 end
3545 nw = div want 8
3546 for wi ulong nw bound 8192
3547 w = read_u64_le_io mul wi 8
3548 exp_bits = and shr w 52 2047
3549 if eq exp_bits 2047
3550 put g_u_st US_ERR UE_NONFINITE
3551 ret 0
3552 end
3553 end
3554 done = add done want
3555 end
3556 ret fnv
3557end
3558
3559fn verify_pack_header rawfd int poff ulong plen ulong to bool
3560 max_up = get g_max_up 0
3561 if gt plen max_up
3562 put g_u_st US_ERR UE_TOO_BIG
3563 put g_u_st US_HTTP_CODE 413
3564 ret false
3565 end
3566 if lt plen 48
3567 put g_u_st US_ERR UE_TRUNCATED
3568 put g_u_st US_HTTP_CODE 400
3569 ret false
3570 end
3571 if not pread_exact rawfd poff 48
3572 put g_u_st US_ERR UE_IO_FAIL
3573 put g_u_st US_HTTP_CODE 500
3574 ret false
3575 end
3576 if ne read_u64_le_io 0 OSPMAG
3577 put g_u_st US_ERR UE_BAD_MAGIC
3578 put g_u_st US_HTTP_CODE 400
3579 ret false
3580 end
3581 bl = read_u64_le_io 8
3582 sl = read_u64_le_io 16
3583 bh = read_u64_le_io 24
3584 npar = read_u64_le_io 32
3585 put g_u_st US_BL bl
3586 put g_u_st US_SL sl
3587 put g_u_st US_BHASH bh
3588 put g_u_st US_NPAR npar
3589 put g_u_st US_PHASH read_u64_le_io 40
3590 if or eq bl 0 or le sl bl gt sl 1024
3591 put g_u_st US_ERR UE_BAD_LAYERS
3592 put g_u_st US_HTTP_CODE 400
3593 ret false
3594 end
3595 if or eq bh 0 eq bh FNV_OFF
3596 put g_u_st US_ERR UE_BAD_BASE_HASH
3597 put g_u_st US_HTTP_CODE 400
3598 ret false
3599 end
3600 if or eq npar 0 gt npar div sub max_up 48 8
3601 put g_u_st US_ERR UE_BAD_PARAMS
3602 put g_u_st US_HTTP_CODE 400
3603 ret false
3604 end
3605 if ne plen add 48 mul npar 8
3606 put g_u_st US_ERR UE_SIZE_MISMATCH
3607 put g_u_st US_HTTP_CODE 400
3608 ret false
3609 end
3610 merr = check_u_pt_merkle bl bh
3611 if ne merr UE_OK
3612 put g_u_st US_ERR merr
3613 put g_u_st US_HTTP_CODE 400
3614 ret false
3615 end
3616 ret true
3617end
3618
3619fn verify_and_store_pack rawfd int to bool
3620 if eq get g_u_st US_HAS_PACK 0
3621 put g_u_st US_ERR UE_MISSING_PACK
3622 put g_u_st US_HTTP_CODE 400
3623 ret false
3624 end
3625 if or ne get g_u_st US_BAD_FN 0 not validate_upload_filename
3626 put g_u_st US_ERR UE_BAD_FILENAME
3627 put g_u_st US_HTTP_CODE 400
3628 ret false
3629 end
3630 if or eq get g_u_namel 0 0 eq get g_u_authl 0 0
3631 put g_u_st US_ERR UE_MISSING_META
3632 put g_u_st US_HTTP_CODE 400
3633 ret false
3634 end
3635 if or ne get g_u_st US_BAD_PT 0 not validate_hex_u_pt
3636 put g_u_st US_ERR UE_BAD_PARENT_TRUNK
3637 put g_u_st US_HTTP_CODE 400
3638 ret false
3639 end
3640 if or ne get g_u_st US_BAD_EXP 0 not validate_hex_u_exp
3641 put g_u_st US_ERR UE_SHA_MISMATCH
3642 put g_u_st US_HTTP_CODE 400
3643 ret false
3644 end
3645 ensure_default_meta
3646 if not validate_upload_filename
3647 put g_u_st US_ERR UE_BAD_FILENAME
3648 put g_u_st US_HTTP_CODE 400
3649 ret false
3650 end
3651 poff = get g_u_st US_POFF
3652 plen = get g_u_st US_PLEN
3653 if not verify_pack_header rawfd poff plen
3654 ret false
3655 end
3656 sha_start
3657 sha_feed_io 0 48
3658 got_fnv = stream_verify_payload rawfd poff mul get g_u_st US_NPAR 8
3659 if ne get g_u_st US_ERR UE_OK
3660 if eq get g_u_st US_ERR UE_IO_FAIL
3661 put g_u_st US_HTTP_CODE 500
3662 else
3663 put g_u_st US_HTTP_CODE 400
3664 end
3665 ret false
3666 end
3667 if ne got_fnv get g_u_st US_PHASH
3668 put g_u_st US_ERR UE_PACK_HASH
3669 put g_u_st US_HTTP_CODE 400
3670 ret false
3671 end
3672 sha_finish
3673 for i ulong 64
3674 put g_u_sha i get g_sh_hex i
3675 end
3676 if eq get g_u_expl 0 64
3677 for i ulong 64
3678 if ne get g_u_exp i get g_u_sha i
3679 put g_u_st US_ERR UE_SHA_MISMATCH
3680 put g_u_st US_HTTP_CODE 400
3681 ret false
3682 end
3683 end
3684 end
3685 dup = find_strand_by_u_sha
3686 if ge dup 0
3687 put g_u_st US_DUP_IDX ulong dup
3688 put g_u_st US_ERR UE_DUPLICATE
3689 put g_u_st US_HTTP_CODE 409
3690 ret false
3691 end
3692 if ge get g_scnt 0 HUB_MAX_STRANDS
3693 put g_u_st US_ERR UE_STORAGE_FULL
3694 put g_u_st US_HTTP_CODE 507
3695 ret false
3696 end
3697 if not copy_pack_to_blob rawfd poff plen
3698 put g_u_st US_ERR UE_IO_FAIL
3699 put g_u_st US_HTTP_CODE 500
3700 ret false
3701 end
3702 nidx = get g_scnt 0
3703 append_u_to_catalog nidx
3704 if not save_catalog
3705 put g_scnt 0 nidx
3706 put g_u_st US_ERR UE_IO_FAIL
3707 put g_u_st US_HTTP_CODE 500
3708 ret false
3709 end
3710 put g_u_st US_NEW_IDX nidx
3711 put g_u_st US_ERR UE_OK
3712 put g_u_st US_HTTP_CODE 201
3713 ret true
3714end
3715
3716note ---- Strand Clip Editor (.spclip composer, store, and fail-closed validator) ----
3717
3718fn reset_clip_edit to void
3719 for i ulong 16
3720 put g_e_st i 0
3721 end
3722 put g_e_st ES_MODE 1
3723 put g_e_st ES_TAU 700
3724 put g_e_st ES_SPLICE 0
3725 put g_e_st ES_TOK 128
3726 put g_e_st ES_SEED 42
3727 put g_e_st ES_STEPS 0
3728 put g_e_st ES_LR 3500
3729 for i ulong len t_def_clip_name bound 80
3730 put g_e_name i get t_def_clip_name i
3731 end
3732 put g_e_namel 0 len t_def_clip_name
3733 for i ulong len t_def_author bound 64
3734 put g_e_auth i get t_def_author i
3735 end
3736 put g_e_authl 0 len t_def_author
3737 for i ulong len t_def_lic bound 32
3738 put g_e_lic i get t_def_lic i
3739 end
3740 put g_e_licl 0 len t_def_lic
3741 for i ulong len t_def_prompt bound 256
3742 put g_e_prompt i get t_def_prompt i
3743 end
3744 put g_e_promptl 0 len t_def_prompt
3745 for i ulong len t_def_merges bound 128
3746 put g_e_merges i get t_def_merges i
3747 end
3748 put g_e_mergesl 0 len t_def_merges
3749 for k ulong 4
3750 put g_e_sset k 0
3751 put g_e_sidx k 0
3752 put g_e_sw k 1000
3753 put g_e_wset k 0
3754 put g_e_wbad k 0
3755 put g_e_vbh k 0
3756 put g_e_vbl k 0
3757 put g_e_vsl k 0
3758 put g_e_vbh_set k 0
3759 put g_e_vbl_set k 0
3760 put g_e_vsl_set k 0
3761 end
3762 put g_e_manl 0 0
3763end
3764
3765fn parse_qval_u64_strict to long
3766 n = get g_qvall 0
3767 if or eq n 0 gt n 20
3768 ret -1
3769 end
3770 v = ulong 0
3771 for i ulong n bound 20
3772 c = get g_qval i
3773 if or lt c 48 gt c 57
3774 ret -1
3775 end
3776 if gt v 1000000000
3777 ret -1
3778 end
3779 v = add mul v 10 ulong sub c 48
3780 end
3781 ret long v
3782end
3783
3784fn parse_qval_i64_strict ok_slot ulong to long
3785 n = get g_qvall 0
3786 if or eq n 0 gt n 20
3787 put g_e_wbad ok_slot 1
3788 ret 0
3789 end
3790 is_neg = false
3791 st = ulong 0
3792 if eq get g_qval 0 45
3793 is_neg = true
3794 st = 1
3795 else
3796 if eq get g_qval 0 43
3797 st = 1
3798 end
3799 end
3800 if ge st n
3801 put g_e_wbad ok_slot 1
3802 ret 0
3803 end
3804 v = ulong 0
3805 i = st
3806 while lt i n bound 20
3807 c = get g_qval i
3808 if or lt c 48 gt c 57
3809 put g_e_wbad ok_slot 1
3810 ret 0
3811 end
3812 if gt v 1000000
3813 put g_e_wbad ok_slot 1
3814 ret 0
3815 end
3816 v = add mul v 10 ulong sub c 48
3817 i = add i 1
3818 end
3819 put g_e_wbad ok_slot 0
3820 if is_neg
3821 ret neg long v
3822 end
3823 ret long v
3824end
3825
3826fn set_clip_slot_sha slot ulong to void
3827 vl = get g_qvall 0
3828 if eq vl 0
3829 put g_e_sset slot 0
3830 ret
3831 end
3832 if ne vl 64
3833 put g_e_sset slot 2
3834 ret
3835 end
3836 base = mul slot 64
3837 for i ulong 64
3838 c = to_low get g_qval i
3839 if not is_hex_low c
3840 put g_e_sset slot 2
3841 ret
3842 end
3843 put g_e_ssha add base i c
3844 end
3845 put g_e_sset slot 1
3846end
3847
3848fn copy_qval_san_e which ulong to void
3849 vl = get g_qvall 0
3850 if eq which 1
3851 if eq vl 0
3852 ret
3853 end
3854 if gt vl F_NAME
3855 vl = F_NAME
3856 end
3857 put g_e_namel 0 0
3858 for i ulong vl bound 80
3859 c = get g_qval i
3860 if or lt c 32 eq c 127
3861 c = 32
3862 end
3863 k = get g_e_namel 0
3864 if lt k F_NAME
3865 put g_e_name k c
3866 put g_e_namel 0 add k 1
3867 end
3868 end
3869 ret
3870 end
3871 if eq which 2
3872 if eq vl 0
3873 ret
3874 end
3875 if gt vl F_AUTH
3876 vl = F_AUTH
3877 end
3878 put g_e_authl 0 0
3879 for i ulong vl bound 64
3880 c = get g_qval i
3881 if or lt c 32 eq c 127
3882 c = 32
3883 end
3884 k = get g_e_authl 0
3885 if lt k F_AUTH
3886 put g_e_auth k c
3887 put g_e_authl 0 add k 1
3888 end
3889 end
3890 ret
3891 end
3892 if eq which 3
3893 if eq vl 0
3894 ret
3895 end
3896 if gt vl F_LIC
3897 vl = F_LIC
3898 end
3899 put g_e_licl 0 0
3900 for i ulong vl bound 32
3901 c = get g_qval i
3902 if or lt c 32 eq c 127
3903 c = 32
3904 end
3905 k = get g_e_licl 0
3906 if lt k F_LIC
3907 put g_e_lic k c
3908 put g_e_licl 0 add k 1
3909 end
3910 end
3911 ret
3912 end
3913 if eq which 4
3914 if eq vl 0
3915 ret
3916 end
3917 if gt vl 256
3918 vl = 256
3919 end
3920 put g_e_promptl 0 0
3921 for i ulong vl bound 256
3922 c = get g_qval i
3923 if or lt c 32 or eq c 127 eq c 34
3924 c = 32
3925 end
3926 k = get g_e_promptl 0
3927 if lt k 256
3928 put g_e_prompt k c
3929 put g_e_promptl 0 add k 1
3930 end
3931 end
3932 ret
3933 end
3934 if eq which 5
3935 if eq vl 0
3936 ret
3937 end
3938 if gt vl 128
3939 vl = 128
3940 end
3941 put g_e_mergesl 0 0
3942 for i ulong vl bound 128
3943 c = get g_qval i
3944 if or le c 32 eq c 127
3945 continue
3946 end
3947 k = get g_e_mergesl 0
3948 if lt k 128
3949 put g_e_merges k c
3950 put g_e_mergesl 0 add k 1
3951 end
3952 end
3953 end
3954end
3955
3956fn apply_clip_form_kv_nums to void
3957 if qkey_eq t_f_mode
3958 v = parse_qval_u64_strict
3959 if lt v 0
3960 put g_e_st ES_MODE 99
3961 else
3962 put g_e_st ES_MODE ulong v
3963 end
3964 ret
3965 end
3966 if qkey_eq t_f_tau
3967 v = parse_qval_u64_strict
3968 if lt v 0
3969 put g_e_st ES_TAU 99999
3970 else
3971 put g_e_st ES_TAU ulong v
3972 end
3973 ret
3974 end
3975 if qkey_eq t_f_splice
3976 if eq get g_qvall 0 0
3977 put g_e_st ES_SPLICE 0
3978 else
3979 v = parse_qval_u64_strict
3980 if lt v 0
3981 put g_e_st ES_SPLICE 99999
3982 else
3983 put g_e_st ES_SPLICE ulong v
3984 end
3985 end
3986 ret
3987 end
3988 if qkey_eq t_f_tokens
3989 v = parse_qval_u64_strict
3990 if lt v 0
3991 put g_e_st ES_TOK 99999
3992 else
3993 put g_e_st ES_TOK ulong v
3994 end
3995 ret
3996 end
3997 if qkey_eq t_f_seed
3998 if gt get g_qvall 0 0
3999 v = parse_qval_u64_strict
4000 if lt v 0
4001 put g_e_st ES_BAD_NUM 1
4002 else
4003 put g_e_st ES_SEED ulong v
4004 end
4005 end
4006 ret
4007 end
4008 if qkey_eq t_f_steps
4009 if gt get g_qvall 0 0
4010 v = parse_qval_u64_strict
4011 if or lt v 0 gt v 1000000
4012 put g_e_st ES_BAD_NUM 1
4013 else
4014 put g_e_st ES_STEPS ulong v
4015 end
4016 end
4017 ret
4018 end
4019 if qkey_eq t_f_lr
4020 if gt get g_qvall 0 0
4021 v = parse_qval_u64_strict
4022 if or lt v 0 gt v 1000000
4023 put g_e_st ES_BAD_NUM 1
4024 else
4025 put g_e_st ES_LR ulong v
4026 end
4027 end
4028 end
4029end
4030
4031fn apply_clip_form_kv to void
4032 if qkey_eq t_f_s1
4033 set_clip_slot_sha 0
4034 ret
4035 end
4036 if qkey_eq t_f_s2
4037 set_clip_slot_sha 1
4038 ret
4039 end
4040 if qkey_eq t_f_s3
4041 set_clip_slot_sha 2
4042 ret
4043 end
4044 if qkey_eq t_f_s4
4045 set_clip_slot_sha 3
4046 ret
4047 end
4048 if qkey_eq t_f_w1
4049 put g_e_sw 0 parse_qval_i64_strict 0
4050 put g_e_wset 0 1
4051 ret
4052 end
4053 if qkey_eq t_f_w2
4054 put g_e_sw 1 parse_qval_i64_strict 1
4055 put g_e_wset 1 1
4056 ret
4057 end
4058 if qkey_eq t_f_w3
4059 put g_e_sw 2 parse_qval_i64_strict 2
4060 put g_e_wset 2 1
4061 ret
4062 end
4063 if qkey_eq t_f_w4
4064 put g_e_sw 3 parse_qval_i64_strict 3
4065 put g_e_wset 3 1
4066 ret
4067 end
4068 if qkey_eq t_f_clip_name
4069 copy_qval_san_e 1
4070 ret
4071 end
4072 if qkey_eq t_f_author
4073 copy_qval_san_e 2
4074 ret
4075 end
4076 if qkey_eq t_f_lic
4077 copy_qval_san_e 3
4078 ret
4079 end
4080 if qkey_eq t_f_prompt
4081 copy_qval_san_e 4
4082 ret
4083 end
4084 if qkey_eq t_f_merges
4085 copy_qval_san_e 5
4086 ret
4087 end
4088 if qkey_eq t_f_action
4089 if eq get g_qvall 0 len t_v_download
4090 ok = true
4091 for i ulong len t_v_download bound 16
4092 if ne get g_qval i get t_v_download i
4093 ok = false
4094 break
4095 end
4096 end
4097 if ok
4098 put g_e_st ES_ACTION 1
4099 end
4100 end
4101 ret
4102 end
4103 apply_clip_form_kv_nums
4104end
4105
4106fn parse_clip_form_vbuf to void
4107 n = get g_e_vbufl 0
4108 i = ulong 0
4109 while lt i n bound 16384
4110 put g_qkeyl 0 0
4111 put g_qvall 0 0
4112 in_val = false
4113 while lt i n bound 16384
4114 c = get g_e_vbuf i
4115 if eq c 38
4116 i = add i 1
4117 break
4118 end
4119 if and not in_val eq c 61
4120 in_val = true
4121 i = add i 1
4122 continue
4123 end
4124 b = c
4125 if eq c 43
4126 b = 32
4127 i = add i 1
4128 else
4129 if eq c 37
4130 if le add i 3 n
4131 h1 = hex_val get g_e_vbuf add i 1
4132 h2 = hex_val get g_e_vbuf add i 2
4133 if and ge h1 0 ge h2 0
4134 b = uchar add mul h1 16 h2
4135 i = add i 3
4136 else
4137 i = add i 1
4138 end
4139 else
4140 i = add i 1
4141 end
4142 else
4143 i = add i 1
4144 end
4145 end
4146 if in_val
4147 vl = get g_qvall 0
4148 if lt vl 256
4149 put g_qval vl b
4150 put g_qvall 0 add vl 1
4151 end
4152 else
4153 kl = get g_qkeyl 0
4154 if lt kl 64
4155 put g_qkey kl b
4156 put g_qkeyl 0 add kl 1
4157 end
4158 end
4159 end
4160 if gt get g_qkeyl 0 0
4161 apply_clip_form_kv
4162 end
4163 end
4164end
4165
4166fn man_reset to void
4167 put g_e_manl 0 0
4168end
4169
4170fn man_b c uchar to void
4171 n = get g_e_manl 0
4172 if lt n 16384
4173 put g_e_man n c
4174 put g_e_manl 0 add n 1
4175 end
4176end
4177
4178fn man_txt t view uchar * to void
4179 for i ulong len t bound 256
4180 man_b get t i
4181 end
4182end
4183
4184fn man_u64 v ulong to void
4185 buf = new uchar 24 0
4186 p = ulong 24
4187 n = v
4188 if eq n 0
4189 man_b 48
4190 ret
4191 end
4192 while gt n 0 bound 24
4193 p = sub p 1
4194 put buf p uchar add 48 rem n 10
4195 n = div n 10
4196 end
4197 while lt p 24 bound 24
4198 man_b get buf p
4199 p = add p 1
4200 end
4201end
4202
4203fn man_i64 v long to void
4204 if lt v 0
4205 man_b 45
4206 man_u64 ulong neg v
4207 ret
4208 end
4209 man_u64 ulong v
4210end
4211
4212fn man_hex16 v ulong to void
4213 fmt_hex16 v
4214 for i ulong 16
4215 man_b get g_hex16 i
4216 end
4217end
4218
4219fn pt_compatible_pair ia ulong ib ulong to bool
4220 la = get g_c_ptl ia
4221 lb = get g_c_ptl ib
4222 if and eq la 64 eq lb 64
4223 ba = mul ia F_PT
4224 bb = mul ib F_PT
4225 for i ulong 64
4226 if ne get g_c_pt add ba i get g_c_pt add bb i
4227 ret false
4228 end
4229 end
4230 end
4231 ret true
4232end
4233
4234fn validate_clip_slot_strict k ulong ik ulong to bool
4235 if eq get g_e_vbh_set k 0
4236 put g_e_st ES_ERR CE_INCOMPAT_TRUNK
4237 ret false
4238 end
4239 if ne get g_e_vbh k get g_c_bhash ik
4240 put g_e_st ES_ERR CE_INCOMPAT_TRUNK
4241 ret false
4242 end
4243 if eq get g_e_vbl_set k 0
4244 put g_e_st ES_ERR CE_BAD_FORMAT
4245 ret false
4246 end
4247 if ne get g_e_vbl k get g_c_bl ik
4248 put g_e_st ES_ERR CE_BAD_FORMAT
4249 ret false
4250 end
4251 if eq get g_e_vsl_set k 0
4252 put g_e_st ES_ERR CE_BAD_FORMAT
4253 ret false
4254 end
4255 if ne get g_e_vsl k get g_c_sl ik
4256 put g_e_st ES_ERR CE_BAD_FORMAT
4257 ret false
4258 end
4259 ret true
4260end
4261
4262fn validate_clip_compat sc ulong to bool
4263 strict = ne get g_e_st ES_STRICT_VERIFY 0
4264 i0 = get g_e_sidx 0
4265 bh0 = get g_c_bhash i0
4266 min_bl = get g_c_bl i0
4267 put g_e_st ES_BHASH bh0
4268 for k ulong sc bound 4
4269 ik = get g_e_sidx k
4270 if ne get g_c_bhash ik bh0
4271 put g_e_st ES_ERR CE_INCOMPAT_TRUNK
4272 ret false
4273 end
4274 if strict
4275 if not validate_clip_slot_strict k ik
4276 ret false
4277 end
4278 end
4279 if lt get g_c_bl ik min_bl
4280 min_bl = get g_c_bl ik
4281 end
4282 end
4283 for a ulong sc bound 4
4284 ia = get g_e_sidx a
4285 for b ulong sc bound 4
4286 if gt b a
4287 if not pt_compatible_pair ia get g_e_sidx b
4288 put g_e_st ES_ERR CE_INCOMPAT_TRUNK
4289 ret false
4290 end
4291 end
4292 end
4293 end
4294 put g_e_st ES_MIN_BL min_bl
4295 ret true
4296end
4297
4298fn validate_clip_slots to bool
4299 strict = ne get g_e_st ES_STRICT_VERIFY 0
4300 seen_gap = false
4301 sc = ulong 0
4302 for k ulong 4
4303 st = get g_e_sset k
4304 if eq st 2
4305 put g_e_st ES_ERR CE_UNKNOWN_STRAND
4306 ret false
4307 end
4308 if eq st 0
4309 seen_gap = true
4310 end
4311 if eq st 1
4312 if and strict seen_gap
4313 put g_e_st ES_ERR CE_BAD_FORMAT
4314 ret false
4315 end
4316 idx = find_strand_by_eslot k
4317 if lt idx 0
4318 put g_e_st ES_ERR CE_UNKNOWN_STRAND
4319 ret false
4320 end
4321 if ne sc k
4322 bsrc = mul k 64
4323 bdst = mul sc 64
4324 for j ulong 64
4325 put g_e_ssha add bdst j get g_e_ssha add bsrc j
4326 end
4327 put g_e_sw sc get g_e_sw k
4328 put g_e_wbad sc get g_e_wbad k
4329 end
4330 put g_e_sidx sc ulong idx
4331 put g_e_sset sc 1
4332 if ne sc k
4333 put g_e_sset k 0
4334 end
4335 sc = add sc 1
4336 end
4337 end
4338 put g_e_st ES_SC sc
4339 if or lt sc 2 gt sc 4
4340 put g_e_st ES_ERR CE_STRAND_COUNT
4341 ret false
4342 end
4343 ret validate_clip_compat sc
4344end
4345
4346fn validate_clip_params to bool
4347 if ne get g_e_st ES_BAD_NUM 0
4348 put g_e_st ES_ERR CE_BAD_FORMAT
4349 ret false
4350 end
4351 mode = get g_e_st ES_MODE
4352 if gt mode 1
4353 put g_e_st ES_ERR CE_BAD_MODE
4354 ret false
4355 end
4356 if gt get g_e_st ES_TAU 5000
4357 put g_e_st ES_ERR CE_BAD_TAU
4358 ret false
4359 end
4360 tok = get g_e_st ES_TOK
4361 if or eq tok 0 gt tok 8192
4362 put g_e_st ES_ERR CE_BAD_TOKENS
4363 ret false
4364 end
4365 sp = get g_e_st ES_SPLICE
4366 min_bl = get g_e_st ES_MIN_BL
4367 if eq sp 0
4368 if ne get g_e_st ES_STRICT_VERIFY 0
4369 put g_e_st ES_ERR CE_BAD_SPLICE
4370 ret false
4371 end
4372 put g_e_st ES_SPLICE min_bl
4373 else
4374 if gt sp min_bl
4375 put g_e_st ES_ERR CE_BAD_SPLICE
4376 ret false
4377 end
4378 end
4379 sc = get g_e_st ES_SC
4380 pos_cnt = ulong 0
4381 for k ulong sc bound 4
4382 if ne get g_e_wbad k 0
4383 put g_e_st ES_ERR CE_BAD_WEIGHT
4384 ret false
4385 end
4386 w = get g_e_sw k
4387 if eq w 0
4388 put g_e_st ES_ERR CE_BAD_WEIGHT
4389 ret false
4390 end
4391 if or lt w -5000 gt w 5000
4392 put g_e_st ES_ERR CE_BAD_WEIGHT
4393 ret false
4394 end
4395 if eq mode 1
4396 if lt w 1
4397 put g_e_st ES_ERR CE_BAD_WEIGHT
4398 ret false
4399 end
4400 end
4401 if gt w 0
4402 pos_cnt = add pos_cnt 1
4403 end
4404 end
4405 if eq pos_cnt 0
4406 put g_e_st ES_ERR CE_BAD_WEIGHT
4407 ret false
4408 end
4409 ret true
4410end
4411
4412fn build_spclip_manifest to void
4413 man_reset
4414 man_txt t_mf_magic
4415 man_b 10
4416 man_txt t_mf_name
4417 for i ulong get g_e_namel 0 bound 80
4418 man_b get g_e_name i
4419 end
4420 man_b 10
4421 man_txt t_mf_auth
4422 for i ulong get g_e_authl 0 bound 64
4423 man_b get g_e_auth i
4424 end
4425 man_b 10
4426 man_txt t_mf_lic
4427 for i ulong get g_e_licl 0 bound 32
4428 man_b get g_e_lic i
4429 end
4430 man_b 10
4431 man_txt t_mf_bh
4432 man_hex16 get g_e_st ES_BHASH
4433 man_b 10
4434 man_txt t_mf_pt
4435 i0 = get g_e_sidx 0
4436 if gt get g_c_ptl i0 0
4437 base0 = mul i0 F_PT
4438 for j ulong get g_c_ptl i0 bound 64
4439 man_b get g_c_pt add base0 j
4440 end
4441 end
4442 man_b 10
4443 man_txt t_mf_sp
4444 man_u64 get g_e_st ES_SPLICE
4445 man_b 10
4446 man_txt t_mf_mode
4447 man_u64 get g_e_st ES_MODE
4448 man_b 10
4449 man_txt t_mf_tau
4450 man_u64 get g_e_st ES_TAU
4451 man_b 10
4452 man_txt t_mf_tok
4453 man_u64 get g_e_st ES_TOK
4454 man_b 10
4455 man_txt t_mf_seed
4456 man_u64 get g_e_st ES_SEED
4457 man_b 10
4458 man_txt t_mf_steps
4459 man_u64 get g_e_st ES_STEPS
4460 man_b 10
4461 man_txt t_mf_lr
4462 man_u64 get g_e_st ES_LR
4463 man_b 10
4464 sc = get g_e_st ES_SC
4465 man_txt t_mf_sc
4466 man_u64 sc
4467 man_b 10
4468 for k ulong sc bound 4
4469 ik = get g_e_sidx k
4470 man_txt t_mf_s_pre
4471 man_u64 add k 1
4472 man_txt t_mf_sha_k
4473 bs = mul ik F_SHA
4474 for j ulong 64
4475 man_b get g_c_sha add bs j
4476 end
4477 man_b 10
4478 man_txt t_mf_s_pre
4479 man_u64 add k 1
4480 man_txt t_mf_bh_k
4481 man_hex16 get g_c_bhash ik
4482 man_b 10
4483 man_txt t_mf_s_pre
4484 man_u64 add k 1
4485 man_txt t_mf_bl_k
4486 man_u64 get g_c_bl ik
4487 man_b 10
4488 man_txt t_mf_s_pre
4489 man_u64 add k 1
4490 man_txt t_mf_sl_k
4491 man_u64 get g_c_sl ik
4492 man_b 10
4493 man_txt t_mf_s_pre
4494 man_u64 add k 1
4495 man_txt t_mf_wm_k
4496 man_i64 get g_e_sw k
4497 man_b 10
4498 man_txt t_mf_s_pre
4499 man_u64 add k 1
4500 man_txt t_mf_fn_k
4501 bf = mul ik F_FN
4502 for j ulong get g_c_fnl ik bound 128
4503 man_b get g_c_fn add bf j
4504 end
4505 man_b 10
4506 end
4507 sha_start
4508 sha_feed_man 0 get g_e_manl 0
4509 sha_finish
4510 for i ulong 64
4511 put g_e_bsha i get g_sh_hex i
4512 end
4513 man_txt t_mf_bsha
4514 for i ulong 64
4515 man_b get g_e_bsha i
4516 end
4517 man_b 10
4518 sha_start
4519 sha_feed_man 0 get g_e_manl 0
4520 sha_finish
4521 for i ulong 64
4522 put g_e_sha i get g_sh_hex i
4523 end
4524end
4525
4526fn write_man_to_clip_file to bool
4527 build_tmp_p1 t_tmp_clip_pre t_ext_spclip
4528 fd = fopen g_p1 0 get g_p1l 0 577 420
4529 if lt fd 0
4530 ret false
4531 end
4532 n = get g_e_manl 0
4533 off = ulong 0
4534 ok = true
4535 for it ulong 32768 bound 32768
4536 if ge off n
4537 break
4538 end
4539 w = write int fd g_e_man off sub n off
4540 if le w 0
4541 if eq w -4
4542 continue
4543 end
4544 ok = false
4545 break
4546 end
4547 off = add off ulong w
4548 end
4549 if lt off n
4550 ok = false
4551 end
4552 if ok
4553 if ne fsync int fd 0
4554 ok = false
4555 end
4556 end
4557 x = fclose int fd
4558 if not ok
4559 u = unlink g_p1 0 get g_p1l 0
4560 ret false
4561 end
4562 p2_reset
4563 p2_dir
4564 p2_txt t_sub_clips
4565 p2_txt t_slash
4566 for i ulong 64
4567 p2_b get g_e_sha i
4568 end
4569 p2_txt t_ext_spclip
4570 if not atomic_rename_p1_p2
4571 u = unlink g_p1 0 get g_p1l 0
4572 ret false
4573 end
4574 ret true
4575end
4576
4577fn append_clip_record idx ulong to void
4578 for i ulong 64
4579 put g_k_sha add mul idx 64 i get g_e_sha i
4580 end
4581 for i ulong get g_e_namel 0 bound 80
4582 put g_k_name add mul idx F_NAME i get g_e_name i
4583 end
4584 put g_k_namel idx get g_e_namel 0
4585 for i ulong get g_e_authl 0 bound 64
4586 put g_k_auth add mul idx F_AUTH i get g_e_auth i
4587 end
4588 put g_k_authl idx get g_e_authl 0
4589 for i ulong get g_e_licl 0 bound 32
4590 put g_k_lic add mul idx F_LIC i get g_e_lic i
4591 end
4592 put g_k_licl idx get g_e_licl 0
4593 put g_k_bhash idx get g_e_st ES_BHASH
4594 put g_k_mode idx get g_e_st ES_MODE
4595 put g_k_tau idx get g_e_st ES_TAU
4596 put g_k_splice idx get g_e_st ES_SPLICE
4597 sc = get g_e_st ES_SC
4598 put g_k_sc idx sc
4599 put g_k_dl idx 0
4600 put g_k_ts idx ulong realns
4601 put g_k_tok idx get g_e_st ES_TOK
4602 put g_k_seed idx get g_e_st ES_SEED
4603 put g_k_steps idx get g_e_st ES_STEPS
4604 put g_k_lr idx get g_e_st ES_LR
4605 for slot ulong 4
4606 base = mul add mul idx 4 slot 64
4607 if lt slot sc
4608 ik = get g_e_sidx slot
4609 bs = mul ik F_SHA
4610 for j ulong 64
4611 put g_k_ssha add base j get g_c_sha add bs j
4612 end
4613 put g_k_sw add mul idx 4 slot get g_e_sw slot
4614 else
4615 for j ulong 64
4616 put g_k_ssha add base j 48
4617 end
4618 put g_k_sw add mul idx 4 slot 0
4619 end
4620 end
4621 put g_kcnt 0 add idx 1
4622end
4623
4624fn compose_and_store_clip to bool
4625 if not validate_clip_slots
4626 ret false
4627 end
4628 if not validate_clip_params
4629 ret false
4630 end
4631 build_spclip_manifest
4632 ex = find_clip_by_esha
4633 if ge ex 0
4634 put g_e_st ES_ERR CE_OK
4635 ret true
4636 end
4637 if ge get g_kcnt 0 HUB_MAX_CLIPS
4638 put g_e_st ES_ERR CE_STORAGE_FULL
4639 ret false
4640 end
4641 if not write_man_to_clip_file
4642 put g_e_st ES_ERR CE_IO_FAIL
4643 ret false
4644 end
4645 kidx = get g_kcnt 0
4646 append_clip_record kidx
4647 if not save_clips_catalog
4648 put g_kcnt 0 kidx
4649 put g_e_st ES_ERR CE_IO_FAIL
4650 ret false
4651 end
4652 put g_e_st ES_ERR CE_OK
4653 ret true
4654end
4655
4656note ---- Standalone fail-closed .spclip manifest verifier (POST /strands/clip/verify) ----
4657
4658fn vbuf_starts off ulong lim ulong pat view uchar * to bool
4659 if gt add off len pat lim
4660 ret false
4661 end
4662 for i ulong len pat bound 64
4663 if ne get g_e_vbuf add off i get pat i
4664 ret false
4665 end
4666 end
4667 ret true
4668end
4669
4670fn copy_vbuf_to_qval lo ulong hi ulong to void
4671 n = sub hi lo
4672 if gt n 256
4673 n = 256
4674 end
4675 for i ulong n bound 256
4676 put g_qval i get g_e_vbuf add lo i
4677 end
4678 put g_qvall 0 n
4679end
4680
4681fn parse_spclip_slot_bh slot ulong to void
4682 if ne get g_qvall 0 16
4683 put g_e_st ES_BAD_NUM 1
4684 ret
4685 end
4686 hv = ulong 0
4687 for hi ulong 16
4688 d = hex_val get g_qval hi
4689 if lt d 0
4690 put g_e_st ES_BAD_NUM 1
4691 ret
4692 end
4693 hv = or shl hv 4 ulong d
4694 end
4695 put g_e_vbh slot hv
4696 put g_e_vbh_set slot 1
4697end
4698
4699fn parse_spclip_slot_meta ls ulong le_pos ulong to bool
4700 if vbuf_starts ls le_pos t_mf_s1_bh
4701 copy_vbuf_to_qval add ls len t_mf_s1_bh le_pos
4702 parse_spclip_slot_bh 0
4703 ret true
4704 end
4705 if vbuf_starts ls le_pos t_mf_s2_bh
4706 copy_vbuf_to_qval add ls len t_mf_s2_bh le_pos
4707 parse_spclip_slot_bh 1
4708 ret true
4709 end
4710 if vbuf_starts ls le_pos t_mf_s3_bh
4711 copy_vbuf_to_qval add ls len t_mf_s3_bh le_pos
4712 parse_spclip_slot_bh 2
4713 ret true
4714 end
4715 if vbuf_starts ls le_pos t_mf_s4_bh
4716 copy_vbuf_to_qval add ls len t_mf_s4_bh le_pos
4717 parse_spclip_slot_bh 3
4718 ret true
4719 end
4720 if vbuf_starts ls le_pos t_mf_s1_bl
4721 copy_vbuf_to_qval add ls len t_mf_s1_bl le_pos
4722 v = parse_qval_u64_strict
4723 if lt v 0
4724 put g_e_st ES_BAD_NUM 1
4725 else
4726 put g_e_vbl 0 ulong v
4727 put g_e_vbl_set 0 1
4728 end
4729 ret true
4730 end
4731 if vbuf_starts ls le_pos t_mf_s2_bl
4732 copy_vbuf_to_qval add ls len t_mf_s2_bl le_pos
4733 v = parse_qval_u64_strict
4734 if lt v 0
4735 put g_e_st ES_BAD_NUM 1
4736 else
4737 put g_e_vbl 1 ulong v
4738 put g_e_vbl_set 1 1
4739 end
4740 ret true
4741 end
4742 if vbuf_starts ls le_pos t_mf_s3_bl
4743 copy_vbuf_to_qval add ls len t_mf_s3_bl le_pos
4744 v = parse_qval_u64_strict
4745 if lt v 0
4746 put g_e_st ES_BAD_NUM 1
4747 else
4748 put g_e_vbl 2 ulong v
4749 put g_e_vbl_set 2 1
4750 end
4751 ret true
4752 end
4753 if vbuf_starts ls le_pos t_mf_s4_bl
4754 copy_vbuf_to_qval add ls len t_mf_s4_bl le_pos
4755 v = parse_qval_u64_strict
4756 if lt v 0
4757 put g_e_st ES_BAD_NUM 1
4758 else
4759 put g_e_vbl 3 ulong v
4760 put g_e_vbl_set 3 1
4761 end
4762 ret true
4763 end
4764 if vbuf_starts ls le_pos t_mf_s1_sl
4765 copy_vbuf_to_qval add ls len t_mf_s1_sl le_pos
4766 v = parse_qval_u64_strict
4767 if lt v 0
4768 put g_e_st ES_BAD_NUM 1
4769 else
4770 put g_e_vsl 0 ulong v
4771 put g_e_vsl_set 0 1
4772 end
4773 ret true
4774 end
4775 if vbuf_starts ls le_pos t_mf_s2_sl
4776 copy_vbuf_to_qval add ls len t_mf_s2_sl le_pos
4777 v = parse_qval_u64_strict
4778 if lt v 0
4779 put g_e_st ES_BAD_NUM 1
4780 else
4781 put g_e_vsl 1 ulong v
4782 put g_e_vsl_set 1 1
4783 end
4784 ret true
4785 end
4786 if vbuf_starts ls le_pos t_mf_s3_sl
4787 copy_vbuf_to_qval add ls len t_mf_s3_sl le_pos
4788 v = parse_qval_u64_strict
4789 if lt v 0
4790 put g_e_st ES_BAD_NUM 1
4791 else
4792 put g_e_vsl 2 ulong v
4793 put g_e_vsl_set 2 1
4794 end
4795 ret true
4796 end
4797 if vbuf_starts ls le_pos t_mf_s4_sl
4798 copy_vbuf_to_qval add ls len t_mf_s4_sl le_pos
4799 v = parse_qval_u64_strict
4800 if lt v 0
4801 put g_e_st ES_BAD_NUM 1
4802 else
4803 put g_e_vsl 3 ulong v
4804 put g_e_vsl_set 3 1
4805 end
4806 ret true
4807 end
4808 ret false
4809end
4810
4811fn parse_spclip_line ls ulong le_pos ulong to void
4812 if parse_spclip_slot_meta ls le_pos
4813 ret
4814 end
4815 if vbuf_starts ls le_pos t_mf_seed
4816 copy_vbuf_to_qval add ls len t_mf_seed le_pos
4817 v = parse_qval_u64_strict
4818 if lt v 0
4819 put g_e_st ES_BAD_NUM 1
4820 else
4821 put g_e_st ES_SEED ulong v
4822 end
4823 ret
4824 end
4825 if vbuf_starts ls le_pos t_mf_steps
4826 copy_vbuf_to_qval add ls len t_mf_steps le_pos
4827 v = parse_qval_u64_strict
4828 if or lt v 0 gt v 1000000
4829 put g_e_st ES_BAD_NUM 1
4830 else
4831 put g_e_st ES_STEPS ulong v
4832 end
4833 ret
4834 end
4835 if vbuf_starts ls le_pos t_mf_lr
4836 copy_vbuf_to_qval add ls len t_mf_lr le_pos
4837 v = parse_qval_u64_strict
4838 if or lt v 0 gt v 1000000
4839 put g_e_st ES_BAD_NUM 1
4840 else
4841 put g_e_st ES_LR ulong v
4842 end
4843 ret
4844 end
4845 if vbuf_starts ls le_pos t_mf_sc
4846 copy_vbuf_to_qval add ls len t_mf_sc le_pos
4847 v = parse_qval_u64_strict
4848 if lt v 0
4849 put g_e_st ES_BAD_NUM 1
4850 else
4851 put g_e_st ES_SC ulong v
4852 end
4853 ret
4854 end
4855 if vbuf_starts ls le_pos t_mf_bh
4856 copy_vbuf_to_qval add ls len t_mf_bh le_pos
4857 if ne get g_qvall 0 16
4858 put g_e_st ES_BAD_NUM 1
4859 ret
4860 end
4861 hv = ulong 0
4862 for hi ulong 16
4863 d = hex_val get g_qval hi
4864 if lt d 0
4865 put g_e_st ES_BAD_NUM 1
4866 ret
4867 end
4868 hv = or shl hv 4 ulong d
4869 end
4870 put g_e_st ES_BHASH hv
4871 ret
4872 end
4873 if vbuf_starts ls le_pos t_mf_mode
4874 copy_vbuf_to_qval add ls len t_mf_mode le_pos
4875 v = parse_qval_u64_strict
4876 if lt v 0
4877 put g_e_st ES_MODE 99
4878 else
4879 put g_e_st ES_MODE ulong v
4880 end
4881 ret
4882 end
4883 if vbuf_starts ls le_pos t_mf_tau
4884 copy_vbuf_to_qval add ls len t_mf_tau le_pos
4885 v = parse_qval_u64_strict
4886 if lt v 0
4887 put g_e_st ES_TAU 99999
4888 else
4889 put g_e_st ES_TAU ulong v
4890 end
4891 ret
4892 end
4893 if vbuf_starts ls le_pos t_mf_sp
4894 copy_vbuf_to_qval add ls len t_mf_sp le_pos
4895 v = parse_qval_u64_strict
4896 if lt v 0
4897 put g_e_st ES_SPLICE 99999
4898 else
4899 put g_e_st ES_SPLICE ulong v
4900 end
4901 ret
4902 end
4903 if vbuf_starts ls le_pos t_mf_tok
4904 copy_vbuf_to_qval add ls len t_mf_tok le_pos
4905 v = parse_qval_u64_strict
4906 if lt v 0
4907 put g_e_st ES_TOK 99999
4908 else
4909 put g_e_st ES_TOK ulong v
4910 end
4911 ret
4912 end
4913 if vbuf_starts ls le_pos t_mf_s1_sha
4914 copy_vbuf_to_qval add ls len t_mf_s1_sha le_pos
4915 set_clip_slot_sha 0
4916 ret
4917 end
4918 if vbuf_starts ls le_pos t_mf_s2_sha
4919 copy_vbuf_to_qval add ls len t_mf_s2_sha le_pos
4920 set_clip_slot_sha 1
4921 ret
4922 end
4923 if vbuf_starts ls le_pos t_mf_s3_sha
4924 copy_vbuf_to_qval add ls len t_mf_s3_sha le_pos
4925 set_clip_slot_sha 2
4926 ret
4927 end
4928 if vbuf_starts ls le_pos t_mf_s4_sha
4929 copy_vbuf_to_qval add ls len t_mf_s4_sha le_pos
4930 set_clip_slot_sha 3
4931 ret
4932 end
4933 if vbuf_starts ls le_pos t_mf_s1_w
4934 copy_vbuf_to_qval add ls len t_mf_s1_w le_pos
4935 put g_e_sw 0 parse_qval_i64_strict 0
4936 put g_e_wset 0 1
4937 ret
4938 end
4939 if vbuf_starts ls le_pos t_mf_s2_w
4940 copy_vbuf_to_qval add ls len t_mf_s2_w le_pos
4941 put g_e_sw 1 parse_qval_i64_strict 1
4942 put g_e_wset 1 1
4943 ret
4944 end
4945 if vbuf_starts ls le_pos t_mf_s3_w
4946 copy_vbuf_to_qval add ls len t_mf_s3_w le_pos
4947 put g_e_sw 2 parse_qval_i64_strict 2
4948 put g_e_wset 2 1
4949 ret
4950 end
4951 if vbuf_starts ls le_pos t_mf_s4_w
4952 copy_vbuf_to_qval add ls len t_mf_s4_w le_pos
4953 put g_e_sw 3 parse_qval_i64_strict 3
4954 put g_e_wset 3 1
4955 ret
4956 end
4957end
4958
4959fn verify_submitted_spclip to bool
4960 reset_clip_edit
4961 put g_e_st ES_STRICT_VERIFY 1
4962 put g_e_st ES_MODE 99
4963 put g_e_st ES_TAU 99999
4964 put g_e_st ES_SPLICE 99999
4965 put g_e_st ES_TOK 99999
4966 put g_e_st ES_SC 0
4967 put g_e_st ES_BHASH 0
4968 for k ulong 4
4969 put g_e_wbad k 1
4970 end
4971 n = get g_e_vbufl 0
4972 if lt n add add len t_mf_magic 1 len t_mf_bsha
4973 put g_e_st ES_ERR CE_BAD_FORMAT
4974 ret false
4975 end
4976 if not vbuf_starts 0 n t_mf_magic
4977 put g_e_st ES_ERR CE_BAD_FORMAT
4978 ret false
4979 end
4980 if ne get g_e_vbuf len t_mf_magic 10
4981 put g_e_st ES_ERR CE_BAD_FORMAT
4982 ret false
4983 end
4984 bsha_pos = ulong 0
4985 found_bsha = false
4986 i = ulong 0
4987 while lt i n bound 16384
4988 ls = i
4989 while lt i n bound 16384
4990 c = get g_e_vbuf i
4991 i = add i 1
4992 if eq c 10
4993 break
4994 end
4995 end
4996 le_pos = i
4997 if gt le_pos ls
4998 if eq get g_e_vbuf sub le_pos 1 10
4999 le_pos = sub le_pos 1
5000 end
5001 end
5002 if vbuf_starts ls le_pos t_mf_bsha
5003 if ne sub le_pos add ls len t_mf_bsha 64
5004 put g_e_st ES_ERR CE_TAMPERED
5005 ret false
5006 end
5007 if ne i n
5008 put g_e_st ES_ERR CE_TAMPERED
5009 ret false
5010 end
5011 bsha_pos = ls
5012 found_bsha = true
5013 for j ulong 64
5014 put g_e_bsha j get g_e_vbuf add add ls len t_mf_bsha j
5015 end
5016 break
5017 end
5018 parse_spclip_line ls le_pos
5019 end
5020 if or not found_bsha ne get g_e_st ES_BAD_NUM 0
5021 put g_e_st ES_ERR CE_BAD_FORMAT
5022 ret false
5023 end
5024 sha_start
5025 sha_feed_vbuf 0 bsha_pos
5026 sha_finish
5027 for j ulong 64
5028 if ne get g_sh_hex j get g_e_bsha j
5029 put g_e_st ES_ERR CE_TAMPERED
5030 ret false
5031 end
5032 end
5033 sha_start
5034 sha_feed_vbuf 0 n
5035 sha_finish
5036 for j ulong 64
5037 put g_e_sha j get g_sh_hex j
5038 end
5039 decl_sc = get g_e_st ES_SC
5040 decl_bh = get g_e_st ES_BHASH
5041 if not validate_clip_slots
5042 ret false
5043 end
5044 if ne decl_sc get g_e_st ES_SC
5045 put g_e_st ES_ERR CE_STRAND_COUNT
5046 ret false
5047 end
5048 if ne decl_bh get g_e_st ES_BHASH
5049 put g_e_st ES_ERR CE_INCOMPAT_TRUNK
5050 ret false
5051 end
5052 if not validate_clip_params
5053 ret false
5054 end
5055 put g_e_st ES_ERR CE_OK
5056 ret true
5057end
5058
5059note ---- HTML / JSON / TSV templates and renderers ----
5060
5061text t_h_doc1 "<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>"
5062text t_h_doc2 "</title><meta name=\"description\" content=\"Interplane Helix Strand Exchange: discover, verify, download, edit, and share open-source OLLMSP01 specialist strands and spclip recipes.\"><link rel=\"icon\" type=\"image/svg+xml\" href=\"/public/favicon.svg\"><link rel=\"stylesheet\" href=\"/public/style.css\"><link rel=\"stylesheet\" href=\"/strands/hub.css\"></head>\n<body><header class=\"site-head\"><div class=\"shell head-inner\"><a class=\"brand\" translate=\"no\" href=\"/\">interplane<i class=\"dot\">.</i></a><nav aria-label=\"Main\"><a href=\"/omega/\">Omega</a><a href=\"/omega/helix/\">Helix</a><a href=\"/strands/\""
5063text t_h_cur " aria-current=\"page\""
5064text t_h_doc3 ">Strands</a><a href=\"/strands/clip\""
5065text t_h_doc4 ">Clip Editor</a><a href=\"/omega/compile/\">Compiler</a><a href=\"https://interplane.io/drakestapleton/omega-mv\">Source</a></nav></div></header>\n<main>"
5066text t_h_foot "</main>\n<footer class=\"site-foot\"><div class=\"shell foot-inner\"><p>Interplane &middot; <a href=\"/strands/\">Strand Exchange</a> &middot; <a href=\"/strands/clip\">Clip Editor</a> &middot; <a href=\"/strands/index.json\">index.json</a> &middot; <a href=\"/strands/index.tsv\">index.tsv</a> &middot; <a href=\"/omega/helix/\">Omega Helix</a></p></div></footer></body></html>\n"
5067
5068text t_ttl_home "Helix Strand Exchange | Interplane"
5069text t_ttl_clip "Strand Clip Editor (spclip) | Interplane"
5070text t_ttl_det "Verified Strand Receipt | Interplane"
5071
5072text t_hero1 "<section class=\"hub-hero\"><div class=\"shell\"><span class=\"eyebrow\">Interplane &middot; Open-Source Strand Exchange &middot; OLLMSP01 &middot; Zero JS</span><h1>Share and combine <em>Helix Strands</em></h1><p class=\"lede\">A <strong>Helix Strand</strong> (<code>OLLMSP01</code>) is a portable, cryptographically verified skill layer&mdash;code, math, reasoning, or domain craft&mdash;that attaches to a frozen base model without overwriting it. Train one focused specialist, share it openly here, and snap 2 to 4 community strands together at inference time with <code>ollm spclip</code>.</p><div class=\"hub-cta\"><a class=\"hub-btn hub-btn-primary\" href=\"#catalog\">Browse verified strands</a><a class=\"hub-btn hub-btn-emerald\" href=\"/strands/clip\">Open Strand Clip Editor</a><a class=\"hub-btn\" href=\"#upload\">Publish a strand</a><a class=\"hub-btn\" href=\"#what-is-a-strand\">How strands work</a></div><div class=\"hub-kpis\"><div class=\"hub-kpi\"><span class=\"hub-kpi-k\">Verified Strands</span><span class=\"hub-kpi-v\">"
5073text t_hero2 "</span></div><div class=\"hub-kpi\"><span class=\"hub-kpi-k\">Compatible Trunks</span><span class=\"hub-kpi-v\">"
5074text t_hero3 "</span></div><div class=\"hub-kpi\"><span class=\"hub-kpi-k\">Saved Clip Recipes</span><span class=\"hub-kpi-v\">"
5075text t_hero4 "</span></div><div class=\"hub-kpi\"><span class=\"hub-kpi-k\">Total Downloads</span><span class=\"hub-kpi-v\">"
5076text t_hero5 "</span></div></div></div></section>\n"
5077
5078text t_intro1 "<section class=\"hub-sec\" id=\"what-is-a-strand\"><div class=\"shell\"><div class=\"hub-sec-head\"><div><span class=\"eyebrow\">Why sharing strands is our main thing</span><h2>Sovereign Intelligence grows by sharing skills, not hoarding giant checkpoints</h2></div><a class=\"hub-btn hub-btn-sm\" href=\"/omega/helix/\">Read Omega Helix docs</a></div><div class=\"hub-grid3\"><article class=\"hub-story\"><h3>1. Keep your base intact</h3><p>Traditional fine-tuning mutates the whole model and causes catastrophic forgetting. A Helix Strand freezes the base trunk bit-for-bit and stores only the added specialist layers (~14 MB instead of 400+ MB).</p></article><article class=\"hub-story st-b\"><h3>2. Cryptographic Merkle proof</h3><p>Every <code>OLLMSP01</code> file carries its parent trunk hash (<code>base_hash</code>) and payload checksum (<code>pack_hash</code>). This hub verifies magic, lineage, finite weights, FNV-1a 64, and SHA-256 before storing a single byte.</p></article><article class=\"hub-story st-d\"><h3>3. Snap community skills together</h3><p>Download any 2 to 4 verified strands that share a base trunk, tune their blend weights in the <a href=\"/strands/clip\">Strand Clip Editor</a>, and fuse them live with <code>ollm spclip</code>&mdash;zero CUDA, zero retraining.</p></article></div>"
5079
5080text t_intro2 "<div class=\"hub-flow\"><div class=\"hub-step\"><span class=\"hub-step-num\">Step 1 &middot; Forge</span><h3>Train one specialist layer</h3><p>Grow a frozen-core layer with <code>ollm growpro</code>, train on your domain with <code>trainfc</code>, and extract the portable delta with <code>ollm spext BASE.ck SPEC.ck skill.sp</code>.</p></div><div class=\"hub-step\"><span class=\"hub-step-num\">Step 2 &middot; Publish</span><h3>Fail-closed verification</h3><p>Upload your <code>.sp</code> below under an open-source license (<code>Apache-2.0</code>, <code>MIT</code>, <code>CC-BY-4.0</code>). Interplane verifies every header field and weight, then indexes it by SHA-256.</p></div><div class=\"hub-step\"><span class=\"hub-step-num\">Step 3 &middot; Clip</span><h3>Edit &amp; combine (S=2..4)</h3><p>Filter strands sharing your <code>base_hash</code>, download in one click, or open the <a href=\"/strands/clip\">Clip Editor</a> to emit a SHA-256-verified <code>.spclip</code> recipe and runnable <code>ollm spclip</code> command.</p></div></div>"
5081
5082text t_intro3 "<details class=\"hub-details\"><summary>Technical anatomy: the 48-byte OLLMSP01 binary header verified on every upload</summary><div class=\"hub-details-body\"><p>Every uploaded strand must match the exact little-endian <code>OLLMSP01</code> wire layout used by <code>ollm spext</code> / <code>spapp</code> / <code>spclip</code>: bytes <code>0..7</code> magic <code>OLLMSP01</code>, <code>8..15</code> <code>base_L</code> (&ge;1), <code>16..23</code> <code>spec_L</code> (&gt;<code>base_L</code>), <code>24..31</code> Merkle parent <code>base_hash</code> (FNV-1a 64 over frozen base shapes &amp; weights), <code>32..39</code> <code>n_spec_params</code> (exact file length <code>48 + 8 * n_spec_params</code>), <code>40..47</code> payload <code>pack_hash</code> (FNV-1a 64), followed by finite IEEE-754 <code>f64</code> weights. Malformed, truncated, non-finite, or forged packs are rejected with HTTP 400 before touching storage.</p></div></details></div></section>\n"
5083
5084text t_cat_open "<section class=\"hub-sec\" id=\"catalog\"><div class=\"shell\"><div class=\"hub-sec-head\"><div><span class=\"eyebrow\">Content-addressed &middot; SHA-256 verified</span><h2>Verified Strand Catalog</h2></div><div class=\"hub-badges\"><a class=\"hub-btn hub-btn-sm\" href=\"/strands/index.json\">index.json</a><a class=\"hub-btn hub-btn-sm\" href=\"/strands/index.tsv\">index.tsv</a><a class=\"hub-btn hub-btn-sm hub-btn-emerald\" href=\"/strands/clip\">Edit spclip</a></div></div>\n<form class=\"hub-filter\" method=\"GET\" action=\"/strands/#catalog\"><input class=\"hub-in hub-in-grow\" type=\"text\" name=\"trunk\" placeholder=\"Filter by compatible base trunk (16-hex base_hash or 64-hex parent_trunk SHA-256)\" value=\""
5085text t_cat_f2 "\"><button class=\"hub-btn hub-btn-primary hub-btn-sm\" type=\"submit\">Filter trunk</button><a class=\"hub-btn hub-btn-sm\" href=\"/strands/#catalog\">Show all</a></form>\n"
5086
5087text t_up_open "<section class=\"hub-sec\" id=\"upload\"><div class=\"shell\"><div class=\"hub-sec-head\"><div><span class=\"eyebrow\">Open-source default &middot; Fail-closed gate</span><h2>Publish a Verified Helix Strand</h2></div><span class=\"hub-badge ok\">Max upload: "
5088text t_up_open2 " bytes</span></div>\n<form class=\"hub-card\" method=\"POST\" action=\"/strands/upload\" enctype=\"multipart/form-data\"><div class=\"hub-form-grid\"><div class=\"hub-fg full\"><label class=\"hub-lbl\" for=\"up-pack\">OLLMSP01 Specialist Pack (.sp)</label><input class=\"hub-in\" id=\"up-pack\" type=\"file\" name=\"pack\" accept=\".sp\" required><span class=\"hub-help\">Verified byte-for-byte before storage: magic, base_L &lt; spec_L, exact size, FNV-1a pack_hash, finite f64 weights, and FIPS 180-4 SHA-256.</span></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-name\">Strand Name</label><input class=\"hub-in\" id=\"up-name\" type=\"text\" name=\"name\" maxlength=\"80\" placeholder=\"om2-arith-specialist\" required></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-auth\">Author / Handle</label><input class=\"hub-in\" id=\"up-auth\" type=\"text\" name=\"author\" maxlength=\"64\" placeholder=\"drakestapleton\" required></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-lic\">Open-Source License (default)</label><select class=\"hub-sel\" id=\"up-lic\" name=\"license\"><option value=\"Apache-2.0\" selected>Apache-2.0 (Recommended)</option><option value=\"MIT\">MIT</option><option value=\"CC-BY-4.0\">CC-BY-4.0</option><option value=\"CC0-1.0\">CC0-1.0</option><option value=\"BSD-3-Clause\">BSD-3-Clause</option><option value=\"AGPL-3.0-only\">AGPL-3.0-only</option></select></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-pt\">Base Trunk SHA-256 / Parent Strand (optional)</label><input class=\"hub-in\" id=\"up-pt\" type=\"text\" name=\"parent_trunk\" maxlength=\"64\" placeholder=\"64-hex BASE.ck or parent strand SHA-256 (or 16-hex base_hash)\" value=\""
5089text t_up_mid "\"></div><div class=\"hub-fg full\"><label class=\"hub-lbl\" for=\"up-desc\">Plain-English Description</label><input class=\"hub-in\" id=\"up-desc\" type=\"text\" name=\"description\" maxlength=\"256\" placeholder=\"What skill does this strand add, and how was it verified?\"></div></div>"
5090
5091text t_up_ext1_pre "<details class=\"hub-details\""
5092text t_up_ext1_open " open"
5093text t_up_ext1_mid "><summary>Optional extensible metadata (sharing terms, provenance, expected SHA-256)</summary><div class=\"hub-details-body\"><div class=\"hub-form-grid\"><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-terms\">Sharing Terms</label><input class=\"hub-in\" id=\"up-terms\" type=\"text\" name=\"terms\" maxlength=\"128\" value=\"open-source\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"up-prov\">Provenance (repo commit, receipt, or spclip link)</label><input class=\"hub-in\" id=\"up-prov\" type=\"text\" name=\"provenance\" maxlength=\"160\" value=\""
5094text t_up_ext2 "\"></div><div class=\"hub-fg full\"><label class=\"hub-lbl\" for=\"up-exp\">Expected SHA-256 Pin (optional 64-hex integrity check)</label><input class=\"hub-in\" id=\"up-exp\" type=\"text\" name=\"expected_sha256\" maxlength=\"64\" placeholder=\"Optional 64-hex SHA-256 computed locally before upload\"></div></div></div></details><div class=\"hub-card-act\"><button class=\"hub-btn hub-btn-primary\" type=\"submit\">Verify &amp; Publish Strand</button></div></form>\n"
5095
5096text t_api_sec "<details class=\"hub-details\"><summary>CLI &amp; Agent API (curl upload + deterministic catalogs)</summary><div class=\"hub-details-body\"><pre class=\"hub-code\"># Upload a verified OLLMSP01 pack via curl:\ncurl -F pack=@skill.sp -F name=my-skill -F author=alice -F license=Apache-2.0 \\\n -F description=\"Specialist strand\" https://interplane.io/strands/upload\n\n# Query deterministic agent catalogs:\ncurl -s https://interplane.io/strands/index.tsv\ncurl -s https://interplane.io/strands/index.json\ncurl -s https://interplane.io/strands/clips/index.json</pre></div></details></div></section>\n"
5097
5098text t_ue_1 "No OLLMSP01 pack file was included in the upload."
5099text t_ue_2 "Both strand name and author are required."
5100text t_ue_3 "Rejected unsafe filename (path traversal, slash, or disallowed characters detected)."
5101text t_ue_4 "Uploaded pack exceeds the server bounded size limit."
5102text t_ue_5 "Pack is truncated (shorter than the 48-byte OLLMSP01 header)."
5103text t_ue_6 "Invalid magic header (expected exact OLLMSP01 bytes)."
5104text t_ue_7 "Invalid layer lineage (requires base_L >= 1, spec_L > base_L, spec_L <= 1024)."
5105text t_ue_8 "Invalid Merkle parent base_hash (zero or empty FNV-1a basis)."
5106text t_ue_9 "Invalid n_spec_params count (zero or exceeds bounded pack capacity)."
5107text t_ue_10 "Exact byte size mismatch (file length must equal 48 + 8 * n_spec_params; truncated or trailing bytes refused)."
5108text t_ue_11 "Payload tamper detected: computed FNV-1a 64 does not match header pack_hash (ollm rc=61)."
5109text t_ue_12 "Non-finite IEEE-754 f64 weight (NaN or Infinity) detected in specialist payload."
5110text t_ue_13 "Malformed parent_trunk (must be empty, exact 16-hex base_hash, or 64-hex SHA-256)."
5111text t_ue_14 "Merkle parent mismatch: supplied 16-hex parent_trunk disagrees with OLLMSP01 header base_hash."
5112text t_ue_15 "Broken Merkle chain lineage: child base_L does not match parent strand spec_L."
5113text t_ue_16 "SHA-256 mismatch: computed digest does not match expected_sha256."
5114text t_ue_17 "Duplicate strand: an identical SHA-256 pack is already verified in the catalog."
5115text t_ue_18 "Catalog storage capacity reached."
5116text t_ue_19 "Internal storage I/O error."
5117text t_ue_20 "Malformed multipart/form-data upload framing."
5118
5119text t_ce_1 "Select between 2 and 4 compatible verified strands to build a clip."
5120text t_ce_2 "One or more selected strand SHA-256 hashes were not found in the verified catalog."
5121text t_ce_3 "Incompatible parent trunks: all strands in a clip must share the exact same OLLMSP01 base_hash (and parent_trunk)."
5122text t_ce_4 "Invalid blend weights: every selected strand must have a non-zero milli-weight in -5000..5000 (strictly positive 1..5000 for MODE=1 Entropy Gate, and at least one positive weight for MODE=0 PoE)."
5123text t_ce_5 "Invalid fusion mode (must be 0 for Signed PoE or 1 for Entropy Gate)."
5124text t_ce_6 "Invalid temperature tau (must be 0..5000 milli-units)."
5125text t_ce_7 "Invalid splice layer (must be 1..min(base_L) across selected strands)."
5126text t_ce_8 "Invalid token count (must be 1..8192)."
5127text t_ce_9 "Manifest tamper detected: body_sha256 does not match canonical .spclip body."
5128text t_ce_10 "Malformed .spclip manifest (expected format=OMSPCLIP01 and trailing body_sha256)."
5129text t_ce_11 "Clip catalog storage capacity reached."
5130text t_ce_12 "Internal clip storage I/O error."
5131
5132fn r_upload_err_msg code ulong to void
5133 if eq code UE_MISSING_PACK
5134 r_txt t_ue_1
5135 ret
5136 end
5137 if eq code UE_MISSING_META
5138 r_txt t_ue_2
5139 ret
5140 end
5141 if eq code UE_BAD_FILENAME
5142 r_txt t_ue_3
5143 ret
5144 end
5145 if eq code UE_TOO_BIG
5146 r_txt t_ue_4
5147 ret
5148 end
5149 if eq code UE_TRUNCATED
5150 r_txt t_ue_5
5151 ret
5152 end
5153 if eq code UE_BAD_MAGIC
5154 r_txt t_ue_6
5155 ret
5156 end
5157 if eq code UE_BAD_LAYERS
5158 r_txt t_ue_7
5159 ret
5160 end
5161 if eq code UE_BAD_BASE_HASH
5162 r_txt t_ue_8
5163 ret
5164 end
5165 if eq code UE_BAD_PARAMS
5166 r_txt t_ue_9
5167 ret
5168 end
5169 if eq code UE_SIZE_MISMATCH
5170 r_txt t_ue_10
5171 ret
5172 end
5173 if eq code UE_PACK_HASH
5174 r_txt t_ue_11
5175 ret
5176 end
5177 if eq code UE_NONFINITE
5178 r_txt t_ue_12
5179 ret
5180 end
5181 if eq code UE_BAD_PARENT_TRUNK
5182 r_txt t_ue_13
5183 ret
5184 end
5185 if eq code UE_MERKLE_MISMATCH
5186 r_txt t_ue_14
5187 ret
5188 end
5189 if eq code UE_MERKLE_LINEAGE
5190 r_txt t_ue_15
5191 ret
5192 end
5193 if eq code UE_SHA_MISMATCH
5194 r_txt t_ue_16
5195 ret
5196 end
5197 if eq code UE_DUPLICATE
5198 r_txt t_ue_17
5199 ret
5200 end
5201 if eq code UE_STORAGE_FULL
5202 r_txt t_ue_18
5203 ret
5204 end
5205 if eq code UE_IO_FAIL
5206 r_txt t_ue_19
5207 ret
5208 end
5209 r_txt t_ue_20
5210end
5211
5212fn r_clip_err_msg code ulong to void
5213 if eq code CE_STRAND_COUNT
5214 r_txt t_ce_1
5215 ret
5216 end
5217 if eq code CE_UNKNOWN_STRAND
5218 r_txt t_ce_2
5219 ret
5220 end
5221 if eq code CE_INCOMPAT_TRUNK
5222 r_txt t_ce_3
5223 ret
5224 end
5225 if eq code CE_BAD_WEIGHT
5226 r_txt t_ce_4
5227 ret
5228 end
5229 if eq code CE_BAD_MODE
5230 r_txt t_ce_5
5231 ret
5232 end
5233 if eq code CE_BAD_TAU
5234 r_txt t_ce_6
5235 ret
5236 end
5237 if eq code CE_BAD_SPLICE
5238 r_txt t_ce_7
5239 ret
5240 end
5241 if eq code CE_BAD_TOKENS
5242 r_txt t_ce_8
5243 ret
5244 end
5245 if eq code CE_TAMPERED
5246 r_txt t_ce_9
5247 ret
5248 end
5249 if eq code CE_BAD_FORMAT
5250 r_txt t_ce_10
5251 ret
5252 end
5253 if eq code CE_STORAGE_FULL
5254 r_txt t_ce_11
5255 ret
5256 end
5257 r_txt t_ce_12
5258end
5259
5260fn render_page_head title view uchar * nav_which ulong to void
5261 r_txt t_h_doc1
5262 r_txt title
5263 r_txt t_h_doc2
5264 if eq nav_which 1
5265 r_txt t_h_cur
5266 end
5267 r_txt t_h_doc3
5268 if eq nav_which 2
5269 r_txt t_h_cur
5270 end
5271 r_txt t_h_doc4
5272end
5273
5274text t_bn_ok1 "<div class=\"shell\"><div class=\"hub-banner ok\"><h3>Verified &amp; Published: OLLMSP01 passed all cryptographic gates</h3><p>SHA-256: <code>"
5275text t_bn_ok2 "</code> &middot; Layers <code>L"
5276text t_bn_ok3 " &rarr; L"
5277text t_bn_ok4 "</code> &middot; Base trunk <code>"
5278text t_bn_ok5 "</code></p><div class=\"hub-card-act\"><a class=\"hub-btn hub-btn-sm hub-btn-primary\" href=\"/strands/strand/"
5279text t_bn_ok6 "\">Inspect receipt</a><a class=\"hub-btn hub-btn-sm\" href=\"/strands/pack/"
5280text t_bn_ok7 ".sp\">Download .sp</a><a class=\"hub-btn hub-btn-sm hub-btn-emerald\" href=\"/strands/clip?s1="
5281text t_bn_ok8 "\">Edit in Clip Studio</a></div></div></div>\n"
5282text t_bn_err1 "<div class=\"shell\"><div class=\"hub-banner err\"><h3>Upload Refused (Fail-Closed Gate &middot; Code "
5283text t_bn_err2 ")</h3><p>"
5284text t_bn_err3 "</p></div></div>\n"
5285
5286fn render_upload_banner has_post bool to void
5287 if not has_post
5288 ret
5289 end
5290 ec = get g_u_st US_ERR
5291 if eq ec UE_OK
5292 r_txt t_bn_ok1
5293 r_u_sha_all
5294 r_txt t_bn_ok2
5295 r_u64 get g_u_st US_BL
5296 r_txt t_bn_ok3
5297 r_u64 get g_u_st US_SL
5298 r_txt t_bn_ok4
5299 r_hex16 get g_u_st US_BHASH
5300 r_txt t_bn_ok5
5301 r_u_sha_all
5302 r_txt t_bn_ok6
5303 r_u_sha_all
5304 r_txt t_bn_ok7
5305 r_u_sha_all
5306 r_txt t_bn_ok8
5307 ret
5308 end
5309 r_txt t_bn_err1
5310 r_u64 ec
5311 r_txt t_bn_err2
5312 r_upload_err_msg ec
5313 r_txt t_bn_err3
5314end
5315
5316text t_card1 "<article class=\"hub-card\"><div class=\"hub-card-top\"><a class=\"hub-card-title\" href=\"/strands/strand/"
5317text t_card2 "\">"
5318text t_card3 "</a><div class=\"hub-badges\"><span class=\"hub-badge lic\">"
5319text t_card4 "</span><span class=\"hub-badge lay\">L"
5320text t_card5 "&rarr;L"
5321text t_card6 "</span><span class=\"hub-badge ok\">OLLMSP01</span></div></div><p>"
5322text t_card7 "</p><div class=\"hub-meta\"><span>by <strong>"
5323text t_card8 "</strong></span><span>trunk <a href=\"/strands/?trunk="
5324text t_card9 "#catalog\"><code>"
5325text t_card10 "</code></a></span><span>sha256 <code>"
5326text t_card11 "&hellip;</code></span><span>"
5327text t_card12 " params</span><span>"
5328text t_card13 " bytes</span><span>"
5329text t_card14 " downloads</span></div><pre class=\"hub-code\">ollm spclip BASE.ck 1 \"prompt\" 128 700 42 - 2 1000 1 "
5330text t_card15 " 1000 1 "
5331text t_card16 "PARTNER.sp"
5332text t_card17 "</pre><div class=\"hub-card-act\"><a class=\"hub-btn hub-btn-primary hub-btn-sm\" href=\"/strands/pack/"
5333text t_card18 ".sp\">Download .sp</a><a class=\"hub-btn hub-btn-sm\" href=\"/strands/strand/"
5334text t_card19 "\">Receipt &amp; CLI</a><a class=\"hub-btn hub-btn-emerald hub-btn-sm\" href=\"/strands/clip?trunk="
5335text t_card20 "&amp;s1="
5336text t_card21 "\">Edit spclip</a></div></article>\n"
5337
5338fn render_strand_card idx ulong to void
5339 r_txt t_card1
5340 r_c_sha idx 64
5341 r_txt t_card2
5342 r_c_name_esc idx
5343 r_txt t_card3
5344 r_c_lic_esc idx
5345 r_txt t_card4
5346 r_u64 get g_c_bl idx
5347 r_txt t_card5
5348 r_u64 get g_c_sl idx
5349 r_txt t_card6
5350 r_c_desc_esc idx
5351 r_txt t_card7
5352 r_c_auth_esc idx
5353 r_txt t_card8
5354 r_hex16 get g_c_bhash idx
5355 r_txt t_card9
5356 r_hex16 get g_c_bhash idx
5357 r_txt t_card10
5358 r_c_sha idx 16
5359 r_txt t_card11
5360 r_u64 get g_c_npar idx
5361 r_txt t_card12
5362 r_u64 get g_c_sz idx
5363 r_txt t_card13
5364 r_u64 get g_c_dl idx
5365 r_txt t_card14
5366 r_c_fn_esc idx
5367 r_txt t_card15
5368 partner = find_partner_strand idx
5369 if ge partner 0
5370 r_c_fn_esc ulong partner
5371 else
5372 r_txt t_card16
5373 end
5374 r_txt t_card17
5375 r_c_sha idx 64
5376 r_txt t_card18
5377 r_c_sha idx 64
5378 r_txt t_card19
5379 r_hex16 get g_c_bhash idx
5380 r_txt t_card20
5381 r_c_sha idx 64
5382 r_txt t_card21
5383end
5384
5385text t_pills_open "<div class=\"hub-pills\"><span class=\"hub-lbl\">Compatible trunks:</span>"
5386text t_pill_a "<a class=\"hub-pill\" href=\"/strands/?trunk="
5387text t_pill_b "#catalog\">"
5388text t_pill_c "</a>"
5389text t_pills_close "</div>\n"
5390text t_cards_open "<div class=\"hub-cards\">\n"
5391text t_cards_close "</div></div></section>\n"
5392text t_empty_cat "<div class=\"hub-empty\"><h3>No matching strands yet</h3><p>Publish the first verified <code>OLLMSP01</code> specialist pack using the form below, or clear your base-trunk filter.</p></div></div></section>\n"
5393
5394fn render_home_catalog to void
5395 r_txt t_cat_open
5396 for i ulong get g_q_trunkl 0 bound 64
5397 r_esc_b get g_q_trunk i
5398 end
5399 r_txt t_cat_f2
5400 nt = collect_distinct_trunks
5401 if gt nt 0
5402 show_nt = nt
5403 if gt show_nt 64
5404 show_nt = 64
5405 end
5406 r_txt t_pills_open
5407 for t ulong show_nt bound 64
5408 r_txt t_pill_a
5409 r_hex16 get g_seen_tr t
5410 r_txt t_pill_b
5411 r_hex16 get g_seen_tr t
5412 r_txt t_pill_c
5413 end
5414 r_txt t_pills_close
5415 end
5416 cnt = get g_scnt 0
5417 shown = ulong 0
5418 for i ulong cnt bound 512
5419 idx = sub sub cnt 1 i
5420 if strand_matches_filters idx
5421 if eq shown 0
5422 r_txt t_cards_open
5423 end
5424 render_strand_card idx
5425 shown = add shown 1
5426 end
5427 end
5428 if eq shown 0
5429 r_txt t_empty_cat
5430 else
5431 r_txt t_cards_close
5432 end
5433end
5434
5435fn render_home_page has_post bool to void
5436 r_reset
5437 render_page_head t_ttl_home 1
5438 r_txt t_hero1
5439 r_u64 get g_scnt 0
5440 r_txt t_hero2
5441 r_u64 collect_distinct_trunks
5442 r_txt t_hero3
5443 r_u64 get g_kcnt 0
5444 r_txt t_hero4
5445 r_u64 total_downloads
5446 r_txt t_hero5
5447 render_upload_banner has_post
5448 r_txt t_intro1
5449 r_txt t_intro2
5450 r_txt t_intro3
5451 render_home_catalog
5452 r_txt t_up_open
5453 r_u64 get g_max_up 0
5454 r_txt t_up_open2
5455 for i ulong get g_q_ptl 0 bound 64
5456 r_esc_b get g_q_pt i
5457 end
5458 r_txt t_up_mid
5459 r_txt t_up_ext1_pre
5460 if gt get g_q_clipl 0 0
5461 r_txt t_up_ext1_open
5462 end
5463 r_txt t_up_ext1_mid
5464 if gt get g_q_clipl 0 0
5465 r_txt t_spclip_prov_pre
5466 for i ulong get g_q_clipl 0 bound 64
5467 r_esc_b get g_q_clip i
5468 end
5469 end
5470 r_txt t_up_ext2
5471 r_txt t_api_sec
5472 r_txt t_h_foot
5473end
5474
5475note ---- Strand detail receipt page (/strands/strand/<sha256>) ----
5476
5477text t_det1 "<section class=\"hub-sec\"><div class=\"shell\"><div class=\"hub-sec-head\"><div><span class=\"eyebrow\">Verified OLLMSP01 Specialist Receipt</span><h1>"
5478text t_det2 "</h1></div><div class=\"hub-card-act\"><a class=\"hub-btn hub-btn-primary\" href=\"/strands/pack/"
5479text t_det3 ".sp\">Download .sp</a><a class=\"hub-btn hub-btn-emerald\" href=\"/strands/clip?trunk="
5480text t_det4 "&amp;s1="
5481text t_det5 "\">Combine in Clip Editor</a>"
5482text t_det5b "<a class=\"hub-btn\" href=\"/strands/#catalog\">Back to catalog</a></div></div>\n<div class=\"hub-card\"><p>"
5483text t_det6 "</p><table class=\"hub-table\"><tr><th>Content SHA-256</th><td><code>"
5484text t_det7 "</code></td></tr><tr><th>Author</th><td>"
5485text t_det8 "</td></tr><tr><th>License &amp; Terms</th><td><span class=\"hub-badge lic\">"
5486text t_det9 "</span> &middot; "
5487text t_det10 "</td></tr><tr><th>Layer Delta</th><td><code>base_L = "
5488text t_det11 "</code> &rarr; <code>spec_L = "
5489text t_det12 "</code></td></tr><tr><th>Merkle Base Hash</th><td><a href=\"/strands/?trunk="
5490text t_det13 "#catalog\"><code>"
5491text t_det14 "</code></a></td></tr><tr><th>Parent Trunk SHA-256</th><td><code>"
5492text t_det15 "</code></td></tr><tr><th>Payload FNV-1a 64</th><td><code>"
5493text t_det16 "</code></td></tr><tr><th>Parameters / Size</th><td>"
5494text t_det17 " f64 params &middot; "
5495text t_det18 " bytes &middot; "
5496text t_det19 " downloads</td></tr><tr><th>Provenance</th><td>"
5497text t_det20 "</td></tr></table><h3>Runnable Omega Helix Commands</h3><pre class=\"hub-code\"># 1. Attach single strand to frozen base checkpoint:\nollm spapp BASE.ck "
5498text t_det21 " OUT.ck\n\n# 2. Sample directly in memory:\nollm spchainsamp BASE.ck 128 700 \"fn main to int\" 42 - 1 "
5499text t_det22 "\n\n# 3. Combine with compatible sibling strand via spclip (S=2, Entropy Gate):\nollm spclip BASE.ck 1 \"fn main to int\" 128 700 42 - 2 1000 1 "
5500text t_det23 " 1000 1 "
5501text t_det24 "</pre></div></div></section>\n"
5502
5503text t_det_ch1 "<a class=\"hub-btn\" href=\"/strands/?parent_trunk="
5504text t_det_ch2 "#upload\">Chain child strand</a>"
5505text t_det_cl1 " &middot; <a href=\"/strands/clip/"
5506text t_det_cl2 ".spclip\">.spclip</a> (parents: "
5507text t_det_cl3 "<a href=\"/strands/strand/"
5508text t_det_cl4 "\"><code>"
5509text t_det_cl5 "</code></a>"
5510text t_det_cl6 ")"
5511text t_comma_sp ", "
5512
5513fn find_clip_from_c_prov idx ulong to long
5514 pl = get g_c_provl idx
5515 if lt pl add len t_spclip_prov_pre 64
5516 ret -1
5517 end
5518 base = mul idx F_PROV
5519 for i ulong len t_spclip_prov_pre bound 16
5520 if ne get g_c_prov add base i get t_spclip_prov_pre i
5521 ret -1
5522 end
5523 end
5524 off = add base len t_spclip_prov_pre
5525 for j ulong 64
5526 c = to_low get g_c_prov add off j
5527 if not is_hex_low c
5528 ret -1
5529 end
5530 put g_dpath j c
5531 end
5532 ret find_clip_by_dpath
5533end
5534
5535fn render_strand_detail idx ulong to void
5536 r_reset
5537 render_page_head t_ttl_det 1
5538 r_txt t_det1
5539 r_c_name_esc idx
5540 r_txt t_det2
5541 r_c_sha idx 64
5542 r_txt t_det3
5543 r_hex16 get g_c_bhash idx
5544 r_txt t_det4
5545 r_c_sha idx 64
5546 r_txt t_det5
5547 r_txt t_det_ch1
5548 r_c_sha idx 64
5549 r_txt t_det_ch2
5550 r_txt t_det5b
5551 r_c_desc_esc idx
5552 r_txt t_det6
5553 r_c_sha idx 64
5554 r_txt t_det7
5555 r_c_auth_esc idx
5556 r_txt t_det8
5557 r_c_lic_esc idx
5558 r_txt t_det9
5559 r_c_term_esc idx
5560 r_txt t_det10
5561 r_u64 get g_c_bl idx
5562 r_txt t_det11
5563 r_u64 get g_c_sl idx
5564 r_txt t_det12
5565 r_hex16 get g_c_bhash idx
5566 r_txt t_det13
5567 r_hex16 get g_c_bhash idx
5568 r_txt t_det14
5569 if gt get g_c_ptl idx 0
5570 r_c_pt_raw idx
5571 else
5572 r_txt t_dash
5573 end
5574 r_txt t_det15
5575 r_hex16 get g_c_phash idx
5576 r_txt t_det16
5577 r_u64 get g_c_npar idx
5578 r_txt t_det17
5579 r_u64 get g_c_sz idx
5580 r_txt t_det18
5581 r_u64 get g_c_dl idx
5582 r_txt t_det19
5583 if gt get g_c_provl idx 0
5584 r_c_prov_esc idx
5585 kidx = find_clip_from_c_prov idx
5586 if ge kidx 0
5587 uk = ulong kidx
5588 r_txt t_det_cl1
5589 r_k_sha uk 64
5590 r_txt t_det_cl2
5591 ksc = get g_k_sc uk
5592 for s ulong ksc bound 4
5593 if gt s 0
5594 r_txt t_comma_sp
5595 end
5596 r_txt t_det_cl3
5597 sb = mul add mul uk 4 s 64
5598 for j ulong 64
5599 r_b get g_k_ssha add sb j
5600 end
5601 r_txt t_det_cl4
5602 for j2 ulong 12
5603 r_b get g_k_ssha add sb j2
5604 end
5605 r_txt t_det_cl5
5606 end
5607 r_txt t_det_cl6
5608 end
5609 else
5610 r_txt t_dash
5611 end
5612 r_txt t_det20
5613 r_c_fn_esc idx
5614 r_txt t_det21
5615 r_c_fn_esc idx
5616 r_txt t_det22
5617 r_c_fn_esc idx
5618 r_txt t_det23
5619 partner = find_partner_strand idx
5620 if ge partner 0
5621 r_c_fn_esc ulong partner
5622 else
5623 r_txt t_card16
5624 end
5625 r_txt t_det24
5626 r_txt t_h_foot
5627end
5628
5629note ---- Strand Clip Editor UI (/strands/clip) ----
5630
5631text t_cl_hero "<section class=\"hub-hero\"><div class=\"shell\"><span class=\"eyebrow\">Strand Clip Studio &middot; OMSPCLIP01 &middot; Zero JS</span><h1>Edit &amp; Compose <em>spclip</em> Recipes</h1><p class=\"lede\">Select 2 to 4 verified strands sharing a common base trunk, tune per-strand blend weights (including negative Anti-DNA firewalls in PoE mode), fusion mode, temperature <code>tau</code>, splice layer, and fine-tune parameters. Every recipe produces a deterministic SHA-256-verified <code>.spclip</code> manifest and runnable CLI command.</p><div class=\"hub-cta\"><a class=\"hub-btn\" href=\"/strands/#catalog\">Back to Strand Catalog</a><a class=\"hub-btn\" href=\"/strands/clips/index.json\">clips/index.json</a><a class=\"hub-btn\" href=\"/strands/clips/index.tsv\">clips/index.tsv</a></div></div></section>\n"
5632
5633text t_cl_ok1 "<div class=\"shell\"><div class=\"hub-banner ok\"><h3>Verified .spclip Recipe Created (OMSPCLIP01)</h3><p>Clip SHA-256: <code>"
5634text t_cl_ok2 "</code> &middot; Body SHA-256: <code>"
5635text t_cl_ok3 "</code> &middot; Shared Trunk: <code>"
5636text t_cl_ok4 "</code></p><div class=\"hub-card-act\"><a class=\"hub-btn hub-btn-primary hub-btn-sm\" href=\"/strands/clip/"
5637text t_cl_ok5 ".spclip\">Download .spclip manifest</a><a class=\"hub-btn hub-btn-emerald hub-btn-sm\" href=\"/strands/?parent_trunk="
5638text t_cl_ok6 "&amp;clip="
5639text t_cl_ok7 "#upload\">Publish fine-tuned child strand linked to this clip</a></div><h3>Runnable ollm spclip Command</h3><pre class=\"hub-code\">ollm spclip BASE.ck "
5640text t_cl_ok8 "</pre><details class=\"hub-details\"><summary>Optional: Fine-tune &amp; extract a child OLLMSP01 strand from this clip</summary><div class=\"hub-details-body\"><pre class=\"hub-code\">"
5641text t_cl_ft_m1 "# 1. Materialize starting child layers from your positive same-depth clip weights:\nollm spmerge BASE.ck 0 clip_init.sp "
5642text t_cl_ft_m2 "\nollm spapp BASE.ck clip_init.sp child.ck"
5643text t_cl_ft_a1 "# 1. Negative Anti-DNA weights or asymmetric strand depths fuse live via ollm spclip above.\n# To fine-tune a child strand, start from the lead positive strand:\nollm spapp BASE.ck "
5644text t_cl_ft_a2 " child.ck"
5645text t_cl_ok9 "\n# 2. Frozen-core fine-tune above splice layer "
5646text t_cl_ok10 ":\nollm trainf child.ck DATA.tok "
5647text t_cl_ok12 "\n# 3. Extract verified child OLLMSP01 pack &amp; upload linked to parent hashes:\nollm spext BASE.ck child.ck child_tuned.sp</pre></div></details><details class=\"hub-details\"><summary>Deterministic .spclip Manifest Preview (OMSPCLIP01)</summary><div class=\"hub-details-body\"><pre class=\"hub-code\">"
5648text t_cl_ok13 "</pre></div></details></div></div>\n"
5649
5650text t_cl_err1 "<div class=\"shell\"><div class=\"hub-banner err\"><h3>Clip Validation Refused (Code "
5651text t_cl_err2 ")</h3><p>"
5652text t_cl_err3 "</p></div></div>\n"
5653
5654fn r_e_sha_all to void
5655 for i ulong 64
5656 r_b get g_e_sha i
5657 end
5658end
5659
5660fn r_e_bsha_all to void
5661 for i ulong 64
5662 r_b get g_e_bsha i
5663 end
5664end
5665
5666fn render_clip_banner to void
5667 if eq get g_e_st ES_HAS_POST 0
5668 ret
5669 end
5670 ec = get g_e_st ES_ERR
5671 if ne ec CE_OK
5672 r_txt t_cl_err1
5673 r_u64 ec
5674 r_txt t_cl_err2
5675 r_clip_err_msg ec
5676 r_txt t_cl_err3
5677 ret
5678 end
5679 r_txt t_cl_ok1
5680 r_e_sha_all
5681 r_txt t_cl_ok2
5682 r_e_bsha_all
5683 r_txt t_cl_ok3
5684 r_hex16 get g_e_st ES_BHASH
5685 r_txt t_cl_ok4
5686 r_e_sha_all
5687 r_txt t_cl_ok5
5688 r_hex16 get g_e_st ES_BHASH
5689 r_txt t_cl_ok6
5690 r_e_sha_all
5691 r_txt t_cl_ok7
5692 r_u64 get g_e_st ES_MODE
5693 r_b 32
5694 r_b 34
5695 for pi ulong get g_e_promptl 0 bound 256
5696 r_esc_b get g_e_prompt pi
5697 end
5698 r_b 34
5699 r_b 32
5700 r_u64 get g_e_st ES_TOK
5701 r_b 32
5702 r_u64 get g_e_st ES_TAU
5703 r_b 32
5704 r_u64 get g_e_st ES_SEED
5705 r_b 32
5706 for mi ulong get g_e_mergesl 0 bound 128
5707 r_esc_b get g_e_merges mi
5708 end
5709 r_b 32
5710 sc = get g_e_st ES_SC
5711 r_u64 sc
5712 for k ulong sc bound 4
5713 r_b 32
5714 r_i64 get g_e_sw k
5715 r_b 32
5716 r_b 49
5717 r_b 32
5718 r_c_fn_esc get g_e_sidx k
5719 end
5720 r_txt t_cl_ok8
5721 can_merge = true
5722 sl0 = get g_c_sl get g_e_sidx 0
5723 best_k = ulong 0
5724 best_w = long -99999
5725 for k ulong sc bound 4
5726 wk = get g_e_sw k
5727 if le wk 0
5728 can_merge = false
5729 end
5730 if ne get g_c_sl get g_e_sidx k sl0
5731 can_merge = false
5732 end
5733 if gt wk best_w
5734 best_w = wk
5735 best_k = k
5736 end
5737 end
5738 if can_merge
5739 r_txt t_cl_ft_m1
5740 for k ulong sc bound 4
5741 if gt k 0
5742 r_b 32
5743 end
5744 r_i64 get g_e_sw k
5745 r_b 32
5746 r_c_fn_esc get g_e_sidx k
5747 end
5748 r_txt t_cl_ft_m2
5749 else
5750 r_txt t_cl_ft_a1
5751 r_c_fn_esc get g_e_sidx best_k
5752 r_txt t_cl_ft_a2
5753 end
5754 r_txt t_cl_ok9
5755 r_u64 get g_e_st ES_SPLICE
5756 r_txt t_cl_ok10
5757 st = get g_e_st ES_STEPS
5758 if eq st 0
5759 st = 100
5760 end
5761 ev = ulong 50
5762 if lt st ev
5763 ev = st
5764 end
5765 r_u64 st
5766 r_b 32
5767 r_u64 ev
5768 r_b 32
5769 r_u64 get g_e_st ES_SPLICE
5770 r_txt t_cl_ok12
5771 for j ulong get g_e_manl 0 bound 16384
5772 r_esc_b get g_e_man j
5773 end
5774 r_txt t_cl_ok13
5775end
5776
5777text t_opt_none "<option value=\"\">-- Optional / None --</option>\n"
5778text t_opt_pick "<option value=\"\">-- Select a verified strand --</option>\n"
5779text t_opt_a "<option value=\""
5780text t_opt_sel "\" selected>"
5781text t_opt_b "\">"
5782text t_opt_c " [trunk "
5783text t_opt_d " | L"
5784text t_opt_e "&rarr;L"
5785text t_opt_f "]</option>\n"
5786
5787fn strand_preselected slot ulong idx ulong to bool
5788 if ne get g_e_st ES_HAS_POST 0
5789 if eq get g_e_sset slot 1
5790 ret sha_eq_cat_eslot idx slot
5791 end
5792 ret false
5793 end
5794 if and eq slot 0 eq get g_q_s1l 0 64
5795 bs = mul idx F_SHA
5796 for i ulong 64
5797 if ne get g_c_sha add bs i get g_q_s1 i
5798 ret false
5799 end
5800 end
5801 ret true
5802 end
5803 if and eq slot 1 eq get g_q_s2l 0 64
5804 bs = mul idx F_SHA
5805 for i ulong 64
5806 if ne get g_c_sha add bs i get g_q_s2 i
5807 ret false
5808 end
5809 end
5810 ret true
5811 end
5812 ret false
5813end
5814
5815fn render_strand_options slot ulong req bool to void
5816 if req
5817 r_txt t_opt_pick
5818 else
5819 r_txt t_opt_none
5820 end
5821 cnt = get g_scnt 0
5822 for i ulong cnt bound 512
5823 if strand_matches_trunk i
5824 r_txt t_opt_a
5825 r_c_sha i 64
5826 if strand_preselected slot i
5827 r_txt t_opt_sel
5828 else
5829 r_txt t_opt_b
5830 end
5831 r_c_name_esc i
5832 r_txt t_opt_c
5833 r_hex16 get g_c_bhash i
5834 r_txt t_opt_d
5835 r_u64 get g_c_bl i
5836 r_txt t_opt_e
5837 r_u64 get g_c_sl i
5838 r_txt t_opt_f
5839 end
5840 end
5841end
5842
5843text t_slot1 "<div class=\"hub-slot\"><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"s"
5844text t_slot2 "\">Strand "
5845text t_slot3 "</label><select class=\"hub-sel\" id=\"s"
5846text t_slot4 "\" name=\"s"
5847text t_slot5 "\">"
5848text t_slot6 "</select></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"w"
5849text t_slot7 "\">Weight w"
5850text t_slot8 " (milli)</label><input class=\"hub-in\" id=\"w"
5851text t_slot9 "\" type=\"number\" name=\"w"
5852text t_slot10 "\" min=\"-5000\" max=\"5000\" value=\""
5853text t_slot11 "\"></div></div>\n"
5854
5855fn render_clip_slot slot ulong req bool to void
5856 num = add slot 1
5857 r_txt t_slot1
5858 r_u64 num
5859 r_txt t_slot2
5860 r_u64 num
5861 r_txt t_slot3
5862 r_u64 num
5863 r_txt t_slot4
5864 r_u64 num
5865 r_txt t_slot5
5866 render_strand_options slot req
5867 r_txt t_slot6
5868 r_u64 num
5869 r_txt t_slot7
5870 r_u64 num
5871 r_txt t_slot8
5872 r_u64 num
5873 r_txt t_slot9
5874 r_u64 num
5875 r_txt t_slot10
5876 r_i64 get g_e_sw slot
5877 r_txt t_slot11
5878end
5879
5880text t_cp_open "<section class=\"hub-sec\"><div class=\"shell\"><div class=\"hub-pills\"><span class=\"hub-lbl\">Filter compatible strands by trunk:</span><a class=\"hub-pill\" href=\"/strands/clip\">All trunks</a>"
5881text t_cp_pa "<a class=\"hub-pill\" href=\"/strands/clip?trunk="
5882text t_cp_pb "\">"
5883text t_cp_pc "</a>"
5884text t_cp_close "</div>\n<form class=\"hub-card\" method=\"POST\" action=\"/strands/clip\"><h2>1. Choose 2&ndash;4 Compatible Strands &amp; Blend Weights</h2><p class=\"hub-help\">Order of slots 1&ndash;4 defines branch order. All selected strands must share the same OLLMSP01 <code>base_hash</code>. Use negative weights (e.g. <code>-500</code>) in MODE=0 PoE for Anti-DNA contrastive firewalls.</p>\n"
5885text t_cp_p2a "<h2>2. Edit Fusion Mode, Temperature, Splice Layer &amp; Fine-Tune Metadata</h2><div class=\"hub-form-grid\"><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-mode\">Fusion Mode</label><select class=\"hub-sel\" id=\"c-mode\" name=\"mode\"><option value=\"1\""
5886text t_cp_p2b ">MODE=1 &middot; Entropy-Gated Multi-Helix</option><option value=\"0\""
5887text t_cp_p2c ">MODE=0 &middot; Signed Product-of-Experts (PoE / Anti-DNA)</option></select></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-tau\">Temperature / Sharpness tau (milli, 0..5000)</label><input class=\"hub-in\" id=\"c-tau\" type=\"number\" name=\"tau\" min=\"0\" max=\"5000\" value=\""
5888text t_opt_msel " selected"
5889text t_cp_p3 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-sp\">Splice Layer (0 = auto min base_L)</label><input class=\"hub-in\" id=\"c-sp\" type=\"number\" name=\"splice_layer\" min=\"0\" max=\"1024\" value=\""
5890text t_cp_p4 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-tok\">Generate Tokens (N, 1..8192)</label><input class=\"hub-in\" id=\"c-tok\" type=\"number\" name=\"tokens\" min=\"1\" max=\"8192\" value=\""
5891text t_cp_p5 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-name\">Clip Name</label><input class=\"hub-in\" id=\"c-name\" type=\"text\" name=\"clip_name\" maxlength=\"80\" value=\""
5892text t_cp_p6 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-auth\">Author</label><input class=\"hub-in\" id=\"c-auth\" type=\"text\" name=\"author\" maxlength=\"64\" value=\""
5893text t_cp_p7a "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-lic\">License</label><select class=\"hub-sel\" id=\"c-lic\" name=\"license\">"
5894text t_cp_p7b "</select></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-seed\">Seed (ft_seed)</label><input class=\"hub-in\" id=\"c-seed\" type=\"number\" name=\"ft_seed\" min=\"0\" value=\""
5895text t_lic_ap "Apache-2.0"
5896text t_lic_mit "MIT"
5897text t_lic_ccby "CC-BY-4.0"
5898text t_lic_cc0 "CC0-1.0"
5899text t_lic_bsd "BSD-3-Clause"
5900text t_lic_agpl "AGPL-3.0-only"
5901text t_opt_end "</option>"
5902
5903fn e_lic_eq pat view uchar * to bool
5904 if ne get g_e_licl 0 len pat
5905 ret false
5906 end
5907 for i ulong len pat bound 32
5908 if ne get g_e_lic i get pat i
5909 ret false
5910 end
5911 end
5912 ret true
5913end
5914
5915fn render_clip_lic_opt pat view uchar * to void
5916 r_txt t_opt_a
5917 r_txt pat
5918 if e_lic_eq pat
5919 r_txt t_opt_sel
5920 else
5921 r_txt t_opt_b
5922 end
5923 r_txt pat
5924 r_txt t_opt_end
5925end
5926text t_cp_p8 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-steps\">Optional Fine-Tune Steps (ft_steps)</label><input class=\"hub-in\" id=\"c-steps\" type=\"number\" name=\"ft_steps\" min=\"0\" max=\"1000000\" value=\""
5927text t_cp_p9 "\"></div><div class=\"hub-fg\"><label class=\"hub-lbl\" for=\"c-lr\">Fine-Tune LR micro-units (ft_lr_micro)</label><input class=\"hub-in\" id=\"c-lr\" type=\"number\" name=\"ft_lr_micro\" min=\"0\" max=\"1000000\" value=\""
5928text t_cp_p10 "\"></div><div class=\"hub-fg full\"><label class=\"hub-lbl\" for=\"c-pr\">Prompt</label><input class=\"hub-in\" id=\"c-pr\" type=\"text\" name=\"prompt\" maxlength=\"256\" value=\""
5929text t_cp_p11 "\"></div></div><div class=\"hub-card-act\"><button class=\"hub-btn hub-btn-primary\" type=\"submit\" name=\"action\" value=\"save\">Compose &amp; Verify .spclip Recipe</button><button class=\"hub-btn hub-btn-emerald\" type=\"submit\" name=\"action\" value=\"download\">Download .spclip Directly</button></div></form>\n"
5930
5931text t_rc_open "</div></section><section class=\"hub-sec\"><div class=\"shell\"><div class=\"hub-sec-head\"><div><span class=\"eyebrow\">SHA-256 content-addressed</span><h2>Saved .spclip Recipes</h2></div></div><div class=\"hub-cards\">\n"
5932text t_rc_1 "<article class=\"hub-card\"><div class=\"hub-card-top\"><span class=\"hub-card-title\">"
5933text t_rc_2 "</span><div class=\"hub-badges\"><span class=\"hub-badge lic\">"
5934text t_rc_3 "</span><span class=\"hub-badge lay\">S="
5935text t_rc_4 " &middot; MODE="
5936text t_rc_5 "</span></div></div><div class=\"hub-meta\"><span>by "
5937text t_rc_6 "</span><span>trunk <code>"
5938text t_rc_7 "</code></span><span>tau="
5939text t_rc_8 "</span><span>splice=L"
5940text t_rc_9 "</span><span>sha256 <code>"
5941text t_rc_10 "&hellip;</code></span></div><div class=\"hub-card-act\"><a class=\"hub-btn hub-btn-primary hub-btn-sm\" href=\"/strands/clip/"
5942text t_rc_11 ".spclip\">Download .spclip</a><a class=\"hub-btn hub-btn-sm\" href=\"/strands/?parent_trunk="
5943text t_rc_12 "&amp;clip="
5944text t_rc_13 "#upload\">Upload child strand</a></div></article>\n"
5945text t_rc_close "</div></div></section>\n"
5946
5947text t_sec_close "</div></section>\n"
5948
5949fn render_saved_clips to void
5950 kc = get g_kcnt 0
5951 if eq kc 0
5952 r_txt t_sec_close
5953 ret
5954 end
5955 r_txt t_rc_open
5956 for i ulong kc bound 256
5957 idx = sub sub kc 1 i
5958 r_txt t_rc_1
5959 r_k_name_esc idx
5960 r_txt t_rc_2
5961 r_k_lic_esc idx
5962 r_txt t_rc_3
5963 r_u64 get g_k_sc idx
5964 r_txt t_rc_4
5965 r_u64 get g_k_mode idx
5966 r_txt t_rc_5
5967 r_k_auth_esc idx
5968 r_txt t_rc_6
5969 r_hex16 get g_k_bhash idx
5970 r_txt t_rc_7
5971 r_u64 get g_k_tau idx
5972 r_txt t_rc_8
5973 r_u64 get g_k_splice idx
5974 r_txt t_rc_9
5975 r_k_sha idx 16
5976 r_txt t_rc_10
5977 r_k_sha idx 64
5978 r_txt t_rc_11
5979 r_hex16 get g_k_bhash idx
5980 r_txt t_rc_12
5981 r_k_sha idx 64
5982 r_txt t_rc_13
5983 end
5984 r_txt t_rc_close
5985end
5986
5987fn render_clip_editor to void
5988 r_reset
5989 render_page_head t_ttl_clip 2
5990 r_txt t_cl_hero
5991 render_clip_banner
5992 r_txt t_cp_open
5993 nt = collect_distinct_trunks
5994 show_nt = nt
5995 if gt show_nt 64
5996 show_nt = 64
5997 end
5998 for t ulong show_nt bound 64
5999 r_txt t_cp_pa
6000 r_hex16 get g_seen_tr t
6001 r_txt t_cp_pb
6002 r_hex16 get g_seen_tr t
6003 r_txt t_cp_pc
6004 end
6005 r_txt t_cp_close
6006 render_clip_slot 0 true
6007 render_clip_slot 1 true
6008 render_clip_slot 2 false
6009 render_clip_slot 3 false
6010 r_txt t_cp_p2a
6011 if ne get g_e_st ES_MODE 0
6012 r_txt t_opt_msel
6013 end
6014 r_txt t_cp_p2b
6015 if eq get g_e_st ES_MODE 0
6016 r_txt t_opt_msel
6017 end
6018 r_txt t_cp_p2c
6019 r_u64 get g_e_st ES_TAU
6020 r_txt t_cp_p3
6021 r_u64 get g_e_st ES_SPLICE
6022 r_txt t_cp_p4
6023 r_u64 get g_e_st ES_TOK
6024 r_txt t_cp_p5
6025 for i ulong get g_e_namel 0 bound 80
6026 r_esc_b get g_e_name i
6027 end
6028 r_txt t_cp_p6
6029 for i ulong get g_e_authl 0 bound 64
6030 r_esc_b get g_e_auth i
6031 end
6032 r_txt t_cp_p7a
6033 render_clip_lic_opt t_lic_ap
6034 render_clip_lic_opt t_lic_mit
6035 render_clip_lic_opt t_lic_ccby
6036 render_clip_lic_opt t_lic_cc0
6037 render_clip_lic_opt t_lic_bsd
6038 render_clip_lic_opt t_lic_agpl
6039 r_txt t_cp_p7b
6040 r_u64 get g_e_st ES_SEED
6041 r_txt t_cp_p8
6042 r_u64 get g_e_st ES_STEPS
6043 r_txt t_cp_p9
6044 r_u64 get g_e_st ES_LR
6045 r_txt t_cp_p10
6046 for i ulong get g_e_promptl 0 bound 256
6047 r_esc_b get g_e_prompt i
6048 end
6049 r_txt t_cp_p11
6050 render_saved_clips
6051 r_txt t_h_foot
6052end
6053
6054note ---- Agent API renderers (TSV and RFC-8259 JSON) ----
6055
6056text t_j_open "{\"schema\":\"omega.strand_hub.v1\",\"hub\":\"interplane-strands\",\"count\":"
6057text t_j_arr ",\"strands\":["
6058text t_j_s1 "{\"sha256\":\""
6059text t_j_s2 "\",\"name\":\""
6060text t_j_s3 "\",\"author\":\""
6061text t_j_s4 "\",\"license\":\""
6062text t_j_s5 "\",\"terms\":\""
6063text t_j_s6 "\",\"provenance\":\""
6064text t_j_s7 "\",\"parent_trunk\":\""
6065text t_j_s8 "\",\"base_hash\":\""
6066text t_j_s9 "\",\"base_l\":"
6067text t_j_s10 ",\"spec_l\":"
6068text t_j_s11 ",\"n_spec_params\":"
6069text t_j_s12 ",\"pack_hash\":\""
6070text t_j_s13 "\",\"size_bytes\":"
6071text t_j_s14 ",\"downloads\":"
6072text t_j_s15 ",\"uploaded_ns\":"
6073text t_j_s16 ",\"filename\":\""
6074text t_j_s17 "\",\"description\":\""
6075text t_j_s18 "\",\"download_url\":\"/strands/pack/"
6076text t_j_s19 ".sp\"}"
6077text t_j_close "]}\n"
6078
6079fn render_index_tsv to void
6080 r_reset
6081 r_txt t_cat_hdr2
6082 cnt = get g_scnt 0
6083 for i ulong cnt bound 512
6084 if strand_matches_filters i
6085 emit_cat_row_resp i
6086 end
6087 end
6088end
6089
6090fn render_one_strand_json idx ulong to void
6091 r_txt t_j_s1
6092 r_c_sha idx 64
6093 r_txt t_j_s2
6094 r_c_name_json idx
6095 r_txt t_j_s3
6096 r_c_auth_json idx
6097 r_txt t_j_s4
6098 r_c_lic_json idx
6099 r_txt t_j_s5
6100 r_c_term_json idx
6101 r_txt t_j_s6
6102 r_c_prov_json idx
6103 r_txt t_j_s7
6104 r_c_pt_raw idx
6105 r_txt t_j_s8
6106 r_hex16 get g_c_bhash idx
6107 r_txt t_j_s9
6108 r_u64 get g_c_bl idx
6109 r_txt t_j_s10
6110 r_u64 get g_c_sl idx
6111 r_txt t_j_s11
6112 r_u64 get g_c_npar idx
6113 r_txt t_j_s12
6114 r_hex16 get g_c_phash idx
6115 r_txt t_j_s13
6116 r_u64 get g_c_sz idx
6117 r_txt t_j_s14
6118 r_u64 get g_c_dl idx
6119 r_txt t_j_s15
6120 r_u64 get g_c_ts idx
6121 r_txt t_j_s16
6122 r_c_fn_json idx
6123 r_txt t_j_s17
6124 r_c_desc_json idx
6125 r_txt t_j_s18
6126 r_c_sha idx 64
6127 r_txt t_j_s19
6128end
6129
6130fn render_index_json to void
6131 r_reset
6132 cnt = get g_scnt 0
6133 matched = ulong 0
6134 for i ulong cnt bound 512
6135 if strand_matches_filters i
6136 matched = add matched 1
6137 end
6138 end
6139 r_txt t_j_open
6140 r_u64 matched
6141 r_txt t_j_arr
6142 first = true
6143 for i ulong cnt bound 512
6144 if strand_matches_filters i
6145 if not first
6146 r_b 44
6147 end
6148 render_one_strand_json i
6149 first = false
6150 end
6151 end
6152 r_txt t_j_close
6153end
6154
6155text t_kj_open "{\"schema\":\"omega.spclip_hub.v1\",\"count\":"
6156text t_kj_arr ",\"clips\":["
6157text t_kj_1 "{\"clip_sha256\":\""
6158text t_kj_2 "\",\"name\":\""
6159text t_kj_3 "\",\"author\":\""
6160text t_kj_4 "\",\"license\":\""
6161text t_kj_5 "\",\"base_hash\":\""
6162text t_kj_6 "\",\"mode\":"
6163text t_kj_7 ",\"tau_milli\":"
6164text t_kj_8 ",\"splice_layer\":"
6165text t_kj_9 ",\"strand_count\":"
6166text t_kj_10 ",\"downloads\":"
6167text t_kj_10b ",\"tokens\":"
6168text t_kj_10c ",\"ft_seed\":"
6169text t_kj_10d ",\"ft_steps\":"
6170text t_kj_10e ",\"ft_lr_micro\":"
6171text t_kj_10f ",\"strands\":["
6172text t_kj_s1 "{\"sha256\":\""
6173text t_kj_s2 "\",\"weight_milli\":"
6174text t_kj_s3 "}"
6175text t_kj_11 "],\"download_url\":\"/strands/clip/"
6176text t_kj_12 ".spclip\"}"
6177
6178fn clip_matches_trunk idx ulong to bool
6179 ql = get g_q_trunkl 0
6180 if eq ql 0
6181 ret true
6182 end
6183 if gt ql 16
6184 ret false
6185 end
6186 fmt_hex16 get g_k_bhash idx
6187 for i ulong ql bound 16
6188 if ne to_low get g_q_trunk i get g_hex16 i
6189 ret false
6190 end
6191 end
6192 ret true
6193end
6194
6195fn render_clips_tsv to void
6196 r_reset
6197 r_txt t_kcat_hdr2
6198 cnt = get g_kcnt 0
6199 for i ulong cnt bound 256
6200 if clip_matches_trunk i
6201 emit_kcat_row_resp i
6202 end
6203 end
6204end
6205
6206fn render_one_clip_json i ulong to void
6207 r_txt t_kj_1
6208 r_k_sha i 64
6209 r_txt t_kj_2
6210 r_k_name_json i
6211 r_txt t_kj_3
6212 r_k_auth_json i
6213 r_txt t_kj_4
6214 r_k_lic_json i
6215 r_txt t_kj_5
6216 r_hex16 get g_k_bhash i
6217 r_txt t_kj_6
6218 r_u64 get g_k_mode i
6219 r_txt t_kj_7
6220 r_u64 get g_k_tau i
6221 r_txt t_kj_8
6222 r_u64 get g_k_splice i
6223 r_txt t_kj_9
6224 sc = get g_k_sc i
6225 r_u64 sc
6226 r_txt t_kj_10
6227 r_u64 get g_k_dl i
6228 r_txt t_kj_10b
6229 r_u64 get g_k_tok i
6230 r_txt t_kj_10c
6231 r_u64 get g_k_seed i
6232 r_txt t_kj_10d
6233 r_u64 get g_k_steps i
6234 r_txt t_kj_10e
6235 r_u64 get g_k_lr i
6236 r_txt t_kj_10f
6237 for s ulong sc bound 4
6238 if gt s 0
6239 r_b 44
6240 end
6241 r_txt t_kj_s1
6242 sb = mul add mul i 4 s 64
6243 for j ulong 64
6244 r_b get g_k_ssha add sb j
6245 end
6246 r_txt t_kj_s2
6247 r_i64 get g_k_sw add mul i 4 s
6248 r_txt t_kj_s3
6249 end
6250 r_txt t_kj_11
6251 r_k_sha i 64
6252 r_txt t_kj_12
6253end
6254
6255fn render_clips_json to void
6256 r_reset
6257 cnt = get g_kcnt 0
6258 matched = ulong 0
6259 for i ulong cnt bound 256
6260 if clip_matches_trunk i
6261 matched = add matched 1
6262 end
6263 end
6264 r_txt t_kj_open
6265 r_u64 matched
6266 r_txt t_kj_arr
6267 first = true
6268 for i ulong cnt bound 256
6269 if clip_matches_trunk i
6270 if not first
6271 r_b 44
6272 end
6273 render_one_clip_json i
6274 first = false
6275 end
6276 end
6277 r_txt t_j_close
6278end
6279
6280text t_vj_ok1 "{\"valid\":true,\"clip_sha256\":\""
6281text t_vj_ok2 "\",\"body_sha256\":\""
6282text t_vj_ok3 "\",\"base_hash\":\""
6283text t_vj_ok4 "\",\"strand_count\":"
6284text t_vj_ok5 ",\"mode\":"
6285text t_vj_ok6 ",\"tau_milli\":"
6286text t_vj_ok7 ",\"splice_layer\":"
6287text t_vj_ok8 "}\n"
6288text t_vj_err1 "{\"valid\":false,\"error_code\":"
6289text t_vj_err2 ",\"error\":\""
6290text t_vj_err3 "\"}\n"
6291
6292fn render_clip_verify_json ok bool to void
6293 r_reset
6294 if ok
6295 r_txt t_vj_ok1
6296 r_e_sha_all
6297 r_txt t_vj_ok2
6298 r_e_bsha_all
6299 r_txt t_vj_ok3
6300 r_hex16 get g_e_st ES_BHASH
6301 r_txt t_vj_ok4
6302 r_u64 get g_e_st ES_SC
6303 r_txt t_vj_ok5
6304 r_u64 get g_e_st ES_MODE
6305 r_txt t_vj_ok6
6306 r_u64 get g_e_st ES_TAU
6307 r_txt t_vj_ok7
6308 r_u64 get g_e_st ES_SPLICE
6309 r_txt t_vj_ok8
6310 ret
6311 end
6312 r_txt t_vj_err1
6313 r_u64 get g_e_st ES_ERR
6314 r_txt t_vj_err2
6315 r_clip_err_msg get g_e_st ES_ERR
6316 r_txt t_vj_err3
6317end
6318
6319note ---- Socket transmission, file streaming, and HTTP route dispatcher ----
6320
6321fn send_hdr_buf fd int to void
6322 n = get g_hdrl 0
6323 off = ulong 0
6324 dl = add long monons HUB_IO_TMO
6325 for it ulong 20000 bound 20000
6326 if ge off n
6327 ret
6328 end
6329 left = sub dl long monons
6330 if le left 0
6331 ret
6332 end
6333 w = waitio fd 4 left
6334 if le w 0
6335 if eq w -4
6336 continue
6337 end
6338 ret
6339 end
6340 k = send fd g_hdr off sub n off 16384
6341 if lt k 0
6342 if or eq k -4 eq k -11
6343 continue
6344 end
6345 ret
6346 end
6347 if eq k 0
6348 ret
6349 end
6350 off = add off ulong k
6351 end
6352end
6353
6354fn send_resp_buf fd int to void
6355 n = get g_respl 0
6356 off = ulong 0
6357 dl = add long monons HUB_IO_TMO
6358 for it ulong 2000000 bound 2000000
6359 if ge off n
6360 ret
6361 end
6362 left = sub dl long monons
6363 if le left 0
6364 ret
6365 end
6366 w = waitio fd 4 left
6367 if le w 0
6368 if eq w -4
6369 continue
6370 end
6371 ret
6372 end
6373 k = send fd g_resp off sub n off 16384
6374 if lt k 0
6375 if or eq k -4 eq k -11
6376 continue
6377 end
6378 ret
6379 end
6380 if eq k 0
6381 ret
6382 end
6383 off = add off ulong k
6384 end
6385end
6386
6387fn send_io_chunk fd int n ulong to bool
6388 off = ulong 0
6389 dl = add long monons HUB_IO_TMO
6390 for it ulong 200000 bound 200000
6391 if ge off n
6392 ret true
6393 end
6394 left = sub dl long monons
6395 if le left 0
6396 ret false
6397 end
6398 w = waitio fd 4 left
6399 if le w 0
6400 if eq w -4
6401 continue
6402 end
6403 ret false
6404 end
6405 k = send fd g_io off sub n off 16384
6406 if lt k 0
6407 if or eq k -4 eq k -11
6408 continue
6409 end
6410 ret false
6411 end
6412 if eq k 0
6413 ret false
6414 end
6415 off = add off ulong k
6416 end
6417 ret ge off n
6418end
6419
6420fn h_status_line code ulong to void
6421 if eq code 201
6422 h_txt t_http_201
6423 ret
6424 end
6425 if eq code 400
6426 h_txt t_http_400
6427 ret
6428 end
6429 if eq code 404
6430 h_txt t_http_404
6431 ret
6432 end
6433 if eq code 405
6434 h_txt t_http_405
6435 ret
6436 end
6437 if eq code 409
6438 h_txt t_http_409
6439 ret
6440 end
6441 if eq code 411
6442 h_txt t_http_411
6443 ret
6444 end
6445 if eq code 413
6446 h_txt t_http_413
6447 ret
6448 end
6449 if eq code 415
6450 h_txt t_http_415
6451 ret
6452 end
6453 if eq code 500
6454 h_txt t_http_500
6455 ret
6456 end
6457 if eq code 507
6458 h_txt t_http_507
6459 ret
6460 end
6461 h_txt t_http_200
6462end
6463
6464note ct_kind: 0=html 1=css 2=txt 3=tsv 4=json 5=bin
6465fn h_ct_line ct_kind ulong to void
6466 if eq ct_kind 1
6467 h_txt t_ct_css
6468 h_crlf
6469 h_txt t_cc_css
6470 ret
6471 end
6472 if eq ct_kind 2
6473 h_txt t_ct_txt
6474 else
6475 if eq ct_kind 3
6476 h_txt t_ct_tsv
6477 else
6478 if eq ct_kind 4
6479 h_txt t_ct_json
6480 else
6481 if eq ct_kind 5
6482 h_txt t_ct_bin
6483 else
6484 h_txt t_ct_html
6485 end
6486 end
6487 end
6488 end
6489 h_crlf
6490 h_txt t_cc_no
6491end
6492
6493fn send_resp fd int code ulong ct_kind ulong head_only bool to void
6494 h_reset
6495 h_status_line code
6496 h_crlf
6497 h_ct_line ct_kind
6498 h_crlf
6499 h_txt t_nosniff
6500 h_crlf
6501 h_txt t_cl_pre
6502 h_u64 get g_respl 0
6503 h_crlf
6504 h_txt t_conn_close
6505 h_crlf
6506 h_crlf
6507 send_hdr_buf fd
6508 if not head_only
6509 send_resp_buf fd
6510 end
6511end
6512
6513fn send_resp_xsha fd int code ulong ct_kind ulong kind ulong to void
6514 h_reset
6515 h_status_line code
6516 h_crlf
6517 h_ct_line ct_kind
6518 h_crlf
6519 h_txt t_nosniff
6520 h_crlf
6521 if eq kind 1
6522 h_txt t_xsha_pre
6523 for i ulong 64
6524 h_b get g_u_sha i
6525 end
6526 h_crlf
6527 h_txt t_xbh_pre
6528 h_hex16 get g_u_st US_BHASH
6529 h_crlf
6530 end
6531 if eq kind 2
6532 h_txt t_xksha_pre
6533 for i ulong 64
6534 h_b get g_e_sha i
6535 end
6536 h_crlf
6537 end
6538 h_txt t_cl_pre
6539 h_u64 get g_respl 0
6540 h_crlf
6541 h_txt t_conn_close
6542 h_crlf
6543 h_crlf
6544 send_hdr_buf fd
6545 send_resp_buf fd
6546end
6547
6548fn extract_hex64_with_ext p0 ulong p1 ulong plen ulong ext view uchar * to bool
6549 el = len ext
6550 if ne sub p1 p0 add add plen 64 el
6551 ret false
6552 end
6553 s0 = add p0 plen
6554 for i ulong 64
6555 c = get g_req add s0 i
6556 if not is_hex_low c
6557 ret false
6558 end
6559 put g_dpath i c
6560 end
6561 e0 = add s0 64
6562 for j ulong el bound 16
6563 if ne get g_req add e0 j get ext j
6564 ret false
6565 end
6566 end
6567 ret true
6568end
6569
6570fn extract_hex64_bare p0 ulong p1 ulong plen ulong to bool
6571 if ne sub p1 p0 add plen 64
6572 ret false
6573 end
6574 s0 = add p0 plen
6575 for i ulong 64
6576 c = get g_req add s0 i
6577 if not is_hex_low c
6578 ret false
6579 end
6580 put g_dpath i c
6581 end
6582 ret true
6583end
6584
6585fn stream_file_to_sock sockfd int ffd int tot ulong to void
6586 pos = ulong 0
6587 for it ulong 200000 bound 200000
6588 if ge pos tot
6589 ret
6590 end
6591 want = sub tot pos
6592 if gt want HUB_IO_SZ
6593 want = HUB_IO_SZ
6594 end
6595 if not pread_exact ffd pos want
6596 ret
6597 end
6598 if not send_io_chunk sockfd want
6599 ret
6600 end
6601 pos = add pos want
6602 end
6603end
6604
6605fn build_pack_dl_hdr uidx ulong fsz ulong to void
6606 h_reset
6607 h_status_line 200
6608 h_crlf
6609 h_ct_line 5
6610 h_crlf
6611 h_txt t_nosniff
6612 h_crlf
6613 h_txt t_cd_pre
6614 bf = mul uidx F_FN
6615 for i ulong get g_c_fnl uidx bound 128
6616 c = get g_c_fn add bf i
6617 if is_safe_fn_char c
6618 h_b c
6619 end
6620 end
6621 h_txt t_cd_suf
6622 h_crlf
6623 h_txt t_xsha_pre
6624 for i ulong 64
6625 h_b get g_dpath i
6626 end
6627 h_crlf
6628 h_txt t_xbh_pre
6629 h_hex16 get g_c_bhash uidx
6630 h_crlf
6631 h_txt t_cl_pre
6632 h_u64 fsz
6633 h_crlf
6634 h_txt t_conn_close
6635 h_crlf
6636 h_crlf
6637end
6638
6639fn handle_pack_download fd int p0 ulong p1 ulong head_only bool to void
6640 if not extract_hex64_with_ext p0 p1 len t_rt_pack_pre t_ext_sp
6641 r_reset
6642 r_txt t_400_body
6643 send_resp fd 400 2 head_only
6644 ret
6645 end
6646 idx = find_strand_by_dpath
6647 if lt idx 0
6648 r_reset
6649 r_txt t_404_body
6650 send_resp fd 404 2 head_only
6651 ret
6652 end
6653 p1_reset
6654 p1_dir
6655 p1_txt t_sub_blobs
6656 p1_txt t_slash
6657 for i ulong 64
6658 p1_b get g_dpath i
6659 end
6660 p1_txt t_ext_sp
6661 bfd = fopen g_p1 0 get g_p1l 0 0 0
6662 if lt bfd 0
6663 r_reset
6664 r_txt t_404_body
6665 send_resp fd 404 2 head_only
6666 ret
6667 end
6668 fsz = fseek int bfd 0 2
6669 if lt fsz 0
6670 x = fclose int bfd
6671 r_reset
6672 r_txt t_404_body
6673 send_resp fd 404 2 head_only
6674 ret
6675 end
6676 if not head_only
6677 put g_c_dl ulong idx add get g_c_dl ulong idx 1
6678 sv = save_catalog
6679 end
6680 build_pack_dl_hdr ulong idx ulong fsz
6681 send_hdr_buf fd
6682 if not head_only
6683 stream_file_to_sock fd int bfd ulong fsz
6684 end
6685 x = fclose int bfd
6686end
6687
6688fn build_clip_dl_hdr fsz ulong to void
6689 h_reset
6690 h_status_line 200
6691 h_crlf
6692 h_ct_line 2
6693 h_crlf
6694 h_txt t_nosniff
6695 h_crlf
6696 h_txt t_cd_pre
6697 for i ulong 16
6698 h_b get g_dpath i
6699 end
6700 h_txt t_ext_spclip
6701 h_txt t_cd_suf
6702 h_crlf
6703 h_txt t_xksha_pre
6704 for i ulong 64
6705 h_b get g_dpath i
6706 end
6707 h_crlf
6708 h_txt t_cl_pre
6709 h_u64 fsz
6710 h_crlf
6711 h_txt t_conn_close
6712 h_crlf
6713 h_crlf
6714end
6715
6716fn handle_clip_download fd int p0 ulong p1 ulong head_only bool to void
6717 if not extract_hex64_with_ext p0 p1 len t_rt_clip_pre t_ext_spclip
6718 r_reset
6719 r_txt t_400_body
6720 send_resp fd 400 2 head_only
6721 ret
6722 end
6723 idx = find_clip_by_dpath
6724 if lt idx 0
6725 r_reset
6726 r_txt t_404_body
6727 send_resp fd 404 2 head_only
6728 ret
6729 end
6730 p1_reset
6731 p1_dir
6732 p1_txt t_sub_clips
6733 p1_txt t_slash
6734 for i ulong 64
6735 p1_b get g_dpath i
6736 end
6737 p1_txt t_ext_spclip
6738 cfd = fopen g_p1 0 get g_p1l 0 0 0
6739 if lt cfd 0
6740 r_reset
6741 r_txt t_404_body
6742 send_resp fd 404 2 head_only
6743 ret
6744 end
6745 fsz = fseek int cfd 0 2
6746 if lt fsz 0
6747 x = fclose int cfd
6748 r_reset
6749 r_txt t_404_body
6750 send_resp fd 404 2 head_only
6751 ret
6752 end
6753 if not head_only
6754 put g_k_dl ulong idx add get g_k_dl ulong idx 1
6755 sv = save_clips_catalog
6756 end
6757 build_clip_dl_hdr ulong fsz
6758 send_hdr_buf fd
6759 if not head_only
6760 stream_file_to_sock fd int cfd ulong fsz
6761 end
6762 x = fclose int cfd
6763end
6764
6765fn copy_req_tail_to_vbuf hend ulong clen ulong to ulong
6766 avail = ulong 0
6767 reql = get g_reql 0
6768 if gt reql hend
6769 avail = sub reql hend
6770 end
6771 if gt avail clen
6772 avail = clen
6773 end
6774 for i ulong avail bound 16384
6775 put g_e_vbuf i get g_req add hend i
6776 end
6777 ret avail
6778end
6779
6780fn read_small_body_vbuf sockfd int hend ulong clen ulong to bool
6781 if gt clen 16384
6782 ret false
6783 end
6784 done = copy_req_tail_to_vbuf hend clen
6785 dl = add long monons HUB_IO_TMO
6786 for it ulong 40000 bound 40000
6787 if ge done clen
6788 break
6789 end
6790 left = sub dl long monons
6791 if le left 0
6792 ret false
6793 end
6794 w = waitio sockfd 1 left
6795 if le w 0
6796 if eq w -4
6797 continue
6798 end
6799 ret false
6800 end
6801 r = recv sockfd g_e_vbuf done sub clen done 0
6802 if lt r 0
6803 if or eq r -4 eq r -11
6804 continue
6805 end
6806 ret false
6807 end
6808 if eq r 0
6809 ret false
6810 end
6811 done = add done ulong r
6812 end
6813 put g_e_vbufl 0 done
6814 ret eq done clen
6815end
6816
6817fn handle_strand_upload fd int to void
6818 if ne get g_rq_st RQ_BAD_HDR 0
6819 r_reset
6820 r_txt t_400_body
6821 send_resp fd 400 2 false
6822 ret
6823 end
6824 if eq get g_rq_st RQ_HAS_CL 0
6825 r_reset
6826 r_txt t_411_body
6827 send_resp fd 411 2 false
6828 ret
6829 end
6830 clen = get g_rq_st RQ_CLEN
6831 ct = get g_rq_st RQ_CTYPE
6832 if and ne ct 1 ne ct 3
6833 r_reset
6834 r_txt t_400_body
6835 send_resp fd 415 2 false
6836 ret
6837 end
6838 lim = add get g_max_up 0 HUB_HDR_OVERHEAD
6839 if eq ct 3
6840 lim = get g_max_up 0
6841 end
6842 if gt clen lim
6843 put g_u_st US_ERR UE_TOO_BIG
6844 render_home_page true
6845 send_resp fd 413 0 false
6846 ret
6847 end
6848 if ne get g_rq_st RQ_EXPECT 0
6849 h_reset
6850 h_txt t_http_100
6851 h_crlf
6852 h_crlf
6853 send_hdr_buf fd
6854 end
6855 build_tmp_p1 t_tmp_raw_pre t_ext_raw
6856 for i ulong get g_p1l 0 bound 1024
6857 put g_tmp_raw i get g_p1 i
6858 end
6859 put g_tmp_rawl 0 get g_p1l 0
6860 rawfd = fopen g_tmp_raw 0 get g_tmp_rawl 0 578 384
6861 if lt rawfd 0
6862 put g_u_st US_ERR UE_IO_FAIL
6863 render_home_page true
6864 send_resp fd 500 0 false
6865 ret
6866 end
6867 if not stream_body_to_file fd get g_rq_st RQ_HEND clen int rawfd
6868 x = fclose int rawfd
6869 u = unlink g_tmp_raw 0 get g_tmp_rawl 0
6870 put g_u_st US_ERR UE_TRUNCATED
6871 render_home_page true
6872 send_resp fd 400 0 false
6873 ret
6874 end
6875 ok_parse = true
6876 if eq ct 1
6877 ok_parse = parse_multipart_file int rawfd clen
6878 else
6879 put g_u_st US_HAS_PACK 1
6880 put g_u_st US_POFF 0
6881 put g_u_st US_PLEN clen
6882 end
6883 ok_ver = false
6884 if ok_parse
6885 ok_ver = verify_and_store_pack int rawfd
6886 else
6887 put g_u_st US_HTTP_CODE 400
6888 end
6889 x = fclose int rawfd
6890 u = unlink g_tmp_raw 0 get g_tmp_rawl 0
6891 code = get g_u_st US_HTTP_CODE
6892 if eq code 0
6893 code = 400
6894 end
6895 render_home_page true
6896 if or ok_ver eq code 409
6897 send_resp_xsha fd code 0 1
6898 else
6899 send_resp fd code 0 false
6900 end
6901end
6902
6903fn handle_clip_post fd int to void
6904 if ne get g_rq_st RQ_BAD_HDR 0
6905 r_reset
6906 r_txt t_400_body
6907 send_resp fd 400 2 false
6908 ret
6909 end
6910 if eq get g_rq_st RQ_HAS_CL 0
6911 r_reset
6912 r_txt t_411_body
6913 send_resp fd 411 2 false
6914 ret
6915 end
6916 clen = get g_rq_st RQ_CLEN
6917 if gt clen 16384
6918 r_reset
6919 r_txt t_413_body
6920 send_resp fd 413 2 false
6921 ret
6922 end
6923 if ne get g_rq_st RQ_EXPECT 0
6924 h_reset
6925 h_txt t_http_100
6926 h_crlf
6927 h_crlf
6928 send_hdr_buf fd
6929 end
6930 if not read_small_body_vbuf fd get g_rq_st RQ_HEND clen
6931 r_reset
6932 r_txt t_400_body
6933 send_resp fd 400 2 false
6934 ret
6935 end
6936 reset_clip_edit
6937 put g_e_st ES_HAS_POST 1
6938 parse_clip_form_vbuf
6939 ok = compose_and_store_clip
6940 if not ok
6941 render_clip_editor
6942 ec = get g_e_st ES_ERR
6943 hcode = ulong 400
6944 if eq ec CE_STORAGE_FULL
6945 hcode = 507
6946 else
6947 if eq ec CE_IO_FAIL
6948 hcode = 500
6949 end
6950 end
6951 send_resp fd hcode 0 false
6952 ret
6953 end
6954 if eq get g_e_st ES_ACTION 1
6955 r_reset
6956 for i ulong get g_e_manl 0 bound 16384
6957 r_b get g_e_man i
6958 end
6959 h_reset
6960 h_status_line 200
6961 h_crlf
6962 h_ct_line 2
6963 h_crlf
6964 h_txt t_nosniff
6965 h_crlf
6966 h_txt t_cd_pre
6967 for i ulong 16
6968 h_b get g_e_sha i
6969 end
6970 h_txt t_ext_spclip
6971 h_txt t_cd_suf
6972 h_crlf
6973 h_txt t_xksha_pre
6974 for i ulong 64
6975 h_b get g_e_sha i
6976 end
6977 h_crlf
6978 h_txt t_cl_pre
6979 h_u64 get g_respl 0
6980 h_crlf
6981 h_txt t_conn_close
6982 h_crlf
6983 h_crlf
6984 send_hdr_buf fd
6985 send_resp_buf fd
6986 ret
6987 end
6988 render_clip_editor
6989 send_resp_xsha fd 201 0 2
6990end
6991
6992fn handle_clip_verify fd int to void
6993 if ne get g_rq_st RQ_BAD_HDR 0
6994 r_reset
6995 r_txt t_400_body
6996 send_resp fd 400 2 false
6997 ret
6998 end
6999 if eq get g_rq_st RQ_HAS_CL 0
7000 r_reset
7001 r_txt t_411_body
7002 send_resp fd 411 2 false
7003 ret
7004 end
7005 clen = get g_rq_st RQ_CLEN
7006 if gt clen 16384
7007 r_reset
7008 r_txt t_413_body
7009 send_resp fd 413 2 false
7010 ret
7011 end
7012 if ne get g_rq_st RQ_EXPECT 0
7013 h_reset
7014 h_txt t_http_100
7015 h_crlf
7016 h_crlf
7017 send_hdr_buf fd
7018 end
7019 if not read_small_body_vbuf fd get g_rq_st RQ_HEND clen
7020 r_reset
7021 r_txt t_400_body
7022 send_resp fd 400 2 false
7023 ret
7024 end
7025 ok = verify_submitted_spclip
7026 render_clip_verify_json ok
7027 if ok
7028 send_resp_xsha fd 200 4 2
7029 else
7030 send_resp fd 400 4 false
7031 end
7032end
7033
7034fn route_get_head fd int p0 ulong p1 ulong head_only bool to void
7035 if req_range_eq p0 p1 t_rt_css
7036 r_reset
7037 r_txt t_css1
7038 r_txt t_css2
7039 r_txt t_css3
7040 send_resp fd 200 1 head_only
7041 ret
7042 end
7043 if req_range_eq p0 p1 t_rt_health
7044 r_reset
7045 r_txt t_ok_body
7046 send_resp fd 200 2 head_only
7047 ret
7048 end
7049 if req_range_eq p0 p1 t_rt_itsv
7050 render_index_tsv
7051 send_resp fd 200 3 head_only
7052 ret
7053 end
7054 if req_range_eq p0 p1 t_rt_ijson
7055 render_index_json
7056 send_resp fd 200 4 head_only
7057 ret
7058 end
7059 if req_range_eq p0 p1 t_rt_ktsv
7060 render_clips_tsv
7061 send_resp fd 200 3 head_only
7062 ret
7063 end
7064 if req_range_eq p0 p1 t_rt_kjson
7065 render_clips_json
7066 send_resp fd 200 4 head_only
7067 ret
7068 end
7069 if req_range_starts p0 p1 t_rt_pack_pre
7070 handle_pack_download fd p0 p1 head_only
7071 ret
7072 end
7073 if req_range_starts p0 p1 t_rt_clip_pre
7074 handle_clip_download fd p0 p1 head_only
7075 ret
7076 end
7077 if req_range_starts p0 p1 t_rt_strand_pre
7078 if not extract_hex64_bare p0 p1 len t_rt_strand_pre
7079 r_reset
7080 r_txt t_400_body
7081 send_resp fd 400 2 head_only
7082 ret
7083 end
7084 idx = find_strand_by_dpath
7085 if lt idx 0
7086 r_reset
7087 r_txt t_404_body
7088 send_resp fd 404 2 head_only
7089 ret
7090 end
7091 render_strand_detail ulong idx
7092 send_resp fd 200 0 head_only
7093 ret
7094 end
7095 if req_range_eq p0 p1 t_rt_clip
7096 reset_clip_edit
7097 render_clip_editor
7098 send_resp fd 200 0 head_only
7099 ret
7100 end
7101 if or req_range_eq p0 p1 t_rt_root or req_range_eq p0 p1 t_rt_root2 req_range_eq p0 p1 t_rt_slash
7102 render_home_page false
7103 send_resp fd 200 0 head_only
7104 ret
7105 end
7106 r_reset
7107 r_txt t_404_body
7108 send_resp fd 404 2 head_only
7109end
7110
7111fn serve_conn fd int to void
7112 if not read_http_headers fd
7113 ret
7114 end
7115 if not parse_req_headers
7116 r_reset
7117 r_txt t_405_body
7118 send_resp fd 405 2 false
7119 ret
7120 end
7121 if ne get g_rq_st RQ_BAD_HDR 0
7122 r_reset
7123 r_txt t_400_body
7124 send_resp fd 400 2 eq get g_rq_st RQ_METH 2
7125 ret
7126 end
7127 parse_query_string
7128 meth = get g_rq_st RQ_METH
7129 p0 = get g_rq_st RQ_P0
7130 p1 = get g_rq_st RQ_P1
7131 if eq meth 3
7132 if or req_range_eq p0 p1 t_rt_upload or req_range_eq p0 p1 t_rt_root req_range_eq p0 p1 t_rt_root2
7133 handle_strand_upload fd
7134 ret
7135 end
7136 if req_range_eq p0 p1 t_rt_clip
7137 handle_clip_post fd
7138 ret
7139 end
7140 if req_range_eq p0 p1 t_rt_clip_ver
7141 handle_clip_verify fd
7142 ret
7143 end
7144 r_reset
7145 r_txt t_404_body
7146 send_resp fd 404 2 false
7147 ret
7148 end
7149 route_get_head fd p0 p1 eq meth 2
7150end
7151
7152note ---- CLI init, TCP listener, and main loop ----
7153
7154fn parse_arg_u64 ai ulong defv ulong to ulong
7155 if le ulong argcnt ai
7156 ret defv
7157 end
7158 buf = new uchar 32 0
7159 k = argread ai buf 0 32
7160 if le k 0
7161 ret defv
7162 end
7163 v = ulong 0
7164 for i ulong ulong k bound 32
7165 c = get buf i
7166 if or lt c 48 gt c 57
7167 ret defv
7168 end
7169 v = add mul v 10 ulong sub c 48
7170 end
7171 if eq v 0
7172 ret defv
7173 end
7174 ret v
7175end
7176
7177fn init_config to void
7178 p = parse_arg_u64 2 HUB_DEF_PORT
7179 if gt p 65535
7180 p = HUB_DEF_PORT
7181 end
7182 put g_port 0 p
7183 put g_max_up 0 parse_arg_u64 3 HUB_DEF_MAX_UP
7184 if gt argcnt 1
7185 k = argread 1 g_dir 0 511
7186 if gt k 0
7187 put g_dirl 0 ulong k
7188 ret
7189 end
7190 end
7191 for i ulong len t_def_dir bound 512
7192 put g_dir i get t_def_dir i
7193 end
7194 put g_dirl 0 len t_def_dir
7195end
7196
7197fn make_listener to long
7198 fd = socknew 2 526337 0
7199 if lt fd 0
7200 ret fd
7201 end
7202 one = new uchar 4 0
7203 put one 0 1
7204 r = sockopt int fd 1 2 one 0 4
7205 port = get g_port 0
7206 a = new uchar 16 0
7207 put a 0 2
7208 put a 2 uchar and shr port 8 255
7209 put a 3 uchar and port 255
7210 put a 4 uchar and shr HUB_IP 24 255
7211 put a 5 uchar and shr HUB_IP 16 255
7212 put a 6 uchar and shr HUB_IP 8 255
7213 put a 7 uchar and HUB_IP 255
7214 if eq r 0
7215 r = bind int fd a 0 16
7216 end
7217 if eq r 0
7218 r = listen int fd 64
7219 end
7220 if ne r 0
7221 x = fclose int fd
7222 ret r
7223 end
7224 ret fd
7225end
7226
7227fn log_startup ok bool to void
7228 r_reset
7229 if ok
7230 r_txt t_started
7231 else
7232 r_txt t_listen_err
7233 end
7234 r_u64 get g_port 0
7235 if ok
7236 r_txt t_dir_msg
7237 for i ulong get g_dirl 0 bound 512
7238 r_b get g_dir i
7239 end
7240 end
7241 r_txt t_nl
7242 x = write 2 g_resp 0 get g_respl 0
7243end
7244
7245fn log_dir_err to void
7246 r_reset
7247 r_txt t_dir_err
7248 for i ulong get g_dirl 0 bound 512
7249 r_b get g_dir i
7250 end
7251 r_txt t_nl
7252 x = write 2 g_resp 0 get g_respl 0
7253end
7254
7255fn main to int
7256 init_config
7257 if not ensure_dirs
7258 log_dir_err
7259 ret 3
7260 end
7261 load_catalog
7262 load_clips_catalog
7263 lfd = make_listener
7264 if lt lfd 0
7265 log_startup false
7266 ret 2
7267 end
7268 log_startup true
7269 for it ulong 4000000000 bound 4000000000
7270 w = waitio int lfd 1 1000000000
7271 if gt w 0
7272 cfd = accept int lfd 526336
7273 if ge cfd 0
7274 serve_conn int cfd
7275 x = fclose int cfd
7276 end
7277 end
7278 end
7279 ret 0
7280end