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 #include <limits.h>
16 #include "securec.h"
17 #include "utils_log.h"
18 #include "parcel.h"
19
20 namespace OHOS {
21 static const size_t DEFAULT_CPACITY = 204800; // 200K
22 static const size_t CAPACITY_THRESHOLD = 4096; // 4k
23 constexpr int SIZE = 2;
24
Parcelable()25 Parcelable::Parcelable() : Parcelable(false)
26 {}
27
Parcelable(bool asRemote)28 Parcelable::Parcelable(bool asRemote)
29 {
30 asRemote_ = asRemote;
31 behavior_ = 0;
32 }
33
Parcel(Allocator * allocator)34 Parcel::Parcel(Allocator *allocator)
35 {
36 if (allocator != nullptr) {
37 allocator_ = allocator;
38 } else {
39 allocator_ = new DefaultAllocator();
40 }
41
42 writeCursor_ = 0;
43 readCursor_ = 0;
44
45 data_ = nullptr;
46 dataSize_ = 0;
47 dataCapacity_ = 0;
48
49 maxDataCapacity_ = DEFAULT_CPACITY;
50 objectOffsets_ = nullptr;
51 objectCursor_ = 0;
52 objectsCapacity_ = 0;
53 }
54
Parcel()55 Parcel::Parcel() : Parcel(new DefaultAllocator())
56 {}
57
~Parcel()58 Parcel::~Parcel()
59 {
60 FlushBuffer();
61 delete allocator_;
62 }
63
GetWritableBytes() const64 size_t Parcel::GetWritableBytes() const
65 {
66 if (dataCapacity_ > writeCursor_) {
67 return dataCapacity_ - writeCursor_;
68 }
69
70 return 0;
71 }
72
GetReadableBytes() const73 size_t Parcel::GetReadableBytes() const
74 {
75 if (dataSize_ > readCursor_) {
76 return dataSize_ - readCursor_;
77 }
78
79 return 0;
80 }
81
CalcNewCapacity(size_t minNewCapacity)82 size_t Parcel::CalcNewCapacity(size_t minNewCapacity)
83 {
84 size_t threshold = CAPACITY_THRESHOLD;
85
86 if (minNewCapacity == threshold) {
87 return threshold;
88 }
89
90 if (minNewCapacity > threshold) {
91 size_t newCapacity = minNewCapacity / threshold * threshold;
92
93 if ((maxDataCapacity_ > 0) && (newCapacity > maxDataCapacity_ - threshold)) {
94 newCapacity = maxDataCapacity_;
95 } else {
96 newCapacity += threshold;
97 }
98
99 return newCapacity;
100 }
101
102 size_t newCapacity = 64;
103
104 while (newCapacity < minNewCapacity) {
105 newCapacity = newCapacity * SIZE;
106 }
107
108 if ((maxDataCapacity_ > 0) && (newCapacity > maxDataCapacity_)) {
109 newCapacity = maxDataCapacity_;
110 }
111
112 return newCapacity;
113 }
114
EnsureWritableCapacity(size_t desireCapacity)115 bool Parcel::EnsureWritableCapacity(size_t desireCapacity)
116 {
117 if (!writable_) {
118 UTILS_LOGW("this parcel data is alloc by driver, which is can not be writen");
119 return false;
120 }
121 if (desireCapacity <= GetWritableBytes()) {
122 return true;
123 }
124
125 size_t minNewCapacity = desireCapacity + writeCursor_;
126 size_t newCapacity = CalcNewCapacity(minNewCapacity);
127 if ((newCapacity <= dataCapacity_) || (newCapacity < minNewCapacity)) {
128 UTILS_LOGW("Failed to ensure parcel capacity, newCapacity = %{public}zu, dataCapacity_ = %{public}zu, "
129 "minNewCapacity = %{public}zu",
130 newCapacity, dataCapacity_, minNewCapacity);
131 return false;
132 }
133
134 if (allocator_ != nullptr) {
135 void *newData = allocator_->Realloc(data_, newCapacity);
136 if (newData != nullptr) {
137 data_ = reinterpret_cast<uint8_t *>(newData);
138 dataCapacity_ = newCapacity;
139 return true;
140 }
141 UTILS_LOGW("Failed to realloc parcel capacity, newCapacity = %{public}zu, dataCapacity_ = %{public}zu",
142 newCapacity, dataCapacity_);
143 }
144
145 return false;
146 }
147
GetDataSize() const148 size_t Parcel::GetDataSize() const
149 {
150 return dataSize_;
151 }
152
GetData() const153 uintptr_t Parcel::GetData() const
154 {
155 return reinterpret_cast<uintptr_t>(data_);
156 }
157
GetObjectOffsets() const158 binder_size_t Parcel::GetObjectOffsets() const
159 {
160 return reinterpret_cast<binder_size_t>(objectOffsets_);
161 }
162
GetOffsetsSize() const163 size_t Parcel::GetOffsetsSize() const
164 {
165 return objectCursor_;
166 }
167
GetDataCapacity() const168 size_t Parcel::GetDataCapacity() const
169 {
170 return dataCapacity_;
171 }
172
SetMaxCapacity(size_t maxCapacity)173 bool Parcel::SetMaxCapacity(size_t maxCapacity)
174 {
175 if (maxCapacity > maxDataCapacity_) {
176 maxDataCapacity_ = maxCapacity;
177 return true;
178 }
179 return true;
180 }
181
SetAllocator(Allocator * allocator)182 bool Parcel::SetAllocator(Allocator *allocator)
183 {
184 if ((allocator == nullptr) || (allocator_ == allocator)) {
185 return false;
186 }
187
188 if ((data_ != nullptr) && (dataSize_ > 0)) {
189 if (allocator_ == nullptr) {
190 return false;
191 }
192
193 void *newData = allocator->Alloc(dataSize_);
194 if (newData == nullptr) {
195 UTILS_LOGE("Failed to alloc parcel size, dataSize_ = %{public}zu", dataSize_);
196 return false;
197 }
198
199 if (memcpy_s(newData, dataSize_, data_, dataSize_) != EOK) {
200 allocator->Dealloc(newData);
201 return false;
202 }
203 allocator_->Dealloc(data_);
204 data_ = reinterpret_cast<uint8_t *>(newData);
205 dataCapacity_ = dataSize_;
206 }
207
208 delete allocator_;
209 allocator_ = allocator;
210 return true;
211 }
212
CheckOffsets()213 bool Parcel::CheckOffsets()
214 {
215 size_t readPos = readCursor_;
216 if ((readPos + sizeof(parcel_flat_binder_object)) > dataSize_) {
217 return false;
218 }
219
220 size_t objSize = objectCursor_;
221 binder_size_t *objects = objectOffsets_;
222 size_t objCount = 0;
223 while (objCount < objSize) {
224 if (objects[objCount] == readPos) {
225 return true;
226 }
227 objCount++;
228 }
229 UTILS_LOGW("CheckOffsets Invalid obj: obj not found.");
230 return false;
231 }
232
InjectOffsets(size_t offsets,size_t offsetSize)233 void Parcel::InjectOffsets(size_t offsets, size_t offsetSize)
234 {
235 if (offsetSize <= 0) {
236 return;
237 }
238
239 auto *newObjectOffsets = reinterpret_cast<binder_size_t *>(offsets);
240 for (size_t index = 0; index < offsetSize; index++) {
241 if (EnsureObjectsCapacity()) {
242 WriteObjectOffset(newObjectOffsets[index]);
243 }
244 }
245 }
246
FlushBuffer()247 void Parcel::FlushBuffer()
248 {
249 if (allocator_ == nullptr) {
250 return;
251 }
252
253 if (data_ != nullptr) {
254 allocator_->Dealloc(data_);
255 dataSize_ = 0;
256 writeCursor_ = 0;
257 readCursor_ = 0;
258 dataCapacity_ = 0;
259 data_ = nullptr;
260 }
261
262 if (objectOffsets_) {
263 objectHolder_.clear();
264 free(objectOffsets_);
265 objectCursor_ = 0;
266 objectOffsets_ = nullptr;
267 }
268 }
269
SetDataCapacity(size_t newCapacity)270 bool Parcel::SetDataCapacity(size_t newCapacity)
271 {
272 if (allocator_ == nullptr || dataSize_ >= newCapacity) {
273 return false;
274 }
275
276 void *newData = allocator_->Realloc(data_, newCapacity);
277 if (newData != nullptr) {
278 data_ = reinterpret_cast<uint8_t *>(newData);
279 dataCapacity_ = newCapacity;
280 return true;
281 }
282 return false;
283 }
284
SetDataSize(size_t dataSize)285 bool Parcel::SetDataSize(size_t dataSize)
286 {
287 if (dataSize > dataCapacity_) {
288 return false;
289 }
290
291 dataSize_ = dataSize;
292 return true;
293 }
294
WriteDataBytes(const void * data,size_t size)295 bool Parcel::WriteDataBytes(const void *data, size_t size)
296 {
297 void *dest = data_ + writeCursor_;
298 size_t writableBytes = GetWritableBytes();
299 if (memcpy_s(dest, writableBytes, data, size) != EOK) {
300 return false;
301 }
302 writeCursor_ += size;
303 dataSize_ += size;
304 return true;
305 }
306
WritePadBytes(size_t padSize)307 void Parcel::WritePadBytes(size_t padSize)
308 {
309 uint8_t *dest = data_ + writeCursor_;
310 static const int MAX_MASK_NUM = 4;
311 #if __BYTE_ORDER == __LITTLE_ENDIAN
312 static const size_t mask[MAX_MASK_NUM] = { 0xFFFFFFFF, 0x00ffffff, 0x0000ffff, 0x000000ff };
313 #else
314 static const size_t mask[MAX_MASK_NUM] = { 0xFFFFFFFF, 0xffffff00, 0xffff0000, 0xff000000 };
315 #endif
316 *reinterpret_cast<uint32_t *>(dest + padSize - MAX_MASK_NUM) &= mask[padSize];
317 writeCursor_ += padSize;
318 dataSize_ += padSize;
319 }
320
WriteBuffer(const void * data,size_t size)321 bool Parcel::WriteBuffer(const void *data, size_t size)
322 {
323 if (data == nullptr || size == 0) {
324 return false;
325 }
326
327 size_t padSize = GetPadSize(size);
328 size_t desireCapacity = size + padSize;
329
330 if (desireCapacity < size || desireCapacity < padSize) {
331 return false;
332 }
333
334 if (EnsureWritableCapacity(desireCapacity)) {
335 if (!WriteDataBytes(data, size)) {
336 return false;
337 }
338 WritePadBytes(padSize);
339 return true;
340 }
341
342 return false;
343 }
344
WriteUnpadBuffer(const void * data,size_t size)345 bool Parcel::WriteUnpadBuffer(const void *data, size_t size)
346 {
347 return WriteBuffer(data, size);
348 }
349
350 template <typename T>
Write(T value)351 bool Parcel::Write(T value)
352 {
353 size_t desireCapacity = sizeof(T);
354
355 if (EnsureWritableCapacity(desireCapacity)) {
356 *reinterpret_cast<T *>(data_ + writeCursor_) = value;
357 writeCursor_ += desireCapacity;
358 dataSize_ += desireCapacity;
359 return true;
360 }
361
362 return false;
363 }
364
WriteBool(bool value)365 bool Parcel::WriteBool(bool value)
366 {
367 return Write<int32_t>(static_cast<int32_t>(value));
368 }
369
WriteBoolUnaligned(bool value)370 bool Parcel::WriteBoolUnaligned(bool value)
371 {
372 return Write<bool>(value);
373 }
374
WriteInt8(int8_t value)375 bool Parcel::WriteInt8(int8_t value)
376 {
377 return Write<int32_t>(static_cast<int32_t>(value));
378 }
379
WriteInt8Unaligned(int8_t value)380 bool Parcel::WriteInt8Unaligned(int8_t value)
381 {
382 return Write<int8_t>(value);
383 }
384
WriteInt16(int16_t value)385 bool Parcel::WriteInt16(int16_t value)
386 {
387 return Write<int32_t>(static_cast<int32_t>(value));
388 }
389
WriteInt16Unaligned(int16_t value)390 bool Parcel::WriteInt16Unaligned(int16_t value)
391 {
392 return Write<int16_t>(value);
393 }
394
WriteInt32(int32_t value)395 bool Parcel::WriteInt32(int32_t value)
396 {
397 return Write<int32_t>(value);
398 }
399
WriteInt64(int64_t value)400 bool Parcel::WriteInt64(int64_t value)
401 {
402 return Write<int64_t>(value);
403 }
404
WriteUint8(uint8_t value)405 bool Parcel::WriteUint8(uint8_t value)
406 {
407 return Write<uint32_t>(static_cast<uint32_t>(value));
408 }
409
WriteUint8Unaligned(uint8_t value)410 bool Parcel::WriteUint8Unaligned(uint8_t value)
411 {
412 return Write<uint8_t>(value);
413 }
414
WriteUint16(uint16_t value)415 bool Parcel::WriteUint16(uint16_t value)
416 {
417 return Write<uint32_t>(static_cast<uint32_t>(value));
418 }
419
WriteUint16Unaligned(uint16_t value)420 bool Parcel::WriteUint16Unaligned(uint16_t value)
421 {
422 return Write<uint16_t>(value);
423 }
424
WriteUint32(uint32_t value)425 bool Parcel::WriteUint32(uint32_t value)
426 {
427 return Write<uint32_t>(value);
428 }
429
WriteUint64(uint64_t value)430 bool Parcel::WriteUint64(uint64_t value)
431 {
432 return Write<uint64_t>(value);
433 }
434
WriteFloat(float value)435 bool Parcel::WriteFloat(float value)
436 {
437 return Write<float>(value);
438 }
439
WriteDouble(double value)440 bool Parcel::WriteDouble(double value)
441 {
442 return Write<double>(value);
443 }
444
WritePointer(uintptr_t value)445 bool Parcel::WritePointer(uintptr_t value)
446 {
447 return Write<binder_uintptr_t>(value);
448 }
449
WriteCString(const char * value)450 bool Parcel::WriteCString(const char *value)
451 {
452 if (value == nullptr) {
453 return false;
454 }
455 int32_t dataLength = strlen(value);
456 int32_t desireCapacity = (dataLength + 1) * sizeof(char);
457 return WriteBuffer(value, desireCapacity);
458 }
459
WriteString(const std::string & value)460 bool Parcel::WriteString(const std::string &value)
461 {
462 if (value.data() == nullptr) {
463 return WriteInt32(-1);
464 }
465
466 int32_t dataLength = value.length();
467 int32_t desireCapacity = dataLength + sizeof(char);
468
469 if (!Write<int32_t>(dataLength)) {
470 return false;
471 }
472
473 return WriteBuffer(value.data(), desireCapacity);
474 }
475
WriteString16(const std::u16string & value)476 bool Parcel::WriteString16(const std::u16string &value)
477 {
478 if (value.data() == nullptr) {
479 return WriteInt32(-1);
480 }
481
482 int32_t dataLength = value.length();
483 int32_t desireCapacity = (dataLength + 1) * sizeof(char16_t);
484
485 if (!Write<int32_t>(dataLength)) {
486 return false;
487 }
488
489 return WriteBuffer(value.data(), desireCapacity);
490 }
491
WriteString16WithLength(const char16_t * value,size_t len)492 bool Parcel::WriteString16WithLength(const char16_t *value, size_t len)
493 {
494 if (!value) {
495 return WriteInt32(-1);
496 }
497
498 int32_t dataLength = len;
499 int32_t desireCapacity = (dataLength + 1) * sizeof(char16_t);
500 std::u16string u16str(reinterpret_cast<const char16_t *>(value), len);
501
502 if (!Write<int32_t>(dataLength)) {
503 return false;
504 }
505
506 return WriteBuffer(u16str.data(), desireCapacity);
507 }
508
EnsureObjectsCapacity()509 bool Parcel::EnsureObjectsCapacity()
510 {
511 if ((objectsCapacity_ - objectCursor_) >= 1) {
512 return true;
513 }
514
515 if (allocator_ == nullptr) {
516 return false;
517 }
518
519 const int NEW_CAPACITY_ADD = 2;
520 const int NEW_CAPACITY_MULTI = 3;
521 const int NEW_CAPACITY_DIV = 2;
522 size_t newCapacity = ((objectsCapacity_ + NEW_CAPACITY_ADD) * NEW_CAPACITY_MULTI) / NEW_CAPACITY_DIV;
523 size_t newBytes = newCapacity * sizeof(binder_size_t);
524
525 void *newOffsets = realloc(objectOffsets_, newBytes);
526 if (newOffsets == nullptr) {
527 return false;
528 }
529
530 objectOffsets_ = reinterpret_cast<binder_size_t *>(newOffsets);
531 objectsCapacity_ = newCapacity;
532 return true;
533 }
534
WriteObjectOffset(size_t offset)535 bool Parcel::WriteObjectOffset(size_t offset)
536 {
537 if (offset > dataSize_) {
538 return false;
539 }
540
541 for (size_t index = 0; index < objectCursor_; index++) {
542 if (objectOffsets_[index] == offset) {
543 return false;
544 }
545 }
546
547 objectOffsets_[objectCursor_] = offset;
548 objectCursor_++;
549 return true;
550 }
551
WriteRemoteObject(const Parcelable * object)552 bool Parcel::WriteRemoteObject(const Parcelable *object)
553 {
554 size_t placeholder = writeCursor_;
555 if ((object == nullptr) || (!object->asRemote_)) {
556 return false;
557 }
558
559 if (!EnsureObjectsCapacity()) {
560 return false;
561 }
562
563 if (!object->Marshalling(*this)) {
564 return false;
565 }
566
567 WriteObjectOffset(placeholder);
568
569 if (object->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT)) {
570 objectHolder_.push_back(const_cast<Parcelable *>(object));
571 }
572
573 return true;
574 }
575
WriteParcelable(const Parcelable * object)576 bool Parcel::WriteParcelable(const Parcelable *object)
577 {
578 size_t placeholder = writeCursor_;
579 size_t restorSize = dataSize_;
580
581 if (object == nullptr) {
582 return WriteInt32(0);
583 }
584
585 if (!object->asRemote_) {
586 if (!WriteInt32(1)) {
587 return false;
588 }
589
590 return object->Marshalling(*this);
591 }
592
593 if (!WriteInt32(1)) {
594 return false;
595 }
596
597 if (WriteRemoteObject(const_cast<Parcelable*>(object))) {
598 return true;
599 }
600 writeCursor_ = placeholder;
601 dataSize_ = restorSize;
602 return false;
603 }
604
WriteStrongParcelable(const sptr<Parcelable> & object)605 bool Parcel::WriteStrongParcelable(const sptr<Parcelable> &object)
606 {
607 if (object == nullptr) {
608 WriteInt32(0);
609 return true;
610 }
611
612 object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
613 return WriteParcelable(object.GetRefPtr());
614 }
615
616 template <typename T>
Read(T & value)617 bool Parcel::Read(T &value)
618 {
619 size_t desireCapacity = sizeof(T);
620
621 if (desireCapacity <= GetReadableBytes()) {
622 const void *data = data_ + readCursor_;
623 readCursor_ += desireCapacity;
624 value = *reinterpret_cast<const T *>(data);
625 return true;
626 }
627
628 return false;
629 }
630
631 template <typename T>
Read()632 T Parcel::Read()
633 {
634 T lvalue{ };
635 return Read<T>(lvalue) ? lvalue : 0;
636 }
637
ParseFrom(uintptr_t data,size_t size)638 bool Parcel::ParseFrom(uintptr_t data, size_t size)
639 {
640 if (data_ != nullptr) {
641 return false;
642 }
643
644 data_ = reinterpret_cast<uint8_t *>(data);
645 dataCapacity_ = size;
646 dataSize_ = size;
647 writable_ = false;
648 return true;
649 }
650
ReadBuffer(size_t length)651 const uint8_t *Parcel::ReadBuffer(size_t length)
652 {
653 if (GetReadableBytes() >= length) {
654 uint8_t *buffer = data_ + readCursor_;
655 readCursor_ += length;
656 return buffer;
657 }
658
659 return nullptr;
660 }
661
ReadUnpadBuffer(size_t length)662 const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
663 {
664 if (GetReadableBytes() >= length) {
665 uint8_t *buffer = data_ + readCursor_;
666 readCursor_ += length;
667 SkipBytes(GetPadSize(length));
668 return buffer;
669 }
670
671 return nullptr;
672 }
673
SkipBytes(size_t bytes)674 void Parcel::SkipBytes(size_t bytes)
675 {
676 if (GetReadableBytes() >= bytes) {
677 readCursor_ += bytes;
678 } else if (readCursor_ < dataCapacity_) {
679 readCursor_ = dataCapacity_;
680 }
681 }
682
GetReadPosition()683 size_t Parcel::GetReadPosition()
684 {
685 return readCursor_;
686 }
687
RewindRead(size_t newPosition)688 bool Parcel::RewindRead(size_t newPosition)
689 {
690 if (newPosition > dataSize_) {
691 return false;
692 }
693 readCursor_ = newPosition;
694 return true;
695 }
696
GetWritePosition()697 size_t Parcel::GetWritePosition()
698 {
699 return writeCursor_;
700 }
701
RewindWrite(size_t newPosition)702 bool Parcel::RewindWrite(size_t newPosition)
703 {
704 if (newPosition > dataSize_) {
705 return false;
706 }
707 writeCursor_ = newPosition;
708 dataSize_ = newPosition;
709 return true;
710 }
711
ReadBool()712 bool Parcel::ReadBool()
713 {
714 int32_t temp = Read<int32_t>();
715 return (temp != 0);
716 }
717
ReadBoolUnaligned()718 bool Parcel::ReadBoolUnaligned()
719 {
720 return Read<bool>();
721 }
722
ReadInt8()723 int8_t Parcel::ReadInt8()
724 {
725 int32_t temp = Read<int32_t>();
726 return static_cast<int8_t>(temp);
727 }
728
ReadInt16()729 int16_t Parcel::ReadInt16()
730 {
731 int32_t temp = Read<int32_t>();
732 return static_cast<int16_t>(temp);
733 }
734
ReadInt32()735 int32_t Parcel::ReadInt32()
736 {
737 return Read<int32_t>();
738 }
739
ReadInt64()740 int64_t Parcel::ReadInt64()
741 {
742 return Read<int64_t>();
743 }
744
ReadUint8()745 uint8_t Parcel::ReadUint8()
746 {
747 uint32_t temp = Read<uint32_t>();
748 return static_cast<uint8_t>(temp);
749 }
750
ReadUint16()751 uint16_t Parcel::ReadUint16()
752 {
753 uint32_t temp = Read<uint32_t>();
754 return static_cast<uint16_t>(temp);
755 }
756
ReadUint32()757 uint32_t Parcel::ReadUint32()
758 {
759 return Read<uint32_t>();
760 }
761
ReadUint64()762 uint64_t Parcel::ReadUint64()
763 {
764 return Read<uint64_t>();
765 }
766
ReadFloat()767 float Parcel::ReadFloat()
768 {
769 return Read<float>();
770 }
771
ReadDouble()772 double Parcel::ReadDouble()
773 {
774 return Read<double>();
775 }
776
777 template <typename T>
ReadPadded(T & value)778 bool Parcel::ReadPadded(T &value)
779 {
780 int32_t temp;
781 bool result = Read<int32_t>(temp);
782 if (result) {
783 value = static_cast<T>(temp);
784 }
785
786 return result;
787 }
788
ReadBool(bool & value)789 bool Parcel::ReadBool(bool &value)
790 {
791 return ReadPadded<bool>(value);
792 }
793
ReadInt8(int8_t & value)794 bool Parcel::ReadInt8(int8_t &value)
795 {
796 return ReadPadded<int8_t>(value);
797 }
798
ReadInt8Unaligned(int8_t & value)799 bool Parcel::ReadInt8Unaligned(int8_t &value)
800 {
801 return Read<int8_t>(value);
802 }
803
ReadInt16(int16_t & value)804 bool Parcel::ReadInt16(int16_t &value)
805 {
806 return ReadPadded<int16_t>(value);
807 }
808
ReadInt16Unaligned(int16_t & value)809 bool Parcel::ReadInt16Unaligned(int16_t &value)
810 {
811 return Read<int16_t>(value);
812 }
813
ReadInt32(int32_t & value)814 bool Parcel::ReadInt32(int32_t &value)
815 {
816 return Read<int32_t>(value);
817 }
818
ReadInt64(int64_t & value)819 bool Parcel::ReadInt64(int64_t &value)
820 {
821 return Read<int64_t>(value);
822 }
823
ReadUint8(uint8_t & value)824 bool Parcel::ReadUint8(uint8_t &value)
825 {
826 return ReadPadded<uint8_t>(value);
827 }
828
ReadUint8Unaligned(uint8_t & value)829 bool Parcel::ReadUint8Unaligned(uint8_t &value)
830 {
831 return Read<uint8_t>(value);
832 }
833
ReadUint16(uint16_t & value)834 bool Parcel::ReadUint16(uint16_t &value)
835 {
836 return ReadPadded<uint16_t>(value);
837 }
838
ReadUint16Unaligned(uint16_t & value)839 bool Parcel::ReadUint16Unaligned(uint16_t &value)
840 {
841 return Read<uint16_t>(value);
842 }
843
ReadUint32(uint32_t & value)844 bool Parcel::ReadUint32(uint32_t &value)
845 {
846 return Read<uint32_t>(value);
847 }
848
ReadUint64(uint64_t & value)849 bool Parcel::ReadUint64(uint64_t &value)
850 {
851 return Read<uint64_t>(value);
852 }
853
ReadFloat(float & value)854 bool Parcel::ReadFloat(float &value)
855 {
856 return Read<float>(value);
857 }
858
ReadDouble(double & value)859 bool Parcel::ReadDouble(double &value)
860 {
861 return Read<double>(value);
862 }
863
ReadPointer()864 uintptr_t Parcel::ReadPointer()
865 {
866 return Read<binder_uintptr_t>();
867 }
868
ReadCString()869 const char *Parcel::ReadCString()
870 {
871 size_t oldCursor = readCursor_;
872 const size_t avail = GetReadableBytes();
873 const char* cstr = reinterpret_cast<const char*>(data_ + readCursor_);
874 const char* eos = reinterpret_cast<const char*>(memchr(cstr, 0, avail));
875 if (eos != nullptr) {
876 const size_t dataLength = eos - cstr;
877 readCursor_ += (dataLength + 1);
878 SkipBytes(GetPadSize(dataLength + 1));
879 return cstr;
880 }
881 readCursor_ = oldCursor;
882 return nullptr;
883 }
884
ReadString()885 const std::string Parcel::ReadString()
886 {
887 int32_t dataLength = 0;
888 size_t oldCursor = readCursor_;
889
890 if (!Read<int32_t>(dataLength) || dataLength < 0) {
891 return std::string();
892 }
893
894 size_t readCapacity = dataLength + 1;
895 if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
896 const uint8_t *dest = ReadBuffer(readCapacity);
897 if (dest != nullptr) {
898 const auto *str = reinterpret_cast<const char *>(dest);
899 SkipBytes(GetPadSize(readCapacity));
900 if (str[dataLength] == 0) {
901 return std::string(str, dataLength);
902 }
903 }
904 }
905
906 readCursor_ = oldCursor;
907 return std::string();
908 }
909
ReadString(std::string & value)910 bool Parcel::ReadString(std::string &value)
911 {
912 int32_t dataLength = 0;
913 size_t oldCursor = readCursor_;
914
915 if (!Read<int32_t>(dataLength) || dataLength < 0) {
916 value = std::string();
917 return false;
918 }
919
920 size_t readCapacity = dataLength + 1;
921 if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
922 const uint8_t *dest = ReadBuffer(readCapacity);
923 if (dest != nullptr) {
924 const auto *str = reinterpret_cast<const char *>(dest);
925 SkipBytes(GetPadSize(readCapacity));
926 if (str[dataLength] == 0) {
927 value = std::string(str, dataLength);
928 return true;
929 }
930 }
931 }
932
933 readCursor_ = oldCursor;
934 value = std::string();
935 return false;
936 }
937
ReadString16()938 const std::u16string Parcel::ReadString16()
939 {
940 int32_t dataLength = 0;
941 size_t oldCursor = readCursor_;
942
943 if (!Read<int32_t>(dataLength) || dataLength < 0) {
944 return std::u16string();
945 }
946
947 size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
948 if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
949 const uint8_t *str = ReadBuffer(readCapacity);
950 if (str != nullptr) {
951 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
952 SkipBytes(GetPadSize(readCapacity));
953 if (u16Str[dataLength] == 0) {
954 return std::u16string(u16Str, dataLength);
955 }
956 }
957 }
958
959 readCursor_ = oldCursor;
960 return std::u16string();
961 }
962
ReadString16(std::u16string & value)963 bool Parcel::ReadString16(std::u16string &value)
964 {
965 int32_t dataLength = 0;
966 size_t oldCursor = readCursor_;
967
968 if (!Read<int32_t>(dataLength) || dataLength < 0) {
969 value = std::u16string();
970 return false;
971 }
972
973 size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
974 if (readCapacity <= GetReadableBytes()) {
975 const uint8_t *str = ReadBuffer(readCapacity);
976 if (str != nullptr) {
977 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
978 SkipBytes(GetPadSize(readCapacity));
979 if (u16Str[dataLength] == 0) {
980 value = std::u16string(u16Str, dataLength);
981 return true;
982 }
983 }
984 }
985
986 readCursor_ = oldCursor;
987 value = std::u16string();
988 return false;
989 }
990
ReadString16WithLength(int32_t & readLength)991 const std::u16string Parcel::ReadString16WithLength(int32_t &readLength)
992 {
993 int32_t dataLength = 0;
994 size_t oldCursor = readCursor_;
995
996 if (!Read<int32_t>(dataLength)) {
997 return std::u16string();
998 }
999
1000 if (dataLength < 0) {
1001 readLength = dataLength;
1002 return std::u16string();
1003 }
1004
1005 size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
1006 if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
1007 const uint8_t *str = ReadBuffer(readCapacity);
1008 if (str != nullptr) {
1009 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
1010 SkipBytes(GetPadSize(readCapacity));
1011 if (u16Str[dataLength] == 0) {
1012 readLength = dataLength;
1013 return std::u16string(u16Str, dataLength);
1014 }
1015 }
1016 }
1017
1018 readCursor_ = oldCursor;
1019 return std::u16string();
1020 }
1021
Alloc(size_t size)1022 void *DefaultAllocator::Alloc(size_t size)
1023 {
1024 return malloc(size);
1025 }
1026
Dealloc(void * data)1027 void DefaultAllocator::Dealloc(void *data)
1028 {
1029 if (data != nullptr) {
1030 free(data);
1031 }
1032 }
1033
Realloc(void * data,size_t newSize)1034 void *DefaultAllocator::Realloc(void *data, size_t newSize)
1035 {
1036 return realloc(data, newSize);
1037 }
1038
1039 template <typename T1, typename T2>
WriteVector(const std::vector<T1> & val,bool (Parcel::* Write)(T2))1040 bool Parcel::WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2))
1041 {
1042 if (val.size() > INT_MAX) {
1043 return false;
1044 }
1045
1046 if (!this->WriteInt32(static_cast<int32_t>(val.size()))) {
1047 return false;
1048 }
1049
1050 for (const auto &v : val) {
1051 if (!(this->*Write)(v)) {
1052 return false;
1053 }
1054 }
1055
1056 size_t padSize = this->GetPadSize(val.size() * sizeof(T1));
1057 this->WritePadBytes(padSize);
1058 return true;
1059 }
1060
WriteBoolVector(const std::vector<bool> & val)1061 bool Parcel::WriteBoolVector(const std::vector<bool> &val)
1062 {
1063 return WriteVector(val, &Parcel::WriteBool);
1064 }
1065
WriteInt8Vector(const std::vector<int8_t> & val)1066 bool Parcel::WriteInt8Vector(const std::vector<int8_t> &val)
1067 {
1068 return WriteVector(val, &Parcel::WriteInt8Unaligned);
1069 }
1070
WriteInt16Vector(const std::vector<int16_t> & val)1071 bool Parcel::WriteInt16Vector(const std::vector<int16_t> &val)
1072 {
1073 return WriteVector(val, &Parcel::WriteInt16);
1074 }
1075
WriteInt32Vector(const std::vector<int32_t> & val)1076 bool Parcel::WriteInt32Vector(const std::vector<int32_t> &val)
1077 {
1078 return WriteVector(val, &Parcel::WriteInt32);
1079 }
1080
WriteInt64Vector(const std::vector<int64_t> & val)1081 bool Parcel::WriteInt64Vector(const std::vector<int64_t> &val)
1082 {
1083 return WriteVector(val, &Parcel::WriteInt64);
1084 }
1085
WriteUInt8Vector(const std::vector<uint8_t> & val)1086 bool Parcel::WriteUInt8Vector(const std::vector<uint8_t> &val)
1087 {
1088 return WriteVector(val, &Parcel::WriteUint8Unaligned);
1089 }
1090
WriteUInt16Vector(const std::vector<uint16_t> & val)1091 bool Parcel::WriteUInt16Vector(const std::vector<uint16_t> &val)
1092 {
1093 return WriteVector(val, &Parcel::WriteUint16Unaligned);
1094 }
1095
WriteUInt32Vector(const std::vector<uint32_t> & val)1096 bool Parcel::WriteUInt32Vector(const std::vector<uint32_t> &val)
1097 {
1098 return WriteVector(val, &Parcel::WriteUint32);
1099 }
1100
WriteUInt64Vector(const std::vector<uint64_t> & val)1101 bool Parcel::WriteUInt64Vector(const std::vector<uint64_t> &val)
1102 {
1103 return WriteVector(val, &Parcel::WriteUint64);
1104 }
1105
WriteFloatVector(const std::vector<float> & val)1106 bool Parcel::WriteFloatVector(const std::vector<float> &val)
1107 {
1108 return WriteVector(val, &Parcel::WriteFloat);
1109 }
1110
WriteDoubleVector(const std::vector<double> & val)1111 bool Parcel::WriteDoubleVector(const std::vector<double> &val)
1112 {
1113 return WriteVector(val, &Parcel::WriteDouble);
1114 }
1115
WriteStringVector(const std::vector<std::string> & val)1116 bool Parcel::WriteStringVector(const std::vector<std::string> &val)
1117 {
1118 return WriteVector(val, &Parcel::WriteString);
1119 }
1120
WriteString16Vector(const std::vector<std::u16string> & val)1121 bool Parcel::WriteString16Vector(const std::vector<std::u16string> &val)
1122 {
1123 return WriteVector(val, &Parcel::WriteString16);
1124 }
1125
1126 template <typename T>
ReadVector(std::vector<T> * val,bool (Parcel::* Read)(T &))1127 bool Parcel::ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &))
1128 {
1129 if (val == nullptr) {
1130 return false;
1131 }
1132
1133 int32_t len = this->ReadInt32();
1134 if (len < 0) {
1135 return false;
1136 }
1137
1138 size_t readAbleSize = this->GetReadableBytes();
1139 size_t size = static_cast<size_t>(len);
1140 if ((size > readAbleSize) || (size > val->max_size())) {
1141 UTILS_LOGE("Failed to read vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1142 return false;
1143 }
1144 val->resize(size);
1145 if (val->size() < size) {
1146 return false;
1147 }
1148
1149 for (auto &v : *val) {
1150 if (!(this->*Read)(v)) {
1151 return false;
1152 }
1153 }
1154
1155 size_t padSize = this->GetPadSize(size * sizeof(T));
1156 this->SkipBytes(padSize);
1157 return true;
1158 }
1159
ReadBoolVector(std::vector<bool> * val)1160 bool Parcel::ReadBoolVector(std::vector<bool> *val)
1161 {
1162 if (val == nullptr) {
1163 return false;
1164 }
1165
1166 int32_t len = this->ReadInt32();
1167 if (len < 0) {
1168 return false;
1169 }
1170
1171 size_t readAbleSize = this->GetReadableBytes();
1172 size_t size = static_cast<size_t>(len);
1173 if ((size > readAbleSize) || (val->max_size() < size)) {
1174 UTILS_LOGE("Failed to read bool vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1175 return false;
1176 }
1177 val->resize(size);
1178 if (val->size() < size) {
1179 return false;
1180 }
1181
1182 for (size_t i = 0; i < size; ++i) {
1183 (*val)[i] = ReadBool();
1184 }
1185
1186 size_t padSize = this->GetPadSize(size * sizeof(bool));
1187 this->SkipBytes(padSize);
1188 return true;
1189 }
1190
ReadInt8Vector(std::vector<int8_t> * val)1191 bool Parcel::ReadInt8Vector(std::vector<int8_t> *val)
1192 {
1193 return ReadVector(val, &Parcel::ReadInt8Unaligned);
1194 }
1195
ReadInt16Vector(std::vector<int16_t> * val)1196 bool Parcel::ReadInt16Vector(std::vector<int16_t> *val)
1197 {
1198 return ReadVector(val, &Parcel::ReadInt16);
1199 }
1200
ReadInt32Vector(std::vector<int32_t> * val)1201 bool Parcel::ReadInt32Vector(std::vector<int32_t> *val)
1202 {
1203 return ReadVector(val, &Parcel::ReadInt32);
1204 }
1205
ReadInt64Vector(std::vector<int64_t> * val)1206 bool Parcel::ReadInt64Vector(std::vector<int64_t> *val)
1207 {
1208 return ReadVector(val, &Parcel::ReadInt64);
1209 }
1210
ReadUInt8Vector(std::vector<uint8_t> * val)1211 bool Parcel::ReadUInt8Vector(std::vector<uint8_t> *val)
1212 {
1213 return ReadVector(val, &Parcel::ReadUint8Unaligned);
1214 }
1215
ReadUInt16Vector(std::vector<uint16_t> * val)1216 bool Parcel::ReadUInt16Vector(std::vector<uint16_t> *val)
1217 {
1218 return ReadVector(val, &Parcel::ReadUint16Unaligned);
1219 }
1220
ReadUInt32Vector(std::vector<uint32_t> * val)1221 bool Parcel::ReadUInt32Vector(std::vector<uint32_t> *val)
1222 {
1223 return ReadVector(val, &Parcel::ReadUint32);
1224 }
1225
ReadUInt64Vector(std::vector<uint64_t> * val)1226 bool Parcel::ReadUInt64Vector(std::vector<uint64_t> *val)
1227 {
1228 return ReadVector(val, &Parcel::ReadUint64);
1229 }
1230
ReadFloatVector(std::vector<float> * val)1231 bool Parcel::ReadFloatVector(std::vector<float> *val)
1232 {
1233 return ReadVector(val, &Parcel::ReadFloat);
1234 }
1235
ReadDoubleVector(std::vector<double> * val)1236 bool Parcel::ReadDoubleVector(std::vector<double> *val)
1237 {
1238 return ReadVector(val, &Parcel::ReadDouble);
1239 }
1240
ReadStringVector(std::vector<std::string> * val)1241 bool Parcel::ReadStringVector(std::vector<std::string> *val)
1242 {
1243 if (val == nullptr) {
1244 return false;
1245 }
1246
1247 int32_t len = this->ReadInt32();
1248 if (len < 0) {
1249 return false;
1250 }
1251
1252 size_t readAbleSize = this->GetReadableBytes();
1253 size_t size = static_cast<size_t>(len);
1254 if ((size > readAbleSize) || (val->max_size() < size)) {
1255 UTILS_LOGE("Failed to read string vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1256 return false;
1257 }
1258 val->resize(size);
1259 if (val->size() < size) {
1260 return false;
1261 }
1262
1263 for (auto &v : *val) {
1264 v = ReadString();
1265 }
1266 return true;
1267 }
1268
ReadString16Vector(std::vector<std::u16string> * val)1269 bool Parcel::ReadString16Vector(std::vector<std::u16string> *val)
1270 {
1271 if (val == nullptr) {
1272 return false;
1273 }
1274
1275 int32_t len = this->ReadInt32();
1276 if (len < 0) {
1277 return false;
1278 }
1279
1280 size_t readAbleSize = this->GetReadableBytes();
1281 size_t size = static_cast<size_t>(len);
1282 if ((size > readAbleSize) || (val->max_size() < size)) {
1283 UTILS_LOGE("Failed to read u16string vector, size = %{public}zu, readAbleSize = %{public}zu",
1284 size, readAbleSize);
1285 return false;
1286 }
1287
1288 val->resize(size);
1289 if (val->size() < size) {
1290 return false;
1291 }
1292
1293 for (auto &v : *val) {
1294 v = ReadString16();
1295 }
1296 return true;
1297 }
1298 } // namespace OHOS
1299