2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-02-22 22:40:53 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2006-02-22 22:40:53 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/endian.h"
|
|
|
|
|
2006-02-25 01:18:01 +00:00
|
|
|
#include "cine/unpack.h"
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-02-25 00:26:14 +00:00
|
|
|
namespace Cine {
|
|
|
|
|
2008-06-10 19:08:53 +00:00
|
|
|
uint32 CineUnpacker::readSource() {
|
2008-06-13 05:57:07 +00:00
|
|
|
if (_src < _srcBegin || _src + 4 > _srcEnd) {
|
|
|
|
_error = true;
|
|
|
|
return 0; // The source pointer is out of bounds, returning a default value
|
|
|
|
}
|
2008-06-10 19:08:53 +00:00
|
|
|
uint32 value = READ_BE_UINT32(_src);
|
|
|
|
_src -= 4;
|
|
|
|
return value;
|
|
|
|
}
|
2007-05-24 21:19:52 +00:00
|
|
|
|
2008-06-13 08:28:14 +00:00
|
|
|
uint CineUnpacker::rcr(bool inputCarry) {
|
|
|
|
uint outputCarry = (_chunk32b & 1);
|
2008-06-10 22:37:55 +00:00
|
|
|
_chunk32b >>= 1;
|
|
|
|
if (inputCarry) {
|
|
|
|
_chunk32b |= 0x80000000;
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
2008-06-10 22:37:55 +00:00
|
|
|
return outputCarry;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 08:28:14 +00:00
|
|
|
uint CineUnpacker::nextBit() {
|
|
|
|
uint carry = rcr(false);
|
2008-06-11 11:06:07 +00:00
|
|
|
// Normally if the chunk becomes zero then the carry is one as
|
|
|
|
// the end of chunk marker is always the last to be shifted out.
|
2008-06-10 22:37:55 +00:00
|
|
|
if (_chunk32b == 0) {
|
|
|
|
_chunk32b = readSource();
|
|
|
|
_crc ^= _chunk32b;
|
2008-06-13 08:28:14 +00:00
|
|
|
carry = rcr(true); // Put the end of chunk marker in the most significant bit
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
2008-06-10 22:37:55 +00:00
|
|
|
return carry;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 08:28:14 +00:00
|
|
|
uint CineUnpacker::getBits(uint numBits) {
|
|
|
|
uint c = 0;
|
2008-06-10 20:57:22 +00:00
|
|
|
while (numBits--) {
|
2006-02-25 18:26:16 +00:00
|
|
|
c <<= 1;
|
2008-06-10 20:57:22 +00:00
|
|
|
c |= nextBit();
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
|
|
|
return c;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 08:28:14 +00:00
|
|
|
void CineUnpacker::unpackRawBytes(uint numBytes) {
|
2008-06-13 05:57:07 +00:00
|
|
|
if (_dst >= _dstEnd || _dst - numBytes + 1 < _dstBegin) {
|
|
|
|
_error = true;
|
|
|
|
return; // Destination pointer is out of bounds for this operation
|
|
|
|
}
|
2008-06-10 21:44:59 +00:00
|
|
|
while (numBytes--) {
|
2008-06-10 20:57:22 +00:00
|
|
|
*_dst = (byte)getBits(8);
|
2008-06-10 19:08:53 +00:00
|
|
|
--_dst;
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-06-13 08:28:14 +00:00
|
|
|
void CineUnpacker::copyRelocatedBytes(uint offset, uint numBytes) {
|
2008-06-13 05:57:07 +00:00
|
|
|
if (_dst + offset >= _dstEnd || _dst - numBytes + 1 < _dstBegin) {
|
|
|
|
_error = true;
|
|
|
|
return; // Destination pointer is out of bounds for this operation
|
|
|
|
}
|
2008-06-10 21:34:26 +00:00
|
|
|
while (numBytes--) {
|
|
|
|
*_dst = *(_dst + offset);
|
2008-06-10 19:08:53 +00:00
|
|
|
--_dst;
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-06-13 05:57:07 +00:00
|
|
|
bool CineUnpacker::unpack(const byte *src, uint srcLen, byte *dst, uint dstLen) {
|
|
|
|
// Initialize variables used for detecting errors during unpacking
|
|
|
|
_error = false;
|
|
|
|
_srcBegin = src;
|
|
|
|
_srcEnd = src + srcLen;
|
|
|
|
_dstBegin = dst;
|
|
|
|
_dstEnd = dst + dstLen;
|
|
|
|
|
2008-08-19 11:55:20 +00:00
|
|
|
// Handle already unpacked data here
|
|
|
|
if (srcLen == dstLen) {
|
|
|
|
// Source length is same as destination length so the source
|
|
|
|
// data is already unpacked. Let's just copy it then.
|
|
|
|
memcpy(dst, src, srcLen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-13 05:57:07 +00:00
|
|
|
// Initialize other variables
|
|
|
|
_src = _srcBegin + srcLen - 4;
|
|
|
|
uint32 unpackedLength = readSource(); // Unpacked length in bytes
|
|
|
|
_dst = _dstBegin + unpackedLength - 1;
|
2008-06-10 19:08:53 +00:00
|
|
|
_crc = readSource();
|
2008-06-10 22:37:55 +00:00
|
|
|
_chunk32b = readSource();
|
|
|
|
_crc ^= _chunk32b;
|
2008-06-13 05:57:07 +00:00
|
|
|
|
|
|
|
while (_dst >= _dstBegin && !_error) {
|
2008-06-11 11:06:07 +00:00
|
|
|
/*
|
|
|
|
Bits => Action:
|
2008-06-18 03:31:13 +00:00
|
|
|
0 0 => unpackRawBytes(3 bits + 1) i.e. unpackRawBytes(1..8)
|
2008-06-11 11:06:07 +00:00
|
|
|
1 1 1 => unpackRawBytes(8 bits + 9) i.e. unpackRawBytes(9..264)
|
|
|
|
0 1 => copyRelocatedBytes(8 bits, 2) i.e. copyRelocatedBytes(0..255, 2)
|
|
|
|
1 0 0 => copyRelocatedBytes(9 bits, 3) i.e. copyRelocatedBytes(0..511, 3)
|
|
|
|
1 0 1 => copyRelocatedBytes(10 bits, 4) i.e. copyRelocatedBytes(0..1023, 4)
|
|
|
|
1 1 0 => copyRelocatedBytes(12 bits, 8 bits + 1) i.e. copyRelocatedBytes(0..4095, 1..256)
|
|
|
|
*/
|
|
|
|
if (!nextBit()) { // 0...
|
|
|
|
if (!nextBit()) { // 0 0
|
2008-06-13 08:37:03 +00:00
|
|
|
uint numBytes = getBits(3) + 1;
|
2008-06-11 11:06:07 +00:00
|
|
|
unpackRawBytes(numBytes);
|
|
|
|
} else { // 0 1
|
2008-06-13 08:37:03 +00:00
|
|
|
uint numBytes = 2;
|
|
|
|
uint offset = getBits(8);
|
2008-06-10 21:34:26 +00:00
|
|
|
copyRelocatedBytes(offset, numBytes);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2008-06-11 11:06:07 +00:00
|
|
|
} else { // 1...
|
2008-06-13 08:37:03 +00:00
|
|
|
uint c = getBits(2);
|
2008-06-11 11:06:07 +00:00
|
|
|
if (c == 3) { // 1 1 1
|
2008-06-13 08:37:03 +00:00
|
|
|
uint numBytes = getBits(8) + 9;
|
2008-06-11 11:06:07 +00:00
|
|
|
unpackRawBytes(numBytes);
|
|
|
|
} else if (c < 2) { // 1 0 x
|
2008-06-13 08:37:03 +00:00
|
|
|
uint numBytes = c + 3;
|
|
|
|
uint offset = getBits(c + 9);
|
2008-06-10 21:34:26 +00:00
|
|
|
copyRelocatedBytes(offset, numBytes);
|
2008-06-11 11:06:07 +00:00
|
|
|
} else { // 1 1 0
|
2008-06-13 08:37:03 +00:00
|
|
|
uint numBytes = getBits(8) + 1;
|
|
|
|
uint offset = getBits(12);
|
2008-06-10 21:34:26 +00:00
|
|
|
copyRelocatedBytes(offset, numBytes);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-13 05:57:07 +00:00
|
|
|
}
|
|
|
|
return !_error && (_crc == 0);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-02-25 00:26:14 +00:00
|
|
|
|
|
|
|
} // End of namespace Cine
|