Skip to content

Commit

Permalink
[Internal shapelib] Give a try to OSGeo/shapelib#94
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 5, 2024
1 parent c4505ed commit 44ecd69
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 463 deletions.
14 changes: 1 addition & 13 deletions ogr/ogrsf_frmts/shape/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
59 changes: 7 additions & 52 deletions ogr/ogrsf_frmts/shape/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,45 +97,13 @@ 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() */
/************************************************************************/

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. */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -191,13 +147,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)
SHP_SWAPDOUBLE(&hSBN->dfMinX);
SHP_SWAPDOUBLE(&hSBN->dfMinY);
SHP_SWAPDOUBLE(&hSBN->dfMaxX);
SHP_SWAPDOUBLE(&hSBN->dfMaxY);
#endif

if (hSBN->dfMinX > hSBN->dfMaxX || hSBN->dfMinY > hSBN->dfMaxY)
{
Expand Down
24 changes: 23 additions & 1 deletion ogr/ogrsf_frmts/shape/shapefil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
{
Expand Down Expand Up @@ -358,7 +381,6 @@ extern "C"
int SHPAPI_CALL SHPWriteTree(SHPTree *hTree, const char *pszFilename);

int SHPAPI_CALL SHPTreeAddShapeId(SHPTree *hTree, SHPObject *psObject);
int SHPAPI_CALL SHPTreeRemoveShapeId(SHPTree *hTree, int nShapeId);

void SHPAPI_CALL SHPTreeTrimExtraNodes(SHPTree *hTree);

Expand Down
78 changes: 78 additions & 0 deletions ogr/ogrsf_frmts/shape/shapefil_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef SHAPEFILE_PRIVATE_H_INCLUDED
#define SHAPEFILE_PRIVATE_H_INCLUDED

/******************************************************************************
*
* Project: Shapelib
* Purpose: Private include file for Shapelib.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* 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(*STATIC_CAST(uint32_t *, p))
#define SHP_SWAP64(p) \
*STATIC_CAST(uint64_t *, p) = _SHP_SWAP64(*STATIC_CAST(uint64_t *, p))
#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)
#endif /* ndef SHAPEFILE_PRIVATE_H_INCLUDED */
Loading

0 comments on commit 44ecd69

Please sign in to comment.