BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
base64.cpp
Go to the documentation of this file.
1 #include <fc/crypto/base64.hpp>
2 #include <ctype.h>
3 /*
4  base64.cpp and base64.h
5 
6  Copyright (C) 2004-2008 RenĂ© Nyffenegger
7 
8  This source code is provided 'as-is', without any express or implied
9  warranty. In no event will the author be held liable for any damages
10  arising from the use of this software.
11 
12  Permission is granted to anyone to use this software for any purpose,
13  including commercial applications, and to alter it and redistribute it
14  freely, subject to the following restrictions:
15 
16  1. The origin of this source code must not be misrepresented; you must not
17  claim that you wrote the original source code. If you use this source code
18  in a product, an acknowledgment in the product documentation would be
19  appreciated but is not required.
20 
21  2. Altered source versions must be plainly marked as such, and must not be
22  misrepresented as being the original source code.
23 
24  3. This notice may not be removed or altered from any source distribution.
25 
26  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
27 
28 */
29 
30 namespace fc {
31 
32 inline const std::string& base64_chars()
33 {
34  static const std::string m_base64_chars =
35  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36  "abcdefghijklmnopqrstuvwxyz"
37  "0123456789+/";
38  return m_base64_chars;
39 }
40 
41 static inline bool is_base64(unsigned char c) {
42  return (isalnum(c) || (c == '+') || (c == '/'));
43 }
44 
45 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len);
46 
47 std::string base64_encode( const std::string& enc ) {
48  char const* s = enc.c_str();
49  return base64_encode( (unsigned char const*)s, enc.size() );
50 }
51 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
52 
53  std::string ret;
54  int i = 0;
55  int j = 0;
56  unsigned char char_array_3[3];
57  unsigned char char_array_4[4];
58 
59  while (in_len--) {
60  char_array_3[i++] = *(bytes_to_encode++);
61  if (i == 3) {
62  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
63  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
64  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
65  char_array_4[3] = char_array_3[2] & 0x3f;
66 
67  for(i = 0; (i <4) ; i++)
68  ret += base64_chars()[char_array_4[i]];
69  i = 0;
70  }
71  }
72 
73  if (i)
74  {
75  for(j = i; j < 3; j++)
76  char_array_3[j] = '\0';
77 
78  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
79  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
80  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
81  char_array_4[3] = char_array_3[2] & 0x3f;
82 
83  for (j = 0; (j < i + 1); j++)
84  ret += base64_chars()[char_array_4[j]];
85 
86  while((i++ < 3))
87  ret += '=';
88 
89  }
90 
91  return ret;
92 
93 }
94 
95 
96 std::string base64_decode(std::string const& encoded_string) {
97  int in_len = encoded_string.size();
98  int i = 0;
99  int j = 0;
100  int in_ = 0;
101  unsigned char char_array_4[4], char_array_3[3];
102  std::string ret;
103 
104  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
105  char_array_4[i++] = encoded_string[in_]; in_++;
106  if (i ==4) {
107  for (i = 0; i <4; i++)
108  char_array_4[i] = base64_chars().find(char_array_4[i]);
109 
110  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
111  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
112  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
113 
114  for (i = 0; (i < 3); i++)
115  ret += char_array_3[i];
116  i = 0;
117  }
118  }
119 
120  if (i) {
121  for (j = i; j <4; j++)
122  char_array_4[j] = 0;
123 
124  for (j = 0; j <4; j++)
125  char_array_4[j] = base64_chars().find(char_array_4[j]);
126 
127  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
128  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
129  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
130 
131  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
132  }
133 
134  return ret;
135 }
136 
137 } // namespace fc
138 
fc::base64_chars
const std::string & base64_chars()
Definition: base64.cpp:32
fc
Definition: api.hpp:15
base64.hpp
fc::base64_encode
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition: base64.cpp:51
fc::base64_decode
std::string base64_decode(const std::string &encoded_string)
Definition: base64.cpp:96