omega-pdf
Omega PDF: read and write PDF files in Omega
Brought by drakestapleton
| 1 | note src/writer.om: S3 page copying, independently implemented from public ISO 32000-1:2008. |
| 2 | note 7.3.3 integers are decimal; reals retain core's original lexical token, including compressed objects. |
| 3 | note 7.3.4.3 strings are hexadecimal with identical decoded bytes; 7.3.5 names escape delimiters/# as #hh. |
| 4 | note 7.3.8.1/7.3.8.2 streams retain every encoded byte, even unsupported image filters; Length is direct. |
| 5 | note External stream F is refused 67, never silently copied as an empty internal stream. |
| 6 | note 7.3.10 references are resolved by core (including generation checks/compressed objects); new object |
| 7 | note numbers are assigned by resolved node identity, all generation zero. Cyclic indirect graphs terminate |
| 8 | note through the already-assigned map. Dangling/free/wrong-generation references become null per 7.3.10. |
| 9 | note References to selected pages map to their new numbers. References to other source pages become null; |
| 10 | note source Pages/Catalog references bind to new objects 1/2 instead of traversing old page trees. Reached |
| 11 | note ObjStm/XRef structural containers are refused 67 rather than copying obsolete embedded references. |
| 12 | note 7.7.3.2/7.7.3.3/7.7.3.4: walk downward through Kids, never Parent; materialize inherited MediaBox, |
| 13 | note CropBox, Resources, Rotate on each page, replacing its Parent. Preserve other reached object values. |
| 14 | note 7.5.2 binary header, 7.5.4 classic xref (20-byte CRLF entries), 7.5.5 fresh Size/Root trailer. |
| 15 | note Pass 0 resolves and measures before creating output; pass 1 repeats and writes. O_EXCL never overwrites. |
| 16 | note pquit calls wrcleanup: only unlink if pathname still identifies the file created here. This is a |
| 17 | note best-effort identity check, not protection against an adversarial concurrent rename after lstat. |
| 18 | note Vendor ocl stays zero: a changed input causing a vendor zlib failure during pass 1 can leave partial |
| 19 | note OUT with a nonzero exit (10..32). Inputs must remain unchanged across passes. No decoding of copied |
| 20 | note stream bodies is requested; core alone may decode object streams for resolving compressed objects. |
| 21 | note Bounds: core's 262144 node pool, 4096 pages/input, 65535 output object numbers (including two roots), |
| 22 | note 4095 input files, 64 nested value levels, 64 page-tree levels, 4194304 work steps/input/pass, |
| 23 | note 10-digit total output length/offset, stream length at most 2^40 bytes. Each bound refuses 63. |
| 24 | note Source names/strings/numeric lexemes must fit the 16MiB core byte pool. No f64 reserialization. |
| 25 | note Core representation is src/core.om; the compiler and vendor pins are in PIN. This unit owns only writer.om. |
| 26 | |
| 27 | const WRNODES ulong 262144 |
| 28 | const WRNUL ulong 4294967295 |
| 29 | const WRMAXO ulong 65535 |
| 30 | const WRWORK ulong 4194304 |
| 31 | const WROFFX ulong 9999999999 |
| 32 | const WRSLEN ulong 1099511627776 |
| 33 | |
| 34 | global wrnew ulong 262144 |
| 35 | global wrpg uchar 262144 |
| 36 | global wrvis uchar 262144 |
| 37 | global wrimb ulong 262144 |
| 38 | global wricb ulong 262144 |
| 39 | global wrires ulong 262144 |
| 40 | global wrirot ulong 262144 |
| 41 | note Inherited MediaBox/CropBox/Resources/Rotate node IDs, four per tree level. |
| 42 | global wrstk ulong 256 |
| 43 | note Queue node and (page index + 1; zero means ordinary copied object). |
| 44 | global wrq ulong 65536 |
| 45 | global wrqp ulong 65536 |
| 46 | global wrqh ulong 1 |
| 47 | global wrqt ulong 1 |
| 48 | global wroff ulong 65536 |
| 49 | global wrkids ulong 65536 |
| 50 | global wrnkid ulong 1 |
| 51 | global wrnobj ulong 1 |
| 52 | global wrob uchar 65536 |
| 53 | global wrobn ulong 1 |
| 54 | global wrcb uchar 65536 |
| 55 | global wrpos ulong 1 |
| 56 | global wrcol ulong 1 |
| 57 | global wrfd int 1 |
| 58 | global wrpass ulong 1 |
| 59 | global wrfsz ulong 1 |
| 60 | global wrwk ulong 1 |
| 61 | global wractive ulong 1 |
| 62 | global wrident uchar 16 |
| 63 | |
| 64 | text wkpar "Parent" |
| 65 | text wkmb "MediaBox" |
| 66 | text wkcb "CropBox" |
| 67 | text wkres "Resources" |
| 68 | text wkrot "Rotate" |
| 69 | text wkkids "Kids" |
| 70 | text wkpages "Pages" |
| 71 | text wkpage "Page" |
| 72 | text wkcat "Catalog" |
| 73 | text wkobjs "ObjStm" |
| 74 | text wkxref "XRef" |
| 75 | text wktype "Type" |
| 76 | text wklen "Length" |
| 77 | text wkf "F" |
| 78 | text wthead "%PDF-1.7\n%" |
| 79 | text wtobj " 0 obj\n" |
| 80 | text wtend "\nendobj\n" |
| 81 | text wtref " 0 R" |
| 82 | text wtnull "null" |
| 83 | text wttrue "true" |
| 84 | text wtfalse "false" |
| 85 | text wtdopen "<<" |
| 86 | text wtdclose "\n>>" |
| 87 | text wtlen "\n/Length " |
| 88 | text wtstrm "\n>>\nstream\n" |
| 89 | text wtends "\nendstream" |
| 90 | text wtmb "\n/MediaBox " |
| 91 | text wtcb "\n/CropBox " |
| 92 | text wtres "\n/Resources " |
| 93 | text wtrot "\n/Rotate " |
| 94 | text wtempty "<< >>" |
| 95 | text wtpar "\n/Parent 1 0 R\n>>" |
| 96 | text wtpages "1 0 obj\n<< /Type /Pages /Count " |
| 97 | text wtkids "\n/Kids [" |
| 98 | text wtkend "]\n>>\nendobj\n" |
| 99 | text wtcat "2 0 obj\n<< /Type /Catalog /Pages 1 0 R >>\nendobj\n" |
| 100 | text wtxref "xref\n0 " |
| 101 | text wtfree "0000000000 65535 f" |
| 102 | text wtinuse " 00000 n" |
| 103 | text wttrail "trailer\n<< /Size " |
| 104 | text wtroot " /Root 2 0 R >>\nstartxref\n" |
| 105 | text wteof "\n%%EOF\n" |
| 106 | text wmusage "split or merge: page range or input list is not valid" |
| 107 | text wmpath "output path is empty or longer than 4095 bytes" |
| 108 | text wmexist "cannot create the output file (it must not exist already)" |
| 109 | text wmnode "node id outside the core pool" |
| 110 | text wmwork "copy graph cycle or work above 4194304 steps for one input" |
| 111 | text wmobjs "more than 65535 output object numbers" |
| 112 | text wmpgs "more than 65535 output pages" |
| 113 | text wminput "more than 4095 input files" |
| 114 | text wmdeep "object nesting deeper than 64" |
| 115 | text wmtree "page tree cycle or depth above 64 levels" |
| 116 | text wmoff "output length above the 10-digit cross-reference limit" |
| 117 | text wmpgtw "a page object appears twice in the page tree" |
| 118 | text wmnopg "no pages to write" |
| 119 | text wmmiss "page dictionary or effective MediaBox missing" |
| 120 | text wmkey "dictionary key is not a name" |
| 121 | text wmodd "dictionary key without a value" |
| 122 | text wmtype "unknown object type" |
| 123 | text wmdstr "stream as a direct object" |
| 124 | text wmext "stream data in an external file (F) is not copied" |
| 125 | text wmnumb "real numeric token missing from core byte pool" |
| 126 | text wmcont "reference to source ObjStm or XRef container is not copyable" |
| 127 | text wmslen "stream longer than 2^40 bytes" |
| 128 | text wmpast "stream data past the end of the input file" |
| 129 | text wmread "read error while copying stream data" |
| 130 | text wmwrite "write error on the output file" |
| 131 | text wmstat "cannot stat the input file" |
| 132 | text wmpgcnt "input page count above 4096" |
| 133 | text wmbytes "object byte span outside the core byte pool" |
| 134 | |
| 135 | note Output buffered writes; each successful iteration advances at least one byte. |
| 136 | fn wrflush to void |
| 137 | n = get wrobn 0 |
| 138 | s = ulong 0 |
| 139 | while lt s n bound 65537 |
| 140 | r = write get wrfd 0 wrob s sub n s |
| 141 | if le r 0 |
| 142 | pquit 68 wmwrite |
| 143 | ret |
| 144 | end |
| 145 | s = add s ulong r |
| 146 | end |
| 147 | put wrobn 0 0 |
| 148 | end |
| 149 | |
| 150 | fn wrcbout n ulong to void |
| 151 | s = ulong 0 |
| 152 | while lt s n bound 65537 |
| 153 | r = write get wrfd 0 wrcb s sub n s |
| 154 | if le r 0 |
| 155 | pquit 68 wmwrite |
| 156 | ret |
| 157 | end |
| 158 | s = add s ulong r |
| 159 | end |
| 160 | end |
| 161 | |
| 162 | fn wrbyte b ulong to void |
| 163 | if ge get wrpos 0 WROFFX |
| 164 | pquit 63 wmoff |
| 165 | ret |
| 166 | end |
| 167 | put wrpos 0 add get wrpos 0 1 |
| 168 | if eq b 10 |
| 169 | put wrcol 0 0 |
| 170 | else |
| 171 | put wrcol 0 add get wrcol 0 1 |
| 172 | end |
| 173 | if eq get wrpass 0 1 |
| 174 | n = get wrobn 0 |
| 175 | put wrob n uchar b |
| 176 | put wrobn 0 add n 1 |
| 177 | if eq add n 1 65536 |
| 178 | wrflush |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | |
| 183 | fn wrstr s view uchar * to void |
| 184 | for i ulong len s bound 65536 |
| 185 | wrbyte ulong get s i |
| 186 | end |
| 187 | end |
| 188 | |
| 189 | fn wrsep to void |
| 190 | if gt get wrcol 0 200 |
| 191 | wrbyte 10 |
| 192 | else |
| 193 | wrbyte 32 |
| 194 | end |
| 195 | end |
| 196 | |
| 197 | fn wrdec v ulong to void |
| 198 | b = new uchar 24 0 |
| 199 | x = v |
| 200 | k = ulong 0 |
| 201 | while true bound 21 |
| 202 | put b k uchar add rem x 10 48 |
| 203 | k = add k 1 |
| 204 | x = div x 10 |
| 205 | if eq x 0 |
| 206 | break |
| 207 | end |
| 208 | end |
| 209 | while gt k 0 bound 21 |
| 210 | k = sub k 1 |
| 211 | wrbyte ulong get b k |
| 212 | end |
| 213 | end |
| 214 | |
| 215 | fn wrint v long to void |
| 216 | if lt v 0 |
| 217 | wrbyte 45 |
| 218 | w = add v 1 |
| 219 | u = ulong neg w |
| 220 | wrdec add u 1 |
| 221 | else |
| 222 | wrdec ulong v |
| 223 | end |
| 224 | end |
| 225 | |
| 226 | fn wrpad10 v ulong to void |
| 227 | b = new uchar 10 0 |
| 228 | x = v |
| 229 | for i ulong 10 |
| 230 | put b sub 9 i uchar add rem x 10 48 |
| 231 | x = div x 10 |
| 232 | end |
| 233 | for j ulong 10 |
| 234 | wrbyte ulong get b j |
| 235 | end |
| 236 | end |
| 237 | |
| 238 | fn wrhex2 c ulong to void |
| 239 | h = shr c 4 |
| 240 | if lt h 10 |
| 241 | wrbyte add h 48 |
| 242 | else |
| 243 | wrbyte add h 55 |
| 244 | end |
| 245 | l = and c 15 |
| 246 | if lt l 10 |
| 247 | wrbyte add l 48 |
| 248 | else |
| 249 | wrbyte add l 55 |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | fn wrreg c ulong to bool |
| 254 | if or lt c 33 gt c 126 |
| 255 | ret false |
| 256 | end |
| 257 | if or or eq c 35 eq c 37 or eq c 40 eq c 41 |
| 258 | ret false |
| 259 | end |
| 260 | if or or eq c 47 eq c 60 or eq c 62 eq c 91 |
| 261 | ret false |
| 262 | end |
| 263 | if or or eq c 93 eq c 123 eq c 125 |
| 264 | ret false |
| 265 | end |
| 266 | ret true |
| 267 | end |
| 268 | |
| 269 | note Guard every pbytes span before reading it, using subtraction to avoid wraparound. |
| 270 | fn wrspan n ulong to void |
| 271 | s = get pstart n |
| 272 | k = get plen n |
| 273 | if gt s 16777216 |
| 274 | pquit 63 wmbytes |
| 275 | ret |
| 276 | end |
| 277 | if gt k sub ulong 16777216 s |
| 278 | pquit 63 wmbytes |
| 279 | end |
| 280 | end |
| 281 | |
| 282 | fn wrname n ulong to void |
| 283 | wrspan n |
| 284 | s = get pstart n |
| 285 | k = get plen n |
| 286 | wrbyte 47 |
| 287 | for i ulong k bound 16777217 |
| 288 | c = ulong get pbytes add s i |
| 289 | if wrreg c |
| 290 | wrbyte c |
| 291 | else |
| 292 | wrbyte 35 |
| 293 | wrhex2 c |
| 294 | end |
| 295 | end |
| 296 | end |
| 297 | |
| 298 | fn wrhexs n ulong to void |
| 299 | wrspan n |
| 300 | s = get pstart n |
| 301 | k = get plen n |
| 302 | wrbyte 60 |
| 303 | for i ulong k bound 16777217 |
| 304 | if gt get wrcol 0 200 |
| 305 | wrbyte 10 |
| 306 | end |
| 307 | wrhex2 ulong get pbytes add s i |
| 308 | end |
| 309 | wrbyte 62 |
| 310 | end |
| 311 | |
| 312 | note Type4 lexical bytes retained by core rather than round-tripping through preal/f64. |
| 313 | fn wrnumeric n ulong to void |
| 314 | wrspan n |
| 315 | s = get pstart n |
| 316 | k = get plen n |
| 317 | if eq k 0 |
| 318 | pquit 67 wmnumb |
| 319 | ret |
| 320 | end |
| 321 | for i ulong k bound 16777217 |
| 322 | wrbyte ulong get pbytes add s i |
| 323 | end |
| 324 | end |
| 325 | |
| 326 | fn wrtick to void |
| 327 | w = add get wrwk 0 1 |
| 328 | put wrwk 0 w |
| 329 | if gt w WRWORK |
| 330 | pquit 63 wmwork |
| 331 | end |
| 332 | end |
| 333 | |
| 334 | fn wrnode n ulong to void |
| 335 | if ge n WRNODES |
| 336 | pquit 63 wmnode |
| 337 | end |
| 338 | end |
| 339 | |
| 340 | fn wralloc to ulong |
| 341 | n = get wrnobj 0 |
| 342 | if gt n WRMAXO |
| 343 | pquit 63 wmobjs |
| 344 | ret 0 |
| 345 | end |
| 346 | put wrnobj 0 add n 1 |
| 347 | ret n |
| 348 | end |
| 349 | |
| 350 | fn wrpush d ulong ix ulong to void |
| 351 | h = get wrqt 0 |
| 352 | if ge h 65536 |
| 353 | pquit 63 wmobjs |
| 354 | ret |
| 355 | end |
| 356 | put wrq h d |
| 357 | put wrqp h ix |
| 358 | put wrqt 0 add h 1 |
| 359 | end |
| 360 | |
| 361 | fn wrkid num ulong to void |
| 362 | k = get wrnkid 0 |
| 363 | if ge k WRMAXO |
| 364 | pquit 63 wmpgs |
| 365 | ret |
| 366 | end |
| 367 | put wrkids k num |
| 368 | put wrnkid 0 add k 1 |
| 369 | end |
| 370 | |
| 371 | fn wrmark num ulong to void |
| 372 | put wroff num get wrpos 0 |
| 373 | end |
| 374 | |
| 375 | note Return structural replacement number, WRNUL if source page absent, or zero for ordinary objects. |
| 376 | note A source ObjStm/XRef container is never copied: its embedded references cannot be renumbered raw. |
| 377 | fn wrstruct u ulong to ulong |
| 378 | if eq get wrpg u 1 |
| 379 | ret WRNUL |
| 380 | end |
| 381 | k = get ptype u |
| 382 | if or eq k 8 eq k 10 |
| 383 | y = pget u wktype |
| 384 | if pne y wkpage |
| 385 | ret WRNUL |
| 386 | end |
| 387 | if pne y wkpages |
| 388 | ret 1 |
| 389 | end |
| 390 | if pne y wkcat |
| 391 | ret 2 |
| 392 | end |
| 393 | a = pne y wkobjs |
| 394 | b = pne y wkxref |
| 395 | if or a b |
| 396 | pquit 67 wmcont |
| 397 | ret 0 |
| 398 | end |
| 399 | end |
| 400 | ret 0 |
| 401 | end |
| 402 | |
| 403 | fn wrmap u ulong to ulong |
| 404 | if eq u 0 |
| 405 | ret 0 |
| 406 | end |
| 407 | wrnode u |
| 408 | x = get wrnew u |
| 409 | if eq x WRNUL |
| 410 | ret 0 |
| 411 | end |
| 412 | if ne x 0 |
| 413 | ret x |
| 414 | end |
| 415 | x = wrstruct u |
| 416 | if ne x 0 |
| 417 | put wrnew u x |
| 418 | if eq x WRNUL |
| 419 | ret 0 |
| 420 | end |
| 421 | ret x |
| 422 | end |
| 423 | x = wralloc |
| 424 | put wrnew u x |
| 425 | wrpush u 0 |
| 426 | ret x |
| 427 | end |
| 428 | |
| 429 | fn wrref v ulong to void |
| 430 | u = pderef v |
| 431 | x = wrmap u |
| 432 | if eq x 0 |
| 433 | wrstr wtnull |
| 434 | ret |
| 435 | end |
| 436 | wrdec x |
| 437 | wrstr wtref |
| 438 | end |
| 439 | |
| 440 | note One direct value; indirect refs queue objects instead of recursively following them. |
| 441 | fn wrval v ulong dep ulong to void |
| 442 | wrtick |
| 443 | if gt dep 64 |
| 444 | pquit 63 wmdeep |
| 445 | ret |
| 446 | end |
| 447 | if eq v 0 |
| 448 | wrstr wtnull |
| 449 | ret |
| 450 | end |
| 451 | wrnode v |
| 452 | k = get ptype v |
| 453 | if eq k 1 |
| 454 | wrstr wtnull |
| 455 | ret |
| 456 | end |
| 457 | if eq k 2 |
| 458 | if ne get pnum v 0 |
| 459 | wrstr wttrue |
| 460 | else |
| 461 | wrstr wtfalse |
| 462 | end |
| 463 | ret |
| 464 | end |
| 465 | if eq k 3 |
| 466 | wrint get pnum v |
| 467 | ret |
| 468 | end |
| 469 | if eq k 4 |
| 470 | wrnumeric v |
| 471 | ret |
| 472 | end |
| 473 | if eq k 5 |
| 474 | wrname v |
| 475 | ret |
| 476 | end |
| 477 | if eq k 6 |
| 478 | wrhexs v |
| 479 | ret |
| 480 | end |
| 481 | if eq k 7 |
| 482 | wrbyte 91 |
| 483 | c = get pfirst v |
| 484 | j = ulong 0 |
| 485 | while ne c 0 bound 4194305 |
| 486 | if gt j 0 |
| 487 | wrsep |
| 488 | end |
| 489 | wrval c add dep 1 |
| 490 | j = add j 1 |
| 491 | c = get pnext c |
| 492 | end |
| 493 | wrbyte 93 |
| 494 | ret |
| 495 | end |
| 496 | if eq k 8 |
| 497 | x = wrstruct v |
| 498 | if ne x 0 |
| 499 | if eq x WRNUL |
| 500 | wrstr wtnull |
| 501 | else |
| 502 | wrdec x |
| 503 | wrstr wtref |
| 504 | end |
| 505 | ret |
| 506 | end |
| 507 | wrstr wtdopen |
| 508 | wrpairs v add dep 1 0 |
| 509 | wrstr wtdclose |
| 510 | ret |
| 511 | end |
| 512 | if eq k 9 |
| 513 | wrref v |
| 514 | ret |
| 515 | end |
| 516 | if eq k 10 |
| 517 | pquit 67 wmdstr |
| 518 | ret |
| 519 | end |
| 520 | pquit 60 wmtype |
| 521 | end |
| 522 | |
| 523 | note Dictionary skip mode 1: stream Length. Mode 2: page Parent and four inheritable attributes. |
| 524 | fn wrskip c ulong mode ulong to bool |
| 525 | if eq mode 1 |
| 526 | ret pne c wklen |
| 527 | end |
| 528 | if eq mode 2 |
| 529 | a = pne c wkpar |
| 530 | b = pne c wkmb |
| 531 | if or a b |
| 532 | ret true |
| 533 | end |
| 534 | a = pne c wkcb |
| 535 | b = pne c wkres |
| 536 | if or a b |
| 537 | ret true |
| 538 | end |
| 539 | ret pne c wkrot |
| 540 | end |
| 541 | ret false |
| 542 | end |
| 543 | |
| 544 | fn wrpairs d ulong dep ulong mode ulong to void |
| 545 | c = get pfirst d |
| 546 | while ne c 0 bound 4194305 |
| 547 | wrtick |
| 548 | wrnode c |
| 549 | v = get pnext c |
| 550 | if eq v 0 |
| 551 | pquit 60 wmodd |
| 552 | ret |
| 553 | end |
| 554 | wrnode v |
| 555 | if ne get ptype c 5 |
| 556 | pquit 60 wmkey |
| 557 | ret |
| 558 | end |
| 559 | s = wrskip c mode |
| 560 | if not s |
| 561 | wrbyte 10 |
| 562 | wrname c |
| 563 | wrbyte 32 |
| 564 | wrval v dep |
| 565 | end |
| 566 | c = get pnext v |
| 567 | end |
| 568 | end |
| 569 | |
| 570 | fn wrobj n ulong to void |
| 571 | num = get wrnew n |
| 572 | wrmark num |
| 573 | wrdec num |
| 574 | wrstr wtobj |
| 575 | if eq get ptype n 10 |
| 576 | wrstrm n |
| 577 | else |
| 578 | wrval n 1 |
| 579 | end |
| 580 | wrstr wtend |
| 581 | end |
| 582 | |
| 583 | fn wrstrm n ulong to void |
| 584 | f = pget n wkf |
| 585 | if ne f 0 |
| 586 | pquit 67 wmext |
| 587 | ret |
| 588 | end |
| 589 | wrstr wtdopen |
| 590 | wrpairs n 2 1 |
| 591 | wrstr wtlen |
| 592 | wrdec get pslen n |
| 593 | wrstr wtstrm |
| 594 | wrraw n |
| 595 | wrstr wtends |
| 596 | end |
| 597 | |
| 598 | note All copied streams remain raw. Pass 0 checks extent only; pass 1 copies with pread/short writes. |
| 599 | fn wrraw n ulong to void |
| 600 | o = get psoff n |
| 601 | l = get pslen n |
| 602 | if gt l WRSLEN |
| 603 | pquit 63 wmslen |
| 604 | ret |
| 605 | end |
| 606 | if gt o get wrfsz 0 |
| 607 | pquit 60 wmpast |
| 608 | ret |
| 609 | end |
| 610 | if gt l sub get wrfsz 0 o |
| 611 | pquit 60 wmpast |
| 612 | ret |
| 613 | end |
| 614 | if gt l sub WROFFX get wrpos 0 |
| 615 | pquit 63 wmoff |
| 616 | ret |
| 617 | end |
| 618 | if eq get wrpass 0 1 |
| 619 | wrflush |
| 620 | s = ulong 0 |
| 621 | while lt s l bound 10000000000 |
| 622 | k = sub l s |
| 623 | if gt k 65536 |
| 624 | k = ulong 65536 |
| 625 | end |
| 626 | r = pread get pfd 0 wrcb 0 k long add o s |
| 627 | if le r 0 |
| 628 | pquit 64 wmread |
| 629 | ret |
| 630 | end |
| 631 | wrcbout ulong r |
| 632 | s = add s ulong r |
| 633 | end |
| 634 | end |
| 635 | put wrpos 0 add get wrpos 0 l |
| 636 | put wrcol 0 1 |
| 637 | end |
| 638 | |
| 639 | note Emit the effective inherited attribute node, so indirect resource/box refs also get renumbered. |
| 640 | fn wrpage n ulong to void |
| 641 | num = get wrnew n |
| 642 | wrmark num |
| 643 | wrdec num |
| 644 | wrstr wtobj |
| 645 | wrstr wtdopen |
| 646 | wrpairs n 2 2 |
| 647 | v = get wrimb n |
| 648 | if eq v 0 |
| 649 | pquit 60 wmmiss |
| 650 | ret |
| 651 | end |
| 652 | wrstr wtmb |
| 653 | wrval v 2 |
| 654 | v = get wricb n |
| 655 | if ne v 0 |
| 656 | wrstr wtcb |
| 657 | wrval v 2 |
| 658 | end |
| 659 | v = get wrires n |
| 660 | wrstr wtres |
| 661 | if ne v 0 |
| 662 | wrval v 2 |
| 663 | else |
| 664 | wrstr wtempty |
| 665 | end |
| 666 | v = get wrirot n |
| 667 | if ne v 0 |
| 668 | wrstr wtrot |
| 669 | wrval v 2 |
| 670 | end |
| 671 | wrstr wtpar |
| 672 | wrstr wtend |
| 673 | end |
| 674 | |
| 675 | note Downward inheritance walk; no Parent read at all. wrvis refuses a duplicate or cycle explicitly. |
| 676 | fn wrwalk n ulong dep ulong to void |
| 677 | wrtick |
| 678 | if gt dep 63 |
| 679 | pquit 63 wmtree |
| 680 | ret |
| 681 | end |
| 682 | d = pderef n |
| 683 | if eq d 0 |
| 684 | ret |
| 685 | end |
| 686 | wrnode d |
| 687 | if eq get wrvis d 1 |
| 688 | if eq get wrpg d 1 |
| 689 | pquit 67 wmpgtw |
| 690 | else |
| 691 | pquit 63 wmtree |
| 692 | end |
| 693 | ret |
| 694 | end |
| 695 | put wrvis d 1 |
| 696 | if ne get ptype d 8 |
| 697 | ret |
| 698 | end |
| 699 | b = mul dep 4 |
| 700 | v = ulong 0 |
| 701 | for k ulong 4 |
| 702 | v = ulong 0 |
| 703 | if gt dep 0 |
| 704 | v = get wrstk sub add b k 4 |
| 705 | end |
| 706 | put wrstk add b k v |
| 707 | end |
| 708 | v = pget d wkmb |
| 709 | if ne v 0 |
| 710 | put wrstk b v |
| 711 | end |
| 712 | v = pget d wkcb |
| 713 | if ne v 0 |
| 714 | put wrstk add b 1 v |
| 715 | end |
| 716 | v = pget d wkres |
| 717 | if ne v 0 |
| 718 | put wrstk add b 2 v |
| 719 | end |
| 720 | v = pget d wkrot |
| 721 | if ne v 0 |
| 722 | put wrstk add b 3 v |
| 723 | end |
| 724 | if eq get wrpg d 1 |
| 725 | put wrimb d get wrstk b |
| 726 | put wricb d get wrstk add b 1 |
| 727 | put wrires d get wrstk add b 2 |
| 728 | put wrirot d get wrstk add b 3 |
| 729 | ret |
| 730 | end |
| 731 | v = pget d wkkids |
| 732 | c = pderef v |
| 733 | if eq c 0 |
| 734 | ret |
| 735 | end |
| 736 | if ne get ptype c 7 |
| 737 | ret |
| 738 | end |
| 739 | e = get pfirst c |
| 740 | while ne e 0 bound 4194305 |
| 741 | wrwalk e add dep 1 |
| 742 | e = get pnext e |
| 743 | end |
| 744 | end |
| 745 | |
| 746 | fn wrreset to void |
| 747 | for i ulong WRNODES bound 262144 |
| 748 | put wrnew i 0 |
| 749 | put wrpg i 0 |
| 750 | put wrvis i 0 |
| 751 | put wrimb i 0 |
| 752 | put wricb i 0 |
| 753 | put wrires i 0 |
| 754 | put wrirot i 0 |
| 755 | end |
| 756 | put wrqh 0 0 |
| 757 | put wrqt 0 0 |
| 758 | put wrwk 0 0 |
| 759 | end |
| 760 | |
| 761 | fn wrfile first ulong last ulong to void |
| 762 | wrreset |
| 763 | n = get pcount 0 |
| 764 | if gt n 4096 |
| 765 | pquit 63 wmpgcnt |
| 766 | ret |
| 767 | end |
| 768 | if eq n 0 |
| 769 | ret |
| 770 | end |
| 771 | if or or eq first 0 gt first last gt last n |
| 772 | pquit 1 wmusage |
| 773 | ret |
| 774 | end |
| 775 | for i ulong n bound 4096 |
| 776 | d = get ppages i |
| 777 | wrnode d |
| 778 | if eq d 0 |
| 779 | pquit 60 wmmiss |
| 780 | ret |
| 781 | end |
| 782 | put wrpg d 1 |
| 783 | end |
| 784 | r = get proot 0 |
| 785 | k = pget r wkpages |
| 786 | wrwalk k 0 |
| 787 | for j ulong add sub last first 1 bound 4096 |
| 788 | i = add sub first 1 j |
| 789 | d = get ppages i |
| 790 | if ne get wrnew d 0 |
| 791 | pquit 67 wmpgtw |
| 792 | ret |
| 793 | end |
| 794 | x = wralloc |
| 795 | put wrnew d x |
| 796 | wrpush d add i 1 |
| 797 | wrkid x |
| 798 | end |
| 799 | while lt get wrqh 0 get wrqt 0 bound 65537 |
| 800 | h = get wrqh 0 |
| 801 | d = get wrq h |
| 802 | ix = get wrqp h |
| 803 | put wrqh 0 add h 1 |
| 804 | if gt ix 0 |
| 805 | wrpage d |
| 806 | else |
| 807 | wrobj d |
| 808 | end |
| 809 | end |
| 810 | end |
| 811 | |
| 812 | fn wrstart to void |
| 813 | put wrpos 0 0 |
| 814 | put wrobn 0 0 |
| 815 | put wrcol 0 0 |
| 816 | put wrnobj 0 3 |
| 817 | put wrnkid 0 0 |
| 818 | wrstr wthead |
| 819 | wrbyte 226 |
| 820 | wrbyte 227 |
| 821 | wrbyte 207 |
| 822 | wrbyte 211 |
| 823 | wrbyte 10 |
| 824 | end |
| 825 | |
| 826 | note Linux AArch64 stat st_size at byte 48, little endian. Reject impossible signed-file offsets. |
| 827 | fn wrfsize to void |
| 828 | sb = new uchar 128 0 |
| 829 | r = fstat get pfd 0 sb 0 128 |
| 830 | if lt r 0 |
| 831 | pquit 64 wmstat |
| 832 | ret |
| 833 | end |
| 834 | v = ulong 0 |
| 835 | for k ulong 8 |
| 836 | v = or shl v 8 ulong get sb sub 55 k |
| 837 | end |
| 838 | if gt v 9223372036854775807 |
| 839 | pquit 63 wmoff |
| 840 | ret |
| 841 | end |
| 842 | put wrfsz 0 v |
| 843 | end |
| 844 | |
| 845 | fn wrfinish to void |
| 846 | k = get wrnkid 0 |
| 847 | if eq k 0 |
| 848 | pquit 67 wmnopg |
| 849 | ret |
| 850 | end |
| 851 | wrmark 1 |
| 852 | wrstr wtpages |
| 853 | wrdec k |
| 854 | wrstr wtkids |
| 855 | for i ulong k bound 65536 |
| 856 | if gt i 0 |
| 857 | wrsep |
| 858 | end |
| 859 | wrdec get wrkids i |
| 860 | wrstr wtref |
| 861 | end |
| 862 | wrstr wtkend |
| 863 | wrmark 2 |
| 864 | wrstr wtcat |
| 865 | x = get wrpos 0 |
| 866 | n = get wrnobj 0 |
| 867 | wrstr wtxref |
| 868 | wrdec n |
| 869 | wrbyte 10 |
| 870 | wrstr wtfree |
| 871 | wrbyte 13 |
| 872 | wrbyte 10 |
| 873 | for j ulong sub n 1 bound 65536 |
| 874 | wrpad10 get wroff add j 1 |
| 875 | wrstr wtinuse |
| 876 | wrbyte 13 |
| 877 | wrbyte 10 |
| 878 | end |
| 879 | wrstr wttrail |
| 880 | wrdec n |
| 881 | wrstr wtroot |
| 882 | wrdec x |
| 883 | wrstr wteof |
| 884 | end |
| 885 | |
| 886 | fn wrbegin outarg ulong to void |
| 887 | put ocl 0 0 |
| 888 | put wractive 0 0 |
| 889 | r = argread outarg opath 0 4096 |
| 890 | if or le r 0 ge r 4096 |
| 891 | pquit 5 wmpath |
| 892 | ret |
| 893 | end |
| 894 | put opl 0 ulong r |
| 895 | end |
| 896 | |
| 897 | note Cleanup hook called by core pquit. Do not remove a path that no longer has the created dev/inode. |
| 898 | note A failed lstat leaves the partial file rather than risking deletion of an unrelated replacement. |
| 899 | fn wrcleanup to void |
| 900 | sb = new uchar 128 0 |
| 901 | if eq get wractive 0 0 |
| 902 | ret |
| 903 | end |
| 904 | put wractive 0 0 |
| 905 | r = lstat opath 0 get opl 0 sb 0 128 |
| 906 | same = eq r 0 |
| 907 | if same |
| 908 | for i ulong 16 |
| 909 | if ne get sb i get wrident i |
| 910 | same = false |
| 911 | end |
| 912 | end |
| 913 | end |
| 914 | if same |
| 915 | r = unlink opath 0 get opl 0 |
| 916 | end |
| 917 | r = fclose get wrfd 0 |
| 918 | end |
| 919 | |
| 920 | note O_WRONLY|O_CREAT|O_EXCL = 193, mode 0644. Never reopen/truncate an existing output. |
| 921 | fn wrcreate to void |
| 922 | sb = new uchar 128 0 |
| 923 | fd = fopen opath 0 get opl 0 193 420 |
| 924 | if lt fd 0 |
| 925 | pquit 5 wmexist |
| 926 | ret |
| 927 | end |
| 928 | put wrfd 0 int fd |
| 929 | r = fstat int fd sb 0 128 |
| 930 | if lt r 0 |
| 931 | r = fclose int fd |
| 932 | pquit 68 wmwrite |
| 933 | ret |
| 934 | end |
| 935 | for i ulong 16 |
| 936 | put wrident i get sb i |
| 937 | end |
| 938 | put wractive 0 1 |
| 939 | end |
| 940 | |
| 941 | fn wrclose to void |
| 942 | wrflush |
| 943 | r = fclose get wrfd 0 |
| 944 | if lt r 0 |
| 945 | pquit 68 wmwrite |
| 946 | ret |
| 947 | end |
| 948 | put wractive 0 0 |
| 949 | end |
| 950 | |
| 951 | fn wsplit first ulong last ulong outarg ulong to void |
| 952 | if or or eq first 0 gt first last gt last get pcount 0 |
| 953 | pquit 1 wmusage |
| 954 | ret |
| 955 | end |
| 956 | wrbegin outarg |
| 957 | for k ulong 2 |
| 958 | put wrpass 0 k |
| 959 | if eq k 1 |
| 960 | wrcreate |
| 961 | end |
| 962 | wrstart |
| 963 | wrfsize |
| 964 | wrfile first last |
| 965 | wrfinish |
| 966 | end |
| 967 | wrclose |
| 968 | end |
| 969 | |
| 970 | fn wmerge outarg ulong firstarg ulong argc ulong to void |
| 971 | if ge firstarg argc |
| 972 | pquit 1 wmusage |
| 973 | ret |
| 974 | end |
| 975 | if gt sub argc firstarg 4095 |
| 976 | pquit 63 wminput |
| 977 | ret |
| 978 | end |
| 979 | wrbegin outarg |
| 980 | for k ulong 2 |
| 981 | put wrpass 0 k |
| 982 | if eq k 1 |
| 983 | wrcreate |
| 984 | end |
| 985 | wrstart |
| 986 | for j ulong sub argc firstarg bound 4095 |
| 987 | popen add firstarg j |
| 988 | wrfsize |
| 989 | wrfile 1 get pcount 0 |
| 990 | pclose |
| 991 | end |
| 992 | wrfinish |
| 993 | end |
| 994 | wrclose |
| 995 | end |