-
Notifications
You must be signed in to change notification settings - Fork 3
/
lzw.pas
314 lines (262 loc) · 7.4 KB
/
lzw.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
unit lzw;
interface
//Based on unlzw.c (v 0.15 1993/06/10 13:28:35) from Gzip v.1.
//Ported to Delphi by S. Fabricius
//Additional changes: https://github.com/PascalVault
//License: MIT
uses classes,sysutils,windows;
const
TBL_CLEAR = 256;
TBL_FIRST = TBL_CLEAR + 1;
LZW_MAGIC = $1f9d;
BITS = 16;
INIT_BITS = 9;
BIT_MASK = $1f;
HDR_EXTENDED = $20;
HDR_FREE = $40;
dBLOCK_MODE = $80;
LZW_RESERVED = $60;
INBUFSIZ = $8000;
INBUF_EXTRA = 64;
OUTBUFSIZ = $4000;
OUTBUF_EXTRA = 2048;
DIST_BUFSIZE = $8000;
WSIZE = $8000;
type
EUxCDecompession = class(Exception);
EUxCMagic = class(Exception);
{ TLZW }
TLZW = class
private
tab_prefix : array [0.. 1 shl BITS - 1] of word;
tab_suffix : array [0.. 2 * WSIZE - 1]of Byte;
outbuf : array [0.. INBUFSIZ + INBUF_EXTRA - 1] of Byte;
inbuf : array [0.. OUTBUFSIZ + OUTBUF_EXTRA - 1] of Byte;
de_stack : array[0.. DIST_BUFSIZE - 2] of byte;
inptr : integer;
maxbits : Byte;
bytes_in : integer;
bytes_out : integer;
insize : integer;
rsize : integer;
block_mode : Boolean;
inStr,outStr:TStream;
procedure memcpy(var src: array of byte; srcStart: Integer;var dst: array of byte; dstStart: Integer; len: integer);
procedure input(var b: array of Byte; var o: Integer; var c: Integer; n: Integer; m: Integer);
function get_byte(str: TStream): Byte;
public
constructor Create(InF, OutF: TStream);
function Decode(Bits: Byte; PackedSize: Int64): Boolean;
end;
implementation
procedure TLZW.memcpy(var src: array of byte; srcStart: Integer;var dst: array of byte; dstStart: Integer; len: integer);
var
i : integer;
buf : array of byte;
begin
setlength(buf,len);
for i := 0 to len - 1 do
buf[i]:=src[srcStart+i];
for i := 0 to len - 1 do
dst[dstStart+i]:=buf[i];
end;
procedure TLZW.input(var b: array of Byte; var o: Integer; var c: Integer; n: Integer; m: Integer);
var
p: integer;
begin
p := o shr 3;
c := (((b[p] and $FF) or ((b[p + 1] and $FF) shl 8) or ((b[p + 2] and $FF) shl 16)) shr (o and $7)) and m;
o := o + n;
end;
function TLZW.get_byte(str: TStream): Byte;
begin
str.Read(Result,1);
end;
constructor TLZW.Create(InF, OutF: TStream);
begin
block_mode := false;
inStr := InF;
outStr := OutF;
end;
function TLZW.Decode(Bits: Byte; PackedSize: Int64): Boolean;
var
stackp : Integer;
code : LongInt;
finchar : byte;
oldcode : LongInt;
incode : LongInt;
inbits : LongInt;
posbits : LongInt;
outpos : integer;
bitmask : Word;
free_ent : LongInt;
maxcode : LongInt;
maxmaxcode : LongInt;
n_bits : Integer;
magic : Word;
i, e, o : integer;
label ResetBuf;
begin
{
magic := get_byte(inStr) shl 8;
magic := magic + get_byte(inStr);
if magic <> LZW_MAGIC then
raise EUxCMagic.Create('Input not in compress format (read magic number 0x'+IntToHex(magic,4)+')');
maxbits:=get_byte(inStr);
}
maxbits := bits;
block_mode := (maxbits and dBLOCK_MODE) > 0;
maxbits := maxbits and BIT_MASK;
maxmaxcode := 1 shl maxbits;
if maxbits > BITS then
raise EUxCDecompession.Create('compressed with '+IntToStr(maxbits)+' bits, but can only handle '+IntToStr(BITS)+' bits');
inptr := 0;
insize := 0;
rsize := -1;
n_bits := INIT_BITS;
maxcode := (1 shl n_bits) - 1;
bitmask := maxcode;
oldcode := -1;
finchar := 0;
outpos := 0;
bytes_in := 3;
bytes_out := 0;
posbits := inptr shl 3;
if block_mode then
free_ent := TBL_FIRST
else
free_ent := 256;
FillChar(tab_prefix,256,$0);
for code:=255 downto 0 do
tab_suffix[code]:=code;
while rsize <> 0 do
begin
ResetBuf :
o := posbits shr 3;
e := insize - o;
for i := 0 to e - 1 do
inbuf[i] := inbuf[i+o];
insize := e;
posbits := 0;
if insize < INBUF_EXTRA then
begin
rsize:=inStr.Read(inbuf[insize],INBUFSIZ);
insize := insize + rsize;
bytes_in := bytes_in + rsize;
end;
if rsize > 0 then
inbits := (insize - insize mod n_bits) shl 3
else
inbits := (insize shl 3) - (n_bits - 1);
while inbits > posbits do
begin
if free_ent > maxcode then
begin
posbits := (posbits - 1) + ((n_bits shl 3) - (posbits - 1 + (n_bits shl 3)) mod (n_bits shl 3));
inc(n_bits);
if n_bits = maxbits then
maxcode := maxmaxcode
else
maxcode := (1 shl n_bits) - 1;
bitmask := (1 shl n_bits) - 1;
goto ResetBuf;
end;
input(inbuf,posbits,code,n_bits,bitmask);
if oldcode = -1 then
begin
if code >= 256 then
raise EUxCDecompession.Create('corrupt input: code=' + intToStr(code) + ' > 255');
finchar := code;
oldcode := code;
outbuf[outpos] := finchar;
inc(outpos);
continue;
end;
// handle CLEAR code
if (code = TBL_CLEAR) and block_mode then
begin
FillChar(tab_prefix,256,$0);
free_ent := TBL_FIRST - 1;
posbits := (posbits - 1) + ((n_bits shl 3) - (posbits - 1 + (n_bits shl 3)) mod (n_bits shl 3));
n_bits := INIT_BITS;
maxcode := (1 shl n_bits) - 1;
bitmask := maxcode;
goto ResetBuf;
end;
// setup
incode := code;
stackp := Length(de_stack);
// Handle KwK case
if code >= free_ent then
begin
if code > free_ent then
begin
if outpos > 0 then
begin
outStr.write(outbuf[0],outpos);
bytes_out := bytes_out + outpos;
end;
raise EUxCDecompession.Create('corrupt input: code=' + intToStr(code) + ', free_ent=' + intToStr(free_ent) + ', Bytes in=' + intToStr(bytes_in) + ', Bytes out=' + intToStr(bytes_out));
end;
dec(stackp);
de_stack[stackp] := finchar;
code := oldcode;
end;
// Generate output characters in reverse order
while code >= 256 do
begin
dec(stackp);
de_stack[stackp] := tab_suffix[code];
code := tab_prefix[code];
end;
finchar := tab_suffix[code];
dec(stackp);
de_stack[stackp] := finchar;
// And put them out in forward order
i := length(de_stack) - stackp;
if outpos + i >= OUTBUFSIZ then
begin
while i <> -1 do
begin
if i > OUTBUFSIZ - outpos then i := OUTBUFSIZ - outpos;
if i > 0 then
begin
memcpy(de_stack,stackp,outbuf,outpos,i);
outpos := outpos + i;
end;
if outpos >= OUTBUFSIZ then
begin
outStr.write(outbuf[0],outpos);
bytes_out := bytes_out + outpos;
outpos := 0;
end;
stackp := stackp + i;
i := length(de_stack) - stackp;
if i = 0 then i:=-1;
end;
end
else
begin
memcpy(de_stack,stackp,outbuf,outpos,i);
outpos := outpos + i;
end;
// generate new entry in table
code := free_ent;
if free_ent < maxmaxcode then
begin
tab_prefix[code] := oldcode;
tab_suffix[code] := finchar;
free_ent := code + 1;
end;
// Remember previous code
oldcode := incode;
end;
end;
if outpos > 0 then
begin
outStr.write(outbuf[0],outpos);
bytes_out := bytes_out + outpos;
end;
Result := true;
end;
end.