all repos — mgba @ 1fe5a7847a0609a4f281d76a77f23c145874ac93

mGBA Game Boy Advance Emulator

Util: Perform checksum validation on BPS patches
Jeffrey Pfau jeffrey@endrift.com
Sun, 11 Jan 2015 06:22:37 -0800
commit

1fe5a7847a0609a4f281d76a77f23c145874ac93

parent

8c2d3e5d598cd50433e8869c09a54f974af22ab3

1 files changed, 19 insertions(+), 1 deletions(-)

jump to
M src/util/patch-ups.csrc/util/patch-ups.c

@@ -110,7 +110,18 @@ return true;

} bool _BPSApplyPatch(struct Patch* patch, void* in, size_t inSize, void* out, size_t outSize) { - // TODO: Input checksum + patch->vf->seek(patch->vf, IN_CHECKSUM, SEEK_END); + uint32_t expectedInChecksum; + uint32_t expectedOutChecksum; + patch->vf->read(patch->vf, &expectedInChecksum, sizeof(expectedInChecksum)); + patch->vf->read(patch->vf, &expectedOutChecksum, sizeof(expectedOutChecksum)); + + uint32_t inputChecksum = doCrc32(in, inSize); + uint32_t outputChecksum = 0; + + if (inputChecksum != expectedInChecksum) { + return false; + } ssize_t filesize = patch->vf->size(patch->vf); patch->vf->seek(patch->vf, 4, SEEK_SET);

@@ -137,6 +148,7 @@ switch (command & 0x3) {

case 0x0: // SourceRead memmove(&writeBuffer[writeLocation], &readBuffer[writeLocation], length); + outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; break; case 0x1:

@@ -144,6 +156,7 @@ // TargetRead

if (patch->vf->read(patch->vf, &writeBuffer[writeLocation], length) != length) { return false; } + outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; break; case 0x2:

@@ -158,6 +171,7 @@ if (readSourceLocation < 0 || readSourceLocation > inSize) {

return false; } memmove(&writeBuffer[writeLocation], &readBuffer[readSourceLocation], length); + outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; readSourceLocation += length; break;

@@ -178,8 +192,12 @@ writeBuffer[writeLocation] = writeBuffer[readTargetLocation];

++writeLocation; ++readTargetLocation; } + outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation - length], length); break; } + } + if (expectedOutChecksum != outputChecksum) { + return false; } return true; }