1 /* 2 * Copyright (c) 2025 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 IMF_FRAMEWORKS_RESPONSE_DATA_UTIL_H 17 #define IMF_FRAMEWORKS_RESPONSE_DATA_UTIL_H 18 19 #include <cstdint> 20 #include <variant> 21 22 #include "input_method_property.h" 23 #include "input_method_utils.h" 24 25 namespace OHOS { 26 namespace MiscServices { 27 inline constexpr size_t MAX_SIZE = 102400; 28 class ResponseDataUtil { 29 public: Unmarshall(Parcel & in,std::vector<T> & out)30 template<typename T> static bool Unmarshall(Parcel &in, std::vector<T> &out) 31 { 32 int32_t len = 0; 33 if (!in.ReadInt32(len)) { 34 return false; 35 } 36 if (len < 0) { 37 return false; 38 } 39 auto size = static_cast<size_t>(len); 40 if (size > MAX_SIZE) { 41 return false; 42 } 43 out.clear(); 44 for (size_t i = 0; i < size; ++i) { 45 T value; 46 if (!value.ReadFromParcel(in)) { 47 return false; 48 } 49 out.push_back(value); 50 } 51 return true; 52 } 53 Marshall(const std::vector<T> & in,Parcel & out)54 template<typename T> static bool Marshall(const std::vector<T> &in, Parcel &out) 55 { 56 auto size = in.size(); 57 if (size > INT32_MAX) { 58 return false; 59 } 60 if (!out.WriteInt32(static_cast<int32_t>(size))) { 61 return false; 62 } 63 for (const auto &it : in) { 64 if (!it.Marshalling(out)) { 65 return false; 66 } 67 } 68 return true; 69 } 70 }; 71 } // namespace MiscServices 72 } // namespace OHOS 73 74 #endif // IMF_FRAMEWORKS_RESPONSE_DATA_UTIL_H 75