1 /*
2 * Copyright (C) 2005 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 #ifndef ANDROID_PARCEL_H
18 #define ANDROID_PARCEL_H
19
20 #include <string>
21 #include <vector>
22
23 #include <linux/android/binder.h>
24
25 #include <android-base/unique_fd.h>
26 #include <cutils/native_handle.h>
27 #include <utils/Errors.h>
28 #include <utils/RefBase.h>
29 #include <utils/String16.h>
30 #include <utils/Vector.h>
31 #include <utils/Flattenable.h>
32
33 #include <binder/IInterface.h>
34 #include <binder/Parcelable.h>
35 #include <binder/Map.h>
36
37 // ---------------------------------------------------------------------------
38 namespace android {
39
40 template <typename T> class Flattenable;
41 template <typename T> class LightFlattenable;
42 class IBinder;
43 class IPCThreadState;
44 class ProcessState;
45 class String8;
46 class TextOutput;
47
48 namespace binder {
49 class Value;
50 };
51
52 class Parcel {
53 friend class IPCThreadState;
54 public:
55 class ReadableBlob;
56 class WritableBlob;
57
58 Parcel();
59 ~Parcel();
60
61 const uint8_t* data() const;
62 size_t dataSize() const;
63 size_t dataAvail() const;
64 size_t dataPosition() const;
65 size_t dataCapacity() const;
66
67 status_t setDataSize(size_t size);
68 void setDataPosition(size_t pos) const;
69 status_t setDataCapacity(size_t size);
70
71 status_t setData(const uint8_t* buffer, size_t len);
72
73 status_t appendFrom(const Parcel *parcel,
74 size_t start, size_t len);
75
76 int compareData(const Parcel& other);
77
78 bool allowFds() const;
79 bool pushAllowFds(bool allowFds);
80 void restoreAllowFds(bool lastValue);
81
82 bool hasFileDescriptors() const;
83
84 // Writes the RPC header.
85 status_t writeInterfaceToken(const String16& interface);
86
87 // Parses the RPC header, returning true if the interface name
88 // in the header matches the expected interface from the caller.
89 //
90 // Additionally, enforceInterface does part of the work of
91 // propagating the StrictMode policy mask, populating the current
92 // IPCThreadState, which as an optimization may optionally be
93 // passed in.
94 bool enforceInterface(const String16& interface,
95 IPCThreadState* threadState = nullptr) const;
96 bool checkInterface(IBinder*) const;
97
98 void freeData();
99
100 private:
101 const binder_size_t* objects() const;
102
103 public:
104 size_t objectsCount() const;
105
106 status_t errorCheck() const;
107 void setError(status_t err);
108
109 status_t write(const void* data, size_t len);
110 void* writeInplace(size_t len);
111 status_t writeUnpadded(const void* data, size_t len);
112 status_t writeInt32(int32_t val);
113 status_t writeUint32(uint32_t val);
114 status_t writeInt64(int64_t val);
115 status_t writeUint64(uint64_t val);
116 status_t writeFloat(float val);
117 status_t writeDouble(double val);
118 status_t writeCString(const char* str);
119 status_t writeString8(const String8& str);
120 status_t writeString16(const String16& str);
121 status_t writeString16(const std::unique_ptr<String16>& str);
122 status_t writeString16(const char16_t* str, size_t len);
123 status_t writeStrongBinder(const sp<IBinder>& val);
124 status_t writeWeakBinder(const wp<IBinder>& val);
125 status_t writeInt32Array(size_t len, const int32_t *val);
126 status_t writeByteArray(size_t len, const uint8_t *val);
127 status_t writeBool(bool val);
128 status_t writeChar(char16_t val);
129 status_t writeByte(int8_t val);
130
131 // Take a UTF8 encoded string, convert to UTF16, write it to the parcel.
132 status_t writeUtf8AsUtf16(const std::string& str);
133 status_t writeUtf8AsUtf16(const std::unique_ptr<std::string>& str);
134
135 status_t writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val);
136 status_t writeByteVector(const std::vector<int8_t>& val);
137 status_t writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val);
138 status_t writeByteVector(const std::vector<uint8_t>& val);
139 status_t writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val);
140 status_t writeInt32Vector(const std::vector<int32_t>& val);
141 status_t writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val);
142 status_t writeInt64Vector(const std::vector<int64_t>& val);
143 status_t writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val);
144 status_t writeUint64Vector(const std::vector<uint64_t>& val);
145 status_t writeFloatVector(const std::unique_ptr<std::vector<float>>& val);
146 status_t writeFloatVector(const std::vector<float>& val);
147 status_t writeDoubleVector(const std::unique_ptr<std::vector<double>>& val);
148 status_t writeDoubleVector(const std::vector<double>& val);
149 status_t writeBoolVector(const std::unique_ptr<std::vector<bool>>& val);
150 status_t writeBoolVector(const std::vector<bool>& val);
151 status_t writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val);
152 status_t writeCharVector(const std::vector<char16_t>& val);
153 status_t writeString16Vector(
154 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val);
155 status_t writeString16Vector(const std::vector<String16>& val);
156 status_t writeUtf8VectorAsUtf16Vector(
157 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val);
158 status_t writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val);
159
160 status_t writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val);
161 status_t writeStrongBinderVector(const std::vector<sp<IBinder>>& val);
162
163 template<typename T>
164 status_t writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val);
165 template<typename T>
166 status_t writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val);
167 template<typename T>
168 status_t writeParcelableVector(const std::vector<T>& val);
169
170 template<typename T>
171 status_t writeNullableParcelable(const std::unique_ptr<T>& parcelable);
172
173 status_t writeParcelable(const Parcelable& parcelable);
174
175 status_t writeValue(const binder::Value& value);
176
177 template<typename T>
178 status_t write(const Flattenable<T>& val);
179
180 template<typename T>
181 status_t write(const LightFlattenable<T>& val);
182
183 template<typename T>
184 status_t writeVectorSize(const std::vector<T>& val);
185 template<typename T>
186 status_t writeVectorSize(const std::unique_ptr<std::vector<T>>& val);
187
188 status_t writeMap(const binder::Map& map);
189 status_t writeNullableMap(const std::unique_ptr<binder::Map>& map);
190
191 // Place a native_handle into the parcel (the native_handle's file-
192 // descriptors are dup'ed, so it is safe to delete the native_handle
193 // when this function returns).
194 // Doesn't take ownership of the native_handle.
195 status_t writeNativeHandle(const native_handle* handle);
196
197 // Place a file descriptor into the parcel. The given fd must remain
198 // valid for the lifetime of the parcel.
199 // The Parcel does not take ownership of the given fd unless you ask it to.
200 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
201
202 // Place a file descriptor into the parcel. A dup of the fd is made, which
203 // will be closed once the parcel is destroyed.
204 status_t writeDupFileDescriptor(int fd);
205
206 // Place a Java "parcel file descriptor" into the parcel. The given fd must remain
207 // valid for the lifetime of the parcel.
208 // The Parcel does not take ownership of the given fd unless you ask it to.
209 status_t writeParcelFileDescriptor(int fd, bool takeOwnership = false);
210
211 // Place a Java "parcel file descriptor" into the parcel. A dup of the fd is made, which will
212 // be closed once the parcel is destroyed.
213 status_t writeDupParcelFileDescriptor(int fd);
214
215 // Place a file descriptor into the parcel. This will not affect the
216 // semantics of the smart file descriptor. A new descriptor will be
217 // created, and will be closed when the parcel is destroyed.
218 status_t writeUniqueFileDescriptor(
219 const base::unique_fd& fd);
220
221 // Place a vector of file desciptors into the parcel. Each descriptor is
222 // dup'd as in writeDupFileDescriptor
223 status_t writeUniqueFileDescriptorVector(
224 const std::unique_ptr<std::vector<base::unique_fd>>& val);
225 status_t writeUniqueFileDescriptorVector(
226 const std::vector<base::unique_fd>& val);
227
228 // Writes a blob to the parcel.
229 // If the blob is small, then it is stored in-place, otherwise it is
230 // transferred by way of an anonymous shared memory region. Prefer sending
231 // immutable blobs if possible since they may be subsequently transferred between
232 // processes without further copying whereas mutable blobs always need to be copied.
233 // The caller should call release() on the blob after writing its contents.
234 status_t writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob);
235
236 // Write an existing immutable blob file descriptor to the parcel.
237 // This allows the client to send the same blob to multiple processes
238 // as long as it keeps a dup of the blob file descriptor handy for later.
239 status_t writeDupImmutableBlobFileDescriptor(int fd);
240
241 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
242
243 // Like Parcel.java's writeNoException(). Just writes a zero int32.
244 // Currently the native implementation doesn't do any of the StrictMode
245 // stack gathering and serialization that the Java implementation does.
246 status_t writeNoException();
247
248 void remove(size_t start, size_t amt);
249
250 status_t read(void* outData, size_t len) const;
251 const void* readInplace(size_t len) const;
252 int32_t readInt32() const;
253 status_t readInt32(int32_t *pArg) const;
254 uint32_t readUint32() const;
255 status_t readUint32(uint32_t *pArg) const;
256 int64_t readInt64() const;
257 status_t readInt64(int64_t *pArg) const;
258 uint64_t readUint64() const;
259 status_t readUint64(uint64_t *pArg) const;
260 float readFloat() const;
261 status_t readFloat(float *pArg) const;
262 double readDouble() const;
263 status_t readDouble(double *pArg) const;
264 intptr_t readIntPtr() const;
265 status_t readIntPtr(intptr_t *pArg) const;
266 bool readBool() const;
267 status_t readBool(bool *pArg) const;
268 char16_t readChar() const;
269 status_t readChar(char16_t *pArg) const;
270 int8_t readByte() const;
271 status_t readByte(int8_t *pArg) const;
272
273 // Read a UTF16 encoded string, convert to UTF8
274 status_t readUtf8FromUtf16(std::string* str) const;
275 status_t readUtf8FromUtf16(std::unique_ptr<std::string>* str) const;
276
277 const char* readCString() const;
278 String8 readString8() const;
279 status_t readString8(String8* pArg) const;
280 String16 readString16() const;
281 status_t readString16(String16* pArg) const;
282 status_t readString16(std::unique_ptr<String16>* pArg) const;
283 const char16_t* readString16Inplace(size_t* outLen) const;
284 sp<IBinder> readStrongBinder() const;
285 status_t readStrongBinder(sp<IBinder>* val) const;
286 status_t readNullableStrongBinder(sp<IBinder>* val) const;
287 wp<IBinder> readWeakBinder() const;
288
289 template<typename T>
290 status_t readParcelableVector(
291 std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const;
292 template<typename T>
293 status_t readParcelableVector(std::vector<T>* val) const;
294
295 status_t readParcelable(Parcelable* parcelable) const;
296
297 template<typename T>
298 status_t readParcelable(std::unique_ptr<T>* parcelable) const;
299
300 status_t readValue(binder::Value* value) const;
301
302 template<typename T>
303 status_t readStrongBinder(sp<T>* val) const;
304
305 template<typename T>
306 status_t readNullableStrongBinder(sp<T>* val) const;
307
308 status_t readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const;
309 status_t readStrongBinderVector(std::vector<sp<IBinder>>* val) const;
310
311 status_t readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const;
312 status_t readByteVector(std::vector<int8_t>* val) const;
313 status_t readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const;
314 status_t readByteVector(std::vector<uint8_t>* val) const;
315 status_t readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const;
316 status_t readInt32Vector(std::vector<int32_t>* val) const;
317 status_t readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const;
318 status_t readInt64Vector(std::vector<int64_t>* val) const;
319 status_t readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const;
320 status_t readUint64Vector(std::vector<uint64_t>* val) const;
321 status_t readFloatVector(std::unique_ptr<std::vector<float>>* val) const;
322 status_t readFloatVector(std::vector<float>* val) const;
323 status_t readDoubleVector(std::unique_ptr<std::vector<double>>* val) const;
324 status_t readDoubleVector(std::vector<double>* val) const;
325 status_t readBoolVector(std::unique_ptr<std::vector<bool>>* val) const;
326 status_t readBoolVector(std::vector<bool>* val) const;
327 status_t readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const;
328 status_t readCharVector(std::vector<char16_t>* val) const;
329 status_t readString16Vector(
330 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const;
331 status_t readString16Vector(std::vector<String16>* val) const;
332 status_t readUtf8VectorFromUtf16Vector(
333 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const;
334 status_t readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const;
335
336 template<typename T>
337 status_t read(Flattenable<T>& val) const;
338
339 template<typename T>
340 status_t read(LightFlattenable<T>& val) const;
341
342 template<typename T>
343 status_t resizeOutVector(std::vector<T>* val) const;
344 template<typename T>
345 status_t resizeOutVector(std::unique_ptr<std::vector<T>>* val) const;
346
347 status_t readMap(binder::Map* map)const;
348 status_t readNullableMap(std::unique_ptr<binder::Map>* map) const;
349
350 // Like Parcel.java's readExceptionCode(). Reads the first int32
351 // off of a Parcel's header, returning 0 or the negative error
352 // code on exceptions, but also deals with skipping over rich
353 // response headers. Callers should use this to read & parse the
354 // response headers rather than doing it by hand.
355 int32_t readExceptionCode() const;
356
357 // Retrieve native_handle from the parcel. This returns a copy of the
358 // parcel's native_handle (the caller takes ownership). The caller
359 // must free the native_handle with native_handle_close() and
360 // native_handle_delete().
361 native_handle* readNativeHandle() const;
362
363
364 // Retrieve a file descriptor from the parcel. This returns the raw fd
365 // in the parcel, which you do not own -- use dup() to get your own copy.
366 int readFileDescriptor() const;
367
368 // Retrieve a Java "parcel file descriptor" from the parcel. This returns the raw fd
369 // in the parcel, which you do not own -- use dup() to get your own copy.
370 int readParcelFileDescriptor() const;
371
372 // Retrieve a smart file descriptor from the parcel.
373 status_t readUniqueFileDescriptor(
374 base::unique_fd* val) const;
375
376 // Retrieve a Java "parcel file descriptor" from the parcel.
377 status_t readUniqueParcelFileDescriptor(base::unique_fd* val) const;
378
379
380 // Retrieve a vector of smart file descriptors from the parcel.
381 status_t readUniqueFileDescriptorVector(
382 std::unique_ptr<std::vector<base::unique_fd>>* val) const;
383 status_t readUniqueFileDescriptorVector(
384 std::vector<base::unique_fd>* val) const;
385
386 // Reads a blob from the parcel.
387 // The caller should call release() on the blob after reading its contents.
388 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
389
390 const flat_binder_object* readObject(bool nullMetaData) const;
391
392 // Explicitly close all file descriptors in the parcel.
393 void closeFileDescriptors();
394
395 // Debugging: get metrics on current allocations.
396 static size_t getGlobalAllocSize();
397 static size_t getGlobalAllocCount();
398
399 bool replaceCallingWorkSourceUid(uid_t uid);
400 // Returns the work source provided by the caller. This can only be trusted for trusted calling
401 // uid.
402 uid_t readCallingWorkSourceUid();
403 void readRequestHeaders() const;
404
405 private:
406 typedef void (*release_func)(Parcel* parcel,
407 const uint8_t* data, size_t dataSize,
408 const binder_size_t* objects, size_t objectsSize,
409 void* cookie);
410
411 uintptr_t ipcData() const;
412 size_t ipcDataSize() const;
413 uintptr_t ipcObjects() const;
414 size_t ipcObjectsCount() const;
415 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
416 const binder_size_t* objects, size_t objectsCount,
417 release_func relFunc, void* relCookie);
418
419 public:
420 void print(TextOutput& to, uint32_t flags = 0) const;
421
422 private:
423 Parcel(const Parcel& o);
424 Parcel& operator=(const Parcel& o);
425
426 status_t finishWrite(size_t len);
427 void releaseObjects();
428 void acquireObjects();
429 status_t growData(size_t len);
430 status_t restartWrite(size_t desired);
431 status_t continueWrite(size_t desired);
432 status_t writePointer(uintptr_t val);
433 status_t readPointer(uintptr_t *pArg) const;
434 uintptr_t readPointer() const;
435 void freeDataNoInit();
436 void initState();
437 void scanForFds() const;
438 status_t validateReadData(size_t len) const;
439 void updateWorkSourceRequestHeaderPosition() const;
440
441 template<class T>
442 status_t readAligned(T *pArg) const;
443
444 template<class T> T readAligned() const;
445
446 template<class T>
447 status_t writeAligned(T val);
448
449 status_t writeRawNullableParcelable(const Parcelable*
450 parcelable);
451
452 template<typename T, typename U>
453 status_t unsafeReadTypedVector(std::vector<T>* val,
454 status_t(Parcel::*read_func)(U*) const) const;
455 template<typename T>
456 status_t readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
457 status_t(Parcel::*read_func)(T*) const) const;
458 template<typename T>
459 status_t readTypedVector(std::vector<T>* val,
460 status_t(Parcel::*read_func)(T*) const) const;
461 template<typename T, typename U>
462 status_t unsafeWriteTypedVector(const std::vector<T>& val,
463 status_t(Parcel::*write_func)(U));
464 template<typename T>
465 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
466 status_t(Parcel::*write_func)(const T&));
467 template<typename T>
468 status_t writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
469 status_t(Parcel::*write_func)(T));
470 template<typename T>
471 status_t writeTypedVector(const std::vector<T>& val,
472 status_t(Parcel::*write_func)(const T&));
473 template<typename T>
474 status_t writeTypedVector(const std::vector<T>& val,
475 status_t(Parcel::*write_func)(T));
476
477 status_t mError;
478 uint8_t* mData;
479 size_t mDataSize;
480 size_t mDataCapacity;
481 mutable size_t mDataPos;
482 binder_size_t* mObjects;
483 size_t mObjectsSize;
484 size_t mObjectsCapacity;
485 mutable size_t mNextObjectHint;
486 mutable bool mObjectsSorted;
487
488 mutable bool mRequestHeaderPresent;
489 mutable size_t mWorkSourceRequestHeaderPosition;
490
491 mutable bool mFdsKnown;
492 mutable bool mHasFds;
493 bool mAllowFds;
494
495 release_func mOwner;
496 void* mOwnerCookie;
497
498 class Blob {
499 public:
500 Blob();
501 ~Blob();
502
503 void clear();
504 void release();
size()505 inline size_t size() const { return mSize; }
fd()506 inline int fd() const { return mFd; }
isMutable()507 inline bool isMutable() const { return mMutable; }
508
509 protected:
510 void init(int fd, void* data, size_t size, bool isMutable);
511
512 int mFd; // owned by parcel so not closed when released
513 void* mData;
514 size_t mSize;
515 bool mMutable;
516 };
517
518 #if defined(__clang__)
519 #pragma clang diagnostic push
520 #pragma clang diagnostic ignored "-Wweak-vtables"
521 #endif
522
523 // FlattenableHelperInterface and FlattenableHelper avoid generating a vtable entry in objects
524 // following Flattenable template/protocol.
525 class FlattenableHelperInterface {
526 protected:
~FlattenableHelperInterface()527 ~FlattenableHelperInterface() { }
528 public:
529 virtual size_t getFlattenedSize() const = 0;
530 virtual size_t getFdCount() const = 0;
531 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
532 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
533 };
534
535 #if defined(__clang__)
536 #pragma clang diagnostic pop
537 #endif
538
539 // Concrete implementation of FlattenableHelperInterface that delegates virtual calls to the
540 // specified class T implementing the Flattenable protocol. It "virtualizes" a compile-time
541 // protocol.
542 template<typename T>
543 class FlattenableHelper : public FlattenableHelperInterface {
544 friend class Parcel;
545 const Flattenable<T>& val;
FlattenableHelper(const Flattenable<T> & _val)546 explicit FlattenableHelper(const Flattenable<T>& _val) : val(_val) { }
547
548 protected:
549 ~FlattenableHelper() = default;
550 public:
getFlattenedSize()551 virtual size_t getFlattenedSize() const {
552 return val.getFlattenedSize();
553 }
getFdCount()554 virtual size_t getFdCount() const {
555 return val.getFdCount();
556 }
flatten(void * buffer,size_t size,int * fds,size_t count)557 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
558 return val.flatten(buffer, size, fds, count);
559 }
unflatten(void const * buffer,size_t size,int const * fds,size_t count)560 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
561 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
562 }
563 };
564 status_t write(const FlattenableHelperInterface& val);
565 status_t read(FlattenableHelperInterface& val) const;
566
567 public:
568 class ReadableBlob : public Blob {
569 friend class Parcel;
570 public:
data()571 inline const void* data() const { return mData; }
mutableData()572 inline void* mutableData() { return isMutable() ? mData : nullptr; }
573 };
574
575 class WritableBlob : public Blob {
576 friend class Parcel;
577 public:
data()578 inline void* data() { return mData; }
579 };
580
581 private:
582 size_t mOpenAshmemSize;
583
584 public:
585 // TODO: Remove once ABI can be changed.
586 size_t getBlobAshmemSize() const;
587 size_t getOpenAshmemSize() const;
588 };
589
590 // ---------------------------------------------------------------------------
591
592 template<typename T>
write(const Flattenable<T> & val)593 status_t Parcel::write(const Flattenable<T>& val) {
594 const FlattenableHelper<T> helper(val);
595 return write(helper);
596 }
597
598 template<typename T>
write(const LightFlattenable<T> & val)599 status_t Parcel::write(const LightFlattenable<T>& val) {
600 size_t size(val.getFlattenedSize());
601 if (!val.isFixedSize()) {
602 if (size > INT32_MAX) {
603 return BAD_VALUE;
604 }
605 status_t err = writeInt32(static_cast<int32_t>(size));
606 if (err != NO_ERROR) {
607 return err;
608 }
609 }
610 if (size) {
611 void* buffer = writeInplace(size);
612 if (buffer == nullptr)
613 return NO_MEMORY;
614 return val.flatten(buffer, size);
615 }
616 return NO_ERROR;
617 }
618
619 template<typename T>
read(Flattenable<T> & val)620 status_t Parcel::read(Flattenable<T>& val) const {
621 FlattenableHelper<T> helper(val);
622 return read(helper);
623 }
624
625 template<typename T>
read(LightFlattenable<T> & val)626 status_t Parcel::read(LightFlattenable<T>& val) const {
627 size_t size;
628 if (val.isFixedSize()) {
629 size = val.getFlattenedSize();
630 } else {
631 int32_t s;
632 status_t err = readInt32(&s);
633 if (err != NO_ERROR) {
634 return err;
635 }
636 size = static_cast<size_t>(s);
637 }
638 if (size) {
639 void const* buffer = readInplace(size);
640 return buffer == nullptr ? NO_MEMORY :
641 val.unflatten(buffer, size);
642 }
643 return NO_ERROR;
644 }
645
646 template<typename T>
writeVectorSize(const std::vector<T> & val)647 status_t Parcel::writeVectorSize(const std::vector<T>& val) {
648 if (val.size() > INT32_MAX) {
649 return BAD_VALUE;
650 }
651 return writeInt32(static_cast<int32_t>(val.size()));
652 }
653
654 template<typename T>
writeVectorSize(const std::unique_ptr<std::vector<T>> & val)655 status_t Parcel::writeVectorSize(const std::unique_ptr<std::vector<T>>& val) {
656 if (!val) {
657 return writeInt32(-1);
658 }
659
660 return writeVectorSize(*val);
661 }
662
663 template<typename T>
resizeOutVector(std::vector<T> * val)664 status_t Parcel::resizeOutVector(std::vector<T>* val) const {
665 int32_t size;
666 status_t err = readInt32(&size);
667 if (err != NO_ERROR) {
668 return err;
669 }
670
671 if (size < 0) {
672 return UNEXPECTED_NULL;
673 }
674 val->resize(size_t(size));
675 return OK;
676 }
677
678 template<typename T>
resizeOutVector(std::unique_ptr<std::vector<T>> * val)679 status_t Parcel::resizeOutVector(std::unique_ptr<std::vector<T>>* val) const {
680 int32_t size;
681 status_t err = readInt32(&size);
682 if (err != NO_ERROR) {
683 return err;
684 }
685
686 val->reset();
687 if (size >= 0) {
688 val->reset(new std::vector<T>(size_t(size)));
689 }
690
691 return OK;
692 }
693
694 template<typename T>
readStrongBinder(sp<T> * val)695 status_t Parcel::readStrongBinder(sp<T>* val) const {
696 sp<IBinder> tmp;
697 status_t ret = readStrongBinder(&tmp);
698
699 if (ret == OK) {
700 *val = interface_cast<T>(tmp);
701
702 if (val->get() == nullptr) {
703 return UNKNOWN_ERROR;
704 }
705 }
706
707 return ret;
708 }
709
710 template<typename T>
readNullableStrongBinder(sp<T> * val)711 status_t Parcel::readNullableStrongBinder(sp<T>* val) const {
712 sp<IBinder> tmp;
713 status_t ret = readNullableStrongBinder(&tmp);
714
715 if (ret == OK) {
716 *val = interface_cast<T>(tmp);
717
718 if (val->get() == nullptr && tmp.get() != nullptr) {
719 ret = UNKNOWN_ERROR;
720 }
721 }
722
723 return ret;
724 }
725
726 template<typename T, typename U>
unsafeReadTypedVector(std::vector<T> * val,status_t (Parcel::* read_func)(U *)const)727 status_t Parcel::unsafeReadTypedVector(
728 std::vector<T>* val,
729 status_t(Parcel::*read_func)(U*) const) const {
730 int32_t size;
731 status_t status = this->readInt32(&size);
732
733 if (status != OK) {
734 return status;
735 }
736
737 if (size < 0) {
738 return UNEXPECTED_NULL;
739 }
740
741 if (val->max_size() < static_cast<size_t>(size)) {
742 return NO_MEMORY;
743 }
744
745 val->resize(static_cast<size_t>(size));
746
747 if (val->size() < static_cast<size_t>(size)) {
748 return NO_MEMORY;
749 }
750
751 for (auto& v: *val) {
752 status = (this->*read_func)(&v);
753
754 if (status != OK) {
755 return status;
756 }
757 }
758
759 return OK;
760 }
761
762 template<typename T>
readTypedVector(std::vector<T> * val,status_t (Parcel::* read_func)(T *)const)763 status_t Parcel::readTypedVector(std::vector<T>* val,
764 status_t(Parcel::*read_func)(T*) const) const {
765 return unsafeReadTypedVector(val, read_func);
766 }
767
768 template<typename T>
readNullableTypedVector(std::unique_ptr<std::vector<T>> * val,status_t (Parcel::* read_func)(T *)const)769 status_t Parcel::readNullableTypedVector(std::unique_ptr<std::vector<T>>* val,
770 status_t(Parcel::*read_func)(T*) const) const {
771 const size_t start = dataPosition();
772 int32_t size;
773 status_t status = readInt32(&size);
774 val->reset();
775
776 if (status != OK || size < 0) {
777 return status;
778 }
779
780 setDataPosition(start);
781 val->reset(new std::vector<T>());
782
783 status = unsafeReadTypedVector(val->get(), read_func);
784
785 if (status != OK) {
786 val->reset();
787 }
788
789 return status;
790 }
791
792 template<typename T, typename U>
unsafeWriteTypedVector(const std::vector<T> & val,status_t (Parcel::* write_func)(U))793 status_t Parcel::unsafeWriteTypedVector(const std::vector<T>& val,
794 status_t(Parcel::*write_func)(U)) {
795 if (val.size() > std::numeric_limits<int32_t>::max()) {
796 return BAD_VALUE;
797 }
798
799 status_t status = this->writeInt32(static_cast<int32_t>(val.size()));
800
801 if (status != OK) {
802 return status;
803 }
804
805 for (const auto& item : val) {
806 status = (this->*write_func)(item);
807
808 if (status != OK) {
809 return status;
810 }
811 }
812
813 return OK;
814 }
815
816 template<typename T>
writeTypedVector(const std::vector<T> & val,status_t (Parcel::* write_func)(const T &))817 status_t Parcel::writeTypedVector(const std::vector<T>& val,
818 status_t(Parcel::*write_func)(const T&)) {
819 return unsafeWriteTypedVector(val, write_func);
820 }
821
822 template<typename T>
writeTypedVector(const std::vector<T> & val,status_t (Parcel::* write_func)(T))823 status_t Parcel::writeTypedVector(const std::vector<T>& val,
824 status_t(Parcel::*write_func)(T)) {
825 return unsafeWriteTypedVector(val, write_func);
826 }
827
828 template<typename T>
writeNullableTypedVector(const std::unique_ptr<std::vector<T>> & val,status_t (Parcel::* write_func)(const T &))829 status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
830 status_t(Parcel::*write_func)(const T&)) {
831 if (val.get() == nullptr) {
832 return this->writeInt32(-1);
833 }
834
835 return unsafeWriteTypedVector(*val, write_func);
836 }
837
838 template<typename T>
writeNullableTypedVector(const std::unique_ptr<std::vector<T>> & val,status_t (Parcel::* write_func)(T))839 status_t Parcel::writeNullableTypedVector(const std::unique_ptr<std::vector<T>>& val,
840 status_t(Parcel::*write_func)(T)) {
841 if (val.get() == nullptr) {
842 return this->writeInt32(-1);
843 }
844
845 return unsafeWriteTypedVector(*val, write_func);
846 }
847
848 template<typename T>
readParcelableVector(std::vector<T> * val)849 status_t Parcel::readParcelableVector(std::vector<T>* val) const {
850 return unsafeReadTypedVector<T, Parcelable>(val, &Parcel::readParcelable);
851 }
852
853 template<typename T>
readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>> * val)854 status_t Parcel::readParcelableVector(std::unique_ptr<std::vector<std::unique_ptr<T>>>* val) const {
855 const size_t start = dataPosition();
856 int32_t size;
857 status_t status = readInt32(&size);
858 val->reset();
859
860 if (status != OK || size < 0) {
861 return status;
862 }
863
864 setDataPosition(start);
865 val->reset(new std::vector<std::unique_ptr<T>>());
866
867 status = unsafeReadTypedVector(val->get(), &Parcel::readParcelable<T>);
868
869 if (status != OK) {
870 val->reset();
871 }
872
873 return status;
874 }
875
876 template<typename T>
readParcelable(std::unique_ptr<T> * parcelable)877 status_t Parcel::readParcelable(std::unique_ptr<T>* parcelable) const {
878 const size_t start = dataPosition();
879 int32_t present;
880 status_t status = readInt32(&present);
881 parcelable->reset();
882
883 if (status != OK || !present) {
884 return status;
885 }
886
887 setDataPosition(start);
888 parcelable->reset(new T());
889
890 status = readParcelable(parcelable->get());
891
892 if (status != OK) {
893 parcelable->reset();
894 }
895
896 return status;
897 }
898
899 template<typename T>
writeNullableParcelable(const std::unique_ptr<T> & parcelable)900 status_t Parcel::writeNullableParcelable(const std::unique_ptr<T>& parcelable) {
901 return writeRawNullableParcelable(parcelable.get());
902 }
903
904 template<typename T>
writeParcelableVector(const std::vector<T> & val)905 status_t Parcel::writeParcelableVector(const std::vector<T>& val) {
906 return unsafeWriteTypedVector<T,const Parcelable&>(val, &Parcel::writeParcelable);
907 }
908
909 template<typename T>
writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>> & val)910 status_t Parcel::writeParcelableVector(const std::unique_ptr<std::vector<std::unique_ptr<T>>>& val) {
911 if (val.get() == nullptr) {
912 return this->writeInt32(-1);
913 }
914
915 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
916 }
917
918 template<typename T>
writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>> & val)919 status_t Parcel::writeParcelableVector(const std::shared_ptr<std::vector<std::unique_ptr<T>>>& val) {
920 if (val.get() == nullptr) {
921 return this->writeInt32(-1);
922 }
923
924 return unsafeWriteTypedVector(*val, &Parcel::writeNullableParcelable<T>);
925 }
926
927 // ---------------------------------------------------------------------------
928
929 inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
930 {
931 parcel.print(to);
932 return to;
933 }
934
935 // ---------------------------------------------------------------------------
936
937 // Generic acquire and release of objects.
938 void acquire_object(const sp<ProcessState>& proc,
939 const flat_binder_object& obj, const void* who);
940 void release_object(const sp<ProcessState>& proc,
941 const flat_binder_object& obj, const void* who);
942
943 void flatten_binder(const sp<ProcessState>& proc,
944 const sp<IBinder>& binder, flat_binder_object* out);
945 void flatten_binder(const sp<ProcessState>& proc,
946 const wp<IBinder>& binder, flat_binder_object* out);
947 status_t unflatten_binder(const sp<ProcessState>& proc,
948 const flat_binder_object& flat, sp<IBinder>* out);
949 status_t unflatten_binder(const sp<ProcessState>& proc,
950 const flat_binder_object& flat, wp<IBinder>* out);
951
952 }; // namespace android
953
954 // ---------------------------------------------------------------------------
955
956 #endif // ANDROID_PARCEL_H
957