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