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