Skip to content

Commit

Permalink
Merge pull request #94 from thbeu/update-swap
Browse files Browse the repository at this point in the history
Prefer builtin swap functions
  • Loading branch information
rouault authored Feb 6, 2024
2 parents 5c97568 + 6ed838d commit 0e46de8
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 265 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ set(lib_SRC
shptree.c
sbnsearch.c
shapefil.h
shapefil_private.h
shapelib.def
)

Expand All @@ -125,7 +126,7 @@ endif()
if(MSVC)
target_compile_options(${PACKAGE} PRIVATE /W4)
else()
target_compile_options(${PACKAGE} PRIVATE -Wall -Wextra -Wformat-signedness -Wno-unknown-warning-option -pedantic)
target_compile_options(${PACKAGE} PRIVATE -Wall -Wextra -Wformat-signedness -pedantic -Wno-unknown-warning-option)
endif()

if(WIN32 AND NOT CYGWIN)
Expand Down
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SUBDIRS = . contrib

ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = dist-zip
AUTOMAKE_OPTIONS = dist-zip

if PLATFORM_WIN32
no_undefined = -no-undefined
Expand Down Expand Up @@ -37,6 +37,7 @@ pkgconfig_DATA = shapelib.pc
lib_LTLIBRARIES = libshp.la
libshp_la_includedir = $(includedir)
libshp_la_include_HEADERS = shapefil.h
noinst_HEADERS = shapefil_private.h
libshp_la_SOURCES = shpopen.c dbfopen.c safileio.c shptree.c sbnsearch.c
libshp_la_LDFLAGS = -version-info $(SHAPELIB_SO_VERSION) $(no_undefined) $(LIBM)

Expand Down
14 changes: 1 addition & 13 deletions dbfopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************/

#include "shapefil.h"
#include "shapefil_private.h"

#include <math.h>
#include <stdbool.h>
Expand Down Expand Up @@ -68,18 +68,6 @@ CPL_INLINE static void CPL_IGNORE_RET_VAL_INT(CPL_UNUSED int unused)
#define CPL_IGNORE_RET_VAL_INT(x) x
#endif

#ifdef __cplusplus
#define STATIC_CAST(type, x) static_cast<type>(x)
#define REINTERPRET_CAST(type, x) reinterpret_cast<type>(x)
#define CONST_CAST(type, x) const_cast<type>(x)
#define SHPLIB_NULLPTR nullptr
#else
#define STATIC_CAST(type, x) ((type)(x))
#define REINTERPRET_CAST(type, x) ((type)(x))
#define CONST_CAST(type, x) ((type)(x))
#define SHPLIB_NULLPTR NULL
#endif

/************************************************************************/
/* DBFWriteHeader() */
/* */
Expand Down
46 changes: 7 additions & 39 deletions sbnsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************/

#include "shapefil.h"
#include "shapefil_private.h"

#include <assert.h>
#include <math.h>
Expand All @@ -33,18 +33,6 @@

#define CACHED_DEPTH_LIMIT 8

#ifdef __cplusplus
#define STATIC_CAST(type, x) static_cast<type>(x)
#define REINTERPRET_CAST(type, x) reinterpret_cast<type>(x)
#define CONST_CAST(type, x) const_cast<type>(x)
#define SHPLIB_NULLPTR nullptr
#else
#define STATIC_CAST(type, x) ((type)(x))
#define REINTERPRET_CAST(type, x) ((type)(x))
#define CONST_CAST(type, x) ((type)(x))
#define SHPLIB_NULLPTR NULL
#endif

#define READ_MSB_INT(ptr) \
STATIC_CAST(int, (((STATIC_CAST(unsigned, (ptr)[0])) << 24) | \
((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]))
Expand Down Expand Up @@ -109,26 +97,6 @@ typedef struct
#endif
} SearchStruct;

/************************************************************************/
/* SwapWord() */
/* */
/* Swap a 2, 4 or 8 byte word. */
/************************************************************************/

#ifndef SwapWord_defined
#define SwapWord_defined
static void SwapWord(int length, void *wordP)
{
for (int i = 0; i < length / 2; i++)
{
const unsigned char temp = STATIC_CAST(unsigned char *, wordP)[i];
STATIC_CAST(unsigned char *, wordP)
[i] = STATIC_CAST(unsigned char *, wordP)[length - i - 1];
STATIC_CAST(unsigned char *, wordP)[length - i - 1] = temp;
}
}
#endif

