cmText.c : Implemented cmTextEncodeBase64().

This commit is contained in:
Kevin Larke 2015-07-31 11:12:22 -07:00
parent c5eaaf54b0
commit fd59f5ae69

View File

@ -1192,6 +1192,8 @@ cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN
--yn; --yn;
unsigned v = 0; unsigned v = 0;
assert( i + 4 <= xN );
v += t[(int)xV[i++]] << 18; v += t[(int)xV[i++]] << 18;
v += t[(int)xV[i++]] << 12; v += t[(int)xV[i++]] << 12;
@ -1227,11 +1229,67 @@ cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN
unsigned cmTextEncodeBase64BufferByteCount( unsigned binByteCnt ) unsigned cmTextEncodeBase64BufferByteCount( unsigned binByteCnt )
{ {
return 0; int rem = binByteCnt % 3;
binByteCnt -= rem;
int n = binByteCnt / 3 * 4;
if( rem )
n += 4;
return n;
} }
cmTxRC_t cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN ) unsigned cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN )
{ {
// const char* t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const char* t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return kOkTxRC; const char* zV = (const char*)xV;
unsigned i = 0;
unsigned j = 0;
while( 1 )
{
unsigned k = 3;
unsigned v = ((int)zV[i++]) << 16;
if( i < xN )
v += ((int)zV[i++]) << 8;
else
--k;
if( i < xN )
v += ((int)zV[i++]);
else
--k;
if( j >= yN )
break;
yV[j++] = t[ (v & 0xfc0000) >> 18 ];
if( j >= yN )
break;
yV[j++] = t[ (v & 0x03f000) >> 12 ];
if( j >= yN )
break;
if( k > 1 )
yV[j++] = t[ (v & 0x000fc0) >> 6 ];
else
yV[j++] = '=';
if( j >= yN )
break;
if( k > 2 )
yV[j++] = t[ (v & 0x00003f) >> 0 ];
else
yV[j++] = '=';
}
return j;
} }