CnC_Remastered_Collection

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

LCWUNCMP.CPP (7245B)


      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/LCWUNCMP.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 extern "C" {
     36 
     37 /***************************************************************************
     38  * LCW_UNCOMPRESS -- Decompress an LCW encoded data block.                 *
     39  *                                                                         *
     40  * Uncompress data to the following codes in the format b = byte, w = word *
     41  * n = byte code pulled from compressed data.                              *
     42  *                                                                         *
     43  * Command code, n        |Description                                     *
     44  * ------------------------------------------------------------------------*
     45  * n=0xxxyyyy,yyyyyyyy    |short copy back y bytes and run x+3   from dest *
     46  * n=10xxxxxx,n1,n2,...,nx+1|med length copy the next x+1 bytes from source*
     47  * n=11xxxxxx,w1          |med copy from dest x+3 bytes from offset w1     *
     48  * n=11111111,w1,w2       |long copy from dest w1 bytes from offset w2     *
     49  * n=11111110,w1,b1       |long run of byte b1 for w1 bytes                *
     50  * n=10000000             |end of data reached                             *
     51  *                                                                         *
     52  *                                                                         *
     53  * INPUT:                                                                  *
     54  *      void * source ptr                                                  *
     55  *      void * destination ptr                                             *
     56  *      unsigned long length of uncompressed data                          *
     57  *                                                                         *
     58  *                                                                         *
     59  * OUTPUT:                                                                 *
     60  *     unsigned long # of destination bytes written                        *
     61  *                                                                         *
     62  * WARNINGS:                                                               *
     63  *     3rd argument is dummy. It exists to provide cross-platform          *
     64  *      compatibility. Note therefore that this implementation does not    *
     65  *      check for corrupt source data by testing the uncompressed length.  *
     66  *                                                                         *
     67  * HISTORY:                                                                *
     68  *    03/20/1995 IML : Created.                                            *
     69  *=========================================================================*/
     70 unsigned long __cdecl LCW_Uncompress (void * source, void * dest, unsigned long )
     71 //unsigned long LCW_Uncompress (void * source, void * dest, unsigned long length)
     72 {
     73 	unsigned char * source_ptr, * dest_ptr, * copy_ptr, op_code, data;
     74 	unsigned	  count, * word_dest_ptr, word_data;
     75 
     76 	/* Copy the source and destination ptrs. */
     77 	source_ptr = (unsigned char*) source;
     78 	dest_ptr   = (unsigned char*) dest;
     79 
     80 	while (1 /*TRUE*/) {
     81 
     82 		/* Read in the operation code. */
     83 		op_code = *source_ptr++;
     84 
     85 		if (!(op_code & 0x80)) {
     86 
     87 			/* Do a short copy from destination. */
     88 			count	 = (op_code >> 4) + 3;
     89 			copy_ptr = dest_ptr - ((unsigned) *source_ptr++ + (((unsigned) op_code & 0x0f) << 8));
     90 
     91 			while (count--) *dest_ptr++ = *copy_ptr++;
     92 
     93 		} else {
     94 
     95 			if (!(op_code & 0x40)) {
     96 
     97 				if (op_code == 0x80) {
     98 
     99 					/* Return # of destination bytes written. */
    100 					return ((unsigned long) (dest_ptr - (unsigned char*) dest));
    101 
    102 				} else {
    103 
    104 					/* Do a medium copy from source. */
    105 					count = op_code & 0x3f;
    106 
    107 					while (count--) *dest_ptr++ = *source_ptr++;
    108 				}
    109 
    110 			} else {
    111 
    112 				if (op_code == 0xfe) {
    113 
    114 					/* Do a long run. */
    115 					count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    116 					word_data = data = *(source_ptr + 2);
    117 					word_data  = (word_data << 24) + (word_data << 16) + (word_data << 8) + word_data;
    118 					source_ptr += 3;
    119 
    120 					copy_ptr = dest_ptr + 4 - ((unsigned) dest_ptr & 0x3);
    121 					count -= (copy_ptr - dest_ptr);
    122 					while (dest_ptr < copy_ptr) *dest_ptr++ = data;
    123 
    124 					word_dest_ptr = (unsigned*) dest_ptr;
    125 
    126 					dest_ptr += (count & 0xfffffffc);
    127 
    128 					while (word_dest_ptr < (unsigned*) dest_ptr) {
    129 						*word_dest_ptr		= word_data;
    130 						*(word_dest_ptr + 1) = word_data;
    131 						word_dest_ptr += 2;
    132 					}
    133 
    134 					copy_ptr = dest_ptr + (count & 0x3);
    135 					while (dest_ptr < copy_ptr) *dest_ptr++ = data;
    136 
    137 				} else {
    138 
    139 					if (op_code == 0xff) {
    140 
    141 						/* Do a long copy from destination. */
    142 						count	 = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    143 						copy_ptr = (unsigned char*) dest + *(source_ptr + 2) + ((unsigned) *(source_ptr + 3) << 8);
    144 						source_ptr += 4;
    145 
    146 						while (count--) *dest_ptr++ = *copy_ptr++;
    147 
    148 					} else {
    149 
    150 						/* Do a medium copy from destination. */
    151 						count = (op_code & 0x3f) + 3;
    152 						copy_ptr = (unsigned char*) dest + *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
    153 						source_ptr += 2;
    154 
    155 						while (count--) *dest_ptr++ = *copy_ptr++;
    156 					}
    157 				}
    158 			}
    159 		}
    160 	}
    161 }
    162 
    163 }