Clementine-audio-player-Mac.../3rdparty/taglib/riff/rifffile.cpp

362 lines
9.2 KiB
C++
Raw Normal View History

2012-10-28 02:12:18 +02:00
/***************************************************************************
copyright : (C) 2002 - 2008 by Scott Wheeler
email : wheeler@kde.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
2016-07-19 16:58:52 +02:00
#include <algorithm>
#include <vector>
2012-10-28 02:12:18 +02:00
#include <tbytevector.h>
#include <tdebug.h>
#include <tstring.h>
#include "rifffile.h"
2016-07-19 16:58:52 +02:00
#include "riffutils.h"
2012-10-28 02:12:18 +02:00
using namespace TagLib;
struct Chunk
{
2016-07-19 16:58:52 +02:00
ByteVector name;
unsigned int offset;
unsigned int size;
unsigned int padding;
2012-10-28 02:12:18 +02:00
};
class RIFF::File::FilePrivate
{
public:
2016-07-19 16:58:52 +02:00
FilePrivate(Endianness endianness) :
endianness(endianness),
size(0),
sizeOffset(0) {}
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
const Endianness endianness;
unsigned int size;
long sizeOffset;
2012-10-28 02:12:18 +02:00
std::vector<Chunk> chunks;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
RIFF::File::~File()
{
delete d;
}
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////
2016-07-19 16:58:52 +02:00
RIFF::File::File(FileName file, Endianness endianness) :
TagLib::File(file),
d(new FilePrivate(endianness))
2012-10-28 02:12:18 +02:00
{
if(isOpen())
read();
}
2016-07-19 16:58:52 +02:00
RIFF::File::File(IOStream *stream, Endianness endianness) :
TagLib::File(stream),
d(new FilePrivate(endianness))
2012-10-28 02:12:18 +02:00
{
if(isOpen())
read();
}
2016-07-19 16:58:52 +02:00
unsigned int RIFF::File::riffSize() const
2012-10-28 02:12:18 +02:00
{
return d->size;
}
2016-07-19 16:58:52 +02:00
unsigned int RIFF::File::chunkCount() const
2012-10-28 02:12:18 +02:00
{
2018-06-06 22:47:08 +02:00
return static_cast<unsigned int>(d->chunks.size());
2012-10-28 02:12:18 +02:00
}
2016-07-19 16:58:52 +02:00
unsigned int RIFF::File::chunkDataSize(unsigned int i) const
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
2018-06-06 22:47:08 +02:00
debug("RIFF::File::chunkDataSize() - Index out of range. Returning 0.");
2016-07-19 16:58:52 +02:00
return 0;
}
2012-10-28 02:12:18 +02:00
return d->chunks[i].size;
}
2016-07-19 16:58:52 +02:00
unsigned int RIFF::File::chunkOffset(unsigned int i) const
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
2018-06-06 22:47:08 +02:00
debug("RIFF::File::chunkOffset() - Index out of range. Returning 0.");
2016-07-19 16:58:52 +02:00
return 0;
}
2012-10-28 02:12:18 +02:00
return d->chunks[i].offset;
}
2016-07-19 16:58:52 +02:00
unsigned int RIFF::File::chunkPadding(unsigned int i) const
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
debug("RIFF::File::chunkPadding() - Index out of range. Returning 0.");
return 0;
}
2012-10-28 02:12:18 +02:00
return d->chunks[i].padding;
}
2016-07-19 16:58:52 +02:00
ByteVector RIFF::File::chunkName(unsigned int i) const
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
debug("RIFF::File::chunkName() - Index out of range. Returning an empty vector.");
return ByteVector();
}
2012-10-28 02:12:18 +02:00
return d->chunks[i].name;
}
2016-07-19 16:58:52 +02:00
ByteVector RIFF::File::chunkData(unsigned int i)
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
debug("RIFF::File::chunkData() - Index out of range. Returning an empty vector.");
return ByteVector();
}
2012-10-28 02:12:18 +02:00
2015-11-24 19:36:24 +01:00
seek(d->chunks[i].offset);
2012-10-28 02:12:18 +02:00
return readBlock(d->chunks[i].size);
}
2016-07-19 16:58:52 +02:00
void RIFF::File::setChunkData(unsigned int i, const ByteVector &data)
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
debug("RIFF::File::setChunkData() - Index out of range.");
return;
}
2012-10-28 02:12:18 +02:00
// Now update the specific chunk
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
std::vector<Chunk>::iterator it = d->chunks.begin();
std::advance(it, i);
2018-06-06 22:47:08 +02:00
const long long originalSize = static_cast<long long>(it->size) + it->padding;
2016-07-19 16:58:52 +02:00
writeChunk(it->name, data, it->offset - 8, it->size + it->padding + 8);
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
it->size = data.size();
2018-06-06 22:47:08 +02:00
it->padding = data.size() % 2;
2016-07-19 16:58:52 +02:00
2018-06-06 22:47:08 +02:00
const long long diff = static_cast<long long>(it->size) + it->padding - originalSize;
2012-10-28 02:12:18 +02:00
// Now update the internal offsets
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
for(++it; it != d->chunks.end(); ++it)
2018-06-06 22:47:08 +02:00
it->offset += static_cast<int>(diff);
2016-07-19 16:58:52 +02:00
// Update the global size.
updateGlobalSize();
}
2012-10-28 02:12:18 +02:00
void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data)
{
setChunkData(name, data, false);
}
2012-10-28 02:12:18 +02:00
void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data, bool alwaysCreate)
{
2018-06-06 22:47:08 +02:00
if(d->chunks.empty()) {
debug("RIFF::File::setChunkData - No valid chunks found.");
return;
}
2012-10-28 02:12:18 +02:00
if(alwaysCreate && name != "LIST") {
debug("RIFF::File::setChunkData - alwaysCreate should be used for only \"LIST\" chunks.");
return;
}
2012-10-28 02:12:18 +02:00
if(!alwaysCreate) {
2016-07-19 16:58:52 +02:00
for(unsigned int i = 0; i < d->chunks.size(); i++) {
if(d->chunks[i].name == name) {
setChunkData(i, data);
return;
}
2012-10-28 02:12:18 +02:00
}
}
// Couldn't find an existing chunk, so let's create a new one.
2016-07-19 16:58:52 +02:00
// Adjust the padding of the last chunk to place the new chunk at even position.
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
Chunk &last = d->chunks.back();
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
long offset = last.offset + last.size + last.padding;
if(offset & 1) {
if(last.padding == 1) {
last.padding = 0; // This should not happen unless the file is corrupted.
offset--;
removeBlock(offset, 1);
}
else {
insert(ByteVector("\0", 1), offset, 0);
last.padding = 1;
offset++;
}
}
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
// Now add the chunk to the file.
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
writeChunk(name, data, offset, 0);
2012-10-28 02:12:18 +02:00
// And update our internal structure
Chunk chunk;
2016-07-19 16:58:52 +02:00
chunk.name = name;
chunk.size = data.size();
chunk.offset = offset + 8;
chunk.padding = data.size() % 2;
2012-10-28 02:12:18 +02:00
d->chunks.push_back(chunk);
2016-07-19 16:58:52 +02:00
// Update the global size.
updateGlobalSize();
2012-10-28 02:12:18 +02:00
}
2016-07-19 16:58:52 +02:00
void RIFF::File::removeChunk(unsigned int i)
{
2016-07-19 16:58:52 +02:00
if(i >= d->chunks.size()) {
debug("RIFF::File::removeChunk() - Index out of range.");
return;
2016-07-19 16:58:52 +02:00
}
2015-11-24 19:36:24 +01:00
std::vector<Chunk>::iterator it = d->chunks.begin();
std::advance(it, i);
2016-07-19 16:58:52 +02:00
const unsigned int removeSize = it->size + it->padding + 8;
2015-11-24 19:36:24 +01:00
removeBlock(it->offset - 8, removeSize);
it = d->chunks.erase(it);
for(; it != d->chunks.end(); ++it)
it->offset -= removeSize;
2016-07-19 16:58:52 +02:00
// Update the global size.
updateGlobalSize();
}
void RIFF::File::removeChunk(const ByteVector &name)
{
2018-06-06 22:47:08 +02:00
for(int i = static_cast<int>(d->chunks.size()) - 1; i >= 0; --i) {
if(d->chunks[i].name == name)
2015-11-24 19:36:24 +01:00
removeChunk(i);
}
}
2012-10-28 02:12:18 +02:00
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void RIFF::File::read()
{
2016-07-19 16:58:52 +02:00
const bool bigEndian = (d->endianness == BigEndian);
long offset = tell();
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
offset += 4;
d->sizeOffset = offset;
seek(offset);
2012-10-28 02:12:18 +02:00
d->size = readBlock(4).toUInt(bigEndian);
2016-07-19 16:58:52 +02:00
offset += 8;
2012-10-28 02:12:18 +02:00
// + 8: chunk header at least, fix for additional junk bytes
2016-07-19 16:58:52 +02:00
while(offset + 8 <= length()) {
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
seek(offset);
const ByteVector chunkName = readBlock(4);
const unsigned int chunkSize = readBlock(4).toUInt(bigEndian);
if(!isValidChunkName(chunkName)) {
2012-10-28 02:12:18 +02:00
debug("RIFF::File::read() -- Chunk '" + chunkName + "' has invalid ID");
setValid(false);
break;
}
2018-06-06 22:47:08 +02:00
if(static_cast<long long>(offset) + 8 + chunkSize > length()) {
2012-10-28 02:12:18 +02:00
debug("RIFF::File::read() -- Chunk '" + chunkName + "' has invalid size (larger than the file size)");
setValid(false);
break;
}
Chunk chunk;
2018-06-06 22:47:08 +02:00
chunk.name = chunkName;
chunk.size = chunkSize;
chunk.offset = offset + 8;
chunk.padding = 0;
2016-07-19 16:58:52 +02:00
2018-06-06 22:47:08 +02:00
offset = chunk.offset + chunk.size;
2016-07-19 16:58:52 +02:00
// Check padding
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
if(offset & 1) {
2018-06-06 22:47:08 +02:00
seek(offset);
2016-07-19 16:58:52 +02:00
const ByteVector iByte = readBlock(1);
if(iByte.size() == 1 && iByte[0] == '\0') {
2012-10-28 02:12:18 +02:00
chunk.padding = 1;
2016-07-19 16:58:52 +02:00
offset++;
2012-10-28 02:12:18 +02:00
}
}
2016-07-19 16:58:52 +02:00
d->chunks.push_back(chunk);
2012-10-28 02:12:18 +02:00
}
}
void RIFF::File::writeChunk(const ByteVector &name, const ByteVector &data,
2016-07-19 16:58:52 +02:00
unsigned long offset, unsigned long replace)
2012-10-28 02:12:18 +02:00
{
ByteVector combined;
2016-07-19 16:58:52 +02:00
2012-10-28 02:12:18 +02:00
combined.append(name);
combined.append(ByteVector::fromUInt(data.size(), d->endianness == BigEndian));
combined.append(data);
2016-07-19 16:58:52 +02:00
if(data.size() & 1)
combined.resize(combined.size() + 1, '\0');
2012-10-28 02:12:18 +02:00
insert(combined, offset, replace);
}
2016-07-19 16:58:52 +02:00
void RIFF::File::updateGlobalSize()
{
const Chunk first = d->chunks.front();
const Chunk last = d->chunks.back();
d->size = last.offset + last.size + last.padding - first.offset + 12;
const ByteVector data = ByteVector::fromUInt(d->size, d->endianness == BigEndian);
insert(data, d->sizeOffset, 4);
}