⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plx_charset.c

📁 mtk wap和mms代码。。适应mtk 25。26平台
💻 C
📖 第 1 页 / 共 5 页
字号:
/***************************************************************************
 *
 *                      Pollex Mobile Platform
 *
 * Copyright (c) 2005 by Pollex Mobile Software Co., Ltd. 
 *                       All Rights Reserved 
 *
 * Module   : Pollex OS Adaptation Layer.
 *
 * Purpose  : Implements Pollex charset conversion.
 *            
\**************************************************************************/

#ifdef MMI_ON_HARDWARE_P

#include "PixtelDataTypes.h"
#include "Unicodexdcl.h"
#include "conversions.h"
#include "plxosal.h"

#ifdef PLX_CHSET_DEBUG
	void WAP_DataOut(const unsigned char *fragment, unsigned short length, const unsigned char *name);
#endif

/****************************************************************************
* Function	PlxUCS2ToGB2312
* Purpose	convert ucs2 to gb2312
* Params	
* Return	converted length
* Remarks	
\****************************************************************************/
int  PlxUCS2ToGB2312(const char */*IN*/lpUCS2Str, int /*IN*/chInUCS2, char */*OUT*/lpGB2312Str, int /*IN*/bufLenGB2312)
{
	int srcLen  = 0;
	char *srcBuf;
	int ret;
	char *dstBuf;
	int dstLen;

	if (lpUCS2Str == NULL || chInUCS2 == 0)
	{
		return 0;
	}

	if (chInUCS2 > 0)
	{
		srcLen = pfnUnicodeStrlen((const S8 *)lpUCS2Str);
		if (srcLen <= 0)
		{
			return 0;
		}
		if (srcLen > chInUCS2)
		{
			srcBuf = plx_malloc(chInUCS2 * 2 + 2);
			if (NULL == srcBuf)
			{
				return 0;
			}
			plx_memcpy(srcBuf, lpUCS2Str, chInUCS2 * 2);
			srcBuf[chInUCS2 * 2] = srcBuf[chInUCS2 * 2 + 1] = 0;
		}
		else
		{
			srcBuf = (char *)lpUCS2Str;
		}
	}
	else
	{
		srcBuf = (char *)lpUCS2Str;
	}

	if (NULL == lpGB2312Str || bufLenGB2312 <= 0)
	{
		dstBuf = plx_malloc(srcLen);
		if (NULL == dstBuf)
		{
			if (srcLen > chInUCS2 && chInUCS2 > 0)
			{
				plx_free(srcBuf);
			}
			return 0;
		}
		dstLen = srcLen;
	}
	else
	{
		dstBuf = lpGB2312Str;
		dstLen = bufLenGB2312;
	}
	ret = mmi_chset_convert(MMI_CHSET_UCS2, MMI_CHSET_GB2312, srcBuf, dstBuf, dstLen);

	if (srcLen > chInUCS2 && chInUCS2 > 0 ) //for chInUCS2 != -1
	{
		plx_free(srcBuf);
	}
	
	if (NULL == lpGB2312Str || bufLenGB2312 <= 0)
	{
		plx_free(dstBuf);
	}
	return ret;
}

/****************************************************************************
* Function	PlxGB2312ToUCS2
* Purpose	convert gb2312 to ucs2
* Params	
* Return	converted length
* Remarks	
\****************************************************************************/
int PlxGB2312ToUCS2(const char */*IN*/lpGB2312Str, int /*IN*/chInGB2312, char */*OUT*/lpUCS2Str, int /*IN*/bufLenUCS2)
{
	int srcLen = 0;
	char *srcBuf;
	int ret;
	char *dstBuf;
	int dstLen;

	if (lpGB2312Str == NULL || chInGB2312 == 0)
	{
		return 0;
	}
	
	if (chInGB2312 > 0)
	{
		srcLen = plx_strlen((const char *)lpGB2312Str);
		if (srcLen <= 0)
		{
			return 0;
		}
		if (srcLen > chInGB2312)
		{
			srcBuf = plx_malloc(chInGB2312 + 1);
			if (NULL == srcBuf)
			{
				return 0;
			}
			plx_memcpy(srcBuf, lpUCS2Str, chInGB2312);
			srcBuf[chInGB2312] = 0;
		}
		else
		{
			srcBuf = (char *)lpGB2312Str;
		}
	}
	else
	{
		srcBuf = (char *)lpGB2312Str;
	}

	if (NULL == lpUCS2Str || bufLenUCS2 <= 0)
	{
		dstLen = srcLen * 2 + 2;
		dstBuf = plx_malloc(dstLen);
		if (NULL == dstBuf)
		{
			if (srcLen < chInGB2312 && chInGB2312 > 0)
			{
				plx_free(srcBuf);
			}
			return 0;
		}
	}
	else
	{
		dstLen = bufLenUCS2;
		dstBuf = lpUCS2Str;
	}

	ret = mmi_chset_convert(MMI_CHSET_GB2312, MMI_CHSET_UCS2, srcBuf, dstBuf, dstLen);
	
	if (srcLen > chInGB2312 && chInGB2312 > 0)
	{
		plx_free(srcBuf);
	}
	if (NULL == lpUCS2Str || bufLenUCS2 <= 0)
	{
		plx_free(dstBuf);
	}
	return ret;
}

