41#include <winpr/wtypes.h>
42#include <winpr/string.h>
43#include <winpr/assert.h>
44#include <winpr/cast.h>
49#define TAG WINPR_TAG("unicode")
60#define UNI_REPLACEMENT_CHAR (uint32_t)0x0000FFFD
61#define UNI_MAX_BMP (uint32_t)0x0000FFFF
62#define UNI_MAX_UTF16 (uint32_t)0x0010FFFF
63#define UNI_MAX_UTF32 (uint32_t)0x7FFFFFFF
64#define UNI_MAX_LEGAL_UTF32 (uint32_t)0x0010FFFF
80static const int halfShift = 10;
82static const uint32_t halfBase = 0x0010000UL;
83static const uint32_t halfMask = 0x3FFUL;
85#define UNI_SUR_HIGH_START (uint32_t)0xD800
86#define UNI_SUR_HIGH_END (uint32_t)0xDBFF
87#define UNI_SUR_LOW_START (uint32_t)0xDC00
88#define UNI_SUR_LOW_END (uint32_t)0xDFFF
99static const char trailingBytesForUTF8[256] = {
100 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,
101 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,
102 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,
103 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,
104 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,
105 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,
106 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,
107 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
115static const uint32_t offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
116 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
125static const uint8_t firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
128static WCHAR setWcharFrom(WCHAR w)
130#if defined(__BIG_ENDIAN__)
138 const char c = cnv.c[0];
159static ConversionResult winpr_ConvertUTF16toUTF8_Internal(
const uint16_t** sourceStart,
160 const uint16_t* sourceEnd,
161 uint8_t** targetStart,
162 const uint8_t* targetEnd,
163 ConversionFlags flags,
size_t* computed)
165 bool computeLength = (!targetEnd) ?
true : false;
167 const uint16_t* source = *sourceStart;
168 uint8_t* target = *targetStart;
169 ConversionResult result = conversionOK;
171 while (source < sourceEnd)
174 unsigned short bytesToWrite = 0;
175 const uint32_t byteMask = 0xBF;
176 const uint32_t byteMark = 0x80;
177 const uint16_t* oldSource =
180 ch = setWcharFrom(*source++);
183 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END)
186 if (source < sourceEnd)
188 uint32_t ch2 = setWcharFrom(*source);
191 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END)
193 ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + (ch2 - UNI_SUR_LOW_START) +
197 else if (flags == strictConversion)
201 result = sourceIllegal;
209 result = sourceExhausted;
213 else if (flags == strictConversion)
216 if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END)
219 result = sourceIllegal;
225 if (ch < (uint32_t)0x80)
229 else if (ch < (uint32_t)0x800)
233 else if (ch < (uint32_t)0x10000)
237 else if (ch < (uint32_t)0x110000)
244 ch = UNI_REPLACEMENT_CHAR;
249 count += bytesToWrite;
253 target += bytesToWrite;
255 if ((target > targetEnd) && (!computeLength))
258 target -= bytesToWrite;
259 result = targetExhausted;
263 switch (bytesToWrite)
267 *--target = (uint8_t)((ch | byteMark) & byteMask);
272 *--target = (uint8_t)((ch | byteMark) & byteMask);
278 *--target = (uint8_t)((ch | byteMark) & byteMask);
284 *--target = (uint8_t)(ch | firstByteMark[bytesToWrite]);
287 return sourceIllegal;
290 target += bytesToWrite;
293 *sourceStart = source;
294 *targetStart = target;
313static bool isLegalUTF8(
const uint8_t* source,
int length)
316 const uint8_t* srcptr = source + length;
325 if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
331 if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
337 if ((a = (*--srcptr)) > 0xBF)
376 if (*source >= 0x80 && *source < 0xC2)
388static ConversionResult winpr_ConvertUTF8toUTF16_Internal(
const uint8_t** sourceStart,
389 const uint8_t* sourceEnd,
390 uint16_t** targetStart,
391 const uint16_t* targetEnd,
392 ConversionFlags flags,
size_t* computed)
394 bool computeLength = (!targetEnd) ?
true : false;
396 ConversionResult result = conversionOK;
397 const uint8_t* source = *sourceStart;
398 uint16_t* target = *targetStart;
400 while (source < sourceEnd)
403 unsigned short extraBytesToRead =
404 WINPR_ASSERTING_INT_CAST(
unsigned short, trailingBytesForUTF8[*source]);
406 if ((source + extraBytesToRead) >= sourceEnd)
408 result = sourceExhausted;
413 if (!isLegalUTF8(source, extraBytesToRead + 1))
415 result = sourceIllegal;
422 switch (extraBytesToRead)
458 return sourceIllegal;
461 ch -= offsetsFromUTF8[extraBytesToRead];
463 if ((target >= targetEnd) && (!computeLength))
465 source -= (extraBytesToRead + 1);
466 result = targetExhausted;
470 if (ch <= UNI_MAX_BMP)
474 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
476 if (flags == strictConversion)
478 source -= (extraBytesToRead + 1);
479 result = sourceIllegal;
485 *target++ = setWcharFrom(UNI_REPLACEMENT_CHAR);
493 *target++ = setWcharFrom((WCHAR)ch);
498 else if (ch > UNI_MAX_UTF16)
500 if (flags == strictConversion)
502 result = sourceIllegal;
503 source -= (extraBytesToRead + 1);
509 *target++ = setWcharFrom(UNI_REPLACEMENT_CHAR);
517 if ((!computeLength) && (target + 1 >= targetEnd))
519 source -= (extraBytesToRead + 1);
520 result = targetExhausted;
528 *target++ = setWcharFrom((WCHAR)((ch >> halfShift) + UNI_SUR_HIGH_START));
529 *target++ = setWcharFrom((WCHAR)((ch & halfMask) + UNI_SUR_LOW_START));
536 *sourceStart = source;
537 *targetStart = target;
547static int winpr_ConvertUTF8toUTF16(
const uint8_t* src,
int cchSrc, uint16_t* dst,
int cchDst)
550 uint16_t* dstBeg =
nullptr;
551 uint16_t* dstEnd =
nullptr;
552 const uint8_t* srcBeg =
nullptr;
553 const uint8_t* srcEnd =
nullptr;
554 ConversionResult result = sourceIllegal;
557 cchSrc = (int)strnlen((
const char*)src, INT32_MAX - 1) + 1;
560 srcEnd = &src[cchSrc];
564 result = winpr_ConvertUTF8toUTF16_Internal(&srcBeg, srcEnd, &dstBeg, dstEnd,
565 strictConversion, &length);
570 dstEnd = &dst[cchDst];
572 result = winpr_ConvertUTF8toUTF16_Internal(&srcBeg, srcEnd, &dstBeg, dstEnd,
573 strictConversion,
nullptr);
575 length = dstBeg - dst;
578 if (result == targetExhausted)
580 SetLastError(ERROR_INSUFFICIENT_BUFFER);
584 return (result == conversionOK) ? WINPR_ASSERTING_INT_CAST(
int, length) : 0;
587static int winpr_ConvertUTF16toUTF8(
const uint16_t* src,
int cchSrc, uint8_t* dst,
int cchDst)
590 uint8_t* dstBeg =
nullptr;
591 uint8_t* dstEnd =
nullptr;
592 const uint16_t* srcBeg =
nullptr;
593 const uint16_t* srcEnd =
nullptr;
594 ConversionResult result = sourceIllegal;
597 cchSrc = (int)_wcsnlen((
const WCHAR*)src, INT32_MAX - 1) + 1;
600 srcEnd = &src[cchSrc];
604 result = winpr_ConvertUTF16toUTF8_Internal(&srcBeg, srcEnd, &dstBeg, dstEnd,
605 strictConversion, &length);
610 dstEnd = &dst[cchDst];
612 result = winpr_ConvertUTF16toUTF8_Internal(&srcBeg, srcEnd, &dstBeg, dstEnd,
613 strictConversion,
nullptr);
615 length = dstBeg - dst;
618 if (result == targetExhausted)
620 SetLastError(ERROR_INSUFFICIENT_BUFFER);
624 return (result == conversionOK) ? WINPR_ASSERTING_INT_CAST(
int, length) : 0;
629int int_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
int cbMultiByte,
630 LPWSTR lpWideCharStr,
int cchWideChar)
632 size_t cbCharLen = (size_t)cbMultiByte;
634 WINPR_UNUSED(dwFlags);
637 if ((cbMultiByte == 0) || (cbMultiByte < -1))
645 const size_t len = strlen(lpMultiByteStr);
646 if (len >= INT32_MAX)
648 cbCharLen = (int)len + 1;
651 cbCharLen = cbMultiByte;
653 WINPR_ASSERT(lpMultiByteStr);
661 WLog_ERR(TAG,
"Unsupported encoding %u", CodePage);
665 return winpr_ConvertUTF8toUTF16((
const uint8_t*)lpMultiByteStr,
666 WINPR_ASSERTING_INT_CAST(
int, cbCharLen),
667 (uint16_t*)lpWideCharStr, cchWideChar);
670int int_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
int cchWideChar,
671 LPSTR lpMultiByteStr,
int cbMultiByte, LPCSTR lpDefaultChar,
672 LPBOOL lpUsedDefaultChar)
674 size_t cbCharLen = (size_t)cchWideChar;
676 WINPR_UNUSED(dwFlags);
678 if ((cchWideChar == 0) || (cchWideChar < -1))
684 WINPR_ASSERT(lpWideCharStr);
686 if (cchWideChar == -1)
688 const size_t len = _wcslen(lpWideCharStr);
689 if (len >= INT32_MAX)
691 cbCharLen = (int)len + 1;
694 cbCharLen = cchWideChar;
701 return winpr_ConvertUTF16toUTF8((
const uint16_t*)lpWideCharStr,
702 WINPR_ASSERTING_INT_CAST(
int, cbCharLen),
703 (uint8_t*)lpMultiByteStr, cbMultiByte);