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