codecs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Function parameters for encoding functions:
  2. #define BASE64_ENC_PARAMS \
  3. ( struct base64_state *state \
  4. , const char *src \
  5. , size_t srclen \
  6. , char *out \
  7. , size_t *outlen \
  8. )
  9. // Function parameters for decoding functions:
  10. #define BASE64_DEC_PARAMS \
  11. ( struct base64_state *state \
  12. , const char *src \
  13. , size_t srclen \
  14. , char *out \
  15. , size_t *outlen \
  16. )
  17. // Function signature for encoding functions:
  18. #define BASE64_ENC_FUNCTION(arch) \
  19. void \
  20. base64_stream_encode_ ## arch \
  21. BASE64_ENC_PARAMS
  22. // Function signature for decoding functions:
  23. #define BASE64_DEC_FUNCTION(arch) \
  24. int \
  25. base64_stream_decode_ ## arch \
  26. BASE64_DEC_PARAMS
  27. // Cast away unused variable, silence compiler:
  28. #define UNUSED(x) ((void)(x))
  29. // Stub function when encoder arch unsupported:
  30. #define BASE64_ENC_STUB \
  31. UNUSED(state); \
  32. UNUSED(src); \
  33. UNUSED(srclen); \
  34. UNUSED(out); \
  35. \
  36. *outlen = 0;
  37. // Stub function when decoder arch unsupported:
  38. #define BASE64_DEC_STUB \
  39. UNUSED(state); \
  40. UNUSED(src); \
  41. UNUSED(srclen); \
  42. UNUSED(out); \
  43. UNUSED(outlen); \
  44. \
  45. return -1;
  46. struct codec
  47. {
  48. void (* enc) BASE64_ENC_PARAMS;
  49. int (* dec) BASE64_DEC_PARAMS;
  50. };
  51. // Define machine endianness. This is for GCC:
  52. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  53. #define BASE64_LITTLE_ENDIAN 1
  54. #else
  55. #define BASE64_LITTLE_ENDIAN 0
  56. #endif
  57. // This is for Clang:
  58. #ifdef __LITTLE_ENDIAN__
  59. #define BASE64_LITTLE_ENDIAN 1
  60. #endif
  61. #ifdef __BIG_ENDIAN__
  62. #define BASE64_LITTLE_ENDIAN 0
  63. #endif
  64. // Endian conversion functions:
  65. #if BASE64_LITTLE_ENDIAN
  66. #if defined(_MSC_VER)
  67. // Microsoft Visual C++:
  68. #define cpu_to_be32(x) _byteswap_ulong(x)
  69. #define cpu_to_be64(x) _byteswap_uint64(x)
  70. #define be32_to_cpu(x) _byteswap_ulong(x)
  71. #define be64_to_cpu(x) _byteswap_uint64(x)
  72. #else
  73. // GCC and Clang:
  74. #define cpu_to_be32(x) __builtin_bswap32(x)
  75. #define cpu_to_be64(x) __builtin_bswap64(x)
  76. #define be32_to_cpu(x) __builtin_bswap32(x)
  77. #define be64_to_cpu(x) __builtin_bswap64(x)
  78. #endif
  79. #else
  80. // No conversion needed:
  81. #define cpu_to_be32(x) (x)
  82. #define cpu_to_be64(x) (x)
  83. #define be32_to_cpu(x) (x)
  84. #define be64_to_cpu(x) (x)
  85. #endif
  86. // detect word size
  87. #ifdef _INTEGRAL_MAX_BITS
  88. #define BASE64_WORDSIZE _INTEGRAL_MAX_BITS
  89. #else
  90. #define BASE64_WORDSIZE __WORDSIZE
  91. #endif
  92. // end-of-file definitions
  93. // Almost end-of-file when waiting for the last '=' character:
  94. #define BASE64_AEOF 1
  95. // End-of-file when stream end has been reached or invalid input provided:
  96. #define BASE64_EOF 2
  97. // GCC 7 defaults to issuing a warning for fallthrough in switch statements,
  98. // unless the fallthrough cases are marked with an attribute. As we use
  99. // fallthrough deliberately, define an alias for the attribute:
  100. #if __GNUC__ >= 7
  101. #define BASE64_FALLTHROUGH __attribute__((fallthrough));
  102. #else
  103. #define BASE64_FALLTHROUGH
  104. #endif
  105. extern void codec_choose (struct codec *, int flags);
  106. // These tables are used by all codecs
  107. // for fallback plain encoding/decoding:
  108. extern const uint8_t base64_table_enc[];
  109. extern const uint8_t base64_table_dec[];
  110. extern const uint32_t base64_table_dec_d0[];
  111. extern const uint32_t base64_table_dec_d1[];
  112. extern const uint32_t base64_table_dec_d2[];
  113. extern const uint32_t base64_table_dec_d3[];