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