-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLemDosCmp.pas
172 lines (147 loc) · 3.84 KB
/
LemDosCmp.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
{-------------------------------------------------------------------------------
This unit contains:
o the dos decompression algorithm, based on the c-code of ccexplore.
o a dos compression algorithm, based on free basic code of Mindless
-------------------------------------------------------------------------------}
unit LemDosCmp;
interface
uses
Classes, SysUtils;
procedure DecompressDat(aSrc: TStream; aDst: TStream);
implementation
type
TCompressionHeaderRec = packed record
BitCnt : Byte;
Checksum : Byte;
DecompressedSize : LongWord; // Note: These are in big endian, need to be reversed!
CompressedSize : LongWord;
end;
procedure DecompressDat(aSrc: TStream; aDst: TStream);
var
CurBit: Integer;
MaxBit: Integer;
CurByte: PByte;
InTempStream, OutTempStream: TMemoryStream;
procedure Error;
begin
raise Exception.Create('DecompressDat encountered an unexpected end of data.');
end;
procedure DoByteOrderSwap(var aLongword: LongWord);
var
P1, P2: ^Byte;
TempLW: LongWord;
i: Integer;
begin
TempLW := aLongword;
P1 := @aLongword;
for i := 0 to 2 do
Inc(P1);
P2 := @TempLW;
for i := 0 to 3 do
begin
P1^ := P2^;
Dec(P1);
Inc(P2);
end;
end;
function GetValue(aBits: Integer): Cardinal;
var
Flag: Byte;
begin
Result := 0;
while aBits > 0 do
begin
Dec(aBits);
Flag := 1 shl CurBit;
if CurByte^ and Flag <> 0 then
Inc(Result, 1 shl aBits);
Inc(CurBit);
if CurBit > MaxBit then
begin
CurBit := 0;
Dec(CurByte);
MaxBit := 7;
end;
end;
end;
var
Header: TCompressionHeaderRec;
DstByte, SrcByte: PByte;
CopyCount: Integer;
ReadCount: Integer;
// InTempStream, OutTempStream: TMemoryStream;
Operation: Integer;
begin
if aSrc.Read(Header, SizeOf(TCompressionHeaderRec)) <> SizeOf(TCompressionHeaderRec) then
Error;
DoByteOrderSwap(Header.DecompressedSize);
DoByteOrderSwap(Header.CompressedSize);
Dec(Header.CompressedSize, 10);
InTempStream := TMemoryStream.Create;
OutTempStream := TMemoryStream.Create;
try
if InTempStream.CopyFrom(aSrc, Header.CompressedSize) <> Header.CompressedSize then
Error;
OutTempStream.Size := Header.DecompressedSize;
DstByte := OutTempStream.Memory;
Inc(DstByte, OutTempStream.Size-1);
CurByte := InTempStream.Memory;
Inc(CurByte, InTempStream.Size-1);
CurBit := 0;
if Header.BitCnt = 0 then
begin
Dec(CurByte);
MaxBit := 7;
end else
MaxBit := Header.BitCnt - 1;
while Integer(CurByte) >= Integer(InTempStream.Memory) do
begin
if GetValue(1) = 0 then
Operation := GetValue(1)
else
Operation := GetValue(2) + 4;
CopyCount := 0;
ReadCount := 0;
SrcByte := DstByte;
case Operation of
0: ReadCount := GetValue(3) + 1;
1: begin
CopyCount := 2;
Inc(SrcByte, GetValue(8) + 1);
end;
4: begin
CopyCount := 3;
Inc(SrcByte, GetValue(9) + 1);
end;
5: begin
CopyCount := 4;
Inc(SrcByte, GetValue(10) + 1);
end;
6: begin
CopyCount := GetValue(8) + 1;
Inc(SrcByte, GetValue(12) + 1);
end;
7: ReadCount := GetValue(8) + 9;
end;
while CopyCount > 0 do
begin
DstByte^ := SrcByte^;
Dec(DstByte);
Dec(SrcByte);
Dec(CopyCount);
end;
while ReadCount > 0 do
begin
DstByte^ := GetValue(8);
Dec(DstByte);
Dec(ReadCount);
end;
end;
OutTempStream.Position := 0; // just in case
aDst.CopyFrom(OutTempStream, OutTempStream.Size);
finally
InTempStream.Free;
OutTempStream.Free;
end;
end;
end.