whitespace cosmetics: Remove all trailing whitespace.
[mplayer.git] / libmpdemux / demux_vqf.c
index b7f071a..4ec8865 100644 (file)
@@ -1,8 +1,27 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
 #include "config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
 #include "libavutil/common.h"
+#include "libavutil/intreadwrite.h"
 #include "mpbswap.h"
 
 #include "stream/stream.h"
@@ -10,7 +29,7 @@
 #include "stheader.h"
 #include "libmpcodecs/vqf.h"
 
-static int demux_probe_vqf(demuxer_t* demuxer) 
+static int demux_probe_vqf(demuxer_t* demuxer)
 {
   char buf[KEYWORD_BYTES];
   stream_t *s;
@@ -50,19 +69,22 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
     unsigned chunk_size;
     hi->size=chunk_size=stream_read_dword(s); /* include itself */
     stream_read(s,chunk_id,4);
-    if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('C','O','M','M'))
+    if (chunk_size < 8) return NULL;
+    chunk_size -= 8;
+    if(AV_RL32(chunk_id)==mmioFOURCC('C','O','M','M'))
     {
-    char buf[chunk_size-8];
+    char buf[BUFSIZ];
     unsigned i,subchunk_size;
-    if(stream_read(s,buf,chunk_size-8)!=chunk_size-8) return NULL;
+    if (chunk_size > sizeof(buf) || chunk_size < 20) return NULL;
+    if(stream_read(s,buf,chunk_size)!=chunk_size) return NULL;
     i=0;
-    subchunk_size=be2me_32(*((uint32_t *)&buf[0]));
-    hi->channelMode=be2me_32(*((uint32_t *)&buf[4]));
+    subchunk_size      = AV_RB32(buf);
+    hi->channelMode    = AV_RB32(buf + 4);
     w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
-    hi->bitRate=be2me_32(*((uint32_t *)&buf[8]));
+    hi->bitRate        = AV_RB32(buf + 8);
     sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
     w->nAvgBytesPerSec = sh_audio->i_bps;
-    hi->samplingRate=be2me_32(*((uint32_t *)&buf[12]));
+    hi->samplingRate   = AV_RB32(buf + 12);
     switch(hi->samplingRate){
     case 44:
         w->nSamplesPerSec=44100;
@@ -78,21 +100,23 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
         break;
     }
     sh_audio->samplerate=w->nSamplesPerSec;
-    hi->securityLevel=be2me_32(*((uint32_t *)&buf[16]));
+    hi->securityLevel  = AV_RB32(buf + 16);
     w->nBlockAlign = 0;
     sh_audio->samplesize = 4;
     w->wBitsPerSample = 8*sh_audio->samplesize;
     w->cbSize = 0;
+    if (subchunk_size > chunk_size - 4) continue;
     i+=subchunk_size+4;
-    while(i<chunk_size-8)
+    while(i + 8 < chunk_size)
     {
         unsigned slen,sid;
-        char sdata[chunk_size];
-        sid=*((uint32_t *)&buf[i]); i+=4;
-        slen=be2me_32(*((uint32_t *)&buf[i])); i+=4;
+        char sdata[BUFSIZ];
+        sid  = AV_RL32(buf + i); i+=4;
+        slen = AV_RB32(buf + i); i+=4;
+        if (slen > sizeof(sdata) - 1 || slen > chunk_size - i) break;
         if(sid==mmioFOURCC('D','S','I','Z'))
         {
-        hi->Dsiz=be2me_32(*((uint32_t *)&buf[i]));
+        hi->Dsiz=AV_RB32(buf + i);
         continue; /* describes the same info as size of DATA chunk */
         }
         memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
@@ -138,21 +162,22 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
     }
     }
     else
-    if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('D','A','T','A'))
+    if(AV_RL32(chunk_id)==mmioFOURCC('D','A','T','A'))
     {
     demuxer->movi_start=stream_tell(s);
-    demuxer->movi_end=demuxer->movi_start+chunk_size-8;
+    demuxer->movi_end=demuxer->movi_start+chunk_size;
     mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
     /* Done! play it */
     break;
     }
     else
     {
-    mp_msg(MSGT_DEMUX, MSGL_V, "Unhandled chunk '%c%c%c%c' %u bytes\n",((char *)&chunk_id)[0],((char *)&chunk_id)[1],((char *)&chunk_id)[2],((char *)&chunk_id)[3],chunk_size);
-    stream_skip(s,chunk_size-8); /*unknown chunk type */
+    mp_msg(MSGT_DEMUX, MSGL_V, "Unhandled chunk '%c%c%c%c' %u bytes\n",chunk_id[0],chunk_id[1],chunk_id[2],chunk_id[3],chunk_size);
+    stream_skip(s,chunk_size); /*unknown chunk type */
     }
   }
 
+  demuxer->audio->id = 0;
   demuxer->audio->sh = sh_audio;
   sh_audio->ds = demuxer->audio;
   stream_seek(s,demuxer->movi_start);
@@ -186,8 +211,8 @@ static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_de
   sh_audio_t* sh_audio = demuxer->audio->sh;
   off_t base,pos;
 
-  base = (flags & 1) ? demuxer->movi_start : stream_tell(s);
-  if(flags & 2)
+  base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
+  if(flags & SEEK_FACTOR)
     pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
   else
     pos = base + (rel_seek_secs*sh_audio->i_bps);
@@ -200,7 +225,7 @@ static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_de
 static void demux_close_vqf(demuxer_t* demuxer) {}
 
 
-demuxer_desc_t demuxer_desc_vqf = {
+const demuxer_desc_t demuxer_desc_vqf = {
   "TwinVQ demuxer",
   "vqf",
   "VQF",