GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
shapefil_private.h
Go to the documentation of this file.
1 #ifndef SHAPEFILE_PRIVATE_H_INCLUDED
2 #define SHAPEFILE_PRIVATE_H_INCLUDED
3 
4 /******************************************************************************
5  *
6  * Project: Shapelib
7  * Purpose: Private include file for Shapelib.
8  * Author: Frank Warmerdam, warmerdam@pobox.com
9  *
10  ******************************************************************************
11  * Copyright (c) 1999, Frank Warmerdam
12  * Copyright (c) 2012-2016, Even Rouault <even dot rouault at spatialys.com>
13  *
14  * SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
15  ******************************************************************************
16  *
17  */
18 
19 #ifdef __cplusplus
20 #define STATIC_CAST(type, x) static_cast<type>(x)
21 #define REINTERPRET_CAST(type, x) reinterpret_cast<type>(x)
22 #define CONST_CAST(type, x) const_cast<type>(x)
23 #define SHPLIB_NULLPTR nullptr
24 #else
25 #define STATIC_CAST(type, x) ((type)(x))
26 #define REINTERPRET_CAST(type, x) ((type)(x))
27 #define CONST_CAST(type, x) ((type)(x))
28 #define SHPLIB_NULLPTR NULL
29 #endif
30 
31 #if !defined(SHP_BIG_ENDIAN)
32 #if defined(CPL_MSB)
33 #define SHP_BIG_ENDIAN 1
34 #elif (defined(__GNUC__) && __GNUC__ >= 5) || \
35  (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && \
36  __GNUC_MINOR__ >= 6)
37 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
38 #define SHP_BIG_ENDIAN 1
39 #endif
40 #elif defined(__GLIBC__)
41 #if __BYTE_ORDER == __BIG_ENDIAN
42 #define SHP_BIG_ENDIAN 1
43 #endif
44 #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
45 #define SHP_BIG_ENDIAN 1
46 #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
47 #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || \
48  defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || \
49  defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
50 #define SHP_BIG_ENDIAN 1
51 #endif
52 #endif
53 
54 #include "shapefil.h"
55 #include <stdint.h>
56 #include <stdlib.h>
57 
58 /************************************************************************/
59 /* Little endian <==> big endian byte swap macros. */
60 /************************************************************************/
61 
62 #if (defined(__GNUC__) && __GNUC__ >= 5) || \
63  (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && \
64  __GNUC_MINOR__ >= 8)
65 #define _SHP_SWAP32(x) \
66  STATIC_CAST(uint32_t, __builtin_bswap32(STATIC_CAST(uint32_t, x)))
67 #define _SHP_SWAP64(x) \
68  STATIC_CAST(uint64_t, __builtin_bswap64(STATIC_CAST(uint64_t, x)))
69 #elif defined(_MSC_VER)
70 #define _SHP_SWAP32(x) \
71  STATIC_CAST(uint32_t, _byteswap_ulong(STATIC_CAST(uint32_t, x)))
72 #define _SHP_SWAP64(x) \
73  STATIC_CAST(uint64_t, _byteswap_uint64(STATIC_CAST(uint64_t, x)))
74 #else
75 #define _SHP_SWAP32(x) \
76  STATIC_CAST(uint32_t, \
77  ((STATIC_CAST(uint32_t, x) & 0x000000ffU) << 24) | \
78  ((STATIC_CAST(uint32_t, x) & 0x0000ff00U) << 8) | \
79  ((STATIC_CAST(uint32_t, x) & 0x00ff0000U) >> 8) | \
80  ((STATIC_CAST(uint32_t, x) & 0xff000000U) >> 24))
81 #define _SHP_SWAP64(x) \
82  ((STATIC_CAST(uint64_t, _SHP_SWAP32(STATIC_CAST(uint32_t, x))) << 32) | \
83  (STATIC_CAST(uint64_t, _SHP_SWAP32(STATIC_CAST( \
84  uint32_t, STATIC_CAST(uint64_t, x) >> 32)))))
85 
86 #endif
87 
88 /* in-place uint32_t* swap */
89 #define SHP_SWAP32(p) \
90  *REINTERPRET_CAST(uint32_t *, p) = \
91  _SHP_SWAP32(*REINTERPRET_CAST(uint32_t *, p))
92 /* in-place uint64_t* swap */
93 #define SHP_SWAP64(p) \
94  *REINTERPRET_CAST(uint64_t *, p) = \
95  _SHP_SWAP64(*REINTERPRET_CAST(uint64_t *, p))
96 /* in-place double* swap */
97 #define SHP_SWAPDOUBLE(x) \
98  do { \
99  uint64_t _n64; \
100  void *_lx = x; \
101  memcpy(&_n64, _lx, 8); \
102  _n64 = _SHP_SWAP64(_n64); \
103  memcpy(_lx, &_n64, 8); \
104  } while (0)
105 /* copy double* swap*/
106 #define SHP_SWAPDOUBLE_CPY(dst, src) \
107  do { \
108  uint64_t _n64; \
109  const void *_ls = src; \
110  void *_ld = dst; \
111  memcpy(&_n64, _ls, 8); \
112  _n64 = _SHP_SWAP64(_n64); \
113  memcpy(_ld, &_n64, 8); \
114  } while (0)
115 #endif /* ndef SHAPEFILE_PRIVATE_H_INCLUDED */