]> granicus.if.org Git - taglib/commitdiff
xiph: preserve any picture data we can't decode in the text comments
authorSander Jansen <s.jansen@gmail.com>
Mon, 18 May 2015 13:24:02 +0000 (08:24 -0500)
committerSander Jansen <s.jansen@gmail.com>
Thu, 12 Nov 2015 14:50:34 +0000 (08:50 -0600)
taglib/ogg/xiphcomment.cpp

index 0424ed5582ffdb74f785dfde4a9a98f9b998479e..8ec108dd978fc3d02e08d00a0767cea9f622ebfb 100644 (file)
@@ -419,51 +419,74 @@ void Ogg::XiphComment::parse(const ByteVector &data)
     // Handle Pictures separately
     if(entry.startsWith("METADATA_BLOCK_PICTURE=")) {
 
-      // Decode base64 picture data
-      ByteVector picturedata = ByteVector::fromBase64(entry.mid(23));
-
-      if(picturedata.size() == 0) {
-        debug("Empty picture data. Discarding content");
-        continue;
+      // We need base64 encoded data including padding
+      if((entry.size() - 23) > 3 && ((entry.size() - 23) % 4) == 0) {
+
+        // Decode base64 picture data
+        ByteVector picturedata = ByteVector::fromBase64(entry.mid(23));
+        if (picturedata.size()) {
+
+          // Decode Flac Picture
+          FLAC::Picture * picture = new FLAC::Picture();
+          if(picture->parse(picturedata)) {
+
+            d->pictureList.append(picture);
+
+            // continue to next field
+            continue;
+          }
+          else {
+            delete picture;
+            debug("Failed to decode FlacPicture block");
+          }
+        }
+        else {
+          debug("Failed to decode base64 encoded data");
+        }
+      }
+      else {
+        debug("Invalid base64 encoded data");
       }
-
-      FLAC::Picture * picture = new FLAC::Picture();
-
-      if(picture->parse(picturedata))
-        d->pictureList.append(picture);
-      else
-        debug("Unable to parse METADATA_BLOCK_PICTURE. Discarding content.");
     }
-    else if (entry.startsWith("COVERART=")) {
 
-      // Decode base64 picture data
-      ByteVector picturedata = ByteVector::fromBase64(entry.mid(9));
+    // Handle old picture standard
+    if (entry.startsWith("COVERART=")) {
 
-      if (picturedata.size() == 0) {
-        debug("Empty coverart data. Discaring content");
-        continue;
-      }
+      if((entry.size() - 9) > 3 && ((entry.size() - 9) % 4) == 0) {
 
-      // Assume it's some type of image file
-      FLAC::Picture * picture = new FLAC::Picture();
-      picture->setData(picturedata);
-      picture->setMimeType("image/");
-      picture->setType(FLAC::Picture::Other);
-      d->pictureList.append(picture);
-    }
-    else {
+        // Decode base64 picture data
+        ByteVector picturedata = ByteVector::fromBase64(entry.mid(9));
+        if (picturedata.size()) {
+
+          // Assume it's some type of image file
+          FLAC::Picture * picture = new FLAC::Picture();
+          picture->setData(picturedata);
+          picture->setMimeType("image/");
+          picture->setType(FLAC::Picture::Other);
+          d->pictureList.append(picture);
 
-      // Check for field separator
-      int sep = entry.find('=');
-      if (sep < 1) {
-        debug("Discarding invalid comment field.");
-        continue;
+          // continue to next field
+          continue;
+        }
+        else {
+          debug("Failed to decode base64 encoded data");
+        }
+      }
+      else {
+        debug("Invalid base64 encoded data");
       }
+    }
 
-      // Parse key and value
-      String key = String(entry.mid(0, sep), String::UTF8);
-      String value = String(entry.mid(sep + 1), String::UTF8);
-      addField(key, value, false);
+    // Check for field separator
+    int sep = entry.find('=');
+    if (sep < 1) {
+      debug("Discarding invalid comment field.");
+      continue;
     }
+
+    // Parse key and value
+    String key = String(entry.mid(0, sep), String::UTF8);
+    String value = String(entry.mid(sep + 1), String::UTF8);
+    addField(key, value, false);
   }
 }