📄 plx_charset.c
字号:
*/
if (HIBYTE(wCode) == 0) case_no = 0;
else if ((wCode >= 0x0390L) && (wCode < 0x03B0L)) case_no = 5;
else if ((wCode >= 0x20A0L) && (wCode < 0x20B0L)) case_no = 6;
else if ((wCode >= 0x4E00L) && (wCode <= 0x9FFFL)) case_no = 1;
else if ((wCode >= 0xE000L) && (wCode <= 0xE8FFL)) case_no = 2;
else
{
int i;
for (i = 0; i <= 19; i++)
{
if (HIBYTE(wCode) == table[i]) break;
}
if (i <= 19)
case_no = 3;
else
case_no = 4; // use the default char '?'
}
switch (case_no)
{
case 0:
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
}
if (wCode <= 0x7F)
{
if (!bForSize)
lpDst[*pDstChars] = (BYTE)wCode;
count = 1;
}
else
{
// for ASCII or Greek characters in user-defined gb areas
int i = wCode - 0x80;
if (sbuni2gb[i * 2 + 1] != 0x00)
{
// in user-defined gb areas
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
lpDst[*pDstChars] = sbuni2gb[2 * i];
lpDst[*pDstChars + 1] = sbuni2gb[2 * i + 1];
}
count = 2;
}
else
{
// invisible characters
if (!bForSize)
lpDst[*pDstChars] = 0x20;
count = 1;
}
} // 0x7F
break;
case 1:
if (!bForSize)
{
long offset;
*pchMultiByte -= 2;
if (*pchMultiByte < 0)
return 0;
offset = (wCode - 0x4E00L) * 2L;
memcpy_far(lpDst + *pDstChars, (PBYTEHUGE)pFileUni2Gb + offset, 2);
}
count = 2;
break;
case 2:
if (!bForSize)
{
long offset;
*pchMultiByte -= 2;
if (*pchMultiByte < 0)
return 0;
offset = (wCode - 0xE000L + 0x5200L) * 2L;
memcpy_far(lpDst + *pDstChars, (PBYTEHUGE)pFileUni2Gb + offset, 2);
}
count = 2;
break;
case 3:
{
BYTE low;
WCHAR wval = 0x0;
char * buffer;
int start = 0, end = 0, point = 0;
end = dwOtherGb;
buffer = (char *) MemAlloc(dwOtherGb);
if (buffer == NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
memcpy_far(buffer, (PBYTEHUGE)pFileOtherGb, dwOtherGb);
while (ABS(end - start) > 4)
{
point = (start + end) / 2;
point &= 0xFFFC; //align to 4 byte
//if not assign to a BYTE var, then there will be error
//when doing or operation.
low = buffer[point];
wval = (buffer[point + 1] << 8) | low;
if (wval == wCode)
{
//found it, break from while
break; //break while (ABS(end - start) > 4)
}
else
if (wval > wCode) end = point;
else start = point;
}
if (ABS(end - start) > 4) //found it
{
if (!bForSize)
{
*pchMultiByte -= 2;
if (*pchMultiByte < 0)
return 0;
memcpy(lpDst + *pDstChars, &buffer[point + 2], 2);
}
count = 2;
MemFree(buffer);
break; //break case 3.
}
//else, not found, then go to case 4.
MemFree(buffer);
}
case 4:
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
lpDst[*pDstChars] = ' '; // '?';
}
count = 1;
break;
case 5:
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
}
{
// for ASCII or Greek characters in user-defined gb areas
int i = IsUniInUserGb(wCode);
if (i != -1)
{
// in user-defined gb areas
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
lpDst[*pDstChars] = 0xAA;
lpDst[*pDstChars + 1] = 0xA1 + i;
}
count = 2;
}
else
{
// invisible characters
if (!bForSize)
lpDst[*pDstChars] = 0x20;
count = 1;
}
}
break;
case 6:
if (!bForSize)
{
(*pchMultiByte)--;
if (*pchMultiByte < 0)
return 0;
if (wCode == 0x20AC) // for special char, euro currency
lpDst[*pDstChars] = 0x80;
else
lpDst[*pDstChars] = 0x20;
}
count = 1;
break;
default:
return 0;
} // end of switch (case_no)
*pDstChars += count;
return count;
}
#if !defined(REMOVE_GB18030_SUPPORT)
/**************************************************************************\
* Function : WideCharToGB18030
* Purpose :
* maps a wide-character string to a new GB18030 string.
\**************************************************************************/
static int WideCharToGB18030(UINT CodePage, DWORD dwFlags,
LPCWSTR lpWideCharStr, int cchWideChar,
LPSTR lpMultiByteStr, int cchMultiByte,
LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
{
WCHAR wCode;
int nDstChars;
BYTE* lpSrc = NULL;
BYTE* lpDst = NULL;
BOOL bForSize = FALSE;
/* to get the required buffer size in multi bytes */
if (cchMultiByte == 0)
bForSize = TRUE;
if (CodePage != CP_ACP ||
dwFlags != 0 || lpWideCharStr == NULL ||
lpDefaultChar != NULL || lpUsedDefaultChar != NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
if (!bForSize && lpMultiByteStr == 0)
{
/* null pointer,function fails */
ASSERT(0);
//SetLastError(1);
return 0;
}
lpSrc = (BYTE *)lpWideCharStr;
lpDst = (BYTE *)lpMultiByteStr;
nDstChars = 0;
while (cchWideChar--)
{
int count;
wCode = *lpSrc + (*(lpSrc + 1) << 8);
count = Ucs2ToGB18030(wCode, &lpDst, &nDstChars,
cchMultiByte, bForSize);
if (count == 0)
return 0;
lpSrc += 2;
}
return nDstChars;
}
#endif // REMOVE_GB18030_SUPPORT
/**************************************************************************\
* Function : WideCharToMultiByte
* Purpose :
* maps a wide-character string to a new character string.
* Params :
* codePage : Specifies the code page used to perform the
* conversion.
* dwFlags : reserved, must be zero.
* lpWideCharStr : Points to the wide-character string to be
* converted.
* cchWideChar : Specifies the number of wide characters in the
* string pointed to by the lpWideCharStr parameter.
* If this value is –1, the string is assumed to be
* null-terminated and the length is calculated
* automatically. The lengthwill include the
* null-terminator.
* lpMultiByteStr : Points to the buffer to receive the translated
* string.
* cchMultiByte : Specifies the size, in bytes, of the buffer
* pointed to by the lpMultiByteStr parameter.
* If this value is zero, the function returns
* the number of bytes required for the buffer.
* lpDefaultChar : reserved, must be zero.
* lpUsedDefaultChar : reserved, must be zero.
* Return
* Remarks
\**************************************************************************/
int PlxWideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
int cchWideChar, LPSTR lpMultiByteStr, int cchMultiByte,
LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
{
WCHAR wCode;
BYTE *lpSrc;
int nDstChars;
// TEXTMETRIC tm;
BOOL bIsGb18030 = FALSE;
// used as the flag whether need to return the converted result.
BOOL bForSize = (cchMultiByte == 0);
// record the number in widechar to be handled.
if (cchWideChar == -1)
cchWideChar = hp_wcslen(lpWideCharStr) + 1;
/* check if default font is GB18030 */
// if (GetTextMetrics(NULL, &tm))
{
#if !defined(REMOVE_GB18030_SUPPORT)
if (tm.tmCharSet == CHARSET_GB18030)
bIsGb18030 = TRUE;
#endif // REMOVE_GB18030_SUPPORT
}
#if !defined(REMOVE_GB18030_SUPPORT)
if (bIsGb18030)
{
return WideCharToGB18030(CodePage, dwFlags, lpWideCharStr,
cchWideChar, lpMultiByteStr, cchMultiByte,
lpDefaultChar, lpUsedDefaultChar);
}
#endif // REMOVE_GB18030_SUPPORT
if (!InitConvChars())
{
ASSERT(0);
//SetLastError(1);
return 0;
}
/* now we can handle only if:
* (CodePage = CP_ACP) && (dwFlags == 0) &&
* (lpDefaultChar != NULL) && (lpUsedDefaultChar == NULL)
*/
if (CodePage != CP_ACP ||
dwFlags != 0 || lpWideCharStr == NULL ||
lpDefaultChar != NULL || lpUsedDefaultChar != NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
if (!bForSize && lpMultiByteStr == 0)
{
/* null pointer,function fails */
ASSERT(0);
//SetLastError(1);
return 0;
}
lpSrc = (BYTE *)lpWideCharStr;
nDstChars = 0;
while (cchWideChar--)
{
int count;
wCode = *lpSrc + (*(lpSrc + 1) << 8);
count = Ucs2ToGB2312(CodePage, wCode, (BYTE *)lpMultiByteStr,
&nDstChars, &cchMultiByte, bForSize);
if (count == 0)
return 0;
lpSrc += 2;
}
return nDstChars;
}
static int GetUTF8CharCount(WCHAR wCode)
{
if (wCode < 0x80)
return 1;
else if (wCode < 0x800)
return 2;
else
return 3;
}
static void Unicode2UTF8(WCHAR wCode, BYTE **lppUtf8)
{
if (wCode < 0x80)
*(*lppUtf8)++ = (BYTE)wCode;
else if (wCode < 0x800)
{
*(*lppUtf8)++ = (BYTE)((wCode >> 6) | 0xC0);
*(*lppUtf8)++ = (BYTE)((wCode & 0x3F) | 0x80);
}
else
{
*(*lppUtf8)++ = (BYTE)((wCode >> 12) | 0xE0);
*(*lppUtf8)++ = (BYTE)(((wCode >> 6) & 0x3F) | 0x80);
*(*lppUtf8)++ = (BYTE)((wCode & 0x3F) | 0x80);
}
}
static BOOL UTF82Unicode(BYTE **lppUtf8, int *pBufSize, WCHAR *pCode)
{
BYTE *lpUtf8;
int bufsize;
lbl_nextbyte:
lpUtf8 = *lppUtf8;
bufsize = *pBufSize;
if (bufsize <= 0)
return FALSE;
if ((*lpUtf8 & 0xF0) == 0xE0)
{
if (bufsize < 3)
return FALSE;
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 FALSE;
if ((*(lpUtf8 + 1) & 0xC0) != 0x80)
{
(*lppUtf8)++;
(*pBufSize)--;
goto lbl_nextbyte;
}
if ( pCode != NULL)
*pCode = ((*lpUtf8 & 0x1F) << 6) + (*(lpUtf8 + 1) & 0x3F);
*lppUtf8 += 2;
*pBufSize -= 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -