field.cpp 8.1 KB

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