cipstring.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "opener_api.h"
  15. CipString *SetCipStringByData
  16. (
  17. CipString *p_cip_string,
  18. size_t str_len,
  19. const EipUint8 *p_data
  20. )
  21. {
  22. CipString *p_result = p_cip_string;
  23. if (NULL != p_cip_string->string) {
  24. CipFree(p_cip_string->string);
  25. p_cip_string->string = NULL;
  26. p_cip_string->length = 0;
  27. }
  28. if (str_len) {
  29. /* Allocate & clear +1 bytes for the trailing '\0' character. */
  30. p_cip_string->string = CipCalloc(str_len+1, sizeof (EipUint8));
  31. if (NULL == p_cip_string->string) {
  32. p_result = NULL;
  33. }
  34. else {
  35. p_cip_string->length = str_len;
  36. memcpy(p_cip_string->string, p_data, str_len);
  37. }
  38. }
  39. return p_result;
  40. }
  41. CipString *SetCipStringByCstr
  42. (
  43. CipString *p_cip_string,
  44. const char *p_string
  45. )
  46. {
  47. return SetCipStringByData(p_cip_string, strlen(p_string),
  48. (const EipByte *)p_string);
  49. }
  50. CipShortString *SetCipShortStringByData
  51. (
  52. CipShortString *p_cip_string,
  53. size_t str_len,
  54. const EipUint8 *p_data
  55. )
  56. {
  57. CipShortString *p_result = p_cip_string;
  58. if (NULL != p_cip_string->string) {
  59. CipFree(p_cip_string->string);
  60. p_cip_string->string = NULL;
  61. p_cip_string->length = 0;
  62. }
  63. if (str_len) {
  64. /* Allocate & clear +1 bytes for the trailing '\0' character. */
  65. p_cip_string->string = CipCalloc(str_len+1, sizeof (EipUint8));
  66. if (NULL == p_cip_string->string) {
  67. p_result = NULL;
  68. }
  69. else {
  70. p_cip_string->length = str_len;
  71. memcpy(p_cip_string->string, p_data, str_len);
  72. }
  73. }
  74. return p_result;
  75. }
  76. CipShortString *SetCipShortStringByCstr
  77. (
  78. CipShortString *p_cip_string,
  79. const char *p_string
  80. )
  81. {
  82. return SetCipShortStringByData(p_cip_string, strlen(p_string),
  83. (const EipByte *)p_string);
  84. }