MRPT  1.9.9
base64.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/common.h>
13 #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 (unsigned char c : inputData)
40  {
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, std::vector<uint8_t>& 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]] = true;
102  decoder[alphabet[i]] = static_cast<char>(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 (unsigned char c : inString)
116  {
117  if (c == '=')
118  {
119  finish_flag_found = true;
120  break;
121  }
122  if (!inalphabet[c]) continue;
123 
124  bits += decoder[c];
125  char_count++;
126  if (char_count == 4)
127  {
128  outData.push_back((bits >> 16));
129  outData.push_back(((bits >> 8) & 0xff));
130  outData.push_back((bits & 0xff));
131  bits = 0;
132  char_count = 0;
133  }
134  else
135  bits <<= 6;
136  }
137 
138  if (!finish_flag_found)
139  {
140  if (char_count)
141  {
142  std::cerr << "[decodeBase64] ERROR: base64 encoding incomplete, at"
143  "least "
144  << ((4 - char_count) * 6) << "bits truncated."
145  << std::endl;
146  errors++;
147  }
148  }
149  else
150  { /* c == '=' */
151  switch (char_count)
152  {
153  case 1:
154  std::cerr << "[decodeBase64] ERROR: base64 encoding "
155  "incomplete, at least 2 bits missing"
156  << std::endl;
157  errors++;
158  break;
159  case 2:
160  outData.push_back((bits >> 10));
161  break;
162  case 3:
163  outData.push_back((bits >> 16));
164  outData.push_back(((bits >> 8) & 0xff));
165  break;
166  }
167  }
168 
169  return errors == 0;
170 }
STL namespace.
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:88
const GLubyte * c
Definition: glext.h:6406
GLsizei const GLchar ** string
Definition: glext.h:4116
const unsigned char alphabet[64+1]
Definition: base64.cpp:23
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
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: abb8b1a1e Fri Oct 18 14:19:12 2019 +0200 at vie oct 18 14:20:13 CEST 2019