001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.host;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.CharBuffer;
033: import com.caucho.util.L10N;
034:
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: /**
039: * Domain name normalization
040: */
041: public class DomainName {
042: static final Logger log = Log.open(DomainName.class);
043: static final L10N L = new L10N(DomainName.class);
044:
045: private final static char ENCODE[];
046: private final static int DECODE[];
047:
048: private final static int base = 36;
049: private final static int tmin = 1;
050: private final static int tmax = 26;
051: private final static int skew = 38;
052: private final static int damp = 700;
053: private final static int initialBias = 72;
054: private final static int initialN = 128;
055:
056: /**
057: * Converts from the ascii "punicode" to a string.
058: */
059: public static String fromAscii(String source) {
060: CharBuffer result = CharBuffer.allocate();
061: CharBuffer cb = CharBuffer.allocate();
062:
063: int index = 0;
064: int length = source.length();
065: boolean isFirst = true;
066:
067: try {
068: while (index < length) {
069: char ch = source.charAt(index + 0);
070:
071: if (isFirst && index + 4 < length
072: && source.charAt(index + 0) == 'x'
073: && source.charAt(index + 1) == 'n'
074: && source.charAt(index + 2) == '-'
075: && source.charAt(index + 3) == '-') {
076: int p = source.indexOf('.', index);
077: String seq;
078:
079: if (p < 0)
080: seq = source.substring(index + 4);
081: else
082: seq = source.substring(index + 4, p);
083:
084: decode(result, cb, seq);
085:
086: index += 4 + seq.length();
087: continue;
088: }
089:
090: index++;
091:
092: isFirst = false;
093:
094: if (ch == '.') {
095: isFirst = true;
096:
097: result.append(ch);
098: } else
099: result.append(Character.toLowerCase(ch));
100: }
101:
102: return result.close();
103: } catch (Throwable e) {
104: log.log(Level.WARNING, e.toString(), e);
105:
106: throw new RuntimeException(e);
107: }
108: }
109:
110: /**
111: * Converts to the ascii "punicode".
112: */
113: public static String toAscii(String source) {
114: CharBuffer result = CharBuffer.allocate();
115: CharBuffer cb = CharBuffer.allocate();
116:
117: int head = 0;
118: int length = source.length();
119:
120: while (head < length) {
121: boolean isAscii = true;
122:
123: cb.clear();
124:
125: int i = head;
126: for (; i < length; i++) {
127: char ch = source.charAt(i);
128:
129: if (ch == '.') {
130: cb.append(ch);
131: break;
132: } else if (ch <= 0x7f)
133: cb.append(ch);
134: else {
135: isAscii = false;
136: break;
137: }
138: }
139:
140: if (isAscii) {
141: head = i + 1;
142: result.append(cb);
143: continue;
144: }
145:
146: cb.clear();
147: i = head;
148: for (; i < length; i++) {
149: char ch = source.charAt(i);
150:
151: if (ch == '.')
152: break;
153:
154: //cb.append(Character.toLowerCase(ch));
155: cb.append(ch);
156: }
157: head = i;
158:
159: String seq = cb.toString();
160:
161: cb.clear();
162:
163: toAscii(cb, seq);
164:
165: result.append(cb);
166: }
167:
168: return result.close();
169: }
170:
171: private static void decode(CharBuffer result, CharBuffer cb,
172: String seq) {
173: int length = seq.length();
174: int b = 0;
175:
176: for (int i = 0; i < length; i++) {
177: char ch = seq.charAt(i);
178:
179: if (ch == '-')
180: b = i;
181: }
182:
183: for (int i = 0; i < b; i++) {
184: char ch = seq.charAt(i);
185:
186: cb.append(Character.toLowerCase(ch));
187: }
188:
189: int in = b > 0 ? b + 1 : 0;
190: int i = 0;
191: int bias = initialBias;
192: int out = cb.length();
193: int n = initialN;
194:
195: while (in < length) {
196: int oldi = i;
197: int w = 1;
198:
199: for (int k = base; true; k += base) {
200: char ch = seq.charAt(in++);
201: int digit = DECODE[ch];
202:
203: i += digit * w;
204:
205: int t;
206: if (k <= bias)
207: t = tmin;
208: else if (bias + tmax <= k)
209: t = tmax;
210: else
211: t = k - bias;
212:
213: if (digit < t)
214: break;
215:
216: w *= (base - t);
217: }
218:
219: bias = adapt(i - oldi, out + 1, oldi == 0);
220:
221: n += i / (out + 1);
222: i %= (out + 1);
223:
224: cb.append(' ');
225: char[] cBuf = cb.getBuffer();
226:
227: System.arraycopy(cBuf, i, cBuf, i + 1, out - i);
228: cBuf[i++] = Character.toLowerCase((char) n);
229:
230: out++;
231: }
232:
233: result.append(cb);
234: }
235:
236: private static void toAscii(CharBuffer cb, String seq) {
237: cb.append("xn--");
238:
239: int length = seq.length();
240:
241: int index = 0;
242: int n = initialN;
243: int delta = 0;
244: int bias = initialBias;
245: int b = 0; // # of basic code points
246:
247: for (int i = 0; i < length; i++) {
248: char ch = seq.charAt(i);
249: if (ch < 0x80) {
250: cb.append(ch);
251: b++;
252: }
253: }
254:
255: if (b > 0)
256: cb.append('-');
257:
258: int h = b;
259:
260: while (h < length) {
261: int m = 0xffff;
262:
263: for (int i = 0; i < length; i++) {
264: char ch = seq.charAt(i);
265:
266: if (n <= ch && ch < m)
267: m = ch;
268: }
269:
270: // XXX: overflow
271: delta = delta + (m - n) * (h + 1);
272: n = m;
273:
274: for (int i = 0; i < length; i++) {
275: int ch = seq.charAt(i);
276:
277: if (ch < n) {
278: delta++;
279: }
280:
281: if (ch == n) {
282: int q = delta;
283:
284: for (int k = base; true; k += base) {
285: int t;
286:
287: if (k <= bias)
288: t = tmin;
289: else if (bias + tmax <= k)
290: t = tmax;
291: else
292: t = k - bias;
293:
294: if (q < t)
295: break;
296:
297: cb.append(ENCODE[t + (q - t) % (base - t)]);
298: q = (q - t) / (base - t);
299: }
300:
301: cb.append(ENCODE[q]);
302: bias = adapt(delta, h + 1, h == b);
303: delta = 0;
304: h++;
305: }
306: }
307:
308: delta++;
309: n++;
310: }
311: }
312:
313: private static int adapt(int delta, int nPoints, boolean isFirst) {
314: int k;
315:
316: delta = isFirst ? delta / damp : delta / 2;
317: delta += delta / nPoints;
318:
319: for (k = 0; ((base - tmin) * tmax) / 2 < delta; k += base) {
320: delta /= base - tmin;
321: }
322:
323: return k + (base - tmin + 1) * delta / (delta + skew);
324: }
325:
326: static {
327: ENCODE = new char[36];
328:
329: for (int i = 0; i < 26; i++) {
330: ENCODE[i] = (char) ('a' + i);
331: }
332:
333: for (int i = 0; i < 10; i++) {
334: ENCODE[i + 26] = (char) ('0' + i);
335: }
336:
337: DECODE = new int[0x80];
338:
339: for (int i = 0; i < 26; i++) {
340: DECODE[(char) ('a' + i)] = i;
341: DECODE[(char) ('A' + i)] = i;
342: }
343:
344: for (int i = 0; i < 10; i++) {
345: DECODE[(char) ('0' + i)] = 26 + i;
346: }
347: }
348: }
|