mirror of
https://github.com/dborth/vbagx.git
synced 2024-11-26 04:24:16 +01:00
changes for save states. although there's still save corruption :(
This commit is contained in:
parent
a7b1ba9ae3
commit
6bb71834d3
@ -394,42 +394,7 @@ bool SaveBatteryOrState(char * filepath, int action, bool silent)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool written = emulator.emuWriteMemState((char *)savebuffer, SAVEBUFFERSIZE);
|
datasize = emulator.emuWriteMemState((char *)savebuffer, SAVEBUFFERSIZE);
|
||||||
// we need to set datasize to the exact memory size written
|
|
||||||
// but emuWriteMemState doesn't return that for us
|
|
||||||
// so instead we'll find the end of the save the old fashioned way
|
|
||||||
if(written)
|
|
||||||
{
|
|
||||||
datasize = (1024*192); // we'll start at 192K - no save should be larger
|
|
||||||
char check = savebuffer[datasize];
|
|
||||||
while(check == 0)
|
|
||||||
{
|
|
||||||
datasize -= 16384;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
}
|
|
||||||
datasize += 16384;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
while(check == 0)
|
|
||||||
{
|
|
||||||
datasize -= 1024;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
}
|
|
||||||
datasize += 1024;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
while(check == 0)
|
|
||||||
{
|
|
||||||
datasize -= 64;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
}
|
|
||||||
datasize += 64;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
while(check == 0)
|
|
||||||
{
|
|
||||||
datasize -= 1;
|
|
||||||
check = savebuffer[datasize];
|
|
||||||
}
|
|
||||||
datasize += 2; // include last byte AND a null byte
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write savebuffer into file
|
// write savebuffer into file
|
||||||
|
@ -25,7 +25,7 @@ struct EmulatedSystem {
|
|||||||
// load memory state (rewind)
|
// load memory state (rewind)
|
||||||
bool (*emuReadMemState)(char *, int);
|
bool (*emuReadMemState)(char *, int);
|
||||||
// write memory state (rewind)
|
// write memory state (rewind)
|
||||||
bool (*emuWriteMemState)(char *, int);
|
int (*emuWriteMemState)(char *, int);
|
||||||
// write PNG file
|
// write PNG file
|
||||||
bool (*emuWritePNG)(const char *);
|
bool (*emuWritePNG)(const char *);
|
||||||
// write BMP file
|
// write BMP file
|
||||||
|
@ -163,7 +163,7 @@ void utilWriteData(gzFile gzFile, variable_desc *data)
|
|||||||
|
|
||||||
gzFile utilGzOpen(const char *file, const char *mode)
|
gzFile utilGzOpen(const char *file, const char *mode)
|
||||||
{
|
{
|
||||||
utilGzWriteFunc = (int (*)(void *,void * const, unsigned int))gzwrite;
|
utilGzWriteFunc = (int (ZEXPORT *)(void *,void * const, unsigned int))gzwrite;
|
||||||
utilGzReadFunc = gzread;
|
utilGzReadFunc = gzread;
|
||||||
utilGzCloseFunc = gzclose;
|
utilGzCloseFunc = gzclose;
|
||||||
utilGzSeekFunc = gzseek;
|
utilGzSeekFunc = gzseek;
|
||||||
|
@ -9,15 +9,33 @@
|
|||||||
* Adapted from original gzio.c from zlib library by Forgotten
|
* Adapted from original gzio.c from zlib library by Forgotten
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @(#) $Id: memgzio.c,v 1.3 2004/01/17 23:07:32 kxu Exp $ */
|
/* @(#) $Id: memgzio.c,v 1.5 2006/06/06 21:04:20 spacy51 Exp $ */
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "memgzio.h"
|
#include "memgzio.h"
|
||||||
|
|
||||||
|
#ifndef local
|
||||||
|
#define local static
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef DEF_MEM_LEVEL
|
||||||
|
# define DEF_MEM_LEVEL 8
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef OS_CODE
|
||||||
|
#define OS_CODE 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef zmemcpy
|
||||||
|
#define zmemcpy memcpy
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*struct internal_state {int dummy;};*/ /* for buggy compilers */
|
/*struct internal_state {int dummy;};*/ /* for buggy compilers */
|
||||||
|
|
||||||
#ifndef Z_BUFSIZE
|
#ifndef Z_BUFSIZE
|
||||||
@ -44,18 +62,15 @@ static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
|
|||||||
#define COMMENT 0x10 /* bit 4 set: file comment present */
|
#define COMMENT 0x10 /* bit 4 set: file comment present */
|
||||||
#define RESERVED 0xE0 /* bits 5..7: reserved */
|
#define RESERVED 0xE0 /* bits 5..7: reserved */
|
||||||
|
|
||||||
typedef struct _MemFile
|
typedef struct _MemFile {
|
||||||
{
|
|
||||||
char *memory;
|
char *memory;
|
||||||
char *next;
|
char *next;
|
||||||
int available;
|
int available;
|
||||||
int error;
|
int error;
|
||||||
char mode;
|
char mode;
|
||||||
}
|
} MEMFILE;
|
||||||
MEMFILE;
|
|
||||||
|
|
||||||
typedef struct mem_stream
|
typedef struct mem_stream {
|
||||||
{
|
|
||||||
z_stream stream;
|
z_stream stream;
|
||||||
int z_err; /* error code for last stream operation */
|
int z_err; /* error code for last stream operation */
|
||||||
int z_eof; /* set if end of input file */
|
int z_eof; /* set if end of input file */
|
||||||
@ -67,8 +82,7 @@ typedef struct mem_stream
|
|||||||
int transparent; /* 1 if input file is not a .gz file */
|
int transparent; /* 1 if input file is not a .gz file */
|
||||||
char mode; /* 'w' or 'r' */
|
char mode; /* 'w' or 'r' */
|
||||||
long startpos; /* start of compressed data in file (header skipped) */
|
long startpos; /* start of compressed data in file (header skipped) */
|
||||||
}
|
} mem_stream;
|
||||||
mem_stream;
|
|
||||||
|
|
||||||
|
|
||||||
local gzFile gz_open OF((char *memory, const int available, const char *mode));
|
local gzFile gz_open OF((char *memory, const int available, const char *mode));
|
||||||
@ -95,8 +109,7 @@ local MEMFILE *memOpen(char *memory, int available, char mode)
|
|||||||
f->mode = mode;
|
f->mode = mode;
|
||||||
f->error = 0;
|
f->error = 0;
|
||||||
|
|
||||||
if(mode == 'w')
|
if(mode == 'w') {
|
||||||
{
|
|
||||||
f->available = available - 8;
|
f->available = available - 8;
|
||||||
f->next = memory + 8;
|
f->next = memory + 8;
|
||||||
memory[0] = 'V';
|
memory[0] = 'V';
|
||||||
@ -104,12 +117,9 @@ local MEMFILE *memOpen(char *memory, int available, char mode)
|
|||||||
memory[2] = 'A';
|
memory[2] = 'A';
|
||||||
memory[3] = ' ';
|
memory[3] = ' ';
|
||||||
*((int *)(memory+4)) = 0;
|
*((int *)(memory+4)) = 0;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
if(memory[0] != 'V' || memory[1] != 'B' || memory[2] != 'A' ||
|
if(memory[0] != 'V' || memory[1] != 'B' || memory[2] != 'A' ||
|
||||||
memory[3] != ' ')
|
memory[3] != ' ') {
|
||||||
{
|
|
||||||
free(f);
|
free(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -125,18 +135,16 @@ local size_t memWrite(const void *buffer, size_t size, size_t count,
|
|||||||
{
|
{
|
||||||
size_t total = size*count;
|
size_t total = size*count;
|
||||||
|
|
||||||
if(file->mode != 'w')
|
if(file->mode != 'w') {
|
||||||
{
|
|
||||||
file->error = 1;
|
file->error = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(total > (size_t)file->available)
|
if(total > (size_t)file->available) {
|
||||||
{
|
|
||||||
total = file->available;
|
total = file->available;
|
||||||
}
|
}
|
||||||
memcpy(file->next, buffer, total);
|
memcpy(file->next, buffer, total);
|
||||||
file->available -= total;
|
file->available -= (int)total;
|
||||||
file->next += total;
|
file->next += total;
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
@ -146,8 +154,7 @@ local size_t memRead(void *buffer, size_t size, size_t count,
|
|||||||
{
|
{
|
||||||
size_t total = size*count;
|
size_t total = size*count;
|
||||||
|
|
||||||
if(file->mode != 'r')
|
if(file->mode != 'r') {
|
||||||
{
|
|
||||||
file->error = 1;
|
file->error = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -155,30 +162,26 @@ local size_t memRead(void *buffer, size_t size, size_t count,
|
|||||||
if(file->available == 0)
|
if(file->available == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if(total > (size_t)file->available)
|
if(total > (size_t)file->available) {
|
||||||
{
|
|
||||||
total = file->available;
|
total = file->available;
|
||||||
}
|
}
|
||||||
memcpy(buffer, file->next, total);
|
memcpy(buffer, file->next, total);
|
||||||
file->available -= total;
|
file->available -= (int)total;
|
||||||
file->next += total;
|
file->next += total;
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
local int memPutc(int c, MEMFILE *file)
|
local int memPutc(int c, MEMFILE *file)
|
||||||
{
|
{
|
||||||
if(file->mode != 'w')
|
if(file->mode != 'w') {
|
||||||
{
|
|
||||||
file->error = 1;
|
file->error = 1;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file->available >= 1)
|
if(file->available >= 1) {
|
||||||
{
|
|
||||||
*file->next++ = c;
|
*file->next++ = c;
|
||||||
file->available--;
|
file->available--;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
@ -186,7 +189,7 @@ local int memPutc(int c, MEMFILE *file)
|
|||||||
|
|
||||||
local long memTell(MEMFILE *f)
|
local long memTell(MEMFILE *f)
|
||||||
{
|
{
|
||||||
return (f->next - f->memory) - 8;
|
return (long)(f->next - f->memory) - 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
local int memError(MEMFILE *f)
|
local int memError(MEMFILE *f)
|
||||||
@ -196,8 +199,7 @@ local int memError(MEMFILE *f)
|
|||||||
|
|
||||||
local int memClose(MEMFILE *f)
|
local int memClose(MEMFILE *f)
|
||||||
{
|
{
|
||||||
if(f->mode == 'w')
|
if(f->mode == 'w') {
|
||||||
{
|
|
||||||
*((int *)(f->memory+4)) = memTell(f);
|
*((int *)(f->memory+4)) = memTell(f);
|
||||||
}
|
}
|
||||||
free(f);
|
free(f);
|
||||||
@ -214,7 +216,7 @@ local int memPrintf(MEMFILE *f, const char *format, ...)
|
|||||||
len = vsprintf(buffer, format, list);
|
len = vsprintf(buffer, format, list);
|
||||||
va_end(list);
|
va_end(list);
|
||||||
|
|
||||||
return memWrite(buffer, 1, len, f);
|
return (int)memWrite(buffer, 1, len, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ===========================================================================
|
/* ===========================================================================
|
||||||
@ -256,32 +258,22 @@ const char *mode;
|
|||||||
s->file = NULL;
|
s->file = NULL;
|
||||||
|
|
||||||
s->mode = '\0';
|
s->mode = '\0';
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
if (*p == 'r') s->mode = 'r';
|
if (*p == 'r') s->mode = 'r';
|
||||||
if (*p == 'w' || *p == 'a') s->mode = 'w';
|
if (*p == 'w' || *p == 'a') s->mode = 'w';
|
||||||
if (*p >= '0' && *p <= '9')
|
if (*p >= '0' && *p <= '9') {
|
||||||
{
|
|
||||||
level = *p - '0';
|
level = *p - '0';
|
||||||
}
|
} else if (*p == 'f') {
|
||||||
else if (*p == 'f')
|
|
||||||
{
|
|
||||||
strategy = Z_FILTERED;
|
strategy = Z_FILTERED;
|
||||||
}
|
} else if (*p == 'h') {
|
||||||
else if (*p == 'h')
|
|
||||||
{
|
|
||||||
strategy = Z_HUFFMAN_ONLY;
|
strategy = Z_HUFFMAN_ONLY;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
*m++ = *p; /* copy the mode */
|
*m++ = *p; /* copy the mode */
|
||||||
}
|
}
|
||||||
}
|
} while (*p++ && m != fmode + sizeof(fmode));
|
||||||
while (*p++ && m != fmode + sizeof(fmode));
|
|
||||||
if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
|
if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
|
||||||
|
|
||||||
if (s->mode == 'w')
|
if (s->mode == 'w') {
|
||||||
{
|
|
||||||
#ifdef NO_DEFLATE
|
#ifdef NO_DEFLATE
|
||||||
err = Z_STREAM_ERROR;
|
err = Z_STREAM_ERROR;
|
||||||
#else
|
#else
|
||||||
@ -291,13 +283,10 @@ const char *mode;
|
|||||||
|
|
||||||
s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
|
s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
|
||||||
#endif
|
#endif
|
||||||
if (err != Z_OK || s->outbuf == Z_NULL)
|
if (err != Z_OK || s->outbuf == Z_NULL) {
|
||||||
{
|
|
||||||
return destroy(s), (gzFile)Z_NULL;
|
return destroy(s), (gzFile)Z_NULL;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
|
s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
|
||||||
|
|
||||||
err = inflateInit2(&(s->stream), -MAX_WBITS);
|
err = inflateInit2(&(s->stream), -MAX_WBITS);
|
||||||
@ -307,8 +296,7 @@ const char *mode;
|
|||||||
* return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
|
* return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
|
||||||
* present after the compressed stream.
|
* present after the compressed stream.
|
||||||
*/
|
*/
|
||||||
if (err != Z_OK || s->inbuf == Z_NULL)
|
if (err != Z_OK || s->inbuf == Z_NULL) {
|
||||||
{
|
|
||||||
return destroy(s), (gzFile)Z_NULL;
|
return destroy(s), (gzFile)Z_NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -317,13 +305,11 @@ const char *mode;
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
s->file = memOpen(memory, available, s->mode);
|
s->file = memOpen(memory, available, s->mode);
|
||||||
|
|
||||||
if (s->file == NULL)
|
if (s->file == NULL) {
|
||||||
{
|
|
||||||
return destroy(s), (gzFile)Z_NULL;
|
return destroy(s), (gzFile)Z_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s->mode == 'w')
|
if (s->mode == 'w') {
|
||||||
{
|
|
||||||
/* Write a very simple .gz header:
|
/* Write a very simple .gz header:
|
||||||
*/
|
*/
|
||||||
memPrintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
|
memPrintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
|
||||||
@ -334,9 +320,7 @@ const char *mode;
|
|||||||
* startpos anyway in write mode, so this initialization is not
|
* startpos anyway in write mode, so this initialization is not
|
||||||
* necessary.
|
* necessary.
|
||||||
*/
|
*/
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
check_header(s); /* skip the .gz header */
|
check_header(s); /* skip the .gz header */
|
||||||
s->startpos = (memTell(s->file) - s->stream.avail_in);
|
s->startpos = (memTell(s->file) - s->stream.avail_in);
|
||||||
}
|
}
|
||||||
@ -364,12 +348,10 @@ local int get_byte(s)
|
|||||||
mem_stream *s;
|
mem_stream *s;
|
||||||
{
|
{
|
||||||
if (s->z_eof) return EOF;
|
if (s->z_eof) return EOF;
|
||||||
if (s->stream.avail_in == 0)
|
if (s->stream.avail_in == 0) {
|
||||||
{
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
|
s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
|
||||||
if (s->stream.avail_in == 0)
|
if (s->stream.avail_in == 0) {
|
||||||
{
|
|
||||||
s->z_eof = 1;
|
s->z_eof = 1;
|
||||||
if (memError(s->file)) s->z_err = Z_ERRNO;
|
if (memError(s->file)) s->z_err = Z_ERRNO;
|
||||||
return EOF;
|
return EOF;
|
||||||
@ -398,14 +380,11 @@ mem_stream *s;
|
|||||||
int c;
|
int c;
|
||||||
|
|
||||||
/* Check the gzip magic header */
|
/* Check the gzip magic header */
|
||||||
for (len = 0; len < 2; len++)
|
for (len = 0; len < 2; len++) {
|
||||||
{
|
|
||||||
c = get_byte(s);
|
c = get_byte(s);
|
||||||
if (c != gz_magic[len])
|
if (c != gz_magic[len]) {
|
||||||
{
|
|
||||||
if (len != 0) s->stream.avail_in++, s->stream.next_in--;
|
if (len != 0) s->stream.avail_in++, s->stream.next_in--;
|
||||||
if (c != EOF)
|
if (c != EOF) {
|
||||||
{
|
|
||||||
s->stream.avail_in++, s->stream.next_in--;
|
s->stream.avail_in++, s->stream.next_in--;
|
||||||
s->transparent = 1;
|
s->transparent = 1;
|
||||||
}
|
}
|
||||||
@ -415,8 +394,7 @@ mem_stream *s;
|
|||||||
}
|
}
|
||||||
method = get_byte(s);
|
method = get_byte(s);
|
||||||
flags = get_byte(s);
|
flags = get_byte(s);
|
||||||
if (method != Z_DEFLATED || (flags & RESERVED) != 0)
|
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
|
||||||
{
|
|
||||||
s->z_err = Z_DATA_ERROR;
|
s->z_err = Z_DATA_ERROR;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -424,23 +402,19 @@ mem_stream *s;
|
|||||||
/* Discard time, xflags and OS code: */
|
/* Discard time, xflags and OS code: */
|
||||||
for (len = 0; len < 6; len++) (void)get_byte(s);
|
for (len = 0; len < 6; len++) (void)get_byte(s);
|
||||||
|
|
||||||
if ((flags & EXTRA_FIELD) != 0)
|
if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
|
||||||
{ /* skip the extra field */
|
|
||||||
len = (uInt)get_byte(s);
|
len = (uInt)get_byte(s);
|
||||||
len += ((uInt)get_byte(s))<<8;
|
len += ((uInt)get_byte(s))<<8;
|
||||||
/* len is garbage if EOF but the loop below will quit anyway */
|
/* len is garbage if EOF but the loop below will quit anyway */
|
||||||
while (len-- != 0 && get_byte(s) != EOF) ;
|
while (len-- != 0 && get_byte(s) != EOF) ;
|
||||||
}
|
}
|
||||||
if ((flags & ORIG_NAME) != 0)
|
if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
|
||||||
{ /* skip the original file name */
|
|
||||||
while ((c = get_byte(s)) != 0 && c != EOF) ;
|
while ((c = get_byte(s)) != 0 && c != EOF) ;
|
||||||
}
|
}
|
||||||
if ((flags & COMMENT) != 0)
|
if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
|
||||||
{ /* skip the .gz file comment */
|
|
||||||
while ((c = get_byte(s)) != 0 && c != EOF) ;
|
while ((c = get_byte(s)) != 0 && c != EOF) ;
|
||||||
}
|
}
|
||||||
if ((flags & HEAD_CRC) != 0)
|
if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
|
||||||
{ /* skip the header crc */
|
|
||||||
for (len = 0; len < 2; len++) (void)get_byte(s);
|
for (len = 0; len < 2; len++) (void)get_byte(s);
|
||||||
}
|
}
|
||||||
s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
|
s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
|
||||||
@ -459,23 +433,18 @@ mem_stream *s;
|
|||||||
|
|
||||||
TRYFREE(s->msg);
|
TRYFREE(s->msg);
|
||||||
|
|
||||||
if (s->stream.state != NULL)
|
if (s->stream.state != NULL) {
|
||||||
{
|
if (s->mode == 'w') {
|
||||||
if (s->mode == 'w')
|
|
||||||
{
|
|
||||||
#ifdef NO_DEFLATE
|
#ifdef NO_DEFLATE
|
||||||
err = Z_STREAM_ERROR;
|
err = Z_STREAM_ERROR;
|
||||||
#else
|
#else
|
||||||
err = deflateEnd(&(s->stream));
|
err = deflateEnd(&(s->stream));
|
||||||
#endif
|
#endif
|
||||||
}
|
} else if (s->mode == 'r') {
|
||||||
else if (s->mode == 'r')
|
|
||||||
{
|
|
||||||
err = inflateEnd(&(s->stream));
|
err = inflateEnd(&(s->stream));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (s->file != NULL && memClose(s->file))
|
if (s->file != NULL && memClose(s->file)) {
|
||||||
{
|
|
||||||
#ifdef ESPIPE
|
#ifdef ESPIPE
|
||||||
if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
|
if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
|
||||||
#endif
|
#endif
|
||||||
@ -511,16 +480,13 @@ unsigned len;
|
|||||||
s->stream.next_out = (Bytef*)buf;
|
s->stream.next_out = (Bytef*)buf;
|
||||||
s->stream.avail_out = len;
|
s->stream.avail_out = len;
|
||||||
|
|
||||||
while (s->stream.avail_out != 0)
|
while (s->stream.avail_out != 0) {
|
||||||
{
|
|
||||||
|
|
||||||
if (s->transparent)
|
if (s->transparent) {
|
||||||
{
|
|
||||||
/* Copy first the lookahead bytes: */
|
/* Copy first the lookahead bytes: */
|
||||||
uInt n = s->stream.avail_in;
|
uInt n = s->stream.avail_in;
|
||||||
if (n > s->stream.avail_out) n = s->stream.avail_out;
|
if (n > s->stream.avail_out) n = s->stream.avail_out;
|
||||||
if (n > 0)
|
if (n > 0) {
|
||||||
{
|
|
||||||
zmemcpy(s->stream.next_out, s->stream.next_in, n);
|
zmemcpy(s->stream.next_out, s->stream.next_in, n);
|
||||||
next_out += n;
|
next_out += n;
|
||||||
s->stream.next_out = next_out;
|
s->stream.next_out = next_out;
|
||||||
@ -528,10 +494,8 @@ unsigned len;
|
|||||||
s->stream.avail_out -= n;
|
s->stream.avail_out -= n;
|
||||||
s->stream.avail_in -= n;
|
s->stream.avail_in -= n;
|
||||||
}
|
}
|
||||||
if (s->stream.avail_out > 0)
|
if (s->stream.avail_out > 0) {
|
||||||
{
|
s->stream.avail_out -= (uInt)memRead(next_out, 1, s->stream.avail_out, s->file);
|
||||||
s->stream.avail_out -= memRead(next_out, 1, s->stream.avail_out,
|
|
||||||
s->file);
|
|
||||||
}
|
}
|
||||||
len -= s->stream.avail_out;
|
len -= s->stream.avail_out;
|
||||||
s->stream.total_in += (uLong)len;
|
s->stream.total_in += (uLong)len;
|
||||||
@ -539,16 +503,13 @@ unsigned len;
|
|||||||
if (len == 0) s->z_eof = 1;
|
if (len == 0) s->z_eof = 1;
|
||||||
return (int)len;
|
return (int)len;
|
||||||
}
|
}
|
||||||
if (s->stream.avail_in == 0 && !s->z_eof)
|
if (s->stream.avail_in == 0 && !s->z_eof) {
|
||||||
{
|
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
|
s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
|
||||||
if (s->stream.avail_in == 0)
|
if (s->stream.avail_in == 0) {
|
||||||
{
|
|
||||||
s->z_eof = 1;
|
s->z_eof = 1;
|
||||||
if (memError(s->file))
|
if (memError(s->file)) {
|
||||||
{
|
|
||||||
s->z_err = Z_ERRNO;
|
s->z_err = Z_ERRNO;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -557,26 +518,21 @@ unsigned len;
|
|||||||
}
|
}
|
||||||
s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
|
s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
|
||||||
|
|
||||||
if (s->z_err == Z_STREAM_END)
|
if (s->z_err == Z_STREAM_END) {
|
||||||
{
|
|
||||||
/* Check CRC and original size */
|
/* Check CRC and original size */
|
||||||
s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
|
s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
|
||||||
start = s->stream.next_out;
|
start = s->stream.next_out;
|
||||||
|
|
||||||
if (getLong(s) != s->crc)
|
if (getLong(s) != s->crc) {
|
||||||
{
|
|
||||||
s->z_err = Z_DATA_ERROR;
|
s->z_err = Z_DATA_ERROR;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
(void)getLong(s);
|
(void)getLong(s);
|
||||||
/* The uncompressed length returned by above getlong() may
|
/* The uncompressed length returned by above getlong() may
|
||||||
* be different from s->stream.total_out) in case of
|
* be different from s->stream.total_out) in case of
|
||||||
* concatenated .gz files. Check for such files:
|
* concatenated .gz files. Check for such files:
|
||||||
*/
|
*/
|
||||||
check_header(s);
|
check_header(s);
|
||||||
if (s->z_err == Z_OK)
|
if (s->z_err == Z_OK) {
|
||||||
{
|
|
||||||
uLong total_in = s->stream.total_in;
|
uLong total_in = s->stream.total_in;
|
||||||
uLong total_out = s->stream.total_out;
|
uLong total_out = s->stream.total_out;
|
||||||
|
|
||||||
@ -612,15 +568,12 @@ unsigned len;
|
|||||||
s->stream.next_in = (Bytef*)buf;
|
s->stream.next_in = (Bytef*)buf;
|
||||||
s->stream.avail_in = len;
|
s->stream.avail_in = len;
|
||||||
|
|
||||||
while (s->stream.avail_in != 0)
|
while (s->stream.avail_in != 0) {
|
||||||
{
|
|
||||||
|
|
||||||
if (s->stream.avail_out == 0)
|
if (s->stream.avail_out == 0) {
|
||||||
{
|
|
||||||
|
|
||||||
s->stream.next_out = s->outbuf;
|
s->stream.next_out = s->outbuf;
|
||||||
if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE)
|
if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
|
||||||
{
|
|
||||||
s->z_err = Z_ERRNO;
|
s->z_err = Z_ERRNO;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -650,14 +603,11 @@ int flush;
|
|||||||
|
|
||||||
s->stream.avail_in = 0; /* should be zero already anyway */
|
s->stream.avail_in = 0; /* should be zero already anyway */
|
||||||
|
|
||||||
for (;;)
|
for (;;) {
|
||||||
{
|
|
||||||
len = Z_BUFSIZE - s->stream.avail_out;
|
len = Z_BUFSIZE - s->stream.avail_out;
|
||||||
|
|
||||||
if (len != 0)
|
if (len != 0) {
|
||||||
{
|
if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len) {
|
||||||
if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len)
|
|
||||||
{
|
|
||||||
s->z_err = Z_ERRNO;
|
s->z_err = Z_ERRNO;
|
||||||
return Z_ERRNO;
|
return Z_ERRNO;
|
||||||
}
|
}
|
||||||
@ -688,8 +638,7 @@ MEMFILE *file;
|
|||||||
uLong x;
|
uLong x;
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
for (n = 0; n < 4; n++)
|
for (n = 0; n < 4; n++) {
|
||||||
{
|
|
||||||
memPutc((int)(x & 0xff), file);
|
memPutc((int)(x & 0xff), file);
|
||||||
x >>= 8;
|
x >>= 8;
|
||||||
}
|
}
|
||||||
@ -725,8 +674,7 @@ gzFile file;
|
|||||||
|
|
||||||
if (s == NULL) return Z_STREAM_ERROR;
|
if (s == NULL) return Z_STREAM_ERROR;
|
||||||
|
|
||||||
if (s->mode == 'w')
|
if (s->mode == 'w') {
|
||||||
{
|
|
||||||
#ifdef NO_DEFLATE
|
#ifdef NO_DEFLATE
|
||||||
return Z_STREAM_ERROR;
|
return Z_STREAM_ERROR;
|
||||||
#else
|
#else
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
#ifndef MEMGZIO_H
|
||||||
|
#define MEMGZIO_H
|
||||||
|
|
||||||
/* gzio.c -- IO on .gz files
|
/* gzio.c -- IO on .gz files
|
||||||
* Copyright (C) 1995-2002 Jean-loup Gailly.
|
* Copyright (C) 1995-2002 Jean-loup Gailly.
|
||||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
@ -8,27 +11,13 @@
|
|||||||
/* memgzio.c - IO on .gz files in memory
|
/* memgzio.c - IO on .gz files in memory
|
||||||
* Adapted from original gzio.c from zlib library by Forgotten
|
* Adapted from original gzio.c from zlib library by Forgotten
|
||||||
*/
|
*/
|
||||||
#ifndef HAVE_ZUTIL_H
|
|
||||||
#include "../win32/include/zlib/zutil.h"
|
|
||||||
#else
|
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef local
|
gzFile ZEXPORT memgzopen(char *memory, int available, const char *mode);
|
||||||
# define local static
|
int ZEXPORT memgzread(gzFile file, voidp buf, unsigned len);
|
||||||
#endif
|
int ZEXPORT memgzwrite(gzFile file, const voidp buf, unsigned len);
|
||||||
|
int ZEXPORT memgzclose(gzFile file);
|
||||||
|
long ZEXPORT memtell(gzFile file);
|
||||||
|
|
||||||
#if MAX_MEM_LEVEL >= 8
|
#endif // MEMGZIO_H
|
||||||
# define DEF_MEM_LEVEL 8
|
|
||||||
#else
|
|
||||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define OS_CODE 0x03 /* assume Unix */
|
|
||||||
#define zmemcpy memcpy
|
|
||||||
|
|
||||||
gzFile ZEXPORT memgzopen(char *memory, int, const char *);
|
|
||||||
int ZEXPORT memgzread(gzFile, voidp, unsigned);
|
|
||||||
int ZEXPORT memgzwrite(gzFile, const voidp, unsigned);
|
|
||||||
int ZEXPORT memgzclose(gzFile);
|
|
||||||
long ZEXPORT memtell(gzFile);
|
|
||||||
|
@ -3550,24 +3550,23 @@ static bool gbWriteSaveState(gzFile gzFile)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gbWriteMemSaveState(char *memory, int available)
|
int gbWriteMemSaveState(char *memory, int available)
|
||||||
{
|
{
|
||||||
|
int pos = 0;
|
||||||
gzFile gzFile = utilMemGzOpen(memory, available, "w");
|
gzFile gzFile = utilMemGzOpen(memory, available, "w");
|
||||||
|
|
||||||
if(gzFile == NULL) {
|
if(gzFile == NULL)
|
||||||
return false;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
bool res = gbWriteSaveState(gzFile);
|
if(gbWriteSaveState(gzFile))
|
||||||
|
{
|
||||||
long pos = utilGzMemTell(gzFile)+8;
|
pos = utilGzMemTell(gzFile)+8;
|
||||||
|
|
||||||
if(pos >= (available))
|
if(pos >= (available))
|
||||||
res = false;
|
pos = 0;
|
||||||
|
}
|
||||||
utilGzClose(gzFile);
|
utilGzClose(gzFile);
|
||||||
|
return pos;
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gbWriteSaveState(const char *name)
|
bool gbWriteSaveState(const char *name)
|
||||||
|
@ -30,7 +30,7 @@ bool gbWriteBatteryFile(const char *);
|
|||||||
bool gbWriteBatteryFile(const char *, bool);
|
bool gbWriteBatteryFile(const char *, bool);
|
||||||
bool gbReadBatteryFile(const char *);
|
bool gbReadBatteryFile(const char *);
|
||||||
bool gbWriteSaveState(const char *);
|
bool gbWriteSaveState(const char *);
|
||||||
bool gbWriteMemSaveState(char *, int);
|
int gbWriteMemSaveState(char *, int);
|
||||||
bool gbReadSaveState(const char *);
|
bool gbReadSaveState(const char *);
|
||||||
bool gbReadMemSaveState(char *, int);
|
bool gbReadMemSaveState(char *, int);
|
||||||
void gbSgbRenderBorder();
|
void gbSgbRenderBorder();
|
||||||
|
@ -634,24 +634,23 @@ bool CPUWriteState(const char *file)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CPUWriteMemState(char *memory, int available)
|
int CPUWriteMemState(char *memory, int available)
|
||||||
{
|
{
|
||||||
|
int pos = 0;
|
||||||
gzFile gzFile = utilMemGzOpen(memory, available, "w");
|
gzFile gzFile = utilMemGzOpen(memory, available, "w");
|
||||||
|
|
||||||
if(gzFile == NULL) {
|
if(gzFile == NULL)
|
||||||
return false;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
bool res = CPUWriteState(gzFile);
|
if(CPUWriteState(gzFile))
|
||||||
|
{
|
||||||
long pos = utilGzMemTell(gzFile)+8;
|
pos = utilGzMemTell(gzFile)+8;
|
||||||
|
|
||||||
if(pos >= (available))
|
if(pos >= (available))
|
||||||
res = false;
|
pos = 0;
|
||||||
|
}
|
||||||
utilGzClose(gzFile);
|
utilGzClose(gzFile);
|
||||||
|
return pos;
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CPUReadState(gzFile gzFile)
|
static bool CPUReadState(gzFile gzFile)
|
||||||
|
@ -90,7 +90,7 @@ extern void CPUUpdateRender();
|
|||||||
extern void CPUUpdateRenderBuffers(bool);
|
extern void CPUUpdateRenderBuffers(bool);
|
||||||
extern bool CPUReadMemState(char *, int);
|
extern bool CPUReadMemState(char *, int);
|
||||||
extern bool CPUReadState(const char *);
|
extern bool CPUReadState(const char *);
|
||||||
extern bool CPUWriteMemState(char *, int);
|
extern int CPUWriteMemState(char *, int);
|
||||||
extern bool CPUWriteState(const char *);
|
extern bool CPUWriteState(const char *);
|
||||||
extern int CPULoadRom(const char *);
|
extern int CPULoadRom(const char *);
|
||||||
extern void doMirroring(bool);
|
extern void doMirroring(bool);
|
||||||
|
Loading…
Reference in New Issue
Block a user