• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  *
6  *   Copyright (C) 2003-2012, International Business Machines
7  *   Corporation and others.  All Rights Reserved.
8  *
9  *******************************************************************************
10  *   file name:  spreptst.c
11  *   encoding:   UTF-8
12  *   tab size:   8 (not used)
13  *   indentation:4
14  *
15  *   created on: 2003jul11
16  *   created by: Ram Viswanadha
17  */
18 #define USPREP_TYPE_NAMES_ARRAY
19 
20 #include "unicode/utypes.h"
21 
22 #if !UCONFIG_NO_IDNA
23 
24 #include <stdbool.h>
25 
26 #include "unicode/ustring.h"
27 #include "unicode/putil.h"
28 #include "cintltst.h"
29 #include "unicode/usprep.h"
30 #include "unicode/utf16.h"
31 #include "sprpimpl.h"
32 #include "uparse.h"
33 #include "cmemory.h"
34 #include "ustr_imp.h"
35 #include "cstring.h"
36 
37 static void
38 parseMappings(const char *filename, UStringPrepProfile* data, UBool reportError, UErrorCode *pErrorCode);
39 
40 static void
41 compareMapping(UStringPrepProfile* data, uint32_t codepoint, uint32_t* mapping, int32_t mapLength,
42                UStringPrepType option);
43 
44 static void
45 compareFlagsForRange(UStringPrepProfile* data, uint32_t start, uint32_t end,UStringPrepType option);
46 
47 void
48 doStringPrepTest(const char* binFileName, const char* txtFileName, int32_t options, UErrorCode* errorCode);
49 
50 static void U_CALLCONV
strprepProfileLineFn(void * context,char * fields[][2],int32_t fieldCount,UErrorCode * pErrorCode)51 strprepProfileLineFn(void *context,
52               char *fields[][2], int32_t fieldCount,
53               UErrorCode *pErrorCode) {
54     (void)fieldCount; // suppress compiler warnings about unused variable
55     uint32_t mapping[40];
56     char *end, *map;
57     uint32_t code;
58     int32_t length;
59     UStringPrepProfile* data = (UStringPrepProfile*) context;
60     const char* typeName;
61     uint32_t rangeStart=0,rangeEnd =0;
62 
63     typeName = fields[2][0];
64     map = fields[1][0];
65 
66     if(strstr(typeName, usprepTypeNames[USPREP_UNASSIGNED])!=NULL){
67 
68         u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode);
69 
70         /* store the range */
71         compareFlagsForRange(data, rangeStart,rangeEnd,USPREP_UNASSIGNED);
72 
73     }else if(strstr(typeName, usprepTypeNames[USPREP_PROHIBITED])!=NULL){
74 
75         u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode);
76 
77         /* store the range */
78         compareFlagsForRange(data, rangeStart,rangeEnd,USPREP_PROHIBITED);
79 
80     }else if(strstr(typeName, usprepTypeNames[USPREP_MAP])!=NULL){
81         /* get the character code, field 0 */
82         code=(uint32_t)uprv_strtoul(fields[0][0], &end, 16);
83 
84         /* parse the mapping string */
85         length=u_parseCodePoints(map, mapping, sizeof(mapping)/4, pErrorCode);
86 
87         /* compare the mapping */
88         compareMapping(data, code,mapping, length,USPREP_MAP);
89     }else{
90         *pErrorCode = U_INVALID_FORMAT_ERROR;
91     }
92 
93 }
94 
95 
96 
97 static void
parseMappings(const char * filename,UStringPrepProfile * data,UBool reportError,UErrorCode * pErrorCode)98 parseMappings(const char *filename, UStringPrepProfile* data, UBool reportError, UErrorCode *pErrorCode) {
99     char *fields[3][2];
100 
101     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
102         return;
103     }
104 
105     u_parseDelimitedFile(filename, ';', fields, 3, strprepProfileLineFn, (void*)data, pErrorCode);
106 
107     /*fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);*/
108 
109     if(U_FAILURE(*pErrorCode) && (reportError || *pErrorCode!=U_FILE_ACCESS_ERROR)) {
110         log_err( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename, u_errorName(*pErrorCode));
111     }
112 }
113 
114 
115 static UStringPrepType
getValues(uint32_t result,int32_t * value,UBool * isIndex)116 getValues(uint32_t result, int32_t* value, UBool* isIndex){
117 
118     UStringPrepType type;
119     if(result == 0){
120         /*
121          * Initial value stored in the mapping table
122          * just return USPREP_TYPE_LIMIT .. so that
123          * the source codepoint is copied to the destination
124          */
125         type = USPREP_TYPE_LIMIT;
126     }else if(result >= _SPREP_TYPE_THRESHOLD){
127         type = (UStringPrepType) (result - _SPREP_TYPE_THRESHOLD);
128     }else{
129         /* get the type */
130         type = USPREP_MAP;
131         /* ascertain if the value is index or delta */
132         if(result & 0x02){
133             *isIndex = true;
134             *value = result  >> 2;
135 
136         }else{
137             *isIndex = false;
138             *value = (int16_t)result;
139             *value =  (*value >> 2);
140 
141         }
142         if((result>>2) == _SPREP_MAX_INDEX_VALUE){
143             type = USPREP_DELETE;
144             isIndex =false;
145             value = 0;
146         }
147     }
148     return type;
149 }
150 
151 static void
compareMapping(UStringPrepProfile * data,uint32_t codepoint,uint32_t * mapping,int32_t mapLength,UStringPrepType type)152 compareMapping(UStringPrepProfile* data, uint32_t codepoint, uint32_t* mapping,int32_t mapLength,
153                UStringPrepType type){
154     uint32_t result = 0;
155     int32_t length=0;
156     UBool isIndex = false;
157     UStringPrepType retType;
158     int32_t value=0, idx=0, delta=0;
159     int32_t* indexes = data->indexes;
160     UTrie trie = data->sprepTrie;
161     const uint16_t* mappingData = data->mappingData;
162     int32_t realLength =0;
163     int32_t j=0;
164     int8_t i=0;
165 
166     UTRIE_GET16(&trie, codepoint, result);
167     retType = getValues(result,&value,&isIndex);
168 
169 
170     if(type != retType && retType != USPREP_DELETE){
171 
172         log_err( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint, USPREP_MAP, type);
173 
174     }
175 
176     if(isIndex){
177         idx = value;
178         if(idx >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] &&
179                  idx < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){
180             length = 1;
181         }else if(idx >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] &&
182                  idx < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){
183             length = 2;
184         }else if(idx >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] &&
185                  idx < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){
186             length = 3;
187         }else{
188             length = mappingData[idx++];
189         }
190     }else{
191         delta = value;
192         length = (retType == USPREP_DELETE)? 0 :  1;
193     }
194 
195     /* figure out the real length */
196     for(j=0; j<mapLength; j++){
197         if(mapping[j] > 0xFFFF){
198             realLength +=2;
199         }else{
200             realLength++;
201         }
202     }
203 
204     if(realLength != length){
205         log_err( "Did not get the expected length. Expected: %i Got: %i\n", mapLength, length);
206     }
207 
208     if(isIndex){
209         for(i =0; i< mapLength; i++){
210             if(mapping[i] <= 0xFFFF){
211                 if(mappingData[idx+i] != (uint16_t)mapping[i]){
212                     log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[i], mappingData[idx+i]);
213                 }
214             }else{
215                 UChar lead  = U16_LEAD(mapping[i]);
216                 UChar trail = U16_TRAIL(mapping[i]);
217                 if(mappingData[idx+i] != lead ||
218                     mappingData[idx+i+1] != trail){
219                     log_err( "Did not get the expected result. Expected: 0x%04X 0x%04X  Got: 0x%04X 0x%04X\n", lead, trail, mappingData[idx+i], mappingData[idx+i+1]);
220                 }
221             }
222         }
223     }else{
224         if(retType!=USPREP_DELETE && (codepoint-delta) != (uint16_t)mapping[0]){
225            log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[0],(codepoint-delta));
226         }
227     }
228 
229 }
230 
231 static void
compareFlagsForRange(UStringPrepProfile * data,uint32_t start,uint32_t end,UStringPrepType type)232 compareFlagsForRange(UStringPrepProfile* data,
233                      uint32_t start, uint32_t end,
234                      UStringPrepType type){
235 
236     uint32_t result =0 ;
237     UStringPrepType retType;
238     UBool isIndex=false;
239     int32_t value=0;
240     UTrie trie = data->sprepTrie;
241 /*
242     // supplementary code point
243     UChar __lead16=U16_LEAD(0x2323E);
244     int32_t __offset;
245 
246     // get data for lead surrogate
247     (result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16));
248     __offset=(&idnTrie)->getFoldingOffset(result);
249 
250     // get the real data from the folded lead/trail units
251     if(__offset>0) {
252         (result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff);
253     } else {
254         (result)=(uint32_t)((&idnTrie)->initialValue);
255     }
256 
257     UTRIE_GET16(&idnTrie,0x2323E, result);
258 */
259     while(start < end+1){
260         UTRIE_GET16(&trie,start, result);
261         retType = getValues(result, &value, &isIndex);
262         if(result > _SPREP_TYPE_THRESHOLD){
263             if(retType != type){
264                 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
265             }
266         }else{
267             if(type == USPREP_PROHIBITED && ((result & 0x01) != 0x01)){
268                 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
269             }
270         }
271 
272         start++;
273     }
274 
275 }
276 
277 void
doStringPrepTest(const char * binFileName,const char * txtFileName,int32_t options,UErrorCode * errorCode)278 doStringPrepTest(const char* binFileName, const char* txtFileName, int32_t options, UErrorCode* errorCode){
279     (void)options; // suppress compiler warnings about unused variable
280     const char *testdatapath = loadTestData(errorCode);
281     const char *srcdatapath = NULL;
282     const char *relativepath = NULL;
283     char *filename = NULL;
284     UStringPrepProfile* profile = NULL;
285 
286 #ifdef U_TOPSRCDIR
287     srcdatapath = U_TOPSRCDIR;
288     relativepath = U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
289 #else
290     srcdatapath = ctest_dataOutDir();
291     relativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
292 #endif
293 
294     profile = usprep_open(testdatapath, binFileName, errorCode);
295 
296     if(*errorCode == U_FILE_ACCESS_ERROR) {
297         log_data_err("Failed to load %s data file. Error: %s \n", binFileName, u_errorName(*errorCode));
298         return;
299     } else if(U_FAILURE(*errorCode)){
300         log_err("Failed to load %s data file. Error: %s \n", binFileName, u_errorName(*errorCode));
301         return;
302     }
303     filename = (char*) malloc(strlen(srcdatapath)+strlen(relativepath)+strlen(txtFileName)+10 );
304     /* open and load the txt file */
305     strcpy(filename,srcdatapath);
306     strcat(filename,relativepath);
307     strcat(filename,txtFileName);
308 
309     parseMappings(filename,profile, true,errorCode);
310 
311     free(filename);
312 }
313 #endif
314 /*
315  * Hey, Emacs, please set the following:
316  *
317  * Local Variables:
318  * indent-tabs-mode: nil
319  * End:
320  *
321  */
322