Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-office/blob/main/src/opc.om # omega-office Omega office documents: read and write office files in Omega Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-office / src / opc.om` 640 lines ยท 12154 bytes Raw Blame History 1 note ======== src/opc.om: the package (ECMA-376 Part 2, Open Packaging Conventions, 5th edition) ======== 2 note Parts are found through relationships, never by fixed names: the package Relationships part 3 note "_rels/.rels" (6.5.2.2) names the main part (relationship type officeDocument), the Media Types 4 note stream "[Content_Types].xml" (7.2.3, 7.3.7) gives its media type, and a part's own Relationships part 5 note (6.5.2.3: "_rels/" before the last segment, ".rels" after it) names the parts it uses. A target is 6 note resolved against its source part (6.4.2, RFC 3986 section 5.2) and mapped to a ZIP item name (7.3.4: 7 note the leading "/" removed, every non-ASCII byte percent-encoded). Part names compare ASCII 8 note case-insensitively (6.2.2.3); relationship types compare exactly (6.5.3.4). 9 10 global os ulong 32 11 const OPNL ulong 0 12 const OBNL ulong 1 13 const OTVL ulong 2 14 const OSVL ulong 3 15 const OMBL ulong 4 16 const OWANT ulong 5 17 const OFND ulong 6 18 const ORTY ulong 7 19 const OSSF ulong 8 20 const OCTF ulong 9 21 const OCDF ulong 10 22 const OCTL ulong 11 23 const ORIDL ulong 12 24 const OKIND ulong 13 25 26 note pname: the ZIP item name to read next; bname: the source part (base of targets); tval, sval: 27 note relationship targets as written; mbuf: a merged path before dot segments are removed. 28 global pname uchar 4096 29 global bname uchar 4096 30 global tval uchar 4096 31 global sval uchar 4096 32 global mbuf uchar 8192 33 global ctyp uchar 1024 34 global rid uchar 1024 35 36 text krels "_rels/.rels" 37 text kctn "[Content_Types].xml" 38 text krelsd "_rels/" 39 text krelse ".rels" 40 text krelsh "Relationships" 41 text krel "Relationship" 42 text kid "Id" 43 text ktype "Type" 44 text ktarg "Target" 45 text ktmode "TargetMode" 46 text kexter "External" 47 text kinter "Internal" 48 text ktypes "Types" 49 text kdefault "Default" 50 text koverr "Override" 51 text kpartn "PartName" 52 text kextn "Extension" 53 text kctype "ContentType" 54 text kcolsp ": " 55 text rtod1 "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" 56 text rtod2 "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" 57 text rtws1 "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" 58 text rtws2 "http://purl.oclc.org/ooxml/officeDocument/relationships/worksheet" 59 text rtss1 "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" 60 text rtss2 "http://purl.oclc.org/ooxml/officeDocument/relationships/sharedStrings" 61 text ctdoc "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" 62 text ctdot "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml" 63 text ctxls "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" 64 text ctxlt "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml" 65 66 text e80 "not a package of the asked kind (no officeDocument relationship, or its media type is not that of the main part of a word processing document or of a workbook)" 67 text e81 "a part the package names is missing" 68 text e81r "the sheet relationship id is not in the workbook Relationships part" 69 text e82 "a relationship target that is not a part name inside this package" 70 text e89 "malformed package part (unexpected root element, or a required attribute missing or too long)" 71 72 note quitp: like quit, with ": NAME" (pname) after the message. 73 fn quitp c int m view uchar * to void 74 warn kpre 75 warn m 76 warn kcolsp 77 r = write 2 pname 0 get os OPNL 78 warn tnl 79 exit c 80 end 81 82 note ---- names ---- 83 fn pput c ulong to void 84 n = get os OPNL 85 if eq n 4095 86 quit 82 e82 87 end 88 put pname n uchar c 89 put os OPNL add n 1 90 end 91 92 fn ptext t view uchar * to void 93 for i ulong len t bound 64 94 pput ulong get t i 95 end 96 end 97 98 fn pset t view uchar * to void 99 put os OPNL 0 100 ptext t 101 end 102 103 note bset: the source part bname = pname; pfromb: back again. 104 fn bset to void 105 n = get os OPNL 106 for i ulong n bound 4096 107 put bname i get pname i 108 end 109 put os OBNL n 110 end 111 112 fn pfromb to void 113 n = get os OBNL 114 for i ulong n bound 4096 115 put pname i get bname i 116 end 117 put os OPNL n 118 end 119 120 note pnmeq: the current central directory name nm[0 .. enl) is pname (ASCII case-insensitive, 6.2.2.3). 121 fn pnmeq to bool 122 n = get enl 0 123 if ne n get os OPNL 124 ret false 125 end 126 for i ulong n bound 4096 127 if ne lc ulong get nm i lc ulong get pname i 128 ret false 129 end 130 end 131 ret true 132 end 133 134 note zfind: the first entry named pname; its central directory fields are then current. 135 fn zfind to bool 136 put cdpos 0 get cdoff 0 137 for i ulong get cdn 0 bound 65535 138 cdnext 139 if pnmeq 140 ret true 141 end 142 end 143 ret false 144 end 145 146 note zpart: entry pname through the XML reader in mode m. Missing: refusal 81 when req, else false. 147 fn zpart m ulong req bool to bool 148 f = zfind 149 if f 150 f = not isdir 151 end 152 if not f 153 if req 154 quitp 81 e81 155 end 156 ret false 157 end 158 precheck 0 159 xreset m 160 osink -1 get eus 0 1 1 161 entout 162 xeof 163 ret true 164 end 165 166 note zopenp: open the zip file named by argument i and check its whole central directory once. 167 fn zopenp i ulong to void 168 fd = argfd i 169 put zfd 0 ulong fd 170 zopen 171 cdcheck 172 end 173 174 note ---- resolving a relationship target (6.4.2, RFC 3986 sections 5.2.2 to 5.2.4) ---- 175 fn mput c ulong to void 176 n = get os OMBL 177 if eq n 8191 178 quit 82 e82 179 end 180 put mbuf n uchar c 181 put os OMBL add n 1 182 end 183 184 note pdrop: remove the last segment of pname (RFC 3986 5.2.4: ".."). 185 fn pdrop to void 186 n = get os OPNL 187 while gt n 0 bound 4096 188 n = sub n 1 189 if eq get pname n 47 190 put os OPNL n 191 ret 192 end 193 end 194 put os OPNL 0 195 end 196 197 note hexd: an uppercase hex digit for v (0..15). 198 fn hexd v ulong to ulong 199 if lt v 10 200 ret add v 48 201 end 202 ret add v 55 203 end 204 205 note pseg: segment mbuf[s .. e) onto pname; last: no segment follows. 206 fn pseg s ulong e ulong last bool to void 207 n = sub e s 208 if eq n 0 209 quit 82 e82 210 end 211 d1 = eq get mbuf s 46 212 if and eq n 1 d1 213 if last 214 quit 82 e82 215 end 216 ret 217 end 218 if and eq n 2 and d1 eq get mbuf add s 1 46 219 if last 220 quit 82 e82 221 end 222 pdrop 223 ret 224 end 225 if eq get mbuf sub e 1 46 226 quit 82 e82 227 end 228 if ne get os OPNL 0 229 pput 47 230 end 231 for i ulong n bound 8192 232 c = ulong get mbuf add s i 233 if or eq c 63 or eq c 35 eq c 92 234 quit 82 e82 235 end 236 if eq c 37 237 pctchk add s i e 238 end 239 if ge c 0x80 240 pput 37 241 pput hexd shr c 4 242 pput hexd and c 15 243 else 244 pput c 245 end 246 end 247 end 248 249 note pctchk: a percent-encoded "/" or "\" is not allowed in a part name (6.2.2.2). 250 fn pctchk p ulong e ulong to void 251 if gt add p 3 e 252 quit 82 e82 253 end 254 a = ulong get mbuf add p 1 255 b = lc ulong get mbuf add p 2 256 if and eq a 50 eq b 102 257 quit 82 e82 258 end 259 if and eq a 53 eq b 99 260 quit 82 e82 261 end 262 end 263 264 fn segwalk to void 265 m = get os OMBL 266 put os OPNL 0 267 s = ulong 0 268 i = ulong 0 269 while le i m bound 8192 270 e = eq i m 271 if not e 272 e = eq get mbuf i 47 273 end 274 if e 275 pseg s i eq i m 276 s = add i 1 277 end 278 i = add i 1 279 end 280 if eq get os OPNL 0 281 quit 82 e82 282 end 283 end 284 285 note hasschm: tval begins with a scheme ("name:" before any "/", "?" or "#"), so it is not a part. 286 fn hasschm n ulong to bool 287 for i ulong n bound 4096 288 c = get tval i 289 if eq c 58 290 ret true 291 end 292 if or eq c 47 or eq c 63 eq c 35 293 ret false 294 end 295 end 296 ret false 297 end 298 299 note resolve: pname = the ZIP item name of target tval[0 .. OTVL) relative to the source part bname 300 note (OBNL = 0: the package root, as for the package Relationships part). 301 fn resolve to void 302 n = get os OTVL 303 if eq n 0 304 quit 82 e82 305 end 306 put os OMBL 0 307 if eq get tval 0 47 308 for i ulong sub n 1 bound 4096 309 mput ulong get tval add i 1 310 end 311 segwalk 312 ret 313 end 314 if hasschm n 315 quit 82 e82 316 end 317 b = get os OBNL 318 d = ulong 0 319 for i ulong b bound 4096 320 if eq get bname i 47 321 d = add i 1 322 end 323 end 324 for i ulong d bound 4096 325 mput ulong get bname i 326 end 327 for i ulong n bound 4096 328 mput ulong get tval i 329 end 330 segwalk 331 end 332 333 note relsname: pname = the Relationships part of the source part bname (6.5.2.3). 334 fn relsname to void 335 b = get os OBNL 336 s = ulong 0 337 for i ulong b bound 4096 338 if eq get bname i 47 339 s = add i 1 340 end 341 end 342 put os OPNL 0 343 for i ulong s bound 4096 344 pput ulong get bname i 345 end 346 ptext krelsd 347 j = s 348 while lt j b bound 4096 349 pput ulong get bname j 350 j = add j 1 351 end 352 ptext krelse 353 end 354 355 note ---- copying attribute values ---- 356 fn cptval i ulong to void 357 n = get avlen i 358 if gt n 4095 359 quit 82 e82 360 end 361 o = get avoff i 362 for j ulong n bound 4096 363 put tval j get apool add o j 364 end 365 put os OTVL n 366 end 367 368 fn cpsval i ulong to void 369 n = get avlen i 370 if gt n 4095 371 quit 82 e82 372 end 373 o = get avoff i 374 for j ulong n bound 4096 375 put sval j get apool add o j 376 end 377 put os OSVL n 378 end 379 380 fn cpctyp i ulong to void 381 n = get avlen i 382 if gt n 1024 383 quit 89 e89 384 end 385 o = get avoff i 386 for j ulong n bound 1024 387 put ctyp j get apool add o j 388 end 389 put os OCTL n 390 end 391 392 fn cprid i ulong to void 393 n = get avlen i 394 if gt n 1024 395 quit 89 e89 396 end 397 o = get avoff i 398 for j ulong n bound 1024 399 put rid j get apool add o j 400 end 401 put os ORIDL n 402 end 403 404 note svtotv: the sharedStrings target becomes the target to resolve. 405 fn svtotv to void 406 n = get os OSVL 407 for j ulong n bound 4096 408 put tval j get sval j 409 end 410 put os OTVL n 411 end 412 413 note aridis: the value of attribute i is rid[0 .. ORIDL). 414 fn aridis i ulong to bool 415 n = get avlen i 416 if ne n get os ORIDL 417 ret false 418 end 419 o = get avoff i 420 for j ulong n bound 1024 421 if ne get apool add o j get rid j 422 ret false 423 end 424 end 425 ret true 426 end 427 428 note ---- the Relationships part (6.5.3): mode MRL ---- 429 note OWANT 1: the first relationship of type officeDocument (target to tval). OWANT 2: the relationship 430 note whose Id is rid (target to tval, ORTY 1 for a worksheet) and the first sharedStrings one (to sval). 431 fn rlstart to void 432 d = get xs QDEP 433 if eq d 1 434 if not eis NSPR krelsh 435 quit 89 e89 436 end 437 ret 438 end 439 if ne d 2 440 ret 441 end 442 if not eis NSPR krel 443 ret 444 end 445 i = afind NSNO kid 446 t = afind NSNO ktype 447 g = afind NSNO ktarg 448 if or eq i txnone or eq t txnone eq g txnone 449 quit 89 e89 450 end 451 x = false 452 m = afind NSNO ktmode 453 if ne m txnone 454 x = aveq m kexter false 455 if not x 456 if not aveq m kinter false 457 quit 89 e89 458 end 459 end 460 end 461 if eq get os OWANT 1 462 if eq get os OFND 1 463 ret 464 end 465 if or aveq t rtod1 false aveq t rtod2 false 466 if x 467 quit 82 e82 468 end 469 cptval g 470 put os OFND 1 471 end 472 ret 473 end 474 rlsheet i t g x 475 end 476 477 fn rlsheet i ulong t ulong g ulong x bool to void 478 if eq get os OFND 0 479 if aridis i 480 if x 481 quit 82 e82 482 end 483 cptval g 484 put os OFND 1 485 put os ORTY 2 486 if or aveq t rtws1 false aveq t rtws2 false 487 put os ORTY 1 488 end 489 end 490 end 491 if eq get os OSSF 0 492 if or aveq t rtss1 false aveq t rtss2 false 493 if x 494 quit 82 e82 495 end 496 cpsval g 497 put os OSSF 1 498 end 499 end 500 end 501 502 note ---- the Media Types stream (7.2.3): mode MCT, looking up the media type of bname ---- 503 fn ctpart i ulong to bool 504 o = get avoff i 505 n = get avlen i 506 b = get os OBNL 507 if ne n add b 1 508 ret false 509 end 510 if ne get apool o 47 511 ret false 512 end 513 for j ulong b bound 4096 514 if ne lc ulong get apool add add o 1 j lc ulong get bname j 515 ret false 516 end 517 end 518 ret true 519 end 520 521 fn ctextm i ulong to bool 522 b = get os OBNL 523 dot = txnone 524 for j ulong b bound 4096 525 c = get bname j 526 if eq c 47 527 dot = txnone 528 end 529 if eq c 46 530 dot = j 531 end 532 end 533 if eq dot txnone 534 ret false 535 end 536 k = sub sub b dot 1 537 if ne get avlen i k 538 ret false 539 end 540 o = get avoff i 541 for j ulong k bound 4096 542 if ne lc ulong get apool add o j lc ulong get bname add add dot 1 j 543 ret false 544 end 545 end 546 ret true 547 end 548 549 fn ctstart to void 550 d = get xs QDEP 551 if eq d 1 552 if not eis NSCT ktypes 553 quit 89 e89 554 end 555 ret 556 end 557 if ne d 2 558 ret 559 end 560 c = afind NSNO kctype 561 if eis NSCT koverr 562 p = afind NSNO kpartn 563 if or eq p txnone eq c txnone 564 quit 89 e89 565 end 566 if ctpart p 567 cpctyp c 568 put os OCTF 1 569 end 570 ret 571 end 572 if eis NSCT kdefault 573 e = afind NSNO kextn 574 if or eq e txnone eq c txnone 575 quit 89 e89 576 end 577 if ctextm e 578 if eq get os OCTF 0 579 cpctyp c 580 end 581 put os OCDF 1 582 end 583 end 584 end 585 586 note cteq: the media type found (ctyp) is t, ASCII case-insensitive (RFC 7231 3.1.1.1). 587 fn cteq t view uchar * to bool 588 n = get os OCTL 589 if ne n len t 590 ret false 591 end 592 for j ulong n bound 1024 593 if ne lc ulong get ctyp j lc ulong get t j 594 ret false 595 end 596 end 597 ret true 598 end 599 600 note ctcheck: the media type of bname must be that of a main part of kind OKIND (2 document, 3 workbook). 601 fn ctcheck to void 602 put os OCTF 0 603 put os OCDF 0 604 pset kctn 605 f = zpart MCT true 606 if and eq get os OCTF 0 eq get os OCDF 0 607 quit 80 e80 608 end 609 ok = false 610 if eq get os OKIND 2 611 ok = or cteq ctdoc cteq ctdot 612 else 613 ok = or cteq ctxls cteq ctxlt 614 end 615 if not ok 616 quit 80 e80 617 end 618 pfromb 619 end 620 621 note pkgmain: pname = bname = the main part of the package, of kind k (2 document, 3 workbook). 622 fn pkgmain k ulong to void 623 put os OKIND k 624 pset krels 625 put os OWANT 1 626 put os OFND 0 627 if not zpart MRL false 628 quit 80 e80 629 end 630 if eq get os OFND 0 631 quit 80 e80 632 end 633 put os OBNL 0 634 resolve 635 if not zfind 636 quitp 81 e81 637 end 638 bset 639 ctcheck 640 end