| 160 | // ---------------------------------------------------------------------------------------- |
| 161 | |
| 162 | void base64:: |
| 163 | encode ( |
| 164 | std::istream& in_, |
| 165 | std::ostream& out_ |
| 166 | ) const |
| 167 | { |
| 168 | using namespace std; |
| 169 | streambuf& in = *in_.rdbuf(); |
| 170 | streambuf& out = *out_.rdbuf(); |
| 171 | |
| 172 | unsigned char inbuf[3]; |
| 173 | unsigned char outbuf[4]; |
| 174 | streamsize status = in.sgetn(reinterpret_cast<char*>(&inbuf),3); |
| 175 | |
| 176 | unsigned char c1, c2, c3, c4, c5, c6; |
| 177 | |
| 178 | int counter = 19; |
| 179 | |
| 180 | // while we haven't hit the end of the input stream |
| 181 | while (status != 0) |
| 182 | { |
| 183 | if (counter == 0) |
| 184 | { |
| 185 | counter = 19; |
| 186 | // write a newline |
| 187 | char ch; |
| 188 | switch (eol_style) |
| 189 | { |
| 190 | case CR: |
| 191 | ch = '\r'; |
| 192 | if (out.sputn(&ch,1)!=1) |
| 193 | throw std::ios_base::failure("error occurred in the base64 object"); |
| 194 | break; |
| 195 | case LF: |
| 196 | ch = '\n'; |
| 197 | if (out.sputn(&ch,1)!=1) |
| 198 | throw std::ios_base::failure("error occurred in the base64 object"); |
| 199 | break; |
| 200 | case CRLF: |
| 201 | ch = '\r'; |
| 202 | if (out.sputn(&ch,1)!=1) |
| 203 | throw std::ios_base::failure("error occurred in the base64 object"); |
| 204 | ch = '\n'; |
| 205 | if (out.sputn(&ch,1)!=1) |
| 206 | throw std::ios_base::failure("error occurred in the base64 object"); |
| 207 | break; |
| 208 | default: |
| 209 | DLIB_CASSERT(false,"this should never happen"); |
| 210 | } |
| 211 | } |
| 212 | --counter; |
| 213 | |
| 214 | if (status == 3) |
| 215 | { |
| 216 | // encode the bytes in inbuf to base64 and write them to the output stream |
| 217 | c1 = inbuf[0]&0xfc; |
| 218 | c2 = inbuf[0]&0x03; |
| 219 | c3 = inbuf[1]&0xf0; |
nothing calls this directly
no outgoing calls
no test coverage detected