Imported Upstream version 58.1
[platform/upstream/icu.git] / source / tools / gensprep / store.c
1 // Copyright (C) 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:   US-ASCII
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
176 setUnicodeVersion(const char *v) {
177     UVersionInfo version;
178     u_versionFromString(version, v);
179     uprv_memcpy(dataInfo.dataVersion, version, 4);
180 }
181
182 void
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
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 */
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 */
228 static int32_t U_CALLCONV hashEntry(const UHashTok parm) {
229     return  parm.integer;
230 }
231
232 /* Callback for comparing two entries */
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 
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
352 extern void setOptions(int32_t options){
353     indexes[_SPREP_OPTIONS] = options;
354 }
355 extern void
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 << 2;
416
417
418             /* make sure that the second bit is OFF */
419             if((trieWord & 0x02) != 0 ){
420                 fprintf(stderr,"The second bit in the trie word is not zero while storing a delta.\n");
421                 exit(U_INTERNAL_PROGRAM_ERROR);
422             }
423             /* make sure that the value of trieWord is less than the threshold */
424             if(trieWord < _SPREP_TYPE_THRESHOLD){   
425                 /* now set the value in the trie */
426                 if(!utrie_set32(sprepTrie,codepoint,trieWord)){
427                     fprintf(stderr,"Could not set the value for code point.\n");
428                     exit(U_ILLEGAL_ARGUMENT_ERROR);   
429                 }
430                 /* value is set so just return */
431                 return;
432             }
433         }
434         /* 
435          * if the delta is not in the given range or if the trieWord is larger than the threshold
436          * just fall through for storing the mapping in the mapping table
437          */
438     }
439
440     map = (UChar*) uprv_calloc(adjustedLen + 1, U_SIZEOF_UCHAR);
441     
442     for (i=0, j=0; i<length; i++) {
443         U16_APPEND_UNSAFE(map, j, mapping[i]);
444     }
445     
446     value = (ValueStruct*) uprv_malloc(sizeof(ValueStruct));
447     value->mapping = map;
448     value->type    = type;
449     value->length  = adjustedLen;
450     if(value->length > _SPREP_MAX_INDEX_TOP_LENGTH){
451         mappingDataCapacity++;
452     }
453     if(maxLength < value->length){
454         maxLength = value->length;
455     }
456     uhash_iput(hashTable,codepoint,value,status);
457     mappingDataCapacity += adjustedLen;
458
459     if(U_FAILURE(*status)){
460         fprintf(stderr, "Failed to put entries into the hastable. Error: %s\n", u_errorName(*status));
461         exit(*status);
462     }
463 }
464
465
466 extern void
467 storeRange(uint32_t start, uint32_t end, UStringPrepType type,UErrorCode* status){
468     uint16_t trieWord = 0;
469
470     if((int)(_SPREP_TYPE_THRESHOLD + type) > 0xFFFF){
471         fprintf(stderr,"trieWord cannot contain value greater than 0xFFFF.\n");
472         exit(U_ILLEGAL_CHAR_FOUND);
473     }
474     trieWord = (_SPREP_TYPE_THRESHOLD + type); /* the top 4 bits contain the value */
475     if(start == end){
476         uint32_t savedTrieWord = utrie_get32(sprepTrie, start, NULL);
477         if(savedTrieWord>0){
478             if(savedTrieWord < _SPREP_TYPE_THRESHOLD && type == USPREP_PROHIBITED){
479                 /* 
480                  * A mapping is stored in the trie word 
481                  * and the only other possible type that a 
482                  * code point can have is USPREP_PROHIBITED
483                  *
484                  */
485
486                 /* turn on the 0th bit in the savedTrieWord */
487                 savedTrieWord += 0x01;
488
489                 /* the downcast is safe since we only save 16 bit values */
490                 trieWord = (uint16_t)savedTrieWord;
491
492                 /* make sure that the value of trieWord is less than the threshold */
493                 if(trieWord < _SPREP_TYPE_THRESHOLD){   
494                     /* now set the value in the trie */
495                     if(!utrie_set32(sprepTrie,start,trieWord)){
496                         fprintf(stderr,"Could not set the value for code point.\n");
497                         exit(U_ILLEGAL_ARGUMENT_ERROR);   
498                     }
499                     /* value is set so just return */
500                     return;
501                 }else{
502                     fprintf(stderr,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD);
503                     exit(U_ILLEGAL_CHAR_FOUND);
504                 }
505  
506             }else if(savedTrieWord != trieWord){
507                 fprintf(stderr,"Value for codepoint \\U%08X already set!.\n", (int)start);
508                 exit(U_ILLEGAL_ARGUMENT_ERROR);
509             }
510             /* if savedTrieWord == trieWord .. fall through and set the value */
511         }
512         if(!utrie_set32(sprepTrie,start,trieWord)){
513             fprintf(stderr,"Could not set the value for code point \\U%08X.\n", (int)start);
514             exit(U_ILLEGAL_ARGUMENT_ERROR);   
515         }
516     }else{
517         if(!utrie_setRange32(sprepTrie, start, end+1, trieWord, FALSE)){
518             fprintf(stderr,"Value for certain codepoint already set.\n");
519             exit(U_ILLEGAL_CHAR_FOUND);   
520         }
521     }
522
523 }
524
525 /* folding value: just store the offset (16 bits) if there is any non-0 entry */
526 static uint32_t U_CALLCONV
527 getFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset) {
528     uint32_t value;
529     UChar32 limit=0;
530     UBool inBlockZero;
531
532     limit=start+0x400;
533     while(start<limit) {
534         value=utrie_get32(trie, start, &inBlockZero);
535         if(inBlockZero) {
536             start+=UTRIE_DATA_BLOCK_LENGTH;
537         } else if(value!=0) {
538             return (uint32_t)offset;
539         } else {
540             ++start;
541         }
542     }
543     return 0;
544
545 }
546
547 #endif /* #if !UCONFIG_NO_IDNA */
548
549 extern void
550 generateData(const char *dataDir, const char* bundleName) {
551     static uint8_t sprepTrieBlock[100000];
552
553     UNewDataMemory *pData;
554     UErrorCode errorCode=U_ZERO_ERROR;
555     int32_t size, dataLength;
556     char* fileName = (char*) uprv_malloc(uprv_strlen(bundleName) +100);
557
558 #if UCONFIG_NO_IDNA
559
560     size=0;
561
562 #else
563
564     int32_t sprepTrieSize;
565
566     /* sort and add mapping data */
567     storeMappingData();
568     
569     sprepTrieSize=utrie_serialize(sprepTrie, sprepTrieBlock, sizeof(sprepTrieBlock), getFoldedValue, TRUE, &errorCode);
570     if(U_FAILURE(errorCode)) {
571         fprintf(stderr, "error: utrie_serialize(sprep trie) failed, %s\n", u_errorName(errorCode));
572         exit(errorCode);
573     }
574     
575     size = sprepTrieSize + mappingDataCapacity*U_SIZEOF_UCHAR + sizeof(indexes);
576     if(beVerbose) {
577         printf("size of sprep trie              %5u bytes\n", (int)sprepTrieSize);
578         printf("size of " U_ICUDATA_NAME "_%s." DATA_TYPE " contents: %ld bytes\n", bundleName,(long)size);
579         printf("size of mapping data array %5u bytes\n",(int)mappingDataCapacity * U_SIZEOF_UCHAR);
580         printf("Number of code units in mappingData (currentIndex) are: %i \n", currentIndex);
581         printf("Maximum length of the mapping string is : %i \n", (int)maxLength);
582     }
583
584 #endif
585
586     fileName[0]=0;
587     uprv_strcat(fileName,bundleName);
588     /* write the data */
589     pData=udata_create(dataDir, DATA_TYPE, fileName, &dataInfo,
590                        haveCopyright ? U_COPYRIGHT_STRING : NULL, &errorCode);
591     if(U_FAILURE(errorCode)) {
592         fprintf(stderr, "gensprep: unable to create the output file, error %d\n", errorCode);
593         exit(errorCode);
594     }
595
596 #if !UCONFIG_NO_IDNA
597
598     indexes[_SPREP_INDEX_TRIE_SIZE]=sprepTrieSize;
599     indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]=mappingDataCapacity*U_SIZEOF_UCHAR;
600     
601     udata_writeBlock(pData, indexes, sizeof(indexes));
602     udata_writeBlock(pData, sprepTrieBlock, sprepTrieSize);
603     udata_writeBlock(pData, mappingData, indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]);
604     
605
606 #endif
607
608     /* finish up */
609     dataLength=udata_finish(pData, &errorCode);
610     if(U_FAILURE(errorCode)) {
611         fprintf(stderr, "gensprep: error %d writing the output file\n", errorCode);
612         exit(errorCode);
613     }
614
615     if(dataLength!=size) {
616         fprintf(stderr, "gensprep error: data length %ld != calculated size %ld\n",
617             (long)dataLength, (long)size);
618         exit(U_INTERNAL_PROGRAM_ERROR);
619     }
620
621 #if !UCONFIG_NO_IDNA
622     /* done with writing the data .. close the hashtable */
623     if (hashTable != NULL) {
624         uhash_close(hashTable);
625     }
626 #endif
627
628     uprv_free(fileName);
629 }
630
631 #if !UCONFIG_NO_IDNA
632
633 extern void
634 cleanUpData(void) {
635     uprv_free(mappingData);
636     utrie_close(sprepTrie);
637     uprv_free(sprepTrie);
638 }
639
640 #endif /* #if !UCONFIG_NO_IDNA */
641
642 /*
643  * Hey, Emacs, please set the following:
644  *
645  * Local Variables:
646  * indent-tabs-mode: nil
647  * End:
648  *
649  */