/* ** ** Copyright 2016, 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. */ #ifndef KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_ #define KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_ #include #include #include namespace keystore { template inline auto nullable(Fn fn, const android::Parcel& in, Args&&... args) -> NullOr(args)...))> { if (in.readInt32() != 1) { return {}; } return fn(in, std::forward(args)...); } template inline android::status_t nullable(Fn fn, const NullOr& arg, android::Parcel* out) { if (!arg.isOk()) { return out->writeInt32(0); } auto rc = out->writeInt32(1); if (rc != ::android::OK) return rc; return fn(arg.value(), out); } template inline android::status_t nullable(Fn fn, Arg&& arg, android::Parcel* out) { auto rc = out->writeInt32(1); if (rc != ::android::OK) return rc; return fn(std::forward(arg), out); } inline android::status_t nullable(android::Parcel* out) { return out->writeInt32(0); } /** * makes a copy only if inPlace is false */ hidl_vec readKeymasterBlob(const android::Parcel& in, bool inPlace = true); android::status_t writeKeymasterBlob(const hidl_vec& blob, android::Parcel* out); NullOr> readBlobAsByteArray(const android::Parcel& in, bool inPlace = true); android::status_t writeBlobAsByteArray(const NullOr&>& blob, android::Parcel* out); NullOr readKeyParameterFromParcel(const android::Parcel& in); android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out); hidl_vec readParamSetFromParcel(const android::Parcel& in); android::status_t writeParamSetToParcel(const hidl_vec& params, android::Parcel* out); KeyCharacteristics readKeyCharacteristicsFromParcel(const android::Parcel& in); android::status_t writeKeyCharacteristicsToParcel(const KeyCharacteristics& keyChara, android::Parcel* out); hidl_vec> readCertificateChainFromParcel(const android::Parcel& in); android::status_t writeCertificateChainToParcel(const hidl_vec>& certs, android::Parcel* out); } #endif // KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_