Parcel.cpp revision f4c1088d750a182bd85889c86b3c9ef4ba772405
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#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
20#include <binder/Parcel.h>
21
22#include <binder/IPCThreadState.h>
23#include <binder/Binder.h>
24#include <binder/BpBinder.h>
25#include <utils/Debug.h>
26#include <binder/ProcessState.h>
27#include <utils/Log.h>
28#include <utils/String8.h>
29#include <utils/String16.h>
30#include <utils/TextOutput.h>
31#include <utils/misc.h>
32#include <utils/Flattenable.h>
33#include <cutils/ashmem.h>
34
35#include <private/binder/binder_module.h>
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdint.h>
40#include <sys/mman.h>
41
42#ifndef INT32_MAX
43#define INT32_MAX ((int32_t)(2147483647))
44#endif
45
46#define LOG_REFS(...)
47//#define LOG_REFS(...) LOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
48
49// ---------------------------------------------------------------------------
50
51#define PAD_SIZE(s) (((s)+3)&~3)
52
53// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
54#define STRICT_MODE_PENALTY_GATHER 0x100
55
56// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
57#define EX_HAS_REPLY_HEADER -128
58
59// Maximum size of a blob to transfer in-place.
60static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
61
62// XXX This can be made public if we want to provide
63// support for typed data.
64struct small_flat_data
65{
66    uint32_t type;
67    uint32_t data;
68};
69
70namespace android {
71
72void acquire_object(const sp<ProcessState>& proc,
73    const flat_binder_object& obj, const void* who)
74{
75    switch (obj.type) {
76        case BINDER_TYPE_BINDER:
77            if (obj.binder) {
78                LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
79                static_cast<IBinder*>(obj.cookie)->incStrong(who);
80            }
81            return;
82        case BINDER_TYPE_WEAK_BINDER:
83            if (obj.binder)
84                static_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
85            return;
86        case BINDER_TYPE_HANDLE: {
87            const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
88            if (b != NULL) {
89                LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
90                b->incStrong(who);
91            }
92            return;
93        }
94        case BINDER_TYPE_WEAK_HANDLE: {
95            const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
96            if (b != NULL) b.get_refs()->incWeak(who);
97            return;
98        }
99        case BINDER_TYPE_FD: {
100            // intentionally blank -- nothing to do to acquire this, but we do
101            // recognize it as a legitimate object type.
102            return;
103        }
104    }
105
106    LOGD("Invalid object type 0x%08lx", obj.type);
107}
108
109void release_object(const sp<ProcessState>& proc,
110    const flat_binder_object& obj, const void* who)
111{
112    switch (obj.type) {
113        case BINDER_TYPE_BINDER:
114            if (obj.binder) {
115                LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
116                static_cast<IBinder*>(obj.cookie)->decStrong(who);
117            }
118            return;
119        case BINDER_TYPE_WEAK_BINDER:
120            if (obj.binder)
121                static_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
122            return;
123        case BINDER_TYPE_HANDLE: {
124            const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
125            if (b != NULL) {
126                LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
127                b->decStrong(who);
128            }
129            return;
130        }
131        case BINDER_TYPE_WEAK_HANDLE: {
132            const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
133            if (b != NULL) b.get_refs()->decWeak(who);
134            return;
135        }
136        case BINDER_TYPE_FD: {
137            if (obj.cookie != (void*)0) close(obj.handle);
138            return;
139        }
140    }
141
142    LOGE("Invalid object type 0x%08lx", obj.type);
143}
144
145inline static status_t finish_flatten_binder(
146    const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out)
147{
148    return out->writeObject(flat, false);
149}
150
151status_t flatten_binder(const sp<ProcessState>& proc,
152    const sp<IBinder>& binder, Parcel* out)
153{
154    flat_binder_object obj;
155
156    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
157    if (binder != NULL) {
158        IBinder *local = binder->localBinder();
159        if (!local) {
160            BpBinder *proxy = binder->remoteBinder();
161            if (proxy == NULL) {
162                LOGE("null proxy");
163            }
164            const int32_t handle = proxy ? proxy->handle() : 0;
165            obj.type = BINDER_TYPE_HANDLE;
166            obj.handle = handle;
167            obj.cookie = NULL;
168        } else {
169            obj.type = BINDER_TYPE_BINDER;
170            obj.binder = local->getWeakRefs();
171            obj.cookie = local;
172        }
173    } else {
174        obj.type = BINDER_TYPE_BINDER;
175        obj.binder = NULL;
176        obj.cookie = NULL;
177    }
178
179    return finish_flatten_binder(binder, obj, out);
180}
181
182status_t flatten_binder(const sp<ProcessState>& proc,
183    const wp<IBinder>& binder, Parcel* out)
184{
185    flat_binder_object obj;
186
187    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
188    if (binder != NULL) {
189        sp<IBinder> real = binder.promote();
190        if (real != NULL) {
191            IBinder *local = real->localBinder();
192            if (!local) {
193                BpBinder *proxy = real->remoteBinder();
194                if (proxy == NULL) {
195                    LOGE("null proxy");
196                }
197                const int32_t handle = proxy ? proxy->handle() : 0;
198                obj.type = BINDER_TYPE_WEAK_HANDLE;
199                obj.handle = handle;
200                obj.cookie = NULL;
201            } else {
202                obj.type = BINDER_TYPE_WEAK_BINDER;
203                obj.binder = binder.get_refs();
204                obj.cookie = binder.unsafe_get();
205            }
206            return finish_flatten_binder(real, obj, out);
207        }
208
209        // XXX How to deal?  In order to flatten the given binder,
210        // we need to probe it for information, which requires a primary
211        // reference...  but we don't have one.
212        //
213        // The OpenBinder implementation uses a dynamic_cast<> here,
214        // but we can't do that with the different reference counting
215        // implementation we are using.
216        LOGE("Unable to unflatten Binder weak reference!");
217        obj.type = BINDER_TYPE_BINDER;
218        obj.binder = NULL;
219        obj.cookie = NULL;
220        return finish_flatten_binder(NULL, obj, out);
221
222    } else {
223        obj.type = BINDER_TYPE_BINDER;
224        obj.binder = NULL;
225        obj.cookie = NULL;
226        return finish_flatten_binder(NULL, obj, out);
227    }
228}
229
230inline static status_t finish_unflatten_binder(
231    BpBinder* proxy, const flat_binder_object& flat, const Parcel& in)
232{
233    return NO_ERROR;
234}
235
236status_t unflatten_binder(const sp<ProcessState>& proc,
237    const Parcel& in, sp<IBinder>* out)
238{
239    const flat_binder_object* flat = in.readObject(false);
240
241    if (flat) {
242        switch (flat->type) {
243            case BINDER_TYPE_BINDER:
244                *out = static_cast<IBinder*>(flat->cookie);
245                return finish_unflatten_binder(NULL, *flat, in);
246            case BINDER_TYPE_HANDLE:
247                *out = proc->getStrongProxyForHandle(flat->handle);
248                return finish_unflatten_binder(
249                    static_cast<BpBinder*>(out->get()), *flat, in);
250        }
251    }
252    return BAD_TYPE;
253}
254
255status_t unflatten_binder(const sp<ProcessState>& proc,
256    const Parcel& in, wp<IBinder>* out)
257{
258    const flat_binder_object* flat = in.readObject(false);
259
260    if (flat) {
261        switch (flat->type) {
262            case BINDER_TYPE_BINDER:
263                *out = static_cast<IBinder*>(flat->cookie);
264                return finish_unflatten_binder(NULL, *flat, in);
265            case BINDER_TYPE_WEAK_BINDER:
266                if (flat->binder != NULL) {
267                    out->set_object_and_refs(
268                        static_cast<IBinder*>(flat->cookie),
269                        static_cast<RefBase::weakref_type*>(flat->binder));
270                } else {
271                    *out = NULL;
272                }
273                return finish_unflatten_binder(NULL, *flat, in);
274            case BINDER_TYPE_HANDLE:
275            case BINDER_TYPE_WEAK_HANDLE:
276                *out = proc->getWeakProxyForHandle(flat->handle);
277                return finish_unflatten_binder(
278                    static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
279        }
280    }
281    return BAD_TYPE;
282}
283
284// ---------------------------------------------------------------------------
285
286Parcel::Parcel()
287{
288    initState();
289}
290
291Parcel::~Parcel()
292{
293    freeDataNoInit();
294}
295
296const uint8_t* Parcel::data() const
297{
298    return mData;
299}
300
301size_t Parcel::dataSize() const
302{
303    return (mDataSize > mDataPos ? mDataSize : mDataPos);
304}
305
306size_t Parcel::dataAvail() const
307{
308    // TODO: decide what to do about the possibility that this can
309    // report an available-data size that exceeds a Java int's max
310    // positive value, causing havoc.  Fortunately this will only
311    // happen if someone constructs a Parcel containing more than two
312    // gigabytes of data, which on typical phone hardware is simply
313    // not possible.
314    return dataSize() - dataPosition();
315}
316
317size_t Parcel::dataPosition() const
318{
319    return mDataPos;
320}
321
322size_t Parcel::dataCapacity() const
323{
324    return mDataCapacity;
325}
326
327status_t Parcel::setDataSize(size_t size)
328{
329    status_t err;
330    err = continueWrite(size);
331    if (err == NO_ERROR) {
332        mDataSize = size;
333        LOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
334    }
335    return err;
336}
337
338void Parcel::setDataPosition(size_t pos) const
339{
340    mDataPos = pos;
341    mNextObjectHint = 0;
342}
343
344status_t Parcel::setDataCapacity(size_t size)
345{
346    if (size > mDataCapacity) return continueWrite(size);
347    return NO_ERROR;
348}
349
350status_t Parcel::setData(const uint8_t* buffer, size_t len)
351{
352    status_t err = restartWrite(len);
353    if (err == NO_ERROR) {
354        memcpy(const_cast<uint8_t*>(data()), buffer, len);
355        mDataSize = len;
356        mFdsKnown = false;
357    }
358    return err;
359}
360
361status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
362{
363    const sp<ProcessState> proc(ProcessState::self());
364    status_t err;
365    const uint8_t *data = parcel->mData;
366    const size_t *objects = parcel->mObjects;
367    size_t size = parcel->mObjectsSize;
368    int startPos = mDataPos;
369    int firstIndex = -1, lastIndex = -2;
370
371    if (len == 0) {
372        return NO_ERROR;
373    }
374
375    // range checks against the source parcel size
376    if ((offset > parcel->mDataSize)
377            || (len > parcel->mDataSize)
378            || (offset + len > parcel->mDataSize)) {
379        return BAD_VALUE;
380    }
381
382    // Count objects in range
383    for (int i = 0; i < (int) size; i++) {
384        size_t off = objects[i];
385        if ((off >= offset) && (off < offset + len)) {
386            if (firstIndex == -1) {
387                firstIndex = i;
388            }
389            lastIndex = i;
390        }
391    }
392    int numObjects = lastIndex - firstIndex + 1;
393
394    if ((mDataSize+len) > mDataCapacity) {
395        // grow data
396        err = growData(len);
397        if (err != NO_ERROR) {
398            return err;
399        }
400    }
401
402    // append data
403    memcpy(mData + mDataPos, data + offset, len);
404    mDataPos += len;
405    mDataSize += len;
406
407    if (numObjects > 0) {
408        // grow objects
409        if (mObjectsCapacity < mObjectsSize + numObjects) {
410            int newSize = ((mObjectsSize + numObjects)*3)/2;
411            size_t *objects =
412                (size_t*)realloc(mObjects, newSize*sizeof(size_t));
413            if (objects == (size_t*)0) {
414                return NO_MEMORY;
415            }
416            mObjects = objects;
417            mObjectsCapacity = newSize;
418        }
419
420        // append and acquire objects
421        int idx = mObjectsSize;
422        for (int i = firstIndex; i <= lastIndex; i++) {
423            size_t off = objects[i] - offset + startPos;
424            mObjects[idx++] = off;
425            mObjectsSize++;
426
427            flat_binder_object* flat
428                = reinterpret_cast<flat_binder_object*>(mData + off);
429            acquire_object(proc, *flat, this);
430
431            if (flat->type == BINDER_TYPE_FD) {
432                // If this is a file descriptor, we need to dup it so the
433                // new Parcel now owns its own fd, and can declare that we
434                // officially know we have fds.
435                flat->handle = dup(flat->handle);
436                flat->cookie = (void*)1;
437                mHasFds = mFdsKnown = true;
438            }
439        }
440    }
441
442    return NO_ERROR;
443}
444
445bool Parcel::hasFileDescriptors() const
446{
447    if (!mFdsKnown) {
448        scanForFds();
449    }
450    return mHasFds;
451}
452
453// Write RPC headers.  (previously just the interface token)
454status_t Parcel::writeInterfaceToken(const String16& interface)
455{
456    writeInt32(IPCThreadState::self()->getStrictModePolicy() |
457               STRICT_MODE_PENALTY_GATHER);
458    // currently the interface identification token is just its name as a string
459    return writeString16(interface);
460}
461
462bool Parcel::checkInterface(IBinder* binder) const
463{
464    return enforceInterface(binder->getInterfaceDescriptor());
465}
466
467bool Parcel::enforceInterface(const String16& interface,
468                              IPCThreadState* threadState) const
469{
470    int32_t strictPolicy = readInt32();
471    if (threadState == NULL) {
472        threadState = IPCThreadState::self();
473    }
474    if ((threadState->getLastTransactionBinderFlags() &
475         IBinder::FLAG_ONEWAY) != 0) {
476      // For one-way calls, the callee is running entirely
477      // disconnected from the caller, so disable StrictMode entirely.
478      // Not only does disk/network usage not impact the caller, but
479      // there's no way to commuicate back any violations anyway.
480      threadState->setStrictModePolicy(0);
481    } else {
482      threadState->setStrictModePolicy(strictPolicy);
483    }
484    const String16 str(readString16());
485    if (str == interface) {
486        return true;
487    } else {
488        LOGW("**** enforceInterface() expected '%s' but read '%s'\n",
489                String8(interface).string(), String8(str).string());
490        return false;
491    }
492}
493
494const size_t* Parcel::objects() const
495{
496    return mObjects;
497}
498
499size_t Parcel::objectsCount() const
500{
501    return mObjectsSize;
502}
503
504status_t Parcel::errorCheck() const
505{
506    return mError;
507}
508
509void Parcel::setError(status_t err)
510{
511    mError = err;
512}
513
514status_t Parcel::finishWrite(size_t len)
515{
516    //printf("Finish write of %d\n", len);
517    mDataPos += len;
518    LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
519    if (mDataPos > mDataSize) {
520        mDataSize = mDataPos;
521        LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
522    }
523    //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
524    return NO_ERROR;
525}
526
527status_t Parcel::writeUnpadded(const void* data, size_t len)
528{
529    size_t end = mDataPos + len;
530    if (end < mDataPos) {
531        // integer overflow
532        return BAD_VALUE;
533    }
534
535    if (end <= mDataCapacity) {
536restart_write:
537        memcpy(mData+mDataPos, data, len);
538        return finishWrite(len);
539    }
540
541    status_t err = growData(len);
542    if (err == NO_ERROR) goto restart_write;
543    return err;
544}
545
546status_t Parcel::write(const void* data, size_t len)
547{
548    void* const d = writeInplace(len);
549    if (d) {
550        memcpy(d, data, len);
551        return NO_ERROR;
552    }
553    return mError;
554}
555
556void* Parcel::writeInplace(size_t len)
557{
558    const size_t padded = PAD_SIZE(len);
559
560    // sanity check for integer overflow
561    if (mDataPos+padded < mDataPos) {
562        return NULL;
563    }
564
565    if ((mDataPos+padded) <= mDataCapacity) {
566restart_write:
567        //printf("Writing %ld bytes, padded to %ld\n", len, padded);
568        uint8_t* const data = mData+mDataPos;
569
570        // Need to pad at end?
571        if (padded != len) {
572#if BYTE_ORDER == BIG_ENDIAN
573            static const uint32_t mask[4] = {
574                0x00000000, 0xffffff00, 0xffff0000, 0xff000000
575            };
576#endif
577#if BYTE_ORDER == LITTLE_ENDIAN
578            static const uint32_t mask[4] = {
579                0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
580            };
581#endif
582            //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
583            //    *reinterpret_cast<void**>(data+padded-4));
584            *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
585        }
586
587        finishWrite(padded);
588        return data;
589    }
590
591    status_t err = growData(padded);
592    if (err == NO_ERROR) goto restart_write;
593    return NULL;
594}
595
596status_t Parcel::writeInt32(int32_t val)
597{
598    return writeAligned(val);
599}
600
601status_t Parcel::writeInt64(int64_t val)
602{
603    return writeAligned(val);
604}
605
606status_t Parcel::writeFloat(float val)
607{
608    return writeAligned(val);
609}
610
611status_t Parcel::writeDouble(double val)
612{
613    return writeAligned(val);
614}
615
616status_t Parcel::writeIntPtr(intptr_t val)
617{
618    return writeAligned(val);
619}
620
621status_t Parcel::writeCString(const char* str)
622{
623    return write(str, strlen(str)+1);
624}
625
626status_t Parcel::writeString8(const String8& str)
627{
628    status_t err = writeInt32(str.bytes());
629    // only write string if its length is more than zero characters,
630    // as readString8 will only read if the length field is non-zero.
631    // this is slightly different from how writeString16 works.
632    if (str.bytes() > 0 && err == NO_ERROR) {
633        err = write(str.string(), str.bytes()+1);
634    }
635    return err;
636}
637
638status_t Parcel::writeString16(const String16& str)
639{
640    return writeString16(str.string(), str.size());
641}
642
643status_t Parcel::writeString16(const char16_t* str, size_t len)
644{
645    if (str == NULL) return writeInt32(-1);
646
647    status_t err = writeInt32(len);
648    if (err == NO_ERROR) {
649        len *= sizeof(char16_t);
650        uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
651        if (data) {
652            memcpy(data, str, len);
653            *reinterpret_cast<char16_t*>(data+len) = 0;
654            return NO_ERROR;
655        }
656        err = mError;
657    }
658    return err;
659}
660
661status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
662{
663    return flatten_binder(ProcessState::self(), val, this);
664}
665
666status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
667{
668    return flatten_binder(ProcessState::self(), val, this);
669}
670
671status_t Parcel::writeNativeHandle(const native_handle* handle)
672{
673    if (!handle || handle->version != sizeof(native_handle))
674        return BAD_TYPE;
675
676    status_t err;
677    err = writeInt32(handle->numFds);
678    if (err != NO_ERROR) return err;
679
680    err = writeInt32(handle->numInts);
681    if (err != NO_ERROR) return err;
682
683    for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
684        err = writeDupFileDescriptor(handle->data[i]);
685
686    if (err != NO_ERROR) {
687        LOGD("write native handle, write dup fd failed");
688        return err;
689    }
690    err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
691    return err;
692}
693
694status_t Parcel::writeFileDescriptor(int fd)
695{
696    flat_binder_object obj;
697    obj.type = BINDER_TYPE_FD;
698    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
699    obj.handle = fd;
700    obj.cookie = (void*)0;
701    return writeObject(obj, true);
702}
703
704status_t Parcel::writeDupFileDescriptor(int fd)
705{
706    flat_binder_object obj;
707    obj.type = BINDER_TYPE_FD;
708    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
709    obj.handle = dup(fd);
710    obj.cookie = (void*)1;
711    return writeObject(obj, true);
712}
713
714status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
715{
716    if (len <= IN_PLACE_BLOB_LIMIT) {
717        LOGV("writeBlob: write in place");
718        void* ptr = writeInplace(len);
719        if (!ptr) return NO_MEMORY;
720
721        outBlob->init(false /*mapped*/, ptr, len);
722        return NO_ERROR;
723    }
724
725    LOGV("writeBlob: write to ashmem");
726    int fd = ashmem_create_region("Parcel Blob", len);
727    if (fd < 0) return NO_MEMORY;
728
729    status_t status;
730    int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
731    if (result < 0) {
732        status = -result;
733    } else {
734        void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
735        if (ptr == MAP_FAILED) {
736            status = -errno;
737        } else {
738            result = ashmem_set_prot_region(fd, PROT_READ);
739            if (result < 0) {
740                status = -result;
741            } else {
742                status = writeFileDescriptor(fd);
743                if (!status) {
744                    outBlob->init(true /*mapped*/, ptr, len);
745                    return NO_ERROR;
746                }
747            }
748        }
749        ::munmap(ptr, len);
750    }
751    ::close(fd);
752    return status;
753}
754
755status_t Parcel::write(const Flattenable& val)
756{
757    status_t err;
758
759    // size if needed
760    size_t len = val.getFlattenedSize();
761    size_t fd_count = val.getFdCount();
762
763    err = this->writeInt32(len);
764    if (err) return err;
765
766    err = this->writeInt32(fd_count);
767    if (err) return err;
768
769    // payload
770    void* buf = this->writeInplace(PAD_SIZE(len));
771    if (buf == NULL)
772        return BAD_VALUE;
773
774    int* fds = NULL;
775    if (fd_count) {
776        fds = new int[fd_count];
777    }
778
779    err = val.flatten(buf, len, fds, fd_count);
780    for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
781        err = this->writeDupFileDescriptor( fds[i] );
782    }
783
784    if (fd_count) {
785        delete [] fds;
786    }
787
788    return err;
789}
790
791status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
792{
793    const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
794    const bool enoughObjects = mObjectsSize < mObjectsCapacity;
795    if (enoughData && enoughObjects) {
796restart_write:
797        *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
798
799        // Need to write meta-data?
800        if (nullMetaData || val.binder != NULL) {
801            mObjects[mObjectsSize] = mDataPos;
802            acquire_object(ProcessState::self(), val, this);
803            mObjectsSize++;
804        }
805
806        // remember if it's a file descriptor
807        if (val.type == BINDER_TYPE_FD) {
808            mHasFds = mFdsKnown = true;
809        }
810
811        return finishWrite(sizeof(flat_binder_object));
812    }
813
814    if (!enoughData) {
815        const status_t err = growData(sizeof(val));
816        if (err != NO_ERROR) return err;
817    }
818    if (!enoughObjects) {
819        size_t newSize = ((mObjectsSize+2)*3)/2;
820        size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
821        if (objects == NULL) return NO_MEMORY;
822        mObjects = objects;
823        mObjectsCapacity = newSize;
824    }
825
826    goto restart_write;
827}
828
829status_t Parcel::writeNoException()
830{
831    return writeInt32(0);
832}
833
834void Parcel::remove(size_t start, size_t amt)
835{
836    LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
837}
838
839status_t Parcel::read(void* outData, size_t len) const
840{
841    if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
842        memcpy(outData, mData+mDataPos, len);
843        mDataPos += PAD_SIZE(len);
844        LOGV("read Setting data pos of %p to %d\n", this, mDataPos);
845        return NO_ERROR;
846    }
847    return NOT_ENOUGH_DATA;
848}
849
850const void* Parcel::readInplace(size_t len) const
851{
852    if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
853        const void* data = mData+mDataPos;
854        mDataPos += PAD_SIZE(len);
855        LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
856        return data;
857    }
858    return NULL;
859}
860
861template<class T>
862status_t Parcel::readAligned(T *pArg) const {
863    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
864
865    if ((mDataPos+sizeof(T)) <= mDataSize) {
866        const void* data = mData+mDataPos;
867        mDataPos += sizeof(T);
868        *pArg =  *reinterpret_cast<const T*>(data);
869        return NO_ERROR;
870    } else {
871        return NOT_ENOUGH_DATA;
872    }
873}
874
875template<class T>
876T Parcel::readAligned() const {
877    T result;
878    if (readAligned(&result) != NO_ERROR) {
879        result = 0;
880    }
881
882    return result;
883}
884
885template<class T>
886status_t Parcel::writeAligned(T val) {
887    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
888
889    if ((mDataPos+sizeof(val)) <= mDataCapacity) {
890restart_write:
891        *reinterpret_cast<T*>(mData+mDataPos) = val;
892        return finishWrite(sizeof(val));
893    }
894
895    status_t err = growData(sizeof(val));
896    if (err == NO_ERROR) goto restart_write;
897    return err;
898}
899
900status_t Parcel::readInt32(int32_t *pArg) const
901{
902    return readAligned(pArg);
903}
904
905int32_t Parcel::readInt32() const
906{
907    return readAligned<int32_t>();
908}
909
910
911status_t Parcel::readInt64(int64_t *pArg) const
912{
913    return readAligned(pArg);
914}
915
916
917int64_t Parcel::readInt64() const
918{
919    return readAligned<int64_t>();
920}
921
922status_t Parcel::readFloat(float *pArg) const
923{
924    return readAligned(pArg);
925}
926
927
928float Parcel::readFloat() const
929{
930    return readAligned<float>();
931}
932
933status_t Parcel::readDouble(double *pArg) const
934{
935    return readAligned(pArg);
936}
937
938
939double Parcel::readDouble() const
940{
941    return readAligned<double>();
942}
943
944status_t Parcel::readIntPtr(intptr_t *pArg) const
945{
946    return readAligned(pArg);
947}
948
949
950intptr_t Parcel::readIntPtr() const
951{
952    return readAligned<intptr_t>();
953}
954
955
956const char* Parcel::readCString() const
957{
958    const size_t avail = mDataSize-mDataPos;
959    if (avail > 0) {
960        const char* str = reinterpret_cast<const char*>(mData+mDataPos);
961        // is the string's trailing NUL within the parcel's valid bounds?
962        const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
963        if (eos) {
964            const size_t len = eos - str;
965            mDataPos += PAD_SIZE(len+1);
966            LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
967            return str;
968        }
969    }
970    return NULL;
971}
972
973String8 Parcel::readString8() const
974{
975    int32_t size = readInt32();
976    // watch for potential int overflow adding 1 for trailing NUL
977    if (size > 0 && size < INT32_MAX) {
978        const char* str = (const char*)readInplace(size+1);
979        if (str) return String8(str, size);
980    }
981    return String8();
982}
983
984String16 Parcel::readString16() const
985{
986    size_t len;
987    const char16_t* str = readString16Inplace(&len);
988    if (str) return String16(str, len);
989    LOGE("Reading a NULL string not supported here.");
990    return String16();
991}
992
993const char16_t* Parcel::readString16Inplace(size_t* outLen) const
994{
995    int32_t size = readInt32();
996    // watch for potential int overflow from size+1
997    if (size >= 0 && size < INT32_MAX) {
998        *outLen = size;
999        const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1000        if (str != NULL) {
1001            return str;
1002        }
1003    }
1004    *outLen = 0;
1005    return NULL;
1006}
1007
1008sp<IBinder> Parcel::readStrongBinder() const
1009{
1010    sp<IBinder> val;
1011    unflatten_binder(ProcessState::self(), *this, &val);
1012    return val;
1013}
1014
1015wp<IBinder> Parcel::readWeakBinder() const
1016{
1017    wp<IBinder> val;
1018    unflatten_binder(ProcessState::self(), *this, &val);
1019    return val;
1020}
1021
1022int32_t Parcel::readExceptionCode() const
1023{
1024  int32_t exception_code = readAligned<int32_t>();
1025  if (exception_code == EX_HAS_REPLY_HEADER) {
1026    int32_t header_size = readAligned<int32_t>();
1027    // Skip over fat responses headers.  Not used (or propagated) in
1028    // native code
1029    setDataPosition(dataPosition() + header_size);
1030    // And fat response headers are currently only used when there are no
1031    // exceptions, so return no error:
1032    return 0;
1033  }
1034  return exception_code;
1035}
1036
1037native_handle* Parcel::readNativeHandle() const
1038{
1039    int numFds, numInts;
1040    status_t err;
1041    err = readInt32(&numFds);
1042    if (err != NO_ERROR) return 0;
1043    err = readInt32(&numInts);
1044    if (err != NO_ERROR) return 0;
1045
1046    native_handle* h = native_handle_create(numFds, numInts);
1047    for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
1048        h->data[i] = dup(readFileDescriptor());
1049        if (h->data[i] < 0) err = BAD_VALUE;
1050    }
1051    err = read(h->data + numFds, sizeof(int)*numInts);
1052    if (err != NO_ERROR) {
1053        native_handle_close(h);
1054        native_handle_delete(h);
1055        h = 0;
1056    }
1057    return h;
1058}
1059
1060
1061int Parcel::readFileDescriptor() const
1062{
1063    const flat_binder_object* flat = readObject(true);
1064    if (flat) {
1065        switch (flat->type) {
1066            case BINDER_TYPE_FD:
1067                //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
1068                return flat->handle;
1069        }
1070    }
1071    return BAD_TYPE;
1072}
1073
1074status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1075{
1076    if (len <= IN_PLACE_BLOB_LIMIT) {
1077        LOGV("readBlob: read in place");
1078        const void* ptr = readInplace(len);
1079        if (!ptr) return BAD_VALUE;
1080
1081        outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1082        return NO_ERROR;
1083    }
1084
1085    LOGV("readBlob: read from ashmem");
1086    int fd = readFileDescriptor();
1087    if (fd == int(BAD_TYPE)) return BAD_VALUE;
1088
1089    void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1090    if (!ptr) return NO_MEMORY;
1091
1092    outBlob->init(true /*mapped*/, ptr, len);
1093    return NO_ERROR;
1094}
1095
1096status_t Parcel::read(Flattenable& val) const
1097{
1098    // size
1099    const size_t len = this->readInt32();
1100    const size_t fd_count = this->readInt32();
1101
1102    // payload
1103    void const* buf = this->readInplace(PAD_SIZE(len));
1104    if (buf == NULL)
1105        return BAD_VALUE;
1106
1107    int* fds = NULL;
1108    if (fd_count) {
1109        fds = new int[fd_count];
1110    }
1111
1112    status_t err = NO_ERROR;
1113    for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1114        fds[i] = dup(this->readFileDescriptor());
1115        if (fds[i] < 0) err = BAD_VALUE;
1116    }
1117
1118    if (err == NO_ERROR) {
1119        err = val.unflatten(buf, len, fds, fd_count);
1120    }
1121
1122    if (fd_count) {
1123        delete [] fds;
1124    }
1125
1126    return err;
1127}
1128const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1129{
1130    const size_t DPOS = mDataPos;
1131    if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1132        const flat_binder_object* obj
1133                = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1134        mDataPos = DPOS + sizeof(flat_binder_object);
1135        if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
1136            // When transferring a NULL object, we don't write it into
1137            // the object list, so we don't want to check for it when
1138            // reading.
1139            LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1140            return obj;
1141        }
1142
1143        // Ensure that this object is valid...
1144        size_t* const OBJS = mObjects;
1145        const size_t N = mObjectsSize;
1146        size_t opos = mNextObjectHint;
1147
1148        if (N > 0) {
1149            LOGV("Parcel %p looking for obj at %d, hint=%d\n",
1150                 this, DPOS, opos);
1151
1152            // Start at the current hint position, looking for an object at
1153            // the current data position.
1154            if (opos < N) {
1155                while (opos < (N-1) && OBJS[opos] < DPOS) {
1156                    opos++;
1157                }
1158            } else {
1159                opos = N-1;
1160            }
1161            if (OBJS[opos] == DPOS) {
1162                // Found it!
1163                LOGV("Parcel found obj %d at index %d with forward search",
1164                     this, DPOS, opos);
1165                mNextObjectHint = opos+1;
1166                LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1167                return obj;
1168            }
1169
1170            // Look backwards for it...
1171            while (opos > 0 && OBJS[opos] > DPOS) {
1172                opos--;
1173            }
1174            if (OBJS[opos] == DPOS) {
1175                // Found it!
1176                LOGV("Parcel found obj %d at index %d with backward search",
1177                     this, DPOS, opos);
1178                mNextObjectHint = opos+1;
1179                LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1180                return obj;
1181            }
1182        }
1183        LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
1184             this, DPOS);
1185    }
1186    return NULL;
1187}
1188
1189void Parcel::closeFileDescriptors()
1190{
1191    size_t i = mObjectsSize;
1192    if (i > 0) {
1193        //LOGI("Closing file descriptors for %d objects...", mObjectsSize);
1194    }
1195    while (i > 0) {
1196        i--;
1197        const flat_binder_object* flat
1198            = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1199        if (flat->type == BINDER_TYPE_FD) {
1200            //LOGI("Closing fd: %ld\n", flat->handle);
1201            close(flat->handle);
1202        }
1203    }
1204}
1205
1206const uint8_t* Parcel::ipcData() const
1207{
1208    return mData;
1209}
1210
1211size_t Parcel::ipcDataSize() const
1212{
1213    return (mDataSize > mDataPos ? mDataSize : mDataPos);
1214}
1215
1216const size_t* Parcel::ipcObjects() const
1217{
1218    return mObjects;
1219}
1220
1221size_t Parcel::ipcObjectsCount() const
1222{
1223    return mObjectsSize;
1224}
1225
1226void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1227    const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1228{
1229    freeDataNoInit();
1230    mError = NO_ERROR;
1231    mData = const_cast<uint8_t*>(data);
1232    mDataSize = mDataCapacity = dataSize;
1233    //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
1234    mDataPos = 0;
1235    LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
1236    mObjects = const_cast<size_t*>(objects);
1237    mObjectsSize = mObjectsCapacity = objectsCount;
1238    mNextObjectHint = 0;
1239    mOwner = relFunc;
1240    mOwnerCookie = relCookie;
1241    scanForFds();
1242}
1243
1244void Parcel::print(TextOutput& to, uint32_t flags) const
1245{
1246    to << "Parcel(";
1247
1248    if (errorCheck() != NO_ERROR) {
1249        const status_t err = errorCheck();
1250        to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1251    } else if (dataSize() > 0) {
1252        const uint8_t* DATA = data();
1253        to << indent << HexDump(DATA, dataSize()) << dedent;
1254        const size_t* OBJS = objects();
1255        const size_t N = objectsCount();
1256        for (size_t i=0; i<N; i++) {
1257            const flat_binder_object* flat
1258                = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1259            to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1260                << TypeCode(flat->type & 0x7f7f7f00)
1261                << " = " << flat->binder;
1262        }
1263    } else {
1264        to << "NULL";
1265    }
1266
1267    to << ")";
1268}
1269
1270void Parcel::releaseObjects()
1271{
1272    const sp<ProcessState> proc(ProcessState::self());
1273    size_t i = mObjectsSize;
1274    uint8_t* const data = mData;
1275    size_t* const objects = mObjects;
1276    while (i > 0) {
1277        i--;
1278        const flat_binder_object* flat
1279            = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1280        release_object(proc, *flat, this);
1281    }
1282}
1283
1284void Parcel::acquireObjects()
1285{
1286    const sp<ProcessState> proc(ProcessState::self());
1287    size_t i = mObjectsSize;
1288    uint8_t* const data = mData;
1289    size_t* const objects = mObjects;
1290    while (i > 0) {
1291        i--;
1292        const flat_binder_object* flat
1293            = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1294        acquire_object(proc, *flat, this);
1295    }
1296}
1297
1298void Parcel::freeData()
1299{
1300    freeDataNoInit();
1301    initState();
1302}
1303
1304void Parcel::freeDataNoInit()
1305{
1306    if (mOwner) {
1307        //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1308        mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1309    } else {
1310        releaseObjects();
1311        if (mData) free(mData);
1312        if (mObjects) free(mObjects);
1313    }
1314}
1315
1316status_t Parcel::growData(size_t len)
1317{
1318    size_t newSize = ((mDataSize+len)*3)/2;
1319    return (newSize <= mDataSize)
1320            ? (status_t) NO_MEMORY
1321            : continueWrite(newSize);
1322}
1323
1324status_t Parcel::restartWrite(size_t desired)
1325{
1326    if (mOwner) {
1327        freeData();
1328        return continueWrite(desired);
1329    }
1330
1331    uint8_t* data = (uint8_t*)realloc(mData, desired);
1332    if (!data && desired > mDataCapacity) {
1333        mError = NO_MEMORY;
1334        return NO_MEMORY;
1335    }
1336
1337    releaseObjects();
1338
1339    if (data) {
1340        mData = data;
1341        mDataCapacity = desired;
1342    }
1343
1344    mDataSize = mDataPos = 0;
1345    LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1346    LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
1347
1348    free(mObjects);
1349    mObjects = NULL;
1350    mObjectsSize = mObjectsCapacity = 0;
1351    mNextObjectHint = 0;
1352    mHasFds = false;
1353    mFdsKnown = true;
1354
1355    return NO_ERROR;
1356}
1357
1358status_t Parcel::continueWrite(size_t desired)
1359{
1360    // If shrinking, first adjust for any objects that appear
1361    // after the new data size.
1362    size_t objectsSize = mObjectsSize;
1363    if (desired < mDataSize) {
1364        if (desired == 0) {
1365            objectsSize = 0;
1366        } else {
1367            while (objectsSize > 0) {
1368                if (mObjects[objectsSize-1] < desired)
1369                    break;
1370                objectsSize--;
1371            }
1372        }
1373    }
1374
1375    if (mOwner) {
1376        // If the size is going to zero, just release the owner's data.
1377        if (desired == 0) {
1378            freeData();
1379            return NO_ERROR;
1380        }
1381
1382        // If there is a different owner, we need to take
1383        // posession.
1384        uint8_t* data = (uint8_t*)malloc(desired);
1385        if (!data) {
1386            mError = NO_MEMORY;
1387            return NO_MEMORY;
1388        }
1389        size_t* objects = NULL;
1390
1391        if (objectsSize) {
1392            objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1393            if (!objects) {
1394                mError = NO_MEMORY;
1395                return NO_MEMORY;
1396            }
1397
1398            // Little hack to only acquire references on objects
1399            // we will be keeping.
1400            size_t oldObjectsSize = mObjectsSize;
1401            mObjectsSize = objectsSize;
1402            acquireObjects();
1403            mObjectsSize = oldObjectsSize;
1404        }
1405
1406        if (mData) {
1407            memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1408        }
1409        if (objects && mObjects) {
1410            memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1411        }
1412        //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1413        mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1414        mOwner = NULL;
1415
1416        mData = data;
1417        mObjects = objects;
1418        mDataSize = (mDataSize < desired) ? mDataSize : desired;
1419        LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1420        mDataCapacity = desired;
1421        mObjectsSize = mObjectsCapacity = objectsSize;
1422        mNextObjectHint = 0;
1423
1424    } else if (mData) {
1425        if (objectsSize < mObjectsSize) {
1426            // Need to release refs on any objects we are dropping.
1427            const sp<ProcessState> proc(ProcessState::self());
1428            for (size_t i=objectsSize; i<mObjectsSize; i++) {
1429                const flat_binder_object* flat
1430                    = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1431                if (flat->type == BINDER_TYPE_FD) {
1432                    // will need to rescan because we may have lopped off the only FDs
1433                    mFdsKnown = false;
1434                }
1435                release_object(proc, *flat, this);
1436            }
1437            size_t* objects =
1438                (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1439            if (objects) {
1440                mObjects = objects;
1441            }
1442            mObjectsSize = objectsSize;
1443            mNextObjectHint = 0;
1444        }
1445
1446        // We own the data, so we can just do a realloc().
1447        if (desired > mDataCapacity) {
1448            uint8_t* data = (uint8_t*)realloc(mData, desired);
1449            if (data) {
1450                mData = data;
1451                mDataCapacity = desired;
1452            } else if (desired > mDataCapacity) {
1453                mError = NO_MEMORY;
1454                return NO_MEMORY;
1455            }
1456        } else {
1457            if (mDataSize > desired) {
1458                mDataSize = desired;
1459                LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1460            }
1461            if (mDataPos > desired) {
1462                mDataPos = desired;
1463                LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1464            }
1465        }
1466
1467    } else {
1468        // This is the first data.  Easy!
1469        uint8_t* data = (uint8_t*)malloc(desired);
1470        if (!data) {
1471            mError = NO_MEMORY;
1472            return NO_MEMORY;
1473        }
1474
1475        if(!(mDataCapacity == 0 && mObjects == NULL
1476             && mObjectsCapacity == 0)) {
1477            LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
1478        }
1479
1480        mData = data;
1481        mDataSize = mDataPos = 0;
1482        LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1483        LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1484        mDataCapacity = desired;
1485    }
1486
1487    return NO_ERROR;
1488}
1489
1490void Parcel::initState()
1491{
1492    mError = NO_ERROR;
1493    mData = 0;
1494    mDataSize = 0;
1495    mDataCapacity = 0;
1496    mDataPos = 0;
1497    LOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1498    LOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
1499    mObjects = NULL;
1500    mObjectsSize = 0;
1501    mObjectsCapacity = 0;
1502    mNextObjectHint = 0;
1503    mHasFds = false;
1504    mFdsKnown = true;
1505    mOwner = NULL;
1506}
1507
1508void Parcel::scanForFds() const
1509{
1510    bool hasFds = false;
1511    for (size_t i=0; i<mObjectsSize; i++) {
1512        const flat_binder_object* flat
1513            = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1514        if (flat->type == BINDER_TYPE_FD) {
1515            hasFds = true;
1516            break;
1517        }
1518    }
1519    mHasFds = hasFds;
1520    mFdsKnown = true;
1521}
1522
1523// --- Parcel::Blob ---
1524
1525Parcel::Blob::Blob() :
1526        mMapped(false), mData(NULL), mSize(0) {
1527}
1528
1529Parcel::Blob::~Blob() {
1530    release();
1531}
1532
1533void Parcel::Blob::release() {
1534    if (mMapped && mData) {
1535        ::munmap(mData, mSize);
1536    }
1537    clear();
1538}
1539
1540void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1541    mMapped = mapped;
1542    mData = data;
1543    mSize = size;
1544}
1545
1546void Parcel::Blob::clear() {
1547    mMapped = false;
1548    mData = NULL;
1549    mSize = 0;
1550}
1551
1552}; // namespace android
1553