Blame


1 7957cbd9 2021-01-27 op /*
2 7957cbd9 2021-01-27 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 7957cbd9 2021-01-27 op *
4 7957cbd9 2021-01-27 op * Permission to use, copy, modify, and distribute this software for any
5 7957cbd9 2021-01-27 op * purpose with or without fee is hereby granted, provided that the above
6 7957cbd9 2021-01-27 op * copyright notice and this permission notice appear in all copies.
7 7957cbd9 2021-01-27 op *
8 7957cbd9 2021-01-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7957cbd9 2021-01-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7957cbd9 2021-01-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7957cbd9 2021-01-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7957cbd9 2021-01-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7957cbd9 2021-01-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7957cbd9 2021-01-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7957cbd9 2021-01-27 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 7957cbd9 2021-01-27 op
19 7957cbd9 2021-01-27 op #include <stddef.h>
20 7957cbd9 2021-01-27 op #include <stdint.h>
21 7957cbd9 2021-01-27 op #include <string.h>
22 7957cbd9 2021-01-27 op
23 7957cbd9 2021-01-27 op #define BASE 36
24 7957cbd9 2021-01-27 op #define TMIN 1
25 7957cbd9 2021-01-27 op #define TMAX 26
26 7957cbd9 2021-01-27 op #define SKEW 38
27 7957cbd9 2021-01-27 op #define DAMP 700
28 7957cbd9 2021-01-27 op #define IBIAS 72
29 7957cbd9 2021-01-27 op #define IN 128
30 7957cbd9 2021-01-27 op
31 7957cbd9 2021-01-27 op static int
32 7957cbd9 2021-01-27 op adapt(int delta, int numpoints, int firsttime)
33 7957cbd9 2021-01-27 op {
34 7957cbd9 2021-01-27 op int k;
35 7957cbd9 2021-01-27 op
36 7957cbd9 2021-01-27 op if (firsttime)
37 7957cbd9 2021-01-27 op delta = delta / DAMP;
38 7957cbd9 2021-01-27 op else
39 7957cbd9 2021-01-27 op delta = delta / 2;
40 7957cbd9 2021-01-27 op
41 7957cbd9 2021-01-27 op delta += (delta / numpoints);
42 7957cbd9 2021-01-27 op
43 7957cbd9 2021-01-27 op k = 0;
44 7957cbd9 2021-01-27 op while (delta > ((BASE - TMIN) * TMAX) / 2) {
45 7957cbd9 2021-01-27 op delta = delta / (BASE - TMIN);
46 7957cbd9 2021-01-27 op k += BASE;
47 7957cbd9 2021-01-27 op }
48 7957cbd9 2021-01-27 op return k + (((BASE - TMIN + 1) * delta) / (delta + SKEW));
49 7957cbd9 2021-01-27 op }
50 7957cbd9 2021-01-27 op
51 7957cbd9 2021-01-27 op static const char *
52 35cf19e3 2021-01-28 op copy_label(const char *s, char *out, size_t len)
53 7957cbd9 2021-01-27 op {
54 7957cbd9 2021-01-27 op char *end, *t;
55 0cd66754 2021-01-27 op size_t l;
56 7957cbd9 2021-01-27 op
57 7957cbd9 2021-01-27 op end = strchr(s, '\0');
58 0cd66754 2021-01-27 op l = end - s;
59 0cd66754 2021-01-27 op if (l > len)
60 7957cbd9 2021-01-27 op return NULL;
61 7957cbd9 2021-01-27 op
62 7957cbd9 2021-01-27 op for (t = end; t >= s; --t)
63 7957cbd9 2021-01-27 op if (*t == '-')
64 7957cbd9 2021-01-27 op break;
65 7957cbd9 2021-01-27 op
66 7957cbd9 2021-01-27 op if (t < s)
67 7957cbd9 2021-01-27 op t = end;
68 7957cbd9 2021-01-27 op
69 7957cbd9 2021-01-27 op for (; s < t; ++s, ++out) {
70 7957cbd9 2021-01-27 op if (*s > 'z')
71 7957cbd9 2021-01-27 op return NULL;
72 7957cbd9 2021-01-27 op *out = *s;
73 7957cbd9 2021-01-27 op }
74 7957cbd9 2021-01-27 op
75 7957cbd9 2021-01-27 op return s;
76 7957cbd9 2021-01-27 op }
77 7957cbd9 2021-01-27 op
78 7957cbd9 2021-01-27 op static unsigned int
79 7957cbd9 2021-01-27 op digit_value(char c)
80 7957cbd9 2021-01-27 op {
81 7957cbd9 2021-01-27 op if ('A' <= c && c <= 'Z')
82 7957cbd9 2021-01-27 op return c - 'A';
83 7957cbd9 2021-01-27 op
84 7957cbd9 2021-01-27 op if ('a' <= c && c <= 'z')
85 7957cbd9 2021-01-27 op return c - 'a';
86 7957cbd9 2021-01-27 op
87 7957cbd9 2021-01-27 op if ('0' <= c && c <= '9')
88 7957cbd9 2021-01-27 op return 26 + c - '0';
89 7957cbd9 2021-01-27 op
90 7957cbd9 2021-01-27 op return c;
91 7957cbd9 2021-01-27 op }
92 7957cbd9 2021-01-27 op
93 7957cbd9 2021-01-27 op static int
94 4a3ab609 2021-01-29 op insert(char *out, size_t len, int codepoint, size_t i, const char **err)
95 7957cbd9 2021-01-27 op {
96 7957cbd9 2021-01-27 op int l;
97 7957cbd9 2021-01-27 op char *t;
98 7957cbd9 2021-01-27 op
99 a2fd8013 2021-01-29 op if (codepoint <= 0x7F) {
100 a2fd8013 2021-01-29 op *err = "puny: invalid decoded character (ASCII range)";
101 7957cbd9 2021-01-27 op return 0;
102 a2fd8013 2021-01-29 op } else if (codepoint <= 0x7FF) {
103 7957cbd9 2021-01-27 op l = 2;
104 a2fd8013 2021-01-29 op } else if (codepoint <= 0xFFFF) {
105 7957cbd9 2021-01-27 op l = 3;
106 a2fd8013 2021-01-29 op } else if (codepoint <= 0x10FFFF) {
107 7957cbd9 2021-01-27 op l = 4;
108 a2fd8013 2021-01-29 op } else {
109 a2fd8013 2021-01-29 op *err = "puny: invalid decoded character";
110 7957cbd9 2021-01-27 op return 0;
111 a2fd8013 2021-01-29 op }
112 7957cbd9 2021-01-27 op
113 a2fd8013 2021-01-29 op if ((t = utf8_nth(out, i)) == NULL) {
114 a2fd8013 2021-01-29 op *err = "puny: invalid insert position";
115 7957cbd9 2021-01-27 op return 0;
116 a2fd8013 2021-01-29 op }
117 a2fd8013 2021-01-29 op
118 a2fd8013 2021-01-29 op if (t + l >= out + len) {
119 a2fd8013 2021-01-29 op *err = "puny: insert would overflow";
120 7957cbd9 2021-01-27 op return 0;
121 a2fd8013 2021-01-29 op }
122 7957cbd9 2021-01-27 op
123 7957cbd9 2021-01-27 op memmove(t + l, t, strlen(t));
124 7957cbd9 2021-01-27 op
125 7957cbd9 2021-01-27 op switch (l) {
126 7957cbd9 2021-01-27 op case 2:
127 7957cbd9 2021-01-27 op t[1] = ( codepoint & 0x3F) + 0x80;
128 35cf19e3 2021-01-28 op t[0] = ((codepoint >> 6) & 0x1F) + 0xC0;
129 7957cbd9 2021-01-27 op break;
130 7957cbd9 2021-01-27 op case 3:
131 7957cbd9 2021-01-27 op t[2] = ( codepoint & 0x3F) + 0x80;
132 35cf19e3 2021-01-28 op t[1] = ((codepoint >> 6) & 0x3F) + 0x80;
133 7957cbd9 2021-01-27 op t[0] = ((codepoint >> 12) & 0x0F) + 0xE0;
134 7957cbd9 2021-01-27 op break;
135 7957cbd9 2021-01-27 op case 4:
136 7957cbd9 2021-01-27 op t[3] = ( codepoint & 0x3F) + 0x80;
137 35cf19e3 2021-01-28 op t[2] = ((codepoint >> 6) & 0x3F) + 0x80;
138 7957cbd9 2021-01-27 op t[1] = ((codepoint >> 12) & 0x3F) + 0x80;
139 7957cbd9 2021-01-27 op t[0] = ((codepoint >> 18) & 0x07) + 0xF0;
140 7957cbd9 2021-01-27 op break;
141 7957cbd9 2021-01-27 op }
142 7957cbd9 2021-01-27 op return 1;
143 7957cbd9 2021-01-27 op }
144 7957cbd9 2021-01-27 op
145 7957cbd9 2021-01-27 op static int
146 a2fd8013 2021-01-29 op decode(const char *str, char *out, size_t len, const char **err)
147 7957cbd9 2021-01-27 op {
148 7957cbd9 2021-01-27 op size_t i;
149 7957cbd9 2021-01-27 op uint32_t n;
150 7957cbd9 2021-01-27 op unsigned int oldi, bias, w, k, digit, t;
151 7957cbd9 2021-01-27 op unsigned int numpoints;
152 7957cbd9 2021-01-27 op const char *s;
153 7957cbd9 2021-01-27 op
154 44ee1bac 2021-01-27 op if (!starts_with(str, "xn--")) {
155 7957cbd9 2021-01-27 op strncpy(out, str, len);
156 7957cbd9 2021-01-27 op return 1;
157 7957cbd9 2021-01-27 op }
158 7957cbd9 2021-01-27 op
159 7957cbd9 2021-01-27 op /* skip the xn-- */
160 7957cbd9 2021-01-27 op str += 4;
161 7957cbd9 2021-01-27 op
162 7957cbd9 2021-01-27 op if (strchr(str, '-') != NULL) {
163 a2fd8013 2021-01-29 op if ((s = copy_label(str, out, len)) == NULL) {
164 a2fd8013 2021-01-29 op *err = "puny: invalid label";
165 7957cbd9 2021-01-27 op return 0;
166 a2fd8013 2021-01-29 op }
167 7957cbd9 2021-01-27 op if (*s == '-')
168 7957cbd9 2021-01-27 op s++;
169 7957cbd9 2021-01-27 op } else
170 7957cbd9 2021-01-27 op s = str;
171 7957cbd9 2021-01-27 op
172 7957cbd9 2021-01-27 op numpoints = strlen(out);
173 7957cbd9 2021-01-27 op
174 7957cbd9 2021-01-27 op n = IN;
175 7957cbd9 2021-01-27 op i = 0;
176 7957cbd9 2021-01-27 op bias = IBIAS;
177 7957cbd9 2021-01-27 op
178 7957cbd9 2021-01-27 op while (*s != '\0') {
179 7957cbd9 2021-01-27 op oldi = i;
180 7957cbd9 2021-01-27 op w = 1;
181 7957cbd9 2021-01-27 op
182 7957cbd9 2021-01-27 op for (k = BASE; ; k += BASE) {
183 a2fd8013 2021-01-29 op if (*s == '\0') {
184 a2fd8013 2021-01-29 op *err = "puny: label truncated?";
185 7957cbd9 2021-01-27 op return 0;
186 a2fd8013 2021-01-29 op }
187 7957cbd9 2021-01-27 op /* fail eventually? */
188 7957cbd9 2021-01-27 op digit = digit_value(*s);
189 7957cbd9 2021-01-27 op s++;
190 7957cbd9 2021-01-27 op
191 7957cbd9 2021-01-27 op /* fail on overflow */
192 7957cbd9 2021-01-27 op i += digit * w;
193 7957cbd9 2021-01-27 op
194 7957cbd9 2021-01-27 op if (k <= bias)
195 7957cbd9 2021-01-27 op t = TMIN;
196 7957cbd9 2021-01-27 op else if (k >= bias + TMAX)
197 7957cbd9 2021-01-27 op t = TMAX;
198 7957cbd9 2021-01-27 op else
199 7957cbd9 2021-01-27 op t = k - bias;
200 7957cbd9 2021-01-27 op
201 7957cbd9 2021-01-27 op if (digit < t)
202 7957cbd9 2021-01-27 op break;
203 7957cbd9 2021-01-27 op w *= (BASE - t);
204 7957cbd9 2021-01-27 op }
205 7957cbd9 2021-01-27 op
206 7957cbd9 2021-01-27 op bias = adapt(i - oldi, numpoints+1, oldi == 0);
207 7957cbd9 2021-01-27 op n += i / (numpoints+1); /* fail on overflow */
208 7957cbd9 2021-01-27 op i = i % (numpoints+1);
209 7957cbd9 2021-01-27 op
210 4a3ab609 2021-01-29 op if (!insert(out, len, n, i, err))
211 7957cbd9 2021-01-27 op return 0;
212 7957cbd9 2021-01-27 op numpoints++;
213 7957cbd9 2021-01-27 op ++i;
214 7957cbd9 2021-01-27 op }
215 7957cbd9 2021-01-27 op
216 7957cbd9 2021-01-27 op return 1;
217 7957cbd9 2021-01-27 op }
218 7957cbd9 2021-01-27 op
219 0cd66754 2021-01-27 op static const char *
220 35cf19e3 2021-01-28 op end_of_label(const char *hostname)
221 7957cbd9 2021-01-27 op {
222 7957cbd9 2021-01-27 op for (; *hostname != '\0' && *hostname != '.'; ++hostname)
223 7957cbd9 2021-01-27 op ; /* nop */
224 7957cbd9 2021-01-27 op return hostname;
225 7957cbd9 2021-01-27 op }
226 7957cbd9 2021-01-27 op
227 7957cbd9 2021-01-27 op int
228 a2fd8013 2021-01-29 op puny_decode(const char *hostname, char *out, size_t len, const char **err)
229 7957cbd9 2021-01-27 op {
230 35cf19e3 2021-01-28 op char label[LABEL_LEN];
231 7957cbd9 2021-01-27 op const char *s, *end;
232 7957cbd9 2021-01-27 op size_t l;
233 7957cbd9 2021-01-27 op
234 7957cbd9 2021-01-27 op memset(out, 0, len);
235 44ee1bac 2021-01-27 op if (hostname == NULL)
236 44ee1bac 2021-01-27 op return 1;
237 7957cbd9 2021-01-27 op
238 7957cbd9 2021-01-27 op s = hostname;
239 7957cbd9 2021-01-27 op for (;;) {
240 415ac7a2 2021-01-28 op end = end_of_label(s);
241 0cd66754 2021-01-27 op l = end - s;
242 a2fd8013 2021-01-29 op if (l >= sizeof(label)) {
243 a2fd8013 2021-01-29 op *err = "label too long";
244 7957cbd9 2021-01-27 op return 0;
245 a2fd8013 2021-01-29 op }
246 7957cbd9 2021-01-27 op
247 35cf19e3 2021-01-28 op memcpy(label, s, l);
248 35cf19e3 2021-01-28 op label[l] = '\0';
249 7957cbd9 2021-01-27 op
250 a2fd8013 2021-01-29 op if (!decode(label, out, len, err))
251 7957cbd9 2021-01-27 op return 0;
252 7957cbd9 2021-01-27 op
253 7957cbd9 2021-01-27 op if (*end == '\0')
254 7957cbd9 2021-01-27 op return 1;
255 7957cbd9 2021-01-27 op
256 a2fd8013 2021-01-29 op if (strlcat(out, ".", len) >= len) {
257 a2fd8013 2021-01-29 op *err = "domain name too long";
258 7957cbd9 2021-01-27 op return 0;
259 a2fd8013 2021-01-29 op }
260 7957cbd9 2021-01-27 op
261 7957cbd9 2021-01-27 op l = strlen(out);
262 a2fd8013 2021-01-29 op if (l >= len) {
263 a2fd8013 2021-01-29 op *err = "domain name too long";
264 7957cbd9 2021-01-27 op return 0;
265 a2fd8013 2021-01-29 op }
266 7957cbd9 2021-01-27 op out += l;
267 7957cbd9 2021-01-27 op len -= l;
268 7957cbd9 2021-01-27 op
269 7957cbd9 2021-01-27 op s = end+1;
270 7957cbd9 2021-01-27 op }
271 7957cbd9 2021-01-27 op }