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