From fd59f5ae69fecd7ea9fb38391ffa3db4336aa82d Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Fri, 31 Jul 2015 11:12:22 -0700 Subject: [PATCH] cmText.c : Implemented cmTextEncodeBase64(). --- cmText.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/cmText.c b/cmText.c index 1dba9ec..2448d98 100644 --- a/cmText.c +++ b/cmText.c @@ -1192,6 +1192,8 @@ cmTxRC_t cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN --yn; unsigned v = 0; + + assert( i + 4 <= xN ); v += t[(int)xV[i++]] << 18; 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 ) { - 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+/"; - return kOkTxRC; + const char* t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + 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; }