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