Coverage Report

Created: 2024-04-24 06:23

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