]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mms.c
mov: Wrap stsc index and count compare in a separate function
[ffmpeg] / libavformat / mms.c
index 5afa077ccdc563e3adcd9a3588933684b1d1ebf5..fc967c189226eccbdf179b4c347524f722bb8607 100644 (file)
@@ -1,29 +1,31 @@
 /*
  * MMS protocol common definitions.
  * Copyright (c) 2006,2007 Ryan Martell
- * Copyright (c) 2007 Björn Axelsson
+ * Copyright (c) 2007 Björn Axelsson
  * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav 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 FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "mms.h"
 #include "asf.h"
 #include "libavutil/intreadwrite.h"
 
+#define MMS_MAX_STREAMS 256    /**< arbitrary sanity check value */
+
 int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
 {
     char *pos;
@@ -68,7 +70,12 @@ int ff_mms_asf_header_parser(MMSContext *mms)
 
     p += sizeof(ff_asf_guid) + 14;
     while(end - p >= sizeof(ff_asf_guid) + 8) {
-        uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
+        uint64_t chunksize;
+        if (!memcmp(p, ff_asf_data_header, sizeof(ff_asf_guid))) {
+            chunksize = 50; // see Reference [2] section 5.1
+        } else {
+            chunksize = AV_RL64(p + sizeof(ff_asf_guid));
+        }
         if (!chunksize || chunksize > end - p) {
             av_log(NULL, AV_LOG_ERROR,
                    "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
@@ -90,10 +97,15 @@ int ff_mms_asf_header_parser(MMSContext *mms)
             flags     = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24);
             stream_id = flags & 0x7F;
             //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size,
-            //we can calcuate the packet size by stream_num.
+            //we can calculate the packet size by stream_num.
             //Please see function send_stream_selection_request().
-            if (mms->stream_num < MAX_STREAMS &&
+            if (mms->stream_num < MMS_MAX_STREAMS &&
                     46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) {
+                mms->streams = av_fast_realloc(mms->streams,
+                                   &mms->nb_streams_allocated,
+                                   (mms->stream_num + 1) * sizeof(MMSStream));
+                if (!mms->streams)
+                    return AVERROR(ENOMEM);
                 mms->streams[mms->stream_num].id = stream_id;
                 mms->stream_num++;
             } else {
@@ -101,6 +113,34 @@ int ff_mms_asf_header_parser(MMSContext *mms)
                        "Corrupt stream (too many A/V streams)\n");
                 return AVERROR_INVALIDDATA;
             }
+        } else if (!memcmp(p, ff_asf_ext_stream_header, sizeof(ff_asf_guid))) {
+            if (end - p >= 88) {
+                int stream_count = AV_RL16(p + 84), ext_len_count = AV_RL16(p + 86);
+                uint64_t skip_bytes = 88;
+                while (stream_count--) {
+                    if (end - p < skip_bytes + 4) {
+                        av_log(NULL, AV_LOG_ERROR,
+                               "Corrupt stream (next stream name length is not in the buffer)\n");
+                        return AVERROR_INVALIDDATA;
+                    }
+                    skip_bytes += 4 + AV_RL16(p + skip_bytes + 2);
+                }
+                while (ext_len_count--) {
+                    if (end - p < skip_bytes + 22) {
+                        av_log(NULL, AV_LOG_ERROR,
+                               "Corrupt stream (next extension system info length is not in the buffer)\n");
+                        return AVERROR_INVALIDDATA;
+                    }
+                    skip_bytes += 22 + AV_RL32(p + skip_bytes + 18);
+                }
+                if (end - p < skip_bytes) {
+                    av_log(NULL, AV_LOG_ERROR,
+                           "Corrupt stream (the last extension system info length is invalid)\n");
+                    return AVERROR_INVALIDDATA;
+                }
+                if (chunksize - skip_bytes > 24)
+                    chunksize = skip_bytes;
+            }
         } else if (!memcmp(p, ff_asf_head1_guid, sizeof(ff_asf_guid))) {
             chunksize = 46; // see references [2] section 3.4. This should be set 46.
         }