~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/common/ucnv.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
******************************************************************************
3
 
*
4
 
*   Copyright (C) 1998-2001, International Business Machines
5
 
*   Corporation and others.  All Rights Reserved.
6
 
*
7
 
******************************************************************************
8
 
*
9
 
*  ucnv.c:
10
 
*  Implements APIs for the ICU's codeset conversion library;
11
 
*  mostly calls through internal functions;
12
 
*  created by Bertrand A. Damiba
13
 
*
14
 
* Modification History:
15
 
*
16
 
*   Date        Name        Description
17
 
*   04/04/99    helena      Fixed internal header inclusion.
18
 
*   05/09/00    helena      Added implementation to handle fallback mappings.
19
 
*   06/20/2000  helena      OS/400 port changes; mostly typecast.
20
 
*/
21
 
 
22
 
#include "unicode/utypes.h"
23
 
#include "unicode/ustring.h"
24
 
#include "unicode/ures.h"
25
 
#include "unicode/ucnv.h"
26
 
#include "unicode/ucnv_err.h"
27
 
#include "cmemory.h"
28
 
#include "cstring.h"
29
 
#include "umutex.h"
30
 
#include "uhash.h"
31
 
#include "ustr_imp.h"
32
 
#include "ucnv_imp.h"
33
 
#include "ucnv_io.h"
34
 
#include "ucnv_cnv.h"
35
 
#include "ucnv_bld.h"
36
 
 
37
 
#if 0
38
 
/* debugging for converters */
39
 
# include <stdio.h>
40
 
void UCNV_DEBUG_LOG(char *what, char *who, void *p, int l)
41
 
{
42
 
   static FILE *f = NULL;
43
 
   if(f==NULL)
44
 
   {
45
 
       f = fopen("c:\\UCNV_DEBUG_LOG.txt", "w");
46
 
   }
47
 
   fprintf(f, "%-20s %-10s %p@%d\n",
48
 
        who,what,p,l);
49
 
   fflush(f);
50
 
}
51
 
# define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
52
 
#else
53
 
# define UCNV_DEBUG_LOG(x,y,z)
54
 
#endif
55
 
 
56
 
/* size of intermediate and preflighting buffers in ucnv_convert() */
57
 
#define CHUNK_SIZE 5*1024
58
 
 
59
 
typedef struct UAmbiguousConverter {
60
 
    const char *name;
61
 
    const UChar variant5c;
62
 
} UAmbiguousConverter;
63
 
 
64
 
static const UAmbiguousConverter ambiguousConverters[]={
65
 
    { "ibm-942_P120-2000", 0xa5 },
66
 
    { "ibm-943_P130-2000", 0xa5 },
67
 
    { "ibm-33722_P120-2000", 0xa5 },
68
 
    { "ibm-949_P110-2000", 0x20a9 },
69
 
    { "ibm-1363_P110-2000", 0x20a9 },
70
 
    { "ISO_2022,locale=ko,version=0", 0x20a9 }
71
 
};
72
 
 
73
 
U_CAPI const char*  U_EXPORT2
74
 
ucnv_getDefaultName ()
75
 
{
76
 
    return ucnv_io_getDefaultConverterName();
77
 
}
78
 
 
79
 
U_CAPI void U_EXPORT2
80
 
ucnv_setDefaultName (const char *converterName)
81
 
{
82
 
  ucnv_io_setDefaultConverterName(converterName);
83
 
}
84
 
/*Calls through createConverter */
85
 
U_CAPI UConverter* U_EXPORT2
86
 
ucnv_open (const char *name,
87
 
                       UErrorCode * err)
88
 
{
89
 
    if (err == NULL || U_FAILURE (*err)) {
90
 
        return NULL;
91
 
    }
92
 
 
93
 
    return ucnv_createConverter (name, err);
94
 
}
95
 
 
96
 
/*Extracts the UChar* to a char* and calls through createConverter */
97
 
U_CAPI UConverter*   U_EXPORT2
98
 
ucnv_openU (const UChar * name,
99
 
                         UErrorCode * err)
100
 
{
101
 
    char asciiName[UCNV_MAX_CONVERTER_NAME_LENGTH];
102
 
 
103
 
    if (U_FAILURE (*err))
104
 
        return NULL;
105
 
    if (name == NULL)
106
 
        return ucnv_open (NULL, err);
107
 
    if (u_strlen (name) > UCNV_MAX_CONVERTER_NAME_LENGTH)
108
 
    {
109
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
110
 
        return NULL;
111
 
    }
112
 
    return ucnv_open (u_austrcpy (asciiName, name), err);
113
 
}
114
 
 
115
 
/*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls
116
 
 *through createConverter*/
117
 
U_CAPI UConverter*   U_EXPORT2
118
 
ucnv_openCCSID (int32_t codepage,
119
 
                UConverterPlatform platform,
120
 
                UErrorCode * err)
121
 
{
122
 
    char myName[UCNV_MAX_CONVERTER_NAME_LENGTH];
123
 
    int32_t myNameLen;
124
 
 
125
 
    if (U_FAILURE (*err))
126
 
        return NULL;
127
 
 
128
 
    ucnv_copyPlatformString (myName, platform);
129
 
    myNameLen = uprv_strlen(myName);
130
 
    myName[myNameLen++] = '-';
131
 
    myName[myNameLen] = 0;
132
 
    T_CString_integerToString (myName + myNameLen, codepage, 10);
133
 
 
134
 
    return ucnv_createConverter (myName, err);
135
 
}
136
 
 
137
 
/* Creating a temporary stack-based object that can be used in one thread, 
138
 
and created from a converter that is shared across threads.
139
 
*/
140
 
 
141
 
U_CAPI UConverter* U_EXPORT2
142
 
ucnv_safeClone(const UConverter* cnv, void *stackBuffer, int32_t *pBufferSize, UErrorCode *status)
143
 
