| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2005 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #define LOG_TAG "Parcel" | 
 | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 20 | #include <binder/Parcel.h> | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> | 
 | 24 | #include <binder/BpBinder.h> | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | #include <utils/Debug.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/ProcessState.h> | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> | 
 | 28 | #include <utils/String8.h> | 
 | 29 | #include <utils/String16.h> | 
 | 30 | #include <utils/TextOutput.h> | 
 | 31 | #include <utils/misc.h> | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 32 | #include <utils/Flattenable.h> | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 |  | 
| Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 34 | #include <private/binder/binder_module.h> | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 |  | 
 | 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 Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 51 | // Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER | 
 | 52 | #define STRICT_MODE_PENALTY_GATHER 0x100 | 
 | 53 |  | 
| Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 54 | // 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 Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | // XXX This can be made public if we want to provide | 
 | 58 | // support for typed data. | 
 | 59 | struct small_flat_data | 
 | 60 | { | 
 | 61 |     uint32_t type; | 
 | 62 |     uint32_t data; | 
 | 63 | }; | 
 | 64 |  | 
 | 65 | namespace android { | 
 | 66 |  | 
 | 67 | void 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 |  | 
 | 104 | void 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 |  | 
 | 140 | inline 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 |  | 
 | 146 | status_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 |  | 
 | 177 | status_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 |  | 
 | 225 | inline static status_t finish_unflatten_binder( | 
 | 226 |     BpBinder* proxy, const flat_binder_object& flat, const Parcel& in) | 
 | 227 | { | 
 | 228 |     return NO_ERROR; | 
 | 229 | } | 
 | 230 |      | 
 | 231 | status_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 |  | 
 | 250 | status_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 |  | 
 | 281 | Parcel::Parcel() | 
 | 282 | { | 
 | 283 |     initState(); | 
 | 284 | } | 
 | 285 |  | 
 | 286 | Parcel::~Parcel() | 
 | 287 | { | 
 | 288 |     freeDataNoInit(); | 
 | 289 | } | 
 | 290 |  | 
 | 291 | const uint8_t* Parcel::data() const | 
 | 292 | { | 
 | 293 |     return mData; | 
 | 294 | } | 
 | 295 |  | 
 | 296 | size_t Parcel::dataSize() const | 
 | 297 | { | 
 | 298 |     return (mDataSize > mDataPos ? mDataSize : mDataPos); | 
 | 299 | } | 
 | 300 |  | 
 | 301 | size_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 |  | 
 | 312 | size_t Parcel::dataPosition() const | 
 | 313 | { | 
 | 314 |     return mDataPos; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | size_t Parcel::dataCapacity() const | 
 | 318 | { | 
 | 319 |     return mDataCapacity; | 
 | 320 | } | 
 | 321 |  | 
 | 322 | status_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 |  | 
 | 333 | void Parcel::setDataPosition(size_t pos) const | 
 | 334 | { | 
 | 335 |     mDataPos = pos; | 
 | 336 |     mNextObjectHint = 0; | 
 | 337 | } | 
 | 338 |  | 
 | 339 | status_t Parcel::setDataCapacity(size_t size) | 
 | 340 | { | 
| Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 341 |     if (size > mDataCapacity) return continueWrite(size); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 |     return NO_ERROR; | 
 | 343 | } | 
 | 344 |  | 
 | 345 | status_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 Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 356 | status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len) | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | { | 
 | 358 |     const sp<ProcessState> proc(ProcessState::self()); | 
 | 359 |     status_t err; | 
| Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 360 |     const uint8_t *data = parcel->mData; | 
 | 361 |     const size_t *objects = parcel->mObjects; | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 362 |     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 Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 389 |     if ((mDataSize+len) > mDataCapacity) { | 
 | 390 |         // grow data | 
 | 391 |         err = growData(len); | 
 | 392 |         if (err != NO_ERROR) { | 
 | 393 |             return err; | 
 | 394 |         } | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 |     } | 
 | 396 |  | 
 | 397 |     // append data | 
 | 398 |     memcpy(mData + mDataPos, data + offset, len); | 
 | 399 |     mDataPos += len; | 
 | 400 |     mDataSize += len; | 
 | 401 |  | 
 | 402 |     if (numObjects > 0) { | 
 | 403 |         // grow objects | 
 | 404 |         if (mObjectsCapacity < mObjectsSize + numObjects) { | 
 | 405 |             int newSize = ((mObjectsSize + numObjects)*3)/2; | 
 | 406 |             size_t *objects = | 
 | 407 |                 (size_t*)realloc(mObjects, newSize*sizeof(size_t)); | 
 | 408 |             if (objects == (size_t*)0) { | 
 | 409 |                 return NO_MEMORY; | 
 | 410 |             } | 
 | 411 |             mObjects = objects; | 
 | 412 |             mObjectsCapacity = newSize; | 
 | 413 |         } | 
 | 414 |          | 
 | 415 |         // append and acquire objects | 
 | 416 |         int idx = mObjectsSize; | 
 | 417 |         for (int i = firstIndex; i <= lastIndex; i++) { | 
 | 418 |             size_t off = objects[i] - offset + startPos; | 
 | 419 |             mObjects[idx++] = off; | 
 | 420 |             mObjectsSize++; | 
 | 421 |  | 
| Dianne Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 422 |             flat_binder_object* flat | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 423 |                 = reinterpret_cast<flat_binder_object*>(mData + off); | 
 | 424 |             acquire_object(proc, *flat, this); | 
 | 425 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 426 |             if (flat->type == BINDER_TYPE_FD) { | 
| Dianne Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 427 |                 // If this is a file descriptor, we need to dup it so the | 
 | 428 |                 // new Parcel now owns its own fd, and can declare that we | 
 | 429 |                 // officially know we have fds. | 
 | 430 |                 flat->handle = dup(flat->handle); | 
 | 431 |                 flat->cookie = (void*)1; | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 |                 mHasFds = mFdsKnown = true; | 
 | 433 |             } | 
 | 434 |         } | 
 | 435 |     } | 
 | 436 |  | 
 | 437 |     return NO_ERROR; | 
 | 438 | } | 
 | 439 |  | 
 | 440 | bool Parcel::hasFileDescriptors() const | 
 | 441 | { | 
 | 442 |     if (!mFdsKnown) { | 
 | 443 |         scanForFds(); | 
 | 444 |     } | 
 | 445 |     return mHasFds; | 
 | 446 | } | 
 | 447 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 448 | // Write RPC headers.  (previously just the interface token) | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | status_t Parcel::writeInterfaceToken(const String16& interface) | 
 | 450 | { | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 451 |     writeInt32(IPCThreadState::self()->getStrictModePolicy() | | 
 | 452 |                STRICT_MODE_PENALTY_GATHER); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 453 |     // currently the interface identification token is just its name as a string | 
 | 454 |     return writeString16(interface); | 
 | 455 | } | 
 | 456 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 457 | bool Parcel::checkInterface(IBinder* binder) const | 
 | 458 | { | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 459 |     return enforceInterface(binder->getInterfaceDescriptor()); | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 460 | } | 
 | 461 |  | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 462 | bool Parcel::enforceInterface(const String16& interface, | 
| Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 463 |                               IPCThreadState* threadState) const | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | { | 
| Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 465 |     int32_t strictPolicy = readInt32(); | 
 | 466 |     if (threadState == NULL) { | 
 | 467 |         threadState = IPCThreadState::self(); | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 468 |     } | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 469 |     if ((threadState->getLastTransactionBinderFlags() & | 
 | 470 |          IBinder::FLAG_ONEWAY) != 0) { | 
 | 471 |       // For one-way calls, the callee is running entirely | 
 | 472 |       // disconnected from the caller, so disable StrictMode entirely. | 
 | 473 |       // Not only does disk/network usage not impact the caller, but | 
 | 474 |       // there's no way to commuicate back any violations anyway. | 
 | 475 |       threadState->setStrictModePolicy(0); | 
 | 476 |     } else { | 
 | 477 |       threadState->setStrictModePolicy(strictPolicy); | 
 | 478 |     } | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 479 |     const String16 str(readString16()); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 |     if (str == interface) { | 
 | 481 |         return true; | 
 | 482 |     } else { | 
 | 483 |         LOGW("**** enforceInterface() expected '%s' but read '%s'\n", | 
 | 484 |                 String8(interface).string(), String8(str).string()); | 
 | 485 |         return false; | 
 | 486 |     } | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 487 | } | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 |  | 
 | 489 | const size_t* Parcel::objects() const | 
 | 490 | { | 
 | 491 |     return mObjects; | 
 | 492 | } | 
 | 493 |  | 
 | 494 | size_t Parcel::objectsCount() const | 
 | 495 | { | 
 | 496 |     return mObjectsSize; | 
 | 497 | } | 
 | 498 |  | 
 | 499 | status_t Parcel::errorCheck() const | 
 | 500 | { | 
 | 501 |     return mError; | 
 | 502 | } | 
 | 503 |  | 
 | 504 | void Parcel::setError(status_t err) | 
 | 505 | { | 
 | 506 |     mError = err; | 
 | 507 | } | 
 | 508 |  | 
 | 509 | status_t Parcel::finishWrite(size_t len) | 
 | 510 | { | 
 | 511 |     //printf("Finish write of %d\n", len); | 
 | 512 |     mDataPos += len; | 
 | 513 |     LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos); | 
 | 514 |     if (mDataPos > mDataSize) { | 
 | 515 |         mDataSize = mDataPos; | 
 | 516 |         LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize); | 
 | 517 |     } | 
 | 518 |     //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); | 
 | 519 |     return NO_ERROR; | 
 | 520 | } | 
 | 521 |  | 
 | 522 | status_t Parcel::writeUnpadded(const void* data, size_t len) | 
 | 523 | { | 
 | 524 |     size_t end = mDataPos + len; | 
 | 525 |     if (end < mDataPos) { | 
 | 526 |         // integer overflow | 
 | 527 |         return BAD_VALUE; | 
 | 528 |     } | 
 | 529 |  | 
 | 530 |     if (end <= mDataCapacity) { | 
 | 531 | restart_write: | 
 | 532 |         memcpy(mData+mDataPos, data, len); | 
 | 533 |         return finishWrite(len); | 
 | 534 |     } | 
 | 535 |  | 
 | 536 |     status_t err = growData(len); | 
 | 537 |     if (err == NO_ERROR) goto restart_write; | 
 | 538 |     return err; | 
 | 539 | } | 
 | 540 |  | 
 | 541 | status_t Parcel::write(const void* data, size_t len) | 
 | 542 | { | 
 | 543 |     void* const d = writeInplace(len); | 
 | 544 |     if (d) { | 
 | 545 |         memcpy(d, data, len); | 
 | 546 |         return NO_ERROR; | 
 | 547 |     } | 
 | 548 |     return mError; | 
 | 549 | } | 
 | 550 |  | 
 | 551 | void* Parcel::writeInplace(size_t len) | 
 | 552 | { | 
 | 553 |     const size_t padded = PAD_SIZE(len); | 
 | 554 |  | 
 | 555 |     // sanity check for integer overflow | 
 | 556 |     if (mDataPos+padded < mDataPos) { | 
 | 557 |         return NULL; | 
 | 558 |     } | 
 | 559 |  | 
 | 560 |     if ((mDataPos+padded) <= mDataCapacity) { | 
 | 561 | restart_write: | 
 | 562 |         //printf("Writing %ld bytes, padded to %ld\n", len, padded); | 
 | 563 |         uint8_t* const data = mData+mDataPos; | 
 | 564 |  | 
 | 565 |         // Need to pad at end? | 
 | 566 |         if (padded != len) { | 
 | 567 | #if BYTE_ORDER == BIG_ENDIAN | 
 | 568 |             static const uint32_t mask[4] = { | 
 | 569 |                 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 | 
 | 570 |             }; | 
 | 571 | #endif | 
 | 572 | #if BYTE_ORDER == LITTLE_ENDIAN | 
 | 573 |             static const uint32_t mask[4] = { | 
 | 574 |                 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff | 
 | 575 |             }; | 
 | 576 | #endif | 
 | 577 |             //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], | 
 | 578 |             //    *reinterpret_cast<void**>(data+padded-4)); | 
 | 579 |             *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; | 
 | 580 |         } | 
 | 581 |  | 
 | 582 |         finishWrite(padded); | 
 | 583 |         return data; | 
 | 584 |     } | 
 | 585 |  | 
 | 586 |     status_t err = growData(padded); | 
 | 587 |     if (err == NO_ERROR) goto restart_write; | 
 | 588 |     return NULL; | 
 | 589 | } | 
 | 590 |  | 
 | 591 | status_t Parcel::writeInt32(int32_t val) | 
 | 592 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 593 |     return writeAligned(val); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 594 | } | 
 | 595 |  | 
 | 596 | status_t Parcel::writeInt64(int64_t val) | 
 | 597 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 598 |     return writeAligned(val); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 599 | } | 
 | 600 |  | 
 | 601 | status_t Parcel::writeFloat(float val) | 
 | 602 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 603 |     return writeAligned(val); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 604 | } | 
 | 605 |  | 
 | 606 | status_t Parcel::writeDouble(double val) | 
 | 607 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 608 |     return writeAligned(val); | 
 | 609 | } | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 |  | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 611 | status_t Parcel::writeIntPtr(intptr_t val) | 
 | 612 | { | 
 | 613 |     return writeAligned(val); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 614 | } | 
 | 615 |  | 
 | 616 | status_t Parcel::writeCString(const char* str) | 
 | 617 | { | 
 | 618 |     return write(str, strlen(str)+1); | 
 | 619 | } | 
 | 620 |  | 
 | 621 | status_t Parcel::writeString8(const String8& str) | 
 | 622 | { | 
 | 623 |     status_t err = writeInt32(str.bytes()); | 
| Pravat Dalbehera | d1dff8d | 2010-12-15 08:40:00 +0100 | [diff] [blame] | 624 |     // only write string if its length is more than zero characters, | 
 | 625 |     // as readString8 will only read if the length field is non-zero. | 
 | 626 |     // this is slightly different from how writeString16 works. | 
 | 627 |     if (str.bytes() > 0 && err == NO_ERROR) { | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 628 |         err = write(str.string(), str.bytes()+1); | 
 | 629 |     } | 
 | 630 |     return err; | 
 | 631 | } | 
 | 632 |  | 
 | 633 | status_t Parcel::writeString16(const String16& str) | 
 | 634 | { | 
 | 635 |     return writeString16(str.string(), str.size()); | 
 | 636 | } | 
 | 637 |  | 
 | 638 | status_t Parcel::writeString16(const char16_t* str, size_t len) | 
 | 639 | { | 
 | 640 |     if (str == NULL) return writeInt32(-1); | 
 | 641 |      | 
 | 642 |     status_t err = writeInt32(len); | 
 | 643 |     if (err == NO_ERROR) { | 
 | 644 |         len *= sizeof(char16_t); | 
 | 645 |         uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); | 
 | 646 |         if (data) { | 
 | 647 |             memcpy(data, str, len); | 
 | 648 |             *reinterpret_cast<char16_t*>(data+len) = 0; | 
 | 649 |             return NO_ERROR; | 
 | 650 |         } | 
 | 651 |         err = mError; | 
 | 652 |     } | 
 | 653 |     return err; | 
 | 654 | } | 
 | 655 |  | 
 | 656 | status_t Parcel::writeStrongBinder(const sp<IBinder>& val) | 
 | 657 | { | 
 | 658 |     return flatten_binder(ProcessState::self(), val, this); | 
 | 659 | } | 
 | 660 |  | 
 | 661 | status_t Parcel::writeWeakBinder(const wp<IBinder>& val) | 
 | 662 | { | 
 | 663 |     return flatten_binder(ProcessState::self(), val, this); | 
 | 664 | } | 
 | 665 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 666 | status_t Parcel::writeNativeHandle(const native_handle* handle) | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 667 | { | 
| Mathias Agopian | 1d0a95b | 2009-07-31 16:12:13 -0700 | [diff] [blame] | 668 |     if (!handle || handle->version != sizeof(native_handle)) | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 669 |         return BAD_TYPE; | 
 | 670 |  | 
 | 671 |     status_t err; | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 672 |     err = writeInt32(handle->numFds); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 673 |     if (err != NO_ERROR) return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 675 |     err = writeInt32(handle->numInts); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 676 |     if (err != NO_ERROR) return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 678 |     for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) | 
 | 679 |         err = writeDupFileDescriptor(handle->data[i]); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 |  | 
 | 681 |     if (err != NO_ERROR) { | 
 | 682 |         LOGD("write native handle, write dup fd failed"); | 
 | 683 |         return err; | 
 | 684 |     } | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 685 |     err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 686 |     return err; | 
 | 687 | } | 
 | 688 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 689 | status_t Parcel::writeFileDescriptor(int fd) | 
 | 690 | { | 
 | 691 |     flat_binder_object obj; | 
 | 692 |     obj.type = BINDER_TYPE_FD; | 
 | 693 |     obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; | 
 | 694 |     obj.handle = fd; | 
 | 695 |     obj.cookie = (void*)0; | 
 | 696 |     return writeObject(obj, true); | 
 | 697 | } | 
 | 698 |  | 
 | 699 | status_t Parcel::writeDupFileDescriptor(int fd) | 
 | 700 | { | 
 | 701 |     flat_binder_object obj; | 
 | 702 |     obj.type = BINDER_TYPE_FD; | 
 | 703 |     obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; | 
 | 704 |     obj.handle = dup(fd); | 
 | 705 |     obj.cookie = (void*)1; | 
 | 706 |     return writeObject(obj, true); | 
 | 707 | } | 
 | 708 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 709 | status_t Parcel::write(const Flattenable& val) | 
 | 710 | { | 
 | 711 |     status_t err; | 
 | 712 |  | 
 | 713 |     // size if needed | 
 | 714 |     size_t len = val.getFlattenedSize(); | 
 | 715 |     size_t fd_count = val.getFdCount(); | 
 | 716 |  | 
 | 717 |     err = this->writeInt32(len); | 
 | 718 |     if (err) return err; | 
 | 719 |  | 
 | 720 |     err = this->writeInt32(fd_count); | 
 | 721 |     if (err) return err; | 
 | 722 |  | 
 | 723 |     // payload | 
 | 724 |     void* buf = this->writeInplace(PAD_SIZE(len)); | 
 | 725 |     if (buf == NULL) | 
 | 726 |         return BAD_VALUE; | 
 | 727 |  | 
 | 728 |     int* fds = NULL; | 
 | 729 |     if (fd_count) { | 
 | 730 |         fds = new int[fd_count]; | 
 | 731 |     } | 
 | 732 |  | 
 | 733 |     err = val.flatten(buf, len, fds, fd_count); | 
 | 734 |     for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { | 
 | 735 |         err = this->writeDupFileDescriptor( fds[i] ); | 
 | 736 |     } | 
 | 737 |  | 
 | 738 |     if (fd_count) { | 
 | 739 |         delete [] fds; | 
 | 740 |     } | 
 | 741 |  | 
 | 742 |     return err; | 
 | 743 | } | 
 | 744 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) | 
 | 746 | { | 
 | 747 |     const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; | 
 | 748 |     const bool enoughObjects = mObjectsSize < mObjectsCapacity; | 
 | 749 |     if (enoughData && enoughObjects) { | 
 | 750 | restart_write: | 
 | 751 |         *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; | 
 | 752 |          | 
 | 753 |         // Need to write meta-data? | 
 | 754 |         if (nullMetaData || val.binder != NULL) { | 
 | 755 |             mObjects[mObjectsSize] = mDataPos; | 
 | 756 |             acquire_object(ProcessState::self(), val, this); | 
 | 757 |             mObjectsSize++; | 
 | 758 |         } | 
 | 759 |          | 
 | 760 |         // remember if it's a file descriptor | 
 | 761 |         if (val.type == BINDER_TYPE_FD) { | 
 | 762 |             mHasFds = mFdsKnown = true; | 
 | 763 |         } | 
 | 764 |  | 
 | 765 |         return finishWrite(sizeof(flat_binder_object)); | 
 | 766 |     } | 
 | 767 |  | 
 | 768 |     if (!enoughData) { | 
 | 769 |         const status_t err = growData(sizeof(val)); | 
 | 770 |         if (err != NO_ERROR) return err; | 
 | 771 |     } | 
 | 772 |     if (!enoughObjects) { | 
 | 773 |         size_t newSize = ((mObjectsSize+2)*3)/2; | 
 | 774 |         size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t)); | 
 | 775 |         if (objects == NULL) return NO_MEMORY; | 
 | 776 |         mObjects = objects; | 
 | 777 |         mObjectsCapacity = newSize; | 
 | 778 |     } | 
 | 779 |      | 
 | 780 |     goto restart_write; | 
 | 781 | } | 
 | 782 |  | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 783 | status_t Parcel::writeNoException() | 
 | 784 | { | 
 | 785 |     return writeInt32(0); | 
 | 786 | } | 
 | 787 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 788 | void Parcel::remove(size_t start, size_t amt) | 
 | 789 | { | 
 | 790 |     LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); | 
 | 791 | } | 
 | 792 |  | 
 | 793 | status_t Parcel::read(void* outData, size_t len) const | 
 | 794 | { | 
 | 795 |     if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { | 
 | 796 |         memcpy(outData, mData+mDataPos, len); | 
 | 797 |         mDataPos += PAD_SIZE(len); | 
 | 798 |         LOGV("read Setting data pos of %p to %d\n", this, mDataPos); | 
 | 799 |         return NO_ERROR; | 
 | 800 |     } | 
 | 801 |     return NOT_ENOUGH_DATA; | 
 | 802 | } | 
 | 803 |  | 
 | 804 | const void* Parcel::readInplace(size_t len) const | 
 | 805 | { | 
 | 806 |     if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { | 
 | 807 |         const void* data = mData+mDataPos; | 
 | 808 |         mDataPos += PAD_SIZE(len); | 
 | 809 |         LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos); | 
 | 810 |         return data; | 
 | 811 |     } | 
 | 812 |     return NULL; | 
 | 813 | } | 
 | 814 |  | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 815 | template<class T> | 
 | 816 | status_t Parcel::readAligned(T *pArg) const { | 
 | 817 |     COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); | 
 | 818 |  | 
 | 819 |     if ((mDataPos+sizeof(T)) <= mDataSize) { | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 820 |         const void* data = mData+mDataPos; | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 821 |         mDataPos += sizeof(T); | 
 | 822 |         *pArg =  *reinterpret_cast<const T*>(data); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 823 |         return NO_ERROR; | 
 | 824 |     } else { | 
 | 825 |         return NOT_ENOUGH_DATA; | 
 | 826 |     } | 
 | 827 | } | 
 | 828 |  | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 829 | template<class T> | 
 | 830 | T Parcel::readAligned() const { | 
 | 831 |     T result; | 
 | 832 |     if (readAligned(&result) != NO_ERROR) { | 
 | 833 |         result = 0; | 
 | 834 |     } | 
 | 835 |  | 
 | 836 |     return result; | 
 | 837 | } | 
 | 838 |  | 
 | 839 | template<class T> | 
 | 840 | status_t Parcel::writeAligned(T val) { | 
 | 841 |     COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); | 
 | 842 |  | 
 | 843 |     if ((mDataPos+sizeof(val)) <= mDataCapacity) { | 
 | 844 | restart_write: | 
 | 845 |         *reinterpret_cast<T*>(mData+mDataPos) = val; | 
 | 846 |         return finishWrite(sizeof(val)); | 
 | 847 |     } | 
 | 848 |  | 
 | 849 |     status_t err = growData(sizeof(val)); | 
 | 850 |     if (err == NO_ERROR) goto restart_write; | 
 | 851 |     return err; | 
 | 852 | } | 
 | 853 |  | 
 | 854 | status_t Parcel::readInt32(int32_t *pArg) const | 
 | 855 | { | 
 | 856 |     return readAligned(pArg); | 
 | 857 | } | 
 | 858 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 859 | int32_t Parcel::readInt32() const | 
 | 860 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 861 |     return readAligned<int32_t>(); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 862 | } | 
 | 863 |  | 
 | 864 |  | 
 | 865 | status_t Parcel::readInt64(int64_t *pArg) const | 
 | 866 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 867 |     return readAligned(pArg); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 868 | } | 
 | 869 |  | 
 | 870 |  | 
 | 871 | int64_t Parcel::readInt64() const | 
 | 872 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 873 |     return readAligned<int64_t>(); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 874 | } | 
 | 875 |  | 
 | 876 | status_t Parcel::readFloat(float *pArg) const | 
 | 877 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 878 |     return readAligned(pArg); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 879 | } | 
 | 880 |  | 
 | 881 |  | 
 | 882 | float Parcel::readFloat() const | 
 | 883 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 884 |     return readAligned<float>(); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 885 | } | 
 | 886 |  | 
 | 887 | status_t Parcel::readDouble(double *pArg) const | 
 | 888 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 889 |     return readAligned(pArg); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 890 | } | 
 | 891 |  | 
 | 892 |  | 
 | 893 | double Parcel::readDouble() const | 
 | 894 | { | 
| Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 895 |     return readAligned<double>(); | 
 | 896 | } | 
 | 897 |  | 
 | 898 | status_t Parcel::readIntPtr(intptr_t *pArg) const | 
 | 899 | { | 
 | 900 |     return readAligned(pArg); | 
 | 901 | } | 
 | 902 |  | 
 | 903 |  | 
 | 904 | intptr_t Parcel::readIntPtr() const | 
 | 905 | { | 
 | 906 |     return readAligned<intptr_t>(); | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 907 | } | 
 | 908 |  | 
 | 909 |  | 
 | 910 | const char* Parcel::readCString() const | 
 | 911 | { | 
 | 912 |     const size_t avail = mDataSize-mDataPos; | 
 | 913 |     if (avail > 0) { | 
 | 914 |         const char* str = reinterpret_cast<const char*>(mData+mDataPos); | 
 | 915 |         // is the string's trailing NUL within the parcel's valid bounds? | 
 | 916 |         const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); | 
 | 917 |         if (eos) { | 
 | 918 |             const size_t len = eos - str; | 
 | 919 |             mDataPos += PAD_SIZE(len+1); | 
 | 920 |             LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos); | 
 | 921 |             return str; | 
 | 922 |         } | 
 | 923 |     } | 
 | 924 |     return NULL; | 
 | 925 | } | 
 | 926 |  | 
 | 927 | String8 Parcel::readString8() const | 
 | 928 | { | 
 | 929 |     int32_t size = readInt32(); | 
 | 930 |     // watch for potential int overflow adding 1 for trailing NUL | 
 | 931 |     if (size > 0 && size < INT32_MAX) { | 
 | 932 |         const char* str = (const char*)readInplace(size+1); | 
 | 933 |         if (str) return String8(str, size); | 
 | 934 |     } | 
 | 935 |     return String8(); | 
 | 936 | } | 
 | 937 |  | 
 | 938 | String16 Parcel::readString16() const | 
 | 939 | { | 
 | 940 |     size_t len; | 
 | 941 |     const char16_t* str = readString16Inplace(&len); | 
 | 942 |     if (str) return String16(str, len); | 
 | 943 |     LOGE("Reading a NULL string not supported here."); | 
 | 944 |     return String16(); | 
 | 945 | } | 
 | 946 |  | 
 | 947 | const char16_t* Parcel::readString16Inplace(size_t* outLen) const | 
 | 948 | { | 
 | 949 |     int32_t size = readInt32(); | 
 | 950 |     // watch for potential int overflow from size+1 | 
 | 951 |     if (size >= 0 && size < INT32_MAX) { | 
 | 952 |         *outLen = size; | 
 | 953 |         const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); | 
 | 954 |         if (str != NULL) { | 
 | 955 |             return str; | 
 | 956 |         } | 
 | 957 |     } | 
 | 958 |     *outLen = 0; | 
 | 959 |     return NULL; | 
 | 960 | } | 
 | 961 |  | 
 | 962 | sp<IBinder> Parcel::readStrongBinder() const | 
 | 963 | { | 
 | 964 |     sp<IBinder> val; | 
 | 965 |     unflatten_binder(ProcessState::self(), *this, &val); | 
 | 966 |     return val; | 
 | 967 | } | 
 | 968 |  | 
 | 969 | wp<IBinder> Parcel::readWeakBinder() const | 
 | 970 | { | 
 | 971 |     wp<IBinder> val; | 
 | 972 |     unflatten_binder(ProcessState::self(), *this, &val); | 
 | 973 |     return val; | 
 | 974 | } | 
 | 975 |  | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 976 | int32_t Parcel::readExceptionCode() const | 
 | 977 | { | 
 | 978 |   int32_t exception_code = readAligned<int32_t>(); | 
| Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 979 |   if (exception_code == EX_HAS_REPLY_HEADER) { | 
 | 980 |     int32_t header_size = readAligned<int32_t>(); | 
 | 981 |     // Skip over fat responses headers.  Not used (or propagated) in | 
 | 982 |     // native code | 
 | 983 |     setDataPosition(dataPosition() + header_size); | 
 | 984 |     // And fat response headers are currently only used when there are no | 
 | 985 |     // exceptions, so return no error: | 
 | 986 |     return 0; | 
 | 987 |   } | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 988 |   return exception_code; | 
 | 989 | } | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 990 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 991 | native_handle* Parcel::readNativeHandle() const | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 992 | { | 
 | 993 |     int numFds, numInts; | 
 | 994 |     status_t err; | 
 | 995 |     err = readInt32(&numFds); | 
 | 996 |     if (err != NO_ERROR) return 0; | 
 | 997 |     err = readInt32(&numInts); | 
 | 998 |     if (err != NO_ERROR) return 0; | 
 | 999 |  | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1000 |     native_handle* h = native_handle_create(numFds, numInts); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1001 |     for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { | 
| Rebecca Schultz Zavin | 360211f | 2009-02-13 16:34:38 -0800 | [diff] [blame] | 1002 |         h->data[i] = dup(readFileDescriptor()); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1003 |         if (h->data[i] < 0) err = BAD_VALUE; | 
 | 1004 |     } | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1005 |     err = read(h->data + numFds, sizeof(int)*numInts); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1006 |     if (err != NO_ERROR) { | 
| Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1007 |         native_handle_close(h); | 
 | 1008 |         native_handle_delete(h); | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1009 |         h = 0; | 
 | 1010 |     } | 
 | 1011 |     return h; | 
 | 1012 | } | 
 | 1013 |  | 
 | 1014 |  | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1015 | int Parcel::readFileDescriptor() const | 
 | 1016 | { | 
 | 1017 |     const flat_binder_object* flat = readObject(true); | 
 | 1018 |     if (flat) { | 
 | 1019 |         switch (flat->type) { | 
 | 1020 |             case BINDER_TYPE_FD: | 
 | 1021 |                 //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this); | 
 | 1022 |                 return flat->handle; | 
 | 1023 |         }         | 
 | 1024 |     } | 
 | 1025 |     return BAD_TYPE; | 
 | 1026 | } | 
 | 1027 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1028 | status_t Parcel::read(Flattenable& val) const | 
 | 1029 | { | 
 | 1030 |     // size | 
 | 1031 |     const size_t len = this->readInt32(); | 
 | 1032 |     const size_t fd_count = this->readInt32(); | 
 | 1033 |  | 
 | 1034 |     // payload | 
 | 1035 |     void const* buf = this->readInplace(PAD_SIZE(len)); | 
 | 1036 |     if (buf == NULL) | 
 | 1037 |         return BAD_VALUE; | 
 | 1038 |  | 
 | 1039 |     int* fds = NULL; | 
 | 1040 |     if (fd_count) { | 
 | 1041 |         fds = new int[fd_count]; | 
 | 1042 |     } | 
 | 1043 |  | 
 | 1044 |     status_t err = NO_ERROR; | 
 | 1045 |     for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { | 
 | 1046 |         fds[i] = dup(this->readFileDescriptor()); | 
 | 1047 |         if (fds[i] < 0) err = BAD_VALUE; | 
 | 1048 |     } | 
 | 1049 |  | 
 | 1050 |     if (err == NO_ERROR) { | 
 | 1051 |         err = val.unflatten(buf, len, fds, fd_count); | 
 | 1052 |     } | 
 | 1053 |  | 
 | 1054 |     if (fd_count) { | 
 | 1055 |         delete [] fds; | 
 | 1056 |     } | 
 | 1057 |  | 
 | 1058 |     return err; | 
 | 1059 | } | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1060 | const flat_binder_object* Parcel::readObject(bool nullMetaData) const | 
 | 1061 | { | 
 | 1062 |     const size_t DPOS = mDataPos; | 
 | 1063 |     if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { | 
 | 1064 |         const flat_binder_object* obj | 
 | 1065 |                 = reinterpret_cast<const flat_binder_object*>(mData+DPOS); | 
 | 1066 |         mDataPos = DPOS + sizeof(flat_binder_object); | 
 | 1067 |         if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) { | 
| The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1068 |             // When transferring a NULL object, we don't write it into | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1069 |             // the object list, so we don't want to check for it when | 
 | 1070 |             // reading. | 
 | 1071 |             LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1072 |             return obj; | 
 | 1073 |         } | 
 | 1074 |          | 
 | 1075 |         // Ensure that this object is valid... | 
 | 1076 |         size_t* const OBJS = mObjects; | 
 | 1077 |         const size_t N = mObjectsSize; | 
 | 1078 |         size_t opos = mNextObjectHint; | 
 | 1079 |          | 
 | 1080 |         if (N > 0) { | 
 | 1081 |             LOGV("Parcel %p looking for obj at %d, hint=%d\n", | 
 | 1082 |                  this, DPOS, opos); | 
 | 1083 |              | 
 | 1084 |             // Start at the current hint position, looking for an object at | 
 | 1085 |             // the current data position. | 
 | 1086 |             if (opos < N) { | 
 | 1087 |                 while (opos < (N-1) && OBJS[opos] < DPOS) { | 
 | 1088 |                     opos++; | 
 | 1089 |                 } | 
 | 1090 |             } else { | 
 | 1091 |                 opos = N-1; | 
 | 1092 |             } | 
 | 1093 |             if (OBJS[opos] == DPOS) { | 
 | 1094 |                 // Found it! | 
 | 1095 |                 LOGV("Parcel found obj %d at index %d with forward search", | 
 | 1096 |                      this, DPOS, opos); | 
 | 1097 |                 mNextObjectHint = opos+1; | 
 | 1098 |                 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1099 |                 return obj; | 
 | 1100 |             } | 
 | 1101 |          | 
 | 1102 |             // Look backwards for it... | 
 | 1103 |             while (opos > 0 && OBJS[opos] > DPOS) { | 
 | 1104 |                 opos--; | 
 | 1105 |             } | 
 | 1106 |             if (OBJS[opos] == DPOS) { | 
 | 1107 |                 // Found it! | 
 | 1108 |                 LOGV("Parcel found obj %d at index %d with backward search", | 
 | 1109 |                      this, DPOS, opos); | 
 | 1110 |                 mNextObjectHint = opos+1; | 
 | 1111 |                 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1112 |                 return obj; | 
 | 1113 |             } | 
 | 1114 |         } | 
 | 1115 |         LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list", | 
 | 1116 |              this, DPOS); | 
 | 1117 |     } | 
 | 1118 |     return NULL; | 
 | 1119 | } | 
 | 1120 |  | 
 | 1121 | void Parcel::closeFileDescriptors() | 
 | 1122 | { | 
 | 1123 |     size_t i = mObjectsSize; | 
 | 1124 |     if (i > 0) { | 
 | 1125 |         //LOGI("Closing file descriptors for %d objects...", mObjectsSize); | 
 | 1126 |     } | 
 | 1127 |     while (i > 0) { | 
 | 1128 |         i--; | 
 | 1129 |         const flat_binder_object* flat | 
 | 1130 |             = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); | 
 | 1131 |         if (flat->type == BINDER_TYPE_FD) { | 
 | 1132 |             //LOGI("Closing fd: %ld\n", flat->handle); | 
 | 1133 |             close(flat->handle); | 
 | 1134 |         } | 
 | 1135 |     } | 
 | 1136 | } | 
 | 1137 |  | 
 | 1138 | const uint8_t* Parcel::ipcData() const | 
 | 1139 | { | 
 | 1140 |     return mData; | 
 | 1141 | } | 
 | 1142 |  | 
 | 1143 | size_t Parcel::ipcDataSize() const | 
 | 1144 | { | 
 | 1145 |     return (mDataSize > mDataPos ? mDataSize : mDataPos); | 
 | 1146 | } | 
 | 1147 |  | 
 | 1148 | const size_t* Parcel::ipcObjects() const | 
 | 1149 | { | 
 | 1150 |     return mObjects; | 
 | 1151 | } | 
 | 1152 |  | 
 | 1153 | size_t Parcel::ipcObjectsCount() const | 
 | 1154 | { | 
 | 1155 |     return mObjectsSize; | 
 | 1156 | } | 
 | 1157 |  | 
 | 1158 | void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, | 
 | 1159 |     const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) | 
 | 1160 | { | 
 | 1161 |     freeDataNoInit(); | 
 | 1162 |     mError = NO_ERROR; | 
 | 1163 |     mData = const_cast<uint8_t*>(data); | 
 | 1164 |     mDataSize = mDataCapacity = dataSize; | 
 | 1165 |     //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid()); | 
 | 1166 |     mDataPos = 0; | 
 | 1167 |     LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1168 |     mObjects = const_cast<size_t*>(objects); | 
 | 1169 |     mObjectsSize = mObjectsCapacity = objectsCount; | 
 | 1170 |     mNextObjectHint = 0; | 
 | 1171 |     mOwner = relFunc; | 
 | 1172 |     mOwnerCookie = relCookie; | 
 | 1173 |     scanForFds(); | 
 | 1174 | } | 
 | 1175 |  | 
 | 1176 | void Parcel::print(TextOutput& to, uint32_t flags) const | 
 | 1177 | { | 
 | 1178 |     to << "Parcel("; | 
 | 1179 |      | 
 | 1180 |     if (errorCheck() != NO_ERROR) { | 
 | 1181 |         const status_t err = errorCheck(); | 
 | 1182 |         to << "Error: " << (void*)err << " \"" << strerror(-err) << "\""; | 
 | 1183 |     } else if (dataSize() > 0) { | 
 | 1184 |         const uint8_t* DATA = data(); | 
 | 1185 |         to << indent << HexDump(DATA, dataSize()) << dedent; | 
 | 1186 |         const size_t* OBJS = objects(); | 
 | 1187 |         const size_t N = objectsCount(); | 
 | 1188 |         for (size_t i=0; i<N; i++) { | 
 | 1189 |             const flat_binder_object* flat | 
 | 1190 |                 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); | 
 | 1191 |             to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " | 
 | 1192 |                 << TypeCode(flat->type & 0x7f7f7f00) | 
 | 1193 |                 << " = " << flat->binder; | 
 | 1194 |         } | 
 | 1195 |     } else { | 
 | 1196 |         to << "NULL"; | 
 | 1197 |     } | 
 | 1198 |      | 
 | 1199 |     to << ")"; | 
 | 1200 | } | 
 | 1201 |  | 
 | 1202 | void Parcel::releaseObjects() | 
 | 1203 | { | 
 | 1204 |     const sp<ProcessState> proc(ProcessState::self()); | 
 | 1205 |     size_t i = mObjectsSize; | 
 | 1206 |     uint8_t* const data = mData; | 
 | 1207 |     size_t* const objects = mObjects; | 
 | 1208 |     while (i > 0) { | 
 | 1209 |         i--; | 
 | 1210 |         const flat_binder_object* flat | 
 | 1211 |             = reinterpret_cast<flat_binder_object*>(data+objects[i]); | 
 | 1212 |         release_object(proc, *flat, this); | 
 | 1213 |     } | 
 | 1214 | } | 
 | 1215 |  | 
 | 1216 | void Parcel::acquireObjects() | 
 | 1217 | { | 
 | 1218 |     const sp<ProcessState> proc(ProcessState::self()); | 
 | 1219 |     size_t i = mObjectsSize; | 
 | 1220 |     uint8_t* const data = mData; | 
 | 1221 |     size_t* const objects = mObjects; | 
 | 1222 |     while (i > 0) { | 
 | 1223 |         i--; | 
 | 1224 |         const flat_binder_object* flat | 
 | 1225 |             = reinterpret_cast<flat_binder_object*>(data+objects[i]); | 
 | 1226 |         acquire_object(proc, *flat, this); | 
 | 1227 |     } | 
 | 1228 | } | 
 | 1229 |  | 
 | 1230 | void Parcel::freeData() | 
 | 1231 | { | 
 | 1232 |     freeDataNoInit(); | 
 | 1233 |     initState(); | 
 | 1234 | } | 
 | 1235 |  | 
 | 1236 | void Parcel::freeDataNoInit() | 
 | 1237 | { | 
 | 1238 |     if (mOwner) { | 
 | 1239 |         //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); | 
 | 1240 |         mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); | 
 | 1241 |     } else { | 
 | 1242 |         releaseObjects(); | 
 | 1243 |         if (mData) free(mData); | 
 | 1244 |         if (mObjects) free(mObjects); | 
 | 1245 |     } | 
 | 1246 | } | 
 | 1247 |  | 
 | 1248 | status_t Parcel::growData(size_t len) | 
 | 1249 | { | 
 | 1250 |     size_t newSize = ((mDataSize+len)*3)/2; | 
 | 1251 |     return (newSize <= mDataSize) | 
 | 1252 |             ? (status_t) NO_MEMORY | 
 | 1253 |             : continueWrite(newSize); | 
 | 1254 | } | 
 | 1255 |  | 
 | 1256 | status_t Parcel::restartWrite(size_t desired) | 
 | 1257 | { | 
 | 1258 |     if (mOwner) { | 
 | 1259 |         freeData(); | 
 | 1260 |         return continueWrite(desired); | 
 | 1261 |     } | 
 | 1262 |      | 
 | 1263 |     uint8_t* data = (uint8_t*)realloc(mData, desired); | 
 | 1264 |     if (!data && desired > mDataCapacity) { | 
 | 1265 |         mError = NO_MEMORY; | 
 | 1266 |         return NO_MEMORY; | 
 | 1267 |     } | 
 | 1268 |      | 
 | 1269 |     releaseObjects(); | 
 | 1270 |      | 
 | 1271 |     if (data) { | 
 | 1272 |         mData = data; | 
 | 1273 |         mDataCapacity = desired; | 
 | 1274 |     } | 
 | 1275 |      | 
 | 1276 |     mDataSize = mDataPos = 0; | 
 | 1277 |     LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize); | 
 | 1278 |     LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1279 |          | 
 | 1280 |     free(mObjects); | 
 | 1281 |     mObjects = NULL; | 
 | 1282 |     mObjectsSize = mObjectsCapacity = 0; | 
 | 1283 |     mNextObjectHint = 0; | 
 | 1284 |     mHasFds = false; | 
 | 1285 |     mFdsKnown = true; | 
 | 1286 |      | 
 | 1287 |     return NO_ERROR; | 
 | 1288 | } | 
 | 1289 |  | 
 | 1290 | status_t Parcel::continueWrite(size_t desired) | 
 | 1291 | { | 
 | 1292 |     // If shrinking, first adjust for any objects that appear | 
 | 1293 |     // after the new data size. | 
 | 1294 |     size_t objectsSize = mObjectsSize; | 
 | 1295 |     if (desired < mDataSize) { | 
 | 1296 |         if (desired == 0) { | 
 | 1297 |             objectsSize = 0; | 
 | 1298 |         } else { | 
 | 1299 |             while (objectsSize > 0) { | 
 | 1300 |                 if (mObjects[objectsSize-1] < desired) | 
 | 1301 |                     break; | 
 | 1302 |                 objectsSize--; | 
 | 1303 |             } | 
 | 1304 |         } | 
 | 1305 |     } | 
 | 1306 |      | 
 | 1307 |     if (mOwner) { | 
 | 1308 |         // If the size is going to zero, just release the owner's data. | 
 | 1309 |         if (desired == 0) { | 
 | 1310 |             freeData(); | 
 | 1311 |             return NO_ERROR; | 
 | 1312 |         } | 
 | 1313 |  | 
 | 1314 |         // If there is a different owner, we need to take | 
 | 1315 |         // posession. | 
 | 1316 |         uint8_t* data = (uint8_t*)malloc(desired); | 
 | 1317 |         if (!data) { | 
 | 1318 |             mError = NO_MEMORY; | 
 | 1319 |             return NO_MEMORY; | 
 | 1320 |         } | 
 | 1321 |         size_t* objects = NULL; | 
 | 1322 |          | 
 | 1323 |         if (objectsSize) { | 
 | 1324 |             objects = (size_t*)malloc(objectsSize*sizeof(size_t)); | 
 | 1325 |             if (!objects) { | 
 | 1326 |                 mError = NO_MEMORY; | 
 | 1327 |                 return NO_MEMORY; | 
 | 1328 |             } | 
 | 1329 |  | 
 | 1330 |             // Little hack to only acquire references on objects | 
 | 1331 |             // we will be keeping. | 
 | 1332 |             size_t oldObjectsSize = mObjectsSize; | 
 | 1333 |             mObjectsSize = objectsSize; | 
 | 1334 |             acquireObjects(); | 
 | 1335 |             mObjectsSize = oldObjectsSize; | 
 | 1336 |         } | 
 | 1337 |          | 
 | 1338 |         if (mData) { | 
 | 1339 |             memcpy(data, mData, mDataSize < desired ? mDataSize : desired); | 
 | 1340 |         } | 
 | 1341 |         if (objects && mObjects) { | 
 | 1342 |             memcpy(objects, mObjects, objectsSize*sizeof(size_t)); | 
 | 1343 |         } | 
 | 1344 |         //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); | 
 | 1345 |         mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); | 
 | 1346 |         mOwner = NULL; | 
 | 1347 |  | 
 | 1348 |         mData = data; | 
 | 1349 |         mObjects = objects; | 
 | 1350 |         mDataSize = (mDataSize < desired) ? mDataSize : desired; | 
 | 1351 |         LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); | 
 | 1352 |         mDataCapacity = desired; | 
 | 1353 |         mObjectsSize = mObjectsCapacity = objectsSize; | 
 | 1354 |         mNextObjectHint = 0; | 
 | 1355 |  | 
 | 1356 |     } else if (mData) { | 
 | 1357 |         if (objectsSize < mObjectsSize) { | 
 | 1358 |             // Need to release refs on any objects we are dropping. | 
 | 1359 |             const sp<ProcessState> proc(ProcessState::self()); | 
 | 1360 |             for (size_t i=objectsSize; i<mObjectsSize; i++) { | 
 | 1361 |                 const flat_binder_object* flat | 
 | 1362 |                     = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); | 
 | 1363 |                 if (flat->type == BINDER_TYPE_FD) { | 
 | 1364 |                     // will need to rescan because we may have lopped off the only FDs | 
 | 1365 |                     mFdsKnown = false; | 
 | 1366 |                 } | 
 | 1367 |                 release_object(proc, *flat, this); | 
 | 1368 |             } | 
 | 1369 |             size_t* objects = | 
 | 1370 |                 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t)); | 
 | 1371 |             if (objects) { | 
 | 1372 |                 mObjects = objects; | 
 | 1373 |             } | 
 | 1374 |             mObjectsSize = objectsSize; | 
 | 1375 |             mNextObjectHint = 0; | 
 | 1376 |         } | 
 | 1377 |  | 
 | 1378 |         // We own the data, so we can just do a realloc(). | 
 | 1379 |         if (desired > mDataCapacity) { | 
 | 1380 |             uint8_t* data = (uint8_t*)realloc(mData, desired); | 
 | 1381 |             if (data) { | 
 | 1382 |                 mData = data; | 
 | 1383 |                 mDataCapacity = desired; | 
 | 1384 |             } else if (desired > mDataCapacity) { | 
 | 1385 |                 mError = NO_MEMORY; | 
 | 1386 |                 return NO_MEMORY; | 
 | 1387 |             } | 
 | 1388 |         } else { | 
| Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1389 |             if (mDataSize > desired) { | 
 | 1390 |                 mDataSize = desired; | 
 | 1391 |                 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); | 
 | 1392 |             } | 
