| 1 |
/* |
| 2 |
base64.cpp and base64.h |
| 3 |
|
| 4 |
Copyright (C) 2004-2008 René Nyffenegger |
| 5 |
|
| 6 |
This source code is provided 'as-is', without any express or implied |
| 7 |
warranty. In no event will the author be held liable for any damages |
| 8 |
arising from the use of this software. |
| 9 |
|
| 10 |
Permission is granted to anyone to use this software for any purpose, |
| 11 |
including commercial applications, and to alter it and redistribute it |
| 12 |
freely, subject to the following restrictions: |
| 13 |
|
| 14 |
1. The origin of this source code must not be misrepresented; you must not |
| 15 |
claim that you wrote the original source code. If you use this source code |
| 16 |
in a product, an acknowledgment in the product documentation would be |
| 17 |
appreciated but is not required. |
| 18 |
|
| 19 |
2. Altered source versions must be plainly marked as such, and must not be |
| 20 |
misrepresented as being the original source code. |
| 21 |
|
| 22 |
3. This notice may not be removed or altered from any source distribution. |
| 23 |
|
| 24 |
René Nyffenegger [email protected] |
| 25 |
|
| 26 |
Code modified to meet XBMC stand coding style by Palle Ehmsen ([email protected]) 2012 |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "Base64.h" |
| 30 |
|
| 31 |
#include <iostream> |
| 32 |
|
| 33 |
static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 34 |
"abcdefghijklmnopqrstuvwxyz" |
| 35 |
"0123456789+/"; |
| 36 |
|
| 37 |
static inline bool is_base64(unsigned char c) |
| 38 |
{ |
| 39 |
return (isalnum(c) || (c == '+') || (c == '/') || (c == '_') || (c == '-')); |
| 40 |
} |
| 41 |
|
| 42 |
std::string base64_encode(char const* bytes_to_encode, unsigned int in_len) |
| 43 |
{ |
| 44 |
std::string ret; |
| 45 |
int i = 0; |
| 46 |
int j = 0; |
| 47 |
unsigned char char_array_3[3]; |
| 48 |
unsigned char char_array_4[4]; |
| 49 |
|
| 50 |
while (in_len--) |
| 51 |
{ |
| 52 |
char_array_3[i++] = *(bytes_to_encode++); |
| 53 |
if (i == 3) |
| 54 |
{ |
| 55 |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 56 |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 57 |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
| 58 |
char_array_4[3] = char_array_3[2] & 0x3f; |
| 59 |
|
| 60 |
for (i = 0; (i < 4); i++) |
| 61 |
ret += BASE64_CHARS[char_array_4[i]]; |
| 62 |
i = 0; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if (i) |
| 67 |
{ |
| 68 |
for (j = i; j < 3; j++) |
| 69 |
char_array_3[j] = '\0'; |
| 70 |
|
| 71 |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 72 |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 73 |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
| 74 |
char_array_4[3] = char_array_3[2] & 0x3f; |
| 75 |
|
| 76 |
for (j = 0; (j < i + 1); j++) |
| 77 |
ret += BASE64_CHARS[char_array_4[j]]; |
| 78 |
|
| 79 |
while ((i++ < 3)) |
| 80 |
ret += '='; |
| 81 |
} |
| 82 |
return ret; |
| 83 |
} |
| 84 |
|
| 85 |
std::string base64_decode(std::string const& encoded_string) |
| 86 |
{ |
| 87 |
int in_len = encoded_string.size(); |
| 88 |
int i = 0; |
| 89 |
int j = 0; |
| 90 |
int in_ = 0; |
| 91 |
unsigned char char_array_4[4], char_array_3[3]; |
| 92 |
std::string ret; |
| 93 |
|
| 94 |
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) |
| 95 |
{ |
| 96 |
if (encoded_string[in_] == '_') |
| 97 |
{ |
| 98 |
// workaround for base64 URL safe, see: rfc4648 |
| 99 |
char_array_4[i++] = '/'; |
| 100 |
} |
| 101 |
else if (encoded_string[in_] == '-') |
| 102 |
{ |
| 103 |
// workaround for base64 URL safe, see: rfc4648 |
| 104 |
char_array_4[i++] = '+'; |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
char_array_4[i++] = encoded_string[in_]; |
| 109 |
} |
| 110 |
in_++; |
| 111 |
if (i == 4) |
| 112 |
{ |
| 113 |
for (i = 0; i < 4; i++) |
| 114 |
char_array_4[i] = BASE64_CHARS.find(char_array_4[i]); |
| 115 |
|
| 116 |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 117 |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 118 |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 119 |
|
| 120 |
for (i = 0; (i < 3); i++) |
| 121 |
ret += char_array_3[i]; |
| 122 |
i = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (i) |
| 127 |
{ |
| 128 |
for (j = i; j < 4; j++) |
| 129 |
char_array_4[j] = 0; |
| 130 |
|
| 131 |
for (j = 0; j < 4; j++) |
| 132 |
char_array_4[j] = BASE64_CHARS.find(char_array_4[j]); |
| 133 |
|
| 134 |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 135 |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 136 |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 137 |
|
| 138 |
for (j = 0; (j < i - 1); j++) |
| 139 |
ret += char_array_3[j]; |
| 140 |
} |
| 141 |
|
| 142 |
return ret; |
| 143 |
} |