Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMFF Performance boost: don't read boxes we're not interested in (backport #1974) #1975

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ namespace Exiv2
return box == TAG_meta || box == TAG_iinf || box == TAG_iloc;
}

static bool skipBox(uint32_t box)
{
// Allows boxHandler() to optimise the reading of files by identifying
// box types that we're not interested in. Box types listed here must
// not appear in the cases in switch (box_type) in boxHandler().
return box == TAG_mdat; // mdat is where the main image lives and can be huge
}

std::string BmffImage::mimeType() const
{
switch (fileType_) {
Expand Down Expand Up @@ -232,7 +240,18 @@ namespace Exiv2
long restore = io_->tell();
enforce(box_length >= hdrsize, Exiv2::kerCorruptedMetadata);
enforce(box_length - hdrsize <= static_cast<size_t>(pbox_end - restore), Exiv2::kerCorruptedMetadata);
DataBuf data(static_cast<long>(box_length - hdrsize));

const long buffer_size = static_cast<long>(box_length - hdrsize);
if (skipBox(box_type)) {
if (bTrace) {
out << std::endl;
}
// The enforce() above checks that restore + buffer_size won't
// exceed pbox_end, and by implication, won't excced LONG_MAX
return restore + buffer_size;
}

DataBuf data(buffer_size);
const long box_end = restore + data.size_;
io_->read(data.pData_, data.size_);
io_->seek(restore, BasicIo::beg);
Expand All @@ -250,6 +269,7 @@ namespace Exiv2
}

switch (box_type) {
// See notes in skipBox()
case TAG_ftyp: {
enforce(data.size_ >= 4, Exiv2::kerCorruptedMetadata);
fileType_ = getLong(data.pData_, endian_);
Expand Down