blob: 8eeab7aef6fc6459bb52589de359001246ed9506 [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{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700341 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700389 if ((mDataSize+len) > mDataCapacity) {
390 // grow data
391 err = growData(len);
392 if (err != NO_ERROR) {
393 return err;
394 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 }
396
397 // append data
398 memcpy(mData + mDataPos, data + offset, len);
399 mDataPos += len;
400 mDataSize += len;
401
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400402 err = NO_ERROR;
403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 if (numObjects > 0) {
405 // grow objects
406 if (mObjectsCapacity < mObjectsSize + numObjects) {
407 int newSize = ((mObjectsSize + numObjects)*3)/2;
408 size_t *objects =
409 (size_t*)realloc(mObjects, newSize*sizeof(size_t));
410 if (objects == (size_t*)0) {
411 return NO_MEMORY;
412 }
413 mObjects = objects;
414 mObjectsCapacity = newSize;
415 }
416
417 // append and acquire objects
418 int idx = mObjectsSize;
419 for (int i = firstIndex; i <= lastIndex; i++) {
420 size_t off = objects[i] - offset + startPos;
421 mObjects[idx++] = off;
422 mObjectsSize++;
423
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700424 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 = reinterpret_cast<flat_binder_object*>(mData + off);
426 acquire_object(proc, *flat, this);
427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700429 // If this is a file descriptor, we need to dup it so the
430 // new Parcel now owns its own fd, and can declare that we
431 // officially know we have fds.
432 flat->handle = dup(flat->handle);
433 flat->cookie = (void*)1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400435 if (!mAllowFds) {
436 err = FDS_NOT_ALLOWED;
437 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 }
439 }
440 }
441
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400442 return err;
443}
444
445bool Parcel::setAllowFds(bool allowFds)
446{
447 const bool origValue = mAllowFds;
448 mAllowFds = allowFds;
449 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450}
451
452bool Parcel::hasFileDescriptors() const
453{
454 if (!mFdsKnown) {
455 scanForFds();
456 }
457 return mHasFds;
458}
459
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700460// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461status_t Parcel::writeInterfaceToken(const String16& interface)
462{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700463 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
464 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465 // currently the interface identification token is just its name as a string
466 return writeString16(interface);
467}
468
Mathias Agopian83c04462009-05-22 19:00:22 -0700469bool Parcel::checkInterface(IBinder* binder) const
470{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700471 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700472}
473
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700474bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700475 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700477 int32_t strictPolicy = readInt32();
478 if (threadState == NULL) {
479 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700480 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700481 if ((threadState->getLastTransactionBinderFlags() &
482 IBinder::FLAG_ONEWAY) != 0) {
483 // For one-way calls, the callee is running entirely
484 // disconnected from the caller, so disable StrictMode entirely.
485 // Not only does disk/network usage not impact the caller, but
486 // there's no way to commuicate back any violations anyway.
487 threadState->setStrictModePolicy(0);
488 } else {
489 threadState->setStrictModePolicy(strictPolicy);
490 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700491 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700492 if (str == interface) {
493 return true;
494 } else {
495 LOGW("**** enforceInterface() expected '%s' but read '%s'\n",
496 String8(interface).string(), String8(str).string());
497 return false;
498 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700499}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500
501const size_t* Parcel::objects() const
502{
503 return mObjects;
504}
505
506size_t Parcel::objectsCount() const
507{
508 return mObjectsSize;
509}
510
511status_t Parcel::errorCheck() const
512{
513 return mError;
514}
515
516void Parcel::setError(status_t err)
517{
518 mError = err;
519}
520
521status_t Parcel::finishWrite(size_t len)
522{
523 //printf("Finish write of %d\n", len);
524 mDataPos += len;
525 LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
526 if (mDataPos > mDataSize) {
527 mDataSize = mDataPos;
528 LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
529 }
530 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
531 return NO_ERROR;
532}
533
534status_t Parcel::writeUnpadded(const void* data, size_t len)
535{
536 size_t end = mDataPos + len;
537 if (end < mDataPos) {
538 // integer overflow
539 return BAD_VALUE;
540 }
541
542 if (end <= mDataCapacity) {
543restart_write:
544 memcpy(mData+mDataPos, data, len);
545 return finishWrite(len);
546 }
547
548 status_t err = growData(len);
549 if (err == NO_ERROR) goto restart_write;
550 return err;
551}
552
553status_t Parcel::write(const void* data, size_t len)
554{
555 void* const d = writeInplace(len);
556 if (d) {
557 memcpy(d, data, len);
558 return NO_ERROR;
559 }
560 return mError;
561}
562
563void* Parcel::writeInplace(size_t len)
564{
565 const size_t padded = PAD_SIZE(len);
566
567 // sanity check for integer overflow
568 if (mDataPos+padded < mDataPos) {
569 return NULL;
570 }
571
572 if ((mDataPos+padded) <= mDataCapacity) {
573restart_write:
574 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
575 uint8_t* const data = mData+mDataPos;
576
577 // Need to pad at end?
578 if (padded != len) {
579#if BYTE_ORDER == BIG_ENDIAN
580 static const uint32_t mask[4] = {
581 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
582 };
583#endif
584#if BYTE_ORDER == LITTLE_ENDIAN
585 static const uint32_t mask[4] = {
586 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
587 };
588#endif
589 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
590 // *reinterpret_cast<void**>(data+padded-4));
591 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
592 }
593
594 finishWrite(padded);
595 return data;
596 }
597
598 status_t err = growData(padded);
599 if (err == NO_ERROR) goto restart_write;
600 return NULL;
601}
602
603status_t Parcel::writeInt32(int32_t val)
604{
Andreas Huber84a6d042009-08-17 13:33:27 -0700605 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606}
607
608status_t Parcel::writeInt64(int64_t val)
609{
Andreas Huber84a6d042009-08-17 13:33:27 -0700610 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611}
612
613status_t Parcel::writeFloat(float val)
614{
Andreas Huber84a6d042009-08-17 13:33:27 -0700615 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616}
617
618status_t Parcel::writeDouble(double val)
619{
Andreas Huber84a6d042009-08-17 13:33:27 -0700620 return writeAligned(val);
621}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622
Andreas Huber84a6d042009-08-17 13:33:27 -0700623status_t Parcel::writeIntPtr(intptr_t val)
624{
625 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626}
627
628status_t Parcel::writeCString(const char* str)
629{
630 return write(str, strlen(str)+1);
631}
632
633status_t Parcel::writeString8(const String8& str)
634{
635 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100636 // only write string if its length is more than zero characters,
637 // as readString8 will only read if the length field is non-zero.
638 // this is slightly different from how writeString16 works.
639 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640 err = write(str.string(), str.bytes()+1);
641 }
642 return err;
643}
644
645status_t Parcel::writeString16(const String16& str)
646{
647 return writeString16(str.string(), str.size());
648}
649
650status_t Parcel::writeString16(const char16_t* str, size_t len)
651{
652 if (str == NULL) return writeInt32(-1);
653
654 status_t err = writeInt32(len);
655 if (err == NO_ERROR) {
656 len *= sizeof(char16_t);
657 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
658 if (data) {
659 memcpy(data, str, len);
660 *reinterpret_cast<char16_t*>(data+len) = 0;
661 return NO_ERROR;
662 }
663 err = mError;
664 }
665 return err;
666}
667
668status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
669{
670 return flatten_binder(ProcessState::self(), val, this);
671}
672
673status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
674{
675 return flatten_binder(ProcessState::self(), val, this);
676}
677
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700678status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800679{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700680 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800681 return BAD_TYPE;
682
683 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700684 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800685 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700687 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800688 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700690 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
691 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
693 if (err != NO_ERROR) {
694 LOGD("write native handle, write dup fd failed");
695 return err;
696 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700697 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800698 return err;
699}
700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701status_t Parcel::writeFileDescriptor(int fd)
702{
703 flat_binder_object obj;
704 obj.type = BINDER_TYPE_FD;
705 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
706 obj.handle = fd;
707 obj.cookie = (void*)0;
708 return writeObject(obj, true);
709}
710
711status_t Parcel::writeDupFileDescriptor(int fd)
712{
713 flat_binder_object obj;
714 obj.type = BINDER_TYPE_FD;
715 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
716 obj.handle = dup(fd);
717 obj.cookie = (void*)1;
718 return writeObject(obj, true);
719}
720
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800721status_t Parcel::write(const Flattenable& val)
722{
723 status_t err;
724
725 // size if needed
726 size_t len = val.getFlattenedSize();
727 size_t fd_count = val.getFdCount();
728
729 err = this->writeInt32(len);
730 if (err) return err;
731
732 err = this->writeInt32(fd_count);
733 if (err) return err;
734
735 // payload
736 void* buf = this->writeInplace(PAD_SIZE(len));
737 if (buf == NULL)
738 return BAD_VALUE;
739
740 int* fds = NULL;
741 if (fd_count) {
742 fds = new int[fd_count];
743 }
744
745 err = val.flatten(buf, len, fds, fd_count);
746 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
747 err = this->writeDupFileDescriptor( fds[i] );
748 }
749
750 if (fd_count) {
751 delete [] fds;
752 }
753
754 return err;
755}
756
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700757status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
758{
759 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
760 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
761 if (enoughData && enoughObjects) {
762restart_write:
763 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
764
765 // Need to write meta-data?
766 if (nullMetaData || val.binder != NULL) {
767 mObjects[mObjectsSize] = mDataPos;
768 acquire_object(ProcessState::self(), val, this);
769 mObjectsSize++;
770 }
771
772 // remember if it's a file descriptor
773 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400774 if (!mAllowFds) {
775 return FDS_NOT_ALLOWED;
776 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700777 mHasFds = mFdsKnown = true;
778 }
779
780 return finishWrite(sizeof(flat_binder_object));
781 }
782
783 if (!enoughData) {
784 const status_t err = growData(sizeof(val));
785 if (err != NO_ERROR) return err;
786 }
787 if (!enoughObjects) {
788 size_t newSize = ((mObjectsSize+2)*3)/2;
789 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
790 if (objects == NULL) return NO_MEMORY;
791 mObjects = objects;
792 mObjectsCapacity = newSize;
793 }
794
795 goto restart_write;
796}
797
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700798status_t Parcel::writeNoException()
799{
800 return writeInt32(0);
801}
802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700803void Parcel::remove(size_t start, size_t amt)
804{
805 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
806}
807
808status_t Parcel::read(void* outData, size_t len) const
809{
810 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
811 memcpy(outData, mData+mDataPos, len);
812 mDataPos += PAD_SIZE(len);
813 LOGV("read Setting data pos of %p to %d\n", this, mDataPos);
814 return NO_ERROR;
815 }
816 return NOT_ENOUGH_DATA;
817}
818
819const void* Parcel::readInplace(size_t len) const
820{
821 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
822 const void* data = mData+mDataPos;
823 mDataPos += PAD_SIZE(len);
824 LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
825 return data;
826 }
827 return NULL;
828}
829
Andreas Huber84a6d042009-08-17 13:33:27 -0700830template<class T>
831status_t Parcel::readAligned(T *pArg) const {
832 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
833
834 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700835 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700836 mDataPos += sizeof(T);
837 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700838 return NO_ERROR;
839 } else {
840 return NOT_ENOUGH_DATA;
841 }
842}
843
Andreas Huber84a6d042009-08-17 13:33:27 -0700844template<class T>
845T Parcel::readAligned() const {
846 T result;
847 if (readAligned(&result) != NO_ERROR) {
848 result = 0;
849 }
850
851 return result;
852}
853
854template<class T>
855status_t Parcel::writeAligned(T val) {
856 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
857
858 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
859restart_write:
860 *reinterpret_cast<T*>(mData+mDataPos) = val;
861 return finishWrite(sizeof(val));
862 }
863
864 status_t err = growData(sizeof(val));
865 if (err == NO_ERROR) goto restart_write;
866 return err;
867}
868
869status_t Parcel::readInt32(int32_t *pArg) const
870{
871 return readAligned(pArg);
872}
873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700874int32_t Parcel::readInt32() const
875{
Andreas Huber84a6d042009-08-17 13:33:27 -0700876 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877}
878
879
880status_t Parcel::readInt64(int64_t *pArg) const
881{
Andreas Huber84a6d042009-08-17 13:33:27 -0700882 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883}
884
885
886int64_t Parcel::readInt64() const
887{
Andreas Huber84a6d042009-08-17 13:33:27 -0700888 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700889}
890
891status_t Parcel::readFloat(float *pArg) const
892{
Andreas Huber84a6d042009-08-17 13:33:27 -0700893 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894}
895
896
897float Parcel::readFloat() const
898{
Andreas Huber84a6d042009-08-17 13:33:27 -0700899 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900}
901
902status_t Parcel::readDouble(double *pArg) const
903{
Andreas Huber84a6d042009-08-17 13:33:27 -0700904 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700905}
906
907
908double Parcel::readDouble() const
909{
Andreas Huber84a6d042009-08-17 13:33:27 -0700910 return readAligned<double>();
911}
912
913status_t Parcel::readIntPtr(intptr_t *pArg) const
914{
915 return readAligned(pArg);
916}
917
918
919intptr_t Parcel::readIntPtr() const
920{
921 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700922}
923
924
925const char* Parcel::readCString() const
926{
927 const size_t avail = mDataSize-mDataPos;
928 if (avail > 0) {
929 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
930 // is the string's trailing NUL within the parcel's valid bounds?
931 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
932 if (eos) {
933 const size_t len = eos - str;
934 mDataPos += PAD_SIZE(len+1);
935 LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
936 return str;
937 }
938 }
939 return NULL;
940}
941
942String8 Parcel::readString8() const
943{
944 int32_t size = readInt32();
945 // watch for potential int overflow adding 1 for trailing NUL
946 if (size > 0 && size < INT32_MAX) {
947 const char* str = (const char*)readInplace(size+1);
948 if (str) return String8(str, size);
949 }
950 return String8();
951}
952
953String16 Parcel::readString16() const
954{
955 size_t len;
956 const char16_t* str = readString16Inplace(&len);
957 if (str) return String16(str, len);
958 LOGE("Reading a NULL string not supported here.");
959 return String16();
960}
961
962const char16_t* Parcel::readString16Inplace(size_t* outLen) const
963{
964 int32_t size = readInt32();
965 // watch for potential int overflow from size+1
966 if (size >= 0 && size < INT32_MAX) {
967 *outLen = size;
968 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
969 if (str != NULL) {
970 return str;
971 }
972 }
973 *outLen = 0;
974 return NULL;
975}
976
977sp<IBinder> Parcel::readStrongBinder() const
978{
979 sp<IBinder> val;
980 unflatten_binder(ProcessState::self(), *this, &val);
981 return val;
982}
983
984wp<IBinder> Parcel::readWeakBinder() const
985{
986 wp<IBinder> val;
987 unflatten_binder(ProcessState::self(), *this, &val);
988 return val;
989}
990
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700991int32_t Parcel::readExceptionCode() const
992{
993 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -0700994 if (exception_code == EX_HAS_REPLY_HEADER) {
995 int32_t header_size = readAligned<int32_t>();
996 // Skip over fat responses headers. Not used (or propagated) in
997 // native code
998 setDataPosition(dataPosition() + header_size);
999 // And fat response headers are currently only used when there are no
1000 // exceptions, so return no error:
1001 return 0;
1002 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001003 return exception_code;
1004}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001005
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001006native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001007{
1008 int numFds, numInts;
1009 status_t err;
1010 err = readInt32(&numFds);
1011 if (err != NO_ERROR) return 0;
1012 err = readInt32(&numInts);
1013 if (err != NO_ERROR) return 0;
1014
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001015 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001016 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001017 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001018 if (h->data[i] < 0) err = BAD_VALUE;
1019 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001020 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001021 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001022 native_handle_close(h);
1023 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001024 h = 0;
1025 }
1026 return h;
1027}
1028
1029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030int Parcel::readFileDescriptor() const
1031{
1032 const flat_binder_object* flat = readObject(true);
1033 if (flat) {
1034 switch (flat->type) {
1035 case BINDER_TYPE_FD:
1036 //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
1037 return flat->handle;
1038 }
1039 }
1040 return BAD_TYPE;
1041}
1042
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001043status_t Parcel::read(Flattenable& val) const
1044{
1045 // size
1046 const size_t len = this->readInt32();
1047 const size_t fd_count = this->readInt32();
1048
1049 // payload
1050 void const* buf = this->readInplace(PAD_SIZE(len));
1051 if (buf == NULL)
1052 return BAD_VALUE;
1053
1054 int* fds = NULL;
1055 if (fd_count) {
1056 fds = new int[fd_count];
1057 }
1058
1059 status_t err = NO_ERROR;
1060 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1061 fds[i] = dup(this->readFileDescriptor());
1062 if (fds[i] < 0) err = BAD_VALUE;
1063 }
1064
1065 if (err == NO_ERROR) {
1066 err = val.unflatten(buf, len, fds, fd_count);
1067 }
1068
1069 if (fd_count) {
1070 delete [] fds;
1071 }
1072
1073 return err;
1074}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1076{
1077 const size_t DPOS = mDataPos;
1078 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1079 const flat_binder_object* obj
1080 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1081 mDataPos = DPOS + sizeof(flat_binder_object);
1082 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001083 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 // the object list, so we don't want to check for it when
1085 // reading.
1086 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1087 return obj;
1088 }
1089
1090 // Ensure that this object is valid...
1091 size_t* const OBJS = mObjects;
1092 const size_t N = mObjectsSize;
1093 size_t opos = mNextObjectHint;
1094
1095 if (N > 0) {
1096 LOGV("Parcel %p looking for obj at %d, hint=%d\n",
1097 this, DPOS, opos);
1098
1099 // Start at the current hint position, looking for an object at
1100 // the current data position.
1101 if (opos < N) {
1102 while (opos < (N-1) && OBJS[opos] < DPOS) {
1103 opos++;
1104 }
1105 } else {
1106 opos = N-1;
1107 }
1108 if (OBJS[opos] == DPOS) {
1109 // Found it!
1110 LOGV("Parcel found obj %d at index %d with forward search",
1111 this, DPOS, opos);
1112 mNextObjectHint = opos+1;
1113 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1114 return obj;
1115 }
1116
1117 // Look backwards for it...
1118 while (opos > 0 && OBJS[opos] > DPOS) {
1119 opos--;
1120 }
1121 if (OBJS[opos] == DPOS) {
1122 // Found it!
1123 LOGV("Parcel found obj %d at index %d with backward search",
1124 this, DPOS, opos);
1125 mNextObjectHint = opos+1;
1126 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1127 return obj;
1128 }
1129 }
1130 LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
1131 this, DPOS);
1132 }
1133 return NULL;
1134}
1135
1136void Parcel::closeFileDescriptors()
1137{
1138 size_t i = mObjectsSize;
1139 if (i > 0) {
1140 //LOGI("Closing file descriptors for %d objects...", mObjectsSize);
1141 }
1142 while (i > 0) {
1143 i--;
1144 const flat_binder_object* flat
1145 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1146 if (flat->type == BINDER_TYPE_FD) {
1147 //LOGI("Closing fd: %ld\n", flat->handle);
1148 close(flat->handle);
1149 }
1150 }
1151}
1152
1153const uint8_t* Parcel::ipcData() const
1154{
1155 return mData;
1156}
1157
1158size_t Parcel::ipcDataSize() const
1159{
1160 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1161}
1162
1163const size_t* Parcel::ipcObjects() const
1164{
1165 return mObjects;
1166}
1167
1168size_t Parcel::ipcObjectsCount() const
1169{
1170 return mObjectsSize;
1171}
1172
1173void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1174 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1175{
1176 freeDataNoInit();
1177 mError = NO_ERROR;
1178 mData = const_cast<uint8_t*>(data);
1179 mDataSize = mDataCapacity = dataSize;
1180 //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
1181 mDataPos = 0;
1182 LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
1183 mObjects = const_cast<size_t*>(objects);
1184 mObjectsSize = mObjectsCapacity = objectsCount;
1185 mNextObjectHint = 0;
1186 mOwner = relFunc;
1187 mOwnerCookie = relCookie;
1188 scanForFds();
1189}
1190
1191void Parcel::print(TextOutput& to, uint32_t flags) const
1192{
1193 to << "Parcel(";
1194
1195 if (errorCheck() != NO_ERROR) {
1196 const status_t err = errorCheck();
1197 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1198 } else if (dataSize() > 0) {
1199 const uint8_t* DATA = data();
1200 to << indent << HexDump(DATA, dataSize()) << dedent;
1201 const size_t* OBJS = objects();
1202 const size_t N = objectsCount();
1203 for (size_t i=0; i<N; i++) {
1204 const flat_binder_object* flat
1205 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1206 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1207 << TypeCode(flat->type & 0x7f7f7f00)
1208 << " = " << flat->binder;
1209 }
1210 } else {
1211 to << "NULL";
1212 }
1213
1214 to << ")";
1215}
1216
1217void Parcel::releaseObjects()
1218{
1219 const sp<ProcessState> proc(ProcessState::self());
1220 size_t i = mObjectsSize;
1221 uint8_t* const data = mData;
1222 size_t* const objects = mObjects;
1223 while (i > 0) {
1224 i--;
1225 const flat_binder_object* flat
1226 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1227 release_object(proc, *flat, this);
1228 }
1229}
1230
1231void Parcel::acquireObjects()
1232{
1233 const sp<ProcessState> proc(ProcessState::self());
1234 size_t i = mObjectsSize;
1235 uint8_t* const data = mData;
1236 size_t* const objects = mObjects;
1237 while (i > 0) {
1238 i--;
1239 const flat_binder_object* flat
1240 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1241 acquire_object(proc, *flat, this);
1242 }
1243}
1244
1245void Parcel::freeData()
1246{
1247 freeDataNoInit();
1248 initState();
1249}
1250
1251void Parcel::freeDataNoInit()
1252{
1253 if (mOwner) {
1254 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1255 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1256 } else {
1257 releaseObjects();
1258 if (mData) free(mData);
1259 if (mObjects) free(mObjects);
1260 }
1261}
1262
1263status_t Parcel::growData(size_t len)
1264{
1265 size_t newSize = ((mDataSize+len)*3)/2;
1266 return (newSize <= mDataSize)
1267 ? (status_t) NO_MEMORY
1268 : continueWrite(newSize);
1269}
1270
1271status_t Parcel::restartWrite(size_t desired)
1272{
1273 if (mOwner) {
1274 freeData();
1275 return continueWrite(desired);
1276 }
1277
1278 uint8_t* data = (uint8_t*)realloc(mData, desired);
1279 if (!data && desired > mDataCapacity) {
1280 mError = NO_MEMORY;
1281 return NO_MEMORY;
1282 }
1283
1284 releaseObjects();
1285
1286 if (data) {
1287 mData = data;
1288 mDataCapacity = desired;
1289 }
1290
1291 mDataSize = mDataPos = 0;
1292 LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1293 LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
1294
1295 free(mObjects);
1296 mObjects = NULL;
1297 mObjectsSize = mObjectsCapacity = 0;
1298 mNextObjectHint = 0;
1299 mHasFds = false;
1300 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001301 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302
1303 return NO_ERROR;
1304}
1305
1306status_t Parcel::continueWrite(size_t desired)
1307{
1308 // If shrinking, first adjust for any objects that appear
1309 // after the new data size.
1310 size_t objectsSize = mObjectsSize;
1311 if (desired < mDataSize) {
1312 if (desired == 0) {
1313 objectsSize = 0;
1314 } else {
1315 while (objectsSize > 0) {
1316 if (mObjects[objectsSize-1] < desired)
1317 break;
1318 objectsSize--;
1319 }
1320 }
1321 }
1322
1323 if (mOwner) {
1324 // If the size is going to zero, just release the owner's data.
1325 if (desired == 0) {
1326 freeData();
1327 return NO_ERROR;
1328 }
1329
1330 // If there is a different owner, we need to take
1331 // posession.
1332 uint8_t* data = (uint8_t*)malloc(desired);
1333 if (!data) {
1334 mError = NO_MEMORY;
1335 return NO_MEMORY;
1336 }
1337 size_t* objects = NULL;
1338
1339 if (objectsSize) {
1340 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1341 if (!objects) {
1342 mError = NO_MEMORY;
1343 return NO_MEMORY;
1344 }
1345
1346 // Little hack to only acquire references on objects
1347 // we will be keeping.
1348 size_t oldObjectsSize = mObjectsSize;
1349 mObjectsSize = objectsSize;
1350 acquireObjects();
1351 mObjectsSize = oldObjectsSize;
1352 }
1353
1354 if (mData) {
1355 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1356 }
1357 if (objects && mObjects) {
1358 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1359 }
1360 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1361 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1362 mOwner = NULL;
1363
1364 mData = data;
1365 mObjects = objects;
1366 mDataSize = (mDataSize < desired) ? mDataSize : desired;
1367 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1368 mDataCapacity = desired;
1369 mObjectsSize = mObjectsCapacity = objectsSize;
1370 mNextObjectHint = 0;
1371
1372 } else if (mData) {
1373 if (objectsSize < mObjectsSize) {
1374 // Need to release refs on any objects we are dropping.
1375 const sp<ProcessState> proc(ProcessState::self());
1376 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1377 const flat_binder_object* flat
1378 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1379 if (flat->type == BINDER_TYPE_FD) {
1380 // will need to rescan because we may have lopped off the only FDs
1381 mFdsKnown = false;
1382 }
1383 release_object(proc, *flat, this);
1384 }
1385 size_t* objects =
1386 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1387 if (objects) {
1388 mObjects = objects;
1389 }
1390 mObjectsSize = objectsSize;
1391 mNextObjectHint = 0;
1392 }
1393
1394 // We own the data, so we can just do a realloc().
1395 if (desired > mDataCapacity) {
1396 uint8_t* data = (uint8_t*)realloc(mData, desired);
1397 if (data) {
1398 mData = data;
1399 mDataCapacity = desired;
1400 } else if (desired > mDataCapacity) {
1401 mError = NO_MEMORY;
1402 return NO_MEMORY;
1403 }
1404 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001405 if (mDataSize > desired) {
1406 mDataSize = desired;
1407 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1408 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 if (mDataPos > desired) {
1410 mDataPos = desired;
1411 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1412 }
1413 }
1414
1415 } else {
1416 // This is the first data. Easy!
1417 uint8_t* data = (uint8_t*)malloc(desired);
1418 if (!data) {
1419 mError = NO_MEMORY;
1420 return NO_MEMORY;
1421 }
1422
1423 if(!(mDataCapacity == 0 && mObjects == NULL
1424 && mObjectsCapacity == 0)) {
1425 LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
1426 }
1427
1428 mData = data;
1429 mDataSize = mDataPos = 0;
1430 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1431 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1432 mDataCapacity = desired;
1433 }
1434
1435 return NO_ERROR;
1436}
1437
1438void Parcel::initState()
1439{
1440 mError = NO_ERROR;
1441 mData = 0;
1442 mDataSize = 0;
1443 mDataCapacity = 0;
1444 mDataPos = 0;
1445 LOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1446 LOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
1447 mObjects = NULL;
1448 mObjectsSize = 0;
1449 mObjectsCapacity = 0;
1450 mNextObjectHint = 0;
1451 mHasFds = false;
1452 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001453 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454 mOwner = NULL;
1455}
1456
1457void Parcel::scanForFds() const
1458{
1459 bool hasFds = false;
1460 for (size_t i=0; i<mObjectsSize; i++) {
1461 const flat_binder_object* flat
1462 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1463 if (flat->type == BINDER_TYPE_FD) {
1464 hasFds = true;
1465 break;
1466 }
1467 }
1468 mHasFds = hasFds;
1469 mFdsKnown = true;
1470}
1471
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472}; // namespace android