]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 2003, International Business Machines | |
5 | * Corporation and others. All Rights Reserved. | |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: testidn.cpp | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2003-02-06 | |
14 | * created by: Ram Viswanadha | |
15 | * | |
16 | * This program reads the rfc3454_*.txt files, | |
17 | * parses them, and extracts the data for Nameprep conformance. | |
18 | * It then preprocesses it and writes a binary file for efficient use | |
19 | * in various IDNA conversion processes. | |
20 | */ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include "unicode/utypes.h" | |
25 | ||
26 | #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION | |
27 | ||
374ca955 A |
28 | #define USPREP_TYPE_NAMES_ARRAY |
29 | ||
b75a7d8f A |
30 | #include "unicode/uchar.h" |
31 | #include "unicode/putil.h" | |
32 | #include "cmemory.h" | |
33 | #include "cstring.h" | |
34 | #include "unicode/udata.h" | |
35 | #include "unewdata.h" | |
36 | #include "uoptions.h" | |
37 | #include "uparse.h" | |
38 | #include "utrie.h" | |
39 | #include "umutex.h" | |
40 | #include "sprpimpl.h" | |
41 | #include "testidna.h" | |
374ca955 | 42 | #include "punyref.h" |
b75a7d8f A |
43 | |
44 | UBool beVerbose=FALSE, haveCopyright=TRUE; | |
45 | ||
46 | /* prototypes --------------------------------------------------------------- */ | |
47 | ||
48 | ||
b75a7d8f | 49 | static void |
374ca955 | 50 | parseMappings(const char *filename, UBool reportError,TestIDNA& test, UErrorCode *pErrorCode); |
b75a7d8f A |
51 | |
52 | static void | |
53 | compareMapping(uint32_t codepoint, uint32_t* mapping, int32_t mapLength, | |
374ca955 | 54 | UStringPrepType option); |
b75a7d8f A |
55 | |
56 | static void | |
374ca955 | 57 | compareFlagsForRange(uint32_t start, uint32_t end,UStringPrepType option); |
b75a7d8f A |
58 | |
59 | static void | |
60 | testAllCodepoints(TestIDNA& test); | |
61 | ||
62 | static TestIDNA* pTestIDNA =NULL; | |
63 | ||
64 | static const char* fileNames[] = { | |
374ca955 A |
65 | "NamePrepProfile.txt" |
66 | }; | |
67 | static UStringPrepProfile *profile = NULL; | |
68 | static const UTrie *idnTrie = NULL; | |
69 | static const int32_t *indexes = NULL; | |
70 | static const uint16_t *mappingData = NULL; | |
b75a7d8f A |
71 | /* -------------------------------------------------------------------------- */ |
72 | ||
73 | /* file definitions */ | |
74 | #define DATA_NAME "uidna" | |
75 | #define DATA_TYPE "icu" | |
76 | ||
77 | #define MISC_DIR "misc" | |
78 | ||
79 | extern int | |
80 | testData(TestIDNA& test) { | |
374ca955 | 81 | char* filename = (char*) malloc(strlen(IntlTest::pathToDataDirectory())*5555); |
b75a7d8f A |
82 | //TODO get the srcDir dynamically |
83 | const char *srcDir=IntlTest::pathToDataDirectory(); | |
84 | char *basename=NULL; | |
85 | UErrorCode errorCode=U_ZERO_ERROR; | |
86 | char *saveBasename =NULL; | |
87 | ||
374ca955 A |
88 | profile = usprep_open(NULL, DATA_NAME, &errorCode); |
89 | if(U_FAILURE(errorCode)){ | |
90 | test.errln("Failed to load IDNA data file. " + UnicodeString(u_errorName(errorCode))); | |
91 | return errorCode; | |
b75a7d8f A |
92 | } |
93 | ||
374ca955 A |
94 | idnTrie = &profile->sprepTrie; |
95 | indexes = profile->indexes; | |
96 | mappingData = profile->mappingData; | |
97 | ||
b75a7d8f A |
98 | //initialize |
99 | pTestIDNA = &test; | |
100 | ||
101 | /* prepare the filename beginning with the source dir */ | |
102 | if(uprv_strchr(srcDir,U_FILE_SEP_CHAR) == NULL){ | |
103 | filename[0] = 0x2E; | |
104 | filename[1] = U_FILE_SEP_CHAR; | |
105 | uprv_strcpy(filename+2,srcDir); | |
106 | }else{ | |
107 | uprv_strcpy(filename, srcDir); | |
108 | } | |
109 | basename=filename+uprv_strlen(filename); | |
110 | if(basename>filename && *(basename-1)!=U_FILE_SEP_CHAR) { | |
111 | *basename++=U_FILE_SEP_CHAR; | |
112 | } | |
113 | ||
114 | /* process unassigned */ | |
115 | basename=filename+uprv_strlen(filename); | |
116 | if(basename>filename && *(basename-1)!=U_FILE_SEP_CHAR) { | |
117 | *basename++=U_FILE_SEP_CHAR; | |
118 | } | |
119 | ||
120 | /* first copy misc directory */ | |
121 | saveBasename = basename; | |
122 | uprv_strcpy(basename,MISC_DIR); | |
123 | basename = basename + uprv_strlen(MISC_DIR); | |
124 | *basename++=U_FILE_SEP_CHAR; | |
125 | ||
126 | /* process unassigned */ | |
127 | uprv_strcpy(basename,fileNames[0]); | |
374ca955 | 128 | parseMappings(filename,TRUE, test,&errorCode); |
b75a7d8f A |
129 | if(U_FAILURE(errorCode)) { |
130 | test.errln( "Could not open file %s for reading \n", filename); | |
131 | return errorCode; | |
132 | } | |
133 | ||
134 | testAllCodepoints(test); | |
135 | ||
374ca955 | 136 | usprep_close(profile); |
b75a7d8f A |
137 | pTestIDNA = NULL; |
138 | free(filename); | |
139 | return errorCode; | |
140 | } | |
141 | U_CDECL_BEGIN | |
374ca955 | 142 | |
b75a7d8f | 143 | static void U_CALLCONV |
374ca955 A |
144 | strprepProfileLineFn(void * /*context*/, |
145 | char *fields[][2], int32_t fieldCount, | |
b75a7d8f A |
146 | UErrorCode *pErrorCode) { |
147 | uint32_t mapping[40]; | |
374ca955 | 148 | char *end, *map; |
b75a7d8f A |
149 | uint32_t code; |
150 | int32_t length; | |
374ca955 A |
151 | /*UBool* mapWithNorm = (UBool*) context;*/ |
152 | const char* typeName; | |
153 | uint32_t rangeStart=0,rangeEnd =0; | |
154 | ||
155 | if(fieldCount != 3){ | |
156 | *pErrorCode = U_INVALID_FORMAT_ERROR; | |
157 | return; | |
158 | } | |
b75a7d8f | 159 | |
374ca955 A |
160 | typeName = fields[2][0]; |
161 | map = fields[1][0]; | |
162 | ||
163 | if(uprv_strstr(typeName, usprepTypeNames[USPREP_UNASSIGNED])!=NULL){ | |
b75a7d8f | 164 | |
374ca955 A |
165 | u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode); |
166 | ||
167 | /* store the range */ | |
168 | compareFlagsForRange(rangeStart,rangeEnd,USPREP_UNASSIGNED); | |
169 | ||
170 | }else if(uprv_strstr(typeName, usprepTypeNames[USPREP_PROHIBITED])!=NULL){ | |
171 | ||
172 | u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode); | |
b75a7d8f | 173 | |
374ca955 A |
174 | /* store the range */ |
175 | compareFlagsForRange(rangeStart,rangeEnd,USPREP_PROHIBITED); | |
b75a7d8f | 176 | |
374ca955 A |
177 | }else if(uprv_strstr(typeName, usprepTypeNames[USPREP_MAP])!=NULL){ |
178 | /* get the character code, field 0 */ | |
179 | code=(uint32_t)uprv_strtoul(fields[0][0], &end, 16); | |
180 | ||
181 | /* parse the mapping string */ | |
182 | length=u_parseCodePoints(map, mapping, sizeof(mapping)/4, pErrorCode); | |
183 | ||
184 | /* store the mapping */ | |
185 | compareMapping(code,mapping, length,USPREP_MAP); | |
186 | ||
187 | }else{ | |
188 | *pErrorCode = U_INVALID_FORMAT_ERROR; | |
189 | } | |
b75a7d8f | 190 | |
b75a7d8f | 191 | } |
374ca955 | 192 | |
b75a7d8f A |
193 | U_CDECL_END |
194 | ||
195 | static void | |
374ca955 | 196 | parseMappings(const char *filename,UBool reportError, TestIDNA& test, UErrorCode *pErrorCode) { |
b75a7d8f A |
197 | char *fields[3][2]; |
198 | ||
199 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
200 | return; | |
201 | } | |
202 | ||
374ca955 | 203 | u_parseDelimitedFile(filename, ';', fields, 3, strprepProfileLineFn, (void*)filename, pErrorCode); |
b75a7d8f A |
204 | |
205 | //fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len); | |
206 | ||
207 | if(U_FAILURE(*pErrorCode) && (reportError || *pErrorCode!=U_FILE_ACCESS_ERROR)) { | |
208 | test.errln( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename, u_errorName(*pErrorCode)); | |
209 | } | |
210 | } | |
211 | ||
b75a7d8f | 212 | |
374ca955 A |
213 | static inline UStringPrepType |
214 | getValues(uint32_t result, int32_t& value, UBool& isIndex){ | |
b75a7d8f | 215 | |
374ca955 | 216 | UStringPrepType type; |
b75a7d8f | 217 | |
374ca955 A |
218 | if(result == 0){ |
219 | /* | |
220 | * Initial value stored in the mapping table | |
221 | * just return USPREP_TYPE_LIMIT .. so that | |
222 | * the source codepoint is copied to the destination | |
223 | */ | |
224 | type = USPREP_TYPE_LIMIT; | |
225 | }else if(result >= _SPREP_TYPE_THRESHOLD){ | |
226 | type = (UStringPrepType) (result - _SPREP_TYPE_THRESHOLD); | |
227 | }else{ | |
228 | /* get the state */ | |
229 | type = USPREP_MAP; | |
230 | /* ascertain if the value is index or delta */ | |
231 | if(result & 0x02){ | |
232 | isIndex = TRUE; | |
233 | value = result >> 2; //mask off the lower 2 bits and shift | |
b75a7d8f | 234 | |
374ca955 A |
235 | }else{ |
236 | isIndex = FALSE; | |
237 | value = (int16_t)result; | |
238 | value = (value >> 2); | |
b75a7d8f | 239 | |
374ca955 A |
240 | } |
241 | if((result>>2) == _SPREP_MAX_INDEX_VALUE){ | |
242 | type = USPREP_DELETE; | |
243 | isIndex =FALSE; | |
244 | value = 0; | |
245 | } | |
246 | } | |
247 | return type; | |
b75a7d8f A |
248 | } |
249 | ||
374ca955 | 250 | |
b75a7d8f A |
251 | |
252 | static void | |
374ca955 A |
253 | testAllCodepoints(TestIDNA& test){ |
254 | /* | |
255 | { | |
256 | UChar str[19] = { | |
257 | 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774, | |
258 | 0x070F,//prohibited | |
259 | 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74 | |
260 | }; | |
261 | uint32_t in[19] = {0}; | |
262 | UErrorCode status = U_ZERO_ERROR; | |
263 | int32_t inLength=0, outLength=100; | |
264 | char output[100] = {0}; | |
265 | punycode_status error; | |
266 | u_strToUTF32((UChar32*)in,19,&inLength,str,19,&status); | |
267 | ||
268 | error= punycode_encode(inLength, in, NULL, (uint32_t*)&outLength, output); | |
269 | printf(output); | |
270 | ||
b75a7d8f | 271 | } |
374ca955 | 272 | */ |
b75a7d8f | 273 | |
374ca955 A |
274 | uint32_t i = 0; |
275 | int32_t unassigned = 0; | |
276 | int32_t prohibited = 0; | |
277 | int32_t mappedWithNorm = 0; | |
278 | int32_t mapped = 0; | |
279 | int32_t noValueInTrie = 0; | |
b75a7d8f | 280 | |
374ca955 A |
281 | UStringPrepType type; |
282 | int32_t value; | |
283 | UBool isIndex = FALSE; | |
b75a7d8f | 284 | |
374ca955 A |
285 | for(i=0;i<=0x10FFFF;i++){ |
286 | uint32_t result = 0; | |
287 | UTRIE_GET16(idnTrie,i, result); | |
288 | type = getValues(result,value, isIndex); | |
289 | if(type != USPREP_TYPE_LIMIT ){ | |
290 | if(type == USPREP_UNASSIGNED){ | |
291 | unassigned++; | |
292 | } | |
293 | if(type == USPREP_PROHIBITED){ | |
294 | prohibited++; | |
295 | } | |
296 | if(type == USPREP_MAP){ | |
297 | mapped++; | |
298 | } | |
299 | }else{ | |
300 | noValueInTrie++; | |
301 | if(result > 0){ | |
302 | test.errln("The return value for 0x%06X is wrong. %i\n",i,result); | |
b75a7d8f A |
303 | } |
304 | } | |
b75a7d8f | 305 | } |
b75a7d8f | 306 | |
374ca955 A |
307 | test.logln("Number of Unassinged code points : %i \n",unassigned); |
308 | test.logln("Number of Prohibited code points : %i \n",prohibited); | |
309 | test.logln("Number of Mapped code points : %i \n",mapped); | |
310 | test.logln("Number of Mapped with NFKC code points : %i \n",mappedWithNorm); | |
311 | test.logln("Number of code points that have no value in Trie: %i \n",noValueInTrie); | |
312 | ||
313 | ||
b75a7d8f A |
314 | } |
315 | ||
316 | static void | |
317 | compareMapping(uint32_t codepoint, uint32_t* mapping,int32_t mapLength, | |
374ca955 A |
318 | UStringPrepType type){ |
319 | uint32_t result = 0; | |
320 | UTRIE_GET16(idnTrie,codepoint, result); | |
b75a7d8f | 321 | |
374ca955 A |
322 | int32_t length=0; |
323 | UBool isIndex; | |
324 | UStringPrepType retType; | |
325 | int32_t value, index=0, delta=0; | |
326 | ||
327 | retType = getValues(result,value,isIndex); | |
b75a7d8f A |
328 | |
329 | ||
374ca955 A |
330 | if(type != retType && retType != USPREP_DELETE){ |
331 | ||
332 | pTestIDNA->errln( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint, USPREP_MAP, type); | |
333 | ||
334 | } | |
335 | ||
336 | if(isIndex){ | |
337 | index = value; | |
338 | if(index >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] && | |
339 | index < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){ | |
340 | length = 1; | |
341 | }else if(index >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] && | |
342 | index < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){ | |
343 | length = 2; | |
344 | }else if(index >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] && | |
345 | index < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){ | |
346 | length = 3; | |
b75a7d8f | 347 | }else{ |
374ca955 | 348 | length = mappingData[index++]; |
b75a7d8f | 349 | } |
374ca955 A |
350 | }else{ |
351 | delta = value; | |
352 | length = (retType == USPREP_DELETE)? 0 : 1; | |
353 | } | |
b75a7d8f | 354 | |
374ca955 A |
355 | int32_t realLength =0; |
356 | /* figure out the real length */ | |
357 | for(int32_t j=0; j<mapLength; j++){ | |
358 | if(mapping[j] > 0xFFFF){ | |
359 | realLength +=2; | |
360 | }else{ | |
361 | realLength++; | |
362 | } | |
363 | } | |
b75a7d8f | 364 | |
374ca955 A |
365 | if(realLength != length){ |
366 | pTestIDNA->errln( "Did not get the expected length. Expected: %i Got: %i\n", mapLength, length); | |
367 | } | |
368 | ||
369 | if(isIndex){ | |
b75a7d8f A |
370 | for(int8_t i =0; i< mapLength; i++){ |
371 | if(mapping[i] <= 0xFFFF){ | |
372 | if(mappingData[index+i] != (uint16_t)mapping[i]){ | |
373 | pTestIDNA->errln("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[i], mappingData[index+i]); | |
374 | } | |
375 | }else{ | |
376 | UChar lead = UTF16_LEAD(mapping[i]); | |
377 | UChar trail = UTF16_TRAIL(mapping[i]); | |
378 | if(mappingData[index+i] != lead || | |
379 | mappingData[index+i+1] != trail){ | |
380 | pTestIDNA->errln( "Did not get the expected result. Expected: 0x%04X 0x%04X Got: 0x%04X 0x%04X", lead, trail, mappingData[index+i], mappingData[index+i+1]); | |
381 | } | |
382 | } | |
383 | } | |
374ca955 A |
384 | }else{ |
385 | if(retType!=USPREP_DELETE && (codepoint-delta) != (uint16_t)mapping[0]){ | |
386 | pTestIDNA->errln("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[0],(codepoint-delta)); | |
387 | } | |
b75a7d8f A |
388 | } |
389 | ||
390 | } | |
391 | ||
392 | static void | |
393 | compareFlagsForRange(uint32_t start, uint32_t end, | |
374ca955 A |
394 | UStringPrepType type){ |
395 | ||
396 | uint32_t result =0 ; | |
397 | UStringPrepType retType; | |
398 | UBool isIndex=FALSE; | |
399 | int32_t value=0; | |
400 | /* | |
401 | // supplementary code point | |
402 | UChar __lead16=UTF16_LEAD(0x2323E); | |
403 | int32_t __offset; | |
404 | ||
405 | // get data for lead surrogate | |
406 | (result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16)); | |
407 | __offset=(&idnTrie)->getFoldingOffset(result); | |
408 | ||
409 | // get the real data from the folded lead/trail units | |
410 | if(__offset>0) { | |
411 | (result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff); | |
b75a7d8f | 412 | } else { |
374ca955 | 413 | (result)=(uint32_t)((&idnTrie)->initialValue); |
b75a7d8f | 414 | } |
b75a7d8f | 415 | |
374ca955 A |
416 | UTRIE_GET16(&idnTrie,0x2323E, result); |
417 | */ | |
418 | while(start < end+1){ | |
419 | UTRIE_GET16(idnTrie,start, result); | |
420 | retType = getValues(result,value,isIndex); | |
421 | if(result > _SPREP_TYPE_THRESHOLD){ | |
422 | if(retType != type){ | |
423 | pTestIDNA->errln( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]); | |
424 | } | |
425 | }else{ | |
426 | if(type == USPREP_PROHIBITED && ((result & 0x01) != 0x01)){ | |
427 | pTestIDNA->errln( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]); | |
428 | } | |
b75a7d8f | 429 | } |
b75a7d8f | 430 | |
374ca955 | 431 | start++; |
b75a7d8f | 432 | } |
374ca955 | 433 | |
b75a7d8f A |
434 | } |
435 | ||
374ca955 | 436 | |
b75a7d8f A |
437 | #endif /* #if !UCONFIG_NO_IDNA */ |
438 | ||
439 | /* | |
440 | * Hey, Emacs, please set the following: | |
441 | * | |
442 | * Local Variables: | |
443 | * indent-tabs-mode: nil | |
444 | * End: | |
445 | * | |
446 | */ |