1 // Streaming R/O Virtual File System v0.2.0
2 // Copyright (C) XL A.S. Ketmar. All rights reserved
3 // See the file aplicense.txt for conditions of use.
5 // grouping files with packing:
6 // zip, jar: PKZIP-compatible archives (store, deflate)
7 // fout2 : Fallout II .DAT
8 // vtdb : Asphyre's VTDb
9 // dfwad : D2D:F wad archives
11 {.$DEFINE SFS_DEBUG_ZIPFS}
19 SysUtils
, Classes
, Contnrs
, sfs
;
24 TSFSZipVolumeType
= (sfszvNone
, sfszvZIP
, sfszvF2DAT
, sfszvVTDB
, sfszvDFWAD
);
26 TSFSZipVolume
= class(TSFSVolume
)
28 fType
: TSFSZipVolumeType
;
30 procedure ZIPReadDirectory ();
31 procedure F2DATReadDirectory ();
32 procedure VTDBReadDirectory ();
33 procedure DFWADReadDirectory ();
35 procedure ReadDirectory (); override;
36 procedure removeCommonPath (); override;
39 function OpenFileByIndex (const index
: Integer): TStream
; override;
42 TSFSZipVolumeFactory
= class (TSFSVolumeFactory
)
44 function IsMyVolumePrefix (const prefix
: TSFSString
): Boolean; override;
45 function Produce (const prefix
, fileName
: TSFSString
; st
: TStream
): TSFSVolume
; override;
46 procedure Recycle (vol
: TSFSVolume
); override;
58 TZDecompressionStream
= TDecompressionStream
;
61 TSFSZipFileInfo
= class (TSFSFileInfo
)
63 fMethod
: Byte; // 0: store; 8: deflate; 255: other
67 TZLocalFileHeader
= packed record
81 function ZIPCheckMagic (st
: TStream
): Boolean;
83 sign
: packed array [0..3] of Char;
86 st
.ReadBuffer(sign
[0], 4);
87 st
.Seek(-4, soCurrent
);
88 if (sign
<> 'PK'#3#4) and (sign
<> 'PK'#5#6) then exit
;
92 function F2DATCheckMagic (st
: TStream
): Boolean;
97 st
.Position
:= st
.Size
-8;
98 st
.ReadBuffer(dsize
, 4); st
.ReadBuffer(fiSz
, 4);
100 if (fiSz
<> st
.Size
) or (dsize
< 5+13) or (dsize
> fiSz
-4) then exit
;
104 function VTDBCheckMagic (st
: TStream
): Boolean;
106 sign
: packed array [0..3] of Char;
110 if st
.Size
< 32 then exit
;
111 st
.ReadBuffer(sign
[0], 4);
112 st
.ReadBuffer(fcnt
, 4); st
.ReadBuffer(dofs
, 4);
113 st
.Seek(-12, soCurrent
);
114 if sign
<> 'vtdm' then exit
;
115 if (fcnt
< 0) or (dofs
< 32) or (dofs
+fcnt
*8 > st
.Size
) then exit
;
119 function DFWADCheckMagic (st
: TStream
): Boolean;
121 sign
: packed array [0..5] of Char;
125 if st
.Size
< 10 then exit
;
126 st
.ReadBuffer(sign
[0], 6);
127 st
.ReadBuffer(fcnt
, 2);
128 st
.Seek(-8, soCurrent
);
129 //writeln('trying DFWAD... [', sign, ']');
130 if (sign
[0] <> 'D') and (sign
[1] <> 'F') and (sign
[2] <> 'W') and
131 (sign
[3] <> 'A') and (sign
[4] <> 'D') and (sign
[5] <> #
$01) then exit
;
132 //writeln('DFWAD FOUND, with ', fcnt, ' files');
133 //if (fcnt < 0) then exit;
137 function maxPrefix (s0
: string; s1
: string): Integer;
141 for f
:= 1 to length(s0
) do
143 if f
> length(s1
) then begin result
:= f
; exit
; end;
144 if SFSUpCase(s0
[f
]) <> SFSUpCase(s1
[f
]) then begin result
:= f
; exit
; end;
146 result
:= length(s0
);
149 procedure TSFSZipVolume
.removeCommonPath ();
151 f
, pl
, maxsc
, sc
, c
: integer;
155 if fType
<> sfszvZIP
then exit
;
157 if fFiles
.Count
= 0 then exit
;
159 for f
:= 0 to fFiles
.Count
-1 do
161 fi
:= TSFSZipFileInfo(fFiles
[f
]);
163 if length(s
) > 0 then begin cp
:= s
; break
; end;
165 if length(cp
) = 0 then exit
;
166 for f
:= 0 to fFiles
.Count
-1 do
168 fi
:= TSFSZipFileInfo(fFiles
[f
]);
170 if length(s
) = 0 then continue
;
171 pl
:= maxPrefix(cp
, s
);
172 //writeln('s=[', s, ']; cp=[', cp, ']; pl=', pl);
173 if pl
= 0 then exit
; // no common prefix at all
174 cp
:= Copy(cp
, 1, pl
);
176 for c
:= 1 to length(s
) do if s
[c
] = '/' then Inc(sc
);
177 if sc
> maxsc
then maxsc
:= sc
;
179 if maxsc
< 2 then exit
; // alas
180 while (length(cp
) > 0) and (cp
[length(cp
)] <> '/') do cp
:= Copy(cp
, 1, length(cp
)-1);
181 if length(cp
) < 2 then exit
; // nothing to do
182 for f
:= 0 to fFiles
.Count
-1 do
184 fi
:= TSFSZipFileInfo(fFiles
[f
]);
185 if length(fi
.fPath
) >= length(cp
) then
188 fi
.fPath
:= Copy(fi
.fPath
, length(cp
)+1, length(fi
.fPath
));
189 //writeln('FIXED [', s, '] -> [', fi.fPath, ']');
195 procedure TSFSZipVolume
.ZIPReadDirectory ();
199 sign
, dSign
: packed array [0..3] of Char;
200 lhdr
: TZLocalFileHeader
;
201 ignoreFile
, skipped
: Boolean;
202 crc
, psz
, usz
: LongWord;
203 buf
: packed array of Byte;
204 bufPos
, bufUsed
: Integer;
207 // read local directory
209 fFileStream
.ReadBuffer(sign
[0], Length(sign
));
211 if sign
<> 'PK'#3#4 then break
;
216 fi
:= TSFSZipFileInfo
.Create(self
);
220 fFileStream
.ReadBuffer(lhdr
, SizeOf(lhdr
));
221 if lhdr
.fnameSz
> 255 then name
[0] := #255 else name
[0] := chr(lhdr
.fnameSz
);
222 fFileStream
.ReadBuffer(name
[1], Length(name
));
223 fFileStream
.Seek(lhdr
.fnameSz
-Length(name
), soCurrent
); // rest of the name (if any)
225 fFileStream
.Seek(lhdr
.localExtraSz
, soCurrent
);
227 if (lhdr
.flags
and 1) <> 0 then
229 // encrypted file: skip it
233 if (lhdr
.method
<> 0) and (lhdr
.method
<> 8) then
235 // not stored. not deflated. skip.
239 fi
.fOfs
:= fFileStream
.Position
;
240 fi
.fSize
:= lhdr
.unpackSz
;
241 fi
.fPackSz
:= lhdr
.packSz
;
242 fi
.fMethod
:= lhdr
.method
;
244 if (lhdr
.flags
and (1 shl 3)) <> 0 then
246 // it has a descriptor. stupid thing at all...
247 {$IFDEF SFS_DEBUG_ZIPFS}
248 WriteLn(ErrOutput
, 'descr: $', IntToHex(fFileStream
.Position
, 8));
249 WriteLn(ErrOutput
, 'size: ', lhdr
.unpackSz
);
250 WriteLn(ErrOutput
, 'psize: ', lhdr
.packSz
);
254 if lhdr
.packSz
<> 0 then
256 // some kind of idiot already did our work (maybe paritally)
257 // trust him (her? %-)
258 fFileStream
.Seek(lhdr
.packSz
, soCurrent
);
261 // scan for descriptor
262 if Length(buf
) = 0 then SetLength(buf
, 65536);
263 bufPos
:= 0; bufUsed
:= 0;
264 fFileStream
.ReadBuffer(dSign
[0], 4);
266 if dSign
<> 'PK'#7#8 then
269 Move(dSign
[1], dSign
[0], 3);
270 if bufPos
>= bufUsed
then
274 if fFileStream
.Size
-fFileStream
.Position
> Length(buf
) then
275 bufUsed
:= Length(buf
)
276 else bufUsed
:= fFileStream
.Size
-fFileStream
.Position
;
277 if bufUsed
= 0 then raise ESFSError
.Create('invalid ZIP file');
278 fFileStream
.ReadBuffer(buf
[0], bufUsed
);
280 dSign
[3] := chr(buf
[bufPos
]); Inc(bufPos
);
284 // signature found: check if it is a real one
285 // ???: make stronger check (for the correct following signature)?
286 // sign, crc, packsize, unpacksize
287 fFileStream
.Seek(-bufUsed
+bufPos
, soCurrent
); bufPos
:= 0; bufUsed
:= 0;
288 fFileStream
.ReadBuffer(crc
, 4); // crc
289 fFileStream
.ReadBuffer(psz
, 4); // packed size
291 if psz
= lhdr
.packSz
then
293 // this is a real description. fuck it off
294 fFileStream
.ReadBuffer(usz
, 4); // unpacked size
297 // this is just a sequence of bytes
298 fFileStream
.Seek(-8, soCurrent
);
299 fFileStream
.ReadBuffer(dSign
[0], 4);
302 // store correct values
308 if not skipped
then fFileStream
.Seek(lhdr
.packSz
, soCurrent
);
309 if ignoreFile
then fi
.Free();
312 if (sign
<> 'PK'#1#2) and (sign
<> 'PK'#5#6) then
314 {$IFDEF SFS_DEBUG_ZIPFS}
315 WriteLn(ErrOutput
, 'end: $', IntToHex(fFileStream
.Position
, 8));
316 WriteLn(ErrOutput
, 'sign: $', sign
[0], sign
[1], '#', ord(sign
[2]), '#', ord(sign
[3]));
318 raise ESFSError
.Create('invalid .ZIP archive (no central dir)');
322 procedure TSFSZipVolume
.F2DATReadDirectory ();
330 fFileStream
.Position
:= fFileStream
.Size
-8;
331 fFileStream
.ReadBuffer(dsize
, 4);
332 fFileStream
.Seek(-dsize
, soCurrent
); Dec(dsize
, 4);
335 fi
:= TSFSZipFileInfo
.Create(self
);
336 fFileStream
.ReadBuffer(f
, 4);
337 if (f
< 1) or (f
> 255) then raise ESFSError
.Create('invalid Fallout II .DAT file');
339 if dsize
< 0 then raise ESFSError
.Create('invalid Fallout II .DAT file');
340 name
[0] := chr(f
); if f
> 0 then fFileStream
.ReadBuffer(name
[1], f
);
341 f
:= 1; while (f
<= ord(name
[0])) and (name
[f
] <> #0) do Inc(f
); name
[0] := chr(f
-1);
343 fFileStream
.ReadBuffer(b
, 1); // packed?
344 if b
= 0 then fi
.fMethod
:= 0 else fi
.fMethod
:= 255;
345 fFileStream
.ReadBuffer(fi
.fSize
, 4);
346 fFileStream
.ReadBuffer(fi
.fPackSz
, 4);
347 fFileStream
.ReadBuffer(fi
.fOfs
, 4);
351 procedure TSFSZipVolume
.VTDBReadDirectory ();
355 keys
: array of record name
: string; ofs
: Integer; end;
360 fFileStream
.Seek(4, soCurrent
); // skip signature
361 fFileStream
.ReadBuffer(fcnt
, 4);
362 fFileStream
.ReadBuffer(dofs
, 4);
363 fFileStream
.Seek(dofs
, soBeginning
);
366 SetLength(keys
, fcnt
);
367 for f
:= 0 to fcnt
-1 do
369 fFileStream
.ReadBuffer(c
, 4);
370 if (c
< 0) or (c
> 1023) then raise ESFSError
.Create('invalid VTDB file');
371 SetLength(keys
[f
].name
, c
);
374 fFileStream
.ReadBuffer(keys
[f
].name
[1], c
);
375 keys
[f
].name
:= SFSReplacePathDelims(keys
[f
].name
, '/');
376 if keys
[f
].name
[1] = '/' then Delete(keys
[f
].name
, 1, 1);
378 fFileStream
.ReadBuffer(keys
[f
].ofs
, 4);
381 // read records (record type will be converted to directory name)
382 for f
:= 0 to fcnt
-1 do
384 fFileStream
.Position
:= keys
[f
].ofs
;
385 fi
:= TSFSZipFileInfo
.Create(self
);
386 fFileStream
.ReadBuffer(rtype
, 2);
387 fFileStream
.ReadBuffer(fi
.fSize
, 4);
388 fFileStream
.ReadBuffer(fi
.fPackSz
, 4);
389 fi
.fOfs
:= fFileStream
.Position
+12;
390 fi
.fName
:= keys
[f
].name
;
391 fi
.fPath
:= IntToHex(rtype
, 4)+'/';
396 procedure TSFSZipVolume
.DFWADReadDirectory ();
401 f
, c
, fofs
, fpksize
: Integer;
402 curpath
, fname
: string;
403 name
: packed array [0..15] of Char;
406 fFileStream
.Seek(6, soCurrent
); // skip signature
407 fFileStream
.ReadBuffer(fcnt
, 2);
408 if fcnt
= 0 then exit
;
410 for f
:= 0 to fcnt
-1 do
412 fFileStream
.ReadBuffer(name
[0], 16);
413 fFileStream
.ReadBuffer(fofs
, 4);
414 fFileStream
.ReadBuffer(fpksize
, 4);
417 while (c
< 16) and (name
[c
] <> #0) do
419 if name
[c
] = '\' then name
[c
] := '/'
420 else if name
[c
] = '/' then name
[c
] := '_';
421 fname
:= fname
+name
[c
];
425 if (fofs
= 0) and (fpksize
= 0) then
427 if length(fname
) <> 0 then fname
:= fname
+'/';
431 if length(fname
) = 0 then continue
; // just in case
432 //writeln('DFWAD: [', curpath, '] [', fname, '] at ', fofs, ', size ', fpksize);
433 // create file record
434 fi
:= TSFSZipFileInfo
.Create(self
);
437 fi
.fPackSz
:= fpksize
;
444 procedure TSFSZipVolume
.ReadDirectory ();
447 sfszvZIP
: ZIPReadDirectory();
448 sfszvF2DAT
: F2DATReadDirectory();
449 sfszvVTDB
: VTDBReadDirectory();
450 sfszvDFWAD
: DFWADReadDirectory();
451 else raise ESFSError
.Create('invalid zipped SFS');
455 function TSFSZipVolume
.OpenFileByIndex (const index
: Integer): TStream
;
457 zs
: TZDecompressionStream
;
461 buf
: packed array [0..1023] of Char;
468 if fFiles
= nil then exit
;
469 if (index
< 0) or (index
>= fFiles
.Count
) or (fFiles
[index
] = nil) then exit
;
474 fs := TFileStream.Create(fFileName, fmOpenRead or fmShareDenyWrite);
481 if TSFSZipFileInfo(fFiles
[index
]).fMethod
= 0 then
483 result
:= TSFSPartialStream
.Create(fs
,
484 TSFSZipFileInfo(fFiles
[index
]).fOfs
,
485 TSFSZipFileInfo(fFiles
[index
]).fSize
, kill
);
489 fs
.Seek(TSFSZipFileInfo(fFiles
[index
]).fOfs
, soBeginning
);
490 if TSFSZipFileInfo(fFiles
[index
]).fMethod
= 255 then
492 zs
:= TZDecompressionStream
.Create(fs
)
496 zs
:= TZDecompressionStream
.Create(fs
, true {-15}{MAX_WBITS});
498 // sorry, pals, DFWAD is completely broken, so users of it should SUFFER
499 if TSFSZipFileInfo(fFiles
[index
]).fSize
= -1 then
501 TSFSZipFileInfo(fFiles
[index
]).fSize
:= 0;
502 //writeln('trying to determine file size for [', TSFSZipFileInfo(fFiles[index]).fPath, TSFSZipFileInfo(fFiles[index]).fName, ']');
506 rd
:= zs
.read(buf
, 1024);
507 //writeln(' got ', rd, ' bytes');
508 if rd
> 0 then Inc(TSFSZipFileInfo(fFiles
[index
]).fSize
, rd
);
509 if rd
< 1024 then break
;
511 //writeln(' resulting size: ', TSFSZipFileInfo(fFiles[index]).fSize, ' bytes');
514 fs
.Seek(TSFSZipFileInfo(fFiles
[index
]).fOfs
, soBeginning
);
515 zs
:= TZDecompressionStream
.Create(fs
)
517 //writeln('*** CAN''T determine file size for [', TSFSZipFileInfo(fFiles[index]).fPath, TSFSZipFileInfo(fFiles[index]).fName, ']');
519 if kill
then FreeAndNil(fs
);
524 gs
:= TSFSGuardStream
.Create(zs
, fs
, true, kill
, false);
527 result
:= TSFSPartialStream
.Create(gs
, 0, TSFSZipFileInfo(fFiles
[index
]).fSize
, true);
532 if kill
then FreeAndNil(fs
);
539 { TSFSZipVolumeFactory }
540 function TSFSZipVolumeFactory
.IsMyVolumePrefix (const prefix
: TSFSString
): Boolean;
543 SFSStrEqu(prefix
, 'zip') or
544 SFSStrEqu(prefix
, 'jar') or
545 SFSStrEqu(prefix
, 'fout2') or
546 SFSStrEqu(prefix
, 'vtdb') or
547 SFSStrEqu(prefix
, 'wad') or
548 SFSStrEqu(prefix
, 'dfwad');
551 procedure TSFSZipVolumeFactory
.Recycle (vol
: TSFSVolume
);
556 function TSFSZipVolumeFactory
.Produce (const prefix
, fileName
: TSFSString
; st
: TStream
): TSFSVolume
;
558 vt
: TSFSZipVolumeType
;
561 if ZIPCheckMagic(st
) then vt
:= sfszvZIP
562 else if DFWADCheckMagic(st
) then vt
:= sfszvDFWAD
563 else if F2DATCheckMagic(st
) then vt
:= sfszvF2DAT
564 else if VTDBCheckMagic(st
) then vt
:= sfszvVTDB
;
566 if vt
<> sfszvNone
then
568 result
:= TSFSZipVolume
.Create(fileName
, st
);
569 TSFSZipVolume(result
).fType
:= vt
;
571 result
.DoDirectoryRead();
572 except {$IFDEF SFS_DEBUG_ZIPFS} on e
: Exception
do begin
573 WriteLn(errOutput
, 'ZIP ERROR: [', e
.ClassName
, ']: ', e
.Message);
577 {$IFDEF SFS_DEBUG_ZIPFS}end;{$ENDIF}
588 zipf
: TSFSZipVolumeFactory
;
590 zipf
:= TSFSZipVolumeFactory
.Create();
591 SFSRegisterVolumeFactory(zipf
);
593 SFSUnregisterVolumeFactory(zipf
);