• 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 #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     template<class T>
111     static bool Marshalling(const std::vector<T> &val, MessageParcel &parcel);
112     template<class T>
113     static bool Unmarshalling(std::vector<T> &val, MessageParcel &parcel);
114 
115     template<class K, class V>
116     static bool Marshalling(const std::map<K, V> &val, MessageParcel &parcel);
117     template<class K, class V>
118     static bool Unmarshalling(std::map<K, V> &val, MessageParcel &parcel);
119 
120     template<class K, class V>
121     static bool Marshalling(const std::unordered_map<K, V> &val, MessageParcel &parcel);
122     template<class K, class V>
123     static bool Unmarshalling(std::unordered_map<K, V> &val, MessageParcel &parcel);
124 
125     template<typename T, typename... Types>
126     static bool Marshal(MessageParcel &parcel, const T &first, const Types &... others);
127     template<typename T, typename... Types>
128     static bool Unmarshal(MessageParcel &parcel, T &first, Types &... others);
129 };
130 
131 template<class T>
Marshalling(const std::vector<T> & val,MessageParcel & parcel)132 bool ITypesUtil::Marshalling(const std::vector<T> &val, MessageParcel &parcel)
133 {
134     if (val.size() > INT_MAX) {
135         return false;
136     }
137 
138     if (!parcel.WriteInt32(static_cast<int32_t>(val.size()))) {
139         return false;
140     }
141 
142     for (auto &v : val) {
143         if (!Marshalling(v, parcel)) {
144             return false;
145         }
146     }
147     return true;
148 }
149 
150 template<class T>
Unmarshalling(std::vector<T> & val,MessageParcel & parcel)151 bool ITypesUtil::Unmarshalling(std::vector<T> &val, MessageParcel &parcel)
152 {
153     int32_t len = parcel.ReadInt32();
154     if (len < 0) {
155         return false;
156     }
157 
158     size_t readAbleSize = parcel.GetReadableBytes();
159     size_t size = static_cast<size_t>(len);
160     if ((size > readAbleSize) || (size > val.max_size())) {
161         return false;
162     }
163 
164     val.resize(size);
165     if (val.size() < size) {
166         return false;
167     }
168 
169     for (auto &v : val) {
170         if (!Unmarshalling(v, parcel)) {
171             return false;
172         }
173     }
174 
175     return true;
176 }
177 
178 template<typename T, typename... Types>
Marshal(MessageParcel & parcel,const T & first,const Types &...others)179 bool ITypesUtil::Marshal(MessageParcel &parcel, const T &first, const Types &... others)
180 {
181     if (!Marshalling(first, parcel)) {
182         return false;
183     }
184     return Marshal(parcel, others...);
185 }
186 
187 template<typename T, typename... Types>
Unmarshal(MessageParcel & parcel,T & first,Types &...others)188 bool ITypesUtil::Unmarshal(MessageParcel &parcel, T &first, Types &... others)
189 {
190     if (!Unmarshalling(first, parcel)) {
191         return false;
192     }
193     return Unmarshal(parcel, others...);
194 }
195 
196 template<class K, class V>
Marshalling(const std::map<K,V> & result,MessageParcel & parcel)197 bool ITypesUtil::Marshalling(const std::map<K, V> &result, MessageParcel &parcel)
198 {
199     if (!parcel.WriteInt32(static_cast<int32_t>(result.size()))) {
200         return false;
201     }
202     for (const auto &entry : result) {
203         if (!Marshalling(entry.first, parcel)) {
204             return false;
205         }
206         if (!Marshalling(entry.second, parcel)) {
207             return false;
208         }
209     }
210     return true;
211 }
212 
213 template<class K, class V>
Unmarshalling(std::map<K,V> & val,MessageParcel & parcel)214 bool ITypesUtil::Unmarshalling(std::map<K, V> &val, MessageParcel &parcel)
215 {
216     int32_t size = 0;
217     if (!parcel.ReadInt32(size)) {
218         return false;
219     }
220     if (size < 0) {
221         return false;
222     }
223 
224     size_t readAbleSize = parcel.GetReadableBytes();
225     size_t len = static_cast<size_t>(size);
226     if ((len > readAbleSize) || len > val.max_size()) {
227         return false;
228     }
229 
230     for (int32_t i = 0; i < size; i++) {
231         K key;
232         if (!Unmarshalling(key, parcel)) {
233             return false;
234         }
235         V value;
236         if (!Unmarshalling(value, parcel)) {
237             return false;
238         }
239         val.insert({ key, value });
240     }
241     return true;
242 }
243 
244 template<class K, class V>
Marshalling(const std::unordered_map<K,V> & result,MessageParcel & parcel)245 bool ITypesUtil::Marshalling(const std::unordered_map<K, V> &result, MessageParcel &parcel)
246 {
247     if (!parcel.WriteInt32(static_cast<int32_t>(result.size()))) {
248         return false;
249     }
250     for (const auto &entry : result) {
251         if (!Marshalling(entry.first, parcel)) {
252             return false;
253         }
254         if (!Marshalling(entry.second, parcel)) {
255             return false;
256         }
257     }
258     return true;
259 }
260 
261 template<class K, class V>
Unmarshalling(std::unordered_map<K,V> & val,MessageParcel & parcel)262 bool ITypesUtil::Unmarshalling(std::unordered_map<K, V> &val, MessageParcel &parcel)
263 {
264     int32_t size = 0;
265     if (!parcel.ReadInt32(size)) {
266         return false;
267     }
268     if (size < 0) {
269         return false;
270     }
271 
272     size_t readAbleSize = parcel.GetReadableBytes();
273     size_t len = static_cast<size_t>(size);
274     if ((len > readAbleSize) || len > val.max_size()) {
275         return false;
276     }
277 
278     for (int32_t i = 0; i < size; i++) {
279         K key;
280         if (!Unmarshalling(key, parcel)) {
281             return false;
282         }
283         V value;
284         if (!Unmarshalling(value, parcel)) {
285             return false;
286         }
287         val.insert({ key, value });
288     }
289     return true;
290 }
291 } // namespace MiscServices
292 } // namespace OHOS
293 #endif
294