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 
16 #include "cert_parcel.h"
17 #include "dlp_permission_log.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace DlpPermission {
22 namespace {
23 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "CertParcel" };
24 }
25 
CertParcel()26 CertParcel::CertParcel()
27 {
28     isNeedAdapter = false;
29     contactAccount = "";
30 }
31 
Marshalling(Parcel & data) const32 bool CertParcel::Marshalling(Parcel& data) const
33 {
34     if (!data.WriteBool(this->isNeedAdapter)) {
35         DLP_LOG_ERROR(LABEL, "Write bool isNeedAdapter fail");
36         return false;
37     }
38     if (!data.WriteString(this->contactAccount)) {
39         DLP_LOG_ERROR(LABEL, "Write string contactAccount fail");
40         return false;
41     }
42     if (!data.WriteUInt8Vector(this->cert)) {
43         DLP_LOG_ERROR(LABEL, "Write uint8 vector fail");
44         return false;
45     }
46     if (!data.WriteUInt8Vector(this->offlineCert)) {
47         DLP_LOG_ERROR(LABEL, "Write uint8 offlineCert vector fail");
48         return false;
49     }
50     return true;
51 }
52 
FreeCertParcel(CertParcel * parcel)53 static CertParcel* FreeCertParcel(CertParcel* parcel)
54 {
55     delete parcel;
56     return nullptr;
57 }
58 
Unmarshalling(Parcel & data)59 CertParcel* CertParcel::Unmarshalling(Parcel& data)
60 {
61     auto* parcel = new (std::nothrow) CertParcel();
62     if (parcel == nullptr) {
63         DLP_LOG_ERROR(LABEL, "Alloc buff for parcel fail");
64         return nullptr;
65     }
66     if (!data.ReadBool(parcel->isNeedAdapter)) {
67         DLP_LOG_ERROR(LABEL, "Read isNeedAdapter fail");
68         return FreeCertParcel(parcel);
69     }
70     if (!data.ReadString(parcel->contactAccount)) {
71         DLP_LOG_ERROR(LABEL, "Read contactAccount fail");
72         return FreeCertParcel(parcel);
73     }
74     if (!data.ReadUInt8Vector(&parcel->cert)) {
75         DLP_LOG_ERROR(LABEL, "Read cert fail");
76         return FreeCertParcel(parcel);
77     }
78     if (!data.ReadUInt8Vector(&parcel->offlineCert)) {
79         DLP_LOG_ERROR(LABEL, "Read offlineCert fail");
80         return FreeCertParcel(parcel);
81     }
82     return parcel;
83 }
84 } // namespace DlpPermission
85 } // namespace Security
86 } // namespace OHOS
87