-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArcMethod.pas
470 lines (397 loc) · 9.28 KB
/
ArcMethod.pas
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
unit ArcMethod;
interface
{$mode objfpc}{$H+}
//Based on DEARC.PAS from 1988
//Authors: Roy Collins, David W. Carroll, Richard P. Byrne, Paul Roub, PascalVault
//https://github.com/PascalVault
//Licence: MIT
uses
classes, sysutils, dialogs;
const
BLOCKSIZE = 512; // I/O block size
strlen = 100; // standard string length
fnlen = 12; // file name length - 1
ERROR = -1;
SPEOF = 256;
NUMVALS = 256; // 1 less than the number of values
DLE = $90;
Crunch_BITS = 12;
Squash_BITS = 13;
INIT_BITS = 9;
FIRST = 257;
CLEAR = 256;
HSIZEM1 = 8191;
BITSM1 = 12;
RMASK : array[0..8] of byte = ($00, $01, $03, $07, $0f, $1f, $3f, $7f, $ff);
TABSIZE = 4096;
TABSIZEM1 = 4095;
NO_PRED : word = $FFFF;
EMPTY : word = $FFFF;
MaxInt16 = 32767;
type
strtype = string[strlen];
fntype = array [0..fnlen] of char;
buftype = array [1..BLOCKSIZE] of byte;
nd = record
child : array [0..1] of Int16
end;
entry = packed record
used : boolean;
next : Int16;
predecessor : Int16;
follower : byte
end;
type
{ TArcMethod }
TArcMethod = class
protected
arcfile : TStream;
arcbuf : buftype;
arcptr : Int16;
endfile : boolean;
extfile : TStream;
extbuf : buftype;
extptr : Int16;
size : longint;
state : (NOHIST, INREP);
firstch : boolean;
lastc : Int16;
code : Int16;
oldcode : Int16;
finchar : Int16;
bits,
n_bits,
maxcode : Int16;
prefix : array[0..HSIZEM1] of Int16;
suffix : array[0..HSIZEM1] of byte;
buf : array[0..BITSM1] of byte;
clear_flg : Int16;
stack1 : array[0..HSIZEM1] of byte;
free_ent : Int16;
maxcodemax : Int16;
offset,
sizex : Int16;
procedure Read_Block;
procedure Write_Block;
function get_arc : byte;
procedure put_ext(c : byte);
procedure putc_unp(c : Int16);
procedure putc_ncr(c : Int16);
function getc_unp : Int16;
procedure open_arc;
procedure open_ext;
procedure close_arc;
procedure close_ext;
function getcode : Int16;
procedure decomp(SquashFlag : Int16);
public
constructor Create(InF, OutF: TStream);
end;
{ TCrunch8 }
TCrunch8 = class(TArcMethod)
public
procedure Decode(PackedSize: Int64);
end;
TSquash = class(TArcMethod)
public
procedure Decode(PackedSize: Int64);
end;
implementation
// read a block from the archive file
procedure TArcMethod.Read_Block;
var res : Int64;
begin
res := arcfile.read(arcbuf, BLOCKSIZE);
if res < 0 then endfile := True;
arcptr := 1;
end;
procedure TArcMethod.Write_Block;
begin
extfile.Write(extbuf, extptr);
extptr := 1
end;
// read 1 character from the archive file
function TArcMethod.get_arc : byte;
begin
if endfile then
get_arc := 0
else
begin
get_arc := arcbuf[arcptr];
if arcptr = BLOCKSIZE then
Read_Block
else
arcptr := arcptr + 1
end
end;
//write 1 character to the extracted file
procedure TArcMethod.put_ext(c : byte);
begin
extbuf[extptr] := c;
if extptr = BLOCKSIZE then
Write_Block
else
extptr := extptr + 1
end;
// put one character to extracted file
procedure TArcMethod.putc_unp(c : Int16);
begin
put_ext(c)
end;
// put one char, checking for run-length compression
procedure TArcMethod.putc_ncr(c : Int16);
begin
case state of
NOHIST :
if c = DLE then
state := INREP
else
begin
lastc := c;
putc_unp(c)
end;
INREP :
begin
if c = 0 then
putc_unp(DLE)
else
begin
c := c - 1;
while (c <> 0) do
begin
putc_unp(lastc);
c := c - 1
end
end;
state := NOHIST
end
end // case
end;
//get one character from archive
function TArcMethod.getc_unp : Int16;
begin
if size = 0.0 then
getc_unp := -1
else
begin
size := size - 1;
getc_unp := get_arc
end;
end;
//open the archive file for input processing
procedure TArcMethod.open_arc;
begin
endfile := FALSE;
Read_Block
end;
//open the extracted file for writing
procedure TArcMethod.open_ext;
begin
extptr := 1;
end;
// close the archive file
procedure TArcMethod.close_arc;
begin
//arcfile.free;
end;
// close the extracted file
procedure TArcMethod.close_ext;
var
dt : longint;
handle : word;
begin
extptr := extptr - 1;
if (extptr <> 0) then
Write_Block;
//extfile.free;
end;
function TArcMethod.getcode : Int16;
var
r_off, bitsx : Int16;
bp : byte;
skipped: Boolean;
sizex2: Int16;
begin
if firstch then
begin
offset := 0;
sizex := 0;
firstch := false;
end;
bp := 0;
if (clear_flg > 0) or (offset >= sizex) or (free_ent > maxcode) then
begin
if free_ent > maxcode then
begin
n_bits := n_bits + 1;
if n_bits = BITS then
maxcode := maxcodemax
else
maxcode := (1 shl n_bits) - 1;
end;
if clear_flg > 0 then
begin
n_bits := INIT_BITS;
maxcode := (1 shl n_bits) - 1;
clear_flg := 0;
end;
skipped := false;
sizex2 := sizex;
for sizex2 := 0 to n_bits-1 do
begin
code := getc_unp;
if code = -1 then begin
skipped := true;
break;
end
else
buf[sizex2] := code;
end;
sizex := sizex2; //maybe not needed
if not skipped then
sizex := sizex + 1;
if sizex <= 0 then
begin
result := -1;
exit;
end;
offset := 0;
sizex := (sizex shl 3) - (n_bits - 1);
end;
r_off := offset;
bitsx := n_bits;
//get first byte
bp := bp + (r_off shr 3);
r_off := r_off and 7;
//get first part (low order bits)
code := buf[bp] shr r_off;
bp := bp + 1;
bitsx := bitsx - (8 - r_off);
r_off := 8 - r_off;
if bitsx >= 8 then
begin
code := code or (buf[bp] shl r_off);
bp := bp + 1;
r_off := r_off + 8;
bitsx := bitsx - 8;
end;
code := code or Int16(((buf[bp] and rmask[bitsx]) shl r_off));
offset := offset + n_bits;
result := code;
end;
// decompress a file with LZW
procedure TArcMethod.decomp(SquashFlag : Int16);
var
stackp : Int16;
// acode,
incode : Int16;
codee: Int16;
begin
if SquashFlag = 0 then
Bits := crunch_BITS
else
Bits := squash_BITS;
if firstch then
maxcodemax := 1 shl bits;
if SquashFlag = 0 then
begin
code := getc_unp;
if code <> BITS then
begin
raise Exception.Create(Format('File packed with %d bits, I can only handle %d', [code, Bits] ));
Exit;
end;
end;
clear_flg := 0;
n_bits := INIT_BITS;
maxcode := (1 shl n_bits ) - 1;
for codee := 255 downto 0 do
begin
prefix[codee] := 0;
suffix[codee] := codee;
end;
free_ent := FIRST;
oldcode := getcode;
finchar := oldcode;
if oldcode = -1 then
exit;
if SquashFlag = 0 then
putc_ncr(finchar)
else
putc_unp(finchar);
stackp := 0;
code := getcode;
while (code > -1) do
begin
if code = CLEAR then
begin
for codee := 255 downto 0 do
prefix[codee] := 0;
clear_flg := 1;
free_ent := FIRST - 1;
code := getcode;
end;
incode := code;
if code >= free_ent then
begin
stack1[stackp] := finchar;
stackp := stackp + 1;
code := oldcode;
end;
while (code >= 256) do
begin
stack1[stackp] := suffix[code];
stackp := stackp + 1;
code := prefix[code];
end;
finchar := suffix[code];
stack1[stackp] := finchar;
stackp := stackp + 1;
repeat
stackp := stackp - 1;
If SquashFlag = 0 then
putc_ncr(stack1[stackp])
else
putc_unp(stack1[stackp]);
until stackp <= 0;
code := free_ent;
if code < maxcodemax then
begin
prefix[code] := oldcode;
suffix[code] := finchar;
free_ent := code + 1;
end;
oldcode := incode;
code := getcode;
end;
end;
constructor TArcMethod.Create(InF, OutF: TStream);
begin
arcfile := InF;
extfile := OutF;
end;
{ TSquash }
procedure TSquash.Decode(PackedSize: Int64);
begin
open_arc;
open_ext;
Size := PackedSize;
state := NOHIST;
FirstCh := TRUE;
decomp(1);
close_ext();
close_arc;
end;
{ TCrunch8 }
procedure TCrunch8.Decode(PackedSize: Int64);
begin
open_arc;
open_ext;
Size := PackedSize;
state := NOHIST;
FirstCh := TRUE;
decomp(0);
close_ext();
close_arc;
end;
end.