1 /*
2  * Copyright (c) 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_change_scope_parcel.h"
17 #include "parcel_utils.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
Marshalling(Parcel & out) const22 bool PermStateChangeScopeParcel::Marshalling(Parcel& out) const
23 {
24     RETURN_IF_FALSE(out.WriteUint32((this->scope.tokenIDs.size())));
25     for (const auto& tokenID : this->scope.tokenIDs) {
26         RETURN_IF_FALSE(out.WriteUint32(tokenID));
27     }
28 
29     RETURN_IF_FALSE(out.WriteUint32((this->scope.permList.size())));
30     for (const auto& permissionName : this->scope.permList) {
31         RETURN_IF_FALSE(out.WriteString(permissionName));
32     }
33     return true;
34 }
35 
Unmarshalling(Parcel & in)36 PermStateChangeScopeParcel* PermStateChangeScopeParcel::Unmarshalling(Parcel& in)
37 {
38     auto* permStateChangeScopeParcel = new (std::nothrow) PermStateChangeScopeParcel();
39     if (permStateChangeScopeParcel == nullptr) {
40         return nullptr;
41     }
42     uint32_t tokenIdListSize = 0;
43     RELEASE_IF_FALSE(in.ReadUint32(tokenIdListSize), permStateChangeScopeParcel);
44     RELEASE_IF_FALSE(tokenIdListSize <= TOKENIDS_LIST_SIZE_MAX, permStateChangeScopeParcel);
45     for (uint32_t i = 0; i < tokenIdListSize; i++) {
46         AccessTokenID tokenID;
47         RELEASE_IF_FALSE(in.ReadUint32(tokenID), permStateChangeScopeParcel);
48         permStateChangeScopeParcel->scope.tokenIDs.emplace_back(tokenID);
49     }
50 
51     uint32_t permListSize = 0;
52     RELEASE_IF_FALSE(in.ReadUint32(permListSize), permStateChangeScopeParcel);
53     RELEASE_IF_FALSE(permListSize <= PERMS_LIST_SIZE_MAX, permStateChangeScopeParcel);
54     for (uint32_t i = 0; i < permListSize; i++) {
55         std::string permName;
56         RELEASE_IF_FALSE(in.ReadString(permName), permStateChangeScopeParcel);
57         permStateChangeScopeParcel->scope.permList.emplace_back(permName);
58     }
59     return permStateChangeScopeParcel;
60 }
61 } // namespace AccessToken
62 } // namespace Security
63 } // namespace OHOS
64