1 /*
2 * Copyright (c) 2021-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 <vector>
17 #include <string>
18 #include "hdf_sbuf_ipc.h"
19 #include "hdf_sbuf_impl.h"
20 #include "hdf_core_log.h"
21 #include "hdf_remote_adapter.h"
22
23 using namespace OHOS;
24
25 static void MParcelImplInterfaceAssign(struct HdfSBufImpl *inf);
26
27 struct SBufMParcelImpl {
SBufMParcelImplSBufMParcelImpl28 explicit SBufMParcelImpl(
29 OHOS::MessageParcel *parcel = nullptr, bool owned = true): realParcel_(parcel), owned_(owned)
30 {
31 MParcelImplInterfaceAssign(&infImpl);
32 }
~SBufMParcelImplSBufMParcelImpl33 ~SBufMParcelImpl()
34 {
35 if (owned_ && realParcel_ != nullptr) {
36 delete realParcel_;
37 realParcel_ = nullptr;
38 }
39 }
40 struct HdfSBufImpl infImpl;
41 OHOS::MessageParcel *realParcel_;
42 std::vector<std::u16string> str16Pool_;
43 bool owned_;
44 };
45
MParcelCast(struct HdfSBufImpl * impl)46 static MessageParcel *MParcelCast(struct HdfSBufImpl *impl)
47 {
48 SBufMParcelImpl *sbufImpl = reinterpret_cast<SBufMParcelImpl *>(impl);
49 return sbufImpl->realParcel_;
50 }
51
SbufMParcelImplWriteBuffer(struct HdfSBufImpl * sbuf,const uint8_t * data,uint32_t writeSize)52 static bool SbufMParcelImplWriteBuffer(struct HdfSBufImpl *sbuf, const uint8_t *data, uint32_t writeSize)
53 {
54 auto parcel = MParcelCast(sbuf);
55 if (!parcel->WriteUint32(writeSize)) {
56 return false;
57 }
58 return parcel->WriteUnpadBuffer(static_cast<const void *>(data), writeSize);
59 }
60
61 // LCOV_EXCL_START
SbufMParcelImplWriteUnpadBuffer(struct HdfSBufImpl * sbuf,const uint8_t * data,uint32_t writeSize)62 static bool SbufMParcelImplWriteUnpadBuffer(struct HdfSBufImpl *sbuf, const uint8_t *data, uint32_t writeSize)
63 {
64 return MParcelCast(sbuf)->WriteUnpadBuffer(static_cast<const void *>(data), writeSize);
65 }
66
SbufMParcelImplWriteUint64(struct HdfSBufImpl * sbuf,uint64_t value)67 static bool SbufMParcelImplWriteUint64(struct HdfSBufImpl *sbuf, uint64_t value)
68 {
69 return MParcelCast(sbuf)->WriteUint64(value);
70 }
71 // LCOV_EXCL_STOP
72
SbufMParcelImplWriteUint32(struct HdfSBufImpl * sbuf,uint32_t value)73 static bool SbufMParcelImplWriteUint32(struct HdfSBufImpl *sbuf, uint32_t value)
74 {
75 return MParcelCast(sbuf)->WriteUint32(value);
76 }
77
78 // LCOV_EXCL_START
SbufMParcelImplWriteUint16(struct HdfSBufImpl * sbuf,uint16_t value)79 static bool SbufMParcelImplWriteUint16(struct HdfSBufImpl *sbuf, uint16_t value)
80 {
81 return MParcelCast(sbuf)->WriteUint16(value);
82 }
83
SbufMParcelImplWriteUint8(struct HdfSBufImpl * sbuf,uint8_t value)84 static bool SbufMParcelImplWriteUint8(struct HdfSBufImpl *sbuf, uint8_t value)
85 {
86 return MParcelCast(sbuf)->WriteUint8(value);
87 }
88
SbufMParcelImplWriteInt64(struct HdfSBufImpl * sbuf,int64_t value)89 static bool SbufMParcelImplWriteInt64(struct HdfSBufImpl *sbuf, int64_t value)
90 {
91 return MParcelCast(sbuf)->WriteInt64(value);
92 }
93 // LCOV_EXCL_STOP
94
SbufMParcelImplWriteInt32(struct HdfSBufImpl * sbuf,int32_t value)95 static bool SbufMParcelImplWriteInt32(struct HdfSBufImpl *sbuf, int32_t value)
96 {
97 return MParcelCast(sbuf)->WriteInt32(value);
98 }
99
SbufMParcelImplWriteInt16(struct HdfSBufImpl * sbuf,int16_t value)100 static bool SbufMParcelImplWriteInt16(struct HdfSBufImpl *sbuf, int16_t value)
101 {
102 return MParcelCast(sbuf)->WriteInt16(value);
103 }
104
SbufMParcelImplWriteInt8(struct HdfSBufImpl * sbuf,int8_t value)105 static bool SbufMParcelImplWriteInt8(struct HdfSBufImpl *sbuf, int8_t value)
106 {
107 return MParcelCast(sbuf)->WriteInt8(value);
108 }
109
SbufMParcelImplWriteString(struct HdfSBufImpl * sbuf,const char * value)110 static bool SbufMParcelImplWriteString(struct HdfSBufImpl *sbuf, const char *value)
111 {
112 return MParcelCast(sbuf)->WriteCString(value);
113 }
114
115 // LCOV_EXCL_START
SbufMParcelImplWriteString16(struct HdfSBufImpl * sbuf,const char16_t * value,uint32_t size)116 static bool SbufMParcelImplWriteString16(struct HdfSBufImpl *sbuf, const char16_t *value, uint32_t size)
117 {
118 return MParcelCast(sbuf)->WriteString16WithLength(value, size);
119 }
120
SbufMParcelImplWriteFileDescriptor(struct HdfSBufImpl * sbuf,int fd)121 static bool SbufMParcelImplWriteFileDescriptor(struct HdfSBufImpl *sbuf, int fd)
122 {
123 return MParcelCast(sbuf)->WriteFileDescriptor(fd);
124 }
125
SbufMParcelImplWriteDouble(struct HdfSBufImpl * sbuf,double value)126 static bool SbufMParcelImplWriteDouble(struct HdfSBufImpl *sbuf, double value)
127 {
128 return MParcelCast(sbuf)->WriteDouble(value);
129 }
130
SbufMParcelImplWriteFloat(struct HdfSBufImpl * sbuf,float value)131 static bool SbufMParcelImplWriteFloat(struct HdfSBufImpl *sbuf, float value)
132 {
133 return MParcelCast(sbuf)->WriteFloat(value);
134 }
135
SbufMParcelImplReadFloat(struct HdfSBufImpl * sbuf,float * value)136 static bool SbufMParcelImplReadFloat(struct HdfSBufImpl *sbuf, float *value)
137 {
138 if (value == nullptr) {
139 return false;
140 }
141 float v = 0;
142 bool ret = MParcelCast(sbuf)->ReadFloat(v);
143 *value = v;
144 return ret;
145 }
146
SbufMParcelImplReadDouble(struct HdfSBufImpl * sbuf,double * value)147 static bool SbufMParcelImplReadDouble(struct HdfSBufImpl *sbuf, double *value)
148 {
149 if (value == nullptr) {
150 return false;
151 }
152 double v = 0;
153 bool ret = MParcelCast(sbuf)->ReadDouble(v);
154 *value = v;
155 return ret;
156 }
157
SbufMParcelImplReadFileDescriptor(struct HdfSBufImpl * sbuf)158 static int SbufMParcelImplReadFileDescriptor(struct HdfSBufImpl *sbuf)
159 {
160 return MParcelCast(sbuf)->ReadFileDescriptor();
161 }
162
SbufMParcelImplReadBuffer(struct HdfSBufImpl * sbuf,const uint8_t ** data,uint32_t * readSize)163 static bool SbufMParcelImplReadBuffer(struct HdfSBufImpl *sbuf, const uint8_t **data, uint32_t *readSize)
164 {
165 if (data == nullptr || readSize == nullptr) {
166 return false;
167 }
168 MessageParcel *parcel = MParcelCast(sbuf);
169 *readSize = parcel->ReadUint32();
170 *data = parcel->ReadUnpadBuffer(*readSize);
171 return *data != nullptr;
172 }
173
SbufMParcelImplReadUnpadBuffer(struct HdfSBufImpl * sbuf,size_t length)174 static const uint8_t *SbufMParcelImplReadUnpadBuffer(struct HdfSBufImpl *sbuf, size_t length)
175 {
176 return MParcelCast(sbuf)->ReadUnpadBuffer(length);
177 }
178
SbufMParcelImplReadUint64(struct HdfSBufImpl * sbuf,uint64_t * value)179 static bool SbufMParcelImplReadUint64(struct HdfSBufImpl *sbuf, uint64_t *value)
180 {
181 if (value == nullptr) {
182 return false;
183 }
184 uint64_t v = 0;
185 bool ret = MParcelCast(sbuf)->ReadUint64(v);
186 *value = v;
187 return ret;
188 }
189 // LCOV_EXCL_STOP
190
SbufMParcelImplReadUint32(struct HdfSBufImpl * sbuf,uint32_t * value)191 static bool SbufMParcelImplReadUint32(struct HdfSBufImpl *sbuf, uint32_t *value)
192 {
193 if (value == nullptr) {
194 return false;
195 }
196 uint32_t v = 0;
197 bool ret = MParcelCast(sbuf)->ReadUint32(v);
198 *value = v;
199 return ret;
200 }
201
SbufMParcelImplReadUint16(struct HdfSBufImpl * sbuf,uint16_t * value)202 static bool SbufMParcelImplReadUint16(struct HdfSBufImpl *sbuf, uint16_t *value)
203 {
204 if (value == nullptr) {
205 return false;
206 }
207 uint16_t v = 0;
208 bool ret = MParcelCast(sbuf)->ReadUint16(v);
209 *value = v;
210 return ret;
211 }
212 // LCOV_EXCL_START
SbufMParcelImplReadUint8(struct HdfSBufImpl * sbuf,uint8_t * value)213 static bool SbufMParcelImplReadUint8(struct HdfSBufImpl *sbuf, uint8_t *value)
214 {
215 if (value == nullptr) {
216 return false;
217 }
218 uint8_t v = 0;
219 bool ret = MParcelCast(sbuf)->ReadUint8(v);
220 *value = v;
221 return ret;
222 }
223
SbufMParcelImplReadInt64(struct HdfSBufImpl * sbuf,int64_t * value)224 static bool SbufMParcelImplReadInt64(struct HdfSBufImpl *sbuf, int64_t *value)
225 {
226 if (value == nullptr) {
227 return false;
228 }
229 int64_t v = 0;
230 bool ret = MParcelCast(sbuf)->ReadInt64(v);
231 *value = v;
232 return ret;
233 }
234 // LCOV_EXCL_STOP
SbufMParcelImplReadInt32(struct HdfSBufImpl * sbuf,int32_t * value)235 static bool SbufMParcelImplReadInt32(struct HdfSBufImpl *sbuf, int32_t *value)
236 {
237 if (value == nullptr) {
238 return false;
239 }
240 int32_t v = 0;
241 bool ret = MParcelCast(sbuf)->ReadInt32(v);
242 *value = v;
243 return ret;
244 }
245 // LCOV_EXCL_START
SbufMParcelImplReadInt16(struct HdfSBufImpl * sbuf,int16_t * value)246 static bool SbufMParcelImplReadInt16(struct HdfSBufImpl *sbuf, int16_t *value)
247 {
248 if (value == nullptr) {
249 return false;
250 }
251 int16_t v = 0;
252 bool ret = MParcelCast(sbuf)->ReadInt16(v);
253 *value = v;
254 return ret;
255 }
256
SbufMParcelImplReadInt8(struct HdfSBufImpl * sbuf,int8_t * value)257 static bool SbufMParcelImplReadInt8(struct HdfSBufImpl *sbuf, int8_t *value)
258 {
259 if (value == nullptr) {
260 return false;
261 }
262 int8_t v = 0;
263 bool ret = MParcelCast(sbuf)->ReadInt8(v);
264 *value = v;
265 return ret;
266 }
267 // LCOV_EXCL_STOP
SbufMParcelImplReadString(struct HdfSBufImpl * sbuf)268 static const char *SbufMParcelImplReadString(struct HdfSBufImpl *sbuf)
269 {
270 return MParcelCast(sbuf)->ReadCString();
271 }
272
273 // LCOV_EXCL_START
SbufMParcelImplReadString16(struct HdfSBufImpl * sbuf)274 static const char16_t *SbufMParcelImplReadString16(struct HdfSBufImpl *sbuf)
275 {
276 SBufMParcelImpl *sbufImpl = reinterpret_cast<SBufMParcelImpl *>(sbuf);
277 sbufImpl->str16Pool_.emplace_back(sbufImpl->realParcel_->ReadString16());
278 return sbufImpl->str16Pool_.rbegin()->c_str();
279 }
280
SbufMParcelImplWriteRemoteService(struct HdfSBufImpl * sbuf,const struct HdfRemoteService * service)281 static int32_t SbufMParcelImplWriteRemoteService(struct HdfSBufImpl *sbuf, const struct HdfRemoteService *service)
282 {
283 if (service == nullptr) {
284 return HDF_ERR_INVALID_PARAM;
285 }
286 MessageParcel *parcel = MParcelCast(sbuf);
287 const struct HdfRemoteServiceHolder *holder = reinterpret_cast<const struct HdfRemoteServiceHolder *>(service);
288 return parcel->WriteRemoteObject(holder->remote_) ? HDF_SUCCESS : HDF_FAILURE;
289 }
290 // LCOV_EXCL_STOP
291
SbufMParcelImplReadRemoteService(struct HdfSBufImpl * sbuf)292 static struct HdfRemoteService *SbufMParcelImplReadRemoteService(struct HdfSBufImpl *sbuf)
293 {
294 auto remote = MParcelCast(sbuf)->ReadRemoteObject();
295 if (remote == nullptr) {
296 HDF_LOGE("%{public}s: read remote object fail", __func__);
297 return nullptr;
298 }
299 return HdfRemoteAdapterBind(remote);
300 }
301 // LCOV_EXCL_START
SbufMParcelImplGetData(const struct HdfSBufImpl * sbuf)302 static const uint8_t *SbufMParcelImplGetData(const struct HdfSBufImpl *sbuf)
303 {
304 return reinterpret_cast<const uint8_t *>(MParcelCast(const_cast<struct HdfSBufImpl *>(sbuf))->GetData());
305 }
306
SbufMParcelImplFlush(struct HdfSBufImpl * sbuf)307 static void SbufMParcelImplFlush(struct HdfSBufImpl *sbuf)
308 {
309 return MParcelCast(sbuf)->FlushBuffer();
310 }
311
SbufMParcelImplGetCapacity(const struct HdfSBufImpl * sbuf)312 static size_t SbufMParcelImplGetCapacity(const struct HdfSBufImpl *sbuf)
313 {
314 return MParcelCast(const_cast<struct HdfSBufImpl *>(sbuf))->GetDataCapacity();
315 }
316
SbufMParcelImplGetDataSize(const struct HdfSBufImpl * sbuf)317 static size_t SbufMParcelImplGetDataSize(const struct HdfSBufImpl *sbuf)
318 {
319 return MParcelCast(const_cast<struct HdfSBufImpl *>(sbuf))->GetDataSize();
320 }
321
SbufMParcelImplSetDataSize(struct HdfSBufImpl * sbuf,size_t size)322 static void SbufMParcelImplSetDataSize(struct HdfSBufImpl *sbuf, size_t size)
323 {
324 MParcelCast(sbuf)->SetDataSize(size);
325 }
326 // LCOV_EXCL_STOP
SbufMParcelImplRecycle(struct HdfSBufImpl * sbuf)327 static void SbufMParcelImplRecycle(struct HdfSBufImpl *sbuf)
328 {
329 SBufMParcelImpl *sbufImpl = reinterpret_cast<SBufMParcelImpl *>(sbuf);
330 delete sbufImpl;
331 }
332
MParcelImplInterfaceAssign(struct HdfSBufImpl * inf)333 static void MParcelImplInterfaceAssign(struct HdfSBufImpl *inf)
334 {
335 inf->writeBuffer = SbufMParcelImplWriteBuffer;
336 inf->writeUnpadBuffer = SbufMParcelImplWriteUnpadBuffer;
337 inf->writeUint64 = SbufMParcelImplWriteUint64;
338 inf->writeUint32 = SbufMParcelImplWriteUint32;
339 inf->writeUint16 = SbufMParcelImplWriteUint16;
340 inf->writeUint8 = SbufMParcelImplWriteUint8;
341 inf->writeInt64 = SbufMParcelImplWriteInt64;
342 inf->writeInt32 = SbufMParcelImplWriteInt32;
343 inf->writeInt16 = SbufMParcelImplWriteInt16;
344 inf->writeInt8 = SbufMParcelImplWriteInt8;
345 inf->writeString = SbufMParcelImplWriteString;
346 inf->writeString16 = SbufMParcelImplWriteString16;
347 inf->writeFileDescriptor = SbufMParcelImplWriteFileDescriptor;
348 inf->writeFloat = SbufMParcelImplWriteFloat;
349 inf->writeDouble = SbufMParcelImplWriteDouble;
350 inf->readDouble = SbufMParcelImplReadDouble;
351 inf->readFloat = SbufMParcelImplReadFloat;
352 inf->readFileDescriptor = SbufMParcelImplReadFileDescriptor;
353 inf->readBuffer = SbufMParcelImplReadBuffer;
354 inf->readUnpadBuffer = SbufMParcelImplReadUnpadBuffer;
355 inf->readUint64 = SbufMParcelImplReadUint64;
356 inf->readUint32 = SbufMParcelImplReadUint32;
357 inf->readUint16 = SbufMParcelImplReadUint16;
358 inf->readUint8 = SbufMParcelImplReadUint8;
359 inf->readInt64 = SbufMParcelImplReadInt64;
360 inf->readInt32 = SbufMParcelImplReadInt32;
361 inf->readInt16 = SbufMParcelImplReadInt16;
362 inf->readInt8 = SbufMParcelImplReadInt8;
363 inf->readString = SbufMParcelImplReadString;
364 inf->readString16 = SbufMParcelImplReadString16;
365 inf->writeRemoteService = SbufMParcelImplWriteRemoteService;
366 inf->readRemoteService = SbufMParcelImplReadRemoteService;
367 inf->getData = SbufMParcelImplGetData;
368 inf->flush = SbufMParcelImplFlush;
369 inf->getCapacity = SbufMParcelImplGetCapacity;
370 inf->getDataSize = SbufMParcelImplGetDataSize;
371 inf->setDataSize = SbufMParcelImplSetDataSize;
372 inf->recycle = SbufMParcelImplRecycle;
373 inf->transDataOwnership = nullptr;
374 inf->copy = nullptr;
375 inf->move = nullptr;
376 }
377
SbufObtainIpc(size_t capacity)378 extern "C" struct HdfSBufImpl *SbufObtainIpc(size_t capacity)
379 {
380 (void)capacity;
381 struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(new MessageParcel());
382 return &sbuf->infImpl;
383 }
384 // LCOV_EXCL_START
385 class SbufAllocator : public Allocator {
386 public:
Realloc(void * data,size_t newSize)387 void *Realloc(void *data, size_t newSize) override
388 {
389 (void)data;
390 (void)newSize;
391 return nullptr;
392 }
393
Alloc(size_t size)394 void *Alloc(size_t size) override
395 {
396 (void)size;
397 return nullptr;
398 }
399
Dealloc(void * data)400 void Dealloc(void *data) override
401 {
402 (void)data;
403 }
404 };
405
SbufBindIpc(uintptr_t base,size_t size)406 extern "C" struct HdfSBufImpl *SbufBindIpc(uintptr_t base, size_t size)
407 {
408 struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(new MessageParcel(new SbufAllocator()));
409 sbuf->realParcel_->ParseFrom(base, size);
410 return &sbuf->infImpl;
411 }
412 // LCOV_EXCL_STOP
ParcelToSbuf(OHOS::MessageParcel * parcel)413 struct HdfSBuf *ParcelToSbuf(OHOS::MessageParcel *parcel)
414 {
415 struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(parcel, false);
416 return HdfSbufTypedObtainInplace(SBUF_IPC, &sbuf->infImpl);
417 }
418
SbufToParcel(struct HdfSBuf * sbuf,OHOS::MessageParcel ** parcel)419 int32_t SbufToParcel(struct HdfSBuf *sbuf, OHOS::MessageParcel **parcel)
420 {
421 if (sbuf == nullptr || parcel == nullptr) {
422 return HDF_ERR_INVALID_PARAM;
423 }
424 struct SBufMParcelImpl *impl = reinterpret_cast<SBufMParcelImpl *>(HdfSbufGetImpl(sbuf));
425 if (impl == nullptr) {
426 return HDF_ERR_INVALID_OBJECT;
427 }
428
429 *parcel = impl->realParcel_;
430 return HDF_SUCCESS;
431 }