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 } 67 return true; 68 } 69 70 template<class T> MarshallingVectorObj(Parcel & parcel,const std::vector<T> & data,std::function<bool (Parcel &,const T &)> func)71 static bool MarshallingVectorObj(Parcel &parcel, const std::vector<T>& data, 72 std::function<bool(Parcel&, const T&)> func) 73 { 74 if (data.size() > INT_MAX) { 75 return false; 76 } 77 if (func == nullptr) { 78 return false; 79 } 80 if (!parcel.WriteInt32(static_cast<int32_t>(data.size()))) { 81 return false; 82 } 83 for (const auto &v : data) { 84 if (!func(parcel, v)) { 85 return false; 86 } 87 } 88 return true; 89 } 90 91 template<class T> UnmarshallingVectorObj(Parcel & parcel,std::vector<T> & data,std::function<bool (Parcel &,T &)> func)92 static bool UnmarshallingVectorObj(Parcel &parcel, std::vector<T>& data, std::function<bool(Parcel&, T&)> func) 93 { 94 if (func == nullptr) { 95 return false; 96 } 97 int32_t len = parcel.ReadInt32(); 98 if (len < 0) { 99 return false; 100 } 101 102 size_t readAbleSize = parcel.GetReadableBytes(); 103 size_t size = static_cast<size_t>(len); 104 if ((size > readAbleSize) || (size > data.max_size())) { 105 return false; 106 } 107 data.resize(size); 108 if (data.size() < size) { 109 return false; 110 } 111 112 for (size_t i = 0; i < size; i++) { 113 if (!func(parcel, data[i])) { 114 return false; 115 } 116 } 117 return true; 118 } 119 }; 120 } // namespace OHOS::Rosen 121 #endif // FOUNDATION_MARSHALLING_HELPER_H