{
144
 
    UConverter * localConverter;
145
 
    int32_t bufferSizeNeeded;
146
 
    char *stackBufferChars = (char *)stackBuffer;
147
 
 
148
 
    if (status == NULL || U_FAILURE(*status)){
149
 
        return 0;
150
 
    }
151
 
    if (!pBufferSize || !cnv){
152
 
       *status = U_ILLEGAL_ARGUMENT_ERROR;
153
 
        return 0;
154
 
    }
155
 
    /* Pointers on 64-bit platforms need to be aligned
156
 
     * on a 64-bit boundry in memory.
157
 
     */
158
 
    if (U_ALIGNMENT_OFFSET(stackBuffer) != 0) {
159
 
        int32_t offsetUp = (int32_t)U_ALIGNMENT_OFFSET_UP(stackBufferChars);
160
 
        *pBufferSize -= offsetUp;
161
 
        stackBufferChars += offsetUp;
162
 
    }
163
 
    stackBuffer = (void *)stackBufferChars;
164
 
    
165
 
    if (cnv->sharedData->impl->safeClone != NULL) {
166
 
        /* call the custom safeClone function for sizing */
167
 
        bufferSizeNeeded = 0;
168
 
        cnv->sharedData->impl->safeClone(cnv, stackBuffer, &bufferSizeNeeded, status);
169
 
    }
170
 
    else
171
 
    {
172
 
        bufferSizeNeeded = sizeof(UConverter);
173
 
    }
174
 
 
175
 
    if (*pBufferSize <= 0){ /* 'preflighting' request - set needed size into *pBufferSize */
176
 
        *pBufferSize = bufferSizeNeeded;
177
 
        return 0;
178
 
    }
179
 
 
180
 
    if (*pBufferSize < bufferSizeNeeded || stackBuffer == NULL)
181
 
    {
182
 
        /* allocate one here...*/
183
 
        localConverter = ucnv_createConverter (ucnv_getName (cnv, status), status);
184
 
        if (U_SUCCESS(*status))
185
 
        {
186
 
            *status = U_SAFECLONE_ALLOCATED_ERROR;
187
 
        }
188
 
    } else {
189
 
        if (cnv->sharedData->impl->safeClone != NULL) {
190
 
            /* call the custom safeClone function */
191
 
            localConverter = cnv->sharedData->impl->safeClone(cnv, stackBuffer, pBufferSize, status);
192
 
        }
193
 
        else
194
 
        {
195
 
            localConverter = (UConverter *)stackBuffer;
196
 
            uprv_memcpy(localConverter, cnv, sizeof(UConverter));
197
 
            localConverter->isCopyLocal = TRUE;
198
 
        }
199
 
    }
200
 
    return localConverter;
201
 
}
202
 
 
203
 
 
204
 
 
205
 
/*Decreases the reference counter in the shared immutable section of the object
206
 
 *and frees the mutable part*/
207
 
 
208
 
U_CAPI void  U_EXPORT2
209
 
ucnv_close (UConverter * converter)
210
 
{
211
 
    /* first, notify the callback functions that the converter is closed */
212
 
    UConverterToUnicodeArgs toUArgs = {
213
 
        sizeof(UConverterToUnicodeArgs),
214
 
            TRUE,
215
 
            NULL,
216
 
            NULL,
217
 
            NULL,
218
 
            NULL,
219
 
            NULL,
220
 
            NULL
221
 
    };
222
 
    UConverterFromUnicodeArgs fromUArgs = {
223
 
        sizeof(UConverterFromUnicodeArgs),
224
 
            TRUE,
225
 
            NULL,
226
 
            NULL,
227
 
            NULL,
228
 
            NULL,
229
 
            NULL,
230
 
            NULL
231
 
    };
232
 
    UErrorCode errorCode;
233
 
 
234
 
    if (converter == NULL)
235
 
    {
236
 
        return;
237
 
    }
238
 
 
239
 
    toUArgs.converter = fromUArgs.converter = converter;
240
 
    errorCode = U_ZERO_ERROR;
241
 
    converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, NULL, 0, UCNV_CLOSE, &errorCode);
242
 
    errorCode = U_ZERO_ERROR;
243
 
    converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, NULL, 0, 0, UCNV_CLOSE, &errorCode);
244
 
 
245
 
    if (converter->sharedData->impl->close != NULL) {
246
 
        converter->sharedData->impl->close(converter);
247
 
    }
248
 
    if(!converter->isCopyLocal){
249
 
        if (converter->sharedData->referenceCounter != ~0) {
250
 
            umtx_lock (NULL);
251
 
            if (converter->sharedData->referenceCounter != 0) {
252
 
                converter->sharedData->referenceCounter--;
253
 
            }
254
 
            umtx_unlock (NULL);
255
 
        }
256
 
        uprv_free (converter);
257
 
    }
258
 
    return;
259
 
}
260
 
 
261
 
/*returns a single Name from the list, will return NULL if out of bounds
262
 
 */
263
 
U_CAPI const char*   U_EXPORT2
264
 
ucnv_getAvailableName (int32_t n)
265
 
{
266
 
  if (0 <= n && n <= 0xffff) {
267
 
    UErrorCode err = U_ZERO_ERROR;
268
 
    const char *name = ucnv_io_getAvailableConverter((uint16_t)n, &err);
269
 
    if (U_SUCCESS(err)) {
270
 
      return name;
271
 
    }
272
 
  }
273
 
  return NULL;
274
 
}
275
 
 
276
 
U_CAPI int32_t   U_EXPORT2
277
 
ucnv_countAvailable ()
278
 
{
279
 
    UErrorCode err = U_ZERO_ERROR;
280
 
    return ucnv_io_countAvailableConverters(&err);
281
 
}
282
 
 
283
 
U_CAPI uint16_t U_EXPORT2
284
 
ucnv_countAliases(const char *alias, UErrorCode *pErrorCode)
285
 
{
286
 
    const char *p;
287
 
    return ucnv_io_getAliases(alias, &p, pErrorCode);
288
 
}
289
 
 
290
 
 
291
 
U_CAPI const char* U_EXPORT2
292
 
ucnv_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode)
293
 
{
294
 
    return ucnv_io_getAlias(alias, n, pErrorCode);
295
 
}
296
 
 
297
 
U_CAPI void U_EXPORT2
298
 
ucnv_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode)
299
 
{
300
 
    const char *p;
301
 
    uint16_t count=ucnv_io_getAliases(alias, &p, pErrorCode);
302
 
    while(count>0) {
303
 
        *aliases++=p;
304
 
        /* skip a name, first the canonical converter name */
305
 
        p+=uprv_strlen(p)+1;
306
 
        --count;
307
 
    }
308
 
}
309
 
 
310
 
U_CAPI uint16_t U_EXPORT2
311
 
ucnv_countStandards(void)
312
 
{
313
 
    UErrorCode err = U_ZERO_ERROR;
314
 
    return ucnv_io_countStandards(&err);
315
 
}
316
 
 
317
 
U_CAPI void    U_EXPORT2
318
 
ucnv_getSubstChars (const UConverter * converter,
319
 
                    char *mySubChar,
320
 
                    int8_t * len,
321
 
                    UErrorCode * err)
322
 
