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