#include <taglib.h>
#include <tdebug.h>
-#include "trefcounter.h"
+#include <trefcounter.h>
+
#include "asfattribute.h"
#include "asffile.h"
+#include "asfutils.h"
using namespace TagLib;
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) {
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:
break;
case UnicodeType:
- data.append(File::renderString(d->stringValue));
+ data.append(renderString(d->stringValue));
break;
case BytesType:
}
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) +
#include <tbytevectorlist.h>
#include <tpropertymap.h>
#include <tstring.h>
+
#include "asffile.h"
#include "asftag.h"
#include "asfproperties.h"
+#include "asfutils.h"
using namespace TagLib;
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));
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);
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);
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);
{
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);
break;
}
bool ok;
- long long size = file->readQWORD(&ok);
+ long long size = readQWORD(file, &ok);
if(!ok) {
file->setValid(false);
break;
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()) {
}
////////////////////////////////////////////////////////////////////////////////
-// 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;
}
-
--- /dev/null
+/***************************************************************************
+ 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