1 | /* $Id: utf-8.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UTF-8 Decoding.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 | /**
|
---|
43 | * Get get length in code points of a UTF-8 encoded string.
|
---|
44 | * The string is validated while doing this.
|
---|
45 | *
|
---|
46 | * @returns IPRT status code.
|
---|
47 | * @param psz Pointer to the UTF-8 string.
|
---|
48 | * @param cch The max length of the string. (btw cch = cb)
|
---|
49 | * Use RTSTR_MAX if all of the string is to be examined.
|
---|
50 | * @param pcuc Where to store the length in unicode code points.
|
---|
51 | * @param pcchActual Where to store the actual size of the UTF-8 string
|
---|
52 | * on success (cch = cb again). Optional.
|
---|
53 | */
|
---|
54 | int rtUtf8Length(const char *psz, size_t cch, size_t *pcuc, size_t *pcchActual)
|
---|
55 | {
|
---|
56 | const unsigned char *puch = (const unsigned char *)psz;
|
---|
57 | size_t cCodePoints = 0;
|
---|
58 | while (cch > 0)
|
---|
59 | {
|
---|
60 | const unsigned char uch = *puch;
|
---|
61 | if (!uch)
|
---|
62 | break;
|
---|
63 | if (uch & RT_BIT(7))
|
---|
64 | {
|
---|
65 | /* figure sequence length and validate the first byte */
|
---|
66 | /** @todo RT_USE_RTC_3629 */
|
---|
67 | unsigned cb;
|
---|
68 | if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5))) == (RT_BIT(7) | RT_BIT(6)))
|
---|
69 | cb = 2;
|
---|
70 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5)))
|
---|
71 | cb = 3;
|
---|
72 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4)))
|
---|
73 | cb = 4;
|
---|
74 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3)))
|
---|
75 | cb = 5;
|
---|
76 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2) | RT_BIT(1))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2)))
|
---|
77 | cb = 6;
|
---|
78 | else
|
---|
79 | {
|
---|
80 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(cch, 10), puch));
|
---|
81 | return VERR_INVALID_UTF8_ENCODING;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* check length */
|
---|
85 | if (cb > cch)
|
---|
86 | {
|
---|
87 | RTStrAssertMsgFailed(("Invalid UTF-8 length: cb=%d cch=%d (%.*Rhxs)\n", cb, cch, RT_MIN(cch, 10), puch));
|
---|
88 | return VERR_INVALID_UTF8_ENCODING;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* validate the rest */
|
---|
92 | switch (cb)
|
---|
93 | {
|
---|
94 | case 6:
|
---|
95 | RTStrAssertMsgReturn((puch[5] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("6/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
96 | case 5:
|
---|
97 | RTStrAssertMsgReturn((puch[4] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("5/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
98 | case 4:
|
---|
99 | RTStrAssertMsgReturn((puch[3] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("4/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
100 | case 3:
|
---|
101 | RTStrAssertMsgReturn((puch[2] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("3/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
102 | case 2:
|
---|
103 | RTStrAssertMsgReturn((puch[1] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("2/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* validate the code point. */
|
---|
108 | RTUNICP uc;
|
---|
109 | switch (cb)
|
---|
110 | {
|
---|
111 | case 6:
|
---|
112 | uc = (puch[5] & 0x3f)
|
---|
113 | | ((RTUNICP)(puch[4] & 0x3f) << 6)
|
---|
114 | | ((RTUNICP)(puch[3] & 0x3f) << 12)
|
---|
115 | | ((RTUNICP)(puch[2] & 0x3f) << 18)
|
---|
116 | | ((RTUNICP)(puch[1] & 0x3f) << 24)
|
---|
117 | | ((RTUNICP)(uch & 0x01) << 30);
|
---|
118 | RTStrAssertMsgReturn(uc >= 0x04000000 && uc <= 0x7fffffff,
|
---|
119 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
120 | break;
|
---|
121 | case 5:
|
---|
122 | uc = (puch[4] & 0x3f)
|
---|
123 | | ((RTUNICP)(puch[3] & 0x3f) << 6)
|
---|
124 | | ((RTUNICP)(puch[2] & 0x3f) << 12)
|
---|
125 | | ((RTUNICP)(puch[1] & 0x3f) << 18)
|
---|
126 | | ((RTUNICP)(uch & 0x03) << 24);
|
---|
127 | RTStrAssertMsgReturn(uc >= 0x00200000 && uc <= 0x03ffffff,
|
---|
128 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
129 | break;
|
---|
130 | case 4:
|
---|
131 | uc = (puch[3] & 0x3f)
|
---|
132 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
133 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
134 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
135 | RTStrAssertMsgReturn(uc >= 0x00010000 && uc <= 0x001fffff,
|
---|
136 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
137 | break;
|
---|
138 | case 3:
|
---|
139 | uc = (puch[2] & 0x3f)
|
---|
140 | | ((RTUNICP)(puch[1] & 0x3f) << 6)
|
---|
141 | | ((RTUNICP)(uch & 0x0f) << 12);
|
---|
142 | RTStrAssertMsgReturn(uc >= 0x00000800 && uc <= 0x0000fffd,
|
---|
143 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch),
|
---|
144 | uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING);
|
---|
145 | RTStrAssertMsgReturn(uc < 0xd800 || uc > 0xdfff,
|
---|
146 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_CODE_POINT_SURROGATE);
|
---|
147 | break;
|
---|
148 | case 2:
|
---|
149 | uc = (puch[1] & 0x3f)
|
---|
150 | | ((RTUNICP)(uch & 0x1f) << 6);
|
---|
151 | RTStrAssertMsgReturn(uc >= 0x00000080 && uc <= 0x000007ff,
|
---|
152 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
153 | break;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* advance */
|
---|
157 | cch -= cb;
|
---|
158 | puch += cb;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | /* one ASCII byte */
|
---|
163 | puch++;
|
---|
164 | cch--;
|
---|
165 | }
|
---|
166 | cCodePoints++;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* done */
|
---|
170 | *pcuc = cCodePoints;
|
---|
171 | if (pcchActual)
|
---|
172 | *pcchActual = puch - (unsigned char const *)psz;
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Decodes and UTF-8 string into an array of unicode code point.
|
---|
179 | *
|
---|
180 | * Since we know the input is valid, we do *not* perform encoding or length checks.
|
---|
181 | *
|
---|
182 | * @returns iprt status code.
|
---|
183 | * @param psz The UTF-8 string to recode. This is a valid encoding.
|
---|
184 | * @param cch The number of chars (the type char, so bytes if you like) to process of the UTF-8 string.
|
---|
185 | * The recoding will stop when cch or '\\0' is reached. Pass RTSTR_MAX to process up to '\\0'.
|
---|
186 | * @param paCps Where to store the code points array.
|
---|
187 | * @param cCps The number of RTUNICP items the paCps buffer can hold, excluding the terminator ('\\0').
|
---|
188 | */
|
---|
189 | static int rtUtf8Decode(const char *psz, size_t cch, PRTUNICP paCps, size_t cCps)
|
---|
190 | {
|
---|
191 | int rc = VINF_SUCCESS;
|
---|
192 | const unsigned char *puch = (const unsigned char *)psz;
|
---|
193 | PRTUNICP pCp = paCps;
|
---|
194 | while (cch > 0)
|
---|
195 | {
|
---|
196 | /* read the next char and check for terminator. */
|
---|
197 | const unsigned char uch = *puch;
|
---|
198 | if (!uch)
|
---|
199 | break;
|
---|
200 |
|
---|
201 | /* check for output overflow */
|
---|
202 | if (RT_UNLIKELY(cCps < 1))
|
---|
203 | {
|
---|
204 | rc = VERR_BUFFER_OVERFLOW;
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | cCps--;
|
---|
208 |
|
---|
209 | /* decode and recode the code point */
|
---|
210 | if (!(uch & RT_BIT(7)))
|
---|
211 | {
|
---|
212 | *pCp++ = uch;
|
---|
213 | puch++;
|
---|
214 | cch--;
|
---|
215 | }
|
---|
216 | #ifdef RT_STRICT
|
---|
217 | else if (!(uch & RT_BIT(6)))
|
---|
218 | AssertMsgFailed(("Internal error!\n"));
|
---|
219 | #endif
|
---|
220 | else if (!(uch & RT_BIT(5)))
|
---|
221 | {
|
---|
222 | *pCp++ = (puch[1] & 0x3f)
|
---|
223 | | ((uint16_t)(uch & 0x1f) << 6);
|
---|
224 | puch += 2;
|
---|
225 | cch -= 2;
|
---|
226 | }
|
---|
227 | else if (!(uch & RT_BIT(4)))
|
---|
228 | {
|
---|
229 | *pCp++ = (puch[2] & 0x3f)
|
---|
230 | | ((uint16_t)(puch[1] & 0x3f) << 6)
|
---|
231 | | ((uint16_t)(uch & 0x0f) << 12);
|
---|
232 | puch += 3;
|
---|
233 | cch -= 3;
|
---|
234 | }
|
---|
235 | else if (!(uch & RT_BIT(3)))
|
---|
236 | {
|
---|
237 | *pCp++ = (puch[3] & 0x3f)
|
---|
238 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
239 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
240 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
241 | puch += 4;
|
---|
242 | cch -= 4;
|
---|
243 | }
|
---|
244 | else if (!(uch & RT_BIT(2)))
|
---|
245 | {
|
---|
246 | *pCp++ = (puch[4] & 0x3f)
|
---|
247 | | ((RTUNICP)(puch[3] & 0x3f) << 6)
|
---|
248 | | ((RTUNICP)(puch[2] & 0x3f) << 12)
|
---|
249 | | ((RTUNICP)(puch[1] & 0x3f) << 18)
|
---|
250 | | ((RTUNICP)(uch & 0x03) << 24);
|
---|
251 | puch += 5;
|
---|
252 | cch -= 6;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | Assert(!(uch & RT_BIT(1)));
|
---|
257 | *pCp++ = (puch[5] & 0x3f)
|
---|
258 | | ((RTUNICP)(puch[4] & 0x3f) << 6)
|
---|
259 | | ((RTUNICP)(puch[3] & 0x3f) << 12)
|
---|
260 | | ((RTUNICP)(puch[2] & 0x3f) << 18)
|
---|
261 | | ((RTUNICP)(puch[1] & 0x3f) << 24)
|
---|
262 | | ((RTUNICP)(uch & 0x01) << 30);
|
---|
263 | puch += 6;
|
---|
264 | cch -= 6;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* done */
|
---|
269 | *pCp = 0;
|
---|
270 | return rc;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | RTDECL(size_t) RTStrUniLen(const char *psz)
|
---|
275 | {
|
---|
276 | size_t cCodePoints;
|
---|
277 | int rc = rtUtf8Length(psz, RTSTR_MAX, &cCodePoints, NULL);
|
---|
278 | return RT_SUCCESS(rc) ? cCodePoints : 0;
|
---|
279 | }
|
---|
280 | RT_EXPORT_SYMBOL(RTStrUniLen);
|
---|
281 |
|
---|
282 |
|
---|
283 | RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcCps)
|
---|
284 | {
|
---|
285 | size_t cCodePoints;
|
---|
286 | int rc = rtUtf8Length(psz, cch, &cCodePoints, NULL);
|
---|
287 | if (pcCps)
|
---|
288 | *pcCps = RT_SUCCESS(rc) ? cCodePoints : 0;
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 | RT_EXPORT_SYMBOL(RTStrUniLenEx);
|
---|
292 |
|
---|
293 |
|
---|
294 | RTDECL(int) RTStrValidateEncoding(const char *psz)
|
---|
295 | {
|
---|
296 | return RTStrValidateEncodingEx(psz, RTSTR_MAX, 0);
|
---|
297 | }
|
---|
298 | RT_EXPORT_SYMBOL(RTStrValidateEncoding);
|
---|
299 |
|
---|
300 |
|
---|
301 | RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags)
|
---|
302 | {
|
---|
303 | AssertReturn(!(fFlags & ~(RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED)), VERR_INVALID_PARAMETER);
|
---|
304 | AssertPtr(psz);
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Use rtUtf8Length for the job.
|
---|
308 | */
|
---|
309 | size_t cchActual;
|
---|
310 | size_t cCpsIgnored;
|
---|
311 | int rc = rtUtf8Length(psz, cch, &cCpsIgnored, &cchActual);
|
---|
312 | if (RT_SUCCESS(rc))
|
---|
313 | {
|
---|
314 | if ( (fFlags & RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED)
|
---|
315 | && cchActual >= cch)
|
---|
316 | rc = VERR_BUFFER_OVERFLOW;
|
---|
317 | }
|
---|
318 | return rc;
|
---|
319 | }
|
---|
320 | RT_EXPORT_SYMBOL(RTStrValidateEncodingEx);
|
---|
321 |
|
---|
322 |
|
---|
323 | RTDECL(bool) RTStrIsValidEncoding(const char *psz)
|
---|
324 | {
|
---|
325 | int rc = RTStrValidateEncodingEx(psz, RTSTR_MAX, 0);
|
---|
326 | return RT_SUCCESS(rc);
|
---|
327 | }
|
---|
328 | RT_EXPORT_SYMBOL(RTStrIsValidEncoding);
|
---|
329 |
|
---|
330 |
|
---|
331 | RTDECL(size_t) RTStrPurgeEncoding(char *psz)
|
---|
332 | {
|
---|
333 | size_t cErrors = 0;
|
---|
334 | for (;;)
|
---|
335 | {
|
---|
336 | RTUNICP Cp;
|
---|
337 | int rc = RTStrGetCpEx((const char **)&psz, &Cp);
|
---|
338 | if (RT_SUCCESS(rc))
|
---|
339 | {
|
---|
340 | if (!Cp)
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | psz[-1] = '?';
|
---|
346 | cErrors++;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | return cErrors;
|
---|
350 | }
|
---|
351 | RT_EXPORT_SYMBOL(RTStrPurgeEncoding);
|
---|
352 |
|
---|
353 |
|
---|
354 | RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppaCps)
|
---|
355 | {
|
---|
356 | /*
|
---|
357 | * Validate input.
|
---|
358 | */
|
---|
359 | Assert(VALID_PTR(pszString));
|
---|
360 | Assert(VALID_PTR(ppaCps));
|
---|
361 | *ppaCps = NULL;
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Validate the UTF-8 input and count its code points.
|
---|
365 | */
|
---|
366 | size_t cCps;
|
---|
367 | int rc = rtUtf8Length(pszString, RTSTR_MAX, &cCps, NULL);
|
---|
368 | if (RT_SUCCESS(rc))
|
---|
369 | {
|
---|
370 | /*
|
---|
371 | * Allocate buffer.
|
---|
372 | */
|
---|
373 | PRTUNICP paCps = (PRTUNICP)RTMemAlloc((cCps + 1) * sizeof(RTUNICP));
|
---|
374 | if (paCps)
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * Decode the string.
|
---|
378 | */
|
---|
379 | rc = rtUtf8Decode(pszString, RTSTR_MAX, paCps, cCps);
|
---|
380 | if (RT_SUCCESS(rc))
|
---|
381 | {
|
---|
382 | *ppaCps = paCps;
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 | RTMemFree(paCps);
|
---|
386 | }
|
---|
387 | else
|
---|
388 | rc = VERR_NO_CODE_POINT_MEMORY;
|
---|
389 | }
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 | RT_EXPORT_SYMBOL(RTStrToUni);
|
---|
393 |
|
---|
394 |
|
---|
395 | RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps)
|
---|
396 | {
|
---|
397 | /*
|
---|
398 | * Validate input.
|
---|
399 | */
|
---|
400 | Assert(VALID_PTR(pszString));
|
---|
401 | Assert(VALID_PTR(ppaCps));
|
---|
402 | Assert(!pcCps || VALID_PTR(pcCps));
|
---|
403 |
|
---|
404 | /*
|
---|
405 | * Validate the UTF-8 input and count the code points.
|
---|
406 | */
|
---|
407 | size_t cCpsResult;
|
---|
408 | int rc = rtUtf8Length(pszString, cchString, &cCpsResult, NULL);
|
---|
409 | if (RT_SUCCESS(rc))
|
---|
410 | {
|
---|
411 | if (pcCps)
|
---|
412 | *pcCps = cCpsResult;
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Check buffer size / Allocate buffer.
|
---|
416 | */
|
---|
417 | bool fShouldFree;
|
---|
418 | PRTUNICP paCpsResult;
|
---|
419 | if (cCps > 0 && *ppaCps)
|
---|
420 | {
|
---|
421 | fShouldFree = false;
|
---|
422 | if (cCps <= cCpsResult)
|
---|
423 | return VERR_BUFFER_OVERFLOW;
|
---|
424 | paCpsResult = *ppaCps;
|
---|
425 | }
|
---|
426 | else
|
---|
427 | {
|
---|
428 | *ppaCps = NULL;
|
---|
429 | fShouldFree = true;
|
---|
430 | cCps = RT_MAX(cCpsResult + 1, cCps);
|
---|
431 | paCpsResult = (PRTUNICP)RTMemAlloc(cCps * sizeof(RTUNICP));
|
---|
432 | }
|
---|
433 | if (paCpsResult)
|
---|
434 | {
|
---|
435 | /*
|
---|
436 | * Encode the UTF-16 string.
|
---|
437 | */
|
---|
438 | rc = rtUtf8Decode(pszString, cchString, paCpsResult, cCps - 1);
|
---|
439 | if (RT_SUCCESS(rc))
|
---|
440 | {
|
---|
441 | *ppaCps = paCpsResult;
|
---|
442 | return rc;
|
---|
443 | }
|
---|
444 | if (fShouldFree)
|
---|
445 | RTMemFree(paCpsResult);
|
---|
446 | }
|
---|
447 | else
|
---|
448 | rc = VERR_NO_CODE_POINT_MEMORY;
|
---|
449 | }
|
---|
450 | return rc;
|
---|
451 | }
|
---|
452 | RT_EXPORT_SYMBOL(RTStrToUniEx);
|
---|
453 |
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Calculates the UTF-16 length of a string, validating the encoding while doing so.
|
---|
457 | *
|
---|
458 | * @returns IPRT status code.
|
---|
459 | * @param psz Pointer to the UTF-8 string.
|
---|
460 | * @param cch The max length of the string. (btw cch = cb)
|
---|
461 | * Use RTSTR_MAX if all of the string is to be examined.
|
---|
462 | * @param pcwc Where to store the length of the UTF-16 string as a number of RTUTF16 characters.
|
---|
463 | */
|
---|
464 | static int rtUtf8CalcUtf16Length(const char *psz, size_t cch, size_t *pcwc)
|
---|
465 | {
|
---|
466 | const unsigned char *puch = (const unsigned char *)psz;
|
---|
467 | size_t cwc = 0;
|
---|
468 | while (cch > 0)
|
---|
469 | {
|
---|
470 | const unsigned char uch = *puch;
|
---|
471 | if (!uch)
|
---|
472 | break;
|
---|
473 | if (!(uch & RT_BIT(7)))
|
---|
474 | {
|
---|
475 | /* one ASCII byte */
|
---|
476 | cwc++;
|
---|
477 | puch++;
|
---|
478 | cch--;
|
---|
479 | }
|
---|
480 | else
|
---|
481 | {
|
---|
482 | /* figure sequence length and validate the first byte */
|
---|
483 | unsigned cb;
|
---|
484 | if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5))) == (RT_BIT(7) | RT_BIT(6)))
|
---|
485 | cb = 2;
|
---|
486 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5)))
|
---|
487 | cb = 3;
|
---|
488 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4)))
|
---|
489 | cb = 4;
|
---|
490 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3)))
|
---|
491 | cb = 5;
|
---|
492 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2) | RT_BIT(1))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3) | RT_BIT(2)))
|
---|
493 | cb = 6;
|
---|
494 | else
|
---|
495 | {
|
---|
496 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(cch, 10), puch));
|
---|
497 | return VERR_INVALID_UTF8_ENCODING;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /* check length */
|
---|
501 | if (cb > cch)
|
---|
502 | {
|
---|
503 | RTStrAssertMsgFailed(("Invalid UTF-8 length: cb=%d cch=%d (%.*Rhxs)\n", cb, cch, RT_MIN(cch, 10), puch));
|
---|
504 | return VERR_INVALID_UTF8_ENCODING;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* validate the rest */
|
---|
508 | switch (cb)
|
---|
509 | {
|
---|
510 | case 6:
|
---|
511 | RTStrAssertMsgReturn((puch[5] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("6/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
512 | case 5:
|
---|
513 | RTStrAssertMsgReturn((puch[4] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("5/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
514 | case 4:
|
---|
515 | RTStrAssertMsgReturn((puch[3] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("4/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
516 | case 3:
|
---|
517 | RTStrAssertMsgReturn((puch[2] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("3/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
518 | case 2:
|
---|
519 | RTStrAssertMsgReturn((puch[1] & (RT_BIT(7) | RT_BIT(6))) == RT_BIT(7), ("2/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
520 | break;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /* validate the code point. */
|
---|
524 | RTUNICP uc;
|
---|
525 | switch (cb)
|
---|
526 | {
|
---|
527 | case 6:
|
---|
528 | uc = (puch[5] & 0x3f)
|
---|
529 | | ((RTUNICP)(puch[4] & 0x3f) << 6)
|
---|
530 | | ((RTUNICP)(puch[3] & 0x3f) << 12)
|
---|
531 | | ((RTUNICP)(puch[2] & 0x3f) << 18)
|
---|
532 | | ((RTUNICP)(puch[1] & 0x3f) << 24)
|
---|
533 | | ((RTUNICP)(uch & 0x01) << 30);
|
---|
534 | RTStrAssertMsgReturn(uc >= 0x04000000 && uc <= 0x7fffffff,
|
---|
535 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
536 | RTStrAssertMsgFailed(("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch));
|
---|
537 | return VERR_CANT_RECODE_AS_UTF16;
|
---|
538 | case 5:
|
---|
539 | uc = (puch[4] & 0x3f)
|
---|
540 | | ((RTUNICP)(puch[3] & 0x3f) << 6)
|
---|
541 | | ((RTUNICP)(puch[2] & 0x3f) << 12)
|
---|
542 | | ((RTUNICP)(puch[1] & 0x3f) << 18)
|
---|
543 | | ((RTUNICP)(uch & 0x03) << 24);
|
---|
544 | RTStrAssertMsgReturn(uc >= 0x00200000 && uc <= 0x03ffffff,
|
---|
545 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
546 | RTStrAssertMsgFailed(("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch));
|
---|
547 | return VERR_CANT_RECODE_AS_UTF16;
|
---|
548 | case 4:
|
---|
549 | uc = (puch[3] & 0x3f)
|
---|
550 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
551 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
552 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
553 | RTStrAssertMsgReturn(uc >= 0x00010000 && uc <= 0x001fffff,
|
---|
554 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
555 | RTStrAssertMsgReturn(uc <= 0x0010ffff,
|
---|
556 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_CANT_RECODE_AS_UTF16);
|
---|
557 | cwc++;
|
---|
558 | break;
|
---|
559 | case 3:
|
---|
560 | uc = (puch[2] & 0x3f)
|
---|
561 | | ((RTUNICP)(puch[1] & 0x3f) << 6)
|
---|
562 | | ((RTUNICP)(uch & 0x0f) << 12);
|
---|
563 | RTStrAssertMsgReturn(uc >= 0x00000800 && uc <= 0x0000fffd,
|
---|
564 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch),
|
---|
565 | uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING);
|
---|
566 | RTStrAssertMsgReturn(uc < 0xd800 || uc > 0xdfff,
|
---|
567 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_CODE_POINT_SURROGATE);
|
---|
568 | break;
|
---|
569 | case 2:
|
---|
570 | uc = (puch[1] & 0x3f)
|
---|
571 | | ((RTUNICP)(uch & 0x1f) << 6);
|
---|
572 | RTStrAssertMsgReturn(uc >= 0x00000080 && uc <= 0x000007ff,
|
---|
573 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, cch), puch), VERR_INVALID_UTF8_ENCODING);
|
---|
574 | break;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* advance */
|
---|
578 | cch -= cb;
|
---|
579 | puch += cb;
|
---|
580 | cwc++;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | /* done */
|
---|
585 | *pcwc = cwc;
|
---|
586 | return VINF_SUCCESS;
|
---|
587 | }
|
---|
588 |
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Recodes a valid UTF-8 string as UTF-16.
|
---|
592 | *
|
---|
593 | * Since we know the input is valid, we do *not* perform encoding or length checks.
|
---|
594 | *
|
---|
595 | * @returns iprt status code.
|
---|
596 | * @param psz The UTF-8 string to recode. This is a valid encoding.
|
---|
597 | * @param cch The number of chars (the type char, so bytes if you like) to process of the UTF-8 string.
|
---|
598 | * The recoding will stop when cch or '\\0' is reached. Pass RTSTR_MAX to process up to '\\0'.
|
---|
599 | * @param pwsz Where to store the UTF-16 string.
|
---|
600 | * @param cwc The number of RTUTF16 items the pwsz buffer can hold, excluding the terminator ('\\0').
|
---|
601 | */
|
---|
602 | static int rtUtf8RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc)
|
---|
603 | {
|
---|
604 | int rc = VINF_SUCCESS;
|
---|
605 | const unsigned char *puch = (const unsigned char *)psz;
|
---|
606 | PRTUTF16 pwc = pwsz;
|
---|
607 | while (cch > 0)
|
---|
608 | {
|
---|
609 | /* read the next char and check for terminator. */
|
---|
610 | const unsigned char uch = *puch;
|
---|
611 | if (!uch)
|
---|
612 | break;
|
---|
613 |
|
---|
614 | /* check for output overflow */
|
---|
615 | if (RT_UNLIKELY(cwc < 1))
|
---|
616 | {
|
---|
617 | rc = VERR_BUFFER_OVERFLOW;
|
---|
618 | break;
|
---|
619 | }
|
---|
620 | cwc--;
|
---|
621 |
|
---|
622 | /* decode and recode the code point */
|
---|
623 | if (!(uch & RT_BIT(7)))
|
---|
624 | {
|
---|
625 | *pwc++ = uch;
|
---|
626 | puch++;
|
---|
627 | cch--;
|
---|
628 | }
|
---|
629 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5))) == (RT_BIT(7) | RT_BIT(6)))
|
---|
630 | {
|
---|
631 | uint16_t uc = (puch[1] & 0x3f)
|
---|
632 | | ((uint16_t)(uch & 0x1f) << 6);
|
---|
633 | *pwc++ = uc;
|
---|
634 | puch += 2;
|
---|
635 | cch -= 2;
|
---|
636 | }
|
---|
637 | else if ((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5)))
|
---|
638 | {
|
---|
639 | uint16_t uc = (puch[2] & 0x3f)
|
---|
640 | | ((uint16_t)(puch[1] & 0x3f) << 6)
|
---|
641 | | ((uint16_t)(uch & 0x0f) << 12);
|
---|
642 | *pwc++ = uc;
|
---|
643 | puch += 3;
|
---|
644 | cch -= 3;
|
---|
645 | }
|
---|
646 | else
|
---|
647 | {
|
---|
648 | /* generate surrogate pair */
|
---|
649 | Assert((uch & (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4) | RT_BIT(3))) == (RT_BIT(7) | RT_BIT(6) | RT_BIT(5) | RT_BIT(4)));
|
---|
650 | RTUNICP uc = (puch[3] & 0x3f)
|
---|
651 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
652 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
653 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
654 | if (RT_UNLIKELY(cwc < 1))
|
---|
655 | {
|
---|
656 | rc = VERR_BUFFER_OVERFLOW;
|
---|
657 | break;
|
---|
658 | }
|
---|
659 | cwc--;
|
---|
660 |
|
---|
661 | uc -= 0x10000;
|
---|
662 | *pwc++ = 0xd800 | (uc >> 10);
|
---|
663 | *pwc++ = 0xdc00 | (uc & 0x3ff);
|
---|
664 | puch += 4;
|
---|
665 | cch -= 4;
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | /* done */
|
---|
670 | *pwc = '\0';
|
---|
671 | return rc;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag)
|
---|
676 | {
|
---|
677 | /*
|
---|
678 | * Validate input.
|
---|
679 | */
|
---|
680 | Assert(VALID_PTR(ppwszString));
|
---|
681 | Assert(VALID_PTR(pszString));
|
---|
682 | *ppwszString = NULL;
|
---|
683 |
|
---|
684 | /*
|
---|
685 | * Validate the UTF-8 input and calculate the length of the UTF-16 string.
|
---|
686 | */
|
---|
687 | size_t cwc;
|
---|
688 | int rc = rtUtf8CalcUtf16Length(pszString, RTSTR_MAX, &cwc);
|
---|
689 | if (RT_SUCCESS(rc))
|
---|
690 | {
|
---|
691 | /*
|
---|
692 | * Allocate buffer.
|
---|
693 | */
|
---|
694 | PRTUTF16 pwsz = (PRTUTF16)RTMemAllocTag((cwc + 1) * sizeof(RTUTF16), pszTag);
|
---|
695 | if (pwsz)
|
---|
696 | {
|
---|
697 | /*
|
---|
698 | * Encode the UTF-16 string.
|
---|
699 | */
|
---|
700 | rc = rtUtf8RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc);
|
---|
701 | if (RT_SUCCESS(rc))
|
---|
702 | {
|
---|
703 | *ppwszString = pwsz;
|
---|
704 | return rc;
|
---|
705 | }
|
---|
706 | RTMemFree(pwsz);
|
---|
707 | }
|
---|
708 | else
|
---|
709 | rc = VERR_NO_UTF16_MEMORY;
|
---|
710 | }
|
---|
711 | return rc;
|
---|
712 | }
|
---|
713 | RT_EXPORT_SYMBOL(RTStrToUtf16Tag);
|
---|
714 |
|
---|
715 |
|
---|
716 | RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString,
|
---|
717 | PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag)
|
---|
718 | {
|
---|
719 | /*
|
---|
720 | * Validate input.
|
---|
721 | */
|
---|
722 | Assert(VALID_PTR(pszString));
|
---|
723 | Assert(VALID_PTR(ppwsz));
|
---|
724 | Assert(!pcwc || VALID_PTR(pcwc));
|
---|
725 |
|
---|
726 | /*
|
---|
727 | * Validate the UTF-8 input and calculate the length of the UTF-16 string.
|
---|
728 | */
|
---|
729 | size_t cwcResult;
|
---|
730 | int rc = rtUtf8CalcUtf16Length(pszString, cchString, &cwcResult);
|
---|
731 | if (RT_SUCCESS(rc))
|
---|
732 | {
|
---|
733 | if (pcwc)
|
---|
734 | *pcwc = cwcResult;
|
---|
735 |
|
---|
736 | /*
|
---|
737 | * Check buffer size / Allocate buffer.
|
---|
738 | */
|
---|
739 | bool fShouldFree;
|
---|
740 | PRTUTF16 pwszResult;
|
---|
741 | if (cwc > 0 && *ppwsz)
|
---|
742 | {
|
---|
743 | fShouldFree = false;
|
---|
744 | if (cwc <= cwcResult)
|
---|
745 | return VERR_BUFFER_OVERFLOW;
|
---|
746 | pwszResult = *ppwsz;
|
---|
747 | }
|
---|
748 | else
|
---|
749 | {
|
---|
750 | *ppwsz = NULL;
|
---|
751 | fShouldFree = true;
|
---|
752 | cwc = RT_MAX(cwcResult + 1, cwc);
|
---|
753 | pwszResult = (PRTUTF16)RTMemAllocTag(cwc * sizeof(RTUTF16), pszTag);
|
---|
754 | }
|
---|
755 | if (pwszResult)
|
---|
756 | {
|
---|
757 | /*
|
---|
758 | * Encode the UTF-16 string.
|
---|
759 | */
|
---|
760 | rc = rtUtf8RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1);
|
---|
761 | if (RT_SUCCESS(rc))
|
---|
762 | {
|
---|
763 | *ppwsz = pwszResult;
|
---|
764 | return rc;
|
---|
765 | }
|
---|
766 | if (fShouldFree)
|
---|
767 | RTMemFree(pwszResult);
|
---|
768 | }
|
---|
769 | else
|
---|
770 | rc = VERR_NO_UTF16_MEMORY;
|
---|
771 | }
|
---|
772 | return rc;
|
---|
773 | }
|
---|
774 | RT_EXPORT_SYMBOL(RTStrToUtf16ExTag);
|
---|
775 |
|
---|
776 |
|
---|
777 | RTDECL(size_t) RTStrCalcUtf16Len(const char *psz)
|
---|
778 | {
|
---|
779 | size_t cwc;
|
---|
780 | int rc = rtUtf8CalcUtf16Length(psz, RTSTR_MAX, &cwc);
|
---|
781 | return RT_SUCCESS(rc) ? cwc : 0;
|
---|
782 | }
|
---|
783 | RT_EXPORT_SYMBOL(RTStrCalcUtf16Len);
|
---|
784 |
|
---|
785 |
|
---|
786 | RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc)
|
---|
787 | {
|
---|
788 | size_t cwc;
|
---|
789 | int rc = rtUtf8CalcUtf16Length(psz, cch, &cwc);
|
---|
790 | if (pcwc)
|
---|
791 | *pcwc = RT_SUCCESS(rc) ? cwc : ~(size_t)0;
|
---|
792 | return rc;
|
---|
793 | }
|
---|
794 | RT_EXPORT_SYMBOL(RTStrCalcUtf16LenEx);
|
---|
795 |
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Calculates the length of the UTF-8 encoding of a Latin-1 string.
|
---|
799 | *
|
---|
800 | * @returns iprt status code.
|
---|
801 | * @param psz The Latin-1 string.
|
---|
802 | * @param cchIn The max length of the Latin-1 string to consider.
|
---|
803 | * @param pcch Where to store the length (excluding '\\0') of the UTF-8 string. (cch == cb, btw)
|
---|
804 | */
|
---|
805 | static int rtLatin1CalcUtf8Length(const char *psz, size_t cchIn, size_t *pcch)
|
---|
806 | {
|
---|
807 | size_t cch = 0;
|
---|
808 | for (;;)
|
---|
809 | {
|
---|
810 | RTUNICP Cp;
|
---|
811 | int rc = RTLatin1GetCpNEx(&psz, &cchIn, &Cp);
|
---|
812 | if (Cp == 0 || rc == VERR_END_OF_STRING)
|
---|
813 | break;
|
---|
814 | if (RT_FAILURE(rc))
|
---|
815 | return rc;
|
---|
816 | cch += RTStrCpSize(Cp); /* cannot fail */
|
---|
817 | }
|
---|
818 |
|
---|
819 | /* done */
|
---|
820 | *pcch = cch;
|
---|
821 | return VINF_SUCCESS;
|
---|
822 | }
|
---|
823 |
|
---|
824 |
|
---|
825 | /**
|
---|
826 | * Recodes a Latin-1 string as UTF-8.
|
---|
827 | *
|
---|
828 | * @returns iprt status code.
|
---|
829 | * @param psz The Latin-1 string.
|
---|
830 | * @param cchIn The number of characters to process from psz. The recoding
|
---|
831 | * will stop when cch or '\\0' is reached.
|
---|
832 | * @param psz Where to store the UTF-8 string.
|
---|
833 | * @param cch The size of the UTF-8 buffer, excluding the terminator.
|
---|
834 | */
|
---|
835 | static int rtLatin1RecodeAsUtf8(const char *pszIn, size_t cchIn, char *psz, size_t cch)
|
---|
836 | {
|
---|
837 | int rc;
|
---|
838 | for (;;)
|
---|
839 | {
|
---|
840 | RTUNICP Cp;
|
---|
841 | size_t cchCp;
|
---|
842 | rc = RTLatin1GetCpNEx(&pszIn, &cchIn, &Cp);
|
---|
843 | if (Cp == 0 || RT_FAILURE(rc))
|
---|
844 | break;
|
---|
845 | cchCp = RTStrCpSize(Cp);
|
---|
846 | if (RT_UNLIKELY(cch < cchCp))
|
---|
847 | {
|
---|
848 | RTStrAssertMsgFailed(("Buffer overflow! 1\n"));
|
---|
849 | rc = VERR_BUFFER_OVERFLOW;
|
---|
850 | break;
|
---|
851 | }
|
---|
852 | cch -= cchCp;
|
---|
853 | psz = RTStrPutCp(psz, Cp);
|
---|
854 | }
|
---|
855 |
|
---|
856 | /* done */
|
---|
857 | if (rc == VERR_END_OF_STRING)
|
---|
858 | rc = VINF_SUCCESS;
|
---|
859 | *psz = '\0';
|
---|
860 | return rc;
|
---|
861 | }
|
---|
862 |
|
---|
863 |
|
---|
864 |
|
---|
865 | RTDECL(int) RTLatin1ToUtf8Tag(const char *pszString, char **ppszString, const char *pszTag)
|
---|
866 | {
|
---|
867 | /*
|
---|
868 | * Validate input.
|
---|
869 | */
|
---|
870 | Assert(VALID_PTR(ppszString));
|
---|
871 | Assert(VALID_PTR(pszString));
|
---|
872 | *ppszString = NULL;
|
---|
873 |
|
---|
874 | /*
|
---|
875 | * Calculate the length of the UTF-8 encoding of the Latin-1 string.
|
---|
876 | */
|
---|
877 | size_t cch;
|
---|
878 | int rc = rtLatin1CalcUtf8Length(pszString, RTSTR_MAX, &cch);
|
---|
879 | if (RT_SUCCESS(rc))
|
---|
880 | {
|
---|
881 | /*
|
---|
882 | * Allocate buffer and recode it.
|
---|
883 | */
|
---|
884 | char *pszResult = (char *)RTMemAllocTag(cch + 1, pszTag);
|
---|
885 | if (pszResult)
|
---|
886 | {
|
---|
887 | rc = rtLatin1RecodeAsUtf8(pszString, RTSTR_MAX, pszResult, cch);
|
---|
888 | if (RT_SUCCESS(rc))
|
---|
889 | {
|
---|
890 | *ppszString = pszResult;
|
---|
891 | return rc;
|
---|
892 | }
|
---|
893 |
|
---|
894 | RTMemFree(pszResult);
|
---|
895 | }
|
---|
896 | else
|
---|
897 | rc = VERR_NO_STR_MEMORY;
|
---|
898 | }
|
---|
899 | return rc;
|
---|
900 | }
|
---|
901 | RT_EXPORT_SYMBOL(RTLatin1ToUtf8Tag);
|
---|
902 |
|
---|
903 |
|
---|
904 | RTDECL(int) RTLatin1ToUtf8ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag)
|
---|
905 | {
|
---|
906 | /*
|
---|
907 | * Validate input.
|
---|
908 | */
|
---|
909 | Assert(VALID_PTR(pszString));
|
---|
910 | Assert(VALID_PTR(ppsz));
|
---|
911 | Assert(!pcch || VALID_PTR(pcch));
|
---|
912 |
|
---|
913 | /*
|
---|
914 | * Calculate the length of the UTF-8 encoding of the Latin-1 string.
|
---|
915 | */
|
---|
916 | size_t cchResult;
|
---|
917 | int rc = rtLatin1CalcUtf8Length(pszString, cchString, &cchResult);
|
---|
918 | if (RT_SUCCESS(rc))
|
---|
919 | {
|
---|
920 | if (pcch)
|
---|
921 | *pcch = cchResult;
|
---|
922 |
|
---|
923 | /*
|
---|
924 | * Check buffer size / Allocate buffer and recode it.
|
---|
925 | */
|
---|
926 | bool fShouldFree;
|
---|
927 | char *pszResult;
|
---|
928 | if (cch > 0 && *ppsz)
|
---|
929 | {
|
---|
930 | fShouldFree = false;
|
---|
931 | if (RT_UNLIKELY(cch <= cchResult))
|
---|
932 | return VERR_BUFFER_OVERFLOW;
|
---|
933 | pszResult = *ppsz;
|
---|
934 | }
|
---|
935 | else
|
---|
936 | {
|
---|
937 | *ppsz = NULL;
|
---|
938 | fShouldFree = true;
|
---|
939 | cch = RT_MAX(cch, cchResult + 1);
|
---|
940 | pszResult = (char *)RTStrAllocTag(cch, pszTag);
|
---|
941 | }
|
---|
942 | if (pszResult)
|
---|
943 | {
|
---|
944 | rc = rtLatin1RecodeAsUtf8(pszString, cchString, pszResult, cch - 1);
|
---|
945 | if (RT_SUCCESS(rc))
|
---|
946 | {
|
---|
947 | *ppsz = pszResult;
|
---|
948 | return rc;
|
---|
949 | }
|
---|
950 |
|
---|
951 | if (fShouldFree)
|
---|
952 | RTStrFree(pszResult);
|
---|
953 | }
|
---|
954 | else
|
---|
955 | rc = VERR_NO_STR_MEMORY;
|
---|
956 | }
|
---|
957 | return rc;
|
---|
958 | }
|
---|
959 | RT_EXPORT_SYMBOL(RTLatin1ToUtf8ExTag);
|
---|
960 |
|
---|
961 |
|
---|
962 | RTDECL(size_t) RTLatin1CalcUtf8Len(const char *psz)
|
---|
963 | {
|
---|
964 | size_t cch;
|
---|
965 | int rc = rtLatin1CalcUtf8Length(psz, RTSTR_MAX, &cch);
|
---|
966 | return RT_SUCCESS(rc) ? cch : 0;
|
---|
967 | }
|
---|
968 | RT_EXPORT_SYMBOL(RTLatin1CalcUtf8Len);
|
---|
969 |
|
---|
970 |
|
---|
971 | RTDECL(int) RTLatin1CalcUtf8LenEx(const char *psz, size_t cchIn, size_t *pcch)
|
---|
972 | {
|
---|
973 | size_t cch;
|
---|
974 | int rc = rtLatin1CalcUtf8Length(psz, cchIn, &cch);
|
---|
975 | if (pcch)
|
---|
976 | *pcch = RT_SUCCESS(rc) ? cch : ~(size_t)0;
|
---|
977 | return rc;
|
---|
978 | }
|
---|
979 | RT_EXPORT_SYMBOL(RTLatin1CalcUtf8LenEx);
|
---|
980 |
|
---|
981 |
|
---|
982 | /**
|
---|
983 | * Calculates the Latin-1 length of a string, validating the encoding while
|
---|
984 | * doing so.
|
---|
985 | *
|
---|
986 | * @returns IPRT status code.
|
---|
987 | * @param psz Pointer to the UTF-8 string.
|
---|
988 | * @param cchIn The max length of the string. (btw cch = cb)
|
---|
989 | * Use RTSTR_MAX if all of the string is to be examined.
|
---|
990 | * @param pcch Where to store the length of the Latin-1 string in bytes.
|
---|
991 | */
|
---|
992 | static int rtUtf8CalcLatin1Length(const char *psz, size_t cchIn, size_t *pcch)
|
---|
993 | {
|
---|
994 | size_t cch = 0;
|
---|
995 | for (;;)
|
---|
996 | {
|
---|
997 | RTUNICP Cp;
|
---|
998 | size_t cchCp;
|
---|
999 | int rc = RTStrGetCpNEx(&psz, &cchIn, &Cp);
|
---|
1000 | if (Cp == 0 || rc == VERR_END_OF_STRING)
|
---|
1001 | break;
|
---|
1002 | if (RT_FAILURE(rc))
|
---|
1003 | return rc;
|
---|
1004 | cchCp = RTLatin1CpSize(Cp);
|
---|
1005 | if (cchCp == 0)
|
---|
1006 | return VERR_NO_TRANSLATION;
|
---|
1007 | cch += cchCp;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /* done */
|
---|
1011 | *pcch = cch;
|
---|
1012 | return VINF_SUCCESS;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | /**
|
---|
1017 | * Recodes a valid UTF-8 string as Latin-1.
|
---|
1018 | *
|
---|
1019 | * Since we know the input is valid, we do *not* perform encoding or length checks.
|
---|
1020 | *
|
---|
1021 | * @returns iprt status code.
|
---|
1022 | * @param pszIn The UTF-8 string to recode. This is a valid encoding.
|
---|
1023 | * @param cchIn The number of chars (the type char, so bytes if you like) to process of the UTF-8 string.
|
---|
1024 | * The recoding will stop when cch or '\\0' is reached. Pass RTSTR_MAX to process up to '\\0'.
|
---|
1025 | * @param psz Where to store the Latin-1 string.
|
---|
1026 | * @param cch The number of characters the pszOut buffer can hold, excluding the terminator ('\\0').
|
---|
1027 | */
|
---|
1028 | static int rtUtf8RecodeAsLatin1(const char *pszIn, size_t cchIn, char *psz, size_t cch)
|
---|
1029 | {
|
---|
1030 | int rc;
|
---|
1031 | for (;;)
|
---|
1032 | {
|
---|
1033 | RTUNICP Cp;
|
---|
1034 | size_t cchCp;
|
---|
1035 | rc = RTStrGetCpNEx(&pszIn, &cchIn, &Cp);
|
---|
1036 | if (Cp == 0 || RT_FAILURE(rc))
|
---|
1037 | break;
|
---|
1038 | cchCp = RTLatin1CpSize(Cp);
|
---|
1039 | if (RT_UNLIKELY(cch < cchCp))
|
---|
1040 | {
|
---|
1041 | RTStrAssertMsgFailed(("Buffer overflow! 1\n"));
|
---|
1042 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1043 | break;
|
---|
1044 | }
|
---|
1045 | cch -= cchCp;
|
---|
1046 | psz = RTLatin1PutCp(psz, Cp);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | /* done */
|
---|
1050 | if (rc == VERR_END_OF_STRING)
|
---|
1051 | rc = VINF_SUCCESS;
|
---|
1052 | *psz = '\0';
|
---|
1053 | return rc;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 |
|
---|
1057 |
|
---|
1058 | RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag)
|
---|
1059 | {
|
---|
1060 | /*
|
---|
1061 | * Validate input.
|
---|
1062 | */
|
---|
1063 | Assert(VALID_PTR(ppszString));
|
---|
1064 | Assert(VALID_PTR(pszString));
|
---|
1065 | *ppszString = NULL;
|
---|
1066 |
|
---|
1067 | /*
|
---|
1068 | * Validate the UTF-8 input and calculate the length of the Latin-1 string.
|
---|
1069 | */
|
---|
1070 | size_t cch;
|
---|
1071 | int rc = rtUtf8CalcLatin1Length(pszString, RTSTR_MAX, &cch);
|
---|
1072 | if (RT_SUCCESS(rc))
|
---|
1073 | {
|
---|
1074 | /*
|
---|
1075 | * Allocate buffer.
|
---|
1076 | */
|
---|
1077 | char *psz = (char *)RTMemAllocTag(cch + 1, pszTag);
|
---|
1078 | if (psz)
|
---|
1079 | {
|
---|
1080 | /*
|
---|
1081 | * Encode the UTF-16 string.
|
---|
1082 | */
|
---|
1083 | rc = rtUtf8RecodeAsLatin1(pszString, RTSTR_MAX, psz, cch);
|
---|
1084 | if (RT_SUCCESS(rc))
|
---|
1085 | {
|
---|
1086 | *ppszString = psz;
|
---|
1087 | return rc;
|
---|
1088 | }
|
---|
1089 | RTMemFree(psz);
|
---|
1090 | }
|
---|
1091 | else
|
---|
1092 | rc = VERR_NO_STR_MEMORY;
|
---|
1093 | }
|
---|
1094 | return rc;
|
---|
1095 | }
|
---|
1096 | RT_EXPORT_SYMBOL(RTStrToLatin1Tag);
|
---|
1097 |
|
---|
1098 |
|
---|
1099 | RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString,
|
---|
1100 | char **ppsz, size_t cch, size_t *pcch, const char *pszTag)
|
---|
1101 | {
|
---|
1102 | /*
|
---|
1103 | * Validate input.
|
---|
1104 | */
|
---|
1105 | Assert(VALID_PTR(pszString));
|
---|
1106 | Assert(VALID_PTR(ppsz));
|
---|
1107 | Assert(!pcch || VALID_PTR(pcch));
|
---|
1108 |
|
---|
1109 | /*
|
---|
1110 | * Validate the UTF-8 input and calculate the length of the UTF-16 string.
|
---|
1111 | */
|
---|
1112 | size_t cchResult;
|
---|
1113 | int rc = rtUtf8CalcLatin1Length(pszString, cchString, &cchResult);
|
---|
1114 | if (RT_SUCCESS(rc))
|
---|
1115 | {
|
---|
1116 | if (pcch)
|
---|
1117 | *pcch = cchResult;
|
---|
1118 |
|
---|
1119 | /*
|
---|
1120 | * Check buffer size / Allocate buffer.
|
---|
1121 | */
|
---|
1122 | bool fShouldFree;
|
---|
1123 | char *pszResult;
|
---|
1124 | if (cch > 0 && *ppsz)
|
---|
1125 | {
|
---|
1126 | fShouldFree = false;
|
---|
1127 | if (cch <= cchResult)
|
---|
1128 | return VERR_BUFFER_OVERFLOW;
|
---|
1129 | pszResult = *ppsz;
|
---|
1130 | }
|
---|
1131 | else
|
---|
1132 | {
|
---|
1133 | *ppsz = NULL;
|
---|
1134 | fShouldFree = true;
|
---|
1135 | cch = RT_MAX(cchResult + 1, cch);
|
---|
1136 | pszResult = (char *)RTMemAllocTag(cch, pszTag);
|
---|
1137 | }
|
---|
1138 | if (pszResult)
|
---|
1139 | {
|
---|
1140 | /*
|
---|
1141 | * Encode the Latin-1 string.
|
---|
1142 | */
|
---|
1143 | rc = rtUtf8RecodeAsLatin1(pszString, cchString, pszResult, cch - 1);
|
---|
1144 | if (RT_SUCCESS(rc))
|
---|
1145 | {
|
---|
1146 | *ppsz = pszResult;
|
---|
1147 | return rc;
|
---|
1148 | }
|
---|
1149 | if (fShouldFree)
|
---|
1150 | RTMemFree(pszResult);
|
---|
1151 | }
|
---|
1152 | else
|
---|
1153 | rc = VERR_NO_STR_MEMORY;
|
---|
1154 | }
|
---|
1155 | return rc;
|
---|
1156 | }
|
---|
1157 | RT_EXPORT_SYMBOL(RTStrToLatin1Tag);
|
---|
1158 |
|
---|
1159 |
|
---|
1160 | RTDECL(size_t) RTStrCalcLatin1Len(const char *psz)
|
---|
1161 | {
|
---|
1162 | size_t cch;
|
---|
1163 | int rc = rtUtf8CalcLatin1Length(psz, RTSTR_MAX, &cch);
|
---|
1164 | return RT_SUCCESS(rc) ? cch : 0;
|
---|
1165 | }
|
---|
1166 | RT_EXPORT_SYMBOL(RTStrCalcLatin1Len);
|
---|
1167 |
|
---|
1168 |
|
---|
1169 | RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cchIn, size_t *pcch)
|
---|
1170 | {
|
---|
1171 | size_t cch;
|
---|
1172 | int rc = rtUtf8CalcLatin1Length(psz, cchIn, &cch);
|
---|
1173 | if (pcch)
|
---|
1174 | *pcch = RT_SUCCESS(rc) ? cch : ~(size_t)0;
|
---|
1175 | return rc;
|
---|
1176 | }
|
---|
1177 | RT_EXPORT_SYMBOL(RTStrCalcLatin1LenEx);
|
---|
1178 |
|
---|
1179 |
|
---|
1180 | /**
|
---|
1181 | * Handle invalid encodings passed to RTStrGetCp() and RTStrGetCpEx().
|
---|
1182 | * @returns rc
|
---|
1183 | * @param ppsz The pointer to the string position point.
|
---|
1184 | * @param pCp Where to store RTUNICP_INVALID.
|
---|
1185 | * @param rc The iprt error code.
|
---|
1186 | */
|
---|
1187 | static int rtStrGetCpExFailure(const char **ppsz, PRTUNICP pCp, int rc)
|
---|
1188 | {
|
---|
1189 | /*
|
---|
1190 | * Try find a valid encoding.
|
---|
1191 | */
|
---|
1192 | (*ppsz)++; /** @todo code this! */
|
---|
1193 | *pCp = RTUNICP_INVALID;
|
---|
1194 | return rc;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz)
|
---|
1199 | {
|
---|
1200 | RTUNICP Cp;
|
---|
1201 | RTStrGetCpExInternal(&psz, &Cp);
|
---|
1202 | return Cp;
|
---|
1203 | }
|
---|
1204 | RT_EXPORT_SYMBOL(RTStrGetCpInternal);
|
---|
1205 |
|
---|
1206 |
|
---|
1207 | RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp)
|
---|
1208 | {
|
---|
1209 | const unsigned char *puch = (const unsigned char *)*ppsz;
|
---|
1210 | const unsigned char uch = *puch;
|
---|
1211 | RTUNICP uc;
|
---|
1212 |
|
---|
1213 | /* ASCII ? */
|
---|
1214 | if (!(uch & RT_BIT(7)))
|
---|
1215 | {
|
---|
1216 | uc = uch;
|
---|
1217 | puch++;
|
---|
1218 | }
|
---|
1219 | else if (uch & RT_BIT(6))
|
---|
1220 | {
|
---|
1221 | /* figure the length and validate the first octet. */
|
---|
1222 | /** @todo RT_USE_RTC_3629 */
|
---|
1223 | unsigned cb;
|
---|
1224 | if (!(uch & RT_BIT(5)))
|
---|
1225 | cb = 2;
|
---|
1226 | else if (!(uch & RT_BIT(4)))
|
---|
1227 | cb = 3;
|
---|
1228 | else if (!(uch & RT_BIT(3)))
|
---|
1229 | cb = 4;
|
---|
1230 | else if (!(uch & RT_BIT(2)))
|
---|
1231 | cb = 5;
|
---|
1232 | else if (!(uch & RT_BIT(1)))
|
---|
1233 | cb = 6;
|
---|
1234 | else
|
---|
1235 | {
|
---|
1236 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
|
---|
1237 | return rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | /* validate the rest */
|
---|
1241 | switch (cb)
|
---|
1242 | {
|
---|
1243 | case 6:
|
---|
1244 | RTStrAssertMsgReturn((puch[5] & 0xc0) == 0x80, ("6/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1245 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1246 | case 5:
|
---|
1247 | RTStrAssertMsgReturn((puch[4] & 0xc0) == 0x80, ("5/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1248 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1249 | case 4:
|
---|
1250 | RTStrAssertMsgReturn((puch[3] & 0xc0) == 0x80, ("4/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1251 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1252 | case 3:
|
---|
1253 | RTStrAssertMsgReturn((puch[2] & 0xc0) == 0x80, ("3/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1254 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1255 | case 2:
|
---|
1256 | RTStrAssertMsgReturn((puch[1] & 0xc0) == 0x80, ("2/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1257 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1258 | break;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | /* get and validate the code point. */
|
---|
1262 | switch (cb)
|
---|
1263 | {
|
---|
1264 | case 6:
|
---|
1265 | uc = (puch[5] & 0x3f)
|
---|
1266 | | ((RTUNICP)(puch[4] & 0x3f) << 6)
|
---|
1267 | | ((RTUNICP)(puch[3] & 0x3f) << 12)
|
---|
1268 | | ((RTUNICP)(puch[2] & 0x3f) << 18)
|
---|
1269 | | ((RTUNICP)(puch[1] & 0x3f) << 24)
|
---|
1270 | | ((RTUNICP)(uch & 0x01) << 30);
|
---|
1271 | RTStrAssertMsgReturn(uc >= 0x04000000 && uc <= 0x7fffffff,
|
---|
1272 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1273 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1274 | break;
|
---|
1275 | case 5:
|
---|
1276 | uc = (puch[4] & 0x3f)
|
---|
1277 | | ((RTUNICP)(puch[3] & 0x3f) << 6)
|
---|
1278 | | ((RTUNICP)(puch[2] & 0x3f) << 12)
|
---|
1279 | | ((RTUNICP)(puch[1] & 0x3f) << 18)
|
---|
1280 | | ((RTUNICP)(uch & 0x03) << 24);
|
---|
1281 | RTStrAssertMsgReturn(uc >= 0x00200000 && uc <= 0x03ffffff,
|
---|
1282 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1283 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1284 | break;
|
---|
1285 | case 4:
|
---|
1286 | uc = (puch[3] & 0x3f)
|
---|
1287 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
1288 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
1289 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
1290 | RTStrAssertMsgReturn(uc >= 0x00010000 && uc <= 0x001fffff,
|
---|
1291 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1292 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1293 | break;
|
---|
1294 | case 3:
|
---|
1295 | uc = (puch[2] & 0x3f)
|
---|
1296 | | ((RTUNICP)(puch[1] & 0x3f) << 6)
|
---|
1297 | | ((RTUNICP)(uch & 0x0f) << 12);
|
---|
1298 | RTStrAssertMsgReturn(uc >= 0x00000800 && uc <= 0x0000fffd,
|
---|
1299 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1300 | rtStrGetCpExFailure(ppsz, pCp, uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING));
|
---|
1301 | RTStrAssertMsgReturn(uc < 0xd800 || uc > 0xdfff,
|
---|
1302 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1303 | rtStrGetCpExFailure(ppsz, pCp, VERR_CODE_POINT_SURROGATE));
|
---|
1304 | break;
|
---|
1305 | case 2:
|
---|
1306 | uc = (puch[1] & 0x3f)
|
---|
1307 | | ((RTUNICP)(uch & 0x1f) << 6);
|
---|
1308 | RTStrAssertMsgReturn(uc >= 0x00000080 && uc <= 0x000007ff,
|
---|
1309 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1310 | rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1311 | break;
|
---|
1312 | default: /* impossible, but GCC is bitching. */
|
---|
1313 | uc = RTUNICP_INVALID;
|
---|
1314 | break;
|
---|
1315 | }
|
---|
1316 | puch += cb;
|
---|
1317 | }
|
---|
1318 | else
|
---|
1319 | {
|
---|
1320 | /* 6th bit is always set. */
|
---|
1321 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
|
---|
1322 | return rtStrGetCpExFailure(ppsz, pCp, VERR_INVALID_UTF8_ENCODING);
|
---|
1323 | }
|
---|
1324 | *pCp = uc;
|
---|
1325 | *ppsz = (const char *)puch;
|
---|
1326 | return VINF_SUCCESS;
|
---|
1327 | }
|
---|
1328 | RT_EXPORT_SYMBOL(RTStrGetCpExInternal);
|
---|
1329 |
|
---|
1330 |
|
---|
1331 | /**
|
---|
1332 | * Handle invalid encodings passed to RTStrGetCpNEx().
|
---|
1333 | * @returns rc
|
---|
1334 | * @param ppsz The pointer to the string position point.
|
---|
1335 | * @param pcch Pointer to the string length.
|
---|
1336 | * @param pCp Where to store RTUNICP_INVALID.
|
---|
1337 | * @param rc The iprt error code.
|
---|
1338 | */
|
---|
1339 | static int rtStrGetCpNExFailure(const char **ppsz, size_t *pcch, PRTUNICP pCp, int rc)
|
---|
1340 | {
|
---|
1341 | /*
|
---|
1342 | * Try find a valid encoding.
|
---|
1343 | */
|
---|
1344 | (*ppsz)++; /** @todo code this! */
|
---|
1345 | (*pcch)--;
|
---|
1346 | *pCp = RTUNICP_INVALID;
|
---|
1347 | return rc;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp)
|
---|
1352 | {
|
---|
1353 | const unsigned char *puch = (const unsigned char *)*ppsz;
|
---|
1354 | const unsigned char uch = *puch;
|
---|
1355 | size_t cch = *pcch;
|
---|
1356 | RTUNICP uc;
|
---|
1357 |
|
---|
1358 | if (cch == 0)
|
---|
1359 | {
|
---|
1360 | *pCp = RTUNICP_INVALID;
|
---|
1361 | return VERR_END_OF_STRING;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | /* ASCII ? */
|
---|
1365 | if (!(uch & RT_BIT(7)))
|
---|
1366 | {
|
---|
1367 | uc = uch;
|
---|
1368 | puch++;
|
---|
1369 | cch--;
|
---|
1370 | }
|
---|
1371 | else if (uch & RT_BIT(6))
|
---|
1372 | {
|
---|
1373 | /* figure the length and validate the first octet. */
|
---|
1374 | /** @todo RT_USE_RTC_3629 */
|
---|
1375 | unsigned cb;
|
---|
1376 | if (!(uch & RT_BIT(5)))
|
---|
1377 | cb = 2;
|
---|
1378 | else if (!(uch & RT_BIT(4)))
|
---|
1379 | cb = 3;
|
---|
1380 | else if (!(uch & RT_BIT(3)))
|
---|
1381 | cb = 4;
|
---|
1382 | else if (!(uch & RT_BIT(2)))
|
---|
1383 | cb = 5;
|
---|
1384 | else if (!(uch & RT_BIT(1)))
|
---|
1385 | cb = 6;
|
---|
1386 | else
|
---|
1387 | {
|
---|
1388 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
|
---|
1389 | return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | if (cb > cch)
|
---|
1393 | return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
|
---|
1394 |
|
---|
1395 | /* validate the rest */
|
---|
1396 | switch (cb)
|
---|
1397 | {
|
---|
1398 | case 6:
|
---|
1399 | RTStrAssertMsgReturn((puch[5] & 0xc0) == 0x80, ("6/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1400 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1401 | case 5:
|
---|
1402 | RTStrAssertMsgReturn((puch[4] & 0xc0) == 0x80, ("5/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1403 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1404 | case 4:
|
---|
1405 | RTStrAssertMsgReturn((puch[3] & 0xc0) == 0x80, ("4/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1406 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1407 | case 3:
|
---|
1408 | RTStrAssertMsgReturn((puch[2] & 0xc0) == 0x80, ("3/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1409 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1410 | case 2:
|
---|
1411 | RTStrAssertMsgReturn((puch[1] & 0xc0) == 0x80, ("2/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1412 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1413 | break;
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /* get and validate the code point. */
|
---|
1417 | switch (cb)
|
---|
1418 | {
|
---|
1419 | case 6:
|
---|
1420 | uc = (puch[5] & 0x3f)
|
---|
1421 | | ((RTUNICP)(puch[4] & 0x3f) << 6)
|
---|
1422 | | ((RTUNICP)(puch[3] & 0x3f) << 12)
|
---|
1423 | | ((RTUNICP)(puch[2] & 0x3f) << 18)
|
---|
1424 | | ((RTUNICP)(puch[1] & 0x3f) << 24)
|
---|
1425 | | ((RTUNICP)(uch & 0x01) << 30);
|
---|
1426 | RTStrAssertMsgReturn(uc >= 0x04000000 && uc <= 0x7fffffff,
|
---|
1427 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1428 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1429 | break;
|
---|
1430 | case 5:
|
---|
1431 | uc = (puch[4] & 0x3f)
|
---|
1432 | | ((RTUNICP)(puch[3] & 0x3f) << 6)
|
---|
1433 | | ((RTUNICP)(puch[2] & 0x3f) << 12)
|
---|
1434 | | ((RTUNICP)(puch[1] & 0x3f) << 18)
|
---|
1435 | | ((RTUNICP)(uch & 0x03) << 24);
|
---|
1436 | RTStrAssertMsgReturn(uc >= 0x00200000 && uc <= 0x03ffffff,
|
---|
1437 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1438 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1439 | break;
|
---|
1440 | case 4:
|
---|
1441 | uc = (puch[3] & 0x3f)
|
---|
1442 | | ((RTUNICP)(puch[2] & 0x3f) << 6)
|
---|
1443 | | ((RTUNICP)(puch[1] & 0x3f) << 12)
|
---|
1444 | | ((RTUNICP)(uch & 0x07) << 18);
|
---|
1445 | RTStrAssertMsgReturn(uc >= 0x00010000 && uc <= 0x001fffff,
|
---|
1446 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1447 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1448 | break;
|
---|
1449 | case 3:
|
---|
1450 | uc = (puch[2] & 0x3f)
|
---|
1451 | | ((RTUNICP)(puch[1] & 0x3f) << 6)
|
---|
1452 | | ((RTUNICP)(uch & 0x0f) << 12);
|
---|
1453 | RTStrAssertMsgReturn(uc >= 0x00000800 && uc <= 0x0000fffd,
|
---|
1454 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1455 | rtStrGetCpNExFailure(ppsz, pcch, pCp, uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING));
|
---|
1456 | RTStrAssertMsgReturn(uc < 0xd800 || uc > 0xdfff,
|
---|
1457 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1458 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_CODE_POINT_SURROGATE));
|
---|
1459 | break;
|
---|
1460 | case 2:
|
---|
1461 | uc = (puch[1] & 0x3f)
|
---|
1462 | | ((RTUNICP)(uch & 0x1f) << 6);
|
---|
1463 | RTStrAssertMsgReturn(uc >= 0x00000080 && uc <= 0x000007ff,
|
---|
1464 | ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
|
---|
1465 | rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
|
---|
1466 | break;
|
---|
1467 | default: /* impossible, but GCC is bitching. */
|
---|
1468 | uc = RTUNICP_INVALID;
|
---|
1469 | break;
|
---|
1470 | }
|
---|
1471 | puch += cb;
|
---|
1472 | cch -= cb;
|
---|
1473 | }
|
---|
1474 | else
|
---|
1475 | {
|
---|
1476 | /* 6th bit is always set. */
|
---|
1477 | RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
|
---|
1478 | return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
|
---|
1479 | }
|
---|
1480 | *pCp = uc;
|
---|
1481 | *ppsz = (const char *)puch;
|
---|
1482 | (*pcch) = cch;
|
---|
1483 | return VINF_SUCCESS;
|
---|
1484 | }
|
---|
1485 | RT_EXPORT_SYMBOL(RTStrGetCpNExInternal);
|
---|
1486 |
|
---|
1487 |
|
---|
1488 | RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP uc)
|
---|
1489 | {
|
---|
1490 | unsigned char *puch = (unsigned char *)psz;
|
---|
1491 | if (uc < 0x80)
|
---|
1492 | *puch++ = (unsigned char )uc;
|
---|
1493 | else if (uc < 0x00000800)
|
---|
1494 | {
|
---|
1495 | *puch++ = 0xc0 | (uc >> 6);
|
---|
1496 | *puch++ = 0x80 | (uc & 0x3f);
|
---|
1497 | }
|
---|
1498 | else if (uc < 0x00010000)
|
---|
1499 | {
|
---|
1500 | /** @todo RT_USE_RTC_3629 */
|
---|
1501 | if ( uc < 0x0000d8000
|
---|
1502 | || ( uc > 0x0000dfff
|
---|
1503 | && uc < 0x0000fffe))
|
---|
1504 | {
|
---|
1505 | *puch++ = 0xe0 | (uc >> 12);
|
---|
1506 | *puch++ = 0x80 | ((uc >> 6) & 0x3f);
|
---|
1507 | *puch++ = 0x80 | (uc & 0x3f);
|
---|
1508 | }
|
---|
1509 | else
|
---|
1510 | {
|
---|
1511 | AssertMsgFailed(("Invalid code point U+%05x!\n", uc));
|
---|
1512 | *puch++ = 0x7f;
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 | /** @todo RT_USE_RTC_3629 */
|
---|
1516 | else if (uc < 0x00200000)
|
---|
1517 | {
|
---|
1518 | *puch++ = 0xf0 | (uc >> 18);
|
---|
1519 | *puch++ = 0x80 | ((uc >> 12) & 0x3f);
|
---|
1520 | *puch++ = 0x80 | ((uc >> 6) & 0x3f);
|
---|
1521 | *puch++ = 0x80 | (uc & 0x3f);
|
---|
1522 | }
|
---|
1523 | else if (uc < 0x04000000)
|
---|
1524 | {
|
---|
1525 | *puch++ = 0xf8 | (uc >> 24);
|
---|
1526 | *puch++ = 0x80 | ((uc >> 18) & 0x3f);
|
---|
1527 | *puch++ = 0x80 | ((uc >> 12) & 0x3f);
|
---|
1528 | *puch++ = 0x80 | ((uc >> 6) & 0x3f);
|
---|
1529 | *puch++ = 0x80 | (uc & 0x3f);
|
---|
1530 | }
|
---|
1531 | else if (uc <= 0x7fffffff)
|
---|
1532 | {
|
---|
1533 | *puch++ = 0xfc | (uc >> 30);
|
---|
1534 | *puch++ = 0x80 | ((uc >> 24) & 0x3f);
|
---|
1535 | *puch++ = 0x80 | ((uc >> 18) & 0x3f);
|
---|
1536 | *puch++ = 0x80 | ((uc >> 12) & 0x3f);
|
---|
1537 | *puch++ = 0x80 | ((uc >> 6) & 0x3f);
|
---|
1538 | *puch++ = 0x80 | (uc & 0x3f);
|
---|
1539 | }
|
---|
1540 | else
|
---|
1541 | {
|
---|
1542 | AssertMsgFailed(("Invalid code point U+%08x!\n", uc));
|
---|
1543 | *puch++ = 0x7f;
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | return (char *)puch;
|
---|
1547 | }
|
---|
1548 | RT_EXPORT_SYMBOL(RTStrPutCpInternal);
|
---|
1549 |
|
---|
1550 |
|
---|
1551 | RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz)
|
---|
1552 | {
|
---|
1553 | if (pszStart < psz)
|
---|
1554 | {
|
---|
1555 | /* simple char? */
|
---|
1556 | const unsigned char *puch = (const unsigned char *)psz;
|
---|
1557 | unsigned uch = *--puch;
|
---|
1558 | if (!(uch & RT_BIT(7)))
|
---|
1559 | return (char *)puch;
|
---|
1560 | RTStrAssertMsgReturn(!(uch & RT_BIT(6)), ("uch=%#x\n", uch), (char *)pszStart);
|
---|
1561 |
|
---|
1562 | /* two or more. */
|
---|
1563 | uint32_t uMask = 0xffffffc0;
|
---|
1564 | while ( (const unsigned char *)pszStart < puch
|
---|
1565 | && !(uMask & 1))
|
---|
1566 | {
|
---|
1567 | uch = *--puch;
|
---|
1568 | if ((uch & 0xc0) != 0x80)
|
---|
1569 | {
|
---|
1570 | RTStrAssertMsgReturn((uch & (uMask >> 1)) == (uMask & 0xff),
|
---|
1571 | ("Invalid UTF-8 encoding: %.*Rhxs puch=%p psz=%p\n", psz - (char *)puch, puch, psz),
|
---|
1572 | (char *)pszStart);
|
---|
1573 | return (char *)puch;
|
---|
1574 | }
|
---|
1575 | uMask >>= 1;
|
---|
1576 | }
|
---|
1577 | RTStrAssertMsgFailed(("Invalid UTF-8 encoding: %.*Rhxs puch=%p psz=%p\n", psz - (char *)puch, puch, psz));
|
---|
1578 | }
|
---|
1579 | return (char *)pszStart;
|
---|
1580 | }
|
---|
1581 | RT_EXPORT_SYMBOL(RTStrPrevCp);
|
---|
1582 |
|
---|