FFmpeg coverage


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