1
0
Fork 0
mirror of https://github.com/marcrobledo/RomPatcher.js.git synced 2025-06-27 16:25:54 +00:00

refactor (bps): added BPS.calculateFileChecksum

This commit is contained in:
Marc Robledo 2024-08-25 19:59:59 +02:00
parent b2f1b8d57e
commit 5e248e2565
2 changed files with 11 additions and 10 deletions

View file

@ -26,6 +26,10 @@ BPS.prototype.toString=function(){
s+='\n#Actions: '+this.actions.length;
return s
}
BPS.prototype.calculateFileChecksum = function () {
var patchFile = this.export();
return patchFile.hashCRC32(0, patchFile.fileSize - 4);
}
BPS.prototype.validateSource=function(romFile,headerSize){return this.sourceChecksum===romFile.hashCRC32(headerSize)}
BPS.prototype.getValidationInfo=function(){
return {
@ -121,7 +125,7 @@ BPS.fromFile=function(file){
patch.targetChecksum=file.readU32();
patch.patchChecksum=file.readU32();
if(patch.patchChecksum!==file.hashCRC32(0, file.fileSize - 4)){
if (patch.patchChecksum !== patch.calculateFileChecksum()) {
throw new Error('Patch checksum mismatch');
}
@ -240,8 +244,7 @@ BPS.buildFromRoms=function(original, modified, deltaMode){
patch.sourceChecksum=original.hashCRC32();
patch.targetChecksum=modified.hashCRC32();
var patchFile=patch.export();
patch.patchChecksum=patchFile.hashCRC32(0, patchFile.fileSize - 4);
patch.patchChecksum = patch.calculateFileChecksum();
return patch;
}

12
test.js
View file

@ -114,11 +114,11 @@ const TEST_DATA = (new Uint8Array([
_test('HashCalculator integrity', function () {
if (HashCalculator.md5(TEST_DATA) !== '55c76e7e683fd7cd63c673c5df3efa6e')
throw new Error('invalid MD5');
if (HashCalculator.crc32(TEST_DATA).toString(16) !== '903a031b')
if (HashCalculator.crc32(TEST_DATA) !== 0x903a031b)
throw new Error('invalid CRC32');
if (HashCalculator.adler32(TEST_DATA).toString(16) !== 'ef984205')
if (HashCalculator.adler32(TEST_DATA) !== 0xef984205)
throw new Error('invalid ADLER32');
if (HashCalculator.crc16(TEST_DATA).toString(16) !== '96e4')
if (HashCalculator.crc16(TEST_DATA) !== 0x96e4)
throw new Error('invalid SHA1');
});
@ -134,11 +134,9 @@ const MODIFIED_TEST_DATA = (new Uint8Array([
const patchedFile = RomPatcher.applyPatch(originalFile, patch, { requireValidation: true });
if (patchFormat === 'bps') {
const patchFile = patch.export();
const patchChecksum = patchFile.hashCRC32(0, patchFile.fileSize - 4);
if (patch.patchChecksum !== patchChecksum)
if (patch.patchChecksum !== patch.calculateFileChecksum())
throw new Error('invalid patch checksum');
else if (patchFile.hashCRC32() !== 0x2144df1c)
else if (patch.export().hashCRC32() !== 0x2144df1c)
throw new Error('invalid BPS crc32');
}