• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *   Copyright (C) 1996-2011, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 *   file name:  ucol_res.cpp
7 *   encoding:   US-ASCII
8 *   tab size:   8 (not used)
9 *   indentation:4
10 *
11 * Description:
12 * This file contains dependencies that the collation run-time doesn't normally
13 * need. This mainly contains resource bundle usage and collation meta information
14 *
15 * Modification history
16 * Date        Name      Comments
17 * 1996-1999   various members of ICU team maintained C API for collation framework
18 * 02/16/2001  synwee    Added internal method getPrevSpecialCE
19 * 03/01/2001  synwee    Added maxexpansion functionality.
20 * 03/16/2001  weiv      Collation framework is rewritten in C and made UCA compliant
21 * 12/08/2004  grhoten   Split part of ucol.cpp into ucol_res.cpp
22 */
23 
24 #include "unicode/utypes.h"
25 
26 #if !UCONFIG_NO_COLLATION
27 #include "unicode/uloc.h"
28 #include "unicode/coll.h"
29 #include "unicode/tblcoll.h"
30 #include "unicode/caniter.h"
31 #include "unicode/uscript.h"
32 #include "unicode/ustring.h"
33 
34 #include "ucol_bld.h"
35 #include "ucol_imp.h"
36 #include "ucol_tok.h"
37 #include "ucol_elm.h"
38 #include "uresimp.h"
39 #include "ustr_imp.h"
40 #include "cstring.h"
41 #include "umutex.h"
42 #include "ucln_in.h"
43 #include "ustrenum.h"
44 #include "putilimp.h"
45 #include "utracimp.h"
46 #include "cmemory.h"
47 #include "uenumimp.h"
48 #include "ulist.h"
49 
50 U_NAMESPACE_USE
51 
52 static void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status);
53 
54 // static UCA. There is only one. Collators don't use it.
55 // It is referenced only in ucol_initUCA and ucol_cleanup
56 static UCollator* _staticUCA = NULL;
57 // static pointer to udata memory. Inited in ucol_initUCA
58 // used for cleanup in ucol_cleanup
59 static UDataMemory* UCA_DATA_MEM = NULL;
60 
61 U_CDECL_BEGIN
62 static UBool U_CALLCONV
ucol_res_cleanup(void)63 ucol_res_cleanup(void)
64 {
65     if (UCA_DATA_MEM) {
66         udata_close(UCA_DATA_MEM);
67         UCA_DATA_MEM = NULL;
68     }
69     if (_staticUCA) {
70         ucol_close(_staticUCA);
71         _staticUCA = NULL;
72     }
73     return TRUE;
74 }
75 
76 static UBool U_CALLCONV
isAcceptableUCA(void *,const char *,const char *,const UDataInfo * pInfo)77 isAcceptableUCA(void * /*context*/,
78              const char * /*type*/, const char * /*name*/,
79              const UDataInfo *pInfo){
80   /* context, type & name are intentionally not used */
81     if( pInfo->size>=20 &&
82         pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
83         pInfo->charsetFamily==U_CHARSET_FAMILY &&
84         pInfo->dataFormat[0]==UCA_DATA_FORMAT_0 &&   /* dataFormat="UCol" */
85         pInfo->dataFormat[1]==UCA_DATA_FORMAT_1 &&
86         pInfo->dataFormat[2]==UCA_DATA_FORMAT_2 &&
87         pInfo->dataFormat[3]==UCA_DATA_FORMAT_3 &&
88         pInfo->formatVersion[0]==UCA_FORMAT_VERSION_0
89 #if UCA_FORMAT_VERSION_1!=0
90         && pInfo->formatVersion[1]>=UCA_FORMAT_VERSION_1
91 #endif
92         //pInfo->formatVersion[1]==UCA_FORMAT_VERSION_1 &&
93         //pInfo->formatVersion[2]==UCA_FORMAT_VERSION_2 && // Too harsh
94         //pInfo->formatVersion[3]==UCA_FORMAT_VERSION_3 && // Too harsh
95         ) {
96         UVersionInfo UCDVersion;
97         u_getUnicodeVersion(UCDVersion);
98         return (UBool)(pInfo->dataVersion[0]==UCDVersion[0]
99             && pInfo->dataVersion[1]==UCDVersion[1]);
100             //&& pInfo->dataVersion[2]==ucaDataInfo.dataVersion[2]
101             //&& pInfo->dataVersion[3]==ucaDataInfo.dataVersion[3]);
102     } else {
103         return FALSE;
104     }
105 }
106 U_CDECL_END
107 
108 /* do not close UCA returned by ucol_initUCA! */
109 UCollator *
ucol_initUCA(UErrorCode * status)110 ucol_initUCA(UErrorCode *status) {
111     if(U_FAILURE(*status)) {
112         return NULL;
113     }
114     UBool needsInit;
115     UMTX_CHECK(NULL, (_staticUCA == NULL), needsInit);
116 
117     if(needsInit) {
118         UDataMemory *result = udata_openChoice(U_ICUDATA_COLL, UCA_DATA_TYPE, UCA_DATA_NAME, isAcceptableUCA, NULL, status);
119 
120         if(U_SUCCESS(*status)){
121             UCollator *newUCA = ucol_initCollator((const UCATableHeader *)udata_getMemory(result), NULL, NULL, status);
122             if(U_SUCCESS(*status)){
123                 // Initalize variables for implicit generation
124                 uprv_uca_initImplicitConstants(status);
125 
126                 umtx_lock(NULL);
127                 if(_staticUCA == NULL) {
128                     UCA_DATA_MEM = result;
129                     _staticUCA = newUCA;
130                     newUCA = NULL;
131                     result = NULL;
132                 }
133                 umtx_unlock(NULL);
134 
135                 ucln_i18n_registerCleanup(UCLN_I18N_UCOL_RES, ucol_res_cleanup);
136                 if(newUCA != NULL) {
137                     ucol_close(newUCA);
138                     udata_close(result);
139                 }
140             }else{
141                 ucol_close(newUCA);
142                 udata_close(result);
143             }
144         }
145         else {
146             udata_close(result);
147         }
148     }
149     return _staticUCA;
150 }
151 
152 U_CAPI void U_EXPORT2
ucol_forgetUCA(void)153 ucol_forgetUCA(void)
154 {
155     _staticUCA = NULL;
156     UCA_DATA_MEM = NULL;
157 }
158 
159 /****************************************************************************/
160 /* Following are the open/close functions                                   */
161 /*                                                                          */
162 /****************************************************************************/
163 static UCollator*
tryOpeningFromRules(UResourceBundle * collElem,UErrorCode * status)164 tryOpeningFromRules(UResourceBundle *collElem, UErrorCode *status) {
165     int32_t rulesLen = 0;
166     const UChar *rules = ures_getStringByKey(collElem, "Sequence", &rulesLen, status);
167     return ucol_openRules(rules, rulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, status);
168 }
169 
170 
171 // API in ucol_imp.h
172 
173 U_CFUNC UCollator*
ucol_open_internal(const char * loc,UErrorCode * status)174 ucol_open_internal(const char *loc,
175                    UErrorCode *status)
176 {
177     UErrorCode intStatus = U_ZERO_ERROR;
178     const UCollator* UCA = ucol_initUCA(status);
179 
180     /* New version */
181     if(U_FAILURE(*status)) return 0;
182 
183 
184 
185     UCollator *result = NULL;
186     UResourceBundle *b = ures_open(U_ICUDATA_COLL, loc, status);
187 
188     /* we try to find stuff from keyword */
189     UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status);
190     UResourceBundle *collElem = NULL;
191     char keyBuffer[256];
192     // if there is a keyword, we pick it up and try to get elements
193     if(!uloc_getKeywordValue(loc, "collation", keyBuffer, 256, status) ||
194         !uprv_strcmp(keyBuffer,"default")) { /* Treat 'zz@collation=default' as 'zz'. */
195         // no keyword. we try to find the default setting, which will give us the keyword value
196         intStatus = U_ZERO_ERROR;
197         // finding default value does not affect collation fallback status
198         UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, &intStatus);
199         if(U_SUCCESS(intStatus)) {
200             int32_t defaultKeyLen = 0;
201             const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, &intStatus);
202             u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen);
203             keyBuffer[defaultKeyLen] = 0;
204         } else {
205             *status = U_INTERNAL_PROGRAM_ERROR;
206             return NULL;
207         }
208         ures_close(defaultColl);
209     }
210     collElem = ures_getByKeyWithFallback(collations, keyBuffer, collations, status);
211     collations = NULL; // We just reused the collations object as collElem.
212 
213     UResourceBundle *binary = NULL;
214     UResourceBundle *reorderRes = NULL;
215 
216     if(*status == U_MISSING_RESOURCE_ERROR) { /* We didn't find the tailoring data, we fallback to the UCA */
217         *status = U_USING_DEFAULT_WARNING;
218         result = ucol_initCollator(UCA->image, result, UCA, status);
219         if (U_FAILURE(*status)) {
220             goto clean;
221         }
222         // if we use UCA, real locale is root
223         ures_close(b);
224         b = ures_open(U_ICUDATA_COLL, "", status);
225         ures_close(collElem);
226         collElem = ures_open(U_ICUDATA_COLL, "", status);
227         if(U_FAILURE(*status)) {
228             goto clean;
229         }
230         result->hasRealData = FALSE;
231     } else if(U_SUCCESS(*status)) {
232         intStatus = U_ZERO_ERROR;
233 
234         binary = ures_getByKey(collElem, "%%CollationBin", NULL, &intStatus);
235 
236         if(intStatus == U_MISSING_RESOURCE_ERROR) { /* we didn't find the binary image, we should use the rules */
237             binary = NULL;
238             result = tryOpeningFromRules(collElem, status);
239             if(U_FAILURE(*status)) {
240                 goto clean;
241             }
242         } else if(U_SUCCESS(intStatus)) { /* otherwise, we'll pick a collation data that exists */
243             int32_t len = 0;
244             const uint8_t *inData = ures_getBinary(binary, &len, status);
245             if(U_FAILURE(*status)) {
246                 goto clean;
247             }
248             UCATableHeader *colData = (UCATableHeader *)inData;
249             if(uprv_memcmp(colData->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)) != 0 ||
250                 uprv_memcmp(colData->UCDVersion, UCA->image->UCDVersion, sizeof(UVersionInfo)) != 0 ||
251                 colData->version[0] != UCOL_BUILDER_VERSION)
252             {
253                 *status = U_DIFFERENT_UCA_VERSION;
254                 result = tryOpeningFromRules(collElem, status);
255             } else {
256                 if(U_FAILURE(*status)){
257                     goto clean;
258                 }
259                 if((uint32_t)len > (paddedsize(sizeof(UCATableHeader)) + paddedsize(sizeof(UColOptionSet)))) {
260                     result = ucol_initCollator((const UCATableHeader *)inData, result, UCA, status);
261                     if(U_FAILURE(*status)){
262                         goto clean;
263                     }
264                     result->hasRealData = TRUE;
265                 } else {
266                     result = ucol_initCollator(UCA->image, result, UCA, status);
267                     ucol_setOptionsFromHeader(result, (UColOptionSet *)(inData+((const UCATableHeader *)inData)->options), status);
268                     if(U_FAILURE(*status)){
269                         goto clean;
270                     }
271                     result->hasRealData = FALSE;
272                 }
273                 result->freeImageOnClose = FALSE;
274 
275                 reorderRes = ures_getByKey(collElem, "%%ReorderCodes", NULL, &intStatus);
276                 if (U_SUCCESS(intStatus)) {
277                     int32_t reorderCodesLen = 0;
278                     const int32_t* reorderCodes = ures_getIntVector(reorderRes, &reorderCodesLen, status);
279                     if (reorderCodesLen > 0) {
280                         ucol_setReorderCodes(result, reorderCodes, reorderCodesLen, status);
281                         // copy the reorder codes into the default reorder codes
282                         result->defaultReorderCodesLength = result->reorderCodesLength;
283                         result->defaultReorderCodes =  (int32_t*) uprv_malloc(result->defaultReorderCodesLength * sizeof(int32_t));
284                         uprv_memcpy(result->defaultReorderCodes, result->reorderCodes, result->defaultReorderCodesLength * sizeof(int32_t));
285                         result->freeDefaultReorderCodesOnClose = TRUE;
286                     }
287                     if (U_FAILURE(*status)) {
288                         goto clean;
289                     }
290                 }
291             }
292 
293         } else { // !U_SUCCESS(binaryStatus)
294             if(U_SUCCESS(*status)) {
295                 *status = intStatus; // propagate underlying error
296             }
297             goto clean;
298         }
299         intStatus = U_ZERO_ERROR;
300         result->rules = ures_getStringByKey(collElem, "Sequence", &result->rulesLength, &intStatus);
301         result->freeRulesOnClose = FALSE;
302     } else { /* There is another error, and we're just gonna clean up */
303         goto clean;
304     }
305 
306     intStatus = U_ZERO_ERROR;
307     result->ucaRules = ures_getStringByKey(b,"UCARules",NULL,&intStatus);
308 
309     if(loc == NULL) {
310         loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status);
311     }
312     result->requestedLocale = uprv_strdup(loc);
313     /* test for NULL */
314     if (result->requestedLocale == NULL) {
315         *status = U_MEMORY_ALLOCATION_ERROR;
316         goto clean;
317     }
318     loc = ures_getLocaleByType(collElem, ULOC_ACTUAL_LOCALE, status);
319     result->actualLocale = uprv_strdup(loc);
320     /* test for NULL */
321     if (result->actualLocale == NULL) {
322         *status = U_MEMORY_ALLOCATION_ERROR;
323         goto clean;
324     }
325     loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status);
326     result->validLocale = uprv_strdup(loc);
327     /* test for NULL */
328     if (result->validLocale == NULL) {
329         *status = U_MEMORY_ALLOCATION_ERROR;
330         goto clean;
331     }
332 
333     ures_close(b);
334     ures_close(collElem);
335     ures_close(binary);
336     ures_close(reorderRes);
337     return result;
338 
339 clean:
340     ures_close(b);
341     ures_close(collElem);
342     ures_close(binary);
343     ures_close(reorderRes);
344     ucol_close(result);
345     return NULL;
346 }
347 
348 U_CAPI UCollator*
ucol_open(const char * loc,UErrorCode * status)349 ucol_open(const char *loc,
350           UErrorCode *status)
351 {
352     U_NAMESPACE_USE
353 
354     UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN);
355     UTRACE_DATA1(UTRACE_INFO, "locale = \"%s\"", loc);
356     UCollator *result = NULL;
357 
358 #if !UCONFIG_NO_SERVICE
359     result = Collator::createUCollator(loc, status);
360     if (result == NULL)
361 #endif
362     {
363         result = ucol_open_internal(loc, status);
364     }
365     UTRACE_EXIT_PTR_STATUS(result, *status);
366     return result;
367 }
368 
369 
370 UCollator*
ucol_openRulesForImport(const UChar * rules,int32_t rulesLength,UColAttributeValue normalizationMode,UCollationStrength strength,UParseError * parseError,GetCollationRulesFunction importFunc,void * context,UErrorCode * status)371 ucol_openRulesForImport( const UChar        *rules,
372                          int32_t            rulesLength,
373                          UColAttributeValue normalizationMode,
374                          UCollationStrength strength,
375                          UParseError        *parseError,
376                          GetCollationRulesFunction  importFunc,
377                          void* context,
378                          UErrorCode         *status)
379 {
380     UColTokenParser src;
381     UColAttributeValue norm;
382     UParseError tErr;
383 
384     if(status == NULL || U_FAILURE(*status)){
385         return 0;
386     }
387 
388     if(rules == NULL || rulesLength < -1) {
389         *status = U_ILLEGAL_ARGUMENT_ERROR;
390         return 0;
391     }
392 
393     if(rulesLength == -1) {
394         rulesLength = u_strlen(rules);
395     }
396 
397     if(parseError == NULL){
398         parseError = &tErr;
399     }
400 
401     switch(normalizationMode) {
402     case UCOL_OFF:
403     case UCOL_ON:
404     case UCOL_DEFAULT:
405         norm = normalizationMode;
406         break;
407     default:
408         *status = U_ILLEGAL_ARGUMENT_ERROR;
409         return 0;
410     }
411 
412     UCollator *result = NULL;
413     UCATableHeader *table = NULL;
414     UCollator *UCA = ucol_initUCA(status);
415 
416     if(U_FAILURE(*status)){
417         return NULL;
418     }
419 
420     ucol_tok_initTokenList(&src, rules, rulesLength, UCA, importFunc, context, status);
421     ucol_tok_assembleTokenList(&src,parseError, status);
422 
423     if(U_FAILURE(*status)) {
424         /* if status is U_ILLEGAL_ARGUMENT_ERROR, src->current points at the offending option */
425         /* if status is U_INVALID_FORMAT_ERROR, src->current points after the problematic part of the rules */
426         /* so something might be done here... or on lower level */
427 #ifdef UCOL_DEBUG
428         if(*status == U_ILLEGAL_ARGUMENT_ERROR) {
429             fprintf(stderr, "bad option starting at offset %i\n", (int)(src.current-src.source));
430         } else {
431             fprintf(stderr, "invalid rule just before offset %i\n", (int)(src.current-src.source));
432         }
433 #endif
434         goto cleanup;
435     }
436 
437     if(src.resultLen > 0 || src.removeSet != NULL) { /* we have a set of rules, let's make something of it */
438         /* also, if we wanted to remove some contractions, we should make a tailoring */
439         table = ucol_assembleTailoringTable(&src, status);
440         if(U_SUCCESS(*status)) {
441             // builder version
442             table->version[0] = UCOL_BUILDER_VERSION;
443             // no tailoring information on this level
444             table->version[1] = table->version[2] = table->version[3] = 0;
445             // set UCD version
446             u_getUnicodeVersion(table->UCDVersion);
447             // set UCA version
448             uprv_memcpy(table->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo));
449             result = ucol_initCollator(table, 0, UCA, status);
450             if (U_FAILURE(*status)) {
451                 goto cleanup;
452             }
453             result->hasRealData = TRUE;
454             result->freeImageOnClose = TRUE;
455         }
456     } else { /* no rules, but no error either */
457         // must be only options
458         // We will init the collator from UCA
459         result = ucol_initCollator(UCA->image, 0, UCA, status);
460         // Check for null result
461         if (U_FAILURE(*status)) {
462             goto cleanup;
463         }
464         // And set only the options
465         UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet));
466         /* test for NULL */
467         if (opts == NULL) {
468             *status = U_MEMORY_ALLOCATION_ERROR;
469             goto cleanup;
470         }
471         uprv_memcpy(opts, src.opts, sizeof(UColOptionSet));
472         ucol_setOptionsFromHeader(result, opts, status);
473         ucol_setReorderCodesFromParser(result, &src, status);
474         result->freeOptionsOnClose = TRUE;
475         result->hasRealData = FALSE;
476         result->freeImageOnClose = FALSE;
477     }
478 
479     // BEGIN android-added
480     // Apply the fixes for ICU ticket#9095
481     ucol_setReorderCodesFromParser(result, &src, status);
482     // END android-added
483 
484     if(U_SUCCESS(*status)) {
485         UChar *newRules;
486         result->dataVersion[0] = UCOL_BUILDER_VERSION;
487         if(rulesLength > 0) {
488             newRules = (UChar *)uprv_malloc((rulesLength+1)*U_SIZEOF_UCHAR);
489             /* test for NULL */
490             if (newRules == NULL) {
491                 *status = U_MEMORY_ALLOCATION_ERROR;
492                 goto cleanup;
493             }
494             uprv_memcpy(newRules, rules, rulesLength*U_SIZEOF_UCHAR);
495             newRules[rulesLength]=0;
496             result->rules = newRules;
497             result->rulesLength = rulesLength;
498             result->freeRulesOnClose = TRUE;
499         }
500         result->ucaRules = NULL;
501         result->actualLocale = NULL;
502         result->validLocale = NULL;
503         result->requestedLocale = NULL;
504         ucol_buildPermutationTable(result, status);
505         ucol_setAttribute(result, UCOL_STRENGTH, strength, status);
506         ucol_setAttribute(result, UCOL_NORMALIZATION_MODE, norm, status);
507     } else {
508 cleanup:
509         if(result != NULL) {
510             ucol_close(result);
511         } else {
512             if(table != NULL) {
513                 uprv_free(table);
514             }
515         }
516         result = NULL;
517     }
518 
519     ucol_tok_closeTokenList(&src);
520 
521     return result;
522 }
523 
524 U_CAPI UCollator* U_EXPORT2
ucol_openRules(const UChar * rules,int32_t rulesLength,UColAttributeValue normalizationMode,UCollationStrength strength,UParseError * parseError,UErrorCode * status)525 ucol_openRules( const UChar        *rules,
526                int32_t            rulesLength,
527                UColAttributeValue normalizationMode,
528                UCollationStrength strength,
529                UParseError        *parseError,
530                UErrorCode         *status)
531 {
532     return ucol_openRulesForImport(rules,
533                                    rulesLength,
534                                    normalizationMode,
535                                    strength,
536                                    parseError,
537                                    ucol_tok_getRulesFromBundle,
538                                    NULL,
539                                    status);
540 }
541 
542 U_CAPI int32_t U_EXPORT2
ucol_getRulesEx(const UCollator * coll,UColRuleOption delta,UChar * buffer,int32_t bufferLen)543 ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) {
544     UErrorCode status = U_ZERO_ERROR;
545     int32_t len = 0;
546     int32_t UCAlen = 0;
547     const UChar* ucaRules = 0;
548     const UChar *rules = ucol_getRules(coll, &len);
549     if(delta == UCOL_FULL_RULES) {
550         /* take the UCA rules and append real rules at the end */
551         /* UCA rules will be probably coming from the root RB */
552         ucaRules = coll->ucaRules;
553         if (ucaRules) {
554             UCAlen = u_strlen(ucaRules);
555         }
556         /*
557         ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status);
558         UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status);
559         UResourceBundle*  uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status);
560         ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status);
561         ures_close(uca);
562         ures_close(cresb);
563         */
564     }
565     if(U_FAILURE(status)) {
566         return 0;
567     }
568     if(buffer!=0 && bufferLen>0){
569         *buffer=0;
570         if(UCAlen > 0) {
571             u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen));
572         }
573         if(len > 0 && bufferLen > UCAlen) {
574             u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen));
575         }
576     }
577     return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status);
578 }
579 
580 static const UChar _NUL = 0;
581 
582 U_CAPI const UChar* U_EXPORT2
ucol_getRules(const UCollator * coll,int32_t * length)583 ucol_getRules(    const    UCollator       *coll,
584               int32_t            *length)
585 {
586     if(coll->rules != NULL) {
587         *length = coll->rulesLength;
588         return coll->rules;
589     }
590     else {
591         *length = 0;
592         return &_NUL;
593     }
594 }
595 
596 U_CAPI UBool U_EXPORT2
ucol_equals(const UCollator * source,const UCollator * target)597 ucol_equals(const UCollator *source, const UCollator *target) {
598     UErrorCode status = U_ZERO_ERROR;
599     // if pointers are equal, collators are equal
600     if(source == target) {
601         return TRUE;
602     }
603     int32_t i = 0, j = 0;
604     // if any of attributes are different, collators are not equal
605     for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
606         if(ucol_getAttribute(source, (UColAttribute)i, &status) != ucol_getAttribute(target, (UColAttribute)i, &status) || U_FAILURE(status)) {
607             return FALSE;
608         }
609     }
610     if (source->reorderCodesLength != target->reorderCodesLength){
611         return FALSE;
612     }
613     for (i = 0; i < source->reorderCodesLength; i++) {
614         if(source->reorderCodes[i] != target->reorderCodes[i]) {
615             return FALSE;
616         }
617     }
618 
619     int32_t sourceRulesLen = 0, targetRulesLen = 0;
620     const UChar *sourceRules = ucol_getRules(source, &sourceRulesLen);
621     const UChar *targetRules = ucol_getRules(target, &targetRulesLen);
622 
623     if(sourceRulesLen == targetRulesLen && u_strncmp(sourceRules, targetRules, sourceRulesLen) == 0) {
624         // all the attributes are equal and the rules are equal - collators are equal
625         return(TRUE);
626     }
627     // hard part, need to construct tree from rules and see if they yield the same tailoring
628     UBool result = TRUE;
629     UParseError parseError;
630     UColTokenParser sourceParser, targetParser;
631     int32_t sourceListLen = 0, targetListLen = 0;
632     ucol_tok_initTokenList(&sourceParser, sourceRules, sourceRulesLen, source->UCA, ucol_tok_getRulesFromBundle, NULL, &status);
633     ucol_tok_initTokenList(&targetParser, targetRules, targetRulesLen, target->UCA, ucol_tok_getRulesFromBundle, NULL, &status);
634     sourceListLen = ucol_tok_assembleTokenList(&sourceParser, &parseError, &status);
635     targetListLen = ucol_tok_assembleTokenList(&targetParser, &parseError, &status);
636 
637     if(sourceListLen != targetListLen) {
638         // different number of resets
639         result = FALSE;
640     } else {
641         UColToken *sourceReset = NULL, *targetReset = NULL;
642         UChar *sourceResetString = NULL, *targetResetString = NULL;
643         int32_t sourceStringLen = 0, targetStringLen = 0;
644         for(i = 0; i < sourceListLen; i++) {
645             sourceReset = sourceParser.lh[i].reset;
646             sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF);
647             sourceStringLen = sourceReset->source >> 24;
648             for(j = 0; j < sourceListLen; j++) {
649                 targetReset = targetParser.lh[j].reset;
650                 targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF);
651                 targetStringLen = targetReset->source >> 24;
652                 if(sourceStringLen == targetStringLen && (u_strncmp(sourceResetString, targetResetString, sourceStringLen) == 0)) {
653                     sourceReset = sourceParser.lh[i].first;
654                     targetReset = targetParser.lh[j].first;
655                     while(sourceReset != NULL && targetReset != NULL) {
656                         sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF);
657                         sourceStringLen = sourceReset->source >> 24;
658                         targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF);
659                         targetStringLen = targetReset->source >> 24;
660                         if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) {
661                             result = FALSE;
662                             goto returnResult;
663                         }
664                         // probably also need to check the expansions
665                         if(sourceReset->expansion) {
666                             if(!targetReset->expansion) {
667                                 result = FALSE;
668                                 goto returnResult;
669                             } else {
670                                 // compare expansions
671                                 sourceResetString = sourceParser.source+(sourceReset->expansion& 0xFFFFFF);
672                                 sourceStringLen = sourceReset->expansion >> 24;
673                                 targetResetString = targetParser.source+(targetReset->expansion & 0xFFFFFF);
674                                 targetStringLen = targetReset->expansion >> 24;
675                                 if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) {
676                                     result = FALSE;
677                                     goto returnResult;
678                                 }
679                             }
680                         } else {
681                             if(targetReset->expansion) {
682                                 result = FALSE;
683                                 goto returnResult;
684                             }
685                         }
686                         sourceReset = sourceReset->next;
687                         targetReset = targetReset->next;
688                     }
689                     if(sourceReset != targetReset) { // at least one is not NULL
690                         // there are more tailored elements in one list
691                         result = FALSE;
692                         goto returnResult;
693                     }
694 
695 
696                     break;
697                 }
698             }
699             // couldn't find the reset anchor, so the collators are not equal
700             if(j == sourceListLen) {
701                 result = FALSE;
702                 goto returnResult;
703             }
704         }
705     }
706 
707 returnResult:
708     ucol_tok_closeTokenList(&sourceParser);
709     ucol_tok_closeTokenList(&targetParser);
710     return result;
711 
712 }
713 
714 U_CAPI int32_t U_EXPORT2
ucol_getDisplayName(const char * objLoc,const char * dispLoc,UChar * result,int32_t resultLength,UErrorCode * status)715 ucol_getDisplayName(    const    char        *objLoc,
716                     const    char        *dispLoc,
717                     UChar             *result,
718                     int32_t         resultLength,
719                     UErrorCode        *status)
720 {
721     U_NAMESPACE_USE
722 
723     if(U_FAILURE(*status)) return -1;
724     UnicodeString dst;
725     if(!(result==NULL && resultLength==0)) {
726         // NULL destination for pure preflighting: empty dummy string
727         // otherwise, alias the destination buffer
728         dst.setTo(result, 0, resultLength);
729     }
730     Collator::getDisplayName(Locale(objLoc), Locale(dispLoc), dst);
731     return dst.extract(result, resultLength, *status);
732 }
733 
734 U_CAPI const char* U_EXPORT2
ucol_getAvailable(int32_t index)735 ucol_getAvailable(int32_t index)
736 {
737     int32_t count = 0;
738     const Locale *loc = Collator::getAvailableLocales(count);
739     if (loc != NULL && index < count) {
740         return loc[index].getName();
741     }
742     return NULL;
743 }
744 
745 U_CAPI int32_t U_EXPORT2
ucol_countAvailable()746 ucol_countAvailable()
747 {
748     int32_t count = 0;
749     Collator::getAvailableLocales(count);
750     return count;
751 }
752 
753 #if !UCONFIG_NO_SERVICE
754 U_CAPI UEnumeration* U_EXPORT2
ucol_openAvailableLocales(UErrorCode * status)755 ucol_openAvailableLocales(UErrorCode *status) {
756     U_NAMESPACE_USE
757 
758     // This is a wrapper over Collator::getAvailableLocales()
759     if (U_FAILURE(*status)) {
760         return NULL;
761     }
762     StringEnumeration *s = Collator::getAvailableLocales();
763     if (s == NULL) {
764         *status = U_MEMORY_ALLOCATION_ERROR;
765         return NULL;
766     }
767     return uenum_openFromStringEnumeration(s, status);
768 }
769 #endif
770 
771 // Note: KEYWORDS[0] != RESOURCE_NAME - alan
772 
773 static const char RESOURCE_NAME[] = "collations";
774 
775 static const char* const KEYWORDS[] = { "collation" };
776 
777 #define KEYWORD_COUNT (sizeof(KEYWORDS)/sizeof(KEYWORDS[0]))
778 
779 U_CAPI UEnumeration* U_EXPORT2
ucol_getKeywords(UErrorCode * status)780 ucol_getKeywords(UErrorCode *status) {
781     UEnumeration *result = NULL;
782     if (U_SUCCESS(*status)) {
783         return uenum_openCharStringsEnumeration(KEYWORDS, KEYWORD_COUNT, status);
784     }
785     return result;
786 }
787 
788 U_CAPI UEnumeration* U_EXPORT2
ucol_getKeywordValues(const char * keyword,UErrorCode * status)789 ucol_getKeywordValues(const char *keyword, UErrorCode *status) {
790     if (U_FAILURE(*status)) {
791         return NULL;
792     }
793     // hard-coded to accept exactly one collation keyword
794     // modify if additional collation keyword is added later
795     if (keyword==NULL || uprv_strcmp(keyword, KEYWORDS[0])!=0)
796     {
797         *status = U_ILLEGAL_ARGUMENT_ERROR;
798         return NULL;
799     }
800     return ures_getKeywordValues(U_ICUDATA_COLL, RESOURCE_NAME, status);
801 }
802 
803 static const UEnumeration defaultKeywordValues = {
804     NULL,
805     NULL,
806     ulist_close_keyword_values_iterator,
807     ulist_count_keyword_values,
808     uenum_unextDefault,
809     ulist_next_keyword_value,
810     ulist_reset_keyword_values_iterator
811 };
812 
813 #include <stdio.h>
814 
815 U_CAPI UEnumeration* U_EXPORT2
ucol_getKeywordValuesForLocale(const char *,const char * locale,UBool,UErrorCode * status)816 ucol_getKeywordValuesForLocale(const char* /*key*/, const char* locale,
817                                UBool /*commonlyUsed*/, UErrorCode* status) {
818     /* Get the locale base name. */
819     char localeBuffer[ULOC_FULLNAME_CAPACITY] = "";
820     uloc_getBaseName(locale, localeBuffer, sizeof(localeBuffer), status);
821 
822     /* Create the 2 lists
823      * -values is the temp location for the keyword values
824      * -results hold the actual list used by the UEnumeration object
825      */
826     UList *values = ulist_createEmptyList(status);
827     UList *results = ulist_createEmptyList(status);
828     UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
829     if (U_FAILURE(*status) || en == NULL) {
830         if (en == NULL) {
831             *status = U_MEMORY_ALLOCATION_ERROR;
832         } else {
833             uprv_free(en);
834         }
835         ulist_deleteList(values);
836         ulist_deleteList(results);
837         return NULL;
838     }
839 
840     memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
841     en->context = results;
842 
843     /* Open the resource bundle for collation with the given locale. */
844     UResourceBundle bundle, collations, collres, defres;
845     ures_initStackObject(&bundle);
846     ures_initStackObject(&collations);
847     ures_initStackObject(&collres);
848     ures_initStackObject(&defres);
849 
850     ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status);
851 
852     while (U_SUCCESS(*status)) {
853         ures_getByKey(&bundle, RESOURCE_NAME, &collations, status);
854         ures_resetIterator(&collations);
855         while (U_SUCCESS(*status) && ures_hasNext(&collations)) {
856             ures_getNextResource(&collations, &collres, status);
857             const char *key = ures_getKey(&collres);
858             /* If the key is default, get the string and store it in results list only
859              * if results list is empty.
860              */
861             if (uprv_strcmp(key, "default") == 0) {
862                 if (ulist_getListSize(results) == 0) {
863                     char *defcoll = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY);
864                     int32_t defcollLength = ULOC_KEYWORDS_CAPACITY;
865 
866                     ures_getNextResource(&collres, &defres, status);
867 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
868 			/* optimize - use the utf-8 string */
869                     ures_getUTF8String(&defres, defcoll, &defcollLength, TRUE, status);
870 #else
871                     {
872                        const UChar* defString = ures_getString(&defres, &defcollLength, status);
873                        if(U_SUCCESS(*status)) {
874 			   if(defcollLength+1 > ULOC_KEYWORDS_CAPACITY) {
875 				*status = U_BUFFER_OVERFLOW_ERROR;
876 			   } else {
877                            	u_UCharsToChars(defString, defcoll, defcollLength+1);
878 			   }
879                        }
880                     }
881 #endif
882 
883                     ulist_addItemBeginList(results, defcoll, TRUE, status);
884                 }
885             } else {
886                 ulist_addItemEndList(values, key, FALSE, status);
887             }
888         }
889 
890         /* If the locale is "" this is root so exit. */
891         if (uprv_strlen(localeBuffer) == 0) {
892             break;
893         }
894         /* Get the parent locale and open a new resource bundle. */
895         uloc_getParent(localeBuffer, localeBuffer, sizeof(localeBuffer), status);
896         ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status);
897     }
898 
899     ures_close(&defres);
900     ures_close(&collres);
901     ures_close(&collations);
902     ures_close(&bundle);
903 
904     if (U_SUCCESS(*status)) {
905         char *value = NULL;
906         ulist_resetList(values);
907         while ((value = (char *)ulist_getNext(values)) != NULL) {
908             if (!ulist_containsString(results, value, (int32_t)uprv_strlen(value))) {
909                 ulist_addItemEndList(results, value, FALSE, status);
910                 if (U_FAILURE(*status)) {
911                     break;
912                 }
913             }
914         }
915     }
916 
917     ulist_deleteList(values);
918 
919     if (U_FAILURE(*status)){
920         uenum_close(en);
921         en = NULL;
922     } else {
923         ulist_resetList(results);
924     }
925 
926     return en;
927 }
928 
929 U_CAPI int32_t U_EXPORT2
ucol_getFunctionalEquivalent(char * result,int32_t resultCapacity,const char * keyword,const char * locale,UBool * isAvailable,UErrorCode * status)930 ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity,
931                              const char* keyword, const char* locale,
932                              UBool* isAvailable, UErrorCode* status)
933 {
934     // N.B.: Resource name is "collations" but keyword is "collation"
935     return ures_getFunctionalEquivalent(result, resultCapacity, U_ICUDATA_COLL,
936         "collations", keyword, locale,
937         isAvailable, TRUE, status);
938 }
939 
940 /* returns the locale name the collation data comes from */
941 U_CAPI const char * U_EXPORT2
ucol_getLocale(const UCollator * coll,ULocDataLocaleType type,UErrorCode * status)942 ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) {
943     return ucol_getLocaleByType(coll, type, status);
944 }
945 
946 U_CAPI const char * U_EXPORT2
ucol_getLocaleByType(const UCollator * coll,ULocDataLocaleType type,UErrorCode * status)947 ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) {
948     const char *result = NULL;
949     if(status == NULL || U_FAILURE(*status)) {
950         return NULL;
951     }
952     UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE);
953     UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll);
954 
955     switch(type) {
956     case ULOC_ACTUAL_LOCALE:
957         result = coll->actualLocale;
958         break;
959     case ULOC_VALID_LOCALE:
960         result = coll->validLocale;
961         break;
962     case ULOC_REQUESTED_LOCALE:
963         result = coll->requestedLocale;
964         break;
965     default:
966         *status = U_ILLEGAL_ARGUMENT_ERROR;
967     }
968     UTRACE_DATA1(UTRACE_INFO, "result = %s", result);
969     UTRACE_EXIT_STATUS(*status);
970     return result;
971 }
972 
973 U_CFUNC void U_EXPORT2
ucol_setReqValidLocales(UCollator * coll,char * requestedLocaleToAdopt,char * validLocaleToAdopt,char * actualLocaleToAdopt)974 ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *validLocaleToAdopt, char *actualLocaleToAdopt)
975 {
976     if (coll) {
977         if (coll->validLocale) {
978             uprv_free(coll->validLocale);
979         }
980         coll->validLocale = validLocaleToAdopt;
981         if (coll->requestedLocale) { // should always have
982             uprv_free(coll->requestedLocale);
983         }
984         coll->requestedLocale = requestedLocaleToAdopt;
985         if (coll->actualLocale) {
986             uprv_free(coll->actualLocale);
987         }
988         coll->actualLocale = actualLocaleToAdopt;
989     }
990 }
991 
992 U_CAPI USet * U_EXPORT2
ucol_getTailoredSet(const UCollator * coll,UErrorCode * status)993 ucol_getTailoredSet(const UCollator *coll, UErrorCode *status)
994 {
995     U_NAMESPACE_USE
996 
997     if(status == NULL || U_FAILURE(*status)) {
998         return NULL;
999     }
1000     if(coll == NULL || coll->UCA == NULL) {
1001         *status = U_ILLEGAL_ARGUMENT_ERROR;
1002         return NULL;
1003     }
1004     UParseError parseError;
1005     UColTokenParser src;
1006     int32_t rulesLen = 0;
1007     const UChar *rules = ucol_getRules(coll, &rulesLen);
1008     UBool startOfRules = TRUE;
1009     // we internally use the C++ class, for the following reasons:
1010     // 1. we need to utilize canonical iterator, which is a C++ only class
1011     // 2. canonical iterator returns UnicodeStrings - USet cannot take them
1012     // 3. USet is internally really UnicodeSet, C is just a wrapper
1013     UnicodeSet *tailored = new UnicodeSet();
1014     UnicodeString pattern;
1015     UnicodeString empty;
1016     CanonicalIterator it(empty, *status);
1017 
1018 
1019     // The idea is to tokenize the rule set. For each non-reset token,
1020     // we add all the canonicaly equivalent FCD sequences
1021     ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, ucol_tok_getRulesFromBundle, NULL, status);
1022     while (ucol_tok_parseNextToken(&src, startOfRules, &parseError, status) != NULL) {
1023         startOfRules = FALSE;
1024         if(src.parsedToken.strength != UCOL_TOK_RESET) {
1025             const UChar *stuff = src.source+(src.parsedToken.charsOffset);
1026             it.setSource(UnicodeString(stuff, src.parsedToken.charsLen), *status);
1027             pattern = it.next();
1028             while(!pattern.isBogus()) {
1029                 if(Normalizer::quickCheck(pattern, UNORM_FCD, *status) != UNORM_NO) {
1030                     tailored->add(pattern);
1031                 }
1032                 pattern = it.next();
1033             }
1034         }
1035     }
1036     ucol_tok_closeTokenList(&src);
1037     return (USet *)tailored;
1038 }
1039 
1040 /*
1041  * Collation Reordering
1042  */
1043 
ucol_setReorderCodesFromParser(UCollator * coll,UColTokenParser * parser,UErrorCode * status)1044 void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status) {
1045     if (U_FAILURE(*status)) {
1046         return;
1047     }
1048 
1049     if (parser->reorderCodesLength == 0 || parser->reorderCodes == NULL) {
1050         return;
1051     }
1052 
1053     coll->reorderCodesLength = 0;
1054     if (coll->reorderCodes != NULL && coll->freeReorderCodesOnClose == TRUE) {
1055         uprv_free(coll->reorderCodes);
1056     }
1057 
1058     if (coll->defaultReorderCodes != NULL && coll->freeDefaultReorderCodesOnClose == TRUE) {
1059         uprv_free(coll->defaultReorderCodes);
1060     }
1061     coll->defaultReorderCodesLength = parser->reorderCodesLength;
1062     coll->defaultReorderCodes =  (int32_t*) uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t));
1063     if (coll->defaultReorderCodes == NULL) {
1064         *status = U_MEMORY_ALLOCATION_ERROR;
1065         return;
1066     }
1067     uprv_memcpy(coll->defaultReorderCodes, parser->reorderCodes, coll->defaultReorderCodesLength * sizeof(int32_t));
1068     coll->freeDefaultReorderCodesOnClose = TRUE;
1069 
1070     coll->reorderCodesLength = parser->reorderCodesLength;
1071     coll->reorderCodes = (int32_t*) uprv_malloc(coll->reorderCodesLength * sizeof(int32_t));
1072     if (coll->reorderCodes == NULL) {
1073         *status = U_MEMORY_ALLOCATION_ERROR;
1074         return;
1075     }
1076     uprv_memcpy(coll->reorderCodes, parser->reorderCodes, coll->reorderCodesLength * sizeof(int32_t));
1077     coll->freeReorderCodesOnClose = TRUE;
1078 }
1079 
1080 /*
1081  * Data is stored in the reorder code to lead byte table as:
1082  *  index count - unsigned short (2 bytes) - number of index entries
1083  *  data size - unsigned short (2 bytes) - number of unsigned short data elements
1084  *  index[index count] - array of 2 unsigned shorts (4 bytes each entry)
1085  *      - reorder code, offset
1086  *      - index is sorted by reorder code
1087  *      - if an offset has the high bit set then it is not an offset but a single data entry
1088  *        once the high bit is stripped off
1089  *  data[data size] - array of unsigned short (2 bytes each entry)
1090  *      - the data is an usigned short count followed by count number
1091  *        of lead bytes stored in an unsigned short
1092  */
1093 U_CFUNC int U_EXPORT2
ucol_getLeadBytesForReorderCode(const UCollator * uca,int reorderCode,uint16_t * returnLeadBytes,int returnCapacity)1094 ucol_getLeadBytesForReorderCode(const UCollator *uca, int reorderCode, uint16_t* returnLeadBytes, int returnCapacity) {
1095     uint16_t reorderCodeIndexLength = *((uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte));
1096     uint16_t* reorderCodeIndex = (uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte + 2 *sizeof(uint16_t));
1097 
1098     // reorder code index is 2 uint16_t's - reorder code + offset
1099     for (int i = 0; i < reorderCodeIndexLength; i++) {
1100         if (reorderCode == reorderCodeIndex[i*2]) {
1101             uint16_t dataOffset = reorderCodeIndex[(i*2) + 1];
1102             if ((dataOffset & 0x8000) == 0x8000) {
1103                 // offset isn't offset but instead is a single data element
1104                 if (returnCapacity >= 1) {
1105                     returnLeadBytes[0] = dataOffset & ~0x8000;
1106                     return 1;
1107                 }
1108                 return 0;
1109             }
1110             uint16_t* dataOffsetBase = (uint16_t*) ((uint8_t *)reorderCodeIndex + reorderCodeIndexLength * (2 * sizeof(uint16_t)));
1111             uint16_t leadByteCount = *(dataOffsetBase + dataOffset);
1112             leadByteCount = leadByteCount > returnCapacity ? returnCapacity : leadByteCount;
1113             uprv_memcpy(returnLeadBytes, dataOffsetBase + dataOffset + 1, leadByteCount * sizeof(uint16_t));
1114             return leadByteCount;
1115         }
1116     }
1117     return 0;
1118 }
1119 
1120 /*
1121  * Data is stored in the lead byte to reorder code table as:
1122  *  index count - unsigned short (2 bytes) - number of index entries
1123  *  data size - unsigned short (2 bytes) - number of unsigned short data elements
1124  *  index[index count] - array of unsigned short (2 bytes each entry)
1125  *      - index is sorted by lead byte
1126  *      - if an index has the high bit set then it is not an index but a single data entry
1127  *        once the high bit is stripped off
1128  *  data[data size] - array of unsigned short (2 bytes each entry)
1129  *      - the data is an usigned short count followed by count number of reorder codes
1130  */
1131 U_CFUNC int U_EXPORT2
ucol_getReorderCodesForLeadByte(const UCollator * uca,int leadByte,int16_t * returnReorderCodes,int returnCapacity)1132 ucol_getReorderCodesForLeadByte(const UCollator *uca, int leadByte, int16_t* returnReorderCodes, int returnCapacity) {
1133     uint16_t* leadByteTable = ((uint16_t*) ((uint8_t *)uca->image + uca->image->leadByteToScript));
1134     uint16_t leadByteIndexLength = *leadByteTable;
1135     if (leadByte >= leadByteIndexLength) {
1136         return 0;
1137     }
1138     uint16_t leadByteIndex = *(leadByteTable + (2 + leadByte));
1139 
1140     if ((leadByteIndex & 0x8000) == 0x8000) {
1141         // offset isn't offset but instead is a single data element
1142         if (returnCapacity >= 1) {
1143             returnReorderCodes[0] = leadByteIndex & ~0x8000;
1144             return 1;
1145         }
1146         return 0;
1147     }
1148     //uint16_t* dataOffsetBase = leadByteTable + (2 + leadByteIndexLength);
1149     uint16_t* reorderCodeData = leadByteTable + (2 + leadByteIndexLength) + leadByteIndex;
1150     uint16_t reorderCodeCount = *reorderCodeData > returnCapacity ? returnCapacity : *reorderCodeData;
1151     uprv_memcpy(returnReorderCodes, reorderCodeData + 1, reorderCodeCount * sizeof(uint16_t));
1152     return reorderCodeCount;
1153 }
1154 
1155 // used to mark ignorable reorder code slots
1156 static const int32_t UCOL_REORDER_CODE_IGNORE = UCOL_REORDER_CODE_LIMIT + 1;
1157 
1158 U_CFUNC void U_EXPORT2
ucol_buildPermutationTable(UCollator * coll,UErrorCode * status)1159 ucol_buildPermutationTable(UCollator *coll, UErrorCode *status) {
1160     uint16_t leadBytesSize = 256;
1161     uint16_t leadBytes[256];
1162     int32_t internalReorderCodesLength = coll->reorderCodesLength + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST);
1163     int32_t* internalReorderCodes;
1164 
1165     // The lowest byte that hasn't been assigned a mapping
1166     int toBottom = 0x03;
1167     // The highest byte that hasn't been assigned a mapping - don't include the special or trailing
1168     int toTop = 0xe4;
1169 
1170     // are we filling from the bottom?
1171     bool fromTheBottom = true;
1172     int32_t reorderCodesIndex = -1;
1173 
1174     // lead bytes that have alread been assigned to the permutation table
1175     bool newLeadByteUsed[256];
1176     // permutation table slots that have already been filled
1177     bool permutationSlotFilled[256];
1178 
1179     // nothing to do
1180     if(U_FAILURE(*status) || coll == NULL) {
1181         return;
1182     }
1183 
1184     // clear the reordering
1185     if (coll->reorderCodes == NULL || coll->reorderCodesLength == 0
1186             || (coll->reorderCodesLength == 1 && coll->reorderCodes[0] == UCOL_REORDER_CODE_NONE)) {
1187         if (coll->leadBytePermutationTable != NULL) {
1188             if (coll->freeLeadBytePermutationTableOnClose) {
1189                 uprv_free(coll->leadBytePermutationTable);
1190             }
1191             coll->leadBytePermutationTable = NULL;
1192             coll->reorderCodesLength = 0;
1193         }
1194         return;
1195     }
1196 
1197     // set reordering to the default reordering
1198     if (coll->reorderCodes[0] == UCOL_REORDER_CODE_DEFAULT) {
1199         if (coll->reorderCodesLength != 1) {
1200             *status = U_ILLEGAL_ARGUMENT_ERROR;
1201             return;
1202         }
1203         if (coll->freeReorderCodesOnClose == TRUE) {
1204             uprv_free(coll->reorderCodes);
1205         }
1206         coll->reorderCodes = NULL;
1207 
1208         if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1209             uprv_free(coll->leadBytePermutationTable);
1210         }
1211         coll->leadBytePermutationTable = NULL;
1212 
1213         if (coll->defaultReorderCodesLength == 0) {
1214             return;
1215         }
1216 
1217         coll->reorderCodes = (int32_t*)uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t));
1218         coll->freeReorderCodesOnClose = TRUE;
1219         if (coll->reorderCodes == NULL) {
1220             *status = U_MEMORY_ALLOCATION_ERROR;
1221             return;
1222         }
1223         coll->reorderCodesLength = coll->defaultReorderCodesLength;
1224         uprv_memcpy(coll->defaultReorderCodes, coll->reorderCodes, coll->reorderCodesLength * sizeof(int32_t));
1225     }
1226 
1227     if (coll->leadBytePermutationTable == NULL) {
1228         coll->leadBytePermutationTable = (uint8_t*)uprv_malloc(256*sizeof(uint8_t));
1229         coll->freeLeadBytePermutationTableOnClose = TRUE;
1230         if (coll->leadBytePermutationTable == NULL) {
1231             *status = U_MEMORY_ALLOCATION_ERROR;
1232             return;
1233         }
1234     }
1235 
1236     // prefill the reordering codes with the leading entries
1237     internalReorderCodes = (int32_t*)uprv_malloc(internalReorderCodesLength * sizeof(int32_t));
1238     if (internalReorderCodes == NULL) {
1239         *status = U_MEMORY_ALLOCATION_ERROR;
1240         if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1241             uprv_free(coll->leadBytePermutationTable);
1242         }
1243         coll->leadBytePermutationTable = NULL;
1244         return;
1245     }
1246 
1247     for (uint32_t codeIndex = 0; codeIndex < (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); codeIndex++) {
1248         internalReorderCodes[codeIndex] = UCOL_REORDER_CODE_FIRST + codeIndex;
1249     }
1250     for (int32_t codeIndex = 0; codeIndex < coll->reorderCodesLength; codeIndex++) {
1251         uint32_t reorderCodesCode = coll->reorderCodes[codeIndex];
1252         internalReorderCodes[codeIndex + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST)] = reorderCodesCode;
1253         if (reorderCodesCode >= UCOL_REORDER_CODE_FIRST && reorderCodesCode < UCOL_REORDER_CODE_LIMIT) {
1254             internalReorderCodes[reorderCodesCode - UCOL_REORDER_CODE_FIRST] = UCOL_REORDER_CODE_IGNORE;
1255         }
1256     }
1257 
1258     for (int i = 0; i < 256; i++) {
1259         if (i < toBottom || i > toTop) {
1260             permutationSlotFilled[i] = true;
1261             newLeadByteUsed[i] = true;
1262             coll->leadBytePermutationTable[i] = i;
1263         } else {
1264             permutationSlotFilled[i] = false;
1265             newLeadByteUsed[i] = false;
1266             coll->leadBytePermutationTable[i] = 0;
1267         }
1268     }
1269 
1270     /* Start from the front of the list and place each script we encounter at the
1271      * earliest possible locatation in the permutation table. If we encounter
1272      * UNKNOWN, start processing from the back, and place each script in the last
1273      * possible location. At each step, we also need to make sure that any scripts
1274      * that need to not be moved are copied to their same location in the final table.
1275      */
1276     for (int reorderCodesCount = 0; reorderCodesCount < internalReorderCodesLength; reorderCodesCount++) {
1277         reorderCodesIndex += fromTheBottom ? 1 : -1;
1278         int32_t next = internalReorderCodes[reorderCodesIndex];
1279         if (next == UCOL_REORDER_CODE_IGNORE) {
1280             continue;
1281         }
1282         if (next == USCRIPT_UNKNOWN) {
1283             if (fromTheBottom == false) {
1284                 // double turnaround
1285                 *status = U_ILLEGAL_ARGUMENT_ERROR;
1286                 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1287                     uprv_free(coll->leadBytePermutationTable);
1288                 }
1289                 coll->leadBytePermutationTable = NULL;
1290                 coll->reorderCodesLength = 0;
1291                 if (internalReorderCodes != NULL) {
1292                     uprv_free(internalReorderCodes);
1293                 }
1294                 return;
1295             }
1296             fromTheBottom = false;
1297             reorderCodesIndex = internalReorderCodesLength;
1298             continue;
1299         }
1300 
1301         uint16_t leadByteCount = ucol_getLeadBytesForReorderCode(coll->UCA, next, leadBytes, leadBytesSize);
1302         if (fromTheBottom) {
1303             for (int leadByteIndex = 0; leadByteIndex < leadByteCount; leadByteIndex++) {
1304                 // don't place a lead byte twice in the permutation table
1305                 if (permutationSlotFilled[leadBytes[leadByteIndex]]) {
1306                     // lead byte already used
1307                     *status = U_ILLEGAL_ARGUMENT_ERROR;
1308                     if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1309                         uprv_free(coll->leadBytePermutationTable);
1310                     }
1311                     coll->leadBytePermutationTable = NULL;
1312                     coll->reorderCodesLength = 0;
1313                     if (internalReorderCodes != NULL) {
1314                         uprv_free(internalReorderCodes);
1315                     }
1316                     return;
1317                 }
1318 
1319                 coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toBottom;
1320                 newLeadByteUsed[toBottom] = true;
1321                 permutationSlotFilled[leadBytes[leadByteIndex]] = true;
1322                 toBottom++;
1323             }
1324         } else {
1325             for (int leadByteIndex = leadByteCount - 1; leadByteIndex >= 0; leadByteIndex--) {
1326                 // don't place a lead byte twice in the permutation table
1327                 if (permutationSlotFilled[leadBytes[leadByteIndex]]) {
1328                     // lead byte already used
1329                     *status = U_ILLEGAL_ARGUMENT_ERROR;
1330                     if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1331                         uprv_free(coll->leadBytePermutationTable);
1332                     }
1333                     coll->leadBytePermutationTable = NULL;
1334                     coll->reorderCodesLength = 0;
1335                     if (internalReorderCodes != NULL) {
1336                         uprv_free(internalReorderCodes);
1337                     }
1338                     return;
1339                 }
1340 
1341                 coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toTop;
1342                 newLeadByteUsed[toTop] = true;
1343                 permutationSlotFilled[leadBytes[leadByteIndex]] = true;
1344                 toTop--;
1345             }
1346         }
1347     }
1348 
1349 #ifdef REORDER_DEBUG
1350     fprintf(stdout, "\n@@@@ Partial Script Reordering Table\n");
1351     for (int i = 0; i < 256; i++) {
1352         fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]);
1353     }
1354     fprintf(stdout, "\n@@@@ Lead Byte Used Table\n");
1355     for (int i = 0; i < 256; i++) {
1356         fprintf(stdout, "\t%02x = %02x\n", i, newLeadByteUsed[i]);
1357     }
1358     fprintf(stdout, "\n@@@@ Permutation Slot Filled Table\n");
1359     for (int i = 0; i < 256; i++) {
1360         fprintf(stdout, "\t%02x = %02x\n", i, permutationSlotFilled[i]);
1361     }
1362 #endif
1363 
1364     /* Copy everything that's left over */
1365     int reorderCode = 0;
1366     for (int i = 0; i < 256; i++) {
1367         if (!permutationSlotFilled[i]) {
1368             while (reorderCode < 256 && newLeadByteUsed[reorderCode]) {
1369                 reorderCode++;
1370             }
1371             coll->leadBytePermutationTable[i] = reorderCode;
1372             permutationSlotFilled[i] = true;
1373             newLeadByteUsed[reorderCode] = true;
1374         }
1375     }
1376 
1377 #ifdef REORDER_DEBUG
1378     fprintf(stdout, "\n@@@@ Script Reordering Table\n");
1379     for (int i = 0; i < 256; i++) {
1380         fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]);
1381     }
1382 #endif
1383 
1384     if (internalReorderCodes != NULL) {
1385         uprv_free(internalReorderCodes);
1386     }
1387 
1388     // force a regen of the latin one table since it is affected by the script reordering
1389     coll->latinOneRegenTable = TRUE;
1390     ucol_updateInternalState(coll, status);
1391 }
1392 
1393 #endif /* #if !UCONFIG_NO_COLLATION */
1394