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());
57 return std::string(comment());
59 return std::string(cover());
61 return std::string(date());
63 return std::string(title());
65 return std::string(genre());
67 return std::string(trackNumber());
69 return std::string(diskNumber());
71 return std::string(partNumber());
73 return std::string(composer());
75 return std::string(encoder());
77 return std::string(encodedBy());
79 return std::string(encoderSettings());
81 return std::string(description());
83 return std::string(grouping());
85 return std::string(label());
87 return std::string(performer());
89 return std::string(language());
91 return std::string(lyricist());
93 return std::string(lyrics());
95 return std::string(albumArtist());
97 return std::string(conductor());
99 return std::string(copyright());
101 return std::string(license());
103 return std::string(director());
105 return std::string(isrc());
107 return std::string(rating());
109 return std::string(bpm());
111 return std::string(publisher());
113 return std::string(publisherWebpage());
115 return std::string();
121 using namespace VorbisCommentIds;
123 static const std::map<std::string_view, KnownField, CaseInsensitiveStringComparer> fieldMap({
162void VorbisComment::extendPositionInSetField(std::string_view field, std::string_view totalField,
const std::string &diagContext,
Diagnostics &diag)
164 auto totalValues = std::vector<std::int32_t>();
165 auto fieldsIter =
fields().equal_range(std::string(totalField));
166 auto fieldsDist = std::distance(fieldsIter.first, fieldsIter.second);
170 totalValues.reserve(
static_cast<std::size_t
>(fieldsDist));
171 for (; fieldsIter.first != fieldsIter.second;) {
173 totalValues.emplace_back(fieldsIter.first->second.value().toInteger());
174 fields().erase(fieldsIter.first++);
175 }
catch (
const ConversionException &e) {
176 diag.emplace_back(
DiagLevel::Warning, argsToString(
"Unable to parse \"", totalField,
"\" as integer: ", e.what()), diagContext);
177 totalValues.emplace_back(0);
182 auto totalIter = totalValues.begin(), totalEnd = totalValues.end();
183 for (fieldsIter =
fields().equal_range(std::string(field)); fieldsIter.first != fieldsIter.second && totalIter != totalEnd;
184 ++fieldsIter.first, ++totalIter) {
185 auto &v = fieldsIter.first->second.value();
187 auto p = v.toPositionInSet();
188 if (p.total() && p.total() != *totalIter) {
190 argsToString(
"The \"", totalField,
"\" field value (", *totalIter,
") does not match \"", field,
"\" field value (", p.total(),
191 "). Discarding the former in favor of the latter."),
194 p.setTotal(*totalIter);
197 }
catch (
const ConversionException &e) {
199 argsToString(
"Unable to parse \"", field,
"\" as position in set for incorporating \"", totalField,
"\": ", e.what()), diagContext);
202 if (totalIter != totalEnd) {
204 DiagLevel::Warning, argsToString(
"Vorbis Comment contains more \"", totalField,
"\" fields than \"", field,
"\" fields."), diagContext);
206 for (; totalIter != totalEnd; ++totalIter) {
207 fields().insert(std::make_pair(field, VorbisCommentField(std::string(field), TagValue(
PositionInSet(0, *totalIter)))));
215void VorbisComment::convertTotalFields(
const std::string &diagContext, Diagnostics &diag)
225template <
class StreamType>
226void VorbisComment::internalParse(StreamType &stream, std::uint64_t maxSize,
VorbisCommentFlags flags, std::uint64_t &padding, Diagnostics &diag)
229 static const string context(
"parsing Vorbis comment");
230 const auto startOffset =
static_cast<std::uint64_t
>(stream.tellg());
235 if (!skipSignature) {
238 skipSignature = (BE::toInt<std::uint64_t>(sig) & 0xffffffffffffff00u) == 0x03766F7262697300u;
245 const auto vendorSize = LE::toUInt32(sig);
246 if (vendorSize <= maxSize) {
247 auto buff = make_unique<char[]>(vendorSize);
248 stream.read(buff.get(), vendorSize);
253 throw TruncatedDataException();
255 maxSize -= vendorSize;
261 for (std::uint32_t i = 0; i <
fieldCount; ++i) {
263 VorbisCommentField field;
265 field.parse(stream, maxSize, diag);
266 fields().emplace(field.id(), std::move(field));
267 }
catch (
const TruncatedDataException &) {
269 }
catch (
const Failure &) {
276 m_size =
static_cast<std::uint64_t
>(stream.tellg()) - startOffset;
282 const auto [first, end] =
fields().equal_range(yearFieldId);
283 for (
auto i = first; i != end; ++i) {
284 fields().emplace(dateFieldId, std::move(i->second));
286 fields().erase(first, end);
290 throw InvalidDataException();
292 }
catch (
const TruncatedDataException &) {
293 m_size =
static_cast<std::uint64_t
>(stream.tellg()) - startOffset;
299 if constexpr (std::is_same_v<std::decay_t<StreamType>, OggIterator>) {
300 auto bytesRemaining = std::uint64_t();
302 bytesRemaining = stream.remainingBytesInCurrentSegment();
303 if (stream.currentPage().isLastSegmentUnconcluded()) {
304 stream.nextSegment();
306 bytesRemaining += stream.remainingBytesInCurrentSegment();
310 if (bytesRemaining) {
311 diag.emplace_back(
DiagLevel::Information, argsToString(bytesRemaining,
" bytes left in last segment."), context);
312 padding += bytesRemaining;
317 convertTotalFields(context, diag);
330 auto padding = std::uint64_t();
331 internalParse(iterator, iterator.
streamSize(), flags, padding, diag);
343 internalParse(iterator, iterator.
streamSize(), flags, padding, diag);
355 auto padding = std::uint64_t();
356 internalParse(stream, maxSize, flags, padding, diag);
366 if (field.
make(writer, flags, diag)) {
369 }
catch (
const Failure &) {
385 static const string context(
"making Vorbis comment");
389 }
catch (
const ConversionException &) {
390 diag.emplace_back(
DiagLevel::Warning,
"Can not convert the assigned vendor to string.", context);
392 BinaryWriter writer(&stream);
395 static const char sig[7] = { 0x03, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73 };
396 stream.write(sig,
sizeof(sig));
399 writer.writeUInt32LE(
static_cast<std::uint32_t
>(
vendor.size()));
400 writer.writeString(
vendor);
402 const auto fieldCountOffset = stream.tellp();
403 writer.writeUInt32LE(0);
405 std::uint32_t fieldsWritten = 0;
407 for (
auto &i :
fields()) {
408 if (i.first != coverId) {
409 fieldsWritten += makeField(i.second, writer, flags, diag);
412 if (
const auto cover =
fields().find(coverId); cover !=
fields().end()) {
413 fieldsWritten += makeField(cover->second, writer, flags, diag);
416 const auto framingByteOffset = stream.tellp();
417 stream.seekp(fieldCountOffset);
418 writer.writeUInt32LE(fieldsWritten);
419 stream.seekp(framingByteOffset);
The Diagnostics class is a container for DiagMessage.
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
KnownField knownField(const IdentifierType &id) const
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...
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.