1 /******************************************************************************
2  * $Id: cpl_port.h efdea747194ad7dc810fb30654f4cf3f09756013 2021-04-15 10:09:43 +0100 Andrew C Aitchison $
3  *
4  * Project:  CPL - Common Portability Library
5  * Author:   Frank Warmerdam, warmerdam@pobox.com
6  * Purpose:  Include file providing low level portability services for CPL.
7  *           This should be the first include file for any CPL based code.
8  *
9  ******************************************************************************
10  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
11  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef CPL_BASE_H_INCLUDED
33 #define CPL_BASE_H_INCLUDED
34 
35 /**
36  * \file cpl_port.h
37  *
38  * Core portability definitions for CPL.
39  *
40  */
41 
42 /* ==================================================================== */
43 /*      We will use WIN32 as a standard windows define.                 */
44 /* ==================================================================== */
45 #if defined(_WIN32) && !defined(WIN32)
46 #  define WIN32
47 #endif
48 
49 #if defined(_WINDOWS) && !defined(WIN32)
50 #  define WIN32
51 #endif
52 
53 /* -------------------------------------------------------------------- */
54 /*      The following apparently allow you to use strcpy() and other    */
55 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
56 /* -------------------------------------------------------------------- */
57 #ifdef _MSC_VER
58 #  ifndef _CRT_SECURE_NO_DEPRECATE
59 #    define _CRT_SECURE_NO_DEPRECATE
60 #  endif
61 #  ifndef _CRT_NONSTDC_NO_DEPRECATE
62 #    define _CRT_NONSTDC_NO_DEPRECATE
63 #  endif
64 #endif
65 
66 #include "cpl_config.h"
67 
68 /* ==================================================================== */
69 /*      A few sanity checks, mainly to detect problems that sometimes   */
70 /*      arise with bad configured cross-compilation.                    */
71 /* ==================================================================== */
72 
73 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
74 #error "Unexpected value for SIZEOF_INT"
75 #endif
76 
77 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
78 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
79 #endif
80 
81 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
82 #error "Unexpected value for SIZEOF_VOIDP"
83 #endif
84 
85 /* ==================================================================== */
86 /*      This will disable most WIN32 stuff in a Cygnus build which      */
87 /*      defines unix to 1.                                              */
88 /* ==================================================================== */
89 
90 #ifdef unix
91 #  undef WIN32
92 #endif
93 
94 /*! @cond Doxygen_Suppress */
95 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
96 #  define _LARGEFILE64_SOURCE 1
97 #endif
98 
99 /* ==================================================================== */
100 /*      If iconv() is available use extended recoding module.           */
101 /*      Stub implementation is always compiled in, because it works     */
102 /*      faster than iconv() for encodings it supports.                  */
103 /* ==================================================================== */
104 
105 #if defined(HAVE_ICONV)
106 #  define CPL_RECODE_ICONV
107 #endif
108 
109 #define CPL_RECODE_STUB
110 /*! @endcond */
111 
112 /* ==================================================================== */
113 /*      MinGW stuff                                                     */
114 /* ==================================================================== */
115 
116 /* We need __MSVCRT_VERSION__ >= 0x0700 to have "_aligned_malloc" */
117 /* Latest versions of mingw32 define it, but with older ones, */
118 /* we need to define it manually */
119 #if defined(__MINGW32__)
120 #ifndef __MSVCRT_VERSION__
121 #define __MSVCRT_VERSION__ 0x0700
122 #endif
123 #endif
124 
125 /* Needed for std=c11 on Solaris to have strcasecmp() */
126 #if defined(GDAL_COMPILATION) && defined(__sun__) && (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
127 #ifdef _XOPEN_SOURCE
128 #undef _XOPEN_SOURCE
129 #endif
130 #define _XOPEN_SOURCE 600
131 #endif
132 
133 /* ==================================================================== */
134 /*      Standard include files.                                         */
135 /* ==================================================================== */
136 
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <math.h>
140 #include <stdarg.h>
141 #include <string.h>
142 #include <ctype.h>
143 #include <limits.h>
144 
145 #include <time.h>
146 
147 #if defined(HAVE_ERRNO_H)
148 #  include <errno.h>
149 #endif
150 
151 #ifdef HAVE_LOCALE_H
152 #  include <locale.h>
153 #endif
154 
155 #ifdef HAVE_DIRECT_H
156 #  include <direct.h>
157 #endif
158 
159 #if !defined(WIN32)
160 #  include <strings.h>
161 #endif
162 
163 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
164 #  define DBMALLOC
165 #  include <dbmalloc.h>
166 #endif
167 
168 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
169 #  define USE_DMALLOC
170 #  include <dmalloc.h>
171 #endif
172 
173 /* ==================================================================== */
174 /*      Base portability stuff ... this stuff may need to be            */
175 /*      modified for new platforms.                                     */
176 /* ==================================================================== */
177 
178 /* -------------------------------------------------------------------- */
179 /*      Which versions of C++ are available.                            */
180 /* -------------------------------------------------------------------- */
181 
182 /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
183 /* as a minimum */
184 
185 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
186 #  if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
187 #    error Must have C++11 or newer.
188 #  endif
189 #  if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
190 #    define HAVE_CXX14 1
191 #  endif
192 #  if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
193 #    define HAVE_CXX17 1
194 #  endif
195 #endif  /* __cplusplus */
196 
197 /*---------------------------------------------------------------------
198  *        types for 16 and 32 bits integers, etc...
199  *--------------------------------------------------------------------*/
200 #if UINT_MAX == 65535
201 typedef long            GInt32;
202 typedef unsigned long   GUInt32;
203 #else
204 /** Int32 type */
205 typedef int             GInt32;
206 /** Unsigned int32 type */
207 typedef unsigned int    GUInt32;
208 #endif
209 
210 /** Int16 type */
211 typedef short           GInt16;
212 /** Unsigned int16 type */
213 typedef unsigned short  GUInt16;
214 /** Unsigned byte type */
215 typedef unsigned char   GByte;
216 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
217 /* in include/poppler/goo/gtypes.h */
218 #ifndef CPL_GBOOL_DEFINED
219 /*! @cond Doxygen_Suppress */
220 #define CPL_GBOOL_DEFINED
221 /*! @endcond */
222 /** Type for boolean values (alias to int) */
223 typedef int             GBool;
224 #endif
225 
226 /*! @cond Doxygen_Suppress */
227 #ifdef __cplusplus
228 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
229 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
230 #else
231 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
232 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
233 #endif
234 /*! @endcond */
235 
236 /* -------------------------------------------------------------------- */
237 /*      64bit support                                                   */
238 /* -------------------------------------------------------------------- */
239 
240 #if defined(WIN32) && defined(_MSC_VER)
241 #define VSI_LARGE_API_SUPPORTED
242 #endif
243 
244 #if HAVE_LONG_LONG
245 
246 /** Large signed integer type (generally 64-bit integer type).
247  *  Use GInt64 when exactly 64 bit is needed */
248 typedef long long        GIntBig;
249 /** Large unsigned integer type (generally 64-bit unsigned integer type).
250  *  Use GUInt64 when exactly 64 bit is needed */
251 typedef unsigned long long GUIntBig;
252 
253 /** Minimum GIntBig value */
254 #define GINTBIG_MIN     (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
255 /** Maximum GIntBig value */
256 #define GINTBIG_MAX     ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
257 /** Maximum GUIntBig value */
258 #define GUINTBIG_MAX    ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
259 
260 /*! @cond Doxygen_Suppress */
261 #define CPL_HAS_GINT64 1
262 /*! @endcond */
263 
264 /* Note: we might want to use instead int64_t / uint64_t if they are available */
265 
266 /** Signed 64 bit integer type */
267 typedef GIntBig          GInt64;
268 /** Unsigned 64 bit integer type */
269 typedef GUIntBig         GUInt64;
270 
271 /** Minimum GInt64 value */
272 #define GINT64_MIN      GINTBIG_MIN
273 /** Maximum GInt64 value */
274 #define GINT64_MAX      GINTBIG_MAX
275 /** Minimum GUInt64 value */
276 #define GUINT64_MAX     GUINTBIG_MAX
277 
278 #else
279 
280 #error "64bit integer support required"
281 
282 #endif
283 
284 #if SIZEOF_VOIDP == 8
285 /** Integer type large enough to hold the difference between 2 addresses */
286 typedef GIntBig          GPtrDiff_t;
287 #else
288 /** Integer type large enough to hold the difference between 2 addresses */
289 typedef int              GPtrDiff_t;
290 #endif
291 
292 #ifdef GDAL_COMPILATION
293 #if HAVE_UINTPTR_T
294 #include <stdint.h>
295 typedef uintptr_t GUIntptr_t;
296 #elif SIZEOF_VOIDP == 8
297 typedef GUIntBig GUIntptr_t;
298 #else
299 typedef unsigned int  GUIntptr_t;
300 #endif
301 
302 #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
303 
304 #endif
305 
306 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
307   #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
308 #elif HAVE_LONG_LONG
309 /** Printf formatting suffix for GIntBig */
310   #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
311 #else
312   #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
313 #endif
314 
315 /** Printf formatting for GIntBig */
316 #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
317 /** Printf formatting for GUIntBig */
318 #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
319 
320 /*! @cond Doxygen_Suppress */
321 #define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
322 /*! @endcond */
323 
324 /*! @cond Doxygen_Suppress */
325 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
326 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
327 #else
328 #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
329 #endif
330 /*! @endcond */
331 
332 /* ==================================================================== */
333 /*      Other standard services.                                        */
334 /* ==================================================================== */
335 #ifdef __cplusplus
336 /** Macro to start a block of C symbols */
337 #  define CPL_C_START           extern "C" {
338 /** Macro to end a block of C symbols */
339 #  define CPL_C_END             }
340 #else
341 #  define CPL_C_START
342 #  define CPL_C_END
343 #endif
344 
345 #ifndef CPL_DLL
346 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
347 #  ifdef GDAL_COMPILATION
348 #    define CPL_DLL __declspec(dllexport)
349 #  else
350 #    define CPL_DLL
351 #  endif
352 #  define CPL_INTERNAL
353 #else
354 #  if defined(USE_GCC_VISIBILITY_FLAG)
355 #    define CPL_DLL     __attribute__ ((visibility("default")))
356 #    if !defined(__MINGW32__)
357 #        define CPL_INTERNAL __attribute__((visibility("hidden")))
358 #    else
359 #        define CPL_INTERNAL
360 #    endif
361 #  else
362 #    define CPL_DLL
363 #    define CPL_INTERNAL
364 #  endif
365 #endif
366 
367 // Marker for unstable API
368 #define CPL_UNSTABLE_API CPL_DLL
369 
370 #endif
371 
372 /*! @cond Doxygen_Suppress */
373 /* Should optional (normally private) interfaces be exported? */
374 #ifdef CPL_OPTIONAL_APIS
375 #  define CPL_ODLL CPL_DLL
376 #else
377 #  define CPL_ODLL
378 #endif
379 /*! @endcond */
380 
381 #ifndef CPL_STDCALL
382 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
383 #  define CPL_STDCALL     __stdcall
384 #else
385 #  define CPL_STDCALL
386 #endif
387 #endif
388 
389 /*! @cond Doxygen_Suppress */
390 #ifdef _MSC_VER
391 #  define FORCE_CDECL  __cdecl
392 #else
393 #  define FORCE_CDECL
394 #endif
395 /*! @endcond */
396 
397 /*! @cond Doxygen_Suppress */
398 /* TODO : support for other compilers needed */
399 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
400 #define HAS_CPL_INLINE  1
401 #define CPL_INLINE __inline
402 #elif defined(__SUNPRO_CC)
403 #define HAS_CPL_INLINE  1
404 #define CPL_INLINE inline
405 #else
406 #define CPL_INLINE
407 #endif
408 /*! @endcond*/
409 
410 #ifndef MAX
411 /** Macro to compute the minimum of 2 values */
412 #  define MIN(a,b)      (((a)<(b)) ? (a) : (b))
413 /** Macro to compute the maximum of 2 values */
414 #  define MAX(a,b)      (((a)>(b)) ? (a) : (b))
415 #endif
416 
417 #ifndef ABS
418 /** Macro to compute the absolute value */
419 #  define ABS(x)        (((x)<0) ? (-1*(x)) : (x))
420 #endif
421 
422 #ifndef M_PI
423 /** PI definition */
424 # define M_PI 3.14159265358979323846
425 /* 3.1415926535897932384626433832795 */
426 #endif
427 
428 /* -------------------------------------------------------------------- */
429 /*      Macro to test equality of two floating point values.            */
430 /*      We use fabs() function instead of ABS() macro to avoid side     */
431 /*      effects.                                                        */
432 /* -------------------------------------------------------------------- */
433 /*! @cond Doxygen_Suppress */
434 #ifndef CPLIsEqual
435 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
436 #endif
437 /*! @endcond */
438 
439 /* -------------------------------------------------------------------- */
440 /*      Provide macros for case insensitive string comparisons.         */
441 /* -------------------------------------------------------------------- */
442 #ifndef EQUAL
443 
444 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
445 
446 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
447         __attribute__((always_inline));
448 
CPL_afl_friendly_memcmp(const void * ptr1,const void * ptr2,size_t len)449 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
450 {
451     const unsigned char* bptr1 = (const unsigned char*)ptr1;
452     const unsigned char* bptr2 = (const unsigned char*)ptr2;
453     while( len-- )
454     {
455         unsigned char b1 = *(bptr1++);
456         unsigned char b2 = *(bptr2++);
457         if( b1 != b2 ) return b1 - b2;
458     }
459     return 0;
460 }
461 
462 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
463         __attribute__((always_inline));
464 
CPL_afl_friendly_strcmp(const char * ptr1,const char * ptr2)465 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
466 {
467     const unsigned char* usptr1 = (const unsigned char*)ptr1;
468     const unsigned char* usptr2 = (const unsigned char*)ptr2;
469     while( 1 )
470     {
471         unsigned char ch1 = *(usptr1++);
472         unsigned char ch2 = *(usptr2++);
473         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
474     }
475 }
476 
477 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
478         __attribute__((always_inline));
479 
CPL_afl_friendly_strncmp(const char * ptr1,const char * ptr2,size_t len)480 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
481 {
482     const unsigned char* usptr1 = (const unsigned char*)ptr1;
483     const unsigned char* usptr2 = (const unsigned char*)ptr2;
484     while( len -- )
485     {
486         unsigned char ch1 = *(usptr1++);
487         unsigned char ch2 = *(usptr2++);
488         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
489     }
490     return 0;
491 }
492 
493 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
494         __attribute__((always_inline));
495 
CPL_afl_friendly_strcasecmp(const char * ptr1,const char * ptr2)496 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
497 {
498     const unsigned char* usptr1 = (const unsigned char*)ptr1;
499     const unsigned char* usptr2 = (const unsigned char*)ptr2;
500     while( 1 )
501     {
502         unsigned char ch1 = *(usptr1++);
503         unsigned char ch2 = *(usptr2++);
504         ch1 = (unsigned char)toupper(ch1);
505         ch2 = (unsigned char)toupper(ch2);
506         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
507     }
508 }
509 
510 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
511         __attribute__((always_inline));
512 
CPL_afl_friendly_strncasecmp(const char * ptr1,const char * ptr2,size_t len)513 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
514 {
515     const unsigned char* usptr1 = (const unsigned char*)ptr1;
516     const unsigned char* usptr2 = (const unsigned char*)ptr2;
517     while( len-- )
518     {
519         unsigned char ch1 = *(usptr1++);
520         unsigned char ch2 = *(usptr2++);
521         ch1 = (unsigned char)toupper(ch1);
522         ch2 = (unsigned char)toupper(ch2);
523         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
524     }
525     return 0;
526 }
527 
528 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
529         __attribute__((always_inline));
530 
CPL_afl_friendly_strstr(const char * haystack,const char * needle)531 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
532 {
533     const char* ptr_haystack = haystack;
534     while( 1 )
535     {
536         const char* ptr_haystack2 = ptr_haystack;
537         const char* ptr_needle = needle;
538         while( 1 )
539         {
540             char ch1 = *(ptr_haystack2++);
541             char ch2 = *(ptr_needle++);
542             if( ch2 == 0 )
543                 return (char*)ptr_haystack;
544             if( ch1 != ch2 )
545                 break;
546         }
547         if( *ptr_haystack == 0 )
548             return NULL;
549         ptr_haystack ++;
550     }
551 }
552 
553 #undef strcmp
554 #undef strncmp
555 #define memcmp CPL_afl_friendly_memcmp
556 #define strcmp CPL_afl_friendly_strcmp
557 #define strncmp CPL_afl_friendly_strncmp
558 #define strcasecmp CPL_afl_friendly_strcasecmp
559 #define strncasecmp CPL_afl_friendly_strncasecmp
560 #define strstr CPL_afl_friendly_strstr
561 
562 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
563 
564 #  if defined(WIN32)
565 #    define STRCASECMP(a,b)         (_stricmp(a,b))
566 #    define STRNCASECMP(a,b,n)      (_strnicmp(a,b,n))
567 #  else
568 /** Alias for strcasecmp() */
569 #    define STRCASECMP(a,b)         (strcasecmp(a,b))
570 /** Alias for strncasecmp() */
571 #    define STRNCASECMP(a,b,n)      (strncasecmp(a,b,n))
572 #  endif
573 /** Alias for strncasecmp() == 0 */
574 #  define EQUALN(a,b,n)           (STRNCASECMP(a,b,n)==0)
575 /** Alias for strcasecmp() == 0 */
576 #  define EQUAL(a,b)              (STRCASECMP(a,b)==0)
577 #endif
578 
579 /*---------------------------------------------------------------------
580  * Does a string "a" start with string "b".  Search is case-sensitive or,
581  * with CI, it is a case-insensitive comparison.
582  *--------------------------------------------------------------------- */
583 #ifndef STARTS_WITH_CI
584 /** Returns whether a starts with b */
585 #define STARTS_WITH(a,b)               (strncmp(a,b,strlen(b)) == 0)
586 /** Returns whether a starts with b (case insensitive comparison) */
587 #define STARTS_WITH_CI(a,b)            EQUALN(a,b,strlen(b))
588 #endif
589 
590 /*! @cond Doxygen_Suppress */
591 #ifndef CPL_THREADLOCAL
592 #  define CPL_THREADLOCAL
593 #endif
594 /*! @endcond */
595 
596 /* -------------------------------------------------------------------- */
597 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
598 /*      are supposed to be macros according to C99, defined in math.h   */
599 /*      Some systems (i.e. Tru64) don't have isinf() at all, so if      */
600 /*      the macro is not defined we just assume nothing is infinite.    */
601 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
602 /*      function but no corresponding macro, but I can live with        */
603 /*      that since it isn't that important a test.                      */
604 /* -------------------------------------------------------------------- */
605 #ifdef _MSC_VER
606 #  include <float.h>
607 #  define CPLIsNan(x) _isnan(x)
608 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
609 #  define CPLIsFinite(x) _finite(x)
610 #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
611 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
612 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
613 #  define CPLIsNan(x)    __builtin_isnan(x)
614 #  define CPLIsInf(x)    __builtin_isinf(x)
615 #  define CPLIsFinite(x) __builtin_isfinite(x)
616 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
617 extern "C++" {
618 #ifndef DOXYGEN_SKIP
619 #include <cmath>
620 #endif
CPLIsNan(float f)621 static inline int CPLIsNan(float f) { return std::isnan(f); }
CPLIsNan(double f)622 static inline int CPLIsNan(double f) { return std::isnan(f); }
CPLIsInf(float f)623 static inline int CPLIsInf(float f) { return std::isinf(f); }
CPLIsInf(double f)624 static inline int CPLIsInf(double f) { return std::isinf(f); }
CPLIsFinite(float f)625 static inline int CPLIsFinite(float f) { return std::isfinite(f); }
CPLIsFinite(double f)626 static inline int CPLIsFinite(double f) { return std::isfinite(f); }
627 }
628 #else
629 /** Return whether a floating-pointer number is NaN */
630 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
631 /* so to not get warning about conversion from double to float with */
632 /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
633 extern "C++" {
CPLIsNan(float f)634 static inline int CPLIsNan(float f) { return __isnanf(f); }
CPLIsNan(double f)635 static inline int CPLIsNan(double f) { return __isnan(f); }
CPLIsInf(float f)636 static inline int CPLIsInf(float f) { return __isinff(f); }
CPLIsInf(double f)637 static inline int CPLIsInf(double f) { return __isinf(f); }
CPLIsFinite(float f)638 static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
CPLIsFinite(double f)639 static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
640 }
641 #else
642 #  define CPLIsNan(x) isnan(x)
643 #  if defined(isinf) || defined(__FreeBSD__)
644 /** Return whether a floating-pointer number is +/- infinity */
645 #    define CPLIsInf(x) isinf(x)
646 /** Return whether a floating-pointer number is finite */
647 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
648 #  elif defined(__sun__)
649 #    include <ieeefp.h>
650 #    define CPLIsInf(x)    (!finite(x) && !isnan(x))
651 #    define CPLIsFinite(x) finite(x)
652 #  else
653 #    define CPLIsInf(x)    (0)
654 #    define CPLIsFinite(x) (!isnan(x))
655 #  endif
656 #endif
657 #endif
658 
659 /*! @cond Doxygen_Suppress */
660 /*---------------------------------------------------------------------
661  *                         CPL_LSB and CPL_MSB
662  * Only one of these 2 macros should be defined and specifies the byte
663  * ordering for the current platform.
664  * This should be defined in the Makefile, but if it is not then
665  * the default is CPL_LSB (Intel ordering, LSB first).
666  *--------------------------------------------------------------------*/
667 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
668 #  define CPL_MSB
669 #endif
670 
671 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
672 #define CPL_LSB
673 #endif
674 
675 #if defined(CPL_LSB)
676 #  define CPL_IS_LSB 1
677 #else
678 #  define CPL_IS_LSB 0
679 #endif
680 /*! @endcond */
681 
682 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
683 
684 /*! @cond Doxygen_Suppress */
685 extern "C++" {
686 
687 template <bool b> struct CPLStaticAssert {};
688 template<> struct CPLStaticAssert<true>
689 {
690     static void my_function() {}
691 };
692 
693 } /* extern "C++" */
694 
695 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
696 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
697 
698 #else  /* __cplusplus */
699 
700 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
701 
702 #endif  /* __cplusplus */
703 /*! @endcond */
704 
705 /*---------------------------------------------------------------------
706  *        Little endian <==> big endian byte swap macros.
707  *--------------------------------------------------------------------*/
708 
709 /** Byte-swap a 16bit unsigned integer */
710 #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
711 
712 #if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
713 /* Could potentially be extended to other architectures but must be checked */
714 /* that the intrinsic is indeed efficient */
715 /* GCC (at least 4.6  or above) need that include */
716 #include <x86intrin.h>
717 /** Byte-swap a 32bit unsigned integer */
718 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
719 /** Byte-swap a 64bit unsigned integer */
720 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
721 #elif defined(_MSC_VER)
722 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
723 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
724 #else
725 /** Byte-swap a 32bit unsigned integer */
726 #define CPL_SWAP32(x) \
727         CPL_STATIC_CAST(GUInt32, \
728             ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
729             ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) <<  8) | \
730             ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >>  8) | \
731             ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
732 
733 /** Byte-swap a 64bit unsigned integer */
734 #define CPL_SWAP64(x) \
735             ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
736              (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
737 
738 #endif
739 
740 /** Byte-swap a 16 bit pointer */
741 #define CPL_SWAP16PTR(x) \
742 do {                                                              \
743     GByte       byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x);              \
744     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
745                                                                   \
746     byTemp = _pabyDataT[0];                                       \
747     _pabyDataT[0] = _pabyDataT[1];                                \
748     _pabyDataT[1] = byTemp;                                       \
749 } while(0)
750 
751 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
752 
753 /** Byte-swap a 32 bit pointer */
754 #define CPL_SWAP32PTR(x) \
755 do {                                                              \
756     GByte       byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x);              \
757     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4);  \
758                                                                   \
759     byTemp = _pabyDataT[0];                                       \
760     _pabyDataT[0] = _pabyDataT[3];                                \
761     _pabyDataT[3] = byTemp;                                       \
762     byTemp = _pabyDataT[1];                                       \
763     _pabyDataT[1] = _pabyDataT[2];                                \
764     _pabyDataT[2] = byTemp;                                       \
765 } while(0)
766 
767 /** Byte-swap a 64 bit pointer */
768 #define CPL_SWAP64PTR(x) \
769 do {                                                              \
770     GByte       byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x);              \
771     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
772                                                                   \
773     byTemp = _pabyDataT[0];                                       \
774     _pabyDataT[0] = _pabyDataT[7];                                \
775     _pabyDataT[7] = byTemp;                                       \
776     byTemp = _pabyDataT[1];                                       \
777     _pabyDataT[1] = _pabyDataT[6];                                \
778     _pabyDataT[6] = byTemp;                                       \
779     byTemp = _pabyDataT[2];                                       \
780     _pabyDataT[2] = _pabyDataT[5];                                \
781     _pabyDataT[5] = byTemp;                                       \
782     byTemp = _pabyDataT[3];                                       \
783     _pabyDataT[3] = _pabyDataT[4];                                \
784     _pabyDataT[4] = byTemp;                                       \
785 } while(0)
786 
787 #else
788 
789 /** Byte-swap a 32 bit pointer */
790 #define CPL_SWAP32PTR(x) \
791 do {                                                                        \
792     GUInt32 _n32;                                                           \
793     void* _lx = x;                                                          \
794     memcpy(&_n32, _lx, 4);                                                  \
795     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
796     _n32 = CPL_SWAP32(_n32);                                                \
797     memcpy(_lx, &_n32, 4);                                                  \
798 } while(0)
799 
800 /** Byte-swap a 64 bit pointer */
801 #define CPL_SWAP64PTR(x) \
802 do {                                                                        \
803     GUInt64 _n64;                                                           \
804     void* _lx = x;                                                          \
805     memcpy(&_n64, _lx, 8);                                                    \
806     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
807     _n64 = CPL_SWAP64(_n64);                                                \
808     memcpy(_lx, &_n64, 8);                                                    \
809 } while(0)
810 
811 #endif
812 
813 /** Byte-swap a 64 bit pointer */
814 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
815 
816 #ifdef CPL_MSB
817 #  define CPL_MSBWORD16(x)      (x)
818 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
819 #  define CPL_MSBWORD32(x)      (x)
820 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
821 #  define CPL_MSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
822 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
823 #  define CPL_MSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
824 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
825 #  define CPL_MSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
826 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
827 #else
828 /** Return a 16bit word from a originally LSB ordered word */
829 #  define CPL_LSBWORD16(x)      (x)
830 /** Return a 16bit word from a originally MSB ordered word */
831 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
832 /** Return a 32bit word from a originally LSB ordered word */
833 #  define CPL_LSBWORD32(x)      (x)
834 /** Return a 32bit word from a originally MSB ordered word */
835 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
836 /** Byte-swap if necessary a 16bit word at the location pointed from a originally LSB ordered pointer */
837 #  define CPL_LSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
838 /** Byte-swap if necessary a 16bit word at the location pointed from a originally MSB ordered pointer */
839 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
840 /** Byte-swap if necessary a 32bit word at the location pointed from a originally LSB ordered pointer */
841 #  define CPL_LSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
842 /** Byte-swap if necessary a 32bit word at the location pointed from a originally MSB ordered pointer */
843 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
844 /** Byte-swap if necessary a 64bit word at the location pointed from a originally LSB ordered pointer */
845 #  define CPL_LSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
846 /** Byte-swap if necessary a 64bit word at the location pointed from a originally MSB ordered pointer */
847 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
848 #endif
849 
850 /** Return a Int16 from the 2 bytes ordered in LSB order at address x.
851  * @deprecated Use rather CPL_LSBSINT16PTR or CPL_LSBUINT16PTR for explicit
852  * signedness. */
853 #define CPL_LSBINT16PTR(x)    ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
854 
855 /** Return a Int32 from the 4 bytes ordered in LSB order at address x.
856  * @deprecated Use rather CPL_LSBSINT32PTR or CPL_LSBUINT32PTR for explicit
857  * signedness. */
858 #define CPL_LSBINT32PTR(x)    ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
859                               (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
860 
861 /** Return a signed Int16 from the 2 bytes ordered in LSB order at address x */
862 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
863 
864 /** Return a unsigned Int16 from the 2 bytes ordered in LSB order at address x */
865 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
866 
867 /** Return a signed Int32 from the 4 bytes ordered in LSB order at address x */
868 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
869 
870 /** Return a unsigned Int32 from the 4 bytes ordered in LSB order at address x */
871 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
872 
873 /*! @cond Doxygen_Suppress */
874 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
875 #ifndef UNREFERENCED_PARAM
876 #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
877 #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
878 #  else
879 #    define UNREFERENCED_PARAM(param) ((void)param)
880 #  endif /* UNREFERENCED_PARAMETER */
881 #endif /* UNREFERENCED_PARAM */
882 /*! @endcond */
883 
884 /***********************************************************************
885  * Define CPL_CVSID() macro.  It can be disabled during a build by
886  * defining DISABLE_CVSID in the compiler options.
887  *
888  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
889  * being unused.
890  */
891 
892 /*! @cond Doxygen_Suppress */
893 #ifndef DISABLE_CVSID
894 #if defined(__GNUC__) && __GNUC__ >= 4
895 #  define CPL_CVSID(string)     static const char cpl_cvsid[] __attribute__((used)) = string;
896 #else
897 #  define CPL_CVSID(string)     static const char cpl_cvsid[] = string; \
898 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
899 #endif
900 #else
901 #  define CPL_CVSID(string)
902 #endif
903 /*! @endcond */
904 
905 /* We exclude mingw64 4.6 which seems to be broken regarding this */
906 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
907 /** Null terminated variadic */
908 #   define CPL_NULL_TERMINATED     __attribute__((__sentinel__))
909 #else
910 /** Null terminated variadic */
911 #   define CPL_NULL_TERMINATED
912 #endif
913 
914 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
915 /** Tag a function to have printf() formatting */
916 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
917 /** Tag a function to have scanf() formatting */
918 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
919 #else
920 /** Tag a function to have printf() formatting */
921 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
922 /** Tag a function to have scanf() formatting */
923 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
924 #endif
925 
926 #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
927 #include <sal.h>
928 /** Macro into which to wrap the format argument of a printf-like function.
929  * Only used if ANALYZE=1 is specified to nmake */
930 #  define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
931 /** Macro into which to wrap the format argument of a sscanf-like function.
932  * Only used if ANALYZE=1 is specified to nmake */
933 #  define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
934 #else
935 /** Macro into which to wrap the format argument of a printf-like function */
936 # define CPL_FORMAT_STRING(arg) arg
937 /** Macro into which to wrap the format argument of a sscanf-like function. */
938 # define CPL_SCANF_FORMAT_STRING(arg) arg
939 #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
940 
941 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
942 /** Qualifier to warn when the return value of a function is not used */
943 #define CPL_WARN_UNUSED_RESULT                        __attribute__((warn_unused_result))
944 #else
945 /** Qualifier to warn when the return value of a function is not used */
946 #define CPL_WARN_UNUSED_RESULT
947 #endif
948 
949 #if defined(__GNUC__) && __GNUC__ >= 4
950 /** Qualifier for an argument that is unused */
951 #  define CPL_UNUSED __attribute((__unused__))
952 #else
953 /* TODO: add cases for other compilers */
954 /** Qualifier for an argument that is unused */
955 #  define CPL_UNUSED
956 #endif
957 
958 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
959 /** Qualifier for a function that does not return at all (terminates the process) */
960 #define CPL_NO_RETURN                                __attribute__((noreturn))
961 #else
962 /** Qualifier for a function that does not return at all (terminates the process) */
963 #define CPL_NO_RETURN
964 #endif
965 
966 /*! @cond Doxygen_Suppress */
967 /* Clang __has_attribute */
968 #ifndef __has_attribute
969   #define __has_attribute(x) 0  // Compatibility with non-clang compilers.
970 #endif
971 
972 /*! @endcond */
973 
974 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
975 /** Qualifier for a function that does not return NULL */
976 #  define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
977 #else
978 /** Qualifier for a function that does not return NULL */
979 #  define CPL_RETURNS_NONNULL
980 #endif
981 
982 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
983 /** restrict keyword to declare that pointers do not alias */
984 #define CPL_RESTRICT __restrict__
985 #else
986 /** restrict keyword to declare that pointers do not alias */
987 #define CPL_RESTRICT
988 #endif
989 
990 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
991 
992 /** To be used in public headers only. For non-public headers or .cpp files,
993  * use override directly. */
994 #  define CPL_OVERRIDE override
995 
996 /** C++11 final qualifier */
997 #  define CPL_FINAL final
998 
999 /** Mark that a class is explicitly recognized as non-final */
1000 #  define CPL_NON_FINAL
1001 
1002 /** Helper to remove the copy and assignment constructors so that the compiler
1003    will not generate the default versions.
1004 
1005    Must be placed in the private section of a class and should be at the end.
1006 */
1007 #  define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
1008     ClassName( const ClassName & ) = delete; \
1009     ClassName &operator=( const ClassName & ) = delete;
1010 
1011 #endif /* __cplusplus */
1012 
1013 #if !defined(DOXYGEN_SKIP)
1014 #if defined(__has_extension)
1015   #if __has_extension(attribute_deprecated_with_message)
1016     /* Clang extension */
1017     #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated(x)))
1018   #else
1019     #define CPL_WARN_DEPRECATED(x)
1020   #endif
1021 #elif defined(__GNUC__)
1022     #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated))
1023 #else
1024   #define CPL_WARN_DEPRECATED(x)
1025 #endif
1026 #endif
1027 
1028 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1029 CPL_C_START
1030 #  if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1031 int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
1032     CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
1033 int snprintf(char *str, size_t size, const char* fmt, ...)
1034     CPL_PRINT_FUNC_FORMAT(3,4)
1035     CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1036 int sprintf(char *str, const char* fmt, ...)
1037     CPL_PRINT_FUNC_FORMAT(2, 3)
1038     CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1039 #  elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1040 int sprintf(char *str, const char* fmt, ...)
1041     CPL_PRINT_FUNC_FORMAT(2, 3)
1042     CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
1043 #  endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
1044 CPL_C_END
1045 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
1046 
1047 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
1048 /*! @cond Doxygen_Suppress */
1049 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1050 /*! @endcond */
1051 #endif
1052 
1053 #if defined(__cplusplus)
1054 /** Returns the size of C style arrays. */
1055 #define CPL_ARRAYSIZE(array) \
1056   ((sizeof(array) / sizeof(*(array))) / \
1057   static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1058 
1059 extern "C++" {
1060 template<class T> static void CPL_IGNORE_RET_VAL(const T&) {}
1061 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
1062 } /* extern "C++" */
1063 
1064 #endif  /* __cplusplus */
1065 
1066 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1067 #define HAVE_GCC_DIAGNOSTIC_PUSH
1068 #endif
1069 
1070 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1071 #define HAVE_GCC_SYSTEM_HEADER
1072 #endif
1073 
1074 #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1075 /** Macro for fallthrough in a switch case construct */
1076 #  define CPL_FALLTHROUGH [[clang::fallthrough]];
1077 #else
1078 /** Macro for fallthrough in a switch case construct */
1079 #  define CPL_FALLTHROUGH
1080 #endif
1081 
1082 /*! @cond Doxygen_Suppress */
1083 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
1084 // a integer is assigned to a bool
1085 // WARNING: use only at compilation time, since it is know to not work
1086 //  at runtime for unknown reasons (crash in MongoDB driver for example)
1087 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1088 extern "C++" {
1089 class MSVCPedanticBool
1090 {
1091 
1092         friend bool operator== (const bool& one, const MSVCPedanticBool& other);
1093         friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
1094 
1095         bool b;
1096         MSVCPedanticBool(int bIn);
1097 
1098     public:
1099         /* b not initialized on purpose in default ctor to flag use. */
1100         /* cppcheck-suppress uninitMemberVar */
1101         MSVCPedanticBool() {}
1102         MSVCPedanticBool(bool bIn) : b(bIn) {}
1103         MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
1104 
1105         MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
1106         MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
1107         MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
1108 
1109         bool operator== (const bool& other) const { return b == other; }
1110         bool operator!= (const bool& other) const { return b != other; }
1111         bool operator< (const bool& other) const { return b < other; }
1112         bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
1113         bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
1114         bool operator< (const MSVCPedanticBool& other) const { return b < other.b; }
1115 
1116         bool operator! () const { return !b; }
1117         operator bool() const { return b; }
1118         operator int() const { return b; }
1119         operator GIntBig() const { return b; }
1120 };
1121 
1122 inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
1123 inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
1124 
1125 /* We must include all C++ stuff before to avoid issues with templates that use bool */
1126 #include <vector>
1127 #include <map>
1128 #include <set>
1129 #include <string>
1130 #include <cstddef>
1131 #include <limits>
1132 #include <sstream>
1133 #include <fstream>
1134 #include <algorithm>
1135 #include <functional>
1136 #include <memory>
1137 #include <queue>
1138 #include <mutex>
1139 #include <unordered_map>
1140 #include <thread>
1141 #include <unordered_set>
1142 #include <complex>
1143 #include <iomanip>
1144 
1145 } /* extern C++ */
1146 
1147 #undef FALSE
1148 #define FALSE false
1149 #undef TRUE
1150 #define TRUE true
1151 
1152 /* In the very few cases we really need a "simple" type, fallback to bool */
1153 #define EMULATED_BOOL int
1154 
1155 /* Use our class instead of bool */
1156 #define bool MSVCPedanticBool
1157 
1158 /* "volatile bool" with the below substitution doesn't really work. */
1159 /* Just for the sake of the debug, we don't really need volatile */
1160 #define VOLATILE_BOOL bool
1161 
1162 #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1163 
1164 #ifndef FALSE
1165 #  define FALSE 0
1166 #endif
1167 
1168 #ifndef TRUE
1169 #  define TRUE 1
1170 #endif
1171 
1172 #define EMULATED_BOOL bool
1173 #define VOLATILE_BOOL volatile bool
1174 
1175 #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1176 
1177 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1178 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1179 #else
1180 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1181 #endif
1182 
1183 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && defined(GDAL_COMPILATION)
1184 extern "C++" {
1185 template<class C, class A, class B>
1186 CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1187 inline C CPLUnsanitizedAdd(A a, B b)
1188 {
1189     return a + b;
1190 }
1191 }
1192 #endif
1193 
1194 /*! @endcond */
1195 
1196 /*! @cond Doxygen_Suppress */
1197 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1198 #define CPL_NULLPTR nullptr
1199 #else
1200 #define CPL_NULLPTR NULL
1201 #endif
1202 /*! @endcond */
1203 
1204 /* This typedef is for C functions that take char** as argument, but */
1205 /* with the semantics of a const list. In C, char** is not implicitly cast to */
1206 /* const char* const*, contrary to C++. So when seen for C++, it is OK */
1207 /* to expose the prototypes as const char* const*, but for C we keep the */
1208 /* historical definition to avoid warnings. */
1209 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
1210 /** Type of a constant null-terminated list of nul terminated strings.
1211  * Seen as char** from C and const char* const* from C++ */
1212 typedef const char* const* CSLConstList;
1213 #else
1214 /** Type of a constant null-terminated list of nul terminated strings.
1215  * Seen as char** from C and const char* const* from C++ */
1216 typedef char** CSLConstList;
1217 #endif
1218 
1219 #endif /* ndef CPL_BASE_H_INCLUDED */
1220