interplane.Get Omega
Read in your language

omega-office

Omega office documents: read and write office files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-office / src / xml.om

2002 lines · 30998 bytes Raw Blame History
1note ======== src/xml.om: a streaming XML 1.0 (Fifth Edition) reader with Namespaces in XML 1.0 ========
2note Input arrives one byte at a time (xin), from a zip entry or a file, so a part of any size streams
3note through fixed buffers; nothing holds a whole part. Events go to the handler of the current mode:
4note hstart, hend and htext (src/main.om). Accepted: elements, attributes, character data, the five
5note predefined entities, character references, CDATA sections, comments and processing instructions
6note (both skipped), one XML declaration at the very start. Input is UTF-8 (an optional byte order mark)
7note or UTF-16 with a byte order mark (XML 1.0 section 4.3.3), turned into UTF-8 here. A document type
8note declaration is refused (ECMA-376 Part 2 6.2.5 b), so no entity other than the five predefined ones
9note can exist and nothing is ever expanded or fetched. Every bound refuses with its own exit code.
10
11const XNAME ulong 256
12const XATTR ulong 256
13const XAPOOL ulong 262144
14const XDEPTH ulong 1024
15const XBIND ulong 4096
16const XBPOOL ulong 262144
17const XREF ulong 32
18const XTBUF ulong 65536
19const XDECL ulong 512
20
21note states of the byte machine
22const ZTX ulong 0
23const ZLT ulong 1
24const ZSN ulong 2
25const ZSW ulong 3
26const ZSA ulong 4
27const ZAN ulong 5
28const ZAE ulong 6
29const ZAQ ulong 7
30const ZAV ulong 8
31const ZSE ulong 9
32const ZEN ulong 10
33const ZEW ulong 11
34const ZRF ulong 12
35const ZBG ulong 13
36const ZC1 ulong 14
37const ZCM ulong 15
38const ZCS ulong 16
39const ZCD ulong 17
40const ZDT ulong 18
41const ZPT ulong 19
42const ZPB ulong 20
43const ZXD ulong 21
44
45note xs[Q...]: the scalar state of the reader
46const QST ulong 0
47const QRET ulong 1
48const QDEP ulong 2
49const QROOT ulong 3
50const QNL ulong 4
51const QAC ulong 5
52const QAP ulong 6
53const QQ ulong 7
54const QRL ulong 8
55const QTL ulong 9
56const QCR ulong 10
57const QU8 ulong 11
58const QB1 ulong 12
59const QB2 ulong 13
60const QBC ulong 14
61const QBP ulong 15
62const QSP ulong 16
63const QPOS ulong 17
64const QK ulong 18
65const QENC ulong 19
66const QHB ulong 20
67const QHI ulong 21
68const QMODE ulong 22
69const QNB ulong 23
70const QBR ulong 24
71const QCP ulong 25
72const QCK ulong 26
73const QNF ulong 27
74const QEB ulong 28
75const QELO ulong 29
76const QDOK ulong 30
77const QU16 ulong 31
78const QDL ulong 32
79const QVO ulong 33
80const QVL ulong 34
81const QCB ulong 35
82const QCBG ulong 36
83const QGEN ulong 37
84
85note namespace ids the handlers compare (bid); NSOTH is any other URI
86const NSNO ulong 0
87const NSW ulong 1
88const NSS ulong 2
89const NSR ulong 3
90const NSPR ulong 4
91const NSCT ulong 5
92const NSMC ulong 6
93const NSXML ulong 7
94const NSOTH ulong 8
95note abind value of an attribute that is a namespace declaration (xmlns or xmlns:p)
96const BDECL ulong 18446744073709551614
97
98global xs ulong 40
99global xhb uchar 4
100note xname: the current element name; nbuf: an end-tag name or a PI target; xpi: the XML declaration
101global xname uchar 256
102global nbuf uchar 256
103global xpi uchar 512
104global rbuf uchar 32
105note tbuf: character data waiting for htext (flushed at markup and when full)
106global tbuf uchar 65536
107note attributes of the start tag being read: names and values in apool
108global apool uchar 262144
109global anoff ulong 256
110global anlen ulong 256
111global avoff ulong 256
112global avlen ulong 256
113global abind ulong 256
114global alo ulong 256
115note open elements: names in spool, the namespace binding and the local-name offset of each
116global spool uchar 262144
117global soff ulong 1024
118global slen ulong 1024
119global sbind ulong 1024
120global slo ulong 1024
121note namespace bindings in scope: prefix and URI bytes in bpool, the id, the depth that declared it
122global bpool uchar 262144
123global bpo ulong 4096
124global bpl ulong 4096
125global buo ulong 4096
126global bul ulong 4096
127global bid ulong 4096
128global bdep ulong 4096
129
130text kpre "officeread: "
131text kat " at byte "
132text kcdata "CDATA["
133text kdoct "OCTYPE"
134text kxmlns "xmlns"
135text kxml "xml"
136text kver "version"
137text kenc "encoding"
138text ksa "standalone"
139text k10 "1.0"
140text kutf8 "UTF-8"
141text kutf16 "UTF-16"
142text kyes "yes"
143text kno "no"
144text kamp "amp"
145text klt "lt"
146text kgt "gt"
147text kquot "quot"
148text kapos "apos"
149text unxml "http://www.w3.org/XML/1998/namespace"
150text unxmlns "http://www.w3.org/2000/xmlns/"
151text unw1 "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
152text unw2 "http://purl.oclc.org/ooxml/wordprocessingml/main"
153text uns1 "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
154text uns2 "http://purl.oclc.org/ooxml/spreadsheetml/main"
155text unr1 "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
156text unr2 "http://purl.oclc.org/ooxml/officeDocument/relationships"
157text unpr "http://schemas.openxmlformats.org/package/2006/relationships"
158text unct "http://schemas.openxmlformats.org/package/2006/content-types"
159text unmc "http://schemas.openxmlformats.org/markup-compatibility/2006"
160
161text e60 "not well-formed XML"
162text e61 "XML ends before the document is complete"
163text e62 "end tag does not match the start tag"
164text e63 "bad UTF-8 or UTF-16, or a character XML 1.0 does not allow"
165text e64 "XML declaration: encoding is not that of the input (UTF-8 or UTF-16) or version is not 1.0"
166text e65 "DOCTYPE refused: no DTDs, so no entities beyond the five predefined ones"
167text e66 "undefined entity, or a character reference to a character XML 1.0 does not allow"
168text e67 "namespace error: undeclared or reserved prefix, empty prefix binding, or a bad colon"
169text e68 "duplicate attribute"
170text e70 "name longer than 256 bytes"
171text e71 "more than 256 attributes on one element"
172text e72 "attributes of one start tag above 262144 bytes"
173text e73 "elements nested deeper than 1024"
174text e74 "more than 4095 namespace declarations in scope, or more than 262144 bytes of their prefixes and URIs"
175text e75 "entity or character reference longer than 32 bytes"
176
177note ---- refusals ----
178note xfail: one stderr line "officeread: MESSAGE at byte N", N counting the UTF-8 bytes of this part read so far.
179fn xfail c int m view uchar * to void
180b = new uchar 32 0
181e = tx_fdec get xs QPOS b 0
182warn kpre
183warn m
184warn kat
185r = write 2 b 0 ulong e
186warn tnl
187exit c
188end
189
190fn xbad to void
191xfail 60 e60
192end
193
194note ---- character classes ----
195fn isws c ulong to bool
196if eq c 32
197ret true
198end
199if eq c 10
200ret true
201end
202ret eq c 9
203end
204
205note nascii: an ASCII name character (XML 1.0 section 2.3 NameStartChar when first, else NameChar).
206fn nascii c ulong first bool to bool
207if and ge c 97 le c 122
208ret true
209end
210if and ge c 65 le c 90
211ret true
212end
213if or eq c 95 eq c 58
214ret true
215end
216if first
217ret false
218end
219if and ge c 48 le c 57
220ret true
221end
222ret or eq c 45 eq c 46
223end
224
225note ncpok: a non-ASCII code point in a name, the ranges of XML 1.0 Fifth Edition section 2.3.
226fn ncpok c ulong first bool to bool
227if and ge c 0xc0 le c 0xd6
228ret true
229end
230if and ge c 0xd8 le c 0xf6
231ret true
232end
233if and ge c 0xf8 le c 0x2ff
234ret true
235end
236if and ge c 0x370 le c 0x37d
237ret true
238end
239if and ge c 0x37f le c 0x1fff
240ret true
241end
242if and ge c 0x200c le c 0x200d
243ret true
244end
245if and ge c 0x2070 le c 0x218f
246ret true
247end
248if and ge c 0x2c00 le c 0x2fef
249ret true
250end
251if and ge c 0x3001 le c 0xd7ff
252ret true
253end
254if and ge c 0xf900 le c 0xfdcf
255ret true
256end
257if and ge c 0xfdf0 le c 0xfffd
258ret true
259end
260if and ge c 0x10000 le c 0xeffff
261ret true
262end
263if first
264ret false
265end
266if eq c 0xb7
267ret true
268end
269if and ge c 0x300 le c 0x36f
270ret true
271end
272ret and ge c 0x203f le c 0x2040
273end
274
275note xchar: a code point XML 1.0 section 2.2 allows (Char).
276fn xchar v ulong to bool
277if or eq v 9 or eq v 10 eq v 13
278ret true
279end
280if and ge v 0x20 le v 0xd7ff
281ret true
282end
283if and ge v 0xe000 le v 0xfffd
284ret true
285end
286ret and ge v 0x10000 le v 0x10ffff
287end
288
289note ---- names: each byte is checked as it arrives (the code point is rebuilt from UTF-8) ----
290fn nreset to void
291put xs QCP 0
292put xs QCK 0
293put xs QNF 0
294end
295
296fn nchk c ulong to void
297k = get xs QCK
298f = eq get xs QNF 0
299if eq k 0
300if lt c 0x80
301if not nascii c f
302xbad
303end
304put xs QNF 1
305ret
306end
307if lt c 0xe0
308put xs QCP and c 0x1f
309put xs QCK 1
310ret
311end
312if lt c 0xf0
313put xs QCP and c 0x0f
314put xs QCK 2
315ret
316end
317put xs QCP and c 0x07
318put xs QCK 3
319ret
320end
321p = or shl get xs QCP 6 and c 0x3f
322put xs QCP p
323k = sub k 1
324put xs QCK k
325if eq k 0
326if not ncpok p f
327xbad
328end
329put xs QNF 1
330end
331end
332
333fn xnput c ulong to void
334n = get xs QNL
335if eq n XNAME
336xfail 70 e70
337end
338put xname n uchar c
339put xs QNL add n 1
340end
341
342fn nbput c ulong to void
343n = get xs QNB
344if eq n XNAME
345xfail 70 e70
346end
347put nbuf n uchar c
348put xs QNB add n 1
349end
350
351fn apput c ulong to void
352p = get xs QAP
353if eq p XAPOOL
354xfail 72 e72
355end
356put apool p uchar c
357put xs QAP add p 1
358end
359
360note ---- character data ----
361fn tflush to void
362if eq get xs QTL 0
363ret
364end
365htext
366put xs QTL 0
367end
368
369fn tput c ulong to void
370n = get xs QTL
371put tbuf n uchar c
372n = add n 1
373put xs QTL n
374if eq n XTBUF
375tflush
376end
377end
378
379note rput: a decoded byte of a reference goes to the attribute value or to the text it was in.
380fn rput c ulong to void
381if eq get xs QRET ZAV
382apput c
383ret
384end
385tput c
386end
387
388note rcp: code point v as UTF-8 through rput.
389fn rcp v ulong to void
390if lt v 0x80
391rput v
392ret
393end
394if lt v 0x800
395rput or 0xc0 shr v 6
396rput or 0x80 and v 0x3f
397ret
398end
399if lt v 0x10000
400rput or 0xe0 shr v 12
401rput or 0x80 and shr v 6 0x3f
402rput or 0x80 and v 0x3f
403ret
404end
405rput or 0xf0 shr v 18
406rput or 0x80 and shr v 12 0x3f
407rput or 0x80 and shr v 6 0x3f
408rput or 0x80 and v 0x3f
409end
410
411note ---- references (XML 1.0 sections 4.1 and 4.6) ----
412note rbis: the reference body rbuf[0 .. n) equals t.
413fn rbis n ulong t view uchar * to bool
414if ne n len t
415ret false
416end
417for i ulong n bound 32
418if ne get rbuf i get t i
419ret false
420end
421end
422ret true
423end
424
425note xcref: the value of a character reference &#N; or &#xH; (body rbuf[0 .. n), n >= 1, rbuf[0] = '#').
426fn xcref n ulong to ulong
427i = ulong 1
428b = ulong 10
429if gt n 1
430if eq get rbuf 1 120
431i = 2
432b = 16
433end
434end
435if ge i n
436xbad
437ret 0
438end
439v = ulong 0
440while lt i n bound 32
441c = get rbuf i
442d = tx_hexv c
443if ge d b
444xbad
445end
446v = add mul v b d
447if gt v 0x10ffff
448v = 0x110000
449end
450i = add i 1
451end
452if not xchar v
453xfail 66 e66
454end
455ret v
456end
457
458fn xref to void
459n = get xs QRL
460if eq n 0
461xbad
462end
463if eq get rbuf 0 35
464rcp xcref n
465ret
466end
467if rbis n kamp
468rput 38
469ret
470end
471if rbis n klt
472rput 60
473ret
474end
475if rbis n kgt
476rput 62
477ret
478end
479if rbis n kquot
480rput 34
481ret
482end
483if rbis n kapos
484rput 39
485ret
486end
487xfail 66 e66
488end
489
490fn zrf c ulong to void
491if eq c 59
492xref
493put xs QST get xs QRET
494ret
495end
496n = get xs QRL
497if eq n XREF
498xfail 75 e75
499end
500if lt c 0x80
501if eq n 0
502if not or eq c 35 nascii c true
503xbad
504end
505else
506if not nascii c false
507xbad
508end
509end
510end
511put rbuf n uchar c
512put xs QRL add n 1
513end
514
515note ---- namespaces (Namespaces in XML 1.0, sections 3 to 6) ----
516note apeq: apool[o .. o+n) equals t.
517fn apeq o ulong n ulong t view uchar * to bool
518if ne n len t
519ret false
520end
521for i ulong n bound 262144
522if ne get apool add o i get t i
523ret false
524end
525end
526ret true
527end
528
529note bpeq: bpool[o .. o+n) equals t.
530fn bpeq o ulong n ulong t view uchar * to bool
531if ne n len t
532ret false
533end
534for i ulong n bound 262144
535if ne get bpool add o i get t i
536ret false
537end
538end
539ret true
540end
541
542note nsclass: the id of the URI bpool[o .. o+n). Transitional and Strict URIs of one vocabulary share an id.
543fn nsclass o ulong n ulong to ulong
544if or bpeq o n unw1 bpeq o n unw2
545ret NSW
546end
547if or bpeq o n uns1 bpeq o n uns2
548ret NSS
549end
550if or bpeq o n unr1 bpeq o n unr2
551ret NSR
552end
553if bpeq o n unpr
554ret NSPR
555end
556if bpeq o n unct
557ret NSCT
558end
559if bpeq o n unmc
560ret NSMC
561end
562if bpeq o n unxml
563ret NSXML
564end
565ret NSOTH
566end
567
568note isdecl: attribute i is "xmlns" (1), "xmlns:..." (2) or neither (0).
569fn isdecl i ulong to ulong
570o = get anoff i
571n = get anlen i
572if lt n 5
573ret 0
574end
575if not apeq o 5 kxmlns
576ret 0
577end
578if eq n 5
579ret 1
580end
581if eq get apool add o 5 58
582ret 2
583end
584ret 0
585end
586
587fn bcopy o ulong n ulong to ulong
588p = get xs QBP
589if gt n sub XBPOOL p
590xfail 74 e74
591end
592for i ulong n bound 262144
593put bpool add p i get apool add o i
594end
595put xs QBP add p n
596ret p
597end
598
599note bpush: the declaration in attribute i (k = 1 default, 2 prefixed) is in scope from this element on.
600fn bpush i ulong k ulong to void
601c = get xs QBC
602if eq c XBIND
603xfail 74 e74
604end
605o = get anoff i
606po = o
607pl = ulong 0
608if eq k 2
609po = add o 6
610pl = sub get anlen i 6
611end
612vl = get avlen i
613put bpo c bcopy po pl
614put bpl c pl
615put buo c bcopy get avoff i vl
616put bul c vl
617put bid c nsclass get buo c vl
618put bdep c add get xs QDEP 1
619put xs QBC add c 1
620put xs QGEN add get xs QGEN 1
621bcheck c k
622end
623
624note bcheck: the rules of Namespaces in XML 1.0 section 3 for binding c.
625fn bcheck c ulong k ulong to void
626po = get bpo c
627pl = get bpl c
628uo = get buo c
629ul = get bul c
630x = bpeq uo ul unxml
631y = bpeq uo ul unxmlns
632if eq k 1
633if or x y
634xfail 67 e67
635end
636ret
637end
638if eq pl 0
639xfail 67 e67
640end
641for i ulong pl bound 256
642if eq get bpool add po i 58
643xfail 67 e67
644end
645end
646if bpeq po pl kxmlns
647xfail 67 e67
648end
649if bpeq po pl kxml
650if not x
651xfail 67 e67
652end
653ret
654end
655if or x y
656xfail 67 e67
657end
658if eq ul 0
659xfail 67 e67
660end
661end
662
663note bseed: the prefix xml is bound to its namespace from the start (binding 0, never removed).
664fn bseed to void
665for i ulong 3
666put bpool i get kxml i
667end
668n = len unxml
669for i ulong n bound 64
670put bpool add 3 i get unxml i
671end
672put bpo 0 0
673put bpl 0 3
674put buo 0 3
675put bul 0 n
676put bid 0 NSXML
677put bdep 0 0
678put xs QBC 1
679put xs QBP add 3 n
680end
681
682fn bpop d ulong to void
683c = get xs QBC
684k = c
685while gt c 1 bound 4096
686if ne get bdep sub c 1 d
687break
688end
689c = sub c 1
690put xs QBP get bpo c
691end
692if ne c k
693put xs QBC c
694put xs QGEN add get xs QGEN 1
695end
696end
697
698note bxeq: binding b has the prefix xname[0 .. pl).
699fn bxeq b ulong pl ulong to bool
700if ne get bpl b pl
701ret false
702end
703o = get bpo b
704for i ulong pl bound 256
705if ne get bpool add o i get xname i
706ret false
707end
708end
709ret true
710end
711
712note baeq: binding b has the prefix apool[ao .. ao+pl).
713fn baeq b ulong ao ulong pl ulong to bool
714if ne get bpl b pl
715ret false
716end
717o = get bpo b
718for i ulong pl bound 256
719if ne get bpool add o i get apool add ao i
720ret false
721end
722end
723ret true
724end
725
726note bfindx: the binding of prefix xname[0 .. pl) (pl = 0: the default namespace), or txnone. The last
727note binding found is tried first while no binding was added or removed since (QGEN unchanged).
728fn bfindx pl ulong to ulong
729if eq get xs QCBG get xs QGEN
730b = get xs QCB
731if bxeq b pl
732ret b
733end
734end
735c = get xs QBC
736while gt c 0 bound 4096
737c = sub c 1
738if bxeq c pl
739put xs QCB c
740put xs QCBG get xs QGEN
741ret c
742end
743end
744ret txnone
745end
746
747note bfinda: the binding of prefix apool[ao .. ao+pl), or txnone (the same one-entry memory as bfindx).
748fn bfinda ao ulong pl ulong to ulong
749if eq get xs QCBG get xs QGEN
750b = get xs QCB
751if baeq b ao pl
752ret b
753end
754end
755c = get xs QBC
756while gt c 0 bound 4096
757c = sub c 1
758if baeq c ao pl
759put xs QCB c
760put xs QCBG get xs QGEN
761ret c
762end
763end
764ret txnone
765end
766
767note xcolon: the index of the one colon in xname, or txnone; a second colon, one at either end, or a local
768note part that does not start with a NameStartChar is 67.
769fn xcolon to ulong
770n = get xs QNL
771c = txnone
772for i ulong n bound 256
773if eq get xname i 58
774if ne c txnone
775xfail 67 e67
776end
777c = i
778end
779end
780if ne c txnone
781if or eq c 0 eq c sub n 1
782xfail 67 e67
783end
784if not lcstart 0 add c 1
785xfail 67 e67
786end
787end
788ret c
789end
790
791fn acolon o ulong n ulong to ulong
792c = txnone
793for i ulong n bound 256
794if eq get apool add o i 58
795if ne c txnone
796xfail 67 e67
797end
798c = i
799end
800end
801if ne c txnone
802if or eq c 0 eq c sub n 1
803xfail 67 e67
804end
805if not lcstart 1 add o add c 1
806xfail 67 e67
807end
808end
809ret c
810end
811
812note lcbyte: byte i of the name in xname (k 0) or in apool (k 1).
813fn lcbyte k ulong i ulong to ulong
814if eq k 0
815ret ulong get xname i
816end
817ret ulong get apool i
818end
819
820note lcstart: the character at byte i of that name, right after its colon, is a NameStartChar. Namespaces in
821note XML 1.0 (sections 3 and 4): a prefix and a local part are each an NCName, so "p:1x", "p:-x", "p:.x" and
822note "p:" U+00B7 "x" are not namespace-well-formed although each is an XML 1.0 Name. The bytes were already
823note checked as UTF-8 NameChars, so the decode needs no checks of its own.
824fn lcstart k ulong i ulong to bool
825b = lcbyte k i
826if lt b 0x80
827ret nascii b true
828end
829p = and b 0x07
830m = ulong 3
831if lt b 0xf0
832p = and b 0x0f
833m = 2
834end
835if lt b 0xe0
836p = and b 0x1f
837m = 1
838end
839for j ulong m bound 3
840p = or shl p 6 and lcbyte k add i add j 1 0x3f
841end
842ret ncpok p true
843end
844
845note elres: the element's namespace (QEB, a binding or txnone) and local name start (QELO).
846fn elres to void
847c = xcolon
848if eq c txnone
849b = bfindx 0
850if ne b txnone
851if eq get bul b 0
852b = txnone
853end
854end
855put xs QEB b
856put xs QELO 0
857ret
858end
859if eq c 5
860x = true
861for i ulong 5
862if ne get xname i get kxmlns i
863x = false
864end
865end
866if x
867xfail 67 e67
868end
869end
870b = bfindx c
871if eq b txnone
872xfail 67 e67
873end
874put xs QEB b
875put xs QELO add c 1
876end
877
878fn atone i ulong to void
879d = isdecl i
880if ne d 0
881if and eq d 2 gt get anlen i 6
882if not lcstart 1 add get anoff i 6
883xfail 67 e67
884end
885end
886put abind i BDECL
887put alo i 0
888ret
889end
890o = get anoff i
891c = acolon o get anlen i
892if eq c txnone
893put abind i txnone
894put alo i 0
895ret
896end
897b = bfinda o c
898if eq b txnone
899xfail 67 e67
900end
901put abind i b
902put alo i add c 1
903end
904
905fn apsame o1 ulong n1 ulong o2 ulong n2 ulong to bool
906if ne n1 n2
907ret false
908end
909for i ulong n1 bound 262144
910if ne get apool add o1 i get apool add o2 i
911ret false
912end
913end
914ret true
915end
916
917fn bpsame b1 ulong b2 ulong to bool
918if eq b1 b2
919ret true
920end
921n = get bul b1
922if ne n get bul b2
923ret false
924end
925o1 = get buo b1
926o2 = get buo b2
927for i ulong n bound 262144
928if ne get bpool add o1 i get bpool add o2 i
929ret false
930end
931end
932ret true
933end
934
935note adup: attributes i and j have the same name, or (both not declarations, both prefixed) the same
936note namespace URI and local name (XML 1.0 WFC Unique Att Spec; Namespaces in XML 1.0 section 6.3).
937fn adup i ulong j ulong to bool
938if apsame get anoff i get anlen i get anoff j get anlen j
939ret true
940end
941bi = get abind i
942bj = get abind j
943if or eq bi BDECL eq bj BDECL
944ret false
945end
946if or eq bi txnone eq bj txnone
947ret false
948end
949if not bpsame bi bj
950ret false
951end
952li = get alo i
953lj = get alo j
954oi = add get anoff i li
955ni = sub get anlen i li
956oj = add get anoff j lj
957nj = sub get anlen j lj
958ret apsame oi ni oj nj
959end
960
961fn dupchk to void
962n = get xs QAC
963for i ulong n bound 256
964j = add i 1
965while lt j n bound 256
966if adup i j
967xfail 68 e68
968end
969j = add j 1
970end
971end
972end
973
974note ---- tags ----
975fn spush to void
976d = get xs QDEP
977o = get xs QSP
978n = get xs QNL
979for i ulong n bound 256
980put spool add o i get xname i
981end
982put soff d o
983put slen d n
984put sbind d get xs QEB
985put slo d get xs QELO
986put xs QSP add o n
987end
988
989note eclose: the end of the innermost element (its name and namespace are in xname, QEB and QELO).
990fn eclose to void
991hend
992d = get xs QDEP
993bpop d
994d = sub d 1
995put xs QDEP d
996put xs QSP get soff d
997if eq d 0
998put xs QROOT 2
999end
1000end
1001
1002fn stagend em bool to void
1003if eq get xs QDEP XDEPTH
1004xfail 73 e73
1005end
1006n = get xs QAC
1007for i ulong n bound 256
1008k = isdecl i
1009if ne k 0
1010bpush i k
1011end
1012end
1013elres
1014for i ulong n bound 256
1015atone i
1016end
1017dupchk
1018spush
1019put xs QDEP add get xs QDEP 1
1020put xs QROOT 1
1021hstart
1022if em
1023eclose
1024end
1025put xs QST ZTX
1026end
1027
1028fn etag to void
1029d = sub get xs QDEP 1
1030o = get soff d
1031n = get slen d
1032if ne n get xs QNB
1033xfail 62 e62
1034end
1035for i ulong n bound 256
1036c = get nbuf i
1037if ne c get spool add o i
1038xfail 62 e62
1039end
1040put xname i c
1041end
1042put xs QNL n
1043put xs QEB get sbind d
1044put xs QELO get slo d
1045eclose
1046put xs QST ZTX
1047end
1048
1049note ---- the XML declaration (XML 1.0 section 2.8 and 4.3.3), body xpi[0 .. n) ----
1050fn dws p ulong n ulong to ulong
1051q = p
1052while lt q n bound 512
1053if not isws ulong get xpi q
1054ret q
1055end
1056q = add q 1
1057end
1058ret q
1059end
1060
1061note dkey: xpi[p ..] starts with t: the index after it, else txnone.
1062fn dkey p ulong n ulong t view uchar * to ulong
1063k = len t
1064if gt k sub n p
1065ret txnone
1066end
1067for i ulong k bound 64
1068if ne get xpi add p i get t i
1069ret txnone
1070end
1071end
1072ret add p k
1073end
1074
1075note dval: Eq and a quoted value after p; the value is xpi[QVO .. QVO+QVL); returns the index after it.
1076fn dval p ulong n ulong to ulong
1077q = dws p n
1078if ge q n
1079xbad
1080end
1081if ne get xpi q 61
1082xbad
1083end
1084q = dws add q 1 n
1085if ge q n
1086xbad
1087end
1088c = get xpi q
1089if and ne c 34 ne c 39
1090xbad
1091end
1092s = add q 1
1093e = s
1094while lt e n bound 512
1095if eq get xpi e c
1096put xs QVO s
1097put xs QVL sub e s
1098ret add e 1
1099end
1100e = add e 1
1101end
1102xbad
1103ret 0
1104end
1105
1106fn lc c ulong to ulong
1107if and ge c 65 le c 90
1108ret add c 32
1109end
1110ret c
1111end
1112
1113note dvis: the value is t (ci: ASCII case-insensitive).
1114fn dvis t view uchar * ci bool to bool
1115o = get xs QVO
1116n = get xs QVL
1117if ne n len t
1118ret false
1119end
1120for i ulong n bound 512
1121a = ulong get xpi add o i
1122b = ulong get t i
1123if ci
1124a = lc a
1125b = lc b
1126end
1127if ne a b
1128ret false
1129end
1130end
1131ret true
1132end
1133
1134fn xdecl to void
1135n = sub get xs QDL 1
1136p = dws 0 n
1137q = dkey p n kver
1138if eq q txnone
1139xbad
1140end
1141p = dval q n
1142if not dvis k10 false
1143xfail 64 e64
1144end
1145q = dws p n
1146if eq q n
1147ret
1148end
1149if eq q p
1150xbad
1151end
1152r = dkey q n kenc
1153if ne r txnone
1154p = dval r n
1155if eq get xs QENC 1
1156if not dvis kutf8 true
1157xfail 64 e64
1158end
1159else
1160if not dvis kutf16 true
1161xfail 64 e64
1162end
1163end
1164q = dws p n
1165if eq q n
1166ret
1167end
1168if eq q p
1169xbad
1170end
1171end
1172r = dkey q n ksa
1173if eq r txnone
1174xbad
1175end
1176p = dval r n
1177if not or dvis kyes false dvis kno false
1178xbad
1179end
1180if ne dws p n n
1181xbad
1182end
1183end
1184
1185note ---- the byte machine (one function per state) ----
1186fn ztx c ulong to void
1187if eq c 60
1188tflush
1189put xs QBR 0
1190put xs QST ZLT
1191ret
1192end
1193if eq c 38
1194if eq get xs QDEP 0
1195xbad
1196end
1197put xs QRET ZTX
1198put xs QRL 0
1199put xs QBR 0
1200put xs QST ZRF
1201ret
1202end
1203if eq get xs QDEP 0
1204if not isws c
1205xbad
1206end
1207ret
1208end
1209if eq c 93
1210put xs QBR add get xs QBR 1
1211tput c
1212ret
1213end
1214if eq c 62
1215if ge get xs QBR 2
1216xbad
1217end
1218end
1219put xs QBR 0
1220tput c
1221end
1222
1223fn zlt c ulong to void
1224if eq c 47
1225if eq get xs QDEP 0
1226xbad
1227end
1228put xs QNB 0
1229nreset
1230put xs QST ZEN
1231ret
1232end
1233if eq c 33
1234put xs QST ZBG
1235ret
1236end
1237if eq c 63
1238put xs QDOK 0
1239if eq get xs QPOS 2
1240put xs QDOK 1
1241end
1242put xs QNB 0
1243nreset
1244put xs QST ZPT
1245ret
1246end
1247if eq get xs QROOT 2
1248xbad
1249end
1250nreset
1251nchk c
1252put xs QNL 0
1253put xs QAC 0
1254put xs QAP 0
1255xnput c
1256put xs QST ZSN
1257end
1258
1259fn zsn c ulong to void
1260if isws c
1261put xs QST ZSW
1262ret
1263end
1264if eq c 62
1265stagend false
1266ret
1267end
1268if eq c 47
1269put xs QST ZSE
1270ret
1271end
1272nchk c
1273xnput c
1274end
1275
1276fn zsw c ulong to void
1277if isws c
1278ret
1279end
1280if eq c 62
1281stagend false
1282ret
1283end
1284if eq c 47
1285put xs QST ZSE
1286ret
1287end
1288k = get xs QAC
1289if eq k XATTR
1290xfail 71 e71
1291end
1292nreset
1293nchk c
1294put anoff k get xs QAP
1295apput c
1296put xs QST ZAN
1297end
1298
1299fn zan c ulong to void
1300k = get xs QAC
1301if isws c
1302put anlen k sub get xs QAP get anoff k
1303put xs QST ZAE
1304ret
1305end
1306if eq c 61
1307put anlen k sub get xs QAP get anoff k
1308put xs QST ZAQ
1309ret
1310end
1311nchk c
1312if eq sub get xs QAP get anoff k XNAME
1313xfail 70 e70
1314end
1315apput c
1316end
1317
1318fn zae c ulong to void
1319if isws c
1320ret
1321end
1322if eq c 61
1323put xs QST ZAQ
1324ret
1325end
1326xbad
1327end
1328
1329fn zaq c ulong to void
1330if isws c
1331ret
1332end
1333if or eq c 34 eq c 39
1334put xs QQ c
1335put avoff get xs QAC get xs QAP
1336put xs QST ZAV
1337ret
1338end
1339xbad
1340end
1341
1342note zav: attribute value; a literal tab or line end becomes a space (XML 1.0 section 3.3.3).
1343fn zav c ulong to void
1344if eq c get xs QQ
1345k = get xs QAC
1346put avlen k sub get xs QAP get avoff k
1347put xs QAC add k 1
1348put xs QST ZSA
1349ret
1350end
1351if eq c 60
1352xbad
1353end
1354if eq c 38
1355put xs QRET ZAV
1356put xs QRL 0
1357put xs QST ZRF
1358ret
1359end
1360if or eq c 9 eq c 10
1361apput 32
1362ret
1363end
1364apput c
1365end
1366
1367fn zsa c ulong to void
1368if isws c
1369put xs QST ZSW
1370ret
1371end
1372if eq c 62
1373stagend false
1374ret
1375end
1376if eq c 47
1377put xs QST ZSE
1378ret
1379end
1380xbad
1381end
1382
1383fn zse c ulong to void
1384if eq c 62
1385stagend true
1386ret
1387end
1388xbad
1389end
1390
1391fn zen c ulong to void
1392if or isws c eq c 62
1393if eq get xs QNB 0
1394xbad
1395end
1396if eq c 62
1397etag
1398ret
1399end
1400put xs QST ZEW
1401ret
1402end
1403nchk c
1404nbput c
1405end
1406
1407fn zew c ulong to void
1408if isws c
1409ret
1410end
1411if eq c 62
1412etag
1413ret
1414end
1415xbad
1416end
1417
1418fn zbg c ulong to void
1419if eq c 45
1420put xs QST ZC1
1421ret
1422end
1423if eq c 91
1424if eq get xs QDEP 0
1425xbad
1426end
1427put xs QK 0
1428put xs QST ZCS
1429ret
1430end
1431if eq c 68
1432put xs QK 0
1433put xs QST ZDT
1434ret
1435end
1436xbad
1437end
1438
1439fn zc1 c ulong to void
1440if eq c 45
1441put xs QK 0
1442put xs QST ZCM
1443ret
1444end
1445xbad
1446end
1447
1448note zcm: comment body; "--" must be followed by ">" (XML 1.0 section 2.5).
1449fn zcm c ulong to void
1450k = get xs QK
1451if eq c 45
1452if eq k 2
1453xbad
1454end
1455put xs QK add k 1
1456ret
1457end
1458if eq k 2
1459if eq c 62
1460put xs QST ZTX
1461ret
1462end
1463xbad
1464end
1465put xs QK 0
1466end
1467
1468fn zcs c ulong to void
1469k = get xs QK
1470if ne c ulong get kcdata k
1471xbad
1472end
1473k = add k 1
1474if eq k 6
1475put xs QK 0
1476put xs QST ZCD
1477ret
1478end
1479put xs QK k
1480end
1481
1482note zcd: CDATA section body; QK counts the "]" held back in case "]]>" follows.
1483fn zcd c ulong to void
1484k = get xs QK
1485if eq c 93
1486if eq k 2
1487tput 93
1488ret
1489end
1490put xs QK add k 1
1491ret
1492end
1493if and eq c 62 eq k 2
1494put xs QK 0
1495put xs QST ZTX
1496ret
1497end
1498for i ulong k bound 2
1499tput 93
1500end
1501put xs QK 0
1502tput c
1503end
1504
1505fn zdt c ulong to void
1506k = get xs QK
1507if ne c ulong get kdoct k
1508xbad
1509end
1510k = add k 1
1511if eq k 6
1512xfail 65 e65
1513end
1514put xs QK k
1515end
1516
1517note pikind: the PI target nbuf[0 .. n) is exactly "xml" (1), another case of x, m, l (2), or neither (0).
1518fn pikind n ulong to ulong
1519if ne n 3
1520ret 0
1521end
1522x = true
1523y = true
1524for i ulong 3
1525c = ulong get nbuf i
1526k = ulong get kxml i
1527if ne c k
1528x = false
1529end
1530if ne lc c k
1531y = false
1532end
1533end
1534if x
1535ret 1
1536end
1537if y
1538ret 2
1539end
1540ret 0
1541end
1542
1543fn zpt c ulong to void
1544if or isws c eq c 63
1545n = get xs QNB
1546if eq n 0
1547xbad
1548end
1549t = pikind n
1550if eq t 1
1551if eq get xs QDOK 0
1552xbad
1553end
1554if eq c 63
1555xbad
1556end
1557put xs QDL 0
1558put xs QK 0
1559put xs QST ZXD
1560ret
1561end
1562if eq t 2
1563xbad
1564end
1565put xs QK 0
1566if eq c 63
1567put xs QK 1
1568end
1569put xs QST ZPB
1570ret
1571end
1572nchk c
1573nbput c
1574end
1575
1576fn zpb c ulong to void
1577if and eq c 62 eq get xs QK 1
1578put xs QST ZTX
1579ret
1580end
1581put xs QK 0
1582if eq c 63
1583put xs QK 1
1584end
1585end
1586
1587fn zxd c ulong to void
1588if and eq c 62 eq get xs QK 1
1589xdecl
1590put xs QST ZTX
1591ret
1592end
1593put xs QK 0
1594if eq c 63
1595put xs QK 1
1596end
1597n = get xs QDL
1598if eq n XDECL
1599xbad
1600end
1601put xpi n uchar c
1602put xs QDL add n 1
1603end
1604
1605note xbyte: one byte of normalized UTF-8 (line ends already LF) through the state machine.
1606fn xbyte c ulong to void
1607put xs QPOS add get xs QPOS 1
1608s = get xs QST
1609if eq s ZTX
1610ztx c
1611ret
1612end
1613if eq s ZAV
1614zav c
1615ret
1616end
1617if eq s ZSN
1618zsn c
1619ret
1620end
1621if eq s ZAN
1622zan c
1623ret
1624end
1625if eq s ZEN
1626zen c
1627ret
1628end
1629if eq s ZSW
1630zsw c
1631ret
1632end
1633if eq s ZLT
1634zlt c
1635ret
1636end
1637if eq s ZSA
1638zsa c
1639ret
1640end
1641if eq s ZRF
1642zrf c
1643ret
1644end
1645if eq s ZCD
1646zcd c
1647ret
1648end
1649if eq s ZCM
1650zcm c
1651ret
1652end
1653if eq s ZAE
1654zae c
1655ret
1656end
1657if eq s ZAQ
1658zaq c
1659ret
1660end
1661if eq s ZSE
1662zse c
1663ret
1664end
1665if eq s ZEW
1666zew c
1667ret
1668end
1669if eq s ZBG
1670zbg c
1671ret
1672end
1673if eq s ZC1
1674zc1 c
1675ret
1676end
1677if eq s ZCS
1678zcs c
1679ret
1680end
1681if eq s ZDT
1682zdt c
1683ret
1684end
1685if eq s ZPT
1686zpt c
1687ret
1688end
1689if eq s ZPB
1690zpb c
1691ret
1692end
1693zxd c
1694end
1695
1696note ---- input decoding ----
1697note x8: one byte of UTF-8: well-formed (tx_u8st), a Char of XML 1.0 section 2.2 (no C0 control other
1698note than tab, LF and CR; not U+FFFE or U+FFFF), then line ends (section 2.11): CR LF and CR become LF.
1699fn x8 c ulong to void
1700s = ulong 0
1701u = get xs QU8
1702if or ge c 0x80 ne u 0
1703s = tx_u8st u uchar c
1704end
1705if eq s 9
1706xfail 63 e63
1707end
1708put xs QU8 s
1709if lt c 32
1710if and ne c 9 and ne c 10 ne c 13
1711xfail 63 e63
1712end
1713end
1714b1 = get xs QB1
1715if and eq b1 0xbf or eq c 0xbe eq c 0xbf
1716if eq get xs QB2 0xef
1717xfail 63 e63
1718end
1719end
1720put xs QB2 b1
1721put xs QB1 c
1722if eq c 13
1723put xs QCR 1
1724xbyte 10
1725ret
1726end
1727if eq c 10
1728if eq get xs QCR 1
1729put xs QCR 0
1730ret
1731end
1732end
1733put xs QCR 0
1734xbyte c
1735end
1736
1737note xcp: code point v (from UTF-16) as UTF-8 through x8.
1738fn xcp v ulong to void
1739if lt v 0x80
1740x8 v
1741ret
1742end
1743if lt v 0x800
1744x8 or 0xc0 shr v 6
1745x8 or 0x80 and v 0x3f
1746ret
1747end
1748if lt v 0x10000
1749x8 or 0xe0 shr v 12
1750x8 or 0x80 and shr v 6 0x3f
1751x8 or 0x80 and v 0x3f
1752ret
1753end
1754x8 or 0xf0 shr v 18
1755x8 or 0x80 and shr v 12 0x3f
1756x8 or 0x80 and shr v 6 0x3f
1757x8 or 0x80 and v 0x3f
1758end
1759
1760note x16: one byte of UTF-16 (QENC 2 big-endian, 3 little-endian); surrogates must pair.
1761fn x16 c ulong to void
1762h = get xs QU16
1763if eq h 0
1764put xs QU16 add c 256
1765ret
1766end
1767put xs QU16 0
1768b = sub h 256
1769u = or shl b 8 c
1770if eq get xs QENC 3
1771u = or shl c 8 b
1772end
1773hi = get xs QHI
1774if ne hi 0
1775if or lt u 0xdc00 gt u 0xdfff
1776xfail 63 e63
1777end
1778put xs QHI 0
1779xcp add 0x10000 or shl sub hi 0xd800 10 sub u 0xdc00
1780ret
1781end
1782if and ge u 0xd800 le u 0xdbff
1783put xs QHI u
1784ret
1785end
1786if and ge u 0xdc00 le u 0xdfff
1787xfail 63 e63
1788end
1789xcp u
1790end
1791
1792note xbom: the first bytes: EF BB BF is skipped (UTF-8), FE FF is UTF-16BE, FF FE is UTF-16LE; anything
1793note else is UTF-8 from the first byte (XML 1.0 section 4.3.3 and appendix F).
1794fn xbom c ulong to void
1795h = get xs QHB
1796if eq h 0
1797if or eq c 0xef or eq c 0xfe eq c 0xff
1798put xhb 0 uchar c
1799put xs QHB 1
1800ret
1801end
1802put xs QENC 1
1803x8 c
1804ret
1805end
1806b = ulong get xhb 0
1807if and eq b 0xfe eq c 0xff
1808put xs QENC 2
1809put xs QHB 0
1810ret
1811end
1812if and eq b 0xff eq c 0xfe
1813put xs QENC 3
1814put xs QHB 0
1815ret
1816end
1817if eq b 0xef
1818if and eq h 1 eq c 0xbb
1819put xhb 1 187
1820put xs QHB 2
1821ret
1822end
1823if and eq h 2 eq c 0xbf
1824put xs QENC 1
1825put xs QHB 0
1826ret
1827end
1828end
1829put xs QENC 1
1830put xs QHB 0
1831for i ulong h bound 2
1832x8 ulong get xhb i
1833end
1834x8 c
1835end
1836
1837fn xin c ulong to void
1838e = get xs QENC
1839if eq e 1
1840x8 c
1841ret
1842end
1843if eq e 0
1844xbom c
1845ret
1846end
1847x16 c
1848end
1849
1850note ccls: the class of an input byte for the fast path of wout (src/sink.om). Bit 1: plain character data
1851note (0x20..0x7E but not < & ] >); bit 2: an ASCII NameChar (XML 1.0 section 2.3); bit 4: a plain attribute
1852note value byte (0x20..0x7E but not < & " '); bit 8: an ASCII NameStartChar. Any other byte (class 0) takes the full path.
1853global ccls uchar 256
1854
1855fn cinit to void
1856for i ulong 256
1857v = ulong 0
1858if and ge i 32 le i 126
1859v = 5
1860if or eq i 60 eq i 38
1861v = 0
1862end
1863if or eq i 93 eq i 62
1864v = and v 4
1865end
1866if or eq i 34 eq i 39
1867v = and v 1
1868end
1869if nascii i false
1870v = or v 2
1871end
1872if nascii i true
1873v = or v 8
1874end
1875end
1876put ccls i uchar v
1877end
1878end
1879
1880note xreset: a new document; the handler mode m is kept for hstart, hend and htext.
1881fn xreset m ulong to void
1882for i ulong 40
1883put xs i 0
1884end
1885put xs QST ZTX
1886put xs QMODE m
1887bseed
1888cinit
1889end
1890
1891note xeof: the input is complete: it must have closed its root element and every construct.
1892fn xeof to void
1893e = get xs QENC
1894if eq e 0
1895h = get xs QHB
1896put xs QENC 1
1897put xs QHB 0
1898for i ulong h bound 2
1899x8 ulong get xhb i
1900end
1901end
1902if ge e 2
1903if or ne get xs QU16 0 ne get xs QHI 0
1904xfail 63 e63
1905end
1906end
1907if ne get xs QU8 0
1908xfail 63 e63
1909end
1910tflush
1911if ne get xs QST ZTX
1912xfail 61 e61
1913end
1914if ne get xs QROOT 2
1915xfail 61 e61
1916end
1917end
1918
1919note ---- what the handlers use ----
1920note eid: the namespace id of the event element.
1921fn eid to ulong
1922b = get xs QEB
1923if eq b txnone
1924ret NSNO
1925end
1926ret get bid b
1927end
1928
1929note eis: the event element is local name t in namespace id ns.
1930fn eis ns ulong t view uchar * to bool
1931if ne eid ns
1932ret false
1933end
1934o = get xs QELO
1935n = sub get xs QNL o
1936if ne n len t
1937ret false
1938end
1939for i ulong n bound 256
1940if ne get xname add o i get t i
1941ret false
1942end
1943end
1944ret true
1945end
1946
1947fn aname i ulong ns ulong t view uchar * to bool
1948b = get abind i
1949if eq b BDECL
1950ret false
1951end
1952e = NSNO
1953if ne b txnone
1954e = get bid b
1955end
1956if ne e ns
1957ret false
1958end
1959l = get alo i
1960o = add get anoff i l
1961n = sub get anlen i l
1962if ne n len t
1963ret false
1964end
1965for j ulong n bound 256
1966if ne get apool add o j get t j
1967ret false
1968end
1969end
1970ret true
1971end
1972
1973note afind: the index of attribute t in namespace ns (NSNO: unprefixed) of the event element, or txnone.
1974fn afind ns ulong t view uchar * to ulong
1975for i ulong get xs QAC bound 256
1976if aname i ns t
1977ret i
1978end
1979end
1980ret txnone
1981end
1982
1983note aveq: the value of attribute i is t (ci: ASCII case-insensitive).
1984fn aveq i ulong t view uchar * ci bool to bool
1985o = get avoff i
1986n = get avlen i
1987if ne n len t
1988ret false
1989end
1990for j ulong n bound 262144
1991a = ulong get apool add o j
1992b = ulong get t j
1993if ci
1994a = lc a
1995b = lc b
1996end
1997if ne a b
1998ret false
1999end
2000end
2001ret true
2002end