FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/aviobuf.c
Date: 2026-09-14 12:48:48
Exec Total Coverage
Lines: 741 850 87.2%
Functions: 86 88 97.7%
Branches: 431 606 71.1%

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 248654 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 248654 AVIOContext *const s = &ctx->pub;
60
61 248654 memset(ctx, 0, sizeof(*ctx));
62
63 248654 s->buffer = buffer;
64 248654 ctx->orig_buffer_size =
65 248654 s->buffer_size = buffer_size;
66 248654 s->buf_ptr = buffer;
67 248654 s->buf_ptr_max = buffer;
68 248654 s->opaque = opaque;
69 248654 s->direct = 0;
70
71
2/2
✓ Branch 0 taken 53308 times.
✓ Branch 1 taken 195346 times.
248654 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
72
73 248654 s->write_packet = write_packet;
74 248654 s->read_packet = read_packet;
75 248654 s->seek = seek;
76 248654 s->pos = 0;
77 248654 s->eof_reached = 0;
78 248654 s->error = 0;
79 248654 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
80 248654 s->min_packet_size = 0;
81 248654 s->max_packet_size = 0;
82 248654 s->update_checksum = NULL;
83 248654 ctx->short_seek_threshold = SHORT_SEEK_THRESHOLD;
84
85
4/4
✓ Branch 0 taken 131290 times.
✓ Branch 1 taken 117364 times.
✓ Branch 2 taken 87594 times.
✓ Branch 3 taken 43696 times.
248654 if (!read_packet && !write_flag) {
86 87594 s->pos = buffer_size;
87 87594 s->buf_end = s->buffer + buffer_size;
88 }
89 248654 s->read_pause = NULL;
90 248654 s->read_seek = NULL;
91
92 248654 s->write_data_type = NULL;
93 248654 s->ignore_boundary_point = 0;
94 248654 ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
95 248654 ctx->last_time = AV_NOPTS_VALUE;
96 248654 ctx->short_seek_get = NULL;
97 248654 }
98
99 87398 void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
100 {
101 87398 ffio_init_context(s, (unsigned char*)buffer, buffer_size, 0, NULL, NULL, NULL, NULL);
102 87398 }
103
104 231 void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
105 {
106 231 ffio_init_context(s, buffer, buffer_size, 1, NULL, NULL, NULL, NULL);
107 231 }
108
109 117376 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 117376 FFIOContext *s = av_malloc(sizeof(*s));
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117376 times.
117376 if (!s)
120 return NULL;
121 117376 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
122 read_packet, write_packet, seek);
123 117376 return &s->pub;
124 }
125
126 138805 void avio_context_free(AVIOContext **ps)
127 {
128 138805 AVIOContext *s = *ps;
129
2/2
✓ Branch 0 taken 138803 times.
✓ Branch 1 taken 2 times.
138805 if (s) {
130 138803 av_freep(&s->protocol_whitelist);
131 138803 av_freep(&s->protocol_blacklist);
132 }
133 138805 av_freep(ps);
134 138805 }
135
136 447843 static void writeout(AVIOContext *s, const uint8_t *data, int len)
137 {
138 447843 FFIOContext *const ctx = ffiocontext(s);
139
1/2
✓ Branch 0 taken 447843 times.
✗ Branch 1 not taken.
447843 if (!s->error) {
140 447843 int ret = 0;
141
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 447714 times.
447843 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
2/2
✓ Branch 0 taken 447701 times.
✓ Branch 1 taken 13 times.
447714 else if (s->write_packet)
147 447701 ret = s->write_packet(s->opaque, data, len);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 447843 times.
447843 if (ret < 0) {
149 s->error = ret;
150 } else {
151 447843 ctx->bytes_written += len;
152 447843 s->bytes_written = ctx->bytes_written;
153
154
2/2
✓ Branch 0 taken 445911 times.
✓ Branch 1 taken 1932 times.
447843 if (s->pos + len > ctx->written_output_size) {
155 445911 ctx->written_output_size = s->pos + len;
156 }
157 }
158 }
159
2/2
✓ Branch 0 taken 447796 times.
✓ Branch 1 taken 47 times.
447843 if (ctx->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
160
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 447791 times.
447796 ctx->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
161 52 ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
162 }
163 447843 ctx->last_time = AV_NOPTS_VALUE;
164 447843 ctx->writeout_count++;
165 447843 s->pos += len;
166 447843 }
167
168 589066 static void flush_buffer(AVIOContext *s)
169 {
170 589066 s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
171
4/4
✓ Branch 0 taken 481343 times.
✓ Branch 1 taken 107723 times.
✓ Branch 2 taken 440577 times.
✓ Branch 3 taken 40766 times.
589066 if (s->write_flag && s->buf_ptr_max > s->buffer) {
172 440577 writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 440577 times.
440577 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 589066 s->buf_ptr = s->buf_ptr_max = s->buffer;
180
2/2
✓ Branch 0 taken 107723 times.
✓ Branch 1 taken 481343 times.
589066 if (!s->write_flag)
181 107723 s->buf_end = s->buffer;
182 589066 }
183
184 3686810 void avio_w8(AVIOContext *s, int b)
185 {
186 av_assert2(b>=-128 && b<=255);
187 3686810 *s->buf_ptr++ = b;
188
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 3686745 times.
3686810 if (s->buf_ptr >= s->buf_end)
189 65 flush_buffer(s);
190 3686810 }
191
192 34730 void ffio_fill(AVIOContext *s, int b, int64_t count)
193 {
194
2/2
✓ Branch 0 taken 22330 times.
✓ Branch 1 taken 34730 times.
57060 while (count > 0) {
195 22330 int len = FFMIN(s->buf_end - s->buf_ptr, count);
196 22330 memset(s->buf_ptr, b, len);
197 22330 s->buf_ptr += len;
198
199
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 22094 times.
22330 if (s->buf_ptr >= s->buf_end)
200 236 flush_buffer(s);
201
202 22330 count -= len;
203 }
204 34730 }
205
206 919995 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
207 {
208
2/2
✓ Branch 0 taken 9090 times.
✓ Branch 1 taken 910905 times.
919995 if (size <= 0)
209 9090 return;
210
3/4
✓ Branch 0 taken 7266 times.
✓ Branch 1 taken 903639 times.
✓ Branch 2 taken 7266 times.
✗ Branch 3 not taken.
910905 if (s->direct && !s->update_checksum) {
211 7266 avio_flush(s);
212 7266 writeout(s, buf, size);
213 7266 return;
214 }
215 do {
216 1066188 int len = FFMIN(s->buf_end - s->buf_ptr, size);
217 1066188 memcpy(s->buf_ptr, buf, len);
218 1066188 s->buf_ptr += len;
219
220
2/2
✓ Branch 0 taken 163431 times.
✓ Branch 1 taken 902757 times.
1066188 if (s->buf_ptr >= s->buf_end)
221 163431 flush_buffer(s);
222
223 1066188 buf += len;
224 1066188 size -= len;
225
2/2
✓ Branch 0 taken 162549 times.
✓ Branch 1 taken 903639 times.
1066188 } while (size > 0);
226 }
227
228 422196 void avio_flush(AVIOContext *s)
229 {
230
2/2
✓ Branch 0 taken 314473 times.
✓ Branch 1 taken 107723 times.
422196 int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
231 422196 flush_buffer(s);
232
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 422068 times.
422196 if (seekback)
233 128 avio_seek(s, seekback, SEEK_CUR);
234 422196 }
235
236 4085074 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
237 {
238 4085074 FFIOContext *const ctx = ffiocontext(s);
239 int64_t offset1;
240 int64_t pos;
241 int buffer_size;
242 int short_seek;
243 4085074 whence &= ~AVSEEK_FORCE; // force flag does nothing
244
245
2/2
✓ Branch 0 taken 6578 times.
✓ Branch 1 taken 4078496 times.
4085074 if(!s)
246 6578 return AVERROR(EINVAL);
247
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4078496 times.
4078496 if ((whence & AVSEEK_SIZE))
249 return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
250
251 4078496 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
2/2
✓ Branch 0 taken 3762808 times.
✓ Branch 1 taken 315688 times.
4078496 pos = s->pos - (s->write_flag ? 0 : buffer_size);
254
255
3/4
✓ Branch 0 taken 230917 times.
✓ Branch 1 taken 3847579 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 230917 times.
4078496 if (whence != SEEK_CUR && whence != SEEK_SET)
256 return AVERROR(EINVAL);
257
258
2/2
✓ Branch 0 taken 3847579 times.
✓ Branch 1 taken 230917 times.
4078496 if (whence == SEEK_CUR) {
259 3847579 offset1 = pos + (s->buf_ptr - s->buffer);
260
2/2
✓ Branch 0 taken 3690416 times.
✓ Branch 1 taken 157163 times.
3847579 if (offset == 0)
261 3690416 return offset1;
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157163 times.
157163 if (offset > INT64_MAX - offset1)
263 return AVERROR(EINVAL);
264 157163 offset += offset1;
265 }
266
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 388026 times.
388080 if (offset < 0)
267 54 return AVERROR(EINVAL);
268
269 388026 short_seek = ctx->short_seek_threshold;
270
2/2
✓ Branch 0 taken 370607 times.
✓ Branch 1 taken 17419 times.
388026 if (ctx->short_seek_get) {
271 370607 int tmp = ctx->short_seek_get(s->opaque);
272 370607 short_seek = FFMAX(tmp, short_seek);
273 }
274
275 388026 offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
276 388026 s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
277
5/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 388017 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 380032 times.
✓ Branch 5 taken 7985 times.
388026 if ((!s->direct || !s->seek) &&
278
4/4
✓ Branch 0 taken 31816 times.
✓ Branch 1 taken 348216 times.
✓ Branch 2 taken 373419 times.
✓ Branch 3 taken 6613 times.
380032 offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
279 /* can do the seek inside the buffer */
280 373419 s->buf_ptr = s->buffer + offset1;
281
2/2
✓ Branch 0 taken 14602 times.
✓ Branch 1 taken 5 times.
14607 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
282
2/2
✓ Branch 0 taken 10575 times.
✓ Branch 1 taken 4027 times.
14602 offset1 <= buffer_size + short_seek) &&
283
4/4
✓ Branch 0 taken 8430 times.
✓ Branch 1 taken 2150 times.
✓ Branch 2 taken 2293 times.
✓ Branch 3 taken 6137 times.
10580 !s->write_flag && offset1 >= 0 &&
284
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2286 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
2293 (!s->direct || !s->seek)) {
285
4/4
✓ Branch 0 taken 2459 times.
✓ Branch 1 taken 2186 times.
✓ Branch 2 taken 2359 times.
✓ Branch 3 taken 100 times.
4645 while(s->pos < offset && !s->eof_reached)
286 2359 fill_buffer(s);
287
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 2186 times.
2286 if (s->eof_reached)
288 100 return AVERROR_EOF;
289 2186 s->buf_ptr = s->buf_end - (s->pos - offset);
290
10/10
✓ Branch 0 taken 9183 times.
✓ Branch 1 taken 3138 times.
✓ Branch 2 taken 6137 times.
✓ Branch 3 taken 3046 times.
✓ Branch 4 taken 486 times.
✓ Branch 5 taken 5651 times.
✓ Branch 6 taken 484 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 474 times.
✓ Branch 9 taken 10 times.
12321 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
291 int64_t res;
292
293 474 pos -= FFMIN(buffer_size>>1, pos);
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 474 times.
474 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
295 return res;
296 474 s->buf_end =
297 474 s->buf_ptr = s->buffer;
298 474 s->pos = pos;
299 474 s->eof_reached = 0;
300 474 fill_buffer(s);
301 474 return avio_seek(s, offset, SEEK_SET);
302 } else {
303 int64_t res;
304
2/2
✓ Branch 0 taken 3138 times.
✓ Branch 1 taken 8709 times.
11847 if (s->write_flag) {
305 3138 flush_buffer(s);
306 }
307
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11845 times.
11847 if (!s->seek)
308 2 return AVERROR(EPIPE);
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11845 times.
11845 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
310 return res;
311 11845 ctx->seek_count++;
312
2/2
✓ Branch 0 taken 8707 times.
✓ Branch 1 taken 3138 times.
11845 if (!s->write_flag)
313 8707 s->buf_end = s->buffer;
314 11845 s->checksum_ptr = s->buf_ptr = s->buf_ptr_max = s->buffer;
315 11845 s->pos = offset;
316 }
317 387450 s->eof_reached = 0;
318 387450 return offset;
319 }
320
321 158412 int64_t avio_skip(AVIOContext *s, int64_t offset)
322 {
323 158412 return avio_seek(s, offset, SEEK_CUR);
324 }
325
326 727704 int64_t avio_size(AVIOContext *s)
327 {
328 727704 FFIOContext *const ctx = ffiocontext(s);
329 int64_t size;
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 727704 times.
727704 if (!s)
332 return AVERROR(EINVAL);
333
334
2/2
✓ Branch 0 taken 453235 times.
✓ Branch 1 taken 274469 times.
727704 if (ctx->written_output_size)
335 453235 return ctx->written_output_size;
336
337
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 274463 times.
274469 if (!s->seek)
338 6 return AVERROR(ENOSYS);
339 274463 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
340
2/2
✓ Branch 0 taken 14562 times.
✓ Branch 1 taken 259901 times.
274463 if (size < 0) {
341
2/2
✓ Branch 1 taken 14557 times.
✓ Branch 2 taken 5 times.
14562 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
342 14557 return size;
343 5 size++;
344 5 s->seek(s->opaque, s->pos, SEEK_SET);
345 }
346 259906 return size;
347 }
348
349 6481665 int avio_feof(AVIOContext *s)
350 {
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6481665 times.
6481665 if(!s)
352 return 0;
353
2/2
✓ Branch 0 taken 7900 times.
✓ Branch 1 taken 6473765 times.
6481665 if(s->eof_reached){
354 7900 s->eof_reached=0;
355 7900 fill_buffer(s);
356 }
357 6481665 return s->eof_reached;
358 }
359
360 545457 void avio_wl32(AVIOContext *s, unsigned int val)
361 {
362 545457 avio_w8(s, (uint8_t) val );
363 545457 avio_w8(s, (uint8_t)(val >> 8 ));
364 545457 avio_w8(s, (uint8_t)(val >> 16));
365 545457 avio_w8(s, val >> 24 );
366 545457 }
367
368 167229 void avio_wb32(AVIOContext *s, unsigned int val)
369 {
370 167229 avio_w8(s, val >> 24 );
371 167229 avio_w8(s, (uint8_t)(val >> 16));
372 167229 avio_w8(s, (uint8_t)(val >> 8 ));
373 167229 avio_w8(s, (uint8_t) val );
374 167229 }
375
376 626 int avio_put_str(AVIOContext *s, const char *str)
377 {
378 626 int len = 1;
379
1/2
✓ Branch 0 taken 626 times.
✗ Branch 1 not taken.
626 if (str) {
380 626 len += strlen(str);
381 626 avio_write(s, (const unsigned char *) str, len);
382 } else
383 avio_w8(s, 0);
384 626 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
2/2
✓ Branch 0 taken 1973 times.
✓ Branch 1 taken 250 times.
2223 while (*q) {
394 uint32_t ch;
395 uint16_t tmp16;
396
397
5/8
✓ Branch 0 taken 1973 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1973 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 1973 times.
2005 GET_UTF8(ch, *q++, goto invalid;)
398
3/8
✓ Branch 0 taken 1973 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 918 times.
✓ Branch 3 taken 1055 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
1973 PUT_UTF16(ch, tmp16, be ? avio_wb16(s, tmp16) : avio_wl16(s, tmp16);
399 ret += 2;)
400 1973 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
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 106 times.
250 if (be)
408 144 avio_wb16(s, 0);
409 else
410 106 avio_wl16(s, 0);
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
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 27187 void avio_wb64(AVIOContext *s, uint64_t val)
435 {
436 27187 avio_wb32(s, (uint32_t)(val >> 32));
437 27187 avio_wb32(s, (uint32_t)(val & 0xffffffff));
438 27187 }
439
440 29589 void avio_wl16(AVIOContext *s, unsigned int val)
441 {
442 29589 avio_w8(s, (uint8_t)val);
443 29589 avio_w8(s, (int)val >> 8);
444 29589 }
445
446 88506 void avio_wb16(AVIOContext *s, unsigned int val)
447 {
448 88506 avio_w8(s, (int)val >> 8);
449 88506 avio_w8(s, (uint8_t)val);
450 88506 }
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 12947 void avio_wb24(AVIOContext *s, unsigned int val)
459 {
460 12947 avio_wb16(s, (int)val >> 8);
461 12947 avio_w8(s, (uint8_t)val);
462 12947 }
463
464 644648 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
465 {
466 644648 FFIOContext *const ctx = ffiocontext(s);
467
2/2
✓ Branch 0 taken 622281 times.
✓ Branch 1 taken 22367 times.
644648 if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
468
2/2
✓ Branch 0 taken 279549 times.
✓ Branch 1 taken 342732 times.
622281 if (s->buf_ptr - s->buffer >= s->min_packet_size)
469 279549 avio_flush(s);
470 622281 return;
471 }
472
2/2
✓ Branch 0 taken 22179 times.
✓ Branch 1 taken 188 times.
22367 if (!s->write_data_type)
473 22179 return;
474 // If ignoring boundary points, just treat it as unknown
475
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
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
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 159 times.
188 if (type == AVIO_DATA_MARKER_UNKNOWN &&
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 (ctx->current_type != AVIO_DATA_MARKER_HEADER &&
481 ctx->current_type != AVIO_DATA_MARKER_TRAILER))
482 return;
483
484
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 81 times.
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
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 96 times.
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 227858 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
502 {
503 int ret;
504
505
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 227855 times.
227858 if (!s->read_packet)
506 3 return AVERROR(EINVAL);
507 227855 ret = s->read_packet(s->opaque, buf, size);
508 av_assert2(ret || s->max_packet_size);
509 227855 return ret;
510 }
511
512 /* Input stream */
513
514 101283 static void fill_buffer(AVIOContext *s)
515 {
516 101283 FFIOContext *const ctx = (FFIOContext *)s;
517 202566 int max_buffer_size = s->max_packet_size ?
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101283 times.
101283 s->max_packet_size : IO_BUFFER_SIZE;
519
2/2
✓ Branch 0 taken 25164 times.
✓ Branch 1 taken 76119 times.
101283 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
520 s->buf_end : s->buffer;
521 101283 int len = s->buffer_size - (dst - s->buffer);
522
523 /* can't fill the buffer without read_packet, just set EOF if appropriate */
524
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 101251 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 3 times.
101283 if (!s->read_packet && s->buf_ptr >= s->buf_end)
525 29 s->eof_reached = 1;
526
527 /* no need to do anything if EOF already reached */
528
2/2
✓ Branch 0 taken 11654 times.
✓ Branch 1 taken 89629 times.
101283 if (s->eof_reached)
529 11654 return;
530
531
3/4
✓ Branch 0 taken 441 times.
✓ Branch 1 taken 89188 times.
✓ Branch 2 taken 441 times.
✗ Branch 3 not taken.
89629 if (s->update_checksum && dst == s->buffer) {
532
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 73 times.
441 if (s->buf_end > s->checksum_ptr)
533 368 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
534 368 s->buf_end - s->checksum_ptr);
535 441 s->checksum_ptr = s->buffer;
536 }
537
538 /* make buffer smaller in case it ended up large after probing */
539
3/4
✓ Branch 0 taken 89626 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 89626 times.
✗ Branch 3 not taken.
89629 if (s->read_packet && ctx->orig_buffer_size &&
540
3/4
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 89198 times.
✓ Branch 2 taken 428 times.
✗ Branch 3 not taken.
89626 s->buffer_size > ctx->orig_buffer_size && len >= ctx->orig_buffer_size) {
541
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 53 times.
428 if (dst == s->buffer && s->buf_ptr != dst) {
542 23 int ret = set_buf_size(s, ctx->orig_buffer_size);
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
544 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
545
546 23 s->checksum_ptr = dst = s->buffer;
547 }
548 428 len = ctx->orig_buffer_size;
549 }
550
551 89629 len = read_packet_wrapper(s, dst, len);
552
2/2
✓ Branch 0 taken 12856 times.
✓ Branch 1 taken 76773 times.
89629 if (len == AVERROR_EOF) {
553 /* do not modify buffer if EOF reached so that a seek back can
554 be done without rereading data */
555 12856 s->eof_reached = 1;
556
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 76770 times.
76773 } else if (len < 0) {
557 3 s->eof_reached = 1;
558 3 s->error= len;
559 } else {
560 76770 s->pos += len;
561 76770 s->buf_ptr = dst;
562 76770 s->buf_end = dst + len;
563 76770 ffiocontext(s)->bytes_read += len;
564 76770 s->bytes_read = ffiocontext(s)->bytes_read;
565 }
566 }
567
568 36044 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
569 unsigned int len)
570 {
571 36044 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
572 }
573
574 9 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
575 unsigned int len)
576 {
577 9 return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
578 }
579
580 189 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
581 unsigned int len)
582 {
583 189 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
584 }
585
586 32641 unsigned long ffio_get_checksum(AVIOContext *s)
587 {
588 65282 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
589 32641 s->buf_ptr - s->checksum_ptr);
590 32641 s->update_checksum = NULL;
591 32641 return s->checksum;
592 }
593
594 32791 void ffio_init_checksum(AVIOContext *s,
595 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
596 unsigned long checksum)
597 {
598 32791 s->update_checksum = update_checksum;
599
2/2
✓ Branch 0 taken 32731 times.
✓ Branch 1 taken 60 times.
32791 if (s->update_checksum) {
600 32731 s->checksum = checksum;
601 32731 s->checksum_ptr = s->buf_ptr;
602 }
603 32791 }
604
605 /* XXX: put an inline version */
606 12163738 int avio_r8(AVIOContext *s)
607 {
608
2/2
✓ Branch 0 taken 26079 times.
✓ Branch 1 taken 12137659 times.
12163738 if (s->buf_ptr >= s->buf_end)
609 26079 fill_buffer(s);
610
2/2
✓ Branch 0 taken 12151257 times.
✓ Branch 1 taken 12481 times.
12163738 if (s->buf_ptr < s->buf_end)
611 12151257 return *s->buf_ptr++;
612 12481 return 0;
613 }
614
615 768239 int avio_read(AVIOContext *s, unsigned char *buf, int size)
616 {
617 int len, size1;
618
619 768239 size1 = size;
620
2/2
✓ Branch 0 taken 839281 times.
✓ Branch 1 taken 765592 times.
1604873 while (size > 0) {
621 839281 len = FFMIN(s->buf_end - s->buf_ptr, size);
622
3/4
✓ Branch 0 taken 657241 times.
✓ Branch 1 taken 182040 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 657241 times.
839281 if (len == 0 || s->write_flag) {
623
7/8
✓ Branch 0 taken 181987 times.
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 138185 times.
✓ Branch 3 taken 43802 times.
✓ Branch 4 taken 138229 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 138229 times.
✗ Branch 7 not taken.
182040 if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
624 // bypass the buffer and read data directly into buf
625 138229 len = read_packet_wrapper(s, buf, size);
626
2/2
✓ Branch 0 taken 584 times.
✓ Branch 1 taken 137645 times.
138229 if (len == AVERROR_EOF) {
627 /* do not modify buffer if EOF reached so that a seek back can
628 be done without rereading data */
629 584 s->eof_reached = 1;
630 584 break;
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137645 times.
137645 } else if (len < 0) {
632 s->eof_reached = 1;
633 s->error= len;
634 break;
635 } else {
636 137645 s->pos += len;
637 137645 ffiocontext(s)->bytes_read += len;
638 137645 s->bytes_read = ffiocontext(s)->bytes_read;
639 137645 size -= len;
640 137645 buf += len;
641 // reset the buffer
642 137645 s->buf_ptr = s->buffer;
643 137645 s->buf_end = s->buffer/* + len*/;
644 }
645 } else {
646 43811 fill_buffer(s);
647 43811 len = s->buf_end - s->buf_ptr;
648
2/2
✓ Branch 0 taken 2063 times.
✓ Branch 1 taken 41748 times.
43811 if (len == 0)
649 2063 break;
650 }
651 } else {
652 657241 memcpy(buf, s->buf_ptr, len);
653 657241 buf += len;
654 657241 s->buf_ptr += len;
655 657241 size -= len;
656 }
657 }
658
2/2
✓ Branch 0 taken 3601 times.
✓ Branch 1 taken 764638 times.
768239 if (size1 == size) {
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3601 times.
3601 if (s->error) return s->error;
660
2/2
✓ Branch 1 taken 1872 times.
✓ Branch 2 taken 1729 times.
3601 if (avio_feof(s)) return AVERROR_EOF;
661 }
662 766367 return size1 - size;
663 }
664
665 183730 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
666 {
667 183730 int ret = avio_read(s, buf, size);
668
2/2
✓ Branch 0 taken 183651 times.
✓ Branch 1 taken 79 times.
183730 if (ret == size)
669 183651 return ret;
670
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
79 if (ret < 0 && ret != AVERROR_EOF)
671 return ret;
672 79 return AVERROR_INVALIDDATA;
673 }
674
675 462338 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
676 {
677
3/4
✓ Branch 0 taken 459082 times.
✓ Branch 1 taken 3256 times.
✓ Branch 2 taken 459082 times.
✗ Branch 3 not taken.
462338 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
678 459082 *data = s->buf_ptr;
679 459082 s->buf_ptr += size;
680 459082 return size;
681 } else {
682 3256 *data = buf;
683 3256 return avio_read(s, buf, size);
684 }
685 }
686
687 607016 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
688 {
689 int len;
690
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 607016 times.
607016 if (size < 0)
692 return AVERROR(EINVAL);
693
694
2/4
✓ Branch 0 taken 607016 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 607016 times.
607016 if (s->read_packet && s->write_flag) {
695 len = read_packet_wrapper(s, buf, size);
696 if (len > 0)
697 s->pos += len;
698 return len;
699 }
700
701 607016 len = s->buf_end - s->buf_ptr;
702
2/2
✓ Branch 0 taken 20660 times.
✓ Branch 1 taken 586356 times.
607016 if (len == 0) {
703 20660 fill_buffer(s);
704 20660 len = s->buf_end - s->buf_ptr;
705 }
706
2/2
✓ Branch 0 taken 585853 times.
✓ Branch 1 taken 21163 times.
607016 if (len > size)
707 585853 len = size;
708 607016 memcpy(buf, s->buf_ptr, len);
709 607016 s->buf_ptr += len;
710
2/2
✓ Branch 0 taken 1999 times.
✓ Branch 1 taken 605017 times.
607016 if (!len) {
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1999 times.
1999 if (s->error) return s->error;
712
1/2
✓ Branch 1 taken 1999 times.
✗ Branch 2 not taken.
1999 if (avio_feof(s)) return AVERROR_EOF;
713 }
714 605017 return len;
715 }
716
717 877404 unsigned int avio_rl16(AVIOContext *s)
718 {
719 unsigned int val;
720 877404 val = avio_r8(s);
721 877404 val |= avio_r8(s) << 8;
722 877404 return val;
723 }
724
725 337 unsigned int avio_rl24(AVIOContext *s)
726 {
727 unsigned int val;
728 337 val = avio_rl16(s);
729 337 val |= avio_r8(s) << 16;
730 337 return val;
731 }
732
733 419301 unsigned int avio_rl32(AVIOContext *s)
734 {
735 unsigned int val;
736 419301 val = avio_rl16(s);
737 419301 val |= avio_rl16(s) << 16;
738 419301 return val;
739 }
740
741 5681 uint64_t avio_rl64(AVIOContext *s)
742 {
743 uint64_t val;
744 5681 val = (uint64_t)avio_rl32(s);
745 5681 val |= (uint64_t)avio_rl32(s) << 32;
746 5681 return val;
747 }
748
749 1202998 unsigned int avio_rb16(AVIOContext *s)
750 {
751 unsigned int val;
752 1202998 val = avio_r8(s) << 8;
753 1202998 val |= avio_r8(s);
754 1202998 return val;
755 }
756
757 28013 unsigned int avio_rb24(AVIOContext *s)
758 {
759 unsigned int val;
760 28013 val = avio_rb16(s) << 8;
761 28013 val |= avio_r8(s);
762 28013 return val;
763 }
764 536736 unsigned int avio_rb32(AVIOContext *s)
765 {
766 unsigned int val;
767 536736 val = avio_rb16(s) << 16;
768 536736 val |= avio_rb16(s);
769 536736 return val;
770 }
771
772 1003 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
773 {
774 1003 int i = 0;
775 char c;
776
777 do {
778 25968 c = avio_r8(s);
779
3/4
✓ Branch 0 taken 25933 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 25933 times.
✗ Branch 3 not taken.
25968 if (c && i < maxlen-1)
780 25933 buf[i++] = c;
781
6/6
✓ Branch 0 taken 25310 times.
✓ Branch 1 taken 658 times.
✓ Branch 2 taken 25000 times.
✓ Branch 3 taken 310 times.
✓ Branch 4 taken 24965 times.
✓ Branch 5 taken 35 times.
25968 } while (c != '\n' && c != '\r' && c);
782
3/6
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 693 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 310 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1003 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
783 avio_skip(s, -1);
784
785 1003 buf[i] = 0;
786 1003 return i;
787 }
788
789 302 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
790 {
791 302 int len = ff_get_line(s, buf, maxlen);
792
4/4
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 283 times.
✓ Branch 3 taken 282 times.
585 while (len > 0 && av_isspace(buf[len - 1]))
793 283 buf[--len] = '\0';
794 302 return len;
795 }
796
797 typedef enum FFBPrintReadStringMode {
798 FFBPrintReadString = 0,
799 FFBPrintReadLine = 1,
800 } FFBPrintReadStringMode;
801
802 338 static int64_t read_string_to_bprint(AVIOContext *s, AVBPrint *bp,
803 FFBPrintReadStringMode mode,
804 int64_t max_len)
805 {
806 int len, end;
807 338 int64_t read = 0;
808 char tmp[1024];
809 char c;
810
811
1/2
✓ Branch 0 taken 338 times.
✗ Branch 1 not taken.
338 if (!max_len)
812 return 0;
813
814 do {
815 338 len = 0;
816 do {
817 5328 c = avio_r8(s);
818
7/8
✓ Branch 0 taken 4562 times.
✓ Branch 1 taken 766 times.
✓ Branch 2 taken 4562 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4294 times.
✓ Branch 5 taken 268 times.
✓ Branch 6 taken 70 times.
✓ Branch 7 taken 4990 times.
5328 end = ((mode == FFBPrintReadLine && (c == '\r' || c == '\n')) ||
819 c == '\0');
820
2/2
✓ Branch 0 taken 4990 times.
✓ Branch 1 taken 338 times.
5328 if (!end)
821 4990 tmp[len++] = c;
822
5/6
✓ Branch 0 taken 4990 times.
✓ Branch 1 taken 338 times.
✓ Branch 2 taken 4990 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4289 times.
✓ Branch 5 taken 701 times.
5328 } while (!end && len < sizeof(tmp) &&
823
1/2
✓ Branch 0 taken 701 times.
✗ Branch 1 not taken.
701 ((max_len < 0) || (read + len < max_len)));
824 338 av_bprint_append_data(bp, tmp, len);
825 338 read += len;
826
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
338 } while (!end && ((max_len < 0) || (read < max_len)));
827
828
3/4
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
338 if (mode == FFBPrintReadLine &&
829 c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
830 avio_skip(s, -1);
831
832
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 268 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
338 if (!c && s->error)
833 return s->error;
834
835
6/6
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 268 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 64 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 1 times.
338 if (!c && !read && avio_feof(s))
836 5 return AVERROR_EOF;
837
838 333 return read;
839 }
840
841 338 static int64_t read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp,
842 FFBPrintReadStringMode mode,
843 int64_t max_len)
844 {
845 int64_t ret;
846
847 338 av_bprint_clear(bp);
848 338 ret = read_string_to_bprint(s, bp, mode, max_len);
849
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 333 times.
338 if (ret < 0)
850 5 return ret;
851
852
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 333 times.
333 if (!av_bprint_is_complete(bp))
853 return AVERROR(ENOMEM);
854
855 333 return bp->len;
856 }
857
858 273 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
859 {
860 273 return read_string_to_bprint_overwrite(s, bp, FFBPrintReadLine, -1);
861 }
862
863 65 int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp,
864 int64_t max_len)
865 {
866 65 return read_string_to_bprint_overwrite(s, bp, FFBPrintReadString, max_len);
867 }
868
869 626 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
870 {
871 int i;
872
873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 626 times.
626 if (buflen <= 0)
874 return AVERROR(EINVAL);
875 // reserve 1 byte for terminating 0
876 626 buflen = FFMIN(buflen - 1, maxlen);
877
2/2
✓ Branch 0 taken 9237 times.
✓ Branch 1 taken 272 times.
9509 for (i = 0; i < buflen; i++)
878
2/2
✓ Branch 1 taken 354 times.
✓ Branch 2 taken 8883 times.
9237 if (!(buf[i] = avio_r8(s)))
879 354 return i + 1;
880 272 buf[i] = 0;
881
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 262 times.
542 for (; i < maxlen; i++)
882
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 270 times.
280 if (!avio_r8(s))
883 10 return i + 1;
884 262 return maxlen;
885 }
886
887 #define GET_STR16(type, read) \
888 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
889 {\
890 char* q = buf;\
891 int ret = 0;\
892 if (buflen <= 0) \
893 return AVERROR(EINVAL); \
894 while (ret + 1 < maxlen) {\
895 uint8_t tmp;\
896 uint32_t ch;\
897 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
898 if (!ch)\
899 break;\
900 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
901 }\
902 *q = 0;\
903 return ret;\
904 }\
905
906
14/26
✗ Branch 0 not taken.
✓ Branch 1 taken 778 times.
✓ Branch 2 taken 10277 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 10277 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 756 times.
✓ Branch 15 taken 9521 times.
✓ Branch 16 taken 9518 times.
✓ Branch 17 taken 3 times.
✓ Branch 18 taken 9518 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 3 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 3 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 3 times.
✓ Branch 25 taken 3 times.
✓ Branch 26 taken 10277 times.
✓ Branch 27 taken 22 times.
10302 GET_STR16(le, avio_rl16)
907
9/26
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 5514 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5514 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 539 times.
✓ Branch 15 taken 4975 times.
✓ Branch 16 taken 4975 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 4975 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✓ Branch 26 taken 5514 times.
✓ Branch 27 taken 57 times.
5571 GET_STR16(be, avio_rb16)
908
909 #undef GET_STR16
910
911 145918 uint64_t avio_rb64(AVIOContext *s)
912 {
913 uint64_t val;
914 145918 val = (uint64_t)avio_rb32(s) << 32;
915 145918 val |= (uint64_t)avio_rb32(s);
916 145918 return val;
917 }
918
919 15934 uint64_t ffio_read_varlen(AVIOContext *bc){
920 15934 uint64_t val = 0;
921 int tmp;
922
923 do{
924 24651 tmp = avio_r8(bc);
925 24651 val= (val<<7) + (tmp&127);
926
2/2
✓ Branch 0 taken 8717 times.
✓ Branch 1 taken 15934 times.
24651 }while(tmp&128);
927 15934 return val;
928 }
929
930 1247 unsigned int ffio_read_leb(AVIOContext *s) {
931 1247 int more, i = 0;
932 1247 unsigned leb = 0;
933
934 do {
935 1726 int byte = avio_r8(s);
936 1726 unsigned bits = byte & 0x7f;
937 1726 more = byte & 0x80;
938
1/2
✓ Branch 0 taken 1726 times.
✗ Branch 1 not taken.
1726 if (i <= 4)
939 1726 leb |= bits << (i * 7);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1726 times.
1726 if (++i == 8)
941 break;
942
2/2
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 1247 times.
1726 } while (more);
943
944 1247 return leb;
945 }
946
947 2683 void ffio_write_leb(AVIOContext *s, unsigned val)
948 {
949 int len;
950 uint8_t byte;
951
952 2683 len = (av_log2(val) + 7) / 7;
953
954
2/2
✓ Branch 0 taken 3720 times.
✓ Branch 1 taken 2683 times.
6403 for (int i = 0; i < len; i++) {
955 3720 byte = val >> (7 * i) & 0x7f;
956
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 2683 times.
3720 if (i < len - 1)
957 1037 byte |= 0x80;
958
959 3720 avio_w8(s, byte);
960 }
961 2683 }
962
963 28 void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size,
964 const unsigned char *ending)
965 {
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 int ending_len = ending ? strlen(ending) : 1;
967
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!ending)
968 28 ending = "\n";
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (size < 0)
970 size = strlen(buf);
971
972
2/2
✓ Branch 0 taken 439 times.
✓ Branch 1 taken 28 times.
467 while (size > 0) {
973 439 size_t len = 0;
974 439 char last = 0;
975
1/2
✓ Branch 0 taken 19804 times.
✗ Branch 1 not taken.
19804 for (; len < size; len++) {
976 19804 last = buf[len];
977
4/4
✓ Branch 0 taken 19701 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 19365 times.
✓ Branch 3 taken 336 times.
19804 if (last == '\r' || last == '\n')
978 break;
979 }
980
981 439 avio_write(s, buf, len);
982 439 avio_write(s, ending, ending_len);
983
984 439 buf += len + 1;
985 439 size -= len + 1;
986
987
5/6
✓ Branch 0 taken 415 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 312 times.
✓ Branch 4 taken 103 times.
✗ Branch 5 not taken.
439 if (size > 0 && last == '\r' && buf[0] == '\n') {
988 103 buf++;
989 103 size--;
990 }
991 }
992 28 }
993
994 16 int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts)
995 {
996 16 const char *opts[] = {
997 "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", "prefer_libcurl", NULL };
998 16 const char **opt = opts;
999 16 uint8_t *buf = NULL;
1000 16 int ret = 0;
1001
1002
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 16 times.
144 while (*opt) {
1003
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 96 times.
128 if (av_opt_get(pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1004
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (buf[0] != '\0') {
1005 32 ret = av_dict_set(avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
1007 return ret;
1008 } else {
1009 av_freep(&buf);
1010 }
1011 }
1012 128 opt++;
1013 }
1014
1015 16 return ret;
1016 }
1017
1018 15 static void update_checksum(AVIOContext *s)
1019 {
1020
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
1021 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
1022 s->buf_ptr - s->checksum_ptr);
1023 }
1024 15 }
1025
1026 20467 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
1027 {
1028 uint8_t *buffer;
1029 40934 int max_buffer_size = s->max_packet_size ?
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20467 times.
20467 s->max_packet_size : IO_BUFFER_SIZE;
1031 20467 ptrdiff_t filled = s->buf_end - s->buf_ptr;
1032
1033
2/2
✓ Branch 0 taken 17697 times.
✓ Branch 1 taken 2770 times.
20467 if (buf_size <= s->buf_end - s->buf_ptr)
1034 17697 return 0;
1035
1036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2770 times.
2770 if (buf_size > INT_MAX - max_buffer_size)
1037 return AVERROR(EINVAL);
1038
1039 2770 buf_size += max_buffer_size - 1;
1040
1041
5/6
✓ Branch 0 taken 2770 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 2744 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 15 times.
2770 if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1042 2755 return 0;
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_assert0(!s->write_flag);
1044
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (buf_size <= s->buffer_size) {
1046 update_checksum(s);
1047 memmove(s->buffer, s->buf_ptr, filled);
1048 } else {
1049 15 buffer = av_malloc(buf_size);
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!buffer)
1051 return AVERROR(ENOMEM);
1052 15 update_checksum(s);
1053 15 memcpy(buffer, s->buf_ptr, filled);
1054 15 av_free(s->buffer);
1055 15 s->buffer = buffer;
1056 15 s->buffer_size = buf_size;
1057 }
1058 15 s->buf_ptr = s->buffer;
1059 15 s->buf_end = s->buffer + filled;
1060 15 s->checksum_ptr = s->buffer;
1061 15 return 0;
1062 }
1063
1064 6852 int ffio_limit(AVIOContext *s, int size)
1065 {
1066 6852 FFIOContext *const ctx = ffiocontext(s);
1067
2/2
✓ Branch 0 taken 6825 times.
✓ Branch 1 taken 27 times.
6852 if (ctx->maxsize >= 0) {
1068 6825 int64_t pos = avio_tell(s);
1069 6825 int64_t remaining = ctx->maxsize - pos;
1070
2/2
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 6369 times.
6825 if (remaining < size) {
1071 456 int64_t newsize = avio_size(s);
1072
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 451 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
456 if (!ctx->maxsize || ctx->maxsize < newsize)
1073 451 ctx->maxsize = newsize - !newsize;
1074
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 454 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
456 if (pos > ctx->maxsize && ctx->maxsize >= 0)
1075 1 ctx->maxsize = AVERROR(EIO);
1076
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 2 times.
456 if (ctx->maxsize >= 0)
1077 454 remaining = ctx->maxsize - pos;
1078 }
1079
1080
5/6
✓ Branch 0 taken 6823 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 6818 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
6825 if (ctx->maxsize >= 0 && remaining < size && size > 1) {
1081 5 av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
1082 "Truncating packet of size %d to %"PRId64"\n",
1083
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 size, remaining + !remaining);
1084 5 size = remaining + !remaining;
1085 }
1086 }
1087 6852 return size;
1088 }
1089
1090 23 static int set_buf_size(AVIOContext *s, int buf_size)
1091 {
1092 uint8_t *buffer;
1093 23 buffer = av_malloc(buf_size);
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (!buffer)
1095 return AVERROR(ENOMEM);
1096
1097 23 av_free(s->buffer);
1098 23 s->buffer = buffer;
1099 46 ffiocontext(s)->orig_buffer_size =
1100 23 s->buffer_size = buf_size;
1101 23 s->buf_ptr = s->buf_ptr_max = buffer;
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1103 23 return 0;
1104 }
1105
1106 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1107 {
1108 uint8_t *buffer;
1109 int data_size;
1110
1111 if (!s->buffer_size)
1112 return set_buf_size(s, buf_size);
1113
1114 if (buf_size <= s->buffer_size)
1115 return 0;
1116
1117 buffer = av_malloc(buf_size);
1118 if (!buffer)
1119 return AVERROR(ENOMEM);
1120
1121 data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1122 if (data_size > 0)
1123 memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1124 av_free(s->buffer);
1125 s->buffer = buffer;
1126 ffiocontext(s)->orig_buffer_size = buf_size;
1127 s->buffer_size = buf_size;
1128 s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1129 if (s->write_flag)
1130 s->buf_ptr_max = s->buffer + data_size;
1131
1132 s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1133
1134 return 0;
1135 }
1136
1137 248677 static int url_resetbuf(AVIOContext *s, int flags)
1138 {
1139 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1140
1141
2/2
✓ Branch 0 taken 53308 times.
✓ Branch 1 taken 195369 times.
248677 if (flags & AVIO_FLAG_WRITE) {
1142 53308 s->buf_end = s->buffer + s->buffer_size;
1143 53308 s->write_flag = 1;
1144 } else {
1145 195369 s->buf_end = s->buffer;
1146 195369 s->write_flag = 0;
1147 }
1148 248677 return 0;
1149 }
1150
1151 4127 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1152 {
1153 int64_t buffer_start;
1154 int buffer_size;
1155 int overlap, new_size, alloc_size;
1156 4127 uint8_t *buf = *bufp;
1157
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4127 times.
4127 if (s->write_flag) {
1159 av_freep(bufp);
1160 return AVERROR(EINVAL);
1161 }
1162
1163 4127 buffer_size = s->buf_end - s->buffer;
1164
1165 /* the buffers must touch or overlap */
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4127 times.
4127 if ((buffer_start = s->pos - buffer_size) > buf_size) {
1167 av_freep(bufp);
1168 return AVERROR(EINVAL);
1169 }
1170
1171 4127 overlap = buf_size - buffer_start;
1172 4127 new_size = buf_size + buffer_size - overlap;
1173
1174 4127 alloc_size = FFMAX(s->buffer_size, new_size);
1175
2/2
✓ Branch 0 taken 4072 times.
✓ Branch 1 taken 55 times.
4127 if (alloc_size > buf_size)
1176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4072 times.
4072 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1177 return AVERROR(ENOMEM);
1178
1179
2/2
✓ Branch 0 taken 3884 times.
✓ Branch 1 taken 243 times.
4127 if (new_size > buf_size) {
1180 3884 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1181 3884 buf_size = new_size;
1182 }
1183
1184 4127 av_free(s->buffer);
1185 4127 s->buf_ptr = s->buffer = buf;
1186 4127 s->buffer_size = alloc_size;
1187 4127 s->pos = buf_size;
1188 4127 s->buf_end = s->buf_ptr + buf_size;
1189 4127 s->eof_reached = 0;
1190
1191 4127 return 0;
1192 }
1193
1194 26221 int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
1195 {
1196 AVBPrint bp;
1197
1198 26221 av_bprint_init(&bp, 0, INT_MAX);
1199 26221 av_vbprintf(&bp, fmt, ap);
1200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26221 times.
26221 if (!av_bprint_is_complete(&bp)) {
1201 av_bprint_finalize(&bp, NULL);
1202 s->error = AVERROR(ENOMEM);
1203 return AVERROR(ENOMEM);
1204 }
1205 26221 avio_write(s, bp.str, bp.len);
1206 26221 av_bprint_finalize(&bp, NULL);
1207 26221 return bp.len;
1208 }
1209
1210 26221 int avio_printf(AVIOContext *s, const char *fmt, ...)
1211 {
1212 va_list ap;
1213 int ret;
1214
1215 26221 va_start(ap, fmt);
1216 26221 ret = avio_vprintf(s, fmt, ap);
1217 26221 va_end(ap);
1218
1219 26221 return ret;
1220 }
1221
1222 485 void avio_print_string_array(AVIOContext *s, const char *const strings[])
1223 {
1224
2/2
✓ Branch 0 taken 1455 times.
✓ Branch 1 taken 485 times.
1940 for(; *strings; strings++)
1225 1455 avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1226 485 }
1227
1228 int avio_pause(AVIOContext *s, int pause)
1229 {
1230 if (!s->read_pause)
1231 return AVERROR(ENOSYS);
1232 return s->read_pause(s->opaque, pause);
1233 }
1234
1235 315 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1236 int64_t timestamp, int flags)
1237 {
1238 int64_t ret;
1239
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 if (!s->read_seek)
1240 315 return AVERROR(ENOSYS);
1241 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1242 if (ret >= 0) {
1243 int64_t pos;
1244 s->buf_ptr = s->buf_end; // Flush buffer
1245 pos = s->seek(s->opaque, 0, SEEK_CUR);
1246 if (pos >= 0)
1247 s->pos = pos;
1248 else if (pos != AVERROR(ENOSYS))
1249 ret = pos;
1250 }
1251 return ret;
1252 }
1253
1254 77 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1255 {
1256 int ret;
1257 char buf[1024];
1258
2/2
✓ Branch 0 taken 8137 times.
✓ Branch 1 taken 2 times.
8139 while (max_size) {
1259 8137 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1260
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 8062 times.
8137 if (ret == AVERROR_EOF)
1261 75 return 0;
1262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8062 times.
8062 if (ret <= 0)
1263 return ret;
1264 8062 av_bprint_append_data(pb, buf, ret);
1265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8062 times.
8062 if (!av_bprint_is_complete(pb))
1266 return AVERROR(ENOMEM);
1267 8062 max_size -= ret;
1268 }
1269 2 return 0;
1270 }
1271
1272 /* output in a dynamic buffer */
1273
1274 typedef struct DynBuffer {
1275 int pos, size, allocated_size;
1276 uint8_t *buffer;
1277 int io_buffer_size;
1278 uint8_t io_buffer[1];
1279 } DynBuffer;
1280
1281 38047 static int dyn_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1282 {
1283 38047 DynBuffer *d = opaque;
1284 unsigned new_size;
1285
1286 /* reallocate buffer if needed */
1287 38047 new_size = (unsigned)d->pos + buf_size;
1288
2/4
✓ Branch 0 taken 38047 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38047 times.
38047 if (new_size < d->pos || new_size > INT_MAX)
1289 return AVERROR(ERANGE);
1290
2/2
✓ Branch 0 taken 8749 times.
✓ Branch 1 taken 29298 times.
38047 if (new_size > d->allocated_size) {
1291 19463 unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1292
2/2
✓ Branch 0 taken 1965 times.
✓ Branch 1 taken 6784 times.
8749 : new_size;
1293 int err;
1294
2/2
✓ Branch 0 taken 3420 times.
✓ Branch 1 taken 8749 times.
12169 while (new_size > new_allocated_size)
1295 3420 new_allocated_size += new_allocated_size / 2 + 1;
1296
1297
1/2
✓ Branch 0 taken 8749 times.
✗ Branch 1 not taken.
8749 new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1298
1299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8749 times.
8749 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1300 d->allocated_size = 0;
1301 d->size = 0;
1302 return err;
1303 }
1304 8749 d->allocated_size = new_allocated_size;
1305 }
1306 38047 memcpy(d->buffer + d->pos, buf, buf_size);
1307 38047 d->pos = new_size;
1308
2/2
✓ Branch 0 taken 38029 times.
✓ Branch 1 taken 18 times.
38047 if (d->pos > d->size)
1309 38029 d->size = d->pos;
1310 38047 return buf_size;
1311 }
1312
1313 265 static int dyn_packet_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1314 {
1315 unsigned char buf1[4];
1316 int ret;
1317
1318 /* packetized write: output the header */
1319 265 AV_WB32(buf1, buf_size);
1320 265 ret = dyn_buf_write(opaque, buf1, 4);
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 265 times.
265 if (ret < 0)
1322 return ret;
1323
1324 /* then the data */
1325 265 return dyn_buf_write(opaque, buf, buf_size);
1326 }
1327
1328 295 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1329 {
1330 295 DynBuffer *d = opaque;
1331
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (whence == SEEK_CUR)
1333 offset += d->pos;
1334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 else if (whence == SEEK_END)
1335 offset += d->size;
1336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (offset < 0)
1337 return AVERROR(EINVAL);
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (offset > INT_MAX)
1339 return AVERROR(ERANGE);
1340 295 d->pos = offset;
1341 295 return 0;
1342 }
1343
1344 21427 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1345 {
1346 struct { FFIOContext pb; DynBuffer d; } *ret;
1347 DynBuffer *d;
1348
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 21389 times.
21427 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1349
1350 if (sizeof(*ret) + io_buffer_size < io_buffer_size)
1351 return AVERROR(ERANGE);
1352 21427 ret = av_mallocz(sizeof(*ret) + io_buffer_size);
1353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21427 times.
21427 if (!ret)
1354 return AVERROR(ENOMEM);
1355 21427 d = &ret->d;
1356 21427 d->io_buffer_size = io_buffer_size;
1357
4/4
✓ Branch 0 taken 21389 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 21389 times.
21427 ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1358 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1359 max_packet_size ? NULL : dyn_buf_seek);
1360 21427 *s = &ret->pb.pub;
1361 21427 (*s)->max_packet_size = max_packet_size;
1362 21427 return 0;
1363 }
1364
1365 21175 int avio_open_dyn_buf(AVIOContext **s)
1366 {
1367 21175 return url_open_dyn_buf_internal(s, 0);
1368 }
1369
1370 38 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1371 {
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (max_packet_size <= 0)
1373 return AVERROR(EINVAL);
1374 38 return url_open_dyn_buf_internal(s, max_packet_size);
1375 }
1376
1377 25105 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1378 {
1379 DynBuffer *d;
1380
1381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25105 times.
25105 if (!s) {
1382 *pbuffer = NULL;
1383 return 0;
1384 }
1385 25105 d = s->opaque;
1386
1387
3/4
✓ Branch 0 taken 25105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24316 times.
✓ Branch 3 taken 789 times.
25105 if (!s->error && !d->size) {
1388 24316 *pbuffer = d->io_buffer;
1389 24316 return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1390 }
1391
1392 789 avio_flush(s);
1393
1394 789 *pbuffer = d->buffer;
1395
1396 789 return d->size;
1397 }
1398
1399 22009 void ffio_reset_dyn_buf(AVIOContext *s)
1400 {
1401 22009 DynBuffer *d = s->opaque;
1402 22009 int max_packet_size = s->max_packet_size;
1403
1404 22009 ffio_init_context(ffiocontext(s), d->io_buffer, d->io_buffer_size,
1405 1, d, NULL, s->write_packet, s->seek);
1406 22009 s->max_packet_size = max_packet_size;
1407 22009 d->pos = d->size = 0;
1408 22009 }
1409
1410 6680 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1411 {
1412 DynBuffer *d;
1413 int size;
1414 6680 int padding = 0;
1415
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6680 times.
6680 if (!s) {
1417 *pbuffer = NULL;
1418 return 0;
1419 }
1420
1421 /* don't attempt to pad fixed-size packet buffers */
1422
2/2
✓ Branch 0 taken 6644 times.
✓ Branch 1 taken 36 times.
6680 if (!s->max_packet_size) {
1423 6644 ffio_fill(s, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1424 6644 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1425 }
1426
1427 6680 avio_flush(s);
1428
1429 6680 d = s->opaque;
1430 6680 *pbuffer = d->buffer;
1431 6680 size = d->size;
1432
1433 6680 avio_context_free(&s);
1434
1435 6680 return size - padding;
1436 }
1437
1438 15606 void ffio_free_dyn_buf(AVIOContext **s)
1439 {
1440 DynBuffer *d;
1441
1442
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 14533 times.
15606 if (!*s)
1443 1073 return;
1444
1445 14533 d = (*s)->opaque;
1446 14533 av_free(d->buffer);
1447 14533 avio_context_free(s);
1448 }
1449
1450 422 static int null_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1451 {
1452 422 DynBuffer *d = opaque;
1453
1454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 422 times.
422 if ((unsigned)d->pos + (unsigned)buf_size > INT_MAX)
1455 return AVERROR(ERANGE);
1456 422 d->pos += buf_size;
1457
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 138 times.
422 if (d->pos > d->size)
1458 284 d->size = d->pos;
1459 422 return buf_size;
1460 }
1461
1462 214 int ffio_open_null_buf(AVIOContext **s)
1463 {
1464 214 int ret = url_open_dyn_buf_internal(s, 0);
1465
1/2
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
214 if (ret >= 0) {
1466 214 AVIOContext *pb = *s;
1467 214 pb->write_packet = null_buf_write;
1468 }
1469 214 return ret;
1470 }
1471
1472 214 int ffio_close_null_buf(AVIOContext *s)
1473 {
1474 214 DynBuffer *d = s->opaque;
1475 int size;
1476
1477 214 avio_flush(s);
1478
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 size = s->error ? s->error : d->size;
1480
1481 214 avio_context_free(&s);
1482
1483 214 return size;
1484 }
1485