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