FIELD.CPP (6870B)
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 /*************************************************************************** 17 * * 18 * Project Name : Westwood Auto Registration App * 19 * * 20 * File Name : FIELD.CPP * 21 * * 22 * Programmer : Philip W. Gorrow * 23 * * 24 * Start Date : 04/22/96 * 25 * * 26 * Last Update : April 22, 1996 [PWG] * 27 * * 28 * Actual member function for the field class. * 29 *-------------------------------------------------------------------------* 30 * Functions: * 31 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 32 #include <string.h> 33 #include "field.h" 34 35 // ST - 12/18/2018 10:14AM 36 #pragma warning(disable:4996) 37 38 FieldClass::FieldClass(char *id, char data) 39 { 40 strncpy(ID, id, sizeof(ID)); 41 DataType = TYPE_CHAR; 42 Size = sizeof(data); 43 Data = new char[Size]; 44 memcpy(Data, &data, Size); 45 Next = NULL; 46 } 47 48 FieldClass::FieldClass(char *id, unsigned char data) 49 { 50 strncpy(ID, id, sizeof(ID)); 51 DataType = TYPE_UNSIGNED_CHAR; 52 Size = sizeof(data); 53 Data = new char[Size]; 54 memcpy(Data, &data, Size); 55 Next = NULL; 56 } 57 58 FieldClass::FieldClass(char *id, short data) 59 { 60 strncpy(ID, id, sizeof(ID)); 61 DataType = TYPE_SHORT; 62 Size = sizeof(data); 63 Data = new char[Size]; 64 memcpy(Data, &data, Size); 65 Next = NULL; 66 } 67 68 FieldClass::FieldClass(char *id, unsigned short data) 69 { 70 strncpy(ID, id, sizeof(ID)); 71 DataType = TYPE_UNSIGNED_SHORT; 72 Size = sizeof(data); 73 Data = new char[Size]; 74 memcpy(Data, &data, Size); 75 Next = NULL; 76 } 77 78 FieldClass::FieldClass(char *id, long data) 79 { 80 strncpy(ID, id, sizeof(ID)); 81 DataType = TYPE_LONG; 82 Size = sizeof(data); 83 Data = new char[Size]; 84 memcpy(Data, &data, Size); 85 Next = NULL; 86 } 87 88 FieldClass::FieldClass(char *id, unsigned long data) 89 { 90 strncpy(ID, id, sizeof(ID)); 91 DataType = TYPE_UNSIGNED_LONG; 92 Size = sizeof(data); 93 Data = new char[Size]; 94 memcpy(Data, &data, Size); 95 Next = NULL; 96 } 97 98 FieldClass::FieldClass(char *id, char *data) 99 { 100 strncpy(ID, id, sizeof(ID)); 101 DataType = TYPE_STRING; 102 Size = (unsigned short)(strlen(data)+1); 103 Data = new char[Size]; 104 memcpy(Data, data, Size); 105 Next = NULL; 106 } 107 108 FieldClass::FieldClass(char *id, void *data, int length) 109 { 110 strncpy(ID, id, sizeof(ID)); 111 DataType = TYPE_CHUNK; 112 Size = (unsigned short)length; 113 Data = new char[Size]; 114 memcpy(Data, data, Size); 115 Next = NULL; 116 } 117 118 119 /************************************************************************** 120 * PACKETCLASS::HOST_TO_NET_FIELD -- Converts host field to net format * 121 * * 122 * INPUT: FIELD * to the data field we need to convert * 123 * * 124 * OUTPUT: none * 125 * * 126 * HISTORY: * 127 * 04/22/1996 PWG : Created. * 128 *========================================================================*/ 129 void FieldClass::Host_To_Net(void) 130 { 131 // 132 // Before we convert the data type, we should convert the actual data 133 // sent. 134 // 135 switch (DataType) { 136 case TYPE_CHAR: 137 case TYPE_UNSIGNED_CHAR: 138 case TYPE_STRING: 139 case TYPE_CHUNK: 140 break; 141 142 case TYPE_SHORT: 143 case TYPE_UNSIGNED_SHORT: 144 *((unsigned short *)Data) = htons(*((unsigned short *)Data)); 145 break; 146 147 case TYPE_LONG: 148 case TYPE_UNSIGNED_LONG: 149 *((unsigned long *)Data) = htonl(*((unsigned long *)Data)); 150 break; 151 152 // 153 // Might be good to insert some type of error message here for unknown 154 // datatypes -- but will leave that for later. 155 // 156 default: 157 break; 158 } 159 // 160 // Finally convert over the data type and the size of the packet. 161 // 162 DataType = htons(DataType); 163 Size = htons(Size); 164 } 165 /************************************************************************** 166 * PACKETCLASS::NET_TO_HOST_FIELD -- Converts net field to host format * 167 * * 168 * INPUT: FIELD * to the data field we need to convert * 169 * * 170 * OUTPUT: none * 171 * * 172 * HISTORY: * 173 * 04/22/1996 PWG : Created. * 174 *========================================================================*/ 175 void FieldClass::Net_To_Host(void) 176 { 177 // 178 // Convert the variables to host order. This needs to be converted so 179 // the switch statement does compares on the data that follows. 180 // 181 Size = ntohs(Size); 182 183 DataType = ntohs(DataType); 184 185 // 186 // Before we convert the data type, we should convert the actual data 187 // sent. 188 // 189 switch (DataType) { 190 case TYPE_CHAR: 191 case TYPE_UNSIGNED_CHAR: 192 case TYPE_STRING: 193 case TYPE_CHUNK: 194 break; 195 196 case TYPE_SHORT: 197 case TYPE_UNSIGNED_SHORT: 198 *((unsigned short *)Data) = ntohs(*((unsigned short *)Data)); 199 break; 200 201 case TYPE_LONG: 202 case TYPE_UNSIGNED_LONG: 203 *((unsigned long *)Data) = ntohl(*((unsigned long *)Data)); 204 break; 205 206 // 207 // Might be good to insert some type of error message here for unknown 208 // datatypes -- but will leave that for later. 209 // 210 default: 211 break; 212 } 213 }