]> granicus.if.org Git - taglib/commitdiff
ASF: Hide some internal functions from the public header.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Tue, 23 Jun 2015 09:22:31 +0000 (18:22 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Tue, 23 Jun 2015 09:22:31 +0000 (18:22 +0900)
taglib/asf/asfattribute.cpp
taglib/asf/asffile.cpp
taglib/asf/asffile.h
taglib/asf/asfpicture.cpp
taglib/asf/asfpicture.h
taglib/asf/asfutils.h [new file with mode: 0644]

index 4ee2d0a1851c0564ac17e041356d8239d852d26c..116bfe21d79e67620b22660b398e4cf74316e7d8 100644 (file)
 
 #include <taglib.h>
 #include <tdebug.h>
-#include "trefcounter.h"
+#include <trefcounter.h>
+
 #include "asfattribute.h"
 #include "asffile.h"
+#include "asfutils.h"
 
 using namespace TagLib;
 
@@ -181,23 +183,23 @@ String ASF::Attribute::parse(ASF::File &f, int kind)
   d->pictureValue = Picture::fromInvalid();
   // extended content descriptor
   if(kind == 0) {
-    nameLength = f.readWORD();
-    name = f.readString(nameLength);
-    d->type = ASF::Attribute::AttributeTypes(f.readWORD());
-    size = f.readWORD();
+    nameLength = readWORD(&f);
+    name = readString(&f, nameLength);
+    d->type = ASF::Attribute::AttributeTypes(readWORD(&f));
+    size = readWORD(&f);
   }
   // metadata & metadata library
   else {
-    int temp = f.readWORD();
+    int temp = readWORD(&f);
     // metadata library
     if(kind == 2) {
       d->language = temp;
     }
-    d->stream = f.readWORD();
-    nameLength = f.readWORD();
-    d->type = ASF::Attribute::AttributeTypes(f.readWORD());
-    size = f.readDWORD();
-    name = f.readString(nameLength);
+    d->stream = readWORD(&f);
+    nameLength = readWORD(&f);
+    d->type = ASF::Attribute::AttributeTypes(readWORD(&f));
+    size = readDWORD(&f);
+    name = readString(&f, nameLength);
   }
 
   if(kind != 2 && size > 65535) {
@@ -206,28 +208,28 @@ String ASF::Attribute::parse(ASF::File &f, int kind)
 
   switch(d->type) {
   case WordType:
-    d->shortValue = f.readWORD();
+    d->shortValue = readWORD(&f);
     break;
 
   case BoolType:
     if(kind == 0) {
-      d->boolValue = f.readDWORD() == 1;
+      d->boolValue = (readDWORD(&f) == 1);
     }
     else {
-      d->boolValue = f.readWORD() == 1;
+      d->boolValue = (readWORD(&f) == 1);
     }
     break;
 
   case DWordType:
-    d->intValue = f.readDWORD();
+    d->intValue = readDWORD(&f);
     break;
 
   case QWordType:
-    d->longLongValue = f.readQWORD();
+    d->longLongValue = readQWORD(&f);
     break;
 
   case UnicodeType:
-    d->stringValue = f.readString(size);
+    d->stringValue = readString(&f, size);
     break;
 
   case BytesType:
@@ -295,7 +297,7 @@ ByteVector ASF::Attribute::render(const String &name, int kind) const
     break;
 
   case UnicodeType:
-    data.append(File::renderString(d->stringValue));
+    data.append(renderString(d->stringValue));
     break;
 
   case BytesType:
@@ -309,13 +311,13 @@ ByteVector ASF::Attribute::render(const String &name, int kind) const
   }
 
   if(kind == 0) {
-    data = File::renderString(name, true) +
+    data = renderString(name, true) +
            ByteVector::fromShort((int)d->type, false) +
            ByteVector::fromShort(data.size(), false) +
            data;
   }
   else {
-    ByteVector nameData = File::renderString(name);
+    ByteVector nameData = renderString(name);
     data = ByteVector::fromShort(kind == 2 ? d->language : 0, false) +
            ByteVector::fromShort(d->stream, false) +
            ByteVector::fromShort(nameData.size(), false) +
index c59d8b5a6f0a45e2b12ce239eac1d206c1d31408..3ddf75697d7856525342de23962100b196061bd2 100644 (file)
 #include <tbytevectorlist.h>
 #include <tpropertymap.h>
 #include <tstring.h>
+
 #include "asffile.h"
 #include "asftag.h"
 #include "asfproperties.h"
+#include "asfutils.h"
 
 using namespace TagLib;
 
@@ -260,25 +262,25 @@ ByteVector ASF::File::FilePrivate::ContentDescriptionObject::guid() const
 void ASF::File::FilePrivate::ContentDescriptionObject::parse(ASF::File *file, uint /*size*/)
 {
   file->d->contentDescriptionObject = this;
-  int titleLength = file->readWORD();
-  int artistLength = file->readWORD();
-  int copyrightLength = file->readWORD();
-  int commentLength = file->readWORD();
-  int ratingLength = file->readWORD();
-  file->d->tag->setTitle(file->readString(titleLength));
-  file->d->tag->setArtist(file->readString(artistLength));
-  file->d->tag->setCopyright(file->readString(copyrightLength));
-  file->d->tag->setComment(file->readString(commentLength));
-  file->d->tag->setRating(file->readString(ratingLength));
+  const int titleLength     = readWORD(file);
+  const int artistLength    = readWORD(file);
+  const int copyrightLength = readWORD(file);
+  const int commentLength   = readWORD(file);
+  const int ratingLength    = readWORD(file);
+  file->d->tag->setTitle(readString(file,titleLength));
+  file->d->tag->setArtist(readString(file,artistLength));
+  file->d->tag->setCopyright(readString(file,copyrightLength));
+  file->d->tag->setComment(readString(file,commentLength));
+  file->d->tag->setRating(readString(file,ratingLength));
 }
 
 ByteVector ASF::File::FilePrivate::ContentDescriptionObject::render(ASF::File *file)
 {
-  ByteVector v1 = file->renderString(file->d->tag->title());
-  ByteVector v2 = file->renderString(file->d->tag->artist());
-  ByteVector v3 = file->renderString(file->d->tag->copyright());
-  ByteVector v4 = file->renderString(file->d->tag->comment());
-  ByteVector v5 = file->renderString(file->d->tag->rating());
+  const ByteVector v1 = renderString(file->d->tag->title());
+  const ByteVector v2 = renderString(file->d->tag->artist());
+  const ByteVector v3 = renderString(file->d->tag->copyright());
+  const ByteVector v4 = renderString(file->d->tag->comment());
+  const ByteVector v5 = renderString(file->d->tag->rating());
   data.clear();
   data.append(ByteVector::fromShort(v1.size(), false));
   data.append(ByteVector::fromShort(v2.size(), false));
@@ -301,7 +303,7 @@ ByteVector ASF::File::FilePrivate::ExtendedContentDescriptionObject::guid() cons
 void ASF::File::FilePrivate::ExtendedContentDescriptionObject::parse(ASF::File *file, uint /*size*/)
 {
   file->d->extendedContentDescriptionObject = this;
-  int count = file->readWORD();
+  int count = readWORD(file);
   while(count--) {
     ASF::Attribute attribute;
     String name = attribute.parse(*file);
@@ -325,7 +327,7 @@ ByteVector ASF::File::FilePrivate::MetadataObject::guid() const
 void ASF::File::FilePrivate::MetadataObject::parse(ASF::File *file, uint /*size*/)
 {
   file->d->metadataObject = this;
-  int count = file->readWORD();
+  int count = readWORD(file);
   while(count--) {
     ASF::Attribute attribute;
     String name = attribute.parse(*file, 1);
@@ -349,7 +351,7 @@ ByteVector ASF::File::FilePrivate::MetadataLibraryObject::guid() const
 void ASF::File::FilePrivate::MetadataLibraryObject::parse(ASF::File *file, uint /*size*/)
 {
   file->d->metadataLibraryObject = this;
-  int count = file->readWORD();
+  int count = readWORD(file);
   while(count--) {
     ASF::Attribute attribute;
     String name = attribute.parse(*file, 2);
@@ -379,7 +381,7 @@ void ASF::File::FilePrivate::HeaderExtensionObject::parse(ASF::File *file, uint
 {
   file->d->headerExtensionObject = this;
   file->seek(18, File::Current);
-  long long dataSize = file->readDWORD();
+  long long dataSize = readDWORD(file);
   long long dataPos = 0;
   while(dataPos < dataSize) {
     ByteVector guid = file->readBlock(16);
@@ -388,7 +390,7 @@ void ASF::File::FilePrivate::HeaderExtensionObject::parse(ASF::File *file, uint
       break;
     }
     bool ok;
-    long long size = file->readQWORD(&ok);
+    long long size = readQWORD(file, &ok);
     if(!ok) {
       file->setValid(false);
       break;
@@ -524,77 +526,6 @@ ASF::Properties *ASF::File::audioProperties() const
   return d->properties;
 }
 
-void ASF::File::read(bool /*readProperties*/, Properties::ReadStyle /*propertiesStyle*/)
-{
-  if(!isValid())
-    return;
-
-  ByteVector guid = readBlock(16);
-  if(guid != headerGuid) {
-    debug("ASF: Not an ASF file.");
-    setValid(false);
-    return;
-  }
-
-  d->tag = new ASF::Tag();
-  d->properties = new ASF::Properties();
-
-  bool ok;
-  d->size = readQWORD(&ok);
-  if(!ok) {
-    setValid(false);
-    return;
-  }
-  int numObjects = readDWORD(&ok);
-  if(!ok) {
-    setValid(false);
-    return;
-  }
-  seek(2, Current);
-
-  for(int i = 0; i < numObjects; i++) {
-    ByteVector guid = readBlock(16);
-    if(guid.size() != 16) {
-      setValid(false);
-      break;
-    }
-    long size = (long)readQWORD(&ok);
-    if(!ok) {
-      setValid(false);
-      break;
-    }
-    FilePrivate::BaseObject *obj;
-    if(guid == filePropertiesGuid) {
-      obj = new FilePrivate::FilePropertiesObject();
-    }
-    else if(guid == streamPropertiesGuid) {
-      obj = new FilePrivate::StreamPropertiesObject();
-    }
-    else if(guid == contentDescriptionGuid) {
-      obj = new FilePrivate::ContentDescriptionObject();
-    }
-    else if(guid == extendedContentDescriptionGuid) {
-      obj = new FilePrivate::ExtendedContentDescriptionObject();
-    }
-    else if(guid == headerExtensionGuid) {
-      obj = new FilePrivate::HeaderExtensionObject();
-    }
-    else if(guid == codecListGuid) {
-      obj = new FilePrivate::CodecListObject();
-    }
-    else {
-      if(guid == contentEncryptionGuid ||
-         guid == extendedContentEncryptionGuid ||
-         guid == advancedContentEncryptionGuid) {
-        d->properties->setEncrypted(true);
-      }
-      obj = new FilePrivate::UnknownObject(guid);
-    }
-    obj->parse(this, size);
-    d->objects.append(obj);
-  }
-}
-
 bool ASF::File::save()
 {
   if(readOnly()) {
@@ -669,64 +600,76 @@ bool ASF::File::save()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-// protected members
+// private members
 ////////////////////////////////////////////////////////////////////////////////
 
-int ASF::File::readWORD(bool *ok)
+void ASF::File::read(bool /*readProperties*/, Properties::ReadStyle /*propertiesStyle*/)
 {
-  ByteVector v = readBlock(2);
-  if(v.size() != 2) {
-    if(ok) *ok = false;
-    return 0;
-  }
-  if(ok) *ok = true;
-  return v.toUShort(false);
-}
+  if(!isValid())
+    return;
 
-unsigned int ASF::File::readDWORD(bool *ok)
-{
-  ByteVector v = readBlock(4);
-  if(v.size() != 4) {
-    if(ok) *ok = false;
-    return 0;
+  ByteVector guid = readBlock(16);
+  if(guid != headerGuid) {
+    debug("ASF: Not an ASF file.");
+    setValid(false);
+    return;
   }
-  if(ok) *ok = true;
-  return v.toUInt(false);
-}
 
-long long ASF::File::readQWORD(bool *ok)
-{
-  ByteVector v = readBlock(8);
-  if(v.size() != 8) {
-    if(ok) *ok = false;
-    return 0;
-  }
-  if(ok) *ok = true;
-  return v.toLongLong(false);
-}
+  d->tag = new ASF::Tag();
+  d->properties = new ASF::Properties();
 
-String ASF::File::readString(int length)
-{
-  ByteVector data = readBlock(length);
-  unsigned int size = data.size();
-  while (size >= 2) {
-    if(data[size - 1] != '\0' || data[size - 2] != '\0') {
-      break;
-    }
-    size -= 2;
+  bool ok;
+  d->size = readQWORD(this, &ok);
+  if(!ok) {
+    setValid(false);
+    return;
   }
-  if(size != data.size()) {
-    data.resize(size);
+  int numObjects = readDWORD(this, &ok);
+  if(!ok) {
+    setValid(false);
+    return;
   }
-  return String(data, String::UTF16LE);
-}
+  seek(2, Current);
 
-ByteVector ASF::File::renderString(const String &str, bool includeLength)
-{
-  ByteVector data = str.data(String::UTF16LE) + ByteVector::fromShort(0, false);
-  if(includeLength) {
-    data = ByteVector::fromShort(data.size(), false) + data;
+  for(int i = 0; i < numObjects; i++) {
+    ByteVector guid = readBlock(16);
+    if(guid.size() != 16) {
+      setValid(false);
+      break;
+    }
+    long size = (long)readQWORD(this, &ok);
+    if(!ok) {
+      setValid(false);
+      break;
+    }
+    FilePrivate::BaseObject *obj;
+    if(guid == filePropertiesGuid) {
+      obj = new FilePrivate::FilePropertiesObject();
+    }
+    else if(guid == streamPropertiesGuid) {
+      obj = new FilePrivate::StreamPropertiesObject();
+    }
+    else if(guid == contentDescriptionGuid) {
+      obj = new FilePrivate::ContentDescriptionObject();
+    }
+    else if(guid == extendedContentDescriptionGuid) {
+      obj = new FilePrivate::ExtendedContentDescriptionObject();
+    }
+    else if(guid == headerExtensionGuid) {
+      obj = new FilePrivate::HeaderExtensionObject();
+    }
+    else if(guid == codecListGuid) {
+      obj = new FilePrivate::CodecListObject();
+    }
+    else {
+      if(guid == contentEncryptionGuid ||
+         guid == extendedContentEncryptionGuid ||
+         guid == advancedContentEncryptionGuid) {
+        d->properties->setEncrypted(true);
+      }
+      obj = new FilePrivate::UnknownObject(guid);
+    }
+    obj->parse(this, size);
+    d->objects.append(obj);
   }
-  return data;
 }
-
index 4bced68cc6ba2890df68a2bc972ad35e070cd674..f2bba9909f983a34530262b748ec2bc2bee1ac45 100644 (file)
@@ -116,16 +116,8 @@ namespace TagLib {
       virtual bool save();
 
     private:
-      int readWORD(bool *ok = 0);
-      unsigned int readDWORD(bool *ok = 0);
-      long long readQWORD(bool *ok = 0);
-      static ByteVector renderString(const String &str, bool includeLength = false);
-      String readString(int len);
       void read(bool readProperties, Properties::ReadStyle propertiesStyle);
 
-      friend class Attribute;
-      friend class Picture;
-
       class FilePrivate;
       FilePrivate *d;
     };
index 999f920438c58f1836e9e613b0176cfd7a929b86..cdf6e758517224ca3feb451e2c937a918eea3bd6 100644 (file)
 
 #include <taglib.h>
 #include <tdebug.h>
-#include "trefcounter.h"
+#include <trefcounter.h>
+
 #include "asfattribute.h"
 #include "asffile.h"
 #include "asfpicture.h"
+#include "asfutils.h"
 
 using namespace TagLib;
 
@@ -134,8 +136,8 @@ ByteVector ASF::Picture::render() const
   return
     ByteVector((char)d->type) +
     ByteVector::fromUInt(d->picture.size(), false) +
-    ASF::File::renderString(d->mimeType) +
-    ASF::File::renderString(d->description) +
+    renderString(d->mimeType) +
+    renderString(d->description) +
     d->picture;
 }
 
index aa0a060c19f2eac21833b3ab7137f4dd7ca62bb2..b510c35f1690a8f3b411e4d2dff94300a2661728 100644 (file)
@@ -205,8 +205,8 @@ namespace TagLib
       /* THIS IS PRIVATE, DON'T TOUCH IT! */
       void parse(const ByteVector& );
       static Picture fromInvalid();
-      friend class Attribute;
 #endif
+
       private:
         class PicturePrivate;
         PicturePrivate *d;
diff --git a/taglib/asf/asfutils.h b/taglib/asf/asfutils.h
new file mode 100644 (file)
index 0000000..21dbd03
--- /dev/null
@@ -0,0 +1,101 @@
+/***************************************************************************
+    copyright            : (C) 2015 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_ASFUTILS_H
+#define TAGLIB_ASFUTILS_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 ASF
+  {
+
+    inline ushort readWORD(File *file, bool *ok = 0)
+    {
+      const ByteVector v = file->readBlock(2);
+      if(v.size() != 2) {
+        if(ok) *ok = false;
+        return 0;
+      }
+      if(ok) *ok = true;
+      return v.toUShort(false);
+    }
+
+    inline uint readDWORD(File *file, bool *ok = 0)
+    {
+      const ByteVector v = file->readBlock(4);
+      if(v.size() != 4) {
+        if(ok) *ok = false;
+        return 0;
+      }
+      if(ok) *ok = true;
+      return v.toUInt(false);
+    }
+
+    inline long long readQWORD(File *file, bool *ok = 0)
+    {
+      const ByteVector v = file->readBlock(8);
+      if(v.size() != 8) {
+        if(ok) *ok = false;
+        return 0;
+      }
+      if(ok) *ok = true;
+      return v.toLongLong(false);
+    }
+
+    inline String readString(File *file, int length)
+    {
+      ByteVector data = file->readBlock(length);
+      unsigned int size = data.size();
+      while (size >= 2) {
+        if(data[size - 1] != '\0' || data[size - 2] != '\0') {
+          break;
+        }
+        size -= 2;
+      }
+      if(size != data.size()) {
+        data.resize(size);
+      }
+      return String(data, String::UTF16LE);
+    }
+
+    inline ByteVector renderString(const String &str, bool includeLength = false)
+    {
+      ByteVector data = str.data(String::UTF16LE) + ByteVector::fromShort(0, false);
+      if(includeLength) {
+        data = ByteVector::fromShort(data.size(), false) + data;
+      }
+      return data;
+    }
+
+  }
+}
+
+#endif
+
+#endif