|
| 1 | +/* |
| 2 | + base64.cpp and base64.h |
| 3 | + base64 encoding and decoding with C++. |
| 4 | + Version: 1.01.00 |
| 5 | + Copyright (C) 2004-2017 René Nyffenegger |
| 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 | + Permission is granted to anyone to use this software for any purpose, |
| 10 | + including commercial applications, and to alter it and redistribute it |
| 11 | + freely, subject to the following restrictions: |
| 12 | + 1. The origin of this source code must not be misrepresented; you must not |
| 13 | + claim that you wrote the original source code. If you use this source code |
| 14 | + in a product, an acknowledgment in the product documentation would be |
| 15 | + appreciated but is not required. |
| 16 | + 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | + misrepresented as being the original source code. |
| 18 | + 3. This notice may not be removed or altered from any source distribution. |
| 19 | + René Nyffenegger [email protected] |
| 20 | +*/ |
| 21 | + |
| 22 | +#include "base64.h" |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +static const std::string base64_chars = |
| 26 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 27 | + "abcdefghijklmnopqrstuvwxyz" |
| 28 | + "0123456789+/"; |
| 29 | + |
| 30 | + |
| 31 | +static inline bool is_base64(unsigned char c) { |
| 32 | + return (isalnum(c) || (c == '+') || (c == '/')); |
| 33 | +} |
| 34 | + |
| 35 | +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { |
| 36 | + std::string ret; |
| 37 | + int i = 0; |
| 38 | + int j = 0; |
| 39 | + unsigned char char_array_3[3]; |
| 40 | + unsigned char char_array_4[4]; |
| 41 | + |
| 42 | + while (in_len--) { |
| 43 | + char_array_3[i++] = *(bytes_to_encode++); |
| 44 | + if (i == 3) { |
| 45 | + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 46 | + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 47 | + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
| 48 | + char_array_4[3] = char_array_3[2] & 0x3f; |
| 49 | + |
| 50 | + for(i = 0; (i <4) ; i++) |
| 51 | + ret += base64_chars[char_array_4[i]]; |
| 52 | + i = 0; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (i) |
| 57 | + { |
| 58 | + for(j = i; j < 3; j++) |
| 59 | + char_array_3[j] = '\0'; |
| 60 | + |
| 61 | + char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2; |
| 62 | + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 63 | + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
| 64 | + |
| 65 | + for (j = 0; (j < i + 1); j++) |
| 66 | + ret += base64_chars[char_array_4[j]]; |
| 67 | + |
| 68 | + while((i++ < 3)) |
| 69 | + ret += '='; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + return ret; |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +std::string base64_decode(std::string const& encoded_string) { |
| 78 | + int in_len = encoded_string.size(); |
| 79 | + int i = 0; |
| 80 | + int j = 0; |
| 81 | + int in_ = 0; |
| 82 | + unsigned char char_array_4[4], char_array_3[3]; |
| 83 | + std::string ret; |
| 84 | + |
| 85 | + while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { |
| 86 | + char_array_4[i++] = encoded_string[in_]; in_++; |
| 87 | + if (i ==4) { |
| 88 | + for (i = 0; i <4; i++) |
| 89 | + char_array_4[i] = base64_chars.find(char_array_4[i]); |
| 90 | + |
| 91 | + char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4); |
| 92 | + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 93 | + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 94 | + |
| 95 | + for (i = 0; (i < 3); i++) |
| 96 | + ret += char_array_3[i]; |
| 97 | + i = 0; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (i) { |
| 102 | + for (j = 0; j < i; j++) |
| 103 | + char_array_4[j] = base64_chars.find(char_array_4[j]); |
| 104 | + |
| 105 | + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 106 | + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 107 | + |
| 108 | + for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; |
| 109 | + } |
| 110 | + |
| 111 | + return ret; |
| 112 | +} |
0 commit comments