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