-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpack.dpr
556 lines (485 loc) · 12 KB
/
pack.dpr
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
program pack;
{$APPTYPE CONSOLE}
{$R+}
uses
SysUtils;
const
MAX_SIZE = $10000 - 8;
type
BufPtr = ^Buffer;
Buffer = array[0..MAX_SIZE - 1] of Char;
var
Buf1,
Buf2: BufPtr;
InputName,
OutputName: string;
fInput,
fOutput: File;
InputSize,
OutputSize: LongInt;
NumRead: Integer;
procedure PackIt;
type
Method =
(mdCopy, { = 0 }
mdStore0, { = 1 }
mdStoreB, { = 2 }
mdStoreW, { = 3 }
mdStoreD, { = 4 }
mdStoreSP, { = 5 }
mdSkip, { = 6 }
mdNone); { = 7 }
var
CurPos: Word;
CurMethod,
MaxMethod: Method;
MaxGain: Integer;
MaxBytes: Word;
Buf: string[255];
//BufSize: Byte absolute Buf;
Progress: string[255];
BytesLeft,
L: LongInt;
i, j: Word;
CopyCount,
CopyStart: Word;
function BufSize: Integer;
begin
result := Length (Buf);
end;
procedure WriteStr (S: string);
var
i: Integer;
begin
for i := 1 to Length (S) do
Buf2^[OutputSize + i - 1] := S[i];
Inc (OutputSize, Length (S));
end;
function WordStr (W: Word): string;
begin
WordStr := Chr (Lo (W)) + Chr (Hi (W));
end;
procedure CheckStore0;
var
Count: Integer;
begin
Count := 0;
while (Buf1^[CurPos + Count] = #0) and (Count < BufSize) do
Inc (Count);
if Count > MaxGain + (0) then
begin
MaxMethod := mdStore0;
MaxBytes := Count;
MaxGain := Count - (0);
end;
end;
procedure CheckStoreSP;
var
Count: Integer;
begin
Count := 0;
while (Buf1^[CurPos + Count] = ' ') and (Count < BufSize) do
Inc (Count);
if Count > MaxGain + (0) then
begin
MaxMethod := mdStoreSP;
MaxBytes := Count;
MaxGain := Count - (0);
end;
end;
procedure CheckStoreB;
var
Count: Integer;
begin
Count := 0;
while (Buf1^[CurPos + Count] = Buf[1]) and (Count < BufSize) do
Inc (Count);
if Count > MaxGain + (1) then
begin
MaxMethod := mdStoreB;
MaxBytes := Count;
MaxGain := Count - (1);
end;
end;
procedure CheckStoreW;
var
Count: Integer;
begin
Count := 0;
while (Buf1^[CurPos + Count] = Buf[1])
and (Buf1^[CurPos + Count + 1] = Buf[2])
and (Count < BufSize) do
Inc (Count, 2);
if Count > MaxGain + (2) then
begin
MaxMethod := mdStoreW;
MaxBytes := Count;
MaxGain := Count - (2);
end;
end;
procedure CheckStoreD;
var
Count: Integer;
begin
Count := 0;
while (Count + 3 < BufSize)
and (Buf1^[CurPos + Count] = Buf[1])
and (Buf1^[CurPos + Count + 1] = Buf[2])
and (Buf1^[CurPos + Count + 2] = Buf[3])
and (Buf1^[CurPos + Count + 3] = Buf[4])
do
Inc (Count, 4);
if Count > MaxGain + (4) then
begin
MaxMethod := mdStoreD;
MaxBytes := Count;
MaxGain := Count - (4);
end;
end;
procedure CheckSkip;
var
Count: Integer;
S: string;
//L: byte absolute S;
function L: Integer;
begin
result := Length (S);
end;
begin
Count := 0;
S := '';
while (Buf1^[CurPos + Count] = Buf[1])
and ((L <= 2) or (S[L] <> S[L - 1]) or (S[L - 1] <> S[L - 2]))
and (Count < BufSize) do
begin
Inc (Count, 2);
S := S + Buf1^[CurPos + Count + 3];
end;
if Count > MaxGain + (2 + Count div 2) then
begin
MaxMethod := mdSkip;
MaxBytes := Count;
MaxGain := Count - (2 + Count div 2);
end;
end;
procedure WriteStore0;
begin
if MaxBytes <= $1F then
WriteStr (Chr (Byte (mdStore0) shl 5 + MaxBytes))
else
WriteStr (Chr (Byte (mdStore0) shl 5 + 0) + WordStr (MaxBytes));
end;
procedure WriteStoreSP;
begin
if MaxBytes <= $1F then
WriteStr (Chr (Byte (mdStoreSP) shl 5 + MaxBytes))
else
WriteStr (Chr (Byte (mdStoreSP) shl 5 + 0) + WordStr (MaxBytes));
end;
procedure WriteStoreB;
begin
if MaxBytes <= $1F then
WriteStr (Chr (Byte (mdStoreB) shl 5 + MaxBytes) + Buf[1])
else
WriteStr (Chr (Byte (mdStoreB) shl 5 + 0) + WordStr (MaxBytes) + Buf[1]);
end;
procedure WriteStoreW;
begin
if MaxBytes div 2 <= $1F then
WriteStr (Chr (Byte (mdStoreW) shl 5 + MaxBytes div 2) + Buf[1] + Buf[2])
else
WriteStr (Chr (Byte (mdStoreW) shl 5 + 0) + WordStr (MaxBytes div 2) + Buf[1] + Buf[2]);
end;
procedure WriteStoreD;
begin
if MaxBytes div 4 <= $1F then
WriteStr (Chr (Byte (mdStoreD) shl 5 + MaxBytes div 4) + Buf[1] + Buf[2] + Buf[3] + Buf[4])
else
WriteStr (Chr (Byte (mdStoreD) shl 5 + 0) + WordStr (MaxBytes div 4) + Buf[1] + Buf[2] + Buf[3] + Buf[4]);
end;
procedure WriteSkip;
var
i: Integer;
begin
if MaxBytes div 2 < $1F then
WriteStr (Chr (Byte (mdSkip) shl 5 + MaxBytes div 2))
else
WriteStr (Chr (Byte (mdSkip) shl 5 + 0) + WordStr (MaxBytes div 2));
WriteStr (Buf[1]);
for i := 1 to MaxBytes div 2 do
WriteStr (Buf[i * 2]);
end;
procedure JustCopyChar;
var
i: Word;
begin
if CurMethod <> mdCopy then
begin
CopyStart := OutputSize;
CopyCount := 0;
WriteStr (Chr (Byte (mdCopy) shl 5));
CurMethod := mdCopy;
end;
if CopyCount = $1F then
begin
for i := OutputSize downto CopyStart + 1 do
Buf2^[i + 2] := Buf2^[i];
Buf2^[CopyStart] := Chr (Byte (mdCopy) shl 5);
Inc (OutputSize, 2);
end;
WriteStr (Buf[1]);
Inc (CopyCount);
if CopyCount > $1F then
begin
Buf2^[CopyStart + 1] := Chr (Lo (CopyCount));
Buf2^[CopyStart + 2] := Chr (Hi (CopyCount));
end
else
Buf2^[CopyStart] := Chr (Byte (mdCopy) shl 5 + CopyCount);
end;
function MakeString (n: Integer; c: Char): string;
var
i: Integer;
s: string;
begin
s := '';
for i := 1 to n do
result := result + c;
MakeString := s;
end;
procedure CopyToBuf (n: Integer);
var
i: Integer;
begin
Buf := '';
for i := 1 to n do
Buf := Buf + Buf1^[CurPos + i - 1];
end;
begin { Packit }
CurPos := 0;
OutputSize := 0;
CopyCount := 0;
CopyStart := $FFFF;
CurMethod := mdNone;
repeat
i := InputSize - CurPos;
BytesLeft := i;
j := SizeOf (Buf) - 1;
if i > j then
i := j;
//Move (Buf1^[CurPos], Buf[1], i);
//BufSize := i;
CopyToBuf (i);
MaxMethod := mdNone;
MaxGain := 0;
MaxBytes := 0;
CheckStore0;
CheckStoreSP;
CheckStoreB;
CheckStoreW;
CheckStoreD;
CheckSkip;
if MaxGain = 0 then
JustCopyChar
else
begin
case MaxMethod of
mdStore0:
WriteStore0;
mdStoreB:
WriteStoreB;
mdStoreW:
WriteStoreW;
mdStoreD:
WriteStoreD;
mdStoreSP:
WriteStoreSP;
mdSkip:
WriteSkip;
end;
CurMethod := MaxMethod;
Inc (CurPos, MaxBytes - 1);
end;
Inc (CurPos);
//FillChar (Progress, SizeOf (Progress), '.');
//Progress[0] := #50;
Progress := MakeString (50, '.');
L := CurPos;
j := 100 * L div InputSize;
for i := 1 to j div 2 do
Progress[i] := '+';
L := OutputSize;
for i := 1 to (100 * L div InputSize) div 2 do
Progress[i] := '#';
Write (#13, Progress, ' ', j: 3, '%');
until CurPos >= InputSize;
Write (#13, Progress, ' ', (100 * L / InputSize): 1:1, '% ');
end;
{ ===== UNPACK.INC ======================================================== }
{ Unpack routines - use PACK.EXE to pack data files }
const
mdCopy = 0;
mdStore0 = 1;
mdStoreB = 2;
mdStoreW = 3;
mdStoreD = 4;
mdStoreSP = 5;
mdSkip = 6;
type
ByteArrayPtr = ^ByteArray;
ByteArray = array[0..$7FFE] of Byte;
var
CurBlock: ByteArrayPtr;
CurPos: Word;
CurByte: Byte;
CurMethod: Byte;
CurCount: Word;
CurData: LongInt;
CurDataAr: array[0..3] of Byte absolute CurData;
procedure Unpack (P: Pointer);
begin
CurBlock := P;
CurPos := 0;
CurMethod := $FF;
end;
function GetNextByte: Byte;
var
b: Byte;
begin
if CurMethod = $FF then
begin
b := CurBlock^[CurPos];
Inc (CurPos);
CurMethod := b shr 5;
CurCount := b and $1F;
if CurCount = 0 then
begin
CurCount := CurBlock^[CurPos] + CurBlock^[CurPos + 1] shl 8;
Inc (CurPos, 2);
end;
case CurMethod of
mdStoreB,
mdSkip:
begin
CurData := CurBlock^[CurPos];
Inc (CurPos);
CurByte := 0;
end;
mdStoreW:
begin
CurData := CurBlock^[CurPos] + CurBlock^[CurPos + 1] shl 8;
Inc (CurPos, 2);
CurByte := 0;
end;
mdStoreD:
begin
Move (CurBlock^[CurPos], CurData, SizeOf (CurData));
Inc (CurPos, 4);
CurByte := 0;
end;
end;
end;
case CurMethod of
mdCopy:
begin
GetNextByte := CurBlock^[CurPos];
Inc (CurPos);
end;
mdStore0:
GetNextByte := 0;
mdStoreSP:
GetNextByte := Ord (' ');
mdStoreB:
GetNextByte := CurData;
mdStoreW:
begin
if CurByte = 0 then
GetNextByte := CurData and $FF
else
GetNextByte := CurData shr 8;
CurByte := (CurByte + 1) mod 2;
Inc (CurCount, CurByte);
end;
mdStoreD:
begin
GetNextByte := CurDataAr[CurByte];
CurByte := (CurByte + 1) mod 4;
Inc (CurCount, Byte (CurByte <> 0));
end;
mdSkip:
begin
if CurByte = 0 then
GetNextByte := CurData
else
begin
GetNextByte := CurBlock^[CurPos];
Inc (CurPos);
end;
CurByte := (CurByte + 1) mod 2;
Inc (CurCount, CurByte);
end;
end;
Dec (CurCount);
if CurCount = 0 then
CurMethod := $FF;
end;
{ ========================================================================= }
var
i, j: Word;
begin
if ParamCount < 2 then
begin
WriteLn ('Pack 1.0 (C) Copyright 1995-2017, by Mike Wiering, The Netherlands.');
WriteLn;
WriteLn (' pack <inputfile> <outputfile>');
Halt (1);
end;
InputName := ParamStr (1);
AssignFile (fInput, InputName);
Reset (fInput, 1);
if IOResult <> 0 then
begin
WriteLn ('File not found: "', InputName, '".');
Halt (1);
end;
(*
if MemAvail < SizeOf (Buf1^) + SizeOf (Buf2^) then
begin
WriteLn ('Out of memory.');
Halt (1);
end;
*)
New (Buf1);
New (Buf2);
BlockRead (fInput, Buf1^, SizeOf (Buf1^), NumRead);
InputSize := NumRead;
CloseFile (fInput);
if InputSize = 0 then
begin
WriteLn ('Input file empty.');
Halt (1);
end;
PackIt;
WriteLn;
WriteLn ('Old size: ', InputSize, ' New size: ', OutputSize,
' Gain: ', InputSize - OutputSize, ' (',
(((InputSize - OutputSize) / InputSize) * 100):1:1, '%)');
OutputName := ParamStr (2);
AssignFile (fOutput, OutputName);
ReWrite (fOutput, 1);
BlockWrite (fOutput, Buf2^, OutputSize);
CloseFile (fOutput);
{
UnPack (buf2);
Assign (fOutput, OutputName);
ReWrite (fOutput, 1);
for i := 1 to InputSize do
begin
j := GetNextByte;
BlockWrite (fOutput, j, 1);
end;
Close (fOutput);
}
Dispose (Buf2);
Dispose (Buf1);
end.