binary.cpp
Go to the documentation of this file.
1 #include "yaml-cpp-pm/binary.h"
2 #include "yaml-cpp-pm/node.h"
3 
4 namespace YAML_PM
5 {
6  static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
7 
8  std::string EncodeBase64(const unsigned char *data, std::size_t size)
9  {
10  const char PAD = '=';
11 
12  std::string ret;
13  ret.resize(4 * size / 3 + 3);
14  char *out = &ret[0];
15 
16  std::size_t chunks = size / 3;
17  std::size_t remainder = size % 3;
18 
19  for(std::size_t i=0;i<chunks;i++, data += 3) {
20  *out++ = encoding[data[0] >> 2];
21  *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
22  *out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
23  *out++ = encoding[data[2] & 0x3f];
24  }
25 
26  switch(remainder) {
27  case 0:
28  break;
29  case 1:
30  *out++ = encoding[data[0] >> 2];
31  *out++ = encoding[((data[0] & 0x3) << 4)];
32  *out++ = PAD;
33  *out++ = PAD;
34  break;
35  case 2:
36  *out++ = encoding[data[0] >> 2];
37  *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
38  *out++ = encoding[((data[1] & 0xf) << 2)];
39  *out++ = PAD;
40  break;
41  }
42 
43  ret.resize(out - &ret[0]);
44  return ret;
45  }
46 
47  static const unsigned char decoding[] = {
48  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
49  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
50  255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
51  52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
52  255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
53  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
54  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
55  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
56  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
57  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
58  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
59  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
60  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
61  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
62  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
63  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
64  };
65 
66  std::vector<unsigned char> DecodeBase64(const std::string& input)
67  {
68  typedef std::vector<unsigned char> ret_type;
69  if(input.empty())
70  return ret_type();
71 
72  ret_type ret(3 * input.size() / 4 + 1);
73  unsigned char *out = &ret[0];
74 
75  unsigned value = 0;
76  for(std::size_t i=0;i<input.size();i++) {
77  unsigned char d = decoding[static_cast<unsigned>(input[i])];
78  if(d == 255)
79  return ret_type();
80 
81  value = (value << 6) | d;
82  if(i % 4 == 3) {
83  *out++ = value >> 16;
84  if(i > 0 && input[i - 1] != '=')
85  *out++ = value >> 8;
86  if(input[i] != '=')
87  *out++ = value;
88  }
89  }
90 
91  ret.resize(out - &ret[0]);
92  return ret;
93  }
94 
95  void operator >> (const Node& node, Binary& binary)
96  {
97  std::string scalar;
98  node.GetScalar(scalar);
99  std::vector<unsigned char> data = DecodeBase64(scalar);
100  binary.swap(data);
101  }
102 }
std::vector< unsigned char > DecodeBase64(const std::string &input)
Definition: binary.cpp:66
static const char encoding[]
Definition: binary.cpp:6
::std::string string
Definition: gtest.h:1979
data
Definition: icp.py:50
bool GetScalar(std::string &s) const
Definition: node.cpp:201
void operator>>(const Node &node, Binary &binary)
Definition: binary.cpp:95
static const unsigned char decoding[]
Definition: binary.cpp:47
std::string EncodeBase64(const unsigned char *data, std::size_t size)
Definition: binary.cpp:8
void swap(std::vector< unsigned char > &rhs)
Definition: binary.h:27


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:36:30