libbase64.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef LIBBASE64_H
  2. #define LIBBASE64_H
  3. #include <stddef.h> /* size_t */
  4. #if defined(_WIN32) || defined(__CYGWIN__)
  5. #define BASE64_SYMBOL_IMPORT __declspec(dllimport)
  6. #define BASE64_SYMBOL_EXPORT __declspec(dllexport)
  7. #define BASE64_SYMBOL_PRIVATE
  8. #elif __GNUC__ >= 4
  9. #define BASE64_SYMBOL_IMPORT __attribute__ ((visibility ("default")))
  10. #define BASE64_SYMBOL_EXPORT __attribute__ ((visibility ("default")))
  11. #define BASE64_SYMBOL_PRIVATE __attribute__ ((visibility ("hidden")))
  12. #else
  13. #define BASE64_SYMBOL_IMPORT
  14. #define BASE64_SYMBOL_EXPORT
  15. #define BASE64_SYMBOL_PRIVATE
  16. #endif
  17. #if defined(BASE64_STATIC_DEFINE)
  18. #define BASE64_EXPORT
  19. #define BASE64_NO_EXPORT
  20. #else
  21. #if defined(BASE64_EXPORTS) // defined if we are building the shared library
  22. #define BASE64_EXPORT BASE64_SYMBOL_EXPORT
  23. #else
  24. #define BASE64_EXPORT BASE64_SYMBOL_IMPORT
  25. #endif
  26. #define BASE64_NO_EXPORT BASE64_SYMBOL_PRIVATE
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* These are the flags that can be passed in the `flags` argument. The values
  32. * below force the use of a given codec, even if that codec is a no-op in the
  33. * current build. Used in testing. Set to 0 for the default behavior, which is
  34. * runtime feature detection on x86, a compile-time fixed codec on ARM, and
  35. * the plain codec on other platforms: */
  36. #define BASE64_FORCE_AVX2 (1 << 0)
  37. #define BASE64_FORCE_NEON32 (1 << 1)
  38. #define BASE64_FORCE_NEON64 (1 << 2)
  39. #define BASE64_FORCE_PLAIN (1 << 3)
  40. #define BASE64_FORCE_SSSE3 (1 << 4)
  41. #define BASE64_FORCE_SSE41 (1 << 5)
  42. #define BASE64_FORCE_SSE42 (1 << 6)
  43. #define BASE64_FORCE_AVX (1 << 7)
  44. #define BASE64_FORCE_AVX512 (1 << 8)
  45. struct base64_state {
  46. int eof;
  47. int bytes;
  48. int flags;
  49. unsigned char carry;
  50. };
  51. /* Wrapper function to encode a plain string of given length. Output is written
  52. * to *out without trailing zero. Output length in bytes is written to *outlen.
  53. * The buffer in `out` has been allocated by the caller and is at least 4/3 the
  54. * size of the input. See above for `flags`; set to 0 for default operation: */
  55. void BASE64_EXPORT base64_encode
  56. ( const char *src
  57. , size_t srclen
  58. , char *out
  59. , size_t *outlen
  60. , int flags
  61. ) ;
  62. /* Call this before calling base64_stream_encode() to init the state. See above
  63. * for `flags`; set to 0 for default operation: */
  64. void BASE64_EXPORT base64_stream_encode_init
  65. ( struct base64_state *state
  66. , int flags
  67. ) ;
  68. /* Encodes the block of data of given length at `src`, into the buffer at
  69. * `out`. Caller is responsible for allocating a large enough out-buffer; it
  70. * must be at least 4/3 the size of the in-buffer, but take some margin. Places
  71. * the number of new bytes written into `outlen` (which is set to zero when the
  72. * function starts). Does not zero-terminate or finalize the output. */
  73. void BASE64_EXPORT base64_stream_encode
  74. ( struct base64_state *state
  75. , const char *src
  76. , size_t srclen
  77. , char *out
  78. , size_t *outlen
  79. ) ;
  80. /* Finalizes the output begun by previous calls to `base64_stream_encode()`.
  81. * Adds the required end-of-stream markers if appropriate. `outlen` is modified
  82. * and will contain the number of new bytes written at `out` (which will quite
  83. * often be zero). */
  84. void BASE64_EXPORT base64_stream_encode_final
  85. ( struct base64_state *state
  86. , char *out
  87. , size_t *outlen
  88. ) ;
  89. /* Wrapper function to decode a plain string of given length. Output is written
  90. * to *out without trailing zero. Output length in bytes is written to *outlen.
  91. * The buffer in `out` has been allocated by the caller and is at least 3/4 the
  92. * size of the input. See above for `flags`, set to 0 for default operation: */
  93. int BASE64_EXPORT base64_decode
  94. ( const char *src
  95. , size_t srclen
  96. , char *out
  97. , size_t *outlen
  98. , int flags
  99. ) ;
  100. /* Call this before calling base64_stream_decode() to init the state. See above
  101. * for `flags`; set to 0 for default operation: */
  102. void BASE64_EXPORT base64_stream_decode_init
  103. ( struct base64_state *state
  104. , int flags
  105. ) ;
  106. /* Decodes the block of data of given length at `src`, into the buffer at
  107. * `out`. Caller is responsible for allocating a large enough out-buffer; it
  108. * must be at least 3/4 the size of the in-buffer, but take some margin. Places
  109. * the number of new bytes written into `outlen` (which is set to zero when the
  110. * function starts). Does not zero-terminate the output. Returns 1 if all is
  111. * well, and 0 if a decoding error was found, such as an invalid character.
  112. * Returns -1 if the chosen codec is not included in the current build. Used by
  113. * the test harness to check whether a codec is available for testing. */
  114. int BASE64_EXPORT base64_stream_decode
  115. ( struct base64_state *state
  116. , const char *src
  117. , size_t srclen
  118. , char *out
  119. , size_t *outlen
  120. ) ;
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* LIBBASE64_H */