| 113 | |
| 114 | template <class InputIterator, class OutputIterator> |
| 115 | void decode(const InputIterator& begin, |
| 116 | const InputIterator& end, |
| 117 | OutputIterator out) { |
| 118 | InputIterator it = begin; |
| 119 | int chars; |
| 120 | |
| 121 | do { |
| 122 | uint8 input[4] = {0, 0, 0, 0}; |
| 123 | |
| 124 | // get four characters |
| 125 | chars = 0; |
| 126 | while ((chars < 4) && (it != end)) { |
| 127 | uint8 c = static_cast<char>(*it); |
| 128 | if (c == '=') { break; } // pad character marks the end of the stream |
| 129 | ++it; |
| 130 | |
| 131 | if (std::find(to_table, to_table_end, c) != to_table_end) { |
| 132 | input[chars] = from_table[c]; |
| 133 | chars++; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // output the binary data |
| 138 | if (chars >= 2) { |
| 139 | *out = static_cast<uint8>((input[0] << 2) + (input[1] >> 4)); |
| 140 | ++out; |
| 141 | if (chars >= 3) { |
| 142 | *out = static_cast<uint8>((input[1] << 4) + (input[2] >> 2)); |
| 143 | ++out; |
| 144 | if (chars >= 4) { |
| 145 | *out = static_cast<uint8>((input[2] << 6) + input[3]); |
| 146 | ++out; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | while (chars == 4); |
| 152 | } |
| 153 | } // end namespace |
| 154 | |
| 155 | #endif |