src/burner/state.cpp
// Write a savestate as a chunk of an "FB1 " file
// nOffset is the absolute offset from the beginning of the file
// -1: Append at current position
// -2: Append at EOF
Write a savestate as a chunk of an "FB1 " file
nOffset is the absolute offset from the beginning of the file
1: Append at current position
2: Append at EOF
Write a savestate as a chunk of an " FB1 " file nOffset is the absolute offset from the beginning of the file 1 : Append at current position 2 : Append at EOF
INT32 BurnStateSaveEmbed(FILE* fp, INT32 nOffset, INT32 bAll)
{
	const char* szHeader = "FS1 ";						
	INT32 nLen = 0;
	INT32 nNvMin = 0, nAMin = 0;
	INT32 nZero = 0;
	char szGame[33];
	UINT8 *Def = NULL;
	INT32 nDefLen = 0;									
	INT32 nRet = 0;
	if (fp == NULL) {
		return -1;
	}
	StateInfo(&nLen, &nNvMin, 0);						
	nAMin = nNvMin;
	if (bAll) {											
		StateInfo(&nLen, &nAMin, 1);
	}
	if (nLen <= 0) {									
		return -1;
	}
	if (nOffset >= 0) {
		fseek(fp, nOffset, SEEK_SET);
	} else {
		if (nOffset == -2) {
			fseek(fp, 0, SEEK_END);
		} else {
			fseek(fp, 0, SEEK_CUR);
		}
	}
	fwrite(szHeader, 1, 4, fp);							
	INT32 nSizeOffset = ftell(fp);						
	fwrite(&nZero, 1, 4, fp);							
	fwrite(&nBurnVer, 1, 4, fp);						 Version of FB this was saved from
	fwrite(&nNvMin, 1, 4, fp);							 Min version of FB NV  data will work with
	fwrite(&nAMin, 1, 4, fp);							 Min version of FB All data will work with
	fwrite(&nZero, 1, 4, fp);							 Reserve space to write the compressed data size
	memset(szGame, 0, sizeof(szGame));					 Game name
	sprintf(szGame, "%.32s", BurnDrvGetTextA(DRV_NAME));			
	fwrite(szGame, 1, 32, fp);							
	fwrite(&nCurrentFrame, 1, 4, fp);					 Current frame
	fwrite(&nZero, 1, 4, fp);							 Reserved
	fwrite(&nZero, 1, 4, fp);							
	fwrite(&nZero, 1, 4, fp);							
	nRet = BurnStateCompress(&Def, &nDefLen, bAll);		 Compress block from driver and return deflated buffer
	if (Def == NULL) {
		return -1;
	}
	nRet = fwrite(Def, 1, nDefLen, fp);					 Write block to disk
	if (Def) {
		free(Def);											 free deflated block and close file
		Def = NULL;
	}
	if (nRet != nDefLen) {								 error writing block to disk
		return -1;
	}
	if (nDefLen & 3) {									 Chunk size must be a multiple of 4
		fwrite(&nZero, 1, 4 - (nDefLen & 3), fp);		 Pad chunk if needed
	}
	fseek(fp, nSizeOffset + 0x10, SEEK_SET);			 Write size of the compressed data
	fwrite(&nDefLen, 1, 4, fp);							
	nDefLen = (nDefLen + 0x43) & ~3;					 Add for header size and align
	fseek(fp, nSizeOffset, SEEK_SET);					 Write size of the chunk
	fwrite(&nDefLen, 1, 4, fp);							
	fseek (fp, 0, SEEK_END);							 Set file pointer to the end of the chunk
	return nDefLen;
}
INT32 BurnStateSaveEmbed ( FILE * fp , INT32 nOffset , INT32 bAll ) { const char * szHeader = " " ; INT32 nLen = 0 ; INT32 nNvMin = 0 , nAMin = 0 ; INT32 nZero = 0 ; char szGame [ 33 ] ; UINT8 * Def = NULL ; INT32 nDefLen = 0 ; INT32 nRet = 0 ; if ( fp == NULL ) { return -1 ; } StateInfo ( & nLen , & nNvMin , 0 ) ; nAMin = nNvMin ; if ( bAll ) { StateInfo ( & nLen , & nAMin , 1 ) ; } if ( nLen <= 0 ) { return -1 ; } if ( nOffset >= 0 ) { fseek ( fp , nOffset , SEEK_SET ) ; } else { if ( nOffset == -2 ) { fseek ( fp , 0 , SEEK_END ) ; } else { fseek ( fp , 0 , SEEK_CUR ) ; } } fwrite ( szHeader , 1 , 4 , fp ) ; INT32 nSizeOffset = ftell ( fp ) ; fwrite ( & nZero , 1 , 4 , fp ) ; fwrite ( & nBurnVer , 1 , 4 , fp ) ; fwrite ( & nNvMin , 1 , 4 , fp ) ; fwrite ( & nAMin , 1 , 4 , fp ) ; fwrite ( & nZero , 1 , 4 , fp ) ; memset ( szGame , 0 , sizeof ( szGame ) ) ; sprintf ( szGame , " " , BurnDrvGetTextA ( DRV_NAME ) ) ; fwrite ( szGame , 1 , 32 , fp ) ; fwrite ( & nCurrentFrame , 1 , 4 , fp ) ; fwrite ( & nZero , 1 , 4 , fp ) ; fwrite ( & nZero , 1 , 4 , fp ) ; fwrite ( & nZero , 1 , 4 , fp ) ; nRet = BurnStateCompress ( & Def , & nDefLen , bAll ) ; if ( Def == NULL ) { return -1 ; } nRet = fwrite ( Def , 1 , nDefLen , fp ) ; if ( Def ) { free ( Def ) ; Def = NULL ; } if ( nRet != nDefLen ) { return -1 ; } if ( nDefLen & 3 ) { fwrite ( & nZero , 1 , 4 - ( nDefLen & 3 ) , fp ) ; } fseek ( fp , nSizeOffset + 0x10 , SEEK_SET ) ; fwrite ( & nDefLen , 1 , 4 , fp ) ; nDefLen = ( nDefLen + 0x43 ) & ~ 3 ; fseek ( fp , nSizeOffset , SEEK_SET ) ; fwrite ( & nDefLen , 1 , 4 , fp ) ; fseek ( fp , 0 , SEEK_END ) ; return nDefLen ; }
Write a savestate as a chunk of an "FB1 " file
nOffset is the absolute offset from the beginning of the file
1: Append at current position
2: Append at EOF
Write a savestate as a chunk of an " FB1 " file nOffset is the absolute offset from the beginning of the file 1 : Append at current position 2 : Append at EOF
// Chunk identifier // Deflated version // Get minimum version for NV part // Get minimum version for All data // No memory to save // Chunk identifier // Reserve space to write the size of this chunk // // Version of FB this was saved from // Min version of FB NV  data will work with // Min version of FB All data will work with // Reserve space to write the compressed data size // Game name // // // Current frame // Reserved // // // Compress block from driver and return deflated buffer // Write block to disk // free deflated block and close file // error writing block to disk // Chunk size must be a multiple of 4 // Pad chunk if needed // Write size of the compressed data // // Add for header size and align // Write size of the chunk // // Set file pointer to the end of the chunk