EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Base64 Class Reference

#include "Base64.h"

Static Public Member Functions

static void encode (FILE *, std::string &, bool add_crlf=true)
 
static void encode (const std::string &, std::string &, bool add_crlf=true)
 
static void encode (const char *, size_t, std::string &, bool add_crlf=true)
 
static void encode (unsigned char *, size_t, std::string &, bool add_crlf=true)
 
static void decode (const std::string &, std::string &)
 
static void decode (const std::string &in, unsigned char *out, size_t &)
 
static size_t decode_length (const std::string &)
 

Static Private Attributes

static const char * bstr
 
static const char rstr [128]
 

Detailed Description

Base64 encode/decode.

Definition at line 44 of file Base64.h.

Member Function Documentation

void Base64::decode ( const std::string &  input,
std::string &  output 
)
static

Definition at line 171 of file Base64.cpp.

172 {
173  size_t i = 0;
174  size_t l = input.size();
175 
176  output = "";
177  while (i < l)
178  {
179  while (i < l && (input[i] == 13 || input[i] == 10))
180  ++i;
181  if (i < l)
182  {
183  char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
184  (rstr[(int)input[i + 1]] >> 4 & 0x03));
185  output += b1;
186  if (input[i + 2] != '=')
187  {
188  char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
189  (rstr[(int)input[i + 2]] >> 2 & 0x0f));
190  output += b2;
191  }
192  if (input[i + 3] != '=')
193  {
194  char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
195  rstr[(int)input[i + 3]]);
196  output += b3;
197  }
198  i += 4;
199  }
200  }
201 }
static const char rstr[128]
Definition: Base64.h:59
void Base64::decode ( const std::string &  in,
unsigned char *  out,
size_t &  sz 
)
static

Definition at line 204 of file Base64.cpp.

205 {
206  size_t i = 0;
207  size_t l = input.size();
208  size_t j = 0;
209 
210  while (i < l)
211  {
212  while (i < l && (input[i] == 13 || input[i] == 10))
213  ++i;
214  if (i < l)
215  {
216  unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
217  (rstr[(int)input[i + 1]] >> 4 & 0x03));
218  if (output)
219  {
220  output[j] = b1;
221  }
222  ++j;
223  if (input[i + 2] != '=')
224  {
225  unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
226  (rstr[(int)input[i + 2]] >> 2 & 0x0f));
227  if (output)
228  {
229  output[j] = b2;
230  }
231  ++j;
232  }
233  if (input[i + 3] != '=')
234  {
235  unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
236  rstr[(int)input[i + 3]]);
237  if (output)
238  {
239  output[j] = b3;
240  }
241  ++j;
242  }
243  i += 4;
244  }
245  }
246  sz = j;
247 }
static const char rstr[128]
Definition: Base64.h:59
size_t Base64::decode_length ( const std::string &  str64)
static

Definition at line 250 of file Base64.cpp.

251 {
252  if (!str64.size() || str64.size() % 4)
253  return 0;
254  size_t l = 3 * (str64.size() / 4 - 1) + 1;
255  if (str64[str64.size() - 2] != '=')
256  ++l;
257  if (str64[str64.size() - 1] != '=')
258  ++l;
259  return l;
260 }
void Base64::encode ( FILE *  fil,
std::string &  output,
bool  add_crlf = true 
)
static

Definition at line 54 of file Base64.cpp.

Referenced by CachedObjectMgr::GetCacheFileName().

55 {
56  size_t remain;
57  size_t i = 0;
58  size_t o = 0;
59  char input[4];
60 
61  output = "";
62  remain = fread(input,1,3,fil);
63  while (remain > 0)
64  {
65  if (add_crlf && o && o % 76 == 0)
66  output += "\n";
67  switch (remain)
68  {
69  case 1:
70  output += bstr[ ((input[i] >> 2) & 0x3f) ];
71  output += bstr[ ((input[i] << 4) & 0x30) ];
72  output += "==";
73  break;
74  case 2:
75  output += bstr[ ((input[i] >> 2) & 0x3f) ];
76  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
77  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
78  output += "=";
79  break;
80  default:
81  output += bstr[ ((input[i] >> 2) & 0x3f) ];
82  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
83  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
84  output += bstr[ (input[i + 2] & 0x3f) ];
85  }
86  o += 4;
87  //
88  remain = fread(input,1,3,fil);
89  }
90 }
static const char * bstr
Definition: Base64.h:58

Here is the caller graph for this function:

void Base64::encode ( const std::string &  str_in,
std::string &  str_out,
bool  add_crlf = true 
)
static

Definition at line 93 of file Base64.cpp.

94 {
95  encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
96 }
static void encode(FILE *, std::string &, bool add_crlf=true)
Definition: Base64.cpp:54
void Base64::encode ( const char *  input,
size_t  l,
std::string &  output,
bool  add_crlf = true 
)
static

Definition at line 99 of file Base64.cpp.

100 {
101  size_t i = 0;
102  size_t o = 0;
103 
104  output = "";
105  while (i < l)
106  {
107  size_t remain = l - i;
108  if (add_crlf && o && o % 76 == 0)
109  output += "\n";
110  switch (remain)
111  {
112  case 1:
113  output += bstr[ ((input[i] >> 2) & 0x3f) ];
114  output += bstr[ ((input[i] << 4) & 0x30) ];
115  output += "==";
116  break;
117  case 2:
118  output += bstr[ ((input[i] >> 2) & 0x3f) ];
119  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
120  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
121  output += "=";
122  break;
123  default:
124  output += bstr[ ((input[i] >> 2) & 0x3f) ];
125  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
126  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
127  output += bstr[ (input[i + 2] & 0x3f) ];
128  }
129  o += 4;
130  i += 3;
131  }
132 }
static const char * bstr
Definition: Base64.h:58
void Base64::encode ( unsigned char *  input,
size_t  l,
std::string &  output,
bool  add_crlf = true 
)
static

Definition at line 135 of file Base64.cpp.

136 {
137  size_t i = 0;
138  size_t o = 0;
139 
140  output = "";
141  while (i < l)
142  {
143  size_t remain = l - i;
144  if (add_crlf && o && o % 76 == 0)
145  output += "\n";
146  switch (remain)
147  {
148  case 1:
149  output += bstr[ ((input[i] >> 2) & 0x3f) ];
150  output += bstr[ ((input[i] << 4) & 0x30) ];
151  output += "==";
152  break;
153  case 2:
154  output += bstr[ ((input[i] >> 2) & 0x3f) ];
155  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
156  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
157  output += "=";
158  break;
159  default:
160  output += bstr[ ((input[i] >> 2) & 0x3f) ];
161  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
162  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
163  output += bstr[ (input[i + 2] & 0x3f) ];
164  }
165  o += 4;
166  i += 3;
167  }
168 }
static const char * bstr
Definition: Base64.h:58

Member Data Documentation

const char * Base64::bstr
staticprivate
Initial value:
=
"ABCDEFGHIJKLMNOPQ"
"RSTUVWXYZabcdefgh"
"ijklmnopqrstuvwxy"
"z0123456789+/"

Definition at line 58 of file Base64.h.

const char Base64::rstr
staticprivate
Initial value:
= {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0}

Definition at line 59 of file Base64.h.


The documentation for this class was generated from the following files: