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 FOUNDATION_MARSHALLING_HELPER_H
17 #define FOUNDATION_MARSHALLING_HELPER_H
18 
19 #include <parcel.h>
20 
21 namespace OHOS::Rosen {
22 class MarshallingHelper : public Parcelable {
23 public:
24     MarshallingHelper() = delete;
25     template<class T>
MarshallingVectorParcelableObj(Parcel & parcel,const std::vector<sptr<T>> & data)26     static bool MarshallingVectorParcelableObj(Parcel& parcel, const std::vector<sptr<T>>& data)
27     {
28         if (data.size() > INT_MAX) {
29             return false;
30         }
31         if (!parcel.WriteInt32(static_cast<int32_t>(data.size()))) {
32             return false;
33         }
34         for (const auto &v : data) {
35             if (!parcel.WriteParcelable(v)) {
36                 return false;
37             }
38         }
39         return true;
40     }
41 
42     template<class T>
UnmarshallingVectorParcelableObj(Parcel & parcel,std::vector<sptr<T>> & data)43     static bool UnmarshallingVectorParcelableObj(Parcel& parcel, std::vector<sptr<T>>& data)
44     {
45         int32_t len = parcel.ReadInt32();
46         if (len < 0) {
47             return false;
48         }
49 
50         size_t readAbleSize = parcel.GetReadableBytes();
51         size_t size = static_cast<size_t>(len);
52         if ((size > readAbleSize) || (size > data.max_size())) {
53             return false;
54         }
55         data.resize(size);
56         if (data.size() < size) {
57             return false;
58         }
59         size_t minDesireCapacity = sizeof(int32_t);
60         for (size_t i = 0; i < size; i++) {
61             readAbleSize = parcel.GetReadableBytes();
62             if (minDesireCapacity > readAbleSize) {
63                 return false;
64             }
65             data[i] = parcel.ReadParcelable<T>();
66             if (data[i] == nullptr) {
67                 return false;
68             }
69         }
70         return true;
71     }
72 
73     template<class T>
MarshallingVectorObj(Parcel & parcel,const std::vector<T> & data,std::function<bool (Parcel &,const T &)> func)74     static bool MarshallingVectorObj(Parcel& parcel, const std::vector<T>& data,
75         std::function<bool(Parcel&, const T&)> func)
76     {
77         if (data.size() > INT_MAX) {
78             return false;
79         }
80         if (func == nullptr) {
81             return false;
82         }
83         if (!parcel.WriteInt32(static_cast<int32_t>(data.size()))) {
84             return false;
85         }
86         for (const auto &v : data) {
87             if (!func(parcel, v)) {
88                 return false;
89             }
90         }
91         return true;
92     }
93 
94     template<class T>
UnmarshallingVectorObj(Parcel & parcel,std::vector<T> & data,std::function<bool (Parcel &,T &)> func)95     static bool UnmarshallingVectorObj(Parcel& parcel, std::vector<T>& data, std::function<bool(Parcel&, T&)> func)
96     {
97         if (func == nullptr) {
98             return false;
99         }
100         int32_t len = parcel.ReadInt32();
101         if (len < 0) {
102             return false;
103         }
104 
105         size_t readAbleSize = parcel.GetReadableBytes();
106         size_t size = static_cast<size_t>(len);
107         if ((size > readAbleSize) || (size > data.max_size())) {
108             return false;
109         }
110         data.resize(size);
111         if (data.size() < size) {
112             return false;
113         }
114 
115         for (size_t i = 0; i < size; i++) {
116             if (!func(parcel, data[i])) {
117                 return false;
118             }
119         }
120         return true;
121     }
122 };
123 } // namespace OHOS::Rosen
124 #endif // FOUNDATION_MARSHALLING_HELPER_H