andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "plbase64.h" andre@0: #include "prlog.h" /* For PR_NOT_REACHED */ andre@0: #include "prmem.h" /* for malloc / PR_MALLOC */ andre@0: andre@0: #include /* for strlen */ andre@0: andre@0: static unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; andre@0: andre@0: static void andre@0: encode3to4 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: PRUint32 b32 = (PRUint32)0; andre@0: PRIntn i, j = 18; andre@0: andre@0: for( i = 0; i < 3; i++ ) andre@0: { andre@0: b32 <<= 8; andre@0: b32 |= (PRUint32)src[i]; andre@0: } andre@0: andre@0: for( i = 0; i < 4; i++ ) andre@0: { andre@0: dest[i] = base[ (PRUint32)((b32>>j) & 0x3F) ]; andre@0: j -= 6; andre@0: } andre@0: andre@0: return; andre@0: } andre@0: andre@0: static void andre@0: encode2to4 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ]; andre@0: dest[1] = base[ (PRUint32)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)) ]; andre@0: dest[2] = base[ (PRUint32)((src[1] & 0x0F) << 2) ]; andre@0: dest[3] = (unsigned char)'='; andre@0: return; andre@0: } andre@0: andre@0: static void andre@0: encode1to4 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ]; andre@0: dest[1] = base[ (PRUint32)((src[0] & 0x03) << 4) ]; andre@0: dest[2] = (unsigned char)'='; andre@0: dest[3] = (unsigned char)'='; andre@0: return; andre@0: } andre@0: andre@0: static void andre@0: encode andre@0: ( andre@0: const unsigned char *src, andre@0: PRUint32 srclen, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: while( srclen >= 3 ) andre@0: { andre@0: encode3to4(src, dest); andre@0: src += 3; andre@0: dest += 4; andre@0: srclen -= 3; andre@0: } andre@0: andre@0: switch( srclen ) andre@0: { andre@0: case 2: andre@0: encode2to4(src, dest); andre@0: break; andre@0: case 1: andre@0: encode1to4(src, dest); andre@0: break; andre@0: case 0: andre@0: break; andre@0: default: andre@0: PR_NOT_REACHED("coding error"); andre@0: } andre@0: andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * PL_Base64Encode andre@0: * andre@0: * If the destination argument is NULL, a return buffer is andre@0: * allocated, and the data therein will be null-terminated. andre@0: * If the destination argument is not NULL, it is assumed to andre@0: * be of sufficient size, and the contents will not be null- andre@0: * terminated by this routine. andre@0: * andre@0: * Returns null if the allocation fails. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_Base64Encode andre@0: ( andre@0: const char *src, andre@0: PRUint32 srclen, andre@0: char *dest andre@0: ) andre@0: { andre@0: if( 0 == srclen ) andre@0: { andre@0: size_t len = strlen(src); andre@0: srclen = len; andre@0: /* Detect truncation. */ andre@0: if( srclen != len ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: } andre@0: andre@0: if( (char *)0 == dest ) andre@0: { andre@0: PRUint32 destlen; andre@0: /* Ensure all PRUint32 values stay within range. */ andre@0: if( srclen > (PR_UINT32_MAX/4) * 3 ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: destlen = ((srclen + 2)/3) * 4; andre@0: dest = (char *)PR_MALLOC(destlen + 1); andre@0: if( (char *)0 == dest ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: dest[ destlen ] = (char)0; /* null terminate */ andre@0: } andre@0: andre@0: encode((const unsigned char *)src, srclen, (unsigned char *)dest); andre@0: return dest; andre@0: } andre@0: andre@0: static PRInt32 andre@0: codetovalue andre@0: ( andre@0: unsigned char c andre@0: ) andre@0: { andre@0: if( (c >= (unsigned char)'A') && (c <= (unsigned char)'Z') ) andre@0: { andre@0: return (PRInt32)(c - (unsigned char)'A'); andre@0: } andre@0: else if( (c >= (unsigned char)'a') && (c <= (unsigned char)'z') ) andre@0: { andre@0: return ((PRInt32)(c - (unsigned char)'a') +26); andre@0: } andre@0: else if( (c >= (unsigned char)'0') && (c <= (unsigned char)'9') ) andre@0: { andre@0: return ((PRInt32)(c - (unsigned char)'0') +52); andre@0: } andre@0: else if( (unsigned char)'+' == c ) andre@0: { andre@0: return (PRInt32)62; andre@0: } andre@0: else if( (unsigned char)'/' == c ) andre@0: { andre@0: return (PRInt32)63; andre@0: } andre@0: else andre@0: { andre@0: return -1; andre@0: } andre@0: } andre@0: andre@0: static PRStatus andre@0: decode4to3 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: PRUint32 b32 = (PRUint32)0; andre@0: PRInt32 bits; andre@0: PRIntn i; andre@0: andre@0: for( i = 0; i < 4; i++ ) andre@0: { andre@0: bits = codetovalue(src[i]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: b32 <<= 6; andre@0: b32 |= bits; andre@0: } andre@0: andre@0: dest[0] = (unsigned char)((b32 >> 16) & 0xFF); andre@0: dest[1] = (unsigned char)((b32 >> 8) & 0xFF); andre@0: dest[2] = (unsigned char)((b32 ) & 0xFF); andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus andre@0: decode3to2 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: PRUint32 b32 = (PRUint32)0; andre@0: PRInt32 bits; andre@0: PRUint32 ubits; andre@0: andre@0: bits = codetovalue(src[0]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: b32 = (PRUint32)bits; andre@0: b32 <<= 6; andre@0: andre@0: bits = codetovalue(src[1]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: b32 |= (PRUint32)bits; andre@0: b32 <<= 4; andre@0: andre@0: bits = codetovalue(src[2]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: ubits = (PRUint32)bits; andre@0: b32 |= (ubits >> 2); andre@0: andre@0: dest[0] = (unsigned char)((b32 >> 8) & 0xFF); andre@0: dest[1] = (unsigned char)((b32 ) & 0xFF); andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus andre@0: decode2to1 andre@0: ( andre@0: const unsigned char *src, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: PRUint32 b32; andre@0: PRUint32 ubits; andre@0: PRInt32 bits; andre@0: andre@0: bits = codetovalue(src[0]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: ubits = (PRUint32)bits; andre@0: b32 = (ubits << 2); andre@0: andre@0: bits = codetovalue(src[1]); andre@0: if( bits < 0 ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: ubits = (PRUint32)bits; andre@0: b32 |= (ubits >> 4); andre@0: andre@0: dest[0] = (unsigned char)b32; andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus andre@0: decode andre@0: ( andre@0: const unsigned char *src, andre@0: PRUint32 srclen, andre@0: unsigned char *dest andre@0: ) andre@0: { andre@0: PRStatus rv; andre@0: andre@0: while( srclen >= 4 ) andre@0: { andre@0: rv = decode4to3(src, dest); andre@0: if( PR_SUCCESS != rv ) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: src += 4; andre@0: dest += 3; andre@0: srclen -= 4; andre@0: } andre@0: andre@0: switch( srclen ) andre@0: { andre@0: case 3: andre@0: rv = decode3to2(src, dest); andre@0: break; andre@0: case 2: andre@0: rv = decode2to1(src, dest); andre@0: break; andre@0: case 1: andre@0: rv = PR_FAILURE; andre@0: break; andre@0: case 0: andre@0: rv = PR_SUCCESS; andre@0: break; andre@0: default: andre@0: PR_NOT_REACHED("coding error"); andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * PL_Base64Decode andre@0: * andre@0: * If the destination argument is NULL, a return buffer is andre@0: * allocated and the data therein will be null-terminated. andre@0: * If the destination argument is not null, it is assumed andre@0: * to be of sufficient size, and the data will not be null- andre@0: * terminated by this routine. andre@0: * andre@0: * Returns null if the allocation fails, or if the source string is andre@0: * not well-formed. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_Base64Decode andre@0: ( andre@0: const char *src, andre@0: PRUint32 srclen, andre@0: char *dest andre@0: ) andre@0: { andre@0: PRStatus status; andre@0: PRBool allocated = PR_FALSE; andre@0: andre@0: if( (char *)0 == src ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: andre@0: if( 0 == srclen ) andre@0: { andre@0: size_t len = strlen(src); andre@0: srclen = len; andre@0: /* Detect truncation. */ andre@0: if( srclen != len ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: } andre@0: andre@0: if( srclen && (0 == (srclen & 3)) ) andre@0: { andre@0: if( (char)'=' == src[ srclen-1 ] ) andre@0: { andre@0: if( (char)'=' == src[ srclen-2 ] ) andre@0: { andre@0: srclen -= 2; andre@0: } andre@0: else andre@0: { andre@0: srclen -= 1; andre@0: } andre@0: } andre@0: } andre@0: andre@0: if( (char *)0 == dest ) andre@0: { andre@0: /* The following computes ((srclen * 3) / 4) without overflow. */ andre@0: PRUint32 destlen = (srclen / 4) * 3 + ((srclen % 4) * 3) / 4; andre@0: dest = (char *)PR_MALLOC(destlen + 1); andre@0: if( (char *)0 == dest ) andre@0: { andre@0: return (char *)0; andre@0: } andre@0: dest[ destlen ] = (char)0; /* null terminate */ andre@0: allocated = PR_TRUE; andre@0: } andre@0: andre@0: status = decode((const unsigned char *)src, srclen, (unsigned char *)dest); andre@0: if( PR_SUCCESS != status ) andre@0: { andre@0: if( PR_TRUE == allocated ) andre@0: { andre@0: PR_DELETE(dest); andre@0: } andre@0: andre@0: return (char *)0; andre@0: } andre@0: andre@0: return dest; andre@0: }