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