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:
parent
b2f1b8d57e
commit
5e248e2565
2 changed files with 11 additions and 10 deletions
|
@ -26,6 +26,10 @@ BPS.prototype.toString=function(){
|
||||||
s+='\n#Actions: '+this.actions.length;
|
s+='\n#Actions: '+this.actions.length;
|
||||||
return s
|
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.validateSource=function(romFile,headerSize){return this.sourceChecksum===romFile.hashCRC32(headerSize)}
|
||||||
BPS.prototype.getValidationInfo=function(){
|
BPS.prototype.getValidationInfo=function(){
|
||||||
return {
|
return {
|
||||||
|
@ -121,7 +125,7 @@ BPS.fromFile=function(file){
|
||||||
patch.targetChecksum=file.readU32();
|
patch.targetChecksum=file.readU32();
|
||||||
patch.patchChecksum=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');
|
throw new Error('Patch checksum mismatch');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,8 +244,7 @@ BPS.buildFromRoms=function(original, modified, deltaMode){
|
||||||
|
|
||||||
patch.sourceChecksum=original.hashCRC32();
|
patch.sourceChecksum=original.hashCRC32();
|
||||||
patch.targetChecksum=modified.hashCRC32();
|
patch.targetChecksum=modified.hashCRC32();
|
||||||
var patchFile=patch.export();
|
patch.patchChecksum = patch.calculateFileChecksum();
|
||||||
patch.patchChecksum=patchFile.hashCRC32(0, patchFile.fileSize - 4);
|
|
||||||
return patch;
|
return patch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
test.js
12
test.js
|
@ -114,11 +114,11 @@ const TEST_DATA = (new Uint8Array([
|
||||||
_test('HashCalculator integrity', function () {
|
_test('HashCalculator integrity', function () {
|
||||||
if (HashCalculator.md5(TEST_DATA) !== '55c76e7e683fd7cd63c673c5df3efa6e')
|
if (HashCalculator.md5(TEST_DATA) !== '55c76e7e683fd7cd63c673c5df3efa6e')
|
||||||
throw new Error('invalid MD5');
|
throw new Error('invalid MD5');
|
||||||
if (HashCalculator.crc32(TEST_DATA).toString(16) !== '903a031b')
|
if (HashCalculator.crc32(TEST_DATA) !== 0x903a031b)
|
||||||
throw new Error('invalid CRC32');
|
throw new Error('invalid CRC32');
|
||||||
if (HashCalculator.adler32(TEST_DATA).toString(16) !== 'ef984205')
|
if (HashCalculator.adler32(TEST_DATA) !== 0xef984205)
|
||||||
throw new Error('invalid ADLER32');
|
throw new Error('invalid ADLER32');
|
||||||
if (HashCalculator.crc16(TEST_DATA).toString(16) !== '96e4')
|
if (HashCalculator.crc16(TEST_DATA) !== 0x96e4)
|
||||||
throw new Error('invalid SHA1');
|
throw new Error('invalid SHA1');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -134,11 +134,9 @@ const MODIFIED_TEST_DATA = (new Uint8Array([
|
||||||
const patchedFile = RomPatcher.applyPatch(originalFile, patch, { requireValidation: true });
|
const patchedFile = RomPatcher.applyPatch(originalFile, patch, { requireValidation: true });
|
||||||
|
|
||||||
if (patchFormat === 'bps') {
|
if (patchFormat === 'bps') {
|
||||||
const patchFile = patch.export();
|
if (patch.patchChecksum !== patch.calculateFileChecksum())
|
||||||
const patchChecksum = patchFile.hashCRC32(0, patchFile.fileSize - 4);
|
|
||||||
if (patch.patchChecksum !== patchChecksum)
|
|
||||||
throw new Error('invalid patch checksum');
|
throw new Error('invalid patch checksum');
|
||||||
else if (patchFile.hashCRC32() !== 0x2144df1c)
|
else if (patch.export().hashCRC32() !== 0x2144df1c)
|
||||||
throw new Error('invalid BPS crc32');
|
throw new Error('invalid BPS crc32');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue