Genesis-Plus-GX/source/unused/dos/fileio.c

152 lines
3.3 KiB
C
Raw Normal View History

2008-08-07 14:43:17 +02:00
#include "shared.h"
/*
Load a normal file, or ZIP/GZ archive.
Returns NULL if an error occured.
*/
uint8 *load_archive(char *filename, int *file_size)
{
int size = 0;
uint8 *buf = NULL;
if(check_zip(filename))
{
unzFile *fd = NULL;
unz_file_info info;
int ret = 0;
/* Attempt to open the archive */
fd = unzOpen(filename);
if(!fd) return (NULL);
/* Go to first file in archive */
ret = unzGoToFirstFile(fd);
if(ret != UNZ_OK)
{
unzClose(fd);
return (NULL);
}
[Core/VDP] --------------- * added support for Master System compatibility mode (Z80 ports access mode), incl. Mode 5 rendering. * added Mode 4 rendering for both Genesis & Master System modes. * added alternate BG planes rendering functions (should be faster on PPC architectures). [Core/IO] --------------- * added support for Master System compatibility mode (Z80 ports access mode). * added Master System peripherals emulation (Control Pad, Paddle, Sports Pad & Light Phaser). * added XE-1AP (analog controller) emulation. * added Activator emulation. [Core/Extra] --------------- * added support for all known Master System cartridge mappers. * added copy-protection hardware emulation for a few MD unlicensed games: fixes 777 Casino (crash when talking to bunny girls). (NB: most of those unlicensed games seem to have been already patched by ROM dumpers, main purpose is documenting them) * added support for Top Shooter arcade board controller. (A=Shoot, B=Bet, C/RIGHT=Coins, START=Start, hold UP on startup to enter service mode) * improved King of Fighters 98 mapper emulation (registers address decoding is now 100% accurate) * fixed Game Genie when several codes affect same ROM address. * fixed EEPROM types for Brian Lara Cricket & NBA Jam TE (verified on real cartridges) [Core/General] --------------- * added Master System compatibility mode emulation (automatically enabled when loading ROM file with .sms extension). * improved savestate stability & compatibility (support for old 1.4.x savestates is preserved) * various code cleanup & comments. [Gamecube/Wii] --------------- * fixed cheat codes handling when several codes affect same ROM address. * improved input controller detection on menu exit. * improved key remapping dialog box to match emulated device * changed Menu key for Gamecube controller to allow MODE button mapping * fixed DVD not being unmounted on swap (memory leak) [Wii only] --------------- * added USB mouse support for Sega Mouse emulation * compiled with latest libogc: improves USB compatibility & fixes stability issues with Wiimotes.
2011-04-01 00:11:05 +02:00
ret = unzGetCurrentFileInfo(fd, &info, filename, 128, NULL, 0, NULL, 0);
2008-08-07 14:43:17 +02:00
if(ret != UNZ_OK)
{
unzClose(fd);
return (NULL);
}
/* Open the file for reading */
ret = unzOpenCurrentFile(fd);
if(ret != UNZ_OK)
{
unzClose(fd);
return (NULL);
}
/* Allocate file data buffer */
size = info.uncompressed_size;
buf = malloc(size);
if(!buf)
{
unzClose(fd);
return (NULL);
}
/* Read (decompress) the file */
ret = unzReadCurrentFile(fd, buf, info.uncompressed_size);
if(ret != info.uncompressed_size)
{
free(buf);
unzCloseCurrentFile(fd);
unzClose(fd);
return (NULL);
}
/* Close the current file */
ret = unzCloseCurrentFile(fd);
if(ret != UNZ_OK)
{
free(buf);
unzClose(fd);
return (NULL);
}
/* Close the archive */
ret = unzClose(fd);
if(ret != UNZ_OK)
{
free(buf);
return (NULL);
}
/* Update file size and return pointer to file data */
*file_size = size;
return (buf);
}
else
{
gzFile *gd = NULL;
/* Open file */
gd = gzopen(filename, "rb");
if(!gd) return (0);
/* Get file size */
size = gzsize(gd);
/* Allocate file data buffer */
buf = malloc(size);
if(!buf)
{
gzclose(gd);
return (0);
}
/* Read file data */
gzread(gd, buf, size);
/* Close file */
gzclose(gd);
/* Update file size and return pointer to file data */
*file_size = size;
return (buf);
}
}
/*
Verifies if a file is a ZIP archive or not.
Returns: 1= ZIP archive, 0= not a ZIP archive
*/
int check_zip(char *filename)
{
uint8 buf[2];
FILE *fd = NULL;
fd = fopen(filename, "rb");
if(!fd) return (0);
fread(buf, 2, 1, fd);
fclose(fd);
if(memcmp(buf, "PK", 2) == 0) return (1);
return (0);
}
/*
Returns the size of a GZ compressed file.
*/
int gzsize(gzFile *gd)
{
#define CHUNKSIZE (0x10000)
int size = 0, length = 0;
unsigned char buffer[CHUNKSIZE];
gzrewind(gd);
do {
size = gzread(gd, buffer, CHUNKSIZE);
if(size <= 0) break;
length += size;
} while (!gzeof(gd));
gzrewind(gd);
return (length);
#undef CHUNKSIZE
}