tagparser/vorbis/vorbiscomment.cpp

284 lines
10 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
#include "../diagnostics.h"
2018-03-07 01:17:50 +01: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 <map>
2017-02-05 21:02:40 +01:00
#include <memory>
2015-04-22 19:22:01 +02:00
using namespace std;
2019-06-10 22:49:11 +02:00
using namespace CppUtilities;
2015-04-22 19:22:01 +02:00
namespace TagParser {
2015-04-22 19:22:01 +02:00
/*!
* \class TagParser::VorbisComment
* \brief Implementation of TagParser::Tag for Vorbis comments.
2015-04-22 19:22:01 +02:00
*/
const TagValue &VorbisComment::value(KnownField field) const
{
2018-03-07 01:17:50 +01:00
switch (field) {
case KnownField::Vendor:
return vendor();
default:
return FieldMapBasedTag<VorbisComment>::value(field);
}
}
bool VorbisComment::setValue(KnownField field, const TagValue &value)
{
2018-03-07 01:17:50 +01:00
switch (field) {
case KnownField::Vendor:
setVendor(value);
return true;
default:
return FieldMapBasedTag<VorbisComment>::setValue(field, value);
}
}
VorbisComment::IdentifierType VorbisComment::internallyGetFieldId(KnownField field) const
2015-04-22 19:22:01 +02:00
{
using namespace VorbisCommentIds;
2018-03-07 01:17:50 +01:00
switch (field) {
case KnownField::Album:
return std::string(album());
2018-03-07 01:17:50 +01:00
case KnownField::Artist:
return std::string(artist());
2018-03-07 01:17:50 +01:00
case KnownField::Comment:
return std::string(comment());
2018-03-07 01:17:50 +01:00
case KnownField::Cover:
return std::string(cover());
case KnownField::RecordDate:
return std::string(date());
2018-03-07 01:17:50 +01:00
case KnownField::Title:
return std::string(title());
2018-03-07 01:17:50 +01:00
case KnownField::Genre:
return std::string(genre());
2018-03-07 01:17:50 +01:00
case KnownField::TrackPosition:
return std::string(trackNumber());
2018-03-07 01:17:50 +01:00
case KnownField::DiskPosition:
return std::string(diskNumber());
2018-03-07 01:17:50 +01:00
case KnownField::PartNumber:
return std::string(partNumber());
2018-03-07 01:17:50 +01:00
case KnownField::Composer:
return std::string(composer());
2018-03-07 01:17:50 +01:00
case KnownField::Encoder:
return std::string(encoder());
2018-03-07 01:17:50 +01:00
case KnownField::EncoderSettings:
return std::string(encoderSettings());
2018-03-07 01:17:50 +01:00
case KnownField::Description:
return std::string(description());
case KnownField::Grouping:
return std::string(grouping());
2018-03-07 01:17:50 +01:00
case KnownField::RecordLabel:
return std::string(label());
2018-03-07 01:17:50 +01:00
case KnownField::Performers:
return std::string(performer());
2018-03-07 01:17:50 +01:00
case KnownField::Language:
return std::string(language());
2018-03-07 01:17:50 +01:00
case KnownField::Lyricist:
return std::string(lyricist());
2019-08-12 00:29:08 +02:00
case KnownField::Lyrics:
return std::string(lyrics());
case KnownField::AlbumArtist:
return std::string(albumArtist());
2018-03-07 01:17:50 +01:00
default:
return std::string();
2015-04-22 19:22:01 +02:00
}
}
KnownField VorbisComment::internallyGetKnownField(const IdentifierType &id) const
2015-04-22 19:22:01 +02:00
{
using namespace VorbisCommentIds;
// clang-format off
static const std::map<std::string_view, KnownField, CaseInsensitiveStringComparer> fieldMap({
{ album(), KnownField::Album },
{ artist(), KnownField::Artist },
{ comment(), KnownField::Comment },
{ cover(), KnownField::Cover },
{ date(), KnownField::RecordDate },
{ year(), KnownField::RecordDate },
{ title(), KnownField::Title },
{ genre(), KnownField::Genre },
{ trackNumber(), KnownField::TrackPosition },
{ diskNumber(), KnownField::DiskPosition },
{ partNumber(), KnownField::PartNumber },
{ composer(), KnownField::Composer },
{ encoder(), KnownField::Encoder },
{ encoderSettings(), KnownField::EncoderSettings },
{ description(), KnownField::Description },
{ grouping(), KnownField::Grouping },
{ label(), KnownField::RecordLabel },
{ performer(), KnownField::Performers },
{ lyricist(), KnownField::Lyricist },
2019-08-12 00:29:08 +02:00
{ lyrics(), KnownField::Lyrics },
{ albumArtist(), KnownField::AlbumArtist },
});
// clang-format on
const auto knownField(fieldMap.find(id));
return knownField != fieldMap.cend() ? knownField->second : KnownField::Invalid;
2015-04-22 19:22:01 +02:00
}
/*!
2016-05-16 20:56:53 +02:00
* \brief Internal implementation for parsing.
2015-04-22 19:22:01 +02:00
*/
2019-03-13 19:06:42 +01:00
template <class StreamType> void VorbisComment::internalParse(StreamType &stream, std::uint64_t maxSize, VorbisCommentFlags flags, Diagnostics &diag)
2015-04-22 19:22:01 +02:00
{
// prepare parsing
static const string context("parsing Vorbis comment");
2019-03-13 19:06:42 +01:00
std::uint64_t startOffset = static_cast<std::uint64_t>(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;
2018-03-07 01:17:50 +01:00
if (!skipSignature) {
CHECK_MAX_SIZE(7)
2016-05-16 20:56:53 +02:00
stream.read(sig, 7);
2019-06-10 22:49:11 +02:00
skipSignature = (BE::toUInt64(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u;
2016-01-17 19:32:58 +01:00
}
2018-03-07 01:17:50 +01:00
if (skipSignature) {
2015-04-22 19:22:01 +02:00
// read vendor (length prefixed string)
{
CHECK_MAX_SIZE(4)
2016-05-16 20:56:53 +02:00
stream.read(sig, 4);
2016-05-14 00:24:01 +02:00
const auto vendorSize = LE::toUInt32(sig);
2018-03-07 01:17:50 +01:00
if (vendorSize <= maxSize) {
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 {
diag.emplace_back(DiagLevel::Critical, "Vendor information is truncated.", context);
2016-01-17 19:32:58 +01:00
throw TruncatedDataException();
}
2016-05-16 20:56:53 +02:00
maxSize -= vendorSize;
2015-04-22 19:22:01 +02:00
}
// read field count
CHECK_MAX_SIZE(4)
2016-05-16 20:56:53 +02:00
stream.read(sig, 4);
2019-03-13 19:06:42 +01:00
std::uint32_t fieldCount = LE::toUInt32(sig);
for (std::uint32_t i = 0; i < fieldCount; ++i) {
2015-04-22 19:22:01 +02:00
// read fields
VorbisCommentField field;
2015-04-22 19:22:01 +02:00
try {
field.parse(stream, maxSize, diag);
fields().emplace(field.id(), move(field));
2018-03-07 01:17:50 +01:00
} catch (const TruncatedDataException &) {
2015-04-22 19:22:01 +02:00
throw;
2018-03-07 01:17:50 +01:00
} catch (const Failure &) {
2015-04-22 19:22:01 +02:00
// nothing to do here since notifications will be added anyways
}
}
2018-03-07 01:17:50 +01: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
}
2019-03-13 19:06:42 +01:00
m_size = static_cast<std::uint32_t>(static_cast<std::uint64_t>(stream.tellg()) - startOffset);
// turn "YEAR" into "DATE" (unless "DATE" exists)
// note: "DATE" is an official field and "YEAR" only an inofficial one but present in some files. In consistency with
// MediaInfo and VLC player it is treated like "DATE" here.
static const auto dateFieldId = std::string(VorbisCommentIds::date()), yearFieldId = std::string(VorbisCommentIds::year());
if (fields().find(dateFieldId) == fields().end()) {
const auto [first, end] = fields().equal_range(yearFieldId);
for (auto i = first; i != end; ++i) {
fields().emplace(dateFieldId, std::move(i->second));
}
fields().erase(first, end);
}
2015-04-22 19:22:01 +02:00
} else {
diag.emplace_back(DiagLevel::Critical, "Signature is invalid.", context);
2015-04-22 19:22:01 +02:00
throw InvalidDataException();
}
2018-03-07 01:17:50 +01:00
} catch (const TruncatedDataException &) {
2019-03-13 19:06:42 +01:00
m_size = static_cast<std::uint32_t>(static_cast<std::uint64_t>(stream.tellg()) - startOffset);
diag.emplace_back(DiagLevel::Critical, "Vorbis comment is truncated.", context);
2015-04-22 19:22:01 +02:00
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 TagParser::Failure or a derived exception when a parsing
2016-05-16 20:56:53 +02:00
* error occurs.
*/
void VorbisComment::parse(OggIterator &iterator, VorbisCommentFlags flags, Diagnostics &diag)
2016-05-16 20:56:53 +02:00
{
internalParse(iterator, iterator.streamSize(), flags, diag);
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 TagParser::Failure or a derived exception when a parsing
2016-05-16 20:56:53 +02:00
* error occurs.
*/
2019-03-13 19:06:42 +01:00
void VorbisComment::parse(istream &stream, std::uint64_t maxSize, VorbisCommentFlags flags, Diagnostics &diag)
2016-05-16 20:56:53 +02:00
{
internalParse(stream, maxSize, flags, diag);
2016-05-16 20:56:53 +02:00
}
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 TagParser::Failure or a derived exception when a making
2015-04-22 19:22:01 +02:00
* error occurs.
*/
void VorbisComment::make(std::ostream &stream, VorbisCommentFlags flags, Diagnostics &diag)
2015-04-22 19:22:01 +02:00
{
// prepare making
static const string context("making Vorbis comment");
string vendor;
try {
m_vendor.toString(vendor);
2018-03-07 01:17:50 +01:00
} catch (const ConversionException &) {
diag.emplace_back(DiagLevel::Warning, "Can not convert the assigned vendor to string.", context);
}
2015-04-22 19:22:01 +02:00
BinaryWriter writer(&stream);
2018-03-07 01:17:50 +01:00
if (!(flags & VorbisCommentFlags::NoSignature)) {
2016-01-17 19:32:58 +01:00
// write signature
2018-03-07 01:17:50 +01:00
static const char sig[7] = { 0x03, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73 };
2016-01-17 19:32:58 +01:00
stream.write(sig, sizeof(sig));
}
2015-04-22 19:22:01 +02:00
// write vendor
2021-03-20 21:26:25 +01:00
writer.writeUInt32LE(static_cast<std::uint32_t>(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
2019-03-13 19:06:42 +01:00
std::uint32_t fieldsWritten = 0;
2018-03-07 01:17:50 +01:00
for (auto i : fields()) {
2015-04-22 19:22:01 +02:00
VorbisCommentField &field = i.second;
2018-03-07 01:17:50 +01:00
if (!field.value().isEmpty()) {
2015-08-16 23:39:42 +02:00
try {
2018-03-07 01:17:50 +01:00
if (field.make(writer, flags, diag)) {
2016-05-16 20:56:53 +02:00
++fieldsWritten;
}
2018-03-07 01:17:50 +01:00
} catch (const Failure &) {
2015-08-16 23:39:42 +02:00
}
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
2018-03-07 01:17:50 +01:00
if (!(flags & VorbisCommentFlags::NoFramingByte)) {
2016-05-14 00:24:01 +02:00
stream.put(0x01);
}
2015-04-22 19:22:01 +02:00
}
2018-03-07 01:17:50 +01:00
} // namespace TagParser