{
323
 
    if (U_FAILURE (*err))
324
 
        return;
325
 
 
326
 
    if (*len < converter->subCharLen) /*not enough space in subChars */
327
 
    {
328
 
        *err = U_INDEX_OUTOFBOUNDS_ERROR;
329
 
        return;
330
 
    }
331
 
 
332
 
  uprv_memcpy (mySubChar, converter->subChar, converter->subCharLen);   /*fills in the subchars */
333
 
  *len = converter->subCharLen; /*store # of bytes copied to buffer */
334
 
    uprv_memcpy (mySubChar, converter->subChar, converter->subCharLen);   /*fills in the subchars */
335
 
    *len = converter->subCharLen; /*store # of bytes copied to buffer */
336
 
}
337
 
 
338
 
U_CAPI void    U_EXPORT2
339
 
ucnv_setSubstChars (UConverter * converter,
340
 
                    const char *mySubChar,
341
 
                    int8_t len,
342
 
                    UErrorCode * err)
343
 
{
344
 
    if (U_FAILURE (*err))
345
 
        return;
346
 
    
347
 
    /*Makes sure that the subChar is within the codepages char length boundaries */
348
 
    if ((len > converter->sharedData->staticData->maxBytesPerChar)
349
 
     || (len < converter->sharedData->staticData->minBytesPerChar))
350
 
    {
351
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
352
 
        return;
353
 
    }
354
 
    
355
 
    uprv_memcpy (converter->subChar, mySubChar, len); /*copies the subchars */
356
 
    converter->subCharLen = len;  /*sets the new len */
357
 
 
358
 
    /*
359
 
    * There is currently (2001Feb) no separate API to set/get subChar1.
360
 
    * In order to always have subChar written after it is explicitly set,
361
 
    * we set subChar1 to 0.
362
 
    */
363
 
    converter->subChar1 = 0;
364
 
    
365
 
    return;
366
 
}
367
 
 
368
 
U_CAPI int32_t U_EXPORT2
369
 
ucnv_getDisplayName(const UConverter *cnv,
370
 
                    const char *displayLocale,
371
 
                    UChar *displayName, int32_t displayNameCapacity,
372
 
                    UErrorCode *pErrorCode) {
373
 
    UResourceBundle *rb;
374
 
    const UChar *name;
375
 
    int32_t length;
376
 
 
377
 
    /* check arguments */
378
 
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
379
 
        return 0;
380
 
    }
381
 
 
382
 
    if(cnv==NULL || displayNameCapacity<0 || (displayNameCapacity>0 && displayName==NULL)) {
383
 
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
384
 
        return 0;
385
 
    }
386
 
 
387
 
    /* open the resource bundle and get the display name string */
388
 
    rb=ures_open(NULL, displayLocale, pErrorCode);
389
 
    if(U_FAILURE(*pErrorCode)) {
390
 
        return 0;
391
 
    }
392
 
 
393
 
    /* use the internal name as the key */
394
 
    name=ures_getStringByKey(rb, cnv->sharedData->staticData->name, &length, pErrorCode);
395
 
    ures_close(rb);
396
 
 
397
 
    if(U_SUCCESS(*pErrorCode)) {
398
 
        /* copy the string */
399
 
        u_memcpy(displayName, name, uprv_min(length, displayNameCapacity)*U_SIZEOF_UCHAR);
400
 
    } else {
401
 
        /* convert the internal name into a Unicode string */
402
 
        *pErrorCode=U_ZERO_ERROR;
403
 
        length=uprv_strlen(cnv->sharedData->staticData->name);
404
 
        u_charsToUChars(cnv->sharedData->staticData->name, displayName, uprv_min(length, displayNameCapacity));
405
 
    }
406
 
    return u_terminateUChars(displayName, displayNameCapacity, length, pErrorCode);
407
 
}
408
 
 
409
 
/*resets the internal states of a converter
410
 
 *goal : have the same behaviour than a freshly created converter
411
 
 */
412
 
static void _reset(UConverter *converter, UConverterResetChoice choice) {
413
 
    /* first, notify the callback functions that the converter is reset */
414
 
    UConverterToUnicodeArgs toUArgs = {
415
 
        sizeof(UConverterToUnicodeArgs),
416
 
            TRUE,
417
 
            NULL,
418
 
            NULL,
419
 
            NULL,
420
 
            NULL,
421
 
            NULL,
422
 
            NULL
423
 
    };
424
 
    UConverterFromUnicodeArgs fromUArgs = {
425
 
        sizeof(UConverterFromUnicodeArgs),
426
 
            TRUE,
427
 
            NULL,
428
 
            NULL,
429
 
            NULL,
430
 
            NULL,
431
 
            NULL,
432
 
            NULL
433
 
    };
434
 
    UErrorCode errorCode;
435
 
 
436
 
    if(converter == NULL) {
437
 
        return;
438
 
    }
439
 
 
440
 
    toUArgs.converter = fromUArgs.converter = converter;
441
 
    if(choice<=UCNV_RESET_TO_UNICODE) {
442
 
        errorCode = U_ZERO_ERROR;
443
 
        converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, NULL, 0, UCNV_RESET, &errorCode);
444
 
    }
445
 
    if(choice!=UCNV_RESET_TO_UNICODE) {
446
 
        errorCode = U_ZERO_ERROR;
447
 
        converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, NULL, 0, 0, UCNV_RESET, &errorCode);
448
 
    }
449
 
 
450
 
    /* now reset the converter itself */
451
 
    if(choice<=UCNV_RESET_TO_UNICODE) {
452
 
        converter->toUnicodeStatus = converter->sharedData->toUnicodeStatus;
453
 
        converter->UCharErrorBufferLength = 0;
454
 
    }
455
 
    if(choice!=UCNV_RESET_TO_UNICODE) {
456
 
        converter->fromUnicodeStatus = 0;
457
 
        converter->charErrorBufferLength = 0;
458
 
    }
459
 
 
460
 
    if (converter->sharedData->impl->reset != NULL) {
461
 
        /* call the custom reset function */
462
 
        converter->sharedData->impl->reset(converter, choice);
463
 
    } else if(choice<=UCNV_RESET_TO_UNICODE) {
464
 
        converter->mode = UCNV_SI;
465
 
    }
466
 
}
467
 
 
468
 
U_CAPI void  U_EXPORT2
469
 
ucnv_reset(UConverter *converter)
470
 
{
471
 
    _reset(converter, UCNV_RESET_BOTH);
472
 
}
473
 
 
474
 
U_CAPI void  U_EXPORT2
475
 
ucnv_resetToUnicode(UConverter *converter)
476
 
