• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
57 struct base64_state {
58 	int eof;
59 	int bytes;
60 	int flags;
61 	unsigned char carry;
62 };
63 
64 /* Wrapper function to encode 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 4/3 the
67  * size of the input. See above for `flags`; set to 0 for default operation: */
68 void BASE64_EXPORT base64_encode
69 	( const char		*src
70 	, size_t		 srclen
71 	, char			*out
72 	, size_t		*outlen
73 	, int			 flags
74 	) ;
75 
76 /* Call this before calling base64_stream_encode() to init the state. See above
77  * for `flags`; set to 0 for default operation: */
78 void BASE64_EXPORT base64_stream_encode_init
79 	( struct base64_state	*state
80 	, int			 flags
81 	) ;
82 
83 /* Encodes the block of data of given length at `src`, into the buffer at
84  * `out`. Caller is responsible for allocating a large enough out-buffer; it
85  * must be at least 4/3 the size of the in-buffer, but take some margin. Places
86  * the number of new bytes written into `outlen` (which is set to zero when the
87  * function starts). Does not zero-terminate or finalize the output. */
88 void BASE64_EXPORT base64_stream_encode
89 	( struct base64_state	*state
90 	, const char		*src
91 	, size_t		 srclen
92 	, char			*out
93 	, size_t		*outlen
94 	) ;
95 
96 /* Finalizes the output begun by previous calls to `base64_stream_encode()`.
97  * Adds the required end-of-stream markers if appropriate. `outlen` is modified
98  * and will contain the number of new bytes written at `out` (which will quite
99  * often be zero). */
100 void BASE64_EXPORT base64_stream_encode_final
101 	( struct base64_state	*state
102 	, char			*out
103 	, size_t		*outlen
104 	) ;
105 
106 /* Wrapper function to decode a plain string of given length. Output is written
107  * to *out without trailing zero. Output length in bytes is written to *outlen.
108  * The buffer in `out` has been allocated by the caller and is at least 3/4 the
109  * size of the input. See above for `flags`, set to 0 for default operation: */
110 int BASE64_EXPORT base64_decode
111 	( const char		*src
112 	, size_t		 srclen
113 	, char			*out
114 	, size_t		*outlen
115 	, int			 flags
116 	) ;
117 
118 /* Call this before calling base64_stream_decode() to init the state. See above
119  * for `flags`; set to 0 for default operation: */
120 void BASE64_EXPORT base64_stream_decode_init
121 	( struct base64_state	*state
122 	, int			 flags
123 	) ;
124 
125 /* Decodes the block of data of given length at `src`, into the buffer at
126  * `out`. Caller is responsible for allocating a large enough out-buffer; it
127  * must be at least 3/4 the size of the in-buffer, but take some margin. Places
128  * the number of new bytes written into `outlen` (which is set to zero when the
129  * function starts). Does not zero-terminate the output. Returns 1 if all is
130  * well, and 0 if a decoding error was found, such as an invalid character.
131  * Returns -1 if the chosen codec is not included in the current build. Used by
132  * the test harness to check whether a codec is available for testing. */
133 int BASE64_EXPORT base64_stream_decode
134 	( struct base64_state	*state
135 	, const char		*src
136 	, size_t		 srclen
137 	, char			*out
138 	, size_t		*outlen
139 	) ;
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #endif /* LIBBASE64_H */
146