tag/Id3Load: use class InputStream instead of FILE*
Prepare for ID3 support on userspace NFS/SMB/CIFS mounts.
This commit is contained in:
parent
7ae9e49f51
commit
8a86460b8f
@ -1890,6 +1890,11 @@ test_ReadApeTags_SOURCES = test/ReadApeTags.cxx
|
||||
if ENABLE_ID3TAG
|
||||
test_dump_rva2_LDADD = \
|
||||
$(TAG_LIBS) \
|
||||
$(INPUT_LIBS) \
|
||||
$(ARCHIVE_LIBS) \
|
||||
$(FS_LIBS) \
|
||||
$(ICU_LDADD) \
|
||||
libsystem.a \
|
||||
libutil.a
|
||||
test_dump_rva2_SOURCES = \
|
||||
src/Log.cxx src/LogBackend.cxx \
|
||||
|
@ -19,9 +19,10 @@
|
||||
|
||||
#include "config.h" /* must be first for large file support */
|
||||
#include "Aiff.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "input/InputStream.hxx"
|
||||
#include "system/ByteOrder.hxx"
|
||||
#include "Log.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <limits>
|
||||
|
||||
@ -29,8 +30,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
static constexpr Domain aiff_domain("aiff");
|
||||
|
||||
struct aiff_header {
|
||||
char id[4];
|
||||
uint32_t size;
|
||||
@ -42,36 +41,21 @@ struct aiff_chunk_header {
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
gcc_pure
|
||||
static off_t
|
||||
GetFileSize(FILE *file)
|
||||
{
|
||||
struct stat st;
|
||||
return fstat(fileno(file), &st) == 0
|
||||
? st.st_size
|
||||
: -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
aiff_seek_id3(FILE *file)
|
||||
aiff_seek_id3(InputStream &is)
|
||||
{
|
||||
/* determine the file size */
|
||||
|
||||
const off_t file_size = GetFileSize(file);
|
||||
|
||||
/* seek to the beginning and read the AIFF header */
|
||||
|
||||
if (fseek(file, 0, SEEK_SET) != 0) {
|
||||
LogErrno(aiff_domain, "Failed to seek");
|
||||
Error error;
|
||||
if (!is.Rewind(error)) {
|
||||
LogError(error, "Failed to seek");
|
||||
return 0;
|
||||
}
|
||||
|
||||
aiff_header header;
|
||||
size_t size = fread(&header, sizeof(header), 1, file);
|
||||
if (size != 1 ||
|
||||
if (!is.ReadFull(&header, sizeof(header), IgnoreError()) ||
|
||||
memcmp(header.id, "FORM", 4) != 0 ||
|
||||
(file_size >= 0 &&
|
||||
FromBE32(header.size) > (uint32_t)file_size) ||
|
||||
(is.KnownSize() && FromLE32(header.size) > is.GetSize()) ||
|
||||
(memcmp(header.format, "AIFF", 4) != 0 &&
|
||||
memcmp(header.format, "AIFC", 4) != 0))
|
||||
/* not a AIFF file */
|
||||
@ -81,12 +65,11 @@ aiff_seek_id3(FILE *file)
|
||||
/* read the chunk header */
|
||||
|
||||
aiff_chunk_header chunk;
|
||||
size = fread(&chunk, sizeof(chunk), 1, file);
|
||||
if (size != 1)
|
||||
if (!is.ReadFull(&chunk, sizeof(chunk), IgnoreError()))
|
||||
return 0;
|
||||
|
||||
size = FromBE32(chunk.size);
|
||||
if (size > unsigned(std::numeric_limits<int>::max()))
|
||||
size_t size = FromBE32(chunk.size);
|
||||
if (size > size_t(std::numeric_limits<int>::max()))
|
||||
/* too dangerous, bail out: possible integer
|
||||
underflow when casting to off_t */
|
||||
return 0;
|
||||
@ -99,7 +82,7 @@ aiff_seek_id3(FILE *file)
|
||||
/* pad byte */
|
||||
++size;
|
||||
|
||||
if (fseek(file, size, SEEK_CUR) != 0)
|
||||
if (!is.Skip(size, IgnoreError()))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,17 @@
|
||||
#define MPD_AIFF_HXX
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class InputStream;
|
||||
|
||||
/**
|
||||
* Seeks the AIFF file to the ID3 chunk.
|
||||
*
|
||||
* @param is a locked #InputStream
|
||||
* @return the size of the ID3 chunk on success, or 0 if this is not a
|
||||
* AIFF file or no ID3 chunk was found
|
||||
*/
|
||||
size_t
|
||||
aiff_seek_id3(FILE *file);
|
||||
aiff_seek_id3(InputStream &is);
|
||||
|
||||
#endif
|
||||
|
@ -25,15 +25,15 @@
|
||||
#include "Riff.hxx"
|
||||
#include "Aiff.hxx"
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/NarrowPath.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "thread/Mutex.hxx"
|
||||
#include "thread/Cond.hxx"
|
||||
#include "input/InputStream.hxx"
|
||||
#include "input/LocalOpen.hxx"
|
||||
|
||||
#include <id3tag.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static constexpr Domain id3_domain("id3");
|
||||
|
||||
static constexpr size_t ID3V1_SIZE = 128;
|
||||
@ -45,37 +45,32 @@ tag_is_id3v1(struct id3_tag *tag)
|
||||
return (id3_tag_options(tag, 0, 0) & ID3_TAG_OPTION_ID3V1) != 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
fill_buffer(void *buf, size_t size, FILE *stream, long offset, int whence)
|
||||
{
|
||||
if (fseek(stream, offset, whence) != 0) return 0;
|
||||
return fread(buf, 1, size, stream);
|
||||
}
|
||||
|
||||
static long
|
||||
get_id3v2_footer_size(FILE *stream, long offset, int whence)
|
||||
get_id3v2_footer_size(InputStream &is, offset_type offset)
|
||||
{
|
||||
id3_byte_t buf[ID3_TAG_QUERYSIZE];
|
||||
size_t bufsize = fill_buffer(buf, ID3_TAG_QUERYSIZE, stream, offset, whence);
|
||||
if (bufsize == 0) return 0;
|
||||
return id3_tag_query(buf, bufsize);
|
||||
if (!is.Seek(offset, IgnoreError()))
|
||||
return 0;
|
||||
|
||||
if (!is.ReadFull(buf, sizeof(buf), IgnoreError()))
|
||||
return 0;
|
||||
|
||||
return id3_tag_query(buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
ReadId3Tag(FILE *file)
|
||||
ReadId3Tag(InputStream &is)
|
||||
{
|
||||
id3_byte_t query_buffer[ID3_TAG_QUERYSIZE];
|
||||
size_t query_buffer_size = fread(query_buffer, 1, sizeof(query_buffer),
|
||||
file);
|
||||
if (query_buffer_size == 0)
|
||||
if (!is.ReadFull(query_buffer, sizeof(query_buffer), IgnoreError()))
|
||||
return nullptr;
|
||||
|
||||
/* Look for a tag header */
|
||||
long tag_size = id3_tag_query(query_buffer, query_buffer_size);
|
||||
long tag_size = id3_tag_query(query_buffer, sizeof(query_buffer));
|
||||
if (tag_size <= 0) return nullptr;
|
||||
|
||||
/* Found a tag. Allocate a buffer and read it in. */
|
||||
if (size_t(tag_size) <= query_buffer_size)
|
||||
if (size_t(tag_size) <= sizeof(query_buffer))
|
||||
/* we have enough data already */
|
||||
return UniqueId3Tag(id3_tag_parse(query_buffer, tag_size));
|
||||
|
||||
@ -83,12 +78,12 @@ ReadId3Tag(FILE *file)
|
||||
|
||||
/* 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,
|
||||
id3_byte_t *end = std::copy_n(query_buffer, sizeof(query_buffer),
|
||||
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, file);
|
||||
const size_t remaining = tag_size - sizeof(query_buffer);
|
||||
const size_t nbytes = is.Read(end, remaining, IgnoreError());
|
||||
if (nbytes != remaining)
|
||||
return nullptr;
|
||||
|
||||
@ -96,29 +91,38 @@ ReadId3Tag(FILE *file)
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
ReadId3v1Tag(FILE *file)
|
||||
ReadId3Tag(InputStream &is, off_t offset)
|
||||
{
|
||||
if (!is.Seek(offset, IgnoreError()))
|
||||
return nullptr;
|
||||
|
||||
return ReadId3Tag(is);
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
ReadId3v1Tag(InputStream &is)
|
||||
{
|
||||
id3_byte_t buffer[ID3V1_SIZE];
|
||||
|
||||
if (fread(buffer, 1, ID3V1_SIZE, file) != ID3V1_SIZE)
|
||||
if (is.Read(buffer, ID3V1_SIZE, IgnoreError()) != ID3V1_SIZE)
|
||||
return nullptr;
|
||||
|
||||
return UniqueId3Tag(id3_tag_parse(buffer, ID3V1_SIZE));
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
tag_id3_read(FILE *file, long offset, int whence)
|
||||
ReadId3v1Tag(InputStream &is, off_t offset)
|
||||
{
|
||||
if (fseek(file, offset, whence) != 0)
|
||||
return 0;
|
||||
if (!is.Seek(offset, IgnoreError()))
|
||||
return nullptr;
|
||||
|
||||
return ReadId3Tag(file);
|
||||
return ReadId3v1Tag(is);
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
tag_id3_find_from_beginning(FILE *stream)
|
||||
tag_id3_find_from_beginning(InputStream &is)
|
||||
{
|
||||
auto tag = ReadId3Tag(stream);
|
||||
auto tag = ReadId3Tag(is);
|
||||
if (!tag) {
|
||||
return nullptr;
|
||||
} else if (tag_is_id3v1(tag.get())) {
|
||||
@ -135,7 +139,7 @@ tag_id3_find_from_beginning(FILE *stream)
|
||||
break;
|
||||
|
||||
/* Get the tag specified by the SEEK frame */
|
||||
auto seektag = tag_id3_read(stream, seek, SEEK_CUR);
|
||||
auto seektag = ReadId3Tag(is, is.GetOffset() + seek);
|
||||
if (!seektag || tag_is_id3v1(seektag.get()))
|
||||
break;
|
||||
|
||||
@ -147,25 +151,37 @@ tag_id3_find_from_beginning(FILE *stream)
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
tag_id3_find_from_end(FILE *stream)
|
||||
tag_id3_find_from_end(InputStream &is)
|
||||
{
|
||||
off_t offset = -(off_t)ID3V1_SIZE;
|
||||
|
||||
/* Get an id3v1 tag from the end of file for later use */
|
||||
if (fseek(stream, offset, SEEK_END) != 0)
|
||||
if (!is.KnownSize() || !is.CheapSeeking())
|
||||
return nullptr;
|
||||
|
||||
auto v1tag = ReadId3v1Tag(stream);
|
||||
const offset_type size = is.GetSize();
|
||||
if (size < ID3V1_SIZE)
|
||||
return nullptr;
|
||||
|
||||
offset_type offset = size - ID3V1_SIZE;
|
||||
|
||||
/* Get an id3v1 tag from the end of file for later use */
|
||||
auto v1tag = ReadId3v1Tag(is, offset);
|
||||
if (!v1tag)
|
||||
offset = 0;
|
||||
offset = size;
|
||||
|
||||
/* Get the id3v2 tag size from the footer (located before v1tag) */
|
||||
int tagsize = get_id3v2_footer_size(stream, offset - ID3_TAG_QUERYSIZE, SEEK_END);
|
||||
if (tagsize >= 0)
|
||||
if (offset < ID3_TAG_QUERYSIZE)
|
||||
return v1tag;
|
||||
|
||||
long tag_offset =
|
||||
get_id3v2_footer_size(is, offset - ID3_TAG_QUERYSIZE);
|
||||
if (tag_offset >= 0)
|
||||
return v1tag;
|
||||
|
||||
offset_type tag_size = -tag_offset;
|
||||
if (tag_size > offset)
|
||||
return v1tag;
|
||||
|
||||
/* Get the tag which the footer belongs to */
|
||||
auto tag = tag_id3_read(stream, tagsize, SEEK_CUR);
|
||||
auto tag = ReadId3Tag(is, offset - tag_size);
|
||||
if (!tag)
|
||||
return v1tag;
|
||||
|
||||
@ -174,11 +190,11 @@ tag_id3_find_from_end(FILE *stream)
|
||||
}
|
||||
|
||||
static UniqueId3Tag
|
||||
tag_id3_riff_aiff_load(FILE *file)
|
||||
tag_id3_riff_aiff_load(InputStream &is)
|
||||
{
|
||||
size_t size = riff_seek_id3(file);
|
||||
size_t size = riff_seek_id3(is);
|
||||
if (size == 0)
|
||||
size = aiff_seek_id3(file);
|
||||
size = aiff_seek_id3(is);
|
||||
if (size == 0)
|
||||
return nullptr;
|
||||
|
||||
@ -187,8 +203,7 @@ tag_id3_riff_aiff_load(FILE *file)
|
||||
return nullptr;
|
||||
|
||||
std::unique_ptr<id3_byte_t[]> buffer(new id3_byte_t[size]);
|
||||
size_t ret = fread(buffer.get(), size, 1, file);
|
||||
if (ret != 1) {
|
||||
if (!is.ReadFull(buffer.get(), size, IgnoreError())) {
|
||||
LogWarning(id3_domain, "Failed to read RIFF chunk");
|
||||
return nullptr;
|
||||
}
|
||||
@ -197,22 +212,30 @@ tag_id3_riff_aiff_load(FILE *file)
|
||||
}
|
||||
|
||||
UniqueId3Tag
|
||||
tag_id3_load(Path path_fs, Error &error)
|
||||
tag_id3_load(InputStream &is)
|
||||
{
|
||||
FILE *file = FOpen(path_fs, PATH_LITERAL("rb"));
|
||||
if (file == nullptr) {
|
||||
error.FormatErrno("Failed to open file %s",
|
||||
NarrowPath(path_fs).c_str());
|
||||
return nullptr;
|
||||
}
|
||||
const ScopeLock protect(is.mutex);
|
||||
|
||||
auto tag = tag_id3_find_from_beginning(file);
|
||||
if (tag == nullptr) {
|
||||
tag = tag_id3_riff_aiff_load(file);
|
||||
auto tag = tag_id3_find_from_beginning(is);
|
||||
if (tag == nullptr && is.CheapSeeking()) {
|
||||
tag = tag_id3_riff_aiff_load(is);
|
||||
if (tag == nullptr)
|
||||
tag = tag_id3_find_from_end(file);
|
||||
tag = tag_id3_find_from_end(is);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return tag;
|
||||
}
|
||||
|
||||
UniqueId3Tag
|
||||
tag_id3_load(Path path_fs, Error &error)
|
||||
{
|
||||
Mutex mutex;
|
||||
Cond cond;
|
||||
|
||||
std::unique_ptr<InputStream> is(OpenLocalInputStream(path_fs, mutex,
|
||||
cond, error));
|
||||
if (!is)
|
||||
return nullptr;
|
||||
|
||||
return tag_id3_load(*is);
|
||||
}
|
||||
|
@ -25,10 +25,19 @@
|
||||
|
||||
class Path;
|
||||
class Error;
|
||||
class InputStream;
|
||||
|
||||
/**
|
||||
* Loads the ID3 tags from the file into a libid3tag object. The
|
||||
* return value must be freed with id3_tag_delete().
|
||||
* Loads the ID3 tags from the #InputStream into a libid3tag object.
|
||||
*
|
||||
* @return nullptr on error or if no ID3 tag was found in the file (no
|
||||
* Error will be set)
|
||||
*/
|
||||
UniqueId3Tag
|
||||
tag_id3_load(InputStream &is);
|
||||
|
||||
/**
|
||||
* Loads the ID3 tags from the file into a libid3tag object.
|
||||
*
|
||||
* @return nullptr on error or if no ID3 tag was found in the file (no
|
||||
* Error will be set)
|
||||
|
@ -19,18 +19,16 @@
|
||||
|
||||
#include "config.h" /* must be first for large file support */
|
||||
#include "Riff.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "input/InputStream.hxx"
|
||||
#include "system/ByteOrder.hxx"
|
||||
#include "Log.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
static constexpr Domain riff_domain("riff");
|
||||
|
||||
struct riff_header {
|
||||
char id[4];
|
||||
uint32_t size;
|
||||
@ -42,35 +40,21 @@ struct riff_chunk_header {
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
gcc_pure
|
||||
static off_t
|
||||
GetFileSize(FILE *file)
|
||||
{
|
||||
struct stat st;
|
||||
return fstat(fileno(file), &st) == 0
|
||||
? st.st_size
|
||||
: -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
riff_seek_id3(FILE *file)
|
||||
riff_seek_id3(InputStream &is)
|
||||
{
|
||||
/* determine the file size */
|
||||
|
||||
const off_t file_size = GetFileSize(file);
|
||||
|
||||
/* seek to the beginning and read the RIFF header */
|
||||
|
||||
if (fseek(file, 0, SEEK_SET) != 0) {
|
||||
LogErrno(riff_domain, "Failed to seek");
|
||||
Error error;
|
||||
if (!is.Rewind(error)) {
|
||||
LogError(error, "Failed to seek");
|
||||
return 0;
|
||||
}
|
||||
|
||||
riff_header header;
|
||||
size_t size = fread(&header, sizeof(header), 1, file);
|
||||
if (size != 1 ||
|
||||
if (!is.ReadFull(&header, sizeof(header), IgnoreError()) ||
|
||||
memcmp(header.id, "RIFF", 4) != 0 ||
|
||||
(file_size >= 0 && FromLE32(header.size) > (uint32_t)file_size))
|
||||
(is.KnownSize() && FromLE32(header.size) > is.GetSize()))
|
||||
/* not a RIFF file */
|
||||
return 0;
|
||||
|
||||
@ -78,11 +62,10 @@ riff_seek_id3(FILE *file)
|
||||
/* read the chunk header */
|
||||
|
||||
riff_chunk_header chunk;
|
||||
size = fread(&chunk, sizeof(chunk), 1, file);
|
||||
if (size != 1)
|
||||
if (!is.ReadFull(&chunk, sizeof(chunk), IgnoreError()))
|
||||
return 0;
|
||||
|
||||
size = FromLE32(chunk.size);
|
||||
size_t size = FromLE32(chunk.size);
|
||||
if (size > size_t(std::numeric_limits<int>::max()))
|
||||
/* too dangerous, bail out: possible integer
|
||||
underflow when casting to off_t */
|
||||
@ -97,7 +80,7 @@ riff_seek_id3(FILE *file)
|
||||
/* pad byte */
|
||||
++size;
|
||||
|
||||
if (fseek(file, size, SEEK_CUR) != 0)
|
||||
if (!is.Skip(size, IgnoreError()))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,17 @@
|
||||
#define MPD_RIFF_HXX
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class InputStream;
|
||||
|
||||
/**
|
||||
* Seeks the RIFF file to the ID3 chunk.
|
||||
*
|
||||
* @param is a locked #InputStream
|
||||
* @return the size of the ID3 chunk on success, or 0 if this is not a
|
||||
* RIFF file or no ID3 chunk was found
|
||||
*/
|
||||
size_t
|
||||
riff_seek_id3(FILE *file);
|
||||
riff_seek_id3(InputStream &is);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user