env.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef BASE64_ENV_H
  2. #define BASE64_ENV_H
  3. // This header file contains macro definitions that describe certain aspects of
  4. // the compile-time environment. Compatibility and portability macros go here.
  5. // Define machine endianness. This is for GCC:
  6. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  7. # define BASE64_LITTLE_ENDIAN 1
  8. #else
  9. # define BASE64_LITTLE_ENDIAN 0
  10. #endif
  11. // This is for Clang:
  12. #ifdef __LITTLE_ENDIAN__
  13. # define BASE64_LITTLE_ENDIAN 1
  14. #endif
  15. #ifdef __BIG_ENDIAN__
  16. # define BASE64_LITTLE_ENDIAN 0
  17. #endif
  18. // MSVC++ needs intrin.h for _byteswap_uint64 (issue #68):
  19. #if BASE64_LITTLE_ENDIAN && defined(_MSC_VER)
  20. # include <intrin.h>
  21. #endif
  22. // Endian conversion functions:
  23. #if BASE64_LITTLE_ENDIAN
  24. # ifdef _MSC_VER
  25. // Microsoft Visual C++:
  26. # define BASE64_HTOBE32(x) _byteswap_ulong(x)
  27. # define BASE64_HTOBE64(x) _byteswap_uint64(x)
  28. # else
  29. // GCC and Clang:
  30. # define BASE64_HTOBE32(x) __builtin_bswap32(x)
  31. # define BASE64_HTOBE64(x) __builtin_bswap64(x)
  32. # endif
  33. #else
  34. // No conversion needed:
  35. # define BASE64_HTOBE32(x) (x)
  36. # define BASE64_HTOBE64(x) (x)
  37. #endif
  38. // Detect word size:
  39. #if defined (__x86_64__)
  40. // This also works for the x32 ABI, which has a 64-bit word size.
  41. # define BASE64_WORDSIZE 64
  42. #elif defined (_INTEGRAL_MAX_BITS)
  43. # define BASE64_WORDSIZE _INTEGRAL_MAX_BITS
  44. #elif defined (__WORDSIZE)
  45. # define BASE64_WORDSIZE __WORDSIZE
  46. #elif defined (__SIZE_WIDTH__)
  47. # define BASE64_WORDSIZE __SIZE_WIDTH__
  48. #else
  49. # error BASE64_WORDSIZE_NOT_DEFINED
  50. #endif
  51. // End-of-file definitions.
  52. // Almost end-of-file when waiting for the last '=' character:
  53. #define BASE64_AEOF 1
  54. // End-of-file when stream end has been reached or invalid input provided:
  55. #define BASE64_EOF 2
  56. // GCC 7 defaults to issuing a warning for fallthrough in switch statements,
  57. // unless the fallthrough cases are marked with an attribute. As we use
  58. // fallthrough deliberately, define an alias for the attribute:
  59. #if __GNUC__ >= 7
  60. # define BASE64_FALLTHROUGH __attribute__((fallthrough));
  61. #else
  62. # define BASE64_FALLTHROUGH
  63. #endif
  64. #endif // BASE64_ENV_H