From 60e7b20971ec10b5444fd43f32b88b91e40a3c22 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Thu, 18 Jul 2002 15:21:45 +0000 Subject: More detailed mp3 frame header check git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1383 a1c6a512-1295-4272-9138-f99709370657 --- firmware/id3.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/firmware/id3.c b/firmware/id3.c index c2ae42e46d..16a155dd44 100644 --- a/firmware/id3.c +++ b/firmware/id3.c @@ -34,7 +34,6 @@ #include "id3.h" /* Some utility macros used in getsonglength() */ -#define CHECKSYNC(x) (((x >> 21) & 0x07FF) == 0x7FF) #define BYTE0(x) ((x >> 24) & 0xFF) #define BYTE1(x) ((x >> 16) & 0xFF) #define BYTE2(x) ((x >> 8) & 0xFF) @@ -345,6 +344,29 @@ static int getid3v1len(int fd) return offset; } +/* check if 'head' is a valid mp3 frame header */ +static bool mp3frameheader(unsigned long head) +{ + if ((head & 0xffe00000) != 0xffe00000) /* bad sync? */ + return false; + if (!((head >> 17) & 3)) /* no layer? */ + return false; + if (((head >> 12) & 0xf) == 0xf) /* bad bitrate? */ + return false; + if (!((head >> 12) & 0xf)) /* no bitrate? */ + return false; + if (((head >> 10) & 0x3) == 0x3) /* bad sample rate? */ + return false; + if (((head >> 19) & 1) == 1 && + ((head >> 17) & 3) == 3 && + ((head >> 16) & 1) == 1) + return false; + if ((head & 0xffff0000) == 0xfffe0000) + return false; + + return true; +} + /* * Calculates the length (in milliseconds) of an MP3 file. Currently this code * doesn't care about VBR (Variable BitRate) files since it would have to scan @@ -397,7 +419,7 @@ static int getsonglength(int fd, struct mp3entry *entry) if(!read(fd, &tmp, 1)) return -1; header |= tmp; - } while(!CHECKSYNC(header)); + } while(!mp3frameheader(header)); /* * Some files are filled with garbage in the beginning, -- cgit v1.2.3