tag/Id3Load: use class InputStream instead of FILE*

Prepare for ID3 support on userspace NFS/SMB/CIFS mounts.
This commit is contained in:
Max Kellermann
2016-02-19 19:35:16 +01:00
parent 7ae9e49f51
commit 8a86460b8f
7 changed files with 130 additions and 123 deletions

View File

@@ -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;
}
}