CnC_Remastered_Collection

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

BLWSTRAW.CPP (7972B)


      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/BLWSTRAW.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 : BLWSTRAW.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 07/02/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : July 3, 1996 [JLB]                                           *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   BlowStraw::Get -- Fetch a block of data from the straw.                                   *
     34  *   BlowStraw::Key -- Submit a key to the Blowfish straw.                                     *
     35  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     36 
     37 
     38 #include	"blwstraw.h"
     39 #include	<string.h>
     40 #include	<assert.h>
     41 
     42 
     43 /***********************************************************************************************
     44  * BlowStraw::Get -- Fetch a block of data from the straw.                                     *
     45  *                                                                                             *
     46  *    This routine will take a block of data from the straw and process it according to the    *
     47  *    encrypt/decrypt flag and the key supplied. Prior to a key be supplied, the data passes   *
     48  *    through this straw unchanged.                                                            *
     49  *                                                                                             *
     50  * INPUT:   source   -- Pointer to the buffer to hold the data being requested.                *
     51  *                                                                                             *
     52  *          length   -- The length of the data being requested.                                *
     53  *                                                                                             *
     54  * OUTPUT:  Returns with the actual number of bytes stored into the buffer. If the number      *
     55  *          returned is less than the number requested, then this indicates that the data      *
     56  *          source has been exhausted.                                                         *
     57  *                                                                                             *
     58  * WARNINGS:   none                                                                            *
     59  *                                                                                             *
     60  * HISTORY:                                                                                    *
     61  *   07/03/1996 JLB : Created.                                                                 *
     62  *=============================================================================================*/
     63 int BlowStraw::Get(void * source, int slen)
     64 {
     65 	/*
     66 	**	Verify the parameter for legality.
     67 	*/
     68 	if (source == NULL || slen <= 0) {
     69 		return(0);
     70 	}
     71 
     72 	/*
     73 	**	If there is no blowfish engine present, then merely pass the data through
     74 	**	unchanged.
     75 	*/
     76 	if (BF == NULL) {
     77 		return(Straw::Get(source, slen));
     78 	}
     79 
     80 	int total = 0;
     81 
     82 	while (slen > 0) {
     83 
     84 		/*
     85 		**	If there are any left over bytes in the buffer, pass them
     86 		**	through first.
     87 		*/
     88 		if (Counter > 0) {
     89 			int sublen = (slen < Counter) ? slen : Counter;
     90 			memmove(source, &Buffer[sizeof(Buffer)-Counter], sublen);
     91 			Counter -= sublen;
     92 			source = ((char *)source) + sublen;
     93 			slen -= sublen;
     94 			total += sublen;
     95 		}
     96 		if (slen == 0) break;
     97 
     98 		/*
     99 		**	Fetch and encrypt/decrypt the next block.
    100 		*/
    101 		int incount = Straw::Get(Buffer, sizeof(Buffer));
    102 		if (incount == 0) break;
    103 
    104 		/*
    105 		**	Only full blocks are processed. Partial blocks are
    106 		**	merely passed through unchanged.
    107 		*/
    108 		if (incount == sizeof(Buffer)) {
    109 			if (Control == DECRYPT) {
    110 				BF->Decrypt(Buffer, incount, Buffer);
    111 			} else {
    112 				BF->Encrypt(Buffer, incount, Buffer);
    113 			}
    114 		} else {
    115 			memmove(&Buffer[sizeof(Buffer)-incount], Buffer, incount);
    116 		}
    117 		Counter = incount;
    118 	}
    119 
    120 	/*
    121 	**	Return with the total number of bytes placed into the buffer.
    122 	*/
    123 	return(total);
    124 }
    125 
    126 
    127 /***********************************************************************************************
    128  * BlowStraw::Key -- Submit a key to the Blowfish straw.                                       *
    129  *                                                                                             *
    130  *    This will take the key specified and use it to process the data that flows through this  *
    131  *    straw segment. Prior to a key being submitted, the data will flow through unchanged.     *
    132  *                                                                                             *
    133  * INPUT:   key   -- Pointer to the key to submit.                                             *
    134  *                                                                                             *
    135  *          length-- The length of the key. The length must not exceed 56 bytes.               *
    136  *                                                                                             *
    137  * OUTPUT:  none                                                                               *
    138  *                                                                                             *
    139  * WARNINGS:   none                                                                            *
    140  *                                                                                             *
    141  * HISTORY:                                                                                    *
    142  *   07/03/1996 JLB : Created.                                                                 *
    143  *=============================================================================================*/
    144 void BlowStraw::Key(void const * key, int length)
    145 {
    146 	/*
    147 	**	Create the blowfish engine if one isn't already present.
    148 	*/
    149 	if (BF == NULL) {
    150 		BF = new BlowfishEngine;
    151 	}
    152 
    153 	assert(BF != NULL);
    154 
    155 	if (BF != NULL) {
    156 		BF->Submit_Key(key, length);
    157 	}
    158 }