1 /*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 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 <string.h>
23
32
33 #if FF_API_DESTRUCT_PACKET
34
36 {
39 }
40
41 /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
42 * NULL => static data */
44 {
46 }
47 #endif
48
50 {
58 #if FF_API_DESTRUCT_PACKET
62 #endif
66 }
67
69 {
73
75 if (ret < 0)
77
79
80 return 0;
81 }
82
84 {
87 if (ret < 0)
89
94 #if FF_API_DESTRUCT_PACKET
98 #endif
99
100 return 0;
101 }
102
104 {
105 if (pkt->
size <= size)
106 return;
109 }
110
112 {
113 int new_size;
117 if ((unsigned)grow_by >
119 return -1;
120
124 if (ret < 0)
126 } else {
131 #if FF_API_DESTRUCT_PACKET
135 #endif
136 }
138 pkt->
size += grow_by;
139 memset(pkt->
data + pkt->
size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
140
141 return 0;
142 }
143
145 {
148
153
156 #if FF_API_DESTRUCT_PACKET
160 #endif
161
162 return 0;
163 }
164
165 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
166 #define ALLOC_BUF(data, size) \
167 do { \
168 av_buffer_realloc(&pkt->buf, size); \
169 data = pkt->buf ? pkt->buf->data : NULL; \
170 } while (0)
171
172 #define DUP_DATA(dst, src, size, padding, ALLOC) \
173 do { \
174 void *data; \
175 if (padding) { \
176 if ((unsigned)(size) > \
177 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
178 goto failed_alloc; \
179 ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
180 } else { \
181 ALLOC(data, size); \
182 } \
183 if (!data) \
184 goto failed_alloc; \
185 memcpy(data, src, size); \
186 if (padding) \
187 memset((uint8_t *)data + size, 0, \
188 FF_INPUT_BUFFER_PADDING_SIZE); \
189 dst = data; \
190 } while (0)
191
192 /* Makes duplicates of data, side_data, but does not copy any other fields */
194 {
199 if (!ref)
203 } else {
205 }
206 #if FF_API_DESTRUCT_PACKET
210 #endif
215 }
216 return 0;
217
218 failed_alloc:
221 }
222
224 {
226 int i;
229 if (src != pkt) {
232 }
238 }
239 }
241 return 0;
242
243 failed_alloc:
246 }
247
249 {
251
256 #endif
257 ) {
261 }
262 return 0;
263 }
264
266 {
269 }
270
272 {
273 int i;
278 }
279
281 {
282 if (pkt) {
286 #if FF_API_DESTRUCT_PACKET
290 #endif
294
296 }
297 }
298
301 {
303
304 if ((
unsigned)elems + 1 > INT_MAX /
sizeof(*pkt->
side_data))
308
313
320
322 }
323
326 {
327 int i;
328
331 if (size)
334 }
335 }
337 }
338
339 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
340
344 int i;
350 }
351 if (size > INT_MAX)
354 if (!buf)
358 #if FF_API_DESTRUCT_PACKET
362 #endif
369 }
372 memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
376 return 1;
377 }
378 return 0;
379 }
380
383 int i;
386
388 for (i=1; ; i++){
390 if (size>INT_MAX || p - pkt->
data < size)
391 return 0;
392 if (p[4]&128)
393 break;
394 p-= size+5;
395 }
396
400
402 for (i=0; ; i++){
411 pkt->
size -= size + 5;
412 if(p[4]&128)
413 break;
414 p-= size+5;
415 }
418 return 1;
419 }
420 return 0;
421 }
422
424 {
427 *size = 0;
428
429 if (!dict)
431
433 const size_t keylen = strlen(t->
key);
434 const size_t valuelen = strlen(t->
value);
435 const size_t new_size = *size + keylen + 1 + valuelen + 1;
437
438 if (!new_data)
439 goto fail;
440 data = new_data;
441 if (new_size > INT_MAX)
442 goto fail;
443
444 memcpy(data + *size, t->
key, keylen + 1);
445 memcpy(data + *size + keylen + 1, t->
value, valuelen + 1);
446
447 *size = new_size;
448 }
449
451
452 fail:
454 *size = 0;
456 }
457
459 {
462
463 if (!dict || !data || !size)
465 if (size && end[-1])
467 while (data < end) {
470
471 if (val >= end)
473
475 if (ret < 0)
476 break;
477 data = val + strlen(val) + 1;
478 }
479
481 }
482
485 {
486 int i;
487
493 return 0;
494 }
495 }
497 }
498
500 {
501 int i;
502
510
516
517 if (!dst_data) {
520 }
521 memcpy(dst_data, src_data, size);
522 }
523
524 return 0;
525 }
526
528 {
534 }
535
537 {
539
541 if (ret < 0)
543
546 if (ret < 0)
547 goto fail;
549 } else {
553 goto fail;
554 }
555 }
556
559 return 0;
560 fail:
563 }
564
566 {
569 }
570
572 {
581 }