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