aviobuf.c 25 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Buffered I/O for ffmpeg system
Fabrice Bellard's avatar
Fabrice Bellard committed
3
 * Copyright (c) 2000,2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22

#include "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
24
#include "avformat.h"
25
#include "avio.h"
26
#include "avio_internal.h"
27
#include "internal.h"
28
#include <stdarg.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
29 30 31

#define IO_BUFFER_SIZE 32768

32 33 34 35 36 37 38
/**
 * Do seeks within this distance ahead of the current buffer by skipping
 * data instead of calling the protocol seek function, for seekable
 * protocols.
 */
#define SHORT_SEEK_THRESHOLD 4096

39
static void fill_buffer(AVIOContext *s);
40
#if !FF_API_URL_RESETBUF
41
static int url_resetbuf(AVIOContext *s, int flags);
42
#endif
43

44
int ffio_init_context(AVIOContext *s,
Fabrice Bellard's avatar
Fabrice Bellard committed
45 46 47 48
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
49
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
Michael Niedermayer's avatar
Michael Niedermayer committed
50
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
51
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
Fabrice Bellard's avatar
Fabrice Bellard committed
52 53 54 55 56
{
    s->buffer = buffer;
    s->buffer_size = buffer_size;
    s->buf_ptr = buffer;
    s->opaque = opaque;
57
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
Fabrice Bellard's avatar
Fabrice Bellard committed
58 59 60 61 62 63
    s->write_packet = write_packet;
    s->read_packet = read_packet;
    s->seek = seek;
    s->pos = 0;
    s->must_flush = 0;
    s->eof_reached = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
64
    s->error = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
65
    s->is_streamed = 0;
66
    s->max_packet_size = 0;
67
    s->update_checksum= NULL;
68 69 70 71
    if(!read_packet && !write_flag){
        s->pos = buffer_size;
        s->buf_end = s->buffer + buffer_size;
    }
72 73
    s->read_pause = NULL;
    s->read_seek  = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
74 75
    return 0;
}
76

77 78 79 80 81 82 83 84 85 86 87 88 89
#if FF_API_OLD_AVIO
int init_put_byte(AVIOContext *s,
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
                                read_packet, write_packet, seek);
}
90 91 92 93 94 95 96 97 98 99 100 101
AVIOContext *av_alloc_put_byte(
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
                              read_packet, write_packet, seek);
}
102 103
#endif

104
AVIOContext *avio_alloc_context(
105 106 107 108 109 110
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
Diego Biurrun's avatar
Diego Biurrun committed
111 112
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
113
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
114
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
115 116 117 118
                  read_packet, write_packet, seek);
    return s;
}

