support for zipped ISOs
[picodrive.git] / Pico / Cart.c
index d3e7743..0762473 100644 (file)
@@ -8,6 +8,152 @@
 \r
 \r
 #include "PicoInt.h"\r
+#include "../zlib/zlib.h"\r
+#include "../unzip/unzip.h"\r
+#include "../unzip/unzip_stream.h"\r
+\r
+static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
+\r
+\r
+pm_file *pm_open(const char *path)\r
+{\r
+  pm_file *file = NULL;\r
+  const char *ext;\r
+  FILE *f;\r
+\r
+  if (path == NULL) return NULL;\r
+\r
+  if (strlen(path) < 5) ext = NULL; // no ext\r
+  else ext = path + strlen(path) - 3;\r
+\r
+  if (ext && strcasecmp(ext, "zip") == 0)\r
+  {\r
+    struct zipent *zipentry;\r
+    gzFile gzf = NULL;\r
+    ZIP *zipfile;\r
+    int i;\r
+\r
+    zipfile = openzip(path);\r
+\r
+    if (zipfile != NULL)\r
+    {\r
+      /* search for suitable file (right extension or large enough file) */\r
+      while ((zipentry = readzip(zipfile)) != NULL)\r
+      {\r
+        if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;\r
+        if (strlen(zipentry->name) < 5) continue;\r
+\r
+        ext = zipentry->name+strlen(zipentry->name)-3;\r
+        for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
+          if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
+      }\r
+\r
+      /* zipfile given, but nothing found suitable for us inside */\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 = malloc(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
+      return file;\r
+\r
+zip_failed:\r
+      if (gzf) {\r
+        gzclose(gzf);\r
+        zipfile->fp = NULL; // gzclose() closed it\r
+      }\r
+      closezip(zipfile);\r
+      return NULL;\r
+    }\r
+  }\r
+\r
+  /* not a zip, treat as uncompressed file */\r
+  f = fopen(path, "rb");\r
+  if (f == NULL) return NULL;\r
+\r
+  file = malloc(sizeof(*file));\r
+  if (file == NULL) {\r
+    fclose(f);\r
+    return NULL;\r
+  }\r
+  fseek(f, 0, SEEK_END);\r
+  file->file  = f;\r
+  file->param = NULL;\r
+  file->size  = ftell(f);\r
+  file->type  = PMT_UNCOMPRESSED;\r
+  fseek(f, 0, SEEK_SET);\r
+  return file;\r
+}\r
+\r
+size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
+{\r
+  int ret;\r
+\r
+  if (stream->type == PMT_UNCOMPRESSED)\r
+  {\r
+    ret = fread(ptr, 1, bytes, stream->file);\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
+  }\r
+  else\r
+    ret = 0;\r
+\r
+  return ret;\r
+}\r
+\r
+int pm_seek(pm_file *stream, long offset, int whence)\r
+{\r
+  if (stream->type == PMT_UNCOMPRESSED)\r
+  {\r
+    return fseek(stream->file, offset, whence);\r
+  }\r
+  else if (stream->type == PMT_ZIP)\r
+  {\r
+    return gzseek((gzFile) stream->param, offset, whence);\r
+  }\r
+  else\r
+    return -1;\r
+}\r
+\r
+int pm_close(pm_file *fp)\r
+{\r
+  int ret = 0;\r
+\r
+  if (fp == NULL) return EOF;\r
+\r
+  if (fp->type == PMT_UNCOMPRESSED)\r
+  {\r
+    fclose(fp->file);\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
+  }\r
+  else\r
+    ret = EOF;\r
+\r
+  free(fp);\r
+  return ret;\r
+}\r
+\r
 \r
 void Byteswap(unsigned char *data,int len)\r
 {\r
@@ -86,12 +232,12 @@ static unsigned char *PicoCartAlloc(int filesize)
   return rom;\r
 }\r
 \r
-int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)\r
+int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
 {\r
   unsigned char *rom=NULL; int size;\r
   if (f==NULL) return 1;\r
 \r
-  fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);\r
+  size=f->size;\r
   if (size <= 0) return 1;\r
   size=(size+3)&~3; // Round up to a multiple of 4\r
 \r
@@ -99,7 +245,7 @@ int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
   rom=PicoCartAlloc(size);\r
   if (rom==NULL) return 1; // { fclose(f); return 1; }\r
 \r
-  fread(rom,1,size,f); // Load up the rom\r
+  pm_read(rom,size,f); // Load up the rom\r
 \r
   // maybe we are loading MegaCD BIOS?\r
   if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
@@ -139,69 +285,3 @@ int PicoUnloadCart(unsigned char* romdata)
   return 0;\r
 }\r
 \r
-\r
-#ifdef _UNZIP_SUPPORT\r
-\r
-// notaz\r
-#include "../unzip/unzip.h"\r
-\r
-// nearly same as PicoCartLoad, but works with zipfiles\r
-int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize)\r
-{\r
-       unsigned char *rom=0;\r
-       struct zipent* zipentry;\r
-       int size;\r
-       ZIP *zipfile = openzip(fname);\r
-\r
-       if(!zipfile) return 1;\r
-\r
-       // find first bin or smd\r
-       while((zipentry = readzip(zipfile)) != 0)\r
-       {\r
-               char *ext;\r
-               if(strlen(zipentry->name) < 5) continue;\r
-               ext = zipentry->name+strlen(zipentry->name)-4;\r
-\r
-               if(!strcasecmp(ext, ".bin") || !strcasecmp(ext, ".smd") || !strcasecmp(ext, ".gen")) break;\r
-       }\r
-\r
-       if(!zipentry) {\r
-               closezip(zipfile);\r
-               return 4; // no roms\r
-       }\r
-\r
-       size = zipentry->uncompressed_size;\r
-\r
-       size=(size+3)&~3; // Round up to a multiple of 4\r
-\r
-       // Allocate space for the rom plus padding\r
-       rom=PicoCartAlloc(size);\r
-       if (rom==NULL) { closezip(zipfile); return 2; }\r
-\r
-       if(readuncompresszip(zipfile, zipentry, (char *)rom) != 0) {\r
-               free(rom);\r
-               rom = 0;\r
-               closezip(zipfile);\r
-               return 5; // unzip failed\r
-       }\r
-\r
-       closezip(zipfile);\r
-\r
-        // maybe we are loading MegaCD BIOS?\r
-        if (!(PicoMCD&1) && size == 0x20000 &&\r
-                       (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
-               PicoMCD |= 1;\r
-               rom = cd_realloc(rom, size);\r
-        }\r
-\r
-       // Check for SMD:\r
-       if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
-       else Byteswap(rom,size); // Just byteswap\r
-\r
-       if (prom)  *prom=rom;\r
-       if (psize) *psize=size;\r
-\r
-       return 0;\r
-}\r
-\r
-#endif\r