-
Notifications
You must be signed in to change notification settings - Fork 3
/
reduce.pas
308 lines (268 loc) · 7.8 KB
/
reduce.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
unit reduce;
{$mode delphi}{$H+}
//Code by TurboPower Software
//Taken from library TurboPower Abbrevia
//License: MPL
interface
uses
Classes, SysUtils, AbSWStm;
const
AbBufferSize = 32768;
AbLastDisk = -1;
AbLastImage = -1;
type
TAbFollower = {used to expand reduced files}
packed record
Size : Byte; {size of follower set}
FSet : array[0..31] of Byte; {follower set}
end;
PAbFollowerSets = ^TAbFollowerSets;
TAbFollowerSets = array[0..255] of TAbFollower;
PAbByteArray4K = ^TAbByteArray4K;
TAbByteArray4K = array[1..4096] of Byte;
PAbByteArray = ^TAbByteArray;
TAbByteArray = array[0..65535-1] of Byte;
{ TReduce }
TReduce = class
private
FReduction: Byte;
FBitsLeft: Byte;
FInPos : Integer; {current position in input buffer}
FInCnt : Integer; {number of bytes in input buffer}
FCurByte : Byte; {current input byte}
FInBuf : TAbByteArray4K;
FInEof : Boolean; {set when stream read returns 0}
FZStream : TStream;
FOutBuf : PAbByteArray; {output buffer}
FOutSent : Integer; {number of bytes sent to output buffer}
FOutPos : Cardinal; {current position in output buffer}
FBitSValid : Byte; {Number of valid bits}
FUnCompressedSize : Integer;
FOutWriter : TStream;
FOutStream: TStream;
procedure uzUnReduce;
procedure uzFlushOutBuf;
procedure uzWriteByte(B : Byte);
function uzReadBits(Bits : Byte) : Integer;
procedure uzReadNextPrim;
public
constructor Create(InStream, OutStream: TStream; Reduction: Byte);
procedure Decode(UnpackedSize: Cardinal);
end;
implementation
constructor TReduce.Create(InStream, OutStream: TStream; Reduction: Byte);
begin
FBitsLeft := 0;
FCurByte := 0;
FInEof := False;
FInCnt := 0;
FOutSent := 0;
FOutPos := 0;
FOutBuf := AllocMem( AbBufferSize );
FInPos := 1+SizeOf(FInBuf);
FZStream := InStream;
FOutStream := OutStream;
if Reduction < 1 then Reduction := 1
else if Reduction > 4 then Reduction := 4;
FReduction := Reduction;
end;
procedure TReduce.Decode(UnpackedSize: Cardinal);
begin
FUnCompressedSize := UnpackedSize;
FOutWriter := TabSlidingWindowStream.Create(FOutStream);
uzUnReduce;
uzFlushOutBuf;
FOutWriter.Free;
end;
procedure TReduce.uzReadNextPrim;
begin
FInCnt := FZStream.Read( FInBuf, sizeof( FInBuf ) );
FInEof := FInCnt = 0;
{load first byte in buffer and set position counter}
FCurByte := FInBuf[1];
FInPos := 2;
end;
function TReduce.uzReadBits(Bits : Byte) : Integer;
{-Read the specified number of bits}
var
SaveCurByte, Delta, SaveBitsLeft : Byte;
begin
{read next byte if we're out of bits}
if FBitsLeft = 0 then begin
{do we still have a byte buffered?}
if FInPos <= FInCnt then begin
{get next byte out of buffer and advance position counter}
FCurByte := FInBuf[FInPos];
Inc(FInPos);
end
{are there any left to read?}
else
uzReadNextPrim;
FBitsLeft := 8;
end;
if ( Bits < FBitsLeft ) then begin
Dec( FBitsLeft, Bits );
Result := ((1 shl Bits) - 1) and FCurByte;
FCurByte := FCurByte shr Bits;
end
else if ( Bits = FBitsLeft ) then begin
Result := FCurByte;
FCurByte := 0;
FBitsLeft := 0;
end
else begin
SaveCurByte := FCurByte;
SaveBitsLeft := FBitsLeft;
{number of additional bits that we need}
Delta := Bits - FBitsLeft;
{do we still have a byte buffered?}
if FInPos <= FInCnt then begin
{get next byte out of buffer and advance position counter}
FCurByte := FInBuf[FInPos];
Inc(FInPos);
end
{are there any left to read?}
else
uzReadNextPrim;
FBitsLeft := 8;
Result := ( uzReadBits( Delta ) shl SaveBitsLeft ) or SaveCurByte;
end;
end;
procedure TReduce.uzFlushOutBuf;
{-flushes the output buffer}
begin
if (FOutPos <> 0) then begin
FOutWriter.Write( FOutBuf^, FOutPos );
Inc( FOutSent, FOutPos );
FOutPos := 0;
end;
end;
procedure TReduce.uzWriteByte(B : Byte);
{-Write one byte to the output buffer}
begin
FOutBuf^[FOutPos] := B;
inc(FOutPos);
if (FOutPos = AbBufferSize) or
(Integer(FOutPos) + FOutSent = FUncompressedSize) then
uzFlushOutBuf;
end;
procedure TReduce.uzUnReduce;
const
FactorMasks : array[1..4] of Byte = ($7F, $3F, $1F, $0F);
DLE = 144;
var
C, Last : Byte;
OpI : LongInt;
I, J, Sz : Integer;
D : Word;
SPos : LongInt;
MyByte : Byte;
Factor : Byte; {reduction Factor}
FactorMask : Byte; {bit mask to use based on Factor}
Followers : PAbFollowerSets; {array of follower sets}
State : Integer; {used while processing reduced files}
V : Integer; {"}
Len : Integer; {"}
function BitsNeeded( i : Byte ) : Word;
begin
dec( i );
Result := 0;
repeat
inc( Result );
i := i shr 1;
until i = 0;
end;
begin
GetMem(Followers, SizeOf(TAbFollowerSets));
try
Factor := FReduction;//Ord( FCompressionMethod ) - 1;
FactorMask := FactorMasks[Factor];
State := 0;
C := 0;
V := 0;
Len := 0;
D := 0;
{load follower sets}
for I := 255 downto 0 do begin
Sz := uzReadBits(6);
Followers^[I].Size := Sz;
Dec(Sz);
for J := 0 to Sz do
Followers^[I].FSet[J] := uzReadBits(8);
end;
while (not FInEof) and ((FOutSent + LongInt(FOutPos)) < FUncompressedSize) do begin
Last := C;
with Followers^[Last] do
if Size = 0 then
C := uzReadBits(8)
else begin
C := uzReadBits(1);
if C <> 0 then
C := uzReadBits(8)
else
C := FSet[uzReadBits(BitsNeeded(Size))];
end;
if FInEof then
Exit;
case State of
0 :
if C <> DLE then
uzWriteByte(C)
else
State := 1;
1 :
if C <> 0 then begin
V := C;
Len := V and FactorMask;
if Len = FactorMask then
State := 2
else
State := 3;
end
else begin
uzWriteByte(DLE);
State := 0;
end;
2 :
begin
Inc(Len, C);
State := 3;
end;
3 :
begin
case Factor of
1 : D := (V shr 7) and $01;
2 : D := (V shr 6) and $03;
3 : D := (V shr 5) and $07;
4 : D := (V shr 4) and $0f;
else
raise Exception.Create('Wrong factor');
end;
{Delphi raises compiler Hints here, saying D might
be undefined... If Factor is not in [1..4], the
exception gets raised, and we never execute the following
line}
OpI := (FOutSent + LongInt(FOutPos))-(Swap(D)+C+1);
for I := 0 to Len+2 do begin
if OpI < 0 then
uzWriteByte(0)
else if OpI >= FOutSent then
uzWriteByte(FOutBuf[OpI - FOutSent])
else begin
SPos := FOutWriter.Position;
FOutWriter.Position := OpI;
FOutWriter.Read( MyByte, 1 );
FOutWriter.Position := SPos;
uzWriteByte(MyByte);
end;
Inc(OpI);
end;
State := 0;
end;
end;
end;
finally
FreeMem(Followers, SizeOf(Followers^));
end;
end;
end.