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 / translate / lib.om
| 1 | note apps/translate/lib.om: pure-Omega translation service core (converted from aien-translate). |
| 2 | note Composable OM-2 library unit (no om2 line, no fn main). |
| 3 | note Compose with: si/directives/om2.om om/tls/sha2.om apps/translate/lib.om <main.om|test.om> |
| 4 | note |
| 5 | note Implements: |
| 6 | note - ISO-639 and translate.js allowlist validator for all 31 supported languages + auto source |
| 7 | note - Strict RFC 3629 UTF-8 validator, codepoint counter, and NUL/size payload guards |
| 8 | note - UTF-8-aware paragraph/sentence/word chunker (never splits multi-byte code points) |
| 9 | note - Lead/core/trail splitter preserving whitespace and split URL edge fragments (split_ws) |
| 10 | note - Protected-token & keep-list translation guard (URLs, code spans, placeholders, tags, keep words) |
| 11 | note - Deterministic SHA-256 keyed bounded LRU cache (keyed by prompt_ver/model/src/dst/text) |
| 12 | note - Strict JSONL request parser and escaped JSONL response serializer |
| 13 | |
| 14 | const TR_MAX_BYTES ulong 16384 |
| 15 | const TR_MAX_CHARS ulong 3000 |
| 16 | const TR_MAX_TEXTS ulong 40 |
| 17 | const TR_MAX_CHUNKS ulong 64 |
| 18 | |
| 19 | const TR_LANG_COUNT ulong 31 |
| 20 | const TR_LANG_AUTO ulong 31 |
| 21 | const TR_LANG_INVALID ulong 255 |
| 22 | |
| 23 | const TR_OK ulong 0 |
| 24 | const TR_ERR_EMPTY ulong 1 |
| 25 | const TR_ERR_OVERSIZE ulong 2 |
| 26 | const TR_ERR_UTF8 ulong 3 |
| 27 | const TR_ERR_NUL ulong 4 |
| 28 | const TR_ERR_LANG ulong 5 |
| 29 | const TR_ERR_JSON ulong 6 |
| 30 | const TR_ERR_GUARD ulong 7 |
| 31 | const TR_ERR_CHUNK ulong 8 |
| 32 | |
| 33 | const TR_CACHE_MAX_SLOTS ulong 32 |
| 34 | const TR_CACHE_VAL_CAP ulong 512 |
| 35 | |
| 36 | note Cache globals (bounded LRU, full 32-byte SHA-256 keys) |
| 37 | global tr_g_cache_cap ulong 1 |
| 38 | global tr_g_cache_used ulong 32 |
| 39 | global tr_g_cache_tick ulong 32 |
| 40 | global tr_g_cache_clock ulong 1 |
| 41 | global tr_g_cache_key uchar 1024 |
| 42 | global tr_g_cache_src ulong 32 |
| 43 | global tr_g_cache_dst ulong 32 |
| 44 | global tr_g_cache_vlen ulong 32 |
| 45 | global tr_g_cache_val uchar 16384 |
| 46 | global tr_g_cache_hits ulong 1 |
| 47 | global tr_g_cache_misses ulong 1 |
| 48 | global tr_g_cache_evicts ulong 1 |
| 49 | global tr_g_cache_cnt ulong 1 |
| 50 | |
| 51 | note Chunker & split_ws output globals |
| 52 | global tr_g_chk_off ulong 64 |
| 53 | global tr_g_chk_len ulong 64 |
| 54 | global tr_g_chk_cnt ulong 1 |
| 55 | global tr_g_sw_lead ulong 1 |
| 56 | global tr_g_sw_core_end ulong 1 |
| 57 | |
| 58 | note Parsed request globals |
| 59 | global tr_g_req_from uchar 64 |
| 60 | global tr_g_req_from_len ulong 1 |
| 61 | global tr_g_req_to uchar 64 |
| 62 | global tr_g_req_to_len ulong 1 |
| 63 | global tr_g_req_model uchar 64 |
| 64 | global tr_g_req_model_len ulong 1 |
| 65 | global tr_g_req_text uchar 16385 |
| 66 | global tr_g_req_text_len ulong 1 |
| 67 | global tr_g_req_maxc ulong 1 |
| 68 | |
| 69 | note Scratch & JSONL response globals |
| 70 | global tr_g_key_buf uchar 32 |
| 71 | global tr_g_tmp_out uchar 1024 |
| 72 | global tr_g_tmp_out_len ulong 1 |
| 73 | global tr_g_resp uchar 32768 |
| 74 | global tr_g_resp_len ulong 1 |
| 75 | global tr_g_item_buf uchar 1024 |
| 76 | global tr_g_item_len ulong 1 |
| 77 | |
| 78 | note Prompt version & default model |
| 79 | text tr_t_pver "p6" |
| 80 | text tr_t_defmodel "aien-local" |
| 81 | text tr_t_auto "auto" |
| 82 | |
| 83 | note 31 translate.js language ids (exact match to upstream src/langs.rs) |
| 84 | text tr_lid_0 "english" |
| 85 | text tr_lid_1 "chinese_simplified" |
| 86 | text tr_lid_2 "chinese_traditional" |
| 87 | text tr_lid_3 "japanese" |
| 88 | text tr_lid_4 "korean" |
| 89 | text tr_lid_5 "spanish" |
| 90 | text tr_lid_6 "french" |
| 91 | text tr_lid_7 "deutsch" |
| 92 | text tr_lid_8 "portuguese" |
| 93 | text tr_lid_9 "italian" |
| 94 | text tr_lid_10 "russian" |
| 95 | text tr_lid_11 "ukrainian" |
| 96 | text tr_lid_12 "polish" |
| 97 | text tr_lid_13 "dutch" |
| 98 | text tr_lid_14 "swedish" |
| 99 | text tr_lid_15 "danish" |
| 100 | text tr_lid_16 "norwegian" |
| 101 | text tr_lid_17 "finnish" |
| 102 | text tr_lid_18 "czech" |
| 103 | text tr_lid_19 "romanian" |
| 104 | text tr_lid_20 "hungarian" |
| 105 | text tr_lid_21 "greek" |
| 106 | text tr_lid_22 "turkish" |
| 107 | text tr_lid_23 "arabic" |
| 108 | text tr_lid_24 "persian" |
| 109 | text tr_lid_25 "hindi" |
| 110 | text tr_lid_26 "bengali" |
| 111 | text tr_lid_27 "thai" |
| 112 | text tr_lid_28 "vietnamese" |
| 113 | text tr_lid_29 "indonesian" |
| 114 | text tr_lid_30 "malay" |
| 115 | |
| 116 | note 31 canonical ISO-639 codes (exact match to upstream src/langs.rs) |
| 117 | text tr_iso_0 "en" |
| 118 | text tr_iso_1 "zh-CN" |
| 119 | text tr_iso_2 "zh-TW" |
| 120 | text tr_iso_3 "ja" |
| 121 | text tr_iso_4 "ko" |
| 122 | text tr_iso_5 "es" |
| 123 | text tr_iso_6 "fr" |
| 124 | text tr_iso_7 "de" |
| 125 | text tr_iso_8 "pt" |
| 126 | text tr_iso_9 "it" |
| 127 | text tr_iso_10 "ru" |
| 128 | text tr_iso_11 "uk" |
| 129 | text tr_iso_12 "pl" |
| 130 | text tr_iso_13 "nl" |
| 131 | text tr_iso_14 "sv" |
| 132 | text tr_iso_15 "da" |
| 133 | text tr_iso_16 "no" |
| 134 | text tr_iso_17 "fi" |
| 135 | text tr_iso_18 "cs" |
| 136 | text tr_iso_19 "ro" |
| 137 | text tr_iso_20 "hu" |
| 138 | text tr_iso_21 "el" |
| 139 | text tr_iso_22 "tr" |
| 140 | text tr_iso_23 "ar" |
| 141 | text tr_iso_24 "fa" |
| 142 | text tr_iso_25 "hi" |
| 143 | text tr_iso_26 "bn" |
| 144 | text tr_iso_27 "th" |
| 145 | text tr_iso_28 "vi" |
| 146 | text tr_iso_29 "id" |
| 147 | text tr_iso_30 "ms" |
| 148 | |
| 149 | note Built-in DEFAULT_KEEP terms from upstream src/lib.rs |
| 150 | text tr_kp_0 "AIEN" |
| 151 | text tr_kp_1 "AienOS" |
| 152 | text tr_kp_2 "Aienux" |
| 153 | text tr_kp_3 "AIENOS" |
| 154 | text tr_kp_4 "Aienix" |
| 155 | text tr_kp_5 "Aien" |
| 156 | text tr_kp_6 "Omega" |
| 157 | text tr_kp_7 "OMEGA" |
| 158 | text tr_kp_8 "Interplane" |
| 159 | text tr_kp_9 "Allen" |
| 160 | text tr_kp_10 "Synapse" |
| 161 | text tr_kp_11 "Atlas" |
| 162 | text tr_kp_12 "Cortex" |
| 163 | text tr_kp_13 "Radicle" |
| 164 | text tr_kp_14 "Spark" |
| 165 | text tr_kp_15 "DGX" |
| 166 | text tr_kp_16 "GB10" |
| 167 | text tr_kp_17 "NVIDIA" |
| 168 | text tr_kp_18 "translate.js" |
| 169 | text tr_kp_19 "GitHub" |
| 170 | text tr_kp_20 "Rust" |
| 171 | text tr_kp_21 "Linux" |
| 172 | |
| 173 | note JSON keys and fragments |
| 174 | text tr_jk_from "from" |
| 175 | text tr_jk_src "src" |
| 176 | text tr_jk_to "to" |
| 177 | text tr_jk_dst "dst" |
| 178 | text tr_jk_text "text" |
| 179 | text tr_jk_model "model" |
| 180 | text tr_jk_maxc "max_chunk" |
| 181 | text tr_t_http "http://" |
| 182 | text tr_t_https "https://" |
| 183 | text tr_t_https_col "https:" |
| 184 | text tr_t_http_col "http:" |
| 185 | text tr_t_hex "0123456789abcdef" |
| 186 | |
| 187 | text tr_e_empty "empty text" |
| 188 | text tr_e_over "request payload oversize" |
| 189 | text tr_e_utf8 "invalid utf-8 sequence" |
| 190 | text tr_e_nul "nul byte refused" |
| 191 | text tr_e_lang "unsupported language code" |
| 192 | text tr_e_json "malformed json request" |
| 193 | text tr_e_guard "translation guard failed" |
| 194 | text tr_e_chunk "chunking refused" |
| 195 | |
| 196 | text tr_j_ok_pre "{\"result\":1,\"from\":\"" |
| 197 | text tr_j_to "\",\"to\":\"" |
| 198 | text tr_j_chunks "\",\"chunks\":" |
| 199 | text tr_j_fc ",\"from_cache\":" |
| 200 | text tr_j_ms ",\"misses\":" |
| 201 | text tr_j_ev ",\"evictions\":" |
| 202 | text tr_j_items ",\"items\":[" |
| 203 | text tr_j_end "]}\n" |
| 204 | text tr_j_err_pre "{\"result\":0,\"code\":" |
| 205 | text tr_j_err_msg ",\"error\":\"" |
| 206 | text tr_j_err_end "\"}\n" |
| 207 | |
| 208 | note --- Character & byte classification --- |
| 209 | |
| 210 | fn tr_ascii_lower b uchar to uchar |
| 211 | if and ge b 65 le b 90 |
| 212 | ret add b 32 |
| 213 | end |
| 214 | ret b |
| 215 | end |
| 216 | |
| 217 | fn tr_is_ws b uchar to bool |
| 218 | if eq b 32 |
| 219 | ret true |
| 220 | end |
| 221 | if eq b 9 |
| 222 | ret true |
| 223 | end |
| 224 | if eq b 10 |
| 225 | ret true |
| 226 | end |
| 227 | if eq b 13 |
| 228 | ret true |
| 229 | end |
| 230 | ret false |
| 231 | end |
| 232 | |
| 233 | fn tr_is_digit b uchar to bool |
| 234 | ret and ge b 48 le b 57 |
| 235 | end |
| 236 | |
| 237 | fn tr_is_alpha b uchar to bool |
| 238 | if and ge b 65 le b 90 |
| 239 | ret true |
| 240 | end |
| 241 | if and ge b 97 le b 122 |
| 242 | ret true |
| 243 | end |
| 244 | ret false |
| 245 | end |
| 246 | |
| 247 | fn tr_is_alnum b uchar to bool |
| 248 | if tr_is_alpha b |
| 249 | ret true |
| 250 | end |
| 251 | ret tr_is_digit b |
| 252 | end |
| 253 | |
| 254 | fn tr_eq_ci_slice a view uchar * aoff ulong alen ulong b view uchar * to bool |
| 255 | if ne alen len b |
| 256 | ret false |
| 257 | end |
| 258 | i = ulong 0 |
| 259 | while lt i alen bound 64 |
| 260 | ca = tr_ascii_lower get a add aoff i |
| 261 | cb = tr_ascii_lower get b i |
| 262 | if ne ca cb |
| 263 | ret false |
| 264 | end |
| 265 | i = add i 1 |
| 266 | end |
| 267 | ret true |
| 268 | end |
| 269 | |
| 270 | fn tr_eq_exact_slice a view uchar * aoff ulong alen ulong b view uchar * to bool |
| 271 | if ne alen len b |
| 272 | ret false |
| 273 | end |
| 274 | i = ulong 0 |
| 275 | while lt i alen bound 1024 |
| 276 | if ne get a add aoff i get b i |
| 277 | ret false |
| 278 | end |
| 279 | i = add i 1 |
| 280 | end |
| 281 | ret true |
| 282 | end |
| 283 | |
| 284 | note Helper: compare hay[i..i+nlen] against ndl[0..nlen] |
| 285 | fn tr_sub_match_at hay view uchar * i ulong ndl view uchar * nlen ulong to bool |
| 286 | j = ulong 0 |
| 287 | while lt j nlen bound 1024 |
| 288 | if ne get hay add i j get ndl j |
| 289 | ret false |
| 290 | end |
| 291 | j = add j 1 |
| 292 | end |
| 293 | ret true |
| 294 | end |
| 295 | |
| 296 | note Find exact substring ndl[0..nlen] inside hay[0..hlen]. Returns index or hlen+1 if absent. |
| 297 | fn tr_find_sub hay view uchar * hlen ulong ndl view uchar * nlen ulong to ulong |
| 298 | if eq nlen 0 |
| 299 | ret 0 |
| 300 | end |
| 301 | if gt nlen hlen |
| 302 | ret add hlen 1 |
| 303 | end |
| 304 | lim = add sub hlen nlen 1 |
| 305 | i = ulong 0 |
| 306 | while lt i lim bound 16385 |
| 307 | if tr_sub_match_at hay i ndl nlen |
| 308 | ret i |
| 309 | end |
| 310 | i = add i 1 |
| 311 | end |
| 312 | ret add hlen 1 |
| 313 | end |
| 314 | |
| 315 | note Helper: compare hay[i..i+nlen] against ndl[noff..noff+nlen] (nlen from tr_g_item_len) |
| 316 | fn tr_slice_match_at hay view uchar * i ulong ndl view uchar * noff ulong to bool |
| 317 | nlen = get tr_g_item_len 0 |
| 318 | j = ulong 0 |
| 319 | while lt j nlen bound 1024 |
| 320 | a = get hay add i j |
| 321 | b = get ndl add noff j |
| 322 | if ne a b |
| 323 | ret false |
| 324 | end |
| 325 | j = add j 1 |
| 326 | end |
| 327 | ret true |
| 328 | end |
| 329 | |
| 330 | note Find slice ndl[noff..noff+nlen] inside hay[0..hlen]. Returns true if found. |
| 331 | fn tr_has_slice hay view uchar * hlen ulong ndl view uchar * noff ulong to bool |
| 332 | nlen = get tr_g_item_len 0 |
| 333 | if eq nlen 0 |
| 334 | ret true |
| 335 | end |
| 336 | if gt nlen hlen |
| 337 | ret false |
| 338 | end |
| 339 | lim = add sub hlen nlen 1 |
| 340 | i = ulong 0 |
| 341 | while lt i lim bound 16385 |
| 342 | if tr_slice_match_at hay i ndl noff |
| 343 | ret true |
| 344 | end |
| 345 | i = add i 1 |
| 346 | end |
| 347 | ret false |
| 348 | end |
| 349 | |
| 350 | note Check if whole word ndl appears in hay[0..hlen] (not glued to ASCII alnum bytes). |
| 351 | fn tr_contains_word hay view uchar * hlen ulong ndl view uchar * to bool |
| 352 | nlen = len ndl |
| 353 | if eq nlen 0 |
| 354 | ret false |
| 355 | end |
| 356 | if gt nlen hlen |
| 357 | ret false |
| 358 | end |
| 359 | lim = add sub hlen nlen 1 |
| 360 | i = ulong 0 |
| 361 | while lt i lim bound 16385 |
| 362 | ok = true |
| 363 | j = ulong 0 |
| 364 | while lt j nlen bound 64 |
| 365 | if ne get hay add i j get ndl j |
| 366 | ok = false |
| 367 | break |
| 368 | end |
| 369 | j = add j 1 |
| 370 | end |
| 371 | if ok |
| 372 | left_ok = true |
| 373 | if gt i 0 |
| 374 | if tr_is_alnum get hay sub i 1 |
| 375 | left_ok = false |
| 376 | end |
| 377 | end |
| 378 | right_ok = true |
| 379 | ep = add i nlen |
| 380 | if lt ep hlen |
| 381 | if tr_is_alnum get hay ep |
| 382 | right_ok = false |
| 383 | end |
| 384 | end |
| 385 | if and left_ok right_ok |
| 386 | ret true |
| 387 | end |
| 388 | end |
| 389 | i = add i 1 |
| 390 | end |
| 391 | ret false |
| 392 | end |
| 393 | |
| 394 | note --- ISO-639 & translate.js Language Allowlist (all 31 upstream languages) --- |
| 395 | |
| 396 | fn tr_lang_match_pair buf view uchar * off ulong slen ulong idx ulong to bool |
| 397 | if eq idx 0 |
| 398 | ret or tr_eq_ci_slice buf off slen tr_lid_0 tr_eq_ci_slice buf off slen tr_iso_0 |
| 399 | end |
| 400 | if eq idx 1 |
| 401 | ret or tr_eq_ci_slice buf off slen tr_lid_1 tr_eq_ci_slice buf off slen tr_iso_1 |
| 402 | end |
| 403 | if eq idx 2 |
| 404 | ret or tr_eq_ci_slice buf off slen tr_lid_2 tr_eq_ci_slice buf off slen tr_iso_2 |
| 405 | end |
| 406 | if eq idx 3 |
| 407 | ret or tr_eq_ci_slice buf off slen tr_lid_3 tr_eq_ci_slice buf off slen tr_iso_3 |
| 408 | end |
| 409 | if eq idx 4 |
| 410 | ret or tr_eq_ci_slice buf off slen tr_lid_4 tr_eq_ci_slice buf off slen tr_iso_4 |
| 411 | end |
| 412 | if eq idx 5 |
| 413 | ret or tr_eq_ci_slice buf off slen tr_lid_5 tr_eq_ci_slice buf off slen tr_iso_5 |
| 414 | end |
| 415 | if eq idx 6 |
| 416 | ret or tr_eq_ci_slice buf off slen tr_lid_6 tr_eq_ci_slice buf off slen tr_iso_6 |
| 417 | end |
| 418 | if eq idx 7 |
| 419 | ret or tr_eq_ci_slice buf off slen tr_lid_7 tr_eq_ci_slice buf off slen tr_iso_7 |
| 420 | end |
| 421 | if eq idx 8 |
| 422 | ret or tr_eq_ci_slice buf off slen tr_lid_8 tr_eq_ci_slice buf off slen tr_iso_8 |
| 423 | end |
| 424 | if eq idx 9 |
| 425 | ret or tr_eq_ci_slice buf off slen tr_lid_9 tr_eq_ci_slice buf off slen tr_iso_9 |
| 426 | end |
| 427 | if eq idx 10 |
| 428 | ret or tr_eq_ci_slice buf off slen tr_lid_10 tr_eq_ci_slice buf off slen tr_iso_10 |
| 429 | end |
| 430 | if eq idx 11 |
| 431 | ret or tr_eq_ci_slice buf off slen tr_lid_11 tr_eq_ci_slice buf off slen tr_iso_11 |
| 432 | end |
| 433 | if eq idx 12 |
| 434 | ret or tr_eq_ci_slice buf off slen tr_lid_12 tr_eq_ci_slice buf off slen tr_iso_12 |
| 435 | end |
| 436 | if eq idx 13 |
| 437 | ret or tr_eq_ci_slice buf off slen tr_lid_13 tr_eq_ci_slice buf off slen tr_iso_13 |
| 438 | end |
| 439 | if eq idx 14 |
| 440 | ret or tr_eq_ci_slice buf off slen tr_lid_14 tr_eq_ci_slice buf off slen tr_iso_14 |
| 441 | end |
| 442 | if eq idx 15 |
| 443 | ret or tr_eq_ci_slice buf off slen tr_lid_15 tr_eq_ci_slice buf off slen tr_iso_15 |
| 444 | end |
| 445 | ret false |
| 446 | end |
| 447 | |
| 448 | fn tr_lang_match_pair2 buf view uchar * off ulong slen ulong idx ulong to bool |
| 449 | if eq idx 16 |
| 450 | ret or tr_eq_ci_slice buf off slen tr_lid_16 tr_eq_ci_slice buf off slen tr_iso_16 |
| 451 | end |
| 452 | if eq idx 17 |
| 453 | ret or tr_eq_ci_slice buf off slen tr_lid_17 tr_eq_ci_slice buf off slen tr_iso_17 |
| 454 | end |
| 455 | if eq idx 18 |
| 456 | ret or tr_eq_ci_slice buf off slen tr_lid_18 tr_eq_ci_slice buf off slen tr_iso_18 |
| 457 | end |
| 458 | if eq idx 19 |
| 459 | ret or tr_eq_ci_slice buf off slen tr_lid_19 tr_eq_ci_slice buf off slen tr_iso_19 |
| 460 | end |
| 461 | if eq idx 20 |
| 462 | ret or tr_eq_ci_slice buf off slen tr_lid_20 tr_eq_ci_slice buf off slen tr_iso_20 |
| 463 | end |
| 464 | if eq idx 21 |
| 465 | ret or tr_eq_ci_slice buf off slen tr_lid_21 tr_eq_ci_slice buf off slen tr_iso_21 |
| 466 | end |
| 467 | if eq idx 22 |
| 468 | ret or tr_eq_ci_slice buf off slen tr_lid_22 tr_eq_ci_slice buf off slen tr_iso_22 |
| 469 | end |
| 470 | if eq idx 23 |
| 471 | ret or tr_eq_ci_slice buf off slen tr_lid_23 tr_eq_ci_slice buf off slen tr_iso_23 |
| 472 | end |
| 473 | if eq idx 24 |
| 474 | ret or tr_eq_ci_slice buf off slen tr_lid_24 tr_eq_ci_slice buf off slen tr_iso_24 |
| 475 | end |
| 476 | if eq idx 25 |
| 477 | ret or tr_eq_ci_slice buf off slen tr_lid_25 tr_eq_ci_slice buf off slen tr_iso_25 |
| 478 | end |
| 479 | if eq idx 26 |
| 480 | ret or tr_eq_ci_slice buf off slen tr_lid_26 tr_eq_ci_slice buf off slen tr_iso_26 |
| 481 | end |
| 482 | if eq idx 27 |
| 483 | ret or tr_eq_ci_slice buf off slen tr_lid_27 tr_eq_ci_slice buf off slen tr_iso_27 |
| 484 | end |
| 485 | if eq idx 28 |
| 486 | ret or tr_eq_ci_slice buf off slen tr_lid_28 tr_eq_ci_slice buf off slen tr_iso_28 |
| 487 | end |
| 488 | if eq idx 29 |
| 489 | ret or tr_eq_ci_slice buf off slen tr_lid_29 tr_eq_ci_slice buf off slen tr_iso_29 |
| 490 | end |
| 491 | if eq idx 30 |
| 492 | ret or tr_eq_ci_slice buf off slen tr_lid_30 tr_eq_ci_slice buf off slen tr_iso_30 |
| 493 | end |
| 494 | ret false |
| 495 | end |
| 496 | |
| 497 | fn tr_lang_find buf view uchar * off ulong slen ulong to ulong |
| 498 | if gt slen 64 |
| 499 | ret TR_LANG_INVALID |
| 500 | end |
| 501 | s = off |
| 502 | e = add off slen |
| 503 | while lt s e bound 64 |
| 504 | if not tr_is_ws get buf s |
| 505 | break |
| 506 | end |
| 507 | s = add s 1 |
| 508 | end |
| 509 | while gt e s bound 64 |
| 510 | if not tr_is_ws get buf sub e 1 |
| 511 | break |
| 512 | end |
| 513 | e = sub e 1 |
| 514 | end |
| 515 | tlen = sub e s |
| 516 | if eq tlen 0 |
| 517 | ret TR_LANG_INVALID |
| 518 | end |
| 519 | i = ulong 0 |
| 520 | while lt i 16 bound 16 |
| 521 | if tr_lang_match_pair buf s tlen i |
| 522 | ret i |
| 523 | end |
| 524 | i = add i 1 |
| 525 | end |
| 526 | while lt i TR_LANG_COUNT bound 16 |
| 527 | if tr_lang_match_pair2 buf s tlen i |
| 528 | ret i |
| 529 | end |
| 530 | i = add i 1 |
| 531 | end |
| 532 | ret TR_LANG_INVALID |
| 533 | end |
| 534 | |
| 535 | fn tr_lang_find_src buf view uchar * off ulong slen ulong to ulong |
| 536 | if gt slen 64 |
| 537 | ret TR_LANG_INVALID |
| 538 | end |
| 539 | s = off |
| 540 | e = add off slen |
| 541 | while lt s e bound 64 |
| 542 | if not tr_is_ws get buf s |
| 543 | break |
| 544 | end |
| 545 | s = add s 1 |
| 546 | end |
| 547 | while gt e s bound 64 |
| 548 | if not tr_is_ws get buf sub e 1 |
| 549 | break |
| 550 | end |
| 551 | e = sub e 1 |
| 552 | end |
| 553 | tlen = sub e s |
| 554 | if eq tlen 0 |
| 555 | ret TR_LANG_AUTO |
| 556 | end |
| 557 | if tr_eq_ci_slice buf s tlen tr_t_auto |
| 558 | ret TR_LANG_AUTO |
| 559 | end |
| 560 | ret tr_lang_find buf s tlen |
| 561 | end |
| 562 | |
| 563 | fn tr_copy_text dst edit uchar * doff ulong src view uchar * to ulong |
| 564 | n = len src |
| 565 | i = ulong 0 |
| 566 | while lt i n bound 64 |
| 567 | put dst add doff i get src i |
| 568 | i = add i 1 |
| 569 | end |
| 570 | ret n |
| 571 | end |
| 572 | |
| 573 | fn tr_lang_code_write lid ulong dst edit uchar * doff ulong to ulong |
| 574 | if eq lid 0 |
| 575 | ret tr_copy_text dst doff tr_iso_0 |
| 576 | end |
| 577 | if eq lid 1 |
| 578 | ret tr_copy_text dst doff tr_iso_1 |
| 579 | end |
| 580 | if eq lid 2 |
| 581 | ret tr_copy_text dst doff tr_iso_2 |
| 582 | end |
| 583 | if eq lid 3 |
| 584 | ret tr_copy_text dst doff tr_iso_3 |
| 585 | end |
| 586 | if eq lid 4 |
| 587 | ret tr_copy_text dst doff tr_iso_4 |
| 588 | end |
| 589 | if eq lid 5 |
| 590 | ret tr_copy_text dst doff tr_iso_5 |
| 591 | end |
| 592 | if eq lid 6 |
| 593 | ret tr_copy_text dst doff tr_iso_6 |
| 594 | end |
| 595 | if eq lid 7 |
| 596 | ret tr_copy_text dst doff tr_iso_7 |
| 597 | end |
| 598 | if eq lid 8 |
| 599 | ret tr_copy_text dst doff tr_iso_8 |
| 600 | end |
| 601 | if eq lid 9 |
| 602 | ret tr_copy_text dst doff tr_iso_9 |
| 603 | end |
| 604 | if eq lid 10 |
| 605 | ret tr_copy_text dst doff tr_iso_10 |
| 606 | end |
| 607 | if eq lid 11 |
| 608 | ret tr_copy_text dst doff tr_iso_11 |
| 609 | end |
| 610 | if eq lid 12 |
| 611 | ret tr_copy_text dst doff tr_iso_12 |
| 612 | end |
| 613 | if eq lid 13 |
| 614 | ret tr_copy_text dst doff tr_iso_13 |
| 615 | end |
| 616 | if eq lid 14 |
| 617 | ret tr_copy_text dst doff tr_iso_14 |
| 618 | end |
| 619 | if eq lid 15 |
| 620 | ret tr_copy_text dst doff tr_iso_15 |
| 621 | end |
| 622 | ret tr_lang_code_write2 lid dst doff |
| 623 | end |
| 624 | |
| 625 | fn tr_lang_code_write2 lid ulong dst edit uchar * doff ulong to ulong |
| 626 | if eq lid 16 |
| 627 | ret tr_copy_text dst doff tr_iso_16 |
| 628 | end |
| 629 | if eq lid 17 |
| 630 | ret tr_copy_text dst doff tr_iso_17 |
| 631 | end |
| 632 | if eq lid 18 |
| 633 | ret tr_copy_text dst doff tr_iso_18 |
| 634 | end |
| 635 | if eq lid 19 |
| 636 | ret tr_copy_text dst doff tr_iso_19 |
| 637 | end |
| 638 | if eq lid 20 |
| 639 | ret tr_copy_text dst doff tr_iso_20 |
| 640 | end |
| 641 | if eq lid 21 |
| 642 | ret tr_copy_text dst doff tr_iso_21 |
| 643 | end |
| 644 | if eq lid 22 |
| 645 | ret tr_copy_text dst doff tr_iso_22 |
| 646 | end |
| 647 | if eq lid 23 |
| 648 | ret tr_copy_text dst doff tr_iso_23 |
| 649 | end |
| 650 | if eq lid 24 |
| 651 | ret tr_copy_text dst doff tr_iso_24 |
| 652 | end |
| 653 | if eq lid 25 |
| 654 | ret tr_copy_text dst doff tr_iso_25 |
| 655 | end |
| 656 | if eq lid 26 |
| 657 | ret tr_copy_text dst doff tr_iso_26 |
| 658 | end |
| 659 | if eq lid 27 |
| 660 | ret tr_copy_text dst doff tr_iso_27 |
| 661 | end |
| 662 | if eq lid 28 |
| 663 | ret tr_copy_text dst doff tr_iso_28 |
| 664 | end |
| 665 | if eq lid 29 |
| 666 | ret tr_copy_text dst doff tr_iso_29 |
| 667 | end |
| 668 | if eq lid 30 |
| 669 | ret tr_copy_text dst doff tr_iso_30 |
| 670 | end |
| 671 | if eq lid TR_LANG_AUTO |
| 672 | ret tr_copy_text dst doff tr_t_auto |
| 673 | end |
| 674 | ret 0 |
| 675 | end |
| 676 | |
| 677 | note --- Strict RFC 3629 UTF-8 validation & codepoint stepping --- |
| 678 | |
| 679 | fn tr_is_cont b uchar to bool |
| 680 | ret and ge b 0x80 le b 0xbf |
| 681 | end |
| 682 | |
| 683 | note Returns UTF-8 sequence byte length (1..4), or 0 if invalid UTF-8 at pos. |
| 684 | fn tr_utf8_step buf view uchar * pos ulong blen ulong to ulong |
| 685 | if ge pos blen |
| 686 | ret 0 |
| 687 | end |
| 688 | b0 = get buf pos |
| 689 | if lt b0 0x80 |
| 690 | ret 1 |
| 691 | end |
| 692 | if and ge b0 0xc2 le b0 0xdf |
| 693 | if ge add pos 1 blen |
| 694 | ret 0 |
| 695 | end |
| 696 | if tr_is_cont get buf add pos 1 |
| 697 | ret 2 |
| 698 | end |
| 699 | ret 0 |
| 700 | end |
| 701 | if and ge b0 0xe0 le b0 0xef |
| 702 | if ge add pos 2 blen |
| 703 | ret 0 |
| 704 | end |
| 705 | b1 = get buf add pos 1 |
| 706 | b2 = get buf add pos 2 |
| 707 | if not tr_is_cont b2 |
| 708 | ret 0 |
| 709 | end |
| 710 | if eq b0 0xe0 |
| 711 | if and ge b1 0xa0 le b1 0xbf |
| 712 | ret 3 |
| 713 | end |
| 714 | ret 0 |
| 715 | end |
| 716 | if eq b0 0xed |
| 717 | if and ge b1 0x80 le b1 0x9f |
| 718 | ret 3 |
| 719 | end |
| 720 | ret 0 |
| 721 | end |
| 722 | if tr_is_cont b1 |
| 723 | ret 3 |
| 724 | end |
| 725 | ret 0 |
| 726 | end |
| 727 | if and ge b0 0xf0 le b0 0xf4 |
| 728 | if ge add pos 3 blen |
| 729 | ret 0 |
| 730 | end |
| 731 | b1 = get buf add pos 1 |
| 732 | b2 = get buf add pos 2 |
| 733 | b3 = get buf add pos 3 |
| 734 | if not and tr_is_cont b2 tr_is_cont b3 |
| 735 | ret 0 |
| 736 | end |
| 737 | if eq b0 0xf0 |
| 738 | if and ge b1 0x90 le b1 0xbf |
| 739 | ret 4 |
| 740 | end |
| 741 | ret 0 |
| 742 | end |
| 743 | if eq b0 0xf4 |
| 744 | if and ge b1 0x80 le b1 0x8f |
| 745 | ret 4 |
| 746 | end |
| 747 | ret 0 |
| 748 | end |
| 749 | if tr_is_cont b1 |
| 750 | ret 4 |
| 751 | end |
| 752 | ret 0 |
| 753 | end |
| 754 | ret 0 |
| 755 | end |
| 756 | |
| 757 | fn tr_utf8_valid buf view uchar * blen ulong to bool |
| 758 | if gt blen TR_MAX_BYTES |
| 759 | ret false |
| 760 | end |
| 761 | pos = ulong 0 |
| 762 | while lt pos blen bound 16385 |
| 763 | step = tr_utf8_step buf pos blen |
| 764 | if eq step 0 |
| 765 | ret false |
| 766 | end |
| 767 | pos = add pos step |
| 768 | end |
| 769 | ret true |
| 770 | end |
| 771 | |
| 772 | fn tr_utf8_chars buf view uchar * blen ulong to ulong |
| 773 | if gt blen TR_MAX_BYTES |
| 774 | ret 0 |
| 775 | end |
| 776 | pos = ulong 0 |
| 777 | cnt = ulong 0 |
| 778 | while lt pos blen bound 16385 |
| 779 | step = tr_utf8_step buf pos blen |
| 780 | if eq step 0 |
| 781 | ret 0 |
| 782 | end |
| 783 | pos = add pos step |
| 784 | cnt = add cnt 1 |
| 785 | end |
| 786 | ret cnt |
| 787 | end |
| 788 | |
| 789 | note Strict request text validation: rejects empty, oversize, NUL byte, invalid UTF-8. |
| 790 | fn tr_validate_text buf view uchar * blen ulong to ulong |
| 791 | if eq blen 0 |
| 792 | ret TR_ERR_EMPTY |
| 793 | end |
| 794 | if gt blen TR_MAX_BYTES |
| 795 | ret TR_ERR_OVERSIZE |
| 796 | end |
| 797 | pos = ulong 0 |
| 798 | chars = ulong 0 |
| 799 | nonws = false |
| 800 | while lt pos blen bound 16385 |
| 801 | b0 = get buf pos |
| 802 | if eq b0 0 |
| 803 | ret TR_ERR_NUL |
| 804 | end |
| 805 | step = tr_utf8_step buf pos blen |
| 806 | if eq step 0 |
| 807 | ret TR_ERR_UTF8 |
| 808 | end |
| 809 | if eq step 1 |
| 810 | if not tr_is_ws b0 |
| 811 | nonws = true |
| 812 | end |
| 813 | else |
| 814 | nonws = true |
| 815 | end |
| 816 | pos = add pos step |
| 817 | chars = add chars 1 |
| 818 | end |
| 819 | if gt chars TR_MAX_CHARS |
| 820 | ret TR_ERR_OVERSIZE |
| 821 | end |
| 822 | if not nonws |
| 823 | ret TR_ERR_EMPTY |
| 824 | end |
| 825 | ret TR_OK |
| 826 | end |
| 827 | |
| 828 | note --- UTF-8-aware paragraph/sentence/word chunker (never splits multi-byte code points) --- |
| 829 | |
| 830 | fn tr_is_cjk_sent buf view uchar * pos ulong blen ulong to bool |
| 831 | if gt add pos 3 blen |
| 832 | ret false |
| 833 | end |
| 834 | b0 = get buf pos |
| 835 | b1 = get buf add pos 1 |
| 836 | b2 = get buf add pos 2 |
| 837 | note U+3002 IDEOGRAPHIC FULL STOP (e3 80 82) |
| 838 | if and eq b0 0xe3 and eq b1 0x80 eq b2 0x82 |
| 839 | ret true |
| 840 | end |
| 841 | note U+FF01 FULLWIDTH EXCLAMATION (ef bc 81) or U+FF1F FULLWIDTH QUESTION (ef bc 9f) |
| 842 | if and eq b0 0xef eq b1 0xbc |
| 843 | if or eq b2 0x81 eq b2 0x9f |
| 844 | ret true |
| 845 | end |
| 846 | end |
| 847 | ret false |
| 848 | end |
| 849 | |
| 850 | fn tr_is_sent_punct b uchar to bool |
| 851 | if eq b 46 |
| 852 | ret true |
| 853 | end |
| 854 | if eq b 33 |
| 855 | ret true |
| 856 | end |
| 857 | ret eq b 63 |
| 858 | end |
| 859 | |
| 860 | fn tr_pick_chunk_end buf view uchar * pos ulong blen ulong maxb ulong to ulong |
| 861 | bests = new ulong 4 0 |
| 862 | if le sub blen pos maxb |
| 863 | ret blen |
| 864 | end |
| 865 | lim = add pos maxb |
| 866 | cur = pos |
| 867 | last_cp = pos |
| 868 | while lt cur lim bound 16385 |
| 869 | step = tr_utf8_step buf cur blen |
| 870 | if eq step 0 |
| 871 | ret 0 |
| 872 | end |
| 873 | nxt = add cur step |
| 874 | if gt nxt lim |
| 875 | break |
| 876 | end |
| 877 | last_cp = nxt |
| 878 | if eq step 1 |
| 879 | b = get buf cur |
| 880 | if eq b 10 |
| 881 | put bests 1 nxt |
| 882 | if lt nxt lim |
| 883 | if eq get buf nxt 10 |
| 884 | put bests 0 add nxt 1 |
| 885 | end |
| 886 | end |
| 887 | end |
| 888 | if tr_is_sent_punct b |
| 889 | if lt nxt blen |
| 890 | if tr_is_ws get buf nxt |
| 891 | put bests 2 nxt |
| 892 | end |
| 893 | end |
| 894 | end |
| 895 | if eq b 32 |
| 896 | put bests 3 nxt |
| 897 | end |
| 898 | if eq b 9 |
| 899 | put bests 3 nxt |
| 900 | end |
| 901 | else |
| 902 | if eq step 3 |
| 903 | if tr_is_cjk_sent buf cur blen |
| 904 | put bests 2 nxt |
| 905 | end |
| 906 | end |
| 907 | end |
| 908 | cur = nxt |
| 909 | end |
| 910 | if gt get bests 0 pos |
| 911 | ret get bests 0 |
| 912 | end |
| 913 | if gt get bests 1 pos |
| 914 | ret get bests 1 |
| 915 | end |
| 916 | if gt get bests 2 pos |
| 917 | ret get bests 2 |
| 918 | end |
| 919 | if gt get bests 3 pos |
| 920 | ret get bests 3 |
| 921 | end |
| 922 | ret last_cp |
| 923 | end |
| 924 | |
| 925 | note Splits buf[0..blen] into UTF-8-safe chunks <= maxb bytes each. |
| 926 | note Writes offsets/lengths into tr_g_chk_off / tr_g_chk_len and count into tr_g_chk_cnt[0]. |
| 927 | fn tr_chunk_utf8 buf view uchar * blen ulong maxb ulong to ulong |
| 928 | put tr_g_chk_cnt 0 0 |
| 929 | if lt maxb 4 |
| 930 | ret TR_ERR_CHUNK |
| 931 | end |
| 932 | verr = tr_validate_text buf blen |
| 933 | if ne verr TR_OK |
| 934 | ret verr |
| 935 | end |
| 936 | pos = ulong 0 |
| 937 | k = ulong 0 |
| 938 | while lt pos blen bound 65 |
| 939 | if ge k TR_MAX_CHUNKS |
| 940 | ret TR_ERR_CHUNK |
| 941 | end |
| 942 | endp = tr_pick_chunk_end buf pos blen maxb |
| 943 | if le endp pos |
| 944 | ret TR_ERR_UTF8 |
| 945 | end |
| 946 | put tr_g_chk_off k pos |
| 947 | put tr_g_chk_len k sub endp pos |
| 948 | k = add k 1 |
| 949 | pos = endp |
| 950 | end |
| 951 | put tr_g_chk_cnt 0 k |
| 952 | ret TR_OK |
| 953 | end |
| 954 | |
| 955 | note --- Lead / Core / Trail splitter (upstream split_ws) --- |
| 956 | |
| 957 | fn tr_split_ws buf view uchar * blen ulong to void |
| 958 | a = ulong 0 |
| 959 | b = blen |
| 960 | while lt a b bound 16385 |
| 961 | if not tr_is_ws get buf a |
| 962 | break |
| 963 | end |
| 964 | a = add a 1 |
| 965 | end |
| 966 | while gt b a bound 16385 |
| 967 | if not tr_is_ws get buf sub b 1 |
| 968 | break |
| 969 | end |
| 970 | b = sub b 1 |
| 971 | end |
| 972 | note Preserve translate.js split URL tail ("https:" or "http:") at end of core |
| 973 | clen = sub b a |
| 974 | if ge clen 6 |
| 975 | if tr_eq_ci_slice buf sub b 6 6 tr_t_https_col |
| 976 | ok_tail = true |
| 977 | if gt clen 6 |
| 978 | if tr_is_alnum get buf sub b 7 |
| 979 | ok_tail = false |
| 980 | end |
| 981 | end |
| 982 | if ok_tail |
| 983 | b = sub b 6 |
| 984 | while gt b a bound 64 |
| 985 | if not tr_is_ws get buf sub b 1 |
| 986 | break |
| 987 | end |
| 988 | b = sub b 1 |
| 989 | end |
| 990 | end |
| 991 | end |
| 992 | else |
| 993 | if ge clen 5 |
| 994 | if tr_eq_ci_slice buf sub b 5 5 tr_t_http_col |
| 995 | ok_tail2 = true |
| 996 | if gt clen 5 |
| 997 | if tr_is_alnum get buf sub b 6 |
| 998 | ok_tail2 = false |
| 999 | end |
| 1000 | end |
| 1001 | if ok_tail2 |
| 1002 | b = sub b 5 |
| 1003 | while gt b a bound 64 |
| 1004 | if not tr_is_ws get buf sub b 1 |
| 1005 | break |
| 1006 | end |
| 1007 | b = sub b 1 |
| 1008 | end |
| 1009 | end |
| 1010 | end |
| 1011 | end |
| 1012 | end |
| 1013 | put tr_g_sw_lead 0 a |
| 1014 | put tr_g_sw_core_end 0 b |
| 1015 | end |
| 1016 | |
| 1017 | note --- Translation Guard & Protection Engine (upstream src/guard.rs) --- |
| 1018 | |
| 1019 | fn tr_keep_check_one src view uchar * slen ulong trn view uchar * tlen ulong to ulong |
| 1020 | if tr_contains_word src slen tr_kp_0 |
| 1021 | if not tr_contains_word trn tlen tr_kp_0 |
| 1022 | ret TR_ERR_GUARD |
| 1023 | end |
| 1024 | end |
| 1025 | if tr_contains_word src slen tr_kp_1 |
| 1026 | if not tr_contains_word trn tlen tr_kp_1 |
| 1027 | ret TR_ERR_GUARD |
| 1028 | end |
| 1029 | end |
| 1030 | if tr_contains_word src slen tr_kp_2 |
| 1031 | if not tr_contains_word trn tlen tr_kp_2 |
| 1032 | ret TR_ERR_GUARD |
| 1033 | end |
| 1034 | end |
| 1035 | if tr_contains_word src slen tr_kp_3 |
| 1036 | if not tr_contains_word trn tlen tr_kp_3 |
| 1037 | ret TR_ERR_GUARD |
| 1038 | end |
| 1039 | end |
| 1040 | if tr_contains_word src slen tr_kp_4 |
| 1041 | if not tr_contains_word trn tlen tr_kp_4 |
| 1042 | ret TR_ERR_GUARD |
| 1043 | end |
| 1044 | end |
| 1045 | if tr_contains_word src slen tr_kp_5 |
| 1046 | if not tr_contains_word trn tlen tr_kp_5 |
| 1047 | ret TR_ERR_GUARD |
| 1048 | end |
| 1049 | end |
| 1050 | if tr_contains_word src slen tr_kp_6 |
| 1051 | if not tr_contains_word trn tlen tr_kp_6 |
| 1052 | ret TR_ERR_GUARD |
| 1053 | end |
| 1054 | end |
| 1055 | if tr_contains_word src slen tr_kp_7 |
| 1056 | if not tr_contains_word trn tlen tr_kp_7 |
| 1057 | ret TR_ERR_GUARD |
| 1058 | end |
| 1059 | end |
| 1060 | if tr_contains_word src slen tr_kp_8 |
| 1061 | if not tr_contains_word trn tlen tr_kp_8 |
| 1062 | ret TR_ERR_GUARD |
| 1063 | end |
| 1064 | end |
| 1065 | if tr_contains_word src slen tr_kp_9 |
| 1066 | if not tr_contains_word trn tlen tr_kp_9 |
| 1067 | ret TR_ERR_GUARD |
| 1068 | end |
| 1069 | end |
| 1070 | if tr_contains_word src slen tr_kp_10 |
| 1071 | if not tr_contains_word trn tlen tr_kp_10 |
| 1072 | ret TR_ERR_GUARD |
| 1073 | end |
| 1074 | end |
| 1075 | ret TR_OK |
| 1076 | end |
| 1077 | |
| 1078 | fn tr_keep_check_two src view uchar * slen ulong trn view uchar * tlen ulong to ulong |
| 1079 | if tr_contains_word src slen tr_kp_11 |
| 1080 | if not tr_contains_word trn tlen tr_kp_11 |
| 1081 | ret TR_ERR_GUARD |
| 1082 | end |
| 1083 | end |
| 1084 | if tr_contains_word src slen tr_kp_12 |
| 1085 | if not tr_contains_word trn tlen tr_kp_12 |
| 1086 | ret TR_ERR_GUARD |
| 1087 | end |
| 1088 | end |
| 1089 | if tr_contains_word src slen tr_kp_13 |
| 1090 | if not tr_contains_word trn tlen tr_kp_13 |
| 1091 | ret TR_ERR_GUARD |
| 1092 | end |
| 1093 | end |
| 1094 | if tr_contains_word src slen tr_kp_14 |
| 1095 | if not tr_contains_word trn tlen tr_kp_14 |
| 1096 | ret TR_ERR_GUARD |
| 1097 | end |
| 1098 | end |
| 1099 | if tr_contains_word src slen tr_kp_15 |
| 1100 | if not tr_contains_word trn tlen tr_kp_15 |
| 1101 | ret TR_ERR_GUARD |
| 1102 | end |
| 1103 | end |
| 1104 | if tr_contains_word src slen tr_kp_16 |
| 1105 | if not tr_contains_word trn tlen tr_kp_16 |
| 1106 | ret TR_ERR_GUARD |
| 1107 | end |
| 1108 | end |
| 1109 | if tr_contains_word src slen tr_kp_17 |
| 1110 | if not tr_contains_word trn tlen tr_kp_17 |
| 1111 | ret TR_ERR_GUARD |
| 1112 | end |
| 1113 | end |
| 1114 | if tr_contains_word src slen tr_kp_18 |
| 1115 | if not tr_contains_word trn tlen tr_kp_18 |
| 1116 | ret TR_ERR_GUARD |
| 1117 | end |
| 1118 | end |
| 1119 | if tr_contains_word src slen tr_kp_19 |
| 1120 | if not tr_contains_word trn tlen tr_kp_19 |
| 1121 | ret TR_ERR_GUARD |
| 1122 | end |
| 1123 | end |
| 1124 | if tr_contains_word src slen tr_kp_20 |
| 1125 | if not tr_contains_word trn tlen tr_kp_20 |
| 1126 | ret TR_ERR_GUARD |
| 1127 | end |
| 1128 | end |
| 1129 | if tr_contains_word src slen tr_kp_21 |
| 1130 | if not tr_contains_word trn tlen tr_kp_21 |
| 1131 | ret TR_ERR_GUARD |
| 1132 | end |
| 1133 | end |
| 1134 | ret TR_OK |
| 1135 | end |
| 1136 | |
| 1137 | fn tr_is_url_stop c uchar to bool |
| 1138 | if tr_is_ws c |
| 1139 | ret true |
| 1140 | end |
| 1141 | if eq c 60 |
| 1142 | ret true |
| 1143 | end |
| 1144 | if eq c 62 |
| 1145 | ret true |
| 1146 | end |
| 1147 | if eq c 40 |
| 1148 | ret true |
| 1149 | end |
| 1150 | if eq c 41 |
| 1151 | ret true |
| 1152 | end |
| 1153 | if eq c 91 |
| 1154 | ret true |
| 1155 | end |
| 1156 | ret eq c 93 |
| 1157 | end |
| 1158 | |
| 1159 | fn tr_is_url_trail c uchar to bool |
| 1160 | if eq c 46 |
| 1161 | ret true |
| 1162 | end |
| 1163 | if eq c 44 |
| 1164 | ret true |
| 1165 | end |
| 1166 | if eq c 59 |
| 1167 | ret true |
| 1168 | end |
| 1169 | if eq c 58 |
| 1170 | ret true |
| 1171 | end |
| 1172 | if eq c 33 |
| 1173 | ret true |
| 1174 | end |
| 1175 | ret eq c 63 |
| 1176 | end |
| 1177 | |
| 1178 | fn tr_url_end buf view uchar * pos ulong blen ulong to ulong |
| 1179 | e = pos |
| 1180 | while lt e blen bound 1024 |
| 1181 | if tr_is_url_stop get buf e |
| 1182 | break |
| 1183 | end |
| 1184 | e = add e 1 |
| 1185 | end |
| 1186 | while gt e pos bound 64 |
| 1187 | if tr_is_url_trail get buf sub e 1 |
| 1188 | e = sub e 1 |
| 1189 | else |
| 1190 | break |
| 1191 | end |
| 1192 | end |
| 1193 | ret e |
| 1194 | end |
| 1195 | |
| 1196 | fn tr_scan_bt_end src view uchar * i ulong slen ulong to ulong |
| 1197 | j = add i 1 |
| 1198 | while lt j slen bound 1024 |
| 1199 | cj = get src j |
| 1200 | if eq cj 10 |
| 1201 | ret 0 |
| 1202 | end |
| 1203 | if eq cj 96 |
| 1204 | ret add j 1 |
| 1205 | end |
| 1206 | j = add j 1 |
| 1207 | end |
| 1208 | ret 0 |
| 1209 | end |
| 1210 | |
| 1211 | fn tr_scan_ph_end src view uchar * i ulong slen ulong to ulong |
| 1212 | j = add i 1 |
| 1213 | while lt j slen bound 128 |
| 1214 | cj = get src j |
| 1215 | if eq cj 125 |
| 1216 | if gt j add i 1 |
| 1217 | ret add j 1 |
| 1218 | end |
| 1219 | ret 0 |
| 1220 | end |
| 1221 | if not tr_is_alnum cj |
| 1222 | if ne cj 95 |
| 1223 | ret 0 |
| 1224 | end |
| 1225 | end |
| 1226 | j = add j 1 |
| 1227 | end |
| 1228 | ret 0 |
| 1229 | end |
| 1230 | |
| 1231 | fn tr_scan_tag_end src view uchar * i ulong slen ulong to ulong |
| 1232 | if ge add i 2 slen |
| 1233 | ret 0 |
| 1234 | end |
| 1235 | b1 = get src add i 1 |
| 1236 | ok_lead = false |
| 1237 | if eq b1 103 |
| 1238 | ok_lead = true |
| 1239 | end |
| 1240 | if eq b1 120 |
| 1241 | ok_lead = true |
| 1242 | end |
| 1243 | if eq b1 47 |
| 1244 | ok_lead = true |
| 1245 | end |
| 1246 | if not ok_lead |
| 1247 | ret 0 |
| 1248 | end |
| 1249 | j = add i 1 |
| 1250 | while lt j slen bound 16 |
| 1251 | if eq get src j 62 |
| 1252 | e = add j 1 |
| 1253 | if le sub e i 10 |
| 1254 | ret e |
| 1255 | end |
| 1256 | ret 0 |
| 1257 | end |
| 1258 | j = add j 1 |
| 1259 | end |
| 1260 | ret 0 |
| 1261 | end |
| 1262 | |
| 1263 | fn tr_scan_url_end src view uchar * i ulong slen ulong to ulong |
| 1264 | if le add i 8 slen |
| 1265 | if tr_eq_exact_slice src i 8 tr_t_https |
| 1266 | ue = tr_url_end src i slen |
| 1267 | if gt ue add i 8 |
| 1268 | ret ue |
| 1269 | end |
| 1270 | end |
| 1271 | end |
| 1272 | if le add i 7 slen |
| 1273 | if tr_eq_exact_slice src i 7 tr_t_http |
| 1274 | ue2 = tr_url_end src i slen |
| 1275 | if gt ue2 add i 7 |
| 1276 | ret ue2 |
| 1277 | end |
| 1278 | end |
| 1279 | end |
| 1280 | ret 0 |
| 1281 | end |
| 1282 | |
| 1283 | note Verify URLs, backtick spans, {placeholders}, %s/%d, and <gN>/</gN>/<xN/> survive in trn. |
| 1284 | fn tr_guard_tokens src view uchar * slen ulong trn view uchar * tlen ulong to ulong |
| 1285 | i = ulong 0 |
| 1286 | while lt i slen bound 16385 |
| 1287 | c = get src i |
| 1288 | e = ulong 0 |
| 1289 | if eq c 96 |
| 1290 | e = tr_scan_bt_end src i slen |
| 1291 | end |
| 1292 | if eq c 104 |
| 1293 | e = tr_scan_url_end src i slen |
| 1294 | end |
| 1295 | if eq c 123 |
| 1296 | e = tr_scan_ph_end src i slen |
| 1297 | end |
| 1298 | if eq c 60 |
| 1299 | e = tr_scan_tag_end src i slen |
| 1300 | end |
| 1301 | if eq c 37 |
| 1302 | if lt add i 1 slen |
| 1303 | nx = get src add i 1 |
| 1304 | if or eq nx 115 eq nx 100 |
| 1305 | e = add i 2 |
| 1306 | end |
| 1307 | end |
| 1308 | end |
| 1309 | if gt e i |
| 1310 | put tr_g_item_len 0 sub e i |
| 1311 | if not tr_has_slice trn tlen src i |
| 1312 | ret TR_ERR_GUARD |
| 1313 | end |
| 1314 | i = e |
| 1315 | else |
| 1316 | i = add i 1 |
| 1317 | end |
| 1318 | end |
| 1319 | ret TR_OK |
| 1320 | end |
| 1321 | |
| 1322 | note Full guard check matching upstream guard::check(source, translated, keep). |
| 1323 | fn tr_guard_check src view uchar * slen ulong trn view uchar * tlen ulong to ulong |
| 1324 | note 1. Reject empty or whitespace-only translation |
| 1325 | has_nw = false |
| 1326 | i = ulong 0 |
| 1327 | while lt i tlen bound 16385 |
| 1328 | if not tr_is_ws get trn i |
| 1329 | has_nw = true |
| 1330 | break |
| 1331 | end |
| 1332 | i = add i 1 |
| 1333 | end |
| 1334 | if not has_nw |
| 1335 | ret TR_ERR_GUARD |
| 1336 | end |
| 1337 | note 2. Reject runaway output: tr_chars > src_chars * 4 + 80 |
| 1338 | sc = tr_utf8_chars src slen |
| 1339 | tc = tr_utf8_chars trn tlen |
| 1340 | if eq tc 0 |
| 1341 | ret TR_ERR_GUARD |
| 1342 | end |
| 1343 | lim = add mul sc 4 80 |
| 1344 | if gt tc lim |
| 1345 | ret TR_ERR_GUARD |
| 1346 | end |
| 1347 | note 3. Protected keep-list words & tokens |
| 1348 | e1 = tr_keep_check_one src slen trn tlen |
| 1349 | if ne e1 TR_OK |
| 1350 | ret e1 |
| 1351 | end |
| 1352 | e2 = tr_keep_check_two src slen trn tlen |
| 1353 | if ne e2 TR_OK |
| 1354 | ret e2 |
| 1355 | end |
| 1356 | ret tr_guard_tokens src slen trn tlen |
| 1357 | end |
| 1358 | |
| 1359 | note Upstream guard::tidy: trim whitespace, strip spurious backticks if source had none. |
| 1360 | fn tr_tidy src view uchar * slen ulong trn view uchar * tlen ulong to ulong |
| 1361 | src_has_bt = false |
| 1362 | i = ulong 0 |
| 1363 | while lt i slen bound 16385 |
| 1364 | if eq get src i 96 |
| 1365 | src_has_bt = true |
| 1366 | break |
| 1367 | end |
| 1368 | i = add i 1 |
| 1369 | end |
| 1370 | s = ulong 0 |
| 1371 | e = tlen |
| 1372 | while lt s e bound 16385 |
| 1373 | if not tr_is_ws get trn s |
| 1374 | break |
| 1375 | end |
| 1376 | s = add s 1 |
| 1377 | end |
| 1378 | while gt e s bound 16385 |
| 1379 | if not tr_is_ws get trn sub e 1 |
| 1380 | break |
| 1381 | end |
| 1382 | e = sub e 1 |
| 1383 | end |
| 1384 | w = ulong 0 |
| 1385 | k = s |
| 1386 | while lt k e bound 1024 |
| 1387 | c = get trn k |
| 1388 | if or src_has_bt ne c 96 |
| 1389 | if lt w 1024 |
| 1390 | put tr_g_tmp_out w c |
| 1391 | w = add w 1 |
| 1392 | end |
| 1393 | end |
| 1394 | k = add k 1 |
| 1395 | end |
| 1396 | put tr_g_tmp_out_len 0 w |
| 1397 | ret w |
| 1398 | end |
| 1399 | |
| 1400 | note Upstream engine::list_closed: true once JSON array [...] closes outside strings. |
| 1401 | fn tr_list_closed buf view uchar * blen ulong to bool |
| 1402 | start = blen |
| 1403 | i = ulong 0 |
| 1404 | while lt i blen bound 16385 |
| 1405 | if eq get buf i 91 |
| 1406 | start = i |
| 1407 | break |
| 1408 | end |
| 1409 | i = add i 1 |
| 1410 | end |
| 1411 | if eq start blen |
| 1412 | ret false |
| 1413 | end |
| 1414 | depth = ulong 0 |
| 1415 | in_str = false |
| 1416 | esc = false |
| 1417 | pos = start |
| 1418 | while lt pos blen bound 16385 |
| 1419 | c = get buf pos |
| 1420 | if in_str |
| 1421 | if esc |
| 1422 | esc = false |
| 1423 | else |
| 1424 | if eq c 92 |
| 1425 | esc = true |
| 1426 | else |
| 1427 | if eq c 34 |
| 1428 | in_str = false |
| 1429 | end |
| 1430 | end |
| 1431 | end |
| 1432 | else |
| 1433 | if eq c 34 |
| 1434 | in_str = true |
| 1435 | else |
| 1436 | if or eq c 91 eq c 123 |
| 1437 | depth = add depth 1 |
| 1438 | else |
| 1439 | if or eq c 93 eq c 125 |
| 1440 | if gt depth 0 |
| 1441 | depth = sub depth 1 |
| 1442 | end |
| 1443 | if eq depth 0 |
| 1444 | ret true |
| 1445 | end |
| 1446 | end |
| 1447 | end |
| 1448 | end |
| 1449 | end |
| 1450 | pos = add pos 1 |
| 1451 | end |
| 1452 | ret false |
| 1453 | end |
| 1454 | |
| 1455 | note --- Deterministic SHA-256 Keyed Bounded LRU Cache (upstream src/cache.rs) --- |
| 1456 | |
| 1457 | fn tr_cache_init cap ulong to void |
| 1458 | c = cap |
| 1459 | if eq c 0 |
| 1460 | c = 1 |
| 1461 | end |
| 1462 | if gt c TR_CACHE_MAX_SLOTS |
| 1463 | c = TR_CACHE_MAX_SLOTS |
| 1464 | end |
| 1465 | put tr_g_cache_cap 0 c |
| 1466 | put tr_g_cache_clock 0 0 |
| 1467 | put tr_g_cache_hits 0 0 |
| 1468 | put tr_g_cache_misses 0 0 |
| 1469 | put tr_g_cache_evicts 0 0 |
| 1470 | put tr_g_cache_cnt 0 0 |
| 1471 | i = ulong 0 |
| 1472 | while lt i TR_CACHE_MAX_SLOTS bound 32 |
| 1473 | put tr_g_cache_used i 0 |
| 1474 | put tr_g_cache_tick i 0 |
| 1475 | put tr_g_cache_vlen i 0 |
| 1476 | i = add i 1 |
| 1477 | end |
| 1478 | end |
| 1479 | |
| 1480 | note Computes SHA-256(p6\0 model\0 src_code\0 dst_code\0 txt\0) into tr_g_key_buf. |
| 1481 | fn tr_cache_compute_key src_id ulong dst_id ulong txt view uchar * tlen ulong to void |
| 1482 | ctx = new ulong 40 0 |
| 1483 | nul = new uchar 1 0 |
| 1484 | wbuf = new uchar 64 0 |
| 1485 | sha256_init ctx |
| 1486 | sha256_update ctx tr_t_pver |
| 1487 | sha256_update ctx nul |
| 1488 | n = get tr_g_req_model_len 0 |
| 1489 | if eq n 0 |
| 1490 | sha256_update ctx tr_t_defmodel |
| 1491 | else |
| 1492 | if gt n 64 |
| 1493 | n = 64 |
| 1494 | end |
| 1495 | i = ulong 0 |
| 1496 | while lt i n bound 64 |
| 1497 | put wbuf i get tr_g_req_model i |
| 1498 | i = add i 1 |
| 1499 | end |
| 1500 | sha256_update ctx part wbuf 0 n |
| 1501 | end |
| 1502 | sha256_update ctx nul |
| 1503 | n = tr_lang_code_write src_id wbuf 0 |
| 1504 | sha256_update ctx part wbuf 0 n |
| 1505 | sha256_update ctx nul |
| 1506 | n = tr_lang_code_write dst_id wbuf 0 |
| 1507 | sha256_update ctx part wbuf 0 n |
| 1508 | sha256_update ctx nul |
| 1509 | if gt tlen 0 |
| 1510 | sha256_update ctx part txt 0 tlen |
| 1511 | end |
| 1512 | sha256_update ctx nul |
| 1513 | sha256_final ctx wbuf |
| 1514 | j = ulong 0 |
| 1515 | while lt j 32 bound 32 |
| 1516 | put tr_g_key_buf j get wbuf j |
| 1517 | j = add j 1 |
| 1518 | end |
| 1519 | end |
| 1520 | |
| 1521 | fn tr_cache_key_match slot ulong to bool |
| 1522 | base = mul slot 32 |
| 1523 | i = ulong 0 |
| 1524 | while lt i 32 bound 32 |
| 1525 | if ne get tr_g_cache_key add base i get tr_g_key_buf i |
| 1526 | ret false |
| 1527 | end |
| 1528 | i = add i 1 |
| 1529 | end |
| 1530 | ret true |
| 1531 | end |
| 1532 | |
| 1533 | fn tr_cache_find_slot cap ulong src_id ulong dst_id ulong to ulong |
| 1534 | i = ulong 0 |
| 1535 | while lt i cap bound 32 |
| 1536 | if eq get tr_g_cache_used i 1 |
| 1537 | if and eq get tr_g_cache_src i src_id eq get tr_g_cache_dst i dst_id |
| 1538 | if tr_cache_key_match i |
| 1539 | ret i |
| 1540 | end |
| 1541 | end |
| 1542 | end |
| 1543 | i = add i 1 |
| 1544 | end |
| 1545 | ret cap |
| 1546 | end |
| 1547 | |
| 1548 | fn tr_cache_read_slot slot ulong to void |
| 1549 | clk = add get tr_g_cache_clock 0 1 |
| 1550 | put tr_g_cache_clock 0 clk |
| 1551 | put tr_g_cache_tick slot clk |
| 1552 | put tr_g_cache_hits 0 add get tr_g_cache_hits 0 1 |
| 1553 | vlen = get tr_g_cache_vlen slot |
| 1554 | vbase = mul slot TR_CACHE_VAL_CAP |
| 1555 | j = ulong 0 |
| 1556 | while lt j vlen bound 512 |
| 1557 | put tr_g_tmp_out j get tr_g_cache_val add vbase j |
| 1558 | j = add j 1 |
| 1559 | end |
| 1560 | put tr_g_tmp_out_len 0 vlen |
| 1561 | end |
| 1562 | |
| 1563 | note Look up (src_id, dst_id, txt[0..tlen]) in cache. |
| 1564 | note On hit: copies cached output into tr_g_tmp_out, sets tr_g_tmp_out_len, returns 1. |
| 1565 | note On miss: increments misses, returns 0. |
| 1566 | fn tr_cache_get src_id ulong dst_id ulong txt view uchar * tlen ulong to ulong |
| 1567 | cap = get tr_g_cache_cap 0 |
| 1568 | if eq cap 0 |
| 1569 | tr_cache_init 16 |
| 1570 | cap = 16 |
| 1571 | end |
| 1572 | tr_cache_compute_key src_id dst_id txt tlen |
| 1573 | slot = tr_cache_find_slot cap src_id dst_id |
| 1574 | if lt slot cap |
| 1575 | tr_cache_read_slot slot |
| 1576 | ret 1 |
| 1577 | end |
| 1578 | put tr_g_cache_misses 0 add get tr_g_cache_misses 0 1 |
| 1579 | ret 0 |
| 1580 | end |
| 1581 | |
| 1582 | fn tr_cache_pick_slot cap ulong src_id ulong dst_id ulong to ulong |
| 1583 | slot = tr_cache_find_slot cap src_id dst_id |
| 1584 | if lt slot cap |
| 1585 | ret slot |
| 1586 | end |
| 1587 | j = ulong 0 |
| 1588 | while lt j cap bound 32 |
| 1589 | if eq get tr_g_cache_used j 0 |
| 1590 | put tr_g_cache_cnt 0 add get tr_g_cache_cnt 0 1 |
| 1591 | ret j |
| 1592 | end |
| 1593 | j = add j 1 |
| 1594 | end |
| 1595 | best = ulong 0 |
| 1596 | best_t = get tr_g_cache_tick 0 |
| 1597 | k = ulong 1 |
| 1598 | while lt k cap bound 32 |
| 1599 | tk = get tr_g_cache_tick k |
| 1600 | if lt tk best_t |
| 1601 | best = k |
| 1602 | best_t = tk |
| 1603 | end |
| 1604 | k = add k 1 |
| 1605 | end |
| 1606 | put tr_g_cache_evicts 0 add get tr_g_cache_evicts 0 1 |
| 1607 | ret best |
| 1608 | end |
| 1609 | |
| 1610 | fn tr_cache_write_slot slot ulong src_id ulong dst_id ulong to void |
| 1611 | clk = add get tr_g_cache_clock 0 1 |
| 1612 | put tr_g_cache_clock 0 clk |
| 1613 | put tr_g_cache_used slot 1 |
| 1614 | put tr_g_cache_tick slot clk |
| 1615 | put tr_g_cache_src slot src_id |
| 1616 | put tr_g_cache_dst slot dst_id |
| 1617 | kbase = mul slot 32 |
| 1618 | ki = ulong 0 |
| 1619 | while lt ki 32 bound 32 |
| 1620 | put tr_g_cache_key add kbase ki get tr_g_key_buf ki |
| 1621 | ki = add ki 1 |
| 1622 | end |
| 1623 | vlen = get tr_g_tmp_out_len 0 |
| 1624 | if gt vlen TR_CACHE_VAL_CAP |
| 1625 | vlen = TR_CACHE_VAL_CAP |
| 1626 | end |
| 1627 | put tr_g_cache_vlen slot vlen |
| 1628 | vbase = mul slot TR_CACHE_VAL_CAP |
| 1629 | vi = ulong 0 |
| 1630 | while lt vi vlen bound 512 |
| 1631 | put tr_g_cache_val add vbase vi get tr_g_tmp_out vi |
| 1632 | vi = add vi 1 |
| 1633 | end |
| 1634 | end |
| 1635 | |
| 1636 | note Store tr_g_tmp_out[0..tr_g_tmp_out_len] under (src_id, dst_id, txt[0..tlen]). |
| 1637 | note Performs deterministic LRU eviction when cache is at capacity. |
| 1638 | fn tr_cache_put src_id ulong dst_id ulong txt view uchar * tlen ulong to void |
| 1639 | cap = get tr_g_cache_cap 0 |
| 1640 | if eq cap 0 |
| 1641 | tr_cache_init 16 |
| 1642 | cap = 16 |
| 1643 | end |
| 1644 | tr_cache_compute_key src_id dst_id txt tlen |
| 1645 | slot = tr_cache_pick_slot cap src_id dst_id |
| 1646 | tr_cache_write_slot slot src_id dst_id |
| 1647 | end |
| 1648 | |
| 1649 | note --- JSONL Response Builder --- |
| 1650 | |
| 1651 | fn tr_resp_reset to void |
| 1652 | put tr_g_resp_len 0 0 |
| 1653 | end |
| 1654 | |
| 1655 | fn tr_resp_byte b uchar to void |
| 1656 | p = get tr_g_resp_len 0 |
| 1657 | if lt p 32768 |
| 1658 | put tr_g_resp p b |
| 1659 | put tr_g_resp_len 0 add p 1 |
| 1660 | end |
| 1661 | end |
| 1662 | |
| 1663 | fn tr_resp_text s view uchar * to void |
| 1664 | n = len s |
| 1665 | i = ulong 0 |
| 1666 | while lt i n bound 256 |
| 1667 | tr_resp_byte get s i |
| 1668 | i = add i 1 |
| 1669 | end |
| 1670 | end |
| 1671 | |
| 1672 | fn tr_resp_u64 v ulong to void |
| 1673 | digs = new uchar 24 0 |
| 1674 | if eq v 0 |
| 1675 | tr_resp_byte 48 |
| 1676 | ret |
| 1677 | end |
| 1678 | x = v |
| 1679 | k = ulong 0 |
| 1680 | while gt x 0 bound 24 |
| 1681 | d = rem x 10 |
| 1682 | put digs k uchar add 48 d |
| 1683 | x = div x 10 |
| 1684 | k = add k 1 |
| 1685 | end |
| 1686 | while gt k 0 bound 24 |
| 1687 | k = sub k 1 |
| 1688 | tr_resp_byte get digs k |
| 1689 | end |
| 1690 | end |
| 1691 | |
| 1692 | fn tr_resp_json_str_g len_val ulong to void |
| 1693 | note Writes tr_g_tmp_out[0..len_val] as a JSON-escaped string (with enclosing quotes) |
| 1694 | tr_resp_byte 34 |
| 1695 | i = ulong 0 |
| 1696 | while lt i len_val bound 1024 |
| 1697 | c = get tr_g_tmp_out i |
| 1698 | if eq c 34 |
| 1699 | tr_resp_byte 92 |
| 1700 | tr_resp_byte 34 |
| 1701 | else |
| 1702 | if eq c 92 |
| 1703 | tr_resp_byte 92 |
| 1704 | tr_resp_byte 92 |
| 1705 | else |
| 1706 | if eq c 10 |
| 1707 | tr_resp_byte 92 |
| 1708 | tr_resp_byte 110 |
| 1709 | else |
| 1710 | if eq c 13 |
| 1711 | tr_resp_byte 92 |
| 1712 | tr_resp_byte 114 |
| 1713 | else |
| 1714 | if eq c 9 |
| 1715 | tr_resp_byte 92 |
| 1716 | tr_resp_byte 116 |
| 1717 | else |
| 1718 | tr_resp_byte c |
| 1719 | end |
| 1720 | end |
| 1721 | end |
| 1722 | end |
| 1723 | end |
| 1724 | i = add i 1 |
| 1725 | end |
| 1726 | tr_resp_byte 34 |
| 1727 | end |
| 1728 | |
| 1729 | fn tr_resp_error code ulong to void |
| 1730 | tr_resp_reset |
| 1731 | tr_resp_text tr_j_err_pre |
| 1732 | tr_resp_u64 code |
| 1733 | tr_resp_text tr_j_err_msg |
| 1734 | if eq code TR_ERR_EMPTY |
| 1735 | tr_resp_text tr_e_empty |
| 1736 | end |
| 1737 | if eq code TR_ERR_OVERSIZE |
| 1738 | tr_resp_text tr_e_over |
| 1739 | end |
| 1740 | if eq code TR_ERR_UTF8 |
| 1741 | tr_resp_text tr_e_utf8 |
| 1742 | end |
| 1743 | if eq code TR_ERR_NUL |
| 1744 | tr_resp_text tr_e_nul |
| 1745 | end |
| 1746 | if eq code TR_ERR_LANG |
| 1747 | tr_resp_text tr_e_lang |
| 1748 | end |
| 1749 | if eq code TR_ERR_JSON |
| 1750 | tr_resp_text tr_e_json |
| 1751 | end |
| 1752 | if eq code TR_ERR_GUARD |
| 1753 | tr_resp_text tr_e_guard |
| 1754 | end |
| 1755 | if eq code TR_ERR_CHUNK |
| 1756 | tr_resp_text tr_e_chunk |
| 1757 | end |
| 1758 | tr_resp_text tr_j_err_end |
| 1759 | end |
| 1760 | |
| 1761 | note --- Strict JSONL Request Parser --- |
| 1762 | |
| 1763 | fn tr_skip_ws buf view uchar * pos ulong blen ulong to ulong |
| 1764 | p = pos |
| 1765 | while lt p blen bound 16385 |
| 1766 | if not tr_is_ws get buf p |
| 1767 | break |
| 1768 | end |
| 1769 | p = add p 1 |
| 1770 | end |
| 1771 | ret p |
| 1772 | end |
| 1773 | |
| 1774 | note Parses a JSON string starting at buf[pos]=='"', storing unescaped bytes into target kind: |
| 1775 | note kind 0=key(tr_g_item_buf), 1=from, 2=to, 3=model, 4=text. |
| 1776 | note Returns new position after closing '"', or 0 on syntax/nul/oversize error. |
| 1777 | fn tr_parse_jstr buf view uchar * pos ulong blen ulong kind ulong to ulong |
| 1778 | if ge pos blen |
| 1779 | ret 0 |
| 1780 | end |
| 1781 | if ne get buf pos 34 |
| 1782 | ret 0 |
| 1783 | end |
| 1784 | p = add pos 1 |
| 1785 | w = ulong 0 |
| 1786 | cap = ulong 64 |
| 1787 | if eq kind 4 |
| 1788 | cap = TR_MAX_BYTES |
| 1789 | end |
| 1790 | while lt p blen bound 16385 |
| 1791 | c = get buf p |
| 1792 | if eq c 0 |
| 1793 | ret 0 |
| 1794 | end |
| 1795 | if eq c 34 |
| 1796 | if eq kind 0 |
| 1797 | put tr_g_item_len 0 w |
| 1798 | end |
| 1799 | if eq kind 1 |
| 1800 | put tr_g_req_from_len 0 w |
| 1801 | end |
| 1802 | if eq kind 2 |
| 1803 | put tr_g_req_to_len 0 w |
| 1804 | end |
| 1805 | if eq kind 3 |
| 1806 | put tr_g_req_model_len 0 w |
| 1807 | end |
| 1808 | if eq kind 4 |
| 1809 | put tr_g_req_text_len 0 w |
| 1810 | end |
| 1811 | ret add p 1 |
| 1812 | end |
| 1813 | outb = c |
| 1814 | if eq c 92 |
| 1815 | p = add p 1 |
| 1816 | if ge p blen |
| 1817 | ret 0 |
| 1818 | end |
| 1819 | e = get buf p |
| 1820 | if eq e 34 |
| 1821 | outb = 34 |
| 1822 | else |
| 1823 | if eq e 92 |
| 1824 | outb = 92 |
| 1825 | else |
| 1826 | if eq e 110 |
| 1827 | outb = 10 |
| 1828 | else |
| 1829 | if eq e 114 |
| 1830 | outb = 13 |
| 1831 | else |
| 1832 | if eq e 116 |
| 1833 | outb = 9 |
| 1834 | else |
| 1835 | if eq e 47 |
| 1836 | outb = 47 |
| 1837 | else |
| 1838 | ret 0 |
| 1839 | end |
| 1840 | end |
| 1841 | end |
| 1842 | end |
| 1843 | end |
| 1844 | end |
| 1845 | else |
| 1846 | if lt c 32 |
| 1847 | ret 0 |
| 1848 | end |
| 1849 | end |
| 1850 | if ge w cap |
| 1851 | ret 0 |
| 1852 | end |
| 1853 | if eq kind 0 |
| 1854 | put tr_g_item_buf w outb |
| 1855 | end |
| 1856 | if eq kind 1 |
| 1857 | put tr_g_req_from w outb |
| 1858 | end |
| 1859 | if eq kind 2 |
| 1860 | put tr_g_req_to w outb |
| 1861 | end |
| 1862 | if eq kind 3 |
| 1863 | put tr_g_req_model w outb |
| 1864 | end |
| 1865 | if eq kind 4 |
| 1866 | put tr_g_req_text w outb |
| 1867 | end |
| 1868 | w = add w 1 |
| 1869 | p = add p 1 |
| 1870 | end |
| 1871 | ret 0 |
| 1872 | end |
| 1873 | |
| 1874 | fn tr_key_kind to ulong |
| 1875 | klen = get tr_g_item_len 0 |
| 1876 | kb = new uchar 32 0 |
| 1877 | if gt klen 32 |
| 1878 | ret 99 |
| 1879 | end |
| 1880 | i = ulong 0 |
| 1881 | while lt i klen bound 32 |
| 1882 | put kb i get tr_g_item_buf i |
| 1883 | i = add i 1 |
| 1884 | end |
| 1885 | if or tr_eq_exact_slice kb 0 klen tr_jk_from tr_eq_exact_slice kb 0 klen tr_jk_src |
| 1886 | ret 1 |
| 1887 | end |
| 1888 | if or tr_eq_exact_slice kb 0 klen tr_jk_to tr_eq_exact_slice kb 0 klen tr_jk_dst |
| 1889 | ret 2 |
| 1890 | end |
| 1891 | if tr_eq_exact_slice kb 0 klen tr_jk_model |
| 1892 | ret 3 |
| 1893 | end |
| 1894 | if tr_eq_exact_slice kb 0 klen tr_jk_text |
| 1895 | ret 4 |
| 1896 | end |
| 1897 | if tr_eq_exact_slice kb 0 klen tr_jk_maxc |
| 1898 | ret 5 |
| 1899 | end |
| 1900 | ret 99 |
| 1901 | end |
| 1902 | |
| 1903 | fn tr_parse_maxc line view uchar * pos ulong llen ulong to ulong |
| 1904 | p = pos |
| 1905 | val = ulong 0 |
| 1906 | digits = ulong 0 |
| 1907 | while lt p llen bound 16 |
| 1908 | dc = get line p |
| 1909 | if not tr_is_digit dc |
| 1910 | break |
| 1911 | end |
| 1912 | val = add mul val 10 ulong sub dc 48 |
| 1913 | digits = add digits 1 |
| 1914 | p = add p 1 |
| 1915 | end |
| 1916 | if eq digits 0 |
| 1917 | ret 0 |
| 1918 | end |
| 1919 | put tr_g_req_maxc 0 val |
| 1920 | ret p |
| 1921 | end |
| 1922 | |
| 1923 | fn tr_parse_req_line line view uchar * llen ulong to ulong |
| 1924 | put tr_g_req_from_len 0 0 |
| 1925 | put tr_g_req_to_len 0 0 |
| 1926 | put tr_g_req_model_len 0 0 |
| 1927 | put tr_g_req_text_len 0 0 |
| 1928 | put tr_g_req_maxc 0 512 |
| 1929 | if eq llen 0 |
| 1930 | ret TR_ERR_EMPTY |
| 1931 | end |
| 1932 | if gt llen TR_MAX_BYTES |
| 1933 | ret TR_ERR_OVERSIZE |
| 1934 | end |
| 1935 | ni = ulong 0 |
| 1936 | while lt ni llen bound 16385 |
| 1937 | if eq get line ni 0 |
| 1938 | ret TR_ERR_NUL |
| 1939 | end |
| 1940 | ni = add ni 1 |
| 1941 | end |
| 1942 | p = tr_skip_ws line 0 llen |
| 1943 | if ge p llen |
| 1944 | ret TR_ERR_EMPTY |
| 1945 | end |
| 1946 | if ne get line p 123 |
| 1947 | ret TR_ERR_JSON |
| 1948 | end |
| 1949 | p = tr_skip_ws line add p 1 llen |
| 1950 | got_to = false |
| 1951 | got_text = false |
| 1952 | fields = ulong 0 |
| 1953 | while lt fields 16 bound 16 |
| 1954 | if ge p llen |
| 1955 | ret TR_ERR_JSON |
| 1956 | end |
| 1957 | if eq get line p 125 |
| 1958 | p = tr_skip_ws line add p 1 llen |
| 1959 | if ne p llen |
| 1960 | ret TR_ERR_JSON |
| 1961 | end |
| 1962 | break |
| 1963 | end |
| 1964 | p = tr_parse_jstr line p llen 0 |
| 1965 | if eq p 0 |
| 1966 | ret TR_ERR_JSON |
| 1967 | end |
| 1968 | kk = tr_key_kind |
| 1969 | p = tr_skip_ws line p llen |
| 1970 | if ge p llen |
| 1971 | ret TR_ERR_JSON |
| 1972 | end |
| 1973 | if ne get line p 58 |
| 1974 | ret TR_ERR_JSON |
| 1975 | end |
| 1976 | p = tr_skip_ws line add p 1 llen |
| 1977 | if ge p llen |
| 1978 | ret TR_ERR_JSON |
| 1979 | end |
| 1980 | if and ge kk 1 le kk 4 |
| 1981 | p = tr_parse_jstr line p llen kk |
| 1982 | if eq p 0 |
| 1983 | ret TR_ERR_JSON |
| 1984 | end |
| 1985 | if eq kk 2 |
| 1986 | got_to = true |
| 1987 | end |
| 1988 | if eq kk 4 |
| 1989 | got_text = true |
| 1990 | end |
| 1991 | else |
| 1992 | if eq kk 5 |
| 1993 | p = tr_parse_maxc line p llen |
| 1994 | if eq p 0 |
| 1995 | ret TR_ERR_JSON |
| 1996 | end |
| 1997 | else |
| 1998 | ret TR_ERR_JSON |
| 1999 | end |
| 2000 | end |
| 2001 | p = tr_skip_ws line p llen |
| 2002 | if ge p llen |
| 2003 | ret TR_ERR_JSON |
| 2004 | end |
| 2005 | if eq get line p 44 |
| 2006 | p = tr_skip_ws line add p 1 llen |
| 2007 | else |
| 2008 | if ne get line p 125 |
| 2009 | ret TR_ERR_JSON |
| 2010 | end |
| 2011 | end |
| 2012 | fields = add fields 1 |
| 2013 | end |
| 2014 | if not and got_to got_text |
| 2015 | ret TR_ERR_JSON |
| 2016 | end |
| 2017 | ret TR_OK |
| 2018 | end |
| 2019 | |
| 2020 | note --- End-to-End Request Pipeline (validate -> chunk -> cache -> guard -> JSONL) --- |
| 2021 | |
| 2022 | fn tr_resp_lang_code lid ulong to void |
| 2023 | lcode = new uchar 16 0 |
| 2024 | n = tr_lang_code_write lid lcode 0 |
| 2025 | i = ulong 0 |
| 2026 | while lt i n bound 16 |
| 2027 | tr_resp_byte get lcode i |
| 2028 | i = add i 1 |
| 2029 | end |
| 2030 | end |
| 2031 | |
| 2032 | fn tr_emit_one_item src_id ulong dst_id ulong idx ulong to void |
| 2033 | cbuf = new uchar 512 0 |
| 2034 | coff = get tr_g_chk_off idx |
| 2035 | clen = get tr_g_chk_len idx |
| 2036 | if gt clen 512 |
| 2037 | clen = 512 |
| 2038 | end |
| 2039 | ci = ulong 0 |
| 2040 | while lt ci clen bound 512 |
| 2041 | put cbuf ci get tr_g_req_text add coff ci |
| 2042 | ci = add ci 1 |
| 2043 | end |
| 2044 | h2 = tr_cache_get src_id dst_id part cbuf 0 clen clen |
| 2045 | if eq h2 1 |
| 2046 | put tr_g_cache_hits 0 sub get tr_g_cache_hits 0 1 |
| 2047 | tr_resp_json_str_g get tr_g_tmp_out_len 0 |
| 2048 | else |
| 2049 | put tr_g_cache_misses 0 sub get tr_g_cache_misses 0 1 |
| 2050 | ci2 = ulong 0 |
| 2051 | while lt ci2 clen bound 512 |
| 2052 | put tr_g_tmp_out ci2 get cbuf ci2 |
| 2053 | ci2 = add ci2 1 |
| 2054 | end |
| 2055 | tr_resp_json_str_g clen |
| 2056 | end |
| 2057 | end |
| 2058 | |
| 2059 | fn tr_emit_ok_resp src_id ulong dst_id ulong cnt ulong req_hits ulong req_misses ulong to void |
| 2060 | tr_resp_reset |
| 2061 | tr_resp_text tr_j_ok_pre |
| 2062 | tr_resp_lang_code src_id |
| 2063 | tr_resp_text tr_j_to |
| 2064 | tr_resp_lang_code dst_id |
| 2065 | tr_resp_text tr_j_chunks |
| 2066 | tr_resp_u64 cnt |
| 2067 | tr_resp_text tr_j_fc |
| 2068 | tr_resp_u64 req_hits |
| 2069 | tr_resp_text tr_j_ms |
| 2070 | tr_resp_u64 req_misses |
| 2071 | tr_resp_text tr_j_ev |
| 2072 | tr_resp_u64 get tr_g_cache_evicts 0 |
| 2073 | tr_resp_text tr_j_items |
| 2074 | m = ulong 0 |
| 2075 | while lt m cnt bound 64 |
| 2076 | if gt m 0 |
| 2077 | tr_resp_byte 44 |
| 2078 | end |
| 2079 | tr_emit_one_item src_id dst_id m |
| 2080 | m = add m 1 |
| 2081 | end |
| 2082 | tr_resp_text tr_j_end |
| 2083 | end |
| 2084 | |
| 2085 | fn tr_req_from_id to ulong |
| 2086 | fbuf = new uchar 64 0 |
| 2087 | flen = get tr_g_req_from_len 0 |
| 2088 | fi = ulong 0 |
| 2089 | while lt fi flen bound 64 |
| 2090 | put fbuf fi get tr_g_req_from fi |
| 2091 | fi = add fi 1 |
| 2092 | end |
| 2093 | ret tr_lang_find_src fbuf 0 flen |
| 2094 | end |
| 2095 | |
| 2096 | fn tr_req_to_id to ulong |
| 2097 | tbuf = new uchar 64 0 |
| 2098 | tlen = get tr_g_req_to_len 0 |
| 2099 | ti = ulong 0 |
| 2100 | while lt ti tlen bound 64 |
| 2101 | put tbuf ti get tr_g_req_to ti |
| 2102 | ti = add ti 1 |
| 2103 | end |
| 2104 | ret tr_lang_find tbuf 0 tlen |
| 2105 | end |
| 2106 | |
| 2107 | fn tr_process_chunk src_id ulong dst_id ulong idx ulong to ulong |
| 2108 | cbuf = new uchar 512 0 |
| 2109 | coff = get tr_g_chk_off idx |
| 2110 | clen = get tr_g_chk_len idx |
| 2111 | if gt clen TR_CACHE_VAL_CAP |
| 2112 | clen = TR_CACHE_VAL_CAP |
| 2113 | end |
| 2114 | ci = ulong 0 |
| 2115 | while lt ci clen bound 512 |
| 2116 | put cbuf ci get tr_g_req_text add coff ci |
| 2117 | ci = add ci 1 |
| 2118 | end |
| 2119 | hit = tr_cache_get src_id dst_id part cbuf 0 clen clen |
| 2120 | if eq hit 1 |
| 2121 | ret 1 |
| 2122 | end |
| 2123 | ci2 = ulong 0 |
| 2124 | while lt ci2 clen bound 512 |
| 2125 | put tr_g_tmp_out ci2 get cbuf ci2 |
| 2126 | ci2 = add ci2 1 |
| 2127 | end |
| 2128 | put tr_g_tmp_out_len 0 clen |
| 2129 | tr_cache_put src_id dst_id part cbuf 0 clen clen |
| 2130 | ret 0 |
| 2131 | end |
| 2132 | |
| 2133 | fn tr_handle_request line view uchar * llen ulong to ulong |
| 2134 | txt = new uchar 16385 0 |
| 2135 | perr = tr_parse_req_line line llen |
| 2136 | if ne perr TR_OK |
| 2137 | tr_resp_error perr |
| 2138 | ret perr |
| 2139 | end |
| 2140 | src_id = tr_req_from_id |
| 2141 | if eq src_id TR_LANG_INVALID |
| 2142 | tr_resp_error TR_ERR_LANG |
| 2143 | ret TR_ERR_LANG |
| 2144 | end |
| 2145 | dst_id = tr_req_to_id |
| 2146 | if eq dst_id TR_LANG_INVALID |
| 2147 | tr_resp_error TR_ERR_LANG |
| 2148 | ret TR_ERR_LANG |
| 2149 | end |
| 2150 | txlen = get tr_g_req_text_len 0 |
| 2151 | xi = ulong 0 |
| 2152 | while lt xi txlen bound 16385 |
| 2153 | put txt xi get tr_g_req_text xi |
| 2154 | xi = add xi 1 |
| 2155 | end |
| 2156 | cerr = tr_chunk_utf8 txt txlen get tr_g_req_maxc 0 |
| 2157 | if ne cerr TR_OK |
| 2158 | tr_resp_error cerr |
| 2159 | ret cerr |
| 2160 | end |
| 2161 | cnt = get tr_g_chk_cnt 0 |
| 2162 | req_hits = ulong 0 |
| 2163 | req_misses = ulong 0 |
| 2164 | k = ulong 0 |
| 2165 | while lt k cnt bound 64 |
| 2166 | if eq tr_process_chunk src_id dst_id k 1 |
| 2167 | req_hits = add req_hits 1 |
| 2168 | else |
| 2169 | req_misses = add req_misses 1 |
| 2170 | end |
| 2171 | k = add k 1 |
| 2172 | end |
| 2173 | tr_emit_ok_resp src_id dst_id cnt req_hits req_misses |
| 2174 | ret TR_OK |
| 2175 | end |