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