/************************************************************************/
/* SBNOpenDiskTree() */
/************************************************************************/
Expand Down Expand Up @@ -174,16 +142,16 @@ SBNSearchHandle SBNOpenDiskTree(const char *pszSBNFilename,
/* Read shapes bounding box. */
/* -------------------------------------------------------------------- */

#if !defined(SHP_BIG_ENDIAN)
SHP_SWAPDOUBLE_CPY(&hSBN->dfMinX, abyHeader + 32);
SHP_SWAPDOUBLE_CPY(&hSBN->dfMinY, abyHeader + 40);
SHP_SWAPDOUBLE_CPY(&hSBN->dfMaxX, abyHeader + 48);
SHP_SWAPDOUBLE_CPY(&hSBN->dfMaxY, abyHeader + 56);
#else
memcpy(&hSBN->dfMinX, abyHeader + 32, 8);
memcpy(&hSBN->dfMinY, abyHeader + 40, 8);
memcpy(&hSBN->dfMaxX, abyHeader + 48, 8);
memcpy(&hSBN->dfMaxY, abyHeader + 56, 8);

#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)
Expand Down
92 changes: 92 additions & 0 deletions shapefil_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef SHAPEFILE_PRIVATE_H_INCLUDED
#define SHAPEFILE_PRIVATE_H_INCLUDED

/******************************************************************************
*
* Project: Shapelib
* Purpose: Private include file for Shapelib.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2012-2016, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************
*
*/

#ifdef __cplusplus
#define STATIC_CAST(type, x) static_cast<type>(x)
#define REINTERPRET_CAST(type, x) reinterpret_cast<type>(x)
#define CONST_CAST(type, x) const_cast<type>(x)
#define SHPLIB_NULLPTR nullptr
#else
#define STATIC_CAST(type, x) ((type)(x))
#define REINTERPRET_CAST(type, x) ((type)(x))
#define CONST_CAST(type, x) ((type)(x))
#define SHPLIB_NULLPTR NULL
#endif

#include "shapefil.h"
#include <stdint.h>
#include <stdlib.h>

/************************************************************************/
/* Little endian <==> big endian byte swap macros. */
/************************************************************************/

#if (defined(__GNUC__) && __GNUC__ >= 5) || \
(defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && \
__GNUC_MINOR__ >= 8)
#define _SHP_SWAP32(x) \
STATIC_CAST(uint32_t, __builtin_bswap32(STATIC_CAST(uint32_t, x)))
#define _SHP_SWAP64(x) \
STATIC_CAST(uint64_t, __builtin_bswap64(STATIC_CAST(uint64_t, x)))
#elif defined(_MSC_VER)
#define _SHP_SWAP32(x) \
STATIC_CAST(uint32_t, _byteswap_ulong(STATIC_CAST(uint32_t, x)))
#define _SHP_SWAP64(x) \
STATIC_CAST(uint64_t, _byteswap_uint64(STATIC_CAST(uint64_t, x)))
#else
#define _SHP_SWAP32(x) \
STATIC_CAST(uint32_t, \
((STATIC_CAST(uint32_t, x) & 0x000000ffU) << 24) | \
((STATIC_CAST(uint32_t, x) & 0x0000ff00U) << 8) | \
((STATIC_CAST(uint32_t, x) & 0x00ff0000U) >> 8) | \
((STATIC_CAST(uint32_t, x) & 0xff000000U) >> 24))
#define _SHP_SWAP64(x) \
((STATIC_CAST(uint64_t, _SHP_SWAP32(STATIC_CAST(uint32_t, x))) << 32) | \
(STATIC_CAST(uint64_t, _SHP_SWAP32(STATIC_CAST( \
uint32_t, STATIC_CAST(uint64_t, x) >> 32)))))

#endif

/* in-place uint32_t* swap */
#define SHP_SWAP32(p) \
*STATIC_CAST(uint32_t *, p) = _SHP_SWAP32(*STATIC_CAST(uint32_t *, p))
/* in-place uint64_t* swap */
#define SHP_SWAP64(p) \
*STATIC_CAST(uint64_t *, p) = _SHP_SWAP64(*STATIC_CAST(uint64_t *, p))
/* in-place double* swap */
#define SHP_SWAPDOUBLE(x) \
do \
{ \
uint64_t _n64; \
void *_lx = x; \
memcpy(&_n64, _lx, 8); \
_n64 = _SHP_SWAP64(_n64); \
memcpy(_lx, &_n64, 8); \
} while (0)
/* copy double* swap*/
#define SHP_SWAPDOUBLE_CPY(dst, src) \
do \
{ \
uint64_t _n64; \
const void *_ls = src; \
void *_ld = dst; \
memcpy(&_n64, _ls, 8); \
_n64 = _SHP_SWAP64(_n64); \
memcpy(_ld, &_n64, 8); \
} while (0)
#endif /* ndef SHAPEFILE_PRIVATE_H_INCLUDED */
Loading

0 comments on commit 0e46de8

Please sign in to comment.