cipstring.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*******************************************************************************
  2. * Copyright (c) 2019, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. /** @file
  7. * @brief Implements functions to operate on CIP string types
  8. *
  9. * Some functions to create CIP string types from C strings or data buffers.
  10. */
  11. #include "cipstring.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "trace.h"
  15. #include "opener_api.h"
  16. CipStringN *ClearCipStringN(CipStringN *const cip_string) {
  17. if(NULL != cip_string) {
  18. if(NULL != cip_string->string) {
  19. CipFree(cip_string->string);
  20. }
  21. cip_string->string = NULL;
  22. cip_string->length = 0;
  23. cip_string->size = 0;
  24. } else {
  25. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  26. }
  27. return cip_string;
  28. }
  29. void FreeCipStringN(CipStringN *const cip_string) {
  30. if(NULL != cip_string) {
  31. ClearCipStringN(cip_string);
  32. CipFree(cip_string);
  33. } else {
  34. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  35. }
  36. }
  37. /** @brief Sets length and data for a CipStringN based on an octet stream, symbol length, and symbol width
  38. *
  39. * @param cip_string The string to be set
  40. * @param str_len Amount of CipStringN symbols
  41. * @param size Width of the CipStringN symbols
  42. * @param data The octet stream
  43. *
  44. * @return The CipStringN address
  45. */
  46. CipStringN *SetCipStringNByData(CipStringN *const cip_string,
  47. CipUint str_len,
  48. CipUint size,
  49. const CipOctet *const data) {
  50. CipStringN *result = cip_string;
  51. (void) ClearCipStringN(cip_string);
  52. if(0 != str_len) {
  53. /* No trailing '\0' character! */
  54. cip_string->length = str_len;
  55. cip_string->size = size;
  56. cip_string->string = CipCalloc(cip_string->length,
  57. cip_string->size * sizeof(CipOctet) );
  58. if(NULL == cip_string->string) {
  59. result = NULL;
  60. cip_string->length = 0;
  61. cip_string->size = 0;
  62. } else {
  63. memcpy(cip_string->string,
  64. data,
  65. cip_string->length * cip_string->size * sizeof(CipOctet) );
  66. }
  67. }
  68. return result;
  69. }
  70. CipStringN *SetCipStringNByCstr(CipStringN *const cip_string,
  71. const char *const string,
  72. const CipUint symbol_size) {
  73. if(0 != strlen(string) % symbol_size) {
  74. OPENER_TRACE_ERR("Not enough octets for %d width StringN\n", symbol_size);
  75. return cip_string;
  76. }
  77. /* We expect here, that the length of the string is the total length in Octets */
  78. return SetCipStringNByData(cip_string,
  79. (CipUint) strlen(string) / symbol_size,
  80. symbol_size,
  81. (const CipOctet *) string);
  82. }
  83. void FreeCipString2(CipString2 *const cip_string) {
  84. if(NULL != cip_string) {
  85. ClearCipString2(cip_string);
  86. CipFree(cip_string);
  87. } else {
  88. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  89. }
  90. }
  91. /** @brief Frees a CipString2 structure
  92. *
  93. * @param cip_string The CipString2 structure to be freed
  94. *
  95. * @return Freed CipString2 structure
  96. *
  97. */
  98. CipString2 *ClearCipString2(CipString2 *const cip_string) {
  99. if(NULL != cip_string) {
  100. if(NULL != cip_string->string) {
  101. CipFree(cip_string->string);
  102. cip_string->string = NULL;
  103. cip_string->length = 0;
  104. }
  105. } else {
  106. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  107. }
  108. return cip_string;
  109. }
  110. CipString2 *SetCipString2ByData(CipString2 *const cip_string,
  111. CipUint str_len,
  112. const CipOctet *const data) {
  113. CipString2 *result = cip_string;
  114. (void) ClearCipString2(cip_string);
  115. if(0 != str_len) {
  116. /* No trailing '\0' character! */
  117. cip_string->string = CipCalloc(str_len, 2 * sizeof(CipOctet) );
  118. if(NULL == cip_string->string) {
  119. result = NULL;
  120. } else {
  121. cip_string->length = str_len;
  122. memcpy(cip_string->string, data, str_len * 2 * sizeof(CipOctet) );
  123. }
  124. }
  125. return result;
  126. }
  127. CipString2 *SetCipString2ByCstr(CipString2 *const cip_string,
  128. const char *const string) {
  129. return SetCipString2ByData(cip_string, (CipUint) strlen(string) / 2,
  130. (const CipOctet *) string);
  131. }
  132. CipString *ClearCipString(CipString *const cip_string) {
  133. if(NULL != cip_string) {
  134. if(NULL != cip_string->string) {
  135. CipFree(cip_string->string);
  136. cip_string->string = NULL;
  137. cip_string->length = 0;
  138. }
  139. } else {
  140. OPENER_TRACE_ERR("Trying to free NULL CipString!\n");
  141. }
  142. return cip_string;
  143. }
  144. void FreeCipString(CipString *const cip_string) {
  145. if(NULL != cip_string) {
  146. ClearCipString(cip_string);
  147. CipFree(cip_string);
  148. } else {
  149. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  150. }
  151. }
  152. CipString *SetCipStringByData(CipString *const cip_string,
  153. CipUint str_len,
  154. const CipOctet *const data) {
  155. CipString *result = cip_string;
  156. (void) ClearCipString(cip_string);
  157. if(0 != str_len) {
  158. /* No trailing '\0' character. */
  159. cip_string->string = CipCalloc(str_len, sizeof(CipOctet) );
  160. if(NULL == cip_string->string) {
  161. result = NULL;
  162. } else {
  163. cip_string->length = str_len;
  164. memcpy(cip_string->string, data, str_len);
  165. }
  166. }
  167. return result;
  168. }
  169. CipString *SetCipStringByCstr(CipString *const cip_string,
  170. const char *const string) {
  171. return SetCipStringByData(cip_string, (CipUint) strlen(string),
  172. (const CipOctet *) string);
  173. }
  174. CipShortString *ClearCipShortString(CipShortString *const cip_string) {
  175. if(NULL != cip_string) {
  176. if(NULL != cip_string->string) {
  177. CipFree(cip_string->string);
  178. cip_string->string = NULL;
  179. cip_string->length = 0;
  180. }
  181. } else {
  182. OPENER_TRACE_ERR("Trying to free NULL CipShortString!\n");
  183. }
  184. return cip_string;
  185. }
  186. /** @brief Frees a CipShortString structure
  187. *
  188. * @param cip_string The CipShortString structure to be freed
  189. *
  190. * @return Freed CipShortString structure
  191. *
  192. */
  193. void FreeCipShortString(CipShortString *const cip_string) {
  194. if(NULL != cip_string) {
  195. ClearCipShortString(cip_string);
  196. CipFree(cip_string);
  197. } else {
  198. OPENER_TRACE_ERR("Trying to free NULL CipString2!\n");
  199. }
  200. }
  201. /** @brief Sets length and data for a CipShortString based on an octet stream and symbol length
  202. *
  203. * @param cip_string The CipShortString to be set
  204. * @param str_len Amount of CipShortString symbols
  205. * @param data The octet stream
  206. *
  207. * @return The CipShortString address
  208. */
  209. CipShortString *SetCipShortStringByData(CipShortString *const cip_string,
  210. CipUsint str_len,
  211. const CipOctet *const data) {
  212. CipShortString *result = cip_string;
  213. (void) ClearCipShortString(cip_string);
  214. if(0 != str_len) {
  215. /* No trailing '\0' character. */
  216. cip_string->string = CipCalloc(str_len, sizeof(CipOctet) );
  217. if(NULL == cip_string->string) {
  218. result = NULL;
  219. } else {
  220. cip_string->length = str_len;
  221. memcpy(cip_string->string, data, str_len);
  222. }
  223. }
  224. return result;
  225. }
  226. /** @brief Copies the content of C-string to a CipShortString under the expectation, that each C-String element is a CipShortString octet
  227. *
  228. * @param cip_string Target CipShortString
  229. * @param string Source C-string
  230. *
  231. * @return Target CipShortString
  232. *
  233. */
  234. CipShortString *SetCipShortStringByCstr(CipShortString *const cip_string,
  235. const char *const string) {
  236. return SetCipShortStringByData(cip_string, (CipUsint) strlen(string),
  237. (const CipOctet *) string);
  238. }