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 "securec.h"
18
19 namespace OHOS {
20 namespace AAFwk {
Marshalling(const ChangeInfo & input,MessageParcel & data)21 bool ChangeInfo::Marshalling(const ChangeInfo &input, MessageParcel &data)
22 {
23 if (!data.WriteUint32(static_cast<uint32_t>(input.changeType_))) {
24 return false;
25 }
26
27 if (input.uris_.size() > std::numeric_limits<uint32_t>::max() ||
28 !data.WriteUint32(static_cast<uint32_t>(input.uris_.size()))) {
29 return false;
30 }
31
32 for (auto const &uri : input.uris_) {
33 if (!data.WriteString(uri.ToString())) {
34 return false;
35 }
36 }
37
38 if (!data.WriteUint32(input.size_)) {
39 return false;
40 }
41
42 return input.size_ == 0 || data.WriteBuffer(input.data_, input.size_);
43 }
44
Unmarshalling(ChangeInfo & output,MessageParcel & parcel)45 bool ChangeInfo::Unmarshalling(ChangeInfo &output, MessageParcel &parcel)
46 {
47 uint32_t changeType;
48 if (!parcel.ReadUint32(changeType)) {
49 return false;
50 }
51
52 uint32_t len = 0;
53 if (!parcel.ReadUint32(len)) {
54 return false;
55 }
56 if (len > LIST_MAX_COUNT) {
57 return false;
58 }
59
60 std::list<Uri> uris;
61 for (uint32_t i = 0; i < len; i++) {
62 Uri uri = Uri(parcel.ReadString());
63 if (uri.ToString().empty()) {
64 return false;
65 }
66 uris.emplace_back(std::move(uri));
67 }
68
69 uint32_t size = 0;
70 if (!parcel.ReadUint32(size)) {
71 return false;
72 }
73
74 const uint8_t *data = size > 0 ? parcel.ReadBuffer(size) : nullptr;
75 if (size > 0 && data == nullptr) {
76 return false;
77 }
78 output.changeType_ = static_cast<ChangeType>(changeType);
79 std::swap(output.uris_, uris);
80 output.data_ = const_cast<uint8_t*>(data);
81 output.size_ = size;
82 return true;
83 }
84 } // namespace AAFwk
85 } // namespace OHOS