Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
blowpipe.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Command & Conquer *
24 * *
25 * $Archive:: /Commando/Library/BLOWPIPE.CPP $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 7/22/97 11:37a $*
30 * *
31 * $Revision:: 1 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * BlowPipe::Flush -- Flushes any pending data out the pipe. *
36 * BlowPipe::Key -- Submit a key to the blowfish pipe handler. *
37 * BlowPipe::Put -- Submit a block of data for encrypt/decrypt. *
38 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39
40#include "always.h"
41#include "blowpipe.h"
42#include <string.h>
43#include <assert.h>
44
45
46/***********************************************************************************************
47 * BlowPipe::Flush -- Flushes any pending data out the pipe. *
48 * *
49 * If there is any pending data in the holding buffer, then this routine will force it to *
50 * be flushed out the end of the pipe. *
51 * *
52 * INPUT: none *
53 * *
54 * OUTPUT: Returns with the actual number of bytes output at the end final distant pipe *
55 * segment in the chain. *
56 * *
57 * WARNINGS: none *
58 * *
59 * HISTORY: *
60 * 07/03/1996 JLB : Created. *
61 *=============================================================================================*/
63{
64 int total = 0;
65 if (Counter > 0 && BF != NULL) {
66 total += Pipe::Put(Buffer, Counter);
67 }
68 Counter = 0;
69 total += Pipe::Flush();
70 return(total);
71}
72
73
74/***********************************************************************************************
75 * BlowPipe::Put -- Submit a block of data for encrypt/decrypt. *
76 * *
77 * This will take the data block specified and process it before passing it on to the next *
78 * link in the pipe chain. A key must be submitted before this routine will actually perform*
79 * any processing. Prior to key submission, the data is passed through unchanged. *
80 * *
81 * INPUT: source -- Pointer to the buffer that contains the data to pass through. *
82 * *
83 * length -- The length of the data in the buffer. *
84 * *
85 * OUTPUT: Returns with then actual number of bytes output at the final distant end link in *
86 * the pipe chain. *
87 * *
88 * WARNINGS: none *
89 * *
90 * HISTORY: *
91 * 07/03/1996 JLB : Created. *
92 *=============================================================================================*/
93int BlowPipe::Put(void const * source, int slen)
94{
95 if (source == NULL || slen < 1) {
96 return(Pipe::Put(source, slen));
97 }
98
99 /*
100 ** If there is no blowfish engine present, then merely pass the data through
101 ** unchanged in any way.
102 */
103 if (BF == NULL) {
104 return(Pipe::Put(source, slen));
105 }
106
107 int total = 0;
108
109 /*
110 ** If there is a partial block accumulated, then tag on the new data to
111 ** this block and process it if the block is full. Proceed with the bulk
112 ** processing if there are any left over bytes from this step. This step
113 ** can be skipped if there are no pending bytes in the buffer.
114 */
115 if (Counter) {
116 int sublen = ((int)sizeof(Buffer)-Counter < slen) ? (sizeof(Buffer)-Counter) : slen;
117 memmove(&Buffer[Counter], source, sublen);
118 Counter += sublen;
119 source = ((char *)source) + sublen;
120 slen -= sublen;
121
122 if (Counter == sizeof(Buffer)) {
123 if (Control == DECRYPT) {
124 BF->Decrypt(Buffer, sizeof(Buffer), Buffer);
125 } else {
126 BF->Encrypt(Buffer, sizeof(Buffer), Buffer);
127 }
128 total += Pipe::Put(Buffer, sizeof(Buffer));
129 Counter = 0;
130 }
131 }
132
133 /*
134 ** Process the input data in blocks until there is not enough
135 ** source data to fill a full block of data.
136 */
137 while (slen >= sizeof(Buffer)) {
138 if (Control == DECRYPT) {
139 BF->Decrypt(source, sizeof(Buffer), Buffer);
140 } else {
141 BF->Encrypt(source, sizeof(Buffer), Buffer);
142 }
143 total += Pipe::Put(Buffer, sizeof(Buffer));
144 source = ((char *)source) + sizeof(Buffer);
145 slen -= sizeof(Buffer);
146 }
147
148 /*
149 ** If there are any left over bytes, then they must be less than the size of
150 ** the staging buffer. Store the bytes in the staging buffer for later
151 ** processing.
152 */
153 if (slen > 0) {
154 memmove(Buffer, source, slen);
155 Counter = slen;
156 }
157
158 /*
159 ** Return with the total number of bytes flushed out to the final end of the
160 ** pipe chain.
161 */
162 return(total);
163}
164
165
166/***********************************************************************************************
167 * BlowPipe::Key -- Submit a key to the blowfish pipe handler. *
168 * *
169 * This routine will take the key provided and use it to process the data that passes *
170 * through this pipe. Prior to a key being submitted, the data passes through the pipe *
171 * unchanged. *
172 * *
173 * INPUT: key -- Pointer to the key data to use. *
174 * *
175 * length-- The length of the key. The key length must not be greater than 56 bytes. *
176 * *
177 * OUTPUT: none *
178 * *
179 * WARNINGS: none *
180 * *
181 * HISTORY: *
182 * 07/03/1996 JLB : Created. *
183 *=============================================================================================*/
184void BlowPipe::Key(void const * key, int length)
185{
186 /*
187 ** Create the blowfish engine if one isn't already present.
188 */
189 if (BF == NULL) {
190 BF = new BlowfishEngine;
191 }
192
193 assert(BF != NULL);
194
195 if (BF != NULL) {
196 BF->Submit_Key(key, length);
197 }
198}
199
200
201
#define NULL
Definition BaseType.h:92
virtual int Put(void const *source, int slen)
Definition blowpipe.cpp:93
@ DECRYPT
Definition blowpipe.h:52
void Key(void const *key, int length)
Definition blowpipe.cpp:184
virtual int Flush(void)
Definition blowpipe.cpp:62
BlowfishEngine * BF
Definition blowpipe.h:71
virtual int Flush(void)
Definition pipe.cpp:158
virtual int Put(void const *source, int slen)
Definition pipe.cpp:131