aboutsummaryrefslogtreecommitdiff
path: root/sky/rnc_deco.cpp
diff options
context:
space:
mode:
authorOliver Kiehl2003-03-05 21:50:41 +0000
committerOliver Kiehl2003-03-05 21:50:41 +0000
commit9612e866b90f1fe13048f64e3e32efb154addfaa (patch)
tree2a7f342d387512a27db61245f965fa0428a63157 /sky/rnc_deco.cpp
parent447820a77d619d22363f49223f89f84bf21d7250 (diff)
downloadscummvm-rg350-9612e866b90f1fe13048f64e3e32efb154addfaa.tar.gz
scummvm-rg350-9612e866b90f1fe13048f64e3e32efb154addfaa.tar.bz2
scummvm-rg350-9612e866b90f1fe13048f64e3e32efb154addfaa.zip
some more cleanup
svn-id: r6697
Diffstat (limited to 'sky/rnc_deco.cpp')
-rw-r--r--sky/rnc_deco.cpp211
1 files changed, 95 insertions, 116 deletions
diff --git a/sky/rnc_deco.cpp b/sky/rnc_deco.cpp
index 0cac411089..370861879c 100644
--- a/sky/rnc_deco.cpp
+++ b/sky/rnc_deco.cpp
@@ -19,7 +19,6 @@
*
*/
-#include "stdafx.h"
#include <string.h>
#include "common/scummsys.h"
@@ -28,18 +27,18 @@
#define XCHG(a, b) (a ^=b, b ^= a, a ^= b)
//conditional flags
-#define CHECKSUMS 1
-#define PROTECTED 0
+#define CHECKSUMS 1
+#define PROTECTED 0
//return codes
-#define NOT_PACKED 0
-#define PACKED_CRC -1
-#define UNPACKED_CRC -2
+#define NOT_PACKED 0
+#define PACKED_CRC -1
+#define UNPACKED_CRC -2
//other defines
-#define TABLE_SIZE (16*8)
-#define MIN_LENGTH 2
-#define HEADER_LEN 18
+#define TABLE_SIZE (16*8)
+#define MIN_LENGTH 2
+#define HEADER_LEN 18
uint16 raw_table[TABLE_SIZE/2];
uint16 pos_table[TABLE_SIZE/2];
@@ -73,16 +72,15 @@ void init_crc(void)
uint16 tmp2=0;
for (tmp2 = 0; tmp2 < 0x100; tmp2++) {
- tmp1 = tmp2;
+ tmp1 = tmp2;
for (cnt = 8; cnt > 0; cnt--) {
- if (tmp1 % 2) {
+ if (tmp1 % 2) {
tmp1 /= 2;
tmp1 ^= 0x0a001;
- }
- else
- tmp1 /= 2;
+ } else
+ tmp1 /= 2;
}
- crc_table[tmp2] = tmp1;
+ crc_table[tmp2] = tmp1;
}
}
@@ -93,15 +91,15 @@ uint16 crc_block(uint8 *block, uint32 size)
uint8 *crcTable8 = (uint8 *)crc_table; //make a uint8* to crc_table
uint8 tmp;
uint32 i;
-
+
for (i = 0; i < size; i++) {
tmp = *block++;
crc ^= tmp;
- tmp = (uint8)((crc>>8)&0x00FF);
- crc &= 0x00FF;
+ tmp = (uint8)((crc>>8)&0x00FF);
+ crc &= 0x00FF;
crc = crc << 1;
- crc = *(uint16 *)&crcTable8[crc];
- crc ^= tmp;
+ crc = *(uint16 *)&crcTable8[crc];
+ crc ^= tmp;
}
return crc;
@@ -111,7 +109,7 @@ uint16 input_bits(uint8 amount)
{
uint16 newBitBuffh = bit_buffh;
uint16 newBitBuffl = bit_buffl;
- int8 newBitCount = bit_count;
+ int16 newBitCount = bit_count;
uint16 remBits, returnVal;
returnVal = ((1 << amount) - 1) & newBitBuffl;
@@ -196,129 +194,110 @@ uint16 input_value(uint16 *table)
return value;
}
-uint32 UnpackM1(void *input, void *output, uint16 key)
+int UnpackM1(void *input, void *output, uint16 key)
{
- uint8 cl;
- uint8 *inputHigh, *outputLow;
- uint32 eax, ebx, ecx, edx;
- uint8 *inputptr = (uint8 *)input;
+ uint8 *inputHigh, *outputLow, *outputHigh;
+ uint8 *inputptr = (uint8 *)input;
- if (CHECKSUMS)
- init_crc();
+ if (CHECKSUMS)
+ init_crc();
- //Check for "RNC "
- if (READ_BE_UINT32(inputptr) != 0x524e4301)
- return NOT_PACKED;
+ //Check for "RNC "
+ if (READ_BE_UINT32(inputptr) != 0x524e4301)
+ return NOT_PACKED;
- inputptr += 4;
+ inputptr += 4;
- // read unpacked/packed file length
- unpack_len = READ_BE_UINT32(inputptr); inputptr += 4;
- pack_len = READ_BE_UINT32(inputptr); inputptr += 4;
+ // read unpacked/packed file length
+ unpack_len = READ_BE_UINT32(inputptr); inputptr += 4;
+ pack_len = READ_BE_UINT32(inputptr); inputptr += 4;
- blocks = *(inputptr+5);
+ blocks = *(inputptr+5);
- if (CHECKSUMS) {
- //read CRC's
- crc_u = READ_BE_UINT16(inputptr); inputptr += 2;
- crc_p = READ_BE_UINT16(inputptr); inputptr += 2;
-
- inputptr = (inputptr+HEADER_LEN-16);
-
- if (crc_block(inputptr, pack_len) != crc_p)
- return PACKED_CRC;
+ if (CHECKSUMS) {
+ //read CRC's
+ crc_u = READ_BE_UINT16(inputptr); inputptr += 2;
+ crc_p = READ_BE_UINT16(inputptr); inputptr += 2;
+ inputptr = (inputptr+HEADER_LEN-16);
- inputptr = (((uint8 *)input)+HEADER_LEN);
- esiptr = inputptr;
+ if (crc_block(inputptr, pack_len) != crc_p)
+ return PACKED_CRC;
- }
+ inputptr = (((uint8 *)input)+HEADER_LEN);
+ esiptr = inputptr;
+ }
+ // inputLow = *input
inputHigh = ((uint8 *)input) + pack_len + HEADER_LEN;;
outputLow = (uint8 *)output;
+ outputHigh = *(((uint8 *)input)+16) + unpack_len + outputLow;
- eax = *(((uint8 *)input)+16) + unpack_len;
-
- if (! ((inputHigh <= outputLow ) || ((outputLow+eax) <= inputHigh )) ) {
+ if (! ((inputHigh <= outputLow) || (outputHigh <= inputHigh)) ) {
esiptr = inputHigh;
- ediptr = (outputLow+eax);
- memcpy((ediptr-pack_len), (esiptr-pack_len), pack_len);
- esiptr = (ediptr-pack_len);
+ ediptr = outputHigh;
+ memcpy((ediptr-pack_len), (esiptr-pack_len), pack_len);
+ esiptr = (ediptr-pack_len);
}
-
//unpack3:
- ediptr = (uint8 *)output;
- bit_count = 0;
+ ediptr = (uint8 *)output;
+ bit_count = 0;
- bit_buffl = READ_LE_UINT16(esiptr);
- /*eax =*/ input_bits(2);
-
-
- //Argh! Labels!!
- unpack4:
- make_huftable(raw_table);
- make_huftable(pos_table);
- make_huftable(len_table);
-
- counts = input_bits(16);
+ bit_buffl = READ_LE_UINT16(esiptr);
+ input_bits(2);
- goto unpack6;
+ do {
+ make_huftable(raw_table);
+ make_huftable(pos_table);
+ make_huftable(len_table);
+
+ counts = input_bits(16);
+
+ for (;;) {
+ uint32 input_bits = input_value(raw_table);
+
+ if (input_bits) {
+ memcpy(ediptr, esiptr, input_bits); //memcpy is allowed here
+ ediptr += input_bits;
+ esiptr += input_bits;
+ uint16 b = READ_LE_UINT16(esiptr);
+ uint16 a = ROL(b, bit_count);
+ uint16 d = ((1 << bit_count) - 1);
+ bit_buffl &= d;
+ d &= a;
+
+ a = READ_LE_UINT16((esiptr+2));
+ b = (b << bit_count);
+ a = (a << bit_count);
+ a |= d;
+ bit_buffl |= b;
+ bit_buffh = a;
+ }
- unpack5:
+ if (--counts) {
+ uint32 input_offset = input_value(pos_table) + 1;
+ uint32 input_length = input_value(len_table) + MIN_LENGTH;
- eax = input_value(pos_table) + 1; //input offset
- ecx = input_value(len_table) + MIN_LENGTH; //input length
+ inputHigh = esiptr;
+ esiptr = (ediptr-input_offset);
- inputHigh = esiptr;
- esiptr = (ediptr-eax);
+ //Don't use memcpy here! because input and output overlap
+ while (input_length--)
+ *ediptr++ = *esiptr++;
- //Don't use memcpy here! because input and output overlap
- while (ecx) {
- *ediptr++ = *esiptr++; ecx--;
- }
-
- esiptr = inputHigh;
+ esiptr = inputHigh;
+ } else
+ break;
- unpack6:
-
- ecx = input_value(raw_table);
-
- if (ecx == 0)
- goto unpack7;
-
- memcpy(ediptr, esiptr, ecx); //memcpy is allowed here
- ediptr += ecx;
- esiptr += ecx;
- cl = bit_count;
- eax = READ_LE_UINT16(esiptr);
- ebx = eax;
- eax = ROL((uint16)eax, cl);
- edx = ((1 << cl) - 1) & 0x0000FFFF; //make sure we only get 16bits
- bit_buffl &= (uint16)edx;
- edx &= eax;
-
- eax = READ_LE_UINT16((esiptr+2));
- ebx = (ebx << cl)&0x0000FFFF;
- eax = (eax << cl)&0x0000FFFF;
- eax |= edx;
- bit_buffl |= (uint16)ebx;
- bit_buffh = (uint16)eax;
-
- unpack7:
-
- counts--;
- if (counts)
- goto unpack5;
-
- blocks--;
- if (blocks)
- goto unpack4;
+ }
+ } while (--blocks);
if (CHECKSUMS) {
if (crc_block((uint8 *)output, unpack_len) != crc_u)
return UNPACKED_CRC;
}
- // all is done..return the amount of unpacked bytes
- return unpack_len;
+ // all is done..return the amount of unpacked bytes
+ return unpack_len;
}
+