1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/utils/Rfc3492Idn.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * $HeadURL$ 1.6 + * $Revision$ 1.7 + * $Date$ 1.8 + * 1.9 + * ==================================================================== 1.10 + * 1.11 + * Licensed to the Apache Software Foundation (ASF) under one or more 1.12 + * contributor license agreements. See the NOTICE file distributed with 1.13 + * this work for additional information regarding copyright ownership. 1.14 + * The ASF licenses this file to You under the Apache License, Version 2.0 1.15 + * (the "License"); you may not use this file except in compliance with 1.16 + * the License. You may obtain a copy of the License at 1.17 + * 1.18 + * http://www.apache.org/licenses/LICENSE-2.0 1.19 + * 1.20 + * Unless required by applicable law or agreed to in writing, software 1.21 + * distributed under the License is distributed on an "AS IS" BASIS, 1.22 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.23 + * See the License for the specific language governing permissions and 1.24 + * limitations under the License. 1.25 + * ==================================================================== 1.26 + * 1.27 + * This software consists of voluntary contributions made by many 1.28 + * individuals on behalf of the Apache Software Foundation. For more 1.29 + * information on the Apache Software Foundation, please see 1.30 + * <http://www.apache.org/>. 1.31 + * 1.32 + */ 1.33 + 1.34 +package ch.boye.httpclientandroidlib.client.utils; 1.35 + 1.36 +import java.util.StringTokenizer; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.39 + 1.40 +/** 1.41 + * Implementation from pseudo code in RFC 3492. 1.42 + * 1.43 + * @since 4.0 1.44 + */ 1.45 +@Immutable 1.46 +public class Rfc3492Idn implements Idn { 1.47 + private static final int base = 36; 1.48 + private static final int tmin = 1; 1.49 + private static final int tmax = 26; 1.50 + private static final int skew = 38; 1.51 + private static final int damp = 700; 1.52 + private static final int initial_bias = 72; 1.53 + private static final int initial_n = 128; 1.54 + private static final char delimiter = '-'; 1.55 + private static final String ACE_PREFIX = "xn--"; 1.56 + 1.57 + private int adapt(int delta, int numpoints, boolean firsttime) { 1.58 + if (firsttime) delta = delta / damp; 1.59 + else delta = delta / 2; 1.60 + delta = delta + (delta / numpoints); 1.61 + int k = 0; 1.62 + while (delta > ((base - tmin) * tmax) / 2) { 1.63 + delta = delta / (base - tmin); 1.64 + k = k + base; 1.65 + } 1.66 + return k + (((base - tmin + 1) * delta) / (delta + skew)); 1.67 + } 1.68 + 1.69 + private int digit(char c) { 1.70 + if ((c >= 'A') && (c <= 'Z')) return (c - 'A'); 1.71 + if ((c >= 'a') && (c <= 'z')) return (c - 'a'); 1.72 + if ((c >= '0') && (c <= '9')) return (c - '0') + 26; 1.73 + throw new IllegalArgumentException("illegal digit: "+ c); 1.74 + } 1.75 + 1.76 + public String toUnicode(String punycode) { 1.77 + StringBuilder unicode = new StringBuilder(punycode.length()); 1.78 + StringTokenizer tok = new StringTokenizer(punycode, "."); 1.79 + while (tok.hasMoreTokens()) { 1.80 + String t = tok.nextToken(); 1.81 + if (unicode.length() > 0) unicode.append('.'); 1.82 + if (t.startsWith(ACE_PREFIX)) t = decode(t.substring(4)); 1.83 + unicode.append(t); 1.84 + } 1.85 + return unicode.toString(); 1.86 + } 1.87 + 1.88 + protected String decode(String input) { 1.89 + int n = initial_n; 1.90 + int i = 0; 1.91 + int bias = initial_bias; 1.92 + StringBuilder output = new StringBuilder(input.length()); 1.93 + int lastdelim = input.lastIndexOf(delimiter); 1.94 + if (lastdelim != -1) { 1.95 + output.append(input.subSequence(0, lastdelim)); 1.96 + input = input.substring(lastdelim + 1); 1.97 + } 1.98 + 1.99 + while (input.length() > 0) { 1.100 + int oldi = i; 1.101 + int w = 1; 1.102 + for (int k = base;; k += base) { 1.103 + if (input.length() == 0) break; 1.104 + char c = input.charAt(0); 1.105 + input = input.substring(1); 1.106 + int digit = digit(c); 1.107 + i = i + digit * w; // FIXME fail on overflow 1.108 + int t; 1.109 + if (k <= bias + tmin) { 1.110 + t = tmin; 1.111 + } else if (k >= bias + tmax) { 1.112 + t = tmax; 1.113 + } else { 1.114 + t = k - bias; 1.115 + } 1.116 + if (digit < t) break; 1.117 + w = w * (base - t); // FIXME fail on overflow 1.118 + } 1.119 + bias = adapt(i - oldi, output.length() + 1, (oldi == 0)); 1.120 + n = n + i / (output.length() + 1); // FIXME fail on overflow 1.121 + i = i % (output.length() + 1); 1.122 + // {if n is a basic code point then fail} 1.123 + output.insert(i, (char) n); 1.124 + i++; 1.125 + } 1.126 + return output.toString(); 1.127 + } 1.128 + 1.129 +}