michael@0: /* michael@0: * $HeadURL$ michael@0: * $Revision$ michael@0: * $Date$ michael@0: * michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.client.utils; michael@0: michael@0: import java.util.StringTokenizer; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: /** michael@0: * Implementation from pseudo code in RFC 3492. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class Rfc3492Idn implements Idn { michael@0: private static final int base = 36; michael@0: private static final int tmin = 1; michael@0: private static final int tmax = 26; michael@0: private static final int skew = 38; michael@0: private static final int damp = 700; michael@0: private static final int initial_bias = 72; michael@0: private static final int initial_n = 128; michael@0: private static final char delimiter = '-'; michael@0: private static final String ACE_PREFIX = "xn--"; michael@0: michael@0: private int adapt(int delta, int numpoints, boolean firsttime) { michael@0: if (firsttime) delta = delta / damp; michael@0: else delta = delta / 2; michael@0: delta = delta + (delta / numpoints); michael@0: int k = 0; michael@0: while (delta > ((base - tmin) * tmax) / 2) { michael@0: delta = delta / (base - tmin); michael@0: k = k + base; michael@0: } michael@0: return k + (((base - tmin + 1) * delta) / (delta + skew)); michael@0: } michael@0: michael@0: private int digit(char c) { michael@0: if ((c >= 'A') && (c <= 'Z')) return (c - 'A'); michael@0: if ((c >= 'a') && (c <= 'z')) return (c - 'a'); michael@0: if ((c >= '0') && (c <= '9')) return (c - '0') + 26; michael@0: throw new IllegalArgumentException("illegal digit: "+ c); michael@0: } michael@0: michael@0: public String toUnicode(String punycode) { michael@0: StringBuilder unicode = new StringBuilder(punycode.length()); michael@0: StringTokenizer tok = new StringTokenizer(punycode, "."); michael@0: while (tok.hasMoreTokens()) { michael@0: String t = tok.nextToken(); michael@0: if (unicode.length() > 0) unicode.append('.'); michael@0: if (t.startsWith(ACE_PREFIX)) t = decode(t.substring(4)); michael@0: unicode.append(t); michael@0: } michael@0: return unicode.toString(); michael@0: } michael@0: michael@0: protected String decode(String input) { michael@0: int n = initial_n; michael@0: int i = 0; michael@0: int bias = initial_bias; michael@0: StringBuilder output = new StringBuilder(input.length()); michael@0: int lastdelim = input.lastIndexOf(delimiter); michael@0: if (lastdelim != -1) { michael@0: output.append(input.subSequence(0, lastdelim)); michael@0: input = input.substring(lastdelim + 1); michael@0: } michael@0: michael@0: while (input.length() > 0) { michael@0: int oldi = i; michael@0: int w = 1; michael@0: for (int k = base;; k += base) { michael@0: if (input.length() == 0) break; michael@0: char c = input.charAt(0); michael@0: input = input.substring(1); michael@0: int digit = digit(c); michael@0: i = i + digit * w; // FIXME fail on overflow michael@0: int t; michael@0: if (k <= bias + tmin) { michael@0: t = tmin; michael@0: } else if (k >= bias + tmax) { michael@0: t = tmax; michael@0: } else { michael@0: t = k - bias; michael@0: } michael@0: if (digit < t) break; michael@0: w = w * (base - t); // FIXME fail on overflow michael@0: } michael@0: bias = adapt(i - oldi, output.length() + 1, (oldi == 0)); michael@0: n = n + i / (output.length() + 1); // FIXME fail on overflow michael@0: i = i % (output.length() + 1); michael@0: // {if n is a basic code point then fail} michael@0: output.insert(i, (char) n); michael@0: i++; michael@0: } michael@0: return output.toString(); michael@0: } michael@0: michael@0: }