]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
374ca955 A |
3 | /* |
4 | ******************************************************************************* | |
5 | * | |
b331163b | 6 | * Copyright (C) 1999-2014, International Business Machines |
374ca955 A |
7 | * Corporation and others. All Rights Reserved. |
8 | * | |
9 | ******************************************************************************* | |
10 | * file name: store.c | |
f3c0d7a5 | 11 | * encoding: UTF-8 |
374ca955 A |
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" | |
4388f060 | 27 | #include "unicode/utf16.h" |
374ca955 A |
28 | #include "utrie.h" |
29 | #include "unewdata.h" | |
30 | #include "gensprep.h" | |
31 | #include "uhash.h" | |
32 | ||
33 | ||
374ca955 A |
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 | ||
4388f060 | 202 | sprepTrie = (UNewTrie *)uprv_calloc(1, sizeof(UNewTrie)); |
374ca955 A |
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 | ||
b331163b | 241 | int32_t pos = UHASH_FIRST; |
374ca955 A |
242 | const UHashElement* element = NULL; |
243 | ValueStruct* value = NULL; | |
244 | int32_t codepoint = 0; | |
729e4ab9 | 245 | int32_t elementCount = 0; |
374ca955 A |
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 | ||
729e4ab9 A |
252 | if (hashTable == NULL) { |
253 | return; | |
254 | } | |
255 | elementCount = uhash_count(hashTable); | |
256 | ||
257 | /*initialize the mapping data */ | |
4388f060 | 258 | mappingData = (uint16_t*) uprv_calloc(mappingDataCapacity, U_SIZEOF_UCHAR); |
374ca955 A |
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){ | |
51004dcb A |
319 | fprintf(stderr, "Too many entries in the mapping table %i. Maximum allowed is %i\n", |
320 | currentIndex+value->length, _SPREP_MAX_INDEX_VALUE); | |
374ca955 A |
321 | exit(U_INDEX_OUTOFBOUNDS_ERROR); |
322 | } | |
323 | ||
324 | /* copy the mapping data */ | |
51004dcb A |
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 */ | |
a62d09fc | 331 | u_memmove(mappingData+currentIndex, value->mapping, value->length); |
51004dcb A |
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); | |
374ca955 | 337 | } |
374ca955 A |
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; | |
51004dcb | 361 | int16_t adjustedLen=0, i, j; |
374ca955 A |
362 | uint16_t trieWord = 0; |
363 | ValueStruct *value = NULL; | |
364 | uint32_t savedTrieWord = 0; | |
365 | ||
366 | /* initialize the hashtable */ | |
367 | if(hashTable==NULL){ | |
73c04bcf | 368 | hashTable = uhash_open(hashEntry, compareEntries, NULL, status); |
374ca955 A |
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++){ | |
51004dcb | 390 | adjustedLen += U16_LENGTH(mapping[i]); |
374ca955 A |
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 | ||
4388f060 | 440 | map = (UChar*) uprv_calloc(adjustedLen + 1, U_SIZEOF_UCHAR); |
374ca955 | 441 | |
51004dcb A |
442 | for (i=0, j=0; i<length; i++) { |
443 | U16_APPEND_UNSAFE(map, j, mapping[i]); | |
374ca955 A |
444 | } |
445 | ||
446 | value = (ValueStruct*) uprv_malloc(sizeof(ValueStruct)); | |
447 | value->mapping = map; | |
51004dcb | 448 | value->type = type; |
374ca955 A |
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 | |
340931cb A |
467 | storeRange(uint32_t start, uint32_t end, UStringPrepType type, UErrorCode* status){ |
468 | (void)status; // suppress compiler warnings about unused variable | |
374ca955 A |
469 | uint16_t trieWord = 0; |
470 | ||
73c04bcf | 471 | if((int)(_SPREP_TYPE_THRESHOLD + type) > 0xFFFF){ |
374ca955 A |
472 | fprintf(stderr,"trieWord cannot contain value greater than 0xFFFF.\n"); |
473 | exit(U_ILLEGAL_CHAR_FOUND); | |
474 | } | |
73c04bcf | 475 | trieWord = (_SPREP_TYPE_THRESHOLD + type); /* the top 4 bits contain the value */ |
374ca955 A |
476 | if(start == end){ |
477 | uint32_t savedTrieWord = utrie_get32(sprepTrie, start, NULL); | |
478 | if(savedTrieWord>0){ | |
479 | if(savedTrieWord < _SPREP_TYPE_THRESHOLD && type == USPREP_PROHIBITED){ | |
480 | /* | |
481 | * A mapping is stored in the trie word | |
482 | * and the only other possible type that a | |
483 | * code point can have is USPREP_PROHIBITED | |
484 | * | |
485 | */ | |
486 | ||
487 | /* turn on the 0th bit in the savedTrieWord */ | |
488 | savedTrieWord += 0x01; | |
489 | ||
490 | /* the downcast is safe since we only save 16 bit values */ | |
491 | trieWord = (uint16_t)savedTrieWord; | |
492 | ||
493 | /* make sure that the value of trieWord is less than the threshold */ | |
494 | if(trieWord < _SPREP_TYPE_THRESHOLD){ | |
495 | /* now set the value in the trie */ | |
496 | if(!utrie_set32(sprepTrie,start,trieWord)){ | |
497 | fprintf(stderr,"Could not set the value for code point.\n"); | |
498 | exit(U_ILLEGAL_ARGUMENT_ERROR); | |
499 | } | |
500 | /* value is set so just return */ | |
501 | return; | |
502 | }else{ | |
503 | fprintf(stderr,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD); | |
504 | exit(U_ILLEGAL_CHAR_FOUND); | |
505 | } | |
506 | ||
507 | }else if(savedTrieWord != trieWord){ | |
508 | fprintf(stderr,"Value for codepoint \\U%08X already set!.\n", (int)start); | |
509 | exit(U_ILLEGAL_ARGUMENT_ERROR); | |
510 | } | |
511 | /* if savedTrieWord == trieWord .. fall through and set the value */ | |
512 | } | |
513 | if(!utrie_set32(sprepTrie,start,trieWord)){ | |
514 | fprintf(stderr,"Could not set the value for code point \\U%08X.\n", (int)start); | |
515 | exit(U_ILLEGAL_ARGUMENT_ERROR); | |
516 | } | |
517 | }else{ | |
518 | if(!utrie_setRange32(sprepTrie, start, end+1, trieWord, FALSE)){ | |
519 | fprintf(stderr,"Value for certain codepoint already set.\n"); | |
520 | exit(U_ILLEGAL_CHAR_FOUND); | |
521 | } | |
522 | } | |
523 | ||
524 | } | |
525 | ||
526 | /* folding value: just store the offset (16 bits) if there is any non-0 entry */ | |
527 | static uint32_t U_CALLCONV | |
528 | getFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset) { | |
4388f060 | 529 | uint32_t value; |
374ca955 A |
530 | UChar32 limit=0; |
531 | UBool inBlockZero; | |
532 | ||
374ca955 A |
533 | limit=start+0x400; |
534 | while(start<limit) { | |
535 | value=utrie_get32(trie, start, &inBlockZero); | |
536 | if(inBlockZero) { | |
537 | start+=UTRIE_DATA_BLOCK_LENGTH; | |
538 | } else if(value!=0) { | |
539 | return (uint32_t)offset; | |
540 | } else { | |
541 | ++start; | |
542 | } | |
543 | } | |
544 | return 0; | |
545 | ||
546 | } | |
547 | ||
548 | #endif /* #if !UCONFIG_NO_IDNA */ | |
549 | ||
550 | extern void | |
73c04bcf | 551 | generateData(const char *dataDir, const char* bundleName) { |
374ca955 A |
552 | static uint8_t sprepTrieBlock[100000]; |
553 | ||
554 | UNewDataMemory *pData; | |
555 | UErrorCode errorCode=U_ZERO_ERROR; | |
556 | int32_t size, dataLength; | |
557 | char* fileName = (char*) uprv_malloc(uprv_strlen(bundleName) +100); | |
558 | ||
559 | #if UCONFIG_NO_IDNA | |
560 | ||
561 | size=0; | |
562 | ||
563 | #else | |
564 | ||
565 | int32_t sprepTrieSize; | |
566 | ||
567 | /* sort and add mapping data */ | |
568 | storeMappingData(); | |
569 | ||
570 | sprepTrieSize=utrie_serialize(sprepTrie, sprepTrieBlock, sizeof(sprepTrieBlock), getFoldedValue, TRUE, &errorCode); | |
571 | if(U_FAILURE(errorCode)) { | |
572 | fprintf(stderr, "error: utrie_serialize(sprep trie) failed, %s\n", u_errorName(errorCode)); | |
573 | exit(errorCode); | |
574 | } | |
575 | ||
576 | size = sprepTrieSize + mappingDataCapacity*U_SIZEOF_UCHAR + sizeof(indexes); | |
577 | if(beVerbose) { | |
578 | printf("size of sprep trie %5u bytes\n", (int)sprepTrieSize); | |
579 | printf("size of " U_ICUDATA_NAME "_%s." DATA_TYPE " contents: %ld bytes\n", bundleName,(long)size); | |
580 | printf("size of mapping data array %5u bytes\n",(int)mappingDataCapacity * U_SIZEOF_UCHAR); | |
581 | printf("Number of code units in mappingData (currentIndex) are: %i \n", currentIndex); | |
582 | printf("Maximum length of the mapping string is : %i \n", (int)maxLength); | |
583 | } | |
584 | ||
585 | #endif | |
586 | ||
73c04bcf | 587 | fileName[0]=0; |
374ca955 A |
588 | uprv_strcat(fileName,bundleName); |
589 | /* write the data */ | |
590 | pData=udata_create(dataDir, DATA_TYPE, fileName, &dataInfo, | |
591 | haveCopyright ? U_COPYRIGHT_STRING : NULL, &errorCode); | |
592 | if(U_FAILURE(errorCode)) { | |
593 | fprintf(stderr, "gensprep: unable to create the output file, error %d\n", errorCode); | |
594 | exit(errorCode); | |
595 | } | |
596 | ||
597 | #if !UCONFIG_NO_IDNA | |
598 | ||
599 | indexes[_SPREP_INDEX_TRIE_SIZE]=sprepTrieSize; | |
600 | indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]=mappingDataCapacity*U_SIZEOF_UCHAR; | |
601 | ||
602 | udata_writeBlock(pData, indexes, sizeof(indexes)); | |
603 | udata_writeBlock(pData, sprepTrieBlock, sprepTrieSize); | |
604 | udata_writeBlock(pData, mappingData, indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]); | |
605 | ||
606 | ||
607 | #endif | |
608 | ||
609 | /* finish up */ | |
610 | dataLength=udata_finish(pData, &errorCode); | |
611 | if(U_FAILURE(errorCode)) { | |
612 | fprintf(stderr, "gensprep: error %d writing the output file\n", errorCode); | |
613 | exit(errorCode); | |
614 | } | |
615 | ||
616 | if(dataLength!=size) { | |
617 | fprintf(stderr, "gensprep error: data length %ld != calculated size %ld\n", | |
618 | (long)dataLength, (long)size); | |
619 | exit(U_INTERNAL_PROGRAM_ERROR); | |
620 | } | |
621 | ||
622 | #if !UCONFIG_NO_IDNA | |
623 | /* done with writing the data .. close the hashtable */ | |
729e4ab9 A |
624 | if (hashTable != NULL) { |
625 | uhash_close(hashTable); | |
626 | } | |
374ca955 | 627 | #endif |
51004dcb A |
628 | |
629 | uprv_free(fileName); | |
374ca955 A |
630 | } |
631 | ||
632 | #if !UCONFIG_NO_IDNA | |
633 | ||
634 | extern void | |
635 | cleanUpData(void) { | |
51004dcb | 636 | uprv_free(mappingData); |
374ca955 A |
637 | utrie_close(sprepTrie); |
638 | uprv_free(sprepTrie); | |
639 | } | |
640 | ||
641 | #endif /* #if !UCONFIG_NO_IDNA */ | |
642 | ||
643 | /* | |
644 | * Hey, Emacs, please set the following: | |
645 | * | |
646 | * Local Variables: | |
647 | * indent-tabs-mode: nil | |
648 | * End: | |
649 | * | |
650 | */ |