[winpr,crt] Added unicode conversion backends
* Support apple with NSString * Support android via JNI * Split ICU to own file
This commit is contained in:
parent
00ee213f97
commit
5ae159303f
@ -64,7 +64,6 @@ option(WITH_DEBUG_SYMBOLS "Pack debug symbols to installer" OFF)
|
||||
option(WITH_NATIVE_SSPI "Use native SSPI modules" ${NATIVE_SSPI})
|
||||
option(WITH_SMARTCARD_INSPECT "Enable SmartCard API Inspector" OFF)
|
||||
option(WITH_DEBUG_MUTEX "Print mutex debug messages" ${DEFAULT_DEBUG_OPTION})
|
||||
option(WITH_ICU "Use ICU for unicode conversion" OFF)
|
||||
option(WITH_GSSAPI "Compile support for kerberos authentication. (EXPERIMENTAL)" OFF)
|
||||
option(WITH_INTERNAL_MD4 "Use compiled in md4 hash functions instead of OpenSSL/MBedTLS" OFF)
|
||||
option(WITH_INTERNAL_MD5 "Use compiled in md5 hash functions instead of OpenSSL/MBedTLS" OFF)
|
||||
|
@ -27,7 +27,6 @@
|
||||
#cmakedefine WITH_EVENTFD_READ_WRITE
|
||||
|
||||
#cmakedefine WITH_NATIVE_SSPI
|
||||
#cmakedefine WITH_ICU
|
||||
#cmakedefine WITH_SPNEGO
|
||||
#cmakedefine WITH_INTERNAL_MD4
|
||||
#cmakedefine WITH_INTERNAL_MD5
|
||||
|
@ -22,17 +22,18 @@ set (CRT_FILES alignment.c
|
||||
unicode.c
|
||||
string.c)
|
||||
|
||||
if (NOT WITH_ICU)
|
||||
set (CRT_FILES ${CRT_FILES}
|
||||
utf.c
|
||||
utf.h)
|
||||
endif(NOT WITH_ICU)
|
||||
|
||||
if (WITH_ICU)
|
||||
IF (ANDROID)
|
||||
list(APPEND CRT_FILES unicode_android.c)
|
||||
elseif (NOT APPLE AND NOT WIN32)
|
||||
find_package(ICU REQUIRED i18n uc io)
|
||||
list(APPEND CRT_FILES unicode_icu.c)
|
||||
winpr_include_directory_add(${ICU_INCLUDE_DIRS})
|
||||
winpr_library_add_private(${ICU_LIBRARIES})
|
||||
endif (WITH_ICU)
|
||||
elseif(APPLE)
|
||||
list(APPEND CRT_FILES unicode_apple.m)
|
||||
find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
|
||||
winpr_library_add_private(${FOUNDATION_FRAMEWORK})
|
||||
endif()
|
||||
|
||||
winpr_module_add(${CRT_FILES})
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
* Unicode Conversion (CRT)
|
||||
*
|
||||
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
||||
* Copyright 2022 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2022 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -18,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include <winpr/config.h>
|
||||
#include <winpr/assert.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <wctype.h>
|
||||
@ -26,14 +29,13 @@
|
||||
#include <winpr/error.h>
|
||||
#include <winpr/print.h>
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#if defined(WITH_ICU)
|
||||
#include <unicode/ucnv.h>
|
||||
#include <unicode/ustring.h>
|
||||
#else
|
||||
#include "utf.h"
|
||||
#endif
|
||||
#include "unicode.h"
|
||||
|
||||
#include "../log.h"
|
||||
#define TAG WINPR_TAG("unicode")
|
||||
@ -152,87 +154,8 @@
|
||||
int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
|
||||
LPWSTR lpWideCharStr, int cchWideChar)
|
||||
{
|
||||
LPWSTR targetStart;
|
||||
#if !defined(WITH_ICU)
|
||||
const BYTE* sourceStart;
|
||||
int length;
|
||||
ConversionResult result;
|
||||
#endif
|
||||
|
||||
/* If cbMultiByte is 0, the function fails */
|
||||
|
||||
if ((cbMultiByte == 0) || (cbMultiByte < -1))
|
||||
return 0;
|
||||
|
||||
/* If cbMultiByte is -1, the string is null-terminated */
|
||||
|
||||
if (cbMultiByte == -1)
|
||||
{
|
||||
size_t len = strnlen((const char*)lpMultiByteStr, INT32_MAX);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cbMultiByte = (int)len + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* if cchWideChar is 0, the function returns the required buffer size
|
||||
* in characters for lpWideCharStr and makes no use of the output parameter itself.
|
||||
*/
|
||||
#if defined(WITH_ICU)
|
||||
{
|
||||
UErrorCode error;
|
||||
int32_t targetLength;
|
||||
int32_t targetCapacity;
|
||||
|
||||
switch (CodePage)
|
||||
{
|
||||
case CP_ACP:
|
||||
case CP_UTF8:
|
||||
break;
|
||||
|
||||
default:
|
||||
WLog_ERR(TAG, "Unsupported encoding %u", CodePage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
targetStart = lpWideCharStr;
|
||||
targetCapacity = cchWideChar;
|
||||
error = U_ZERO_ERROR;
|
||||
|
||||
if (cchWideChar == 0)
|
||||
{
|
||||
u_strFromUTF8(NULL, 0, &targetLength, lpMultiByteStr, cbMultiByte, &error);
|
||||
cchWideChar = targetLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
u_strFromUTF8(targetStart, targetCapacity, &targetLength, lpMultiByteStr, cbMultiByte,
|
||||
&error);
|
||||
cchWideChar = U_SUCCESS(error) ? targetLength : 0;
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
if (cchWideChar == 0)
|
||||
{
|
||||
sourceStart = (const BYTE*)lpMultiByteStr;
|
||||
targetStart = (WCHAR*)NULL;
|
||||
result = ConvertUTF8toUTF16(&sourceStart, &sourceStart[cbMultiByte], &targetStart, NULL,
|
||||
strictConversion);
|
||||
length = targetStart - ((WCHAR*)NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceStart = (const BYTE*)lpMultiByteStr;
|
||||
targetStart = lpWideCharStr;
|
||||
result = ConvertUTF8toUTF16(&sourceStart, &sourceStart[cbMultiByte], &targetStart,
|
||||
&targetStart[cchWideChar], strictConversion);
|
||||
length = targetStart - ((WCHAR*)lpWideCharStr);
|
||||
}
|
||||
|
||||
cchWideChar = (result == conversionOK) ? length : 0;
|
||||
#endif
|
||||
return cchWideChar;
|
||||
return int_MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr,
|
||||
cchWideChar);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -276,89 +199,8 @@ int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int
|
||||
LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
|
||||
LPBOOL lpUsedDefaultChar)
|
||||
{
|
||||
#if !defined(WITH_ICU)
|
||||
int length;
|
||||
const WCHAR* sourceStart;
|
||||
ConversionResult result;
|
||||
BYTE* targetStart;
|
||||
#else
|
||||
char* targetStart;
|
||||
#endif
|
||||
|
||||
/* If cchWideChar is 0, the function fails */
|
||||
|
||||
if ((cchWideChar == 0) || (cchWideChar < -1))
|
||||
return 0;
|
||||
|
||||
/* If cchWideChar is -1, the string is null-terminated */
|
||||
|
||||
if (cchWideChar == -1)
|
||||
{
|
||||
size_t len = _wcslen(lpWideCharStr);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cchWideChar = (int)len + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* if cbMultiByte is 0, the function returns the required buffer size
|
||||
* in bytes for lpMultiByteStr and makes no use of the output parameter itself.
|
||||
*/
|
||||
#if defined(WITH_ICU)
|
||||
{
|
||||
UErrorCode error;
|
||||
int32_t targetLength;
|
||||
int32_t targetCapacity;
|
||||
|
||||
switch (CodePage)
|
||||
{
|
||||
case CP_ACP:
|
||||
case CP_UTF8:
|
||||
break;
|
||||
|
||||
default:
|
||||
WLog_ERR(TAG, "Unsupported encoding %u", CodePage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
targetStart = lpMultiByteStr;
|
||||
targetCapacity = cbMultiByte;
|
||||
error = U_ZERO_ERROR;
|
||||
|
||||
if (cbMultiByte == 0)
|
||||
{
|
||||
u_strToUTF8(NULL, 0, &targetLength, lpWideCharStr, cchWideChar, &error);
|
||||
cbMultiByte = targetLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
u_strToUTF8(targetStart, targetCapacity, &targetLength, lpWideCharStr, cchWideChar,
|
||||
&error);
|
||||
cbMultiByte = U_SUCCESS(error) ? targetLength : 0;
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
if (cbMultiByte == 0)
|
||||
{
|
||||
sourceStart = (const WCHAR*)lpWideCharStr;
|
||||
targetStart = (BYTE*)NULL;
|
||||
result = ConvertUTF16toUTF8(&sourceStart, &sourceStart[cchWideChar], &targetStart, NULL,
|
||||
strictConversion);
|
||||
length = targetStart - ((BYTE*)NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceStart = (const WCHAR*)lpWideCharStr;
|
||||
targetStart = (BYTE*)lpMultiByteStr;
|
||||
result = ConvertUTF16toUTF8(&sourceStart, &sourceStart[cchWideChar], &targetStart,
|
||||
&targetStart[cbMultiByte], strictConversion);
|
||||
length = targetStart - ((BYTE*)lpMultiByteStr);
|
||||
}
|
||||
|
||||
cbMultiByte = (result == conversionOK) ? length : 0;
|
||||
#endif
|
||||
return cbMultiByte;
|
||||
return int_WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr,
|
||||
cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
32
winpr/libwinpr/crt/unicode.h
Normal file
32
winpr/libwinpr/crt/unicode.h
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* WinPR: Windows Portable Runtime
|
||||
* Unicode Conversion (CRT)
|
||||
*
|
||||
* Copyright 2022 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2022 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef WINPR_CRT_UNICODE_INTERNAL
|
||||
#define WINPR_CRT_UNICODE_INTERNAL
|
||||
|
||||
#include <winpr/wtypes.h>
|
||||
|
||||
int int_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
|
||||
LPWSTR lpWideCharStr, int cchWideChar);
|
||||
|
||||
int int_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
|
||||
LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
|
||||
LPBOOL lpUsedDefaultChar);
|
||||
#endif
|
188
winpr/libwinpr/crt/unicode_android.c
Normal file
188
winpr/libwinpr/crt/unicode_android.c
Normal file
@ -0,0 +1,188 @@
|
||||
/**
|
||||
* WinPR: Windows Portable Runtime
|
||||
* Unicode Conversion (CRT)
|
||||
*
|
||||
* Copyright 2022 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2022 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <winpr/config.h>
|
||||
#include <winpr/assert.h>
|
||||
#include <winpr/string.h>
|
||||
|
||||
#include "../utils/android.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#include "../log.h"
|
||||
#define TAG WINPR_TAG("unicode")
|
||||
|
||||
static int convert_int(JNIEnv* env, const void* data, size_t size, void* buffer, size_t buffersize,
|
||||
BOOL toUTF16)
|
||||
{
|
||||
WINPR_ASSERT(env);
|
||||
WINPR_ASSERT(data || (size == 0));
|
||||
WINPR_ASSERT(buffer || (buffersize == 0));
|
||||
|
||||
jstring utf8 = (*env)->NewStringUTF(env, "UTF-8");
|
||||
jstring utf16 = (*env)->NewStringUTF(env, "UTF-16LE");
|
||||
jclass stringClass = (*env)->FindClass(env, "java/lang/String");
|
||||
|
||||
if (!utf8 || !utf16 || !stringClass)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] utf8-%p, utf16=%p, stringClass=%p", __func__, utf8, utf16, stringClass);
|
||||
return -1;
|
||||
}
|
||||
|
||||
jmethodID constructorID =
|
||||
(*env)->GetMethodID(env, stringClass, "<init>", "([BLjava/lang/String;)V");
|
||||
jmethodID getBytesID =
|
||||
(*env)->GetMethodID(env, stringClass, "getBytes", "(Ljava/lang/String;)[B");
|
||||
if (!constructorID || !getBytesID)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] constructorID=%p, getBytesID=%p", __func__, constructorID, getBytesID);
|
||||
return -2;
|
||||
}
|
||||
|
||||
jbyteArray ret = (*env)->NewByteArray(env, size);
|
||||
if (!ret)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] NewByteArray(%" PRIuz ") failed", __func__, size);
|
||||
return -3;
|
||||
}
|
||||
|
||||
(*env)->SetByteArrayRegion(env, ret, 0, size, data);
|
||||
|
||||
jobject obj = (*env)->NewObject(env, stringClass, constructorID, ret, toUTF16 ? utf8 : utf16);
|
||||
if (!obj)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] NewObject(String, byteArray, UTF-%d) failed", __func__,
|
||||
toUTF16 ? 16 : 8);
|
||||
return -4;
|
||||
}
|
||||
|
||||
jbyteArray res = (*env)->CallObjectMethod(env, obj, getBytesID, toUTF16 ? utf16 : utf8);
|
||||
if (!res)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] CallObjectMethod(String, getBytes, UTF-%d) failed", __func__,
|
||||
toUTF16 ? 16 : 8);
|
||||
return -4;
|
||||
}
|
||||
|
||||
jsize rlen = (*env)->GetArrayLength(env, res);
|
||||
if (buffersize > 0)
|
||||
{
|
||||
rlen = MIN(rlen, buffersize);
|
||||
(*env)->GetByteArrayRegion(env, res, 0, rlen, buffer);
|
||||
}
|
||||
|
||||
if (toUTF16)
|
||||
rlen /= sizeof(WCHAR);
|
||||
|
||||
return rlen;
|
||||
}
|
||||
|
||||
static int convert(const void* data, size_t size, void* buffer, size_t buffersize, BOOL toUTF16)
|
||||
{
|
||||
int rc;
|
||||
JNIEnv* env = NULL;
|
||||
jboolean attached = winpr_jni_attach_thread(&env);
|
||||
rc = convert_int(env, data, size, buffer, buffersize, toUTF16);
|
||||
if (attached)
|
||||
winpr_jni_detach_thread();
|
||||
return rc;
|
||||
}
|
||||
|
||||
int int_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
|
||||
LPWSTR lpWideCharStr, int cchWideChar)
|
||||
{
|
||||
size_t cbCharLen = (size_t)cbMultiByte;
|
||||
|
||||
WINPR_UNUSED(dwFlags);
|
||||
|
||||
/* If cbMultiByte is 0, the function fails */
|
||||
if ((cbMultiByte == 0) || (cbMultiByte < -1))
|
||||
return 0;
|
||||
|
||||
if (cchWideChar < 0)
|
||||
return -1;
|
||||
|
||||
if (cbMultiByte < 0)
|
||||
{
|
||||
const size_t len = strlen(lpMultiByteStr);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cbCharLen = (int)len + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
cbCharLen = strnlen(lpMultiByteStr, cbMultiByte);
|
||||
if (cbCharLen < cbMultiByte)
|
||||
cbCharLen++;
|
||||
}
|
||||
|
||||
WINPR_ASSERT(lpMultiByteStr);
|
||||
switch (CodePage)
|
||||
{
|
||||
case CP_ACP:
|
||||
case CP_UTF8:
|
||||
break;
|
||||
|
||||
default:
|
||||
WLog_ERR(TAG, "Unsupported encoding %u", CodePage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return convert(lpMultiByteStr, cbCharLen, lpWideCharStr, cchWideChar * sizeof(WCHAR), TRUE);
|
||||
}
|
||||
|
||||
int int_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
|
||||
LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
|
||||
LPBOOL lpUsedDefaultChar)
|
||||
{
|
||||
size_t cbCharLen = (size_t)cchWideChar;
|
||||
|
||||
WINPR_UNUSED(dwFlags);
|
||||
/* If cchWideChar is 0, the function fails */
|
||||
if ((cchWideChar == 0) || (cchWideChar < -1))
|
||||
return 0;
|
||||
|
||||
if (cbMultiByte < 0)
|
||||
return -1;
|
||||
|
||||
WINPR_ASSERT(lpWideCharStr);
|
||||
/* If cchWideChar is -1, the string is null-terminated */
|
||||
if (cchWideChar == -1)
|
||||
{
|
||||
const size_t len = _wcslen(lpWideCharStr);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cbCharLen = (int)len + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
cbCharLen = (int)_wcsnlen(lpWideCharStr, cchWideChar);
|
||||
if (cbCharLen < cchWideChar)
|
||||
cbCharLen++;
|
||||
}
|
||||
|
||||
/*
|
||||
* if cbMultiByte is 0, the function returns the required buffer size
|
||||
* in bytes for lpMultiByteStr and makes no use of the output parameter itself.
|
||||
*/
|
||||
return convert(lpWideCharStr, cbCharLen * sizeof(WCHAR), lpMultiByteStr, cbMultiByte, FALSE);
|
||||
}
|
169
winpr/libwinpr/crt/unicode_apple.m
Normal file
169
winpr/libwinpr/crt/unicode_apple.m
Normal file
@ -0,0 +1,169 @@
|
||||
/**
|
||||
* WinPR: Windows Portable Runtime
|
||||
* Unicode Conversion (CRT)
|
||||
*
|
||||
* Copyright 2022 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2022 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include <winpr/config.h>
|
||||
#include <winpr/assert.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <wctype.h>
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/error.h>
|
||||
#include <winpr/print.h>
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#include "../log.h"
|
||||
#define TAG WINPR_TAG("unicode")
|
||||
|
||||
int int_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
|
||||
LPWSTR lpWideCharStr, int cchWideChar)
|
||||
{
|
||||
BOOL addNullTerminator = FALSE;
|
||||
|
||||
/* If cbMultiByte is 0, the function fails */
|
||||
if ((cbMultiByte == 0) || (cbMultiByte < -1))
|
||||
return 0;
|
||||
|
||||
/* If cbMultiByte is -1, the string is null-terminated */
|
||||
if (cbMultiByte == -1)
|
||||
{
|
||||
size_t len = strnlen((const char *)lpMultiByteStr, INT32_MAX);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cbMultiByte = (int)len;
|
||||
addNullTerminator = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
const size_t len = strnlen(lpMultiByteStr, (size_t)cbMultiByte);
|
||||
addNullTerminator = len < (size_t)cbMultiByte; /* If len == cbMultiByte no '\0' was found */
|
||||
cbMultiByte = (int)len;
|
||||
if (addNullTerminator)
|
||||
cbMultiByte++;
|
||||
}
|
||||
|
||||
NSString *utf = [[NSString alloc] initWithBytes:lpMultiByteStr
|
||||
length:cbMultiByte
|
||||
encoding:NSUTF8StringEncoding];
|
||||
if (!utf)
|
||||
{
|
||||
WLog_WARN(TAG, "[NSString alloc] NSUTF8StringEncoding failed [%d] '%s'", cbMultiByte,
|
||||
lpMultiByteStr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const WCHAR *utf16 =
|
||||
(const WCHAR *)[utf cStringUsingEncoding:NSUTF16LittleEndianStringEncoding];
|
||||
const size_t utf16ByteLen = [utf lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
|
||||
const size_t utf16CharLen = utf16ByteLen / sizeof(WCHAR);
|
||||
if (!utf16)
|
||||
{
|
||||
WLog_WARN(TAG, "[utf cStringUsingEncoding:NSUTF16LittleEndianStringEncoding] failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cchWideChar == 0)
|
||||
{
|
||||
cchWideChar = _wcsnlen(utf16, utf16CharLen);
|
||||
if (addNullTerminator)
|
||||
cchWideChar++;
|
||||
}
|
||||
else
|
||||
{
|
||||
const size_t len = _wcsnlen(utf16, MIN(utf16CharLen, (size_t)cchWideChar));
|
||||
memcpy(lpWideCharStr, utf16, len * sizeof(WCHAR));
|
||||
if ((len < (size_t)cchWideChar) && (len > 0) && (lpWideCharStr[len - 1] != '\0'))
|
||||
lpWideCharStr[len] = '\0';
|
||||
if (addNullTerminator && (len < cchWideChar))
|
||||
cchWideChar = (int)len + 1;
|
||||
else
|
||||
cchWideChar = (int)len;
|
||||
}
|
||||
return cchWideChar;
|
||||
}
|
||||
|
||||
int int_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
|
||||
LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
|
||||
LPBOOL lpUsedDefaultChar)
|
||||
{
|
||||
BOOL addNullTerminator = FALSE;
|
||||
|
||||
/* If cchWideChar is 0, the function fails */
|
||||
if ((cchWideChar == 0) || (cchWideChar < -1))
|
||||
return 0;
|
||||
|
||||
/* If cchWideChar is -1, the string is null-terminated */
|
||||
if (cchWideChar == -1)
|
||||
{
|
||||
size_t len = _wcslen(lpWideCharStr);
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cchWideChar = (int)len;
|
||||
addNullTerminator = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
const size_t len = _wcsnlen(lpWideCharStr, (size_t)cchWideChar);
|
||||
addNullTerminator = len < (size_t)cchWideChar; /* If len == cchWideChar no '\0' was found */
|
||||
cchWideChar = (int)len;
|
||||
if (addNullTerminator)
|
||||
cchWideChar++;
|
||||
}
|
||||
|
||||
NSString *utf = [[NSString alloc] initWithCharacters:lpWideCharStr length:cchWideChar];
|
||||
if (!utf)
|
||||
{
|
||||
WLog_WARN(TAG, "[NSString alloc] initWithCharacters failed [%d] 'XXX'", cchWideChar);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *utf8 = [utf cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
const size_t utf8Len = [utf lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
||||
if (!utf8)
|
||||
{
|
||||
WLog_WARN(TAG, "[utf cStringUsingEncoding:NSUTF8StringEncoding] failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cbMultiByte == 0)
|
||||
{
|
||||
cbMultiByte = strnlen(utf8, utf8Len);
|
||||
if (addNullTerminator)
|
||||
cbMultiByte++;
|
||||
}
|
||||
else
|
||||
{
|
||||
const size_t len = strnlen(utf8, MIN((size_t)cbMultiByte, utf8Len));
|
||||
memcpy(lpMultiByteStr, utf8, len * sizeof(char));
|
||||
if ((len < (size_t)cbMultiByte) && (len > 0) && (lpMultiByteStr[len - 1] != '\0'))
|
||||
lpMultiByteStr[len] = '\0';
|
||||
if (addNullTerminator && (len < cbMultiByte))
|
||||
cbMultiByte = (int)len + 1;
|
||||
else
|
||||
cbMultiByte = (int)len;
|
||||
}
|
||||
|
||||
return cbMultiByte;
|
||||
}
|
224
winpr/libwinpr/crt/unicode_icu.c
Normal file
224
winpr/libwinpr/crt/unicode_icu.c
Normal file
@ -0,0 +1,224 @@
|
||||
/**
|
||||
* WinPR: Windows Portable Runtime
|
||||
* Unicode Conversion (CRT)
|
||||
*
|
||||
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
||||
* Copyright 2022 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2022 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <winpr/config.h>
|
||||
#include <winpr/assert.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <wctype.h>
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/error.h>
|
||||
#include <winpr/print.h>
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#include <unicode/ucnv.h>
|
||||
#include <unicode/ustring.h>
|
||||
|
||||
#include "unicode.h"
|
||||
|
||||
#include "../log.h"
|
||||
#define TAG WINPR_TAG("unicode")
|
||||
|
||||
int int_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
|
||||
LPWSTR lpWideCharStr, int cchWideChar)
|
||||
{
|
||||
BOOL isNullTerminated = TRUE;
|
||||
LPWSTR targetStart;
|
||||
|
||||
WINPR_UNUSED(dwFlags);
|
||||
|
||||
/* If cbMultiByte is 0, the function fails */
|
||||
|
||||
if ((cbMultiByte == 0) || (cbMultiByte < -1))
|
||||
return 0;
|
||||
|
||||
size_t len;
|
||||
if (cbMultiByte == -1)
|
||||
len = strlen(lpMultiByteStr) + 1;
|
||||
else
|
||||
{
|
||||
len = strnlen(lpMultiByteStr, cbMultiByte);
|
||||
isNullTerminated = (len < cbMultiByte);
|
||||
if (isNullTerminated)
|
||||
len++;
|
||||
}
|
||||
if (len >= INT_MAX)
|
||||
return -1;
|
||||
cbMultiByte = (int)len;
|
||||
/*
|
||||
* if cchWideChar is 0, the function returns the required buffer size
|
||||
* in characters for lpWideCharStr and makes no use of the output parameter itself.
|
||||
*/
|
||||
{
|
||||
UErrorCode error;
|
||||
int32_t targetLength;
|
||||
int32_t targetCapacity;
|
||||
|
||||
switch (CodePage)
|
||||
{
|
||||
case CP_ACP:
|
||||
case CP_UTF8:
|
||||
break;
|
||||
|
||||
default:
|
||||
WLog_ERR(TAG, "Unsupported encoding %u", CodePage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
targetStart = lpWideCharStr;
|
||||
targetCapacity = cchWideChar;
|
||||
error = U_ZERO_ERROR;
|
||||
|
||||
u_strFromUTF8(targetStart, targetCapacity, &targetLength, lpMultiByteStr, cbMultiByte,
|
||||
&error);
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case U_BUFFER_OVERFLOW_ERROR:
|
||||
if (targetCapacity > 0)
|
||||
{
|
||||
cchWideChar = 0;
|
||||
WLog_ERR(TAG, "[%s] insufficient buffer supplied, got %d, required %d",
|
||||
__FUNCTION__, targetCapacity, targetLength);
|
||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||
}
|
||||
else
|
||||
cchWideChar = targetLength;
|
||||
break;
|
||||
case U_STRING_NOT_TERMINATED_WARNING:
|
||||
cchWideChar = targetLength;
|
||||
break;
|
||||
case U_ZERO_ERROR:
|
||||
cchWideChar = targetLength;
|
||||
break;
|
||||
default:
|
||||
WLog_WARN(TAG, "[%s] unexpected ICU error code %s [0x%08" PRIx32 "]", __func__,
|
||||
u_errorName(error), error);
|
||||
if (U_FAILURE(error))
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] unexpected ICU error code %s [0x%08" PRIx32 "] is fatal",
|
||||
__func__, u_errorName(error), error);
|
||||
cchWideChar = 0;
|
||||
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
|
||||
}
|
||||
else
|
||||
cchWideChar = targetLength;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cchWideChar;
|
||||
}
|
||||
|
||||
int int_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
|
||||
LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
|
||||
LPBOOL lpUsedDefaultChar)
|
||||
{
|
||||
BOOL isNullTerminated = TRUE;
|
||||
char* targetStart;
|
||||
|
||||
/* If cchWideChar is 0, the function fails */
|
||||
|
||||
if ((cchWideChar == 0) || (cchWideChar < -1))
|
||||
return 0;
|
||||
|
||||
/* If cchWideChar is -1, the string is null-terminated */
|
||||
|
||||
size_t len;
|
||||
if (cchWideChar == -1)
|
||||
len = _wcslen(lpWideCharStr) + 1;
|
||||
else
|
||||
{
|
||||
len = _wcsnlen(lpWideCharStr, cchWideChar);
|
||||
isNullTerminated = len < cchWideChar;
|
||||
if (isNullTerminated)
|
||||
len++;
|
||||
}
|
||||
|
||||
if (len >= INT32_MAX)
|
||||
return 0;
|
||||
cchWideChar = (int)len;
|
||||
|
||||
/*
|
||||
* if cbMultiByte is 0, the function returns the required buffer size
|
||||
* in bytes for lpMultiByteStr and makes no use of the output parameter itself.
|
||||
*/
|
||||
{
|
||||
UErrorCode error;
|
||||
int32_t targetLength;
|
||||
int32_t targetCapacity;
|
||||
|
||||
switch (CodePage)
|
||||
{
|
||||
case CP_ACP:
|
||||
case CP_UTF8:
|
||||
break;
|
||||
|
||||
default:
|
||||
WLog_ERR(TAG, "Unsupported encoding %u", CodePage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
targetStart = lpMultiByteStr;
|
||||
targetCapacity = cbMultiByte;
|
||||
error = U_ZERO_ERROR;
|
||||
|
||||
u_strToUTF8(targetStart, targetCapacity, &targetLength, lpWideCharStr, cchWideChar, &error);
|
||||
switch (error)
|
||||
{
|
||||
case U_BUFFER_OVERFLOW_ERROR:
|
||||
if (targetCapacity > 0)
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] insufficient buffer supplied, got %d, required %d",
|
||||
__FUNCTION__, targetCapacity, targetLength);
|
||||
cbMultiByte = 0;
|
||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||
}
|
||||
else
|
||||
cbMultiByte = targetLength;
|
||||
break;
|
||||
case U_STRING_NOT_TERMINATED_WARNING:
|
||||
cbMultiByte = targetLength;
|
||||
break;
|
||||
case U_ZERO_ERROR:
|
||||
cbMultiByte = targetLength;
|
||||
break;
|
||||
default:
|
||||
WLog_WARN(TAG, "[%s] unexpected ICU error code %s [0x%08" PRIx32 "]", __func__,
|
||||
u_errorName(error), error);
|
||||
if (U_FAILURE(error))
|
||||
{
|
||||
WLog_ERR(TAG, "[%s] unexpected ICU error code %s [0x%08" PRIx32 "] is fatal",
|
||||
__func__, u_errorName(error), error);
|
||||
cbMultiByte = 0;
|
||||
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
|
||||
}
|
||||
else
|
||||
cbMultiByte = targetLength;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cbMultiByte;
|
||||
}
|
@ -1,883 +0,0 @@
|
||||
/*
|
||||
* Copyright 2001-2004 Unicode, Inc.
|
||||
*
|
||||
* Disclaimer
|
||||
*
|
||||
* This source code is provided as is by Unicode, Inc. No claims are
|
||||
* made as to fitness for any particular purpose. No warranties of any
|
||||
* kind are expressed or implied. The recipient agrees to determine
|
||||
* applicability of information provided. If this file has been
|
||||
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||
* sole remedy for any claim will be exchange of defective media
|
||||
* within 90 days of receipt.
|
||||
*
|
||||
* Limitations on Rights to Redistribute This Code
|
||||
*
|
||||
* Unicode, Inc. hereby grants the right to freely use the information
|
||||
* supplied in this file in the creation of products supporting the
|
||||
* Unicode Standard, and to make copies of this file in any form
|
||||
* for internal or external distribution as long as this notice
|
||||
* remains attached.
|
||||
*/
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
|
||||
Conversions between UTF32, UTF-16, and UTF-8. Source code file.
|
||||
Author: Mark E. Davis, 1994.
|
||||
Rev History: Rick McGowan, fixes & updates May 2001.
|
||||
Sept 2001: fixed const & error conditions per
|
||||
mods suggested by S. Parent & A. Lillich.
|
||||
June 2002: Tim Dodd added detection and handling of incomplete
|
||||
source sequences, enhanced error detection, added casts
|
||||
to eliminate compiler warnings.
|
||||
July 2003: slight mods to back out aggressive FFFE detection.
|
||||
Jan 2004: updated switches in from-UTF8 conversions.
|
||||
Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
|
||||
|
||||
See the header file "utf.h" for complete documentation.
|
||||
|
||||
------------------------------------------------------------------------ */
|
||||
|
||||
#include "utf.h"
|
||||
#include <winpr/endian.h>
|
||||
|
||||
#if __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
|
||||
#endif
|
||||
static const int halfShift = 10; /* used for shifting by 10 bits */
|
||||
|
||||
static const DWORD halfBase = 0x0010000UL;
|
||||
static const DWORD halfMask = 0x3FFUL;
|
||||
|
||||
#define UNI_SUR_HIGH_START (DWORD)0xD800
|
||||
#define UNI_SUR_HIGH_END (DWORD)0xDBFF
|
||||
#define UNI_SUR_LOW_START (DWORD)0xDC00
|
||||
#define UNI_SUR_LOW_END (DWORD)0xDFFF
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF32toUTF16(const DWORD** sourceStart, const DWORD* sourceEnd,
|
||||
WCHAR** targetStart, WCHAR* targetEnd, ConversionFlags flags)
|
||||
{
|
||||
ConversionResult result = conversionOK;
|
||||
const DWORD* source = *sourceStart;
|
||||
WCHAR* target = *targetStart;
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
DWORD ch;
|
||||
|
||||
if (target >= targetEnd)
|
||||
{
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
ch = *source++;
|
||||
|
||||
if (ch <= UNI_MAX_BMP) /* Target is a character <= 0xFFFF */
|
||||
{
|
||||
/* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved
|
||||
* values */
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
*target++ = UNI_REPLACEMENT_CHAR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*target++ = (WCHAR)ch; /* normal case */
|
||||
}
|
||||
}
|
||||
else if (ch > UNI_MAX_LEGAL_UTF32)
|
||||
{
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
result = sourceIllegal;
|
||||
}
|
||||
else
|
||||
{
|
||||
*target++ = UNI_REPLACEMENT_CHAR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* target is a character in range 0xFFFF - 0x10FFFF. */
|
||||
if (target + 1 >= targetEnd)
|
||||
{
|
||||
--source; /* Back up source pointer! */
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
ch -= halfBase;
|
||||
*target++ = (WCHAR)((ch >> halfShift) + UNI_SUR_HIGH_START);
|
||||
*target++ = (WCHAR)((ch & halfMask) + UNI_SUR_LOW_START);
|
||||
}
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
*targetStart = target;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF16toUTF32(const WCHAR** sourceStart, const WCHAR* sourceEnd,
|
||||
DWORD** targetStart, DWORD* targetEnd, ConversionFlags flags)
|
||||
{
|
||||
ConversionResult result = conversionOK;
|
||||
const WCHAR* source = *sourceStart;
|
||||
DWORD* target = *targetStart;
|
||||
DWORD ch, ch2;
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
const WCHAR* oldSource =
|
||||
source; /* In case we have to back up because of target overflow. */
|
||||
ch = *source++;
|
||||
|
||||
/* If we have a surrogate pair, convert to UTF32 first. */
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END)
|
||||
{
|
||||
/* If the 16 bits following the high surrogate are in the source buffer... */
|
||||
if (source < sourceEnd)
|
||||
{
|
||||
ch2 = *source;
|
||||
|
||||
/* If it's a low surrogate, convert to UTF32. */
|
||||
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END)
|
||||
{
|
||||
ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + (ch2 - UNI_SUR_LOW_START) +
|
||||
halfBase;
|
||||
++source;
|
||||
}
|
||||
else if (flags == strictConversion) /* it's an unpaired high surrogate */
|
||||
{
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else /* We don't have the 16 bits following the high surrogate. */
|
||||
{
|
||||
--source; /* return to the high surrogate */
|
||||
result = sourceExhausted;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (flags == strictConversion)
|
||||
{
|
||||
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (target >= targetEnd)
|
||||
{
|
||||
source = oldSource; /* Back up source pointer! */
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
*target++ = ch;
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
*targetStart = target;
|
||||
#ifdef CVTUTF_DEBUG
|
||||
|
||||
if (result == sourceIllegal)
|
||||
{
|
||||
WLOG_WARN(TAG, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x", ch, ch2);
|
||||
}
|
||||
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Index into the table below with the first byte of a UTF-8 sequence to
|
||||
* get the number of trailing bytes that are supposed to follow it.
|
||||
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
|
||||
* left as-is for anyone who may want to do such conversion, which was
|
||||
* allowed in earlier algorithms.
|
||||
*/
|
||||
static const char trailingBytesForUTF8[256] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
|
||||
};
|
||||
|
||||
/*
|
||||
* Magic values subtracted from a buffer value during UTF8 conversion.
|
||||
* This table contains as many values as there might be trailing bytes
|
||||
* in a UTF-8 sequence.
|
||||
*/
|
||||
static const DWORD offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
|
||||
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
|
||||
|
||||
/*
|
||||
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
|
||||
* into the first byte, depending on how many bytes follow. There are
|
||||
* as many entries in this table as there are UTF-8 sequence types.
|
||||
* (I.e., one byte sequence, two byte... etc.). Remember that sequencs
|
||||
* for *legal* UTF-8 will be 4 or fewer bytes total.
|
||||
*/
|
||||
static const BYTE firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
/* The interface converts a whole buffer to avoid function-call overhead.
|
||||
* Constants have been gathered. Loops & conditionals have been removed as
|
||||
* much as possible for efficiency, in favor of drop-through switches.
|
||||
* (See "Note A" at the bottom of the file for equivalent code.)
|
||||
* If your compiler supports it, the "isLegalUTF8" call can be turned
|
||||
* into an inline function.
|
||||
*/
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF16toUTF8(const WCHAR** sourceStart, const WCHAR* sourceEnd,
|
||||
BYTE** targetStart, BYTE* te, ConversionFlags flags)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t end = 0;
|
||||
const WCHAR* source;
|
||||
const BOOL computeLength = (!te) ? TRUE : FALSE;
|
||||
ConversionResult result = conversionOK;
|
||||
|
||||
if (targetStart && te)
|
||||
{
|
||||
const size_t s = (size_t)*targetStart;
|
||||
const size_t e = (size_t)te;
|
||||
if (s > e)
|
||||
return sourceIllegal;
|
||||
end = e - s;
|
||||
}
|
||||
|
||||
source = *sourceStart;
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
DWORD ch;
|
||||
unsigned short bytesToWrite = 0;
|
||||
const DWORD byteMask = 0xBF;
|
||||
const DWORD byteMark = 0x80;
|
||||
const WCHAR* oldSource =
|
||||
source; /* In case we have to back up because of target overflow. */
|
||||
Data_Read_UINT16(source, ch);
|
||||
source++;
|
||||
|
||||
/* If we have a surrogate pair, convert to UTF32 first. */
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END)
|
||||
{
|
||||
/* If the 16 bits following the high surrogate are in the source buffer... */
|
||||
if (source < sourceEnd)
|
||||
{
|
||||
DWORD ch2;
|
||||
Data_Read_UINT16(source, ch2);
|
||||
|
||||
/* If it's a low surrogate, convert to UTF32. */
|
||||
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END)
|
||||
{
|
||||
ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + (ch2 - UNI_SUR_LOW_START) +
|
||||
halfBase;
|
||||
++source;
|
||||
}
|
||||
else if (flags == strictConversion)
|
||||
{
|
||||
/* it's an unpaired high surrogate */
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* We don't have the 16 bits following the high surrogate. */
|
||||
--source; /* return to the high surrogate */
|
||||
result = sourceExhausted;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (flags == strictConversion)
|
||||
{
|
||||
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Figure out how many bytes the result will require */
|
||||
if (ch < (DWORD)0x80)
|
||||
{
|
||||
bytesToWrite = 1;
|
||||
}
|
||||
else if (ch < (DWORD)0x800)
|
||||
{
|
||||
bytesToWrite = 2;
|
||||
}
|
||||
else if (ch < (DWORD)0x10000)
|
||||
{
|
||||
bytesToWrite = 3;
|
||||
}
|
||||
else if (ch < (DWORD)0x110000)
|
||||
{
|
||||
bytesToWrite = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite = 3;
|
||||
ch = UNI_REPLACEMENT_CHAR;
|
||||
}
|
||||
|
||||
pos += bytesToWrite;
|
||||
|
||||
if ((pos > end) && (!computeLength))
|
||||
{
|
||||
source = oldSource; /* Back up source pointer! */
|
||||
pos -= bytesToWrite;
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!computeLength)
|
||||
{
|
||||
switch (bytesToWrite)
|
||||
{
|
||||
/* note: everything falls through. */
|
||||
case 4:
|
||||
(*targetStart)[--pos] = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 3:
|
||||
(*targetStart)[--pos] = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 2:
|
||||
(*targetStart)[--pos] = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 1:
|
||||
(*targetStart)[--pos] = (BYTE)(ch | firstByteMark[bytesToWrite]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (bytesToWrite)
|
||||
{
|
||||
/* note: everything falls through. */
|
||||
case 4:
|
||||
--pos;
|
||||
|
||||
case 3:
|
||||
--pos;
|
||||
|
||||
case 2:
|
||||
--pos;
|
||||
|
||||
case 1:
|
||||
--pos;
|
||||
}
|
||||
}
|
||||
|
||||
pos += bytesToWrite;
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
if (targetStart && *targetStart)
|
||||
*targetStart = &(*targetStart)[pos];
|
||||
else if (targetStart)
|
||||
*targetStart = (BYTE*)pos;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
|
||||
* This must be called with the length pre-determined by the first byte.
|
||||
* If not calling this from ConvertUTF8to*, then the length can be set by:
|
||||
* length = trailingBytesForUTF8[*source]+1;
|
||||
* and the sequence is illegal right away if there aren't that many bytes
|
||||
* available.
|
||||
* If presented with a length > 4, this returns FALSE. The Unicode
|
||||
* definition of UTF-8 goes up to 4-byte sequences.
|
||||
*/
|
||||
|
||||
static BOOL isLegalUTF8(const BYTE* source, int length)
|
||||
{
|
||||
BYTE a;
|
||||
const BYTE* srcptr = source + length;
|
||||
|
||||
switch (length)
|
||||
{
|
||||
default:
|
||||
return FALSE;
|
||||
|
||||
/* Everything else falls through when "TRUE"... */
|
||||
case 4:
|
||||
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
|
||||
return FALSE;
|
||||
|
||||
case 3:
|
||||
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
|
||||
return FALSE;
|
||||
|
||||
case 2:
|
||||
if ((a = (*--srcptr)) > 0xBF)
|
||||
return FALSE;
|
||||
|
||||
switch (*source)
|
||||
{
|
||||
/* no fall-through in this inner switch */
|
||||
case 0xE0:
|
||||
if (a < 0xA0)
|
||||
return FALSE;
|
||||
|
||||
break;
|
||||
|
||||
case 0xED:
|
||||
if (a > 0x9F)
|
||||
return FALSE;
|
||||
|
||||
break;
|
||||
|
||||
case 0xF0:
|
||||
if (a < 0x90)
|
||||
return FALSE;
|
||||
|
||||
break;
|
||||
|
||||
case 0xF4:
|
||||
if (a > 0x8F)
|
||||
return FALSE;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if (a < 0x80)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
case 1:
|
||||
if (*source >= 0x80 && *source < 0xC2)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (*source > 0xF4)
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Exported function to return whether a UTF-8 sequence is legal or not.
|
||||
* This is not used here; it's just exported.
|
||||
*/
|
||||
BOOL isLegalUTF8Sequence(const BYTE* source, const BYTE* sourceEnd)
|
||||
{
|
||||
int length = trailingBytesForUTF8[*source] + 1;
|
||||
|
||||
if (source + length > sourceEnd)
|
||||
return FALSE;
|
||||
|
||||
return isLegalUTF8(source, length);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF8toUTF16(const BYTE** sourceStart, const BYTE* sourceEnd,
|
||||
WCHAR** targetStart, WCHAR* targetEnd, ConversionFlags flags)
|
||||
{
|
||||
size_t target = 0;
|
||||
size_t end = 0;
|
||||
const BYTE* source;
|
||||
BOOL computeLength;
|
||||
ConversionResult result;
|
||||
computeLength = (!targetEnd) ? TRUE : FALSE;
|
||||
result = conversionOK;
|
||||
source = *sourceStart;
|
||||
|
||||
if (targetStart && targetEnd)
|
||||
{
|
||||
const size_t s = (size_t)*targetStart;
|
||||
const size_t e = (size_t)targetEnd;
|
||||
if (s > e)
|
||||
return sourceIllegal;
|
||||
|
||||
end = ((size_t)(targetEnd)) - ((size_t)(*targetStart));
|
||||
}
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
DWORD ch = 0;
|
||||
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
|
||||
|
||||
if ((source + extraBytesToRead) >= sourceEnd)
|
||||
{
|
||||
result = sourceExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Do this check whether lenient or strict */
|
||||
if (!isLegalUTF8(source, extraBytesToRead + 1))
|
||||
{
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* The cases all fall through. See "Note A" below.
|
||||
*/
|
||||
switch (extraBytesToRead)
|
||||
{
|
||||
case 5:
|
||||
ch += *source++;
|
||||
ch <<= 6; /* remember, illegal UTF-8 */
|
||||
|
||||
case 4:
|
||||
ch += *source++;
|
||||
ch <<= 6; /* remember, illegal UTF-8 */
|
||||
|
||||
case 3:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 2:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 1:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 0:
|
||||
ch += *source++;
|
||||
}
|
||||
|
||||
ch -= offsetsFromUTF8[extraBytesToRead];
|
||||
|
||||
if ((target * sizeof(WCHAR) >= end) && (!computeLength))
|
||||
{
|
||||
source -= (extraBytesToRead + 1); /* Back up source pointer! */
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch <= UNI_MAX_BMP)
|
||||
{
|
||||
/* Target is a character <= 0xFFFF */
|
||||
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
source -= (extraBytesToRead + 1); /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!computeLength)
|
||||
Data_Write_UINT16(&(*targetStart)[target], UNI_REPLACEMENT_CHAR);
|
||||
target++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!computeLength)
|
||||
Data_Write_UINT16(&(*targetStart)[target], ch); /* normal case */
|
||||
target++;
|
||||
}
|
||||
}
|
||||
else if (ch > UNI_MAX_UTF16)
|
||||
{
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
result = sourceIllegal;
|
||||
source -= (extraBytesToRead + 1); /* return to the start */
|
||||
break; /* Bail out; shouldn't continue */
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!computeLength)
|
||||
Data_Write_UINT16(&(*targetStart)[target], UNI_REPLACEMENT_CHAR);
|
||||
target++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* target is a character in range 0xFFFF - 0x10FFFF. */
|
||||
if (((target + 1) * sizeof(WCHAR) >= end) && (!computeLength))
|
||||
{
|
||||
source -= (extraBytesToRead + 1); /* Back up source pointer! */
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
ch -= halfBase;
|
||||
|
||||
if (!computeLength)
|
||||
{
|
||||
WCHAR wchar;
|
||||
wchar = (WCHAR)((ch >> halfShift) + UNI_SUR_HIGH_START);
|
||||
Data_Write_UINT16(&(*targetStart)[target++], wchar);
|
||||
wchar = (WCHAR)((ch & halfMask) + UNI_SUR_LOW_START);
|
||||
Data_Write_UINT16(&(*targetStart)[target++], wchar);
|
||||
}
|
||||
else
|
||||
{
|
||||
target++;
|
||||
target++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
if (targetStart && (*targetStart))
|
||||
*targetStart = &(*targetStart)[target];
|
||||
else if (targetStart)
|
||||
*targetStart = (WCHAR*)(target * sizeof(WCHAR));
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF32toUTF8(const DWORD** sourceStart, const DWORD* sourceEnd,
|
||||
BYTE** targetStart, BYTE* targetEnd, ConversionFlags flags)
|
||||
{
|
||||
ConversionResult result = conversionOK;
|
||||
const DWORD* source = *sourceStart;
|
||||
BYTE* target = *targetStart;
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
DWORD ch;
|
||||
unsigned short bytesToWrite = 0;
|
||||
const DWORD byteMask = 0xBF;
|
||||
const DWORD byteMark = 0x80;
|
||||
ch = *source++;
|
||||
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
--source; /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Figure out how many bytes the result will require. Turn any
|
||||
* illegally large UTF32 things (> Plane 17) into replacement chars.
|
||||
*/
|
||||
if (ch < (DWORD)0x80)
|
||||
{
|
||||
bytesToWrite = 1;
|
||||
}
|
||||
else if (ch < (DWORD)0x800)
|
||||
{
|
||||
bytesToWrite = 2;
|
||||
}
|
||||
else if (ch < (DWORD)0x10000)
|
||||
{
|
||||
bytesToWrite = 3;
|
||||
}
|
||||
else if (ch <= UNI_MAX_LEGAL_UTF32)
|
||||
{
|
||||
bytesToWrite = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite = 3;
|
||||
ch = UNI_REPLACEMENT_CHAR;
|
||||
result = sourceIllegal;
|
||||
}
|
||||
|
||||
target += bytesToWrite;
|
||||
|
||||
if (target > targetEnd)
|
||||
{
|
||||
--source; /* Back up source pointer! */
|
||||
target -= bytesToWrite;
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (bytesToWrite) /* note: everything falls through. */
|
||||
{
|
||||
case 4:
|
||||
*--target = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 3:
|
||||
*--target = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 2:
|
||||
*--target = (BYTE)((ch | byteMark) & byteMask);
|
||||
ch >>= 6;
|
||||
|
||||
case 1:
|
||||
*--target = (BYTE)(ch | firstByteMark[bytesToWrite]);
|
||||
}
|
||||
|
||||
target += bytesToWrite;
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
*targetStart = target;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF8toUTF32(const BYTE** sourceStart, const BYTE* sourceEnd,
|
||||
DWORD** targetStart, DWORD* targetEnd, ConversionFlags flags)
|
||||
{
|
||||
ConversionResult result = conversionOK;
|
||||
const BYTE* source = *sourceStart;
|
||||
DWORD* target = *targetStart;
|
||||
|
||||
while (source < sourceEnd)
|
||||
{
|
||||
DWORD ch = 0;
|
||||
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
|
||||
|
||||
if (source + extraBytesToRead >= sourceEnd)
|
||||
{
|
||||
result = sourceExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Do this check whether lenient or strict */
|
||||
if (!isLegalUTF8(source, extraBytesToRead + 1))
|
||||
{
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* The cases all fall through. See "Note A" below.
|
||||
*/
|
||||
switch (extraBytesToRead)
|
||||
{
|
||||
case 5:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 4:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 3:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 2:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 1:
|
||||
ch += *source++;
|
||||
ch <<= 6;
|
||||
|
||||
case 0:
|
||||
ch += *source++;
|
||||
}
|
||||
|
||||
ch -= offsetsFromUTF8[extraBytesToRead];
|
||||
|
||||
if (target >= targetEnd)
|
||||
{
|
||||
source -= (extraBytesToRead + 1); /* Back up the source pointer! */
|
||||
result = targetExhausted;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch <= UNI_MAX_LEGAL_UTF32)
|
||||
{
|
||||
/*
|
||||
* UTF-16 surrogate values are illegal in UTF-32, and anything
|
||||
* over Plane 17 (> 0x10FFFF) is illegal.
|
||||
*/
|
||||
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
|
||||
{
|
||||
if (flags == strictConversion)
|
||||
{
|
||||
source -= (extraBytesToRead + 1); /* return to the illegal value itself */
|
||||
result = sourceIllegal;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
*target++ = UNI_REPLACEMENT_CHAR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*target++ = ch;
|
||||
}
|
||||
}
|
||||
else /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
|
||||
{
|
||||
result = sourceIllegal;
|
||||
*target++ = UNI_REPLACEMENT_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
*sourceStart = source;
|
||||
*targetStart = target;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
|
||||
Note A.
|
||||
The fall-through switches in UTF-8 reading code save a
|
||||
temp variable, some decrements & conditionals. The switches
|
||||
are equivalent to the following loop:
|
||||
{
|
||||
int tmpBytesToRead = extraBytesToRead+1;
|
||||
do {
|
||||
ch += *source++;
|
||||
--tmpBytesToRead;
|
||||
if (tmpBytesToRead) ch <<= 6;
|
||||
} while (tmpBytesToRead > 0);
|
||||
}
|
||||
In UTF-8 writing code, the switches on "bytesToWrite" are
|
||||
similarly unrolled loops.
|
||||
|
||||
--------------------------------------------------------------------- */
|
||||
#if __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
@ -1,150 +0,0 @@
|
||||
/*
|
||||
* Copyright 2001-2004 Unicode, Inc.
|
||||
*
|
||||
* Disclaimer
|
||||
*
|
||||
* This source code is provided as is by Unicode, Inc. No claims are
|
||||
* made as to fitness for any particular purpose. No warranties of any
|
||||
* kind are expressed or implied. The recipient agrees to determine
|
||||
* applicability of information provided. If this file has been
|
||||
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||
* sole remedy for any claim will be exchange of defective media
|
||||
* within 90 days of receipt.
|
||||
*
|
||||
* Limitations on Rights to Redistribute This Code
|
||||
*
|
||||
* Unicode, Inc. hereby grants the right to freely use the information
|
||||
* supplied in this file in the creation of products supporting the
|
||||
* Unicode Standard, and to make copies of this file in any form
|
||||
* for internal or external distribution as long as this notice
|
||||
* remains attached.
|
||||
*/
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
|
||||
Conversions between UTF32, UTF-16, and UTF-8. Header file.
|
||||
|
||||
Several funtions are included here, forming a complete set of
|
||||
conversions between the three formats. UTF-7 is not included
|
||||
here, but is handled in a separate source file.
|
||||
|
||||
Each of these routines takes pointers to input buffers and output
|
||||
buffers. The input buffers are const.
|
||||
|
||||
Each routine converts the text between *sourceStart and sourceEnd,
|
||||
putting the result into the buffer between *targetStart and
|
||||
targetEnd. Note: the end pointers are *after* the last item: e.g.
|
||||
*(sourceEnd - 1) is the last item.
|
||||
|
||||
The return result indicates whether the conversion was successful,
|
||||
and if not, whether the problem was in the source or target buffers.
|
||||
(Only the first encountered problem is indicated.)
|
||||
|
||||
After the conversion, *sourceStart and *targetStart are both
|
||||
updated to point to the end of last text successfully converted in
|
||||
the respective buffers.
|
||||
|
||||
Input parameters:
|
||||
sourceStart - pointer to a pointer to the source buffer.
|
||||
The contents of this are modified on return so that
|
||||
it points at the next thing to be converted.
|
||||
targetStart - similarly, pointer to pointer to the target buffer.
|
||||
sourceEnd, targetEnd - respectively pointers to the ends of the
|
||||
two buffers, for overflow checking only.
|
||||
|
||||
These conversion functions take a ConversionFlags argument. When this
|
||||
flag is set to strict, both irregular sequences and isolated surrogates
|
||||
will cause an error. When the flag is set to lenient, both irregular
|
||||
sequences and isolated surrogates are converted.
|
||||
|
||||
Whether the flag is strict or lenient, all illegal sequences will cause
|
||||
an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
|
||||
or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
|
||||
must check for illegal sequences.
|
||||
|
||||
When the flag is set to lenient, characters over 0x10FFFF are converted
|
||||
to the replacement character; otherwise (when the flag is set to strict)
|
||||
they constitute an error.
|
||||
|
||||
Output parameters:
|
||||
The value "sourceIllegal" is returned from some routines if the input
|
||||
sequence is malformed. When "sourceIllegal" is returned, the source
|
||||
value will point to the illegal value that caused the problem. E.g.,
|
||||
in UTF-8 when a sequence is malformed, it points to the start of the
|
||||
malformed sequence.
|
||||
|
||||
Author: Mark E. Davis, 1994.
|
||||
Rev History: Rick McGowan, fixes & updates May 2001.
|
||||
Fixes & updates, Sept 2001.
|
||||
|
||||
------------------------------------------------------------------------ */
|
||||
|
||||
#ifndef WINPR_UNICODE_CONVERT_UTF_H
|
||||
#define WINPR_UNICODE_CONVERT_UTF_H
|
||||
|
||||
#include <winpr/wtypes.h>
|
||||
|
||||
/*
|
||||
* Character Types:
|
||||
*
|
||||
* UTF8: BYTE 8 bits
|
||||
* UTF16: WCHAR 16 bits
|
||||
* UTF32: DWORD 32 bits
|
||||
*/
|
||||
|
||||
/* Some fundamental constants */
|
||||
#define UNI_REPLACEMENT_CHAR (DWORD)0x0000FFFD
|
||||
#define UNI_MAX_BMP (DWORD)0x0000FFFF
|
||||
#define UNI_MAX_UTF16 (DWORD)0x0010FFFF
|
||||
#define UNI_MAX_UTF32 (DWORD)0x7FFFFFFF
|
||||
#define UNI_MAX_LEGAL_UTF32 (DWORD)0x0010FFFF
|
||||
|
||||
typedef enum
|
||||
{
|
||||
conversionOK, /* conversion successful */
|
||||
sourceExhausted, /* partial character in source, but hit end */
|
||||
targetExhausted, /* insuff. room in target for conversion */
|
||||
sourceIllegal /* source sequence is illegal/malformed */
|
||||
} ConversionResult;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
strictConversion = 0,
|
||||
lenientConversion
|
||||
} ConversionFlags;
|
||||
|
||||
/* This is for C++ and does no harm in C */
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
ConversionResult ConvertUTF8toUTF16(const BYTE** sourceStart, const BYTE* sourceEnd,
|
||||
WCHAR** targetStart, WCHAR* targetEnd,
|
||||
ConversionFlags flags);
|
||||
|
||||
ConversionResult ConvertUTF16toUTF8(const WCHAR** sourceStart, const WCHAR* sourceEnd,
|
||||
BYTE** targetStart, BYTE* targetEnd, ConversionFlags flags);
|
||||
|
||||
ConversionResult ConvertUTF8toUTF32(const BYTE** sourceStart, const BYTE* sourceEnd,
|
||||
DWORD** targetStart, DWORD* targetEnd,
|
||||
ConversionFlags flags);
|
||||
|
||||
ConversionResult ConvertUTF32toUTF8(const DWORD** sourceStart, const DWORD* sourceEnd,
|
||||
BYTE** targetStart, BYTE* targetEnd, ConversionFlags flags);
|
||||
|
||||
ConversionResult ConvertUTF16toUTF32(const WCHAR** sourceStart, const WCHAR* sourceEnd,
|
||||
DWORD** targetStart, DWORD* targetEnd,
|
||||
ConversionFlags flags);
|
||||
|
||||
ConversionResult ConvertUTF32toUTF16(const DWORD** sourceStart, const DWORD* sourceEnd,
|
||||
WCHAR** targetStart, WCHAR* targetEnd,
|
||||
ConversionFlags flags);
|
||||
|
||||
BOOL isLegalUTF8Sequence(const BYTE* source, const BYTE* sourceEnd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* WINPR_UNICODE_CONVERT_UTF_H */
|
Loading…
Reference in New Issue
Block a user