]> granicus.if.org Git - taglib/commitdiff
Separate zlib related code rather than having several #ifdef blocks.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 21 Feb 2016 14:16:48 +0000 (23:16 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 21 Feb 2016 14:16:48 +0000 (23:16 +0900)
taglib/CMakeLists.txt
taglib/mpeg/id3v2/id3v2frame.cpp
taglib/mpeg/id3v2/id3v2framefactory.cpp
taglib/toolkit/tzlib.cpp [new file with mode: 0644]
taglib/toolkit/tzlib.h [new file with mode: 0644]
tests/test_id3v2.cpp

index c30929fc97967730e3b84e208e67d5e745af8ae1..f248d0d1fbd152f73a8fd21bb3b7cb47dbc84fb9 100644 (file)
@@ -309,6 +309,7 @@ set(toolkit_SRCS
   toolkit/tpropertymap.cpp
   toolkit/trefcounter.cpp
   toolkit/tdebuglistener.cpp
+  toolkit/tzlib.cpp
 )
 
 if(NOT WIN32)
index a9d48f71a381b4778402ac2bcd53dd8dfac27077..d065f2ae32be473119b4e6aca378429bf350e8b5 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#if HAVE_ZLIB
-#include <zlib.h>
-#endif
-
 #include <bitset>
 
 #include <tdebug.h>
 #include <tstringlist.h>
+#include <tzlib.h>
 
 #include "id3v2tag.h"
 #include "id3v2frame.h"
 #include "id3v2synchdata.h"
+
 #include "tpropertymap.h"
 #include "frames/textidentificationframe.h"
 #include "frames/urllinkframe.h"
@@ -251,60 +245,21 @@ ByteVector Frame::fieldData(const ByteVector &frameData) const
     frameDataOffset += 4;
   }
 
-#if HAVE_ZLIB
-  if(d->header->compression() &&
-     !d->header->encryption())
-  {
+  if(zlib::isAvailable() && d->header->compression() && !d->header->encryption()) {
     if(frameData.size() <= frameDataOffset) {
       debug("Compressed frame doesn't have enough data to decode");
       return ByteVector();
     }
 
-    z_stream stream = {};
-
-    if(inflateInit(&stream) != Z_OK)
-      return ByteVector();
-
-    ByteVector inData = frameData;
-
-    stream.avail_in = static_cast<uInt>(inData.size() - frameDataOffset);
-    stream.next_in = reinterpret_cast<Bytef *>(inData.data() + frameDataOffset);
-
-    static const unsigned int chunkSize = 1024;
-
-    ByteVector outData;
-    ByteVector chunk(chunkSize);
-
-    do {
-      stream.avail_out = static_cast<uInt>(chunk.size());
-      stream.next_out = reinterpret_cast<Bytef *>(chunk.data());
-
-      int result = inflate(&stream, Z_NO_FLUSH);
-
-      if(result == Z_STREAM_ERROR ||
-         result == Z_NEED_DICT ||
-         result == Z_DATA_ERROR ||
-         result == Z_MEM_ERROR)
-      {
-        if(result != Z_STREAM_ERROR)
-          inflateEnd(&stream);
-        debug("Error reading compressed stream");
-        return ByteVector();
-      }
-
-      outData.append(stream.avail_out == 0 ? chunk : chunk.mid(0, chunk.size() - stream.avail_out));
-    } while(stream.avail_out == 0);
-
-    inflateEnd(&stream);
-
-    if(frameDataLength != outData.size())
+    const ByteVector outData = zlib::decompress(frameData.mid(frameDataOffset));
+    if(frameDataLength != outData.size()) {
       debug("frameDataLength does not match the data length returned by zlib");
+    }
 
     return outData;
   }
-  else
-#endif
-    return frameData.mid(frameDataOffset, frameDataLength);
+
+  return frameData.mid(frameDataOffset, frameDataLength);
 }
 
 String Frame::readStringField(const ByteVector &data, String::Type encoding, int *position)
index cfa6be234eb23e518c49cac43b52d81f2f7a7f07..bbeb56f0b6b3d66ed6426385e8b8c5e919efa141 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <tdebug.h>
+#include <tzlib.h>
 
 #include "id3v2framefactory.h"
 #include "id3v2synchdata.h"
@@ -174,12 +171,11 @@ Frame *FrameFactory::createFrame(const ByteVector &origData, Header *tagHeader)
   // TagLib doesn't mess with encrypted frames, so just treat them
   // as unknown frames.
 
