FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avio.c
Date: 2026-08-28 22:32:55
Exec Total Coverage
Lines: 278 510 54.5%
Functions: 32 45 71.1%
Branches: 161 384 41.9%

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