xref: /minix3/crypto/external/bsd/heimdal/dist/lib/wind/punycode.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: punycode.c,v 1.1.1.1 2011/04/13 18:15:58 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37*ebfedea0SLionel Sambuc #include <config.h>
38*ebfedea0SLionel Sambuc #endif
39*ebfedea0SLionel Sambuc #include <string.h>
40*ebfedea0SLionel Sambuc #include "windlocl.h"
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc static const unsigned base         = 36;
43*ebfedea0SLionel Sambuc static const unsigned t_min        = 1;
44*ebfedea0SLionel Sambuc static const unsigned t_max        = 26;
45*ebfedea0SLionel Sambuc static const unsigned skew         = 38;
46*ebfedea0SLionel Sambuc static const unsigned damp         = 700;
47*ebfedea0SLionel Sambuc static const unsigned initial_n    = 128;
48*ebfedea0SLionel Sambuc static const unsigned initial_bias = 72;
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc static unsigned
digit(unsigned n)51*ebfedea0SLionel Sambuc digit(unsigned n)
52*ebfedea0SLionel Sambuc {
53*ebfedea0SLionel Sambuc     return "abcdefghijklmnopqrstuvwxyz0123456789"[n];
54*ebfedea0SLionel Sambuc }
55*ebfedea0SLionel Sambuc 
56*ebfedea0SLionel Sambuc static unsigned
adapt(unsigned delta,unsigned numpoints,int first)57*ebfedea0SLionel Sambuc adapt(unsigned delta, unsigned numpoints, int first)
58*ebfedea0SLionel Sambuc {
59*ebfedea0SLionel Sambuc     unsigned k;
60*ebfedea0SLionel Sambuc 
61*ebfedea0SLionel Sambuc     if (first)
62*ebfedea0SLionel Sambuc 	delta = delta / damp;
63*ebfedea0SLionel Sambuc     else
64*ebfedea0SLionel Sambuc 	delta /= 2;
65*ebfedea0SLionel Sambuc     delta += delta / numpoints;
66*ebfedea0SLionel Sambuc     k = 0;
67*ebfedea0SLionel Sambuc     while (delta > ((base - t_min) * t_max) / 2) {
68*ebfedea0SLionel Sambuc 	delta /= base - t_min;
69*ebfedea0SLionel Sambuc 	k += base;
70*ebfedea0SLionel Sambuc     }
71*ebfedea0SLionel Sambuc     return k + (((base - t_min + 1) * delta) / (delta + skew));
72*ebfedea0SLionel Sambuc }
73*ebfedea0SLionel Sambuc 
74*ebfedea0SLionel Sambuc /**
75*ebfedea0SLionel Sambuc  * Convert an UCS4 string to a puny-coded DNS label string suitable
76*ebfedea0SLionel Sambuc  * when combined with delimiters and other labels for DNS lookup.
77*ebfedea0SLionel Sambuc  *
78*ebfedea0SLionel Sambuc  * @param in an UCS4 string to convert
79*ebfedea0SLionel Sambuc  * @param in_len the length of in.
80*ebfedea0SLionel Sambuc  * @param out the resulting puny-coded string. The string is not NUL
81*ebfedea0SLionel Sambuc  * terminatied.
82*ebfedea0SLionel Sambuc  * @param out_len before processing out_len should be the length of
83*ebfedea0SLionel Sambuc  * the out variable, after processing it will be the length of the out
84*ebfedea0SLionel Sambuc  * string.
85*ebfedea0SLionel Sambuc  *
86*ebfedea0SLionel Sambuc  * @return returns 0 on success, an wind error code otherwise
87*ebfedea0SLionel Sambuc  * @ingroup wind
88*ebfedea0SLionel Sambuc  */
89*ebfedea0SLionel Sambuc 
90*ebfedea0SLionel Sambuc int
wind_punycode_label_toascii(const uint32_t * in,size_t in_len,char * out,size_t * out_len)91*ebfedea0SLionel Sambuc wind_punycode_label_toascii(const uint32_t *in, size_t in_len,
92*ebfedea0SLionel Sambuc 			    char *out, size_t *out_len)
93*ebfedea0SLionel Sambuc {
94*ebfedea0SLionel Sambuc     unsigned n     = initial_n;
95*ebfedea0SLionel Sambuc     unsigned delta = 0;
96*ebfedea0SLionel Sambuc     unsigned bias  = initial_bias;
97*ebfedea0SLionel Sambuc     unsigned h = 0;
98*ebfedea0SLionel Sambuc     unsigned b;
99*ebfedea0SLionel Sambuc     unsigned i;
100*ebfedea0SLionel Sambuc     unsigned o = 0;
101*ebfedea0SLionel Sambuc     unsigned m;
102*ebfedea0SLionel Sambuc 
103*ebfedea0SLionel Sambuc     for (i = 0; i < in_len; ++i) {
104*ebfedea0SLionel Sambuc 	if (in[i] < 0x80) {
105*ebfedea0SLionel Sambuc 	    ++h;
106*ebfedea0SLionel Sambuc 	    if (o >= *out_len)
107*ebfedea0SLionel Sambuc 		return WIND_ERR_OVERRUN;
108*ebfedea0SLionel Sambuc 	    out[o++] = in[i];
109*ebfedea0SLionel Sambuc 	}
110*ebfedea0SLionel Sambuc     }
111*ebfedea0SLionel Sambuc     b = h;
112*ebfedea0SLionel Sambuc     if (b > 0) {
113*ebfedea0SLionel Sambuc 	if (o >= *out_len)
114*ebfedea0SLionel Sambuc 	    return WIND_ERR_OVERRUN;
115*ebfedea0SLionel Sambuc 	out[o++] = 0x2D;
116*ebfedea0SLionel Sambuc     }
117*ebfedea0SLionel Sambuc     /* is this string punycoded */
118*ebfedea0SLionel Sambuc     if (h < in_len) {
119*ebfedea0SLionel Sambuc 	if (o + 4 >= *out_len)
120*ebfedea0SLionel Sambuc 	    return WIND_ERR_OVERRUN;
121*ebfedea0SLionel Sambuc 	memmove(out + 4, out, o);
122*ebfedea0SLionel Sambuc 	memcpy(out, "xn--", 4);
123*ebfedea0SLionel Sambuc 	o += 4;
124*ebfedea0SLionel Sambuc     }
125*ebfedea0SLionel Sambuc 
126*ebfedea0SLionel Sambuc     while (h < in_len) {
127*ebfedea0SLionel Sambuc 	m = (unsigned)-1;
128*ebfedea0SLionel Sambuc 	for (i = 0; i < in_len; ++i)
129*ebfedea0SLionel Sambuc 	    if(in[i] < m && in[i] >= n)
130*ebfedea0SLionel Sambuc 		m = in[i];
131*ebfedea0SLionel Sambuc 
132*ebfedea0SLionel Sambuc 	delta += (m - n) * (h + 1);
133*ebfedea0SLionel Sambuc 	n = m;
134*ebfedea0SLionel Sambuc 	for (i = 0; i < in_len; ++i) {
135*ebfedea0SLionel Sambuc 	    if (in[i] < n) {
136*ebfedea0SLionel Sambuc 		++delta;
137*ebfedea0SLionel Sambuc 	    } else if (in[i] == n) {
138*ebfedea0SLionel Sambuc 		unsigned q = delta;
139*ebfedea0SLionel Sambuc 		unsigned k;
140*ebfedea0SLionel Sambuc 		for (k = base; ; k += base) {
141*ebfedea0SLionel Sambuc 		    unsigned t;
142*ebfedea0SLionel Sambuc 		    if (k <= bias)
143*ebfedea0SLionel Sambuc 			t = t_min;
144*ebfedea0SLionel Sambuc 		    else if (k >= bias + t_max)
145*ebfedea0SLionel Sambuc 			t = t_max;
146*ebfedea0SLionel Sambuc 		    else
147*ebfedea0SLionel Sambuc 			t = k - bias;
148*ebfedea0SLionel Sambuc 		    if (q < t)
149*ebfedea0SLionel Sambuc 			break;
150*ebfedea0SLionel Sambuc 		    if (o >= *out_len)
151*ebfedea0SLionel Sambuc 			return WIND_ERR_OVERRUN;
152*ebfedea0SLionel Sambuc 		    out[o++] = digit(t + ((q - t) % (base - t)));
153*ebfedea0SLionel Sambuc 		    q = (q - t) / (base - t);
154*ebfedea0SLionel Sambuc 		}
155*ebfedea0SLionel Sambuc 		if (o >= *out_len)
156*ebfedea0SLionel Sambuc 		    return WIND_ERR_OVERRUN;
157*ebfedea0SLionel Sambuc 		out[o++] = digit(q);
158*ebfedea0SLionel Sambuc 		/* output */
159*ebfedea0SLionel Sambuc 		bias = adapt(delta, h + 1, h == b);
160*ebfedea0SLionel Sambuc 		delta = 0;
161*ebfedea0SLionel Sambuc 		++h;
162*ebfedea0SLionel Sambuc 	    }
163*ebfedea0SLionel Sambuc 	}
164*ebfedea0SLionel Sambuc 	++delta;
165*ebfedea0SLionel Sambuc 	++n;
166*ebfedea0SLionel Sambuc     }
167*ebfedea0SLionel Sambuc 
168*ebfedea0SLionel Sambuc     *out_len = o;
169*ebfedea0SLionel Sambuc     return 0;
170*ebfedea0SLionel Sambuc }
171