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

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