/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "dataobs_mgr_changeinfo.h" #include "dataobs_utils.h" #include "securec.h" namespace OHOS { namespace AAFwk { using Value = std::variant>; using VBucket = std::map; using VBuckets = std::vector; bool ChangeInfo::Marshalling(const ChangeInfo &input, MessageParcel &parcel) { if (!parcel.WriteUint32(static_cast(input.changeType_))) { return false; } if (input.uris_.size() > std::numeric_limits::max() || !parcel.WriteUint32(static_cast(input.uris_.size()))) { return false; } for (auto const &uri : input.uris_) { if (!parcel.WriteString(uri.ToString())) { return false; } } if (!parcel.WriteUint32(input.size_)) { return false; } if (!(input.size_ == 0 || parcel.WriteBuffer(input.data_, input.size_))) { return false; } if (!DataObsUtils::Marshal(parcel, input.valueBuckets_)) { return false; } return true; } bool ChangeInfo::Unmarshalling(ChangeInfo &output, MessageParcel &parcel) { uint32_t changeType; if (!parcel.ReadUint32(changeType)) { return false; } uint32_t len = 0; if (!parcel.ReadUint32(len)) { return false; } if (len > LIST_MAX_COUNT) { return false; } std::list uris; for (uint32_t i = 0; i < len; i++) { Uri uri = Uri(parcel.ReadString()); if (uri.ToString().empty()) { return false; } uris.emplace_back(std::move(uri)); } uint32_t size = 0; if (!parcel.ReadUint32(size)) { return false; } const uint8_t *data = size > 0 ? parcel.ReadBuffer(size) : nullptr; if (size > 0 && data == nullptr) { return false; } VBuckets buckets; if (!(DataObsUtils::Unmarshal(parcel, buckets))) { return false; } output.changeType_ = static_cast(changeType); std::swap(output.uris_, uris); output.data_ = const_cast(data); output.size_ = size; output.valueBuckets_ = std::move(buckets); return true; } } // namespace AAFwk } // namespace OHOS