dm_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include "iotx_dm_internal.h"
  5. int dm_utils_copy_direct(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
  6. {
  7. if (input == NULL || output == NULL || *output != NULL) {
  8. return DM_INVALID_PARAMETER;
  9. }
  10. *output = HAL_Malloc(output_len);
  11. if (*output == NULL) {
  12. return DM_MEMORY_NOT_ENOUGH;
  13. }
  14. memset(*output, 0, output_len);
  15. memcpy(*output, input, input_len);
  16. return SUCCESS_RETURN;
  17. }
  18. int dm_utils_copy(_IN_ void *input, _IN_ int input_len, _OU_ void **output, _IN_ int output_len)
  19. {
  20. if (input == NULL || output == NULL || *output != NULL) {
  21. return DM_INVALID_PARAMETER;
  22. }
  23. *output = DM_malloc(output_len);
  24. if (*output == NULL) {
  25. return DM_MEMORY_NOT_ENOUGH;
  26. }
  27. memset(*output, 0, output_len);
  28. memcpy(*output, input, input_len);
  29. return SUCCESS_RETURN;
  30. }
  31. int dm_utils_strarr_index(_IN_ char *input, _IN_ int input_len,
  32. _OU_ int *partial_input_len, _OU_ int *array_input_len, _OU_ int *array_index)
  33. {
  34. int index = 0;
  35. int deep = 0;
  36. char *bracket_pre = NULL;
  37. char *bracket_suf = NULL;
  38. char array_index_str[10] = {0};
  39. if (input == NULL || input_len <= 1 || array_index == NULL) {
  40. return DM_INVALID_PARAMETER;
  41. }
  42. for (index = 0; index < input_len; index++) {
  43. switch (input[index]) {
  44. case '[': {
  45. if (deep != 0) {
  46. return FAIL_RETURN;
  47. }
  48. deep++;
  49. if (!bracket_pre) {
  50. bracket_pre = (char *)&input[index];
  51. }
  52. }
  53. break;
  54. case ']': {
  55. if (deep != 1) {
  56. return FAIL_RETURN;
  57. }
  58. deep--;
  59. if (input[index - 1] == '[') {
  60. return FAIL_RETURN;
  61. }
  62. if (!bracket_suf) {
  63. bracket_suf = (char *)&input[index];
  64. }
  65. }
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. if (bracket_pre && bracket_suf && ((bracket_suf - input + 1) == input_len)) {
  72. if (partial_input_len) {
  73. *partial_input_len = bracket_pre - input;
  74. }
  75. if (array_input_len) {
  76. *array_input_len = bracket_suf - input + 1;
  77. }
  78. /* Get Index */
  79. memcpy(array_index_str, bracket_pre + 1, bracket_suf - bracket_pre - 1);
  80. *array_index = atoi(array_index_str);
  81. return SUCCESS_RETURN;
  82. }
  83. return FAIL_RETURN;
  84. }
  85. int dm_utils_itoa_direct(_IN_ int input, _OU_ char **output)
  86. {
  87. int res = 0;
  88. char temp_output[10 + 1] = {0};
  89. if (output == NULL || *output != NULL) {
  90. return DM_INVALID_PARAMETER;
  91. }
  92. res = HAL_Snprintf(temp_output, 10, "%d", input);
  93. if (res < 0) {
  94. return FAIL_RETURN;
  95. }
  96. *output = HAL_Malloc(strlen(temp_output) + 1);
  97. if (*output == NULL) {
  98. return DM_MEMORY_NOT_ENOUGH;
  99. }
  100. memset(*output, 0, strlen(temp_output) + 1);
  101. memcpy(*output, temp_output, strlen(temp_output));
  102. return SUCCESS_RETURN;
  103. }
  104. int dm_utils_itoa(_IN_ int input, _OU_ char **output)
  105. {
  106. int res = 0;
  107. char temp_output[10 + 1] = {0};
  108. if (output == NULL || *output != NULL) {
  109. return DM_INVALID_PARAMETER;
  110. }
  111. res = HAL_Snprintf(temp_output, 10, "%d", input);
  112. if (res < 0) {
  113. return FAIL_RETURN;
  114. }
  115. *output = DM_malloc(strlen(temp_output) + 1);
  116. if (*output == NULL) {
  117. return DM_MEMORY_NOT_ENOUGH;
  118. }
  119. memset(*output, 0, strlen(temp_output) + 1);
  120. memcpy(*output, temp_output, strlen(temp_output));
  121. return SUCCESS_RETURN;
  122. }
  123. int dm_utils_ftoa_direct(_IN_ double input, _OU_ char **output)
  124. {
  125. int res = 0;
  126. char temp_output[30 + 1] = {0};
  127. if (output == NULL || *output != NULL) {
  128. return DM_INVALID_PARAMETER;
  129. }
  130. res = HAL_Snprintf(temp_output, 30, "%f", input);
  131. if (res < 0) {
  132. return FAIL_RETURN;
  133. }
  134. *output = HAL_Malloc(strlen(temp_output) + 1);
  135. if (*output == NULL) {
  136. return DM_MEMORY_NOT_ENOUGH;
  137. }
  138. memset(*output, 0, strlen(temp_output) + 1);
  139. memcpy(*output, temp_output, strlen(temp_output));
  140. return SUCCESS_RETURN;
  141. }
  142. int dm_utils_ftoa(_IN_ double input, _OU_ char **output)
  143. {
  144. int res = 0;
  145. char temp_output[30 + 1] = {0};
  146. if (output == NULL || *output != NULL) {
  147. return DM_INVALID_PARAMETER;
  148. }
  149. res = HAL_Snprintf(temp_output, 30, "%f", input);
  150. if (res < 0) {
  151. return FAIL_RETURN;
  152. }
  153. *output = DM_malloc(strlen(temp_output) + 1);
  154. if (*output == NULL) {
  155. return DM_MEMORY_NOT_ENOUGH;
  156. }
  157. memset(*output, 0, strlen(temp_output) + 1);
  158. memcpy(*output, temp_output, strlen(temp_output));
  159. return SUCCESS_RETURN;
  160. }
  161. int dm_utils_hex_to_str(_IN_ unsigned char *input, _IN_ int input_len, _OU_ char **output)
  162. {
  163. int index = 0, output_len = 0;
  164. unsigned char iter_char = 0;
  165. if (input == NULL || input_len <= 0 || output == NULL || *output != NULL) {
  166. return DM_INVALID_PARAMETER;
  167. }
  168. output_len = input_len * 2;
  169. *output = DM_malloc(output_len + 1);
  170. if (*output == NULL) {
  171. return DM_MEMORY_NOT_ENOUGH;
  172. }
  173. memset(*output, 0, output_len + 1);
  174. for (index = 0; index < input_len; index++) {
  175. iter_char = (input[index] >> 4) & 0x0F;
  176. if (iter_char <= 0x09) {
  177. iter_char += '0';
  178. } else if (iter_char >= 0x0A && iter_char <= 0x0F) {
  179. iter_char += 'A' - 0x0A;
  180. }
  181. (*output)[index * 2] = iter_char;
  182. iter_char = (input[index]) & 0x0F;
  183. if (iter_char <= 0x09) {
  184. iter_char += '0';
  185. } else if (iter_char >= 0x0A && iter_char <= 0x0F) {
  186. iter_char += 'A' - 0x0A;
  187. }
  188. (*output)[index * 2 + 1] = iter_char;
  189. }
  190. return SUCCESS_RETURN;
  191. }
  192. int dm_utils_str_to_hex(_IN_ char *input, _IN_ int input_len, _OU_ unsigned char **output, _OU_ int *output_len)
  193. {
  194. int index = 0;
  195. char iter_char = 0;
  196. if (input == NULL || input_len <= 0 || input_len % 2 != 0 ||
  197. output == NULL || *output != NULL || output_len == NULL) {
  198. return DM_INVALID_PARAMETER;
  199. }
  200. *output_len = input_len / 2;
  201. *output = DM_malloc(*output_len);
  202. if (*output == NULL) {
  203. return DM_MEMORY_NOT_ENOUGH;
  204. }
  205. memset(*output, 0, *output_len);
  206. for (index = 0; index < input_len; index += 2) {
  207. if (input[index] >= '0' && input[index] <= '9') {
  208. iter_char = input[index] - '0';
  209. } else if (input[index] >= 'A' && input[index] <= 'F') {
  210. iter_char = input[index] - 'A' + 0x0A;
  211. }
  212. (*output)[index / 2] |= (iter_char << 4) & 0xF0;
  213. if (input[index + 1] >= '0' && input[index + 1] <= '9') {
  214. iter_char = input[index + 1] - '0';
  215. } else if (input[index + 1] >= 'A' && input[index + 1] <= 'F') {
  216. iter_char = input[index + 1] - 'A' + 0x0A;
  217. }
  218. (*output)[index / 2] |= (iter_char) & 0x0F;
  219. }
  220. return SUCCESS_RETURN;
  221. }
  222. int dm_utils_memtok(_IN_ char *input, _IN_ int input_len, _IN_ char delimiter, _IN_ int index, _OU_ int *offset)
  223. {
  224. int item_index = 0;
  225. int count = 0;
  226. if (input == NULL || input_len <= 0 || offset == NULL) {
  227. return DM_INVALID_PARAMETER;
  228. }
  229. for (item_index = 0; item_index < input_len; item_index++) {
  230. if (input[item_index] == delimiter && (item_index + 1) < input_len) {
  231. count++;
  232. if (count == index) {
  233. *offset = item_index;
  234. return SUCCESS_RETURN;
  235. }
  236. }
  237. }
  238. return FAIL_RETURN;
  239. }
  240. int dm_utils_replace_char(_IN_ char *input, _IN_ int input_len, _IN_ char src, _IN_ char dest)
  241. {
  242. int index = 0;
  243. if (input == NULL || input_len <= 0) {
  244. return DM_INVALID_PARAMETER;
  245. }
  246. for (index = 0; index < input_len; index++) {
  247. if (input[index] == src) {
  248. input[index] = dest;
  249. }
  250. }
  251. return SUCCESS_RETURN;
  252. }
  253. int dm_utils_service_name(_IN_ const char *prefix, _IN_ const char *name, _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
  254. _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ char **service_name)
  255. {
  256. int prefix_len = (prefix == NULL) ? (0) : (strlen(prefix));
  257. int name_len = (name == NULL) ? (0) : (strlen(name));
  258. int service_name_len = 0;
  259. if ((prefix == NULL && name == NULL) || product_key == NULL || device_name == NULL ||
  260. service_name == NULL || *service_name != NULL) {
  261. return DM_INVALID_PARAMETER;
  262. }
  263. service_name_len = prefix_len + name_len + strlen(product_key) + strlen(device_name) + 1;
  264. *service_name = DM_malloc(service_name_len);
  265. if (*service_name == NULL) {
  266. return DM_MEMORY_NOT_ENOUGH;
  267. }
  268. memset(*service_name, 0, service_name_len);
  269. if (prefix != NULL) {
  270. HAL_Snprintf(*service_name, service_name_len, prefix, product_key, device_name);
  271. }
  272. if (name != NULL) {
  273. memcpy(*service_name + strlen(*service_name), name, name_len);
  274. }
  275. return SUCCESS_RETURN;
  276. }
  277. int dm_utils_uri_add_prefix(_IN_ const char *prefix, _IN_ char *uri, _OU_ char **new_uri)
  278. {
  279. int new_uri_len = 0;
  280. if (prefix == NULL || uri == NULL || new_uri == NULL || *new_uri != NULL) {
  281. return DM_INVALID_PARAMETER;
  282. }
  283. new_uri_len = strlen(prefix) + strlen(uri) + 1;
  284. *new_uri = DM_malloc(new_uri_len);
  285. if (*new_uri == NULL) {
  286. return DM_MEMORY_NOT_ENOUGH;
  287. }
  288. memset(*new_uri, 0, new_uri_len);
  289. memcpy(*new_uri, prefix, strlen(prefix));
  290. memcpy(*new_uri + strlen(*new_uri), uri, strlen(uri));
  291. return SUCCESS_RETURN;
  292. }
  293. int dm_utils_json_parse(_IN_ const char *payload, _IN_ int payload_len, _IN_ int type, _OU_ lite_cjson_t *lite)
  294. {
  295. int res = 0;
  296. if (payload == NULL || payload_len <= 0 || type < 0 || lite == NULL) {
  297. return DM_INVALID_PARAMETER;
  298. }
  299. memset(lite, 0, sizeof(lite_cjson_t));
  300. res = lite_cjson_parse(payload, payload_len, lite);
  301. if (res != SUCCESS_RETURN) {
  302. memset(lite, 0, sizeof(lite_cjson_t));
  303. return FAIL_RETURN;
  304. }
  305. if (type != cJSON_Invalid && lite->type != type) {
  306. memset(lite, 0, sizeof(lite_cjson_t));
  307. return FAIL_RETURN;
  308. }
  309. return SUCCESS_RETURN;
  310. }
  311. int dm_utils_json_object_item(_IN_ lite_cjson_t *lite, _IN_ const char *key, _IN_ int key_len, _IN_ int type,
  312. _OU_ lite_cjson_t *lite_item)
  313. {
  314. int res = 0;
  315. if (lite == NULL || lite->type != cJSON_Object || key == NULL || key_len <= 0 || type < 0 || lite_item == NULL) {
  316. return DM_INVALID_PARAMETER;
  317. }
  318. if (lite->type != cJSON_Object) {
  319. dm_log_err("lite->type != cJSON_Object, %d", lite->type);
  320. }
  321. memset(lite_item, 0, sizeof(lite_cjson_t));
  322. res = lite_cjson_object_item(lite, key, key_len, lite_item);
  323. if (res != SUCCESS_RETURN) {
  324. /* dm_log_err(DM_UTILS_LOG_JSON_PARSE_FAILED, lite->value_length, lite->value); */
  325. memset(lite_item, 0, sizeof(lite_cjson_t));
  326. return FAIL_RETURN;
  327. }
  328. if (type != cJSON_Invalid && lite_item->type != type) {
  329. memset(lite_item, 0, sizeof(lite_cjson_t));
  330. return FAIL_RETURN;
  331. }
  332. return SUCCESS_RETURN;
  333. }
  334. void *dm_utils_malloc(unsigned int size)
  335. {
  336. #ifdef INFRA_MEM_STATS
  337. return LITE_malloc(size, MEM_MAGIC, "lite_cjson");
  338. #else
  339. return HAL_Malloc(size);
  340. #endif
  341. }
  342. void dm_utils_free(void *ptr)
  343. {
  344. #ifdef INFRA_MEM_STATS
  345. LITE_free(ptr);
  346. #else
  347. HAL_Free((void *)ptr);
  348. #endif
  349. }