From: Mikko Rasa Date: Wed, 20 Jun 2018 10:32:30 +0000 (+0300) Subject: Add signature detection for sound files X-Git-Url: http://git.tdb.fi/?p=libs%2Fal.git;a=commitdiff_plain;h=d035e638b940f25cb5d46a049a00b34dc60dc5e0 Add signature detection for sound files --- diff --git a/source/oggdecoder.cpp b/source/oggdecoder.cpp index efea1f6..d287342 100644 --- a/source/oggdecoder.cpp +++ b/source/oggdecoder.cpp @@ -107,6 +107,14 @@ OggDecoder::~OggDecoder() delete priv; } +bool OggDecoder::detect(const std::string &sig) +{ + static const char ogg_sig[] = { 'O', 'g', 'g', 'S' }; + if(sig.size()ovfile, 0); diff --git a/source/oggdecoder.h b/source/oggdecoder.h index 49cadbb..aab8f5c 100644 --- a/source/oggdecoder.h +++ b/source/oggdecoder.h @@ -29,6 +29,8 @@ public: OggDecoder(IO::Seekable &); ~OggDecoder(); + static bool detect(const std::string &); + virtual void rewind(); virtual unsigned read(char *, unsigned); }; diff --git a/source/sounddecoder.cpp b/source/sounddecoder.cpp index 138117f..a2cd51a 100644 --- a/source/sounddecoder.cpp +++ b/source/sounddecoder.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "oggdecoder.h" #include "sounddecoder.h" @@ -29,14 +30,29 @@ SoundDecoder::~SoundDecoder() SoundDecoder *SoundDecoder::open_file(const string &fn) { RefPtr file = new IO::BufferedFile(fn); - SoundDecoder *decoder = new OggDecoder(*file); + SoundDecoder *decoder = open_io(*file); decoder->source = file.release(); return decoder; } SoundDecoder *SoundDecoder::open_io(IO::Seekable &io) { - return new OggDecoder(io); + char sig_buf[8]; + io.read(sig_buf, sizeof(sig_buf)); + string signature(sig_buf, sizeof(sig_buf)); + if(OggDecoder::detect(signature)) + return new OggDecoder(io); + else + { + string sig_hex; + for(unsigned i=0; i(sig_buf[i])); + } + throw unsupported_sound(sig_hex); + } } } // namespace AL