4#include "../ogg/oggiterator.h"
6#include "../diagnostics.h"
7#include "../exceptions.h"
9#include <c++utilities/conversion/stringbuilder.h>
10#include <c++utilities/io/binaryreader.h>
11#include <c++utilities/io/binarywriter.h>
12#include <c++utilities/io/copy.h>
50 using namespace VorbisCommentIds;
53 return std::string(
album());
55 return std::string(
artist());
59 return std::string(
cover());
61 return std::string(
date());
63 return std::string(
title());
65 return std::string(
genre());
83 return std::string(
label());
91 return std::string(
lyrics());
105 using namespace VorbisCommentIds;
107 static const std::map<std::string_view, KnownField, CaseInsensitiveStringComparer> fieldMap({
140template <
class StreamType>
void VorbisComment::internalParse(StreamType &stream, std::uint64_t maxSize,
VorbisCommentFlags flags,
Diagnostics &diag)
143 static const string context(
"parsing Vorbis comment");
144 const auto startOffset =
static_cast<std::uint64_t
>(stream.tellg());
149 if (!skipSignature) {
152 skipSignature = (BE::toUInt64(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u;
159 const auto vendorSize = LE::toUInt32(sig);
160 if (vendorSize <= maxSize) {
161 auto buff = make_unique<char[]>(vendorSize);
162 stream.read(buff.get(), vendorSize);
167 throw TruncatedDataException();
169 maxSize -= vendorSize;
175 for (std::uint32_t i = 0; i <
fieldCount; ++i) {
177 VorbisCommentField field;
179 field.parse(stream, maxSize, diag);
180 fields().emplace(field.id(), move(field));
181 }
catch (
const TruncatedDataException &) {
183 }
catch (
const Failure &) {
190 m_size =
static_cast<std::uint64_t
>(stream.tellg()) - startOffset;
196 const auto [first, end] =
fields().equal_range(yearFieldId);
197 for (
auto i = first; i != end; ++i) {
198 fields().emplace(dateFieldId, std::move(i->second));
200 fields().erase(first, end);
204 throw InvalidDataException();
206 }
catch (
const TruncatedDataException &) {
207 m_size =
static_cast<std::uint64_t
>(stream.tellg()) - startOffset;
213 if constexpr (std::is_same_v<std::decay_t<StreamType>, OggIterator>) {
214 auto bytesRemaining = std::uint64_t();
216 bytesRemaining = stream.remainingBytesInCurrentSegment();
217 if (stream.currentPage().isLastSegmentUnconcluded()) {
218 stream.nextSegment();
220 bytesRemaining += stream.remainingBytesInCurrentSegment();
224 if (bytesRemaining) {
225 diag.emplace_back(
DiagLevel::Warning, argsToString(bytesRemaining,
" bytes left in last segment."), context);
239 internalParse(iterator, iterator.
streamSize(), flags, diag);
251 internalParse(stream, maxSize, flags, diag);
264 static const string context(
"making Vorbis comment");
268 }
catch (
const ConversionException &) {
269 diag.emplace_back(
DiagLevel::Warning,
"Can not convert the assigned vendor to string.", context);
271 BinaryWriter writer(&stream);
274 static const char sig[7] = { 0x03, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73 };
275 stream.write(sig,
sizeof(sig));
278 writer.writeUInt32LE(
static_cast<std::uint32_t
>(
vendor.size()));
279 writer.writeString(
vendor);
281 const auto fieldCountOffset = stream.tellp();
282 writer.writeUInt32LE(0);
284 std::uint32_t fieldsWritten = 0;
285 for (
auto &i :
fields()) {
289 if (field.
make(writer, flags, diag)) {
297 const auto framingByteOffset = stream.tellp();
298 stream.seekp(fieldCountOffset);
299 writer.writeUInt32LE(fieldsWritten);
300 stream.seekp(framingByteOffset);
The Diagnostics class is a container for DiagMessage.
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
bool setValue(const IdentifierType &id, const TagValue &value)
Assigns the given value to the field with the specified id.
typename FieldMapBasedTagTraits< VorbisComment >::FieldType::IdentifierType IdentifierType
const TagValue & value(const IdentifierType &id) const
Returns the value of the field with the specified id.
const std::multimap< IdentifierType, FieldType, Compare > & fields() const
Returns the fields of the tag by providing direct access to the field map of the tag.
KnownField knownField(const IdentifierType &id) const
Returns the field for the specified ID.
std::size_t fieldCount() const
The OggIterator class helps iterating through all segments of an OGG bitstream.
std::uint64_t streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
TagValue & value()
Returns the value of the current TagField.
The TagValue class wraps values of different types.
void assignData(const char *data, std::size_t length, TagDataType type=TagDataType::Binary, TagTextEncoding encoding=TagTextEncoding::Latin1)
std::string toString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::string representation.
bool isEmpty() const
Returns whether no or an empty value is assigned.
#define CHECK_MAX_SIZE(sizeDenotation)
Throws TruncatedDataException() if the specified sizeDenotation exceeds maxSize; otherwise maxSize is...
constexpr TAG_PARSER_EXPORT std::string_view album()
constexpr TAG_PARSER_EXPORT std::string_view encoderSettings()
constexpr TAG_PARSER_EXPORT std::string_view description()
constexpr TAG_PARSER_EXPORT std::string_view title()
constexpr TAG_PARSER_EXPORT std::string_view comment()
constexpr TAG_PARSER_EXPORT std::string_view language()
constexpr TAG_PARSER_EXPORT std::string_view composer()
constexpr TAG_PARSER_EXPORT std::string_view partNumber()
constexpr TAG_PARSER_EXPORT std::string_view conductor()
constexpr TAG_PARSER_EXPORT std::string_view genre()
constexpr TAG_PARSER_EXPORT std::string_view artist()
constexpr TAG_PARSER_EXPORT std::string_view copyright()
constexpr TAG_PARSER_EXPORT std::string_view lyrics()
constexpr TAG_PARSER_EXPORT std::string_view encoder()
constexpr TAG_PARSER_EXPORT std::string_view lyricist()
Contains all classes and functions of the TagInfo library.
KnownField
Specifies the field.
VorbisCommentFlags
The VorbisCommentFlags enum specifies flags which controls parsing and making of Vorbis comments.