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 / aienux / atlas / lib.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/atlas/lib.om: Pure-Omega Atlas Stage-0 Boot Verification Engine. |
| 3 | note Faithful OM-2 library (no om2 line, no fn main) derived from upstream atlas |
| 4 | note (CONTRACT-QEMU-VIRT-AARCH64-M1, atlas.s, sha256_clean.c, machine_contract.json). |
| 5 | note Compose with: si/directives/om2.om aienux/atlas/lib.om <main.om|test.om> |
| 6 | |
| 7 | struct AtlasHeader magic uint ver uint stage_len ulong entry_off ulong epoch ulong end |
| 8 | |
| 9 | struct AtlasBootDesc ram_base ulong ram_size ulong uart_base ulong payload_base ulong payload_size ulong entry_addr ulong desc_addr ulong stack_top ulong cookie ulong epoch ulong verdict uint halted uint end |
| 10 | |
| 11 | note Contract constants (CONTRACT-QEMU-VIRT-AARCH64-M1) |
| 12 | const ATLAS_MAGIC_LE uint 0x534c5441 |
| 13 | const ATLAS_HDR_VER uint 1 |
| 14 | const ATLAS_HDR_SIZE ulong 64 |
| 15 | const ATLAS_RAM_BASE ulong 0x40000000 |
| 16 | const ATLAS_RAM_SIZE ulong 0x08000000 |
| 17 | const ATLAS_UART_BASE ulong 0x09000000 |
| 18 | const ATLAS_STACK_BASE ulong 0x401f0000 |
| 19 | const ATLAS_STACK_TOP ulong 0x401fc000 |
| 20 | const ATLAS_GUARD_GAP ulong 8192 |
| 21 | const ATLAS_DESC_ADDR ulong 0x401fe000 |
| 22 | const ATLAS_DESC_SIZE ulong 64 |
| 23 | const ATLAS_PAYLOAD_BASE ulong 0x40200000 |
| 24 | const ATLAS_MAX_STAGE ulong 0x04000000 |
| 25 | const ATLAS_COOKIE ulong 0x5048595349435330 |
| 26 | |
| 27 | note Verification status / fail-closed refusal codes |
| 28 | const ATLAS_OK uint 0 |
| 29 | const ATLAS_ERR_SHORT_HDR uint 1 |
| 30 | const ATLAS_ERR_MAGIC uint 2 |
| 31 | const ATLAS_ERR_VERSION uint 3 |
| 32 | const ATLAS_ERR_STAGE_LEN uint 4 |
| 33 | const ATLAS_ERR_ALIGN uint 5 |
| 34 | const ATLAS_ERR_ENTRY_OOB uint 6 |
| 35 | const ATLAS_ERR_TRUNCATED uint 7 |
| 36 | const ATLAS_ERR_ROLLBACK uint 8 |
| 37 | const ATLAS_ERR_MEMMAP uint 9 |
| 38 | const ATLAS_ERR_DIGEST uint 10 |
| 39 | |
| 40 | note ---- Self-contained FIPS 180-4 SHA-256 (matching upstream sha256_clean.c) ---- |
| 41 | |
| 42 | fn atlas_sha256_k k edit uint 64 to void |
| 43 | put k 0 0x428a2f98 |
| 44 | put k 1 0x71374491 |
| 45 | put k 2 0xb5c0fbcf |
| 46 | put k 3 0xe9b5dba5 |
| 47 | put k 4 0x3956c25b |
| 48 | put k 5 0x59f111f1 |
| 49 | put k 6 0x923f82a4 |
| 50 | put k 7 0xab1c5ed5 |
| 51 | put k 8 0xd807aa98 |
| 52 | put k 9 0x12835b01 |
| 53 | put k 10 0x243185be |
| 54 | put k 11 0x550c7dc3 |
| 55 | put k 12 0x72be5d74 |
| 56 | put k 13 0x80deb1fe |
| 57 | put k 14 0x9bdc06a7 |
| 58 | put k 15 0xc19bf174 |
| 59 | put k 16 0xe49b69c1 |
| 60 | put k 17 0xefbe4786 |
| 61 | put k 18 0x0fc19dc6 |
| 62 | put k 19 0x240ca1cc |
| 63 | put k 20 0x2de92c6f |
| 64 | put k 21 0x4a7484aa |
| 65 | put k 22 0x5cb0a9dc |
| 66 | put k 23 0x76f988da |
| 67 | put k 24 0x983e5152 |
| 68 | put k 25 0xa831c66d |
| 69 | put k 26 0xb00327c8 |
| 70 | put k 27 0xbf597fc7 |
| 71 | put k 28 0xc6e00bf3 |
| 72 | put k 29 0xd5a79147 |
| 73 | put k 30 0x06ca6351 |
| 74 | put k 31 0x14292967 |
| 75 | put k 32 0x27b70a85 |
| 76 | put k 33 0x2e1b2138 |
| 77 | put k 34 0x4d2c6dfc |
| 78 | put k 35 0x53380d13 |
| 79 | put k 36 0x650a7354 |
| 80 | put k 37 0x766a0abb |
| 81 | put k 38 0x81c2c92e |
| 82 | put k 39 0x92722c85 |
| 83 | put k 40 0xa2bfe8a1 |
| 84 | put k 41 0xa81a664b |
| 85 | put k 42 0xc24b8b70 |
| 86 | put k 43 0xc76c51a3 |
| 87 | put k 44 0xd192e819 |
| 88 | put k 45 0xd6990624 |
| 89 | put k 46 0xf40e3585 |
| 90 | put k 47 0x106aa070 |
| 91 | put k 48 0x19a4c116 |
| 92 | put k 49 0x1e376c08 |
| 93 | put k 50 0x2748774c |
| 94 | put k 51 0x34b0bcb5 |
| 95 | put k 52 0x391c0cb3 |
| 96 | put k 53 0x4ed8aa4a |
| 97 | put k 54 0x5b9cca4f |
| 98 | put k 55 0x682e6ff3 |
| 99 | put k 56 0x748f82ee |
| 100 | put k 57 0x78a5636f |
| 101 | put k 58 0x84c87814 |
| 102 | put k 59 0x8cc70208 |
| 103 | put k 60 0x90befffa |
| 104 | put k 61 0xa4506ceb |
| 105 | put k 62 0xbef9a3f7 |
| 106 | put k 63 0xc67178f2 |
| 107 | end |
| 108 | |
| 109 | fn atlas_be32 buf view uchar * off ulong to uint |
| 110 | b0 = shl uint get buf off 24 |
| 111 | b1 = shl uint get buf add off 1 16 |
| 112 | b2 = shl uint get buf add off 2 8 |
| 113 | b3 = uint get buf add off 3 |
| 114 | ret or or b0 b1 or b2 b3 |
| 115 | end |
| 116 | |
| 117 | fn atlas_s0 x uint to uint |
| 118 | ret xor xor rotr x 7 rotr x 18 shr x 3 |
| 119 | end |
| 120 | |
| 121 | fn atlas_s1 x uint to uint |
| 122 | ret xor xor rotr x 17 rotr x 19 shr x 10 |
| 123 | end |
| 124 | |
| 125 | fn atlas_cap0 x uint to uint |
| 126 | ret xor xor rotr x 2 rotr x 13 rotr x 22 |
| 127 | end |
| 128 | |
| 129 | fn atlas_cap1 x uint to uint |
| 130 | ret xor xor rotr x 6 rotr x 11 rotr x 25 |
| 131 | end |
| 132 | |
| 133 | fn atlas_ch x uint y uint z uint to uint |
| 134 | ret xor and x y and not x z |
| 135 | end |
| 136 | |
| 137 | fn atlas_maj x uint y uint z uint to uint |
| 138 | ret xor xor and x y and x z and y z |
| 139 | end |
| 140 | |
| 141 | fn atlas_sha256_sched w edit uint 64 blk view uchar * boff ulong to void |
| 142 | for i ulong 16 |
| 143 | put w i atlas_be32 blk add boff mul i 4 |
| 144 | end |
| 145 | i = ulong 16 |
| 146 | while lt i 64 bound 64 |
| 147 | t0 = atlas_s0 get w sub i 15 |
| 148 | t1 = atlas_s1 get w sub i 2 |
| 149 | val = add add get w sub i 16 t0 add get w sub i 7 t1 |
| 150 | put w i val |
| 151 | i = add i 1 |
| 152 | end |
| 153 | end |
| 154 | |
| 155 | fn atlas_sha256_compress st edit uint 8 w view uint 64 k view uint 64 to void |
| 156 | v = new uint 8 0 |
| 157 | for i ulong 8 |
| 158 | put v i get st i |
| 159 | end |
| 160 | for i ulong 64 |
| 161 | t1 = add get v 7 atlas_cap1 get v 4 |
| 162 | t1 = add t1 atlas_ch get v 4 get v 5 get v 6 |
| 163 | t1 = add t1 get k i |
| 164 | t1 = add t1 get w i |
| 165 | t2 = add atlas_cap0 get v 0 atlas_maj get v 0 get v 1 get v 2 |
| 166 | put v 7 get v 6 |
| 167 | put v 6 get v 5 |
| 168 | put v 5 get v 4 |
| 169 | put v 4 add get v 3 t1 |
| 170 | put v 3 get v 2 |
| 171 | put v 2 get v 1 |
| 172 | put v 1 get v 0 |
| 173 | put v 0 add t1 t2 |
| 174 | end |
| 175 | for i ulong 8 |
| 176 | put st i add get st i get v i |
| 177 | put v i 0 |
| 178 | end |
| 179 | end |
| 180 | |
| 181 | fn atlas_sha256_block st edit uint 8 k view uint 64 blk view uchar * boff ulong to void |
| 182 | w = new uint 64 0 |
| 183 | atlas_sha256_sched w blk boff |
| 184 | atlas_sha256_compress st w k |
| 185 | for i ulong 64 |
| 186 | put w i 0 |
| 187 | end |
| 188 | end |
| 189 | |
| 190 | fn atlas_put_be64 buf edit uchar 128 off ulong v ulong to void |
| 191 | for j ulong 8 |
| 192 | sh = mul sub 7 j 8 |
| 193 | b = uchar and shr v sh 0xff |
| 194 | put buf add off j b |
| 195 | end |
| 196 | end |
| 197 | |
| 198 | fn atlas_sha256_pad_fill pad edit uchar 128 msg view uchar * off ulong to ulong |
| 199 | n = len msg |
| 200 | remn = sub n off |
| 201 | i = ulong 0 |
| 202 | while lt i remn bound 64 |
| 203 | put pad i get msg add off i |
| 204 | i = add i 1 |
| 205 | end |
| 206 | put pad remn uchar 0x80 |
| 207 | pblks = ulong 1 |
| 208 | if ge remn 56 |
| 209 | pblks = ulong 2 |
| 210 | end |
| 211 | atlas_put_be64 pad sub mul pblks 64 8 shl n 3 |
| 212 | ret pblks |
| 213 | end |
| 214 | |
| 215 | fn atlas_sha256_pad_final st edit uint 8 k view uint 64 msg view uchar * off ulong to void |
| 216 | pad = new uchar 128 0 |
| 217 | pblks = atlas_sha256_pad_fill pad msg off |
| 218 | atlas_sha256_block st k pad 0 |
| 219 | if eq pblks 2 |
| 220 | atlas_sha256_block st k pad 64 |
| 221 | end |
| 222 | for j ulong 128 |
| 223 | put pad j 0 |
| 224 | end |
| 225 | end |
| 226 | |
| 227 | fn atlas_sha256_emit st edit uint 8 digest edit uchar 32 to void |
| 228 | for j ulong 8 |
| 229 | wd = get st j |
| 230 | bo = mul j 4 |
| 231 | put digest bo uchar and shr wd 24 0xff |
| 232 | put digest add bo 1 uchar and shr wd 16 0xff |
| 233 | put digest add bo 2 uchar and shr wd 8 0xff |
| 234 | put digest add bo 3 uchar and wd 0xff |
| 235 | put st j 0 |
| 236 | end |
| 237 | end |
| 238 | |
| 239 | fn atlas_sha256_compute msg view uchar * digest edit uchar 32 to void |
| 240 | st = new uint 8 0 |
| 241 | k = new uint 64 0 |
| 242 | atlas_sha256_k k |
| 243 | put st 0 0x6a09e667 |
| 244 | put st 1 0xbb67ae85 |
| 245 | put st 2 0x3c6ef372 |
| 246 | put st 3 0xa54ff53a |
| 247 | put st 4 0x510e527f |
| 248 | put st 5 0x9b05688c |
| 249 | put st 6 0x1f83d9ab |
| 250 | put st 7 0x5be0cd19 |
| 251 | off = ulong 0 |
| 252 | remn = len msg |
| 253 | note Each iteration consumes 64 bytes; bound 1048576 supports up to 64 MiB stages. |
| 254 | while ge remn 64 bound 1048576 |
| 255 | atlas_sha256_block st k msg off |
| 256 | off = add off 64 |
| 257 | remn = sub remn 64 |
| 258 | end |
| 259 | atlas_sha256_pad_final st k msg off |
| 260 | atlas_sha256_emit st digest |
| 261 | end |
| 262 | |
| 263 | note ---- Constant-time 32-byte digest comparison (zero early exit, zero secret branches) ---- |
| 264 | |
| 265 | fn atlas_ct_eq32 a view uchar * b view uchar * to bool |
| 266 | if ne len a 32 |
| 267 | ret false |
| 268 | end |
| 269 | if ne len b 32 |
| 270 | ret false |
| 271 | end |
| 272 | acc = uint 0 |
| 273 | for i ulong 32 |
| 274 | da = uint get a i |
| 275 | db = uint get b i |
| 276 | acc = or acc xor da db |
| 277 | end |
| 278 | ret eq acc 0 |
| 279 | end |
| 280 | |
| 281 | note ---- Little-endian wire encoding/decoding ---- |
| 282 | |
| 283 | fn atlas_le32 buf view uchar * off ulong to uint |
| 284 | b0 = uint get buf off |
| 285 | b1 = shl uint get buf add off 1 8 |
| 286 | b2 = shl uint get buf add off 2 16 |
| 287 | b3 = shl uint get buf add off 3 24 |
| 288 | ret or or b0 b1 or b2 b3 |
| 289 | end |
| 290 | |
| 291 | fn atlas_le64 buf view uchar * off ulong to ulong |
| 292 | lo = ulong atlas_le32 buf off |
| 293 | hi = ulong atlas_le32 buf add off 4 |
| 294 | ret or lo shl hi 32 |
| 295 | end |
| 296 | |
| 297 | fn atlas_put_le32 buf edit uchar * off ulong v uint to void |
| 298 | put buf off uchar and v 0xff |
| 299 | put buf add off 1 uchar and shr v 8 0xff |
| 300 | put buf add off 2 uchar and shr v 16 0xff |
| 301 | put buf add off 3 uchar and shr v 24 0xff |
| 302 | end |
| 303 | |
| 304 | fn atlas_put_le64 buf edit uchar * off ulong v ulong to void |
| 305 | for i ulong 8 |
| 306 | put buf add off i uchar and shr v mul i 8 0xff |
| 307 | end |
| 308 | end |
| 309 | |
| 310 | note ---- Atlas Header encode/parse ---- |
| 311 | |
| 312 | fn atlas_header_encode buf edit uchar * hdr view AtlasHeader exp view uchar 32 to bool |
| 313 | if lt len buf ATLAS_HDR_SIZE |
| 314 | ret false |
| 315 | end |
| 316 | atlas_put_le32 buf 0 get hdr magic |
| 317 | atlas_put_le32 buf 4 get hdr ver |
| 318 | atlas_put_le64 buf 8 get hdr stage_len |
| 319 | atlas_put_le64 buf 16 get hdr entry_off |
| 320 | atlas_put_le64 buf 24 get hdr epoch |
| 321 | for i ulong 32 |
| 322 | put buf add 32 i get exp i |
| 323 | end |
| 324 | ret true |
| 325 | end |
| 326 | |
| 327 | fn atlas_header_parse buf view uchar * hdr edit AtlasHeader exp edit uchar 32 to uint |
| 328 | if lt len buf ATLAS_HDR_SIZE |
| 329 | ret ATLAS_ERR_SHORT_HDR |
| 330 | end |
| 331 | put hdr magic atlas_le32 buf 0 |
| 332 | put hdr ver atlas_le32 buf 4 |
| 333 | put hdr stage_len atlas_le64 buf 8 |
| 334 | put hdr entry_off atlas_le64 buf 16 |
| 335 | put hdr epoch atlas_le64 buf 24 |
| 336 | for i ulong 32 |
| 337 | put exp i get buf add 32 i |
| 338 | end |
| 339 | if ne get hdr magic ATLAS_MAGIC_LE |
| 340 | ret ATLAS_ERR_MAGIC |
| 341 | end |
| 342 | if ne get hdr ver ATLAS_HDR_VER |
| 343 | ret ATLAS_ERR_VERSION |
| 344 | end |
| 345 | ret ATLAS_OK |
| 346 | end |
| 347 | |
| 348 | note ---- Memory map & disjointness proof check (CONTRACT-QEMU-VIRT-AARCH64-M1) ---- |
| 349 | |
| 350 | fn atlas_add_overflow a ulong b ulong to bool |
| 351 | s = add a b |
| 352 | ret lt s a |
| 353 | end |
| 354 | |
| 355 | fn atlas_check_memmap stack_base ulong stack_top ulong desc_addr ulong payload_base ulong stage_len ulong to bool |
| 356 | if ge stack_base stack_top |
| 357 | ret false |
| 358 | end |
| 359 | if ne and stack_top 15 0 |
| 360 | ret false |
| 361 | end |
| 362 | if ne and desc_addr 63 0 |
| 363 | ret false |
| 364 | end |
| 365 | if ne and payload_base 63 0 |
| 366 | ret false |
| 367 | end |
| 368 | if atlas_add_overflow stack_top ATLAS_GUARD_GAP |
| 369 | ret false |
| 370 | end |
| 371 | if gt add stack_top ATLAS_GUARD_GAP desc_addr |
| 372 | ret false |
| 373 | end |
| 374 | if atlas_add_overflow desc_addr ATLAS_DESC_SIZE |
| 375 | ret false |
| 376 | end |
| 377 | if gt add desc_addr ATLAS_DESC_SIZE payload_base |
| 378 | ret false |
| 379 | end |
| 380 | if lt payload_base ATLAS_RAM_BASE |
| 381 | ret false |
| 382 | end |
| 383 | if atlas_add_overflow payload_base stage_len |
| 384 | ret false |
| 385 | end |
| 386 | if atlas_add_overflow ATLAS_RAM_BASE ATLAS_RAM_SIZE |
| 387 | ret false |
| 388 | end |
| 389 | if gt add payload_base stage_len add ATLAS_RAM_BASE ATLAS_RAM_SIZE |
| 390 | ret false |
| 391 | end |
| 392 | ret true |
| 393 | end |
| 394 | |
| 395 | note ---- Fail-closed descriptor zeroing & full stage-0 verification ---- |
| 396 | |
| 397 | fn atlas_desc_halt desc edit AtlasBootDesc code uint to void |
| 398 | put desc ram_base 0 |
| 399 | put desc ram_size 0 |
| 400 | put desc uart_base 0 |
| 401 | put desc payload_base 0 |
| 402 | put desc payload_size 0 |
| 403 | put desc entry_addr 0 |
| 404 | put desc desc_addr 0 |
| 405 | put desc stack_top 0 |
| 406 | put desc cookie 0 |
| 407 | put desc epoch 0 |
| 408 | put desc verdict code |
| 409 | put desc halted 1 |
| 410 | end |
| 411 | |
| 412 | fn atlas_validate_hdr hdr view AtlasHeader img_len ulong min_epoch ulong to uint |
| 413 | slen = get hdr stage_len |
| 414 | eoff = get hdr entry_off |
| 415 | ep = get hdr epoch |
| 416 | if eq slen 0 |
| 417 | ret ATLAS_ERR_STAGE_LEN |
| 418 | end |
| 419 | if gt slen ATLAS_MAX_STAGE |
| 420 | ret ATLAS_ERR_STAGE_LEN |
| 421 | end |
| 422 | if ne and slen 3 0 |
| 423 | ret ATLAS_ERR_ALIGN |
| 424 | end |
| 425 | if ne and eoff 3 0 |
| 426 | ret ATLAS_ERR_ALIGN |
| 427 | end |
| 428 | if ge eoff slen |
| 429 | ret ATLAS_ERR_ENTRY_OOB |
| 430 | end |
| 431 | if atlas_add_overflow ATLAS_HDR_SIZE slen |
| 432 | ret ATLAS_ERR_STAGE_LEN |
| 433 | end |
| 434 | if lt img_len add ATLAS_HDR_SIZE slen |
| 435 | ret ATLAS_ERR_TRUNCATED |
| 436 | end |
| 437 | if lt ep min_epoch |
| 438 | ret ATLAS_ERR_ROLLBACK |
| 439 | end |
| 440 | if not atlas_check_memmap ATLAS_STACK_BASE ATLAS_STACK_TOP ATLAS_DESC_ADDR ATLAS_PAYLOAD_BASE slen |
| 441 | ret ATLAS_ERR_MEMMAP |
| 442 | end |
| 443 | ret ATLAS_OK |
| 444 | end |
| 445 | |
| 446 | fn atlas_desc_fill desc edit AtlasBootDesc hdr view AtlasHeader to void |
| 447 | put desc ram_base ATLAS_RAM_BASE |
| 448 | put desc ram_size ATLAS_RAM_SIZE |
| 449 | put desc uart_base ATLAS_UART_BASE |
| 450 | put desc payload_base ATLAS_PAYLOAD_BASE |
| 451 | put desc payload_size get hdr stage_len |
| 452 | put desc entry_addr add ATLAS_PAYLOAD_BASE get hdr entry_off |
| 453 | put desc desc_addr ATLAS_DESC_ADDR |
| 454 | put desc stack_top ATLAS_STACK_TOP |
| 455 | put desc cookie ATLAS_COOKIE |
| 456 | put desc epoch get hdr epoch |
| 457 | put desc verdict ATLAS_OK |
| 458 | put desc halted 0 |
| 459 | end |
| 460 | |
| 461 | fn atlas_verify_image img view uchar * min_epoch ulong hdr edit AtlasHeader desc edit AtlasBootDesc comp edit uchar 32 to uint |
| 462 | exp = new uchar 32 0 |
| 463 | atlas_desc_halt desc ATLAS_ERR_SHORT_HDR |
| 464 | for i ulong 32 |
| 465 | put comp i 0 |
| 466 | end |
| 467 | rc = atlas_header_parse img hdr exp |
| 468 | if ne rc ATLAS_OK |
| 469 | atlas_desc_halt desc rc |
| 470 | ret rc |
| 471 | end |
| 472 | rc = atlas_validate_hdr hdr len img min_epoch |
| 473 | if ne rc ATLAS_OK |
| 474 | atlas_desc_halt desc rc |
| 475 | ret rc |
| 476 | end |
| 477 | atlas_sha256_compute part img ATLAS_HDR_SIZE get hdr stage_len comp |
| 478 | if not atlas_ct_eq32 comp exp |
| 479 | atlas_desc_halt desc ATLAS_ERR_DIGEST |
| 480 | ret ATLAS_ERR_DIGEST |
| 481 | end |
| 482 | atlas_desc_fill desc hdr |
| 483 | ret ATLAS_OK |
| 484 | end |
| 485 | |
| 486 | note ---- Machine Boot Descriptor 64-byte wire emitter (matches atlas.s offsets) ---- |
| 487 | note +0: ram_base, +8: ram_size, +16: uart_base, +24: payload_base, |
| 488 | note +32: payload_size, +40: entry_addr, +48: epoch, +56: cookie |
| 489 | |
| 490 | fn atlas_emit_desc_bytes desc view AtlasBootDesc dst edit uchar 64 to bool |
| 491 | for i ulong 64 |
| 492 | put dst i 0 |
| 493 | end |
| 494 | if ne get desc verdict ATLAS_OK |
| 495 | ret false |
| 496 | end |
| 497 | if ne get desc halted 0 |
| 498 | ret false |
| 499 | end |
| 500 | if ne get desc cookie ATLAS_COOKIE |
| 501 | ret false |
| 502 | end |
| 503 | atlas_put_le64 dst 0 get desc ram_base |
| 504 | atlas_put_le64 dst 8 get desc ram_size |
| 505 | atlas_put_le64 dst 16 get desc uart_base |
| 506 | atlas_put_le64 dst 24 get desc payload_base |
| 507 | atlas_put_le64 dst 32 get desc payload_size |
| 508 | atlas_put_le64 dst 40 get desc entry_addr |
| 509 | atlas_put_le64 dst 48 get desc epoch |
| 510 | atlas_put_le64 dst 56 get desc cookie |
| 511 | ret true |
| 512 | end |
| 513 | |
| 514 | note ---- I/O and hex formatting helpers ---- |
| 515 | |
| 516 | fn atlas_write_all fd int buf view uchar * to bool |
| 517 | n = len buf |
| 518 | off = ulong 0 |
| 519 | while lt off n bound 65536 |
| 520 | k = write fd buf off sub n off |
| 521 | if le k 0 |
| 522 | ret false |
| 523 | end |
| 524 | off = add off ulong k |
| 525 | end |
| 526 | ret true |
| 527 | end |
| 528 | |
| 529 | fn atlas_hex_nibble v uchar to uchar |
| 530 | x = and v 15 |
| 531 | if lt x 10 |
| 532 | ret add 48 x |
| 533 | end |
| 534 | ret add 87 x |
| 535 | end |
| 536 | |
| 537 | fn atlas_parse_hex_nibble c uchar to int |
| 538 | if ge c 48 |
| 539 | if le c 57 |
| 540 | ret int sub c 48 |
| 541 | end |
| 542 | end |
| 543 | if ge c 97 |
| 544 | if le c 102 |
| 545 | ret add 10 int sub c 97 |
| 546 | end |
| 547 | end |
| 548 | if ge c 65 |
| 549 | if le c 70 |
| 550 | ret add 10 int sub c 65 |
| 551 | end |
| 552 | end |
| 553 | ret -1 |
| 554 | end |
| 555 | |
| 556 | fn atlas_parse_hex_bytes hex view uchar * dst edit uchar * to bool |
| 557 | hlen = len hex |
| 558 | if ne and hlen 1 0 |
| 559 | ret false |
| 560 | end |
| 561 | need = shr hlen 1 |
| 562 | if lt len dst need |
| 563 | ret false |
| 564 | end |
| 565 | i = ulong 0 |
| 566 | while lt i need bound 65536 |
| 567 | hi = atlas_parse_hex_nibble get hex mul i 2 |
| 568 | lo = atlas_parse_hex_nibble get hex add mul i 2 1 |
| 569 | if lt hi 0 |
| 570 | ret false |
| 571 | end |
| 572 | if lt lo 0 |
| 573 | ret false |
| 574 | end |
| 575 | put dst i uchar or shl hi 4 lo |
| 576 | i = add i 1 |
| 577 | end |
| 578 | ret true |
| 579 | end |
| 580 | |
| 581 | fn atlas_write_hex64 fd int v ulong to bool |
| 582 | buf = new uchar 18 0 |
| 583 | put buf 0 uchar 48 |
| 584 | put buf 1 uchar 120 |
| 585 | for i ulong 16 |
| 586 | sh = mul sub 15 i 4 |
| 587 | nib = uchar and shr v sh 15 |
| 588 | put buf add 2 i atlas_hex_nibble nib |
| 589 | end |
| 590 | ret atlas_write_all fd buf |
| 591 | end |
| 592 | |
| 593 | fn atlas_write_digest fd int d view uchar 32 to bool |
| 594 | buf = new uchar 64 0 |
| 595 | for i ulong 32 |
| 596 | b = get d i |
| 597 | put buf mul i 2 atlas_hex_nibble shr b 4 |
| 598 | put buf add mul i 2 1 atlas_hex_nibble and b 15 |
| 599 | end |
| 600 | ret atlas_write_all fd buf |
| 601 | end |
| 602 | |
| 603 | fn atlas_write_u64 fd int v ulong to bool |
| 604 | buf = new uchar 24 0 |
| 605 | if eq v 0 |
| 606 | put buf 0 uchar 48 |
| 607 | ret atlas_write_all fd part buf 0 1 |
| 608 | end |
| 609 | cur = v |
| 610 | pos = ulong 24 |
| 611 | while gt cur 0 bound 24 |
| 612 | pos = sub pos 1 |
| 613 | put buf pos add uchar 48 uchar rem cur 10 |
| 614 | cur = div cur 10 |
| 615 | end |
| 616 | ret atlas_write_all fd part buf pos sub 24 pos |
| 617 | end |