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
omega-mv / apps / omega-projects / lib.om
| 1 | note omega-projects: pure Omega project validator, TSV metadata parser, and HTML index emitter. |
| 2 | note Ported from upstream omega-projects (.github/check_projects.py and index.html). |
| 3 | note Compose with si/directives/om2.om before this file. |
| 4 | |
| 5 | const OP_MAX_SLUG ulong 40 |
| 6 | const OP_MAX_TITLE ulong 120 |
| 7 | const OP_MAX_DESC ulong 400 |
| 8 | const OP_MAX_SAVED ulong 32 |
| 9 | |
| 10 | struct OpMeta h_off ulong h_len ulong s_off ulong s_len ulong t_off ulong t_len ulong d_off ulong d_len ulong v_off ulong v_len ulong valid ulong end |
| 11 | |
| 12 | text op_t_anon "anon-" |
| 13 | text op_t_projects "projects/" |
| 14 | text op_t_idxhtml "index.html" |
| 15 | text op_t_readme "README.md" |
| 16 | text op_t_metajson "meta.json" |
| 17 | text op_t_amp "&" |
| 18 | text op_t_lt "<" |
| 19 | text op_t_gt ">" |
| 20 | text op_t_quot """ |
| 21 | text op_t_apos "'" |
| 22 | |
| 23 | text op_t_ghp "ghp_" |
| 24 | text op_t_gho "gho_" |
| 25 | text op_t_ghu "ghu_" |
| 26 | text op_t_ghs "ghs_" |
| 27 | text op_t_ghr "ghr_" |
| 28 | text op_t_ghpat "github_pat_" |
| 29 | text op_t_sk "sk-" |
| 30 | text op_t_akia "AKIA" |
| 31 | text op_t_aiza "AIza" |
| 32 | text op_t_privkey "PRIVATE KEY-----" |
| 33 | text op_t_jwt "eyJ" |
| 34 | |
| 35 | text op_t_ext_script "<script" |
| 36 | text op_t_ext_iframe "<iframe" |
| 37 | text op_t_ext_link "<link" |
| 38 | text op_t_ext_object "<object" |
| 39 | text op_t_ext_embed "<embed" |
| 40 | text op_t_ext_base "<base" |
| 41 | text op_t_ext_http "http://" |
| 42 | text op_t_ext_https "https://" |
| 43 | text op_t_ext_fetch "fetch(" |
| 44 | text op_t_ext_xhr "xmlhttprequest" |
| 45 | text op_t_ext_ws "websocket" |
| 46 | text op_t_ext_beacon "sendbeacon" |
| 47 | |
| 48 | text op_t_page_head "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self'; script-src 'none'; style-src 'unsafe-inline'\">\n<title>Omega projects</title>\n<style>\nbody{font:16px/1.5 system-ui,sans-serif;max-width:44rem;margin:2rem auto;padding:0 1rem;color:#1b1b1b}\na{color:#0b57d0}\n</style>\n</head>\n<body>\n<h1>Omega projects</h1>\n<ul id=\"projects\">\n" |
| 49 | text op_t_page_empty "<li>No projects are live yet.</li>\n" |
| 50 | text op_t_page_foot "</ul>\n<p>Each live project is served at <code>projects/<handle>/<project>/</code>.</p>\n</body>\n</html>\n" |
| 51 | |
| 52 | text op_t_card_a "<li class=\"project-card\" data-handle=\"" |
| 53 | text op_t_card_b "\" data-slug=\"" |
| 54 | text op_t_card_c "\"><a href=\"projects/" |
| 55 | text op_t_card_d "/\"><strong>" |
| 56 | text op_t_card_e "</strong></a> <span class=\"meta\">by " |
| 57 | text op_t_card_f " (" |
| 58 | text op_t_card_g ")</span><p>" |
| 59 | text op_t_card_h "</p></li>\n" |
| 60 | |
| 61 | fn op_is_lower c uchar to bool |
| 62 | if lt c 97 |
| 63 | ret false |
| 64 | end |
| 65 | if gt c 122 |
| 66 | ret false |
| 67 | end |
| 68 | ret true |
| 69 | end |
| 70 | |
| 71 | fn op_is_digit c uchar to bool |
| 72 | if lt c 48 |
| 73 | ret false |
| 74 | end |
| 75 | if gt c 57 |
| 76 | ret false |
| 77 | end |
| 78 | ret true |
| 79 | end |
| 80 | |
| 81 | fn op_is_alnum_lower c uchar to bool |
| 82 | if op_is_lower c |
| 83 | ret true |
| 84 | end |
| 85 | ret op_is_digit c |
| 86 | end |
| 87 | |
| 88 | fn op_is_hex_lower c uchar to bool |
| 89 | if op_is_digit c |
| 90 | ret true |
| 91 | end |
| 92 | if lt c 97 |
| 93 | ret false |
| 94 | end |
| 95 | if gt c 102 |
| 96 | ret false |
| 97 | end |
| 98 | ret true |
| 99 | end |
| 100 | |
| 101 | fn op_to_lower c uchar to uchar |
| 102 | if ge c 65 |
| 103 | if le c 90 |
| 104 | ret add c 32 |
| 105 | end |
| 106 | end |
| 107 | ret c |
| 108 | end |
| 109 | |
| 110 | fn op_slice_eq a view uchar * aoff ulong alen ulong b view uchar * to bool |
| 111 | if ne alen len b |
| 112 | ret false |
| 113 | end |
| 114 | if gt add aoff alen len a |
| 115 | ret false |
| 116 | end |
| 117 | i = ulong 0 |
| 118 | while lt i alen bound 65536 |
| 119 | if ne get a add aoff i get b i |
| 120 | ret false |
| 121 | end |
| 122 | i = add i 1 |
| 123 | end |
| 124 | ret true |
| 125 | end |
| 126 | |
| 127 | fn op_match_at_ci hay view uchar * pos ulong ned view uchar * to bool |
| 128 | nlen = len ned |
| 129 | j = ulong 0 |
| 130 | while lt j nlen bound 256 |
| 131 | hc = op_to_lower get hay add pos j |
| 132 | nc = op_to_lower get ned j |
| 133 | if ne hc nc |
| 134 | ret false |
| 135 | end |
| 136 | j = add j 1 |
| 137 | end |
| 138 | ret true |
| 139 | end |
| 140 | |
| 141 | fn op_has_substr_ci hay view uchar * hoff ulong hlen ulong ned view uchar * to bool |
| 142 | nlen = len ned |
| 143 | if eq nlen 0 |
| 144 | ret true |
| 145 | end |
| 146 | if lt hlen nlen |
| 147 | ret false |
| 148 | end |
| 149 | if gt add hoff hlen len hay |
| 150 | ret false |
| 151 | end |
| 152 | limit = add sub hlen nlen 1 |
| 153 | i = ulong 0 |
| 154 | while lt i limit bound 65536 |
| 155 | if op_match_at_ci hay add hoff i ned |
| 156 | ret true |
| 157 | end |
| 158 | i = add i 1 |
| 159 | end |
| 160 | ret false |
| 161 | end |
| 162 | |
| 163 | fn op_match_at hay view uchar * pos ulong ned view uchar * to bool |
| 164 | nlen = len ned |
| 165 | j = ulong 0 |
| 166 | while lt j nlen bound 256 |
| 167 | if ne get hay add pos j get ned j |
| 168 | ret false |
| 169 | end |
| 170 | j = add j 1 |
| 171 | end |
| 172 | ret true |
| 173 | end |
| 174 | |
| 175 | fn op_has_substr hay view uchar * hoff ulong hlen ulong ned view uchar * to bool |
| 176 | nlen = len ned |
| 177 | if eq nlen 0 |
| 178 | ret true |
| 179 | end |
| 180 | if lt hlen nlen |
| 181 | ret false |
| 182 | end |
| 183 | if gt add hoff hlen len hay |
| 184 | ret false |
| 185 | end |
| 186 | limit = add sub hlen nlen 1 |
| 187 | i = ulong 0 |
| 188 | while lt i limit bound 65536 |
| 189 | if op_match_at hay add hoff i ned |
| 190 | ret true |
| 191 | end |
| 192 | i = add i 1 |
| 193 | end |
| 194 | ret false |
| 195 | end |
| 196 | |
| 197 | note Check control characters: refuse < 32 except tab(9), lf(10), cr(13), and refuse DEL(127). |
| 198 | fn op_has_bad_text buf view uchar * off ulong n ulong to bool |
| 199 | if gt add off n len buf |
| 200 | ret true |
| 201 | end |
| 202 | i = ulong 0 |
| 203 | while lt i n bound 65536 |
| 204 | c = get buf add off i |
| 205 | if eq c 127 |
| 206 | ret true |
| 207 | end |
| 208 | if lt c 32 |
| 209 | if ne c 9 |
| 210 | if ne c 10 |
| 211 | if ne c 13 |
| 212 | ret true |
| 213 | end |
| 214 | end |
| 215 | end |
| 216 | end |
| 217 | i = add i 1 |
| 218 | end |
| 219 | ret false |
| 220 | end |
| 221 | |
| 222 | note Validate project slug: [a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])? (1..40 bytes). |
| 223 | note Refuses path traversal (.., /), uppercase, spaces, control chars, HTML chars. |
| 224 | fn op_valid_slug buf view uchar * off ulong n ulong to bool |
| 225 | if eq n 0 |
| 226 | ret false |
| 227 | end |
| 228 | if gt n OP_MAX_SLUG |
| 229 | ret false |
| 230 | end |
| 231 | if gt add off n len buf |
| 232 | ret false |
| 233 | end |
| 234 | first = get buf off |
| 235 | if not op_is_alnum_lower first |
| 236 | ret false |
| 237 | end |
| 238 | last = get buf sub add off n 1 |
| 239 | if not op_is_alnum_lower last |
| 240 | ret false |
| 241 | end |
| 242 | i = ulong 0 |
| 243 | while lt i n bound 64 |
| 244 | c = get buf add off i |
| 245 | if not op_is_alnum_lower c |
| 246 | if ne c 45 |
| 247 | ret false |
| 248 | end |
| 249 | end |
| 250 | i = add i 1 |
| 251 | end |
| 252 | ret true |
| 253 | end |
| 254 | |
| 255 | note Validate builder handle: |
| 256 | note anon-[0-9a-f]{10} OR [a-z](?:[a-z0-9]|-(?=[a-z0-9])){1,23}-[0-9a-f]{8} |
| 257 | fn op_valid_handle buf view uchar * off ulong n ulong to bool |
| 258 | if gt add off n len buf |
| 259 | ret false |
| 260 | end |
| 261 | if eq n 15 |
| 262 | if op_slice_eq buf off 5 op_t_anon |
| 263 | i = ulong 5 |
| 264 | while lt i 15 bound 16 |
| 265 | if not op_is_hex_lower get buf add off i |
| 266 | ret false |
| 267 | end |
| 268 | i = add i 1 |
| 269 | end |
| 270 | ret true |
| 271 | end |
| 272 | end |
| 273 | if lt n 11 |
| 274 | ret false |
| 275 | end |
| 276 | if gt n 33 |
| 277 | ret false |
| 278 | end |
| 279 | if not op_is_lower get buf off |
| 280 | ret false |
| 281 | end |
| 282 | dash_pos = sub n 9 |
| 283 | if ne get buf add off dash_pos 45 |
| 284 | ret false |
| 285 | end |
| 286 | j = ulong 1 |
| 287 | prev_dash = false |
| 288 | while lt j dash_pos bound 32 |
| 289 | c = get buf add off j |
| 290 | if eq c 45 |
| 291 | if prev_dash |
| 292 | ret false |
| 293 | end |
| 294 | if eq add j 1 dash_pos |
| 295 | ret false |
| 296 | end |
| 297 | prev_dash = true |
| 298 | else |
| 299 | if not op_is_alnum_lower c |
| 300 | ret false |
| 301 | end |
| 302 | prev_dash = false |
| 303 | end |
| 304 | j = add j 1 |
| 305 | end |
| 306 | k = add dash_pos 1 |
| 307 | while lt k n bound 16 |
| 308 | if not op_is_hex_lower get buf add off k |
| 309 | ret false |
| 310 | end |
| 311 | k = add k 1 |
| 312 | end |
| 313 | ret true |
| 314 | end |
| 315 | |
| 316 | note Validate project file name: |
| 317 | note index.html | README.md | meta.json | [a-z0-9][a-z0-9_-]{0,39}.(om|js|css|md|txt|patch) |
| 318 | fn op_valid_ext buf view uchar * off ulong n ulong to bool |
| 319 | if eq n 2 |
| 320 | c0 = get buf off |
| 321 | c1 = get buf add off 1 |
| 322 | if eq c0 111 |
| 323 | if eq c1 109 |
| 324 | ret true |
| 325 | end |
| 326 | end |
| 327 | if eq c0 106 |
| 328 | if eq c1 115 |
| 329 | ret true |
| 330 | end |
| 331 | end |
| 332 | if eq c0 109 |
| 333 | if eq c1 100 |
| 334 | ret true |
| 335 | end |
| 336 | end |
| 337 | ret false |
| 338 | end |
| 339 | if eq n 3 |
| 340 | c0 = get buf off |
| 341 | c1 = get buf add off 1 |
| 342 | c2 = get buf add off 2 |
| 343 | if eq c0 99 |
| 344 | if eq c1 115 |
| 345 | if eq c2 115 |
| 346 | ret true |
| 347 | end |
| 348 | end |
| 349 | end |
| 350 | if eq c0 116 |
| 351 | if eq c1 120 |
| 352 | if eq c2 116 |
| 353 | ret true |
| 354 | end |
| 355 | end |
| 356 | end |
| 357 | ret false |
| 358 | end |
| 359 | if eq n 5 |
| 360 | if eq get buf off 112 |
| 361 | if eq get buf add off 1 97 |
| 362 | if eq get buf add off 2 116 |
| 363 | if eq get buf add off 3 99 |
| 364 | if eq get buf add off 4 104 |
| 365 | ret true |
| 366 | end |
| 367 | end |
| 368 | end |
| 369 | end |
| 370 | end |
| 371 | end |
| 372 | ret false |
| 373 | end |
| 374 | |
| 375 | fn op_valid_filename buf view uchar * off ulong n ulong to bool |
| 376 | if eq n 0 |
| 377 | ret false |
| 378 | end |
| 379 | if gt n 46 |
| 380 | ret false |
| 381 | end |
| 382 | if gt add off n len buf |
| 383 | ret false |
| 384 | end |
| 385 | if op_slice_eq buf off n op_t_idxhtml |
| 386 | ret true |
| 387 | end |
| 388 | if op_slice_eq buf off n op_t_readme |
| 389 | ret true |
| 390 | end |
| 391 | if op_slice_eq buf off n op_t_metajson |
| 392 | ret true |
| 393 | end |
| 394 | dot = ulong 0 |
| 395 | found_dot = false |
| 396 | i = ulong 0 |
| 397 | while lt i n bound 64 |
| 398 | c = get buf add off i |
| 399 | if eq c 46 |
| 400 | if found_dot |
| 401 | ret false |
| 402 | end |
| 403 | dot = i |
| 404 | found_dot = true |
| 405 | end |
| 406 | i = add i 1 |
| 407 | end |
| 408 | if not found_dot |
| 409 | ret false |
| 410 | end |
| 411 | if eq dot 0 |
| 412 | ret false |
| 413 | end |
| 414 | if gt dot 40 |
| 415 | ret false |
| 416 | end |
| 417 | if not op_is_alnum_lower get buf off |
| 418 | ret false |
| 419 | end |
| 420 | j = ulong 1 |
| 421 | while lt j dot bound 64 |
| 422 | c = get buf add off j |
| 423 | if not op_is_alnum_lower c |
| 424 | if ne c 45 |
| 425 | if ne c 95 |
| 426 | ret false |
| 427 | end |
| 428 | end |
| 429 | end |
| 430 | j = add j 1 |
| 431 | end |
| 432 | ext_off = add add off dot 1 |
| 433 | ext_len = sub sub n dot 1 |
| 434 | ret op_valid_ext buf ext_off ext_len |
| 435 | end |
| 436 | |
| 437 | note Validate full repository path: projects/<handle>/<slug>/<filename> |
| 438 | fn op_valid_project_path buf view uchar * off ulong n ulong to bool |
| 439 | if lt n 12 |
| 440 | ret false |
| 441 | end |
| 442 | if gt add off n len buf |
| 443 | ret false |
| 444 | end |
| 445 | if not op_slice_eq buf off 9 op_t_projects |
| 446 | ret false |
| 447 | end |
| 448 | p1 = ulong 9 |
| 449 | slash1 = ulong 0 |
| 450 | found1 = false |
| 451 | i = p1 |
| 452 | while lt i n bound 256 |
| 453 | if eq get buf add off i 47 |
| 454 | slash1 = i |
| 455 | found1 = true |
| 456 | break |
| 457 | end |
| 458 | i = add i 1 |
| 459 | end |
| 460 | if not found1 |
| 461 | ret false |
| 462 | end |
| 463 | if not op_valid_handle buf add off p1 sub slash1 p1 |
| 464 | ret false |
| 465 | end |
| 466 | p2 = add slash1 1 |
| 467 | slash2 = ulong 0 |
| 468 | found2 = false |
| 469 | j = p2 |
| 470 | while lt j n bound 256 |
| 471 | if eq get buf add off j 47 |
| 472 | slash2 = j |
| 473 | found2 = true |
| 474 | break |
| 475 | end |
| 476 | j = add j 1 |
| 477 | end |
| 478 | if not found2 |
| 479 | ret false |
| 480 | end |
| 481 | if not op_valid_slug buf add off p2 sub slash2 p2 |
| 482 | ret false |
| 483 | end |
| 484 | p3 = add slash2 1 |
| 485 | if ge p3 n |
| 486 | ret false |
| 487 | end |
| 488 | ret op_valid_filename buf add off p3 sub n p3 |
| 489 | end |
| 490 | |
| 491 | note Detect secret patterns matching upstream check_projects.py |
| 492 | fn op_has_secret buf view uchar * off ulong n ulong to bool |
| 493 | if op_has_substr buf off n op_t_ghp |
| 494 | ret true |
| 495 | end |
| 496 | if op_has_substr buf off n op_t_gho |
| 497 | ret true |
| 498 | end |
| 499 | if op_has_substr buf off n op_t_ghu |
| 500 | ret true |
| 501 | end |
| 502 | if op_has_substr buf off n op_t_ghs |
| 503 | ret true |
| 504 | end |
| 505 | if op_has_substr buf off n op_t_ghr |
| 506 | ret true |
| 507 | end |
| 508 | if op_has_substr buf off n op_t_ghpat |
| 509 | ret true |
| 510 | end |
| 511 | if op_has_substr buf off n op_t_sk |
| 512 | ret true |
| 513 | end |
| 514 | if op_has_substr buf off n op_t_akia |
| 515 | ret true |
| 516 | end |
| 517 | if op_has_substr buf off n op_t_aiza |
| 518 | ret true |
| 519 | end |
| 520 | if op_has_substr buf off n op_t_privkey |
| 521 | ret true |
| 522 | end |
| 523 | if op_has_substr buf off n op_t_jwt |
| 524 | ret true |
| 525 | end |
| 526 | ret false |
| 527 | end |
| 528 | |
| 529 | note Detect external web references matching upstream check_projects.py |
| 530 | fn op_has_external_web buf view uchar * off ulong n ulong to bool |
| 531 | if op_has_substr_ci buf off n op_t_ext_script |
| 532 | ret true |
| 533 | end |
| 534 | if op_has_substr_ci buf off n op_t_ext_iframe |
| 535 | ret true |
| 536 | end |
| 537 | if op_has_substr_ci buf off n op_t_ext_link |
| 538 | ret true |
| 539 | end |
| 540 | if op_has_substr_ci buf off n op_t_ext_object |
| 541 | ret true |
| 542 | end |
| 543 | if op_has_substr_ci buf off n op_t_ext_embed |
| 544 | ret true |
| 545 | end |
| 546 | if op_has_substr_ci buf off n op_t_ext_base |
| 547 | ret true |
| 548 | end |
| 549 | if op_has_substr_ci buf off n op_t_ext_http |
| 550 | ret true |
| 551 | end |
| 552 | if op_has_substr_ci buf off n op_t_ext_https |
| 553 | ret true |
| 554 | end |
| 555 | if op_has_substr_ci buf off n op_t_ext_fetch |
| 556 | ret true |
| 557 | end |
| 558 | if op_has_substr_ci buf off n op_t_ext_xhr |
| 559 | ret true |
| 560 | end |
| 561 | if op_has_substr_ci buf off n op_t_ext_ws |
| 562 | ret true |
| 563 | end |
| 564 | if op_has_substr_ci buf off n op_t_ext_beacon |
| 565 | ret true |
| 566 | end |
| 567 | ret false |
| 568 | end |
| 569 | |
| 570 | note Validate saved timestamp field (e.g. 2026-11-18 or 2026-11-18T12:00:00Z). |
| 571 | fn op_valid_saved buf view uchar * off ulong n ulong to bool |
| 572 | if lt n 4 |
| 573 | ret false |
| 574 | end |
| 575 | if gt n OP_MAX_SAVED |
| 576 | ret false |
| 577 | end |
| 578 | if gt add off n len buf |
| 579 | ret false |
| 580 | end |
| 581 | i = ulong 0 |
| 582 | while lt i n bound 64 |
| 583 | c = get buf add off i |
| 584 | ok = op_is_digit c |
| 585 | if eq c 45 |
| 586 | ok = true |
| 587 | end |
| 588 | if eq c 58 |
| 589 | ok = true |
| 590 | end |
| 591 | if eq c 84 |
| 592 | ok = true |
| 593 | end |
| 594 | if eq c 90 |
| 595 | ok = true |
| 596 | end |
| 597 | if eq c 46 |
| 598 | ok = true |
| 599 | end |
| 600 | if not ok |
| 601 | ret false |
| 602 | end |
| 603 | i = add i 1 |
| 604 | end |
| 605 | ret true |
| 606 | end |
| 607 | |
| 608 | fn op_validate_meta src view uchar * meta edit OpMeta to int |
| 609 | if not op_valid_handle src get meta h_off get meta h_len |
| 610 | ret -2 |
| 611 | end |
| 612 | if not op_valid_slug src get meta s_off get meta s_len |
| 613 | ret -3 |
| 614 | end |
| 615 | tlen = get meta t_len |
| 616 | if eq tlen 0 |
| 617 | ret -4 |
| 618 | end |
| 619 | if gt tlen OP_MAX_TITLE |
| 620 | ret -4 |
| 621 | end |
| 622 | dlen = get meta d_len |
| 623 | if gt dlen OP_MAX_DESC |
| 624 | ret -5 |
| 625 | end |
| 626 | if not op_valid_saved src get meta v_off get meta v_len |
| 627 | ret -6 |
| 628 | end |
| 629 | if op_has_bad_text src get meta t_off tlen |
| 630 | ret -7 |
| 631 | end |
| 632 | if op_has_bad_text src get meta d_off dlen |
| 633 | ret -7 |
| 634 | end |
| 635 | if op_has_secret src get meta t_off tlen |
| 636 | ret -7 |
| 637 | end |
| 638 | if op_has_secret src get meta d_off dlen |
| 639 | ret -7 |
| 640 | end |
| 641 | put meta valid ulong 1 |
| 642 | ret 0 |
| 643 | end |
| 644 | |
| 645 | note Parse TSV project metadata row: handle\tslug\ttitle\tdescription\tsaved |
| 646 | note Returns 0 on success, negative error code on refusal: |
| 647 | note -1: bad column count (must be 5) |
| 648 | note -2: invalid handle |
| 649 | note -3: invalid slug |
| 650 | note -4: invalid title (empty or too long) |
| 651 | note -5: invalid description (too long) |
| 652 | note -6: invalid saved timestamp |
| 653 | note -7: control character or secret detected |
| 654 | fn op_parse_tsv_line src view uchar * off ulong n ulong meta edit OpMeta to int |
| 655 | put meta valid ulong 0 |
| 656 | if gt add off n len src |
| 657 | ret -1 |
| 658 | end |
| 659 | eff = n |
| 660 | if gt eff 0 |
| 661 | if eq get src sub add off eff 1 10 |
| 662 | eff = sub eff 1 |
| 663 | end |
| 664 | end |
| 665 | if gt eff 0 |
| 666 | if eq get src sub add off eff 1 13 |
| 667 | eff = sub eff 1 |
| 668 | end |
| 669 | end |
| 670 | tabs = new ulong 5 0 |
| 671 | tcnt = ulong 0 |
| 672 | i = ulong 0 |
| 673 | while lt i eff bound 16384 |
| 674 | if eq get src add off i 9 |
| 675 | if lt tcnt 5 |
| 676 | put tabs tcnt i |
| 677 | end |
| 678 | tcnt = add tcnt 1 |
| 679 | end |
| 680 | i = add i 1 |
| 681 | end |
| 682 | if ne tcnt 4 |
| 683 | ret -1 |
| 684 | end |
| 685 | put meta h_off off |
| 686 | put meta h_len get tabs 0 |
| 687 | put meta s_off add add off get tabs 0 1 |
| 688 | put meta s_len sub sub get tabs 1 get tabs 0 1 |
| 689 | put meta t_off add add off get tabs 1 1 |
| 690 | put meta t_len sub sub get tabs 2 get tabs 1 1 |
| 691 | put meta d_off add add off get tabs 2 1 |
| 692 | put meta d_len sub sub get tabs 3 get tabs 2 1 |
| 693 | put meta v_off add add off get tabs 3 1 |
| 694 | put meta v_len sub sub eff get tabs 3 1 |
| 695 | ret op_validate_meta src meta |
| 696 | end |
| 697 | |
| 698 | note Append slice src[0..len src] to dst at pos. Returns new pos or -1 on overflow. |
| 699 | fn op_append_raw dst edit uchar * pos ulong src view uchar * to long |
| 700 | n = len src |
| 701 | if gt add pos n len dst |
| 702 | ret -1 |
| 703 | end |
| 704 | i = ulong 0 |
| 705 | while lt i n bound 65536 |
| 706 | put dst add pos i get src i |
| 707 | i = add i 1 |
| 708 | end |
| 709 | ret long add pos n |
| 710 | end |
| 711 | |
| 712 | note Strict HTML escape: &, <, >, ", ' -> HTML entities. Refuses control bytes (<32 except \t\n\r, or 127). |
| 713 | note Returns new pos (>= 0), -1 on buffer overflow, -2 on forbidden control byte. |
| 714 | fn op_escape_html dst edit uchar * pos ulong src view uchar * to long |
| 715 | n = len src |
| 716 | p = pos |
| 717 | i = ulong 0 |
| 718 | while lt i n bound 65536 |
| 719 | c = get src i |
| 720 | if eq c 127 |
| 721 | ret -2 |
| 722 | end |
| 723 | if lt c 32 |
| 724 | if ne c 9 |
| 725 | if ne c 10 |
| 726 | if ne c 13 |
| 727 | ret -2 |
| 728 | end |
| 729 | end |
| 730 | end |
| 731 | end |
| 732 | if eq c 38 |
| 733 | np = op_append_raw dst p op_t_amp |
| 734 | if lt np 0 |
| 735 | ret -1 |
| 736 | end |
| 737 | p = ulong np |
| 738 | else |
| 739 | if eq c 60 |
| 740 | np = op_append_raw dst p op_t_lt |
| 741 | if lt np 0 |
| 742 | ret -1 |
| 743 | end |
| 744 | p = ulong np |
| 745 | else |
| 746 | if eq c 62 |
| 747 | np = op_append_raw dst p op_t_gt |
| 748 | if lt np 0 |
| 749 | ret -1 |
| 750 | end |
| 751 | p = ulong np |
| 752 | else |
| 753 | if eq c 34 |
| 754 | np = op_append_raw dst p op_t_quot |
| 755 | if lt np 0 |
| 756 | ret -1 |
| 757 | end |
| 758 | p = ulong np |
| 759 | else |
| 760 | if eq c 39 |
| 761 | np = op_append_raw dst p op_t_apos |
| 762 | if lt np 0 |
| 763 | ret -1 |
| 764 | end |
| 765 | p = ulong np |
| 766 | else |
| 767 | if ge p len dst |
| 768 | ret -1 |
| 769 | end |
| 770 | put dst p c |
| 771 | p = add p 1 |
| 772 | end |
| 773 | end |
| 774 | end |
| 775 | end |
| 776 | end |
| 777 | i = add i 1 |
| 778 | end |
| 779 | ret long p |
| 780 | end |
| 781 | |
| 782 | fn op_emit_card_head dst edit uchar * pos ulong src view uchar * meta view OpMeta to long |
| 783 | p = op_append_raw dst pos op_t_card_a |
| 784 | if lt p 0 |
| 785 | ret -1 |
| 786 | end |
| 787 | p = op_append_raw dst ulong p part src get meta h_off get meta h_len |
| 788 | if lt p 0 |
| 789 | ret -1 |
| 790 | end |
| 791 | p = op_append_raw dst ulong p op_t_card_b |
| 792 | if lt p 0 |
| 793 | ret -1 |
| 794 | end |
| 795 | p = op_append_raw dst ulong p part src get meta s_off get meta s_len |
| 796 | if lt p 0 |
| 797 | ret -1 |
| 798 | end |
| 799 | p = op_append_raw dst ulong p op_t_card_c |
| 800 | if lt p 0 |
| 801 | ret -1 |
| 802 | end |
| 803 | p = op_append_raw dst ulong p part src get meta h_off get meta h_len |
| 804 | if lt p 0 |
| 805 | ret -1 |
| 806 | end |
| 807 | if ge ulong p len dst |
| 808 | ret -1 |
| 809 | end |
| 810 | put dst ulong p uchar 47 |
| 811 | p = add p 1 |
| 812 | p = op_append_raw dst ulong p part src get meta s_off get meta s_len |
| 813 | if lt p 0 |
| 814 | ret -1 |
| 815 | end |
| 816 | ret op_append_raw dst ulong p op_t_card_d |
| 817 | end |
| 818 | |
| 819 | fn op_emit_card_body dst edit uchar * pos ulong src view uchar * meta view OpMeta to long |
| 820 | p = op_escape_html dst pos part src get meta t_off get meta t_len |
| 821 | if lt p 0 |
| 822 | ret p |
| 823 | end |
| 824 | p = op_append_raw dst ulong p op_t_card_e |
| 825 | if lt p 0 |
| 826 | ret -1 |
| 827 | end |
| 828 | p = op_append_raw dst ulong p part src get meta h_off get meta h_len |
| 829 | if lt p 0 |
| 830 | ret -1 |
| 831 | end |
| 832 | p = op_append_raw dst ulong p op_t_card_f |
| 833 | if lt p 0 |
| 834 | ret -1 |
| 835 | end |
| 836 | p = op_append_raw dst ulong p part src get meta v_off get meta v_len |
| 837 | if lt p 0 |
| 838 | ret -1 |
| 839 | end |
| 840 | p = op_append_raw dst ulong p op_t_card_g |
| 841 | if lt p 0 |
| 842 | ret -1 |
| 843 | end |
| 844 | p = op_escape_html dst ulong p part src get meta d_off get meta d_len |
| 845 | if lt p 0 |
| 846 | ret p |
| 847 | end |
| 848 | ret op_append_raw dst ulong p op_t_card_h |
| 849 | end |
| 850 | |
| 851 | note Emit one deterministic HTML index card with strict HTML escaping. |
| 852 | note Re-validates handle and slug before emitting. Returns new pos or negative error. |
| 853 | fn op_emit_card dst edit uchar * pos ulong src view uchar * meta view OpMeta to long |
| 854 | if ne get meta valid ulong 1 |
| 855 | ret -2 |
| 856 | end |
| 857 | if not op_valid_handle src get meta h_off get meta h_len |
| 858 | ret -2 |
| 859 | end |
| 860 | if not op_valid_slug src get meta s_off get meta s_len |
| 861 | ret -3 |
| 862 | end |
| 863 | p = op_emit_card_head dst pos src meta |
| 864 | if lt p 0 |
| 865 | ret p |
| 866 | end |
| 867 | ret op_emit_card_body dst ulong p src meta |
| 868 | end |
| 869 | |
| 870 | fn op_emit_index_header dst edit uchar * pos ulong to long |
| 871 | ret op_append_raw dst pos op_t_page_head |
| 872 | end |
| 873 | |
| 874 | fn op_emit_index_empty dst edit uchar * pos ulong to long |
| 875 | ret op_append_raw dst pos op_t_page_empty |
| 876 | end |
| 877 | |
| 878 | fn op_emit_index_footer dst edit uchar * pos ulong to long |
| 879 | ret op_append_raw dst pos op_t_page_foot |
| 880 | end |