1 /*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "parsedata.h"
17 #include "securec.h"
18 #include "mem_stat.h"
19 #include "jsonutil.h"
20 #include "log.h"
21
parse_header(const char * data)22 uint32_t parse_header(const char *data)
23 {
24 int32_t ret = 0;
25 json_handle obj = NULL;
26 obj = parse_json(data);
27 if (obj == NULL) {
28 return -1;
29 }
30 ret = get_json_int(obj, FIELD_MESSAGE);
31 free_json(obj);
32 return ret;
33 }
34
parse_data(const char * data)35 struct pass_through_data *parse_data(const char *data)
36 {
37 const char *payload = NULL;
38 struct pass_through_data *msg_data = (struct pass_through_data *)MALLOC(sizeof(struct pass_through_data));
39 if (msg_data == NULL) {
40 return NULL;
41 }
42 json_handle obj = parse_json(data);
43 if (obj == NULL) {
44 LOGE("Passthrough Data parse_json failed");
45 goto error;
46 }
47 int32_t message_code = get_json_int(obj, FIELD_MESSAGE);
48 if (message_code == -1) {
49 LOGE("Passthrough Data failed, field is null in message");
50 goto error;
51 }
52 json_pobject obj_value = get_json_obj(obj, FIELD_PAYLOAD);
53 payload = json_to_string(obj_value);
54 if (payload == NULL) {
55 LOGE("Passthrough Data failed, field is null in payload");
56 goto error;
57 }
58 (void)memset_s(msg_data, sizeof(*msg_data), 0, sizeof(*msg_data));
59 msg_data->message_code = (uint32_t)message_code;
60 int32_t len = strlen(payload);
61 if (len > 0) {
62 ++len; /* add terminator */
63 char *tmp_data = (char *)MALLOC(len);
64 if (tmp_data == NULL) {
65 goto error;
66 }
67 (void)memset_s(tmp_data, len, 0, len);
68 (void)memcpy_s(tmp_data, len, payload, len);
69 msg_data->payload_data = tmp_data;
70 }
71 FREE((char *)payload);
72 free_json(obj);
73 return msg_data;
74 error:
75 if (payload != NULL) {
76 FREE((char *)payload);
77 }
78 free_json(obj);
79 FREE(msg_data);
80 return NULL;
81 }
82
free_data(struct pass_through_data * data)83 void free_data(struct pass_through_data *data)
84 {
85 if (data != NULL) {
86 if (data->payload_data != NULL) {
87 FREE(data->payload_data);
88 }
89 FREE(data);
90 }
91 }
92
parse_payload(const char * payload,enum json_object_data_type data_type)93 void *parse_payload(const char *payload, enum json_object_data_type data_type)
94 {
95 if (data_type == JSON_STRING_DATA) {
96 return parse_json(payload);
97 } else if (data_type == JSON_OBJECT_DATA) {
98 return (void *)payload;
99 } else {
100 return NULL;
101 }
102 }
103
free_payload(char * data,enum json_object_data_type data_type)104 void free_payload(char *data, enum json_object_data_type data_type)
105 {
106 if (data_type == JSON_STRING_DATA) {
107 free_json(data);
108 } else {
109 return;
110 }
111 }
112