michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2002-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: punycode.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2002jan31 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: michael@0: /* This ICU code derived from: */ michael@0: /* michael@0: punycode.c 0.4.0 (2001-Nov-17-Sat) michael@0: http://www.cs.berkeley.edu/~amc/idn/ michael@0: Adam M. Costello michael@0: http://www.nicemice.net/amc/ michael@0: michael@0: Disclaimer and license michael@0: michael@0: Regarding this entire document or any portion of it (including michael@0: the pseudocode and C code), the author makes no guarantees and michael@0: is not responsible for any damage resulting from its use. The michael@0: author grants irrevocable permission to anyone to use, modify, michael@0: and distribute it in any way that does not diminish the rights michael@0: of anyone else to use, modify, and distribute it, provided that michael@0: redistributed derivative works do not contain misleading author or michael@0: version information. Derivative works need not be licensed under michael@0: similar terms. michael@0: */ michael@0: /* michael@0: * ICU modifications: michael@0: * - ICU data types and coding conventions michael@0: * - ICU string buffer handling with implicit source lengths michael@0: * and destination preflighting michael@0: * - UTF-16 handling michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_IDNA michael@0: michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ustr_imp.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: #include "punycode.h" michael@0: #include "uassert.h" michael@0: michael@0: michael@0: /* Punycode ----------------------------------------------------------------- */ michael@0: michael@0: /* Punycode parameters for Bootstring */ michael@0: #define BASE 36 michael@0: #define TMIN 1 michael@0: #define TMAX 26 michael@0: #define SKEW 38 michael@0: #define DAMP 700 michael@0: #define INITIAL_BIAS 72 michael@0: #define INITIAL_N 0x80 michael@0: michael@0: /* "Basic" Unicode/ASCII code points */ michael@0: #define _HYPHEN 0X2d michael@0: #define DELIMITER _HYPHEN michael@0: michael@0: #define _ZERO_ 0X30 michael@0: #define _NINE 0x39 michael@0: michael@0: #define _SMALL_A 0X61 michael@0: #define _SMALL_Z 0X7a michael@0: michael@0: #define _CAPITAL_A 0X41 michael@0: #define _CAPITAL_Z 0X5a michael@0: michael@0: #define IS_BASIC(c) ((c)<0x80) michael@0: #define IS_BASIC_UPPERCASE(c) (_CAPITAL_A<=(c) && (c)<=_CAPITAL_Z) michael@0: michael@0: /** michael@0: * digitToBasic() returns the basic code point whose value michael@0: * (when used for representing integers) is d, which must be in the michael@0: * range 0 to BASE-1. The lowercase form is used unless the uppercase flag is michael@0: * nonzero, in which case the uppercase form is used. michael@0: */ michael@0: static inline char michael@0: digitToBasic(int32_t digit, UBool uppercase) { michael@0: /* 0..25 map to ASCII a..z or A..Z */ michael@0: /* 26..35 map to ASCII 0..9 */ michael@0: if(digit<26) { michael@0: if(uppercase) { michael@0: return (char)(_CAPITAL_A+digit); michael@0: } else { michael@0: return (char)(_SMALL_A+digit); michael@0: } michael@0: } else { michael@0: return (char)((_ZERO_-26)+digit); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * basicToDigit[] contains the numeric value of a basic code michael@0: * point (for use in representing integers) in the range 0 to michael@0: * BASE-1, or -1 if b is does not represent a value. michael@0: */ michael@0: static const int8_t michael@0: basicToDigit[256]={ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1, michael@0: michael@0: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, michael@0: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, michael@0: michael@0: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, michael@0: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, michael@0: michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 michael@0: }; michael@0: michael@0: static inline char michael@0: asciiCaseMap(char b, UBool uppercase) { michael@0: if(uppercase) { michael@0: if(_SMALL_A<=b && b<=_SMALL_Z) { michael@0: b-=(_SMALL_A-_CAPITAL_A); michael@0: } michael@0: } else { michael@0: if(_CAPITAL_A<=b && b<=_CAPITAL_Z) { michael@0: b+=(_SMALL_A-_CAPITAL_A); michael@0: } michael@0: } michael@0: return b; michael@0: } michael@0: michael@0: /* Punycode-specific Bootstring code ---------------------------------------- */ michael@0: michael@0: /* michael@0: * The following code omits the {parts} of the pseudo-algorithm in the spec michael@0: * that are not used with the Punycode parameter set. michael@0: */ michael@0: michael@0: /* Bias adaptation function. */ michael@0: static int32_t michael@0: adaptBias(int32_t delta, int32_t length, UBool firstTime) { michael@0: int32_t count; michael@0: michael@0: if(firstTime) { michael@0: delta/=DAMP; michael@0: } else { michael@0: delta/=2; michael@0: } michael@0: michael@0: delta+=delta/length; michael@0: for(count=0; delta>((BASE-TMIN)*TMAX)/2; count+=BASE) { michael@0: delta/=(BASE-TMIN); michael@0: } michael@0: michael@0: return count+(((BASE-TMIN+1)*delta)/(delta+SKEW)); michael@0: } michael@0: michael@0: #define MAX_CP_COUNT 200 michael@0: michael@0: U_CFUNC int32_t michael@0: u_strToPunycode(const UChar *src, int32_t srcLength, michael@0: UChar *dest, int32_t destCapacity, michael@0: const UBool *caseFlags, michael@0: UErrorCode *pErrorCode) { michael@0: michael@0: int32_t cpBuffer[MAX_CP_COUNT]; michael@0: int32_t n, delta, handledCPCount, basicLength, destLength, bias, j, m, q, k, t, srcCPCount; michael@0: UChar c, c2; michael@0: michael@0: /* argument checking */ michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(src==NULL || srcLength<-1 || (dest==NULL && destCapacity!=0)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * Handle the basic code points and michael@0: * convert extended ones to UTF-32 in cpBuffer (caseFlag in sign bit): michael@0: */ michael@0: srcCPCount=destLength=0; michael@0: if(srcLength==-1) { michael@0: /* NUL-terminated input */ michael@0: for(j=0; /* no condition */; ++j) { michael@0: if((c=src[j])==0) { michael@0: break; michael@0: } michael@0: if(srcCPCount==MAX_CP_COUNT) { michael@0: /* too many input code points */ michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: if(IS_BASIC(c)) { michael@0: cpBuffer[srcCPCount++]=0; michael@0: if(destLength0) { michael@0: if(destLength state to , but guard against overflow: michael@0: */ michael@0: if(m-n>(0x7fffffff-MAX_CP_COUNT-delta)/(handledCPCount+1)) { michael@0: *pErrorCode=U_INTERNAL_PROGRAM_ERROR; michael@0: return 0; michael@0: } michael@0: delta+=(m-n)*(handledCPCount+1); michael@0: n=m; michael@0: michael@0: /* Encode a sequence of same code points n */ michael@0: for(j=0; jTMAX) { michael@0: t=TMAX; michael@0: } michael@0: */ michael@0: michael@0: t=k-bias; michael@0: if(t=(bias+TMAX)) { michael@0: t=TMAX; michael@0: } michael@0: michael@0: if(q0;) { michael@0: if(src[--j]==DELIMITER) { michael@0: break; michael@0: } michael@0: } michael@0: destLength=basicLength=destCPCount=j; michael@0: U_ASSERT(destLength>=0); michael@0: michael@0: while(j>0) { michael@0: b=src[--j]; michael@0: if(!IS_BASIC(b)) { michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: michael@0: if(j0 ? basicLength+1 : 0; in=srcLength) { michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: michael@0: digit=basicToDigit[(uint8_t)src[in++]]; michael@0: if(digit<0) { michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: if(digit>(0x7fffffff-i)/w) { michael@0: /* integer overflow */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: michael@0: i+=digit*w; michael@0: /** RAM: comment out the old code for conformance with draft-ietf-idn-punycode-03.txt michael@0: t=k-bias; michael@0: if(tTMAX) { michael@0: t=TMAX; michael@0: } michael@0: */ michael@0: t=k-bias; michael@0: if(t=(bias+TMAX)) { michael@0: t=TMAX; michael@0: } michael@0: if(digit0x7fffffff/(BASE-t)) { michael@0: /* integer overflow */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: w*=BASE-t; michael@0: } michael@0: michael@0: /* michael@0: * Modification from sample code: michael@0: * Increments destCPCount here, michael@0: * where needed instead of in for() loop tail. michael@0: */ michael@0: ++destCPCount; michael@0: bias=adaptBias(i-oldi, destCPCount, (UBool)(oldi==0)); michael@0: michael@0: /* michael@0: * i was supposed to wrap around from (incremented) destCPCount to 0, michael@0: * incrementing n each time, so we'll fix that now: michael@0: */ michael@0: if(i/destCPCount>(0x7fffffff-n)) { michael@0: /* integer overflow */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: michael@0: n+=i/destCPCount; michael@0: i%=destCPCount; michael@0: /* not needed for Punycode: */ michael@0: /* if (decode_digit(n) <= BASE) return punycode_invalid_input; */ michael@0: michael@0: if(n>0x10ffff || U_IS_SURROGATE(n)) { michael@0: /* Unicode code point overflow */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return 0; michael@0: } michael@0: michael@0: /* Insert n at position i of the output: */ michael@0: cpLength=U16_LENGTH(n); michael@0: if(dest!=NULL && ((destLength+cpLength)<=destCapacity)) { michael@0: int32_t codeUnitIndex; michael@0: michael@0: /* michael@0: * Handle indexes when supplementary code points are present. michael@0: * michael@0: * In almost all cases, there will be only BMP code points before i michael@0: * and even in the entire string. michael@0: * This is handled with the same efficiency as with UTF-32. michael@0: * michael@0: * Only the rare cases with supplementary code points are handled michael@0: * more slowly - but not too bad since this is an insertion anyway. michael@0: */ michael@0: if(i<=firstSupplementaryIndex) { michael@0: codeUnitIndex=i; michael@0: if(cpLength>1) { michael@0: firstSupplementaryIndex=codeUnitIndex; michael@0: } else { michael@0: ++firstSupplementaryIndex; michael@0: } michael@0: } else { michael@0: codeUnitIndex=firstSupplementaryIndex; michael@0: U16_FWD_N(dest, codeUnitIndex, destLength, i-codeUnitIndex); michael@0: } michael@0: michael@0: /* use the UChar index codeUnitIndex instead of the code point index i */ michael@0: if(codeUnitIndex=0); michael@0: ++i; michael@0: } michael@0: michael@0: return u_terminateUChars(dest, destCapacity, destLength, pErrorCode); michael@0: } michael@0: michael@0: /* ### check notes on overflow handling - only necessary if not IDNA? are these Punycode functions to be public? */ michael@0: michael@0: #endif /* #if !UCONFIG_NO_IDNA */