From dc0c48a91e2970e192f2c09181ded9a11cee790d Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 24 Aug 2021 21:37:06 +0200 Subject: [PATCH] mp4: Introduce skip_bytes(). We often call one of the read_intX() helpers with a NULL result pointer just to move the file position forward. Calling ->seek() with whence set to SEEK_CUR is simpler and has the advantage that this operation cannot fail. If we happen to seek beyond EOF, the next read will return EOF and we'll abort then. This patch provides the skip_bytes() helper and replaces all read_intX(f, NULL) calls by calls to skip_bytes() and removes the error checking. Due to this cleanup read_int8() and read_int24() and read_u24_be() (the latter being an inline function defined in portable_io.h) have become unused, so remove these as well. --- mp4.c | 140 +++++++++----------------------------------------- portable_io.h | 5 -- 2 files changed, 23 insertions(+), 122 deletions(-) diff --git a/mp4.c b/mp4.c index 08812c89..20aaa2cc 100644 --- a/mp4.c +++ b/mp4.c @@ -90,7 +90,7 @@ static int read_int64(struct mp4 *f, uint64_t *result) uint8_t data[8]; int ret = read_data(f, data, 8); - if (ret > 0 && result) + if (ret > 0) *result = read_u64_be(data); return ret; } @@ -100,41 +100,21 @@ static int read_int32(struct mp4 *f, uint32_t *result) uint8_t data[4]; int ret = read_data(f, data, 4); - if (ret > 0 && result) + if (ret > 0) *result = read_u32_be(data); return ret; } -static int read_int24(struct mp4 *f, uint32_t *result) -{ - uint8_t data[3]; - int ret = read_data(f, data, 3); - - if (ret > 0 && result) - *result = read_u24_be(data); - return ret; -} - static int read_int16(struct mp4 *f, uint16_t *result) { uint8_t data[2]; int ret = read_data(f, data, 2); - if (ret > 0 && result) + if (ret > 0) *result = read_u16_be(data); return ret; } -static uint8_t read_int8(struct mp4 *f, uint8_t *result) -{ - uint8_t data[1]; - int ret = read_data(f, data, 1); - - if (ret > 0 && result) - *result = data[0]; - return ret; -} - #define ATOM_ITEMS \ ATOM_ITEM(MOOV, 'm', 'o', 'o', 'v') \ ATOM_ITEM(TRAK, 't', 'r', 'a', 'k') \ @@ -218,6 +198,11 @@ static void set_position(struct mp4 *f, off_t position) f->cb->seek(f->cb->user_data, position, SEEK_SET); } +static void skip_bytes(struct mp4 *f, off_t num_skip) +{ + f->cb->seek(f->cb->user_data, num_skip, SEEK_CUR); +} + static int read_stsz(struct mp4 *f) { int ret; @@ -227,12 +212,7 @@ static int read_stsz(struct mp4 *f) if (f->total_tracks == 0) return -1; t = f->track[f->total_tracks - 1]; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ ret = read_int32(f, &t->stsz_sample_size); if (ret <= 0) return ret; @@ -261,12 +241,7 @@ static int read_stts(struct mp4 *f) t = f->track[f->total_tracks - 1]; if (t->stts_entry_count) return 0; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ ret = read_int32(f, &t->stts_entry_count); if (ret <= 0) return ret; @@ -276,9 +251,7 @@ static int read_stts(struct mp4 *f) ret = read_int32(f, &t->stts_sample_count[i]); if (ret <= 0) return ret; - ret = read_int32(f, NULL); /* sample delta */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* sample delta */ } return 1; } @@ -293,12 +266,7 @@ static int read_stsc(struct mp4 *f) return -1; t = f->track[f->total_tracks - 1]; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ ret = read_int32(f, &t->stsc_entry_count); if (ret <= 0) return ret; @@ -312,9 +280,7 @@ static int read_stsc(struct mp4 *f) ret = read_int32(f, &t->stsc_samples_per_chunk[i]); if (ret <= 0) return ret; - ret = read_int32(f, NULL); /* sample desc index */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* sample desc index */ } return 1; } @@ -329,12 +295,7 @@ static int read_stco(struct mp4 *f) return -1; t = f->track[f->total_tracks - 1]; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ ret = read_int32(f, &t->stco_entry_count); if (ret <= 0) return ret; @@ -351,39 +312,17 @@ static int read_stco(struct mp4 *f) static int read_mp4a(struct mp4 *f) { int ret; - int32_t i; struct mp4_track *t; if (f->total_tracks == 0) return -1; t = f->track[f->total_tracks - 1]; - - for (i = 0; i < 6; i++) { - ret = read_int8(f, NULL); /* reserved */ - if (ret <= 0) - return ret; - } - ret = read_int16(f, NULL); /* data_reference_index */ - if (ret <= 0) - return ret; - ret = read_int32(f, NULL); /* reserved */ - if (ret <= 0) - return ret; - ret = read_int32(f, NULL); /* reserved */ - if (ret <= 0) - return ret; + /* reserved (6), data reference index (2), reserved (8) */ + skip_bytes(f, 16); ret = read_int16(f, &t->channel_count); if (ret <= 0) return ret; - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; + skip_bytes(f, 6); return read_int16(f, &t->sample_rate); } @@ -396,12 +335,7 @@ static int read_stsd(struct mp4 *f) if (f->total_tracks == 0) return -1; t = f->track[f->total_tracks - 1]; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ ret = read_int32(f, &entry_count); if (ret <= 0) return ret; @@ -456,15 +390,7 @@ static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size) destpos = get_position(f) + subsize - header_size; if (atom_type != ATOM_DATA) continue; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - goto fail; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - goto fail; - ret = read_int32(f, NULL); /* reserved */ - if (ret <= 0) - goto fail; + skip_bytes(f, 8); /* version (1), flags (3), reserved (4) */ ret = -ERRNO_TO_PARA_ERROR(EINVAL); if (subsize < header_size + 8 || subsize > UINT_MAX) goto fail; @@ -505,12 +431,7 @@ static int read_mdhd(struct mp4 *f) if (ret <= 0) return ret; if (version == 1) { - ret = read_int64(f, NULL); /* creation-time */ - if (ret <= 0) - return ret; - ret = read_int64(f, NULL); /* modification-time */ - if (ret <= 0) - return ret; + skip_bytes(f, 16); /* creation time (8), modification time (8) */ ret = read_int32(f, &t->time_scale); if (ret <= 0) return ret; @@ -520,12 +441,7 @@ static int read_mdhd(struct mp4 *f) } else { //version == 0 uint32_t temp; - ret = read_int32(f, NULL); /* creation-time */ - if (ret <= 0) - return ret; - ret = read_int32(f, NULL); /* modification-time */ - if (ret <= 0) - return ret; + skip_bytes(f, 8); /* creation time (4), modification time (4) */ ret = read_int32(f, &t->time_scale); if (ret <= 0) return ret; @@ -535,12 +451,7 @@ static int read_mdhd(struct mp4 *f) t->duration = (temp == (uint32_t) (-1))? (uint64_t) (-1) : (uint64_t) (temp); } - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; + skip_bytes(f, 4); return 1; } @@ -580,12 +491,7 @@ static int32_t read_meta(struct mp4 *f, uint64_t size) uint8_t atom_type; uint8_t header_size = 0; - ret = read_int8(f, NULL); /* version */ - if (ret <= 0) - return ret; - ret = read_int24(f, NULL); /* flags */ - if (ret <= 0) - return ret; + skip_bytes(f, 4); /* version (1), flags (3) */ while (sumsize < (size - (header_size + 4))) { ret = atom_read_header(f, &atom_type, &header_size, &subsize); if (ret <= 0) diff --git a/portable_io.h b/portable_io.h index 81dfcf57..fdd4165d 100644 --- a/portable_io.h +++ b/portable_io.h @@ -58,11 +58,6 @@ static inline uint32_t read_u32_be(const void *buf) return read_portable_be(32, buf); } -static inline uint32_t read_u24_be(const void *buf) -{ - return read_portable_be(24, buf); -} - static inline uint16_t read_u16_be(const void *buf) { return read_portable_be(16, buf); -- 2.39.2