2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* ZIP support routines for PhysicsFS.
|
|
|
|
*
|
|
|
|
* Please see the file LICENSE in the source's root directory.
|
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-07-15 11:29:30 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2001-07-08 12:58:10 +02:00
|
|
|
#include "physfs.h"
|
2001-07-15 11:29:30 +02:00
|
|
|
#include "unzip.h"
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
|
|
|
#if (!defined PHYSFS_SUPPORTS_ZIP)
|
|
|
|
#error PHYSFS_SUPPORTS_ZIP must be defined.
|
|
|
|
#endif
|
|
|
|
|
2001-07-15 11:29:30 +02:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unzFile handle;
|
|
|
|
uLong totalEntries;
|
|
|
|
} ZIPinfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
} ZIPfileinfo;
|
|
|
|
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
|
2001-07-08 12:58:10 +02:00
|
|
|
static const FileFunctions __PHYSFS_FileFunctions_ZIP;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int ZIP_read(FileHandle *handle, void *buffer,
|
|
|
|
unsigned int objSize, unsigned int objCount)
|
|
|
|
{
|
|
|
|
} /* ZIP_read */
|
|
|
|
|
|
|
|
|
|
|
|
static int ZIP_eof(FileHandle *handle)
|
|
|
|
{
|
|
|
|
} /* ZIP_eof */
|
|
|
|
|
|
|
|
|
|
|
|
static int ZIP_tell(FileHandle *handle)
|
|
|
|
{
|
|
|
|
} /* ZIP_tell */
|
|
|
|
|
|
|
|
|
|
|
|
static int ZIP_seek(FileHandle *handle, int offset)
|
|
|
|
{
|
|
|
|
} /* ZIP_seek */
|
|
|
|
|
|
|
|
|
2001-07-09 06:15:35 +02:00
|
|
|
static int ZIP_fileLength(FileHandle *handle)
|
|
|
|
{
|
|
|
|
} /* ZIP_fileLength */
|
|
|
|
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
static int ZIP_fileClose(FileHandle *handle)
|
|
|
|
{
|
|
|
|
} /* ZIP_fileClose */
|
|
|
|
|
|
|
|
|
|
|
|
static int ZIP_isArchive(const char *filename, int forWriting)
|
|
|
|
{
|
2001-07-15 11:29:30 +02:00
|
|
|
int retval = 0;
|
|
|
|
unzFile unz = unzOpen(name);
|
|
|
|
unz_global_info global;
|
|
|
|
|
|
|
|
if (unz != NULL)
|
|
|
|
{
|
|
|
|
if (unzGetGlobalInfo(unz, &global) == UNZ_OK)
|
|
|
|
retval = 1;
|
|
|
|
unzClose(unz);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_isArchive */
|
|
|
|
|
|
|
|
|
|
|
|
static DirHandle *ZIP_openArchive(const char *name, int forWriting)
|
|
|
|
{
|
2001-07-15 11:29:30 +02:00
|
|
|
unzFile unz = NULL;
|
|
|
|
DirHandle *retval = NULL;
|
|
|
|
unz_global_info global;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
BAIL_IF_MACRO(access(name, R_OK) != 0, strerror(errno), NULL);
|
|
|
|
|
|
|
|
retval = malloc(sizeof (DirHandle));
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
|
unz = unzOpen(name);
|
|
|
|
if ((unz == NULL) || (unzGetGlobalInfo(unz, &global) != UNZ_OK))
|
|
|
|
{
|
|
|
|
if (unz)
|
|
|
|
unzClose(unz);
|
2001-07-16 12:31:28 +02:00
|
|
|
free(retval);
|
2001-07-15 11:29:30 +02:00
|
|
|
BAIL_IF_MACRO(1, ERR_UNSUPPORTED_ARCHIVE, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
retval->opaque = malloc(sizeof (ZIPinfo));
|
|
|
|
if (retval->opaque == NULL)
|
|
|
|
{
|
|
|
|
free(retval);
|
|
|
|
unzClose(unz);
|
|
|
|
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
((ZIPinfo *) (retval->opaque))->handle = unz;
|
2001-07-16 12:31:28 +02:00
|
|
|
((ZIPinfo *) (retval->opaque))->totalEntries = global.number_entry;
|
2001-07-15 11:29:30 +02:00
|
|
|
|
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_openArchive */
|
|
|
|
|
|
|
|
|
2001-07-16 19:36:28 +02:00
|
|
|
static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
|
|
|
|
const char *dirname,
|
|
|
|
int omitSymLinks)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2001-07-15 11:29:30 +02:00
|
|
|
ZIPinfo *zi = (ZIPinfo *) (h->opaque);
|
|
|
|
unzFile fh = zi->handle;
|
|
|
|
int i;
|
|
|
|
LinkedStringList *retval = NULL;
|
|
|
|
LinkedStringList *l = NULL;
|
|
|
|
LinkedStringList *prev = NULL;
|
|
|
|
char buffer[256];
|
|
|
|
char *d;
|
|
|
|
|
|
|
|
/* jump to first file entry... */
|
|
|
|
BAIL_IF_MACRO(unzGoToFirstFile(fh) != UNZ_OK, ERR_IO_ERROR, NULL);
|
|
|
|
|
|
|
|
i = strlen(dirname);
|
|
|
|
d = malloc(i + 1);
|
|
|
|
strcpy(d, dirname);
|
|
|
|
if ((i > 0) && (d[i - 1] == '/')) /* no trailing slash. */
|
|
|
|
d[i - 1] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < zi->totalEntries; i++)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
unzGetCurrentFileInfo(fh, NULL, buf, sizeof (buf), NULL, 0, NULL, 0);
|
|
|
|
ptr = strrchr(p, '/'); /* !!! check this! */
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
if (*d != '\0')
|
|
|
|
continue; /* not for this dir; skip it. */
|
|
|
|
ptr = buf;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
ptr++;
|
|
|
|
if (strcmp(buf, d) != 0)
|
|
|
|
continue; /* not for this dir; skip it. */
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
|
2001-07-16 16:36:02 +02:00
|
|
|
if (l == NULL)
|
2001-07-15 11:29:30 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
l->str = (char *) malloc(strlen(ptr) + 1);
|
|
|
|
if (l->str == NULL)
|
|
|
|
{
|
|
|
|
free(l);
|
|
|
|
break;
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
strcpy(l->str, ptr);
|
|
|
|
|
2001-07-15 11:29:30 +02:00
|
|
|
if (retval == NULL)
|
|
|
|
retval = l;
|
|
|
|
else
|
|
|
|
prev->next = l;
|
|
|
|
|
|
|
|
prev = l;
|
|
|
|
l->next = NULL;
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
free(d);
|
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_enumerateFiles */
|
|
|
|
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static int ZIP_exists(DirHandle *h, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
} /* ZIP_exists */
|
|
|
|
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static int ZIP_isDirectory(DirHandle *h, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
} /* ZIP_isDirectory */
|
|
|
|
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static int ZIP_isSymLink(DirHandle *h, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
} /* ZIP_isSymLink */
|
|
|
|
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static FileHandle *ZIP_openRead(DirHandle *h, const char *filename)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
} /* ZIP_openRead */
|
|
|
|
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static void ZIP_dirClose(DirHandle *h)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
} /* ZIP_dirClose */
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
static const FileFunctions __PHYSFS_FileFunctions_ZIP =
|
2001-07-07 10:24:47 +02:00
|
|
|
{
|
2001-07-09 06:15:35 +02:00
|
|
|
ZIP_read, /* read() method */
|
|
|
|
NULL, /* write() method */
|
|
|
|
ZIP_eof, /* eof() method */
|
|
|
|
ZIP_tell, /* tell() method */
|
|
|
|
ZIP_seek, /* seek() method */
|
|
|
|
ZIP_fileLength, /* fileLength() method */
|
|
|
|
ZIP_fileClose /* fileClose() method */
|
2001-07-07 10:24:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const DirFunctions __PHYSFS_DirFunctions_ZIP =
|
|
|
|
{
|
2001-07-08 05:25:12 +02:00
|
|
|
ZIP_isArchive, /* isArchive() method */
|
|
|
|
ZIP_openArchive, /* openArchive() method */
|
|
|
|
ZIP_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
ZIP_exists, /* exists() method */
|
|
|
|
ZIP_isDirectory, /* isDirectory() method */
|
|
|
|
ZIP_isSymLink, /* isSymLink() method */
|
|
|
|
ZIP_openRead, /* openRead() method */
|
|
|
|
NULL, /* openWrite() method */
|
|
|
|
NULL, /* openAppend() method */
|
|
|
|
NULL, /* remove() method */
|
|
|
|
NULL, /* mkdir() method */
|
2001-07-08 12:58:10 +02:00
|
|
|
ZIP_dirClose /* dirClose() method */
|
2001-07-07 10:24:47 +02:00
|
|
|
};
|
|
|
|
|
2001-07-08 12:58:10 +02:00
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
|
2001-07-07 10:24:47 +02:00
|
|
|
{
|
|
|
|
"ZIP",
|
2001-07-10 01:33:37 +02:00
|
|
|
"PkZip/WinZip/Info-Zip compatible",
|
2001-07-16 12:31:28 +02:00
|
|
|
"Ryan C. Gordon (icculus@linuxgames.com)",
|
|
|
|
"http://www.icculus.org/~icculus/",
|
2001-07-07 10:24:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* end of zip.c ... */
|
|
|
|
|