39 #ifndef OME_COMMON_BASE64_H 40 #define OME_COMMON_BASE64_H 70 output_base64_char(std::string& encoded,
76 static const char *
const base64_chars =
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 78 "abcdefghijklmnopqrstuvwxyz" 81 encoded += base64_chars[value];
82 if (linebreak && (chars + 1) % linebreak == 0)
101 static const uint8_t base64_codes[] =
104 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,
106 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 98, 99, 99,
108 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
110 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99,
112 99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
114 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
117 if (c < 0x20 || c >
'z')
118 throw std::runtime_error(
"Invalid Base64 input: character outside permitted range");
119 uint8_t v = base64_codes[c - 0x20];
121 throw std::runtime_error(
"Invalid Base64 input: character outside permitted range");
141 next_base64_value(std::string::const_iterator& pos,
142 std::string::const_iterator end)
146 if (!std::isspace(*pos))
148 uint8_t value = base64_value(*pos++);
171 template<
typename Iterator>
175 uint8_t linebreak = 76)
178 encoded.reserve((std::distance(begin, end) * 4) / 3);
183 for (Iterator i = begin; i != end; ++bytes)
188 detail::output_base64_char(encoded, accum, chars++, linebreak);
189 accum = (byte & 0x3) << 4;
194 accum |= (byte & 0xF0) >> 4;
195 detail::output_base64_char(encoded, accum, chars++, linebreak);
196 accum = (byte & 0x0F) << 2;
201 accum |= (byte & 0xC0) >> 6;
202 detail::output_base64_char(encoded, accum, chars++, linebreak);
204 detail::output_base64_char(encoded, accum, chars++, linebreak);
208 detail::output_base64_char(encoded, accum, chars++, linebreak);
214 detail::output_base64_char(encoded, accum, chars++, linebreak);
232 template<
typename InsertIterator>
237 bool pad_seen =
false;
240 for (std::string::const_iterator i = base64.begin();
246 for (
int j = 0; j < 4; ++j)
248 bytes[j] = detail::next_base64_value(i, base64.end());
249 if(j == 0 && bytes[j] == 255)
255 if (bytes[1] == 255 || bytes[2] == 255 || bytes[3] == 255)
256 throw std::runtime_error(
"Invalid Base64 input: unexpected end of input");
258 if (bytes[0] < 64 && bytes[1] < 64)
261 throw std::runtime_error(
"Invalid Base64 input: padding only permitted at end of input");
263 output = bytes[0] << 2 | bytes[1] >> 4;
267 output = bytes[1] << 4 | bytes[2] >> 2;
271 output = bytes[2] << 6 | bytes[3];
286 throw(std::runtime_error(
"Invalid Base64 input: padding encountered unexpectedly"));
301 template<
typename Container>
307 decoded.reserve((base64.size() * 3) / 4);
317 #endif // OME_COMMON_BASE64_H void base64_decode(std::string base64, InsertIterator dest)
Decode a Base64-encoded string.
Definition: base64.h:234
Open Microscopy Environment C++.
Definition: base64.h:49
std::string base64_encode(Iterator begin, Iterator end, uint8_t linebreak=76)
Base64-encode a range of bytes.
Definition: base64.h:173