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