File: shapefil_private.h

package info (click to toggle)
shapelib 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,884 kB
  • sloc: ansic: 10,567; sh: 4,620; cpp: 752; makefile: 192; pascal: 159
file content (117 lines) | stat: -rw-r--r-- 5,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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-2024, 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

#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

#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)                                                          \
    *REINTERPRET_CAST(uint32_t *, p) =                                         \
        _SHP_SWAP32(*REINTERPRET_CAST(uint32_t *, p))
/* in-place uint64_t* swap */
#define SHP_SWAP64(p)                                                          \
    *REINTERPRET_CAST(uint64_t *, p) =                                         \
        _SHP_SWAP64(*REINTERPRET_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 */