CnC_Remastered_Collection

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

B64STRAW.CPP (5725B)


      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/B64STRAW.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 : B64STRAW.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  *   Base64Straw::Get -- Fetch data and convert it to/from base 64 encoding.                   *
     34  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     35 
     36 
     37 #include	"b64straw.h"
     38 #include	"base64.h"
     39 #include	<string.h>
     40 
     41 
     42 /***********************************************************************************************
     43  * Base64Straw::Get -- Fetch data and convert it to/from base 64 encoding.                     *
     44  *                                                                                             *
     45  *    This routine will fetch the number of bytes requested and perform any conversion as      *
     46  *    necessary upon the data. The nature of Base 64 encoding means that the data will         *
     47  *    increase in size by 30% when encoding and decrease in like manner when decoding.         *
     48  *                                                                                             *
     49  * INPUT:   source   -- The buffer to hold the processed data.                                 *
     50  *                                                                                             *
     51  *          length   -- The number of bytes requested.                                         *
     52  *                                                                                             *
     53  * OUTPUT:  Returns with the number of bytes stored into the buffer. If the number is less     *
     54  *          than requested, then this indicates that the data stream has been exhausted.       *
     55  *                                                                                             *
     56  * WARNINGS:   none                                                                            *
     57  *                                                                                             *
     58  * HISTORY:                                                                                    *
     59  *   07/03/1996 JLB : Created.                                                                 *
     60  *=============================================================================================*/
     61 int Base64Straw::Get(void * source, int slen)
     62 {
     63 	int total = 0;
     64 
     65 	char * from;
     66 	int fromsize;
     67 	char * to;
     68 	int tosize;
     69 
     70 	if (Control == ENCODE) {
     71 		from = PBuffer;
     72 		fromsize = sizeof(PBuffer);
     73 		to = CBuffer;
     74 		tosize = sizeof(CBuffer);
     75 	} else {
     76 		from = CBuffer;
     77 		fromsize = sizeof(CBuffer);
     78 		to = PBuffer;
     79 		tosize = sizeof(PBuffer);
     80 	}
     81 
     82 	/*
     83 	**	Process the byte request in code blocks until there are either
     84 	**	no more source bytes available or the request has been fulfilled.
     85 	*/
     86 	while (slen > 0) {
     87 
     88 		/*
     89 		**	Transfer any processed bytes available to the request buffer.
     90 		*/
     91 		if (Counter > 0) {
     92 			int len = (slen < Counter) ? slen : Counter;
     93 			memmove(source, &to[tosize-Counter], len);
     94 			Counter -= len;
     95 			slen -= len;
     96 			source = ((char *)source) + len;
     97 			total += len;
     98 		}
     99 		if (slen == 0) break;
    100 
    101 		/*
    102 		**	More bytes are needed, so fetch and process another base 64 block.
    103 		*/
    104 		int incount = Straw::Get(from, fromsize);
    105 		if (Control == ENCODE) {
    106 			Counter = Base64_Encode(from, incount, to, tosize);
    107 		} else {
    108 			Counter = Base64_Decode(from, incount, to, tosize);
    109 		}
    110 		if (Counter == 0) break;
    111 	}
    112 
    113 	return(total);
    114 }