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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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() {
|
|
|
|
uint32 value = READ_BE_UINT32(_src);
|
|
|
|
_src -= 4;
|
|
|
|
return value;
|
|
|
|
}
|
2007-05-24 21:19:52 +00:00
|
|
|
|
2008-06-10 19:08:53 +00:00
|
|
|
int CineUnpacker::rcr(int CF) {
|
|
|
|
int rCF = (_chk & 1);
|
|
|
|
_chk >>= 1;
|
2006-02-25 18:26:16 +00:00
|
|
|
if (CF) {
|
2008-06-10 19:08:53 +00:00
|
|
|
_chk |= 0x80000000;
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
|
|
|
return rCF;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-10 20:57:22 +00:00
|
|
|
int CineUnpacker::nextBit() {
|
2008-06-10 19:08:53 +00:00
|
|
|
int CF = rcr(0);
|
|
|
|
if (_chk == 0) {
|
|
|
|
_chk = readSource();
|
|
|
|
_crc ^= _chk;
|
|
|
|
CF = rcr(1);
|
2006-02-25 18:26:16 +00:00
|
|
|
}
|
|
|
|
return CF;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2008-06-10 20:57:22 +00:00
|
|
|
uint16 CineUnpacker::getBits(byte numBits) {
|
2006-02-25 18:26:16 +00:00
|
|
|
uint16 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-10 21:44:59 +00:00
|
|
|
void CineUnpacker::unpackBytes(uint16 numBytes) {
|
|
|
|
_datasize -= numBytes;
|
|
|
|
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-10 21:34:26 +00:00
|
|
|
void CineUnpacker::copyRelocatedBytes(uint16 offset, uint16 numBytes) {
|
|
|
|
_datasize -= numBytes;
|
|
|
|
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-10 19:08:53 +00:00
|
|
|
bool CineUnpacker::unpack(byte *dst, const byte *src, int srcLen) {
|
|
|
|
_src = src + srcLen - 4;
|
|
|
|
_datasize = readSource();
|
|
|
|
_dst = dst + _datasize - 1;
|
|
|
|
_crc = readSource();
|
|
|
|
_chk = readSource();
|
|
|
|
_crc ^= _chk;
|
2006-02-25 18:26:16 +00:00
|
|
|
do {
|
2008-06-10 20:57:22 +00:00
|
|
|
if (!nextBit()) {
|
|
|
|
if (!nextBit()) {
|
2008-06-10 21:44:59 +00:00
|
|
|
uint16 numBytes = getBits(3) + 1;
|
|
|
|
unpackBytes(numBytes);
|
2006-02-25 18:26:16 +00:00
|
|
|
} else {
|
2008-06-10 21:34:26 +00:00
|
|
|
uint16 numBytes = 2;
|
|
|
|
uint16 offset = getBits(8);
|
|
|
|
copyRelocatedBytes(offset, numBytes);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-02-25 18:26:16 +00:00
|
|
|
} else {
|
2008-06-10 20:57:22 +00:00
|
|
|
uint16 c = getBits(2);
|
2006-02-25 18:26:16 +00:00
|
|
|
if (c == 3) {
|
2008-06-10 21:44:59 +00:00
|
|
|
uint16 numBytes = getBits(8) + 9;
|
|
|
|
unpackBytes(numBytes);
|
2008-06-10 21:34:26 +00:00
|
|
|
} else if (c < 2) { // c == 0 || c == 1
|
|
|
|
uint16 numBytes = c + 3;
|
|
|
|
uint16 offset = getBits(c + 9);
|
|
|
|
copyRelocatedBytes(offset, numBytes);
|
|
|
|
} else { // c == 2
|
|
|
|
uint16 numBytes = getBits(8) + 1;
|
|
|
|
uint16 offset = getBits(12);
|
|
|
|
copyRelocatedBytes(offset, numBytes);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-10 19:08:53 +00:00
|
|
|
} while (_datasize > 0 && _src >= src - 4);
|
|
|
|
return _crc == 0;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-02-25 00:26:14 +00:00
|
|
|
|
|
|
|
} // End of namespace Cine
|