• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "dslm_msg_serialize.h"
17 
18 #include <securec.h>
19 #include <stdbool.h>
20 #include <string.h>
21 
22 #include "utils_json.h"
23 #include "utils_log.h"
24 #include "utils_mem.h"
25 
IsAscii(const uint8_t ch)26 static inline bool IsAscii(const uint8_t ch)
27 {
28     return (((ch) & (~0x7f)) == 0);
29 }
30 
CheckMessage(const uint8_t * msg,uint32_t length)31 bool CheckMessage(const uint8_t *msg, uint32_t length)
32 {
33     // our msgs is a printable string
34     if (msg[length - 1] != '\0') {
35         return false;
36     }
37     for (uint32_t i = 0; i < length - 1; i++) {
38         if (!IsAscii(msg[i])) {
39             return false;
40         }
41     }
42     return true;
43 }
44 
ParseMessage(const MessageBuff * buff)45 MessagePacket *ParseMessage(const MessageBuff *buff)
46 {
47     if (!CheckMessage(buff->buff, buff->length)) {
48         SECURITY_LOG_DEBUG("ERR MSG");
49         return NULL;
50     }
51 
52     JsonHandle handle = CreateJson((const char *)buff->buff);
53     if (handle == NULL) {
54         SECURITY_LOG_DEBUG("ERR JSON MSG");
55         return NULL;
56     }
57 
58     char *payload = NULL;
59     MessagePacket *packet = NULL;
60     do {
61         int32_t msgType = GetJsonFieldInt(handle, FIELD_MESSAGE);
62         if (msgType < 0) {
63             break;
64         }
65         payload = ConvertJsonToString(GetJsonFieldJson(handle, FIELD_PAYLOAD));
66         if (payload == NULL) {
67             break;
68         }
69         packet = MALLOC(sizeof(MessagePacket));
70         if (packet == NULL) {
71             free(payload);
72             break;
73         }
74         packet->type = (uint32_t)msgType;
75         packet->payload = (uint8_t *)payload;
76         packet->length = strlen(payload) + 1; // for the end flag '\0'
77     } while (0);
78 
79     DestroyJson(handle);
80     return packet;
81 }
82 
SerializeMessage(const MessagePacket * packet)83 MessageBuff *SerializeMessage(const MessagePacket *packet)
84 {
85     if (!CheckMessage(packet->payload, packet->length)) {
86         SECURITY_LOG_DEBUG("ERR MSG");
87         return NULL;
88     }
89 
90     MessageBuff *out = MALLOC(sizeof(MessageBuff));
91     if (out == NULL) {
92         return NULL;
93     }
94     memset_s(out, sizeof(MessageBuff), 0, sizeof(MessageBuff));
95 
96     JsonHandle json = CreateJson(NULL);
97     if (json == NULL) {
98         FREE(out);
99         return NULL;
100     }
101 
102     AddFieldIntToJson(json, FIELD_MESSAGE, packet->type);
103     AddFieldStringToJson(json, FIELD_PAYLOAD, (const char *)packet->payload);
104 
105     out->buff = (uint8_t *)ConvertJsonToString(json);
106     if (out->buff == NULL) {
107         FREE(out);
108         DestroyJson(json);
109         return NULL;
110     }
111     out->length = strlen((char *)out->buff) + 1;
112     DestroyJson(json);
113     return out;
114 }
115 
FreeMessagePacket(MessagePacket * packet)116 void FreeMessagePacket(MessagePacket *packet)
117 {
118     if (packet == NULL) {
119         return;
120     }
121     if (packet->payload != NULL) {
122         FREE(packet->payload);
123         packet->payload = NULL;
124     }
125     FREE((void *)packet);
126 }
127 
FreeMessageBuff(MessageBuff * buff)128 void FreeMessageBuff(MessageBuff *buff)
129 {
130     if (buff == NULL) {
131         return;
132     }
133     if (buff->buff != NULL) {
134         FREE(buff->buff);
135         buff->buff = NULL;
136     }
137     FREE((void *)buff);
138 }
139