rtsputil.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 763 行 · 第 1/2 页

CPP
763
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: rtsputil.cpp,v 1.6.32.3 2004/07/09 01:48:16 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
 * 
 * The contents of this file, and the files included with this file,
 * are subject to the current version of the RealNetworks Public
 * Source License (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (the "RCSL") available at
 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
 * will apply. You may also obtain the license terms directly from
 * RealNetworks.  You may not use this file except in compliance with
 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
 * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
 * the rights, obligations and limitations governing use of the
 * contents of the file.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the
 * portions it created.
 * 
 * This file, and the files included with this file, is distributed
 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 * 
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 * 
 * Contributor(s):
 * 
 * ***** END LICENSE BLOCK ***** */

#include "hxtypes.h"
#include "hxstring.h"
#include "dbcs.h"
#include "rtsputil.h"
#include "safestring.h"

#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE		
static const char HX_THIS_FILE[] = __FILE__;
#endif


/*
 * Table for encoding base64
 */
static const char Base64[] =
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/*
 * Table for encoding URL-safe base64
 */
static const char URLBase64[] =
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!*";

#define XX 127
/*
 * Table for decoding base64
 */
static const char Index64[256] = { /* Flawfinder: ignore */
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,62,XX,XX, XX,XX,XX,XX, XX,XX,63,62, XX,XX,XX,63,
    52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX,
    XX, 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,XX, XX,XX,XX,XX,
    XX,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,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX
};
#define CHAR64(c)  (Index64[(unsigned char)(c)])

static void 
Output64Chunk(int c1, int c2, int c3, int pads, char* pBuf,
    INT32 bufOffset)
{
    pBuf[bufOffset++] = Base64[c1>>2];
    pBuf[bufOffset++] = Base64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
    if (pads == 2) 
    {
        pBuf[bufOffset++] = '=';
        pBuf[bufOffset++] = '=';
    }
    else if (pads) 
    {
        pBuf[bufOffset++] = Base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
        pBuf[bufOffset++] = '=';
    } 
    else 
    {
        pBuf[bufOffset++] = Base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
        pBuf[bufOffset++] = Base64[c3 & 0x3F];
    }
}

static void 
OutputURL64Chunk(int c1, int c2, int c3, int pads, char* pBuf,
    INT32 bufOffset)
{
    pBuf[bufOffset++] = URLBase64[c1>>2];
    pBuf[bufOffset++] = URLBase64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
    if (pads == 2) 
    {
        pBuf[bufOffset++] = '=';
        pBuf[bufOffset++] = '=';
    }
    else if (pads) 
    {
        pBuf[bufOffset++] = URLBase64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
        pBuf[bufOffset++] = '=';
    } 
    else 
    {
        pBuf[bufOffset++] = URLBase64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
        pBuf[bufOffset++] = URLBase64[c3 & 0x3F];
    }
}

INT32 
BinTo64(const BYTE* pInBuf, INT32 len, char* pOutBuf)
{
    int c1, c2, c3;
    //INT32 ct = 0;
    //INT32 written = 0;
    INT32 inOffset = 0;
    INT32 outOffset = 0;

    while(inOffset < len)
    {
	c1 = pInBuf[inOffset++];
	if(inOffset == len)
	{
	    Output64Chunk(c1, 0, 0, 2, pOutBuf, outOffset);
	    outOffset += 4;
	}
	else
	{
	    c2 = pInBuf[inOffset++];
	    if(inOffset == len)
	    {
		Output64Chunk(c1, c2, 0, 1, pOutBuf, outOffset);
		outOffset += 4;
	    }
	    else
	    {
		c3 = pInBuf[inOffset++];
		Output64Chunk(c1, c2, c3, 0, pOutBuf, outOffset);
		outOffset += 4;
	    }
	}
#if XXXBAB
	ct += 4;
	if(ct > 44)	// EOL after 45 chars
	{
	    pOutBuf[outOffset++] = '\n';
	    written += 46;
	    ct = 0;
	}
#endif
    }
#if XXXBAB
    if(ct)
    {
	pOutBuf[outOffset++] = '\n';
	ct++;
    }
#endif
    pOutBuf[outOffset++] = '\0';
    return outOffset;
}

INT32 
BinToURL64(const BYTE* pInBuf, INT32 len, char* pOutBuf)
{
    int c1, c2, c3;
    INT32 inOffset = 0;
    INT32 outOffset = 0;

    while(inOffset < len)
    {
	c1 = pInBuf[inOffset++];
	if(inOffset == len)
	{
	    OutputURL64Chunk(c1, 0, 0, 2, pOutBuf, outOffset);
	    outOffset += 4;
	}
	else
	{
	    c2 = pInBuf[inOffset++];
	    if(inOffset == len)
	    {
		OutputURL64Chunk(c1, c2, 0, 1, pOutBuf, outOffset);
		outOffset += 4;
	    }
	    else
	    {
		c3 = pInBuf[inOffset++];
		OutputURL64Chunk(c1, c2, c3, 0, pOutBuf, outOffset);
		outOffset += 4;
	    }
	}
    }
    pOutBuf[outOffset++] = '\0';
    return outOffset;
}

INT32
BinFrom64(const char* pInBuf, INT32 len, BYTE* pOutBuf)
{
    int c1, c2, c3, c4;
    INT32 offset = 0;
    INT32 outOffset = 0;

    while(offset < len)
    {
	c1 = pInBuf[offset++];
	if(c1 != '=' && CHAR64(c1) == XX)
	{
	    continue;
	}
	else if (offset == len)
	{
	    /* BAD data */
	    return -1;
	}
	do
	{
	    c2 = pInBuf[offset++];
	} 
	while(offset < len && c2 != '=' && CHAR64(c2) == XX);
	if (offset == len)
	{
	    /* BAD data */
	    return -1;
	}
        do
	{
	    c3 = pInBuf[offset++];
	}
	while(offset < len && c3 != '=' && CHAR64(c3) == XX);
	if (offset == len)
	{
	    /* BAD data */
	    return -1;
	}
	do
	{
	    c4 = pInBuf[offset++];
	}
	while(offset < len && c4 != '=' && CHAR64(c4) == XX);
	if (offset == len && c4 != '=' && CHAR64(c4) == XX)
	{
	    /* BAD data */
	    return -1;
	}
	if(c1 == '=' || c2 == '=')
	{
	    continue;
	}
	c1 = CHAR64(c1);
	c2 = CHAR64(c2);
	pOutBuf[outOffset++]  = ((c1 << 2) | ((c2 & 0x30) >> 4));
	if(c3 == '=')
	{
	    continue;
	}
	else
	{
	    c3 = CHAR64(c3);
	    pOutBuf[outOffset++]  = (((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2));
	    if(c4 == '=')
	    {
		continue;
	    }
	    else
	    {
		c4 = CHAR64(c4);
		pOutBuf[outOffset++] = (((c3 & 0x03) << 6) | c4);
	    }
	}
    }

    return outOffset;
}

const char*
EncodeCString(const char* pStr)
{
    CHXString tmp;

    for(size_t i=0;i<strlen(pStr);++i)
    {
        switch(pStr[i])
        {
            case '\n':
            {
                tmp += "\\n";
            }
            break;

            case '\t':
            {
                tmp += "\\t";
            }
            break;

            case '\r':
            {
                tmp += "\\r";
            }
            break;

            case '\"':
            {
                tmp += "\\\"";
            }
            break;

            case '\\':
            {
                tmp += "\\\\";
            }
            break;

            default:
            {
                tmp += pStr[i];
            }
        }
    }
    return tmp;
}


#ifdef XXXBAB

void
printbuf(unsigned char* pBuf, int len)
{
    for(int i=0; i<len; i++)
    {
	printf("%02x ", pBuf[i]);
    }
    printf("\n");
}

static unsigned char testBytes[] =
    { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
      0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
      0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
      0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
      0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xb3, 0x5f, 0x78,
      0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
      0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
      0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
      0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?