1 /*
2  * Copyright (c) 2023 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 #include "sec_comp_click_event_parcel.h"
16 
17 #include "sec_comp_log.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace SecurityComponent {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClickEventParcel"};
26 }
27 
MarshallingPointEvent(Parcel & out) const28 bool SecCompClickEventParcel::MarshallingPointEvent(Parcel& out) const
29 {
30     if (!(out.WriteDouble(this->clickInfoParams_.point.touchX)) ||
31         !(out.WriteDouble(this->clickInfoParams_.point.touchY))) {
32         SC_LOG_ERROR(LABEL, "Write touch xy pointer fail");
33         return false;
34     }
35 
36     if (!(out.WriteUint64(this->clickInfoParams_.point.timestamp))) {
37         SC_LOG_ERROR(LABEL, "Write touch timestamp fail");
38         return false;
39     }
40     return true;
41 }
42 
MarshallingKeyEvent(Parcel & out) const43 bool SecCompClickEventParcel::MarshallingKeyEvent(Parcel& out) const
44 {
45     if (!(out.WriteUint64(this->clickInfoParams_.key.timestamp))) {
46         SC_LOG_ERROR(LABEL, "Write key timestamp fail");
47         return false;
48     }
49 
50     if (!(out.WriteInt32(this->clickInfoParams_.key.keyCode))) {
51         SC_LOG_ERROR(LABEL, "Write key code fail");
52         return false;
53     }
54     return true;
55 }
56 
Marshalling(Parcel & out) const57 bool SecCompClickEventParcel::Marshalling(Parcel& out) const
58 {
59     if (!(out.WriteInt32(static_cast<int32_t>(this->clickInfoParams_.type)))) {
60         SC_LOG_ERROR(LABEL, "Write click type fail");
61         return false;
62     }
63 
64     if (this->clickInfoParams_.type == ClickEventType::POINT_EVENT_TYPE) {
65         if (!MarshallingPointEvent(out)) {
66             return false;
67         }
68     } else if (this->clickInfoParams_.type == ClickEventType::KEY_EVENT_TYPE) {
69         if (!MarshallingKeyEvent(out)) {
70             return false;
71         }
72     } else {
73         SC_LOG_ERROR(LABEL, "click type %{public}d is error", this->clickInfoParams_.type);
74         return false;
75     }
76 
77     if (!(out.WriteUint32(this->clickInfoParams_.extraInfo.dataSize))) {
78         SC_LOG_ERROR(LABEL, "Write extraInfo dataSize fail");
79         return false;
80     }
81     if (this->clickInfoParams_.extraInfo.dataSize != 0 &&
82         !(out.WriteBuffer(this->clickInfoParams_.extraInfo.data, this->clickInfoParams_.extraInfo.dataSize))) {
83         SC_LOG_ERROR(LABEL, "Write click extraInfo data fail");
84         return false;
85     }
86 
87     return true;
88 }
89 
UnmarshallingPointEvent(Parcel & in,SecCompClickEvent & clickInfo)90 bool SecCompClickEventParcel::UnmarshallingPointEvent(Parcel& in, SecCompClickEvent& clickInfo)
91 {
92     if (!in.ReadDouble(clickInfo.point.touchX) || !in.ReadDouble(clickInfo.point.touchY)) {
93         SC_LOG_ERROR(LABEL, "Read touch xy porinter fail");
94         return false;
95     }
96 
97     if (!in.ReadUint64(clickInfo.point.timestamp)) {
98         SC_LOG_ERROR(LABEL, "Read timestamp fail");
99         return false;
100     }
101     return true;
102 }
103 
UnmarshallingKeyEvent(Parcel & in,SecCompClickEvent & clickInfo)104 bool SecCompClickEventParcel::UnmarshallingKeyEvent(Parcel& in, SecCompClickEvent& clickInfo)
105 {
106     if (!in.ReadUint64(clickInfo.key.timestamp)) {
107         SC_LOG_ERROR(LABEL, "Read timestamp fail");
108         return false;
109     }
110 
111     if (!in.ReadInt32(clickInfo.key.keyCode)) {
112         SC_LOG_ERROR(LABEL, "Read keyCode fail");
113         return false;
114     }
115     return true;
116 }
117 
Unmarshalling(Parcel & in)118 SecCompClickEventParcel* SecCompClickEventParcel::Unmarshalling(Parcel& in)
119 {
120     SecCompClickEventParcel* clickInfoParcel = new (std::nothrow) SecCompClickEventParcel();
121     if (clickInfoParcel == nullptr) {
122         return nullptr;
123     }
124 
125     SecCompClickEvent clickInfo;
126     int32_t type;
127     if (!in.ReadInt32(type)) {
128         SC_LOG_ERROR(LABEL, "Read click type fail");
129         delete clickInfoParcel;
130         return nullptr;
131     }
132     clickInfo.type = static_cast<ClickEventType>(type);
133 
134     if (clickInfo.type == ClickEventType::POINT_EVENT_TYPE) {
135         if (!UnmarshallingPointEvent(in, clickInfo)) {
136             delete clickInfoParcel;
137             return nullptr;
138         }
139     } else if (clickInfo.type == ClickEventType::KEY_EVENT_TYPE) {
140         if (!UnmarshallingKeyEvent(in, clickInfo)) {
141             delete clickInfoParcel;
142             return nullptr;
143         }
144     } else {
145         SC_LOG_ERROR(LABEL, "click type %{public}d is error", clickInfo.type);
146         delete clickInfoParcel;
147         return nullptr;
148     }
149 
150     if (!in.ReadUint32(clickInfo.extraInfo.dataSize)) {
151         SC_LOG_ERROR(LABEL, "Read extraInfo data size fail");
152         delete clickInfoParcel;
153         return nullptr;
154     }
155 
156     if (clickInfo.extraInfo.dataSize == 0) {
157         clickInfoParcel->clickInfoParams_ = clickInfo;
158         return clickInfoParcel;
159     } else if (clickInfo.extraInfo.dataSize > MAX_EXTRA_SIZE) {
160         SC_LOG_ERROR(LABEL, "Read extraInfo data size invalid");
161         delete clickInfoParcel;
162         return nullptr;
163     }
164 
165     clickInfo.extraInfo.data = const_cast<uint8_t*>(in.ReadBuffer(clickInfo.extraInfo.dataSize));
166     if (clickInfo.extraInfo.data == nullptr) {
167         SC_LOG_ERROR(LABEL, "Read extraInfo data failed");
168         delete clickInfoParcel;
169         return nullptr;
170     }
171 
172     clickInfoParcel->clickInfoParams_ = clickInfo;
173     return clickInfoParcel;
174 }
175 }  // namespace SecurityComponent
176 }  // namespace Security
177 }  // namespace OHOS
178