1 /*
2 * Copyright (c) 2025 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 "domain_account_parcel.h"
17
18 #include <securec.h>
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const unsigned int DOMAIN_DATA_MAX_SIZE = 6144; // os account and domain account property limits addition
25 }
ReadFromParcel(Parcel & parcel)26 bool DomainAccountParcel::ReadFromParcel(Parcel &parcel)
27 {
28 uint32_t size;
29 if (!parcel.ReadUint32(size)) {
30 ACCOUNT_LOGE("Read size failed, please check size value in parcel");
31 return false;
32 }
33 if (size > DOMAIN_DATA_MAX_SIZE) {
34 return false;
35 }
36 const uint8_t *buffer = parcel.ReadBuffer(size);
37 if (buffer == nullptr) {
38 ACCOUNT_LOGE("Read buffer failed, please check buffer value in parcel");
39 return false;
40 }
41 void *bufferNew = nullptr;
42 bufferNew = malloc(size);
43 if (bufferNew == nullptr) {
44 return false;
45 }
46 if (memcpy_s(bufferNew, size, buffer, size) != EOK) {
47 free(bufferNew);
48 bufferNew = nullptr;
49 return false;
50 }
51 if (!parcelData_.ParseFrom(reinterpret_cast<uintptr_t>(bufferNew), size)) {
52 ACCOUNT_LOGE("Parse from failed, please check data");
53 free(bufferNew);
54 bufferNew = nullptr;
55 return false;
56 }
57 return true;
58 }
59
Marshalling(Parcel & parcel) const60 bool DomainAccountParcel::Marshalling(Parcel &parcel) const
61 {
62 uint32_t size = parcelData_.GetDataSize();
63 if (!parcel.WriteUint32(size)) {
64 ACCOUNT_LOGE("Write size failed, please check size value in parcelData_");
65 return false;
66 }
67 if (!parcel.WriteBuffer(reinterpret_cast<const uint8_t *>(parcelData_.GetData()), size)) {
68 ACCOUNT_LOGE("Write buffer failed, please check buffer value in parcelData_");
69 return false;
70 }
71 return true;
72 }
73
Unmarshalling(Parcel & parcel)74 DomainAccountParcel *DomainAccountParcel::Unmarshalling(Parcel &parcel)
75 {
76 DomainAccountParcel *info = new (std::nothrow) DomainAccountParcel();
77 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
78 ACCOUNT_LOGW("Read from parcel failed, please check info value");
79 delete info;
80 info = nullptr;
81 }
82 return info;
83 }
84 } // namespace AccountSA
85 } // namespace OHOS
86