#include "../zlib/zlib.h"\r
#include "../cpu/debug.h"\r
#include "../unzip/unzip.h"\r
-#include "../unzip/unzip_stream.h"\r
\r
\r
static int rom_alloc_size;\r
return ext;\r
}\r
\r
+struct zip_file {\r
+ pm_file file;\r
+ ZIP *zip;\r
+ struct zipent *entry;\r
+ z_stream stream;\r
+ unsigned char inbuf[16384];\r
+ long start;\r
+ unsigned int pos;\r
+};\r
+\r
pm_file *pm_open(const char *path)\r
{\r
pm_file *file = NULL;\r
ext = get_ext(path);\r
if (strcasecmp(ext, "zip") == 0)\r
{\r
+ struct zip_file *zfile = NULL;\r
struct zipent *zipentry;\r
- gzFile gzf = NULL;\r
ZIP *zipfile;\r
- int i;\r
+ int i, ret;\r
\r
zipfile = openzip(path);\r
if (zipfile != NULL)\r
goto zip_failed;\r
\r
found_rom_zip:\r
- /* try to convert to gzip stream, so we could use standard gzio functions from zlib */\r
- gzf = zip2gz(zipfile, zipentry);\r
- if (gzf == NULL) goto zip_failed;\r
-\r
- file = calloc(1, sizeof(*file));\r
- if (file == NULL) goto zip_failed;\r
- file->file = zipfile;\r
- file->param = gzf;\r
- file->size = zipentry->uncompressed_size;\r
- file->type = PMT_ZIP;\r
- strncpy(file->ext, ext, sizeof(file->ext) - 1);\r
- return file;\r
+ zfile = calloc(1, sizeof(*zfile));\r
+ if (zfile == NULL)\r
+ goto zip_failed;\r
+ ret = seekcompresszip(zipfile, zipentry);\r
+ if (ret != 0)\r
+ goto zip_failed;\r
+ ret = inflateInit2(&zfile->stream, -15);\r
+ if (ret != Z_OK) {\r
+ elprintf(EL_STATUS, "zip: inflateInit2 %d", ret);\r
+ goto zip_failed;\r
+ }\r
+ zfile->zip = zipfile;\r
+ zfile->entry = zipentry;\r
+ zfile->start = ftell(zipfile->fp);\r
+ zfile->file.file = zfile;\r
+ zfile->file.size = zipentry->uncompressed_size;\r
+ zfile->file.type = PMT_ZIP;\r
+ strncpy(zfile->file.ext, ext, sizeof(zfile->file.ext) - 1);\r
+ return &zfile->file;\r
\r
zip_failed:\r
- if (gzf) {\r
- gzclose(gzf);\r
- zipfile->fp = NULL; // gzclose() closed it\r
- }\r
closezip(zipfile);\r
+ free(zfile);\r
return NULL;\r
}\r
}\r
}\r
else if (stream->type == PMT_ZIP)\r
{\r
- gzFile gf = stream->param;\r
- int err;\r
- ret = gzread(gf, ptr, bytes);\r
- err = gzerror2(gf);\r
- if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))\r
- /* we must reset stream pointer or else next seek/read fails */\r
- gzrewind(gf);\r
+ struct zip_file *z = stream->file;\r
+\r
+ if (z->entry->compression_method == 0) {\r
+ int ret = fread(ptr, 1, bytes, z->zip->fp);\r
+ z->pos += ret;\r
+ return ret;\r
+ }\r
+\r
+ z->stream.next_out = ptr;\r
+ z->stream.avail_out = bytes;\r
+ while (z->stream.avail_out != 0) {\r
+ if (z->stream.avail_in == 0) {\r
+ z->stream.avail_in = fread(z->inbuf, 1, sizeof(z->inbuf), z->zip->fp);\r
+ if (z->stream.avail_in == 0)\r
+ break;\r
+ z->stream.next_in = z->inbuf;\r
+ }\r
+ ret = inflate(&z->stream, Z_NO_FLUSH);\r
+ if (ret == Z_STREAM_END)\r
+ break;\r
+ if (ret != Z_OK) {\r
+ elprintf(EL_STATUS, "zip: inflate: %d", ret);\r
+ return 0;\r
+ }\r
+ }\r
+ z->pos += bytes - z->stream.avail_out;\r
+ return bytes - z->stream.avail_out;\r
}\r
else if (stream->type == PMT_CSO)\r
{\r
}\r
else if (stream->type == PMT_ZIP)\r
{\r
- if (PicoMessage != NULL && offset > 6*1024*1024) {\r
- long pos = gztell((gzFile) stream->param);\r
- if (offset < pos || offset - pos > 6*1024*1024)\r
- PicoMessage("Decompressing data...");\r
+ struct zip_file *z = stream->file;\r
+ unsigned int pos = z->pos;\r
+ int ret;\r
+\r
+ switch (whence)\r
+ {\r
+ case SEEK_CUR: pos += offset; break;\r
+ case SEEK_SET: pos = offset; break;\r
+ case SEEK_END: pos = stream->size - offset; break;\r
+ }\r
+ if (z->entry->compression_method == 0) {\r
+ ret = fseek(z->zip->fp, z->start + pos, SEEK_SET);\r
+ if (ret == 0)\r
+ return (z->pos = pos);\r
+ return -1;\r
+ }\r
+ offset = pos - z->pos;\r
+ if (pos < z->pos) {\r
+ // full decompress from the start\r
+ fseek(z->zip->fp, z->start, SEEK_SET);\r
+ z->stream.avail_in = 0;\r
+ z->stream.next_in = z->inbuf;\r
+ inflateReset(&z->stream);\r
+ z->pos = 0;\r
+ offset = pos;\r
+ }\r
+\r
+ if (PicoMessage != NULL && offset > 4 * 1024 * 1024)\r
+ PicoMessage("Decompressing data...");\r
+\r
+ while (offset > 0) {\r
+ char buf[16 * 1024];\r
+ size_t l = offset > sizeof(buf) ? sizeof(buf) : offset;\r
+ ret = pm_read(buf, l, stream);\r
+ if (ret != l)\r
+ break;\r
+ offset -= l;\r
}\r
- return gzseek((gzFile) stream->param, offset, whence);\r
+ return z->pos;\r
}\r
else if (stream->type == PMT_CSO)\r
{\r
}\r
else if (fp->type == PMT_ZIP)\r
{\r
- ZIP *zipfile = fp->file;\r
- gzclose((gzFile) fp->param);\r
- zipfile->fp = NULL; // gzclose() closed it\r
- closezip(zipfile);\r
+ struct zip_file *z = fp->file;\r
+ inflateEnd(&z->stream);\r
+ closezip(z->zip);\r
}\r
else if (fp->type == PMT_CSO)\r
{\r
+++ /dev/null
-/* seekable zip */
-
-#include "unzip.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include "zlib/zlib.h"
-
-
-#define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
-
-
-/* from gzio.c . Be careful with binary compatibility */
-typedef struct gz_stream {
- z_stream stream;
- int z_err; /* error code for last stream operation */
- int z_eof; /* set if end of input file */
- FILE *file; /* .gz file */
- Byte *inbuf; /* input buffer */
- Byte *outbuf; /* output buffer */
- uLong crc; /* crc32 of uncompressed data */
- char *msg; /* error message */
- char *path; /* path name for debugging only */
- int transparent; /* 1 if input file is not a .gz file */
- char mode; /* 'w' or 'r' */
- z_off_t start; /* start of compressed data in file (header skipped) */
- z_off_t in; /* bytes into deflate or inflate */
- z_off_t out; /* bytes out of deflate or inflate */
- int back; /* one character push-back */
- int last; /* true if push-back is last character */
-} gz_stream;
-
-#ifndef Z_BUFSIZE
-# ifdef MAXSEG_64K
-# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
-# else
-# define Z_BUFSIZE 16384
-# endif
-#endif
-#ifndef Z_PRINTF_BUFSIZE
-# define Z_PRINTF_BUFSIZE 4096
-#endif
-
-#define ALLOC(size) malloc(size)
-
-int destroy OF((gz_stream *s));
-
-
-gzFile zip2gz(ZIP* zip, struct zipent* ent)
-{
- int err;
- gz_stream *s;
- const char *path;
- int transparent = 0;
- uInt len;
-
- if (!zip || !ent)
- return NULL;
-
- /* zip stuff */
- if (ent->compression_method == 0x0000)
- {
- /* file is not compressed, simply stored */
-
- /* check if size are equal */
- if (ent->compressed_size != ent->uncompressed_size) {
- errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
- return NULL;
- }
-
- transparent = 1;
- }
- else if (ent->compression_method == 0x0008)
- {
- /* file is compressed using "Deflate" method */
- if (ent->version_needed_to_extract > 0x14) {
- errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
- return NULL;
- }
-
- if (ent->os_needed_to_extract != 0x00) {
- errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
- return NULL;
- }
-
- if (ent->disk_number_start != zip->number_of_this_disk) {
- errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
- return NULL;
- }
-
- } else {
- errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
- return NULL;
- }
-
- /* seek to compressed data */
- if (seekcompresszip(zip,ent) != 0) {
- return NULL;
- }
-
- path = zip->zip;
-
- /* normal gzip init for read */
- s = (gz_stream *)ALLOC(sizeof(gz_stream));
- if (!s) return Z_NULL;
-
- s->stream.zalloc = (alloc_func)0;
- s->stream.zfree = (free_func)0;
- s->stream.opaque = (voidpf)0;
- s->stream.next_in = s->inbuf = Z_NULL;
- s->stream.next_out = s->outbuf = Z_NULL;
- s->stream.avail_in = s->stream.avail_out = 0;
- s->file = NULL;
- s->z_err = Z_OK;
- s->z_eof = 0;
- s->in = 0;
- s->out = 0;
- s->back = EOF;
- s->crc = crc32(0L, Z_NULL, 0);
- s->msg = NULL;
- s->transparent = transparent;
- s->mode = 'r';
-
- s->path = (char*)ALLOC(strlen(path)+1);
- if (s->path == NULL) {
- return destroy(s), (gzFile)Z_NULL;
- }
- strcpy(s->path, path); /* do this early for debugging */
-
- s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
-
- err = inflateInit2(&(s->stream), -MAX_WBITS);
- /* windowBits is passed < 0 to tell that there is no zlib header.
- * Note that in this case inflate *requires* an extra "dummy" byte
- * after the compressed stream in order to complete decompression and
- * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
- * present after the compressed stream.
- */
- if (err != Z_OK || s->inbuf == Z_NULL) {
- return destroy(s), (gzFile)Z_NULL;
- }
- s->stream.avail_out = Z_BUFSIZE;
-
- errno = 0;
- s->file = zip->fp;
- if (s->file == NULL) {
- return destroy(s), (gzFile)Z_NULL;
- }
-
- /* check_header(s); */
- errno = 0;
- len = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
- if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
- s->stream.avail_in += len;
- s->stream.next_in = s->inbuf;
- if (s->stream.avail_in < 2) {
- return destroy(s), (gzFile)Z_NULL;
- }
-
- s->start = ftell(s->file) - s->stream.avail_in;
-
- return (gzFile)s;
-}
-
-
-int gzerror2(gzFile file)
-{
- gz_stream *s = (gz_stream*)file;
-
- if (s == NULL)
- return Z_STREAM_ERROR;
-
- return s->z_err;
-}
-
-