+ // We can assume we have JST_NONE at this point. :-P
+ // TODO: Add a dialog box that tells the user that they're trying to feed VJ a bogus file.
+ return false;
+}
+
+//
+// Get the length of a (possibly) gzipped file
+//
+static int gzfilelength(gzFile gd)
+{
+ int size = 0, length = 0;
+ unsigned char buffer[0x10000];
+
+ gzrewind(gd);
+
+ do
+ {
+ // Read in chunks until EOF
+ size = gzread(gd, buffer, 0x10000);
+
+ if (size <= 0)
+ break;
+
+ length += size;
+ }
+ while (!gzeof(gd));
+
+ gzrewind(gd);
+ return length;
+}
+
+//
+// Compare extension to passed in filename. If equal, return true; otherwise false.
+//
+static bool CheckExtension(const char * filename, const char * ext)
+{
+ const char * filenameExt = strrchr(filename, '.'); // Get the file's extension (if any)
+ return (strcasecmp(filenameExt, ext) == 0 ? true : false);
+}
+
+//
+// Get file from .ZIP
+// Returns the size of the file inside the .ZIP file that we're looking at
+// NOTE: If the thing we're looking for is found, it allocates it in the passed in buffer.
+// Which means we have to deallocate it later.
+//
+uint32 GetFileFromZIP(const char * zipFile, FileType type, uint8 * &buffer)
+{
+// NOTE: We could easily check for this by discarding anything that's larger than the RAM/ROM
+// size of the Jaguar console.
+#warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
+ const char ftStrings[5][32] = { "Software", "EEPROM", "Label", "Box Art", "Controller Overlay" };
+ ZIP * zip = openzip(0, 0, zipFile);
+
+ if (zip == NULL)
+ {
+ WriteLog("FILE: Could not open file '%s'!\n", zipFile);
+ return 0;
+ }
+
+ zipent * ze;
+ bool found = false;
+
+ // The order is here is important: If the file is found, we need to short-circuit the
+ // readzip() call because otherwise, 'ze' will be pointing to the wrong file!
+ while (!found && readzip(zip))
+ {
+ ze = &zip->ent;
+
+ // Here we simply rely on the file extension to tell the truth, but we know
+ // that extensions lie like sons-a-bitches. So this is naive, we need to do
+ // something a little more robust to keep bad things from happening here.
+#warning "!!! Checking for image by extension can be fooled !!!"
+ if ((type == FT_LABEL) && (CheckExtension(ze->name, ".png") || CheckExtension(ze->name, ".jpg") || CheckExtension(ze->name, ".gif")))
+ {
+ found = true;
+ WriteLog("FILE: Found image file '%s'.\n", ze->name);
+ }
+
+ if ((type == FT_SOFTWARE) && (CheckExtension(ze->name, ".j64")
+ || CheckExtension(ze->name, ".rom") || CheckExtension(ze->name, ".abs")
+ || CheckExtension(ze->name, ".cof") || CheckExtension(ze->name, ".jag")))
+ {
+ found = true;
+ WriteLog("FILE: Found software file '%s'.\n", ze->name);
+ }
+
+ if ((type == FT_EEPROM) && (CheckExtension(ze->name, ".eep") || CheckExtension(ze->name, ".eeprom")))
+ {
+ found = true;
+ WriteLog("FILE: Found EEPROM file '%s'.\n", ze->name);
+ }
+ }
+
+ uint32 fileSize = 0;
+
+ if (found)
+ {
+ WriteLog("FILE: Uncompressing...");
+// Insert file size sanity check here...
+ buffer = new uint8[ze->uncompressed_size];
+
+ if (readuncompresszip(zip, ze, (char *)buffer) == 0)
+ {
+ fileSize = ze->uncompressed_size;
+ WriteLog("success! (%u bytes)\n", fileSize);
+ }
+ else
+ {
+ delete[] buffer;
+ buffer = NULL;
+ WriteLog("FAILED!\n");
+ }
+ }
+ else
+ // Didn't find what we're looking for...
+ WriteLog("FILE: Failed to find file of type %s...\n", ftStrings[type]);
+
+ closezip(zip);
+ return fileSize;
+}
+
+//
+// Parse the file type based upon file size and/or headers.
+//
+uint32 ParseFileType(uint8 header1, uint8 header2, uint32 size)
+{
+ // Check headers first...
+
+ // ABS/COFF type 1
+ if (header1 == 0x60 && header2 == 0x1B)
+ return JST_ABS_TYPE1;
+
+ // ABS/COFF type 2
+ if (header1 == 0x01 && header2 == 0x50)
+ return JST_ABS_TYPE2;
+
+ // Jag Server
+ if (header1 == 0x60 && header2 == 0x1A)
+ return JST_JAGSERVER;
+
+ // And if that fails, try file sizes...
+
+ // If the file size is divisible by 1M, we probably have an regular ROM.
+ // We can also check our CRC32 against the internal ROM database to be sure.
+ if ((size % 1048576) == 0)
+ return JST_ROM;
+
+ // If the file size + 8192 bytes is divisible by 1M, we probably have an
+ // Alpine format ROM.
+ if (((size + 8192) % 1048576) == 0)
+ return JST_ALPINE;
+
+ // Headerless crap
+ return JST_NONE;
+}
+
+//
+// Check for universal header
+//
+bool HasUniversalHeader(uint8 * rom, uint32 romSize)
+{
+ // Sanity check
+ if (romSize < 8192)
+ return false;
+
+ for(int i=0; i<8192; i++)
+ if (rom[i] != universalCartHeader[i])
+ return false;
+
+ return true;
+}
+
+#if 0
+// Misc. doco