1 /*
2 **
3 ** Copyright 2019, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #define LOG_TAG "gatekeeperd"
19 
20 #include <gatekeeper/GateKeeperResponse.h>
21 
22 #include <binder/Parcel.h>
23 
24 #include <android-base/logging.h>
25 
26 namespace android {
27 namespace service {
28 namespace gatekeeper {
29 
readFromParcel(const Parcel * in)30 status_t GateKeeperResponse::readFromParcel(const Parcel* in) {
31     if (in == nullptr) {
32         LOG(ERROR) << "readFromParcel got null in parameter";
33         return BAD_VALUE;
34     }
35     timeout_ = 0;
36     should_reenroll_ = false;
37     payload_ = {};
38     response_code_ = ResponseCode(in->readInt32());
39     if (response_code_ == ResponseCode::OK) {
40         should_reenroll_ = in->readInt32();
41         ssize_t length = in->readInt32();
42         if (length > 0) {
43             length = in->readInt32();
44             const uint8_t* buf = reinterpret_cast<const uint8_t*>(in->readInplace(length));
45             if (buf == nullptr) {
46                 LOG(ERROR) << "readInplace returned null buffer for length " << length;
47                 return BAD_VALUE;
48             }
49             payload_.resize(length);
50             std::copy(buf, buf + length, payload_.data());
51         }
52     } else if (response_code_ == ResponseCode::RETRY) {
53         timeout_ = in->readInt32();
54     }
55     return NO_ERROR;
56 }
writeToParcel(Parcel * out) const57 status_t GateKeeperResponse::writeToParcel(Parcel* out) const {
58     if (out == nullptr) {
59         LOG(ERROR) << "writeToParcel got null out parameter";
60         return BAD_VALUE;
61     }
62     out->writeInt32(int32_t(response_code_));
63     if (response_code_ == ResponseCode::OK) {
64         out->writeInt32(should_reenroll_);
65         out->writeInt32(payload_.size());
66         if (payload_.size() != 0) {
67             out->writeInt32(payload_.size());
68             uint8_t* buf = reinterpret_cast<uint8_t*>(out->writeInplace(payload_.size()));
69             if (buf == nullptr) {
70                 LOG(ERROR) << "writeInplace returned null buffer for length " << payload_.size();
71                 return BAD_VALUE;
72             }
73             std::copy(payload_.begin(), payload_.end(), buf);
74         }
75     } else if (response_code_ == ResponseCode::RETRY) {
76         out->writeInt32(timeout_);
77     }
78     return NO_ERROR;
79 }
80 
81 }  // namespace gatekeeper
82 }  // namespace service
83 }  // namespace android
84