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 / tests / mkcases.om

792 lines · 15392 bytes Raw Blame History
1om2
2note First-party Apache-2.0 PDF fixtures. Run from repository root.
3note ISO 32000-1: 7.3 objects, 7.4 filters, 7.5 xrefs/object streams/incremental updates,
4note 7.7 page trees, 9.4 text, 9.6 simple fonts, 9.7 composite fonts, 9.10 ToUnicode.
5note RFC1950 Adler-32 and RFC1951 stored blocks; all byte offsets calculated here.
6global ob uchar 1048576
7global nb ulong 1
8global cb uchar 262144
9global nc ulong 1
10global tmp uchar 262144
11global ofs ulong 256
12global typ ulong 256
13global val ulong 256
14global mx ulong 1
15global xp ulong 1
16global pv ulong 1
17global md ulong 1
18text hdr "%PDF-1.7\n%"
19text nl "\n"
20text oh " 0 obj\n"
21text oe "\nendobj\n"
22text xd "xref\n"
23text fr "0000000000 65535 f \n"
24text en " 00000 n \n"
25text tr "trailer\n<< /Size "
26text rt " /Root 1 0 R "
27text ep ">>\nstartxref\n"
28text eof "\n%%EOF\n"
29text prev " /Prev "
30text xs " /XRefStm "
31text enc " /Encrypt << /Filter /Standard /V 1 /R 2 /O (01234567890123456789012345678901) /U (01234567890123456789012345678901) /P -4 >> "
32text cat "<< /Type /Catalog /Pages 2 0 R >>"
33text pts "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"
34text pg "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>"
35text ft "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica"
36text win " /Encoding /WinAnsiEncoding >>"
37text mac " /Encoding /MacRomanEncoding >>"
38text std " >>"
39text dif " /Encoding << /Type /Encoding /Differences [65 /Aacute /fi /Lslash /adieresis /Euro] >> >>"
40text ff "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding /ToUnicode 6 0 R >>"
41text body "BT /F1 12 Tf 72 700 Td (Hello corpus) Tj ET\n"
42text words "BT /F1 12 Tf 72 700 Td <41424344452080e982> Tj ET\n"
43text stdw "BT /F1 12 Tf 72 700 Td <41272060ae20afb1e1fb> Tj ET\n"
44text difw "BT /F1 12 Tf 72 700 Td <4142434445> Tj ET\n"
45text opsx "BT /F1 12 Tf 14 TL 1 0 0 1 72 700 Tm (First) Tj 0 -20 Td (Second) Tj 0 -20 TD (Third) Tj T* (Fourth) Tj (Fifth) ' 0 0 (Sixth) \" T* [(wide) -900 (gap)] TJ ET\n"
46text cmhead "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /Test def\n/CMapType 2 def\n"
47text cmone "1 begincodespacerange\n<00> <ff>\nendcodespacerange\n3 beginbfchar\n<41> <0041>\n<42> <00660069>\n<43> <d83dde00>\nendbfchar\n2 beginbfrange\n<44> <45> <0064>\n<46> <48> [<0058> <0059> <005a>]\nendbfrange\n"
48text cmtwo "1 begincodespacerange\n<0000> <ffff>\nendcodespacerange\n2 beginbfchar\n<0001> <0048>\n<0002> <0069>\nendbfchar\n"
49text cmbad "1 begincodespacerange\n<00> <ff>\nendcodespacerange\n1 beginbfrange\n<41> <48> [<0041> <0042>]\nendbfrange\n"
50text cmend "endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n"
51text cmtext "BT /F1 12 Tf 72 700 Td (ABCDEFGH) Tj ET\n"
52text cidtext "BT /F1 12 Tf 72 700 Td <00010002> Tj ET\n"
53text cidfont "<< /Type /Font /Subtype /Type0 /BaseFont /HeiseiKakuGo-W5 /Encoding /Identity-H /DescendantFonts [7 0 R] /ToUnicode 6 0 R >>"
54text cidnou "<< /Type /Font /Subtype /Type0 /BaseFont /HeiseiKakuGo-W5 /Encoding /Identity-H /DescendantFonts [7 0 R] >>"
55text cidv "<< /Type /Font /Subtype /Type0 /BaseFont /HeiseiKakuGo-W5 /Encoding /Identity-V /DescendantFonts [7 0 R] /ToUnicode 6 0 R >>"
56text frl " /Filter /RunLengthDecode"
57text pgpi "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R /PieceInfo 6 0 R >>"
58text oh4 "4 0 "
59text extst "<< /Length 0 /F (external-content.bin) >>\nstream\n\nendstream"
60text cid "<< /Type /Font /Subtype /CIDFontType0 /BaseFont /HeiseiKakuGo-W5 /CIDSystemInfo << /Registry (Adobe) /Ordering (Japan1) /Supplement 2 >> /FontDescriptor 8 0 R /DW 1000 >>"
61text cidd "<< /Type /FontDescriptor /FontName /HeiseiKakuGo-W5 /Flags 4 /FontBBox [-250 -250 1100 1100] /ItalicAngle 0 /Ascent 880 /Descent -120 /CapHeight 700 /StemV 80 >>"
62text sl "<< /Length "
63text ss " >>\nstream\n"
64text se "\nendstream"
65text blank " "
66text fx " /Filter /FlateDecode"
67text fh " /Filter /ASCIIHexDecode"
68text fa " /Filter /ASCII85Decode"
69text fc " /Filter [/ASCII85Decode /FlateDecode]"
70text fl " /Filter /LZWDecode"
71text lr "<< /Length 6 0 R >>\nstream\n"
72text tree "<< /Type /Pages /Kids [6 0 R] /Count 2 /MediaBox [0 0 400 800] /Rotate 90 /Resources << /Font << /F1 4 0 R >> >> >>"
73text branch "<< /Type /Pages /Parent 2 0 R /Kids [3 0 R 7 0 R] /Count 2 >>"
74text leaf2 "<< /Type /Page /Parent 6 0 R /MediaBox [0 0 500 750] /Rotate 180 /Contents 5 0 R >>"
75text leaf "<< /Type /Page /Parent 6 0 R /Contents 5 0 R >>"
76text arrpg "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents [5 0 R 6 0 R] >>"
77text second "BT /F1 12 Tf 72 670 Td (Second stream) Tj ET\n"
78text imgpg "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> /XObject << /Im1 6 0 R >> >> /Contents 5 0 R >>"
79text image " /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode"
80text arbdata "Unused arbitrary JPEG bytes: text must not decode this resource."
81text xt " /Type /XRef /Size "
82text xw " /Root 1 0 R /W [1 4 2] /Filter /FlateDecode /DecodeParms << /Predictor 12 /Columns 7 >>"
83text ot " /Type /ObjStm /N 1 /First 4"
84text oh3 "3 0 "
85text upd "BT /F1 12 Tf 72 700 Td (Updated text) Tj ET\n"
86text cyc "<< /Type /Pages /Kids [2 0 R] /Count 1 >>"
87text lpre "<< /Type /Pages /Kids [3 0 R] /Count 1 /Unused /"
88text junk "<< /Unused "
89text close ">>"
90text gn "corpus/gen/"
91text rn "corpus/refuse/"
92text sd "corpus/s1refuse/"
93text names "inherit winansi macroman standard differences tounicode type0 textops lenref ahx a85 a85flate xrefstm objstm incr hybrid carray image lzw"
94text badnames "bad-startxref bad-xref-offset bad-object-number missing-endstream wrong-length encrypted kids-cycle prev-cycle objstm-self nesting name-long lzw-text runlength cid-no-tounicode identity-v bad-cmap w-objstm-ref w-external"
95text vend "e-empty.raw s-trunc.raw f-trunc.raw b-type3.raw s-nlen.raw d-manylen.raw d-manydist.raw d-clinc.raw d-clover.raw d-repfirst.raw d-reppast.raw d-litinc.raw d-litover.raw d-distinc.raw d-distover.raw d-noeob.raw f-lit286.raw f-dist30.raw f-far.raw zl-fcheck.zlib zl-cinfo.zlib zl-method.zlib zl-dict.zlib zl-adler.zlib zl-trunc.zlib"
96text vnames "v10-e-empty v10-s-trunc v10-f-trunc v11-b-type3 v12-s-nlen v13-d-manylen v13-d-manydist v14-d-clinc v14-d-clover v15-d-repfirst v15-d-reppast v16-d-litinc v16-d-litover v17-d-distinc v17-d-distover v18-d-noeob v19-f-lit286 v20-f-dist30 v21-f-far v30-zl-fcheck v30-zl-cinfo v30-zl-method v31-zl-dict v32-zl-adler v10-zl-trunc"
97text suffix ".pdf"
98text err "mkcases: I/O failure\n"
99text deep "<< /Type /Pages /Kids [3 0 R] /Count 1 /Resources 6 0 R >>"
100text xstart "startxref\n"
101fn die to void
102r = write 2 err 0 len err
103exit 1
104end
105fn byteout c ulong to void
106put ob get nb 0 uchar and c 255
107put nb 0 add get nb 0 1
108end
109fn emit t view uchar * to void
110for i ulong len t bound 262144
111byteout ulong get t i
112end
113end
114fn num n ulong width ulong to void
115b = new uchar 24 0
116p = ulong 24
117v = n
118while true bound 24
119p = sub p 1
120put b p uchar add rem v 10 48
121v = div v 10
122if and eq v 0 ge sub 24 p width
123break
124end
125end
126emit part b p sub 24 p
127end
128fn beginobj id ulong to void
129put ofs id get nb 0
130put typ id 1
131if ge id get mx 0
132put mx 0 add id 1
133end
134num id 0
135emit oh
136end
137fn object id ulong t view uchar * to void
138beginobj id
139emit t
140emit oe
141end
142fn reset to void
143put nb 0 0
144put nc 0 0
145put mx 0 1
146put pv 0 0
147put md 0 0
148for i ulong 256
149put ofs i 0
150put typ i 0
151put val i 0
152end
153emit hdr
154for i ulong 4
155byteout 200
156end
157emit nl
158end
159fn cbyte c ulong to void
160put cb get nc 0 uchar and c 255
161put nc 0 add get nc 0 1
162end
163fn content t view uchar * to void
164put nc 0 0
165for i ulong len t bound 262144
166cbyte ulong get t i
167end
168end
169fn catblob t view uchar * to void
170for i ulong len t bound 262144
171cbyte ulong get t i
172end
173end
174fn blobout to void
175for i ulong get nc 0 bound 262144
176byteout ulong get cb i
177end
178end
179fn stream id ulong extra view uchar * to void
180beginobj id
181emit sl
182num get nc 0 0
183emit extra
184emit ss
185blobout
186emit se
187emit oe
188end
189fn font t view uchar * to void
190beginobj 4
191emit ft
192emit t
193emit oe
194end
195fn base to void
196reset
197object 1 cat
198object 2 pts
199object 3 pg
200font win
201end
202fn table to void
203put xp 0 get nb 0
204emit xd
205m = get mx 0
206i = ulong 0
207while lt i m bound 256
208if eq get typ i 2
209i = add i 1
210continue
211end
212j = i
213while lt j m bound 256
214if eq get typ j 2
215break
216end
217j = add j 1
218end
219num i 0
220emit blank
221num sub j i 0
222emit nl
223for q ulong sub j i bound 256
224id = add i q
225if and ne id 0 eq get typ id 1
226num get ofs id 10
227emit en
228else
229emit fr
230end
231end
232i = j
233end
234end
235fn tailer extra view uchar * to void
236emit tr
237num get mx 0 0
238emit rt
239emit extra
240if ne get pv 0 0
241emit prev
242num get pv 0 0
243end
244emit ep
245num get xp 0 0
246emit eof
247end
248fn finish to void
249table
250tailer blank
251end
252fn word t view uchar * idx ulong dst edit uchar * pos ulong to ulong
253w = ulong 0
254p = pos
255inside = false
256for i ulong len t bound 4096
257c = get t i
258if eq c 32
259if inside
260w = add w 1
261end
262inside = false
263else
264inside = true
265if eq w idx
266put dst p c
267p = add p 1
268end
269end
270end
271ret p
272end
273fn save dir view uchar * list view uchar * idx ulong to void
274p = new uchar 256 0
275n = ulong 0
276for i ulong len dir bound 256
277put p n get dir i
278n = add n 1
279end
280n = word list idx p n
281for i ulong len suffix bound 8
282put p n get suffix i
283n = add n 1
284end
285fd = fopen p 0 n 577 420
286if lt fd 0
287die
288end
289off = ulong 0
290while lt off get nb 0 bound 1048576
291r = write int fd ob off sub get nb 0 off
292if le r 0
293die
294end
295off = add off ulong r
296end
297r = fclose int fd
298if lt r 0
299die
300end
301end
302fn snapshot to ulong
303n = get nc 0
304for i ulong n bound 262144
305put tmp i get cb i
306end
307put nc 0 0
308ret n
309end
310fn zlibstored to void
311n = snapshot
312cbyte 120
313cbyte 1
314a = ulong 1
315b = ulong 0
316p = ulong 0
317while lt p n bound 262144
318k = sub n p
319if gt k 65535
320k = 65535
321end
322final = ulong 0
323if eq add p k n
324final = 1
325end
326cbyte final
327cbyte k
328cbyte shr k 8
329cbyte xor k 65535
330cbyte shr xor k 65535 8
331for j ulong k bound 65535
332v = ulong get tmp add p j
333cbyte v
334a = rem add a v 65521
335b = rem add b a 65521
336end
337p = add p k
338end
339cbyte shr b 8
340cbyte b
341cbyte shr a 8
342cbyte a
343end
344fn hexencode to void
345n = snapshot
346for i ulong n bound 262144
347v = ulong get tmp i
348for j ulong 2
349d = and shr v mul sub 1 j 4 15
350if lt d 10
351cbyte add d 48
352else
353cbyte add d 55
354end
355end
356end
357cbyte 62
358end
359fn a85 to void
360b = new uchar 5 0
361n = snapshot
362p = ulong 0
363while lt p n bound 262144
364k = sub n p
365if gt k 4
366k = 4
367end
368v = ulong 0
369for i ulong 4
370v = mul v 256
371if lt i k
372v = add v ulong get tmp add p i
373end
374end
375for j ulong 5
376put b sub 4 j uchar add rem v 85 33
377v = div v 85
378end
379for j ulong add k 1 bound 5
380cbyte ulong get b j
381end
382p = add p k
383end
384cbyte 126
385cbyte 62
386end
387fn rle to void
388n = snapshot
389p = ulong 0
390while lt p n bound 262144
391k = sub n p
392if gt k 128
393k = 128
394end
395cbyte sub k 1
396for j ulong k bound 128
397cbyte ulong get tmp add p j
398end
399p = add p k
400end
401cbyte 128
402end
403fn lzw to void
404note Literal-only nine-bit LZW with Clear before 200 input bytes; fixture is shorter.
405n = snapshot
406bits = ulong 0
407count = ulong 0
408for i ulong add n 2 bound 262144
409v = ulong 256
410if gt i 0
411v = 257
412if le i n
413v = ulong get tmp sub i 1
414end
415end
416bits = or shl bits 9 v
417count = add count 9
418while ge count 8 bound 3
419count = sub count 8
420cbyte shr bits count
421end
422end
423if gt count 0
424cbyte shl bits sub 8 count
425end
426end
427fn cmap kind ulong to void
428content cmhead
429if eq kind 1
430catblob cmtwo
431end
432if eq kind 0
433catblob cmone
434end
435if eq kind 2
436catblob cmbad
437end
438catblob cmend
439stream 6 blank
440end
441fn xrefrows id ulong to void
442row = new uchar 7 0
443last = new uchar 7 0
444put ofs id get nb 0
445put typ id 1
446put mx 0 add id 1
447put nc 0 0
448for i ulong get mx 0 bound 256
449t = get typ i
450a = get ofs i
451b = ulong 0
452if eq i 0
453b = 65535
454end
455if eq t 2
456b = get val i
457end
458put row 0 uchar t
459for j ulong 4
460sh = mul sub 3 j 8
461v = and shr a sh 255
462put row add j 1 uchar v
463end
464put row 5 uchar shr b 8
465put row 6 uchar and b 255
466cbyte 2
467for j ulong 7
468cbyte sub ulong get row j ulong get last j
469put last j get row j
470end
471end
472zlibstored
473end
474fn xrefstream id ulong to void
475xrefrows id
476put xp 0 get nb 0
477beginobj id
478emit sl
479num get nc 0 0
480emit xt
481num get mx 0 0
482emit xw
483emit ss
484blobout
485emit se
486emit oe
487end
488fn xend to void
489emit xstart
490num get xp 0 0
491emit eof
492end
493fn gen k ulong to void
494base
495if eq k 0
496object 2 tree
497object 6 branch
498object 3 leaf
499object 7 leaf2
500end
501if eq k 2
502font mac
503end
504if eq k 3
505font std
506end
507if eq k 4
508font dif
509end
510content body
511if and ge k 1 le k 2
512content words
513end
514if eq k 3
515content stdw
516end
517if eq k 4
518content difw
519end
520if eq k 5
521object 4 ff
522cmap 0
523content cmtext
524end
525if eq k 6
526object 4 cidfont
527object 7 cid
528object 8 cidd
529cmap 1
530content cidtext
531end
532if eq k 7
533content opsx
534end
535if eq k 8
536beginobj 6
537num get nc 0 0
538emit oe
539beginobj 5
540emit lr
541blobout
542emit se
543emit oe
544else
545if eq k 9
546hexencode
547stream 5 fh
548else
549if eq k 10
550catblob blank
551a85
552stream 5 fa
553else
554if eq k 11
555zlibstored
556a85
557stream 5 fc
558else
559if eq k 18
560lzw
561stream 5 fl
562else
563stream 5 blank
564end
565end
566end
567end
568end
569if eq k 16
570object 3 arrpg
571content second
572stream 6 blank
573end
574if eq k 17
575object 3 imgpg
576content arbdata
577stream 6 image
578end
579if or eq k 13 eq k 15
580content oh3
581catblob pg
582stream 6 ot
583put typ 3 2
584put ofs 3 6
585put val 3 0
586end
587if or or eq k 12 eq k 13 eq k 15
588xrefstream 9
589if eq k 15
590old = get xp 0
591table
592emit tr
593num get mx 0 0
594emit rt
595emit xs
596num old 0
597emit ep
598num get xp 0 0
599emit eof
600else
601xend
602end
603else
604finish
605end
606if eq k 14
607put pv 0 get xp 0
608content upd
609stream 5 blank
610finish
611end
612save gn names k
613end
614fn bad k ulong to void
615base
616content body
617if and ge k 13 le k 15
618if eq k 13
619object 4 cidnou
620end
621if eq k 14
622object 4 cidv
623cmap 1
624end
625if eq k 15
626object 4 ff
627cmap 2
628content cmtext
629else
630object 7 cid
631object 8 cidd
632content cidtext
633end
634end
635if eq k 3
636beginobj 5
637emit sl
638num get nc 0 0
639emit ss
640blobout
641emit oe
642else
643if eq k 4
644beginobj 5
645emit sl
646num add get nc 0 9 0
647emit ss
648blobout
649emit se
650emit oe
651else
652if eq k 11
653lzw
654stream 5 fl
655else
656if eq k 12
657rle
658stream 5 frl
659else
660if eq k 17
661beginobj 5
662emit extst
663emit oe
664else
665stream 5 blank
666end
667end
668end
669end
670end
671if eq k 6
672object 2 cyc
673end
674if eq k 8
675content oh3
676catblob pg
677stream 6 ot
678put typ 3 2
679put ofs 3 6
680put val 3 0
681put typ 6 2
682put ofs 6 6
683xrefstream 9
684xend
685save rn badnames k
686ret
687end
688if eq k 16
689object 3 pgpi
690content oh4
691catblob ft
692catblob win
693stream 6 ot
694put typ 4 2
695put ofs 4 6
696put val 4 0
697xrefstream 9
698xend
699save rn badnames k
700ret
701end
702if eq k 9
703beginobj 6
704emit junk
705for i ulong 70
706byteout 91
707end
708byteout 48
709for i ulong 70
710byteout 93
711end
712emit close
713emit oe
714note Make the deeply nested object reachable from a required page attribute.
715object 2 deep
716end
717if eq k 10
718beginobj 2
719emit lpre
720for i ulong 300
721byteout 65
722end
723emit close
724emit oe
725end
726if eq k 1
727put ofs 3 add get ofs 3 1
728end
729if eq k 2
730put ob get ofs 3 57
731end
732table
733if eq k 0
734put xp 0 999999
735end
736if eq k 7
737put pv 0 get xp 0
738end
739if eq k 5
740tailer enc
741else
742tailer blank
743end
744save rn badnames k
745end
746fn vendor k ulong to void
747p = new uchar 256 0
748n = ulong 0
749for i ulong len sd bound 256
750put p n get sd i
751n = add n 1
752end
753n = word vend k p n
754fd = fopen p 0 n 0 0
755if lt fd 0
756die
757end
758base
759put nc 0 0
760if lt k 19
761cbyte 120
762cbyte 1
763end
764while true bound 262144
765r = read int fd cb get nc 0 sub 262144 get nc 0
766if lt r 0
767die
768end
769if eq r 0
770break
771end
772put nc 0 add get nc 0 ulong r
773end
774r = fclose int fd
775stream 5 fx
776finish
777save rn vnames k
778end
779fn main to int
780r = makedir gn 0 len gn 493
781r = makedir rn 0 len rn 493
782for i ulong 19
783gen i
784end
785for i ulong 18
786bad i
787end
788for i ulong 25
789vendor i
790end
791ret 0
792end