-#if !defined(HAVE_ZLIB) || HAVE_ZLIB == 0
-  if(header->compression()) {
+  if(!zlib::isAvailable() && header->compression()) {
     debug("Compressed frames are currently not supported.");
     return new UnknownFrame(data, header);
   }
-#endif
+
   if(header->encryption()) {
     debug("Encrypted frames are currently not supported.");
     return new UnknownFrame(data, header);
diff --git a/taglib/toolkit/tzlib.cpp b/taglib/toolkit/tzlib.cpp
new file mode 100644 (file)
index 0000000..7198b63
--- /dev/null
@@ -0,0 +1,107 @@
+/***************************************************************************
+    copyright            : (C) 2016 by Tsuda Kageyu
+    email                : tsuda.kageyu@gmail.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful, but   *
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
+ *   02110-1301  USA                                                       *
+ *                                                                         *
+ *   Alternatively, this file is available under the Mozilla Public        *
+ *   License Version 1.1.  You may obtain a copy of the License at         *
+ *   http://www.mozilla.org/MPL/                                           *
+ ***************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef HAVE_ZLIB
+# include <zlib.h>
+#endif
+
+#include <tstring.h>
+#include <tdebug.h>
+
+#include "tzlib.h"
+
+using namespace TagLib;
+
+bool zlib::isAvailable()
+{
+#ifdef HAVE_ZLIB
+
+  return true;
+
+#else
+
+  return false;
+
+#endif
+}
+
+ByteVector zlib::decompress(const ByteVector &data)
+{
+#ifdef HAVE_ZLIB
+
+    z_stream stream = {};
+
+    if(inflateInit(&stream) != Z_OK) {
+      debug("zlib::decompress() - Failed to initizlize zlib.");
+      return ByteVector();
+    }
+
+    ByteVector inData = data;
+
+    stream.avail_in = static_cast<uInt>(inData.size());
+    stream.next_in  = reinterpret_cast<Bytef *>(inData.data());
+
+    const unsigned int chunkSize = 1024;
+
+    ByteVector outData;
+
+    do {
+      const size_t offset = outData.size();
+      outData.resize(outData.size() + chunkSize);
+
+      stream.avail_out = static_cast<uInt>(chunkSize);
+      stream.next_out  = reinterpret_cast<Bytef *>(outData.data() + offset);
+
+      const int result = inflate(&stream, Z_NO_FLUSH);
+
+      if(result == Z_STREAM_ERROR ||
+         result == Z_NEED_DICT ||
+         result == Z_DATA_ERROR ||
+         result == Z_MEM_ERROR)
+      {
+        if(result != Z_STREAM_ERROR)
+          inflateEnd(&stream);
+
+        debug("zlib::decompress() - Error reading compressed stream.");
+        return ByteVector();
+      }
+
+      outData.resize(outData.size() - stream.avail_out);
+    } while(stream.avail_out == 0);
+
+    inflateEnd(&stream);
+
+    return outData;
+
+#else
+
+  return ByteVector();
+
+#endif
+}
diff --git a/taglib/toolkit/tzlib.h b/taglib/toolkit/tzlib.h
new file mode 100644 (file)
index 0000000..b1f1fca
--- /dev/null
@@ -0,0 +1,54 @@
+/***************************************************************************
+    copyright            : (C) 2016 by Tsuda Kageyu
+    email                : tsuda.kageyu@gmail.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *   This library is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU Lesser General Public License version   *
+ *   2.1 as published by the Free Software Foundation.                     *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful, but   *
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
+ *   02110-1301  USA                                                       *
+ *                                                                         *
+ *   Alternatively, this file is available under the Mozilla Public        *
+ *   License Version 1.1.  You may obtain a copy of the License at         *
+ *   http://www.mozilla.org/MPL/                                           *
+ ***************************************************************************/
+
+#ifndef TAGLIB_TZLIB_H
+#define TAGLIB_TZLIB_H
+
+#include <tbytevector.h>
+
+// THIS FILE IS NOT A PART OF THE TAGLIB API
+
+#ifndef DO_NOT_DOCUMENT  // tell Doxygen not to document this header
+
+namespace TagLib {
+
+  namespace zlib {
+
+     /*!
+      * Returns whether or not zlib is installed and ready to use.
+      */
+     bool isAvailable();
+
+     /*!
+      * Decompress \a data by zlib.
+      */
+     ByteVector decompress(const ByteVector &data);
+
+  }
+}
+
+#endif
+
+#endif
index 8b9e5b3f660da8b6f2f3bc588ea0bb0002b23ed9..a560658b5227a10695f57d86abaf3714267725a5 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <string>
 #include <stdio.h>
 #include <id3v2tag.h>
@@ -48,6 +44,7 @@
 #include <tableofcontentsframe.h>
 #include <tdebug.h>
 #include <tpropertymap.h>
+#include <tzlib.h>
 #include <cppunit/extensions/HelperMacros.h>
 #include "utils.h"
 
@@ -767,26 +764,23 @@ public:
     MPEG::File f(TEST_FILE_PATH_C("compressed_id3_frame.mp3"), false);
     CPPUNIT_ASSERT(f.ID3v2Tag()->frameListMap().contains("APIC"));
 
-#ifdef HAVE_ZLIB
-
-    ID3v2::AttachedPictureFrame *frame
-      = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
-    CPPUNIT_ASSERT(frame);
-    CPPUNIT_ASSERT_EQUAL(String("image/bmp"), frame->mimeType());
-    CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::Other, frame->type());
-    CPPUNIT_ASSERT_EQUAL(String(""), frame->description());
-    CPPUNIT_ASSERT_EQUAL((unsigned int)86414, frame->picture().size());
-
-#else
-
-    // Skip the test if ZLIB is not installed.
-    // The message "Compressed frames are currently not supported." will be displayed.
-
-    ID3v2::UnknownFrame *frame
-      = dynamic_cast<TagLib::ID3v2::UnknownFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
-    CPPUNIT_ASSERT(frame);
+    if(zlib::isAvailable()) {
+      ID3v2::AttachedPictureFrame *frame
+        = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
+      CPPUNIT_ASSERT(frame);
+      CPPUNIT_ASSERT_EQUAL(String("image/bmp"), frame->mimeType());
+      CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::Other, frame->type());
+      CPPUNIT_ASSERT_EQUAL(String(""), frame->description());
+      CPPUNIT_ASSERT_EQUAL((unsigned int)86414, frame->picture().size());
+    }
+    else {
+      // Skip the test if ZLIB is not installed.
+      // The message "Compressed frames are currently not supported." will be displayed.
 
-#endif
+      ID3v2::UnknownFrame *frame
+        = dynamic_cast<TagLib::ID3v2::UnknownFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
+      CPPUNIT_ASSERT(frame);
+    }
   }
 
   void testW000()