]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/riffdec.c
avformat/riffde: Fix integer overflow in bitrate
[ffmpeg] / libavformat / riffdec.c
index fc6f76ceaf2a5292a0174dc272795a060baa0623..7eecdb24b887a5d2f985ad008dbee268504f4c23 100644 (file)
@@ -2,20 +2,20 @@
  * RIFF demuxing functions and data
  * Copyright (c) 2000 Fabrice Bellard
  *
- * 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
  */
 
 #include "avio_internal.h"
 #include "riff.h"
 
-const AVCodecGuid ff_codec_wav_guids[] = {
-    { AV_CODEC_ID_AC3,      { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
-    { AV_CODEC_ID_ATRAC3P,  { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } },
-    { AV_CODEC_ID_EAC3,     { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
-    { AV_CODEC_ID_MP2,      { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
-    { AV_CODEC_ID_NONE }
-};
+int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
+{
+    int ret;
+    av_assert0(sizeof(*g) == 16); //compiler will optimize this out
+    ret = avio_read(s, *g, sizeof(*g));
+    if (ret < (int)sizeof(*g)) {
+        memset(*g, 0, sizeof(*g));
+        return ret < 0 ? ret : AVERROR_INVALIDDATA;
+    }
+    return 0;
+}
 
 enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
 {
@@ -57,7 +61,10 @@ enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
 static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
 {
     ff_asf_guid subformat;
-    c->bits_per_coded_sample = avio_rl16(pb);
+    int bps = avio_rl16(pb);
+    if (bps)
+        c->bits_per_coded_sample = bps;
+
     c->channel_layout        = avio_rl32(pb); /* dwChannelMask */
 
     ff_get_guid(pb, &subformat);
@@ -75,24 +82,41 @@ static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
     }
 }
 
+/* "big_endian" values are needed for RIFX file format */
 int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
-                      AVCodecContext *codec, int size)
+                      AVCodecContext *codec, int size, int big_endian)
 {
     int id;
+    uint64_t bitrate;
 
-    if (size < 14)
+    if (size < 14) {
+        avpriv_request_sample(codec, "wav header size < 14");
         return AVERROR_INVALIDDATA;
+    }
 
-    id                 = avio_rl16(pb);
     codec->codec_type  = AVMEDIA_TYPE_AUDIO;
-    codec->channels    = avio_rl16(pb);
-    codec->sample_rate = avio_rl32(pb);
-    codec->bit_rate    = avio_rl32(pb) * 8;
-    codec->block_align = avio_rl16(pb);
+    if (!big_endian) {
+        id                 = avio_rl16(pb);
+        codec->channels    = avio_rl16(pb);
+        codec->sample_rate = avio_rl32(pb);
+        bitrate            = avio_rl32(pb) * 8LL;
+        codec->block_align = avio_rl16(pb);
+    } else {
+        id                 = avio_rb16(pb);
+        codec->channels    = avio_rb16(pb);
+        codec->sample_rate = avio_rb32(pb);
+        bitrate            = avio_rb32(pb) * 8LL;
+        codec->block_align = avio_rb16(pb);
+    }
     if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
         codec->bits_per_coded_sample = 8;
-    } else
-        codec->bits_per_coded_sample = avio_rl16(pb);
+    } else {
+        if (!big_endian) {
+            codec->bits_per_coded_sample = avio_rl16(pb);
+        } else {
+            codec->bits_per_coded_sample = avio_rb16(pb);
+        }
+    }
     if (id == 0xFFFE) {
         codec->codec_tag = 0;
     } else {
@@ -102,6 +126,10 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
     }
     if (size >= 18) {  /* We're obviously dealing with WAVEFORMATEX */
         int cbSize = avio_rl16(pb); /* cbSize */
+        if (big_endian) {
+            avpriv_report_missing_feature(codec, "WAVEFORMATEX support for RIFX files\n");
+            return AVERROR_PATCHWELCOME;
+        }
         size  -= 18;
         cbSize = FFMIN(size, cbSize);
         if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
@@ -109,14 +137,10 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
             cbSize -= 22;
             size   -= 22;
         }
-        codec->extradata_size = cbSize;
         if (cbSize > 0) {
-            av_free(codec->extradata);
-            codec->extradata = av_mallocz(codec->extradata_size +
-                                          FF_INPUT_BUFFER_PADDING_SIZE);
-            if (!codec->extradata)
+            av_freep(&codec->extradata);
+            if (ff_get_extradata(codec, pb, cbSize) < 0)
                 return AVERROR(ENOMEM);
-            avio_read(pb, codec->extradata, codec->extradata_size);
             size -= cbSize;
         }
 
@@ -124,6 +148,23 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
         if (size > 0)
             avio_skip(pb, size);
     }
+
+    if (bitrate > INT_MAX) {
+        if (s->error_recognition & AV_EF_EXPLODE) {
+            av_log(s, AV_LOG_ERROR,
+                   "The bitrate %"PRIu64" is too large.\n",
+                    bitrate);
+            return AVERROR_INVALIDDATA;
+        } else {
+            av_log(s, AV_LOG_WARNING,
+                   "The bitrate %"PRIu64" is too large, resetting to 0.",
+                   bitrate);
+            codec->bit_rate = 0;
+        }
+    } else {
+        codec->bit_rate = bitrate;
+    }
+
     if (codec->sample_rate <= 0) {
         av_log(s, AV_LOG_ERROR,
                "Invalid sample rate: %d\n", codec->sample_rate);
@@ -136,7 +177,7 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
         codec->sample_rate = 0;
     }
     /* override bits_per_coded_sample for G.726 */
-    if (codec->codec_id == AV_CODEC_ID_ADPCM_G726)
+    if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
         codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
 
     return 0;
@@ -159,10 +200,11 @@ enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
     return id;
 }
 
-int ff_get_bmp_header(AVIOContext *pb, AVStream *st)
+int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
 {
     int tag1;
-    avio_rl32(pb); /* size */
+    if(esize) *esize  = avio_rl32(pb);
+    else                avio_rl32(pb);
     st->codec->width  = avio_rl32(pb);
     st->codec->height = (int32_t)avio_rl32(pb);
     avio_rl16(pb); /* planes */
@@ -193,12 +235,23 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size)
 
         chunk_code = avio_rl32(pb);
         chunk_size = avio_rl32(pb);
-
+        if (avio_feof(pb)) {
+            if (chunk_code || chunk_size) {
+                av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
+                return AVERROR_INVALIDDATA;
+            }
+            return AVERROR_EOF;
+        }
         if (chunk_size > end ||
             end - chunk_size < cur ||
             chunk_size == UINT_MAX) {
-            av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
-            break;
+            avio_seek(pb, -9, SEEK_CUR);
+            chunk_code = avio_rl32(pb);
+            chunk_size = avio_rl32(pb);
+            if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
+                av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
+                return AVERROR_INVALIDDATA;
+            }
         }
 
         chunk_size += (chunk_size & 1);
@@ -213,7 +266,7 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size)
             continue;
         }
 
-        value = av_malloc(chunk_size + 1);
+        value = av_mallocz(chunk_size + 1);
         if (!value) {
             av_log(s, AV_LOG_ERROR,
                    "out of memory, unable to read INFO tag\n");
@@ -223,14 +276,10 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size)
         AV_WL32(key, chunk_code);
 
         if (avio_read(pb, value, chunk_size) != chunk_size) {
-            av_free(value);
             av_log(s, AV_LOG_WARNING,
                    "premature end of file while reading INFO tag\n");
-            break;
         }
 
-        value[chunk_size] = 0;
-
         av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
     }