Blame


1 5546fdd5 2022-12-23 op /*
2 5546fdd5 2022-12-23 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 5546fdd5 2022-12-23 op *
4 5546fdd5 2022-12-23 op * Permission to use, copy, modify, and distribute this software for any
5 5546fdd5 2022-12-23 op * purpose with or without fee is hereby granted, provided that the above
6 5546fdd5 2022-12-23 op * copyright notice and this permission notice appear in all copies.
7 5546fdd5 2022-12-23 op *
8 5546fdd5 2022-12-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5546fdd5 2022-12-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5546fdd5 2022-12-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5546fdd5 2022-12-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5546fdd5 2022-12-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5546fdd5 2022-12-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5546fdd5 2022-12-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5546fdd5 2022-12-23 op */
16 5546fdd5 2022-12-23 op
17 5546fdd5 2022-12-23 op #include <ctype.h>
18 871b5bb7 2022-12-24 op #include <errno.h>
19 871b5bb7 2022-12-24 op #include <stddef.h>
20 5546fdd5 2022-12-23 op #include <stdint.h>
21 5546fdd5 2022-12-23 op #include <stdlib.h>
22 5546fdd5 2022-12-23 op #include <string.h>
23 5546fdd5 2022-12-23 op
24 5546fdd5 2022-12-23 op #include "iri.h"
25 5546fdd5 2022-12-23 op
26 5546fdd5 2022-12-23 op /* TODO: URI -> IRI. accept IRI but emit always URI */
27 5546fdd5 2022-12-23 op
28 5546fdd5 2022-12-23 op static inline int
29 5546fdd5 2022-12-23 op cpstr(const char *start, const char *till, char *buf, size_t len)
30 5546fdd5 2022-12-23 op {
31 5546fdd5 2022-12-23 op size_t slen = till - start;
32 5546fdd5 2022-12-23 op
33 5546fdd5 2022-12-23 op if (slen + 1 >= len)
34 5546fdd5 2022-12-23 op return (-1);
35 5546fdd5 2022-12-23 op memcpy(buf, start, slen);
36 5546fdd5 2022-12-23 op buf[slen] = '\0';
37 5546fdd5 2022-12-23 op return (0);
38 5546fdd5 2022-12-23 op }
39 5546fdd5 2022-12-23 op
40 5546fdd5 2022-12-23 op static inline int
41 5546fdd5 2022-12-23 op unreserved(int c)
42 5546fdd5 2022-12-23 op {
43 5546fdd5 2022-12-23 op return (isalnum((unsigned char)c) ||
44 5546fdd5 2022-12-23 op c == '-' ||
45 5546fdd5 2022-12-23 op c == '.' ||
46 5546fdd5 2022-12-23 op c == '_' ||
47 5546fdd5 2022-12-23 op c == '~');
48 5546fdd5 2022-12-23 op }
49 5546fdd5 2022-12-23 op
50 5546fdd5 2022-12-23 op static inline int
51 5546fdd5 2022-12-23 op pctenc(const char *s)
52 5546fdd5 2022-12-23 op {
53 5546fdd5 2022-12-23 op const char *t = s;
54 5546fdd5 2022-12-23 op
55 5546fdd5 2022-12-23 op return (t[0] == '%' &&
56 5546fdd5 2022-12-23 op isxdigit((unsigned char)t[1]) &&
57 5546fdd5 2022-12-23 op isxdigit((unsigned char)t[2]));
58 5546fdd5 2022-12-23 op }
59 5546fdd5 2022-12-23 op
60 5546fdd5 2022-12-23 op static inline int
61 5546fdd5 2022-12-23 op sub_delims(int c)
62 5546fdd5 2022-12-23 op {
63 5546fdd5 2022-12-23 op return (c == '!' || c == '$' || c == '&' || c == '\'' ||
64 5546fdd5 2022-12-23 op c == '(' || c == ')' || c == '*' || c == '+' || c == ',' ||
65 5546fdd5 2022-12-23 op c == ';' || c == '=');
66 5546fdd5 2022-12-23 op }
67 5546fdd5 2022-12-23 op
68 5546fdd5 2022-12-23 op static inline const char *
69 5546fdd5 2022-12-23 op advance_pchar(const char *s)
70 5546fdd5 2022-12-23 op {
71 5546fdd5 2022-12-23 op if (unreserved(*s) || sub_delims(*s) || *s == ':' || *s == '@')
72 5546fdd5 2022-12-23 op return (s + 1);
73 5546fdd5 2022-12-23 op if (pctenc(s))
74 5546fdd5 2022-12-23 op return (s + 3);
75 5546fdd5 2022-12-23 op return (NULL);
76 5546fdd5 2022-12-23 op }
77 5546fdd5 2022-12-23 op
78 5546fdd5 2022-12-23 op static inline const char *
79 5546fdd5 2022-12-23 op advance_segment(const char *s)
80 5546fdd5 2022-12-23 op {
81 5546fdd5 2022-12-23 op const char *t = s;
82 5546fdd5 2022-12-23 op
83 5546fdd5 2022-12-23 op while ((t = advance_pchar(s)) != NULL)
84 5546fdd5 2022-12-23 op s = t;
85 5546fdd5 2022-12-23 op return (s);
86 5546fdd5 2022-12-23 op }
87 5546fdd5 2022-12-23 op
88 5546fdd5 2022-12-23 op static inline const char *
89 5546fdd5 2022-12-23 op advance_segment_nz(const char *s)
90 5546fdd5 2022-12-23 op {
91 5546fdd5 2022-12-23 op const char *t;
92 5546fdd5 2022-12-23 op
93 5546fdd5 2022-12-23 op if ((t = advance_pchar(s)) == NULL)
94 5546fdd5 2022-12-23 op return (NULL);
95 5546fdd5 2022-12-23 op return (advance_segment(t));
96 5546fdd5 2022-12-23 op }
97 5546fdd5 2022-12-23 op
98 5546fdd5 2022-12-23 op static inline const char *
99 5546fdd5 2022-12-23 op advance_segment_nz_nc(const char *s)
100 5546fdd5 2022-12-23 op {
101 5546fdd5 2022-12-23 op const char *t = s;
102 5546fdd5 2022-12-23 op
103 5546fdd5 2022-12-23 op for (;;) {
104 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == '@')
105 5546fdd5 2022-12-23 op t++;
106 5546fdd5 2022-12-23 op else if (pctenc(t))
107 5546fdd5 2022-12-23 op t += 3;
108 5546fdd5 2022-12-23 op else
109 5546fdd5 2022-12-23 op break;
110 5546fdd5 2022-12-23 op }
111 5546fdd5 2022-12-23 op
112 5546fdd5 2022-12-23 op return (t != s ? t : NULL);
113 5546fdd5 2022-12-23 op }
114 5546fdd5 2022-12-23 op
115 5546fdd5 2022-12-23 op static const char *
116 5546fdd5 2022-12-23 op parse_scheme(const char *s, struct iri *iri)
117 5546fdd5 2022-12-23 op {
118 5546fdd5 2022-12-23 op const char *t = s;
119 5546fdd5 2022-12-23 op
120 5546fdd5 2022-12-23 op if (!isalpha((unsigned char)*t))
121 5546fdd5 2022-12-23 op return (NULL);
122 5546fdd5 2022-12-23 op
123 5546fdd5 2022-12-23 op while (isalnum((unsigned char)*t) ||
124 5546fdd5 2022-12-23 op *t == '+' ||
125 5546fdd5 2022-12-23 op *t == '-' ||
126 5546fdd5 2022-12-23 op *t == '.')
127 5546fdd5 2022-12-23 op t++;
128 5546fdd5 2022-12-23 op
129 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_scheme, sizeof(iri->iri_scheme)) == -1)
130 5546fdd5 2022-12-23 op return (NULL);
131 5546fdd5 2022-12-23 op
132 5546fdd5 2022-12-23 op iri->iri_flags |= IH_SCHEME;
133 5546fdd5 2022-12-23 op return (t);
134 5546fdd5 2022-12-23 op }
135 5546fdd5 2022-12-23 op
136 5546fdd5 2022-12-23 op /* userinfo is always optional */
137 5546fdd5 2022-12-23 op static const char *
138 5546fdd5 2022-12-23 op parse_uinfo(const char *s, struct iri *iri)
139 5546fdd5 2022-12-23 op {
140 5546fdd5 2022-12-23 op const char *t = s;
141 5546fdd5 2022-12-23 op
142 5546fdd5 2022-12-23 op for (;;) {
143 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == ':')
144 5546fdd5 2022-12-23 op t++;
145 5546fdd5 2022-12-23 op else if (pctenc(t))
146 5546fdd5 2022-12-23 op t += 3;
147 5546fdd5 2022-12-23 op else
148 5546fdd5 2022-12-23 op break;
149 5546fdd5 2022-12-23 op }
150 5546fdd5 2022-12-23 op
151 5546fdd5 2022-12-23 op if (*t != '@')
152 5546fdd5 2022-12-23 op return (s);
153 5546fdd5 2022-12-23 op
154 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_uinfo, sizeof(iri->iri_uinfo)) == -1)
155 5546fdd5 2022-12-23 op return (NULL);
156 5546fdd5 2022-12-23 op iri->iri_flags |= IH_UINFO;
157 5546fdd5 2022-12-23 op return (t + 1);
158 5546fdd5 2022-12-23 op }
159 5546fdd5 2022-12-23 op
160 5546fdd5 2022-12-23 op static const char *
161 5546fdd5 2022-12-23 op parse_host(const char *s, struct iri *iri)
162 5546fdd5 2022-12-23 op {
163 5546fdd5 2022-12-23 op const char *t = s;
164 5546fdd5 2022-12-23 op
165 5546fdd5 2022-12-23 op /*
166 5546fdd5 2022-12-23 op * cheating a bit by relaxing and merging the rule for
167 5546fdd5 2022-12-23 op * IPv6address and IPvFuture and by merging IPv4address and
168 5546fdd5 2022-12-23 op * reg-name.
169 5546fdd5 2022-12-23 op */
170 5546fdd5 2022-12-23 op
171 5546fdd5 2022-12-23 op if (*t == '[') {
172 5546fdd5 2022-12-23 op while (*t && *t != ']')
173 5546fdd5 2022-12-23 op ++t;
174 5546fdd5 2022-12-23 op if (*t == '\0')
175 5546fdd5 2022-12-23 op return (NULL);
176 5546fdd5 2022-12-23 op t++;
177 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_host, sizeof(iri->iri_host)) == -1)
178 5546fdd5 2022-12-23 op return (NULL);
179 5546fdd5 2022-12-23 op iri->iri_flags |= IH_HOST;
180 5546fdd5 2022-12-23 op return (t);
181 5546fdd5 2022-12-23 op }
182 5546fdd5 2022-12-23 op
183 5546fdd5 2022-12-23 op for (;;) {
184 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t))
185 5546fdd5 2022-12-23 op t++;
186 5546fdd5 2022-12-23 op else if (pctenc(t))
187 5546fdd5 2022-12-23 op t += 3;
188 5546fdd5 2022-12-23 op else
189 5546fdd5 2022-12-23 op break;
190 5546fdd5 2022-12-23 op }
191 5546fdd5 2022-12-23 op
192 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_host, sizeof(iri->iri_host)) == -1)
193 5546fdd5 2022-12-23 op return (NULL);
194 5546fdd5 2022-12-23 op iri->iri_flags |= IH_HOST;
195 5546fdd5 2022-12-23 op return (t);
196 5546fdd5 2022-12-23 op }
197 5546fdd5 2022-12-23 op
198 5546fdd5 2022-12-23 op static const char *
199 5546fdd5 2022-12-23 op parse_port(const char *s, struct iri *iri)
200 5546fdd5 2022-12-23 op {
201 5546fdd5 2022-12-23 op const char *t = s;
202 5546fdd5 2022-12-23 op const char *errstr;
203 5546fdd5 2022-12-23 op
204 5546fdd5 2022-12-23 op while (isdigit((unsigned char)*t))
205 5546fdd5 2022-12-23 op t++;
206 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_portstr, sizeof(iri->iri_portstr)) == -1)
207 5546fdd5 2022-12-23 op return (NULL);
208 5546fdd5 2022-12-23 op iri->iri_port = strtonum(iri->iri_portstr, 1, UINT16_MAX, &errstr);
209 5546fdd5 2022-12-23 op if (errstr)
210 5546fdd5 2022-12-23 op return (NULL);
211 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PORT;
212 5546fdd5 2022-12-23 op return (t);
213 5546fdd5 2022-12-23 op }
214 5546fdd5 2022-12-23 op
215 5546fdd5 2022-12-23 op static const char *
216 5546fdd5 2022-12-23 op parse_authority(const char *s, struct iri *iri)
217 5546fdd5 2022-12-23 op {
218 5546fdd5 2022-12-23 op const char *t;
219 5546fdd5 2022-12-23 op
220 5546fdd5 2022-12-23 op if ((t = parse_uinfo(s, iri)) == NULL)
221 5546fdd5 2022-12-23 op return (NULL);
222 5546fdd5 2022-12-23 op
223 5546fdd5 2022-12-23 op if ((t = parse_host(t, iri)) == NULL)
224 5546fdd5 2022-12-23 op return (NULL);
225 5546fdd5 2022-12-23 op
226 5546fdd5 2022-12-23 op if (*t == ':')
227 5546fdd5 2022-12-23 op return (parse_port(t, iri));
228 5546fdd5 2022-12-23 op
229 5546fdd5 2022-12-23 op return (t);
230 5546fdd5 2022-12-23 op }
231 5546fdd5 2022-12-23 op
232 5546fdd5 2022-12-23 op static const char *
233 5546fdd5 2022-12-23 op parse_path_abempty(const char *s, struct iri *iri)
234 5546fdd5 2022-12-23 op {
235 5546fdd5 2022-12-23 op const char *t = s;
236 5546fdd5 2022-12-23 op
237 5546fdd5 2022-12-23 op while (*t == '/')
238 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
239 5546fdd5 2022-12-23 op
240 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
241 5546fdd5 2022-12-23 op return (NULL);
242 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
243 5546fdd5 2022-12-23 op return (t);
244 5546fdd5 2022-12-23 op }
245 5546fdd5 2022-12-23 op
246 5546fdd5 2022-12-23 op static const char *
247 5546fdd5 2022-12-23 op parse_path_absolute(const char *s, struct iri *iri)
248 5546fdd5 2022-12-23 op {
249 5546fdd5 2022-12-23 op const char *t;
250 5546fdd5 2022-12-23 op
251 5546fdd5 2022-12-23 op if (*s != '/')
252 5546fdd5 2022-12-23 op return (NULL);
253 5546fdd5 2022-12-23 op
254 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s + 1)) == NULL)
255 5546fdd5 2022-12-23 op return (s + 1);
256 5546fdd5 2022-12-23 op
257 5546fdd5 2022-12-23 op while (*t == '/')
258 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
259 5546fdd5 2022-12-23 op
260 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
261 5546fdd5 2022-12-23 op return (NULL);
262 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
263 5546fdd5 2022-12-23 op return (t);
264 5546fdd5 2022-12-23 op }
265 5546fdd5 2022-12-23 op
266 5546fdd5 2022-12-23 op static const char *
267 5546fdd5 2022-12-23 op parse_path_rootless(const char *s, struct iri *iri)
268 5546fdd5 2022-12-23 op {
269 5546fdd5 2022-12-23 op const char *t;
270 5546fdd5 2022-12-23 op
271 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s)) == NULL)
272 5546fdd5 2022-12-23 op return (NULL);
273 5546fdd5 2022-12-23 op
274 5546fdd5 2022-12-23 op while (*t == '/')
275 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
276 5546fdd5 2022-12-23 op
277 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
278 5546fdd5 2022-12-23 op return (NULL);
279 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
280 5546fdd5 2022-12-23 op return (t);
281 5546fdd5 2022-12-23 op }
282 5546fdd5 2022-12-23 op
283 5546fdd5 2022-12-23 op static const char *
284 5546fdd5 2022-12-23 op parse_path_noscheme(const char *s, struct iri *iri)
285 5546fdd5 2022-12-23 op {
286 5546fdd5 2022-12-23 op const char *t;
287 5546fdd5 2022-12-23 op
288 5546fdd5 2022-12-23 op if ((t = advance_segment_nz_nc(s)) == NULL)
289 5546fdd5 2022-12-23 op return (NULL);
290 5546fdd5 2022-12-23 op
291 5546fdd5 2022-12-23 op while (*t == '/')
292 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
293 5546fdd5 2022-12-23 op
294 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
295 5546fdd5 2022-12-23 op return (NULL);
296 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
297 5546fdd5 2022-12-23 op return (t);
298 5546fdd5 2022-12-23 op }
299 5546fdd5 2022-12-23 op
300 5546fdd5 2022-12-23 op static const char *
301 5546fdd5 2022-12-23 op parse_path_empty(const char *s, struct iri *iri)
302 5546fdd5 2022-12-23 op {
303 5546fdd5 2022-12-23 op iri->iri_path[0] = '\0';
304 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
305 5546fdd5 2022-12-23 op return (s);
306 5546fdd5 2022-12-23 op }
307 5546fdd5 2022-12-23 op
308 5546fdd5 2022-12-23 op static const char *
309 5546fdd5 2022-12-23 op parse_hier(const char *s, struct iri *iri)
310 5546fdd5 2022-12-23 op {
311 5546fdd5 2022-12-23 op const char *t;
312 5546fdd5 2022-12-23 op
313 5546fdd5 2022-12-23 op if (!strncmp(s, "//", 2)) {
314 5546fdd5 2022-12-23 op if ((t = parse_authority(s + 2, iri)) == NULL)
315 5546fdd5 2022-12-23 op return (NULL);
316 5546fdd5 2022-12-23 op return (parse_path_abempty(t, iri));
317 5546fdd5 2022-12-23 op }
318 5546fdd5 2022-12-23 op
319 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
320 5546fdd5 2022-12-23 op return (t);
321 5546fdd5 2022-12-23 op
322 5546fdd5 2022-12-23 op if ((t = parse_path_rootless(s, iri)) != NULL)
323 5546fdd5 2022-12-23 op return (t);
324 5546fdd5 2022-12-23 op
325 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
326 5546fdd5 2022-12-23 op }
327 5546fdd5 2022-12-23 op
328 5546fdd5 2022-12-23 op static const char *
329 5546fdd5 2022-12-23 op parse_relative(const char *s, struct iri *iri)
330 5546fdd5 2022-12-23 op {
331 5546fdd5 2022-12-23 op const char *t = s;
332 5546fdd5 2022-12-23 op
333 5546fdd5 2022-12-23 op if (!strncmp(s, "//", 2)) {
334 5546fdd5 2022-12-23 op if ((t = parse_authority(s + 2, iri)) == NULL)
335 5546fdd5 2022-12-23 op return (NULL);
336 5546fdd5 2022-12-23 op return (parse_path_abempty(t, iri));
337 5546fdd5 2022-12-23 op }
338 5546fdd5 2022-12-23 op
339 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
340 5546fdd5 2022-12-23 op return (t);
341 5546fdd5 2022-12-23 op
342 5546fdd5 2022-12-23 op if ((t = parse_path_noscheme(s, iri)) != NULL)
343 5546fdd5 2022-12-23 op return (t);
344 5546fdd5 2022-12-23 op
345 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
346 5546fdd5 2022-12-23 op }
347 5546fdd5 2022-12-23 op
348 5546fdd5 2022-12-23 op static const char *
349 5546fdd5 2022-12-23 op parse_query(const char *s, struct iri *iri)
350 5546fdd5 2022-12-23 op {
351 5546fdd5 2022-12-23 op const char *n, *t = s;
352 5546fdd5 2022-12-23 op
353 5546fdd5 2022-12-23 op for (;;) {
354 5546fdd5 2022-12-23 op if ((n = advance_pchar(t)) != NULL)
355 5546fdd5 2022-12-23 op t = n;
356 5546fdd5 2022-12-23 op else if (*t == '/' || *t == '?')
357 5546fdd5 2022-12-23 op t++;
358 5546fdd5 2022-12-23 op else
359 5546fdd5 2022-12-23 op break;
360 5546fdd5 2022-12-23 op }
361 5546fdd5 2022-12-23 op
362 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_query, sizeof(iri->iri_query)) == -1)
363 5546fdd5 2022-12-23 op return (NULL);
364 5546fdd5 2022-12-23 op iri->iri_flags |= IH_QUERY;
365 5546fdd5 2022-12-23 op return (t);
366 5546fdd5 2022-12-23 op }
367 5546fdd5 2022-12-23 op
368 5546fdd5 2022-12-23 op static int
369 5546fdd5 2022-12-23 op parse_uri(const char *s, struct iri *iri)
370 5546fdd5 2022-12-23 op {
371 5546fdd5 2022-12-23 op if ((s = parse_scheme(s, iri)) == NULL)
372 5546fdd5 2022-12-23 op return (-1);
373 5546fdd5 2022-12-23 op
374 5546fdd5 2022-12-23 op if (*s != ':')
375 5546fdd5 2022-12-23 op return (-1);
376 5546fdd5 2022-12-23 op
377 5546fdd5 2022-12-23 op if ((s = parse_hier(s + 1, iri)) == NULL)
378 5546fdd5 2022-12-23 op return (-1);
379 5546fdd5 2022-12-23 op
380 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
381 5546fdd5 2022-12-23 op return (-1);
382 5546fdd5 2022-12-23 op
383 5546fdd5 2022-12-23 op /* skip fragments */
384 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
385 5546fdd5 2022-12-23 op return (0);
386 5546fdd5 2022-12-23 op
387 5546fdd5 2022-12-23 op return (-1);
388 5546fdd5 2022-12-23 op }
389 5546fdd5 2022-12-23 op
390 5546fdd5 2022-12-23 op static int
391 5546fdd5 2022-12-23 op parse_relative_ref(const char *s, struct iri *iri)
392 5546fdd5 2022-12-23 op {
393 5546fdd5 2022-12-23 op if ((s = parse_relative(s, iri)) == NULL)
394 5546fdd5 2022-12-23 op return (-1);
395 5546fdd5 2022-12-23 op
396 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
397 5546fdd5 2022-12-23 op return (-1);
398 5546fdd5 2022-12-23 op
399 5546fdd5 2022-12-23 op /* skip fragments */
400 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
401 5546fdd5 2022-12-23 op return (0);
402 5546fdd5 2022-12-23 op
403 5546fdd5 2022-12-23 op return (-1);
404 5546fdd5 2022-12-23 op }
405 5546fdd5 2022-12-23 op
406 5546fdd5 2022-12-23 op static int
407 5546fdd5 2022-12-23 op parse(const char *s, struct iri *iri)
408 5546fdd5 2022-12-23 op {
409 5546fdd5 2022-12-23 op iri->iri_flags = 0;
410 5546fdd5 2022-12-23 op
411 5546fdd5 2022-12-23 op if (s == NULL)
412 5546fdd5 2022-12-23 op return (0);
413 5546fdd5 2022-12-23 op
414 5546fdd5 2022-12-23 op if (parse_uri(s, iri) == -1) {
415 5546fdd5 2022-12-23 op iri->iri_flags = 0;
416 5546fdd5 2022-12-23 op if (parse_relative_ref(s, iri) == -1)
417 5546fdd5 2022-12-23 op return (-1);
418 5546fdd5 2022-12-23 op }
419 5546fdd5 2022-12-23 op
420 5546fdd5 2022-12-23 op return (0);
421 5546fdd5 2022-12-23 op }
422 5546fdd5 2022-12-23 op
423 51762770 2022-12-24 op static inline void
424 51762770 2022-12-24 op lowerify(char *s)
425 51762770 2022-12-24 op {
426 51762770 2022-12-24 op for (; *s; ++s)
427 51762770 2022-12-24 op *s = tolower((unsigned char)*s);
428 51762770 2022-12-24 op }
429 51762770 2022-12-24 op
430 5546fdd5 2022-12-23 op static void
431 5546fdd5 2022-12-23 op cpfields(struct iri *dest, const struct iri *src, int flags)
432 5546fdd5 2022-12-23 op {
433 5546fdd5 2022-12-23 op if (flags & IH_SCHEME) {
434 5546fdd5 2022-12-23 op dest->iri_flags |= IH_SCHEME;
435 5546fdd5 2022-12-23 op if (src->iri_flags & IH_SCHEME)
436 5546fdd5 2022-12-23 op memcpy(dest->iri_scheme, src->iri_scheme,
437 5546fdd5 2022-12-23 op sizeof(dest->iri_scheme));
438 51762770 2022-12-24 op lowerify(dest->iri_scheme);
439 5546fdd5 2022-12-23 op }
440 5546fdd5 2022-12-23 op if (flags & IH_UINFO) {
441 997b9cd0 2022-12-24 op if (src->iri_flags & IH_UINFO) {
442 5546fdd5 2022-12-23 op memcpy(dest->iri_uinfo, src->iri_uinfo,
443 5546fdd5 2022-12-23 op sizeof(dest->iri_uinfo));
444 997b9cd0 2022-12-24 op dest->iri_flags |= IH_UINFO;
445 997b9cd0 2022-12-24 op }
446 5546fdd5 2022-12-23 op }
447 5546fdd5 2022-12-23 op if (flags & IH_HOST) {
448 5546fdd5 2022-12-23 op dest->iri_flags |= IH_HOST;
449 5546fdd5 2022-12-23 op if (src->iri_flags & IH_HOST)
450 5546fdd5 2022-12-23 op memcpy(dest->iri_host, src->iri_host,
451 5546fdd5 2022-12-23 op sizeof(dest->iri_host));
452 51762770 2022-12-24 op lowerify(dest->iri_host);
453 5546fdd5 2022-12-23 op }
454 5546fdd5 2022-12-23 op if (flags & IH_PORT) {
455 997b9cd0 2022-12-24 op if (src->iri_flags & IH_PORT) {
456 5546fdd5 2022-12-23 op dest->iri_port = src->iri_port;
457 997b9cd0 2022-12-24 op dest->iri_flags |= IH_PORT;
458 997b9cd0 2022-12-24 op }
459 5546fdd5 2022-12-23 op }
460 5546fdd5 2022-12-23 op if (flags & IH_PATH) {
461 5546fdd5 2022-12-23 op dest->iri_flags |= IH_PATH;
462 5546fdd5 2022-12-23 op if (src->iri_flags & IH_PATH)
463 5546fdd5 2022-12-23 op memcpy(dest->iri_path, src->iri_path,
464 5546fdd5 2022-12-23 op sizeof(dest->iri_path));
465 5546fdd5 2022-12-23 op }
466 5546fdd5 2022-12-23 op if (flags & IH_QUERY) {
467 997b9cd0 2022-12-24 op if (src->iri_flags & IH_QUERY) {
468 997b9cd0 2022-12-24 op dest->iri_flags |= IH_QUERY;
469 5546fdd5 2022-12-23 op memcpy(dest->iri_query, src->iri_query,
470 5546fdd5 2022-12-23 op sizeof(dest->iri_query));
471 997b9cd0 2022-12-24 op }
472 5546fdd5 2022-12-23 op }
473 5546fdd5 2022-12-23 op }
474 5546fdd5 2022-12-23 op
475 871b5bb7 2022-12-24 op static inline int
476 8b2b06b5 2022-12-25 op remove_dot_segments(struct iri *i)
477 5546fdd5 2022-12-23 op {
478 8b2b06b5 2022-12-25 op char *p, *q, *buf;
479 871b5bb7 2022-12-24 op ptrdiff_t bufsize;
480 5546fdd5 2022-12-23 op
481 8b2b06b5 2022-12-25 op buf = p = q = i->iri_path;
482 8b2b06b5 2022-12-25 op bufsize = sizeof(i->iri_path);
483 871b5bb7 2022-12-24 op while (*p && (q - buf < bufsize)) {
484 8b2b06b5 2022-12-25 op if (p[0] == '/' && p[1] == '.' &&
485 8b2b06b5 2022-12-25 op (p[2] == '/' || p[2] == '\0')) {
486 871b5bb7 2022-12-24 op p += 2;
487 8b2b06b5 2022-12-25 op if (*p != '/')
488 8b2b06b5 2022-12-25 op *q++ = '/';
489 8b2b06b5 2022-12-25 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
490 871b5bb7 2022-12-24 op (p[3] == '/' || p[3] == '\0')) {
491 871b5bb7 2022-12-24 op p += 3;
492 8b2b06b5 2022-12-25 op while (q > buf && *--q != '/')
493 871b5bb7 2022-12-24 op continue;
494 8b2b06b5 2022-12-25 op if (*p != '/' && (q > buf && q[-1] != '/'))
495 8b2b06b5 2022-12-25 op *q++ = '/';
496 8b2b06b5 2022-12-25 op } else
497 871b5bb7 2022-12-24 op *q++ = *p++;
498 871b5bb7 2022-12-24 op }
499 8b2b06b5 2022-12-25 op if ((*p == '\0') && (q - buf < bufsize)) {
500 871b5bb7 2022-12-24 op *q = '\0';
501 871b5bb7 2022-12-24 op return (0);
502 871b5bb7 2022-12-24 op }
503 871b5bb7 2022-12-24 op
504 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
505 871b5bb7 2022-12-24 op return (-1);
506 871b5bb7 2022-12-24 op }
507 871b5bb7 2022-12-24 op
508 871b5bb7 2022-12-24 op static inline int
509 871b5bb7 2022-12-24 op mergepath(struct iri *i, struct iri *base, struct iri *r)
510 5546fdd5 2022-12-23 op {
511 871b5bb7 2022-12-24 op const char *bpath, *rpath, *s;
512 871b5bb7 2022-12-24 op
513 871b5bb7 2022-12-24 op bpath = (base->iri_flags & IH_PATH) ? base->iri_path : "/";
514 871b5bb7 2022-12-24 op rpath = (r->iri_flags & IH_PATH) ? r->iri_path : "/";
515 871b5bb7 2022-12-24 op
516 871b5bb7 2022-12-24 op i->iri_flags |= IH_PATH;
517 871b5bb7 2022-12-24 op i->iri_path[0] = '\0';
518 871b5bb7 2022-12-24 op
519 871b5bb7 2022-12-24 op if ((base->iri_flags & IH_AUTHORITY) &&
520 871b5bb7 2022-12-24 op (*bpath == '\0' || !strcmp(bpath, "/"))) {
521 871b5bb7 2022-12-24 op if (*rpath == '/')
522 871b5bb7 2022-12-24 op rpath++;
523 871b5bb7 2022-12-24 op strlcpy(i->iri_path, "/", sizeof(i->iri_path));
524 871b5bb7 2022-12-24 op strlcat(i->iri_path, rpath, sizeof(i->iri_path));
525 871b5bb7 2022-12-24 op return (0);
526 871b5bb7 2022-12-24 op }
527 871b5bb7 2022-12-24 op
528 871b5bb7 2022-12-24 op if ((s = strrchr(bpath, '/')) != NULL) {
529 871b5bb7 2022-12-24 op cpstr(bpath, s + 1, i->iri_path, sizeof(i->iri_path));
530 871b5bb7 2022-12-24 op if (*rpath == '/')
531 871b5bb7 2022-12-24 op rpath++;
532 871b5bb7 2022-12-24 op }
533 871b5bb7 2022-12-24 op if (strlcat(i->iri_path, rpath, sizeof(i->iri_path)) >=
534 871b5bb7 2022-12-24 op sizeof(i->iri_path)) {
535 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
536 871b5bb7 2022-12-24 op return (-1);
537 871b5bb7 2022-12-24 op }
538 871b5bb7 2022-12-24 op
539 871b5bb7 2022-12-24 op return (0);
540 5546fdd5 2022-12-23 op }
541 5546fdd5 2022-12-23 op
542 5546fdd5 2022-12-23 op int
543 5546fdd5 2022-12-23 op iri_parse(const char *base, const char *str, struct iri *iri)
544 5546fdd5 2022-12-23 op {
545 5546fdd5 2022-12-23 op static struct iri ibase, iparsed;
546 5546fdd5 2022-12-23 op
547 5546fdd5 2022-12-23 op memset(iri, 0, sizeof(*iri));
548 5546fdd5 2022-12-23 op
549 5546fdd5 2022-12-23 op if (base == NULL) {
550 5546fdd5 2022-12-23 op ibase.iri_flags = 0;
551 f5bc8482 2022-12-24 op if (parse_uri(str, &iparsed) == -1) {
552 f5bc8482 2022-12-24 op errno = EINVAL;
553 5546fdd5 2022-12-23 op return (-1);
554 f5bc8482 2022-12-24 op }
555 5546fdd5 2022-12-23 op } else {
556 f5bc8482 2022-12-24 op if (parse_uri(base, &ibase) == -1 ||
557 f5bc8482 2022-12-24 op parse(str, &iparsed) == -1) {
558 f5bc8482 2022-12-24 op errno = EINVAL;
559 5546fdd5 2022-12-23 op return (-1);
560 f5bc8482 2022-12-24 op }
561 5546fdd5 2022-12-23 op }
562 5546fdd5 2022-12-23 op
563 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_SCHEME) {
564 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, iparsed.iri_flags);
565 5546fdd5 2022-12-23 op remove_dot_segments(iri);
566 5546fdd5 2022-12-23 op return (0);
567 5546fdd5 2022-12-23 op }
568 5546fdd5 2022-12-23 op
569 5546fdd5 2022-12-23 op /* if fragments are supported, copy iparsed fragment to iri */
570 5546fdd5 2022-12-23 op
571 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_SCHEME);
572 5546fdd5 2022-12-23 op
573 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_HOST) {
574 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_AUTHORITY|IH_PATH|IH_QUERY);
575 5546fdd5 2022-12-23 op remove_dot_segments(iri);
576 5546fdd5 2022-12-23 op return (0);
577 5546fdd5 2022-12-23 op }
578 5546fdd5 2022-12-23 op
579 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_AUTHORITY);
580 5546fdd5 2022-12-23 op
581 5546fdd5 2022-12-23 op if ((iparsed.iri_flags & IH_PATH) && *iparsed.iri_path == '\0') {
582 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_PATH);
583 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_QUERY)
584 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
585 5546fdd5 2022-12-23 op else
586 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_QUERY);
587 5546fdd5 2022-12-23 op return (0);
588 5546fdd5 2022-12-23 op }
589 5546fdd5 2022-12-23 op
590 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
591 5cb6cd4d 2022-12-24 op if ((iparsed.iri_flags & IH_PATH) && *iparsed.iri_path == '/')
592 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_PATH);
593 5546fdd5 2022-12-23 op else {
594 5546fdd5 2022-12-23 op if (!(ibase.iri_flags & IH_PATH))
595 5546fdd5 2022-12-23 op ibase.iri_path[0] = '\0';
596 5546fdd5 2022-12-23 op if (!(iparsed.iri_flags & IH_PATH))
597 5546fdd5 2022-12-23 op iparsed.iri_path[0] = '\0';
598 f5bc8482 2022-12-24 op if (mergepath(iri, &ibase, &iparsed) == -1)
599 f5bc8482 2022-12-24 op return (-1);
600 5546fdd5 2022-12-23 op }
601 f5bc8482 2022-12-24 op if (remove_dot_segments(iri) == -1)
602 f5bc8482 2022-12-24 op return (-1);
603 5546fdd5 2022-12-23 op return (0);
604 5546fdd5 2022-12-23 op }
605 5546fdd5 2022-12-23 op
606 5546fdd5 2022-12-23 op int
607 d59fad58 2022-12-24 op iri_unparse(const struct iri *i, char *buf, size_t buflen)
608 5546fdd5 2022-12-23 op {
609 d59fad58 2022-12-24 op if (buflen == 0)
610 d59fad58 2022-12-24 op goto err;
611 d59fad58 2022-12-24 op
612 d59fad58 2022-12-24 op /* TODO: should %enc octets if needed */
613 d59fad58 2022-12-24 op
614 d59fad58 2022-12-24 op buf[0] = '\0';
615 d59fad58 2022-12-24 op
616 d59fad58 2022-12-24 op if (i->iri_flags & IH_SCHEME) {
617 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_scheme, buflen) >= buflen ||
618 d59fad58 2022-12-24 op strlcat(buf, ":", buflen) >= buflen)
619 d59fad58 2022-12-24 op goto err;
620 d59fad58 2022-12-24 op }
621 d59fad58 2022-12-24 op
622 d59fad58 2022-12-24 op if (i->iri_flags & IH_AUTHORITY) {
623 d59fad58 2022-12-24 op if (strlcat(buf, "//", buflen) >= buflen)
624 d59fad58 2022-12-24 op goto err;
625 d59fad58 2022-12-24 op }
626 d59fad58 2022-12-24 op
627 d59fad58 2022-12-24 op if (i->iri_flags & IH_UINFO) {
628 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_uinfo, buflen) >= buflen ||
629 d59fad58 2022-12-24 op strlcat(buf, "@", buflen) >= buflen)
630 d59fad58 2022-12-24 op goto err;
631 d59fad58 2022-12-24 op }
632 d59fad58 2022-12-24 op if (i->iri_flags & IH_HOST) {
633 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_host, buflen) >= buflen)
634 d59fad58 2022-12-24 op goto err;
635 d59fad58 2022-12-24 op }
636 d59fad58 2022-12-24 op if (i->iri_flags & IH_PORT) {
637 d59fad58 2022-12-24 op if (strlcat(buf, ":", buflen) >= buflen ||
638 d59fad58 2022-12-24 op strlcat(buf, i->iri_portstr, buflen) >= buflen)
639 d59fad58 2022-12-24 op goto err;
640 d59fad58 2022-12-24 op }
641 d59fad58 2022-12-24 op
642 d59fad58 2022-12-24 op if (i->iri_flags & IH_PATH) {
643 a655e85a 2022-12-24 op if (i->iri_flags & IH_AUTHORITY &&
644 d59fad58 2022-12-24 op i->iri_path[0] != '/' &&
645 d59fad58 2022-12-24 op strlcat(buf, "/", buflen) >= buflen)
646 d59fad58 2022-12-24 op goto err;
647 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_path, buflen) >= buflen)
648 d59fad58 2022-12-24 op goto err;
649 d59fad58 2022-12-24 op }
650 d59fad58 2022-12-24 op
651 d59fad58 2022-12-24 op if (i->iri_flags & IH_QUERY) {
652 d59fad58 2022-12-24 op if (strlcat(buf, "?", buflen) >= buflen ||
653 d59fad58 2022-12-24 op strlcat(buf, i->iri_query, buflen) >= buflen)
654 d59fad58 2022-12-24 op goto err;
655 d59fad58 2022-12-24 op }
656 d59fad58 2022-12-24 op
657 d59fad58 2022-12-24 op return (0);
658 d59fad58 2022-12-24 op
659 d59fad58 2022-12-24 op err:
660 d59fad58 2022-12-24 op errno = ENOBUFS;
661 5546fdd5 2022-12-23 op return (-1);
662 5546fdd5 2022-12-23 op }
663 5546fdd5 2022-12-23 op
664 5546fdd5 2022-12-23 op int
665 5546fdd5 2022-12-23 op iri_human(const struct iri *iri, char *buf, size_t buflen)
666 5546fdd5 2022-12-23 op {
667 5546fdd5 2022-12-23 op memset(buf, 0, buflen);
668 5546fdd5 2022-12-23 op return (-1);
669 5546fdd5 2022-12-23 op }
670 5546fdd5 2022-12-23 op
671 5546fdd5 2022-12-23 op int
672 5546fdd5 2022-12-23 op iri_setquery(struct iri *iri, const char *text)
673 5546fdd5 2022-12-23 op {
674 5546fdd5 2022-12-23 op return (-1);
675 5546fdd5 2022-12-23 op }