{
477
 
    _reset(converter, UCNV_RESET_TO_UNICODE);
478
 
}
479
 
 
480
 
U_CAPI void  U_EXPORT2
481
 
ucnv_resetFromUnicode(UConverter *converter)
482
 
{
483
 
    _reset(converter, UCNV_RESET_FROM_UNICODE);
484
 
}
485
 
 
486
 
U_CAPI int8_t   U_EXPORT2
487
 
ucnv_getMaxCharSize (const UConverter * converter)
488
 
{
489
 
    return converter->sharedData->staticData->maxBytesPerChar;
490
 
}
491
 
 
492
 
 
493
 
U_CAPI int8_t   U_EXPORT2
494
 
ucnv_getMinCharSize (const UConverter * converter)
495
 
{
496
 
    return converter->sharedData->staticData->minBytesPerChar;
497
 
}
498
 
 
499
 
U_CAPI const char*   U_EXPORT2
500
 
ucnv_getName (const UConverter * converter, UErrorCode * err)
501
 
     
502
 
{
503
 
    if (U_FAILURE (*err))
504
 
        return NULL;
505
 
    if(converter->sharedData->impl->getName){
506
 
        const char* temp= converter->sharedData->impl->getName(converter);
507
 
        if(temp)
508
 
            return temp;
509
 
    }
510
 
    return converter->sharedData->staticData->name;
511
 
}
512
 
 
513
 
U_CAPI int32_t   U_EXPORT2
514
 
ucnv_getCCSID (const UConverter * converter,
515
 
                        UErrorCode * err)
516
 
{
517
 
    if (U_FAILURE (*err))
518
 
        return -1;
519
 
 
520
 
    return converter->sharedData->staticData->codepage;
521
 
}
522
 
 
523
 
 
524
 
U_CAPI UConverterPlatform   U_EXPORT2
525
 
ucnv_getPlatform (const UConverter * converter,
526
 
                                      UErrorCode * err)
527
 
{
528
 
    if (U_FAILURE (*err))
529
 
        return UCNV_UNKNOWN;
530
 
 
531
 
    return (UConverterPlatform)converter->sharedData->staticData->platform;
532
 
}
533
 
 
534
 
U_CAPI void U_EXPORT2
535
 
    ucnv_getToUCallBack (const UConverter * converter,
536
 
                         UConverterToUCallback *action,
537
 
                         const void **context)
538
 
{
539
 
    *action = converter->fromCharErrorBehaviour;
540
 
    *context = converter->toUContext;
541
 
}
542
 
 
543
 
U_CAPI void U_EXPORT2
544
 
    ucnv_getFromUCallBack (const UConverter * converter,
545
 
                           UConverterFromUCallback *action,
546
 
                           const void **context)
547
 
{
548
 
    *action = converter->fromUCharErrorBehaviour;
549
 
    *context = converter->fromUContext;
550
 
}
551
 
 
552
 
U_CAPI void    U_EXPORT2
553
 
ucnv_setToUCallBack (UConverter * converter,
554
 
                            UConverterToUCallback newAction,
555
 
                            const void* newContext,
556
 
                            UConverterToUCallback *oldAction,
557
 
                            const void** oldContext,
558
 
                            UErrorCode * err)
559
 
{
560
 
    if (U_FAILURE (*err))
561
 
        return;
562
 
    if (oldAction) *oldAction = converter->fromCharErrorBehaviour;
563
 
    converter->fromCharErrorBehaviour = newAction;
564
 
    if (oldContext) *oldContext = converter->toUContext;
565
 
    converter->toUContext = newContext;
566
 
}
567
 
 
568
 
U_CAPI void  U_EXPORT2
569
 
ucnv_setFromUCallBack (UConverter * converter,
570
 
                            UConverterFromUCallback newAction,
571
 
                            const void* newContext,
572
 
                            UConverterFromUCallback *oldAction,
573
 
                            const void** oldContext,
574
 
                            UErrorCode * err)
575
 
{
576
 
    if (U_FAILURE (*err))
577
 
        return;
578
 
    if (oldAction) *oldAction = converter->fromUCharErrorBehaviour;
579
 
    converter->fromUCharErrorBehaviour = newAction;
580
 
    if (oldContext) *oldContext = converter->fromUContext;
581
 
    converter->fromUContext = newContext;
582
 
}
583
 
 
584
 
U_CAPI void  U_EXPORT2
585
 
ucnv_fromUnicode (UConverter * _this,
586
 
                       char **target,
587
 
                       const char *targetLimit,
588
 
                       const UChar ** source,
589
 
                       const UChar * sourceLimit,
590
 
                       int32_t* offsets,
591
 
                       UBool flush,
592
 
                       UErrorCode * err)
593
 
{
594
 
    UConverterFromUnicodeArgs args;
595
 
    const char *t;
596
 
 
597
 
    /*
598
 
    * Check parameters in for all conversions
599
 
    */
600
 
    if (err == NULL || U_FAILURE (*err)) {
601
 
        return;
602
 
    }
603
 
 
604
 
    if (_this == NULL || target == NULL || source == NULL) {
605
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
606
 
        return;
607
 
    }
608
 
 
609
 
    t = *target;
610
 
    if (targetLimit < t || sourceLimit < *source)
611
 
    {
612
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
613
 
        return;
614
 
    }
615
 
 
616
 
    /*
617
 
    * Make sure that the target buffer size does not exceed the number range for int32_t
618
 
    * because some functions use the size rather than comparing pointers.
619
 
    * size_t is guaranteed to be unsigned.
620
 
    */
621
 
    if((size_t)(targetLimit - t) > (size_t)0x7fffffff && targetLimit > t)
622
 
    {
623
 
        targetLimit = t + 0x7fffffff;
624
 
    }
625
 
    
626
 
    /*
627
 
    * Deal with stored carry over data.  This is done in the common location
628
 
    * to avoid doing it for each conversion.
629
 
    */
630
 
    if (_this->charErrorBufferLength > 0)
631
 
    {
632
 
        int32_t myTargetIndex = 0;
633
 
        
634
 
        ucnv_flushInternalCharBuffer (_this, 
635
 
                (char *)t,
636
 
                &myTargetIndex,
637
 
                targetLimit - *target,
638
 
                offsets?&offsets:NULL,
639
 
                err);
640
 
        *target += myTargetIndex;
641
 
        if (U_FAILURE (*err))
642
 
            return;
643
 
    }
644
 
    
645
 
    args.converter = _this;
646
 
    args.flush = flush;
647
 
    args.offsets = offsets;
648
 
    args.source = *source;
649
 
    args.sourceLimit = sourceLimit;
650
 
    args.target = *target;
651
 
    args.targetLimit = targetLimit;
652
 
    args.size = sizeof(args);
653
 
    if (offsets)
654
 
    {
655
 
        if (_this->sharedData->impl->fromUnicodeWithOffsets != NULL)
656
 
        {
657
 
            _this->sharedData->impl->fromUnicodeWithOffsets(&args, err);
658
 
            *source = args.source;
659
 
            *target = args.target;
660
 
            return;
661
 
        }
662
 
        else {
663
 
            /* there is no implementation that sets offsets, set them all to -1 */
664
 
            int32_t i, targetSize = targetLimit - *target;
665
 
            
666
 
            for (i=0; i<targetSize; i++) {
667
 
                offsets[i] = -1;
668
 
            }
669
 
        }
670
 
    }
671
 
    
672
 
    /*calls the specific conversion routines */
673
 
    _this->sharedData->impl->fromUnicode(&args, err);
674
 
    *source = args.source;
675
 
    *target = args.target;
676
 
}
677
 
 
678
 
 
679
 
 
680
 
