2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static const unsigned base
= 36;
41 static const unsigned t_min
= 1;
42 static const unsigned t_max
= 26;
43 static const unsigned skew
= 38;
44 static const unsigned damp
= 700;
45 static const unsigned initial_n
= 128;
46 static const unsigned initial_bias
= 72;
51 return "abcdefghijklmnopqrstuvwxyz0123456789"[n
];
55 adapt(unsigned delta
, unsigned numpoints
, int first
)
63 delta
+= delta
/ numpoints
;
65 while (delta
> ((base
- t_min
) * t_max
) / 2) {
66 delta
/= base
- t_min
;
69 return k
+ (((base
- t_min
+ 1) * delta
) / (delta
+ skew
));
73 * Convert an UCS4 string to a puny-coded DNS label string suitable
74 * when combined with delimiters and other labels for DNS lookup.
76 * @param in an UCS4 string to convert
77 * @param in_len the length of in.
78 * @param out the resulting puny-coded string. The string is not NUL
80 * @param out_len before processing out_len should be the length of
81 * the out variable, after processing it will be the length of the out
84 * @return returns 0 on success, an wind error code otherwise
89 wind_punycode_label_toascii(const uint32_t *in
, size_t in_len
,
90 char *out
, size_t *out_len
)
92 unsigned n
= initial_n
;
94 unsigned bias
= initial_bias
;
101 for (i
= 0; i
< in_len
; ++i
) {
105 return WIND_ERR_OVERRUN
;
112 return WIND_ERR_OVERRUN
;
115 /* is this string punycoded */
117 if (o
+ 4 >= *out_len
)
118 return WIND_ERR_OVERRUN
;
119 memmove(out
+ 4, out
, o
);
120 memcpy(out
, "xn--", 4);
126 for (i
= 0; i
< in_len
; ++i
)
127 if(in
[i
] < m
&& in
[i
] >= n
)
130 delta
+= (m
- n
) * (h
+ 1);
132 for (i
= 0; i
< in_len
; ++i
) {
135 } else if (in
[i
] == n
) {
138 for (k
= base
; ; k
+= base
) {
142 else if (k
>= bias
+ t_max
)
149 return WIND_ERR_OVERRUN
;
150 out
[o
++] = digit(t
+ ((q
- t
) % (base
- t
)));
151 q
= (q
- t
) / (base
- t
);
154 return WIND_ERR_OVERRUN
;
157 bias
= adapt(delta
, h
+ 1, h
== b
);