1/*
2 * Copyright (C) 2022-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 "message_parcel.h"
17
18#include <sys/mman.h>
19#include <unistd.h>
20
21#include "iremote_object.h"
22
23namespace {
24static std::u16string g_interfaceTokenName {};
25} // namespace
26
27namespace OHOS {
28MessageParcel::MessageParcel()
29    : Parcel(), writeRawDataFd_(-1), readRawDataFd_(-1),
30    kernelMappedWrite_(nullptr), kernelMappedRead_(nullptr),
31    rawData_(nullptr), rawDataSize_(0)
32{
33}
34
35MessageParcel::MessageParcel(Allocator* allocator)
36    : Parcel(allocator), writeRawDataFd_(-1), readRawDataFd_(-1),
37    kernelMappedWrite_(nullptr), kernelMappedRead_(nullptr),
38    rawData_(nullptr), rawDataSize_(0)
39{
40}
41
42MessageParcel::~MessageParcel()
43{
44    if (kernelMappedWrite_ != nullptr) {
45        ::munmap(kernelMappedWrite_, rawDataSize_);
46        kernelMappedWrite_ = nullptr;
47    }
48    if (kernelMappedRead_ != nullptr) {
49        ::munmap(kernelMappedRead_, rawDataSize_);
50        kernelMappedRead_ = nullptr;
51    }
52
53    if (readRawDataFd_ > 0) {
54        ::close(readRawDataFd_);
55        readRawDataFd_ = -1;
56    }
57    if (writeRawDataFd_ > 0) {
58        ::close(writeRawDataFd_);
59        writeRawDataFd_ = -1;
60    }
61
62    ClearFileDescriptor();
63
64    rawData_ = nullptr;
65    rawDataSize_ = 0;
66}
67
68#ifndef CONFIG_IPC_SINGLE
69bool MessageParcel::WriteDBinderProxy(const sptr<IRemoteObject>& object, uint32_t handle, uint64_t stubIndex)
70{
71    (void)object;
72    (void)handle;
73    (void)stubIndex;
74    return false;
75}
76#endif
77
78bool MessageParcel::WriteRemoteObject(const sptr<IRemoteObject>& object)
79{
80    (void)object;
81    return false;
82}
83
84sptr<IRemoteObject> MessageParcel::ReadRemoteObject()
85{
86    return nullptr;
87}
88
89bool MessageParcel::WriteFileDescriptor(int fd)
90{
91    (void)fd;
92    return false;
93}
94
95int MessageParcel::ReadFileDescriptor()
96{
97    return -1;
98}
99
100void MessageParcel::ClearFileDescriptor() {}
101
102bool MessageParcel::ContainFileDescriptors() const
103{
104    return false;
105}
106
107bool MessageParcel::WriteInterfaceToken(std::u16string name)
108{
109#ifdef MOCK_WRITE_INTERFACE_TOKEN_RETURN_TRUE
110    g_interfaceTokenName = name;
111    return true;
112#else
113    (void)name;
114    return false;
115#endif
116}
117
118std::u16string MessageParcel::ReadInterfaceToken()
119{
120#ifdef MOCK_WRITE_INTERFACE_TOKEN_RETURN_TRUE
121    return g_interfaceTokenName;
122#else
123    return ReadString16();
124#endif
125}
126
127bool MessageParcel::WriteRawData(const void* data, size_t size)
128{
129    (void)data;
130    (void)size;
131    return false;
132}
133
134bool MessageParcel::RestoreRawData(std::shared_ptr<char> rawData, size_t size)
135{
136    (void)rawData;
137    (void)size;
138    return false;
139}
140
141const void* MessageParcel::ReadRawData(size_t size)
142{
143    (void)size;
144    return nullptr;
145}
146
147const void* MessageParcel::GetRawData() const
148{
149    return nullptr;
150}
151
152size_t MessageParcel::GetRawDataSize() const
153{
154    return rawDataSize_;
155}
156
157size_t MessageParcel::GetRawDataCapacity() const
158{
159    return MAX_RAWDATA_SIZE;
160}
161
162void MessageParcel::WriteNoException()
163{
164    WriteInt32(0);
165}
166
167int32_t MessageParcel::ReadException()
168{
169    return ReadInt32();
170}
171
172bool MessageParcel::WriteAshmem(sptr<Ashmem> ashmem)
173{
174    (void)ashmem;
175    return false;
176}
177
178sptr<Ashmem> MessageParcel::ReadAshmem()
179{
180    return nullptr;
181}
182
183bool MessageParcel::Append(MessageParcel& data)
184{
185    (void)data;
186    return false;
187}
188} // namespace OHOS
189