Tag Parser  10.0.1
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 #include <string_view>
16 
17 namespace TagParser {
18 
19 class EbmlElement;
20 class MatroskaContainer;
21 
26 public:
28  using IdentifierType = std::uint32_t;
29  using DataSizeType = std::uint64_t;
30 };
31 
32 class TAG_PARSER_EXPORT EbmlElement : public GenericFileElement<EbmlElement> {
33  friend class GenericFileElement<EbmlElement>;
34 
35 public:
36  EbmlElement(MatroskaContainer &container, std::uint64_t startOffset);
37 
38  std::string idToString() const;
39  bool isParent() const;
40  bool isPadding() const;
41  std::uint64_t firstChildOffset() const;
42  std::string readString();
43  std::uint64_t readUInteger();
44  double readFloat();
45 
46  static std::uint8_t calculateIdLength(IdentifierType id);
47  static std::uint8_t calculateSizeDenotationLength(std::uint64_t size);
48  static std::uint8_t makeId(IdentifierType id, char *buff);
49  static std::uint8_t makeSizeDenotation(std::uint64_t size, char *buff);
50  static std::uint8_t makeSizeDenotation(std::uint64_t size, char *buff, std::uint8_t minBytes);
51  static std::uint8_t calculateUIntegerLength(std::uint64_t integer);
52  static std::uint8_t makeUInteger(std::uint64_t value, char *buff);
53  static std::uint8_t makeUInteger(std::uint64_t value, char *buff, std::uint8_t minBytes);
54  static void makeSimpleElement(std::ostream &stream, IdentifierType id, std::uint64_t content);
55  static void makeSimpleElement(std::ostream &stream, IdentifierType id, std::string_view content);
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  if (const auto name = matroskaIdName(id()); !name.empty()) {
75  return argsToString('0', 'x', numberToString(id(), 16u), ' ', '\"', name, '\"');
76  } else {
77  return "0x" + numberToString(id(), 16u);
78  }
79 }
80 
87 inline bool EbmlElement::isParent() const
88 {
89  using namespace EbmlIds;
90  using namespace MatroskaIds;
91  switch (id()) {
92  case Header:
93  case SignatureSlot:
94  case SignatureElements:
96  case Segment:
97  case SeekHead:
98  case Seek:
99  case SegmentInfo:
100  case ChapterTranslate:
101  case Cluster:
102  case SilentTracks:
103  case BlockGroup:
104  case BlockAdditions:
105  case BlockMore:
106  case Slices:
107  case TimeSlice:
108  case ReferenceFrame:
109  case Tracks:
110  case TrackEntry:
111  case TrackTranslate:
112  case TrackVideo:
113  case TrackAudio:
114  case TrackOperation:
115  case TrackCombinePlanes:
116  case TrackPlane:
117  case TrackJoinBlocks:
118  case ContentEncodings:
119  case ContentEncoding:
120  case ContentCompression:
121  case ContentEncryption:
122  case Cues:
123  case CuePoint:
124  case CueTrackPositions:
125  case CueReference:
126  case Attachments:
127  case AttachedFile:
128  case Chapters:
129  case EditionEntry:
130  case ChapterAtom:
131  case ChapterTrack:
132  case ChapterDisplay:
133  case ChapProcess:
134  case ChapProcessCommand:
135  case Tags:
136  case MatroskaIds::Tag:
137  case Targets:
138  case SimpleTag:
139  return true;
140  default:
141  return false;
142  }
143 }
144 
148 inline bool EbmlElement::isPadding() const
149 {
150  return id() == EbmlIds::Void;
151 }
152 
157 inline std::uint64_t EbmlElement::firstChildOffset() const
158 {
159  return isParent() ? (idLength() + sizeLength()) : 0;
160 }
161 
162 } // namespace TagParser
163 
164 #endif // TAG_PARSER_EBMLELEMENT_H
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
The EbmlElement class helps to parse EBML files such as Matroska files.
Definition: ebmlelement.h:32
static void makeSimpleElement(std::ostream &stream, IdentifierType id, std::string_view content)
std::uint64_t firstChildOffset() const
Returns the offset of the first child of the element.
Definition: ebmlelement.h:157
bool isPadding() const
Returns an indication whether the element is considered as padding.
Definition: ebmlelement.h:148
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
std::string idToString() const
Converts the specified EBML ID to a printable string.
Definition: ebmlelement.h:71
bool isParent() const
Returns an indication whether the element is a parent element.
Definition: ebmlelement.h:87
Defines traits for the specified ImplementationType.
The GenericFileElement class helps to parse binary files which consist of an arboreal element structu...
const IdentifierType & id() const
Returns the element ID.
std::uint32_t sizeLength() const
Returns the length of the size denotation of the element in byte.
std::uint32_t idLength() const
Returns the length of the id denotation in byte.
Implementation of GenericContainer<MediaFileInfo, MatroskaTag, MatroskaTrack, EbmlElement>.
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
@ SignatureElementList
Definition: ebmlid.h:38
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TAG_PARSER_EXPORT std::string_view matroskaIdName(std::uint32_t matroskaId)
Returns a string for the specified matroskaId if known; otherwise returns an empty string.
Definition: matroskaid.cpp:22