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-2018, 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 "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/common.h>
14 #include <mrpt/core/round.h> // round()
15 #include <iostream>
16 
17 using namespace mrpt::system;
18 using namespace std;
19 
20 // This code is based on files in the public domain:
21 // http://gd.tuwien.ac.at/infosys/mail/vm
22 
23 const unsigned char alphabet[64 + 1] =
24  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
25 
26 /*---------------------------------------------------------------
27  encodeBase64
28 ---------------------------------------------------------------*/
30  const std::vector<uint8_t>& inputData, std::string& outString)
31 {
32  outString.clear();
33  outString.reserve(inputData.size() * mrpt::round(4.0 / 3.0));
34 
35  int char_count = 0;
36  int bits = 0;
37  int cols = 0;
38 
39  for (size_t i = 0; i < inputData.size(); i++)
40  {
41  const uint8_t c = inputData[i];
42  bits += c;
43  char_count++;
44 
45  if (char_count == 3)
46  {
47  outString.push_back(alphabet[bits >> 18]);
48  outString.push_back(alphabet[(bits >> 12) & 0x3f]);
49  outString.push_back(alphabet[(bits >> 6) & 0x3f]);
50  outString.push_back(alphabet[bits & 0x3f]);
51  cols += 4;
52  if (cols == 72)
53  {
54  outString.push_back('\n');
55  cols = 0;
56  }
57  bits = 0;
58  char_count = 0;
59  }
60  else
61  {
62  bits <<= 8;
63  }
64  }
65 
66  if (char_count != 0)
67  {
68  bits <<= 16 - (8 * char_count);
69  outString.push_back(alphabet[bits >> 18]);
70  outString.push_back(alphabet[(bits >> 12) & 0x3f]);
71 
72  if (char_count == 1)
73  {
74  outString.push_back('=');
75  outString.push_back('=');
76  }
77  else
78  {
79  outString.push_back(alphabet[(bits >> 6) & 0x3f]);
80  outString.push_back('=');
81  }
82  if (cols > 0) outString.push_back('\n');
83  }
84 }
85 
86 /*---------------------------------------------------------------
87  decodeBase64
88 ---------------------------------------------------------------*/
90  const std::string& inString, std::vector<uint8_t>& outData)
91 {
92  static bool inalphabet[256];
93  static char decoder[256];
94 
95  static bool tablesBuilt = false;
96 
97  if (!tablesBuilt)
98  {
99  tablesBuilt = true;
100  for (int i = (sizeof(alphabet)) - 1; i >= 0; i--)
101  {
102  inalphabet[alphabet[i]] = 1;
103  decoder[alphabet[i]] = i;
104  }
105  }
106 
107  outData.clear();
108  outData.reserve(inString.size() * round(3.0 / 4.0));
109 
110  int errors = 0;
111 
112  int char_count = 0;
113  int bits = 0;
114  bool finish_flag_found = false;
115 
116  for (size_t i = 0; i < inString.size(); i++)
117  {
118  const unsigned char c = inString[i];
119 
120  if (c == '=')
121  {
122  finish_flag_found = true;
123  break;
124  }
125  if (!inalphabet[c]) continue;
126 
127  bits += decoder[c];
128  char_count++;
129  if (char_count == 4)
130  {
131  outData.push_back((bits >> 16));
132  outData.push_back(((bits >> 8) & 0xff));
133  outData.push_back((bits & 0xff));
134  bits = 0;
135  char_count = 0;
136  }
137  else
138  bits <<= 6;
139  }
140 
141  if (!finish_flag_found)
142  {
143  if (char_count)
144  {
145  std::cerr << "[decodeBase64] ERROR: base64 encoding incomplete, at"
146  "least "
147  << ((4 - char_count) * 6) << "bits truncated."
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 }
string_utils.h
c
const GLubyte * c
Definition: glext.h:6313
system-precomp.h
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::system::decodeBase64
bool decodeBase64(const std::string &inString, std::vector< uint8_t > &outData)
Decode a base-64 string into the original sequence of bytes.
Definition: base64.cpp:89
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23
round.h
common.h
mrpt::system::encodeBase64
void encodeBase64(const std::vector< uint8_t > &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
Definition: base64.cpp:29
string
GLsizei const GLchar ** string
Definition: glext.h:4101
alphabet
const unsigned char alphabet[64+1]
Definition: base64.cpp:23
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST