FIELD.CPP 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. FieldClass::FieldClass(char *id, char data)
  33. {
  34. strncpy(ID, id, sizeof(ID));
  35. DataType = TYPE_CHAR;
  36. Size = sizeof(data);
  37. Data = new char[Size];
  38. memcpy(Data, &data, Size);
  39. Next = NULL;
  40. }
  41. FieldClass::FieldClass(char *id, unsigned char data)
  42. {
  43. strncpy(ID, id, sizeof(ID));
  44. DataType = TYPE_UNSIGNED_CHAR;
  45. Size = sizeof(data);
  46. Data = new char[Size];
  47. memcpy(Data, &data, Size);
  48. Next = NULL;
  49. }
  50. FieldClass::FieldClass(char *id, short data)
  51. {
  52. strncpy(ID, id, sizeof(ID));
  53. DataType = TYPE_SHORT;
  54. Size = sizeof(data);
  55. Data = new char[Size];
  56. memcpy(Data, &data, Size);
  57. Next = NULL;
  58. }
  59. FieldClass::FieldClass(char *id, unsigned short data)
  60. {
  61. strncpy(ID, id, sizeof(ID));
  62. DataType = TYPE_UNSIGNED_SHORT;
  63. Size = sizeof(data);
  64. Data = new char[Size];
  65. memcpy(Data, &data, Size);
  66. Next = NULL;
  67. }
  68. FieldClass::FieldClass(char *id, long data)
  69. {
  70. strncpy(ID, id, sizeof(ID));
  71. DataType = TYPE_LONG;
  72. Size = sizeof(data);
  73. Data = new char[Size];
  74. memcpy(Data, &data, Size);
  75. Next = NULL;
  76. }
  77. FieldClass::FieldClass(char *id, unsigned long data)
  78. {
  79. strncpy(ID, id, sizeof(ID));
  80. DataType = TYPE_UNSIGNED_LONG;
  81. Size = sizeof(data);
  82. Data = new char[Size];
  83. memcpy(Data, &data, Size);
  84. Next = NULL;
  85. }
  86. FieldClass::FieldClass(char *id, char *data)
  87. {
  88. strncpy(ID, id, sizeof(ID));
  89. DataType = TYPE_STRING;
  90. Size = (unsigned short)(strlen(data)+1);
  91. Data = new char[Size];
  92. memcpy(Data, data, Size);
  93. Next = NULL;
  94. }
  95. FieldClass::FieldClass(char *id, void *data, int length)
  96. {
  97. strncpy(ID, id, sizeof(ID));
  98. DataType = TYPE_CHUNK;
  99. Size = (unsigned short)length;
  100. Data = new char[Size];
  101. memcpy(Data, data, Size);
  102. Next = NULL;
  103. }
  104. /**************************************************************************
  105. * PACKETCLASS::HOST_TO_NET_FIELD -- Converts host field to net format *
  106. * *
  107. * INPUT: FIELD * to the data field we need to convert *
  108. * *
  109. * OUTPUT: none *
  110. * *
  111. * HISTORY: *
  112. * 04/22/1996 PWG : Created. *
  113. *========================================================================*/
  114. void FieldClass::Host_To_Net(void)
  115. {
  116. //
  117. // Before we convert the data type, we should convert the actual data
  118. // sent.
  119. //
  120. switch (DataType) {
  121. case TYPE_CHAR:
  122. case TYPE_UNSIGNED_CHAR:
  123. case TYPE_STRING:
  124. case TYPE_CHUNK:
  125. break;
  126. case TYPE_SHORT:
  127. case TYPE_UNSIGNED_SHORT:
  128. *((unsigned short *)Data) = htons(*((unsigned short *)Data));
  129. break;
  130. case TYPE_LONG:
  131. case TYPE_UNSIGNED_LONG:
  132. *((unsigned long *)Data) = htonl(*((unsigned long *)Data));
  133. break;
  134. //
  135. // Might be good to insert some type of error message here for unknown
  136. // datatypes -- but will leave that for later.
  137. //
  138. default:
  139. break;
  140. }
  141. //
  142. // Finally convert over the data type and the size of the packet.
  143. //
  144. DataType = htons(DataType);
  145. Size = htons(Size);
  146. }
  147. /**************************************************************************
  148. * PACKETCLASS::NET_TO_HOST_FIELD -- Converts net field to host format *
  149. * *
  150. * INPUT: FIELD * to the data field we need to convert *
  151. * *
  152. * OUTPUT: none *
  153. * *
  154. * HISTORY: *
  155. * 04/22/1996 PWG : Created. *
  156. *========================================================================*/
  157. void FieldClass::Net_To_Host(void)
  158. {
  159. //
  160. // Convert the variables to host order. This needs to be converted so
  161. // the switch statement does compares on the data that follows.
  162. //
  163. Size = ntohs(Size);
  164. DataType = ntohs(DataType);
  165. //
  166. // Before we convert the data type, we should convert the actual data
  167. // sent.
  168. //
  169. switch (DataType) {
  170. case TYPE_CHAR:
  171. case TYPE_UNSIGNED_CHAR:
  172. case TYPE_STRING:
  173. case TYPE_CHUNK:
  174. break;
  175. case TYPE_SHORT:
  176. case TYPE_UNSIGNED_SHORT:
  177. *((unsigned short *)Data) = ntohs(*((unsigned short *)Data));
  178. break;
  179. case TYPE_LONG:
  180. case TYPE_UNSIGNED_LONG:
  181. *((unsigned long *)Data) = ntohl(*((unsigned long *)Data));
  182. break;
  183. //
  184. // Might be good to insert some type of error message here for unknown
  185. // datatypes -- but will leave that for later.
  186. //
  187. default:
  188. break;
  189. }
  190. }