base64Decoder.h
Go to the documentation of this file.
1 
8 #include <vector>
9 #include <string>
10 
18 inline std::vector<unsigned char> base64_to_binary(const std::string& in)
19 {
20 
21  std::string out;
22  std::string in_padded;
23 
24  // Add padding
25  if (in_padded.length() % 4 == 2)
26  {
27  in_padded = in + "==";
28  }
29 
30  else if (in_padded.length() % 4 == 3)
31  {
32  in_padded = in + "=";
33  }
34 
35  else
36  {
37  in_padded = in;
38  }
39 
40  std::vector<int> T(256, -1);
41  for (int i = 0; i < 64; i++)
42  T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] =
43  i;
44 
45  int val = 0, valb = -8;
46  for (unsigned char c : in_padded)
47  {
48  if (T[c] == -1)
49  break;
50  val = (val << 6) + T[c];
51  valb += 6;
52  if (valb >= 0)
53  {
54  out.push_back(char((val >> valb) & 0xFF));
55  valb -= 8;
56  }
57  }
58  return std::vector<unsigned char>(out.begin(), out.end());
59 }
60 
68 inline unsigned long getValueAt(std::vector<unsigned char>& bytes,
69  const unsigned int startByteIndex,
70  const unsigned int endByteIndex)
71 {
72  unsigned long out = 0;
73 
74  for (int i = startByteIndex; i <= endByteIndex; i++)
75  {
76  if (i >= bytes.size())
77  throw std::out_of_range("Out of bounds error while decoding b64.");
78  out = out | ((0x0000000000000000 | bytes[i]) << 8 * (i - startByteIndex));
79  }
80  return out;
81 }
82 
91 inline long convertValue(const unsigned long value, const unsigned int size,
92  bool isSigned)
93 {
94  if (!isSigned)
95  return static_cast<unsigned int>(value);
96  unsigned long mask = 0x0000000000000080;
97  mask = mask << (8 * (size - 1));
98  mask = mask & value;
99  for (int i = 0; i < 64 - (size * 8); i++)
100  {
101  mask = mask | (mask << 1);
102  }
103  return value | mask;
104 }
105 
134 inline long base64_decode(const std::string& in,
135  const unsigned int startByteIndex,
136  const unsigned int endByteIndex,
137  const unsigned int size, bool isSigned)
138 {
139  std::vector<unsigned char> bytes = base64_to_binary(in);
140  unsigned long value = getValueAt(bytes, startByteIndex, endByteIndex);
141  return convertValue(value, size, isSigned);
142 }
unsigned long getValueAt(std::vector< unsigned char > &bytes, const unsigned int startByteIndex, const unsigned int endByteIndex)
Concatenates bytes from index a to index b of a byte array.
Definition: base64Decoder.h:68
long convertValue(const unsigned long value, const unsigned int size, bool isSigned)
Converts a 32bit signed/unsigned integer from its bit representation to signed 64bit integer...
Definition: base64Decoder.h:91
std::vector< unsigned char > base64_to_binary(const std::string &in)
Decodes a base64 encoded string to bytes.
Definition: base64Decoder.h:18
long base64_decode(const std::string &in, const unsigned int startByteIndex, const unsigned int endByteIndex, const unsigned int size, bool isSigned)
Function decodes a Base64 string and returns the decoded string (or only parts of it) up to a maximum...


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13