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> |
John Reck | 79fb24b | 2020-02-14 13:56:19 -0800 | [diff] [blame] | 18 | #include <android/binder_parcel_platform.h> |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 19 | #include <binder/Parcel.h> |
| 20 | #include <binder/ParcelFileDescriptor.h> |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 21 | #include <binder/unique_fd.h> |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | #include <utils/Unicode.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 24 | |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 25 | #include <limits> |
| 26 | |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 27 | #include "ibinder_internal.h" |
| 28 | #include "parcel_internal.h" |
| 29 | #include "status_internal.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; |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 35 | using ::android::binder::unique_fd; |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 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 | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 39 | using ContiguousArrayAllocator = bool (*)(void* arrayData, int32_t length, T** outBuffer); |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 40 | |
| 41 | template <typename T> |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 42 | using ArrayAllocator = bool (*)(void* arrayData, int32_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 | |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 48 | static binder_status_t WriteAndValidateArraySize(AParcel* parcel, bool isNullArray, |
| 49 | int32_t length) { |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 50 | // only -1 can be used to represent a null array |
| 51 | if (length < -1) return STATUS_BAD_VALUE; |
| 52 | |
| 53 | if (!isNullArray && length < 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 54 | ALOGE("non-null array but length is %" PRIi32, length); |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 55 | return STATUS_BAD_VALUE; |
| 56 | } |
| 57 | if (isNullArray && length > 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 58 | ALOGE("null buffer cannot be for size %" PRIi32 " array.", length); |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 59 | return STATUS_BAD_VALUE; |
| 60 | } |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 61 | |
| 62 | Parcel* rawParcel = parcel->get(); |
| 63 | |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 64 | status_t status = rawParcel->writeInt32(length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 65 | if (status != STATUS_OK) return PruneStatusT(status); |
| 66 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 67 | return STATUS_OK; |
| 68 | } |
| 69 | |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 70 | static binder_status_t ReadAndValidateArraySize(const AParcel* parcel, int32_t* length) { |
| 71 | if (status_t status = parcel->get()->readInt32(length); status != STATUS_OK) { |
| 72 | return PruneStatusT(status); |
| 73 | } |
| 74 | |
| 75 | if (*length < -1) return STATUS_BAD_VALUE; // libbinder_ndk reserves these |
| 76 | if (*length <= 0) return STATUS_OK; // null |
| 77 | if (static_cast<size_t>(*length) > parcel->get()->dataAvail()) return STATUS_NO_MEMORY; |
| 78 | |
| 79 | return STATUS_OK; |
| 80 | } |
| 81 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 82 | template <typename T> |
| 83 | binder_status_t WriteArray(AParcel* parcel, const T* array, int32_t length) { |
| 84 | binder_status_t status = WriteAndValidateArraySize(parcel, array == nullptr, length); |
| 85 | if (status != STATUS_OK) return status; |
| 86 | if (length <= 0) return STATUS_OK; |
| 87 | |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 88 | int32_t size = 0; |
| 89 | if (__builtin_smul_overflow(sizeof(T), length, &size)) return STATUS_NO_MEMORY; |
| 90 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 91 | void* const data = parcel->get()->writeInplace(size); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 92 | if (data == nullptr) return STATUS_NO_MEMORY; |
| 93 | |
| 94 | memcpy(data, array, size); |
| 95 | |
| 96 | return STATUS_OK; |
| 97 | } |
| 98 | |
| 99 | // Each element in a char16_t array is converted to an int32_t (not packed). |
| 100 | template <> |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 101 | binder_status_t WriteArray<char16_t>(AParcel* parcel, const char16_t* array, int32_t length) { |
| 102 | binder_status_t status = WriteAndValidateArraySize(parcel, array == nullptr, length); |
| 103 | if (status != STATUS_OK) return status; |
| 104 | if (length <= 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 105 | |
| 106 | int32_t size = 0; |
| 107 | if (__builtin_smul_overflow(sizeof(char16_t), length, &size)) return STATUS_NO_MEMORY; |
| 108 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 109 | Parcel* rawParcel = parcel->get(); |
| 110 | |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 111 | for (int32_t i = 0; i < length; i++) { |
| 112 | status = rawParcel->writeChar(array[i]); |
| 113 | |
| 114 | if (status != STATUS_OK) return PruneStatusT(status); |
| 115 | } |
| 116 | |
| 117 | return STATUS_OK; |
| 118 | } |
| 119 | |
| 120 | template <typename T> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 121 | binder_status_t ReadArray(const AParcel* parcel, void* arrayData, |
| 122 | ContiguousArrayAllocator<T> allocator) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 123 | const Parcel* rawParcel = parcel->get(); |
| 124 | |
| 125 | int32_t length; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 126 | if (binder_status_t status = ReadAndValidateArraySize(parcel, &length); status != STATUS_OK) { |
| 127 | return status; |
| 128 | } |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 129 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 130 | T* array; |
Devin Moore | 7a45024 | 2022-11-15 22:21:07 +0000 | [diff] [blame] | 131 | if (!allocator(arrayData, length, &array)) { |
| 132 | if (length < 0) { |
| 133 | return STATUS_UNEXPECTED_NULL; |
| 134 | } else { |
| 135 | return STATUS_NO_MEMORY; |
| 136 | } |
| 137 | } |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 138 | |
| 139 | if (length <= 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 140 | if (array == nullptr) return STATUS_NO_MEMORY; |
| 141 | |
| 142 | int32_t size = 0; |
| 143 | if (__builtin_smul_overflow(sizeof(T), length, &size)) return STATUS_NO_MEMORY; |
| 144 | |
| 145 | const void* data = rawParcel->readInplace(size); |
| 146 | if (data == nullptr) return STATUS_NO_MEMORY; |
| 147 | |
| 148 | memcpy(array, data, size); |
| 149 | |
| 150 | return STATUS_OK; |
| 151 | } |
| 152 | |
| 153 | // Each element in a char16_t array is converted to an int32_t (not packed) |
| 154 | template <> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 155 | binder_status_t ReadArray<char16_t>(const AParcel* parcel, void* arrayData, |
| 156 | ContiguousArrayAllocator<char16_t> allocator) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 157 | const Parcel* rawParcel = parcel->get(); |
| 158 | |
| 159 | int32_t length; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 160 | if (binder_status_t status = ReadAndValidateArraySize(parcel, &length); status != STATUS_OK) { |
| 161 | return status; |
| 162 | } |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 163 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 164 | char16_t* array; |
Devin Moore | 7a45024 | 2022-11-15 22:21:07 +0000 | [diff] [blame] | 165 | if (!allocator(arrayData, length, &array)) { |
| 166 | if (length < 0) { |
| 167 | return STATUS_UNEXPECTED_NULL; |
| 168 | } else { |
| 169 | return STATUS_NO_MEMORY; |
| 170 | } |
| 171 | } |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 172 | |
| 173 | if (length <= 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 174 | if (array == nullptr) return STATUS_NO_MEMORY; |
| 175 | |
| 176 | int32_t size = 0; |
| 177 | if (__builtin_smul_overflow(sizeof(char16_t), length, &size)) return STATUS_NO_MEMORY; |
| 178 | |
| 179 | for (int32_t i = 0; i < length; i++) { |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 180 | status_t status = rawParcel->readChar(array + i); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 181 | |
| 182 | if (status != STATUS_OK) return PruneStatusT(status); |
| 183 | } |
| 184 | |
| 185 | return STATUS_OK; |
| 186 | } |
| 187 | |
| 188 | template <typename T> |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 189 | binder_status_t WriteArray(AParcel* parcel, const void* arrayData, int32_t length, |
Steven Moreland | 16e1eae | 2018-11-01 09:51:53 -0700 | [diff] [blame] | 190 | ArrayGetter<T> getter, status_t (Parcel::*write)(T)) { |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 191 | // we have no clue if arrayData represents a null object or not, we can only infer from length |
| 192 | bool arrayIsNull = length < 0; |
| 193 | binder_status_t status = WriteAndValidateArraySize(parcel, arrayIsNull, length); |
| 194 | if (status != STATUS_OK) return status; |
| 195 | if (length <= 0) return STATUS_OK; |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 196 | |
| 197 | Parcel* rawParcel = parcel->get(); |
| 198 | |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 199 | for (int32_t i = 0; i < length; i++) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 200 | status = (rawParcel->*write)(getter(arrayData, i)); |
| 201 | |
| 202 | if (status != STATUS_OK) return PruneStatusT(status); |
| 203 | } |
| 204 | |
| 205 | return STATUS_OK; |
| 206 | } |
| 207 | |
| 208 | template <typename T> |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 209 | binder_status_t ReadArray(const AParcel* parcel, void* arrayData, ArrayAllocator<T> allocator, |
| 210 | ArraySetter<T> setter, status_t (Parcel::*read)(T*) const) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 211 | const Parcel* rawParcel = parcel->get(); |
| 212 | |
| 213 | int32_t length; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 214 | if (binder_status_t status = ReadAndValidateArraySize(parcel, &length); status != STATUS_OK) { |
| 215 | return status; |
| 216 | } |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 217 | |
Devin Moore | 7a45024 | 2022-11-15 22:21:07 +0000 | [diff] [blame] | 218 | if (!allocator(arrayData, length)) { |
| 219 | if (length < 0) { |
| 220 | return STATUS_UNEXPECTED_NULL; |
| 221 | } else { |
| 222 | return STATUS_NO_MEMORY; |
| 223 | } |
| 224 | } |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 225 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 226 | if (length <= 0) return STATUS_OK; |
| 227 | |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 228 | for (int32_t i = 0; i < length; i++) { |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 229 | T readTarget; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 230 | status_t status = (rawParcel->*read)(&readTarget); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 231 | if (status != STATUS_OK) return PruneStatusT(status); |
| 232 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 233 | setter(arrayData, i, readTarget); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return STATUS_OK; |
| 237 | } |
| 238 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 239 | void AParcel_delete(AParcel* parcel) { |
| 240 | delete parcel; |
Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Steven Moreland | f32d1b0 | 2018-11-27 12:44:10 -0800 | [diff] [blame] | 243 | binder_status_t AParcel_setDataPosition(const AParcel* parcel, int32_t position) { |
| 244 | if (position < 0) { |
| 245 | return STATUS_BAD_VALUE; |
| 246 | } |
| 247 | |
| 248 | parcel->get()->setDataPosition(position); |
| 249 | return STATUS_OK; |
| 250 | } |
| 251 | |
| 252 | int32_t AParcel_getDataPosition(const AParcel* parcel) { |
| 253 | return parcel->get()->dataPosition(); |
| 254 | } |
| 255 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 256 | void AParcel_markSensitive(const AParcel* parcel) { |
| 257 | return parcel->get()->markSensitive(); |
| 258 | } |
| 259 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 260 | binder_status_t AParcel_writeStrongBinder(AParcel* parcel, AIBinder* binder) { |
Steven Moreland | c0e46d3 | 2018-09-12 15:40:49 -0700 | [diff] [blame] | 261 | sp<IBinder> writeBinder = binder != nullptr ? binder->getBinder() : nullptr; |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 262 | return parcel->get()->writeStrongBinder(writeBinder); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 263 | } |
| 264 | binder_status_t AParcel_readStrongBinder(const AParcel* parcel, AIBinder** binder) { |
| 265 | sp<IBinder> readBinder = nullptr; |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 266 | status_t status = parcel->get()->readNullableStrongBinder(&readBinder); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 267 | if (status != STATUS_OK) { |
| 268 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 269 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 270 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(readBinder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 271 | AIBinder_incStrong(ret.get()); |
Steven Moreland | 418914a | 2023-06-28 21:47:14 +0000 | [diff] [blame] | 272 | |
| 273 | if (ret.get() != nullptr && parcel->get()->isServiceFuzzing()) { |
| 274 | if (auto bp = ret->asABpBinder(); bp != nullptr) { |
| 275 | bp->setServiceFuzzing(); |
| 276 | } |
| 277 | } |
| 278 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 279 | *binder = ret.get(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 280 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 281 | } |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 282 | |
| 283 | binder_status_t AParcel_writeParcelFileDescriptor(AParcel* parcel, int fd) { |
Steven Moreland | 4ce59c7 | 2018-11-16 15:21:15 -0800 | [diff] [blame] | 284 | if (fd < 0) { |
| 285 | if (fd != -1) { |
| 286 | return STATUS_UNKNOWN_ERROR; |
| 287 | } |
Steven Moreland | 073c9c1 | 2020-02-19 17:02:08 -0800 | [diff] [blame] | 288 | return PruneStatusT(parcel->get()->writeInt32(0)); // null |
Steven Moreland | 4ce59c7 | 2018-11-16 15:21:15 -0800 | [diff] [blame] | 289 | } |
Steven Moreland | 073c9c1 | 2020-02-19 17:02:08 -0800 | [diff] [blame] | 290 | status_t status = parcel->get()->writeInt32(1); // not-null |
| 291 | if (status != STATUS_OK) return PruneStatusT(status); |
Steven Moreland | 4ce59c7 | 2018-11-16 15:21:15 -0800 | [diff] [blame] | 292 | |
Steven Moreland | 073c9c1 | 2020-02-19 17:02:08 -0800 | [diff] [blame] | 293 | status = parcel->get()->writeDupParcelFileDescriptor(fd); |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 294 | return PruneStatusT(status); |
| 295 | } |
| 296 | |
| 297 | binder_status_t AParcel_readParcelFileDescriptor(const AParcel* parcel, int* fd) { |
Jooyung Han | 4d9f91a | 2020-11-18 13:24:16 +0900 | [diff] [blame] | 298 | std::optional<ParcelFileDescriptor> parcelFd; |
Steven Moreland | 4ce59c7 | 2018-11-16 15:21:15 -0800 | [diff] [blame] | 299 | |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 300 | status_t status = parcel->get()->readParcelable(&parcelFd); |
| 301 | if (status != STATUS_OK) return PruneStatusT(status); |
| 302 | |
Steven Moreland | 4ce59c7 | 2018-11-16 15:21:15 -0800 | [diff] [blame] | 303 | if (parcelFd) { |
| 304 | *fd = parcelFd->release().release(); |
| 305 | } else { |
| 306 | *fd = -1; |
| 307 | } |
| 308 | |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 309 | return STATUS_OK; |
| 310 | } |
| 311 | |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 312 | binder_status_t AParcel_writeStatusHeader(AParcel* parcel, const AStatus* status) { |
Steven Moreland | 85c138a | 2020-09-22 22:00:52 +0000 | [diff] [blame] | 313 | return PruneStatusT(status->get().writeToParcel(parcel->get())); |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 314 | } |
| 315 | binder_status_t AParcel_readStatusHeader(const AParcel* parcel, AStatus** status) { |
| 316 | ::android::binder::Status bstatus; |
| 317 | binder_status_t ret = PruneStatusT(bstatus.readFromParcel(*parcel->get())); |
Steven Moreland | c1a11b8 | 2018-10-29 18:47:23 -0700 | [diff] [blame] | 318 | if (ret == STATUS_OK) { |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 319 | *status = new AStatus(std::move(bstatus)); |
| 320 | } |
Steven Moreland | c1a11b8 | 2018-10-29 18:47:23 -0700 | [diff] [blame] | 321 | return PruneStatusT(ret); |
Steven Moreland | 9a51db8 | 2018-09-14 10:59:35 -0700 | [diff] [blame] | 322 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 323 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 324 | binder_status_t AParcel_writeString(AParcel* parcel, const char* string, int32_t length) { |
| 325 | if (string == nullptr) { |
| 326 | if (length != -1) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 327 | ALOGW("null string must be used with length == -1."); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 328 | return STATUS_BAD_VALUE; |
| 329 | } |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 330 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 331 | status_t err = parcel->get()->writeInt32(-1); |
| 332 | return PruneStatusT(err); |
| 333 | } |
| 334 | |
| 335 | if (length < 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 336 | ALOGW("Negative string length: %" PRIi32, length); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 337 | return STATUS_BAD_VALUE; |
| 338 | } |
| 339 | |
| 340 | const uint8_t* str8 = (uint8_t*)string; |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 341 | const ssize_t len16 = utf8_to_utf16_length(str8, length); |
| 342 | |
| 343 | if (len16 < 0 || len16 >= std::numeric_limits<int32_t>::max()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 344 | ALOGW("Invalid string length: %zd", len16); |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 345 | return STATUS_BAD_VALUE; |
| 346 | } |
| 347 | |
| 348 | status_t err = parcel->get()->writeInt32(len16); |
| 349 | if (err) { |
| 350 | return PruneStatusT(err); |
| 351 | } |
| 352 | |
| 353 | void* str16 = parcel->get()->writeInplace((len16 + 1) * sizeof(char16_t)); |
| 354 | if (str16 == nullptr) { |
| 355 | return STATUS_NO_MEMORY; |
| 356 | } |
| 357 | |
| 358 | utf8_to_utf16(str8, length, (char16_t*)str16, (size_t)len16 + 1); |
| 359 | |
| 360 | return STATUS_OK; |
| 361 | } |
| 362 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 363 | binder_status_t AParcel_readString(const AParcel* parcel, void* stringData, |
| 364 | AParcel_stringAllocator allocator) { |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 365 | size_t len16; |
| 366 | const char16_t* str16 = parcel->get()->readString16Inplace(&len16); |
| 367 | |
| 368 | if (str16 == nullptr) { |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 369 | if (allocator(stringData, -1, nullptr)) { |
| 370 | return STATUS_OK; |
| 371 | } |
| 372 | |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 373 | return STATUS_UNEXPECTED_NULL; |
| 374 | } |
| 375 | |
| 376 | ssize_t len8; |
| 377 | |
| 378 | if (len16 == 0) { |
| 379 | len8 = 1; |
| 380 | } else { |
| 381 | len8 = utf16_to_utf8_length(str16, len16) + 1; |
| 382 | } |
| 383 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 384 | if (len8 <= 0 || len8 > std::numeric_limits<int32_t>::max()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 385 | ALOGW("Invalid string length: %zd", len8); |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 386 | return STATUS_BAD_VALUE; |
| 387 | } |
| 388 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 389 | char* str8; |
| 390 | bool success = allocator(stringData, len8, &str8); |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 391 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 392 | if (!success || str8 == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 393 | ALOGW("AParcel_stringAllocator failed to allocate."); |
Steven Moreland | 7b06f59 | 2018-10-03 19:25:32 -0700 | [diff] [blame] | 394 | return STATUS_NO_MEMORY; |
| 395 | } |
| 396 | |
| 397 | utf16_to_utf8(str16, len16, str8, len8); |
| 398 | |
| 399 | return STATUS_OK; |
| 400 | } |
| 401 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 402 | 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] | 403 | AParcel_stringArrayElementGetter getter) { |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 404 | // we have no clue if arrayData represents a null object or not, we can only infer from length |
| 405 | bool arrayIsNull = length < 0; |
| 406 | binder_status_t status = WriteAndValidateArraySize(parcel, arrayIsNull, length); |
| 407 | if (status != STATUS_OK) return status; |
| 408 | if (length <= 0) return STATUS_OK; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 409 | |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 410 | for (int32_t i = 0; i < length; i++) { |
| 411 | int32_t elementLength = 0; |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 412 | const char* str = getter(arrayData, i, &elementLength); |
| 413 | if (str == nullptr && elementLength != -1) return STATUS_BAD_VALUE; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 414 | |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 415 | binder_status_t status = AParcel_writeString(parcel, str, elementLength); |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 416 | if (status != STATUS_OK) return status; |
| 417 | } |
| 418 | |
| 419 | return STATUS_OK; |
| 420 | } |
| 421 | |
| 422 | // This implements AParcel_stringAllocator for a string using an array, index, and element |
| 423 | // allocator. |
| 424 | struct StringArrayElementAllocationAdapter { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 425 | void* arrayData; // stringData from the NDK |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 426 | int32_t index; // index into the string array |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 427 | AParcel_stringArrayElementAllocator elementAllocator; |
| 428 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 429 | static bool Allocator(void* stringData, int32_t length, char** buffer) { |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 430 | StringArrayElementAllocationAdapter* adapter = |
| 431 | static_cast<StringArrayElementAllocationAdapter*>(stringData); |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 432 | return adapter->elementAllocator(adapter->arrayData, adapter->index, length, buffer); |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 433 | } |
| 434 | }; |
| 435 | |
| 436 | binder_status_t AParcel_readStringArray(const AParcel* parcel, void* arrayData, |
| 437 | AParcel_stringArrayAllocator allocator, |
| 438 | AParcel_stringArrayElementAllocator elementAllocator) { |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 439 | int32_t length; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 440 | if (binder_status_t status = ReadAndValidateArraySize(parcel, &length); status != STATUS_OK) { |
| 441 | return status; |
| 442 | } |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 443 | |
| 444 | if (!allocator(arrayData, length)) return STATUS_NO_MEMORY; |
| 445 | |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 446 | if (length == -1) return STATUS_OK; // null string array |
| 447 | |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 448 | StringArrayElementAllocationAdapter adapter{ |
| 449 | .arrayData = arrayData, |
| 450 | .index = 0, |
| 451 | .elementAllocator = elementAllocator, |
| 452 | }; |
| 453 | |
| 454 | for (; adapter.index < length; adapter.index++) { |
Steven Moreland | b4e1461 | 2018-11-14 17:25:45 -0800 | [diff] [blame] | 455 | binder_status_t status = AParcel_readString(parcel, static_cast<void*>(&adapter), |
| 456 | StringArrayElementAllocationAdapter::Allocator); |
| 457 | |
| 458 | if (status != STATUS_OK) return status; |
Steven Moreland | 07fb9c9 | 2018-11-01 17:14:29 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return STATUS_OK; |
| 462 | } |
| 463 | |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 464 | binder_status_t AParcel_writeParcelableArray(AParcel* parcel, const void* arrayData, int32_t length, |
| 465 | AParcel_writeParcelableElement elementWriter) { |
| 466 | // we have no clue if arrayData represents a null object or not, we can only infer from length |
| 467 | bool arrayIsNull = length < 0; |
| 468 | binder_status_t status = WriteAndValidateArraySize(parcel, arrayIsNull, length); |
| 469 | if (status != STATUS_OK) return status; |
| 470 | if (length <= 0) return STATUS_OK; |
| 471 | |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 472 | for (int32_t i = 0; i < length; i++) { |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 473 | binder_status_t status = elementWriter(parcel, arrayData, i); |
| 474 | if (status != STATUS_OK) return status; |
| 475 | } |
| 476 | |
| 477 | return STATUS_OK; |
| 478 | } |
| 479 | |
| 480 | binder_status_t AParcel_readParcelableArray(const AParcel* parcel, void* arrayData, |
| 481 | AParcel_parcelableArrayAllocator allocator, |
| 482 | AParcel_readParcelableElement elementReader) { |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 483 | int32_t length; |
Steven Moreland | b3adc03 | 2021-05-14 20:41:11 +0000 | [diff] [blame] | 484 | if (binder_status_t status = ReadAndValidateArraySize(parcel, &length); status != STATUS_OK) { |
| 485 | return status; |
| 486 | } |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 487 | |
| 488 | if (!allocator(arrayData, length)) return STATUS_NO_MEMORY; |
| 489 | |
| 490 | if (length == -1) return STATUS_OK; // null array |
| 491 | |
Steven Moreland | 763dc4c | 2018-12-12 11:30:06 -0800 | [diff] [blame] | 492 | for (int32_t i = 0; i < length; i++) { |
Steven Moreland | e22a994 | 2018-12-11 18:57:05 -0800 | [diff] [blame] | 493 | binder_status_t status = elementReader(parcel, arrayData, i); |
| 494 | if (status != STATUS_OK) return status; |
| 495 | } |
| 496 | |
| 497 | return STATUS_OK; |
| 498 | } |
| 499 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 500 | // See gen_parcel_helper.py. These auto-generated read/write methods use the same types for |
| 501 | // libbinder and this library. |
| 502 | // @START |
| 503 | binder_status_t AParcel_writeInt32(AParcel* parcel, int32_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 504 | status_t status = parcel->get()->writeInt32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 505 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | binder_status_t AParcel_writeUint32(AParcel* parcel, uint32_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 509 | status_t status = parcel->get()->writeUint32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 510 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | binder_status_t AParcel_writeInt64(AParcel* parcel, int64_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 514 | status_t status = parcel->get()->writeInt64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 515 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | binder_status_t AParcel_writeUint64(AParcel* parcel, uint64_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 519 | status_t status = parcel->get()->writeUint64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 520 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | binder_status_t AParcel_writeFloat(AParcel* parcel, float value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 524 | status_t status = parcel->get()->writeFloat(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 525 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | binder_status_t AParcel_writeDouble(AParcel* parcel, double value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 529 | status_t status = parcel->get()->writeDouble(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 530 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | binder_status_t AParcel_writeBool(AParcel* parcel, bool value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 534 | status_t status = parcel->get()->writeBool(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 535 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | binder_status_t AParcel_writeChar(AParcel* parcel, char16_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 539 | status_t status = parcel->get()->writeChar(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 540 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | binder_status_t AParcel_writeByte(AParcel* parcel, int8_t value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 544 | status_t status = parcel->get()->writeByte(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 545 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | binder_status_t AParcel_readInt32(const AParcel* parcel, int32_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 549 | status_t status = parcel->get()->readInt32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 550 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | binder_status_t AParcel_readUint32(const AParcel* parcel, uint32_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 554 | status_t status = parcel->get()->readUint32(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 555 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | binder_status_t AParcel_readInt64(const AParcel* parcel, int64_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 559 | status_t status = parcel->get()->readInt64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 560 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | binder_status_t AParcel_readUint64(const AParcel* parcel, uint64_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 564 | status_t status = parcel->get()->readUint64(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 565 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | binder_status_t AParcel_readFloat(const AParcel* parcel, float* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 569 | status_t status = parcel->get()->readFloat(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 570 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | binder_status_t AParcel_readDouble(const AParcel* parcel, double* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 574 | status_t status = parcel->get()->readDouble(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 575 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | binder_status_t AParcel_readBool(const AParcel* parcel, bool* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 579 | status_t status = parcel->get()->readBool(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 580 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | binder_status_t AParcel_readChar(const AParcel* parcel, char16_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 584 | status_t status = parcel->get()->readChar(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 585 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | binder_status_t AParcel_readByte(const AParcel* parcel, int8_t* value) { |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 589 | status_t status = parcel->get()->readByte(value); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 590 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 593 | binder_status_t AParcel_writeInt32Array(AParcel* parcel, const int32_t* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 594 | return WriteArray<int32_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 597 | binder_status_t AParcel_writeUint32Array(AParcel* parcel, const uint32_t* arrayData, |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 598 | int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 599 | return WriteArray<uint32_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 602 | binder_status_t AParcel_writeInt64Array(AParcel* parcel, const int64_t* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 603 | return WriteArray<int64_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 606 | binder_status_t AParcel_writeUint64Array(AParcel* parcel, const uint64_t* arrayData, |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 607 | int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 608 | return WriteArray<uint64_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 611 | binder_status_t AParcel_writeFloatArray(AParcel* parcel, const float* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 612 | return WriteArray<float>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 615 | binder_status_t AParcel_writeDoubleArray(AParcel* parcel, const double* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 616 | return WriteArray<double>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 619 | binder_status_t AParcel_writeBoolArray(AParcel* parcel, const void* arrayData, int32_t length, |
Steven Moreland | 16e1eae | 2018-11-01 09:51:53 -0700 | [diff] [blame] | 620 | AParcel_boolArrayGetter getter) { |
| 621 | return WriteArray<bool>(parcel, arrayData, length, getter, &Parcel::writeBool); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 624 | binder_status_t AParcel_writeCharArray(AParcel* parcel, const char16_t* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 625 | return WriteArray<char16_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Steven Moreland | 01ebcf7 | 2018-11-15 15:06:26 -0800 | [diff] [blame] | 628 | binder_status_t AParcel_writeByteArray(AParcel* parcel, const int8_t* arrayData, int32_t length) { |
Steven Moreland | e3d0958 | 2018-11-02 19:29:36 -0700 | [diff] [blame] | 629 | return WriteArray<int8_t>(parcel, arrayData, length); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 632 | binder_status_t AParcel_readInt32Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 633 | AParcel_int32ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 634 | return ReadArray<int32_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 637 | binder_status_t AParcel_readUint32Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 638 | AParcel_uint32ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 639 | return ReadArray<uint32_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 642 | binder_status_t AParcel_readInt64Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 643 | AParcel_int64ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 644 | return ReadArray<int64_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 647 | binder_status_t AParcel_readUint64Array(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 648 | AParcel_uint64ArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 649 | return ReadArray<uint64_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 652 | binder_status_t AParcel_readFloatArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 653 | AParcel_floatArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 654 | return ReadArray<float>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 657 | binder_status_t AParcel_readDoubleArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 658 | AParcel_doubleArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 659 | return ReadArray<double>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 662 | binder_status_t AParcel_readBoolArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 663 | AParcel_boolArrayAllocator allocator, |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 664 | AParcel_boolArraySetter setter) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 665 | return ReadArray<bool>(parcel, arrayData, allocator, setter, &Parcel::readBool); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 668 | binder_status_t AParcel_readCharArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 669 | AParcel_charArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 670 | return ReadArray<char16_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 673 | binder_status_t AParcel_readByteArray(const AParcel* parcel, void* arrayData, |
Steven Moreland | 9ac9dca | 2018-11-01 10:07:31 -0700 | [diff] [blame] | 674 | AParcel_byteArrayAllocator allocator) { |
Steven Moreland | 71872f8 | 2018-10-29 11:46:56 -0700 | [diff] [blame] | 675 | return ReadArray<int8_t>(parcel, arrayData, allocator); |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 676 | } |
| 677 | |
John Reck | 79fb24b | 2020-02-14 13:56:19 -0800 | [diff] [blame] | 678 | bool AParcel_getAllowFds(const AParcel* parcel) { |
| 679 | return parcel->get()->allowFds(); |
| 680 | } |
| 681 | |
Jeongik Cha | 62240b9 | 2020-10-14 00:06:01 +0900 | [diff] [blame] | 682 | binder_status_t AParcel_reset(AParcel* parcel) { |
| 683 | parcel->get()->freeData(); |
| 684 | return STATUS_OK; |
| 685 | } |
| 686 | |
| 687 | int32_t AParcel_getDataSize(const AParcel* parcel) { |
| 688 | return parcel->get()->dataSize(); |
| 689 | } |
| 690 | |
| 691 | binder_status_t AParcel_appendFrom(const AParcel* from, AParcel* to, int32_t start, int32_t size) { |
| 692 | status_t status = to->get()->appendFrom(from->get(), start, size); |
| 693 | return PruneStatusT(status); |
| 694 | } |
| 695 | |
| 696 | AParcel* AParcel_create() { |
| 697 | return new AParcel(nullptr); |
| 698 | } |
| 699 | |
Yu Shan | fb39f9c | 2021-08-25 14:58:09 -0700 | [diff] [blame] | 700 | binder_status_t AParcel_marshal(const AParcel* parcel, uint8_t* buffer, size_t start, size_t len) { |
| 701 | if (parcel->get()->objectsCount()) { |
| 702 | return STATUS_INVALID_OPERATION; |
| 703 | } |
Pawan Wagh | d886a44 | 2023-01-21 02:16:38 +0000 | [diff] [blame] | 704 | // b/264739302 - getDataSize will return dataPos if it is greater than dataSize |
| 705 | // which will cause crashes in memcpy at later point. Instead compare with |
| 706 | // actual length of internal buffer |
| 707 | int32_t dataSize = parcel->get()->dataBufferSize(); |
Yu Shan | fb39f9c | 2021-08-25 14:58:09 -0700 | [diff] [blame] | 708 | if (len > static_cast<size_t>(dataSize) || start > static_cast<size_t>(dataSize) - len) { |
| 709 | return STATUS_BAD_VALUE; |
| 710 | } |
| 711 | const uint8_t* internalBuffer = parcel->get()->data(); |
Pawan Wagh | 0beb956 | 2023-01-20 19:11:39 +0000 | [diff] [blame] | 712 | if (internalBuffer == nullptr) { |
| 713 | return STATUS_UNEXPECTED_NULL; |
| 714 | } |
Yu Shan | fb39f9c | 2021-08-25 14:58:09 -0700 | [diff] [blame] | 715 | memcpy(buffer, internalBuffer + start, len); |
| 716 | return STATUS_OK; |
| 717 | } |
| 718 | |
| 719 | binder_status_t AParcel_unmarshal(AParcel* parcel, const uint8_t* buffer, size_t len) { |
| 720 | status_t status = parcel->get()->setDataSize(len); |
| 721 | if (status != ::android::OK) { |
| 722 | return PruneStatusT(status); |
| 723 | } |
| 724 | parcel->get()->setDataPosition(0); |
| 725 | |
| 726 | void* raw = parcel->get()->writeInplace(len); |
| 727 | if (raw == nullptr) { |
| 728 | return STATUS_NO_MEMORY; |
| 729 | } |
| 730 | memcpy(raw, buffer, len); |
| 731 | return STATUS_OK; |
| 732 | } |
| 733 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 734 | // @END |