1 /*
2 * buffered I/O
3 * Copyright (c) 2000,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/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "avformat.h"
30 #include "avio.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "url.h"
34 #include <stdarg.h>
35
36 #define IO_BUFFER_SIZE 32768
37
38 /**
39 * Do seeks within this distance ahead of the current buffer by skipping
40 * data instead of calling the protocol seek function, for seekable
41 * protocols.
42 */
43 #define SHORT_SEEK_THRESHOLD 32768
44
ff_avio_child_next(void * obj,void * prev)45 static void *ff_avio_child_next(void *obj, void *prev)
46 {
47 AVIOContext *s = obj;
48 return prev ? NULL : s->opaque;
49 }
50
51 #if FF_API_CHILD_CLASS_NEXT
ff_avio_child_class_next(const AVClass * prev)52 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
53 {
54 return prev ? NULL : &ffurl_context_class;
55 }
56 #endif
57
child_class_iterate(void ** iter)58 static const AVClass *child_class_iterate(void **iter)
59 {
60 const AVClass *c = *iter ? NULL : &ffurl_context_class;
61 *iter = (void*)(uintptr_t)c;
62 return c;
63 }
64
65 #define OFFSET(x) offsetof(AVIOContext,x)
66 #define E AV_OPT_FLAG_ENCODING_PARAM
67 #define D AV_OPT_FLAG_DECODING_PARAM
68 static const AVOption ff_avio_options[] = {
69 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
70 { NULL },
71 };
72
73 const AVClass ff_avio_class = {
74 .class_name = "AVIOContext",
75 .item_name = av_default_item_name,
76 .version = LIBAVUTIL_VERSION_INT,
77 .option = ff_avio_options,
78 .child_next = ff_avio_child_next,
79 #if FF_API_CHILD_CLASS_NEXT
80 .child_class_next = ff_avio_child_class_next,
81 #endif
82 .child_class_iterate = child_class_iterate,
83 };
84
85 static void fill_buffer(AVIOContext *s);
86 static int url_resetbuf(AVIOContext *s, int flags);
87
ffio_init_context(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))88 int ffio_init_context(AVIOContext *s,
89 unsigned char *buffer,
90 int buffer_size,
91 int write_flag,
92 void *opaque,
93 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
94 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
95 int64_t (*seek)(void *opaque, int64_t offset, int whence))
96 {
97 memset(s, 0, sizeof(AVIOContext));
98
99 s->buffer = buffer;
100 s->orig_buffer_size =
101 s->buffer_size = buffer_size;
102 s->buf_ptr = buffer;
103 s->buf_ptr_max = buffer;
104 s->opaque = opaque;
105 s->direct = 0;
106
107 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
108
109 s->write_packet = write_packet;
110 s->read_packet = read_packet;
111 s->seek = seek;
112 s->pos = 0;
113 s->eof_reached = 0;
114 s->error = 0;
115 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
116 s->min_packet_size = 0;
117 s->max_packet_size = 0;
118 s->update_checksum = NULL;
119 s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
120
121 if (!read_packet && !write_flag) {
122 s->pos = buffer_size;
123 s->buf_end = s->buffer + buffer_size;
124 }
125 s->read_pause = NULL;
126 s->read_seek = NULL;
127
128 s->write_data_type = NULL;
129 s->ignore_boundary_point = 0;
130 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
131 s->last_time = AV_NOPTS_VALUE;
132 s->short_seek_get = NULL;
133 s->written = 0;
134
135 return 0;
136 }
137
avio_alloc_context(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))138 AVIOContext *avio_alloc_context(
139 unsigned char *buffer,
140 int buffer_size,
141 int write_flag,
142 void *opaque,
143 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
144 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
145 int64_t (*seek)(void *opaque, int64_t offset, int whence))
146 {
147 AVIOContext *s = av_malloc(sizeof(AVIOContext));
148 if (!s)
149 return NULL;
150 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
151 read_packet, write_packet, seek);
152 return s;
153 }
154
avio_context_free(AVIOContext ** ps)155 void avio_context_free(AVIOContext **ps)
156 {
157 av_freep(ps);
158 }
159
writeout(AVIOContext * s,const uint8_t * data,int len)160 static void writeout(AVIOContext *s, const uint8_t *data, int len)
161 {
162 if (!s->error) {
163 int ret = 0;
164 if (s->write_data_type)
165 ret = s->write_data_type(s->opaque, (uint8_t *)data,
166 len,
167 s->current_type,
168 s->last_time);
169 else if (s->write_packet)
170 ret = s->write_packet(s->opaque, (uint8_t *)data, len);
171 if (ret < 0) {
172 s->error = ret;
173 } else {
174 if (s->pos + len > s->written)
175 s->written = s->pos + len;
176 }
177 }
178 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
179 s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
180 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
181 }
182 s->last_time = AV_NOPTS_VALUE;
183 s->writeout_count ++;
184 s->pos += len;
185 }
186
flush_buffer(AVIOContext * s)187 static void flush_buffer(AVIOContext *s)
188 {
189 s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
190 if (s->write_flag && s->buf_ptr_max > s->buffer) {
191 writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
192 if (s->update_checksum) {
193 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
194 s->buf_ptr_max - s->checksum_ptr);
195 s->checksum_ptr = s->buffer;
196 }
197 }
198 s->buf_ptr = s->buf_ptr_max = s->buffer;
199 if (!s->write_flag)
200 s->buf_end = s->buffer;
201 }
202
avio_w8(AVIOContext * s,int b)203 void avio_w8(AVIOContext *s, int b)
204 {
205 av_assert2(b>=-128 && b<=255);
206 *s->buf_ptr++ = b;
207 if (s->buf_ptr >= s->buf_end)
208 flush_buffer(s);
209 }
210
ffio_fill(AVIOContext * s,int b,int count)211 void ffio_fill(AVIOContext *s, int b, int count)
212 {
213 while (count > 0) {
214 int len = FFMIN(s->buf_end - s->buf_ptr, count);
215 memset(s->buf_ptr, b, len);
216 s->buf_ptr += len;
217
218 if (s->buf_ptr >= s->buf_end)
219 flush_buffer(s);
220
221 count -= len;
222 }
223 }
224
avio_write(AVIOContext * s,const unsigned char * buf,int size)225 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
226 {
227 if (s->direct && !s->update_checksum) {
228 avio_flush(s);
229 writeout(s, buf, size);
230 return;
231 }
232 while (size > 0) {
233 int len = FFMIN(s->buf_end - s->buf_ptr, size);
234 memcpy(s->buf_ptr, buf, len);
235 s->buf_ptr += len;
236
237 if (s->buf_ptr >= s->buf_end)
238 flush_buffer(s);
239
240 buf += len;
241 size -= len;
242 }
243 }
244
avio_flush(AVIOContext * s)245 void avio_flush(AVIOContext *s)
246 {
247 int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
248 flush_buffer(s);
249 if (seekback)
250 avio_seek(s, seekback, SEEK_CUR);
251 }
252
avio_seek(AVIOContext * s,int64_t offset,int whence)253 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
254 {
255 int64_t offset1;
256 int64_t pos;
257 int force = whence & AVSEEK_FORCE;
258 int buffer_size;
259 int short_seek;
260 whence &= ~AVSEEK_FORCE;
261
262 if(!s)
263 return AVERROR(EINVAL);
264
265 if ((whence & AVSEEK_SIZE))
266 return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
267
268 buffer_size = s->buf_end - s->buffer;
269 // pos is the absolute position that the beginning of s->buffer corresponds to in the file
270 pos = s->pos - (s->write_flag ? 0 : buffer_size);
271
272 if (whence != SEEK_CUR && whence != SEEK_SET)
273 return AVERROR(EINVAL);
274
275 if (whence == SEEK_CUR) {
276 offset1 = pos + (s->buf_ptr - s->buffer);
277 if (offset == 0)
278 return offset1;
279 if (offset > INT64_MAX - offset1)
280 return AVERROR(EINVAL);
281 offset += offset1;
282 }
283 if (offset < 0)
284 return AVERROR(EINVAL);
285
286 short_seek = s->short_seek_threshold;
287 if (s->short_seek_get)
288 short_seek = FFMAX(s->short_seek_get(s->opaque), short_seek);
289
290 offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
291 s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
292 if ((!s->direct || !s->seek) &&
293 offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
294 /* can do the seek inside the buffer */
295 s->buf_ptr = s->buffer + offset1;
296 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
297 offset1 <= buffer_size + short_seek) &&
298 !s->write_flag && offset1 >= 0 &&
299 (!s->direct || !s->seek) &&
300 (whence != SEEK_END || force)) {
301 while(s->pos < offset && !s->eof_reached)
302 fill_buffer(s);
303 if (s->eof_reached)
304 return AVERROR_EOF;
305 s->buf_ptr = s->buf_end - (s->pos - offset);
306 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
307 int64_t res;
308
309 pos -= FFMIN(buffer_size>>1, pos);
310 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
311 return res;
312 s->buf_end =
313 s->buf_ptr = s->buffer;
314 s->pos = pos;
315 s->eof_reached = 0;
316 fill_buffer(s);
317 return avio_seek(s, offset, SEEK_SET | force);
318 } else {
319 int64_t res;
320 if (s->write_flag) {
321 flush_buffer(s);
322 }
323 if (!s->seek)
324 return AVERROR(EPIPE);
325 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
326 return res;
327 s->seek_count ++;
328 if (!s->write_flag)
329 s->buf_end = s->buffer;
330 s->buf_ptr = s->buf_ptr_max = s->buffer;
331 s->pos = offset;
332 }
333 s->eof_reached = 0;
334 return offset;
335 }
336
avio_skip(AVIOContext * s,int64_t offset)337 int64_t avio_skip(AVIOContext *s, int64_t offset)
338 {
339 return avio_seek(s, offset, SEEK_CUR);
340 }
341
avio_size(AVIOContext * s)342 int64_t avio_size(AVIOContext *s)
343 {
344 int64_t size;
345
346 if (!s)
347 return AVERROR(EINVAL);
348
349 if (s->written)
350 return s->written;
351
352 if (!s->seek)
353 return AVERROR(ENOSYS);
354 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
355 if (size < 0) {
356 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
357 return size;
358 size++;
359 s->seek(s->opaque, s->pos, SEEK_SET);
360 }
361 return size;
362 }
363
avio_feof(AVIOContext * s)364 int avio_feof(AVIOContext *s)
365 {
366 if(!s)
367 return 0;
368 if(s->eof_reached){
369 s->eof_reached=0;
370 fill_buffer(s);
371 }
372 return s->eof_reached;
373 }
374
avio_wl32(AVIOContext * s,unsigned int val)375 void avio_wl32(AVIOContext *s, unsigned int val)
376 {
377 avio_w8(s, (uint8_t) val );
378 avio_w8(s, (uint8_t)(val >> 8 ));
379 avio_w8(s, (uint8_t)(val >> 16));
380 avio_w8(s, val >> 24 );
381 }
382
avio_wb32(AVIOContext * s,unsigned int val)383 void avio_wb32(AVIOContext *s, unsigned int val)
384 {
385 avio_w8(s, val >> 24 );
386 avio_w8(s, (uint8_t)(val >> 16));
387 avio_w8(s, (uint8_t)(val >> 8 ));
388 avio_w8(s, (uint8_t) val );
389 }
390
avio_put_str(AVIOContext * s,const char * str)391 int avio_put_str(AVIOContext *s, const char *str)
392 {
393 int len = 1;
394 if (str) {
395 len += strlen(str);
396 avio_write(s, (const unsigned char *) str, len);
397 } else
398 avio_w8(s, 0);
399 return len;
400 }
401
put_str16(AVIOContext * s,const char * str,const int be)402 static inline int put_str16(AVIOContext *s, const char *str, const int be)
403 {
404 const uint8_t *q = str;
405 int ret = 0;
406 int err = 0;
407
408 while (*q) {
409 uint32_t ch;
410 uint16_t tmp;
411
412 GET_UTF8(ch, *q++, goto invalid;)
413 PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
414 ret += 2;)
415 continue;
416 invalid:
417 av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
418 err = AVERROR(EINVAL);
419 if (!*(q-1))
420 break;
421 }
422 if (be)
423 avio_wb16(s, 0);
424 else
425 avio_wl16(s, 0);
426 if (err)
427 return err;
428 ret += 2;
429 return ret;
430 }
431
432 #define PUT_STR16(type, big_endian) \
433 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
434 { \
435 return put_str16(s, str, big_endian); \
436 }
437
438 PUT_STR16(le, 0)
439 PUT_STR16(be, 1)
440
441 #undef PUT_STR16
442
avio_wl64(AVIOContext * s,uint64_t val)443 void avio_wl64(AVIOContext *s, uint64_t val)
444 {
445 avio_wl32(s, (uint32_t)(val & 0xffffffff));
446 avio_wl32(s, (uint32_t)(val >> 32));
447 }
448
avio_wb64(AVIOContext * s,uint64_t val)449 void avio_wb64(AVIOContext *s, uint64_t val)
450 {
451 avio_wb32(s, (uint32_t)(val >> 32));
452 avio_wb32(s, (uint32_t)(val & 0xffffffff));
453 }
454
avio_wl16(AVIOContext * s,unsigned int val)455 void avio_wl16(AVIOContext *s, unsigned int val)
456 {
457 avio_w8(s, (uint8_t)val);
458 avio_w8(s, (int)val >> 8);
459 }
460
avio_wb16(AVIOContext * s,unsigned int val)461 void avio_wb16(AVIOContext *s, unsigned int val)
462 {
463 avio_w8(s, (int)val >> 8);
464 avio_w8(s, (uint8_t)val);
465 }
466
avio_wl24(AVIOContext * s,unsigned int val)467 void avio_wl24(AVIOContext *s, unsigned int val)
468 {
469 avio_wl16(s, val & 0xffff);
470 avio_w8(s, (int)val >> 16);
471 }
472
avio_wb24(AVIOContext * s,unsigned int val)473 void avio_wb24(AVIOContext *s, unsigned int val)
474 {
475 avio_wb16(s, (int)val >> 8);
476 avio_w8(s, (uint8_t)val);
477 }
478
avio_write_marker(AVIOContext * s,int64_t time,enum AVIODataMarkerType type)479 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
480 {
481 if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
482 if (s->buf_ptr - s->buffer >= s->min_packet_size)
483 avio_flush(s);
484 return;
485 }
486 if (!s->write_data_type)
487 return;
488 // If ignoring boundary points, just treat it as unknown
489 if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
490 type = AVIO_DATA_MARKER_UNKNOWN;
491 // Avoid unnecessary flushes if we are already in non-header/trailer
492 // data and setting the type to unknown
493 if (type == AVIO_DATA_MARKER_UNKNOWN &&
494 (s->current_type != AVIO_DATA_MARKER_HEADER &&
495 s->current_type != AVIO_DATA_MARKER_TRAILER))
496 return;
497
498 switch (type) {
499 case AVIO_DATA_MARKER_HEADER:
500 case AVIO_DATA_MARKER_TRAILER:
501 // For header/trailer, ignore a new marker of the same type;
502 // consecutive header/trailer markers can be merged.
503 if (type == s->current_type)
504 return;
505 break;
506 }
507
508 // If we've reached here, we have a new, noteworthy marker.
509 // Flush the previous data and mark the start of the new data.
510 avio_flush(s);
511 s->current_type = type;
512 s->last_time = time;
513 }
514
read_packet_wrapper(AVIOContext * s,uint8_t * buf,int size)515 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
516 {
517 int ret;
518
519 if (!s->read_packet)
520 return AVERROR(EINVAL);
521 ret = s->read_packet(s->opaque, buf, size);
522 #if FF_API_OLD_AVIO_EOF_0
523 if (!ret && !s->max_packet_size) {
524 av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
525 ret = AVERROR_EOF;
526 }
527 #else
528 av_assert2(ret || s->max_packet_size);
529 #endif
530 return ret;
531 }
532
533 /* Input stream */
534
fill_buffer(AVIOContext * s)535 static void fill_buffer(AVIOContext *s)
536 {
537 int max_buffer_size = s->max_packet_size ?
538 s->max_packet_size : IO_BUFFER_SIZE;
539 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
540 s->buf_end : s->buffer;
541 int len = s->buffer_size - (dst - s->buffer);
542
543 /* can't fill the buffer without read_packet, just set EOF if appropriate */
544 if (!s->read_packet && s->buf_ptr >= s->buf_end)
545 s->eof_reached = 1;
546
547 /* no need to do anything if EOF already reached */
548 if (s->eof_reached)
549 return;
550
551 if (s->update_checksum && dst == s->buffer) {
552 if (s->buf_end > s->checksum_ptr)
553 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
554 s->buf_end - s->checksum_ptr);
555 s->checksum_ptr = s->buffer;
556 }
557
558 /* make buffer smaller in case it ended up large after probing */
559 if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) {
560 if (dst == s->buffer && s->buf_ptr != dst) {
561 int ret = ffio_set_buf_size(s, s->orig_buffer_size);
562 if (ret < 0)
563 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
564
565 s->checksum_ptr = dst = s->buffer;
566 }
567 len = s->orig_buffer_size;
568 }
569
570 len = read_packet_wrapper(s, dst, len);
571 if (len == AVERROR_EOF) {
572 /* do not modify buffer if EOF reached so that a seek back can
573 be done without rereading data */
574 s->eof_reached = 1;
575 } else if (len < 0) {
576 s->eof_reached = 1;
577 s->error= len;
578 } else {
579 s->pos += len;
580 s->buf_ptr = dst;
581 s->buf_end = dst + len;
582 s->bytes_read += len;
583 }
584 }
585
ff_crc04C11DB7_update(unsigned long checksum,const uint8_t * buf,unsigned int len)586 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
587 unsigned int len)
588 {
589 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
590 }
591
ff_crcEDB88320_update(unsigned long checksum,const uint8_t * buf,unsigned int len)592 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
593 unsigned int len)
594 {
595 return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
596 }
597
ff_crcA001_update(unsigned long checksum,const uint8_t * buf,unsigned int len)598 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
599 unsigned int len)
600 {
601 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
602 }
603
ffio_get_checksum(AVIOContext * s)604 unsigned long ffio_get_checksum(AVIOContext *s)
605 {
606 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
607 s->buf_ptr - s->checksum_ptr);
608 s->update_checksum = NULL;
609 return s->checksum;
610 }
611
ffio_init_checksum(AVIOContext * s,unsigned long (* update_checksum)(unsigned long c,const uint8_t * p,unsigned int len),unsigned long checksum)612 void ffio_init_checksum(AVIOContext *s,
613 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
614 unsigned long checksum)
615 {
616 s->update_checksum = update_checksum;
617 if (s->update_checksum) {
618 s->checksum = checksum;
619 s->checksum_ptr = s->buf_ptr;
620 }
621 }
622
623 /* XXX: put an inline version */
avio_r8(AVIOContext * s)624 int avio_r8(AVIOContext *s)
625 {
626 if (s->buf_ptr >= s->buf_end)
627 fill_buffer(s);
628 if (s->buf_ptr < s->buf_end)
629 return *s->buf_ptr++;
630 return 0;
631 }
632
avio_read(AVIOContext * s,unsigned char * buf,int size)633 int avio_read(AVIOContext *s, unsigned char *buf, int size)
634 {
635 int len, size1;
636
637 size1 = size;
638 while (size > 0) {
639 len = FFMIN(s->buf_end - s->buf_ptr, size);
640 if (len == 0 || s->write_flag) {
641 if((s->direct || size > s->buffer_size) && !s->update_checksum) {
642 // bypass the buffer and read data directly into buf
643 len = read_packet_wrapper(s, buf, size);
644 if (len == AVERROR_EOF) {
645 /* do not modify buffer if EOF reached so that a seek back can
646 be done without rereading data */
647 s->eof_reached = 1;
648 break;
649 } else if (len < 0) {
650 s->eof_reached = 1;
651 s->error= len;
652 break;
653 } else {
654 s->pos += len;
655 s->bytes_read += len;
656 size -= len;
657 buf += len;
658 // reset the buffer
659 s->buf_ptr = s->buffer;
660 s->buf_end = s->buffer/* + len*/;
661 }
662 } else {
663 fill_buffer(s);
664 len = s->buf_end - s->buf_ptr;
665 if (len == 0)
666 break;
667 }
668 } else {
669 memcpy(buf, s->buf_ptr, len);
670 buf += len;
671 s->buf_ptr += len;
672 size -= len;
673 }
674 }
675 if (size1 == size) {
676 if (s->error) return s->error;
677 if (avio_feof(s)) return AVERROR_EOF;
678 }
679 return size1 - size;
680 }
681
ffio_read_size(AVIOContext * s,unsigned char * buf,int size)682 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
683 {
684 int ret = avio_read(s, buf, size);
685 if (ret == size)
686 return ret;
687 if (ret < 0 && ret != AVERROR_EOF)
688 return ret;
689 return AVERROR_INVALIDDATA;
690 }
691
ffio_read_indirect(AVIOContext * s,unsigned char * buf,int size,const unsigned char ** data)692 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
693 {
694 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
695 *data = s->buf_ptr;
696 s->buf_ptr += size;
697 return size;
698 } else {
699 *data = buf;
700 return avio_read(s, buf, size);
701 }
702 }
703
avio_read_partial(AVIOContext * s,unsigned char * buf,int size)704 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
705 {
706 int len;
707
708 if (size < 0)
709 return AVERROR(EINVAL);
710
711 if (s->read_packet && s->write_flag) {
712 len = read_packet_wrapper(s, buf, size);
713 if (len > 0)
714 s->pos += len;
715 return len;
716 }
717
718 len = s->buf_end - s->buf_ptr;
719 if (len == 0) {
720 fill_buffer(s);
721 len = s->buf_end - s->buf_ptr;
722 }
723 if (len > size)
724 len = size;
725 memcpy(buf, s->buf_ptr, len);
726 s->buf_ptr += len;
727 if (!len) {
728 if (s->error) return s->error;
729 if (avio_feof(s)) return AVERROR_EOF;
730 }
731 return len;
732 }
733
avio_rl16(AVIOContext * s)734 unsigned int avio_rl16(AVIOContext *s)
735 {
736 unsigned int val;
737 val = avio_r8(s);
738 val |= avio_r8(s) << 8;
739 return val;
740 }
741
avio_rl24(AVIOContext * s)742 unsigned int avio_rl24(AVIOContext *s)
743 {
744 unsigned int val;
745 val = avio_rl16(s);
746 val |= avio_r8(s) << 16;
747 return val;
748 }
749
avio_rl32(AVIOContext * s)750 unsigned int avio_rl32(AVIOContext *s)
751 {
752 unsigned int val;
753 val = avio_rl16(s);
754 val |= avio_rl16(s) << 16;
755 return val;
756 }
757
avio_rl64(AVIOContext * s)758 uint64_t avio_rl64(AVIOContext *s)
759 {
760 uint64_t val;
761 val = (uint64_t)avio_rl32(s);
762 val |= (uint64_t)avio_rl32(s) << 32;
763 return val;
764 }
765
avio_rb16(AVIOContext * s)766 unsigned int avio_rb16(AVIOContext *s)
767 {
768 unsigned int val;
769 val = avio_r8(s) << 8;
770 val |= avio_r8(s);
771 return val;
772 }
773
avio_rb24(AVIOContext * s)774 unsigned int avio_rb24(AVIOContext *s)
775 {
776 unsigned int val;
777 val = avio_rb16(s) << 8;
778 val |= avio_r8(s);
779 return val;
780 }
avio_rb32(AVIOContext * s)781 unsigned int avio_rb32(AVIOContext *s)
782 {
783 unsigned int val;
784 val = avio_rb16(s) << 16;
785 val |= avio_rb16(s);
786 return val;
787 }
788
ff_get_line(AVIOContext * s,char * buf,int maxlen)789 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
790 {
791 int i = 0;
792 char c;
793
794 do {
795 c = avio_r8(s);
796 if (c && i < maxlen-1)
797 buf[i++] = c;
798 } while (c != '\n' && c != '\r' && c);
799 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
800 avio_skip(s, -1);
801
802 buf[i] = 0;
803 return i;
804 }
805
ff_get_chomp_line(AVIOContext * s,char * buf,int maxlen)806 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
807 {
808 int len = ff_get_line(s, buf, maxlen);
809 while (len > 0 && av_isspace(buf[len - 1]))
810 buf[--len] = '\0';
811 return len;
812 }
813
ff_read_line_to_bprint(AVIOContext * s,AVBPrint * bp)814 int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
815 {
816 int len, end;
817 int64_t read = 0;
818 char tmp[1024];
819 char c;
820
821 do {
822 len = 0;
823 do {
824 c = avio_r8(s);
825 end = (c == '\r' || c == '\n' || c == '\0');
826 if (!end)
827 tmp[len++] = c;
828 } while (!end && len < sizeof(tmp));
829 av_bprint_append_data(bp, tmp, len);
830 read += len;
831 } while (!end);
832
833 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
834 avio_skip(s, -1);
835
836 if (!c && s->error)
837 return s->error;
838
839 if (!c && !read && avio_feof(s))
840 return AVERROR_EOF;
841
842 return read;
843 }
844
ff_read_line_to_bprint_overwrite(AVIOContext * s,AVBPrint * bp)845 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
846 {
847 int64_t ret;
848
849 av_bprint_clear(bp);
850 ret = ff_read_line_to_bprint(s, bp);
851 if (ret < 0)
852 return ret;
853
854 if (!av_bprint_is_complete(bp))
855 return AVERROR(ENOMEM);
856
857 return bp->len;
858 }
859
avio_get_str(AVIOContext * s,int maxlen,char * buf,int buflen)860 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
861 {
862 int i;
863
864 if (buflen <= 0)
865 return AVERROR(EINVAL);
866 // reserve 1 byte for terminating 0
867 buflen = FFMIN(buflen - 1, maxlen);
868 for (i = 0; i < buflen; i++)
869 if (!(buf[i] = avio_r8(s)))
870 return i + 1;
871 buf[i] = 0;
872 for (; i < maxlen; i++)
873 if (!avio_r8(s))
874 return i + 1;
875 return maxlen;
876 }
877
878 #define GET_STR16(type, read) \
879 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
880 {\
881 char* q = buf;\
882 int ret = 0;\
883 if (buflen <= 0) \
884 return AVERROR(EINVAL); \
885 while (ret + 1 < maxlen) {\
886 uint8_t tmp;\
887 uint32_t ch;\
888 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
889 if (!ch)\
890 break;\
891 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
892 }\
893 *q = 0;\
894 return ret;\
895 }\
896
GET_STR16(le,avio_rl16)897 GET_STR16(le, avio_rl16)
898 GET_STR16(be, avio_rb16)
899
900 #undef GET_STR16
901
902 uint64_t avio_rb64(AVIOContext *s)
903 {
904 uint64_t val;
905 val = (uint64_t)avio_rb32(s) << 32;
906 val |= (uint64_t)avio_rb32(s);
907 return val;
908 }
909
ffio_read_varlen(AVIOContext * bc)910 uint64_t ffio_read_varlen(AVIOContext *bc){
911 uint64_t val = 0;
912 int tmp;
913
914 do{
915 tmp = avio_r8(bc);
916 val= (val<<7) + (tmp&127);
917 }while(tmp&128);
918 return val;
919 }
920
ffio_fdopen(AVIOContext ** s,URLContext * h)921 int ffio_fdopen(AVIOContext **s, URLContext *h)
922 {
923 uint8_t *buffer = NULL;
924 int buffer_size, max_packet_size;
925
926 max_packet_size = h->max_packet_size;
927 if (max_packet_size) {
928 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
929 } else {
930 buffer_size = IO_BUFFER_SIZE;
931 }
932 if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
933 if (buffer_size > INT_MAX/2)
934 return AVERROR(EINVAL);
935 buffer_size *= 2;
936 }
937 buffer = av_malloc(buffer_size);
938 if (!buffer)
939 return AVERROR(ENOMEM);
940
941 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
942 (int (*)(void *, uint8_t *, int)) ffurl_read,
943 (int (*)(void *, uint8_t *, int)) ffurl_write,
944 (int64_t (*)(void *, int64_t, int))ffurl_seek);
945 if (!*s)
946 goto fail;
947
948 (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
949 if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
950 avio_closep(s);
951 goto fail;
952 }
953 (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
954 if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
955 avio_closep(s);
956 goto fail;
957 }
958 (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
959
960 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
961 (*s)->max_packet_size = max_packet_size;
962 (*s)->min_packet_size = h->min_packet_size;
963 if(h->prot) {
964 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
965 (*s)->read_seek =
966 (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
967
968 if (h->prot->url_read_seek)
969 (*s)->seekable |= AVIO_SEEKABLE_TIME;
970 }
971 (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
972 (*s)->av_class = &ff_avio_class;
973 return 0;
974 fail:
975 av_freep(&buffer);
976 return AVERROR(ENOMEM);
977 }
978
ffio_geturlcontext(AVIOContext * s)979 URLContext* ffio_geturlcontext(AVIOContext *s)
980 {
981 if (!s)
982 return NULL;
983
984 if (s->opaque && s->read_packet == (int (*)(void *, uint8_t *, int))ffurl_read)
985 return s->opaque;
986 else
987 return NULL;
988 }
989
update_checksum(AVIOContext * s)990 static void update_checksum(AVIOContext *s)
991 {
992 if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
993 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
994 s->buf_ptr - s->checksum_ptr);
995 }
996 }
997
ffio_ensure_seekback(AVIOContext * s,int64_t buf_size)998 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
999 {
1000 uint8_t *buffer;
1001 int max_buffer_size = s->max_packet_size ?
1002 s->max_packet_size : IO_BUFFER_SIZE;
1003 ptrdiff_t filled = s->buf_end - s->buf_ptr;
1004
1005 if (buf_size <= s->buf_end - s->buf_ptr)
1006 return 0;
1007
1008 buf_size += max_buffer_size - 1;
1009
1010 if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1011 return 0;
1012 av_assert0(!s->write_flag);
1013
1014 if (buf_size <= s->buffer_size) {
1015 update_checksum(s);
1016 memmove(s->buffer, s->buf_ptr, filled);
1017 } else {
1018 buffer = av_malloc(buf_size);
1019 if (!buffer)
1020 return AVERROR(ENOMEM);
1021 update_checksum(s);
1022 memcpy(buffer, s->buf_ptr, filled);
1023 av_free(s->buffer);
1024 s->buffer = buffer;
1025 s->buffer_size = buf_size;
1026 }
1027 s->buf_ptr = s->buffer;
1028 s->buf_end = s->buffer + filled;
1029 s->checksum_ptr = s->buffer;
1030 return 0;
1031 }
1032
ffio_set_buf_size(AVIOContext * s,int buf_size)1033 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1034 {
1035 uint8_t *buffer;
1036 buffer = av_malloc(buf_size);
1037 if (!buffer)
1038 return AVERROR(ENOMEM);
1039
1040 av_free(s->buffer);
1041 s->buffer = buffer;
1042 s->orig_buffer_size =
1043 s->buffer_size = buf_size;
1044 s->buf_ptr = s->buf_ptr_max = buffer;
1045 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1046 return 0;
1047 }
1048
ffio_realloc_buf(AVIOContext * s,int buf_size)1049 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1050 {
1051 uint8_t *buffer;
1052 int data_size;
1053
1054 if (!s->buffer_size)
1055 return ffio_set_buf_size(s, buf_size);
1056
1057 if (buf_size <= s->buffer_size)
1058 return 0;
1059
1060 buffer = av_malloc(buf_size);
1061 if (!buffer)
1062 return AVERROR(ENOMEM);
1063
1064 data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1065 if (data_size > 0)
1066 memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1067 av_free(s->buffer);
1068 s->buffer = buffer;
1069 s->orig_buffer_size = buf_size;
1070 s->buffer_size = buf_size;
1071 s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1072 if (s->write_flag)
1073 s->buf_ptr_max = s->buffer + data_size;
1074
1075 s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1076
1077 return 0;
1078 }
1079
url_resetbuf(AVIOContext * s,int flags)1080 static int url_resetbuf(AVIOContext *s, int flags)
1081 {
1082 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1083
1084 if (flags & AVIO_FLAG_WRITE) {
1085 s->buf_end = s->buffer + s->buffer_size;
1086 s->write_flag = 1;
1087 } else {
1088 s->buf_end = s->buffer;
1089 s->write_flag = 0;
1090 }
1091 return 0;
1092 }
1093
ffio_rewind_with_probe_data(AVIOContext * s,unsigned char ** bufp,int buf_size)1094 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1095 {
1096 int64_t buffer_start;
1097 int buffer_size;
1098 int overlap, new_size, alloc_size;
1099 uint8_t *buf = *bufp;
1100
1101 if (s->write_flag) {
1102 av_freep(bufp);
1103 return AVERROR(EINVAL);
1104 }
1105
1106 buffer_size = s->buf_end - s->buffer;
1107
1108 /* the buffers must touch or overlap */
1109 if ((buffer_start = s->pos - buffer_size) > buf_size) {
1110 av_freep(bufp);
1111 return AVERROR(EINVAL);
1112 }
1113
1114 overlap = buf_size - buffer_start;
1115 new_size = buf_size + buffer_size - overlap;
1116
1117 alloc_size = FFMAX(s->buffer_size, new_size);
1118 if (alloc_size > buf_size)
1119 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1120 return AVERROR(ENOMEM);
1121
1122 if (new_size > buf_size) {
1123 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1124 buf_size = new_size;
1125 }
1126
1127 av_free(s->buffer);
1128 s->buf_ptr = s->buffer = buf;
1129 s->buffer_size = alloc_size;
1130 s->pos = buf_size;
1131 s->buf_end = s->buf_ptr + buf_size;
1132 s->eof_reached = 0;
1133
1134 return 0;
1135 }
1136
avio_open(AVIOContext ** s,const char * filename,int flags)1137 int avio_open(AVIOContext **s, const char *filename, int flags)
1138 {
1139 return avio_open2(s, filename, flags, NULL, NULL);
1140 }
1141
ffio_open_whitelist(AVIOContext ** s,const char * filename,int flags,const AVIOInterruptCB * int_cb,AVDictionary ** options,const char * whitelist,const char * blacklist)1142 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1143 const AVIOInterruptCB *int_cb, AVDictionary **options,
1144 const char *whitelist, const char *blacklist
1145 )
1146 {
1147 URLContext *h;
1148 int err;
1149
1150 *s = NULL;
1151
1152 err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1153 if (err < 0)
1154 return err;
1155 err = ffio_fdopen(s, h);
1156 if (err < 0) {
1157 ffurl_close(h);
1158 return err;
1159 }
1160 return 0;
1161 }
1162
avio_open2(AVIOContext ** s,const char * filename,int flags,const AVIOInterruptCB * int_cb,AVDictionary ** options)1163 int avio_open2(AVIOContext **s, const char *filename, int flags,
1164 const AVIOInterruptCB *int_cb, AVDictionary **options)
1165 {
1166 return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1167 }
1168
avio_close(AVIOContext * s)1169 int avio_close(AVIOContext *s)
1170 {
1171 URLContext *h;
1172
1173 if (!s)
1174 return 0;
1175
1176 avio_flush(s);
1177 h = s->opaque;
1178 s->opaque = NULL;
1179
1180 av_freep(&s->buffer);
1181 if (s->write_flag)
1182 av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1183 else
1184 av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1185 av_opt_free(s);
1186
1187 avio_context_free(&s);
1188
1189 return ffurl_close(h);
1190 }
1191
avio_closep(AVIOContext ** s)1192 int avio_closep(AVIOContext **s)
1193 {
1194 int ret = avio_close(*s);
1195 *s = NULL;
1196 return ret;
1197 }
1198
avio_printf(AVIOContext * s,const char * fmt,...)1199 int avio_printf(AVIOContext *s, const char *fmt, ...)
1200 {
1201 va_list ap;
1202 AVBPrint bp;
1203
1204 av_bprint_init(&bp, 0, INT_MAX);
1205 va_start(ap, fmt);
1206 av_vbprintf(&bp, fmt, ap);
1207 va_end(ap);
1208 if (!av_bprint_is_complete(&bp)) {
1209 av_bprint_finalize(&bp, NULL);
1210 s->error = AVERROR(ENOMEM);
1211 return AVERROR(ENOMEM);
1212 }
1213 avio_write(s, bp.str, bp.len);
1214 av_bprint_finalize(&bp, NULL);
1215 return bp.len;
1216 }
1217
avio_print_string_array(AVIOContext * s,const char * strings[])1218 void avio_print_string_array(AVIOContext *s, const char *strings[])
1219 {
1220 for(; *strings; strings++)
1221 avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1222 }
1223
avio_pause(AVIOContext * s,int pause)1224 int avio_pause(AVIOContext *s, int pause)
1225 {
1226 if (!s->read_pause)
1227 return AVERROR(ENOSYS);
1228 return s->read_pause(s->opaque, pause);
1229 }
1230
avio_seek_time(AVIOContext * s,int stream_index,int64_t timestamp,int flags)1231 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1232 int64_t timestamp, int flags)
1233 {
1234 int64_t ret;
1235 if (!s->read_seek)
1236 return AVERROR(ENOSYS);
1237 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1238 if (ret >= 0) {
1239 int64_t pos;
1240 s->buf_ptr = s->buf_end; // Flush buffer
1241 pos = s->seek(s->opaque, 0, SEEK_CUR);
1242 if (pos >= 0)
1243 s->pos = pos;
1244 else if (pos != AVERROR(ENOSYS))
1245 ret = pos;
1246 }
1247 return ret;
1248 }
1249
avio_read_to_bprint(AVIOContext * h,AVBPrint * pb,size_t max_size)1250 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1251 {
1252 int ret;
1253 char buf[1024];
1254 while (max_size) {
1255 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1256 if (ret == AVERROR_EOF)
1257 return 0;
1258 if (ret <= 0)
1259 return ret;
1260 av_bprint_append_data(pb, buf, ret);
1261 if (!av_bprint_is_complete(pb))
1262 return AVERROR(ENOMEM);
1263 max_size -= ret;
1264 }
1265 return 0;
1266 }
1267
avio_accept(AVIOContext * s,AVIOContext ** c)1268 int avio_accept(AVIOContext *s, AVIOContext **c)
1269 {
1270 int ret;
1271 URLContext *sc = s->opaque;
1272 URLContext *cc = NULL;
1273 ret = ffurl_accept(sc, &cc);
1274 if (ret < 0)
1275 return ret;
1276 return ffio_fdopen(c, cc);
1277 }
1278
avio_handshake(AVIOContext * c)1279 int avio_handshake(AVIOContext *c)
1280 {
1281 URLContext *cc = c->opaque;
1282 return ffurl_handshake(cc);
1283 }
1284
1285 /* output in a dynamic buffer */
1286
1287 typedef struct DynBuffer {
1288 int pos, size, allocated_size;
1289 uint8_t *buffer;
1290 int io_buffer_size;
1291 uint8_t io_buffer[1];
1292 } DynBuffer;
1293
dyn_buf_write(void * opaque,uint8_t * buf,int buf_size)1294 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1295 {
1296 DynBuffer *d = opaque;
1297 unsigned new_size;
1298
1299 /* reallocate buffer if needed */
1300 new_size = (unsigned)d->pos + buf_size;
1301 if (new_size < d->pos || new_size > INT_MAX)
1302 return AVERROR(ERANGE);
1303 if (new_size > d->allocated_size) {
1304 unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1305 : new_size;
1306 int err;
1307 while (new_size > new_allocated_size)
1308 new_allocated_size += new_allocated_size / 2 + 1;
1309
1310 new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1311
1312 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1313 d->allocated_size = 0;
1314 d->size = 0;
1315 return err;
1316 }
1317 d->allocated_size = new_allocated_size;
1318 }
1319 memcpy(d->buffer + d->pos, buf, buf_size);
1320 d->pos = new_size;
1321 if (d->pos > d->size)
1322 d->size = d->pos;
1323 return buf_size;
1324 }
1325
dyn_packet_buf_write(void * opaque,uint8_t * buf,int buf_size)1326 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1327 {
1328 unsigned char buf1[4];
1329 int ret;
1330
1331 /* packetized write: output the header */
1332 AV_WB32(buf1, buf_size);
1333 ret = dyn_buf_write(opaque, buf1, 4);
1334 if (ret < 0)
1335 return ret;
1336
1337 /* then the data */
1338 return dyn_buf_write(opaque, buf, buf_size);
1339 }
1340
dyn_buf_seek(void * opaque,int64_t offset,int whence)1341 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1342 {
1343 DynBuffer *d = opaque;
1344
1345 if (whence == SEEK_CUR)
1346 offset += d->pos;
1347 else if (whence == SEEK_END)
1348 offset += d->size;
1349 if (offset < 0)
1350 return AVERROR(EINVAL);
1351 if (offset > INT_MAX)
1352 return AVERROR(ERANGE);
1353 d->pos = offset;
1354 return 0;
1355 }
1356
url_open_dyn_buf_internal(AVIOContext ** s,int max_packet_size)1357 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1358 {
1359 DynBuffer *d;
1360 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1361
1362 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1363 return AVERROR(ERANGE);
1364 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1365 if (!d)
1366 return AVERROR(ENOMEM);
1367 d->io_buffer_size = io_buffer_size;
1368 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1369 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1370 max_packet_size ? NULL : dyn_buf_seek);
1371 if(!*s) {
1372 av_free(d);
1373 return AVERROR(ENOMEM);
1374 }
1375 (*s)->max_packet_size = max_packet_size;
1376 return 0;
1377 }
1378
avio_open_dyn_buf(AVIOContext ** s)1379 int avio_open_dyn_buf(AVIOContext **s)
1380 {
1381 return url_open_dyn_buf_internal(s, 0);
1382 }
1383
ffio_open_dyn_packet_buf(AVIOContext ** s,int max_packet_size)1384 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1385 {
1386 if (max_packet_size <= 0)
1387 return AVERROR(EINVAL);
1388 return url_open_dyn_buf_internal(s, max_packet_size);
1389 }
1390
avio_get_dyn_buf(AVIOContext * s,uint8_t ** pbuffer)1391 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1392 {
1393 DynBuffer *d;
1394
1395 if (!s) {
1396 *pbuffer = NULL;
1397 return 0;
1398 }
1399 d = s->opaque;
1400
1401 if (!s->error && !d->size) {
1402 *pbuffer = d->io_buffer;
1403 return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1404 }
1405
1406 avio_flush(s);
1407
1408 *pbuffer = d->buffer;
1409
1410 return d->size;
1411 }
1412
ffio_reset_dyn_buf(AVIOContext * s)1413 void ffio_reset_dyn_buf(AVIOContext *s)
1414 {
1415 DynBuffer *d = s->opaque;
1416 int max_packet_size = s->max_packet_size;
1417
1418 ffio_init_context(s, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1419 s->write_packet, s->seek);
1420 s->max_packet_size = max_packet_size;
1421 d->pos = d->size = 0;
1422 }
1423
avio_close_dyn_buf(AVIOContext * s,uint8_t ** pbuffer)1424 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1425 {
1426 DynBuffer *d;
1427 int size;
1428 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1429 int padding = 0;
1430
1431 if (!s) {
1432 *pbuffer = NULL;
1433 return 0;
1434 }
1435
1436 /* don't attempt to pad fixed-size packet buffers */
1437 if (!s->max_packet_size) {
1438 avio_write(s, padbuf, sizeof(padbuf));
1439 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1440 }
1441
1442 avio_flush(s);
1443
1444 d = s->opaque;
1445 *pbuffer = d->buffer;
1446 size = d->size;
1447 av_free(d);
1448
1449 avio_context_free(&s);
1450
1451 return size - padding;
1452 }
1453
ffio_free_dyn_buf(AVIOContext ** s)1454 void ffio_free_dyn_buf(AVIOContext **s)
1455 {
1456 DynBuffer *d;
1457
1458 if (!*s)
1459 return;
1460
1461 d = (*s)->opaque;
1462 av_free(d->buffer);
1463 av_free(d);
1464 avio_context_free(s);
1465 }
1466
null_buf_write(void * opaque,uint8_t * buf,int buf_size)1467 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1468 {
1469 DynBuffer *d = opaque;
1470
1471 d->pos += buf_size;
1472 if (d->pos > d->size)
1473 d->size = d->pos;
1474 return buf_size;
1475 }
1476
ffio_open_null_buf(AVIOContext ** s)1477 int ffio_open_null_buf(AVIOContext **s)
1478 {
1479 int ret = url_open_dyn_buf_internal(s, 0);
1480 if (ret >= 0) {
1481 AVIOContext *pb = *s;
1482 pb->write_packet = null_buf_write;
1483 }
1484 return ret;
1485 }
1486
ffio_close_null_buf(AVIOContext * s)1487 int ffio_close_null_buf(AVIOContext *s)
1488 {
1489 DynBuffer *d = s->opaque;
1490 int size;
1491
1492 avio_flush(s);
1493
1494 size = d->size;
1495 av_free(d);
1496
1497 avio_context_free(&s);
1498
1499 return size;
1500 }
1501