VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/utf-16.cpp@ 62477

Last change on this file since 62477 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 19.9 KB
Line 
1/* $Id: utf-16.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - UTF-16.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/string.h>
32#include "internal/iprt.h"
33
34#include <iprt/uni.h>
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/err.h>
38#include "internal/string.h"
39
40
41/**
42 * Get get length in code points of an UTF-16 encoded string, validating the
43 * string while doing so.
44 *
45 * @returns IPRT status code.
46 * @param pwsz Pointer to the UTF-16 string.
47 * @param cwc The max length of the string in UTF-16 units. Use
48 * RTSTR_MAX if all of the string is to be examined.
49 * @param pcuc Where to store the length in unicode code points.
50 * @param pcwcActual Where to store the actual size of the UTF-16 string
51 * on success. Optional.
52 */
53static int rtUtf16Length(PCRTUTF16 pwsz, size_t cwc, size_t *pcuc, size_t *pcwcActual)
54{
55 PCRTUTF16 pwszStart = pwsz;
56 size_t cCodePoints = 0;
57 while (cwc > 0)
58 {
59 RTUTF16 wc = *pwsz;
60 if (!wc)
61 break;
62 if (wc < 0xd800 || wc > 0xdfff)
63 {
64 cCodePoints++;
65 pwsz++;
66 cwc--;
67 }
68 /* Surrogate pair: */
69 else if (wc >= 0xdc00)
70 {
71 RTStrAssertMsgFailed(("Lone UTF-16 trail surrogate: %#x (%.*Rhxs)\n", wc, RT_MIN(cwc * 2, 10), pwsz));
72 return VERR_INVALID_UTF16_ENCODING;
73 }
74 else if (cwc < 2)
75 {
76 RTStrAssertMsgFailed(("Lone UTF-16 lead surrogate: %#x\n", wc));
77 return VERR_INVALID_UTF16_ENCODING;
78 }
79 else
80 {
81 RTUTF16 wcTrail = pwsz[1];
82 if (wcTrail < 0xdc00 || wcTrail > 0xdfff)
83 {
84 RTStrAssertMsgFailed(("Invalid UTF-16 trail surrogate: %#x (lead %#x)\n", wcTrail, wc));
85 return VERR_INVALID_UTF16_ENCODING;
86 }
87
88 cCodePoints++;
89 pwsz += 2;
90 cwc -= 2;
91 }
92 }
93
94 /* done */
95 *pcuc = cCodePoints;
96 if (pcwcActual)
97 *pcwcActual = pwsz - pwszStart;
98 return VINF_SUCCESS;
99}
100
101
102RTDECL(PRTUTF16) RTUtf16AllocTag(size_t cb, const char *pszTag)
103{
104 if (cb > sizeof(RTUTF16))
105 cb = RT_ALIGN_Z(cb, sizeof(RTUTF16));
106 else
107 cb = sizeof(RTUTF16);
108 PRTUTF16 pwsz = (PRTUTF16)RTMemAllocTag(cb, pszTag);
109 if (pwsz)
110 *pwsz = '\0';
111 return pwsz;
112}
113RT_EXPORT_SYMBOL(RTUtf16AllocTag);
114
115
116RTDECL(int) RTUtf16ReallocTag(PRTUTF16 *ppwsz, size_t cbNew, const char *pszTag)
117{
118 PRTUTF16 pwszOld = *ppwsz;
119 cbNew = RT_ALIGN_Z(cbNew, sizeof(RTUTF16));
120 if (!cbNew)
121 {
122 RTMemFree(pwszOld);
123 *ppwsz = NULL;
124 }
125 else if (pwszOld)
126 {
127 PRTUTF16 pwszNew = (PRTUTF16)RTMemReallocTag(pwszOld, cbNew, pszTag);
128 if (!pwszNew)
129 return VERR_NO_STR_MEMORY;
130 pwszNew[cbNew / sizeof(RTUTF16) - 1] = '\0';
131 *ppwsz = pwszNew;
132 }
133 else
134 {
135 PRTUTF16 pwszNew = (PRTUTF16)RTMemAllocTag(cbNew, pszTag);
136 if (!pwszNew)
137 return VERR_NO_UTF16_MEMORY;
138 pwszNew[0] = '\0';
139 pwszNew[cbNew / sizeof(RTUTF16) - 1] = '\0';
140 *ppwsz = pwszNew;
141 }
142 return VINF_SUCCESS;
143}
144RT_EXPORT_SYMBOL(RTUtf16ReallocTag);
145
146
147RTDECL(void) RTUtf16Free(PRTUTF16 pwszString)
148{
149 if (pwszString)
150 RTMemTmpFree(pwszString);
151}
152RT_EXPORT_SYMBOL(RTUtf16Free);
153
154
155RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag)
156{
157 Assert(pwszString);
158 size_t cb = (RTUtf16Len(pwszString) + 1) * sizeof(RTUTF16);
159 PRTUTF16 pwsz = (PRTUTF16)RTMemAllocTag(cb, pszTag);
160 if (pwsz)
161 memcpy(pwsz, pwszString, cb);
162 return pwsz;
163}
164RT_EXPORT_SYMBOL(RTUtf16DupTag);
165
166
167RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag)
168{
169 Assert(pwszString);
170 size_t cb = (RTUtf16Len(pwszString) + 1) * sizeof(RTUTF16);
171 PRTUTF16 pwsz = (PRTUTF16)RTMemAllocTag(cb + cwcExtra * sizeof(RTUTF16), pszTag);
172 if (pwsz)
173 {
174 memcpy(pwsz, pwszString, cb);
175 *ppwszString = pwsz;
176 return VINF_SUCCESS;
177 }
178 return VERR_NO_MEMORY;
179}
180RT_EXPORT_SYMBOL(RTUtf16DupExTag);
181
182
183RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString)
184{
185 if (!pwszString)
186 return 0;
187
188 PCRTUTF16 pwsz = pwszString;
189 while (*pwsz)
190 pwsz++;
191 return pwsz - pwszString;
192}
193RT_EXPORT_SYMBOL(RTUtf16Len);
194
195
196RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2)
197{
198 if (pwsz1 == pwsz2)
199 return 0;
200 if (!pwsz1)
201 return -1;
202 if (!pwsz2)
203 return 1;
204
205 for (;;)
206 {
207 register RTUTF16 wcs = *pwsz1;
208 register int iDiff = wcs - *pwsz2;
209 if (iDiff || !wcs)
210 return iDiff;
211 pwsz1++;
212 pwsz2++;
213 }
214}
215RT_EXPORT_SYMBOL(RTUtf16Cmp);
216
217
218RTDECL(int) RTUtf16CmpUtf8(PCRTUTF16 pwsz1, const char *psz2)
219{
220 /*
221 * NULL and empty strings are all the same.
222 */
223 if (!pwsz1)
224 return !psz2 || !*psz2 ? 0 : -1;
225 if (!psz2)
226 return !*pwsz1 ? 0 : 1;
227
228 /*
229 * Compare with a UTF-8 string by enumerating them char by char.
230 */
231 for (;;)
232 {
233 RTUNICP uc1;
234 int rc = RTUtf16GetCpEx(&pwsz1, &uc1);
235 AssertRCReturn(rc, 1);
236
237 RTUNICP uc2;
238 rc = RTStrGetCpEx(&psz2, &uc2);
239 AssertRCReturn(rc, -1);
240 if (uc1 == uc2)
241 {
242 if (uc1)
243 continue;
244 return 0;
245 }
246 return uc1 < uc2 ? -1 : 1;
247 }
248}
249RT_EXPORT_SYMBOL(RTUtf16CmpUtf8);
250
251
252RTDECL(int) RTUtf16ValidateEncoding(PCRTUTF16 pwsz)
253{
254 return RTUtf16ValidateEncodingEx(pwsz, RTSTR_MAX, 0);
255}
256RT_EXPORT_SYMBOL(RTUtf16ValidateEncoding);
257
258
259RTDECL(int) RTUtf16ValidateEncodingEx(PCRTUTF16 pwsz, size_t cwc, uint32_t fFlags)
260{
261 AssertReturn(!(fFlags & ~(RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED | RTSTR_VALIDATE_ENCODING_EXACT_LENGTH)),
262 VERR_INVALID_PARAMETER);
263 AssertPtr(pwsz);
264
265 /*
266 * Use rtUtf16Length for the job.
267 */
268 size_t cwcActual = 0; /* Shut up cc1plus. */
269 size_t cCpsIgnored;
270 int rc = rtUtf16Length(pwsz, cwc, &cCpsIgnored, &cwcActual);
271 if (RT_SUCCESS(rc))
272 {
273 if (fFlags & RTSTR_VALIDATE_ENCODING_EXACT_LENGTH)
274 {
275 if (fFlags & RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED)
276 cwcActual++;
277 if (cwcActual == cwc)
278 rc = VINF_SUCCESS;
279 else if (cwcActual < cwc)
280 rc = VERR_BUFFER_UNDERFLOW;
281 else
282 rc = VERR_BUFFER_OVERFLOW;
283 }
284 else if ( (fFlags & RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED)
285 && cwcActual >= cwc)
286 rc = VERR_BUFFER_OVERFLOW;
287 }
288 return rc;
289}
290RT_EXPORT_SYMBOL(RTUtf16ValidateEncodingEx);
291
292
293RTDECL(bool) RTUtf16IsValidEncoding(PCRTUTF16 pwsz)
294{
295 int rc = RTUtf16ValidateEncodingEx(pwsz, RTSTR_MAX, 0);
296 return RT_SUCCESS(rc);
297}
298RT_EXPORT_SYMBOL(RTUtf16IsValidEncoding);
299
300
301RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidSet, char chReplacement)
302{
303 size_t cReplacements = 0;
304 AssertReturn(chReplacement && (unsigned)chReplacement < 128, -1);
305 /* Validate the encoding. */
306 for (;;)
307 {
308 RTUNICP Cp;
309 PCRTUNICP pCp;
310 PRTUTF16 pwszOld = pwsz;
311 if (RT_FAILURE(RTUtf16GetCpEx((PCRTUTF16 *)&pwsz, &Cp)))
312 return -1;
313 if (!Cp)
314 break;
315 for (pCp = puszValidSet; *pCp; pCp += 2)
316 {
317 AssertReturn(*(pCp + 1), -1);
318 if (*pCp <= Cp && *(pCp + 1) >= Cp) /* No, I won't do * and ++. */
319 break;
320 }
321 if (!*pCp)
322 {
323 for (; pwszOld != pwsz; ++pwszOld)
324 *pwszOld = chReplacement;
325 ++cReplacements;
326 }
327 }
328 return cReplacements;
329}
330RT_EXPORT_SYMBOL(RTUtf16PurgeComplementSet);
331
332
333/**
334 * Validate the UTF-16 encoding and calculates the length of an UTF-8 encoding.
335 *
336 * @returns iprt status code.
337 * @param pwsz The UTF-16 string.
338 * @param cwc The max length of the UTF-16 string to consider.
339 * @param pcch Where to store the length (excluding '\\0') of the UTF-8 string. (cch == cb, btw)
340 */
341static int rtUtf16CalcUtf8Length(PCRTUTF16 pwsz, size_t cwc, size_t *pcch)
342{
343 int rc = VINF_SUCCESS;
344 size_t cch = 0;
345 while (cwc > 0)
346 {
347 RTUTF16 wc = *pwsz++; cwc--;
348 if (!wc)
349 break;
350 else if (wc < 0xd800 || wc > 0xdfff)
351 {
352 if (wc < 0x80)
353 cch++;
354 else if (wc < 0x800)
355 cch += 2;
356 else if (wc < 0xfffe)
357 cch += 3;
358 else
359 {
360 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc));
361 rc = VERR_CODE_POINT_ENDIAN_INDICATOR;
362 break;
363 }
364 }
365 else
366 {
367 if (wc >= 0xdc00)
368 {
369 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc));
370 rc = VERR_INVALID_UTF16_ENCODING;
371 break;
372 }
373 if (cwc <= 0)
374 {
375 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc));
376 rc = VERR_INVALID_UTF16_ENCODING;
377 break;
378 }
379 wc = *pwsz++; cwc--;
380 if (wc < 0xdc00 || wc > 0xdfff)
381 {
382 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc));
383 rc = VERR_INVALID_UTF16_ENCODING;
384 break;
385 }
386 cch += 4;
387 }
388 }
389
390
391 /* done */
392 *pcch = cch;
393 return rc;
394}
395
396
397/**
398 * Recodes an valid UTF-16 string as UTF-8.
399 *
400 * @returns iprt status code.
401 * @param pwsz The UTF-16 string.
402 * @param cwc The number of RTUTF16 characters to process from pwsz. The recoding
403 * will stop when cwc or '\\0' is reached.
404 * @param psz Where to store the UTF-8 string.
405 * @param cch The size of the UTF-8 buffer, excluding the terminator.
406 * @param pcch Where to store the number of octets actually encoded.
407 */
408static int rtUtf16RecodeAsUtf8(PCRTUTF16 pwsz, size_t cwc, char *psz, size_t cch, size_t *pcch)
409{
410 unsigned char *pwch = (unsigned char *)psz;
411 int rc = VINF_SUCCESS;
412 while (cwc > 0)
413 {
414 RTUTF16 wc = *pwsz++; cwc--;
415 if (!wc)
416 break;
417 else if (wc < 0xd800 || wc > 0xdfff)
418 {
419 if (wc < 0x80)
420 {
421 if (RT_UNLIKELY(cch < 1))
422 {
423 RTStrAssertMsgFailed(("Buffer overflow! 1\n"));
424 rc = VERR_BUFFER_OVERFLOW;
425 break;
426 }
427 cch--;
428 *pwch++ = (unsigned char)wc;
429 }
430 else if (wc < 0x800)
431 {
432 if (RT_UNLIKELY(cch < 2))
433 {
434 RTStrAssertMsgFailed(("Buffer overflow! 2\n"));
435 rc = VERR_BUFFER_OVERFLOW;
436 break;
437 }
438 cch -= 2;
439 *pwch++ = 0xc0 | (wc >> 6);
440 *pwch++ = 0x80 | (wc & 0x3f);
441 }
442 else if (wc < 0xfffe)
443 {
444 if (RT_UNLIKELY(cch < 3))
445 {
446 RTStrAssertMsgFailed(("Buffer overflow! 3\n"));
447 rc = VERR_BUFFER_OVERFLOW;
448 break;
449 }
450 cch -= 3;
451 *pwch++ = 0xe0 | (wc >> 12);
452 *pwch++ = 0x80 | ((wc >> 6) & 0x3f);
453 *pwch++ = 0x80 | (wc & 0x3f);
454 }
455 else
456 {
457 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc));
458 rc = VERR_CODE_POINT_ENDIAN_INDICATOR;
459 break;
460 }
461 }
462 else
463 {
464 if (wc >= 0xdc00)
465 {
466 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc));
467 rc = VERR_INVALID_UTF16_ENCODING;
468 break;
469 }
470 if (cwc <= 0)
471 {
472 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc));
473 rc = VERR_INVALID_UTF16_ENCODING;
474 break;
475 }
476 RTUTF16 wc2 = *pwsz++; cwc--;
477 if (wc2 < 0xdc00 || wc2 > 0xdfff)
478 {
479 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc));
480 rc = VERR_INVALID_UTF16_ENCODING;
481 break;
482 }
483 uint32_t CodePoint = 0x10000
484 + ( ((wc & 0x3ff) << 10)
485 | (wc2 & 0x3ff));
486 if (RT_UNLIKELY(cch < 4))
487 {
488 RTStrAssertMsgFailed(("Buffer overflow! 4\n"));
489 rc = VERR_BUFFER_OVERFLOW;
490 break;
491 }
492 cch -= 4;
493 *pwch++ = 0xf0 | (CodePoint >> 18);
494 *pwch++ = 0x80 | ((CodePoint >> 12) & 0x3f);
495 *pwch++ = 0x80 | ((CodePoint >> 6) & 0x3f);
496 *pwch++ = 0x80 | (CodePoint & 0x3f);
497 }
498 }
499
500 /* done */
501 *pwch = '\0';
502 *pcch = (char *)pwch - psz;
503 return rc;
504}
505
506
507
508RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag)
509{
510 /*
511 * Validate input.
512 */
513 Assert(VALID_PTR(ppszString));
514 Assert(VALID_PTR(pwszString));
515 *ppszString = NULL;
516
517 /*
518 * Validate the UTF-16 string and calculate the length of the UTF-8 encoding of it.
519 */
520 size_t cch;
521 int rc = rtUtf16CalcUtf8Length(pwszString, RTSTR_MAX, &cch);
522 if (RT_SUCCESS(rc))
523 {
524 /*
525 * Allocate buffer and recode it.
526 */
527 char *pszResult = (char *)RTMemAllocTag(cch + 1, pszTag);
528 if (pszResult)
529 {
530 rc = rtUtf16RecodeAsUtf8(pwszString, RTSTR_MAX, pszResult, cch, &cch);
531 if (RT_SUCCESS(rc))
532 {
533 *ppszString = pszResult;
534 return rc;
535 }
536
537 RTMemFree(pszResult);
538 }
539 else
540 rc = VERR_NO_STR_MEMORY;
541 }
542 return rc;
543}
544RT_EXPORT_SYMBOL(RTUtf16ToUtf8Tag);
545
546
547RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag)
548{
549 /*
550 * Validate input.
551 */
552 Assert(VALID_PTR(pwszString));
553 Assert(VALID_PTR(ppsz));
554 Assert(!pcch || VALID_PTR(pcch));
555
556 /*
557 * Validate the UTF-16 string and calculate the length of the UTF-8 encoding of it.
558 */
559 size_t cchResult;
560 int rc = rtUtf16CalcUtf8Length(pwszString, cwcString, &cchResult);
561 if (RT_SUCCESS(rc))
562 {
563 if (pcch)
564 *pcch = cchResult;
565
566 /*
567 * Check buffer size / Allocate buffer and recode it.
568 */
569 bool fShouldFree;
570 char *pszResult;
571 if (cch > 0 && *ppsz)
572 {
573 fShouldFree = false;
574 if (RT_UNLIKELY(cch <= cchResult))
575 return VERR_BUFFER_OVERFLOW;
576 pszResult = *ppsz;
577 }
578 else
579 {
580 *ppsz = NULL;
581 fShouldFree = true;
582 cch = RT_MAX(cch, cchResult + 1);
583 pszResult = (char *)RTStrAllocTag(cch, pszTag);
584 }
585 if (pszResult)
586 {
587 rc = rtUtf16RecodeAsUtf8(pwszString, cwcString, pszResult, cch - 1, &cch);
588 if (RT_SUCCESS(rc))
589 {
590 *ppsz = pszResult;
591 return rc;
592 }
593
594 if (fShouldFree)
595 RTStrFree(pszResult);
596 }
597 else
598 rc = VERR_NO_STR_MEMORY;
599 }
600 return rc;
601}
602RT_EXPORT_SYMBOL(RTUtf16ToUtf8ExTag);
603
604
605RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz)
606{
607 size_t cch;
608 int rc = rtUtf16CalcUtf8Length(pwsz, RTSTR_MAX, &cch);
609 return RT_SUCCESS(rc) ? cch : 0;
610}
611RT_EXPORT_SYMBOL(RTUtf16CalcUtf8Len);
612
613
614RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch)
615{
616 size_t cch;
617 int rc = rtUtf16CalcUtf8Length(pwsz, cwc, &cch);
618 if (pcch)
619 *pcch = RT_SUCCESS(rc) ? cch : ~(size_t)0;
620 return rc;
621}
622RT_EXPORT_SYMBOL(RTUtf16CalcUtf8LenEx);
623
624
625RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz)
626{
627 const RTUTF16 wc = *pwsz;
628
629 /* simple */
630 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
631 return wc;
632 if (wc < 0xfffe)
633 {
634 /* surrogate pair */
635 if (wc < 0xdc00)
636 {
637 const RTUTF16 wc2 = pwsz[1];
638 if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
639 {
640 RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
641 return uc;
642 }
643
644 RTStrAssertMsgFailed(("wc=%#08x wc2=%#08x - invalid 2nd char in surrogate pair\n", wc, wc2));
645 }
646 else
647 RTStrAssertMsgFailed(("wc=%#08x - invalid surrogate pair order\n", wc));
648 }
649 else
650 RTStrAssertMsgFailed(("wc=%#08x - endian indicator\n", wc));
651 return RTUNICP_INVALID;
652}
653RT_EXPORT_SYMBOL(RTUtf16GetCpInternal);
654
655
656RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp)
657{
658 const RTUTF16 wc = **ppwsz;
659
660 /* simple */
661 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
662 {
663 (*ppwsz)++;
664 *pCp = wc;
665 return VINF_SUCCESS;
666 }
667
668 int rc;
669 if (wc < 0xfffe)
670 {
671 /* surrogate pair */
672 if (wc < 0xdc00)
673 {
674 const RTUTF16 wc2 = (*ppwsz)[1];
675 if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
676 {
677 RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
678 *pCp = uc;
679 (*ppwsz) += 2;
680 return VINF_SUCCESS;
681 }
682
683 RTStrAssertMsgFailed(("wc=%#08x wc2=%#08x - invalid 2nd char in surrogate pair\n", wc, wc2));
684 }
685 else
686 RTStrAssertMsgFailed(("wc=%#08x - invalid surrogate pair order\n", wc));
687 rc = VERR_INVALID_UTF16_ENCODING;
688 }
689 else
690 {
691 RTStrAssertMsgFailed(("wc=%#08x - endian indicator\n", wc));
692 rc = VERR_CODE_POINT_ENDIAN_INDICATOR;
693 }
694 *pCp = RTUNICP_INVALID;
695 (*ppwsz)++;
696 return rc;
697}
698RT_EXPORT_SYMBOL(RTUtf16GetCpExInternal);
699
700
701RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint)
702{
703 /* simple */
704 if ( CodePoint < 0xd800
705 || ( CodePoint > 0xdfff
706 && CodePoint < 0xfffe))
707 {
708 *pwsz++ = (RTUTF16)CodePoint;
709 return pwsz;
710 }
711
712 /* surrogate pair */
713 if (CodePoint >= 0x10000 && CodePoint <= 0x0010ffff)
714 {
715 CodePoint -= 0x10000;
716 *pwsz++ = 0xd800 | (CodePoint >> 10);
717 *pwsz++ = 0xdc00 | (CodePoint & 0x3ff);
718 return pwsz;
719 }
720
721 /* invalid code point. */
722 RTStrAssertMsgFailed(("Invalid codepoint %#x\n", CodePoint));
723 *pwsz++ = 0x7f;
724 return pwsz;
725}
726RT_EXPORT_SYMBOL(RTUtf16PutCpInternal);
727
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette