1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 2003-02-06
16 * created by: Ram Viswanadha
22 #include "unicode/utypes.h"
26 #include "unicode/udata.h"
27 #include "unicode/utf16.h"
34 #define DO_DEBUG_OUT 0
38 * StringPrep profile file format ------------------------------------
40 * The file format prepared and written here contains a 16-bit trie and a mapping table.
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.
47 * The following is a description of format version 2.
51 * The contents is a parsed, binary form of RFC3454 and possibly
52 * NormalizationCorrections.txt depending on the options specified on the profile.
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
59 * *.spp files customarily begin with a UDataInfo structure, see udata.h and .c.
60 * After that there are the following structures:
62 * int32_t indexes[_SPREP_INDEX_TOP]; -- _SPREP_INDEX_TOP=16, see enum in sprpimpl.h file
64 * UTrie stringPrepTrie; -- size in bytes=indexes[_SPREP_INDEX_TRIE_SIZE]
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]
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
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.
85 * - structure of data words from the trie
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;
97 * ii) A value less than _SPREP_TYPE_THRESHOLD means the type is USPREP_MAP and
98 * contains distribution described below
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
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:
113 * if( index >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] &&
114 * index < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){
116 * }else if(index >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] &&
117 * index < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){
119 * }else if(index >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] &&
120 * index < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){
123 * // The first position in the mapping table contains the length
125 * length = mappingTable[index++];
131 /* file data ---------------------------------------------------------------- */
132 /* indexes[] value names */
136 /* dummy UDataInfo cf. udata.h */
137 static UDataInfo dataInfo
= {
146 { 0, 0, 0, 0 }, /* dummy dataFormat */
147 { 0, 0, 0, 0 }, /* dummy formatVersion */
148 { 0, 0, 0, 0 } /* dummy dataVersion */
153 static int32_t indexes
[_SPREP_INDEX_TOP
]={ 0 };
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 */
161 /* UDataInfo cf. udata.h */
162 static UDataInfo dataInfo
={
171 { 0x53, 0x50, 0x52, 0x50 }, /* dataFormat="SPRP" */
172 { 3, 2, UTRIE_SHIFT
, UTRIE_INDEX_SHIFT
}, /* formatVersion */
173 { 3, 2, 0, 0 } /* dataVersion (Unicode version) */
176 setUnicodeVersion(const char *v
) {
177 UVersionInfo version
;
178 u_versionFromString(version
, v
);
179 uprv_memcpy(dataInfo
.dataVersion
, version
, 4);
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
;
190 static UNewTrie
*sprepTrie
;
192 #define MAX_DATA_LENGTH 11500
195 #define SPREP_DELTA_RANGE_POSITIVE_LIMIT 8191
196 #define SPREP_DELTA_RANGE_NEGATIVE_LIMIT -8192
202 sprepTrie
= (UNewTrie
*)uprv_calloc(1, sizeof(UNewTrie
));
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
);
211 static UHashtable
* hashTable
= NULL
;
214 typedef struct ValueStruct
{
217 UStringPrepType type
;
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
);
227 /* Callback for hashing the entry */
228 static int32_t U_CALLCONV
hashEntry(const UHashTok parm
) {
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
);
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;
252 if (hashTable
== NULL
) {
255 elementCount
= uhash_count(hashTable
);
257 /*initialize the mapping data */
258 mappingData
= (uint16_t*) uprv_calloc(mappingDataCapacity
, U_SIZEOF_UCHAR
);
260 while(writtenElementCount
< elementCount
){
262 while( (element
= uhash_nextElement(hashTable
, &pos
))!=NULL
){
264 codepoint
= element
->key
.integer
;
265 value
= (ValueStruct
*)element
->value
.pointer
;
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
;
273 if(oldMappingLength
<= _SPREP_MAX_INDEX_TOP_LENGTH
&&
274 mappingLength
== _SPREP_MAX_INDEX_TOP_LENGTH
+1){
276 limitIndex
= currentIndex
;
279 oldMappingLength
= mappingLength
;
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 */
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
);
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 */
300 * the codepoint has value something other than prohibited
301 * and a mapping .. error!
303 fprintf(stderr
,"Type for codepoint \\U%08X already set!.\n", (int)codepoint
);
304 exit(U_ILLEGAL_ARGUMENT_ERROR
);
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
);
314 /* written the trie word for the codepoint... increment the count*/
315 writtenElementCount
++;
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
);
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
;
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
);
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;
347 indexes
[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START
] = limitIndex
;
352 extern void setOptions(int32_t options
){
353 indexes
[_SPREP_OPTIONS
] = options
;
356 storeMapping(uint32_t codepoint
, uint32_t* mapping
,int32_t length
,
357 UStringPrepType type
, UErrorCode
* status
){
361 int16_t adjustedLen
=0, i
, j
;
362 uint16_t trieWord
= 0;
363 ValueStruct
*value
= NULL
;
364 uint32_t savedTrieWord
= 0;
366 /* initialize the hashtable */
368 hashTable
= uhash_open(hashEntry
, compareEntries
, NULL
, status
);
369 uhash_setValueDeleter(hashTable
, valueDeleter
);
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 */
380 * the codepoint has value something other than prohibited
381 * and a mapping .. error!
383 fprintf(stderr
,"Type for codepoint \\U%08X already set!.\n", (int)codepoint
);
384 exit(U_ILLEGAL_ARGUMENT_ERROR
);
388 /* figure out the real length */
389 for(i
=0; i
<length
; i
++){
390 adjustedLen
+= U16_LENGTH(mapping
[i
]);
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
);
402 /* value is set so just return */
405 fprintf(stderr
,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD
);
406 exit(U_ILLEGAL_CHAR_FOUND
);
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
){
415 trieWord
= delta
<< 2;
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
);
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
);
430 /* value is set so just return */
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
440 map
= (UChar
*) uprv_calloc(adjustedLen
+ 1, U_SIZEOF_UCHAR
);
442 for (i
=0, j
=0; i
<length
; i
++) {
443 U16_APPEND_UNSAFE(map
, j
, mapping
[i
]);
446 value
= (ValueStruct
*) uprv_malloc(sizeof(ValueStruct
));
447 value
->mapping
= map
;
449 value
->length
= adjustedLen
;
450 if(value
->length
> _SPREP_MAX_INDEX_TOP_LENGTH
){
451 mappingDataCapacity
++;
453 if(maxLength
< value
->length
){
454 maxLength
= value
->length
;
456 uhash_iput(hashTable
,codepoint
,value
,status
);
457 mappingDataCapacity
+= adjustedLen
;
459 if(U_FAILURE(*status
)){
460 fprintf(stderr
, "Failed to put entries into the hastable. Error: %s\n", u_errorName(*status
));
467 storeRange(uint32_t start
, uint32_t end
, UStringPrepType type
,UErrorCode
* status
){
468 uint16_t trieWord
= 0;
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
);
474 trieWord
= (_SPREP_TYPE_THRESHOLD
+ type
); /* the top 4 bits contain the value */
476 uint32_t savedTrieWord
= utrie_get32(sprepTrie
, start
, NULL
);
478 if(savedTrieWord
< _SPREP_TYPE_THRESHOLD
&& type
== USPREP_PROHIBITED
){
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
486 /* turn on the 0th bit in the savedTrieWord */
487 savedTrieWord
+= 0x01;
489 /* the downcast is safe since we only save 16 bit values */
490 trieWord
= (uint16_t)savedTrieWord
;
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
);
499 /* value is set so just return */
502 fprintf(stderr
,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD
);
503 exit(U_ILLEGAL_CHAR_FOUND
);
506 }else if(savedTrieWord
!= trieWord
){
507 fprintf(stderr
,"Value for codepoint \\U%08X already set!.\n", (int)start
);
508 exit(U_ILLEGAL_ARGUMENT_ERROR
);
510 /* if savedTrieWord == trieWord .. fall through and set the value */
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
);
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
);
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
) {
534 value
=utrie_get32(trie
, start
, &inBlockZero
);
536 start
+=UTRIE_DATA_BLOCK_LENGTH
;
537 } else if(value
!=0) {
538 return (uint32_t)offset
;
547 #endif /* #if !UCONFIG_NO_IDNA */
550 generateData(const char *dataDir
, const char* bundleName
) {
551 static uint8_t sprepTrieBlock
[100000];
553 UNewDataMemory
*pData
;
554 UErrorCode errorCode
=U_ZERO_ERROR
;
555 int32_t size
, dataLength
;
556 char* fileName
= (char*) uprv_malloc(uprv_strlen(bundleName
) +100);
564 int32_t sprepTrieSize
;
566 /* sort and add mapping data */
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
));
575 size
= sprepTrieSize
+ mappingDataCapacity
*U_SIZEOF_UCHAR
+ sizeof(indexes
);
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
);
587 uprv_strcat(fileName
,bundleName
);
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
);
598 indexes
[_SPREP_INDEX_TRIE_SIZE
]=sprepTrieSize
;
599 indexes
[_SPREP_INDEX_MAPPING_DATA_SIZE
]=mappingDataCapacity
*U_SIZEOF_UCHAR
;
601 udata_writeBlock(pData
, indexes
, sizeof(indexes
));
602 udata_writeBlock(pData
, sprepTrieBlock
, sprepTrieSize
);
603 udata_writeBlock(pData
, mappingData
, indexes
[_SPREP_INDEX_MAPPING_DATA_SIZE
]);
609 dataLength
=udata_finish(pData
, &errorCode
);
610 if(U_FAILURE(errorCode
)) {
611 fprintf(stderr
, "gensprep: error %d writing the output file\n", errorCode
);
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
);
622 /* done with writing the data .. close the hashtable */
623 if (hashTable
!= NULL
) {
624 uhash_close(hashTable
);
635 uprv_free(mappingData
);
636 utrie_close(sprepTrie
);
637 uprv_free(sprepTrie
);
640 #endif /* #if !UCONFIG_NO_IDNA */
643 * Hey, Emacs, please set the following:
646 * indent-tabs-mode: nil