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