2008-08-06 03:55:59 +02:00
|
|
|
/****************************************************************************
|
2008-08-07 07:19:17 +02:00
|
|
|
* Snes9x 1.50
|
2008-08-06 03:55:59 +02:00
|
|
|
*
|
|
|
|
* Nintendo Gamecube Unzip - borrowed from the GPP
|
|
|
|
*
|
|
|
|
* softdev July 2006
|
|
|
|
****************************************************************************/
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include "dvd.h"
|
|
|
|
#include "video.h"
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "menudraw.h"
|
2008-08-06 03:55:59 +02:00
|
|
|
#include "unzip.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PKWare Zip Header - adopted into zip standard
|
|
|
|
*/
|
|
|
|
#define PKZIPID 0x504b0304
|
|
|
|
#define MAXROM 0x500000
|
|
|
|
#define ZIPCHUNK 2048
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Zip files are stored little endian
|
|
|
|
* Support functions for short and int types
|
|
|
|
*/
|
|
|
|
u32
|
|
|
|
FLIP32 (u32 b)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
unsigned int c;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
c = (b & 0xff000000) >> 24;
|
|
|
|
c |= (b & 0xff0000) >> 8;
|
|
|
|
c |= (b & 0xff00) << 8;
|
|
|
|
c |= (b & 0xff) << 24;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return c;
|
2008-08-06 03:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u16
|
|
|
|
FLIP16 (u16 b)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
u16 c;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
c = (b & 0xff00) >> 8;
|
|
|
|
c |= (b & 0xff) << 8;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return c;
|
2008-08-06 03:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* IsZipFile
|
|
|
|
*
|
|
|
|
* Returns TRUE when PKZIPID is first four characters of buffer
|
|
|
|
****************************************************************************/
|
|
|
|
int
|
|
|
|
IsZipFile (char *buffer)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
unsigned int *check;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
check = (unsigned int *) buffer;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (check[0] == PKZIPID)
|
|
|
|
return 1;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return 0;
|
2008-08-06 03:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* unzip
|
|
|
|
*
|
|
|
|
* It should be noted that there is a limit of 5MB total size for any ROM
|
|
|
|
******************************************************************************/
|
2008-08-12 05:25:16 +02:00
|
|
|
|
2008-08-06 03:55:59 +02:00
|
|
|
int
|
2008-08-12 05:25:16 +02:00
|
|
|
UnZipBuffer (unsigned char *outbuffer, u64 inoffset, short where, FILE* filehandle)
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
PKZIPHEADER pkzip;
|
|
|
|
int zipoffset = 0;
|
|
|
|
int zipchunk = 0;
|
|
|
|
char out[ZIPCHUNK];
|
|
|
|
z_stream zs;
|
|
|
|
int res;
|
|
|
|
int bufferoffset = 0;
|
|
|
|
int have = 0;
|
2008-08-14 00:44:59 +02:00
|
|
|
char readbuffer[ZIPCHUNK];
|
2008-08-12 05:25:16 +02:00
|
|
|
char msg[128];
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Read Zip Header ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
switch (where)
|
|
|
|
{
|
|
|
|
case 0: // SD Card
|
2008-08-06 03:55:59 +02:00
|
|
|
fseek(filehandle, 0, SEEK_SET);
|
2008-08-14 00:44:59 +02:00
|
|
|
fread (readbuffer, 1, ZIPCHUNK, filehandle);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // DVD
|
2008-08-14 00:44:59 +02:00
|
|
|
dvd_read (readbuffer, ZIPCHUNK, inoffset);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // From buffer
|
2008-08-14 00:44:59 +02:00
|
|
|
memcpy(readbuffer, outbuffer, ZIPCHUNK);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Copy PKZip header to local, used as info ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
memcpy (&pkzip, readbuffer, sizeof (PKZIPHEADER));
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);
|
2008-08-07 07:19:17 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
sprintf (msg, "Unzipping %d bytes ... Wait",
|
|
|
|
pkzip.uncompressedSize);
|
|
|
|
ShowAction (msg);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Prepare the zip stream ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
memset (&zs, 0, sizeof (z_stream));
|
|
|
|
zs.zalloc = Z_NULL;
|
|
|
|
zs.zfree = Z_NULL;
|
|
|
|
zs.opaque = Z_NULL;
|
|
|
|
zs.avail_in = 0;
|
|
|
|
zs.next_in = Z_NULL;
|
|
|
|
res = inflateInit2 (&zs, -MAX_WBITS);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (res != Z_OK)
|
|
|
|
return 0;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Set ZipChunk for first pass ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
zipoffset =
|
|
|
|
(sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) +
|
|
|
|
FLIP16 (pkzip.extraDataLength));
|
|
|
|
zipchunk = ZIPCHUNK - zipoffset;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Now do it! ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
do
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
zs.avail_in = zipchunk;
|
|
|
|
zs.next_in = (Bytef *) & readbuffer[zipoffset];
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
/*** Now inflate until input buffer is exhausted ***/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
zs.avail_out = ZIPCHUNK;
|
|
|
|
zs.next_out = (Bytef *) & out;
|
|
|
|
|
|
|
|
res = inflate (&zs, Z_NO_FLUSH);
|
|
|
|
|
|
|
|
if (res == Z_MEM_ERROR)
|
|
|
|
{
|
2008-08-14 00:44:59 +02:00
|
|
|
inflateEnd (&zs);
|
|
|
|
return 0;
|
2008-08-12 05:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
have = ZIPCHUNK - zs.avail_out;
|
|
|
|
if (have)
|
|
|
|
{
|
2008-08-14 00:44:59 +02:00
|
|
|
/*** Copy to normal block buffer ***/
|
|
|
|
memcpy (&outbuffer[bufferoffset], &out, have);
|
|
|
|
bufferoffset += have;
|
2008-08-12 05:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (zs.avail_out == 0);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
|
|
|
/*** Readup the next 2k block ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
zipoffset = 0;
|
|
|
|
zipchunk = ZIPCHUNK;
|
|
|
|
|
|
|
|
switch (where)
|
|
|
|
{
|
|
|
|
case 0: // SD Card
|
2008-08-14 00:44:59 +02:00
|
|
|
fread (readbuffer, 1, ZIPCHUNK, filehandle);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // DVD
|
2008-08-14 00:44:59 +02:00
|
|
|
inoffset += ZIPCHUNK;
|
|
|
|
dvd_read (readbuffer, ZIPCHUNK, inoffset);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // From buffer
|
2008-08-14 00:44:59 +02:00
|
|
|
inoffset += ZIPCHUNK;
|
|
|
|
memcpy(readbuffer, outbuffer+inoffset, ZIPCHUNK);
|
2008-08-12 05:25:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (res != Z_STREAM_END);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
inflateEnd (&zs);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (res == Z_STREAM_END)
|
|
|
|
{
|
|
|
|
if (pkzip.uncompressedSize == (u32) bufferoffset)
|
|
|
|
return bufferoffset;
|
|
|
|
else
|
|
|
|
return pkzip.uncompressedSize;
|
|
|
|
}
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return 0;
|
2008-08-06 03:55:59 +02:00
|
|
|
}
|