21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note that we check to ensure strings are properly padded. If the string isn’t properly<br />

padded or otherwise terminates prematurely, we return an error.<br />

#include <br />

#include <br />

static char b64revtb[256] = {<br />

-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0-15*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16-31*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /*32-47*/<br />

52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, /*48-63*/<br />

-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /*64-79*/<br />

15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /*80-95*/<br />

-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /*96-111*/<br />

41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /*112-127*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128-143*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*144-159*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*160-175*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*176-191*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*192-207*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*208-223*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*224-239*/<br />

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /*240-255*/<br />

};<br />

static unsigned int raw_base64_decode(unsigned char *in, unsigned char *out,<br />

int strict, int *err) {<br />

unsigned int result = 0, x;<br />

unsigned char buf[3], *p = in, pad = 0;<br />

*err = 0;<br />

while (!pad) {<br />

switch ((x = b64revtb[*p++])) {<br />

case -3: /* NULL TERMINATOR */<br />

if (((p - 1) - in) % 4) *err = 1;<br />

return result;<br />

case -2: /* PADDING CHARACTER. INVALID HERE */<br />

if (((p - 1) - in) % 4 < 2) {<br />

*err = 1;<br />

return result;<br />

} else if (((p - 1) - in) % 4 = = 2) {<br />

/* Make sure there's appropriate padding */<br />

if (*p != '=') {<br />

*err = 1;<br />

return result;<br />

}<br />

buf[2] = 0;<br />

pad = 2;<br />

result++;<br />

break;<br />

} else {<br />

pad = 1;<br />

result += 2;<br />

break;<br />

126 | Chapter 4: Symmetric Cryptography Fundamentals<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!