1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h> 18*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcelable.h> 19*38e8c45fSAndroid Build Coastguard Worker #include <binder/ParcelableHolder.h> 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #define RETURN_ON_FAILURE(expr) \ 22*38e8c45fSAndroid Build Coastguard Worker do { \ 23*38e8c45fSAndroid Build Coastguard Worker android::status_t _status = (expr); \ 24*38e8c45fSAndroid Build Coastguard Worker if (_status != android::OK) return _status; \ 25*38e8c45fSAndroid Build Coastguard Worker } while (false) 26*38e8c45fSAndroid Build Coastguard Worker 27*38e8c45fSAndroid Build Coastguard Worker namespace android { 28*38e8c45fSAndroid Build Coastguard Worker namespace os { writeToParcel(Parcel * p) const29*38e8c45fSAndroid Build Coastguard Workerstatus_t ParcelableHolder::writeToParcel(Parcel* p) const { 30*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeInt32(static_cast<int32_t>(this->getStability()))); 31*38e8c45fSAndroid Build Coastguard Worker if (this->mParcelPtr) { 32*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeInt32(this->mParcelPtr->dataSize())); 33*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->appendFrom(this->mParcelPtr.get(), 0, this->mParcelPtr->dataSize())); 34*38e8c45fSAndroid Build Coastguard Worker return OK; 35*38e8c45fSAndroid Build Coastguard Worker } 36*38e8c45fSAndroid Build Coastguard Worker if (this->mParcelable) { 37*38e8c45fSAndroid Build Coastguard Worker size_t sizePos = p->dataPosition(); 38*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeInt32(0)); 39*38e8c45fSAndroid Build Coastguard Worker size_t dataStartPos = p->dataPosition(); 40*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeString16(this->mParcelableName)); 41*38e8c45fSAndroid Build Coastguard Worker this->mParcelable->writeToParcel(p); 42*38e8c45fSAndroid Build Coastguard Worker size_t dataSize = p->dataPosition() - dataStartPos; 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker p->setDataPosition(sizePos); 45*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeInt32(dataSize)); 46*38e8c45fSAndroid Build Coastguard Worker p->setDataPosition(p->dataPosition() + dataSize); 47*38e8c45fSAndroid Build Coastguard Worker return OK; 48*38e8c45fSAndroid Build Coastguard Worker } 49*38e8c45fSAndroid Build Coastguard Worker 50*38e8c45fSAndroid Build Coastguard Worker RETURN_ON_FAILURE(p->writeInt32(0)); 51*38e8c45fSAndroid Build Coastguard Worker return OK; 52*38e8c45fSAndroid Build Coastguard Worker } 53*38e8c45fSAndroid Build Coastguard Worker readFromParcel(const Parcel * p)54*38e8c45fSAndroid Build Coastguard Workerstatus_t ParcelableHolder::readFromParcel(const Parcel* p) { 55*38e8c45fSAndroid Build Coastguard Worker int32_t wireStability; 56*38e8c45fSAndroid Build Coastguard Worker if (status_t status = p->readInt32(&wireStability); status != OK) return status; 57*38e8c45fSAndroid Build Coastguard Worker if (static_cast<int32_t>(this->mStability) != wireStability) return BAD_VALUE; 58*38e8c45fSAndroid Build Coastguard Worker 59*38e8c45fSAndroid Build Coastguard Worker this->mParcelable = nullptr; 60*38e8c45fSAndroid Build Coastguard Worker this->mParcelableName = std::nullopt; 61*38e8c45fSAndroid Build Coastguard Worker int32_t rawDataSize; 62*38e8c45fSAndroid Build Coastguard Worker 63*38e8c45fSAndroid Build Coastguard Worker status_t status = p->readInt32(&rawDataSize); 64*38e8c45fSAndroid Build Coastguard Worker if (status != android::OK || rawDataSize < 0) { 65*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr = nullptr; 66*38e8c45fSAndroid Build Coastguard Worker return status != android::OK ? status : BAD_VALUE; 67*38e8c45fSAndroid Build Coastguard Worker } 68*38e8c45fSAndroid Build Coastguard Worker if (rawDataSize == 0) { 69*38e8c45fSAndroid Build Coastguard Worker if (this->mParcelPtr) { 70*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr = nullptr; 71*38e8c45fSAndroid Build Coastguard Worker } 72*38e8c45fSAndroid Build Coastguard Worker return OK; 73*38e8c45fSAndroid Build Coastguard Worker } 74*38e8c45fSAndroid Build Coastguard Worker 75*38e8c45fSAndroid Build Coastguard Worker size_t dataSize = rawDataSize; 76*38e8c45fSAndroid Build Coastguard Worker 77*38e8c45fSAndroid Build Coastguard Worker size_t dataStartPos = p->dataPosition(); 78*38e8c45fSAndroid Build Coastguard Worker 79*38e8c45fSAndroid Build Coastguard Worker if (dataStartPos > SIZE_MAX - dataSize) { 80*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr = nullptr; 81*38e8c45fSAndroid Build Coastguard Worker return BAD_VALUE; 82*38e8c45fSAndroid Build Coastguard Worker } 83*38e8c45fSAndroid Build Coastguard Worker 84*38e8c45fSAndroid Build Coastguard Worker if (!this->mParcelPtr) { 85*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr = std::make_unique<Parcel>(); 86*38e8c45fSAndroid Build Coastguard Worker } 87*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr->freeData(); 88*38e8c45fSAndroid Build Coastguard Worker 89*38e8c45fSAndroid Build Coastguard Worker status = this->mParcelPtr->appendFrom(p, dataStartPos, dataSize); 90*38e8c45fSAndroid Build Coastguard Worker if (status != android::OK) { 91*38e8c45fSAndroid Build Coastguard Worker this->mParcelPtr = nullptr; 92*38e8c45fSAndroid Build Coastguard Worker return status; 93*38e8c45fSAndroid Build Coastguard Worker } 94*38e8c45fSAndroid Build Coastguard Worker p->setDataPosition(dataStartPos + dataSize); 95*38e8c45fSAndroid Build Coastguard Worker return OK; 96*38e8c45fSAndroid Build Coastguard Worker } 97*38e8c45fSAndroid Build Coastguard Worker } // namespace os 98*38e8c45fSAndroid Build Coastguard Worker } // namespace android 99