Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer builtin swap functions (updated alternative PR) #96

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 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 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
42 changes: 5 additions & 37 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 @@ -180,10 +148,10 @@ SBNSearchHandle SBNOpenDiskTree(const char *pszSBNFilename,
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);
SHP_SWAP64(STATIC_CAST(uint64_t *, STATIC_CAST(void *, &hSBN->dfMinX)));
SHP_SWAP64(STATIC_CAST(uint64_t *, STATIC_CAST(void *, &hSBN->dfMinY)));
SHP_SWAP64(STATIC_CAST(uint64_t *, STATIC_CAST(void *, &hSBN->dfMaxX)));
SHP_SWAP64(STATIC_CAST(uint64_t *, STATIC_CAST(void *, &hSBN->dfMaxY)));
#endif

if (hSBN->dfMinX > hSBN->dfMaxX || hSBN->dfMinY > hSBN->dfMaxY)
Expand Down
68 changes: 68 additions & 0 deletions shapefil_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#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

#define SHP_SWAP32(p) *STATIC_CAST(uint32_t *, p) = _SHP_SWAP32(*(p))
#define SHP_SWAP64(p) *STATIC_CAST(uint64_t *, p) = _SHP_SWAP64(*(p))

#endif /* ndef SHAPEFILE_PRIVATE_H_INCLUDED */
Loading
Loading