| 1 |
#ifndef BASE64_ENV_H |
| 2 |
#define BASE64_ENV_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
// This header file contains macro definitions that describe certain aspects of |
| 7 |
// the compile-time environment. Compatibility and portability macros go here. |
| 8 |
|
| 9 |
// Define machine endianness. This is for GCC: |
| 10 |
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 11 |
# define BASE64_LITTLE_ENDIAN 1 |
| 12 |
#else |
| 13 |
# define BASE64_LITTLE_ENDIAN 0 |
| 14 |
#endif |
| 15 |
|
| 16 |
// This is for Clang: |
| 17 |
#ifdef __LITTLE_ENDIAN__ |
| 18 |
# define BASE64_LITTLE_ENDIAN 1 |
| 19 |
#endif |
| 20 |
|
| 21 |
#ifdef __BIG_ENDIAN__ |
| 22 |
# define BASE64_LITTLE_ENDIAN 0 |
| 23 |
#endif |
| 24 |
|
| 25 |
// MSVC++ needs intrin.h for _byteswap_uint64 (issue #68): |
| 26 |
#if BASE64_LITTLE_ENDIAN && defined(_MSC_VER) |
| 27 |
# include <intrin.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
// Endian conversion functions: |
| 31 |
#if BASE64_LITTLE_ENDIAN |
| 32 |
# ifdef _MSC_VER |
| 33 |
// Microsoft Visual C++: |
| 34 |
# define BASE64_HTOBE32(x) _byteswap_ulong(x) |
| 35 |
# define BASE64_HTOBE64(x) _byteswap_uint64(x) |
| 36 |
# else |
| 37 |
// GCC and Clang: |
| 38 |
# define BASE64_HTOBE32(x) __builtin_bswap32(x) |
| 39 |
# define BASE64_HTOBE64(x) __builtin_bswap64(x) |
| 40 |
# endif |
| 41 |
#else |
| 42 |
// No conversion needed: |
| 43 |
# define BASE64_HTOBE32(x) (x) |
| 44 |
# define BASE64_HTOBE64(x) (x) |
| 45 |
#endif |
| 46 |
|
| 47 |
// Detect word size: |
| 48 |
#if defined (__x86_64__) |
| 49 |
// This also works for the x32 ABI, which has a 64-bit word size. |
| 50 |
# define BASE64_WORDSIZE 64 |
| 51 |
#elif SIZE_MAX == UINT32_MAX |
| 52 |
# define BASE64_WORDSIZE 32 |
| 53 |
#elif SIZE_MAX == UINT64_MAX |
| 54 |
# define BASE64_WORDSIZE 64 |
| 55 |
#else |
| 56 |
# error BASE64_WORDSIZE_NOT_DEFINED |
| 57 |
#endif |
| 58 |
|
| 59 |
// End-of-file definitions. |
| 60 |
// Almost end-of-file when waiting for the last '=' character: |
| 61 |
#define BASE64_AEOF 1 |
| 62 |
// End-of-file when stream end has been reached or invalid input provided: |
| 63 |
#define BASE64_EOF 2 |
| 64 |
|
| 65 |
// GCC 7 defaults to issuing a warning for fallthrough in switch statements, |
| 66 |
// unless the fallthrough cases are marked with an attribute. As we use |
| 67 |
// fallthrough deliberately, define an alias for the attribute: |
| 68 |
#if __GNUC__ >= 7 |
| 69 |
# define BASE64_FALLTHROUGH __attribute__((fallthrough)); |
| 70 |
#else |
| 71 |
# define BASE64_FALLTHROUGH |
| 72 |
#endif |
| 73 |
|
| 74 |
// Declare macros to ensure that functions that are intended to be inlined, are |
| 75 |
// actually inlined, even when no optimization is applied. A lot of inner loop |
| 76 |
// code is factored into separate functions for reasons of readability, but |
| 77 |
// that code should always be inlined (and optimized) in the main loop. |
| 78 |
#ifdef _MSC_VER |
| 79 |
# define BASE64_FORCE_INLINE __forceinline |
| 80 |
#else |
| 81 |
# define BASE64_FORCE_INLINE inline __attribute__((always_inline)) |
| 82 |
#endif |
| 83 |
|
| 84 |
#endif // BASE64_ENV_H |