Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-pdf/blob/main/src/core.om # omega-pdf Omega PDF: read and write PDF files in Omega Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-pdf / src / core.om` 2140 lines ยท 32256 bytes Raw Blame History 1 note Core is an independent implementation from ISO 32000-1:2008 clauses cited below. 2 text ppref "opdf: " 3 text pnl "\n" 4 text pbad "malformed PDF/xref/object" 5 text pbound "PDF bound or structural cycle" 6 text piomsg "I/O error" 7 text penc "encrypted PDF refused" 8 text pfilt "unsupported stream filter/predictor" 9 text kLength "Length" 10 text kType "Type" 11 text kRoot "Root" 12 text kInfo "Info" 13 text kSize "Size" 14 text kPrev "Prev" 15 text kXRefStm "XRefStm" 16 text kEncrypt "Encrypt" 17 text kFilter "Filter" 18 text kParms "DecodeParms" 19 text kPredict "Predictor" 20 text kColors "Colors" 21 text kBits "BitsPerComponent" 22 text kColumns "Columns" 23 text kXRef "XRef" 24 text kObjStm "ObjStm" 25 text kFirst "First" 26 text kN "N" 27 text kW "W" 28 text kIndex "Index" 29 text kPages "Pages" 30 text kPage "Page" 31 text kKids "Kids" 32 text kCount "Count" 33 text kMedia "MediaBox" 34 text kCrop "CropBox" 35 text kRes "Resources" 36 text kRotate "Rotate" 37 text kCont "Contents" 38 text kVersion "Version" 39 text kTitle "Title" 40 text kAuthor "Author" 41 text kProd "Producer" 42 text kDate "CreationDate" 43 text kF "F" 44 text kFlate "FlateDecode" 45 text kFl "Fl" 46 text kHex "ASCIIHexDecode" 47 text kAHx "AHx" 48 text kA85 "ASCII85Decode" 49 text kA85s "A85" 50 text pObj "obj" 51 text pEndObj "endobj" 52 text pStream "stream" 53 text pEndStm "endstream" 54 text pXref "xref" 55 text pTrailer "trailer" 56 text pR "R" 57 text pTrue "true" 58 text pFalse "false" 59 text pNull "null" 60 text pStart "startxref" 61 text pEOF "%%EOF" 62 text pMem "omega-pdf-filter" 63 fn pquit c int msg view uchar * to void 64 wrcleanup 65 r = write 2 ppref 0 len ppref 66 r = write 2 msg 0 len msg 67 r = write 2 pnl 0 1 68 exit c 69 end 70 fn pwork to void 71 v = get pst 8 72 if ge v `100000000` 73 pquit 63 pbound 74 end 75 put pst 8 add v 1 76 end 77 fn palloc typ ulong to ulong 78 n = get pst 0 79 if ge n 262143 80 pquit 63 pbound 81 end 82 n = add n 1 83 put pst 0 n 84 put ptype n typ 85 put pnum n 0 86 put pgen n 0 87 put preal n 0.0f64 88 put pfirst n 0 89 put pnext n 0 90 put pstart n 0 91 put plen n 0 92 put psoff n 0 93 put pslen n 0 94 ret n 95 end 96 fn pstore c ulong to void 97 n = get pst 1 98 if ge n `16777216` 99 pquit 63 pbound 100 end 101 put pbytes n uchar and c 255 102 put pst 1 add n 1 103 end 104 note 7.5.4 random access. The file is never read into a whole-file buffer. 105 fn pbyte to ulong 106 pwork 107 p = get pst 2 108 if ne get pst 3 0 109 if ge p get pst 4 110 ret 256 111 end 112 ret ulong get pmbuf p 113 end 114 if ge p get psize 0 115 ret 256 116 end 117 b = get pst 5 118 z = get pst 6 119 if ge p b 120 if lt sub p b z 121 ret ulong get pwin sub p b 122 end 123 end 124 r = pread get pfd 0 pwin 0 65536 long p 125 if lt r 0 126 pquit 64 piomsg 127 end 128 if eq r 0 129 pquit 60 pbad 130 end 131 put pst 5 p 132 put pst 6 ulong r 133 ret ulong get pwin 0 134 end 135 fn ptake to ulong 136 c = pbyte 137 if eq c 256 138 pquit 60 pbad 139 end 140 put pst 2 add get pst 2 1 141 ret c 142 end 143 fn pwhite c ulong to bool 144 ret or or or eq c 0 eq c 9 or eq c 10 eq c 12 or eq c 13 eq c 32 145 end 146 fn pdelim c ulong to bool 147 if pwhite c 148 ret true 149 end 150 a = or or eq c 40 eq c 41 or eq c 60 eq c 62 151 b = or or eq c 91 eq c 93 or eq c 123 eq c 125 152 ret or or a b or eq c 47 eq c 37 153 end 154 fn pskip to void 155 while true bound `100000001` 156 c = pbyte 157 if eq c 256 158 ret 159 end 160 if pwhite c 161 put pst 2 add get pst 2 1 162 continue 163 end 164 if ne c 37 165 ret 166 end 167 while true bound `100000001` 168 if eq pbyte 256 169 ret 170 end 171 c = ptake 172 if or eq c 10 eq c 13 173 break 174 end 175 end 176 end 177 end 178 fn phex c ulong to ulong 179 if and ge c 48 le c 57 180 ret sub c 48 181 end 182 if and ge c 65 le c 70 183 ret sub c 55 184 end 185 if and ge c 97 le c 102 186 ret sub c 87 187 end 188 pquit 60 pbad 189 ret 0 190 end 191 fn pstrput c ulong start ulong maxn ulong to void 192 if ge sub get pst 1 start maxn 193 pquit 63 pbound 194 end 195 pstore c 196 end 197 note 7.3.4.2: literal escapes, balanced parentheses, CR/CRLF normalisation. 198 fn pliteral to ulong 199 n = palloc 6 200 s = get pst 1 201 d = ulong 1 202 while gt d 0 bound `100000001` 203 c = ptake 204 if eq c 40 205 d = add d 1 206 if gt d 64 207 pquit 63 pbound 208 end 209 end 210 if eq c 41 211 d = sub d 1 212 if eq d 0 213 break 214 end 215 end 216 if eq c 92 217 c = ptake 218 if or eq c 10 eq c 13 219 if eq c 13 220 if eq pbyte 10 221 v = ptake 222 end 223 end 224 continue 225 end 226 if and ge c 48 le c 55 227 v = sub c 48 228 j = ulong 0 229 while lt j 2 bound 3 230 q = pbyte 231 if or lt q 48 gt q 55 232 break 233 end 234 q = ptake 235 v = add mul v 8 sub q 48 236 j = add j 1 237 end 238 c = and v 255 239 else 240 if eq c 110 241 c = 10 242 end 243 if eq c 114 244 c = 13 245 end 246 if eq c 116 247 c = 9 248 end 249 if eq c 98 250 c = 8 251 end 252 if eq c 102 253 c = 12 254 end 255 end 256 else 257 if eq c 13 258 if eq pbyte 10 259 v = ptake 260 end 261 c = 10 262 end 263 end 264 pstrput c s 65536 265 end 266 put pstart n s 267 put plen n sub get pst 1 s 268 ret n 269 end 270 fn phexstr to ulong 271 n = palloc 6 272 s = get pst 1 273 hi = ulong 256 274 while true bound `100000001` 275 c = ptake 276 if eq c 62 277 break 278 end 279 if pwhite c 280 continue 281 end 282 v = phex c 283 if eq hi 256 284 hi = v 285 else 286 pstrput add mul hi 16 v s 65536 287 hi = 256 288 end 289 end 290 if ne hi 256 291 pstrput mul hi 16 s 65536 292 end 293 put pstart n s 294 put plen n sub get pst 1 s 295 ret n 296 end 297 fn pname to ulong 298 n = palloc 5 299 s = get pst 1 300 while true bound `100000001` 301 c = pbyte 302 if eq c 256 303 break 304 end 305 if pdelim c 306 break 307 end 308 c = ptake 309 if eq c 35 310 a = phex ptake 311 b = phex ptake 312 c = add mul a 16 b 313 end 314 if eq c 0 315 pquit 60 pbad 316 end 317 pstrput c s 256 318 end 319 put pstart n s 320 put plen n sub get pst 1 s 321 ret n 322 end 323 note Token codes: 0 EOF, 1 word, 2 preallocated name/string, 3 [, 4 ], 5 <<, 6 >>. 324 fn ptoken to ulong 325 pskip 326 put pst 7 0 327 put pst 22 0 328 c = pbyte 329 if eq c 256 330 ret 0 331 end 332 c = ptake 333 if eq c 47 334 put pst 22 pname 335 ret 2 336 end 337 if eq c 40 338 put pst 22 pliteral 339 ret 2 340 end 341 if eq c 91 342 ret 3 343 end 344 if eq c 93 345 ret 4 346 end 347 if eq c 60 348 if eq pbyte 60 349 v = ptake 350 ret 5 351 end 352 put pst 22 phexstr 353 ret 2 354 end 355 if eq c 62 356 if ne ptake 62 357 pquit 60 pbad 358 end 359 ret 6 360 end 361 if pdelim c 362 pquit 60 pbad 363 end 364 n = ulong 0 365 while true bound 257 366 if ge n 256 367 pquit 63 pbound 368 end 369 put ptok n uchar c 370 n = add n 1 371 c = pbyte 372 if eq c 256 373 break 374 end 375 if pdelim c 376 break 377 end 378 c = ptake 379 end 380 put pst 7 n 381 ret 1 382 end 383 fn pword s view uchar * to bool 384 n = get pst 7 385 if ne n len s 386 ret false 387 end 388 for i ulong n bound 256 389 if ne get ptok i get s i 390 ret false 391 end 392 end 393 ret true 394 end 395 fn pexpect s view uchar * to void 396 if ne ptoken 1 397 pquit 60 pbad 398 end 399 if not pword s 400 pquit 60 pbad 401 end 402 end 403 note Bounded signed magnitude accumulator; explicit bounds prevent signed overflow traps. 404 fn pnumtok to ulong 405 z = get pst 7 406 if or eq z 0 gt z 128 407 pquit 63 pbound 408 end 409 n = palloc 3 410 s = get pst 1 411 for j ulong z bound 128 412 pstore ulong get ptok j 413 end 414 put pstart n s 415 put plen n z 416 i = ulong 0 417 minus = false 418 c = ulong get ptok 0 419 if or eq c 43 eq c 45 420 minus = eq c 45 421 i = 1 422 end 423 v = ulong 0 424 f = f64 0.0 425 scale = f64 1.0 426 dot = false 427 digits = ulong 0 428 while lt i z bound 129 429 c = ulong get ptok i 430 i = add i 1 431 if eq c 46 432 if dot 433 pquit 60 pbad 434 end 435 dot = true 436 continue 437 end 438 if or lt c 48 gt c 57 439 pquit 60 pbad 440 end 441 digits = add digits 1 442 q = sub c 48 443 if not dot 444 if gt v `922337203685477580` 445 pquit 63 pbound 446 end 447 if eq v `922337203685477580` 448 lim = ulong 7 449 if minus 450 lim = 8 451 end 452 if gt q lim 453 pquit 63 pbound 454 end 455 end 456 v = add mul v 10 q 457 end 458 f = add mul f 10.0f64 f64 q 459 if dot 460 scale = mul scale 10.0f64 461 end 462 end 463 if eq digits 0 464 pquit 60 pbad 465 end 466 if dot 467 put ptype n 4 468 f = div f scale 469 if minus 470 f = neg f 471 end 472 put preal n f 473 else 474 if minus 475 if eq v `9223372036854775808` 476 put pnum n -`9223372036854775808` 477 else 478 put pnum n neg long v 479 end 480 else 481 put pnum n long v 482 end 483 end 484 ret n 485 end 486 fn pscalar code ulong to ulong 487 if eq code 2 488 ret get pst 22 489 end 490 if ne code 1 491 pquit 60 pbad 492 end 493 if pword pTrue 494 n = palloc 2 495 put pnum n 1 496 ret n 497 end 498 if pword pFalse 499 ret palloc 2 500 end 501 if pword pNull 502 ret palloc 1 503 end 504 n = pnumtok 505 if ne get ptype n 3 506 ret n 507 end 508 if lt get pnum n 0 509 ret n 510 end 511 save = get pst 2 512 t0 = get pst 0 513 t1 = get pst 1 514 code2 = ptoken 515 if eq code2 1 516 z = get pst 7 517 numeric = gt z 0 518 for j ulong z bound 256 519 c = ulong get ptok j 520 if or lt c 48 gt c 57 521 numeric = false 522 end 523 end 524 if numeric 525 g = pnumtok 526 if eq get ptype g 3 527 if eq ptoken 1 528 if pword pR 529 if gt get pnum g 65535 530 pquit 63 pbound 531 end 532 put ptype n 9 533 put pgen n ulong get pnum g 534 put pst 0 t0 535 put pst 1 t1 536 ret n 537 end 538 end 539 end 540 end 541 end 542 put pst 2 save 543 put pst 0 t0 544 put pst 1 t1 545 ret n 546 end 547 note 7.3.6-7.3.7: iterative containers, at most 64 levels; child links never recurse. 548 note pstk, ptl, pcn: the open containers of pparse (node, last child, child count); pparse never nests. 549 global pstk ulong 64 550 global ptl ulong 64 551 global pcn ulong 64 552 fn pparse to ulong 553 d = ulong 0 554 root = ulong 0 555 while true bound `100000001` 556 pwork 557 code = ptoken 558 if or eq code 4 eq code 6 559 if eq d 0 560 pquit 60 pbad 561 end 562 j = sub d 1 563 n = get pstk j 564 typ = get ptype n 565 if or and eq code 4 ne typ 7 and eq code 6 ne typ 8 566 pquit 60 pbad 567 end 568 if eq typ 8 569 if ne rem get pcn j 2 0 570 pquit 60 pbad 571 end 572 end 573 d = j 574 if eq d 0 575 ret root 576 end 577 continue 578 end 579 n = ulong 0 580 if eq code 3 581 n = palloc 7 582 else 583 if eq code 5 584 n = palloc 8 585 else 586 n = pscalar code 587 end 588 end 589 if eq d 0 590 root = n 591 else 592 j = sub d 1 593 parent = get pstk j 594 if eq get ptype parent 8 595 if eq rem get pcn j 2 0 596 if ne get ptype n 5 597 pquit 60 pbad 598 end 599 end 600 end 601 lastn = get ptl j 602 if eq lastn 0 603 put pfirst parent n 604 else 605 put pnext lastn n 606 end 607 put ptl j n 608 put pcn j add get pcn j 1 609 end 610 if or eq code 3 eq code 5 611 if ge d 64 612 pquit 63 pbound 613 end 614 put pstk d n 615 put ptl d 0 616 put pcn d 0 617 d = add d 1 618 else 619 if eq d 0 620 ret root 621 end 622 end 623 end 624 pquit 63 pbound 625 ret 0 626 end 627 fn pnameeq n ulong s view uchar * to bool 628 if eq n 0 629 ret false 630 end 631 if ne get ptype n 5 632 ret false 633 end 634 z = get plen n 635 if ne z len s 636 ret false 637 end 638 b = get pstart n 639 for i ulong z bound 256 640 if ne get pbytes add b i get s i 641 ret false 642 end 643 end 644 ret true 645 end 646 fn pgetraw d ulong key view uchar * to ulong 647 if eq d 0 648 ret 0 649 end 650 if and ne get ptype d 8 ne get ptype d 10 651 pquit 60 pbad 652 end 653 k = get pfirst d 654 steps = ulong 0 655 while ne k 0 bound 262145 656 if ge steps 262144 657 pquit 63 pbound 658 end 659 steps = add steps 1 660 v = get pnext k 661 if eq v 0 662 pquit 60 pbad 663 end 664 if pnameeq k key 665 if eq get ptype v 1 666 ret 0 667 end 668 ret v 669 end 670 k = get pnext v 671 end 672 ret 0 673 end 674 fn pget d ulong key view uchar * to ulong 675 ret pgetraw pderef d key 676 end 677 fn pne n ulong name view uchar * to bool 678 ret pnameeq pderef n name 679 end 680 fn pint n ulong to long 681 n = pderef n 682 if eq n 0 683 pquit 60 pbad 684 end 685 if ne get ptype n 3 686 pquit 60 pbad 687 end 688 ret get pnum n 689 end 690 fn pnumber n ulong to f64 691 n = pderef n 692 if eq n 0 693 pquit 60 pbad 694 end 695 if eq get ptype n 3 696 ret f64 get pnum n 697 end 698 if ne get ptype n 4 699 pquit 60 pbad 700 end 701 ret get preal n 702 end 703 fn ppositive n ulong to ulong 704 v = pint n 705 if lt v 0 706 pquit 60 pbad 707 end 708 ret ulong v 709 end 710 fn parray n ulong to ulong 711 n = pderef n 712 if eq n 0 713 pquit 60 pbad 714 end 715 if ne get ptype n 7 716 pquit 60 pbad 717 end 718 ret get pfirst n 719 end 720 fn pparam d ulong key view uchar * def ulong to ulong 721 n = pderef pget d key 722 if eq n 0 723 ret def 724 end 725 ret ppositive n 726 end 727 728 note S2 rename seam: wflush calls this sink; checksum is already updated by vendor io.om. 729 fn wout n ulong to void 730 z = get pdlen 0 731 if gt n sub ulong `16777216` z 732 pquit 63 pbound 733 end 734 for i ulong n bound 32768 735 put pdbuf add z i get win i 736 end 737 put pdlen 0 add z n 738 end 739 fn pcopyfilter to void 740 n = get pdlen 0 741 for i ulong n bound `16777216` 742 put pfbuf i get pdbuf i 743 end 744 put pdlen 0 0 745 end 746 fn pdput c ulong to void 747 n = get pdlen 0 748 if ge n `16777216` 749 pquit 63 pbound 750 end 751 put pdbuf n uchar and c 255 752 put pdlen 0 add n 1 753 end 754 fn phexdecode n ulong to void 755 hi = ulong 256 756 i = ulong 0 757 done = false 758 while lt i n bound `16777217` 759 c = ulong get pfbuf i 760 i = add i 1 761 if pwhite c 762 continue 763 end 764 if done 765 pquit 60 pbad 766 end 767 if eq c 62 768 done = true 769 if ne hi 256 770 pdput mul hi 16 771 end 772 continue 773 end 774 v = phex c 775 if eq hi 256 776 hi = v 777 else 778 pdput add mul hi 16 v 779 hi = 256 780 end 781 end 782 if not done 783 pquit 60 pbad 784 end 785 end 786 fn pa85group v ulong n ulong to void 787 if gt v `4294967295` 788 pquit 60 pbad 789 end 790 for j ulong n bound 4 791 pdput and shr v mul sub ulong 3 j 8 255 792 end 793 end 794 note 7.4.3: z at group boundaries only; final 2..4 digits padded with u. 795 fn pa85decode n ulong to void 796 i = ulong 0 797 v = ulong 0 798 cnt = ulong 0 799 done = false 800 while lt i n bound `16777217` 801 c = ulong get pfbuf i 802 i = add i 1 803 if pwhite c 804 continue 805 end 806 if done 807 pquit 60 pbad 808 end 809 if eq c 126 810 if ge i n 811 pquit 60 pbad 812 end 813 if ne get pfbuf i 62 814 pquit 60 pbad 815 end 816 i = add i 1 817 if eq cnt 1 818 pquit 60 pbad 819 end 820 if gt cnt 1 821 keep = sub cnt 1 822 while lt cnt 5 bound 5 823 v = add mul v 85 84 824 cnt = add cnt 1 825 end 826 pa85group v keep 827 end 828 done = true 829 continue 830 end 831 if eq c 122 832 if ne cnt 0 833 pquit 60 pbad 834 end 835 pa85group 0 4 836 continue 837 end 838 if or lt c 33 gt c 117 839 pquit 60 pbad 840 end 841 v = add mul v 85 sub c 33 842 cnt = add cnt 1 843 if eq cnt 5 844 pa85group v 4 845 cnt = 0 846 v = 0 847 end 848 end 849 if not done 850 pquit 60 pbad 851 end 852 end 853 note 7.4.4 Flate/RFC1950: only a temporary anonymous file adapts the S1 pread interface; 854 note all decoding is Omega and vendor status codes remain unchanged. 855 fn pflate n ulong to void 856 fd = memfd pMem 0 len pMem 0 857 if lt fd 0 858 pquit 64 piomsg 859 end 860 i = ulong 0 861 while lt i n bound `16777217` 862 r = write int fd pfbuf i sub n i 863 if le r 0 864 pquit 64 piomsg 865 end 866 i = add i ulong r 867 end 868 insrc ulong fd 1 0 n 869 osink -1 OUTMAX 0 2 870 c = inbyte 871 f = inbyte 872 if or ne and c 15 8 gt shr c 4 7 873 fail 30 tzhdr 874 end 875 if ne rem or shl c 8 f 31 0 876 fail 30 tzhdr 877 end 878 if ne and f 32 0 879 fail 31 tzdict 880 end 881 inflate 882 put bitb 0 0 883 put bitn 0 0 884 v = ulong 0 885 for j ulong 4 886 v = or shl v 8 inbyte 887 end 888 if ne v or shl get adb 0 16 get ada 0 889 fail 32 tzsum 890 end 891 used = sub sub n get irem 0 sub get iend 0 get ipos 0 892 r = fclose int fd 893 if lt r 0 894 pquit 64 piomsg 895 end 896 if ne used n 897 pquit 60 pbad 898 end 899 end 900 fn ppaeth a long b long c long to ulong 901 p = sub add a b c 902 x = sub p a 903 if lt x 0 904 x = neg x 905 end 906 y = sub p b 907 if lt y 0 908 y = neg y 909 end 910 z = sub p c 911 if lt z 0 912 z = neg z 913 end 914 if and le x y le x z 915 ret ulong a 916 end 917 if le y z 918 ret ulong b 919 end 920 ret ulong c 921 end 922 note 7.4.4.4: each PNG row carries its own filter byte even Predictor=10..14. 923 fn ppngbyte pos ulong row ulong bpp ulong tag ulong to void 924 a = ulong 0 925 b = ulong 0 926 c = ulong 0 927 col = rem pos row 928 if ge col bpp 929 a = ulong get pdbuf sub pos bpp 930 end 931 if ge pos row 932 b = ulong get pdbuf sub pos row 933 if ge col bpp 934 c = ulong get pdbuf sub sub pos row bpp 935 end 936 end 937 v = ulong get pfbuf add add div pos row 1 pos 938 if eq tag 1 939 v = add v a 940 end 941 if eq tag 2 942 v = add v b 943 end 944 if eq tag 3 945 v = add v div add a b 2 946 end 947 if eq tag 4 948 v = add v ppaeth long a long b long c 949 end 950 pdput v 951 end 952 fn ppredict pred ulong colors ulong bitsn ulong cols ulong to void 953 if eq pred 1 954 ret 955 end 956 if or lt pred 10 gt pred 15 957 pquit 62 pfilt 958 end 959 if or eq colors 0 eq cols 0 960 pquit 60 pbad 961 end 962 if or gt colors `16777216` gt cols `16777216` 963 pquit 63 pbound 964 end 965 ok = or or eq bitsn 1 eq bitsn 2 or eq bitsn 4 or eq bitsn 8 eq bitsn 16 966 if not ok 967 pquit 62 pfilt 968 end 969 sample = mul colors bitsn 970 if gt sample `134217728` 971 pquit 63 pbound 972 end 973 if gt cols div ulong `134217728` sample 974 pquit 63 pbound 975 end 976 row = div add mul sample cols 7 8 977 bpp = div add sample 7 8 978 n = get pdlen 0 979 pcopyfilter 980 stride = add row 1 981 if ne rem n stride 0 982 pquit 60 pbad 983 end 984 ppngrows n row bpp 985 end 986 fn ppngrows n ulong row ulong bpp ulong to void 987 stride = add row 1 988 rows = div n stride 989 for j ulong rows bound `16777216` 990 tag = ulong get pfbuf mul j stride 991 if gt tag 4 992 pquit 60 pbad 993 end 994 for k ulong row bound `16777216` 995 ppngbyte add mul j row k row bpp tag 996 end 997 end 998 end 999 fn pfilterkind n ulong to ulong 1000 if or pne n kFlate pne n kFl 1001 ret 1 1002 end 1003 if or pne n kHex pne n kAHx 1004 ret 2 1005 end 1006 if or pne n kA85 pne n kA85s 1007 ret 3 1008 end 1009 pquit 62 pfilt 1010 ret 0 1011 end 1012 note pfk, pfp, pfc, pfb, pfw: filter kind, Predictor, Colors, BitsPerComponent, Columns of each filter; 1013 note decode depth d (pst[16]) uses slots 64(d-1) .. 64(d-1)+63, so a nested pdecode never overwrites them. 1014 global pfk ulong 4096 1015 global pfp ulong 4096 1016 global pfc ulong 4096 1017 global pfb ulong 4096 1018 global pfw ulong 4096 1019 fn pdecfilters n ulong to void 1020 bs = mul sub get pst 16 1 64 1021 f = pderef pget n kFilter 1022 p = pderef pget n kParms 1023 multi = false 1024 if ne f 0 1025 if eq get ptype f 7 1026 multi = true 1027 f = get pfirst f 1028 end 1029 end 1030 if multi 1031 if ne p 0 1032 p = parray p 1033 end 1034 end 1035 cnt = ulong 0 1036 while ne f 0 bound 65 1037 if ge cnt 64 1038 pquit 63 pbound 1039 end 1040 put pfk add bs cnt pfilterkind f 1041 put pfp add bs cnt pparam p kPredict 1 1042 put pfc add bs cnt pparam p kColors 1 1043 put pfb add bs cnt pparam p kBits 8 1044 put pfw add bs cnt pparam p kColumns 1 1045 cnt = add cnt 1 1046 if not multi 1047 break 1048 end 1049 f = get pnext f 1050 if ne p 0 1051 p = get pnext p 1052 end 1053 end 1054 if multi 1055 if ne p 0 1056 pquit 60 pbad 1057 end 1058 end 1059 put pst 23 cnt 1060 pdecodedata n bs 1061 end 1062 fn pdecodedata n ulong bs ulong to void 1063 note Resolve filter/parameter references before placing data in the clobberable decoded buffer. 1064 z = get pslen n 1065 if gt z `16777216` 1066 pquit 63 pbound 1067 end 1068 i = ulong 0 1069 while lt i z bound `16777217` 1070 r = pread get pfd 0 pdbuf i sub z i long add get psoff n i 1071 if lt r 0 1072 pquit 64 piomsg 1073 end 1074 if eq r 0 1075 pquit 60 pbad 1076 end 1077 i = add i ulong r 1078 end 1079 put pdlen 0 z 1080 for j ulong get pst 23 bound 64 1081 pfstep add bs j 1082 end 1083 end 1084 note pfstep: apply filter slot k to pdbuf[0 .. pdlen). 1085 fn pfstep k ulong to void 1086 z = get pdlen 0 1087 pcopyfilter 1088 kind = get pfk k 1089 if eq kind 1 1090 pflate z 1091 pfpred k 1092 end 1093 if eq kind 2 1094 phexdecode z 1095 end 1096 if eq kind 3 1097 pa85decode z 1098 end 1099 end 1100 fn pfpred k ulong to void 1101 a = get pfp k 1102 b = get pfc k 1103 c = get pfb k 1104 d = get pfw k 1105 ppredict a b c d 1106 end 1107 fn pdecode n ulong to void 1108 n = pderef n 1109 if eq n 0 1110 pquit 60 pbad 1111 end 1112 if ne get ptype n 10 1113 pquit 60 pbad 1114 end 1115 if ne pderef pget n kF 0 1116 pquit 62 pfilt 1117 end 1118 d = get pst 16 1119 if ge d 64 1120 pquit 63 pbound 1121 end 1122 put pst 16 add d 1 1123 pdecfilters n 1124 put pst 16 d 1125 end 1126 1127 note 7.3.8 and 7.3.10: streams have exact Length, possibly an indirect integer. 1128 fn pobject off ulong want ulong gen ulong to ulong 1129 if ge off get psize 0 1130 pquit 60 pbad 1131 end 1132 put pst 3 0 1133 put pst 2 off 1134 if ne ptoken 1 1135 pquit 60 pbad 1136 end 1137 id = pnumval 1138 if eq id 0 1139 pquit 60 pbad 1140 end 1141 if gt id 65535 1142 pquit 63 pbound 1143 end 1144 if ne ptoken 1 1145 pquit 60 pbad 1146 end 1147 g = pnumval 1148 if gt g 65535 1149 pquit 63 pbound 1150 end 1151 if ne want 0 1152 if or ne id want ne g gen 1153 pquit 60 pbad 1154 end 1155 end 1156 pexpect pObj 1157 n = pparse 1158 if ne ptoken 1 1159 pquit 60 pbad 1160 end 1161 if pword pStream 1162 if ne get ptype n 8 1163 pquit 60 pbad 1164 end 1165 c = ptake 1166 if eq c 13 1167 if ne ptake 10 1168 pquit 60 pbad 1169 end 1170 else 1171 if ne c 10 1172 pquit 60 pbad 1173 end 1174 end 1175 start = get pst 2 1176 l = pgetraw n kLength 1177 z = ppositive l 1178 put pst 3 0 1179 put pst 2 start 1180 if gt z sub get psize 0 start 1181 pquit 60 pbad 1182 end 1183 put psoff n start 1184 put pslen n z 1185 put ptype n 10 1186 put pst 2 add start z 1187 c = pbyte 1188 if eq c 13 1189 c = ptake 1190 if eq pbyte 10 1191 c = ptake 1192 end 1193 else 1194 if eq c 10 1195 c = ptake 1196 end 1197 end 1198 pexpect pEndStm 1199 pexpect pEndObj 1200 else 1201 if not pword pEndObj 1202 pquit 60 pbad 1203 end 1204 end 1205 ret n 1206 end 1207 fn ploadplain id ulong to ulong 1208 ret pobject get pxa id id get pxb id 1209 end 1210 note 7.5.7: decompress one object stream and parse every bounded object once. 1211 note The separate pmbuf snapshot survives later stream decodes; parsing has no dereferences. 1212 note Structural integers are parsed without retaining nodes or lexical bytes. 1213 fn pnumval to ulong 1214 t0 = get pst 0 1215 t1 = get pst 1 1216 n = pnumtok 1217 if ne get ptype n 3 1218 pquit 60 pbad 1219 end 1220 if lt get pnum n 0 1221 pquit 60 pbad 1222 end 1223 v = ulong get pnum n 1224 put pst 0 t0 1225 put pst 1 t1 1226 ret v 1227 end 1228 fn pmreadint to ulong 1229 if ne ptoken 1 1230 pquit 60 pbad 1231 end 1232 ret pnumval 1233 end 1234 fn pmheader n ulong first ulong to void 1235 put pst 3 1 1236 put pst 2 0 1237 put pst 4 first 1238 prev = ulong 0 1239 for i ulong n bound 65536 1240 id = pmreadint 1241 off = pmreadint 1242 if or eq id 0 gt id 65535 1243 pquit 63 pbound 1244 end 1245 if ne get pmseen id 0 1246 pquit 60 pbad 1247 end 1248 put pmseen id 1 1249 if ne i 0 1250 if le off prev 1251 pquit 60 pbad 1252 end 1253 end 1254 prev = off 1255 if ge off sub get pdlen 0 first 1256 pquit 60 pbad 1257 end 1258 put pmids i id 1259 put pmoffs i add first off 1260 end 1261 pskip 1262 if ne get pst 2 first 1263 pquit 60 pbad 1264 end 1265 end 1266 fn pmparse n ulong total ulong owner ulong to void 1267 for i ulong n bound 65536 1268 id = get pmids i 1269 put pst 2 get pmoffs i 1270 last = total 1271 if lt add i 1 n 1272 last = get pmoffs add i 1 1273 end 1274 put pst 4 last 1275 node = pparse 1276 if eq get ptype node 9 1277 pquit 60 pbad 1278 end 1279 pskip 1280 if ne get pst 2 last 1281 pquit 60 pbad 1282 end 1283 if eq get pxt id 3 1284 if eq get pxa id owner 1285 if ne get pxb id i 1286 pquit 60 pbad 1287 end 1288 if eq get pobjs id 0 1289 put pobjs id node 1290 end 1291 end 1292 end 1293 put pmseen id 0 1294 end 1295 end 1296 fn ploadpacked id ulong to ulong 1297 owner = get pxa id 1298 if or eq owner 0 gt owner 65535 1299 pquit 60 pbad 1300 end 1301 if ne get pxt owner 2 1302 pquit 60 pbad 1303 end 1304 s = pload owner 1305 if not pne pget s kType kObjStm 1306 pquit 60 pbad 1307 end 1308 n = ppositive pget s kN 1309 first = ppositive pget s kFirst 1310 if gt n 65536 1311 pquit 63 pbound 1312 end 1313 if ge get pxb id n 1314 pquit 60 pbad 1315 end 1316 pdecode s 1317 z = get pdlen 0 1318 if gt first z 1319 pquit 60 pbad 1320 end 1321 for i ulong z bound `16777216` 1322 put pmbuf i get pdbuf i 1323 end 1324 pmheader n first 1325 pmparse n z owner 1326 put pst 14 1 1327 ret get pobjs id 1328 end 1329 fn pload id ulong to ulong 1330 if or eq id 0 gt id 65535 1331 ret 0 1332 end 1333 if ge id get pst 11 1334 ret 0 1335 end 1336 t = get pxt id 1337 if or eq t 0 eq t 1 1338 ret 0 1339 end 1340 n = get pobjs id 1341 if ne n 0 1342 ret n 1343 end 1344 if ne get poload id 0 1345 pquit 63 pbound 1346 end 1347 d = get pst 9 1348 if ge d 64 1349 pquit 63 pbound 1350 end 1351 put pst 9 add d 1 1352 put poload id 1 1353 pos = get pst 2 1354 mode = get pst 3 1355 limit = get pst 4 1356 if eq t 2 1357 n = ploadplain id 1358 else 1359 n = ploadpacked id 1360 end 1361 put pst 2 pos 1362 put pst 3 mode 1363 put pst 4 limit 1364 put poload id 0 1365 put pst 9 d 1366 put pobjs id n 1367 ret n 1368 end 1369 note 7.3.9-7.3.10: nonexistent/free/wrong-generation references are null, not errors. 1370 fn pderef n ulong to ulong 1371 steps = ulong 0 1372 while ne n 0 bound 65 1373 if ge n 262144 1374 pquit 63 pbound 1375 end 1376 t = get ptype n 1377 if eq t 1 1378 ret 0 1379 end 1380 if ne t 9 1381 ret n 1382 end 1383 if ge steps 64 1384 pquit 63 pbound 1385 end 1386 steps = add steps 1 1387 idn = get pnum n 1388 if or le idn 0 gt idn 65535 1389 ret 0 1390 end 1391 id = ulong idn 1392 if ge id get pst 11 1393 ret 0 1394 end 1395 if eq get pxt id 3 1396 if ne get pgen n 0 1397 ret 0 1398 end 1399 else 1400 if ne get pgen n get pxb id 1401 ret 0 1402 end 1403 end 1404 n = pload id 1405 end 1406 ret 0 1407 end 1408 note Xref traversal is newest-first. Both free and unknown entries mask older revisions. 1409 fn pxput id ulong typ ulong a ulong b ulong to void 1410 if gt id 65535 1411 pquit 63 pbound 1412 end 1413 rev = get pst 12 1414 if eq get pxrev id rev 1415 pquit 60 pbad 1416 end 1417 if ne get pxt id 0 1418 ret 1419 end 1420 if eq typ 2 1421 if or eq id 0 ge a get psize 0 1422 pquit 60 pbad 1423 end 1424 if gt b 65535 1425 pquit 63 pbound 1426 end 1427 end 1428 if eq typ 3 1429 if or eq a 0 gt a 65535 1430 pquit 63 pbound 1431 end 1432 if gt b 65535 1433 pquit 63 pbound 1434 end 1435 put pst 14 1 1436 end 1437 put pxt id typ 1438 put pxa id a 1439 put pxb id b 1440 put pxrev id rev 1441 end 1442 fn pxvisit off ulong to void 1443 n = get pst 10 1444 if ge n 1024 1445 pquit 63 pbound 1446 end 1447 for i ulong n bound 1024 1448 if eq get pvis i off 1449 pquit 63 pbound 1450 end 1451 end 1452 put pvis n off 1453 put pst 10 add n 1 1454 put pst 12 add get pst 12 1 1455 end 1456 fn pxtrailer d ulong to void 1457 if ne pgetraw d kEncrypt 0 1458 pquit 61 penc 1459 end 1460 z = ppositive pgetraw d kSize 1461 if or eq z 0 gt z 65536 1462 pquit 63 pbound 1463 end 1464 if eq get pst 11 0 1465 put pst 11 z 1466 put proot 0 pgetraw d kRoot 1467 put pinfo 0 pgetraw d kInfo 1468 end 1469 end 1470 fn pxclassic to ulong 1471 sections = ulong 0 1472 while true bound 65538 1473 if ge sections 65536 1474 pquit 63 pbound 1475 end 1476 sections = add sections 1 1477 code = ptoken 1478 if ne code 1 1479 pquit 60 pbad 1480 end 1481 if pword pTrailer 1482 n = pparse 1483 if ne get ptype n 8 1484 pquit 60 pbad 1485 end 1486 pxtrailer n 1487 ret n 1488 end 1489 start = pnumval 1490 count = pmreadint 1491 if gt start 65535 1492 pquit 63 pbound 1493 end 1494 if gt count sub ulong 65536 start 1495 pquit 63 pbound 1496 end 1497 for j ulong count bound 65536 1498 a = pmreadint 1499 b = pmreadint 1500 if ne ptoken 1 1501 pquit 60 pbad 1502 end 1503 if and eq get pst 7 1 eq get ptok 0 110 1504 pxput add start j 2 a b 1505 else 1506 if and eq get pst 7 1 eq get ptok 0 102 1507 pxput add start j 1 a b 1508 else 1509 pquit 60 pbad 1510 end 1511 end 1512 end 1513 end 1514 pquit 63 pbound 1515 ret 0 1516 end 1517 fn pxfield pos ulong width ulong to ulong 1518 v = ulong 0 1519 for j ulong width bound 8 1520 v = or shl v 8 ulong get pdbuf add pos j 1521 end 1522 ret v 1523 end 1524 fn pxrange start ulong count ulong pos ulong widths view ulong 3 to ulong 1525 if gt start 65535 1526 pquit 63 pbound 1527 end 1528 if gt count sub ulong 65536 start 1529 pquit 63 pbound 1530 end 1531 w0 = get widths 0 1532 w1 = get widths 1 1533 w2 = get widths 2 1534 stride = add add w0 w1 w2 1535 if gt count div sub get pdlen 0 pos stride 1536 pquit 60 pbad 1537 end 1538 for j ulong count bound 65536 1539 t = ulong 1 1540 if ne w0 0 1541 t = pxfield pos w0 1542 end 1543 a = pxfield add pos w0 w1 1544 b = pxfield add add pos w0 w1 w2 1545 if gt t 2 1546 t = 0 1547 end 1548 t = add t 1 1549 pxput add start j t a b 1550 pos = add pos stride 1551 end 1552 ret pos 1553 end 1554 note 7.5.8: W fields are big-endian, absent type defaults to 1, unknown types are null. 1555 fn pxstream off ulong to ulong 1556 widths = new ulong 3 0 1557 n = pobject off 0 0 1558 if ne get ptype n 10 1559 pquit 60 pbad 1560 end 1561 if not pne pgetraw n kType kXRef 1562 pquit 60 pbad 1563 end 1564 pxtrailer n 1565 w = parray pgetraw n kW 1566 for j ulong 3 1567 if eq w 0 1568 pquit 60 pbad 1569 end 1570 v = ppositive w 1571 if gt v 8 1572 pquit 63 pbound 1573 end 1574 put widths j v 1575 w = get pnext w 1576 end 1577 if ne w 0 1578 pquit 60 pbad 1579 end 1580 stride = add add get widths 0 get widths 1 get widths 2 1581 if eq stride 0 1582 pquit 60 pbad 1583 end 1584 idx = pderef pgetraw n kIndex 1585 pdecode n 1586 pos = ulong 0 1587 if eq idx 0 1588 pos = pxrange 0 ppositive pgetraw n kSize pos widths 1589 else 1590 k = parray idx 1591 last = ulong 0 1592 steps = ulong 0 1593 while ne k 0 bound 65537 1594 if ge steps 65536 1595 pquit 63 pbound 1596 end 1597 steps = add steps 1 1598 start = ppositive k 1599 k = get pnext k 1600 if eq k 0 1601 pquit 60 pbad 1602 end 1603 count = ppositive k 1604 if lt start last 1605 pquit 60 pbad 1606 end 1607 pos = pxrange start count pos widths 1608 last = add start count 1609 k = get pnext k 1610 end 1611 end 1612 if ne pos get pdlen 0 1613 pquit 60 pbad 1614 end 1615 put pst 13 1 1616 ret n 1617 end 1618 fn pxchain off ulong to void 1619 while ne off 0 bound 1025 1620 pxvisit off 1621 put pst 3 0 1622 put pst 2 off 1623 code = ptoken 1624 d = ulong 0 1625 if and eq code 1 pword pXref 1626 d = pxclassic 1627 hybrid = pgetraw d kXRefStm 1628 if ne hybrid 0 1629 h = ppositive hybrid 1630 pxvisit h 1631 ignored = pxstream h 1632 end 1633 else 1634 d = pxstream off 1635 end 1636 prev = pgetraw d kPrev 1637 if eq prev 0 1638 ret 1639 end 1640 off = ppositive prev 1641 if eq off 0 1642 pquit 60 pbad 1643 end 1644 end 1645 pquit 63 pbound 1646 end 1647 note 7.5.5: locate only the final startxref/EOF sequence; never reconstruct a damaged xref. 1648 fn pfindxref to ulong 1649 z = get psize 0 1650 if lt z 20 1651 pquit 60 pbad 1652 end 1653 lo = ulong 0 1654 if gt z 65536 1655 lo = sub z 65536 1656 end 1657 put pst 2 lo 1658 c = pbyte 1659 pos = sub z len pStart 1660 while ge pos lo bound 65537 1661 put pst 2 pos 1662 match = true 1663 for j ulong len pStart bound 9 1664 if ne ptake ulong get pStart j 1665 match = false 1666 break 1667 end 1668 end 1669 if match 1670 v = pmreadint 1671 while pwhite pbyte bound 65537 1672 c = ptake 1673 end 1674 for j ulong len pEOF bound 5 1675 if ne ptake ulong get pEOF j 1676 pquit 60 pbad 1677 end 1678 end 1679 while lt get pst 2 z bound 65537 1680 if not pwhite ptake 1681 pquit 60 pbad 1682 end 1683 end 1684 ret v 1685 end 1686 if eq pos lo 1687 break 1688 end 1689 pos = sub pos 1 1690 end 1691 pquit 60 pbad 1692 ret 0 1693 end 1694 1695 note 7.7.3.2-7.7.3.4: depth-first page tree with effective inheritable attributes. 1696 fn ptpush node ulong depth ulong to void 1697 if ge depth 64 1698 pquit 63 pbound 1699 end 1700 node = pderef node 1701 if eq node 0 1702 pquit 60 pbad 1703 end 1704 if ne get ptype node 8 1705 pquit 60 pbad 1706 end 1707 if ne get pmark node 0 1708 pquit 63 pbound 1709 end 1710 put pmark node 1 1711 put ptnode depth node 1712 put ptkid depth 0 1713 put ptbase depth get pcount 0 1714 put ptexp depth 0 1715 put ptres depth 0 1716 put ptbox depth 0 1717 put ptcrop depth 0 1718 put ptrot depth 0 1719 if ne depth 0 1720 j = sub depth 1 1721 put ptres depth get ptres j 1722 put ptbox depth get ptbox j 1723 put ptcrop depth get ptcrop j 1724 put ptrot depth get ptrot j 1725 end 1726 v = pderef pget node kRes 1727 if ne v 0 1728 if ne get ptype v 8 1729 pquit 60 pbad 1730 end 1731 put ptres depth v 1732 end 1733 v = pderef pget node kMedia 1734 if ne v 0 1735 put ptbox depth v 1736 end 1737 v = pderef pget node kCrop 1738 if ne v 0 1739 put ptcrop depth v 1740 end 1741 v = pderef pget node kRotate 1742 if ne v 0 1743 rot = pint v 1744 if ne rem rot 90 0 1745 pquit 60 pbad 1746 end 1747 put ptrot depth rot 1748 end 1749 end 1750 fn ptboxout b ulong idx ulong to void 1751 k = parray b 1752 for j ulong 4 1753 if eq k 0 1754 pquit 60 pbad 1755 end 1756 put pbox add mul idx 4 j pnumber k 1757 k = get pnext k 1758 end 1759 if ne k 0 1760 pquit 60 pbad 1761 end 1762 end 1763 fn ptcontents page ulong to ulong 1764 n = pderef pget page kCont 1765 if eq n 0 1766 ret 0 1767 end 1768 if eq get ptype n 10 1769 ret 1 1770 end 1771 k = parray n 1772 cnt = ulong 0 1773 while ne k 0 bound 262145 1774 if ge cnt 262144 1775 pquit 63 pbound 1776 end 1777 v = pderef k 1778 if ne v 0 1779 if ne get ptype v 10 1780 pquit 60 pbad 1781 end 1782 end 1783 cnt = add cnt 1 1784 k = get pnext k 1785 end 1786 ret cnt 1787 end 1788 fn ptleaf depth ulong to void 1789 i = get pcount 0 1790 if ge i 4096 1791 pquit 63 pbound 1792 end 1793 n = get ptnode depth 1794 b = get ptbox depth 1795 if eq b 0 1796 pquit 60 pbad 1797 end 1798 ptboxout b i 1799 crop = get ptcrop depth 1800 if ne crop 0 1801 k = parray crop 1802 for j ulong 4 1803 if eq k 0 1804 pquit 60 pbad 1805 end 1806 val = pnumber k 1807 k = get pnext k 1808 end 1809 if ne k 0 1810 pquit 60 pbad 1811 end 1812 end 1813 put ppages i n 1814 put pres i get ptres depth 1815 put pcrop i crop 1816 put prot i get ptrot depth 1817 put pcont i ptcontents n 1818 put pcount 0 add i 1 1819 end 1820 fn ptree root ulong to void 1821 ptpush root 0 1822 depth = ulong 1 1823 steps = ulong 0 1824 while gt depth 0 bound 262145 1825 if ge steps 262144 1826 pquit 63 pbound 1827 end 1828 steps = add steps 1 1829 j = sub depth 1 1830 n = get ptnode j 1831 if eq get pmark n 1 1832 if pne pget n kType kPage 1833 ptleaf j 1834 put pmark n 2 1835 depth = j 1836 continue 1837 end 1838 if not pne pget n kType kPages 1839 pquit 60 pbad 1840 end 1841 expect = ppositive pget n kCount 1842 if gt expect 4096 1843 pquit 63 pbound 1844 end 1845 put ptexp j expect 1846 put ptkid j parray pget n kKids 1847 put pmark n 3 1848 end 1849 k = get ptkid j 1850 if eq k 0 1851 if ne sub get pcount 0 get ptbase j get ptexp j 1852 pquit 60 pbad 1853 end 1854 put pmark n 2 1855 depth = j 1856 continue 1857 end 1858 put ptkid j get pnext k 1859 if ne get ptype k 9 1860 pquit 60 pbad 1861 end 1862 ptpush k depth 1863 depth = add depth 1 1864 end 1865 end 1866 fn pclose to void 1867 if ne get pst 20 0 1868 r = fclose get pfd 0 1869 put pst 20 0 1870 if lt r 0 1871 pquit 64 piomsg 1872 end 1873 end 1874 end 1875 fn preset to void 1876 for i ulong 24 1877 put pst i 0 1878 end 1879 for i ulong 65536 bound 65536 1880 put pobjs i 0 1881 put pxt i 0 1882 put pxa i 0 1883 put pxb i 0 1884 put pxrev i 0 1885 put poload i 0 1886 put pmseen i 0 1887 end 1888 for i ulong 262144 bound 262144 1889 put pmark i 0 1890 end 1891 put proot 0 0 1892 put pinfo 0 0 1893 put pcount 0 0 1894 put pdlen 0 0 1895 end 1896 fn pheader to void 1897 put pst 2 0 1898 if ne ptake 37 1899 pquit 60 pbad 1900 end 1901 if ne ptake 80 1902 pquit 60 pbad 1903 end 1904 if ne ptake 68 1905 pquit 60 pbad 1906 end 1907 if ne ptake 70 1908 pquit 60 pbad 1909 end 1910 if ne ptake 45 1911 pquit 60 pbad 1912 end 1913 if ne ptake 49 1914 pquit 60 pbad 1915 end 1916 if ne ptake 46 1917 pquit 60 pbad 1918 end 1919 c = ptake 1920 if or lt c 48 gt c 55 1921 pquit 60 pbad 1922 end 1923 put pst 15 sub c 48 1924 if not pwhite pbyte 1925 pquit 60 pbad 1926 end 1927 end 1928 fn popen arg ulong to void 1929 pathbuf = new uchar 4096 0 1930 pclose 1931 preset 1932 r = argread arg pathbuf 0 4096 1933 if le r 0 1934 pquit 2 piomsg 1935 end 1936 fd = fopen pathbuf 0 ulong r 0 0 1937 if lt fd 0 1938 pquit 2 piomsg 1939 end 1940 put pfd 0 int fd 1941 put pst 20 1 1942 z = fseek int fd 0 2 1943 if lt z 0 1944 pquit 64 piomsg 1945 end 1946 put psize 0 ulong z 1947 pheader 1948 pxchain pfindxref 1949 if eq get pst 11 0 1950 pquit 60 pbad 1951 end 1952 root = pderef get proot 0 1953 if eq root 0 1954 pquit 60 pbad 1955 end 1956 if ne get ptype root 8 1957 pquit 60 pbad 1958 end 1959 put proot 0 root 1960 put pinfo 0 pderef get pinfo 0 1961 pcatver pget root kVersion 1962 ptree pget root kPages 1963 end 1964 note pcatver: the catalog's Version (7.7.2, a name) raises the effective minor version when it is a 1965 note later 1.x; a name of another form (for example 2.0) is not used, a value that is not a name is 60. 1966 fn pcatver v0 ulong to void 1967 if eq v0 0 1968 ret 1969 end 1970 v = pderef v0 1971 if ne get ptype v 5 1972 pquit 60 pbad 1973 end 1974 if ne get plen v 3 1975 ret 1976 end 1977 s = get pstart v 1978 if ne get pbytes s 49 1979 ret 1980 end 1981 if ne get pbytes add s 1 46 1982 ret 1983 end 1984 c = ulong get pbytes add s 2 1985 if or lt c 48 gt c 57 1986 ret 1987 end 1988 if gt sub c 48 get pst 15 1989 put pst 15 sub c 48 1990 end 1991 end 1992 fn pout textval view uchar * to void 1993 i = ulong 0 1994 n = len textval 1995 while lt i n bound `100000001` 1996 pwork 1997 r = write 1 textval i sub n i 1998 if le r 0 1999 pquit 64 piomsg 2000 end 2001 i = add i ulong r 2002 end 2003 end 2004 fn pdecimal n long to void 2005 b = new uchar 24 0 2006 z = tx_fdeci n b 0 2007 if lt z 0 2008 pquit 63 pbound 2009 end 2010 pout part b 0 ulong z 2011 end 2012 fn praw n ulong to void 2013 n = pderef n 2014 if eq n 0 2015 ret 2016 end 2017 if ne get ptype n 6 2018 pquit 60 pbad 2019 end 2020 s = get pstart n 2021 z = get plen n 2022 for i ulong z bound 65537 2023 prawb ulong get pbytes add s i 2024 end 2025 end 2026 note prawb: one string byte for info: printable ASCII as itself, a backslash as two backslashes, every 2027 note other byte (control bytes, UTF-16 and PDFDocEncoding bytes above 126) as \xHH, so the output is text. 2028 text phexd "0123456789ABCDEF" 2029 fn prawb c ulong to void 2030 b = new uchar 4 92 2031 k = ulong 1 2032 if and ge c 32 le c 126 2033 put b 0 uchar c 2034 if eq c 92 2035 k = 2 2036 end 2037 else 2038 put b 1 120 2039 put b 2 get phexd shr c 4 2040 put b 3 get phexd and c 15 2041 k = 4 2042 end 2043 pout part b 0 k 2044 end 2045 text pdot "." 2046 text pminus "-" 2047 text pspace " " 2048 note Deterministic display only: six fractional places, rounded, trailing zeroes omitted. 2049 note Writer uses the original number token in pstart/plen, never this display formatter. 2050 fn pfloat value f64 to void 2051 b = new uchar 6 48 2052 if or gt value `9000000000000`.0f64 lt value -`9000000000000`.0f64 2053 pquit 63 pbound 2054 end 2055 if ne value value 2056 pquit 60 pbad 2057 end 2058 v = fabs value 2059 scaled = long add mul v `1000000`.0f64 0.5f64 2060 if and lt value 0.0f64 ne scaled 0 2061 pout pminus 2062 end 2063 pdecimal div scaled `1000000` 2064 frac = rem scaled `1000000` 2065 if eq frac 0 2066 ret 2067 end 2068 pout pdot 2069 for i ulong 6 2070 put b sub ulong 5 i uchar add rem frac 10 48 2071 frac = div frac 10 2072 end 2073 z = ulong 6 2074 while gt z 0 bound 7 2075 if ne get b sub z 1 48 2076 break 2077 end 2078 z = sub z 1 2079 end 2080 pout part b 0 z 2081 end 2082 text piv "version=1." 2083 text pip "pages=" 2084 text pienc "encrypted=no\n" 2085 text pix "xref-streams=" 2086 text pios "object-streams=" 2087 text pyes "yes\n" 2088 text pno "no\n" 2089 text pieq "=" 2090 fn pmetadata key view uchar * to void 2091 pout key 2092 pout pieq 2093 praw pget get pinfo 0 key 2094 pout pnl 2095 end 2096 fn pinfoout to void 2097 pout piv 2098 pdecimal long get pst 15 2099 pout pnl 2100 pout pip 2101 pdecimal long get pcount 0 2102 pout pnl 2103 pout pienc 2104 pout pix 2105 if ne get pst 13 0 2106 pout pyes 2107 else 2108 pout pno 2109 end 2110 pout pios 2111 if ne get pst 14 0 2112 pout pyes 2113 else 2114 pout pno 2115 end 2116 pmetadata kTitle 2117 pmetadata kAuthor 2118 pmetadata kProd 2119 pmetadata kDate 2120 end 2121 text ppmb " MediaBox=" 2122 text pprot " Rotate=" 2123 text ppcont " Contents=" 2124 fn ppagesout to void 2125 for i ulong get pcount 0 bound 4096 2126 pdecimal long add i 1 2127 pout ppmb 2128 for j ulong 4 2129 if ne j 0 2130 pout pspace 2131 end 2132 pfloat get pbox add mul i 4 j 2133 end 2134 pout pprot 2135 pdecimal get prot i 2136 pout ppcont 2137 pdecimal long get pcont i 2138 pout pnl 2139 end 2140 end