Debian Codegraph @

src/libs/alibxx/crypto/base64.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) 2013 Adrien Devresse <[email protected]>, CERN
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21
22
23 #include "base64.hpp"
24 #include <iostream>
25
26
27 namespace Base64{
28
29 static const std::string base64_chars =
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31 "abcdefghijklmnopqrstuvwxyz"
32 "0123456789+/";
33
34
35 static inline bool is_base64(unsigned char c) {
36 return (isalnum(c) || (c == '+') || (c == '/'));
37 }
38
39 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
40 std::string ret;
41 int i = 0;
42 int j = 0;
43 unsigned char char_array_3[3];
44 unsigned char char_array_4[4];
45
46 while (in_len--) {
47 char_array_3[i++] = *(bytes_to_encode++);
48 if (i == 3) {
49 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
50 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
51 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
52 char_array_4[3] = char_array_3[2] & 0x3f;
53
54 for(i = 0; (i <4) ; i++)
55 ret += base64_chars[char_array_4[i]];
56 i = 0;
57 }
58 }
59
60 if (i)
61 {
62 for(j = i; j < 3; j++)
63 char_array_3[j] = '\0';
64
65 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
66 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
67 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
68 char_array_4[3] = char_array_3[2] & 0x3f;
69
70 for (j = 0; (j < i + 1); j++)
71 ret += base64_chars[char_array_4[j]];
72
73 while((i++ < 3))
74 ret += '=';
75
76 }
77
78 return ret;
79
80 }
81
82 std::string base64_decode(std::string const& encoded_string) {
83 int in_len = encoded_string.size();
84 int i = 0;
85 int j = 0;
86 int in_ = 0;
87 unsigned char char_array_4[4], char_array_3[3];
88 std::string ret;
89
90 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
91 char_array_4[i++] = encoded_string[in_]; in_++;
92 if (i ==4) {
93 for (i = 0; i <4; i++)
94 char_array_4[i] = base64_chars.find(char_array_4[i]);
95
96 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
97 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
98 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
99
100 for (i = 0; (i < 3); i++)
101 ret += char_array_3[i];
102 i = 0;
103 }
104 }
105
106 if (i) {
107 for (j = i; j <4; j++)
108 char_array_4[j] = 0;
109
110 for (j = 0; j <4; j++)
111 char_array_4[j] = base64_chars.find(char_array_4[j]);
112
113 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
114 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
115 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
116
117 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
118 }
119
120 return ret;
121 }
122
123 }