tagparser/vorbis/vorbiscomment.cpp

256 lines
8.7 KiB
C++
Raw Normal View History

2015-09-06 19:57:33 +02:00
#include "./vorbiscomment.h"
#include "./vorbiscommentids.h"
2015-04-22 19:22:01 +02:00
2015-09-06 19:57:33 +02:00
#include "../ogg/oggiterator.h"
2015-04-22 19:22:01 +02:00
2015-09-06 19:57:33 +02:00
#include "../exceptions.h"
2015-04-22 19:22:01 +02:00
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
#include <c++utilities/io/copy.h>
#include <c++utilities/misc/memory.h>
#include <map>
using namespace std;
using namespace IoUtilities;
using namespace ConversionUtilities;
namespace Media {
/*!
* \class Media::VorbisComment
2016-05-14 00:24:01 +02:00
* \brief Implementation of Media::Tag for Vorbis comments.
2015-04-22 19:22:01 +02:00
*/
const TagValue &VorbisComment::value(KnownField field) const
{
switch(field) {
case KnownField::Vendor:
return vendor();
default:
return FieldMapBasedTag<VorbisCommentField, CaseInsensitiveStringComparer>::value(field);
}
}
bool VorbisComment::setValue(KnownField field, const TagValue &value)
{
switch(field) {
case KnownField::Vendor:
setVendor(value);
return true;
default:
return FieldMapBasedTag<VorbisCommentField, CaseInsensitiveStringComparer>::setValue(field, value);
}
}
2015-04-22 19:22:01 +02:00
string VorbisComment::fieldId(KnownField field) const
{
using namespace VorbisCommentIds;
switch(field) {
case KnownField::Album: return album();
case KnownField::Artist: return artist();
case KnownField::Comment: return comment();
case KnownField::Cover: return cover();
case KnownField::Year: return date();
case KnownField::Title: return title();
case KnownField::Genre: return genre();
case KnownField::TrackPosition: return trackNumber();
case KnownField::DiskPosition: return diskNumber();
case KnownField::PartNumber: return partNumber();
case KnownField::Composer: return composer();
2016-03-22 22:52:36 +01:00
case KnownField::Encoder: return encoder();
2015-04-22 19:22:01 +02:00
case KnownField::EncoderSettings: return encoderSettings();
case KnownField::Description: return description();
case KnownField::RecordLabel: return label();
case KnownField::Performers: return performer();
2016-01-17 19:32:58 +01:00
case KnownField::Language: return language();
2015-04-22 19:22:01 +02:00
case KnownField::Lyricist: return lyricist();
default: return string();
}
}
KnownField VorbisComment::knownField(const string &id) const
{
using namespace VorbisCommentIds;
2016-03-22 22:52:36 +01:00
static const map<string, KnownField> fieldMap({
2015-04-22 19:22:01 +02:00
{album(), KnownField::Album},
{artist(), KnownField::Artist},
{comment(), KnownField::Comment},
{cover(), KnownField::Cover},
{date(), KnownField::Year},
{title(), KnownField::Title},
{genre(), KnownField::Genre},
{trackNumber(), KnownField::TrackPosition},
{diskNumber(), KnownField::DiskPosition},
{partNumber(), KnownField::PartNumber},
{composer(), KnownField::Composer},
2016-03-22 22:52:36 +01:00
{encoder(), KnownField::Encoder},
2015-04-22 19:22:01 +02:00
{encoderSettings(), KnownField::EncoderSettings},
{description(), KnownField::Description},
{label(), KnownField::RecordLabel},
{performer(), KnownField::Performers},
{lyricist(), KnownField::Lyricist}
});
try {
2016-03-22 22:52:36 +01:00
return fieldMap.at(id);
2015-04-22 19:22:01 +02:00
} catch(out_of_range &) {
return KnownField::Invalid;
}
}
/*!
2016-05-16 20:56:53 +02:00
* \brief Internal implementation for parsing.
2015-04-22 19:22:01 +02:00
*/
2016-05-16 20:56:53 +02:00
template<class StreamType>
void VorbisComment::internalParse(StreamType &stream, uint64 maxSize, VorbisCommentFlags flags)
2015-04-22 19:22:01 +02:00
{
// prepare parsing
invalidateStatus();
static const string context("parsing Vorbis comment");
2016-05-16 20:56:53 +02:00
uint64 startOffset = static_cast<uint64>(stream.tellg());
2015-04-22 19:22:01 +02:00
try {
// read signature: 0x3 + "vorbis"
char sig[8];
2016-05-14 00:24:01 +02:00
bool skipSignature = flags & VorbisCommentFlags::NoSignature;
2016-01-17 19:32:58 +01:00
if(!skipSignature) {
2016-05-16 20:56:53 +02:00
CHECK_MAX_SIZE(7);
stream.read(sig, 7);
2016-01-17 19:32:58 +01:00
skipSignature = (ConversionUtilities::BE::toUInt64(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u;
}
if(skipSignature) {
2015-04-22 19:22:01 +02:00
// read vendor (length prefixed string)
{
2016-05-16 20:56:53 +02:00
CHECK_MAX_SIZE(4);
stream.read(sig, 4);
2016-05-14 00:24:01 +02:00
const auto vendorSize = LE::toUInt32(sig);
2016-05-16 20:56:53 +02:00
if(vendorSize <= maxSize) {
2016-01-17 19:32:58 +01:00
auto buff = make_unique<char []>(vendorSize);
2016-05-16 20:56:53 +02:00
stream.read(buff.get(), vendorSize);
2016-05-14 00:24:01 +02:00
m_vendor.assignData(move(buff), vendorSize, TagDataType::Text, TagTextEncoding::Utf8);
// TODO: Is the vendor string actually UTF-8 (like the field values)?
2016-01-17 19:32:58 +01:00
} else {
addNotification(NotificationType::Critical, "Vendor information is truncated.", context);
throw TruncatedDataException();
}
2016-05-16 20:56:53 +02:00
maxSize -= vendorSize;
2015-04-22 19:22:01 +02:00
}
// read field count
2016-05-16 20:56:53 +02:00
CHECK_MAX_SIZE(4);
stream.read(sig, 4);
2015-04-22 19:22:01 +02:00
uint32 fieldCount = LE::toUInt32(sig);
VorbisCommentField field;
const string &fieldId = field.id();
for(uint32 i = 0; i < fieldCount; ++i) {
// read fields
try {
2016-05-16 20:56:53 +02:00
field.parse(stream, maxSize);
2015-04-22 19:22:01 +02:00
fields().insert(pair<fieldType::identifierType, fieldType>(fieldId, field));
2016-01-17 19:32:58 +01:00
} catch(const TruncatedDataException &) {
2015-04-22 19:22:01 +02:00
addNotifications(field);
throw;
2016-01-17 19:32:58 +01:00
} catch(const Failure &) {
2015-04-22 19:22:01 +02:00
// nothing to do here since notifications will be added anyways
}
addNotifications(field);
field.invalidateNotifications();
}
2016-05-14 00:24:01 +02:00
if(!(flags & VorbisCommentFlags::NoFramingByte)) {
2016-05-16 20:56:53 +02:00
stream.ignore(); // skip framing byte
2016-05-14 00:24:01 +02:00
}
2016-05-16 20:56:53 +02:00
m_size = static_cast<uint32>(static_cast<uint64>(stream.tellg()) - startOffset);
2015-04-22 19:22:01 +02:00
} else {
addNotification(NotificationType::Critical, "Signature is invalid.", context);
throw InvalidDataException();
}
2016-01-17 19:32:58 +01:00
} catch(const TruncatedDataException &) {
2016-05-16 20:56:53 +02:00
m_size = static_cast<uint32>(static_cast<uint64>(stream.tellg()) - startOffset);
2015-04-22 19:22:01 +02:00
addNotification(NotificationType::Critical, "Vorbis comment is truncated.", context);
throw;
}
}
2016-05-16 20:56:53 +02:00
/*!
* \brief Parses tag information using the specified OGG \a iterator.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Media::Failure or a derived exception when a parsing
* error occurs.
*/
void VorbisComment::parse(OggIterator &iterator, VorbisCommentFlags flags)
{
internalParse(iterator, iterator.streamSize(), flags);
}
/*!
* \brief Parses tag information using the specified OGG \a iterator.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Media::Failure or a derived exception when a parsing
* error occurs.
*/
void VorbisComment::parse(istream &stream, uint64 maxSize, VorbisCommentFlags flags)
{
internalParse(stream, maxSize, flags);
}
2015-04-22 19:22:01 +02:00
/*!
* \brief Writes tag information to the specified \a stream.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Media::Failure or a derived exception when a making
* error occurs.
*/
2016-05-14 00:24:01 +02:00
void VorbisComment::make(std::ostream &stream, VorbisCommentFlags flags)
2015-04-22 19:22:01 +02:00
{
// prepare making
invalidateStatus();
static const string context("making Vorbis comment");
string vendor;
try {
m_vendor.toString(vendor);
2016-03-22 22:52:36 +01:00
} catch(const ConversionException &) {
addNotification(NotificationType::Warning, "Can not convert the assigned vendor to string.", context);
}
2015-04-22 19:22:01 +02:00
BinaryWriter writer(&stream);
2016-05-14 00:24:01 +02:00
if(!(flags & VorbisCommentFlags::NoSignature)) {
2016-01-17 19:32:58 +01:00
// write signature
static const char sig[7] = {0x03, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73};
stream.write(sig, sizeof(sig));
}
2015-04-22 19:22:01 +02:00
// write vendor
writer.writeUInt32LE(vendor.size());
writer.writeString(vendor);
2016-05-16 20:56:53 +02:00
// write field count later
const auto fieldCountOffset = stream.tellp();
writer.writeUInt32LE(0);
2015-04-22 19:22:01 +02:00
// write fields
2016-05-16 20:56:53 +02:00
uint32 fieldsWritten = 0;
2015-04-22 19:22:01 +02:00
for(auto i : fields()) {
VorbisCommentField &field = i.second;
2015-08-16 23:39:42 +02:00
if(!field.value().isEmpty()) {
try {
2016-05-16 20:56:53 +02:00
if(field.make(writer, flags)) {
++fieldsWritten;
}
2016-05-14 00:24:01 +02:00
} catch(const Failure &) {
2015-08-16 23:39:42 +02:00
// nothing to do here since notifications will be added anyways
}
// add making notifications
addNotifications(context, field);
field.invalidateNotifications();
2015-04-22 19:22:01 +02:00
}
}
2016-05-16 20:56:53 +02:00
// write field count
const auto framingByteOffset = stream.tellp();
stream.seekp(fieldCountOffset);
writer.writeUInt32LE(fieldsWritten);
stream.seekp(framingByteOffset);
2015-04-22 19:22:01 +02:00
// write framing byte
2016-05-14 00:24:01 +02:00
if(!(flags & VorbisCommentFlags::NoFramingByte)) {
stream.put(0x01);
}
2015-04-22 19:22:01 +02:00
}
}