2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 adapt(int delta, int numpoints, int firsttime)
41 delta += (delta / numpoints);
44 while (delta > ((BASE - TMIN) * TMAX) / 2) {
45 delta = delta / (BASE - TMIN);
48 return k + (((BASE - TMIN + 1) * delta) / (delta + SKEW));
52 copy_until_delimiter(const char *s, char *out, size_t len)
57 end = strchr(s, '\0');
62 for (t = end; t >= s; --t)
69 for (; s < t; ++s, ++out) {
81 if ('A' <= c && c <= 'Z')
84 if ('a' <= c && c <= 'z')
87 if ('0' <= c && c <= '9')
94 insert(char *out, size_t len, int codepoint, size_t i)
99 if (codepoint <= 0x7F)
101 else if (codepoint <= 0x7FF)
103 else if (codepoint <= 0xFFFF)
105 else if (codepoint <= 0x10FFFF)
110 if ((t = utf8_nth(out, i)) == NULL)
112 if (t + l >= out + len)
115 memmove(t + l, t, strlen(t));
119 t[1] = ( codepoint & 0x3F) + 0x80;
120 t[0] = ((codepoint >> 6) & 0x1F) + 0xC0;
123 t[2] = ( codepoint & 0x3F) + 0x80;
124 t[1] = ((codepoint >> 6) & 0x3F) + 0x80;
125 t[0] = ((codepoint >> 12) & 0x0F) + 0xE0;
128 t[3] = ( codepoint & 0x3F) + 0x80;
129 t[2] = ((codepoint >> 6) & 0x3F) + 0x80;
130 t[1] = ((codepoint >> 12) & 0x3F) + 0x80;
131 t[0] = ((codepoint >> 18) & 0x07) + 0xF0;
138 decode(const char *str, char *out, size_t len)
142 unsigned int oldi, bias, w, k, digit, t;
143 unsigned int numpoints;
146 if (!starts_with(str, "xn--")) {
147 strncpy(out, str, len);
154 if (strchr(str, '-') != NULL) {
155 if ((s = copy_until_delimiter(str, out, len)) == NULL)
162 numpoints = strlen(out);
172 for (k = BASE; ; k += BASE) {
175 /* fail eventually? */
176 digit = digit_value(*s);
179 /* fail on overflow */
184 else if (k >= bias + TMAX)
194 bias = adapt(i - oldi, numpoints+1, oldi == 0);
195 n += i / (numpoints+1); /* fail on overflow */
196 i = i % (numpoints+1);
198 if (!insert(out, len, n, i))
208 end_of_component(const char *hostname)
210 for (; *hostname != '\0' && *hostname != '.'; ++hostname)
216 puny_decode(const char *hostname, char *out, size_t len)
218 char comp[DOMAIN_NAME_LEN];
223 if (hostname == NULL)
228 end = end_of_component(s);
230 if (l >= sizeof(comp))
233 memcpy(comp, s, end - s);
234 comp[end - s] = '\0';
236 if (!decode(comp, out, len))
242 if (strlcat(out, ".", len) >= len)