aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Montoir2007-05-24 21:19:52 +0000
committerGregory Montoir2007-05-24 21:19:52 +0000
commit659e7ed76e010fcfa6c742add172bf96051cb5d8 (patch)
tree941a1aa65467b763e8c317723dc8c8d7b1d68b8c
parent0c22443ed680a714a2ffd497ca3d6f53722620dc (diff)
downloadscummvm-rg350-659e7ed76e010fcfa6c742add172bf96051cb5d8.tar.gz
scummvm-rg350-659e7ed76e010fcfa6c742add172bf96051cb5d8.tar.bz2
scummvm-rg350-659e7ed76e010fcfa6c742add172bf96051cb5d8.zip
sync resource unpacking code of cruise with cine
svn-id: r26933
-rw-r--r--engines/cine/unpack.cpp8
-rw-r--r--engines/cine/unpack.h8
-rw-r--r--engines/cruise/cruise_main.cpp15
-rw-r--r--engines/cruise/cruise_main.h2
-rw-r--r--engines/cruise/delphine-unpack.cpp209
-rw-r--r--engines/cruise/overlay.cpp25
-rw-r--r--engines/cruise/volume.cpp7
7 files changed, 93 insertions, 181 deletions
diff --git a/engines/cine/unpack.cpp b/engines/cine/unpack.cpp
index 597d412123..f15c46a84e 100644
--- a/engines/cine/unpack.cpp
+++ b/engines/cine/unpack.cpp
@@ -29,6 +29,14 @@
namespace Cine {
+struct UnpackCtx {
+ int size, datasize;
+ uint32 crc;
+ uint32 chk;
+ byte *dst;
+ const byte *src;
+};
+
static int rcr(UnpackCtx *uc, int CF) {
int rCF = (uc->chk & 1);
uc->chk >>= 1;
diff --git a/engines/cine/unpack.h b/engines/cine/unpack.h
index eadf19d947..5a57f836a0 100644
--- a/engines/cine/unpack.h
+++ b/engines/cine/unpack.h
@@ -30,14 +30,6 @@
namespace Cine {
-struct UnpackCtx {
- int size, datasize;
- uint32 crc;
- uint32 chk;
- byte *dst;
- const byte *src;
-};
-
bool delphineUnpack(byte *dst, const byte *src, int len);
} // End of namespace Cine
diff --git a/engines/cruise/cruise_main.cpp b/engines/cruise/cruise_main.cpp
index 1f3c1f2f6a..dcc8777bad 100644
--- a/engines/cruise/cruise_main.cpp
+++ b/engines/cruise/cruise_main.cpp
@@ -23,6 +23,7 @@
*/
#include "common/stdafx.h"
+#include "common/endian.h"
#include "common/events.h"
#include "cruise/cruise_main.h"
@@ -384,27 +385,17 @@ int loadFileSub1(uint8 **ptr, uint8 *name, uint8 *ptr2) {
lastFileSize = unpackedSize;
if (volumePtrToFileDescriptor[fileIdx].size + 2 != unpackedSize) {
- unsigned short int realUnpackedSize;
- uint8 *tempBuffer;
uint8 *pakedBuffer =
(uint8 *) mallocAndZero(volumePtrToFileDescriptor[fileIdx].
size + 2);
loadPakedFileToMem(fileIdx, pakedBuffer);
- realUnpackedSize =
- *(uint16 *) (pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 2);
- flipShort((int16 *) & realUnpackedSize);
+ uint32 realUnpackedSize = READ_BE_UINT32(pakedBuffer + volumePtrToFileDescriptor[fileIdx].size - 4);
lastFileSize = realUnpackedSize;
- tempBuffer = (uint8 *) mallocAndZero(realUnpackedSize);
-
- decomp((uint8 *) pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 4,
- (uint8 *) unpackedBuffer + realUnpackedSize,
- realUnpackedSize);
+ delphineUnpack(unpackedBuffer, pakedBuffer, volumePtrToFileDescriptor[fileIdx].size);
free(pakedBuffer);
} else {
diff --git a/engines/cruise/cruise_main.h b/engines/cruise/cruise_main.h
index df8550e1a1..a36d8a066e 100644
--- a/engines/cruise/cruise_main.h
+++ b/engines/cruise/cruise_main.h
@@ -66,7 +66,7 @@ namespace Cruise {
#define ASSERT_PTR assert
#define ASSERT assert
-int32 decomp(uint8 * in, uint8 * out, int32 size);
+bool delphineUnpack(byte *dst, const byte *src, int len);
ovlData3Struct *getOvlData3Entry(int32 scriptNumber, int32 param);
ovlData3Struct *scriptFunc1Sub2(int32 scriptNumber, int32 param);
diff --git a/engines/cruise/delphine-unpack.cpp b/engines/cruise/delphine-unpack.cpp
index fe32cbb018..611d1573fd 100644
--- a/engines/cruise/delphine-unpack.cpp
+++ b/engines/cruise/delphine-unpack.cpp
@@ -22,154 +22,99 @@
*
*/
-#include "cruise/cruise_main.h"
+#include "common/stdafx.h"
+#include "common/endian.h"
namespace Cruise {
-uint32 crc; // variable at 5C5A
-uint32 bitbucket; // dx:bx
-
-uint16 swap16(uint16 r) {
- return (r >> 8) | (r << 8);
+struct UnpackCtx {
+ int size, datasize;
+ uint32 crc;
+ uint32 chk;
+ byte *dst;
+ const byte *src;
+};
+
+static int rcr(UnpackCtx *uc, int CF) {
+ int rCF = (uc->chk & 1);
+ uc->chk >>= 1;
+ if (CF) {
+ uc->chk |= 0x80000000;
+ }
+ return rCF;
}
-#define loadd(p, d) {\
- d = *(--p);\
- d |= (*(--p)) << 8;\
- d |= (*(--p)) << 16;\
- d |= (*(--p)) << 24;\
+static int nextChunk(UnpackCtx *uc) {
+ int CF = rcr(uc, 0);
+ if (uc->chk == 0) {
+ uc->chk = READ_BE_UINT32(uc->src); uc->src -= 4;
+ uc->crc ^= uc->chk;
+ CF = rcr(uc, 1);
+ }
+ return CF;
}
-#define store(p, b) *(--p) = b
-#define getbit(p, b) {\
- b = (uint8)(bitbucket & 1);\
- bitbucket >>= 1;\
- if (!bitbucket) {\
- loadd(p, bitbucket);\
- crc ^= bitbucket;\
- b = (uint8)(bitbucket & 1);\
- bitbucket >>= 1;\
- bitbucket |= 0x80000000;\
- }\
+static uint16 getCode(UnpackCtx *uc, byte numChunks) {
+ uint16 c = 0;
+ while (numChunks--) {
+ c <<= 1;
+ if (nextChunk(uc)) {
+ c |= 1;
+ }
+ }
+ return c;
}
-#define loadbits(p, b) {\
- b = 0;\
- do {\
- getbit(p, bit);\
- b <<= 1;\
- b |= bit;\
- nbits--;\
- } while (nbits);\
+static void unpackHelper1(UnpackCtx *uc, byte numChunks, byte addCount) {
+ uint16 count = getCode(uc, numChunks) + addCount + 1;
+ uc->datasize -= count;
+ while (count--) {
+ *uc->dst = (byte)getCode(uc, 8);
+ --uc->dst;
+ }
}
-int32 decomp(uint8 *in, uint8 *out, int32 size) {
- uint8 bit = 0; // Carry flag
- uint8 nbits = 0; // cl
- uint8 byte = 0; // ch
- uint16 counter = 0; // bp
- uint16 var = 0; // variable at 5C58
- uint16 ptr = 0;
- uint16 flags = 0;
- enum {
- DO_COPY,
- DO_UNPACK
- } action;
-
- loadd(in, crc);
- loadd(in, bitbucket);
- crc ^= bitbucket;
+static void unpackHelper2(UnpackCtx *uc, byte numChunks) {
+ uint16 i = getCode(uc, numChunks);
+ uint16 count = uc->size + 1;
+ uc->datasize -= count;
+ while (count--) {
+ *uc->dst = *(uc->dst + i);
+ --uc->dst;
+ }
+}
- do { // 5A4C
- getbit(in, bit);
- if (!bit) { // 5A94
- getbit(in, bit);
- if (!bit) { // 5AC8
- nbits = 3;
- byte = 0;
- action = DO_COPY;
- } else { // 5ACA
- var = 1;
- nbits = 8;
- action = DO_UNPACK;
+bool delphineUnpack(byte *dst, const byte *src, int len) {
+ UnpackCtx uc;
+ uc.src = src + len - 4;
+ uc.datasize = READ_BE_UINT32(uc.src); uc.src -= 4;
+ uc.dst = dst + uc.datasize - 1;
+ uc.size = 0;
+ uc.crc = READ_BE_UINT32(uc.src); uc.src -= 4;
+ uc.chk = READ_BE_UINT32(uc.src); uc.src -= 4;
+ uc.crc ^= uc.chk;
+ do {
+ if (!nextChunk(&uc)) {
+ uc.size = 1;
+ if (!nextChunk(&uc)) {
+ unpackHelper1(&uc, 3, 0);
+ } else {
+ unpackHelper2(&uc, 8);
}
- } else { // 5B4F
- nbits = 2;
- loadbits(in, flags);
- if (flags < 2) {
- nbits = flags + 9; // 5BC3
- var = flags + 2;
- action = DO_UNPACK;
- } else if (flags == 3) {
- nbits = 8; // 5B4A
- byte = 8;
- action = DO_COPY;
+ } else {
+ uint16 c = getCode(&uc, 2);
+ if (c == 3) {
+ unpackHelper1(&uc, 8, 8);
+ } else if (c < 2) {
+ uc.size = c + 2;
+ unpackHelper2(&uc, c + 9);
} else {
- nbits = 8;
- loadbits(in, var);
- nbits = 12;
- action = DO_UNPACK;
+ uc.size = getCode(&uc, 8);
+ unpackHelper2(&uc, 12);
}
}
-
- switch (action) {
- case DO_COPY:
- // 5AD1
- loadbits(in, counter); // 5AFD
- counter += byte;
- counter++;
- size -= counter;
- do {
- nbits = 8;
- loadbits(in, byte); // 5B3F
- store(out, byte);
- counter--;
- } while (counter); // 5B45
- break;
- case DO_UNPACK:
- // 5BD3
- loadbits(in, ptr); // 5BFF
- counter = var + 1;
- size -= counter;
- do {
- byte = *(out + ptr - 1);
- store(out, byte);
- counter--;
- } while (counter);
- }
- } while (size > 0);
- // 5C32
- // ???
-
- if (crc) {
- return -1;
- } else {
- return 0;
- }
+ } while (uc.datasize > 0);
+ return uc.crc == 0;
}
-/*
-int main(void) {
- FILE * in, * out;
- uint8 * bufin, * bufout;
- uint32 isize, osize;
-
- in = fopen("FIN.FR", "rb");
- out = fopen("FIN.FR.out", "wb");
-
- fseek(in, -4, SEEK_END);
- bufin = (uint8 *) mallocAndZero((isize = ftell(in)));
- fread(&osize, 4, 1, in);
- osize = (osize >> 24) | ((osize >> 8) & 0xff00) | ((osize << 8) & 0xff0000) | (osize << 24);
- bufout = (uint8 *) mallocAndZero(osize);
- fseek(in, 0, SEEK_SET);
- fread(bufin, 1, isize, in);
-
- decomp(bufin + isize, bufout + osize, osize);
-
- fwrite(bufout, 1, osize, out);
- fclose(out);
- fclose(in);
-}*/
-
} // End of namespace Cruise
diff --git a/engines/cruise/overlay.cpp b/engines/cruise/overlay.cpp
index af52f25e16..10e807380a 100644
--- a/engines/cruise/overlay.cpp
+++ b/engines/cruise/overlay.cpp
@@ -111,25 +111,13 @@ int loadOverlay(uint8 *scriptName) {
}
if (volumePtrToFileDescriptor[fileIdx].size + 2 != unpackedSize) {
- char *tempBuffer;
- uint16 realUnpackedSize;
char *pakedBuffer =
(char *)mallocAndZero(volumePtrToFileDescriptor[fileIdx].
size + 2);
loadPakedFileToMem(fileIdx, (uint8 *) pakedBuffer);
- realUnpackedSize =
- *(int16 *) (pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 2);
- flipShort(&realUnpackedSize);
-
- tempBuffer = (char *)mallocAndZero(realUnpackedSize);
-
- decomp((uint8 *) pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 4,
- (uint8 *) unpackedBuffer + realUnpackedSize,
- realUnpackedSize);
+ delphineUnpack((uint8 *)unpackedBuffer, (const uint8 *)pakedBuffer, volumePtrToFileDescriptor[fileIdx].size);
free(pakedBuffer);
} else {
@@ -559,7 +547,6 @@ int loadOverlay(uint8 *scriptName) {
if (volumePtrToFileDescriptor[fileIdx].size + 2 !=
unpackedSize) {
- short int realUnpackedSize;
char *pakedBuffer =
(char *)
mallocAndZero(volumePtrToFileDescriptor[fileIdx].
@@ -567,15 +554,7 @@ int loadOverlay(uint8 *scriptName) {
loadPakedFileToMem(fileIdx, (uint8 *) pakedBuffer);
- realUnpackedSize =
- *(int16 *) (pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 2);
- flipShort(&realUnpackedSize);
-
- decomp((uint8 *) pakedBuffer +
- volumePtrToFileDescriptor[fileIdx].size - 4,
- (uint8 *) unpackedBuffer + realUnpackedSize,
- realUnpackedSize);
+ delphineUnpack((uint8 *) unpackedBuffer, (const uint8 *)pakedBuffer, volumePtrToFileDescriptor[fileIdx].size);
free(pakedBuffer);
} else {
diff --git a/engines/cruise/volume.cpp b/engines/cruise/volume.cpp
index be8c0e926e..e4a00945e9 100644
--- a/engines/cruise/volume.cpp
+++ b/engines/cruise/volume.cpp
@@ -426,13 +426,10 @@ int16 readVolCnf(void) {
(char *)mallocAndZero(buffer[j].unk2 +
500);
- decomp((uint8 *) bufferLocal + buffer[j].size -
- 4,
- (uint8 *) uncompBuffer + buffer[j].unk2 +
- 500, buffer[j].unk2);
+ delphineUnpack((uint8 *) uncompBuffer, (const uint8 *) bufferLocal, buffer[j].size);
FILE *fOut = fopen(nameBuffer, "wb+");
- fwrite(uncompBuffer + 500, buffer[j].unk2, 1,
+ fwrite(uncompBuffer, buffer[j].unk2, 1,
fOut);
fclose(fOut);