Tag Parser 11.5.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Loading...
Searching...
No Matches
vorbiscommentfield.cpp
Go to the documentation of this file.
3
4#include "../flac/flacmetadata.h"
5
6#include "../ogg/oggiterator.h"
7
8#include "../diagnostics.h"
9#include "../exceptions.h"
10
11#include <c++utilities/conversion/binaryconversion.h>
12#include <c++utilities/conversion/stringbuilder.h>
13#include <c++utilities/conversion/stringconversion.h>
14#include <c++utilities/io/binaryreader.h>
15#include <c++utilities/io/binarywriter.h>
16
17#include <iostream>
18#include <memory>
19
20using namespace std;
21using namespace CppUtilities;
22
23namespace TagParser {
24
34{
35}
36
40VorbisCommentField::VorbisCommentField(const IdentifierType &id, const TagValue &value)
41 : TagField<VorbisCommentField>(id, value)
42{
43}
44
48template <class StreamType> void VorbisCommentField::internalParse(StreamType &stream, std::uint64_t &maxSize, Diagnostics &diag)
49{
50 static const string context("parsing Vorbis comment field");
51 char buff[4];
52 if (maxSize < 4) {
53 diag.emplace_back(DiagLevel::Critical, argsToString("Field expected at ", static_cast<std::streamoff>(stream.tellg()), '.'), context);
55 } else {
56 maxSize -= 4;
57 }
58 stream.read(buff, 4);
59 if (const auto size = LE::toUInt32(buff)) { // read size
60 if (size <= maxSize) {
61 maxSize -= size;
62 // read data
63 auto data = make_unique<char[]>(size);
64 stream.read(data.get(), size);
65 std::uint32_t idSize = 0;
66 for (const char *i = data.get(), *end = data.get() + size; i != end && *i != '='; ++i, ++idSize)
67 ;
68 // extract id
69 setId(string(data.get(), idSize));
70 if (!idSize) {
71 // empty field ID
72 diag.emplace_back(
73 DiagLevel::Critical, argsToString("The field ID at ", static_cast<std::streamoff>(stream.tellg()), " is empty."), context);
74 throw InvalidDataException();
75 } else if (id() == VorbisCommentIds::cover()) {
76 // extract cover value
77 try {
78 auto decoded = decodeBase64(data.get() + idSize + 1, size - idSize - 1);
79 stringstream bufferStream(ios_base::in | ios_base::out | ios_base::binary);
80 bufferStream.exceptions(ios_base::failbit | ios_base::badbit);
81 bufferStream.rdbuf()->pubsetbuf(reinterpret_cast<char *>(decoded.first.get()), decoded.second);
82 FlacMetaDataBlockPicture pictureBlock(value());
83 pictureBlock.parse(bufferStream, decoded.second);
84 setTypeInfo(pictureBlock.pictureType());
85 } catch (const TruncatedDataException &) {
86 diag.emplace_back(DiagLevel::Critical, "METADATA_BLOCK_PICTURE is truncated.", context);
87 throw;
88 } catch (const ConversionException &) {
89 diag.emplace_back(DiagLevel::Critical, "Base64 coding of METADATA_BLOCK_PICTURE is invalid.", context);
90 throw InvalidDataException();
91 } catch (const std::ios_base::failure &failure) {
92 diag.emplace_back(DiagLevel::Critical,
93 argsToString("An IO error occurred when reading the METADATA_BLOCK_PICTURE struct: ", failure.what()), context);
94 throw Failure();
95 }
96 } else if (id().size() + 1 < size) {
97 const auto str = std::string_view(data.get() + idSize + 1, size - idSize - 1);
98 if (id() == VorbisCommentIds::rating()) {
99 try {
100 // set rating as Popularity to preserve the scale information
101 value().assignPopularity(Popularity{ .rating = stringToNumber<double>(str), .scale = TagType::VorbisComment });
102 } catch (const ConversionException &) {
103 // fallback to text
105 diag.emplace_back(DiagLevel::Warning, argsToString("The rating is not a number."), context);
106 }
107 } else {
108 // extract other values (as string)
110 }
111 }
112 } else {
113 diag.emplace_back(DiagLevel::Critical, argsToString("Field at ", static_cast<std::streamoff>(stream.tellg()), " is truncated."), context);
114 throw TruncatedDataException();
115 }
116 }
117}
118
130{
131 std::uint64_t maxSize = iterator.streamSize() - iterator.currentCharacterOffset();
132 internalParse(iterator, maxSize, diag);
133}
134
145void VorbisCommentField::parse(OggIterator &iterator, std::uint64_t &maxSize, Diagnostics &diag)
146{
147 internalParse(iterator, maxSize, diag);
148}
149
160void VorbisCommentField::parse(istream &stream, std::uint64_t &maxSize, Diagnostics &diag)
161{
162 internalParse(stream, maxSize, diag);
163}
164
174bool VorbisCommentField::make(BinaryWriter &writer, VorbisCommentFlags flags, Diagnostics &diag)
175{
176 static const string context("making Vorbis comment field");
177 if (id().empty()) {
178 diag.emplace_back(DiagLevel::Critical, "The field ID is empty.", context);
179 }
180 try {
181 // try to convert value to string
182 string valueString;
183 if (id() == VorbisCommentIds::cover()) {
184 if (flags & VorbisCommentFlags::NoCovers) {
185 return false;
186 }
187 // make cover
188 if (value().type() != TagDataType::Picture) {
189 diag.emplace_back(DiagLevel::Critical, "Assigned value of cover field is not picture data.", context);
190 throw InvalidDataException();
191 }
192 try {
193 FlacMetaDataBlockPicture pictureBlock(value());
194 pictureBlock.setPictureType(typeInfo());
195
196 const auto requiredSize = pictureBlock.requiredSize();
197 auto buffer = make_unique<char[]>(requiredSize);
198 stringstream bufferStream(ios_base::in | ios_base::out | ios_base::binary);
199 bufferStream.exceptions(ios_base::failbit | ios_base::badbit);
200 bufferStream.rdbuf()->pubsetbuf(buffer.get(), requiredSize);
201
202 pictureBlock.make(bufferStream);
203 valueString = encodeBase64(reinterpret_cast<std::uint8_t *>(buffer.get()), requiredSize);
204 } catch (const Failure &) {
205 diag.emplace_back(DiagLevel::Critical, "Unable to make METADATA_BLOCK_PICTURE struct from the assigned value.", context);
206 throw;
207 } catch (const std::ios_base::failure &failure) {
208 diag.emplace_back(DiagLevel::Critical,
209 argsToString("An IO error occurred when writing the METADATA_BLOCK_PICTURE struct: ", failure.what()), context);
210 throw Failure();
211 }
212 } else if (value().type() == TagDataType::Popularity) {
214 } else {
215 // make normal string value
216 valueString = value().toString(TagTextEncoding::Utf8);
217 }
218 const auto size(valueString.size() + id().size() + 1);
219 if (size > numeric_limits<std::uint32_t>::max()) {
220 diag.emplace_back(DiagLevel::Critical, "Assigned value exceeds the maximum size.", context);
221 throw InvalidDataException();
222 }
223 writer.writeUInt32LE(static_cast<std::uint32_t>(size));
224 writer.writeString(id());
225 writer.writeChar('=');
226 writer.writeString(valueString);
227 } catch (const ConversionException &) {
228 diag.emplace_back(DiagLevel::Critical, "Assigned value can not be converted appropriately.", context);
229 throw InvalidDataException();
230 }
231 return true;
232}
233
234} // namespace TagParser
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
Definition: exceptions.h:11
The FlacMetaDataBlockPicture class is a FLAC "METADATA_BLOCK_PICTURE" parser and maker.
Definition: flacmetadata.h:248
std::uint32_t requiredSize() const
Returns the number of bytes make() will write.
void make(std::ostream &outputStream)
Makes the FLAC "METADATA_BLOCK_PICTURE".
void setPictureType(std::uint32_t pictureType)
Sets the picture type according to the ID3v2 APIC frame.
Definition: flacmetadata.h:289
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
Definition: exceptions.h:25
The OggIterator class helps iterating through all segments of an OGG bitstream.
Definition: oggiterator.h:11
std::uint64_t currentCharacterOffset() const
Returns the offset of the current character in the input stream if the iterator is valid; otherwise a...
Definition: oggiterator.h:222
std::uint64_t streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
Definition: oggiterator.h:117
The TagField class is used by FieldMapBasedTag to store the fields.
void setTypeInfo(const TypeInfoType &typeInfo)
Sets the type info of the current TagField.
const TypeInfoType & typeInfo() const
Returns the type info of the current TagField.
void setId(const IdentifierType &id)
Sets the id of the current Tag Field.
TagValue & value()
Returns the value of the current TagField.
The TagValue class wraps values of different types.
Definition: tagvalue.h:143
void assignText(const char *text, std::size_t textSize, TagTextEncoding textEncoding=TagTextEncoding::Latin1, TagTextEncoding convertTo=TagTextEncoding::Unspecified)
Assigns a copy of the given text.
Definition: tagvalue.cpp:1066
void assignPopularity(const Popularity &value)
Assigns the specified popularity value.
Definition: tagvalue.cpp:1188
Popularity toScaledPopularity(TagType scale=TagType::Unspecified) const
Converts the value of the current TagValue object to its equivalent Popularity representation using t...
Definition: tagvalue.cpp:758
std::string toString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::string representation.
Definition: tagvalue.h:577
The exception that is thrown when the data to be parsed is truncated and therefore can not be parsed ...
Definition: exceptions.h:39
The VorbisCommentField class is used by VorbisComment to store the fields.
void parse(OggIterator &iterator, Diagnostics &diag)
Parses a field using the specified iterator.
VorbisCommentField()
Constructs a new Vorbis comment field.
bool make(CppUtilities::BinaryWriter &writer, VorbisCommentFlags flags, Diagnostics &diag)
Writes the field to a stream using the specified writer.
constexpr TAG_PARSER_EXPORT std::string_view rating()
constexpr TAG_PARSER_EXPORT std::string_view cover()
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
VorbisCommentFlags
The VorbisCommentFlags enum specifies flags which controls parsing and making of Vorbis comments.
std::string toString() const
Returns the popularity as string in the format "rating" if only a rating is present or in the format ...
Definition: tagvalue.cpp:1360