U_CAPI void    U_EXPORT2
681
 
ucnv_toUnicode (UConverter * _this,
682
 
                UChar ** target,
683
 
                const UChar * targetLimit,
684
 
                const char **source,
685
 
                const char *sourceLimit,
686
 
                int32_t* offsets,
687
 
                UBool flush,
688
 
                UErrorCode * err)
689
 
{
690
 
    UConverterToUnicodeArgs args;
691
 
    const UChar *t;
692
 
 
693
 
    /*
694
 
    * Check parameters in for all conversions
695
 
    */
696
 
    if (err == NULL || U_FAILURE (*err)) {
697
 
        return;
698
 
    }
699
 
 
700
 
    if (_this == NULL || target == NULL || source == NULL) {
701
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
702
 
        return;
703
 
    }
704
 
 
705
 
    t = *target;
706
 
    if (targetLimit < t || sourceLimit < *source) {
707
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
708
 
        return;
709
 
    }
710
 
 
711
 
    /*
712
 
    * Make sure that the target buffer size does not exceed the number range for int32_t
713
 
    * because some functions use the size rather than comparing pointers.
714
 
    * size_t is guaranteed to be unsigned.
715
 
    */
716
 
    if((size_t)(targetLimit - t) > (size_t)0x3fffffff && targetLimit > t) {
717
 
        targetLimit = t + 0x3fffffff;
718
 
    }
719
 
 
720
 
    /*
721
 
    * Deal with stored carry over data.  This is done in the common location
722
 
    * to avoid doing it for each conversion.
723
 
    */
724
 
    if (_this->UCharErrorBufferLength > 0)
725
 
    {
726
 
        int32_t myTargetIndex = 0;
727
 
 
728
 
        ucnv_flushInternalUnicodeBuffer (_this, 
729
 
                (UChar *)t,
730
 
                &myTargetIndex,
731
 
                targetLimit - *target,
732
 
                offsets?&offsets:NULL,
733
 
                err);
734
 
        *target += myTargetIndex;
735
 
        if (U_FAILURE (*err))
736
 
            return;
737
 
    }
738
 
 
739
 
    args.converter = _this;
740
 
    args.flush = flush;
741
 
    args.offsets = offsets;
742
 
    args.source = (char *) *source;
743
 
    args.sourceLimit = sourceLimit;
744
 
    args.target =  *target;
745
 
    args.targetLimit = targetLimit;
746
 
    args.size = sizeof(args);
747
 
    if (offsets) {
748
 
        if (_this->sharedData->impl->toUnicodeWithOffsets != NULL) {
749
 
            _this->sharedData->impl->toUnicodeWithOffsets(&args, err);
750
 
            *source = args.source;
751
 
            *target = args.target;
752
 
            return;
753
 
        } else {
754
 
            /* there is no implementation that sets offsets, set them all to -1 */
755
 
            int32_t i, targetSize = targetLimit - *target;
756
 
            
757
 
            for (i=0; i<targetSize; i++) {
758
 
                offsets[i] = -1;
759
 
            }
760
 
        }
761
 
    }
762
 
 
763
 
    /*calls the specific conversion routines */
764
 
    _this->sharedData->impl->toUnicode(&args, err); 
765
 
 
766
 
    *source = args.source;
767
 
    *target = args.target;
768
 
    return;
769
 
}
770
 
 
771
 
U_CAPI int32_t U_EXPORT2
772
 
ucnv_fromUChars(UConverter *cnv,
773
 
                char *dest, int32_t destCapacity,
774
 
                const UChar *src, int32_t srcLength,
775
 
                UErrorCode *pErrorCode) {
776
 
    const UChar *srcLimit;
777
 
    char *originalDest, *destLimit;
778
 
    int32_t destLength;
779
 
 
780
 
    /* check arguments */
781
 
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
782
 
        return 0;
783
 
    }
784
 
 
785
 
    if( cnv==NULL ||
786
 
        destCapacity<0 || (destCapacity>0 && dest==NULL) ||
787
 
        srcLength<-1 || (srcLength!=0 && src==NULL)
788
 
    ) {
789
 
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
790
 
        return 0;
791
 
    }
792
 
 
793
 
    /* initialize */
794
 
    ucnv_resetFromUnicode(cnv);
795
 
    originalDest=dest;
796
 
    if(srcLength==-1) {
797
 
        srcLength=u_strlen(src);
798
 
    }
799
 
    if(srcLength>0) {
800
 
        srcLimit=src+srcLength;
801
 
        destLimit=dest+destCapacity;
802
 
 
803
 
        /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
804
 
        if(destLimit<dest || (destLimit==NULL && dest!=NULL)) {
805
 
            destLimit=(char *)U_MAX_PTR(dest);
806
 
        }
807
 
 
808
 
        /* perform the conversion */
809
 
        ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, pErrorCode);
810
 
        destLength=(int32_t)(dest-originalDest);
811
 
 
812
 
        /* if an overflow occurs, then get the preflighting length */
813
 
        if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
814
 
            char buffer[1024];
815
 
 
816
 
            destLimit=buffer+sizeof(buffer);
817
 
            do {
818
 
                dest=buffer;
819
 
                *pErrorCode=U_ZERO_ERROR;
820
 
                ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, pErrorCode);
821
 
                destLength+=(int32_t)(dest-buffer);
822
 
            } while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR);
823
 
        }
824
 
    } else {
825
 
        destLength=0;
826
 
    }
827
 
 
828
 
    return u_terminateChars(originalDest, destCapacity, destLength, pErrorCode);
