Fabcoin Core  0.16.2
P2P Digital Currency
Base64.cpp
Go to the documentation of this file.
1 /*
2  base64.cpp and base64.h
3 
4  Copyright (C) 2004-2008 RenĂ© Nyffenegger
5 
6  This source code is provided 'as-is', without any express or implied
7  warranty. In no event will the author be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this source code must not be misrepresented; you must not
15  claim that you wrote the original source code. If you use this source code
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18 
19  2. Altered source versions must be plainly marked as such, and must not be
20  misrepresented as being the original source code.
21 
22  3. This notice may not be removed or altered from any source distribution.
23 
24  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
25 */
28 
29 #include "Base64.h"
30 
31 using namespace std;
32 using namespace dev;
33 
34 static inline bool is_base64(byte c)
35 {
36  return (isalnum(c) || (c == '+') || (c == '/'));
37 }
38 
39 static inline byte find_base64_char_index(byte c)
40 {
41  if ('A' <= c && c <= 'Z') return c - 'A';
42  else if ('a' <= c && c <= 'z') return c - 'a' + 1 + find_base64_char_index('Z');
43  else if ('0' <= c && c <= '9') return c - '0' + 1 + find_base64_char_index('z');
44  else if (c == '+') return 1 + find_base64_char_index('9');
45  else if (c == '/') return 1 + find_base64_char_index('+');
46  else return 1 + find_base64_char_index('/');
47 }
48 
50 {
51  static const char base64_chars[] =
52  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53  "abcdefghijklmnopqrstuvwxyz"
54  "0123456789+/";
55 
56  string ret;
57  int i = 0;
58  int j = 0;
59  byte char_array_3[3];
60  byte char_array_4[4];
61 
62  auto buf = _in.data();
63  auto bufLen = _in.size();
64 
65  while (bufLen--)
66  {
67  char_array_3[i++] = *(buf++);
68  if (i == 3)
69  {
70  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
71  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
72  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
73  char_array_4[3] = char_array_3[2] & 0x3f;
74 
75  for (i = 0; i < 4; i++)
76  ret += base64_chars[char_array_4[i]];
77  i = 0;
78  }
79  }
80 
81  if (i)
82  {
83  for (j = i; j < 3; j++)
84  char_array_3[j] = '\0';
85 
86  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
87  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
88  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
89  char_array_4[3] = char_array_3[2] & 0x3f;
90 
91  for (j = 0; j < i + 1; j++)
92  ret += base64_chars[char_array_4[j]];
93 
94  while (i++ < 3)
95  ret += '=';
96  }
97 
98  return ret;
99 }
100 
101 bytes dev::fromBase64(string const& encoded_string)
102 {
103  auto in_len = encoded_string.size();
104  int i = 0;
105  int j = 0;
106  int in_ = 0;
107  byte char_array_3[3];
108  byte char_array_4[4];
109  bytes ret;
110 
111  while (in_len-- && encoded_string[in_] != '=' && is_base64(encoded_string[in_]))
112  {
113  char_array_4[i++] = encoded_string[in_]; in_++;
114  if (i == 4)
115  {
116  for (i = 0; i < 4; i++)
117  char_array_4[i] = find_base64_char_index(char_array_4[i]);
118 
119  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
120  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
121  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
122 
123  for (i = 0; (i < 3); i++)
124  ret.push_back(char_array_3[i]);
125  i = 0;
126  }
127  }
128 
129  if (i)
130  {
131  for (j = i; j < 4; j++)
132  char_array_4[j] = 0;
133 
134  for (j = 0; j < 4; j++)
135  char_array_4[j] = find_base64_char_index(char_array_4[j]);
136 
137  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
138  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
139  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
140 
141  for (j = 0; j < i - 1; j++)
142  ret.push_back(char_array_3[j]);
143  }
144 
145  return ret;
146 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
#define c(i)
std::hash for asio::adress
Definition: Common.h:323
std::vector< byte > bytes
Definition: Common.h:75
size_t size() const
Definition: vector_ref.h:55
std::string toBase64(bytesConstRef _in)
Definition: Base64.cpp:49
_T * data() const
Definition: vector_ref.h:51
bytes fromBase64(std::string const &_in)