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

[Internal shapelib] Resync with upstream #9188

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
79 changes: 55 additions & 24 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 Expand Up @@ -310,7 +298,6 @@ void SHPAPI_CALL DBFSetLastModifiedDate(DBFHandle psDBF, int nYYSince1900,
/************************************************************************/

DBFHandle SHPAPI_CALL DBFOpen(const char *pszFilename, const char *pszAccess)

{
SAHooks sHooks;

Expand Down Expand Up @@ -1077,7 +1064,6 @@ double SHPAPI_CALL DBFReadDoubleAttribute(DBFHandle psDBF, int iRecord,

const char SHPAPI_CALL1(*)
DBFReadStringAttribute(DBFHandle psDBF, int iRecord, int iField)

{
return STATIC_CAST(const char *,
DBFReadAttribute(psDBF, iRecord, iField, 'C'));
Expand All @@ -1091,12 +1077,41 @@ const char SHPAPI_CALL1(*)

const char SHPAPI_CALL1(*)
DBFReadLogicalAttribute(DBFHandle psDBF, int iRecord, int iField)

{
return STATIC_CAST(const char *,
DBFReadAttribute(psDBF, iRecord, iField, 'L'));
}

/************************************************************************/
/* DBFReadDateAttribute() */
/* */
/* Read a date attribute. */
/************************************************************************/

SHPDate SHPAPI_CALL DBFReadDateAttribute(DBFHandle psDBF, int iRecord,
int iField)
{
const char *pdateValue = DBFReadStringAttribute(psDBF, iRecord, iField);

SHPDate date;

if (pdateValue == SHPLIB_NULLPTR)
{
date.year = 0;
date.month = 0;
date.day = 0;
}
else if (3 != sscanf(pdateValue, "%4d%2d%2d", &date.year, &date.month,
&date.day))
{
date.year = 0;
date.month = 0;
date.day = 0;
}

return date;
}

/************************************************************************/
/* DBFIsValueNULL() */
/* */
Expand Down Expand Up @@ -1171,7 +1186,6 @@ int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle psDBF, int iRecord, int iField)
/************************************************************************/

int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)

{
return (psDBF->nFields);
}
Expand All @@ -1183,7 +1197,6 @@ int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)
/************************************************************************/

int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)

{
return (psDBF->nRecords);
}
Expand All @@ -1199,7 +1212,6 @@ int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField,
char *pszFieldName, int *pnWidth,
int *pnDecimals)

{
if (iField < 0 || iField >= psDBF->nFields)
return (FTInvalid);
Expand Down Expand Up @@ -1479,7 +1491,6 @@ int SHPAPI_CALL DBFWriteIntegerAttribute(DBFHandle psDBF, int iRecord,

int SHPAPI_CALL DBFWriteStringAttribute(DBFHandle psDBF, int iRecord,
int iField, const char *pszValue)

{
return (
DBFWriteAttribute(psDBF, iRecord, iField,
Expand All @@ -1493,7 +1504,6 @@ int SHPAPI_CALL DBFWriteStringAttribute(DBFHandle psDBF, int iRecord,
/************************************************************************/

int SHPAPI_CALL DBFWriteNULLAttribute(DBFHandle psDBF, int iRecord, int iField)

{
return (DBFWriteAttribute(psDBF, iRecord, iField, SHPLIB_NULLPTR));
}
Expand All @@ -1506,13 +1516,36 @@ int SHPAPI_CALL DBFWriteNULLAttribute(DBFHandle psDBF, int iRecord, int iField)

int SHPAPI_CALL DBFWriteLogicalAttribute(DBFHandle psDBF, int iRecord,
int iField, const char lValue)

{
return (
DBFWriteAttribute(psDBF, iRecord, iField,
STATIC_CAST(void *, CONST_CAST(char *, &lValue))));
}

/************************************************************************/
/* DBFWriteDateAttribute() */
/* */
/* Write a date attribute. */
/************************************************************************/

int SHPAPI_CALL DBFWriteDateAttribute(DBFHandle psDBF, int iRecord, int iField,
const SHPDate *lValue)
{
if (NULL == lValue)
return false;
/* check for supported digit range, but do not check for valid date */
if (lValue->year < 0 || lValue->year > 9999)
return false;
if (lValue->month < 0 || lValue->month > 99)
return false;
if (lValue->day < 0 || lValue->day > 99)
return false;
char dateValue[9]; /* "yyyyMMdd\0" */
snprintf(dateValue, sizeof(dateValue), "%04d%02d%02d", lValue->year,
lValue->month, lValue->day);
return (DBFWriteStringAttribute(psDBF, iRecord, iField, dateValue));
}

/************************************************************************/
/* DBFWriteTuple() */
/* */
Expand Down Expand Up @@ -1572,7 +1605,6 @@ int SHPAPI_CALL DBFWriteTuple(DBFHandle psDBF, int hEntity,
/************************************************************************/

const char SHPAPI_CALL1(*) DBFReadTuple(DBFHandle psDBF, int hEntity)

{
if (hEntity < 0 || hEntity >= psDBF->nRecords)
return SHPLIB_NULLPTR;
Expand Down Expand Up @@ -1649,7 +1681,6 @@ DBFHandle SHPAPI_CALL DBFCloneEmpty(DBFHandle psDBF, const char *pszFilename)
/************************************************************************/

char SHPAPI_CALL DBFGetNativeFieldType(DBFHandle psDBF, int iField)

{
if (iField >= 0 && iField < psDBF->nFields)
return psDBF->pachFieldType[iField];
Expand Down
61 changes: 8 additions & 53 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 @@ -186,18 +142,17 @@ 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 (!bBigEndian)
{
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
36 changes: 35 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 @@ -193,6 +216,13 @@ extern "C"

typedef SHPInfo *SHPHandle;

typedef struct
{
int year;
int month;
int day;
} SHPDate;

/* -------------------------------------------------------------------- */
/* Shape types (nSHPType) */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -358,7 +388,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 Expand Up @@ -522,6 +551,8 @@ extern "C"
DBFReadStringAttribute(DBFHandle hDBF, int iShape, int iField);
const char SHPAPI_CALL1(*)
DBFReadLogicalAttribute(DBFHandle hDBF, int iShape, int iField);
SHPDate SHPAPI_CALL DBFReadDateAttribute(DBFHandle hDBF, int iShape,
int iField);
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle hDBF, int iShape, int iField);

int SHPAPI_CALL DBFWriteIntegerAttribute(DBFHandle hDBF, int iShape,
Expand All @@ -537,6 +568,9 @@ extern "C"
int SHPAPI_CALL DBFWriteLogicalAttribute(DBFHandle hDBF, int iShape,
int iField,
const char lFieldValue);
int SHPAPI_CALL DBFWriteDateAttribute(DBFHandle hDBF, int iShape,
int iField,
const SHPDate *dateFieldValue);
int SHPAPI_CALL DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity,
int iField, const void *pValue);
const char SHPAPI_CALL1(*) DBFReadTuple(DBFHandle psDBF, int hEntity);
Expand Down
Loading
Loading