Debian Codegraph @

intl/icu/source/common/uidna.cpp

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