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