Tag Parser  9.1.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
ebmlelement.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_EBMLELEMENT_H
2 #define TAG_PARSER_EBMLELEMENT_H
3 
4 #include "./ebmlid.h"
5 #include "./matroskaid.h"
6 
7 #include "../genericfileelement.h"
8 
9 #include <c++utilities/conversion/stringbuilder.h>
10 #include <c++utilities/conversion/stringconversion.h>
11 
12 #include <cstdint>
13 #include <iostream>
14 #include <memory>
15 
16 namespace TagParser {
17 
18 class EbmlElement;
19 class MatroskaContainer;
20 
25 public:
27  using IdentifierType = std::uint32_t;
28  using DataSizeType = std::uint64_t;
29 };
30 
31 class TAG_PARSER_EXPORT EbmlElement : public GenericFileElement<EbmlElement> {
33 
34 public:
35  EbmlElement(MatroskaContainer &container, std::uint64_t startOffset);
36 
37  std::string idToString() const;
38  bool isParent() const;
39  bool isPadding() const;
40  std::uint64_t firstChildOffset() const;
41  std::string readString();
42  std::uint64_t readUInteger();
43  double readFloat();
44 
45  static std::uint8_t calculateIdLength(IdentifierType id);
46  static std::uint8_t calculateSizeDenotationLength(std::uint64_t size);
47  static std::uint8_t makeId(IdentifierType id, char *buff);
48  static std::uint8_t makeSizeDenotation(std::uint64_t size, char *buff);
49  static std::uint8_t makeSizeDenotation(std::uint64_t size, char *buff, std::uint8_t minBytes);
50  static std::uint8_t calculateUIntegerLength(std::uint64_t integer);
51  static std::uint8_t makeUInteger(std::uint64_t value, char *buff);
52  static std::uint8_t makeUInteger(std::uint64_t value, char *buff, std::uint8_t minBytes);
53  static void makeSimpleElement(std::ostream &stream, IdentifierType id, std::uint64_t content);
54  static void makeSimpleElement(std::ostream &stream, IdentifierType id, const std::string &content);
55  static void makeSimpleElement(std::ostream &stream, IdentifierType id, const char *data, std::size_t dataSize);
56  static std::uint64_t bytesToBeSkipped;
57 
58 protected:
59  EbmlElement(EbmlElement &parent, std::uint64_t startOffset);
60  EbmlElement(MatroskaContainer &container, std::uint64_t startOffset, std::uint64_t maxSize);
61 
62  void internalParse(Diagnostics &diag);
63 
64 private:
65  std::string parsingContext() const;
66 };
67 
71 inline std::string EbmlElement::idToString() const
72 {
73  using namespace CppUtilities;
74  const char *const name = matroskaIdName(id());
75  if (*name) {
76  return argsToString('0', 'x', numberToString(id(), 16), ' ', '\"', name, '\"');
77  } else {
78  return "0x" + numberToString(id(), 16);
79  }
80 }
81 
88 inline bool EbmlElement::isParent() const
89 {
90  using namespace EbmlIds;
91  using namespace MatroskaIds;
92  switch (id()) {
93  case Header:
94  case SignatureSlot:
95  case SignatureElements:
97  case Segment:
98  case SeekHead:
99  case Seek:
100  case SegmentInfo:
101  case ChapterTranslate:
102  case Cluster:
103  case SilentTracks:
104  case BlockGroup:
105  case BlockAdditions:
106  case BlockMore:
107  case Slices:
108  case TimeSlice:
109  case ReferenceFrame:
110  case Tracks:
111  case TrackEntry:
112  case TrackTranslate:
113  case TrackVideo:
114  case TrackAudio:
115  case TrackOperation:
116  case TrackCombinePlanes:
117  case TrackPlane:
118  case TrackJoinBlocks:
119  case ContentEncodings:
120  case ContentEncoding:
121  case ContentCompression:
122  case ContentEncryption:
123  case Cues:
124  case CuePoint:
125  case CueTrackPositions:
126  case CueReference:
127  case Attachments:
128  case AttachedFile:
129  case Chapters:
130  case EditionEntry:
131  case ChapterAtom:
132  case ChapterTrack:
133  case ChapterDisplay:
134  case ChapProcess:
135  case ChapProcessCommand:
136  case Tags:
137  case MatroskaIds::Tag:
138  case Targets:
139  case SimpleTag:
140  return true;
141  default:
142  return false;
143  }
144 }
145 
149 inline bool EbmlElement::isPadding() const
150 {
151  return id() == EbmlIds::Void;
152 }
153 
158 inline std::uint64_t EbmlElement::firstChildOffset() const
159 {
160  return isParent() ? (idLength() + sizeLength()) : 0;
161 }
162 
163 } // namespace TagParser
164 
165 #endif // TAG_PARSER_EBMLELEMENT_H
TagParser::MatroskaIds::SilentTracks
Definition: matroskaid.h:344
TagParser::MatroskaIds::CuePoint
Definition: matroskaid.h:241
TagParser::MatroskaIds::SimpleTag
Definition: matroskaid.h:211
TagParser::MatroskaIds::SegmentInfo
Definition: matroskaid.h:17
TagParser::MatroskaIds::TrackOperation
Definition: matroskaid.h:105
TagParser::EbmlIds::SignatureSlot
Definition: ebmlid.h:28
TagParser::MatroskaIds::Cues
Definition: matroskaid.h:19
TagParser::MatroskaIds::TimeSlice
Definition: matroskaid.h:387
ebmlid.h
TagParser::GenericFileElement::sizeLength
std::uint32_t sizeLength() const
Returns the length of the size denotation of the element in byte.
Definition: genericfileelement.h:323
TagParser::GenericFileElement::idLength
std::uint32_t idLength() const
Returns the length of the id denotation in byte.
Definition: genericfileelement.h:294
TagParser::matroskaIdName
const TAG_PARSER_EXPORT char * matroskaIdName(std::uint32_t matroskaId)
Returns a string for the specified matroskaId if known; otherwise returns an empty string.
Definition: matroskaid.cpp:22
TagParser::MatroskaIds::CueReference
Definition: matroskaid.h:258
TagParser::MatroskaIds::EditionEntry
Definition: matroskaid.h:288
TagParser::MatroskaIds::Attachments
Definition: matroskaid.h:23
TagParser::MatroskaIds::Chapters
Definition: matroskaid.h:24
TagParser::MatroskaIds::CueTrackPositions
Definition: matroskaid.h:246
TagParser::EbmlElement::isPadding
bool isPadding() const
Returns an indication whether the element is considered as padding.
Definition: ebmlelement.h:149
TagParser::FileElementTraits
Defines traits for the specified ImplementationType.
Definition: genericfileelement.h:34
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
TagParser::GenericFileElement::IdentifierType
typename FileElementTraits< ImplementationType >::IdentifierType IdentifierType
Specifies the type used to store identifiers.
Definition: genericfileelement.h:57
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::GenericFileElement::id
const IdentifierType & id() const
Returns the element ID.
Definition: genericfileelement.h:278
TagParser::EbmlElement::isParent
bool isParent() const
Returns an indication whether the element is a parent element.
Definition: ebmlelement.h:88
TagParser::GenericFileElement
The GenericFileElement class helps to parse binary files which consist of an arboreal element strucut...
Definition: genericfileelement.h:45
matroskaid.h
TagParser::MatroskaIds::BlockGroup
Definition: matroskaid.h:348
TagParser::MatroskaIds::TrackCombinePlanes
Definition: matroskaid.h:150
TagParser::EbmlElement
The EbmlElement class helps to parse EBML files such as Matroska files.
Definition: ebmlelement.h:31
TagParser::MatroskaIds::ChapProcessCommand
Definition: matroskaid.h:332
TagParser::MatroskaIds::Tracks
Definition: matroskaid.h:18
TagParser::MatroskaIds::ChapterTrack
Definition: matroskaid.h:314
TagParser::FileElementTraits< EbmlElement >::DataSizeType
std::uint64_t DataSizeType
Definition: ebmlelement.h:28
TagParser::MatroskaIds::Tags
Definition: matroskaid.h:20
TagParser::EbmlElement::bytesToBeSkipped
static std::uint64_t bytesToBeSkipped
Specifies the number of bytes to be skipped till a valid EBML element is found in the stream.
Definition: ebmlelement.h:56
TagParser::MatroskaIds::ChapterDisplay
Definition: matroskaid.h:315
TagParser::MatroskaIds::Targets
Definition: matroskaid.h:212
TagParser::MatroskaIds::TrackVideo
Definition: matroskaid.h:104
CppUtilities
Definition: abstractcontainer.h:15
TagParser::MatroskaIds::Segment
Definition: matroskaid.h:16
TagParser::EbmlElement::idToString
std::string idToString() const
Converts the specified EBML ID to a printable string.
Definition: ebmlelement.h:71
TagParser::MatroskaIds::ChapterTranslate
Definition: matroskaid.h:56
TagParser::MatroskaIds::TrackTranslate
Definition: matroskaid.h:102
TagParser::MatroskaIds::ReferenceFrame
Definition: matroskaid.h:371
TagParser::MatroskaIds::ContentEncoding
Definition: matroskaid.h:170
TagParser::EbmlIds::Void
Definition: ebmlid.h:28
TagParser::EbmlIds::Header
Definition: ebmlid.h:15
TagParser::MatroskaIds::TrackEntry
Definition: matroskaid.h:68
TagParser::MatroskaIds::SeekHead
Definition: matroskaid.h:21
TagParser::MatroskaIds::ContentEncryption
Definition: matroskaid.h:180
TagParser::EbmlIds::SignatureElementList
Definition: ebmlid.h:38
TagParser::MatroskaIds::Tag
Definition: matroskaid.h:204
TagParser::EbmlIds::SignatureElements
Definition: ebmlid.h:33
TAG_PARSER_EXPORT
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
TagParser::EbmlElement::firstChildOffset
std::uint64_t firstChildOffset() const
Returns the offset of the first child of the element.
Definition: ebmlelement.h:158
TagParser::MatroskaIds::TrackPlane
Definition: matroskaid.h:155
TagParser::MatroskaIds::BlockAdditions
Definition: matroskaid.h:363
TagParser::MatroskaIds::ContentEncodings
Definition: matroskaid.h:111
TagParser::MatroskaIds::AttachedFile
Definition: matroskaid.h:269
TagParser::MatroskaContainer
Implementation of GenericContainer<MediaFileInfo, MatroskaTag, MatroskaTrack, EbmlElement>.
Definition: matroskacontainer.h:24
TagParser::MatroskaIds::ContentCompression
Definition: matroskaid.h:179
TagParser::MatroskaIds::ChapProcess
Definition: matroskaid.h:316
TagParser::MatroskaIds::Cluster
Definition: matroskaid.h:22
TagParser::FileElementTraits< EbmlElement >::IdentifierType
std::uint32_t IdentifierType
Definition: ebmlelement.h:27
TagParser::MatroskaIds::TrackJoinBlocks
Definition: matroskaid.h:150
TagParser::MatroskaIds::BlockMore
Definition: matroskaid.h:377
TagParser::MatroskaIds::ChapterAtom
Definition: matroskaid.h:298
TagParser::MatroskaIds::TrackAudio
Definition: matroskaid.h:103
TagParser::MatroskaIds::Slices
Definition: matroskaid.h:370
TagParser::MatroskaIds::Seek
Definition: matroskaid.h:31