CnC_Remastered_Collection

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

LCWSTRAW.CPP (10070B)


      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/LCWSTRAW.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 : Command & Conquer                                            *
     22  *                                                                                             *
     23  *                    File Name : LCWSTRAW.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 07/02/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : July 4, 1996 [JLB]                                           *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   LCWStraw::Get -- Fetch data through the LCW processor.                                    *
     34  *   LCWStraw::LCWStraw -- Constructor for LCW straw object.                                   *
     35  *   LCWStraw::~LCWStraw -- Destructor for the LCW straw.                                      *
     36  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     37 
     38 #include	"lcwstraw.h"
     39 #include	"lcw.h"
     40 #include	<string.h>
     41 #include	<assert.h>
     42 
     43 
     44 /***********************************************************************************************
     45  * LCWStraw::LCWStraw -- Constructor for LCW straw object.                                     *
     46  *                                                                                             *
     47  *    This will initialize the LCW straw object. Whether the object is to compress or          *
     48  *    decompress and the block size to use is specified. The data is compressed in blocks      *
     49  *    that are sized to be quick to compress and yet still yield good compression ratios.      *
     50  *                                                                                             *
     51  * INPUT:   decrypt  -- Should the data be decompressed?                                       *
     52  *                                                                                             *
     53  *          blocksize-- The size of the blocks to process.                                     *
     54  *                                                                                             *
     55  * OUTPUT:  none                                                                               *
     56  *                                                                                             *
     57  * WARNINGS:   It takes two buffers of the blocksize specified if compression is to be         *
     58  *             performed.                                                                      *
     59  *                                                                                             *
     60  * HISTORY:                                                                                    *
     61  *   07/04/1996 JLB : Created.                                                                 *
     62  *=============================================================================================*/
     63 LCWStraw::LCWStraw(CompControl control, int blocksize) :
     64 		Control(control),
     65 		Counter(0),
     66 		Buffer(NULL),
     67 		Buffer2(NULL),
     68 		BlockSize(blocksize)
     69 {
     70 	SafetyMargin = BlockSize/128+1;
     71 	Buffer = new char[BlockSize+SafetyMargin];
     72 	if (control == COMPRESS) {
     73 		Buffer2 = new char[BlockSize+SafetyMargin];
     74 	}
     75 }
     76 
     77 
     78 /***********************************************************************************************
     79  * LCWStraw::~LCWStraw -- Destructor for the LCW straw.                                        *
     80  *                                                                                             *
     81  *    The destructor will free up the allocated buffers that it allocated in the constructor.  *
     82  *                                                                                             *
     83  * INPUT:   none                                                                               *
     84  *                                                                                             *
     85  * OUTPUT:  none                                                                               *
     86  *                                                                                             *
     87  * WARNINGS:   none                                                                            *
     88  *                                                                                             *
     89  * HISTORY:                                                                                    *
     90  *   07/04/1996 JLB : Created.                                                                 *
     91  *=============================================================================================*/
     92 LCWStraw::~LCWStraw(void)
     93 {
     94 	delete [] Buffer;
     95 	Buffer = NULL;
     96 
     97 	delete [] Buffer2;
     98 	Buffer2 = NULL;
     99 }
    100 
    101 
    102 /***********************************************************************************************
    103  * LCWStraw::Get -- Fetch data through the LCW processor.                                      *
    104  *                                                                                             *
    105  *    This routine will fetch the data bytes specified. It does this by first accumulating     *
    106  *    a full block of data and then compressing or decompressing it as indicated. Subsequent   *
    107  *    requests for data will draw from this buffer of processed data until it is exhausted     *
    108  *    and another block must be fetched.                                                       *
    109  *                                                                                             *
    110  * INPUT:   destbuf  -- Pointer to the buffer to hold the data requested.                      *
    111  *                                                                                             *
    112  *          length   -- The number of data bytes requested.                                    *
    113  *                                                                                             *
    114  * OUTPUT:  Returns with the actual number of bytes stored into the buffer. If this number     *
    115  *          is less than that requested, then this indicates that the data source has been     *
    116  *          exhausted.                                                                         *
    117  *                                                                                             *
    118  * WARNINGS:   none                                                                            *
    119  *                                                                                             *
    120  * HISTORY:                                                                                    *
    121  *   07/04/1996 JLB : Created.                                                                 *
    122  *=============================================================================================*/
    123 int LCWStraw::Get(void * destbuf, int slen)
    124 {
    125 	assert(Buffer != NULL);
    126 
    127 	int total = 0;
    128 
    129 	/*
    130 	**	Verify parameters for legality.
    131 	*/
    132 	if (destbuf == NULL || slen < 1) {
    133 		return(0);
    134 	}
    135 
    136 	while (slen > 0) {
    137 
    138 		/*
    139 		**	Copy as much data is requested and available into the desired
    140 		**	destination buffer.
    141 		*/
    142 		if (Counter) {
    143 			int len = (slen < Counter) ? slen : Counter;
    144 			if (Control == DECOMPRESS) {
    145 				memmove(destbuf, &Buffer[BlockHeader.UncompCount-Counter], len);
    146 			} else {
    147 				memmove(destbuf, &Buffer2[(BlockHeader.CompCount+sizeof(BlockHeader))-Counter], len);
    148 			}
    149 			destbuf = ((char *)destbuf) + len;
    150 			slen -= len;
    151 			Counter -= len;
    152 			total += len;
    153 		}
    154 		if (slen == 0) break;
    155 
    156 		if (Control == DECOMPRESS) {
    157 			int incount = Straw::Get(&BlockHeader, sizeof(BlockHeader));
    158 			if (incount != sizeof(BlockHeader)) break;
    159 
    160 			void * ptr = &Buffer[(BlockSize+SafetyMargin) - BlockHeader.CompCount];
    161 			incount = Straw::Get(ptr, BlockHeader.CompCount);
    162 			if (incount != BlockHeader.CompCount) break;
    163 
    164 			LCW_Uncomp(ptr, Buffer);
    165 			Counter = BlockHeader.UncompCount;
    166 		} else {
    167 			BlockHeader.UncompCount = (unsigned short)Straw::Get(Buffer, BlockSize);
    168 			if (BlockHeader.UncompCount == 0) break;
    169 			BlockHeader.CompCount = (unsigned short)LCW_Comp(Buffer, &Buffer2[sizeof(BlockHeader)], BlockHeader.UncompCount);
    170 			memmove(Buffer2, &BlockHeader, sizeof(BlockHeader));
    171 			Counter = BlockHeader.CompCount+sizeof(BlockHeader);
    172 		}
    173 	}
    174 
    175 	return(total);
    176 }