mp3dec: decode more data from Info header
[libav.git] / libavformat / aviobuf.c
CommitLineData
de6d9b64 1/*
124e2884 2 * buffered I/O
19720f15 3 * Copyright (c) 2000,2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da
DB
21
22#include "libavutil/crc.h"
1dee0aca 23#include "libavutil/dict.h"
aa805f94 24#include "libavutil/intreadwrite.h"
1dee0aca
MS
25#include "libavutil/log.h"
26#include "libavutil/opt.h"
de6d9b64 27#include "avformat.h"
5c91a675 28#include "avio.h"
e731b8d8 29#include "avio_internal.h"
603e5c0b 30#include "internal.h"
0589da0a 31#include "url.h"
abba3dd1 32#include <stdarg.h>
de6d9b64
FB
33
34#define IO_BUFFER_SIZE 32768
35
7939d0ed
MS
36/**
37 * Do seeks within this distance ahead of the current buffer by skipping
38 * data instead of calling the protocol seek function, for seekable
39 * protocols.
40 */
41#define SHORT_SEEK_THRESHOLD 4096
42
1dee0aca
MS
43static void *ffio_url_child_next(void *obj, void *prev)
44{
45 AVIOContext *s = obj;
46 return prev ? NULL : s->opaque;
47}
48
49static const AVClass *ffio_url_child_class_next(const AVClass *prev)
50{
51 return prev ? NULL : &ffurl_context_class;
52}
53
54static const AVOption ffio_url_options[] = {
55 { NULL },
56};
57
58const AVClass ffio_url_class = {
59 .class_name = "AVIOContext",
60 .item_name = av_default_item_name,
61 .version = LIBAVUTIL_VERSION_INT,
62 .option = ffio_url_options,
63 .child_next = ffio_url_child_next,
64 .child_class_next = ffio_url_child_class_next,
65};
2439f2ca 66
ae628ec1 67static void fill_buffer(AVIOContext *s);
ae628ec1 68static int url_resetbuf(AVIOContext *s, int flags);
398f5d3f 69
e731b8d8 70int ffio_init_context(AVIOContext *s,
de6d9b64
FB
71 unsigned char *buffer,
72 int buffer_size,
73 int write_flag,
74 void *opaque,
0c1a9eda 75 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 76 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
bc5c918e 77 int64_t (*seek)(void *opaque, int64_t offset, int whence))
de6d9b64 78{
1dd1c1c8 79 s->buffer = buffer;
de6d9b64 80 s->buffer_size = buffer_size;
1dd1c1c8
LB
81 s->buf_ptr = buffer;
82 s->opaque = opaque;
83
59d96941 84 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1dd1c1c8
LB
85
86 s->write_packet = write_packet;
87 s->read_packet = read_packet;
88 s->seek = seek;
89 s->pos = 0;
90 s->must_flush = 0;
91 s->eof_reached = 0;
92 s->error = 0;
b5123601 93 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
abba3dd1 94 s->max_packet_size = 0;
1dd1c1c8
LB
95 s->update_checksum = NULL;
96
97 if (!read_packet && !write_flag) {
98 s->pos = buffer_size;
942f3bb5
MN
99 s->buf_end = s->buffer + buffer_size;
100 }
e7e4810a
BA
101 s->read_pause = NULL;
102 s->read_seek = NULL;
1dd1c1c8 103
de6d9b64
FB
104 return 0;
105}
115329f1 106
8d9ac969 107AVIOContext *avio_alloc_context(
1e0f3468
RD
108 unsigned char *buffer,
109 int buffer_size,
110 int write_flag,
111 void *opaque,
112 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
113 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
7f37f568
DB
114 int64_t (*seek)(void *opaque, int64_t offset, int whence))
115{
ae628ec1 116 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
9e2dabed
CW
117 if (!s)
118 return NULL;
e731b8d8 119 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
1e0f3468
RD
120 read_packet, write_packet, seek);
121 return s;
122}
123
ae628ec1 124static void flush_buffer(AVIOContext *s)
de6d9b64
FB
125{
126 if (s->buf_ptr > s->buffer) {
1dd1c1c8
LB
127 if (s->write_packet && !s->error) {
128 int ret = s->write_packet(s->opaque, s->buffer,
129 s->buf_ptr - s->buffer);
130 if (ret < 0) {
576ae256
MN
131 s->error = ret;
132 }
133 }
1dd1c1c8
LB
134 if (s->update_checksum) {
135 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
136 s->buf_ptr - s->checksum_ptr);
137 s->checksum_ptr = s->buffer;
ee9f36a8 138 }
de6d9b64
FB
139 s->pos += s->buf_ptr - s->buffer;
140 }
141 s->buf_ptr = s->buffer;
142}
143
77eb5504 144void avio_w8(AVIOContext *s, int b)
de6d9b64 145{
3dc99a18 146 *s->buf_ptr++ = b;
115329f1 147 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
148 flush_buffer(s);
149}
150
0ac8e2bf 151void ffio_fill(AVIOContext *s, int b, int count)
17ee8f66
AH
152{
153 while (count > 0) {
154 int len = FFMIN(s->buf_end - s->buf_ptr, count);
155 memset(s->buf_ptr, b, len);
156 s->buf_ptr += len;
157
158 if (s->buf_ptr >= s->buf_end)
159 flush_buffer(s);
160
161 count -= len;
162 }
163}
164
77eb5504 165void avio_write(AVIOContext *s, const unsigned char *buf, int size)
de6d9b64 166{
de6d9b64 167 while (size > 0) {
90aa6ace 168 int len = FFMIN(s->buf_end - s->buf_ptr, size);
de6d9b64
FB
169 memcpy(s->buf_ptr, buf, len);
170 s->buf_ptr += len;
171
115329f1 172 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
173 flush_buffer(s);
174
175 buf += len;
176 size -= len;
177 }
178}
179
b7f2fdde 180void avio_flush(AVIOContext *s)
de6d9b64
FB
181{
182 flush_buffer(s);
183 s->must_flush = 0;
184}
185
6b4aa5da 186int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
de6d9b64 187{
bc5c918e
DB
188 int64_t offset1;
189 int64_t pos;
c6a5e087
TH
190 int force = whence & AVSEEK_FORCE;
191 whence &= ~AVSEEK_FORCE;
899681cd
BA
192
193 if(!s)
194 return AVERROR(EINVAL);
195
196 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
de6d9b64
FB
197
198 if (whence != SEEK_CUR && whence != SEEK_SET)
8fa36ae0 199 return AVERROR(EINVAL);
115329f1 200
f40d1739
MN
201 if (whence == SEEK_CUR) {
202 offset1 = pos + (s->buf_ptr - s->buffer);
203 if (offset == 0)
204 return offset1;
205 offset += offset1;
206 }
207 offset1 = offset - pos;
208 if (!s->must_flush &&
b3eab9fb 209 offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
f40d1739
MN
210 /* can do the seek inside the buffer */
211 s->buf_ptr = s->buffer + offset1;
8978feda 212 } else if ((!s->seekable ||
7939d0ed
MS
213 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
214 !s->write_flag && offset1 >= 0 &&
7a6fe01f 215 (whence != SEEK_END || force)) {
398f5d3f
MN
216 while(s->pos < offset && !s->eof_reached)
217 fill_buffer(s);
5cb4b82a 218 if (s->eof_reached)
8d5e638f 219 return AVERROR_EOF;
398f5d3f 220 s->buf_ptr = s->buf_end + offset - s->pos;
f40d1739 221 } else {
9d30e068 222 int64_t res;
68fcdbf1 223
f40d1739 224 if (s->write_flag) {
de6d9b64
FB
225 flush_buffer(s);
226 s->must_flush = 1;
de6d9b64 227 }
9d30e068
SS
228 if (!s->seek)
229 return AVERROR(EPIPE);
230 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
68fcdbf1 231 return res;
39a127bf
BC
232 if (!s->write_flag)
233 s->buf_end = s->buffer;
234 s->buf_ptr = s->buffer;
f40d1739 235 s->pos = offset;
de6d9b64 236 }
f40d1739 237 s->eof_reached = 0;
de6d9b64
FB
238 return offset;
239}
240
76aa876e 241int64_t avio_size(AVIOContext *s)
a965c478 242{
bc5c918e 243 int64_t size;
115329f1 244
1dd1c1c8 245 if (!s)
899681cd
BA
246 return AVERROR(EINVAL);
247
a965c478 248 if (!s->seek)
4c4ef3db 249 return AVERROR(ENOSYS);
8e287af0 250 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
1dd1c1c8 251 if (size < 0) {
8f42f523
RB
252 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
253 return size;
254 size++;
1ae2c5f2 255 s->seek(s->opaque, s->pos, SEEK_SET);
8e287af0 256 }
a965c478
AJ
257 return size;
258}
259
77eb5504 260void avio_wl32(AVIOContext *s, unsigned int val)
de6d9b64 261{
77eb5504
AK
262 avio_w8(s, val);
263 avio_w8(s, val >> 8);
264 avio_w8(s, val >> 16);
265 avio_w8(s, val >> 24);
de6d9b64
FB
266}
267
77eb5504 268void avio_wb32(AVIOContext *s, unsigned int val)
de6d9b64 269{
77eb5504
AK
270 avio_w8(s, val >> 24);
271 avio_w8(s, val >> 16);
272 avio_w8(s, val >> 8);
273 avio_w8(s, val);
de6d9b64
FB
274}
275
ae628ec1 276int avio_put_str(AVIOContext *s, const char *str)
4efd5cf3
AK
277{
278 int len = 1;
279 if (str) {
280 len += strlen(str);
77eb5504 281 avio_write(s, (const unsigned char *) str, len);
4efd5cf3 282 } else
77eb5504 283 avio_w8(s, 0);
4efd5cf3 284 return len;
75bdb984
PG
285}
286
ae628ec1 287int avio_put_str16le(AVIOContext *s, const char *str)
dccbd97d
AK
288{
289 const uint8_t *q = str;
290 int ret = 0;
291
292 while (*q) {
293 uint32_t ch;
294 uint16_t tmp;
295
296 GET_UTF8(ch, *q++, break;)
1dd1c1c8 297 PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
dccbd97d 298 }
77eb5504 299 avio_wl16(s, 0);
dccbd97d
AK
300 ret += 2;
301 return ret;
302}
303
1dd1c1c8
LB
304int ff_get_v_length(uint64_t val)
305{
306 int i = 1;
603e5c0b 307
1dd1c1c8 308 while (val >>= 7)
603e5c0b
MC
309 i++;
310
311 return i;
312}
313
1dd1c1c8
LB
314void ff_put_v(AVIOContext *bc, uint64_t val)
315{
316 int i = ff_get_v_length(val);
603e5c0b 317
1dd1c1c8
LB
318 while (--i > 0)
319 avio_w8(bc, 128 | (val >> (7 * i)));
603e5c0b 320
1dd1c1c8 321 avio_w8(bc, val & 127);
603e5c0b
MC
322}
323
77eb5504 324void avio_wl64(AVIOContext *s, uint64_t val)
de6d9b64 325{
77eb5504
AK
326 avio_wl32(s, (uint32_t)(val & 0xffffffff));
327 avio_wl32(s, (uint32_t)(val >> 32));
de6d9b64
FB
328}
329
77eb5504 330void avio_wb64(AVIOContext *s, uint64_t val)
de6d9b64 331{
77eb5504
AK
332 avio_wb32(s, (uint32_t)(val >> 32));
333 avio_wb32(s, (uint32_t)(val & 0xffffffff));
de6d9b64
FB
334}
335
77eb5504 336void avio_wl16(AVIOContext *s, unsigned int val)
de6d9b64 337{
77eb5504
AK
338 avio_w8(s, val);
339 avio_w8(s, val >> 8);
de6d9b64
FB
340}
341
77eb5504 342void avio_wb16(AVIOContext *s, unsigned int val)
de6d9b64 343{
77eb5504
AK
344 avio_w8(s, val >> 8);
345 avio_w8(s, val);
de6d9b64
FB
346}
347
77eb5504 348void avio_wl24(AVIOContext *s, unsigned int val)
ea395e8c 349{
77eb5504
AK
350 avio_wl16(s, val & 0xffff);
351 avio_w8(s, val >> 16);
ea395e8c
AJ
352}
353
77eb5504 354void avio_wb24(AVIOContext *s, unsigned int val)
a254c574 355{
77eb5504
AK
356 avio_wb16(s, val >> 8);
357 avio_w8(s, val);
a254c574
MN
358}
359
de6d9b64
FB
360/* Input stream */
361
ae628ec1 362static void fill_buffer(AVIOContext *s)
de6d9b64 363{
1dd1c1c8
LB
364 uint8_t *dst = !s->max_packet_size &&
365 s->buf_end - s->buffer < s->buffer_size ?
366 s->buf_end : s->buffer;
367 int len = s->buffer_size - (dst - s->buffer);
368 int max_buffer_size = s->max_packet_size ?
369 s->max_packet_size : IO_BUFFER_SIZE;
4589dc5c 370
511cf612 371 /* can't fill the buffer without read_packet, just set EOF if appropriate */
a2d1d216
JG
372 if (!s->read_packet && s->buf_ptr >= s->buf_end)
373 s->eof_reached = 1;
374
abba3dd1
FB
375 /* no need to do anything if EOF already reached */
376 if (s->eof_reached)
377 return;
ee9f36a8 378
1dd1c1c8
LB
379 if (s->update_checksum && dst == s->buffer) {
380 if (s->buf_end > s->checksum_ptr)
381 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
382 s->buf_end - s->checksum_ptr);
383 s->checksum_ptr = s->buffer;
ee9f36a8
MN
384 }
385
01d91b9b
TH
386 /* make buffer smaller in case it ended up large after probing */
387 if (s->buffer_size > max_buffer_size) {
59f65d95 388 ffio_set_buf_size(s, max_buffer_size);
01d91b9b
TH
389
390 s->checksum_ptr = dst = s->buffer;
391 len = s->buffer_size;
392 }
393
1dd1c1c8 394 if (s->read_packet)
c66f53cf 395 len = s->read_packet(s->opaque, dst, len);
4589dc5c
MN
396 else
397 len = 0;
abba3dd1
FB
398 if (len <= 0) {
399 /* do not modify buffer if EOF reached so that a seek back can
400 be done without rereading data */
de6d9b64 401 s->eof_reached = 1;
1dd1c1c8
LB
402 if (len < 0)
403 s->error = len;
abba3dd1
FB
404 } else {
405 s->pos += len;
4589dc5c
MN
406 s->buf_ptr = dst;
407 s->buf_end = dst + len;
de6d9b64
FB
408 }
409}
410
7f37f568
DB
411unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
412 unsigned int len)
413{
3abe5fbd 414 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
992e78f5
BC
415}
416
32d05934
AG
417unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
418 unsigned int len)
419{
420 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
421}
422
b64030f2 423unsigned long ffio_get_checksum(AVIOContext *s)
7f37f568 424{
1dd1c1c8
LB
425 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
426 s->buf_ptr - s->checksum_ptr);
427 s->update_checksum = NULL;
ee9f36a8
MN
428 return s->checksum;
429}
430
4c4427a7 431void ffio_init_checksum(AVIOContext *s,
7f37f568
DB
432 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
433 unsigned long checksum)
434{
1dd1c1c8
LB
435 s->update_checksum = update_checksum;
436 if (s->update_checksum) {
437 s->checksum = checksum;
438 s->checksum_ptr = s->buf_ptr;
465e1dad 439 }
ee9f36a8
MN
440}
441
abba3dd1 442/* XXX: put an inline version */
b7effd4e 443int avio_r8(AVIOContext *s)
de6d9b64 444{
3aa13da9 445 if (s->buf_ptr >= s->buf_end)
de6d9b64 446 fill_buffer(s);
853a0fcc
RD
447 if (s->buf_ptr < s->buf_end)
448 return *s->buf_ptr++;
449 return 0;
de6d9b64
FB
450}
451
b7effd4e 452int avio_read(AVIOContext *s, unsigned char *buf, int size)
de6d9b64
FB
453{
454 int len, size1;
455
456 size1 = size;
457 while (size > 0) {
458 len = s->buf_end - s->buf_ptr;
459 if (len > size)
460 len = size;
a0b7e289 461 if (len == 0 || s->write_flag) {
3e2b6358 462 if(size > s->buffer_size && !s->update_checksum){
602dd8f6 463 if(s->read_packet)
c66f53cf 464 len = s->read_packet(s->opaque, buf, size);
3e2b6358
MN
465 if (len <= 0) {
466 /* do not modify buffer if EOF reached so that a seek back can
467 be done without rereading data */
468 s->eof_reached = 1;
469 if(len<0)
470 s->error= len;
471 break;
472 } else {
473 s->pos += len;
474 size -= len;
475 buf += len;
476 s->buf_ptr = s->buffer;
477 s->buf_end = s->buffer/* + len*/;
478 }
1dd1c1c8 479 } else {
3e2b6358
MN
480 fill_buffer(s);
481 len = s->buf_end - s->buf_ptr;
482 if (len == 0)
483 break;
484 }
de6d9b64
FB
485 } else {
486 memcpy(buf, s->buf_ptr, len);
487 buf += len;
488 s->buf_ptr += len;
489 size -= len;
490 }
491 }
7f7686df 492 if (size1 == size) {
3e68b3ba 493 if (s->error) return s->error;
66e5b1df 494 if (s->eof_reached) return AVERROR_EOF;
7f7686df 495 }
de6d9b64
FB
496 return size1 - size;
497}
498
5afe1d27 499int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
daf1e0d3
BA
500{
501 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
502 *data = s->buf_ptr;
503 s->buf_ptr += size;
504 return size;
505 } else {
506 *data = buf;
507 return avio_read(s, buf, size);
508 }
509}
510
b3db9cee 511int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
e15dec10
LS
512{
513 int len;
115329f1 514
1dd1c1c8 515 if (size < 0)
0ecca7a4 516 return -1;
e15dec10 517
a0b7e289
MS
518 if (s->read_packet && s->write_flag) {
519 len = s->read_packet(s->opaque, buf, size);
520 if (len > 0)
521 s->pos += len;
522 return len;
523 }
524
e15dec10
LS
525 len = s->buf_end - s->buf_ptr;
526 if (len == 0) {
53c25ee0
MS
527 /* Reset the buf_end pointer to the start of the buffer, to make sure
528 * the fill_buffer call tries to read as much data as fits into the
529 * full buffer, instead of just what space is left after buf_end.
530 * This avoids returning partial packets at the end of the buffer,
531 * for packet based inputs.
532 */
533 s->buf_end = s->buf_ptr = s->buffer;
e15dec10
LS
534 fill_buffer(s);
535 len = s->buf_end - s->buf_ptr;
536 }
537 if (len > size)
538 len = size;
539 memcpy(buf, s->buf_ptr, len);
540 s->buf_ptr += len;
7f7686df 541 if (!len) {
3e68b3ba 542 if (s->error) return s->error;
66e5b1df 543 if (s->eof_reached) return AVERROR_EOF;
7f7686df 544 }
e15dec10
LS
545 return len;
546}
547
b7effd4e 548unsigned int avio_rl16(AVIOContext *s)
de6d9b64
FB
549{
550 unsigned int val;
b7effd4e
AK
551 val = avio_r8(s);
552 val |= avio_r8(s) << 8;
de6d9b64
FB
553 return val;
554}
555
b7effd4e 556unsigned int avio_rl24(AVIOContext *s)
ea395e8c
AJ
557{
558 unsigned int val;
b7effd4e
AK
559 val = avio_rl16(s);
560 val |= avio_r8(s) << 16;
ea395e8c
AJ
561 return val;
562}
563
b7effd4e 564unsigned int avio_rl32(AVIOContext *s)
de6d9b64
FB
565{
566 unsigned int val;
b7effd4e
AK
567 val = avio_rl16(s);
568 val |= avio_rl16(s) << 16;
de6d9b64
FB
569 return val;
570}
571
b7effd4e 572uint64_t avio_rl64(AVIOContext *s)
de6d9b64 573{
0c1a9eda 574 uint64_t val;
b7effd4e
AK
575 val = (uint64_t)avio_rl32(s);
576 val |= (uint64_t)avio_rl32(s) << 32;
de6d9b64
FB
577 return val;
578}
579
b7effd4e 580unsigned int avio_rb16(AVIOContext *s)
de6d9b64
FB
581{
582 unsigned int val;
b7effd4e
AK
583 val = avio_r8(s) << 8;
584 val |= avio_r8(s);
de6d9b64
FB
585 return val;
586}
587
b7effd4e 588unsigned int avio_rb24(AVIOContext *s)
de6d9b64
FB
589{
590 unsigned int val;
b7effd4e
AK
591 val = avio_rb16(s) << 8;
592 val |= avio_r8(s);
de6d9b64
FB
593 return val;
594}
b7effd4e 595unsigned int avio_rb32(AVIOContext *s)
a254c574
MN
596{
597 unsigned int val;
b7effd4e
AK
598 val = avio_rb16(s) << 16;
599 val |= avio_rb16(s);
a254c574
MN
600 return val;
601}
de6d9b64 602
ae628ec1 603int ff_get_line(AVIOContext *s, char *buf, int maxlen)
7c892951
AJ
604{
605 int i = 0;
606 char c;
607
608 do {
b7effd4e 609 c = avio_r8(s);
a43416a5 610 if (c && i < maxlen-1)
7c892951
AJ
611 buf[i++] = c;
612 } while (c != '\n' && c);
613
614 buf[i] = 0;
a43416a5 615 return i;
7c892951
AJ
616}
617
41d8555f
RD
618int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
619{
620 int i;
621
ab294069
AC
622 if (buflen <= 0)
623 return AVERROR(EINVAL);
41d8555f
RD
624 // reserve 1 byte for terminating 0
625 buflen = FFMIN(buflen - 1, maxlen);
626 for (i = 0; i < buflen; i++)
627 if (!(buf[i] = avio_r8(s)))
628 return i + 1;
ab294069 629 buf[i] = 0;
41d8555f
RD
630 for (; i < maxlen; i++)
631 if (!avio_r8(s))
632 return i + 1;
633 return maxlen;
634}
635
93b78d12 636#define GET_STR16(type, read) \
ae628ec1 637 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
93b78d12
AK
638{\
639 char* q = buf;\
640 int ret = 0;\
ab294069
AC
641 if (buflen <= 0) \
642 return AVERROR(EINVAL); \
93b78d12
AK
643 while (ret + 1 < maxlen) {\
644 uint8_t tmp;\
645 uint32_t ch;\
646 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
647 if (!ch)\
648 break;\
649 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
650 }\
651 *q = 0;\
652 return ret;\
653}\
654
b7effd4e
AK
655GET_STR16(le, avio_rl16)
656GET_STR16(be, avio_rb16)
93b78d12
AK
657
658#undef GET_STR16
659
b7effd4e 660uint64_t avio_rb64(AVIOContext *s)
de6d9b64 661{
0c1a9eda 662 uint64_t val;
b7effd4e
AK
663 val = (uint64_t)avio_rb32(s) << 32;
664 val |= (uint64_t)avio_rb32(s);
de6d9b64
FB
665 return val;
666}
667
4839c192 668uint64_t ffio_read_varlen(AVIOContext *bc){
897d3eef
KS
669 uint64_t val = 0;
670 int tmp;
671
672 do{
b7effd4e 673 tmp = avio_r8(bc);
897d3eef
KS
674 val= (val<<7) + (tmp&127);
675 }while(tmp&128);
676 return val;
677}
678
724f6a0f 679int ffio_fdopen(AVIOContext **s, URLContext *h)
de6d9b64 680{
0c1a9eda 681 uint8_t *buffer;
abba3dd1 682 int buffer_size, max_packet_size;
de6d9b64 683
5958df34 684 max_packet_size = h->max_packet_size;
abba3dd1
FB
685 if (max_packet_size) {
686 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
687 } else {
688 buffer_size = IO_BUFFER_SIZE;
689 }
1ea4f593 690 buffer = av_malloc(buffer_size);
de6d9b64 691 if (!buffer)
8fa36ae0 692 return AVERROR(ENOMEM);
de6d9b64 693
c3bec4cd
AK
694 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
695 ffurl_read, ffurl_write, ffurl_seek);
696 if (!*s) {
899681cd
BA
697 av_free(buffer);
698 return AVERROR(ENOMEM);
699 }
8978feda 700 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
899681cd 701 (*s)->max_packet_size = max_packet_size;
e7e4810a 702 if(h->prot) {
502bdf68 703 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
bc5c918e 704 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
e7e4810a 705 }
1dee0aca 706 (*s)->av_class = &ffio_url_class;
de6d9b64
FB
707 return 0;
708}
709
59f65d95 710int ffio_set_buf_size(AVIOContext *s, int buf_size)
de6d9b64 711{
0c1a9eda 712 uint8_t *buffer;
1ea4f593 713 buffer = av_malloc(buf_size);
de6d9b64 714 if (!buffer)
8fa36ae0 715 return AVERROR(ENOMEM);
de6d9b64 716
1ea4f593 717 av_free(s->buffer);
de6d9b64
FB
718 s->buffer = buffer;
719 s->buffer_size = buf_size;
720 s->buf_ptr = buffer;
59d96941 721 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
770d9daf
BA
722 return 0;
723}
724
ae628ec1 725static int url_resetbuf(AVIOContext *s, int flags)
770d9daf 726{
59d96941 727 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
770d9daf 728
59d96941 729 if (flags & AVIO_FLAG_WRITE) {
770d9daf
BA
730 s->buf_end = s->buffer + s->buffer_size;
731 s->write_flag = 1;
732 } else {
733 s->buf_end = s->buffer;
734 s->write_flag = 0;
735 }
de6d9b64
FB
736 return 0;
737}
738
f1ef2cd9 739int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
01d91b9b
TH
740{
741 int64_t buffer_start;
742 int buffer_size;
496c645d 743 int overlap, new_size, alloc_size;
01d91b9b
TH
744
745 if (s->write_flag)
746 return AVERROR(EINVAL);
747
748 buffer_size = s->buf_end - s->buffer;
749
750 /* the buffers must touch or overlap */
751 if ((buffer_start = s->pos - buffer_size) > buf_size)
752 return AVERROR(EINVAL);
753
754 overlap = buf_size - buffer_start;
755 new_size = buf_size + buffer_size - overlap;
756
496c645d
MS
757 alloc_size = FFMAX(s->buffer_size, new_size);
758 if (alloc_size > buf_size)
759 if (!(buf = av_realloc(buf, alloc_size)))
01d91b9b
TH
760 return AVERROR(ENOMEM);
761
496c645d 762 if (new_size > buf_size) {
01d91b9b
TH
763 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
764 buf_size = new_size;
765 }
766
767 av_free(s->buffer);
768 s->buf_ptr = s->buffer = buf;
496c645d
MS
769 s->buffer_size = alloc_size;
770 s->pos = buf_size;
01d91b9b
TH
771 s->buf_end = s->buf_ptr + buf_size;
772 s->eof_reached = 0;
773 s->must_flush = 0;
774
775 return 0;
776}
777
22a3212e 778int avio_open(AVIOContext **s, const char *filename, int flags)
de6d9b64 779{
1dee0aca
MS
780 return avio_open2(s, filename, flags, NULL, NULL);
781}
782
783int avio_open2(AVIOContext **s, const char *filename, int flags,
784 const AVIOInterruptCB *int_cb, AVDictionary **options)
785{
de6d9b64
FB
786 URLContext *h;
787 int err;
788
1dee0aca 789 err = ffurl_open(&h, filename, flags, int_cb, options);
de6d9b64
FB
790 if (err < 0)
791 return err;
724f6a0f 792 err = ffio_fdopen(s, h);
de6d9b64 793 if (err < 0) {
e52a9145 794 ffurl_close(h);
de6d9b64
FB
795 return err;
796 }
797 return 0;
798}
799
22a3212e 800int avio_close(AVIOContext *s)
de6d9b64 801{
e1e146a2
LB
802 URLContext *h;
803
804 if (!s)
805 return 0;
115329f1 806
5d1203f0 807 avio_flush(s);
e1e146a2 808 h = s->opaque;
30939390 809 av_freep(&s->buffer);
899681cd 810 av_free(s);
e52a9145 811 return ffurl_close(h);
de6d9b64
FB
812}
813
b522000e
LB
814int avio_closep(AVIOContext **s)
815{
816 int ret = avio_close(*s);
817 *s = NULL;
818 return ret;
819}
820
d9d86e00 821int avio_printf(AVIOContext *s, const char *fmt, ...)
abba3dd1
FB
822{
823 va_list ap;
824 char buf[4096];
825 int ret;
826
827 va_start(ap, fmt);
828 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
829 va_end(ap);
77eb5504 830 avio_write(s, buf, strlen(buf));
abba3dd1
FB
831 return ret;
832}
833
ff1ec0c3 834int avio_pause(AVIOContext *s, int pause)
e7e4810a
BA
835{
836 if (!s->read_pause)
837 return AVERROR(ENOSYS);
502bdf68 838 return s->read_pause(s->opaque, pause);
e7e4810a
BA
839}
840
ff1ec0c3 841int64_t avio_seek_time(AVIOContext *s, int stream_index,
933e90a6 842 int64_t timestamp, int flags)
e7e4810a
BA
843{
844 URLContext *h = s->opaque;
bc5c918e 845 int64_t ret;
e7e4810a
BA
846 if (!s->read_seek)
847 return AVERROR(ENOSYS);
848 ret = s->read_seek(h, stream_index, timestamp, flags);
1dd1c1c8 849 if (ret >= 0) {
c007c687 850 int64_t pos;
e7e4810a 851 s->buf_ptr = s->buf_end; // Flush buffer
c007c687
HC
852 pos = s->seek(h, 0, SEEK_CUR);
853 if (pos >= 0)
854 s->pos = pos;
855 else if (pos != AVERROR(ENOSYS))
856 ret = pos;
e7e4810a
BA
857 }
858 return ret;
859}
860
abba3dd1
FB
861/* output in a dynamic buffer */
862
863typedef struct DynBuffer {
864 int pos, size, allocated_size;
0c1a9eda 865 uint8_t *buffer;
abba3dd1 866 int io_buffer_size;
0c1a9eda 867 uint8_t io_buffer[1];
abba3dd1
FB
868} DynBuffer;
869
576ae256 870static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
871{
872 DynBuffer *d = opaque;
6bc03a69 873 unsigned new_size, new_allocated_size;
115329f1 874
abba3dd1
FB
875 /* reallocate buffer if needed */
876 new_size = d->pos + buf_size;
877 new_allocated_size = d->allocated_size;
1dd1c1c8 878 if (new_size < d->pos || new_size > INT_MAX/2)
568e18b1 879 return -1;
abba3dd1
FB
880 while (new_size > new_allocated_size) {
881 if (!new_allocated_size)
882 new_allocated_size = new_size;
883 else
115329f1 884 new_allocated_size += new_allocated_size / 2 + 1;
abba3dd1 885 }
115329f1 886
abba3dd1 887 if (new_allocated_size > d->allocated_size) {
5626f994 888 int err;
d872fb0f
MS
889 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
890 d->allocated_size = 0;
891 d->size = 0;
5626f994 892 return err;
d872fb0f 893 }
abba3dd1
FB
894 d->allocated_size = new_allocated_size;
895 }
896 memcpy(d->buffer + d->pos, buf, buf_size);
897 d->pos = new_size;
898 if (d->pos > d->size)
899 d->size = d->pos;
576ae256 900 return buf_size;
abba3dd1
FB
901}
902
576ae256 903static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
904{
905 unsigned char buf1[4];
576ae256 906 int ret;
abba3dd1
FB
907
908 /* packetized write: output the header */
aa805f94 909 AV_WB32(buf1, buf_size);
1dd1c1c8
LB
910 ret = dyn_buf_write(opaque, buf1, 4);
911 if (ret < 0)
576ae256 912 return ret;
abba3dd1
FB
913
914 /* then the data */
576ae256 915 return dyn_buf_write(opaque, buf, buf_size);
abba3dd1
FB
916}
917
bc5c918e 918static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
abba3dd1
FB
919{
920 DynBuffer *d = opaque;
921
922 if (whence == SEEK_CUR)
923 offset += d->pos;
924 else if (whence == SEEK_END)
925 offset += d->size;
926 if (offset < 0 || offset > 0x7fffffffLL)
927 return -1;
928 d->pos = offset;
929 return 0;
930}
931
ae628ec1 932static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
abba3dd1
FB
933{
934 DynBuffer *d;
0c5a43d6 935 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
115329f1 936
1dd1c1c8 937 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
568e18b1 938 return -1;
dc2c2eea 939 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
abba3dd1 940 if (!d)
0e7ceb24 941 return AVERROR(ENOMEM);
8bea72f7
AK
942 d->io_buffer_size = io_buffer_size;
943 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
944 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
945 max_packet_size ? NULL : dyn_buf_seek);
899681cd
BA
946 if(!*s) {
947 av_free(d);
948 return AVERROR(ENOMEM);
949 }
8bea72f7
AK
950 (*s)->max_packet_size = max_packet_size;
951 return 0;
abba3dd1
FB
952}
953
b92c5452 954int avio_open_dyn_buf(AVIOContext **s)
abba3dd1
FB
955{
956 return url_open_dyn_buf_internal(s, 0);
957}
958
403ee835 959int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
abba3dd1
FB
960{
961 if (max_packet_size <= 0)
962 return -1;
963 return url_open_dyn_buf_internal(s, max_packet_size);
964}
965
6dc7d80d 966int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
abba3dd1
FB
967{
968 DynBuffer *d = s->opaque;
969 int size;
de1807bb
JA
970 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
971 int padding = 0;
972
d07b51bf
MS
973 if (!s) {
974 *pbuffer = NULL;
975 return 0;
976 }
977
de1807bb
JA
978 /* don't attempt to pad fixed-size packet buffers */
979 if (!s->max_packet_size) {
77eb5504 980 avio_write(s, padbuf, sizeof(padbuf));
de1807bb
JA
981 padding = FF_INPUT_BUFFER_PADDING_SIZE;
982 }
abba3dd1 983
b7f2fdde 984 avio_flush(s);
abba3dd1
FB
985
986 *pbuffer = d->buffer;
987 size = d->size;
988 av_free(d);
899681cd 989 av_free(s);
de1807bb 990 return size - padding;
abba3dd1 991}
3627ce2f
MS
992
993static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
994{
995 DynBuffer *d = opaque;
996
997 d->pos += buf_size;
998 if (d->pos > d->size)
999 d->size = d->pos;
1000 return buf_size;
1001}
1002
1003int ffio_open_null_buf(AVIOContext **s)
1004{
1005 int ret = url_open_dyn_buf_internal(s, 0);
1006 if (ret >= 0) {
1007 AVIOContext *pb = *s;
1008 pb->write_packet = null_buf_write;
1009 }
1010 return ret;
1011}
1012
1013int ffio_close_null_buf(AVIOContext *s)
1014{
1015 DynBuffer *d = s->opaque;
1016 int size;
1017
1018 avio_flush(s);
1019
1020 size = d->size;
1021 av_free(d);
1022 av_free(s);
1023 return size;
1024}