CnC_Remastered_Collection

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

CRC.H (4811B)


      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/CRC.H 1     3/03/97 10:24a 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 : CRC.H                                                        *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 03/02/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : March 2, 1996 [JLB]                                          *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     34 
     35 
     36 #ifndef CRC_H
     37 #define CRC_H
     38 
     39 #include	<stdlib.h>
     40 
     41 /*
     42 **	The "bool" integral type was defined by the C++ comittee in
     43 **	November of '94. Until the compiler supports this, use the following
     44 **	definition.
     45 */
     46 #ifndef __BORLANDC__
     47 #ifndef TRUE_FALSE_DEFINED
     48 #define TRUE_FALSE_DEFINED
     49 enum {false=0,true=1};
     50 typedef int bool;
     51 #endif
     52 #endif
     53 
     54 /*
     55 **	This is a CRC engine class. It will process submitted data and generate a CRC from it.
     56 **	Well, actually, the value returned is not a true CRC. However, it shares the same strength
     57 **	characteristic and is faster to generate than the traditional CRC. This object is treated like
     58 **	a method class. If it is called as a function (using the function operator), it will return
     59 **	the CRC value. There are other function operators to submit data for processing.
     60 */
     61 class CRCEngine {
     62 	public:
     63 
     64 		// Constructor for CRC engine (it can have an override initial CRC value).
     65 		CRCEngine(long initial=0) : CRC(initial), Index(0) {
     66 			StagingBuffer.Composite = 0;
     67 		};
     68 
     69 		// Fetches CRC value.
     70 		long operator() (void) const {return(Value());};
     71 
     72 		// Submits one byte sized datum to the CRC accumulator.
     73 		void operator() (char datum);
     74 
     75 		// Submits an arbitrary buffer to the CRC accumulator.
     76 		long operator() (void const * buffer, int length);
     77 
     78 		// Implicit conversion operator so this object appears like a 'long integer'.
     79 		operator long(void) const {return(Value());};
     80 
     81 	protected:
     82 
     83 		bool Buffer_Needs_Data(void) const {
     84 			return(Index != 0);
     85 		};
     86 
     87 		long Value(void) const {
     88 			if (Buffer_Needs_Data()) {
     89 				return(_lrotl(CRC, 1) + StagingBuffer.Composite);
     90 			}
     91 			return(CRC);
     92 		};
     93 
     94 		/*
     95 		**	Current accumulator of the CRC value. This value doesn't take into
     96 		**	consideration any pending data in the staging buffer.
     97 		*/
     98 		long CRC;
     99 
    100 		/*
    101 		**	This is the sub index into the staging buffer used to keep track of
    102 		**	partial data blocks as they are submitted to the CRC engine.
    103 		*/
    104 		int Index;
    105 
    106 		/*
    107 		**	This is the buffer that holds the incoming partial data. When the buffer
    108 		**	is filled, the value is transformed into the CRC and the buffer is flushed
    109 		**	in preparation for additional data.
    110 		*/
    111 		union {
    112 			long Composite;
    113 			char Buffer[sizeof(long)];
    114 		} StagingBuffer;
    115 };
    116 
    117 #endif
    118