-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEXPAND.PAS
More file actions
338 lines (315 loc) · 8.48 KB
/
EXPAND.PAS
File metadata and controls
338 lines (315 loc) · 8.48 KB
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2025
@website(https://www.gladir.com/msdos0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program EXPAND;
{$A-}
{$M 32768,0,640000}
Uses
DOS;
Const
BUFFER_SIZE=16384;
DICT_SIZE=4096;
MAX_DICT_SIZE=DICT_SIZE-1;
SZDD_SIGNATURE:Array[0..3]of Char='SZDD';
KWAJ_SIGNATURE:Array[0..3]of Char='KWAJ';
KWAJ_METHOD_NONE = 0;
KWAJ_METHOD_LZ77 = 1;
KWAJ_METHOD_LZ77_RLE = 2;
KWAJ_METHOD_HUFFMAN = 3;
KWAJ_METHOD_MSCOMP = $88; { Ajout de la m‚thode 136 }
Type
TDictEntry=Record
Prefix:Word;
Suffix:Byte;
End;
TDictBuffer=Array[0..MAX_DICT_SIZE] of Byte;
TKWAJHeader=Record
Signature:Array[0..3] of Char; { 'KWAJ' }
Method:Byte; { M‚thode de compression }
DictionarySize:Byte; { Taille du dictionaire }
Flags:Word; { Drapeaux de compression }
OriginalSize:LongInt; { Taille des donn�es d�compress�s }
End;
TSZDDHeader=Record
Signature:Array[0..3] of Char;
Algorithm:Byte;
DollarChar:Char;
End;
Var
DecompressMethod:(methodNone,methodDict,methodSZDD,methodKWAJ);
SourceFile,DestFile:File;
KWAJHeader:TKWAJHeader;
SZDDHeader:TSZDDHeader;
DictCount:Word;
Dictionary:Array[0..DICT_SIZE-1] of TDictEntry;
Buffer:Array[0..BUFFER_SIZE-1] of Byte Absolute Dictionary;
Function IsCompressedMS(Var f:File):Boolean;
Var
Signature:Word;
Begin
IsCompressedMS:=False;
Seek(f,0);
BlockRead(f,Signature,2);
If(Signature=$4D53)Then Begin { Signature 'MS' }
IsCompressedMS:=True;
DecompressMethod:=methodDict;
End;
Seek(f,0);
End;
Function IsCompressedSZDD(Var f:File):Boolean;Begin
IsCompressedSZDD:=False;
Seek(f,0);
BlockRead(f,SZDDHeader,SizeOf(SZDDHeader));
If(SZDDHeader.Signature='SZDD')Then Begin
IsCompressedSZDD:=True;
DecompressMethod:=methodSZDD;
End;
Seek(f,0);
End;
Function IsCompressedKWAJ(Var f:File):Boolean;
Var
Signature:Array[0..3] of Char;
Begin
IsCompressedKWAJ:=False;
Seek(f,0);
BlockRead(f,Signature, 4);
If(Signature='KWAJ')Then Begin
IsCompressedKWAJ:=True;
DecompressMethod:=methodKWAJ;
End;
Seek(f,0);
End;
Procedure InitDictionary;
Var
i:Word;
Begin
DictCount:=256;
For i:=0 to 255 do Begin
Dictionary[i].prefix:=$FFFF;
Dictionary[i].suffix:=i;
End;
End;
Procedure ExpandFile(Var Source,Dest:File);
Var
InBuf,OutBuf:Array[0..BUFFER_SIZE-1] of Byte;
InSize,OutSize:Word;
Code,OldCode,NewCode:Word;
Char:Byte;
Begin
InitDictionary;
BlockRead(Source,InBuf,2); { Saute la signature }
While Not Eof(Source) do Begin
BlockRead(Source,Code,2,InSize);
If InSize=0 Then Break;
If Code<DictCount Then Begin
{ Code existes dans le dictionaire }
NewCode:=Code;
While NewCode<256 do Begin
OutBuf[OutSize]:=Dictionary[NewCode].suffix;
Inc(OutSize);
If OutSize=BUFFER_SIZE Then Begin
BlockWrite(Dest, OutBuf, OutSize);
OutSize:=0;
End;
NewCode:=Dictionary[NewCode].prefix;
End;
End;
If OldCode<>$FFFF Then Begin
{ Ajout d'une entr‚e dans le dictionnaire }
Dictionary[DictCount].prefix:=OldCode;
Dictionary[DictCount].suffix:=Char;
Inc(DictCount);
End;
OldCode:=Code;
Char:=Dictionary[Code].suffix;
End;
If OutSize>0 Then BlockWrite(Dest,OutBuf,OutSize);
End;
Procedure CopyUncompressed(Var Source,Dest:File);
Var
Buffer:Array[0..BUFFER_SIZE-1] of Byte;
BytesRead:Word;
Begin
While Not Eof(Source)do Begin
BlockRead(Source,Buffer,BUFFER_SIZE,BytesRead);
If BytesRead>0 Then BlockWrite(Dest,Buffer,BytesRead);
End;
End;
Procedure DecompressLZ77(Var Source,Dest:file);
Var
InBuf:Array[0..BUFFER_SIZE-1] of Byte;
OutBuf:Array[0..BUFFER_SIZE-1] of Byte;
Dictionary:TDictBuffer;
InPos,OutPos:LongInt;
DictPos:Word;
InSize:Word;
Control:Byte;
Len,Offset:LongInt;
i:LongInt;
Begin
DictPos := 0;
While not Eof(Source)do Begin
BlockRead(Source, InBuf, BUFFER_SIZE, InSize);
If InSize = 0 Then Break;
InPos:=0;
OutPos:=0;
While InPos < InSize do Begin
Control := InBuf[InPos];
Inc(InPos);
If Control and $80<>0 Then Begin
Len:=((Control and $7F) shr 4) + 3;
Offset:=((Control and $0F) shl 8);
If InPos>=InSize Then Break; { V‚rification de s‚curit‚ }
Offset:=Offset+InBuf[InPos];
Inc(InPos);
{ Protection contre les d‚bordements }
If(Offset>DictPos)or(Offset>MAX_DICT_SIZE)Then Offset:=DictPos;
For i:=0 to Len-1 do Begin
If OutPos >= BUFFER_SIZE then Break; { Protection de tampon de sortie }
{ S‚curisation de l'accŠs au dictionnaire }
If(DictPos - Offset + i) >= 0 Then
OutBuf[OutPos]:=Dictionary[(DictPos - Offset + i) and MAX_DICT_SIZE]
Else
OutBuf[OutPos]:=0;
{ Protection ‚criture dictionnaire }
If DictPos<=MAX_DICT_SIZE Then Dictionary[DictPos]:=OutBuf[OutPos];
Inc(DictPos);
DictPos := DictPos and MAX_DICT_SIZE;
Inc(OutPos);
End;
End
else
Begin
Len:=Control+1;
{ Protection contre d‚bordement de tampon }
If Len>(InSize-InPos)Then Len:=InSize-InPos;
For i:=0 to Len-1 do Begin
If(OutPos>=BUFFER_SIZE)or(InPos>=InSize)Then Break;
OutBuf[OutPos]:=InBuf[InPos];
{ Protection ‚criture dictionnaire }
If DictPos<=MAX_DICT_SIZE Then Dictionary[DictPos]:=InBuf[InPos];
Inc(InPos);
Inc(DictPos);
DictPos := DictPos and MAX_DICT_SIZE;
Inc(OutPos);
End;
End;
{ ‚criture du tampon quand il est presque plein }
If OutPos >= BUFFER_SIZE-512 Then Begin
BlockWrite(Dest, OutBuf, OutPos);
OutPos := 0;
End;
End;
If OutPos>0 Then BlockWrite(Dest,OutBuf,OutPos);
End;
End;
Procedure DecompressHuffman(Var Source,Dest:File);Begin
WriteLn('Erreur: Compression Huffman n''est pas impl‚ment‚');
Halt(9);
End;
Procedure DecompressKWAJ(Var Source,Dest:File);Begin
BlockRead(Source, KWAJHeader,SizeOf(KWAJHeader));
Case KWAJHeader.Method of
KWAJ_METHOD_NONE:
CopyUncompressed(Source, Dest);
KWAJ_METHOD_LZ77,
KWAJ_METHOD_LZ77_RLE,
KWAJ_METHOD_MSCOMP: { Ajout du cas 0x88 }
DecompressLZ77(Source, Dest);
KWAJ_METHOD_HUFFMAN:
DecompressHuffman(Source, Dest);
Else WriteLn('Erreur: M‚thode de compression non-support‚: ', KWAJHeader.Method);
End;
End;
Procedure DecompressSZDD(Var Source,Dest:File);
Var
InBuf,OutBuf:Array[0..BUFFER_SIZE-1] of Byte;
InPos,OutPos:LongInt;
InSize:Word;
Control:Byte;
Begin
BlockRead(Source,SZDDHeader,SizeOf(SZDDHeader));
While Not Eof(Source)do Begin
BlockRead(Source,InBuf,BUFFER_SIZE,InSize);
If InSize=0 Then Break;
InPos:=0;
OutPos:=0;
While InPos<InSize do Begin
If InPos>=InSize Then Break;
Control := InBuf[InPos];
Inc(InPos);
If(Control=$FF)and(InPos<InSize)Then Begin
If OutPos<BUFFER_SIZE Then Begin
OutBuf[OutPos] := InBuf[InPos];
Inc(OutPos);
End;
Inc(InPos);
End
Else
Begin
If OutPos<BUFFER_SIZE Then Begin
OutBuf[OutPos] := Control;
Inc(OutPos);
End;
End;
{ ‚crire le tampon quand il est presque plein }
If OutPos >= BUFFER_SIZE - 256 then Begin
BlockWrite(Dest, OutBuf, OutPos);
OutPos := 0;
End;
End;
If OutPos>0 Then BlockWrite(Dest, OutBuf, OutPos);
End;
End;
Var
SourceName,DestName:String;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
WriteLn('EXPAND : Cette commande permet d''effectuer la d‚compression d''un fichier.');
WriteLn;
WriteLn('Syntaxe : EXPAND source [destination]');
WriteLn;
WriteLn(' source Indique le fichier compress‚');
WriteLn(' destination Indique le fichier … cr‚er');
Halt;
End;
DecompressMethod:=methodNone;
SourceName:=ParamStr(1);
If ParamCount=2 Then DestName:=ParamStr(2)
Else DestName:=SourceName + '.EXP';
{$I-} Assign(SourceFile,SourceName);
Reset(SourceFile,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur: Impossible d''ouvrir le fichier');
Exit;
End;
If Not(IsCompressedMS(SourceFile)or
IsCompressedSZDD(SourceFile)or
IsCompressedKWAJ(SourceFile))Then Begin
WriteLn('Erreur: Fichier source n''est pas compress‚.');
Close(SourceFile);
Halt(4);
End;
{$I-}Assign(DestFile, DestName);
Rewrite(DestFile,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur: Impossible de cr‚er le fichier de destination');
Close(SourceFile);
Halt(2);
End;
Case DecompressMethod of
methodKWAJ:DecompressKWAJ(SourceFile,DestFile);
methodDict:ExpandFile(SourceFile,DestFile);
methodSZDD:DecompressSZDD(SourceFile,DestFile);
Else Begin
WriteLn('M�thode de compression non reconnu');
Halt(3);
End;
End;
Close(SourceFile);
Close(DestFile);
WriteLn('Expension du fichier r‚ussis');
END.