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