119
static void flush_buffer(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
120 121
{
    if (s->buf_ptr > s->buffer) {
Michael Niedermayer's avatar
Michael Niedermayer committed
122
        if (s->write_packet && !s->error){
123
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
Michael Niedermayer's avatar
Michael Niedermayer committed
124 125 126 127
            if(ret < 0){
                s->error = ret;
            }
        }
128
        if(s->update_checksum){
129 130 131
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
            s->checksum_ptr= s->buffer;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
132 133 134 135 136
        s->pos += s->buf_ptr - s->buffer;
    }
    s->buf_ptr = s->buffer;
}

137
void put_byte(AVIOContext *s, int b)
Fabrice Bellard's avatar
Fabrice Bellard committed
138 139
{
    *(s->buf_ptr)++ = b;
140
    if (s->buf_ptr >= s->buf_end)
Fabrice Bellard's avatar
Fabrice Bellard committed
141 142 143
        flush_buffer(s);
}

144
void put_nbyte(AVIOContext *s, int b, int count)
145 146 147 148 149 150 151 152 153 154 155 156 157
{
    while (count > 0) {
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
        memset(s->buf_ptr, b, len);
        s->buf_ptr += len;

        if (s->buf_ptr >= s->buf_end)
            flush_buffer(s);

        count -= len;
    }
}

158
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
159 160
{
    while (size > 0) {
161
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
Fabrice Bellard's avatar
Fabrice Bellard committed
162 163 164
        memcpy(s->buf_ptr, buf, len);
        s->buf_ptr += len;

165
        if (s->buf_ptr >= s->buf_end)
Fabrice Bellard's avatar
Fabrice Bellard committed
166 167 168 169 170 171 172
            flush_buffer(s);

        buf += len;
        size -= len;
    }
}

173
void put_flush_packet(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
174 175 176 177 178
{
    flush_buffer(s);
    s->must_flush = 0;
}

179
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
180
{
181 182
    int64_t offset1;
    int64_t pos;
183 184
    int force = whence & AVSEEK_FORCE;
    whence &= ~AVSEEK_FORCE;
185 186 187 188 189

    if(!s)
        return AVERROR(EINVAL);

    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
Fabrice Bellard's avatar
Fabrice Bellard committed
190 191

    if (whence != SEEK_CUR && whence != SEEK_SET)
192
        return AVERROR(EINVAL);
193

Michael Niedermayer's avatar
Michael Niedermayer committed
194 195 196 197 198 199 200 201
    if (whence == SEEK_CUR) {
        offset1 = pos + (s->buf_ptr - s->buffer);
        if (offset == 0)
            return offset1;
        offset += offset1;
    }
    offset1 = offset - pos;
    if (!s->must_flush &&
202
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
Michael Niedermayer's avatar
Michael Niedermayer committed
203 204
        /* can do the seek inside the buffer */
        s->buf_ptr = s->buffer + offset1;
205 206 207
    } else if ((s->is_streamed ||
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
               !s->write_flag && offset1 >= 0 &&
208
              (whence != SEEK_END || force)) {
209 210
        while(s->pos < offset && !s->eof_reached)
            fill_buffer(s);
211
        if (s->eof_reached)
212
            return AVERROR_EOF;
213
        s->buf_ptr = s->buf_end + offset - s->pos;
Michael Niedermayer's avatar
Michael Niedermayer committed
214
    } else {
215
        int64_t res;
216

217
#if CONFIG_MUXERS || CONFIG_NETWORK
Michael Niedermayer's avatar
Michael Niedermayer committed
218
        if (s->write_flag) {
Fabrice Bellard's avatar
Fabrice Bellard committed
219 220 221
            flush_buffer(s);
            s->must_flush = 1;
        }
222
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
223 224 225
        if (!s->seek)
            return AVERROR(EPIPE);
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
226
            return res;
227 228 229
        if (!s->write_flag)
            s->buf_end = s->buffer;
        s->buf_ptr = s->buffer;
Michael Niedermayer's avatar
Michael Niedermayer committed
230
        s->pos = offset;
Fabrice Bellard's avatar
Fabrice Bellard committed
231
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
232
    s->eof_reached = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
233 234 235
    return offset;
}

236
int url_fskip(AVIOContext *s, int64_t offset)
Fabrice Bellard's avatar
Fabrice Bellard committed
237
{
238 239
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
    return ret < 0 ? ret : 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
240 241
}

242
int64_t url_ftell(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
243 244 245 246
{
    return url_fseek(s, 0, SEEK_CUR);
}

247
int64_t url_fsize(AVIOContext *s)
248
{
249
    int64_t size;
250

251 252 253
    if(!s)
        return AVERROR(EINVAL);

254
    if (!s->seek)
255
        return AVERROR(ENOSYS);
256 257
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
    if(size<0){
258 259 260
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
            return size;
        size++;
261
        s->seek(s->opaque, s->pos, SEEK_SET);
262
    }
263 264 265
    return size;
}

266
int url_feof(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
267
{
268 269
    if(!s)
        return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
270 271 272
    return s->eof_reached;
}

273
int url_ferror(AVIOContext *s)
Michael Niedermayer's avatar
Michael Niedermayer committed
274
{
275 276
    if(!s)
        return 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
277 278 279
    return s->error;
}

280
void put_le32(AVIOContext *s, unsigned int val)
Fabrice Bellard's avatar
Fabrice Bellard committed
281 282 283 284 285 286 287
{
    put_byte(s, val);
    put_byte(s, val >> 8);
    put_byte(s, val >> 16);
    put_byte(s, val >> 24);
}

288
void put_be32(AVIOContext *s, unsigned int val)
Fabrice Bellard's avatar
Fabrice Bellard committed
289 290 291 292 293 294 295
{
    put_byte(s, val >> 24);
    put_byte(s, val >> 16);
    put_byte(s, val >> 8);
    put_byte(s, val);
}

296
#if FF_API_OLD_AVIO
297
void put_strz(AVIOContext *s, const char *str)
298
{
299 300 301 302
    avio_put_str(s, str);
}
#endif

303
int avio_put_str(AVIOContext *s, const char *str)
304 305 306 307 308 309
{
    int len = 1;
    if (str) {
        len += strlen(str);
        put_buffer(s, (const unsigned char *) str, len);
    } else
310
        put_byte(s, 0);
311
    return len;
312 313
}

314
int avio_put_str16le(AVIOContext *s, const char *str)
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
{
    const uint8_t *q = str;
    int ret = 0;

    while (*q) {
        uint32_t ch;
        uint16_t tmp;

        GET_UTF8(ch, *q++, break;)
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
    }
    put_le16(s, 0);
    ret += 2;
    return ret;
}

331 332 333 334 335 336 337 338 339
int ff_get_v_length(uint64_t val){
    int i=1;

    while(val>>=7)
        i++;

    return i;
}

340
void ff_put_v(AVIOContext *bc, uint64_t val){
341 342 343 344 345 346 347 348
    int i= ff_get_v_length(val);

    while(--i>0)
        put_byte(bc, 128 | (val>>(7*i)));

    put_byte(bc, val&127);
}

349
void put_le64(AVIOContext *s, uint64_t val)
Fabrice Bellard's avatar
Fabrice Bellard committed
350
{
351 352
    put_le32(s, (uint32_t)(val & 0xffffffff));
    put_le32(s, (uint32_t)(val >> 32));
Fabrice Bellard's avatar
Fabrice Bellard committed
353 354
}

355
void put_be64(AVIOContext *s, uint64_t val)
Fabrice Bellard's avatar
Fabrice Bellard committed
356
{
357 358
    put_be32(s, (uint32_t)(val >> 32));
    put_be32(s, (uint32_t)(val & 0xffffffff));
Fabrice Bellard's avatar
Fabrice Bellard committed
359 360
}

361
void put_le16(AVIOContext *s, unsigned int val)
Fabrice Bellard's avatar
Fabrice Bellard committed
362 363 364 365 366
{
    put_byte(s, val);
    put_byte(s, val >> 8);
}

367
void put_be16(AVIOContext *s, unsigned int val)
Fabrice Bellard's avatar
Fabrice Bellard committed
368 369 370 371 372
{
    put_byte(s, val >> 8);
    put_byte(s, val);
}

373
void put_le24(AVIOContext *s, unsigned int val)
374 375 376 377 378
{
    put_le16(s, val & 0xffff);
    put_byte(s, val >> 16);
}

379
void put_be24(AVIOContext *s, unsigned int val)
380 381 382 383 384
{
    put_be16(s, val >> 8);
    put_byte(s, val);
}

385
void put_tag(AVIOContext *s, const char *tag)
Fabrice Bellard's avatar
Fabrice Bellard committed
386 387 388 389 390 391 392 393
{
    while (*tag) {
        put_byte(s, *tag++);
    }
}

/* Input stream */

394
static void fill_buffer(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
395
{
396
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
397
    int len= s->buffer_size - (dst - s->buffer);
398
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
399

400 401 402
    /* no need to do anything if EOF already reached */
    if (s->eof_reached)
        return;
403

404
    if(s->update_checksum && dst == s->buffer){
Michael Niedermayer's avatar
Michael Niedermayer committed
405 406
        if(s->buf_end > s->checksum_ptr)
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
407 408 409
        s->checksum_ptr= s->buffer;
    }

410 411 412 413 414 415 416 417
    /* make buffer smaller in case it ended up large after probing */
    if (s->buffer_size > max_buffer_size) {
        url_setbufsize(s, max_buffer_size);

        s->checksum_ptr = dst = s->buffer;
        len = s->buffer_size;
    }

418
    if(s->read_packet)
419
        len = s->read_packet(s->opaque, dst, len);
420 421
    else
        len = 0;
422 423 424
    if (len <= 0) {
        /* do not modify buffer if EOF reached so that a seek back can
           be done without rereading data */
Fabrice Bellard's avatar
Fabrice Bellard committed
425
        s->eof_reached = 1;
426 427
        if(len<0)
            s->error= len;
428 429
    } else {
        s->pos += len;
430 431
        s->buf_ptr = dst;
        s->buf_end = dst + len;
Fabrice Bellard's avatar
Fabrice Bellard committed
432 433 434
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
435 436 437
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
                                    unsigned int len)
{
Aurelien Jacobs's avatar
Aurelien Jacobs committed
438
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
439 440
}

441
unsigned long get_checksum(AVIOContext *s)
Diego Biurrun's avatar
Diego Biurrun committed
442
{
443
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
444
    s->update_checksum= NULL;
445 446 447
    return s->checksum;
}

448
void init_checksum(AVIOContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
449 450 451
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
                   unsigned long checksum)
{
452
    s->update_checksum= update_checksum;
453
    if(s->update_checksum){
454
        s->checksum= checksum;
455 456
        s->checksum_ptr= s->buf_ptr;
    }
457 458
}

459
/* XXX: put an inline version */
460
int get_byte(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
461
{
462
    if (s->buf_ptr >= s->buf_end)
Fabrice Bellard's avatar
Fabrice Bellard committed
463
        fill_buffer(s);
464 465 466
    if (s->buf_ptr < s->buf_end)
        return *s->buf_ptr++;
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
467 468
}

469
int url_fgetc(AVIOContext *s)
470
{
471
    if (s->buf_ptr >= s->buf_end)
472
        fill_buffer(s);
473 474 475
    if (s->buf_ptr < s->buf_end)
        return *s->buf_ptr++;
    return URL_EOF;
476 477
}

478
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
479 480 481 482 483 484 485 486 487
{
    int len, size1;

    size1 = size;
    while (size > 0) {
        len = s->buf_end - s->buf_ptr;
        if (len > size)
            len = size;
        if (len == 0) {
488
            if(size > s->buffer_size && !s->update_checksum){
489
                if(s->read_packet)
490
                    len = s->read_packet(s->opaque, buf, size);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                if (len <= 0) {
                    /* do not modify buffer if EOF reached so that a seek back can
                    be done without rereading data */
                    s->eof_reached = 1;
                    if(len<0)
                        s->error= len;
                    break;
                } else {
                    s->pos += len;
                    size -= len;
                    buf += len;
                    s->buf_ptr = s->buffer;
                    s->buf_end = s->buffer/* + len*/;
                }
            }else{
                fill_buffer(s);
                len = s->buf_end - s->buf_ptr;
                if (len == 0)
                    break;
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
511 512 513 514 515 516 517
        } else {
            memcpy(buf, s->buf_ptr, len);
            buf += len;
            s->buf_ptr += len;
            size -= len;
        }
    }
518 519 520 521
    if (size1 == size) {
        if (url_ferror(s)) return url_ferror(s);
        if (url_feof(s))   return AVERROR_EOF;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
522 523 524
    return size1 - size;
}

525
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
526 527
{
    int len;
528

529 530
    if(size<0)
        return -1;
531 532 533 534 535 536 537 538 539 540

    len = s->buf_end - s->buf_ptr;
    if (len == 0) {
        fill_buffer(s);
        len = s->buf_end - s->buf_ptr;
    }
    if (len > size)
        len = size;
    memcpy(buf, s->buf_ptr, len);
    s->buf_ptr += len;
541 542 543 544
    if (!len) {
        if (url_ferror(s)) return url_ferror(s);
        if (url_feof(s))   return AVERROR_EOF;
    }
545 546 547
    return len;
}

548
unsigned int get_le16(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
549 550 551 552 553 554 555
{
    unsigned int val;
    val = get_byte(s);
    val |= get_byte(s) << 8;
    return val;
}

556
unsigned int get_le24(AVIOContext *s)
557 558 559 560 561 562 563
{
    unsigned int val;
    val = get_le16(s);
    val |= get_byte(s) << 16;
    return val;
}

564
unsigned int get_le32(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
565 566
{
    unsigned int val;
567 568
    val = get_le16(s);
    val |= get_le16(s) << 16;
Fabrice Bellard's avatar
Fabrice Bellard committed
569 570 571
    return val;
}

572
uint64_t get_le64(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
573
{
574 575 576
    uint64_t val;
    val = (uint64_t)get_le32(s);
    val |= (uint64_t)get_le32(s) << 32;
Fabrice Bellard's avatar
Fabrice Bellard committed
577 578 579
    return val;
}

580
unsigned int get_be16(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
581 582 583 584 585 586 587
{
    unsigned int val;
    val = get_byte(s) << 8;
    val |= get_byte(s);
    return val;
}

588
unsigned int get_be24(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
589 590
{
    unsigned int val;
591
    val = get_be16(s) << 8;
Fabrice Bellard's avatar
Fabrice Bellard committed
592 593 594
    val |= get_byte(s);
    return val;
}
595
unsigned int get_be32(AVIOContext *s)
596 597 598 599 600 601
{
    unsigned int val;
    val = get_be16(s) << 16;
    val |= get_be16(s);
    return val;
}
Fabrice Bellard's avatar
Fabrice Bellard committed
602

603
char *get_strz(AVIOContext *s, char *buf, int maxlen)
604 605 606 607 608 609 610 611
{
    int i = 0;
    char c;

    while ((c = get_byte(s))) {
        if (i < maxlen-1)
            buf[i++] = c;
    }
612

613 614 615 616 617
    buf[i] = 0; /* Ensure null terminated, but may be truncated */

    return buf;
}

618
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
619 620 621 622 623 624
{
    int i = 0;
    char c;

    do {
        c = get_byte(s);
625
        if (c && i < maxlen-1)
626 627 628 629
            buf[i++] = c;
    } while (c != '\n' && c);

    buf[i] = 0;
630
    return i;
631 632
}

633
#define GET_STR16(type, read) \
634
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
{\
    char* q = buf;\
    int ret = 0;\
    while (ret + 1 < maxlen) {\
        uint8_t tmp;\
        uint32_t ch;\
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
        if (!ch)\
            break;\
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
    }\
    *q = 0;\
    return ret;\
}\

GET_STR16(le, get_le16)
GET_STR16(be, get_be16)

#undef GET_STR16

655
uint64_t get_be64(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
656
{
657 658 659
    uint64_t val;
    val = (uint64_t)get_be32(s) << 32;
    val |= (uint64_t)get_be32(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
660 661 662
    return val;
}

663
uint64_t ff_get_v(AVIOContext *bc){
664 665 666 667 668 669 670 671 672 673
    uint64_t val = 0;
    int tmp;

    do{
        tmp = get_byte(bc);
        val= (val<<7) + (tmp&127);
    }while(tmp&128);
    return val;
}

674
int url_fdopen(AVIOContext **s, URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
675
{
676
    uint8_t *buffer;
677
    int buffer_size, max_packet_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
678

679 680 681 682 683 684
    max_packet_size = url_get_max_packet_size(h);
    if (max_packet_size) {
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
    } else {
        buffer_size = IO_BUFFER_SIZE;
    }
685
    buffer = av_malloc(buffer_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
686
    if (!buffer)
687
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
688

689
    *s = av_mallocz(sizeof(AVIOContext));
690 691 692 693 694
    if(!*s) {
        av_free(buffer);
        return AVERROR(ENOMEM);
    }

695
    if (ffio_init_context(*s, buffer, buffer_size,
696
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
697
                      url_read, url_write, url_seek) < 0) {
698
        av_free(buffer);
699
        av_freep(s);
700
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
701
    }
702 703
    (*s)->is_streamed = h->is_streamed;
    (*s)->max_packet_size = max_packet_size;
704
    if(h->prot) {
705
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
706
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
707
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
708 709 710
    return 0;
}

711
int url_setbufsize(AVIOContext *s, int buf_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
712
{
713
    uint8_t *buffer;
714
    buffer = av_malloc(buf_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
715
    if (!buffer)
716
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
717

718
    av_free(s->buffer);
Fabrice Bellard's avatar
Fabrice Bellard committed
719 720 721
    s->buffer = buffer;
    s->buffer_size = buf_size;
    s->buf_ptr = buffer;
722 723 724 725
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
    return 0;
}

726
#if FF_API_URL_RESETBUF
727
int url_resetbuf(AVIOContext *s, int flags)
728
#else
729
static int url_resetbuf(AVIOContext *s, int flags)
730
#endif
731
{
732
#if FF_API_URL_RESETBUF
733
    if (flags & URL_RDWR)
734
        return AVERROR(EINVAL);
735 736 737
#else
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
#endif
738 739 740 741 742 743 744 745

    if (flags & URL_WRONLY) {
        s->buf_end = s->buffer + s->buffer_size;
        s->write_flag = 1;
    } else {
        s->buf_end = s->buffer;
        s->write_flag = 0;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
746 747 748
    return 0;
}

749
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
750 751 752
{
    int64_t buffer_start;
    int buffer_size;
753
    int overlap, new_size, alloc_size;
754 755 756 757 758 759 760 761 762 763 764 765 766

    if (s->write_flag)
        return AVERROR(EINVAL);

    buffer_size = s->buf_end - s->buffer;

    /* the buffers must touch or overlap */
    if ((buffer_start = s->pos - buffer_size) > buf_size)
        return AVERROR(EINVAL);

    overlap = buf_size - buffer_start;
    new_size = buf_size + buffer_size - overlap;

767 768 769
    alloc_size = FFMAX(s->buffer_size, new_size);
    if (alloc_size > buf_size)
        if (!(buf = av_realloc(buf, alloc_size)))
770 771
            return AVERROR(ENOMEM);

772
    if (new_size > buf_size) {
773 774 775 776 777 778
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
        buf_size = new_size;
    }

    av_free(s->buffer);
    s->buf_ptr = s->buffer = buf;
779 780
    s->buffer_size = alloc_size;
    s->pos = buf_size;
781 782 783 784 785 786 787
    s->buf_end = s->buf_ptr + buf_size;
    s->eof_reached = 0;
    s->must_flush = 0;

    return 0;
}

788
int url_fopen(AVIOContext **s, const char *filename, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
{
    URLContext *h;
    int err;

    err = url_open(&h, filename, flags);
    if (err < 0)
        return err;
    err = url_fdopen(s, h);
    if (err < 0) {
        url_close(h);
        return err;
    }
    return 0;
}

804
int url_fclose(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
805 806
{
    URLContext *h = s->opaque;
807

808
    av_free(s->buffer);
809
    av_free(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
810 811 812
    return url_close(h);
}

813
URLContext *url_fileno(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
814 815 816 817
{
    return s->opaque;
}

818
#if CONFIG_MUXERS
819
int url_fprintf(AVIOContext *s, const char *fmt, ...)
820 821 822 823 824 825 826 827 828 829 830
{
    va_list ap;
    char buf[4096];
    int ret;

    va_start(ap, fmt);
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);
    put_buffer(s, buf, strlen(buf));
    return ret;
}
831
#endif //CONFIG_MUXERS
832

833
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
{
    int c;
    char *q;

    c = url_fgetc(s);
    if (c == EOF)
        return NULL;
    q = buf;
    for(;;) {
        if (c == EOF || c == '\n')
            break;
        if ((q - buf) < buf_size - 1)
            *q++ = c;
        c = url_fgetc(s);
    }
    if (buf_size > 0)
        *q = '\0';
    return buf;
}

854
int url_fget_max_packet_size(AVIOContext *s)
855 856 857 858
{
    return s->max_packet_size;
}

859
int av_url_read_fpause(AVIOContext *s, int pause)
860 861 862
{
    if (!s->read_pause)
        return AVERROR(ENOSYS);
863
    return s->read_pause(s->opaque, pause);
864 865
}

866
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
Diego Biurrun's avatar
Diego Biurrun committed
867
                          int64_t timestamp, int flags)
868 869
{
    URLContext *h = s->opaque;
870
    int64_t ret;
871 872 873 874
    if (!s->read_seek)
        return AVERROR(ENOSYS);
    ret = s->read_seek(h, stream_index, timestamp, flags);
    if(ret >= 0) {
875
        int64_t pos;
876
        s->buf_ptr = s->buf_end; // Flush buffer
877 878 879 880 881
        pos = s->seek(h, 0, SEEK_CUR);
        if (pos >= 0)
            s->pos = pos;
        else if (pos != AVERROR(ENOSYS))
            ret = pos;
882 883 884 885
    }
    return ret;
}

886
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
887 888
 * back to the server even if CONFIG_MUXERS is false. */
#if CONFIG_MUXERS || CONFIG_NETWORK
Fabrice Bellard's avatar
Fabrice Bellard committed
889
/* buffer handling */
890
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
891
{
892
    int ret;
893
    *s = av_mallocz(sizeof(AVIOContext));
894 895
    if(!*s)
        return AVERROR(ENOMEM);
896
    ret = ffio_init_context(*s, buf, buf_size,
897 898 899 900 901
                        (flags & URL_WRONLY || flags & URL_RDWR),
                        NULL, NULL, NULL, NULL);
    if(ret != 0)
        av_freep(s);
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
902 903
}

904
int url_close_buf(AVIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
905
{
906
    put_flush_packet(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
907 908
    return s->buf_ptr - s->buffer;
}
909 910 911 912 913

/* output in a dynamic buffer */

typedef struct DynBuffer {
    int pos, size, allocated_size;
914
    uint8_t *buffer;
915
    int io_buffer_size;
916
    uint8_t io_buffer[1];
917 918
} DynBuffer;

Michael Niedermayer's avatar
Michael Niedermayer committed
919
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
920 921
{
    DynBuffer *d = opaque;
922
    unsigned new_size, new_allocated_size;
923

924 925 926
    /* reallocate buffer if needed */
    new_size = d->pos + buf_size;
    new_allocated_size = d->allocated_size;
927 928
    if(new_size < d->pos || new_size > INT_MAX/2)
        return -1;
929 930 931 932
    while (new_size > new_allocated_size) {
        if (!new_allocated_size)
            new_allocated_size = new_size;
        else
933
            new_allocated_size += new_allocated_size / 2 + 1;
934
    }
935

936
    if (new_allocated_size > d->allocated_size) {
937 938
        d->buffer = av_realloc(d->buffer, new_allocated_size);
        if(d->buffer == NULL)
939
             return AVERROR(ENOMEM);
940 941 942 943 944 945
        d->allocated_size = new_allocated_size;
    }
    memcpy(d->buffer + d->pos, buf, buf_size);
    d->pos = new_size;
    if (d->pos > d->size)
        d->size = d->pos;
Michael Niedermayer's avatar
Michael Niedermayer committed
946
    return buf_size;
947 948
}

Michael Niedermayer's avatar
Michael Niedermayer committed
949
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
950 951
{
    unsigned char buf1[4];
Michael Niedermayer's avatar
Michael Niedermayer committed
952
    int ret;
953 954

    /* packetized write: output the header */
955
    AV_WB32(buf1, buf_size);
Michael Niedermayer's avatar
Michael Niedermayer committed
956 957 958
    ret= dyn_buf_write(opaque, buf1, 4);
    if(ret < 0)
        return ret;
959 960

    /* then the data */
Michael Niedermayer's avatar
Michael Niedermayer committed
961
    return dyn_buf_write(opaque, buf, buf_size);
962 963
}

964
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
965 966 967 968 969 970 971 972 973 974 975 976 977
{
    DynBuffer *d = opaque;

    if (whence == SEEK_CUR)
        offset += d->pos;
    else if (whence == SEEK_END)
        offset += d->size;
    if (offset < 0 || offset > 0x7fffffffLL)
        return -1;
    d->pos = offset;
    return 0;
}

978
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
979 980
{
    DynBuffer *d;
981
    int ret;
982
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
983

984 985
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
        return -1;
986
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
987
    if (!d)
988
        return AVERROR(ENOMEM);
989
    *s = av_mallocz(sizeof(AVIOContext));
990 991 992 993
    if(!*s) {
        av_free(d);
        return AVERROR(ENOMEM);
    }
994
    d->io_buffer_size = io_buffer_size;
995
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
996 997
                        1, d, NULL,
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
998 999
                        max_packet_size ? NULL : dyn_buf_seek);
    if (ret == 0) {
1000 1001 1002 1003
        (*s)->max_packet_size = max_packet_size;
    } else {
        av_free(d);
        av_freep(s);
1004 1005 1006 1007
    }
    return ret;
}

1008
int url_open_dyn_buf(AVIOContext **s)
1009 1010 1011 1012
{
    return url_open_dyn_buf_internal(s, 0);
}

1013
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1014 1015 1016 1017 1018 1019
{
    if (max_packet_size <= 0)
        return -1;
    return url_open_dyn_buf_internal(s, max_packet_size);
}

1020
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1021 1022 1023
{
    DynBuffer *d = s->opaque;
    int size;
1024 1025 1026 1027 1028 1029 1030 1031
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
    int padding = 0;

    /* don't attempt to pad fixed-size packet buffers */
    if (!s->max_packet_size) {
        put_buffer(s, padbuf, sizeof(padbuf));
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
    }
1032 1033 1034 1035 1036 1037

    put_flush_packet(s);

    *pbuffer = d->buffer;
    size = d->size;
    av_free(d);
1038
    av_free(s);
1039
    return size - padding;
1040
}
1041
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */