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 / gpu / driver / gsp / bindata.om
| 1 | note gsp/bindata.om: the NVIDIA generated bindata reader in seeded Omega (OM-1). Ports the extract path |
| 2 | note of aienux PR #441 native/kernel/tools/ck_gsp_bindata_extract.c: refuse, next_line, after, |
| 3 | note parse_u32, copy_field, parse_hex_array, parse_pvt, parse_archive, parse_file, find_label, |
| 4 | note decode_desc, region_ok, check_semantics, report (stdout lines; no --out writing) and the read |
| 5 | note step of cmd_extract. Every refusal text is the C's, "line N: " prefix included. |
| 6 | note Library unit: no main. Prefix bd. Needs gsp/sha2.om, gsp/inflate.om and gsp/fw.om first |
| 7 | note (SHA-256/384, DEFLATE + CRC-32 + ISIZE, and the fwo* stdout writer). |
| 8 | note The text is read through a 1 MiB pread window (the C reads the whole file, at most 64 MiB, first; |
| 9 | note this port hashes the whole file first, so a read failure is still reported before any parse). |
| 10 | note Deviations (documented): arrays live in two 16 MiB pools, bdraw for compressed arrays as stored and |
| 11 | note bdpl for plain bytes (uncompressed arrays are parsed straight into bdpl). Running out of a pool is |
| 12 | note the C's own "out of memory for LABEL" refusal; the C would malloc up to MAX_ARRAY per array and |
| 13 | note DATA SIZE per inflated array. A directory path is refused as "File too large", the text the C |
| 14 | note prints on this host (glibc ftell on a directory). |
| 15 | const BDMAXB ulong 16 |
| 16 | const BDPOOL ulong 16777216 |
| 17 | const BDMAXA ulong 8388608 |
| 18 | const BDNONE ulong 999 |
| 19 | const BDUNS ulong 4294967295 |
| 20 | const BDWIN ulong 1048576 |
| 21 | global bdok ulong 1 |
| 22 | global bdsgb ulong 160 |
| 23 | global bdsgl ulong 160 |
| 24 | global bdpath uchar 4096 |
| 25 | global bdpathn ulong 1 |
| 26 | global bdfd int 1 |
| 27 | global bdst uchar 128 |
| 28 | global bdfsz ulong 1 |
| 29 | global bdfpos ulong 1 |
| 30 | global bdtp ulong 1 |
| 31 | global bdte ulong 1 |
| 32 | global bdtx uchar 1048576 |
| 33 | global bdln uchar 520 |
| 34 | global bdll ulong 1 |
| 35 | global bdlno ulong 1 |
| 36 | global bdrs uchar 256 |
| 37 | global bdrsn ulong 1 |
| 38 | global bdn ulong 1 |
| 39 | global bdarc ulong 1 |
| 40 | global bdcmp ulong 16 |
| 41 | global bdds ulong 16 |
| 42 | global bdcs ulong 16 |
| 43 | global bdhh ulong 16 |
| 44 | global bdhd ulong 16 |
| 45 | global bdhp ulong 16 |
| 46 | global bdnb ulong 16 |
| 47 | global bdbo ulong 16 |
| 48 | global bdpo ulong 16 |
| 49 | global bdpn ulong 16 |
| 50 | global bdfn uchar 1536 |
| 51 | global bdfnl ulong 16 |
| 52 | global bdlb uchar 1024 |
| 53 | global bdlbl ulong 16 |
| 54 | global bdfm uchar 2560 |
| 55 | global bdfml ulong 16 |
| 56 | global bdid uchar 3072 |
| 57 | global bdidl ulong 16 |
| 58 | global bdraw uchar 16777216 |
| 59 | global bdrtop ulong 1 |
| 60 | global bdpl uchar 16777216 |
| 61 | global bdptop ulong 1 |
| 62 | global bdimg uchar 64 |
| 63 | global bdimgn ulong 1 |
| 64 | global bdfsh uchar 64 |
| 65 | note segment k of BDTXT is the text between the k-th and (k+1)-th '|': 1..76 refusal pieces in C |
| 66 | note wording, 77..114 the C's line prefixes and tokens, 115.. report pieces |
| 67 | text BDTXT "|line |: |line longer than 511 characters|array size out of range for |expected '{' after the array declaration of |out of memory for |file ends inside the array of |non-hex token in the array of |byte token without a trailing comma in |more bytes than the declared size in |fewer bytes than the declared size in |expected '{' in the storage record of |storage record: bad uncompressed size for |storage record: uncompressed size differs from DATA SIZE for |storage record: bad compressed size for |storage record: compressed size differs from the header for |storage record: missing pointer line for |storage record: data pointer is not the array of |storage record: missing compressed flag for |storage record: bad compressed flag for |storage record: compressed flag disagrees with COMPRESSION for |second archive table|archive table name is not the file's function|expected '{' after the archive declaration|archive: bad entryNum|archive: entryNum differs from the number of arrays|archive: expected entries block|archive: expected the entries comment|archive: missing entry |archive: malformed entry for |archive: entry order or label differs from the arrays at |archive: entry without a storage index for |archive: storage index is not this array for |archive: expected end of entries|archive: expected end of table|array | has no data or no storage record|more than 16 arrays|FUNCTION line without a BINDATA_LABEL|FUNCTION line not terminated|bad function name|bad label|array belongs to a different function: |duplicate label |function is not a kgspGetBinArchive function: |identifier too long for |data before the first FUNCTION header|bad file name for |FILE TYPE is not BINARY for |COMPRESSION is neither YES nor NO for |COMPLEX_STRUCT is not NO for |bad DATA SIZE for |bad COMPRESSED SIZE for |second array for |array header incomplete for |COMPRESSED SIZE missing with COMPRESSION YES for |COMPRESSED SIZE given with COMPRESSION NO for |array identifier is not the expected one for |storage record before the array of |second storage record for |no arrays found|no archive table|descriptor is not 84 bytes: |descriptor version is not 5: |descriptor monitor not enabled: |descriptor without a matching image: |descriptor manifest region outside the image or unaligned: |descriptor monitor-data region outside the image or unaligned: |descriptor monitor-code region outside the image or unaligned: |hash array is not 48 bytes|hash array without UCODE_IMAGE|UCODE_HASH is not SHA-384 of UCODE_IMAGE| is not 384 bytes|unknown label | (|)|// FUNCTION: |(\"BINDATA_LABEL_|\")|kgspGetBinArchive|kgspBinArchive|_BINDATA_LABEL_|_data|// FILE NAME: |// FILE TYPE: |BINARY|// COMPRESSION: |YES|NO|// COMPLEX_STRUCT: |// DATA SIZE (bytes): |// COMPRESSED SIZE (bytes): |N/A|static BINDATA_CONST NvU8 |[] = |#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)|static const BINDATA_ARCHIVE __|static BINDATA_CONST|static const BINDATA_ARCHIVE|{|};|NV_TRUE,|NV_FALSE,| =|// entries[]|{ BINDATA_LABEL_|&g_bindata_pvt[__idx_|}|UCODE_DESC|UCODE_IMAGE|UCODE_HASH|UCODE_SIG|UCODE_PKEY|//|function | arrays=|desc | version=| manifest=0x|+0x| monitor_data=0x| monitor_code=0x| monitor_enabled=| fb_reserved=0x| signed_as_code=| app_version=| image=|/|hash384 UCODE_HASH equals sha384(UCODE_IMAGE)| file=| stored=| compressed=|yes|no| size=| sha256=|RESULT OK arrays=|REFUSED: |REFUSED: cannot read | |" |
| 68 | note build the segment table and reset the pools (call once per program) |
| 69 | fn bdsetup to void |
| 70 | if eq get bdok 0 1 |
| 71 | ret |
| 72 | end |
| 73 | put bdok 0 1 |
| 74 | fwsetup |
| 75 | infsetup |
| 76 | seg = ulong 0 |
| 77 | for i ulong len BDTXT |
| 78 | c = get BDTXT i |
| 79 | if eq c 124 |
| 80 | seg = add seg 1 |
| 81 | put bdsgb seg add i 1 |
| 82 | put bdsgl seg 0 |
| 83 | else |
| 84 | put bdsgl seg add get bdsgl seg 1 |
| 85 | end |
| 86 | end |
| 87 | put bdrtop 0 0 |
| 88 | put bdptop 0 0 |
| 89 | end |
| 90 | note ---- printing through the fw.om writer ---- |
| 91 | fn bdos k ulong to void |
| 92 | b = get bdsgb k |
| 93 | for i ulong get bdsgl k |
| 94 | fwoc get BDTXT add b i |
| 95 | end |
| 96 | end |
| 97 | fn bdolab i ulong to void |
| 98 | b = mul i 64 |
| 99 | for j ulong get bdlbl i |
| 100 | fwoc get bdlb add b j |
| 101 | end |
| 102 | end |
| 103 | fn bdofn i ulong to void |
| 104 | b = mul i 96 |
| 105 | for j ulong get bdfnl i |
| 106 | fwoc get bdfn add b j |
| 107 | end |
| 108 | end |
| 109 | fn bdofm i ulong to void |
| 110 | b = mul i 160 |
| 111 | for j ulong get bdfml i |
| 112 | fwoc get bdfm add b j |
| 113 | end |
| 114 | end |
| 115 | fn bdopath to void |
| 116 | for i ulong get bdpathn 0 |
| 117 | fwoc get bdpath i |
| 118 | end |
| 119 | end |
| 120 | note ---- the refusal reason (C f->reason, 256 bytes: at most 255 characters kept) ---- |
| 121 | fn bdrc c uchar to void |
| 122 | n = get bdrsn 0 |
| 123 | if lt n 255 |
| 124 | put bdrs n c |
| 125 | put bdrsn 0 add n 1 |
| 126 | end |
| 127 | end |
| 128 | fn bdrseg k ulong to void |
| 129 | b = get bdsgb k |
| 130 | for i ulong get bdsgl k |
| 131 | bdrc get BDTXT add b i |
| 132 | end |
| 133 | end |
| 134 | fn bdrlab i ulong to void |
| 135 | b = mul i 64 |
| 136 | for j ulong get bdlbl i |
| 137 | bdrc get bdlb add b j |
| 138 | end |
| 139 | end |
| 140 | fn bdrfn i ulong to void |
| 141 | b = mul i 96 |
| 142 | for j ulong get bdfnl i |
| 143 | bdrc get bdfn add b j |
| 144 | end |
| 145 | end |
| 146 | note "line N: " (C refuse: N is the cursor's line count, 0 when the cursor is NULL) |
| 147 | fn bdrhd ln ulong to void |
| 148 | put bdrsn 0 0 |
| 149 | bdrseg 1 |
| 150 | d = new uchar 24 0 |
| 151 | n = ulong 0 |
| 152 | x = ln |
| 153 | while lt n 24 bound 24 |
| 154 | put d n add uchar rem x 10 48 |
| 155 | n = add n 1 |
| 156 | x = div x 10 |
| 157 | if eq x 0 |
| 158 | break |
| 159 | end |
| 160 | end |
| 161 | while gt n 0 bound 24 |
| 162 | n = sub n 1 |
| 163 | bdrc get d n |
| 164 | end |
| 165 | bdrseg 2 |
| 166 | end |
| 167 | note refuse at the current line with reason k, then the label of block b unless b is BDNONE |
| 168 | fn bdrl k ulong b ulong to ulong |
| 169 | bdrhd get bdlno 0 |
| 170 | bdrseg k |
| 171 | if ne b BDNONE |
| 172 | bdrlab b |
| 173 | end |
| 174 | ret 2 |
| 175 | end |
| 176 | note refuse at the current line with reason k and the function name of block b |
| 177 | fn bdrf k ulong b ulong to ulong |
| 178 | bdrhd get bdlno 0 |
| 179 | bdrseg k |
| 180 | bdrfn b |
| 181 | ret 2 |
| 182 | end |
| 183 | note refuse with line 0 (semantic checks) with reason k, then the label of b unless BDNONE |
| 184 | fn bdr0 k ulong b ulong to ulong |
| 185 | bdrhd 0 |
| 186 | bdrseg k |
| 187 | if ne b BDNONE |
| 188 | bdrlab b |
| 189 | end |
| 190 | ret 2 |
| 191 | end |
| 192 | note "array LABEL has no data or no storage record" at the current line |
| 193 | fn bdrnd b ulong to ulong |
| 194 | bdrhd get bdlno 0 |
| 195 | bdrseg 36 |
| 196 | bdrlab b |
| 197 | bdrseg 37 |
| 198 | ret 2 |
| 199 | end |
| 200 | note ---- the text window and C next_line ---- |
| 201 | note keep at least 512 unread bytes in bdtx unless the file is used up: 0, or 3 on a read error |
| 202 | fn bdfill to ulong |
| 203 | tp = get bdtp 0 |
| 204 | av = sub get bdte 0 tp |
| 205 | if ge av 512 |
| 206 | ret 0 |
| 207 | end |
| 208 | fp = get bdfpos 0 |
| 209 | if ge fp get bdfsz 0 |
| 210 | ret 0 |
| 211 | end |
| 212 | for i ulong av |
| 213 | put bdtx i get bdtx add tp i |
| 214 | end |
| 215 | put bdtp 0 0 |
| 216 | put bdte 0 av |
| 217 | w = sub BDWIN av |
| 218 | lf = sub get bdfsz 0 fp |
| 219 | if lt lf w |
| 220 | w = lf |
| 221 | end |
| 222 | q = ulong 0 |
| 223 | while lt q w bound 999999 |
| 224 | r = pread get bdfd 0 bdtx add av q sub w q long add fp q |
| 225 | if lt r 1 |
| 226 | ret 3 |
| 227 | end |
| 228 | q = add q ulong r |
| 229 | end |
| 230 | put bdte 0 add av w |
| 231 | put bdfpos 0 add fp w |
| 232 | ret 0 |
| 233 | end |
| 234 | note C next_line into bdln[0..bdll): 1 = a line, 0 = end of text, 2 = longer than 511 characters |
| 235 | note (reason set, line count advanced), 3 = read error. A trailing CR is dropped and the line ends |
| 236 | note at its first NUL (the C's string view of its buffer). |
| 237 | fn bdnext to ulong |
| 238 | e = bdfill |
| 239 | if ne e 0 |
| 240 | ret 3 |
| 241 | end |
| 242 | tp = get bdtp 0 |
| 243 | te = get bdte 0 |
| 244 | if ge tp te |
| 245 | ret 0 |
| 246 | end |
| 247 | lim = add tp 512 |
| 248 | if gt lim te |
| 249 | lim = te |
| 250 | end |
| 251 | j = tp |
| 252 | nl = ulong 0 |
| 253 | while lt j lim bound 600 |
| 254 | if eq get bdtx j 10 |
| 255 | nl = 1 |
| 256 | break |
| 257 | end |
| 258 | j = add j 1 |
| 259 | end |
| 260 | put bdlno 0 add get bdlno 0 1 |
| 261 | n = sub j tp |
| 262 | if ge n 512 |
| 263 | bdrhd get bdlno 0 |
| 264 | bdrseg 3 |
| 265 | ret 2 |
| 266 | end |
| 267 | for i ulong n |
| 268 | put bdln i get bdtx add tp i |
| 269 | end |
| 270 | if gt n 0 |
| 271 | if eq get bdln sub n 1 13 |
| 272 | n = sub n 1 |
| 273 | end |
| 274 | end |
| 275 | k = ulong 0 |
| 276 | while lt k n bound 600 |
| 277 | if eq get bdln k 0 |
| 278 | break |
| 279 | end |
| 280 | k = add k 1 |
| 281 | end |
| 282 | put bdln k 0 |
| 283 | put bdll 0 k |
| 284 | if eq nl 1 |
| 285 | put bdtp 0 add j 1 |
| 286 | else |
| 287 | put bdtp 0 te |
| 288 | end |
| 289 | ret 1 |
| 290 | end |
| 291 | note ---- string helpers over the line ---- |
| 292 | fn bdlc i ulong to uchar |
| 293 | if ge i get bdll 0 |
| 294 | ret 0 |
| 295 | end |
| 296 | ret get bdln i |
| 297 | end |
| 298 | note does the line at p start with segment k (C after / strncmp)? |
| 299 | fn bdpre p ulong k ulong to ulong |
| 300 | b = get bdsgb k |
| 301 | l = get bdsgl k |
| 302 | if gt add p l get bdll 0 |
| 303 | ret 0 |
| 304 | end |
| 305 | for i ulong l |
| 306 | if ne get bdln add p i get BDTXT add b i |
| 307 | ret 0 |
| 308 | end |
| 309 | end |
| 310 | ret 1 |
| 311 | end |
| 312 | note is the line from p to its end exactly segment k (C strcmp)? |
| 313 | fn bdeqs p ulong k ulong to ulong |
| 314 | L = get bdll 0 |
| 315 | if gt p L |
| 316 | ret 0 |
| 317 | end |
| 318 | if ne sub L p get bdsgl k |
| 319 | ret 0 |
| 320 | end |
| 321 | ret bdpre p k |
| 322 | end |
| 323 | note C strspn(line + p, " ") |
| 324 | fn bdsp p ulong to ulong |
| 325 | q = p |
| 326 | L = get bdll 0 |
| 327 | while lt q L bound 600 |
| 328 | if ne get bdln q 32 |
| 329 | break |
| 330 | end |
| 331 | q = add q 1 |
| 332 | end |
| 333 | ret q |
| 334 | end |
| 335 | note first position at or after p where segment k occurs (C strstr), BDNONE if none |
| 336 | fn bdfind p ulong k ulong to ulong |
| 337 | q = p |
| 338 | L = get bdll 0 |
| 339 | while le q L bound 600 |
| 340 | if eq bdpre q k 1 |
| 341 | ret q |
| 342 | end |
| 343 | q = add q 1 |
| 344 | end |
| 345 | ret BDNONE |
| 346 | end |
| 347 | note first '(' at or after p (C strchr), BDNONE if none |
| 348 | fn bdfpar p ulong to ulong |
| 349 | q = p |
| 350 | L = get bdll 0 |
| 351 | while lt q L bound 600 |
| 352 | if eq get bdln q 40 |
| 353 | ret q |
| 354 | end |
| 355 | q = add q 1 |
| 356 | end |
| 357 | ret BDNONE |
| 358 | end |
| 359 | note C parse_u32 at p: decimal at most 4294967295, then spaces, commas or tabs, then the end or "//" |
| 360 | fn bdu32 p ulong to long |
| 361 | c = bdlc p |
| 362 | if lt c 48 |
| 363 | ret -1 |
| 364 | end |
| 365 | if gt c 57 |
| 366 | ret -1 |
| 367 | end |
| 368 | acc = ulong 0 |
| 369 | q = p |
| 370 | while lt q get bdll 0 bound 600 |
| 371 | c = get bdln q |
| 372 | if lt c 48 |
| 373 | break |
| 374 | end |
| 375 | if gt c 57 |
| 376 | break |
| 377 | end |
| 378 | acc = add mul acc 10 sub ulong c 48 |
| 379 | if gt acc BDUNS |
| 380 | ret -1 |
| 381 | end |
| 382 | q = add q 1 |
| 383 | end |
| 384 | while lt q get bdll 0 bound 600 |
| 385 | c = get bdln q |
| 386 | if and and ne c 32 ne c 44 ne c 9 |
| 387 | break |
| 388 | end |
| 389 | q = add q 1 |
| 390 | end |
| 391 | if lt q get bdll 0 |
| 392 | if eq bdpre q 114 0 |
| 393 | ret -1 |
| 394 | end |
| 395 | end |
| 396 | ret long acc |
| 397 | end |
| 398 | note does the line at p hold the first n characters of block i's identifier? |
| 399 | fn bdlnid p ulong i ulong n ulong to ulong |
| 400 | if gt add p n get bdll 0 |
| 401 | ret 0 |
| 402 | end |
| 403 | b = mul i 192 |
| 404 | for j ulong n |
| 405 | if ne get bdln add p j get bdid add b j |
| 406 | ret 0 |
| 407 | end |
| 408 | end |
| 409 | ret 1 |
| 410 | end |
| 411 | note does the line at p start with block i's label? |
| 412 | fn bdlnlb p ulong i ulong to ulong |
| 413 | n = get bdlbl i |
| 414 | if gt add p n get bdll 0 |
| 415 | ret 0 |
| 416 | end |
| 417 | b = mul i 64 |
| 418 | for j ulong n |
| 419 | if ne get bdln add p j get bdlb add b j |
| 420 | ret 0 |
| 421 | end |
| 422 | end |
| 423 | ret 1 |
| 424 | end |
| 425 | note does the line at p start with block i's function name? |
| 426 | fn bdlnfn p ulong i ulong to ulong |
| 427 | n = get bdfnl i |
| 428 | if gt add p n get bdll 0 |
| 429 | ret 0 |
| 430 | end |
| 431 | b = mul i 96 |
| 432 | for j ulong n |
| 433 | if ne get bdln add p j get bdfn add b j |
| 434 | ret 0 |
| 435 | end |
| 436 | end |
| 437 | ret 1 |
| 438 | end |
| 439 | note labels of blocks i and j equal? |
| 440 | fn bdlbeq i ulong j ulong to ulong |
| 441 | n = get bdlbl i |
| 442 | if ne n get bdlbl j |
| 443 | ret 0 |
| 444 | end |
| 445 | a = mul i 64 |
| 446 | b = mul j 64 |
| 447 | for k ulong n |
| 448 | if ne get bdlb add a k get bdlb add b k |
| 449 | ret 0 |
| 450 | end |
| 451 | end |
| 452 | ret 1 |
| 453 | end |
| 454 | note function names of blocks i and j equal? |
| 455 | fn bdfneq i ulong j ulong to ulong |
| 456 | n = get bdfnl i |
| 457 | if ne n get bdfnl j |
| 458 | ret 0 |
| 459 | end |
| 460 | a = mul i 96 |
| 461 | b = mul j 96 |
| 462 | for k ulong n |
| 463 | if ne get bdfn add a k get bdfn add b k |
| 464 | ret 0 |
| 465 | end |
| 466 | end |
| 467 | ret 1 |
| 468 | end |
| 469 | note label of block i starts with segment k (sw 0) or equals it (sw 1)? |
| 470 | fn bdlbsg i ulong k ulong sw ulong to ulong |
| 471 | n = get bdlbl i |
| 472 | l = get bdsgl k |
| 473 | if lt n l |
| 474 | ret 0 |
| 475 | end |
| 476 | if eq sw 1 |
| 477 | if ne n l |
| 478 | ret 0 |
| 479 | end |
| 480 | end |
| 481 | a = mul i 64 |
| 482 | b = get bdsgb k |
| 483 | for j ulong l |
| 484 | if ne get bdlb add a j get BDTXT add b j |
| 485 | ret 0 |
| 486 | end |
| 487 | end |
| 488 | ret 1 |
| 489 | end |
| 490 | note function name of block i starts with segment k? |
| 491 | fn bdfnsg i ulong k ulong to ulong |
| 492 | n = get bdfnl i |
| 493 | l = get bdsgl k |
| 494 | if lt n l |
| 495 | ret 0 |
| 496 | end |
| 497 | a = mul i 96 |
| 498 | b = get bdsgb k |
| 499 | for j ulong l |
| 500 | if ne get bdfn add a j get BDTXT add b j |
| 501 | ret 0 |
| 502 | end |
| 503 | end |
| 504 | ret 1 |
| 505 | end |
| 506 | note C find_label for the name in bdimg[0..bdimgn): block index or BDNONE |
| 507 | fn bdfimg to ulong |
| 508 | for i ulong get bdn 0 |
| 509 | n = get bdlbl i |
| 510 | if eq n get bdimgn 0 |
| 511 | a = mul i 64 |
| 512 | m = ulong 1 |
| 513 | for j ulong n |
| 514 | if ne get bdlb add a j get bdimg j |
| 515 | m = 0 |
| 516 | end |
| 517 | end |
| 518 | if eq m 1 |
| 519 | ret i |
| 520 | end |
| 521 | end |
| 522 | end |
| 523 | ret BDNONE |
| 524 | end |
| 525 | note C find_label for segment k: block index or BDNONE |
| 526 | fn bdfseg k ulong to ulong |
| 527 | for i ulong get bdn 0 |
| 528 | if eq bdlbsg i k 1 1 |
| 529 | ret i |
| 530 | end |
| 531 | end |
| 532 | ret BDNONE |
| 533 | end |
| 534 | note ---- C copy_field into the block's fields (len checked by the caller) ---- |
| 535 | fn bdsetfn i ulong p ulong n ulong to void |
| 536 | b = mul i 96 |
| 537 | for j ulong n |
| 538 | put bdfn add b j get bdln add p j |
| 539 | end |
| 540 | put bdfnl i n |
| 541 | end |
| 542 | fn bdsetlb i ulong p ulong n ulong to void |
| 543 | b = mul i 64 |
| 544 | for j ulong n |
| 545 | put bdlb add b j get bdln add p j |
| 546 | end |
| 547 | put bdlbl i n |
| 548 | end |
| 549 | fn bdsetfm i ulong p ulong n ulong to void |
| 550 | b = mul i 160 |
| 551 | for j ulong n |
| 552 | put bdfm add b j get bdln add p j |
| 553 | end |
| 554 | put bdfml i n |
| 555 | end |
| 556 | note append segment k to block i's identifier at q; returns the new end |
| 557 | fn bdidsg i ulong q ulong k ulong to ulong |
| 558 | b = mul i 192 |
| 559 | s = get bdsgb k |
| 560 | for j ulong get bdsgl k |
| 561 | put bdid add b add q j get BDTXT add s j |
| 562 | end |
| 563 | ret add q get bdsgl k |
| 564 | end |
| 565 | note identifier kgspBinArchive<stem>_BINDATA_LABEL_<label>_data (caller checked its length) |
| 566 | fn bdmkid i ulong to void |
| 567 | b = mul i 192 |
| 568 | q = bdidsg i 0 81 |
| 569 | fb = mul i 96 |
| 570 | note stem = function name after "kgspGetBinArchive" (17 characters) |
| 571 | for j ulong sub get bdfnl i 17 |
| 572 | put bdid add b q get bdfn add add fb 17 j |
| 573 | q = add q 1 |
| 574 | end |
| 575 | q = bdidsg i q 82 |
| 576 | lb = mul i 64 |
| 577 | for j ulong get bdlbl i |
| 578 | put bdid add b q get bdlb add lb j |
| 579 | q = add q 1 |
| 580 | end |
| 581 | q = bdidsg i q 83 |
| 582 | put bdidl i q |
| 583 | end |
| 584 | note ---- C parse_file: one "// FUNCTION: " line ---- |
| 585 | fn bdfunc to ulong |
| 586 | n = get bdn 0 |
| 587 | if gt n 0 |
| 588 | b = sub n 1 |
| 589 | if eq and eq get bdhd b 1 eq get bdhp b 1 false |
| 590 | ret bdrnd b |
| 591 | end |
| 592 | end |
| 593 | if ge n BDMAXB |
| 594 | ret bdrl 38 BDNONE |
| 595 | end |
| 596 | v = get bdsgl 77 |
| 597 | lp = bdfpar v |
| 598 | if eq lp BDNONE |
| 599 | ret bdrl 39 BDNONE |
| 600 | end |
| 601 | if eq bdpre lp 78 0 |
| 602 | ret bdrl 39 BDNONE |
| 603 | end |
| 604 | pe = bdfind add lp 16 79 |
| 605 | if eq pe BDNONE |
| 606 | ret bdrl 40 BDNONE |
| 607 | end |
| 608 | if ne add pe 2 get bdll 0 |
| 609 | ret bdrl 40 BDNONE |
| 610 | end |
| 611 | put bdn 0 add n 1 |
| 612 | fl = sub lp v |
| 613 | if or eq fl 0 ge fl 96 |
| 614 | ret bdrl 41 BDNONE |
| 615 | end |
| 616 | bdsetfn n v fl |
| 617 | ll = sub sub pe lp 16 |
| 618 | if or eq ll 0 ge ll 64 |
| 619 | ret bdrl 42 BDNONE |
| 620 | end |
| 621 | bdsetlb n add lp 16 ll |
| 622 | ret bdfunc2 n |
| 623 | end |
| 624 | note the rest of the FUNCTION header: same function, unique label, stem, identifier |
| 625 | fn bdfunc2 n ulong to ulong |
| 626 | if gt n 0 |
| 627 | if eq bdfneq 0 n 0 |
| 628 | ret bdrf 43 n |
| 629 | end |
| 630 | end |
| 631 | for j ulong n |
| 632 | if eq bdlbeq j n 1 |
| 633 | ret bdrl 44 n |
| 634 | end |
| 635 | end |
| 636 | put bdcs n BDUNS |
| 637 | put bdcmp n 2 |
| 638 | put bdds n 0 |
| 639 | put bdhh n 0 |
| 640 | put bdhd n 0 |
| 641 | put bdhp n 0 |
| 642 | put bdnb n 0 |
| 643 | put bdfml n 0 |
| 644 | if eq bdfnsg n 80 0 |
| 645 | ret bdrf 45 n |
| 646 | end |
| 647 | note 14 + (funclen - 17) + 15 + labellen + 5 characters must stay below 192 |
| 648 | t = add sub get bdfnl n 17 34 |
| 649 | t = add t get bdlbl n |
| 650 | if ge t 192 |
| 651 | ret bdrl 46 n |
| 652 | end |
| 653 | bdmkid n |
| 654 | ret 0 |
| 655 | end |
| 656 | note "static BINDATA_CONST NvU8 " line of block b |
| 657 | fn bdarrhd b ulong to ulong |
| 658 | if eq get bdhd b 1 |
| 659 | ret bdrl 54 b |
| 660 | end |
| 661 | c = get bdcmp b |
| 662 | m = ulong 0 |
| 663 | if eq c 2 |
| 664 | m = 1 |
| 665 | end |
| 666 | if eq get bdds b 0 |
| 667 | m = 1 |
| 668 | end |
| 669 | if eq get bdcs b BDUNS |
| 670 | m = 1 |
| 671 | end |
| 672 | if eq get bdfml b 0 |
| 673 | m = 1 |
| 674 | end |
| 675 | if eq m 1 |
| 676 | ret bdrl 55 b |
| 677 | end |
| 678 | if and eq c 1 eq get bdcs b 0 |
| 679 | ret bdrl 56 b |
| 680 | end |
| 681 | if and eq c 0 ne get bdcs b 0 |
| 682 | ret bdrl 57 b |
| 683 | end |
| 684 | v = get bdsgl 94 |
| 685 | il = get bdidl b |
| 686 | if eq bdlnid v b il 0 |
| 687 | ret bdrl 58 b |
| 688 | end |
| 689 | if eq bdeqs add v il 95 0 |
| 690 | ret bdrl 58 b |
| 691 | end |
| 692 | put bdhh b 1 |
| 693 | ret bdhex b |
| 694 | end |
| 695 | fn bdhexd c uchar to ulong |
| 696 | if and ge c 48 le c 57 |
| 697 | ret sub ulong c 48 |
| 698 | end |
| 699 | if and ge c 97 le c 102 |
| 700 | ret sub ulong c 87 |
| 701 | end |
| 702 | if and ge c 65 le c 70 |
| 703 | ret sub ulong c 55 |
| 704 | end |
| 705 | ret 16 |
| 706 | end |
| 707 | note C parse_hex_array: the "{" line, the byte lines, "};" |
| 708 | fn bdhex b ulong to ulong |
| 709 | want = get bdds b |
| 710 | if eq get bdcmp b 1 |
| 711 | want = get bdcs b |
| 712 | end |
| 713 | if or eq want 0 gt want BDMAXA |
| 714 | ret bdrl 4 b |
| 715 | end |
| 716 | r = bdnext |
| 717 | if eq r 3 |
| 718 | ret 3 |
| 719 | end |
| 720 | if ne r 1 |
| 721 | ret bdrl 5 b |
| 722 | end |
| 723 | if eq bdeqs 0 100 0 |
| 724 | ret bdrl 5 b |
| 725 | end |
| 726 | if eq get bdcmp b 1 |
| 727 | t = get bdrtop 0 |
| 728 | if gt add t want BDPOOL |
| 729 | ret bdrl 6 b |
| 730 | end |
| 731 | put bdbo b t |
| 732 | put bdrtop 0 add t want |
| 733 | else |
| 734 | t = get bdptop 0 |
| 735 | if gt add t want BDPOOL |
| 736 | ret bdrl 6 b |
| 737 | end |
| 738 | put bdbo b t |
| 739 | put bdpo b t |
| 740 | put bdpn b want |
| 741 | put bdptop 0 add t want |
| 742 | end |
| 743 | put bdnb b 0 |
| 744 | go = ulong 1 |
| 745 | while eq go 1 bound 100 |
| 746 | while eq go 1 bound 999999 |
| 747 | r = bdnext |
| 748 | if eq r 3 |
| 749 | ret 3 |
| 750 | end |
| 751 | if eq r 2 |
| 752 | ret 2 |
| 753 | end |
| 754 | if eq r 0 |
| 755 | ret bdrl 7 b |
| 756 | end |
| 757 | if eq bdeqs 0 101 1 |
| 758 | go = 0 |
| 759 | else |
| 760 | e = bdhexln b want |
| 761 | if ne e 0 |
| 762 | ret e |
| 763 | end |
| 764 | end |
| 765 | end |
| 766 | end |
| 767 | if ne get bdnb b want |
| 768 | ret bdrl 11 b |
| 769 | end |
| 770 | put bdhd b 1 |
| 771 | ret 0 |
| 772 | end |
| 773 | note one line of "0xHH," tokens of block b |
| 774 | fn bdhexln b ulong want ulong to ulong |
| 775 | p = ulong 0 |
| 776 | L = get bdll 0 |
| 777 | cm = get bdcmp b |
| 778 | base = get bdbo b |
| 779 | while lt p L bound 600 |
| 780 | while lt p L bound 600 |
| 781 | c = get bdln p |
| 782 | if and ne c 32 ne c 9 |
| 783 | break |
| 784 | end |
| 785 | p = add p 1 |
| 786 | end |
| 787 | if ge p L |
| 788 | break |
| 789 | end |
| 790 | if ne bdlc p 48 |
| 791 | ret bdrl 8 b |
| 792 | end |
| 793 | if ne bdlc add p 1 120 |
| 794 | ret bdrl 8 b |
| 795 | end |
| 796 | h = bdhexd bdlc add p 2 |
| 797 | if eq h 16 |
| 798 | ret bdrl 8 b |
| 799 | end |
| 800 | l = bdhexd bdlc add p 3 |
| 801 | if eq l 16 |
| 802 | ret bdrl 8 b |
| 803 | end |
| 804 | if ne bdlc add p 4 44 |
| 805 | ret bdrl 9 b |
| 806 | end |
| 807 | nb = get bdnb b |
| 808 | if ge nb want |
| 809 | ret bdrl 10 b |
| 810 | end |
| 811 | v = uchar add mul h 16 l |
| 812 | if eq cm 1 |
| 813 | put bdraw add base nb v |
| 814 | else |
| 815 | put bdpl add base nb v |
| 816 | end |
| 817 | put bdnb b add nb 1 |
| 818 | p = add p 5 |
| 819 | end |
| 820 | ret 0 |
| 821 | end |
| 822 | note C parse_pvt: the storage record after "#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)" |
| 823 | fn bdpvt b ulong to ulong |
| 824 | r = bdnext |
| 825 | if eq r 3 |
| 826 | ret 3 |
| 827 | end |
| 828 | if ne r 1 |
| 829 | ret bdrl 12 b |
| 830 | end |
| 831 | if eq bdeqs 0 100 0 |
| 832 | ret bdrl 12 b |
| 833 | end |
| 834 | r = bdnext |
| 835 | if eq r 3 |
| 836 | ret 3 |
| 837 | end |
| 838 | if ne r 1 |
| 839 | ret bdrl 13 b |
| 840 | end |
| 841 | v = bdu32 bdsp 0 |
| 842 | if lt v 0 |
| 843 | ret bdrl 13 b |
| 844 | end |
| 845 | if ne ulong v get bdds b |
| 846 | ret bdrl 14 b |
| 847 | end |
| 848 | r = bdnext |
| 849 | if eq r 3 |
| 850 | ret 3 |
| 851 | end |
| 852 | if ne r 1 |
| 853 | ret bdrl 15 b |
| 854 | end |
| 855 | v = bdu32 bdsp 0 |
| 856 | if lt v 0 |
| 857 | ret bdrl 15 b |
| 858 | end |
| 859 | w = get bdds b |
| 860 | if eq get bdcmp b 1 |
| 861 | w = get bdcs b |
| 862 | end |
| 863 | if ne ulong v w |
| 864 | ret bdrl 16 b |
| 865 | end |
| 866 | ret bdpvt2 b |
| 867 | end |
| 868 | note the pointer and flag lines of the storage record |
| 869 | fn bdpvt2 b ulong to ulong |
| 870 | r = bdnext |
| 871 | if eq r 3 |
| 872 | ret 3 |
| 873 | end |
| 874 | if ne r 1 |
| 875 | ret bdrl 17 b |
| 876 | end |
| 877 | p = bdsp 0 |
| 878 | il = get bdidl b |
| 879 | if eq bdlnid p b il 0 |
| 880 | ret bdrl 18 b |
| 881 | end |
| 882 | if ne bdlc add p il 44 |
| 883 | ret bdrl 18 b |
| 884 | end |
| 885 | r = bdnext |
| 886 | if eq r 3 |
| 887 | ret 3 |
| 888 | end |
| 889 | if ne r 1 |
| 890 | ret bdrl 19 b |
| 891 | end |
| 892 | p = bdsp 0 |
| 893 | fl = ulong 2 |
| 894 | if eq bdpre p 102 1 |
| 895 | fl = 1 |
| 896 | else |
| 897 | if eq bdpre p 103 1 |
| 898 | fl = 0 |
| 899 | end |
| 900 | end |
| 901 | if eq fl 2 |
| 902 | ret bdrl 20 b |
| 903 | end |
| 904 | if ne fl get bdcmp b |
| 905 | ret bdrl 21 b |
| 906 | end |
| 907 | put bdhp b 1 |
| 908 | ret 0 |
| 909 | end |
| 910 | note C parse_archive (the declaration is the current line) |
| 911 | fn bdarch to ulong |
| 912 | if eq get bdarc 0 1 |
| 913 | ret bdrl 22 BDNONE |
| 914 | end |
| 915 | p = get bdsgl 97 |
| 916 | if eq bdlnfn p 0 0 |
| 917 | ret bdrl 23 BDNONE |
| 918 | end |
| 919 | if eq bdeqs add p get bdfnl 0 104 0 |
| 920 | ret bdrl 23 BDNONE |
| 921 | end |
| 922 | r = bdnext |
| 923 | if eq r 3 |
| 924 | ret 3 |
| 925 | end |
| 926 | if ne r 1 |
| 927 | ret bdrl 24 BDNONE |
| 928 | end |
| 929 | if eq bdeqs 0 100 0 |
| 930 | ret bdrl 24 BDNONE |
| 931 | end |
| 932 | r = bdnext |
| 933 | if eq r 3 |
| 934 | ret 3 |
| 935 | end |
| 936 | if ne r 1 |
| 937 | ret bdrl 25 BDNONE |
| 938 | end |
| 939 | v = bdu32 bdsp 0 |
| 940 | if lt v 0 |
| 941 | ret bdrl 25 BDNONE |
| 942 | end |
| 943 | if ne ulong v get bdn 0 |
| 944 | ret bdrl 26 BDNONE |
| 945 | end |
| 946 | ret bdarch2 |
| 947 | end |
| 948 | note the entries block of the archive table |
| 949 | fn bdarch2 to ulong |
| 950 | r = bdnext |
| 951 | if eq r 3 |
| 952 | ret 3 |
| 953 | end |
| 954 | if ne r 1 |
| 955 | ret bdrl 27 BDNONE |
| 956 | end |
| 957 | if eq bdeqs bdsp 0 100 0 |
| 958 | ret bdrl 27 BDNONE |
| 959 | end |
| 960 | r = bdnext |
| 961 | if eq r 3 |
| 962 | ret 3 |
| 963 | end |
| 964 | if ne r 1 |
| 965 | ret bdrl 28 BDNONE |
| 966 | end |
| 967 | if eq bdpre bdsp 0 105 0 |
| 968 | ret bdrl 28 BDNONE |
| 969 | end |
| 970 | for i ulong get bdn 0 |
| 971 | e = bdarent i |
| 972 | if ne e 0 |
| 973 | ret e |
| 974 | end |
| 975 | end |
| 976 | r = bdnext |
| 977 | if eq r 3 |
| 978 | ret 3 |
| 979 | end |
| 980 | if ne r 1 |
| 981 | ret bdrl 34 BDNONE |
| 982 | end |
| 983 | if eq bdeqs bdsp 0 108 0 |
| 984 | ret bdrl 34 BDNONE |
| 985 | end |
| 986 | r = bdnext |
| 987 | if eq r 3 |
| 988 | ret 3 |
| 989 | end |
| 990 | if ne r 1 |
| 991 | ret bdrl 35 BDNONE |
| 992 | end |
| 993 | if eq bdeqs 0 101 0 |
| 994 | ret bdrl 35 BDNONE |
| 995 | end |
| 996 | put bdarc 0 1 |
| 997 | ret 0 |
| 998 | end |
| 999 | note one archive entry, for block i |
| 1000 | fn bdarent i ulong to ulong |
| 1001 | r = bdnext |
| 1002 | if eq r 3 |
| 1003 | ret 3 |
| 1004 | end |
| 1005 | if ne r 1 |
| 1006 | ret bdrl 29 i |
| 1007 | end |
| 1008 | p = bdsp 0 |
| 1009 | if eq bdpre p 106 0 |
| 1010 | ret bdrl 30 i |
| 1011 | end |
| 1012 | lab = add p get bdsgl 106 |
| 1013 | if eq bdlnlb lab i 0 |
| 1014 | ret bdrl 31 i |
| 1015 | end |
| 1016 | c = bdlc add lab get bdlbl i |
| 1017 | if and ne c 32 ne c 44 |
| 1018 | ret bdrl 31 i |
| 1019 | end |
| 1020 | x = bdfind lab 107 |
| 1021 | if eq x BDNONE |
| 1022 | ret bdrl 32 i |
| 1023 | end |
| 1024 | x = add x get bdsgl 107 |
| 1025 | il = sub get bdidl i 5 |
| 1026 | if eq bdlnid x i il 0 |
| 1027 | ret bdrl 33 i |
| 1028 | end |
| 1029 | if ne bdlc add x il 93 |
| 1030 | ret bdrl 33 i |
| 1031 | end |
| 1032 | ret 0 |
| 1033 | end |
| 1034 | note C parse_file: one line after the FUNCTION check (b = current block) |
| 1035 | fn bdline to ulong |
| 1036 | if eq bdpre 0 77 1 |
| 1037 | ret bdfunc |
| 1038 | end |
| 1039 | n = get bdn 0 |
| 1040 | if eq n 0 |
| 1041 | if or eq bdpre 0 98 1 eq bdpre 0 99 1 |
| 1042 | ret bdrl 47 BDNONE |
| 1043 | end |
| 1044 | ret 0 |
| 1045 | end |
| 1046 | b = sub n 1 |
| 1047 | if eq bdpre 0 84 1 |
| 1048 | v = get bdsgl 84 |
| 1049 | l = sub get bdll 0 v |
| 1050 | if or eq l 0 ge l 160 |
| 1051 | ret bdrl 48 b |
| 1052 | end |
| 1053 | bdsetfm b v l |
| 1054 | ret 0 |
| 1055 | end |
| 1056 | if eq bdpre 0 85 1 |
| 1057 | if eq bdeqs get bdsgl 85 86 0 |
| 1058 | ret bdrl 49 b |
| 1059 | end |
| 1060 | ret 0 |
| 1061 | end |
| 1062 | if eq bdpre 0 87 1 |
| 1063 | v = get bdsgl 87 |
| 1064 | if eq bdeqs v 88 1 |
| 1065 | put bdcmp b 1 |
| 1066 | else |
| 1067 | if eq bdeqs v 89 1 |
| 1068 | put bdcmp b 0 |
| 1069 | else |
| 1070 | ret bdrl 50 b |
| 1071 | end |
| 1072 | end |
| 1073 | ret 0 |
| 1074 | end |
| 1075 | ret bdline2 b |
| 1076 | end |
| 1077 | note the remaining header comments and the three record kinds |
| 1078 | fn bdline2 b ulong to ulong |
| 1079 | if eq bdpre 0 90 1 |
| 1080 | if eq bdeqs get bdsgl 90 89 0 |
| 1081 | ret bdrl 51 b |
| 1082 | end |
| 1083 | ret 0 |
| 1084 | end |
| 1085 | if eq bdpre 0 91 1 |
| 1086 | x = bdu32 get bdsgl 91 |
| 1087 | if lt x 0 |
| 1088 | ret bdrl 52 b |
| 1089 | end |
| 1090 | put bdds b ulong x |
| 1091 | ret 0 |
| 1092 | end |
| 1093 | if eq bdpre 0 92 1 |
| 1094 | v = get bdsgl 92 |
| 1095 | if eq bdeqs v 93 1 |
| 1096 | put bdcs b 0 |
| 1097 | else |
| 1098 | x = bdu32 v |
| 1099 | if lt x 0 |
| 1100 | ret bdrl 53 b |
| 1101 | end |
| 1102 | put bdcs b ulong x |
| 1103 | end |
| 1104 | ret 0 |
| 1105 | end |
| 1106 | if eq bdpre 0 94 1 |
| 1107 | ret bdarrhd b |
| 1108 | end |
| 1109 | if eq bdeqs 0 96 1 |
| 1110 | if eq get bdhd b 0 |
| 1111 | ret bdrl 59 b |
| 1112 | end |
| 1113 | if eq get bdhp b 1 |
| 1114 | ret bdrl 60 b |
| 1115 | end |
| 1116 | ret bdpvt b |
| 1117 | end |
| 1118 | if eq bdpre 0 97 1 |
| 1119 | if eq and eq get bdhd b 1 eq get bdhp b 1 false |
| 1120 | ret bdrnd b |
| 1121 | end |
| 1122 | ret bdarch |
| 1123 | end |
| 1124 | ret 0 |
| 1125 | end |
| 1126 | note copy bdraw[o..o+n) to infin[0..n), n <= 8 MiB, in chunks below the 1,000,000-pass limit |
| 1127 | fn bdcpin o ulong n ulong to void |
| 1128 | p = ulong 0 |
| 1129 | while lt p n bound 64 |
| 1130 | k = sub n p |
| 1131 | if gt k 524288 |
| 1132 | k = 524288 |
| 1133 | end |
| 1134 | for j ulong k |
| 1135 | put infin add p j get bdraw add add o p j |
| 1136 | end |
| 1137 | p = add p k |
| 1138 | end |
| 1139 | end |
| 1140 | note copy infout[0..n) to bdpl[o..o+n) |
| 1141 | fn bdcpout o ulong n ulong to void |
| 1142 | p = ulong 0 |
| 1143 | while lt p n bound 64 |
| 1144 | k = sub n p |
| 1145 | if gt k 524288 |
| 1146 | k = 524288 |
| 1147 | end |
| 1148 | for j ulong k |
| 1149 | put bdpl add add o p j get infout add p j |
| 1150 | end |
| 1151 | p = add p k |
| 1152 | end |
| 1153 | end |
| 1154 | note inflate block i (C unpack_gz_body) into the plain pool |
| 1155 | fn bdunpk i ulong to ulong |
| 1156 | ds = get bdds i |
| 1157 | t = get bdptop 0 |
| 1158 | if gt add t ds BDPOOL |
| 1159 | ret bdrl 6 i |
| 1160 | end |
| 1161 | nb = get bdnb i |
| 1162 | bdcpin get bdbo i nb |
| 1163 | e = infgz nb ds |
| 1164 | if ne e 0 |
| 1165 | infmsg e |
| 1166 | bdrhd 0 |
| 1167 | for j ulong get infmn 0 |
| 1168 | bdrc get infm j |
| 1169 | end |
| 1170 | bdrseg 75 |
| 1171 | bdrlab i |
| 1172 | bdrseg 76 |
| 1173 | ret 2 |
| 1174 | end |
| 1175 | bdcpout t ds |
| 1176 | put bdpo i t |
| 1177 | put bdpn i ds |
| 1178 | put bdptop 0 add t ds |
| 1179 | ret 0 |
| 1180 | end |
| 1181 | note C parse_file: the whole text, then the end checks and the inflation; 0, 2 refused, 3 read error |
| 1182 | fn bdparse to ulong |
| 1183 | put bdn 0 0 |
| 1184 | put bdarc 0 0 |
| 1185 | put bdlno 0 0 |
| 1186 | put bdtp 0 0 |
| 1187 | put bdte 0 0 |
| 1188 | put bdfpos 0 0 |
| 1189 | go = ulong 1 |
| 1190 | while eq go 1 bound 100 |
| 1191 | while eq go 1 bound 999999 |
| 1192 | r = bdnext |
| 1193 | if eq r 3 |
| 1194 | ret 3 |
| 1195 | end |
| 1196 | if eq r 2 |
| 1197 | ret 2 |
| 1198 | end |
| 1199 | if eq r 0 |
| 1200 | go = 0 |
| 1201 | else |
| 1202 | e = bdline |
| 1203 | if ne e 0 |
| 1204 | ret e |
| 1205 | end |
| 1206 | end |
| 1207 | end |
| 1208 | end |
| 1209 | if eq get bdn 0 0 |
| 1210 | ret bdrl 61 BDNONE |
| 1211 | end |
| 1212 | if eq get bdarc 0 0 |
| 1213 | ret bdrl 62 BDNONE |
| 1214 | end |
| 1215 | for i ulong get bdn 0 |
| 1216 | if eq and eq get bdhd i 1 eq get bdhp i 1 false |
| 1217 | ret bdrnd i |
| 1218 | end |
| 1219 | if eq get bdcmp i 1 |
| 1220 | e = bdunpk i |
| 1221 | if ne e 0 |
| 1222 | ret e |
| 1223 | end |
| 1224 | end |
| 1225 | end |
| 1226 | ret 0 |
| 1227 | end |
| 1228 | note ---- C check_semantics ---- |
| 1229 | note little-endian u32 word w of block i's plain bytes |
| 1230 | fn bdw32 i ulong w ulong to ulong |
| 1231 | p = add get bdpo i mul w 4 |
| 1232 | v = ulong 0 |
| 1233 | for k ulong 4 |
| 1234 | v = or v shl ulong get bdpl add p k mul k 8 |
| 1235 | end |
| 1236 | ret v |
| 1237 | end |
| 1238 | note C region_ok: size != 0, off % 256 == 0, off + size <= image |
| 1239 | fn bdreg off ulong size ulong img ulong to ulong |
| 1240 | if eq size 0 |
| 1241 | ret 0 |
| 1242 | end |
| 1243 | if ne rem off 256 0 |
| 1244 | ret 0 |
| 1245 | end |
| 1246 | if gt add off size img |
| 1247 | ret 0 |
| 1248 | end |
| 1249 | ret 1 |
| 1250 | end |
| 1251 | note a UCODE_DESC* block |
| 1252 | fn bddesc i ulong to ulong |
| 1253 | if ne get bdpn i 84 |
| 1254 | ret bdr0 63 i |
| 1255 | end |
| 1256 | if ne bdw32 i 0 5 |
| 1257 | ret bdr0 64 i |
| 1258 | end |
| 1259 | if ne bdw32 i 14 1 |
| 1260 | ret bdr0 65 i |
| 1261 | end |
| 1262 | note img = "UCODE_IMAGE" + label suffix after "UCODE_DESC", cut to 63 characters (C char img[64]) |
| 1263 | n = ulong 0 |
| 1264 | s = get bdsgb 110 |
| 1265 | for j ulong get bdsgl 110 |
| 1266 | put bdimg n get BDTXT add s j |
| 1267 | n = add n 1 |
| 1268 | end |
| 1269 | a = mul i 64 |
| 1270 | j = ulong 10 |
| 1271 | while lt j get bdlbl i bound 64 |
| 1272 | if lt n 63 |
| 1273 | put bdimg n get bdlb add a j |
| 1274 | n = add n 1 |
| 1275 | end |
| 1276 | j = add j 1 |
| 1277 | end |
| 1278 | put bdimgn 0 n |
| 1279 | ib = bdfimg |
| 1280 | if eq ib BDNONE |
| 1281 | ret bdr0 66 i |
| 1282 | end |
| 1283 | ret bddesc2 i ib |
| 1284 | end |
| 1285 | note the three regions, then the desc line |
| 1286 | fn bddesc2 i ulong ib ulong to ulong |
| 1287 | il = get bdpn ib |
| 1288 | if eq bdreg bdw32 i 8 bdw32 i 9 il 0 |
| 1289 | ret bdr0 67 i |
| 1290 | end |
| 1291 | if eq bdreg bdw32 i 10 bdw32 i 11 il 0 |
| 1292 | ret bdr0 68 i |
| 1293 | end |
| 1294 | if eq bdreg bdw32 i 12 bdw32 i 13 il 0 |
| 1295 | ret bdr0 69 i |
| 1296 | end |
| 1297 | bdos 117 |
| 1298 | bdolab i |
| 1299 | bdos 118 |
| 1300 | fwodec bdw32 i 0 |
| 1301 | bdos 119 |
| 1302 | fwohex bdw32 i 8 |
| 1303 | bdos 120 |
| 1304 | fwohex bdw32 i 9 |
| 1305 | bdos 121 |
| 1306 | fwohex bdw32 i 10 |
| 1307 | bdos 120 |
| 1308 | fwohex bdw32 i 11 |
| 1309 | bdos 122 |
| 1310 | fwohex bdw32 i 12 |
| 1311 | bdos 120 |
| 1312 | fwohex bdw32 i 13 |
| 1313 | bdos 123 |
| 1314 | fwodec bdw32 i 14 |
| 1315 | bdos 124 |
| 1316 | fwohex bdw32 i 19 |
| 1317 | bdos 125 |
| 1318 | fwodec bdw32 i 20 |
| 1319 | bdos 126 |
| 1320 | fwodec bdw32 i 7 |
| 1321 | bdos 127 |
| 1322 | for j ulong get bdimgn 0 |
| 1323 | fwoc get bdimg j |
| 1324 | end |
| 1325 | bdos 128 |
| 1326 | fwodec il |
| 1327 | fwoc 10 |
| 1328 | ret 0 |
| 1329 | end |
| 1330 | note SHA-384 of bdpl[o..o+n) into shout[0..48) |
| 1331 | fn bdsh384 o ulong n ulong to void |
| 1332 | sh5init |
| 1333 | p = ulong 0 |
| 1334 | while lt p n bound 64 |
| 1335 | k = sub n p |
| 1336 | if gt k SHWIN |
| 1337 | k = SHWIN |
| 1338 | end |
| 1339 | for j ulong k |
| 1340 | put shin j get bdpl add add o p j |
| 1341 | end |
| 1342 | sh5upd 0 k |
| 1343 | p = add p k |
| 1344 | end |
| 1345 | sh5fin |
| 1346 | end |
| 1347 | note SHA-256 (context 4) of bdpl[o..o+n) into shout[0..32) |
| 1348 | fn bdsh256 o ulong n ulong to void |
| 1349 | sh2init 4 |
| 1350 | p = ulong 0 |
| 1351 | while lt p n bound 64 |
| 1352 | k = sub n p |
| 1353 | if gt k SHWIN |
| 1354 | k = SHWIN |
| 1355 | end |
| 1356 | for j ulong k |
| 1357 | put shin j get bdpl add add o p j |
| 1358 | end |
| 1359 | sh2upd 4 0 k |
| 1360 | p = add p k |
| 1361 | end |
| 1362 | sh2fin 4 |
| 1363 | end |
| 1364 | note the UCODE_HASH block |
| 1365 | fn bdhash i ulong to ulong |
| 1366 | if ne get bdpn i 48 |
| 1367 | ret bdr0 70 BDNONE |
| 1368 | end |
| 1369 | ib = bdfseg 110 |
| 1370 | if eq ib BDNONE |
| 1371 | ret bdr0 71 BDNONE |
| 1372 | end |
| 1373 | bdsh384 get bdpo ib get bdpn ib |
| 1374 | o = get bdpo i |
| 1375 | for j ulong 48 |
| 1376 | if ne get shout j get bdpl add o j |
| 1377 | ret bdr0 72 BDNONE |
| 1378 | end |
| 1379 | end |
| 1380 | bdos 129 |
| 1381 | fwoc 10 |
| 1382 | ret 0 |
| 1383 | end |
| 1384 | fn bdsem to ulong |
| 1385 | for i ulong get bdn 0 |
| 1386 | e = ulong 0 |
| 1387 | if eq bdlbsg i 109 0 1 |
| 1388 | e = bddesc i |
| 1389 | else |
| 1390 | if eq bdlbsg i 111 1 1 |
| 1391 | e = bdhash i |
| 1392 | else |
| 1393 | if or eq bdlbsg i 112 1 1 eq bdlbsg i 113 1 1 |
| 1394 | if ne get bdpn i 384 |
| 1395 | bdrhd 0 |
| 1396 | bdrlab i |
| 1397 | bdrseg 73 |
| 1398 | e = 2 |
| 1399 | end |
| 1400 | else |
| 1401 | if eq bdlbsg i 110 0 0 |
| 1402 | e = bdr0 74 i |
| 1403 | end |
| 1404 | end |
| 1405 | end |
| 1406 | end |
| 1407 | if ne e 0 |
| 1408 | ret e |
| 1409 | end |
| 1410 | end |
| 1411 | ret 0 |
| 1412 | end |
| 1413 | note C report without --out: one array line per block |
| 1414 | fn bdreport to void |
| 1415 | for i ulong get bdn 0 |
| 1416 | bdos 36 |
| 1417 | bdofn i |
| 1418 | bdos 140 |
| 1419 | bdolab i |
| 1420 | bdos 130 |
| 1421 | bdofm i |
| 1422 | bdos 131 |
| 1423 | fwodec get bdnb i |
| 1424 | bdos 132 |
| 1425 | if eq get bdcmp i 1 |
| 1426 | bdos 133 |
| 1427 | else |
| 1428 | bdos 134 |
| 1429 | end |
| 1430 | bdos 135 |
| 1431 | fwodec get bdpn i |
| 1432 | bdos 136 |
| 1433 | bdsh256 get bdpo i get bdpn i |
| 1434 | shhex 32 |
| 1435 | fwohx 32 |
| 1436 | fwoc 10 |
| 1437 | end |
| 1438 | end |
| 1439 | note "REFUSED: cannot read PATH: <strerror e>" |
| 1440 | fn bdcantrd e ulong to ulong |
| 1441 | bdos 139 |
| 1442 | bdopath |
| 1443 | fwos 64 |
| 1444 | fwoerrno e |
| 1445 | fwoc 10 |
| 1446 | ret 2 |
| 1447 | end |
| 1448 | note C cmd_extract (no --out) for the path in bdpath[0..bdpathn). Prints the C's stdout bytes |
| 1449 | note through the fw.om writer. Returns 0 = RESULT OK, 2 = REFUSED. bdfsh gets the file's SHA-256 |
| 1450 | note hex, bdfsz its size; the blocks stay in bd* for the caller (bdpl keeps the plain bytes across |
| 1451 | note calls: the pools are only reset by bdsetup). |
| 1452 | fn bdfile to ulong |
| 1453 | bdsetup |
| 1454 | fd = fopen bdpath 0 get bdpathn 0 524288 0 |
| 1455 | if lt fd 0 |
| 1456 | ret bdcantrd ulong neg fd |
| 1457 | end |
| 1458 | put bdfd 0 int fd |
| 1459 | s = fstat int fd bdst 0 128 |
| 1460 | if lt s 0 |
| 1461 | c = fclose int fd |
| 1462 | ret bdcantrd ulong neg s |
| 1463 | end |
| 1464 | sz = ulong 0 |
| 1465 | for i ulong 8 |
| 1466 | sz = or sz shl ulong get bdst add 48 i mul i 8 |
| 1467 | end |
| 1468 | md = ulong 0 |
| 1469 | for i ulong 4 |
| 1470 | md = or md shl ulong get bdst add 16 i mul i 8 |
| 1471 | end |
| 1472 | note S_IFMT 0170000 = 61440, S_IFDIR 0040000 = 16384; 64 MiB = 67108864 (the C's read_file limit) |
| 1473 | if or eq and md 61440 16384 gt sz 67108864 |
| 1474 | c = fclose int fd |
| 1475 | ret bdcantrd 27 |
| 1476 | end |
| 1477 | put bdfsz 0 sz |
| 1478 | sh2init 3 |
| 1479 | r = sh2file 3 int fd 0 sz |
| 1480 | if ne r 0 |
| 1481 | c = fclose int fd |
| 1482 | ret bdcantrd 5 |
| 1483 | end |
| 1484 | sh2fin 3 |
| 1485 | shhex 32 |
| 1486 | for i ulong 64 |
| 1487 | put bdfsh i get shhx i |
| 1488 | end |
| 1489 | ret bdfile2 int fd |
| 1490 | end |
| 1491 | fn bdfile2 fd int to ulong |
| 1492 | e = bdparse |
| 1493 | c = fclose fd |
| 1494 | if eq e 3 |
| 1495 | ret bdcantrd 5 |
| 1496 | end |
| 1497 | if eq e 2 |
| 1498 | bdos 138 |
| 1499 | for i ulong get bdrsn 0 |
| 1500 | fwoc get bdrs i |
| 1501 | end |
| 1502 | fwoc 10 |
| 1503 | ret 2 |
| 1504 | end |
| 1505 | bdos 115 |
| 1506 | for i ulong get bdfnl 0 |
| 1507 | fwoc get bdfn i |
| 1508 | end |
| 1509 | bdos 116 |
| 1510 | fwodec get bdn 0 |
| 1511 | fwoc 10 |
| 1512 | e = bdsem |
| 1513 | if eq e 2 |
| 1514 | bdos 138 |
| 1515 | for i ulong get bdrsn 0 |
| 1516 | fwoc get bdrs i |
| 1517 | end |
| 1518 | fwoc 10 |
| 1519 | ret 2 |
| 1520 | end |
| 1521 | bdreport |
| 1522 | bdos 137 |
| 1523 | fwodec get bdn 0 |
| 1524 | fwoc 10 |
| 1525 | ret 0 |
| 1526 | end |