field.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ** Command & Conquer Renegade(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. * Project Name : Westwood Auto Registration App *
  21. * *
  22. * File Name : FIELD.CPP *
  23. * *
  24. * Programmer : Philip W. Gorrow *
  25. * *
  26. * Start Date : 04/22/96 *
  27. * *
  28. * Last Update : April 22, 1996 [PWG] *
  29. * *
  30. * Actual member function for the field class. *
  31. *-------------------------------------------------------------------------*
  32. * Functions: *
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #ifndef _WINDOWS
  37. #include <netinet/in.h>
  38. #else
  39. #define Win32_Winsock
  40. #include <windows.h>
  41. #endif
  42. #include "field.h"
  43. // private member func
  44. void FieldClass::Clear(void)
  45. {
  46. delete[](Data);
  47. strcpy(ID,"");
  48. DataType=0;
  49. Size=0;
  50. Data=NULL;
  51. Next=NULL;
  52. }
  53. FieldClass::FieldClass(char *id, char data)
  54. {
  55. Data=NULL;
  56. Set(id,data);
  57. }
  58. FieldClass::FieldClass(char *id, unsigned char data)
  59. {
  60. Data=NULL;
  61. Set(id,data);
  62. }
  63. FieldClass::FieldClass(char *id, short data)
  64. {
  65. Data=NULL;
  66. Set(id,data);
  67. }
  68. FieldClass::FieldClass(char *id, unsigned short data)
  69. {
  70. Data=NULL;
  71. Set(id,data);
  72. }
  73. FieldClass::FieldClass(char *id, long data)
  74. {
  75. Data=NULL;
  76. Set(id,data);
  77. }
  78. FieldClass::FieldClass(char *id, unsigned long data)
  79. {
  80. Data=NULL;
  81. Set(id,data);
  82. }
  83. FieldClass::FieldClass(char *id, char *data)
  84. {
  85. Data=NULL;
  86. Set(id,data);
  87. }
  88. FieldClass::FieldClass(char *id, void *data, int length)
  89. {
  90. Data=NULL;
  91. Set(id,data,length);
  92. }
  93. void FieldClass::Set(char *id, char data)
  94. {
  95. FieldClass *Nextsave=Next;
  96. Clear();
  97. strncpy(ID, id, sizeof(ID));
  98. DataType = TYPE_CHAR;
  99. Size = sizeof(data);
  100. Data = new char[Size];
  101. memcpy(Data, &data, Size);
  102. Next = Nextsave;
  103. }
  104. void FieldClass::Set(char *id, unsigned char data)
  105. {
  106. FieldClass *Nextsave=Next;
  107. Clear();
  108. strncpy(ID, id, sizeof(ID));
  109. DataType = TYPE_UNSIGNED_CHAR;
  110. Size = sizeof(data);
  111. Data = new char[Size];
  112. memcpy(Data, &data, Size);
  113. Next = Nextsave;
  114. }
  115. void FieldClass::Set(char *id, short data)
  116. {
  117. FieldClass *Nextsave=Next;
  118. Clear();
  119. strncpy(ID, id, sizeof(ID));
  120. DataType = TYPE_SHORT;
  121. Size = sizeof(data);
  122. Data = new char[Size];
  123. memcpy(Data, &data, Size);
  124. Next = Nextsave;
  125. }
  126. void FieldClass::Set(char *id, unsigned short data)
  127. {
  128. FieldClass *Nextsave=Next;
  129. Clear();
  130. strncpy(ID, id, sizeof(ID));
  131. DataType = TYPE_UNSIGNED_SHORT;
  132. Size = sizeof(data);
  133. Data = new char[Size];
  134. memcpy(Data, &data, Size);
  135. Next = Nextsave;
  136. }
  137. void FieldClass::Set(char *id, long data)
  138. {
  139. FieldClass *Nextsave=Next;
  140. Clear();
  141. strncpy(ID, id, sizeof(ID));
  142. DataType = TYPE_LONG;
  143. Size = sizeof(data);
  144. Data = new char[Size];
  145. memcpy(Data, &data, Size);
  146. Next = Nextsave;
  147. }
  148. void FieldClass::Set(char *id, unsigned long data)
  149. {
  150. FieldClass *Nextsave=Next;
  151. Clear();
  152. strncpy(ID, id, sizeof(ID));
  153. DataType = TYPE_UNSIGNED_LONG;
  154. Size = sizeof(data);
  155. Data = new char[Size];
  156. memcpy(Data, &data, Size);
  157. Next = Nextsave;
  158. }
  159. void FieldClass::Set(char *id, char *data)
  160. {
  161. FieldClass *Nextsave=Next;
  162. Clear();
  163. strncpy(ID, id, sizeof(ID));
  164. DataType = TYPE_STRING;
  165. Size = (unsigned short)(strlen(data)+1);
  166. Data = new char[Size];
  167. memcpy(Data, data, Size);
  168. Next = Nextsave;
  169. }
  170. void FieldClass::Set(char *id, void *data, int length)
  171. {
  172. FieldClass *Nextsave=Next;
  173. Clear();
  174. strncpy(ID, id, sizeof(ID));
  175. DataType = TYPE_CHUNK;
  176. Size = (unsigned short)length;
  177. Data = new char[Size];
  178. memcpy(Data, data, Size);
  179. Next = Nextsave;
  180. }
  181. FieldClass::~FieldClass()
  182. {
  183. Clear();
  184. }
  185. // Fetch the datatype
  186. int FieldClass::Get_Type(void)
  187. {
  188. return(DataType);
  189. }
  190. void *FieldClass::Get_Data(void)
  191. {
  192. return(Data);
  193. }
  194. char *FieldClass::Get_ID(void)
  195. {
  196. return(ID);
  197. }
  198. /**************************************************************************
  199. * PACKETCLASS::HOST_TO_NET_FIELD -- Converts host field to net format *
  200. * *
  201. * INPUT: FIELD * to the data field we need to convert *
  202. * *
  203. * OUTPUT: none *
  204. * *
  205. * HISTORY: *
  206. * 04/22/1996 PWG : Created. *
  207. *========================================================================*/
  208. void FieldClass::Host_To_Net(void)
  209. {
  210. //
  211. // Before we convert the data type, we should convert the actual data
  212. // sent.
  213. //
  214. switch (DataType) {
  215. case TYPE_CHAR:
  216. case TYPE_UNSIGNED_CHAR:
  217. case TYPE_STRING:
  218. break;
  219. case TYPE_SHORT:
  220. case TYPE_UNSIGNED_SHORT:
  221. *((unsigned short *)Data) = htons(*((unsigned short *)Data));
  222. break;
  223. case TYPE_LONG:
  224. case TYPE_UNSIGNED_LONG:
  225. *((unsigned long *)Data) = htonl(*((unsigned long *)Data));
  226. break;
  227. //
  228. // Might be good to insert some type of error message here for unknown
  229. // datatypes -- but will leave that for later.
  230. //
  231. default:
  232. break;
  233. }
  234. //
  235. // Finally convert over the data type and the size of the packet.
  236. //
  237. DataType = htons(DataType);
  238. Size = htons(Size);
  239. }
  240. /**************************************************************************
  241. * PACKETCLASS::NET_TO_HOST_FIELD -- Converts net field to host format *
  242. * *
  243. * INPUT: FIELD * to the data field we need to convert *
  244. * *
  245. * OUTPUT: none *
  246. * *
  247. * HISTORY: *
  248. * 04/22/1996 PWG : Created. *
  249. *========================================================================*/
  250. void FieldClass::Net_To_Host(void)
  251. {
  252. //
  253. // Finally convert over the data type and the size of the packet.
  254. //
  255. DataType = ntohs(DataType);
  256. Size = ntohs(Size);
  257. //
  258. // Before we convert the data type, we should convert the actual data
  259. // sent.
  260. //
  261. switch (DataType) {
  262. case TYPE_CHAR:
  263. case TYPE_UNSIGNED_CHAR:
  264. case TYPE_STRING:
  265. break;
  266. case TYPE_SHORT:
  267. case TYPE_UNSIGNED_SHORT:
  268. *((unsigned short *)Data) = ntohs(*((unsigned short *)Data));
  269. break;
  270. case TYPE_LONG:
  271. case TYPE_UNSIGNED_LONG:
  272. *((unsigned long *)Data) = ntohl(*((unsigned long *)Data));
  273. break;
  274. //
  275. // Might be good to insert some type of error message here for unknown
  276. // datatypes -- but will leave that for later.
  277. //
  278. default:
  279. break;
  280. }
  281. }