scclib
Stable Cloud Computing C++ Library
base64.cc
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <encode/base64.h>
32 #include <cassert>
33 #include <stdexcept>
34 #include <vector>
35 #include <string>
36 #include <iostream>
37 #include <iomanip>
38 
39 using namespace scc::encode;
40 
41 //
42 // The following array has the character indexed by the 6 bits that it represents
43 //
44 
45 static const char encVal[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46 
47 //
48 // This array is indexed by character value and provides the 6 bits that such character
49 // contributes. Values above 63 have the following meaning:
50 //
51 // 64 - ignore this character (pad)
52 // 65 - ignore this character (whitespace)
53 // 66 - invalid character, ignore and return false
54 //
55 
56 static unsigned char decVal[128] =
57 {
58  66, 66, 66, 66, 66, 66, 66, 66, 66, 65, 65, 66, 66, 65, 66, 66,
59  66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
60  65, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 62, 66, 66, 66, 63,
61  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 66, 66, 66, 64, 66, 66,
62  66, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
63  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 66, 66, 66, 66, 66,
64  66, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
65  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 66, 66, 66, 66, 66,
66 };
67 
68 template <class T>
69 void Base64::base64_encode(const std::vector<T>& vin, std::string& s) noexcept
70 {
72  unsigned char* v = (unsigned char*)&vin[0];
73  unsigned int b = 0; // bits
74  int bc = 0; // count of bits
75 
76  s.clear();
77  s.reserve((vin.size() + 2)/3 * 4);
78 
79  for (size_t i = 0; i < vin.size(); ++i)
80  {
81  b = (b << 8) | v[i];
82  bc += 8;
83 
84  while (bc >= 6)
85  {
86  int val = (b >> (bc-6)) & 63;
87  s.push_back(encVal[val]);
88  bc -= 6;
89  }
90  }
91 
92  if (bc)
93  {
94  int val = (b << (6-bc)) & 63;
95  s.push_back(encVal[val]);
96  }
97 
98  switch (bc)
99  {
100  case 2: s.push_back('='); // plus fall through...
101  case 4: s.push_back('=');
102  }
103 }
104 
105 template void Base64::base64_encode<char>(const std::vector<char>&, std::string&);
106 template void Base64::base64_encode<unsigned char>(const std::vector<unsigned char>&, std::string&);
107 
108 std::string Base64::str_to_base64(const std::string& s) noexcept
109 {
110  std::vector<char> v(s.begin(), s.end());
111  std::string ret;
112  base64_encode(v, ret);
113  return ret;
114 }
115 
116 template <class T>
117 bool Base64::base64_decode(const std::string& s, std::vector<T>& v) noexcept
118 {
119  bool ok = true;
120  unsigned int b = 0;
121  int bc = 0;
122  int pc = 0;
123 
124  v.clear();
125 
126  for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
127  {
128  if (*i < 0 || *i > 127)
129  {
130  ok = false;
131  continue;
132  }
133 
134  int d = decVal[(int)*i];
135 
136  switch (d)
137  {
138  case 66: ok = false; // ignored character (error)
139  case 65: pc = 0; // ignored character (no error)
140  break;
141  case 64: ++pc; // count the pad
142  break;
143 
144  default:
145  pc = 0; // reset the pad count
146  b = (b << 6) | (d & 63);
147  bc += 6;
148  break;
149  }
150 
151  if (bc >= 8)
152  {
153  int val = b >> (bc-8);
154  //v.push_back(static_cast<unsigned char>(val & 255));
155  v.push_back(val & 255);
156  bc -= 8;
157  }
158  }
159 
160  if (bc - 2*pc) ok = false; // check on spare bits hanging around
161 
162  return ok;
163 }
164 
165 template bool Base64::base64_decode<char>(const std::string& s, std::vector<char>& v) noexcept;
166 template bool Base64::base64_decode<unsigned char>(const std::string& s, std::vector<unsigned char>& v) noexcept;
167 
168 std::string Base64::base64_to_str(const std::string& s) noexcept
169 {
170  std::vector<char> v;
171  if (!base64_decode(s, v))
172  {
173  return std::string();
174  }
175  return std::string(v.begin(), v.end());
176 }
177 
178 std::string Base64::base64_to_base64url(const std::string& b) noexcept
179 {
180  std::string ret;
181  for (char ch : b)
182  {
183  if (ch == '+') ret.push_back('-');
184  else if (ch == '/') ret.push_back('_');
185  else if (ch != '=') ret.push_back(ch);
186  }
187  return ret;
188 }
189 
190 std::string Base64::base64url_to_base64(const std::string& u) noexcept
191 {
192  std::string ret;
193  for (char ch : u)
194  {
195  if (ch == '-') ret.push_back('+');
196  else if (ch == '_') ret.push_back('/');
197  else ret.push_back(ch);
198  }
199  switch (u.size() % 4)
200  {
201  case 2: ret += "=="; break;
202  case 3: ret += "="; break;
203  // 0 is allowed, 1 is technically illegal, but we will just produce bad base64 since we don't throw exceptions
204  }
205 
206  return ret;
207 }
static std::string base64_to_str(const std::string &) noexcept
Convert from base64 string to string.
Definition: base64.cc:168
static void base64_encode(const std::vector< T > &, std::string &) noexcept
Convert from binary to base64 string.
Definition: base64.cc:69
static std::string str_to_base64(const std::string &s) noexcept
Convert from string to base64 string.
Definition: base64.cc:108
static std::string base64_to_base64url(const std::string &) noexcept
Convert from base64 to base64url.
Definition: base64.cc:178
static bool base64_decode(const std::string &, std::vector< T > &) noexcept
Convert from base64 string to binary.
Definition: base64.cc:117
static std::string base64url_to_base64(const std::string &) noexcept
Convert from base64url to base64.
Definition: base64.cc:190