| 
    
       /*
 
     | 
  
  
     | 
    
        Copyright (c) 2003, Dominik Reichl <dominik.reichl@t-online.de>
 
     | 
  
  
     | 
    
        All rights reserved.
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
        LICENSE TERMS
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
        Redistribution and use in source and binary forms, with or without
 
     | 
  
  
     | 
    
        modification, are permitted provided that the following conditions are met:
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
        * Redistributions of source code must retain the above copyright notice, this
 
     | 
  
  
     | 
    
          list of conditions and the following disclaimer.
 
     | 
  
  
     | 
    
        * Redistributions in binary form must reproduce the above copyright notice,
 
     | 
  
  
     | 
    
          this list of conditions and the following disclaimer in the documentation
 
     | 
  
  
     | 
    
          and/or other materials provided with the distribution.
 
     | 
  
  
     | 
    
        * Neither the name of ReichlSoft nor the names of its contributors may be used
 
     | 
  
  
     | 
    
          to endorse or promote products derived from this software without specific
 
     | 
  
  
     | 
    
          prior written permission.
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
        DISCLAIMER
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
     | 
  
  
     | 
    
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
     | 
  
  
     | 
    
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
     | 
  
  
     | 
    
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 
     | 
  
  
     | 
    
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
     | 
  
  
     | 
    
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
     | 
  
  
     | 
    
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
     | 
  
  
     | 
    
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
     | 
  
  
     | 
    
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
     | 
  
  
     | 
    
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
     | 
  
  
     | 
    
       */
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       #include "base64.h"
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       static const char *g_pCodes =
 
     | 
  
  
     | 
    
       	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       static const UWORD8 g_pMap[256] =
 
     | 
  
  
     | 
    
       {
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
 
     | 
  
  
     | 
    
       	 52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
 
     | 
  
  
     | 
    
       	255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
 
     | 
  
  
     | 
    
       	  7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
 
     | 
  
  
     | 
    
       	 19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
 
     | 
  
  
     | 
    
       	 37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
 
     | 
  
  
     | 
    
       	 49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 
     | 
  
  
     | 
    
       	255, 255, 255, 255
 
     | 
  
  
     | 
    
       };
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       CBase64Codec::CBase64Codec()
 
     | 
  
  
     | 
    
       {
 
     | 
  
  
     | 
    
       }
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       CBase64Codec::~CBase64Codec()
 
     | 
  
  
     | 
    
       {
 
     | 
  
  
     | 
    
       }
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       bool CBase64Codec::Encode(const UWORD8 *pIn, UWORD32 uInLen, UWORD8 *pOut, UWORD32 *uOutLen)
 
     | 
  
  
     | 
    
       {
 
     | 
  
  
     | 
    
       	UWORD32 i, len2, leven;
 
     | 
  
  
     | 
    
       	UWORD8 *p;
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	RH_ASSERT(pIn != NULL);
 
     | 
  
  
     | 
    
       	RH_ASSERT(uInLen != 0);
 
     | 
  
  
     | 
    
       	RH_ASSERT(pOut != NULL);
 
     | 
  
  
     | 
    
       	RH_ASSERT(uOutLen != NULL);
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	len2 = ((uInLen + 2) / 3) << 2;
 
     | 
  
  
     | 
    
       	if((*uOutLen) < (len2 + 1)) return false;
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	p = pOut;
 
     | 
  
  
     | 
    
       	leven = 3 * (uInLen / 3);
 
     | 
  
  
     | 
    
       	for(i = 0; i < leven; i += 3)
 
     | 
  
  
     | 
    
       	{
 
     | 
  
  
     | 
    
       		*p++ = g_pCodes[pIn[0] >> 2];
 
     | 
  
  
     | 
    
       		*p++ = g_pCodes[((pIn[0] & 3) << 4) + (pIn[1] >> 4)];
 
     | 
  
  
     | 
    
       		*p++ = g_pCodes[((pIn[1] & 0xf) << 2) + (pIn[2] >> 6)];
 
     | 
  
  
     | 
    
       		*p++ = g_pCodes[pIn[2] & 0x3f];
 
     | 
  
  
     | 
    
       		pIn += 3;
 
     | 
  
  
     | 
    
       	}
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	if (i < uInLen)
 
     | 
  
  
     | 
    
       	{
 
     | 
  
  
     | 
    
       		UWORD32 a = pIn[0];
 
     | 
  
  
     | 
    
       		UWORD32 b = ((i + 1) < uInLen) ? pIn[1] : 0;
 
     | 
  
  
     | 
    
       		UWORD32 c = 0;
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       		*p++ = g_pCodes[a >> 2];
 
     | 
  
  
     | 
    
       		*p++ = g_pCodes[((a & 3) << 4) + (b >> 4)];
 
     | 
  
  
     | 
    
       		*p++ = ((i + 1) < uInLen) ? g_pCodes[((b & 0xf) << 2) + (c >> 6)] : '=';
 
     | 
  
  
     | 
    
       		*p++ = '=';
 
     | 
  
  
     | 
    
       	}
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	*p = 0; // Append NULL byte
 
     | 
  
  
     | 
    
       	*uOutLen = p - pOut;
 
     | 
  
  
     | 
    
       	return true;
 
     | 
  
  
     | 
    
       }
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       bool CBase64Codec::Decode(const UWORD8 *pIn, UWORD32 uInLen, UWORD8 *pOut, UWORD32 *uOutLen)
 
     | 
  
  
     | 
    
       {
 
     | 
  
  
     | 
    
       	UWORD32 t, x, y, z;
 
     | 
  
  
     | 
    
       	UWORD8 c;
 
     | 
  
  
     | 
    
       	INTPREF g = 3;
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	RH_ASSERT(pIn != NULL);
 
     | 
  
  
     | 
    
       	RH_ASSERT(uInLen != 0);
 
     | 
  
  
     | 
    
       	RH_ASSERT(pOut != NULL);
 
     | 
  
  
     | 
    
       	RH_ASSERT(uOutLen != NULL);
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	for(x = y = z = t = 0; x < uInLen; x++)
 
     | 
  
  
     | 
    
       	{
 
     | 
  
  
     | 
    
       		c = g_pMap[pIn[x]];
 
     | 
  
  
     | 
    
       		if(c == 255) continue;
 
     | 
  
  
     | 
    
       		if(c == 254) { c = 0; g--; }
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       		t = (t << 6) | c;
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       		if(++y == 4)
 
     | 
  
  
     | 
    
       		{
 
     | 
  
  
     | 
    
       			if((z + g) > *uOutLen) { return false; } // Buffer overflow
 
     | 
  
  
     | 
    
       			pOut[z++] = (UWORD8)((t>>16)&255);
 
     | 
  
  
     | 
    
       			if(g > 1) pOut[z++] = (UWORD8)((t>>8)&255);
 
     | 
  
  
     | 
    
       			if(g > 2) pOut[z++] = (UWORD8)(t&255);
 
     | 
  
  
     | 
    
       			y = t = 0;
 
     | 
  
  
     | 
    
       		}
 
     | 
  
  
     | 
    
       	}
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       	*uOutLen = z;
 
     | 
  
  
     | 
    
       	return true;
 
     | 
  
  
     | 
    
       }
 
     |