2019-05-16 20:23:37 +02:00
|
|
|
#include <QFile>
|
2019-05-10 18:56:19 +02:00
|
|
|
#include "img.h"
|
|
|
|
#include "subfile.h"
|
|
|
|
|
2019-05-16 20:23:37 +02:00
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
bool SubFile::seek(Handle &handle, quint32 pos) const
|
|
|
|
{
|
2019-05-16 20:23:37 +02:00
|
|
|
if (_file)
|
|
|
|
return _file->seek(pos);
|
|
|
|
else {
|
|
|
|
quint32 blockSize = _img->blockSize();
|
|
|
|
int blockNum = pos / blockSize;
|
|
|
|
|
|
|
|
if (handle.blockNum != blockNum) {
|
2019-09-05 22:31:13 +02:00
|
|
|
if (blockNum >= _blocks->size())
|
2019-05-16 20:23:37 +02:00
|
|
|
return false;
|
2019-09-05 22:31:13 +02:00
|
|
|
if (!_img->readBlock(_blocks->at(blockNum), handle.data))
|
2019-05-16 20:23:37 +02:00
|
|
|
return false;
|
|
|
|
handle.blockNum = blockNum;
|
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-05-16 20:23:37 +02:00
|
|
|
handle.blockPos = pos % blockSize;
|
|
|
|
handle.pos = pos;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-05-16 20:23:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SubFile::readByte(Handle &handle, quint8 &val) const
|
|
|
|
{
|
2019-05-16 20:23:37 +02:00
|
|
|
if (_file)
|
|
|
|
return _file->getChar((char*)&val);
|
|
|
|
else {
|
|
|
|
val = handle.data.at(handle.blockPos++);
|
|
|
|
handle.pos++;
|
|
|
|
return (handle.blockPos >= _img->blockSize())
|
|
|
|
? seek(handle, handle.pos) : true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:37:33 +02:00
|
|
|
bool SubFile::readVUInt32(Handle &hdl, quint32 &val) const
|
|
|
|
{
|
|
|
|
quint8 bytes, shift, b;
|
|
|
|
|
|
|
|
if (!readByte(hdl, b))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((b & 1) == 0) {
|
|
|
|
if ((b & 2) == 0) {
|
|
|
|
bytes = ((b >> 2) & 1) ^ 3;
|
|
|
|
shift = 5;
|
|
|
|
} else {
|
|
|
|
shift = 6;
|
|
|
|
bytes = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shift = 7;
|
|
|
|
bytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = b >> (8 - shift);
|
|
|
|
|
|
|
|
for (int i = 1; i <= bytes; i++) {
|
|
|
|
if (!readByte(hdl, b))
|
|
|
|
return false;
|
|
|
|
val |= (((quint32)b) << (i * 8)) >> (8 - shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-21 21:50:13 +01:00
|
|
|
bool SubFile::readVBitfield32(Handle &hdl, quint32 &bitfield) const
|
|
|
|
{
|
|
|
|
quint8 bits;
|
|
|
|
|
|
|
|
if (!readUInt8(hdl, bits))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(bits & 1)) {
|
|
|
|
seek(hdl, hdl.pos - 1);
|
|
|
|
if (!((bits>>1) & 1)) {
|
|
|
|
if (!((bits>>2) & 1)) {
|
|
|
|
if (!readUInt32(hdl, bitfield))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!readUInt24(hdl, bitfield))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bitfield >>= 3;
|
|
|
|
} else {
|
|
|
|
if (!readUInt16(hdl, bitfield))
|
|
|
|
return false;
|
|
|
|
bitfield >>= 2;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
bitfield = bits>>1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-16 20:23:37 +02:00
|
|
|
QString SubFile::fileName() const
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
2019-08-12 22:20:12 +02:00
|
|
|
return _file ? _file->fileName() : _img->fileName();
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|