]> git.saurik.com Git - wxWidgets.git/blame - src/common/base64.cpp
no need to check for wxID_HELP buttons in SetLabel() now that it's overridden in...
[wxWidgets.git] / src / common / base64.cpp
CommitLineData
4db03d26
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/base64.cpp
3// Purpose: implementation of BASE64 encoding/decoding functions
4// Author: Charles Reimers, Vadim Zeitlin
5// Created: 2007-06-18
6// RCS-ID: $Id$
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10#include "wx/wxprec.h"
11
b4715d08
VZ
12#ifdef __BORLANDC__
13 #pragma hdrstop
14#endif
15
4db03d26
VZ
16#if wxUSE_BASE64
17
18#include "wx/base64.h"
19
20size_t
21wxBase64Encode(char *dst, size_t dstLen, const void *src_, size_t srcLen)
22{
23 wxCHECK_MSG( src_, wxCONV_FAILED, _T("NULL input buffer") );
24
5c33522f 25 const unsigned char *src = static_cast<const unsigned char *>(src_);
4db03d26
VZ
26
27 static const char b64[] =
28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29
30
31 size_t encLen = 0;
32
33 // encode blocks of 3 bytes into 4 base64 characters
34 for ( ; srcLen >= 3; srcLen -= 3, src += 3 )
35 {
36 encLen += 4;
37 if ( dst )
38 {
39 if ( encLen > dstLen )
40 return wxCONV_FAILED;
41
42 *dst++ = b64[src[0] >> 2];
43 *dst++ = b64[((src[0] & 0x03) << 4) | ((src[1] & 0xf0) >> 4)];
44 *dst++ = b64[((src[1] & 0x0f) << 2) | ((src[2] & 0xc0) >> 6)];
45 *dst++ = b64[src[2] & 0x3f];
46 }
47 }
48
49 // finish with the remaining characters
50 if ( srcLen )
51 {
52 encLen += 4;
53 if ( dst )
54 {
55 if ( encLen > dstLen )
56 return wxCONV_FAILED;
57
58 // we have definitely one and maybe two bytes remaining
59 unsigned char next = srcLen == 2 ? src[1] : 0;
60 *dst++ = b64[src[0] >> 2];
61 *dst++ = b64[((src[0] & 0x03) << 4) | ((next & 0xf0) >> 4)];
62 *dst++ = srcLen == 2 ? b64[((next & 0x0f) << 2)] : '=';
63 *dst = '=';
64 }
65 }
66
67 return encLen;
68}
69
70size_t
71wxBase64Decode(void *dst_, size_t dstLen,
72 const char *src, size_t srcLen,
73 wxBase64DecodeMode mode,
74 size_t *posErr)
75{
76 wxCHECK_MSG( src, wxCONV_FAILED, _T("NULL input buffer") );
77
5c33522f 78 unsigned char *dst = static_cast<unsigned char *>(dst_);
4db03d26
VZ
79
80 size_t decLen = 0;
81
82 if ( srcLen == wxNO_LEN )
83 srcLen = strlen(src);
84
85 // this table contains the values, in base 64, of all valid characters and
86 // special values WSP or INV for white space and invalid characters
87 // respectively as well as a special PAD value for '='
88 enum
89 {
90 WSP = 200,
91 INV,
92 PAD
93 };
94
d05005c1 95 static const unsigned char decode[256] =
4db03d26
VZ
96 {
97 WSP,INV,INV,INV,INV,INV,INV,INV,INV,WSP,WSP,INV,WSP,WSP,INV,INV,
98 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
99 WSP,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,076,INV,INV,INV,077,
100 064,065,066,067,070,071,072,073,074,075,INV,INV,INV,PAD,INV,INV,
101 INV,000,001,002,003,004,005,006,007,010,011,012,013,014,015,016,
102 017,020,021,022,023,024,025,026,027,030,031,INV,INV,INV,INV,INV,
103 INV,032,033,034,035,036,037,040,041,042,043,044,045,046,047,050,
104 051,052,053,054,055,056,057,060,061,062,063,INV,INV,INV,INV,INV,
105 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
106 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
107 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
108 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
109 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
110 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
111 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
112 INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,INV,
113 };
114
115 // we decode input by groups of 4 characters but things are complicated by
116 // the fact that there can be whitespace and other junk in it too so keep
117 // record of where exactly we're inside the current quartet in this var
118 int n = 0;
119 unsigned char in[4]; // current quartet
120 bool end = false; // set when we find padding
121 size_t padLen = 0; // length lost to padding
122 const char *p;
123 for ( p = src; srcLen; p++, srcLen-- )
124 {
5c33522f 125 const unsigned char c = decode[static_cast<unsigned char>(*p)];
4db03d26
VZ
126 switch ( c )
127 {
128 case WSP:
129 if ( mode == wxBase64DecodeMode_SkipWS )
130 continue;
131 // fall through
132
133 case INV:
134 if ( mode == wxBase64DecodeMode_Relaxed )
135 continue;
136
137 // force the loop to stop and an error to be returned
138 n = -1;
7bac0684 139 srcLen = 1;
4db03d26
VZ
140 break;
141
142 case PAD:
143 // set the flag telling us that we're past the end now
144 end = true;
145
146 // there can be either a single '=' at the end of a quartet or
147 // "==" in positions 2 and 3
148 if ( n == 3 )
149 {
150 padLen = 1;
151 in[n++] = '\0';
152 }
153 else if ( (n == 2) && (--srcLen && *++p == '=') )
154 {
155 padLen = 2;
156 in[n++] = '\0';
157 in[n++] = '\0';
158 }
159 else // invalid padding
160 {
161 // force the loop terminate with an error
162 n = -1;
7bac0684 163 srcLen = 1;
4db03d26
VZ
164 }
165 break;
166
167 default:
168 if ( end )
169 {
170 // nothing is allowed after the end so provoke error return
171 n = -1;
7bac0684 172 srcLen = 1;
4db03d26
VZ
173 break;
174 }
175
176 in[n++] = c;
177 }
178
179 if ( n == 4 )
180 {
181 // got entire block, decode
182 decLen += 3 - padLen;
183 if ( dst )
184 {
185 if ( decLen > dstLen )
186 return wxCONV_FAILED;
187
188 // undo the bit shifting done during encoding
189 *dst++ = in[0] << 2 | in[1] >> 4;
190 *dst++ = in[1] << 4 | in[2] >> 2;
191 *dst++ = in[2] << 6 | in[3];
192 }
193
194 n = 0;
195 }
196 }
197
198 if ( n )
199 {
200 if ( posErr )
7bac0684
VZ
201 {
202 // notice that the error was on a previous position as we did one
203 // extra "p++" in the loop line after it
204 *posErr = p - src - 1;
205 }
4db03d26
VZ
206
207 return wxCONV_FAILED;
208 }
209
210 return decLen;
211}
212
213wxMemoryBuffer wxBase64Decode(const char *src,
214 size_t srcLen,
215 wxBase64DecodeMode mode,
216 size_t *posErr)
217{
218 wxMemoryBuffer buf;
219 wxCHECK_MSG( src, buf, _T("NULL input buffer") );
220
221 if ( srcLen == wxNO_LEN )
222 srcLen = strlen(src);
223
224 size_t len = wxBase64DecodedSize(srcLen);
225 len = wxBase64Decode(buf.GetWriteBuf(len), len, src, srcLen, mode, posErr);
226 if ( len == wxCONV_FAILED )
227 len = 0;
228
229 buf.SetDataLen(len);
230
231 return buf;
232}
233
234#endif // wxUSE_BASE64