KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > Punycode


1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.text;
8
9 import com.ibm.icu.lang.UCharacter;
10
11 /**
12  * Ported code from ICU punycode.c
13  * @author ram
14  */

15
16 /* Package Private class */
17 final class Punycode {
18
19     /* Punycode parameters for Bootstring */
20     private static final int BASE = 36;
21     private static final int TMIN = 1;
22     private static final int TMAX = 26;
23     private static final int SKEW = 38;
24     private static final int DAMP = 700;
25     private static final int INITIAL_BIAS = 72;
26     private static final int INITIAL_N = 0x80;
27     
28     /* "Basic" Unicode/ASCII code points */
29     private static final int HYPHEN = 0x2d;
30     private static final int DELIMITER = HYPHEN;
31     
32     private static final int ZERO = 0x30;
33     private static final int NINE = 0x39;
34     
35     private static final int SMALL_A = 0x61;
36     private static final int SMALL_Z = 0x7a;
37     
38     private static final int CAPITAL_A = 0x41;
39     private static final int CAPITAL_Z = 0x5a;
40     private static final int MAX_CP_COUNT = 200;
41     private static final int UINT_MAGIC = 0x80000000;
42     private static final long ULONG_MAGIC = 0x8000000000000000L;
43     
44     private static int adaptBias(int delta, int length, boolean firstTime){
45         if(firstTime){
46             delta /=DAMP;
47         }else{
48             delta /= 2;
49         }
50         delta += delta/length;
51
52         int count=0;
53         for(; delta>((BASE-TMIN)*TMAX)/2; count+=BASE) {
54             delta/=(BASE-TMIN);
55         }
56
57         return count+(((BASE-TMIN+1)*delta)/(delta+SKEW));
58     }
59
60     /**
61      * basicToDigit[] contains the numeric value of a basic code
62      * point (for use in representing integers) in the range 0 to
63      * BASE-1, or -1 if b is does not represent a value.
64      */

65     static final int[] basicToDigit= new int[]{
66         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68     
69         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70         26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
71     
72         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
73         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
74     
75         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
76         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
77     
78         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80     
81         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
82         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
83     
84         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
86     
87         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
88         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
89     };
90
91     ///CLOVER:OFF
92
private static char asciiCaseMap(char b, boolean uppercase) {
93         if(uppercase) {
94             if(SMALL_A<=b && b<=SMALL_Z) {
95                 b-=(SMALL_A-CAPITAL_A);
96             }
97         } else {
98             if(CAPITAL_A<=b && b<=CAPITAL_Z) {
99                 b+=(SMALL_A-CAPITAL_A);
100             }
101         }
102         return b;
103     }
104     ///CLOVER:ON
105
/**
106      * digitToBasic() returns the basic code point whose value
107      * (when used for representing integers) is d, which must be in the
108      * range 0 to BASE-1. The lowercase form is used unless the uppercase flag is
109      * nonzero, in which case the uppercase form is used.
110      */

111     private static char digitToBasic(int digit, boolean uppercase) {
112         /* 0..25 map to ASCII a..z or A..Z */
113         /* 26..35 map to ASCII 0..9 */
114         if(digit<26) {
115             if(uppercase) {
116                 return (char)(CAPITAL_A+digit);
117             } else {
118                 return (char)(SMALL_A+digit);
119             }
120         } else {
121             return (char)((ZERO-26)+digit);
122         }
123     }
124     /**
125      * Converts Unicode to Punycode.
126      * The input string must not contain single, unpaired surrogates.
127      * The output will be represented as an array of ASCII code points.
128      *
129      * @param src
130      * @param caseFlags
131      * @return
132      * @throws ParseException
133      */

134     public static StringBuffer JavaDoc encode(StringBuffer JavaDoc src, boolean[] caseFlags) throws StringPrepParseException{
135         
136         int[] cpBuffer = new int[MAX_CP_COUNT];
137         int n, delta, handledCPCount, basicLength, destLength, bias, j, m, q, k, t, srcCPCount;
138         char c, c2;
139         int srcLength = src.length();
140         int destCapacity = MAX_CP_COUNT;
141         char[] dest = new char[destCapacity];
142         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
143         /*
144          * Handle the basic code points and
145          * convert extended ones to UTF-32 in cpBuffer (caseFlag in sign bit):
146          */

147         srcCPCount=destLength=0;
148         
149         for(j=0; j<srcLength; ++j) {
150             if(srcCPCount==MAX_CP_COUNT) {
151                 /* too many input code points */
152                 throw new IndexOutOfBoundsException JavaDoc();
153             }
154             c=src.charAt(j);
155             if(isBasic(c)) {
156                 if(destLength<destCapacity) {
157                     cpBuffer[srcCPCount++]=0;
158                     dest[destLength]=
159                         caseFlags!=null ?
160                             asciiCaseMap((char)c, caseFlags[j]) :
161                             (char)c;
162                 }
163                 ++destLength;
164             } else {
165                 n=((caseFlags!=null && caseFlags[j])? 1 : 0)<<31L;
166                 if(!UTF16.isSurrogate(c)) {
167                     n|=c;
168                 } else if(UTF16.isLeadSurrogate(c) && (j+1)<srcLength && UTF16.isTrailSurrogate(c2=src.charAt(j+1))) {
169                     ++j;
170                     
171                     n|=UCharacter.getCodePoint(c, c2);
172                 } else {
173                     /* error: unmatched surrogate */
174                     throw new StringPrepParseException("Illegal char found",StringPrepParseException.ILLEGAL_CHAR_FOUND);
175                 }
176                 cpBuffer[srcCPCount++]=n;
177             }
178         }
179
180         /* Finish the basic string - if it is not empty - with a delimiter. */
181         basicLength=destLength;
182         if(basicLength>0) {
183             if(destLength<destCapacity) {
184                 dest[destLength]=DELIMITER;
185             }
186             ++destLength;
187         }
188
189         /*
190          * handledCPCount is the number of code points that have been handled
191          * basicLength is the number of basic code points
192          * destLength is the number of chars that have been output
193          */

194
195         /* Initialize the state: */
196         n=INITIAL_N;
197         delta=0;
198         bias=INITIAL_BIAS;
199
200         /* Main encoding loop: */
201         for(handledCPCount=basicLength; handledCPCount<srcCPCount; /* no op */) {
202             /*
203              * All non-basic code points < n have been handled already.
204              * Find the next larger one:
205              */

206             for(m=0x7fffffff, j=0; j<srcCPCount; ++j) {
207                 q=cpBuffer[j]&0x7fffffff; /* remove case flag from the sign bit */
208                 if(n<=q && q<m) {
209                     m=q;
210                 }
211             }
212
213             /*
214              * Increase delta enough to advance the decoder's
215              * <n,i> state to <m,0>, but guard against overflow:
216              */

217             if(m-n>(0x7fffffff-MAX_CP_COUNT-delta)/(handledCPCount+1)) {
218                 throw new IllegalStateException JavaDoc("Internal program error");
219             }
220             delta+=(m-n)*(handledCPCount+1);
221             n=m;
222
223             /* Encode a sequence of same code points n */
224             for(j=0; j<srcCPCount; ++j) {
225                 q=cpBuffer[j]&0x7fffffff; /* remove case flag from the sign bit */
226                 if(q<n) {
227                     ++delta;
228                 } else if(q==n) {
229                     /* Represent delta as a generalized variable-length integer: */
230                     for(q=delta, k=BASE; /* no condition */; k+=BASE) {
231
232                         /** RAM: comment out the old code for conformance with draft-ietf-idn-punycode-03.txt
233
234                         t=k-bias;
235                         if(t<TMIN) {
236                             t=TMIN;
237                         } else if(t>TMAX) {
238                             t=TMAX;
239                         }
240                         */

241                     
242                         t=k-bias;
243                         if(t<TMIN) {
244                             t=TMIN;
245                         } else if(k>=(bias+TMAX)) {
246                             t=TMAX;
247                         }
248
249                         if(q<t) {
250                             break;
251                         }
252
253                         if(destLength<destCapacity) {
254                             dest[destLength++]=digitToBasic(t+(q-t)%(BASE-t), false);
255                         }
256                         q=(q-t)/(BASE-t);
257                     }
258
259                     if(destLength<destCapacity) {
260                         dest[destLength++]=digitToBasic(q, (cpBuffer[j]<0));
261                     }
262                     bias=adaptBias(delta, handledCPCount+1,(handledCPCount==basicLength));
263                     delta=0;
264                     ++handledCPCount;
265                 }
266             }
267
268             ++delta;
269             ++n;
270         }
271
272         return result.append(dest, 0, destLength);
273     }
274     
275     private static boolean isBasic(int ch){
276         return (ch < INITIAL_N);
277     }
278     ///CLOVER:OFF
279
private static boolean isBasicUpperCase(int ch){
280         return( CAPITAL_A<=ch && ch >= CAPITAL_Z);
281     }
282     ///CLOVER:ON
283
private static boolean isSurrogate(int ch){
284         return (((ch)&0xfffff800)==0xd800);
285     }
286     /**
287      * Converts Punycode to Unicode.
288      * The Unicode string will be at most as long as the Punycode string.
289      *
290      * @param src
291      * @param caseFlags
292      * @return
293      * @throws ParseException
294      */

295     public static StringBuffer JavaDoc decode(StringBuffer JavaDoc src, boolean[] caseFlags)
296                                throws StringPrepParseException{
297         int srcLength = src.length();
298         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
299         int n, destLength, i, bias, basicLength, j, in, oldi, w, k, digit, t,
300                 destCPCount, firstSupplementaryIndex, cpLength;
301         char b;
302         int destCapacity = MAX_CP_COUNT;
303         char[] dest = new char[destCapacity];
304
305         /*
306          * Handle the basic code points:
307          * Let basicLength be the number of input code points
308          * before the last delimiter, or 0 if there is none,
309          * then copy the first basicLength code points to the output.
310          *
311          * The two following loops iterate backward.
312          */

313         for(j=srcLength; j>0;) {
314             if(src.charAt(--j)==DELIMITER) {
315                 break;
316             }
317         }
318         destLength=basicLength=destCPCount=j;
319
320         while(j>0) {
321             b=src.charAt(--j);
322             if(!isBasic(b)) {
323                 throw new StringPrepParseException("Illegal char found", StringPrepParseException.INVALID_CHAR_FOUND);
324             }
325
326             if(j<destCapacity) {
327                 dest[j]= b;
328
329                 if(caseFlags!=null) {
330                     caseFlags[j]=isBasicUpperCase(b);
331                 }
332             }
333         }
334
335         /* Initialize the state: */
336         n=INITIAL_N;
337         i=0;
338         bias=INITIAL_BIAS;
339         firstSupplementaryIndex=1000000000;
340
341         /*
342          * Main decoding loop:
343          * Start just after the last delimiter if any
344          * basic code points were copied; start at the beginning otherwise.
345          */

346         for(in=basicLength>0 ? basicLength+1 : 0; in<srcLength; /* no op */) {
347             /*
348              * in is the index of the next character to be consumed, and
349              * destCPCount is the number of code points in the output array.
350              *
351              * Decode a generalized variable-length integer into delta,
352              * which gets added to i. The overflow checking is easier
353              * if we increase i as we go, then subtract off its starting
354              * value at the end to obtain delta.
355              */

356             for(oldi=i, w=1, k=BASE; /* no condition */; k+=BASE) {
357                 if(in>=srcLength) {
358                     throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
359                 }
360
361                 digit=basicToDigit[src.charAt(in++) & 0xFF];
362                 if(digit<0) {
363                     throw new StringPrepParseException("Invalid char found", StringPrepParseException.INVALID_CHAR_FOUND);
364                 }
365                 if(digit>(0x7fffffff-i)/w) {
366                     /* integer overflow */
367                     throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
368                 }
369
370                 i+=digit*w;
371                 t=k-bias;
372                 if(t<TMIN) {
373                     t=TMIN;
374                 } else if(k>=(bias+TMAX)) {
375                     t=TMAX;
376                 }
377                 if(digit<t) {
378                     break;
379                 }
380
381                 if(w>0x7fffffff/(BASE-t)) {
382                     /* integer overflow */
383                     throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
384                 }
385                 w*=BASE-t;
386             }
387
388             /*
389              * Modification from sample code:
390              * Increments destCPCount here,
391              * where needed instead of in for() loop tail.
392              */

393             ++destCPCount;
394             bias=adaptBias(i-oldi, destCPCount, (oldi==0));
395
396             /*
397              * i was supposed to wrap around from (incremented) destCPCount to 0,
398              * incrementing n each time, so we'll fix that now:
399              */

400             if(i/destCPCount>(0x7fffffff-n)) {
401                 /* integer overflow */
402                 throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
403             }
404
405             n+=i/destCPCount;
406             i%=destCPCount;
407             /* not needed for Punycode: */
408             /* if (decode_digit(n) <= BASE) return punycode_invalid_input; */
409
410             if(n>0x10ffff || isSurrogate(n)) {
411                 /* Unicode code point overflow */
412                 throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
413             }
414
415             /* Insert n at position i of the output: */
416             cpLength=UTF16.getCharCount(n);
417             if((destLength+cpLength)<destCapacity) {
418                 int codeUnitIndex;
419
420                 /*
421                  * Handle indexes when supplementary code points are present.
422                  *
423                  * In almost all cases, there will be only BMP code points before i
424                  * and even in the entire string.
425                  * This is handled with the same efficiency as with UTF-32.
426                  *
427                  * Only the rare cases with supplementary code points are handled
428                  * more slowly - but not too bad since this is an insertion anyway.
429                  */

430                 if(i<=firstSupplementaryIndex) {
431                     codeUnitIndex=i;
432                     if(cpLength>1) {
433                         firstSupplementaryIndex=codeUnitIndex;
434                     } else {
435                         ++firstSupplementaryIndex;
436                     }
437                 } else {
438                     codeUnitIndex=firstSupplementaryIndex;
439                     codeUnitIndex=UTF16.moveCodePointOffset(dest, 0, destLength, codeUnitIndex, i-codeUnitIndex);
440                 }
441
442                 /* use the UChar index codeUnitIndex instead of the code point index i */
443                 if(codeUnitIndex<destLength) {
444                     System.arraycopy(dest, codeUnitIndex,
445                                      dest, codeUnitIndex+cpLength,
446                                     (destLength-codeUnitIndex));
447                     if(caseFlags!=null) {
448                         System.arraycopy(caseFlags, codeUnitIndex,
449                                          caseFlags, codeUnitIndex+cpLength,
450                                          destLength-codeUnitIndex);
451                     }
452                 }
453                 if(cpLength==1) {
454                     /* BMP, insert one code unit */
455                     dest[codeUnitIndex]=(char)n;
456                 } else {
457                     /* supplementary character, insert two code units */
458                     dest[codeUnitIndex]=UTF16.getLeadSurrogate(n);
459                     dest[codeUnitIndex+1]=UTF16.getTrailSurrogate(n);
460                 }
461                 if(caseFlags!=null) {
462                     /* Case of last character determines uppercase flag: */
463                     caseFlags[codeUnitIndex]=isBasicUpperCase(src.charAt(in-1));
464                     if(cpLength==2) {
465                         caseFlags[codeUnitIndex+1]=false;
466                     }
467                 }
468             }
469             destLength+=cpLength;
470             ++i;
471         }
472         result.append(dest, 0, destLength);
473         return result;
474     }
475 }
476
477
Popular Tags