diff options
author | 2021-09-10 00:38:33 +0000 | |
---|---|---|
committer | 2021-09-10 00:38:33 +0000 | |
commit | cf2e66bf19e65be7bd8bd9f73a3d4bcb229ebdde (patch) | |
tree | 5178570d414e40a0ef9fc9ba485836515cc66be7 /libs | |
parent | 33e1d32ceb839c4f223830dccd22a0209522cf9d (diff) | |
parent | fb39f9c06808b3f1d9684594de395797dd2825af (diff) |
Merge "Add marshal() and unmarshal() APIs."
Diffstat (limited to 'libs')
-rw-r--r-- | libs/binder/ndk/include_ndk/android/binder_parcel.h | 35 | ||||
-rw-r--r-- | libs/binder/ndk/libbinder_ndk.map.txt | 2 | ||||
-rw-r--r-- | libs/binder/ndk/parcel.cpp | 28 |
3 files changed, 65 insertions, 0 deletions
diff --git a/libs/binder/ndk/include_ndk/android/binder_parcel.h b/libs/binder/ndk/include_ndk/android/binder_parcel.h index 527b151457..a2f5c93cb9 100644 --- a/libs/binder/ndk/include_ndk/android/binder_parcel.h +++ b/libs/binder/ndk/include_ndk/android/binder_parcel.h @@ -1163,6 +1163,41 @@ binder_status_t AParcel_appendFrom(const AParcel* from, AParcel* to, int32_t sta * \return A parcel which is not related to any IBinder objects. */ AParcel* AParcel_create() __INTRODUCED_IN(31); + +/** + * Marshals the raw bytes of the Parcel to a buffer. + * + * The parcel must not contain any binders or file descriptors. + * + * The data you retrieve here must not be placed in any kind of persistent storage. (on local disk, + * across a network, etc). For that, you should use standard serialization or another kind of + * general serialization mechanism. The Parcel marshalled representation is highly optimized for + * local IPC, and as such does not attempt to maintain compatibility with data created in different + * versions of the platform. + * + * \param parcel The parcel of which to get the data. + * \param buffer The buffer to copy the raw bytes to. + * \param start The start position in the buffer to copy from. + * \param len The size of the data to copy, buffer size must be larger or equal to this. + * + * \return STATUS_OK on success, STATUS_INVALID_OPERATION if parcel contains binders or file + * descriptors. STATUS_BAD_VALUE if the buffer size is less than parcel size. + */ +binder_status_t AParcel_marshal(const AParcel* parcel, uint8_t* buffer, size_t start, size_t len) + __INTRODUCED_IN(33); + +/** + * Set the data in the parcel to the raw bytes from the buffer. + * + * \param parcel The parcel to set data. + * \param buffer The data buffer to set. + * \param len The size of the data to set. + * + * \return STATUS_OK on success. + */ +binder_status_t AParcel_unmarshal(AParcel* parcel, const uint8_t* buffer, size_t len) + __INTRODUCED_IN(33); + __END_DECLS /** @} */ diff --git a/libs/binder/ndk/libbinder_ndk.map.txt b/libs/binder/ndk/libbinder_ndk.map.txt index 4eb51c4379..ac892db1c4 100644 --- a/libs/binder/ndk/libbinder_ndk.map.txt +++ b/libs/binder/ndk/libbinder_ndk.map.txt @@ -144,6 +144,8 @@ LIBBINDER_NDK31 { # introduced=31 LIBBINDER_NDK33 { # introduced=33 global: AIBinder_Class_disableInterfaceTokenHeader; + AParcel_marshal; + AParcel_unmarshal; }; LIBBINDER_NDK_PLATFORM { diff --git a/libs/binder/ndk/parcel.cpp b/libs/binder/ndk/parcel.cpp index b2f21c7408..c320e8d6df 100644 --- a/libs/binder/ndk/parcel.cpp +++ b/libs/binder/ndk/parcel.cpp @@ -673,4 +673,32 @@ AParcel* AParcel_create() { return new AParcel(nullptr); } +binder_status_t AParcel_marshal(const AParcel* parcel, uint8_t* buffer, size_t start, size_t len) { + if (parcel->get()->objectsCount()) { + return STATUS_INVALID_OPERATION; + } + int32_t dataSize = AParcel_getDataSize(parcel); + if (len > static_cast<size_t>(dataSize) || start > static_cast<size_t>(dataSize) - len) { + return STATUS_BAD_VALUE; + } + const uint8_t* internalBuffer = parcel->get()->data(); + memcpy(buffer, internalBuffer + start, len); + return STATUS_OK; +} + +binder_status_t AParcel_unmarshal(AParcel* parcel, const uint8_t* buffer, size_t len) { + status_t status = parcel->get()->setDataSize(len); + if (status != ::android::OK) { + return PruneStatusT(status); + } + parcel->get()->setDataPosition(0); + + void* raw = parcel->get()->writeInplace(len); + if (raw == nullptr) { + return STATUS_NO_MEMORY; + } + memcpy(raw, buffer, len); + return STATUS_OK; +} + // @END |