REminiscence/unpack.cpp

107 lines
2.2 KiB
C++
Raw Normal View History

2016-03-20 17:00:00 +01:00
/*
* REminiscence - Flashback interpreter
2018-01-20 17:00:00 +01:00
* Copyright (C) 2005-2018 Gregory Montoir (cyx@users.sourceforge.net)
2015-08-02 18:00:00 +02:00
*/
#include "unpack.h"
2018-03-28 18:00:00 +02:00
#include "util.h"
2015-08-02 18:00:00 +02:00
struct UnpackCtx {
2018-03-28 18:00:00 +02:00
int size;
2015-08-02 18:00:00 +02:00
uint32_t crc;
uint32_t bits;
uint8_t *dst;
const uint8_t *src;
};
2018-02-24 17:00:00 +01:00
static bool nextBit(UnpackCtx *uc) {
bool carry = (uc->bits & 1) != 0;
2015-08-02 18:00:00 +02:00
uc->bits >>= 1;
if (uc->bits == 0) {
uc->bits = READ_BE_UINT32(uc->src); uc->src -= 4;
uc->crc ^= uc->bits;
2018-02-24 17:00:00 +01:00
carry = (uc->bits & 1) != 0;
uc->bits = (1 << 31) | (uc->bits >> 1);
2015-08-02 18:00:00 +02:00
}
2018-02-24 17:00:00 +01:00
return carry;
2015-08-02 18:00:00 +02:00
}
2018-03-28 18:00:00 +02:00
static int getBits(UnpackCtx *uc, int count) {
int bits = 0;
for (int i = 0; i < count; ++i) {
bits <<= 1;
2015-08-02 18:00:00 +02:00
if (nextBit(uc)) {
2018-03-28 18:00:00 +02:00
bits |= 1;
2015-08-02 18:00:00 +02:00
}
}
2018-03-28 18:00:00 +02:00
return bits;
2015-08-02 18:00:00 +02:00
}
2018-02-24 17:00:00 +01:00
static void copyLiteral(UnpackCtx *uc, int bitsCount, int len) {
2018-03-28 18:00:00 +02:00
int count = getBits(uc, bitsCount) + len + 1;
uc->size -= count;
if (uc->size < 0) {
count += uc->size;
uc->size = 0;
}
2018-02-24 17:00:00 +01:00
for (int i = 0; i < count; ++i) {
2018-03-28 18:00:00 +02:00
*(uc->dst - i) = (uint8_t)getBits(uc, 8);
2015-08-02 18:00:00 +02:00
}
2018-03-28 18:00:00 +02:00
uc->dst -= count;
2015-08-02 18:00:00 +02:00
}
2018-02-24 17:00:00 +01:00
static void copyReference(UnpackCtx *uc, int bitsCount, int count) {
2018-03-28 18:00:00 +02:00
uc->size -= count;
if (uc->size < 0) {
count += uc->size;
uc->size = 0;
}
const int offset = getBits(uc, bitsCount);
2018-02-24 17:00:00 +01:00
for (int i = 0; i < count; ++i) {
2018-03-28 18:00:00 +02:00
*(uc->dst - i) = *(uc->dst - i + offset);
2015-08-02 18:00:00 +02:00
}
2018-03-28 18:00:00 +02:00
uc->dst -= count;
2015-08-02 18:00:00 +02:00
}
2018-03-28 18:00:00 +02:00
bool delphine_unpack(uint8_t *dst, int dstSize, const uint8_t *src, int srcSize) {
2015-08-02 18:00:00 +02:00
UnpackCtx uc;
2018-03-28 18:00:00 +02:00
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;
2015-08-02 18:00:00 +02:00
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)) {
2018-02-24 17:00:00 +01:00
copyLiteral(&uc, 3, 0);
2015-08-02 18:00:00 +02:00
} else {
2018-02-24 17:00:00 +01:00
copyReference(&uc, 8, 2);
2015-08-02 18:00:00 +02:00
}
} else {
2018-02-24 17:00:00 +01:00
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;
2015-08-02 18:00:00 +02:00
}
}
2018-03-28 18:00:00 +02:00
} while (uc.size > 0);
assert(uc.size == 0);
2015-08-02 18:00:00 +02:00
return uc.crc == 0;
}