📄 parse.cpp
字号:
}
return FALSE;
}
case 0x81:
case 0x82:
{
// Make sure at least 3 bytes of data
if ( (pszIn+3) > pszInEnd )
{
return FALSE;
}
// Second octet is # of characters in the string
BYTE Length = (BYTE)*pszIn++;
WCHAR UCSBase;
if (c==0x81)
{
// Third octet is bits 14-7 of the base pointer
UCSBase = CAST_CTOW(*pszIn++) << 7;
}
else
{
// Third & fourth octets are the base pointer
UCSBase = CAST_CTOW(*pszIn++) << 8;
UCSBase += CAST_CTOW(*pszIn++);
}
if (pszIn>=pszInEnd)
{
return FALSE;
}
UINT uiOutputLength = (pwszOutEnd > pwszOut) ? (pwszOutEnd - pwszOut) : 0;
UINT ruiCharactersUsed = 0;
BOOL ConvertResult = FALSE;
if ( uiOutputLength && Length)
{
ConvertResult = ConvertGSMMixedToUnicode(UCSBase, pszIn, Length, pwszOut, uiOutputLength, ruiCharactersUsed);
if ( ConvertResult )
{
if ( ruiCharactersUsed < uiOutputLength)
{
pwszOut[ruiCharactersUsed] = L'\0';
}
else
{
pwszOut[uiOutputLength] = L'\0';
}
}
else
{
pwszOut[0] = L'\0';
}
}
return ConvertResult;
}
default:
{
// Normal string data - similar to ParseString above
while (pwszOut < pwszOutEnd)
{
GSMCharToUnicode(c, pwszOut);
pwszOut++;
if (pszIn==pszInEnd)
{
*pwszOut = L'\0';
return TRUE;
}
c = *pszIn++;
}
return FALSE;
}
}
return FALSE;
}
BOOL ParseStringHexW(LPCSTR pszIn, const LPCSTR pszInEnd, LPWSTR pwszOut, const LPWSTR pwszOutEnd)
{
BOOL bRet;
// Handle empty string (must have at least two characters).
if ( (pszIn+2) > pszInEnd )
{
*pwszOut = L'\0';
return TRUE;
}
UINT cbInTmp = ((pszInEnd-pszIn)>>1);
LPSTR pszInTmp = (LPSTR)LocalAlloc(0,cbInTmp);
if (!pszInTmp)
{
return FALSE;
}
bRet = GSMHexToGSM(pszIn, pszInEnd-pszIn, pszInTmp, cbInTmp, cbInTmp);
if (!bRet)
{
goto Exit;
}
bRet = ParseStringGSMW(pszInTmp, pszInTmp+cbInTmp, pwszOut, pwszOutEnd);
Exit:
LocalFree(pszInTmp);
return bRet;
}
BOOL ParseStringUnicodeW(LPCSTR pszIn, const LPCSTR pszInEnd, LPWSTR pwszOut, const LPWSTR pwszOutEnd)
{
// Handle empty string
if (pszIn==pszInEnd)
{
*pwszOut = L'\0';
return TRUE;
}
UINT cbLen = 0;
BOOL fRet = ConvertToUnicode(ENCODING_GSMDEFAULT_UNICODE,
pszIn, ( pszInEnd - pszIn ),
pwszOut, ( pwszOutEnd - pwszOut ),
cbLen);
if ( fRet )
{
if ( cbLen < (UINT)( pwszOutEnd - pwszOut ) )
{
pwszOut[cbLen] = L'\0';
}
else
{
fRet = FALSE;
}
}
return fRet;
}
BOOL ParseStringUTF8W(LPCSTR pszIn, const LPCSTR pszInEnd, LPWSTR pwszOut, const LPWSTR pwszOutEnd)
{
// Handle empty string
if (pszIn==pszInEnd)
{
*pwszOut = L'\0';
return TRUE;
}
UINT cbLen = 0;
BOOL fRet = ConvertToUnicode(ENCODING_GSMDEFAULT_UTF8,
pszIn, ( pszInEnd - pszIn ),
pwszOut, ( pwszOutEnd - pwszOut ),
cbLen);
if ( fRet )
{
if ( cbLen < (UINT)( pwszOutEnd - pwszOut ) )
{
pwszOut[cbLen] = L'\0';
}
else
{
fRet = FALSE;
}
}
return fRet;
}
BOOL ParseEncodedString(ENCODING_TYPE enc, LPCSTR pszIn, const LPCSTR pszInEnd, LPWSTR pwszOut, const LPWSTR pwszOutEnd)
{
switch (enc)
{
case ENCODING_GSMDEFAULT:
return ParseStringGSMW(pszIn, pszInEnd, pwszOut, pwszOutEnd);
case ENCODING_GSMDEFAULT_HEX:
return ParseStringHexW(pszIn, pszInEnd, pwszOut, pwszOutEnd);
case ENCODING_GSMDEFAULT_UNICODE:
return ParseStringUnicodeW(pszIn, pszInEnd, pwszOut, pwszOutEnd);
case ENCODING_GSMDEFAULT_UTF8:
return ParseStringUTF8W(pszIn, pszInEnd, pwszOut, pwszOutEnd);
}
return FALSE;
}
BOOL ParseQuotedEncodedString(ENCODING_TYPE enc, LPCSTR& pszIn, LPWSTR pwszOut, const LPWSTR pwszOutEnd)
{
LPCSTR pszInTmp = pszIn;
// Skip over the leading quote
DWORD c;
c = *pszInTmp++;
if (c != '\"')
{
return FALSE;
}
// Calculate string length
LPCSTR pszInEnd = strchr(pszInTmp, '\"');
if (!pszInEnd)
{
return FALSE;
}
if (!ParseEncodedString(enc, pszInTmp, pszInEnd, pwszOut, pwszOutEnd))
{
return FALSE;
}
pszIn = pszInEnd+1;
return TRUE;
}
#ifdef TF_FEATURE_RUSSIA_UNICOD81
//ning wenfeng add to encode to UCS 81
BOOL AppendStringUCS281(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd)
{
LPSTR pszOutTmp = pszOut;
LPCWSTR pwszInTmp = pwszIn;
WCHAR wc=0;
UINT cbIn,i;
cbIn = _tcslen(pwszIn);
if (pszOutTmp>=pszOutEnd)
{
return FALSE;
}
*pszOutTmp++ = (BYTE)0x81;
*pszOutTmp++ = (BYTE)cbIn;
*pszOutTmp++ =(BYTE)0;
for(i = 0 ; i < cbIn ; i++)
{
wc=*pwszInTmp++;
if (pszOutTmp >= pszOutEnd)
{
return FALSE;
}
if(wc <= 0x007f)
{
*pszOutTmp++=(BYTE)(wc);
}
else
{
*pszOutTmp = (BYTE)(wc);
*pszOutTmp++ = *pszOutTmp|0x80;
if(*(pszOut+2)==0)
{
*(pszOut+2)=(BYTE)(wc>>7);
}
}
}
pszOut = pszOutTmp;
g_EncodeType = ENCODE_UCS281;
return TRUE;
}
#endif
BOOL AppendStringUCS2W(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd)
{
LPSTR pszOutTmp = pszOut;
LPCWSTR pwszInTmp = pwszIn;
WCHAR wc;
if (pszOutTmp>=pszOutEnd)
{
return FALSE;
}
*pszOutTmp++ = (BYTE)0x80;
// Iterate through all the characters in the buffer and store
pwszInTmp = pwszIn;
while (wc=*pwszInTmp++)
{
if (pszOutTmp>=pszOutEnd)
{
return FALSE;
}
*pszOutTmp++=(BYTE)(wc>>8);
if (pszOutTmp>=pszOutEnd)
{
return FALSE;
}
*pszOutTmp++=(BYTE)(wc);
}
pszOut = pszOutTmp;
g_EncodeType = ENCODE_UCS2;
return TRUE;
}
BOOL AppendStringGSMW(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd, BOOL bAllowUnicode, BOOL bFilterGsmChars)
{
LPSTR pszOutTmp = pszOut;
LPCWSTR pwszInTmp = pwszIn;
WCHAR wc;
while (wc=*pwszInTmp++)
{
// If character can't be converted to GSM,
// or if it's a control character (which can screw up the module)
// or if it's a double-quote character (which will also screw up the module)
// Then skip it (or switch to Unicode if allowed)
char c;
if ( (!UnicodeCharToGSM(wc, &c)) ||
(bFilterGsmChars && ((c<32) || (c=='\"'))) )
{
if (bAllowUnicode)
{
#ifdef TF_FEATURE_RUSSIA_UNICOD81
#pragma message("TF_FEATURE_RUSSIA_UNICOD81")
RETAILMSG(1 ,(TEXT("[TI]TF_FEATURE_RUSSIA_UNICOD81\r\n")));
if(wc>=0x0384&&wc<=0x045f)
{
//Append in 81 mode
return AppendStringUCS281(pwszIn,pszOut,pszOutEnd);
}
else
{
// Append in unicode mode
return AppendStringUCS2W(pwszIn,pszOut,pszOutEnd);
}
#else
#pragma message("NO TF_FEATURE_RUSSIA_UNICOD81")
{
RETAILMSG(1 ,(TEXT("[TI]NO TF_FEATURE_RUSSIA_UNICOD81\r\n")));
// Append in unicode mode
return AppendStringUCS2W(pwszIn,pszOut,pszOutEnd);
}
#endif
}
else
{
// Skip the character
continue;
}
}
if (pszOutTmp>=pszOutEnd)
{
return FALSE;
}
*pszOutTmp++=c;
}
pszOut = pszOutTmp;
g_EncodeType = ENCODE_ASCII;
return TRUE;
}
BOOL AppendStringHexW(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd, BOOL bAllowUnicode)
{
BOOL bRet;
UINT cbIn, cbOut;
// Allocate a buffer to hold the raw (non-hex) gsm characters
cbIn = _tcslen(pwszIn);
#ifdef TF_FEATURE_RUSSIA_UNICOD81
// If we might use 81code, we need enough space in the intermediate buffer to hold the 81code format,
// which takes 1 bytes per 81code char, plus 3 additional byte at the beginning.
if (bAllowUnicode)
{
cbIn = (cbIn<<1) + 2;
}
#else
// If we might use unicode, we need enough space in the intermediate buffer to hold the Unicode format,
// which takes 2 bytes per unicode char, plus 1 additional byte at the beginning.
if (bAllowUnicode)
{
cbIn = (cbIn<<1) + 1;
}
#endif
// Handle empty string.
if (cbIn==0)
{
return TRUE;
}
LPSTR pszIn = (LPSTR)LocalAlloc(0,cbIn);
if (!pszIn)
{
return FALSE;
}
// Pass in a temp ptr, since this will update the ptr and we don't want to lose pszIn.
LPSTR pszInTmp=pszIn;
bRet = AppendStringGSMW(pwszIn, pszInTmp, pszInTmp + cbIn, bAllowUnicode, FALSE);
if (!bRet)
{
goto Exit;
}
cbIn = pszInTmp-pszIn;
cbOut = pszOutEnd-pszOut;
bRet = GSMToGSMHex(pszIn, cbIn, pszOut, cbOut, cbOut);
pszOut = pszOut + cbOut;
g_bEncodeHex = TRUE;
Exit:
LocalFree(pszIn);
return bRet;
}
BOOL AppendStringUnicodeW(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd, BOOL bAllowUnicode)
{
UINT cbIn = _tcslen( pwszIn );
UINT cbOut = ( pszOutEnd - pszOut );
UINT cbLen = 0;
BOOL fRet = ConvertFromUnicode( ENCODING_GSMDEFAULT_UNICODE, pwszIn, cbIn, pszOut, cbOut, cbLen );
if ( fRet )
{
pszOut += cbLen;
if ( cbLen < cbOut )
{
*pszOut = '\0';
}
else
{
fRet = FALSE;
}
}
g_EncodeType = ENCODE_UNKOWN;
return fRet;
}
BOOL AppendStringUTF8W(LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd, BOOL bAllowUnicode)
{
UINT cbIn = _tcslen( pwszIn );
UINT cbOut = ( pszOutEnd - pszOut );
UINT cbLen = 0;
BOOL fRet = ConvertFromUnicode( ENCODING_GSMDEFAULT_UTF8, pwszIn, cbIn, pszOut, cbOut, cbLen );
if ( fRet )
{
pszOut += cbLen;
if ( cbLen < cbOut )
{
*pszOut = '\0';
}
else
{
fRet = FALSE;
}
}
g_EncodeType = ENCODE_UNKOWN;
return fRet;
}
BOOL AppendEncodedString(ENCODING_TYPE enc, LPCWSTR pwszIn, LPSTR& pszOut, const LPSTR pszOutEnd, BOOL bAllowUnicode)
{
switch (enc)
{
case ENCODING_GSMDEFAULT:
return AppendStringGSMW(pwszIn,pszOut,pszOutEnd,bAllowUnicode,TRUE);
case ENCODING_GSMDEFAULT_HEX:
return AppendStringHexW(pwszIn,pszOut,pszOutEnd,bAllowUnicode);
case ENCODING_GSMDEFAULT_UNICODE:
return AppendStringUnicodeW(pwszIn,pszOut,pszOutEnd,bAllowUnicode);
case ENCODING_GSMDEFAULT_UTF8:
return AppendStringUTF8W(pwszIn,pszOut,pszOutEnd,bAllowUnicode);
default:
return FALSE;
}
return FALSE;
}
BOOL AppendQuotedEncodedString(ENCODING_TYPE enc, LPCWSTR pwszIn, LPSTR& pszOut, const PUCHAR pszOutEnd, BOOL bAllowUnicode)
{
BOOL bResult;
LPSTR pszOutTmp = pszOut;
if (pszOutTmp>=(LPSTR)pszOutEnd)
{
return FALSE;
}
*pszOutTmp++='\"';
if (pszOut>=(LPSTR)pszOutEnd)
{
return FALSE;
}
bResult = AppendEncodedString(enc,pwszIn,pszOutTmp,(LPSTR)pszOutEnd,bAllowUnicode);
if (!bResult)
{
return FALSE;
}
*pszOutTmp++='\"';
if (pszOutTmp>=(LPSTR)pszOutEnd)
{
return FALSE;
}
*pszOutTmp='\0'; // Put on a NULL just to be safe
pszOut = pszOutTmp;
return TRUE;
}
//
//
//
BOOL ParseUnquotedString(const LPCSTR szData, const char chDelimiter, const LPSTR szOut, const UINT cbOut, LPCSTR& rszPointer)
{
// FUNCTION_TRACE(ParseUnquotedString);
LPSTR pchOut;
LPSTR szOutEnd;
BOOL fRet = FALSE;
rszPointer = szData;
pchOut = szOut;
szOutEnd = szOut + cbOut;
// Copy the string data until we encounter the delimiter character
while (chDelimiter != *rszPointer && pchOut < szOutEnd - 1)
{
*pchOut++ = *rszPointer++;
}
*pchOut = '\0';
// Skip forward until the delimiter character, if we haven't reached it yet
if (chDelimiter != *rszPointer)
{
rszPointer = strchr(rszPointer, chDelimiter);
if (!rszPointer)
{
// We didn't find the delimiter in the string -- fail
goto Error;
}
}
fRet = TRUE;
Error:
if (!fRet)
{
rszPointer = szData;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -