Util: Don't build crc32 if the function already exists
Vicki Pfau vi@endrift.com
Sat, 09 Sep 2017 10:51:40 -0700
5 files changed,
27 insertions(+),
9 deletions(-)
M
CHANGES
→
CHANGES
@@ -45,6 +45,7 @@ - Qt: Redo GameController into multiple classes
- SDL: Fix 2.0.5 build on macOS under some circumstances - Test: Restructure test suite into multiple executables - Python: Integrate tests from cinema test suite + - Util: Don't build crc32 if the function already exists 0.6.0: (2017-07-16) Features:
M
CMakeLists.txt
→
CMakeLists.txt
@@ -500,6 +500,14 @@ list(APPEND FEATURES ZLIB)
include_directories(AFTER ${ZLIB_INCLUDE_DIRS}) list(APPEND DEPENDENCY_LIB ${ZLIB_LIBRARIES}) set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},zlib1g") + set(HAVE_CRC32 ON) +else() + # zlib pulls in crc32 + check_function_exists(crc32 HAVE_CRC32) +endif() + +if(HAVE_CRC32) + list(APPEND FUNCTION_DEFINES HAVE_CRC32) endif() if(WANT_PNG AND USE_ZLIB AND NOT USE_PNG)
M
include/mgba-util/crc32.h
→
include/mgba-util/crc32.h
@@ -12,8 +12,13 @@ CXX_GUARD_START
struct VFile; +#ifndef HAVE_CRC32 +uint32_t crc32(uint32_t crc, const void* buf, size_t size); +#else +#include <zlib.h> +#endif + uint32_t doCrc32(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); CXX_GUARD_END
M
src/util/crc32.c
→
src/util/crc32.c
@@ -48,6 +48,7 @@ enum {
BUFFER_SIZE = 1024 }; +#ifndef HAVE_CRC32 static uint32_t crc32Table[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,@@ -93,12 +94,14 @@ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; +#endif uint32_t doCrc32(const void* buf, size_t size) { - return updateCrc32(0, buf, size); + return crc32(0, buf, size); } -uint32_t updateCrc32(uint32_t crc, const void* buf, size_t size) { +#ifndef HAVE_CRC32 +uint32_t crc32(uint32_t crc, const void* buf, size_t size) { const uint8_t* p = buf; crc = ~crc;@@ -108,9 +111,10 @@ }
return ~crc; } +#endif uint32_t fileCrc32(struct VFile* vf, size_t endOffset) { - char buffer[BUFFER_SIZE]; + uint8_t buffer[BUFFER_SIZE]; size_t blocksize; size_t alreadyRead = 0; if (vf->seek(vf, 0, SEEK_SET) < 0) {@@ -124,7 +128,7 @@ toRead = endOffset - alreadyRead;
} blocksize = vf->read(vf, buffer, toRead); alreadyRead += blocksize; - crc = updateCrc32(crc, buffer, blocksize); + crc = crc32(crc, buffer, blocksize); if (blocksize < toRead) { return 0; }
M
src/util/patch-ups.c
→
src/util/patch-ups.c
@@ -154,7 +154,7 @@ switch (command & 0x3) {
case 0x0: // SourceRead memmove(&writeBuffer[writeLocation], &readBuffer[writeLocation], length); - outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); + outputChecksum = crc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; break; case 0x1:@@ -162,7 +162,7 @@ // TargetRead
if (patch->vf->read(patch->vf, &writeBuffer[writeLocation], length) != (ssize_t) length) { return false; } - outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); + outputChecksum = crc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; break; case 0x2:@@ -177,7 +177,7 @@ if (readSourceLocation < 0 || readSourceLocation > (ssize_t) inSize) {
return false; } memmove(&writeBuffer[writeLocation], &readBuffer[readSourceLocation], length); - outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation], length); + outputChecksum = crc32(outputChecksum, &writeBuffer[writeLocation], length); writeLocation += length; readSourceLocation += length; break;@@ -198,7 +198,7 @@ writeBuffer[writeLocation] = writeBuffer[readTargetLocation];
++writeLocation; ++readTargetLocation; } - outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation - length], length); + outputChecksum = crc32(outputChecksum, &writeBuffer[writeLocation - length], length); break; } }