• 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 
WriteUnpadBuffer(const void * data,size_t size)352 bool Parcel::WriteUnpadBuffer(const void *data, size_t size)
353 {
354     return WriteBuffer(data, size);
355 }
356 
357 template <typename T>
Write(T value)358 bool Parcel::Write(T value)
359 {
360     size_t desireCapacity = sizeof(T);
361 
362     if (EnsureWritableCapacity(desireCapacity)) {
363         *reinterpret_cast<T *>(data_ + writeCursor_) = value;
364         writeCursor_ += desireCapacity;
365         dataSize_ += desireCapacity;
366         return true;
367     }
368 
369     return false;
370 }
371 
WriteBool(bool value)372 bool Parcel::WriteBool(bool value)
373 {
374     return Write<int32_t>(static_cast<int32_t>(value));
375 }
376 
WriteBoolUnaligned(bool value)377 bool Parcel::WriteBoolUnaligned(bool value)
378 {
379     return Write<bool>(value);
380 }
381 
WriteInt8(int8_t value)382 bool Parcel::WriteInt8(int8_t value)
383 {
384     return Write<int32_t>(static_cast<int32_t>(value));
385 }
386 
WriteInt8Unaligned(int8_t value)387 bool Parcel::WriteInt8Unaligned(int8_t value)
388 {
389     return Write<int8_t>(value);
390 }
391 
WriteInt16(int16_t value)392 bool Parcel::WriteInt16(int16_t value)
393 {
394     return Write<int32_t>(static_cast<int32_t>(value));
395 }
396 
WriteInt16Unaligned(int16_t value)397 bool Parcel::WriteInt16Unaligned(int16_t value)
398 {
399     return Write<int16_t>(value);
400 }
401 
WriteInt32(int32_t value)402 bool Parcel::WriteInt32(int32_t value)
403 {
404     return Write<int32_t>(value);
405 }
406 
WriteInt64(int64_t value)407 bool Parcel::WriteInt64(int64_t value)
408 {
409     return Write<int64_t>(value);
410 }
411 
WriteUint8(uint8_t value)412 bool Parcel::WriteUint8(uint8_t value)
413 {
414     return Write<uint32_t>(static_cast<uint32_t>(value));
415 }
416 
WriteUint8Unaligned(uint8_t value)417 bool Parcel::WriteUint8Unaligned(uint8_t value)
418 {
419     return Write<uint8_t>(value);
420 }
421 
WriteUint16(uint16_t value)422 bool Parcel::WriteUint16(uint16_t value)
423 {
424     return Write<uint32_t>(static_cast<uint32_t>(value));
425 }
426 
WriteUint16Unaligned(uint16_t value)427 bool Parcel::WriteUint16Unaligned(uint16_t value)
428 {
429     return Write<uint16_t>(value);
430 }
431 
WriteUint32(uint32_t value)432 bool Parcel::WriteUint32(uint32_t value)
433 {
434     return Write<uint32_t>(value);
435 }
436 
WriteUint64(uint64_t value)437 bool Parcel::WriteUint64(uint64_t value)
438 {
439     return Write<uint64_t>(value);
440 }
441 
WriteFloat(float value)442 bool Parcel::WriteFloat(float value)
443 {
444     return Write<float>(value);
445 }
446 
WriteDouble(double value)447 bool Parcel::WriteDouble(double value)
448 {
449     return Write<double>(value);
450 }
451 
WritePointer(uintptr_t value)452 bool Parcel::WritePointer(uintptr_t value)
453 {
454     return Write<binder_uintptr_t>(value);
455 }
456 
WriteCString(const char * value)457 bool Parcel::WriteCString(const char *value)
458 {
459     if (value == nullptr) {
460         return false;
461     }
462     int32_t dataLength = strlen(value);
463     int32_t desireCapacity = (dataLength + 1) * sizeof(char);
464     return WriteBuffer(value, desireCapacity);
465 }
466 
WriteString(const std::string & value)467 bool Parcel::WriteString(const std::string &value)
468 {
469     if (value.data() == nullptr) {
470         return WriteInt32(-1);
471     }
472 
473     int32_t dataLength = value.length();
474     int32_t desireCapacity = dataLength + sizeof(char);
475 
476     if (!Write<int32_t>(dataLength)) {
477         return false;
478     }
479 
480     return WriteBuffer(value.data(), desireCapacity);
481 }
482 
WriteString16(const std::u16string & value)483 bool Parcel::WriteString16(const std::u16string &value)
484 {
485     if (value.data() == nullptr) {
486         return WriteInt32(-1);
487     }
488 
489     int32_t dataLength = value.length();
490     int32_t desireCapacity = (dataLength + 1) * sizeof(char16_t);
491 
492     if (!Write<int32_t>(dataLength)) {
493         return false;
494     }
495 
496     return WriteBuffer(value.data(), desireCapacity);
497 }
498 
WriteString16WithLength(const char16_t * value,size_t len)499 bool Parcel::WriteString16WithLength(const char16_t *value, size_t len)
500 {
501     if (!value) {
502         return WriteInt32(-1);
503     }
504 
505     int32_t dataLength = len;
506     int32_t desireCapacity = (dataLength + 1) * sizeof(char16_t);
507     std::u16string u16str(reinterpret_cast<const char16_t *>(value), len);
508 
509     if (!Write<int32_t>(dataLength)) {
510         return false;
511     }
512 
513     return WriteBuffer(u16str.data(), desireCapacity);
514 }
515 
EnsureObjectsCapacity()516 bool Parcel::EnsureObjectsCapacity()
517 {
518     if ((objectsCapacity_ - objectCursor_) >= 1) {
519         return true;
520     }
521 
522     if (allocator_ == nullptr) {
523         return false;
524     }
525 
526     const int NEW_CAPACITY_ADD = 2;
527     const int NEW_CAPACITY_MULTI = 3;
528     const int NEW_CAPACITY_DIV = 2;
529     size_t newCapacity = ((objectsCapacity_ + NEW_CAPACITY_ADD) * NEW_CAPACITY_MULTI) / NEW_CAPACITY_DIV;
530     size_t newBytes = newCapacity * sizeof(binder_size_t);
531 
532     void *newOffsets = realloc(objectOffsets_, newBytes);
533     if (newOffsets == nullptr) {
534         return false;
535     }
536 
537     objectOffsets_ = reinterpret_cast<binder_size_t *>(newOffsets);
538     objectsCapacity_ = newCapacity;
539     return true;
540 }
541 
WriteObjectOffset(binder_size_t offset)542 bool Parcel::WriteObjectOffset(binder_size_t offset)
543 {
544     if (offset > dataSize_) {
545         return false;
546     }
547 
548     for (size_t index = 0; index < objectCursor_; index++) {
549         if (objectOffsets_[index] == offset) {
550             return false;
551         }
552     }
553 
554     objectOffsets_[objectCursor_] = offset;
555     objectCursor_++;
556     return true;
557 }
558 
WriteRemoteObject(const Parcelable * object)559 bool Parcel::WriteRemoteObject(const Parcelable *object)
560 {
561     size_t placeholder = writeCursor_;
562     // Parcelable is nullptr
563     if ((object == nullptr) || (!object->asRemote_)) {
564         return false;
565     }
566 
567     if (!EnsureObjectsCapacity()) {
568         return false;
569     }
570 
571     if (!object->Marshalling(*this)) {
572         return false;
573     }
574 
575     WriteObjectOffset(placeholder);
576 
577     if (object->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT)) {
578         objectHolder_.push_back(const_cast<Parcelable *>(object));
579     }
580 
581     return true;
582 }
583 
WriteParcelable(const Parcelable * object)584 bool Parcel::WriteParcelable(const Parcelable *object)
585 {
586     size_t placeholder = writeCursor_;
587     size_t restorSize = dataSize_;
588 
589     // Parcelable is nullptr
590     if (object == nullptr) {
591         // write the meta data to indicate pass an null object.
592         return WriteInt32(0);
593     }
594 
595     if (!object->asRemote_) {
596         // meta data indicate we have an parcelable object.
597         if (!WriteInt32(1)) {
598             return false;
599         }
600 
601         return object->Marshalling(*this);
602     }
603 
604     // Write the remote object flag
605     if (!WriteInt32(1)) {
606         return false;
607     }
608 
609     if (WriteRemoteObject(const_cast<Parcelable*>(object))) {
610         return true;
611     }
612 
613     // rollback the write position.
614     writeCursor_ = placeholder;
615     dataSize_ = restorSize;
616     return false;
617 }
618 
WriteStrongParcelable(const sptr<Parcelable> & object)619 bool Parcel::WriteStrongParcelable(const sptr<Parcelable> &object)
620 {
621     if (object == nullptr) {
622         WriteInt32(0);
623         return true;
624     }
625 
626     object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
627     return WriteParcelable(object.GetRefPtr());
628 }
629 
630 template <typename T>
Read(T & value)631 bool Parcel::Read(T &value)
632 {
633     size_t desireCapacity = sizeof(T);
634 
635     if (desireCapacity <= GetReadableBytes()) {
636         const void *data = data_ + readCursor_;
637         readCursor_ += desireCapacity;
638         value = *reinterpret_cast<const T *>(data);
639         return true;
640     }
641 
642     return false;
643 }
644 
645 template <typename T>
Read()646 T Parcel::Read()
647 {
648     T lvalue{};
649     return Read<T>(lvalue) ? lvalue : 0;
650 }
651 
ParseFrom(uintptr_t data,size_t size)652 bool Parcel::ParseFrom(uintptr_t data, size_t size)
653 {
654     if (data_ != nullptr) {
655         return false;
656     }
657 
658     data_ = reinterpret_cast<uint8_t *>(data);
659     dataCapacity_ = size;
660     dataSize_ = size;
661     /* data is alloc by driver, can not write again */
662     writable_ = false;
663     return true;
664 }
665 
ReadBuffer(size_t length)666 const uint8_t *Parcel::ReadBuffer(size_t length)
667 {
668     if (GetReadableBytes() >= length) {
669         uint8_t *buffer = data_ + readCursor_;
670         readCursor_ += length;
671         return buffer;
672     }
673 
674     return nullptr;
675 }
676 
ReadUnpadBuffer(size_t length)677 const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
678 {
679     if (GetReadableBytes() >= length) {
680         uint8_t *buffer = data_ + readCursor_;
681         readCursor_ += length;
682         SkipBytes(GetPadSize(length));
683         return buffer;
684     }
685 
686     return nullptr;
687 }
688 
SkipBytes(size_t bytes)689 void Parcel::SkipBytes(size_t bytes)
690 {
691     if (GetReadableBytes() >= bytes) {
692         readCursor_ += bytes;
693     } else if (readCursor_ < dataCapacity_) {
694         readCursor_ = dataCapacity_;
695     }
696 }
697 
GetReadPosition()698 size_t Parcel::GetReadPosition()
699 {
700     return readCursor_;
701 }
702 
RewindRead(size_t newPosition)703 bool Parcel::RewindRead(size_t newPosition)
704 {
705     if (newPosition > dataSize_) {
706         return false;
707     }
708     readCursor_ = newPosition;
709     return true;
710 }
711 
GetWritePosition()712 size_t Parcel::GetWritePosition()
713 {
714     return writeCursor_;
715 }
716 
RewindWrite(size_t newPosition)717 bool Parcel::RewindWrite(size_t newPosition)
718 {
719     if (newPosition > dataSize_) {
720         return false;
721     }
722     writeCursor_ = newPosition;
723     dataSize_ = newPosition;
724     return true;
725 }
726 
ReadBool()727 bool Parcel::ReadBool()
728 {
729     int32_t temp = Read<int32_t>();
730     return (temp != 0);
731 }
732 
ReadBoolUnaligned()733 bool Parcel::ReadBoolUnaligned()
734 {
735     return Read<bool>();
736 }
737 
ReadInt8()738 int8_t Parcel::ReadInt8()
739 {
740     int32_t temp = Read<int32_t>();
741     return static_cast<int8_t>(temp);
742 }
743 
ReadInt16()744 int16_t Parcel::ReadInt16()
745 {
746     int32_t temp = Read<int32_t>();
747     return static_cast<int16_t>(temp);
748 }
749 
ReadInt32()750 int32_t Parcel::ReadInt32()
751 {
752     return Read<int32_t>();
753 }
754 
ReadInt64()755 int64_t Parcel::ReadInt64()
756 {
757     return Read<int64_t>();
758 }
759 
ReadUint8()760 uint8_t Parcel::ReadUint8()
761 {
762     uint32_t temp = Read<uint32_t>();
763     return static_cast<uint8_t>(temp);
764 }
765 
ReadUint16()766 uint16_t Parcel::ReadUint16()
767 {
768     uint32_t temp = Read<uint32_t>();
769     return static_cast<uint16_t>(temp);
770 }
771 
ReadUint32()772 uint32_t Parcel::ReadUint32()
773 {
774     return Read<uint32_t>();
775 }
776 
ReadUint64()777 uint64_t Parcel::ReadUint64()
778 {
779     return Read<uint64_t>();
780 }
781 
ReadFloat()782 float Parcel::ReadFloat()
783 {
784     return Read<float>();
785 }
786 
ReadDouble()787 double Parcel::ReadDouble()
788 {
789     return Read<double>();
790 }
791 
792 template <typename T>
ReadPadded(T & value)793 bool Parcel::ReadPadded(T &value)
794 {
795     int32_t temp;
796     bool result = Read<int32_t>(temp);
797     if (result) {
798         value = static_cast<T>(temp);
799     }
800 
801     return result;
802 }
803 
ReadBool(bool & value)804 bool Parcel::ReadBool(bool &value)
805 {
806     return ReadPadded<bool>(value);
807 }
808 
ReadInt8(int8_t & value)809 bool Parcel::ReadInt8(int8_t &value)
810 {
811     return ReadPadded<int8_t>(value);
812 }
813 
ReadInt8Unaligned(int8_t & value)814 bool Parcel::ReadInt8Unaligned(int8_t &value)
815 {
816     return Read<int8_t>(value);
817 }
818 
ReadInt16(int16_t & value)819 bool Parcel::ReadInt16(int16_t &value)
820 {
821     return ReadPadded<int16_t>(value);
822 }
823 
ReadInt16Unaligned(int16_t & value)824 bool Parcel::ReadInt16Unaligned(int16_t &value)
825 {
826     return Read<int16_t>(value);
827 }
828 
ReadInt32(int32_t & value)829 bool Parcel::ReadInt32(int32_t &value)
830 {
831     return Read<int32_t>(value);
832 }
833 
ReadInt64(int64_t & value)834 bool Parcel::ReadInt64(int64_t &value)
835 {
836     return Read<int64_t>(value);
837 }
838 
ReadUint8(uint8_t & value)839 bool Parcel::ReadUint8(uint8_t &value)
840 {
841     return ReadPadded<uint8_t>(value);
842 }
843 
ReadUint8Unaligned(uint8_t & value)844 bool Parcel::ReadUint8Unaligned(uint8_t &value)
845 {
846     return Read<uint8_t>(value);
847 }
848 
ReadUint16(uint16_t & value)849 bool Parcel::ReadUint16(uint16_t &value)
850 {
851     return ReadPadded<uint16_t>(value);
852 }
853 
ReadUint16Unaligned(uint16_t & value)854 bool Parcel::ReadUint16Unaligned(uint16_t &value)
855 {
856     return Read<uint16_t>(value);
857 }
858 
ReadUint32(uint32_t & value)859 bool Parcel::ReadUint32(uint32_t &value)
860 {
861     return Read<uint32_t>(value);
862 }
863 
ReadUint64(uint64_t & value)864 bool Parcel::ReadUint64(uint64_t &value)
865 {
866     return Read<uint64_t>(value);
867 }
868 
ReadFloat(float & value)869 bool Parcel::ReadFloat(float &value)
870 {
871     return Read<float>(value);
872 }
873 
ReadDouble(double & value)874 bool Parcel::ReadDouble(double &value)
875 {
876     return Read<double>(value);
877 }
878 
ReadPointer()879 uintptr_t Parcel::ReadPointer()
880 {
881     return Read<binder_uintptr_t>();
882 }
883 
ReadCString()884 const char *Parcel::ReadCString()
885 {
886     size_t oldCursor = readCursor_;
887     const size_t avail = GetReadableBytes();
888     const char* cstr = reinterpret_cast<const char*>(data_ + readCursor_);
889     // is the string's trailing NUL within the parcel's valid bounds?
890     const char* eos = reinterpret_cast<const char*>(memchr(cstr, 0, avail));
891     if (eos != nullptr) {
892         const size_t dataLength = eos - cstr;
893         readCursor_ += (dataLength + 1);
894         SkipBytes(GetPadSize(dataLength + 1));
895         return cstr;
896     }
897     readCursor_ = oldCursor;
898     return nullptr;
899 }
900 
ReadString()901 const std::string Parcel::ReadString()
902 {
903     int32_t dataLength = 0;
904     size_t oldCursor = readCursor_;
905 
906     if (!Read<int32_t>(dataLength) || dataLength < 0) {
907         return std::string();
908     }
909 
910     size_t readCapacity = dataLength + 1;
911     if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
912         const uint8_t *dest = ReadBuffer(readCapacity);
913         if (dest != nullptr) {
914             const auto *str = reinterpret_cast<const char *>(dest);
915             SkipBytes(GetPadSize(readCapacity));
916             if (str[dataLength] == 0) {
917                 return std::string(str, dataLength);
918             }
919         }
920     }
921 
922     readCursor_ = oldCursor;
923     return std::string();
924 }
925 
ReadString(std::string & value)926 bool Parcel::ReadString(std::string &value)
927 {
928     int32_t dataLength = 0;
929     size_t oldCursor = readCursor_;
930 
931     if (!Read<int32_t>(dataLength) || dataLength < 0) {
932         value = std::string();
933         return false;
934     }
935 
936     size_t readCapacity = dataLength + 1;
937     if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
938         const uint8_t *dest = ReadBuffer(readCapacity);
939         if (dest != nullptr) {
940             const auto *str = reinterpret_cast<const char *>(dest);
941             SkipBytes(GetPadSize(readCapacity));
942             if (str[dataLength] == 0) {
943                 value = std::string(str, dataLength);
944                 return true;
945             }
946         }
947     }
948 
949     readCursor_ = oldCursor;
950     value = std::string();
951     return false;
952 }
953 
ReadString16()954 const std::u16string Parcel::ReadString16()
955 {
956     int32_t dataLength = 0;
957     size_t oldCursor = readCursor_;
958 
959     if (!Read<int32_t>(dataLength) || dataLength < 0) {
960         return std::u16string();
961     }
962 
963     size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
964     if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
965         const uint8_t *str = ReadBuffer(readCapacity);
966         if (str != nullptr) {
967             const auto *u16Str = reinterpret_cast<const char16_t *>(str);
968             SkipBytes(GetPadSize(readCapacity));
969             if (u16Str[dataLength] == 0) {
970                 return std::u16string(u16Str, dataLength);
971             }
972         }
973     }
974 
975     readCursor_ = oldCursor;
976     return std::u16string();
977 }
978 
ReadString16(std::u16string & value)979 bool Parcel::ReadString16(std::u16string &value)
980 {
981     int32_t dataLength = 0;
982     size_t oldCursor = readCursor_;
983 
984     if (!Read<int32_t>(dataLength) || dataLength < 0) {
985         value = std::u16string();
986         return false;
987     }
988 
989     size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
990     if (readCapacity <= GetReadableBytes()) {
991         const uint8_t *str = ReadBuffer(readCapacity);
992         if (str != nullptr) {
993             const auto *u16Str = reinterpret_cast<const char16_t *>(str);
994             SkipBytes(GetPadSize(readCapacity));
995             if (u16Str[dataLength] == 0) {
996                 value = std::u16string(u16Str, dataLength);
997                 return true;
998             }
999         }
1000     }
1001 
1002     readCursor_ = oldCursor;
1003     value = std::u16string();
1004     return false;
1005 }
1006 
ReadString16WithLength(int32_t & readLength)1007 const std::u16string Parcel::ReadString16WithLength(int32_t &readLength)
1008 {
1009     int32_t dataLength = 0;
1010     size_t oldCursor = readCursor_;
1011 
1012     if (!Read<int32_t>(dataLength)) {
1013         return std::u16string();
1014     }
1015 
1016     if (dataLength < 0) {
1017         readLength = dataLength;
1018         return std::u16string();
1019     }
1020 
1021     size_t readCapacity = (dataLength + 1) * sizeof(char16_t);
1022     if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) {
1023         const uint8_t *str = ReadBuffer(readCapacity);
1024         if (str != nullptr) {
1025             const auto *u16Str = reinterpret_cast<const char16_t *>(str);
1026             SkipBytes(GetPadSize(readCapacity));
1027             if (u16Str[dataLength] == 0) {
1028                 readLength = dataLength;
1029                 return std::u16string(u16Str, dataLength);
1030             }
1031         }
1032     }
1033 
1034     readCursor_ = oldCursor;
1035     return std::u16string();
1036 }
1037 
Alloc(size_t size)1038 void *DefaultAllocator::Alloc(size_t size)
1039 {
1040     return malloc(size);
1041 }
1042 
Dealloc(void * data)1043 void DefaultAllocator::Dealloc(void *data)
1044 {
1045     if (data != nullptr) {
1046         free(data);
1047     }
1048 }
1049 
Realloc(void * data,size_t newSize)1050 void *DefaultAllocator::Realloc(void *data, size_t newSize)
1051 {
1052     return realloc(data, newSize);
1053 }
1054 
1055 template <typename T1, typename T2>
WriteVector(const std::vector<T1> & val,bool (Parcel::* Write)(T2))1056 bool Parcel::WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2))
1057 {
1058     if (val.size() > INT_MAX) {
1059         return false;
1060     }
1061 
1062     if (!this->WriteInt32(static_cast<int32_t>(val.size()))) {
1063         return false;
1064     }
1065 
1066     for (const auto &v : val) {
1067         if (!(this->*Write)(v)) {
1068             return false;
1069         }
1070     }
1071 
1072     size_t padSize = this->GetPadSize(val.size() * sizeof(T1));
1073     this->WritePadBytes(padSize);
1074     return true;
1075 }
1076 
WriteBoolVector(const std::vector<bool> & val)1077 bool Parcel::WriteBoolVector(const std::vector<bool> &val)
1078 {
1079     return WriteVector(val, &Parcel::WriteBool);
1080 }
1081 
WriteInt8Vector(const std::vector<int8_t> & val)1082 bool Parcel::WriteInt8Vector(const std::vector<int8_t> &val)
1083 {
1084     return WriteVector(val, &Parcel::WriteInt8Unaligned);
1085 }
1086 
WriteInt16Vector(const std::vector<int16_t> & val)1087 bool Parcel::WriteInt16Vector(const std::vector<int16_t> &val)
1088 {
1089     return WriteVector(val, &Parcel::WriteInt16);
1090 }
1091 
WriteInt32Vector(const std::vector<int32_t> & val)1092 bool Parcel::WriteInt32Vector(const std::vector<int32_t> &val)
1093 {
1094     return WriteVector(val, &Parcel::WriteInt32);
1095 }
1096 
WriteInt64Vector(const std::vector<int64_t> & val)1097 bool Parcel::WriteInt64Vector(const std::vector<int64_t> &val)
1098 {
1099     return WriteVector(val, &Parcel::WriteInt64);
1100 }
1101 
WriteUInt8Vector(const std::vector<uint8_t> & val)1102 bool Parcel::WriteUInt8Vector(const std::vector<uint8_t> &val)
1103 {
1104     return WriteVector(val, &Parcel::WriteUint8Unaligned);
1105 }
1106 
WriteUInt16Vector(const std::vector<uint16_t> & val)1107 bool Parcel::WriteUInt16Vector(const std::vector<uint16_t> &val)
1108 {
1109     return WriteVector(val, &Parcel::WriteUint16Unaligned);
1110 }
1111 
WriteUInt32Vector(const std::vector<uint32_t> & val)1112 bool Parcel::WriteUInt32Vector(const std::vector<uint32_t> &val)
1113 {
1114     return WriteVector(val, &Parcel::WriteUint32);
1115 }
1116 
WriteUInt64Vector(const std::vector<uint64_t> & val)1117 bool Parcel::WriteUInt64Vector(const std::vector<uint64_t> &val)
1118 {
1119     return WriteVector(val, &Parcel::WriteUint64);
1120 }
1121 
WriteFloatVector(const std::vector<float> & val)1122 bool Parcel::WriteFloatVector(const std::vector<float> &val)
1123 {
1124     return WriteVector(val, &Parcel::WriteFloat);
1125 }
1126 
WriteDoubleVector(const std::vector<double> & val)1127 bool Parcel::WriteDoubleVector(const std::vector<double> &val)
1128 {
1129     return WriteVector(val, &Parcel::WriteDouble);
1130 }
1131 
WriteStringVector(const std::vector<std::string> & val)1132 bool Parcel::WriteStringVector(const std::vector<std::string> &val)
1133 {
1134     return WriteVector(val, &Parcel::WriteString);
1135 }
1136 
WriteString16Vector(const std::vector<std::u16string> & val)1137 bool Parcel::WriteString16Vector(const std::vector<std::u16string> &val)
1138 {
1139     return WriteVector(val, &Parcel::WriteString16);
1140 }
1141 
1142 template <typename T>
ReadVector(std::vector<T> * val,bool (Parcel::* Read)(T &))1143 bool Parcel::ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &))
1144 {
1145     if (val == nullptr) {
1146         return false;
1147     }
1148 
1149     int32_t len = this->ReadInt32();
1150     if (len < 0) {
1151         return false;
1152     }
1153 
1154     size_t readAbleSize = this->GetReadableBytes();
1155     size_t size = static_cast<size_t>(len);
1156     if ((size > readAbleSize) || (size > val->max_size())) {
1157         UTILS_LOGE("Failed to read vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1158         return false;
1159     }
1160     val->resize(size);
1161     if (val->size() < size) {
1162         return false;
1163     }
1164 
1165     for (auto &v : *val) {
1166         if (!(this->*Read)(v)) {
1167             return false;
1168         }
1169     }
1170 
1171     size_t padSize = this->GetPadSize(size * sizeof(T));
1172     this->SkipBytes(padSize);
1173     return true;
1174 }
1175 
ReadBoolVector(std::vector<bool> * val)1176 bool Parcel::ReadBoolVector(std::vector<bool> *val)
1177 {
1178     if (val == nullptr) {
1179         return false;
1180     }
1181 
1182     int32_t len = this->ReadInt32();
1183     if (len < 0) {
1184         return false;
1185     }
1186 
1187     size_t readAbleSize = this->GetReadableBytes();
1188     size_t size = static_cast<size_t>(len);
1189     if ((size > readAbleSize) || (val->max_size() < size)) {
1190         UTILS_LOGE("Failed to read bool vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1191         return false;
1192     }
1193     val->resize(size);
1194     if (val->size() < size) {
1195         return false;
1196     }
1197 
1198     for (size_t i = 0; i < size; ++i) {
1199         (*val)[i] = ReadBool();
1200     }
1201 
1202     size_t padSize = this->GetPadSize(size * sizeof(bool));
1203     this->SkipBytes(padSize);
1204     return true;
1205 }
1206 
ReadInt8Vector(std::vector<int8_t> * val)1207 bool Parcel::ReadInt8Vector(std::vector<int8_t> *val)
1208 {
1209     return ReadVector(val, &Parcel::ReadInt8Unaligned);
1210 }
1211 
ReadInt16Vector(std::vector<int16_t> * val)1212 bool Parcel::ReadInt16Vector(std::vector<int16_t> *val)
1213 {
1214     return ReadVector(val, &Parcel::ReadInt16);
1215 }
1216 
ReadInt32Vector(std::vector<int32_t> * val)1217 bool Parcel::ReadInt32Vector(std::vector<int32_t> *val)
1218 {
1219     return ReadVector(val, &Parcel::ReadInt32);
1220 }
1221 
ReadInt64Vector(std::vector<int64_t> * val)1222 bool Parcel::ReadInt64Vector(std::vector<int64_t> *val)
1223 {
1224     return ReadVector(val, &Parcel::ReadInt64);
1225 }
1226 
ReadUInt8Vector(std::vector<uint8_t> * val)1227 bool Parcel::ReadUInt8Vector(std::vector<uint8_t> *val)
1228 {
1229     return ReadVector(val, &Parcel::ReadUint8Unaligned);
1230 }
1231 
ReadUInt16Vector(std::vector<uint16_t> * val)1232 bool Parcel::ReadUInt16Vector(std::vector<uint16_t> *val)
1233 {
1234     return ReadVector(val, &Parcel::ReadUint16Unaligned);
1235 }
1236 
ReadUInt32Vector(std::vector<uint32_t> * val)1237 bool Parcel::ReadUInt32Vector(std::vector<uint32_t> *val)
1238 {
1239     return ReadVector(val, &Parcel::ReadUint32);
1240 }
1241 
ReadUInt64Vector(std::vector<uint64_t> * val)1242 bool Parcel::ReadUInt64Vector(std::vector<uint64_t> *val)
1243 {
1244     return ReadVector(val, &Parcel::ReadUint64);
1245 }
1246 
ReadFloatVector(std::vector<float> * val)1247 bool Parcel::ReadFloatVector(std::vector<float> *val)
1248 {
1249     return ReadVector(val, &Parcel::ReadFloat);
1250 }
1251 
ReadDoubleVector(std::vector<double> * val)1252 bool Parcel::ReadDoubleVector(std::vector<double> *val)
1253 {
1254     return ReadVector(val, &Parcel::ReadDouble);
1255 }
1256 
ReadStringVector(std::vector<std::string> * val)1257 bool Parcel::ReadStringVector(std::vector<std::string> *val)
1258 {
1259     if (val == nullptr) {
1260         return false;
1261     }
1262 
1263     int32_t len = this->ReadInt32();
1264     if (len < 0) {
1265         return false;
1266     }
1267 
1268     size_t readAbleSize = this->GetReadableBytes();
1269     size_t size = static_cast<size_t>(len);
1270     if ((size > readAbleSize) || (val->max_size() < size)) {
1271         UTILS_LOGE("Failed to read string vector, size = %{public}zu, readAbleSize = %{public}zu", size, readAbleSize);
1272         return false;
1273     }
1274     val->resize(size);
1275     if (val->size() < size) {
1276         return false;
1277     }
1278 
1279     for (auto &v : *val) {
1280         v = ReadString();
1281     }
1282 
1283     return true;
1284 }
1285 
ReadString16Vector(std::vector<std::u16string> * val)1286 bool Parcel::ReadString16Vector(std::vector<std::u16string> *val)
1287 {
1288     if (val == nullptr) {
1289         return false;
1290     }
1291 
1292     int32_t len = this->ReadInt32();
1293     if (len < 0) {
1294         return false;
1295     }
1296 
1297     size_t readAbleSize = this->GetReadableBytes();
1298     size_t size = static_cast<size_t>(len);
1299     if ((size > readAbleSize) || (val->max_size() < size)) {
1300         UTILS_LOGE("Failed to read u16string vector, size = %{public}zu, readAbleSize = %{public}zu",
1301             size, readAbleSize);
1302         return false;
1303     }
1304 
1305     val->resize(size);
1306     if (val->size() < size) {
1307         return false;
1308     }
1309 
1310     for (auto &v : *val) {
1311         v = ReadString16();
1312     }
1313 
1314     return true;
1315 }
1316 }  // namespace OHOS
1317