void BackupDevice::ensure(u32 addr, u8 val, EMUFILE_FILE *fpOut) { if (!fpOut && (addr < fsize)) return; EMUFILE_FILE *fp = fpOut?fpOut:fpMC; #ifndef _DONT_SAVE_BACKUP fp->fseek(fsize, SEEK_SET); #endif u32 padSize = pad_up_size(addr); u32 size = padSize - fsize; info.padSize = info.size = fsize = padSize; int type = searchFileSaveType(fsize); if (type != 0xFF) info.type = (type + 1); #ifndef _DONT_SAVE_BACKUP if (size > 0) { u8 *tmp = new u8[size]; memset(tmp, val, size); fwrite(tmp, 1, size, fp->get_fp()); delete [] tmp; } //this is just for humans to read fp->fprintf(DESMUME_BACKUP_FOOTER_TXT); //and now the actual footer fp->write32le(addr); //the size of data that has actually been written fp->write32le(padSize); //the size we padded it to fp->write32le(info.type); //save memory type fp->write32le(addr_size); fp->write32le(info.size); //save memory size fp->write32le((u32)0); //version number fp->fprintf("%s", kDesmumeSaveCookie); //this is what we'll use to recognize the desmume format save fp->fflush(); //this is a HORRIBLE IDEA. //leave the FP positioned to write the final byte //this is a HACK to make the basic read/write byte operation work when it calls ensure(). //IDEALLY, no assumptions about the file pointer can be made. //but someone (actually, not really) so very carefully profiled the save IO code and discovered that not fseeking for every byte read/write was a great optimization. //so, now all this code is depending/assuming on the FP being kept in a precise position, and I dont think its smart to change the main user of this assumption to paper over this bug by making it fseek before read/write, while leaving other unknown assuming clients intact fpMC->fseek(addr-1, SEEK_SET); #endif }
void BackupDevice::ensure(u32 addr, u8 val, EMUFILE_FILE *fpOut) { if (!fpOut && (addr < fsize)) return; EMUFILE_FILE *fp = fpOut?fpOut:fpMC; #ifndef _DONT_SAVE_BACKUP fp->fseek(fsize, SEEK_SET); #endif u32 padSize = pad_up_size(addr); u32 size = padSize - fsize; info.padSize = info.size = fsize = padSize; int type = searchFileSaveType(fsize); if (type != 0xFF) info.type = (type + 1); #ifndef _DONT_SAVE_BACKUP if (size > 0) { u8 *tmp = new u8[size]; memset(tmp, val, size); fwrite(tmp, 1, size, fp->get_fp()); delete [] tmp; } //this is just for humans to read fp->fprintf(DESMUME_BACKUP_FOOTER_TXT); //and now the actual footer fp->write32le(addr); //the size of data that has actually been written fp->write32le(padSize); //the size we padded it to fp->write32le(info.type); //save memory type fp->write32le(addr_size); fp->write32le(info.size); //save memory size fp->write32le((u32)0); //version number fp->fprintf("%s", kDesmumeSaveCookie); //this is what we'll use to recognize the desmume format save fp->fflush(); fp->fseek(addr, SEEK_SET); #endif }