1 /*
2 * Copyright (c) 2021 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 #include "securec.h"
18 #include "utils_log.h"
19
20 namespace OHOS {
21
22 static const size_t DEFAULT_CPACITY = 204800; // 200K
23 static const size_t CAPACITY_THRESHOLD = 4096; // 4k
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 nextObjectIdx_ = 0;
52 objectCursor_ = 0;
53 objectsCapacity_ = 0;
54 }
55
Parcel()56 Parcel::Parcel() : Parcel(new DefaultAllocator())
57 {}
58
~Parcel()59 Parcel::~Parcel()
60 {
61 FlushBuffer();
62 delete allocator_;
63 allocator_ = nullptr;
64 }
65
GetWritableBytes() const66 size_t Parcel::GetWritableBytes() const
67 {
68 if (dataCapacity_ > writeCursor_) {
69 return dataCapacity_ - writeCursor_;
70 }
71
72 return 0;
73 }
74
GetReadableBytes() const75 size_t Parcel::GetReadableBytes() const
76 {
77 if (dataSize_ > readCursor_) {
78 return dataSize_ - readCursor_;
79 }
80
81 return 0;
82 }
83
CalcNewCapacity(size_t minNewCapacity)84 size_t Parcel::CalcNewCapacity(size_t minNewCapacity)
85 {
86 size_t threshold = CAPACITY_THRESHOLD;
87
88 if (minNewCapacity == threshold) {
89 return threshold;
90 }
91
92 // If over threshold, step by threshold.
93 if (minNewCapacity > threshold) {
94 size_t newCapacity = minNewCapacity / threshold * threshold;
95
96 if ((maxDataCapacity_ > 0) && (newCapacity > maxDataCapacity_ - threshold)) {
97 newCapacity = maxDataCapacity_;
98 } else {
99 newCapacity += threshold;
100 }
101
102 return newCapacity;
103 }
104
105 // Not over threshold. Double it.
106 size_t newCapacity = 64;
107
108 while (newCapacity < minNewCapacity) {
109 newCapacity = newCapacity * 2;
110 }
111
112 if ((maxDataCapacity_ > 0) && (newCapacity > maxDataCapacity_)) {
113 newCapacity = maxDataCapacity_;
114 }
115
116 return newCapacity;
117 }
118
EnsureWritableCapacity(size_t desireCapacity)119 bool Parcel::EnsureWritableCapacity(size_t desireCapacity)
120 {
121 if (!writable_) {
122 UTILS_LOGW("this parcel data is alloc by driver, which is can not be writen");
123 return false;
124 }
125 if (desireCapacity <= GetWritableBytes()) {
126 return true;
127 }
128
129 size_t minNewCapacity = desireCapacity + writeCursor_;
130 size_t newCapacity = CalcNewCapacity(minNewCapacity);
131 if ((newCapacity <= dataCapacity_) || (newCapacity < minNewCapacity)) {
132 UTILS_LOGW("Failed to ensure parcel capacity, newCapacity = %{public}zu, dataCapacity_ = %{public}zu, "
133 "minNewCapacity = %{public}zu",
134 newCapacity, dataCapacity_, minNewCapacity);
135 return false;
136 }
137
138 if (allocator_ != nullptr) {
139 void *newData = allocator_->Realloc(data_, newCapacity);
140 if (newData != nullptr) {
141 data_ = reinterpret_cast<uint8_t *>(newData);
142 dataCapacity_ = newCapacity;
143 return true;
144 }
145 UTILS_LOGW("Failed to realloc parcel capacity, newCapacity = %{public}zu, dataCapacity_ = %{public}zu",
146 newCapacity, dataCapacity_);
147 }
148
149 return false;
150 }
151
152 // ValidateReadData only works in basic type read. It doesn't work when read remote object.
153 // And read/write remote object has no effect on "nextObjectIdx_".
ValidateReadData(size_t upperBound)154 bool Parcel::ValidateReadData([[maybe_unused]]size_t upperBound)
155 {
156 #ifdef PARCEL_OBJECT_CHECK
157 if (objectOffsets_ == nullptr || objectCursor_ == 0) {
158 return true;
159 }
160 size_t readPos = readCursor_;
161 size_t objSize = objectCursor_;
162 binder_size_t *objects = objectOffsets_;
163 if (nextObjectIdx_ < objSize && upperBound > objects[nextObjectIdx_]) {
164 size_t nextObj = nextObjectIdx_;
165 do {
166 if (readPos < objects[nextObj] + sizeof(parcel_flat_binder_object)) {
167 UTILS_LOGE("Non-object Read object data, readPos = %{public}zu, upperBound = %{public}zu",
168 readPos, upperBound);
169 return false;
170 }
171 nextObj++;
172 } while (nextObj < objSize && upperBound > objects[nextObj]);
173 nextObjectIdx_ = nextObj;
174 }
175 #endif
176 return true;
177 }
178
GetDataSize() const179 size_t Parcel::GetDataSize() const
180 {
181 return dataSize_;
182 }
183
GetData() const184 uintptr_t Parcel::GetData() const
185 {
186 return reinterpret_cast<uintptr_t>(data_);
187 }
188
GetObjectOffsets() const189 binder_size_t Parcel::GetObjectOffsets() const
190 {
191 return reinterpret_cast<binder_size_t>(objectOffsets_);
192 }
193
GetOffsetsSize() const194 size_t Parcel::GetOffsetsSize() const
195 {
196 return objectCursor_;
197 }
198
GetDataCapacity() const199 size_t Parcel::GetDataCapacity() const
200 {
201 return dataCapacity_;
202 }
203
GetMaxCapacity() const204 size_t Parcel::GetMaxCapacity() const
205 {
206 return maxDataCapacity_;
207 }
208
SetMaxCapacity(size_t maxCapacity)209 bool Parcel::SetMaxCapacity(size_t maxCapacity)
210 {
211 if (maxCapacity > maxDataCapacity_) {
212 maxDataCapacity_ = maxCapacity;
213 return true;
214 }
215
216 return false;
217 }
218
SetAllocator(Allocator * allocator)219 bool Parcel::SetAllocator(Allocator *allocator)
220 {
221 if ((allocator == nullptr) || (allocator_ == allocator)) {
222 return false;
223 }
224
225 if ((data_ != nullptr) && (dataSize_ > 0)) {
226 if (allocator_ == nullptr) {
227 return false;
228 }
229
230 void *newData = allocator->Alloc(dataSize_);
231 if (newData == nullptr) {
232 UTILS_LOGE("Failed to alloc parcel size, dataSize_ = %{public}zu", dataSize_);
233 return false;
234 }
235
236 if (memcpy_s(newData, dataSize_, data_, dataSize_) != EOK) {
237 allocator->Dealloc(newData);
238 return false;
239 }
240 allocator_->Dealloc(data_);
241 data_ = reinterpret_cast<uint8_t *>(newData);
242 dataCapacity_ = dataSize_;
243 }
244
245 delete allocator_;
246 allocator_ = allocator;
247 return true;
248 }
249
CheckOffsets()250 bool Parcel::CheckOffsets()
251 {
252 size_t readPos = readCursor_;
253 if ((readPos + sizeof(parcel_flat_binder_object)) > dataSize_) {
254 UTILS_LOGD("CheckOffsets Invalid obj, obj size overflow. objSize:%{public}zu, dataSize:%{public}zu",
255 readPos + sizeof(parcel_flat_binder_object), dataSize_);
256 return false;
257 }
258
259 size_t objSize = objectCursor_;
260 binder_size_t *objects = objectOffsets_;
261 size_t objCount = 0;
262 while (objCount < objSize) {
263 if (objects[objCount] == readPos) {
264 return true;
265 }
266 objCount++;
267 }
268 UTILS_LOGW("CheckOffsets Invalid obj: obj not found.");
269 return false;
270 }
271
InjectOffsets(binder_size_t offsets,size_t offsetSize)272 void Parcel::InjectOffsets(binder_size_t offsets, size_t offsetSize)
273 {
274 if (offsetSize <= 0) {
275 return;
276 }
277
278 auto *newObjectOffsets = reinterpret_cast<binder_size_t *>(offsets);
279 for (size_t index = 0; index < offsetSize; index++) {
280 if (EnsureObjectsCapacity()) {
281 WriteObjectOffset(newObjectOffsets[index]);
282 }
283 }
284 }
285
ClearObjects()286 void Parcel::ClearObjects()
287 {
288 objectHolder_.clear();
289 free(objectOffsets_);
290 nextObjectIdx_ = 0;
291 objectCursor_ = 0;
292 objectOffsets_ = nullptr;
293 objectsCapacity_ = 0;
294 }
295
FlushBuffer()296 void Parcel::FlushBuffer()
297 {
298 if (allocator_ == nullptr) {
299 return;
300 }
301
302 if (data_ != nullptr) {
303 allocator_->Dealloc(data_);
304 dataSize_ = 0;
305 writeCursor_ = 0;
306 readCursor_ = 0;
307 dataCapacity_ = 0;
308 data_ = nullptr;
309 }
310
311 if (objectOffsets_) {
312 ClearObjects();
313 }
314 }
315
SetDataCapacity(size_t newCapacity)316 bool Parcel::SetDataCapacity(size_t newCapacity)
317 {
318 if (allocator_ == nullptr || dataSize_ >= newCapacity) {
319 return false;
320 }
321
322 void *newData = allocator_->Realloc(data_, newCapacity);
323 if (newData != nullptr) {
324 data_ = reinterpret_cast<uint8_t *>(newData);
325 dataCapacity_ = newCapacity;
326 return true;
327 }
328 return false;
329 }
330
SetDataSize(size_t dataSize)331 bool Parcel::SetDataSize(size_t dataSize)
332 {
333 if (dataSize > dataCapacity_) {
334 return false;
335 }
336
337 dataSize_ = dataSize;
338 return true;
339 }
340
WriteDataBytes(const void * data,size_t size)341 bool Parcel::WriteDataBytes(const void *data, size_t size)
342 {
343 void *dest = data_ + writeCursor_;
344 size_t writableBytes = GetWritableBytes();
345 if (memcpy_s(dest, writableBytes, data, size) != EOK) {
346 return false;
347 }
348 writeCursor_ += size;
349 dataSize_ += size;
350 return true;
351 }
352
WritePadBytes(size_t padSize)353 void Parcel::WritePadBytes(size_t padSize)
354 {
355 uint8_t *dest = data_ + writeCursor_;
356 static const int MAX_MASK_NUM = 4;
357 #if __BYTE_ORDER == __LITTLE_ENDIAN
358 static const size_t mask[MAX_MASK_NUM] = { 0xFFFFFFFF, 0x00ffffff, 0x0000ffff, 0x000000ff };
359 #else
360 static const size_t mask[MAX_MASK_NUM] = { 0xFFFFFFFF, 0xffffff00, 0xffff0000, 0xff000000 };
361 #endif
362 *reinterpret_cast<uint32_t *>(dest + padSize - MAX_MASK_NUM) &= mask[padSize];
363 writeCursor_ += padSize;
364 dataSize_ += padSize;
365 }
366
WriteBuffer(const void * data,size_t size)367 bool Parcel::WriteBuffer(const void *data, size_t size)
368 {
369 if (data == nullptr || size == 0) {
370 return false;
371 }
372
373 size_t padSize = GetPadSize(size);
374 size_t desireCapacity = size + padSize;
375
376 // in case of desireCapacity overflow
377 if (desireCapacity < size || desireCapacity < padSize) {
378 return false;
379 }
380
381 if (EnsureWritableCapacity(desireCapacity)) {
382 if (!WriteDataBytes(data, size)) {
383 return false;
384 }
385 WritePadBytes(padSize);
386 return true;
387 }
388
389 return false;
390 }
391
WriteBufferAddTerminator(const void * data,size_t size,size_t typeSize)392 bool Parcel::WriteBufferAddTerminator(const void *data, size_t size, size_t typeSize)
393 {
394 if (data == nullptr || size < typeSize) {
395 return false;
396 }
397
398 size_t padSize = GetPadSize(size);
399 size_t desireCapacity = size + padSize;
400
401 // in case of desireCapacity overflow
402 if (desireCapacity < size || desireCapacity < padSize) {
403 return false;
404 }
405
406 if (EnsureWritableCapacity(desireCapacity)) {
407 if (!WriteDataBytes(data, size - typeSize)) {
408 return false;
409 }
410
411 // Reserved for 32 bits
412 const char terminator[] = {0, 0, 0, 0};
413 if (!WriteDataBytes(terminator, typeSize)) {
414 return false;
415 }
416 WritePadBytes(padSize);
417 return true;
418 }
419
420 return false;
421 }
422
WriteUnpadBuffer(const void * data,size_t size)423 bool Parcel::WriteUnpadBuffer(const void *data, size_t size)
424 {
425 return WriteBuffer(data, size);
426 }
427
428 template <typename T>
Write(T value)429 bool Parcel::Write(T value)
430 {
431 size_t desireCapacity = sizeof(T);
432
433 if (EnsureWritableCapacity(desireCapacity)) {
434 *reinterpret_cast<T *>(data_ + writeCursor_) = value;
435 writeCursor_ += desireCapacity;
436 dataSize_ += desireCapacity;
437 return true;
438 }
439
440 return false;
441 }
442
WriteBool(bool value)443 bool Parcel::WriteBool(bool value)
444 {
445 return Write<int32_t>(static_cast<int32_t>(value));
446 }
447
WriteBoolUnaligned(bool value)448 bool Parcel::WriteBoolUnaligned(bool value)
449 {
450 return Write<bool>(value);
451 }
452
WriteInt8(int8_t value)453 bool Parcel::WriteInt8(int8_t value)
454 {
455 return Write<int32_t>(static_cast<int32_t>(value));
456 }
457
WriteInt8Unaligned(int8_t value)458 bool Parcel::WriteInt8Unaligned(int8_t value)
459 {
460 return Write<int8_t>(value);
461 }
462
WriteInt16(int16_t value)463 bool Parcel::WriteInt16(int16_t value)
464 {
465 return Write<int32_t>(static_cast<int32_t>(value));
466 }
467
WriteInt16Unaligned(int16_t value)468 bool Parcel::WriteInt16Unaligned(int16_t value)
469 {
470 return Write<int16_t>(value);
471 }
472
WriteInt32(int32_t value)473 bool Parcel::WriteInt32(int32_t value)
474 {
475 return Write<int32_t>(value);
476 }
477
WriteInt64(int64_t value)478 bool Parcel::WriteInt64(int64_t value)
479 {
480 return Write<int64_t>(value);
481 }
482
WriteUint8(uint8_t value)483 bool Parcel::WriteUint8(uint8_t value)
484 {
485 return Write<uint32_t>(static_cast<uint32_t>(value));
486 }
487
WriteUint8Unaligned(uint8_t value)488 bool Parcel::WriteUint8Unaligned(uint8_t value)
489 {
490 return Write<uint8_t>(value);
491 }
492
WriteUint16(uint16_t value)493 bool Parcel::WriteUint16(uint16_t value)
494 {
495 return Write<uint32_t>(static_cast<uint32_t>(value));
496 }
497
WriteUint16Unaligned(uint16_t value)498 bool Parcel::WriteUint16Unaligned(uint16_t value)
499 {
500 return Write<uint16_t>(value);
501 }
502
WriteUint32(uint32_t value)503 bool Parcel::WriteUint32(uint32_t value)
504 {
505 return Write<uint32_t>(value);
506 }
507
WriteUint64(uint64_t value)508 bool Parcel::WriteUint64(uint64_t value)
509 {
510 return Write<uint64_t>(value);
511 }
512
WriteFloat(float value)513 bool Parcel::WriteFloat(float value)
514 {
515 return Write<float>(value);
516 }
517
WriteDouble(double value)518 bool Parcel::WriteDouble(double value)
519 {
520 return Write<double>(value);
521 }
522
WritePointer(uintptr_t value)523 bool Parcel::WritePointer(uintptr_t value)
524 {
525 return Write<binder_uintptr_t>(value);
526 }
527
WriteCString(const char * value)528 bool Parcel::WriteCString(const char *value)
529 {
530 if (value == nullptr) {
531 return false;
532 }
533 int32_t dataLength = strlen(value);
534 int32_t desireCapacity = (dataLength + 1) * sizeof(char);
535 return WriteBuffer(value, desireCapacity);
536 }
537
WriteString(const std::string & value)538 bool Parcel::WriteString(const std::string &value)
539 {
540 if (value.data() == nullptr) {
541 return WriteInt32(-1);
542 }
543
544 int32_t dataLength = value.length();
545 int32_t typeSize = sizeof(char);
546 int32_t desireCapacity = dataLength + typeSize;
547
548 if (!Write<int32_t>(dataLength)) {
549 return false;
550 }
551
552 return WriteBufferAddTerminator(value.data(), desireCapacity, typeSize);
553 }
554
WriteString16(const std::u16string & value)555 bool Parcel::WriteString16(const std::u16string &value)
556 {
557 if (value.data() == nullptr) {
558 return WriteInt32(-1);
559 }
560
561 int32_t dataLength = value.length();
562 int32_t typeSize = sizeof(char16_t);
563 int32_t desireCapacity = (dataLength + 1) * typeSize;
564
565 if (!Write<int32_t>(dataLength)) {
566 return false;
567 }
568
569 return WriteBufferAddTerminator(value.data(), desireCapacity, typeSize);
570 }
571
WriteString16WithLength(const char16_t * value,size_t len)572 bool Parcel::WriteString16WithLength(const char16_t *value, size_t len)
573 {
574 if (!value) {
575 return WriteInt32(-1);
576 }
577
578 int32_t dataLength = len;
579 int32_t typeSize = sizeof(char16_t);
580 int32_t desireCapacity = (dataLength + 1) * typeSize;
581 std::u16string u16str(reinterpret_cast<const char16_t *>(value), len);
582
583 if (!Write<int32_t>(dataLength)) {
584 return false;
585 }
586
587 return WriteBufferAddTerminator(u16str.data(), desireCapacity, typeSize);
588 }
589
WriteString8WithLength(const char * value,size_t len)590 bool Parcel::WriteString8WithLength(const char *value, size_t len)
591 {
592 if (!value) {
593 return WriteInt32(-1);
594 }
595
596 int32_t dataLength = len;
597 int32_t typeSize = sizeof(char);
598 int32_t desireCapacity = (dataLength + 1) * typeSize;
599
600 if (!Write<int32_t>(dataLength)) {
601 return false;
602 }
603
604 return WriteBufferAddTerminator(value, desireCapacity, typeSize);
605 }
606
EnsureObjectsCapacity()607 bool Parcel::EnsureObjectsCapacity()
608 {
609 if ((objectsCapacity_ - objectCursor_) >= 1) {
610 return true;
611 }
612
613 if (allocator_ == nullptr) {
614 return false;
615 }
616
617 const int NEW_CAPACITY_ADD = 2;
618 const int NEW_CAPACITY_MULTI = 3;
619 const int NEW_CAPACITY_DIV = 2;
620 size_t newCapacity = ((objectsCapacity_ + NEW_CAPACITY_ADD) * NEW_CAPACITY_MULTI) / NEW_CAPACITY_DIV;
621 size_t newBytes = newCapacity * sizeof(binder_size_t);
622
623 void *newOffsets = realloc(objectOffsets_, newBytes);
624 if (newOffsets == nullptr) {
625 return false;
626 }
627
628 objectOffsets_ = reinterpret_cast<binder_size_t *>(newOffsets);
629 objectsCapacity_ = newCapacity;
630 return true;
631 }
632
WriteObjectOffset(binder_size_t offset)633 bool Parcel::WriteObjectOffset(binder_size_t offset)
634 {
635 if (offset > dataSize_) {
636 return false;
637 }
638
639 for (size_t index = 0; index < objectCursor_; index++) {
640 if (objectOffsets_[index] == offset) {
641 return false;
642 }
643 }
644
645 objectOffsets_[objectCursor_] = offset;
646 objectCursor_++;
647 return true;
648 }
649
WriteRemoteObject(const Parcelable * object)650 bool Parcel::WriteRemoteObject(const Parcelable *object)
651 {
652 size_t placeholder = writeCursor_;
653 // Parcelable is nullptr
654 if ((object == nullptr) || (!object->asRemote_)) {
655 return false;
656 }
657
658 if (!EnsureObjectsCapacity()) {
659 return false;
660 }
661
662 if (!object->Marshalling(*this)) {
663 return false;
664 }
665
666 WriteObjectOffset(placeholder);
667
668 if (object->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT)) {
669 sptr<Parcelable> tmp(const_cast<Parcelable *>(object));
670 objectHolder_.push_back(tmp);
671 }
672
673 return true;
674 }
675
WriteParcelable(const Parcelable * object)676 bool Parcel::WriteParcelable(const Parcelable *object)
677 {
678 size_t placeholder = writeCursor_;
679 size_t restorSize = dataSize_;
680
681 // Parcelable is nullptr
682 if (object == nullptr) {
683 // write the meta data to indicate pass an null object.
684 return WriteInt32(0);
685 }
686
687 if (!object->asRemote_) {
688 // meta data indicate we have an parcelable object.
689 if (!WriteInt32(1)) {
690 return false;
691 }
692
693 return object->Marshalling(*this);
694 }
695
696 // Write the remote object flag
697 if (!WriteInt32(1)) {
698 return false;
699 }
700
701 if (WriteRemoteObject(const_cast<Parcelable*>(object))) {
702 return true;
703 }
704
705 // rollback the write position.
706 writeCursor_ = placeholder;
707 dataSize_ = restorSize;
708 return false;
709 }
710
WriteStrongParcelable(const sptr<Parcelable> & object)711 bool Parcel::WriteStrongParcelable(const sptr<Parcelable> &object)
712 {
713 if (object == nullptr) {
714 WriteInt32(0);
715 return true;
716 }
717
718 object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
719 return WriteParcelable(object.GetRefPtr());
720 }
721
722 template <typename T>
Read(T & value)723 bool Parcel::Read(T &value)
724 {
725 size_t desireCapacity = sizeof(T);
726
727 if (desireCapacity <= GetReadableBytes()) {
728 const void *data = data_ + readCursor_;
729 #ifdef PARCEL_OBJECT_CHECK
730 size_t upperBound = readCursor_ + desireCapacity;
731 if (!ValidateReadData(upperBound)) {
732 readCursor_ += desireCapacity;
733 return false;
734 }
735 #endif
736 readCursor_ += desireCapacity;
737 value = *reinterpret_cast<const T *>(data);
738 return true;
739 }
740
741 return false;
742 }
743
744 template <typename T>
Read()745 T Parcel::Read()
746 {
747 T lvalue {};
748 return Read<T>(lvalue) ? lvalue : 0;
749 }
750
ParseFrom(uintptr_t data,size_t size)751 bool Parcel::ParseFrom(uintptr_t data, size_t size)
752 {
753 if (data_ != nullptr) {
754 return false;
755 }
756
757 data_ = reinterpret_cast<uint8_t *>(data);
758 dataCapacity_ = size;
759 dataSize_ = size;
760 /* data is alloc by driver, can not write again */
761 writable_ = false;
762 #ifdef PARCEL_OBJECT_CHECK
763 if (objectOffsets_) {
764 ClearObjects();
765 }
766 #endif
767 return true;
768 }
769
ReadBuffer(size_t length)770 const uint8_t *Parcel::ReadBuffer(size_t length)
771 {
772 if (GetReadableBytes() >= length) {
773 uint8_t *buffer = data_ + readCursor_;
774 readCursor_ += length;
775 return buffer;
776 }
777
778 return nullptr;
779 }
780
BasicReadBuffer(size_t length)781 const uint8_t *Parcel::BasicReadBuffer([[maybe_unused]]size_t length)
782 {
783 #ifdef PARCEL_OBJECT_CHECK
784 if (GetReadableBytes() >= length) {
785 uint8_t *buffer = data_ + readCursor_;
786 size_t upperBound = readCursor_ + length;
787 if (!ValidateReadData(upperBound)) {
788 readCursor_ += length;
789 return nullptr;
790 }
791 readCursor_ += length;
792 return buffer;
793 }
794 #endif
795 return nullptr;
796 }
797
ReadUnpadBuffer(size_t length)798 const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
799 {
800 if (GetReadableBytes() >= length) {
801 uint8_t *buffer = data_ + readCursor_;
802 readCursor_ += length;
803 SkipBytes(GetPadSize(length));
804 return buffer;
805 }
806
807 return nullptr;
808 }
809
SkipBytes(size_t bytes)810 void Parcel::SkipBytes(size_t bytes)
811 {
812 if (GetReadableBytes() >= bytes) {
813 readCursor_ += bytes;
814 } else if (readCursor_ < dataCapacity_) {
815 readCursor_ = dataCapacity_;
816 }
817 }
818
GetReadPosition()819 size_t Parcel::GetReadPosition()
820 {
821 return readCursor_;
822 }
823
RewindRead(size_t newPosition)824 bool Parcel::RewindRead(size_t newPosition)
825 {
826 if (newPosition > dataSize_) {
827 return false;
828 }
829 readCursor_ = newPosition;
830 nextObjectIdx_ = 0;
831 return true;
832 }
833
GetWritePosition()834 size_t Parcel::GetWritePosition()
835 {
836 return writeCursor_;
837 }
838
RewindWrite(size_t newPosition)839 bool Parcel::RewindWrite(size_t newPosition)
840 {
841 if (newPosition > dataSize_) {
842 return false;
843 }
844 writeCursor_ = newPosition;
845 dataSize_ = newPosition;
846 #ifdef PARCEL_OBJECT_CHECK
847 if (objectOffsets_ == nullptr || objectCursor_ == 0) {
848 return true;
849 }
850 size_t objectSize = objectCursor_;
851 if (objectOffsets_[objectSize - 1] + sizeof(parcel_flat_binder_object) > newPosition) {
852 while (objectSize > 0) {
853 if (objectOffsets_[objectSize - 1] + sizeof(parcel_flat_binder_object) <= newPosition) {
854 break;
855 }
856 objectSize--;
857 }
858 if (objectSize == 0) {
859 ClearObjects();
860 return true;
861 }
862 size_t newBytes = objectSize * sizeof(binder_size_t);
863 void *newOffsets = realloc(objectOffsets_, newBytes);
864 if (newOffsets == nullptr) {
865 return false;
866 }
867 objectOffsets_ = reinterpret_cast<binder_size_t *>(newOffsets);
868 objectCursor_ = objectSize;
869 objectsCapacity_ = objectCursor_;
870 objectHolder_.resize(objectSize);
871 nextObjectIdx_ = 0;
872 return true;
873 }
874 #endif
875 return true;
876 }
877
ReadBool()878 bool Parcel::ReadBool()
879 {
880 int32_t temp = Read<int32_t>();
881 return (temp != 0);
882 }
883
ReadBoolUnaligned()884 bool Parcel::ReadBoolUnaligned()
885 {
886 return Read<bool>();
887 }
888
ReadInt8()889 int8_t Parcel::ReadInt8()
890 {
891 int32_t temp = Read<int32_t>();
892 return static_cast<int8_t>(temp);
893 }
894
ReadInt16()895 int16_t Parcel::ReadInt16()
896 {
897 int32_t temp = Read<int32_t>();
898 return static_cast<int16_t>(temp);
899 }
900
ReadInt32()901 int32_t Parcel::ReadInt32()
902 {
903 return Read<int32_t>();
904 }
905
ReadInt64()906 int64_t Parcel::ReadInt64()
907 {
908 return Read<int64_t>();
909 }
910
ReadUint8()911 uint8_t Parcel::ReadUint8()
912 {
913 uint32_t temp = Read<uint32_t>();
914 return static_cast<uint8_t>(temp);
915 }
916
ReadUint16()917 uint16_t Parcel::ReadUint16()
918 {
919 uint32_t temp = Read<uint32_t>();
920 return static_cast<uint16_t>(temp);
921 }
922
ReadUint32()923 uint32_t Parcel::ReadUint32()
924 {
925 return Read<uint32_t>();
926 }
927
ReadUint64()928 uint64_t Parcel::ReadUint64()
929 {
930 return Read<uint64_t>();
931 }
932
ReadFloat()933 float Parcel::ReadFloat()
934 {
935 return Read<float>();
936 }
937
ReadDouble()938 double Parcel::ReadDouble()
939 {
940 return Read<double>();
941 }
942
943 template <typename T>
ReadPadded(T & value)944 bool Parcel::ReadPadded(T &value)
945 {
946 int32_t temp;
947 bool result = Read<int32_t>(temp);
948 if (result) {
949 value = static_cast<T>(temp);
950 }
951
952 return result;
953 }
954
ReadBool(bool & value)955 bool Parcel::ReadBool(bool &value)
956 {
957 return ReadPadded<bool>(value);
958 }
959
ReadInt8(int8_t & value)960 bool Parcel::ReadInt8(int8_t &value)
961 {
962 return ReadPadded<int8_t>(value);
963 }
964
ReadInt8Unaligned(int8_t & value)965 bool Parcel::ReadInt8Unaligned(int8_t &value)
966 {
967 return Read<int8_t>(value);
968 }
969
ReadInt16(int16_t & value)970 bool Parcel::ReadInt16(int16_t &value)
971 {
972 return ReadPadded<int16_t>(value);
973 }
974
ReadInt16Unaligned(int16_t & value)975 bool Parcel::ReadInt16Unaligned(int16_t &value)
976 {
977 return Read<int16_t>(value);
978 }
979
ReadInt32(int32_t & value)980 bool Parcel::ReadInt32(int32_t &value)
981 {
982 return Read<int32_t>(value);
983 }
984
ReadInt64(int64_t & value)985 bool Parcel::ReadInt64(int64_t &value)
986 {
987 return Read<int64_t>(value);
988 }
989
ReadUint8(uint8_t & value)990 bool Parcel::ReadUint8(uint8_t &value)
991 {
992 return ReadPadded<uint8_t>(value);
993 }
994
ReadUint8Unaligned(uint8_t & value)995 bool Parcel::ReadUint8Unaligned(uint8_t &value)
996 {
997 return Read<uint8_t>(value);
998 }
999
ReadUint16(uint16_t & value)1000 bool Parcel::ReadUint16(uint16_t &value)
1001 {
1002 return ReadPadded<uint16_t>(value);
1003 }
1004
ReadUint16Unaligned(uint16_t & value)1005 bool Parcel::ReadUint16Unaligned(uint16_t &value)
1006 {
1007 return Read<uint16_t>(value);
1008 }
1009
ReadUint32(uint32_t & value)1010 bool Parcel::ReadUint32(uint32_t &value)
1011 {
1012 return Read<uint32_t>(value);
1013 }
1014
ReadUint64(uint64_t & value)1015 bool Parcel::ReadUint64(uint64_t &value)
1016 {
1017 return Read<uint64_t>(value);
1018 }
1019
ReadFloat(float & value)1020 bool Parcel::ReadFloat(float &value)
1021 {
1022 return Read<float>(value);
1023 }
1024
ReadDouble(double & value)1025 bool Parcel::ReadDouble(double &value)
1026 {
1027 return Read<double>(value);
1028 }
1029
ReadPointer()1030 uintptr_t Parcel::ReadPointer()
1031 {
1032 return Read<binder_uintptr_t>();
1033 }
1034
ReadCString()1035 const char *Parcel::ReadCString()
1036 {
1037 size_t oldCursor = readCursor_;
1038 const size_t avail = GetReadableBytes();
1039 const char* cstr = reinterpret_cast<const char*>(data_ + readCursor_);
1040 // is the string's trailing NUL within the parcel's valid bounds?
1041 const char* eos = reinterpret_cast<const char*>(memchr(cstr, 0, avail));
1042 if (eos != nullptr) {
1043 const size_t dataLength = eos - cstr;
1044 readCursor_ += (dataLength + 1);
1045 SkipBytes(GetPadSize(dataLength + 1));
1046 return cstr;
1047 }
1048 readCursor_ = oldCursor;
1049 return nullptr;
1050 }
1051
ReadString()1052 const std::string Parcel::ReadString()
1053 {
1054 int32_t dataLength = 0;
1055 size_t oldCursor = readCursor_;
1056
1057 if (!Read<int32_t>(dataLength) || dataLength < 0 || dataLength >= INT32_MAX) {
1058 return std::string();
1059 }
1060
1061 size_t readCapacity = static_cast<size_t>(dataLength) + 1;
1062 if (readCapacity <= GetReadableBytes()) {
1063 #ifdef PARCEL_OBJECT_CHECK
1064 const uint8_t *dest = BasicReadBuffer(readCapacity);
1065 #else
1066 const uint8_t *dest = ReadBuffer(readCapacity);
1067 #endif
1068 if (dest != nullptr) {
1069 const auto *str = reinterpret_cast<const char *>(dest);
1070 SkipBytes(GetPadSize(readCapacity));
1071 if (str[dataLength] == 0) {
1072 return std::string(str, dataLength);
1073 }
1074 }
1075 }
1076
1077 readCursor_ = oldCursor;
1078 return std::string();
1079 }
1080
ReadString(std::string & value)1081 bool Parcel::ReadString(std::string &value)
1082 {
1083 int32_t dataLength = 0;
1084 size_t oldCursor = readCursor_;
1085
1086 if (!Read<int32_t>(dataLength) || dataLength < 0 || dataLength >= INT32_MAX) {
1087 value = std::string();
1088 return false;
1089 }
1090
1091 size_t readCapacity = static_cast<size_t>(dataLength) + 1;
1092 if (readCapacity <= GetReadableBytes()) {
1093 #ifdef PARCEL_OBJECT_CHECK
1094 const uint8_t *dest = BasicReadBuffer(readCapacity);
1095 #else
1096 const uint8_t *dest = ReadBuffer(readCapacity);
1097 #endif
1098 if (dest != nullptr) {
1099 const auto *str = reinterpret_cast<const char *>(dest);
1100 SkipBytes(GetPadSize(readCapacity));
1101 if (str[dataLength] == 0) {
1102 value = std::string(str, dataLength);
1103 return true;
1104 }
1105 }
1106 }
1107
1108 readCursor_ = oldCursor;
1109 value = std::string();
1110 return false;
1111 }
1112
ReadString16()1113 const std::u16string Parcel::ReadString16()
1114 {
1115 int32_t dataLength = 0;
1116 size_t oldCursor = readCursor_;
1117
1118 if (!Read<int32_t>(dataLength) || dataLength < 0 || dataLength >= INT32_MAX) {
1119 return std::u16string();
1120 }
1121
1122 size_t readCapacity = (static_cast<size_t>(dataLength) + 1) * sizeof(char16_t);
1123 if ((readCapacity > (static_cast<size_t>(dataLength))) && (readCapacity <= GetReadableBytes())) {
1124 #ifdef PARCEL_OBJECT_CHECK
1125 const uint8_t *str = BasicReadBuffer(readCapacity);
1126 #else
1127 const uint8_t *str = ReadBuffer(readCapacity);
1128 #endif
1129 if (str != nullptr) {
1130 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
1131 SkipBytes(GetPadSize(readCapacity));
1132 if (u16Str[dataLength] == 0) {
1133 return std::u16string(u16Str, dataLength);
1134 }
1135 }
1136 }
1137
1138 readCursor_ = oldCursor;
1139 return std::u16string();
1140 }
1141
ReadString16(std::u16string & value)1142 bool Parcel::ReadString16(std::u16string &value)
1143 {
1144 int32_t dataLength = 0;
1145 size_t oldCursor = readCursor_;
1146
1147 if (!Read<int32_t>(dataLength) || dataLength < 0 || dataLength >= INT32_MAX) {
1148 value = std::u16string();
1149 return false;
1150 }
1151
1152 size_t readCapacity = (static_cast<size_t>(dataLength) + 1) * sizeof(char16_t);
1153 if ((readCapacity > (static_cast<size_t>(dataLength))) && (readCapacity <= GetReadableBytes())) {
1154 #ifdef PARCEL_OBJECT_CHECK
1155 const uint8_t *str = BasicReadBuffer(readCapacity);
1156 #else
1157 const uint8_t *str = ReadBuffer(readCapacity);
1158 #endif
1159 if (str != nullptr) {
1160 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
1161 SkipBytes(GetPadSize(readCapacity));
1162 if (u16Str[dataLength] == 0) {
1163 value = std::u16string(u16Str, dataLength);
1164 return true;
1165 }
1166 }
1167 }
1168
1169 readCursor_ = oldCursor;
1170 value = std::u16string();
1171 return false;
1172 }
1173
ReadString16WithLength(int32_t & readLength)1174 const std::u16string Parcel::ReadString16WithLength(int32_t &readLength)
1175 {
1176 int32_t dataLength = 0;
1177 size_t oldCursor = readCursor_;
1178
1179 if (!Read<int32_t>(dataLength)) {
1180 return std::u16string();
1181 }
1182
1183 if (dataLength < 0 || dataLength >= INT32_MAX) {
1184 readLength = dataLength;
1185 return std::u16string();
1186 }
1187
1188 size_t readCapacity = (static_cast<size_t>(dataLength) + 1) * sizeof(char16_t);
1189 if ((readCapacity > (static_cast<size_t>(dataLength))) && (readCapacity <= GetReadableBytes())) {
1190 #ifdef PARCEL_OBJECT_CHECK
1191 const uint8_t *str = BasicReadBuffer(readCapacity);
1192 #else
1193 const uint8_t *str = ReadBuffer(readCapacity);
1194 #endif
1195 if (str != nullptr) {
1196 const auto *u16Str = reinterpret_cast<const char16_t *>(str);
1197 SkipBytes(GetPadSize(readCapacity));
1198 if (u16Str[dataLength] == 0) {
1199 readLength = dataLength;
1200 return std::u16string(u16Str, dataLength);
1201 }
1202 }
1203 }
1204
1205 readCursor_ = oldCursor;
1206 return std::u16string();
1207 }
1208
ReadString8WithLength(int32_t & readLength)1209 const std::string Parcel::ReadString8WithLength(int32_t &readLength)
1210 {
1211 int32_t dataLength = 0;
1212 size_t oldCursor = readCursor_;
1213
1214 if (!Read<int32_t>(dataLength)) {
1215 return std::string();
1216 }
1217
1218 if (dataLength < 0 || dataLength >= INT32_MAX) {
1219 readLength = dataLength;
1220 return std::string();
1221 }
1222
1223 size_t readCapacity = (static_cast<size_t>(dataLength) + 1) * sizeof(char);
1224 if (readCapacity <= GetReadableBytes()) {
1225 #ifdef PARCEL_OBJECT_CHECK
1226 const uint8_t *str = BasicReadBuffer(readCapacity);
1227 #else
1228 const uint8_t *str = ReadBuffer(readCapacity);
1229 #endif
1230 if (str != nullptr) {
1231 const auto *u8Str = reinterpret_cast<const char *>(str);
1232 SkipBytes(GetPadSize(readCapacity));
1233 if (u8Str[dataLength] == 0) {
1234 readLength = dataLength;
1235 return std::string(u8Str, dataLength);
1236 }
1237 }
1238 }
1239
1240 readCursor_ = oldCursor;
1241 return std::string();
1242 }
1243
Alloc(size_t size)1244 void *DefaultAllocator::Alloc(size_t size)
1245 {
1246 return malloc(size);
1247 }
1248
Dealloc(void * data)1249 void DefaultAllocator::Dealloc(void *data)
1250 {
1251 if (data != nullptr) {
1252 free(data);
1253 }
1254 }
1255
Realloc(void * data,size_t newSize)1256 void *DefaultAllocator::Realloc(void *data, size_t newSize)
1257 {
1258 return realloc(data, newSize);
1259 }
1260
1261 template <typename T1, typename T2>
WriteVector(const std::vector<T1> & val,bool (Parcel::* Write)(T2))1262 bool Parcel::WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2))
1263 {
1264 if (val.size() > INT_MAX) {
1265 return false;
1266 }
1267
1268 if (!this->WriteInt32(static_cast<int32_t>(val.size()))) {
1269 return false;
1270 }
1271
1272 for (const auto &v : val) {
1273 if (!(this->*Write)(v)) {
1274 return false;
1275 }
1276 }
1277
1278 size_t padSize = this->GetPadSize(val.size() * sizeof(T1));
1279 this->WritePadBytes(padSize);
1280 return true;
1281 }
1282
1283 template <typename Type, typename T1, typename T2>
WriteFixedAlignVector(const std::vector<T1> & originVal,bool (Parcel::* SpecialWrite)(T2))1284 bool Parcel::WriteFixedAlignVector(const std::vector<T1> &originVal, bool (Parcel::*SpecialWrite)(T2))
1285 {
1286 if (originVal.size() > INT_MAX) {
1287 return false;
1288 }
1289
1290 if (!this->WriteInt32(static_cast<int32_t>(originVal.size()))) {
1291 return false;
1292 }
1293 // Use the specified interface to write a single element.
1294 for (const auto &v : originVal) {
1295 if (!(this->*SpecialWrite)(v)) {
1296 return false;
1297 }
1298 }
1299 // The write length of these interfaces is different from the original type.
1300 // They need to use the specified write length and calculate the padSize based on this.
1301 size_t padSize = this->GetPadSize(originVal.size() * sizeof(Type));
1302 this->WritePadBytes(padSize);
1303 return true;
1304 }
1305
WriteBoolVector(const std::vector<bool> & val)1306 bool Parcel::WriteBoolVector(const std::vector<bool> &val)
1307 {
1308 return WriteVector(val, &Parcel::WriteBool);
1309 }
1310
WriteInt8Vector(const std::vector<int8_t> & val)1311 bool Parcel::WriteInt8Vector(const std::vector<int8_t> &val)
1312 {
1313 return WriteVector(val, &Parcel::WriteInt8Unaligned);
1314 }
1315
WriteInt16Vector(const std::vector<int16_t> & val)1316 bool Parcel::WriteInt16Vector(const std::vector<int16_t> &val)
1317 {
1318 return WriteVector(val, &Parcel::WriteInt16);
1319 }
1320
WriteInt32Vector(const std::vector<int32_t> & val)1321 bool Parcel::WriteInt32Vector(const std::vector<int32_t> &val)
1322 {
1323 return WriteVector(val, &Parcel::WriteInt32);
1324 }
1325
WriteInt64Vector(const std::vector<int64_t> & val)1326 bool Parcel::WriteInt64Vector(const std::vector<int64_t> &val)
1327 {
1328 return WriteVector(val, &Parcel::WriteInt64);
1329 }
1330
WriteUInt8Vector(const std::vector<uint8_t> & val)1331 bool Parcel::WriteUInt8Vector(const std::vector<uint8_t> &val)
1332 {
1333 return WriteVector(val, &Parcel::WriteUint8Unaligned);
1334 }
1335
WriteUInt16Vector(const std::vector<uint16_t> & val)1336 bool Parcel::WriteUInt16Vector(const std::vector<uint16_t> &val)
1337 {
1338 return WriteVector(val, &Parcel::WriteUint16Unaligned);
1339 }
1340
WriteUInt32Vector(const std::vector<uint32_t> & val)1341 bool Parcel::WriteUInt32Vector(const std::vector<uint32_t> &val)
1342 {
1343 return WriteVector(val, &Parcel::WriteUint32);
1344 }
1345
WriteUInt64Vector(const std::vector<uint64_t> & val)1346 bool Parcel::WriteUInt64Vector(const std::vector<uint64_t> &val)
1347 {
1348 return WriteVector(val, &Parcel::WriteUint64);
1349 }
1350
WriteFloatVector(const std::vector<float> & val)1351 bool Parcel::WriteFloatVector(const std::vector<float> &val)
1352 {
1353 return WriteVector(val, &Parcel::WriteFloat);
1354 }
1355
WriteDoubleVector(const std::vector<double> & val)1356 bool Parcel::WriteDoubleVector(const std::vector<double> &val)
1357 {
1358 return WriteVector(val, &Parcel::WriteDouble);
1359 }
1360
WriteStringVector(const std::vector<std::string> & val)1361 bool Parcel::WriteStringVector(const std::vector<std::string> &val)
1362 {
1363 return WriteVector(val, &Parcel::WriteString);
1364 }
1365
WriteString16Vector(const std::vector<std::u16string> & val)1366 bool Parcel::WriteString16Vector(const std::vector<std::u16string> &val)
1367 {
1368 return WriteVector(val, &Parcel::WriteString16);
1369 }
1370
1371 template <typename T>
ReadVector(std::vector<T> * val,bool (Parcel::* Read)(T &))1372 bool Parcel::ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &))
1373 {
1374 if (val == nullptr) {
1375 return false;
1376 }
1377
1378 int32_t len = this->ReadInt32();
1379 if (len < 0) {
1380 return false;
1381 }
1382
1383 size_t readAbleSize = this->GetReadableBytes() / sizeof(T);
1384 size_t size = static_cast<size_t>(len);
1385 if ((size > readAbleSize) || (size > val->max_size())) {
1386 UTILS_LOGE("Failed to read vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1387 return false;
1388 }
1389 val->resize(size);
1390 if (val->size() < size) {
1391 return false;
1392 }
1393
1394 for (auto &v : *val) {
1395 if (!(this->*Read)(v)) {
1396 return false;
1397 }
1398 }
1399
1400 size_t padSize = this->GetPadSize(size * sizeof(T));
1401 this->SkipBytes(padSize);
1402 return true;
1403 }
1404
ReadBoolVector(std::vector<bool> * val)1405 bool Parcel::ReadBoolVector(std::vector<bool> *val)
1406 {
1407 if (val == nullptr) {
1408 return false;
1409 }
1410
1411 int32_t len = this->ReadInt32();
1412 if (len < 0) {
1413 return false;
1414 }
1415
1416 size_t readAbleSize = this->GetReadableBytes();
1417 size_t size = static_cast<size_t>(len);
1418 if ((size > readAbleSize) || (val->max_size() < size)) {
1419 UTILS_LOGE("Failed to read bool vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1420 return false;
1421 }
1422 val->resize(size);
1423 if (val->size() < size) {
1424 return false;
1425 }
1426
1427 for (size_t i = 0; i < size; ++i) {
1428 (*val)[i] = ReadBool();
1429 }
1430
1431 size_t padSize = this->GetPadSize(size * sizeof(bool));
1432 this->SkipBytes(padSize);
1433 return true;
1434 }
1435
ReadInt8Vector(std::vector<int8_t> * val)1436 bool Parcel::ReadInt8Vector(std::vector<int8_t> *val)
1437 {
1438 return ReadVector(val, &Parcel::ReadInt8Unaligned);
1439 }
1440
ReadInt16Vector(std::vector<int16_t> * val)1441 bool Parcel::ReadInt16Vector(std::vector<int16_t> *val)
1442 {
1443 return ReadVector(val, &Parcel::ReadInt16);
1444 }
1445
ReadInt32Vector(std::vector<int32_t> * val)1446 bool Parcel::ReadInt32Vector(std::vector<int32_t> *val)
1447 {
1448 return ReadVector(val, &Parcel::ReadInt32);
1449 }
1450
ReadInt64Vector(std::vector<int64_t> * val)1451 bool Parcel::ReadInt64Vector(std::vector<int64_t> *val)
1452 {
1453 return ReadVector(val, &Parcel::ReadInt64);
1454 }
1455
ReadUInt8Vector(std::vector<uint8_t> * val)1456 bool Parcel::ReadUInt8Vector(std::vector<uint8_t> *val)
1457 {
1458 return ReadVector(val, &Parcel::ReadUint8Unaligned);
1459 }
1460
ReadUInt16Vector(std::vector<uint16_t> * val)1461 bool Parcel::ReadUInt16Vector(std::vector<uint16_t> *val)
1462 {
1463 return ReadVector(val, &Parcel::ReadUint16Unaligned);
1464 }
1465
ReadUInt32Vector(std::vector<uint32_t> * val)1466 bool Parcel::ReadUInt32Vector(std::vector<uint32_t> *val)
1467 {
1468 return ReadVector(val, &Parcel::ReadUint32);
1469 }
1470
ReadUInt64Vector(std::vector<uint64_t> * val)1471 bool Parcel::ReadUInt64Vector(std::vector<uint64_t> *val)
1472 {
1473 return ReadVector(val, &Parcel::ReadUint64);
1474 }
1475
ReadFloatVector(std::vector<float> * val)1476 bool Parcel::ReadFloatVector(std::vector<float> *val)
1477 {
1478 return ReadVector(val, &Parcel::ReadFloat);
1479 }
1480
ReadDoubleVector(std::vector<double> * val)1481 bool Parcel::ReadDoubleVector(std::vector<double> *val)
1482 {
1483 return ReadVector(val, &Parcel::ReadDouble);
1484 }
1485
ReadStringVector(std::vector<std::string> * val)1486 bool Parcel::ReadStringVector(std::vector<std::string> *val)
1487 {
1488 if (val == nullptr) {
1489 return false;
1490 }
1491
1492 int32_t len = this->ReadInt32();
1493 if (len < 0) {
1494 return false;
1495 }
1496
1497 size_t readAbleSize = this->GetReadableBytes();
1498 size_t size = static_cast<size_t>(len);
1499 if ((size > readAbleSize) || (val->max_size() < size)) {
1500 UTILS_LOGE("Failed to read string vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1501 return false;
1502 }
1503 val->resize(size);
1504 if (val->size() < size) {
1505 return false;
1506 }
1507
1508 for (auto &v : *val) {
1509 v = ReadString();
1510 }
1511
1512 return true;
1513 }
1514
ReadString16Vector(std::vector<std::u16string> * val)1515 bool Parcel::ReadString16Vector(std::vector<std::u16string> *val)
1516 {
1517 if (val == nullptr) {
1518 return false;
1519 }
1520
1521 int32_t len = this->ReadInt32();
1522 if (len < 0) {
1523 return false;
1524 }
1525
1526 size_t readAbleSize = this->GetReadableBytes();
1527 size_t size = static_cast<size_t>(len);
1528 if ((size > readAbleSize) || (val->max_size() < size)) {
1529 UTILS_LOGE("Failed to read u16string vector, size = %{public}zu, readAbleSize = %{public}zu",
1530 size, readAbleSize);
1531 return false;
1532 }
1533
1534 val->resize(size);
1535 if (val->size() < size) {
1536 return false;
1537 }
1538
1539 for (auto &v : *val) {
1540 v = ReadString16();
1541 }
1542
1543 return true;
1544 }
1545 } // namespace OHOS
1546