2 * base64 encoder/decoder
4 * Copyright 2005 by Kai Blin
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
28 static const char b64[] =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 SECURITY_STATUS encodeBase64(PBYTE in_buf, int in_len, char* out_buf,
32 int max_len, int *out_len)
36 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
38 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
39 *out_len = bytes + pad_bytes;
41 if(bytes + pad_bytes + 1 > max_len)
42 return SEC_E_BUFFER_TOO_SMALL;
44 /* Three bytes of input give 4 chars of output */
50 /* first char is the first 6 bits of the first byte*/
51 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
52 /* second char is the last 2 bits of the first byte and the first 4
53 * bits of the second byte */
54 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
55 /* third char is the last 4 bits of the second byte and the first 2
56 * bits of the third byte */
57 out_buf[i + 2] = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
58 /* fourth char is the remaining 6 bits of the third byte */
59 out_buf[i + 3] = b64[ d[2] & 0x3f];
68 /* first char is the first 6 bits of the first byte*/
69 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
70 /* second char is the last 2 bits of the first byte and the first 4
71 * bits of the second byte */
72 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
73 /* third char is the last 4 bits of the second byte padded with
75 out_buf[i + 2] = b64[ ((d[1] >> 4) & 0x3c) ];
76 /* fourth char is a = to indicate one byte of padding */
81 /* first char is the first 6 bits of the first byte*/
82 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
83 /* second char is the last 2 bits of the first byte padded with
85 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30)];
86 /* third char is = to indicate padding */
88 /* fourth char is = to indicate padding */
99 static inline BYTE decode(char c)
101 if( c >= 'A' && c <= 'Z')
103 if( c >= 'a' && c <= 'z')
105 if( c >= '0' && c <= '9')
115 SECURITY_STATUS decodeBase64(char *in_buf, int in_len, PBYTE out_buf,
116 int max_len, int *out_len)
120 int ip0, ip1, ip2, ip3;
122 TRACE("in_len: %d\n", in_len);
124 if((in_len % 4) != 0)
125 return SEC_E_INVALID_TOKEN;
128 return SEC_E_BUFFER_TOO_SMALL;
133 if((ip0 = decode(d[0])) > 63)
134 return SEC_E_INVALID_TOKEN;
135 if((ip1 = decode(d[1])) > 63)
136 return SEC_E_INVALID_TOKEN;
137 if((ip2 = decode(d[2])) > 63)
138 return SEC_E_INVALID_TOKEN;
139 if((ip3 = decode(d[3])) > 63)
140 return SEC_E_INVALID_TOKEN;
142 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
143 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
144 out_buf[i + 2] = (ip2 << 6) | ip3;
152 if((ip0 = decode(d[0])) > 63)
153 return SEC_E_INVALID_TOKEN;
154 if((ip1 = decode(d[1])) > 63)
155 return SEC_E_INVALID_TOKEN;
157 out_buf[i] = (ip0 << 2) | (ip1 >> 4);
162 if((ip0 = decode(d[0])) > 63)
163 return SEC_E_INVALID_TOKEN;
164 if((ip1 = decode(d[1])) > 63)
165 return SEC_E_INVALID_TOKEN;
166 if((ip2 = decode(d[2])) > 63)
167 return SEC_E_INVALID_TOKEN;
169 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
170 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
175 if((ip0 = decode(d[0])) > 63)
176 return SEC_E_INVALID_TOKEN;
177 if((ip1 = decode(d[1])) > 63)
178 return SEC_E_INVALID_TOKEN;
179 if((ip2 = decode(d[2])) > 63)
180 return SEC_E_INVALID_TOKEN;
181 if((ip3 = decode(d[3])) > 63)
182 return SEC_E_INVALID_TOKEN;
185 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
186 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
187 out_buf[i + 2] = (ip2 << 6) | ip3;