1 /*
2 * Copyright (c) 2023 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 #include <limits>
16 #include "dataobs_mgr_changeinfo.h"
17 #include "dataobs_utils.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace AAFwk {
22 using Value = std::variant<std::monostate, int64_t, double, std::string, bool, std::vector<uint8_t>>;
23 using VBucket = std::map<std::string, Value>;
24 using VBuckets = std::vector<VBucket>;
Marshalling(const ChangeInfo & input,MessageParcel & parcel)25 bool ChangeInfo::Marshalling(const ChangeInfo &input, MessageParcel &parcel)
26 {
27 if (!parcel.WriteUint32(static_cast<uint32_t>(input.changeType_))) {
28 return false;
29 }
30
31 if (input.uris_.size() > std::numeric_limits<uint32_t>::max() ||
32 !parcel.WriteUint32(static_cast<uint32_t>(input.uris_.size()))) {
33 return false;
34 }
35
36 for (auto const &uri : input.uris_) {
37 if (!parcel.WriteString(uri.ToString())) {
38 return false;
39 }
40 }
41
42 if (!parcel.WriteUint32(input.size_)) {
43 return false;
44 }
45
46 if (!(input.size_ == 0 || parcel.WriteBuffer(input.data_, input.size_))) {
47 return false;
48 }
49
50 if (!DataObsUtils::Marshal(parcel, input.valueBuckets_)) {
51 return false;
52 }
53 return true;
54 }
55
Unmarshalling(ChangeInfo & output,MessageParcel & parcel)56 bool ChangeInfo::Unmarshalling(ChangeInfo &output, MessageParcel &parcel)
57 {
58 uint32_t changeType;
59 if (!parcel.ReadUint32(changeType)) {
60 return false;
61 }
62
63 uint32_t len = 0;
64 if (!parcel.ReadUint32(len)) {
65 return false;
66 }
67 if (len > LIST_MAX_COUNT) {
68 return false;
69 }
70
71 std::list<Uri> uris;
72 for (uint32_t i = 0; i < len; i++) {
73 Uri uri = Uri(parcel.ReadString());
74 if (uri.ToString().empty()) {
75 return false;
76 }
77 uris.emplace_back(std::move(uri));
78 }
79
80 uint32_t size = 0;
81 if (!parcel.ReadUint32(size)) {
82 return false;
83 }
84
85 const uint8_t *data = size > 0 ? parcel.ReadBuffer(size) : nullptr;
86 if (size > 0 && data == nullptr) {
87 return false;
88 }
89 VBuckets buckets;
90 if (!(DataObsUtils::Unmarshal(parcel, buckets))) {
91 return false;
92 }
93 output.changeType_ = static_cast<ChangeType>(changeType);
94 std::swap(output.uris_, uris);
95 output.data_ = const_cast<uint8_t*>(data);
96 output.size_ = size;
97 output.valueBuckets_ = std::move(buckets);
98 return true;
99 }
100 } // namespace AAFwk
101 } // namespace OHOS