]> granicus.if.org Git - taglib/commitdiff
Combine two internal functions which are always used together.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 20 Jan 2017 16:34:50 +0000 (01:34 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 20 Jan 2017 16:34:50 +0000 (01:34 +0900)
taglib/mpeg/mpegfile.cpp
taglib/mpeg/mpegheader.cpp
taglib/mpeg/mpegutils.h

index 6860053bc9500ee2391b77d1827ceb15a1d812bf..64cae2d31d06e45f711e711eafc759562668670f 100644 (file)
@@ -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<long>(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;
     }
 
index e678f15bf95b8c911a98b2654de41fc92b9ede40..610b0320a6360defb0ced9c57df52a0048e7309a 100644 (file)
@@ -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;
   }
index e35f752fb28be14f95bbab8f72586f452a6a9203..1cee918a6cddc78f3d3832adc2770c0a4484b031 100644 (file)
@@ -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);
-      }
-
-      inline bool secondSynchByte(unsigned char byte)
-      {
-        // 0xFF is possible in theory, but it's very unlikely be a header.
+        // 0xFF in the second byte is possible in theory, but it's very unlikely.
 
-        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);
       }
 
     }