829
 
}
830
 
 
831
 
U_CAPI int32_t U_EXPORT2
832
 
ucnv_toUChars(UConverter *cnv,
833
 
              UChar *dest, int32_t destCapacity,
834
 
              const char *src, int32_t srcLength,
835
 
              UErrorCode *pErrorCode) {
836
 
    const char *srcLimit;
837
 
    UChar *originalDest, *destLimit;
838
 
    int32_t destLength;
839
 
 
840
 
    /* check arguments */
841
 
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
842
 
        return 0;
843
 
    }
844
 
 
845
 
    if( cnv==NULL ||
846
 
        destCapacity<0 || (destCapacity>0 && dest==NULL) ||
847
 
        srcLength<-1 || (srcLength!=0 && src==NULL))
848
 
    {
849
 
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
850
 
        return 0;
851
 
    }
852
 
 
853
 
    /* initialize */
854
 
    ucnv_resetToUnicode(cnv);
855
 
    originalDest=dest;
856
 
    if(srcLength==-1) {
857
 
        srcLength=uprv_strlen(src);
858
 
    }
859
 
    if(srcLength>0) {
860
 
        srcLimit=src+srcLength;
861
 
        destLimit=dest+destCapacity;
862
 
 
863
 
        /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
864
 
        if(destLimit<dest || (destLimit==NULL && dest!=NULL)) {
865
 
            destLimit=(UChar *)U_MAX_PTR(dest);
866
 
        }
867
 
 
868
 
        /* perform the conversion */
869
 
        ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, pErrorCode);
870
 
        destLength=(int32_t)(dest-originalDest);
871
 
 
872
 
        /* if an overflow occurs, then get the preflighting length */
873
 
        if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR)
874
 
        {
875
 
            UChar buffer[1024];
876
 
 
877
 
            destLimit=buffer+sizeof(buffer)/U_SIZEOF_UCHAR;
878
 
            do {
879
 
                dest=buffer;
880
 
                *pErrorCode=U_ZERO_ERROR;
881
 
                ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, pErrorCode);
882
 
                destLength+=(int32_t)(dest-buffer);
883
 
            }
884
 
            while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR);
885
 
        }
886
 
    } else {
887
 
        destLength=0;
888
 
    }
889
 
 
890
 
    return u_terminateUChars(originalDest, destCapacity, destLength, pErrorCode);
891
 
}
892
 
 
893
 
U_CAPI UChar32  U_EXPORT2
894
 
ucnv_getNextUChar(UConverter * converter,
895
 
                  const char **source,
896
 
                  const char *sourceLimit,
897
 
                  UErrorCode * err)
898
 
{
899
 
    UConverterToUnicodeArgs args;
900
 
    UChar32 ch;
901
 
 
902
 
    if(err == NULL || U_FAILURE(*err)) {
903
 
        return 0xffff;
904
 
    }
905
 
 
906
 
    if(converter == NULL || source == NULL || sourceLimit < *source) {
907
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
908
 
        return 0xffff;
909
 
    }
910
 
 
911
 
    /* In case internal data had been stored
912
 
    * we return the first UChar32 in the internal buffer,
913
 
    * and update the internal state accordingly
914
 
    */
915
 
    if (converter->UCharErrorBufferLength > 0)
916
 
    {
917
 
        int32_t i = 0;
918
 
        UChar32 myUChar;
919
 
        UTF_NEXT_CHAR(converter->UCharErrorBuffer, i, sizeof(converter->UCharErrorBuffer), myUChar);
920
 
        /*In this memmove we update the internal buffer by
921
 
        *popping the first character.
922
 
        *Note that in the call itself we decrement
923
 
        *UCharErrorBufferLength
924
 
        */
925
 
        uprv_memmove (converter->UCharErrorBuffer,
926
 
            converter->UCharErrorBuffer + i,
927
 
            (converter->UCharErrorBufferLength - i) * sizeof (UChar));
928
 
        converter->UCharErrorBufferLength -= (int8_t)i;
929
 
        return myUChar;
930
 
    }
931
 
    /*calls the specific conversion routines */
932
 
    /*as dictated in a code review, avoids a switch statement */
933
 
    args.converter = converter;
934
 
    args.flush = TRUE;
935
 
    args.offsets = NULL;
936
 
    args.source = *source;
937
 
    args.sourceLimit = sourceLimit;
938
 
    args.target = NULL;
939
 
    args.targetLimit = NULL;
940
 
    args.size = sizeof(args);
941
 
    if (converter->sharedData->impl->getNextUChar != NULL)
942
 
    {
943
 
        ch = converter->sharedData->impl->getNextUChar(&args, err);
944
 
    } else {
945
 
        /* default implementation */
946
 
        ch = ucnv_getNextUCharFromToUImpl(&args, converter->sharedData->impl->toUnicode, FALSE, err);
947
 
    }
948
 
    *source = args.source;
949
 
    return ch;
950
 
}
951
 
 
952
 
U_CAPI int32_t U_EXPORT2
953
 
ucnv_convert(const char *toConverterName, const char *fromConverterName,
954
 
             char *target, int32_t targetSize,
955
 
             const char *source, int32_t sourceSize,
956
 
             UErrorCode *pErrorCode) {
957
 
    UChar pivotBuffer[CHUNK_SIZE];
958
 
    UChar *pivot, *pivot2;
959
 
 
960
 
    UConverter *inConverter, *outConverter;
961
 
    char *myTarget;
962
 
    const char *sourceLimit;
963
 
    const char *targetLimit;
964
 
    int32_t targetCapacity=0;
965
 
 
966
 
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
967
 
        return 0;
968
 
    }
969
 
 
970
 
    if(sourceSize<0 || targetSize<0 || source==NULL
971
 
        || (targetSize>0 && target==NULL))
972
 
    {
973
 
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
974
 
        return 0;
975
 
    }
976
 
 
977
 
    /* if there is no input data, we're done */
978
 
    if(sourceSize==0) {
979
 
        return 0;
980
 
    }
981
 
 
982
 
    /* create the converters */
983
 
    inConverter=ucnv_open(fromConverterName, pErrorCode);
984
 
    if(U_FAILURE(*pErrorCode)) {
985
 
        return 0;
986
 
    }
987
 
 
988
 
    outConverter=ucnv_open(toConverterName, pErrorCode);
989
 
    if(U_FAILURE(*pErrorCode)) {
990
 
        ucnv_close(inConverter);
991
 
        return 0;
992
 
    }
993
 
 
994
 
    /* set up the other variables */
995
 
    sourceLimit=source+sourceSize;
