From 6651858affd34318b729d89aabc14ca39328c8d6 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Sun, 22 Apr 2012 11:02:08 +0000 Subject: [PATCH] snd_mp3.c, snd_vorbis.c, snd_wave.c: Make _sure_ that the number of channels in the music file is supported, i.e. either stereo or mono. Minor tidy up of the error return paths. git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@661 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/snd_mp3.c | 16 ++++++++++++++-- Quake/snd_mpg123.c | 2 +- Quake/snd_vorbis.c | 32 +++++++++++++++++++------------- Quake/snd_wave.c | 13 +++++++++---- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Quake/snd_mp3.c b/Quake/snd_mp3.c index 07d81c6b..27c84f6c 100644 --- a/Quake/snd_mp3.c +++ b/Quake/snd_mp3.c @@ -499,6 +499,7 @@ static void S_MP3_CodecShutdown (void) static snd_stream_t *S_MP3_CodecOpenStream (const char *filename) { snd_stream_t *stream; + int err; stream = S_CodecUtilOpen(filename, &mp3_codec); if (!stream) @@ -516,10 +517,21 @@ static snd_stream_t *S_MP3_CodecOpenStream (const char *filename) } #endif - if (mp3_startread(stream) == 0) + err = mp3_startread(stream); + if (err != 0) + { + Con_Printf("%s is not a valid mp3 file\n", filename); + } + else if (stream->info.channels != 1 && stream->info.channels != 2) + { + Con_Printf("Unsupported number of channels %d in %s\n", + stream->info.channels, filename); + } + else + { return stream; + } - Con_Printf("%s is not a valid mp3 file\n", filename); #if 0 /*defined(CODECS_USE_ZONE)*/ Z_Free(stream->priv); #else diff --git a/Quake/snd_mpg123.c b/Quake/snd_mpg123.c index 4ac75160..9a4d6896 100644 --- a/Quake/snd_mpg123.c +++ b/Quake/snd_mpg123.c @@ -132,7 +132,7 @@ static snd_stream_t *S_MP3_CodecOpenStream (const char *filename) stream->info.channels = 2; break; default: - Con_Printf("Unsupported channel count in %s\n", filename); + Con_Printf("Unsupported number of channels %d in %s\n", channels, filename); goto _fail; } diff --git a/Quake/snd_vorbis.c b/Quake/snd_vorbis.c index e445dd45..cd33f2c8 100644 --- a/Quake/snd_vorbis.c +++ b/Quake/snd_vorbis.c @@ -85,12 +85,11 @@ static snd_stream_t *S_OGG_CodecOpenStream (const char *filename) ovFile = (OggVorbis_File *) Z_Malloc(sizeof(OggVorbis_File)); res = ov_open_callbacks(&stream->fh, ovFile, NULL, 0, ovc_qfs); - if (res < 0) + if (res != 0) { - Con_Printf("%s is not a valid Ogg Vorbis file (error %i).\n", filename, res); - Z_Free(ovFile); - S_CodecUtilClose(&stream); - return NULL; + Con_Printf("%s is not a valid Ogg Vorbis file (error %i).\n", + filename, res); + goto _fail; } stream->priv = ovFile; @@ -98,20 +97,21 @@ static snd_stream_t *S_OGG_CodecOpenStream (const char *filename) if (!ov_seekable(ovFile)) { Con_Printf("OGG_Open: stream %s not seekable.\n", filename); - ov_clear(ovFile); - Z_Free(ovFile); - S_CodecUtilClose(&stream); - return NULL; + goto _fail; } ogg_info = ov_info(ovFile, 0); if (!ogg_info) { Con_Printf("Unable to get stream information for %s.\n", filename); - ov_clear(ovFile); - Z_Free(ovFile); - S_CodecUtilClose(&stream); - return NULL; + goto _fail; + } + + if (ogg_info->channels != 1 && ogg_info->channels != 2) + { + Con_Printf("Unsupported number of channels %d in %s\n", + ogg_info->channels, filename); + goto _fail; } stream->info.rate = ogg_info->rate; @@ -119,6 +119,12 @@ static snd_stream_t *S_OGG_CodecOpenStream (const char *filename) stream->info.width = OGG_SAMPLEWIDTH; return stream; +_fail: + if (res == 0) + ov_clear(ovFile); + Z_Free(ovFile); + S_CodecUtilClose(&stream); + return NULL; } static int S_OGG_CodecReadStream (snd_stream_t *stream, int bytes, void *buffer) diff --git a/Quake/snd_wave.c b/Quake/snd_wave.c index 5db35918..3d3e781e 100644 --- a/Quake/snd_wave.c +++ b/Quake/snd_wave.c @@ -203,20 +203,25 @@ snd_stream_t *S_WAV_CodecOpenStream(const char *filename) * for the FS_*() functions: We will manipulate the * file by ourselves from now on. */ if (!WAV_ReadRIFFHeader(filename, stream->fh.file, &stream->info)) + goto _fail; + if (stream->info.channels != 1 && stream->info.channels != 2) { - S_CodecUtilClose(&stream); - return NULL; + Con_Printf("Unsupported number of channels %d in %s\n", + stream->info.channels, filename); + goto _fail; } stream->fh.start = ftell(stream->fh.file); /* reset to data position */ if (stream->fh.start - start + stream->info.size > stream->fh.length) { Con_Printf("%s data size mismatch\n", filename); - S_CodecUtilClose(&stream); - return NULL; + goto _fail; } return stream; +_fail: + S_CodecUtilClose(&stream); + return NULL; } /*