FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avio.c
Date: 2026-09-25 23:13:28
Exec Total Coverage
Lines: 282 515 54.8%
Functions: 32 45 71.1%
Branches: 164 390 42.1%

Line Branch Exec Source
1 /*
2 * unbuffered I/O
3 * Copyright (c) 2001 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 "config_components.h"
23
24 #include <stdlib.h>
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/time.h"
31 #include "libavutil/avassert.h"
32 #include "avio_internal.h"
33 #include "os_support.h"
34 #include "internal.h"
35 #if CONFIG_NETWORK
36 #include "network.h"
37 #endif
38 #include "url.h"
39
40 #define IO_BUFFER_SIZE 32768
41 #define NETWORK_IO_BUFFER_SIZE 262144
42
43 /** @name Logging context. */
44 /*@{*/
45 6 static const char *urlcontext_to_name(void *ptr)
46 {
47 6 URLContext *h = (URLContext *)ptr;
48
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (h->prot)
49 6 return h->prot->name;
50 else
51 ✗ return "NULL";
52 }
53
54 39872 static void *urlcontext_child_next(void *obj, void *prev)
55 {
56 39872 URLContext *h = obj;
57
5/6
✓ Branch 0 taken 19940 times.
✓ Branch 1 taken 19932 times.
✓ Branch 2 taken 19940 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19932 times.
✓ Branch 5 taken 8 times.
39872 if (!prev && h->priv_data && h->prot->priv_data_class)
58 19932 return h->priv_data;
59 19940 return NULL;
60 }
61
62 #define OFFSET(x) offsetof(URLContext,x)
63 #define E AV_OPT_FLAG_ENCODING_PARAM
64 #define D AV_OPT_FLAG_DECODING_PARAM
65 static const AVOption urlcontext_options[] = {
66 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
67 {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
68 {"rw_timeout", "Timeout for IO operations (in microseconds)", offsetof(URLContext, rw_timeout), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_DECODING_PARAM },
69 {"prefer_libcurl", "use the libcurl protocol for http(s) URLs when available", OFFSET(prefer_libcurl), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
70 { NULL }
71 };
72
73 static const AVClass url_context_class = {
74 .class_name = "URLContext",
75 .item_name = urlcontext_to_name,
76 .option = urlcontext_options,
77 .version = LIBAVUTIL_VERSION_INT,
78 .child_next = urlcontext_child_next,
79 .child_class_iterate = ff_urlcontext_child_class_iterate,
80 };
81 /*@}*/
82
83 39848 static void *avio_child_next(void *obj, void *prev)
84 {
85 39848 AVIOContext *s = obj;
86
2/2
✓ Branch 0 taken 19940 times.
✓ Branch 1 taken 19908 times.
39848 return prev ? NULL : s->opaque;
87 }
88
89 245533 static const AVClass *child_class_iterate(void **iter)
90 {
91
2/2
✓ Branch 0 taken 122774 times.
✓ Branch 1 taken 122759 times.
245533 const AVClass *c = *iter ? NULL : &url_context_class;
92 245533 *iter = (void*)(uintptr_t)c;
93 245533 return c;
94 }
95
96 #define AVIOOFFSET(x) offsetof(AVIOContext,x)
97 #define E AV_OPT_FLAG_ENCODING_PARAM
98 #define D AV_OPT_FLAG_DECODING_PARAM
99 static const AVOption avio_options[] = {
100 {"protocol_whitelist", "List of protocols that are allowed to be used", AVIOOFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
101 { NULL },
102 };
103
104 const AVClass ff_avio_class = {
105 .class_name = "AVIOContext",
106 .item_name = av_default_item_name,
107 .version = LIBAVUTIL_VERSION_INT,
108 .option = avio_options,
109 .child_next = avio_child_next,
110 .child_class_iterate = child_class_iterate,
111 };
112
113 66 URLContext *ffio_geturlcontext(AVIOContext *s)
114 {
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!s)
116 ✗ return NULL;
117
118
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
66 if (s->opaque && s->read_packet == ffurl_read2)
119 66 return s->opaque;
120 else
121 ✗ return NULL;
122 }
123
124 170097 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
125 const char *filename, int flags,
126 const AVIOInterruptCB *int_cb)
127 {
128 URLContext *uc;
129 int err;
130
131 #if CONFIG_NETWORK
132
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 170097 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
170097 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && (err = ff_network_init()) < 0)
133 ✗ return err;
134 #endif
135
3/4
✓ Branch 0 taken 159950 times.
✓ Branch 1 taken 10147 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 159950 times.
170097 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
136 ✗ av_log(NULL, AV_LOG_ERROR,
137 ✗ "Impossible to open the '%s' protocol for reading\n", up->name);
138 ✗ return AVERROR(EIO);
139 }
140
3/4
✓ Branch 0 taken 10262 times.
✓ Branch 1 taken 159835 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10262 times.
170097 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
141 ✗ av_log(NULL, AV_LOG_ERROR,
142 ✗ "Impossible to open the '%s' protocol for writing\n", up->name);
143 ✗ return AVERROR(EIO);
144 }
145 170097 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170097 times.
170097 if (!uc) {
147 ✗ err = AVERROR(ENOMEM);
148 ✗ goto fail;
149 }
150 170097 uc->av_class = &url_context_class;
151 170097 uc->filename = (char *)&uc[1];
152 170097 strcpy(uc->filename, filename);
153 170097 uc->prot = up;
154 170097 uc->flags = flags;
155 170097 uc->is_streamed = 0; /* default = not streamed */
156 170097 uc->uses_network = !!(up->flags & URL_PROTOCOL_FLAG_NETWORK);
157 170097 uc->max_packet_size = 0; /* default: stream file */
158
1/2
✓ Branch 0 taken 170097 times.
✗ Branch 1 not taken.
170097 if (up->priv_data_size) {
159 170097 uc->priv_data = av_mallocz(up->priv_data_size);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170097 times.
170097 if (!uc->priv_data) {
161 ✗ err = AVERROR(ENOMEM);
162 ✗ goto fail;
163 }
164
2/2
✓ Branch 0 taken 167291 times.
✓ Branch 1 taken 2806 times.
170097 if (up->priv_data_class) {
165 char *start;
166 167291 *(const AVClass **)uc->priv_data = up->priv_data_class;
167 167291 av_opt_set_defaults(uc->priv_data);
168
3/4
✓ Branch 1 taken 2901 times.
✓ Branch 2 taken 164390 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2901 times.
167291 if (av_strstart(uc->filename, up->name, (const char**)&start) && *start == ',') {
169 ✗ int ret= 0;
170 ✗ char *p= start;
171 ✗ char sep= *++p;
172 char *key, *val;
173 ✗ p++;
174
175 ✗ if (strcmp(up->name, "subfile"))
176 ✗ ret = AVERROR(EINVAL);
177
178 ✗ while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
179 ✗ *val= *key= 0;
180 ✗ ret = av_opt_set(uc->priv_data, p, key+1, 0);
181 ✗ if (ret == AVERROR_OPTION_NOT_FOUND)
182 ✗ av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
183 ✗ *val= *key= sep;
184 ✗ p= val+1;
185 }
186 ✗ if(ret<0 || p!=key){
187 ✗ av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
188 ✗ err = AVERROR(EINVAL);
189 ✗ goto fail;
190 }
191 ✗ memmove(start, key+1, strlen(key));
192 }
193 }
194 }
195
2/2
✓ Branch 0 taken 117139 times.
✓ Branch 1 taken 52958 times.
170097 if (int_cb)
196 117139 uc->interrupt_callback = *int_cb;
197
198 170097 *puc = uc;
199 170097 return 0;
200 ✗ fail:
201 ✗ *puc = NULL;
202 ✗ if (uc)
203 ✗ av_freep(&uc->priv_data);
204 ✗ av_freep(&uc);
205 #if CONFIG_NETWORK
206 ✗ if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
207 ✗ ff_network_close();
208 #endif
209 ✗ return err;
210 }
211
212 117263 int ffurl_connect(URLContext *uc, AVDictionary **options)
213 {
214 int err;
215 117263 AVDictionary *tmp_opts = NULL;
216 AVDictionaryEntry *e;
217
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117263 times.
117263 if (!options)
219 ✗ options = &tmp_opts;
220
221 // Check that URLContext was initialized correctly and lists are matching if set
222
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
117263 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
223 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
224
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
117263 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
225 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
226
227
3/4
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 117074 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 189 times.
117263 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
228 ✗ av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
229 ✗ return AVERROR(EINVAL);
230 }
231
232
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 117263 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
117263 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
233 ✗ av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
234 ✗ return AVERROR(EINVAL);
235 }
236
237
4/4
✓ Branch 0 taken 117074 times.
✓ Branch 1 taken 189 times.
✓ Branch 2 taken 114269 times.
✓ Branch 3 taken 2805 times.
117263 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
238 114269 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
239 114269 uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114269 times.
114269 if (!uc->protocol_whitelist) {
241 ✗ return AVERROR(ENOMEM);
242 }
243
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 189 times.
2994 } else if (!uc->protocol_whitelist)
244 2805 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
245
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
247 ✗ return err;
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
249 ✗ goto fail;
250
251 117263 err =
252 117264 uc->prot->url_open2 ? uc->prot->url_open2(uc,
253 1 uc->filename,
254 uc->flags,
255
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 117262 times.
117263 options) :
256 117262 uc->prot->url_open(uc, uc->filename, uc->flags);
257
258 117263 av_dict_set(options, "protocol_whitelist", NULL, 0);
259 117263 av_dict_set(options, "protocol_blacklist", NULL, 0);
260
261
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 117262 times.
117263 if (err)
262 1 return err;
263 117262 uc->is_connected = 1;
264 /* We must be careful here as ffurl_seek() could be slow,
265 * for example for http */
266
4/4
✓ Branch 0 taken 107232 times.
✓ Branch 1 taken 10030 times.
✓ Branch 2 taken 107228 times.
✓ Branch 3 taken 4 times.
117262 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
267
4/4
✓ Branch 0 taken 114359 times.
✓ Branch 1 taken 2899 times.
✓ Branch 3 taken 2804 times.
✓ Branch 4 taken 111555 times.
117258 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
268 2804 uc->is_streamed = 1;
269 117262 return 0;
270
271 ✗ fail:
272 ✗ if (options == &tmp_opts)
273 ✗ av_dict_free(&tmp_opts);
274 ✗ return err;
275 }
276
277 ✗ int ffurl_accept(URLContext *s, URLContext **c)
278 {
279 ✗ av_assert0(!*c);
280 ✗ if (s->prot->url_accept)
281 ✗ return s->prot->url_accept(s, c);
282 ✗ return AVERROR(EBADF);
283 }
284
285 ✗ int avio_accept(AVIOContext *s, AVIOContext **c)
286 {
287 int ret;
288 ✗ URLContext *sc = s->opaque;
289 ✗ URLContext *cc = NULL;
290 ✗ ret = ffurl_accept(sc, &cc);
291 ✗ if (ret < 0)
292 ✗ return ret;
293 ✗ return ffio_fdopen(c, cc);
294 }
295
296 ✗ int ffurl_handshake(URLContext *c)
297 {
298 int ret;
299 ✗ if (c->prot->url_handshake) {
300 ✗ ret = c->prot->url_handshake(c);
301 ✗ if (ret)
302 ✗ return ret;
303 }
304 ✗ c->is_connected = 1;
305 ✗ return 0;
306 }
307
308 ✗ int avio_handshake(AVIOContext *c)
309 {
310 ✗ URLContext *cc = c->opaque;
311 ✗ return ffurl_handshake(cc);
312 }
313
314 #define URL_SCHEME_CHARS \
315 "abcdefghijklmnopqrstuvwxyz" \
316 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
317 "0123456789+-."
318
319 180504 static const struct URLProtocol *url_find_protocol(const char *filename)
320 {
321 const URLProtocol **protocols;
322 char proto_str[128], proto_nested[128], *ptr;
323 180504 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
324 int i;
325
326
2/2
✓ Branch 0 taken 169122 times.
✓ Branch 1 taken 11382 times.
180504 if (filename[proto_len] != ':' &&
327
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 169122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 11382 times.
180504 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
328 11382 is_dos_path(filename))
329 169122 strcpy(proto_str, "file");
330 else
331 11382 av_strlcpy(proto_str, filename,
332 11382 FFMIN(proto_len + 1, sizeof(proto_str)));
333
334 180504 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180504 times.
180504 if ((ptr = strchr(proto_nested, '+')))
336 ✗ *ptr = '\0';
337
338 180504 protocols = ffurl_get_protocols(NULL, NULL);
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180504 times.
180504 if (!protocols)
340 ✗ return NULL;
341
1/2
✓ Branch 0 taken 1721321 times.
✗ Branch 1 not taken.
1721321 for (i = 0; protocols[i]; i++) {
342 1721321 const URLProtocol *up = protocols[i];
343
2/2
✓ Branch 0 taken 180504 times.
✓ Branch 1 taken 1540817 times.
1721321 if (!strcmp(proto_str, up->name)) {
344 180504 av_freep(&protocols);
345 180504 return up;
346 }
347
2/2
✓ Branch 0 taken 180501 times.
✓ Branch 1 taken 1360316 times.
1540817 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180501 times.
180501 !strcmp(proto_nested, up->name)) {
349 ✗ av_freep(&protocols);
350 ✗ return up;
351 }
352 }
353 ✗ av_freep(&protocols);
354 ✗ if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL) ||
355 ✗ av_strstart(filename, "dtls:", NULL))
356 ✗ av_log(NULL, AV_LOG_WARNING, "https or dtls protocol not found, recompile FFmpeg with "
357 "openssl, gnutls or securetransport enabled.\n");
358
359 ✗ return NULL;
360 }
361
362 170097 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
363 const AVIOInterruptCB *int_cb)
364 {
365 170097 const URLProtocol *p = NULL;
366
367 170097 p = url_find_protocol(filename);
368
1/2
✓ Branch 0 taken 170097 times.
✗ Branch 1 not taken.
170097 if (p)
369 170097 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
370
371 ✗ *puc = NULL;
372 ✗ return AVERROR_PROTOCOL_NOT_FOUND;
373 }
374
375 #if CONFIG_LIBCURL_PROTOCOL
376 extern const URLProtocol ff_libcurl_protocol;
377
378 /* Decide whether an http(s) URL should be routed to the libcurl protocol instead
379 * of the native one. Controlled by the per-open "prefer_libcurl" option. */
380 117263 static int prefer_libcurl(const char *filename, AVDictionary **options,
381 URLContext *parent)
382 {
383 117263 URLContext dummy = { .av_class = &url_context_class };
384 AVDictionaryEntry *e;
385
386
2/4
✓ Branch 1 taken 117263 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 117263 times.
✗ Branch 4 not taken.
234526 if (!av_strstart(filename, "http://", NULL) &&
387 117263 !av_strstart(filename, "https://", NULL))
388 117263 return 0;
389
390 ✗ if (parent && parent->prefer_libcurl)
391 ✗ return 1;
392
393 ✗ if (!options || !(e = av_dict_get(*options, "prefer_libcurl", NULL, 0)))
394 ✗ return 0;
395 ✗ if (av_opt_set(&dummy, "prefer_libcurl", e->value, 0) < 0)
396 ✗ return 0;
397
398 ✗ return dummy.prefer_libcurl;
399 }
400 #endif
401
402 117263 static int url_open_whitelist(URLContext **puc, const char *filename, int flags,
403 const AVIOInterruptCB *int_cb, AVDictionary **options,
404 const char *whitelist, const char* blacklist,
405 URLContext *parent, AVFormatContext *avfc)
406 {
407 117263 AVDictionary *tmp_opts = NULL;
408 AVDictionaryEntry *e;
409 int ret;
410
411 #if CONFIG_LIBCURL_PROTOCOL
412 /* The option itself is applied to the URLContext further down; here it only
413 * picks the protocol, before the context exists. */
414
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if (prefer_libcurl(filename, options, parent))
415 ✗ ret = url_alloc_for_protocol(puc, &ff_libcurl_protocol, filename, flags,
416 int_cb);
417 else
418 #endif
419 117263 ret = ffurl_alloc(puc, filename, flags, int_cb);
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117263 times.
117263 if (ret < 0)
421 ✗ return ret;
422 117263 (*puc)->avfc = avfc;
423
2/2
✓ Branch 0 taken 411 times.
✓ Branch 1 taken 116852 times.
117263 if (parent) {
424 411 ret = av_opt_copy(*puc, parent);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 if (ret < 0)
426 ✗ goto fail;
427 }
428
3/4
✓ Branch 0 taken 14749 times.
✓ Branch 1 taken 102514 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14749 times.
132012 if (options &&
429 14749 (ret = av_opt_set_dict(*puc, options)) < 0)
430 ✗ goto fail;
431
5/6
✓ Branch 0 taken 14749 times.
✓ Branch 1 taken 102514 times.
✓ Branch 2 taken 11943 times.
✓ Branch 3 taken 2806 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 11943 times.
129206 if (options && (*puc)->prot->priv_data_class &&
432 11943 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
433 ✗ goto fail;
434
435
2/2
✓ Branch 0 taken 102514 times.
✓ Branch 1 taken 14749 times.
117263 if (!options)
436 102514 options = &tmp_opts;
437
438
3/6
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 117074 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 189 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
117263 av_assert0(!whitelist ||
439 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
440 !strcmp(whitelist, e->value));
441
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 117263 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
117263 av_assert0(!blacklist ||
442 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
443 !strcmp(blacklist, e->value));
444
445
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
446 ✗ goto fail;
447
448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
449 ✗ goto fail;
450
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117263 times.
117263 if ((ret = av_opt_set_dict(*puc, options)) < 0)
452 ✗ goto fail;
453
454 117263 ret = ffurl_connect(*puc, options);
455
456
2/2
✓ Branch 0 taken 117262 times.
✓ Branch 1 taken 1 times.
117263 if (!ret) {
457
2/2
✓ Branch 0 taken 411 times.
✓ Branch 1 taken 116851 times.
117262 if (parent)
458 411 parent->uses_network |= (*puc)->uses_network;
459 117262 return 0;
460 }
461 1 fail:
462 1 ffurl_closep(puc);
463 1 return ret;
464 }
465
466 411 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
467 const AVIOInterruptCB *int_cb, AVDictionary **options,
468 const char *whitelist, const char* blacklist,
469 URLContext *parent)
470 {
471 411 return url_open_whitelist(puc, filename, flags, int_cb, options,
472 whitelist, blacklist, parent, NULL);
473 }
474
475 116851 int ffio_fdopen(AVIOContext **sp, URLContext *h)
476 {
477 AVIOContext *s;
478 116851 uint8_t *buffer = NULL;
479 int buffer_size, max_packet_size;
480
481 116851 max_packet_size = h->max_packet_size;
482
2/2
✓ Branch 0 taken 3923 times.
✓ Branch 1 taken 112928 times.
116851 if (max_packet_size) {
483 3923 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
484
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 112928 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
112928 } else if (h->uses_network && !(h->flags & AVIO_FLAG_WRITE)) {
485 ✗ buffer_size = NETWORK_IO_BUFFER_SIZE;
486 } else {
487 112928 buffer_size = IO_BUFFER_SIZE;
488 }
489
3/4
✓ Branch 0 taken 107225 times.
✓ Branch 1 taken 9626 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 107225 times.
116851 if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
490 ✗ if (buffer_size > INT_MAX/2)
491 ✗ return AVERROR(EINVAL);
492 ✗ buffer_size *= 2;
493 }
494 116851 buffer = av_malloc(buffer_size);
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (!buffer)
496 ✗ return AVERROR(ENOMEM);
497
498 116851 *sp = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
499 ffurl_read2, ffurl_write2, ffurl_seek2);
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (!*sp) {
501 ✗ av_freep(&buffer);
502 ✗ return AVERROR(ENOMEM);
503 }
504 116851 s = *sp;
505
2/2
✓ Branch 0 taken 114046 times.
✓ Branch 1 taken 2805 times.
116851 if (h->protocol_whitelist) {
506 114046 s->protocol_whitelist = av_strdup(h->protocol_whitelist);
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114046 times.
114046 if (!s->protocol_whitelist) {
508 ✗ avio_closep(sp);
509 ✗ return AVERROR(ENOMEM);
510 }
511 }
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (h->protocol_blacklist) {
513 ✗ s->protocol_blacklist = av_strdup(h->protocol_blacklist);
514 ✗ if (!s->protocol_blacklist) {
515 ✗ avio_closep(sp);
516 ✗ return AVERROR(ENOMEM);
517 }
518 }
519 116851 s->direct = h->flags & AVIO_FLAG_DIRECT;
520
521 116851 s->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
522 116851 s->max_packet_size = max_packet_size;
523 116851 s->min_packet_size = h->min_packet_size;
524
1/2
✓ Branch 0 taken 116851 times.
✗ Branch 1 not taken.
116851 if(h->prot) {
525 116851 s->read_pause = h->prot->url_read_pause;
526 116851 s->read_seek = h->prot->url_read_seek;
527
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (h->prot->url_read_seek)
529 ✗ s->seekable |= AVIO_SEEKABLE_TIME;
530 }
531 116851 ((FFIOContext*)s)->short_seek_get = ffurl_get_short_seek;
532 116851 s->av_class = &ff_avio_class;
533 116851 return 0;
534 }
535
536 116852 int ffio_open_whitelist2(AVIOContext **s, const char *filename, int flags,
537 const AVIOInterruptCB *int_cb, AVDictionary **options,
538 const char *whitelist, const char *blacklist,
539 AVFormatContext *avfc)
540 {
541 URLContext *h;
542 int err;
543
544 116852 *s = NULL;
545
546 116852 err = url_open_whitelist(&h, filename, flags, int_cb, options, whitelist,
547 blacklist, NULL, avfc);
548
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 116851 times.
116852 if (err < 0)
549 1 return err;
550 116851 err = ffio_fdopen(s, h);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (err < 0) {
552 ✗ ffurl_close(h);
553 ✗ return err;
554 }
555 116851 return 0;
556 }
557
558 8821 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
559 const AVIOInterruptCB *int_cb, AVDictionary **options,
560 const char *whitelist, const char *blacklist)
561 {
562 8821 return ffio_open_whitelist2(s, filename, flags, int_cb, options,
563 whitelist, blacklist, NULL);
564 }
565
566 8821 int avio_open2(AVIOContext **s, const char *filename, int flags,
567 const AVIOInterruptCB *int_cb, AVDictionary **options)
568 {
569 8821 return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
570 }
571
572 124 int avio_open(AVIOContext **s, const char *filename, int flags)
573 {
574 124 return avio_open2(s, filename, flags, NULL, NULL);
575 }
576
577
578 636595 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
579 const uint8_t *cbuf,
580 int size, int size_min,
581 int read)
582 {
583 int ret, len;
584 636595 int fast_retries = 5;
585 636595 int64_t wait_since = 0;
586
587 636595 len = 0;
588
2/2
✓ Branch 0 taken 636595 times.
✓ Branch 1 taken 623211 times.
1259806 while (len < size_min) {
589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 636595 times.
636595 if (ff_check_interrupt(&h->interrupt_callback))
590 ✗ return AVERROR_EXIT;
591
2/2
✓ Branch 0 taken 226305 times.
✓ Branch 1 taken 410290 times.
636595 ret = read ? h->prot->url_read (h, buf + len, size - len):
592 410290 h->prot->url_write(h, cbuf + len, size - len);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636595 times.
636595 if (ret == AVERROR(EINTR))
594 ✗ continue;
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636595 times.
636595 if (h->flags & AVIO_FLAG_NONBLOCK)
596 ✗ return ret;
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636595 times.
636595 if (ret == AVERROR(EAGAIN)) {
598 ✗ ret = 0;
599 ✗ if (fast_retries) {
600 ✗ fast_retries--;
601 } else {
602 ✗ if (h->rw_timeout) {
603 ✗ if (!wait_since)
604 ✗ wait_since = av_gettime_relative();
605 ✗ else if (av_gettime_relative() > wait_since + h->rw_timeout)
606 ✗ return AVERROR(EIO);
607 }
608 ✗ av_usleep(1000);
609 }
610
2/2
✓ Branch 0 taken 13384 times.
✓ Branch 1 taken 623211 times.
636595 } else if (ret == AVERROR_EOF)
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13384 times.
13384 return (len > 0) ? len : AVERROR_EOF;
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 623211 times.
623211 else if (ret < 0)
613 ✗ return ret;
614
1/2
✓ Branch 0 taken 623211 times.
✗ Branch 1 not taken.
623211 if (ret) {
615 623211 fast_retries = FFMAX(fast_retries, 2);
616 623211 wait_since = 0;
617 }
618 623211 len += ret;
619 }
620 623211 return len;
621 }
622
623 226305 int ffurl_read2(void *urlcontext, uint8_t *buf, int size)
624 {
625 226305 URLContext *h = urlcontext;
626
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 226305 times.
226305 if (!(h->flags & AVIO_FLAG_READ))
628 ✗ return AVERROR(EIO);
629 226305 return retry_transfer_wrapper(h, buf, NULL, size, 1, 1);
630 }
631
632 ✗ int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
633 {
634 ✗ if (!(h->flags & AVIO_FLAG_READ))
635 ✗ return AVERROR(EIO);
636 ✗ return retry_transfer_wrapper(h, buf, NULL, size, size, 1);
637 }
638
639 410290 int ffurl_write2(void *urlcontext, const uint8_t *buf, int size)
640 {
641 410290 URLContext *h = urlcontext;
642
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410290 times.
410290 if (!(h->flags & AVIO_FLAG_WRITE))
644 ✗ return AVERROR(EIO);
645 /* avoid sending too big packets */
646
3/4
✓ Branch 0 taken 81596 times.
✓ Branch 1 taken 328694 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 81596 times.
410290 if (h->max_packet_size && size > h->max_packet_size)
647 ✗ return AVERROR(EIO);
648
649 410290 return retry_transfer_wrapper(h, NULL, buf, size, size, 0);
650 }
651
652 415389 int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence)
653 {
654 415389 URLContext *h = urlcontext;
655 int64_t ret;
656
657
2/2
✓ Branch 0 taken 31936 times.
✓ Branch 1 taken 383453 times.
415389 if (!h->prot->url_seek)
658 31936 return AVERROR(ENOSYS);
659 383453 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
660 383453 return ret;
661 }
662
663 170097 int ffurl_closep(URLContext **hh)
664 {
665 170097 URLContext *h= *hh;
666 170097 int ret = 0;
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170097 times.
170097 if (!h)
668 ✗ return 0; /* can happen when ffurl_open fails */
669
670
3/4
✓ Branch 0 taken 117262 times.
✓ Branch 1 taken 52835 times.
✓ Branch 2 taken 117262 times.
✗ Branch 3 not taken.
170097 if (h->is_connected && h->prot->url_close)
671 117262 ret = h->prot->url_close(h);
672 #if CONFIG_NETWORK
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170097 times.
170097 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
674 ✗ ff_network_close();
675 #endif
676
1/2
✓ Branch 0 taken 170097 times.
✗ Branch 1 not taken.
170097 if (h->prot->priv_data_size) {
677
2/2
✓ Branch 0 taken 167291 times.
✓ Branch 1 taken 2806 times.
170097 if (h->prot->priv_data_class)
678 167291 av_opt_free(h->priv_data);
679 170097 av_freep(&h->priv_data);
680 }
681 170097 av_opt_free(h);
682 170097 av_freep(hh);
683 170097 return ret;
684 }
685
686 170089 int ffurl_close(URLContext *h)
687 {
688 170089 return ffurl_closep(&h);
689 }
690
691 125547 int avio_close(AVIOContext *s)
692 {
693 125547 FFIOContext *const ctx = ffiocontext(s);
694 URLContext *h;
695 int ret, error;
696
697
2/2
✓ Branch 0 taken 8696 times.
✓ Branch 1 taken 116851 times.
125547 if (!s)
698 8696 return 0;
699
700 116851 avio_flush(s);
701 116851 h = s->opaque;
702 116851 s->opaque = NULL;
703
704 116851 av_freep(&s->buffer);
705
2/2
✓ Branch 0 taken 9626 times.
✓ Branch 1 taken 107225 times.
116851 if (s->write_flag)
706 9626 av_log(s, AV_LOG_VERBOSE,
707 "Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",
708 ctx->bytes_written, ctx->seek_count, ctx->writeout_count);
709 else
710 107225 av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",
711 ctx->bytes_read, ctx->seek_count);
712 116851 av_opt_free(s);
713
714 116851 error = s->error;
715 116851 avio_context_free(&s);
716
717 116851 ret = ffurl_close(h);
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116851 times.
116851 if (ret < 0)
719 ✗ return ret;
720
721 116851 return error;
722 }
723
724 17517 int avio_closep(AVIOContext **s)
725 {
726 17517 int ret = avio_close(*s);
727 17517 *s = NULL;
728 17517 return ret;
729 }
730
731
732 10407 const char *avio_find_protocol_name(const char *url)
733 {
734 10407 const URLProtocol *p = url_find_protocol(url);
735
736
1/2
✓ Branch 0 taken 10407 times.
✗ Branch 1 not taken.
10407 return p ? p->name : NULL;
737 }
738
739 52602 int avio_check(const char *url, int flags)
740 {
741 URLContext *h;
742 52602 int ret = ffurl_alloc(&h, url, flags, NULL);
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52602 times.
52602 if (ret < 0)
744 ✗ return ret;
745
746
1/2
✓ Branch 0 taken 52602 times.
✗ Branch 1 not taken.
52602 if (h->prot->url_check) {
747 52602 ret = h->prot->url_check(h, flags);
748 } else {
749 ✗ ret = ffurl_connect(h, NULL);
750 ✗ if (ret >= 0)
751 ✗ ret = flags;
752 }
753
754 52602 ffurl_close(h);
755 52602 return ret;
756 }
757
758 116 int ffurl_move(const char *url_src, const char *url_dst)
759 {
760 URLContext *h_src, *h_dst;
761 116 int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (ret < 0)
763 ✗ return ret;
764 116 ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (ret < 0) {
766 ✗ ffurl_close(h_src);
767 ✗ return ret;
768 }
769
770
2/4
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
116 if (h_src->prot == h_dst->prot && h_src->prot->url_move)
771 116 ret = h_src->prot->url_move(h_src, h_dst);
772 else
773 ✗ ret = AVERROR(ENOSYS);
774
775 116 ffurl_close(h_src);
776 116 ffurl_close(h_dst);
777 116 return ret;
778 }
779
780 ✗ int ffurl_delete(const char *url)
781 {
782 URLContext *h;
783 ✗ int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
784 ✗ if (ret < 0)
785 ✗ return ret;
786
787 ✗ if (h->prot->url_delete)
788 ✗ ret = h->prot->url_delete(h);
789 else
790 ✗ ret = AVERROR(ENOSYS);
791
792 ✗ ffurl_close(h);
793 ✗ return ret;
794 }
795
796 struct AVIODirContext {
797 struct URLContext *url_context;
798 };
799
800 ✗ int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
801 {
802 ✗ URLContext *h = NULL;
803 ✗ AVIODirContext *ctx = NULL;
804 int ret;
805 ✗ av_assert0(s);
806
807 ✗ ctx = av_mallocz(sizeof(*ctx));
808 ✗ if (!ctx) {
809 ✗ ret = AVERROR(ENOMEM);
810 ✗ goto fail;
811 }
812
813 ✗ if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
814 ✗ goto fail;
815
816 ✗ if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
817 ✗ if (options && h->prot->priv_data_class &&
818 ✗ (ret = av_opt_set_dict(h->priv_data, options)) < 0)
819 ✗ goto fail;
820 ✗ ret = h->prot->url_open_dir(h);
821 } else
822 ✗ ret = AVERROR(ENOSYS);
823 ✗ if (ret < 0)
824 ✗ goto fail;
825
826 ✗ h->is_connected = 1;
827 ✗ ctx->url_context = h;
828 ✗ *s = ctx;
829 ✗ return 0;
830
831 ✗ fail:
832 ✗ av_free(ctx);
833 ✗ *s = NULL;
834 ✗ ffurl_close(h);
835 ✗ return ret;
836 }
837
838 ✗ int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
839 {
840 URLContext *h;
841 int ret;
842
843 ✗ if (!s || !s->url_context)
844 ✗ return AVERROR(EINVAL);
845 ✗ h = s->url_context;
846 ✗ if ((ret = h->prot->url_read_dir(h, next)) < 0)
847 ✗ avio_free_directory_entry(next);
848 ✗ return ret;
849 }
850
851 ✗ int avio_close_dir(AVIODirContext **s)
852 {
853 URLContext *h;
854
855 ✗ av_assert0(s);
856 ✗ if (!(*s) || !(*s)->url_context)
857 ✗ return AVERROR(EINVAL);
858 ✗ h = (*s)->url_context;
859 ✗ h->prot->url_close_dir(h);
860 ✗ ffurl_close(h);
861 ✗ av_freep(s);
862 ✗ *s = NULL;
863 ✗ return 0;
864 }
865
866 ✗ void avio_free_directory_entry(AVIODirEntry **entry)
867 {
868 ✗ if (!entry || !*entry)
869 ✗ return;
870 ✗ av_free((*entry)->name);
871 ✗ av_freep(entry);
872 }
873
874 6 int64_t ffurl_size(URLContext *h)
875 {
876 int64_t pos, size;
877
878 6 size = ffurl_seek(h, 0, AVSEEK_SIZE);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (size < 0) {
880 ✗ pos = ffurl_seek(h, 0, SEEK_CUR);
881 ✗ if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
882 ✗ return size;
883 ✗ size++;
884 ✗ ffurl_seek(h, pos, SEEK_SET);
885 }
886 6 return size;
887 }
888
889 ✗ int ffurl_get_file_handle(URLContext *h)
890 {
891 ✗ if (!h || !h->prot || !h->prot->url_get_file_handle)
892 ✗ return -1;
893 ✗ return h->prot->url_get_file_handle(h);
894 }
895
896 ✗ int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
897 {
898 ✗ if (!h || !h->prot)
899 ✗ return AVERROR(ENOSYS);
900 ✗ if (!h->prot->url_get_multi_file_handle) {
901 ✗ if (!h->prot->url_get_file_handle)
902 ✗ return AVERROR(ENOSYS);
903 ✗ *handles = av_malloc(sizeof(**handles));
904 ✗ if (!*handles)
905 ✗ return AVERROR(ENOMEM);
906 ✗ *numhandles = 1;
907 ✗ *handles[0] = h->prot->url_get_file_handle(h);
908 ✗ return 0;
909 }
910 ✗ return h->prot->url_get_multi_file_handle(h, handles, numhandles);
911 }
912
913 375497 int ffurl_get_short_seek(void *urlcontext)
914 {
915 375497 URLContext *h = urlcontext;
916
917
3/6
✓ Branch 0 taken 375497 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 375497 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 375497 times.
✗ Branch 5 not taken.
375497 if (!h || !h->prot || !h->prot->url_get_short_seek)
918 375497 return AVERROR(ENOSYS);
919 ✗ return h->prot->url_get_short_seek(h);
920 }
921
922 ✗ int ffurl_shutdown(URLContext *h, int flags)
923 {
924 ✗ if (!h || !h->prot || !h->prot->url_shutdown)
925 ✗ return AVERROR(ENOSYS);
926 ✗ return h->prot->url_shutdown(h, flags);
927 }
928
929 846909 int ff_check_interrupt(AVIOInterruptCB *cb)
930 {
931
3/4
✓ Branch 0 taken 846909 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 815977 times.
✓ Branch 3 taken 30932 times.
846909 if (cb && cb->callback)
932 815977 return cb->callback(cb->opaque);
933 30932 return 0;
934 }
935
936 116 int ff_rename(const char *url_src, const char *url_dst, void *logctx)
937 {
938 116 int ret = ffurl_move(url_src, url_dst);
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (ret < 0)
940 ✗ av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));
941 116 return ret;
942 }
943