all repos — mgba @ 381fc94d7397a324207a4a48195a6a29b9e5825e

mGBA Game Boy Advance Emulator

Move file CRC32 code into crc32.h
Jeffrey Pfau jeffrey@endrift.com
Sun, 20 Jul 2014 15:11:22 -0700
commit

381fc94d7397a324207a4a48195a6a29b9e5825e

parent

7a0f86ae991c24f29d656bb863da0704ad78a9be

3 files changed, 34 insertions(+), 20 deletions(-)

jump to
M src/util/crc32.csrc/util/crc32.c

@@ -42,6 +42,12 @@ */

#include "util/crc32.h" +#include "util/vfs.h" + +enum { + BUFFER_SIZE = 1024 +}; + static uint32_t crc32Table[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,

@@ -102,3 +108,26 @@ }

return ~crc; } + +uint32_t fileCrc32(struct VFile* vf, size_t endOffset) { + char buffer[BUFFER_SIZE]; + size_t blocksize; + size_t alreadyRead = 0; + if (vf->seek(vf, 0, SEEK_SET) < 0) { + return 0; + } + uint32_t crc = 0; + while (alreadyRead < endOffset) { + size_t toRead = sizeof(buffer); + if (toRead + alreadyRead > endOffset) { + toRead = endOffset - alreadyRead; + } + blocksize = vf->read(vf, buffer, toRead); + alreadyRead += blocksize; + crc = updateCrc32(crc, buffer, blocksize); + if (blocksize < toRead) { + return 0; + } + } + return crc; +}
M src/util/crc32.hsrc/util/crc32.h

@@ -4,7 +4,10 @@

#include <stdint.h> #include <string.h> +struct VFile; + uint32_t crc32(const void* buf, size_t size); uint32_t updateCrc32(uint32_t crc, const void* buf, size_t size); +uint32_t fileCrc32(struct VFile* file, size_t endOffset); #endif
M src/util/patch-ups.csrc/util/patch-ups.c

@@ -8,8 +8,6 @@ enum {

IN_CHECKSUM = -12, OUT_CHECKSUM = -8, PATCH_CHECKSUM = -4, - - BUFFER_SIZE = 1024 }; static size_t _UPSOutputSize(struct Patch* patch, size_t inSize);

@@ -19,7 +17,7 @@

bool loadPatchUPS(struct Patch* patch) { patch->vf->seek(patch->vf, 0, SEEK_SET); - char buffer[BUFFER_SIZE]; + char buffer[4]; if (patch->vf->read(patch->vf, buffer, 4) != 4) { return false; }

@@ -36,23 +34,7 @@ if (patch->vf->read(patch->vf, &goodCrc32, 4) != 4) {

return false; } - size_t blocksize; - size_t alreadyRead = 0; - patch->vf->seek(patch->vf, 0, SEEK_SET); - uint32_t crc = 0; - while (alreadyRead < filesize + PATCH_CHECKSUM) { - size_t toRead = sizeof(buffer); - if (toRead + alreadyRead > filesize + PATCH_CHECKSUM) { - toRead = filesize + PATCH_CHECKSUM - alreadyRead; - } - blocksize = patch->vf->read(patch->vf, buffer, toRead); - alreadyRead += blocksize; - crc = updateCrc32(crc, buffer, blocksize); - if (blocksize < toRead) { - return 0; - } - } - + uint32_t crc = fileCrc32(patch->vf, filesize + PATCH_CHECKSUM); if (crc != goodCrc32) { return false; }