summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/test-tools/host-tool/src/host_tool_utils.c
blob: 9ea3d6ca98d815f078a004ca338a36dcf141a991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "host_tool_utils.h"
#include "bi-inc/shared_utils.h"
#include "bh_platform.h"

#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

typedef union jvalue {
    bool z;
    int8_t i8;
    uint8_t u8;
    int16_t i16;
    uint16_t u16;
    int32_t i32;
    uint32_t u32;
    int64_t i64;
    uint64_t u64;
    float f;
    double d;
} jvalue;

static inline int16_t
get_int16(const char *buf)
{
    int16_t ret;
    bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t));
    return ret;
}

static inline uint16_t
get_uint16(const char *buf)
{
    return get_int16(buf);
}

static inline int32_t
get_int32(const char *buf)
{
    int32_t ret;
    bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t));
    return ret;
}

static inline uint32_t
get_uint32(const char *buf)
{
    return get_int32(buf);
}

char *
attr_container_get_attr_begin(const attr_container_t *attr_cont,
                              uint32_t *p_total_length, uint16_t *p_attr_num);

cJSON *
attr2json(const attr_container_t *attr_cont)
{
    uint32_t total_length;
    uint16_t attr_num, i, j, type;
    const char *p, *tag, *key;
    jvalue value;
    cJSON *root;

    if (!attr_cont)
        return NULL;

    root = cJSON_CreateObject();
    if (!root)
        return NULL;

    /* TODO: how to convert the tag? */
    tag = attr_container_get_tag(attr_cont);
    if (!tag)
        goto fail;

    p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num);
    if (!p)
        goto fail;

    for (i = 0; i < attr_num; i++) {
        cJSON *obj;

        key = p + 2;
        /* Skip key len and key */
        p += 2 + get_uint16(p);
        type = *p++;

        switch (type) {
            case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */
                bh_memcpy_s(&value.i8, 1, p, 1);
                if (NULL == (obj = cJSON_CreateNumber(value.i8)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p++;
                break;
            case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */
                bh_memcpy_s(&value.i16, sizeof(int16_t), p, sizeof(int16_t));
                if (NULL == (obj = cJSON_CreateNumber(value.i16)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                /* another approach: cJSON_AddNumberToObject(root, key, value.s)
                 */
                p += 2;
                break;
            case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */
                bh_memcpy_s(&value.i32, sizeof(int32_t), p, sizeof(int32_t));
                if (NULL == (obj = cJSON_CreateNumber(value.i32)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 4;
                break;
            case ATTR_TYPE_INT64:
                bh_memcpy_s(&value.i64, sizeof(int64_t), p, sizeof(int64_t));
                if (NULL == (obj = cJSON_CreateNumber(value.i64)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 8;
                break;
            case ATTR_TYPE_UINT8:
                bh_memcpy_s(&value.u8, 1, p, 1);
                if (NULL == (obj = cJSON_CreateNumber(value.u8)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p++;
                break;
            case ATTR_TYPE_UINT16:
                bh_memcpy_s(&value.u16, sizeof(uint16_t), p, sizeof(uint16_t));
                if (NULL == (obj = cJSON_CreateNumber(value.u16)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 2;
                break;
            case ATTR_TYPE_UINT32:
                bh_memcpy_s(&value.u32, sizeof(uint32_t), p, sizeof(uint32_t));
                if (NULL == (obj = cJSON_CreateNumber(value.u32)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 4;
                break;
            case ATTR_TYPE_UINT64:
                bh_memcpy_s(&value.u64, sizeof(uint64_t), p, sizeof(uint64_t));
                if (NULL == (obj = cJSON_CreateNumber(value.u64)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 8;
                break;
            case ATTR_TYPE_FLOAT:
                bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
                if (NULL == (obj = cJSON_CreateNumber(value.f)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 4;
                break;
            case ATTR_TYPE_DOUBLE:
                bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
                if (NULL == (obj = cJSON_CreateNumber(value.d)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += 8;
                break;
            case ATTR_TYPE_BOOLEAN:
                bh_memcpy_s(&value.z, 1, p, 1);
                if (NULL == (obj = cJSON_CreateBool(value.z)))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p++;
                break;
            case ATTR_TYPE_STRING:
                if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t))))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                p += sizeof(uint16_t) + get_uint16(p);
                break;
            case ATTR_TYPE_BYTEARRAY:
                if (NULL == (obj = cJSON_CreateArray()))
                    goto fail;
                cJSON_AddItemToObject(root, key, obj);
                for (j = 0; j < get_uint32(p); j++) {
                    cJSON *item =
                        cJSON_CreateNumber(*(p + sizeof(uint32_t) + j));
                    if (item == NULL)
                        goto fail;
                    cJSON_AddItemToArray(obj, item);
                }
                p += sizeof(uint32_t) + get_uint32(p);
                break;
        }
    }

    return root;

fail:
    cJSON_Delete(root);
    return NULL;
}

attr_container_t *
json2attr(const cJSON *json_obj)
{
    attr_container_t *attr_cont;
    cJSON *item;

    if (NULL == (attr_cont = attr_container_create("")))
        return NULL;

    if (!cJSON_IsObject(json_obj))
        goto fail;

    cJSON_ArrayForEach(item, json_obj)
    {

        if (cJSON_IsNumber(item)) {
            attr_container_set_double(&attr_cont, item->string,
                                      item->valuedouble);
        }
        else if (cJSON_IsTrue(item)) {
            attr_container_set_bool(&attr_cont, item->string, true);
        }
        else if (cJSON_IsFalse(item)) {
            attr_container_set_bool(&attr_cont, item->string, false);
        }
        else if (cJSON_IsString(item)) {
            attr_container_set_string(&attr_cont, item->string,
                                      item->valuestring);
        }
        else if (cJSON_IsArray(item)) {
            int8_t *array;
            int i = 0, len = sizeof(int8_t) * cJSON_GetArraySize(item);
            cJSON *array_item;

            if (0 == len || NULL == (array = (int8_t *)malloc(len)))
                goto fail;
            memset(array, 0, len);

            cJSON_ArrayForEach(array_item, item)
            {
                /* must be number array */
                if (!cJSON_IsNumber(array_item))
                    break;
                /* TODO: if array_item->valuedouble > 127 or < -128 */
                array[i++] = (int8_t)array_item->valuedouble;
            }
            if (i > 0)
                attr_container_set_bytearray(&attr_cont, item->string, array,
                                             i);
            free(array);
        }
    }

    return attr_cont;

fail:
    attr_container_destroy(attr_cont);
    return NULL;
}

int g_mid = 0;

int
gen_random_id()
{
    static bool init = false;
    int r;

    if (!init) {
        srand(time(NULL));
        init = true;
    }

    r = rand();
    g_mid = r;

    return r;
}

char *
read_file_to_buffer(const char *filename, int *ret_size)
{
    char *buffer;
    int file;
    int file_size, read_size;
    struct stat stat_buf;

    if (!filename || !ret_size) {
        return NULL;
    }

    if ((file = open(filename, O_RDONLY, 0)) == -1) {
        return NULL;
    }

    if (fstat(file, &stat_buf) != 0) {
        close(file);
        return NULL;
    }

    file_size = stat_buf.st_size;

    if (!(buffer = malloc(file_size))) {
        close(file);
        return NULL;
    }

    read_size = read(file, buffer, file_size);
    close(file);

    if (read_size < file_size) {
        free(buffer);
        return NULL;
    }

    *ret_size = file_size;
    return buffer;
}

int
wirte_buffer_to_file(const char *filename, const char *buffer, int size)
{
    int file, ret;

    if ((file = open(filename, O_RDWR | O_CREAT | O_APPEND, 0644)) == -1)
        return -1;

    ret = write(file, buffer, size);

    close(file);

    return ret;
}