forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compress.Mod
1967 lines (1895 loc) · 47.4 KB
/
Compress.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE Compress; (** portable *) (* (c) ejz, first version: 14.1.92, this version: 8.1.98 / 1.1.98 gs *)
IMPORT Files, Modules, Texts, Oberon,
(* TextDocs *)
Objects, Gadgets, Documents, TextDocs, Desktops;
(* TextDocs *)
(* TextFrames
MenuViewers, TextFrames, Viewers;
TextFrames *)
CONST
BufferSize = 4*1024;
IndexBitCount = 12;
LengthBitCount = 4;
WindowSize = 4096;
RawLookAheadSize = 16;
BreakEven = 1;
LookAheadSize = RawLookAheadSize + BreakEven;
TreeRoot = WindowSize;
EndOfStream = 0;
Unused = 0;
Temp = "Compress.Temp";
err1 = "Error in archive";
err2 = " not found";
err3 = "encryption key needed";
xx = 32768;
Menu = 0;
Cmd = 1;
EOFName = "~ ";
Done *= 0;
ArchiveNotFound *= 1;
ErrorInArchive *= 2;
EntryNotFound *= 3;
ArchiveExists *= 4;
FileError *= 5;
KeyNeeded* = 6;
DocId0 = 0F7X;
DocId1 = 07X;
(* TextDocs *)
DocMenu = "Compress.Open[Open] Compress.Add[Add] Compress.Extract[Extract] Compress.Delete[Delete] TextDocs.Search[Search]";
(* TextDocs *)
(* TextFrames
DirMenu = "System.Close System.Grow Compress.Open Compress.Extract Compress.Delete Compress.Add";
EditMenu = "System.Close System.Copy System.Grow Edit.Search Edit.Store";
TextFrames *)
(* extensions *)
encryption* = 0;
TYPE
Node = RECORD
parent, smallerChild, largerChild: INTEGER
END;
Name *= ARRAY 32 OF CHAR;
Header* = RECORD
name*: Name;
length*, check: LONGINT;
date*, time*: LONGINT;
ratio*: REAL;
extensions*: SET (* encryption *)
END;
List = POINTER TO ListDesc;
ListDesc = RECORD
name: ARRAY 64 OF CHAR;
next: List
END;
HeaderList = POINTER TO HeaderListDesc;
HeaderListDesc = RECORD
header: Header;
next: HeaderList
END;
EnumProc* = PROCEDURE (h: Header; VAR stop: BOOLEAN);
CryptProc* = PROCEDURE (VAR Rin: Files.Rider; nIn: LONGINT; VAR Rout: Files.Rider; key: ARRAY OF CHAR; VAR res: INTEGER);
VAR
W: Texts.Writer;
Buffer: ARRAY BufferSize OF CHAR;
BufferPtr, CurBitNr, Len, maxLen: LONGINT;
CurByte: LONGINT;
Window: ARRAY WindowSize+RawLookAheadSize+1 OF CHAR;
Tree: ARRAY WindowSize+1 OF Node;
Err, verbose, sorted: BOOLEAN;
cmdSource, help: INTEGER;
headerList: HeaderList;
Decrypt*, Encrypt*: CryptProc;
curKey: ARRAY 64 OF CHAR;
PROCEDURE ReadHeader(VAR R: Files.Rider; VAR h: Header; VAR err: BOOLEAN);
VAR
chk, pos: LONGINT;
i: INTEGER;
BEGIN
pos := Files.Pos(R);
Files.ReadBytes(R, h.name, 32);
IF (h.name[0] = DocId0) & (h.name[1] = DocId1) THEN
(* new doc-format skip header *)
Files.Set(R, Files.Base(R), pos);
Files.ReadInt(R, i);
Files.ReadString(R, h.name);
Files.ReadInt(R, i);
Files.ReadInt(R, i);
Files.ReadInt(R, i);
Files.ReadInt(R, i);
ReadHeader(R, h, err)
ELSE
IF R.eof & (R.res = 32) THEN
h.name := EOFName;
err := FALSE;
RETURN
END;
Files.ReadLInt(R, h.length);
Files.ReadLInt(R, h.check);
Files.ReadLInt(R, h.date);
Files.ReadLInt(R, h.time);
Files.ReadReal(R, h.ratio);
IF h.length < 0 THEN
Files.ReadSet(R, h.extensions);
h.length := -h.length
ELSE
h.extensions := {}
END;
IF (h.ratio > 0.0) & (h.ratio < 1000000.0) THEN
pos := 0; chk := 0;
WHILE pos < 32 DO
chk := chk+ORD(h.name[pos]);
INC(pos)
END;
h.name[31] := 0X;
chk := chk+h.length+ENTIER(h.ratio)+(h.time MOD xx)+(h.date MOD xx);
err := chk # h.check
ELSE
err := TRUE
END
END
END ReadHeader;
PROCEDURE WriteHeader(VAR R: Files.Rider; VAR h: Header);
VAR i: LONGINT;
BEGIN
IF curKey # "" THEN
INCL(h.extensions, encryption)
END;
h.check := 0;
i := 0;
WHILE (i < 32) & (h.name[i] # 0X) DO
h.check := h.check + ORD(h.name[i]);
INC(i)
END;
WHILE i < 32 DO
h.name[i] := 0X; INC(i)
END;
Oberon.GetClock(h.time, h.date);
h.check := h.check+h.length+(h.time MOD xx)+(h.date MOD xx)+ENTIER(h.ratio);
Files.WriteBytes(R, h.name, 32);
IF h.extensions # {} THEN
Files.WriteLInt(R, -h.length)
ELSE
Files.WriteLInt(R, h.length)
END;
Files.WriteLInt(R, h.check);
Files.WriteLInt(R, h.date);
Files.WriteLInt(R, h.time);
Files.WriteReal(R, h.ratio);
IF h.extensions # {} THEN
Files.WriteSet(R, h.extensions)
END
END WriteHeader;
PROCEDURE CopyFrom(VAR Ri, Ro: Files.Rider; len: LONGINT);
VAR i: LONGINT;
BEGIN
Files.ReadBytes(Ri, Buffer, BufferSize);
i := BufferSize;
WHILE i <= len DO
Files.WriteBytes(Ro, Buffer, BufferSize);
Files.ReadBytes(Ri, Buffer, BufferSize);
INC(i, BufferSize)
END;
Files.WriteBytes(Ro, Buffer, len MOD BufferSize)
END CopyFrom;
PROCEDURE CopyTo(VAR Ri, Ro: Files.Rider);
BEGIN
Files.ReadBytes(Ri, Buffer, BufferSize);
WHILE ~Ri.eof DO
Files.WriteBytes(Ro, Buffer, BufferSize);
Files.ReadBytes(Ri, Buffer, BufferSize)
END;
Files.WriteBytes(Ro, Buffer, BufferSize-Ri.res)
END CopyTo;
PROCEDURE FlushBits(VAR R: Files.Rider);
BEGIN
IF CurBitNr # 7 THEN
Buffer[BufferPtr] := CHR(CurByte);
INC(BufferPtr)
END;
IF BufferPtr > 0 THEN
Files.WriteBytes(R, Buffer, BufferPtr);
INC(Len, BufferPtr)
END
END FlushBits;
PROCEDURE InputBit(VAR R: Files.Rider): LONGINT;
VAR h: LONGINT;
BEGIN
IF CurBitNr = 7 THEN
IF BufferPtr = BufferSize THEN
Files.ReadBytes(R, Buffer, BufferSize);
INC(Len, BufferSize-R.res);
IF Len >= (maxLen+BufferSize) THEN Err := TRUE END;
BufferPtr := 0
END;
CurByte := ORD(Buffer[BufferPtr]);
INC(BufferPtr)
END;
h := ASH(CurByte, -CurBitNr) MOD 2;
DEC(CurBitNr);
IF CurBitNr < 0 THEN CurBitNr := 7 END;
RETURN h
END InputBit;
PROCEDURE InputBits(VAR R: Files.Rider; count: LONGINT): LONGINT;
VAR i, h: LONGINT;
BEGIN
h := 0;
i := count-1;
WHILE i >= 0 DO
IF CurBitNr = 7 THEN
IF BufferPtr = BufferSize THEN
Files.ReadBytes(R, Buffer, BufferSize);
INC(Len, BufferSize-R.res);
IF Len >= (maxLen+BufferSize) THEN Err := TRUE END;
BufferPtr := 0
END;
CurByte := ORD(Buffer[BufferPtr]);
INC(BufferPtr)
END;
IF ASH(CurByte, -CurBitNr) MOD 2 = 1 THEN
h := h+ASH(1, i)
END;
DEC(CurBitNr);
IF CurBitNr < 0 THEN CurBitNr := 7 END;
DEC(i)
END;
RETURN h
END InputBits;
PROCEDURE OutputBit(VAR R: Files.Rider; bit: LONGINT);
BEGIN
IF bit = 1 THEN
CurByte := CurByte+ASH(1, CurBitNr)
END;
DEC(CurBitNr);
IF CurBitNr < 0 THEN
Buffer[BufferPtr] := CHR(CurByte);
INC(BufferPtr);
IF BufferPtr = BufferSize THEN
Files.WriteBytes(R, Buffer, BufferSize);
INC(Len, BufferSize);
BufferPtr := 0
END;
CurBitNr := 7;
CurByte := 0
END
END OutputBit;
PROCEDURE OutputBits(VAR R: Files.Rider; bits, count: LONGINT);
VAR i, h: LONGINT;
BEGIN
h := bits;
i := count-1;
WHILE i >= 0 DO
IF ASH(h, -i) MOD 2 = 1 THEN
CurByte := CurByte+ASH(1, CurBitNr)
END;
DEC(CurBitNr);
IF CurBitNr < 0 THEN
Buffer[BufferPtr] := CHR(CurByte);
INC(BufferPtr);
IF BufferPtr = BufferSize THEN
Files.WriteBytes(R, Buffer, BufferSize);
INC(Len, BufferSize);
BufferPtr := 0
END;
CurBitNr := 7;
CurByte := 0
END;
DEC(i)
END
END OutputBits;
PROCEDURE Init();
VAR i: LONGINT;
BEGIN
i := 0;
WHILE i < WindowSize DO
Tree[i].parent := Unused;
Tree[i].smallerChild := Unused;
Tree[i].largerChild := Unused;
Window[i] := CHR(0);
INC(i)
END;
Tree[i].parent := Unused;
Tree[i].smallerChild := Unused;
Tree[i].largerChild := Unused;
WHILE i < WindowSize+RawLookAheadSize+1 DO
Window[i] := CHR(0);
INC(i)
END
END Init;
PROCEDURE InitTree(r: INTEGER);
BEGIN
Tree[TreeRoot].largerChild := r;
Tree[r].parent := TreeRoot;
Tree[r].largerChild := Unused;
Tree[r].smallerChild := Unused
END InitTree;
PROCEDURE ContractNode(oldNode, newNode: INTEGER);
BEGIN
help := Tree[oldNode].parent;
Tree[newNode].parent := help;
help := Tree[oldNode].parent;
IF Tree[help].largerChild = oldNode THEN
Tree[help].largerChild := newNode
ELSE
Tree[help].smallerChild := newNode
END;
Tree[oldNode].parent := Unused
END ContractNode;
PROCEDURE ReplaceNode(oldNode, newNode: INTEGER);
VAR parent: INTEGER;
BEGIN
parent := Tree[oldNode].parent;
IF Tree[parent].smallerChild = oldNode THEN
Tree[parent].smallerChild := newNode
ELSE
Tree[parent].largerChild := newNode
END;
Tree[newNode] := Tree[oldNode];
help := Tree[newNode].smallerChild;
Tree[help].parent := newNode;
help := Tree[newNode].largerChild;
Tree[help].parent := newNode;
Tree[oldNode].parent := Unused
END ReplaceNode;
PROCEDURE FindNextNode(node: INTEGER): INTEGER;
VAR next: INTEGER;
BEGIN
next := Tree[node].smallerChild;
WHILE Tree[next].largerChild # Unused DO
next := Tree[next].largerChild
END;
RETURN next
END FindNextNode;
PROCEDURE DeleteString(p: INTEGER);
VAR replacement: INTEGER;
BEGIN
IF Tree[p].parent = Unused THEN
RETURN
END;
IF Tree[p].largerChild = Unused THEN
ContractNode(p, Tree[p].smallerChild)
ELSIF Tree[p].smallerChild = Unused THEN
ContractNode(p, Tree[p].largerChild)
ELSE
replacement := FindNextNode(p);
DeleteString(replacement);
ReplaceNode(p, replacement)
END
END DeleteString;
PROCEDURE AddString(newNode: INTEGER; VAR matchPosition: INTEGER): INTEGER;
VAR i, testNode, delta, matchLength, child: INTEGER;
BEGIN
IF newNode = EndOfStream THEN
RETURN 0
END;
testNode := Tree[TreeRoot].largerChild;
matchLength := 0;
LOOP
i := 0;
delta := 0;
WHILE (i < LookAheadSize) & (delta = 0) DO
delta := ORD(Window[newNode+i]) - ORD(Window[testNode+i]);
INC(i)
END;
IF delta # 0 THEN DEC(i) END;
IF i >= matchLength THEN
matchLength := i;
matchPosition := testNode;
IF matchLength >= LookAheadSize THEN
ReplaceNode(testNode, newNode);
RETURN matchLength
END;
END;
IF delta >= 0 THEN
child := Tree[testNode].largerChild
ELSE
child := Tree[testNode].smallerChild
END;
IF child = Unused THEN
IF delta >= 0 THEN
Tree[testNode].largerChild := newNode
ELSE
Tree[testNode].smallerChild := newNode
END;
Tree[newNode].parent := testNode;
Tree[newNode].largerChild := Unused;
Tree[newNode].smallerChild := Unused;
RETURN matchLength
END;
testNode := child
END
END AddString;
PROCEDURE Compress(VAR Input, Output: Files.Rider; maxbytes: LONGINT);
VAR
i, lookAheadBytes, currentPosition, replaceCount, matchLength, matchPosition: INTEGER;
ch: CHAR;
bytesread: LONGINT;
BEGIN
Init();
bytesread := 0;
currentPosition := 1;
i := 0;
WHILE (i < LookAheadSize) & ~Input.eof & (bytesread < maxbytes) DO
Files.Read(Input, ch);
INC(bytesread);
Window[currentPosition+i] := ch;
IF currentPosition+i < RawLookAheadSize+1 THEN
Window[currentPosition+i+WindowSize-1] := ch
END;
INC(i)
END;
IF Input.eof OR (bytesread >= maxbytes) THEN DEC(i) END;
lookAheadBytes := i;
InitTree(currentPosition);
matchLength := 0;
matchPosition := 0;
WHILE lookAheadBytes > 0 DO
IF matchLength > lookAheadBytes THEN
matchLength := lookAheadBytes
END;
IF matchLength <= BreakEven THEN
replaceCount := 1;
OutputBit(Output, 1);
OutputBits(Output, ORD(Window[currentPosition]), 8)
ELSE
OutputBit(Output, 0);
OutputBits(Output, matchPosition, IndexBitCount);
OutputBits(Output, matchLength-(BreakEven+1), LengthBitCount);
replaceCount := matchLength
END;
i := 0;
WHILE i < replaceCount DO
DeleteString((currentPosition+LookAheadSize) MOD (WindowSize-1));
Files.Read(Input, ch);
INC(bytesread);
IF Input.eof OR (bytesread >= maxbytes) THEN
DEC(lookAheadBytes)
ELSE
Window[currentPosition+LookAheadSize] := ch;
Window[(currentPosition+LookAheadSize) MOD (WindowSize-1)] := ch
END;
currentPosition := (currentPosition+1) MOD (WindowSize-1);
IF lookAheadBytes # 0 THEN
matchLength := AddString(currentPosition, matchPosition)
END;
INC(i)
END
END;
OutputBit(Output, 0);
OutputBits(Output, EndOfStream, IndexBitCount)
END Compress;
PROCEDURE Expand(VAR Input, Output: Files.Rider);
VAR
i, currentPosition, matchLength, matchPosition: INTEGER;
ch: CHAR;
BEGIN
Err := FALSE;
Init;
currentPosition := 1;
LOOP
IF InputBit(Input) # 0 THEN
ch := CHR(InputBits(Input, 8));
Files.Write(Output, ch);
Window[currentPosition] := ch;
IF currentPosition < RawLookAheadSize+1 THEN
Window[currentPosition+WindowSize-1] := ch
END;
currentPosition := (currentPosition+1) MOD (WindowSize-1)
ELSE
matchPosition := SHORT(InputBits(Input, IndexBitCount));
IF matchPosition = EndOfStream THEN EXIT END;
matchLength := SHORT(InputBits(Input, LengthBitCount));
INC(matchLength, BreakEven);
i := 0;
WHILE i <= matchLength DO
ch := Window[matchPosition+i];
Files.Write(Output, ch);
Window[currentPosition] := ch;
IF currentPosition < RawLookAheadSize+1 THEN
Window[currentPosition+WindowSize-1] := ch;
END;
currentPosition := (currentPosition+1) MOD (WindowSize-1);
INC(i)
END
END;
IF Err THEN RETURN END
END
END Expand;
(* Compress len Bytes form Ri to Ro. *)
PROCEDURE CopyToArc(VAR Ri, Ro: Files.Rider; len: LONGINT; VAR h: Header);
VAR
F: Files.File;
R: Files.Rider;
len0: LONGINT;
res: INTEGER;
BEGIN
res := Done;
len0 := len; INC(len); Len := 0;
BufferPtr := 0; CurBitNr := 7; CurByte := 0;
IF encryption IN h.extensions THEN
F := Files.New(""); Files.Set(R, F, 0);
Compress(Ri, R, len);
FlushBits(R); Files.Set(R, F, 0);
len := Len; Len := Files.Pos(Ro);
Encrypt(R, len, Ro, curKey, res); ASSERT(res = Done);
Len := Files.Pos(Ro)-Len
ELSE
Compress(Ri, Ro, len);
FlushBits(Ro)
END;
IF len0 < 1 THEN
len0 := 1
END;
h.length := Len; h.ratio := 100*Len/len0
END CopyToArc;
(* Expand a maximum of len Bytes from Ri to Ro.
res:
Done
ErrorInArchive: error in compressed data *)
PROCEDURE CopyFromArc(VAR Ri, Ro: Files.Rider; VAR h: Header; VAR res: INTEGER);
VAR
F: Files.File;
R: Files.Rider;
BEGIN
res := Done; Len := 0;
BufferPtr := BufferSize; CurBitNr := 7; CurByte := 0;
IF encryption IN h.extensions THEN
IF (Decrypt # NIL) & (curKey # "") THEN
F := Files.New(""); Files.Set(R, F, 0);
Decrypt(Ri, h.length, R, curKey, res); ASSERT(res = Done);
maxLen := Files.Length(F); Files.Set(R, F, 0);
Expand(R, Ro);
Err := Err OR ((Len MOD BufferSize) # BufferPtr)
ELSE
Texts.WriteString(W, err3);
Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
res := KeyNeeded
END
ELSE
maxLen := h.length;
Expand(Ri, Ro)
END;
IF Err THEN
res := ErrorInArchive
END
END CopyFromArc;
(* TextDocs *)
PROCEDURE InMenu(context: Objects.Object): BOOLEAN;
VAR obj, L: Objects.Object;
BEGIN
obj := context; L := NIL;
WHILE (obj # NIL) & ~((obj IS Desktops.DocGadget) OR (obj IS Desktops.DocViewer)) DO L := obj; obj := obj.dlink END;
IF obj # NIL THEN
IF obj IS Desktops.DocGadget THEN RETURN L = Desktops.Menu(obj(Desktops.DocGadget))
ELSIF (obj IS Desktops.DocViewer) & Desktops.HasMenu(obj(Desktops.DocViewer)) THEN RETURN L = obj(Desktops.DocViewer).dsc
ELSE RETURN FALSE
END
ELSE RETURN FALSE
END
END InMenu;
PROCEDURE GetArcName(VAR name: ARRAY OF CHAR);
VAR D: Documents.Document;
BEGIN
IF InMenu(Gadgets.context) THEN
D := Desktops.CurDoc(Gadgets.context);
IF D # NIL THEN
cmdSource := Menu;
name := D.name;
RETURN
END
END;
cmdSource := Cmd; name := EOFName
END GetArcName;
(* TextDocs *)
(* TextFrames
PROCEDURE GetArcName(VAR name: ARRAY OF CHAR);
VAR
V: Viewers.Viewer;
S: Texts.Scanner;
BEGIN
V := Oberon.Par.vwr;
IF (V.dsc IS TextFrames.Frame) & (V.dsc = Oberon.Par.frame) THEN
Texts.OpenScanner(S, V.dsc(TextFrames.Frame).text, 0);
Texts.Scan(S);
IF S.class = Texts.Name THEN
cmdSource := Menu;
name := S.s;
RETURN
END
END;
cmdSource := Cmd; name := EOFName
END GetArcName;
TextFrames *)
PROCEDURE StringLen(str: ARRAY OF CHAR): INTEGER;
VAR i: INTEGER;
BEGIN
i := 0;
WHILE (i < LEN(str)) & (str[i] # CHR(0)) DO
INC(i)
END;
RETURN i
END StringLen;
PROCEDURE Remove(VAR nameList: List; VAR name: Name);
VAR cur, prev: List;
BEGIN
cur := nameList.next;
prev := nameList;
WHILE cur # NIL DO
IF cur.name = name THEN
prev.next := cur.next;
RETURN
ELSE
prev := cur
END;
cur := cur.next
END
END Remove;
PROCEDURE Search(nameList: List; VAR name: ARRAY OF CHAR): List;
BEGIN
WHILE nameList # NIL DO
IF nameList.name = name THEN RETURN nameList END;
nameList := nameList.next
END;
RETURN NIL
END Search;
PROCEDURE ClearCrypt*();
BEGIN
Decrypt := NIL; Encrypt := NIL; curKey := ""
END ClearCrypt;
PROCEDURE InstallCrypt*(install, key: ARRAY OF CHAR);
VAR
mod: Modules.Module;
cmd: Modules.Command;
i, j: LONGINT;
BEGIN
ClearCrypt(); curKey := key;
i := 0;
WHILE (install[i] # 0X) & (install[i] # ".") DO
INC(i)
END;
IF install[i] = "." THEN
install[i] := 0X; INC(i);
mod := Modules.ThisMod(install);
IF mod # NIL THEN
j := 0;
WHILE install[i] # 0X DO
install[j] := install[i]; INC(i); INC(j)
END;
install[j] := 0X;
cmd := Modules.ThisCommand(mod, install);
IF cmd # NIL THEN
cmd()
END
END
END;
ASSERT((Decrypt # NIL) & (Encrypt # NIL))
END InstallCrypt;
PROCEDURE GetArgs(VAR nameList: List);
VAR
h, last: List;
S: Texts.Scanner;
mn: ARRAY 64 OF CHAR;
arrow: BOOLEAN;
T: Texts.Text;
beg, end, time, pos: LONGINT;
BEGIN
ClearCrypt();
verbose := FALSE; sorted := FALSE;
pos := 0;
end := 0;
arrow := FALSE;
nameList := NIL;
last := NIL;
GetArcName(mn);
IF mn # EOFName THEN
arrow := TRUE;
NEW(h);
h.next := NIL;
h.name := mn;
nameList := h;
last := nameList;
Oberon.GetSelection(T, beg, end, time);
IF time > 0 THEN
Texts.OpenScanner(S, T, beg); pos := beg; Texts.Scan(S)
ELSE
RETURN
END
ELSE
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(S);
WHILE (S.class = Texts.Char) & (S.c = Oberon.OptionChar) DO
Texts.Scan(S);
ASSERT(S.class = Texts.Name);
CASE CAP(S.s[0]) OF
"C": Texts.Scan(S); mn := S.s; ASSERT(S.class = Texts.Name);
Texts.Scan(S); ASSERT(S.class IN {Texts.Name, Texts.String});
InstallCrypt(mn, S.s)
|"D": verbose := TRUE
|"S": sorted := TRUE
END;
Texts.Scan(S)
END;
IF (S.class = Texts.Char) & (S.c = "^") THEN
arrow := TRUE;
Oberon.GetSelection(T, beg, end, time);
IF time > 0 THEN
Texts.OpenScanner(S, T, beg); pos := beg; Texts.Scan(S)
ELSE
RETURN
END
END
END;
WHILE ~S.eot & ((cmdSource = Menu) & (pos <= end+StringLen(S.s))) OR
((cmdSource = Cmd) & (S.class = Texts.Name) & (~arrow OR (arrow & (pos <= end+StringLen(S.s))))) DO
NEW(h);
h.next := NIL;
h.name := S.s;
IF Search(nameList, h.name) = NIL THEN
IF last = NIL THEN
nameList := h
ELSE
last.next := h
END;
last := h
END;
Texts.Scan(S);
IF ~arrow & (S.class = Texts.Char) & (S.c = "^") THEN
arrow := TRUE;
Oberon.GetSelection(T, beg, end, time);
IF time > 0 THEN
Texts.OpenScanner(S, T, beg); Texts.Scan(S)
END
END;
pos := Texts.Pos(S)
END
END GetArgs;
PROCEDURE Trimm(VAR name: ARRAY OF CHAR);
VAR
l, i, j: LONGINT;
back: Name;
ch: CHAR;
BEGIN
l := LEN(name);
j := -1;
i := 0;
WHILE (i < l) & (name[i] # 0X) DO
ch := name[i];
IF (ch = "/") OR (ch = "\") THEN
j := i
END;
INC(i)
END;
IF j >= 0 THEN
back := name;
j := j+1;
i := 0;
WHILE (j < l) & (back[j] # 0X) DO
name[i] := back[j];
INC(i);
INC(j)
END;
name[i] := 0X
END
END Trimm;
PROCEDURE NextName(VAR name: ARRAY OF CHAR);
VAR
i, l: LONGINT;
ch: CHAR;
BEGIN
l := LEN(name);
i := 0;
WHILE (i < l) & (name[i] # 0X) DO
INC(i)
END;
IF i >= l THEN
name[l-1] := CHR(ORD(name[l-1])+1)
ELSE
ch := name[i-1];
IF (ch >= "0") & (ch <= "8") THEN
name[i-1] := CHR(ORD(name[i-1])+1)
ELSE
name[i] := "0";
IF (i+1) < l THEN
name[i+1] := 0X
END
END
END
END NextName;
PROCEDURE *InsertHeadSort(h: Header; VAR stop: BOOLEAN);
VAR
newElem,prevElem,curElem: HeaderList;
BEGIN
NEW(newElem); newElem.header:= h;
prevElem:= NIL; curElem:= headerList;
WHILE (curElem # NIL) & (curElem.header.name < h.name) DO
prevElem:= curElem; curElem:= curElem.next;
END;
newElem.next:= curElem;
IF (prevElem = NIL) THEN headerList:= newElem; ELSE prevElem.next:= newElem; END;
END InsertHeadSort;
(** Enumerate all entries in the archive (archive). Stop if stop (in enumProc) is set or if at end of archive.
res:
Done
ArchiveNotFound: archive-file not found
ErrorInArchive: internal error in archive-file *)
PROCEDURE Enumerate*(archive: ARRAY OF CHAR; enumProc: EnumProc; sorted: BOOLEAN; VAR res: INTEGER);
VAR
ArcF: Files.File;
R: Files.Rider;
h: Header;
err, stop: BOOLEAN;
BEGIN
IF sorted THEN
headerList:= NIL;
Enumerate(archive, InsertHeadSort, FALSE, res);
IF res = Done THEN
stop := FALSE;
WHILE ~stop & (headerList # NIL) DO
enumProc(headerList.header, stop); headerList:= headerList.next
END
END;
headerList := NIL
ELSE
ArcF := Files.Old(archive);
IF ArcF # NIL THEN
err := FALSE;
stop := FALSE;
Files.Set(R, ArcF, 0);
ReadHeader(R, h, err);
WHILE (h.name # EOFName) & ~err & ~stop DO
enumProc(h, stop);
Files.Set(R, ArcF, Files.Pos(R)+h.length);
ReadHeader(R, h, err)
END;
IF err THEN
res := ErrorInArchive
ELSE
res := Done
END
ELSE
res := ArchiveNotFound
END
END
END Enumerate;
(** Add a new entry (file) to the archive (archive) with data read from R.
file: input: name of the entry
output: name choosen for entry (may differ if names collaps)
res:
Done
ArchiveNotFound: archive-file not found
ErrorInArchive: internal error in archive-file *)
PROCEDURE AddFile*(archive: ARRAY OF CHAR; VAR file: ARRAY OF CHAR; VAR R: Files.Rider; len: LONGINT; VAR res: INTEGER);
VAR
ArcF: Files.File;
Ra: Files.Rider;
h: Header;
pos: LONGINT;
addL, ha: List;
err: BOOLEAN;
BEGIN
ArcF := Files.Old(archive);
IF ArcF # NIL THEN
Files.Set(Ra, ArcF, 0);
addL := NIL;
pos := Files.Pos(Ra);
ReadHeader(Ra, h, err);
WHILE (h.name # EOFName) & ~err DO
IF addL = NIL THEN
NEW(addL); addL.name := h.name;
addL.next := NIL
ELSE
NEW(ha); ha.name := h.name;
ha.next := addL;
addL := ha
END;
Files.Set(Ra, ArcF, Files.Pos(Ra)+h.length);
pos := Files.Pos(Ra);
ReadHeader(Ra, h, err)
END;
IF err THEN
res := ErrorInArchive;
RETURN
END;
Trimm(file);
WHILE Search(addL, file) # NIL DO
NextName(file)
END;
pos := Files.Pos(Ra);
h.name := file;
h.extensions := {};
WriteHeader(Ra, h);
CopyToArc(R, Ra, len, h);
Files.Set(Ra, ArcF, pos);
WriteHeader(Ra, h);
Files.Close(ArcF);
res := Done
ELSE
res := ArchiveNotFound
END
END AddFile;
PROCEDURE WriteDocHead(VAR R: Files.Rider);
BEGIN
(* TextDocs *)
Files.WriteInt(R, Documents.Id);
(* TextDocs *)
(* TextFrames
Files.Write(R, DocId0);
Files.Write(R, DocId1);
TextFrames *)
Files.WriteString(R, "Compress.NewDoc");
Files.WriteInt(R, 0);
Files.WriteInt(R, 0);
Files.WriteInt(R, 200);
Files.WriteInt(R, 250)
END WriteDocHead;
(** Delete entry (file) from the archive (archive).
res:
Done
ArchiveNotFound: archive-file not found
ErrorInArchive: internal error in archive-file
EntryNotFound: no such entry (entry) found *)
PROCEDURE DeleteFile*(archive, file: ARRAY OF CHAR; VAR res: INTEGER);
VAR
ArcF, TmpF: Files.File;
Ra, Rt: Files.Rider;
h: Header;
err, del: BOOLEAN;
pos: LONGINT;
BEGIN
ArcF := Files.Old(archive);
IF ArcF # NIL THEN
TmpF := Files.New(Temp);
Files.Set(Rt, TmpF, 0);
del := FALSE;
Files.Set(Ra, ArcF, 0);
ReadHeader(Ra, h, err);
WHILE (h.name # EOFName) & ~err DO
pos := Files.Pos(Ra);