39 #ifndef OME_COMMON_BASE64_H 40 #define OME_COMMON_BASE64_H 69 output_base64_char(std::string& encoded,
75 static const char *
const base64_chars =
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 77 "abcdefghijklmnopqrstuvwxyz" 80 encoded += base64_chars[value];
81 if (linebreak && (chars + 1) % linebreak == 0)
100 static const uint8_t base64_codes[] =
103 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,
105 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 98, 99, 99,
107 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
109 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99,
111 99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
113 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
116 if (c < 0x20 || c >
'z')
117 throw std::runtime_error(
"Invalid Base64 input: character outside permitted range");
118 uint8_t v = base64_codes[c - 0x20];
120 throw std::runtime_error(
"Invalid Base64 input: character outside permitted range");
140 next_base64_value(std::string::const_iterator& pos,
141 std::string::const_iterator end)
145 if (!std::isspace(*pos))
147 uint8_t value = base64_value(*pos++);
170 template<
typename Iterator>
174 uint8_t linebreak = 76)
177 encoded.reserve((std::distance(begin, end) * 4) / 3);
182 for (Iterator i = begin; i != end; ++bytes)
187 detail::output_base64_char(encoded, accum, chars++, linebreak);
188 accum = (byte & 0x3) << 4;
193 accum |= (byte & 0xF0) >> 4;
194 detail::output_base64_char(encoded, accum, chars++, linebreak);
195 accum = (byte & 0x0F) << 2;
200 accum |= (byte & 0xC0) >> 6;
201 detail::output_base64_char(encoded, accum, chars++, linebreak);
203 detail::output_base64_char(encoded, accum, chars++, linebreak);
207 detail::output_base64_char(encoded, accum, chars++, linebreak);
213 detail::output_base64_char(encoded, accum, chars++, linebreak);
231 template<
typename InsertIterator>
236 bool pad_seen =
false;
239 for (std::string::const_iterator i = base64.begin();
245 for (
int j = 0; j < 4; ++j)
247 bytes[j] = detail::next_base64_value(i, base64.end());
248 if(j == 0 && bytes[j] == 255)
254 if (bytes[1] == 255 || bytes[2] == 255 || bytes[3] == 255)
255 throw std::runtime_error(
"Invalid Base64 input: unexpected end of input");
257 if (bytes[0] < 64 && bytes[1] < 64)
260 throw std::runtime_error(
"Invalid Base64 input: padding only permitted at end of input");
262 output = bytes[0] << 2 | bytes[1] >> 4;
266 output = bytes[1] << 4 | bytes[2] >> 2;
270 output = bytes[2] << 6 | bytes[3];
285 throw(std::runtime_error(
"Invalid Base64 input: padding encountered unexpectedly"));
300 template<
typename Container>
306 decoded.reserve((base64.size() * 3) / 4);
316 #endif // OME_COMMON_BASE64_H void base64_decode(std::string base64, InsertIterator dest)
Decode a Base64-encoded string.
Definition: base64.h:233
Open Microscopy Environment C++.
Definition: base64.h:48
std::string base64_encode(Iterator begin, Iterator end, uint8_t linebreak=76)
Base64-encode a range of bytes.
Definition: base64.h:172