ee1cc1c272b7e6c42e884a78f643632fe376f570
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}
12 {.$DEFINE SFS_ZIPFS_FULL}
20 SysUtils
, Classes
, Contnrs
, sfs
;
27 {$IFDEF SFS_ZIPFS_FULL}
34 TSFSZipVolume
= class(TSFSVolume
)
36 fType
: TSFSZipVolumeType
;
38 procedure ZIPReadDirectory ();
39 procedure DFWADReadDirectory ();
40 {$IFDEF SFS_ZIPFS_FULL}
41 procedure F2DATReadDirectory ();
42 procedure VTDBReadDirectory ();
45 procedure ReadDirectory (); override;
46 procedure removeCommonPath (); override;
49 function OpenFileByIndex (const index
: Integer): TStream
; override;
52 TSFSZipVolumeFactory
= class (TSFSVolumeFactory
)
54 function IsMyVolumePrefix (const prefix
: TSFSString
): Boolean; override;
55 function Produce (const prefix
, fileName
: TSFSString
; st
: TStream
): TSFSVolume
; override;
56 procedure Recycle (vol
: TSFSVolume
); override;
68 TZDecompressionStream
= TDecompressionStream
;
71 TSFSZipFileInfo
= class (TSFSFileInfo
)
73 fMethod
: Byte; // 0: store; 8: deflate; 255: other
77 TZLocalFileHeader
= packed record
91 function ZIPCheckMagic (st
: TStream
): Boolean;
93 sign
: packed array [0..3] of Char;
96 st
.ReadBuffer(sign
[0], 4);
97 st
.Seek(-4, soCurrent
);
98 if (sign
<> 'PK'#3#4) and (sign
<> 'PK'#5#6) then exit
;
102 {$IFDEF SFS_ZIPFS_FULL}
103 function F2DATCheckMagic (st
: TStream
): Boolean;
105 dsize
, fiSz
: Integer;
108 st
.Position
:= st
.Size
-8;
109 st
.ReadBuffer(dsize
, 4); st
.ReadBuffer(fiSz
, 4);
111 if (fiSz
<> st
.Size
) or (dsize
< 5+13) or (dsize
> fiSz
-4) then exit
;
115 function VTDBCheckMagic (st
: TStream
): Boolean;
117 sign
: packed array [0..3] of Char;
121 if st
.Size
< 32 then exit
;
122 st
.ReadBuffer(sign
[0], 4);
123 st
.ReadBuffer(fcnt
, 4); st
.ReadBuffer(dofs
, 4);
124 st
.Seek(-12, soCurrent
);
125 if sign
<> 'vtdm' then exit
;
126 if (fcnt
< 0) or (dofs
< 32) or (dofs
+fcnt
*8 > st
.Size
) then exit
;
131 function DFWADCheckMagic (st
: TStream
): Boolean;
133 sign
: packed array [0..5] of Char;
137 if st
.Size
< 10 then exit
;
138 st
.ReadBuffer(sign
[0], 6);
139 st
.ReadBuffer(fcnt
, 2);
140 st
.Seek(-8, soCurrent
);
141 //writeln('trying DFWAD... [', sign, ']');
142 if (sign
[0] <> 'D') and (sign
[1] <> 'F') and (sign
[2] <> 'W') and
143 (sign
[3] <> 'A') and (sign
[4] <> 'D') and (sign
[5] <> #
$01) then exit
;
144 //writeln('DFWAD FOUND, with ', fcnt, ' files');
145 //if (fcnt < 0) then exit;
149 function maxPrefix (s0
: string; s1
: string): Integer;
153 for f
:= 1 to length(s0
) do
155 if f
> length(s1
) then begin result
:= f
; exit
; end;
156 if SFSUpCase(s0
[f
]) <> SFSUpCase(s1
[f
]) then begin result
:= f
; exit
; end;
158 result
:= length(s0
);
161 procedure TSFSZipVolume
.removeCommonPath ();
163 f
, pl
, maxsc
, sc
, c
: integer;
167 if fType
<> sfszvZIP
then exit
;
169 if fFiles
.Count
= 0 then exit
;
171 for f
:= 0 to fFiles
.Count
-1 do
173 fi
:= TSFSZipFileInfo(fFiles
[f
]);
175 if length(s
) > 0 then begin cp
:= s
; break
; end;
177 if length(cp
) = 0 then exit
;
178 for f
:= 0 to fFiles
.Count
-1 do
180 fi
:= TSFSZipFileInfo(fFiles
[f
]);
182 if length(s
) = 0 then continue
;
183 pl
:= maxPrefix(cp
, s
);
184 //writeln('s=[', s, ']; cp=[', cp, ']; pl=', pl);
185 if pl
= 0 then exit
; // no common prefix at all
186 cp
:= Copy(cp
, 1, pl
);
188 for c
:= 1 to length(s
) do if s
[c
] = '/' then Inc(sc
);
189 if sc
> maxsc
then maxsc
:= sc
;
191 if maxsc
< 2 then exit
; // alas
192 while (length(cp
) > 0) and (cp
[length(cp
)] <> '/') do cp
:= Copy(cp
, 1, length(cp
)-1);
193 if length(cp
) < 2 then exit
; // nothing to do
194 for f
:= 0 to fFiles
.Count
-1 do
196 fi
:= TSFSZipFileInfo(fFiles
[f
]);
197 if length(fi
.fPath
) >= length(cp
) then
200 fi
.fPath
:= Copy(fi
.fPath
, length(cp
)+1, length(fi
.fPath
));
201 //writeln('FIXED [', s, '] -> [', fi.fPath, ']');
208 procedure TSFSZipVolume
.ZIPReadDirectory ();
212 sign
, dSign
: packed array [0..3] of Char;
213 lhdr
: TZLocalFileHeader
;
214 ignoreFile
, skipped
: Boolean;
215 crc
, psz
, usz
: LongWord;
216 buf
: packed array of Byte;
217 bufPos
, bufUsed
: Integer;
220 // read local directory
222 fFileStream
.ReadBuffer(sign
[0], Length(sign
));
224 if sign
<> 'PK'#3#4 then break
;
229 fi
:= TSFSZipFileInfo
.Create(self
);
233 fFileStream
.ReadBuffer(lhdr
, SizeOf(lhdr
));
234 if lhdr
.fnameSz
> 255 then name
[0] := #255 else name
[0] := chr(lhdr
.fnameSz
);
235 fFileStream
.ReadBuffer(name
[1], Length(name
));
236 fFileStream
.Seek(lhdr
.fnameSz
-Length(name
), soCurrent
); // rest of the name (if any)
238 fFileStream
.Seek(lhdr
.localExtraSz
, soCurrent
);
240 if (lhdr
.flags
and 1) <> 0 then
242 // encrypted file: skip it
246 if (lhdr
.method
<> 0) and (lhdr
.method
<> 8) then
248 // not stored. not deflated. skip.
252 fi
.fOfs
:= fFileStream
.Position
;
253 fi
.fSize
:= lhdr
.unpackSz
;
254 fi
.fPackSz
:= lhdr
.packSz
;
255 fi
.fMethod
:= lhdr
.method
;
257 if (lhdr
.flags
and (1 shl 3)) <> 0 then
259 // it has a descriptor. stupid thing at all...
260 {$IFDEF SFS_DEBUG_ZIPFS}
261 WriteLn(ErrOutput
, 'descr: $', IntToHex(fFileStream
.Position
, 8));
262 WriteLn(ErrOutput
, 'size: ', lhdr
.unpackSz
);
263 WriteLn(ErrOutput
, 'psize: ', lhdr
.packSz
);
267 if lhdr
.packSz
<> 0 then
269 // some kind of idiot already did our work (maybe paritally)
270 // trust him (her? %-)
271 fFileStream
.Seek(lhdr
.packSz
, soCurrent
);
274 // scan for descriptor
275 if Length(buf
) = 0 then SetLength(buf
, 65536);
276 bufPos
:= 0; bufUsed
:= 0;
277 fFileStream
.ReadBuffer(dSign
[0], 4);
279 if dSign
<> 'PK'#7#8 then
282 Move(dSign
[1], dSign
[0], 3);
283 if bufPos
>= bufUsed
then
287 if fFileStream
.Size
-fFileStream
.Position
> Length(buf
) then
288 bufUsed
:= Length(buf
)
289 else bufUsed
:= fFileStream
.Size
-fFileStream
.Position
;
290 if bufUsed
= 0 then raise ESFSError
.Create('invalid ZIP file');
291 fFileStream
.ReadBuffer(buf
[0], bufUsed
);
293 dSign
[3] := chr(buf
[bufPos
]); Inc(bufPos
);
297 // signature found: check if it is a real one
298 // ???: make stronger check (for the correct following signature)?
299 // sign, crc, packsize, unpacksize
300 fFileStream
.Seek(-bufUsed
+bufPos
, soCurrent
); bufPos
:= 0; bufUsed
:= 0;
301 fFileStream
.ReadBuffer(crc
, 4); // crc
302 fFileStream
.ReadBuffer(psz
, 4); // packed size
304 if psz
= lhdr
.packSz
then
306 // this is a real description. fuck it off
307 fFileStream
.ReadBuffer(usz
, 4); // unpacked size
310 // this is just a sequence of bytes
311 fFileStream
.Seek(-8, soCurrent
);
312 fFileStream
.ReadBuffer(dSign
[0], 4);
315 // store correct values
321 if not skipped
then fFileStream
.Seek(lhdr
.packSz
, soCurrent
);
322 if ignoreFile
then fi
.Free();
325 if (sign
<> 'PK'#1#2) and (sign
<> 'PK'#5#6) then
327 {$IFDEF SFS_DEBUG_ZIPFS}
328 WriteLn(ErrOutput
, 'end: $', IntToHex(fFileStream
.Position
, 8));
329 WriteLn(ErrOutput
, 'sign: $', sign
[0], sign
[1], '#', ord(sign
[2]), '#', ord(sign
[3]));
331 raise ESFSError
.Create('invalid .ZIP archive (no central dir)');
335 {$IFDEF SFS_ZIPFS_FULL}
336 procedure TSFSZipVolume
.F2DATReadDirectory ();
344 fFileStream
.Position
:= fFileStream
.Size
-8;
345 fFileStream
.ReadBuffer(dsize
, 4);
346 fFileStream
.Seek(-dsize
, soCurrent
); Dec(dsize
, 4);
349 fi
:= TSFSZipFileInfo
.Create(self
);
350 fFileStream
.ReadBuffer(f
, 4);
351 if (f
< 1) or (f
> 255) then raise ESFSError
.Create('invalid Fallout II .DAT file');
353 if dsize
< 0 then raise ESFSError
.Create('invalid Fallout II .DAT file');
354 name
[0] := chr(f
); if f
> 0 then fFileStream
.ReadBuffer(name
[1], f
);
355 f
:= 1; while (f
<= ord(name
[0])) and (name
[f
] <> #0) do Inc(f
); name
[0] := chr(f
-1);
357 fFileStream
.ReadBuffer(b
, 1); // packed?
358 if b
= 0 then fi
.fMethod
:= 0 else fi
.fMethod
:= 255;
359 fFileStream
.ReadBuffer(fi
.fSize
, 4);
360 fFileStream
.ReadBuffer(fi
.fPackSz
, 4);
361 fFileStream
.ReadBuffer(fi
.fOfs
, 4);
365 procedure TSFSZipVolume
.VTDBReadDirectory ();
369 keys
: array of record name
: string; ofs
: Integer; end;
374 fFileStream
.Seek(4, soCurrent
); // skip signature
375 fFileStream
.ReadBuffer(fcnt
, 4);
376 fFileStream
.ReadBuffer(dofs
, 4);
377 fFileStream
.Seek(dofs
, soBeginning
);
380 SetLength(keys
, fcnt
);
381 for f
:= 0 to fcnt
-1 do
383 fFileStream
.ReadBuffer(c
, 4);
384 if (c
< 0) or (c
> 1023) then raise ESFSError
.Create('invalid VTDB file');
385 SetLength(keys
[f
].name
, c
);
388 fFileStream
.ReadBuffer(keys
[f
].name
[1], c
);
389 keys
[f
].name
:= SFSReplacePathDelims(keys
[f
].name
, '/');
390 if keys
[f
].name
[1] = '/' then Delete(keys
[f
].name
, 1, 1);
392 fFileStream
.ReadBuffer(keys
[f
].ofs
, 4);
395 // read records (record type will be converted to directory name)
396 for f
:= 0 to fcnt
-1 do
398 fFileStream
.Position
:= keys
[f
].ofs
;
399 fi
:= TSFSZipFileInfo
.Create(self
);
400 fFileStream
.ReadBuffer(rtype
, 2);
401 fFileStream
.ReadBuffer(fi
.fSize
, 4);
402 fFileStream
.ReadBuffer(fi
.fPackSz
, 4);
403 fi
.fOfs
:= fFileStream
.Position
+12;
404 fi
.fName
:= keys
[f
].name
;
405 fi
.fPath
:= IntToHex(rtype
, 4)+'/';
411 procedure TSFSZipVolume
.DFWADReadDirectory ();
416 f
, c
, fofs
, fpksize
: Integer;
417 curpath
, fname
: string;
418 name
: packed array [0..15] of Char;
421 fFileStream
.Seek(6, soCurrent
); // skip signature
422 fFileStream
.ReadBuffer(fcnt
, 2);
423 if fcnt
= 0 then exit
;
425 for f
:= 0 to fcnt
-1 do
427 fFileStream
.ReadBuffer(name
[0], 16);
428 fFileStream
.ReadBuffer(fofs
, 4);
429 fFileStream
.ReadBuffer(fpksize
, 4);
432 while (c
< 16) and (name
[c
] <> #0) do
434 if name
[c
] = '\' then name
[c
] := '/'
435 else if name
[c
] = '/' then name
[c
] := '_';
436 fname
:= fname
+name
[c
];
440 if (fofs
= 0) and (fpksize
= 0) then
442 if length(fname
) <> 0 then fname
:= fname
+'/';
446 if length(fname
) = 0 then continue
; // just in case
447 //writeln('DFWAD: [', curpath, '] [', fname, '] at ', fofs, ', size ', fpksize);
448 // create file record
449 fi
:= TSFSZipFileInfo
.Create(self
);
452 fi
.fPackSz
:= fpksize
;
459 procedure TSFSZipVolume
.ReadDirectory ();
462 sfszvZIP
: ZIPReadDirectory();
463 {$IFDEF SFS_ZIPFS_FULL}
464 sfszvF2DAT
: F2DATReadDirectory();
465 sfszvVTDB
: VTDBReadDirectory();
467 sfszvDFWAD
: DFWADReadDirectory();
468 else raise ESFSError
.Create('invalid zipped SFS');
472 function TSFSZipVolume
.OpenFileByIndex (const index
: Integer): TStream
;
474 zs
: TZDecompressionStream
;
478 buf
: packed array [0..1023] of Char;
486 if fFiles
= nil then exit
;
487 if (index
< 0) or (index
>= fFiles
.Count
) or (fFiles
[index
] = nil) then exit
;
492 fs := TFileStream.Create(fFileName, fmOpenRead or fmShareDenyWrite);
499 if TSFSZipFileInfo(fFiles
[index
]).fMethod
= 0 then
501 result
:= TSFSPartialStream
.Create(fs
,
502 TSFSZipFileInfo(fFiles
[index
]).fOfs
,
503 TSFSZipFileInfo(fFiles
[index
]).fSize
, kill
);
507 fs
.Seek(TSFSZipFileInfo(fFiles
[index
]).fOfs
, soBeginning
);
508 if TSFSZipFileInfo(fFiles
[index
]).fMethod
= 255 then
510 // sorry, pals, DFWAD is completely broken, so users of it should SUFFER
511 if TSFSZipFileInfo(fFiles
[index
]).fSize
= -1 then
513 TSFSZipFileInfo(fFiles
[index
]).fSize
:= 0;
514 //writeln('trying to determine file size for [', TSFSZipFileInfo(fFiles[index]).fPath, TSFSZipFileInfo(fFiles[index]).fName, ']');
515 zs
:= TZDecompressionStream
.Create(fs
);
519 rd
:= zs
.read(buf
, 1024);
520 //writeln(' got ', rd, ' bytes');
521 if rd
> 0 then Inc(TSFSZipFileInfo(fFiles
[index
]).fSize
, rd
);
522 if rd
< 1024 then break
;
524 //writeln(' resulting size: ', TSFSZipFileInfo(fFiles[index]).fSize, ' bytes');
527 fs
.Seek(TSFSZipFileInfo(fFiles
[index
]).fOfs
, soBeginning
);
529 //writeln('*** CAN''T determine file size for [', TSFSZipFileInfo(fFiles[index]).fPath, TSFSZipFileInfo(fFiles[index]).fName, ']');
531 if kill
then FreeAndNil(fs
);
536 rs
:= TSFSPartialStream
.Create(fs
, TSFSZipFileInfo(fFiles
[index
]).fOfs
, TSFSZipFileInfo(fFiles
[index
]).fPackSz
, true);
537 zs
:= TZDecompressionStream
.Create(rs
);
542 rs
:= TSFSPartialStream
.Create(fs
, TSFSZipFileInfo(fFiles
[index
]).fOfs
, TSFSZipFileInfo(fFiles
[index
]).fPackSz
, true);
543 zs
:= TZDecompressionStream
.Create(rs
, true {-15}{MAX_WBITS});
546 gs
:= TSFSGuardStream
.Create(zs
, fs
, true, kill
, false);
549 result
:= TSFSPartialStream
.Create(gs
, 0, TSFSZipFileInfo(fFiles
[index
]).fSize
, true);
555 if kill
then FreeAndNil(fs
);
562 { TSFSZipVolumeFactory }
563 function TSFSZipVolumeFactory
.IsMyVolumePrefix (const prefix
: TSFSString
): Boolean;
566 SFSStrEqu(prefix
, 'zip') or
567 SFSStrEqu(prefix
, 'dfwad')
568 {$IFDEF SFS_ZIPFS_FULL}
569 or SFSStrEqu(prefix
, 'jar') or
570 SFSStrEqu(prefix
, 'fout2') or
571 SFSStrEqu(prefix
, 'vtdb') or
572 SFSStrEqu(prefix
, 'wad')
577 procedure TSFSZipVolumeFactory
.Recycle (vol
: TSFSVolume
);
582 function TSFSZipVolumeFactory
.Produce (const prefix
, fileName
: TSFSString
; st
: TStream
): TSFSVolume
;
584 vt
: TSFSZipVolumeType
;
587 if ZIPCheckMagic(st
) then vt
:= sfszvZIP
588 else if DFWADCheckMagic(st
) then vt
:= sfszvDFWAD
589 {$IFDEF SFS_ZIPFS_FULL}
590 else if F2DATCheckMagic(st
) then vt
:= sfszvF2DAT
591 else if VTDBCheckMagic(st
) then vt
:= sfszvVTDB
595 if vt
<> sfszvNone
then
597 result
:= TSFSZipVolume
.Create(fileName
, st
);
598 TSFSZipVolume(result
).fType
:= vt
;
600 result
.DoDirectoryRead();
601 except {$IFDEF SFS_DEBUG_ZIPFS} on e
: Exception
do begin
602 WriteLn(errOutput
, 'ZIP ERROR: [', e
.ClassName
, ']: ', e
.Message);
606 {$IFDEF SFS_DEBUG_ZIPFS}end;{$ENDIF}
617 zipf
: TSFSZipVolumeFactory
;
619 zipf
:= TSFSZipVolumeFactory
.Create();
620 SFSRegisterVolumeFactory(zipf
);
622 // SFSUnregisterVolumeFactory(zipf);