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