MCPcopy Create free account
hub / github.com/davisking/dlib / decode

Method decode

dlib/base64/base64_kernel_1.cpp:309–396  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

307// ----------------------------------------------------------------------------------------
308
309 void base64::
310 decode (
311 std::istream& in_,
312 std::ostream& out_
313 ) const
314 {
315 using namespace std;
316 streambuf& in = *in_.rdbuf();
317 streambuf& out = *out_.rdbuf();
318
319 unsigned char inbuf[4];
320 unsigned char outbuf[3];
321 int inbuf_pos = 0;
322 streamsize status = in.sgetn(reinterpret_cast<char*>(inbuf),1);
323
324 // only count this character if it isn't some kind of filler
325 if (status == 1 && decode_table[inbuf[0]] != bad_value )
326 ++inbuf_pos;
327
328 unsigned char c1, c2, c3, c4, c5, c6;
329 streamsize outsize;
330
331 // while we haven't hit the end of the input stream
332 while (status != 0)
333 {
334 // if we have 4 valid characters
335 if (inbuf_pos == 4)
336 {
337 inbuf_pos = 0;
338
339 // this might be the end of the encoded data so we need to figure out if
340 // there was any padding applied.
341 outsize = 3;
342 if (inbuf[3] == '=')
343 {
344 if (inbuf[2] == '=')
345 outsize = 1;
346 else
347 outsize = 2;
348 }
349
350 // decode the incoming characters
351 inbuf[0] = decode_table[inbuf[0]];
352 inbuf[1] = decode_table[inbuf[1]];
353 inbuf[2] = decode_table[inbuf[2]];
354 inbuf[3] = decode_table[inbuf[3]];
355
356
357 // now pack these guys into bytes rather than 6 bit chunks
358 c1 = inbuf[0]<<2;
359 c2 = inbuf[1]>>4;
360 c3 = inbuf[1]<<4;
361 c4 = inbuf[2]>>2;
362 c5 = inbuf[2]<<6;
363 c6 = inbuf[3];
364
365 outbuf[0] = c1|c2;
366 outbuf[1] = c3|c4;

Callers

nothing calls this directly

Calls 2

decode_errorClass · 0.70
strMethod · 0.45

Tested by

no test coverage detected