View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.utils;
28  
29  import java.util.StringTokenizer;
30  
31  import org.apache.http.annotation.Immutable;
32  
33  /**
34   * Implementation from pseudo code in RFC 3492.
35   *
36   * @since 4.0
37   */
38  @Immutable
39  public class Rfc3492Idn implements Idn {
40      private static final int base = 36;
41      private static final int tmin = 1;
42      private static final int tmax = 26;
43      private static final int skew = 38;
44      private static final int damp = 700;
45      private static final int initial_bias = 72;
46      private static final int initial_n = 128;
47      private static final char delimiter = '-';
48      private static final String ACE_PREFIX = "xn--";
49  
50      private int adapt(final int delta, final int numpoints, final boolean firsttime) {
51          int d = delta;
52          if (firsttime) {
53              d = d / damp;
54          } else {
55              d = d / 2;
56          }
57          d = d + (d / numpoints);
58          int k = 0;
59          while (d > ((base - tmin) * tmax) / 2) {
60            d = d / (base - tmin);
61            k = k + base;
62          }
63          return k + (((base - tmin + 1) * d) / (d + skew));
64      }
65  
66      private int digit(final char c) {
67          if ((c >= 'A') && (c <= 'Z')) {
68              return (c - 'A');
69          }
70          if ((c >= 'a') && (c <= 'z')) {
71              return (c - 'a');
72          }
73          if ((c >= '0') && (c <= '9')) {
74              return (c - '0') + 26;
75          }
76          throw new IllegalArgumentException("illegal digit: "+ c);
77      }
78  
79      @Override
80      public String toUnicode(final String punycode) {
81          final StringBuilder unicode = new StringBuilder(punycode.length());
82          final StringTokenizer tok = new StringTokenizer(punycode, ".");
83          while (tok.hasMoreTokens()) {
84              String t = tok.nextToken();
85              if (unicode.length() > 0) {
86                  unicode.append('.');
87              }
88              if (t.startsWith(ACE_PREFIX)) {
89                  t = decode(t.substring(4));
90              }
91              unicode.append(t);
92          }
93          return unicode.toString();
94      }
95  
96      protected String decode(final String s) {
97          String input = s;
98          int n = initial_n;
99          int i = 0;
100         int bias = initial_bias;
101         final StringBuilder output = new StringBuilder(input.length());
102         final int lastdelim = input.lastIndexOf(delimiter);
103         if (lastdelim != -1) {
104             output.append(input.subSequence(0, lastdelim));
105             input = input.substring(lastdelim + 1);
106         }
107 
108         while (!input.isEmpty()) {
109             final int oldi = i;
110             int w = 1;
111             for (int k = base;; k += base) {
112                 if (input.isEmpty()) {
113                     break;
114                 }
115                 final char c = input.charAt(0);
116                 input = input.substring(1);
117                 final int digit = digit(c);
118                 i = i + digit * w; // FIXME fail on overflow
119                 final int t;
120                 if (k <= bias + tmin) {
121                     t = tmin;
122                 } else if (k >= bias + tmax) {
123                     t = tmax;
124                 } else {
125                     t = k - bias;
126                 }
127                 if (digit < t) {
128                     break;
129                 }
130                 w = w * (base - t); // FIXME fail on overflow
131             }
132             bias = adapt(i - oldi, output.length() + 1, (oldi == 0));
133             n = n + i / (output.length() + 1); // FIXME fail on overflow
134             i = i % (output.length() + 1);
135             // {if n is a basic code point then fail}
136             output.insert(i, (char) n);
137             i++;
138         }
139         return output.toString();
140     }
141 
142 }