intl/icu/source/common/uidna.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2003-2009, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 * file name: uidna.cpp
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: 2003feb1
michael@0 14 * created by: Ram Viswanadha
michael@0 15 */
michael@0 16
michael@0 17 #include "unicode/utypes.h"
michael@0 18
michael@0 19 #if !UCONFIG_NO_IDNA
michael@0 20
michael@0 21 #include "unicode/uidna.h"
michael@0 22 #include "unicode/ustring.h"
michael@0 23 #include "unicode/usprep.h"
michael@0 24 #include "punycode.h"
michael@0 25 #include "ustr_imp.h"
michael@0 26 #include "cmemory.h"
michael@0 27 #include "uassert.h"
michael@0 28 #include "sprpimpl.h"
michael@0 29
michael@0 30 /* it is official IDNA ACE Prefix is "xn--" */
michael@0 31 static const UChar ACE_PREFIX[] ={ 0x0078,0x006E,0x002d,0x002d } ;
michael@0 32 #define ACE_PREFIX_LENGTH 4
michael@0 33
michael@0 34 #define MAX_LABEL_LENGTH 63
michael@0 35 /* The Max length of the labels should not be more than MAX_LABEL_LENGTH */
michael@0 36 #define MAX_LABEL_BUFFER_SIZE 100
michael@0 37
michael@0 38 #define MAX_DOMAIN_NAME_LENGTH 255
michael@0 39 /* The Max length of the domain names should not be more than MAX_DOMAIN_NAME_LENGTH */
michael@0 40 #define MAX_IDN_BUFFER_SIZE MAX_DOMAIN_NAME_LENGTH+1
michael@0 41
michael@0 42 #define LOWER_CASE_DELTA 0x0020
michael@0 43 #define HYPHEN 0x002D
michael@0 44 #define FULL_STOP 0x002E
michael@0 45 #define CAPITAL_A 0x0041
michael@0 46 #define CAPITAL_Z 0x005A
michael@0 47
michael@0 48 inline static UChar
michael@0 49 toASCIILower(UChar ch){
michael@0 50 if(CAPITAL_A <= ch && ch <= CAPITAL_Z){
michael@0 51 return ch + LOWER_CASE_DELTA;
michael@0 52 }
michael@0 53 return ch;
michael@0 54 }
michael@0 55
michael@0 56 inline static UBool
michael@0 57 startsWithPrefix(const UChar* src , int32_t srcLength){
michael@0 58 UBool startsWithPrefix = TRUE;
michael@0 59
michael@0 60 if(srcLength < ACE_PREFIX_LENGTH){
michael@0 61 return FALSE;
michael@0 62 }
michael@0 63
michael@0 64 for(int8_t i=0; i< ACE_PREFIX_LENGTH; i++){
michael@0 65 if(toASCIILower(src[i]) != ACE_PREFIX[i]){
michael@0 66 startsWithPrefix = FALSE;
michael@0 67 }
michael@0 68 }
michael@0 69 return startsWithPrefix;
michael@0 70 }
michael@0 71
michael@0 72
michael@0 73 inline static int32_t
michael@0 74 compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
michael@0 75 const UChar* s2, int32_t s2Len){
michael@0 76
michael@0 77 int32_t minLength;
michael@0 78 int32_t lengthResult;
michael@0 79
michael@0 80 // are we comparing different lengths?
michael@0 81 if(s1Len != s2Len) {
michael@0 82 if(s1Len < s2Len) {
michael@0 83 minLength = s1Len;
michael@0 84 lengthResult = -1;
michael@0 85 } else {
michael@0 86 minLength = s2Len;
michael@0 87 lengthResult = 1;
michael@0 88 }
michael@0 89 } else {
michael@0 90 // ok the lengths are equal
michael@0 91 minLength = s1Len;
michael@0 92 lengthResult = 0;
michael@0 93 }
michael@0 94
michael@0 95 UChar c1,c2;
michael@0 96 int32_t rc;
michael@0 97
michael@0 98 for(int32_t i =0;/* no condition */;i++) {
michael@0 99
michael@0 100 /* If we reach the ends of both strings then they match */
michael@0 101 if(i == minLength) {
michael@0 102 return lengthResult;
michael@0 103 }
michael@0 104
michael@0 105 c1 = s1[i];
michael@0 106 c2 = s2[i];
michael@0 107
michael@0 108 /* Case-insensitive comparison */
michael@0 109 if(c1!=c2) {
michael@0 110 rc=(int32_t)toASCIILower(c1)-(int32_t)toASCIILower(c2);
michael@0 111 if(rc!=0) {
michael@0 112 lengthResult=rc;
michael@0 113 break;
michael@0 114 }
michael@0 115 }
michael@0 116 }
michael@0 117 return lengthResult;
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 /**
michael@0 122 * Ascertain if the given code point is a label separator as
michael@0 123 * defined by the IDNA RFC
michael@0 124 *
michael@0 125 * @param ch The code point to be ascertained
michael@0 126 * @return true if the char is a label separator
michael@0 127 * @stable ICU 2.8
michael@0 128 */
michael@0 129 static inline UBool isLabelSeparator(UChar ch){
michael@0 130 switch(ch){
michael@0 131 case 0x002e:
michael@0 132 case 0x3002:
michael@0 133 case 0xFF0E:
michael@0 134 case 0xFF61:
michael@0 135 return TRUE;
michael@0 136 default:
michael@0 137 return FALSE;
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 // returns the length of the label excluding the separator
michael@0 142 // if *limit == separator then the length returned does not include
michael@0 143 // the separtor.
michael@0 144 static inline int32_t
michael@0 145 getNextSeparator(UChar *src, int32_t srcLength,
michael@0 146 UChar **limit, UBool *done){
michael@0 147 if(srcLength == -1){
michael@0 148 int32_t i;
michael@0 149 for(i=0 ; ;i++){
michael@0 150 if(src[i] == 0){
michael@0 151 *limit = src + i; // point to null
michael@0 152 *done = TRUE;
michael@0 153 return i;
michael@0 154 }
michael@0 155 if(isLabelSeparator(src[i])){
michael@0 156 *limit = src + (i+1); // go past the delimiter
michael@0 157 return i;
michael@0 158
michael@0 159 }
michael@0 160 }
michael@0 161 }else{
michael@0 162 int32_t i;
michael@0 163 for(i=0;i<srcLength;i++){
michael@0 164 if(isLabelSeparator(src[i])){
michael@0 165 *limit = src + (i+1); // go past the delimiter
michael@0 166 return i;
michael@0 167 }
michael@0 168 }
michael@0 169 // we have not found the delimiter
michael@0 170 // if(i==srcLength)
michael@0 171 *limit = src+srcLength;
michael@0 172 *done = TRUE;
michael@0 173
michael@0 174 return i;
michael@0 175 }
michael@0 176 }
michael@0 177 static inline UBool isLDHChar(UChar ch){
michael@0 178 // high runner case
michael@0 179 if(ch>0x007A){
michael@0 180 return FALSE;
michael@0 181 }
michael@0 182 //[\\u002D \\u0030-\\u0039 \\u0041-\\u005A \\u0061-\\u007A]
michael@0 183 if( (ch==0x002D) ||
michael@0 184 (0x0030 <= ch && ch <= 0x0039) ||
michael@0 185 (0x0041 <= ch && ch <= 0x005A) ||
michael@0 186 (0x0061 <= ch && ch <= 0x007A)
michael@0 187 ){
michael@0 188 return TRUE;
michael@0 189 }
michael@0 190 return FALSE;
michael@0 191 }
michael@0 192
michael@0 193 static int32_t
michael@0 194 _internal_toASCII(const UChar* src, int32_t srcLength,
michael@0 195 UChar* dest, int32_t destCapacity,
michael@0 196 int32_t options,
michael@0 197 UStringPrepProfile* nameprep,
michael@0 198 UParseError* parseError,
michael@0 199 UErrorCode* status)
michael@0 200 {
michael@0 201
michael@0 202 // TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too.
michael@0 203 UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE];
michael@0 204 //initialize pointers to stack buffers
michael@0 205 UChar *b1 = b1Stack, *b2 = b2Stack;
michael@0 206 int32_t b1Len=0, b2Len,
michael@0 207 b1Capacity = MAX_LABEL_BUFFER_SIZE,
michael@0 208 b2Capacity = MAX_LABEL_BUFFER_SIZE ,
michael@0 209 reqLength=0;
michael@0 210
michael@0 211 int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0;
michael@0 212 UBool* caseFlags = NULL;
michael@0 213
michael@0 214 // the source contains all ascii codepoints
michael@0 215 UBool srcIsASCII = TRUE;
michael@0 216 // assume the source contains all LDH codepoints
michael@0 217 UBool srcIsLDH = TRUE;
michael@0 218
michael@0 219 int32_t j=0;
michael@0 220
michael@0 221 //get the options
michael@0 222 UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
michael@0 223
michael@0 224 int32_t failPos = -1;
michael@0 225
michael@0 226 if(srcLength == -1){
michael@0 227 srcLength = u_strlen(src);
michael@0 228 }
michael@0 229
michael@0 230 if(srcLength > b1Capacity){
michael@0 231 b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
michael@0 232 if(b1==NULL){
michael@0 233 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 234 goto CLEANUP;
michael@0 235 }
michael@0 236 b1Capacity = srcLength;
michael@0 237 }
michael@0 238
michael@0 239 // step 1
michael@0 240 for( j=0;j<srcLength;j++){
michael@0 241 if(src[j] > 0x7F){
michael@0 242 srcIsASCII = FALSE;
michael@0 243 }
michael@0 244 b1[b1Len++] = src[j];
michael@0 245 }
michael@0 246
michael@0 247 // step 2 is performed only if the source contains non ASCII
michael@0 248 if(srcIsASCII == FALSE){
michael@0 249
michael@0 250 // step 2
michael@0 251 b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Capacity, namePrepOptions, parseError, status);
michael@0 252
michael@0 253 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 254 // redo processing of string
michael@0 255 // we do not have enough room so grow the buffer
michael@0 256 if(b1 != b1Stack){
michael@0 257 uprv_free(b1);
michael@0 258 }
michael@0 259 b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
michael@0 260 if(b1==NULL){
michael@0 261 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 262 goto CLEANUP;
michael@0 263 }
michael@0 264
michael@0 265 *status = U_ZERO_ERROR; // reset error
michael@0 266
michael@0 267 b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status);
michael@0 268 }
michael@0 269 }
michael@0 270 // error bail out
michael@0 271 if(U_FAILURE(*status)){
michael@0 272 goto CLEANUP;
michael@0 273 }
michael@0 274 if(b1Len == 0){
michael@0 275 *status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
michael@0 276 goto CLEANUP;
michael@0 277 }
michael@0 278
michael@0 279 // for step 3 & 4
michael@0 280 srcIsASCII = TRUE;
michael@0 281 for( j=0;j<b1Len;j++){
michael@0 282 // check if output of usprep_prepare is all ASCII
michael@0 283 if(b1[j] > 0x7F){
michael@0 284 srcIsASCII = FALSE;
michael@0 285 }else if(isLDHChar(b1[j])==FALSE){ // if the char is in ASCII range verify that it is an LDH character
michael@0 286 srcIsLDH = FALSE;
michael@0 287 failPos = j;
michael@0 288 }
michael@0 289 }
michael@0 290 if(useSTD3ASCIIRules == TRUE){
michael@0 291 // verify 3a and 3b
michael@0 292 // 3(a) Verify the absence of non-LDH ASCII code points; that is, the
michael@0 293 // absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
michael@0 294 // 3(b) Verify the absence of leading and trailing hyphen-minus; that
michael@0 295 // is, the absence of U+002D at the beginning and end of the
michael@0 296 // sequence.
michael@0 297 if( srcIsLDH == FALSE /* source at this point should not contain anyLDH characters */
michael@0 298 || b1[0] == HYPHEN || b1[b1Len-1] == HYPHEN){
michael@0 299 *status = U_IDNA_STD3_ASCII_RULES_ERROR;
michael@0 300
michael@0 301 /* populate the parseError struct */
michael@0 302 if(srcIsLDH==FALSE){
michael@0 303 // failPos is always set the index of failure
michael@0 304 uprv_syntaxError(b1,failPos, b1Len,parseError);
michael@0 305 }else if(b1[0] == HYPHEN){
michael@0 306 // fail position is 0
michael@0 307 uprv_syntaxError(b1,0,b1Len,parseError);
michael@0 308 }else{
michael@0 309 // the last index in the source is always length-1
michael@0 310 uprv_syntaxError(b1, (b1Len>0) ? b1Len-1 : b1Len, b1Len,parseError);
michael@0 311 }
michael@0 312
michael@0 313 goto CLEANUP;
michael@0 314 }
michael@0 315 }
michael@0 316 // Step 4: if the source is ASCII then proceed to step 8
michael@0 317 if(srcIsASCII){
michael@0 318 if(b1Len <= destCapacity){
michael@0 319 uprv_memmove(dest, b1, b1Len * U_SIZEOF_UCHAR);
michael@0 320 reqLength = b1Len;
michael@0 321 }else{
michael@0 322 reqLength = b1Len;
michael@0 323 goto CLEANUP;
michael@0 324 }
michael@0 325 }else{
michael@0 326 // step 5 : verify the sequence does not begin with ACE prefix
michael@0 327 if(!startsWithPrefix(b1,b1Len)){
michael@0 328
michael@0 329 //step 6: encode the sequence with punycode
michael@0 330
michael@0 331 // do not preserve the case flags for now!
michael@0 332 // TODO: Preserve the case while implementing the RFE
michael@0 333 // caseFlags = (UBool*) uprv_malloc(b1Len * sizeof(UBool));
michael@0 334 // uprv_memset(caseFlags,TRUE,b1Len);
michael@0 335
michael@0 336 b2Len = u_strToPunycode(b1,b1Len,b2,b2Capacity,caseFlags, status);
michael@0 337
michael@0 338 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 339 // redo processing of string
michael@0 340 /* we do not have enough room so grow the buffer*/
michael@0 341 b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
michael@0 342 if(b2 == NULL){
michael@0 343 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 344 goto CLEANUP;
michael@0 345 }
michael@0 346
michael@0 347 *status = U_ZERO_ERROR; // reset error
michael@0 348
michael@0 349 b2Len = u_strToPunycode(b1,b1Len,b2,b2Len,caseFlags, status);
michael@0 350 }
michael@0 351 //error bail out
michael@0 352 if(U_FAILURE(*status)){
michael@0 353 goto CLEANUP;
michael@0 354 }
michael@0 355 // TODO : Reconsider while implementing the case preserve RFE
michael@0 356 // convert all codepoints to lower case ASCII
michael@0 357 // toASCIILower(b2,b2Len);
michael@0 358 reqLength = b2Len+ACE_PREFIX_LENGTH;
michael@0 359
michael@0 360 if(reqLength > destCapacity){
michael@0 361 *status = U_BUFFER_OVERFLOW_ERROR;
michael@0 362 goto CLEANUP;
michael@0 363 }
michael@0 364 //Step 7: prepend the ACE prefix
michael@0 365 uprv_memcpy(dest,ACE_PREFIX,ACE_PREFIX_LENGTH * U_SIZEOF_UCHAR);
michael@0 366 //Step 6: copy the contents in b2 into dest
michael@0 367 uprv_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len * U_SIZEOF_UCHAR);
michael@0 368
michael@0 369 }else{
michael@0 370 *status = U_IDNA_ACE_PREFIX_ERROR;
michael@0 371 //position of failure is 0
michael@0 372 uprv_syntaxError(b1,0,b1Len,parseError);
michael@0 373 goto CLEANUP;
michael@0 374 }
michael@0 375 }
michael@0 376 // step 8: verify the length of label
michael@0 377 if(reqLength > MAX_LABEL_LENGTH){
michael@0 378 *status = U_IDNA_LABEL_TOO_LONG_ERROR;
michael@0 379 }
michael@0 380
michael@0 381 CLEANUP:
michael@0 382 if(b1 != b1Stack){
michael@0 383 uprv_free(b1);
michael@0 384 }
michael@0 385 if(b2 != b2Stack){
michael@0 386 uprv_free(b2);
michael@0 387 }
michael@0 388 uprv_free(caseFlags);
michael@0 389
michael@0 390 return u_terminateUChars(dest, destCapacity, reqLength, status);
michael@0 391 }
michael@0 392
michael@0 393 static int32_t
michael@0 394 _internal_toUnicode(const UChar* src, int32_t srcLength,
michael@0 395 UChar* dest, int32_t destCapacity,
michael@0 396 int32_t options,
michael@0 397 UStringPrepProfile* nameprep,
michael@0 398 UParseError* parseError,
michael@0 399 UErrorCode* status)
michael@0 400 {
michael@0 401
michael@0 402 //get the options
michael@0 403 //UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
michael@0 404 int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0;
michael@0 405
michael@0 406 // TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too.
michael@0 407 UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE], b3Stack[MAX_LABEL_BUFFER_SIZE];
michael@0 408
michael@0 409 //initialize pointers to stack buffers
michael@0 410 UChar *b1 = b1Stack, *b2 = b2Stack, *b1Prime=NULL, *b3=b3Stack;
michael@0 411 int32_t b1Len, b2Len, b1PrimeLen, b3Len,
michael@0 412 b1Capacity = MAX_LABEL_BUFFER_SIZE,
michael@0 413 b2Capacity = MAX_LABEL_BUFFER_SIZE,
michael@0 414 b3Capacity = MAX_LABEL_BUFFER_SIZE,
michael@0 415 reqLength=0;
michael@0 416
michael@0 417 b1Len = 0;
michael@0 418 UBool* caseFlags = NULL;
michael@0 419
michael@0 420 UBool srcIsASCII = TRUE;
michael@0 421 /*UBool srcIsLDH = TRUE;
michael@0 422 int32_t failPos =0;*/
michael@0 423
michael@0 424 // step 1: find out if all the codepoints in src are ASCII
michael@0 425 if(srcLength==-1){
michael@0 426 srcLength = 0;
michael@0 427 for(;src[srcLength]!=0;){
michael@0 428 if(src[srcLength]> 0x7f){
michael@0 429 srcIsASCII = FALSE;
michael@0 430 }/*else if(isLDHChar(src[srcLength])==FALSE){
michael@0 431 // here we do not assemble surrogates
michael@0 432 // since we know that LDH code points
michael@0 433 // are in the ASCII range only
michael@0 434 srcIsLDH = FALSE;
michael@0 435 failPos = srcLength;
michael@0 436 }*/
michael@0 437 srcLength++;
michael@0 438 }
michael@0 439 }else if(srcLength > 0){
michael@0 440 for(int32_t j=0; j<srcLength; j++){
michael@0 441 if(src[j]> 0x7f){
michael@0 442 srcIsASCII = FALSE;
michael@0 443 }/*else if(isLDHChar(src[j])==FALSE){
michael@0 444 // here we do not assemble surrogates
michael@0 445 // since we know that LDH code points
michael@0 446 // are in the ASCII range only
michael@0 447 srcIsLDH = FALSE;
michael@0 448 failPos = j;
michael@0 449 }*/
michael@0 450 }
michael@0 451 }else{
michael@0 452 return 0;
michael@0 453 }
michael@0 454
michael@0 455 if(srcIsASCII == FALSE){
michael@0 456 // step 2: process the string
michael@0 457 b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Capacity, namePrepOptions, parseError, status);
michael@0 458 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 459 // redo processing of string
michael@0 460 /* we do not have enough room so grow the buffer*/
michael@0 461 b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
michael@0 462 if(b1==NULL){
michael@0 463 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 464 goto CLEANUP;
michael@0 465 }
michael@0 466
michael@0 467 *status = U_ZERO_ERROR; // reset error
michael@0 468
michael@0 469 b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status);
michael@0 470 }
michael@0 471 //bail out on error
michael@0 472 if(U_FAILURE(*status)){
michael@0 473 goto CLEANUP;
michael@0 474 }
michael@0 475 }else{
michael@0 476
michael@0 477 //just point src to b1
michael@0 478 b1 = (UChar*) src;
michael@0 479 b1Len = srcLength;
michael@0 480 }
michael@0 481
michael@0 482 // The RFC states that
michael@0 483 // <quote>
michael@0 484 // ToUnicode never fails. If any step fails, then the original input
michael@0 485 // is returned immediately in that step.
michael@0 486 // </quote>
michael@0 487
michael@0 488 //step 3: verify ACE Prefix
michael@0 489 if(startsWithPrefix(b1,b1Len)){
michael@0 490
michael@0 491 //step 4: Remove the ACE Prefix
michael@0 492 b1Prime = b1 + ACE_PREFIX_LENGTH;
michael@0 493 b1PrimeLen = b1Len - ACE_PREFIX_LENGTH;
michael@0 494
michael@0 495 //step 5: Decode using punycode
michael@0 496 b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Capacity, caseFlags,status);
michael@0 497
michael@0 498 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 499 // redo processing of string
michael@0 500 /* we do not have enough room so grow the buffer*/
michael@0 501 b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
michael@0 502 if(b2==NULL){
michael@0 503 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 504 goto CLEANUP;
michael@0 505 }
michael@0 506
michael@0 507 *status = U_ZERO_ERROR; // reset error
michael@0 508
michael@0 509 b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Len, caseFlags, status);
michael@0 510 }
michael@0 511
michael@0 512
michael@0 513 //step 6:Apply toASCII
michael@0 514 b3Len = uidna_toASCII(b2, b2Len, b3, b3Capacity, options, parseError, status);
michael@0 515
michael@0 516 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 517 // redo processing of string
michael@0 518 /* we do not have enough room so grow the buffer*/
michael@0 519 b3 = (UChar*) uprv_malloc(b3Len * U_SIZEOF_UCHAR);
michael@0 520 if(b3==NULL){
michael@0 521 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 522 goto CLEANUP;
michael@0 523 }
michael@0 524
michael@0 525 *status = U_ZERO_ERROR; // reset error
michael@0 526
michael@0 527 b3Len = uidna_toASCII(b2,b2Len,b3,b3Len,options,parseError, status);
michael@0 528
michael@0 529 }
michael@0 530 //bail out on error
michael@0 531 if(U_FAILURE(*status)){
michael@0 532 goto CLEANUP;
michael@0 533 }
michael@0 534
michael@0 535 //step 7: verify
michael@0 536 if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){
michael@0 537 // Cause the original to be returned.
michael@0 538 *status = U_IDNA_VERIFICATION_ERROR;
michael@0 539 goto CLEANUP;
michael@0 540 }
michael@0 541
michael@0 542 //step 8: return output of step 5
michael@0 543 reqLength = b2Len;
michael@0 544 if(b2Len <= destCapacity) {
michael@0 545 uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR);
michael@0 546 }
michael@0 547 }
michael@0 548 else{
michael@0 549 // See the start of this if statement for why this is commented out.
michael@0 550 // verify that STD3 ASCII rules are satisfied
michael@0 551 /*if(useSTD3ASCIIRules == TRUE){
michael@0 552 if( srcIsLDH == FALSE // source contains some non-LDH characters
michael@0 553 || src[0] == HYPHEN || src[srcLength-1] == HYPHEN){
michael@0 554 *status = U_IDNA_STD3_ASCII_RULES_ERROR;
michael@0 555
michael@0 556 // populate the parseError struct
michael@0 557 if(srcIsLDH==FALSE){
michael@0 558 // failPos is always set the index of failure
michael@0 559 uprv_syntaxError(src,failPos, srcLength,parseError);
michael@0 560 }else if(src[0] == HYPHEN){
michael@0 561 // fail position is 0
michael@0 562 uprv_syntaxError(src,0,srcLength,parseError);
michael@0 563 }else{
michael@0 564 // the last index in the source is always length-1
michael@0 565 uprv_syntaxError(src, (srcLength>0) ? srcLength-1 : srcLength, srcLength,parseError);
michael@0 566 }
michael@0 567
michael@0 568 goto CLEANUP;
michael@0 569 }
michael@0 570 }*/
michael@0 571 // just return the source
michael@0 572 //copy the source to destination
michael@0 573 if(srcLength <= destCapacity){
michael@0 574 uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
michael@0 575 }
michael@0 576 reqLength = srcLength;
michael@0 577 }
michael@0 578
michael@0 579
michael@0 580 CLEANUP:
michael@0 581
michael@0 582 if(b1 != b1Stack && b1!=src){
michael@0 583 uprv_free(b1);
michael@0 584 }
michael@0 585 if(b2 != b2Stack){
michael@0 586 uprv_free(b2);
michael@0 587 }
michael@0 588 uprv_free(caseFlags);
michael@0 589
michael@0 590 // The RFC states that
michael@0 591 // <quote>
michael@0 592 // ToUnicode never fails. If any step fails, then the original input
michael@0 593 // is returned immediately in that step.
michael@0 594 // </quote>
michael@0 595 // So if any step fails lets copy source to destination
michael@0 596 if(U_FAILURE(*status)){
michael@0 597 //copy the source to destination
michael@0 598 if(dest && srcLength <= destCapacity){
michael@0 599 // srcLength should have already been set earlier.
michael@0 600 U_ASSERT(srcLength >= 0);
michael@0 601 uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
michael@0 602 }
michael@0 603 reqLength = srcLength;
michael@0 604 *status = U_ZERO_ERROR;
michael@0 605 }
michael@0 606
michael@0 607 return u_terminateUChars(dest, destCapacity, reqLength, status);
michael@0 608 }
michael@0 609
michael@0 610 U_CAPI int32_t U_EXPORT2
michael@0 611 uidna_toASCII(const UChar* src, int32_t srcLength,
michael@0 612 UChar* dest, int32_t destCapacity,
michael@0 613 int32_t options,
michael@0 614 UParseError* parseError,
michael@0 615 UErrorCode* status){
michael@0 616
michael@0 617 if(status == NULL || U_FAILURE(*status)){
michael@0 618 return 0;
michael@0 619 }
michael@0 620 if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
michael@0 621 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 622 return 0;
michael@0 623 }
michael@0 624
michael@0 625 UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
michael@0 626
michael@0 627 if(U_FAILURE(*status)){
michael@0 628 return -1;
michael@0 629 }
michael@0 630
michael@0 631 int32_t retLen = _internal_toASCII(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
michael@0 632
michael@0 633 /* close the profile*/
michael@0 634 usprep_close(nameprep);
michael@0 635
michael@0 636 return retLen;
michael@0 637 }
michael@0 638
michael@0 639 U_CAPI int32_t U_EXPORT2
michael@0 640 uidna_toUnicode(const UChar* src, int32_t srcLength,
michael@0 641 UChar* dest, int32_t destCapacity,
michael@0 642 int32_t options,
michael@0 643 UParseError* parseError,
michael@0 644 UErrorCode* status){
michael@0 645
michael@0 646 if(status == NULL || U_FAILURE(*status)){
michael@0 647 return 0;
michael@0 648 }
michael@0 649 if( (src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
michael@0 650 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 651 return 0;
michael@0 652 }
michael@0 653
michael@0 654 UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
michael@0 655
michael@0 656 if(U_FAILURE(*status)){
michael@0 657 return -1;
michael@0 658 }
michael@0 659
michael@0 660 int32_t retLen = _internal_toUnicode(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
michael@0 661
michael@0 662 usprep_close(nameprep);
michael@0 663
michael@0 664 return retLen;
michael@0 665 }
michael@0 666
michael@0 667
michael@0 668 U_CAPI int32_t U_EXPORT2
michael@0 669 uidna_IDNToASCII( const UChar *src, int32_t srcLength,
michael@0 670 UChar* dest, int32_t destCapacity,
michael@0 671 int32_t options,
michael@0 672 UParseError *parseError,
michael@0 673 UErrorCode *status){
michael@0 674
michael@0 675 if(status == NULL || U_FAILURE(*status)){
michael@0 676 return 0;
michael@0 677 }
michael@0 678 if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
michael@0 679 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 680 return 0;
michael@0 681 }
michael@0 682
michael@0 683 int32_t reqLength = 0;
michael@0 684
michael@0 685 UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
michael@0 686
michael@0 687 if(U_FAILURE(*status)){
michael@0 688 return 0;
michael@0 689 }
michael@0 690
michael@0 691 //initialize pointers
michael@0 692 UChar *delimiter = (UChar*)src;
michael@0 693 UChar *labelStart = (UChar*)src;
michael@0 694 UChar *currentDest = (UChar*) dest;
michael@0 695 int32_t remainingLen = srcLength;
michael@0 696 int32_t remainingDestCapacity = destCapacity;
michael@0 697 int32_t labelLen = 0, labelReqLength = 0;
michael@0 698 UBool done = FALSE;
michael@0 699
michael@0 700
michael@0 701 for(;;){
michael@0 702
michael@0 703 labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
michael@0 704 labelReqLength = 0;
michael@0 705 if(!(labelLen==0 && done)){// make sure this is not a root label separator.
michael@0 706
michael@0 707 labelReqLength = _internal_toASCII( labelStart, labelLen,
michael@0 708 currentDest, remainingDestCapacity,
michael@0 709 options, nameprep,
michael@0 710 parseError, status);
michael@0 711
michael@0 712 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 713
michael@0 714 *status = U_ZERO_ERROR; // reset error
michael@0 715 remainingDestCapacity = 0;
michael@0 716 }
michael@0 717 }
michael@0 718
michael@0 719
michael@0 720 if(U_FAILURE(*status)){
michael@0 721 break;
michael@0 722 }
michael@0 723
michael@0 724 reqLength +=labelReqLength;
michael@0 725 // adjust the destination pointer
michael@0 726 if(labelReqLength < remainingDestCapacity){
michael@0 727 currentDest = currentDest + labelReqLength;
michael@0 728 remainingDestCapacity -= labelReqLength;
michael@0 729 }else{
michael@0 730 // should never occur
michael@0 731 remainingDestCapacity = 0;
michael@0 732 }
michael@0 733
michael@0 734 if(done == TRUE){
michael@0 735 break;
michael@0 736 }
michael@0 737
michael@0 738 // add the label separator
michael@0 739 if(remainingDestCapacity > 0){
michael@0 740 *currentDest++ = FULL_STOP;
michael@0 741 remainingDestCapacity--;
michael@0 742 }
michael@0 743 reqLength++;
michael@0 744
michael@0 745 labelStart = delimiter;
michael@0 746 if(remainingLen >0 ){
michael@0 747 remainingLen = (int32_t)(srcLength - (delimiter - src));
michael@0 748 }
michael@0 749
michael@0 750 }
michael@0 751
michael@0 752 if(reqLength > MAX_DOMAIN_NAME_LENGTH){
michael@0 753 *status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
michael@0 754 }
michael@0 755
michael@0 756 usprep_close(nameprep);
michael@0 757
michael@0 758 return u_terminateUChars(dest, destCapacity, reqLength, status);
michael@0 759 }
michael@0 760
michael@0 761 U_CAPI int32_t U_EXPORT2
michael@0 762 uidna_IDNToUnicode( const UChar* src, int32_t srcLength,
michael@0 763 UChar* dest, int32_t destCapacity,
michael@0 764 int32_t options,
michael@0 765 UParseError* parseError,
michael@0 766 UErrorCode* status){
michael@0 767
michael@0 768 if(status == NULL || U_FAILURE(*status)){
michael@0 769 return 0;
michael@0 770 }
michael@0 771 if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
michael@0 772 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 773 return 0;
michael@0 774 }
michael@0 775
michael@0 776 int32_t reqLength = 0;
michael@0 777
michael@0 778 UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
michael@0 779
michael@0 780 if(U_FAILURE(*status)){
michael@0 781 return 0;
michael@0 782 }
michael@0 783
michael@0 784 //initialize pointers
michael@0 785 UChar *delimiter = (UChar*)src;
michael@0 786 UChar *labelStart = (UChar*)src;
michael@0 787 UChar *currentDest = (UChar*) dest;
michael@0 788 int32_t remainingLen = srcLength;
michael@0 789 int32_t remainingDestCapacity = destCapacity;
michael@0 790 int32_t labelLen = 0, labelReqLength = 0;
michael@0 791 UBool done = FALSE;
michael@0 792
michael@0 793 for(;;){
michael@0 794
michael@0 795 labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
michael@0 796
michael@0 797 // The RFC states that
michael@0 798 // <quote>
michael@0 799 // ToUnicode never fails. If any step fails, then the original input
michael@0 800 // is returned immediately in that step.
michael@0 801 // </quote>
michael@0 802 // _internal_toUnicode will copy the label.
michael@0 803 /*if(labelLen==0 && done==FALSE){
michael@0 804 *status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
michael@0 805 break;
michael@0 806 }*/
michael@0 807
michael@0 808 labelReqLength = _internal_toUnicode(labelStart, labelLen,
michael@0 809 currentDest, remainingDestCapacity,
michael@0 810 options, nameprep,
michael@0 811 parseError, status);
michael@0 812
michael@0 813 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 814 *status = U_ZERO_ERROR; // reset error
michael@0 815 remainingDestCapacity = 0;
michael@0 816 }
michael@0 817
michael@0 818 if(U_FAILURE(*status)){
michael@0 819 break;
michael@0 820 }
michael@0 821
michael@0 822 reqLength +=labelReqLength;
michael@0 823 // adjust the destination pointer
michael@0 824 if(labelReqLength < remainingDestCapacity){
michael@0 825 currentDest = currentDest + labelReqLength;
michael@0 826 remainingDestCapacity -= labelReqLength;
michael@0 827 }else{
michael@0 828 // should never occur
michael@0 829 remainingDestCapacity = 0;
michael@0 830 }
michael@0 831
michael@0 832 if(done == TRUE){
michael@0 833 break;
michael@0 834 }
michael@0 835
michael@0 836 // add the label separator
michael@0 837 // Unlike the ToASCII operation we don't normalize the label separators
michael@0 838 if(remainingDestCapacity > 0){
michael@0 839 *currentDest++ = *(labelStart + labelLen);
michael@0 840 remainingDestCapacity--;
michael@0 841 }
michael@0 842 reqLength++;
michael@0 843
michael@0 844 labelStart = delimiter;
michael@0 845 if(remainingLen >0 ){
michael@0 846 remainingLen = (int32_t)(srcLength - (delimiter - src));
michael@0 847 }
michael@0 848
michael@0 849 }
michael@0 850
michael@0 851 if(reqLength > MAX_DOMAIN_NAME_LENGTH){
michael@0 852 *status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
michael@0 853 }
michael@0 854
michael@0 855 usprep_close(nameprep);
michael@0 856
michael@0 857 return u_terminateUChars(dest, destCapacity, reqLength, status);
michael@0 858 }
michael@0 859
michael@0 860 U_CAPI int32_t U_EXPORT2
michael@0 861 uidna_compare( const UChar *s1, int32_t length1,
michael@0 862 const UChar *s2, int32_t length2,
michael@0 863 int32_t options,
michael@0 864 UErrorCode* status){
michael@0 865
michael@0 866 if(status == NULL || U_FAILURE(*status)){
michael@0 867 return -1;
michael@0 868 }
michael@0 869
michael@0 870 UChar b1Stack[MAX_IDN_BUFFER_SIZE], b2Stack[MAX_IDN_BUFFER_SIZE];
michael@0 871 UChar *b1 = b1Stack, *b2 = b2Stack;
michael@0 872 int32_t b1Len, b2Len, b1Capacity = MAX_IDN_BUFFER_SIZE, b2Capacity = MAX_IDN_BUFFER_SIZE;
michael@0 873 int32_t result=-1;
michael@0 874
michael@0 875 UParseError parseError;
michael@0 876
michael@0 877 b1Len = uidna_IDNToASCII(s1, length1, b1, b1Capacity, options, &parseError, status);
michael@0 878 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 879 // redo processing of string
michael@0 880 b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
michael@0 881 if(b1==NULL){
michael@0 882 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 883 goto CLEANUP;
michael@0 884 }
michael@0 885
michael@0 886 *status = U_ZERO_ERROR; // reset error
michael@0 887
michael@0 888 b1Len = uidna_IDNToASCII(s1,length1,b1,b1Len, options, &parseError, status);
michael@0 889
michael@0 890 }
michael@0 891
michael@0 892 b2Len = uidna_IDNToASCII(s2,length2, b2,b2Capacity, options, &parseError, status);
michael@0 893 if(*status == U_BUFFER_OVERFLOW_ERROR){
michael@0 894 // redo processing of string
michael@0 895 b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
michael@0 896 if(b2==NULL){
michael@0 897 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 898 goto CLEANUP;
michael@0 899 }
michael@0 900
michael@0 901 *status = U_ZERO_ERROR; // reset error
michael@0 902
michael@0 903 b2Len = uidna_IDNToASCII(s2, length2, b2, b2Len, options, &parseError, status);
michael@0 904
michael@0 905 }
michael@0 906 // when toASCII is applied all label separators are replaced with FULL_STOP
michael@0 907 result = compareCaseInsensitiveASCII(b1,b1Len,b2,b2Len);
michael@0 908
michael@0 909 CLEANUP:
michael@0 910 if(b1 != b1Stack){
michael@0 911 uprv_free(b1);
michael@0 912 }
michael@0 913
michael@0 914 if(b2 != b2Stack){
michael@0 915 uprv_free(b2);
michael@0 916 }
michael@0 917
michael@0 918 return result;
michael@0 919 }
michael@0 920
michael@0 921 #endif /* #if !UCONFIG_NO_IDNA */

mercurial