]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/file.cpp
Various improvements to the GUI, including Power and Pause buttons, Load
[virtualjaguar] / src / file.cpp
index 4fc852c8c430f309db0c2d64e716a63e830d1973..81b5e084e097e79ea145ee9cd1ba4816e3272b20 100644 (file)
@@ -3,6 +3,14 @@
 //
 // File support
 // by James L. Hammons
+// (C) 2010 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -------------------------------------------------------------
+// JLH  01/16/2010  Created this log ;-)
+// JLH  02/28/2010  Added functions to look inside .ZIP files and handle contents
 //
 
 #include "file.h"
 #include "eeprom.h"
 #include "jaguar.h"
 #include "log.h"
+#include "memory.h"
 #include "unzip.h"
 #include "zlib.h"
 
 // Private function prototypes
 
 static int gzfilelength(gzFile gd);
+static bool CheckExtension(const char * filename, const char * ext);
 
 //
 // Generic ROM loading
@@ -27,66 +37,61 @@ uint32 JaguarLoadROM(uint8 * rom, char * path)
 {
 // We really should have some kind of sanity checking for the ROM size here to prevent
 // a buffer overflow... !!! FIX !!!
-#warning !!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!
+#warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
        uint32 romSize = 0;
 
-WriteLog("JaguarLoadROM: Attempting to load file '%s'...", path);
+       WriteLog("JaguarLoadROM: Attempting to load file '%s'...", path);
        char * ext = strrchr(path, '.');
-if (ext == NULL)
-       WriteLog("FAILED!\n");
-else
-       WriteLog("Succeeded in finding extension (%s)!\n", ext);
 
-       if (ext != NULL)
+       // No filename extension == YUO FAIL IT (it is loading the file).
+       // This is naive, but it works. But should probably come up with something a little
+       // more robust, to prevent problems with dopes trying to exploit this.
+       if (ext == NULL)
        {
-               WriteLog("VJ: Loading \"%s\"...", path);
-
-               if (strcasecmp(ext, ".zip") == 0)
-               {
-                       // Handle ZIP file loading here...
-                       WriteLog("(ZIPped)...");
+               WriteLog("FAILED!\n");
+               return 0;
+       }
 
-                       if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
-                       {
-                               WriteLog("Failed!\n");
-                               return 0;
-                       }
-               }
-               else
-               {
-/*                     FILE * fp = fopen(path, "rb");
+       WriteLog("Succeeded in finding extension (%s)!\n", ext);
+       WriteLog("VJ: Loading \"%s\"...", path);
 
-                       if (fp == NULL)
-                       {
-                               WriteLog("Failed!\n");
-                               return 0;
-                       }
+       if (strcasecmp(ext, ".zip") == 0)
+       {
+               // Handle ZIP file loading here...
+               WriteLog("(ZIPped)...");
 
-                       fseek(fp, 0, SEEK_END);
-                       romSize = ftell(fp);
-                       fseek(fp, 0, SEEK_SET);
-                       fread(rom, 1, romSize, fp);
-                       fclose(fp);*/
+               uint8_t * buffer = NULL;
+               romSize = GetFileFromZIP(path, FT_SOFTWARE, buffer);
 
-                       // Handle gzipped files transparently [Adam Green]...
+               if (romSize == 0)
+               {
+                       WriteLog("Failed!\n");
+                       return 0;
+               }
 
-                       gzFile fp = gzopen(path, "rb");
+               memcpy(rom, buffer, romSize);
+               delete[] buffer;
+       }
+       else
+       {
+               // Handle gzipped files transparently [Adam Green]...
 
-                       if (fp == NULL)
-                       {
-                               WriteLog("Failed!\n");
-                               return 0;
-                       }
+               gzFile fp = gzopen(path, "rb");
 
-                       romSize = gzfilelength(fp);
-                       gzseek(fp, 0, SEEK_SET);
-                       gzread(fp, rom, romSize);
-                       gzclose(fp);
+               if (fp == NULL)
+               {
+                       WriteLog("Failed!\n");
+                       return 0;
                }
 
-               WriteLog("OK (%i bytes)\n", romSize);
+               romSize = gzfilelength(fp);
+               gzseek(fp, 0, SEEK_SET);
+               gzread(fp, rom, romSize);
+               gzclose(fp);
        }
 
+       WriteLog("OK (%i bytes)\n", romSize);
+
        return romSize;
 }
 
@@ -126,6 +131,7 @@ bool JaguarLoadFile(char * path)
        char * ext = strrchr(path, '.');                                // Get the file's extension for non-cartridge checking
 
 //NOTE: Should fix JaguarLoadROM() to replace .zip with what's *in* the zip (.abs, .j64, etc.)
+#warning "!!! Need more robust file type checking in JaguarLoadROM() !!!"
        if (strcasecmp(ext, ".rom") == 0)
        {
                // File extension ".ROM": Alpine image that loads/runs at $802000
@@ -279,7 +285,7 @@ Start of Data Segment = 0x00803dd0
                }
                else
                {
-                       WriteLog("GUI: Couldn't find correct ABS format: %02X %02X\n", jaguarMainROM[0], jaguarMainROM[1]);
+                       WriteLog("GUI: Couldn't find correct ABS/COF format: %02X %02X\n", jaguarMainROM[0], jaguarMainROM[1]);
                        return false;
                }
        }
@@ -337,3 +343,92 @@ static int gzfilelength(gzFile 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;
+       }
+
+       while (readzip(zip))
+       {
+               zipent * ze = &zip->ent;
+               bool found = false;
+
+               // 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")))
+               {
+                       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);
+               }
+
+               if (found)
+               {
+                       WriteLog("FILE: Uncompressing...");
+// Insert file size sanity check here...
+                       buffer = new uint8[ze->uncompressed_size];
+
+                       if (readuncompresszip(zip, ze, (char *)buffer) == 0)
+                       {
+                               WriteLog("success! (%u bytes)\n", ze->uncompressed_size);
+                               closezip(zip);
+                               return ze->uncompressed_size;
+                       }
+                       else
+                       {
+                               WriteLog("FAILED!\n");
+                               delete[] buffer;
+                               buffer = NULL;
+                               closezip(zip);
+                               return 0;
+                       }
+               }
+       }
+
+       closezip(zip);
+
+       WriteLog("FILE: Failed to find file of type %s...\n", ftStrings[type]);
+       // Didn't find what we're looking for...
+       return 0;
+}
+
+//ParseFileType, etc.
+