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