CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

LCW.CPP (7125B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header: /CounterStrike/LCW.CPP 1     3/03/97 10:25a Joe_bostic $ */
     17 /***************************************************************************
     18  **    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        **
     19  ***************************************************************************
     20  *                                                                         *
     21  *               Project Name : WESTWOOD LIBRARY (PSX)                     *
     22  *                                                                         *
     23  *                 File Name : LCWUNCMP.CPP                                *
     24  *                                                                         *
     25  *                Programmer : Ian M. Leslie                               *
     26  *                                                                         *
     27  *                Start Date : May 17, 1995                                *
     28  *                                                                         *
     29  *               Last Update : May 17, 1995    [IML]                       *
     30  *                                                                         *
     31  *-------------------------------------------------------------------------*
     32  * Functions:                                                              *
     33  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     34 
     35 
     36 /***************************************************************************
     37  * LCW_Uncomp -- Decompress an LCW encoded data block.                     *
     38  *                                                                         *
     39  * Uncompress data to the following codes in the format b = byte, w = word *
     40  * n = byte code pulled from compressed data.                              *
     41  *                                                                         *
     42  *   Command code, n        |Description                                   *
     43  * ------------------------------------------------------------------------*
     44  * n=0xxxyyyy,yyyyyyyy      |short copy back y bytes and run x+3 from dest *
     45  * n=10xxxxxx,n1,n2,...,nx+1|med length copy the next x+1 bytes from source*
     46  * n=11xxxxxx,w1            |med copy from dest x+3 bytes from offset w1   *
     47  * n=11111111,w1,w2         |long copy from dest w1 bytes from offset w2   *
     48  * n=11111110,w1,b1         |long run of byte b1 for w1 bytes              *
     49  * n=10000000               |end of data reached                           *
     50  *                                                                         *
     51  *                                                                         *
     52  * INPUT:                                                                  *
     53  *      void * source ptr                                                  *
     54  *      void * destination ptr                                             *
     55  *      unsigned long length of uncompressed data                          *
     56  *                                                                         *
     57  *                                                                         *
     58  * OUTPUT:                                                                 *
     59  *     unsigned long # of destination bytes written                        *
     60  *                                                                         *
     61  * WARNINGS:                                                               *
     62  *     3rd argument is dummy. It exists to provide cross-platform          *
     63  *      compatibility. Note therefore that this implementation does not    *
     64  *      check for corrupt source data by testing the uncompressed length.  *
     65  *                                                                         *
     66  * HISTORY:                                                                *
     67  *    03/20/1995 IML : Created.                                            *
     68  *=========================================================================*/
     69 int LCW_Uncomp(void const * source, void * dest, unsigned long )
     70 {
     71 	unsigned char * source_ptr, * dest_ptr, * copy_ptr, op_code, data;
     72 	unsigned	  count, * word_dest_ptr, word_data;
     73 
     74 	/* Copy the source and destination ptrs. */
     75 	source_ptr = (unsigned char*) source;
     76 	dest_ptr   = (unsigned char*) dest;
     77 
     78 	while (1 /*TRUE*/) {
     79 
     80 		/* Read in the operation code. */
     81 		op_code = *source_ptr++;
     82 
     83 		if (!(op_code & 0x80)) {
     84 
     85 			/* Do a short copy from destination. */
     86 			count	 = (op_code >> 4) + 3;
     87 			copy_ptr = dest_ptr - ((unsigned) *source_ptr++ + (((unsigned) op_code & 0x0f) << 8));
     88 
     89 			while (count--) *dest_ptr++ = *copy_ptr++;
     90 
     91 		} else {
     92 
     93 			if (!(op_code & 0x40)) {
     94 
     95 				if (op_code == 0x80) {
     96 
     97 					/* Return # of destination bytes written. */
     98 					return ((unsigned long) (dest_ptr - (unsigned char*) dest));
     99 
    100 				} else {
    101 
    102 					/* Do a medium copy from source. */
    103 					count = op_code & 0x3f;
    104 
    105 					while (count--) *dest_ptr++ = *source_ptr++;
    106 				}
    107 
    108 			} else {
    109 
    110 				if (op_code == 0xfe) {
    111 
    112 					/* Do a long run. */
    113 					count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    114 					word_data = data = *(source_ptr + 2);
    115 					word_data  = (word_data << 24) + (word_data << 16) + (word_data << 8) + word_data;
    116 					source_ptr += 3;
    117 
    118 					copy_ptr = dest_ptr + 4 - ((unsigned) dest_ptr & 0x3);
    119 					count -= (copy_ptr - dest_ptr);
    120 					while (dest_ptr < copy_ptr) *dest_ptr++ = data;
    121 
    122 					word_dest_ptr = (unsigned*) dest_ptr;
    123 
    124 					dest_ptr += (count & 0xfffffffc);
    125 
    126 					while (word_dest_ptr < (unsigned*) dest_ptr) {
    127 						*word_dest_ptr		= word_data;
    128 						*(word_dest_ptr + 1) = word_data;
    129 						word_dest_ptr += 2;
    130 					}
    131 
    132 					copy_ptr = dest_ptr + (count & 0x3);
    133 					while (dest_ptr < copy_ptr) *dest_ptr++ = data;
    134 
    135 				} else {
    136 
    137 					if (op_code == 0xff) {
    138 
    139 						/* Do a long copy from destination. */
    140 						count	 = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    141 						copy_ptr = (unsigned char*) dest + *(source_ptr + 2) + ((unsigned) *(source_ptr + 3) << 8);
    142 						source_ptr += 4;
    143 
    144 						while (count--) *dest_ptr++ = *copy_ptr++;
    145 
    146 					} else {
    147 
    148 						/* Do a medium copy from destination. */
    149 						count = (op_code & 0x3f) + 3;
    150 						copy_ptr = (unsigned char*) dest + *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    151 						source_ptr += 2;
    152 
    153 						while (count--) *dest_ptr++ = *copy_ptr++;
    154 					}
    155 				}
    156 			}
    157 		}
    158 	}
    159 }