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