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