/src/icu/icu4c/source/common/ucnv.cpp
Line | Count | Source |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ****************************************************************************** |
5 | | * |
6 | | * Copyright (C) 1998-2016, International Business Machines |
7 | | * Corporation and others. All Rights Reserved. |
8 | | * |
9 | | ****************************************************************************** |
10 | | * |
11 | | * ucnv.c: |
12 | | * Implements APIs for the ICU's codeset conversion library; |
13 | | * mostly calls through internal functions; |
14 | | * created by Bertrand A. Damiba |
15 | | * |
16 | | * Modification History: |
17 | | * |
18 | | * Date Name Description |
19 | | * 04/04/99 helena Fixed internal header inclusion. |
20 | | * 05/09/00 helena Added implementation to handle fallback mappings. |
21 | | * 06/20/2000 helena OS/400 port changes; mostly typecast. |
22 | | */ |
23 | | |
24 | | #include "unicode/utypes.h" |
25 | | |
26 | | #if !UCONFIG_NO_CONVERSION |
27 | | |
28 | | #include <memory> |
29 | | |
30 | | #include "unicode/ustring.h" |
31 | | #include "unicode/ucnv.h" |
32 | | #include "unicode/ucnv_err.h" |
33 | | #include "unicode/uset.h" |
34 | | #include "unicode/utf.h" |
35 | | #include "unicode/utf16.h" |
36 | | #include "putilimp.h" |
37 | | #include "cmemory.h" |
38 | | #include "cstring.h" |
39 | | #include "uassert.h" |
40 | | #include "utracimp.h" |
41 | | #include "ustr_imp.h" |
42 | | #include "ucnv_imp.h" |
43 | | #include "ucnv_cnv.h" |
44 | | #include "ucnv_bld.h" |
45 | | |
46 | | /* size of intermediate and preflighting buffers in ucnv_convert() */ |
47 | 0 | #define CHUNK_SIZE 1024 |
48 | | |
49 | | typedef struct UAmbiguousConverter { |
50 | | const char *name; |
51 | | const char16_t variant5c; |
52 | | } UAmbiguousConverter; |
53 | | |
54 | | static const UAmbiguousConverter ambiguousConverters[]={ |
55 | | { "ibm-897_P100-1995", 0xa5 }, |
56 | | { "ibm-942_P120-1999", 0xa5 }, |
57 | | { "ibm-943_P130-1999", 0xa5 }, |
58 | | { "ibm-946_P100-1995", 0xa5 }, |
59 | | { "ibm-33722_P120-1999", 0xa5 }, |
60 | | { "ibm-1041_P100-1995", 0xa5 }, |
61 | | /*{ "ibm-54191_P100-2006", 0xa5 },*/ |
62 | | /*{ "ibm-62383_P100-2007", 0xa5 },*/ |
63 | | /*{ "ibm-891_P100-1995", 0x20a9 },*/ |
64 | | { "ibm-944_P100-1995", 0x20a9 }, |
65 | | { "ibm-949_P110-1999", 0x20a9 }, |
66 | | { "ibm-1363_P110-1997", 0x20a9 }, |
67 | | { "ISO_2022,locale=ko,version=0", 0x20a9 }, |
68 | | { "ibm-1088_P100-1995", 0x20a9 } |
69 | | }; |
70 | | |
71 | | /*Calls through createConverter */ |
72 | | U_CAPI UConverter* U_EXPORT2 |
73 | | ucnv_open (const char *name, |
74 | | UErrorCode * err) |
75 | 10.8k | { |
76 | 10.8k | UConverter *r; |
77 | | |
78 | 10.8k | if (err == nullptr || U_FAILURE (*err)) { |
79 | 0 | return nullptr; |
80 | 0 | } |
81 | | |
82 | 10.8k | r = ucnv_createConverter(nullptr, name, err); |
83 | 10.8k | return r; |
84 | 10.8k | } |
85 | | |
86 | | U_CAPI UConverter* U_EXPORT2 |
87 | | ucnv_openPackage (const char *packageName, const char *converterName, UErrorCode * err) |
88 | 0 | { |
89 | 0 | return ucnv_createConverterFromPackage(packageName, converterName, err); |
90 | 0 | } |
91 | | |
92 | | /*Extracts the char16_t* to a char* and calls through createConverter */ |
93 | | U_CAPI UConverter* U_EXPORT2 |
94 | | ucnv_openU (const char16_t * name, |
95 | | UErrorCode * err) |
96 | 0 | { |
97 | 0 | char asciiName[UCNV_MAX_CONVERTER_NAME_LENGTH]; |
98 | |
|
99 | 0 | if (err == nullptr || U_FAILURE(*err)) |
100 | 0 | return nullptr; |
101 | 0 | if (name == nullptr) |
102 | 0 | return ucnv_open (nullptr, err); |
103 | 0 | if (u_strlen(name) >= UCNV_MAX_CONVERTER_NAME_LENGTH) |
104 | 0 | { |
105 | 0 | *err = U_ILLEGAL_ARGUMENT_ERROR; |
106 | 0 | return nullptr; |
107 | 0 | } |
108 | 0 | return ucnv_open(u_austrcpy(asciiName, name), err); |
109 | 0 | } |
110 | | |
111 | | /* Copy the string that is represented by the UConverterPlatform enum |
112 | | * @param platformString An output buffer |
113 | | * @param platform An enum representing a platform |
114 | | * @return the length of the copied string. |
115 | | */ |
116 | | static int32_t |
117 | | ucnv_copyPlatformString(char *platformString, UConverterPlatform pltfrm) |
118 | 0 | { |
119 | 0 | switch (pltfrm) |
120 | 0 | { |
121 | 0 | case UCNV_IBM: |
122 | 0 | uprv_strcpy(platformString, "ibm-"); |
123 | 0 | return 4; |
124 | 0 | case UCNV_UNKNOWN: |
125 | 0 | break; |
126 | 0 | } |
127 | | |
128 | | /* default to empty string */ |
129 | 0 | *platformString = 0; |
130 | 0 | return 0; |
131 | 0 | } |
132 | | |
133 | | /*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls |
134 | | *through createConverter*/ |
135 | | U_CAPI UConverter* U_EXPORT2 |
136 | | ucnv_openCCSID (int32_t codepage, |
137 | | UConverterPlatform platform, |
138 | | UErrorCode * err) |
139 | 0 | { |
140 | 0 | char myName[UCNV_MAX_CONVERTER_NAME_LENGTH]; |
141 | 0 | int32_t myNameLen; |
142 | |
|
143 | 0 | if (err == nullptr || U_FAILURE (*err)) |
144 | 0 | return nullptr; |
145 | | |
146 | | /* ucnv_copyPlatformString could return "ibm-" or "cp" */ |
147 | 0 | myNameLen = ucnv_copyPlatformString(myName, platform); |
148 | 0 | T_CString_integerToString(myName + myNameLen, codepage, 10); |
149 | |
|
150 | 0 | return ucnv_createConverter(nullptr, myName, err); |
151 | 0 | } |
152 | | |
153 | | /* Creating a temporary stack-based object that can be used in one thread, |
154 | | and created from a converter that is shared across threads. |
155 | | */ |
156 | | |
157 | | U_CAPI UConverter* U_EXPORT2 |
158 | | ucnv_safeClone(const UConverter* cnv, void *stackBuffer, int32_t *pBufferSize, UErrorCode *status) |
159 | 0 | { |
160 | 0 | UConverter *localConverter, *allocatedConverter; |
161 | 0 | int32_t stackBufferSize; |
162 | 0 | int32_t bufferSizeNeeded; |
163 | 0 | UErrorCode cbErr; |
164 | 0 | UConverterToUnicodeArgs toUArgs = { |
165 | 0 | sizeof(UConverterToUnicodeArgs), |
166 | 0 | true, |
167 | 0 | nullptr, |
168 | 0 | nullptr, |
169 | 0 | nullptr, |
170 | 0 | nullptr, |
171 | 0 | nullptr, |
172 | 0 | nullptr |
173 | 0 | }; |
174 | 0 | UConverterFromUnicodeArgs fromUArgs = { |
175 | 0 | sizeof(UConverterFromUnicodeArgs), |
176 | 0 | true, |
177 | 0 | nullptr, |
178 | 0 | nullptr, |
179 | 0 | nullptr, |
180 | 0 | nullptr, |
181 | 0 | nullptr, |
182 | 0 | nullptr |
183 | 0 | }; |
184 | |
|
185 | 0 | UTRACE_ENTRY_OC(UTRACE_UCNV_CLONE); |
186 | |
|
187 | 0 | if (status == nullptr || U_FAILURE(*status)){ |
188 | 0 | UTRACE_EXIT_STATUS(status? *status: U_ILLEGAL_ARGUMENT_ERROR); |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | | |
192 | 0 | if (cnv == nullptr) { |
193 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
194 | 0 | UTRACE_EXIT_STATUS(*status); |
195 | 0 | return nullptr; |
196 | 0 | } |
197 | | |
198 | 0 | UTRACE_DATA3(UTRACE_OPEN_CLOSE, "clone converter %s at %p into stackBuffer %p", |
199 | 0 | ucnv_getName(cnv, status), cnv, stackBuffer); |
200 | |
|
201 | 0 | if (cnv->sharedData->impl->safeClone != nullptr) { |
202 | | /* call the custom safeClone function for sizing */ |
203 | 0 | bufferSizeNeeded = 0; |
204 | 0 | cnv->sharedData->impl->safeClone(cnv, nullptr, &bufferSizeNeeded, status); |
205 | 0 | if (U_FAILURE(*status)) { |
206 | 0 | UTRACE_EXIT_STATUS(*status); |
207 | 0 | return nullptr; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | else |
211 | 0 | { |
212 | | /* inherent sizing */ |
213 | 0 | bufferSizeNeeded = sizeof(UConverter); |
214 | 0 | } |
215 | | |
216 | 0 | if (pBufferSize == nullptr) { |
217 | 0 | stackBufferSize = 1; |
218 | 0 | pBufferSize = &stackBufferSize; |
219 | 0 | } else { |
220 | 0 | stackBufferSize = *pBufferSize; |
221 | 0 | if (stackBufferSize <= 0){ /* 'preflighting' request - set needed size into *pBufferSize */ |
222 | 0 | *pBufferSize = bufferSizeNeeded; |
223 | 0 | UTRACE_EXIT_VALUE(bufferSizeNeeded); |
224 | 0 | return nullptr; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | | /* Adjust (if necessary) the stackBuffer pointer to be aligned correctly for a UConverter. |
229 | | * TODO(Jira ICU-20736) Redo this using std::align() once g++4.9 compatibility is no longer needed. |
230 | | */ |
231 | 0 | if (stackBuffer) { |
232 | 0 | uintptr_t p = reinterpret_cast<uintptr_t>(stackBuffer); |
233 | 0 | uintptr_t aligned_p = (p + alignof(UConverter) - 1) & ~(alignof(UConverter) - 1); |
234 | 0 | ptrdiff_t pointerAdjustment = aligned_p - p; |
235 | 0 | if (bufferSizeNeeded + pointerAdjustment <= stackBufferSize) { |
236 | 0 | stackBuffer = reinterpret_cast<void *>(aligned_p); |
237 | 0 | stackBufferSize -= static_cast<int32_t>(pointerAdjustment); |
238 | 0 | } else { |
239 | | /* prevent using the stack buffer but keep the size > 0 so that we do not just preflight */ |
240 | 0 | stackBufferSize = 1; |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | | /* Now, see if we must allocate any memory */ |
245 | 0 | if (stackBufferSize < bufferSizeNeeded || stackBuffer == nullptr) |
246 | 0 | { |
247 | | /* allocate one here...*/ |
248 | 0 | localConverter = allocatedConverter = (UConverter *) uprv_malloc (bufferSizeNeeded); |
249 | |
|
250 | 0 | if(localConverter == nullptr) { |
251 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
252 | 0 | UTRACE_EXIT_STATUS(*status); |
253 | 0 | return nullptr; |
254 | 0 | } |
255 | | // If pBufferSize was nullptr as the input, pBufferSize is set to &stackBufferSize in this function. |
256 | 0 | if (pBufferSize != &stackBufferSize) { |
257 | 0 | *status = U_SAFECLONE_ALLOCATED_WARNING; |
258 | 0 | } |
259 | | |
260 | | /* record the fact that memory was allocated */ |
261 | 0 | *pBufferSize = bufferSizeNeeded; |
262 | 0 | } else { |
263 | | /* just use the stack buffer */ |
264 | 0 | localConverter = (UConverter*) stackBuffer; |
265 | 0 | allocatedConverter = nullptr; |
266 | 0 | } |
267 | | |
268 | 0 | uprv_memset(localConverter, 0, bufferSizeNeeded); |
269 | | |
270 | | /* Copy initial state */ |
271 | 0 | uprv_memcpy(localConverter, cnv, sizeof(UConverter)); |
272 | 0 | localConverter->isCopyLocal = localConverter->isExtraLocal = false; |
273 | | |
274 | | /* copy the substitution string */ |
275 | 0 | if (cnv->subChars == (uint8_t *)cnv->subUChars) { |
276 | 0 | localConverter->subChars = (uint8_t *)localConverter->subUChars; |
277 | 0 | } else { |
278 | 0 | localConverter->subChars = (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR); |
279 | 0 | if (localConverter->subChars == nullptr) { |
280 | 0 | uprv_free(allocatedConverter); |
281 | 0 | UTRACE_EXIT_STATUS(*status); |
282 | 0 | return nullptr; |
283 | 0 | } |
284 | 0 | uprv_memcpy(localConverter->subChars, cnv->subChars, UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR); |
285 | 0 | } |
286 | | |
287 | | /* now either call the safeclone fcn or not */ |
288 | 0 | if (cnv->sharedData->impl->safeClone != nullptr) { |
289 | | /* call the custom safeClone function */ |
290 | 0 | localConverter = cnv->sharedData->impl->safeClone(cnv, localConverter, pBufferSize, status); |
291 | 0 | } |
292 | |
|
293 | 0 | if(localConverter==nullptr || U_FAILURE(*status)) { |
294 | 0 | if (allocatedConverter != nullptr && allocatedConverter->subChars != (uint8_t *)allocatedConverter->subUChars) { |
295 | 0 | uprv_free(allocatedConverter->subChars); |
296 | 0 | } |
297 | 0 | uprv_free(allocatedConverter); |
298 | 0 | UTRACE_EXIT_STATUS(*status); |
299 | 0 | return nullptr; |
300 | 0 | } |
301 | | |
302 | | /* increment refcount of shared data if needed */ |
303 | 0 | if (cnv->sharedData->isReferenceCounted) { |
304 | 0 | ucnv_incrementRefCount(cnv->sharedData); |
305 | 0 | } |
306 | |
|
307 | 0 | if(localConverter == (UConverter*)stackBuffer) { |
308 | | /* we're using user provided data - set to not destroy */ |
309 | 0 | localConverter->isCopyLocal = true; |
310 | 0 | } |
311 | | |
312 | | /* allow callback functions to handle any memory allocation */ |
313 | 0 | toUArgs.converter = fromUArgs.converter = localConverter; |
314 | 0 | cbErr = U_ZERO_ERROR; |
315 | 0 | cnv->fromCharErrorBehaviour(cnv->toUContext, &toUArgs, nullptr, 0, UCNV_CLONE, &cbErr); |
316 | 0 | cbErr = U_ZERO_ERROR; |
317 | 0 | cnv->fromUCharErrorBehaviour(cnv->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_CLONE, &cbErr); |
318 | |
|
319 | 0 | UTRACE_EXIT_PTR_STATUS(localConverter, *status); |
320 | 0 | return localConverter; |
321 | 0 | } |
322 | | |
323 | | U_CAPI UConverter* U_EXPORT2 |
324 | | ucnv_clone(const UConverter* cnv, UErrorCode *status) |
325 | 0 | { |
326 | 0 | return ucnv_safeClone(cnv, nullptr, nullptr, status); |
327 | 0 | } |
328 | | |
329 | | /*Decreases the reference counter in the shared immutable section of the object |
330 | | *and frees the mutable part*/ |
331 | | |
332 | | U_CAPI void U_EXPORT2 |
333 | | ucnv_close (UConverter * converter) |
334 | 12.4k | { |
335 | 12.4k | UErrorCode errorCode = U_ZERO_ERROR; |
336 | | |
337 | 12.4k | UTRACE_ENTRY_OC(UTRACE_UCNV_CLOSE); |
338 | | |
339 | 12.4k | if (converter == nullptr) |
340 | 1.60k | { |
341 | 1.60k | UTRACE_EXIT(); |
342 | 1.60k | return; |
343 | 1.60k | } |
344 | | |
345 | 10.8k | UTRACE_DATA3(UTRACE_OPEN_CLOSE, "close converter %s at %p, isCopyLocal=%b", |
346 | 10.8k | ucnv_getName(converter, &errorCode), converter, converter->isCopyLocal); |
347 | | |
348 | | /* In order to speed up the close, only call the callbacks when they have been changed. |
349 | | This performance check will only work when the callbacks are set within a shared library |
350 | | or from user code that statically links this code. */ |
351 | | /* first, notify the callback functions that the converter is closed */ |
352 | 10.8k | if (converter->fromCharErrorBehaviour != UCNV_TO_U_DEFAULT_CALLBACK) { |
353 | 0 | UConverterToUnicodeArgs toUArgs = { |
354 | 0 | sizeof(UConverterToUnicodeArgs), |
355 | 0 | true, |
356 | 0 | nullptr, |
357 | 0 | nullptr, |
358 | 0 | nullptr, |
359 | 0 | nullptr, |
360 | 0 | nullptr, |
361 | 0 | nullptr |
362 | 0 | }; |
363 | |
|
364 | 0 | toUArgs.converter = converter; |
365 | 0 | errorCode = U_ZERO_ERROR; |
366 | 0 | converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, nullptr, 0, UCNV_CLOSE, &errorCode); |
367 | 0 | } |
368 | 10.8k | if (converter->fromUCharErrorBehaviour != UCNV_FROM_U_DEFAULT_CALLBACK) { |
369 | 0 | UConverterFromUnicodeArgs fromUArgs = { |
370 | 0 | sizeof(UConverterFromUnicodeArgs), |
371 | 0 | true, |
372 | 0 | nullptr, |
373 | 0 | nullptr, |
374 | 0 | nullptr, |
375 | 0 | nullptr, |
376 | 0 | nullptr, |
377 | 0 | nullptr |
378 | 0 | }; |
379 | 0 | fromUArgs.converter = converter; |
380 | 0 | errorCode = U_ZERO_ERROR; |
381 | 0 | converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_CLOSE, &errorCode); |
382 | 0 | } |
383 | | |
384 | 10.8k | if (converter->sharedData->impl->close != nullptr) { |
385 | 5.54k | converter->sharedData->impl->close(converter); |
386 | 5.54k | } |
387 | | |
388 | 10.8k | if (converter->subChars != (uint8_t *)converter->subUChars) { |
389 | 0 | uprv_free(converter->subChars); |
390 | 0 | } |
391 | | |
392 | 10.8k | if (converter->sharedData->isReferenceCounted) { |
393 | 2.66k | ucnv_unloadSharedDataIfReady(converter->sharedData); |
394 | 2.66k | } |
395 | | |
396 | 10.8k | if(!converter->isCopyLocal){ |
397 | 10.8k | uprv_free(converter); |
398 | 10.8k | } |
399 | | |
400 | 10.8k | UTRACE_EXIT(); |
401 | 10.8k | } |
402 | | |
403 | | /*returns a single Name from the list, will return nullptr if out of bounds |
404 | | */ |
405 | | U_CAPI const char* U_EXPORT2 |
406 | | ucnv_getAvailableName (int32_t n) |
407 | 7.45k | { |
408 | 7.45k | if (0 <= n && n <= 0xffff) { |
409 | 7.45k | UErrorCode err = U_ZERO_ERROR; |
410 | 7.45k | const char *name = ucnv_bld_getAvailableConverter((uint16_t)n, &err); |
411 | 7.45k | if (U_SUCCESS(err)) { |
412 | 7.45k | return name; |
413 | 7.45k | } |
414 | 7.45k | } |
415 | 0 | return nullptr; |
416 | 7.45k | } |
417 | | |
418 | | U_CAPI int32_t U_EXPORT2 |
419 | | ucnv_countAvailable () |
420 | 7.45k | { |
421 | 7.45k | UErrorCode err = U_ZERO_ERROR; |
422 | 7.45k | return ucnv_bld_countAvailableConverters(&err); |
423 | 7.45k | } |
424 | | |
425 | | U_CAPI void U_EXPORT2 |
426 | | ucnv_getSubstChars (const UConverter * converter, |
427 | | char *mySubChar, |
428 | | int8_t * len, |
429 | | UErrorCode * err) |
430 | 0 | { |
431 | 0 | if (U_FAILURE (*err)) |
432 | 0 | return; |
433 | | |
434 | 0 | if (converter->subCharLen <= 0) { |
435 | | /* Unicode string or empty string from ucnv_setSubstString(). */ |
436 | 0 | *len = 0; |
437 | 0 | return; |
438 | 0 | } |
439 | | |
440 | 0 | if (*len < converter->subCharLen) /*not enough space in subChars */ |
441 | 0 | { |
442 | 0 | *err = U_INDEX_OUTOFBOUNDS_ERROR; |
443 | 0 | return; |
444 | 0 | } |
445 | | |
446 | 0 | uprv_memcpy (mySubChar, converter->subChars, converter->subCharLen); /*fills in the subchars */ |
447 | 0 | *len = converter->subCharLen; /*store # of bytes copied to buffer */ |
448 | 0 | } |
449 | | |
450 | | U_CAPI void U_EXPORT2 |
451 | | ucnv_setSubstChars (UConverter * converter, |
452 | | const char *mySubChar, |
453 | | int8_t len, |
454 | | UErrorCode * err) |
455 | 0 | { |
456 | 0 | if (U_FAILURE (*err)) |
457 | 0 | return; |
458 | | |
459 | | /*Makes sure that the subChar is within the codepages char length boundaries */ |
460 | 0 | if ((len > converter->sharedData->staticData->maxBytesPerChar) |
461 | 0 | || (len < converter->sharedData->staticData->minBytesPerChar)) |
462 | 0 | { |
463 | 0 | *err = U_ILLEGAL_ARGUMENT_ERROR; |
464 | 0 | return; |
465 | 0 | } |
466 | | |
467 | 0 | uprv_memcpy (converter->subChars, mySubChar, len); /*copies the subchars */ |
468 | 0 | converter->subCharLen = len; /*sets the new len */ |
469 | | |
470 | | /* |
471 | | * There is currently (2001Feb) no separate API to set/get subChar1. |
472 | | * In order to always have subChar written after it is explicitly set, |
473 | | * we set subChar1 to 0. |
474 | | */ |
475 | 0 | converter->subChar1 = 0; |
476 | 0 | } |
477 | | |
478 | | U_CAPI void U_EXPORT2 |
479 | | ucnv_setSubstString(UConverter *cnv, |
480 | | const char16_t *s, |
481 | | int32_t length, |
482 | 0 | UErrorCode *err) { |
483 | 0 | alignas(UConverter) char cloneBuffer[U_CNV_SAFECLONE_BUFFERSIZE]; |
484 | 0 | char chars[UCNV_ERROR_BUFFER_LENGTH]; |
485 | |
|
486 | 0 | UConverter *clone; |
487 | 0 | uint8_t *subChars; |
488 | 0 | int32_t cloneSize, length8; |
489 | | |
490 | | /* Let the following functions check all arguments. */ |
491 | 0 | cloneSize = sizeof(cloneBuffer); |
492 | 0 | clone = ucnv_safeClone(cnv, cloneBuffer, &cloneSize, err); |
493 | 0 | ucnv_setFromUCallBack(clone, UCNV_FROM_U_CALLBACK_STOP, nullptr, nullptr, nullptr, err); |
494 | 0 | length8 = ucnv_fromUChars(clone, chars, (int32_t)sizeof(chars), s, length, err); |
495 | 0 | ucnv_close(clone); |
496 | 0 | if (U_FAILURE(*err)) { |
497 | 0 | return; |
498 | 0 | } |
499 | | |
500 | 0 | if (cnv->sharedData->impl->writeSub == nullptr |
501 | 0 | #if !UCONFIG_NO_LEGACY_CONVERSION |
502 | 0 | || (cnv->sharedData->staticData->conversionType == UCNV_MBCS && |
503 | 0 | ucnv_MBCSGetType(cnv) != UCNV_EBCDIC_STATEFUL) |
504 | 0 | #endif |
505 | 0 | ) { |
506 | | /* The converter is not stateful. Store the charset bytes as a fixed string. */ |
507 | 0 | subChars = (uint8_t *)chars; |
508 | 0 | } else { |
509 | | /* |
510 | | * The converter has a non-default writeSub() function, indicating |
511 | | * that it is stateful. |
512 | | * Store the Unicode string for on-the-fly conversion for correct |
513 | | * state handling. |
514 | | */ |
515 | 0 | if (length > UCNV_ERROR_BUFFER_LENGTH) { |
516 | | /* |
517 | | * Should not occur. The converter should output at least one byte |
518 | | * per char16_t, which means that ucnv_fromUChars() should catch all |
519 | | * overflows. |
520 | | */ |
521 | 0 | *err = U_BUFFER_OVERFLOW_ERROR; |
522 | 0 | return; |
523 | 0 | } |
524 | 0 | subChars = (uint8_t *)s; |
525 | 0 | if (length < 0) { |
526 | 0 | length = u_strlen(s); |
527 | 0 | } |
528 | 0 | length8 = length * U_SIZEOF_UCHAR; |
529 | 0 | } |
530 | | |
531 | | /* |
532 | | * For storing the substitution string, select either the small buffer inside |
533 | | * UConverter or allocate a subChars buffer. |
534 | | */ |
535 | 0 | if (length8 > UCNV_MAX_SUBCHAR_LEN) { |
536 | | /* Use a separate buffer for the string. Outside UConverter to not make it too large. */ |
537 | 0 | if (cnv->subChars == (uint8_t *)cnv->subUChars) { |
538 | | /* Allocate a new buffer for the string. */ |
539 | 0 | cnv->subChars = (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR); |
540 | 0 | if (cnv->subChars == nullptr) { |
541 | 0 | cnv->subChars = (uint8_t *)cnv->subUChars; |
542 | 0 | *err = U_MEMORY_ALLOCATION_ERROR; |
543 | 0 | return; |
544 | 0 | } |
545 | 0 | uprv_memset(cnv->subChars, 0, UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR); |
546 | 0 | } |
547 | 0 | } |
548 | | |
549 | | /* Copy the substitution string into the UConverter or its subChars buffer. */ |
550 | 0 | if (length8 == 0) { |
551 | 0 | cnv->subCharLen = 0; |
552 | 0 | } else { |
553 | 0 | uprv_memcpy(cnv->subChars, subChars, length8); |
554 | 0 | if (subChars == (uint8_t *)chars) { |
555 | 0 | cnv->subCharLen = (int8_t)length8; |
556 | 0 | } else /* subChars == s */ { |
557 | 0 | cnv->subCharLen = (int8_t)-length; |
558 | 0 | } |
559 | 0 | } |
560 | | |
561 | | /* See comment in ucnv_setSubstChars(). */ |
562 | 0 | cnv->subChar1 = 0; |
563 | 0 | } |
564 | | |
565 | | /*resets the internal states of a converter |
566 | | *goal : have the same behaviour than a freshly created converter |
567 | | */ |
568 | | static void _reset(UConverter *converter, UConverterResetChoice choice, |
569 | 17.2k | UBool callCallback) { |
570 | 17.2k | if(converter == nullptr) { |
571 | 0 | return; |
572 | 0 | } |
573 | | |
574 | 17.2k | if(callCallback) { |
575 | | /* first, notify the callback functions that the converter is reset */ |
576 | 7.39k | UErrorCode errorCode; |
577 | | |
578 | 7.39k | if(choice<=UCNV_RESET_TO_UNICODE && converter->fromCharErrorBehaviour != UCNV_TO_U_DEFAULT_CALLBACK) { |
579 | 0 | UConverterToUnicodeArgs toUArgs = { |
580 | 0 | sizeof(UConverterToUnicodeArgs), |
581 | 0 | true, |
582 | 0 | nullptr, |
583 | 0 | nullptr, |
584 | 0 | nullptr, |
585 | 0 | nullptr, |
586 | 0 | nullptr, |
587 | 0 | nullptr |
588 | 0 | }; |
589 | 0 | toUArgs.converter = converter; |
590 | 0 | errorCode = U_ZERO_ERROR; |
591 | 0 | converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, nullptr, 0, UCNV_RESET, &errorCode); |
592 | 0 | } |
593 | 7.39k | if(choice!=UCNV_RESET_TO_UNICODE && converter->fromUCharErrorBehaviour != UCNV_FROM_U_DEFAULT_CALLBACK) { |
594 | 0 | UConverterFromUnicodeArgs fromUArgs = { |
595 | 0 | sizeof(UConverterFromUnicodeArgs), |
596 | 0 | true, |
597 | 0 | nullptr, |
598 | 0 | nullptr, |
599 | 0 | nullptr, |
600 | 0 | nullptr, |
601 | 0 | nullptr, |
602 | 0 | nullptr |
603 | 0 | }; |
604 | 0 | fromUArgs.converter = converter; |
605 | 0 | errorCode = U_ZERO_ERROR; |
606 | 0 | converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_RESET, &errorCode); |
607 | 0 | } |
608 | 7.39k | } |
609 | | |
610 | | /* now reset the converter itself */ |
611 | 17.2k | if(choice<=UCNV_RESET_TO_UNICODE) { |
612 | 2.41k | converter->toUnicodeStatus = converter->sharedData->toUnicodeStatus; |
613 | 2.41k | converter->mode = 0; |
614 | 2.41k | converter->toULength = 0; |
615 | 2.41k | converter->invalidCharLength = converter->UCharErrorBufferLength = 0; |
616 | 2.41k | converter->preToULength = 0; |
617 | 2.41k | } |
618 | 17.2k | if(choice!=UCNV_RESET_TO_UNICODE) { |
619 | 14.7k | converter->fromUnicodeStatus = 0; |
620 | 14.7k | converter->fromUChar32 = 0; |
621 | 14.7k | converter->invalidUCharLength = converter->charErrorBufferLength = 0; |
622 | 14.7k | converter->preFromUFirstCP = U_SENTINEL; |
623 | 14.7k | converter->preFromULength = 0; |
624 | 14.7k | } |
625 | | |
626 | 17.2k | if (converter->sharedData->impl->reset != nullptr) { |
627 | | /* call the custom reset function */ |
628 | 11.1k | converter->sharedData->impl->reset(converter, choice); |
629 | 11.1k | } |
630 | 17.2k | } |
631 | | |
632 | | U_CAPI void U_EXPORT2 |
633 | | ucnv_reset(UConverter *converter) |
634 | 0 | { |
635 | 0 | _reset(converter, UCNV_RESET_BOTH, true); |
636 | 0 | } |
637 | | |
638 | | U_CAPI void U_EXPORT2 |
639 | | ucnv_resetToUnicode(UConverter *converter) |
640 | 0 | { |
641 | 0 | _reset(converter, UCNV_RESET_TO_UNICODE, true); |
642 | 0 | } |
643 | | |
644 | | U_CAPI void U_EXPORT2 |
645 | | ucnv_resetFromUnicode(UConverter *converter) |
646 | 7.39k | { |
647 | 7.39k | _reset(converter, UCNV_RESET_FROM_UNICODE, true); |
648 | 7.39k | } |
649 | | |
650 | | U_CAPI int8_t U_EXPORT2 |
651 | | ucnv_getMaxCharSize (const UConverter * converter) |
652 | 0 | { |
653 | 0 | return converter->maxBytesPerUChar; |
654 | 0 | } |
655 | | |
656 | | |
657 | | U_CAPI int8_t U_EXPORT2 |
658 | | ucnv_getMinCharSize (const UConverter * converter) |
659 | 0 | { |
660 | 0 | return converter->sharedData->staticData->minBytesPerChar; |
661 | 0 | } |
662 | | |
663 | | U_CAPI const char* U_EXPORT2 |
664 | | ucnv_getName (const UConverter * converter, UErrorCode * err) |
665 | | |
666 | 0 | { |
667 | 0 | if (U_FAILURE (*err)) |
668 | 0 | return nullptr; |
669 | 0 | if(converter->sharedData->impl->getName){ |
670 | 0 | const char* temp= converter->sharedData->impl->getName(converter); |
671 | 0 | if(temp) |
672 | 0 | return temp; |
673 | 0 | } |
674 | 0 | return converter->sharedData->staticData->name; |
675 | 0 | } |
676 | | |
677 | | U_CAPI int32_t U_EXPORT2 |
678 | | ucnv_getCCSID(const UConverter * converter, |
679 | | UErrorCode * err) |
680 | 0 | { |
681 | 0 | int32_t ccsid; |
682 | 0 | if (U_FAILURE (*err)) |
683 | 0 | return -1; |
684 | | |
685 | 0 | ccsid = converter->sharedData->staticData->codepage; |
686 | 0 | if (ccsid == 0) { |
687 | | /* Rare case. This is for cases like gb18030, |
688 | | which doesn't have an IBM canonical name, but does have an IBM alias. */ |
689 | 0 | const char *standardName = ucnv_getStandardName(ucnv_getName(converter, err), "IBM", err); |
690 | 0 | if (U_SUCCESS(*err) && standardName) { |
691 | 0 | const char *ccsidStr = uprv_strchr(standardName, '-'); |
692 | 0 | if (ccsidStr) { |
693 | 0 | ccsid = (int32_t)atol(ccsidStr+1); /* +1 to skip '-' */ |
694 | 0 | } |
695 | 0 | } |
696 | 0 | } |
697 | 0 | return ccsid; |
698 | 0 | } |
699 | | |
700 | | |
701 | | U_CAPI UConverterPlatform U_EXPORT2 |
702 | | ucnv_getPlatform (const UConverter * converter, |
703 | | UErrorCode * err) |
704 | 0 | { |
705 | 0 | if (U_FAILURE (*err)) |
706 | 0 | return UCNV_UNKNOWN; |
707 | | |
708 | 0 | return (UConverterPlatform)converter->sharedData->staticData->platform; |
709 | 0 | } |
710 | | |
711 | | U_CAPI void U_EXPORT2 |
712 | | ucnv_getToUCallBack (const UConverter * converter, |
713 | | UConverterToUCallback *action, |
714 | | const void **context) |
715 | 0 | { |
716 | 0 | *action = converter->fromCharErrorBehaviour; |
717 | 0 | *context = converter->toUContext; |
718 | 0 | } |
719 | | |
720 | | U_CAPI void U_EXPORT2 |
721 | | ucnv_getFromUCallBack (const UConverter * converter, |
722 | | UConverterFromUCallback *action, |
723 | | const void **context) |
724 | 0 | { |
725 | 0 | *action = converter->fromUCharErrorBehaviour; |
726 | 0 | *context = converter->fromUContext; |
727 | 0 | } |
728 | | |
729 | | U_CAPI void U_EXPORT2 |
730 | | ucnv_setToUCallBack (UConverter * converter, |
731 | | UConverterToUCallback newAction, |
732 | | const void* newContext, |
733 | | UConverterToUCallback *oldAction, |
734 | | const void** oldContext, |
735 | | UErrorCode * err) |
736 | 0 | { |
737 | 0 | if (U_FAILURE (*err)) |
738 | 0 | return; |
739 | 0 | if (oldAction) *oldAction = converter->fromCharErrorBehaviour; |
740 | 0 | converter->fromCharErrorBehaviour = newAction; |
741 | 0 | if (oldContext) *oldContext = converter->toUContext; |
742 | 0 | converter->toUContext = newContext; |
743 | 0 | } |
744 | | |
745 | | U_CAPI void U_EXPORT2 |
746 | | ucnv_setFromUCallBack (UConverter * converter, |
747 | | UConverterFromUCallback newAction, |
748 | | const void* newContext, |
749 | | UConverterFromUCallback *oldAction, |
750 | | const void** oldContext, |
751 | | UErrorCode * err) |
752 | 0 | { |
753 | 0 | if (U_FAILURE (*err)) |
754 | 0 | return; |
755 | 0 | if (oldAction) *oldAction = converter->fromUCharErrorBehaviour; |
756 | 0 | converter->fromUCharErrorBehaviour = newAction; |
757 | 0 | if (oldContext) *oldContext = converter->fromUContext; |
758 | 0 | converter->fromUContext = newContext; |
759 | 0 | } |
760 | | |
761 | | static void |
762 | | _updateOffsets(int32_t *offsets, int32_t length, |
763 | 0 | int32_t sourceIndex, int32_t errorInputLength) { |
764 | 0 | int32_t *limit; |
765 | 0 | int32_t delta, offset; |
766 | |
|
767 | 0 | if(sourceIndex>=0) { |
768 | | /* |
769 | | * adjust each offset by adding the previous sourceIndex |
770 | | * minus the length of the input sequence that caused an |
771 | | * error, if any |
772 | | */ |
773 | 0 | delta=sourceIndex-errorInputLength; |
774 | 0 | } else { |
775 | | /* |
776 | | * set each offset to -1 because this conversion function |
777 | | * does not handle offsets |
778 | | */ |
779 | 0 | delta=-1; |
780 | 0 | } |
781 | |
|
782 | 0 | limit=offsets+length; |
783 | 0 | if(delta==0) { |
784 | | /* most common case, nothing to do */ |
785 | 0 | } else if(delta>0) { |
786 | | /* add the delta to each offset (but not if the offset is <0) */ |
787 | 0 | while(offsets<limit) { |
788 | 0 | offset=*offsets; |
789 | 0 | if(offset>=0) { |
790 | 0 | *offsets=offset+delta; |
791 | 0 | } |
792 | 0 | ++offsets; |
793 | 0 | } |
794 | 0 | } else /* delta<0 */ { |
795 | | /* |
796 | | * set each offset to -1 because this conversion function |
797 | | * does not handle offsets |
798 | | * or the error input sequence started in a previous buffer |
799 | | */ |
800 | 0 | while(offsets<limit) { |
801 | 0 | *offsets++=-1; |
802 | 0 | } |
803 | 0 | } |
804 | 0 | } |
805 | | |
806 | | /* ucnv_fromUnicode --------------------------------------------------------- */ |
807 | | |
808 | | /* |
809 | | * Implementation note for m:n conversions |
810 | | * |
811 | | * While collecting source units to find the longest match for m:n conversion, |
812 | | * some source units may need to be stored for a partial match. |
813 | | * When a second buffer does not yield a match on all of the previously stored |
814 | | * source units, then they must be "replayed", i.e., fed back into the converter. |
815 | | * |
816 | | * The code relies on the fact that replaying will not nest - |
817 | | * converting a replay buffer will not result in a replay. |
818 | | * This is because a replay is necessary only after the _continuation_ of a |
819 | | * partial match failed, but a replay buffer is converted as a whole. |
820 | | * It may result in some of its units being stored again for a partial match, |
821 | | * but there will not be a continuation _during_ the replay which could fail. |
822 | | * |
823 | | * It is conceivable that a callback function could call the converter |
824 | | * recursively in a way that causes another replay to be stored, but that |
825 | | * would be an error in the callback function. |
826 | | * Such violations will cause assertion failures in a debug build, |
827 | | * and wrong output, but they will not cause a crash. |
828 | | */ |
829 | | |
830 | | static void |
831 | 33.9M | _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) { |
832 | 33.9M | UConverterFromUnicode fromUnicode; |
833 | 33.9M | UConverter *cnv; |
834 | 33.9M | const char16_t *s; |
835 | 33.9M | char *t; |
836 | 33.9M | int32_t *offsets; |
837 | 33.9M | int32_t sourceIndex; |
838 | 33.9M | int32_t errorInputLength; |
839 | 33.9M | UBool converterSawEndOfInput, calledCallback; |
840 | | |
841 | | /* variables for m:n conversion */ |
842 | 33.9M | char16_t replay[UCNV_EXT_MAX_UCHARS]; |
843 | 33.9M | const char16_t *realSource, *realSourceLimit; |
844 | 33.9M | int32_t realSourceIndex; |
845 | 33.9M | UBool realFlush; |
846 | | |
847 | 33.9M | cnv=pArgs->converter; |
848 | 33.9M | s=pArgs->source; |
849 | 33.9M | t=pArgs->target; |
850 | 33.9M | offsets=pArgs->offsets; |
851 | | |
852 | | /* get the converter implementation function */ |
853 | 33.9M | sourceIndex=0; |
854 | 33.9M | if(offsets==nullptr) { |
855 | 33.9M | fromUnicode=cnv->sharedData->impl->fromUnicode; |
856 | 33.9M | } else { |
857 | 0 | fromUnicode=cnv->sharedData->impl->fromUnicodeWithOffsets; |
858 | 0 | if(fromUnicode==nullptr) { |
859 | | /* there is no WithOffsets implementation */ |
860 | 0 | fromUnicode=cnv->sharedData->impl->fromUnicode; |
861 | | /* we will write -1 for each offset */ |
862 | 0 | sourceIndex=-1; |
863 | 0 | } |
864 | 0 | } |
865 | | |
866 | 33.9M | if(cnv->preFromULength>=0) { |
867 | | /* normal mode */ |
868 | 33.9M | realSource=nullptr; |
869 | | |
870 | | /* avoid compiler warnings - not otherwise necessary, and the values do not matter */ |
871 | 33.9M | realSourceLimit=nullptr; |
872 | 33.9M | realFlush=false; |
873 | 33.9M | realSourceIndex=0; |
874 | 33.9M | } else { |
875 | | /* |
876 | | * Previous m:n conversion stored source units from a partial match |
877 | | * and failed to consume all of them. |
878 | | * We need to "replay" them from a temporary buffer and convert them first. |
879 | | */ |
880 | 0 | if (!pArgs->source) { |
881 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
882 | 0 | return; |
883 | 0 | } |
884 | 0 | realSource=pArgs->source; |
885 | 0 | realSourceLimit=pArgs->sourceLimit; |
886 | 0 | realFlush=pArgs->flush; |
887 | 0 | realSourceIndex=sourceIndex; |
888 | |
|
889 | 0 | uprv_memcpy(replay, cnv->preFromU, -cnv->preFromULength*U_SIZEOF_UCHAR); |
890 | 0 | pArgs->source=replay; |
891 | 0 | pArgs->sourceLimit=replay-cnv->preFromULength; |
892 | 0 | pArgs->flush=false; |
893 | 0 | sourceIndex=-1; |
894 | |
|
895 | 0 | cnv->preFromULength=0; |
896 | 0 | } |
897 | | |
898 | | /* |
899 | | * loop for conversion and error handling |
900 | | * |
901 | | * loop { |
902 | | * convert |
903 | | * loop { |
904 | | * update offsets |
905 | | * handle end of input |
906 | | * handle errors/call callback |
907 | | * } |
908 | | * } |
909 | | */ |
910 | 113M | for(;;) { |
911 | 113M | if(U_SUCCESS(*err)) { |
912 | | /* convert */ |
913 | 113M | fromUnicode(pArgs, err); |
914 | | |
915 | | /* |
916 | | * set a flag for whether the converter |
917 | | * successfully processed the end of the input |
918 | | * |
919 | | * need not check cnv->preFromULength==0 because a replay (<0) will cause |
920 | | * s<sourceLimit before converterSawEndOfInput is checked |
921 | | */ |
922 | 113M | converterSawEndOfInput= |
923 | 113M | static_cast<UBool>(U_SUCCESS(*err) && |
924 | 33.9M | pArgs->flush && pArgs->source==pArgs->sourceLimit && |
925 | 7.74k | cnv->fromUChar32==0); |
926 | 113M | } else { |
927 | | /* handle error from ucnv_convertEx() */ |
928 | 0 | converterSawEndOfInput=false; |
929 | 0 | } |
930 | | |
931 | | /* no callback called yet for this iteration */ |
932 | 113M | calledCallback=false; |
933 | | |
934 | | /* no sourceIndex adjustment for conversion, only for callback output */ |
935 | 113M | errorInputLength=0; |
936 | | |
937 | | /* |
938 | | * loop for offsets and error handling |
939 | | * |
940 | | * iterates at most 3 times: |
941 | | * 1. to clean up after the conversion function |
942 | | * 2. after the callback |
943 | | * 3. after the callback again if there was truncated input |
944 | | */ |
945 | 192M | for(;;) { |
946 | | /* update offsets if we write any */ |
947 | 192M | if(offsets!=nullptr) { |
948 | 0 | int32_t length = static_cast<int32_t>(pArgs->target - t); |
949 | 0 | if(length>0) { |
950 | 0 | _updateOffsets(offsets, length, sourceIndex, errorInputLength); |
951 | | |
952 | | /* |
953 | | * if a converter handles offsets and updates the offsets |
954 | | * pointer at the end, then pArgs->offset should not change |
955 | | * here; |
956 | | * however, some converters do not handle offsets at all |
957 | | * (sourceIndex<0) or may not update the offsets pointer |
958 | | */ |
959 | 0 | pArgs->offsets=offsets+=length; |
960 | 0 | } |
961 | |
|
962 | 0 | if(sourceIndex>=0) { |
963 | 0 | sourceIndex += static_cast<int32_t>(pArgs->source - s); |
964 | 0 | } |
965 | 0 | } |
966 | | |
967 | 192M | if(cnv->preFromULength<0) { |
968 | | /* |
969 | | * switch the source to new replay units (cannot occur while replaying) |
970 | | * after offset handling and before end-of-input and callback handling |
971 | | */ |
972 | 0 | if(realSource==nullptr) { |
973 | 0 | realSource=pArgs->source; |
974 | 0 | realSourceLimit=pArgs->sourceLimit; |
975 | 0 | realFlush=pArgs->flush; |
976 | 0 | realSourceIndex=sourceIndex; |
977 | |
|
978 | 0 | uprv_memcpy(replay, cnv->preFromU, -cnv->preFromULength*U_SIZEOF_UCHAR); |
979 | 0 | pArgs->source=replay; |
980 | 0 | pArgs->sourceLimit=replay-cnv->preFromULength; |
981 | 0 | pArgs->flush=false; |
982 | 0 | if((sourceIndex+=cnv->preFromULength)<0) { |
983 | 0 | sourceIndex=-1; |
984 | 0 | } |
985 | |
|
986 | 0 | cnv->preFromULength=0; |
987 | 0 | } else { |
988 | | /* see implementation note before _fromUnicodeWithCallback() */ |
989 | 0 | U_ASSERT(realSource==nullptr); |
990 | 0 | *err=U_INTERNAL_PROGRAM_ERROR; |
991 | 0 | } |
992 | 0 | } |
993 | | |
994 | | /* update pointers */ |
995 | 192M | s=pArgs->source; |
996 | 192M | t=pArgs->target; |
997 | | |
998 | 192M | if(U_SUCCESS(*err)) { |
999 | 113M | if(s<pArgs->sourceLimit) { |
1000 | | /* |
1001 | | * continue with the conversion loop while there is still input left |
1002 | | * (continue converting by breaking out of only the inner loop) |
1003 | | */ |
1004 | 79.1M | break; |
1005 | 79.1M | } else if(realSource!=nullptr) { |
1006 | | /* switch back from replaying to the real source and continue */ |
1007 | 0 | pArgs->source=realSource; |
1008 | 0 | pArgs->sourceLimit=realSourceLimit; |
1009 | 0 | pArgs->flush=realFlush; |
1010 | 0 | sourceIndex=realSourceIndex; |
1011 | |
|
1012 | 0 | realSource=nullptr; |
1013 | 0 | break; |
1014 | 33.9M | } else if(pArgs->flush && cnv->fromUChar32!=0) { |
1015 | | /* |
1016 | | * the entire input stream is consumed |
1017 | | * and there is a partial, truncated input sequence left |
1018 | | */ |
1019 | | |
1020 | | /* inject an error and continue with callback handling */ |
1021 | 352 | *err=U_TRUNCATED_CHAR_FOUND; |
1022 | 352 | calledCallback=false; /* new error condition */ |
1023 | 33.9M | } else { |
1024 | | /* input consumed */ |
1025 | 33.9M | if(pArgs->flush) { |
1026 | | /* |
1027 | | * return to the conversion loop once more if the flush |
1028 | | * flag is set and the conversion function has not |
1029 | | * successfully processed the end of the input yet |
1030 | | * |
1031 | | * (continue converting by breaking out of only the inner loop) |
1032 | | */ |
1033 | 9.35k | if(!converterSawEndOfInput) { |
1034 | 1.96k | break; |
1035 | 1.96k | } |
1036 | | |
1037 | | /* reset the converter without calling the callback function */ |
1038 | 7.39k | _reset(cnv, UCNV_RESET_FROM_UNICODE, false); |
1039 | 7.39k | } |
1040 | | |
1041 | | /* done successfully */ |
1042 | 33.9M | return; |
1043 | 33.9M | } |
1044 | 113M | } |
1045 | | |
1046 | | /* U_FAILURE(*err) */ |
1047 | 79.1M | { |
1048 | 79.1M | UErrorCode e; |
1049 | | |
1050 | 79.1M | if( calledCallback || |
1051 | 79.1M | (e=*err)==U_BUFFER_OVERFLOW_ERROR || |
1052 | 79.1M | (e!=U_INVALID_CHAR_FOUND && |
1053 | 37.7M | e!=U_ILLEGAL_CHAR_FOUND && |
1054 | 364 | e!=U_TRUNCATED_CHAR_FOUND) |
1055 | 79.1M | ) { |
1056 | | /* |
1057 | | * the callback did not or cannot resolve the error: |
1058 | | * set output pointers and return |
1059 | | * |
1060 | | * the check for buffer overflow is redundant but it is |
1061 | | * a high-runner case and hopefully documents the intent |
1062 | | * well |
1063 | | * |
1064 | | * if we were replaying, then the replay buffer must be |
1065 | | * copied back into the UConverter |
1066 | | * and the real arguments must be restored |
1067 | | */ |
1068 | 33.8k | if(realSource!=nullptr) { |
1069 | 0 | int32_t length; |
1070 | |
|
1071 | 0 | U_ASSERT(cnv->preFromULength==0); |
1072 | |
|
1073 | 0 | length = static_cast<int32_t>(pArgs->sourceLimit - pArgs->source); |
1074 | 0 | if(length>0) { |
1075 | 0 | u_memcpy(cnv->preFromU, pArgs->source, length); |
1076 | 0 | cnv->preFromULength = static_cast<int8_t>(-length); |
1077 | 0 | } |
1078 | |
|
1079 | 0 | pArgs->source=realSource; |
1080 | 0 | pArgs->sourceLimit=realSourceLimit; |
1081 | 0 | pArgs->flush=realFlush; |
1082 | 0 | } |
1083 | | |
1084 | 33.8k | return; |
1085 | 33.8k | } |
1086 | 79.1M | } |
1087 | | |
1088 | | /* callback handling */ |
1089 | 79.1M | { |
1090 | 79.1M | UChar32 codePoint; |
1091 | | |
1092 | | /* get and write the code point */ |
1093 | 79.1M | codePoint=cnv->fromUChar32; |
1094 | 79.1M | errorInputLength=0; |
1095 | 79.1M | U16_APPEND_UNSAFE(cnv->invalidUCharBuffer, errorInputLength, codePoint); |
1096 | 79.1M | cnv->invalidUCharLength = static_cast<int8_t>(errorInputLength); |
1097 | | |
1098 | | /* set the converter state to deal with the next character */ |
1099 | 79.1M | cnv->fromUChar32=0; |
1100 | | |
1101 | | /* call the callback function */ |
1102 | 79.1M | cnv->fromUCharErrorBehaviour(cnv->fromUContext, pArgs, |
1103 | 79.1M | cnv->invalidUCharBuffer, errorInputLength, codePoint, |
1104 | 79.1M | *err==U_INVALID_CHAR_FOUND ? UCNV_UNASSIGNED : UCNV_ILLEGAL, |
1105 | 79.1M | err); |
1106 | 79.1M | } |
1107 | | |
1108 | | /* |
1109 | | * loop back to the offset handling |
1110 | | * |
1111 | | * this flag will indicate after offset handling |
1112 | | * that a callback was called; |
1113 | | * if the callback did not resolve the error, then we return |
1114 | | */ |
1115 | 79.1M | calledCallback=true; |
1116 | 79.1M | } |
1117 | 113M | } |
1118 | 33.9M | } |
1119 | | |
1120 | | /* |
1121 | | * Output the fromUnicode overflow buffer. |
1122 | | * Call this function if(cnv->charErrorBufferLength>0). |
1123 | | * @return true if overflow |
1124 | | */ |
1125 | | static UBool |
1126 | | ucnv_outputOverflowFromUnicode(UConverter *cnv, |
1127 | | char **target, const char *targetLimit, |
1128 | | int32_t **pOffsets, |
1129 | 22.6k | UErrorCode *err) { |
1130 | 22.6k | int32_t *offsets; |
1131 | 22.6k | char *overflow, *t; |
1132 | 22.6k | int32_t i, length; |
1133 | | |
1134 | 22.6k | t=*target; |
1135 | 22.6k | if(pOffsets!=nullptr) { |
1136 | 22.6k | offsets=*pOffsets; |
1137 | 22.6k | } else { |
1138 | 0 | offsets=nullptr; |
1139 | 0 | } |
1140 | | |
1141 | 22.6k | overflow = reinterpret_cast<char*>(cnv->charErrorBuffer); |
1142 | 22.6k | length=cnv->charErrorBufferLength; |
1143 | 22.6k | i=0; |
1144 | 68.4k | while(i<length) { |
1145 | 45.8k | if(t==targetLimit) { |
1146 | | /* the overflow buffer contains too much, keep the rest */ |
1147 | 0 | int32_t j=0; |
1148 | |
|
1149 | 0 | do { |
1150 | 0 | overflow[j++]=overflow[i++]; |
1151 | 0 | } while(i<length); |
1152 | |
|
1153 | 0 | cnv->charErrorBufferLength = static_cast<int8_t>(j); |
1154 | 0 | *target=t; |
1155 | 0 | if(offsets!=nullptr) { |
1156 | 0 | *pOffsets=offsets; |
1157 | 0 | } |
1158 | 0 | *err=U_BUFFER_OVERFLOW_ERROR; |
1159 | 0 | return true; |
1160 | 0 | } |
1161 | | |
1162 | | /* copy the overflow contents to the target */ |
1163 | 45.8k | *t++=overflow[i++]; |
1164 | 45.8k | if(offsets!=nullptr) { |
1165 | 0 | *offsets++=-1; /* no source index available for old output */ |
1166 | 0 | } |
1167 | 45.8k | } |
1168 | | |
1169 | | /* the overflow buffer is completely copied to the target */ |
1170 | 22.6k | cnv->charErrorBufferLength=0; |
1171 | 22.6k | *target=t; |
1172 | 22.6k | if(offsets!=nullptr) { |
1173 | 0 | *pOffsets=offsets; |
1174 | 0 | } |
1175 | 22.6k | return false; |
1176 | 22.6k | } |
1177 | | |
1178 | | U_CAPI void U_EXPORT2 |
1179 | | ucnv_fromUnicode(UConverter *cnv, |
1180 | | char **target, const char *targetLimit, |
1181 | | const char16_t **source, const char16_t *sourceLimit, |
1182 | | int32_t *offsets, |
1183 | | UBool flush, |
1184 | 33.9M | UErrorCode *err) { |
1185 | 33.9M | UConverterFromUnicodeArgs args; |
1186 | 33.9M | const char16_t *s; |
1187 | 33.9M | char *t; |
1188 | | |
1189 | | /* check parameters */ |
1190 | 33.9M | if(err==nullptr || U_FAILURE(*err)) { |
1191 | 0 | return; |
1192 | 0 | } |
1193 | | |
1194 | 33.9M | if(cnv==nullptr || target==nullptr || source==nullptr) { |
1195 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1196 | 0 | return; |
1197 | 0 | } |
1198 | | |
1199 | 33.9M | s=*source; |
1200 | 33.9M | t=*target; |
1201 | | |
1202 | 33.9M | if ((const void *)U_MAX_PTR(sourceLimit) == (const void *)sourceLimit) { |
1203 | | /* |
1204 | | Prevent code from going into an infinite loop in case we do hit this |
1205 | | limit. The limit pointer is expected to be on a char16_t * boundary. |
1206 | | This also prevents the next argument check from failing. |
1207 | | */ |
1208 | 0 | sourceLimit = (const char16_t *)(((const char *)sourceLimit) - 1); |
1209 | 0 | } |
1210 | | |
1211 | | /* |
1212 | | * All these conditions should never happen. |
1213 | | * |
1214 | | * 1) Make sure that the limits are >= to the address source or target |
1215 | | * |
1216 | | * 2) Make sure that the buffer sizes do not exceed the number range for |
1217 | | * int32_t because some functions use the size (in units or bytes) |
1218 | | * rather than comparing pointers, and because offsets are int32_t values. |
1219 | | * |
1220 | | * size_t is guaranteed to be unsigned and large enough for the job. |
1221 | | * |
1222 | | * Return with an error instead of adjusting the limits because we would |
1223 | | * not be able to maintain the semantics that either the source must be |
1224 | | * consumed or the target filled (unless an error occurs). |
1225 | | * An adjustment would be targetLimit=t+0x7fffffff; for example. |
1226 | | * |
1227 | | * 3) Make sure that the user didn't incorrectly cast a char16_t * pointer |
1228 | | * to a char * pointer and provide an incomplete char16_t code unit. |
1229 | | */ |
1230 | 33.9M | if (sourceLimit<s || targetLimit<t || |
1231 | 33.9M | ((size_t)(sourceLimit-s)>(size_t)0x3fffffff && sourceLimit>s) || |
1232 | 33.9M | ((size_t)(targetLimit-t)>(size_t)0x7fffffff && targetLimit>t) || |
1233 | 33.9M | (((const char *)sourceLimit-(const char *)s) & 1) != 0) |
1234 | 0 | { |
1235 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1236 | 0 | return; |
1237 | 0 | } |
1238 | | |
1239 | | /* output the target overflow buffer */ |
1240 | 33.9M | if( cnv->charErrorBufferLength>0 && |
1241 | 22.6k | ucnv_outputOverflowFromUnicode(cnv, target, targetLimit, &offsets, err) |
1242 | 33.9M | ) { |
1243 | | /* U_BUFFER_OVERFLOW_ERROR */ |
1244 | 0 | return; |
1245 | 0 | } |
1246 | | /* *target may have moved, therefore stop using t */ |
1247 | | |
1248 | 33.9M | if(!flush && s==sourceLimit && cnv->preFromULength>=0) { |
1249 | | /* the overflow buffer is emptied and there is no new input: we are done */ |
1250 | 756 | return; |
1251 | 756 | } |
1252 | | |
1253 | | /* |
1254 | | * Do not simply return with a buffer overflow error if |
1255 | | * !flush && t==targetLimit |
1256 | | * because it is possible that the source will not generate any output. |
1257 | | * For example, the skip callback may be called; |
1258 | | * it does not output anything. |
1259 | | */ |
1260 | | |
1261 | | /* prepare the converter arguments */ |
1262 | 33.9M | args.converter=cnv; |
1263 | 33.9M | args.flush=flush; |
1264 | 33.9M | args.offsets=offsets; |
1265 | 33.9M | args.source=s; |
1266 | 33.9M | args.sourceLimit=sourceLimit; |
1267 | 33.9M | args.target=*target; |
1268 | 33.9M | args.targetLimit=targetLimit; |
1269 | 33.9M | args.size=sizeof(args); |
1270 | | |
1271 | 33.9M | _fromUnicodeWithCallback(&args, err); |
1272 | | |
1273 | 33.9M | *source=args.source; |
1274 | 33.9M | *target=args.target; |
1275 | 33.9M | } |
1276 | | |
1277 | | /* ucnv_toUnicode() --------------------------------------------------------- */ |
1278 | | |
1279 | | static void |
1280 | 2.41k | _toUnicodeWithCallback(UConverterToUnicodeArgs *pArgs, UErrorCode *err) { |
1281 | 2.41k | UConverterToUnicode toUnicode; |
1282 | 2.41k | UConverter *cnv; |
1283 | 2.41k | const char *s; |
1284 | 2.41k | char16_t *t; |
1285 | 2.41k | int32_t *offsets; |
1286 | 2.41k | int32_t sourceIndex; |
1287 | 2.41k | int32_t errorInputLength; |
1288 | 2.41k | UBool converterSawEndOfInput, calledCallback; |
1289 | | |
1290 | | /* variables for m:n conversion */ |
1291 | 2.41k | char replay[UCNV_EXT_MAX_BYTES]; |
1292 | 2.41k | const char *realSource, *realSourceLimit; |
1293 | 2.41k | int32_t realSourceIndex; |
1294 | 2.41k | UBool realFlush; |
1295 | | |
1296 | 2.41k | cnv=pArgs->converter; |
1297 | 2.41k | s=pArgs->source; |
1298 | 2.41k | t=pArgs->target; |
1299 | 2.41k | offsets=pArgs->offsets; |
1300 | | |
1301 | | /* get the converter implementation function */ |
1302 | 2.41k | sourceIndex=0; |
1303 | 2.41k | if(offsets==nullptr) { |
1304 | 2.41k | toUnicode=cnv->sharedData->impl->toUnicode; |
1305 | 2.41k | } else { |
1306 | 0 | toUnicode=cnv->sharedData->impl->toUnicodeWithOffsets; |
1307 | 0 | if(toUnicode==nullptr) { |
1308 | | /* there is no WithOffsets implementation */ |
1309 | 0 | toUnicode=cnv->sharedData->impl->toUnicode; |
1310 | | /* we will write -1 for each offset */ |
1311 | 0 | sourceIndex=-1; |
1312 | 0 | } |
1313 | 0 | } |
1314 | | |
1315 | 2.41k | if(cnv->preToULength>=0) { |
1316 | | /* normal mode */ |
1317 | 2.41k | realSource=nullptr; |
1318 | | |
1319 | | /* avoid compiler warnings - not otherwise necessary, and the values do not matter */ |
1320 | 2.41k | realSourceLimit=nullptr; |
1321 | 2.41k | realFlush=false; |
1322 | 2.41k | realSourceIndex=0; |
1323 | 2.41k | } else { |
1324 | | /* |
1325 | | * Previous m:n conversion stored source units from a partial match |
1326 | | * and failed to consume all of them. |
1327 | | * We need to "replay" them from a temporary buffer and convert them first. |
1328 | | */ |
1329 | 0 | if (!pArgs->source) { |
1330 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1331 | 0 | return; |
1332 | 0 | } |
1333 | 0 | realSource=pArgs->source; |
1334 | 0 | realSourceLimit=pArgs->sourceLimit; |
1335 | 0 | realFlush=pArgs->flush; |
1336 | 0 | realSourceIndex=sourceIndex; |
1337 | |
|
1338 | 0 | uprv_memcpy(replay, cnv->preToU, -cnv->preToULength); |
1339 | 0 | pArgs->source=replay; |
1340 | 0 | pArgs->sourceLimit=replay-cnv->preToULength; |
1341 | 0 | pArgs->flush=false; |
1342 | 0 | sourceIndex=-1; |
1343 | |
|
1344 | 0 | cnv->preToULength=0; |
1345 | 0 | } |
1346 | | |
1347 | | /* |
1348 | | * loop for conversion and error handling |
1349 | | * |
1350 | | * loop { |
1351 | | * convert |
1352 | | * loop { |
1353 | | * update offsets |
1354 | | * handle end of input |
1355 | | * handle errors/call callback |
1356 | | * } |
1357 | | * } |
1358 | | */ |
1359 | 3.82M | for(;;) { |
1360 | 3.82M | if(U_SUCCESS(*err)) { |
1361 | | /* convert */ |
1362 | 3.82M | toUnicode(pArgs, err); |
1363 | | |
1364 | | /* |
1365 | | * set a flag for whether the converter |
1366 | | * successfully processed the end of the input |
1367 | | * |
1368 | | * need not check cnv->preToULength==0 because a replay (<0) will cause |
1369 | | * s<sourceLimit before converterSawEndOfInput is checked |
1370 | | */ |
1371 | 3.82M | converterSawEndOfInput= |
1372 | 3.82M | static_cast<UBool>(U_SUCCESS(*err) && |
1373 | 2.87k | pArgs->flush && pArgs->source==pArgs->sourceLimit && |
1374 | 2.87k | cnv->toULength==0); |
1375 | 3.82M | } else { |
1376 | | /* handle error from getNextUChar() or ucnv_convertEx() */ |
1377 | 0 | converterSawEndOfInput=false; |
1378 | 0 | } |
1379 | | |
1380 | | /* no callback called yet for this iteration */ |
1381 | 3.82M | calledCallback=false; |
1382 | | |
1383 | | /* no sourceIndex adjustment for conversion, only for callback output */ |
1384 | 3.82M | errorInputLength=0; |
1385 | | |
1386 | | /* |
1387 | | * loop for offsets and error handling |
1388 | | * |
1389 | | * iterates at most 3 times: |
1390 | | * 1. to clean up after the conversion function |
1391 | | * 2. after the callback |
1392 | | * 3. after the callback again if there was truncated input |
1393 | | */ |
1394 | 7.64M | for(;;) { |
1395 | | /* update offsets if we write any */ |
1396 | 7.64M | if(offsets!=nullptr) { |
1397 | 0 | int32_t length = static_cast<int32_t>(pArgs->target - t); |
1398 | 0 | if(length>0) { |
1399 | 0 | _updateOffsets(offsets, length, sourceIndex, errorInputLength); |
1400 | | |
1401 | | /* |
1402 | | * if a converter handles offsets and updates the offsets |
1403 | | * pointer at the end, then pArgs->offset should not change |
1404 | | * here; |
1405 | | * however, some converters do not handle offsets at all |
1406 | | * (sourceIndex<0) or may not update the offsets pointer |
1407 | | */ |
1408 | 0 | pArgs->offsets=offsets+=length; |
1409 | 0 | } |
1410 | |
|
1411 | 0 | if(sourceIndex>=0) { |
1412 | 0 | sourceIndex += static_cast<int32_t>(pArgs->source - s); |
1413 | 0 | } |
1414 | 0 | } |
1415 | | |
1416 | 7.64M | if(cnv->preToULength<0) { |
1417 | | /* |
1418 | | * switch the source to new replay units (cannot occur while replaying) |
1419 | | * after offset handling and before end-of-input and callback handling |
1420 | | */ |
1421 | 0 | if(realSource==nullptr) { |
1422 | 0 | realSource=pArgs->source; |
1423 | 0 | realSourceLimit=pArgs->sourceLimit; |
1424 | 0 | realFlush=pArgs->flush; |
1425 | 0 | realSourceIndex=sourceIndex; |
1426 | |
|
1427 | 0 | uprv_memcpy(replay, cnv->preToU, -cnv->preToULength); |
1428 | 0 | pArgs->source=replay; |
1429 | 0 | pArgs->sourceLimit=replay-cnv->preToULength; |
1430 | 0 | pArgs->flush=false; |
1431 | 0 | if((sourceIndex+=cnv->preToULength)<0) { |
1432 | 0 | sourceIndex=-1; |
1433 | 0 | } |
1434 | |
|
1435 | 0 | cnv->preToULength=0; |
1436 | 0 | } else { |
1437 | | /* see implementation note before _fromUnicodeWithCallback() */ |
1438 | 0 | U_ASSERT(realSource==nullptr); |
1439 | 0 | *err=U_INTERNAL_PROGRAM_ERROR; |
1440 | 0 | } |
1441 | 0 | } |
1442 | | |
1443 | | /* update pointers */ |
1444 | 7.64M | s=pArgs->source; |
1445 | 7.64M | t=pArgs->target; |
1446 | | |
1447 | 7.64M | if(U_SUCCESS(*err)) { |
1448 | 3.82M | if(s<pArgs->sourceLimit) { |
1449 | | /* |
1450 | | * continue with the conversion loop while there is still input left |
1451 | | * (continue converting by breaking out of only the inner loop) |
1452 | | */ |
1453 | 3.82M | break; |
1454 | 3.82M | } else if(realSource!=nullptr) { |
1455 | | /* switch back from replaying to the real source and continue */ |
1456 | 0 | pArgs->source=realSource; |
1457 | 0 | pArgs->sourceLimit=realSourceLimit; |
1458 | 0 | pArgs->flush=realFlush; |
1459 | 0 | sourceIndex=realSourceIndex; |
1460 | |
|
1461 | 0 | realSource=nullptr; |
1462 | 0 | break; |
1463 | 4.03k | } else if(pArgs->flush && cnv->toULength>0) { |
1464 | | /* |
1465 | | * the entire input stream is consumed |
1466 | | * and there is a partial, truncated input sequence left |
1467 | | */ |
1468 | | |
1469 | | /* inject an error and continue with callback handling */ |
1470 | 455 | *err=U_TRUNCATED_CHAR_FOUND; |
1471 | 455 | calledCallback=false; /* new error condition */ |
1472 | 3.57k | } else { |
1473 | | /* input consumed */ |
1474 | 3.57k | if(pArgs->flush) { |
1475 | | /* |
1476 | | * return to the conversion loop once more if the flush |
1477 | | * flag is set and the conversion function has not |
1478 | | * successfully processed the end of the input yet |
1479 | | * |
1480 | | * (continue converting by breaking out of only the inner loop) |
1481 | | */ |
1482 | 3.57k | if(!converterSawEndOfInput) { |
1483 | 1.16k | break; |
1484 | 1.16k | } |
1485 | | |
1486 | | /* reset the converter without calling the callback function */ |
1487 | 2.41k | _reset(cnv, UCNV_RESET_TO_UNICODE, false); |
1488 | 2.41k | } |
1489 | | |
1490 | | /* done successfully */ |
1491 | 2.41k | return; |
1492 | 3.57k | } |
1493 | 3.82M | } |
1494 | | |
1495 | | /* U_FAILURE(*err) */ |
1496 | 3.82M | { |
1497 | 3.82M | UErrorCode e; |
1498 | | |
1499 | 3.82M | if( calledCallback || |
1500 | 3.82M | (e=*err)==U_BUFFER_OVERFLOW_ERROR || |
1501 | 3.82M | (e!=U_INVALID_CHAR_FOUND && |
1502 | 2.87M | e!=U_ILLEGAL_CHAR_FOUND && |
1503 | 51.0k | e!=U_TRUNCATED_CHAR_FOUND && |
1504 | 50.5k | e!=U_ILLEGAL_ESCAPE_SEQUENCE && |
1505 | 14.3k | e!=U_UNSUPPORTED_ESCAPE_SEQUENCE) |
1506 | 3.82M | ) { |
1507 | | /* |
1508 | | * the callback did not or cannot resolve the error: |
1509 | | * set output pointers and return |
1510 | | * |
1511 | | * the check for buffer overflow is redundant but it is |
1512 | | * a high-runner case and hopefully documents the intent |
1513 | | * well |
1514 | | * |
1515 | | * if we were replaying, then the replay buffer must be |
1516 | | * copied back into the UConverter |
1517 | | * and the real arguments must be restored |
1518 | | */ |
1519 | 0 | if(realSource!=nullptr) { |
1520 | 0 | int32_t length; |
1521 | |
|
1522 | 0 | U_ASSERT(cnv->preToULength==0); |
1523 | |
|
1524 | 0 | length = static_cast<int32_t>(pArgs->sourceLimit - pArgs->source); |
1525 | 0 | if(length>0) { |
1526 | 0 | uprv_memcpy(cnv->preToU, pArgs->source, length); |
1527 | 0 | cnv->preToULength = static_cast<int8_t>(-length); |
1528 | 0 | } |
1529 | |
|
1530 | 0 | pArgs->source=realSource; |
1531 | 0 | pArgs->sourceLimit=realSourceLimit; |
1532 | 0 | pArgs->flush=realFlush; |
1533 | 0 | } |
1534 | |
|
1535 | 0 | return; |
1536 | 0 | } |
1537 | 3.82M | } |
1538 | | |
1539 | | /* copy toUBytes[] to invalidCharBuffer[] */ |
1540 | 3.82M | errorInputLength=cnv->invalidCharLength=cnv->toULength; |
1541 | 3.82M | if(errorInputLength>0) { |
1542 | 3.82M | uprv_memcpy(cnv->invalidCharBuffer, cnv->toUBytes, errorInputLength); |
1543 | 3.82M | } |
1544 | | |
1545 | | /* set the converter state to deal with the next character */ |
1546 | 3.82M | cnv->toULength=0; |
1547 | | |
1548 | | /* call the callback function */ |
1549 | 3.82M | if(cnv->toUCallbackReason==UCNV_ILLEGAL && *err==U_INVALID_CHAR_FOUND) { |
1550 | 948k | cnv->toUCallbackReason = UCNV_UNASSIGNED; |
1551 | 948k | } |
1552 | 3.82M | cnv->fromCharErrorBehaviour(cnv->toUContext, pArgs, |
1553 | 3.82M | cnv->invalidCharBuffer, errorInputLength, |
1554 | 3.82M | cnv->toUCallbackReason, |
1555 | 3.82M | err); |
1556 | 3.82M | cnv->toUCallbackReason = UCNV_ILLEGAL; /* reset to default value */ |
1557 | | |
1558 | | /* |
1559 | | * loop back to the offset handling |
1560 | | * |
1561 | | * this flag will indicate after offset handling |
1562 | | * that a callback was called; |
1563 | | * if the callback did not resolve the error, then we return |
1564 | | */ |
1565 | 3.82M | calledCallback=true; |
1566 | 3.82M | } |
1567 | 3.82M | } |
1568 | 2.41k | } |
1569 | | |
1570 | | /* |
1571 | | * Output the toUnicode overflow buffer. |
1572 | | * Call this function if(cnv->UCharErrorBufferLength>0). |
1573 | | * @return true if overflow |
1574 | | */ |
1575 | | static UBool |
1576 | | ucnv_outputOverflowToUnicode(UConverter *cnv, |
1577 | | char16_t **target, const char16_t *targetLimit, |
1578 | | int32_t **pOffsets, |
1579 | 0 | UErrorCode *err) { |
1580 | 0 | int32_t *offsets; |
1581 | 0 | char16_t *overflow, *t; |
1582 | 0 | int32_t i, length; |
1583 | |
|
1584 | 0 | t=*target; |
1585 | 0 | if(pOffsets!=nullptr) { |
1586 | 0 | offsets=*pOffsets; |
1587 | 0 | } else { |
1588 | 0 | offsets=nullptr; |
1589 | 0 | } |
1590 | |
|
1591 | 0 | overflow=cnv->UCharErrorBuffer; |
1592 | 0 | length=cnv->UCharErrorBufferLength; |
1593 | 0 | i=0; |
1594 | 0 | while(i<length) { |
1595 | 0 | if(t==targetLimit) { |
1596 | | /* the overflow buffer contains too much, keep the rest */ |
1597 | 0 | int32_t j=0; |
1598 | |
|
1599 | 0 | do { |
1600 | 0 | overflow[j++]=overflow[i++]; |
1601 | 0 | } while(i<length); |
1602 | |
|
1603 | 0 | cnv->UCharErrorBufferLength = static_cast<int8_t>(j); |
1604 | 0 | *target=t; |
1605 | 0 | if(offsets!=nullptr) { |
1606 | 0 | *pOffsets=offsets; |
1607 | 0 | } |
1608 | 0 | *err=U_BUFFER_OVERFLOW_ERROR; |
1609 | 0 | return true; |
1610 | 0 | } |
1611 | | |
1612 | | /* copy the overflow contents to the target */ |
1613 | 0 | *t++=overflow[i++]; |
1614 | 0 | if(offsets!=nullptr) { |
1615 | 0 | *offsets++=-1; /* no source index available for old output */ |
1616 | 0 | } |
1617 | 0 | } |
1618 | | |
1619 | | /* the overflow buffer is completely copied to the target */ |
1620 | 0 | cnv->UCharErrorBufferLength=0; |
1621 | 0 | *target=t; |
1622 | 0 | if(offsets!=nullptr) { |
1623 | 0 | *pOffsets=offsets; |
1624 | 0 | } |
1625 | 0 | return false; |
1626 | 0 | } |
1627 | | |
1628 | | U_CAPI void U_EXPORT2 |
1629 | | ucnv_toUnicode(UConverter *cnv, |
1630 | | char16_t **target, const char16_t *targetLimit, |
1631 | | const char **source, const char *sourceLimit, |
1632 | | int32_t *offsets, |
1633 | | UBool flush, |
1634 | 2.41k | UErrorCode *err) { |
1635 | 2.41k | UConverterToUnicodeArgs args; |
1636 | 2.41k | const char *s; |
1637 | 2.41k | char16_t *t; |
1638 | | |
1639 | | /* check parameters */ |
1640 | 2.41k | if(err==nullptr || U_FAILURE(*err)) { |
1641 | 0 | return; |
1642 | 0 | } |
1643 | | |
1644 | 2.41k | if(cnv==nullptr || target==nullptr || source==nullptr) { |
1645 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1646 | 0 | return; |
1647 | 0 | } |
1648 | | |
1649 | 2.41k | s=*source; |
1650 | 2.41k | t=*target; |
1651 | | |
1652 | 2.41k | if ((const void *)U_MAX_PTR(targetLimit) == (const void *)targetLimit) { |
1653 | | /* |
1654 | | Prevent code from going into an infinite loop in case we do hit this |
1655 | | limit. The limit pointer is expected to be on a char16_t * boundary. |
1656 | | This also prevents the next argument check from failing. |
1657 | | */ |
1658 | 0 | targetLimit = (const char16_t *)(((const char *)targetLimit) - 1); |
1659 | 0 | } |
1660 | | |
1661 | | /* |
1662 | | * All these conditions should never happen. |
1663 | | * |
1664 | | * 1) Make sure that the limits are >= to the address source or target |
1665 | | * |
1666 | | * 2) Make sure that the buffer sizes do not exceed the number range for |
1667 | | * int32_t because some functions use the size (in units or bytes) |
1668 | | * rather than comparing pointers, and because offsets are int32_t values. |
1669 | | * |
1670 | | * size_t is guaranteed to be unsigned and large enough for the job. |
1671 | | * |
1672 | | * Return with an error instead of adjusting the limits because we would |
1673 | | * not be able to maintain the semantics that either the source must be |
1674 | | * consumed or the target filled (unless an error occurs). |
1675 | | * An adjustment would be sourceLimit=t+0x7fffffff; for example. |
1676 | | * |
1677 | | * 3) Make sure that the user didn't incorrectly cast a char16_t * pointer |
1678 | | * to a char * pointer and provide an incomplete char16_t code unit. |
1679 | | */ |
1680 | 2.41k | if (sourceLimit<s || targetLimit<t || |
1681 | 2.41k | ((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s) || |
1682 | 2.41k | ((size_t)(targetLimit-t)>(size_t)0x3fffffff && targetLimit>t) || |
1683 | 2.41k | (((const char *)targetLimit-(const char *)t) & 1) != 0 |
1684 | 2.41k | ) { |
1685 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1686 | 0 | return; |
1687 | 0 | } |
1688 | | |
1689 | | /* output the target overflow buffer */ |
1690 | 2.41k | if( cnv->UCharErrorBufferLength>0 && |
1691 | 0 | ucnv_outputOverflowToUnicode(cnv, target, targetLimit, &offsets, err) |
1692 | 2.41k | ) { |
1693 | | /* U_BUFFER_OVERFLOW_ERROR */ |
1694 | 0 | return; |
1695 | 0 | } |
1696 | | /* *target may have moved, therefore stop using t */ |
1697 | | |
1698 | 2.41k | if(!flush && s==sourceLimit && cnv->preToULength>=0) { |
1699 | | /* the overflow buffer is emptied and there is no new input: we are done */ |
1700 | 0 | return; |
1701 | 0 | } |
1702 | | |
1703 | | /* |
1704 | | * Do not simply return with a buffer overflow error if |
1705 | | * !flush && t==targetLimit |
1706 | | * because it is possible that the source will not generate any output. |
1707 | | * For example, the skip callback may be called; |
1708 | | * it does not output anything. |
1709 | | */ |
1710 | | |
1711 | | /* prepare the converter arguments */ |
1712 | 2.41k | args.converter=cnv; |
1713 | 2.41k | args.flush=flush; |
1714 | 2.41k | args.offsets=offsets; |
1715 | 2.41k | args.source=s; |
1716 | 2.41k | args.sourceLimit=sourceLimit; |
1717 | 2.41k | args.target=*target; |
1718 | 2.41k | args.targetLimit=targetLimit; |
1719 | 2.41k | args.size=sizeof(args); |
1720 | | |
1721 | 2.41k | _toUnicodeWithCallback(&args, err); |
1722 | | |
1723 | 2.41k | *source=args.source; |
1724 | 2.41k | *target=args.target; |
1725 | 2.41k | } |
1726 | | |
1727 | | /* ucnv_to/fromUChars() ----------------------------------------------------- */ |
1728 | | |
1729 | | U_CAPI int32_t U_EXPORT2 |
1730 | | ucnv_fromUChars(UConverter *cnv, |
1731 | | char *dest, int32_t destCapacity, |
1732 | | const char16_t *src, int32_t srcLength, |
1733 | 0 | UErrorCode *pErrorCode) { |
1734 | 0 | const char16_t *srcLimit; |
1735 | 0 | char *originalDest, *destLimit; |
1736 | 0 | int32_t destLength; |
1737 | | |
1738 | | /* check arguments */ |
1739 | 0 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
1740 | 0 | return 0; |
1741 | 0 | } |
1742 | | |
1743 | 0 | if( cnv==nullptr || |
1744 | 0 | destCapacity<0 || (destCapacity>0 && dest==nullptr) || |
1745 | 0 | srcLength<-1 || (srcLength!=0 && src==nullptr) |
1746 | 0 | ) { |
1747 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
1748 | 0 | return 0; |
1749 | 0 | } |
1750 | | |
1751 | | /* initialize */ |
1752 | 0 | ucnv_resetFromUnicode(cnv); |
1753 | 0 | originalDest=dest; |
1754 | 0 | if(srcLength==-1) { |
1755 | 0 | srcLength=u_strlen(src); |
1756 | 0 | } |
1757 | 0 | if(srcLength>0) { |
1758 | 0 | srcLimit=src+srcLength; |
1759 | 0 | destCapacity=pinCapacity(dest, destCapacity); |
1760 | 0 | destLimit=dest+destCapacity; |
1761 | | |
1762 | | /* perform the conversion */ |
1763 | 0 | UErrorCode bufferStatus = U_ZERO_ERROR; |
1764 | 0 | ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &bufferStatus); |
1765 | 0 | destLength=(int32_t)(dest-originalDest); |
1766 | | |
1767 | | /* if an overflow occurs, then get the preflighting length */ |
1768 | 0 | if(bufferStatus==U_BUFFER_OVERFLOW_ERROR) { |
1769 | 0 | char buffer[1024]; |
1770 | |
|
1771 | 0 | destLimit=buffer+sizeof(buffer); |
1772 | 0 | do { |
1773 | 0 | dest=buffer; |
1774 | 0 | bufferStatus=U_ZERO_ERROR; |
1775 | 0 | ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &bufferStatus); |
1776 | 0 | destLength+=(int32_t)(dest-buffer); |
1777 | 0 | } while(bufferStatus==U_BUFFER_OVERFLOW_ERROR); |
1778 | 0 | } |
1779 | 0 | if (U_FAILURE(bufferStatus)) { |
1780 | 0 | *pErrorCode = bufferStatus; |
1781 | 0 | } |
1782 | 0 | } else { |
1783 | 0 | destLength=0; |
1784 | 0 | } |
1785 | |
|
1786 | 0 | return u_terminateChars(originalDest, destCapacity, destLength, pErrorCode); |
1787 | 0 | } |
1788 | | |
1789 | | U_CAPI int32_t U_EXPORT2 |
1790 | | ucnv_toUChars(UConverter *cnv, |
1791 | | char16_t *dest, int32_t destCapacity, |
1792 | | const char *src, int32_t srcLength, |
1793 | 0 | UErrorCode *pErrorCode) { |
1794 | 0 | const char *srcLimit; |
1795 | 0 | char16_t *originalDest, *destLimit; |
1796 | 0 | int32_t destLength; |
1797 | | |
1798 | | /* check arguments */ |
1799 | 0 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
1800 | 0 | return 0; |
1801 | 0 | } |
1802 | | |
1803 | 0 | if( cnv==nullptr || |
1804 | 0 | destCapacity<0 || (destCapacity>0 && dest==nullptr) || |
1805 | 0 | srcLength<-1 || (srcLength!=0 && src==nullptr)) |
1806 | 0 | { |
1807 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
1808 | 0 | return 0; |
1809 | 0 | } |
1810 | | |
1811 | | /* initialize */ |
1812 | 0 | ucnv_resetToUnicode(cnv); |
1813 | 0 | originalDest=dest; |
1814 | 0 | if(srcLength==-1) { |
1815 | 0 | srcLength=(int32_t)uprv_strlen(src); |
1816 | 0 | } |
1817 | 0 | if(srcLength>0) { |
1818 | 0 | srcLimit=src+srcLength; |
1819 | 0 | destCapacity=pinCapacity(dest, destCapacity); |
1820 | 0 | destLimit=dest+destCapacity; |
1821 | | |
1822 | | /* perform the conversion */ |
1823 | 0 | UErrorCode bufferStatus = U_ZERO_ERROR; |
1824 | 0 | ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &bufferStatus); |
1825 | 0 | destLength=(int32_t)(dest-originalDest); |
1826 | | |
1827 | | /* if an overflow occurs, then get the preflighting length */ |
1828 | 0 | if(bufferStatus==U_BUFFER_OVERFLOW_ERROR) |
1829 | 0 | { |
1830 | 0 | char16_t buffer[1024]; |
1831 | |
|
1832 | 0 | destLimit=buffer+UPRV_LENGTHOF(buffer); |
1833 | 0 | do { |
1834 | 0 | dest=buffer; |
1835 | 0 | bufferStatus=U_ZERO_ERROR; |
1836 | 0 | ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &bufferStatus); |
1837 | 0 | destLength+=(int32_t)(dest-buffer); |
1838 | 0 | } |
1839 | 0 | while(bufferStatus==U_BUFFER_OVERFLOW_ERROR); |
1840 | 0 | } |
1841 | 0 | if (U_FAILURE(bufferStatus)) { |
1842 | 0 | *pErrorCode = bufferStatus; |
1843 | 0 | } |
1844 | 0 | } else { |
1845 | 0 | destLength=0; |
1846 | 0 | } |
1847 | |
|
1848 | 0 | return u_terminateUChars(originalDest, destCapacity, destLength, pErrorCode); |
1849 | 0 | } |
1850 | | |
1851 | | /* ucnv_getNextUChar() ------------------------------------------------------ */ |
1852 | | |
1853 | | U_CAPI UChar32 U_EXPORT2 |
1854 | | ucnv_getNextUChar(UConverter *cnv, |
1855 | | const char **source, const char *sourceLimit, |
1856 | 0 | UErrorCode *err) { |
1857 | 0 | UConverterToUnicodeArgs args; |
1858 | 0 | char16_t buffer[U16_MAX_LENGTH]; |
1859 | 0 | const char *s; |
1860 | 0 | UChar32 c; |
1861 | 0 | int32_t i, length; |
1862 | | |
1863 | | /* check parameters */ |
1864 | 0 | if(err==nullptr || U_FAILURE(*err)) { |
1865 | 0 | return 0xffff; |
1866 | 0 | } |
1867 | | |
1868 | 0 | if(cnv==nullptr || source==nullptr) { |
1869 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1870 | 0 | return 0xffff; |
1871 | 0 | } |
1872 | | |
1873 | 0 | s=*source; |
1874 | 0 | if(sourceLimit<s) { |
1875 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1876 | 0 | return 0xffff; |
1877 | 0 | } |
1878 | | |
1879 | | /* |
1880 | | * Make sure that the buffer sizes do not exceed the number range for |
1881 | | * int32_t because some functions use the size (in units or bytes) |
1882 | | * rather than comparing pointers, and because offsets are int32_t values. |
1883 | | * |
1884 | | * size_t is guaranteed to be unsigned and large enough for the job. |
1885 | | * |
1886 | | * Return with an error instead of adjusting the limits because we would |
1887 | | * not be able to maintain the semantics that either the source must be |
1888 | | * consumed or the target filled (unless an error occurs). |
1889 | | * An adjustment would be sourceLimit=t+0x7fffffff; for example. |
1890 | | */ |
1891 | 0 | if(((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s)) { |
1892 | 0 | *err=U_ILLEGAL_ARGUMENT_ERROR; |
1893 | 0 | return 0xffff; |
1894 | 0 | } |
1895 | | |
1896 | 0 | c=U_SENTINEL; |
1897 | | |
1898 | | /* flush the target overflow buffer */ |
1899 | 0 | if(cnv->UCharErrorBufferLength>0) { |
1900 | 0 | char16_t *overflow; |
1901 | |
|
1902 | 0 | overflow=cnv->UCharErrorBuffer; |
1903 | 0 | i=0; |
1904 | 0 | length=cnv->UCharErrorBufferLength; |
1905 | 0 | U16_NEXT(overflow, i, length, c); |
1906 | | |
1907 | | /* move the remaining overflow contents up to the beginning */ |
1908 | 0 | if((cnv->UCharErrorBufferLength=(int8_t)(length-i))>0) { |
1909 | 0 | uprv_memmove(cnv->UCharErrorBuffer, cnv->UCharErrorBuffer+i, |
1910 | 0 | cnv->UCharErrorBufferLength*U_SIZEOF_UCHAR); |
1911 | 0 | } |
1912 | |
|
1913 | 0 | if(!U16_IS_LEAD(c) || i<length) { |
1914 | 0 | return c; |
1915 | 0 | } |
1916 | | /* |
1917 | | * Continue if the overflow buffer contained only a lead surrogate, |
1918 | | * in case the converter outputs single surrogates from complete |
1919 | | * input sequences. |
1920 | | */ |
1921 | 0 | } |
1922 | | |
1923 | | /* |
1924 | | * flush==true is implied for ucnv_getNextUChar() |
1925 | | * |
1926 | | * do not simply return even if s==sourceLimit because the converter may |
1927 | | * not have seen flush==true before |
1928 | | */ |
1929 | | |
1930 | | /* prepare the converter arguments */ |
1931 | 0 | args.converter=cnv; |
1932 | 0 | args.flush=true; |
1933 | 0 | args.offsets=nullptr; |
1934 | 0 | args.source=s; |
1935 | 0 | args.sourceLimit=sourceLimit; |
1936 | 0 | args.target=buffer; |
1937 | 0 | args.targetLimit=buffer+1; |
1938 | 0 | args.size=sizeof(args); |
1939 | |
|
1940 | 0 | if(c<0) { |
1941 | | /* |
1942 | | * call the native getNextUChar() implementation if we are |
1943 | | * at a character boundary (toULength==0) |
1944 | | * |
1945 | | * unlike with _toUnicode(), getNextUChar() implementations must set |
1946 | | * U_TRUNCATED_CHAR_FOUND for truncated input, |
1947 | | * in addition to setting toULength/toUBytes[] |
1948 | | */ |
1949 | 0 | if(cnv->toULength==0 && cnv->sharedData->impl->getNextUChar!=nullptr) { |
1950 | 0 | c=cnv->sharedData->impl->getNextUChar(&args, err); |
1951 | 0 | *source=s=args.source; |
1952 | 0 | if(*err==U_INDEX_OUTOFBOUNDS_ERROR) { |
1953 | | /* reset the converter without calling the callback function */ |
1954 | 0 | _reset(cnv, UCNV_RESET_TO_UNICODE, false); |
1955 | 0 | return 0xffff; /* no output */ |
1956 | 0 | } else if(U_SUCCESS(*err) && c>=0) { |
1957 | 0 | return c; |
1958 | | /* |
1959 | | * else fall through to use _toUnicode() because |
1960 | | * UCNV_GET_NEXT_UCHAR_USE_TO_U: the native function did not want to handle it after all |
1961 | | * U_FAILURE: call _toUnicode() for callback handling (do not output c) |
1962 | | */ |
1963 | 0 | } |
1964 | 0 | } |
1965 | | |
1966 | | /* convert to one char16_t in buffer[0], or handle getNextUChar() errors */ |
1967 | 0 | _toUnicodeWithCallback(&args, err); |
1968 | |
|
1969 | 0 | if(*err==U_BUFFER_OVERFLOW_ERROR) { |
1970 | 0 | *err=U_ZERO_ERROR; |
1971 | 0 | } |
1972 | |
|
1973 | 0 | i=0; |
1974 | 0 | length=(int32_t)(args.target-buffer); |
1975 | 0 | } else { |
1976 | | /* write the lead surrogate from the overflow buffer */ |
1977 | 0 | buffer[0]=(char16_t)c; |
1978 | 0 | args.target=buffer+1; |
1979 | 0 | i=0; |
1980 | 0 | length=1; |
1981 | 0 | } |
1982 | | |
1983 | | /* buffer contents starts at i and ends before length */ |
1984 | | |
1985 | 0 | if(U_FAILURE(*err)) { |
1986 | 0 | c=0xffff; /* no output */ |
1987 | 0 | } else if(length==0) { |
1988 | | /* no input or only state changes */ |
1989 | 0 | *err=U_INDEX_OUTOFBOUNDS_ERROR; |
1990 | | /* no need to reset explicitly because _toUnicodeWithCallback() did it */ |
1991 | 0 | c=0xffff; /* no output */ |
1992 | 0 | } else { |
1993 | 0 | c=buffer[0]; |
1994 | 0 | i=1; |
1995 | 0 | if(!U16_IS_LEAD(c)) { |
1996 | | /* consume c=buffer[0], done */ |
1997 | 0 | } else { |
1998 | | /* got a lead surrogate, see if a trail surrogate follows */ |
1999 | 0 | char16_t c2; |
2000 | |
|
2001 | 0 | if(cnv->UCharErrorBufferLength>0) { |
2002 | | /* got overflow output from the conversion */ |
2003 | 0 | if(U16_IS_TRAIL(c2=cnv->UCharErrorBuffer[0])) { |
2004 | | /* got a trail surrogate, too */ |
2005 | 0 | c=U16_GET_SUPPLEMENTARY(c, c2); |
2006 | | |
2007 | | /* move the remaining overflow contents up to the beginning */ |
2008 | 0 | if((--cnv->UCharErrorBufferLength)>0) { |
2009 | 0 | uprv_memmove(cnv->UCharErrorBuffer, cnv->UCharErrorBuffer+1, |
2010 | 0 | cnv->UCharErrorBufferLength*U_SIZEOF_UCHAR); |
2011 | 0 | } |
2012 | 0 | } else { |
2013 | | /* c is an unpaired lead surrogate, just return it */ |
2014 | 0 | } |
2015 | 0 | } else if(args.source<sourceLimit) { |
2016 | | /* convert once more, to buffer[1] */ |
2017 | 0 | args.targetLimit=buffer+2; |
2018 | 0 | _toUnicodeWithCallback(&args, err); |
2019 | 0 | if(*err==U_BUFFER_OVERFLOW_ERROR) { |
2020 | 0 | *err=U_ZERO_ERROR; |
2021 | 0 | } |
2022 | |
|
2023 | 0 | length=(int32_t)(args.target-buffer); |
2024 | 0 | if(U_SUCCESS(*err) && length==2 && U16_IS_TRAIL(c2=buffer[1])) { |
2025 | | /* got a trail surrogate, too */ |
2026 | 0 | c=U16_GET_SUPPLEMENTARY(c, c2); |
2027 | 0 | i=2; |
2028 | 0 | } |
2029 | 0 | } |
2030 | 0 | } |
2031 | 0 | } |
2032 | | |
2033 | | /* |
2034 | | * move leftover output from buffer[i..length[ |
2035 | | * into the beginning of the overflow buffer |
2036 | | */ |
2037 | 0 | if(i<length) { |
2038 | | /* move further overflow back */ |
2039 | 0 | int32_t delta=length-i; |
2040 | 0 | if((length=cnv->UCharErrorBufferLength)>0) { |
2041 | 0 | uprv_memmove(cnv->UCharErrorBuffer+delta, cnv->UCharErrorBuffer, |
2042 | 0 | length*U_SIZEOF_UCHAR); |
2043 | 0 | } |
2044 | 0 | cnv->UCharErrorBufferLength=(int8_t)(length+delta); |
2045 | |
|
2046 | 0 | cnv->UCharErrorBuffer[0]=buffer[i++]; |
2047 | 0 | if(delta>1) { |
2048 | 0 | cnv->UCharErrorBuffer[1]=buffer[i]; |
2049 | 0 | } |
2050 | 0 | } |
2051 | |
|
2052 | 0 | *source=args.source; |
2053 | 0 | return c; |
2054 | 0 | } |
2055 | | |
2056 | | /* ucnv_convert() and siblings ---------------------------------------------- */ |
2057 | | |
2058 | | U_CAPI void U_EXPORT2 |
2059 | | ucnv_convertEx(UConverter *targetCnv, UConverter *sourceCnv, |
2060 | | char **target, const char *targetLimit, |
2061 | | const char **source, const char *sourceLimit, |
2062 | | char16_t *pivotStart, char16_t **pivotSource, |
2063 | | char16_t **pivotTarget, const char16_t *pivotLimit, |
2064 | | UBool reset, UBool flush, |
2065 | 0 | UErrorCode *pErrorCode) { |
2066 | 0 | char16_t pivotBuffer[CHUNK_SIZE]; |
2067 | 0 | const char16_t *myPivotSource; |
2068 | 0 | char16_t *myPivotTarget; |
2069 | 0 | const char *s; |
2070 | 0 | char *t; |
2071 | |
|
2072 | 0 | UConverterToUnicodeArgs toUArgs; |
2073 | 0 | UConverterFromUnicodeArgs fromUArgs; |
2074 | 0 | UConverterConvert convert; |
2075 | | |
2076 | | /* error checking */ |
2077 | 0 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
2078 | 0 | return; |
2079 | 0 | } |
2080 | | |
2081 | 0 | if( targetCnv==nullptr || sourceCnv==nullptr || |
2082 | 0 | source==nullptr || *source==nullptr || |
2083 | 0 | target==nullptr || *target==nullptr || targetLimit==nullptr |
2084 | 0 | ) { |
2085 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2086 | 0 | return; |
2087 | 0 | } |
2088 | | |
2089 | 0 | s=*source; |
2090 | 0 | t=*target; |
2091 | 0 | if((sourceLimit!=nullptr && sourceLimit<s) || targetLimit<t) { |
2092 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2093 | 0 | return; |
2094 | 0 | } |
2095 | | |
2096 | | /* |
2097 | | * Make sure that the buffer sizes do not exceed the number range for |
2098 | | * int32_t. See ucnv_toUnicode() for a more detailed comment. |
2099 | | */ |
2100 | 0 | if( |
2101 | 0 | (sourceLimit!=nullptr && ((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s)) || |
2102 | 0 | ((size_t)(targetLimit-t)>(size_t)0x7fffffff && targetLimit>t) |
2103 | 0 | ) { |
2104 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2105 | 0 | return; |
2106 | 0 | } |
2107 | | |
2108 | 0 | if(pivotStart==nullptr) { |
2109 | 0 | if(!flush) { |
2110 | | /* streaming conversion requires an explicit pivot buffer */ |
2111 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2112 | 0 | return; |
2113 | 0 | } |
2114 | | |
2115 | | /* use the stack pivot buffer */ |
2116 | 0 | myPivotSource=myPivotTarget=pivotStart=pivotBuffer; |
2117 | 0 | pivotSource=(char16_t **)&myPivotSource; |
2118 | 0 | pivotTarget=&myPivotTarget; |
2119 | 0 | pivotLimit=pivotBuffer+CHUNK_SIZE; |
2120 | 0 | } else if( pivotStart>=pivotLimit || |
2121 | 0 | pivotSource==nullptr || *pivotSource==nullptr || |
2122 | 0 | pivotTarget==nullptr || *pivotTarget==nullptr || |
2123 | 0 | pivotLimit==nullptr |
2124 | 0 | ) { |
2125 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2126 | 0 | return; |
2127 | 0 | } |
2128 | | |
2129 | 0 | if(sourceLimit==nullptr) { |
2130 | | /* get limit of single-byte-NUL-terminated source string */ |
2131 | 0 | sourceLimit=uprv_strchr(*source, 0); |
2132 | 0 | } |
2133 | |
|
2134 | 0 | if(reset) { |
2135 | 0 | ucnv_resetToUnicode(sourceCnv); |
2136 | 0 | ucnv_resetFromUnicode(targetCnv); |
2137 | 0 | *pivotSource=*pivotTarget=pivotStart; |
2138 | 0 | } else if(targetCnv->charErrorBufferLength>0) { |
2139 | | /* output the targetCnv overflow buffer */ |
2140 | 0 | if(ucnv_outputOverflowFromUnicode(targetCnv, target, targetLimit, nullptr, pErrorCode)) { |
2141 | | /* U_BUFFER_OVERFLOW_ERROR */ |
2142 | 0 | return; |
2143 | 0 | } |
2144 | | /* *target has moved, therefore stop using t */ |
2145 | | |
2146 | 0 | if( !flush && |
2147 | 0 | targetCnv->preFromULength>=0 && *pivotSource==*pivotTarget && |
2148 | 0 | sourceCnv->UCharErrorBufferLength==0 && sourceCnv->preToULength>=0 && s==sourceLimit |
2149 | 0 | ) { |
2150 | | /* the fromUnicode overflow buffer is emptied and there is no new input: we are done */ |
2151 | 0 | return; |
2152 | 0 | } |
2153 | 0 | } |
2154 | | |
2155 | | /* Is direct-UTF-8 conversion available? */ |
2156 | 0 | if( sourceCnv->sharedData->staticData->conversionType==UCNV_UTF8 && |
2157 | 0 | targetCnv->sharedData->impl->fromUTF8!=nullptr |
2158 | 0 | ) { |
2159 | 0 | convert=targetCnv->sharedData->impl->fromUTF8; |
2160 | 0 | } else if( targetCnv->sharedData->staticData->conversionType==UCNV_UTF8 && |
2161 | 0 | sourceCnv->sharedData->impl->toUTF8!=nullptr |
2162 | 0 | ) { |
2163 | 0 | convert=sourceCnv->sharedData->impl->toUTF8; |
2164 | 0 | } else { |
2165 | 0 | convert=nullptr; |
2166 | 0 | } |
2167 | | |
2168 | | /* |
2169 | | * If direct-UTF-8 conversion is available, then we use a smaller |
2170 | | * pivot buffer for error handling and partial matches |
2171 | | * so that we quickly return to direct conversion. |
2172 | | * |
2173 | | * 32 is large enough for UCNV_EXT_MAX_UCHARS and UCNV_ERROR_BUFFER_LENGTH. |
2174 | | * |
2175 | | * We could reduce the pivot buffer size further, at the cost of |
2176 | | * buffer overflows from callbacks. |
2177 | | * The pivot buffer should not be smaller than the maximum number of |
2178 | | * fromUnicode extension table input UChars |
2179 | | * (for m:n conversion, see |
2180 | | * targetCnv->sharedData->mbcs.extIndexes[UCNV_EXT_COUNT_UCHARS]) |
2181 | | * or 2 for surrogate pairs. |
2182 | | * |
2183 | | * Too small a buffer can cause thrashing between pivoting and direct |
2184 | | * conversion, with function call overhead outweighing the benefits |
2185 | | * of direct conversion. |
2186 | | */ |
2187 | 0 | if(convert!=nullptr && (pivotLimit-pivotStart)>32) { |
2188 | 0 | pivotLimit=pivotStart+32; |
2189 | 0 | } |
2190 | | |
2191 | | /* prepare the converter arguments */ |
2192 | 0 | fromUArgs.converter=targetCnv; |
2193 | 0 | fromUArgs.flush=false; |
2194 | 0 | fromUArgs.offsets=nullptr; |
2195 | 0 | fromUArgs.target=*target; |
2196 | 0 | fromUArgs.targetLimit=targetLimit; |
2197 | 0 | fromUArgs.size=sizeof(fromUArgs); |
2198 | |
|
2199 | 0 | toUArgs.converter=sourceCnv; |
2200 | 0 | toUArgs.flush=flush; |
2201 | 0 | toUArgs.offsets=nullptr; |
2202 | 0 | toUArgs.source=s; |
2203 | 0 | toUArgs.sourceLimit=sourceLimit; |
2204 | 0 | toUArgs.targetLimit=pivotLimit; |
2205 | 0 | toUArgs.size=sizeof(toUArgs); |
2206 | | |
2207 | | /* |
2208 | | * TODO: Consider separating this function into two functions, |
2209 | | * extracting exactly the conversion loop, |
2210 | | * for readability and to reduce the set of visible variables. |
2211 | | * |
2212 | | * Otherwise stop using s and t from here on. |
2213 | | */ |
2214 | 0 | s=t=nullptr; |
2215 | | |
2216 | | /* |
2217 | | * conversion loop |
2218 | | * |
2219 | | * The sequence of steps in the loop may appear backward, |
2220 | | * but the principle is simple: |
2221 | | * In the chain of |
2222 | | * source - sourceCnv overflow - pivot - targetCnv overflow - target |
2223 | | * empty out later buffers before refilling them from earlier ones. |
2224 | | * |
2225 | | * The targetCnv overflow buffer is flushed out only once before the loop. |
2226 | | */ |
2227 | 0 | for(;;) { |
2228 | | /* |
2229 | | * if(pivot not empty or error or replay or flush fromUnicode) { |
2230 | | * fromUnicode(pivot -> target); |
2231 | | * } |
2232 | | * |
2233 | | * For pivoting conversion; and for direct conversion for |
2234 | | * error callback handling and flushing the replay buffer. |
2235 | | */ |
2236 | 0 | if( *pivotSource<*pivotTarget || |
2237 | 0 | U_FAILURE(*pErrorCode) || |
2238 | 0 | targetCnv->preFromULength<0 || |
2239 | 0 | fromUArgs.flush |
2240 | 0 | ) { |
2241 | 0 | fromUArgs.source=*pivotSource; |
2242 | 0 | fromUArgs.sourceLimit=*pivotTarget; |
2243 | 0 | _fromUnicodeWithCallback(&fromUArgs, pErrorCode); |
2244 | 0 | if(U_FAILURE(*pErrorCode)) { |
2245 | | /* target overflow, or conversion error */ |
2246 | 0 | *pivotSource=(char16_t *)fromUArgs.source; |
2247 | 0 | break; |
2248 | 0 | } |
2249 | | |
2250 | | /* |
2251 | | * _fromUnicodeWithCallback() must have consumed the pivot contents |
2252 | | * (*pivotSource==*pivotTarget) since it returned with U_SUCCESS() |
2253 | | */ |
2254 | 0 | } |
2255 | | |
2256 | | /* The pivot buffer is empty; reset it so we start at pivotStart. */ |
2257 | 0 | *pivotSource=*pivotTarget=pivotStart; |
2258 | | |
2259 | | /* |
2260 | | * if(sourceCnv overflow buffer not empty) { |
2261 | | * move(sourceCnv overflow buffer -> pivot); |
2262 | | * continue; |
2263 | | * } |
2264 | | */ |
2265 | | /* output the sourceCnv overflow buffer */ |
2266 | 0 | if(sourceCnv->UCharErrorBufferLength>0) { |
2267 | 0 | if(ucnv_outputOverflowToUnicode(sourceCnv, pivotTarget, pivotLimit, nullptr, pErrorCode)) { |
2268 | | /* U_BUFFER_OVERFLOW_ERROR */ |
2269 | 0 | *pErrorCode=U_ZERO_ERROR; |
2270 | 0 | } |
2271 | 0 | continue; |
2272 | 0 | } |
2273 | | |
2274 | | /* |
2275 | | * check for end of input and break if done |
2276 | | * |
2277 | | * Checking both flush and fromUArgs.flush ensures that the converters |
2278 | | * have been called with the flush flag set if the ucnv_convertEx() |
2279 | | * caller set it. |
2280 | | */ |
2281 | 0 | if( toUArgs.source==sourceLimit && |
2282 | 0 | sourceCnv->preToULength>=0 && sourceCnv->toULength==0 && |
2283 | 0 | (!flush || fromUArgs.flush) |
2284 | 0 | ) { |
2285 | | /* done successfully */ |
2286 | 0 | break; |
2287 | 0 | } |
2288 | | |
2289 | | /* |
2290 | | * use direct conversion if available |
2291 | | * but not if continuing a partial match |
2292 | | * or flushing the toUnicode replay buffer |
2293 | | */ |
2294 | 0 | if(convert!=nullptr && targetCnv->preFromUFirstCP<0 && sourceCnv->preToULength==0) { |
2295 | 0 | if(*pErrorCode==U_USING_DEFAULT_WARNING) { |
2296 | | /* remove a warning that may be set by this function */ |
2297 | 0 | *pErrorCode=U_ZERO_ERROR; |
2298 | 0 | } |
2299 | 0 | convert(&fromUArgs, &toUArgs, pErrorCode); |
2300 | 0 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) { |
2301 | 0 | break; |
2302 | 0 | } else if(U_FAILURE(*pErrorCode)) { |
2303 | 0 | if(sourceCnv->toULength>0) { |
2304 | | /* |
2305 | | * Fall through to calling _toUnicodeWithCallback() |
2306 | | * for callback handling. |
2307 | | * |
2308 | | * The pivot buffer will be reset with |
2309 | | * *pivotSource=*pivotTarget=pivotStart; |
2310 | | * which indicates a toUnicode error to the caller |
2311 | | * (*pivotSource==pivotStart shows no pivot UChars consumed). |
2312 | | */ |
2313 | 0 | } else { |
2314 | | /* |
2315 | | * Indicate a fromUnicode error to the caller |
2316 | | * (*pivotSource>pivotStart shows some pivot UChars consumed). |
2317 | | */ |
2318 | 0 | *pivotSource=*pivotTarget=pivotStart+1; |
2319 | | /* |
2320 | | * Loop around to calling _fromUnicodeWithCallbacks() |
2321 | | * for callback handling. |
2322 | | */ |
2323 | 0 | continue; |
2324 | 0 | } |
2325 | 0 | } else if(*pErrorCode==U_USING_DEFAULT_WARNING) { |
2326 | | /* |
2327 | | * No error, but the implementation requested to temporarily |
2328 | | * fall back to pivoting. |
2329 | | */ |
2330 | 0 | *pErrorCode=U_ZERO_ERROR; |
2331 | | /* |
2332 | | * The following else branches are almost identical to the end-of-input |
2333 | | * handling in _toUnicodeWithCallback(). |
2334 | | * Avoid calling it just for the end of input. |
2335 | | */ |
2336 | 0 | } else if(flush && sourceCnv->toULength>0) { /* flush==toUArgs.flush */ |
2337 | | /* |
2338 | | * the entire input stream is consumed |
2339 | | * and there is a partial, truncated input sequence left |
2340 | | */ |
2341 | | |
2342 | | /* inject an error and continue with callback handling */ |
2343 | 0 | *pErrorCode=U_TRUNCATED_CHAR_FOUND; |
2344 | 0 | } else { |
2345 | | /* input consumed */ |
2346 | 0 | if(flush) { |
2347 | | /* reset the converters without calling the callback functions */ |
2348 | 0 | _reset(sourceCnv, UCNV_RESET_TO_UNICODE, false); |
2349 | 0 | _reset(targetCnv, UCNV_RESET_FROM_UNICODE, false); |
2350 | 0 | } |
2351 | | |
2352 | | /* done successfully */ |
2353 | 0 | break; |
2354 | 0 | } |
2355 | 0 | } |
2356 | | |
2357 | | /* |
2358 | | * toUnicode(source -> pivot); |
2359 | | * |
2360 | | * For pivoting conversion; and for direct conversion for |
2361 | | * error callback handling, continuing partial matches |
2362 | | * and flushing the replay buffer. |
2363 | | * |
2364 | | * The pivot buffer is empty and reset. |
2365 | | */ |
2366 | 0 | toUArgs.target=pivotStart; /* ==*pivotTarget */ |
2367 | | /* toUArgs.targetLimit=pivotLimit; already set before the loop */ |
2368 | 0 | _toUnicodeWithCallback(&toUArgs, pErrorCode); |
2369 | 0 | *pivotTarget=toUArgs.target; |
2370 | 0 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) { |
2371 | | /* pivot overflow: continue with the conversion loop */ |
2372 | 0 | *pErrorCode=U_ZERO_ERROR; |
2373 | 0 | } else if(U_FAILURE(*pErrorCode) || (!flush && *pivotTarget==pivotStart)) { |
2374 | | /* conversion error, or there was nothing left to convert */ |
2375 | 0 | break; |
2376 | 0 | } |
2377 | | /* |
2378 | | * else: |
2379 | | * _toUnicodeWithCallback() wrote into the pivot buffer, |
2380 | | * continue with fromUnicode conversion. |
2381 | | * |
2382 | | * Set the fromUnicode flush flag if we flush and if toUnicode has |
2383 | | * processed the end of the input. |
2384 | | */ |
2385 | 0 | if( flush && toUArgs.source==sourceLimit && |
2386 | 0 | sourceCnv->preToULength>=0 && |
2387 | 0 | sourceCnv->UCharErrorBufferLength==0 |
2388 | 0 | ) { |
2389 | 0 | fromUArgs.flush=true; |
2390 | 0 | } |
2391 | 0 | } |
2392 | | |
2393 | | /* |
2394 | | * The conversion loop is exited when one of the following is true: |
2395 | | * - the entire source text has been converted successfully to the target buffer |
2396 | | * - a target buffer overflow occurred |
2397 | | * - a conversion error occurred |
2398 | | */ |
2399 | |
|
2400 | 0 | *source=toUArgs.source; |
2401 | 0 | *target=fromUArgs.target; |
2402 | | |
2403 | | /* terminate the target buffer if possible */ |
2404 | 0 | if(flush && U_SUCCESS(*pErrorCode)) { |
2405 | 0 | if(*target!=targetLimit) { |
2406 | 0 | **target=0; |
2407 | 0 | if(*pErrorCode==U_STRING_NOT_TERMINATED_WARNING) { |
2408 | 0 | *pErrorCode=U_ZERO_ERROR; |
2409 | 0 | } |
2410 | 0 | } else { |
2411 | 0 | *pErrorCode=U_STRING_NOT_TERMINATED_WARNING; |
2412 | 0 | } |
2413 | 0 | } |
2414 | 0 | } |
2415 | | |
2416 | | /* internal implementation of ucnv_convert() etc. with preflighting */ |
2417 | | static int32_t |
2418 | | ucnv_internalConvert(UConverter *outConverter, UConverter *inConverter, |
2419 | | char *target, int32_t targetCapacity, |
2420 | | const char *source, int32_t sourceLength, |
2421 | 0 | UErrorCode *pErrorCode) { |
2422 | 0 | char16_t pivotBuffer[CHUNK_SIZE]; |
2423 | 0 | char16_t *pivot, *pivot2; |
2424 | |
|
2425 | 0 | char *myTarget; |
2426 | 0 | const char *sourceLimit; |
2427 | 0 | const char *targetLimit; |
2428 | 0 | int32_t targetLength=0; |
2429 | | |
2430 | | /* set up */ |
2431 | 0 | if(sourceLength<0) { |
2432 | 0 | sourceLimit=uprv_strchr(source, 0); |
2433 | 0 | } else { |
2434 | 0 | sourceLimit=source+sourceLength; |
2435 | 0 | } |
2436 | | |
2437 | | /* if there is no input data, we're done */ |
2438 | 0 | if(source==sourceLimit) { |
2439 | 0 | return u_terminateChars(target, targetCapacity, 0, pErrorCode); |
2440 | 0 | } |
2441 | | |
2442 | 0 | pivot=pivot2=pivotBuffer; |
2443 | 0 | myTarget=target; |
2444 | 0 | targetLength=0; |
2445 | |
|
2446 | 0 | if(targetCapacity>0) { |
2447 | | /* perform real conversion */ |
2448 | 0 | targetLimit=target+targetCapacity; |
2449 | 0 | ucnv_convertEx(outConverter, inConverter, |
2450 | 0 | &myTarget, targetLimit, |
2451 | 0 | &source, sourceLimit, |
2452 | 0 | pivotBuffer, &pivot, &pivot2, pivotBuffer+CHUNK_SIZE, |
2453 | 0 | false, |
2454 | 0 | true, |
2455 | 0 | pErrorCode); |
2456 | 0 | targetLength = static_cast<int32_t>(myTarget - target); |
2457 | 0 | } |
2458 | | |
2459 | | /* |
2460 | | * If the output buffer is exhausted (or we are only "preflighting"), we need to stop writing |
2461 | | * to it but continue the conversion in order to store in targetCapacity |
2462 | | * the number of bytes that was required. |
2463 | | */ |
2464 | 0 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || targetCapacity==0) |
2465 | 0 | { |
2466 | 0 | char targetBuffer[CHUNK_SIZE]; |
2467 | |
|
2468 | 0 | targetLimit=targetBuffer+CHUNK_SIZE; |
2469 | 0 | do { |
2470 | 0 | *pErrorCode=U_ZERO_ERROR; |
2471 | 0 | myTarget=targetBuffer; |
2472 | 0 | ucnv_convertEx(outConverter, inConverter, |
2473 | 0 | &myTarget, targetLimit, |
2474 | 0 | &source, sourceLimit, |
2475 | 0 | pivotBuffer, &pivot, &pivot2, pivotBuffer+CHUNK_SIZE, |
2476 | 0 | false, |
2477 | 0 | true, |
2478 | 0 | pErrorCode); |
2479 | 0 | targetLength += static_cast<int32_t>(myTarget - targetBuffer); |
2480 | 0 | } while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR); |
2481 | | |
2482 | | /* done with preflighting, set warnings and errors as appropriate */ |
2483 | 0 | return u_terminateChars(target, targetCapacity, targetLength, pErrorCode); |
2484 | 0 | } |
2485 | | |
2486 | | /* no need to call u_terminateChars() because ucnv_convertEx() took care of that */ |
2487 | 0 | return targetLength; |
2488 | 0 | } |
2489 | | |
2490 | | U_CAPI int32_t U_EXPORT2 |
2491 | | ucnv_convert(const char *toConverterName, const char *fromConverterName, |
2492 | | char *target, int32_t targetCapacity, |
2493 | | const char *source, int32_t sourceLength, |
2494 | 0 | UErrorCode *pErrorCode) { |
2495 | 0 | UConverter in, out; /* stack-allocated */ |
2496 | 0 | UConverter *inConverter, *outConverter; |
2497 | 0 | int32_t targetLength; |
2498 | |
|
2499 | 0 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
2500 | 0 | return 0; |
2501 | 0 | } |
2502 | | |
2503 | 0 | if( source==nullptr || sourceLength<-1 || |
2504 | 0 | targetCapacity<0 || (targetCapacity>0 && target==nullptr) |
2505 | 0 | ) { |
2506 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2507 | 0 | return 0; |
2508 | 0 | } |
2509 | | |
2510 | | /* if there is no input data, we're done */ |
2511 | 0 | if(sourceLength==0 || (sourceLength<0 && *source==0)) { |
2512 | 0 | return u_terminateChars(target, targetCapacity, 0, pErrorCode); |
2513 | 0 | } |
2514 | | |
2515 | | /* create the converters */ |
2516 | 0 | inConverter=ucnv_createConverter(&in, fromConverterName, pErrorCode); |
2517 | 0 | if(U_FAILURE(*pErrorCode)) { |
2518 | 0 | return 0; |
2519 | 0 | } |
2520 | | |
2521 | 0 | outConverter=ucnv_createConverter(&out, toConverterName, pErrorCode); |
2522 | 0 | if(U_FAILURE(*pErrorCode)) { |
2523 | 0 | ucnv_close(inConverter); |
2524 | 0 | return 0; |
2525 | 0 | } |
2526 | | |
2527 | 0 | targetLength=ucnv_internalConvert(outConverter, inConverter, |
2528 | 0 | target, targetCapacity, |
2529 | 0 | source, sourceLength, |
2530 | 0 | pErrorCode); |
2531 | |
|
2532 | 0 | ucnv_close(inConverter); |
2533 | 0 | ucnv_close(outConverter); |
2534 | |
|
2535 | 0 | return targetLength; |
2536 | 0 | } |
2537 | | |
2538 | | /* @internal */ |
2539 | | static int32_t |
2540 | | ucnv_convertAlgorithmic(UBool convertToAlgorithmic, |
2541 | | UConverterType algorithmicType, |
2542 | | UConverter *cnv, |
2543 | | char *target, int32_t targetCapacity, |
2544 | | const char *source, int32_t sourceLength, |
2545 | 0 | UErrorCode *pErrorCode) { |
2546 | 0 | UConverter algoConverterStatic; /* stack-allocated */ |
2547 | 0 | UConverter *algoConverter, *to, *from; |
2548 | 0 | int32_t targetLength; |
2549 | |
|
2550 | 0 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
2551 | 0 | return 0; |
2552 | 0 | } |
2553 | | |
2554 | 0 | if( cnv==nullptr || source==nullptr || sourceLength<-1 || |
2555 | 0 | targetCapacity<0 || (targetCapacity>0 && target==nullptr) |
2556 | 0 | ) { |
2557 | 0 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2558 | 0 | return 0; |
2559 | 0 | } |
2560 | | |
2561 | | /* if there is no input data, we're done */ |
2562 | 0 | if(sourceLength==0 || (sourceLength<0 && *source==0)) { |
2563 | 0 | return u_terminateChars(target, targetCapacity, 0, pErrorCode); |
2564 | 0 | } |
2565 | | |
2566 | | /* create the algorithmic converter */ |
2567 | 0 | algoConverter=ucnv_createAlgorithmicConverter(&algoConverterStatic, algorithmicType, |
2568 | 0 | "", 0, pErrorCode); |
2569 | 0 | if(U_FAILURE(*pErrorCode)) { |
2570 | 0 | return 0; |
2571 | 0 | } |
2572 | | |
2573 | | /* reset the other converter */ |
2574 | 0 | if(convertToAlgorithmic) { |
2575 | | /* cnv->Unicode->algo */ |
2576 | 0 | ucnv_resetToUnicode(cnv); |
2577 | 0 | to=algoConverter; |
2578 | 0 | from=cnv; |
2579 | 0 | } else { |
2580 | | /* algo->Unicode->cnv */ |
2581 | 0 | ucnv_resetFromUnicode(cnv); |
2582 | 0 | from=algoConverter; |
2583 | 0 | to=cnv; |
2584 | 0 | } |
2585 | |
|
2586 | 0 | targetLength=ucnv_internalConvert(to, from, |
2587 | 0 | target, targetCapacity, |
2588 | 0 | source, sourceLength, |
2589 | 0 | pErrorCode); |
2590 | |
|
2591 | 0 | ucnv_close(algoConverter); |
2592 | |
|
2593 | 0 | return targetLength; |
2594 | 0 | } |
2595 | | |
2596 | | U_CAPI int32_t U_EXPORT2 |
2597 | | ucnv_toAlgorithmic(UConverterType algorithmicType, |
2598 | | UConverter *cnv, |
2599 | | char *target, int32_t targetCapacity, |
2600 | | const char *source, int32_t sourceLength, |
2601 | 0 | UErrorCode *pErrorCode) { |
2602 | 0 | return ucnv_convertAlgorithmic(true, algorithmicType, cnv, |
2603 | 0 | target, targetCapacity, |
2604 | 0 | source, sourceLength, |
2605 | 0 | pErrorCode); |
2606 | 0 | } |
2607 | | |
2608 | | U_CAPI int32_t U_EXPORT2 |
2609 | | ucnv_fromAlgorithmic(UConverter *cnv, |
2610 | | UConverterType algorithmicType, |
2611 | | char *target, int32_t targetCapacity, |
2612 | | const char *source, int32_t sourceLength, |
2613 | 0 | UErrorCode *pErrorCode) UPRV_NO_SANITIZE_UNDEFINED { |
2614 | |
|
2615 | 0 | if(algorithmicType<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES<=algorithmicType) { |
2616 | 0 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
2617 | 0 | return 0; |
2618 | 0 | } |
2619 | 0 | return ucnv_convertAlgorithmic(false, algorithmicType, cnv, |
2620 | 0 | target, targetCapacity, |
2621 | 0 | source, sourceLength, |
2622 | 0 | pErrorCode); |
2623 | 0 | } |
2624 | | |
2625 | | U_CAPI UConverterType U_EXPORT2 |
2626 | | ucnv_getType(const UConverter* converter) |
2627 | 0 | { |
2628 | 0 | int8_t type = converter->sharedData->staticData->conversionType; |
2629 | 0 | #if !UCONFIG_NO_LEGACY_CONVERSION |
2630 | 0 | if(type == UCNV_MBCS) { |
2631 | 0 | return ucnv_MBCSGetType(converter); |
2632 | 0 | } |
2633 | 0 | #endif |
2634 | 0 | return (UConverterType)type; |
2635 | 0 | } |
2636 | | |
2637 | | U_CAPI void U_EXPORT2 |
2638 | | ucnv_getStarters(const UConverter* converter, |
2639 | | UBool starters[256], |
2640 | | UErrorCode* err) |
2641 | 0 | { |
2642 | 0 | if (err == nullptr || U_FAILURE(*err)) { |
2643 | 0 | return; |
2644 | 0 | } |
2645 | | |
2646 | 0 | if(converter->sharedData->impl->getStarters != nullptr) { |
2647 | 0 | converter->sharedData->impl->getStarters(converter, starters, err); |
2648 | 0 | } else { |
2649 | 0 | *err = U_ILLEGAL_ARGUMENT_ERROR; |
2650 | 0 | } |
2651 | 0 | } |
2652 | | |
2653 | | static const UAmbiguousConverter *ucnv_getAmbiguous(const UConverter *cnv) |
2654 | 0 | { |
2655 | 0 | UErrorCode errorCode; |
2656 | 0 | const char *name; |
2657 | 0 | int32_t i; |
2658 | |
|
2659 | 0 | if(cnv==nullptr) { |
2660 | 0 | return nullptr; |
2661 | 0 | } |
2662 | | |
2663 | 0 | errorCode=U_ZERO_ERROR; |
2664 | 0 | name=ucnv_getName(cnv, &errorCode); |
2665 | 0 | if(U_FAILURE(errorCode)) { |
2666 | 0 | return nullptr; |
2667 | 0 | } |
2668 | | |
2669 | 0 | for(i=0; i<UPRV_LENGTHOF(ambiguousConverters); ++i) |
2670 | 0 | { |
2671 | 0 | if(0==uprv_strcmp(name, ambiguousConverters[i].name)) |
2672 | 0 | { |
2673 | 0 | return ambiguousConverters+i; |
2674 | 0 | } |
2675 | 0 | } |
2676 | | |
2677 | 0 | return nullptr; |
2678 | 0 | } |
2679 | | |
2680 | | U_CAPI void U_EXPORT2 |
2681 | | ucnv_fixFileSeparator(const UConverter *cnv, |
2682 | | char16_t* source, |
2683 | 0 | int32_t sourceLength) { |
2684 | 0 | const UAmbiguousConverter *a; |
2685 | 0 | int32_t i; |
2686 | 0 | char16_t variant5c; |
2687 | |
|
2688 | 0 | if(cnv==nullptr || source==nullptr || sourceLength<=0 || (a=ucnv_getAmbiguous(cnv))==nullptr) |
2689 | 0 | { |
2690 | 0 | return; |
2691 | 0 | } |
2692 | | |
2693 | 0 | variant5c=a->variant5c; |
2694 | 0 | for(i=0; i<sourceLength; ++i) { |
2695 | 0 | if(source[i]==variant5c) { |
2696 | 0 | source[i]=0x5c; |
2697 | 0 | } |
2698 | 0 | } |
2699 | 0 | } |
2700 | | |
2701 | | U_CAPI UBool U_EXPORT2 |
2702 | 0 | ucnv_isAmbiguous(const UConverter *cnv) { |
2703 | 0 | return ucnv_getAmbiguous(cnv)!=nullptr; |
2704 | 0 | } |
2705 | | |
2706 | | U_CAPI void U_EXPORT2 |
2707 | | ucnv_setFallback(UConverter *cnv, UBool usesFallback) |
2708 | 0 | { |
2709 | 0 | cnv->useFallback = usesFallback; |
2710 | 0 | } |
2711 | | |
2712 | | U_CAPI UBool U_EXPORT2 |
2713 | | ucnv_usesFallback(const UConverter *cnv) |
2714 | 0 | { |
2715 | 0 | return cnv->useFallback; |
2716 | 0 | } |
2717 | | |
2718 | | U_CAPI void U_EXPORT2 |
2719 | | ucnv_getInvalidChars (const UConverter * converter, |
2720 | | char *errBytes, |
2721 | | int8_t * len, |
2722 | | UErrorCode * err) |
2723 | 0 | { |
2724 | 0 | if (err == nullptr || U_FAILURE(*err)) |
2725 | 0 | { |
2726 | 0 | return; |
2727 | 0 | } |
2728 | 0 | if (len == nullptr || errBytes == nullptr || converter == nullptr) |
2729 | 0 | { |
2730 | 0 | *err = U_ILLEGAL_ARGUMENT_ERROR; |
2731 | 0 | return; |
2732 | 0 | } |
2733 | 0 | if (*len < converter->invalidCharLength) |
2734 | 0 | { |
2735 | 0 | *err = U_INDEX_OUTOFBOUNDS_ERROR; |
2736 | 0 | return; |
2737 | 0 | } |
2738 | 0 | if ((*len = converter->invalidCharLength) > 0) |
2739 | 0 | { |
2740 | 0 | uprv_memcpy (errBytes, converter->invalidCharBuffer, *len); |
2741 | 0 | } |
2742 | 0 | } |
2743 | | |
2744 | | U_CAPI void U_EXPORT2 |
2745 | | ucnv_getInvalidUChars (const UConverter * converter, |
2746 | | char16_t *errChars, |
2747 | | int8_t * len, |
2748 | | UErrorCode * err) |
2749 | 0 | { |
2750 | 0 | if (err == nullptr || U_FAILURE(*err)) |
2751 | 0 | { |
2752 | 0 | return; |
2753 | 0 | } |
2754 | 0 | if (len == nullptr || errChars == nullptr || converter == nullptr) |
2755 | 0 | { |
2756 | 0 | *err = U_ILLEGAL_ARGUMENT_ERROR; |
2757 | 0 | return; |
2758 | 0 | } |
2759 | 0 | if (*len < converter->invalidUCharLength) |
2760 | 0 | { |
2761 | 0 | *err = U_INDEX_OUTOFBOUNDS_ERROR; |
2762 | 0 | return; |
2763 | 0 | } |
2764 | 0 | if ((*len = converter->invalidUCharLength) > 0) |
2765 | 0 | { |
2766 | 0 | u_memcpy (errChars, converter->invalidUCharBuffer, *len); |
2767 | 0 | } |
2768 | 0 | } |
2769 | | |
2770 | 0 | #define SIG_MAX_LEN 5 |
2771 | | |
2772 | | U_CAPI const char* U_EXPORT2 |
2773 | | ucnv_detectUnicodeSignature( const char* source, |
2774 | | int32_t sourceLength, |
2775 | | int32_t* signatureLength, |
2776 | 0 | UErrorCode* pErrorCode) { |
2777 | 0 | int32_t dummy; |
2778 | | |
2779 | | /* initial 0xa5 bytes: make sure that if we read <SIG_MAX_LEN |
2780 | | * bytes we don't misdetect something |
2781 | | */ |
2782 | 0 | char start[SIG_MAX_LEN]={ '\xa5', '\xa5', '\xa5', '\xa5', '\xa5' }; |
2783 | 0 | int i = 0; |
2784 | |
|
2785 | 0 | if((pErrorCode==nullptr) || U_FAILURE(*pErrorCode)){ |
2786 | 0 | return nullptr; |
2787 | 0 | } |
2788 | | |
2789 | 0 | if(source == nullptr || sourceLength < -1){ |
2790 | 0 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
2791 | 0 | return nullptr; |
2792 | 0 | } |
2793 | | |
2794 | 0 | if(signatureLength == nullptr) { |
2795 | 0 | signatureLength = &dummy; |
2796 | 0 | } |
2797 | |
|
2798 | 0 | if(sourceLength==-1){ |
2799 | 0 | sourceLength=(int32_t)uprv_strlen(source); |
2800 | 0 | } |
2801 | | |
2802 | | |
2803 | 0 | while(i<sourceLength&& i<SIG_MAX_LEN){ |
2804 | 0 | start[i]=source[i]; |
2805 | 0 | i++; |
2806 | 0 | } |
2807 | |
|
2808 | 0 | if(start[0] == '\xFE' && start[1] == '\xFF') { |
2809 | 0 | *signatureLength=2; |
2810 | 0 | return "UTF-16BE"; |
2811 | 0 | } else if(start[0] == '\xFF' && start[1] == '\xFE') { |
2812 | 0 | if(start[2] == '\x00' && start[3] =='\x00') { |
2813 | 0 | *signatureLength=4; |
2814 | 0 | return "UTF-32LE"; |
2815 | 0 | } else { |
2816 | 0 | *signatureLength=2; |
2817 | 0 | return "UTF-16LE"; |
2818 | 0 | } |
2819 | 0 | } else if(start[0] == '\xEF' && start[1] == '\xBB' && start[2] == '\xBF') { |
2820 | 0 | *signatureLength=3; |
2821 | 0 | return "UTF-8"; |
2822 | 0 | } else if(start[0] == '\x00' && start[1] == '\x00' && |
2823 | 0 | start[2] == '\xFE' && start[3]=='\xFF') { |
2824 | 0 | *signatureLength=4; |
2825 | 0 | return "UTF-32BE"; |
2826 | 0 | } else if(start[0] == '\x0E' && start[1] == '\xFE' && start[2] == '\xFF') { |
2827 | 0 | *signatureLength=3; |
2828 | 0 | return "SCSU"; |
2829 | 0 | } else if(start[0] == '\xFB' && start[1] == '\xEE' && start[2] == '\x28') { |
2830 | 0 | *signatureLength=3; |
2831 | 0 | return "BOCU-1"; |
2832 | 0 | } else if(start[0] == '\x2B' && start[1] == '\x2F' && start[2] == '\x76') { |
2833 | | /* |
2834 | | * UTF-7: Initial U+FEFF is encoded as +/v8 or +/v9 or +/v+ or +/v/ |
2835 | | * depending on the second UTF-16 code unit. |
2836 | | * Detect the entire, closed Unicode mode sequence +/v8- for only U+FEFF |
2837 | | * if it occurs. |
2838 | | * |
2839 | | * So far we have +/v |
2840 | | */ |
2841 | 0 | if(start[3] == '\x38' && start[4] == '\x2D') { |
2842 | | /* 5 bytes +/v8- */ |
2843 | 0 | *signatureLength=5; |
2844 | 0 | return "UTF-7"; |
2845 | 0 | } else if(start[3] == '\x38' || start[3] == '\x39' || start[3] == '\x2B' || start[3] == '\x2F') { |
2846 | | /* 4 bytes +/v8 or +/v9 or +/v+ or +/v/ */ |
2847 | 0 | *signatureLength=4; |
2848 | 0 | return "UTF-7"; |
2849 | 0 | } |
2850 | 0 | }else if(start[0]=='\xDD' && start[1]== '\x73'&& start[2]=='\x66' && start[3]=='\x73'){ |
2851 | 0 | *signatureLength=4; |
2852 | 0 | return "UTF-EBCDIC"; |
2853 | 0 | } |
2854 | | |
2855 | | |
2856 | | /* no known Unicode signature byte sequence recognized */ |
2857 | 0 | *signatureLength=0; |
2858 | 0 | return nullptr; |
2859 | 0 | } |
2860 | | |
2861 | | U_CAPI int32_t U_EXPORT2 |
2862 | | ucnv_fromUCountPending(const UConverter* cnv, UErrorCode* status) |
2863 | 0 | { |
2864 | 0 | if(status == nullptr || U_FAILURE(*status)){ |
2865 | 0 | return -1; |
2866 | 0 | } |
2867 | 0 | if(cnv == nullptr){ |
2868 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
2869 | 0 | return -1; |
2870 | 0 | } |
2871 | | |
2872 | 0 | if(cnv->preFromUFirstCP >= 0){ |
2873 | 0 | return U16_LENGTH(cnv->preFromUFirstCP)+cnv->preFromULength ; |
2874 | 0 | }else if(cnv->preFromULength < 0){ |
2875 | 0 | return -cnv->preFromULength ; |
2876 | 0 | }else if(cnv->fromUChar32 > 0){ |
2877 | 0 | return 1; |
2878 | 0 | } |
2879 | 0 | return 0; |
2880 | |
|
2881 | 0 | } |
2882 | | |
2883 | | U_CAPI int32_t U_EXPORT2 |
2884 | 0 | ucnv_toUCountPending(const UConverter* cnv, UErrorCode* status){ |
2885 | |
|
2886 | 0 | if(status == nullptr || U_FAILURE(*status)){ |
2887 | 0 | return -1; |
2888 | 0 | } |
2889 | 0 | if(cnv == nullptr){ |
2890 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
2891 | 0 | return -1; |
2892 | 0 | } |
2893 | | |
2894 | 0 | if(cnv->preToULength > 0){ |
2895 | 0 | return cnv->preToULength ; |
2896 | 0 | }else if(cnv->preToULength < 0){ |
2897 | 0 | return -cnv->preToULength; |
2898 | 0 | }else if(cnv->toULength > 0){ |
2899 | 0 | return cnv->toULength; |
2900 | 0 | } |
2901 | 0 | return 0; |
2902 | 0 | } |
2903 | | |
2904 | | U_CAPI UBool U_EXPORT2 |
2905 | 0 | ucnv_isFixedWidth(UConverter *cnv, UErrorCode *status){ |
2906 | 0 | if (U_FAILURE(*status)) { |
2907 | 0 | return false; |
2908 | 0 | } |
2909 | | |
2910 | 0 | if (cnv == nullptr) { |
2911 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
2912 | 0 | return false; |
2913 | 0 | } |
2914 | | |
2915 | 0 | switch (ucnv_getType(cnv)) { |
2916 | 0 | case UCNV_SBCS: |
2917 | 0 | case UCNV_DBCS: |
2918 | 0 | case UCNV_UTF32_BigEndian: |
2919 | 0 | case UCNV_UTF32_LittleEndian: |
2920 | 0 | case UCNV_UTF32: |
2921 | 0 | case UCNV_US_ASCII: |
2922 | 0 | return true; |
2923 | 0 | default: |
2924 | 0 | return false; |
2925 | 0 | } |
2926 | 0 | } |
2927 | | #endif |
2928 | | |
2929 | | /* |
2930 | | * Hey, Emacs, please set the following: |
2931 | | * |
2932 | | * Local Variables: |
2933 | | * indent-tabs-mode: nil |
2934 | | * End: |
2935 | | * |
2936 | | */ |