FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/url.c
Date: 2024-04-18 20:30:25
Exec Total Coverage
Lines: 125 173 72.3%
Functions: 6 8 75.0%
Branches: 142 192 74.0%

Line Branch Exec Source
1 /*
2 * URL utility functions
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23
24 #include "config.h"
25 #include "avio.h"
26 #include "url.h"
27 #if CONFIG_NETWORK
28 #include "network.h"
29 #endif
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/error.h"
33 #include "libavutil/mem.h"
34
35 /**
36 * @file
37 * URL utility functions.
38 */
39
40 int ff_url_join(char *str, int size, const char *proto,
41 const char *authorization, const char *hostname,
42 int port, const char *fmt, ...)
43 {
44 #if CONFIG_NETWORK
45 struct addrinfo hints = { 0 }, *ai;
46 #endif
47
48 str[0] = '\0';
49 if (proto)
50 av_strlcatf(str, size, "%s://", proto);
51 if (authorization && authorization[0])
52 av_strlcatf(str, size, "%s@", authorization);
53 #if CONFIG_NETWORK && defined(AF_INET6)
54 /* Determine if hostname is a numerical IPv6 address,
55 * properly escape it within [] in that case. */
56 hints.ai_flags = AI_NUMERICHOST;
57 if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
58 if (ai->ai_family == AF_INET6) {
59 av_strlcat(str, "[", size);
60 av_strlcat(str, hostname, size);
61 av_strlcat(str, "]", size);
62 } else {
63 av_strlcat(str, hostname, size);
64 }
65 freeaddrinfo(ai);
66 } else
67 #endif
68 /* Not an IPv6 address, just output the plain string. */
69 av_strlcat(str, hostname, size);
70
71 if (port >= 0)
72 av_strlcatf(str, size, ":%d", port);
73 if (fmt) {
74 va_list vl;
75 size_t len = strlen(str);
76
77 va_start(vl, fmt);
78 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
79 va_end(vl);
80 }
81 return strlen(str);
82 }
83
84 4154 static const char *find_delim(const char *delim, const char *cur, const char *end)
85 {
86
4/4
✓ Branch 0 taken 33526 times.
✓ Branch 1 taken 2132 times.
✓ Branch 2 taken 31504 times.
✓ Branch 3 taken 2022 times.
35658 while (cur < end && !strchr(delim, *cur))
87 31504 cur++;
88 4154 return cur;
89 }
90
91 1044 int ff_url_decompose(URLComponents *uc, const char *url, const char *end)
92 {
93 const char *cur, *aend, *p;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1044 times.
1044 av_assert0(url);
96
1/2
✓ Branch 0 taken 1044 times.
✗ Branch 1 not taken.
1044 if (!end)
97 1044 end = url + strlen(url);
98 1044 cur = uc->url = url;
99
100 /* scheme */
101 1044 uc->scheme = cur;
102 1044 p = find_delim(":/?#", cur, end); /* lavf "schemes" can contain options but not some RFC 3986 delimiters */
103
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 658 times.
1044 if (*p == ':')
104 386 cur = p + 1;
105
106 /* authority */
107 1044 uc->authority = cur;
108
6/6
✓ Branch 0 taken 1008 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 557 times.
✓ Branch 3 taken 451 times.
✓ Branch 4 taken 347 times.
✓ Branch 5 taken 210 times.
1044 if (end - cur >= 2 && cur[0] == '/' && cur[1] == '/') {
109 347 cur += 2;
110 347 aend = find_delim("/?#", cur, end);
111
112 /* userinfo */
113 347 uc->userinfo = cur;
114 347 p = find_delim("@", cur, aend);
115
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 346 times.
347 if (*p == '@')
116 1 cur = p + 1;
117
118 /* host */
119 347 uc->host = cur;
120
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 345 times.
347 if (*cur == '[') { /* hello IPv6, thanks for using colons! */
121 2 p = find_delim("]", cur, aend);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (*p != ']')
123 return AVERROR(EINVAL);
124
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (p + 1 < aend && p[1] != ':')
125 return AVERROR(EINVAL);
126 2 cur = p + 1;
127 } else {
128 345 cur = find_delim(":", cur, aend);
129 }
130
131 /* port */
132 347 uc->port = cur;
133 347 cur = aend;
134 } else {
135 697 uc->userinfo = uc->host = uc->port = cur;
136 }
137
138 /* path */
139 1044 uc->path = cur;
140 1044 cur = find_delim("?#", cur, end);
141
142 /* query */
143 1044 uc->query = cur;
144
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 799 times.
1044 if (*cur == '?')
145 245 cur = find_delim("#", cur, end);
146
147 /* fragment */
148 1044 uc->fragment = cur;
149
150 1044 uc->end = end;
151 1044 return 0;
152 }
153
154 101 static int is_fq_dos_path(const char *path)
155 {
156
7/8
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 6 times.
101 if ((path[0] >= 'a' && path[0] <= 'z' || path[0] >= 'A' && path[0] <= 'Z') &&
157
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 78 times.
83 path[1] == ':' &&
158
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5 (path[2] == '/' || path[2] == '\\'))
159 5 return 1;
160
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 85 times.
96 if ((path[0] == '/' || path[0] == '\\') &&
161
3/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 5 times.
11 (path[1] == '/' || path[1] == '\\'))
162 6 return 1;
163 90 return 0;
164 }
165
166 404 static int append_path(char *root, char *out_end, char **rout,
167 const char *in, const char *in_end)
168 {
169 404 char *out = *rout;
170 const char *d, *next;
171
172
3/4
✓ Branch 0 taken 404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 180 times.
404 if (in < in_end && *in == '/')
173 224 in++; /* already taken care of */
174
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 404 times.
1184 while (in < in_end) {
175 780 d = find_delim("/", in, in_end);
176
3/4
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 580 times.
✗ Branch 3 not taken.
780 next = d + (d < in_end && *d == '/');
177
4/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 376 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 364 times.
780 if (d - in == 1 && in[0] == '.') {
178 /* skip */
179
6/6
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 548 times.
✓ Branch 2 taken 184 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 180 times.
✓ Branch 5 taken 4 times.
740 } else if (d - in == 2 && in[0] == '.' && in[1] == '.') {
180 av_assert1(out[-1] == '/');
181
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 80 times.
180 if (out - root > 1)
182
3/4
✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 100 times.
288 while (out > root && (--out)[-1] != '/');
183 } else {
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (out_end - out < next - in)
185 return AVERROR(ENOMEM);
186 560 memmove(out, in, next - in);
187 560 out += next - in;
188 }
189 780 in = next;
190 }
191 404 *rout = out;
192 404 return 0;
193 }
194
195 476 int ff_make_absolute_url2(char *buf, int size, const char *base,
196 const char *rel, int handle_dos_paths)
197 {
198 URLComponents ub, uc;
199 char *out, *out_end, *path;
200 const char *keep, *base_path_end;
201 476 int use_base_path, simplify_path = 0, ret;
202 476 const char *base_separators = "/";
203
204 /* This is tricky.
205 For HTTP, http://server/site/page + ../media/file
206 should resolve into http://server/media/file
207 but for filesystem access, dir/playlist + ../media/file
208 should resolve into dir/../media/file
209 because dir could be a symlink, and .. points to
210 the actual parent of the target directory.
211
212 We'll consider that URLs with an actual scheme and authority,
213 i.e. starting with scheme://, need parent dir simplification,
214 while bare paths or pseudo-URLs starting with proto: without
215 the double slash do not.
216
217 For real URLs, the processing is similar to the algorithm described
218 here:
219 https://tools.ietf.org/html/rfc3986#section-5
220 */
221
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 if (!size)
223 return AVERROR(ENOMEM);
224 476 out = buf;
225 476 out_end = buf + size - 1;
226
227
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 461 times.
476 if (!base)
228 15 base = "";
229
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 393 times.
476 if (handle_dos_paths) {
230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
83 if ((ret = ff_url_decompose(&ub, base, NULL)) < 0)
231 goto error;
232
6/6
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 7 times.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 65 times.
83 if (is_fq_dos_path(base) || av_strstart(base, "file:", NULL) || ub.path == ub.url) {
233 18 base_separators = "/\\";
234
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 14 times.
18 if (is_fq_dos_path(rel))
235 4 base = "";
236 }
237 }
238
2/4
✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 476 times.
952 if ((ret = ff_url_decompose(&ub, base, NULL)) < 0 ||
239 476 (ret = ff_url_decompose(&uc, rel, NULL)) < 0)
240 goto error;
241
242 476 keep = ub.url;
243 #define KEEP(component, also) do { \
244 if (uc.url_component_end_##component == uc.url && \
245 ub.url_component_end_##component > keep) { \
246 keep = ub.url_component_end_##component; \
247 also \
248 } \
249 } while (0)
250
4/4
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 251 times.
✓ Branch 3 taken 193 times.
476 KEEP(scheme, );
251
4/4
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 204 times.
476 KEEP(authority_full, simplify_path = 1;);
252
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 460 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
476 KEEP(path,);
253
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
476 KEEP(query,);
254
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 472 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
476 KEEP(fragment,);
255 #undef KEEP
256 #define COPY(start, end) do { \
257 size_t len = end - start; \
258 if (len > out_end - out) { \
259 ret = AVERROR(ENOMEM); \
260 goto error; \
261 } \
262 memmove(out, start, len); \
263 out += len; \
264 } while (0)
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 COPY(ub.url, keep);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 COPY(uc.url, uc.path);
267
268
4/4
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 441 times.
✓ Branch 3 taken 16 times.
476 use_base_path = URL_COMPONENT_HAVE(ub, path) && keep <= ub.path;
269
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 432 times.
476 if (uc.path > uc.url)
270 44 use_base_path = 0;
271
4/4
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 388 times.
476 if (URL_COMPONENT_HAVE(uc, path) && uc.path[0] == '/')
272 68 use_base_path = 0;
273
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 113 times.
476 if (use_base_path) {
274 363 base_path_end = ub.url_component_end_path;
275
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 if (URL_COMPONENT_HAVE(uc, path))
276
4/4
✓ Branch 0 taken 5270 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 4929 times.
✓ Branch 3 taken 341 times.
5292 while (base_path_end > ub.path && !strchr(base_separators, base_path_end[-1]))
277 4929 base_path_end--;
278 }
279
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 460 times.
476 if (keep > ub.path)
280 16 simplify_path = 0;
281
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 444 times.
476 if (URL_COMPONENT_HAVE(uc, scheme))
282 32 simplify_path = 0;
283
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 460 times.
476 if (URL_COMPONENT_HAVE(uc, authority))
284 16 simplify_path = 1;
285 /* No path at all, leave it */
286
4/4
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 363 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 93 times.
476 if (!use_base_path && !URL_COMPONENT_HAVE(uc, path))
287 20 simplify_path = 0;
288
289
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 252 times.
476 if (simplify_path) {
290 224 const char *root = "/";
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 COPY(root, root + 1);
292 224 path = out;
293
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 44 times.
224 if (use_base_path) {
294 180 ret = append_path(path, out_end, &out, ub.path, base_path_end);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
180 if (ret < 0)
296 goto error;
297 }
298
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 if (URL_COMPONENT_HAVE(uc, path)) {
299 224 ret = append_path(path, out_end, &out, uc.path, uc.url_component_end_path);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 if (ret < 0)
301 goto error;
302 }
303 } else {
304
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 69 times.
252 if (use_base_path)
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 COPY(ub.path, base_path_end);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 COPY(uc.path, uc.url_component_end_path);
307 }
308
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 COPY(uc.url_component_end_path, uc.end);
310 #undef COPY
311 476 *out = 0;
312 476 return 0;
313
314 error:
315 snprintf(buf, size, "invalid:%s",
316 ret == AVERROR(ENOMEM) ? "truncated" :
317 ret == AVERROR(EINVAL) ? "syntax_error" : "");
318 return ret;
319 }
320
321 228 int ff_make_absolute_url(char *buf, int size, const char *base,
322 const char *rel)
323 {
324 228 return ff_make_absolute_url2(buf, size, base, rel, HAVE_DOS_PATHS);
325 }
326
327 AVIODirEntry *ff_alloc_dir_entry(void)
328 {
329 AVIODirEntry *entry = av_mallocz(sizeof(AVIODirEntry));
330 if (entry) {
331 entry->type = AVIO_ENTRY_UNKNOWN;
332 entry->size = -1;
333 entry->modification_timestamp = -1;
334 entry->access_timestamp = -1;
335 entry->status_change_timestamp = -1;
336 entry->user_id = -1;
337 entry->group_id = -1;
338 entry->filemode = -1;
339 }
340 return entry;
341 }
342