blargg_err_t Nes_Rom::resize_chr( long size ) { if ( size != chr_size_ ) { void* p = realloc( chr_, round_to_bank_size( size ) ); BLARGG_CHECK_ALLOC( p || !size ); chr_ = (byte*) p; chr_size_ = size; } return blargg_success; }
blargg_err_t Nes_Rewinder::init() { if ( !frames ) { BLARGG_RETURN_ERR( recorder::init() ); frames = BLARGG_NEW frame_t [frames_size]; BLARGG_CHECK_ALLOC( frames ); } return blargg_success; }
static blargg_err_t apply_ips_patch( Data_Reader& patch, byte** file, long* file_size ) { byte signature [5]; BLARGG_RETURN_ERR( patch.read( signature, sizeof signature ) ); if ( memcmp( signature, "PATCH", sizeof signature ) ) return "Not an IPS patch file"; while ( patch.remain() ) { // read offset byte buf [6]; BLARGG_RETURN_ERR( patch.read( buf, 3 ) ); long offset = buf [0] * 0x10000 + buf [1] * 0x100 + buf [2]; if ( offset == 'EOF' ) break; // read size BLARGG_RETURN_ERR( patch.read( buf, 2 ) ); long size = buf [0] * 0x100 + buf [1]; // size = 0 signals a run of identical bytes int fill = -1; if ( size == 0 ) { BLARGG_RETURN_ERR( patch.read( buf, 3 ) ); size = buf [0] * 0x100 + buf [1]; fill = buf [2]; } // expand file if new data is at exact end of file if ( offset == *file_size ) { *file_size = offset + size; void* p = realloc( *file, *file_size ); BLARGG_CHECK_ALLOC( p ); *file = (byte*) p; } //dprintf( "Patch offset: 0x%04X, size: 0x%04X\n", (int) offset, (int) size ); if ( offset < 0 || *file_size < offset + size ) return "IPS tried to patch past end of file"; // read/fill data if ( fill < 0 ) BLARGG_RETURN_ERR( patch.read( *file + offset, size ) ); else memset( *file + offset, fill, size ); } return blargg_success; }
blargg_err_t Nes_Snapshot_Array::resize( int new_size ) { void* new_mem = realloc( data, new_size * sizeof (Nes_Snapshot) ); BLARGG_CHECK_ALLOC( !new_size || new_mem ); data = (Nes_Snapshot*) new_mem; int old_size = size_; size_ = new_size; for ( int i = old_size; i < new_size; i++ ) (*this) [i].clear(); return blargg_success; }
blargg_err_t Nes_Rom::resize_prg( long size ) { if ( size != prg_size_ ) { // extra byte allows CPU to always read operand of instruction, which // might go past end of ROM void* p = realloc( prg_, round_to_bank_size( size ) + 1 ); BLARGG_CHECK_ALLOC( p || !size ); prg_ = (byte*) p; prg_size_ = size; } return blargg_success; }
blargg_err_t Nes_Rom::load_patched_ines_rom( Data_Reader& in, Data_Reader& patch ) { // read file into memory long size = in.remain(); byte* ines = (byte*) malloc( size ); BLARGG_CHECK_ALLOC( ines ); const char* err = in.read( ines, size ); // apply patch if ( !err ) err = apply_ips_patch( patch, &ines, &size ); // load patched file if ( !err ) { Mem_File_Reader patched( ines, size ); err = load_ines_rom( patched ); } free( ines ); return err; }