host_tool_utils.c 9.0 KB

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