CnC_Remastered_Collection

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

BLOWPIPE.CPP (10363B)


      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/BLOWPIPE.CPP 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 : BLOWPIPE.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 06/30/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : July 3, 1996 [JLB]                                           *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   BlowPipe::Flush -- Flushes any pending data out the pipe.                                 *
     34  *   BlowPipe::Key -- Submit a key to the blowfish pipe handler.                               *
     35  *   BlowPipe::Put -- Submit a block of data for encrypt/decrypt.                              *
     36  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     37 
     38 
     39 #include	"blowpipe.h"
     40 #include	<string.h>
     41 #include	<assert.h>
     42 
     43 
     44 /***********************************************************************************************
     45  * BlowPipe::Flush -- Flushes any pending data out the pipe.                                   *
     46  *                                                                                             *
     47  *    If there is any pending data in the holding buffer, then this routine will force it to   *
     48  *    be flushed out the end of the pipe.                                                      *
     49  *                                                                                             *
     50  * INPUT:   none                                                                               *
     51  *                                                                                             *
     52  * OUTPUT:  Returns with the actual number of bytes output at the end final distant pipe       *
     53  *          segment in the chain.                                                              *
     54  *                                                                                             *
     55  * WARNINGS:   none                                                                            *
     56  *                                                                                             *
     57  * HISTORY:                                                                                    *
     58  *   07/03/1996 JLB : Created.                                                                 *
     59  *=============================================================================================*/
     60 int BlowPipe::Flush(void)
     61 {
     62 	int total = 0;
     63 	if (Counter > 0 && BF != NULL) {
     64 		total += Pipe::Put(Buffer, Counter);
     65 	}
     66 	Counter = 0;
     67 	total += Pipe::Flush();
     68 	return(total);
     69 }
     70 
     71 
     72 /***********************************************************************************************
     73  * BlowPipe::Put -- Submit a block of data for encrypt/decrypt.                                *
     74  *                                                                                             *
     75  *    This will take the data block specified and process it before passing it on to the next  *
     76  *    link in the pipe chain. A key must be submitted before this routine will actually perform*
     77  *    any processing. Prior to key submission, the data is passed through unchanged.           *
     78  *                                                                                             *
     79  * INPUT:   source   -- Pointer to the buffer that contains the data to pass through.          *
     80  *                                                                                             *
     81  *          length   -- The length of the data in the buffer.                                  *
     82  *                                                                                             *
     83  * OUTPUT:  Returns with then actual number of bytes output at the final distant end link in   *
     84  *          the pipe chain.                                                                    *
     85  *                                                                                             *
     86  * WARNINGS:   none                                                                            *
     87  *                                                                                             *
     88  * HISTORY:                                                                                    *
     89  *   07/03/1996 JLB : Created.                                                                 *
     90  *=============================================================================================*/
     91 int BlowPipe::Put(void const * source, int slen)
     92 {
     93 	if (source == NULL || slen < 1) {
     94 		return(Pipe::Put(source, slen));
     95 	}
     96 
     97 	/*
     98 	**	If there is no blowfish engine present, then merely pass the data through
     99 	**	unchanged in any way.
    100 	*/
    101 	if (BF == NULL) {
    102 		return(Pipe::Put(source, slen));
    103 	}
    104 
    105 	int total = 0;
    106 
    107 	/*
    108 	**	If there is a partial block accumulated, then tag on the new data to
    109 	**	this block and process it if the block is full. Proceed with the bulk
    110 	**	processing if there are any left over bytes from this step. This step
    111 	**	can be skipped if there are no pending bytes in the buffer.
    112 	*/
    113 	if (Counter) {
    114 		int sublen = ((int)sizeof(Buffer)-Counter < slen) ? (sizeof(Buffer)-Counter) : slen;
    115 		memmove(&Buffer[Counter], source, sublen);
    116 		Counter += sublen;
    117 		source = ((char *)source) + sublen;
    118 		slen -= sublen;
    119 
    120 		if (Counter == sizeof(Buffer)) {
    121 			if (Control == DECRYPT) {
    122 				BF->Decrypt(Buffer, sizeof(Buffer), Buffer);
    123 			} else {
    124 				BF->Encrypt(Buffer, sizeof(Buffer), Buffer);
    125 			}
    126 			total += Pipe::Put(Buffer, sizeof(Buffer));
    127 			Counter = 0;
    128 		}
    129 	}
    130 
    131 	/*
    132 	**	Process the input data in blocks until there is not enough
    133 	**	source data to fill a full block of data.
    134 	*/
    135 	while (slen >= sizeof(Buffer)) {
    136 		if (Control == DECRYPT) {
    137 			BF->Decrypt(source, sizeof(Buffer), Buffer);
    138 		} else {
    139 			BF->Encrypt(source, sizeof(Buffer), Buffer);
    140 		}
    141 		total += Pipe::Put(Buffer, sizeof(Buffer));
    142 		source = ((char *)source) + sizeof(Buffer);
    143 		slen -= sizeof(Buffer);
    144 	}
    145 
    146 	/*
    147 	**	If there are any left over bytes, then they must be less than the size of
    148 	**	the staging buffer. Store the bytes in the staging buffer for later
    149 	**	processing.
    150 	*/
    151 	if (slen > 0) {
    152 		memmove(Buffer, source, slen);
    153 		Counter = slen;
    154 	}
    155 
    156 	/*
    157 	**	Return with the total number of bytes flushed out to the final end of the
    158 	**	pipe chain.
    159 	*/
    160 	return(total);
    161 }
    162 
    163 
    164 /***********************************************************************************************
    165  * BlowPipe::Key -- Submit a key to the blowfish pipe handler.                                 *
    166  *                                                                                             *
    167  *    This routine will take the key provided and use it to process the data that passes       *
    168  *    through this pipe. Prior to a key being submitted, the data passes through the pipe      *
    169  *    unchanged.                                                                               *
    170  *                                                                                             *
    171  * INPUT:   key   -- Pointer to the key data to use.                                           *
    172  *                                                                                             *
    173  *          length-- The length of the key. The key length must not be greater than 56 bytes.  *
    174  *                                                                                             *
    175  * OUTPUT:  none                                                                               *
    176  *                                                                                             *
    177  * WARNINGS:   none                                                                            *
    178  *                                                                                             *
    179  * HISTORY:                                                                                    *
    180  *   07/03/1996 JLB : Created.                                                                 *
    181  *=============================================================================================*/
    182 void BlowPipe::Key(void const * key, int length)
    183 {
    184 	/*
    185 	**	Create the blowfish engine if one isn't already present.
    186 	*/
    187 	if (BF == NULL) {
    188 		BF = new BlowfishEngine;
    189 	}
    190 
    191 	assert(BF != NULL);
    192 
    193 	if (BF != NULL) {
    194 		BF->Submit_Key(key, length);
    195 	}
    196 }
    197 
    198 
    199