| The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1393 |             if (mDataPos > desired) { | 
 | 1394 |                 mDataPos = desired; | 
 | 1395 |                 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1396 |             } | 
 | 1397 |         } | 
 | 1398 |          | 
 | 1399 |     } else { | 
 | 1400 |         // This is the first data.  Easy! | 
 | 1401 |         uint8_t* data = (uint8_t*)malloc(desired); | 
 | 1402 |         if (!data) { | 
 | 1403 |             mError = NO_MEMORY; | 
 | 1404 |             return NO_MEMORY; | 
 | 1405 |         } | 
 | 1406 |          | 
 | 1407 |         if(!(mDataCapacity == 0 && mObjects == NULL | 
 | 1408 |              && mObjectsCapacity == 0)) { | 
 | 1409 |             LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired); | 
 | 1410 |         } | 
 | 1411 |          | 
 | 1412 |         mData = data; | 
 | 1413 |         mDataSize = mDataPos = 0; | 
 | 1414 |         LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); | 
 | 1415 |         LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1416 |         mDataCapacity = desired; | 
 | 1417 |     } | 
 | 1418 |  | 
 | 1419 |     return NO_ERROR; | 
 | 1420 | } | 
 | 1421 |  | 
 | 1422 | void Parcel::initState() | 
 | 1423 | { | 
 | 1424 |     mError = NO_ERROR; | 
 | 1425 |     mData = 0; | 
 | 1426 |     mDataSize = 0; | 
 | 1427 |     mDataCapacity = 0; | 
 | 1428 |     mDataPos = 0; | 
 | 1429 |     LOGV("initState Setting data size of %p to %d\n", this, mDataSize); | 
 | 1430 |     LOGV("initState Setting data pos of %p to %d\n", this, mDataPos); | 
 | 1431 |     mObjects = NULL; | 
 | 1432 |     mObjectsSize = 0; | 
 | 1433 |     mObjectsCapacity = 0; | 
 | 1434 |     mNextObjectHint = 0; | 
 | 1435 |     mHasFds = false; | 
 | 1436 |     mFdsKnown = true; | 
 | 1437 |     mOwner = NULL; | 
 | 1438 | } | 
 | 1439 |  | 
 | 1440 | void Parcel::scanForFds() const | 
 | 1441 | { | 
 | 1442 |     bool hasFds = false; | 
 | 1443 |     for (size_t i=0; i<mObjectsSize; i++) { | 
 | 1444 |         const flat_binder_object* flat | 
 | 1445 |             = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); | 
 | 1446 |         if (flat->type == BINDER_TYPE_FD) { | 
 | 1447 |             hasFds = true; | 
 | 1448 |             break; | 
 | 1449 |         } | 
 | 1450 |     } | 
 | 1451 |     mHasFds = hasFds; | 
 | 1452 |     mFdsKnown = true; | 
 | 1453 | } | 
 | 1454 |  | 
 | 1455 | }; // namespace android |