7 #include <openssl/sha.h> 8 #include <openssl/hmac.h> 9 #include <openssl/evp.h> 10 #include <openssl/bio.h> 11 #include <openssl/buffer.h> 15 static const char gdtable[] =
17 64, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
18 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
19 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,
20 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,
21 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
22 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,
23 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
24 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128,
25 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
26 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
27 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
28 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
29 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
30 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
31 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
32 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
39 for ( i = 0; i < buflen; ++i )
41 if ( gdtable[(
int)bufcoded[i]] != 0x80 )
44 return (((count + 3) / 4) * 3) + 1;
49 BIO *b64 = BIO_new(BIO_f_base64());
50 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
51 BIO *bmem = BIO_new_mem_buf((
void*)input, length);
52 bmem = BIO_push(b64, bmem);
54 int ret = BIO_read(bmem, decoded, length);
62 return ((len + 2) / 3 * 4) + 1;
73 BIO *b64 = BIO_new(BIO_f_base64());
74 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
75 BIO *bmem = BIO_new(BIO_s_mem());
76 b64 = BIO_push(b64, bmem);
78 BIO_write(b64, input, length);
82 BIO_get_mem_ptr(b64, &bptr);
83 memcpy(encoded, bptr->data, bptr->length);
84 encoded[bptr->length] = 0;
85 int ret = bptr->length+1;
92 #warning OpenSSL functions for base64 not available 121 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
122 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
123 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
124 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
125 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
126 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
127 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
128 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
129 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
130 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
131 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
132 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
133 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
134 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
135 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
136 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
142 register const unsigned char *bufin;
143 register int nprbytes;
145 bufin = (
const unsigned char *) bufcoded;
146 while (
pr2six[*(bufin++)] <= 63 && buflen-- > 0);
148 nprbytes = (bufin - (
const unsigned char *) bufcoded) - 1;
149 nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
151 return nbytesdecoded + 1;
161 bufplain[len] =
'\0';
166 const char *bufcoded,
int buflen)
169 register const unsigned char *bufin;
170 register unsigned char *bufout;
171 register int nprbytes;
173 bufin = (
const unsigned char *) bufcoded;
174 while (
pr2six[*(bufin++)] <= 63 && buflen-- > 0);
175 nprbytes = (bufin - (
const unsigned char *) bufcoded) - 1;
176 nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
178 bufout = (
unsigned char *) bufplain;
179 bufin = (
const unsigned char *) bufcoded;
181 while (nprbytes > 4) {
183 (
unsigned char) (
pr2six[*bufin] << 2 |
pr2six[bufin[1]] >> 4);
185 (
unsigned char) (
pr2six[bufin[1]] << 4 |
pr2six[bufin[2]] >> 2);
187 (
unsigned char) (
pr2six[bufin[2]] << 6 |
pr2six[bufin[3]]);
195 (
unsigned char) (
pr2six[*bufin] << 2 |
pr2six[bufin[1]] >> 4);
199 (
unsigned char) (
pr2six[bufin[1]] << 4 |
pr2six[bufin[2]] >> 2);
203 (
unsigned char) (
pr2six[bufin[2]] << 6 |
pr2six[bufin[3]]);
206 nbytesdecoded -= (4 - (int)nprbytes) & 3;
207 return nbytesdecoded;
211 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
215 return ((len + 2) / 3 * 4) + 1;
226 const unsigned char *
string,
int len)
232 for (i = 0; i < len - 2; i += 3) {
233 *p++ =
basis_64[(
string[i] >> 2) & 0x3F];
234 *p++ =
basis_64[((
string[i] & 0x3) << 4) |
235 ((int) (
string[i + 1] & 0xF0) >> 4)];
236 *p++ =
basis_64[((
string[i + 1] & 0xF) << 2) |
237 ((int) (
string[i + 2] & 0xC0) >> 6)];
238 *p++ =
basis_64[
string[i + 2] & 0x3F];
241 *p++ =
basis_64[(
string[i] >> 2) & 0x3F];
242 if (i == (len - 1)) {
243 *p++ =
basis_64[((
string[i] & 0x3) << 4)];
247 *p++ =
basis_64[((
string[i] & 0x3) << 4) |
248 ((int) (
string[i + 1] & 0xF0) >> 4)];
249 *p++ =
basis_64[((
string[i + 1] & 0xF) << 2)];
255 return (
int)(p - encoded);
268 out = (
char *) malloc(
sizeof(
char) * (elen + 1));
282 out = (
char *) malloc(
sizeof(
char) * (elen + 1));
int apr_base64_encode_binary(char *encoded, const unsigned char *string, int len)
char * b64_encode(char *buf, int len)
int apr_base64_decode(char *bufplain, const char *bufcoded, int buflen)
int apr_base64_decode_len(const char *bufcoded, int buflen)
int apr_base64_encode(char *encoded, const char *string, int len)
static const unsigned char pr2six[256]
char * b64_decode(char *buf)
int apr_base64_encode_len(int len)
int apr_base64_decode_binary(unsigned char *bufplain, const char *bufcoded, int buflen)
static const char basis_64[]