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