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