string_utils.c 4.3 KB

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