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