diff --git a/taglib/mpeg/mpegfile.cpp b/taglib/mpeg/mpegfile.cpp index 6860053b..64cae2d3 100644 --- a/taglib/mpeg/mpegfile.cpp +++ b/taglib/mpeg/mpegfile.cpp @@ -346,7 +346,7 @@ void MPEG::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory) long MPEG::File::nextFrameOffset(long position) { - char frameSyncBytes[2] = {}; + ByteVector frameSyncBytes(2, '\0'); while(true) { seek(position); @@ -357,7 +357,7 @@ long MPEG::File::nextFrameOffset(long position) for(unsigned int i = 0; i < buffer.size(); ++i) { frameSyncBytes[0] = frameSyncBytes[1]; frameSyncBytes[1] = buffer[i]; - if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) { + if(isFrameSync(frameSyncBytes)) { Header header(this, position + i - 1, true); if(header.isValid()) return position + i - 1; @@ -370,7 +370,7 @@ long MPEG::File::nextFrameOffset(long position) long MPEG::File::previousFrameOffset(long position) { - char frameSyncBytes[2] = {}; + ByteVector frameSyncBytes(2, '\0'); while(position > 0) { const long size = std::min(position, bufferSize()); @@ -384,7 +384,7 @@ long MPEG::File::previousFrameOffset(long position) for(int i = buffer.size() - 1; i >= 0; i--) { frameSyncBytes[1] = frameSyncBytes[0]; frameSyncBytes[0] = buffer[i]; - if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) { + if(isFrameSync(frameSyncBytes)) { Header header(this, position + i, true); if(header.isValid()) return position + i + header.frameLength(); @@ -494,8 +494,8 @@ long MPEG::File::findID3v2() // Look for an ID3v2 tag until reaching the first valid MPEG frame. - char frameSyncBytes[2] = {}; - char tagHeaderBytes[4] = {}; + ByteVector frameSyncBytes(2, '\0'); + ByteVector tagHeaderBytes(3, '\0'); long position = 0; while(true) { @@ -507,7 +507,7 @@ long MPEG::File::findID3v2() for(unsigned int i = 0; i < buffer.size(); ++i) { frameSyncBytes[0] = frameSyncBytes[1]; frameSyncBytes[1] = buffer[i]; - if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) { + if(isFrameSync(frameSyncBytes)) { Header header(this, position + i - 1, true); if(header.isValid()) return -1; @@ -516,7 +516,7 @@ long MPEG::File::findID3v2() tagHeaderBytes[0] = tagHeaderBytes[1]; tagHeaderBytes[1] = tagHeaderBytes[2]; tagHeaderBytes[2] = buffer[i]; - if(headerID == tagHeaderBytes) + if(tagHeaderBytes == headerID) return position + i - 2; } diff --git a/taglib/mpeg/mpegheader.cpp b/taglib/mpeg/mpegheader.cpp index e678f15b..610b0320 100644 --- a/taglib/mpeg/mpegheader.cpp +++ b/taglib/mpeg/mpegheader.cpp @@ -182,7 +182,7 @@ void MPEG::Header::parse(File *file, long offset, bool checkLength) // Check for the MPEG synch bytes. - if(!firstSyncByte(data[0]) || !secondSynchByte(data[1])) { + if(!isFrameSync(data)) { debug("MPEG::Header::parse() -- MPEG header did not match MPEG synch."); return; } diff --git a/taglib/mpeg/mpegutils.h b/taglib/mpeg/mpegutils.h index e35f752f..1cee918a 100644 --- a/taglib/mpeg/mpegutils.h +++ b/taglib/mpeg/mpegutils.h @@ -41,17 +41,17 @@ namespace TagLib * MPEG frames can be recognized by the bit pattern 11111111 111, so the * first byte is easy to check for, however checking to see if the second byte * starts with \e 111 is a bit more tricky, hence these functions. + * + * \note This does not check the length of the vector, since this is an + * internal utility function. */ - inline bool firstSyncByte(unsigned char byte) + inline bool isFrameSync(const ByteVector &bytes) { - return (byte == 0xFF); - } + // 0xFF in the second byte is possible in theory, but it's very unlikely. - inline bool secondSynchByte(unsigned char byte) - { - // 0xFF is possible in theory, but it's very unlikely be a header. - - return (byte != 0xFF && ((byte & 0xE0) == 0xE0)); + const unsigned char b1 = bytes[0]; + const unsigned char b2 = bytes[1]; + return (b1 == 0xFF && b2 != 0xFF && (b2 & 0xE0) == 0xE0); } }