GDAL
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
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 
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) || \
78  (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
79 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
80 #endif
81 
82 #if !defined(SIZEOF_VOIDP)
83 #error "Unexpected value for SIZEOF_VOIDP"
84 #endif
85 
86 /* ==================================================================== */
87 /* This will disable most WIN32 stuff in a Cygnus build which */
88 /* defines unix to 1. */
89 /* ==================================================================== */
90 
91 #ifdef unix
92 #undef WIN32
93 #endif
94 
96 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
97 #define _LARGEFILE64_SOURCE 1
98 #endif
99 
100 /* ==================================================================== */
101 /* If iconv() is available use extended recoding module. */
102 /* Stub implementation is always compiled in, because it works */
103 /* faster than iconv() for encodings it supports. */
104 /* ==================================================================== */
105 
106 #if defined(HAVE_ICONV)
107 #define CPL_RECODE_ICONV
108 #endif
109 
110 #define CPL_RECODE_STUB
111 
113 /* ==================================================================== */
114 /* MinGW stuff */
115 /* ==================================================================== */
116 
117 /* We need __MSVCRT_VERSION__ >= 0x0700 to have "_aligned_malloc" */
118 /* Latest versions of mingw32 define it, but with older ones, */
119 /* we need to define it manually */
120 #if defined(__MINGW32__)
121 #ifndef __MSVCRT_VERSION__
122 #define __MSVCRT_VERSION__ 0x0700
123 #endif
124 #endif
125 
126 /* Needed for std=c11 on Solaris to have strcasecmp() */
127 #if defined(GDAL_COMPILATION) && defined(__sun__) && \
128  (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
129 #ifdef _XOPEN_SOURCE
130 #undef _XOPEN_SOURCE
131 #endif
132 #define _XOPEN_SOURCE 600
133 #endif
134 
135 /* ==================================================================== */
136 /* Standard include files. */
137 /* ==================================================================== */
138 
139 #include <stdio.h>
140 #include <stdlib.h>
141 #include <math.h>
142 #include <stdarg.h>
143 #include <string.h>
144 #include <ctype.h>
145 #include <limits.h>
146 
147 #include <time.h>
148 
149 #include <errno.h>
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 /* ==================================================================== */
164 /* Base portability stuff ... this stuff may need to be */
165 /* modified for new platforms. */
166 /* ==================================================================== */
167 
168 /* -------------------------------------------------------------------- */
169 /* Which versions of C++ are available. */
170 /* -------------------------------------------------------------------- */
171 
172 /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
173 /* as a minimum */
174 
175 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
176 #if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
177 #error Must have C++11 or newer.
178 #endif
179 #if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
180 #define HAVE_CXX14 1
181 #endif
182 #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
183 #define HAVE_CXX17 1
184 #endif
185 #endif /* __cplusplus */
186 
187 /*---------------------------------------------------------------------
188  * types for 16 and 32 bits integers, etc...
189  *--------------------------------------------------------------------*/
190 #if UINT_MAX == 65535
191 typedef long GInt32;
192 typedef unsigned long GUInt32;
193 #else
194 
195 typedef int GInt32;
197 typedef unsigned int GUInt32;
198 #endif
199 
201 typedef short GInt16;
203 typedef unsigned short GUInt16;
205 typedef unsigned char GByte;
207 typedef signed char GInt8;
208 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef
209  * bool GBool" */
210 /* in include/poppler/goo/gtypes.h */
211 #ifndef CPL_GBOOL_DEFINED
212 
213 #define CPL_GBOOL_DEFINED
214 
216 typedef int GBool;
217 #endif
218 
220 #ifdef __cplusplus
221 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
222 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
223 #else
224 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
225 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
226 #endif
227 
229 /* -------------------------------------------------------------------- */
230 /* 64bit support */
231 /* -------------------------------------------------------------------- */
232 
235 typedef long long GIntBig;
238 typedef unsigned long long GUIntBig;
239 
241 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
242 
243 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
244 
245 #define GUINTBIG_MAX \
246  ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
247 
249 #define CPL_HAS_GINT64 1
250 
252 /* Note: we might want to use instead int64_t / uint64_t if they are available
253  */
254 
256 typedef GIntBig GInt64;
259 
261 #define GINT64_MIN GINTBIG_MIN
262 
263 #define GINT64_MAX GINTBIG_MAX
264 
265 #define GUINT64_MAX GUINTBIG_MAX
266 
267 #if SIZEOF_VOIDP > 8
268 #include <stddef.h> // ptrdiff_t
270 typedef ptrdiff_t GPtrDiff_t;
271 #elif SIZEOF_VOIDP == 8
272 
273 typedef GIntBig GPtrDiff_t;
274 #else
275 
276 typedef int GPtrDiff_t;
277 #endif
278 
279 #ifdef GDAL_COMPILATION
280 #include <stdint.h>
281 typedef uintptr_t GUIntptr_t;
282 #define CPL_IS_ALIGNED(ptr, quant) \
283  ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void *, ptr)) % \
284  (quant)) == 0)
285 
286 #endif
287 
288 #if (defined(__MSVCRT__) && !(defined(__MINGW64__) && __GNUC__ >= 10)) || \
289  (defined(WIN32) && defined(_MSC_VER))
290 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
291 #else
292 
293 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
294 #endif
295 
297 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
298 
299 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
300 
302 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
303 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
304 #else
305 #define CPL_INT64_FITS_ON_INT32(x) \
306  (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
307 #endif
308 
310 /* ==================================================================== */
311 /* Other standard services. */
312 /* ==================================================================== */
313 #ifdef __cplusplus
314 
315 #define CPL_C_START \
316  extern "C" \
317  {
318 
319 #define CPL_C_END }
320 #else
321 #define CPL_C_START
322 #define CPL_C_END
323 #endif
324 
325 #ifndef CPL_DLL
326 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
327 #ifdef GDAL_COMPILATION
328 #define CPL_DLL __declspec(dllexport)
329 #else
330 #define CPL_DLL
331 #endif
332 #define CPL_INTERNAL
333 #else
334 #if defined(USE_GCC_VISIBILITY_FLAG)
335 #define CPL_DLL __attribute__((visibility("default")))
336 #if !defined(__MINGW32__)
337 #define CPL_INTERNAL __attribute__((visibility("hidden")))
338 #else
339 #define CPL_INTERNAL
340 #endif
341 #else
342 #define CPL_DLL
343 #define CPL_INTERNAL
344 #endif
345 #endif
346 
347 // Marker for unstable API
348 #define CPL_UNSTABLE_API CPL_DLL
349 
350 #endif
351 
353 /* Should optional (normally private) interfaces be exported? */
354 #ifdef CPL_OPTIONAL_APIS
355 #define CPL_ODLL CPL_DLL
356 #else
357 #define CPL_ODLL
358 #endif
359 
361 #ifndef CPL_STDCALL
362 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
363 #define CPL_STDCALL __stdcall
364 #else
365 #define CPL_STDCALL
366 #endif
367 #endif
368 
370 #ifdef _MSC_VER
371 #define FORCE_CDECL __cdecl
372 #else
373 #define FORCE_CDECL
374 #endif
375 
378 /* TODO : support for other compilers needed */
379 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
380 #define HAS_CPL_INLINE 1
381 #define CPL_INLINE __inline
382 #elif defined(__SUNPRO_CC)
383 #define HAS_CPL_INLINE 1
384 #define CPL_INLINE inline
385 #else
386 #define CPL_INLINE
387 #endif
388 
390 #ifndef MAX
391 
392 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
393 
394 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
395 #endif
396 
397 #ifndef ABS
398 
399 #define ABS(x) (((x) < 0) ? (-1 * (x)) : (x))
400 #endif
401 
402 #ifndef M_PI
403 
404 #define M_PI 3.14159265358979323846
405 /* 3.1415926535897932384626433832795 */
406 #endif
407 
408 /* -------------------------------------------------------------------- */
409 /* Macro to test equality of two floating point values. */
410 /* We use fabs() function instead of ABS() macro to avoid side */
411 /* effects. */
412 /* -------------------------------------------------------------------- */
414 #ifndef CPLIsEqual
415 #define CPLIsEqual(x, y) (fabs((x) - (y)) < 0.0000000000001)
416 #endif
417 
419 /* -------------------------------------------------------------------- */
420 /* Provide macros for case insensitive string comparisons. */
421 /* -------------------------------------------------------------------- */
422 #ifndef EQUAL
423 
424 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
425 
426 static inline int CPL_afl_friendly_memcmp(const void *ptr1, const void *ptr2,
427  size_t len)
428  __attribute__((always_inline));
429 
430 static inline int CPL_afl_friendly_memcmp(const void *ptr1, const void *ptr2,
431  size_t len)
432 {
433  const unsigned char *bptr1 = (const unsigned char *)ptr1;
434  const unsigned char *bptr2 = (const unsigned char *)ptr2;
435  while (len--)
436  {
437  unsigned char b1 = *(bptr1++);
438  unsigned char b2 = *(bptr2++);
439  if (b1 != b2)
440  return b1 - b2;
441  }
442  return 0;
443 }
444 
445 static inline int CPL_afl_friendly_strcmp(const char *ptr1, const char *ptr2)
446  __attribute__((always_inline));
447 
448 static inline int CPL_afl_friendly_strcmp(const char *ptr1, const char *ptr2)
449 {
450  const unsigned char *usptr1 = (const unsigned char *)ptr1;
451  const unsigned char *usptr2 = (const unsigned char *)ptr2;
452  while (1)
453  {
454  unsigned char ch1 = *(usptr1++);
455  unsigned char ch2 = *(usptr2++);
456  if (ch1 == 0 || ch1 != ch2)
457  return ch1 - ch2;
458  }
459 }
460 
461 static inline int CPL_afl_friendly_strncmp(const char *ptr1, const char *ptr2,
462  size_t len)
463  __attribute__((always_inline));
464 
465 static inline int CPL_afl_friendly_strncmp(const char *ptr1, const char *ptr2,
466  size_t len)
467 {
468  const unsigned char *usptr1 = (const unsigned char *)ptr1;
469  const unsigned char *usptr2 = (const unsigned char *)ptr2;
470  while (len--)
471  {
472  unsigned char ch1 = *(usptr1++);
473  unsigned char ch2 = *(usptr2++);
474  if (ch1 == 0 || ch1 != ch2)
475  return ch1 - ch2;
476  }
477  return 0;
478 }
479 
480 static inline int CPL_afl_friendly_strcasecmp(const char *ptr1,
481  const char *ptr2)
482  __attribute__((always_inline));
483 
484 static inline int CPL_afl_friendly_strcasecmp(const char *ptr1,
485  const char *ptr2)
486 {
487  const unsigned char *usptr1 = (const unsigned char *)ptr1;
488  const unsigned char *usptr2 = (const unsigned char *)ptr2;
489  while (1)
490  {
491  unsigned char ch1 = *(usptr1++);
492  unsigned char ch2 = *(usptr2++);
493  ch1 = (unsigned char)toupper(ch1);
494  ch2 = (unsigned char)toupper(ch2);
495  if (ch1 == 0 || ch1 != ch2)
496  return ch1 - ch2;
497  }
498 }
499 
500 static inline int CPL_afl_friendly_strncasecmp(const char *ptr1,
501  const char *ptr2, size_t len)
502  __attribute__((always_inline));
503 
504 static inline int CPL_afl_friendly_strncasecmp(const char *ptr1,
505  const char *ptr2, size_t len)
506 {
507  const unsigned char *usptr1 = (const unsigned char *)ptr1;
508  const unsigned char *usptr2 = (const unsigned char *)ptr2;
509  while (len--)
510  {
511  unsigned char ch1 = *(usptr1++);
512  unsigned char ch2 = *(usptr2++);
513  ch1 = (unsigned char)toupper(ch1);
514  ch2 = (unsigned char)toupper(ch2);
515  if (ch1 == 0 || ch1 != ch2)
516  return ch1 - ch2;
517  }
518  return 0;
519 }
520 
521 static inline char *CPL_afl_friendly_strstr(const char *haystack,
522  const char *needle)
523  __attribute__((always_inline));
524 
525 static inline char *CPL_afl_friendly_strstr(const char *haystack,
526  const char *needle)
527 {
528  const char *ptr_haystack = haystack;
529  while (1)
530  {
531  const char *ptr_haystack2 = ptr_haystack;
532  const char *ptr_needle = needle;
533  while (1)
534  {
535  char ch1 = *(ptr_haystack2++);
536  char ch2 = *(ptr_needle++);
537  if (ch2 == 0)
538  return (char *)ptr_haystack;
539  if (ch1 != ch2)
540  break;
541  }
542  if (*ptr_haystack == 0)
543  return NULL;
544  ptr_haystack++;
545  }
546 }
547 
548 #undef strcmp
549 #undef strncmp
550 #define memcmp CPL_afl_friendly_memcmp
551 #define strcmp CPL_afl_friendly_strcmp
552 #define strncmp CPL_afl_friendly_strncmp
553 #define strcasecmp CPL_afl_friendly_strcasecmp
554 #define strncasecmp CPL_afl_friendly_strncasecmp
555 #define strstr CPL_afl_friendly_strstr
556 
557 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
558 
559 #if defined(WIN32)
560 #define STRCASECMP(a, b) (_stricmp(a, b))
561 #define STRNCASECMP(a, b, n) (_strnicmp(a, b, n))
562 #else
563 
564 #define STRCASECMP(a, b) (strcasecmp(a, b))
565 
566 #define STRNCASECMP(a, b, n) (strncasecmp(a, b, n))
567 #endif
568 
569 #define EQUALN(a, b, n) (STRNCASECMP(a, b, n) == 0)
570 
571 #define EQUAL(a, b) (STRCASECMP(a, b) == 0)
572 #endif
573 
574 /*---------------------------------------------------------------------
575  * Does a string "a" start with string "b". Search is case-sensitive or,
576  * with CI, it is a case-insensitive comparison.
577  *--------------------------------------------------------------------- */
578 #ifndef STARTS_WITH_CI
579 
580 #define STARTS_WITH(a, b) (strncmp(a, b, strlen(b)) == 0)
581 
582 #define STARTS_WITH_CI(a, b) EQUALN(a, b, strlen(b))
583 #endif
584 
586 #ifndef CPL_THREADLOCAL
587 #define CPL_THREADLOCAL
588 #endif
589 
591 /* -------------------------------------------------------------------- */
592 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
593 /* are supposed to be macros according to C99, defined in math.h */
594 /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
595 /* the macro is not defined we just assume nothing is infinite. */
596 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
597 /* function but no corresponding macro, but I can live with */
598 /* that since it isn't that important a test. */
599 /* -------------------------------------------------------------------- */
600 #ifdef _MSC_VER
601 #include <float.h>
602 #define CPLIsNan(x) _isnan(x)
603 #define CPLIsInf(x) (!_isnan(x) && !_finite(x))
604 #define CPLIsFinite(x) _finite(x)
605 #elif defined(__GNUC__) && \
606  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
607 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
608 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
609 #define CPLIsNan(x) __builtin_isnan(x)
610 #define CPLIsInf(x) __builtin_isinf(x)
611 #define CPLIsFinite(x) __builtin_isfinite(x)
612 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
613 extern "C++"
614 {
615 #ifndef DOXYGEN_SKIP
616 #include <cmath>
617 #endif
618  static inline int CPLIsNan(float f)
619  {
620  return std::isnan(f);
621  }
622  static inline int CPLIsNan(double f)
623  {
624  return std::isnan(f);
625  }
626  static inline int CPLIsInf(float f)
627  {
628  return std::isinf(f);
629  }
630  static inline int CPLIsInf(double f)
631  {
632  return std::isinf(f);
633  }
634  static inline int CPLIsFinite(float f)
635  {
636  return std::isfinite(f);
637  }
638  static inline int CPLIsFinite(double f)
639  {
640  return std::isfinite(f);
641  }
642 }
643 #else
644 
645 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && \
646  !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
647 /* so to not get warning about conversion from double to float with */
648 /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
649 extern "C++"
650 {
651  static inline int CPLIsNan(float f)
652  {
653  return __isnanf(f);
654  }
655  static inline int CPLIsNan(double f)
656  {
657  return __isnan(f);
658  }
659  static inline int CPLIsInf(float f)
660  {
661  return __isinff(f);
662  }
663  static inline int CPLIsInf(double f)
664  {
665  return __isinf(f);
666  }
667  static inline int CPLIsFinite(float f)
668  {
669  return !__isnanf(f) && !__isinff(f);
670  }
671  static inline int CPLIsFinite(double f)
672  {
673  return !__isnan(f) && !__isinf(f);
674  }
675 }
676 #else
677 #define CPLIsNan(x) isnan(x)
678 #if defined(isinf) || defined(__FreeBSD__)
679 
680 #define CPLIsInf(x) isinf(x)
681 
682 #define CPLIsFinite(x) (!isnan(x) && !isinf(x))
683 #elif defined(__sun__)
684 #include <ieeefp.h>
685 #define CPLIsInf(x) (!finite(x) && !isnan(x))
686 #define CPLIsFinite(x) finite(x)
687 #else
688 #define CPLIsInf(x) (0)
689 #define CPLIsFinite(x) (!isnan(x))
690 #endif
691 #endif
692 #endif
693 
695 /*---------------------------------------------------------------------
696  * CPL_LSB and CPL_MSB
697  * Only one of these 2 macros should be defined and specifies the byte
698  * ordering for the current platform.
699  * This should be defined in the Makefile, but if it is not then
700  * the default is CPL_LSB (Intel ordering, LSB first).
701  *--------------------------------------------------------------------*/
702 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
703 #define CPL_MSB
704 #endif
705 
706 #if !(defined(CPL_LSB) || defined(CPL_MSB))
707 #define CPL_LSB
708 #endif
709 
710 #if defined(CPL_LSB)
711 #define CPL_IS_LSB 1
712 #else
713 #define CPL_IS_LSB 0
714 #endif
715 
717 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
718 
720 extern "C++"
721 {
722 
723  template <bool b> struct CPLStaticAssert
724  {
725  };
726  template <> struct CPLStaticAssert<true>
727  {
728  static void my_function()
729  {
730  }
731  };
732 
733 } /* extern "C++" */
734 
735 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
736 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
737 
738 #else /* __cplusplus */
739 
740 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
741 
742 #endif /* __cplusplus */
743 
745 /*---------------------------------------------------------------------
746  * Little endian <==> big endian byte swap macros.
747  *--------------------------------------------------------------------*/
748 
750 #define CPL_SWAP16(x) \
751  CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | \
752  (CPL_STATIC_CAST(GUInt16, x) >> 8))
753 
754 #if defined(HAVE_GCC_BSWAP)
755 
756 #define CPL_SWAP32(x) \
757  CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
758 
759 #define CPL_SWAP64(x) \
760  CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
761 #elif defined(_MSC_VER)
762 #define CPL_SWAP32(x) \
763  CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
764 #define CPL_SWAP64(x) \
765  CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
766 #else
767 
768 #define CPL_SWAP32(x) \
769  CPL_STATIC_CAST(GUInt32, \
770  ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
771  ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
772  ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
773  ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24))
774 
776 #define CPL_SWAP64(x) \
777  ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) \
778  << 32) | \
779  (CPL_STATIC_CAST(GUInt64, \
780  CPL_SWAP32(CPL_STATIC_CAST( \
781  GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
782 
783 #endif
784 
786 #define CPL_SWAP16PTR(x) \
787  do \
788  { \
789  GUInt16 _n16; \
790  void *_lx = x; \
791  memcpy(&_n16, _lx, 2); \
792  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
793  sizeof(*(x)) == 2); \
794  _n16 = CPL_SWAP16(_n16); \
795  memcpy(_lx, &_n16, 2); \
796  } while (0)
797 
799 #define CPL_SWAP32PTR(x) \
800  do \
801  { \
802  GUInt32 _n32; \
803  void *_lx = x; \
804  memcpy(&_n32, _lx, 4); \
805  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
806  sizeof(*(x)) == 4); \
807  _n32 = CPL_SWAP32(_n32); \
808  memcpy(_lx, &_n32, 4); \
809  } while (0)
810 
812 #define CPL_SWAP64PTR(x) \
813  do \
814  { \
815  GUInt64 _n64; \
816  void *_lx = x; \
817  memcpy(&_n64, _lx, 8); \
818  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
819  sizeof(*(x)) == 8); \
820  _n64 = CPL_SWAP64(_n64); \
821  memcpy(_lx, &_n64, 8); \
822  } while (0)
823 
825 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
826 
827 #ifdef CPL_MSB
828 #define CPL_MSBWORD16(x) (x)
829 #define CPL_LSBWORD16(x) CPL_SWAP16(x)
830 #define CPL_MSBWORD32(x) (x)
831 #define CPL_LSBWORD32(x) CPL_SWAP32(x)
832 #define CPL_MSBPTR16(x) \
833  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
834 #define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
835 #define CPL_MSBPTR32(x) \
836  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
837 #define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
838 #define CPL_MSBPTR64(x) \
839  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
840 #define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
841 #else
842 
843 #define CPL_LSBWORD16(x) (x)
844 
845 #define CPL_MSBWORD16(x) CPL_SWAP16(x)
846 
847 #define CPL_LSBWORD32(x) (x)
848 
849 #define CPL_MSBWORD32(x) CPL_SWAP32(x)
850 
852 #define CPL_LSBPTR16(x) \
853  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
854 
856 #define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
857 
859 #define CPL_LSBPTR32(x) \
860  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
861 
863 #define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
864 
866 #define CPL_LSBPTR64(x) \
867  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
868 
870 #define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
871 #endif
872 
876 #define CPL_LSBINT16PTR(x) \
877  ((*CPL_REINTERPRET_CAST(const GByte *, x)) | \
878  (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 1) << 8))
879 
883 #define CPL_LSBINT32PTR(x) \
884  ((*CPL_REINTERPRET_CAST(const GByte *, x)) | \
885  (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 1) << 8) | \
886  (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 2) << 16) | \
887  (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 3) << 24))
888 
890 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16, CPL_LSBINT16PTR(x))
891 
894 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
895 
897 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
898 
901 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
902 
904 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
905 #ifndef UNREFERENCED_PARAM
906 #ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
907 #define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
908 #else
909 #define UNREFERENCED_PARAM(param) ((void)param)
910 #endif /* UNREFERENCED_PARAMETER */
911 #endif /* UNREFERENCED_PARAM */
912 
914 /***********************************************************************
915  * Define CPL_CVSID() macro. It can be disabled during a build by
916  * defining DISABLE_CVSID in the compiler options.
917  *
918  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
919  * being unused.
920  */
921 
923 #ifndef DISABLE_CVSID
924 #if defined(__GNUC__) && __GNUC__ >= 4
925 #define CPL_CVSID(string) \
926  static const char cpl_cvsid[] __attribute__((used)) = string;
927 #else
928 #define CPL_CVSID(string) \
929  static const char cpl_cvsid[] = string; \
930  static const char *cvsid_aw() \
931  { \
932  return (cvsid_aw() ? NULL : cpl_cvsid); \
933  }
934 #endif
935 #else
936 #define CPL_CVSID(string)
937 #endif
938 
940 /* We exclude mingw64 4.6 which seems to be broken regarding this */
941 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && \
942  !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
943 
944 #define CPL_NULL_TERMINATED __attribute__((__sentinel__))
945 #else
946 
947 #define CPL_NULL_TERMINATED
948 #endif
949 
950 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
951 
952 #define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx) \
953  __attribute__((__format__(__printf__, format_idx, arg_idx)))
954 
955 #define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx) \
956  __attribute__((__format__(__scanf__, format_idx, arg_idx)))
957 #else
958 
959 #define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
960 
961 #define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx)
962 #endif
963 
964 #if defined(_MSC_VER) && \
965  (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
966 #include <sal.h>
969 #define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
970 
972 #define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
973 #else
974 
975 #define CPL_FORMAT_STRING(arg) arg
976 
977 #define CPL_SCANF_FORMAT_STRING(arg) arg
978 #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
979 
980 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
981 
982 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
983 #else
984 
985 #define CPL_WARN_UNUSED_RESULT
986 #endif
987 
988 #if defined(__GNUC__) && __GNUC__ >= 4
989 
990 #define CPL_UNUSED __attribute((__unused__))
991 #else
992 /* TODO: add cases for other compilers */
994 #define CPL_UNUSED
995 #endif
996 
997 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
998 
1000 #define CPL_NO_RETURN __attribute__((noreturn))
1001 #else
1002 
1004 #define CPL_NO_RETURN
1005 #endif
1006 
1008 /* Clang __has_attribute */
1009 #ifndef __has_attribute
1010 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
1011 #endif
1012 
1015 #if ((defined(__GNUC__) && \
1016  (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || \
1017  __has_attribute(returns_nonnull)) && \
1018  !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
1019 
1020 #define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
1021 #else
1022 
1023 #define CPL_RETURNS_NONNULL
1024 #endif
1025 
1026 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
1027 
1028 #define CPL_RESTRICT __restrict__
1029 #else
1030 
1031 #define CPL_RESTRICT
1032 #endif
1033 
1034 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1035 
1038 #define CPL_OVERRIDE override
1039 
1041 #define CPL_FINAL final
1042 
1044 #define CPL_NON_FINAL
1045 
1051 #define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
1052  ClassName(const ClassName &) = delete; \
1053  ClassName &operator=(const ClassName &) = delete;
1054 
1055 #endif /* __cplusplus */
1056 
1057 #if !defined(DOXYGEN_SKIP) && !defined(CPL_WARN_DEPRECATED)
1058 #if defined(__has_extension)
1059 #if __has_extension(attribute_deprecated_with_message)
1060 /* Clang extension */
1061 #define CPL_WARN_DEPRECATED(x) __attribute__((deprecated(x)))
1062 #else
1063 #define CPL_WARN_DEPRECATED(x)
1064 #endif
1065 #elif defined(__GNUC__)
1066 #define CPL_WARN_DEPRECATED(x) __attribute__((deprecated))
1067 #else
1068 #define CPL_WARN_DEPRECATED(x)
1069 #endif
1070 #endif
1071 
1072 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1074 #if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1075 int vsnprintf(char *str, size_t size, const char *fmt, va_list args)
1076  CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
1077 int snprintf(char *str, size_t size, const char *fmt, ...)
1078  CPL_PRINT_FUNC_FORMAT(3, 4)
1079  CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1080 int sprintf(char *str, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3)
1081  CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1082 #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1083 int sprintf(char *str, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3)
1084  CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
1085 #endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
1086 CPL_C_END
1087 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
1088 
1089 #if defined(MAKE_SANITIZE_HAPPY) || \
1090  !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
1091  defined(_M_X64))
1092 
1093 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1094 
1095 #endif
1096 
1097 #if defined(__cplusplus)
1098 #ifndef CPPCHECK
1099 
1100 #define CPL_ARRAYSIZE(array) \
1101  ((sizeof(array) / sizeof(*(array))) / \
1102  static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1103 #else
1104 /* simplified version for cppcheck */
1105 #define CPL_ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
1106 #endif
1107 
1108 extern "C++"
1109 {
1110  template <class T> static void CPL_IGNORE_RET_VAL(const T &)
1111  {
1112  }
1113  inline static bool CPL_TO_BOOL(int x)
1114  {
1115  return x != 0;
1116  }
1117 } /* extern "C++" */
1118 
1119 #endif /* __cplusplus */
1120 
1121 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
1122  (defined(__clang__) && __clang_major__ >= 3)) && \
1123  !defined(_MSC_VER))
1124 #define HAVE_GCC_DIAGNOSTIC_PUSH
1125 #endif
1126 
1127 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && \
1128  !defined(_MSC_VER))
1129 #define HAVE_GCC_SYSTEM_HEADER
1130 #endif
1131 
1132 #if defined(__has_cpp_attribute)
1133 #if __has_cpp_attribute(fallthrough)
1134 
1135 #define CPL_FALLTHROUGH [[fallthrough]];
1136 #endif
1137 #elif defined(__clang__) || __GNUC__ >= 7
1138 
1139 #define CPL_FALLTHROUGH [[clang::fallthrough]];
1140 #endif
1141 #ifndef CPL_FALLTHROUGH
1142 
1143 #define CPL_FALLTHROUGH
1144 #endif
1145 
1148 #ifndef FALSE
1149 #define FALSE 0
1150 #endif
1151 
1152 #ifndef TRUE
1153 #define TRUE 1
1154 #endif
1155 
1156 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1157 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW \
1158  __attribute__((no_sanitize("unsigned-integer-overflow")))
1159 #else
1160 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1161 #endif
1162 
1163 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \
1164  defined(GDAL_COMPILATION)
1165 extern "C++"
1166 {
1167  template <class C, class A, class B>
1168  CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW inline C CPLUnsanitizedAdd(A a, B b)
1169  {
1170  return a + b;
1171  }
1172 }
1173 #endif
1174 
1175 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1176 #define CPL_NULLPTR nullptr
1177 #else
1178 #define CPL_NULLPTR NULL
1179 #endif
1180 
1182 /* This typedef is for C functions that take char** as argument, but */
1183 /* with the semantics of a const list. In C, char** is not implicitly cast to */
1184 /* const char* const*, contrary to C++. So when seen for C++, it is OK */
1185 /* to expose the prototypes as const char* const*, but for C we keep the */
1186 /* historical definition to avoid warnings. */
1187 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \
1188  !defined(DOXYGEN_SKIP)
1189 
1191 typedef const char *const *CSLConstList;
1192 #else
1193 
1195 typedef char **CSLConstList;
1196 #endif
1197 
1198 #endif /* ndef CPL_BASE_H_INCLUDED */
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:959
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:205
GUInt64
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:258
GInt16
short GInt16
Int16 type.
Definition: cpl_port.h:201
CPLsnprintf
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1344
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:256
GPtrDiff_t
int GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:276
GInt8
signed char GInt8
Signed int8 type.
Definition: cpl_port.h:207
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:315
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1195
GUIntBig
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:238
CPLIsNan
#define CPLIsNan(x)
Return whether a floating-pointer number is NaN.
Definition: cpl_port.h:677
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:319
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:235
GBool
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:216
CPLIsInf
#define CPLIsInf(x)
Return whether a floating-pointer number is +/- infinity.
Definition: cpl_port.h:680
GUInt16
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:203
GInt32
int GInt32
Int32 type.
Definition: cpl_port.h:195
CPLIsFinite
#define CPLIsFinite(x)
Return whether a floating-pointer number is finite.
Definition: cpl_port.h:682
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:197