996
 
    pivot=pivot2=pivotBuffer;
997
 
    myTarget=target;
998
 
    targetCapacity=0;
999
 
 
1000
 
    if(targetSize>0) {
1001
 
        /* perform real conversion */
1002
 
 
1003
 
        /*
1004
 
         * loops until the input buffer is completely consumed
1005
 
         * or an error is encountered;
1006
 
         * first we convert from inConverter codepage to Unicode
1007
 
         * then from Unicode to outConverter codepage
1008
 
         */
1009
 
        targetLimit=target+targetSize;
1010
 
        do {
1011
 
            pivot=pivotBuffer;
1012
 
            ucnv_toUnicode(inConverter,
1013
 
                           &pivot, pivotBuffer+CHUNK_SIZE,
1014
 
                           &source, sourceLimit,
1015
 
                           NULL,
1016
 
                           TRUE,
1017
 
                           pErrorCode);
1018
 
 
1019
 
            /* U_BUFFER_OVERFLOW_ERROR only means that the pivot buffer is full */
1020
 
            if(U_SUCCESS(*pErrorCode) || *pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
1021
 
                *pErrorCode=U_ZERO_ERROR;
1022
 
                pivot2=pivotBuffer;
1023
 
                ucnv_fromUnicode(outConverter,
1024
 
                                 &myTarget, targetLimit,
1025
 
                                 (const UChar **)&pivot2, pivot,
1026
 
                                 NULL,
1027
 
                                 (UBool)(source==sourceLimit),
1028
 
                                 pErrorCode);
1029
 
                /*
1030
 
                 * If this overflows the real target, then we must stop
1031
 
                 * converting and preflight with the loop below.
1032
 
                 */
1033
 
            }
1034
 
        } while(U_SUCCESS(*pErrorCode) && source!=sourceLimit);
1035
 
 
1036
 
        targetCapacity=myTarget-target;
1037
 
    }
1038
 
 
1039
 
    /*
1040
 
     * If the output buffer is exhausted (or we are only "preflighting"), we need to stop writing
1041
 
     * to it but continue the conversion in order to store in targetSize
1042
 
     * the number of bytes that was required.
1043
 
     */
1044
 
    if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || targetSize==0)
1045
 
    {
1046
 
        char targetBuffer[CHUNK_SIZE];
1047
 
 
1048
 
        targetLimit=targetBuffer+CHUNK_SIZE;
1049
 
        do {
1050
 
            /* since the pivot buffer may still contain some characters, start with emptying it */
1051
 
            *pErrorCode=U_ZERO_ERROR;
1052
 
            while(pivot2!=pivot && U_SUCCESS(*pErrorCode)) {
1053
 
                myTarget=targetBuffer;
1054
 
                ucnv_fromUnicode(outConverter,
1055
 
                                 &myTarget, targetLimit,
1056
 
                                 (const UChar **)&pivot2, pivot,
1057
 
                                 NULL,
1058
 
                                 (UBool)(source==sourceLimit),
1059
 
                                 pErrorCode);
1060
 
                targetCapacity+=(myTarget-targetBuffer);
1061
 
                if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
1062
 
                    *pErrorCode=U_ZERO_ERROR;
1063
 
                }
1064
 
            }
1065
 
 
1066
 
            if(U_FAILURE(*pErrorCode)) {
1067
 
                /* an error occurred: done */
1068
 
                break;
1069
 
            }
1070
 
 
1071
 
            if(source==sourceLimit) {
1072
 
                /*
1073
 
                 * source is consumed:
1074
 
                 * done, and set the buffer overflow error as
1075
 
                 * the result for the entire function
1076
 
                 */
1077
 
                *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1078
 
                break;
1079
 
            }
1080
 
 
1081
 
            /* now convert from the source into the pivot buffer again */
1082
 
            pivot=pivot2=pivotBuffer;
1083
 
            ucnv_toUnicode(inConverter,
1084
 
                           &pivot, pivotBuffer+CHUNK_SIZE,
1085
 
                           &source, sourceLimit,
1086
 
                           NULL,
1087
 
                           TRUE,
1088
 
                           pErrorCode);
1089
 
        }
1090
 
        while(U_SUCCESS(*pErrorCode) || *pErrorCode==U_BUFFER_OVERFLOW_ERROR);
1091
 
    }
1092
 
 
1093
 
    ucnv_close (inConverter);
1094
 
    ucnv_close (outConverter);
1095
 
 
1096
 
    return u_terminateChars(target, targetSize, targetCapacity, pErrorCode);
1097
 
}
1098
 
 
1099
 
U_CAPI UConverterType  U_EXPORT2
1100
 
ucnv_getType(const UConverter* converter)
1101
 
{
1102
 
    int8_t type = converter->sharedData->staticData->conversionType;
1103
 
    if(type == UCNV_MBCS) {
1104
 
        return _MBCSGetType(converter);
1105
 
    }
1106
 
    return (UConverterType)type;
1107
 
}
1108
 
 
1109
 
U_CAPI void  U_EXPORT2
1110
 
ucnv_getStarters(const UConverter* converter, 
1111
 
                 UBool starters[256],
1112
 
                 UErrorCode* err)
1113
 
{
1114
 
    if (err == NULL || U_FAILURE(*err)) {
1115
 
        return;
1116
 
    }
1117
 
 
1118
 
    if(converter->sharedData->impl->getStarters != NULL) {
1119
 
        converter->sharedData->impl->getStarters(converter, starters, err);
1120
 
    } else {
1121
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
1122
 
    }
1123
 
}
1124
 
 
1125
 
static const UAmbiguousConverter *ucnv_getAmbiguous(const UConverter *cnv)
1126
 
{
1127
 
    UErrorCode errorCode;
1128
 
    const char *name;
1129
 
    int32_t i;
1130
 
 
1131
 
    if(cnv==NULL) {
1132
 
        return NULL;
1133
 
    }
1134
 
 
1135
 
    errorCode=U_ZERO_ERROR;
1136
 
    name=ucnv_getName(cnv, &errorCode);
1137
 
    if(U_FAILURE(errorCode)) {
1138
 
        return NULL;
1139
 
    }
1140
 
 
1141
 
    for(i=0; i<(int32_t)(sizeof(ambiguousConverters)/sizeof(UAmbiguousConverter)); ++i)
1142
 
    {
1143
 
        if(0==uprv_strcmp(name, ambiguousConverters[i].name))
1144
 
        {
1145
 
            return ambiguousConverters+i;
1146
 
        }
1147
 
    }
1148
 
 
1149
 
    return NULL;
1150
 
}
1151
 
 
1152
 
