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 "sane_picture_data.h"
17 #include "sane_info.h"
18 #include "scan_log.h"
19 #include "securec.h"
20 #include "message_parcel.h"
21 #include "parcel.h"
22
23 namespace OHOS::Scan {
SanePictureData()24 SanePictureData::SanePictureData() : ret_(SANE_READ_OK) {}
~SanePictureData()25 SanePictureData::~SanePictureData() {}
26
Marshalling(Parcel & parcel) const27 bool SanePictureData::Marshalling(Parcel &parcel) const
28 {
29 bool status = true;
30 status &= parcel.WriteUint32(ret_);
31 if (ret_ == SANE_READ_OK) {
32 status &= parcel.WriteUint32(dataBuffer_.size());
33 status &= parcel.WriteBuffer((void*) dataBuffer_.data(), dataBuffer_.size());
34 }
35 return status;
36 }
37
Unmarshalling(Parcel & parcel)38 SanePictureData* SanePictureData::Unmarshalling(Parcel &parcel)
39 {
40 std::unique_ptr<SanePictureData> obj = std::make_unique<SanePictureData>();
41 if (obj == nullptr) {
42 SCAN_HILOGE("obj is a nullptr");
43 return nullptr;
44 }
45 obj->ret_ = parcel.ReadUint32();
46 if (obj->ret_ == SANE_READ_OK) {
47 uint32_t bufferSize = parcel.ReadUint32();
48 const uint8_t* data = parcel.ReadUnpadBuffer(bufferSize);
49 if (data == nullptr) {
50 SCAN_HILOGE("data is a nullptr");
51 return nullptr;
52 }
53 if (bufferSize > MAX_BUFLEN) {
54 SCAN_HILOGE("data exceeds MAX_BUFLEN");
55 return nullptr;
56 }
57 obj->dataBuffer_.resize(bufferSize, 0);
58 auto ret = memcpy_s(obj->dataBuffer_.data(), bufferSize, data, bufferSize);
59 if (ret != SANE_STATUS_GOOD) {
60 SCAN_HILOGE("memcpy_s failed, errorCode: %{public}d", ret);
61 return nullptr;
62 }
63 }
64 return obj.release();
65 }
66 } // namespace OHOS::Scan