17c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project/*
27c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * Copyright (C) 2005 The Android Open Source Project
37c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project *
47c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
57c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * you may not use this file except in compliance with the License.
67c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * You may obtain a copy of the License at
77c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project *
87c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
97c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project *
107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
117c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
127c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * See the License for the specific language governing permissions and
147c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project * limitations under the License.
157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project */
167c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
177c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#define LOG_TAG "Parcel"
187c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project//#define LOG_NDEBUG 0
197c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
20abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <errno.h>
2170f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn#include <fcntl.h>
22abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <inttypes.h>
2370f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn#include <pthread.h>
24abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <stdint.h>
25abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <stdio.h>
26abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <stdlib.h>
27abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <sys/mman.h>
28eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn#include <sys/stat.h>
29eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn#include <sys/types.h>
30e4e0ae8abc7f612960fc381a792d0bef4c3eb6f9Christopher Tate#include <sys/resource.h>
31eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn#include <unistd.h>
327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
33c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/Binder.h>
34c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/BpBinder.h>
35abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <binder/IPCThreadState.h>
36abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <binder/Parcel.h>
37c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/ProcessState.h>
3809eb749704afd9e226e1347cb20c90be2016cd21Christopher Wiley#include <binder/Status.h>
39002e1e58dfe19dd3e49a59c6827cbf51573941a2Mathias Agopian#include <binder/TextOutput.h>
40002e1e58dfe19dd3e49a59c6827cbf51573941a2Mathias Agopian
41abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <cutils/ashmem.h>
42002e1e58dfe19dd3e49a59c6827cbf51573941a2Mathias Agopian#include <utils/Debug.h>
43abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <utils/Flattenable.h>
447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#include <utils/Log.h>
45abed7f7aebdbdd20fdb37d0d6e79e50905c7f5d5Mark Salyzyn#include <utils/misc.h>
467c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#include <utils/String8.h>
477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#include <utils/String16.h>
487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
49208059f67ed2dd9fa025e07fcb6954d3cb61c79eMathias Agopian#include <private/binder/binder_module.h>
507e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn#include <private/binder/Static.h>
517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#ifndef INT32_MAX
537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#define INT32_MAX ((int32_t)(2147483647))
547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#endif
557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#define LOG_REFS(...)
57e93390b0bd336cd1b69abebf6eeffd18881f531bMark Salyzyn//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
587e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn#define LOG_ALLOC(...)
59e93390b0bd336cd1b69abebf6eeffd18881f531bMark Salyzyn//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
607c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project// ---------------------------------------------------------------------------
627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
63b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich// This macro should never be used at runtime, as a too large value
64b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich// of s could cause an integer overflow. Instead, you should always
65b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich// use the wrapper function pad_size()
66b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
68b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevichstatic size_t pad_size(size_t s) {
69b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (s > (SIZE_T_MAX - 3)) {
70b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        abort();
71b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
72b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    return PAD_SIZE_UNSAFE(s);
73b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich}
747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
75a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
760c1f5cbb8a41d19195ab49dcfeb5caaea2d3fcdaJeff Sharkey#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
77a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick
787c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project// XXX This can be made public if we want to provide
797c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project// support for typed data.
807c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstruct small_flat_data
817c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
827c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    uint32_t type;
837c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    uint32_t data;
847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project};
857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectnamespace android {
877c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
88a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackbornstatic pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
89a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackbornstatic size_t gParcelGlobalAllocSize = 0;
90a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackbornstatic size_t gParcelGlobalAllocCount = 0;
91a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn
92e4e0ae8abc7f612960fc381a792d0bef4c3eb6f9Christopher Tatestatic size_t gMaxFds = 0;
93e4e0ae8abc7f612960fc381a792d0bef4c3eb6f9Christopher Tate
9413b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown// Maximum size of a blob to transfer in-place.
9513b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brownstatic const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
9613b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown
9713b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brownenum {
9813b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    BLOB_INPLACE = 0,
9913b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    BLOB_ASHMEM_IMMUTABLE = 1,
10013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    BLOB_ASHMEM_MUTABLE = 2,
10113b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown};
10213b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown
10370f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzynstatic dev_t ashmem_rdev()
10470f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn{
10570f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    static dev_t __ashmem_rdev;
10670f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    static pthread_mutex_t __ashmem_rdev_lock = PTHREAD_MUTEX_INITIALIZER;
10770f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
10870f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    pthread_mutex_lock(&__ashmem_rdev_lock);
10970f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
11070f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    dev_t rdev = __ashmem_rdev;
11170f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    if (!rdev) {
11270f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn        int fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDONLY));
11370f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn        if (fd >= 0) {
11470f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn            struct stat st;
11570f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
11670f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn            int ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
11770f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn            close(fd);
11870f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn            if ((ret >= 0) && S_ISCHR(st.st_mode)) {
11970f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn                rdev = __ashmem_rdev = st.st_rdev;
12070f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn            }
12170f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn        }
12270f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    }
12370f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
12470f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    pthread_mutex_unlock(&__ashmem_rdev_lock);
12570f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
12670f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn    return rdev;
12770f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn}
12870f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn
1297c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectvoid acquire_object(const sp<ProcessState>& proc,
130cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos    const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
1317c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
1327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    switch (obj.type) {
1337c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_BINDER:
1347c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (obj.binder) {
1357c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
13684e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
1377c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
1387c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1397c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_WEAK_BINDER:
1407c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (obj.binder)
14184e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
1427c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_HANDLE: {
1447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
1457c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (b != NULL) {
1467c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
1477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                b->incStrong(who);
1487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
1497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
1517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_WEAK_HANDLE: {
1527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
1537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (b != NULL) b.get_refs()->incWeak(who);
1547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
1567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_FD: {
157eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn            if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
158eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                struct stat st;
159eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                int ret = fstat(obj.handle, &st);
16070f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn                if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
1616bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                    // If we own an ashmem fd, keep track of how much memory it refers to.
1626bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                    int size = ashmem_get_size_region(obj.handle);
1636bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                    if (size > 0) {
1646bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                        *outAshmemSize += size;
1656bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                    }
166cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos                }
167cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos            }
1687c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
1707c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
1717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
1726f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Cross    ALOGD("Invalid object type 0x%08x", obj.type);
1737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
1747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
1756bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roosvoid acquire_object(const sp<ProcessState>& proc,
1767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const flat_binder_object& obj, const void* who)
1777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
1786bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos    acquire_object(proc, obj, who, NULL);
1796bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos}
1806bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos
1816bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roosstatic void release_object(const sp<ProcessState>& proc,
182cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos    const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
1837c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
1847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    switch (obj.type) {
1857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_BINDER:
1867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (obj.binder) {
1877c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
18884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
1897c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
1907c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1917c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_WEAK_BINDER:
1927c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (obj.binder)
19384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
1947c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
1957c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_HANDLE: {
1967c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
1977c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (b != NULL) {
1987c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
1997c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                b->decStrong(who);
2007c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
2017c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
2027c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
2037c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_WEAK_HANDLE: {
2047c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
2057c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (b != NULL) b.get_refs()->decWeak(who);
2067c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
2077c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
2087c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        case BINDER_TYPE_FD: {
209b454d8ffa928e33dd54c979a747994acbd117397Mark Salyzyn            if (obj.cookie != 0) { // owned
210b454d8ffa928e33dd54c979a747994acbd117397Mark Salyzyn                if (outAshmemSize != NULL) {
211eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                    struct stat st;
212eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                    int ret = fstat(obj.handle, &st);
21370f3665f8272b2f6d05e77ff0c17683b4b7f82b7Mark Salyzyn                    if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
214eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                        int size = ashmem_get_size_region(obj.handle);
215eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                        if (size > 0) {
216eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                            *outAshmemSize -= size;
217eab2afc7ac5183750a23693ecee89dad7f7abf5fMark Salyzyn                        }
2186bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                    }
2196bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos                }
220b454d8ffa928e33dd54c979a747994acbd117397Mark Salyzyn
221b454d8ffa928e33dd54c979a747994acbd117397Mark Salyzyn                close(obj.handle);
222cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos            }
2237c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return;
2247c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
2257c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
2267c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
2276f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Cross    ALOGE("Invalid object type 0x%08x", obj.type);
2287c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
2297c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
2306bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roosvoid release_object(const sp<ProcessState>& proc,
2316bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos    const flat_binder_object& obj, const void* who)
2326bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos{
2336bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos    release_object(proc, obj, who, NULL);
2346bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos}
2356bb3114246f6f6aa406e65452dbaa12b135029eaAdrian Roos
2367c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectinline static status_t finish_flatten_binder(
2376f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Cross    const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
2387c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
2397c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return out->writeObject(flat, false);
2407c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
2417c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
2426f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Crossstatus_t flatten_binder(const sp<ProcessState>& /*proc*/,
2437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const sp<IBinder>& binder, Parcel* out)
2447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
2457c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    flat_binder_object obj;
246d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
2477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
2487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (binder != NULL) {
2497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        IBinder *local = binder->localBinder();
2507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        if (!local) {
2517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            BpBinder *proxy = binder->remoteBinder();
2527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (proxy == NULL) {
253e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                ALOGE("null proxy");
2547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
2557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            const int32_t handle = proxy ? proxy->handle() : 0;
2567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            obj.type = BINDER_TYPE_HANDLE;
25707fd0f195db6d341cab4e54257f508d802c98832Arve Hjønnevåg            obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
2587c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            obj.handle = handle;
25984e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg            obj.cookie = 0;
2607c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        } else {
2617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            obj.type = BINDER_TYPE_BINDER;
26284e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg            obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
26384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg            obj.cookie = reinterpret_cast<uintptr_t>(local);
2647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
2657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    } else {
2667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        obj.type = BINDER_TYPE_BINDER;
26784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.binder = 0;
26884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.cookie = 0;
2697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
270d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
2717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return finish_flatten_binder(binder, obj, out);
2727c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
2737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
2746f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Crossstatus_t flatten_binder(const sp<ProcessState>& /*proc*/,
2757c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const wp<IBinder>& binder, Parcel* out)
2767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
2777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    flat_binder_object obj;
278d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
2797c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
2807c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (binder != NULL) {
2817c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        sp<IBinder> real = binder.promote();
2827c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        if (real != NULL) {
2837c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            IBinder *local = real->localBinder();
2847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (!local) {
2857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                BpBinder *proxy = real->remoteBinder();
2867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                if (proxy == NULL) {
287e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                    ALOGE("null proxy");
2887c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                }
2897c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                const int32_t handle = proxy ? proxy->handle() : 0;
2907c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                obj.type = BINDER_TYPE_WEAK_HANDLE;
29107fd0f195db6d341cab4e54257f508d802c98832Arve Hjønnevåg                obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
2927c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                obj.handle = handle;
29384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                obj.cookie = 0;
2947c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            } else {
2957c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                obj.type = BINDER_TYPE_WEAK_BINDER;
29684e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
29784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
2987c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
2997c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            return finish_flatten_binder(real, obj, out);
3007c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
301d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
3027c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // XXX How to deal?  In order to flatten the given binder,
3037c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // we need to probe it for information, which requires a primary
3047c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // reference...  but we don't have one.
3057c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        //
3067c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // The OpenBinder implementation uses a dynamic_cast<> here,
3077c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // but we can't do that with the different reference counting
3087c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // implementation we are using.
309e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("Unable to unflatten Binder weak reference!");
3107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        obj.type = BINDER_TYPE_BINDER;
31184e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.binder = 0;
31284e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.cookie = 0;
3137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return finish_flatten_binder(NULL, obj, out);
314d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
3157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    } else {
3167c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        obj.type = BINDER_TYPE_BINDER;
31784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.binder = 0;
31884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg        obj.cookie = 0;
3197c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return finish_flatten_binder(NULL, obj, out);
3207c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
3217c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
3227c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
3237c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectinline static status_t finish_unflatten_binder(
3246f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Cross    BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
3256f4f3ab36c5ed1df84eb3a9f7475f0ac42952f58Colin Cross    const Parcel& /*in*/)
3267c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
3277c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return NO_ERROR;
3287c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
329d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
3307c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t unflatten_binder(const sp<ProcessState>& proc,
3317c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const Parcel& in, sp<IBinder>* out)
3327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
3337c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const flat_binder_object* flat = in.readObject(false);
334d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
3357c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (flat) {
3367c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        switch (flat->type) {
3377c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_BINDER:
33884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                *out = reinterpret_cast<IBinder*>(flat->cookie);
3397c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return finish_unflatten_binder(NULL, *flat, in);
3407c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_HANDLE:
3417c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                *out = proc->getStrongProxyForHandle(flat->handle);
3427c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return finish_unflatten_binder(
3437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                    static_cast<BpBinder*>(out->get()), *flat, in);
344d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn        }
3457c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
3467c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return BAD_TYPE;
3477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
3487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
3497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t unflatten_binder(const sp<ProcessState>& proc,
3507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const Parcel& in, wp<IBinder>* out)
3517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
3527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const flat_binder_object* flat = in.readObject(false);
353d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
3547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (flat) {
3557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        switch (flat->type) {
3567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_BINDER:
35784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                *out = reinterpret_cast<IBinder*>(flat->cookie);
3587c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return finish_unflatten_binder(NULL, *flat, in);
3597c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_WEAK_BINDER:
36084e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                if (flat->binder != 0) {
3617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                    out->set_object_and_refs(
36284e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                        reinterpret_cast<IBinder*>(flat->cookie),
36384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                        reinterpret_cast<RefBase::weakref_type*>(flat->binder));
3647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                } else {
3657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                    *out = NULL;
3667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                }
3677c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return finish_unflatten_binder(NULL, *flat, in);
3687c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_HANDLE:
3697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            case BINDER_TYPE_WEAK_HANDLE:
3707c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                *out = proc->getWeakProxyForHandle(flat->handle);
3717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return finish_unflatten_binder(
3727c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                    static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
3737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
3747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
3757c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return BAD_TYPE;
3767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
3777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
3787c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project// ---------------------------------------------------------------------------
3797c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
3807c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source ProjectParcel::Parcel()
3817c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
3827e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn    LOG_ALLOC("Parcel %p: constructing", this);
3837c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    initState();
3847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
3857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
3867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source ProjectParcel::~Parcel()
3877c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
3887c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    freeDataNoInit();
3897e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn    LOG_ALLOC("Parcel %p: destroyed", this);
3907e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn}
3917e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn
3927e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackbornsize_t Parcel::getGlobalAllocSize() {
393a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
394a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    size_t size = gParcelGlobalAllocSize;
395a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
396a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    return size;
3977e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn}
3987e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackborn
3997e790af756d7891cb1cf1374bb283a8fbcbe6b49Dianne Hackbornsize_t Parcel::getGlobalAllocCount() {
400a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
401a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    size_t count = gParcelGlobalAllocCount;
402a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
403a4cff88d80bf1acbdbd063af88ecad83a8b9b7b4Dianne Hackborn    return count;
4047c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4057c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4067c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectconst uint8_t* Parcel::data() const
4077c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
4087c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mData;
4097c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4117c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectsize_t Parcel::dataSize() const
4127c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
4137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return (mDataSize > mDataPos ? mDataSize : mDataPos);
4147c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4167c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectsize_t Parcel::dataAvail() const
4177c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
418cfe27deac7e0ede89a78b9f03fb4a4159d68be8eNick Kralevich    size_t result = dataSize() - dataPosition();
419cfe27deac7e0ede89a78b9f03fb4a4159d68be8eNick Kralevich    if (result > INT32_MAX) {
420cfe27deac7e0ede89a78b9f03fb4a4159d68be8eNick Kralevich        abort();
421cfe27deac7e0ede89a78b9f03fb4a4159d68be8eNick Kralevich    }
422cfe27deac7e0ede89a78b9f03fb4a4159d68be8eNick Kralevich    return result;
4237c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4247c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4257c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectsize_t Parcel::dataPosition() const
4267c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
4277c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mDataPos;
4287c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4297c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4307c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectsize_t Parcel::dataCapacity() const
4317c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
4327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mDataCapacity;
4337c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4347c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4357c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::setDataSize(size_t size)
4367c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
437b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (size > INT32_MAX) {
438b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
439b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
440b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
441b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
442b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
4437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err;
4447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    err = continueWrite(size);
4457c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (err == NO_ERROR) {
4467c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        mDataSize = size;
447d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn        ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
4487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
4497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return err;
4507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectvoid Parcel::setDataPosition(size_t pos) const
4537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
454b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (pos > INT32_MAX) {
455b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
456b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
457b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        abort();
458b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
459b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
4607c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mDataPos = pos;
4617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mNextObjectHint = 0;
4627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4637c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::setDataCapacity(size_t size)
4657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
466b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (size > INT32_MAX) {
467b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
468b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
469b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
470b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
471b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
47297e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn    if (size > mDataCapacity) return continueWrite(size);
4737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return NO_ERROR;
4747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4757c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
4767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::setData(const uint8_t* buffer, size_t len)
4777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
478b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
479b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
480b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
481b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
482b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
483b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
4847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err = restartWrite(len);
4857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (err == NO_ERROR) {
4867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        memcpy(const_cast<uint8_t*>(data()), buffer, len);
4877c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        mDataSize = len;
4887c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        mFdsKnown = false;
4897c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
4907c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return err;
4917c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
4927c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
49351faf46e2958c57dab46d9420e585319125c7c42Andreas Huberstatus_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
4947c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
4957c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    const sp<ProcessState> proc(ProcessState::self());
4967c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err;
49751faf46e2958c57dab46d9420e585319125c7c42Andreas Huber    const uint8_t *data = parcel->mData;
49884e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    const binder_size_t *objects = parcel->mObjects;
4997c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    size_t size = parcel->mObjectsSize;
5007c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    int startPos = mDataPos;
5017c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    int firstIndex = -1, lastIndex = -2;
5027c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5037c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (len == 0) {
5047c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return NO_ERROR;
5057c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
5067c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
507b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
508b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
509b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
510b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
511b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
512b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
5137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    // range checks against the source parcel size
5147c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if ((offset > parcel->mDataSize)
5157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            || (len > parcel->mDataSize)
5167c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            || (offset + len > parcel->mDataSize)) {
5177c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return BAD_VALUE;
5187c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
5197c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5207c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    // Count objects in range
5217c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    for (int i = 0; i < (int) size; i++) {
5227c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        size_t off = objects[i];
52327182be9f20f4f5b48316666429f09b9ecc1f22eChristopher Tate        if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
5247c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (firstIndex == -1) {
5257c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                firstIndex = i;
5267c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
5277c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            lastIndex = i;
5287c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
5297c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
5307c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    int numObjects = lastIndex - firstIndex + 1;
5317c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
53297e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn    if ((mDataSize+len) > mDataCapacity) {
53397e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn        // grow data
53497e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn        err = growData(len);
53597e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn        if (err != NO_ERROR) {
53697e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn            return err;
53797e2bcd76021b31c5064fd3f669bc3f450b4c952Dianne Hackborn        }
5387c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
5397c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5407c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    // append data
5417c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    memcpy(mData + mDataPos, data + offset, len);
5427c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mDataPos += len;
5437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mDataSize += len;
5447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5458938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn    err = NO_ERROR;
5468938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn
5477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (numObjects > 0) {
5487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // grow objects
5497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        if (mObjectsCapacity < mObjectsSize + numObjects) {
550ed7a50cc7d490ae7aece2d16422c5f7941876468Christopher Tate            size_t newSize = ((mObjectsSize + numObjects)*3)/2;
551ed7a50cc7d490ae7aece2d16422c5f7941876468Christopher Tate            if (newSize < mObjectsSize) return NO_MEMORY;   // overflow
55284e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg            binder_size_t *objects =
55384e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
55484e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg            if (objects == (binder_size_t*)0) {
5557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                return NO_MEMORY;
5567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
5577c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            mObjects = objects;
5587c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            mObjectsCapacity = newSize;
5597c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
560d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn
5617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // append and acquire objects
5627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        int idx = mObjectsSize;
5637c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        for (int i = firstIndex; i <= lastIndex; i++) {
5647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            size_t off = objects[i] - offset + startPos;
5657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            mObjects[idx++] = off;
5667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            mObjectsSize++;
5677c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5688af0f82d89b029edf7e104a36a9a6e48a92f9e93Dianne Hackborn            flat_binder_object* flat
5697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                = reinterpret_cast<flat_binder_object*>(mData + off);
570cbf3726357966539c2a685f46e61c3fc8937f19eAdrian Roos            acquire_object(proc, *flat, this, &mOpenAshmemSize);
5717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5727c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            if (flat->type == BINDER_TYPE_FD) {
5738af0f82d89b029edf7e104a36a9a6e48a92f9e93Dianne Hackborn                // If this is a file descriptor, we need to dup it so the
5748af0f82d89b029edf7e104a36a9a6e48a92f9e93Dianne Hackborn                // new Parcel now owns its own fd, and can declare that we
5758af0f82d89b029edf7e104a36a9a6e48a92f9e93Dianne Hackborn                // officially know we have fds.
5768af0f82d89b029edf7e104a36a9a6e48a92f9e93Dianne Hackborn                flat->handle = dup(flat->handle);
57784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                flat->cookie = 1;
5787c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                mHasFds = mFdsKnown = true;
5798938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn                if (!mAllowFds) {
5808938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn                    err = FDS_NOT_ALLOWED;
5818938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn                }
5827c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            }
5837c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
5847c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
5857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
5868938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn    return err;
5878938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn}
5888938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn
58913b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brownbool Parcel::allowFds() const
59013b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown{
59113b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown    return mAllowFds;
59213b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown}
59313b1604018968408bcc5553e1fa5ea9df3e4e009Jeff Brown
5947746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackbornbool Parcel::pushAllowFds(bool allowFds)
5958938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn{
5968938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn    const bool origValue = mAllowFds;
5977746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn    if (!allowFds) {
5987746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn        mAllowFds = false;
5997746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn    }
6008938ed2c8e906fc332301b64787728d4d34da571Dianne Hackborn    return origValue;
6017c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6027c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6037746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackbornvoid Parcel::restoreAllowFds(bool lastValue)
6047746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn{
6057746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn    mAllowFds = lastValue;
6067746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn}
6077746cc318829358bb2c4dc0a03d40a7856a6d13eDianne Hackborn
6087c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectbool Parcel::hasFileDescriptors() const
6097c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
6107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (!mFdsKnown) {
6117c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        scanForFds();
6127c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
6137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mHasFds;
6147c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
616702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick// Write RPC headers.  (previously just the interface token)
6177c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeInterfaceToken(const String16& interface)
6187c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
619a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick    writeInt32(IPCThreadState::self()->getStrictModePolicy() |
620a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick               STRICT_MODE_PENALTY_GATHER);
6217c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    // currently the interface identification token is just its name as a string
6227c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return writeString16(interface);
6237c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6247c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
62583c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopianbool Parcel::checkInterface(IBinder* binder) const
62683c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopian{
627702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick    return enforceInterface(binder->getInterfaceDescriptor());
62883c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopian}
62983c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopian
630a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrickbool Parcel::enforceInterface(const String16& interface,
63170081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick                              IPCThreadState* threadState) const
6327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
63370081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    int32_t strictPolicy = readInt32();
63470081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick    if (threadState == NULL) {
63570081a1511955f53bd0ed7f2aec9a1bf09f0f379Brad Fitzpatrick        threadState = IPCThreadState::self();
636a877cd85b5a026384542e3271fc310d6a8fe24c6Brad Fitzpatrick    }
6375273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick    if ((threadState->getLastTransactionBinderFlags() &
6385273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick         IBinder::FLAG_ONEWAY) != 0) {
6395273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      // For one-way calls, the callee is running entirely
6405273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      // disconnected from the caller, so disable StrictMode entirely.
6415273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      // Not only does disk/network usage not impact the caller, but
6425273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      // there's no way to commuicate back any violations anyway.
6435273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      threadState->setStrictModePolicy(0);
6445273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick    } else {
6455273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick      threadState->setStrictModePolicy(strictPolicy);
6465273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick    }
64783c0446f27b9542d6c2e724817b2b2d8d1f55085Mathias Agopian    const String16 str(readString16());
6487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (str == interface) {
6497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return true;
6507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    } else {
651d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn        ALOGW("**** enforceInterface() expected '%s' but read '%s'",
6527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                String8(interface).string(), String8(str).string());
6537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return false;
6547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
655702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick}
6567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
65784e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevågconst binder_size_t* Parcel::objects() const
6587c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
6597c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mObjects;
6607c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectsize_t Parcel::objectsCount() const
6637c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
6647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mObjectsSize;
6657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6677c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::errorCheck() const
6687c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
6697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mError;
6707c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6727c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectvoid Parcel::setError(status_t err)
6737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
6747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mError = err;
6757c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::finishWrite(size_t len)
6787c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
679b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
680b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
681b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
682b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
683b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
684b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
6857c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    //printf("Finish write of %d\n", len);
6867c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    mDataPos += len;
687d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn    ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
6887c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (mDataPos > mDataSize) {
6897c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        mDataSize = mDataPos;
690d4ecccfd206604a9b2d8a1220844fc2df85a5998Mark Salyzyn        ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
6917c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
6927c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
6937c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return NO_ERROR;
6947c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
6957c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
6967c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeUnpadded(const void* data, size_t len)
6977c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
698b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
699b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
700b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
701b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
702b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
703b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
7047c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    size_t end = mDataPos + len;
7057c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (end < mDataPos) {
7067c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // integer overflow
7077c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return BAD_VALUE;
7087c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
7097c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (end <= mDataCapacity) {
7117c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectrestart_write:
7127c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        memcpy(mData+mDataPos, data, len);
7137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return finishWrite(len);
7147c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
7157c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7167c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err = growData(len);
7177c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (err == NO_ERROR) goto restart_write;
7187c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return err;
7197c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
7207c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7217c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::write(const void* data, size_t len)
7227c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
723b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
724b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
725b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
726b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
727b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
728b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
7297c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    void* const d = writeInplace(len);
7307c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (d) {
7317c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        memcpy(d, data, len);
7327c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return NO_ERROR;
7337c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
7347c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return mError;
7357c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
7367c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7377c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectvoid* Parcel::writeInplace(size_t len)
7387c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
739b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
740b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
741b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
742b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return NULL;
743b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
744b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
745b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    const size_t padded = pad_size(len);
7467c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    // sanity check for integer overflow
7487c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (mDataPos+padded < mDataPos) {
7497c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return NULL;
7507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
7517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if ((mDataPos+padded) <= mDataCapacity) {
7537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectrestart_write:
7547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        //printf("Writing %ld bytes, padded to %ld\n", len, padded);
7557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        uint8_t* const data = mData+mDataPos;
7567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7577c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        // Need to pad at end?
7587c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        if (padded != len) {
7597c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#if BYTE_ORDER == BIG_ENDIAN
7607c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            static const uint32_t mask[4] = {
7617c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                0x00000000, 0xffffff00, 0xffff0000, 0xff000000
7627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            };
7637c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#endif
7647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#if BYTE_ORDER == LITTLE_ENDIAN
7657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            static const uint32_t mask[4] = {
7667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project                0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
7677c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            };
7687c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project#endif
7697c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
7707c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            //    *reinterpret_cast<void**>(data+padded-4));
7717c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project            *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
7727c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        }
7737c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7747c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        finishWrite(padded);
7757c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        return data;
7767c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
7777c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7787c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err = growData(padded);
7797c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    if (err == NO_ERROR) goto restart_write;
7807c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return NULL;
7817c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
7827c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
7839a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wileystatus_t Parcel::writeUtf8AsUtf16(const std::string& str) {
7849a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    const uint8_t* strData = (uint8_t*)str.data();
7859a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    const size_t strLen= str.length();
7869a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
7879a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
7889a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley        return BAD_VALUE;
7899a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    }
7909a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
7919a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    status_t err = writeInt32(utf16Len);
7929a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    if (err) {
7939a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley        return err;
7949a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    }
7959a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
7969a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    // Allocate enough bytes to hold our converted string and its terminating NULL.
7979a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
7989a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    if (!dst) {
7999a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley        return NO_MEMORY;
8009a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    }
8019a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
8029a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    utf8_to_utf16(strData, strLen, (char16_t*)dst);
8039a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
8049a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    return NO_ERROR;
8059a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley}
8069a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
8079a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wileystatus_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
8089a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley  if (!str) {
8099a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    return writeInt32(-1);
8109a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley  }
8119a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley  return writeUtf8AsUtf16(*str);
8129a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley}
8139a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
8140b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinnamespace {
815b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
8160b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlintemplate<typename T>
8170b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
818451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin{
8195f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin    status_t status;
820451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    if (val.size() > std::numeric_limits<int32_t>::max()) {
8215f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin        status = BAD_VALUE;
8225f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin        return status;
823451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    }
824451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
8250b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    status = parcel->writeInt32(val.size());
826451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    if (status != OK) {
827451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin        return status;
828451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    }
829451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
8300b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    void* data = parcel->writeInplace(val.size());
8315f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin    if (!data) {
8325f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin        status = BAD_VALUE;
8335f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin        return status;
834451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin    }
835451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
8365f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin    memcpy(data, val.data(), val.size());
8375f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin    return status;
838451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin}
839451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
8400b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlintemplate<typename T>
8410b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t writeByteVectorInternalPtr(Parcel* parcel,
8420b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin                                    const std::unique_ptr<std::vector<T>>& val)
8430b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin{
8440b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    if (!val) {
8450b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin        return parcel->writeInt32(-1);
8460b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    }
8470b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8480b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    return writeByteVectorInternal(parcel, *val);
8490b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}
8500b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8510b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}  // namespace
8520b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8530b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
8540b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    return writeByteVectorInternal(this, val);
8550b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}
8560b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8570b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
8580b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin{
8590b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    return writeByteVectorInternalPtr(this, val);
8600b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}
8610b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8620b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
8630b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    return writeByteVectorInternal(this, val);
8640b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}
8650b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8660b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlinstatus_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
8670b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin{
8680b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin    return writeByteVectorInternalPtr(this, val);
8690b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin}
8700b1cf331d281d28eb67b3aa2be01026c2ceda4d9Casey Dahlin
8715f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlinstatus_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
872451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin{
87303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeInt32);
8745f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin}
875451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
876b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
877b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
878b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeInt32);
879b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
880b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
8815f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlinstatus_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
8825f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin{
88303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeInt64);
8845f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin}
885451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
886b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
887b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
888b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeInt64);
889b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
890b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
8915f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlinstatus_t Parcel::writeFloatVector(const std::vector<float>& val)
8925f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin{
89303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeFloat);
8945f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin}
895451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
896b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
897b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
898b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeFloat);
899b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
900b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
9015f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlinstatus_t Parcel::writeDoubleVector(const std::vector<double>& val)
9025f0625695ce6999ec70f5699f70a8a0fa84a1bf6Casey Dahlin{
90303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeDouble);
904451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin}
905451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
906b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
907b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
908b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeDouble);
909b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
910b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
911451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlinstatus_t Parcel::writeBoolVector(const std::vector<bool>& val)
912451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin{
91303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeBool);
914451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin}
915451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
916b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
917b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
918b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeBool);
919b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
920b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
921451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlinstatus_t Parcel::writeCharVector(const std::vector<char16_t>& val)
922451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin{
92303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeChar);
924451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin}
925451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
926b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
927b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
928b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeChar);
929b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
930b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
931451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlinstatus_t Parcel::writeString16Vector(const std::vector<String16>& val)
932451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin{
93303d1eb6bf90bcd0a04b176988478d2e939d3fba0Christopher Wiley    return writeTypedVector(val, &Parcel::writeString16);
934451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin}
935451ff582d730e27f4e22d9f158f8ee24d1bc2729Casey Dahlin
936b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeString16Vector(
937b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin        const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
938b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin{
939b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin    return writeNullableTypedVector(val, &Parcel::writeString16);
940b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin}
941b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlin
9429a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wileystatus_t Parcel::writeUtf8VectorAsUtf16Vector(
9439a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley                        const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
9449a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
9459a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley}
9469a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
9479a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wileystatus_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
9489a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley    return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
9499a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley}
9509a5e32f141fdb43edc1bb31ceacec781b35281b3Christopher Wiley
9517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeInt32(int32_t val)
9527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
95384a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    return writeAligned(val);
9547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
95541a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza
95641a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stozastatus_t Parcel::writeUint32(uint32_t val)
95741a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza{
95841a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza    return writeAligned(val);
95941a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza}
96041a0f2f86c0ca7d2fef50aef6215987f445a7231Dan Stoza
9615c0106e134366312206702f1b072347e20c90374Marco Nelissenstatus_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
962b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
963b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
964b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
965b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
966b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
967b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
9685c0106e134366312206702f1b072347e20c90374Marco Nelissen    if (!val) {
969e59cb43edad0eff28a81b18c3c4484442ff680ddChad Brubaker        return writeInt32(-1);
9705c0106e134366312206702f1b072347e20c90374Marco Nelissen    }
971e59cb43edad0eff28a81b18c3c4484442ff680ddChad Brubaker    status_t ret = writeInt32(static_cast<uint32_t>(len));
9725c0106e134366312206702f1b072347e20c90374Marco Nelissen    if (ret == NO_ERROR) {
9735c0106e134366312206702f1b072347e20c90374Marco Nelissen        ret = write(val, len * sizeof(*val));
9745c0106e134366312206702f1b072347e20c90374Marco Nelissen    }
9755c0106e134366312206702f1b072347e20c90374Marco Nelissen    return ret;
9765c0106e134366312206702f1b072347e20c90374Marco Nelissen}
977f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissenstatus_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
978b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    if (len > INT32_MAX) {
979b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // don't accept size_t values which may have come from an
980b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        // inadvertent conversion from a negative int.
981b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich        return BAD_VALUE;
982b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich    }
983b6b1423c87e457097875dc304fbbe38db6c41abdNick Kralevich
984f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    if (!val) {
985e59cb43edad0eff28a81b18c3c4484442ff680ddChad Brubaker        return writeInt32(-1);
986f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    }
987e59cb43edad0eff28a81b18c3c4484442ff680ddChad Brubaker    status_t ret = writeInt32(static_cast<uint32_t>(len));
988f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    if (ret == NO_ERROR) {
989f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen        ret = write(val, len * sizeof(*val));
990f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    }
991f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen    return ret;
992f0190bff38b6c29abbfc4a877442f71fc3d7dad8Marco Nelissen}
9937c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
994d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlinstatus_t Parcel::writeBool(bool val)
995d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin{
996d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    return writeInt32(int32_t(val));
997d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin}
998d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin
999d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlinstatus_t Parcel::writeChar(char16_t val)
1000d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin{
1001d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    return writeInt32(int32_t(val));
1002d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin}
1003d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin
1004d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlinstatus_t Parcel::writeByte(int8_t val)
1005d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin{
1006d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin    return writeInt32(int32_t(val));
1007d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin}
1008d6848f52e60be17b7f0992be7827dcae4ea2efb1Casey Dahlin
10097c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeInt64(int64_t val)
10107c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
101184a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    return writeAligned(val);
10127c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
10137c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
10142d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wustatus_t Parcel::writeUint64(uint64_t val)
10152d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu{
10162d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu    return writeAligned(val);
10172d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu}
10182d13afdfa1f43973bf3cb917c7d6496a38baa446Ronghua Wu
1019f683e0163a84d93448b9388126902242367cd961Serban Constantinescustatus_t Parcel::writePointer(uintptr_t val)
1020f683e0163a84d93448b9388126902242367cd961Serban Constantinescu{
102184e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg    return writeAligned<binder_uintptr_t>(val);
1022f683e0163a84d93448b9388126902242367cd961Serban Constantinescu}
1023f683e0163a84d93448b9388126902242367cd961Serban Constantinescu
10247c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeFloat(float val)
10257c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
102684a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    return writeAligned(val);
10277c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
10287c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
1029cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung#if defined(__mips__) && defined(__mips_hard_float)
1030cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung
1031cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leungstatus_t Parcel::writeDouble(double val)
1032cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung{
1033cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung    union {
1034cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung        double d;
1035cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung        unsigned long long ll;
1036cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung    } u;
1037cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung    u.d = val;
1038cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung    return writeAligned(u.ll);
1039cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung}
1040cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung
1041cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung#else
1042cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung
10437c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeDouble(double val)
10447c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
104584a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber    return writeAligned(val);
104684a6d041e2c69d4d0b69269a0b59e725c9f119a3Andreas Huber}
10477c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
1048cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung#endif
1049cc1a4bb1e1da3f2f38bdaf1f76348c7bc9fb6adbDouglas Leung
10507c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeCString(const char* str)
10517c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
10527c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return write(str, strlen(str)+1);
10537c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
10547c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
10557c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Projectstatus_t Parcel::writeString8(const String8& str)
10567c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project{
10577c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    status_t err = writeInt32(str.bytes());
1058d1dff8d4d47a5f9142a22b11950adc55ea2b36dfPravat Dalbehera    // only write string if its length is more than zero characters,
1059d1dff8d4d47a5f9142a22b11950adc55ea2b36dfPravat Dalbehera    // as readString8 will only read if the length field is non-zero.
1060d1dff8d4d47a5f9142a22b11950adc55ea2b36dfPravat Dalbehera    // this is slightly different from how writeString16 works.
1061d1dff8d4d47a5f9142a22b11950adc55ea2b36dfPravat Dalbehera    if (str.bytes() > 0 && err == NO_ERROR) {
10627c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project        err = write(str.string(), str.bytes()+1);
10637c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    }
10647c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project    return err;
10657c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project}
10667c1b96a165f970a09ed239bb4fb3f1b0d8f2a40The Android Open Source Project
1067b9872623ca0f2366c348d6d40a8ba45ee0e87710Casey Dahlinstatus_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1068