warning fixes:
[libav.git] / libavformat / aviobuf.c
CommitLineData
de6d9b64
FB
1/*
2 * Buffered I/O for ffmpeg system
19720f15 3 * Copyright (c) 2000,2001 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 *
b78e7197 12 * FFmpeg 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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
de6d9b64 21#include "avformat.h"
5c91a675 22#include "avio.h"
abba3dd1 23#include <stdarg.h>
de6d9b64
FB
24
25#define IO_BUFFER_SIZE 32768
26
398f5d3f
MN
27static void fill_buffer(ByteIOContext *s);
28
de6d9b64
FB
29int init_put_byte(ByteIOContext *s,
30 unsigned char *buffer,
31 int buffer_size,
32 int write_flag,
33 void *opaque,
0c1a9eda 34 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 35 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 36 offset_t (*seek)(void *opaque, offset_t offset, int whence))
de6d9b64
FB
37{
38 s->buffer = buffer;
39 s->buffer_size = buffer_size;
40 s->buf_ptr = buffer;
41 s->write_flag = write_flag;
115329f1 42 if (!s->write_flag)
de6d9b64
FB
43 s->buf_end = buffer;
44 else
45 s->buf_end = buffer + buffer_size;
46 s->opaque = opaque;
47 s->write_packet = write_packet;
48 s->read_packet = read_packet;
49 s->seek = seek;
50 s->pos = 0;
51 s->must_flush = 0;
52 s->eof_reached = 0;
576ae256 53 s->error = 0;
de6d9b64 54 s->is_streamed = 0;
abba3dd1 55 s->max_packet_size = 0;
ee9f36a8 56 s->update_checksum= NULL;
942f3bb5
MN
57 if(!read_packet && !write_flag){
58 s->pos = buffer_size;
59 s->buf_end = s->buffer + buffer_size;
60 }
de6d9b64
FB
61 return 0;
62}
115329f1 63
de6d9b64
FB
64static void flush_buffer(ByteIOContext *s)
65{
66 if (s->buf_ptr > s->buffer) {
576ae256
MN
67 if (s->write_packet && !s->error){
68 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
69 if(ret < 0){
70 s->error = ret;
71 }
72 }
465e1dad 73 if(s->update_checksum){
ee9f36a8
MN
74 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
75 s->checksum_ptr= s->buffer;
76 }
de6d9b64
FB
77 s->pos += s->buf_ptr - s->buffer;
78 }
79 s->buf_ptr = s->buffer;
80}
81
82void put_byte(ByteIOContext *s, int b)
83{
84 *(s->buf_ptr)++ = b;
115329f1 85 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
86 flush_buffer(s);
87}
88
75bdb984 89void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
de6d9b64
FB
90{
91 int len;
92
93 while (size > 0) {
94 len = (s->buf_end - s->buf_ptr);
95 if (len > size)
96 len = size;
97 memcpy(s->buf_ptr, buf, len);
98 s->buf_ptr += len;
99
115329f1 100 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
101 flush_buffer(s);
102
103 buf += len;
104 size -= len;
105 }
106}
107
108void put_flush_packet(ByteIOContext *s)
109{
110 flush_buffer(s);
111 s->must_flush = 0;
112}
113
114offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
115{
116 offset_t offset1;
f40d1739 117 offset_t pos= s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
de6d9b64
FB
118
119 if (whence != SEEK_CUR && whence != SEEK_SET)
8fa36ae0 120 return AVERROR(EINVAL);
115329f1 121
f40d1739
MN
122 if (whence == SEEK_CUR) {
123 offset1 = pos + (s->buf_ptr - s->buffer);
124 if (offset == 0)
125 return offset1;
126 offset += offset1;
127 }
128 offset1 = offset - pos;
129 if (!s->must_flush &&
130 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
131 /* can do the seek inside the buffer */
132 s->buf_ptr = s->buffer + offset1;
398f5d3f
MN
133 } else if(s->is_streamed && !s->write_flag &&
134 offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
135 while(s->pos < offset && !s->eof_reached)
136 fill_buffer(s);
137 s->buf_ptr = s->buf_end + offset - s->pos;
f40d1739 138 } else {
8fa36ae0 139 offset_t res = AVERROR(EPIPE);
68fcdbf1 140
8fa641f8 141#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
f40d1739 142 if (s->write_flag) {
de6d9b64
FB
143 flush_buffer(s);
144 s->must_flush = 1;
f40d1739 145 } else
8fa641f8 146#endif /* defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) */
f40d1739 147 {
de6d9b64 148 s->buf_end = s->buffer;
de6d9b64 149 }
f40d1739 150 s->buf_ptr = s->buffer;
68fcdbf1
RB
151 if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
152 return res;
f40d1739 153 s->pos = offset;
de6d9b64 154 }
f40d1739 155 s->eof_reached = 0;
de6d9b64
FB
156 return offset;
157}
158
159void url_fskip(ByteIOContext *s, offset_t offset)
160{
161 url_fseek(s, offset, SEEK_CUR);
162}
163
164offset_t url_ftell(ByteIOContext *s)
165{
166 return url_fseek(s, 0, SEEK_CUR);
167}
168
a965c478
AJ
169offset_t url_fsize(ByteIOContext *s)
170{
171 offset_t size;
115329f1 172
a965c478 173 if (!s->seek)
8fa36ae0 174 return AVERROR(EPIPE);
8e287af0
MN
175 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
176 if(size<0){
8f42f523
RB
177 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
178 return size;
179 size++;
1ae2c5f2 180 s->seek(s->opaque, s->pos, SEEK_SET);
8e287af0 181 }
a965c478
AJ
182 return size;
183}
184
de6d9b64
FB
185int url_feof(ByteIOContext *s)
186{
187 return s->eof_reached;
188}
189
576ae256
MN
190int url_ferror(ByteIOContext *s)
191{
192 return s->error;
193}
194
de6d9b64
FB
195void put_le32(ByteIOContext *s, unsigned int val)
196{
197 put_byte(s, val);
198 put_byte(s, val >> 8);
199 put_byte(s, val >> 16);
200 put_byte(s, val >> 24);
201}
202
203void put_be32(ByteIOContext *s, unsigned int val)
204{
205 put_byte(s, val >> 24);
206 put_byte(s, val >> 16);
207 put_byte(s, val >> 8);
208 put_byte(s, val);
209}
210
3b4b29dc 211void put_strz(ByteIOContext *s, const char *str)
75bdb984
PG
212{
213 if (str)
214 put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
215 else
216 put_byte(s, 0);
217}
218
0c1a9eda 219void put_le64(ByteIOContext *s, uint64_t val)
de6d9b64 220{
0c1a9eda
ZK
221 put_le32(s, (uint32_t)(val & 0xffffffff));
222 put_le32(s, (uint32_t)(val >> 32));
de6d9b64
FB
223}
224
0c1a9eda 225void put_be64(ByteIOContext *s, uint64_t val)
de6d9b64 226{
0c1a9eda
ZK
227 put_be32(s, (uint32_t)(val >> 32));
228 put_be32(s, (uint32_t)(val & 0xffffffff));
de6d9b64
FB
229}
230
231void put_le16(ByteIOContext *s, unsigned int val)
232{
233 put_byte(s, val);
234 put_byte(s, val >> 8);
235}
236
237void put_be16(ByteIOContext *s, unsigned int val)
238{
239 put_byte(s, val >> 8);
240 put_byte(s, val);
241}
242
ea395e8c
AJ
243void put_le24(ByteIOContext *s, unsigned int val)
244{
245 put_le16(s, val & 0xffff);
246 put_byte(s, val >> 16);
247}
248
a254c574
MN
249void put_be24(ByteIOContext *s, unsigned int val)
250{
251 put_be16(s, val >> 8);
252 put_byte(s, val);
253}
254
0570bf06 255void put_tag(ByteIOContext *s, const char *tag)
de6d9b64
FB
256{
257 while (*tag) {
258 put_byte(s, *tag++);
259 }
260}
261
262/* Input stream */
263
264static void fill_buffer(ByteIOContext *s)
265{
602dd8f6 266 int len=0;
de6d9b64 267
abba3dd1
FB
268 /* no need to do anything if EOF already reached */
269 if (s->eof_reached)
270 return;
ee9f36a8 271
465e1dad 272 if(s->update_checksum){
0f0680c5
MN
273 if(s->buf_end > s->checksum_ptr)
274 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
ee9f36a8
MN
275 s->checksum_ptr= s->buffer;
276 }
277
602dd8f6 278 if(s->read_packet)
2d89f334 279 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
abba3dd1
FB
280 if (len <= 0) {
281 /* do not modify buffer if EOF reached so that a seek back can
282 be done without rereading data */
de6d9b64 283 s->eof_reached = 1;
5f097703
AB
284 if(len<0)
285 s->error= len;
abba3dd1
FB
286 } else {
287 s->pos += len;
288 s->buf_ptr = s->buffer;
289 s->buf_end = s->buffer + len;
de6d9b64
FB
290 }
291}
292
ee9f36a8
MN
293unsigned long get_checksum(ByteIOContext *s){
294 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
465e1dad 295 s->update_checksum= NULL;
ee9f36a8
MN
296 return s->checksum;
297}
298
299void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
300 s->update_checksum= update_checksum;
465e1dad 301 if(s->update_checksum){
ccb15994 302 s->checksum= checksum;
465e1dad
MN
303 s->checksum_ptr= s->buf_ptr;
304 }
ee9f36a8
MN
305}
306
abba3dd1 307/* XXX: put an inline version */
de6d9b64
FB
308int get_byte(ByteIOContext *s)
309{
310 if (s->buf_ptr < s->buf_end) {
311 return *s->buf_ptr++;
312 } else {
313 fill_buffer(s);
314 if (s->buf_ptr < s->buf_end)
315 return *s->buf_ptr++;
316 else
317 return 0;
318 }
319}
320
abba3dd1
FB
321int url_fgetc(ByteIOContext *s)
322{
323 if (s->buf_ptr < s->buf_end) {
324 return *s->buf_ptr++;
325 } else {
326 fill_buffer(s);
327 if (s->buf_ptr < s->buf_end)
328 return *s->buf_ptr++;
329 else
330 return URL_EOF;
331 }
332}
333
de6d9b64
FB
334int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
335{
336 int len, size1;
337
338 size1 = size;
339 while (size > 0) {
340 len = s->buf_end - s->buf_ptr;
341 if (len > size)
342 len = size;
343 if (len == 0) {
3e2b6358 344 if(size > s->buffer_size && !s->update_checksum){
602dd8f6 345 if(s->read_packet)
2d89f334 346 len = s->read_packet(s->opaque, buf, size);
3e2b6358
MN
347 if (len <= 0) {
348 /* do not modify buffer if EOF reached so that a seek back can
349 be done without rereading data */
350 s->eof_reached = 1;
351 if(len<0)
352 s->error= len;
353 break;
354 } else {
355 s->pos += len;
356 size -= len;
357 buf += len;
358 s->buf_ptr = s->buffer;
359 s->buf_end = s->buffer/* + len*/;
360 }
361 }else{
362 fill_buffer(s);
363 len = s->buf_end - s->buf_ptr;
364 if (len == 0)
365 break;
366 }
de6d9b64
FB
367 } else {
368 memcpy(buf, s->buf_ptr, len);
369 buf += len;
370 s->buf_ptr += len;
371 size -= len;
372 }
373 }
374 return size1 - size;
375}
376
e15dec10
LS
377int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
378{
379 int len;
115329f1 380
0ecca7a4
MN
381 if(size<0)
382 return -1;
e15dec10
LS
383
384 len = s->buf_end - s->buf_ptr;
385 if (len == 0) {
386 fill_buffer(s);
387 len = s->buf_end - s->buf_ptr;
388 }
389 if (len > size)
390 len = size;
391 memcpy(buf, s->buf_ptr, len);
392 s->buf_ptr += len;
393 return len;
394}
395
de6d9b64
FB
396unsigned int get_le16(ByteIOContext *s)
397{
398 unsigned int val;
399 val = get_byte(s);
400 val |= get_byte(s) << 8;
401 return val;
402}
403
ea395e8c
AJ
404unsigned int get_le24(ByteIOContext *s)
405{
406 unsigned int val;
407 val = get_le16(s);
408 val |= get_byte(s) << 16;
409 return val;
410}
411
de6d9b64
FB
412unsigned int get_le32(ByteIOContext *s)
413{
414 unsigned int val;
a254c574
MN
415 val = get_le16(s);
416 val |= get_le16(s) << 16;
de6d9b64
FB
417 return val;
418}
419
0c1a9eda 420uint64_t get_le64(ByteIOContext *s)
de6d9b64 421{
0c1a9eda
ZK
422 uint64_t val;
423 val = (uint64_t)get_le32(s);
424 val |= (uint64_t)get_le32(s) << 32;
de6d9b64
FB
425 return val;
426}
427
428unsigned int get_be16(ByteIOContext *s)
429{
430 unsigned int val;
431 val = get_byte(s) << 8;
432 val |= get_byte(s);
433 return val;
434}
435
a254c574 436unsigned int get_be24(ByteIOContext *s)
de6d9b64
FB
437{
438 unsigned int val;
a254c574 439 val = get_be16(s) << 8;
de6d9b64
FB
440 val |= get_byte(s);
441 return val;
442}
a254c574
MN
443unsigned int get_be32(ByteIOContext *s)
444{
445 unsigned int val;
446 val = get_be16(s) << 16;
447 val |= get_be16(s);
448 return val;
449}
de6d9b64 450
3b4b29dc 451char *get_strz(ByteIOContext *s, char *buf, int maxlen)
75bdb984
PG
452{
453 int i = 0;
454 char c;
455
456 while ((c = get_byte(s))) {
457 if (i < maxlen-1)
458 buf[i++] = c;
459 }
115329f1 460
75bdb984
PG
461 buf[i] = 0; /* Ensure null terminated, but may be truncated */
462
463 return buf;
464}
465
0c1a9eda 466uint64_t get_be64(ByteIOContext *s)
de6d9b64 467{
0c1a9eda
ZK
468 uint64_t val;
469 val = (uint64_t)get_be32(s) << 32;
470 val |= (uint64_t)get_be32(s);
de6d9b64
FB
471 return val;
472}
473
474/* link with avio functions */
475
a9e35095 476#ifdef CONFIG_MUXERS
576ae256 477static int url_write_packet(void *opaque, uint8_t *buf, int buf_size)
de6d9b64
FB
478{
479 URLContext *h = opaque;
576ae256 480 return url_write(h, buf, buf_size);
de6d9b64 481}
764ef400 482#else
bb270c08 483#define url_write_packet NULL
a9e35095 484#endif //CONFIG_MUXERS
de6d9b64 485
0c1a9eda 486static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
de6d9b64
FB
487{
488 URLContext *h = opaque;
489 return url_read(h, buf, buf_size);
490}
491
536ffa59 492static offset_t url_seek_packet(void *opaque, offset_t offset, int whence)
de6d9b64
FB
493{
494 URLContext *h = opaque;
53e2f9ca
MN
495 return url_seek(h, offset, whence);
496 //return 0;
de6d9b64
FB
497}
498
499int url_fdopen(ByteIOContext *s, URLContext *h)
500{
0c1a9eda 501 uint8_t *buffer;
abba3dd1 502 int buffer_size, max_packet_size;
de6d9b64 503
115329f1 504
abba3dd1
FB
505 max_packet_size = url_get_max_packet_size(h);
506 if (max_packet_size) {
507 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
508 } else {
509 buffer_size = IO_BUFFER_SIZE;
510 }
1ea4f593 511 buffer = av_malloc(buffer_size);
de6d9b64 512 if (!buffer)
8fa36ae0 513 return AVERROR(ENOMEM);
de6d9b64 514
115329f1 515 if (init_put_byte(s, buffer, buffer_size,
ac9fe33d 516 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
de6d9b64 517 url_read_packet, url_write_packet, url_seek_packet) < 0) {
1ea4f593 518 av_free(buffer);
6f3e0b21 519 return AVERROR(EIO);
de6d9b64
FB
520 }
521 s->is_streamed = h->is_streamed;
abba3dd1 522 s->max_packet_size = max_packet_size;
de6d9b64
FB
523 return 0;
524}
525
de6d9b64
FB
526int url_setbufsize(ByteIOContext *s, int buf_size)
527{
0c1a9eda 528 uint8_t *buffer;
1ea4f593 529 buffer = av_malloc(buf_size);
de6d9b64 530 if (!buffer)
8fa36ae0 531 return AVERROR(ENOMEM);
de6d9b64 532
1ea4f593 533 av_free(s->buffer);
de6d9b64
FB
534 s->buffer = buffer;
535 s->buffer_size = buf_size;
536 s->buf_ptr = buffer;
115329f1 537 if (!s->write_flag)
de6d9b64
FB
538 s->buf_end = buffer;
539 else
540 s->buf_end = buffer + buf_size;
541 return 0;
542}
543
544int url_fopen(ByteIOContext *s, const char *filename, int flags)
545{
546 URLContext *h;
547 int err;
548
549 err = url_open(&h, filename, flags);
550 if (err < 0)
551 return err;
552 err = url_fdopen(s, h);
553 if (err < 0) {
554 url_close(h);
555 return err;
556 }
557 return 0;
558}
559
560int url_fclose(ByteIOContext *s)
561{
562 URLContext *h = s->opaque;
115329f1 563
1ea4f593 564 av_free(s->buffer);
de6d9b64
FB
565 memset(s, 0, sizeof(ByteIOContext));
566 return url_close(h);
567}
568
569URLContext *url_fileno(ByteIOContext *s)
570{
571 return s->opaque;
572}
573
a9e35095 574#ifdef CONFIG_MUXERS
abba3dd1
FB
575int url_fprintf(ByteIOContext *s, const char *fmt, ...)
576{
577 va_list ap;
578 char buf[4096];
579 int ret;
580
581 va_start(ap, fmt);
582 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
583 va_end(ap);
584 put_buffer(s, buf, strlen(buf));
585 return ret;
586}
a9e35095 587#endif //CONFIG_MUXERS
abba3dd1 588
abba3dd1
FB
589char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
590{
591 int c;
592 char *q;
593
594 c = url_fgetc(s);
595 if (c == EOF)
596 return NULL;
597 q = buf;
598 for(;;) {
599 if (c == EOF || c == '\n')
600 break;
601 if ((q - buf) < buf_size - 1)
602 *q++ = c;
603 c = url_fgetc(s);
604 }
605 if (buf_size > 0)
606 *q = '\0';
607 return buf;
608}
609
abba3dd1
FB
610int url_fget_max_packet_size(ByteIOContext *s)
611{
612 return s->max_packet_size;
613}
614
8fa641f8
LLL
615/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
616 * back to the server even if CONFIG_MUXERS is not set. */
617#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
de6d9b64 618/* buffer handling */
0c1a9eda 619int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
de6d9b64 620{
115329f1 621 return init_put_byte(s, buf, buf_size,
ac9fe33d
GB
622 (flags & URL_WRONLY || flags & URL_RDWR),
623 NULL, NULL, NULL, NULL);
de6d9b64
FB
624}
625
de6d9b64
FB
626int url_close_buf(ByteIOContext *s)
627{
abba3dd1 628 put_flush_packet(s);
de6d9b64
FB
629 return s->buf_ptr - s->buffer;
630}
abba3dd1
FB
631
632/* output in a dynamic buffer */
633
634typedef struct DynBuffer {
635 int pos, size, allocated_size;
0c1a9eda 636 uint8_t *buffer;
abba3dd1 637 int io_buffer_size;
0c1a9eda 638 uint8_t io_buffer[1];
abba3dd1
FB
639} DynBuffer;
640
576ae256 641static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
642{
643 DynBuffer *d = opaque;
644 int new_size, new_allocated_size;
115329f1 645
abba3dd1
FB
646 /* reallocate buffer if needed */
647 new_size = d->pos + buf_size;
648 new_allocated_size = d->allocated_size;
568e18b1
MN
649 if(new_size < d->pos || new_size > INT_MAX/2)
650 return -1;
abba3dd1
FB
651 while (new_size > new_allocated_size) {
652 if (!new_allocated_size)
653 new_allocated_size = new_size;
654 else
115329f1 655 new_allocated_size += new_allocated_size / 2 + 1;
abba3dd1 656 }
115329f1 657
abba3dd1 658 if (new_allocated_size > d->allocated_size) {
b88ba823
MH
659 d->buffer = av_realloc(d->buffer, new_allocated_size);
660 if(d->buffer == NULL)
576ae256 661 return -1234;
abba3dd1
FB
662 d->allocated_size = new_allocated_size;
663 }
664 memcpy(d->buffer + d->pos, buf, buf_size);
665 d->pos = new_size;
666 if (d->pos > d->size)
667 d->size = d->pos;
576ae256 668 return buf_size;
abba3dd1
FB
669}
670
576ae256 671static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
672{
673 unsigned char buf1[4];
576ae256 674 int ret;
abba3dd1
FB
675
676 /* packetized write: output the header */
677 buf1[0] = (buf_size >> 24);
678 buf1[1] = (buf_size >> 16);
679 buf1[2] = (buf_size >> 8);
680 buf1[3] = (buf_size);
576ae256
MN
681 ret= dyn_buf_write(opaque, buf1, 4);
682 if(ret < 0)
683 return ret;
abba3dd1
FB
684
685 /* then the data */
576ae256 686 return dyn_buf_write(opaque, buf, buf_size);
abba3dd1
FB
687}
688
536ffa59 689static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence)
abba3dd1
FB
690{
691 DynBuffer *d = opaque;
692
693 if (whence == SEEK_CUR)
694 offset += d->pos;
695 else if (whence == SEEK_END)
696 offset += d->size;
697 if (offset < 0 || offset > 0x7fffffffLL)
698 return -1;
699 d->pos = offset;
700 return 0;
701}
702
703static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
704{
705 DynBuffer *d;
706 int io_buffer_size, ret;
115329f1
DB
707
708 if (max_packet_size)
abba3dd1
FB
709 io_buffer_size = max_packet_size;
710 else
711 io_buffer_size = 1024;
115329f1 712
568e18b1
MN
713 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
714 return -1;
abba3dd1
FB
715 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
716 if (!d)
717 return -1;
718 d->io_buffer_size = io_buffer_size;
719 d->buffer = NULL;
720 d->pos = 0;
721 d->size = 0;
722 d->allocated_size = 0;
115329f1
DB
723 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
724 1, d, NULL,
725 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
abba3dd1
FB
726 max_packet_size ? NULL : dyn_buf_seek);
727 if (ret == 0) {
728 s->max_packet_size = max_packet_size;
729 }
730 return ret;
731}
732
abba3dd1
FB
733int url_open_dyn_buf(ByteIOContext *s)
734{
735 return url_open_dyn_buf_internal(s, 0);
736}
737
abba3dd1
FB
738int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
739{
740 if (max_packet_size <= 0)
741 return -1;
742 return url_open_dyn_buf_internal(s, max_packet_size);
743}
744
0c1a9eda 745int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
abba3dd1
FB
746{
747 DynBuffer *d = s->opaque;
748 int size;
749
750 put_flush_packet(s);
751
752 *pbuffer = d->buffer;
753 size = d->size;
754 av_free(d);
755 return size;
756}
8fa641f8 757#endif /* CONFIG_MUXERS || CONFIG_NETWORK */