• 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 "event_status_manager.h"
25 #include "global.h"
26 #include "input_client_info.h"
27 #include "input_window_info.h"
28 #include "message_parcel.h"
29 #include "panel_info.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 InputWindowInfo &input, MessageParcel &data);
78     static bool Unmarshalling(InputWindowInfo &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(EventType input, MessageParcel &data);
87     static bool Unmarshalling(EventType &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     template<class T>
105     static bool Marshalling(const std::vector<T> &val, MessageParcel &parcel);
106     template<class T>
107     static bool Unmarshalling(std::vector<T> &val, MessageParcel &parcel);
108 
109     template<class K, class V>
110     static bool Marshalling(const std::map<K, V> &val, MessageParcel &parcel);
111     template<class K, class V>
112     static bool Unmarshalling(std::map<K, V> &val, MessageParcel &parcel);
113 
114     template<typename T, typename... Types>
115     static bool Marshal(MessageParcel &parcel, const T &first, const Types &... others);
116     template<typename T, typename... Types>
117     static bool Unmarshal(MessageParcel &parcel, T &first, Types &... others);
118 };
119 
120 template<class T>
Marshalling(const std::vector<T> & val,MessageParcel & parcel)121 bool ITypesUtil::Marshalling(const std::vector<T> &val, MessageParcel &parcel)
122 {
123     if (val.size() > INT_MAX) {
124         return false;
125     }
126 
127     if (!parcel.WriteInt32(static_cast<int32_t>(val.size()))) {
128         return false;
129     }
130 
131     for (auto &v : val) {
132         if (!Marshalling(v, parcel)) {
133             return false;
134         }
135     }
136     return true;
137 }
138 
139 template<class T>
Unmarshalling(std::vector<T> & val,MessageParcel & parcel)140 bool ITypesUtil::Unmarshalling(std::vector<T> &val, MessageParcel &parcel)
141 {
142     int32_t len = parcel.ReadInt32();
143     if (len < 0) {
144         return false;
145     }
146 
147     size_t readAbleSize = parcel.GetReadableBytes();
148     size_t size = static_cast<size_t>(len);
149     if ((size > readAbleSize) || (size > val.max_size())) {
150         return false;
151     }
152 
153     val.resize(size);
154     if (val.size() < size) {
155         return false;
156     }
157 
158     for (auto &v : val) {
159         if (!Unmarshalling(v, parcel)) {
160             return false;
161         }
162     }
163 
164     return true;
165 }
166 
167 template<typename T, typename... Types>
Marshal(MessageParcel & parcel,const T & first,const Types &...others)168 bool ITypesUtil::Marshal(MessageParcel &parcel, const T &first, const Types &... others)
169 {
170     if (!Marshalling(first, parcel)) {
171         return false;
172     }
173     return Marshal(parcel, others...);
174 }
175 
176 template<typename T, typename... Types>
Unmarshal(MessageParcel & parcel,T & first,Types &...others)177 bool ITypesUtil::Unmarshal(MessageParcel &parcel, T &first, Types &... others)
178 {
179     if (!Unmarshalling(first, parcel)) {
180         return false;
181     }
182     return Unmarshal(parcel, others...);
183 }
184 
185 template<class K, class V>
Marshalling(const std::map<K,V> & result,MessageParcel & parcel)186 bool ITypesUtil::Marshalling(const std::map<K, V> &result, MessageParcel &parcel)
187 {
188     if (!parcel.WriteInt32(static_cast<int32_t>(result.size()))) {
189         return false;
190     }
191     for (const auto &entry : result) {
192         if (!Marshalling(entry.first, parcel)) {
193             return false;
194         }
195         if (!Marshalling(entry.second, parcel)) {
196             return false;
197         }
198     }
199     return true;
200 }
201 
202 template<class K, class V>
Unmarshalling(std::map<K,V> & val,MessageParcel & parcel)203 bool ITypesUtil::Unmarshalling(std::map<K, V> &val, MessageParcel &parcel)
204 {
205     int32_t size = 0;
206     if (!parcel.ReadInt32(size)) {
207         return false;
208     }
209     if (size < 0) {
210         return false;
211     }
212 
213     size_t readAbleSize = parcel.GetReadableBytes();
214     size_t len = static_cast<size_t>(size);
215     if ((len > readAbleSize) || len > val.max_size()) {
216         return false;
217     }
218 
219     for (int32_t i = 0; i < size; i++) {
220         K key;
221         if (!Unmarshalling(key, parcel)) {
222             return false;
223         }
224         V value;
225         if (!Unmarshalling(value, parcel)) {
226             return false;
227         }
228         val.insert({ key, value });
229     }
230     return true;
231 }
232 } // namespace MiscServices
233 } // namespace OHOS
234 #endif
235