FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avio.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 188 387 48.6%
Functions: 19 31 61.3%
Branches: 127 320 39.7%

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/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/avassert.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #include "internal.h"
30 #if CONFIG_NETWORK
31 #include "network.h"
32 #endif
33 #include "url.h"
34
35 /** @name Logging context. */
36 /*@{*/
37 6 static const char *urlcontext_to_name(void *ptr)
38 {
39 6 URLContext *h = (URLContext *)ptr;
40
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (h->prot)
41 6 return h->prot->name;
42 else
43 return "NULL";
44 }
45
46 32944 static void *urlcontext_child_next(void *obj, void *prev)
47 {
48 32944 URLContext *h = obj;
49
4/6
✓ Branch 0 taken 16472 times.
✓ Branch 1 taken 16472 times.
✓ Branch 2 taken 16472 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16472 times.
✗ Branch 5 not taken.
32944 if (!prev && h->priv_data && h->prot->priv_data_class)
50 16472 return h->priv_data;
51 16472 return NULL;
52 }
53
54 #define OFFSET(x) offsetof(URLContext,x)
55 #define E AV_OPT_FLAG_ENCODING_PARAM
56 #define D AV_OPT_FLAG_DECODING_PARAM
57 static const AVOption options[] = {
58 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
59 {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
60 {"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 },
61 { NULL }
62 };
63
64 const AVClass ffurl_context_class = {
65 .class_name = "URLContext",
66 .item_name = urlcontext_to_name,
67 .option = options,
68 .version = LIBAVUTIL_VERSION_INT,
69 .child_next = urlcontext_child_next,
70 .child_class_iterate = ff_urlcontext_child_class_iterate,
71 };
72 /*@}*/
73
74 141012 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
75 const char *filename, int flags,
76 const AVIOInterruptCB *int_cb)
77 {
78 URLContext *uc;
79 int err;
80
81 #if CONFIG_NETWORK
82
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 141012 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
141012 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
83 return AVERROR(EIO);
84 #endif
85
3/4
✓ Branch 0 taken 133722 times.
✓ Branch 1 taken 7290 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 133722 times.
141012 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
86 av_log(NULL, AV_LOG_ERROR,
87 "Impossible to open the '%s' protocol for reading\n", up->name);
88 return AVERROR(EIO);
89 }
90
3/4
✓ Branch 0 taken 7368 times.
✓ Branch 1 taken 133644 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7368 times.
141012 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
91 av_log(NULL, AV_LOG_ERROR,
92 "Impossible to open the '%s' protocol for writing\n", up->name);
93 return AVERROR(EIO);
94 }
95 141012 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141012 times.
141012 if (!uc) {
97 err = AVERROR(ENOMEM);
98 goto fail;
99 }
100 141012 uc->av_class = &ffurl_context_class;
101 141012 uc->filename = (char *)&uc[1];
102 141012 strcpy(uc->filename, filename);
103 141012 uc->prot = up;
104 141012 uc->flags = flags;
105 141012 uc->is_streamed = 0; /* default = not streamed */
106 141012 uc->max_packet_size = 0; /* default: stream file */
107
1/2
✓ Branch 0 taken 141012 times.
✗ Branch 1 not taken.
141012 if (up->priv_data_size) {
108 141012 uc->priv_data = av_mallocz(up->priv_data_size);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141012 times.
141012 if (!uc->priv_data) {
110 err = AVERROR(ENOMEM);
111 goto fail;
112 }
113
2/2
✓ Branch 0 taken 138691 times.
✓ Branch 1 taken 2321 times.
141012 if (up->priv_data_class) {
114 char *start;
115 138691 *(const AVClass **)uc->priv_data = up->priv_data_class;
116 138691 av_opt_set_defaults(uc->priv_data);
117
3/4
✓ Branch 1 taken 2488 times.
✓ Branch 2 taken 136203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2488 times.
138691 if (av_strstart(uc->filename, up->name, (const char**)&start) && *start == ',') {
118 int ret= 0;
119 char *p= start;
120 char sep= *++p;
121 char *key, *val;
122 p++;
123
124 if (strcmp(up->name, "subfile"))
125 ret = AVERROR(EINVAL);
126
127 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
128 *val= *key= 0;
129 if (strcmp(p, "start") && strcmp(p, "end")) {
130 ret = AVERROR_OPTION_NOT_FOUND;
131 } else
132 ret= av_opt_set(uc->priv_data, p, key+1, 0);
133 if (ret == AVERROR_OPTION_NOT_FOUND)
134 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
135 *val= *key= sep;
136 p= val+1;
137 }
138 if(ret<0 || p!=key){
139 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
140 av_freep(&uc->priv_data);
141 av_freep(&uc);
142 err = AVERROR(EINVAL);
143 goto fail;
144 }
145 memmove(start, key+1, strlen(key));
146 }
147 }
148 }
149
2/2
✓ Branch 0 taken 96809 times.
✓ Branch 1 taken 44203 times.
141012 if (int_cb)
150 96809 uc->interrupt_callback = *int_cb;
151
152 141012 *puc = uc;
153 141012 return 0;
154 fail:
155 *puc = NULL;
156 if (uc)
157 av_freep(&uc->priv_data);
158 av_freep(&uc);
159 #if CONFIG_NETWORK
160 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
161 ff_network_close();
162 #endif
163 return err;
164 }
165
166 96864 int ffurl_connect(URLContext *uc, AVDictionary **options)
167 {
168 int err;
169 96864 AVDictionary *tmp_opts = NULL;
170 AVDictionaryEntry *e;
171
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96864 times.
96864 if (!options)
173 options = &tmp_opts;
174
175 // Check that URLContext was initialized correctly and lists are matching if set
176
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
96864 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
177 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
178
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
96864 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
179 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
180
181
3/4
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 96731 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 133 times.
96864 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
182 av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
183 return AVERROR(EINVAL);
184 }
185
186
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 96864 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
96864 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
187 av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
188 return AVERROR(EINVAL);
189 }
190
191
4/4
✓ Branch 0 taken 96731 times.
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 94409 times.
✓ Branch 3 taken 2322 times.
96864 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
192 94409 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
193 94409 uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94409 times.
94409 if (!uc->protocol_whitelist) {
195 return AVERROR(ENOMEM);
196 }
197
2/2
✓ Branch 0 taken 2322 times.
✓ Branch 1 taken 133 times.
2455 } else if (!uc->protocol_whitelist)
198 2322 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
199
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
96864 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
201 return err;
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
96864 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
203 return err;
204
205 96864 err =
206 96865 uc->prot->url_open2 ? uc->prot->url_open2(uc,
207 1 uc->filename,
208 uc->flags,
209
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96863 times.
96864 options) :
210 96863 uc->prot->url_open(uc, uc->filename, uc->flags);
211
212 96864 av_dict_set(options, "protocol_whitelist", NULL, 0);
213 96864 av_dict_set(options, "protocol_blacklist", NULL, 0);
214
215
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96863 times.
96864 if (err)
216 1 return err;
217 96863 uc->is_connected = 1;
218 /* We must be careful here as ffurl_seek() could be slow,
219 * for example for http */
220
4/4
✓ Branch 0 taken 89651 times.
✓ Branch 1 taken 7212 times.
✓ Branch 2 taken 89649 times.
✓ Branch 3 taken 2 times.
96863 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
221
4/4
✓ Branch 0 taken 94375 times.
✓ Branch 1 taken 2486 times.
✓ Branch 3 taken 2321 times.
✓ Branch 4 taken 92054 times.
96861 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
222 2321 uc->is_streamed = 1;
223 96863 return 0;
224 }
225
226 int ffurl_accept(URLContext *s, URLContext **c)
227 {
228 av_assert0(!*c);
229 if (s->prot->url_accept)
230 return s->prot->url_accept(s, c);
231 return AVERROR(EBADF);
232 }
233
234 int ffurl_handshake(URLContext *c)
235 {
236 int ret;
237 if (c->prot->url_handshake) {
238 ret = c->prot->url_handshake(c);
239 if (ret)
240 return ret;
241 }
242 c->is_connected = 1;
243 return 0;
244 }
245
246 #define URL_SCHEME_CHARS \
247 "abcdefghijklmnopqrstuvwxyz" \
248 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
249 "0123456789+-."
250
251 148494 static const struct URLProtocol *url_find_protocol(const char *filename)
252 {
253 const URLProtocol **protocols;
254 char proto_str[128], proto_nested[128], *ptr;
255 148494 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
256 int i;
257
258
2/2
✓ Branch 0 taken 138902 times.
✓ Branch 1 taken 9592 times.
148494 if (filename[proto_len] != ':' &&
259
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 138902 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9592 times.
148494 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
260 9592 is_dos_path(filename))
261 138902 strcpy(proto_str, "file");
262 else
263 9592 av_strlcpy(proto_str, filename,
264 9592 FFMIN(proto_len + 1, sizeof(proto_str)));
265
266 148494 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148494 times.
148494 if ((ptr = strchr(proto_nested, '+')))
268 *ptr = '\0';
269
270 148494 protocols = ffurl_get_protocols(NULL, NULL);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148494 times.
148494 if (!protocols)
272 return NULL;
273
1/2
✓ Branch 0 taken 1279215 times.
✗ Branch 1 not taken.
1279215 for (i = 0; protocols[i]; i++) {
274 1279215 const URLProtocol *up = protocols[i];
275
2/2
✓ Branch 0 taken 148494 times.
✓ Branch 1 taken 1130721 times.
1279215 if (!strcmp(proto_str, up->name)) {
276 148494 av_freep(&protocols);
277 148494 return up;
278 }
279
2/2
✓ Branch 0 taken 158084 times.
✓ Branch 1 taken 972637 times.
1130721 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158084 times.
158084 !strcmp(proto_nested, up->name)) {
281 av_freep(&protocols);
282 return up;
283 }
284 }
285 av_freep(&protocols);
286 if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))
287 av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
288 "openssl, gnutls or securetransport enabled.\n");
289
290 return NULL;
291 }
292
293 141012 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
294 const AVIOInterruptCB *int_cb)
295 {
296 141012 const URLProtocol *p = NULL;
297
298 141012 p = url_find_protocol(filename);
299
1/2
✓ Branch 0 taken 141012 times.
✗ Branch 1 not taken.
141012 if (p)
300 141012 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
301
302 *puc = NULL;
303 return AVERROR_PROTOCOL_NOT_FOUND;
304 }
305
306 96864 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
307 const AVIOInterruptCB *int_cb, AVDictionary **options,
308 const char *whitelist, const char* blacklist,
309 URLContext *parent)
310 {
311 96864 AVDictionary *tmp_opts = NULL;
312 AVDictionaryEntry *e;
313 96864 int ret = ffurl_alloc(puc, filename, flags, int_cb);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96864 times.
96864 if (ret < 0)
315 return ret;
316
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96863 times.
96864 if (parent) {
317 1 ret = av_opt_copy(*puc, parent);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
319 goto fail;
320 }
321
3/4
✓ Branch 0 taken 11398 times.
✓ Branch 1 taken 85466 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11398 times.
108262 if (options &&
322 11398 (ret = av_opt_set_dict(*puc, options)) < 0)
323 goto fail;
324
5/6
✓ Branch 0 taken 11398 times.
✓ Branch 1 taken 85466 times.
✓ Branch 2 taken 9077 times.
✓ Branch 3 taken 2321 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9077 times.
105941 if (options && (*puc)->prot->priv_data_class &&
325 9077 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
326 goto fail;
327
328
2/2
✓ Branch 0 taken 85466 times.
✓ Branch 1 taken 11398 times.
96864 if (!options)
329 85466 options = &tmp_opts;
330
331
3/6
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 96731 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 133 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
96864 av_assert0(!whitelist ||
332 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
333 !strcmp(whitelist, e->value));
334
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 96864 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
96864 av_assert0(!blacklist ||
335 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
336 !strcmp(blacklist, e->value));
337
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
96864 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
339 goto fail;
340
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
96864 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
342 goto fail;
343
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96864 times.
96864 if ((ret = av_opt_set_dict(*puc, options)) < 0)
345 goto fail;
346
347 96864 ret = ffurl_connect(*puc, options);
348
349
2/2
✓ Branch 0 taken 96863 times.
✓ Branch 1 taken 1 times.
96864 if (!ret)
350 96863 return 0;
351 1 fail:
352 1 ffurl_closep(puc);
353 1 return ret;
354 }
355
356 503759 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
357 int size, int size_min,
358 int (*transfer_func)(URLContext *h,
359 uint8_t *buf,
360 int size))
361 {
362 int ret, len;
363 503759 int fast_retries = 5;
364 503759 int64_t wait_since = 0;
365
366 503759 len = 0;
367
2/2
✓ Branch 0 taken 503759 times.
✓ Branch 1 taken 492005 times.
995764 while (len < size_min) {
368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 503759 times.
503759 if (ff_check_interrupt(&h->interrupt_callback))
369 return AVERROR_EXIT;
370 503759 ret = transfer_func(h, buf + len, size - len);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 503759 times.
503759 if (ret == AVERROR(EINTR))
372 continue;
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 503759 times.
503759 if (h->flags & AVIO_FLAG_NONBLOCK)
374 return ret;
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 503759 times.
503759 if (ret == AVERROR(EAGAIN)) {
376 ret = 0;
377 if (fast_retries) {
378 fast_retries--;
379 } else {
380 if (h->rw_timeout) {
381 if (!wait_since)
382 wait_since = av_gettime_relative();
383 else if (av_gettime_relative() > wait_since + h->rw_timeout)
384 return AVERROR(EIO);
385 }
386 av_usleep(1000);
387 }
388
2/2
✓ Branch 0 taken 11754 times.
✓ Branch 1 taken 492005 times.
503759 } else if (ret == AVERROR_EOF)
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11754 times.
11754 return (len > 0) ? len : AVERROR_EOF;
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 492005 times.
492005 else if (ret < 0)
391 return ret;
392
1/2
✓ Branch 0 taken 492005 times.
✗ Branch 1 not taken.
492005 if (ret) {
393 492005 fast_retries = FFMAX(fast_retries, 2);
394 492005 wait_since = 0;
395 }
396 492005 len += ret;
397 }
398 492005 return len;
399 }
400
401 195114 int ffurl_read(URLContext *h, unsigned char *buf, int size)
402 {
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195114 times.
195114 if (!(h->flags & AVIO_FLAG_READ))
404 return AVERROR(EIO);
405 195114 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
406 }
407
408 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
409 {
410 if (!(h->flags & AVIO_FLAG_READ))
411 return AVERROR(EIO);
412 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
413 }
414
415 308645 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
416 {
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308645 times.
308645 if (!(h->flags & AVIO_FLAG_WRITE))
418 return AVERROR(EIO);
419 /* avoid sending too big packets */
420
3/4
✓ Branch 0 taken 24191 times.
✓ Branch 1 taken 284454 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24191 times.
308645 if (h->max_packet_size && size > h->max_packet_size)
421 return AVERROR(EIO);
422
423 308645 return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
424 (int (*)(struct URLContext *, uint8_t *, int))
425 308645 h->prot->url_write);
426 }
427
428 325989 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
429 {
430 int64_t ret;
431
432
2/2
✓ Branch 0 taken 30235 times.
✓ Branch 1 taken 295754 times.
325989 if (!h->prot->url_seek)
433 30235 return AVERROR(ENOSYS);
434 295754 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
435 295754 return ret;
436 }
437
438 141012 int ffurl_closep(URLContext **hh)
439 {
440 141012 URLContext *h= *hh;
441 141012 int ret = 0;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141012 times.
141012 if (!h)
443 return 0; /* can happen when ffurl_open fails */
444
445
4/4
✓ Branch 0 taken 96863 times.
✓ Branch 1 taken 44149 times.
✓ Branch 2 taken 94376 times.
✓ Branch 3 taken 2487 times.
141012 if (h->is_connected && h->prot->url_close)
446 94376 ret = h->prot->url_close(h);
447 #if CONFIG_NETWORK
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141012 times.
141012 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
449 ff_network_close();
450 #endif
451
1/2
✓ Branch 0 taken 141012 times.
✗ Branch 1 not taken.
141012 if (h->prot->priv_data_size) {
452
2/2
✓ Branch 0 taken 138691 times.
✓ Branch 1 taken 2321 times.
141012 if (h->prot->priv_data_class)
453 138691 av_opt_free(h->priv_data);
454 141012 av_freep(&h->priv_data);
455 }
456 141012 av_opt_free(h);
457 141012 av_freep(hh);
458 141012 return ret;
459 }
460
461 141010 int ffurl_close(URLContext *h)
462 {
463 141010 return ffurl_closep(&h);
464 }
465
466
467 7482 const char *avio_find_protocol_name(const char *url)
468 {
469 7482 const URLProtocol *p = url_find_protocol(url);
470
471
1/2
✓ Branch 0 taken 7482 times.
✗ Branch 1 not taken.
7482 return p ? p->name : NULL;
472 }
473
474 43992 int avio_check(const char *url, int flags)
475 {
476 URLContext *h;
477 43992 int ret = ffurl_alloc(&h, url, flags, NULL);
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43992 times.
43992 if (ret < 0)
479 return ret;
480
481
1/2
✓ Branch 0 taken 43992 times.
✗ Branch 1 not taken.
43992 if (h->prot->url_check) {
482 43992 ret = h->prot->url_check(h, flags);
483 } else {
484 ret = ffurl_connect(h, NULL);
485 if (ret >= 0)
486 ret = flags;
487 }
488
489 43992 ffurl_close(h);
490 43992 return ret;
491 }
492
493 78 int ffurl_move(const char *url_src, const char *url_dst)
494 {
495 URLContext *h_src, *h_dst;
496 78 int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
498 return ret;
499 78 ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0) {
501 ffurl_close(h_src);
502 return ret;
503 }
504
505
2/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
78 if (h_src->prot == h_dst->prot && h_src->prot->url_move)
506 78 ret = h_src->prot->url_move(h_src, h_dst);
507 else
508 ret = AVERROR(ENOSYS);
509
510 78 ffurl_close(h_src);
511 78 ffurl_close(h_dst);
512 78 return ret;
513 }
514
515 int ffurl_delete(const char *url)
516 {
517 URLContext *h;
518 int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
519 if (ret < 0)
520 return ret;
521
522 if (h->prot->url_delete)
523 ret = h->prot->url_delete(h);
524 else
525 ret = AVERROR(ENOSYS);
526
527 ffurl_close(h);
528 return ret;
529 }
530
531 #if !FF_API_AVIODIRCONTEXT
532 struct AVIODirContext {
533 struct URLContext *url_context;
534 };
535 #endif
536
537 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
538 {
539 URLContext *h = NULL;
540 AVIODirContext *ctx = NULL;
541 int ret;
542 av_assert0(s);
543
544 ctx = av_mallocz(sizeof(*ctx));
545 if (!ctx) {
546 ret = AVERROR(ENOMEM);
547 goto fail;
548 }
549
550 if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
551 goto fail;
552
553 if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
554 if (options && h->prot->priv_data_class &&
555 (ret = av_opt_set_dict(h->priv_data, options)) < 0)
556 goto fail;
557 ret = h->prot->url_open_dir(h);
558 } else
559 ret = AVERROR(ENOSYS);
560 if (ret < 0)
561 goto fail;
562
563 h->is_connected = 1;
564 ctx->url_context = h;
565 *s = ctx;
566 return 0;
567
568 fail:
569 av_free(ctx);
570 *s = NULL;
571 ffurl_close(h);
572 return ret;
573 }
574
575 int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
576 {
577 URLContext *h;
578 int ret;
579
580 if (!s || !s->url_context)
581 return AVERROR(EINVAL);
582 h = s->url_context;
583 if ((ret = h->prot->url_read_dir(h, next)) < 0)
584 avio_free_directory_entry(next);
585 return ret;
586 }
587
588 int avio_close_dir(AVIODirContext **s)
589 {
590 URLContext *h;
591
592 av_assert0(s);
593 if (!(*s) || !(*s)->url_context)
594 return AVERROR(EINVAL);
595 h = (*s)->url_context;
596 h->prot->url_close_dir(h);
597 ffurl_close(h);
598 av_freep(s);
599 *s = NULL;
600 return 0;
601 }
602
603 void avio_free_directory_entry(AVIODirEntry **entry)
604 {
605 if (!entry || !*entry)
606 return;
607 av_free((*entry)->name);
608 av_freep(entry);
609 }
610
611 int64_t ffurl_size(URLContext *h)
612 {
613 int64_t pos, size;
614
615 size = ffurl_seek(h, 0, AVSEEK_SIZE);
616 if (size < 0) {
617 pos = ffurl_seek(h, 0, SEEK_CUR);
618 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
619 return size;
620 size++;
621 ffurl_seek(h, pos, SEEK_SET);
622 }
623 return size;
624 }
625
626 int ffurl_get_file_handle(URLContext *h)
627 {
628 if (!h || !h->prot || !h->prot->url_get_file_handle)
629 return -1;
630 return h->prot->url_get_file_handle(h);
631 }
632
633 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
634 {
635 if (!h || !h->prot)
636 return AVERROR(ENOSYS);
637 if (!h->prot->url_get_multi_file_handle) {
638 if (!h->prot->url_get_file_handle)
639 return AVERROR(ENOSYS);
640 *handles = av_malloc(sizeof(**handles));
641 if (!*handles)
642 return AVERROR(ENOMEM);
643 *numhandles = 1;
644 *handles[0] = h->prot->url_get_file_handle(h);
645 return 0;
646 }
647 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
648 }
649
650 490892 int ffurl_get_short_seek(URLContext *h)
651 {
652
3/6
✓ Branch 0 taken 490892 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 490892 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 490892 times.
✗ Branch 5 not taken.
490892 if (!h || !h->prot || !h->prot->url_get_short_seek)
653 490892 return AVERROR(ENOSYS);
654 return h->prot->url_get_short_seek(h);
655 }
656
657 int ffurl_shutdown(URLContext *h, int flags)
658 {
659 if (!h || !h->prot || !h->prot->url_shutdown)
660 return AVERROR(ENOSYS);
661 return h->prot->url_shutdown(h, flags);
662 }
663
664 690053 int ff_check_interrupt(AVIOInterruptCB *cb)
665 {
666
3/4
✓ Branch 0 taken 690053 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 663113 times.
✓ Branch 3 taken 26940 times.
690053 if (cb && cb->callback)
667 663113 return cb->callback(cb->opaque);
668 26940 return 0;
669 }
670
671 78 int ff_rename(const char *url_src, const char *url_dst, void *logctx)
672 {
673 78 int ret = ffurl_move(url_src, url_dst);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
675 av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));
676 78 return ret;
677 }
678