host_tool_utils.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "host_tool_utils.h"
  6. #include "bi-inc/shared_utils.h"
  7. #include "bh_platform.h"
  8. #include <time.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. typedef union jvalue {
  14. bool z;
  15. int8_t b;
  16. uint16_t c;
  17. int16_t s;
  18. int32_t i;
  19. int64_t j;
  20. float f;
  21. double d;
  22. } jvalue;
  23. static inline int16_t
  24. get_int16(const char *buf)
  25. {
  26. int16_t ret;
  27. bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t));
  28. return ret;
  29. }
  30. static inline uint16_t
  31. get_uint16(const char *buf)
  32. {
  33. return get_int16(buf);
  34. }
  35. static inline int32_t
  36. get_int32(const char *buf)
  37. {
  38. int32_t ret;
  39. bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t));
  40. return ret;
  41. }
  42. static inline uint32_t
  43. get_uint32(const char *buf)
  44. {
  45. return get_int32(buf);
  46. }
  47. char *
  48. attr_container_get_attr_begin(const attr_container_t *attr_cont,
  49. uint32_t *p_total_length, uint16_t *p_attr_num);
  50. cJSON *
  51. attr2json(const attr_container_t *attr_cont)
  52. {
  53. uint32_t total_length;
  54. uint16_t attr_num, i, j, type;
  55. const char *p, *tag, *key;
  56. jvalue value;
  57. cJSON *root;
  58. if (!attr_cont)
  59. return NULL;
  60. root = cJSON_CreateObject();
  61. if (!root)
  62. return NULL;
  63. /* TODO: how to convert the tag? */
  64. tag = attr_container_get_tag(attr_cont);
  65. if (!tag)
  66. goto fail;
  67. p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num);
  68. if (!p)
  69. goto fail;
  70. for (i = 0; i < attr_num; i++) {
  71. cJSON *obj;
  72. key = p + 2;
  73. /* Skip key len and key */
  74. p += 2 + get_uint16(p);
  75. type = *p++;
  76. switch (type) {
  77. case ATTR_TYPE_SHORT:
  78. bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
  79. if (NULL == (obj = cJSON_CreateNumber(value.s)))
  80. goto fail;
  81. cJSON_AddItemToObject(root, key, obj);
  82. /* another approach: cJSON_AddNumberToObject(root, key, value.s)
  83. */
  84. p += 2;
  85. break;
  86. case ATTR_TYPE_INT:
  87. bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
  88. if (NULL == (obj = cJSON_CreateNumber(value.i)))
  89. goto fail;
  90. cJSON_AddItemToObject(root, key, obj);
  91. p += 4;
  92. break;
  93. case ATTR_TYPE_INT64:
  94. bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
  95. if (NULL == (obj = cJSON_CreateNumber(value.j)))
  96. goto fail;
  97. cJSON_AddItemToObject(root, key, obj);
  98. p += 8;
  99. break;
  100. case ATTR_TYPE_BYTE:
  101. bh_memcpy_s(&value.b, 1, p, 1);
  102. if (NULL == (obj = cJSON_CreateNumber(value.b)))
  103. goto fail;
  104. cJSON_AddItemToObject(root, key, obj);
  105. p++;
  106. break;
  107. case ATTR_TYPE_UINT16:
  108. bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
  109. if (NULL == (obj = cJSON_CreateNumber(value.c)))
  110. goto fail;
  111. cJSON_AddItemToObject(root, key, obj);
  112. p += 2;
  113. break;
  114. case ATTR_TYPE_FLOAT:
  115. bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
  116. if (NULL == (obj = cJSON_CreateNumber(value.f)))
  117. goto fail;
  118. cJSON_AddItemToObject(root, key, obj);
  119. p += 4;
  120. break;
  121. case ATTR_TYPE_DOUBLE:
  122. bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
  123. if (NULL == (obj = cJSON_CreateNumber(value.d)))
  124. goto fail;
  125. cJSON_AddItemToObject(root, key, obj);
  126. p += 8;
  127. break;
  128. case ATTR_TYPE_BOOLEAN:
  129. bh_memcpy_s(&value.z, 1, p, 1);
  130. if (NULL == (obj = cJSON_CreateBool(value.z)))
  131. goto fail;
  132. cJSON_AddItemToObject(root, key, obj);
  133. p++;
  134. break;
  135. case ATTR_TYPE_STRING:
  136. if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t))))
  137. goto fail;
  138. cJSON_AddItemToObject(root, key, obj);
  139. p += sizeof(uint16_t) + get_uint16(p);
  140. break;
  141. case ATTR_TYPE_BYTEARRAY:
  142. if (NULL == (obj = cJSON_CreateArray()))
  143. goto fail;
  144. cJSON_AddItemToObject(root, key, obj);
  145. for (j = 0; j < get_uint32(p); j++) {
  146. cJSON *item =
  147. cJSON_CreateNumber(*(p + sizeof(uint32_t) + j));
  148. if (item == NULL)
  149. goto fail;
  150. cJSON_AddItemToArray(obj, item);
  151. }
  152. p += sizeof(uint32_t) + get_uint32(p);
  153. break;
  154. }
  155. }
  156. return root;
  157. fail:
  158. cJSON_Delete(root);
  159. return NULL;
  160. }
  161. attr_container_t *
  162. json2attr(const cJSON *json_obj)
  163. {
  164. attr_container_t *attr_cont;
  165. cJSON *item;
  166. if (NULL == (attr_cont = attr_container_create("")))
  167. return NULL;
  168. if (!cJSON_IsObject(json_obj))
  169. goto fail;
  170. cJSON_ArrayForEach(item, json_obj)
  171. {
  172. if (cJSON_IsNumber(item)) {
  173. attr_container_set_double(&attr_cont, item->string,
  174. item->valuedouble);
  175. }
  176. else if (cJSON_IsTrue(item)) {
  177. attr_container_set_bool(&attr_cont, item->string, true);
  178. }
  179. else if (cJSON_IsFalse(item)) {
  180. attr_container_set_bool(&attr_cont, item->string, false);
  181. }
  182. else if (cJSON_IsString(item)) {
  183. attr_container_set_string(&attr_cont, item->string,
  184. item->valuestring);
  185. }
  186. else if (cJSON_IsArray(item)) {
  187. int8_t *array;
  188. int i = 0, len = sizeof(int8_t) * cJSON_GetArraySize(item);
  189. cJSON *array_item;
  190. if (0 == len || NULL == (array = (int8_t *)malloc(len)))
  191. goto fail;
  192. memset(array, 0, len);
  193. cJSON_ArrayForEach(array_item, item)
  194. {
  195. /* must be number array */
  196. if (!cJSON_IsNumber(array_item))
  197. break;
  198. /* TODO: if array_item->valuedouble > 127 or < -128 */
  199. array[i++] = (int8_t)array_item->valuedouble;
  200. }
  201. if (i > 0)
  202. attr_container_set_bytearray(&attr_cont, item->string, array,
  203. i);
  204. free(array);
  205. }
  206. }
  207. return attr_cont;
  208. fail:
  209. attr_container_destroy(attr_cont);
  210. return NULL;
  211. }
  212. int g_mid = 0;
  213. int
  214. gen_random_id()
  215. {
  216. static bool init = false;
  217. int r;
  218. if (!init) {
  219. srand(time(NULL));
  220. init = true;
  221. }
  222. r = rand();
  223. g_mid = r;
  224. return r;
  225. }
  226. char *
  227. read_file_to_buffer(const char *filename, int *ret_size)
  228. {
  229. char *buffer;
  230. int file;
  231. int file_size, read_size;
  232. struct stat stat_buf;
  233. if (!filename || !ret_size) {
  234. return NULL;
  235. }
  236. if ((file = open(filename, O_RDONLY, 0)) == -1) {
  237. return NULL;
  238. }
  239. if (fstat(file, &stat_buf) != 0) {
  240. close(file);
  241. return NULL;
  242. }
  243. file_size = stat_buf.st_size;
  244. if (!(buffer = malloc(file_size))) {
  245. close(file);
  246. return NULL;
  247. }
  248. read_size = read(file, buffer, file_size);
  249. close(file);
  250. if (read_size < file_size) {
  251. free(buffer);
  252. return NULL;
  253. }
  254. *ret_size = file_size;
  255. return buffer;
  256. }
  257. int
  258. wirte_buffer_to_file(const char *filename, const char *buffer, int size)
  259. {
  260. int file, ret;
  261. if ((file = open(filename, O_RDWR | O_CREAT | O_APPEND, 0644)) == -1)
  262. return -1;
  263. ret = write(file, buffer, size);
  264. close(file);
  265. return ret;
  266. }