From 84fe3bfa8799847d496094476fefcc0d7298bf6d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 19 Feb 2016 19:42:44 +0100 Subject: [PATCH] tag/Id3Load: don't seek twice in tag_id3_read() Copy the query buffer to the allocated buffer, and read only the remaining data. --- src/tag/Id3Load.cxx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/tag/Id3Load.cxx b/src/tag/Id3Load.cxx index c202ac619..d723b50a5 100644 --- a/src/tag/Id3Load.cxx +++ b/src/tag/Id3Load.cxx @@ -30,6 +30,8 @@ #include +#include + #include static constexpr Domain id3_domain("id3"); @@ -77,12 +79,19 @@ tag_id3_read(FILE *stream, long offset, int whence) return UniqueId3Tag(id3_tag_parse(query_buffer, tag_size)); std::unique_ptr tag_buffer(new id3_byte_t[tag_size]); - int tag_buffer_size = fill_buffer(tag_buffer.get(), tag_size, - stream, offset, whence); - if (tag_buffer_size < tag_size) + + /* copy the start of the tag we already have to the allocated + buffer */ + id3_byte_t *end = std::copy_n(query_buffer, query_buffer_size, + tag_buffer.get()); + + /* now read the remaining bytes */ + const size_t remaining = tag_size - query_buffer_size; + const size_t nbytes = fread(end, 1, remaining, stream); + if (nbytes != remaining) return nullptr; - return UniqueId3Tag(id3_tag_parse(tag_buffer.get(), tag_buffer_size)); + return UniqueId3Tag(id3_tag_parse(tag_buffer.get(), tag_size)); } static UniqueId3Tag