Initial commit
This commit is contained in:
125
engines/awe/unpack.cpp
Normal file
125
engines/awe/unpack.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/textconsole.h"
|
||||
#include "awe/unpack.h"
|
||||
|
||||
namespace Awe {
|
||||
|
||||
struct UnpackCtx {
|
||||
int size;
|
||||
uint32 crc;
|
||||
uint32 bits;
|
||||
uint8 *dst;
|
||||
const uint8 *src;
|
||||
};
|
||||
|
||||
static bool nextBit(UnpackCtx *uc) {
|
||||
bool carry = (uc->bits & 1) != 0;
|
||||
uc->bits >>= 1;
|
||||
if (uc->bits == 0) { // getnextlwd
|
||||
uc->bits = READ_BE_UINT32(uc->src); uc->src -= 4;
|
||||
uc->crc ^= uc->bits;
|
||||
carry = (uc->bits & 1) != 0;
|
||||
uc->bits = (1 << 31) | (uc->bits >> 1);
|
||||
}
|
||||
return carry;
|
||||
}
|
||||
|
||||
static int getBits(UnpackCtx *uc, int count) { // rdd1bits
|
||||
int bits = 0;
|
||||
for (int i = 0; i < count; ++i) {
|
||||
bits <<= 1;
|
||||
if (nextBit(uc)) {
|
||||
bits |= 1;
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
static void copyLiteral(UnpackCtx *uc, int bitsCount, int len) { // getd3chr
|
||||
int count = getBits(uc, bitsCount) + len + 1;
|
||||
uc->size -= count;
|
||||
if (uc->size < 0) {
|
||||
count += uc->size;
|
||||
uc->size = 0;
|
||||
}
|
||||
for (int i = 0; i < count; ++i) {
|
||||
*(uc->dst - i) = (uint8)getBits(uc, 8);
|
||||
}
|
||||
uc->dst -= count;
|
||||
}
|
||||
|
||||
static void copyReference(UnpackCtx *uc, int bitsCount, int count) { // copyd3bytes
|
||||
uc->size -= count;
|
||||
if (uc->size < 0) {
|
||||
count += uc->size;
|
||||
uc->size = 0;
|
||||
}
|
||||
const int offset = getBits(uc, bitsCount);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
*(uc->dst - i) = *(uc->dst - i + offset);
|
||||
}
|
||||
uc->dst -= count;
|
||||
}
|
||||
|
||||
bool bytekiller_unpack(uint8 *dst, int dstSize, const uint8 *src, int srcSize) {
|
||||
UnpackCtx uc;
|
||||
uc.src = src + srcSize - 4;
|
||||
uc.size = READ_BE_UINT32(uc.src); uc.src -= 4;
|
||||
if (uc.size > dstSize) {
|
||||
warning("Unexpected unpack size %d, buffer size %d", uc.size, dstSize);
|
||||
return false;
|
||||
}
|
||||
uc.dst = dst + uc.size - 1;
|
||||
uc.crc = READ_BE_UINT32(uc.src); uc.src -= 4;
|
||||
uc.bits = READ_BE_UINT32(uc.src); uc.src -= 4;
|
||||
uc.crc ^= uc.bits;
|
||||
do {
|
||||
if (!nextBit(&uc)) {
|
||||
if (!nextBit(&uc)) {
|
||||
copyLiteral(&uc, 3, 0);
|
||||
} else {
|
||||
copyReference(&uc, 8, 2);
|
||||
}
|
||||
} else {
|
||||
const int code = getBits(&uc, 2);
|
||||
switch (code) {
|
||||
case 3:
|
||||
copyLiteral(&uc, 8, 8);
|
||||
break;
|
||||
case 2:
|
||||
copyReference(&uc, 12, getBits(&uc, 8) + 1);
|
||||
break;
|
||||
case 1:
|
||||
copyReference(&uc, 10, 4);
|
||||
break;
|
||||
case 0:
|
||||
copyReference(&uc, 9, 3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (uc.size > 0);
|
||||
assert(uc.size == 0);
|
||||
return uc.crc == 0;
|
||||
}
|
||||
|
||||
} // namespace Awe
|
||||
Reference in New Issue
Block a user