From f1abbf33f255fcc9b452eb773a2de49972d5c370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Sat, 31 Oct 2009 08:59:40 +0000 Subject: [PATCH] A little more complex checking for broken files git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@1042948 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/mp4/mp4atom.cpp | 12 +++++++++--- taglib/mp4/mp4file.cpp | 20 ++++++++++++++++++++ taglib/mp4/mp4file.h | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/taglib/mp4/mp4atom.cpp b/taglib/mp4/mp4atom.cpp index 25007946..6d86a6e5 100644 --- a/taglib/mp4/mp4atom.cpp +++ b/taglib/mp4/mp4atom.cpp @@ -47,7 +47,7 @@ MP4::Atom::Atom(File *file) if (header.size() != 8) { // The atom header must be 8 bytes long, otherwise there is either // trailing garbage or the file is truncated - debug("MP4: Expected 8 bytes of atom header"); + debug("MP4: Couldn't read 8 bytes of data for atom header"); length = 0; file->seek(0, File::End); return; @@ -83,7 +83,10 @@ MP4::Atom::Atom(File *file) file->seek(4, File::Current); } while(file->tell() < offset + length) { - children.append(new MP4::Atom(file)); + MP4::Atom *child = new MP4::Atom(file); + children.append(child); + if (child->length == 0) + return; } return; } @@ -150,7 +153,10 @@ MP4::Atoms::Atoms(File *file) long end = file->tell(); file->seek(0); while(file->tell() + 8 <= end) { - atoms.append(new MP4::Atom(file)); + MP4::Atom *atom = new MP4::Atom(file); + atoms.append(atom); + if (atom->length == 0) + break; } } diff --git a/taglib/mp4/mp4file.cpp b/taglib/mp4/mp4file.cpp index 3165256c..3c996d1b 100644 --- a/taglib/mp4/mp4file.cpp +++ b/taglib/mp4/mp4file.cpp @@ -89,6 +89,18 @@ MP4::File::audioProperties() const return d->properties; } +bool +MP4::File::checkValid(const MP4::AtomList &list) +{ + for(uint i = 0; i < list.size(); i++) { + if(list[i]->length == 0) + return false; + if(!checkValid(list[i]->children)) + return false; + } + return true; +} + void MP4::File::read(bool readProperties, Properties::ReadStyle audioPropertiesStyle) { @@ -96,6 +108,11 @@ MP4::File::read(bool readProperties, Properties::ReadStyle audioPropertiesStyle) return; d->atoms = new Atoms(this); + if (!checkValid(d->atoms->atoms)) { + setValid(false); + return; + } + d->tag = new Tag(this, d->atoms); if(readProperties) { d->properties = new Properties(this, d->atoms, audioPropertiesStyle); @@ -105,6 +122,9 @@ MP4::File::read(bool readProperties, Properties::ReadStyle audioPropertiesStyle) bool MP4::File::save() { + if(!isValid()) + return false; + return d->tag->save(); } diff --git a/taglib/mp4/mp4file.h b/taglib/mp4/mp4file.h index 0c09c04e..333551a9 100644 --- a/taglib/mp4/mp4file.h +++ b/taglib/mp4/mp4file.h @@ -90,6 +90,7 @@ namespace TagLib { private: void read(bool readProperties, Properties::ReadStyle audioPropertiesStyle); + bool checkValid(const MP4::AtomList &list); class FilePrivate; FilePrivate *d;