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 DATASHARE_COMMON_ITYPES_UTIL_H
17 #define DATASHARE_COMMON_ITYPES_UTIL_H
18
19 #include "parcel.h"
20 #include "datashare_predicates.h"
21 #include "datashare_values_bucket.h"
22
23
24 namespace OHOS::DataShare {
25 class ITypesUtils final {
26 public:
27 static bool Marshal(Parcel &data);
28 static bool Unmarshal(Parcel &data);
29
30 static bool Marshalling(const DataSharePredicates &predicates, Parcel &parcel);
31 static bool Unmarshalling(Parcel &parcel, DataSharePredicates &predicates);
32
33 static bool Marshalling(const DataShareValuesBucket &valuesBucket, Parcel &parcel);
34 static bool Unmarshalling(Parcel &parcel, DataShareValuesBucket &valuesBucket);
35
36 static bool Marshalling(const OperationItem &operationItem, Parcel &parcel);
37 static bool Unmarshalling(Parcel &parcel, OperationItem &operationItem);
38
39 static bool Marshalling(const DataSharePredicatesObject &predicatesObject, Parcel &parcel);
40 static bool Unmarshalling(Parcel &parcel, DataSharePredicatesObject &predicatesObject);
41
42 static bool Marshalling(const DataSharePredicatesObjects &predicatesObject, Parcel &parcel);
43 static bool Unmarshalling(Parcel &parcel, DataSharePredicatesObjects &predicatesObject);
44
45 static bool Marshalling(const DataShareValueObject &valueObject, Parcel &parcel);
46 static bool Unmarshalling(Parcel &parcel, DataShareValueObject &valueObject);
47
48 static bool Marshalling(const std::string &input, Parcel &data);
49 static bool Unmarshalling(Parcel &data, std::string &output);
50
51 template <typename T>
52 static bool Marshalling(const std::vector<T> ¶ms, Parcel &parcel);
53 template <typename T>
54 static bool Unmarshalling(Parcel &parcel, std::vector<T> ¶ms);
55
56 template<typename T, typename... Types>
57 static bool Marshal(Parcel &parcel, const T &first, const Types &...others);
58 template<typename T, typename... Types>
59 static bool Unmarshal(Parcel &parcel, T &first, Types &...others);
60 };
61 template<typename T, typename... Types>
Marshal(Parcel & parcel,const T & first,const Types &...others)62 bool ITypesUtils::Marshal(Parcel &parcel, const T &first, const Types &...others)
63 {
64 if (!Marshalling(first, parcel)) {
65 return false;
66 }
67 return Marshal(parcel, others...);
68 }
69
70 template<typename T, typename... Types>
Unmarshal(Parcel & parcel,T & first,Types &...others)71 bool ITypesUtils::Unmarshal(Parcel &parcel, T &first, Types &...others)
72 {
73 if (!Unmarshalling(parcel, first)) {
74 return false;
75 }
76 return Unmarshal(parcel, others...);
77 }
78
79 template <typename T>
Marshalling(const std::vector<T> & params,Parcel & parcel)80 bool ITypesUtils::Marshalling(const std::vector<T> ¶ms, Parcel &parcel)
81 {
82 if (!parcel.WriteInt32(params.size())) {
83 return false;
84 }
85 for (unsigned long i = 0; i < params.size(); i++) {
86 if (!Marshalling(params[i], parcel)) {
87 return false;
88 }
89 }
90 return true;
91 }
92
93 template <typename T>
Unmarshalling(Parcel & parcel,std::vector<T> & params)94 bool ITypesUtils::Unmarshalling(Parcel &parcel, std::vector<T> ¶ms)
95 {
96 size_t size = static_cast<size_t>(parcel.ReadInt32());
97 if (static_cast<int32_t>(size) < 0) {
98 return false;
99 }
100 if ((size > parcel.GetReadableBytes()) || (params.max_size() < size)) {
101 return false;
102 }
103 params.resize(static_cast<int32_t>(size));
104 for (size_t i = 0; i < size; i++) {
105 T param;
106 if (!Unmarshalling(parcel, param)) {
107 return false;
108 }
109 params[static_cast<int32_t>(i)] = param;
110 }
111 return true;
112 }
113 } // namespace OHOS::DataShare
114 #endif