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