CnC_Remastered_Collection

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

B64PIPE.CPP (7895B)


      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/B64PIPE.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 : B64PIPE.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  *   Base64Pipe::Put -- Processes a block of data through the pipe.                            *
     34  *   Base64Pipe::Flush -- Flushes the final pending data through the pipe.                     *
     35  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     36 
     37 
     38 #include "b64pipe.h"
     39 #include	"base64.h"
     40 #include	<string.h>
     41 
     42 
     43 /***********************************************************************************************
     44  * Base64Pipe::Put -- Processes a block of data through the pipe.                              *
     45  *                                                                                             *
     46  *    This will take the data submitted and either Base64 encode or decode it (as specified    *
     47  *    in the pipe's constructor). The nature of Base64 encoding means that the data will       *
     48  *    grow 30% in size when encoding and decrease by a like amount when decoding.              *
     49  *                                                                                             *
     50  * INPUT:   source   -- Pointer to the data to be translated.                                  *
     51  *                                                                                             *
     52  *          length   -- The number of bytes to translate.                                      *
     53  *                                                                                             *
     54  * OUTPUT:  Returns with the actual number of bytes output at the far distant final end of     *
     55  *          the pipe chain.                                                                    *
     56  *                                                                                             *
     57  * WARNINGS:   none                                                                            *
     58  *                                                                                             *
     59  * HISTORY:                                                                                    *
     60  *   07/03/1996 JLB : Created.                                                                 *
     61  *=============================================================================================*/
     62 int Base64Pipe::Put(void const * source, int slen)
     63 {
     64 	if (source == NULL || slen < 1) {
     65 		return(Pipe::Put(source, slen));
     66 	}
     67 
     68 	int total = 0;
     69 
     70 	char * from;
     71 	int fromsize;
     72 	char * to;
     73 	int tosize;
     74 
     75 	if (Control == ENCODE) {
     76 		from = PBuffer;
     77 		fromsize = sizeof(PBuffer);
     78 		to = CBuffer;
     79 		tosize = sizeof(CBuffer);
     80 	} else {
     81 		from = CBuffer;
     82 		fromsize = sizeof(CBuffer);
     83 		to = PBuffer;
     84 		tosize = sizeof(PBuffer);
     85 	}
     86 
     87 	if (Counter > 0) {
     88 		int len = (slen < (fromsize-Counter)) ? slen : (fromsize-Counter);
     89 		memmove(&from[Counter], source, len);
     90 		Counter += len;
     91 		slen -= len;
     92 		source = ((char *)source) + len;
     93 
     94 		if (Counter == fromsize) {
     95 			int outcount;
     96 			if (Control == ENCODE) {
     97 				outcount = Base64_Encode(from, fromsize, to, tosize);
     98 			} else {
     99 				outcount = Base64_Decode(from, fromsize, to, tosize);
    100 			}
    101 			total += Pipe::Put(to, outcount);
    102 			Counter = 0;
    103 		}
    104 	}
    105 
    106 	while (slen >= fromsize) {
    107 		int outcount;
    108 		if (Control == ENCODE) {
    109 			outcount = Base64_Encode(source, fromsize, to, tosize);
    110 		} else {
    111 			outcount = Base64_Decode(source, fromsize, to, tosize);
    112 		}
    113 		source = ((char *)source) + fromsize;
    114 		total += Pipe::Put(to, outcount);
    115 		slen -= fromsize;
    116 	}
    117 
    118 	if (slen > 0) {
    119 		memmove(from, source, slen);
    120 		Counter = slen;
    121 	}
    122 
    123 	return(total);
    124 }
    125 
    126 
    127 /***********************************************************************************************
    128  * Base64Pipe::Flush -- Flushes the final pending data through the pipe.                       *
    129  *                                                                                             *
    130  *    If there is any non-processed data accumulated in the holding buffer (quite likely when  *
    131  *    encoding), then it will be processed and flushed out the end of the pipe.                *
    132  *                                                                                             *
    133  * INPUT:   none                                                                               *
    134  *                                                                                             *
    135  * OUTPUT:  Returns with the number of bytes output at the far distant final end of the pipe   *
    136  *          chain.                                                                             *
    137  *                                                                                             *
    138  * WARNINGS:   none                                                                            *
    139  *                                                                                             *
    140  * HISTORY:                                                                                    *
    141  *   07/03/1996 JLB : Created.                                                                 *
    142  *=============================================================================================*/
    143 int Base64Pipe::Flush(void)
    144 {
    145 	int len = 0;
    146 
    147 	if (Counter) {
    148 		if (Control == ENCODE) {
    149 			int chars = Base64_Encode(PBuffer, Counter, CBuffer, sizeof(CBuffer));
    150 			len += Pipe::Put(CBuffer, chars);
    151 		} else {
    152 			int chars = Base64_Decode(CBuffer, Counter, PBuffer, sizeof(PBuffer));
    153 			len += Pipe::Put(PBuffer, chars);
    154 		}
    155 		Counter = 0;
    156 	}
    157 	len += Pipe::Flush();
    158 	return(len);
    159 }
    160 
    161 
    162