/* <BASE HREF="http://www.jti.net/brad/base64.c"> */ #include <stdio.h> #include <string.h> #include <ctype.h> #include "base64.h" static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; int DecodeBase64(char *src, char *dest, int szdest) { int destidx, state, ch; const char *pos; state = 0; destidx = 0; while ((ch = *src++) != '\0') { if (isspace(ch)) // Skip whitespace continue; if (ch == Pad64) break; pos = strchr(Base64, ch); if (pos == 0) // A non-base64 character return (-1); switch (state) { case 0: if (dest) { if (destidx >= szdest) return (-1); dest[destidx] = (pos - Base64) << 2; } state = 1; break; case 1: if (dest) { if (destidx + 1 >= szdest) return (-1); dest[destidx] |= (pos - Base64) >> 4; dest[destidx+1] = ((pos - Base64) & 0x0f) << 4 ; } destidx++; state = 2; break; case 2: if (dest) { if (destidx + 1 >= szdest) return (-1); dest[destidx] |= (pos - Base64) >> 2; dest[destidx+1] = ((pos - Base64) & 0x03) << 6; } destidx++; state = 3; break; case 3: if (dest) { if (destidx >= szdest) return (-1); dest[destidx] |= (pos - Base64); } destidx++; state = 0; break; } } // We are done decoding Base-64 chars. Let's see if we ended // on a byte boundary, and/or with erroneous trailing characters. if (ch == Pad64) // We got a pad char { ch = *src++; // Skip it, get next switch (state) { case 0: // Invalid = in first position case 1: // Invalid = in second position return (-1); case 2: // Valid, means one byte of info // Skip any number of spaces for (; ch != '\0'; ch = *src++) if (!isspace(ch)) break; // Make sure there is another trailing = sign if (ch != Pad64) return (-1); ch = *src++; // Skip the = // Fall through to "single trailing =" case // FALLTHROUGH case 3: // Valid, means two bytes of info // We know this char is an =. Is there anything but // whitespace after it? for (; ch != '\0'; ch = *src++) if (!isspace(ch)) return (-1); // Now make sure for cases 2 and 3 that the "extra" // bits that slopped past the last full byte were // zeros. If we don't check them, they become a // subliminal channel. if (dest && dest[destidx] != 0) return (-1); } } else { // We ended by seeing the end of the string. Make sure we // have no partial bytes lying around. if (state != 0) return (-1); } return (destidx); } /* ///////////////////////////////////////////////////////////////////////////// */ /* ///////////////////////////////////////////////////////////////////////////// */ /* ///////////////////////////////////////////////////////////////////////////// */ static unsigned int gsHexDecodeMap[256] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, }; static unsigned int gsHexDecodeD2[256] = { 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, 0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100,0x100, }; int DecodeBase16(char *src, int szsrc, char *dest) { int i; unsigned int val1, val2; unsigned char* p = (unsigned char*) dest; unsigned char* s = (unsigned char*) src; const int buckets = szsrc >> 2; // i.e. szsrc / 4 const int leftover = szsrc & 0x03; // i.e. szsrc % 4 if (leftover & 0x01) { // i.e if leftover is odd, // leftover==1 || leftover == 3 return -1; } // read 4 bytes, output 2. // Note on PPC G4, GCC 4.0, it's quite a bit faster to // NOT use t0,t1,t2,t3, and just put the *s++ in the gsHexDecodeMap // lookup unsigned char t0,t1,t2,t3; for (i = 0; i < buckets; ++i) { t0 = *s++; t1= *s++; t2 = *s++; t3 = *s++; val1 = gsHexDecodeD2[t0] | gsHexDecodeMap[t1]; val2 = gsHexDecodeD2[t2] | gsHexDecodeMap[t3]; if (val1 > 0xff || val2 > 0xff) return -1; *p++ = (unsigned char) val1; *p++ = (unsigned char) val2; } if (leftover == 2) { val1 = gsHexDecodeD2[s[0]] | gsHexDecodeMap[s[1]]; if (val1 > 0xff) return -1; *p++ = (unsigned char) val1; } return p - (unsigned char*)dest; } /* ///////////////////////////////////////////////////////////////////////////// */ /* ///////////////////////////////////////////////////////////////////////////// */ /* ///////////////////////////////////////////////////////////////////////////// */ #include <assert.h> #include <string.h> /* ///////////////////////////////////////////////////////////////////////////// * Constants and definitions */ #ifndef B64_DOCUMENTATION_SKIP_SECTION # define NUM_PLAIN_DATA_BYTES (3) # define NUM_ENCODED_DATA_BYTES (4) #endif /* !B64_DOCUMENTATION_SKIP_SECTION */ /* ///////////////////////////////////////////////////////////////////////////// * Warnings */ #if defined(_MSC_VER) && \ _MSC_VER < 1000 # pragma warning(disable : 4705) #endif /* _MSC_VER < 1000 */ /* ///////////////////////////////////////////////////////////////////////////// * Data */ static const char b64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const signed char b64_indexes[] = { /* 0 - 31 / 0x00 - 0x1f */ -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 /* 32 - 63 / 0x20 - 0x3f */ , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, 62, -1, -1, -1, 63 /* ... , '+', ... '/' */ , 52, 53, 54, 55, 56, 57, 58, 59 /* '0' - '7' */ , 60, 61, -1, -1, -1, -1, -1, -1 /* '8', '9', ... */ /* 64 - 95 / 0x40 - 0x5f */ , -1, 0, 1, 2, 3, 4, 5, 6 /* ..., 'A' - 'G' */ , 7, 8, 9, 10, 11, 12, 13, 14 /* 'H' - 'O' */ , 15, 16, 17, 18, 19, 20, 21, 22 /* 'P' - 'W' */ , 23, 24, 25, -1, -1, -1, -1, -1 /* 'X', 'Y', 'Z', ... */ /* 96 - 127 / 0x60 - 0x7f */ , -1, 26, 27, 28, 29, 30, 31, 32 /* ..., 'a' - 'g' */ , 33, 34, 35, 36, 37, 38, 39, 40 /* 'h' - 'o' */ , 41, 42, 43, 44, 45, 46, 47, 48 /* 'p' - 'w' */ , 49, 50, 51, -1, -1, -1, -1, -1 /* 'x', 'y', 'z', ... */ , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1 }; /* ///////////////////////////////////////////////////////////////////////////// * Helper functions */ /** This function reads in 3 bytes at a time, and translates them into 4 * characters. */ static size_t b64_encode_( unsigned char const *src , size_t srcSize , char *const dest , size_t destLen , unsigned lineLen , B64_RC *rc) { size_t total = ((srcSize + (NUM_PLAIN_DATA_BYTES - 1)) / NUM_PLAIN_DATA_BYTES) * NUM_ENCODED_DATA_BYTES; assert(NULL != rc); *rc = B64_RC_OK; if(lineLen > 0) { unsigned numLines = (total + (lineLen - 1)) / lineLen; total += 2 * (numLines - 1); } if(NULL == dest) { return total; } else if(destLen < total) { *rc = B64_RC_INSUFFICIENT_BUFFER; return 0; } else { char *p = dest; char *end = dest + destLen; size_t len = 0; for(; NUM_PLAIN_DATA_BYTES <= srcSize; srcSize -= NUM_PLAIN_DATA_BYTES) { char characters[NUM_ENCODED_DATA_BYTES]; /* * * | 0 | 1 | 2 | * * | | | | * | | | | | | | * | | | | | | | | | | | | | * | | | | | | | | | | | | | | | | | | | | | | | | | * * | 0 | 1 | 2 | 3 | * */ /* characters[0] is the 6 left-most bits of src[0] */ characters[0] = (char)((src[0] & 0xfc) >> 2); /* characters[0] is the right-most 2 bits of src[0] and the left-most 4 bits of src[1] */ characters[1] = (char)(((src[0] & 0x03) << 4) + ((src[1] & 0xf0) >> 4)); /* characters[0] is the right-most 4 bits of src[1] and the 2 left-most bits of src[2] */ characters[2] = (char)(((src[1] & 0x0f) << 2) + ((src[2] & 0xc0) >> 6)); /* characters[3] is the right-most 6 bits of src[2] */ characters[3] = (char)(src[2] & 0x3f); #ifndef __WATCOMC__ assert(characters[0] >= 0 && characters[0] < 64); assert(characters[1] >= 0 && characters[1] < 64); assert(characters[2] >= 0 && characters[2] < 64); assert(characters[3] >= 0 && characters[3] < 64); #endif /* __WATCOMC__ */ src += NUM_PLAIN_DATA_BYTES; *p++ = b64_chars[(unsigned char)characters[0]]; assert(NULL != strchr(b64_chars, *(p-1))); ++len; assert(len != lineLen); *p++ = b64_chars[(unsigned char)characters[1]]; assert(NULL != strchr(b64_chars, *(p-1))); ++len; assert(len != lineLen); *p++ = b64_chars[(unsigned char)characters[2]]; assert(NULL != strchr(b64_chars, *(p-1))); ++len; assert(len != lineLen); *p++ = b64_chars[(unsigned char)characters[3]]; assert(NULL != strchr(b64_chars, *(p-1))); if( ++len == lineLen && p != end) { *p++ = '\r'; *p++ = '\n'; len = 0; } } if(0 != srcSize) { /* Deal with the overspill, by boosting it up to three bytes (using 0s) * and then appending '=' for any missing characters. * * This is done into a temporary buffer, so we can call ourselves and * have the output continue to be written direct to the destination. */ unsigned char dummy[NUM_PLAIN_DATA_BYTES]; size_t i; for(i = 0; i < srcSize; ++i) { dummy[i] = *src++; } for(; i < NUM_PLAIN_DATA_BYTES; ++i) { dummy[i] = '\0'; } b64_encode_(&dummy[0], NUM_PLAIN_DATA_BYTES, p, NUM_ENCODED_DATA_BYTES * (1 + 2), 0, rc); for(p += 1 + srcSize; srcSize++ < NUM_PLAIN_DATA_BYTES; ) { *p++ = '='; } } return total; } } /** This function reads in a character string in 4-character chunks, and writes * out the converted form in 3-byte chunks to the destination. */ static size_t b64_decode_( char const *src , size_t srcLen , unsigned char *dest , size_t destSize , unsigned flags , char const **badChar , B64_RC *rc) { const size_t wholeChunks = (srcLen / NUM_ENCODED_DATA_BYTES); const size_t remainderBytes = (srcLen % NUM_ENCODED_DATA_BYTES); size_t maxTotal = (wholeChunks + (0 != remainderBytes)) * NUM_PLAIN_DATA_BYTES; unsigned char *dest_ = dest; ((void)remainderBytes); assert(NULL != badChar); assert(NULL != rc); *badChar = NULL; *rc = B64_RC_OK; if(NULL == dest) { return maxTotal; } else if(destSize < maxTotal) { *rc = B64_RC_INSUFFICIENT_BUFFER; return 0; } else { /* Now we iterate through the src, collecting together four characters * at a time from the Base-64 alphabet, until the end-point is reached. * * */ char const *begin = src; char const *const end = begin + srcLen; size_t currIndex = 0; size_t numPads = 0; signed char indexes[NUM_ENCODED_DATA_BYTES]; /* 4 */ for(; begin != end; ++begin) { const char ch = *begin; if('=' == ch) { assert(currIndex < NUM_ENCODED_DATA_BYTES); indexes[currIndex++] = '\0'; ++numPads; } else { signed char index = b64_indexes[(unsigned char)ch]; if(-1 == index) { switch(ch) { case ' ': case '\t': case '\b': case '\v': if(B64_F_STOP_ON_UNEXPECTED_WS & flags) { *rc = B64_RC_DATA_ERROR; *badChar = begin; return 0; } else { /* Fall through */ } case '\r': case '\n': continue; default: if(B64_F_STOP_ON_UNKNOWN_CHAR & flags) { *rc = B64_RC_DATA_ERROR; *badChar = begin; return 0; } else { continue; } } } else { numPads = 0; assert(currIndex < NUM_ENCODED_DATA_BYTES); indexes[currIndex++] = index; } } if(NUM_ENCODED_DATA_BYTES == currIndex) { unsigned char bytes[NUM_PLAIN_DATA_BYTES]; /* 3 */ bytes[0] = (unsigned char)((indexes[0] << 2) + ((indexes[1] & 0x30) >> 4)); currIndex = 0; *dest++ = bytes[0]; if(2 != numPads) { bytes[1] = (unsigned char)(((indexes[1] & 0xf) << 4) + ((indexes[2] & 0x3c) >> 2)); *dest++ = bytes[1]; if(1 != numPads) { bytes[2] = (unsigned char)(((indexes[2] & 0x3) << 6) + indexes[3]); *dest++ = bytes[2]; } } if(0 != numPads) { break; } } } return (size_t)(dest - dest_); } } /* ///////////////////////////////////////////////////////////////////////////// * API functions */ size_t b64_encode(void const *src, size_t srcSize, char *dest, size_t destLen) { /* Use Null Object (Variable) here for rc, so do not need to check * elsewhere. */ B64_RC rc_; return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, 0, &rc_); } size_t b64_encode2( void const *src , size_t srcSize , char *dest , size_t destLen , unsigned flags , int lineLen /* = -1 */ , B64_RC *rc /* = NULL */) { /* Use Null Object (Variable) here for rc, so do not need to check * elsewhere */ B64_RC rc_; if(NULL == rc) { rc = &rc_; } switch(B64_F_LINE_LEN_MASK & flags) { case B64_F_LINE_LEN_USE_PARAM: if(lineLen >= 0) { break; } /* Fall through to 64 */ case B64_F_LINE_LEN_64: lineLen = 64; break; case B64_F_LINE_LEN_76: lineLen = 76; break; default: assert(!"Bad line length flag specified to b64_encode2()"); case B64_F_LINE_LEN_INFINITE: lineLen = 0; break; } assert(0 == (lineLen % 4)); return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, (unsigned)lineLen, rc); } size_t b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize) { /* Use Null Object (Variable) here for rc and badChar, so do not need to * check elsewhere. */ char const *badChar_; B64_RC rc_; return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, B64_F_STOP_ON_NOTHING, &badChar_, &rc_); } size_t b64_decode2( char const *src , size_t srcLen , void *dest , size_t destSize , unsigned flags , char const **badChar /* = NULL */ , B64_RC *rc /* = NULL */) { char const *badChar_; B64_RC rc_; /* Use Null Object (Variable) here for rc and badChar, so do not need to * check elsewhere. */ if(NULL == badChar) { badChar = &badChar_; } if(NULL == rc) { rc = &rc_; } return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, flags, badChar, rc); } /* ////////////////////////////////////////////////////////////////////////// */
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 12234 | Matt Attaway |
Rejigger P4Web project in preparation for official sunsetting The bin directory contains the last official builds of P4Web from the Perforce download site. P4Web is soon to be completely sunsetted; these builds are here for folks who don't want to build their own. To better handle the archived builds the source code has been moved into a separate src directory. |
||
//guest/perforce_software/p4web/Main/base64.cpp | |||||
#1 | 8914 | Matt Attaway | Initial add of the P4Web source code |