Debian Codegraph @

source/code/Libs/include/cereal/external/base64.hpp

raw symbols SCIP
1 /*
2 Copyright (C) 2004-2008 René Nyffenegger
3
4 This source code is provided 'as-is', without any express or implied
5 warranty. In no event will the author be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
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
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original source code.
19
20 3. This notice may not be removed or altered from any source distribution.
21
22 René Nyffenegger [email protected]
23 */
24
25 #ifndef CEREAL_EXTERNAL_BASE64_HPP_
26 #define CEREAL_EXTERNAL_BASE64_HPP_
27
28 #ifdef __GNUC__
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wconversion"
31 #endif
32
33 #include <string>
34
35 namespace cereal
36 {
37 namespace base64
38 {
39 static const std::string chars =
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz"
42 "0123456789+/";
43
44 static inline bool is_base64(unsigned char c) {
45 return (isalnum(c) || (c == '+') || (c == '/'));
46 }
47
48 inline std::string encode(unsigned char const* bytes_to_encode, size_t 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] = static_cast<unsigned char>((char_array_3[0] & 0xfc) >> 2);
59 char_array_4[1] = static_cast<unsigned char>( ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ) );
60 char_array_4[2] = static_cast<unsigned char>( ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ) );
61 char_array_4[3] = static_cast<unsigned char>( char_array_3[2] & 0x3f );
62
63 for(i = 0; (i <4) ; i++)
64 ret += 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 char_array_4[3] = char_array_3[2] & 0x3f;
78
79 for (j = 0; (j < i + 1); j++)
80 ret += chars[char_array_4[j]];
81
82 while((i++ < 3))
83 ret += '=';
84 }
85
86 return ret;
87 }
88
89 inline std::string decode(std::string const& encoded_string) {
90 size_t in_len = encoded_string.size();
91 size_t i = 0;
92 size_t j = 0;
93 int in_ = 0;
94 unsigned char char_array_4[4], char_array_3[3];
95 std::string ret;
96
97 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
98 char_array_4[i++] = encoded_string[in_]; in_++;
99 if (i ==4) {
100 for (i = 0; i <4; i++)
101 char_array_4[i] = static_cast<unsigned char>(chars.find( char_array_4[i] ));
102
103 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
104 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
105 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
106
107 for (i = 0; (i < 3); i++)
108 ret += char_array_3[i];
109 i = 0;
110 }
111 }
112
113 if (i) {
114 for (j = i; j <4; j++)
115 char_array_4[j] = 0;
116
117 for (j = 0; j <4; j++)
118 char_array_4[j] = static_cast<unsigned char>(chars.find( char_array_4[j] ));
119
120 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
121 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
122 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
123
124 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
125 }
126
127 return ret;
128 }
129 } // namespace base64
130 } // namespace cereal
131 #ifdef __GNUC__
132 #pragma GCC diagnostic pop
133 #endif
134 #endif // CEREAL_EXTERNAL_BASE64_HPP_