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