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