Main MRPT website > C++ reference for MRPT 1.9.9
base64.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/utils/round.h> // round()
14 
15 using namespace mrpt::system;
16 using namespace mrpt::utils;
17 using namespace std;
18 
19 // This code is based on files in the public domain:
20 // http://gd.tuwien.ac.at/infosys/mail/vm
21 
22 const unsigned char alphabet[64 + 1] =
23  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 
25 /*---------------------------------------------------------------
26  encodeBase64
27 ---------------------------------------------------------------*/
29  const vector_byte& inputData, std::string& outString)
30 {
31  outString.clear();
32  outString.reserve(inputData.size() * mrpt::utils::round(4.0 / 3.0));
33 
34  int char_count = 0;
35  int bits = 0;
36  int cols = 0;
37 
38  for (size_t i = 0; i < inputData.size(); i++)
39  {
40  const uint8_t c = inputData[i];
41  bits += c;
42  char_count++;
43 
44  if (char_count == 3)
45  {
46  outString.push_back(alphabet[bits >> 18]);
47  outString.push_back(alphabet[(bits >> 12) & 0x3f]);
48  outString.push_back(alphabet[(bits >> 6) & 0x3f]);
49  outString.push_back(alphabet[bits & 0x3f]);
50  cols += 4;
51  if (cols == 72)
52  {
53  outString.push_back('\n');
54  cols = 0;
55  }
56  bits = 0;
57  char_count = 0;
58  }
59  else
60  {
61  bits <<= 8;
62  }
63  }
64 
65  if (char_count != 0)
66  {
67  bits <<= 16 - (8 * char_count);
68  outString.push_back(alphabet[bits >> 18]);
69  outString.push_back(alphabet[(bits >> 12) & 0x3f]);
70 
71  if (char_count == 1)
72  {
73  outString.push_back('=');
74  outString.push_back('=');
75  }
76  else
77  {
78  outString.push_back(alphabet[(bits >> 6) & 0x3f]);
79  outString.push_back('=');
80  }
81  if (cols > 0) outString.push_back('\n');
82  }
83 }
84 
85 /*---------------------------------------------------------------
86  decodeBase64
87 ---------------------------------------------------------------*/
89  const std::string& inString, vector_byte& outData)
90 {
91  static bool inalphabet[256];
92  static char decoder[256];
93 
94  static bool tablesBuilt = false;
95 
96  if (!tablesBuilt)
97  {
98  tablesBuilt = true;
99  for (int i = (sizeof(alphabet)) - 1; i >= 0; i--)
100  {
101  inalphabet[alphabet[i]] = 1;
102  decoder[alphabet[i]] = i;
103  }
104  }
105 
106  outData.clear();
107  outData.reserve(inString.size() * round(3.0 / 4.0));
108 
109  int errors = 0;
110 
111  int char_count = 0;
112  int bits = 0;
113  bool finish_flag_found = false;
114 
115  for (size_t i = 0; i < inString.size(); i++)
116  {
117  const unsigned char c = inString[i];
118 
119  if (c == '=')
120  {
121  finish_flag_found = true;
122  break;
123  }
124  if (!inalphabet[c]) continue;
125 
126  bits += decoder[c];
127  char_count++;
128  if (char_count == 4)
129  {
130  outData.push_back((bits >> 16));
131  outData.push_back(((bits >> 8) & 0xff));
132  outData.push_back((bits & 0xff));
133  bits = 0;
134  char_count = 0;
135  }
136  else
137  bits <<= 6;
138  }
139 
140  if (!finish_flag_found)
141  {
142  if (char_count)
143  {
144  std::cerr << format(
145  "[decodeBase64] ERROR: base64 encoding "
146  "incomplete, at least %d bits truncated",
147  ((4 - char_count) * 6))
148  << std::endl;
149  errors++;
150  }
151  }
152  else
153  { /* c == '=' */
154  switch (char_count)
155  {
156  case 1:
157  std::cerr << "[decodeBase64] ERROR: base64 encoding "
158  "incomplete, at least 2 bits missing"
159  << std::endl;
160  errors++;
161  break;
162  case 2:
163  outData.push_back((bits >> 10));
164  break;
165  case 3:
166  outData.push_back((bits >> 16));
167  outData.push_back(((bits >> 8) & 0xff));
168  break;
169  }
170  }
171 
172  return errors == 0;
173 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::vector< uint8_t > vector_byte
Definition: types_simple.h:27
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
void encodeBase64(const vector_byte &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
Definition: base64.cpp:28
STL namespace.
unsigned char uint8_t
Definition: rptypes.h:41
const GLubyte * c
Definition: glext.h:6313
bool decodeBase64(const std::string &inString, vector_byte &outData)
Decode a base-64 string into the original sequence of bytes.
Definition: base64.cpp:88
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
const unsigned char alphabet[64+1]
Definition: base64.cpp:22
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019