hive-view
aien-dev/hive-view
Brought by drakestapleton
| 1 | note hive-view src/web.om: HTTP routes. One forked process per browser connection (keep-alive). |
| 2 | note Reads need the session cookie; the only write, POST /send, also needs the form key field k, |
| 3 | note a same-origin Origin, a session name present in a fresh `tmux list-sessions`, and text that |
| 4 | note is valid UTF-8 without control characters. It types that text literally (send-keys -l) |
| 5 | note and then Enter, or sends Enter or Escape alone, and logs one line without the text. |
| 6 | global gah uchar 1024 |
| 7 | global gaho ulong 8 |
| 8 | global gahl ulong 8 |
| 9 | global gahn ulong 1 |
| 10 | global glog uchar 1024 |
| 11 | global glogl ulong 1 |
| 12 | global glfd long 1 |
| 13 | text tget "GET" |
| 14 | text tpost "POST" |
| 15 | text troot "/" |
| 16 | text tpcss "/app.css" |
| 17 | text tpsend "/send" |
| 18 | text tphealth "/health" |
| 19 | text tfcss "app.css" |
| 20 | text tctype "Content-Type" |
| 21 | text tthtml "text/html; charset=utf-8" |
| 22 | text ttcss "text/css; charset=utf-8" |
| 23 | text tttext "text/plain; charset=utf-8" |
| 24 | text tcc "Cache-Control" |
| 25 | text tnostore "no-store" |
| 26 | text tcsp "Content-Security-Policy" |
| 27 | text tcspv "default-src 'none'; style-src 'self'; img-src 'self'; frame-src 'self'; form-action 'self'; frame-ancestors 'self'; base-uri 'none'" |
| 28 | text txcto "X-Content-Type-Options" |
| 29 | text tnosniff "nosniff" |
| 30 | text trefp "Referrer-Policy" |
| 31 | text tnoref "same-origin" |
| 32 | text txfo "X-Frame-Options" |
| 33 | text tdeny "SAMEORIGIN" |
| 34 | text tsetck "Set-Cookie" |
| 35 | text tckpre "hv=" |
| 36 | text tckattr "; Path=/; HttpOnly; SameSite=Strict; Max-Age=31536000" |
| 37 | text tloc "Location" |
| 38 | text thhost "host" |
| 39 | text thcook "cookie" |
| 40 | text thorig "origin" |
| 41 | text thttp "http://" |
| 42 | text tqt "/?t=" |
| 43 | text tok200 "ok\n" |
| 44 | text tbadlink "This link is not valid. Ask for the current link (the token is in ~/.config/hive-view/token on the Spark).\n" |
| 45 | text tnoauth "Not signed in: open the one-time link once on this device.\n" |
| 46 | text tbadkey "Refused: the form key is wrong. Reload the page and try again.\n" |
| 47 | text tnoorig "Refused: cross-origin request.\n" |
| 48 | text tbadbody "Refused: malformed request.\n" |
| 49 | text tbigbody "Refused: the text is longer than 4096 bytes.\n" |
| 50 | text tbadname "Refused: that is not a session name the dashboard can type into.\n" |
| 51 | text tnosess "Refused: no tmux session has that name right now.\n" |
| 52 | text tbadact "Refused: unknown action (allowed: line, enter, escape).\n" |
| 53 | text tbadtext "Refused: the text must be one line of printable characters (no control keys).\n" |
| 54 | text ttmuxbad "tmux did not accept the keys; nothing more was sent.\n" |
| 55 | text tlogbad "Refused: the send log ~/.local/state/hive-view/sends.log cannot be written, so nothing was sent.\n" |
| 56 | text tnotfnd "Not found.\n" |
| 57 | text tmeth "Method not allowed.\n" |
| 58 | text tbadhost "Refused: unknown Host.\n" |
| 59 | text tsendk "send-keys" |
| 60 | text tdashl "-l" |
| 61 | text tdd "--" |
| 62 | text tenter "Enter" |
| 63 | text tescape "Escape" |
| 64 | text taline "line" |
| 65 | text taenter "enter" |
| 66 | text taesc "escape" |
| 67 | text tlogp "/.local/state/hive-view/sends.log" |
| 68 | text tlsfmt1 "#{session_name}" |
| 69 | text tlsessn "session=" |
| 70 | text tlact " action=" |
| 71 | text tlbytes " bytes=" |
| 72 | text tlsha " sha256=" |
| 73 | text tlres " result=" |
| 74 | text tlok "ok" |
| 75 | text tlnosess "refused-no-session" |
| 76 | text tlbadtx "refused-text" |
| 77 | text tlfail "tmux-failed" |
| 78 | text tlatt "attempt" |
| 79 | note ---- request checks |
| 80 | fn vwhostok cs edit ulong 320 io edit uchar 131072 to bool |
| 81 | i = hhfind cs io thhost |
| 82 | if lt i 0 |
| 83 | ret false |
| 84 | end |
| 85 | vo = hhvoff cs ulong i |
| 86 | vl = hhvlen cs ulong i |
| 87 | for k ulong get gahn 0 |
| 88 | if vwahis io vo vl k |
| 89 | ret true |
| 90 | end |
| 91 | end |
| 92 | ret false |
| 93 | end |
| 94 | fn vwahis io edit uchar 131072 vo ulong vl ulong k ulong to bool |
| 95 | if ne vl get gahl k |
| 96 | ret false |
| 97 | end |
| 98 | b = get gaho k |
| 99 | for i ulong vl |
| 100 | if ne get io add vo i get gah add b i |
| 101 | ret false |
| 102 | end |
| 103 | end |
| 104 | ret true |
| 105 | end |
| 106 | note vwtokeq: io[off, off+64) equals the token; the comparison time does not depend on where it differs |
| 107 | fn vwtokeq io edit uchar 131072 off ulong to bool |
| 108 | if gt add off 64 len io |
| 109 | ret false |
| 110 | end |
| 111 | d = uchar 0 |
| 112 | for i ulong 64 |
| 113 | d = or d xor get io add off i get gtok i |
| 114 | end |
| 115 | ret eq d 0 |
| 116 | end |
| 117 | note vwauth: any Cookie header carries hv=<token> as one of its cookies |
| 118 | fn vwauth cs edit ulong 320 io edit uchar 131072 to bool |
| 119 | ok = false |
| 120 | for i ulong get cs HHN |
| 121 | if hieqm view part io hhnoff cs i hhnlen cs i thcook |
| 122 | if vwckval io hhvoff cs i hhvlen cs i |
| 123 | ok = true |
| 124 | end |
| 125 | end |
| 126 | end |
| 127 | ret ok |
| 128 | end |
| 129 | fn vwckval io edit uchar 131072 vo ulong vl ulong to bool |
| 130 | p = ulong 0 |
| 131 | for it ulong 4096 |
| 132 | for s ulong 4096 |
| 133 | if ge p vl |
| 134 | break |
| 135 | end |
| 136 | if ne get io add vo p 32 |
| 137 | break |
| 138 | end |
| 139 | p = add p 1 |
| 140 | end |
| 141 | if le add p 67 vl |
| 142 | if vwckat io add vo p |
| 143 | if eq add p 67 vl |
| 144 | ret vwtokeq io add add vo p 3 |
| 145 | end |
| 146 | if eq get io add add vo p 67 59 |
| 147 | ret vwtokeq io add add vo p 3 |
| 148 | end |
| 149 | end |
| 150 | end |
| 151 | for s ulong 4096 |
| 152 | if ge p vl |
| 153 | ret false |
| 154 | end |
| 155 | if eq get io add vo p 59 |
| 156 | break |
| 157 | end |
| 158 | p = add p 1 |
| 159 | end |
| 160 | p = add p 1 |
| 161 | end |
| 162 | ret false |
| 163 | end |
| 164 | fn vwckat io edit uchar 131072 o ulong to bool |
| 165 | if ne get io o 104 |
| 166 | ret false |
| 167 | end |
| 168 | if ne get io add o 1 118 |
| 169 | ret false |
| 170 | end |
| 171 | ret eq get io add o 2 61 |
| 172 | end |
| 173 | note vworigin: the Origin header is present and exactly "http://" + the (already checked) Host. |
| 174 | note Browsers send it on every form POST (Referrer-Policy same-origin keeps it from being "null"). |
| 175 | fn vworigin cs edit ulong 320 io edit uchar 131072 to bool |
| 176 | i = hhfind cs io thorig |
| 177 | if lt i 0 |
| 178 | ret false |
| 179 | end |
| 180 | h = hhfind cs io thhost |
| 181 | if lt h 0 |
| 182 | ret false |
| 183 | end |
| 184 | oo = hhvoff cs ulong i |
| 185 | ol = hhvlen cs ulong i |
| 186 | ho = hhvoff cs ulong h |
| 187 | hl = hhvlen cs ulong h |
| 188 | m = len thttp |
| 189 | if ne ol add m hl |
| 190 | ret false |
| 191 | end |
| 192 | if not heq view part io oo m thttp |
| 193 | ret false |
| 194 | end |
| 195 | ret vwioeq io add oo m ho hl |
| 196 | end |
| 197 | fn vwioeq io edit uchar 131072 a ulong b ulong n ulong to bool |
| 198 | for i ulong n |
| 199 | if ne get io add a i get io add b i |
| 200 | ret false |
| 201 | end |
| 202 | end |
| 203 | ret true |
| 204 | end |
| 205 | note ---- responses |
| 206 | fn vwsech cs edit ulong 320 io edit uchar 131072 to long |
| 207 | r = hhname cs io tcc |
| 208 | r = hfe r hhval cs io tnostore |
| 209 | r = hfe r hhname cs io txcto |
| 210 | r = hfe r hhval cs io tnosniff |
| 211 | r = hfe r hhname cs io trefp |
| 212 | r = hfe r hhval cs io tnoref |
| 213 | r = hfe r hhname cs io txfo |
| 214 | r = hfe r hhval cs io tdeny |
| 215 | r = hfe r hhname cs io tcsp |
| 216 | ret hfe r hhval cs io tcspv |
| 217 | end |
| 218 | fn vwtext cs edit ulong 320 io edit uchar 131072 code ulong t view uchar * to long |
| 219 | r = hrspst cs io code |
| 220 | r = hfe r vwsech cs io |
| 221 | r = hfe r hhname cs io tctype |
| 222 | r = hfe r hhval cs io tttext |
| 223 | r = hfe r hhend cs io 0 len t |
| 224 | r = hfe r hput cs io t |
| 225 | ret hfe r hwfin cs io |
| 226 | end |
| 227 | fn vwcss cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 to long |
| 228 | vwpb |
| 229 | vwpt tfcss |
| 230 | n = vwrdbuf buf |
| 231 | if lt n 0 |
| 232 | ret vwtext cs io 404 tnotfnd |
| 233 | end |
| 234 | r = hrspst cs io 200 |
| 235 | r = hfe r vwsech cs io |
| 236 | r = hfe r hhname cs io tctype |
| 237 | r = hfe r hhval cs io ttcss |
| 238 | r = hfe r hhend cs io 0 ulong n |
| 239 | r = hfe r hput cs io view part buf 0 ulong n |
| 240 | ret hfe r hwfin cs io |
| 241 | end |
| 242 | note vwrdbuf: the whole file at gpth into buf (refused when it does not fit) |
| 243 | fn vwrdbuf buf edit uchar 262144 to long |
| 244 | fd = fopen gpth 0 get gpl 0 524288 0 |
| 245 | if lt fd 0 |
| 246 | ret fd |
| 247 | end |
| 248 | n = ulong 0 |
| 249 | for it ulong 100000 |
| 250 | if ge n len buf |
| 251 | x = fclose int fd |
| 252 | ret -27 |
| 253 | end |
| 254 | r = read int fd buf n sub len buf n |
| 255 | if lt r 0 |
| 256 | x = fclose int fd |
| 257 | ret r |
| 258 | end |
| 259 | if eq r 0 |
| 260 | break |
| 261 | end |
| 262 | n = add n ulong r |
| 263 | end |
| 264 | x = fclose int fd |
| 265 | ret long n |
| 266 | end |
| 267 | note vwlogin: GET /?t=<64 hex> sets the HttpOnly SameSite=Strict cookie and redirects to / |
| 268 | fn vwlogin cs edit ulong 320 io edit uchar 131072 to long |
| 269 | tg = get cs HTOFF |
| 270 | tl = get cs HTLEN |
| 271 | ok = false |
| 272 | if eq tl add len tqt 64 |
| 273 | if heq view part io tg len tqt tqt |
| 274 | ok = vwtokeq io add tg len tqt |
| 275 | end |
| 276 | end |
| 277 | if not ok |
| 278 | ret vwtext cs io 403 tbadlink |
| 279 | end |
| 280 | ck = new uchar 160 0 |
| 281 | n = ulong 0 |
| 282 | for i ulong len tckpre |
| 283 | put ck n get tckpre i |
| 284 | n = add n 1 |
| 285 | end |
| 286 | for i ulong 64 |
| 287 | put ck n get gtok i |
| 288 | n = add n 1 |
| 289 | end |
| 290 | for i ulong len tckattr |
| 291 | put ck n get tckattr i |
| 292 | n = add n 1 |
| 293 | end |
| 294 | r = hrspst cs io 302 |
| 295 | r = hfe r vwsech cs io |
| 296 | r = hfe r hhname cs io tsetck |
| 297 | r = hfe r hhval cs io view part ck 0 n |
| 298 | r = hfe r hhname cs io tloc |
| 299 | r = hfe r hhval cs io troot |
| 300 | r = hfe r hhend cs io 0 0 |
| 301 | ret hfe r hwfin cs io |
| 302 | end |
| 303 | fn vwnlat buf edit uchar 262144 p ulong n ulong to ulong |
| 304 | for i ulong sub n p |
| 305 | if eq get buf add p i 10 |
| 306 | ret add p i |
| 307 | end |
| 308 | end |
| 309 | ret n |
| 310 | end |
| 311 | fn vwbeq buf edit uchar 262144 a ulong n ulong t view uchar * to bool |
| 312 | if ne n len t |
| 313 | ret false |
| 314 | end |
| 315 | for i ulong n |
| 316 | if ne get buf add a i get t i |
| 317 | ret false |
| 318 | end |
| 319 | end |
| 320 | ret true |
| 321 | end |
| 322 | note vwsendp: body "NAME<LF>ACTION<LF>TEXT". Returns 0 sent, 1 malformed, 2 bad name, |
| 323 | note 3 bad action, 4 bad text, 5 no such session, 6 tmux failed, 7 text too long, 8 send log |
| 324 | note cannot be opened or the attempt line cannot be written (both before anything is typed). |
| 325 | note Codes 1, 2, 3 and 7 are refused before the log is opened. The form handler (route.om vwfbuild) |
| 326 | note never passes text with Enter or Escape; code 4 for that case guards direct callers only. |
| 327 | fn vwsendp buf edit uchar 262144 n ulong to ulong |
| 328 | e1 = vwnlat buf 0 n |
| 329 | if ge e1 n |
| 330 | ret 1 |
| 331 | end |
| 332 | e2 = vwnlat buf add e1 1 n |
| 333 | if ge e2 n |
| 334 | ret 1 |
| 335 | end |
| 336 | if or eq e1 0 gt e1 63 |
| 337 | ret 2 |
| 338 | end |
| 339 | for i ulong e1 |
| 340 | if not vwnamec get buf i |
| 341 | ret 2 |
| 342 | end |
| 343 | end |
| 344 | act = ulong 9 |
| 345 | if vwbeq buf add e1 1 sub sub e2 e1 1 taline |
| 346 | act = 0 |
| 347 | end |
| 348 | if vwbeq buf add e1 1 sub sub e2 e1 1 taenter |
| 349 | act = 1 |
| 350 | end |
| 351 | if vwbeq buf add e1 1 sub sub e2 e1 1 taesc |
| 352 | act = 2 |
| 353 | end |
| 354 | if eq act 9 |
| 355 | ret 3 |
| 356 | end |
| 357 | t0 = add e2 1 |
| 358 | tn = sub n t0 |
| 359 | if gt tn 4096 |
| 360 | ret 7 |
| 361 | end |
| 362 | if ne act 0 |
| 363 | if ne tn 0 |
| 364 | ret 4 |
| 365 | end |
| 366 | end |
| 367 | vwhpath tlogp |
| 368 | lf = fopen gpth 0 get gpl 0 525377 384 |
| 369 | if lt lf 0 |
| 370 | ret 8 |
| 371 | end |
| 372 | put glfd 0 lf |
| 373 | if not vwutf8 view part buf t0 tn |
| 374 | x = vwlogl buf e1 act t0 tn 1 |
| 375 | vwlcl |
| 376 | ret 4 |
| 377 | end |
| 378 | if not vwlive buf e1 |
| 379 | x = vwlogl buf e1 act t0 tn 2 |
| 380 | vwlcl |
| 381 | ret 5 |
| 382 | end |
| 383 | if not vwlogl buf e1 act t0 tn 4 |
| 384 | vwlcl |
| 385 | ret 8 |
| 386 | end |
| 387 | ok = vwtype buf e1 act t0 tn |
| 388 | if ok |
| 389 | x = vwlogl buf e1 act t0 tn 0 |
| 390 | vwlcl |
| 391 | ret 0 |
| 392 | end |
| 393 | x = vwlogl buf e1 act t0 tn 3 |
| 394 | vwlcl |
| 395 | ret 6 |
| 396 | end |
| 397 | note vwlcl: close the send log opened by vwsendp |
| 398 | fn vwlcl to void |
| 399 | x = fclose int get glfd 0 |
| 400 | end |
| 401 | note vwutf8: well-formed UTF-8 with no C0 control, DEL or C1 control character |
| 402 | fn vwutf8 a view uchar * to bool |
| 403 | n = len a |
| 404 | i = ulong 0 |
| 405 | for it ulong 8192 |
| 406 | if ge i n |
| 407 | ret true |
| 408 | end |
| 409 | c = get a i |
| 410 | if lt c 128 |
| 411 | if or lt c 32 eq c 127 |
| 412 | ret false |
| 413 | end |
| 414 | i = add i 1 |
| 415 | continue |
| 416 | end |
| 417 | k = vwu8len a i |
| 418 | if eq k 0 |
| 419 | ret false |
| 420 | end |
| 421 | i = add i k |
| 422 | end |
| 423 | ret false |
| 424 | end |
| 425 | note vwu8len: length of the well-formed multi-byte sequence at a[i], or 0 (RFC 3629 table 3-7) |
| 426 | fn vwu8len a view uchar * i ulong to ulong |
| 427 | n = len a |
| 428 | c = get a i |
| 429 | lo = uchar 128 |
| 430 | hi = uchar 191 |
| 431 | k = ulong 0 |
| 432 | if and ge c 194 le c 223 |
| 433 | k = 2 |
| 434 | if eq c 194 |
| 435 | lo = 160 |
| 436 | end |
| 437 | end |
| 438 | if and ge c 224 le c 239 |
| 439 | k = 3 |
| 440 | if eq c 224 |
| 441 | lo = 160 |
| 442 | end |
| 443 | if eq c 237 |
| 444 | hi = 159 |
| 445 | end |
| 446 | end |
| 447 | if and ge c 240 le c 244 |
| 448 | k = 4 |
| 449 | if eq c 240 |
| 450 | lo = 144 |
| 451 | end |
| 452 | if eq c 244 |
| 453 | hi = 143 |
| 454 | end |
| 455 | end |
| 456 | if eq k 0 |
| 457 | ret 0 |
| 458 | end |
| 459 | if gt add i k n |
| 460 | ret 0 |
| 461 | end |
| 462 | b = get a add i 1 |
| 463 | if or lt b lo gt b hi |
| 464 | ret 0 |
| 465 | end |
| 466 | for j ulong sub k 2 |
| 467 | b = get a add i add j 2 |
| 468 | if or lt b 128 gt b 191 |
| 469 | ret 0 |
| 470 | end |
| 471 | end |
| 472 | ret k |
| 473 | end |
| 474 | note vwlive: buf[0, e1) is exactly one line of a fresh `tmux list-sessions -F #{session_name}` |
| 475 | fn vwlive buf edit uchar 262144 e1 ulong to bool |
| 476 | vwkb |
| 477 | vwka ttmux |
| 478 | vwka tlsess |
| 479 | vwka tdashf |
| 480 | vwka tlsfmt1 |
| 481 | r = vwrun 3000000000 |
| 482 | if ne r 0 |
| 483 | ret false |
| 484 | end |
| 485 | a = ulong 0 |
| 486 | n = get gol 0 |
| 487 | for i ulong n |
| 488 | if eq get gob i 10 |
| 489 | if vwobeq buf e1 a i |
| 490 | ret true |
| 491 | end |
| 492 | a = add i 1 |
| 493 | end |
| 494 | end |
| 495 | ret false |
| 496 | end |
| 497 | fn vwobeq buf edit uchar 262144 e1 ulong a ulong b ulong to bool |
| 498 | if ne sub b a e1 |
| 499 | ret false |
| 500 | end |
| 501 | for i ulong e1 |
| 502 | if ne get gob add a i get buf i |
| 503 | ret false |
| 504 | end |
| 505 | end |
| 506 | ret true |
| 507 | end |
| 508 | note vwtarget: the packed argument "=NAME:" (exact session match, its active pane) |
| 509 | fn vwtarget buf edit uchar 262144 e1 ulong to void |
| 510 | vwkopen |
| 511 | vwkc 61 |
| 512 | for i ulong e1 |
| 513 | vwkc get buf i |
| 514 | end |
| 515 | vwkc 58 |
| 516 | end |
| 517 | fn vwkey buf edit uchar 262144 e1 ulong key view uchar * to bool |
| 518 | vwkb |
| 519 | vwka ttmux |
| 520 | vwka tsendk |
| 521 | vwka tdasht |
| 522 | vwtarget buf e1 |
| 523 | vwka key |
| 524 | ret eq vwrun 5000000000 0 |
| 525 | end |
| 526 | note vwtype: send-keys -l in chunks of at most 2048 bytes cut at character boundaries (a packed |
| 527 | note argument list holds 4096 bytes), then Enter. tmux 3.4 would pass a split character through |
| 528 | note intact, but each chunk is a separate write to the program in the pane, and a reader that |
| 529 | note decodes each write alone would show a broken character. tmux reads an argument ending in ';' |
| 530 | note as a command separator; such a chunk is sent as "...\;", which tmux turns back into "...;". |
| 531 | fn vwtype buf edit uchar 262144 e1 ulong act ulong t0 ulong tn ulong to bool |
| 532 | if eq act 1 |
| 533 | ret vwkey buf e1 tenter |
| 534 | end |
| 535 | if eq act 2 |
| 536 | ret vwkey buf e1 tescape |
| 537 | end |
| 538 | p = t0 |
| 539 | e = add t0 tn |
| 540 | for it ulong 8 |
| 541 | if ge p e |
| 542 | break |
| 543 | end |
| 544 | q = hmin e add p 2048 |
| 545 | for s ulong 4 |
| 546 | if ge q e |
| 547 | break |
| 548 | end |
| 549 | if ne and get buf q 192 128 |
| 550 | break |
| 551 | end |
| 552 | q = sub q 1 |
| 553 | end |
| 554 | if not vwchunk buf e1 p q |
| 555 | ret false |
| 556 | end |
| 557 | p = q |
| 558 | end |
| 559 | ret vwkey buf e1 tenter |
| 560 | end |
| 561 | fn vwchunk buf edit uchar 262144 e1 ulong p ulong q ulong to bool |
| 562 | vwkb |
| 563 | vwka ttmux |
| 564 | vwka tsendk |
| 565 | vwka tdasht |
| 566 | vwtarget buf e1 |
| 567 | vwka tdashl |
| 568 | vwka tdd |
| 569 | vwkopen |
| 570 | for i ulong sub q p |
| 571 | if and eq add p i sub q 1 eq get buf add p i 59 |
| 572 | vwkc 92 |
| 573 | end |
| 574 | vwkc get buf add p i |
| 575 | end |
| 576 | ret eq vwrun 5000000000 0 |
| 577 | end |
| 578 | note ---- the send log: one line per attempt that reached the session check (never the text) |
| 579 | fn vwlc c uchar to void |
| 580 | n = get glogl 0 |
| 581 | if lt n len glog |
| 582 | put glog n c |
| 583 | put glogl 0 add n 1 |
| 584 | end |
| 585 | end |
| 586 | fn vwlt t view uchar * to void |
| 587 | for i ulong len t |
| 588 | vwlc get t i |
| 589 | end |
| 590 | end |
| 591 | fn vwld v ulong w ulong to void |
| 592 | d = new uchar 20 0 |
| 593 | i = ulong 20 |
| 594 | x = v |
| 595 | for it ulong 20 |
| 596 | i = sub i 1 |
| 597 | put d i uchar add rem x 10 48 |
| 598 | x = div x 10 |
| 599 | if and eq x 0 ge sub 20 i w |
| 600 | break |
| 601 | end |
| 602 | end |
| 603 | for k ulong sub 20 i |
| 604 | vwlc get d add i k |
| 605 | end |
| 606 | end |
| 607 | note vwltime: realns as UTC YYYY-MM-DDTHH:MM:SSZ (civil-from-days, proleptic Gregorian) |
| 608 | fn vwltime to void |
| 609 | s = div ulong realns 1000000000 |
| 610 | vwldate div s 86400 |
| 611 | sec = rem s 86400 |
| 612 | vwlc 84 |
| 613 | vwld div sec 3600 2 |
| 614 | vwlc 58 |
| 615 | vwld div rem sec 3600 60 2 |
| 616 | vwlc 58 |
| 617 | vwld rem sec 60 2 |
| 618 | vwlc 90 |
| 619 | end |
| 620 | fn vwldate days ulong to void |
| 621 | z = add days 719468 |
| 622 | era = div z 146097 |
| 623 | doe = sub z mul era 146097 |
| 624 | yoe = vwyoe doe |
| 625 | doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 |
| 626 | mp = div add mul 5 doy 2 153 |
| 627 | m = add mp 3 |
| 628 | if ge mp 10 |
| 629 | m = sub mp 9 |
| 630 | end |
| 631 | y = add yoe mul era 400 |
| 632 | if le m 2 |
| 633 | y = add y 1 |
| 634 | end |
| 635 | vwld y 4 |
| 636 | vwlc 45 |
| 637 | vwld m 2 |
| 638 | vwlc 45 |
| 639 | vwld add sub doy div add mul 153 mp 2 5 1 2 |
| 640 | end |
| 641 | fn vwyoe doe ulong to ulong |
| 642 | a = sub doe div doe 1460 |
| 643 | b = add a div doe 36524 |
| 644 | ret div sub b div doe 146096 365 |
| 645 | end |
| 646 | fn vwlres res ulong to void |
| 647 | if eq res 0 |
| 648 | vwlt tlok |
| 649 | ret |
| 650 | end |
| 651 | if eq res 1 |
| 652 | vwlt tlbadtx |
| 653 | ret |
| 654 | end |
| 655 | if eq res 2 |
| 656 | vwlt tlnosess |
| 657 | ret |
| 658 | end |
| 659 | if eq res 4 |
| 660 | vwlt tlatt |
| 661 | ret |
| 662 | end |
| 663 | vwlt tlfail |
| 664 | end |
| 665 | note vwlogl: append one line to the open send log; true only when the whole line was written |
| 666 | fn vwlogl buf edit uchar 262144 e1 ulong act ulong t0 ulong tn ulong res ulong to bool |
| 667 | put glogl 0 0 |
| 668 | vwltime |
| 669 | vwlc 32 |
| 670 | vwlt tlsessn |
| 671 | for i ulong e1 |
| 672 | vwlc get buf i |
| 673 | end |
| 674 | vwlt tlact |
| 675 | if eq act 0 |
| 676 | vwlt taline |
| 677 | end |
| 678 | if eq act 1 |
| 679 | vwlt taenter |
| 680 | end |
| 681 | if eq act 2 |
| 682 | vwlt taesc |
| 683 | end |
| 684 | vwlt tlbytes |
| 685 | vwld tn 1 |
| 686 | vwlt tlsha |
| 687 | dg = new uchar 32 0 |
| 688 | k = sha2_digest 256 view part buf t0 tn dg |
| 689 | for i ulong 32 |
| 690 | c = get dg i |
| 691 | vwlc get thexd shr c 4 |
| 692 | vwlc get thexd and c 15 |
| 693 | end |
| 694 | vwlt tlres |
| 695 | vwlres res |
| 696 | vwlc 10 |
| 697 | fd = get glfd 0 |
| 698 | w = write int fd glog 0 get glogl 0 |
| 699 | ret eq w long get glogl 0 |
| 700 | end |