Main MRPT website > C++ reference for MRPT 1.5.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] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 
24 /*---------------------------------------------------------------
25  encodeBase64
26 ---------------------------------------------------------------*/
27 void mrpt::system::encodeBase64( const vector_byte &inputData, std::string &outString )
28 {
29  outString.clear();
30  outString.reserve( inputData.size() * mrpt::utils::round(4.0/3.0) );
31 
32  int char_count = 0;
33  int bits = 0;
34  int cols = 0;
35 
36  for (size_t i=0;i<inputData.size();i++)
37  {
38  const uint8_t c = inputData[i];
39  bits += c;
40  char_count++;
41 
42  if (char_count == 3)
43  {
44  outString.push_back( alphabet[bits >> 18] );
45  outString.push_back( alphabet[(bits >> 12) & 0x3f] );
46  outString.push_back( alphabet[(bits >> 6) & 0x3f]);
47  outString.push_back( alphabet[bits & 0x3f]);
48  cols += 4;
49  if (cols == 72)
50  {
51  outString.push_back('\n');
52  cols = 0;
53  }
54  bits = 0;
55  char_count = 0;
56  }
57  else
58  {
59  bits <<= 8;
60  }
61  }
62 
63  if (char_count != 0)
64  {
65  bits <<= 16 - (8 * char_count);
66  outString.push_back( alphabet[bits >> 18]);
67  outString.push_back( alphabet[(bits >> 12) & 0x3f]);
68 
69  if (char_count == 1)
70  {
71  outString.push_back('=');
72  outString.push_back('=');
73  }
74  else
75  {
76  outString.push_back( alphabet[(bits >> 6) & 0x3f]);
77  outString.push_back( '=');
78  }
79  if (cols > 0)
80  outString.push_back('\n');
81  }
82 }
83 
84 /*---------------------------------------------------------------
85  decodeBase64
86 ---------------------------------------------------------------*/
87 bool mrpt::system::decodeBase64( const std::string &inString, vector_byte &outData )
88 {
89  static bool inalphabet[256];
90  static char decoder[256];
91 
92  static bool tablesBuilt = false;
93 
94  if (!tablesBuilt)
95  {
96  tablesBuilt = true;
97  for (int i = (sizeof(alphabet)) - 1; i >= 0 ; i--)
98  {
99  inalphabet[alphabet[i]] = 1;
100  decoder[alphabet[i]] = i;
101  }
102  }
103 
104  outData.clear();
105  outData.reserve( inString.size() * round(3.0/4.0) );
106 
107  int errors = 0;
108 
109  int char_count = 0;
110  int bits = 0;
111  bool finish_flag_found = false;
112 
113  for (size_t i=0;i<inString.size();i++)
114  {
115  const unsigned char c = inString[i];
116 
117  if (c == '=')
118  {
119  finish_flag_found = true;
120  break;
121  }
122  if (!inalphabet[c])
123  continue;
124 
125  bits += decoder[c];
126  char_count++;
127  if (char_count == 4)
128  {
129  outData.push_back((bits >> 16));
130  outData.push_back(((bits >> 8) & 0xff));
131  outData.push_back((bits & 0xff));
132  bits = 0;
133  char_count = 0;
134  }
135  else
136  bits <<= 6;
137  }
138 
139  if (!finish_flag_found)
140  {
141  if (char_count)
142  {
143  std::cerr << format("[decodeBase64] ERROR: base64 encoding incomplete, at least %d bits truncated", ((4 - char_count) * 6)) << std::endl;
144  errors++;
145  }
146  }
147  else
148  { /* c == '=' */
149  switch (char_count)
150  {
151  case 1:
152  std::cerr << "[decodeBase64] ERROR: base64 encoding incomplete, at least 2 bits missing" << std::endl;
153  errors++;
154  break;
155  case 2:
156  outData.push_back((bits >> 10));
157  break;
158  case 3:
159  outData.push_back((bits >> 16));
160  outData.push_back(((bits >> 8) & 0xff));
161  break;
162  }
163  }
164 
165  return errors==0;
166 }
167 
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
std::vector< uint8_t > vector_byte
Definition: types_simple.h:26
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
void BASE_IMPEXP encodeBase64(const vector_byte &inputData, std::string &outString)
Encode a sequence of bytes as a string in base-64.
Definition: base64.cpp:27
STL namespace.
unsigned char uint8_t
Definition: rptypes.h:43
const GLubyte * c
Definition: glext.h:5590
bool BASE_IMPEXP decodeBase64(const std::string &inString, vector_byte &outData)
Decode a base-64 string into the original sequence of bytes.
Definition: base64.cpp:87
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
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:26



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020