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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: vfs_zip.c,v 1.12 2007-10-13 16:02:51 dkure Exp $
*
*/
#ifdef WITH_ZIP
#include "quakedef.h"
#include "hash.h"
#include "common.h"
#include "fs.h"
#include "vfs.h"
//===========================
// Unzip library interfacing
//===========================
// These functions provide IO support for the unzip library using our VFS layer
static void *FSZIP_ZOpenFile(void *fin, const char *filename, int mode) {
vfsfile_t *vfs_fin = (vfsfile_t *)fin;
// VFS-TODO Maybe need to increase reference count
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) {
} else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) {
Sys_Error("FSZIP_ZOpenFile: Unsupported file mode: MODE_EXISTING");
return NULL;
} else if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
Sys_Error("FSZIP_ZOpenFile: Unsupported file mode: write");
return NULL;
}
return vfs_fin;
}
static unsigned long FSZIP_ZReadFile(void *fin, void *stream, void *buf, unsigned long size) {
vfsfile_t *vfs_fin = (vfsfile_t *)stream;
return VFS_READ(vfs_fin, buf, size, NULL);
}
static unsigned long FSZIP_ZWriteFile(void *fin, void *stream, const void *buf, unsigned long size) {
vfsfile_t *vfs_fin = (vfsfile_t *)stream;
return VFS_WRITE(vfs_fin, buf, size);
}
static long FSZIP_ZTellFile(void *fin, void *stream) {
vfsfile_t *vfs_fin = (vfsfile_t *)stream;
return VFS_TELL(vfs_fin);
}
static long FSZIP_ZSeekFile(void *fin, void *stream, unsigned long offset, int origin) {
vfsfile_t *vfs_fin = (vfsfile_t *)stream;
int fseek_origin;
int r;
switch (origin)
{
case ZLIB_FILEFUNC_SEEK_CUR: fseek_origin = SEEK_CUR; break;
case ZLIB_FILEFUNC_SEEK_END: fseek_origin = SEEK_END; break;
case ZLIB_FILEFUNC_SEEK_SET: fseek_origin = SEEK_SET; break;
default:
Sys_Error("FSZIP_ZSeekFile: Unsupported seek origin: Unknown");
return -1;
}
r = VFS_SEEK(vfs_fin, offset, fseek_origin);
return r;
}
static int FSZIP_ZCloseFile(void *fin, void *stream) {
// vfsfile_t *vfs_fin = (vfsfile_t *)stream;
// VFS-TODO: Need to decrease references
// VFS_CLOSE(vfs_fin);
return 0;
}
static int FSZIP_ZErrorFileFile(void *fin, void *stream) {
// vfsfile_t *vfs_fin = (vfsfile_t *)stream;
// VFS-TODO: Need to get the error code somehow
return 0;
}
static void FSZIP_CreteFileFuncs(zlib_filefunc_def *funcs) {
if (funcs == NULL)
return;
funcs->zopen_file = FSZIP_ZOpenFile;
funcs->zread_file = FSZIP_ZReadFile;
funcs->zwrite_file = FSZIP_ZWriteFile;
funcs->ztell_file = FSZIP_ZTellFile;
funcs->zseek_file = FSZIP_ZSeekFile;
funcs->zclose_file = FSZIP_ZCloseFile;
funcs->zerror_file = FSZIP_ZErrorFileFile;
}
//==========================================
// ZIP file (*.zip, *.pk3) - VFS Functions
//==========================================
typedef struct zipfile_s
{
char filename[MAX_QPATH];
unzFile handle;
int numfiles;
packfile_t *files;
#ifdef HASH_FILESYSTEM
hashtable_t hash;
#endif
zlib_filefunc_def zlib_funcs;
vfsfile_t *raw;
vfsfile_t *currentfile; //our unzip.c can only handle one active file at any one time
//so we have to keep closing and switching.
//slow, but it works. most of the time we'll only have a single file open anyway.
int references; //and a reference count
} zipfile_t;
typedef struct {
vfsfile_t funcs;
vfsfile_t *defer;
//in case we're forced away.
zipfile_t *parent;
qbool iscompressed;
int pos;
int length; //try and optimise some things
int index;
int startpos;
} vfszip_t;
// VFS-FIXME Need to figure what this function is trying to do
static void VFSZIP_MakeActive(vfszip_t *vfsz)
{
int i;
char buffer[8192]; //must be power of two
if ((vfszip_t*)vfsz->parent->currentfile == vfsz)
return; //already us
if (vfsz->parent->currentfile)
unzCloseCurrentFile(vfsz->parent->handle);
//unzLocateFileMy(vfsz->parent->handle, vfsz->index, vfsz->startpos);
unzSetOffset(vfsz->parent->handle, vfsz->startpos);
unzOpenCurrentFile(vfsz->parent->handle);
if (vfsz->pos > 0)
{
Com_DPrintf("VFSZIP_MakeActive: Shockingly inefficient\n");
//now we need to seek up to where we had previously gotten to.
for (i = 0; i < vfsz->pos-sizeof(buffer); i++)
unzReadCurrentFile(vfsz->parent->handle, buffer, sizeof(buffer));
unzReadCurrentFile(vfsz->parent->handle, buffer, vfsz->pos - i);
}
vfsz->parent->currentfile = (vfsfile_t*)vfsz;
}
static int VFSZIP_ReadBytes (struct vfsfile_s *file, void *buffer, int bytestoread, vfserrno_t *err)
{
int read;
vfszip_t *vfsz = (vfszip_t*)file;
if (vfsz->defer)
return VFS_READ(vfsz->defer, buffer, bytestoread, err);
// if (vfsz->iscompressed)
// {
VFSZIP_MakeActive(vfsz);
read = unzReadCurrentFile(vfsz->parent->handle, buffer, bytestoread);
// }
// else
// {
// VFS-FIXME This stuff seems to be only for optimisation,
// hence we can probably just go with unzReadCurrentFile
// if (vfsz->parent->currentfile != file)
// {
// unzCloseCurrentFile(vfsz->parent->handle);
// VFS_SEEK(vfsz->parent->raw, vfsz->pos+vfsz->startpos, SEEK_SET);
// vfsz->parent->currentfile = file;
// }
// read = VFS_READ(vfsz->parent->raw, buffer, bytestoread, err);
// }
if (err)
*err = ((read || bytestoread <= 0) ? VFSERR_NONE : VFSERR_EOF);
vfsz->pos += read;
return read;
}
static int VFSZIP_WriteBytes (struct vfsfile_s *file, const void *buffer, int bytestoread)
{
Sys_Error("VFSZIP_WriteBytes: Not supported\n");
return 0;
}
// VFS-FIXME: What is going on here... why not just unzSetOffset
static int VFSZIP_Seek (struct vfsfile_s *file, unsigned long pos, int whence)
{
vfszip_t *vfsz = (vfszip_t*)file;
if (vfsz->defer)
return VFS_SEEK(vfsz->defer, pos, whence);
//This is *really* inefficient
if (vfsz->parent->currentfile == file)
{
if (vfsz->iscompressed)
{ //if they're going to seek on a file in a zip, let's just copy it out
char buffer[8192];
unsigned int chunk;
unsigned int i;
unsigned int length;
vfsz->defer = FS_OpenTemp();
if (vfsz->defer)
{
unzCloseCurrentFile(vfsz->parent->handle);
vfsz->parent->currentfile = NULL; //make it not us
length = vfsz->length;
i = 0;
vfsz->pos = 0;
VFSZIP_MakeActive(vfsz);
while (1)
{
chunk = length - i;
if (chunk > sizeof(buffer))
chunk = sizeof(buffer);
if (chunk == 0)
break;
unzReadCurrentFile(vfsz->parent->handle, buffer, chunk);
VFS_WRITE(vfsz->defer, buffer, chunk);
i += chunk;
}
}
}
unzCloseCurrentFile(vfsz->parent->handle);
vfsz->parent->currentfile = NULL; //make it not us
if (vfsz->defer)
return VFS_SEEK(vfsz->defer, pos, SEEK_SET);
}
if (pos > vfsz->length)
return -1;
vfsz->pos = pos;
return 0;
}
static unsigned long VFSZIP_Tell (struct vfsfile_s *file)
{
vfszip_t *vfsz = (vfszip_t*)file;
if (vfsz->defer)
return VFS_TELL(vfsz->defer);
return vfsz->pos;
}
static unsigned long VFSZIP_GetLen (struct vfsfile_s *file)
{
vfszip_t *vfsz = (vfszip_t*)file;
return vfsz->length;
}
static void FSZIP_ClosePath(void *handle);
static void VFSZIP_Close (struct vfsfile_s *file)
{
vfszip_t *vfsz = (vfszip_t*)file;
if (vfsz->parent->currentfile == file)
vfsz->parent->currentfile = NULL; //make it not us
if (vfsz->defer)
VFS_CLOSE(vfsz->defer);
FSZIP_ClosePath(vfsz->parent);
Q_free(vfsz);
}
static vfsfile_t *FSZIP_OpenVFS(void *handle, flocation_t *loc, char *mode)
{
//int rawofs;
zipfile_t *zip = handle;
vfszip_t *vfsz;
if (strcmp(mode, "rb"))
return NULL; //urm, unable to write/append
vfsz = Q_calloc(1, sizeof(vfszip_t));
vfsz->parent = zip;
vfsz->index = loc->index;
vfsz->startpos = zip->files[loc->index].filepos;
vfsz->length = loc->len;
vfsz->funcs.ReadBytes = strcmp(mode, "rb") ? NULL : VFSZIP_ReadBytes;
vfsz->funcs.WriteBytes = strcmp(mode, "wb") ? NULL : VFSZIP_WriteBytes;
vfsz->funcs.Seek = VFSZIP_Seek;
vfsz->funcs.seekingisabadplan = true;
vfsz->funcs.Tell = VFSZIP_Tell;
vfsz->funcs.GetLen = VFSZIP_GetLen;
vfsz->funcs.Close = VFSZIP_Close;
if (loc->search)
vfsz->funcs.copyprotected = loc->search->copyprotected;
// VFS-FIXME:
// What is the point of is compressed etc
//unzLocateFileMy(vfsz->parent->handle, vfsz->index, vfsz->startpos);
//rawofs = unzGetCurrentFileUncompressedPos(zip->handle);
/*vfsz->iscompressed = 1; // rawofs<0;
if (!vfsz->iscompressed)
{
vfsz->startpos = rawofs;
VFS_SEEK(zip->raw, vfsz->startpos, SEEK_SET);
vfsz->parent->currentfile = (vfsfile_t*)vfsz;
}*/
zip->references++;
return (vfsfile_t*)vfsz;
}
//=============================================
// ZIP file (*.zip, *.pk3) - Search Functions
//=============================================
static void FSZIP_PrintPath(void *handle)
{
zipfile_t *zip = handle;
if (zip->references != 1)
Com_Printf("%s (%i)\n", zip->filename, zip->references-1);
else
Com_Printf("%s\n", zip->filename);
}
static void FSZIP_ClosePath(void *handle)
{
zipfile_t *zip = handle;
if (--zip->references > 0)
return; //not yet time
unzClose(zip->handle);
VFS_CLOSE(zip->raw);
if (zip->files)
Q_free(zip->files);
Q_free(zip);
}
static void FSZIP_BuildHash(void *handle)
{
zipfile_t *zip = handle;
int i;
for (i = 0; i < zip->numfiles; i++)
{
if (!Hash_GetInsensitive(filesystemhash, zip->files[i].name))
{
Hash_AddInsensitive(filesystemhash, zip->files[i].name, &zip->files[i]);
fs_hash_files++;
}
else
fs_hash_dups++;
}
}
static qbool FSZIP_FLocate(void *handle, flocation_t *loc, const char *filename, void *hashedresult)
{
packfile_t *pf = (packfile_t *) hashedresult;
zipfile_t *zip = (zipfile_t *) handle;
int i;
// look through all the pak file elements
if (pf)
{ //is this a pointer to a file in this pak?
if (pf < zip->files || pf >= zip->files + zip->numfiles)
return false; //was found in a different path
}
else
{
for (i=0 ; i<zip->numfiles ; i++) //look for the file
{
if (!strcasecmp (zip->files[i].name, filename))
{
pf = &zip->files[i];
break;
}
}
}
if (pf)
{
if (loc)
{
loc->index = pf - zip->files;
strlcpy (loc->rawname, zip->filename, sizeof (loc->rawname));
loc->offset = pf->filepos;
loc->len = pf->filelen;
loc->search = NULL;
// VFS-FIXME: What is the purpose of the stuff below....
// unzLocateFileMy (zip->handle, loc->index, zip->files[loc->index].filepos);
// loc->offset = unzGetCurrentFileUncompressedPos(zip->handle);
// if (loc->offset<0)
// { //file not found, or is compressed.
// *loc->rawname = '\0';
// loc->offset=0;
// }
}
return true;
}
return false;
}
static void FSZIP_ReadFile(void *handle, flocation_t *loc, char *buffer)
{
zipfile_t *zip = handle;
int err;
unzSetOffset(zip->handle, zip->files[loc->index].filepos);
unzOpenCurrentFile (zip->handle);
err = unzReadCurrentFile (zip->handle, buffer, zip->files[loc->index].filelen);
unzCloseCurrentFile (zip->handle);
if (err!=zip->files[loc->index].filelen)
{
Com_Printf ("Can't extract file \"%s:%s\" (corrupt)\n", zip->filename, zip->files[loc->index].name);
return;
}
return;
}
static int FSZIP_EnumerateFiles (void *handle, char *match, int (*func)(char *, int, void *), void *parm)
{
zipfile_t *zip = handle;
int num;
for (num = 0; num < zip->numfiles; num++)
{
if (wildcmp(match, zip->files[num].name))
{
if (!func(zip->files[num].name, zip->files[num].filelen, parm))
return false;
}
}
return true;
}
/*
=================
COM_LoadZipFile
Takes an explicit (not game tree related) path to a pak file.
Loads the header and directory, adding the files at the beginning
of the list so they override previous pack files.
=================
*/
static void *FSZIP_LoadZipFile(vfsfile_t *packhandle, const char *desc)
{
int i, r;
zipfile_t *zip;
packfile_t *newfiles;
zlib_filefunc_def *funcs = NULL;
unz_global_info info;
zip = (zipfile_t *) Q_calloc(1, sizeof(*zip));
strlcpy (zip->filename, desc, sizeof (zip->filename));
FSZIP_CreteFileFuncs(&(zip->zlib_funcs));
zip->raw = packhandle;
zip->handle = unzOpen2(desc, funcs);
if (!zip->handle) goto fail;
if (unzGetGlobalInfo(zip->handle, &info) != UNZ_OK) goto fail;
// Get the number of zip files
zip->numfiles = info.number_entry;
// Create a list of the number of files
zip->files = newfiles = Q_malloc (zip->numfiles * sizeof(packfile_t));
if (unzGoToFirstFile(zip->handle) != UNZ_OK) goto fail;
for (i = 0; i < zip->numfiles; i++) {
unz_file_info file_info;
if (unzGetCurrentFileInfo(zip->handle, &file_info, newfiles[i].name, sizeof(newfiles[i].name), NULL, 0, NULL, 0) != UNZ_OK) goto fail;
Q_strlwr(newfiles[i].name);
newfiles[i].filelen = file_info.uncompressed_size;
newfiles[i].filepos = unzGetOffset(zip->handle); // VFS-FIXME: Need to verify this
r = unzGoToNextFile (zip->handle);
if (r == UNZ_END_OF_LIST_OF_FILE) {
break;
} else if (r != UNZ_OK) {
goto fail;
}
}
zip->references = 1;
zip->currentfile = NULL;
return zip;
fail:
// Q_free is safe to call on NULL pointers
Q_free(funcs);
Q_free(zip->files);
Q_free(zip);
return NULL;
}
// VFS-FIXME: Don't really seem to know what this does
static int FSZIP_GeneratePureCRC(void *handle, int seed, int crctype)
{
zipfile_t *zip = handle;
unz_file_info file_info;
int *filecrcs;
int numcrcs=0;
int i;
filecrcs = Q_malloc((zip->numfiles+1)*sizeof(int));
filecrcs[numcrcs++] = seed;
unzGoToFirstFile(zip->handle);
for (i = 0; i < zip->numfiles; i++)
{
if (zip->files[i].filelen>0)
{
unzGetCurrentFileInfo (zip->handle, &file_info, NULL, 0, NULL, 0, NULL, 0);
filecrcs[numcrcs++] = file_info.crc;
}
unzGoToNextFile (zip->handle);
}
if (crctype)
return Com_BlockChecksum(filecrcs, numcrcs*sizeof(int));
else
return Com_BlockChecksum(filecrcs+1, (numcrcs-1)*sizeof(int));
}
searchpathfuncs_t zipfilefuncs = {
FSZIP_PrintPath,
FSZIP_ClosePath,
FSZIP_BuildHash,
FSZIP_FLocate,
FSZIP_ReadFile,
FSZIP_EnumerateFiles,
FSZIP_LoadZipFile,
FSZIP_GeneratePureCRC,
FSZIP_OpenVFS
};
#endif // WITH_ZIP
|