Skip to content

Commit

Permalink
Merge branch 'filetype-detection' of https://github.com/TsudaKageyu/t…
Browse files Browse the repository at this point in the history
…aglib into filetype-detection
  • Loading branch information
TsudaKageyu committed Feb 7, 2017
2 parents f76b1e5 + f7b15fa commit bf7ee62
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
16 changes: 4 additions & 12 deletions taglib/mpeg/mpegfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,15 @@ bool MPEG::File::isSupported(IOStream *stream)
// MPEG frame headers are really confusing with irrelevant binary data.
// So we check if a frame header is really valid.

const long originalPosition = stream->tell();

long bufferOffset = 0;

stream->seek(0);
const ByteVector data = stream->readBlock(ID3v2::Header::size());
if(data.startsWith(ID3v2::Header::fileIdentifier()))
bufferOffset = ID3v2::Header(data).completeTagSize();

stream->seek(bufferOffset);
const ByteVector buffer = stream->readBlock(bufferSize());
long headerOffset;
const ByteVector buffer = Utils::readHeader(stream, bufferSize(), true, &headerOffset);

const long originalPosition = stream->tell();
AdapterFile file(stream);

for(unsigned int i = 0; i < buffer.size() - 1; ++i) {
if(isFrameSync(buffer, i)) {
const Header header(&file, bufferOffset + i, true);
const Header header(&file, headerOffset + i, true);
if(header.isValid()) {
stream->seek(originalPosition);
return true;
Expand Down
6 changes: 5 additions & 1 deletion taglib/tagutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ long Utils::findAPE(File *file, long id3v1Location)
return -1;
}

ByteVector TagLib::Utils::readHeader(IOStream *stream, unsigned int length, bool skipID3v2)
ByteVector TagLib::Utils::readHeader(IOStream *stream, unsigned int length,
bool skipID3v2, long *headerOffset)
{
if(!stream || !stream->isOpen())
return ByteVector();
Expand All @@ -97,5 +98,8 @@ ByteVector TagLib::Utils::readHeader(IOStream *stream, unsigned int length, bool
const ByteVector header = stream->readBlock(length);
stream->seek(originalPosition);

if(headerOffset)
*headerOffset = bufferOffset;

return header;
}
3 changes: 2 additions & 1 deletion taglib/tagutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace TagLib {

long findAPE(File *file, long id3v1Location);

ByteVector readHeader(IOStream *stream, unsigned int length, bool skipID3v2);
ByteVector readHeader(IOStream *stream, unsigned int length, bool skipID3v2,
long *headerOffset = 0);
}
}

Expand Down

0 comments on commit bf7ee62

Please sign in to comment.