diff --git a/CMakeLists.txt b/CMakeLists.txt index 886f89e..17feb2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,12 @@ target_include_directories(${PACKAGE} $ ) +include(TestBigEndian) +if(HAVE_BYTE_ORDER_BIG_ENDIAN) + # Define SHP_BIG_ENDIAN if the system is big-endian + add_definitions(-DSHP_BIG_ENDIAN=1) +endif() + if(MSVC) target_compile_options(${PACKAGE} PRIVATE /W4) else() @@ -292,4 +298,4 @@ endif() include(cmake/contrib.cmake) -add_subdirectory (cmake) +add_subdirectory(cmake) diff --git a/configure.ac b/configure.ac index 0cef3a0..466f87b 100644 --- a/configure.ac +++ b/configure.ac @@ -9,6 +9,12 @@ AC_INIT(shapelib, shapelib_version_major.shapelib_version_minor.shapelib_version AC_CONFIG_MACRO_DIR(m4) AC_CONFIG_SRCDIR(shapefil.h) +AC_C_BIGENDIAN +if test "x$ac_cv_c_bigendian" = "xyes"; then + # Define SHP_BIG_ENDIAN if the system is big-endian + AC_DEFINE([SHP_BIG_ENDIAN], [1], [Define if the system is big-endian]) +fi + AM_INIT_AUTOMAKE([foreign -Wall -Wextra]) AM_SILENT_RULES([yes]) diff --git a/sbnsearch.c b/sbnsearch.c index 9c4c83d..9f1cd15 100644 --- a/sbnsearch.c +++ b/sbnsearch.c @@ -136,18 +136,6 @@ static void SwapWord(int length, void *wordP) SBNSearchHandle SBNOpenDiskTree(const char *pszSBNFilename, const SAHooks *psHooks) { - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this machine. */ - /* -------------------------------------------------------------------- */ - bool _bBigEndian; - { - int i = 1; - if (*REINTERPRET_CAST(unsigned char *, &i) == 1) - _bBigEndian = false; - else - _bBigEndian = true; - } - /* -------------------------------------------------------------------- */ /* Initialize the handle structure. */ /* -------------------------------------------------------------------- */ @@ -191,13 +179,12 @@ SBNSearchHandle SBNOpenDiskTree(const char *pszSBNFilename, memcpy(&hSBN->dfMaxX, abyHeader + 48, 8); memcpy(&hSBN->dfMaxY, abyHeader + 56, 8); - if (!_bBigEndian) - { - SwapWord(8, &hSBN->dfMinX); - SwapWord(8, &hSBN->dfMinY); - SwapWord(8, &hSBN->dfMaxX); - SwapWord(8, &hSBN->dfMaxY); - } +#if !defined(SHP_BIG_ENDIAN) + SwapWord(8, &hSBN->dfMinX); + SwapWord(8, &hSBN->dfMinY); + SwapWord(8, &hSBN->dfMaxX); + SwapWord(8, &hSBN->dfMaxY); +#endif if (hSBN->dfMinX > hSBN->dfMaxX || hSBN->dfMinY > hSBN->dfMaxY) { diff --git a/shapefil.h b/shapefil.h index 4844b7f..d6fdfe8 100644 --- a/shapefil.h +++ b/shapefil.h @@ -22,6 +22,29 @@ #include "cpl_conv.h" #endif +#if !defined(SHP_BIG_ENDIAN) +#if defined(CPL_MSB) +#define SHP_BIG_ENDIAN 1 +#elif (defined(__GNUC__) && __GNUC__ >= 5) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && \ + __GNUC_MINOR__ >= 6) +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define SHP_BIG_ENDIAN 1 +#endif +#elif defined(__GLIBC__) +#if __BYTE_ORDER == __BIG_ENDIAN +#define SHP_BIG_ENDIAN 1 +#endif +#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +#define SHP_BIG_ENDIAN 1 +#elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +#elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || \ + defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || \ + defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +#define SHP_BIG_ENDIAN 1 +#endif +#endif + #ifdef __cplusplus extern "C" { diff --git a/shpopen.c b/shpopen.c index f91397e..0acb9ca 100644 --- a/shpopen.c +++ b/shpopen.c @@ -46,19 +46,6 @@ #endif #endif -#ifndef bBigEndian -#if defined(CPL_LSB) -#define bBigEndian false -#elif defined(CPL_MSB) -#define bBigEndian true -#else -#ifndef static_var_bBigEndian_defined -#define static_var_bBigEndian_defined -static bool bBigEndian = false; -#endif -#endif -#endif - #ifdef __cplusplus #define STATIC_CAST(type, x) static_cast(x) #define SHPLIB_NULLPTR nullptr @@ -112,58 +99,67 @@ void SHPAPI_CALL SHPWriteHeader(SHPHandle psSHP) uint32_t i32 = psSHP->nFileSize / 2; /* file size */ ByteCopy(&i32, abyHeader + 24, 4); - if (!bBigEndian) - SwapWord(4, abyHeader + 24); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 24); +#endif i32 = 1000; /* version */ ByteCopy(&i32, abyHeader + 28, 4); - if (bBigEndian) - SwapWord(4, abyHeader + 28); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 28); +#endif i32 = psSHP->nShapeType; /* shape type */ ByteCopy(&i32, abyHeader + 32, 4); - if (bBigEndian) - SwapWord(4, abyHeader + 32); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 32); +#endif double dValue = psSHP->adBoundsMin[0]; /* set bounds */ ByteCopy(&dValue, abyHeader + 36, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 36); - +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 36); +#endif dValue = psSHP->adBoundsMin[1]; ByteCopy(&dValue, abyHeader + 44, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 44); - +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 44); +#endif dValue = psSHP->adBoundsMax[0]; ByteCopy(&dValue, abyHeader + 52, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 52); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 52); +#endif dValue = psSHP->adBoundsMax[1]; ByteCopy(&dValue, abyHeader + 60, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 60); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 60); +#endif dValue = psSHP->adBoundsMin[2]; /* z */ ByteCopy(&dValue, abyHeader + 68, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 68); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 68); +#endif dValue = psSHP->adBoundsMax[2]; ByteCopy(&dValue, abyHeader + 76, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 76); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 76); +#endif dValue = psSHP->adBoundsMin[3]; /* m */ ByteCopy(&dValue, abyHeader + 84, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 84); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 84); +#endif dValue = psSHP->adBoundsMax[3]; ByteCopy(&dValue, abyHeader + 92, 8); - if (bBigEndian) - SwapWord(8, abyHeader + 92); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, abyHeader + 92); +#endif /* -------------------------------------------------------------------- */ /* Write .shp file header. */ @@ -185,8 +181,9 @@ void SHPAPI_CALL SHPWriteHeader(SHPHandle psSHP) /* -------------------------------------------------------------------- */ i32 = (psSHP->nRecords * 2 * sizeof(uint32_t) + 100) / 2; /* file size */ ByteCopy(&i32, abyHeader + 24, 4); - if (!bBigEndian) - SwapWord(4, abyHeader + 24); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 24); +#endif if (psSHP->sHooks.FSeek(psSHP->fpSHX, 0, 0) != 0 || psSHP->sHooks.FWrite(abyHeader, 100, 1, psSHP->fpSHX) != 1) @@ -216,10 +213,10 @@ void SHPAPI_CALL SHPWriteHeader(SHPHandle psSHP) { panSHX[i * 2] = psSHP->panRecOffset[i] / 2; panSHX[i * 2 + 1] = psSHP->panRecSize[i] / 2; - if (!bBigEndian) - SwapWord(4, panSHX + i * 2); - if (!bBigEndian) - SwapWord(4, panSHX + i * 2 + 1); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, panSHX + i * 2); + SwapWord(4, panSHX + i * 2 + 1); +#endif } if (STATIC_CAST(int, psSHP->sHooks.FWrite(panSHX, sizeof(uint32_t) * 2, @@ -301,19 +298,6 @@ SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, pszAccess = "rb"; } -/* -------------------------------------------------------------------- */ -/* Establish the byte order on this machine. */ -/* -------------------------------------------------------------------- */ -#if !defined(bBigEndian) - { - int i = 1; - if (*((unsigned char *)&i) == 1) - bBigEndian = false; - else - bBigEndian = true; - } -#endif - /* -------------------------------------------------------------------- */ /* Initialize the info structure. */ /* -------------------------------------------------------------------- */ @@ -468,43 +452,51 @@ SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, /* -------------------------------------------------------------------- */ double dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 36); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 36); +#endif memcpy(&dValue, pabyBuf + 36, 8); psSHP->adBoundsMin[0] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 44); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 44); +#endif memcpy(&dValue, pabyBuf + 44, 8); psSHP->adBoundsMin[1] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 52); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 52); +#endif memcpy(&dValue, pabyBuf + 52, 8); psSHP->adBoundsMax[0] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 60); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 60); +#endif memcpy(&dValue, pabyBuf + 60, 8); psSHP->adBoundsMax[1] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 68); /* z */ +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 68); /* z */ +#endif memcpy(&dValue, pabyBuf + 68, 8); psSHP->adBoundsMin[2] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 76); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 76); +#endif memcpy(&dValue, pabyBuf + 76, 8); psSHP->adBoundsMax[2] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 84); /* z */ +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 84); /* z */ +#endif memcpy(&dValue, pabyBuf + 84, 8); psSHP->adBoundsMin[3] = dValue; - if (bBigEndian) - SwapWord(8, pabyBuf + 92); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyBuf + 92); +#endif memcpy(&dValue, pabyBuf + 92, 8); psSHP->adBoundsMax[3] = dValue; @@ -596,13 +588,15 @@ SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, { unsigned int nOffset; memcpy(&nOffset, pabyBuf + i * 8, 4); - if (!bBigEndian) - SwapWord(4, &nOffset); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nOffset); +#endif unsigned int nLength; memcpy(&nLength, pabyBuf + i * 8 + 4, 4); - if (!bBigEndian) - SwapWord(4, &nLength); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nLength); +#endif if (nOffset > STATIC_CAST(unsigned int, INT_MAX)) { @@ -683,19 +677,6 @@ int SHPAPI_CALL SHPRestoreSHX(const char *pszLayer, const char *pszAccess, pszAccess = "rb"; } -/* -------------------------------------------------------------------- */ -/* Establish the byte order on this machine. */ -/* -------------------------------------------------------------------- */ -#if !defined(bBigEndian) - { - int i = 1; - if (*((unsigned char *)&i) == 1) - bBigEndian = false; - else - bBigEndian = true; - } -#endif - /* -------------------------------------------------------------------- */ /* Open the .shp file. Note that files pulled from */ /* a PC to Unix with upper case filenames won't work! */ @@ -800,16 +781,18 @@ int SHPAPI_CALL SHPRestoreSHX(const char *pszLayer, const char *pszAccess, char abyReadRecord[8]; unsigned int nRecordOffsetBE = nRecordOffset; - if (!bBigEndian) - SwapWord(4, &nRecordOffsetBE); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nRecordOffsetBE); +#endif memcpy(abyReadRecord, &nRecordOffsetBE, 4); memcpy(abyReadRecord + 4, &nRecordLength, 4); - if (!bBigEndian) - SwapWord(4, &nRecordLength); - - if (bBigEndian) - SwapWord(4, &nSHPType); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nRecordLength); +#endif +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &nSHPType); +#endif // Sanity check on record length if (nRecordLength < 1 || @@ -880,8 +863,10 @@ int SHPAPI_CALL SHPRestoreSHX(const char *pszLayer, const char *pszAccess, } nRealSHXContentSize /= 2; // Bytes counted -> WORDs - if (!bBigEndian) - SwapWord(4, &nRealSHXContentSize); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nRealSHXContentSize); +#endif + psHooks->FSeek(fpSHX, 24, 0); psHooks->FWrite(&nRealSHXContentSize, 4, 1, fpSHX); @@ -1014,19 +999,6 @@ SHPHandle SHPAPI_CALL SHPCreate(const char *pszLayer, int nShapeType) SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszLayer, int nShapeType, const SAHooks *psHooks) { -/* -------------------------------------------------------------------- */ -/* Establish the byte order on this system. */ -/* -------------------------------------------------------------------- */ -#if !defined(bBigEndian) - { - int i = 1; - if (*((unsigned char *)&i) == 1) - bBigEndian = false; - else - bBigEndian = true; - } -#endif - /* -------------------------------------------------------------------- */ /* Open the two files so we can write their headers. */ /* -------------------------------------------------------------------- */ @@ -1074,18 +1046,21 @@ SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszLayer, int nShapeType, uint32_t i32 = 50; /* file size */ ByteCopy(&i32, abyHeader + 24, 4); - if (!bBigEndian) - SwapWord(4, abyHeader + 24); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 24); +#endif i32 = 1000; /* version */ ByteCopy(&i32, abyHeader + 28, 4); - if (bBigEndian) - SwapWord(4, abyHeader + 28); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 28); +#endif i32 = nShapeType; /* shape type */ ByteCopy(&i32, abyHeader + 32, 4); - if (bBigEndian) - SwapWord(4, abyHeader + 32); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 32); +#endif double dValue = 0.0; /* set bounds */ ByteCopy(&dValue, abyHeader + 36, 8); @@ -1116,8 +1091,9 @@ SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszLayer, int nShapeType, /* -------------------------------------------------------------------- */ i32 = 50; /* file size */ ByteCopy(&i32, abyHeader + 24, 4); - if (!bBigEndian) - SwapWord(4, abyHeader + 24); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, abyHeader + 24); +#endif if (psHooks->FWrite(abyHeader, 100, 1, fpSHX) != 1) { @@ -1179,13 +1155,12 @@ static void _SHPSetBounds(unsigned char *pabyRec, const SHPObject *psShape) ByteCopy(&(psShape->dfXMax), pabyRec + 16, 8); ByteCopy(&(psShape->dfYMax), pabyRec + 24, 8); - if (bBigEndian) - { - SwapWord(8, pabyRec + 0); - SwapWord(8, pabyRec + 8); - SwapWord(8, pabyRec + 16); - SwapWord(8, pabyRec + 24); - } +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + 0); + SwapWord(8, pabyRec + 8); + SwapWord(8, pabyRec + 16); + SwapWord(8, pabyRec + 24); +#endif } /************************************************************************/ @@ -1464,10 +1439,10 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, _SHPSetBounds(pabyRec + 12, psObject); - if (bBigEndian) - SwapWord(4, &nPoints); - if (bBigEndian) - SwapWord(4, &nParts); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &nPoints); + SwapWord(4, &nParts); +#endif ByteCopy(&nPoints, pabyRec + 40 + 8, 4); ByteCopy(&nParts, pabyRec + 36 + 8, 4); @@ -1481,8 +1456,9 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, 4 * psObject->nParts); for (int i = 0; i < psObject->nParts; i++) { - if (bBigEndian) - SwapWord(4, pabyRec + 44 + 8 + 4 * i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, pabyRec + 44 + 8 + 4 * i); +#endif nRecordSize += 4; } @@ -1495,8 +1471,9 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, 4 * psObject->nParts); for (int i = 0; i < psObject->nParts; i++) { - if (bBigEndian) - SwapWord(4, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, pabyRec + nRecordSize); +#endif nRecordSize += 4; } } @@ -1509,11 +1486,10 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, ByteCopy(psObject->padfX + i, pabyRec + nRecordSize, 8); ByteCopy(psObject->padfY + i, pabyRec + nRecordSize + 8, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); - - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize + 8); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); + SwapWord(8, pabyRec + nRecordSize + 8); +#endif nRecordSize += 2 * 8; } @@ -1526,20 +1502,23 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, psObject->nSHPType == SHPT_MULTIPATCH) { ByteCopy(&(psObject->dfZMin), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; ByteCopy(&(psObject->dfZMax), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; for (int i = 0; i < psObject->nVertices; i++) { ByteCopy(psObject->padfZ + i, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } } @@ -1557,20 +1536,23 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, psObject->nSHPType == SHPT_ARCZ)) { ByteCopy(&(psObject->dfMMin), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; ByteCopy(&(psObject->dfMMax), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; for (int i = 0; i < psObject->nVertices; i++) { ByteCopy(psObject->padfM + i, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } } @@ -1587,8 +1569,9 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, _SHPSetBounds(pabyRec + 12, psObject); - if (bBigEndian) - SwapWord(4, &nPoints); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &nPoints); +#endif ByteCopy(&nPoints, pabyRec + 44, 4); for (int i = 0; i < psObject->nVertices; i++) @@ -1596,10 +1579,10 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, ByteCopy(psObject->padfX + i, pabyRec + 48 + i * 16, 8); ByteCopy(psObject->padfY + i, pabyRec + 48 + i * 16 + 8, 8); - if (bBigEndian) - SwapWord(8, pabyRec + 48 + i * 16); - if (bBigEndian) - SwapWord(8, pabyRec + 48 + i * 16 + 8); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + 48 + i * 16); + SwapWord(8, pabyRec + 48 + i * 16 + 8); +#endif } nRecordSize = 48 + 16 * psObject->nVertices; @@ -1607,20 +1590,23 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, if (psObject->nSHPType == SHPT_MULTIPOINTZ) { ByteCopy(&(psObject->dfZMin), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; ByteCopy(&(psObject->dfZMax), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; for (int i = 0; i < psObject->nVertices; i++) { ByteCopy(psObject->padfZ + i, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } } @@ -1630,20 +1616,23 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, psObject->nSHPType == SHPT_MULTIPOINTM)) { ByteCopy(&(psObject->dfMMin), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; ByteCopy(&(psObject->dfMMax), pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; for (int i = 0; i < psObject->nVertices; i++) { ByteCopy(psObject->padfM + i, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } } @@ -1659,18 +1648,19 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, ByteCopy(psObject->padfX, pabyRec + 12, 8); ByteCopy(psObject->padfY, pabyRec + 20, 8); - if (bBigEndian) - SwapWord(8, pabyRec + 12); - if (bBigEndian) - SwapWord(8, pabyRec + 20); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + 12); + SwapWord(8, pabyRec + 20); +#endif nRecordSize = 28; if (psObject->nSHPType == SHPT_POINTZ) { ByteCopy(psObject->padfZ, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } @@ -1678,8 +1668,9 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, psObject->nSHPType == SHPT_POINTM)) { ByteCopy(psObject->padfM, pabyRec + nRecordSize, 8); - if (bBigEndian) - SwapWord(8, pabyRec + nRecordSize); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, pabyRec + nRecordSize); +#endif nRecordSize += 8; } } @@ -1743,18 +1734,21 @@ int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, /* -------------------------------------------------------------------- */ uint32_t i32 = (nShapeId < 0) ? psSHP->nRecords + 1 : nShapeId + 1; /* record # */ - if (!bBigEndian) - SwapWord(4, &i32); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &i32); +#endif ByteCopy(&i32, pabyRec, 4); i32 = (nRecordSize - 8) / 2; /* record size */ - if (!bBigEndian) - SwapWord(4, &i32); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &i32); +#endif ByteCopy(&i32, pabyRec + 4, 4); i32 = psObject->nSHPType; /* shape type */ - if (bBigEndian) - SwapWord(4, &i32); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &i32); +#endif ByteCopy(&i32, pabyRec + 8, 4); /* -------------------------------------------------------------------- */ @@ -1946,10 +1940,10 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) psSHP->sHooks.Error(str); return SHPLIB_NULLPTR; } - if (!bBigEndian) - SwapWord(4, &nOffset); - if (!bBigEndian) - SwapWord(4, &nLength); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &nOffset); + SwapWord(4, &nLength); +#endif if (nOffset > STATIC_CAST(unsigned int, INT_MAX)) { @@ -2081,8 +2075,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) /* Do a sanity check */ int nSHPContentLength; memcpy(&nSHPContentLength, psSHP->pabyRec + 4, 4); - if (!bBigEndian) - SwapWord(4, &(nSHPContentLength)); +#if !defined(SHP_BIG_ENDIAN) + SwapWord(4, &(nSHPContentLength)); +#endif if (nSHPContentLength < 0 || nSHPContentLength > INT_MAX / 2 - 4 || 2 * nSHPContentLength + 8 != nBytesRead) { @@ -2127,8 +2122,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) int nSHPType; memcpy(&nSHPType, psSHP->pabyRec + 8, 4); - if (bBigEndian) - SwapWord(4, &(nSHPType)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &(nSHPType)); +#endif /* -------------------------------------------------------------------- */ /* Allocate and minimally initialize the object. */ @@ -2182,14 +2178,12 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8); memcpy(&(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfXMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfYMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfXMax)); - if (bBigEndian) - SwapWord(8, &(psShape->dfYMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfXMin)); + SwapWord(8, &(psShape->dfYMin)); + SwapWord(8, &(psShape->dfXMax)); + SwapWord(8, &(psShape->dfYMax)); +#endif /* -------------------------------------------------------------------- */ /* Extract part/point count, and build vertex and part arrays */ @@ -2200,10 +2194,10 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) uint32_t nParts; memcpy(&nParts, psSHP->pabyRec + 36 + 8, 4); - if (bBigEndian) - SwapWord(4, &nPoints); - if (bBigEndian) - SwapWord(4, &nParts); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &nPoints); + SwapWord(4, &nParts); +#endif /* nPoints and nParts are unsigned */ if (/* nPoints < 0 || nParts < 0 || */ @@ -2301,8 +2295,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(psShape->panPartStart, psSHP->pabyRec + 44 + 8, 4 * nParts); for (int i = 0; STATIC_CAST(uint32_t, i) < nParts; i++) { - if (bBigEndian) - SwapWord(4, psShape->panPartStart + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, psShape->panPartStart + i); +#endif /* We check that the offset is inside the vertex array */ if (psShape->panPartStart[i] < 0 || @@ -2347,8 +2342,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(psShape->panPartType, psSHP->pabyRec + nOffset, 4 * nParts); for (int i = 0; STATIC_CAST(uint32_t, i) < nParts; i++) { - if (bBigEndian) - SwapWord(4, psShape->panPartType + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, psShape->panPartType + i); +#endif } nOffset += 4 * nParts; @@ -2364,10 +2360,10 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(psShape->padfY + i, psSHP->pabyRec + nOffset + i * 16 + 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfX + i); - if (bBigEndian) - SwapWord(8, psShape->padfY + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfX + i); + SwapWord(8, psShape->padfY + i); +#endif } nOffset += 16 * nPoints; @@ -2382,17 +2378,18 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfZMin), psSHP->pabyRec + nOffset, 8); memcpy(&(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfZMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfZMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfZMin)); + SwapWord(8, &(psShape->dfZMax)); +#endif for (int i = 0; STATIC_CAST(uint32_t, i) < nPoints; i++) { memcpy(psShape->padfZ + i, psSHP->pabyRec + nOffset + 16 + i * 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfZ + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfZ + i); +#endif } nOffset += 16 + 8 * nPoints; @@ -2413,17 +2410,18 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfMMin), psSHP->pabyRec + nOffset, 8); memcpy(&(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfMMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfMMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfMMin)); + SwapWord(8, &(psShape->dfMMax)); +#endif for (int i = 0; STATIC_CAST(uint32_t, i) < nPoints; i++) { memcpy(psShape->padfM + i, psSHP->pabyRec + nOffset + 16 + i * 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfM + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfM + i); +#endif } psShape->bMeasureIsUsed = TRUE; } @@ -2454,8 +2452,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) uint32_t nPoints; memcpy(&nPoints, psSHP->pabyRec + 44, 4); - if (bBigEndian) - SwapWord(4, &nPoints); +#if defined(SHP_BIG_ENDIAN) + SwapWord(4, &nPoints); +#endif /* nPoints is unsigned */ if (/* nPoints < 0 || */ nPoints > 50 * 1000 * 1000) @@ -2531,10 +2530,10 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(psShape->padfX + i, psSHP->pabyRec + 48 + 16 * i, 8); memcpy(psShape->padfY + i, psSHP->pabyRec + 48 + 16 * i + 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfX + i); - if (bBigEndian) - SwapWord(8, psShape->padfY + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfX + i); + SwapWord(8, psShape->padfY + i); +#endif } int nOffset = 48 + 16 * nPoints; @@ -2547,14 +2546,12 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8); memcpy(&(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfXMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfYMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfXMax)); - if (bBigEndian) - SwapWord(8, &(psShape->dfYMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfXMin)); + SwapWord(8, &(psShape->dfYMin)); + SwapWord(8, &(psShape->dfXMax)); + SwapWord(8, &(psShape->dfYMax)); +#endif /* -------------------------------------------------------------------- */ /* If we have a Z coordinate, collect that now. */ @@ -2564,17 +2561,18 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfZMin), psSHP->pabyRec + nOffset, 8); memcpy(&(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfZMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfZMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfZMin)); + SwapWord(8, &(psShape->dfZMax)); +#endif for (int i = 0; STATIC_CAST(uint32_t, i) < nPoints; i++) { memcpy(psShape->padfZ + i, psSHP->pabyRec + nOffset + 16 + i * 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfZ + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfZ + i); +#endif } nOffset += 16 + 8 * nPoints; @@ -2593,17 +2591,18 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(&(psShape->dfMMin), psSHP->pabyRec + nOffset, 8); memcpy(&(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8); - if (bBigEndian) - SwapWord(8, &(psShape->dfMMin)); - if (bBigEndian) - SwapWord(8, &(psShape->dfMMax)); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, &(psShape->dfMMin)); + SwapWord(8, &(psShape->dfMMax)); +#endif for (int i = 0; STATIC_CAST(uint32_t, i) < nPoints; i++) { memcpy(psShape->padfM + i, psSHP->pabyRec + nOffset + 16 + i * 8, 8); - if (bBigEndian) - SwapWord(8, psShape->padfM + i); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfM + i); +#endif } psShape->bMeasureIsUsed = TRUE; } @@ -2650,10 +2649,10 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) memcpy(psShape->padfX, psSHP->pabyRec + 12, 8); memcpy(psShape->padfY, psSHP->pabyRec + 20, 8); - if (bBigEndian) - SwapWord(8, psShape->padfX); - if (bBigEndian) - SwapWord(8, psShape->padfY); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfX); + SwapWord(8, psShape->padfY); +#endif int nOffset = 20 + 8; @@ -2664,8 +2663,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) { memcpy(psShape->padfZ, psSHP->pabyRec + nOffset, 8); - if (bBigEndian) - SwapWord(8, psShape->padfZ); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfZ); +#endif nOffset += 8; } @@ -2680,8 +2680,9 @@ SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle psSHP, int hEntity) { memcpy(psShape->padfM, psSHP->pabyRec + nOffset, 8); - if (bBigEndian) - SwapWord(8, psShape->padfM); +#if defined(SHP_BIG_ENDIAN) + SwapWord(8, psShape->padfM); +#endif psShape->bMeasureIsUsed = TRUE; } diff --git a/shptree.c b/shptree.c index 7d636fd..b10ba31 100644 --- a/shptree.c +++ b/shptree.c @@ -31,19 +31,6 @@ #define FALSE 0 #endif -#ifndef bBigEndian -#if defined(CPL_LSB) -#define bBigEndian false -#elif defined(CPL_MSB) -#define bBigEndian true -#else -#ifndef static_var_bBigEndian_defined -#define static_var_bBigEndian_defined -static bool bBigEndian = false; -#endif -#endif -#endif - /* -------------------------------------------------------------------- */ /* If the following is 0.5, nodes will be split in half. If it */ /* is 0.6 then each subnode will contain 60% of the parent */ @@ -964,19 +951,6 @@ int *SHPSearchDiskTreeEx(SHPTreeDiskHandle hDiskTree, double *padfBoundsMin, *pnShapeCount = 0; - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this machine. */ - /* -------------------------------------------------------------------- */ -#if !defined(bBigEndian) - { - int i = 1; - if (*REINTERPRET_CAST(unsigned char *, &i) == 1) - bBigEndian = false; - else - bBigEndian = true; - } -#endif - /* -------------------------------------------------------------------- */ /* Read the header. */ /* -------------------------------------------------------------------- */ @@ -986,11 +960,11 @@ int *SHPSearchDiskTreeEx(SHPTreeDiskHandle hDiskTree, double *padfBoundsMin, if (memcmp(abyBuf, "SQT", 3) != 0) return SHPLIB_NULLPTR; - bool bNeedSwap; - if ((abyBuf[3] == 2 && bBigEndian) || (abyBuf[3] == 1 && !bBigEndian)) - bNeedSwap = false; - else - bNeedSwap = true; +#if defined(SHP_BIG_ENDIAN) + bool bNeedSwap = abyBuf[3] != 2; +#else + bool bNeedSwap = abyBuf[3] != 1; +#endif /* -------------------------------------------------------------------- */ /* Search through root node and its descendants. */ @@ -1132,28 +1106,16 @@ int SHPWriteTreeLL(SHPTree *tree, const char *filename, const SAHooks *psHooks) return FALSE; } - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this machine. */ - /* -------------------------------------------------------------------- */ -#if !defined(bBigEndian) - { - int i = 1; - if (*REINTERPRET_CAST(unsigned char *, &i) == 1) - bBigEndian = false; - else - bBigEndian = true; - } -#endif - /* -------------------------------------------------------------------- */ /* Write the header. */ /* -------------------------------------------------------------------- */ memcpy(abyBuf + 0, signature, 3); - if (bBigEndian) - abyBuf[3] = 2; /* New MSB */ - else - abyBuf[3] = 1; /* New LSB */ +#if defined(SHP_BIG_ENDIAN) + abyBuf[3] = 2; /* New MSB */ +#else + abyBuf[3] = 1; /* New LSB */ +#endif abyBuf[4] = 1; /* version */ abyBuf[5] = 0; /* next 3 reserved */