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;
368 a165601e 2022-12-25 op static const char *
369 a165601e 2022-12-25 op parse_fragment(const char *s, struct iri *iri)
371 a165601e 2022-12-25 op const char *n, *t = s;
374 a165601e 2022-12-25 op if ((n = advance_pchar(t)) != NULL)
376 a165601e 2022-12-25 op else if (*t == '/' || *t == '?')
382 a165601e 2022-12-25 op if (cpstr(s, t, iri->iri_fragment, sizeof(iri->iri_fragment)) == -1)
383 a165601e 2022-12-25 op return (NULL);
384 a165601e 2022-12-25 op iri->iri_flags |= IH_FRAGMENT;
389 5546fdd5 2022-12-23 op parse_uri(const char *s, struct iri *iri)
391 5546fdd5 2022-12-23 op if ((s = parse_scheme(s, iri)) == NULL)
394 5546fdd5 2022-12-23 op if (*s != ':')
397 5546fdd5 2022-12-23 op if ((s = parse_hier(s + 1, iri)) == NULL)
400 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
403 a165601e 2022-12-25 op if (*s == '#' && (s = parse_fragment(s + 1, iri)) == NULL)
406 a165601e 2022-12-25 op if (*s == '\0')
413 5546fdd5 2022-12-23 op parse_relative_ref(const char *s, struct iri *iri)
415 5546fdd5 2022-12-23 op if ((s = parse_relative(s, iri)) == NULL)
418 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
421 a165601e 2022-12-25 op if (*s == '#' && (s = parse_fragment(s + 1, iri)) == NULL)
424 a165601e 2022-12-25 op if (*s == '\0')
431 5546fdd5 2022-12-23 op parse(const char *s, struct iri *iri)
433 5546fdd5 2022-12-23 op iri->iri_flags = 0;
435 5546fdd5 2022-12-23 op if (s == NULL)
438 5546fdd5 2022-12-23 op if (parse_uri(s, iri) == -1) {
439 5546fdd5 2022-12-23 op iri->iri_flags = 0;
440 5546fdd5 2022-12-23 op if (parse_relative_ref(s, iri) == -1)
447 51762770 2022-12-24 op static inline void
448 51762770 2022-12-24 op lowerify(char *s)
450 51762770 2022-12-24 op for (; *s; ++s)
451 51762770 2022-12-24 op *s = tolower((unsigned char)*s);
455 5546fdd5 2022-12-23 op cpfields(struct iri *dest, const struct iri *src, int flags)
457 5546fdd5 2022-12-23 op if (flags & IH_SCHEME) {
458 5546fdd5 2022-12-23 op dest->iri_flags |= IH_SCHEME;
459 5546fdd5 2022-12-23 op if (src->iri_flags & IH_SCHEME)
460 5546fdd5 2022-12-23 op memcpy(dest->iri_scheme, src->iri_scheme,
461 5546fdd5 2022-12-23 op sizeof(dest->iri_scheme));
462 51762770 2022-12-24 op lowerify(dest->iri_scheme);
464 5546fdd5 2022-12-23 op if (flags & IH_UINFO) {
465 997b9cd0 2022-12-24 op if (src->iri_flags & IH_UINFO) {
466 5546fdd5 2022-12-23 op memcpy(dest->iri_uinfo, src->iri_uinfo,
467 5546fdd5 2022-12-23 op sizeof(dest->iri_uinfo));
468 997b9cd0 2022-12-24 op dest->iri_flags |= IH_UINFO;
471 5546fdd5 2022-12-23 op if (flags & IH_HOST) {
472 5546fdd5 2022-12-23 op dest->iri_flags |= IH_HOST;
473 5546fdd5 2022-12-23 op if (src->iri_flags & IH_HOST)
474 5546fdd5 2022-12-23 op memcpy(dest->iri_host, src->iri_host,
475 5546fdd5 2022-12-23 op sizeof(dest->iri_host));
476 51762770 2022-12-24 op lowerify(dest->iri_host);
478 5546fdd5 2022-12-23 op if (flags & IH_PORT) {
479 997b9cd0 2022-12-24 op if (src->iri_flags & IH_PORT) {
480 5546fdd5 2022-12-23 op dest->iri_port = src->iri_port;
481 997b9cd0 2022-12-24 op dest->iri_flags |= IH_PORT;
484 5546fdd5 2022-12-23 op if (flags & IH_PATH) {
485 5546fdd5 2022-12-23 op dest->iri_flags |= IH_PATH;
486 5546fdd5 2022-12-23 op if (src->iri_flags & IH_PATH)
487 5546fdd5 2022-12-23 op memcpy(dest->iri_path, src->iri_path,
488 5546fdd5 2022-12-23 op sizeof(dest->iri_path));
490 5546fdd5 2022-12-23 op if (flags & IH_QUERY) {
491 997b9cd0 2022-12-24 op if (src->iri_flags & IH_QUERY) {
492 997b9cd0 2022-12-24 op dest->iri_flags |= IH_QUERY;
493 5546fdd5 2022-12-23 op memcpy(dest->iri_query, src->iri_query,
494 5546fdd5 2022-12-23 op sizeof(dest->iri_query));
497 a165601e 2022-12-25 op if (flags & IH_FRAGMENT) {
498 a165601e 2022-12-25 op if (src->iri_flags & IH_FRAGMENT) {
499 a165601e 2022-12-25 op dest->iri_flags |= IH_FRAGMENT;
500 a165601e 2022-12-25 op memcpy(dest->iri_fragment, src->iri_fragment,
501 a165601e 2022-12-25 op sizeof(dest->iri_fragment));
506 871b5bb7 2022-12-24 op static inline int
507 8b2b06b5 2022-12-25 op remove_dot_segments(struct iri *i)
509 8b2b06b5 2022-12-25 op char *p, *q, *buf;
510 871b5bb7 2022-12-24 op ptrdiff_t bufsize;
512 8b2b06b5 2022-12-25 op buf = p = q = i->iri_path;
513 8b2b06b5 2022-12-25 op bufsize = sizeof(i->iri_path);
514 871b5bb7 2022-12-24 op while (*p && (q - buf < bufsize)) {
515 8b2b06b5 2022-12-25 op if (p[0] == '/' && p[1] == '.' &&
516 8b2b06b5 2022-12-25 op (p[2] == '/' || p[2] == '\0')) {
518 8b2b06b5 2022-12-25 op if (*p != '/')
520 8b2b06b5 2022-12-25 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
521 871b5bb7 2022-12-24 op (p[3] == '/' || p[3] == '\0')) {
523 8b2b06b5 2022-12-25 op while (q > buf && *--q != '/')
525 8b2b06b5 2022-12-25 op if (*p != '/' && (q > buf && q[-1] != '/'))
530 8b2b06b5 2022-12-25 op if ((*p == '\0') && (q - buf < bufsize)) {
535 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
539 871b5bb7 2022-12-24 op static inline int
540 871b5bb7 2022-12-24 op mergepath(struct iri *i, struct iri *base, struct iri *r)
542 871b5bb7 2022-12-24 op const char *bpath, *rpath, *s;
544 871b5bb7 2022-12-24 op bpath = (base->iri_flags & IH_PATH) ? base->iri_path : "/";
545 871b5bb7 2022-12-24 op rpath = (r->iri_flags & IH_PATH) ? r->iri_path : "/";
547 871b5bb7 2022-12-24 op i->iri_flags |= IH_PATH;
548 871b5bb7 2022-12-24 op i->iri_path[0] = '\0';
550 871b5bb7 2022-12-24 op if ((base->iri_flags & IH_AUTHORITY) &&
551 871b5bb7 2022-12-24 op (*bpath == '\0' || !strcmp(bpath, "/"))) {
552 871b5bb7 2022-12-24 op if (*rpath == '/')
554 871b5bb7 2022-12-24 op strlcpy(i->iri_path, "/", sizeof(i->iri_path));
555 871b5bb7 2022-12-24 op strlcat(i->iri_path, rpath, sizeof(i->iri_path));
559 871b5bb7 2022-12-24 op if ((s = strrchr(bpath, '/')) != NULL) {
560 871b5bb7 2022-12-24 op cpstr(bpath, s + 1, i->iri_path, sizeof(i->iri_path));
561 871b5bb7 2022-12-24 op if (*rpath == '/')
564 871b5bb7 2022-12-24 op if (strlcat(i->iri_path, rpath, sizeof(i->iri_path)) >=
565 871b5bb7 2022-12-24 op sizeof(i->iri_path)) {
566 871b5bb7 2022-12-24 op errno = ENAMETOOLONG;
574 5546fdd5 2022-12-23 op iri_parse(const char *base, const char *str, struct iri *iri)
576 5546fdd5 2022-12-23 op static struct iri ibase, iparsed;
578 5546fdd5 2022-12-23 op memset(iri, 0, sizeof(*iri));
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;
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;
594 a165601e 2022-12-25 op cpfields(iri, &iparsed, IH_FRAGMENT);
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 5546fdd5 2022-12-23 op remove_dot_segments(iri);
602 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_SCHEME);
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 5546fdd5 2022-12-23 op remove_dot_segments(iri);
610 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_AUTHORITY);
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);
617 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_QUERY);
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);
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 f5bc8482 2022-12-24 op if (mergepath(iri, &ibase, &iparsed) == -1)
632 f5bc8482 2022-12-24 op if (remove_dot_segments(iri) == -1)
638 d59fad58 2022-12-24 op iri_unparse(const struct iri *i, char *buf, size_t buflen)
640 d59fad58 2022-12-24 op if (buflen == 0)
643 d59fad58 2022-12-24 op /* TODO: should %enc octets if needed */
645 d59fad58 2022-12-24 op buf[0] = '\0';
647 d59fad58 2022-12-24 op if (i->iri_flags & IH_SCHEME) {
648 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_scheme, buflen) >= buflen ||
649 d59fad58 2022-12-24 op strlcat(buf, ":", buflen) >= buflen)
653 d59fad58 2022-12-24 op if (i->iri_flags & IH_AUTHORITY) {
654 d59fad58 2022-12-24 op if (strlcat(buf, "//", buflen) >= buflen)
658 d59fad58 2022-12-24 op if (i->iri_flags & IH_UINFO) {
659 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_uinfo, buflen) >= buflen ||
660 d59fad58 2022-12-24 op strlcat(buf, "@", buflen) >= buflen)
663 d59fad58 2022-12-24 op if (i->iri_flags & IH_HOST) {
664 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_host, buflen) >= buflen)
667 d59fad58 2022-12-24 op if (i->iri_flags & IH_PORT) {
668 d59fad58 2022-12-24 op if (strlcat(buf, ":", buflen) >= buflen ||
669 d59fad58 2022-12-24 op strlcat(buf, i->iri_portstr, buflen) >= buflen)
673 d59fad58 2022-12-24 op if (i->iri_flags & IH_PATH) {
674 a655e85a 2022-12-24 op if (i->iri_flags & IH_AUTHORITY &&
675 d59fad58 2022-12-24 op i->iri_path[0] != '/' &&
676 d59fad58 2022-12-24 op strlcat(buf, "/", buflen) >= buflen)
678 d59fad58 2022-12-24 op if (strlcat(buf, i->iri_path, buflen) >= buflen)
682 d59fad58 2022-12-24 op if (i->iri_flags & IH_QUERY) {
683 d59fad58 2022-12-24 op if (strlcat(buf, "?", buflen) >= buflen ||
684 d59fad58 2022-12-24 op strlcat(buf, i->iri_query, buflen) >= buflen)
688 a165601e 2022-12-25 op if (i->iri_flags & IH_FRAGMENT) {
689 a165601e 2022-12-25 op if (strlcat(buf, "#", buflen) >= buflen ||
690 a165601e 2022-12-25 op strlcat(buf, i->iri_fragment, buflen) >= buflen)
697 d59fad58 2022-12-24 op errno = ENOBUFS;
702 5546fdd5 2022-12-23 op iri_human(const struct iri *iri, char *buf, size_t buflen)
704 5546fdd5 2022-12-23 op memset(buf, 0, buflen);
709 5546fdd5 2022-12-23 op iri_setquery(struct iri *iri, const char *text)