From 13c8b7223995ebc44268446d80321447ddd90aea Mon Sep 17 00:00:00 2001 From: CatKasha Date: Tue, 31 Jan 2023 23:47:15 +0300 Subject: [PATCH 1/6] improved mp3 detection references: https://www.datavoyage.com/mpgscript/mpeghdr.htm https://en.wikipedia.org/wiki/MP3#/media/File:Mp3filestructure.svg --- filetype/types/audio.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index 3d1f20c..bbfaacd 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -40,16 +40,24 @@ def __init__(self): ) def match(self, buf): - return (len(buf) > 2 and - ((buf[0] == 0x49 and - buf[1] == 0x44 and - buf[2] == 0x33) or - (buf[0] == 0xFF and - buf[1] == 0xF2) or - (buf[0] == 0xFF and - buf[1] == 0xF3) or - (buf[0] == 0xFF and - buf[1] == 0xFB))) + if (len(buf) > 2): + if (buf[0] == 0x49 and + buf[1] == 0x44 and + buf[2] == 0x33): + return True + + if (buf[0] == 0xFF): + if (buf[1] == 0xE2 or #MPEG 2.5 with error protection + buf[1] == 0xE3 or #MPEG 2.5 w/o error protection + buf[1] == 0xF2 or #MPEG 2 with error protection + buf[1] == 0xF3 or #MPEG 2 w/o error protection + buf[1] == 0xFA or #MPEG 1 with error protection + buf[1] == 0xFB): #MPEG 1 w/o error protection + return True + else: + return False + else: + return False class M4a(Type): From dc8c9502bf0e968c8a836d523a55f603c1123f73 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Tue, 31 Jan 2023 23:59:27 +0300 Subject: [PATCH 2/6] forgot return in mp3 --- filetype/types/audio.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index bbfaacd..cde680a 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -54,6 +54,8 @@ def match(self, buf): buf[1] == 0xFA or #MPEG 1 with error protection buf[1] == 0xFB): #MPEG 1 w/o error protection return True + else: + return False else: return False else: From 5cb490fa2714e8d52b99e929423f1cb0c87cd9fb Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 1 Feb 2023 00:08:52 +0300 Subject: [PATCH 3/6] less returns in mp3 --- filetype/types/audio.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index cde680a..ee3dc61 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -54,12 +54,7 @@ def match(self, buf): buf[1] == 0xFA or #MPEG 1 with error protection buf[1] == 0xFB): #MPEG 1 w/o error protection return True - else: - return False - else: - return False - else: - return False + return False class M4a(Type): From 5edd75c45dceb6d0e48fc5f0c15e35c5a08d29a9 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 1 Feb 2023 00:19:00 +0300 Subject: [PATCH 4/6] mp3: make flake8 rules happy --- filetype/types/audio.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index ee3dc61..518e529 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -47,12 +47,12 @@ def match(self, buf): return True if (buf[0] == 0xFF): - if (buf[1] == 0xE2 or #MPEG 2.5 with error protection - buf[1] == 0xE3 or #MPEG 2.5 w/o error protection - buf[1] == 0xF2 or #MPEG 2 with error protection - buf[1] == 0xF3 or #MPEG 2 w/o error protection - buf[1] == 0xFA or #MPEG 1 with error protection - buf[1] == 0xFB): #MPEG 1 w/o error protection + if (buf[1] == 0xE2 or #MPEG 2.5 with error protection + buf[1] == 0xE3 or #MPEG 2.5 w/o error protection + buf[1] == 0xF2 or #MPEG 2 with error protection + buf[1] == 0xF3 or #MPEG 2 w/o error protection + buf[1] == 0xFA or #MPEG 1 with error protection + buf[1] == 0xFB): #MPEG 1 w/o error protection return True return False From 9e1714fcce393a9dcf2a53d28fc7219da49fe08d Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 1 Feb 2023 00:29:10 +0300 Subject: [PATCH 5/6] mp3: make flake8 rules happy 2 --- filetype/types/audio.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index 518e529..8eaae3e 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -44,16 +44,16 @@ def match(self, buf): if (buf[0] == 0x49 and buf[1] == 0x44 and buf[2] == 0x33): - return True + return True if (buf[0] == 0xFF): - if (buf[1] == 0xE2 or #MPEG 2.5 with error protection - buf[1] == 0xE3 or #MPEG 2.5 w/o error protection - buf[1] == 0xF2 or #MPEG 2 with error protection - buf[1] == 0xF3 or #MPEG 2 w/o error protection - buf[1] == 0xFA or #MPEG 1 with error protection - buf[1] == 0xFB): #MPEG 1 w/o error protection - return True + if (buf[1] == 0xE2 or # MPEG 2.5 with error protection + buf[1] == 0xE3 or # MPEG 2.5 w/o error protection + buf[1] == 0xF2 or # MPEG 2 with error protection + buf[1] == 0xF3 or # MPEG 2 w/o error protection + buf[1] == 0xFA or # MPEG 1 with error protection + buf[1] == 0xFB): # MPEG 1 w/o error protection + return True return False From 2ba0aeeff67ee10f148df8dc66a4f41a490473fb Mon Sep 17 00:00:00 2001 From: Mikhail Murashov Date: Mon, 6 Mar 2023 12:29:11 +0300 Subject: [PATCH 6/6] Fix flake small fixes for flake --- filetype/types/audio.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/filetype/types/audio.py b/filetype/types/audio.py index 8eaae3e..429d805 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -40,20 +40,24 @@ def __init__(self): ) def match(self, buf): - if (len(buf) > 2): - if (buf[0] == 0x49 and + if len(buf) > 2: + if ( + buf[0] == 0x49 and buf[1] == 0x44 and - buf[2] == 0x33): - return True + buf[2] == 0x33 + ): + return True - if (buf[0] == 0xFF): - if (buf[1] == 0xE2 or # MPEG 2.5 with error protection + if buf[0] == 0xFF: + if ( + buf[1] == 0xE2 or # MPEG 2.5 with error protection buf[1] == 0xE3 or # MPEG 2.5 w/o error protection buf[1] == 0xF2 or # MPEG 2 with error protection buf[1] == 0xF3 or # MPEG 2 w/o error protection buf[1] == 0xFA or # MPEG 1 with error protection - buf[1] == 0xFB): # MPEG 1 w/o error protection - return True + buf[1] == 0xFB # MPEG 1 w/o error protection + ): + return True return False