2 5546fdd5 2022-12-23 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
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>
24 5546fdd5 2022-12-23 op #include "iri.h"
26 5546fdd5 2022-12-23 op /* TODO: URI -> IRI. accept IRI but emit always URI */
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)
31 5546fdd5 2022-12-23 op size_t slen = till - start;
33 5546fdd5 2022-12-23 op if (slen + 1 >= len)
35 5546fdd5 2022-12-23 op memcpy(buf, start, slen);
36 5546fdd5 2022-12-23 op buf[slen] = '\0';
40 5546fdd5 2022-12-23 op static inline int
41 5546fdd5 2022-12-23 op unreserved(int c)
43 5546fdd5 2022-12-23 op return (isalnum((unsigned char)c) ||
50 5546fdd5 2022-12-23 op static inline int
51 5546fdd5 2022-12-23 op pctenc(const char *s)
53 5546fdd5 2022-12-23 op const char *t = s;
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]));
60 5546fdd5 2022-12-23 op static inline int
61 5546fdd5 2022-12-23 op sub_delims(int c)
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 == '=');
68 5546fdd5 2022-12-23 op static inline const char *
69 5546fdd5 2022-12-23 op advance_pchar(const char *s)
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);
78 5546fdd5 2022-12-23 op static inline const char *
79 5546fdd5 2022-12-23 op advance_segment(const char *s)
81 5546fdd5 2022-12-23 op const char *t = s;
83 5546fdd5 2022-12-23 op while ((t = advance_pchar(s)) != NULL)
88 5546fdd5 2022-12-23 op static inline const char *
89 5546fdd5 2022-12-23 op advance_segment_nz(const char *s)
91 5546fdd5 2022-12-23 op const char *t;
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));
98 5546fdd5 2022-12-23 op static inline const char *
99 5546fdd5 2022-12-23 op advance_segment_nz_nc(const char *s)
101 5546fdd5 2022-12-23 op const char *t = s;
104 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == '@')
106 5546fdd5 2022-12-23 op else if (pctenc(t))
112 5546fdd5 2022-12-23 op return (t != s ? t : NULL);
115 5546fdd5 2022-12-23 op static const char *
116 5546fdd5 2022-12-23 op parse_scheme(const char *s, struct iri *iri)
118 5546fdd5 2022-12-23 op const char *t = s;
120 5546fdd5 2022-12-23 op if (!isalpha((unsigned char)*t))
121 5546fdd5 2022-12-23 op return (NULL);
123 5546fdd5 2022-12-23 op while (isalnum((unsigned char)*t) ||
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);
132 5546fdd5 2022-12-23 op iri->iri_flags |= IH_SCHEME;
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)
140 5546fdd5 2022-12-23 op const char *t = s;
143 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == ':')
145 5546fdd5 2022-12-23 op else if (pctenc(t))
151 5546fdd5 2022-12-23 op if (*t != '@')
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);
160 5546fdd5 2022-12-23 op static const char *
161 5546fdd5 2022-12-23 op parse_host(const char *s, struct iri *iri)
163 5546fdd5 2022-12-23 op const char *t = s;
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
171 5546fdd5 2022-12-23 op if (*t == '[') {
172 5546fdd5 2022-12-23 op while (*t && *t != ']')
174 5546fdd5 2022-12-23 op if (*t == '\0')
175 5546fdd5 2022-12-23 op return (NULL);
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;
184 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t))
186 5546fdd5 2022-12-23 op else if (pctenc(t))
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;
198 5546fdd5 2022-12-23 op static const char *
199 5546fdd5 2022-12-23 op parse_port(const char *s, struct iri *iri)
201 5546fdd5 2022-12-23 op const char *t = s;
202 5546fdd5 2022-12-23 op const char *errstr;
204 5546fdd5 2022-12-23 op while (isdigit((unsigned char)*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);
210 5546fdd5 2022-12-23 op return (NULL);
211 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PORT;
215 5546fdd5 2022-12-23 op static const char *
216 5546fdd5 2022-12-23 op parse_authority(const char *s, struct iri *iri)
218 5546fdd5 2022-12-23 op const char *t;
220 5546fdd5 2022-12-23 op if ((t = parse_uinfo(s, iri)) == NULL)
221 5546fdd5 2022-12-23 op return (NULL);
223 5546fdd5 2022-12-23 op if ((t = parse_host(t, iri)) == NULL)
224 5546fdd5 2022-12-23 op return (NULL);
226 5546fdd5 2022-12-23 op if (*t == ':')
227 5546fdd5 2022-12-23 op return (parse_port(t, iri));
232 5546fdd5 2022-12-23 op static const char *
233 5546fdd5 2022-12-23 op parse_path_abempty(const char *s, struct iri *iri)
235 5546fdd5 2022-12-23 op const char *t = s;
237 5546fdd5 2022-12-23 op while (*t == '/')
238 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
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;
246 5546fdd5 2022-12-23 op static const char *
247 5546fdd5 2022-12-23 op parse_path_absolute(const char *s, struct iri *iri)
249 5546fdd5 2022-12-23 op const char *t;
251 5546fdd5 2022-12-23 op if (*s != '/')
252 5546fdd5 2022-12-23 op return (NULL);
254 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s + 1)) == NULL)
255 5546fdd5 2022-12-23 op return (s + 1);
257 5546fdd5 2022-12-23 op while (*t == '/')
258 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
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;
266 5546fdd5 2022-12-23 op static const char *
267 5546fdd5 2022-12-23 op parse_path_rootless(const char *s, struct iri *iri)
269 5546fdd5 2022-12-23 op const char *t;
271 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s)) == NULL)
272 5546fdd5 2022-12-23 op return (NULL);
274 5546fdd5 2022-12-23 op while (*t == '/')
275 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
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;
283 5546fdd5 2022-12-23 op static const char *
284 5546fdd5 2022-12-23 op parse_path_noscheme(const char *s, struct iri *iri)
286 5546fdd5 2022-12-23 op const char *t;
288 5546fdd5 2022-12-23 op if ((t = advance_segment_nz_nc(s)) == NULL)
289 5546fdd5 2022-12-23 op return (NULL);
291 5546fdd5 2022-12-23 op while (*t == '/')
292 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
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;
300 5546fdd5 2022-12-23 op static const char *
301 5546fdd5 2022-12-23 op parse_path_empty(const char *s, struct iri *iri)
303 5546fdd5 2022-12-23 op iri->iri_path[0] = '\0';
304 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
308 5546fdd5 2022-12-23 op static const char *
309 5546fdd5 2022-12-23 op parse_hier(const char *s, struct iri *iri)
311 5546fdd5 2022-12-23 op const char *t;
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));
319 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
322 5546fdd5 2022-12-23 op if ((t = parse_path_rootless(s, iri)) != NULL)
325 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
328 5546fdd5 2022-12-23 op static const char *
329 5546fdd5 2022-12-23 op parse_relative(const char *s, struct iri *iri)
331 5546fdd5 2022-12-23 op const char *t = s;
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));
339 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
342 5546fdd5 2022-12-23 op if ((t = parse_path_noscheme(s, iri)) != NULL)
345 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
348 5546fdd5 2022-12-23 op static const char *
349 5546fdd5 2022-12-23 op parse_query(const char *s, struct iri *iri)
351 5546fdd5 2022-12-23 op const char *n, *t = s;
354 5546fdd5 2022-12-23 op if ((n = advance_pchar(t)) != NULL)
356 5546fdd5 2022-12-23 op else if (*t == '/' || *t == '?')
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;
369 5546fdd5 2022-12-23 op parse_uri(const char *s, struct iri *iri)
371 5546fdd5 2022-12-23 op if ((s = parse_scheme(s, iri)) == NULL)
374 5546fdd5 2022-12-23 op if (*s != ':')
377 5546fdd5 2022-12-23 op if ((s = parse_hier(s + 1, iri)) == NULL)
380 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
383 5546fdd5 2022-12-23 op /* skip fragments */
384 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
391 5546fdd5 2022-12-23 op parse_relative_ref(const char *s, struct iri *iri)
393 5546fdd5 2022-12-23 op if ((s = parse_relative(s, iri)) == NULL)
396 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
399 5546fdd5 2022-12-23 op /* skip fragments */
400 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
407 5546fdd5 2022-12-23 op parse(const char *s, struct iri *iri)
409 5546fdd5 2022-12-23 op iri->iri_flags = 0;
411 5546fdd5 2022-12-23 op if (s == NULL)
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)
423 51762770 2022-12-24 op static inline void
424 51762770 2022-12-24 op lowerify(char *s)
426 51762770 2022-12-24 op for (; *s; ++s)
427 51762770 2022-12-24 op *s = tolower((unsigned char)*s);
431 5546fdd5 2022-12-23 op cpfields(struct iri *dest, const struct iri *src, int flags)
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);
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;
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);
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;
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));
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));
475 871b5bb7 2022-12-24 op static inline int
476 5546fdd5 2022-12-23 op remove_dot_segments(struct iri *iri)
478 245a86c3 2022-12-24 op char *p, *q, *buf, *s;
479 871b5bb7 2022-12-24 op ptrdiff_t bufsize;
481 871b5bb7 2022-12-24 op buf = p = q = iri->iri_path;
482 871b5bb7 2022-12-24 op bufsize = sizeof(iri->iri_path);
484 871b5bb7 2022-12-24 op while (*p && (q - buf < bufsize)) {
486 871b5bb7 2022-12-24 op if (!strncmp(p, "../", 3)) {
490 30398ec1 2022-12-24 op if (!strncmp(p, "./", 2)) {
495 871b5bb7 2022-12-24 op if (!strncmp(p, "/./", 3)) {
500 871b5bb7 2022-12-24 op if (!strcmp(p, "/.")) {
505 871b5bb7 2022-12-24 op if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
506 871b5bb7 2022-12-24 op (p[3] == '/' || p[3] == '\0')) {
508 871b5bb7 2022-12-24 op while (q != buf && *--q != '/')
513 871b5bb7 2022-12-24 op if (!strcmp(p, ".")) {
517 871b5bb7 2022-12-24 op if (!strcmp(p, "..")) {
522 a9eac734 2022-12-24 op if ((s = strchr(p + 1, '/')) != NULL)
524 245a86c3 2022-12-24 op while (*p && p != s && (q - buf < bufsize))
528 871b5bb7 2022-12-24 op if (*p == '\0' && (q - buf < bufsize)) {
533 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
537 871b5bb7 2022-12-24 op static inline int
538 871b5bb7 2022-12-24 op mergepath(struct iri *i, struct iri *base, struct iri *r)
540 871b5bb7 2022-12-24 op const char *bpath, *rpath, *s;
542 871b5bb7 2022-12-24 op bpath = (base->iri_flags & IH_PATH) ? base->iri_path : "/";
543 871b5bb7 2022-12-24 op rpath = (r->iri_flags & IH_PATH) ? r->iri_path : "/";
545 871b5bb7 2022-12-24 op i->iri_flags |= IH_PATH;
546 871b5bb7 2022-12-24 op i->iri_path[0] = '\0';
548 871b5bb7 2022-12-24 op if ((base->iri_flags & IH_AUTHORITY) &&
549 871b5bb7 2022-12-24 op (*bpath == '\0' || !strcmp(bpath, "/"))) {
550 871b5bb7 2022-12-24 op if (*rpath == '/')
552 871b5bb7 2022-12-24 op strlcpy(i->iri_path, "/", sizeof(i->iri_path));
553 871b5bb7 2022-12-24 op strlcat(i->iri_path, rpath, sizeof(i->iri_path));
557 871b5bb7 2022-12-24 op if ((s = strrchr(bpath, '/')) != NULL) {
558 871b5bb7 2022-12-24 op cpstr(bpath, s + 1, i->iri_path, sizeof(i->iri_path));
559 871b5bb7 2022-12-24 op if (*rpath == '/')
562 871b5bb7 2022-12-24 op if (strlcat(i->iri_path, rpath, sizeof(i->iri_path)) >=
563 871b5bb7 2022-12-24 op sizeof(i->iri_path)) {
564 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
572 5546fdd5 2022-12-23 op iri_parse(const char *base, const char *str, struct iri *iri)
574 5546fdd5 2022-12-23 op static struct iri ibase, iparsed;
576 5546fdd5 2022-12-23 op memset(iri, 0, sizeof(*iri));
578 5546fdd5 2022-12-23 op if (base == NULL) {
579 5546fdd5 2022-12-23 op ibase.iri_flags = 0;
580 f5bc8482 2022-12-24 op if (parse_uri(str, &iparsed) == -1) {
581 f5bc8482 2022-12-24 op errno = EINVAL;
585 f5bc8482 2022-12-24 op if (parse_uri(base, &ibase) == -1 ||
586 f5bc8482 2022-12-24 op parse(str, &iparsed) == -1) {
587 f5bc8482 2022-12-24 op errno = EINVAL;
592 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_SCHEME) {
593 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, iparsed.iri_flags);
594 5546fdd5 2022-12-23 op remove_dot_segments(iri);
598 5546fdd5 2022-12-23 op /* if fragments are supported, copy iparsed fragment to iri */
600 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_SCHEME);
602 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_HOST) {
603 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_AUTHORITY|IH_PATH|IH_QUERY);
604 5546fdd5 2022-12-23 op remove_dot_segments(iri);
608 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_AUTHORITY);
610 5546fdd5 2022-12-23 op if ((iparsed.iri_flags & IH_PATH) && *iparsed.iri_path == '\0') {
611 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_PATH);
612 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_QUERY)
613 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
615 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_QUERY);
619 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
620 5cb6cd4d 2022-12-24 op if ((iparsed.iri_flags & IH_PATH) && *iparsed.iri_path == '/')
621 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_PATH);
623 5546fdd5 2022-12-23 op if (!(ibase.iri_flags & IH_PATH))
624 5546fdd5 2022-12-23 op ibase.iri_path[0] = '\0';
625 5546fdd5 2022-12-23 op if (!(iparsed.iri_flags & IH_PATH))
626 5546fdd5 2022-12-23 op iparsed.iri_path[0] = '\0';
627 f5bc8482 2022-12-24 op if (mergepath(iri, &ibase, &iparsed) == -1)
630 f5bc8482 2022-12-24 op if (remove_dot_segments(iri) == -1)
636 d59fad58 2022-12-24 op iri_unparse(const struct iri *i, char *buf, size_t buflen)
638 d59fad58 2022-12-24 op if (buflen == 0)
641 d59fad58 2022-12-24 op /* TODO: should %enc octets if needed */
643 d59fad58 2022-12-24 op buf[0] = '\0';
645 d59fad58 2022-12-24 op if (i->iri_flags & IH_SCHEME) {
646 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_scheme, buflen) >= buflen ||
647 d59fad58 2022-12-24 op strlcat(buf, ":", buflen) >= buflen)
651 d59fad58 2022-12-24 op if (i->iri_flags & IH_AUTHORITY) {
652 d59fad58 2022-12-24 op if (strlcat(buf, "//", buflen) >= buflen)
656 d59fad58 2022-12-24 op if (i->iri_flags & IH_UINFO) {
657 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_uinfo, buflen) >= buflen ||
658 d59fad58 2022-12-24 op strlcat(buf, "@", buflen) >= buflen)
661 d59fad58 2022-12-24 op if (i->iri_flags & IH_HOST) {
662 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_host, buflen) >= buflen)
665 d59fad58 2022-12-24 op if (i->iri_flags & IH_PORT) {
666 d59fad58 2022-12-24 op if (strlcat(buf, ":", buflen) >= buflen ||
667 d59fad58 2022-12-24 op strlcat(buf, i->iri_portstr, buflen) >= buflen)
671 d59fad58 2022-12-24 op if (i->iri_flags & IH_PATH) {
672 a655e85a 2022-12-24 op if (i->iri_flags & IH_AUTHORITY &&
673 d59fad58 2022-12-24 op i->iri_path[0] != '/' &&
674 d59fad58 2022-12-24 op strlcat(buf, "/", buflen) >= buflen)
676 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_path, buflen) >= buflen)
680 d59fad58 2022-12-24 op if (i->iri_flags & IH_QUERY) {
681 d59fad58 2022-12-24 op if (strlcat(buf, "?", buflen) >= buflen ||
682 d59fad58 2022-12-24 op strlcat(buf, i->iri_query, buflen) >= buflen)
689 d59fad58 2022-12-24 op errno = ENOBUFS;
694 5546fdd5 2022-12-23 op iri_human(const struct iri *iri, char *buf, size_t buflen)
696 5546fdd5 2022-12-23 op memset(buf, 0, buflen);
701 5546fdd5 2022-12-23 op iri_setquery(struct iri *iri, const char *text)