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