]> git.tue.mpg.de Git - paraslash.git/commitdiff
mp4: Introduce min_body_size() helper.
authorAndre Noll <maan@tuebingen.mpg.de>
Mon, 9 Aug 2021 19:08:16 +0000 (21:08 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
This increases readability a lot and brings the line length of
mp4ff_parse_tag() down to a reasonable number of characters.

mp4.c

diff --git a/mp4.c b/mp4.c
index 80acf41ac833cc147ad38ac3047aca422b5a96fc..e66fcea2e6ad79adb0e2e09071df20bf853f3bdb 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -952,6 +952,29 @@ static int32_t mp4ff_set_metadata_name(const uint8_t atom_type, char **name)
        return 0;
 }
 
+static uint32_t min_body_size(const uint8_t atom_type)
+{
+       switch(atom_type) {
+       case ATOM_GENRE2:
+       case ATOM_TEMPO:
+               return 10;
+       case ATOM_TRACK:
+               return sizeof (char) /* version */
+                       + sizeof(uint8_t) * 3 /* flags */
+                       + sizeof(uint32_t) /* reserved */
+                       + sizeof(uint16_t) /* leading uint16_t */
+                       + sizeof(uint16_t) /* track */
+                       + sizeof(uint16_t); /* totaltracks */
+       case ATOM_DISC:
+               return sizeof (char) /* version */
+                       + sizeof(uint8_t) * 3 /* flags */
+                       + sizeof(uint32_t) /* reserved */
+                       + sizeof(uint16_t) /* disc */
+                       + sizeof(uint16_t); /* totaldiscs */
+       default: assert(false);
+       }
+}
+
 static int32_t mp4ff_parse_tag(mp4ff_t * f, const uint8_t parent,
                const int32_t size)
 {
@@ -988,45 +1011,42 @@ static int32_t mp4ff_parse_tag(mp4ff_t * f, const uint8_t parent,
 
                /* some need special attention */
                if (parent == ATOM_GENRE2 || parent == ATOM_TEMPO) {
-                       if (subsize - header_size >= 8 + 2) {
-                               uint16_t val = mp4ff_read_int16(f);
-
-                               if (parent == ATOM_TEMPO) {
-                                       char temp[16];
-                                       sprintf(temp, "%.5u BPM", val);
-                                       mp4ff_tag_add_field(&(f-> tags), "tempo", temp, -1);
-                               } else {
-                                       const char *temp = mp4ff_meta_index_to_genre(val);
-                                       if (temp)
-                                               mp4ff_tag_add_field (&(f->tags), "genre", temp, -1);
-                               }
-                               done = 1;
+                       uint16_t val;
+                       if (subsize - header_size < min_body_size(parent))
+                               continue;
+                       val = mp4ff_read_int16(f);
+                       if (parent == ATOM_TEMPO) {
+                               char temp[16];
+                               sprintf(temp, "%.5u BPM", val);
+                               mp4ff_tag_add_field(&(f-> tags), "tempo",
+                                       temp, -1);
+                       } else {
+                               const char *tmp = mp4ff_meta_index_to_genre(val);
+                               if (tmp)
+                                       mp4ff_tag_add_field (&(f->tags),
+                                               "genre", tmp, -1);
                        }
+                       done = 1;
                } else if (parent == ATOM_TRACK || parent == ATOM_DISC) {
-                       if (subsize - header_size >= (sizeof (char) + sizeof (uint8_t) * 3 + sizeof (uint32_t) +        /* version + flags + reserved */
-                                                                +(parent == ATOM_TRACK ? sizeof (uint16_t) : 0)        /* leading uint16_t if ATOM_TRACK */
-                                                                +sizeof (uint16_t)     /* track / disc */
-                                                                +sizeof (uint16_t))    /* totaltracks / totaldiscs */) {
-                               uint16_t index, total;
-                               char temp[32];
+                       uint16_t index, total;
+                       char temp[32];
+                       if (subsize - header_size < min_body_size(parent))
+                               continue;
+                       mp4ff_read_int16(f);
+                       index = mp4ff_read_int16(f);
+                       total = mp4ff_read_int16(f);
+                       if (parent == ATOM_TRACK)
                                mp4ff_read_int16(f);
-                               index = mp4ff_read_int16(f);
-                               total = mp4ff_read_int16(f);
-                               if (parent == ATOM_TRACK)
-                                       mp4ff_read_int16(f);
-
-                               sprintf(temp, "%d", index);
-                               mp4ff_tag_add_field(&(f->tags), parent == ATOM_TRACK ?
-                                       "track" : "disc", temp, -1);
-                               if (total > 0) {
-                                       sprintf(temp, "%d",
-                                               total);
-                                       mp4ff_tag_add_field(& (f-> tags),
-                                               parent == ATOM_TRACK?
-                                               "totaltracks" : "totaldiscs", temp, -1);
-                               }
-                               done = 1;
+                       sprintf(temp, "%d", index);
+                       mp4ff_tag_add_field(&(f->tags), parent == ATOM_TRACK?
+                               "track" : "disc", temp, -1);
+                       if (total > 0) {
+                               sprintf(temp, "%d", total);
+                               mp4ff_tag_add_field(& (f-> tags),
+                                       parent == ATOM_TRACK?
+                                       "totaltracks" : "totaldiscs", temp, -1);
                        }
+                       done = 1;
                } else {
                        free(data);
                        data = mp4ff_read_string(f, subsize - (header_size + 8));