interplane.Get Omega
Read in your language

omega-pdf

Omega PDF: read and write PDF files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-pdf / src / core.om

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