U_CAPI void  U_EXPORT2
1153
 
ucnv_fixFileSeparator(const UConverter *cnv, 
1154
 
                      UChar* source, 
1155
 
                      int32_t sourceLength) {
1156
 
    const UAmbiguousConverter *a;
1157
 
    int32_t i;
1158
 
    UChar variant5c;
1159
 
 
1160
 
    if(cnv==NULL || source==NULL || sourceLength<=0 || (a=ucnv_getAmbiguous(cnv))==NULL)
1161
 
    {
1162
 
        return;
1163
 
    }
1164
 
 
1165
 
    variant5c=a->variant5c;
1166
 
    for(i=0; i<sourceLength; ++i) {
1167
 
        if(source[i]==variant5c) {
1168
 
            source[i]=0x5c;
1169
 
        }
1170
 
    }
1171
 
}
1172
 
 
1173
 
U_CAPI UBool  U_EXPORT2
1174
 
ucnv_isAmbiguous(const UConverter *cnv) {
1175
 
    return (UBool)(ucnv_getAmbiguous(cnv)!=NULL);
1176
 
}
1177
 
 
1178
 
U_CAPI void  U_EXPORT2
1179
 
ucnv_setFallback(UConverter *cnv, UBool usesFallback)
1180
 
{
1181
 
    cnv->useFallback = usesFallback;
1182
 
}
1183
 
 
1184
 
U_CAPI UBool  U_EXPORT2
1185
 
ucnv_usesFallback(const UConverter *cnv)
1186
 
{
1187
 
    return cnv->useFallback;
1188
 
}
1189
 
 
1190
 
U_CAPI void  U_EXPORT2
1191
 
ucnv_getInvalidChars (const UConverter * converter,
1192
 
                      char *errBytes,
1193
 
                      int8_t * len,
1194
 
                      UErrorCode * err)
1195
 
{
1196
 
    if (err == NULL || U_FAILURE(*err))
1197
 
    {
1198
 
        return;
1199
 
    }
1200
 
    if (len == NULL || errBytes == NULL || converter == NULL)
1201
 
    {
1202
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
1203
 
        return;
1204
 
    }
1205
 
    if (*len < converter->invalidCharLength)
1206
 
    {
1207
 
        *err = U_INDEX_OUTOFBOUNDS_ERROR;
1208
 
        return;
1209
 
    }
1210
 
    if ((*len = converter->invalidCharLength) > 0)
1211
 
    {
1212
 
        uprv_memcpy (errBytes, converter->invalidCharBuffer, *len);
1213
 
    }
1214
 
}
1215
 
 
1216
 
U_CAPI void  U_EXPORT2
1217
 
ucnv_getInvalidUChars (const UConverter * converter,
1218
 
                       UChar *errChars,
1219
 
                       int8_t * len,
1220
 
                       UErrorCode * err)
1221
 
{
1222
 
    if (err == NULL || U_FAILURE(*err))
1223
 
    {
1224
 
        return;
1225
 
    }
1226
 
    if (len == NULL || errChars == NULL || converter == NULL)
1227
 
    {
1228
 
        *err = U_ILLEGAL_ARGUMENT_ERROR;
1229
 
        return;
1230
 
    }
1231
 
    if (*len < converter->invalidUCharLength)
1232
 
    {
1233
 
        *err = U_INDEX_OUTOFBOUNDS_ERROR;
1234
 
        return;
1235
 
    }
1236
 
    if ((*len = converter->invalidUCharLength) > 0)
1237
 
    {
1238
 
        uprv_memcpy (errChars, converter->invalidUCharBuffer, sizeof(UChar) * (*len));
1239
 
    }
1240
 
}
1241
 
 
1242
 
#define SIG_MAX_LEN 4
1243
 
 
1244
 
U_CAPI const char* U_EXPORT2
1245
 
ucnv_detectUnicodeSignature( const char* source,
1246
 
                             int32_t sourceLength,
1247
 
                             int32_t* signatureLength,
1248
 
                             UErrorCode* pErrorCode){
1249
 
    
1250
 
    /* initial 0xa5 bytes: make sure that if we read <4 
1251
 
     * bytes we don't misdetect something 
1252
 
     */
1253
 
    char start[SIG_MAX_LEN]={ '\xa5', '\xa5', '\xa5', '\xa5' };
1254
 
    int i = 0;
1255
 
 
1256
 
    if((pErrorCode==NULL) || U_FAILURE(*pErrorCode)){
1257
 
        return NULL;
1258
 
    }
1259
 
    
1260
 
    if(source == NULL || signatureLength == NULL || sourceLength < -1){
1261
 
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1262
 
        return NULL;
1263
 
    }
1264
 
 
1265
 
    if(sourceLength==-1){
1266
 
        sourceLength=uprv_strlen(source);
1267
 
    }
1268
 
 
1269
 
    
1270
 
    while(i<sourceLength&& i<SIG_MAX_LEN){
1271
 
        start[i]=source[i];
1272
 
        i++;
1273
 
    }
1274
 
  
1275
 
    if(start[0] == '\xFE' && start[1] == '\xFF') {
1276
 
        *signatureLength=2;
1277
 
        return  "UTF-16BE";
1278
 
    } else if(start[0] == '\xFF' && start[1] == '\xFE') {
1279
 
        if(start[2] == '\x00' && start[3] =='\x00'){
1280
 
            *signatureLength=4;
1281
 
            return "UTF-32LE";
1282
 
        } else {
1283
 
            *signatureLength=2;
1284
 
            return  "UTF-16LE";
1285
 
        }
1286
 
    } else if(start[0] == '\xEF' && start[1] == '\xBB' && start[2] == '\xBF') {
1287
 
        *signatureLength=3;
1288
 
        return  "UTF-8";
1289
 
    }else if(start[0] == '\x0E' && start[1] == '\xFE' && start[2] == '\xFF'){
1290
 
        *signatureLength=3;
1291
 
        return "SCSU";
1292
 
    }else if(start[0] == '\x00' && start[1] == '\x00' && 
1293
 
            start[2] == '\xFE' && start[3]=='\xFF'){
1294
 
        *signatureLength=4;
1295
 
        return  "UTF-32BE";
1296
 
    }else{
1297
 
        *signatureLength=0;
1298
 
        return NULL;
1299
 
    }
1300
 
}
1301
 
 
1302
 
/*
1303
 
 * Hey, Emacs, please set the following:
1304
 
 *
1305
 
 * Local Variables:
1306
 
 * indent-tabs-mode: nil
1307
 
 * End:
1308
 
 *
1309
 
 */
1310