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