2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2009-03-02 18:00:46 +01:00
|
|
|
|
2021-02-16 19:21:31 +01:00
|
|
|
#include "RiffId3.hxx"
|
|
|
|
#include "RiffFormat.hxx"
|
2016-02-19 19:35:16 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2019-03-08 10:21:10 +01:00
|
|
|
#include "util/ByteOrder.hxx"
|
2009-03-02 18:00:46 +01:00
|
|
|
|
2013-10-17 21:53:19 +02:00
|
|
|
#include <limits>
|
2016-09-09 18:47:42 +02:00
|
|
|
#include <stdexcept>
|
2009-03-02 18:00:46 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
size_t
|
2019-04-26 19:19:45 +02:00
|
|
|
riff_seek_id3(InputStream &is, std::unique_lock<Mutex> &lock)
|
2009-03-02 18:00:46 +01:00
|
|
|
{
|
|
|
|
/* seek to the beginning and read the RIFF header */
|
|
|
|
|
2019-04-26 19:19:45 +02:00
|
|
|
is.Rewind(lock);
|
2009-03-02 18:00:46 +01:00
|
|
|
|
2021-02-16 19:23:20 +01:00
|
|
|
RiffFileHeader header;
|
2019-04-26 19:19:45 +02:00
|
|
|
is.ReadFull(lock, &header, sizeof(header));
|
2016-09-09 18:47:42 +02:00
|
|
|
if (memcmp(header.id, "RIFF", 4) != 0 ||
|
2016-02-19 19:35:16 +01:00
|
|
|
(is.KnownSize() && FromLE32(header.size) > is.GetSize()))
|
2016-09-09 18:47:42 +02:00
|
|
|
throw std::runtime_error("Not a RIFF file");
|
2009-03-02 18:00:46 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
/* read the chunk header */
|
|
|
|
|
2021-02-16 19:23:20 +01:00
|
|
|
RiffChunkHeader chunk;
|
2019-04-26 19:19:45 +02:00
|
|
|
is.ReadFull(lock, &chunk, sizeof(chunk));
|
2009-03-02 18:00:46 +01:00
|
|
|
|
2016-02-19 19:35:16 +01:00
|
|
|
size_t size = FromLE32(chunk.size);
|
2013-10-17 21:53:19 +02:00
|
|
|
if (size > size_t(std::numeric_limits<int>::max()))
|
2009-10-11 23:15:38 +02:00
|
|
|
/* too dangerous, bail out: possible integer
|
|
|
|
underflow when casting to off_t */
|
2016-09-09 18:47:42 +02:00
|
|
|
throw std::runtime_error("RIFF chunk is too large");
|
2009-10-11 23:15:38 +02:00
|
|
|
|
2013-12-19 09:32:01 +01:00
|
|
|
if (memcmp(chunk.id, "id3 ", 4) == 0 ||
|
|
|
|
memcmp(chunk.id, "ID3 ", 4) == 0)
|
2009-03-02 18:00:46 +01:00
|
|
|
/* found it! */
|
|
|
|
return size;
|
|
|
|
|
2016-02-19 17:31:20 +01:00
|
|
|
if (size % 2 != 0)
|
|
|
|
/* pad byte */
|
|
|
|
++size;
|
|
|
|
|
2019-04-26 19:19:45 +02:00
|
|
|
is.Skip(lock, size);
|
2009-03-02 18:00:46 +01:00
|
|
|
}
|
|
|
|
}
|