Libgourou

Libgourou Git Source Tree

Root/include/Base64.h

1#ifndef _MACARON_BASE64_H_
2#define _MACARON_BASE64_H_
3
4/**
5 * The MIT License (MIT)
6 * Copyright (c) 2016 tomykaira
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <string>
29#include <stdint.h>
30
31namespace macaron {
32
33class Base64 {
34 public:
35
36 static std::string Encode(const std::string data) {
37 static
38#if __STDC_VERSION__ >= 201112L
39 constexpr
40#endif /* __STDC_VERSION__ >= 201112L */
41 char sEncodingTable[] = {
42 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
43 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
44 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
45 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
46 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
47 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
48 'w', 'x', 'y', 'z', '0', '1', '2', '3',
49 '4', '5', '6', '7', '8', '9', '+', '/'
50 };
51
52 size_t in_len = data.size();
53 size_t out_len = 4 * ((in_len + 2) / 3);
54 std::string ret(out_len, '\0');
55 size_t i;
56 char *p = const_cast<char*>(ret.c_str());
57
58 for (i = 0; i < in_len - 2; i += 3) {
59 *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
60 *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
61 *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
62 *p++ = sEncodingTable[data[i + 2] & 0x3F];
63 }
64 if (i < in_len) {
65 *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
66 if (i == (in_len - 1)) {
67 *p++ = sEncodingTable[((data[i] & 0x3) << 4)];
68 *p++ = '=';
69 }
70 else {
71 *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
72 *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
73 }
74 *p++ = '=';
75 }
76
77 return ret;
78 }
79
80 static std::string Decode(const std::string& input, std::string& out) {
81 static
82#if __STDC_VERSION__ >= 201112L
83 constexpr
84#endif /* __STDC_VERSION__ >= 201112L */
85 unsigned char kDecodingTable[] = {
86 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
87 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
88 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
89 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
90 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
91 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
92 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
93 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
94 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
95 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
96 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
97 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
98 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
99 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
100 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
101 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
102 };
103
104 size_t in_len = input.size();
105 if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
106
107 size_t out_len = in_len / 4 * 3;
108 if (input[in_len - 1] == '=') out_len--;
109 if (input[in_len - 2] == '=') out_len--;
110
111 out.resize(out_len);
112
113 for (size_t i = 0, j = 0; i < in_len;) {
114 uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
115 uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
116 uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
117 uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
118
119 uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
120
121 if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
122 if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
123 if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
124 }
125
126 return "";
127 }
128
129};
130
131}
132
133#endif /* _MACARON_BASE64_H_ */

Archive Download this file