int StateSav_SaveAtariState(const char *filename, const char *mode, UBYTE SaveVerbose) { UBYTE StateVersion = SAVE_VERSION_NUMBER; if (StateFile != NULL) { GZCLOSE(StateFile); StateFile = NULL; } nFileError = Z_OK; StateFile = GZOPEN(filename, mode); if (StateFile == NULL) { Log_print("Could not open %s for state save.", filename); GetGZErrorText(); return FALSE; } if (GZWRITE(StateFile, "ATARI800", 8) == 0) { GetGZErrorText(); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } StateSav_SaveUBYTE(&StateVersion, 1); StateSav_SaveUBYTE(&SaveVerbose, 1); /* The order here is important. Atari800_StateSave must be first because it saves the machine type, and decisions on what to save/not save are made based off that later in the process */ Atari800_StateSave(); CARTRIDGE_StateSave(); SIO_StateSave(); ANTIC_StateSave(); CPU_StateSave(SaveVerbose); GTIA_StateSave(); PIA_StateSave(); POKEY_StateSave(); #ifdef XEP80_EMULATION XEP80_StateSave(); #else { int local_xep80_enabled = FALSE; StateSav_SaveINT(&local_xep80_enabled, 1); } #endif /* XEP80_EMULATION */ PBI_StateSave(); #ifdef PBI_MIO PBI_MIO_StateSave(); #else { int local_mio_enabled = FALSE; StateSav_SaveINT(&local_mio_enabled, 1); } #endif /* PBI_MIO */ #ifdef PBI_BB PBI_BB_StateSave(); #else { int local_bb_enabled = FALSE; StateSav_SaveINT(&local_bb_enabled, 1); } #endif /* PBI_BB */ #ifdef PBI_XLD PBI_XLD_StateSave(); #else { int local_xld_enabled = FALSE; StateSav_SaveINT(&local_xld_enabled, 1); } #endif /* PBI_XLD */ #ifdef DREAMCAST DCStateSave(); #endif if (GZCLOSE(StateFile) != 0) { StateFile = NULL; return FALSE; } StateFile = NULL; if (nFileError != Z_OK) return FALSE; return TRUE; }
int StateSav_ReadAtariState(const char *filename, const char *mode) { char header_string[8]; UBYTE StateVersion = 0; /* The version of the save file */ UBYTE SaveVerbose = 0; /* Verbose mode means save basic, OS if patched */ if (StateFile != NULL) { GZCLOSE(StateFile); StateFile = NULL; } nFileError = Z_OK; StateFile = GZOPEN(filename, mode); if (StateFile == NULL) { Log_print("Could not open %s for state read.", filename); GetGZErrorText(); return FALSE; } if (GZREAD(StateFile, header_string, 8) == 0) { GetGZErrorText(); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } if (memcmp(header_string, "ATARI800", 8) != 0) { Log_print("This is not an Atari800 state save file."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } if (GZREAD(StateFile, &StateVersion, 1) == 0 || GZREAD(StateFile, &SaveVerbose, 1) == 0) { Log_print("Failed read from Atari state file."); GetGZErrorText(); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } if (StateVersion != SAVE_VERSION_NUMBER && StateVersion < 3) { Log_print("Cannot read this state file because it is an incompatible version."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } Atari800_StateRead(); if (StateVersion >= 4) { CARTRIDGE_StateRead(); SIO_StateRead(); } ANTIC_StateRead(); CPU_StateRead(SaveVerbose, StateVersion); GTIA_StateRead(); PIA_StateRead(); POKEY_StateRead(); if (StateVersion >= 6) { #ifdef XEP80_EMULATION XEP80_StateRead(); #else int local_xep80_enabled; StateSav_ReadINT(&local_xep80_enabled,1); if (local_xep80_enabled) { Log_print("Cannot read this state file because this version does not support XEP80."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } #endif /* XEP80_EMULATION */ PBI_StateRead(); #ifdef PBI_MIO PBI_MIO_StateRead(); #else { int local_mio_enabled; StateSav_ReadINT(&local_mio_enabled,1); if (local_mio_enabled) { Log_print("Cannot read this state file because this version does not support MIO."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } } #endif /* PBI_MIO */ #ifdef PBI_BB PBI_BB_StateRead(); #else { int local_bb_enabled; StateSav_ReadINT(&local_bb_enabled,1); if (local_bb_enabled) { Log_print("Cannot read this state file because this version does not support the Black Box."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } } #endif /* PBI_BB */ #ifdef PBI_XLD PBI_XLD_StateRead(); #else { int local_xld_enabled; StateSav_ReadINT(&local_xld_enabled,1); if (local_xld_enabled) { Log_print("Cannot read this state file because this version does not support the 1400XL/1450XLD."); GZCLOSE(StateFile); StateFile = NULL; return FALSE; } } #endif /* PBI_XLD */ } #ifdef DREAMCAST DCStateRead(); #endif GZCLOSE(StateFile); StateFile = NULL; if (nFileError != Z_OK) return FALSE; return TRUE; }
/* Opens a ZLIB compressed (gzip) file, creates a temporary filename, and decompresses the contents of the .gz file to the temporary file name. Note that *outfilename is actually blank coming in and is filled by mkstemp */ FILE * openzlib(int diskno, const char *infilename, char *outfilename ) { #ifndef HAVE_LIBZ Aprint( "This executable cannot decompress ZLIB files" ); return NULL; #else gzFile gzSource; FILE *file = NULL, *outfile = NULL; char *curptr = outfilename; char *zlib_buffer = NULL; if( zlib_capable() == -1 ) { Aprint( "This executable cannot decompress ZLIB files" ); return NULL; } zlib_buffer = malloc( ZLIB_BUFFER_SIZE + 1 ); if( !zlib_buffer ) { Aprint( "Could not obtain memory for zlib decompression" ); return NULL; } curptr += prepend_tmpfile_path( outfilename ); strcpy(curptr,"TMP_XXXXXX\0"); outfile = fdopen(mkstemp(curptr), "wb"); if (!outfile) { Aprint( "Could not open temporary file" ); free( zlib_buffer ); return NULL; } gzSource = GZOPEN( infilename, "rb" ); if( !gzSource ) { Aprint( "ZLIB could not open file %s", infilename ); fclose( outfile ); } else /* Convert the gzip file to the temporary file */ { int result, temp; Aprint( "Converting %s to %s", infilename, outfilename ); do { result = GZREAD( gzSource, &zlib_buffer[0], ZLIB_BUFFER_SIZE ); if( result > 0 ) { if( fwrite(zlib_buffer, 1, result, outfile) != result ) { Aprint( "Error writing to temporary file %s, disk may be full", outfilename ); result = -1; } } } while( result == ZLIB_BUFFER_SIZE ); temp = GZCLOSE( gzSource ); fclose( outfile ); if( result > -1 ) file = fopen(outfilename, "rb"); else { Aprint( "Error while parsing gzip file" ); file = NULL; } } if(!file) { if( zlib_buffer ) free( zlib_buffer ); Aprint( "Removing temporary file %s", outfilename ); remove( outfilename ); } return file; #endif /* HAVE_LIBZ */ }