2018-11-15 23:31:39 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
|
|
|
|
This file is licensed under the Snes9x License.
|
|
|
|
For further information, consult the LICENSE file in the root directory.
|
|
|
|
\*****************************************************************************/
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
#ifdef UNZIP_SUPPORT
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2016-11-11 16:52:07 +00:00
|
|
|
#ifdef SYSTEM_ZIP
|
|
|
|
#include <minizip/unzip.h>
|
|
|
|
#else
|
2010-09-25 15:46:12 +00:00
|
|
|
#include "unzip/unzip.h"
|
2016-11-11 16:52:07 +00:00
|
|
|
#endif
|
2010-09-25 15:46:12 +00:00
|
|
|
#include "snes9x.h"
|
|
|
|
#include "memmap.h"
|
|
|
|
|
|
|
|
|
2012-01-02 00:48:51 +00:00
|
|
|
bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer)
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
*TotalFileSize = 0;
|
|
|
|
|
|
|
|
unzFile file = unzOpen(zipname);
|
|
|
|
if (file == NULL)
|
|
|
|
return (FALSE);
|
|
|
|
|
2017-08-17 18:30:44 +00:00
|
|
|
// find largest file in zip file (under MAX_ROM_SIZE) or a file with extension .1, or a file named program.rom
|
2010-09-25 15:46:12 +00:00
|
|
|
char filename[132];
|
2012-01-02 00:48:51 +00:00
|
|
|
uint32 filesize = 0;
|
2010-09-25 15:46:12 +00:00
|
|
|
int port = unzGoToFirstFile(file);
|
|
|
|
|
|
|
|
unz_file_info info;
|
|
|
|
|
|
|
|
while (port == UNZ_OK)
|
|
|
|
{
|
|
|
|
char name[132];
|
|
|
|
unzGetCurrentFileInfo(file, &info, name, 128, NULL, 0, NULL, 0);
|
|
|
|
|
|
|
|
if (info.uncompressed_size > CMemory::MAX_ROM_SIZE + 512)
|
|
|
|
{
|
|
|
|
port = unzGoToNextFile(file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-02 00:48:51 +00:00
|
|
|
if (info.uncompressed_size > filesize)
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
strcpy(filename, name);
|
|
|
|
filesize = info.uncompressed_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = strlen(name);
|
|
|
|
if (len > 2 && name[len - 2] == '.' && name[len - 1] == '1')
|
|
|
|
{
|
|
|
|
strcpy(filename, name);
|
|
|
|
filesize = info.uncompressed_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:30:44 +00:00
|
|
|
if (strncasecmp(name, "program.rom", 11) == 0)
|
|
|
|
{
|
|
|
|
strcpy(filename, name);
|
|
|
|
filesize = info.uncompressed_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:12 +00:00
|
|
|
port = unzGoToNextFile(file);
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:30:44 +00:00
|
|
|
int len = strlen(zipname);
|
|
|
|
if (!(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0 ||
|
|
|
|
(len > 5 && strcasecmp(zipname + len - 5, ".msu1") == 0 && strcasecmp(filename, "program.rom") != 0))
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
2018-11-05 21:23:21 +00:00
|
|
|
if (unzClose(file) != UNZ_OK)
|
|
|
|
assert(FALSE);
|
2010-09-25 15:46:12 +00:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find extension
|
|
|
|
char tmp[2] = { 0, 0 };
|
|
|
|
char *ext = strrchr(filename, '.');
|
|
|
|
if (ext)
|
|
|
|
ext++;
|
|
|
|
else
|
|
|
|
ext = tmp;
|
|
|
|
|
|
|
|
uint8 *ptr = buffer;
|
|
|
|
bool8 more = FALSE;
|
|
|
|
|
2017-09-09 11:17:55 +00:00
|
|
|
unzLocateFile(file, filename, 0);
|
2010-09-25 15:46:12 +00:00
|
|
|
unzGetCurrentFileInfo(file, &info, filename, 128, NULL, 0, NULL, 0);
|
|
|
|
|
|
|
|
if (unzOpenCurrentFile(file) != UNZ_OK)
|
|
|
|
{
|
|
|
|
unzClose(file);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
assert(info.uncompressed_size <= CMemory::MAX_ROM_SIZE + 512);
|
|
|
|
|
2012-01-02 00:48:51 +00:00
|
|
|
uint32 FileSize = info.uncompressed_size;
|
2010-09-25 15:46:12 +00:00
|
|
|
int l = unzReadCurrentFile(file, ptr, FileSize);
|
|
|
|
|
|
|
|
if (unzCloseCurrentFile(file) == UNZ_CRCERROR)
|
|
|
|
{
|
|
|
|
unzClose(file);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
2016-11-11 20:59:18 +00:00
|
|
|
if (l <= 0 || l != (int) FileSize)
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
unzClose(file);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
2012-01-02 00:48:51 +00:00
|
|
|
FileSize = Memory.HeaderRemove(FileSize, ptr);
|
2010-09-25 15:46:12 +00:00
|
|
|
ptr += FileSize;
|
|
|
|
*TotalFileSize += FileSize;
|
|
|
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 512 && (isdigit(ext[0]) && ext[1] == 0 && ext[0] < '9'))
|
|
|
|
{
|
|
|
|
more = TRUE;
|
|
|
|
ext[0]++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 512)
|
|
|
|
{
|
|
|
|
if (ext == tmp)
|
|
|
|
len = strlen(filename);
|
|
|
|
else
|
|
|
|
len = ext - filename - 1;
|
|
|
|
|
|
|
|
if ((len == 7 || len == 8) && strncasecmp(filename, "sf", 2) == 0 &&
|
|
|
|
isdigit(filename[2]) && isdigit(filename[3]) && isdigit(filename[4]) &&
|
|
|
|
isdigit(filename[5]) && isalpha(filename[len - 1]))
|
|
|
|
{
|
|
|
|
more = TRUE;
|
|
|
|
filename[len - 1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
more = FALSE;
|
|
|
|
|
|
|
|
if (more)
|
|
|
|
{
|
2017-09-09 11:17:55 +00:00
|
|
|
if (unzLocateFile(file, filename, 0) != UNZ_OK ||
|
2010-09-25 15:46:12 +00:00
|
|
|
unzGetCurrentFileInfo(file, &info, filename, 128, NULL, 0, NULL, 0) != UNZ_OK ||
|
|
|
|
unzOpenCurrentFile(file) != UNZ_OK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (more);
|
|
|
|
|
|
|
|
unzClose(file);
|
|
|
|
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|