[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / src / sockets / Base64.cpp
blobb8cf1237175ff9e8612940fb05530abb89557d5a
1 /** \file Base64.cpp
2 ** \date 2004-02-13
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include "Base64.h"
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE {
34 #endif
37 const char *Base64::bstr =
38 "ABCDEFGHIJKLMNOPQ"
39 "RSTUVWXYZabcdefgh"
40 "ijklmnopqrstuvwxy"
41 "z0123456789+/";
43 const char Base64::rstr[] = {
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
47 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
48 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
50 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
51 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};
54 Base64::Base64()
59 void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
61 size_t remain;
62 size_t i = 0;
63 size_t o = 0;
64 char input[4];
66 output = "";
67 remain = fread(input,1,3,fil);
68 while (remain > 0)
70 if (add_crlf && o && o % 76 == 0)
71 output += "\n";
72 switch (remain)
74 case 1:
75 output += bstr[ ((input[i] >> 2) & 0x3f) ];
76 output += bstr[ ((input[i] << 4) & 0x30) ];
77 output += "==";
78 break;
79 case 2:
80 output += bstr[ ((input[i] >> 2) & 0x3f) ];
81 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
82 output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
83 output += "=";
84 break;
85 default:
86 output += bstr[ ((input[i] >> 2) & 0x3f) ];
87 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
88 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
89 output += bstr[ (input[i + 2] & 0x3f) ];
91 o += 4;
93 remain = fread(input,1,3,fil);
98 void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
100 encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
104 void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
106 size_t i = 0;
107 size_t o = 0;
109 output = "";
110 while (i < l)
112 size_t remain = l - i;
113 if (add_crlf && o && o % 76 == 0)
114 output += "\n";
115 switch (remain)
117 case 1:
118 output += bstr[ ((input[i] >> 2) & 0x3f) ];
119 output += bstr[ ((input[i] << 4) & 0x30) ];
120 output += "==";
121 break;
122 case 2:
123 output += bstr[ ((input[i] >> 2) & 0x3f) ];
124 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
125 output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
126 output += "=";
127 break;
128 default:
129 output += bstr[ ((input[i] >> 2) & 0x3f) ];
130 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
131 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
132 output += bstr[ (input[i + 2] & 0x3f) ];
134 o += 4;
135 i += 3;
140 void Base64::encode(const unsigned char* input,size_t l,std::string& output,bool add_crlf)
142 size_t i = 0;
143 size_t o = 0;
145 output = "";
146 while (i < l)
148 size_t remain = l - i;
149 if (add_crlf && o && o % 76 == 0)
150 output += "\n";
151 switch (remain)
153 case 1:
154 output += bstr[ ((input[i] >> 2) & 0x3f) ];
155 output += bstr[ ((input[i] << 4) & 0x30) ];
156 output += "==";
157 break;
158 case 2:
159 output += bstr[ ((input[i] >> 2) & 0x3f) ];
160 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
161 output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
162 output += "=";
163 break;
164 default:
165 output += bstr[ ((input[i] >> 2) & 0x3f) ];
166 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
167 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
168 output += bstr[ (input[i + 2] & 0x3f) ];
170 o += 4;
171 i += 3;
176 void Base64::decode(const std::string& input,std::string& output)
178 size_t i = 0;
179 size_t l = input.size();
181 output = "";
182 while (i < l)
184 while (i < l && (input[i] == 13 || input[i] == 10))
185 i++;
186 if (i < l)
188 char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
189 (rstr[(int)input[i + 1]] >> 4 & 0x03));
190 output += b1;
191 if (input[i + 2] != '=')
193 char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
194 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
195 output += b2;
197 if (input[i + 3] != '=')
199 char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
200 rstr[(int)input[i + 3]]);
201 output += b3;
203 i += 4;
209 void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
211 size_t i = 0;
212 size_t l = input.size();
213 size_t j = 0;
215 while (i < l)
217 while (i < l && (input[i] == 13 || input[i] == 10))
218 i++;
219 if (i < l)
221 unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
222 (rstr[(int)input[i + 1]] >> 4 & 0x03));
223 if (output)
225 output[j] = b1;
227 j++;
228 if (input[i + 2] != '=')
230 unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
231 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
232 if (output)
234 output[j] = b2;
236 j++;
238 if (input[i + 3] != '=')
240 unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
241 rstr[(int)input[i + 3]]);
242 if (output)
244 output[j] = b3;
246 j++;
248 i += 4;
251 sz = j;
255 size_t Base64::decode_length(const std::string& str64)
257 if (str64.empty() || str64.size() % 4)
258 return 0;
259 size_t l = 3 * (str64.size() / 4 - 1) + 1;
260 if (str64[str64.size() - 2] != '=')
261 l++;
262 if (str64[str64.size() - 1] != '=')
263 l++;
264 return l;
268 #ifdef SOCKETS_NAMESPACE
270 #endif