Tag Parser 12.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Loading...
Searching...
No Matches
signature.cpp
Go to the documentation of this file.
1#include "./signature.h"
3
4#include <c++utilities/conversion/binaryconversion.h>
5
6#include <cstdint>
7
8using namespace CppUtilities;
9
10namespace TagParser {
11
15enum Sig64 : std::uint64_t {
16 ApeTag = 0x4150455441474558ul, // APETAGEX
17 Ar = 0x213C617263683E0A,
18 Asf1 = 0x3026B2758E66CF11ul,
19 Asf2 = 0xA6D900AA0062CE6Cul,
20 Png = 0x89504E470D0A1A0Aul,
21 RiffAvi = 0x415649204C495354ul,
22 YUV4Mpeg2 = 0x595556344D504547ul,
23};
24
28enum Sig56 : std::uint64_t {
29 Rar = 0x526172211A0700ul,
30};
31
35enum Sig48 : std::uint64_t {
36 Gif87a = 0x474946383761ul,
37 Gif89a = 0x474946383961ul,
38 SevenZ = 0x377ABCAF271Cul,
39 Xz = 0xFD377A585A00ul,
40};
41
45enum Sig40 : std::uint64_t {
46 Aiff = 0x464F524D00ul,
47};
48
52enum Sig32 : std::uint32_t {
53 Dirac = 0x42424344u,
54 Elf = 0x7F454C46u,
55 Flac = 0x664C6143u,
56 Ivf = 0x444B4946u,
57 JavaClassFile = 0xCAFEBABEu,
58 Ebml = 0x1A45DFA3u,
59 Midi = 0x4D546864u,
60 MonkeysAudio = 0x4D414320u,
61 Mp4 = 0x66747970u,
62 Ogg = 0x4F676753u,
63 PhotoshopDocument = 0x38425053u,
64 QuickTime = 0x6D6F6F76u,
65 Riff = 0x52494646u,
66 RiffWave = 0x57415645u,
67 TiffBigEndian = 0x4D4D002Au,
68 TiffLittleEndian = 0x49492A00u,
69 Utf32Text = 0xFFFE0000u,
70 WavPack = 0x7776706Bu,
71 WindowsIcon = 0x00000100u,
72 Lzip = 0x4C5A4950u,
73 Zip1 = 0x504B0304u,
74 Zip2 = 0x504B0506u,
75 Zip3 = 0x504B0708u,
76 Zstd = 0x28b52ffdu,
77};
78
82enum Sig24 : std::uint32_t {
83 Bzip2 = 0x425A68u,
84 Flv = 0x464C56u,
85 Gzip = 0x1F8B08u,
86 Id3v2 = 0x494433u,
87 Utf8Text = 0xEFBBBFu,
88};
89
93enum Sig16 : std::uint16_t {
94 Ac3 = 0x0B77u,
95 Adts = 0xFFF0u,
96 AdtsMask = 0xFFF6u,
97 Jpeg = 0xffd8u,
98 Lha = 0x1FA0u,
99 Lzw = 0x1F9Du,
102 Utf16Text = 0xFFFEu,
103 WindowsBitmap = 0x424du,
104};
105
113ContainerFormat parseSignature(std::string_view buffer)
114{
115 // read signature
116 std::uint64_t sig = 0;
117 if (buffer.size() >= 8) {
118 sig = BE::toInt<std::uint64_t>(buffer.data());
119 } else if (buffer.size() >= 4) {
120 sig = BE::toInt<std::uint32_t>(buffer.data());
121 sig <<= 4;
122 } else if (buffer.size() >= 2) {
123 sig = BE::toInt<std::uint16_t>(buffer.data());
124 sig <<= 6;
125 } else {
127 }
128 // return corresponding container format
129 switch (sig) { // check 64-bit signatures
130 case ApeTag:
132 case Ar:
133 return ContainerFormat::Ar;
134 case Asf1:
136 case Asf2:
138 case Png:
140 case YUV4Mpeg2:
141 if (buffer.size() >= 10 && buffer[8] == 0x32 && buffer[9] == 0x20) {
143 }
144 break;
145 default:;
146 }
147 switch (sig & 0x00000000FFFFFFFF) { // check 32-bit signatures @ bit 31
148 case Mp4:
150 case QuickTime:
152 default:;
153 }
154 switch (sig >> 8) { // check 56-bit signatures
155 case Rar:
157 default:;
158 }
159 switch (sig >> 16) { // check 48-bit signatures
160 case Gif87a:
162 case Gif89a:
164 case SevenZ:
166 case Xz:
167 return ContainerFormat::Xz;
168 default:;
169 }
170 switch (sig >> 24) { // check 40-bit signatures
171 case Aiff:
173 default:;
174 }
175 switch (sig >> 32) { // check 32-bit signatures
176 case Dirac:
178 case Elf:
180 case Flac:
182 case Ivf:
184 case JavaClassFile:
186 case Ebml:
188 case Midi:
190 case MonkeysAudio:
192 case Ogg:
196 case Riff:
197 if (buffer.size() >= 16 && BE::toInt<std::uint64_t>(buffer.data() + 8) == Sig64::RiffAvi) {
199 } else if (buffer.size() >= 12 && BE::toInt<std::uint32_t>(buffer.data() + 8) == RiffWave) {
201 } else {
203 }
204 case TiffBigEndian:
206 case TiffLittleEndian:
208 case Utf32Text:
210 case WavPack:
212 case WindowsIcon:
214 case Lzip:
216 case Zip1:
217 case Zip2:
218 case Zip3:
220 case Zstd:
222 default:;
223 }
224 switch (sig >> 40) { // check 24-bit signatures
225 case Bzip2:
227 case Flv:
229 case Gzip:
231 case Id3v2:
233 case Utf8Text:
235 }
236 switch (sig >> 48) { // check 16-bit signatures
237 case Ac3:
239 case Jpeg:
241 case Lha:
243 case Lzw:
247 case Utf16Text:
249 case WindowsBitmap:
251 default:;
252 }
253 // check other signatures
254 if (((sig >> 48) & AdtsMask) == Adts) {
256 }
257 if ((sig >> 53) == MpegAudioFrames) {
259 }
261}
262
269std::string_view containerFormatAbbreviation(ContainerFormat containerFormat, MediaType mediaType, unsigned int version)
270{
271 switch (containerFormat) {
273 return "ac3";
275 return "a";
277 return "asf";
279 return "drc";
281 return "elf";
283 return "flac";
285 return "flv";
288 return "gif";
290 return "ivf";
292 return "class";
294 return "jpeg";
296 return "lzh";
298 return "lzw";
300 switch (mediaType) {
301 case MediaType::Audio:
302 return "m4a";
303 default:
304 return "mp4";
305 }
307 switch (mediaType) {
308 case MediaType::Video:
309 return "ogv";
310 default:
311 switch (version) {
312 case static_cast<unsigned int>(GeneralMediaFormat::Opus):
313 return "opus";
314 case static_cast<unsigned int>(GeneralMediaFormat::Speex):
315 return "spx";
316 default:
317 return "ogg";
318 }
319 }
321 return "psd";
323 return "png";
325 return "exe";
327 return "rar";
329 switch (mediaType) {
330 case MediaType::Audio:
331 return "mka";
332 default:
333 return "mkv";
334 }
336 switch (version) {
337 case 1:
338 return "mp1";
339 case 2:
340 return "mp2";
341 default:
342 return "mp3";
343 }
345 return "riff";
347 return "wav";
349 return "avi";
351 return "tar";
354 return "tiff";
356 return "bmp";
358 return "ico";
360 return "bz";
362 return "gz";
364 return "lz";
366 return "mov";
368 return "zip";
370 return "7z";
372 return "xz";
374 return "y4m";
376 return "wv";
378 return "ape";
380 return "mid";
382 return "aiff";
384 return "zst";
385 default:
386 return "";
387 }
388}
389
395std::string_view containerFormatName(ContainerFormat containerFormat)
396{
397 switch (containerFormat) {
399 return "raw Dolby Digital";
401 return "Audio Data Transport Stream";
403 return "Archive (GNU ar)";
405 return "Advanced Systems Format";
407 return "raw Dirac";
409 return "Executable and Linkable Format";
411 return "raw Free Lossless Audio Codec frames";
413 return "Flash Video";
416 return "Graphics Interchange Format";
418 return "IVF";
420 return "Java class file";
422 return "JPEG File Interchange Format";
424 return "LHA compressed file";
426 return "LZW compressed file";
428 return "MPEG-4 Part 14";
430 return "Ogg transport bitstream";
432 return "Photoshop document";
434 return "Portable Network Graphics";
436 return "Portable Executable";
438 return "RAR Archive";
440 return "EBML";
442 return "Matroska";
444 return "WebM";
446 return "MPEG-1 Layer 1/2/3 frames";
448 return "Resource Interchange File Format";
450 return "RIFF/WAVE";
452 return "RIFF/Audio Video Interleave";
454 return "TAR archive";
457 return "Tagged Image File Format";
459 return "UTF-16 text";
461 return "UTF-32 text";
463 return "UTF-8 text";
465 return "WavPack";
467 return "Microsoft Windows Bitmap";
469 return "Microsoft Windows Icon";
471 return "bzip2 compressed file";
473 return "gzip compressed file";
475 return "lzip compressed file";
477 return "7z archive";
479 return "Quick Time";
481 return "xz compressed file";
483 return "YUV4MPEG2";
485 return "ZIP archive";
487 return "Monkey's Audio";
489 return "MIDI";
491 return "Audio Interchange File Format";
493 return "Zstandard compressed file";
495 return "ID3v2 tag";
497 return "APE tag";
498 default:
499 return "unknown";
500 }
501}
502
508std::string_view containerFormatSubversion(ContainerFormat containerFormat)
509{
510 switch (containerFormat) {
512 return "87a";
514 return "89a";
516 return "big endian";
518 return "little endian";
519 default:
520 return "";
521 }
522}
523
529std::string_view containerMimeType(ContainerFormat containerFormat, MediaType mediaType)
530{
531 switch (containerFormat) {
533 return "audio/ac3";
535 return "video/x-ms-asf";
537 return "audio/flac";
539 return "video/x-flv";
542 return "image/gif";
544 return "image/jpeg";
546 return "image/png";
548 return "audio/mpeg";
550 switch (mediaType) {
551 case MediaType::Audio:
552 return "audio/mp4";
553 default:
554 return "video/mp4";
555 }
557 switch (mediaType) {
558 case MediaType::Audio:
559 return "audio/ogg";
560 default:
561 return "video/ogg";
562 }
564 switch (mediaType) {
565 case MediaType::Audio:
566 return "audio/x-matroska";
567 default:
568 return "video/x-matroska";
569 }
571 return "audio/midi";
573 return "application/x-bzip";
575 return "application/gzip";
577 return "application/x-lzh-compressed";
579 return "application/x-rar-compressed";
581 return "application/x-lzip";
583 return "video/quicktime";
585 return "application/zip";
587 return "application/x-7z-compressed";
589 return "application/x-xz";
591 return "image/bmp";
593 return "image/vnd.microsoft.icon";
595 return "application/zstd";
596 default:
597 return "";
598 }
599}
600
604TagTargetLevel containerTargetLevel(ContainerFormat containerFormat, std::uint64_t targetLevelValue)
605{
606 switch (containerFormat) {
609 return matroskaTagTargetLevel(targetLevelValue);
610 default:
612 }
613}
614
618std::uint64_t containerTargetLevelValue(ContainerFormat containerFormat, TagTargetLevel targetLevel)
619{
620 switch (containerFormat) {
623 return matroskaTagTargetLevelValue(targetLevel);
624 default:
625 return 0;
626 }
627}
628
629} // namespace TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TAG_PARSER_EXPORT std::string_view containerFormatSubversion(ContainerFormat containerFormat)
Returns the subversion of the container format as C-style string.
Sig16
Holds 16-bit signatures.
Definition signature.cpp:93
@ PortableExecutable
TagTargetLevel
The TagTargetLevel enum specifies tag target levels.
Definition tagtarget.h:16
TAG_PARSER_EXPORT std::string_view containerFormatAbbreviation(ContainerFormat containerFormat, MediaType mediaType=MediaType::Unknown, unsigned int version=0)
Returns the abbreviation of the container format as C-style string considering the specified media ty...
Sig24
Holds 24-bit signatures.
Definition signature.cpp:82
Sig48
Holds 48-bit signatures.
Definition signature.cpp:35
constexpr TAG_PARSER_EXPORT std::uint64_t matroskaTagTargetLevelValue(TagTargetLevel targetLevel)
Returns the Matroska specific target level value for the specified general targetLevel.
TAG_PARSER_EXPORT std::string_view containerFormatName(ContainerFormat containerFormat)
Returns the name of the specified container format as C-style string.
TAG_PARSER_EXPORT ContainerFormat parseSignature(const char *buffer, std::size_t bufferSize)
Definition signature.h:83
TAG_PARSER_EXPORT std::string_view containerMimeType(ContainerFormat containerFormat, MediaType mediaType=MediaType::Unknown)
Returns the MIME-type of the container format as C-style string.
constexpr TAG_PARSER_EXPORT TagTargetLevel matroskaTagTargetLevel(std::uint64_t targetLevelValue)
Returns the general TagTargetLevel for the Matroska specific targetLevelValue.
Sig56
Holds 52-bit signatures.
Definition signature.cpp:28
TAG_PARSER_EXPORT TagTargetLevel containerTargetLevel(ContainerFormat containerFormat, std::uint64_t targetLevelValue)
Returns the general TagTargetLevel for the specified container format and raw targetLevelValue.
Sig32
Holds 32-bit signatures.
Definition signature.cpp:52
@ PhotoshopDocument
Definition signature.cpp:63
@ TiffLittleEndian
Definition signature.cpp:68
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition mediaformat.h:14
Sig64
Holds 64-bit signatures.
Definition signature.cpp:15
TAG_PARSER_EXPORT std::uint64_t containerTargetLevelValue(ContainerFormat containerFormat, TagTargetLevel targetLevel)
Returns the raw target level value for the specified containerFormat and general targetLevel.
ContainerFormat
Specifies the container format.
Definition signature.h:18
Sig40
Holds 40-bit signatures.
Definition signature.cpp:45