diff --git a/CHANGES b/CHANGES index bca70f25e..31feb2cd9 100644 --- a/CHANGES +++ b/CHANGES @@ -45,6 +45,7 @@ Misc: - 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: diff --git a/CMakeLists.txt b/CMakeLists.txt index 527dacdac..cc87c268d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -500,6 +500,14 @@ if(USE_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) diff --git a/include/mgba-util/crc32.h b/include/mgba-util/crc32.h index 225998bb9..ba5e694fd 100644 --- a/include/mgba-util/crc32.h +++ b/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 +#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 diff --git a/src/util/crc32.c b/src/util/crc32.c index 21a4354d4..24f611111 100644 --- a/src/util/crc32.c +++ b/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 @@ static uint32_t crc32Table[] = { 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 @@ uint32_t updateCrc32(uint32_t crc, const void* buf, size_t size) { 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 @@ uint32_t fileCrc32(struct VFile* vf, size_t endOffset) { } blocksize = vf->read(vf, buffer, toRead); alreadyRead += blocksize; - crc = updateCrc32(crc, buffer, blocksize); + crc = crc32(crc, buffer, blocksize); if (blocksize < toRead) { return 0; } diff --git a/src/util/patch-ups.c b/src/util/patch-ups.c index 5f24311ef..ea1d531eb 100644 --- a/src/util/patch-ups.c +++ b/src/util/patch-ups.c @@ -154,7 +154,7 @@ bool _BPSApplyPatch(struct Patch* patch, const void* in, size_t inSize, void* ou 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 @@ bool _BPSApplyPatch(struct Patch* patch, const void* in, size_t inSize, void* ou 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 @@ bool _BPSApplyPatch(struct Patch* patch, const void* in, size_t inSize, void* ou 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 @@ bool _BPSApplyPatch(struct Patch* patch, const void* in, size_t inSize, void* ou ++writeLocation; ++readTargetLocation; } - outputChecksum = updateCrc32(outputChecksum, &writeBuffer[writeLocation - length], length); + outputChecksum = crc32(outputChecksum, &writeBuffer[writeLocation - length], length); break; } }