Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <android/binder_parcel.h> |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 18 | #include "parcel_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 19 | |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 20 | #include "ibinder_internal.h" |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 21 | #include "status_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 22 | |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 23 | #include <limits> |
| 24 | |
| 25 | #include <android-base/logging.h> |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 27 | #include <binder/Parcel.h> |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 28 | #include <binder/ParcelFileDescriptor.h> |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 29 | #include <utils/Unicode.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 30 | |
| 31 | using ::android::IBinder; |
| 32 | using ::android::Parcel; |
| 33 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 34 | using ::android::status_t; |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 35 | using ::android::base::unique_fd; |
| 36 | using ::android::os::ParcelFileDescriptor; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 37 | |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 38 | template <typename T> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 39 | using ContiguousArrayAllocator = T* (*)(void* arrayData, size_t length); |
| 40 | |
| 41 | template <typename T> |
| 42 | using ArrayAllocator = bool (*)(void* arrayData, size_t length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 43 | template <typename T> |
| 44 | using ArrayGetter = T (*)(const void* arrayData, size_t index); |
| 45 | template <typename T> |
| 46 | using ArraySetter = void (*)(void* arrayData, size_t index, T value); |
| 47 | |
| 48 | template <typename T> |
| 49 | binder_status_t WriteArray(AParcel* parcel, const T* array, size_t length) { |
| 50 | if (length > std::numeric_limits<int32_t>::max()) return STATUS_BAD_VALUE; |
| 51 | |
| 52 | Parcel* rawParcel = parcel->get(); |
| 53 | |
| 54 | status_t status = rawParcel->writeInt32(static_cast<int32_t>(length)); |
| 55 | if (status != STATUS_OK) return PruneStatusT(status); |
| 56 | |
| 57 | int32_t size = 0; |
| 58 | if (__builtin_smul_overflow(sizeof(T), length, &size)) return STATUS_NO_MEMORY; |
| 59 | |
| 60 | void* const data = rawParcel->writeInplace(size); |
| 61 | if (data == nullptr) return STATUS_NO_MEMORY; |
| 62 | |
| 63 | memcpy(data, array, size); |
| 64 | |
| 65 | return STATUS_OK; |
| 66 | } |
| 67 | |
| 68 | // Each element in a char16_t array is converted to an int32_t (not packed). |
| 69 | template <> |
| 70 | binder_status_t WriteArray<char16_t>(AParcel* parcel, const char16_t* array, size_t length) { |
| 71 | if (length > std::numeric_limits<int32_t>::max()) return STATUS_BAD_VALUE; |
| 72 | |
| 73 | Parcel* rawParcel = parcel->get(); |
| 74 | |
| 75 | status_t status = rawParcel->writeInt32(static_cast<int32_t>(length)); |
| 76 | if (status != STATUS_OK) return PruneStatusT(status); |
| 77 | |
| 78 | int32_t size = 0; |
| 79 | if (__builtin_smul_overflow(sizeof(char16_t), length, &size)) return STATUS_NO_MEMORY; |
| 80 | |
| 81 | for (int32_t i = 0; i < length; i++) { |
| 82 | status = rawParcel->writeChar(array[i]); |
| 83 | |
| 84 | if (status != STATUS_OK) return PruneStatusT(status); |
| 85 | } |
| 86 | |
| 87 | return STATUS_OK; |
| 88 | } |
| 89 | |
| 90 | template <typename T> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 91 | binder_status_t ReadArray(const AParcel* parcel, void* arrayData, |
| 92 | ContiguousArrayAllocator<T> allocator) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 93 | const Parcel* rawParcel = parcel->get(); |
| 94 | |
| 95 | int32_t length; |
| 96 | status_t status = rawParcel->readInt32(&length); |
| 97 | |
| 98 | if (status != STATUS_OK) return PruneStatusT(status); |
| 99 | if (length < 0) return STATUS_UNEXPECTED_NULL; |
| 100 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 101 | T* array = allocator(arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 102 | if (length == 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 103 | if (array == nullptr) return STATUS_NO_MEMORY; |
| 104 | |
| 105 | int32_t size = 0; |
| 106 | if (__builtin_smul_overflow(sizeof(T), length, &size)) return STATUS_NO_MEMORY; |
| 107 | |
| 108 | const void* data = rawParcel->readInplace(size); |
| 109 | if (data == nullptr) return STATUS_NO_MEMORY; |
| 110 | |
| 111 | memcpy(array, data, size); |
| 112 | |
| 113 | return STATUS_OK; |
| 114 | } |
| 115 | |
| 116 | // Each element in a char16_t array is converted to an int32_t (not packed) |
| 117 | template <> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 118 | binder_status_t ReadArray<char16_t>(const AParcel* parcel, void* arrayData, |
| 119 | ContiguousArrayAllocator<char16_t> allocator) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 120 | const Parcel* rawParcel = parcel->get(); |
| 121 | |
| 122 | int32_t length; |
| 123 | status_t status = rawParcel->readInt32(&length); |
| 124 | |
| 125 | if (status != STATUS_OK) return PruneStatusT(status); |
| 126 | if (length < 0) return STATUS_UNEXPECTED_NULL; |
| 127 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 128 | char16_t* array = allocator(arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 129 | if (length == 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 130 | if (array == nullptr) return STATUS_NO_MEMORY; |
| 131 | |
| 132 | int32_t size = 0; |
| 133 | if (__builtin_smul_overflow(sizeof(char16_t), length, &size)) return STATUS_NO_MEMORY; |
| 134 | |
| 135 | for (int32_t i = 0; i < length; i++) { |
| 136 | status = rawParcel->readChar(array + i); |
| 137 | |
| 138 | if (status != STATUS_OK) return PruneStatusT(status); |
| 139 | } |
| 140 | |
| 141 | return STATUS_OK; |
| 142 | } |
| 143 | |
| 144 | template <typename T> |
Steven Moreland | 16e1eae | 2018-11-01 09:51:53 -0700 | [diff] [blame] | 145 | binder_status_t WriteArray(AParcel* parcel, const void* arrayData, size_t length, |
| 146 | ArrayGetter<T> getter, status_t (Parcel::*write)(T)) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 147 | if (length > std::numeric_limits<int32_t>::max()) return STATUS_BAD_VALUE; |
| 148 | |
| 149 | Parcel* rawParcel = parcel->get(); |
| 150 | |
| 151 | status_t status = rawParcel->writeInt32(static_cast<int32_t>(length)); |
| 152 | if (status != STATUS_OK) return PruneStatusT(status); |
| 153 | |
| 154 | for (size_t i = 0; i < length; i++) { |
| 155 | status = (rawParcel->*write)(getter(arrayData, i)); |
| 156 | |
| 157 | if (status != STATUS_OK) return PruneStatusT(status); |
| 158 | } |
| 159 | |
| 160 | return STATUS_OK; |
| 161 | } |
| 162 | |
| 163 | template <typename T> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 164 | binder_status_t ReadArray(const AParcel* parcel, void* arrayData, ArrayAllocator<T> allocator, |
| 165 | ArraySetter<T> setter, status_t (Parcel::*read)(T*) const) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 166 | const Parcel* rawParcel = parcel->get(); |
| 167 | |
| 168 | int32_t length; |
| 169 | status_t status = rawParcel->readInt32(&length); |
| 170 | |
| 171 | if (status != STATUS_OK) return PruneStatusT(status); |
| 172 | if (length < 0) return STATUS_UNEXPECTED_NULL; |
| 173 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 174 | if (!allocator(arrayData, length)) return STATUS_NO_MEMORY; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 175 | |
| 176 | for (size_t i = 0; i < length; i++) { |
| 177 | T readTarget; |
| 178 | status = (rawParcel->*read)(&readTarget); |
| 179 | if (status != STATUS_OK) return PruneStatusT(status); |
| 180 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 181 | setter(arrayData, i, readTarget); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return STATUS_OK; |
| 185 | } |
| 186 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 187 | void AParcel_delete(AParcel* parcel) { |
| 188 | delete parcel; |
Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 191 | binder_status_t AParcel_writeStrongBinder(AParcel* parcel, AIBinder* binder) { |
Steven Moreland | c0e46d3 | 2018-09-12 15:40:49 -0700 | [diff] [blame] | 192 | sp<IBinder> writeBinder = binder != nullptr ? binder->getBinder() : nullptr; |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 193 | return parcel->get()->writeStrongBinder(writeBinder); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 194 | } |
| 195 | binder_status_t AParcel_readStrongBinder(const AParcel* parcel, AIBinder** binder) { |
| 196 | sp<IBinder> readBinder = nullptr; |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 197 | status_t status = parcel->get()->readNullableStrongBinder(&readBinder); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 198 | if (status != STATUS_OK) { |
| 199 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 200 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 201 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(readBinder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 202 | AIBinder_incStrong(ret.get()); |
| 203 | *binder = ret.get(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 204 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 205 | } |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 206 | |
| 207 | binder_status_t AParcel_writeParcelFileDescriptor(AParcel* parcel, int fd) { |
| 208 | ParcelFileDescriptor parcelFd((unique_fd(fd))); |
| 209 | |
| 210 | status_t status = parcel->get()->writeParcelable(parcelFd); |
| 211 | |
| 212 | // ownership is retained by caller |
| 213 | (void)parcelFd.release().release(); |
| 214 | |
| 215 | return PruneStatusT(status); |
| 216 | } |
| 217 | |
| 218 | binder_status_t AParcel_readParcelFileDescriptor(const AParcel* parcel, int* fd) { |
| 219 | ParcelFileDescriptor parcelFd; |
| 220 | // status_t status = parcelFd.readFromParcel(parcel->get()); |
| 221 | status_t status = parcel->get()->readParcelable(&parcelFd); |
| 222 | if (status != STATUS_OK) return PruneStatusT(status); |
| 223 | |
| 224 | *fd = parcelFd.release().release(); |
| 225 | return STATUS_OK; |
| 226 | } |
| 227 | |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 228 | binder_status_t AParcel_writeStatusHeader(AParcel* parcel, const AStatus* status) { |
| 229 | return PruneStatusT(status->get()->writeToParcel(parcel->get())); |
| 230 | } |
| 231 | binder_status_t AParcel_readStatusHeader(const AParcel* parcel, AStatus** status) { |
| 232 | ::android::binder::Status bstatus; |
| 233 | binder_status_t ret = PruneStatusT(bstatus.readFromParcel(*parcel->get())); |
Steven Moreland | c1a11b8 | 2018-10-29 18:47:23 -0700 | [diff] [blame] | 234 | if (ret == STATUS_OK) { |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 235 | *status = new AStatus(std::move(bstatus)); |
| 236 | } |
Steven Moreland | c1a11b8 | 2018-10-29 18:47:23 -0700 | [diff] [blame] | 237 | return PruneStatusT(ret); |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 238 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 239 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 240 | binder_status_t AParcel_writeString(AParcel* parcel, const char* string, int32_t length) { |
| 241 | if (string == nullptr) { |
| 242 | if (length != -1) { |
| 243 | LOG(WARNING) << __func__ << ": null string must be used with length == -1."; |
| 244 | return STATUS_BAD_VALUE; |
| 245 | } |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 246 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 247 | status_t err = parcel->get()->writeInt32(-1); |
| 248 | return PruneStatusT(err); |
| 249 | } |
| 250 | |
| 251 | if (length < 0) { |
| 252 | LOG(WARNING) << __func__ << ": Negative string length: " << length; |
| 253 | return STATUS_BAD_VALUE; |
| 254 | } |
| 255 | |
| 256 | const uint8_t* str8 = (uint8_t*)string; |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 257 | const ssize_t len16 = utf8_to_utf16_length(str8, length); |
| 258 | |
| 259 | if (len16 < 0 || len16 >= std::numeric_limits<int32_t>::max()) { |
| 260 | LOG(WARNING) << __func__ << ": Invalid string length: " << len16; |
| 261 | return STATUS_BAD_VALUE; |
| 262 | } |
| 263 | |
| 264 | status_t err = parcel->get()->writeInt32(len16); |
| 265 | if (err) { |
| 266 | return PruneStatusT(err); |
| 267 | } |
| 268 | |
| 269 | void* str16 = parcel->get()->writeInplace((len16 + 1) * sizeof(char16_t)); |
| 270 | if (str16 == nullptr) { |
| 271 | return STATUS_NO_MEMORY; |
| 272 | } |
| 273 | |
| 274 | utf8_to_utf16(str8, length, (char16_t*)str16, (size_t)len16 + 1); |
| 275 | |
| 276 | return STATUS_OK; |
| 277 | } |
| 278 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 279 | binder_status_t AParcel_readString(const AParcel* parcel, void* stringData, |
| 280 | AParcel_stringAllocator allocator) { |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 281 | size_t len16; |
| 282 | const char16_t* str16 = parcel->get()->readString16Inplace(&len16); |
| 283 | |
| 284 | if (str16 == nullptr) { |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 285 | if (allocator(stringData, -1, nullptr)) { |
| 286 | return STATUS_OK; |
| 287 | } |
| 288 | |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 289 | return STATUS_UNEXPECTED_NULL; |
| 290 | } |
| 291 | |
| 292 | ssize_t len8; |
| 293 | |
| 294 | if (len16 == 0) { |
| 295 | len8 = 1; |
| 296 | } else { |
| 297 | len8 = utf16_to_utf8_length(str16, len16) + 1; |
| 298 | } |
| 299 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 300 | if (len8 <= 0 || len8 > std::numeric_limits<int32_t>::max()) { |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 301 | LOG(WARNING) << __func__ << ": Invalid string length: " << len8; |
| 302 | return STATUS_BAD_VALUE; |
| 303 | } |
| 304 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 305 | char* str8; |
| 306 | bool success = allocator(stringData, len8, &str8); |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 307 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 308 | if (!success || str8 == nullptr) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 309 | LOG(WARNING) << __func__ << ": AParcel_stringAllocator failed to allocate."; |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 310 | return STATUS_NO_MEMORY; |
| 311 | } |
| 312 | |
| 313 | utf16_to_utf8(str16, len16, str8, len8); |
| 314 | |
| 315 | return STATUS_OK; |
| 316 | } |
| 317 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 318 | binder_status_t AParcel_writeStringArray(AParcel* parcel, const void* arrayData, int32_t length, |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 319 | AParcel_stringArrayElementGetter getter) { |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 320 | Parcel* rawParcel = parcel->get(); |
| 321 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 322 | if (length < 0) { |
| 323 | if (length != -1) { |
| 324 | LOG(WARNING) << __func__ << ": null array must be used with length == -1."; |
| 325 | return STATUS_BAD_VALUE; |
| 326 | } |
| 327 | |
| 328 | status_t status = rawParcel->writeInt32(-1); |
| 329 | return PruneStatusT(status); |
| 330 | } |
| 331 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 332 | status_t status = rawParcel->writeInt32(static_cast<int32_t>(length)); |
| 333 | if (status != STATUS_OK) return PruneStatusT(status); |
| 334 | |
| 335 | for (size_t i = 0; i < length; i++) { |
| 336 | size_t length = 0; |
| 337 | const char* str = getter(arrayData, i, &length); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 338 | if (str == nullptr && length != -1) return STATUS_BAD_VALUE; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 339 | |
| 340 | binder_status_t status = AParcel_writeString(parcel, str, length); |
| 341 | if (status != STATUS_OK) return status; |
| 342 | } |
| 343 | |
| 344 | return STATUS_OK; |
| 345 | } |
| 346 | |
| 347 | // This implements AParcel_stringAllocator for a string using an array, index, and element |
| 348 | // allocator. |
| 349 | struct StringArrayElementAllocationAdapter { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 350 | void* arrayData; // stringData from the NDK |
| 351 | size_t index; // index into the string array |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 352 | AParcel_stringArrayElementAllocator elementAllocator; |
| 353 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 354 | static bool Allocator(void* stringData, int32_t length, char** buffer) { |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 355 | StringArrayElementAllocationAdapter* adapter = |
| 356 | static_cast<StringArrayElementAllocationAdapter*>(stringData); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 357 | return adapter->elementAllocator(adapter->arrayData, adapter->index, length, buffer); |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 358 | } |
| 359 | }; |
| 360 | |
| 361 | binder_status_t AParcel_readStringArray(const AParcel* parcel, void* arrayData, |
| 362 | AParcel_stringArrayAllocator allocator, |
| 363 | AParcel_stringArrayElementAllocator elementAllocator) { |
| 364 | const Parcel* rawParcel = parcel->get(); |
| 365 | |
| 366 | int32_t length; |
| 367 | status_t status = rawParcel->readInt32(&length); |
| 368 | |
| 369 | if (status != STATUS_OK) return PruneStatusT(status); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 370 | if (length < -1) return STATUS_BAD_VALUE; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 371 | |
| 372 | if (!allocator(arrayData, length)) return STATUS_NO_MEMORY; |
| 373 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 374 | if (length == -1) return STATUS_OK; // null string array |
| 375 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 376 | StringArrayElementAllocationAdapter adapter{ |
| 377 | .arrayData = arrayData, |
| 378 | .index = 0, |
| 379 | .elementAllocator = elementAllocator, |
| 380 | }; |
| 381 | |
| 382 | for (; adapter.index < length; adapter.index++) { |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame^] | 383 | binder_status_t status = AParcel_readString(parcel, static_cast<void*>(&adapter), |
| 384 | StringArrayElementAllocationAdapter::Allocator); |
| 385 | |
| 386 | if (status != STATUS_OK) return status; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return STATUS_OK; |
| 390 | } |
| 391 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 392 | // See gen_parcel_helper.py. These auto-generated read/write methods use the same types for |
| 393 | // libbinder and this library. |
| 394 | // @START |
| 395 | binder_status_t AParcel_writeInt32(AParcel* parcel, int32_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 396 | status_t status = parcel->get()->writeInt32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 397 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | binder_status_t AParcel_writeUint32(AParcel* parcel, uint32_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 401 | status_t status = parcel->get()->writeUint32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 402 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | binder_status_t AParcel_writeInt64(AParcel* parcel, int64_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 406 | status_t status = parcel->get()->writeInt64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 407 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | binder_status_t AParcel_writeUint64(AParcel* parcel, uint64_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 411 | status_t status = parcel->get()->writeUint64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 412 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | binder_status_t AParcel_writeFloat(AParcel* parcel, float value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 416 | status_t status = parcel->get()->writeFloat(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 417 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | binder_status_t AParcel_writeDouble(AParcel* parcel, double value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 421 | status_t status = parcel->get()->writeDouble(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 422 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | binder_status_t AParcel_writeBool(AParcel* parcel, bool value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 426 | status_t status = parcel->get()->writeBool(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 427 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | binder_status_t AParcel_writeChar(AParcel* parcel, char16_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 431 | status_t status = parcel->get()->writeChar(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 432 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | binder_status_t AParcel_writeByte(AParcel* parcel, int8_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 436 | status_t status = parcel->get()->writeByte(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 437 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | binder_status_t AParcel_readInt32(const AParcel* parcel, int32_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 441 | status_t status = parcel->get()->readInt32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 442 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | binder_status_t AParcel_readUint32(const AParcel* parcel, uint32_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 446 | status_t status = parcel->get()->readUint32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 447 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | binder_status_t AParcel_readInt64(const AParcel* parcel, int64_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 451 | status_t status = parcel->get()->readInt64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 452 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | binder_status_t AParcel_readUint64(const AParcel* parcel, uint64_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 456 | status_t status = parcel->get()->readUint64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 457 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | binder_status_t AParcel_readFloat(const AParcel* parcel, float* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 461 | status_t status = parcel->get()->readFloat(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 462 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | binder_status_t AParcel_readDouble(const AParcel* parcel, double* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 466 | status_t status = parcel->get()->readDouble(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 467 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | binder_status_t AParcel_readBool(const AParcel* parcel, bool* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 471 | status_t status = parcel->get()->readBool(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 472 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | binder_status_t AParcel_readChar(const AParcel* parcel, char16_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 476 | status_t status = parcel->get()->readChar(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 477 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | binder_status_t AParcel_readByte(const AParcel* parcel, int8_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 481 | status_t status = parcel->get()->readByte(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 482 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 485 | binder_status_t AParcel_writeInt32Array(AParcel* parcel, const int32_t* arrayData, size_t length) { |
| 486 | return WriteArray<int32_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 489 | binder_status_t AParcel_writeUint32Array(AParcel* parcel, const uint32_t* arrayData, |
| 490 | size_t length) { |
| 491 | return WriteArray<uint32_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 494 | binder_status_t AParcel_writeInt64Array(AParcel* parcel, const int64_t* arrayData, size_t length) { |
| 495 | return WriteArray<int64_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 498 | binder_status_t AParcel_writeUint64Array(AParcel* parcel, const uint64_t* arrayData, |
| 499 | size_t length) { |
| 500 | return WriteArray<uint64_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 503 | binder_status_t AParcel_writeFloatArray(AParcel* parcel, const float* arrayData, size_t length) { |
| 504 | return WriteArray<float>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 507 | binder_status_t AParcel_writeDoubleArray(AParcel* parcel, const double* arrayData, size_t length) { |
| 508 | return WriteArray<double>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Steven Moreland | 16e1eae | 2018-11-01 09:51:53 -0700 | [diff] [blame] | 511 | binder_status_t AParcel_writeBoolArray(AParcel* parcel, const void* arrayData, size_t length, |
| 512 | AParcel_boolArrayGetter getter) { |
| 513 | return WriteArray<bool>(parcel, arrayData, length, getter, &Parcel::writeBool); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 516 | binder_status_t AParcel_writeCharArray(AParcel* parcel, const char16_t* arrayData, size_t length) { |
| 517 | return WriteArray<char16_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 520 | binder_status_t AParcel_writeByteArray(AParcel* parcel, const int8_t* arrayData, size_t length) { |
| 521 | return WriteArray<int8_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 524 | binder_status_t AParcel_readInt32Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 525 | AParcel_int32ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 526 | return ReadArray<int32_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 529 | binder_status_t AParcel_readUint32Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 530 | AParcel_uint32ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 531 | return ReadArray<uint32_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 534 | binder_status_t AParcel_readInt64Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 535 | AParcel_int64ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 536 | return ReadArray<int64_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 539 | binder_status_t AParcel_readUint64Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 540 | AParcel_uint64ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 541 | return ReadArray<uint64_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 544 | binder_status_t AParcel_readFloatArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 545 | AParcel_floatArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 546 | return ReadArray<float>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 549 | binder_status_t AParcel_readDoubleArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 550 | AParcel_doubleArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 551 | return ReadArray<double>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 554 | binder_status_t AParcel_readBoolArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 555 | AParcel_boolArrayAllocator allocator, |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 556 | AParcel_boolArraySetter setter) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 557 | return ReadArray<bool>(parcel, arrayData, allocator, setter, &Parcel::readBool); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 560 | binder_status_t AParcel_readCharArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 561 | AParcel_charArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 562 | return ReadArray<char16_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 565 | binder_status_t AParcel_readByteArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 566 | AParcel_byteArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 567 | return ReadArray<int8_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 570 | // @END |