-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompressUnit.pas
351 lines (298 loc) · 7.01 KB
/
CompressUnit.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
Unit CompressUnit;
{$I DefineType.pas}
// Модуль LZW паковщика и распаковщика
// требует модуля FileBuffer
interface
uses SysUtils,
{$IFDEF UNIX}
Linux,
{$ENDIF}
FileBuffer, ProgressUnit;
Procedure CompressProc(Var Data, Arc: TByteFile);
Procedure DecompressProc(Var Arc, Data: TByteFile);
Const
MaxBits=16;
MaxCode=65535;
TopChar=255;
ProgressStep=8192;
ClearDictValue=256;
FreezDict=ClearDictValue+1;
StepWordLength=ClearDictValue+2;
EndOfStream=ClearDictValue+3;
Var
MaxWordSize: Byte;
implementation
Type
DictR=Record
Up, Left, Right, Code: Word;
AddChar: Byte;
End;
TDecodeBuffer=Array of Byte;
Var
Overflow: Boolean;
BitSize, AddChar: Byte;
CurMaxCode, DictPos, MaxDictSize: Word;
DecodeBufferSize: Cardinal;
Dict: Array of DictR;
DecodeBuffer: TDecodeBuffer;
Function GetMaxDictSize(PowNum: Byte): Word;
const
BaseNum=2;
var
i: Byte;
s: Cardinal;
begin
s:=1;
For i:=1 to PowNum do
s:=s*BaseNum;
Dec(s);
GetMaxDictSize:=s;
end;
Procedure InitDict;
Begin
MaxDictSize:=GetMaxDictSize(MaxWordSize);
SetLength(Dict, MaxDictSize);
End;
// =========================Dictionary Begin==================================
Procedure AddNode(s: Word; C: Byte);
Var
dc: Word;
Begin
If DictPos<MaxDictSize then
Begin
If Dict[s].Up=ClearDictValue then
Begin
// Никого нет на этой ноде
Dict[s].Up:=DictPos;
Dict[DictPos].Up:=ClearDictValue;
Dict[DictPos].Left:=ClearDictValue;
Dict[DictPos].Right:=ClearDictValue;
Dict[DictPos].Code:=s;
Dict[DictPos].AddChar:=C;
End
Else
Begin
// Кто-то живёт тут
If C>Dict[Dict[s].Up].AddChar then
Begin
// Смотрим куда податься
// Вперёд
dc:=Dict[s].Up;
While Dict[dc].Right<>ClearDictValue do
dc:=Dict[dc].Right;
Dict[dc].Right:=DictPos;
Dict[DictPos].Up:=ClearDictValue;
Dict[DictPos].Left:=DictPos;
Dict[DictPos].Right:=ClearDictValue;
Dict[DictPos].Code:=s;
Dict[DictPos].AddChar:=C;
End
Else
Begin
// Назад
dc:=Dict[s].Up;
While Dict[dc].Left<>ClearDictValue do
dc:=Dict[dc].Left;
Dict[dc].Left:=DictPos;
Dict[DictPos].Up:=ClearDictValue;
Dict[DictPos].Left:=ClearDictValue;
Dict[DictPos].Right:=DictPos;
Dict[DictPos].Code:=s;
Dict[DictPos].AddChar:=C;
End;
End;
Inc(DictPos);
End;
End;
Function FindNode(s: Word; C: Byte): LongInt; inline;
Var
dc: Word;
Begin
Result:= - 1;
If Dict[s].Up<>ClearDictValue then
Begin
dc:=Dict[s].Up;
If Dict[dc].AddChar<>C then
Begin
If Dict[dc].AddChar<C then
Begin
dc:=Dict[dc].Right;
While dc<>ClearDictValue do
Begin
If Dict[dc].AddChar=C then
Begin
FindNode:=dc;
Exit;
End;
dc:=Dict[dc].Right;
End;
End;
If Dict[dc].AddChar>C then
Begin
dc:=Dict[dc].Left;
While dc<>ClearDictValue do
Begin
If Dict[dc].AddChar=C then
Begin
FindNode:=dc;
Exit;
End;
dc:=Dict[dc].Left;
End;
End;
End
Else
FindNode:=dc;
End;
End;
Procedure InitCoder;
Var
i: Word;
Begin
BitSize:=9;
Overflow:=False;
CurMaxCode:=GetMaxDictSize(BitSize);
For i:=0 to MaxDictSize-1 do
Begin
Dict[i].Code:=0;
Dict[i].AddChar:=0;
Dict[i].Up:=ClearDictValue;
Dict[i].Left:=ClearDictValue;
Dict[i].Right:=ClearDictValue;
End;
For i:=0 to TopChar do
Begin
Dict[i].Code:=ClearDictValue;
Dict[i].AddChar:=i;
End;
DictPos:=EndOfStream+1;
End;
// ======================Dictionary End=====================================
Procedure CompressProc(Var Data, Arc: TByteFile);
Var
Code: Word;
Index: LongInt;
Begin
InitDict;
BeginRead;
BeginWrite;
FSize:=GetFSize(Data);
InitCoder;
Code:=GetBytes(Data);
InitProgress(FSize);
While DataPos<FSize do
Begin
AddChar:=GetBytes(Data);
Index:=FindNode(Code, AddChar);
If Index<> - 1 then
Begin
Code:=Index;
End
Else
Begin
If DictPos<MaxCode then
AddNode(Code, AddChar)
Else
Overflow:=True;
If (Code>CurMaxCode)and(BitSize<MaxBits) then
Begin
BitWrite(Arc, StepWordLength, BitSize);
Inc(BitSize);
CurMaxCode:=GetMaxDictSize(BitSize);
End;
BitWrite(Arc, Code, BitSize);
Code:=AddChar;
If Overflow then
Begin
BitWrite(Arc, AddChar, BitSize);
BitWrite(Arc, ClearDictValue, BitSize);
InitCoder;
End;
End;
SetProgress(DataPos);
End;
BitWrite(Arc, Code, BitSize);
BitWrite(Arc, EndOfStream, BitSize);
EndBitWrite(Arc);
ResetBuffer(Arc);
End;
Procedure OutPutDecodeBuffer(Var F: TByteFile; Buff: TDecodeBuffer);
Var
le, i: Cardinal;
Begin
le:=Length(Buff);
For i:=0 to le-1 do
OutputBytes(F, Buff[i]);
End;
Procedure DecodeString(DeCode: Word); inline;
Var
dc: Word;
ReversC, ForwC: Cardinal;
DS: TDecodeBuffer;
Begin
dc:=DeCode;
DecodeBufferSize:=0;
Repeat
SetLength(DS, DecodeBufferSize+1);
DS[DecodeBufferSize]:=Dict[dc].AddChar;
dc:=Dict[dc].Code;
Inc(DecodeBufferSize);
Until dc=ClearDictValue;
SetLength(DecodeBuffer, DecodeBufferSize);
ReversC:=0;
For ForwC:=DecodeBufferSize-1 downto 0 do
Begin
DecodeBuffer[ReversC]:=DS[ForwC];
Inc(ReversC);
End;
End;
Procedure DecompressProc(Var Arc, Data: TByteFile);
Var
NewCode, OldCode: Word;
Begin
InitDict;
NewCode:=0;
InitCoder;
FSize:=GetFSize(Arc);
InitProgress(FSize);
OldCode:=BitRead(Arc, BitSize);
OutputBytes(Data, OldCode);
AddChar:=Byte(OldCode);
While NewCode<>EndOfStream do
Begin
NewCode:=BitRead(Arc, BitSize);
Case NewCode of
EndOfStream:
break;
ClearDictValue:
Begin
InitCoder;
OldCode:=BitRead(Arc, BitSize);
AddChar:=Byte(OldCode);
NewCode:=BitRead(Arc, BitSize);
End;
StepWordLength:
Begin
Inc(BitSize);
CurMaxCode:=GetMaxDictSize(BitSize);
NewCode:=BitRead(Arc, BitSize);
End;
End;
If DictPos<=NewCode then
Begin
DecodeString(OldCode);
Inc(DecodeBufferSize);
SetLength(DecodeBuffer, DecodeBufferSize);
DecodeBuffer[DecodeBufferSize-1]:=AddChar;
End
Else
DecodeString(NewCode);
OutPutDecodeBuffer(Data, DecodeBuffer);
AddChar:=DecodeBuffer[0];
AddNode(OldCode, AddChar);
OldCode:=NewCode;
SetProgress(DataPos);
End;
ResetBuffer(Data);
End;
end.