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 "parcel.h"
17
18 namespace OHOS {
19
20 static const size_t DEFAULT_CPACITY = 204800; // 200K
21
Parcelable()22 Parcelable::Parcelable() : Parcelable(false)
23 {}
24
Parcelable(bool asRemote)25 Parcelable::Parcelable(bool asRemote)
26 {
27 asRemote_ = asRemote;
28 behavior_ = 0;
29 }
30
Parcel(Allocator * allocator)31 Parcel::Parcel(Allocator *allocator)
32 {
33 if (allocator != nullptr) {
34 allocator_ = allocator;
35 } else {
36 allocator_ = new DefaultAllocator();
37 }
38
39 writeCursor_ = 0;
40 readCursor_ = 0;
41
42 data_ = nullptr;
43 dataSize_ = 0;
44 dataCapacity_ = 0;
45
46 maxDataCapacity_ = DEFAULT_CPACITY;
47 objectOffsets_ = nullptr;
48 objectCursor_ = 0;
49 objectsCapacity_ = 0;
50 }
51
Parcel()52 Parcel::Parcel() : Parcel(new DefaultAllocator())
53 {}
54
~Parcel()55 Parcel::~Parcel()
56 {
57 FlushBuffer();
58 delete allocator_;
59 }
60
GetWritableBytes() const61 size_t Parcel::GetWritableBytes() const
62 {
63 return 0;
64 }
65
GetReadableBytes() const66 size_t Parcel::GetReadableBytes() const
67 {
68 return 0;
69 }
70
CalcNewCapacity(size_t minNewCapacity)71 size_t Parcel::CalcNewCapacity(size_t minNewCapacity)
72 {
73 (void)minNewCapacity;
74 return 0;
75 }
76
EnsureWritableCapacity(size_t desireCapacity)77 bool Parcel::EnsureWritableCapacity(size_t desireCapacity)
78 {
79 (void)desireCapacity;
80 return false;
81 }
82
GetDataSize() const83 size_t Parcel::GetDataSize() const
84 {
85 return dataSize_;
86 }
87
GetData() const88 uintptr_t Parcel::GetData() const
89 {
90 return reinterpret_cast<uintptr_t>(data_);
91 }
92
GetObjectOffsets() const93 binder_size_t Parcel::GetObjectOffsets() const
94 {
95 return reinterpret_cast<binder_size_t>(objectOffsets_);
96 }
97
GetOffsetsSize() const98 size_t Parcel::GetOffsetsSize() const
99 {
100 return objectCursor_;
101 }
102
GetDataCapacity() const103 size_t Parcel::GetDataCapacity() const
104 {
105 return dataCapacity_;
106 }
107
SetMaxCapacity(size_t maxCapacity)108 bool Parcel::SetMaxCapacity(size_t maxCapacity)
109 {
110 (void)maxCapacity;
111 return false;
112 }
113
SetAllocator(Allocator * allocator)114 bool Parcel::SetAllocator(Allocator *allocator)
115 {
116 (void)allocator;
117 return false;
118 }
119
CheckOffsets()120 bool Parcel::CheckOffsets()
121 {
122 return false;
123 }
124
InjectOffsets(binder_size_t offsets,size_t offsetSize)125 void Parcel::InjectOffsets(binder_size_t offsets, size_t offsetSize)
126 {
127 (void)offsets;
128 (void)offsetSize;
129 }
130
FlushBuffer()131 void Parcel::FlushBuffer()
132 {
133 if (allocator_ == nullptr) {
134 return;
135 }
136
137 if (data_ != nullptr) {
138 allocator_->Dealloc(data_);
139 dataSize_ = 0;
140 writeCursor_ = 0;
141 readCursor_ = 0;
142 dataCapacity_ = 0;
143 data_ = nullptr;
144 }
145
146 if (objectOffsets_) {
147 objectHolder_.clear();
148 free(objectOffsets_);
149 objectCursor_ = 0;
150 objectOffsets_ = nullptr;
151 objectsCapacity_ = 0;
152 }
153 }
154
SetDataCapacity(size_t newCapacity)155 bool Parcel::SetDataCapacity(size_t newCapacity)
156 {
157 (void)newCapacity;
158 return false;
159 }
160
SetDataSize(size_t dataSize)161 bool Parcel::SetDataSize(size_t dataSize)
162 {
163 (void)dataSize;
164 return false;
165 }
166
WriteDataBytes(const void * data,size_t size)167 bool Parcel::WriteDataBytes(const void *data, size_t size)
168 {
169 (void)data;
170 (void)size;
171 return false;
172 }
173
WritePadBytes(size_t padSize)174 void Parcel::WritePadBytes(size_t padSize)
175 {
176 (void)padSize;
177 }
178
WriteBuffer(const void * data,size_t size)179 bool Parcel::WriteBuffer(const void *data, size_t size)
180 {
181 (void)data;
182 (void)size;
183 return false;
184 }
185
WriteBufferAddTerminator(const void * data,size_t size,size_t typeSize)186 bool Parcel::WriteBufferAddTerminator(const void *data, size_t size, size_t typeSize)
187 {
188 (void)data;
189 (void)size;
190 (void)typeSize;
191 return false;
192 }
193
WriteUnpadBuffer(const void * data,size_t size)194 bool Parcel::WriteUnpadBuffer(const void *data, size_t size)
195 {
196 return WriteBuffer(data, size);
197 }
198
199 template <typename T>
Write(T value)200 bool Parcel::Write(T value)
201 {
202 (void)value;
203 return false;
204 }
205
WriteBool(bool value)206 bool Parcel::WriteBool(bool value)
207 {
208 return Write<int32_t>(static_cast<int32_t>(value));
209 }
210
WriteBoolUnaligned(bool value)211 bool Parcel::WriteBoolUnaligned(bool value)
212 {
213 return Write<bool>(value);
214 }
215
WriteInt8(int8_t value)216 bool Parcel::WriteInt8(int8_t value)
217 {
218 return Write<int32_t>(static_cast<int32_t>(value));
219 }
220
WriteInt8Unaligned(int8_t value)221 bool Parcel::WriteInt8Unaligned(int8_t value)
222 {
223 return Write<int8_t>(value);
224 }
225
WriteInt16(int16_t value)226 bool Parcel::WriteInt16(int16_t value)
227 {
228 return Write<int32_t>(static_cast<int32_t>(value));
229 }
230
WriteInt16Unaligned(int16_t value)231 bool Parcel::WriteInt16Unaligned(int16_t value)
232 {
233 return Write<int16_t>(value);
234 }
235
WriteInt32(int32_t value)236 bool Parcel::WriteInt32(int32_t value)
237 {
238 return Write<int32_t>(value);
239 }
240
WriteInt64(int64_t value)241 bool Parcel::WriteInt64(int64_t value)
242 {
243 return Write<int64_t>(value);
244 }
245
WriteUint8(uint8_t value)246 bool Parcel::WriteUint8(uint8_t value)
247 {
248 return Write<uint32_t>(static_cast<uint32_t>(value));
249 }
250
WriteUint8Unaligned(uint8_t value)251 bool Parcel::WriteUint8Unaligned(uint8_t value)
252 {
253 return Write<uint8_t>(value);
254 }
255
WriteUint16(uint16_t value)256 bool Parcel::WriteUint16(uint16_t value)
257 {
258 return Write<uint32_t>(static_cast<uint32_t>(value));
259 }
260
WriteUint16Unaligned(uint16_t value)261 bool Parcel::WriteUint16Unaligned(uint16_t value)
262 {
263 return Write<uint16_t>(value);
264 }
265
WriteUint32(uint32_t value)266 bool Parcel::WriteUint32(uint32_t value)
267 {
268 return Write<uint32_t>(value);
269 }
270
WriteUint64(uint64_t value)271 bool Parcel::WriteUint64(uint64_t value)
272 {
273 return Write<uint64_t>(value);
274 }
275
WriteFloat(float value)276 bool Parcel::WriteFloat(float value)
277 {
278 return Write<float>(value);
279 }
280
WriteDouble(double value)281 bool Parcel::WriteDouble(double value)
282 {
283 return Write<double>(value);
284 }
285
WritePointer(uintptr_t value)286 bool Parcel::WritePointer(uintptr_t value)
287 {
288 return Write<binder_uintptr_t>(value);
289 }
290
WriteCString(const char * value)291 bool Parcel::WriteCString(const char *value)
292 {
293 (void)value;
294 return false;
295 }
296
WriteString(const std::string & value)297 bool Parcel::WriteString(const std::string &value)
298 {
299 (void)value;
300 return false;
301 }
302
WriteString16(const std::u16string & value)303 bool Parcel::WriteString16(const std::u16string &value)
304 {
305 (void)value;
306 return false;
307 }
308
WriteString16WithLength(const char16_t * value,size_t len)309 bool Parcel::WriteString16WithLength(const char16_t *value, size_t len)
310 {
311 (void)value;
312 (void)len;
313 return false;
314 }
315
WriteString8WithLength(const char * value,size_t len)316 bool Parcel::WriteString8WithLength(const char *value, size_t len)
317 {
318 (void)value;
319 (void)len;
320 return false;
321 }
322
EnsureObjectsCapacity()323 bool Parcel::EnsureObjectsCapacity()
324 {
325 return false;
326 }
327
WriteObjectOffset(binder_size_t offset)328 bool Parcel::WriteObjectOffset(binder_size_t offset)
329 {
330 (void)offset;
331 return false;
332 }
333
WriteRemoteObject(const Parcelable * object)334 bool Parcel::WriteRemoteObject(const Parcelable *object)
335 {
336 (void)object;
337 return false;
338 }
339
WriteParcelable(const Parcelable * object)340 bool Parcel::WriteParcelable(const Parcelable *object)
341 {
342 (void)object;
343 return false;
344 }
345
WriteStrongParcelable(const sptr<Parcelable> & object)346 bool Parcel::WriteStrongParcelable(const sptr<Parcelable> &object)
347 {
348 (void)object;
349 return false;
350 }
351
352 template <typename T>
Read(T & value)353 bool Parcel::Read(T &value)
354 {
355 (void)value;
356 return false;
357 }
358
359 template <typename T>
Read()360 T Parcel::Read()
361 {
362 return 0;
363 }
364
ParseFrom(uintptr_t data,size_t size)365 bool Parcel::ParseFrom(uintptr_t data, size_t size)
366 {
367 (void)data;
368 (void)size;
369 return false;
370 }
371
ReadBuffer(size_t length)372 const uint8_t *Parcel::ReadBuffer(size_t length)
373 {
374 (void)length;
375 return nullptr;
376 }
377
ReadUnpadBuffer(size_t length)378 const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
379 {
380 (void)length;
381 return nullptr;
382 }
383
SkipBytes(size_t bytes)384 void Parcel::SkipBytes(size_t bytes)
385 {
386 (void)bytes;
387 }
388
GetReadPosition()389 size_t Parcel::GetReadPosition()
390 {
391 return readCursor_;
392 }
393
RewindRead(size_t newPosition)394 bool Parcel::RewindRead(size_t newPosition)
395 {
396 (void)newPosition;
397 return false;
398 }
399
GetWritePosition()400 size_t Parcel::GetWritePosition()
401 {
402 return writeCursor_;
403 }
404
RewindWrite(size_t newPosition)405 bool Parcel::RewindWrite(size_t newPosition)
406 {
407 (void)newPosition;
408 return false;
409 }
410
ReadBool()411 bool Parcel::ReadBool()
412 {
413 int32_t temp = Read<int32_t>();
414 return (temp != 0);
415 }
416
ReadBoolUnaligned()417 bool Parcel::ReadBoolUnaligned()
418 {
419 return Read<bool>();
420 }
421
ReadInt8()422 int8_t Parcel::ReadInt8()
423 {
424 int32_t temp = Read<int32_t>();
425 return static_cast<int8_t>(temp);
426 }
427
ReadInt16()428 int16_t Parcel::ReadInt16()
429 {
430 int32_t temp = Read<int32_t>();
431 return static_cast<int16_t>(temp);
432 }
433
ReadInt32()434 int32_t Parcel::ReadInt32()
435 {
436 return Read<int32_t>();
437 }
438
ReadInt64()439 int64_t Parcel::ReadInt64()
440 {
441 return Read<int64_t>();
442 }
443
ReadUint8()444 uint8_t Parcel::ReadUint8()
445 {
446 uint32_t temp = Read<uint32_t>();
447 return static_cast<uint8_t>(temp);
448 }
449
ReadUint16()450 uint16_t Parcel::ReadUint16()
451 {
452 uint32_t temp = Read<uint32_t>();
453 return static_cast<uint16_t>(temp);
454 }
455
ReadUint32()456 uint32_t Parcel::ReadUint32()
457 {
458 return Read<uint32_t>();
459 }
460
ReadUint64()461 uint64_t Parcel::ReadUint64()
462 {
463 return Read<uint64_t>();
464 }
465
ReadFloat()466 float Parcel::ReadFloat()
467 {
468 return Read<float>();
469 }
470
ReadDouble()471 double Parcel::ReadDouble()
472 {
473 return Read<double>();
474 }
475
476 template <typename T>
ReadPadded(T & value)477 bool Parcel::ReadPadded(T &value)
478 {
479 (void)value;
480 return false;
481 }
482
ReadBool(bool & value)483 bool Parcel::ReadBool(bool &value)
484 {
485 return ReadPadded<bool>(value);
486 }
487
ReadInt8(int8_t & value)488 bool Parcel::ReadInt8(int8_t &value)
489 {
490 return ReadPadded<int8_t>(value);
491 }
492
ReadInt8Unaligned(int8_t & value)493 bool Parcel::ReadInt8Unaligned(int8_t &value)
494 {
495 return Read<int8_t>(value);
496 }
497
ReadInt16(int16_t & value)498 bool Parcel::ReadInt16(int16_t &value)
499 {
500 return ReadPadded<int16_t>(value);
501 }
502
ReadInt16Unaligned(int16_t & value)503 bool Parcel::ReadInt16Unaligned(int16_t &value)
504 {
505 return Read<int16_t>(value);
506 }
507
ReadInt32(int32_t & value)508 bool Parcel::ReadInt32(int32_t &value)
509 {
510 return Read<int32_t>(value);
511 }
512
ReadInt64(int64_t & value)513 bool Parcel::ReadInt64(int64_t &value)
514 {
515 return Read<int64_t>(value);
516 }
517
ReadUint8(uint8_t & value)518 bool Parcel::ReadUint8(uint8_t &value)
519 {
520 return ReadPadded<uint8_t>(value);
521 }
522
ReadUint8Unaligned(uint8_t & value)523 bool Parcel::ReadUint8Unaligned(uint8_t &value)
524 {
525 return Read<uint8_t>(value);
526 }
527
ReadUint16(uint16_t & value)528 bool Parcel::ReadUint16(uint16_t &value)
529 {
530 return ReadPadded<uint16_t>(value);
531 }
532
ReadUint16Unaligned(uint16_t & value)533 bool Parcel::ReadUint16Unaligned(uint16_t &value)
534 {
535 return Read<uint16_t>(value);
536 }
537
ReadUint32(uint32_t & value)538 bool Parcel::ReadUint32(uint32_t &value)
539 {
540 return Read<uint32_t>(value);
541 }
542
ReadUint64(uint64_t & value)543 bool Parcel::ReadUint64(uint64_t &value)
544 {
545 return Read<uint64_t>(value);
546 }
547
ReadFloat(float & value)548 bool Parcel::ReadFloat(float &value)
549 {
550 return Read<float>(value);
551 }
552
ReadDouble(double & value)553 bool Parcel::ReadDouble(double &value)
554 {
555 return Read<double>(value);
556 }
557
ReadPointer()558 uintptr_t Parcel::ReadPointer()
559 {
560 return Read<binder_uintptr_t>();
561 }
562
ReadCString()563 const char *Parcel::ReadCString()
564 {
565 return nullptr;
566 }
567
ReadString()568 const std::string Parcel::ReadString()
569 {
570 return "";
571 }
572
ReadString(std::string & value)573 bool Parcel::ReadString(std::string &value)
574 {
575 (void)value;
576 return false;
577 }
578
ReadString16()579 const std::u16string Parcel::ReadString16()
580 {
581 return std::u16string();
582 }
583
ReadString16(std::u16string & value)584 bool Parcel::ReadString16(std::u16string &value)
585 {
586 (void)value;
587 return false;
588 }
589
ReadString16WithLength(int32_t & readLength)590 const std::u16string Parcel::ReadString16WithLength(int32_t &readLength)
591 {
592 (void)readLength;
593 return std::u16string();
594 }
595
ReadString8WithLength(int32_t & readLength)596 const std::string Parcel::ReadString8WithLength(int32_t &readLength)
597 {
598 (void)readLength;
599 return std::string();
600 }
601
602 template <typename T1, typename T2>
WriteVector(const std::vector<T1> & val,bool (Parcel::* Write)(T2))603 bool Parcel::WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2))
604 {
605 (void)val;
606 return false;
607 }
608
WriteBoolVector(const std::vector<bool> & val)609 bool Parcel::WriteBoolVector(const std::vector<bool> &val)
610 {
611 return WriteVector(val, &Parcel::WriteBool);
612 }
613
WriteInt8Vector(const std::vector<int8_t> & val)614 bool Parcel::WriteInt8Vector(const std::vector<int8_t> &val)
615 {
616 return WriteVector(val, &Parcel::WriteInt8Unaligned);
617 }
618
WriteInt16Vector(const std::vector<int16_t> & val)619 bool Parcel::WriteInt16Vector(const std::vector<int16_t> &val)
620 {
621 return WriteVector(val, &Parcel::WriteInt16);
622 }
623
WriteInt32Vector(const std::vector<int32_t> & val)624 bool Parcel::WriteInt32Vector(const std::vector<int32_t> &val)
625 {
626 return WriteVector(val, &Parcel::WriteInt32);
627 }
628
WriteInt64Vector(const std::vector<int64_t> & val)629 bool Parcel::WriteInt64Vector(const std::vector<int64_t> &val)
630 {
631 return WriteVector(val, &Parcel::WriteInt64);
632 }
633
WriteUInt8Vector(const std::vector<uint8_t> & val)634 bool Parcel::WriteUInt8Vector(const std::vector<uint8_t> &val)
635 {
636 return WriteVector(val, &Parcel::WriteUint8Unaligned);
637 }
638
WriteUInt16Vector(const std::vector<uint16_t> & val)639 bool Parcel::WriteUInt16Vector(const std::vector<uint16_t> &val)
640 {
641 return WriteVector(val, &Parcel::WriteUint16Unaligned);
642 }
643
WriteUInt32Vector(const std::vector<uint32_t> & val)644 bool Parcel::WriteUInt32Vector(const std::vector<uint32_t> &val)
645 {
646 return WriteVector(val, &Parcel::WriteUint32);
647 }
648
WriteUInt64Vector(const std::vector<uint64_t> & val)649 bool Parcel::WriteUInt64Vector(const std::vector<uint64_t> &val)
650 {
651 return WriteVector(val, &Parcel::WriteUint64);
652 }
653
WriteFloatVector(const std::vector<float> & val)654 bool Parcel::WriteFloatVector(const std::vector<float> &val)
655 {
656 return WriteVector(val, &Parcel::WriteFloat);
657 }
658
WriteDoubleVector(const std::vector<double> & val)659 bool Parcel::WriteDoubleVector(const std::vector<double> &val)
660 {
661 return WriteVector(val, &Parcel::WriteDouble);
662 }
663
WriteStringVector(const std::vector<std::string> & val)664 bool Parcel::WriteStringVector(const std::vector<std::string> &val)
665 {
666 return WriteVector(val, &Parcel::WriteString);
667 }
668
WriteString16Vector(const std::vector<std::u16string> & val)669 bool Parcel::WriteString16Vector(const std::vector<std::u16string> &val)
670 {
671 return WriteVector(val, &Parcel::WriteString16);
672 }
673
674 template <typename T>
ReadVector(std::vector<T> * val,bool (Parcel::* Read)(T &))675 bool Parcel::ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &))
676 {
677 (void)val;
678 return false;
679 }
680
ReadBoolVector(std::vector<bool> * val)681 bool Parcel::ReadBoolVector(std::vector<bool> *val)
682 {
683 (void)val;
684 return false;
685 }
686
ReadInt8Vector(std::vector<int8_t> * val)687 bool Parcel::ReadInt8Vector(std::vector<int8_t> *val)
688 {
689 return ReadVector(val, &Parcel::ReadInt8Unaligned);
690 }
691
ReadInt16Vector(std::vector<int16_t> * val)692 bool Parcel::ReadInt16Vector(std::vector<int16_t> *val)
693 {
694 return ReadVector(val, &Parcel::ReadInt16);
695 }
696
ReadInt32Vector(std::vector<int32_t> * val)697 bool Parcel::ReadInt32Vector(std::vector<int32_t> *val)
698 {
699 return ReadVector(val, &Parcel::ReadInt32);
700 }
701
ReadInt64Vector(std::vector<int64_t> * val)702 bool Parcel::ReadInt64Vector(std::vector<int64_t> *val)
703 {
704 return ReadVector(val, &Parcel::ReadInt64);
705 }
706
ReadUInt8Vector(std::vector<uint8_t> * val)707 bool Parcel::ReadUInt8Vector(std::vector<uint8_t> *val)
708 {
709 return ReadVector(val, &Parcel::ReadUint8Unaligned);
710 }
711
ReadUInt16Vector(std::vector<uint16_t> * val)712 bool Parcel::ReadUInt16Vector(std::vector<uint16_t> *val)
713 {
714 return ReadVector(val, &Parcel::ReadUint16Unaligned);
715 }
716
ReadUInt32Vector(std::vector<uint32_t> * val)717 bool Parcel::ReadUInt32Vector(std::vector<uint32_t> *val)
718 {
719 return ReadVector(val, &Parcel::ReadUint32);
720 }
721
ReadUInt64Vector(std::vector<uint64_t> * val)722 bool Parcel::ReadUInt64Vector(std::vector<uint64_t> *val)
723 {
724 return ReadVector(val, &Parcel::ReadUint64);
725 }
726
ReadFloatVector(std::vector<float> * val)727 bool Parcel::ReadFloatVector(std::vector<float> *val)
728 {
729 return ReadVector(val, &Parcel::ReadFloat);
730 }
731
ReadDoubleVector(std::vector<double> * val)732 bool Parcel::ReadDoubleVector(std::vector<double> *val)
733 {
734 return ReadVector(val, &Parcel::ReadDouble);
735 }
736
ReadStringVector(std::vector<std::string> * val)737 bool Parcel::ReadStringVector(std::vector<std::string> *val)
738 {
739 (void)val;
740 return false;
741 }
742
ReadString16Vector(std::vector<std::u16string> * val)743 bool Parcel::ReadString16Vector(std::vector<std::u16string> *val)
744 {
745 (void)val;
746 return false;
747 }
748 } // namespace OHOS
749