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