CnC_Remastered_Collection/REDALERT/LCWUNCMP.CPP

164 lines
7.1 KiB
C++

//
// Copyright 2020 Electronic Arts Inc.
//
// TiberianDawn.DLL and RedAlert.dll and corresponding source code 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.
// TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
// in the hope that it will be useful, but with permitted additional restrictions
// under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
// distributed with this program. You should have received a copy of the
// GNU General Public License along with permitted additional restrictions
// with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
/* $Header: /CounterStrike/LCWUNCMP.CPP 1 3/03/97 10:25a Joe_bostic $ */
/***************************************************************************
** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
***************************************************************************
* *
* Project Name : WESTWOOD LIBRARY (PSX) *
* *
* File Name : LCWUNCMP.CPP *
* *
* Programmer : Ian M. Leslie *
* *
* Start Date : May 17, 1995 *
* *
* Last Update : May 17, 1995 [IML] *
* *
*-------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
extern "C" {
/***************************************************************************
* LCW_UNCOMPRESS -- Decompress an LCW encoded data block. *
* *
* Uncompress data to the following codes in the format b = byte, w = word *
* n = byte code pulled from compressed data. *
* *
* Command code, n |Description *
* ------------------------------------------------------------------------*
* n=0xxxyyyy,yyyyyyyy |short copy back y bytes and run x+3 from dest *
* n=10xxxxxx,n1,n2,...,nx+1|med length copy the next x+1 bytes from source*
* n=11xxxxxx,w1 |med copy from dest x+3 bytes from offset w1 *
* n=11111111,w1,w2 |long copy from dest w1 bytes from offset w2 *
* n=11111110,w1,b1 |long run of byte b1 for w1 bytes *
* n=10000000 |end of data reached *
* *
* *
* INPUT: *
* void * source ptr *
* void * destination ptr *
* unsigned long length of uncompressed data *
* *
* *
* OUTPUT: *
* unsigned long # of destination bytes written *
* *
* WARNINGS: *
* 3rd argument is dummy. It exists to provide cross-platform *
* compatibility. Note therefore that this implementation does not *
* check for corrupt source data by testing the uncompressed length. *
* *
* HISTORY: *
* 03/20/1995 IML : Created. *
*=========================================================================*/
unsigned long __cdecl LCW_Uncompress (void * source, void * dest, unsigned long )
//unsigned long LCW_Uncompress (void * source, void * dest, unsigned long length)
{
unsigned char * source_ptr, * dest_ptr, * copy_ptr, op_code, data;
unsigned count, * word_dest_ptr, word_data;
/* Copy the source and destination ptrs. */
source_ptr = (unsigned char*) source;
dest_ptr = (unsigned char*) dest;
while (1 /*TRUE*/) {
/* Read in the operation code. */
op_code = *source_ptr++;
if (!(op_code & 0x80)) {
/* Do a short copy from destination. */
count = (op_code >> 4) + 3;
copy_ptr = dest_ptr - ((unsigned) *source_ptr++ + (((unsigned) op_code & 0x0f) << 8));
while (count--) *dest_ptr++ = *copy_ptr++;
} else {
if (!(op_code & 0x40)) {
if (op_code == 0x80) {
/* Return # of destination bytes written. */
return ((unsigned long) (dest_ptr - (unsigned char*) dest));
} else {
/* Do a medium copy from source. */
count = op_code & 0x3f;
while (count--) *dest_ptr++ = *source_ptr++;
}
} else {
if (op_code == 0xfe) {
/* Do a long run. */
count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
word_data = data = *(source_ptr + 2);
word_data = (word_data << 24) + (word_data << 16) + (word_data << 8) + word_data;
source_ptr += 3;
copy_ptr = dest_ptr + 4 - ((unsigned) dest_ptr & 0x3);
count -= (copy_ptr - dest_ptr);
while (dest_ptr < copy_ptr) *dest_ptr++ = data;
word_dest_ptr = (unsigned*) dest_ptr;
dest_ptr += (count & 0xfffffffc);
while (word_dest_ptr < (unsigned*) dest_ptr) {
*word_dest_ptr = word_data;
*(word_dest_ptr + 1) = word_data;
word_dest_ptr += 2;
}
copy_ptr = dest_ptr + (count & 0x3);
while (dest_ptr < copy_ptr) *dest_ptr++ = data;
} else {
if (op_code == 0xff) {
/* Do a long copy from destination. */
count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
copy_ptr = (unsigned char*) dest + *(source_ptr + 2) + ((unsigned) *(source_ptr + 3) << 8);
source_ptr += 4;
while (count--) *dest_ptr++ = *copy_ptr++;
} else {
/* Do a medium copy from destination. */
count = (op_code & 0x3f) + 3;
copy_ptr = (unsigned char*) dest + *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
source_ptr += 2;
while (count--) *dest_ptr++ = *copy_ptr++;
}
}
}
}
}
}
}