libbase64.h 3.7 KB

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