]> git.tdb.fi Git - libs/al.git/commitdiff
Add signature detection for sound files
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Jun 2018 10:32:30 +0000 (13:32 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Jun 2018 10:32:30 +0000 (13:32 +0300)
source/oggdecoder.cpp
source/oggdecoder.h
source/sounddecoder.cpp

index efea1f6200874542438e6187b50294437f395e42..d28734208d605e2c912444fe662fb3c72c6c5413 100644 (file)
@@ -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()<sizeof(ogg_sig))
+               return false;
+       return !sig.compare(0, sizeof(ogg_sig), ogg_sig);
+}
+
 void OggDecoder::rewind()
 {
        ov_pcm_seek(&priv->ovfile, 0);
index 49cadbb1bea56f3c29b40cd59f4b5f117ca9728f..aab8f5c8da6b06c7b66049e90d1687e2fe4ee4bc 100644 (file)
@@ -29,6 +29,8 @@ public:
        OggDecoder(IO::Seekable &);
        ~OggDecoder();
 
+       static bool detect(const std::string &);
+
        virtual void rewind();
        virtual unsigned read(char *, unsigned);
 };
index 138117f730a9210545b807a0338bd8887861fa1d..a2cd51af4624154cca52312157cfb18f31a9b21e 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/refptr.h>
 #include <msp/io/file.h>
+#include <msp/strings/format.h>
 #include "oggdecoder.h"
 #include "sounddecoder.h"
 
@@ -29,14 +30,29 @@ SoundDecoder::~SoundDecoder()
 SoundDecoder *SoundDecoder::open_file(const string &fn)
 {
        RefPtr<IO::BufferedFile> 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<sizeof(sig_buf); ++i)
+               {
+                       if(i)
+                               sig_hex += ' ';
+                       sig_hex += Msp::format("%02X", static_cast<unsigned char>(sig_buf[i]));
+               }
+               throw unsupported_sound(sig_hex);
+       }
 }
 
 } // namespace AL