-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompressUtil.Mod
414 lines (384 loc) · 10.4 KB
/
CompressUtil.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
(* 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 CompressUtil; (* pjm - based on Compress by ejz *)
IMPORT SYSTEM;
CONST
IndexBitCount = 12;
LengthBitCount = 4;
WindowSize = 4096;
RawLookAheadSize = 16;
BreakEven = 1;
LookAheadSize = RawLookAheadSize + BreakEven;
TreeRoot = WindowSize;
EndOfStream = 0;
Unused = 0;
TYPE
InputProc* = PROCEDURE (VAR ch: CHAR; VAR eof: BOOLEAN);
OutputProc* = PROCEDURE (ch: CHAR);
Node = RECORD
parent, smallerChild, largerChild: INTEGER
END;
VAR
Read: InputProc;
Write: OutputProc;
CurBitNr: LONGINT;
CurByte: LONGINT;
Window: POINTER TO ARRAY WindowSize+RawLookAheadSize+1 OF CHAR;
Tree: POINTER TO ARRAY WindowSize+1 OF Node;
help: INTEGER;
Err: BOOLEAN;
i, currentPosition, matchLength, matchPosition: INTEGER;
PROCEDURE FlushBits();
BEGIN
IF CurBitNr # 7 THEN Write(CHR(CurByte)) END;
END FlushBits;
PROCEDURE InputBit(): LONGINT;
VAR h: LONGINT; ch: CHAR; eof: BOOLEAN;
BEGIN
IF CurBitNr = 7 THEN
Read(ch, eof);
IF eof THEN Err := TRUE; RETURN 1 END;
CurByte := ORD(ch)
END;
h := ASH(CurByte, -CurBitNr) MOD 2;
DEC(CurBitNr);
IF CurBitNr < 0 THEN CurBitNr := 7 END;
RETURN h
END InputBit;
PROCEDURE InputBits(count: LONGINT): LONGINT;
VAR i, h: LONGINT; eof: BOOLEAN; ch: CHAR;
BEGIN
h := 0;
i := count-1;
WHILE i >= 0 DO
IF CurBitNr = 7 THEN
Read(ch, eof);
IF eof THEN Err := TRUE; RETURN 1 END;
CurByte := ORD(ch)
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(bit: LONGINT);
BEGIN
IF bit = 1 THEN
CurByte := CurByte+ASH(1, CurBitNr)
END;
DEC(CurBitNr);
IF CurBitNr < 0 THEN
Write(CHR(CurByte));
CurBitNr := 7;
CurByte := 0
END
END OutputBit;
PROCEDURE OutputBits(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
Write(CHR(CurByte));
CurBitNr := 7;
CurByte := 0
END;
DEC(i)
END
END OutputBits;
PROCEDURE Init();
VAR i: LONGINT;
BEGIN
IF Window = NIL THEN NEW(Window); NEW(Tree) END;
CurBitNr := 7; CurByte := 0;
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;
(** Compress - Compress a stream of bytes. *)
PROCEDURE Compress*(Input: InputProc; Output: OutputProc);
VAR
i, lookAheadBytes, currentPosition, replaceCount, matchLength, matchPosition: INTEGER;
ch: CHAR; eof: BOOLEAN;
BEGIN
Read := Input; Write := Output; eof := FALSE;
Init();
currentPosition := 1;
i := 0;
WHILE (i < LookAheadSize) & ~eof DO
Read(ch, eof);
Window[currentPosition+i] := ch;
IF currentPosition+i < RawLookAheadSize+1 THEN
Window[currentPosition+i+WindowSize-1] := ch
END;
INC(i)
END;
IF eof 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(1);
OutputBits(ORD(Window[currentPosition]), 8)
ELSE
OutputBit(0);
OutputBits(matchPosition, IndexBitCount);
OutputBits(matchLength-(BreakEven+1), LengthBitCount);
replaceCount := matchLength
END;
i := 0;
WHILE i < replaceCount DO
DeleteString((currentPosition+LookAheadSize) MOD (WindowSize-1));
Read(ch, eof);
IF eof 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(0);
OutputBits(EndOfStream, IndexBitCount);
FlushBits()
END Compress;
(*
PROCEDURE Expand*(Input: InputProc; Output: OutputProc; VAR ok: BOOLEAN);
VAR
i, currentPosition, matchLength, matchPosition: INTEGER;
ch: CHAR;
BEGIN
Read := Input; Write := Output;
Err := FALSE;
Init;
currentPosition := 1;
LOOP
IF InputBit() # 0 THEN
ch := CHR(InputBits(8));
IF Err THEN EXIT END;
Write(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(IndexBitCount));
IF matchPosition = EndOfStream THEN EXIT END;
matchLength := SHORT(InputBits(LengthBitCount));
IF Err THEN EXIT END;
INC(matchLength, BreakEven);
i := 0;
WHILE i <= matchLength DO
ch := Window[matchPosition+i];
Write(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
END;
ok := ~Err
END Expand;
*)
(** InitExpand - Initialize expanding of a stream. *)
PROCEDURE InitExpand*(Input: InputProc);
BEGIN
Read := Input; Write := NIL;
Err := FALSE;
Init;
currentPosition := 1; i := -1
END InitExpand;
(** ExpandBlock - Expand a block of maximum len bytes of the stream. outLen is actual number of bytes
expanded, and is -1 on error. eof is set when attempting to read past the end-of-file the first time. *)
PROCEDURE ExpandBlock*(VAR buf: ARRAY OF BYTE; len: LONGINT; VAR outLen: LONGINT; VAR eof: BOOLEAN);
VAR
ch: CHAR; count: LONGINT;
BEGIN
count := 0; eof := FALSE;
LOOP
IF i < 0 THEN
IF InputBit() # 0 THEN
ch := CHR(InputBits(8));
IF Err THEN len := -1; EXIT END;
buf[count] := ch; INC(count);
Window[currentPosition] := ch;
IF currentPosition < RawLookAheadSize+1 THEN
Window[currentPosition+WindowSize-1] := ch
END;
currentPosition := (currentPosition+1) MOD (WindowSize-1)
ELSE
matchPosition := SHORT(InputBits(IndexBitCount));
IF matchPosition = EndOfStream THEN eof := TRUE; len := count; EXIT END;
matchLength := SHORT(InputBits(LengthBitCount));
IF Err THEN len := -1; EXIT END;
INC(matchLength, BreakEven);
i := 0
END
ELSE (* i >= 0 *)
IF i <= matchLength THEN
ch := Window[matchPosition+i];
buf[count] := ch; INC(count);
Window[currentPosition] := ch;
IF currentPosition < RawLookAheadSize+1 THEN
Window[currentPosition+WindowSize-1] := ch
END;
currentPosition := (currentPosition+1) MOD (WindowSize-1);
INC(i)
ELSE
i := -1
END
END;
IF count = len THEN EXIT END
END;
outLen := len
END ExpandBlock;
BEGIN
Window := NIL
END CompressUtil.