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 "native_token_info_parcel.h"
17 #include <iosfwd>
18 #include <new>
19 #include <string>
20 #include <vector>
21 #include "access_token.h"
22 #include "parcel_utils.h"
23 
24 namespace OHOS {
25 namespace Security {
26 namespace AccessToken {
Marshalling(Parcel & out) const27 bool NativeTokenInfoParcel::Marshalling(Parcel& out) const
28 {
29     RETURN_IF_FALSE(out.WriteInt32(this->nativeTokenInfoParams.apl));
30     RETURN_IF_FALSE(out.WriteUint8(this->nativeTokenInfoParams.ver));
31     RETURN_IF_FALSE(out.WriteString(this->nativeTokenInfoParams.processName));
32     RETURN_IF_FALSE(out.WriteUint32(this->nativeTokenInfoParams.tokenID));
33     RETURN_IF_FALSE(out.WriteUint32(this->nativeTokenInfoParams.tokenAttr));
34 
35     if ((this->nativeTokenInfoParams.dcap).size() > MAX_DCAP_SIZE) {
36         return false;
37     }
38     uint32_t dcapSize = (this->nativeTokenInfoParams.dcap).size();
39     RETURN_IF_FALSE(out.WriteUint32(dcapSize));
40 
41     for (const auto& dcapItem : this->nativeTokenInfoParams.dcap) {
42         RETURN_IF_FALSE(out.WriteString(dcapItem));
43     }
44 
45     if ((this->nativeTokenInfoParams.nativeAcls).size() > MAX_ACL_SIZE) {
46         return false;
47     }
48     uint32_t nativeAclSize = (this->nativeTokenInfoParams.nativeAcls).size();
49     RETURN_IF_FALSE(out.WriteUint32(nativeAclSize));
50 
51     for (const auto& item : this->nativeTokenInfoParams.nativeAcls) {
52         RETURN_IF_FALSE(out.WriteString(item));
53     }
54 
55     return true;
56 }
57 
Unmarshalling(Parcel & in)58 NativeTokenInfoParcel* NativeTokenInfoParcel::Unmarshalling(Parcel& in)
59 {
60     auto* nativeTokenInfoParcel = new (std::nothrow) NativeTokenInfoParcel();
61     if (nativeTokenInfoParcel == nullptr) {
62         return nullptr;
63     }
64 
65     int32_t apl;
66     uint8_t ver;
67     RELEASE_IF_FALSE(in.ReadInt32(apl), nativeTokenInfoParcel);
68     RELEASE_IF_FALSE(in.ReadUint8(ver), nativeTokenInfoParcel);
69     nativeTokenInfoParcel->nativeTokenInfoParams.apl = ATokenAplEnum(apl);
70     nativeTokenInfoParcel->nativeTokenInfoParams.ver = ver;
71 
72     nativeTokenInfoParcel->nativeTokenInfoParams.processName = in.ReadString();
73     RELEASE_IF_FALSE(in.ReadUint32(nativeTokenInfoParcel->nativeTokenInfoParams.tokenID), nativeTokenInfoParcel);
74     RELEASE_IF_FALSE(in.ReadUint32(nativeTokenInfoParcel->nativeTokenInfoParams.tokenAttr), nativeTokenInfoParcel);
75 
76     uint32_t dcapSize;
77     RELEASE_IF_FALSE(in.ReadUint32(dcapSize), nativeTokenInfoParcel);
78     RELEASE_IF_FALSE(dcapSize <= MAX_DCAP_SIZE, nativeTokenInfoParcel);
79 
80     for (uint32_t i = 0; i < dcapSize; i++) {
81         std::string dcapsItem;
82         RELEASE_IF_FALSE(in.ReadString(dcapsItem), nativeTokenInfoParcel);
83         nativeTokenInfoParcel->nativeTokenInfoParams.dcap.emplace_back(dcapsItem);
84     }
85 
86     uint32_t nativeAclSize;
87     RELEASE_IF_FALSE(in.ReadUint32(nativeAclSize), nativeTokenInfoParcel);
88     RELEASE_IF_FALSE(nativeAclSize <= MAX_ACL_SIZE, nativeTokenInfoParcel);
89 
90     for (uint32_t i = 0; i < nativeAclSize; i++) {
91         std::string item;
92         RELEASE_IF_FALSE(in.ReadString(item), nativeTokenInfoParcel);
93         nativeTokenInfoParcel->nativeTokenInfoParams.nativeAcls.emplace_back(item);
94     }
95     return nativeTokenInfoParcel;
96 }
97 } // namespace AccessToken
98 } // namespace Security
99 } // namespace OHOS
100