/****************************************************************************
* Function	PlxUTF8ToUCS2
* Purpose	convert utf8 to ucs2
* Params	
* Return	converted length
* Remarks	
\****************************************************************************/
#ifdef _USE_MTK_UTF8TOUCS2_
int PlxUTF8ToUCS2(const char */*IN*/lpUTF8Str, int /*IN*/chInUTF8, char */*OUT*/lpUCS2Str, int /*IN*/bufLenUCS2)
{
	char *dstBuf;
	int srcLen;
	int dstLen;
	int ret;
	char *srcBuf;

	if (lpUTF8Str == NULL)
	{
		return 0;
	}

	srcLen = plx_strlen(lpUTF8Str);
	if (srcLen <= 0)
	{
		return 0;
	}
	if (chInUTF8 > 0 && srcLen > chInUTF8)
	{
		srcBuf = plx_malloc(chInUTF8 + 1);
		plx_strncpy(srcBuf, lpUTF8Str, chInUTF8);
		srcBuf[chInUTF8] = 0;
	}
	else
	{
		srcBuf = (char *)lpUTF8Str;
	}
	if (NULL == lpUCS2Str || bufLenUCS2 <= 0)
	{
		dstLen = srcLen * 2 + 2;
		dstBuf = plx_malloc(dstLen);
		if (NULL == dstBuf)
		{
			if (srcBuf != (char *)lpUTF8Str)
			{
				plx_free(srcBuf);
			}
			return 0;
		}
	}
	else
	{
		dstLen = bufLenUCS2;
		dstBuf = lpUCS2Str;
	}

	ret = mmi_chset_convert(MMI_CHSET_UTF8, MMI_CHSET_UCS2, (char *)srcBuf, dstBuf, dstLen);

	if (NULL == lpUCS2Str || bufLenUCS2 <= 0)
	{
		plx_free(dstBuf);
	}
	if (chInUTF8 > 0 && srcLen > chInUTF8)
	{
		plx_free(srcBuf);
	}
	return ret;
}
#else	//_USE_MTK_UTF8TOUCS2_
static int UTF82Unicode(unsigned char **lppUtf8, int *pBufSize, unsigned short *pCode)
{
    unsigned char *lpUtf8;
    int bufsize;
	
lbl_nextbyte:
    lpUtf8 = *lppUtf8;
    bufsize = *pBufSize;
	
    if (bufsize <= 0)
        return FALSE;
	
    if ((*lpUtf8 & 0xF0) == 0xE0)
    {
        if (bufsize < 3)
            return 0;
        if ((*(lpUtf8 + 1) & 0xC0) != 0x80 || (*(lpUtf8 + 2) & 0xC0) != 0x80)
        {
            (*lppUtf8)++;
            (*pBufSize)--;
            goto lbl_nextbyte;
        }
		
		if ( pCode != NULL)
			*pCode = ((*lpUtf8 & 0x0F) << 12) + ((*(lpUtf8 + 1) & 0x3F) << 6) + 
            (*(lpUtf8 + 2) & 0x3F);
        *lppUtf8 += 3;
        *pBufSize -= 3;
    }
    else if ((*lpUtf8 & 0xE0) == 0xC0)
    {
        if (bufsize < 2)
            return 0;
        if ((*(lpUtf8 + 1) & 0xC0) != 0x80)
        {
            (*lppUtf8)++;
            (*pBufSize)--;
            goto lbl_nextbyte;
        }
		
		if ( pCode != NULL)
			*pCode = ((*lpUtf8 & 0x1F) << 6) + (*(lpUtf8 + 1) & 0x3F);
        *lppUtf8 += 2;
        *pBufSize -= 2;
    }
    else if (*lpUtf8 < 0x80)
    {
		if ( pCode != NULL)
			*pCode = *lpUtf8;
        (*lppUtf8)++;
        (*pBufSize)--;
    }
    else
    {
        (*lppUtf8)++;
        (*pBufSize)--;
        goto lbl_nextbyte;
    }
	
    return 1;
}

