Coverage Report

Created: 2026-05-31 06:50

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