1 /*
2 * Copyright (C) 2015 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 "PersistableBundle"
18
19 #include <binder/PersistableBundle.h>
20
21 #include <limits>
22
23 #include <binder/IBinder.h>
24 #include <binder/Parcel.h>
25 #include <log/log.h>
26 #include <utils/Errors.h>
27
28 #include "ParcelValTypes.h"
29
30 using android::BAD_TYPE;
31 using android::BAD_VALUE;
32 using android::NO_ERROR;
33 using android::Parcel;
34 using android::status_t;
35 using android::UNEXPECTED_NULL;
36
37 using android::binder::VAL_BOOLEAN;
38 using android::binder::VAL_INTEGER;
39 using android::binder::VAL_LONG;
40 using android::binder::VAL_DOUBLE;
41 using android::binder::VAL_STRING;
42 using android::binder::VAL_BOOLEANARRAY;
43 using android::binder::VAL_INTARRAY;
44 using android::binder::VAL_LONGARRAY;
45 using android::binder::VAL_DOUBLEARRAY;
46 using android::binder::VAL_STRINGARRAY;
47 using android::binder::VAL_PERSISTABLEBUNDLE;
48
49 using std::map;
50 using std::set;
51 using std::vector;
52
53 enum {
54 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
55 BUNDLE_MAGIC = 0x4C444E42,
56 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
57 };
58
59 namespace {
60 template <typename T>
getValue(const android::String16 & key,T * out,const map<android::String16,T> & map)61 bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
62 const auto& it = map.find(key);
63 if (it == map.end()) return false;
64 *out = it->second;
65 return true;
66 }
67
68 template <typename T>
getKeys(const map<android::String16,T> & map)69 set<android::String16> getKeys(const map<android::String16, T>& map) {
70 if (map.empty()) return set<android::String16>();
71 set<android::String16> keys;
72 for (const auto& key_value_pair : map) {
73 keys.emplace(key_value_pair.first);
74 }
75 return keys;
76 }
77 } // namespace
78
79 namespace android {
80
81 namespace os {
82
83 #define RETURN_IF_FAILED(calledOnce) \
84 { \
85 status_t returnStatus = calledOnce; \
86 if (returnStatus) { \
87 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
88 return returnStatus; \
89 } \
90 }
91
92 #define RETURN_IF_ENTRY_ERASED(map, key) \
93 { \
94 size_t num_erased = (map).erase(key); \
95 if (num_erased) { \
96 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
97 return num_erased; \
98 } \
99 }
100
writeToParcel(Parcel * parcel) const101 status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
102 /*
103 * Keep implementation in sync with writeToParcelInner() in
104 * frameworks/base/core/java/android/os/BaseBundle.java.
105 */
106
107 // Special case for empty bundles.
108 if (empty()) {
109 RETURN_IF_FAILED(parcel->writeInt32(0));
110 return NO_ERROR;
111 }
112
113 size_t length_pos = parcel->dataPosition();
114 RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
115 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
116
117 size_t start_pos = parcel->dataPosition();
118 RETURN_IF_FAILED(writeToParcelInner(parcel));
119 size_t end_pos = parcel->dataPosition();
120
121 // Backpatch length. This length value includes the length header.
122 parcel->setDataPosition(length_pos);
123 size_t length = end_pos - start_pos;
124 if (length > std::numeric_limits<int32_t>::max()) {
125 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
126 return BAD_VALUE;
127 }
128 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
129 parcel->setDataPosition(end_pos);
130 return NO_ERROR;
131 }
132
readFromParcel(const Parcel * parcel)133 status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
134 /*
135 * Keep implementation in sync with readFromParcelInner() in
136 * frameworks/base/core/java/android/os/BaseBundle.java.
137 */
138 int32_t length = parcel->readInt32();
139 if (length < 0) {
140 ALOGE("Bad length in parcel: %d", length);
141 return UNEXPECTED_NULL;
142 }
143
144 return readFromParcelInner(parcel, static_cast<size_t>(length));
145 }
146
empty() const147 bool PersistableBundle::empty() const {
148 return size() == 0u;
149 }
150
size() const151 size_t PersistableBundle::size() const {
152 return (mBoolMap.size() +
153 mIntMap.size() +
154 mLongMap.size() +
155 mDoubleMap.size() +
156 mStringMap.size() +
157 mBoolVectorMap.size() +
158 mIntVectorMap.size() +
159 mLongVectorMap.size() +
160 mDoubleVectorMap.size() +
161 mStringVectorMap.size() +
162 mPersistableBundleMap.size());
163 }
164
erase(const String16 & key)165 size_t PersistableBundle::erase(const String16& key) {
166 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
167 RETURN_IF_ENTRY_ERASED(mIntMap, key);
168 RETURN_IF_ENTRY_ERASED(mLongMap, key);
169 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
170 RETURN_IF_ENTRY_ERASED(mStringMap, key);
171 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
172 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
173 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
174 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
175 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
176 return mPersistableBundleMap.erase(key);
177 }
178
putBoolean(const String16 & key,bool value)179 void PersistableBundle::putBoolean(const String16& key, bool value) {
180 erase(key);
181 mBoolMap[key] = value;
182 }
183
putInt(const String16 & key,int32_t value)184 void PersistableBundle::putInt(const String16& key, int32_t value) {
185 erase(key);
186 mIntMap[key] = value;
187 }
188
putLong(const String16 & key,int64_t value)189 void PersistableBundle::putLong(const String16& key, int64_t value) {
190 erase(key);
191 mLongMap[key] = value;
192 }
193
putDouble(const String16 & key,double value)194 void PersistableBundle::putDouble(const String16& key, double value) {
195 erase(key);
196 mDoubleMap[key] = value;
197 }
198
putString(const String16 & key,const String16 & value)199 void PersistableBundle::putString(const String16& key, const String16& value) {
200 erase(key);
201 mStringMap[key] = value;
202 }
203
putBooleanVector(const String16 & key,const vector<bool> & value)204 void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
205 erase(key);
206 mBoolVectorMap[key] = value;
207 }
208
putIntVector(const String16 & key,const vector<int32_t> & value)209 void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
210 erase(key);
211 mIntVectorMap[key] = value;
212 }
213
putLongVector(const String16 & key,const vector<int64_t> & value)214 void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
215 erase(key);
216 mLongVectorMap[key] = value;
217 }
218
putDoubleVector(const String16 & key,const vector<double> & value)219 void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
220 erase(key);
221 mDoubleVectorMap[key] = value;
222 }
223
putStringVector(const String16 & key,const vector<String16> & value)224 void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
225 erase(key);
226 mStringVectorMap[key] = value;
227 }
228
putPersistableBundle(const String16 & key,const PersistableBundle & value)229 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
230 erase(key);
231 mPersistableBundleMap[key] = value;
232 }
233
getBoolean(const String16 & key,bool * out) const234 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
235 return getValue(key, out, mBoolMap);
236 }
237
getInt(const String16 & key,int32_t * out) const238 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
239 return getValue(key, out, mIntMap);
240 }
241
getLong(const String16 & key,int64_t * out) const242 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
243 return getValue(key, out, mLongMap);
244 }
245
getDouble(const String16 & key,double * out) const246 bool PersistableBundle::getDouble(const String16& key, double* out) const {
247 return getValue(key, out, mDoubleMap);
248 }
249
getString(const String16 & key,String16 * out) const250 bool PersistableBundle::getString(const String16& key, String16* out) const {
251 return getValue(key, out, mStringMap);
252 }
253
getBooleanVector(const String16 & key,vector<bool> * out) const254 bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
255 return getValue(key, out, mBoolVectorMap);
256 }
257
getIntVector(const String16 & key,vector<int32_t> * out) const258 bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
259 return getValue(key, out, mIntVectorMap);
260 }
261
getLongVector(const String16 & key,vector<int64_t> * out) const262 bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
263 return getValue(key, out, mLongVectorMap);
264 }
265
getDoubleVector(const String16 & key,vector<double> * out) const266 bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
267 return getValue(key, out, mDoubleVectorMap);
268 }
269
getStringVector(const String16 & key,vector<String16> * out) const270 bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
271 return getValue(key, out, mStringVectorMap);
272 }
273
getPersistableBundle(const String16 & key,PersistableBundle * out) const274 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
275 return getValue(key, out, mPersistableBundleMap);
276 }
277
getBooleanKeys() const278 set<String16> PersistableBundle::getBooleanKeys() const {
279 return getKeys(mBoolMap);
280 }
281
getIntKeys() const282 set<String16> PersistableBundle::getIntKeys() const {
283 return getKeys(mIntMap);
284 }
285
getLongKeys() const286 set<String16> PersistableBundle::getLongKeys() const {
287 return getKeys(mLongMap);
288 }
289
getDoubleKeys() const290 set<String16> PersistableBundle::getDoubleKeys() const {
291 return getKeys(mDoubleMap);
292 }
293
getStringKeys() const294 set<String16> PersistableBundle::getStringKeys() const {
295 return getKeys(mStringMap);
296 }
297
getBooleanVectorKeys() const298 set<String16> PersistableBundle::getBooleanVectorKeys() const {
299 return getKeys(mBoolVectorMap);
300 }
301
getIntVectorKeys() const302 set<String16> PersistableBundle::getIntVectorKeys() const {
303 return getKeys(mIntVectorMap);
304 }
305
getLongVectorKeys() const306 set<String16> PersistableBundle::getLongVectorKeys() const {
307 return getKeys(mLongVectorMap);
308 }
309
getDoubleVectorKeys() const310 set<String16> PersistableBundle::getDoubleVectorKeys() const {
311 return getKeys(mDoubleVectorMap);
312 }
313
getStringVectorKeys() const314 set<String16> PersistableBundle::getStringVectorKeys() const {
315 return getKeys(mStringVectorMap);
316 }
317
getPersistableBundleKeys() const318 set<String16> PersistableBundle::getPersistableBundleKeys() const {
319 return getKeys(mPersistableBundleMap);
320 }
321
writeToParcelInner(Parcel * parcel) const322 status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
323 /*
324 * To keep this implementation in sync with writeArrayMapInternal() in
325 * frameworks/base/core/java/android/os/Parcel.java, the number of key
326 * value pairs must be written into the parcel before writing the key-value
327 * pairs themselves.
328 */
329 size_t num_entries = size();
330 if (num_entries > std::numeric_limits<int32_t>::max()) {
331 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
332 num_entries);
333 return BAD_VALUE;
334 }
335 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
336
337 for (const auto& key_val_pair : mBoolMap) {
338 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
339 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
340 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
341 }
342 for (const auto& key_val_pair : mIntMap) {
343 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
344 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
345 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
346 }
347 for (const auto& key_val_pair : mLongMap) {
348 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
349 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
350 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
351 }
352 for (const auto& key_val_pair : mDoubleMap) {
353 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
354 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
355 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
356 }
357 for (const auto& key_val_pair : mStringMap) {
358 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
359 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
360 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
361 }
362 for (const auto& key_val_pair : mBoolVectorMap) {
363 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
364 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
365 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
366 }
367 for (const auto& key_val_pair : mIntVectorMap) {
368 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
369 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
370 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
371 }
372 for (const auto& key_val_pair : mLongVectorMap) {
373 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
374 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
375 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
376 }
377 for (const auto& key_val_pair : mDoubleVectorMap) {
378 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
379 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
380 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
381 }
382 for (const auto& key_val_pair : mStringVectorMap) {
383 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
384 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
385 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
386 }
387 for (const auto& key_val_pair : mPersistableBundleMap) {
388 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
389 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
390 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
391 }
392 return NO_ERROR;
393 }
394
readFromParcelInner(const Parcel * parcel,size_t length)395 status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
396 /*
397 * Note: we don't actually use length for anything other than an empty PersistableBundle
398 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
399 * implementation.
400 */
401 if (length == 0) {
402 // Empty PersistableBundle or end of data.
403 return NO_ERROR;
404 }
405
406 int32_t magic;
407 RETURN_IF_FAILED(parcel->readInt32(&magic));
408 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
409 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
410 return BAD_VALUE;
411 }
412
413 /*
414 * To keep this implementation in sync with unparcel() in
415 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
416 * key-value pairs must be read from the parcel before reading the key-value
417 * pairs themselves.
418 */
419 int32_t num_entries;
420 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
421
422 for (; num_entries > 0; --num_entries) {
423 String16 key;
424 int32_t value_type;
425 RETURN_IF_FAILED(parcel->readString16(&key));
426 RETURN_IF_FAILED(parcel->readInt32(&value_type));
427
428 /*
429 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
430 * are unique.
431 */
432 switch (value_type) {
433 case VAL_STRING: {
434 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
435 break;
436 }
437 case VAL_INTEGER: {
438 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
439 break;
440 }
441 case VAL_LONG: {
442 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
443 break;
444 }
445 case VAL_DOUBLE: {
446 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
447 break;
448 }
449 case VAL_BOOLEAN: {
450 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
451 break;
452 }
453 case VAL_STRINGARRAY: {
454 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
455 break;
456 }
457 case VAL_INTARRAY: {
458 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
459 break;
460 }
461 case VAL_LONGARRAY: {
462 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
463 break;
464 }
465 case VAL_BOOLEANARRAY: {
466 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
467 break;
468 }
469 case VAL_PERSISTABLEBUNDLE: {
470 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
471 break;
472 }
473 case VAL_DOUBLEARRAY: {
474 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
475 break;
476 }
477 default: {
478 ALOGE("Unrecognized type: %d", value_type);
479 return BAD_TYPE;
480 break;
481 }
482 }
483 }
484
485 return NO_ERROR;
486 }
487
488 } // namespace os
489
490 } // namespace android
491