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