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 #ifndef FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_ITYPES_UTIL_H
17 #define FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_ITYPES_UTIL_H
18
19 #include <climits>
20 #include <map>
21 #include <memory>
22
23 #include "element_name.h"
24 #include "global.h"
25 #include "input_client_info.h"
26 #include "input_window_info.h"
27 #include "message_parcel.h"
28 #include "panel_info.h"
29 #include "sys_panel_status.h"
30
31 namespace OHOS {
32 namespace MiscServices {
33 class ITypesUtil final {
34 public:
35 static bool Marshal(MessageParcel &data);
36 static bool Unmarshal(MessageParcel &data);
37
38 static bool Marshalling(bool input, MessageParcel &data);
39 static bool Unmarshalling(bool &output, MessageParcel &data);
40
41 static bool Marshalling(uint32_t input, MessageParcel &data);
42 static bool Unmarshalling(uint32_t &output, MessageParcel &data);
43
44 static bool Marshalling(int32_t input, MessageParcel &data);
45 static bool Unmarshalling(int32_t &output, MessageParcel &data);
46
47 static bool Marshalling(uint64_t input, MessageParcel &data);
48 static bool Unmarshalling(uint64_t &output, MessageParcel &data);
49
50 static bool Marshalling(double input, MessageParcel &data);
51 static bool Unmarshalling(double &output, MessageParcel &data);
52
53 static bool Marshalling(const std::u16string &input, MessageParcel &data);
54 static bool Unmarshalling(std::u16string &output, MessageParcel &data);
55
56 static bool Marshalling(const std::string &input, MessageParcel &data);
57 static bool Unmarshalling(std::string &output, MessageParcel &data);
58
59 static bool Marshalling(const std::vector<uint8_t> &input, MessageParcel &data);
60 static bool Unmarshalling(std::vector<uint8_t> &output, MessageParcel &data);
61
62 static bool Marshalling(const sptr<IRemoteObject> &input, MessageParcel &data);
63 static bool Unmarshalling(sptr<IRemoteObject> &output, MessageParcel &data);
64
65 static bool Marshalling(const Property &input, MessageParcel &data);
66 static bool Unmarshalling(Property &output, MessageParcel &data);
67
68 static bool Marshalling(const SubProperty &input, MessageParcel &data);
69 static bool Unmarshalling(SubProperty &output, MessageParcel &data);
70
71 static bool Marshalling(const InputAttribute &input, MessageParcel &data);
72 static bool Unmarshalling(InputAttribute &output, MessageParcel &data);
73
74 static bool Marshalling(const InputClientInfo &input, MessageParcel &data);
75 static bool Unmarshalling(InputClientInfo &output, MessageParcel &data);
76
77 static bool Marshalling(const ImeWindowInfo &input, MessageParcel &data);
78 static bool Unmarshalling(ImeWindowInfo &output, MessageParcel &data);
79
80 static bool Marshalling(const TextTotalConfig &input, MessageParcel &data);
81 static bool Unmarshalling(TextTotalConfig &output, MessageParcel &data);
82
83 static bool Marshalling(const PanelStatusInfo &info, MessageParcel &data);
84 static bool Unmarshalling(PanelStatusInfo &info, MessageParcel &data);
85
86 static bool Marshalling(const SysPanelStatus &input, MessageParcel &data);
87 static bool Unmarshalling(SysPanelStatus &output, MessageParcel &data);
88
89 static bool Marshalling(InputType input, MessageParcel &data);
90 static bool Unmarshalling(InputType &output, MessageParcel &data);
91
92 static bool Marshalling(const OHOS::AppExecFwk::ElementName &input, MessageParcel &data);
93 static bool Unmarshalling(OHOS::AppExecFwk::ElementName &output, MessageParcel &data);
94
95 static bool Marshalling(const PanelInfo &input, MessageParcel &data);
96 static bool Unmarshalling(PanelInfo &output, MessageParcel &data);
97
98 static bool Marshalling(ClientState input, MessageParcel &data);
99 static bool Unmarshalling(ClientState &output, MessageParcel &data);
100
101 static bool Marshalling(SwitchTrigger input, MessageParcel &data);
102 static bool Unmarshalling(SwitchTrigger &output, MessageParcel &data);
103
104 static bool Marshalling(const PrivateDataValue &input, MessageParcel &data);
105 static bool Unmarshalling(PrivateDataValue &output, MessageParcel &data);
106
107 static bool Marshalling(const Range &input, MessageParcel &data);
108 static bool Unmarshalling(Range &output, MessageParcel &data);
109
110 static bool Marshalling(const ArrayBuffer &input, MessageParcel &data);
111 static bool Unmarshalling(ArrayBuffer &output, MessageParcel &data);
112
113 static bool Marshalling(RequestKeyboardReason input, MessageParcel &data);
114 static bool Unmarshalling(RequestKeyboardReason &output, MessageParcel &data);
115
116 template<class T>
117 static bool Marshalling(const std::vector<T> &val, MessageParcel &parcel);
118 template<class T>
119 static bool Unmarshalling(std::vector<T> &val, MessageParcel &parcel);
120
121 template<class K, class V>
122 static bool Marshalling(const std::map<K, V> &val, MessageParcel &parcel);
123 template<class K, class V>
124 static bool Unmarshalling(std::map<K, V> &val, MessageParcel &parcel);
125
126 template<class K, class V>
127 static bool Marshalling(const std::unordered_map<K, V> &val, MessageParcel &parcel);
128 template<class K, class V>
129 static bool Unmarshalling(std::unordered_map<K, V> &val, MessageParcel &parcel);
130
131 template<typename T, typename... Types>
132 static bool Marshal(MessageParcel &parcel, const T &first, const Types &... others);
133 template<typename T, typename... Types>
134 static bool Unmarshal(MessageParcel &parcel, T &first, Types &... others);
135 };
136
137 template<class T>
Marshalling(const std::vector<T> & val,MessageParcel & parcel)138 bool ITypesUtil::Marshalling(const std::vector<T> &val, MessageParcel &parcel)
139 {
140 if (val.size() > INT_MAX) {
141 return false;
142 }
143
144 if (!parcel.WriteInt32(static_cast<int32_t>(val.size()))) {
145 return false;
146 }
147
148 for (auto &v : val) {
149 if (!Marshalling(v, parcel)) {
150 return false;
151 }
152 }
153 return true;
154 }
155
156 template<class T>
Unmarshalling(std::vector<T> & val,MessageParcel & parcel)157 bool ITypesUtil::Unmarshalling(std::vector<T> &val, MessageParcel &parcel)
158 {
159 int32_t len = parcel.ReadInt32();
160 if (len < 0) {
161 return false;
162 }
163
164 size_t readAbleSize = parcel.GetReadableBytes();
165 size_t size = static_cast<size_t>(len);
166 if ((size > readAbleSize) || (size > val.max_size())) {
167 return false;
168 }
169
170 val.resize(size);
171 if (val.size() < size) {
172 return false;
173 }
174
175 for (auto &v : val) {
176 if (!Unmarshalling(v, parcel)) {
177 return false;
178 }
179 }
180
181 return true;
182 }
183
184 template<typename T, typename... Types>
Marshal(MessageParcel & parcel,const T & first,const Types &...others)185 bool ITypesUtil::Marshal(MessageParcel &parcel, const T &first, const Types &... others)
186 {
187 if (!Marshalling(first, parcel)) {
188 return false;
189 }
190 return Marshal(parcel, others...);
191 }
192
193 template<typename T, typename... Types>
Unmarshal(MessageParcel & parcel,T & first,Types &...others)194 bool ITypesUtil::Unmarshal(MessageParcel &parcel, T &first, Types &... others)
195 {
196 if (!Unmarshalling(first, parcel)) {
197 return false;
198 }
199 return Unmarshal(parcel, others...);
200 }
201
202 template<class K, class V>
Marshalling(const std::map<K,V> & result,MessageParcel & parcel)203 bool ITypesUtil::Marshalling(const std::map<K, V> &result, MessageParcel &parcel)
204 {
205 if (!parcel.WriteInt32(static_cast<int32_t>(result.size()))) {
206 return false;
207 }
208 for (const auto &entry : result) {
209 if (!Marshalling(entry.first, parcel)) {
210 return false;
211 }
212 if (!Marshalling(entry.second, parcel)) {
213 return false;
214 }
215 }
216 return true;
217 }
218
219 template<class K, class V>
Unmarshalling(std::map<K,V> & val,MessageParcel & parcel)220 bool ITypesUtil::Unmarshalling(std::map<K, V> &val, MessageParcel &parcel)
221 {
222 int32_t size = 0;
223 if (!parcel.ReadInt32(size)) {
224 return false;
225 }
226 if (size < 0) {
227 return false;
228 }
229
230 size_t readAbleSize = parcel.GetReadableBytes();
231 size_t len = static_cast<size_t>(size);
232 if ((len > readAbleSize) || len > val.max_size()) {
233 return false;
234 }
235
236 for (int32_t i = 0; i < size; i++) {
237 K key;
238 if (!Unmarshalling(key, parcel)) {
239 return false;
240 }
241 V value;
242 if (!Unmarshalling(value, parcel)) {
243 return false;
244 }
245 val.insert({ key, value });
246 }
247 return true;
248 }
249
250 template<class K, class V>
Marshalling(const std::unordered_map<K,V> & result,MessageParcel & parcel)251 bool ITypesUtil::Marshalling(const std::unordered_map<K, V> &result, MessageParcel &parcel)
252 {
253 if (!parcel.WriteInt32(static_cast<int32_t>(result.size()))) {
254 return false;
255 }
256 for (const auto &entry : result) {
257 if (!Marshalling(entry.first, parcel)) {
258 return false;
259 }
260 if (!Marshalling(entry.second, parcel)) {
261 return false;
262 }
263 }
264 return true;
265 }
266
267 template<class K, class V>
Unmarshalling(std::unordered_map<K,V> & val,MessageParcel & parcel)268 bool ITypesUtil::Unmarshalling(std::unordered_map<K, V> &val, MessageParcel &parcel)
269 {
270 int32_t size = 0;
271 if (!parcel.ReadInt32(size)) {
272 return false;
273 }
274 if (size < 0) {
275 return false;
276 }
277
278 size_t readAbleSize = parcel.GetReadableBytes();
279 size_t len = static_cast<size_t>(size);
280 if ((len > readAbleSize) || len > val.max_size()) {
281 return false;
282 }
283
284 for (int32_t i = 0; i < size; i++) {
285 K key;
286 if (!Unmarshalling(key, parcel)) {
287 return false;
288 }
289 V value;
290 if (!Unmarshalling(value, parcel)) {
291 return false;
292 }
293 val.insert({ key, value });
294 }
295 return true;
296 }
297 } // namespace MiscServices
298 } // namespace OHOS
299 #endif
300