+#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
+void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c\r
+\r
+/* cso struct */\r
+typedef struct _cso_struct\r
+{\r
+ unsigned char in_buff[2*2048];\r
+ unsigned char out_buff[2048];\r
+ struct {\r
+ char magic[4];\r
+ unsigned int unused;\r
+ unsigned int total_bytes;\r
+ unsigned int total_bytes_high; // ignored here\r
+ unsigned int block_size; // 10h\r
+ unsigned char ver;\r
+ unsigned char align;\r
+ unsigned char reserved[2];\r
+ } header;\r
+ unsigned int fpos_in; // input file read pointer\r
+ unsigned int fpos_out; // pos in virtual decompressed file\r
+ int block_in_buff; // block which we have read in in_buff\r
+ int pad;\r
+ int index[0];\r
+}\r
+cso_struct;\r
+\r
+static int uncompress2(void *dest, int destLen, void *source, int sourceLen)\r
+{\r
+ z_stream stream;\r
+ int err;\r
+\r
+ stream.next_in = (Bytef*)source;\r
+ stream.avail_in = (uInt)sourceLen;\r
+ stream.next_out = dest;\r
+ stream.avail_out = (uInt)destLen;\r
+\r
+ stream.zalloc = NULL;\r
+ stream.zfree = NULL;\r
+\r
+ err = inflateInit2(&stream, -15);\r
+ if (err != Z_OK) return err;\r
+\r
+ err = inflate(&stream, Z_FINISH);\r
+ if (err != Z_STREAM_END) {\r
+ inflateEnd(&stream);\r
+ return err;\r
+ }\r
+ //*destLen = stream.total_out;\r
+\r
+ return inflateEnd(&stream);\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
+ else if (ext && strcasecmp(ext, "cso") == 0)\r
+ {\r
+ cso_struct *cso = NULL, *tmp = NULL;\r
+ int size;\r
+ f = fopen(path, "rb");\r
+ if (f == NULL)\r
+ goto cso_failed;\r
+\r
+ cso = malloc(sizeof(*cso));\r
+ if (cso == NULL)\r
+ goto cso_failed;\r
+\r
+ if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))\r
+ goto cso_failed;\r
+\r
+ if (strncmp(cso->header.magic, "CISO", 4) != 0) {\r
+ elprintf(EL_STATUS, "cso: bad header");\r
+ goto cso_failed;\r
+ }\r
+\r
+ if (cso->header.block_size != 2048) {\r
+ elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);\r
+ goto cso_failed;\r
+ }\r
+\r
+ size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);\r
+ tmp = realloc(cso, size);\r
+ if (tmp == NULL)\r
+ goto cso_failed;\r
+ cso = tmp;\r
+ elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);\r
+\r
+ size -= sizeof(*cso); // index size\r
+ if (fread(cso->index, 1, size, f) != size) {\r
+ elprintf(EL_STATUS, "cso: premature EOF");\r
+ goto cso_failed;\r
+ }\r
+\r
+ // all ok\r
+ cso->fpos_in = ftell(f);\r
+ cso->fpos_out = 0;\r
+ cso->block_in_buff = -1;\r
+ file = malloc(sizeof(*file));\r
+ if (file == NULL) goto cso_failed;\r
+ file->file = f;\r
+ file->param = cso;\r
+ file->size = cso->header.total_bytes;\r
+ file->type = PMT_CSO;\r
+ return file;\r
+\r
+cso_failed:\r
+ if (cso != NULL) free(cso);\r
+ if (f != NULL) fclose(f);\r
+ return NULL;\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 if (stream->type == PMT_CSO)\r
+ {\r
+ cso_struct *cso = stream->param;\r
+ int read_pos, read_len, out_offs, rret;\r
+ int block = cso->fpos_out >> 11;\r
+ int index = cso->index[block];\r
+ int index_end = cso->index[block+1];\r
+ unsigned char *out = ptr, *tmp_dst;\r
+\r
+ ret = 0;\r
+ while (bytes != 0)\r
+ {\r
+ out_offs = cso->fpos_out&0x7ff;\r
+ if (out_offs == 0 && bytes >= 2048)\r
+ tmp_dst = out;\r
+ else tmp_dst = cso->out_buff;\r
+\r
+ read_pos = (index&0x7fffffff) << cso->header.align;\r
+\r
+ if (index < 0) {\r
+ if (read_pos != cso->fpos_in)\r
+ fseek(stream->file, read_pos, SEEK_SET);\r
+ rret = fread(tmp_dst, 1, 2048, stream->file);\r
+ cso->fpos_in = read_pos + rret;\r
+ if (rret != 2048) break;\r
+ } else {\r
+ read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;\r
+ if (block != cso->block_in_buff)\r
+ {\r
+ if (read_pos != cso->fpos_in)\r
+ fseek(stream->file, read_pos, SEEK_SET);\r
+ rret = fread(cso->in_buff, 1, read_len, stream->file);\r
+ cso->fpos_in = read_pos + rret;\r
+ if (rret != read_len) {\r
+ elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);\r
+ break;\r
+ }\r
+ cso->block_in_buff = block;\r
+ }\r
+ rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);\r
+ if (rret != 0) {\r
+ elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);\r
+ break;\r
+ }\r
+ }\r
+\r
+ rret = 2048;\r
+ if (out_offs != 0 || bytes < 2048) {\r
+ //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);\r
+ if (bytes < rret) rret = bytes;\r
+ if (2048 - out_offs < rret) rret = 2048 - out_offs;\r
+ memcpy(out, tmp_dst + out_offs, rret);\r
+ }\r
+ ret += rret;\r
+ out += rret;\r
+ cso->fpos_out += rret;\r
+ bytes -= rret;\r
+ block++;\r
+ index = index_end;\r
+ index_end = cso->index[block+1];\r
+ }\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 if (stream->type == PMT_CSO)\r
+ {\r
+ cso_struct *cso = stream->param;\r
+ switch (whence)\r
+ {\r
+ case SEEK_CUR: cso->fpos_out += offset; break;\r
+ case SEEK_SET: cso->fpos_out = offset; break;\r
+ case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;\r
+ }\r
+ return cso->fpos_out;\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 if (fp->type == PMT_CSO)\r
+ {\r
+ free(fp->param);\r
+ fclose(fp->file);\r
+ }\r
+ else\r
+ ret = EOF;\r
+\r
+ free(fp);\r
+ return ret;\r
+}\r
+\r