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 / writer.om

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