FIELD.CPP 6.7 KB

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