string_utils.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "lite-utils.h"
  16. #include "uiot_import.h"
  17. #include "uiot_defs.h"
  18. char *LITE_strdup(const char *src)
  19. {
  20. int len = 0;
  21. char *dst = NULL;
  22. if (!src) {
  23. return NULL;
  24. }
  25. len = strlen(src) + 1;
  26. if (len > 1024) {
  27. LOG_ERROR("Too long string to duplicate, abort! len = %d", len);
  28. return NULL;
  29. }
  30. dst = (char *)HAL_Malloc(sizeof(char) * len);
  31. if (!dst) {
  32. return NULL;
  33. }
  34. strncpy(dst, src, len);
  35. return dst;
  36. }
  37. char *LITE_format_string(const char *fmt, ...)
  38. {
  39. #define TEMP_STRING_MAXLEN (512)
  40. va_list ap;
  41. char *tmp = NULL;
  42. char *dst;
  43. int rc = -1;
  44. va_start(ap, fmt);
  45. tmp = HAL_Malloc(TEMP_STRING_MAXLEN);
  46. memset(tmp, 0, TEMP_STRING_MAXLEN);
  47. rc = HAL_Vsnprintf(tmp, TEMP_STRING_MAXLEN, fmt, ap);
  48. va_end(ap);
  49. LITE_ASSERT(tmp);
  50. LITE_ASSERT(rc < 1024);
  51. dst = LITE_strdup(tmp);
  52. HAL_Free(tmp);
  53. return dst;
  54. #undef TEMP_STRING_MAXLEN
  55. }
  56. char *LITE_format_nstring(const int len, const char *fmt, ...)
  57. {
  58. va_list ap;
  59. char *tmp = NULL;
  60. char *dst;
  61. int rc = -1;
  62. va_start(ap, fmt);
  63. tmp = HAL_Malloc(len+2);
  64. memset(tmp, 0, len+2);
  65. rc = HAL_Vsnprintf(tmp, len+1, fmt, ap);
  66. va_end(ap);
  67. LITE_ASSERT(tmp);
  68. LITE_ASSERT(rc < 1024);
  69. dst = HAL_Malloc(len + 1);
  70. HAL_Snprintf(dst, (len + 1), "%s", tmp);
  71. HAL_Free(tmp);
  72. return dst;
  73. }
  74. void LITE_hexbuf_convert(unsigned char *digest, char *out, int in_len, int uppercase)
  75. {
  76. static char *zEncode[] = {"0123456789abcdef", "0123456789ABCDEF"};
  77. int j = 0;
  78. int i = 0;
  79. int idx = uppercase ? 1 : 0;
  80. for (i = 0; i < in_len; i ++) {
  81. int a = digest[i];
  82. out[j++] = zEncode[idx][(a >> 4) & 0xf];
  83. out[j++] = zEncode[idx][a & 0xf];
  84. }
  85. }
  86. static uint8_t _hexval_of_char(char hex)
  87. {
  88. if (LITE_isdigit(hex)) {
  89. return (hex - '0');
  90. }
  91. if (hex >= 'a' && hex <= 'f') {
  92. return (hex - 'a' + 10);
  93. }
  94. if (hex >= 'A' && hex <= 'F') {
  95. return (hex - 'A' + 10);
  96. }
  97. return 0;
  98. }
  99. void LITE_hexstr_convert(char *hexstr, uint8_t *out_buf, int in_len)
  100. {
  101. int i = 0;
  102. uint8_t ch0, ch1;
  103. if (in_len % 2 != 0) {
  104. LOG_ERROR("hexstr length (%d) is not even", in_len);
  105. return;
  106. }
  107. while (i < in_len) {
  108. ch0 = _hexval_of_char((char)hexstr[2 * i]);
  109. ch1 = _hexval_of_char((char)hexstr[2 * i + 1]);
  110. out_buf[i] = (ch0 << 4 | ch1);
  111. i++;
  112. }
  113. }
  114. void LITE_replace_substr(char originalString[], char key[], char swap[])
  115. {
  116. int lengthOfOriginalString, lengthOfKey, lengthOfSwap, i, j, flag;
  117. char tmp[512];
  118. lengthOfOriginalString = strlen(originalString);
  119. lengthOfKey = strlen(key);
  120. lengthOfSwap = strlen(swap);
  121. for (i = 0; i <= lengthOfOriginalString - lengthOfKey; i++) {
  122. flag = 1;
  123. for (j = 0; j < lengthOfKey; j++) {
  124. if (originalString[i + j] != key[j]) {
  125. flag = 0;
  126. break;
  127. }
  128. }
  129. if (flag) {
  130. strcpy(tmp, originalString);
  131. strcpy(&tmp[i], swap);
  132. strcpy(&tmp[i + lengthOfSwap], &originalString[i + lengthOfKey]);
  133. strcpy(originalString, tmp);
  134. i += lengthOfSwap - 1;
  135. lengthOfOriginalString = strlen(originalString);
  136. }
  137. }
  138. }