blob: 406fee02272b3d5cdbef84b0da504ca311138b0f [file] [log] [blame]
Samuel Tan0a312022015-11-23 18:22:12 -08001/*
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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Samuel Tan0a312022015-11-23 18:22:12 -080026#include <utils/Errors.h>
27
Steven Moreland491e1332019-07-16 16:43:18 -070028#include "ParcelValTypes.h"
29
Samuel Tan0a312022015-11-23 18:22:12 -080030using android::BAD_TYPE;
31using android::BAD_VALUE;
32using android::NO_ERROR;
33using android::Parcel;
Samuel Tan0a312022015-11-23 18:22:12 -080034using android::status_t;
35using android::UNEXPECTED_NULL;
Jiyong Parkea2e23f2020-11-13 17:26:33 +090036
37using android::binder::VAL_BOOLEAN;
38using android::binder::VAL_INTEGER;
39using android::binder::VAL_LONG;
40using android::binder::VAL_DOUBLE;
41using android::binder::VAL_STRING;
42using android::binder::VAL_BOOLEANARRAY;
43using android::binder::VAL_INTARRAY;
44using android::binder::VAL_LONGARRAY;
45using android::binder::VAL_DOUBLEARRAY;
46using android::binder::VAL_STRINGARRAY;
47using android::binder::VAL_PERSISTABLEBUNDLE;
48
Samuel Tan52545f22016-02-12 13:56:17 -080049using std::map;
50using std::set;
51using std::vector;
Samuel Tan0a312022015-11-23 18:22:12 -080052
53enum {
Makoto Onuki39696bd2018-04-26 09:11:28 -070054 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
Samuel Tan0a312022015-11-23 18:22:12 -080055 BUNDLE_MAGIC = 0x4C444E42,
Makoto Onuki39696bd2018-04-26 09:11:28 -070056 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
Samuel Tan0a312022015-11-23 18:22:12 -080057};
58
Samuel Tan0a312022015-11-23 18:22:12 -080059namespace {
60template <typename T>
Samuel Tan52545f22016-02-12 13:56:17 -080061bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
Samuel Tan0a312022015-11-23 18:22:12 -080062 const auto& it = map.find(key);
63 if (it == map.end()) return false;
64 *out = it->second;
65 return true;
66}
Samuel Tan52545f22016-02-12 13:56:17 -080067
68template <typename T>
69set<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}
Samuel Tan0a312022015-11-23 18:22:12 -080077} // namespace
78
79namespace android {
80
81namespace 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 { \
Chih-Hung Hsieh8f6b9b32016-05-20 11:30:10 -070094 size_t num_erased = (map).erase(key); \
Samuel Tan0a312022015-11-23 18:22:12 -080095 if (num_erased) { \
96 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
97 return num_erased; \
98 } \
99 }
100
101status_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
Makoto Onuki39696bd2018-04-26 09:11:28 -0700115 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
Samuel Tan0a312022015-11-23 18:22:12 -0800116
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()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800125 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
Samuel Tan0a312022015-11-23 18:22:12 -0800126 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
133status_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
147bool PersistableBundle::empty() const {
148 return size() == 0u;
149}
150
151size_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
165size_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
179void PersistableBundle::putBoolean(const String16& key, bool value) {
180 erase(key);
181 mBoolMap[key] = value;
182}
183
184void PersistableBundle::putInt(const String16& key, int32_t value) {
185 erase(key);
186 mIntMap[key] = value;
187}
188
189void PersistableBundle::putLong(const String16& key, int64_t value) {
190 erase(key);
191 mLongMap[key] = value;
192}
193
194void PersistableBundle::putDouble(const String16& key, double value) {
195 erase(key);
196 mDoubleMap[key] = value;
197}
198
199void PersistableBundle::putString(const String16& key, const String16& value) {
200 erase(key);
201 mStringMap[key] = value;
202}
203
Samuel Tan52545f22016-02-12 13:56:17 -0800204void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800205 erase(key);
206 mBoolVectorMap[key] = value;
207}
208
Samuel Tan52545f22016-02-12 13:56:17 -0800209void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800210 erase(key);
211 mIntVectorMap[key] = value;
212}
213
Samuel Tan52545f22016-02-12 13:56:17 -0800214void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800215 erase(key);
216 mLongVectorMap[key] = value;
217}
218
Samuel Tan52545f22016-02-12 13:56:17 -0800219void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800220 erase(key);
221 mDoubleVectorMap[key] = value;
222}
223
Samuel Tan52545f22016-02-12 13:56:17 -0800224void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800225 erase(key);
226 mStringVectorMap[key] = value;
227}
228
229void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
230 erase(key);
231 mPersistableBundleMap[key] = value;
232}
233
234bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
235 return getValue(key, out, mBoolMap);
236}
237
238bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
239 return getValue(key, out, mIntMap);
240}
241
242bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
243 return getValue(key, out, mLongMap);
244}
245
246bool PersistableBundle::getDouble(const String16& key, double* out) const {
247 return getValue(key, out, mDoubleMap);
248}
249
250bool PersistableBundle::getString(const String16& key, String16* out) const {
251 return getValue(key, out, mStringMap);
252}
253
Samuel Tan52545f22016-02-12 13:56:17 -0800254bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800255 return getValue(key, out, mBoolVectorMap);
256}
257
Samuel Tan52545f22016-02-12 13:56:17 -0800258bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800259 return getValue(key, out, mIntVectorMap);
260}
261
Samuel Tan52545f22016-02-12 13:56:17 -0800262bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800263 return getValue(key, out, mLongVectorMap);
264}
265
Samuel Tan52545f22016-02-12 13:56:17 -0800266bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800267 return getValue(key, out, mDoubleVectorMap);
268}
269
Samuel Tan52545f22016-02-12 13:56:17 -0800270bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800271 return getValue(key, out, mStringVectorMap);
272}
273
274bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
275 return getValue(key, out, mPersistableBundleMap);
276}
277
Samuel Tan52545f22016-02-12 13:56:17 -0800278set<String16> PersistableBundle::getBooleanKeys() const {
279 return getKeys(mBoolMap);
280}
281
282set<String16> PersistableBundle::getIntKeys() const {
283 return getKeys(mIntMap);
284}
285
286set<String16> PersistableBundle::getLongKeys() const {
287 return getKeys(mLongMap);
288}
289
290set<String16> PersistableBundle::getDoubleKeys() const {
291 return getKeys(mDoubleMap);
292}
293
294set<String16> PersistableBundle::getStringKeys() const {
295 return getKeys(mStringMap);
296}
297
298set<String16> PersistableBundle::getBooleanVectorKeys() const {
299 return getKeys(mBoolVectorMap);
300}
301
302set<String16> PersistableBundle::getIntVectorKeys() const {
303 return getKeys(mIntVectorMap);
304}
305
306set<String16> PersistableBundle::getLongVectorKeys() const {
307 return getKeys(mLongVectorMap);
308}
309
310set<String16> PersistableBundle::getDoubleVectorKeys() const {
311 return getKeys(mDoubleVectorMap);
312}
313
314set<String16> PersistableBundle::getStringVectorKeys() const {
315 return getKeys(mStringVectorMap);
316}
317
318set<String16> PersistableBundle::getPersistableBundleKeys() const {
319 return getKeys(mPersistableBundleMap);
320}
321
Samuel Tan0a312022015-11-23 18:22:12 -0800322status_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()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800331 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
Samuel Tan0a312022015-11-23 18:22:12 -0800332 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
395status_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));
Makoto Onuki39696bd2018-04-26 09:11:28 -0700408 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
Samuel Tan0a312022015-11-23 18:22:12 -0800409 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) {
Samuel Tan0a312022015-11-23 18:22:12 -0800423 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