2003-02-28 21:35:25 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2014-09-11 05:25:13 +00:00
|
|
|
* Copyright (C) 2003-2014, International Business Machines
|
2003-02-28 21:35:25 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
2003-05-06 01:37:52 +00:00
|
|
|
* file name: uidna.cpp
|
2003-02-28 21:35:25 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2003feb1
|
|
|
|
* created by: Ram Viswanadha
|
|
|
|
*/
|
2003-05-06 01:37:52 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_IDNA
|
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
#include "unicode/uidna.h"
|
|
|
|
#include "unicode/ustring.h"
|
2003-07-24 23:23:19 +00:00
|
|
|
#include "unicode/usprep.h"
|
2003-02-28 21:35:25 +00:00
|
|
|
#include "punycode.h"
|
|
|
|
#include "ustr_imp.h"
|
|
|
|
#include "cmemory.h"
|
2007-07-31 21:13:31 +00:00
|
|
|
#include "uassert.h"
|
2003-02-28 21:35:25 +00:00
|
|
|
#include "sprpimpl.h"
|
|
|
|
|
|
|
|
/* it is official IDNA ACE Prefix is "xn--" */
|
|
|
|
static const UChar ACE_PREFIX[] ={ 0x0078,0x006E,0x002d,0x002d } ;
|
|
|
|
#define ACE_PREFIX_LENGTH 4
|
|
|
|
|
|
|
|
#define MAX_LABEL_LENGTH 63
|
2007-01-05 01:26:08 +00:00
|
|
|
/* The Max length of the labels should not be more than MAX_LABEL_LENGTH */
|
2007-01-11 00:33:32 +00:00
|
|
|
#define MAX_LABEL_BUFFER_SIZE 100
|
2007-01-05 01:26:08 +00:00
|
|
|
|
|
|
|
#define MAX_DOMAIN_NAME_LENGTH 255
|
|
|
|
/* The Max length of the domain names should not be more than MAX_DOMAIN_NAME_LENGTH */
|
|
|
|
#define MAX_IDN_BUFFER_SIZE MAX_DOMAIN_NAME_LENGTH+1
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
#define LOWER_CASE_DELTA 0x0020
|
2007-01-05 01:26:08 +00:00
|
|
|
#define HYPHEN 0x002D
|
2003-02-28 21:35:25 +00:00
|
|
|
#define FULL_STOP 0x002E
|
2007-01-05 01:26:08 +00:00
|
|
|
#define CAPITAL_A 0x0041
|
|
|
|
#define CAPITAL_Z 0x005A
|
|
|
|
|
2003-05-29 20:31:57 +00:00
|
|
|
inline static UChar
|
|
|
|
toASCIILower(UChar ch){
|
|
|
|
if(CAPITAL_A <= ch && ch <= CAPITAL_Z){
|
|
|
|
return ch + LOWER_CASE_DELTA;
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
inline static UBool
|
|
|
|
startsWithPrefix(const UChar* src , int32_t srcLength){
|
|
|
|
UBool startsWithPrefix = TRUE;
|
|
|
|
|
|
|
|
if(srcLength < ACE_PREFIX_LENGTH){
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int8_t i=0; i< ACE_PREFIX_LENGTH; i++){
|
2003-05-29 20:31:57 +00:00
|
|
|
if(toASCIILower(src[i]) != ACE_PREFIX[i]){
|
2003-02-28 21:35:25 +00:00
|
|
|
startsWithPrefix = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return startsWithPrefix;
|
|
|
|
}
|
|
|
|
|
2003-03-13 22:17:24 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
inline static int32_t
|
|
|
|
compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
|
|
|
|
const UChar* s2, int32_t s2Len){
|
2003-05-29 20:31:57 +00:00
|
|
|
|
|
|
|
int32_t minLength;
|
|
|
|
int32_t lengthResult;
|
|
|
|
|
|
|
|
// are we comparing different lengths?
|
|
|
|
if(s1Len != s2Len) {
|
|
|
|
if(s1Len < s2Len) {
|
|
|
|
minLength = s1Len;
|
|
|
|
lengthResult = -1;
|
|
|
|
} else {
|
|
|
|
minLength = s2Len;
|
|
|
|
lengthResult = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ok the lengths are equal
|
|
|
|
minLength = s1Len;
|
|
|
|
lengthResult = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2003-05-29 20:31:57 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
UChar c1,c2;
|
|
|
|
int32_t rc;
|
|
|
|
|
|
|
|
for(int32_t i =0;/* no condition */;i++) {
|
2003-05-29 20:31:57 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
/* If we reach the ends of both strings then they match */
|
2003-05-29 20:31:57 +00:00
|
|
|
if(i == minLength) {
|
|
|
|
return lengthResult;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
|
|
|
|
/* Case-insensitive comparison */
|
|
|
|
if(c1!=c2) {
|
|
|
|
rc=(int32_t)toASCIILower(c1)-(int32_t)toASCIILower(c2);
|
|
|
|
if(rc!=0) {
|
2003-05-29 20:31:57 +00:00
|
|
|
lengthResult=rc;
|
|
|
|
break;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-05-29 20:31:57 +00:00
|
|
|
return lengthResult;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-17 19:26:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ascertain if the given code point is a label separator as
|
|
|
|
* defined by the IDNA RFC
|
|
|
|
*
|
|
|
|
* @param ch The code point to be ascertained
|
|
|
|
* @return true if the char is a label separator
|
2005-06-22 23:38:06 +00:00
|
|
|
* @stable ICU 2.8
|
2003-10-17 19:26:35 +00:00
|
|
|
*/
|
|
|
|
static inline UBool isLabelSeparator(UChar ch){
|
|
|
|
switch(ch){
|
|
|
|
case 0x002e:
|
|
|
|
case 0x3002:
|
|
|
|
case 0xFF0E:
|
|
|
|
case 0xFF61:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the length of the label excluding the separator
|
|
|
|
// if *limit == separator then the length returned does not include
|
|
|
|
// the separtor.
|
|
|
|
static inline int32_t
|
2007-01-05 01:26:08 +00:00
|
|
|
getNextSeparator(UChar *src, int32_t srcLength,
|
|
|
|
UChar **limit, UBool *done){
|
2003-07-24 23:23:19 +00:00
|
|
|
if(srcLength == -1){
|
|
|
|
int32_t i;
|
|
|
|
for(i=0 ; ;i++){
|
|
|
|
if(src[i] == 0){
|
|
|
|
*limit = src + i; // point to null
|
|
|
|
*done = TRUE;
|
|
|
|
return i;
|
|
|
|
}
|
2003-10-17 19:26:35 +00:00
|
|
|
if(isLabelSeparator(src[i])){
|
2003-07-24 23:23:19 +00:00
|
|
|
*limit = src + (i+1); // go past the delimiter
|
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
int32_t i;
|
|
|
|
for(i=0;i<srcLength;i++){
|
2003-10-17 19:26:35 +00:00
|
|
|
if(isLabelSeparator(src[i])){
|
2003-07-24 23:23:19 +00:00
|
|
|
*limit = src + (i+1); // go past the delimiter
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we have not found the delimiter
|
|
|
|
// if(i==srcLength)
|
|
|
|
*limit = src+srcLength;
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
return i;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
2003-10-17 19:26:35 +00:00
|
|
|
static inline UBool isLDHChar(UChar ch){
|
|
|
|
// high runner case
|
|
|
|
if(ch>0x007A){
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
//[\\u002D \\u0030-\\u0039 \\u0041-\\u005A \\u0061-\\u007A]
|
|
|
|
if( (ch==0x002D) ||
|
|
|
|
(0x0030 <= ch && ch <= 0x0039) ||
|
|
|
|
(0x0041 <= ch && ch <= 0x005A) ||
|
|
|
|
(0x0061 <= ch && ch <= 0x007A)
|
|
|
|
){
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
static int32_t
|
|
|
|
_internal_toASCII(const UChar* src, int32_t srcLength,
|
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
|
|
|
UStringPrepProfile* nameprep,
|
|
|
|
UParseError* parseError,
|
2007-07-31 21:22:45 +00:00
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2007-07-31 21:22:45 +00:00
|
|
|
// TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too.
|
2003-02-28 21:35:25 +00:00
|
|
|
UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE];
|
|
|
|
//initialize pointers to stack buffers
|
|
|
|
UChar *b1 = b1Stack, *b2 = b2Stack;
|
2005-05-05 20:59:04 +00:00
|
|
|
int32_t b1Len=0, b2Len,
|
2003-02-28 21:35:25 +00:00
|
|
|
b1Capacity = MAX_LABEL_BUFFER_SIZE,
|
|
|
|
b2Capacity = MAX_LABEL_BUFFER_SIZE ,
|
|
|
|
reqLength=0;
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
UBool* caseFlags = NULL;
|
|
|
|
|
|
|
|
// the source contains all ascii codepoints
|
|
|
|
UBool srcIsASCII = TRUE;
|
|
|
|
// assume the source contains all LDH codepoints
|
|
|
|
UBool srcIsLDH = TRUE;
|
|
|
|
|
|
|
|
int32_t j=0;
|
|
|
|
|
|
|
|
//get the options
|
2003-04-29 18:14:37 +00:00
|
|
|
UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
int32_t failPos = -1;
|
|
|
|
|
2005-05-05 20:59:04 +00:00
|
|
|
if(srcLength == -1){
|
|
|
|
srcLength = u_strlen(src);
|
|
|
|
}
|
2005-07-12 20:33:15 +00:00
|
|
|
|
2007-01-11 00:33:32 +00:00
|
|
|
if(srcLength > b1Capacity){
|
|
|
|
b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
|
|
|
|
if(b1==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
b1Capacity = srcLength;
|
2007-01-05 01:26:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-05 20:59:04 +00:00
|
|
|
// step 1
|
|
|
|
for( j=0;j<srcLength;j++){
|
|
|
|
if(src[j] > 0x7F){
|
|
|
|
srcIsASCII = FALSE;
|
|
|
|
}
|
|
|
|
b1[b1Len++] = src[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 2 is performed only if the source contains non ASCII
|
|
|
|
if(srcIsASCII == FALSE){
|
2003-02-28 21:35:25 +00:00
|
|
|
|
2005-05-05 20:59:04 +00:00
|
|
|
// step 2
|
|
|
|
b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Capacity, namePrepOptions, parseError, status);
|
|
|
|
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
// we do not have enough room so grow the buffer
|
2007-01-11 00:33:32 +00:00
|
|
|
if(b1 != b1Stack){
|
|
|
|
uprv_free(b1);
|
|
|
|
}
|
2005-05-05 20:59:04 +00:00
|
|
|
b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b1==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
|
|
|
|
b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status);
|
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
// error bail out
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2005-07-12 20:33:15 +00:00
|
|
|
if(b1Len == 0){
|
|
|
|
*status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
|
2005-05-05 20:59:04 +00:00
|
|
|
// for step 3 & 4
|
|
|
|
srcIsASCII = TRUE;
|
2003-02-28 21:35:25 +00:00
|
|
|
for( j=0;j<b1Len;j++){
|
2005-05-05 20:59:04 +00:00
|
|
|
// check if output of usprep_prepare is all ASCII
|
2003-02-28 21:35:25 +00:00
|
|
|
if(b1[j] > 0x7F){
|
|
|
|
srcIsASCII = FALSE;
|
2004-04-06 02:05:01 +00:00
|
|
|
}else if(isLDHChar(b1[j])==FALSE){ // if the char is in ASCII range verify that it is an LDH character
|
2003-02-28 21:35:25 +00:00
|
|
|
srcIsLDH = FALSE;
|
|
|
|
failPos = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(useSTD3ASCIIRules == TRUE){
|
|
|
|
// verify 3a and 3b
|
2004-04-06 02:05:01 +00:00
|
|
|
// 3(a) Verify the absence of non-LDH ASCII code points; that is, the
|
|
|
|
// absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
|
|
|
|
// 3(b) Verify the absence of leading and trailing hyphen-minus; that
|
|
|
|
// is, the absence of U+002D at the beginning and end of the
|
|
|
|
// sequence.
|
|
|
|
if( srcIsLDH == FALSE /* source at this point should not contain anyLDH characters */
|
2003-02-28 21:35:25 +00:00
|
|
|
|| b1[0] == HYPHEN || b1[b1Len-1] == HYPHEN){
|
|
|
|
*status = U_IDNA_STD3_ASCII_RULES_ERROR;
|
|
|
|
|
|
|
|
/* populate the parseError struct */
|
|
|
|
if(srcIsLDH==FALSE){
|
2003-05-29 20:31:57 +00:00
|
|
|
// failPos is always set the index of failure
|
2003-05-29 21:46:31 +00:00
|
|
|
uprv_syntaxError(b1,failPos, b1Len,parseError);
|
2003-02-28 21:35:25 +00:00
|
|
|
}else if(b1[0] == HYPHEN){
|
2003-05-29 20:31:57 +00:00
|
|
|
// fail position is 0
|
2003-05-29 21:46:31 +00:00
|
|
|
uprv_syntaxError(b1,0,b1Len,parseError);
|
2003-02-28 21:35:25 +00:00
|
|
|
}else{
|
2003-05-29 20:31:57 +00:00
|
|
|
// the last index in the source is always length-1
|
2003-05-29 21:46:31 +00:00
|
|
|
uprv_syntaxError(b1, (b1Len>0) ? b1Len-1 : b1Len, b1Len,parseError);
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
}
|
2005-05-05 20:59:04 +00:00
|
|
|
// Step 4: if the source is ASCII then proceed to step 8
|
2003-02-28 21:35:25 +00:00
|
|
|
if(srcIsASCII){
|
|
|
|
if(b1Len <= destCapacity){
|
|
|
|
uprv_memmove(dest, b1, b1Len * U_SIZEOF_UCHAR);
|
|
|
|
reqLength = b1Len;
|
|
|
|
}else{
|
|
|
|
reqLength = b1Len;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// step 5 : verify the sequence does not begin with ACE prefix
|
|
|
|
if(!startsWithPrefix(b1,b1Len)){
|
|
|
|
|
|
|
|
//step 6: encode the sequence with punycode
|
2003-05-29 20:31:57 +00:00
|
|
|
|
|
|
|
// do not preserve the case flags for now!
|
|
|
|
// TODO: Preserve the case while implementing the RFE
|
|
|
|
// caseFlags = (UBool*) uprv_malloc(b1Len * sizeof(UBool));
|
|
|
|
// uprv_memset(caseFlags,TRUE,b1Len);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
b2Len = u_strToPunycode(b1,b1Len,b2,b2Capacity,caseFlags, status);
|
|
|
|
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
|
|
|
b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b2 == NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
|
|
|
|
b2Len = u_strToPunycode(b1,b1Len,b2,b2Len,caseFlags, status);
|
|
|
|
}
|
|
|
|
//error bail out
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2003-05-29 20:31:57 +00:00
|
|
|
// TODO : Reconsider while implementing the case preserve RFE
|
|
|
|
// convert all codepoints to lower case ASCII
|
|
|
|
// toASCIILower(b2,b2Len);
|
2003-02-28 21:35:25 +00:00
|
|
|
reqLength = b2Len+ACE_PREFIX_LENGTH;
|
|
|
|
|
|
|
|
if(reqLength > destCapacity){
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
//Step 7: prepend the ACE prefix
|
|
|
|
uprv_memcpy(dest,ACE_PREFIX,ACE_PREFIX_LENGTH * U_SIZEOF_UCHAR);
|
|
|
|
//Step 6: copy the contents in b2 into dest
|
|
|
|
uprv_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len * U_SIZEOF_UCHAR);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
*status = U_IDNA_ACE_PREFIX_ERROR;
|
2003-05-29 20:31:57 +00:00
|
|
|
//position of failure is 0
|
2003-05-29 21:46:31 +00:00
|
|
|
uprv_syntaxError(b1,0,b1Len,parseError);
|
2003-02-28 21:35:25 +00:00
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
}
|
2007-01-05 01:26:08 +00:00
|
|
|
// step 8: verify the length of label
|
2003-02-28 21:35:25 +00:00
|
|
|
if(reqLength > MAX_LABEL_LENGTH){
|
|
|
|
*status = U_IDNA_LABEL_TOO_LONG_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
if(b1 != b1Stack){
|
|
|
|
uprv_free(b1);
|
|
|
|
}
|
|
|
|
if(b2 != b2Stack){
|
|
|
|
uprv_free(b2);
|
|
|
|
}
|
|
|
|
uprv_free(caseFlags);
|
|
|
|
|
|
|
|
return u_terminateUChars(dest, destCapacity, reqLength, status);
|
|
|
|
}
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
static int32_t
|
|
|
|
_internal_toUnicode(const UChar* src, int32_t srcLength,
|
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
|
|
|
UStringPrepProfile* nameprep,
|
|
|
|
UParseError* parseError,
|
2007-07-31 21:13:31 +00:00
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
//get the options
|
2007-07-31 21:13:31 +00:00
|
|
|
//UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
|
2003-07-24 23:23:19 +00:00
|
|
|
int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0;
|
2007-07-31 21:22:45 +00:00
|
|
|
|
|
|
|
// TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too.
|
2003-02-28 21:35:25 +00:00
|
|
|
UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE], b3Stack[MAX_LABEL_BUFFER_SIZE];
|
|
|
|
|
|
|
|
//initialize pointers to stack buffers
|
|
|
|
UChar *b1 = b1Stack, *b2 = b2Stack, *b1Prime=NULL, *b3=b3Stack;
|
2014-09-11 05:25:13 +00:00
|
|
|
int32_t b1Len = 0, b2Len, b1PrimeLen, b3Len,
|
2003-02-28 21:35:25 +00:00
|
|
|
b1Capacity = MAX_LABEL_BUFFER_SIZE,
|
|
|
|
b2Capacity = MAX_LABEL_BUFFER_SIZE,
|
|
|
|
b3Capacity = MAX_LABEL_BUFFER_SIZE,
|
|
|
|
reqLength=0;
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
UBool* caseFlags = NULL;
|
|
|
|
|
|
|
|
UBool srcIsASCII = TRUE;
|
2007-07-31 21:13:31 +00:00
|
|
|
/*UBool srcIsLDH = TRUE;
|
|
|
|
int32_t failPos =0;*/
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
// step 1: find out if all the codepoints in src are ASCII
|
|
|
|
if(srcLength==-1){
|
|
|
|
srcLength = 0;
|
|
|
|
for(;src[srcLength]!=0;){
|
|
|
|
if(src[srcLength]> 0x7f){
|
|
|
|
srcIsASCII = FALSE;
|
2007-07-31 21:13:31 +00:00
|
|
|
}/*else if(isLDHChar(src[srcLength])==FALSE){
|
2004-04-06 02:05:01 +00:00
|
|
|
// here we do not assemble surrogates
|
|
|
|
// since we know that LDH code points
|
|
|
|
// are in the ASCII range only
|
2003-05-30 18:20:03 +00:00
|
|
|
srcIsLDH = FALSE;
|
|
|
|
failPos = srcLength;
|
2007-07-31 21:13:31 +00:00
|
|
|
}*/
|
2003-02-28 21:35:25 +00:00
|
|
|
srcLength++;
|
|
|
|
}
|
2004-05-25 23:36:03 +00:00
|
|
|
}else if(srcLength > 0){
|
2003-02-28 21:35:25 +00:00
|
|
|
for(int32_t j=0; j<srcLength; j++){
|
|
|
|
if(src[j]> 0x7f){
|
|
|
|
srcIsASCII = FALSE;
|
2007-07-31 21:13:31 +00:00
|
|
|
}/*else if(isLDHChar(src[j])==FALSE){
|
2004-04-06 02:05:01 +00:00
|
|
|
// here we do not assemble surrogates
|
|
|
|
// since we know that LDH code points
|
|
|
|
// are in the ASCII range only
|
2003-05-30 18:20:03 +00:00
|
|
|
srcIsLDH = FALSE;
|
|
|
|
failPos = j;
|
2007-07-31 21:13:31 +00:00
|
|
|
}*/
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2004-05-25 23:36:03 +00:00
|
|
|
}else{
|
|
|
|
return 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2007-01-05 01:26:08 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
if(srcIsASCII == FALSE){
|
|
|
|
// step 2: process the string
|
2003-07-24 23:23:19 +00:00
|
|
|
b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Capacity, namePrepOptions, parseError, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
|
|
|
b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b1==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
//bail out on error
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
|
|
|
//just point src to b1
|
|
|
|
b1 = (UChar*) src;
|
|
|
|
b1Len = srcLength;
|
|
|
|
}
|
2003-05-30 18:20:03 +00:00
|
|
|
|
2007-07-31 21:13:31 +00:00
|
|
|
// The RFC states that
|
|
|
|
// <quote>
|
|
|
|
// ToUnicode never fails. If any step fails, then the original input
|
|
|
|
// is returned immediately in that step.
|
|
|
|
// </quote>
|
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
//step 3: verify ACE Prefix
|
2007-03-17 01:22:52 +00:00
|
|
|
if(startsWithPrefix(b1,b1Len)){
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
//step 4: Remove the ACE Prefix
|
|
|
|
b1Prime = b1 + ACE_PREFIX_LENGTH;
|
|
|
|
b1PrimeLen = b1Len - ACE_PREFIX_LENGTH;
|
|
|
|
|
|
|
|
//step 5: Decode using punycode
|
|
|
|
b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Capacity, caseFlags,status);
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
|
|
|
b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b2==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Len, caseFlags, status);
|
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
|
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
//step 6:Apply toASCII
|
2007-07-31 21:13:31 +00:00
|
|
|
b3Len = uidna_toASCII(b2, b2Len, b3, b3Capacity, options, parseError, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
|
|
|
b3 = (UChar*) uprv_malloc(b3Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b3==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
b3Len = uidna_toASCII(b2,b2Len,b3,b3Len,options,parseError, status);
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
//bail out on error
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
//step 7: verify
|
|
|
|
if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){
|
2007-07-31 21:13:31 +00:00
|
|
|
// Cause the original to be returned.
|
|
|
|
*status = U_IDNA_VERIFICATION_ERROR;
|
2003-02-28 21:35:25 +00:00
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
//step 8: return output of step 5
|
|
|
|
reqLength = b2Len;
|
|
|
|
if(b2Len <= destCapacity) {
|
|
|
|
uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR);
|
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
// See the start of this if statement for why this is commented out.
|
2003-05-30 18:20:03 +00:00
|
|
|
// verify that STD3 ASCII rules are satisfied
|
2007-07-31 21:13:31 +00:00
|
|
|
/*if(useSTD3ASCIIRules == TRUE){
|
|
|
|
if( srcIsLDH == FALSE // source contains some non-LDH characters
|
2003-05-30 18:20:03 +00:00
|
|
|
|| src[0] == HYPHEN || src[srcLength-1] == HYPHEN){
|
|
|
|
*status = U_IDNA_STD3_ASCII_RULES_ERROR;
|
|
|
|
|
2007-07-31 21:13:31 +00:00
|
|
|
// populate the parseError struct
|
2003-05-30 18:20:03 +00:00
|
|
|
if(srcIsLDH==FALSE){
|
|
|
|
// failPos is always set the index of failure
|
|
|
|
uprv_syntaxError(src,failPos, srcLength,parseError);
|
|
|
|
}else if(src[0] == HYPHEN){
|
|
|
|
// fail position is 0
|
|
|
|
uprv_syntaxError(src,0,srcLength,parseError);
|
|
|
|
}else{
|
|
|
|
// the last index in the source is always length-1
|
|
|
|
uprv_syntaxError(src, (srcLength>0) ? srcLength-1 : srcLength, srcLength,parseError);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
}*/
|
|
|
|
// just return the source
|
2003-02-28 21:35:25 +00:00
|
|
|
//copy the source to destination
|
|
|
|
if(srcLength <= destCapacity){
|
|
|
|
uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
reqLength = srcLength;
|
|
|
|
}
|
|
|
|
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
CLEANUP:
|
|
|
|
|
|
|
|
if(b1 != b1Stack && b1!=src){
|
|
|
|
uprv_free(b1);
|
|
|
|
}
|
|
|
|
if(b2 != b2Stack){
|
|
|
|
uprv_free(b2);
|
|
|
|
}
|
|
|
|
uprv_free(caseFlags);
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-05-30 18:20:03 +00:00
|
|
|
// The RFC states that
|
|
|
|
// <quote>
|
|
|
|
// ToUnicode never fails. If any step fails, then the original input
|
|
|
|
// is returned immediately in that step.
|
|
|
|
// </quote>
|
|
|
|
// So if any step fails lets copy source to destination
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
//copy the source to destination
|
2003-06-06 05:05:22 +00:00
|
|
|
if(dest && srcLength <= destCapacity){
|
2007-07-31 21:13:31 +00:00
|
|
|
// srcLength should have already been set earlier.
|
|
|
|
U_ASSERT(srcLength >= 0);
|
2003-05-30 18:20:03 +00:00
|
|
|
uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
reqLength = srcLength;
|
2007-03-17 01:22:52 +00:00
|
|
|
*status = U_ZERO_ERROR;
|
2003-05-30 18:20:03 +00:00
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
return u_terminateUChars(dest, destCapacity, reqLength, status);
|
|
|
|
}
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uidna_toASCII(const UChar* src, int32_t srcLength,
|
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
|
|
|
UParseError* parseError,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-05-29 20:31:57 +00:00
|
|
|
|
2009-02-02 16:16:07 +00:00
|
|
|
UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t retLen = _internal_toASCII(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
|
|
|
|
|
|
|
|
/* close the profile*/
|
|
|
|
usprep_close(nameprep);
|
|
|
|
|
|
|
|
return retLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uidna_toUnicode(const UChar* src, int32_t srcLength,
|
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
|
|
|
UParseError* parseError,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( (src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-01-05 01:26:08 +00:00
|
|
|
|
2009-02-02 16:16:07 +00:00
|
|
|
UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return -1;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
int32_t retLen = _internal_toUnicode(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
|
|
|
|
|
|
|
|
usprep_close(nameprep);
|
|
|
|
|
|
|
|
return retLen;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2003-03-19 02:03:12 +00:00
|
|
|
uidna_IDNToASCII( const UChar *src, int32_t srcLength,
|
2003-02-28 21:35:25 +00:00
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
2003-03-19 02:03:12 +00:00
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status){
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-09 22:12:52 +00:00
|
|
|
if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
2003-02-28 21:35:25 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t reqLength = 0;
|
|
|
|
|
2009-02-02 16:16:07 +00:00
|
|
|
UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-19 02:03:12 +00:00
|
|
|
//initialize pointers
|
|
|
|
UChar *delimiter = (UChar*)src;
|
|
|
|
UChar *labelStart = (UChar*)src;
|
|
|
|
UChar *currentDest = (UChar*) dest;
|
2003-02-28 21:35:25 +00:00
|
|
|
int32_t remainingLen = srcLength;
|
2003-03-19 02:03:12 +00:00
|
|
|
int32_t remainingDestCapacity = destCapacity;
|
|
|
|
int32_t labelLen = 0, labelReqLength = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
UBool done = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
|
2003-12-02 03:03:23 +00:00
|
|
|
labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
|
2005-07-12 20:33:15 +00:00
|
|
|
labelReqLength = 0;
|
|
|
|
if(!(labelLen==0 && done)){// make sure this is not a root label separator.
|
2003-02-28 21:35:25 +00:00
|
|
|
|
2005-07-12 20:33:15 +00:00
|
|
|
labelReqLength = _internal_toASCII( labelStart, labelLen,
|
|
|
|
currentDest, remainingDestCapacity,
|
|
|
|
options, nameprep,
|
|
|
|
parseError, status);
|
|
|
|
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
remainingDestCapacity = 0;
|
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2003-03-19 02:03:12 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
break;
|
|
|
|
}
|
2003-03-19 02:03:12 +00:00
|
|
|
|
|
|
|
reqLength +=labelReqLength;
|
|
|
|
// adjust the destination pointer
|
|
|
|
if(labelReqLength < remainingDestCapacity){
|
|
|
|
currentDest = currentDest + labelReqLength;
|
|
|
|
remainingDestCapacity -= labelReqLength;
|
|
|
|
}else{
|
|
|
|
// should never occur
|
|
|
|
remainingDestCapacity = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2003-05-29 20:31:57 +00:00
|
|
|
if(done == TRUE){
|
|
|
|
break;
|
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
// add the label separator
|
2003-05-29 20:31:57 +00:00
|
|
|
if(remainingDestCapacity > 0){
|
|
|
|
*currentDest++ = FULL_STOP;
|
|
|
|
remainingDestCapacity--;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
reqLength++;
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
labelStart = delimiter;
|
2003-03-12 23:17:56 +00:00
|
|
|
if(remainingLen >0 ){
|
2004-12-08 01:05:40 +00:00
|
|
|
remainingLen = (int32_t)(srcLength - (delimiter - src));
|
2003-03-12 23:17:56 +00:00
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2007-01-05 01:26:08 +00:00
|
|
|
if(reqLength > MAX_DOMAIN_NAME_LENGTH){
|
|
|
|
*status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
usprep_close(nameprep);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
return u_terminateUChars(dest, destCapacity, reqLength, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uidna_IDNToUnicode( const UChar* src, int32_t srcLength,
|
|
|
|
UChar* dest, int32_t destCapacity,
|
|
|
|
int32_t options,
|
|
|
|
UParseError* parseError,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-09 22:12:52 +00:00
|
|
|
if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
2003-02-28 21:35:25 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t reqLength = 0;
|
|
|
|
|
2009-02-02 16:16:07 +00:00
|
|
|
UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-19 02:03:12 +00:00
|
|
|
//initialize pointers
|
|
|
|
UChar *delimiter = (UChar*)src;
|
|
|
|
UChar *labelStart = (UChar*)src;
|
|
|
|
UChar *currentDest = (UChar*) dest;
|
2003-02-28 21:35:25 +00:00
|
|
|
int32_t remainingLen = srcLength;
|
2003-03-19 02:03:12 +00:00
|
|
|
int32_t remainingDestCapacity = destCapacity;
|
|
|
|
int32_t labelLen = 0, labelReqLength = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
UBool done = FALSE;
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
|
2003-12-02 03:03:23 +00:00
|
|
|
labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
|
2003-03-19 02:03:12 +00:00
|
|
|
|
2007-07-31 21:13:31 +00:00
|
|
|
// The RFC states that
|
|
|
|
// <quote>
|
|
|
|
// ToUnicode never fails. If any step fails, then the original input
|
|
|
|
// is returned immediately in that step.
|
|
|
|
// </quote>
|
|
|
|
// _internal_toUnicode will copy the label.
|
|
|
|
/*if(labelLen==0 && done==FALSE){
|
2005-07-12 20:33:15 +00:00
|
|
|
*status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
|
2007-07-31 21:13:31 +00:00
|
|
|
break;
|
|
|
|
}*/
|
2007-01-05 01:26:08 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
labelReqLength = _internal_toUnicode(labelStart, labelLen,
|
|
|
|
currentDest, remainingDestCapacity,
|
|
|
|
options, nameprep,
|
|
|
|
parseError, status);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
2003-03-19 02:03:12 +00:00
|
|
|
remainingDestCapacity = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2003-03-19 02:03:12 +00:00
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
break;
|
|
|
|
}
|
2003-03-19 02:03:12 +00:00
|
|
|
|
|
|
|
reqLength +=labelReqLength;
|
|
|
|
// adjust the destination pointer
|
|
|
|
if(labelReqLength < remainingDestCapacity){
|
|
|
|
currentDest = currentDest + labelReqLength;
|
|
|
|
remainingDestCapacity -= labelReqLength;
|
|
|
|
}else{
|
|
|
|
// should never occur
|
|
|
|
remainingDestCapacity = 0;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
2003-05-29 20:31:57 +00:00
|
|
|
if(done == TRUE){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-02-28 21:35:25 +00:00
|
|
|
// add the label separator
|
2007-07-31 21:13:31 +00:00
|
|
|
// Unlike the ToASCII operation we don't normalize the label separators
|
2003-05-29 20:31:57 +00:00
|
|
|
if(remainingDestCapacity > 0){
|
2007-07-31 21:13:31 +00:00
|
|
|
*currentDest++ = *(labelStart + labelLen);
|
2003-05-29 20:31:57 +00:00
|
|
|
remainingDestCapacity--;
|
2003-02-28 21:35:25 +00:00
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
reqLength++;
|
2003-02-28 21:35:25 +00:00
|
|
|
|
2003-03-19 02:03:12 +00:00
|
|
|
labelStart = delimiter;
|
2003-03-12 23:17:56 +00:00
|
|
|
if(remainingLen >0 ){
|
2004-12-08 01:05:40 +00:00
|
|
|
remainingLen = (int32_t)(srcLength - (delimiter - src));
|
2003-03-12 23:17:56 +00:00
|
|
|
}
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
}
|
2007-07-31 21:13:31 +00:00
|
|
|
|
2007-01-05 01:26:08 +00:00
|
|
|
if(reqLength > MAX_DOMAIN_NAME_LENGTH){
|
|
|
|
*status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
usprep_close(nameprep);
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
return u_terminateUChars(dest, destCapacity, reqLength, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uidna_compare( const UChar *s1, int32_t length1,
|
|
|
|
const UChar *s2, int32_t length2,
|
|
|
|
int32_t options,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar b1Stack[MAX_IDN_BUFFER_SIZE], b2Stack[MAX_IDN_BUFFER_SIZE];
|
|
|
|
UChar *b1 = b1Stack, *b2 = b2Stack;
|
|
|
|
int32_t b1Len, b2Len, b1Capacity = MAX_IDN_BUFFER_SIZE, b2Capacity = MAX_IDN_BUFFER_SIZE;
|
2003-05-29 20:31:57 +00:00
|
|
|
int32_t result=-1;
|
2003-02-28 21:35:25 +00:00
|
|
|
|
|
|
|
UParseError parseError;
|
|
|
|
|
|
|
|
b1Len = uidna_IDNToASCII(s1, length1, b1, b1Capacity, options, &parseError, status);
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b1==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
|
|
|
|
b1Len = uidna_IDNToASCII(s1,length1,b1,b1Len, options, &parseError, status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
b2Len = uidna_IDNToASCII(s2,length2, b2,b2Capacity, options, &parseError, status);
|
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
// redo processing of string
|
|
|
|
b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
|
|
|
|
if(b2==NULL){
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
*status = U_ZERO_ERROR; // reset error
|
|
|
|
|
|
|
|
b2Len = uidna_IDNToASCII(s2, length2, b2, b2Len, options, &parseError, status);
|
|
|
|
|
|
|
|
}
|
|
|
|
// when toASCII is applied all label separators are replaced with FULL_STOP
|
|
|
|
result = compareCaseInsensitiveASCII(b1,b1Len,b2,b2Len);
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
if(b1 != b1Stack){
|
|
|
|
uprv_free(b1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(b2 != b2Stack){
|
|
|
|
uprv_free(b2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-05-06 01:37:52 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_IDNA */
|