2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "libavutil/avassert.h"
22 #include "libavcodec/avcodec.h"
23 #include "libavcodec/bytestream.h"
24 #include "libavformat/avformat.h"
27 typedef struct IOContext {
34 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
36 static void error(const char *err)
38 fprintf(stderr, "%s", err);
42 static int io_read(void *opaque, uint8_t *buf, int buf_size)
44 IOContext *c = opaque;
45 int size = FFMIN(buf_size, c->fuzz_size);
48 c->filesize = FFMIN(c->pos, c->filesize);
52 memcpy(buf, c->fuzz, size);
56 c->filesize = FFMAX(c->filesize, c->pos);
61 static int64_t io_seek(void *opaque, int64_t offset, int whence)
63 IOContext *c = opaque;
65 if (whence == SEEK_CUR) {
66 if (offset > INT64_MAX - c->pos)
69 } else if (whence == SEEK_END) {
70 if (offset > INT64_MAX - c->filesize)
72 offset += c->filesize;
73 } else if (whence == AVSEEK_SIZE) {
76 if (offset < 0 || offset > c->filesize)
82 // Ensure we don't loop forever
83 const uint32_t maxiteration = 8096;
85 static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
87 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
88 const uint64_t fuzz_tag = FUZZ_TAG;
90 AVFormatContext *avfmt = avformat_alloc_context();
92 char filename[1025] = {0};
93 AVIOContext *fuzzed_pb = NULL;
95 int io_buffer_size = 32768;
96 int64_t filesize = size;
104 avcodec_register_all();
105 av_log_set_level(AV_LOG_PANIC);
110 error("Failed avformat_alloc_context()");
114 memcpy (filename, data + size - 1024, 1024);
115 bytestream2_init(&gbc, data + size - 2048, 1024);
118 io_buffer_size = bytestream2_get_le32(&gbc) & 0xFFFFFFF;
119 seekable = bytestream2_get_byte(&gbc) & 1;
120 filesize = bytestream2_get_le64(&gbc) & 0x7FFFFFFFFFFFFFFF;
122 io_buffer = av_malloc(io_buffer_size);
124 error("Failed to allocate io_buffer");
126 opaque.filesize = filesize;
129 opaque.fuzz_size= size;
130 fuzzed_pb = avio_alloc_context(io_buffer, io_buffer_size, 0, &opaque,
131 io_read, NULL, seekable ? io_seek : NULL);
133 error("avio_alloc_context failed");
135 avfmt->pb = fuzzed_pb;
137 ret = avformat_open_input(&avfmt, filename, NULL, NULL);
139 av_freep(&fuzzed_pb->buffer);
140 av_freep(&fuzzed_pb);
141 avformat_free_context(avfmt);
145 ret = avformat_find_stream_info(avfmt, NULL);
147 av_init_packet(&pkt);
151 for(it = 0; it < maxiteration; it++) {
152 ret = av_read_frame(avfmt, &pkt);
155 av_packet_unref(&pkt);
158 av_freep(&fuzzed_pb->buffer);
159 av_freep(&fuzzed_pb);
160 avformat_close_input(&avfmt);