#include <cctype>
#include <stdexcept>
-namespace NMD
+namespace agent
{
using std::string;
string result;
- for (auto it = source.cbegin(); it != source.cend(); )
- {
+ for (auto it = source.cbegin(); it != source.cend();) {
int word = ((int)(unsigned char) * it) << 16;
int symbols = 2;
- if (++it != source.cend())
- {
+ if (++it != source.cend()) {
word |= ((int)(unsigned char) * it) << 8;
symbols++;
- if (++it != source.cend())
- {
+ if (++it != source.cend()) {
word |= ((int)(unsigned char) * it);
symbols++;
++it;
}
}
- for (int i = 0; i < 4; i++)
- {
- if (i < symbols)
- {
+ for (int i = 0; i < 4; i++) {
+ if (i < symbols) {
int index = (word >> (18 - i * 6)) & 0x3f;
result.push_back(list_code[index]);
- }
- else
- {
+ } else {
result.push_back('=');
}
}
string result;
- for (auto i = source.cbegin(); i != source.cend();)
- {
+ for (auto i = source.cbegin(); i != source.cend();) {
int bits_collected = 0;
unsigned int accumulator = 0;
- for (bits_collected = 0; bits_collected < 24 && i != source.cend(); ++i)
- {
+ for (bits_collected = 0; bits_collected < 24 && i != source.cend(); ++i) {
const int c = *i;
// Skip whitespace and padding. Be liberal in what you accept.
- if (std::isspace(c) || c == '=') continue;
+ if (std::isspace(c) || c == '=') {
+ continue;
+ }
- if ((c > 127) || (c < 0) || (reverse_table[c] > 63))
- {
+ if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
throw std::invalid_argument("Source contains characters not legal in a base64 encoded string.");
}
bits_collected += 6;
}
- if (bits_collected < 8) throw std::invalid_argument("Wrong source length");
+ if (bits_collected < 8) {
+ throw std::invalid_argument("Wrong source length");
+ }
- while (bits_collected >= 8)
- {
+ while (bits_collected >= 8) {
bits_collected -= 8;
result.push_back((char)((accumulator >> bits_collected) & 0xff));
}
return result;
}
-} // namespace NMD
+} // namespace agent