Tag Parser  8.2.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
flacmetadata.cpp
Go to the documentation of this file.
1 #include "./flacmetadata.h"
2 
3 #include "../exceptions.h"
4 #include "../tagvalue.h"
5 
6 #include <c++utilities/conversion/binaryconversion.h>
7 #include <c++utilities/io/binaryreader.h>
8 #include <c++utilities/io/binarywriter.h>
9 #include <c++utilities/io/bitreader.h>
10 
11 #include <cstring>
12 #include <memory>
13 
14 using namespace std;
15 using namespace ConversionUtilities;
16 using namespace IoUtilities;
17 
18 namespace TagParser {
19 
30 void FlacMetaDataBlockHeader::parseHeader(const char *buffer)
31 {
32  m_last = *buffer & 0x80;
33  m_type = *buffer & (0x80 - 1);
34  m_dataSize = BE::toUInt24(buffer + 1);
35 }
36 
41 void FlacMetaDataBlockHeader::makeHeader(std::ostream &outputStream)
42 {
43  byte buff[4];
44  *buff = (m_last ? (0x80 | m_type) : m_type);
45  BE::getBytes24(m_dataSize, reinterpret_cast<char *>(buff) + 1);
46  outputStream.write(reinterpret_cast<char *>(buff), sizeof(buff));
47 }
48 
59 void FlacMetaDataBlockStreamInfo::parse(const char *buffer)
60 {
61  BitReader reader(buffer, 0x22);
62  m_minBlockSize = reader.readBits<uint16>(16);
63  m_maxBlockSize = reader.readBits<uint16>(16);
64  m_minFrameSize = reader.readBits<uint32>(24);
65  m_maxFrameSize = reader.readBits<uint32>(24);
66  m_samplingFrequency = reader.readBits<uint32>(20);
67  m_channelCount = reader.readBits<byte>(3) + 1;
68  m_bitsPerSample = reader.readBits<byte>(5) + 1;
69  m_totalSampleCount = reader.readBits<uint64>(36);
70  memcpy(m_md5Sum, buffer + 0x22 - sizeof(m_md5Sum), sizeof(m_md5Sum));
71 }
72 
84 void FlacMetaDataBlockPicture::parse(istream &inputStream, uint32 maxSize)
85 {
86  CHECK_MAX_SIZE(32);
87  BinaryReader reader(&inputStream);
88  m_pictureType = reader.readUInt32BE();
89  uint32 size = reader.readUInt32BE();
90  CHECK_MAX_SIZE(size);
91  m_value.setMimeType(reader.readString(size));
92  size = reader.readUInt32BE();
93  CHECK_MAX_SIZE(size);
94  m_value.setDescription(reader.readString(size));
95  // skip width, height, color depth, number of colors used
96  inputStream.seekg(4 * 4, ios_base::cur);
97  size = reader.readUInt32BE();
98  CHECK_MAX_SIZE(size);
99  if (size) {
100  auto data = make_unique<char[]>(size);
101  inputStream.read(data.get(), size);
102  m_value.assignData(move(data), size, TagDataType::Picture);
103  } else {
104  m_value.clearData();
105  }
106 }
107 
113 uint32 FlacMetaDataBlockPicture::requiredSize() const
114 {
115  const auto requiredSize(32 + m_value.mimeType().size() + m_value.description().size() + m_value.dataSize());
116  if (requiredSize > numeric_limits<uint32>::max()) {
117  throw InvalidDataException();
118  }
119  return static_cast<uint32>(requiredSize);
120 }
121 
126 void FlacMetaDataBlockPicture::make(ostream &outputStream)
127 {
128  if (m_value.mimeType().size() > numeric_limits<uint32>::max() || m_value.description().size() > numeric_limits<uint32>::max()
129  || m_value.dataSize() > numeric_limits<uint32>::max()) {
130  throw InvalidDataException();
131  }
132  BinaryWriter writer(&outputStream);
133  writer.writeUInt32BE(pictureType());
134  writer.writeUInt32BE(static_cast<uint32>(m_value.mimeType().size()));
135  writer.writeString(m_value.mimeType());
136  writer.writeUInt32BE(static_cast<uint32>(m_value.description().size()));
137  writer.writeString(m_value.description());
138  writer.writeUInt32BE(0); // skip width
139  writer.writeUInt32BE(0); // skip height
140  writer.writeUInt32BE(0); // skip color depth
141  writer.writeUInt32BE(0); // skip number of colors used
142  writer.writeUInt32BE(static_cast<uint32>(m_value.dataSize()));
143  writer.write(value().dataPointer(), static_cast<streamoff>(m_value.dataSize()));
144 }
145 
146 } // namespace TagParser
Contains utility classes helping to read and write streams.
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
Definition: exceptions.h:25
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
#define CHECK_MAX_SIZE(sizeDenotation)
Throws TruncatedDataException() if the specified sizeDenotation exceeds maxSize; otherwise maxSize is...
Definition: exceptions.h:70