1 /*
2 * Copyright (c) 2022 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 "thermal_temp_callback_stub.h"
18
19 namespace OHOS {
20
21 static const size_t DEFAULT_CPACITY = 204800; // 200K
22
Parcelable()23 Parcelable::Parcelable() : Parcelable(false) {}
24
Parcelable(bool asRemote)25 Parcelable::Parcelable(bool asRemote)
26 {
27 asRemote_ = asRemote;
28 behavior_ = 0;
29 }
30
Parcel(Allocator * allocator)31 Parcel::Parcel(Allocator* allocator)
32 {
33 if (allocator != nullptr) {
34 allocator_ = allocator;
35 } else {
36 allocator_ = new DefaultAllocator();
37 }
38
39 writeCursor_ = 0;
40 readCursor_ = 0;
41
42 data_ = nullptr;
43 dataSize_ = 0;
44 dataCapacity_ = 0;
45
46 maxDataCapacity_ = DEFAULT_CPACITY;
47 objectOffsets_ = nullptr;
48 objectCursor_ = 0;
49 objectsCapacity_ = 0;
50 }
51
Parcel()52 Parcel::Parcel() : Parcel(new DefaultAllocator()) {}
53
~Parcel()54 Parcel::~Parcel()
55 {
56 FlushBuffer();
57 delete allocator_;
58 }
59
GetWritableBytes() const60 size_t Parcel::GetWritableBytes() const
61 {
62 return 0;
63 }
64
GetReadableBytes() const65 size_t Parcel::GetReadableBytes() const
66 {
67 return 0;
68 }
69
CalcNewCapacity(size_t minNewCapacity)70 size_t Parcel::CalcNewCapacity(size_t minNewCapacity)
71 {
72 (void)minNewCapacity;
73 return 0;
74 }
75
EnsureWritableCapacity(size_t desireCapacity)76 bool Parcel::EnsureWritableCapacity(size_t desireCapacity)
77 {
78 (void)desireCapacity;
79 return false;
80 }
81
GetDataSize() const82 size_t Parcel::GetDataSize() const
83 {
84 return dataSize_;
85 }
86
GetData() const87 uintptr_t Parcel::GetData() const
88 {
89 return reinterpret_cast<uintptr_t>(data_);
90 }
91
GetObjectOffsets() const92 binder_size_t Parcel::GetObjectOffsets() const
93 {
94 return reinterpret_cast<binder_size_t>(objectOffsets_);
95 }
96
GetOffsetsSize() const97 size_t Parcel::GetOffsetsSize() const
98 {
99 return objectCursor_;
100 }
101
GetDataCapacity() const102 size_t Parcel::GetDataCapacity() const
103 {
104 return dataCapacity_;
105 }
106
SetMaxCapacity(size_t maxCapacity)107 bool Parcel::SetMaxCapacity(size_t maxCapacity)
108 {
109 (void)maxCapacity;
110 return false;
111 }
112
SetAllocator(Allocator * allocator)113 bool Parcel::SetAllocator(Allocator* allocator)
114 {
115 (void)allocator;
116 return false;
117 }
118
CheckOffsets()119 bool Parcel::CheckOffsets()
120 {
121 return false;
122 }
123
InjectOffsets(binder_size_t offsets,size_t offsetSize)124 void Parcel::InjectOffsets(binder_size_t offsets, size_t offsetSize)
125 {
126 (void)offsets;
127 (void)offsetSize;
128 }
129
FlushBuffer()130 void Parcel::FlushBuffer()
131 {
132 if (allocator_ == nullptr) {
133 return;
134 }
135
136 if (data_ != nullptr) {
137 allocator_->Dealloc(data_);
138 dataSize_ = 0;
139 writeCursor_ = 0;
140 readCursor_ = 0;
141 dataCapacity_ = 0;
142 data_ = nullptr;
143 }
144
145 if (objectOffsets_) {
146 objectHolder_.clear();
147 free(objectOffsets_);
148 objectCursor_ = 0;
149 objectOffsets_ = nullptr;
150 objectsCapacity_ = 0;
151 }
152 }
153
SetDataCapacity(size_t newCapacity)154 bool Parcel::SetDataCapacity(size_t newCapacity)
155 {
156 (void)newCapacity;
157 return false;
158 }
159
SetDataSize(size_t dataSize)160 bool Parcel::SetDataSize(size_t dataSize)
161 {
162 (void)dataSize;
163 return false;
164 }
165
WriteDataBytes(const void * data,size_t size)166 bool Parcel::WriteDataBytes(const void* data, size_t size)
167 {
168 (void)data;
169 (void)size;
170 return false;
171 }
172
WritePadBytes(size_t padSize)173 void Parcel::WritePadBytes(size_t padSize)
174 {
175 (void)padSize;
176 }
177
WriteBuffer(const void * data,size_t size)178 bool Parcel::WriteBuffer(const void* data, size_t size)
179 {
180 (void)data;
181 (void)size;
182 return false;
183 }
184
WriteBufferAddTerminator(const void * data,size_t size,size_t typeSize)185 bool Parcel::WriteBufferAddTerminator(const void* data, size_t size, size_t typeSize)
186 {
187 (void)data;
188 (void)size;
189 (void)typeSize;
190 return false;
191 }
192
WriteUnpadBuffer(const void * data,size_t size)193 bool Parcel::WriteUnpadBuffer(const void* data, size_t size)
194 {
195 return WriteBuffer(data, size);
196 }
197
198 template <typename T>
Write(T value)199 bool Parcel::Write(T value)
200 {
201 (void)value;
202 #ifdef MOCK_WRITE_RETURN_TRUE
203 return true;
204 #else
205 return false;
206 #endif
207 }
208
WriteBool(bool value)209 bool Parcel::WriteBool(bool value)
210 {
211 return Write<int32_t>(static_cast<int32_t>(value));
212 }
213
WriteBoolUnaligned(bool value)214 bool Parcel::WriteBoolUnaligned(bool value)
215 {
216 return Write<bool>(value);
217 }
218
WriteInt8(int8_t value)219 bool Parcel::WriteInt8(int8_t value)
220 {
221 return Write<int32_t>(static_cast<int32_t>(value));
222 }
223
WriteInt8Unaligned(int8_t value)224 bool Parcel::WriteInt8Unaligned(int8_t value)
225 {
226 return Write<int8_t>(value);
227 }
228
WriteInt16(int16_t value)229 bool Parcel::WriteInt16(int16_t value)
230 {
231 return Write<int32_t>(static_cast<int32_t>(value));
232 }
233
WriteInt16Unaligned(int16_t value)234 bool Parcel::WriteInt16Unaligned(int16_t value)
235 {
236 return Write<int16_t>(value);
237 }
238
WriteInt32(int32_t value)239 bool Parcel::WriteInt32(int32_t value)
240 {
241 return Write<int32_t>(value);
242 }
243
WriteInt64(int64_t value)244 bool Parcel::WriteInt64(int64_t value)
245 {
246 return Write<int64_t>(value);
247 }
248
WriteUint8(uint8_t value)249 bool Parcel::WriteUint8(uint8_t value)
250 {
251 return Write<uint32_t>(static_cast<uint32_t>(value));
252 }
253
WriteUint8Unaligned(uint8_t value)254 bool Parcel::WriteUint8Unaligned(uint8_t value)
255 {
256 return Write<uint8_t>(value);
257 }
258
WriteUint16(uint16_t value)259 bool Parcel::WriteUint16(uint16_t value)
260 {
261 return Write<uint32_t>(static_cast<uint32_t>(value));
262 }
263
WriteUint16Unaligned(uint16_t value)264 bool Parcel::WriteUint16Unaligned(uint16_t value)
265 {
266 return Write<uint16_t>(value);
267 }
268
WriteUint32(uint32_t value)269 bool Parcel::WriteUint32(uint32_t value)
270 {
271 return Write<uint32_t>(value);
272 }
273
WriteUint64(uint64_t value)274 bool Parcel::WriteUint64(uint64_t value)
275 {
276 return Write<uint64_t>(value);
277 }
278
WriteFloat(float value)279 bool Parcel::WriteFloat(float value)
280 {
281 return Write<float>(value);
282 }
283
WriteDouble(double value)284 bool Parcel::WriteDouble(double value)
285 {
286 return Write<double>(value);
287 }
288
WritePointer(uintptr_t value)289 bool Parcel::WritePointer(uintptr_t value)
290 {
291 return Write<binder_uintptr_t>(value);
292 }
293
WriteCString(const char * value)294 bool Parcel::WriteCString(const char* value)
295 {
296 (void)value;
297 return false;
298 }
299
WriteString(const std::string & value)300 bool Parcel::WriteString(const std::string& value)
301 {
302 (void)value;
303 return false;
304 }
305
WriteString16(const std::u16string & value)306 bool Parcel::WriteString16(const std::u16string& value)
307 {
308 (void)value;
309 return false;
310 }
311
WriteString16WithLength(const char16_t * value,size_t len)312 bool Parcel::WriteString16WithLength(const char16_t* value, size_t len)
313 {
314 (void)value;
315 (void)len;
316 return false;
317 }
318
WriteString8WithLength(const char * value,size_t len)319 bool Parcel::WriteString8WithLength(const char* value, size_t len)
320 {
321 (void)value;
322 (void)len;
323 return false;
324 }
325
EnsureObjectsCapacity()326 bool Parcel::EnsureObjectsCapacity()
327 {
328 return false;
329 }
330
WriteObjectOffset(binder_size_t offset)331 bool Parcel::WriteObjectOffset(binder_size_t offset)
332 {
333 (void)offset;
334 return false;
335 }
336
WriteRemoteObject(const Parcelable * object)337 bool Parcel::WriteRemoteObject(const Parcelable* object)
338 {
339 (void)object;
340 #ifdef MOCK_WRITE_REMOTE_OBJECT_RETURN_TRUE
341 return true;
342 #else
343 return false;
344 #endif
345 }
346
WriteParcelable(const Parcelable * object)347 bool Parcel::WriteParcelable(const Parcelable* object)
348 {
349 (void)object;
350 return false;
351 }
352
WriteStrongParcelable(const sptr<Parcelable> & object)353 bool Parcel::WriteStrongParcelable(const sptr<Parcelable>& object)
354 {
355 (void)object;
356 return false;
357 }
358
359 template <typename T>
Read(T & value)360 bool Parcel::Read(T& value)
361 {
362 (void)value;
363 return false;
364 }
365
366 template <typename T>
Read()367 T Parcel::Read()
368 {
369 #ifdef MOCK_READ_RETURN_ZERO
370 return 0;
371 #else
372 return 1;
373 #endif
374 }
375
ParseFrom(uintptr_t data,size_t size)376 bool Parcel::ParseFrom(uintptr_t data, size_t size)
377 {
378 (void)data;
379 (void)size;
380 return false;
381 }
382
ReadBuffer(size_t length)383 const uint8_t* Parcel::ReadBuffer(size_t length)
384 {
385 (void)length;
386 return nullptr;
387 }
388
ReadUnpadBuffer(size_t length)389 const uint8_t* Parcel::ReadUnpadBuffer(size_t length)
390 {
391 (void)length;
392 return nullptr;
393 }
394
SkipBytes(size_t bytes)395 void Parcel::SkipBytes(size_t bytes)
396 {
397 (void)bytes;
398 }
399
GetReadPosition()400 size_t Parcel::GetReadPosition()
401 {
402 return readCursor_;
403 }
404
RewindRead(size_t newPosition)405 bool Parcel::RewindRead(size_t newPosition)
406 {
407 (void)newPosition;
408 return false;
409 }
410
GetWritePosition()411 size_t Parcel::GetWritePosition()
412 {
413 return writeCursor_;
414 }
415
RewindWrite(size_t newPosition)416 bool Parcel::RewindWrite(size_t newPosition)
417 {
418 (void)newPosition;
419 return false;
420 }
421
ReadBool()422 bool Parcel::ReadBool()
423 {
424 int32_t temp = Read<int32_t>();
425 return (temp != 0);
426 }
427
ReadBoolUnaligned()428 bool Parcel::ReadBoolUnaligned()
429 {
430 return Read<bool>();
431 }
432
ReadInt8()433 int8_t Parcel::ReadInt8()
434 {
435 int32_t temp = Read<int32_t>();
436 return static_cast<int8_t>(temp);
437 }
438
ReadInt16()439 int16_t Parcel::ReadInt16()
440 {
441 int32_t temp = Read<int32_t>();
442 return static_cast<int16_t>(temp);
443 }
444
ReadInt32()445 int32_t Parcel::ReadInt32()
446 {
447 return Read<int32_t>();
448 }
449
ReadInt64()450 int64_t Parcel::ReadInt64()
451 {
452 return Read<int64_t>();
453 }
454
ReadUint8()455 uint8_t Parcel::ReadUint8()
456 {
457 uint32_t temp = Read<uint32_t>();
458 return static_cast<uint8_t>(temp);
459 }
460
ReadUint16()461 uint16_t Parcel::ReadUint16()
462 {
463 uint32_t temp = Read<uint32_t>();
464 return static_cast<uint16_t>(temp);
465 }
466
ReadUint32()467 uint32_t Parcel::ReadUint32()
468 {
469 return Read<uint32_t>();
470 }
471
ReadUint64()472 uint64_t Parcel::ReadUint64()
473 {
474 return Read<uint64_t>();
475 }
476
ReadFloat()477 float Parcel::ReadFloat()
478 {
479 return Read<float>();
480 }
481
ReadDouble()482 double Parcel::ReadDouble()
483 {
484 return Read<double>();
485 }
486
487 template <typename T>
ReadPadded(T & value)488 bool Parcel::ReadPadded(T& value)
489 {
490 (void)value;
491 return false;
492 }
493
ReadBool(bool & value)494 bool Parcel::ReadBool(bool& value)
495 {
496 return ReadPadded<bool>(value);
497 }
498
ReadInt8(int8_t & value)499 bool Parcel::ReadInt8(int8_t& value)
500 {
501 return ReadPadded<int8_t>(value);
502 }
503
ReadInt8Unaligned(int8_t & value)504 bool Parcel::ReadInt8Unaligned(int8_t& value)
505 {
506 return Read<int8_t>(value);
507 }
508
ReadInt16(int16_t & value)509 bool Parcel::ReadInt16(int16_t& value)
510 {
511 return ReadPadded<int16_t>(value);
512 }
513
ReadInt16Unaligned(int16_t & value)514 bool Parcel::ReadInt16Unaligned(int16_t& value)
515 {
516 return Read<int16_t>(value);
517 }
518
ReadInt32(int32_t & value)519 bool Parcel::ReadInt32(int32_t& value)
520 {
521 return Read<int32_t>(value);
522 }
523
ReadInt64(int64_t & value)524 bool Parcel::ReadInt64(int64_t& value)
525 {
526 return Read<int64_t>(value);
527 }
528
ReadUint8(uint8_t & value)529 bool Parcel::ReadUint8(uint8_t& value)
530 {
531 return ReadPadded<uint8_t>(value);
532 }
533
ReadUint8Unaligned(uint8_t & value)534 bool Parcel::ReadUint8Unaligned(uint8_t& value)
535 {
536 return Read<uint8_t>(value);
537 }
538
ReadUint16(uint16_t & value)539 bool Parcel::ReadUint16(uint16_t& value)
540 {
541 return ReadPadded<uint16_t>(value);
542 }
543
ReadUint16Unaligned(uint16_t & value)544 bool Parcel::ReadUint16Unaligned(uint16_t& value)
545 {
546 return Read<uint16_t>(value);
547 }
548
ReadUint32(uint32_t & value)549 bool Parcel::ReadUint32(uint32_t& value)
550 {
551 return Read<uint32_t>(value);
552 }
553
ReadUint64(uint64_t & value)554 bool Parcel::ReadUint64(uint64_t& value)
555 {
556 return Read<uint64_t>(value);
557 }
558
ReadFloat(float & value)559 bool Parcel::ReadFloat(float& value)
560 {
561 return Read<float>(value);
562 }
563
ReadDouble(double & value)564 bool Parcel::ReadDouble(double& value)
565 {
566 return Read<double>(value);
567 }
568
ReadPointer()569 uintptr_t Parcel::ReadPointer()
570 {
571 return Read<binder_uintptr_t>();
572 }
573
ReadCString()574 const char* Parcel::ReadCString()
575 {
576 return nullptr;
577 }
578
ReadString()579 const std::string Parcel::ReadString()
580 {
581 return "";
582 }
583
ReadString(std::string & value)584 bool Parcel::ReadString(std::string& value)
585 {
586 (void)value;
587 #ifdef MOCK_READSTRING_RETURN_TRUE
588 return true;
589 #else
590 return false;
591 #endif
592 }
593
ReadString16()594 const std::u16string Parcel::ReadString16()
595 {
596 #ifdef MOCK_READSTRING16_RETURN_NULL
597 return std::u16string();
598 #else
599 return PowerMgr::ThermalTempCallbackStub::GetDescriptor();
600 #endif
601 }
602
ReadString16(std::u16string & value)603 bool Parcel::ReadString16(std::u16string& value)
604 {
605 (void)value;
606 return false;
607 }
608
ReadString16WithLength(int32_t & readLength)609 const std::u16string Parcel::ReadString16WithLength(int32_t& readLength)
610 {
611 (void)readLength;
612 return std::u16string();
613 }
614
ReadString8WithLength(int32_t & readLength)615 const std::string Parcel::ReadString8WithLength(int32_t& readLength)
616 {
617 (void)readLength;
618 return std::string();
619 }
620
621 template <typename T1, typename T2>
WriteVector(const std::vector<T1> & val,bool (Parcel::* Write)(T2))622 bool Parcel::WriteVector(const std::vector<T1>& val, bool (Parcel::*Write)(T2))
623 {
624 (void)val;
625 #ifdef MOCK_WRITEVECTOR_RETURN_FALSE
626 return false;
627 #else
628 return true;
629 #endif
630 }
631
WriteBoolVector(const std::vector<bool> & val)632 bool Parcel::WriteBoolVector(const std::vector<bool>& val)
633 {
634 return WriteVector(val, &Parcel::WriteBool);
635 }
636
WriteInt8Vector(const std::vector<int8_t> & val)637 bool Parcel::WriteInt8Vector(const std::vector<int8_t>& val)
638 {
639 return WriteVector(val, &Parcel::WriteInt8Unaligned);
640 }
641
WriteInt16Vector(const std::vector<int16_t> & val)642 bool Parcel::WriteInt16Vector(const std::vector<int16_t>& val)
643 {
644 return WriteVector(val, &Parcel::WriteInt16);
645 }
646
WriteInt32Vector(const std::vector<int32_t> & val)647 bool Parcel::WriteInt32Vector(const std::vector<int32_t>& val)
648 {
649 return WriteVector(val, &Parcel::WriteInt32);
650 }
651
WriteInt64Vector(const std::vector<int64_t> & val)652 bool Parcel::WriteInt64Vector(const std::vector<int64_t>& val)
653 {
654 return WriteVector(val, &Parcel::WriteInt64);
655 }
656
WriteUInt8Vector(const std::vector<uint8_t> & val)657 bool Parcel::WriteUInt8Vector(const std::vector<uint8_t>& val)
658 {
659 return WriteVector(val, &Parcel::WriteUint8Unaligned);
660 }
661
WriteUInt16Vector(const std::vector<uint16_t> & val)662 bool Parcel::WriteUInt16Vector(const std::vector<uint16_t>& val)
663 {
664 return WriteVector(val, &Parcel::WriteUint16Unaligned);
665 }
666
WriteUInt32Vector(const std::vector<uint32_t> & val)667 bool Parcel::WriteUInt32Vector(const std::vector<uint32_t>& val)
668 {
669 return WriteVector(val, &Parcel::WriteUint32);
670 }
671
WriteUInt64Vector(const std::vector<uint64_t> & val)672 bool Parcel::WriteUInt64Vector(const std::vector<uint64_t>& val)
673 {
674 return WriteVector(val, &Parcel::WriteUint64);
675 }
676
WriteFloatVector(const std::vector<float> & val)677 bool Parcel::WriteFloatVector(const std::vector<float>& val)
678 {
679 return WriteVector(val, &Parcel::WriteFloat);
680 }
681
WriteDoubleVector(const std::vector<double> & val)682 bool Parcel::WriteDoubleVector(const std::vector<double>& val)
683 {
684 return WriteVector(val, &Parcel::WriteDouble);
685 }
686
WriteStringVector(const std::vector<std::string> & val)687 bool Parcel::WriteStringVector(const std::vector<std::string>& val)
688 {
689 return WriteVector(val, &Parcel::WriteString);
690 }
691
WriteString16Vector(const std::vector<std::u16string> & val)692 bool Parcel::WriteString16Vector(const std::vector<std::u16string>& val)
693 {
694 return WriteVector(val, &Parcel::WriteString16);
695 }
696
697 template <typename T>
ReadVector(std::vector<T> * val,bool (Parcel::* Read)(T &))698 bool Parcel::ReadVector(std::vector<T>* val, bool (Parcel::*Read)(T&))
699 {
700 (void)val;
701 return false;
702 }
703
ReadBoolVector(std::vector<bool> * val)704 bool Parcel::ReadBoolVector(std::vector<bool>* val)
705 {
706 (void)val;
707 return false;
708 }
709
ReadInt8Vector(std::vector<int8_t> * val)710 bool Parcel::ReadInt8Vector(std::vector<int8_t>* val)
711 {
712 return ReadVector(val, &Parcel::ReadInt8Unaligned);
713 }
714
ReadInt16Vector(std::vector<int16_t> * val)715 bool Parcel::ReadInt16Vector(std::vector<int16_t>* val)
716 {
717 return ReadVector(val, &Parcel::ReadInt16);
718 }
719
ReadInt32Vector(std::vector<int32_t> * val)720 bool Parcel::ReadInt32Vector(std::vector<int32_t>* val)
721 {
722 return ReadVector(val, &Parcel::ReadInt32);
723 }
724
ReadInt64Vector(std::vector<int64_t> * val)725 bool Parcel::ReadInt64Vector(std::vector<int64_t>* val)
726 {
727 return ReadVector(val, &Parcel::ReadInt64);
728 }
729
ReadUInt8Vector(std::vector<uint8_t> * val)730 bool Parcel::ReadUInt8Vector(std::vector<uint8_t>* val)
731 {
732 return ReadVector(val, &Parcel::ReadUint8Unaligned);
733 }
734
ReadUInt16Vector(std::vector<uint16_t> * val)735 bool Parcel::ReadUInt16Vector(std::vector<uint16_t>* val)
736 {
737 return ReadVector(val, &Parcel::ReadUint16Unaligned);
738 }
739
ReadUInt32Vector(std::vector<uint32_t> * val)740 bool Parcel::ReadUInt32Vector(std::vector<uint32_t>* val)
741 {
742 return ReadVector(val, &Parcel::ReadUint32);
743 }
744
ReadUInt64Vector(std::vector<uint64_t> * val)745 bool Parcel::ReadUInt64Vector(std::vector<uint64_t>* val)
746 {
747 return ReadVector(val, &Parcel::ReadUint64);
748 }
749
ReadFloatVector(std::vector<float> * val)750 bool Parcel::ReadFloatVector(std::vector<float>* val)
751 {
752 return ReadVector(val, &Parcel::ReadFloat);
753 }
754
ReadDoubleVector(std::vector<double> * val)755 bool Parcel::ReadDoubleVector(std::vector<double>* val)
756 {
757 return ReadVector(val, &Parcel::ReadDouble);
758 }
759
ReadStringVector(std::vector<std::string> * val)760 bool Parcel::ReadStringVector(std::vector<std::string>* val)
761 {
762 (void)val;
763 return false;
764 }
765
ReadString16Vector(std::vector<std::u16string> * val)766 bool Parcel::ReadString16Vector(std::vector<std::u16string>* val)
767 {
768 (void)val;
769 return false;
770 }
771 } // namespace OHOS
772