Skip to content

Commit af238d9

Browse files
committed
Move MKV element magic values into proper #define constants.
1 parent d394a24 commit af238d9

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/format_ebml.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@
4242
/* A value that no EBML var-int is allowed to take. */
4343
#define EBML_UNKNOWN ((unsigned long long) -1)
4444

45+
/* The magic numbers for each element we are interested in.
46+
* Defined here:
47+
* http://www.matroska.org/technical/specs/index.html
48+
* http://www.webmproject.org/docs/container/
49+
*
50+
* Some of the higher-level elements have 4-byte identifiers;
51+
* The lower-level elements have 1-byte identifiers.
52+
*/
53+
#define UNCOMMON_MAGIC_LEN 4
54+
55+
#define SEGMENT_MAGIC "\x18\x53\x80\x67"
56+
#define CLUSTER_MAGIC "\x1F\x43\xB6\x75"
57+
#define TRACKS_MAGIC "\x16\x54\xAE\x6B"
58+
59+
#define COMMON_MAGIC_LEN 1
60+
61+
#define TRACK_ENTRY_MAGIC "\xAE"
62+
#define TRACK_NUMBER_MAGIC "\xD7"
63+
#define TRACK_TYPE_MAGIC "\x83"
64+
#define SIMPLE_BLOCK_MAGIC "\xA3"
65+
4566
typedef enum ebml_read_mode {
4667
EBML_STATE_READING_HEADER = 0,
4768
EBML_STATE_READING_CLUSTERS
@@ -550,14 +571,6 @@ static int ebml_wrote(ebml_t *ebml, int len)
550571
unsigned char flags;
551572
int copy_state;
552573

553-
char *segment_id = "\x18\x53\x80\x67";
554-
char *cluster_id = "\x1F\x43\xB6\x75";
555-
char *tracks_id = "\x16\x54\xAE\x6B";
556-
char *track_entry_id = "\xAE";
557-
char *track_number_id = "\xD7";
558-
char *track_type_id = "\x83";
559-
char *simple_block_id = "\xA3";
560-
561574
ebml->input_position += len;
562575
end_of_buffer = ebml->input_buffer + ebml->input_position;
563576

@@ -587,25 +600,25 @@ static int ebml_wrote(ebml_t *ebml, int len)
587600
}
588601

589602
/* Recognize tags of interest */
590-
if (tag_length > 4) {
591-
if (!memcmp(ebml->input_buffer + cursor, cluster_id, 4)) {
603+
if (tag_length > UNCOMMON_MAGIC_LEN) {
604+
if (!memcmp(ebml->input_buffer + cursor, CLUSTER_MAGIC, UNCOMMON_MAGIC_LEN)) {
592605
/* Found a Cluster */
593606
ebml->parse_state = EBML_STATE_START_CLUSTER;
594607
break;
595-
} else if (!memcmp(ebml->input_buffer + cursor, segment_id, 4)) {
608+
} else if (!memcmp(ebml->input_buffer + cursor, SEGMENT_MAGIC, UNCOMMON_MAGIC_LEN)) {
596609
/* Parse all Segment children */
597610
payload_length = 0;
598611

599-
} else if (!memcmp(ebml->input_buffer + cursor, tracks_id, 4)) {
612+
} else if (!memcmp(ebml->input_buffer + cursor, TRACKS_MAGIC, UNCOMMON_MAGIC_LEN)) {
600613
/* Parse all Tracks children */
601614
payload_length = 0;
602615

603616
}
604617

605618
}
606619

607-
if (tag_length > 1) {
608-
if (!memcmp(ebml->input_buffer + cursor, simple_block_id, 1)) {
620+
if (tag_length > COMMON_MAGIC_LEN) {
621+
if (!memcmp(ebml->input_buffer + cursor, SIMPLE_BLOCK_MAGIC, COMMON_MAGIC_LEN)) {
609622
/* Probe SimpleBlock header for the keyframe status */
610623
if (ebml->cluster_starts_with_keyframe == EBML_KEYFRAME_UNKNOWN) {
611624
track_number_length = ebml_parse_var_int(ebml->input_buffer + cursor + tag_length,
@@ -640,13 +653,13 @@ static int ebml_wrote(ebml_t *ebml, int len)
640653

641654
}
642655

643-
} else if (!memcmp(ebml->input_buffer + cursor, track_entry_id, 1)) {
656+
} else if (!memcmp(ebml->input_buffer + cursor, TRACK_ENTRY_MAGIC, COMMON_MAGIC_LEN)) {
644657
/* Parse all TrackEntry children; reset the state */
645658
payload_length = 0;
646659
ebml->parsing_track_number = EBML_UNKNOWN;
647660
ebml->parsing_track_is_video = 0;
648661

649-
} else if (!memcmp(ebml->input_buffer + cursor, track_number_id, 1)) {
662+
} else if (!memcmp(ebml->input_buffer + cursor, TRACK_NUMBER_MAGIC, COMMON_MAGIC_LEN)) {
650663
/* Probe TrackNumber for value */
651664
value_length = ebml_parse_sized_int(ebml->input_buffer + cursor + tag_length,
652665
end_of_buffer, payload_length, 0, &data_value);
@@ -661,7 +674,7 @@ static int ebml_wrote(ebml_t *ebml, int len)
661674
ebml_check_track(ebml);
662675
}
663676

664-
} else if (!memcmp(ebml->input_buffer + cursor, track_type_id, 1)) {
677+
} else if (!memcmp(ebml->input_buffer + cursor, TRACK_TYPE_MAGIC, COMMON_MAGIC_LEN)) {
665678
/* Probe TrackType for a video flag */
666679
value_length = ebml_parse_sized_int(ebml->input_buffer + cursor + tag_length,
667680
end_of_buffer, payload_length, 0, &data_value);

0 commit comments

Comments
 (0)