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