summaryrefslogtreecommitdiff
path: root/libs/binder/ParcelableHolder.cpp
blob: 2e86b7415a52c5e4f05b1cbe241b11c547f287ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/Parcel.h>
#include <binder/Parcelable.h>
#include <binder/ParcelableHolder.h>

#define RETURN_ON_FAILURE(expr)                     \
    do {                                            \
        android::status_t _status = (expr);         \
        if (_status != android::OK) return _status; \
    } while (false)

namespace android {
namespace os {
status_t ParcelableHolder::writeToParcel(Parcel* p) const {
    RETURN_ON_FAILURE(p->writeInt32(static_cast<int32_t>(this->getStability())));
    if (this->mParcelPtr) {
        RETURN_ON_FAILURE(p->writeInt32(this->mParcelPtr->dataSize()));
        RETURN_ON_FAILURE(p->appendFrom(this->mParcelPtr.get(), 0, this->mParcelPtr->dataSize()));
        return OK;
    }
    if (this->mParcelable) {
        size_t sizePos = p->dataPosition();
        RETURN_ON_FAILURE(p->writeInt32(0));
        size_t dataStartPos = p->dataPosition();
        RETURN_ON_FAILURE(p->writeString16(this->mParcelableName));
        this->mParcelable->writeToParcel(p);
        size_t dataSize = p->dataPosition() - dataStartPos;

        p->setDataPosition(sizePos);
        RETURN_ON_FAILURE(p->writeInt32(dataSize));
        p->setDataPosition(p->dataPosition() + dataSize);
        return OK;
    }

    RETURN_ON_FAILURE(p->writeInt32(0));
    return OK;
}

status_t ParcelableHolder::readFromParcel(const Parcel* p) {
    this->mStability = static_cast<Stability>(p->readInt32());
    this->mParcelable = nullptr;
    this->mParcelableName = std::nullopt;
    int32_t rawDataSize;

    status_t status = p->readInt32(&rawDataSize);
    if (status != android::OK || rawDataSize < 0) {
        this->mParcelPtr = nullptr;
        return status != android::OK ? status : BAD_VALUE;
    }
    if (rawDataSize == 0) {
        if (this->mParcelPtr) {
            this->mParcelPtr = nullptr;
        }
        return OK;
    }

    size_t dataSize = rawDataSize;

    size_t dataStartPos = p->dataPosition();

    if (dataStartPos > SIZE_MAX - dataSize) {
        this->mParcelPtr = nullptr;
        return BAD_VALUE;
    }

    if (!this->mParcelPtr) {
        this->mParcelPtr = std::make_unique<Parcel>();
    }
    this->mParcelPtr->freeData();

    status = this->mParcelPtr->appendFrom(p, dataStartPos, dataSize);
    if (status != android::OK) {
        this->mParcelPtr = nullptr;
        return status;
    }
    p->setDataPosition(dataStartPos + dataSize);
    return OK;
}
} // namespace os
} // namespace android