Bridges-C++  3.2.0
Bridges(C++API)
base64.h
Go to the documentation of this file.
1 #ifndef _BASE64_H_
2 #define _BASE64_H_
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 using namespace std;
8 
43 namespace bridges {
44  typedef unsigned char BYTE;
45 
46 
47  namespace base64 {
48 
49  const string base64_chars =
50  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51  "abcdefghijklmnopqrstuvwxyz"
52  "0123456789+/";
53 
54 
55  inline bool is_base64(BYTE c) {
56  return (isalnum(c) || (c == '+') || (c == '/'));
57  }
58 
59  string inline encode(BYTE const* buf, unsigned int bufLen) {
60  string ret;
61  int i = 0;
62  int j = 0;
63  BYTE char_array_3[3];
64  BYTE char_array_4[4];
65 
66  while (bufLen--) {
67  // 3 chars become 4 chars in base 64 representation
68  // hence the char arrays of 3 and 4
69  char_array_3[i++] = *(buf++);
70  if (i == 3) {
71  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
72  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
73  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
74  char_array_4[3] = char_array_3[2] & 0x3f;
75 
76  for (i = 0; (i < 4) ; i++)
77  ret += base64_chars[char_array_4[i]];
78  i = 0;
79  }
80  }
81 
82  // handle any left over bytes, by padding with end of string chars
83  if (i) {
84  for (j = i; j < 3; j++)
85  char_array_3[j] = '\0';
86 
87  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
88  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
89  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
90  char_array_4[3] = char_array_3[2] & 0x3f;
91 
92  for (j = 0; (j < i + 1); j++)
93  ret += base64_chars[char_array_4[j]];
94 
95  // not sure what this does!
96  while ((i++ < 3))
97  ret += '=';
98  }
99 
100  return ret;
101  }
102 
103  vector<BYTE> inline decode(string const& encoded_string) {
104  size_t in_len = encoded_string.size();
105  int i = 0;
106  // int j = 0;
107  int in_ = 0;
108  BYTE char_array_4[4], char_array_3[3];
109  vector<BYTE> ret;
110 
111  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
112  char_array_4[i++] = encoded_string[in_];
113  in_++;
114  if (i == 4) {
115  for (i = 0; i < 4; i++)
116  char_array_4[i] = (BYTE) base64_chars.find(char_array_4[i]);
117 
118  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
119  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
120  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
121 
122  for (i = 0; (i < 3); i++)
123  ret.push_back(char_array_3[i]);
124  i = 0;
125  }
126  }
127 
128  if (i) {
129  for (int j = i; j < 4; j++)
130  char_array_4[j] = 0;
131 
132  for (int j = 0; j < 4; j++)
133  char_array_4[j] = (BYTE) base64_chars.find(char_array_4[j]);
134 
135  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
136  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
137  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
138 
139  for (int j = 0; (j < i - 1); j++)
140  ret.push_back(char_array_3[j]);
141  }
142 
143  return ret;
144  }
145 
146  } // end namespace base64
147 } // end namespace bridges
148 
149 #endif
string encode(BYTE const *buf, unsigned int bufLen)
Definition: base64.h:59
STL namespace.
bool is_base64(BYTE c)
Definition: base64.h:55
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
unsigned char BYTE
Definition: base64.h:44
const string base64_chars
Definition: base64.h:49
vector< BYTE > decode(string const &encoded_string)
Definition: base64.h:103