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