Navigation Menu

Skip to content

Commit

Permalink
mp3utils.c: removed unnecessary file rewind calls in tag detection code.
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Dec 10, 2019
1 parent abd85ad commit a961f56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
53 changes: 25 additions & 28 deletions src/codecs/mp3utils.c
Expand Up @@ -149,7 +149,6 @@ static SDL_INLINE long get_lyrics3v1_len(struct mp3file_t *m) {
len = (m->length > 5109)? 5109 : (long)m->length;
MP3_RWseek(m, -len, RW_SEEK_END);
MP3_RWread(m, buf, 1, (len -= 9)); /* exclude footer */
MP3_RWseek(m, 0, RW_SEEK_SET);
/* strstr() won't work here. */
for (i = len - 11, p = buf; i >= 0; --i, ++p) {
if (SDL_memcmp(p, "LYRICSBEGIN", 11) == 0)
Expand All @@ -174,81 +173,79 @@ int mp3_skiptags(struct mp3file_t *fil)
{
unsigned char buf[128];
long len; size_t readsize;
int rc = -1;

readsize = MP3_RWread(fil, buf, 1, 128);
if (!readsize) return -1;
if (!readsize) goto fail;

/* ID3v2 tag is at the start */
if (is_id3v2(buf, readsize)) {
len = get_id3v2_len(buf, (long)readsize);
if (len >= fil->length) return -1;
if (len >= fil->length) goto fail;
fil->start += len;
fil->length -= len;
MP3_RWseek(fil, 0, RW_SEEK_SET);
}
/* APE tag _might_ be at the start (discouraged
* but not forbidden, either.) read the header. */
else if (is_apetag(buf, readsize)) {
len = get_ape_len(buf);
if (len >= fil->length) return -1;
if (len >= fil->length) goto fail;
fil->start += len;
fil->length -= len;
MP3_RWseek(fil, 0, RW_SEEK_SET);
}

/* ID3v1 tag is at the end */
if (fil->length < 128) goto ape;
MP3_RWseek(fil, -128, RW_SEEK_END);
readsize = MP3_RWread(fil, buf, 1, 128);
MP3_RWseek(fil, 0, RW_SEEK_SET);
if (readsize != 128) return -1;
if (is_id3v1(buf, 128)) {
fil->length -= 128;

/* FIXME: handle possible double-ID3v1 tags?? */
if (fil->length >= 128) {
MP3_RWseek(fil, -128, RW_SEEK_END);
readsize = MP3_RWread(fil, buf, 1, 128);
if (readsize != 128) goto fail;
if (is_id3v1(buf, 128)) {
fil->length -= 128;
/* FIXME: handle possible double-ID3v1 tags?? */
}
}

/* do we know whether ape or lyrics3 is the first?
* well, we don't: we need to handle that later... */

ape: /* APE tag may be at the end: read the footer */
/* APE tag may be at the end: read the footer */
if (fil->length >= 32) {
MP3_RWseek(fil, -32, RW_SEEK_END);
readsize = MP3_RWread(fil, buf, 1, 32);
MP3_RWseek(fil, 0, RW_SEEK_SET);
if (readsize != 32) return -1;
if (readsize != 32) goto fail;
if (is_apetag(buf, 32)) {
len = get_ape_len(buf);
if (len >= fil->length) return -1;
if (len >= fil->length) goto fail;
fil->length -= len;
}
}

if (fil->length >= 15) {
MP3_RWseek(fil, -15, RW_SEEK_END);
readsize = MP3_RWread(fil, buf, 1, 15);
MP3_RWseek(fil, 0, RW_SEEK_SET);
if (readsize != 15) return -1;
if (readsize != 15) goto fail;
len = is_lyrics3tag(buf, 15);
if (len == 2) {
len = get_lyrics3v2_len(buf, 6);
if (len >= fil->length) return -1;
if (len < 15) return -1;
if (len >= fil->length) goto fail;
if (len < 15) goto fail;
MP3_RWseek(fil, -len, RW_SEEK_END);
readsize = MP3_RWread(fil, buf, 1, 11);
MP3_RWseek(fil, 0, RW_SEEK_SET);
if (readsize != 11) return -1;
if (!verify_lyrics3v2(buf, 11)) return -1;
if (readsize != 11) goto fail;
if (!verify_lyrics3v2(buf, 11)) goto fail;
fil->length -= len;
}
else if (len == 1) {
len = get_lyrics3v1_len(fil);
if (len < 0) return -1;
if (len < 0) goto fail;
fil->length -= len;
}
}

return (fil->length > 0)? 0: -1;
rc = 0;
fail:
MP3_RWseek(fil, 0, RW_SEEK_SET);
return (fil->length > 0)? rc : -1;
}
#endif /* MUSIC_MP3_????? */

Expand Down
2 changes: 1 addition & 1 deletion src/codecs/music_mad.c
Expand Up @@ -169,7 +169,7 @@ static void *MAD_CreateFromRW(SDL_RWops *src, int freesrc)
music->mp3file.length = SDL_RWsize(src);
if (mp3_skiptags(&music->mp3file) < 0) {
SDL_free(music);
Mix_SetError("music_mad: corrupt mp3 file.");
Mix_SetError("music_mad: corrupt mp3 file (bad tags.)");
return NULL;
}

Expand Down

0 comments on commit a961f56

Please sign in to comment.