2 * Base64 encoding/decoding (RFC1341)
3 * Copyright (c) 2005, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 static const unsigned char base64_table
[65] =
21 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 * base64_encode - Base64 encode
25 * @src: Data to be encoded
26 * @len: Length of the data to be encoded
27 * @out_len: Pointer to output length variable, or %NULL if not used
28 * Returns: Allocated buffer of out_len bytes of encoded data,
31 * Caller is responsible for freeing the returned buffer. Returned buffer is
32 * nul terminated to make it easier to use as a C string. The nul terminator is
33 * not included in out_len.
35 unsigned char * base64_encode(const unsigned char *src
, size_t len
,
38 unsigned char *out
, *pos
;
39 const unsigned char *end
, *in
;
43 olen
= len
* 4 / 3 + 4; /* 3-byte blocks to 4-byte */
44 olen
+= olen
/ 72; /* line feeds */
45 olen
++; /* nul termination */
46 out
= os_malloc(olen
);
54 while (end
- in
>= 3) {
55 *pos
++ = base64_table
[in
[0] >> 2];
56 *pos
++ = base64_table
[((in
[0] & 0x03) << 4) | (in
[1] >> 4)];
57 *pos
++ = base64_table
[((in
[1] & 0x0f) << 2) | (in
[2] >> 6)];
58 *pos
++ = base64_table
[in
[2] & 0x3f];
68 *pos
++ = base64_table
[in
[0] >> 2];
70 *pos
++ = base64_table
[(in
[0] & 0x03) << 4];
73 *pos
++ = base64_table
[((in
[0] & 0x03) << 4) |
75 *pos
++ = base64_table
[(in
[1] & 0x0f) << 2];
92 * base64_decode - Base64 decode
93 * @src: Data to be decoded
94 * @len: Length of the data to be decoded
95 * @out_len: Pointer to output length variable
96 * Returns: Allocated buffer of out_len bytes of decoded data,
99 * Caller is responsible for freeing the returned buffer.
101 unsigned char * base64_decode(const unsigned char *src
, size_t len
,
104 unsigned char dtable
[256], *out
, *pos
, in
[4], block
[4], tmp
;
105 size_t i
, count
, olen
;
107 os_memset(dtable
, 0x80, 256);
108 for (i
= 0; i
< sizeof(base64_table
) - 1; i
++)
109 dtable
[base64_table
[i
]] = (unsigned char) i
;
113 for (i
= 0; i
< len
; i
++) {
114 if (dtable
[src
[i
]] != 0x80)
121 olen
= count
/ 4 * 3;
122 pos
= out
= os_malloc(olen
);
127 for (i
= 0; i
< len
; i
++) {
128 tmp
= dtable
[src
[i
]];
136 *pos
++ = (block
[0] << 2) | (block
[1] >> 4);
137 *pos
++ = (block
[1] << 4) | (block
[2] >> 2);
138 *pos
++ = (block
[2] << 6) | block
[3];
146 else if (in
[3] == '=')
150 *out_len
= pos
- out
;
158 int main(int argc
, char *argv
[])
162 unsigned char *buf
, *e
;
165 printf("Usage: base64 <encode|decode> <in file> <out file>\n");
169 buf
= os_readfile(argv
[2], &len
);
173 if (strcmp(argv
[1], "encode") == 0)
174 e
= base64_encode(buf
, len
, &elen
);
176 e
= base64_decode(buf
, len
, &elen
);
179 f
= fopen(argv
[3], "w");
182 fwrite(e
, 1, elen
, f
);
188 #endif /* TEST_MAIN */