diff options
author | Magnus Holmgren <magnushol@gmail.com> | 2007-10-13 11:21:10 +0000 |
---|---|---|
committer | Magnus Holmgren <magnushol@gmail.com> | 2007-10-13 11:21:10 +0000 |
commit | 5fc117ea4e1aedf820a9787e33f208b489a31407 (patch) | |
tree | 06d86e25af3ca4bdf0bf6a2ec26d20d7de249495 /apps/metadata/flac.c | |
parent | c9bd970ad66ebef13db3d41bbfd466e37e08020c (diff) |
Make the FLAC metadata parser a little more robust.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15093 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/metadata/flac.c')
-rw-r--r-- | apps/metadata/flac.c | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/apps/metadata/flac.c b/apps/metadata/flac.c index 5b3644ede2..f2626d7950 100644 --- a/apps/metadata/flac.c +++ b/apps/metadata/flac.c @@ -38,6 +38,7 @@ bool get_flac_metadata(int fd, struct mp3entry* id3) /* Use the trackname part of the id3 structure as a temporary buffer */ unsigned char* buf = (unsigned char *)id3->path; + bool last_metadata = false; bool rc = false; if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4)) @@ -50,24 +51,26 @@ bool get_flac_metadata(int fd, struct mp3entry* id3) return rc; } - while (true) + while (!last_metadata) { - long i; + unsigned long i; + int type; if (read(fd, buf, 4) < 0) { return rc; } + last_metadata = buf[0] & 0x80; + type = buf[0] & 0x7f; /* The length of the block */ i = (buf[1] << 16) | (buf[2] << 8) | buf[3]; - if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ + if (type == 0) /* 0 is the STREAMINFO block */ { unsigned long totalsamples; - - /* FIXME: Don't trust the value of i */ - if (read(fd, buf, i) < 0) + + if (i >= sizeof(id3->path) || read(fd, buf, i) < 0) { return rc; } @@ -92,7 +95,7 @@ bool get_flac_metadata(int fd, struct mp3entry* id3) id3->bitrate = (id3->filesize * 8) / id3->length; } - else if ((buf[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */ + else if (type == 4) /* 4 is the VORBIS_COMMENT block */ { /* The next i bytes of the file contain the VORBIS COMMENTS. */ if (!read_vorbis_tags(fd, id3, i)) @@ -100,20 +103,12 @@ bool get_flac_metadata(int fd, struct mp3entry* id3) return rc; } } - else + else if (!last_metadata) { - if (buf[0] & 0x80) - { - /* If we have reached the last metadata block, abort. */ - break; - } - else + /* Skip to next metadata block */ + if (lseek(fd, i, SEEK_CUR) < 0) { - /* Skip to next metadata block */ - if (lseek(fd, i, SEEK_CUR) < 0) - { - return rc; - } + return rc; } } } |