// This is part of Pico Library\r
\r
// (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006 notaz, All rights reserved.\r
+// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r
// Free for non-commercial use.\r
\r
// For commercial use, separate licencing terms must be obtained.\r
\r
\r
#include "PicoInt.h"\r
+#include "../zlib/zlib.h"\r
+#include "../unzip/unzip.h"\r
+#include "../unzip/unzip_stream.h"\r
+\r
+\r
+static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
+\r
+void (*PicoCartLoadProgressCB)(int percent) = NULL;\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
+ /* we use our own buffering */\r
+ setvbuf(f, NULL, _IONBF, 0);\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
+ fseek(stream->file, offset, whence);\r
+ return ftell(stream->file);\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
+ }\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
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
+ unsigned char *rom=NULL; int size, bytes_read;\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
// Allocate space for the rom plus padding\r
rom=PicoCartAlloc(size);\r
- if (rom==NULL) return 1; // { fclose(f); return 1; }\r
+ if (rom==NULL) {\r
+ printf("out of memory (wanted %i)\n", size);\r
+ return 1;\r
+ }\r
\r
- fread(rom,1,size,f); // Load up the rom\r
+ if (PicoCartLoadProgressCB != NULL)\r
+ {\r
+ // read ROM in blocks, just for fun\r
+ int ret;\r
+ unsigned char *p = rom;\r
+ bytes_read=0;\r
+ do\r
+ {\r
+ int todo = size - bytes_read;\r
+ if (todo > 256*1024) todo = 256*1024;\r
+ ret = pm_read(p,todo,f);\r
+ bytes_read += ret;\r
+ p += ret;\r
+ PicoCartLoadProgressCB(bytes_read * 100 / size);\r
+ }\r
+ while (ret > 0);\r
+ }\r
+ else\r
+ bytes_read = pm_read(rom,size,f); // Load up the rom\r
+ if (bytes_read <= 0) {\r
+ printf("read failed\n");\r
+ free(rom);\r
+ return 1;\r
+ }\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
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