1 /*
2  * Copyright (c) 2021-2022 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 #include "permission_state_full_parcel.h"
17 #include "parcel_utils.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
Marshalling(Parcel & out) const22 bool PermissionStateFullParcel::Marshalling(Parcel& out) const
23 {
24     RETURN_IF_FALSE(out.WriteString(this->permStatFull.permissionName));
25     RETURN_IF_FALSE(out.WriteBool(this->permStatFull.isGeneral));
26 
27     RETURN_IF_FALSE(out.WriteUint32(this->permStatFull.resDeviceID.size()));
28     for (auto devId : this->permStatFull.resDeviceID) {
29         RETURN_IF_FALSE(out.WriteString(devId));
30     }
31 
32     RETURN_IF_FALSE(out.WriteUint32(this->permStatFull.grantStatus.size()));
33     for (auto grantStat : this->permStatFull.grantStatus) {
34         RETURN_IF_FALSE(out.WriteInt32(grantStat));
35     }
36 
37     RETURN_IF_FALSE(out.WriteUint32(this->permStatFull.grantFlags.size()));
38     for (auto grantFlag : this->permStatFull.grantFlags) {
39         RETURN_IF_FALSE(out.WriteUint32(grantFlag));
40     }
41     return true;
42 }
43 
Unmarshalling(Parcel & in)44 PermissionStateFullParcel* PermissionStateFullParcel::Unmarshalling(Parcel& in)
45 {
46     auto* permissionStateParcel = new (std::nothrow) PermissionStateFullParcel();
47     if (permissionStateParcel == nullptr) {
48         return nullptr;
49     }
50 
51     RELEASE_IF_FALSE(in.ReadString(permissionStateParcel->permStatFull.permissionName), permissionStateParcel);
52     RELEASE_IF_FALSE(in.ReadBool(permissionStateParcel->permStatFull.isGeneral), permissionStateParcel);
53 
54     uint32_t resIdSize = 0;
55     RELEASE_IF_FALSE(in.ReadUint32(resIdSize), permissionStateParcel);
56     RELEASE_IF_FALSE(resIdSize <= MAX_DEVICE_ID_SIZE, permissionStateParcel);
57     for (uint32_t i = 0; i < resIdSize; i++) {
58         std::string resId;
59         RELEASE_IF_FALSE(in.ReadString(resId), permissionStateParcel);
60         permissionStateParcel->permStatFull.resDeviceID.emplace_back(resId);
61     }
62 
63     uint32_t grantStatsSize = 0;
64     RELEASE_IF_FALSE(in.ReadUint32(grantStatsSize), permissionStateParcel);
65     RELEASE_IF_FALSE(grantStatsSize <= MAX_DEVICE_ID_SIZE, permissionStateParcel);
66     for (uint32_t i = 0; i < grantStatsSize; i++) {
67         int grantStat;
68         RELEASE_IF_FALSE(in.ReadInt32(grantStat), permissionStateParcel);
69         permissionStateParcel->permStatFull.grantStatus.emplace_back(grantStat);
70     }
71 
72     uint32_t grantFlagSize = 0;
73     RELEASE_IF_FALSE(in.ReadUint32(grantFlagSize), permissionStateParcel);
74     RELEASE_IF_FALSE(grantFlagSize <= MAX_DEVICE_ID_SIZE, permissionStateParcel);
75     for (uint32_t i = 0; i < grantFlagSize; i++) {
76         uint32_t flag;
77         RELEASE_IF_FALSE(in.ReadUint32(flag), permissionStateParcel);
78         permissionStateParcel->permStatFull.grantFlags.emplace_back(flag);
79     }
80     return permissionStateParcel;
81 }
82 } // namespace AccessToken
83 } // namespace Security
84 } // namespace OHOS
85