public:
FilePrivate(Endianness endianness) :
endianness(endianness),
- size(0) {}
+ size(0),
+ sizeOffset(0) {}
const Endianness endianness;
unsigned int size;
+ long sizeOffset;
+
std::vector<Chunk> chunks;
};
void RIFF::File::setChunkData(unsigned int i, const ByteVector &data)
{
- // First we update the global size
-
- d->size += ((data.size() + 1) & ~1) - (d->chunks[i].size + d->chunks[i].padding);
- insert(ByteVector::fromUInt(d->size, d->endianness == BigEndian), 4, 4);
-
// Now update the specific chunk
writeChunk(chunkName(i), data, d->chunks[i].offset - 8, d->chunks[i].size + d->chunks[i].padding + 8);
for(i++; i < d->chunks.size(); i++)
d->chunks[i].offset = d->chunks[i-1].offset + 8 + d->chunks[i-1].size + d->chunks[i-1].padding;
+
+ // Update the global size.
+
+ updateGlobalSize();
}
void RIFF::File::setChunkData(const ByteVector &name, const ByteVector &data)
unsigned long offset = d->chunks.back().offset + d->chunks.back().size;
- // First we update the global size
-
- d->size += (offset & 1) + data.size() + 8;
- insert(ByteVector::fromUInt(d->size, d->endianness == BigEndian), 4, 4);
-
// Now add the chunk to the file
writeChunk(name, data, offset, std::max<long>(0, length() - offset), (offset & 1) ? 1 : 0);
chunk.padding = (data.size() & 0x01) ? 1 : 0;
d->chunks.push_back(chunk);
+
+ // Update the global size.
+
+ updateGlobalSize();
}
void RIFF::File::removeChunk(unsigned int i)
for(; it != d->chunks.end(); ++it)
it->offset -= removeSize;
+
+ // Update the global size.
+
+ updateGlobalSize();
}
void RIFF::File::removeChunk(const ByteVector &name)
seek(baseOffset + 4);
d->size = readBlock(4).toUInt(bigEndian);
+ d->sizeOffset = baseOffset + 4;
seek(baseOffset + 12);
}
insert(combined, offset, replace);
}
+
+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);
+}
CPPUNIT_ASSERT_EQUAL((unsigned int)(3), f.chunkDataSize(3));
CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f.chunkName(3));
CPPUNIT_ASSERT_EQUAL((unsigned int)(1), f.chunkPadding(3));
- CPPUNIT_ASSERT_EQUAL((unsigned int)(4411 - 8), f.riffSize());
+ CPPUNIT_ASSERT_EQUAL((unsigned int)(4412 - 8), f.riffSize());
}
{
PublicRIFF f(filename.c_str());
PublicRIFF f(filename.c_str());
+ CPPUNIT_ASSERT_EQUAL(5928U, f.riffSize());
+ CPPUNIT_ASSERT_EQUAL(5936L, f.length());
CPPUNIT_ASSERT_EQUAL(ByteVector("COMM"), f.chunkName(0));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x000C + 8), f.chunkOffset(0));
CPPUNIT_ASSERT_EQUAL(ByteVector("SSND"), f.chunkName(1));
const ByteVector data(0x400, ' ');
f.setChunkData("SSND", data);
+ CPPUNIT_ASSERT_EQUAL(1070U, f.riffSize());
+ CPPUNIT_ASSERT_EQUAL(1078L, f.length());
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x000C + 8), f.chunkOffset(0));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x0026 + 8), f.chunkOffset(1));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x042E + 8), f.chunkOffset(2));
CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f.readBlock(4));
f.setChunkData(0, data);
+ CPPUNIT_ASSERT_EQUAL(2076U, f.riffSize());
+ CPPUNIT_ASSERT_EQUAL(2084L, f.length());
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x000C + 8), f.chunkOffset(0));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x0414 + 8), f.chunkOffset(1));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x081C + 8), f.chunkOffset(2));
CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f.readBlock(4));
f.removeChunk("SSND");
+ CPPUNIT_ASSERT_EQUAL(1044U, f.riffSize());
+ CPPUNIT_ASSERT_EQUAL(1052L, f.length());
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x000C + 8), f.chunkOffset(0));
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x0414 + 8), f.chunkOffset(1));
CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f.readBlock(4));
f.removeChunk(0);
+ CPPUNIT_ASSERT_EQUAL(12U, f.riffSize());
+ CPPUNIT_ASSERT_EQUAL(20L, f.length());
CPPUNIT_ASSERT_EQUAL((unsigned int)(0x000C + 8), f.chunkOffset(0));
f.seek(f.chunkOffset(0) - 8);