Debian Codegraph @

src/base64.hpp

raw
1 /*
2 MIT License
3
4 Copyright (c) 2019 Tobias Locker
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 /*
26 * Note:
27 * Origin of this file is [email protected]:tobiaslocker/base64.git at db7c6834bd4f733899bd93218926247a659d8924
28 */
29
30 #ifndef BASE_64_HPP
31 #define BASE_64_HPP
32
33 #include <algorithm>
34 #include <string>
35
36 namespace base64 {
37
38 inline const std::string & get_base64_chars() {
39 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz"
41 "0123456789+/";
42 return base64_chars;
43 }
44
45 inline std::string to_base64(std::string const &data) {
46 int counter = 0;
47 uint32_t bit_stream = 0;
48 auto base64_chars = get_base64_chars();
49 std::string encoded;
50 int offset = 0;
51 for (unsigned char c : data) {
52 auto num_val = static_cast<unsigned int>(c);
53 offset = 16 - counter % 3 * 8;
54 bit_stream += num_val << offset;
55 if (offset == 16) {
56 encoded += base64_chars.at(bit_stream >> 18 & 0x3f);
57 }
58 if (offset == 8) {
59 encoded += base64_chars.at(bit_stream >> 12 & 0x3f);
60 }
61 if (offset == 0 && counter != 3) {
62 encoded += base64_chars.at(bit_stream >> 6 & 0x3f);
63 encoded += base64_chars.at(bit_stream & 0x3f);
64 bit_stream = 0;
65 }
66 ++counter;
67 }
68 if (offset == 16) {
69 encoded += base64_chars.at(bit_stream >> 12 & 0x3f);
70 encoded += "==";
71 }
72 if (offset == 8) {
73 encoded += base64_chars.at(bit_stream >> 6 & 0x3f);
74 encoded += '=';
75 }
76 return encoded;
77 }
78
79 inline std::string from_base64(std::string const &data) {
80 int counter = 0;
81 uint32_t bit_stream = 0;
82 std::string decoded;
83 int offset = 0;
84 auto base64_chars = get_base64_chars();
85 for (unsigned char c : data) {
86 auto num_val = base64_chars.find(c);
87 if (num_val != std::string::npos) {
88 offset = 18 - counter % 4 * 6;
89 bit_stream += num_val << offset;
90 if (offset == 12) {
91 decoded += static_cast<char>(bit_stream >> 16 & 0xff);
92 }
93 if (offset == 6) {
94 decoded += static_cast<char>(bit_stream >> 8 & 0xff);
95 }
96 if (offset == 0 && counter != 4) {
97 decoded += static_cast<char>(bit_stream & 0xff);
98 bit_stream = 0;
99 }
100 } else if (c != '=') {
101 return std::string();
102 }
103 counter++;
104 }
105 return decoded;
106 }
107
108 }
109
110 #endif // BASE_64_HPP