Util: Don't build crc32 if the function already exists

This commit is contained in:
Vicki Pfau 2017-09-09 10:51:40 -07:00
parent 076ec733fd
commit bd4dd8de5c
5 changed files with 27 additions and 9 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}
}