1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 1999-2014, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  store.c
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2003-02-06
16 *   created by: Ram Viswanadha
17 *
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "unicode/utypes.h"
23 #include "cmemory.h"
24 #include "cstring.h"
25 #include "filestrm.h"
26 #include "unicode/udata.h"
27 #include "unicode/utf16.h"
28 #include "utrie.h"
29 #include "unewdata.h"
30 #include "gensprep.h"
31 #include "uhash.h"
32 
33 
34 #define DO_DEBUG_OUT 0
35 
36 
37 /*
38  * StringPrep profile file format ------------------------------------
39  *
40  * The file format prepared and written here contains a 16-bit trie and a mapping table.
41  *
42  * Before the data contents described below, there are the headers required by
43  * the udata API for loading ICU data. Especially, a UDataInfo structure
44  * precedes the actual data. It contains platform properties values and the
45  * file format version.
46  *
47  * The following is a description of format version 2.
48  *
49  * Data contents:
50  *
51  * The contents is a parsed, binary form of RFC3454 and possibly
52  * NormalizationCorrections.txt depending on the options specified on the profile.
53  *
54  * Any Unicode code point from 0 to 0x10ffff can be looked up to get
55  * the trie-word, if any, for that code point. This means that the input
56  * to the lookup are 21-bit unsigned integers, with not all of the
57  * 21-bit range used.
58  *
59  * *.spp files customarily begin with a UDataInfo structure, see udata.h and .c.
60  * After that there are the following structures:
61  *
62  * int32_t indexes[_SPREP_INDEX_TOP];           -- _SPREP_INDEX_TOP=16, see enum in sprpimpl.h file
63  *
64  * UTrie stringPrepTrie;                        -- size in bytes=indexes[_SPREP_INDEX_TRIE_SIZE]
65  *
66  * uint16_t mappingTable[];                     -- Contains the sequecence of code units that the code point maps to
67  *                                                 size in bytes = indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]
68  *
69  * The indexes array contains the following values:
70  *  indexes[_SPREP_INDEX_TRIE_SIZE]                  -- The size of the StringPrep trie in bytes
71  *  indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]          -- The size of the mappingTable in bytes
72  *  indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION]  -- The index of Unicode version of last entry in NormalizationCorrections.txt
73  *  indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START]    -- The starting index of 1 UChar  mapping index in the mapping table
74  *  indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]   -- The starting index of 2 UChars mapping index in the mapping table
75  *  indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] -- The starting index of 3 UChars mapping index in the mapping table
76  *  indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]  -- The starting index of 4 UChars mapping index in the mapping table
77  *  indexes[_SPREP_OPTIONS]                          -- Bit set of options to turn on in the profile, e.g: USPREP_NORMALIZATION_ON, USPREP_CHECK_BIDI_ON
78  *
79  *
80  * StringPrep Trie :
81  *
82  * The StringPrep tries is a 16-bit trie that contains data for the profile.
83  * Each code point is associated with a value (trie-word) in the trie.
84  *
85  * - structure of data words from the trie
86  *
87  *  i)  A value greater than or equal to _SPREP_TYPE_THRESHOLD (0xFFF0)
88  *      represents the type associated with the code point
89  *      if(trieWord >= _SPREP_TYPE_THRESHOLD){
90  *          type = trieWord - 0xFFF0;
91  *      }
92  *      The type can be :
93  *             USPREP_UNASSIGNED
94  *             USPREP_PROHIBITED
95  *             USPREP_DELETE
96  *
97  *  ii) A value less than _SPREP_TYPE_THRESHOLD means the type is USPREP_MAP and
98  *      contains distribution described below
99  *
100  *      0       -  ON : The code point is prohibited (USPREP_PROHIBITED). This is to allow for codepoint that are both prohibited and mapped.
101  *      1       -  ON : The value in the next 14 bits is an index into the mapping table
102  *                 OFF: The value in the next 14 bits is an delta value from the code point
103  *      2..15   -  Contains data as described by bit 1. If all bits are set
104  *                 (value = _SPREP_MAX_INDEX_VALUE) then the type is USPREP_DELETE
105  *
106  *
107  * Mapping Table:
108  * The data in mapping table is sorted according to the length of the mapping sequence.
109  * If the type of the code point is USPREP_MAP and value in trie word is an index, the index
110  * is compared with start indexes of sequence length start to figure out the length according to
111  * the following algorithm:
112  *
113  *              if(       index >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] &&
114  *                        index < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){
115  *                   length = 1;
116  *               }else if(index >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] &&
117  *                        index < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){
118  *                   length = 2;
119  *               }else if(index >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] &&
120  *                        index < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){
121  *                   length = 3;
122  *               }else{
123  *                   // The first position in the mapping table contains the length
124  *                   // of the sequence
125  *                   length = mappingTable[index++];
126  *
127  *               }
128  *
129  */
130 
131 /* file data ---------------------------------------------------------------- */
132 /* indexes[] value names */
133 
134 #if UCONFIG_NO_IDNA
135 
136 /* dummy UDataInfo cf. udata.h */
137 static UDataInfo dataInfo = {
138     sizeof(UDataInfo),
139     0,
140 
141     U_IS_BIG_ENDIAN,
142     U_CHARSET_FAMILY,
143     U_SIZEOF_UCHAR,
144     0,
145 
146     { 0, 0, 0, 0 },                 /* dummy dataFormat */
147     { 0, 0, 0, 0 },                 /* dummy formatVersion */
148     { 0, 0, 0, 0 }                  /* dummy dataVersion */
149 };
150 
151 #else
152 
153 static int32_t indexes[_SPREP_INDEX_TOP]={ 0 };
154 
155 static uint16_t* mappingData= NULL;
156 static int32_t mappingDataCapacity = 0; /* we skip the first index in mapping data */
157 static int16_t currentIndex = 0; /* the current index into the data trie */
158 static int32_t maxLength = 0;  /* maximum length of mapping string */
159 
160 
161 /* UDataInfo cf. udata.h */
162 static UDataInfo dataInfo={
163     sizeof(UDataInfo),
164     0,
165 
166     U_IS_BIG_ENDIAN,
167     U_CHARSET_FAMILY,
168     U_SIZEOF_UCHAR,
169     0,
170 
171     { 0x53, 0x50, 0x52, 0x50 },                 /* dataFormat="SPRP" */
172     { 3, 2, UTRIE_SHIFT, UTRIE_INDEX_SHIFT },   /* formatVersion */
173     { 3, 2, 0, 0 }                              /* dataVersion (Unicode version) */
174 };
175 void
setUnicodeVersion(const char * v)176 setUnicodeVersion(const char *v) {
177     UVersionInfo version;
178     u_versionFromString(version, v);
179     uprv_memcpy(dataInfo.dataVersion, version, 4);
180 }
181 
182 void
setUnicodeVersionNC(UVersionInfo version)183 setUnicodeVersionNC(UVersionInfo version){
184     uint32_t univer = version[0] << 24;
185     univer += version[1] << 16;
186     univer += version[2] << 8;
187     univer += version[3];
188     indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION] = univer;
189 }
190 static UNewTrie *sprepTrie;
191 
192 #define MAX_DATA_LENGTH 11500
193 
194 
195 #define SPREP_DELTA_RANGE_POSITIVE_LIMIT              8191
196 #define SPREP_DELTA_RANGE_NEGATIVE_LIMIT              -8192
197 
198 
199 extern void
init()200 init() {
201 
202     sprepTrie = (UNewTrie *)uprv_calloc(1, sizeof(UNewTrie));
203 
204     /* initialize the two tries */
205     if(NULL==utrie_open(sprepTrie, NULL, MAX_DATA_LENGTH, 0, 0, FALSE)) {
206         fprintf(stderr, "error: failed to initialize tries\n");
207         exit(U_MEMORY_ALLOCATION_ERROR);
208     }
209 }
210 
211 static UHashtable* hashTable = NULL;
212 
213 
214 typedef struct ValueStruct {
215     UChar* mapping;
216     int16_t length;
217     UStringPrepType type;
218 } ValueStruct;
219 
220 /* Callback for deleting the value from the hashtable */
valueDeleter(void * obj)221 static void U_CALLCONV valueDeleter(void* obj){
222     ValueStruct* value = (ValueStruct*) obj;
223     uprv_free(value->mapping);
224     uprv_free(value);
225 }
226 
227 /* Callback for hashing the entry */
hashEntry(const UHashTok parm)228 static int32_t U_CALLCONV hashEntry(const UHashTok parm) {
229     return  parm.integer;
230 }
231 
232 /* Callback for comparing two entries */
compareEntries(const UHashTok p1,const UHashTok p2)233 static UBool U_CALLCONV compareEntries(const UHashTok p1, const UHashTok p2) {
234     return (UBool)(p1.integer != p2.integer);
235 }
236 
237 
238 static void
storeMappingData()239 storeMappingData(){
240 
241     int32_t pos = UHASH_FIRST;
242     const UHashElement* element = NULL;
243     ValueStruct* value  = NULL;
244     int32_t codepoint = 0;
245     int32_t elementCount = 0;
246     int32_t writtenElementCount = 0;
247     int32_t mappingLength = 1; /* minimum mapping length */
248     int32_t oldMappingLength = 0;
249     uint16_t trieWord =0;
250     int32_t limitIndex = 0;
251 
252     if (hashTable == NULL) {
253         return;
254     }
255     elementCount = uhash_count(hashTable);
256 
257 	/*initialize the mapping data */
258     mappingData = (uint16_t*) uprv_calloc(mappingDataCapacity, U_SIZEOF_UCHAR);
259 
260     while(writtenElementCount < elementCount){
261 
262         while( (element = uhash_nextElement(hashTable, &pos))!=NULL){
263 
264             codepoint = element->key.integer;
265             value = (ValueStruct*)element->value.pointer;
266 
267             /* store the start of indexes */
268             if(oldMappingLength != mappingLength){
269                 /* Assume that index[] is used according to the enums defined */
270                 if(oldMappingLength <=_SPREP_MAX_INDEX_TOP_LENGTH){
271                     indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION+mappingLength] = currentIndex;
272                 }
273                 if(oldMappingLength <= _SPREP_MAX_INDEX_TOP_LENGTH &&
274                    mappingLength == _SPREP_MAX_INDEX_TOP_LENGTH +1){
275 
276                     limitIndex = currentIndex;
277 
278                 }
279                 oldMappingLength = mappingLength;
280             }
281 
282             if(value->length == mappingLength){
283                 uint32_t savedTrieWord = 0;
284                 trieWord = currentIndex << 2;
285                 /* turn on the 2nd bit to signal that the following bits contain an index */
286                 trieWord += 0x02;
287 
288                 if(trieWord > _SPREP_TYPE_THRESHOLD){
289                     fprintf(stderr,"trieWord cannot contain value greater than 0x%04X.\n",_SPREP_TYPE_THRESHOLD);
290                     exit(U_ILLEGAL_CHAR_FOUND);
291                 }
292                 /* figure out if the code point has type already stored */
293                 savedTrieWord= utrie_get32(sprepTrie,codepoint,NULL);
294                 if(savedTrieWord!=0){
295                     if((savedTrieWord- _SPREP_TYPE_THRESHOLD) == USPREP_PROHIBITED){
296                         /* turn on the first bit in trie word */
297                         trieWord += 0x01;
298                     }else{
299                         /*
300                          * the codepoint has value something other than prohibited
301                          * and a mapping .. error!
302                          */
303                         fprintf(stderr,"Type for codepoint \\U%08X already set!.\n", (int)codepoint);
304                         exit(U_ILLEGAL_ARGUMENT_ERROR);
305                     }
306                 }
307 
308                 /* now set the value in the trie */
309                 if(!utrie_set32(sprepTrie,codepoint,trieWord)){
310                     fprintf(stderr,"Could not set the value for code point.\n");
311                     exit(U_ILLEGAL_ARGUMENT_ERROR);
312                 }
313 
314                 /* written the trie word for the codepoint... increment the count*/
315                 writtenElementCount++;
316 
317                 /* sanity check are we exceeding the max number allowed */
318                 if(currentIndex+value->length+1 > _SPREP_MAX_INDEX_VALUE){
319                     fprintf(stderr, "Too many entries in the mapping table %i. Maximum allowed is %i\n",
320                         currentIndex+value->length, _SPREP_MAX_INDEX_VALUE);
321                     exit(U_INDEX_OUTOFBOUNDS_ERROR);
322                 }
323 
324                 /* copy the mapping data */
325                 /* write the length */
326                 if(mappingLength > _SPREP_MAX_INDEX_TOP_LENGTH ){
327                      /* the cast here is safe since we donot expect the length to be > 65535 */
328                      mappingData[currentIndex++] = (uint16_t) mappingLength;
329                 }
330                 /* copy the contents to mappindData array */
331                 u_memmove(mappingData+currentIndex, value->mapping, value->length);
332                 currentIndex += value->length;
333                 if (currentIndex > mappingDataCapacity) {
334                     /* If this happens there is a bug in the computation of the mapping data size in storeMapping() */
335                     fprintf(stderr, "gensprep, fatal error at %s, %d.  Aborting.\n", __FILE__, __LINE__);
336                     exit(U_INTERNAL_PROGRAM_ERROR);
337                 }
338             }
339         }
340         mappingLength++;
341         pos = -1;
342     }
343     /* set the last length for range check */
344     if(mappingLength <= _SPREP_MAX_INDEX_TOP_LENGTH){
345         indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION+mappingLength] = currentIndex+1;
346     }else{
347         indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START] = limitIndex;
348     }
349 
350 }
351 
setOptions(int32_t options)352 extern void setOptions(int32_t options){
353     indexes[_SPREP_OPTIONS] = options;
354 }
355 extern void
storeMapping(uint32_t codepoint,uint32_t * mapping,int32_t length,UStringPrepType type,UErrorCode * status)356 storeMapping(uint32_t codepoint, uint32_t* mapping,int32_t length,
357              UStringPrepType type, UErrorCode* status){
358 
359 
360     UChar* map = NULL;
361     int16_t adjustedLen=0, i, j;
362     uint16_t trieWord = 0;
363     ValueStruct *value = NULL;
364     uint32_t savedTrieWord = 0;
365 
366     /* initialize the hashtable */
367     if(hashTable==NULL){
368         hashTable = uhash_open(hashEntry, compareEntries, NULL, status);
369         uhash_setValueDeleter(hashTable, valueDeleter);
370     }
371 
372     /* figure out if the code point has type already stored */
373     savedTrieWord= utrie_get32(sprepTrie,codepoint,NULL);
374     if(savedTrieWord!=0){
375         if((savedTrieWord- _SPREP_TYPE_THRESHOLD) == USPREP_PROHIBITED){
376             /* turn on the first bit in trie word */
377             trieWord += 0x01;
378         }else{
379             /*
380              * the codepoint has value something other than prohibited
381              * and a mapping .. error!
382              */
383             fprintf(stderr,"Type for codepoint \\U%08X already set!.\n", (int)codepoint);
384             exit(U_ILLEGAL_ARGUMENT_ERROR);
385         }
386     }
387 
388     /* figure out the real length */
389     for(i=0; i<length; i++){
390         adjustedLen += U16_LENGTH(mapping[i]);
391     }
392 
393     if(adjustedLen == 0){
394         trieWord = (uint16_t)(_SPREP_MAX_INDEX_VALUE << 2);
395         /* make sure that the value of trieWord is less than the threshold */
396         if(trieWord < _SPREP_TYPE_THRESHOLD){
397             /* now set the value in the trie */
398             if(!utrie_set32(sprepTrie,codepoint,trieWord)){
399                 fprintf(stderr,"Could not set the value for code point.\n");
400                 exit(U_ILLEGAL_ARGUMENT_ERROR);
401             }
402             /* value is set so just return */
403             return;
404         }else{
405             fprintf(stderr,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD);
406             exit(U_ILLEGAL_CHAR_FOUND);
407         }
408     }
409 
410     if(adjustedLen == 1){
411         /* calculate the delta */
412         int16_t delta = (int16_t)((int32_t)codepoint - (int16_t) mapping[0]);
413         if(delta >= SPREP_DELTA_RANGE_NEGATIVE_LIMIT && delta <= SPREP_DELTA_RANGE_POSITIVE_LIMIT){
414 
415             trieWord = delta;
416             trieWord <<= 2;
417 
418 
419             /* make sure that the second bit is OFF */
420             if((trieWord & 0x02) != 0 ){
421                 fprintf(stderr,"The second bit in the trie word is not zero while storing a delta.\n");
422                 exit(U_INTERNAL_PROGRAM_ERROR);
423             }
424             /* make sure that the value of trieWord is less than the threshold */
425             if(trieWord < _SPREP_TYPE_THRESHOLD){
426                 /* now set the value in the trie */
427                 if(!utrie_set32(sprepTrie,codepoint,trieWord)){
428                     fprintf(stderr,"Could not set the value for code point.\n");
429                     exit(U_ILLEGAL_ARGUMENT_ERROR);
430                 }
431                 /* value is set so just return */
432                 return;
433             }
434         }
435         /*
436          * if the delta is not in the given range or if the trieWord is larger than the threshold
437          * just fall through for storing the mapping in the mapping table
438          */
439     }
440 
441     map = (UChar*) uprv_calloc(adjustedLen + 1, U_SIZEOF_UCHAR);
442 
443     for (i=0, j=0; i<length; i++) {
444         U16_APPEND_UNSAFE(map, j, mapping[i]);
445     }
446 
447     value = (ValueStruct*) uprv_malloc(sizeof(ValueStruct));
448     value->mapping = map;
449     value->type    = type;
450     value->length  = adjustedLen;
451     if(value->length > _SPREP_MAX_INDEX_TOP_LENGTH){
452         mappingDataCapacity++;
453     }
454     if(maxLength < value->length){
455         maxLength = value->length;
456     }
457     uhash_iput(hashTable,codepoint,value,status);
458     mappingDataCapacity += adjustedLen;
459 
460     if(U_FAILURE(*status)){
461         fprintf(stderr, "Failed to put entries into the hastable. Error: %s\n", u_errorName(*status));
462         exit(*status);
463     }
464 }
465 
466 
467 extern void
storeRange(uint32_t start,uint32_t end,UStringPrepType type,UErrorCode * status)468 storeRange(uint32_t start, uint32_t end, UStringPrepType type, UErrorCode* status){
469     (void)status; // suppress compiler warnings about unused variable
470     uint16_t trieWord = 0;
471 
472     if((int)(_SPREP_TYPE_THRESHOLD + type) > 0xFFFF){
473         fprintf(stderr,"trieWord cannot contain value greater than 0xFFFF.\n");
474         exit(U_ILLEGAL_CHAR_FOUND);
475     }
476     trieWord = (_SPREP_TYPE_THRESHOLD + type); /* the top 4 bits contain the value */
477     if(start == end){
478         uint32_t savedTrieWord = utrie_get32(sprepTrie, start, NULL);
479         if(savedTrieWord>0){
480             if(savedTrieWord < _SPREP_TYPE_THRESHOLD && type == USPREP_PROHIBITED){
481                 /*
482                  * A mapping is stored in the trie word
483                  * and the only other possible type that a
484                  * code point can have is USPREP_PROHIBITED
485                  *
486                  */
487 
488                 /* turn on the 0th bit in the savedTrieWord */
489                 savedTrieWord += 0x01;
490 
491                 /* the downcast is safe since we only save 16 bit values */
492                 trieWord = (uint16_t)savedTrieWord;
493 
494                 /* make sure that the value of trieWord is less than the threshold */
495                 if(trieWord < _SPREP_TYPE_THRESHOLD){
496                     /* now set the value in the trie */
497                     if(!utrie_set32(sprepTrie,start,trieWord)){
498                         fprintf(stderr,"Could not set the value for code point.\n");
499                         exit(U_ILLEGAL_ARGUMENT_ERROR);
500                     }
501                     /* value is set so just return */
502                     return;
503                 }else{
504                     fprintf(stderr,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD);
505                     exit(U_ILLEGAL_CHAR_FOUND);
506                 }
507 
508             }else if(savedTrieWord != trieWord){
509                 fprintf(stderr,"Value for codepoint \\U%08X already set!.\n", (int)start);
510                 exit(U_ILLEGAL_ARGUMENT_ERROR);
511             }
512             /* if savedTrieWord == trieWord .. fall through and set the value */
513         }
514         if(!utrie_set32(sprepTrie,start,trieWord)){
515             fprintf(stderr,"Could not set the value for code point \\U%08X.\n", (int)start);
516             exit(U_ILLEGAL_ARGUMENT_ERROR);
517         }
518     }else{
519         if(!utrie_setRange32(sprepTrie, start, end+1, trieWord, FALSE)){
520             fprintf(stderr,"Value for certain codepoint already set.\n");
521             exit(U_ILLEGAL_CHAR_FOUND);
522         }
523     }
524 
525 }
526 
527 /* folding value: just store the offset (16 bits) if there is any non-0 entry */
528 static uint32_t U_CALLCONV
getFoldedValue(UNewTrie * trie,UChar32 start,int32_t offset)529 getFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset) {
530     uint32_t value;
531     UChar32 limit=0;
532     UBool inBlockZero;
533 
534     limit=start+0x400;
535     while(start<limit) {
536         value=utrie_get32(trie, start, &inBlockZero);
537         if(inBlockZero) {
538             start+=UTRIE_DATA_BLOCK_LENGTH;
539         } else if(value!=0) {
540             return (uint32_t)offset;
541         } else {
542             ++start;
543         }
544     }
545     return 0;
546 
547 }
548 
549 #endif /* #if !UCONFIG_NO_IDNA */
550 
551 extern void
generateData(const char * dataDir,const char * bundleName)552 generateData(const char *dataDir, const char* bundleName) {
553     static uint8_t sprepTrieBlock[100000];
554 
555     UNewDataMemory *pData;
556     UErrorCode errorCode=U_ZERO_ERROR;
557     int32_t size, dataLength;
558     char* fileName = (char*) uprv_malloc(uprv_strlen(bundleName) +100);
559 
560 #if UCONFIG_NO_IDNA
561 
562     size=0;
563 
564 #else
565 
566     int32_t sprepTrieSize;
567 
568     /* sort and add mapping data */
569     storeMappingData();
570 
571     sprepTrieSize=utrie_serialize(sprepTrie, sprepTrieBlock, sizeof(sprepTrieBlock), getFoldedValue, TRUE, &errorCode);
572     if(U_FAILURE(errorCode)) {
573         fprintf(stderr, "error: utrie_serialize(sprep trie) failed, %s\n", u_errorName(errorCode));
574         exit(errorCode);
575     }
576 
577     size = sprepTrieSize + mappingDataCapacity*U_SIZEOF_UCHAR + sizeof(indexes);
578     if(beVerbose) {
579         printf("size of sprep trie              %5u bytes\n", (int)sprepTrieSize);
580         printf("size of " U_ICUDATA_NAME "_%s." DATA_TYPE " contents: %ld bytes\n", bundleName,(long)size);
581         printf("size of mapping data array %5u bytes\n",(int)mappingDataCapacity * U_SIZEOF_UCHAR);
582         printf("Number of code units in mappingData (currentIndex) are: %i \n", currentIndex);
583         printf("Maximum length of the mapping string is : %i \n", (int)maxLength);
584     }
585 
586 #endif
587 
588     fileName[0]=0;
589     uprv_strcat(fileName,bundleName);
590     /* write the data */
591     pData=udata_create(dataDir, DATA_TYPE, fileName, &dataInfo,
592                        haveCopyright ? U_COPYRIGHT_STRING : NULL, &errorCode);
593     if(U_FAILURE(errorCode)) {
594         fprintf(stderr, "gensprep: unable to create the output file, error %d\n", errorCode);
595         exit(errorCode);
596     }
597 
598 #if !UCONFIG_NO_IDNA
599 
600     indexes[_SPREP_INDEX_TRIE_SIZE]=sprepTrieSize;
601     indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]=mappingDataCapacity*U_SIZEOF_UCHAR;
602 
603     udata_writeBlock(pData, indexes, sizeof(indexes));
604     udata_writeBlock(pData, sprepTrieBlock, sprepTrieSize);
605     udata_writeBlock(pData, mappingData, indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]);
606 
607 
608 #endif
609 
610     /* finish up */
611     dataLength=udata_finish(pData, &errorCode);
612     if(U_FAILURE(errorCode)) {
613         fprintf(stderr, "gensprep: error %d writing the output file\n", errorCode);
614         exit(errorCode);
615     }
616 
617     if(dataLength!=size) {
618         fprintf(stderr, "gensprep error: data length %ld != calculated size %ld\n",
619             (long)dataLength, (long)size);
620         exit(U_INTERNAL_PROGRAM_ERROR);
621     }
622 
623 #if !UCONFIG_NO_IDNA
624     /* done with writing the data .. close the hashtable */
625     if (hashTable != NULL) {
626         uhash_close(hashTable);
627     }
628 #endif
629 
630     uprv_free(fileName);
631 }
632 
633 #if !UCONFIG_NO_IDNA
634 
635 extern void
cleanUpData(void)636 cleanUpData(void) {
637     uprv_free(mappingData);
638     utrie_close(sprepTrie);
639     uprv_free(sprepTrie);
640 }
641 
642 #endif /* #if !UCONFIG_NO_IDNA */
643 
644 /*
645  * Hey, Emacs, please set the following:
646  *
647  * Local Variables:
648  * indent-tabs-mode: nil
649  * End:
650  *
651  */
652