19 #if defined(_WIN32) || defined(_WIN64) 20 #pragma comment(lib, "crypt32.lib") 26 DWORD dw_out_len = (DWORD)out_len;
27 if ( CryptStringToBinaryA( in, in_len, CRYPT_STRING_BASE64, out, &dw_out_len, NULL, NULL ) )
35 DWORD dw_out_len = (DWORD)out_len;
36 if ( CryptBinaryToStringA( in, in_len, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, out, &dw_out_len ) )
43 #include <openssl/bio.h> 44 #include <openssl/evp.h> 52 BIO *bio, *b64, *b_in, *b_out;
54 b64 = BIO_new(BIO_f_base64());
55 bio = BIO_new(BIO_s_mem());
56 b64 = BIO_push(b64, bio);
57 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
70 rv = BIO_write(b_out, in, (
int)in_len);
75 rv = BIO_read(b_in, out, (
int)out_len);
91 return Base64_encodeDecode( (
char*)out, out_len, in, in_len, 0 );
96 return Base64_encodeDecode( out, out_len, (
const char*)in, in_len, 1 );
103 static const unsigned char BASE64_DECODE_TABLE[] =
105 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
106 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
107 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV, 62,
NV,
NV,
NV, 63,
108 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
NV,
NV,
NV,
NV,
NV,
NV,
109 NV, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
110 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
NV,
NV,
NV,
NV,
NV,
111 NV, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
112 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
NV,
NV,
NV,
NV,
NV,
113 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
114 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
115 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
116 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
117 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
118 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
119 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
120 NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV,
NV, NV
127 while ( in_len > 3u && out_count < out_len )
131 for ( i = 0; i < 4; ++i, ++in )
132 c[i] = BASE64_DECODE_TABLE[(
int)(*in)];
137 *out |= (c[1] & ~0xF) >> 4;
141 if ( out_count < out_len )
144 *out = (c[1] & 0xF) << 4;
147 *out |= (c[2] & ~0x3) >> 2;
151 if ( out_count < out_len )
154 *out = (c[2] & 0x3) << 6;
170 if ( out_count <= out_len )
173 if ( out_count < out_len )
181 static const char BASE64_ENCODE_TABLE[] =
182 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 183 "abcdefghijklmnopqrstuvwxyz" 188 while ( in_len > 0u && out_count < out_len )
191 unsigned char c[] = { 0, 0, 64, 64 };
195 c[0] = (i & ~0x3) >> 2;
198 c[1] = (i & 0x3) << 4;
204 c[1] |= (i & ~0xF) >> 4;
207 c[2] = (i & 0xF) << 2;
213 c[2] |= (i & ~0x3F) >> 6;
224 for ( i = 0; i < 4 && out_count <= out_len; ++i, ++out )
225 *out = BASE64_ENCODE_TABLE[c[i]];
228 if ( out_count <= out_len )
230 if ( out_count < out_len )
243 if ( in && in_len > 1u )
244 pad += ( in[in_len - 2u] ==
'=' ? 1u : 0u );
245 if ( in && in_len > 0u )
246 pad += ( in[in_len - 1u] ==
'=' ? 1u : 0u );
247 return (in_len / 4u * 3u) -
pad;
252 return ((4u * in_len / 3u) + 3u) & ~0x3;
255 #if defined(BASE64_TEST) 259 #define TEST_EXPECT(i,x) if (!(x)) {fprintf( stderr, "failed test: %s (for i == %d)\n", #x, i ); ++fails;} 261 int main(
int argc,
char *argv[])
270 unsigned int fails = 0u;
271 struct _td test_data[] = {
276 {
"paho",
"cGFobw==" },
277 {
"paho ",
"cGFobyA=" },
278 {
"paho w",
"cGFobyB3" },
279 {
"paho wi",
"cGFobyB3aQ==" },
280 {
"paho wit",
"cGFobyB3aXQ=" },
281 {
"paho with",
"cGFobyB3aXRo" },
282 {
"paho with ",
"cGFobyB3aXRoIA==" },
283 {
"paho with w",
"cGFobyB3aXRoIHc=" },
284 {
"paho with we",
"cGFobyB3aXRoIHdl" },
285 {
"paho with web",
"cGFobyB3aXRoIHdlYg==" },
286 {
"paho with webs",
"cGFobyB3aXRoIHdlYnM=" },
287 {
"paho with webso",
"cGFobyB3aXRoIHdlYnNv" },
288 {
"paho with websoc",
"cGFobyB3aXRoIHdlYnNvYw==" },
289 {
"paho with websock",
"cGFobyB3aXRoIHdlYnNvY2s=" },
290 {
"paho with websocke",
"cGFobyB3aXRoIHdlYnNvY2tl" },
291 {
"paho with websocket",
"cGFobyB3aXRoIHdlYnNvY2tldA==" },
292 {
"paho with websockets",
"cGFobyB3aXRoIHdlYnNvY2tldHM=" },
293 {
"paho with websockets.",
"cGFobyB3aXRoIHdlYnNvY2tldHMu" },
294 {
"The quick brown fox jumps over the lazy dog",
295 "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==" },
296 {
"Man is distinguished, not only by his reason, but by this singular passion from\n" 297 "other animals, which is a lust of the mind, that by a perseverance of delight\n" 298 "in the continued and indefatigable generation of knowledge, exceeds the short\n" 299 "vehemence of any carnal pleasure.",
300 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" 301 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" 302 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu" 303 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" 304 "ZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" },
310 while ( test_data[i].in != NULL )
314 r =
Base64_decode( out,
sizeof(out), test_data[i].out, strlen(test_data[i].out) );
315 TEST_EXPECT( i, r == strlen(test_data[i].in) && strncmp(out, test_data[i].in, strlen(test_data[i].in)) == 0 );
321 while ( test_data[i].in != NULL )
323 TEST_EXPECT( i,
Base64_decodeLength(test_data[i].out, strlen(test_data[i].out)) == strlen(test_data[i].in));
329 while ( test_data[i].in != NULL )
333 r =
Base64_encode( out,
sizeof(out), test_data[i].in, strlen(test_data[i].in) );
334 TEST_EXPECT( i, r == strlen(test_data[i].out) && strncmp(out, test_data[i].out, strlen(test_data[i].out)) == 0 );
340 while ( test_data[i].in != NULL )
342 TEST_EXPECT( i,
Base64_encodeLength(test_data[i].in, strlen(test_data[i].in)) == strlen(test_data[i].out) );
347 printf(
"%u test failed!\n", fails );
349 printf(
"all tests passed\n" );
static unsigned char pad[64]
b64_size_t Base64_decode(b64_data_t *out, b64_size_t out_len, const char *in, b64_size_t in_len)
b64_size_t Base64_decodeLength(const char *in, b64_size_t in_len)
b64_size_t Base64_encodeLength(const b64_data_t *in, b64_size_t in_len)
b64_size_t Base64_encode(char *out, b64_size_t out_len, const b64_data_t *in, b64_size_t in_len)
int main(int argc, char **argv)