int PlxUTF8ToUCS2(const char *lpUtf8Str, int cUtf8Len, 
					  char *lpWideCharStr, int cWideCharLen)
{
    unsigned char *lpSrc = (unsigned char *)lpUtf8Str;
    unsigned short *pwCode = (unsigned short *)lpWideCharStr;
	int len = 0;
	int lestbuflen = cWideCharLen - 2;
	
	if ( cWideCharLen == 0 ) /*in order to get the lenghth of buffer needed*/
	{
		while (UTF82Unicode(&lpSrc, &cUtf8Len, NULL))
		{
			len += 2;
		}	
		return len;
	}
	
	if ( len > lestbuflen)
	{
		return len;
	}
	
    while (UTF82Unicode(&lpSrc, &cUtf8Len, pwCode))
	{
		len += 2;	
		if ( len >= lestbuflen)
		{
			if (NULL != lpWideCharStr)
			{
				lpWideCharStr[len] = lpWideCharStr[len + 1]= 0;
			}
			return len;
		}
		
		if ( pwCode != NULL)
			pwCode++;
	}
	if (NULL != lpWideCharStr)
	{
		lpWideCharStr[len] = lpWideCharStr[len + 1]= 0;
	}
	return len;
}

#endif	//_USE_MTK_UTF8TOUCS2_

/****************************************************************************
* Function	PlxUCS2ToUTF8
* Purpose	
* Params	
* Return	
* Remarks	
\****************************************************************************/
int PlxUCS2ToUTF8(const char */*IN*/lpUCS2Str, int /*IN*/chInUCS2, char */*OUT*/lpUTF8Str, int /*IN*/bufLenUTF8)
{
	int srcLen = 0;
	char *srcBuf;
	int ret;
	char *dstBuf;
	int dstLen;
	
	if (lpUCS2Str == NULL || chInUCS2 == 0)
	{
		return 0;
	}

	if (chInUCS2 > 0)
	{
		srcLen = pfnUnicodeStrlen((const S8 *)lpUCS2Str);
		if (srcLen <= 0)
		{
			return 0;
		}
		if (srcLen > chInUCS2)
		{
			srcBuf = plx_malloc(chInUCS2 * 2 + 2);
			if (NULL == srcBuf)
			{
				return 0;
			}
			plx_memcpy(srcBuf, lpUCS2Str, chInUCS2 * 2);
			srcBuf[chInUCS2 * 2] = srcBuf[chInUCS2 * 2 + 1] = 0;
		}
		else
		{
			srcBuf = (char *)lpUCS2Str;
		}
	}
	else
	{
		srcBuf = (char *)lpUCS2Str;
	}

	if (NULL == lpUTF8Str || bufLenUTF8 <= 0)
	{
		dstLen = srcLen * 3 / 2 + 1;
		dstBuf = plx_malloc(dstLen);
		if (NULL == dstBuf)
		{
			if (srcLen > chInUCS2 && chInUCS2 > 0)
			{
				plx_free(srcBuf);
			}
			return 0;
		}
	}
	else
	{
		dstLen = bufLenUTF8;
		dstBuf = lpUTF8Str;
	}

	ret = mmi_chset_convert(MMI_CHSET_UCS2, MMI_CHSET_UTF8, srcBuf, dstBuf, dstLen);
	if (srcLen > chInUCS2 && chInUCS2 > 0)
	{
		plx_free(srcBuf);
	}
	if (NULL == lpUTF8Str || bufLenUTF8 <= 0)
	{
		plx_free(dstBuf);
	}
	return ret;
}

/****************************************************************************
* Function	PlxGB2312ToUTF8
* Purpose	convert gb2312 to utf8
* Params	
* Return	converted length
* Remarks	
\****************************************************************************/
int PlxGB2312ToUTF8(const char */*IN*/lpGB2312Str, int /*IN*/chInGB2312, char */*OUT*/lpUTF8CharStr, int /*IN*/bufLenUTF8)
{
	char *tmpBuf;
	int srcLen;
	char *srcBuf;
	int ret;
	char *dstBuf;
	int dstLen;
#ifdef PLX_CHSET_DEBUG
	PlxTrace("<PlxGB2312ToUTF8> working...");
#endif
	if (lpGB2312Str == NULL || chInGB2312 == 0)
	{
		return 0;

⌨️ 快捷键说明

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