CnC_Remastered_Collection

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

LZWOTRAW.CPP (10296B)


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