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; int32_t result=-1; 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; }
U_CFUNC int32_t idnaref_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; } 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; int32_t b1Len, b2Len, b1PrimeLen, b3Len, b1Capacity = MAX_LABEL_BUFFER_SIZE, b2Capacity = MAX_LABEL_BUFFER_SIZE, b3Capacity = MAX_LABEL_BUFFER_SIZE, reqLength=0; // UParseError parseError; NamePrepTransform* prep = TestIDNA::getInstance(*status); b1Len = 0; UBool* caseFlags = NULL; //get the options UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0); UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0); UBool srcIsASCII = TRUE; UBool srcIsLDH = TRUE; int32_t failPos =0; if(U_FAILURE(*status)){ goto CLEANUP; } // 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; }if(prep->isLDHChar(src[srcLength])==FALSE){ // here we do not assemble surrogates // since we know that LDH code points // are in the ASCII range only srcIsLDH = FALSE; failPos = srcLength; } srcLength++; } }else{ for(int32_t j=0; j<srcLength; j++){ if(src[j]> 0x7f){ srcIsASCII = FALSE; }else if(prep->isLDHChar(src[j])==FALSE){ // here we do not assemble surrogates // since we know that LDH code points // are in the ASCII range only srcIsLDH = FALSE; failPos = j; } } } if(srcIsASCII == FALSE){ // step 2: process the string b1Len = prep->process(src,srcLength,b1,b1Capacity,allowUnassigned, parseError, *status); 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 b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status); } //bail out on error if(U_FAILURE(*status)){ goto CLEANUP; } }else{ // copy everything to b1 if(srcLength < b1Capacity){ uprv_memmove(b1,src, srcLength * U_SIZEOF_UCHAR); }else{ /* we do not have enough room so grow the buffer*/ b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR); if(b1==NULL){ *status = U_MEMORY_ALLOCATION_ERROR; goto CLEANUP; } uprv_memmove(b1,src, srcLength * U_SIZEOF_UCHAR); } b1Len = srcLength; } //step 3: verify ACE Prefix if(startsWithPrefix(src,srcLength)){ //step 4: Remove the ACE Prefix b1Prime = b1 + ACE_PREFIX_LENGTH; b1PrimeLen = b1Len - ACE_PREFIX_LENGTH; //step 5: Decode using punycode b2Len = convertFromPuny(b1Prime,b1PrimeLen, b2, b2Capacity, *status); //b2Len = u_strFromPunycode(b2, b2Capacity,b1Prime,b1PrimeLen, 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 = convertFromPuny(b1Prime,b1PrimeLen, b2, b2Len, *status); //b2Len = u_strFromPunycode(b2, b2Len,b1Prime,b1PrimeLen,caseFlags, status); } //step 6:Apply toASCII b3Len = idnaref_toASCII(b2,b2Len,b3,b3Capacity,options,parseError, status); 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 b3Len = idnaref_toASCII(b2,b2Len,b3,b3Len, options, parseError, status); } //bail out on error if(U_FAILURE(*status)){ goto CLEANUP; } //step 7: verify if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){ *status = U_IDNA_VERIFICATION_ERROR; goto CLEANUP; } //step 8: return output of step 5 reqLength = b2Len; if(b2Len <= destCapacity) { uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR); } }else{ // verify that STD3 ASCII rules are satisfied if(useSTD3ASCIIRules == TRUE){ if( srcIsLDH == FALSE /* source contains some non-LDH characters */ || src[0] == HYPHEN || src[srcLength-1] == HYPHEN){ *status = U_IDNA_STD3_ASCII_RULES_ERROR; /* populate the parseError struct */ 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; } } //copy the source to destination if(srcLength <= destCapacity){ uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR); } reqLength = srcLength; } CLEANUP: if(b1 != b1Stack){ uprv_free(b1); } if(b2 != b2Stack){ uprv_free(b2); } uprv_free(caseFlags); // delete prep; return u_terminateUChars(dest, destCapacity, reqLength, status); }
static int32_t _internal_toUnicode(const UChar* src, int32_t srcLength, UChar* dest, int32_t destCapacity, int32_t options, UStringPrepProfile* nameprep, UParseError* parseError, UErrorCode* status) { //get the options //UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0); int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0; // TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too. 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; int32_t b1Len, b2Len, b1PrimeLen, b3Len, b1Capacity = MAX_LABEL_BUFFER_SIZE, b2Capacity = MAX_LABEL_BUFFER_SIZE, b3Capacity = MAX_LABEL_BUFFER_SIZE, reqLength=0; b1Len = 0; UBool* caseFlags = NULL; UBool srcIsASCII = TRUE; /*UBool srcIsLDH = TRUE; int32_t failPos =0;*/ // 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; }/*else if(isLDHChar(src[srcLength])==FALSE){ // here we do not assemble surrogates // since we know that LDH code points // are in the ASCII range only srcIsLDH = FALSE; failPos = srcLength; }*/ srcLength++; } }else if(srcLength > 0){ for(int32_t j=0; j<srcLength; j++){ if(src[j]> 0x7f){ srcIsASCII = FALSE; }/*else if(isLDHChar(src[j])==FALSE){ // here we do not assemble surrogates // since we know that LDH code points // are in the ASCII range only srcIsLDH = FALSE; failPos = j; }*/ } }else{ return 0; } if(srcIsASCII == FALSE){ // step 2: process the string 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*/ 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); } //bail out on error if(U_FAILURE(*status)){ goto CLEANUP; } }else{ //just point src to b1 b1 = (UChar*) src; b1Len = srcLength; } // The RFC states that // <quote> // ToUnicode never fails. If any step fails, then the original input // is returned immediately in that step. // </quote> //step 3: verify ACE Prefix if(startsWithPrefix(b1,b1Len)){ //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); 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_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Len, caseFlags, status); } //step 6:Apply toASCII b3Len = uidna_toASCII(b2, b2Len, b3, b3Capacity, options, parseError, status); 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 b3Len = uidna_toASCII(b2,b2Len,b3,b3Len,options,parseError, status); } //bail out on error if(U_FAILURE(*status)){ goto CLEANUP; } //step 7: verify if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){ // Cause the original to be returned. *status = U_IDNA_VERIFICATION_ERROR; goto CLEANUP; } //step 8: return output of step 5 reqLength = b2Len; if(b2Len <= destCapacity) { uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR); } } else{ // See the start of this if statement for why this is commented out. // verify that STD3 ASCII rules are satisfied /*if(useSTD3ASCIIRules == TRUE){ if( srcIsLDH == FALSE // source contains some non-LDH characters || src[0] == HYPHEN || src[srcLength-1] == HYPHEN){ *status = U_IDNA_STD3_ASCII_RULES_ERROR; // populate the parseError struct 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; } }*/ // just return the source //copy the source to destination if(srcLength <= destCapacity){ uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR); } reqLength = srcLength; } CLEANUP: if(b1 != b1Stack && b1!=src){ uprv_free(b1); } if(b2 != b2Stack){ uprv_free(b2); } uprv_free(caseFlags); // 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 if(dest && srcLength <= destCapacity){ // srcLength should have already been set earlier. U_ASSERT(srcLength >= 0); uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR); } reqLength = srcLength; *status = U_ZERO_ERROR; } return u_terminateUChars(dest, destCapacity, reqLength, status); }