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