1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*************************************************************************
RIFFChunk.cpp - information about a RIFF chunk
-------------------
begin : Tue Mar 20 2002
copyright : (C) 2002 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QList>
#include <QString>
#include "libkwave/String.h"
#include "libkwave/Utils.h"
#include "RIFFChunk.h"
//***************************************************************************
Kwave::RIFFChunk::RIFFChunk(RIFFChunk *parent, const QByteArray &name,
const QByteArray &format, quint32 length,
quint32 phys_offset, quint32 phys_length)
:m_type(Sub), m_name(name), m_format(format), m_parent(parent),
m_chunk_length(length), m_phys_offset(phys_offset),
m_phys_length(phys_length), m_sub_chunks()
{
}
//***************************************************************************
Kwave::RIFFChunk::~RIFFChunk()
{
while (!m_sub_chunks.isEmpty())
delete m_sub_chunks.takeLast();
}
//***************************************************************************
// #define CHECK(x) Q_ASSERT(!(x)); if (x) return false;
#define CHECK(x) if (x) return false;
bool Kwave::RIFFChunk::isSane() const
{
CHECK(m_type == Empty)
CHECK(m_type == Garbage)
CHECK((m_type == Main) && m_sub_chunks.isEmpty())
CHECK((m_type == Root) && m_sub_chunks.isEmpty())
#ifdef DEBUG
if (m_phys_length & 0x1) {
// size is not an even number: no criterium for insanity
// but worth a warning
qWarning("%s: physical length is not an even number: %u",
path().data(), m_phys_length);
}
#endif /* DEBUG */
unsigned int datalen = dataLength();
if (m_type == Main) datalen += 4;
if (((datalen + 1) < m_phys_length) || (datalen > m_phys_length)) {
qWarning("%s: dataLength=%u, phys_length=%u",
path().data(), datalen, m_phys_length);
return false;
}
foreach (const Kwave::RIFFChunk *chunk, subChunks())
if (chunk && !chunk->isSane()) return false;
return true;
}
//***************************************************************************
quint32 Kwave::RIFFChunk::physEnd() const
{
quint32 end = m_phys_offset + m_phys_length;
if (m_phys_length) --end;
if ((m_type != Root) && (m_type != Garbage)) end += 8;
return end;
}
//***************************************************************************
const QByteArray Kwave::RIFFChunk::path() const
{
QByteArray p = "";
if (m_parent) p += m_parent->path() + '/';
p += m_name;
if (m_type == Main) p += ':' + m_format;
if (m_parent) {
QListIterator<Kwave::RIFFChunk *> it(m_parent->subChunks());
unsigned int before = 0;
unsigned int after = 0;
const Kwave::RIFFChunk *chunk = nullptr;
while (it.hasNext()) {
chunk = it.next();
if (!chunk) continue;
if (chunk == this) break;
if (chunk->name() != m_name) continue;
if (chunk->type() != m_type) continue;
if ((m_type == Main) && (chunk->format() != m_format)) continue;
before++;
}
if ((chunk == this) && (it.hasNext())) chunk = it.next();
while ((chunk != this) && (it.hasNext())) {
chunk = it.next();
if (!chunk) continue;
if (chunk->name() != m_name) continue;
if (chunk->type() != m_type) continue;
if ((m_type == Main) && (chunk->format() != m_format)) continue;
after++;
}
if (before + after != 0) {
QByteArray index;
index.setNum(before);
p += '(' + index + ')';
}
}
return p;
}
//***************************************************************************
quint32 Kwave::RIFFChunk::dataStart() const
{
return m_phys_offset + ((m_type == Main) ? 12 : 8);
}
//***************************************************************************
quint32 Kwave::RIFFChunk::dataLength() const
{
return m_chunk_length - ((m_type == Main) ? 4 : 0);
}
//***************************************************************************
void Kwave::RIFFChunk::setLength(quint32 length)
{
m_chunk_length = length;
m_phys_length = length;
}
//***************************************************************************
bool Kwave::RIFFChunk::isChildOf(Kwave::RIFFChunk *chunk)
{
if (!chunk) return (m_type == Root); // only root has null as parent
if (chunk == m_parent) return true;
if (m_parent) return m_parent->isChildOf(chunk);
return false;
}
//***************************************************************************
void Kwave::RIFFChunk::fixSize()
{
// pass one: fix sizes of sub chunks recursively
foreach (Kwave::RIFFChunk *chunk, subChunks())
if (chunk) chunk->fixSize();
// pass two: sum up sub-chunks if type is main or root.
if ((m_type == Main) || (m_type == Root)) {
quint32 old_length = m_phys_length;
m_phys_length = 0;
if (m_type == Main) m_phys_length += 4;
foreach (const Kwave::RIFFChunk *chunk, subChunks()) {
if (!chunk) continue;
quint32 len = chunk->physEnd() - chunk->physStart() + 1;
m_phys_length += len;
}
if (m_phys_length != old_length) {
qDebug("%s: setting size from %u to %u",
path().data(), old_length, m_phys_length);
}
// chunk length is always equal to physical length for
// main and root !
m_chunk_length = m_phys_length;
} else {
// just round up if no main or root chunk
if (m_phys_length & 0x1) {
m_phys_length++;
qDebug("%s: rounding up size to %u", path().data(), m_phys_length);
}
// adjust chunk size to physical size if not long enough
if ((m_chunk_length+1 != m_phys_length) &&
(m_chunk_length != m_phys_length))
{
qDebug("%s: resizing chunk from %u to %u",
path().data(), m_chunk_length, m_phys_length);
m_chunk_length = m_phys_length;
}
}
}
//***************************************************************************
void Kwave::RIFFChunk::dumpStructure()
{
// translate the type into a user-readable string
const char *t = "?unknown?";
switch (m_type) {
case Root: t = "ROOT"; break;
case Main: t = "MAIN"; break;
case Sub: t = "SUB"; break;
case Garbage: t = "GARBAGE"; break;
case Empty: t = "EMPTY"; break;
DEFAULT_IGNORE;
}
// dump this chunk
qDebug("[0x%08X-0x%08X] (%10u/%10u) %7s, '%s'",
m_phys_offset, physEnd(), physLength(), length(),
t, path().data()
);
// recursively dump all sub-chunks
foreach (Kwave::RIFFChunk *chunk, m_sub_chunks)
if (chunk) chunk->dumpStructure();
}
//***************************************************************************
//***************************************************************************
|