diff options
Diffstat (limited to 'libavcodec/4xm.c')
-rw-r--r-- | libavcodec/4xm.c | 144 |
1 files changed, 100 insertions, 44 deletions
diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c index 094e8518e4..9d2b4f54c6 100644 --- a/libavcodec/4xm.c +++ b/libavcodec/4xm.c @@ -2,20 +2,20 @@ * 4XM codec * Copyright (c) 2003 Michael Niedermayer * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -31,8 +31,7 @@ #include "get_bits.h" #include "internal.h" -//#undef NDEBUG -//#include <assert.h> +#include "libavutil/avassert.h" #define BLOCK_TYPE_VLC_BITS 5 #define ACDC_VLC_BITS 9 @@ -285,7 +284,7 @@ static void init_mv(FourXContext *f) } #endif -static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, +static inline void mcdc(uint16_t *dst, const uint16_t *src, int log2w, int h, int stride, int scale, unsigned dc) { int i; @@ -329,7 +328,7 @@ static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, } break; default: - assert(0); + av_assert2(0); } } @@ -344,10 +343,14 @@ static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, uint16_t *start = (uint16_t *)f->last_picture->data[0]; uint16_t *end = start + stride * (f->avctx->height - h + 1) - (1 << log2w); - assert(code >= 0 && code <= 6); + av_assert2(code >= 0 && code <= 6); if (code == 0) { - src += f->mv[bytestream2_get_byte(&f->g)]; + if (bytestream2_get_bytes_left(&f->g) < 1) { + av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n"); + return; + } + src += f->mv[bytestream2_get_byteu(&f->g)]; if (start > src || src > end) { av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n"); return; @@ -366,21 +369,37 @@ static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, } else if (code == 3 && f->version < 2) { mcdc(dst, src, log2w, h, stride, 1, 0); } else if (code == 4) { - src += f->mv[bytestream2_get_byte(&f->g)]; + if (bytestream2_get_bytes_left(&f->g) < 1) { + av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n"); + return; + } + src += f->mv[bytestream2_get_byteu(&f->g)]; if (start > src || src > end) { av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n"); return; } - mcdc(dst, src, log2w, h, stride, 1, bytestream2_get_le16(&f->g2)); + if (bytestream2_get_bytes_left(&f->g2) < 2){ + av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n"); + return; + } + mcdc(dst, src, log2w, h, stride, 1, bytestream2_get_le16u(&f->g2)); } else if (code == 5) { - mcdc(dst, src, log2w, h, stride, 0, bytestream2_get_le16(&f->g2)); + if (bytestream2_get_bytes_left(&f->g2) < 2) { + av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n"); + return; + } + mcdc(dst, src, log2w, h, stride, 0, bytestream2_get_le16u(&f->g2)); } else if (code == 6) { + if (bytestream2_get_bytes_left(&f->g2) < 4) { + av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n"); + return; + } if (log2w) { - dst[0] = bytestream2_get_le16(&f->g2); - dst[1] = bytestream2_get_le16(&f->g2); + dst[0] = bytestream2_get_le16u(&f->g2); + dst[1] = bytestream2_get_le16u(&f->g2); } else { - dst[0] = bytestream2_get_le16(&f->g2); - dst[stride] = bytestream2_get_le16(&f->g2); + dst[0] = bytestream2_get_le16u(&f->g2); + dst[stride] = bytestream2_get_le16u(&f->g2); } } } @@ -398,6 +417,8 @@ static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length) if (f->version > 1) { extra = 20; + if (length < extra) + return -1; bitstream_size = AV_RL32(buf + 8); wordstream_size = AV_RL32(buf + 12); bytestream_size = AV_RL32(buf + 16); @@ -408,13 +429,12 @@ static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length) bytestream_size = FFMAX(length - bitstream_size - wordstream_size, 0); } - if (bitstream_size + bytestream_size + wordstream_size + extra != length - || bitstream_size > (1 << 26) - || bytestream_size > (1 << 26) - || wordstream_size > (1 << 26)) { - av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", - bitstream_size, bytestream_size, wordstream_size, - bitstream_size + bytestream_size + wordstream_size - length); + if (bitstream_size > length || bitstream_size >= INT_MAX/8 || + bytestream_size > length - bitstream_size || + wordstream_size > length - bytestream_size - bitstream_size || + extra > length - bytestream_size - bitstream_size - wordstream_size) { + av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size, + bitstream_size+ bytestream_size+ wordstream_size - length); return AVERROR_INVALIDDATA; } @@ -455,6 +475,11 @@ static int decode_i_block(FourXContext *f, DCTELEM *block) { int code, i, j, level, val; + if (get_bits_left(&f->gb) < 2){ + av_log(f->avctx, AV_LOG_ERROR, "%d bits left before decode_i_block()\n", get_bits_left(&f->gb)); + return -1; + } + /* DC coef */ val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3); if (val >> 4) @@ -555,7 +580,7 @@ static int decode_i_mb(FourXContext *f) } static const uint8_t *read_huffman_tables(FourXContext *f, - const uint8_t * const buf) + const uint8_t * const buf, int buf_size) { int frequency[512] = { 0 }; uint8_t flag[512]; @@ -564,6 +589,7 @@ static const uint8_t *read_huffman_tables(FourXContext *f, int bits_tab[257]; int start, end; const uint8_t *ptr = buf; + const uint8_t *ptr_end = buf + buf_size; int j; memset(up, -1, sizeof(up)); @@ -573,6 +599,8 @@ static const uint8_t *read_huffman_tables(FourXContext *f, for (;;) { int i; + if (start <= end && ptr_end - ptr < end - start + 1 + 1) + return NULL; for (i = start; i <= end; i++) frequency[i] = *ptr++; start = *ptr++; @@ -655,6 +683,7 @@ static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length) const int mbs = (FFALIGN(width, 16) >> 4) * (FFALIGN(height, 16) >> 4); uint16_t *dst = (uint16_t*)f->current_picture->data[0]; const int stride = f->current_picture->linesize[0]>>1; + const uint8_t *buf_end = buf + length; GetByteContext g3; if (length < mbs * 8) { @@ -666,6 +695,8 @@ static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length) for (y = 0; y < height; y += 16) { for (x = 0; x < width; x += 16) { unsigned int color[4] = { 0 }, bits; + if (buf_end - buf < 8) + return -1; // warning following is purely guessed ... color[0] = bytestream2_get_le16u(&g3); color[1] = bytestream2_get_le16u(&g3); @@ -699,16 +730,14 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length) const int width = f->avctx->width; const int height = f->avctx->height; const unsigned int bitstream_size = AV_RL32(buf); - int token_count av_unused; unsigned int prestream_size; const uint8_t *prestream; - if (length < bitstream_size + 12) { + if (bitstream_size > (1<<26) || length < bitstream_size + 12) { av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n"); return AVERROR_INVALIDDATA; } - token_count = AV_RL32(buf + bitstream_size + 8); prestream_size = 4 * AV_RL32(buf + bitstream_size + 4); prestream = buf + bitstream_size + 12; @@ -720,7 +749,9 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length) return AVERROR_INVALIDDATA; } - prestream = read_huffman_tables(f, prestream); + prestream = read_huffman_tables(f, prestream, buf + length - prestream); + if (!prestream) + return -1; init_get_bits(&f->gb, buf + 4, 8 * bitstream_size); @@ -763,6 +794,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, AVFrame *p; int i, frame_4cc, frame_size, ret; + if (buf_size < 12) + return AVERROR_INVALIDDATA; frame_4cc = AV_RL32(buf); if (buf_size != AV_RL32(buf + 4) + 8 || buf_size < 20) av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", @@ -775,6 +808,16 @@ static int decode_frame(AVCodecContext *avctx, void *data, const int whole_size = AV_RL32(buf + 16); CFrameBuffer *cfrm; + if (data_size < 0 || whole_size < 0) { + av_log(f->avctx, AV_LOG_ERROR, "sizes invalid\n"); + return AVERROR_INVALIDDATA; + } + + if (f->version <= 1) { + av_log(f->avctx, AV_LOG_ERROR, "cfrm in version %d\n", f->version); + return AVERROR_INVALIDDATA; + } + for (i = 0; i < CFRAME_BUFFER_COUNT; i++) if (f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number) av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", @@ -793,11 +836,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, } cfrm = &f->cfrm[i]; + if (data_size > UINT_MAX - cfrm->size - FF_INPUT_BUFFER_PADDING_SIZE) + return AVERROR_INVALIDDATA; + cfrm->data = av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE); // explicit check needed as memcpy below might not catch a NULL if (!cfrm->data) { - av_log(f->avctx, AV_LOG_ERROR, "realloc failure"); + av_log(f->avctx, AV_LOG_ERROR, "realloc failure\n"); return AVERROR(ENOMEM); } @@ -829,36 +875,40 @@ static int decode_frame(AVCodecContext *avctx, void *data, // alternatively we would have to use our own buffer management avctx->flags |= CODEC_FLAG_EMU_EDGE; - if (p->data[0]) - avctx->release_buffer(avctx, p); - - p->reference = 1; - if ((ret = ff_get_buffer(avctx, p)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + p->reference= 3; + if ((ret = avctx->reget_buffer(avctx, p)) < 0) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return ret; } if (frame_4cc == AV_RL32("ifr2")) { - p->pict_type = AV_PICTURE_TYPE_I; - if ((ret = decode_i2_frame(f, buf - 4, frame_size + 4)) < 0) + p->pict_type= AV_PICTURE_TYPE_I; + if ((ret = decode_i2_frame(f, buf - 4, frame_size + 4)) < 0) { + av_log(f->avctx, AV_LOG_ERROR, "decode i2 frame failed\n"); return ret; + } } else if (frame_4cc == AV_RL32("ifrm")) { - p->pict_type = AV_PICTURE_TYPE_I; - if ((ret = decode_i_frame(f, buf, frame_size)) < 0) + p->pict_type= AV_PICTURE_TYPE_I; + if ((ret = decode_i_frame(f, buf, frame_size)) < 0) { + av_log(f->avctx, AV_LOG_ERROR, "decode i frame failed\n"); return ret; + } } else if (frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")) { if (!f->last_picture->data[0]) { - f->last_picture->reference = 1; + f->last_picture->reference = 3; if ((ret = ff_get_buffer(avctx, f->last_picture)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } - memset(f->last_picture->data[0], 0, avctx->height * FFABS(f->last_picture->linesize[0])); + for (i=0; i<avctx->height; i++) + memset(f->last_picture->data[0] + i*f->last_picture->linesize[0], 0, 2*avctx->width); } p->pict_type = AV_PICTURE_TYPE_P; - if ((ret = decode_p_frame(f, buf, frame_size)) < 0) + if ((ret = decode_p_frame(f, buf, frame_size)) < 0) { + av_log(f->avctx, AV_LOG_ERROR, "decode p frame failed\n"); return ret; + } } else if (frame_4cc == AV_RL32("snd_")) { av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size); @@ -883,9 +933,15 @@ static av_cold int decode_init(AVCodecContext *avctx) if (avctx->extradata_size != 4 || !avctx->extradata) { av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n"); - return 1; + return AVERROR_INVALIDDATA; + } + if((avctx->width % 16) || (avctx->height % 16)) { + av_log(avctx, AV_LOG_ERROR, "unsupported width/height\n"); + return AVERROR_INVALIDDATA; } + avcodec_get_frame_defaults(&f->current_picture); + avcodec_get_frame_defaults(&f->last_picture); f->version = AV_RL32(avctx->extradata) >> 16; ff_dsputil_init(&f->dsp, avctx); f->avctx = avctx; |