summaryrefslogtreecommitdiffstats
path: root/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/utils/Rfc3492Idn.java
blob: ccf9800503149d93619528aeb7822f50cdf92d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package ch.boye.httpclientandroidlib.client.utils;

import java.util.StringTokenizer;

import ch.boye.httpclientandroidlib.annotation.Immutable;

/**
 * Implementation from pseudo code in RFC 3492.
 *
 * @since 4.0
 */
@Immutable
public class Rfc3492Idn implements Idn {
    private static final int base = 36;
    private static final int tmin = 1;
    private static final int tmax = 26;
    private static final int skew = 38;
    private static final int damp = 700;
    private static final int initial_bias = 72;
    private static final int initial_n = 128;
    private static final char delimiter = '-';
    private static final String ACE_PREFIX = "xn--";

    private int adapt(final int delta, final int numpoints, final boolean firsttime) {
        int d = delta;
        if (firsttime) {
            d = d / damp;
        } else {
            d = d / 2;
        }
        d = d + (d / numpoints);
        int k = 0;
        while (d > ((base - tmin) * tmax) / 2) {
          d = d / (base - tmin);
          k = k + base;
        }
        return k + (((base - tmin + 1) * d) / (d + skew));
    }

    private int digit(final char c) {
        if ((c >= 'A') && (c <= 'Z')) {
            return (c - 'A');
        }
        if ((c >= 'a') && (c <= 'z')) {
            return (c - 'a');
        }
        if ((c >= '0') && (c <= '9')) {
            return (c - '0') + 26;
        }
        throw new IllegalArgumentException("illegal digit: "+ c);
    }

    public String toUnicode(final String punycode) {
        final StringBuilder unicode = new StringBuilder(punycode.length());
        final StringTokenizer tok = new StringTokenizer(punycode, ".");
        while (tok.hasMoreTokens()) {
            String t = tok.nextToken();
            if (unicode.length() > 0) {
                unicode.append('.');
            }
            if (t.startsWith(ACE_PREFIX)) {
                t = decode(t.substring(4));
            }
            unicode.append(t);
        }
        return unicode.toString();
    }

    protected String decode(final String s) {
        String input = s;
        int n = initial_n;
        int i = 0;
        int bias = initial_bias;
        final StringBuilder output = new StringBuilder(input.length());
        final int lastdelim = input.lastIndexOf(delimiter);
        if (lastdelim != -1) {
            output.append(input.subSequence(0, lastdelim));
            input = input.substring(lastdelim + 1);
        }

        while (input.length() > 0) {
            final int oldi = i;
            int w = 1;
            for (int k = base;; k += base) {
                if (input.length() == 0) {
                    break;
                }
                final char c = input.charAt(0);
                input = input.substring(1);
                final int digit = digit(c);
                i = i + digit * w; // FIXME fail on overflow
                final int t;
                if (k <= bias + tmin) {
                    t = tmin;
                } else if (k >= bias + tmax) {
                    t = tmax;
                } else {
                    t = k - bias;
                }
                if (digit < t) {
                    break;
                }
                w = w * (base - t); // FIXME fail on overflow
            }
            bias = adapt(i - oldi, output.length() + 1, (oldi == 0));
            n = n + i / (output.length() + 1); // FIXME fail on overflow
            i = i % (output.length() + 1);
            // {if n is a basic code point then fail}
            output.insert(i, (char) n);
            i++;
        }
        return output.toString();
    }

}