blob: 52fe9734478e0ea0153a07e1790716dcd852272c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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 "ResourceType"
18//#define LOG_NDEBUG 0
19
Dan Albert1b4f3162015-04-07 18:43:15 -070020#include <ctype.h>
21#include <memory.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <limits>
Adam Lesinskiff5808d2016-02-23 17:49:53 -080028#include <memory>
Dan Albert1b4f3162015-04-07 18:43:15 -070029#include <type_traits>
30
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070031#include <androidfw/ByteBucketArray.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080032#include <androidfw/ResourceTypes.h>
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070033#include <androidfw/TypeWrappers.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <utils/Atomic.h>
35#include <utils/ByteOrder.h>
36#include <utils/Debug.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080037#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include <utils/String16.h>
39#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Dan Albert1b4f3162015-04-07 18:43:15 -070041#ifdef __ANDROID__
Andreas Gampe2204f0b2014-10-21 23:04:54 -070042#include <binder/TextOutput.h>
43#endif
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#ifndef INT32_MAX
46#define INT32_MAX ((int32_t)(2147483647))
47#endif
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049namespace android {
50
Elliott Hughes59cbe8d2015-07-29 17:49:27 -070051#if defined(_WIN32)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#undef nhtol
53#undef htonl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054#define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
55#define htonl(x) ntohl(x)
56#define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
57#define htons(x) ntohs(x)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058#endif
59
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070060#define IDMAP_MAGIC 0x504D4449
61#define IDMAP_CURRENT_VERSION 0x00000001
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +010062
Adam Lesinskide898ff2014-01-29 18:20:45 -080063#define APP_PACKAGE_ID 0x7f
64#define SYS_PACKAGE_ID 0x01
65
Andreas Gampe2204f0b2014-10-21 23:04:54 -070066static const bool kDebugStringPoolNoisy = false;
67static const bool kDebugXMLNoisy = false;
68static const bool kDebugTableNoisy = false;
69static const bool kDebugTableGetEntry = false;
70static const bool kDebugTableSuperNoisy = false;
71static const bool kDebugLoadTableNoisy = false;
72static const bool kDebugLoadTableSuperNoisy = false;
73static const bool kDebugTableTheme = false;
74static const bool kDebugResXMLTree = false;
75static const bool kDebugLibNoisy = false;
76
77// TODO: This code uses 0xFFFFFFFF converted to bag_set* as a sentinel value. This is bad practice.
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079// Standard C isspace() is only required to look at the low byte of its input, so
80// produces incorrect results for UTF-16 characters. For safety's sake, assume that
81// any high-byte UTF-16 code point is not whitespace.
82inline int isspace16(char16_t c) {
83 return (c < 0x0080 && isspace(c));
84}
85
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070086template<typename T>
87inline static T max(T a, T b) {
88 return a > b ? a : b;
89}
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091// range checked; guaranteed to NUL-terminate within the stated number of available slots
92// NOTE: if this truncates the dst string due to running out of space, no attempt is
93// made to avoid splitting surrogate pairs.
Adam Lesinski4bf58102014-11-03 11:21:19 -080094static void strcpy16_dtoh(char16_t* dst, const uint16_t* src, size_t avail)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095{
Dan Albertf348c152014-09-08 18:28:00 -070096 char16_t* last = dst + avail - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 while (*src && (dst < last)) {
Adam Lesinski4bf58102014-11-03 11:21:19 -080098 char16_t s = dtohs(static_cast<char16_t>(*src));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 *dst++ = s;
100 src++;
101 }
102 *dst = 0;
103}
104
105static status_t validate_chunk(const ResChunk_header* chunk,
106 size_t minSize,
107 const uint8_t* dataEnd,
108 const char* name)
109{
110 const uint16_t headerSize = dtohs(chunk->headerSize);
111 const uint32_t size = dtohl(chunk->size);
112
113 if (headerSize >= minSize) {
114 if (headerSize <= size) {
115 if (((headerSize|size)&0x3) == 0) {
Adam Lesinski7322ea72014-05-14 11:43:26 -0700116 if ((size_t)size <= (size_t)(dataEnd-((const uint8_t*)chunk))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 return NO_ERROR;
118 }
Patrik Bannura443dd932014-02-12 13:38:54 +0100119 ALOGW("%s data size 0x%x extends beyond resource end %p.",
120 name, size, (void*)(dataEnd-((const uint8_t*)chunk)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 return BAD_TYPE;
122 }
Steve Block8564c8d2012-01-05 23:22:43 +0000123 ALOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 name, (int)size, (int)headerSize);
125 return BAD_TYPE;
126 }
Patrik Bannura443dd932014-02-12 13:38:54 +0100127 ALOGW("%s size 0x%x is smaller than header size 0x%x.",
128 name, size, headerSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 return BAD_TYPE;
130 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800131 ALOGW("%s header size 0x%04x is too small.",
Patrik Bannura443dd932014-02-12 13:38:54 +0100132 name, headerSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return BAD_TYPE;
134}
135
Narayan Kamath6381dd42014-03-03 17:12:03 +0000136static void fill9patchOffsets(Res_png_9patch* patch) {
137 patch->xDivsOffset = sizeof(Res_png_9patch);
138 patch->yDivsOffset = patch->xDivsOffset + (patch->numXDivs * sizeof(int32_t));
139 patch->colorsOffset = patch->yDivsOffset + (patch->numYDivs * sizeof(int32_t));
140}
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142inline void Res_value::copyFrom_dtoh(const Res_value& src)
143{
144 size = dtohs(src.size);
145 res0 = src.res0;
146 dataType = src.dataType;
147 data = dtohl(src.data);
148}
149
150void Res_png_9patch::deviceToFile()
151{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000152 int32_t* xDivs = getXDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 for (int i = 0; i < numXDivs; i++) {
154 xDivs[i] = htonl(xDivs[i]);
155 }
Narayan Kamath6381dd42014-03-03 17:12:03 +0000156 int32_t* yDivs = getYDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 for (int i = 0; i < numYDivs; i++) {
158 yDivs[i] = htonl(yDivs[i]);
159 }
160 paddingLeft = htonl(paddingLeft);
161 paddingRight = htonl(paddingRight);
162 paddingTop = htonl(paddingTop);
163 paddingBottom = htonl(paddingBottom);
Narayan Kamath6381dd42014-03-03 17:12:03 +0000164 uint32_t* colors = getColors();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 for (int i=0; i<numColors; i++) {
166 colors[i] = htonl(colors[i]);
167 }
168}
169
170void Res_png_9patch::fileToDevice()
171{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000172 int32_t* xDivs = getXDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 for (int i = 0; i < numXDivs; i++) {
174 xDivs[i] = ntohl(xDivs[i]);
175 }
Narayan Kamath6381dd42014-03-03 17:12:03 +0000176 int32_t* yDivs = getYDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 for (int i = 0; i < numYDivs; i++) {
178 yDivs[i] = ntohl(yDivs[i]);
179 }
180 paddingLeft = ntohl(paddingLeft);
181 paddingRight = ntohl(paddingRight);
182 paddingTop = ntohl(paddingTop);
183 paddingBottom = ntohl(paddingBottom);
Narayan Kamath6381dd42014-03-03 17:12:03 +0000184 uint32_t* colors = getColors();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 for (int i=0; i<numColors; i++) {
186 colors[i] = ntohl(colors[i]);
187 }
188}
189
Narayan Kamath6381dd42014-03-03 17:12:03 +0000190size_t Res_png_9patch::serializedSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191{
192 // The size of this struct is 32 bytes on the 32-bit target system
193 // 4 * int8_t
194 // 4 * int32_t
Narayan Kamath6381dd42014-03-03 17:12:03 +0000195 // 3 * uint32_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 return 32
197 + numXDivs * sizeof(int32_t)
198 + numYDivs * sizeof(int32_t)
199 + numColors * sizeof(uint32_t);
200}
201
Narayan Kamath6381dd42014-03-03 17:12:03 +0000202void* Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
203 const int32_t* yDivs, const uint32_t* colors)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204{
The Android Open Source Project4df24232009-03-05 14:34:35 -0800205 // Use calloc since we're going to leave a few holes in the data
206 // and want this to run cleanly under valgrind
Narayan Kamath6381dd42014-03-03 17:12:03 +0000207 void* newData = calloc(1, patch.serializedSize());
208 serialize(patch, xDivs, yDivs, colors, newData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 return newData;
210}
211
Narayan Kamath6381dd42014-03-03 17:12:03 +0000212void Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
213 const int32_t* yDivs, const uint32_t* colors, void* outData)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000215 uint8_t* data = (uint8_t*) outData;
216 memcpy(data, &patch.wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors
217 memcpy(data + 12, &patch.paddingLeft, 16); // copy paddingXXXX
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 data += 32;
219
Narayan Kamath6381dd42014-03-03 17:12:03 +0000220 memcpy(data, xDivs, patch.numXDivs * sizeof(int32_t));
221 data += patch.numXDivs * sizeof(int32_t);
222 memcpy(data, yDivs, patch.numYDivs * sizeof(int32_t));
223 data += patch.numYDivs * sizeof(int32_t);
224 memcpy(data, colors, patch.numColors * sizeof(uint32_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Narayan Kamath6381dd42014-03-03 17:12:03 +0000226 fill9patchOffsets(reinterpret_cast<Res_png_9patch*>(outData));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700229static bool assertIdmapHeader(const void* idmap, size_t size) {
230 if (reinterpret_cast<uintptr_t>(idmap) & 0x03) {
231 ALOGE("idmap: header is not word aligned");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100232 return false;
233 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700234
235 if (size < ResTable::IDMAP_HEADER_SIZE_BYTES) {
236 ALOGW("idmap: header too small (%d bytes)", (uint32_t) size);
237 return false;
238 }
239
240 const uint32_t magic = htodl(*reinterpret_cast<const uint32_t*>(idmap));
241 if (magic != IDMAP_MAGIC) {
242 ALOGW("idmap: no magic found in header (is 0x%08x, expected 0x%08x)",
243 magic, IDMAP_MAGIC);
244 return false;
245 }
246
247 const uint32_t version = htodl(*(reinterpret_cast<const uint32_t*>(idmap) + 1));
248 if (version != IDMAP_CURRENT_VERSION) {
249 // We are strict about versions because files with this format are
250 // auto-generated and don't need backwards compatibility.
251 ALOGW("idmap: version mismatch in header (is 0x%08x, expected 0x%08x)",
252 version, IDMAP_CURRENT_VERSION);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100253 return false;
254 }
255 return true;
256}
257
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700258class IdmapEntries {
259public:
260 IdmapEntries() : mData(NULL) {}
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100261
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700262 bool hasEntries() const {
263 if (mData == NULL) {
264 return false;
265 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100266
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700267 return (dtohs(*mData) > 0);
268 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100269
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700270 size_t byteSize() const {
271 if (mData == NULL) {
272 return 0;
273 }
274 uint16_t entryCount = dtohs(mData[2]);
275 return (sizeof(uint16_t) * 4) + (sizeof(uint32_t) * static_cast<size_t>(entryCount));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100276 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700277
278 uint8_t targetTypeId() const {
279 if (mData == NULL) {
280 return 0;
281 }
282 return dtohs(mData[0]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100283 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700284
285 uint8_t overlayTypeId() const {
286 if (mData == NULL) {
287 return 0;
288 }
289 return dtohs(mData[1]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100290 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700291
292 status_t setTo(const void* entryHeader, size_t size) {
293 if (reinterpret_cast<uintptr_t>(entryHeader) & 0x03) {
294 ALOGE("idmap: entry header is not word aligned");
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100295 return UNKNOWN_ERROR;
296 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700297
298 if (size < sizeof(uint16_t) * 4) {
299 ALOGE("idmap: entry header is too small (%u bytes)", (uint32_t) size);
300 return UNKNOWN_ERROR;
301 }
302
303 const uint16_t* header = reinterpret_cast<const uint16_t*>(entryHeader);
304 const uint16_t targetTypeId = dtohs(header[0]);
305 const uint16_t overlayTypeId = dtohs(header[1]);
306 if (targetTypeId == 0 || overlayTypeId == 0 || targetTypeId > 255 || overlayTypeId > 255) {
307 ALOGE("idmap: invalid type map (%u -> %u)", targetTypeId, overlayTypeId);
308 return UNKNOWN_ERROR;
309 }
310
311 uint16_t entryCount = dtohs(header[2]);
312 if (size < sizeof(uint32_t) * (entryCount + 2)) {
313 ALOGE("idmap: too small (%u bytes) for the number of entries (%u)",
314 (uint32_t) size, (uint32_t) entryCount);
315 return UNKNOWN_ERROR;
316 }
317 mData = header;
318 return NO_ERROR;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100319 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100320
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700321 status_t lookup(uint16_t entryId, uint16_t* outEntryId) const {
322 uint16_t entryCount = dtohs(mData[2]);
323 uint16_t offset = dtohs(mData[3]);
324
325 if (entryId < offset) {
326 // The entry is not present in this idmap
327 return BAD_INDEX;
328 }
329
330 entryId -= offset;
331
332 if (entryId >= entryCount) {
333 // The entry is not present in this idmap
334 return BAD_INDEX;
335 }
336
337 // It is safe to access the type here without checking the size because
338 // we have checked this when it was first loaded.
339 const uint32_t* entries = reinterpret_cast<const uint32_t*>(mData) + 2;
340 uint32_t mappedEntry = dtohl(entries[entryId]);
341 if (mappedEntry == 0xffffffff) {
342 // This entry is not present in this idmap
343 return BAD_INDEX;
344 }
345 *outEntryId = static_cast<uint16_t>(mappedEntry);
346 return NO_ERROR;
347 }
348
349private:
350 const uint16_t* mData;
351};
352
353status_t parseIdmap(const void* idmap, size_t size, uint8_t* outPackageId, KeyedVector<uint8_t, IdmapEntries>* outMap) {
354 if (!assertIdmapHeader(idmap, size)) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100355 return UNKNOWN_ERROR;
356 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100357
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700358 size -= ResTable::IDMAP_HEADER_SIZE_BYTES;
359 if (size < sizeof(uint16_t) * 2) {
360 ALOGE("idmap: too small to contain any mapping");
361 return UNKNOWN_ERROR;
362 }
363
364 const uint16_t* data = reinterpret_cast<const uint16_t*>(
365 reinterpret_cast<const uint8_t*>(idmap) + ResTable::IDMAP_HEADER_SIZE_BYTES);
366
367 uint16_t targetPackageId = dtohs(*(data++));
368 if (targetPackageId == 0 || targetPackageId > 255) {
369 ALOGE("idmap: target package ID is invalid (%02x)", targetPackageId);
370 return UNKNOWN_ERROR;
371 }
372
373 uint16_t mapCount = dtohs(*(data++));
374 if (mapCount == 0) {
375 ALOGE("idmap: no mappings");
376 return UNKNOWN_ERROR;
377 }
378
379 if (mapCount > 255) {
380 ALOGW("idmap: too many mappings. Only 255 are possible but %u are present", (uint32_t) mapCount);
381 }
382
383 while (size > sizeof(uint16_t) * 4) {
384 IdmapEntries entries;
385 status_t err = entries.setTo(data, size);
386 if (err != NO_ERROR) {
387 return err;
388 }
389
390 ssize_t index = outMap->add(entries.overlayTypeId(), entries);
391 if (index < 0) {
392 return NO_MEMORY;
393 }
394
395 data += entries.byteSize() / sizeof(uint16_t);
396 size -= entries.byteSize();
397 }
398
399 if (outPackageId != NULL) {
400 *outPackageId = static_cast<uint8_t>(targetPackageId);
401 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100402 return NO_ERROR;
403}
404
Narayan Kamath6381dd42014-03-03 17:12:03 +0000405Res_png_9patch* Res_png_9patch::deserialize(void* inData)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000407
408 Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(inData);
409 patch->wasDeserialized = true;
410 fill9patchOffsets(patch);
411
412 return patch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413}
414
415// --------------------------------------------------------------------
416// --------------------------------------------------------------------
417// --------------------------------------------------------------------
418
419ResStringPool::ResStringPool()
Kenny Root19138462009-12-04 09:38:48 -0800420 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421{
422}
423
424ResStringPool::ResStringPool(const void* data, size_t size, bool copyData)
Kenny Root19138462009-12-04 09:38:48 -0800425 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426{
427 setTo(data, size, copyData);
428}
429
430ResStringPool::~ResStringPool()
431{
432 uninit();
433}
434
Adam Lesinskide898ff2014-01-29 18:20:45 -0800435void ResStringPool::setToEmpty()
436{
437 uninit();
438
439 mOwnedData = calloc(1, sizeof(ResStringPool_header));
440 ResStringPool_header* header = (ResStringPool_header*) mOwnedData;
441 mSize = 0;
442 mEntries = NULL;
443 mStrings = NULL;
444 mStringPoolSize = 0;
445 mEntryStyles = NULL;
446 mStyles = NULL;
447 mStylePoolSize = 0;
448 mHeader = (const ResStringPool_header*) header;
449}
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451status_t ResStringPool::setTo(const void* data, size_t size, bool copyData)
452{
453 if (!data || !size) {
454 return (mError=BAD_TYPE);
455 }
456
457 uninit();
458
459 const bool notDeviceEndian = htods(0xf0) != 0xf0;
460
461 if (copyData || notDeviceEndian) {
462 mOwnedData = malloc(size);
463 if (mOwnedData == NULL) {
464 return (mError=NO_MEMORY);
465 }
466 memcpy(mOwnedData, data, size);
467 data = mOwnedData;
468 }
469
470 mHeader = (const ResStringPool_header*)data;
471
472 if (notDeviceEndian) {
473 ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader);
474 h->header.headerSize = dtohs(mHeader->header.headerSize);
475 h->header.type = dtohs(mHeader->header.type);
476 h->header.size = dtohl(mHeader->header.size);
477 h->stringCount = dtohl(mHeader->stringCount);
478 h->styleCount = dtohl(mHeader->styleCount);
479 h->flags = dtohl(mHeader->flags);
480 h->stringsStart = dtohl(mHeader->stringsStart);
481 h->stylesStart = dtohl(mHeader->stylesStart);
482 }
483
484 if (mHeader->header.headerSize > mHeader->header.size
485 || mHeader->header.size > size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000486 ALOGW("Bad string block: header size %d or total size %d is larger than data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size);
488 return (mError=BAD_TYPE);
489 }
490 mSize = mHeader->header.size;
491 mEntries = (const uint32_t*)
492 (((const uint8_t*)data)+mHeader->header.headerSize);
493
494 if (mHeader->stringCount > 0) {
495 if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow?
496 || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t)))
497 > size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000498 ALOGW("Bad string block: entry of %d items extends past data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))),
500 (int)size);
501 return (mError=BAD_TYPE);
502 }
Kenny Root19138462009-12-04 09:38:48 -0800503
504 size_t charSize;
505 if (mHeader->flags&ResStringPool_header::UTF8_FLAG) {
506 charSize = sizeof(uint8_t);
Kenny Root19138462009-12-04 09:38:48 -0800507 } else {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800508 charSize = sizeof(uint16_t);
Kenny Root19138462009-12-04 09:38:48 -0800509 }
510
Adam Lesinskif28d5052014-07-25 15:25:04 -0700511 // There should be at least space for the smallest string
512 // (2 bytes length, null terminator).
513 if (mHeader->stringsStart >= (mSize - sizeof(uint16_t))) {
Steve Block8564c8d2012-01-05 23:22:43 +0000514 ALOGW("Bad string block: string pool starts at %d, after total size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 (int)mHeader->stringsStart, (int)mHeader->header.size);
516 return (mError=BAD_TYPE);
517 }
Adam Lesinskif28d5052014-07-25 15:25:04 -0700518
519 mStrings = (const void*)
520 (((const uint8_t*)data) + mHeader->stringsStart);
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (mHeader->styleCount == 0) {
Adam Lesinskif28d5052014-07-25 15:25:04 -0700523 mStringPoolSize = (mSize - mHeader->stringsStart) / charSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 } else {
Kenny Root5e4d9a02010-06-08 12:34:43 -0700525 // check invariant: styles starts before end of data
Adam Lesinskif28d5052014-07-25 15:25:04 -0700526 if (mHeader->stylesStart >= (mSize - sizeof(uint16_t))) {
Steve Block8564c8d2012-01-05 23:22:43 +0000527 ALOGW("Bad style block: style block starts at %d past data size of %d\n",
Kenny Root5e4d9a02010-06-08 12:34:43 -0700528 (int)mHeader->stylesStart, (int)mHeader->header.size);
529 return (mError=BAD_TYPE);
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 // check invariant: styles follow the strings
532 if (mHeader->stylesStart <= mHeader->stringsStart) {
Steve Block8564c8d2012-01-05 23:22:43 +0000533 ALOGW("Bad style block: style block starts at %d, before strings at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 (int)mHeader->stylesStart, (int)mHeader->stringsStart);
535 return (mError=BAD_TYPE);
536 }
537 mStringPoolSize =
Kenny Root19138462009-12-04 09:38:48 -0800538 (mHeader->stylesStart-mHeader->stringsStart)/charSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540
541 // check invariant: stringCount > 0 requires a string pool to exist
542 if (mStringPoolSize == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000543 ALOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 return (mError=BAD_TYPE);
545 }
546
547 if (notDeviceEndian) {
548 size_t i;
549 uint32_t* e = const_cast<uint32_t*>(mEntries);
550 for (i=0; i<mHeader->stringCount; i++) {
551 e[i] = dtohl(mEntries[i]);
552 }
Kenny Root19138462009-12-04 09:38:48 -0800553 if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800554 const uint16_t* strings = (const uint16_t*)mStrings;
555 uint16_t* s = const_cast<uint16_t*>(strings);
Kenny Root19138462009-12-04 09:38:48 -0800556 for (i=0; i<mStringPoolSize; i++) {
557 s[i] = dtohs(strings[i]);
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561
Kenny Root19138462009-12-04 09:38:48 -0800562 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG &&
563 ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) ||
564 (!mHeader->flags&ResStringPool_header::UTF8_FLAG &&
Adam Lesinski4bf58102014-11-03 11:21:19 -0800565 ((uint16_t*)mStrings)[mStringPoolSize-1] != 0)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000566 ALOGW("Bad string block: last string is not 0-terminated\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 return (mError=BAD_TYPE);
568 }
569 } else {
570 mStrings = NULL;
571 mStringPoolSize = 0;
572 }
573
574 if (mHeader->styleCount > 0) {
575 mEntryStyles = mEntries + mHeader->stringCount;
576 // invariant: integer overflow in calculating mEntryStyles
577 if (mEntryStyles < mEntries) {
Steve Block8564c8d2012-01-05 23:22:43 +0000578 ALOGW("Bad string block: integer overflow finding styles\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 return (mError=BAD_TYPE);
580 }
581
582 if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000583 ALOGW("Bad string block: entry of %d styles extends past data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader),
585 (int)size);
586 return (mError=BAD_TYPE);
587 }
588 mStyles = (const uint32_t*)
589 (((const uint8_t*)data)+mHeader->stylesStart);
590 if (mHeader->stylesStart >= mHeader->header.size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000591 ALOGW("Bad string block: style pool starts %d, after total size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 (int)mHeader->stylesStart, (int)mHeader->header.size);
593 return (mError=BAD_TYPE);
594 }
595 mStylePoolSize =
596 (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t);
597
598 if (notDeviceEndian) {
599 size_t i;
600 uint32_t* e = const_cast<uint32_t*>(mEntryStyles);
601 for (i=0; i<mHeader->styleCount; i++) {
602 e[i] = dtohl(mEntryStyles[i]);
603 }
604 uint32_t* s = const_cast<uint32_t*>(mStyles);
605 for (i=0; i<mStylePoolSize; i++) {
606 s[i] = dtohl(mStyles[i]);
607 }
608 }
609
610 const ResStringPool_span endSpan = {
611 { htodl(ResStringPool_span::END) },
612 htodl(ResStringPool_span::END), htodl(ResStringPool_span::END)
613 };
614 if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))],
615 &endSpan, sizeof(endSpan)) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000616 ALOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 return (mError=BAD_TYPE);
618 }
619 } else {
620 mEntryStyles = NULL;
621 mStyles = NULL;
622 mStylePoolSize = 0;
623 }
624
625 return (mError=NO_ERROR);
626}
627
628status_t ResStringPool::getError() const
629{
630 return mError;
631}
632
633void ResStringPool::uninit()
634{
635 mError = NO_INIT;
Kenny Root19138462009-12-04 09:38:48 -0800636 if (mHeader != NULL && mCache != NULL) {
637 for (size_t x = 0; x < mHeader->stringCount; x++) {
638 if (mCache[x] != NULL) {
639 free(mCache[x]);
640 mCache[x] = NULL;
641 }
642 }
643 free(mCache);
644 mCache = NULL;
645 }
Chris Dearmana1d82ff32012-10-08 12:22:02 -0700646 if (mOwnedData) {
647 free(mOwnedData);
648 mOwnedData = NULL;
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650}
651
Kenny Root300ba682010-11-09 14:37:23 -0800652/**
653 * Strings in UTF-16 format have length indicated by a length encoded in the
654 * stored data. It is either 1 or 2 characters of length data. This allows a
655 * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that
656 * much data in a string, you're abusing them.
657 *
658 * If the high bit is set, then there are two characters or 4 bytes of length
659 * data encoded. In that case, drop the high bit of the first character and
660 * add it together with the next character.
661 */
662static inline size_t
Adam Lesinski4bf58102014-11-03 11:21:19 -0800663decodeLength(const uint16_t** str)
Kenny Root300ba682010-11-09 14:37:23 -0800664{
665 size_t len = **str;
666 if ((len & 0x8000) != 0) {
667 (*str)++;
668 len = ((len & 0x7FFF) << 16) | **str;
669 }
670 (*str)++;
671 return len;
672}
Kenny Root19138462009-12-04 09:38:48 -0800673
Kenny Root300ba682010-11-09 14:37:23 -0800674/**
675 * Strings in UTF-8 format have length indicated by a length encoded in the
676 * stored data. It is either 1 or 2 characters of length data. This allows a
677 * maximum length of 0x7FFF (32767 bytes), but you should consider storing
678 * text in another way if you're using that much data in a single string.
679 *
680 * If the high bit is set, then there are two characters or 2 bytes of length
681 * data encoded. In that case, drop the high bit of the first character and
682 * add it together with the next character.
683 */
684static inline size_t
685decodeLength(const uint8_t** str)
686{
687 size_t len = **str;
688 if ((len & 0x80) != 0) {
689 (*str)++;
690 len = ((len & 0x7F) << 8) | **str;
691 }
692 (*str)++;
693 return len;
694}
695
Dan Albertf348c152014-09-08 18:28:00 -0700696const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697{
698 if (mError == NO_ERROR && idx < mHeader->stringCount) {
Kenny Root19138462009-12-04 09:38:48 -0800699 const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800700 const uint32_t off = mEntries[idx]/(isUTF8?sizeof(uint8_t):sizeof(uint16_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (off < (mStringPoolSize-1)) {
Kenny Root19138462009-12-04 09:38:48 -0800702 if (!isUTF8) {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800703 const uint16_t* strings = (uint16_t*)mStrings;
704 const uint16_t* str = strings+off;
Kenny Root300ba682010-11-09 14:37:23 -0800705
706 *u16len = decodeLength(&str);
707 if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) {
Vishwath Mohan6521a1b2015-03-11 16:08:37 -0700708 // Reject malformed (non null-terminated) strings
709 if (str[*u16len] != 0x0000) {
710 ALOGW("Bad string block: string #%d is not null-terminated",
711 (int)idx);
712 return NULL;
713 }
Adam Lesinski4bf58102014-11-03 11:21:19 -0800714 return reinterpret_cast<const char16_t*>(str);
Kenny Root19138462009-12-04 09:38:48 -0800715 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000716 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
Kenny Root300ba682010-11-09 14:37:23 -0800717 (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize);
Kenny Root19138462009-12-04 09:38:48 -0800718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 } else {
Kenny Root19138462009-12-04 09:38:48 -0800720 const uint8_t* strings = (uint8_t*)mStrings;
Kenny Root300ba682010-11-09 14:37:23 -0800721 const uint8_t* u8str = strings+off;
722
723 *u16len = decodeLength(&u8str);
724 size_t u8len = decodeLength(&u8str);
725
726 // encLen must be less than 0x7FFF due to encoding.
727 if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) {
Kenny Root19138462009-12-04 09:38:48 -0800728 AutoMutex lock(mDecodeLock);
Kenny Root300ba682010-11-09 14:37:23 -0800729
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700730 if (mCache == NULL) {
Elliott Hughesba3fe562015-08-12 14:49:53 -0700731#ifndef __ANDROID__
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700732 if (kDebugStringPoolNoisy) {
733 ALOGI("CREATING STRING CACHE OF %zu bytes",
734 mHeader->stringCount*sizeof(char16_t**));
735 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700736#else
737 // We do not want to be in this case when actually running Android.
Andreas Gampe25df5fb2014-11-07 22:24:57 -0800738 ALOGW("CREATING STRING CACHE OF %zu bytes",
739 static_cast<size_t>(mHeader->stringCount*sizeof(char16_t**)));
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700740#endif
741 mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t**));
742 if (mCache == NULL) {
743 ALOGW("No memory trying to allocate decode cache table of %d bytes\n",
744 (int)(mHeader->stringCount*sizeof(char16_t**)));
745 return NULL;
746 }
747 }
748
Kenny Root19138462009-12-04 09:38:48 -0800749 if (mCache[idx] != NULL) {
750 return mCache[idx];
751 }
Kenny Root300ba682010-11-09 14:37:23 -0800752
753 ssize_t actualLen = utf8_to_utf16_length(u8str, u8len);
754 if (actualLen < 0 || (size_t)actualLen != *u16len) {
Steve Block8564c8d2012-01-05 23:22:43 +0000755 ALOGW("Bad string block: string #%lld decoded length is not correct "
Kenny Root300ba682010-11-09 14:37:23 -0800756 "%lld vs %llu\n",
757 (long long)idx, (long long)actualLen, (long long)*u16len);
758 return NULL;
759 }
760
Vishwath Mohan6521a1b2015-03-11 16:08:37 -0700761 // Reject malformed (non null-terminated) strings
762 if (u8str[u8len] != 0x00) {
763 ALOGW("Bad string block: string #%d is not null-terminated",
764 (int)idx);
765 return NULL;
766 }
767
Kenny Root300ba682010-11-09 14:37:23 -0800768 char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t));
Kenny Root19138462009-12-04 09:38:48 -0800769 if (!u16str) {
Steve Block8564c8d2012-01-05 23:22:43 +0000770 ALOGW("No memory when trying to allocate decode cache for string #%d\n",
Kenny Root19138462009-12-04 09:38:48 -0800771 (int)idx);
772 return NULL;
773 }
Kenny Root300ba682010-11-09 14:37:23 -0800774
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700775 if (kDebugStringPoolNoisy) {
776 ALOGI("Caching UTF8 string: %s", u8str);
777 }
Kenny Root300ba682010-11-09 14:37:23 -0800778 utf8_to_utf16(u8str, u8len, u16str);
Kenny Root19138462009-12-04 09:38:48 -0800779 mCache[idx] = u16str;
780 return u16str;
781 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000782 ALOGW("Bad string block: string #%lld extends to %lld, past end at %lld\n",
Kenny Root300ba682010-11-09 14:37:23 -0800783 (long long)idx, (long long)(u8str+u8len-strings),
784 (long long)mStringPoolSize);
Kenny Root19138462009-12-04 09:38:48 -0800785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000788 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 (int)idx, (int)(off*sizeof(uint16_t)),
790 (int)(mStringPoolSize*sizeof(uint16_t)));
791 }
792 }
793 return NULL;
794}
795
Kenny Root780d2a12010-02-22 22:36:26 -0800796const char* ResStringPool::string8At(size_t idx, size_t* outLen) const
797{
798 if (mError == NO_ERROR && idx < mHeader->stringCount) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700799 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) == 0) {
800 return NULL;
801 }
802 const uint32_t off = mEntries[idx]/sizeof(char);
Kenny Root780d2a12010-02-22 22:36:26 -0800803 if (off < (mStringPoolSize-1)) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700804 const uint8_t* strings = (uint8_t*)mStrings;
805 const uint8_t* str = strings+off;
806 *outLen = decodeLength(&str);
807 size_t encLen = decodeLength(&str);
808 if ((uint32_t)(str+encLen-strings) < mStringPoolSize) {
809 return (const char*)str;
810 } else {
811 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
812 (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize);
Kenny Root780d2a12010-02-22 22:36:26 -0800813 }
814 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000815 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
Kenny Root780d2a12010-02-22 22:36:26 -0800816 (int)idx, (int)(off*sizeof(uint16_t)),
817 (int)(mStringPoolSize*sizeof(uint16_t)));
818 }
819 }
820 return NULL;
821}
822
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800823const String8 ResStringPool::string8ObjectAt(size_t idx) const
824{
825 size_t len;
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700826 const char *str = string8At(idx, &len);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800827 if (str != NULL) {
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700828 return String8(str, len);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800829 }
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700830
831 const char16_t *str16 = stringAt(idx, &len);
832 if (str16 != NULL) {
833 return String8(str16, len);
834 }
835 return String8();
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800836}
837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const
839{
840 return styleAt(ref.index);
841}
842
843const ResStringPool_span* ResStringPool::styleAt(size_t idx) const
844{
845 if (mError == NO_ERROR && idx < mHeader->styleCount) {
846 const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t));
847 if (off < mStylePoolSize) {
848 return (const ResStringPool_span*)(mStyles+off);
849 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000850 ALOGW("Bad string block: style #%d entry is at %d, past end at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 (int)idx, (int)(off*sizeof(uint32_t)),
852 (int)(mStylePoolSize*sizeof(uint32_t)));
853 }
854 }
855 return NULL;
856}
857
858ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
859{
860 if (mError != NO_ERROR) {
861 return mError;
862 }
863
864 size_t len;
865
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700866 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700867 if (kDebugStringPoolNoisy) {
868 ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string());
869 }
Kenny Root19138462009-12-04 09:38:48 -0800870
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700871 // The string pool contains UTF 8 strings; we don't want to cause
872 // temporary UTF-16 strings to be created as we search.
873 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
874 // Do a binary search for the string... this is a little tricky,
875 // because the strings are sorted with strzcmp16(). So to match
876 // the ordering, we need to convert strings in the pool to UTF-16.
877 // But we don't want to hit the cache, so instead we will have a
878 // local temporary allocation for the conversions.
879 char16_t* convBuffer = (char16_t*)malloc(strLen+4);
880 ssize_t l = 0;
881 ssize_t h = mHeader->stringCount-1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700883 ssize_t mid;
884 while (l <= h) {
885 mid = l + (h - l)/2;
886 const uint8_t* s = (const uint8_t*)string8At(mid, &len);
887 int c;
888 if (s != NULL) {
889 char16_t* end = utf8_to_utf16_n(s, len, convBuffer, strLen+3);
890 *end = 0;
891 c = strzcmp16(convBuffer, end-convBuffer, str, strLen);
892 } else {
893 c = -1;
894 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700895 if (kDebugStringPoolNoisy) {
896 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
897 (const char*)s, c, (int)l, (int)mid, (int)h);
898 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700899 if (c == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700900 if (kDebugStringPoolNoisy) {
901 ALOGI("MATCH!");
902 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700903 free(convBuffer);
904 return mid;
905 } else if (c < 0) {
906 l = mid + 1;
907 } else {
908 h = mid - 1;
909 }
910 }
911 free(convBuffer);
912 } else {
913 // It is unusual to get the ID from an unsorted string block...
914 // most often this happens because we want to get IDs for style
915 // span tags; since those always appear at the end of the string
916 // block, start searching at the back.
917 String8 str8(str, strLen);
918 const size_t str8Len = str8.size();
919 for (int i=mHeader->stringCount-1; i>=0; i--) {
920 const char* s = string8At(i, &len);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700921 if (kDebugStringPoolNoisy) {
922 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
923 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700924 if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700925 if (kDebugStringPoolNoisy) {
926 ALOGI("MATCH!");
927 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700928 return i;
929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700934 if (kDebugStringPoolNoisy) {
935 ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string());
936 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700937
938 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
939 // Do a binary search for the string...
940 ssize_t l = 0;
941 ssize_t h = mHeader->stringCount-1;
942
943 ssize_t mid;
944 while (l <= h) {
945 mid = l + (h - l)/2;
946 const char16_t* s = stringAt(mid, &len);
947 int c = s ? strzcmp16(s, len, str, strLen) : -1;
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700948 if (kDebugStringPoolNoisy) {
949 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
950 String8(s).string(), c, (int)l, (int)mid, (int)h);
951 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700952 if (c == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700953 if (kDebugStringPoolNoisy) {
954 ALOGI("MATCH!");
955 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700956 return mid;
957 } else if (c < 0) {
958 l = mid + 1;
959 } else {
960 h = mid - 1;
961 }
962 }
963 } else {
964 // It is unusual to get the ID from an unsorted string block...
965 // most often this happens because we want to get IDs for style
966 // span tags; since those always appear at the end of the string
967 // block, start searching at the back.
968 for (int i=mHeader->stringCount-1; i>=0; i--) {
969 const char16_t* s = stringAt(i, &len);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700970 if (kDebugStringPoolNoisy) {
971 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
972 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700973 if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700974 if (kDebugStringPoolNoisy) {
975 ALOGI("MATCH!");
976 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700977 return i;
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 }
981 }
982
983 return NAME_NOT_FOUND;
984}
985
986size_t ResStringPool::size() const
987{
988 return (mError == NO_ERROR) ? mHeader->stringCount : 0;
989}
990
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800991size_t ResStringPool::styleCount() const
992{
993 return (mError == NO_ERROR) ? mHeader->styleCount : 0;
994}
995
996size_t ResStringPool::bytes() const
997{
998 return (mError == NO_ERROR) ? mHeader->header.size : 0;
999}
1000
1001bool ResStringPool::isSorted() const
1002{
1003 return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0;
1004}
1005
Kenny Rootbb79f642009-12-10 14:20:15 -08001006bool ResStringPool::isUTF8() const
1007{
1008 return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0;
1009}
Kenny Rootbb79f642009-12-10 14:20:15 -08001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011// --------------------------------------------------------------------
1012// --------------------------------------------------------------------
1013// --------------------------------------------------------------------
1014
1015ResXMLParser::ResXMLParser(const ResXMLTree& tree)
1016 : mTree(tree), mEventCode(BAD_DOCUMENT)
1017{
1018}
1019
1020void ResXMLParser::restart()
1021{
1022 mCurNode = NULL;
1023 mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT;
1024}
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001025const ResStringPool& ResXMLParser::getStrings() const
1026{
1027 return mTree.mStrings;
1028}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030ResXMLParser::event_code_t ResXMLParser::getEventType() const
1031{
1032 return mEventCode;
1033}
1034
1035ResXMLParser::event_code_t ResXMLParser::next()
1036{
1037 if (mEventCode == START_DOCUMENT) {
1038 mCurNode = mTree.mRootNode;
1039 mCurExt = mTree.mRootExt;
1040 return (mEventCode=mTree.mRootCode);
1041 } else if (mEventCode >= FIRST_CHUNK_CODE) {
1042 return nextNode();
1043 }
1044 return mEventCode;
1045}
1046
Mathias Agopian5f910972009-06-22 02:35:32 -07001047int32_t ResXMLParser::getCommentID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048{
1049 return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1;
1050}
1051
Dan Albertf348c152014-09-08 18:28:00 -07001052const char16_t* ResXMLParser::getComment(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053{
1054 int32_t id = getCommentID();
1055 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1056}
1057
Mathias Agopian5f910972009-06-22 02:35:32 -07001058uint32_t ResXMLParser::getLineNumber() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059{
1060 return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1;
1061}
1062
Mathias Agopian5f910972009-06-22 02:35:32 -07001063int32_t ResXMLParser::getTextID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064{
1065 if (mEventCode == TEXT) {
1066 return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index);
1067 }
1068 return -1;
1069}
1070
Dan Albertf348c152014-09-08 18:28:00 -07001071const char16_t* ResXMLParser::getText(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072{
1073 int32_t id = getTextID();
1074 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1075}
1076
1077ssize_t ResXMLParser::getTextValue(Res_value* outValue) const
1078{
1079 if (mEventCode == TEXT) {
1080 outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData);
1081 return sizeof(Res_value);
1082 }
1083 return BAD_TYPE;
1084}
1085
Mathias Agopian5f910972009-06-22 02:35:32 -07001086int32_t ResXMLParser::getNamespacePrefixID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087{
1088 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1089 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index);
1090 }
1091 return -1;
1092}
1093
Dan Albertf348c152014-09-08 18:28:00 -07001094const char16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095{
1096 int32_t id = getNamespacePrefixID();
1097 //printf("prefix=%d event=%p\n", id, mEventCode);
1098 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1099}
1100
Mathias Agopian5f910972009-06-22 02:35:32 -07001101int32_t ResXMLParser::getNamespaceUriID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102{
1103 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1104 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index);
1105 }
1106 return -1;
1107}
1108
Dan Albertf348c152014-09-08 18:28:00 -07001109const char16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110{
1111 int32_t id = getNamespaceUriID();
1112 //printf("uri=%d event=%p\n", id, mEventCode);
1113 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1114}
1115
Mathias Agopian5f910972009-06-22 02:35:32 -07001116int32_t ResXMLParser::getElementNamespaceID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117{
1118 if (mEventCode == START_TAG) {
1119 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index);
1120 }
1121 if (mEventCode == END_TAG) {
1122 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index);
1123 }
1124 return -1;
1125}
1126
Dan Albertf348c152014-09-08 18:28:00 -07001127const char16_t* ResXMLParser::getElementNamespace(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128{
1129 int32_t id = getElementNamespaceID();
1130 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1131}
1132
Mathias Agopian5f910972009-06-22 02:35:32 -07001133int32_t ResXMLParser::getElementNameID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134{
1135 if (mEventCode == START_TAG) {
1136 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index);
1137 }
1138 if (mEventCode == END_TAG) {
1139 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index);
1140 }
1141 return -1;
1142}
1143
Dan Albertf348c152014-09-08 18:28:00 -07001144const char16_t* ResXMLParser::getElementName(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145{
1146 int32_t id = getElementNameID();
1147 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1148}
1149
1150size_t ResXMLParser::getAttributeCount() const
1151{
1152 if (mEventCode == START_TAG) {
1153 return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount);
1154 }
1155 return 0;
1156}
1157
Mathias Agopian5f910972009-06-22 02:35:32 -07001158int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159{
1160 if (mEventCode == START_TAG) {
1161 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1162 if (idx < dtohs(tag->attributeCount)) {
1163 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1164 (((const uint8_t*)tag)
1165 + dtohs(tag->attributeStart)
1166 + (dtohs(tag->attributeSize)*idx));
1167 return dtohl(attr->ns.index);
1168 }
1169 }
1170 return -2;
1171}
1172
Dan Albertf348c152014-09-08 18:28:00 -07001173const char16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174{
1175 int32_t id = getAttributeNamespaceID(idx);
1176 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001177 if (kDebugXMLNoisy) {
1178 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1181}
1182
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001183const char* ResXMLParser::getAttributeNamespace8(size_t idx, size_t* outLen) const
1184{
1185 int32_t id = getAttributeNamespaceID(idx);
1186 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001187 if (kDebugXMLNoisy) {
1188 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1189 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001190 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1191}
1192
Mathias Agopian5f910972009-06-22 02:35:32 -07001193int32_t ResXMLParser::getAttributeNameID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194{
1195 if (mEventCode == START_TAG) {
1196 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1197 if (idx < dtohs(tag->attributeCount)) {
1198 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1199 (((const uint8_t*)tag)
1200 + dtohs(tag->attributeStart)
1201 + (dtohs(tag->attributeSize)*idx));
1202 return dtohl(attr->name.index);
1203 }
1204 }
1205 return -1;
1206}
1207
Dan Albertf348c152014-09-08 18:28:00 -07001208const char16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209{
1210 int32_t id = getAttributeNameID(idx);
1211 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001212 if (kDebugXMLNoisy) {
1213 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1216}
1217
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001218const char* ResXMLParser::getAttributeName8(size_t idx, size_t* outLen) const
1219{
1220 int32_t id = getAttributeNameID(idx);
1221 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001222 if (kDebugXMLNoisy) {
1223 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1224 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001225 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1226}
1227
Mathias Agopian5f910972009-06-22 02:35:32 -07001228uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229{
1230 int32_t id = getAttributeNameID(idx);
1231 if (id >= 0 && (size_t)id < mTree.mNumResIds) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07001232 uint32_t resId = dtohl(mTree.mResIds[id]);
1233 if (mTree.mDynamicRefTable != NULL) {
1234 mTree.mDynamicRefTable->lookupResourceId(&resId);
1235 }
1236 return resId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 return 0;
1239}
1240
Mathias Agopian5f910972009-06-22 02:35:32 -07001241int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242{
1243 if (mEventCode == START_TAG) {
1244 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1245 if (idx < dtohs(tag->attributeCount)) {
1246 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1247 (((const uint8_t*)tag)
1248 + dtohs(tag->attributeStart)
1249 + (dtohs(tag->attributeSize)*idx));
1250 return dtohl(attr->rawValue.index);
1251 }
1252 }
1253 return -1;
1254}
1255
Dan Albertf348c152014-09-08 18:28:00 -07001256const char16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257{
1258 int32_t id = getAttributeValueStringID(idx);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001259 if (kDebugXMLNoisy) {
1260 printf("getAttributeValue 0x%zx=0x%x\n", idx, id);
1261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1263}
1264
1265int32_t ResXMLParser::getAttributeDataType(size_t idx) const
1266{
1267 if (mEventCode == START_TAG) {
1268 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1269 if (idx < dtohs(tag->attributeCount)) {
1270 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1271 (((const uint8_t*)tag)
1272 + dtohs(tag->attributeStart)
1273 + (dtohs(tag->attributeSize)*idx));
Adam Lesinskide898ff2014-01-29 18:20:45 -08001274 uint8_t type = attr->typedValue.dataType;
1275 if (type != Res_value::TYPE_DYNAMIC_REFERENCE) {
1276 return type;
1277 }
1278
1279 // This is a dynamic reference. We adjust those references
1280 // to regular references at this level, so lie to the caller.
1281 return Res_value::TYPE_REFERENCE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
1284 return Res_value::TYPE_NULL;
1285}
1286
1287int32_t ResXMLParser::getAttributeData(size_t idx) const
1288{
1289 if (mEventCode == START_TAG) {
1290 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1291 if (idx < dtohs(tag->attributeCount)) {
1292 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1293 (((const uint8_t*)tag)
1294 + dtohs(tag->attributeStart)
1295 + (dtohs(tag->attributeSize)*idx));
Adam Lesinskide898ff2014-01-29 18:20:45 -08001296 if (attr->typedValue.dataType != Res_value::TYPE_DYNAMIC_REFERENCE ||
1297 mTree.mDynamicRefTable == NULL) {
1298 return dtohl(attr->typedValue.data);
1299 }
1300
1301 uint32_t data = dtohl(attr->typedValue.data);
1302 if (mTree.mDynamicRefTable->lookupResourceId(&data) == NO_ERROR) {
1303 return data;
1304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306 }
1307 return 0;
1308}
1309
1310ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const
1311{
1312 if (mEventCode == START_TAG) {
1313 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1314 if (idx < dtohs(tag->attributeCount)) {
1315 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1316 (((const uint8_t*)tag)
1317 + dtohs(tag->attributeStart)
1318 + (dtohs(tag->attributeSize)*idx));
1319 outValue->copyFrom_dtoh(attr->typedValue);
Adam Lesinskide898ff2014-01-29 18:20:45 -08001320 if (mTree.mDynamicRefTable != NULL &&
1321 mTree.mDynamicRefTable->lookupResourceValue(outValue) != NO_ERROR) {
1322 return BAD_TYPE;
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 return sizeof(Res_value);
1325 }
1326 }
1327 return BAD_TYPE;
1328}
1329
1330ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const
1331{
1332 String16 nsStr(ns != NULL ? ns : "");
1333 String16 attrStr(attr);
1334 return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0,
1335 attrStr.string(), attrStr.size());
1336}
1337
1338ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen,
1339 const char16_t* attr, size_t attrLen) const
1340{
1341 if (mEventCode == START_TAG) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001342 if (attr == NULL) {
1343 return NAME_NOT_FOUND;
1344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 const size_t N = getAttributeCount();
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001346 if (mTree.mStrings.isUTF8()) {
1347 String8 ns8, attr8;
1348 if (ns != NULL) {
1349 ns8 = String8(ns, nsLen);
1350 }
1351 attr8 = String8(attr, attrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001352 if (kDebugStringPoolNoisy) {
1353 ALOGI("indexOfAttribute UTF8 %s (%zu) / %s (%zu)", ns8.string(), nsLen,
1354 attr8.string(), attrLen);
1355 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001356 for (size_t i=0; i<N; i++) {
1357 size_t curNsLen = 0, curAttrLen = 0;
1358 const char* curNs = getAttributeNamespace8(i, &curNsLen);
1359 const char* curAttr = getAttributeName8(i, &curAttrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001360 if (kDebugStringPoolNoisy) {
1361 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)", curNs, curNsLen, curAttr, curAttrLen);
1362 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001363 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1364 && memcmp(attr8.string(), curAttr, attrLen) == 0) {
1365 if (ns == NULL) {
1366 if (curNs == NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001367 if (kDebugStringPoolNoisy) {
1368 ALOGI(" FOUND!");
1369 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001370 return i;
1371 }
1372 } else if (curNs != NULL) {
1373 //printf(" --> ns=%s, curNs=%s\n",
1374 // String8(ns).string(), String8(curNs).string());
1375 if (memcmp(ns8.string(), curNs, nsLen) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001376 if (kDebugStringPoolNoisy) {
1377 ALOGI(" FOUND!");
1378 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001379 return i;
1380 }
1381 }
1382 }
1383 }
1384 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001385 if (kDebugStringPoolNoisy) {
1386 ALOGI("indexOfAttribute UTF16 %s (%zu) / %s (%zu)",
1387 String8(ns, nsLen).string(), nsLen,
1388 String8(attr, attrLen).string(), attrLen);
1389 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001390 for (size_t i=0; i<N; i++) {
1391 size_t curNsLen = 0, curAttrLen = 0;
1392 const char16_t* curNs = getAttributeNamespace(i, &curNsLen);
1393 const char16_t* curAttr = getAttributeName(i, &curAttrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001394 if (kDebugStringPoolNoisy) {
1395 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)",
1396 String8(curNs, curNsLen).string(), curNsLen,
1397 String8(curAttr, curAttrLen).string(), curAttrLen);
1398 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001399 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1400 && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) {
1401 if (ns == NULL) {
1402 if (curNs == NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001403 if (kDebugStringPoolNoisy) {
1404 ALOGI(" FOUND!");
1405 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001406 return i;
1407 }
1408 } else if (curNs != NULL) {
1409 //printf(" --> ns=%s, curNs=%s\n",
1410 // String8(ns).string(), String8(curNs).string());
1411 if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001412 if (kDebugStringPoolNoisy) {
1413 ALOGI(" FOUND!");
1414 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001415 return i;
1416 }
1417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419 }
1420 }
1421 }
1422
1423 return NAME_NOT_FOUND;
1424}
1425
1426ssize_t ResXMLParser::indexOfID() const
1427{
1428 if (mEventCode == START_TAG) {
1429 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex);
1430 if (idx > 0) return (idx-1);
1431 }
1432 return NAME_NOT_FOUND;
1433}
1434
1435ssize_t ResXMLParser::indexOfClass() const
1436{
1437 if (mEventCode == START_TAG) {
1438 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex);
1439 if (idx > 0) return (idx-1);
1440 }
1441 return NAME_NOT_FOUND;
1442}
1443
1444ssize_t ResXMLParser::indexOfStyle() const
1445{
1446 if (mEventCode == START_TAG) {
1447 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex);
1448 if (idx > 0) return (idx-1);
1449 }
1450 return NAME_NOT_FOUND;
1451}
1452
1453ResXMLParser::event_code_t ResXMLParser::nextNode()
1454{
1455 if (mEventCode < 0) {
1456 return mEventCode;
1457 }
1458
1459 do {
1460 const ResXMLTree_node* next = (const ResXMLTree_node*)
1461 (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001462 if (kDebugXMLNoisy) {
1463 ALOGI("Next node: prev=%p, next=%p\n", mCurNode, next);
1464 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 if (((const uint8_t*)next) >= mTree.mDataEnd) {
1467 mCurNode = NULL;
1468 return (mEventCode=END_DOCUMENT);
1469 }
1470
1471 if (mTree.validateNode(next) != NO_ERROR) {
1472 mCurNode = NULL;
1473 return (mEventCode=BAD_DOCUMENT);
1474 }
1475
1476 mCurNode = next;
1477 const uint16_t headerSize = dtohs(next->header.headerSize);
1478 const uint32_t totalSize = dtohl(next->header.size);
1479 mCurExt = ((const uint8_t*)next) + headerSize;
1480 size_t minExtSize = 0;
1481 event_code_t eventCode = (event_code_t)dtohs(next->header.type);
1482 switch ((mEventCode=eventCode)) {
1483 case RES_XML_START_NAMESPACE_TYPE:
1484 case RES_XML_END_NAMESPACE_TYPE:
1485 minExtSize = sizeof(ResXMLTree_namespaceExt);
1486 break;
1487 case RES_XML_START_ELEMENT_TYPE:
1488 minExtSize = sizeof(ResXMLTree_attrExt);
1489 break;
1490 case RES_XML_END_ELEMENT_TYPE:
1491 minExtSize = sizeof(ResXMLTree_endElementExt);
1492 break;
1493 case RES_XML_CDATA_TYPE:
1494 minExtSize = sizeof(ResXMLTree_cdataExt);
1495 break;
1496 default:
Steve Block8564c8d2012-01-05 23:22:43 +00001497 ALOGW("Unknown XML block: header type %d in node at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 (int)dtohs(next->header.type),
1499 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)));
1500 continue;
1501 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 if ((totalSize-headerSize) < minExtSize) {
Steve Block8564c8d2012-01-05 23:22:43 +00001504 ALOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 (int)dtohs(next->header.type),
1506 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)),
1507 (int)(totalSize-headerSize), (int)minExtSize);
1508 return (mEventCode=BAD_DOCUMENT);
1509 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n",
1512 // mCurNode, mCurExt, headerSize, minExtSize);
Mark Salyzyn00adb862014-03-19 11:00:06 -07001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 return eventCode;
1515 } while (true);
1516}
1517
1518void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const
1519{
1520 pos->eventCode = mEventCode;
1521 pos->curNode = mCurNode;
1522 pos->curExt = mCurExt;
1523}
1524
1525void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos)
1526{
1527 mEventCode = pos.eventCode;
1528 mCurNode = pos.curNode;
1529 mCurExt = pos.curExt;
1530}
1531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532// --------------------------------------------------------------------
1533
1534static volatile int32_t gCount = 0;
1535
Adam Lesinskide898ff2014-01-29 18:20:45 -08001536ResXMLTree::ResXMLTree(const DynamicRefTable* dynamicRefTable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 : ResXMLParser(*this)
Adam Lesinskide898ff2014-01-29 18:20:45 -08001538 , mDynamicRefTable(dynamicRefTable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 , mError(NO_INIT), mOwnedData(NULL)
1540{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001541 if (kDebugResXMLTree) {
1542 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 restart();
1545}
1546
Adam Lesinskide898ff2014-01-29 18:20:45 -08001547ResXMLTree::ResXMLTree()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 : ResXMLParser(*this)
Adam Lesinskide898ff2014-01-29 18:20:45 -08001549 , mDynamicRefTable(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 , mError(NO_INIT), mOwnedData(NULL)
1551{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001552 if (kDebugResXMLTree) {
1553 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1554 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08001555 restart();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556}
1557
1558ResXMLTree::~ResXMLTree()
1559{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001560 if (kDebugResXMLTree) {
1561 ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
1562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 uninit();
1564}
1565
1566status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData)
1567{
1568 uninit();
1569 mEventCode = START_DOCUMENT;
1570
Kenny Root32d6aef2012-10-10 10:23:47 -07001571 if (!data || !size) {
1572 return (mError=BAD_TYPE);
1573 }
1574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 if (copyData) {
1576 mOwnedData = malloc(size);
1577 if (mOwnedData == NULL) {
1578 return (mError=NO_MEMORY);
1579 }
1580 memcpy(mOwnedData, data, size);
1581 data = mOwnedData;
1582 }
1583
1584 mHeader = (const ResXMLTree_header*)data;
1585 mSize = dtohl(mHeader->header.size);
1586 if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) {
Steve Block8564c8d2012-01-05 23:22:43 +00001587 ALOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 (int)dtohs(mHeader->header.headerSize),
1589 (int)dtohl(mHeader->header.size), (int)size);
1590 mError = BAD_TYPE;
1591 restart();
1592 return mError;
1593 }
1594 mDataEnd = ((const uint8_t*)mHeader) + mSize;
1595
1596 mStrings.uninit();
1597 mRootNode = NULL;
1598 mResIds = NULL;
1599 mNumResIds = 0;
1600
1601 // First look for a couple interesting chunks: the string block
1602 // and first XML node.
1603 const ResChunk_header* chunk =
1604 (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize));
1605 const ResChunk_header* lastChunk = chunk;
1606 while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) &&
1607 ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) {
1608 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML");
1609 if (err != NO_ERROR) {
1610 mError = err;
1611 goto done;
1612 }
1613 const uint16_t type = dtohs(chunk->type);
1614 const size_t size = dtohl(chunk->size);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001615 if (kDebugXMLNoisy) {
1616 printf("Scanning @ %p: type=0x%x, size=0x%zx\n",
1617 (void*)(((uintptr_t)chunk)-((uintptr_t)mHeader)), type, size);
1618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (type == RES_STRING_POOL_TYPE) {
1620 mStrings.setTo(chunk, size);
1621 } else if (type == RES_XML_RESOURCE_MAP_TYPE) {
1622 mResIds = (const uint32_t*)
1623 (((const uint8_t*)chunk)+dtohs(chunk->headerSize));
1624 mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t);
1625 } else if (type >= RES_XML_FIRST_CHUNK_TYPE
1626 && type <= RES_XML_LAST_CHUNK_TYPE) {
1627 if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) {
1628 mError = BAD_TYPE;
1629 goto done;
1630 }
1631 mCurNode = (const ResXMLTree_node*)lastChunk;
1632 if (nextNode() == BAD_DOCUMENT) {
1633 mError = BAD_TYPE;
1634 goto done;
1635 }
1636 mRootNode = mCurNode;
1637 mRootExt = mCurExt;
1638 mRootCode = mEventCode;
1639 break;
1640 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001641 if (kDebugXMLNoisy) {
1642 printf("Skipping unknown chunk!\n");
1643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 lastChunk = chunk;
1646 chunk = (const ResChunk_header*)
1647 (((const uint8_t*)chunk) + size);
1648 }
1649
1650 if (mRootNode == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001651 ALOGW("Bad XML block: no root element node found\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 mError = BAD_TYPE;
1653 goto done;
1654 }
1655
1656 mError = mStrings.getError();
1657
1658done:
1659 restart();
1660 return mError;
1661}
1662
1663status_t ResXMLTree::getError() const
1664{
1665 return mError;
1666}
1667
1668void ResXMLTree::uninit()
1669{
1670 mError = NO_INIT;
Kenny Root19138462009-12-04 09:38:48 -08001671 mStrings.uninit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (mOwnedData) {
1673 free(mOwnedData);
1674 mOwnedData = NULL;
1675 }
1676 restart();
1677}
1678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const
1680{
1681 const uint16_t eventCode = dtohs(node->header.type);
1682
1683 status_t err = validate_chunk(
1684 &node->header, sizeof(ResXMLTree_node),
1685 mDataEnd, "ResXMLTree_node");
1686
1687 if (err >= NO_ERROR) {
1688 // Only perform additional validation on START nodes
1689 if (eventCode != RES_XML_START_ELEMENT_TYPE) {
1690 return NO_ERROR;
1691 }
1692
1693 const uint16_t headerSize = dtohs(node->header.headerSize);
1694 const uint32_t size = dtohl(node->header.size);
1695 const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*)
1696 (((const uint8_t*)node) + headerSize);
1697 // check for sensical values pulled out of the stream so far...
1698 if ((size >= headerSize + sizeof(ResXMLTree_attrExt))
1699 && ((void*)attrExt > (void*)node)) {
1700 const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize))
1701 * dtohs(attrExt->attributeCount);
1702 if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) {
1703 return NO_ERROR;
1704 }
Steve Block8564c8d2012-01-05 23:22:43 +00001705 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 (unsigned int)(dtohs(attrExt->attributeStart)+attrSize),
1707 (unsigned int)(size-headerSize));
1708 }
1709 else {
Steve Block8564c8d2012-01-05 23:22:43 +00001710 ALOGW("Bad XML start block: node header size 0x%x, size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 (unsigned int)headerSize, (unsigned int)size);
1712 }
1713 return BAD_TYPE;
1714 }
1715
1716 return err;
1717
1718#if 0
1719 const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE;
1720
1721 const uint16_t headerSize = dtohs(node->header.headerSize);
1722 const uint32_t size = dtohl(node->header.size);
1723
1724 if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) {
1725 if (size >= headerSize) {
1726 if (((const uint8_t*)node) <= (mDataEnd-size)) {
1727 if (!isStart) {
1728 return NO_ERROR;
1729 }
1730 if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount))
1731 <= (size-headerSize)) {
1732 return NO_ERROR;
1733 }
Steve Block8564c8d2012-01-05 23:22:43 +00001734 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount),
1736 (int)(size-headerSize));
1737 return BAD_TYPE;
1738 }
Steve Block8564c8d2012-01-05 23:22:43 +00001739 ALOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize);
1741 return BAD_TYPE;
1742 }
Steve Block8564c8d2012-01-05 23:22:43 +00001743 ALOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1745 (int)headerSize, (int)size);
1746 return BAD_TYPE;
1747 }
Steve Block8564c8d2012-01-05 23:22:43 +00001748 ALOGW("Bad XML block: node at 0x%x header size 0x%x too small\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1750 (int)headerSize);
1751 return BAD_TYPE;
1752#endif
1753}
1754
1755// --------------------------------------------------------------------
1756// --------------------------------------------------------------------
1757// --------------------------------------------------------------------
1758
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001759void ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) {
1760 const size_t size = dtohl(o.size);
1761 if (size >= sizeof(ResTable_config)) {
1762 *this = o;
1763 } else {
1764 memcpy(this, &o, size);
1765 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
1766 }
1767}
1768
Narayan Kamath48620f12014-01-20 13:57:11 +00001769/* static */ size_t unpackLanguageOrRegion(const char in[2], const char base,
1770 char out[4]) {
1771 if (in[0] & 0x80) {
1772 // The high bit is "1", which means this is a packed three letter
1773 // language code.
1774
1775 // The smallest 5 bits of the second char are the first alphabet.
1776 const uint8_t first = in[1] & 0x1f;
1777 // The last three bits of the second char and the first two bits
1778 // of the first char are the second alphabet.
1779 const uint8_t second = ((in[1] & 0xe0) >> 5) + ((in[0] & 0x03) << 3);
1780 // Bits 3 to 7 (inclusive) of the first char are the third alphabet.
1781 const uint8_t third = (in[0] & 0x7c) >> 2;
1782
1783 out[0] = first + base;
1784 out[1] = second + base;
1785 out[2] = third + base;
1786 out[3] = 0;
1787
1788 return 3;
1789 }
1790
1791 if (in[0]) {
1792 memcpy(out, in, 2);
1793 memset(out + 2, 0, 2);
1794 return 2;
1795 }
1796
1797 memset(out, 0, 4);
1798 return 0;
1799}
1800
Narayan Kamath788fa412014-01-21 15:32:36 +00001801/* static */ void packLanguageOrRegion(const char* in, const char base,
Narayan Kamath48620f12014-01-20 13:57:11 +00001802 char out[2]) {
Narayan Kamath788fa412014-01-21 15:32:36 +00001803 if (in[2] == 0 || in[2] == '-') {
Narayan Kamath48620f12014-01-20 13:57:11 +00001804 out[0] = in[0];
1805 out[1] = in[1];
1806 } else {
Narayan Kamathb2975912014-06-30 15:59:39 +01001807 uint8_t first = (in[0] - base) & 0x007f;
1808 uint8_t second = (in[1] - base) & 0x007f;
1809 uint8_t third = (in[2] - base) & 0x007f;
Narayan Kamath48620f12014-01-20 13:57:11 +00001810
1811 out[0] = (0x80 | (third << 2) | (second >> 3));
1812 out[1] = ((second << 5) | first);
1813 }
1814}
1815
1816
Narayan Kamath788fa412014-01-21 15:32:36 +00001817void ResTable_config::packLanguage(const char* language) {
Narayan Kamath48620f12014-01-20 13:57:11 +00001818 packLanguageOrRegion(language, 'a', this->language);
1819}
1820
Narayan Kamath788fa412014-01-21 15:32:36 +00001821void ResTable_config::packRegion(const char* region) {
Narayan Kamath48620f12014-01-20 13:57:11 +00001822 packLanguageOrRegion(region, '0', this->country);
1823}
1824
1825size_t ResTable_config::unpackLanguage(char language[4]) const {
1826 return unpackLanguageOrRegion(this->language, 'a', language);
1827}
1828
1829size_t ResTable_config::unpackRegion(char region[4]) const {
1830 return unpackLanguageOrRegion(this->country, '0', region);
1831}
1832
1833
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001834void ResTable_config::copyFromDtoH(const ResTable_config& o) {
1835 copyFromDeviceNoSwap(o);
1836 size = sizeof(ResTable_config);
1837 mcc = dtohs(mcc);
1838 mnc = dtohs(mnc);
1839 density = dtohs(density);
1840 screenWidth = dtohs(screenWidth);
1841 screenHeight = dtohs(screenHeight);
1842 sdkVersion = dtohs(sdkVersion);
1843 minorVersion = dtohs(minorVersion);
1844 smallestScreenWidthDp = dtohs(smallestScreenWidthDp);
1845 screenWidthDp = dtohs(screenWidthDp);
1846 screenHeightDp = dtohs(screenHeightDp);
1847}
1848
1849void ResTable_config::swapHtoD() {
1850 size = htodl(size);
1851 mcc = htods(mcc);
1852 mnc = htods(mnc);
1853 density = htods(density);
1854 screenWidth = htods(screenWidth);
1855 screenHeight = htods(screenHeight);
1856 sdkVersion = htods(sdkVersion);
1857 minorVersion = htods(minorVersion);
1858 smallestScreenWidthDp = htods(smallestScreenWidthDp);
1859 screenWidthDp = htods(screenWidthDp);
1860 screenHeightDp = htods(screenHeightDp);
1861}
1862
Narayan Kamath48620f12014-01-20 13:57:11 +00001863/* static */ inline int compareLocales(const ResTable_config &l, const ResTable_config &r) {
1864 if (l.locale != r.locale) {
1865 // NOTE: This is the old behaviour with respect to comparison orders.
1866 // The diff value here doesn't make much sense (given our bit packing scheme)
1867 // but it's stable, and that's all we need.
1868 return l.locale - r.locale;
1869 }
1870
1871 // The language & region are equal, so compare the scripts and variants.
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08001872 const char emptyScript[sizeof(l.localeScript)] = {'\0', '\0', '\0', '\0'};
Roozbeh Pournader79608982016-03-03 15:06:46 -08001873 const char *lScript = l.localeScriptWasComputed ? emptyScript : l.localeScript;
1874 const char *rScript = r.localeScriptWasComputed ? emptyScript : r.localeScript;
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08001875 int script = memcmp(lScript, rScript, sizeof(l.localeScript));
Narayan Kamath48620f12014-01-20 13:57:11 +00001876 if (script) {
1877 return script;
1878 }
1879
1880 // The language, region and script are equal, so compare variants.
1881 //
1882 // This should happen very infrequently (if at all.)
1883 return memcmp(l.localeVariant, r.localeVariant, sizeof(l.localeVariant));
1884}
1885
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001886int ResTable_config::compare(const ResTable_config& o) const {
1887 int32_t diff = (int32_t)(imsi - o.imsi);
1888 if (diff != 0) return diff;
Narayan Kamath48620f12014-01-20 13:57:11 +00001889 diff = compareLocales(*this, o);
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001890 if (diff != 0) return diff;
1891 diff = (int32_t)(screenType - o.screenType);
1892 if (diff != 0) return diff;
1893 diff = (int32_t)(input - o.input);
1894 if (diff != 0) return diff;
1895 diff = (int32_t)(screenSize - o.screenSize);
1896 if (diff != 0) return diff;
1897 diff = (int32_t)(version - o.version);
1898 if (diff != 0) return diff;
1899 diff = (int32_t)(screenLayout - o.screenLayout);
1900 if (diff != 0) return diff;
Adam Lesinski2738c962015-05-14 14:25:36 -07001901 diff = (int32_t)(screenLayout2 - o.screenLayout2);
1902 if (diff != 0) return diff;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001903 diff = (int32_t)(uiMode - o.uiMode);
1904 if (diff != 0) return diff;
1905 diff = (int32_t)(smallestScreenWidthDp - o.smallestScreenWidthDp);
1906 if (diff != 0) return diff;
1907 diff = (int32_t)(screenSizeDp - o.screenSizeDp);
1908 return (int)diff;
1909}
1910
1911int ResTable_config::compareLogical(const ResTable_config& o) const {
1912 if (mcc != o.mcc) {
1913 return mcc < o.mcc ? -1 : 1;
1914 }
1915 if (mnc != o.mnc) {
1916 return mnc < o.mnc ? -1 : 1;
1917 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001918
1919 int diff = compareLocales(*this, o);
1920 if (diff < 0) {
1921 return -1;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001922 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001923 if (diff > 0) {
1924 return 1;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001925 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001926
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001927 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) {
1928 return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1;
1929 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001930 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
1931 return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1;
1932 }
1933 if (screenWidthDp != o.screenWidthDp) {
1934 return screenWidthDp < o.screenWidthDp ? -1 : 1;
1935 }
1936 if (screenHeightDp != o.screenHeightDp) {
1937 return screenHeightDp < o.screenHeightDp ? -1 : 1;
1938 }
1939 if (screenWidth != o.screenWidth) {
1940 return screenWidth < o.screenWidth ? -1 : 1;
1941 }
1942 if (screenHeight != o.screenHeight) {
1943 return screenHeight < o.screenHeight ? -1 : 1;
1944 }
1945 if (density != o.density) {
1946 return density < o.density ? -1 : 1;
1947 }
1948 if (orientation != o.orientation) {
1949 return orientation < o.orientation ? -1 : 1;
1950 }
1951 if (touchscreen != o.touchscreen) {
1952 return touchscreen < o.touchscreen ? -1 : 1;
1953 }
1954 if (input != o.input) {
1955 return input < o.input ? -1 : 1;
1956 }
1957 if (screenLayout != o.screenLayout) {
1958 return screenLayout < o.screenLayout ? -1 : 1;
1959 }
Adam Lesinski2738c962015-05-14 14:25:36 -07001960 if (screenLayout2 != o.screenLayout2) {
1961 return screenLayout2 < o.screenLayout2 ? -1 : 1;
1962 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001963 if (uiMode != o.uiMode) {
1964 return uiMode < o.uiMode ? -1 : 1;
1965 }
1966 if (version != o.version) {
1967 return version < o.version ? -1 : 1;
1968 }
1969 return 0;
1970}
1971
1972int ResTable_config::diff(const ResTable_config& o) const {
1973 int diffs = 0;
1974 if (mcc != o.mcc) diffs |= CONFIG_MCC;
1975 if (mnc != o.mnc) diffs |= CONFIG_MNC;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001976 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
1977 if (density != o.density) diffs |= CONFIG_DENSITY;
1978 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
1979 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
1980 diffs |= CONFIG_KEYBOARD_HIDDEN;
1981 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
1982 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
1983 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
1984 if (version != o.version) diffs |= CONFIG_VERSION;
Fabrice Di Meglio35099352012-12-12 11:52:03 -08001985 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) diffs |= CONFIG_LAYOUTDIR;
1986 if ((screenLayout & ~MASK_LAYOUTDIR) != (o.screenLayout & ~MASK_LAYOUTDIR)) diffs |= CONFIG_SCREEN_LAYOUT;
Adam Lesinski2738c962015-05-14 14:25:36 -07001987 if ((screenLayout2 & MASK_SCREENROUND) != (o.screenLayout2 & MASK_SCREENROUND)) diffs |= CONFIG_SCREEN_ROUND;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001988 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
1989 if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE;
1990 if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE;
Narayan Kamath48620f12014-01-20 13:57:11 +00001991
1992 const int diff = compareLocales(*this, o);
1993 if (diff) diffs |= CONFIG_LOCALE;
1994
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001995 return diffs;
1996}
1997
Narayan Kamath48620f12014-01-20 13:57:11 +00001998int ResTable_config::isLocaleMoreSpecificThan(const ResTable_config& o) const {
1999 if (locale || o.locale) {
2000 if (language[0] != o.language[0]) {
2001 if (!language[0]) return -1;
2002 if (!o.language[0]) return 1;
2003 }
2004
2005 if (country[0] != o.country[0]) {
2006 if (!country[0]) return -1;
2007 if (!o.country[0]) return 1;
2008 }
2009 }
2010
2011 // There isn't a well specified "importance" order between variants and
2012 // scripts. We can't easily tell whether, say "en-Latn-US" is more or less
2013 // specific than "en-US-POSIX".
2014 //
2015 // We therefore arbitrarily decide to give priority to variants over
2016 // scripts since it seems more useful to do so. We will consider
2017 // "en-US-POSIX" to be more specific than "en-Latn-US".
2018
Roozbeh Pournader79608982016-03-03 15:06:46 -08002019 const int score = ((localeScript[0] != '\0' && !localeScriptWasComputed) ? 1 : 0) +
2020 ((localeVariant[0] != '\0') ? 2 : 0);
Narayan Kamath48620f12014-01-20 13:57:11 +00002021
Roozbeh Pournader79608982016-03-03 15:06:46 -08002022 const int oScore = (o.localeScript[0] != '\0' && !o.localeScriptWasComputed ? 1 : 0) +
2023 ((o.localeVariant[0] != '\0') ? 2 : 0);
Narayan Kamath48620f12014-01-20 13:57:11 +00002024
2025 return score - oScore;
2026
2027}
2028
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002029bool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const {
2030 // The order of the following tests defines the importance of one
2031 // configuration parameter over another. Those tests first are more
2032 // important, trumping any values in those following them.
2033 if (imsi || o.imsi) {
2034 if (mcc != o.mcc) {
2035 if (!mcc) return false;
2036 if (!o.mcc) return true;
2037 }
2038
2039 if (mnc != o.mnc) {
2040 if (!mnc) return false;
2041 if (!o.mnc) return true;
2042 }
2043 }
2044
2045 if (locale || o.locale) {
Narayan Kamath48620f12014-01-20 13:57:11 +00002046 const int diff = isLocaleMoreSpecificThan(o);
2047 if (diff < 0) {
2048 return false;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002049 }
2050
Narayan Kamath48620f12014-01-20 13:57:11 +00002051 if (diff > 0) {
2052 return true;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002053 }
2054 }
2055
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002056 if (screenLayout || o.screenLayout) {
2057 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) {
2058 if (!(screenLayout & MASK_LAYOUTDIR)) return false;
2059 if (!(o.screenLayout & MASK_LAYOUTDIR)) return true;
2060 }
2061 }
2062
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002063 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2064 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2065 if (!smallestScreenWidthDp) return false;
2066 if (!o.smallestScreenWidthDp) return true;
2067 }
2068 }
2069
2070 if (screenSizeDp || o.screenSizeDp) {
2071 if (screenWidthDp != o.screenWidthDp) {
2072 if (!screenWidthDp) return false;
2073 if (!o.screenWidthDp) return true;
2074 }
2075
2076 if (screenHeightDp != o.screenHeightDp) {
2077 if (!screenHeightDp) return false;
2078 if (!o.screenHeightDp) return true;
2079 }
2080 }
2081
2082 if (screenLayout || o.screenLayout) {
2083 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
2084 if (!(screenLayout & MASK_SCREENSIZE)) return false;
2085 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
2086 }
2087 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
2088 if (!(screenLayout & MASK_SCREENLONG)) return false;
2089 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
2090 }
2091 }
2092
Adam Lesinski2738c962015-05-14 14:25:36 -07002093 if (screenLayout2 || o.screenLayout2) {
2094 if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0) {
2095 if (!(screenLayout2 & MASK_SCREENROUND)) return false;
2096 if (!(o.screenLayout2 & MASK_SCREENROUND)) return true;
2097 }
2098 }
2099
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002100 if (orientation != o.orientation) {
2101 if (!orientation) return false;
2102 if (!o.orientation) return true;
2103 }
2104
2105 if (uiMode || o.uiMode) {
2106 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
2107 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
2108 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
2109 }
2110 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
2111 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
2112 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
2113 }
2114 }
2115
2116 // density is never 'more specific'
2117 // as the default just equals 160
2118
2119 if (touchscreen != o.touchscreen) {
2120 if (!touchscreen) return false;
2121 if (!o.touchscreen) return true;
2122 }
2123
2124 if (input || o.input) {
2125 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
2126 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
2127 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
2128 }
2129
2130 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
2131 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
2132 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
2133 }
2134
2135 if (keyboard != o.keyboard) {
2136 if (!keyboard) return false;
2137 if (!o.keyboard) return true;
2138 }
2139
2140 if (navigation != o.navigation) {
2141 if (!navigation) return false;
2142 if (!o.navigation) return true;
2143 }
2144 }
2145
2146 if (screenSize || o.screenSize) {
2147 if (screenWidth != o.screenWidth) {
2148 if (!screenWidth) return false;
2149 if (!o.screenWidth) return true;
2150 }
2151
2152 if (screenHeight != o.screenHeight) {
2153 if (!screenHeight) return false;
2154 if (!o.screenHeight) return true;
2155 }
2156 }
2157
2158 if (version || o.version) {
2159 if (sdkVersion != o.sdkVersion) {
2160 if (!sdkVersion) return false;
2161 if (!o.sdkVersion) return true;
2162 }
2163
2164 if (minorVersion != o.minorVersion) {
2165 if (!minorVersion) return false;
2166 if (!o.minorVersion) return true;
2167 }
2168 }
2169 return false;
2170}
2171
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002172bool ResTable_config::isLocaleBetterThan(const ResTable_config& o,
2173 const ResTable_config* requested) const {
2174 if (requested->locale == 0) {
2175 // The request doesn't have a locale, so no resource is better
2176 // than the other.
2177 return false;
2178 }
2179
2180 if (locale == 0 && o.locale == 0) {
2181 // The locales parts of both resources are empty, so no one is better
2182 // than the other.
2183 return false;
2184 }
2185
2186 // Non-matching locales have been filtered out, so both resources
2187 // match the requested locale.
2188 //
2189 // Because of the locale-related checks in match() and the checks, we know
2190 // that:
2191 // 1) The resource languages are either empty or match the request;
2192 // and
2193 // 2) If the request's script is known, the resource scripts are either
2194 // unknown or match the request.
2195
2196 if (language[0] != o.language[0]) {
2197 // The languages of the two resources are not the same. We can only
2198 // assume that one of the two resources matched the request because one
2199 // doesn't have a language and the other has a matching language.
Roozbeh Pournader27953c32016-02-01 13:49:52 -08002200 //
2201 // We consider the one that has the language specified a better match.
2202 //
2203 // The exception is that we consider no-language resources a better match
2204 // for US English and similar locales than locales that are a descendant
2205 // of Internatinal English (en-001), since no-language resources are
2206 // where the US English resource have traditionally lived for most apps.
2207 if (requested->language[0] == 'e' && requested->language[1] == 'n') {
2208 if (requested->country[0] == 'U' && requested->country[1] == 'S') {
2209 // For US English itself, we consider a no-locale resource a
2210 // better match if the other resource has a country other than
2211 // US specified.
2212 if (language[0] != '\0') {
2213 return country[0] == '\0' || (country[0] == 'U' && country[1] == 'S');
2214 } else {
2215 return !(o.country[0] == '\0' || (o.country[0] == 'U' && o.country[1] == 'S'));
2216 }
2217 } else if (localeDataIsCloseToUsEnglish(requested->country)) {
2218 if (language[0] != '\0') {
2219 return localeDataIsCloseToUsEnglish(country);
2220 } else {
2221 return !localeDataIsCloseToUsEnglish(o.country);
2222 }
2223 }
2224 }
2225 return (language[0] != '\0');
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002226 }
2227
2228 // If we are here, both the resources have the same non-empty language as
2229 // the request.
2230 //
2231 // Because the languages are the same, computeScript() always
2232 // returns a non-empty script for languages it knows about, and we have passed
2233 // the script checks in match(), the scripts are either all unknown or are
2234 // all the same. So we can't gain anything by checking the scripts. We need
2235 // to check the region and variant.
2236
2237 // See if any of the regions is better than the other
2238 const int region_comparison = localeDataCompareRegions(
2239 country, o.country,
Roozbeh Pournader4de45962016-02-11 17:58:24 -08002240 language, requested->localeScript, requested->country);
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002241 if (region_comparison != 0) {
2242 return (region_comparison > 0);
2243 }
2244
2245 // The regions are the same. Try the variant.
2246 if (requested->localeVariant[0] != '\0'
2247 && strncmp(localeVariant, requested->localeVariant, sizeof(localeVariant)) == 0) {
2248 return (strncmp(o.localeVariant, requested->localeVariant, sizeof(localeVariant)) != 0);
2249 }
2250
2251 return false;
2252}
2253
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002254bool ResTable_config::isBetterThan(const ResTable_config& o,
2255 const ResTable_config* requested) const {
2256 if (requested) {
2257 if (imsi || o.imsi) {
2258 if ((mcc != o.mcc) && requested->mcc) {
2259 return (mcc);
2260 }
2261
2262 if ((mnc != o.mnc) && requested->mnc) {
2263 return (mnc);
2264 }
2265 }
2266
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002267 if (isLocaleBetterThan(o, requested)) {
2268 return true;
Narayan Kamath48620f12014-01-20 13:57:11 +00002269 }
2270
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002271 if (screenLayout || o.screenLayout) {
2272 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0
2273 && (requested->screenLayout & MASK_LAYOUTDIR)) {
2274 int myLayoutDir = screenLayout & MASK_LAYOUTDIR;
2275 int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR;
2276 return (myLayoutDir > oLayoutDir);
2277 }
2278 }
2279
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002280 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2281 // The configuration closest to the actual size is best.
2282 // We assume that larger configs have already been filtered
2283 // out at this point. That means we just want the largest one.
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002284 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2285 return smallestScreenWidthDp > o.smallestScreenWidthDp;
2286 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002287 }
2288
2289 if (screenSizeDp || o.screenSizeDp) {
2290 // "Better" is based on the sum of the difference between both
2291 // width and height from the requested dimensions. We are
2292 // assuming the invalid configs (with smaller dimens) have
2293 // already been filtered. Note that if a particular dimension
2294 // is unspecified, we will end up with a large value (the
2295 // difference between 0 and the requested dimension), which is
2296 // good since we will prefer a config that has specified a
2297 // dimension value.
2298 int myDelta = 0, otherDelta = 0;
2299 if (requested->screenWidthDp) {
2300 myDelta += requested->screenWidthDp - screenWidthDp;
2301 otherDelta += requested->screenWidthDp - o.screenWidthDp;
2302 }
2303 if (requested->screenHeightDp) {
2304 myDelta += requested->screenHeightDp - screenHeightDp;
2305 otherDelta += requested->screenHeightDp - o.screenHeightDp;
2306 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002307 if (kDebugTableSuperNoisy) {
2308 ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
2309 screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
2310 requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
2311 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002312 if (myDelta != otherDelta) {
2313 return myDelta < otherDelta;
2314 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002315 }
2316
2317 if (screenLayout || o.screenLayout) {
2318 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
2319 && (requested->screenLayout & MASK_SCREENSIZE)) {
2320 // A little backwards compatibility here: undefined is
2321 // considered equivalent to normal. But only if the
2322 // requested size is at least normal; otherwise, small
2323 // is better than the default.
2324 int mySL = (screenLayout & MASK_SCREENSIZE);
2325 int oSL = (o.screenLayout & MASK_SCREENSIZE);
2326 int fixedMySL = mySL;
2327 int fixedOSL = oSL;
2328 if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
2329 if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
2330 if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
2331 }
2332 // For screen size, the best match is the one that is
2333 // closest to the requested screen size, but not over
2334 // (the not over part is dealt with in match() below).
2335 if (fixedMySL == fixedOSL) {
2336 // If the two are the same, but 'this' is actually
2337 // undefined, then the other is really a better match.
2338 if (mySL == 0) return false;
2339 return true;
2340 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002341 if (fixedMySL != fixedOSL) {
2342 return fixedMySL > fixedOSL;
2343 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002344 }
2345 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
2346 && (requested->screenLayout & MASK_SCREENLONG)) {
2347 return (screenLayout & MASK_SCREENLONG);
2348 }
2349 }
2350
Adam Lesinski2738c962015-05-14 14:25:36 -07002351 if (screenLayout2 || o.screenLayout2) {
2352 if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0 &&
2353 (requested->screenLayout2 & MASK_SCREENROUND)) {
2354 return screenLayout2 & MASK_SCREENROUND;
2355 }
2356 }
2357
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002358 if ((orientation != o.orientation) && requested->orientation) {
2359 return (orientation);
2360 }
2361
2362 if (uiMode || o.uiMode) {
2363 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
2364 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
2365 return (uiMode & MASK_UI_MODE_TYPE);
2366 }
2367 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
2368 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
2369 return (uiMode & MASK_UI_MODE_NIGHT);
2370 }
2371 }
2372
2373 if (screenType || o.screenType) {
2374 if (density != o.density) {
Adam Lesinski31245b42014-08-22 19:10:56 -07002375 // Use the system default density (DENSITY_MEDIUM, 160dpi) if none specified.
2376 const int thisDensity = density ? density : int(ResTable_config::DENSITY_MEDIUM);
2377 const int otherDensity = o.density ? o.density : int(ResTable_config::DENSITY_MEDIUM);
2378
2379 // We always prefer DENSITY_ANY over scaling a density bucket.
2380 if (thisDensity == ResTable_config::DENSITY_ANY) {
2381 return true;
2382 } else if (otherDensity == ResTable_config::DENSITY_ANY) {
2383 return false;
2384 }
2385
2386 int requestedDensity = requested->density;
2387 if (requested->density == 0 ||
2388 requested->density == ResTable_config::DENSITY_ANY) {
2389 requestedDensity = ResTable_config::DENSITY_MEDIUM;
2390 }
2391
2392 // DENSITY_ANY is now dealt with. We should look to
2393 // pick a density bucket and potentially scale it.
2394 // Any density is potentially useful
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002395 // because the system will scale it. Scaling down
2396 // is generally better than scaling up.
Adam Lesinski31245b42014-08-22 19:10:56 -07002397 int h = thisDensity;
2398 int l = otherDensity;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002399 bool bImBigger = true;
2400 if (l > h) {
2401 int t = h;
2402 h = l;
2403 l = t;
2404 bImBigger = false;
2405 }
2406
Adam Lesinski31245b42014-08-22 19:10:56 -07002407 if (requestedDensity >= h) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002408 // requested value higher than both l and h, give h
2409 return bImBigger;
2410 }
Adam Lesinski31245b42014-08-22 19:10:56 -07002411 if (l >= requestedDensity) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002412 // requested value lower than both l and h, give l
2413 return !bImBigger;
2414 }
2415 // saying that scaling down is 2x better than up
Adam Lesinski31245b42014-08-22 19:10:56 -07002416 if (((2 * l) - requestedDensity) * h > requestedDensity * requestedDensity) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002417 return !bImBigger;
2418 } else {
2419 return bImBigger;
2420 }
2421 }
2422
2423 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
2424 return (touchscreen);
2425 }
2426 }
2427
2428 if (input || o.input) {
2429 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
2430 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
2431 if (keysHidden != oKeysHidden) {
2432 const int reqKeysHidden =
2433 requested->inputFlags & MASK_KEYSHIDDEN;
2434 if (reqKeysHidden) {
2435
2436 if (!keysHidden) return false;
2437 if (!oKeysHidden) return true;
2438 // For compatibility, we count KEYSHIDDEN_NO as being
2439 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
2440 // these by making an exact match more specific.
2441 if (reqKeysHidden == keysHidden) return true;
2442 if (reqKeysHidden == oKeysHidden) return false;
2443 }
2444 }
2445
2446 const int navHidden = inputFlags & MASK_NAVHIDDEN;
2447 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
2448 if (navHidden != oNavHidden) {
2449 const int reqNavHidden =
2450 requested->inputFlags & MASK_NAVHIDDEN;
2451 if (reqNavHidden) {
2452
2453 if (!navHidden) return false;
2454 if (!oNavHidden) return true;
2455 }
2456 }
2457
2458 if ((keyboard != o.keyboard) && requested->keyboard) {
2459 return (keyboard);
2460 }
2461
2462 if ((navigation != o.navigation) && requested->navigation) {
2463 return (navigation);
2464 }
2465 }
2466
2467 if (screenSize || o.screenSize) {
2468 // "Better" is based on the sum of the difference between both
2469 // width and height from the requested dimensions. We are
2470 // assuming the invalid configs (with smaller sizes) have
2471 // already been filtered. Note that if a particular dimension
2472 // is unspecified, we will end up with a large value (the
2473 // difference between 0 and the requested dimension), which is
2474 // good since we will prefer a config that has specified a
2475 // size value.
2476 int myDelta = 0, otherDelta = 0;
2477 if (requested->screenWidth) {
2478 myDelta += requested->screenWidth - screenWidth;
2479 otherDelta += requested->screenWidth - o.screenWidth;
2480 }
2481 if (requested->screenHeight) {
2482 myDelta += requested->screenHeight - screenHeight;
2483 otherDelta += requested->screenHeight - o.screenHeight;
2484 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002485 if (myDelta != otherDelta) {
2486 return myDelta < otherDelta;
2487 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002488 }
2489
2490 if (version || o.version) {
2491 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
2492 return (sdkVersion > o.sdkVersion);
2493 }
2494
2495 if ((minorVersion != o.minorVersion) &&
2496 requested->minorVersion) {
2497 return (minorVersion);
2498 }
2499 }
2500
2501 return false;
2502 }
2503 return isMoreSpecificThan(o);
2504}
2505
2506bool ResTable_config::match(const ResTable_config& settings) const {
2507 if (imsi != 0) {
2508 if (mcc != 0 && mcc != settings.mcc) {
2509 return false;
2510 }
2511 if (mnc != 0 && mnc != settings.mnc) {
2512 return false;
2513 }
2514 }
2515 if (locale != 0) {
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002516 // Don't consider country and variants when deciding matches.
2517 // (Theoretically, the variant can also affect the script. For
2518 // example, "ar-alalc97" probably implies the Latin script, but since
2519 // CLDR doesn't support getting likely scripts for that, we'll assume
2520 // the variant doesn't change the script.)
Narayan Kamath48620f12014-01-20 13:57:11 +00002521 //
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002522 // If two configs differ only in their country and variant,
2523 // they can be weeded out in the isMoreSpecificThan test.
2524 if (language[0] != settings.language[0] || language[1] != settings.language[1]) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002525 return false;
2526 }
Narayan Kamath48620f12014-01-20 13:57:11 +00002527
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002528 // For backward compatibility and supporting private-use locales, we
2529 // fall back to old behavior if we couldn't determine the script for
Roozbeh Pournader4de45962016-02-11 17:58:24 -08002530 // either of the desired locale or the provided locale. But if we could determine
2531 // the scripts, they should be the same for the locales to match.
2532 bool countriesMustMatch = false;
2533 char computed_script[4];
2534 const char* script;
2535 if (settings.localeScript[0] == '\0') { // could not determine the request's script
2536 countriesMustMatch = true;
2537 } else {
Roozbeh Pournader79608982016-03-03 15:06:46 -08002538 if (localeScript[0] == '\0' && !localeScriptWasComputed) {
2539 // script was not provided or computed, so we try to compute it
Roozbeh Pournader4de45962016-02-11 17:58:24 -08002540 localeDataComputeScript(computed_script, language, country);
2541 if (computed_script[0] == '\0') { // we could not compute the script
2542 countriesMustMatch = true;
2543 } else {
2544 script = computed_script;
2545 }
2546 } else { // script was provided, so just use it
2547 script = localeScript;
2548 }
2549 }
2550
2551 if (countriesMustMatch) {
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002552 if (country[0] != '\0'
2553 && (country[0] != settings.country[0]
2554 || country[1] != settings.country[1])) {
2555 return false;
2556 }
2557 } else {
Roozbeh Pournader4de45962016-02-11 17:58:24 -08002558 if (memcmp(script, settings.localeScript, sizeof(settings.localeScript)) != 0) {
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002559 return false;
2560 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002561 }
2562 }
Narayan Kamath48620f12014-01-20 13:57:11 +00002563
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002564 if (screenConfig != 0) {
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002565 const int layoutDir = screenLayout&MASK_LAYOUTDIR;
2566 const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR;
2567 if (layoutDir != 0 && layoutDir != setLayoutDir) {
2568 return false;
2569 }
2570
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002571 const int screenSize = screenLayout&MASK_SCREENSIZE;
2572 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
2573 // Any screen sizes for larger screens than the setting do not
2574 // match.
2575 if (screenSize != 0 && screenSize > setScreenSize) {
2576 return false;
2577 }
2578
2579 const int screenLong = screenLayout&MASK_SCREENLONG;
2580 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
2581 if (screenLong != 0 && screenLong != setScreenLong) {
2582 return false;
2583 }
2584
2585 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
2586 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
2587 if (uiModeType != 0 && uiModeType != setUiModeType) {
2588 return false;
2589 }
2590
2591 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
2592 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
2593 if (uiModeNight != 0 && uiModeNight != setUiModeNight) {
2594 return false;
2595 }
2596
2597 if (smallestScreenWidthDp != 0
2598 && smallestScreenWidthDp > settings.smallestScreenWidthDp) {
2599 return false;
2600 }
2601 }
Adam Lesinski2738c962015-05-14 14:25:36 -07002602
2603 if (screenConfig2 != 0) {
2604 const int screenRound = screenLayout2 & MASK_SCREENROUND;
2605 const int setScreenRound = settings.screenLayout2 & MASK_SCREENROUND;
2606 if (screenRound != 0 && screenRound != setScreenRound) {
2607 return false;
2608 }
2609 }
2610
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002611 if (screenSizeDp != 0) {
2612 if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002613 if (kDebugTableSuperNoisy) {
2614 ALOGI("Filtering out width %d in requested %d", screenWidthDp,
2615 settings.screenWidthDp);
2616 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002617 return false;
2618 }
2619 if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002620 if (kDebugTableSuperNoisy) {
2621 ALOGI("Filtering out height %d in requested %d", screenHeightDp,
2622 settings.screenHeightDp);
2623 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002624 return false;
2625 }
2626 }
2627 if (screenType != 0) {
2628 if (orientation != 0 && orientation != settings.orientation) {
2629 return false;
2630 }
2631 // density always matches - we can scale it. See isBetterThan
2632 if (touchscreen != 0 && touchscreen != settings.touchscreen) {
2633 return false;
2634 }
2635 }
2636 if (input != 0) {
2637 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
2638 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
2639 if (keysHidden != 0 && keysHidden != setKeysHidden) {
2640 // For compatibility, we count a request for KEYSHIDDEN_NO as also
2641 // matching the more recent KEYSHIDDEN_SOFT. Basically
2642 // KEYSHIDDEN_NO means there is some kind of keyboard available.
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002643 if (kDebugTableSuperNoisy) {
2644 ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
2645 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002646 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002647 if (kDebugTableSuperNoisy) {
2648 ALOGI("No match!");
2649 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002650 return false;
2651 }
2652 }
2653 const int navHidden = inputFlags&MASK_NAVHIDDEN;
2654 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
2655 if (navHidden != 0 && navHidden != setNavHidden) {
2656 return false;
2657 }
2658 if (keyboard != 0 && keyboard != settings.keyboard) {
2659 return false;
2660 }
2661 if (navigation != 0 && navigation != settings.navigation) {
2662 return false;
2663 }
2664 }
2665 if (screenSize != 0) {
2666 if (screenWidth != 0 && screenWidth > settings.screenWidth) {
2667 return false;
2668 }
2669 if (screenHeight != 0 && screenHeight > settings.screenHeight) {
2670 return false;
2671 }
2672 }
2673 if (version != 0) {
2674 if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) {
2675 return false;
2676 }
2677 if (minorVersion != 0 && minorVersion != settings.minorVersion) {
2678 return false;
2679 }
2680 }
2681 return true;
2682}
2683
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002684void ResTable_config::appendDirLocale(String8& out) const {
2685 if (!language[0]) {
2686 return;
2687 }
Roozbeh Pournader79608982016-03-03 15:06:46 -08002688 const bool scriptWasProvided = localeScript[0] != '\0' && !localeScriptWasComputed;
2689 if (!scriptWasProvided && !localeVariant[0]) {
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002690 // Legacy format.
2691 if (out.size() > 0) {
2692 out.append("-");
2693 }
2694
2695 char buf[4];
2696 size_t len = unpackLanguage(buf);
2697 out.append(buf, len);
2698
2699 if (country[0]) {
2700 out.append("-r");
2701 len = unpackRegion(buf);
2702 out.append(buf, len);
2703 }
2704 return;
2705 }
2706
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002707 // We are writing the modified BCP 47 tag.
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002708 // It starts with 'b+' and uses '+' as a separator.
2709
2710 if (out.size() > 0) {
2711 out.append("-");
2712 }
2713 out.append("b+");
2714
2715 char buf[4];
2716 size_t len = unpackLanguage(buf);
2717 out.append(buf, len);
2718
Roozbeh Pournader79608982016-03-03 15:06:46 -08002719 if (scriptWasProvided) {
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002720 out.append("+");
2721 out.append(localeScript, sizeof(localeScript));
2722 }
2723
2724 if (country[0]) {
2725 out.append("+");
2726 len = unpackRegion(buf);
2727 out.append(buf, len);
2728 }
2729
2730 if (localeVariant[0]) {
2731 out.append("+");
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002732 out.append(localeVariant, strnlen(localeVariant, sizeof(localeVariant)));
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002733 }
2734}
2735
Narayan Kamath788fa412014-01-21 15:32:36 +00002736void ResTable_config::getBcp47Locale(char str[RESTABLE_MAX_LOCALE_LEN]) const {
Narayan Kamath48620f12014-01-20 13:57:11 +00002737 memset(str, 0, RESTABLE_MAX_LOCALE_LEN);
2738
2739 // This represents the "any" locale value, which has traditionally been
2740 // represented by the empty string.
2741 if (!language[0] && !country[0]) {
2742 return;
2743 }
2744
2745 size_t charsWritten = 0;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002746 if (language[0]) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002747 charsWritten += unpackLanguage(str);
Narayan Kamath48620f12014-01-20 13:57:11 +00002748 }
2749
Roozbeh Pournader79608982016-03-03 15:06:46 -08002750 if (localeScript[0] && !localeScriptWasComputed) {
Narayan Kamath48620f12014-01-20 13:57:11 +00002751 if (charsWritten) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002752 str[charsWritten++] = '-';
Narayan Kamath48620f12014-01-20 13:57:11 +00002753 }
2754 memcpy(str + charsWritten, localeScript, sizeof(localeScript));
Narayan Kamath788fa412014-01-21 15:32:36 +00002755 charsWritten += sizeof(localeScript);
2756 }
2757
2758 if (country[0]) {
2759 if (charsWritten) {
2760 str[charsWritten++] = '-';
2761 }
2762 charsWritten += unpackRegion(str + charsWritten);
Narayan Kamath48620f12014-01-20 13:57:11 +00002763 }
2764
2765 if (localeVariant[0]) {
2766 if (charsWritten) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002767 str[charsWritten++] = '-';
Narayan Kamath48620f12014-01-20 13:57:11 +00002768 }
2769 memcpy(str + charsWritten, localeVariant, sizeof(localeVariant));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002770 }
2771}
2772
Narayan Kamath788fa412014-01-21 15:32:36 +00002773/* static */ inline bool assignLocaleComponent(ResTable_config* config,
2774 const char* start, size_t size) {
2775
2776 switch (size) {
2777 case 0:
2778 return false;
2779 case 2:
2780 case 3:
2781 config->language[0] ? config->packRegion(start) : config->packLanguage(start);
2782 break;
2783 case 4:
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002784 if ('0' <= start[0] && start[0] <= '9') {
2785 // this is a variant, so fall through
2786 } else {
2787 config->localeScript[0] = toupper(start[0]);
2788 for (size_t i = 1; i < 4; ++i) {
2789 config->localeScript[i] = tolower(start[i]);
2790 }
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002791 break;
Narayan Kamath788fa412014-01-21 15:32:36 +00002792 }
Narayan Kamath788fa412014-01-21 15:32:36 +00002793 case 5:
2794 case 6:
2795 case 7:
2796 case 8:
2797 for (size_t i = 0; i < size; ++i) {
2798 config->localeVariant[i] = tolower(start[i]);
2799 }
2800 break;
2801 default:
2802 return false;
2803 }
2804
2805 return true;
2806}
2807
2808void ResTable_config::setBcp47Locale(const char* in) {
2809 locale = 0;
2810 memset(localeScript, 0, sizeof(localeScript));
2811 memset(localeVariant, 0, sizeof(localeVariant));
2812
2813 const char* separator = in;
2814 const char* start = in;
2815 while ((separator = strchr(start, '-')) != NULL) {
2816 const size_t size = separator - start;
2817 if (!assignLocaleComponent(this, start, size)) {
2818 fprintf(stderr, "Invalid BCP-47 locale string: %s", in);
2819 }
2820
2821 start = (separator + 1);
2822 }
2823
2824 const size_t size = in + strlen(in) - start;
2825 assignLocaleComponent(this, start, size);
Roozbeh Pournader79608982016-03-03 15:06:46 -08002826 localeScriptWasComputed = (localeScript[0] == '\0');
2827 if (localeScriptWasComputed) {
Roozbeh Pournaderb927c552016-01-15 11:23:42 -08002828 computeScript();
Roozbeh Pournader79608982016-03-03 15:06:46 -08002829 }
Narayan Kamath788fa412014-01-21 15:32:36 +00002830}
2831
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002832String8 ResTable_config::toString() const {
2833 String8 res;
2834
2835 if (mcc != 0) {
2836 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07002837 res.appendFormat("mcc%d", dtohs(mcc));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002838 }
2839 if (mnc != 0) {
2840 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07002841 res.appendFormat("mnc%d", dtohs(mnc));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002842 }
Adam Lesinskifab50872014-04-16 14:40:42 -07002843
Adam Lesinski8a9355a2015-03-10 16:55:43 -07002844 appendDirLocale(res);
Narayan Kamath48620f12014-01-20 13:57:11 +00002845
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002846 if ((screenLayout&MASK_LAYOUTDIR) != 0) {
2847 if (res.size() > 0) res.append("-");
2848 switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) {
2849 case ResTable_config::LAYOUTDIR_LTR:
Fabrice Di Meglio8a802db2012-09-05 13:12:02 -07002850 res.append("ldltr");
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002851 break;
2852 case ResTable_config::LAYOUTDIR_RTL:
Fabrice Di Meglio8a802db2012-09-05 13:12:02 -07002853 res.append("ldrtl");
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002854 break;
2855 default:
2856 res.appendFormat("layoutDir=%d",
2857 dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR));
2858 break;
2859 }
2860 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002861 if (smallestScreenWidthDp != 0) {
2862 if (res.size() > 0) res.append("-");
2863 res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp));
2864 }
2865 if (screenWidthDp != 0) {
2866 if (res.size() > 0) res.append("-");
2867 res.appendFormat("w%ddp", dtohs(screenWidthDp));
2868 }
2869 if (screenHeightDp != 0) {
2870 if (res.size() > 0) res.append("-");
2871 res.appendFormat("h%ddp", dtohs(screenHeightDp));
2872 }
2873 if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) {
2874 if (res.size() > 0) res.append("-");
2875 switch (screenLayout&ResTable_config::MASK_SCREENSIZE) {
2876 case ResTable_config::SCREENSIZE_SMALL:
2877 res.append("small");
2878 break;
2879 case ResTable_config::SCREENSIZE_NORMAL:
2880 res.append("normal");
2881 break;
2882 case ResTable_config::SCREENSIZE_LARGE:
2883 res.append("large");
2884 break;
2885 case ResTable_config::SCREENSIZE_XLARGE:
2886 res.append("xlarge");
2887 break;
2888 default:
2889 res.appendFormat("screenLayoutSize=%d",
2890 dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE));
2891 break;
2892 }
2893 }
2894 if ((screenLayout&MASK_SCREENLONG) != 0) {
2895 if (res.size() > 0) res.append("-");
2896 switch (screenLayout&ResTable_config::MASK_SCREENLONG) {
2897 case ResTable_config::SCREENLONG_NO:
2898 res.append("notlong");
2899 break;
2900 case ResTable_config::SCREENLONG_YES:
2901 res.append("long");
2902 break;
2903 default:
2904 res.appendFormat("screenLayoutLong=%d",
2905 dtohs(screenLayout&ResTable_config::MASK_SCREENLONG));
2906 break;
2907 }
2908 }
Adam Lesinski2738c962015-05-14 14:25:36 -07002909 if ((screenLayout2&MASK_SCREENROUND) != 0) {
2910 if (res.size() > 0) res.append("-");
2911 switch (screenLayout2&MASK_SCREENROUND) {
2912 case SCREENROUND_NO:
2913 res.append("notround");
2914 break;
2915 case SCREENROUND_YES:
2916 res.append("round");
2917 break;
2918 default:
2919 res.appendFormat("screenRound=%d", dtohs(screenLayout2&MASK_SCREENROUND));
2920 break;
2921 }
2922 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002923 if (orientation != ORIENTATION_ANY) {
2924 if (res.size() > 0) res.append("-");
2925 switch (orientation) {
2926 case ResTable_config::ORIENTATION_PORT:
2927 res.append("port");
2928 break;
2929 case ResTable_config::ORIENTATION_LAND:
2930 res.append("land");
2931 break;
2932 case ResTable_config::ORIENTATION_SQUARE:
2933 res.append("square");
2934 break;
2935 default:
2936 res.appendFormat("orientation=%d", dtohs(orientation));
2937 break;
2938 }
2939 }
2940 if ((uiMode&MASK_UI_MODE_TYPE) != UI_MODE_TYPE_ANY) {
2941 if (res.size() > 0) res.append("-");
2942 switch (uiMode&ResTable_config::MASK_UI_MODE_TYPE) {
2943 case ResTable_config::UI_MODE_TYPE_DESK:
2944 res.append("desk");
2945 break;
2946 case ResTable_config::UI_MODE_TYPE_CAR:
2947 res.append("car");
2948 break;
2949 case ResTable_config::UI_MODE_TYPE_TELEVISION:
2950 res.append("television");
2951 break;
2952 case ResTable_config::UI_MODE_TYPE_APPLIANCE:
2953 res.append("appliance");
2954 break;
John Spurlock6c191292014-04-03 16:37:27 -04002955 case ResTable_config::UI_MODE_TYPE_WATCH:
2956 res.append("watch");
2957 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002958 default:
2959 res.appendFormat("uiModeType=%d",
2960 dtohs(screenLayout&ResTable_config::MASK_UI_MODE_TYPE));
2961 break;
2962 }
2963 }
2964 if ((uiMode&MASK_UI_MODE_NIGHT) != 0) {
2965 if (res.size() > 0) res.append("-");
2966 switch (uiMode&ResTable_config::MASK_UI_MODE_NIGHT) {
2967 case ResTable_config::UI_MODE_NIGHT_NO:
2968 res.append("notnight");
2969 break;
2970 case ResTable_config::UI_MODE_NIGHT_YES:
2971 res.append("night");
2972 break;
2973 default:
2974 res.appendFormat("uiModeNight=%d",
2975 dtohs(uiMode&MASK_UI_MODE_NIGHT));
2976 break;
2977 }
2978 }
2979 if (density != DENSITY_DEFAULT) {
2980 if (res.size() > 0) res.append("-");
2981 switch (density) {
2982 case ResTable_config::DENSITY_LOW:
2983 res.append("ldpi");
2984 break;
2985 case ResTable_config::DENSITY_MEDIUM:
2986 res.append("mdpi");
2987 break;
2988 case ResTable_config::DENSITY_TV:
2989 res.append("tvdpi");
2990 break;
2991 case ResTable_config::DENSITY_HIGH:
2992 res.append("hdpi");
2993 break;
2994 case ResTable_config::DENSITY_XHIGH:
2995 res.append("xhdpi");
2996 break;
2997 case ResTable_config::DENSITY_XXHIGH:
2998 res.append("xxhdpi");
2999 break;
Adam Lesinski8d5667d2014-08-13 21:02:57 -07003000 case ResTable_config::DENSITY_XXXHIGH:
3001 res.append("xxxhdpi");
3002 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003003 case ResTable_config::DENSITY_NONE:
3004 res.append("nodpi");
3005 break;
Adam Lesinski31245b42014-08-22 19:10:56 -07003006 case ResTable_config::DENSITY_ANY:
3007 res.append("anydpi");
3008 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003009 default:
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08003010 res.appendFormat("%ddpi", dtohs(density));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003011 break;
3012 }
3013 }
3014 if (touchscreen != TOUCHSCREEN_ANY) {
3015 if (res.size() > 0) res.append("-");
3016 switch (touchscreen) {
3017 case ResTable_config::TOUCHSCREEN_NOTOUCH:
3018 res.append("notouch");
3019 break;
3020 case ResTable_config::TOUCHSCREEN_FINGER:
3021 res.append("finger");
3022 break;
3023 case ResTable_config::TOUCHSCREEN_STYLUS:
3024 res.append("stylus");
3025 break;
3026 default:
3027 res.appendFormat("touchscreen=%d", dtohs(touchscreen));
3028 break;
3029 }
3030 }
Adam Lesinskifab50872014-04-16 14:40:42 -07003031 if ((inputFlags&MASK_KEYSHIDDEN) != 0) {
3032 if (res.size() > 0) res.append("-");
3033 switch (inputFlags&MASK_KEYSHIDDEN) {
3034 case ResTable_config::KEYSHIDDEN_NO:
3035 res.append("keysexposed");
3036 break;
3037 case ResTable_config::KEYSHIDDEN_YES:
3038 res.append("keyshidden");
3039 break;
3040 case ResTable_config::KEYSHIDDEN_SOFT:
3041 res.append("keyssoft");
3042 break;
3043 }
3044 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003045 if (keyboard != KEYBOARD_ANY) {
3046 if (res.size() > 0) res.append("-");
3047 switch (keyboard) {
3048 case ResTable_config::KEYBOARD_NOKEYS:
3049 res.append("nokeys");
3050 break;
3051 case ResTable_config::KEYBOARD_QWERTY:
3052 res.append("qwerty");
3053 break;
3054 case ResTable_config::KEYBOARD_12KEY:
3055 res.append("12key");
3056 break;
3057 default:
3058 res.appendFormat("keyboard=%d", dtohs(keyboard));
3059 break;
3060 }
3061 }
Adam Lesinskifab50872014-04-16 14:40:42 -07003062 if ((inputFlags&MASK_NAVHIDDEN) != 0) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003063 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07003064 switch (inputFlags&MASK_NAVHIDDEN) {
3065 case ResTable_config::NAVHIDDEN_NO:
3066 res.append("navexposed");
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003067 break;
Adam Lesinskifab50872014-04-16 14:40:42 -07003068 case ResTable_config::NAVHIDDEN_YES:
3069 res.append("navhidden");
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003070 break;
Adam Lesinskifab50872014-04-16 14:40:42 -07003071 default:
3072 res.appendFormat("inputFlagsNavHidden=%d",
3073 dtohs(inputFlags&MASK_NAVHIDDEN));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003074 break;
3075 }
3076 }
3077 if (navigation != NAVIGATION_ANY) {
3078 if (res.size() > 0) res.append("-");
3079 switch (navigation) {
3080 case ResTable_config::NAVIGATION_NONAV:
3081 res.append("nonav");
3082 break;
3083 case ResTable_config::NAVIGATION_DPAD:
3084 res.append("dpad");
3085 break;
3086 case ResTable_config::NAVIGATION_TRACKBALL:
3087 res.append("trackball");
3088 break;
3089 case ResTable_config::NAVIGATION_WHEEL:
3090 res.append("wheel");
3091 break;
3092 default:
3093 res.appendFormat("navigation=%d", dtohs(navigation));
3094 break;
3095 }
3096 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08003097 if (screenSize != 0) {
3098 if (res.size() > 0) res.append("-");
3099 res.appendFormat("%dx%d", dtohs(screenWidth), dtohs(screenHeight));
3100 }
3101 if (version != 0) {
3102 if (res.size() > 0) res.append("-");
3103 res.appendFormat("v%d", dtohs(sdkVersion));
3104 if (minorVersion != 0) {
3105 res.appendFormat(".%d", dtohs(minorVersion));
3106 }
3107 }
3108
3109 return res;
3110}
3111
3112// --------------------------------------------------------------------
3113// --------------------------------------------------------------------
3114// --------------------------------------------------------------------
3115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116struct ResTable::Header
3117{
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003118 Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL),
3119 resourceIDMap(NULL), resourceIDMapSize(0) { }
3120
3121 ~Header()
3122 {
3123 free(resourceIDMap);
3124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003126 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 void* ownedData;
3128 const ResTable_header* header;
3129 size_t size;
3130 const uint8_t* dataEnd;
3131 size_t index;
Narayan Kamath7c4887f2014-01-27 17:32:37 +00003132 int32_t cookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
3134 ResStringPool values;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003135 uint32_t* resourceIDMap;
3136 size_t resourceIDMapSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137};
3138
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003139struct ResTable::Entry {
3140 ResTable_config config;
3141 const ResTable_entry* entry;
3142 const ResTable_type* type;
3143 uint32_t specFlags;
3144 const Package* package;
3145
3146 StringPoolRef typeStr;
3147 StringPoolRef keyStr;
3148};
3149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150struct ResTable::Type
3151{
3152 Type(const Header* _header, const Package* _package, size_t count)
3153 : header(_header), package(_package), entryCount(count),
3154 typeSpec(NULL), typeSpecFlags(NULL) { }
3155 const Header* const header;
3156 const Package* const package;
3157 const size_t entryCount;
3158 const ResTable_typeSpec* typeSpec;
3159 const uint32_t* typeSpecFlags;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003160 IdmapEntries idmapEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 Vector<const ResTable_type*> configs;
3162};
3163
3164struct ResTable::Package
3165{
Dianne Hackborn78c40512009-07-06 11:07:40 -07003166 Package(ResTable* _owner, const Header* _header, const ResTable_package* _package)
Adam Lesinski18560882014-08-15 17:18:21 +00003167 : owner(_owner), header(_header), package(_package), typeIdOffset(0) {
3168 if (dtohs(package->header.headerSize) == sizeof(package)) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003169 // The package structure is the same size as the definition.
3170 // This means it contains the typeIdOffset field.
Adam Lesinski18560882014-08-15 17:18:21 +00003171 typeIdOffset = package->typeIdOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
3173 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003174
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003175 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 const Header* const header;
Adam Lesinski18560882014-08-15 17:18:21 +00003177 const ResTable_package* const package;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178
Dianne Hackborn78c40512009-07-06 11:07:40 -07003179 ResStringPool typeStrings;
3180 ResStringPool keyStrings;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003181
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003182 size_t typeIdOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183};
3184
3185// A group of objects describing a particular resource package.
3186// The first in 'package' is always the root object (from the resource
3187// table that defined the package); the ones after are skins on top of it.
3188struct ResTable::PackageGroup
3189{
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003190 PackageGroup(
3191 ResTable* _owner, const String16& _name, uint32_t _id,
3192 bool appAsLib, bool _isSystemAsset)
Adam Lesinskide898ff2014-01-29 18:20:45 -08003193 : owner(_owner)
3194 , name(_name)
3195 , id(_id)
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003196 , largestTypeId(0)
Tao Baia6d7e3f2015-09-01 18:49:54 -07003197 , dynamicRefTable(static_cast<uint8_t>(_id), appAsLib)
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003198 , isSystemAsset(_isSystemAsset)
Adam Lesinskide898ff2014-01-29 18:20:45 -08003199 { }
3200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 ~PackageGroup() {
3202 clearBagCache();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003203 const size_t numTypes = types.size();
3204 for (size_t i = 0; i < numTypes; i++) {
3205 const TypeList& typeList = types[i];
3206 const size_t numInnerTypes = typeList.size();
3207 for (size_t j = 0; j < numInnerTypes; j++) {
3208 if (typeList[j]->package->owner == owner) {
3209 delete typeList[j];
3210 }
3211 }
3212 }
3213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 const size_t N = packages.size();
3215 for (size_t i=0; i<N; i++) {
Dianne Hackborn78c40512009-07-06 11:07:40 -07003216 Package* pkg = packages[i];
3217 if (pkg->owner == owner) {
3218 delete pkg;
3219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 }
3221 }
3222
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003223 /**
3224 * Clear all cache related data that depends on parameters/configuration.
3225 * This includes the bag caches and filtered types.
3226 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 void clearBagCache() {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003228 for (size_t i = 0; i < typeCacheEntries.size(); i++) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003229 if (kDebugTableNoisy) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003230 printf("type=%zu\n", i);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003231 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003232 const TypeList& typeList = types[i];
3233 if (!typeList.isEmpty()) {
3234 TypeCacheEntry& cacheEntry = typeCacheEntries.editItemAt(i);
3235
3236 // Reset the filtered configurations.
3237 cacheEntry.filteredConfigs.clear();
3238
3239 bag_set** typeBags = cacheEntry.cachedBags;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003240 if (kDebugTableNoisy) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003241 printf("typeBags=%p\n", typeBags);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003242 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003243
3244 if (typeBags) {
3245 const size_t N = typeList[0]->entryCount;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003246 if (kDebugTableNoisy) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003247 printf("type->entryCount=%zu\n", N);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003248 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003249 for (size_t j = 0; j < N; j++) {
3250 if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF) {
3251 free(typeBags[j]);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003254 free(typeBags);
3255 cacheEntry.cachedBags = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 }
3257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 }
3259 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003260
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003261 ssize_t findType16(const char16_t* type, size_t len) const {
3262 const size_t N = packages.size();
3263 for (size_t i = 0; i < N; i++) {
3264 ssize_t index = packages[i]->typeStrings.indexOfString(type, len);
3265 if (index >= 0) {
3266 return index + packages[i]->typeIdOffset;
3267 }
3268 }
3269 return -1;
3270 }
3271
3272 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 String16 const name;
3274 uint32_t const id;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003275
3276 // This is mainly used to keep track of the loaded packages
3277 // and to clean them up properly. Accessing resources happens from
3278 // the 'types' array.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 Vector<Package*> packages;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003280
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003281 ByteBucketArray<TypeList> types;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003283 uint8_t largestTypeId;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003284
Adam Lesinskiff5808d2016-02-23 17:49:53 -08003285 // Cached objects dependent on the parameters/configuration of this ResTable.
3286 // Gets cleared whenever the parameters/configuration changes.
3287 // These are stored here in a parallel structure because the data in `types` may
3288 // be shared by other ResTable's (framework resources are shared this way).
3289 ByteBucketArray<TypeCacheEntry> typeCacheEntries;
Adam Lesinskide898ff2014-01-29 18:20:45 -08003290
3291 // The table mapping dynamic references to resolved references for
3292 // this package group.
3293 // TODO: We may be able to support dynamic references in overlays
3294 // by having these tables in a per-package scope rather than
3295 // per-package-group.
3296 DynamicRefTable dynamicRefTable;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003297
3298 // If the package group comes from a system asset. Used in
3299 // determining non-system locales.
3300 const bool isSystemAsset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301};
3302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303ResTable::Theme::Theme(const ResTable& table)
3304 : mTable(table)
Alan Viverettec1d52792015-05-05 09:49:03 -07003305 , mTypeSpecFlags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306{
3307 memset(mPackages, 0, sizeof(mPackages));
3308}
3309
3310ResTable::Theme::~Theme()
3311{
3312 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3313 package_info* pi = mPackages[i];
3314 if (pi != NULL) {
3315 free_package(pi);
3316 }
3317 }
3318}
3319
3320void ResTable::Theme::free_package(package_info* pi)
3321{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003322 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 theme_entry* te = pi->types[j].entries;
3324 if (te != NULL) {
3325 free(te);
3326 }
3327 }
3328 free(pi);
3329}
3330
3331ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi)
3332{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003333 package_info* newpi = (package_info*)malloc(sizeof(package_info));
3334 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 size_t cnt = pi->types[j].numEntries;
3336 newpi->types[j].numEntries = cnt;
3337 theme_entry* te = pi->types[j].entries;
Vishwath Mohan6a2c23d2015-03-09 18:55:11 -07003338 size_t cnt_max = SIZE_MAX / sizeof(theme_entry);
3339 if (te != NULL && (cnt < 0xFFFFFFFF-1) && (cnt < cnt_max)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry));
3341 newpi->types[j].entries = newte;
3342 memcpy(newte, te, cnt*sizeof(theme_entry));
3343 } else {
3344 newpi->types[j].entries = NULL;
3345 }
3346 }
3347 return newpi;
3348}
3349
3350status_t ResTable::Theme::applyStyle(uint32_t resID, bool force)
3351{
3352 const bag_entry* bag;
3353 uint32_t bagTypeSpecFlags = 0;
3354 mTable.lock();
3355 const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003356 if (kDebugTableNoisy) {
3357 ALOGV("Applying style 0x%08x to theme %p, count=%zu", resID, this, N);
3358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (N < 0) {
3360 mTable.unlock();
3361 return N;
3362 }
3363
Alan Viverettec1d52792015-05-05 09:49:03 -07003364 mTypeSpecFlags |= bagTypeSpecFlags;
3365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 uint32_t curPackage = 0xffffffff;
3367 ssize_t curPackageIndex = 0;
3368 package_info* curPI = NULL;
3369 uint32_t curType = 0xffffffff;
3370 size_t numEntries = 0;
3371 theme_entry* curEntries = NULL;
3372
3373 const bag_entry* end = bag + N;
3374 while (bag < end) {
3375 const uint32_t attrRes = bag->map.name.ident;
3376 const uint32_t p = Res_GETPACKAGE(attrRes);
3377 const uint32_t t = Res_GETTYPE(attrRes);
3378 const uint32_t e = Res_GETENTRY(attrRes);
3379
3380 if (curPackage != p) {
3381 const ssize_t pidx = mTable.getResourcePackageIndex(attrRes);
3382 if (pidx < 0) {
Steve Block3762c312012-01-06 19:20:56 +00003383 ALOGE("Style contains key with bad package: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 bag++;
3385 continue;
3386 }
3387 curPackage = p;
3388 curPackageIndex = pidx;
3389 curPI = mPackages[pidx];
3390 if (curPI == NULL) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003391 curPI = (package_info*)malloc(sizeof(package_info));
3392 memset(curPI, 0, sizeof(*curPI));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 mPackages[pidx] = curPI;
3394 }
3395 curType = 0xffffffff;
3396 }
3397 if (curType != t) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003398 if (t > Res_MAXTYPE) {
Steve Block3762c312012-01-06 19:20:56 +00003399 ALOGE("Style contains key with bad type: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 bag++;
3401 continue;
3402 }
3403 curType = t;
3404 curEntries = curPI->types[t].entries;
3405 if (curEntries == NULL) {
3406 PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003407 const TypeList& typeList = grp->types[t];
Vishwath Mohan6a2c23d2015-03-09 18:55:11 -07003408 size_t cnt = typeList.isEmpty() ? 0 : typeList[0]->entryCount;
3409 size_t cnt_max = SIZE_MAX / sizeof(theme_entry);
3410 size_t buff_size = (cnt < cnt_max && cnt < 0xFFFFFFFF-1) ?
3411 cnt*sizeof(theme_entry) : 0;
3412 curEntries = (theme_entry*)malloc(buff_size);
3413 memset(curEntries, Res_value::TYPE_NULL, buff_size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 curPI->types[t].numEntries = cnt;
3415 curPI->types[t].entries = curEntries;
3416 }
3417 numEntries = curPI->types[t].numEntries;
3418 }
3419 if (e >= numEntries) {
Steve Block3762c312012-01-06 19:20:56 +00003420 ALOGE("Style contains key with bad entry: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 bag++;
3422 continue;
3423 }
3424 theme_entry* curEntry = curEntries + e;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003425 if (kDebugTableNoisy) {
3426 ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x",
3427 attrRes, bag->map.value.dataType, bag->map.value.data,
3428 curEntry->value.dataType);
3429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (force || curEntry->value.dataType == Res_value::TYPE_NULL) {
3431 curEntry->stringBlock = bag->stringBlock;
3432 curEntry->typeSpecFlags |= bagTypeSpecFlags;
3433 curEntry->value = bag->map.value;
3434 }
3435
3436 bag++;
3437 }
3438
3439 mTable.unlock();
3440
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003441 if (kDebugTableTheme) {
3442 ALOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this);
3443 dumpToLog();
3444 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 return NO_ERROR;
3447}
3448
3449status_t ResTable::Theme::setTo(const Theme& other)
3450{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003451 if (kDebugTableTheme) {
3452 ALOGI("Setting theme %p from theme %p...\n", this, &other);
3453 dumpToLog();
3454 other.dumpToLog();
3455 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 if (&mTable == &other.mTable) {
3458 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3459 if (mPackages[i] != NULL) {
3460 free_package(mPackages[i]);
3461 }
3462 if (other.mPackages[i] != NULL) {
3463 mPackages[i] = copy_package(other.mPackages[i]);
3464 } else {
3465 mPackages[i] = NULL;
3466 }
3467 }
3468 } else {
3469 // @todo: need to really implement this, not just copy
3470 // the system package (which is still wrong because it isn't
3471 // fixing up resource references).
3472 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3473 if (mPackages[i] != NULL) {
3474 free_package(mPackages[i]);
3475 }
3476 if (i == 0 && other.mPackages[i] != NULL) {
3477 mPackages[i] = copy_package(other.mPackages[i]);
3478 } else {
3479 mPackages[i] = NULL;
3480 }
3481 }
3482 }
3483
Alan Viverettec1d52792015-05-05 09:49:03 -07003484 mTypeSpecFlags = other.mTypeSpecFlags;
3485
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003486 if (kDebugTableTheme) {
3487 ALOGI("Final theme:");
3488 dumpToLog();
3489 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 return NO_ERROR;
3492}
3493
Alan Viverettee54d2452015-05-06 10:41:43 -07003494status_t ResTable::Theme::clear()
3495{
3496 if (kDebugTableTheme) {
3497 ALOGI("Clearing theme %p...\n", this);
3498 dumpToLog();
3499 }
3500
3501 for (size_t i = 0; i < Res_MAXPACKAGE; i++) {
3502 if (mPackages[i] != NULL) {
3503 free_package(mPackages[i]);
3504 mPackages[i] = NULL;
3505 }
3506 }
3507
3508 mTypeSpecFlags = 0;
3509
3510 if (kDebugTableTheme) {
3511 ALOGI("Final theme:");
3512 dumpToLog();
3513 }
3514
3515 return NO_ERROR;
3516}
3517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue,
3519 uint32_t* outTypeSpecFlags) const
3520{
3521 int cnt = 20;
3522
3523 if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 do {
3526 const ssize_t p = mTable.getResourcePackageIndex(resID);
3527 const uint32_t t = Res_GETTYPE(resID);
3528 const uint32_t e = Res_GETENTRY(resID);
3529
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003530 if (kDebugTableTheme) {
3531 ALOGI("Looking up attr 0x%08x in theme %p", resID, this);
3532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533
3534 if (p >= 0) {
3535 const package_info* const pi = mPackages[p];
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003536 if (kDebugTableTheme) {
3537 ALOGI("Found package: %p", pi);
3538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 if (pi != NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003540 if (kDebugTableTheme) {
3541 ALOGI("Desired type index is %zd in avail %zu", t, Res_MAXTYPE + 1);
3542 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003543 if (t <= Res_MAXTYPE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 const type_info& ti = pi->types[t];
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003545 if (kDebugTableTheme) {
3546 ALOGI("Desired entry index is %u in avail %zu", e, ti.numEntries);
3547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 if (e < ti.numEntries) {
3549 const theme_entry& te = ti.entries[e];
Dianne Hackbornb8d81672009-11-20 14:26:42 -08003550 if (outTypeSpecFlags != NULL) {
3551 *outTypeSpecFlags |= te.typeSpecFlags;
3552 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003553 if (kDebugTableTheme) {
3554 ALOGI("Theme value: type=0x%x, data=0x%08x",
3555 te.value.dataType, te.value.data);
3556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 const uint8_t type = te.value.dataType;
3558 if (type == Res_value::TYPE_ATTRIBUTE) {
3559 if (cnt > 0) {
3560 cnt--;
3561 resID = te.value.data;
3562 continue;
3563 }
Steve Block8564c8d2012-01-05 23:22:43 +00003564 ALOGW("Too many attribute references, stopped at: 0x%08x\n", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 return BAD_INDEX;
3566 } else if (type != Res_value::TYPE_NULL) {
3567 *outValue = te.value;
3568 return te.stringBlock;
3569 }
3570 return BAD_INDEX;
3571 }
3572 }
3573 }
3574 }
3575 break;
3576
3577 } while (true);
3578
3579 return BAD_INDEX;
3580}
3581
3582ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue,
3583 ssize_t blockIndex, uint32_t* outLastRef,
Dianne Hackborn0d221012009-07-29 15:41:19 -07003584 uint32_t* inoutTypeSpecFlags, ResTable_config* inoutConfig) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585{
3586 //printf("Resolving type=0x%x\n", inOutValue->dataType);
3587 if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) {
3588 uint32_t newTypeSpecFlags;
3589 blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003590 if (kDebugTableTheme) {
3591 ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=0x%x\n",
3592 (int)blockIndex, (int)inOutValue->dataType, inOutValue->data);
3593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags;
3595 //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType);
3596 if (blockIndex < 0) {
3597 return blockIndex;
3598 }
3599 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07003600 return mTable.resolveReference(inOutValue, blockIndex, outLastRef,
3601 inoutTypeSpecFlags, inoutConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602}
3603
Alan Viverettec1d52792015-05-05 09:49:03 -07003604uint32_t ResTable::Theme::getChangingConfigurations() const
3605{
3606 return mTypeSpecFlags;
3607}
3608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609void ResTable::Theme::dumpToLog() const
3610{
Steve Block6215d3f2012-01-04 20:05:49 +00003611 ALOGI("Theme %p:\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3613 package_info* pi = mPackages[i];
3614 if (pi == NULL) continue;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003615
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003616 ALOGI(" Package #0x%02x:\n", (int)(i + 1));
3617 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 type_info& ti = pi->types[j];
3619 if (ti.numEntries == 0) continue;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003620 ALOGI(" Type #0x%02x:\n", (int)(j + 1));
3621 for (size_t k = 0; k < ti.numEntries; k++) {
3622 const theme_entry& te = ti.entries[k];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 if (te.value.dataType == Res_value::TYPE_NULL) continue;
Steve Block6215d3f2012-01-04 20:05:49 +00003624 ALOGI(" 0x%08x: t=0x%x, d=0x%08x (block=%d)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 (int)Res_MAKEID(i, j, k),
3626 te.value.dataType, (int)te.value.data, (int)te.stringBlock);
3627 }
3628 }
3629 }
3630}
3631
3632ResTable::ResTable()
Adam Lesinskide898ff2014-01-29 18:20:45 -08003633 : mError(NO_INIT), mNextPackageId(2)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634{
3635 memset(&mParams, 0, sizeof(mParams));
3636 memset(mPackageMap, 0, sizeof(mPackageMap));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003637 if (kDebugTableSuperNoisy) {
3638 ALOGI("Creating ResTable %p\n", this);
3639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640}
3641
Narayan Kamath7c4887f2014-01-27 17:32:37 +00003642ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData)
Adam Lesinskide898ff2014-01-29 18:20:45 -08003643 : mError(NO_INIT), mNextPackageId(2)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644{
3645 memset(&mParams, 0, sizeof(mParams));
3646 memset(mPackageMap, 0, sizeof(mPackageMap));
Tao Baia6d7e3f2015-09-01 18:49:54 -07003647 addInternal(data, size, NULL, 0, false, cookie, copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table");
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003649 if (kDebugTableSuperNoisy) {
3650 ALOGI("Creating ResTable %p\n", this);
3651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652}
3653
3654ResTable::~ResTable()
3655{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003656 if (kDebugTableSuperNoisy) {
3657 ALOGI("Destroying ResTable in %p\n", this);
3658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 uninit();
3660}
3661
3662inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const
3663{
3664 return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1;
3665}
3666
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003667status_t ResTable::add(const void* data, size_t size, const int32_t cookie, bool copyData) {
Tao Baia6d7e3f2015-09-01 18:49:54 -07003668 return addInternal(data, size, NULL, 0, false, cookie, copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669}
3670
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003671status_t ResTable::add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
Tao Baia6d7e3f2015-09-01 18:49:54 -07003672 const int32_t cookie, bool copyData, bool appAsLib) {
3673 return addInternal(data, size, idmapData, idmapDataSize, appAsLib, cookie, copyData);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003674}
3675
3676status_t ResTable::add(Asset* asset, const int32_t cookie, bool copyData) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 const void* data = asset->getBuffer(true);
3678 if (data == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003679 ALOGW("Unable to get buffer of resource asset file");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 return UNKNOWN_ERROR;
3681 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003682
Tao Baia6d7e3f2015-09-01 18:49:54 -07003683 return addInternal(data, static_cast<size_t>(asset->getLength()), NULL, false, 0, cookie,
3684 copyData);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003685}
3686
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003687status_t ResTable::add(
3688 Asset* asset, Asset* idmapAsset, const int32_t cookie, bool copyData,
3689 bool appAsLib, bool isSystemAsset) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003690 const void* data = asset->getBuffer(true);
3691 if (data == NULL) {
3692 ALOGW("Unable to get buffer of resource asset file");
3693 return UNKNOWN_ERROR;
3694 }
3695
3696 size_t idmapSize = 0;
3697 const void* idmapData = NULL;
3698 if (idmapAsset != NULL) {
3699 idmapData = idmapAsset->getBuffer(true);
3700 if (idmapData == NULL) {
3701 ALOGW("Unable to get buffer of idmap asset file");
3702 return UNKNOWN_ERROR;
3703 }
3704 idmapSize = static_cast<size_t>(idmapAsset->getLength());
3705 }
3706
3707 return addInternal(data, static_cast<size_t>(asset->getLength()),
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003708 idmapData, idmapSize, appAsLib, cookie, copyData, isSystemAsset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709}
3710
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003711status_t ResTable::add(ResTable* src, bool isSystemAsset)
Dianne Hackborn78c40512009-07-06 11:07:40 -07003712{
3713 mError = src->mError;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003714
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003715 for (size_t i=0; i < src->mHeaders.size(); i++) {
Dianne Hackborn78c40512009-07-06 11:07:40 -07003716 mHeaders.add(src->mHeaders[i]);
3717 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003718
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003719 for (size_t i=0; i < src->mPackageGroups.size(); i++) {
Dianne Hackborn78c40512009-07-06 11:07:40 -07003720 PackageGroup* srcPg = src->mPackageGroups[i];
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003721 PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id,
3722 false /* appAsLib */, isSystemAsset || srcPg->isSystemAsset);
Dianne Hackborn78c40512009-07-06 11:07:40 -07003723 for (size_t j=0; j<srcPg->packages.size(); j++) {
3724 pg->packages.add(srcPg->packages[j]);
3725 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003726
3727 for (size_t j = 0; j < srcPg->types.size(); j++) {
3728 if (srcPg->types[j].isEmpty()) {
3729 continue;
3730 }
3731
3732 TypeList& typeList = pg->types.editItemAt(j);
3733 typeList.appendVector(srcPg->types[j]);
3734 }
Adam Lesinski6022deb2014-08-20 14:59:19 -07003735 pg->dynamicRefTable.addMappings(srcPg->dynamicRefTable);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003736 pg->largestTypeId = max(pg->largestTypeId, srcPg->largestTypeId);
Dianne Hackborn78c40512009-07-06 11:07:40 -07003737 mPackageGroups.add(pg);
3738 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003739
Dianne Hackborn78c40512009-07-06 11:07:40 -07003740 memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap));
Mark Salyzyn00adb862014-03-19 11:00:06 -07003741
Dianne Hackborn78c40512009-07-06 11:07:40 -07003742 return mError;
3743}
3744
Adam Lesinskide898ff2014-01-29 18:20:45 -08003745status_t ResTable::addEmpty(const int32_t cookie) {
3746 Header* header = new Header(this);
3747 header->index = mHeaders.size();
3748 header->cookie = cookie;
3749 header->values.setToEmpty();
3750 header->ownedData = calloc(1, sizeof(ResTable_header));
3751
3752 ResTable_header* resHeader = (ResTable_header*) header->ownedData;
3753 resHeader->header.type = RES_TABLE_TYPE;
3754 resHeader->header.headerSize = sizeof(ResTable_header);
3755 resHeader->header.size = sizeof(ResTable_header);
3756
3757 header->header = (const ResTable_header*) resHeader;
3758 mHeaders.add(header);
Adam Lesinski961dda72014-06-09 17:10:29 -07003759 return (mError=NO_ERROR);
Adam Lesinskide898ff2014-01-29 18:20:45 -08003760}
3761
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003762status_t ResTable::addInternal(const void* data, size_t dataSize, const void* idmapData, size_t idmapDataSize,
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003763 bool appAsLib, const int32_t cookie, bool copyData, bool isSystemAsset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003765 if (!data) {
3766 return NO_ERROR;
3767 }
3768
Adam Lesinskif28d5052014-07-25 15:25:04 -07003769 if (dataSize < sizeof(ResTable_header)) {
3770 ALOGE("Invalid data. Size(%d) is smaller than a ResTable_header(%d).",
3771 (int) dataSize, (int) sizeof(ResTable_header));
3772 return UNKNOWN_ERROR;
3773 }
3774
Dianne Hackborn78c40512009-07-06 11:07:40 -07003775 Header* header = new Header(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 header->index = mHeaders.size();
3777 header->cookie = cookie;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003778 if (idmapData != NULL) {
3779 header->resourceIDMap = (uint32_t*) malloc(idmapDataSize);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003780 if (header->resourceIDMap == NULL) {
3781 delete header;
3782 return (mError = NO_MEMORY);
3783 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003784 memcpy(header->resourceIDMap, idmapData, idmapDataSize);
3785 header->resourceIDMapSize = idmapDataSize;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 mHeaders.add(header);
3788
3789 const bool notDeviceEndian = htods(0xf0) != 0xf0;
3790
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003791 if (kDebugLoadTableNoisy) {
3792 ALOGV("Adding resources to ResTable: data=%p, size=%zu, cookie=%d, copy=%d "
3793 "idmap=%p\n", data, dataSize, cookie, copyData, idmapData);
3794 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 if (copyData || notDeviceEndian) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003797 header->ownedData = malloc(dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 if (header->ownedData == NULL) {
3799 return (mError=NO_MEMORY);
3800 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003801 memcpy(header->ownedData, data, dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 data = header->ownedData;
3803 }
3804
3805 header->header = (const ResTable_header*)data;
3806 header->size = dtohl(header->header->header.size);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003807 if (kDebugLoadTableSuperNoisy) {
3808 ALOGI("Got size %zu, again size 0x%x, raw size 0x%x\n", header->size,
3809 dtohl(header->header->header.size), header->header->header.size);
3810 }
3811 if (kDebugLoadTableNoisy) {
3812 ALOGV("Loading ResTable @%p:\n", header->header);
3813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 if (dtohs(header->header->header.headerSize) > header->size
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003815 || header->size > dataSize) {
Steve Block8564c8d2012-01-05 23:22:43 +00003816 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is larger than data size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 (int)dtohs(header->header->header.headerSize),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003818 (int)header->size, (int)dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 return (mError=BAD_TYPE);
3820 }
3821 if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003822 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is not on an integer boundary\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 (int)dtohs(header->header->header.headerSize),
3824 (int)header->size);
3825 return (mError=BAD_TYPE);
3826 }
3827 header->dataEnd = ((const uint8_t*)header->header) + header->size;
3828
3829 // Iterate through all chunks.
3830 size_t curPackage = 0;
3831
3832 const ResChunk_header* chunk =
3833 (const ResChunk_header*)(((const uint8_t*)header->header)
3834 + dtohs(header->header->header.headerSize));
3835 while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) &&
3836 ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) {
3837 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable");
3838 if (err != NO_ERROR) {
3839 return (mError=err);
3840 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003841 if (kDebugTableNoisy) {
3842 ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
3843 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
3844 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
3845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 const size_t csize = dtohl(chunk->size);
3847 const uint16_t ctype = dtohs(chunk->type);
3848 if (ctype == RES_STRING_POOL_TYPE) {
3849 if (header->values.getError() != NO_ERROR) {
3850 // Only use the first string chunk; ignore any others that
3851 // may appear.
3852 status_t err = header->values.setTo(chunk, csize);
3853 if (err != NO_ERROR) {
3854 return (mError=err);
3855 }
3856 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00003857 ALOGW("Multiple string chunks found in resource table.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
3859 } else if (ctype == RES_TABLE_PACKAGE_TYPE) {
3860 if (curPackage >= dtohl(header->header->packageCount)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003861 ALOGW("More package chunks were found than the %d declared in the header.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 dtohl(header->header->packageCount));
3863 return (mError=BAD_TYPE);
3864 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003865
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08003866 if (parsePackage(
3867 (ResTable_package*)chunk, header, appAsLib, isSystemAsset) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 return mError;
3869 }
3870 curPackage++;
3871 } else {
Patrik Bannura443dd932014-02-12 13:38:54 +01003872 ALOGW("Unknown chunk type 0x%x in table at %p.\n",
3873 ctype,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
3875 }
3876 chunk = (const ResChunk_header*)
3877 (((const uint8_t*)chunk) + csize);
3878 }
3879
3880 if (curPackage < dtohl(header->header->packageCount)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003881 ALOGW("Fewer package chunks (%d) were found than the %d declared in the header.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 (int)curPackage, dtohl(header->header->packageCount));
3883 return (mError=BAD_TYPE);
3884 }
3885 mError = header->values.getError();
3886 if (mError != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +00003887 ALOGW("No string values found in resource table!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003889
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003890 if (kDebugTableNoisy) {
3891 ALOGV("Returning from add with mError=%d\n", mError);
3892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 return mError;
3894}
3895
3896status_t ResTable::getError() const
3897{
3898 return mError;
3899}
3900
3901void ResTable::uninit()
3902{
3903 mError = NO_INIT;
3904 size_t N = mPackageGroups.size();
3905 for (size_t i=0; i<N; i++) {
3906 PackageGroup* g = mPackageGroups[i];
3907 delete g;
3908 }
3909 N = mHeaders.size();
3910 for (size_t i=0; i<N; i++) {
3911 Header* header = mHeaders[i];
Dianne Hackborn78c40512009-07-06 11:07:40 -07003912 if (header->owner == this) {
3913 if (header->ownedData) {
3914 free(header->ownedData);
3915 }
3916 delete header;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 }
3919
3920 mPackageGroups.clear();
3921 mHeaders.clear();
3922}
3923
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07003924bool ResTable::getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925{
3926 if (mError != NO_ERROR) {
3927 return false;
3928 }
3929
3930 const ssize_t p = getResourcePackageIndex(resID);
3931 const int t = Res_GETTYPE(resID);
3932 const int e = Res_GETENTRY(resID);
3933
3934 if (p < 0) {
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003935 if (Res_GETPACKAGE(resID)+1 == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003936 ALOGW("No package identifier when getting name for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003937 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00003938 ALOGW("No known package when getting name for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 return false;
3941 }
3942 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003943 ALOGW("No type identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 return false;
3945 }
3946
3947 const PackageGroup* const grp = mPackageGroups[p];
3948 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003949 ALOGW("Bad identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 return false;
3951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003953 Entry entry;
3954 status_t err = getEntry(grp, t, e, NULL, &entry);
3955 if (err != NO_ERROR) {
3956 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 }
3958
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003959 outName->package = grp->name.string();
3960 outName->packageLen = grp->name.size();
3961 if (allowUtf8) {
3962 outName->type8 = entry.typeStr.string8(&outName->typeLen);
3963 outName->name8 = entry.keyStr.string8(&outName->nameLen);
3964 } else {
3965 outName->type8 = NULL;
3966 outName->name8 = NULL;
3967 }
3968 if (outName->type8 == NULL) {
3969 outName->type = entry.typeStr.string16(&outName->typeLen);
3970 // If we have a bad index for some reason, we should abort.
3971 if (outName->type == NULL) {
3972 return false;
3973 }
3974 }
3975 if (outName->name8 == NULL) {
3976 outName->name = entry.keyStr.string16(&outName->nameLen);
3977 // If we have a bad index for some reason, we should abort.
3978 if (outName->name == NULL) {
3979 return false;
3980 }
3981 }
3982
3983 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984}
3985
Kenny Root55fc8502010-10-28 14:47:01 -07003986ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, uint16_t density,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 uint32_t* outSpecFlags, ResTable_config* outConfig) const
3988{
3989 if (mError != NO_ERROR) {
3990 return mError;
3991 }
3992
3993 const ssize_t p = getResourcePackageIndex(resID);
3994 const int t = Res_GETTYPE(resID);
3995 const int e = Res_GETENTRY(resID);
3996
3997 if (p < 0) {
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003998 if (Res_GETPACKAGE(resID)+1 == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003999 ALOGW("No package identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07004000 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004001 ALOGW("No known package when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07004002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 return BAD_INDEX;
4004 }
4005 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004006 ALOGW("No type identifier when getting value for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 return BAD_INDEX;
4008 }
4009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 const PackageGroup* const grp = mPackageGroups[p];
4011 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00004012 ALOGW("Bad identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08004013 return BAD_INDEX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 }
Kenny Root55fc8502010-10-28 14:47:01 -07004015
4016 // Allow overriding density
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004017 ResTable_config desiredConfig = mParams;
Kenny Root55fc8502010-10-28 14:47:01 -07004018 if (density > 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004019 desiredConfig.density = density;
Kenny Root55fc8502010-10-28 14:47:01 -07004020 }
4021
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004022 Entry entry;
4023 status_t err = getEntry(grp, t, e, &desiredConfig, &entry);
4024 if (err != NO_ERROR) {
Adam Lesinskide7de472014-11-03 12:03:08 -08004025 // Only log the failure when we're not running on the host as
4026 // part of a tool. The caller will do its own logging.
4027#ifndef STATIC_ANDROIDFW_FOR_TOOLS
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004028 ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) (error %d)\n",
4029 resID, t, e, err);
Adam Lesinskide7de472014-11-03 12:03:08 -08004030#endif
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004031 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 }
4033
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004034 if ((dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) != 0) {
4035 if (!mayBeBag) {
4036 ALOGW("Requesting resource 0x%08x failed because it is complex\n", resID);
Adam Lesinskide898ff2014-01-29 18:20:45 -08004037 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004038 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004041 const Res_value* value = reinterpret_cast<const Res_value*>(
4042 reinterpret_cast<const uint8_t*>(entry.entry) + entry.entry->size);
4043
4044 outValue->size = dtohs(value->size);
4045 outValue->res0 = value->res0;
4046 outValue->dataType = value->dataType;
4047 outValue->data = dtohl(value->data);
4048
4049 // The reference may be pointing to a resource in a shared library. These
4050 // references have build-time generated package IDs. These ids may not match
4051 // the actual package IDs of the corresponding packages in this ResTable.
4052 // We need to fix the package ID based on a mapping.
4053 if (grp->dynamicRefTable.lookupResourceValue(outValue) != NO_ERROR) {
4054 ALOGW("Failed to resolve referenced package: 0x%08x", outValue->data);
4055 return BAD_VALUE;
Kenny Root55fc8502010-10-28 14:47:01 -07004056 }
4057
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004058 if (kDebugTableNoisy) {
4059 size_t len;
4060 printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n",
4061 entry.package->header->index,
4062 outValue->dataType,
4063 outValue->dataType == Res_value::TYPE_STRING ?
4064 String8(entry.package->header->values.stringAt(outValue->data, &len)).string() :
4065 "",
4066 outValue->data);
4067 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004068
4069 if (outSpecFlags != NULL) {
4070 *outSpecFlags = entry.specFlags;
4071 }
4072
4073 if (outConfig != NULL) {
4074 *outConfig = entry.config;
4075 }
4076
4077 return entry.package->header->index;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078}
4079
4080ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex,
Dianne Hackborn0d221012009-07-29 15:41:19 -07004081 uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags,
4082 ResTable_config* outConfig) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083{
4084 int count=0;
Adam Lesinskide898ff2014-01-29 18:20:45 -08004085 while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE
4086 && value->data != 0 && count < 20) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 if (outLastRef) *outLastRef = value->data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 uint32_t newFlags = 0;
Kenny Root55fc8502010-10-28 14:47:01 -07004089 const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags,
Dianne Hackborn0d221012009-07-29 15:41:19 -07004090 outConfig);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08004091 if (newIndex == BAD_INDEX) {
4092 return BAD_INDEX;
4093 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004094 if (kDebugTableTheme) {
4095 ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n",
4096 value->data, (int)newIndex, (int)value->dataType, value->data);
4097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex);
4099 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags;
4100 if (newIndex < 0) {
4101 // This can fail if the resource being referenced is a style...
4102 // in this case, just return the reference, and expect the
4103 // caller to deal with.
4104 return blockIndex;
4105 }
4106 blockIndex = newIndex;
4107 count++;
4108 }
4109 return blockIndex;
4110}
4111
4112const char16_t* ResTable::valueToString(
4113 const Res_value* value, size_t stringBlock,
Adam Lesinskiad2d07d2014-08-27 16:21:08 -07004114 char16_t /*tmpBuffer*/ [TMP_BUFFER_SIZE], size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115{
4116 if (!value) {
4117 return NULL;
4118 }
4119 if (value->dataType == value->TYPE_STRING) {
4120 return getTableStringBlock(stringBlock)->stringAt(value->data, outLen);
4121 }
4122 // XXX do int to string conversions.
4123 return NULL;
4124}
4125
4126ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const
4127{
4128 mLock.lock();
4129 ssize_t err = getBagLocked(resID, outBag);
4130 if (err < NO_ERROR) {
4131 //printf("*** get failed! unlocking\n");
4132 mLock.unlock();
4133 }
4134 return err;
4135}
4136
Mark Salyzyn00adb862014-03-19 11:00:06 -07004137void ResTable::unlockBag(const bag_entry* /*bag*/) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138{
4139 //printf("<<< unlockBag %p\n", this);
4140 mLock.unlock();
4141}
4142
4143void ResTable::lock() const
4144{
4145 mLock.lock();
4146}
4147
4148void ResTable::unlock() const
4149{
4150 mLock.unlock();
4151}
4152
4153ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag,
4154 uint32_t* outTypeSpecFlags) const
4155{
4156 if (mError != NO_ERROR) {
4157 return mError;
4158 }
4159
4160 const ssize_t p = getResourcePackageIndex(resID);
4161 const int t = Res_GETTYPE(resID);
4162 const int e = Res_GETENTRY(resID);
4163
4164 if (p < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004165 ALOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 return BAD_INDEX;
4167 }
4168 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004169 ALOGW("No type identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 return BAD_INDEX;
4171 }
4172
4173 //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t);
4174 PackageGroup* const grp = mPackageGroups[p];
4175 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00004176 ALOGW("Bad identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 return BAD_INDEX;
4178 }
4179
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004180 const TypeList& typeConfigs = grp->types[t];
4181 if (typeConfigs.isEmpty()) {
4182 ALOGW("Type identifier 0x%x does not exist.", t+1);
4183 return BAD_INDEX;
4184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004186 const size_t NENTRY = typeConfigs[0]->entryCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 if (e >= (int)NENTRY) {
Steve Block8564c8d2012-01-05 23:22:43 +00004188 ALOGW("Entry identifier 0x%x is larger than entry count 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004189 e, (int)typeConfigs[0]->entryCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 return BAD_INDEX;
4191 }
4192
4193 // First see if we've already computed this bag...
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004194 TypeCacheEntry& cacheEntry = grp->typeCacheEntries.editItemAt(t);
4195 bag_set** typeSet = cacheEntry.cachedBags;
4196 if (typeSet) {
4197 bag_set* set = typeSet[e];
4198 if (set) {
4199 if (set != (bag_set*)0xFFFFFFFF) {
4200 if (outTypeSpecFlags != NULL) {
4201 *outTypeSpecFlags = set->typeSpecFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004203 *outBag = (bag_entry*)(set+1);
4204 if (kDebugTableSuperNoisy) {
4205 ALOGI("Found existing bag for: 0x%x\n", resID);
4206 }
4207 return set->numAttrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004209 ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.",
4210 resID);
4211 return BAD_INDEX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 }
4213 }
4214
4215 // Bag not found, we need to compute it!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 if (!typeSet) {
Iliyan Malchev7e1d3952012-02-17 12:15:58 -08004217 typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 if (!typeSet) return NO_MEMORY;
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004219 cacheEntry.cachedBags = typeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 }
4221
4222 // Mark that we are currently working on this one.
4223 typeSet[e] = (bag_set*)0xFFFFFFFF;
4224
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004225 if (kDebugTableNoisy) {
4226 ALOGI("Building bag: %x\n", resID);
4227 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004228
4229 // Now collect all bag attributes
4230 Entry entry;
4231 status_t err = getEntry(grp, t, e, &mParams, &entry);
4232 if (err != NO_ERROR) {
4233 return err;
4234 }
4235
4236 const uint16_t entrySize = dtohs(entry.entry->size);
4237 const uint32_t parent = entrySize >= sizeof(ResTable_map_entry)
4238 ? dtohl(((const ResTable_map_entry*)entry.entry)->parent.ident) : 0;
4239 const uint32_t count = entrySize >= sizeof(ResTable_map_entry)
4240 ? dtohl(((const ResTable_map_entry*)entry.entry)->count) : 0;
4241
4242 size_t N = count;
4243
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004244 if (kDebugTableNoisy) {
4245 ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004246
4247 // If this map inherits from another, we need to start
4248 // with its parent's values. Otherwise start out empty.
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004249 ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent);
4250 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 // This is what we are building.
4253 bag_set* set = NULL;
4254
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004255 if (parent) {
4256 uint32_t resolvedParent = parent;
Mark Salyzyn00adb862014-03-19 11:00:06 -07004257
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004258 // Bags encode a parent reference without using the standard
4259 // Res_value structure. That means we must always try to
4260 // resolve a parent reference in case it is actually a
4261 // TYPE_DYNAMIC_REFERENCE.
4262 status_t err = grp->dynamicRefTable.lookupResourceId(&resolvedParent);
4263 if (err != NO_ERROR) {
4264 ALOGE("Failed resolving bag parent id 0x%08x", parent);
4265 return UNKNOWN_ERROR;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004268 const bag_entry* parentBag;
4269 uint32_t parentTypeSpecFlags = 0;
4270 const ssize_t NP = getBagLocked(resolvedParent, &parentBag, &parentTypeSpecFlags);
4271 const size_t NT = ((NP >= 0) ? NP : 0) + N;
4272 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT);
4273 if (set == NULL) {
4274 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004276 if (NP > 0) {
4277 memcpy(set+1, parentBag, NP*sizeof(bag_entry));
4278 set->numAttrs = NP;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004279 if (kDebugTableNoisy) {
4280 ALOGI("Initialized new bag with %zd inherited attributes.\n", NP);
4281 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004282 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004283 if (kDebugTableNoisy) {
4284 ALOGI("Initialized new bag with no inherited attributes.\n");
4285 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004286 set->numAttrs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004288 set->availAttrs = NT;
4289 set->typeSpecFlags = parentTypeSpecFlags;
4290 } else {
4291 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N);
4292 if (set == NULL) {
4293 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004295 set->numAttrs = 0;
4296 set->availAttrs = N;
4297 set->typeSpecFlags = 0;
4298 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004299
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004300 set->typeSpecFlags |= entry.specFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004302 // Now merge in the new attributes...
4303 size_t curOff = (reinterpret_cast<uintptr_t>(entry.entry) - reinterpret_cast<uintptr_t>(entry.type))
4304 + dtohs(entry.entry->size);
4305 const ResTable_map* map;
4306 bag_entry* entries = (bag_entry*)(set+1);
4307 size_t curEntry = 0;
4308 uint32_t pos = 0;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004309 if (kDebugTableNoisy) {
4310 ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs);
4311 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004312 while (pos < count) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004313 if (kDebugTableNoisy) {
4314 ALOGI("Now at %p\n", (void*)curOff);
4315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004317 if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) {
4318 ALOGW("ResTable_map at %d is beyond type chunk data %d",
4319 (int)curOff, dtohl(entry.type->header.size));
4320 return BAD_TYPE;
4321 }
4322 map = (const ResTable_map*)(((const uint8_t*)entry.type) + curOff);
4323 N++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -07004325 uint32_t newName = htodl(map->name.ident);
4326 if (!Res_INTERNALID(newName)) {
4327 // Attributes don't have a resource id as the name. They specify
4328 // other data, which would be wrong to change via a lookup.
4329 if (grp->dynamicRefTable.lookupResourceId(&newName) != NO_ERROR) {
4330 ALOGE("Failed resolving ResTable_map name at %d with ident 0x%08x",
4331 (int) curOff, (int) newName);
4332 return UNKNOWN_ERROR;
4333 }
4334 }
4335
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004336 bool isInside;
4337 uint32_t oldName = 0;
4338 while ((isInside=(curEntry < set->numAttrs))
4339 && (oldName=entries[curEntry].map.name.ident) < newName) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004340 if (kDebugTableNoisy) {
4341 ALOGI("#%zu: Keeping existing attribute: 0x%08x\n",
4342 curEntry, entries[curEntry].map.name.ident);
4343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 curEntry++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004346
4347 if ((!isInside) || oldName != newName) {
4348 // This is a new attribute... figure out what to do with it.
4349 if (set->numAttrs >= set->availAttrs) {
4350 // Need to alloc more memory...
4351 const size_t newAvail = set->availAttrs+N;
4352 set = (bag_set*)realloc(set,
4353 sizeof(bag_set)
4354 + sizeof(bag_entry)*newAvail);
4355 if (set == NULL) {
4356 return NO_MEMORY;
4357 }
4358 set->availAttrs = newAvail;
4359 entries = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004360 if (kDebugTableNoisy) {
4361 ALOGI("Reallocated set %p, entries=%p, avail=%zu\n",
4362 set, entries, set->availAttrs);
4363 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004364 }
4365 if (isInside) {
4366 // Going in the middle, need to make space.
4367 memmove(entries+curEntry+1, entries+curEntry,
4368 sizeof(bag_entry)*(set->numAttrs-curEntry));
4369 set->numAttrs++;
4370 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004371 if (kDebugTableNoisy) {
4372 ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName);
4373 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004374 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004375 if (kDebugTableNoisy) {
4376 ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName);
4377 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004378 }
4379
4380 bag_entry* cur = entries+curEntry;
4381
4382 cur->stringBlock = entry.package->header->index;
4383 cur->map.name.ident = newName;
4384 cur->map.value.copyFrom_dtoh(map->value);
4385 status_t err = grp->dynamicRefTable.lookupResourceValue(&cur->map.value);
4386 if (err != NO_ERROR) {
4387 ALOGE("Reference item(0x%08x) in bag could not be resolved.", cur->map.value.data);
4388 return UNKNOWN_ERROR;
4389 }
4390
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004391 if (kDebugTableNoisy) {
4392 ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n",
4393 curEntry, cur, cur->stringBlock, cur->map.name.ident,
4394 cur->map.value.dataType, cur->map.value.data);
4395 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004396
4397 // On to the next!
4398 curEntry++;
4399 pos++;
4400 const size_t size = dtohs(map->value.size);
4401 curOff += size + sizeof(*map)-sizeof(map->value);
4402 };
4403
4404 if (curEntry > set->numAttrs) {
4405 set->numAttrs = curEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 }
4407
4408 // And this is it...
4409 typeSet[e] = set;
4410 if (set) {
4411 if (outTypeSpecFlags != NULL) {
4412 *outTypeSpecFlags = set->typeSpecFlags;
4413 }
4414 *outBag = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004415 if (kDebugTableNoisy) {
4416 ALOGI("Returning %zu attrs\n", set->numAttrs);
4417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 return set->numAttrs;
4419 }
4420 return BAD_INDEX;
4421}
4422
4423void ResTable::setParameters(const ResTable_config* params)
4424{
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004425 AutoMutex _lock(mLock);
4426 AutoMutex _lock2(mFilteredConfigLock);
4427
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004428 if (kDebugTableGetEntry) {
4429 ALOGI("Setting parameters: %s\n", params->toString().string());
4430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 mParams = *params;
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004432 for (size_t p = 0; p < mPackageGroups.size(); p++) {
4433 PackageGroup* packageGroup = mPackageGroups.editItemAt(p);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004434 if (kDebugTableNoisy) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004435 ALOGI("CLEARING BAGS FOR GROUP %zu!", p);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004436 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004437 packageGroup->clearBagCache();
4438
4439 // Find which configurations match the set of parameters. This allows for a much
4440 // faster lookup in getEntry() if the set of values is narrowed down.
4441 for (size_t t = 0; t < packageGroup->types.size(); t++) {
4442 if (packageGroup->types[t].isEmpty()) {
4443 continue;
4444 }
4445
4446 TypeList& typeList = packageGroup->types.editItemAt(t);
4447
4448 // Retrieve the cache entry for this type.
4449 TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries.editItemAt(t);
4450
4451 for (size_t ts = 0; ts < typeList.size(); ts++) {
4452 Type* type = typeList.editItemAt(ts);
4453
4454 std::shared_ptr<Vector<const ResTable_type*>> newFilteredConfigs =
4455 std::make_shared<Vector<const ResTable_type*>>();
4456
4457 for (size_t ti = 0; ti < type->configs.size(); ti++) {
4458 ResTable_config config;
4459 config.copyFromDtoH(type->configs[ti]->config);
4460
4461 if (config.match(mParams)) {
4462 newFilteredConfigs->add(type->configs[ti]);
4463 }
4464 }
4465
4466 if (kDebugTableNoisy) {
4467 ALOGD("Updating pkg=%zu type=%zu with %zu filtered configs",
4468 p, t, newFilteredConfigs->size());
4469 }
4470
4471 cacheEntry.filteredConfigs.add(newFilteredConfigs);
4472 }
4473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475}
4476
4477void ResTable::getParameters(ResTable_config* params) const
4478{
4479 mLock.lock();
4480 *params = mParams;
4481 mLock.unlock();
4482}
4483
4484struct id_name_map {
4485 uint32_t id;
4486 size_t len;
4487 char16_t name[6];
4488};
4489
4490const static id_name_map ID_NAMES[] = {
4491 { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } },
4492 { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } },
4493 { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } },
4494 { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } },
4495 { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } },
4496 { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } },
4497 { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } },
4498 { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } },
4499 { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } },
4500 { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } },
4501};
4502
4503uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen,
4504 const char16_t* type, size_t typeLen,
4505 const char16_t* package,
4506 size_t packageLen,
4507 uint32_t* outTypeSpecFlags) const
4508{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004509 if (kDebugTableSuperNoisy) {
4510 printf("Identifier for name: error=%d\n", mError);
4511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512
4513 // Check for internal resource identifier as the very first thing, so
4514 // that we will always find them even when there are no resources.
4515 if (name[0] == '^') {
4516 const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0]));
4517 size_t len;
4518 for (int i=0; i<N; i++) {
4519 const id_name_map* m = ID_NAMES + i;
4520 len = m->len;
4521 if (len != nameLen) {
4522 continue;
4523 }
4524 for (size_t j=1; j<len; j++) {
4525 if (m->name[j] != name[j]) {
4526 goto nope;
4527 }
4528 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004529 if (outTypeSpecFlags) {
4530 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 return m->id;
4533nope:
4534 ;
4535 }
4536 if (nameLen > 7) {
4537 if (name[1] == 'i' && name[2] == 'n'
4538 && name[3] == 'd' && name[4] == 'e' && name[5] == 'x'
4539 && name[6] == '_') {
4540 int index = atoi(String8(name + 7, nameLen - 7).string());
4541 if (Res_CHECKID(index)) {
Steve Block8564c8d2012-01-05 23:22:43 +00004542 ALOGW("Array resource index: %d is too large.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 index);
4544 return 0;
4545 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004546 if (outTypeSpecFlags) {
4547 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 return Res_MAKEARRAY(index);
4550 }
4551 }
4552 return 0;
4553 }
4554
4555 if (mError != NO_ERROR) {
4556 return 0;
4557 }
4558
Dianne Hackborn426431a2011-06-09 11:29:08 -07004559 bool fakePublic = false;
4560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 // Figure out the package and type we are looking in...
4562
4563 const char16_t* packageEnd = NULL;
4564 const char16_t* typeEnd = NULL;
4565 const char16_t* const nameEnd = name+nameLen;
4566 const char16_t* p = name;
4567 while (p < nameEnd) {
4568 if (*p == ':') packageEnd = p;
4569 else if (*p == '/') typeEnd = p;
4570 p++;
4571 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004572 if (*name == '@') {
4573 name++;
4574 if (*name == '*') {
4575 fakePublic = true;
4576 name++;
4577 }
4578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 if (name >= nameEnd) {
4580 return 0;
4581 }
4582
4583 if (packageEnd) {
4584 package = name;
4585 packageLen = packageEnd-name;
4586 name = packageEnd+1;
4587 } else if (!package) {
4588 return 0;
4589 }
4590
4591 if (typeEnd) {
4592 type = name;
4593 typeLen = typeEnd-name;
4594 name = typeEnd+1;
4595 } else if (!type) {
4596 return 0;
4597 }
4598
4599 if (name >= nameEnd) {
4600 return 0;
4601 }
4602 nameLen = nameEnd-name;
4603
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004604 if (kDebugTableNoisy) {
4605 printf("Looking for identifier: type=%s, name=%s, package=%s\n",
4606 String8(type, typeLen).string(),
4607 String8(name, nameLen).string(),
4608 String8(package, packageLen).string());
4609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610
Adam Lesinski9b624c12014-11-19 17:49:26 -08004611 const String16 attr("attr");
4612 const String16 attrPrivate("^attr-private");
4613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 const size_t NG = mPackageGroups.size();
4615 for (size_t ig=0; ig<NG; ig++) {
4616 const PackageGroup* group = mPackageGroups[ig];
4617
4618 if (strzcmp16(package, packageLen,
4619 group->name.string(), group->name.size())) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004620 if (kDebugTableNoisy) {
4621 printf("Skipping package group: %s\n", String8(group->name).string());
4622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 continue;
4624 }
4625
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004626 const size_t packageCount = group->packages.size();
4627 for (size_t pi = 0; pi < packageCount; pi++) {
Adam Lesinski9b624c12014-11-19 17:49:26 -08004628 const char16_t* targetType = type;
4629 size_t targetTypeLen = typeLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630
Adam Lesinski9b624c12014-11-19 17:49:26 -08004631 do {
4632 ssize_t ti = group->packages[pi]->typeStrings.indexOfString(
4633 targetType, targetTypeLen);
4634 if (ti < 0) {
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004635 continue;
4636 }
4637
Adam Lesinski9b624c12014-11-19 17:49:26 -08004638 ti += group->packages[pi]->typeIdOffset;
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004639
Adam Lesinski9b624c12014-11-19 17:49:26 -08004640 const uint32_t identifier = findEntry(group, ti, name, nameLen,
4641 outTypeSpecFlags);
4642 if (identifier != 0) {
4643 if (fakePublic && outTypeSpecFlags) {
4644 *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004645 }
Adam Lesinski9b624c12014-11-19 17:49:26 -08004646 return identifier;
4647 }
4648 } while (strzcmp16(attr.string(), attr.size(), targetType, targetTypeLen) == 0
4649 && (targetType = attrPrivate.string())
4650 && (targetTypeLen = attrPrivate.size())
4651 );
4652 }
4653 break;
4654 }
4655 return 0;
4656}
4657
4658uint32_t ResTable::findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name,
4659 size_t nameLen, uint32_t* outTypeSpecFlags) const {
4660 const TypeList& typeList = group->types[typeIndex];
4661 const size_t typeCount = typeList.size();
4662 for (size_t i = 0; i < typeCount; i++) {
4663 const Type* t = typeList[i];
4664 const ssize_t ei = t->package->keyStrings.indexOfString(name, nameLen);
4665 if (ei < 0) {
4666 continue;
4667 }
4668
4669 const size_t configCount = t->configs.size();
4670 for (size_t j = 0; j < configCount; j++) {
4671 const TypeVariant tv(t->configs[j]);
4672 for (TypeVariant::iterator iter = tv.beginEntries();
4673 iter != tv.endEntries();
4674 iter++) {
4675 const ResTable_entry* entry = *iter;
4676 if (entry == NULL) {
4677 continue;
4678 }
4679
4680 if (dtohl(entry->key.index) == (size_t) ei) {
4681 uint32_t resId = Res_MAKEID(group->id - 1, typeIndex, iter.index());
4682 if (outTypeSpecFlags) {
4683 Entry result;
4684 if (getEntry(group, typeIndex, iter.index(), NULL, &result) != NO_ERROR) {
4685 ALOGW("Failed to find spec flags for 0x%08x", resId);
4686 return 0;
4687 }
4688 *outTypeSpecFlags = result.specFlags;
4689 }
4690 return resId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 }
4692 }
4693 }
4694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 return 0;
4696}
4697
Dan Albertf348c152014-09-08 18:28:00 -07004698bool ResTable::expandResourceRef(const char16_t* refStr, size_t refLen,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 String16* outPackage,
4700 String16* outType,
4701 String16* outName,
4702 const String16* defType,
4703 const String16* defPackage,
Dianne Hackborn426431a2011-06-09 11:29:08 -07004704 const char** outErrorMsg,
4705 bool* outPublicOnly)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706{
4707 const char16_t* packageEnd = NULL;
4708 const char16_t* typeEnd = NULL;
4709 const char16_t* p = refStr;
4710 const char16_t* const end = p + refLen;
4711 while (p < end) {
4712 if (*p == ':') packageEnd = p;
4713 else if (*p == '/') {
4714 typeEnd = p;
4715 break;
4716 }
4717 p++;
4718 }
4719 p = refStr;
4720 if (*p == '@') p++;
4721
Dianne Hackborn426431a2011-06-09 11:29:08 -07004722 if (outPublicOnly != NULL) {
4723 *outPublicOnly = true;
4724 }
4725 if (*p == '*') {
4726 p++;
4727 if (outPublicOnly != NULL) {
4728 *outPublicOnly = false;
4729 }
4730 }
4731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 if (packageEnd) {
4733 *outPackage = String16(p, packageEnd-p);
4734 p = packageEnd+1;
4735 } else {
4736 if (!defPackage) {
4737 if (outErrorMsg) {
4738 *outErrorMsg = "No resource package specified";
4739 }
4740 return false;
4741 }
4742 *outPackage = *defPackage;
4743 }
4744 if (typeEnd) {
4745 *outType = String16(p, typeEnd-p);
4746 p = typeEnd+1;
4747 } else {
4748 if (!defType) {
4749 if (outErrorMsg) {
4750 *outErrorMsg = "No resource type specified";
4751 }
4752 return false;
4753 }
4754 *outType = *defType;
4755 }
4756 *outName = String16(p, end-p);
Konstantin Lopyrevddcafcb2010-06-04 14:36:49 -07004757 if(**outPackage == 0) {
4758 if(outErrorMsg) {
4759 *outErrorMsg = "Resource package cannot be an empty string";
4760 }
4761 return false;
4762 }
4763 if(**outType == 0) {
4764 if(outErrorMsg) {
4765 *outErrorMsg = "Resource type cannot be an empty string";
4766 }
4767 return false;
4768 }
4769 if(**outName == 0) {
4770 if(outErrorMsg) {
4771 *outErrorMsg = "Resource id cannot be an empty string";
4772 }
4773 return false;
4774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 return true;
4776}
4777
4778static uint32_t get_hex(char c, bool* outError)
4779{
4780 if (c >= '0' && c <= '9') {
4781 return c - '0';
4782 } else if (c >= 'a' && c <= 'f') {
4783 return c - 'a' + 0xa;
4784 } else if (c >= 'A' && c <= 'F') {
4785 return c - 'A' + 0xa;
4786 }
4787 *outError = true;
4788 return 0;
4789}
4790
4791struct unit_entry
4792{
4793 const char* name;
4794 size_t len;
4795 uint8_t type;
4796 uint32_t unit;
4797 float scale;
4798};
4799
4800static const unit_entry unitNames[] = {
4801 { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f },
4802 { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4803 { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4804 { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f },
4805 { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f },
4806 { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f },
4807 { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f },
4808 { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 },
4809 { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 },
4810 { NULL, 0, 0, 0, 0 }
4811};
4812
4813static bool parse_unit(const char* str, Res_value* outValue,
4814 float* outScale, const char** outEnd)
4815{
4816 const char* end = str;
4817 while (*end != 0 && !isspace((unsigned char)*end)) {
4818 end++;
4819 }
4820 const size_t len = end-str;
4821
4822 const char* realEnd = end;
4823 while (*realEnd != 0 && isspace((unsigned char)*realEnd)) {
4824 realEnd++;
4825 }
4826 if (*realEnd != 0) {
4827 return false;
4828 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 const unit_entry* cur = unitNames;
4831 while (cur->name) {
4832 if (len == cur->len && strncmp(cur->name, str, len) == 0) {
4833 outValue->dataType = cur->type;
4834 outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT;
4835 *outScale = cur->scale;
4836 *outEnd = end;
4837 //printf("Found unit %s for %s\n", cur->name, str);
4838 return true;
4839 }
4840 cur++;
4841 }
4842
4843 return false;
4844}
4845
Dan Albert1b4f3162015-04-07 18:43:15 -07004846bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847{
4848 while (len > 0 && isspace16(*s)) {
4849 s++;
4850 len--;
4851 }
4852
4853 if (len <= 0) {
4854 return false;
4855 }
4856
4857 size_t i = 0;
Dan Albert1b4f3162015-04-07 18:43:15 -07004858 int64_t val = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 bool neg = false;
4860
4861 if (*s == '-') {
4862 neg = true;
4863 i++;
4864 }
4865
4866 if (s[i] < '0' || s[i] > '9') {
4867 return false;
4868 }
4869
Dan Albert1b4f3162015-04-07 18:43:15 -07004870 static_assert(std::is_same<uint32_t, Res_value::data_type>::value,
4871 "Res_value::data_type has changed. The range checks in this "
4872 "function are no longer correct.");
4873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 // Decimal or hex?
Dan Albert1b4f3162015-04-07 18:43:15 -07004875 bool isHex;
4876 if (len > 1 && s[i] == '0' && s[i+1] == 'x') {
4877 isHex = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 i += 2;
Dan Albert1b4f3162015-04-07 18:43:15 -07004879
4880 if (neg) {
4881 return false;
4882 }
4883
4884 if (i == len) {
4885 // Just u"0x"
4886 return false;
4887 }
4888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 bool error = false;
4890 while (i < len && !error) {
4891 val = (val*16) + get_hex(s[i], &error);
4892 i++;
Dan Albert1b4f3162015-04-07 18:43:15 -07004893
4894 if (val > std::numeric_limits<uint32_t>::max()) {
4895 return false;
4896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 }
4898 if (error) {
4899 return false;
4900 }
4901 } else {
Dan Albert1b4f3162015-04-07 18:43:15 -07004902 isHex = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 while (i < len) {
4904 if (s[i] < '0' || s[i] > '9') {
4905 return false;
4906 }
4907 val = (val*10) + s[i]-'0';
4908 i++;
Dan Albert1b4f3162015-04-07 18:43:15 -07004909
4910 if ((neg && -val < std::numeric_limits<int32_t>::min()) ||
4911 (!neg && val > std::numeric_limits<int32_t>::max())) {
4912 return false;
4913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
4915 }
4916
4917 if (neg) val = -val;
4918
4919 while (i < len && isspace16(s[i])) {
4920 i++;
4921 }
4922
Dan Albert1b4f3162015-04-07 18:43:15 -07004923 if (i != len) {
4924 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 }
4926
Dan Albert1b4f3162015-04-07 18:43:15 -07004927 if (outValue) {
4928 outValue->dataType =
4929 isHex ? outValue->TYPE_INT_HEX : outValue->TYPE_INT_DEC;
4930 outValue->data = static_cast<Res_value::data_type>(val);
4931 }
4932 return true;
4933}
4934
4935bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue)
4936{
4937 return U16StringToInt(s, len, outValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938}
4939
4940bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue)
4941{
4942 while (len > 0 && isspace16(*s)) {
4943 s++;
4944 len--;
4945 }
4946
4947 if (len <= 0) {
4948 return false;
4949 }
4950
4951 char buf[128];
4952 int i=0;
4953 while (len > 0 && *s != 0 && i < 126) {
4954 if (*s > 255) {
4955 return false;
4956 }
4957 buf[i++] = *s++;
4958 len--;
4959 }
4960
4961 if (len > 0) {
4962 return false;
4963 }
Torne (Richard Coles)46a807f2014-08-27 12:36:44 +01004964 if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.' && buf[0] != '-' && buf[0] != '+') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 return false;
4966 }
4967
4968 buf[i] = 0;
4969 const char* end;
4970 float f = strtof(buf, (char**)&end);
4971
4972 if (*end != 0 && !isspace((unsigned char)*end)) {
4973 // Might be a unit...
4974 float scale;
4975 if (parse_unit(end, outValue, &scale, &end)) {
4976 f *= scale;
4977 const bool neg = f < 0;
4978 if (neg) f = -f;
4979 uint64_t bits = (uint64_t)(f*(1<<23)+.5f);
4980 uint32_t radix;
4981 uint32_t shift;
4982 if ((bits&0x7fffff) == 0) {
4983 // Always use 23p0 if there is no fraction, just to make
4984 // things easier to read.
4985 radix = Res_value::COMPLEX_RADIX_23p0;
4986 shift = 23;
4987 } else if ((bits&0xffffffffff800000LL) == 0) {
4988 // Magnitude is zero -- can fit in 0 bits of precision.
4989 radix = Res_value::COMPLEX_RADIX_0p23;
4990 shift = 0;
4991 } else if ((bits&0xffffffff80000000LL) == 0) {
4992 // Magnitude can fit in 8 bits of precision.
4993 radix = Res_value::COMPLEX_RADIX_8p15;
4994 shift = 8;
4995 } else if ((bits&0xffffff8000000000LL) == 0) {
4996 // Magnitude can fit in 16 bits of precision.
4997 radix = Res_value::COMPLEX_RADIX_16p7;
4998 shift = 16;
4999 } else {
5000 // Magnitude needs entire range, so no fractional part.
5001 radix = Res_value::COMPLEX_RADIX_23p0;
5002 shift = 23;
5003 }
5004 int32_t mantissa = (int32_t)(
5005 (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK);
5006 if (neg) {
5007 mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK;
5008 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005009 outValue->data |=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 (radix<<Res_value::COMPLEX_RADIX_SHIFT)
5011 | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT);
5012 //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n",
5013 // f * (neg ? -1 : 1), bits, f*(1<<23),
5014 // radix, shift, outValue->data);
5015 return true;
5016 }
5017 return false;
5018 }
5019
5020 while (*end != 0 && isspace((unsigned char)*end)) {
5021 end++;
5022 }
5023
5024 if (*end == 0) {
5025 if (outValue) {
5026 outValue->dataType = outValue->TYPE_FLOAT;
5027 *(float*)(&outValue->data) = f;
5028 return true;
5029 }
5030 }
5031
5032 return false;
5033}
5034
5035bool ResTable::stringToValue(Res_value* outValue, String16* outString,
5036 const char16_t* s, size_t len,
5037 bool preserveSpaces, bool coerceType,
5038 uint32_t attrID,
5039 const String16* defType,
5040 const String16* defPackage,
5041 Accessor* accessor,
5042 void* accessorCookie,
5043 uint32_t attrType,
5044 bool enforcePrivate) const
5045{
5046 bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting();
5047 const char* errorMsg = NULL;
5048
5049 outValue->size = sizeof(Res_value);
5050 outValue->res0 = 0;
5051
5052 // First strip leading/trailing whitespace. Do this before handling
5053 // escapes, so they can be used to force whitespace into the string.
5054 if (!preserveSpaces) {
5055 while (len > 0 && isspace16(*s)) {
5056 s++;
5057 len--;
5058 }
5059 while (len > 0 && isspace16(s[len-1])) {
5060 len--;
5061 }
5062 // If the string ends with '\', then we keep the space after it.
5063 if (len > 0 && s[len-1] == '\\' && s[len] != 0) {
5064 len++;
5065 }
5066 }
5067
5068 //printf("Value for: %s\n", String8(s, len).string());
5069
5070 uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED;
5071 uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff;
5072 bool fromAccessor = false;
5073 if (attrID != 0 && !Res_INTERNALID(attrID)) {
5074 const ssize_t p = getResourcePackageIndex(attrID);
5075 const bag_entry* bag;
5076 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5077 //printf("For attr 0x%08x got bag of %d\n", attrID, cnt);
5078 if (cnt >= 0) {
5079 while (cnt > 0) {
5080 //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data);
5081 switch (bag->map.name.ident) {
5082 case ResTable_map::ATTR_TYPE:
5083 attrType = bag->map.value.data;
5084 break;
5085 case ResTable_map::ATTR_MIN:
5086 attrMin = bag->map.value.data;
5087 break;
5088 case ResTable_map::ATTR_MAX:
5089 attrMax = bag->map.value.data;
5090 break;
5091 case ResTable_map::ATTR_L10N:
5092 l10nReq = bag->map.value.data;
5093 break;
5094 }
5095 bag++;
5096 cnt--;
5097 }
5098 unlockBag(bag);
5099 } else if (accessor && accessor->getAttributeType(attrID, &attrType)) {
5100 fromAccessor = true;
5101 if (attrType == ResTable_map::TYPE_ENUM
5102 || attrType == ResTable_map::TYPE_FLAGS
5103 || attrType == ResTable_map::TYPE_INTEGER) {
5104 accessor->getAttributeMin(attrID, &attrMin);
5105 accessor->getAttributeMax(attrID, &attrMax);
5106 }
5107 if (localizationSetting) {
5108 l10nReq = accessor->getAttributeL10N(attrID);
5109 }
5110 }
5111 }
5112
5113 const bool canStringCoerce =
5114 coerceType && (attrType&ResTable_map::TYPE_STRING) != 0;
5115
5116 if (*s == '@') {
5117 outValue->dataType = outValue->TYPE_REFERENCE;
5118
5119 // Note: we don't check attrType here because the reference can
5120 // be to any other type; we just need to count on the client making
5121 // sure the referenced type is correct.
Mark Salyzyn00adb862014-03-19 11:00:06 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 //printf("Looking up ref: %s\n", String8(s, len).string());
5124
5125 // It's a reference!
5126 if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') {
Alan Viverettef2969402014-10-29 17:09:36 -07005127 // Special case @null as undefined. This will be converted by
5128 // AssetManager to TYPE_NULL with data DATA_NULL_UNDEFINED.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 outValue->data = 0;
5130 return true;
Alan Viverettef2969402014-10-29 17:09:36 -07005131 } else if (len == 6 && s[1]=='e' && s[2]=='m' && s[3]=='p' && s[4]=='t' && s[5]=='y') {
5132 // Special case @empty as explicitly defined empty value.
5133 outValue->dataType = Res_value::TYPE_NULL;
5134 outValue->data = Res_value::DATA_NULL_EMPTY;
5135 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 } else {
5137 bool createIfNotFound = false;
5138 const char16_t* resourceRefName;
5139 int resourceNameLen;
5140 if (len > 2 && s[1] == '+') {
5141 createIfNotFound = true;
5142 resourceRefName = s + 2;
5143 resourceNameLen = len - 2;
5144 } else if (len > 2 && s[1] == '*') {
5145 enforcePrivate = false;
5146 resourceRefName = s + 2;
5147 resourceNameLen = len - 2;
5148 } else {
5149 createIfNotFound = false;
5150 resourceRefName = s + 1;
5151 resourceNameLen = len - 1;
5152 }
5153 String16 package, type, name;
5154 if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name,
5155 defType, defPackage, &errorMsg)) {
5156 if (accessor != NULL) {
5157 accessor->reportError(accessorCookie, errorMsg);
5158 }
5159 return false;
5160 }
5161
5162 uint32_t specFlags = 0;
5163 uint32_t rid = identifierForName(name.string(), name.size(), type.string(),
5164 type.size(), package.string(), package.size(), &specFlags);
5165 if (rid != 0) {
5166 if (enforcePrivate) {
Adam Lesinski833f3cc2014-06-18 15:06:01 -07005167 if (accessor == NULL || accessor->getAssetsPackage() != package) {
5168 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5169 if (accessor != NULL) {
5170 accessor->reportError(accessorCookie, "Resource is not public.");
5171 }
5172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 }
5175 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005176
5177 if (accessor) {
5178 rid = Res_MAKEID(
5179 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5180 Res_GETTYPE(rid), Res_GETENTRY(rid));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005181 if (kDebugTableNoisy) {
5182 ALOGI("Incl %s:%s/%s: 0x%08x\n",
5183 String8(package).string(), String8(type).string(),
5184 String8(name).string(), rid);
5185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005187
5188 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5189 if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) {
5190 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 outValue->data = rid;
5193 return true;
5194 }
5195
5196 if (accessor) {
5197 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name,
5198 createIfNotFound);
5199 if (rid != 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005200 if (kDebugTableNoisy) {
5201 ALOGI("Pckg %s:%s/%s: 0x%08x\n",
5202 String8(package).string(), String8(type).string(),
5203 String8(name).string(), rid);
5204 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005205 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5206 if (packageId == 0x00) {
5207 outValue->data = rid;
5208 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5209 return true;
5210 } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) {
5211 // We accept packageId's generated as 0x01 in order to support
5212 // building the android system resources
5213 outValue->data = rid;
5214 return true;
5215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 }
5217 }
5218 }
5219
5220 if (accessor != NULL) {
5221 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5222 }
5223 return false;
5224 }
5225
5226 // if we got to here, and localization is required and it's not a reference,
5227 // complain and bail.
5228 if (l10nReq == ResTable_map::L10N_SUGGESTED) {
5229 if (localizationSetting) {
5230 if (accessor != NULL) {
5231 accessor->reportError(accessorCookie, "This attribute must be localized.");
5232 }
5233 }
5234 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 if (*s == '#') {
5237 // It's a color! Convert to an integer of the form 0xaarrggbb.
5238 uint32_t color = 0;
5239 bool error = false;
5240 if (len == 4) {
5241 outValue->dataType = outValue->TYPE_INT_COLOR_RGB4;
5242 color |= 0xFF000000;
5243 color |= get_hex(s[1], &error) << 20;
5244 color |= get_hex(s[1], &error) << 16;
5245 color |= get_hex(s[2], &error) << 12;
5246 color |= get_hex(s[2], &error) << 8;
5247 color |= get_hex(s[3], &error) << 4;
5248 color |= get_hex(s[3], &error);
5249 } else if (len == 5) {
5250 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4;
5251 color |= get_hex(s[1], &error) << 28;
5252 color |= get_hex(s[1], &error) << 24;
5253 color |= get_hex(s[2], &error) << 20;
5254 color |= get_hex(s[2], &error) << 16;
5255 color |= get_hex(s[3], &error) << 12;
5256 color |= get_hex(s[3], &error) << 8;
5257 color |= get_hex(s[4], &error) << 4;
5258 color |= get_hex(s[4], &error);
5259 } else if (len == 7) {
5260 outValue->dataType = outValue->TYPE_INT_COLOR_RGB8;
5261 color |= 0xFF000000;
5262 color |= get_hex(s[1], &error) << 20;
5263 color |= get_hex(s[2], &error) << 16;
5264 color |= get_hex(s[3], &error) << 12;
5265 color |= get_hex(s[4], &error) << 8;
5266 color |= get_hex(s[5], &error) << 4;
5267 color |= get_hex(s[6], &error);
5268 } else if (len == 9) {
5269 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8;
5270 color |= get_hex(s[1], &error) << 28;
5271 color |= get_hex(s[2], &error) << 24;
5272 color |= get_hex(s[3], &error) << 20;
5273 color |= get_hex(s[4], &error) << 16;
5274 color |= get_hex(s[5], &error) << 12;
5275 color |= get_hex(s[6], &error) << 8;
5276 color |= get_hex(s[7], &error) << 4;
5277 color |= get_hex(s[8], &error);
5278 } else {
5279 error = true;
5280 }
5281 if (!error) {
5282 if ((attrType&ResTable_map::TYPE_COLOR) == 0) {
5283 if (!canStringCoerce) {
5284 if (accessor != NULL) {
5285 accessor->reportError(accessorCookie,
5286 "Color types not allowed");
5287 }
5288 return false;
5289 }
5290 } else {
5291 outValue->data = color;
5292 //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color);
5293 return true;
5294 }
5295 } else {
5296 if ((attrType&ResTable_map::TYPE_COLOR) != 0) {
5297 if (accessor != NULL) {
5298 accessor->reportError(accessorCookie, "Color value not valid --"
5299 " must be #rgb, #argb, #rrggbb, or #aarrggbb");
5300 }
5301 #if 0
5302 fprintf(stderr, "%s: Color ID %s value %s is not valid\n",
5303 "Resource File", //(const char*)in->getPrintableSource(),
5304 String8(*curTag).string(),
5305 String8(s, len).string());
5306 #endif
5307 return false;
5308 }
5309 }
5310 }
5311
5312 if (*s == '?') {
5313 outValue->dataType = outValue->TYPE_ATTRIBUTE;
5314
5315 // Note: we don't check attrType here because the reference can
5316 // be to any other type; we just need to count on the client making
5317 // sure the referenced type is correct.
5318
5319 //printf("Looking up attr: %s\n", String8(s, len).string());
5320
5321 static const String16 attr16("attr");
5322 String16 package, type, name;
5323 if (!expandResourceRef(s+1, len-1, &package, &type, &name,
5324 &attr16, defPackage, &errorMsg)) {
5325 if (accessor != NULL) {
5326 accessor->reportError(accessorCookie, errorMsg);
5327 }
5328 return false;
5329 }
5330
5331 //printf("Pkg: %s, Type: %s, Name: %s\n",
5332 // String8(package).string(), String8(type).string(),
5333 // String8(name).string());
5334 uint32_t specFlags = 0;
Mark Salyzyn00adb862014-03-19 11:00:06 -07005335 uint32_t rid =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 identifierForName(name.string(), name.size(),
5337 type.string(), type.size(),
5338 package.string(), package.size(), &specFlags);
5339 if (rid != 0) {
5340 if (enforcePrivate) {
5341 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5342 if (accessor != NULL) {
5343 accessor->reportError(accessorCookie, "Attribute is not public.");
5344 }
5345 return false;
5346 }
5347 }
5348 if (!accessor) {
5349 outValue->data = rid;
5350 return true;
5351 }
5352 rid = Res_MAKEID(
5353 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5354 Res_GETTYPE(rid), Res_GETENTRY(rid));
5355 //printf("Incl %s:%s/%s: 0x%08x\n",
5356 // String8(package).string(), String8(type).string(),
5357 // String8(name).string(), rid);
5358 outValue->data = rid;
5359 return true;
5360 }
5361
5362 if (accessor) {
5363 uint32_t rid = accessor->getCustomResource(package, type, name);
5364 if (rid != 0) {
5365 //printf("Mine %s:%s/%s: 0x%08x\n",
5366 // String8(package).string(), String8(type).string(),
5367 // String8(name).string(), rid);
5368 outValue->data = rid;
5369 return true;
5370 }
5371 }
5372
5373 if (accessor != NULL) {
5374 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5375 }
5376 return false;
5377 }
5378
5379 if (stringToInt(s, len, outValue)) {
5380 if ((attrType&ResTable_map::TYPE_INTEGER) == 0) {
5381 // If this type does not allow integers, but does allow floats,
5382 // fall through on this error case because the float type should
5383 // be able to accept any integer value.
5384 if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) {
5385 if (accessor != NULL) {
5386 accessor->reportError(accessorCookie, "Integer types not allowed");
5387 }
5388 return false;
5389 }
5390 } else {
5391 if (((int32_t)outValue->data) < ((int32_t)attrMin)
5392 || ((int32_t)outValue->data) > ((int32_t)attrMax)) {
5393 if (accessor != NULL) {
5394 accessor->reportError(accessorCookie, "Integer value out of range");
5395 }
5396 return false;
5397 }
5398 return true;
5399 }
5400 }
5401
5402 if (stringToFloat(s, len, outValue)) {
5403 if (outValue->dataType == Res_value::TYPE_DIMENSION) {
5404 if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) {
5405 return true;
5406 }
5407 if (!canStringCoerce) {
5408 if (accessor != NULL) {
5409 accessor->reportError(accessorCookie, "Dimension types not allowed");
5410 }
5411 return false;
5412 }
5413 } else if (outValue->dataType == Res_value::TYPE_FRACTION) {
5414 if ((attrType&ResTable_map::TYPE_FRACTION) != 0) {
5415 return true;
5416 }
5417 if (!canStringCoerce) {
5418 if (accessor != NULL) {
5419 accessor->reportError(accessorCookie, "Fraction types not allowed");
5420 }
5421 return false;
5422 }
5423 } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) {
5424 if (!canStringCoerce) {
5425 if (accessor != NULL) {
5426 accessor->reportError(accessorCookie, "Float types not allowed");
5427 }
5428 return false;
5429 }
5430 } else {
5431 return true;
5432 }
5433 }
5434
5435 if (len == 4) {
5436 if ((s[0] == 't' || s[0] == 'T') &&
5437 (s[1] == 'r' || s[1] == 'R') &&
5438 (s[2] == 'u' || s[2] == 'U') &&
5439 (s[3] == 'e' || s[3] == 'E')) {
5440 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5441 if (!canStringCoerce) {
5442 if (accessor != NULL) {
5443 accessor->reportError(accessorCookie, "Boolean types not allowed");
5444 }
5445 return false;
5446 }
5447 } else {
5448 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5449 outValue->data = (uint32_t)-1;
5450 return true;
5451 }
5452 }
5453 }
5454
5455 if (len == 5) {
5456 if ((s[0] == 'f' || s[0] == 'F') &&
5457 (s[1] == 'a' || s[1] == 'A') &&
5458 (s[2] == 'l' || s[2] == 'L') &&
5459 (s[3] == 's' || s[3] == 'S') &&
5460 (s[4] == 'e' || s[4] == 'E')) {
5461 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5462 if (!canStringCoerce) {
5463 if (accessor != NULL) {
5464 accessor->reportError(accessorCookie, "Boolean types not allowed");
5465 }
5466 return false;
5467 }
5468 } else {
5469 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5470 outValue->data = 0;
5471 return true;
5472 }
5473 }
5474 }
5475
5476 if ((attrType&ResTable_map::TYPE_ENUM) != 0) {
5477 const ssize_t p = getResourcePackageIndex(attrID);
5478 const bag_entry* bag;
5479 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5480 //printf("Got %d for enum\n", cnt);
5481 if (cnt >= 0) {
5482 resource_name rname;
5483 while (cnt > 0) {
5484 if (!Res_INTERNALID(bag->map.name.ident)) {
5485 //printf("Trying attr #%08x\n", bag->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005486 if (getResourceName(bag->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 #if 0
5488 printf("Matching %s against %s (0x%08x)\n",
5489 String8(s, len).string(),
5490 String8(rname.name, rname.nameLen).string(),
5491 bag->map.name.ident);
5492 #endif
5493 if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) {
5494 outValue->dataType = bag->map.value.dataType;
5495 outValue->data = bag->map.value.data;
5496 unlockBag(bag);
5497 return true;
5498 }
5499 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 }
5502 bag++;
5503 cnt--;
5504 }
5505 unlockBag(bag);
5506 }
5507
5508 if (fromAccessor) {
5509 if (accessor->getAttributeEnum(attrID, s, len, outValue)) {
5510 return true;
5511 }
5512 }
5513 }
5514
5515 if ((attrType&ResTable_map::TYPE_FLAGS) != 0) {
5516 const ssize_t p = getResourcePackageIndex(attrID);
5517 const bag_entry* bag;
5518 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5519 //printf("Got %d for flags\n", cnt);
5520 if (cnt >= 0) {
5521 bool failed = false;
5522 resource_name rname;
5523 outValue->dataType = Res_value::TYPE_INT_HEX;
5524 outValue->data = 0;
5525 const char16_t* end = s + len;
5526 const char16_t* pos = s;
5527 while (pos < end && !failed) {
5528 const char16_t* start = pos;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005529 pos++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 while (pos < end && *pos != '|') {
5531 pos++;
5532 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08005533 //printf("Looking for: %s\n", String8(start, pos-start).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 const bag_entry* bagi = bag;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005535 ssize_t i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 for (i=0; i<cnt; i++, bagi++) {
5537 if (!Res_INTERNALID(bagi->map.name.ident)) {
5538 //printf("Trying attr #%08x\n", bagi->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005539 if (getResourceName(bagi->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 #if 0
5541 printf("Matching %s against %s (0x%08x)\n",
5542 String8(start,pos-start).string(),
5543 String8(rname.name, rname.nameLen).string(),
5544 bagi->map.name.ident);
5545 #endif
5546 if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) {
5547 outValue->data |= bagi->map.value.data;
5548 break;
5549 }
5550 }
5551 }
5552 }
5553 if (i >= cnt) {
5554 // Didn't find this flag identifier.
5555 failed = true;
5556 }
5557 if (pos < end) {
5558 pos++;
5559 }
5560 }
5561 unlockBag(bag);
5562 if (!failed) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005563 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 return true;
5565 }
5566 }
5567
5568
5569 if (fromAccessor) {
5570 if (accessor->getAttributeFlags(attrID, s, len, outValue)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005571 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 return true;
5573 }
5574 }
5575 }
5576
5577 if ((attrType&ResTable_map::TYPE_STRING) == 0) {
5578 if (accessor != NULL) {
5579 accessor->reportError(accessorCookie, "String types not allowed");
5580 }
5581 return false;
5582 }
5583
5584 // Generic string handling...
5585 outValue->dataType = outValue->TYPE_STRING;
5586 if (outString) {
5587 bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg);
5588 if (accessor != NULL) {
5589 accessor->reportError(accessorCookie, errorMsg);
5590 }
5591 return failed;
5592 }
5593
5594 return true;
5595}
5596
5597bool ResTable::collectString(String16* outString,
5598 const char16_t* s, size_t len,
5599 bool preserveSpaces,
5600 const char** outErrorMsg,
5601 bool append)
5602{
5603 String16 tmp;
5604
5605 char quoted = 0;
5606 const char16_t* p = s;
5607 while (p < (s+len)) {
5608 while (p < (s+len)) {
5609 const char16_t c = *p;
5610 if (c == '\\') {
5611 break;
5612 }
5613 if (!preserveSpaces) {
5614 if (quoted == 0 && isspace16(c)
5615 && (c != ' ' || isspace16(*(p+1)))) {
5616 break;
5617 }
5618 if (c == '"' && (quoted == 0 || quoted == '"')) {
5619 break;
5620 }
5621 if (c == '\'' && (quoted == 0 || quoted == '\'')) {
Eric Fischerc87d2522009-09-01 15:20:30 -07005622 /*
5623 * In practice, when people write ' instead of \'
5624 * in a string, they are doing it by accident
5625 * instead of really meaning to use ' as a quoting
5626 * character. Warn them so they don't lose it.
5627 */
5628 if (outErrorMsg) {
5629 *outErrorMsg = "Apostrophe not preceded by \\";
5630 }
5631 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633 }
5634 p++;
5635 }
5636 if (p < (s+len)) {
5637 if (p > s) {
5638 tmp.append(String16(s, p-s));
5639 }
5640 if (!preserveSpaces && (*p == '"' || *p == '\'')) {
5641 if (quoted == 0) {
5642 quoted = *p;
5643 } else {
5644 quoted = 0;
5645 }
5646 p++;
5647 } else if (!preserveSpaces && isspace16(*p)) {
5648 // Space outside of a quote -- consume all spaces and
5649 // leave a single plain space char.
5650 tmp.append(String16(" "));
5651 p++;
5652 while (p < (s+len) && isspace16(*p)) {
5653 p++;
5654 }
5655 } else if (*p == '\\') {
5656 p++;
5657 if (p < (s+len)) {
5658 switch (*p) {
5659 case 't':
5660 tmp.append(String16("\t"));
5661 break;
5662 case 'n':
5663 tmp.append(String16("\n"));
5664 break;
5665 case '#':
5666 tmp.append(String16("#"));
5667 break;
5668 case '@':
5669 tmp.append(String16("@"));
5670 break;
5671 case '?':
5672 tmp.append(String16("?"));
5673 break;
5674 case '"':
5675 tmp.append(String16("\""));
5676 break;
5677 case '\'':
5678 tmp.append(String16("'"));
5679 break;
5680 case '\\':
5681 tmp.append(String16("\\"));
5682 break;
5683 case 'u':
5684 {
5685 char16_t chr = 0;
5686 int i = 0;
5687 while (i < 4 && p[1] != 0) {
5688 p++;
5689 i++;
5690 int c;
5691 if (*p >= '0' && *p <= '9') {
5692 c = *p - '0';
5693 } else if (*p >= 'a' && *p <= 'f') {
5694 c = *p - 'a' + 10;
5695 } else if (*p >= 'A' && *p <= 'F') {
5696 c = *p - 'A' + 10;
5697 } else {
5698 if (outErrorMsg) {
5699 *outErrorMsg = "Bad character in \\u unicode escape sequence";
5700 }
5701 return false;
5702 }
5703 chr = (chr<<4) | c;
5704 }
5705 tmp.append(String16(&chr, 1));
5706 } break;
5707 default:
5708 // ignore unknown escape chars.
5709 break;
5710 }
5711 p++;
5712 }
5713 }
5714 len -= (p-s);
5715 s = p;
5716 }
5717 }
5718
5719 if (tmp.size() != 0) {
5720 if (len > 0) {
5721 tmp.append(String16(s, len));
5722 }
5723 if (append) {
5724 outString->append(tmp);
5725 } else {
5726 outString->setTo(tmp);
5727 }
5728 } else {
5729 if (append) {
5730 outString->append(String16(s, len));
5731 } else {
5732 outString->setTo(s, len);
5733 }
5734 }
5735
5736 return true;
5737}
5738
5739size_t ResTable::getBasePackageCount() const
5740{
5741 if (mError != NO_ERROR) {
5742 return 0;
5743 }
5744 return mPackageGroups.size();
5745}
5746
Adam Lesinskide898ff2014-01-29 18:20:45 -08005747const String16 ResTable::getBasePackageName(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748{
5749 if (mError != NO_ERROR) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08005750 return String16();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 }
5752 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5753 "Requested package index %d past package count %d",
5754 (int)idx, (int)mPackageGroups.size());
Adam Lesinskide898ff2014-01-29 18:20:45 -08005755 return mPackageGroups[idx]->name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756}
5757
5758uint32_t ResTable::getBasePackageId(size_t idx) const
5759{
5760 if (mError != NO_ERROR) {
5761 return 0;
5762 }
5763 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5764 "Requested package index %d past package count %d",
5765 (int)idx, (int)mPackageGroups.size());
5766 return mPackageGroups[idx]->id;
5767}
5768
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005769uint32_t ResTable::getLastTypeIdForPackage(size_t idx) const
5770{
5771 if (mError != NO_ERROR) {
5772 return 0;
5773 }
5774 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5775 "Requested package index %d past package count %d",
5776 (int)idx, (int)mPackageGroups.size());
5777 const PackageGroup* const group = mPackageGroups[idx];
5778 return group->largestTypeId;
5779}
5780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781size_t ResTable::getTableCount() const
5782{
5783 return mHeaders.size();
5784}
5785
5786const ResStringPool* ResTable::getTableStringBlock(size_t index) const
5787{
5788 return &mHeaders[index]->values;
5789}
5790
Narayan Kamath7c4887f2014-01-27 17:32:37 +00005791int32_t ResTable::getTableCookie(size_t index) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792{
5793 return mHeaders[index]->cookie;
5794}
5795
Adam Lesinskide898ff2014-01-29 18:20:45 -08005796const DynamicRefTable* ResTable::getDynamicRefTableForCookie(int32_t cookie) const
5797{
5798 const size_t N = mPackageGroups.size();
5799 for (size_t i = 0; i < N; i++) {
5800 const PackageGroup* pg = mPackageGroups[i];
5801 size_t M = pg->packages.size();
5802 for (size_t j = 0; j < M; j++) {
5803 if (pg->packages[j]->header->cookie == cookie) {
5804 return &pg->dynamicRefTable;
5805 }
5806 }
5807 }
5808 return NULL;
5809}
5810
Filip Gruszczynski23493322015-07-29 17:02:59 -07005811void ResTable::getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap,
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005812 bool ignoreAndroidPackage, bool includeSystemConfigs) const {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005813 const size_t packageCount = mPackageGroups.size();
Filip Gruszczynski23493322015-07-29 17:02:59 -07005814 String16 android("android");
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005815 for (size_t i = 0; i < packageCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 const PackageGroup* packageGroup = mPackageGroups[i];
Filip Gruszczynski23493322015-07-29 17:02:59 -07005817 if (ignoreAndroidPackage && android == packageGroup->name) {
5818 continue;
5819 }
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005820 if (!includeSystemConfigs && packageGroup->isSystemAsset) {
5821 continue;
5822 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005823 const size_t typeCount = packageGroup->types.size();
5824 for (size_t j = 0; j < typeCount; j++) {
5825 const TypeList& typeList = packageGroup->types[j];
5826 const size_t numTypes = typeList.size();
5827 for (size_t k = 0; k < numTypes; k++) {
5828 const Type* type = typeList[k];
Adam Lesinski42eea272015-01-15 17:01:39 -08005829 const ResStringPool& typeStrings = type->package->typeStrings;
5830 if (ignoreMipmap && typeStrings.string8ObjectAt(
5831 type->typeSpec->id - 1) == "mipmap") {
5832 continue;
5833 }
5834
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005835 const size_t numConfigs = type->configs.size();
5836 for (size_t m = 0; m < numConfigs; m++) {
5837 const ResTable_type* config = type->configs[m];
Narayan Kamath788fa412014-01-21 15:32:36 +00005838 ResTable_config cfg;
5839 memset(&cfg, 0, sizeof(ResTable_config));
5840 cfg.copyFromDtoH(config->config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 // only insert unique
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005842 const size_t N = configs->size();
5843 size_t n;
5844 for (n = 0; n < N; n++) {
5845 if (0 == (*configs)[n].compare(cfg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 break;
5847 }
5848 }
5849 // if we didn't find it
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005850 if (n == N) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005851 configs->add(cfg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 }
5853 }
5854 }
5855 }
5856 }
5857}
5858
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005859void ResTable::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860{
5861 Vector<ResTable_config> configs;
Steve Block71f2cf12011-10-20 11:56:00 +01005862 ALOGV("calling getConfigurations");
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005863 getConfigurations(&configs,
5864 false /* ignoreMipmap */,
5865 false /* ignoreAndroidPackage */,
5866 includeSystemLocales /* includeSystemConfigs */);
Steve Block71f2cf12011-10-20 11:56:00 +01005867 ALOGV("called getConfigurations size=%d", (int)configs.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 const size_t I = configs.size();
Narayan Kamath48620f12014-01-20 13:57:11 +00005869
5870 char locale[RESTABLE_MAX_LOCALE_LEN];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 for (size_t i=0; i<I; i++) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005872 configs[i].getBcp47Locale(locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 const size_t J = locales->size();
5874 size_t j;
5875 for (j=0; j<J; j++) {
5876 if (0 == strcmp(locale, (*locales)[j].string())) {
5877 break;
5878 }
5879 }
5880 if (j == J) {
5881 locales->add(String8(locale));
5882 }
5883 }
5884}
5885
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005886StringPoolRef::StringPoolRef(const ResStringPool* pool, uint32_t index)
5887 : mPool(pool), mIndex(index) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005889StringPoolRef::StringPoolRef()
5890 : mPool(NULL), mIndex(0) {}
5891
5892const char* StringPoolRef::string8(size_t* outLen) const {
5893 if (mPool != NULL) {
5894 return mPool->string8At(mIndex, outLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005896 if (outLen != NULL) {
5897 *outLen = 0;
5898 }
5899 return NULL;
5900}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005902const char16_t* StringPoolRef::string16(size_t* outLen) const {
5903 if (mPool != NULL) {
5904 return mPool->stringAt(mIndex, outLen);
5905 }
5906 if (outLen != NULL) {
5907 *outLen = 0;
5908 }
5909 return NULL;
5910}
5911
Adam Lesinski82a2dd82014-09-17 18:34:15 -07005912bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const {
5913 if (mError != NO_ERROR) {
5914 return false;
5915 }
5916
5917 const ssize_t p = getResourcePackageIndex(resID);
5918 const int t = Res_GETTYPE(resID);
5919 const int e = Res_GETENTRY(resID);
5920
5921 if (p < 0) {
5922 if (Res_GETPACKAGE(resID)+1 == 0) {
5923 ALOGW("No package identifier when getting flags for resource number 0x%08x", resID);
5924 } else {
5925 ALOGW("No known package when getting flags for resource number 0x%08x", resID);
5926 }
5927 return false;
5928 }
5929 if (t < 0) {
5930 ALOGW("No type identifier when getting flags for resource number 0x%08x", resID);
5931 return false;
5932 }
5933
5934 const PackageGroup* const grp = mPackageGroups[p];
5935 if (grp == NULL) {
5936 ALOGW("Bad identifier when getting flags for resource number 0x%08x", resID);
5937 return false;
5938 }
5939
5940 Entry entry;
5941 status_t err = getEntry(grp, t, e, NULL, &entry);
5942 if (err != NO_ERROR) {
5943 return false;
5944 }
5945
5946 *outFlags = entry.specFlags;
5947 return true;
5948}
5949
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005950status_t ResTable::getEntry(
5951 const PackageGroup* packageGroup, int typeIndex, int entryIndex,
5952 const ResTable_config* config,
5953 Entry* outEntry) const
5954{
5955 const TypeList& typeList = packageGroup->types[typeIndex];
5956 if (typeList.isEmpty()) {
5957 ALOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 return BAD_TYPE;
5959 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005960
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005961 const ResTable_type* bestType = NULL;
5962 uint32_t bestOffset = ResTable_type::NO_ENTRY;
5963 const Package* bestPackage = NULL;
5964 uint32_t specFlags = 0;
5965 uint8_t actualTypeIndex = typeIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 ResTable_config bestConfig;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005967 memset(&bestConfig, 0, sizeof(bestConfig));
Mark Salyzyn00adb862014-03-19 11:00:06 -07005968
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005969 // Iterate over the Types of each package.
5970 const size_t typeCount = typeList.size();
5971 for (size_t i = 0; i < typeCount; i++) {
5972 const Type* const typeSpec = typeList[i];
Mark Salyzyn00adb862014-03-19 11:00:06 -07005973
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005974 int realEntryIndex = entryIndex;
5975 int realTypeIndex = typeIndex;
5976 bool currentTypeIsOverlay = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005978 // Runtime overlay packages provide a mapping of app resource
5979 // ID to package resource ID.
5980 if (typeSpec->idmapEntries.hasEntries()) {
5981 uint16_t overlayEntryIndex;
5982 if (typeSpec->idmapEntries.lookup(entryIndex, &overlayEntryIndex) != NO_ERROR) {
5983 // No such mapping exists
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 continue;
5985 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005986 realEntryIndex = overlayEntryIndex;
5987 realTypeIndex = typeSpec->idmapEntries.overlayTypeId() - 1;
5988 currentTypeIsOverlay = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005990
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005991 if (static_cast<size_t>(realEntryIndex) >= typeSpec->entryCount) {
5992 ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)",
5993 Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex),
5994 entryIndex, static_cast<int>(typeSpec->entryCount));
5995 // We should normally abort here, but some legacy apps declare
5996 // resources in the 'android' package (old bug in AAPT).
5997 continue;
5998 }
5999
6000 // Aggregate all the flags for each package that defines this entry.
6001 if (typeSpec->typeSpecFlags != NULL) {
6002 specFlags |= dtohl(typeSpec->typeSpecFlags[realEntryIndex]);
6003 } else {
6004 specFlags = -1;
6005 }
6006
Adam Lesinskiff5808d2016-02-23 17:49:53 -08006007 const Vector<const ResTable_type*>* candidateConfigs = &typeSpec->configs;
6008
6009 std::shared_ptr<Vector<const ResTable_type*>> filteredConfigs;
6010 if (config && memcmp(&mParams, config, sizeof(mParams)) == 0) {
6011 // Grab the lock first so we can safely get the current filtered list.
6012 AutoMutex _lock(mFilteredConfigLock);
6013
6014 // This configuration is equal to the one we have previously cached for,
6015 // so use the filtered configs.
6016
6017 const TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries[typeIndex];
6018 if (i < cacheEntry.filteredConfigs.size()) {
6019 if (cacheEntry.filteredConfigs[i]) {
6020 // Grab a reference to the shared_ptr so it doesn't get destroyed while
6021 // going through this list.
6022 filteredConfigs = cacheEntry.filteredConfigs[i];
6023
6024 // Use this filtered list.
6025 candidateConfigs = filteredConfigs.get();
6026 }
6027 }
6028 }
6029
6030 const size_t numConfigs = candidateConfigs->size();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006031 for (size_t c = 0; c < numConfigs; c++) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08006032 const ResTable_type* const thisType = candidateConfigs->itemAt(c);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006033 if (thisType == NULL) {
6034 continue;
6035 }
6036
6037 ResTable_config thisConfig;
6038 thisConfig.copyFromDtoH(thisType->config);
6039
6040 // Check to make sure this one is valid for the current parameters.
6041 if (config != NULL && !thisConfig.match(*config)) {
6042 continue;
6043 }
6044
6045 // Check if there is the desired entry in this type.
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006046 const uint32_t* const eindex = reinterpret_cast<const uint32_t*>(
6047 reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize));
6048
6049 uint32_t thisOffset = dtohl(eindex[realEntryIndex]);
6050 if (thisOffset == ResTable_type::NO_ENTRY) {
6051 // There is no entry for this index and configuration.
6052 continue;
6053 }
6054
6055 if (bestType != NULL) {
6056 // Check if this one is less specific than the last found. If so,
6057 // we will skip it. We check starting with things we most care
6058 // about to those we least care about.
6059 if (!thisConfig.isBetterThan(bestConfig, config)) {
6060 if (!currentTypeIsOverlay || thisConfig.compare(bestConfig) != 0) {
6061 continue;
6062 }
6063 }
6064 }
6065
6066 bestType = thisType;
6067 bestOffset = thisOffset;
6068 bestConfig = thisConfig;
6069 bestPackage = typeSpec->package;
6070 actualTypeIndex = realTypeIndex;
6071
6072 // If no config was specified, any type will do, so skip
6073 if (config == NULL) {
6074 break;
6075 }
6076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006078
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006079 if (bestType == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 return BAD_INDEX;
6081 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006082
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006083 bestOffset += dtohl(bestType->entriesStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006085 if (bestOffset > (dtohl(bestType->header.size)-sizeof(ResTable_entry))) {
Steve Block8564c8d2012-01-05 23:22:43 +00006086 ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006087 bestOffset, dtohl(bestType->header.size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 return BAD_TYPE;
6089 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006090 if ((bestOffset & 0x3) != 0) {
6091 ALOGW("ResTable_entry at 0x%x is not on an integer boundary", bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 return BAD_TYPE;
6093 }
6094
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006095 const ResTable_entry* const entry = reinterpret_cast<const ResTable_entry*>(
6096 reinterpret_cast<const uint8_t*>(bestType) + bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 if (dtohs(entry->size) < sizeof(*entry)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006098 ALOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 return BAD_TYPE;
6100 }
6101
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006102 if (outEntry != NULL) {
6103 outEntry->entry = entry;
6104 outEntry->config = bestConfig;
6105 outEntry->type = bestType;
6106 outEntry->specFlags = specFlags;
6107 outEntry->package = bestPackage;
6108 outEntry->typeStr = StringPoolRef(&bestPackage->typeStrings, actualTypeIndex - bestPackage->typeIdOffset);
6109 outEntry->keyStr = StringPoolRef(&bestPackage->keyStrings, dtohl(entry->key.index));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006111 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112}
6113
6114status_t ResTable::parsePackage(const ResTable_package* const pkg,
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006115 const Header* const header, bool appAsLib, bool isSystemAsset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116{
6117 const uint8_t* base = (const uint8_t*)pkg;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006118 status_t err = validate_chunk(&pkg->header, sizeof(*pkg) - sizeof(pkg->typeIdOffset),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 header->dataEnd, "ResTable_package");
6120 if (err != NO_ERROR) {
6121 return (mError=err);
6122 }
6123
Patrik Bannura443dd932014-02-12 13:38:54 +01006124 const uint32_t pkgSize = dtohl(pkg->header.size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125
6126 if (dtohl(pkg->typeStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006127 ALOGW("ResTable_package type strings at 0x%x are past chunk size 0x%x.",
6128 dtohl(pkg->typeStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 return (mError=BAD_TYPE);
6130 }
6131 if ((dtohl(pkg->typeStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006132 ALOGW("ResTable_package type strings at 0x%x is not on an integer boundary.",
6133 dtohl(pkg->typeStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 return (mError=BAD_TYPE);
6135 }
6136 if (dtohl(pkg->keyStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006137 ALOGW("ResTable_package key strings at 0x%x are past chunk size 0x%x.",
6138 dtohl(pkg->keyStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 return (mError=BAD_TYPE);
6140 }
6141 if ((dtohl(pkg->keyStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006142 ALOGW("ResTable_package key strings at 0x%x is not on an integer boundary.",
6143 dtohl(pkg->keyStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 return (mError=BAD_TYPE);
6145 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006146
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006147 uint32_t id = dtohl(pkg->id);
6148 KeyedVector<uint8_t, IdmapEntries> idmapEntries;
Mark Salyzyn00adb862014-03-19 11:00:06 -07006149
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006150 if (header->resourceIDMap != NULL) {
6151 uint8_t targetPackageId = 0;
6152 status_t err = parseIdmap(header->resourceIDMap, header->resourceIDMapSize, &targetPackageId, &idmapEntries);
6153 if (err != NO_ERROR) {
6154 ALOGW("Overlay is broken");
6155 return (mError=err);
6156 }
6157 id = targetPackageId;
6158 }
6159
6160 if (id >= 256) {
6161 LOG_ALWAYS_FATAL("Package id out of range");
6162 return NO_ERROR;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006163 } else if (id == 0 || appAsLib || isSystemAsset) {
6164 // This is a library or a system asset, so assign an ID
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006165 id = mNextPackageId++;
6166 }
6167
6168 PackageGroup* group = NULL;
6169 Package* package = new Package(this, header, pkg);
6170 if (package == NULL) {
6171 return (mError=NO_MEMORY);
6172 }
6173
6174 err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings),
6175 header->dataEnd-(base+dtohl(pkg->typeStrings)));
6176 if (err != NO_ERROR) {
6177 delete group;
6178 delete package;
6179 return (mError=err);
6180 }
6181
6182 err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings),
6183 header->dataEnd-(base+dtohl(pkg->keyStrings)));
6184 if (err != NO_ERROR) {
6185 delete group;
6186 delete package;
6187 return (mError=err);
6188 }
6189
6190 size_t idx = mPackageMap[id];
6191 if (idx == 0) {
6192 idx = mPackageGroups.size() + 1;
6193
Adam Lesinski4bf58102014-11-03 11:21:19 -08006194 char16_t tmpName[sizeof(pkg->name)/sizeof(pkg->name[0])];
6195 strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(pkg->name[0]));
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006196 group = new PackageGroup(this, String16(tmpName), id, appAsLib, isSystemAsset);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006197 if (group == NULL) {
6198 delete package;
Dianne Hackborn78c40512009-07-06 11:07:40 -07006199 return (mError=NO_MEMORY);
6200 }
Adam Lesinskifab50872014-04-16 14:40:42 -07006201
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006202 err = mPackageGroups.add(group);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 if (err < NO_ERROR) {
6204 return (mError=err);
6205 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006206
6207 mPackageMap[id] = static_cast<uint8_t>(idx);
6208
6209 // Find all packages that reference this package
6210 size_t N = mPackageGroups.size();
6211 for (size_t i = 0; i < N; i++) {
6212 mPackageGroups[i]->dynamicRefTable.addMapping(
6213 group->name, static_cast<uint8_t>(group->id));
6214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006216 group = mPackageGroups.itemAt(idx - 1);
6217 if (group == NULL) {
6218 return (mError=UNKNOWN_ERROR);
6219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 }
6221
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006222 err = group->packages.add(package);
6223 if (err < NO_ERROR) {
6224 return (mError=err);
6225 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 // Iterate through all chunks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 const ResChunk_header* chunk =
6229 (const ResChunk_header*)(((const uint8_t*)pkg)
6230 + dtohs(pkg->header.headerSize));
6231 const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size);
6232 while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) &&
6233 ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006234 if (kDebugTableNoisy) {
6235 ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
6236 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
6237 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
6238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 const size_t csize = dtohl(chunk->size);
6240 const uint16_t ctype = dtohs(chunk->type);
6241 if (ctype == RES_TABLE_TYPE_SPEC_TYPE) {
6242 const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk);
6243 err = validate_chunk(&typeSpec->header, sizeof(*typeSpec),
6244 endPos, "ResTable_typeSpec");
6245 if (err != NO_ERROR) {
6246 return (mError=err);
6247 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249 const size_t typeSpecSize = dtohl(typeSpec->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006250 const size_t newEntryCount = dtohl(typeSpec->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006251
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006252 if (kDebugLoadTableNoisy) {
6253 ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
6254 (void*)(base-(const uint8_t*)chunk),
6255 dtohs(typeSpec->header.type),
6256 dtohs(typeSpec->header.headerSize),
6257 (void*)typeSpecSize);
6258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 // look for block overrun or int overflow when multiplying by 4
6260 if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006261 || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 > typeSpecSize)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006263 ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006264 (void*)(dtohs(typeSpec->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6265 (void*)typeSpecSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 return (mError=BAD_TYPE);
6267 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 if (typeSpec->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006270 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 return (mError=BAD_TYPE);
6272 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006273
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006274 if (newEntryCount > 0) {
6275 uint8_t typeIndex = typeSpec->id - 1;
6276 ssize_t idmapIndex = idmapEntries.indexOfKey(typeSpec->id);
6277 if (idmapIndex >= 0) {
6278 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6279 }
6280
6281 TypeList& typeList = group->types.editItemAt(typeIndex);
6282 if (!typeList.isEmpty()) {
6283 const Type* existingType = typeList[0];
6284 if (existingType->entryCount != newEntryCount && idmapIndex < 0) {
6285 ALOGW("ResTable_typeSpec entry count inconsistent: given %d, previously %d",
6286 (int) newEntryCount, (int) existingType->entryCount);
6287 // We should normally abort here, but some legacy apps declare
6288 // resources in the 'android' package (old bug in AAPT).
6289 }
6290 }
6291
6292 Type* t = new Type(header, package, newEntryCount);
6293 t->typeSpec = typeSpec;
6294 t->typeSpecFlags = (const uint32_t*)(
6295 ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize));
6296 if (idmapIndex >= 0) {
6297 t->idmapEntries = idmapEntries[idmapIndex];
6298 }
6299 typeList.add(t);
6300 group->largestTypeId = max(group->largestTypeId, typeSpec->id);
6301 } else {
6302 ALOGV("Skipping empty ResTable_typeSpec for type %d", typeSpec->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 } else if (ctype == RES_TABLE_TYPE_TYPE) {
6306 const ResTable_type* type = (const ResTable_type*)(chunk);
6307 err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4,
6308 endPos, "ResTable_type");
6309 if (err != NO_ERROR) {
6310 return (mError=err);
6311 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006312
Patrik Bannura443dd932014-02-12 13:38:54 +01006313 const uint32_t typeSize = dtohl(type->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006314 const size_t newEntryCount = dtohl(type->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006315
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006316 if (kDebugLoadTableNoisy) {
6317 printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n",
6318 (void*)(base-(const uint8_t*)chunk),
6319 dtohs(type->header.type),
6320 dtohs(type->header.headerSize),
6321 typeSize);
6322 }
6323 if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006324 ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006325 (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6326 typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 return (mError=BAD_TYPE);
6328 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006329
6330 if (newEntryCount != 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006332 ALOGW("ResTable_type entriesStart at 0x%x extends beyond chunk end 0x%x.",
6333 dtohl(type->entriesStart), typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 return (mError=BAD_TYPE);
6335 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 if (type->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006338 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 return (mError=BAD_TYPE);
6340 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006341
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006342 if (newEntryCount > 0) {
6343 uint8_t typeIndex = type->id - 1;
6344 ssize_t idmapIndex = idmapEntries.indexOfKey(type->id);
6345 if (idmapIndex >= 0) {
6346 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6347 }
6348
6349 TypeList& typeList = group->types.editItemAt(typeIndex);
6350 if (typeList.isEmpty()) {
6351 ALOGE("No TypeSpec for type %d", type->id);
6352 return (mError=BAD_TYPE);
6353 }
6354
6355 Type* t = typeList.editItemAt(typeList.size() - 1);
6356 if (newEntryCount != t->entryCount) {
6357 ALOGE("ResTable_type entry count inconsistent: given %d, previously %d",
6358 (int)newEntryCount, (int)t->entryCount);
6359 return (mError=BAD_TYPE);
6360 }
6361
6362 if (t->package != package) {
6363 ALOGE("No TypeSpec for type %d", type->id);
6364 return (mError=BAD_TYPE);
6365 }
6366
6367 t->configs.add(type);
6368
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006369 if (kDebugTableGetEntry) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006370 ResTable_config thisConfig;
6371 thisConfig.copyFromDtoH(type->config);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006372 ALOGI("Adding config to type %d: %s\n", type->id,
6373 thisConfig.toString().string());
6374 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006375 } else {
6376 ALOGV("Skipping empty ResTable_type for type %d", type->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006378
Adam Lesinskide898ff2014-01-29 18:20:45 -08006379 } else if (ctype == RES_TABLE_LIBRARY_TYPE) {
6380 if (group->dynamicRefTable.entries().size() == 0) {
6381 status_t err = group->dynamicRefTable.load((const ResTable_lib_header*) chunk);
6382 if (err != NO_ERROR) {
6383 return (mError=err);
6384 }
6385
6386 // Fill in the reference table with the entries we already know about.
6387 size_t N = mPackageGroups.size();
6388 for (size_t i = 0; i < N; i++) {
6389 group->dynamicRefTable.addMapping(mPackageGroups[i]->name, mPackageGroups[i]->id);
6390 }
6391 } else {
6392 ALOGW("Found multiple library tables, ignoring...");
6393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 } else {
6395 status_t err = validate_chunk(chunk, sizeof(ResChunk_header),
6396 endPos, "ResTable_package:unknown");
6397 if (err != NO_ERROR) {
6398 return (mError=err);
6399 }
6400 }
6401 chunk = (const ResChunk_header*)
6402 (((const uint8_t*)chunk) + csize);
6403 }
6404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 return NO_ERROR;
6406}
6407
Tao Baia6d7e3f2015-09-01 18:49:54 -07006408DynamicRefTable::DynamicRefTable(uint8_t packageId, bool appAsLib)
Adam Lesinskide898ff2014-01-29 18:20:45 -08006409 : mAssignedPackageId(packageId)
Tao Baia6d7e3f2015-09-01 18:49:54 -07006410 , mAppAsLib(appAsLib)
Adam Lesinskide898ff2014-01-29 18:20:45 -08006411{
6412 memset(mLookupTable, 0, sizeof(mLookupTable));
6413
6414 // Reserved package ids
6415 mLookupTable[APP_PACKAGE_ID] = APP_PACKAGE_ID;
6416 mLookupTable[SYS_PACKAGE_ID] = SYS_PACKAGE_ID;
6417}
6418
6419status_t DynamicRefTable::load(const ResTable_lib_header* const header)
6420{
6421 const uint32_t entryCount = dtohl(header->count);
6422 const uint32_t sizeOfEntries = sizeof(ResTable_lib_entry) * entryCount;
6423 const uint32_t expectedSize = dtohl(header->header.size) - dtohl(header->header.headerSize);
6424 if (sizeOfEntries > expectedSize) {
6425 ALOGE("ResTable_lib_header size %u is too small to fit %u entries (x %u).",
6426 expectedSize, entryCount, (uint32_t)sizeof(ResTable_lib_entry));
6427 return UNKNOWN_ERROR;
6428 }
6429
6430 const ResTable_lib_entry* entry = (const ResTable_lib_entry*)(((uint8_t*) header) +
6431 dtohl(header->header.headerSize));
6432 for (uint32_t entryIndex = 0; entryIndex < entryCount; entryIndex++) {
6433 uint32_t packageId = dtohl(entry->packageId);
6434 char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)];
6435 strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006436 if (kDebugLibNoisy) {
6437 ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
6438 dtohl(entry->packageId));
6439 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08006440 if (packageId >= 256) {
6441 ALOGE("Bad package id 0x%08x", packageId);
6442 return UNKNOWN_ERROR;
6443 }
6444 mEntries.replaceValueFor(String16(tmpName), (uint8_t) packageId);
6445 entry = entry + 1;
6446 }
6447 return NO_ERROR;
6448}
6449
Adam Lesinski6022deb2014-08-20 14:59:19 -07006450status_t DynamicRefTable::addMappings(const DynamicRefTable& other) {
6451 if (mAssignedPackageId != other.mAssignedPackageId) {
6452 return UNKNOWN_ERROR;
6453 }
6454
6455 const size_t entryCount = other.mEntries.size();
6456 for (size_t i = 0; i < entryCount; i++) {
6457 ssize_t index = mEntries.indexOfKey(other.mEntries.keyAt(i));
6458 if (index < 0) {
6459 mEntries.add(other.mEntries.keyAt(i), other.mEntries[i]);
6460 } else {
6461 if (other.mEntries[i] != mEntries[index]) {
6462 return UNKNOWN_ERROR;
6463 }
6464 }
6465 }
6466
6467 // Merge the lookup table. No entry can conflict
6468 // (value of 0 means not set).
6469 for (size_t i = 0; i < 256; i++) {
6470 if (mLookupTable[i] != other.mLookupTable[i]) {
6471 if (mLookupTable[i] == 0) {
6472 mLookupTable[i] = other.mLookupTable[i];
6473 } else if (other.mLookupTable[i] != 0) {
6474 return UNKNOWN_ERROR;
6475 }
6476 }
6477 }
6478 return NO_ERROR;
6479}
6480
Adam Lesinskide898ff2014-01-29 18:20:45 -08006481status_t DynamicRefTable::addMapping(const String16& packageName, uint8_t packageId)
6482{
6483 ssize_t index = mEntries.indexOfKey(packageName);
6484 if (index < 0) {
6485 return UNKNOWN_ERROR;
6486 }
6487 mLookupTable[mEntries.valueAt(index)] = packageId;
6488 return NO_ERROR;
6489}
6490
6491status_t DynamicRefTable::lookupResourceId(uint32_t* resId) const {
6492 uint32_t res = *resId;
6493 size_t packageId = Res_GETPACKAGE(res) + 1;
6494
Tao Baia6d7e3f2015-09-01 18:49:54 -07006495 if (packageId == APP_PACKAGE_ID && !mAppAsLib) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08006496 // No lookup needs to be done, app package IDs are absolute.
6497 return NO_ERROR;
6498 }
6499
Tao Baia6d7e3f2015-09-01 18:49:54 -07006500 if (packageId == 0 || (packageId == APP_PACKAGE_ID && mAppAsLib)) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08006501 // The package ID is 0x00. That means that a shared library is accessing
Tao Baia6d7e3f2015-09-01 18:49:54 -07006502 // its own local resource.
6503 // Or if app resource is loaded as shared library, the resource which has
6504 // app package Id is local resources.
6505 // so we fix up those resources with the calling package ID.
6506 *resId = (0xFFFFFF & (*resId)) | (((uint32_t) mAssignedPackageId) << 24);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006507 return NO_ERROR;
6508 }
6509
6510 // Do a proper lookup.
6511 uint8_t translatedId = mLookupTable[packageId];
6512 if (translatedId == 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006513 ALOGV("DynamicRefTable(0x%02x): No mapping for build-time package ID 0x%02x.",
Adam Lesinskide898ff2014-01-29 18:20:45 -08006514 (uint8_t)mAssignedPackageId, (uint8_t)packageId);
6515 for (size_t i = 0; i < 256; i++) {
6516 if (mLookupTable[i] != 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006517 ALOGV("e[0x%02x] -> 0x%02x", (uint8_t)i, mLookupTable[i]);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006518 }
6519 }
6520 return UNKNOWN_ERROR;
6521 }
6522
6523 *resId = (res & 0x00ffffff) | (((uint32_t) translatedId) << 24);
6524 return NO_ERROR;
6525}
6526
6527status_t DynamicRefTable::lookupResourceValue(Res_value* value) const {
Tao Baia6d7e3f2015-09-01 18:49:54 -07006528 if (value->dataType != Res_value::TYPE_DYNAMIC_REFERENCE &&
6529 (value->dataType != Res_value::TYPE_REFERENCE || !mAppAsLib)) {
6530 // If the package is loaded as shared library, the resource reference
6531 // also need to be fixed.
Adam Lesinskide898ff2014-01-29 18:20:45 -08006532 return NO_ERROR;
6533 }
6534
6535 status_t err = lookupResourceId(&value->data);
6536 if (err != NO_ERROR) {
6537 return err;
6538 }
6539
6540 value->dataType = Res_value::TYPE_REFERENCE;
6541 return NO_ERROR;
6542}
6543
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006544struct IdmapTypeMap {
6545 ssize_t overlayTypeId;
6546 size_t entryOffset;
6547 Vector<uint32_t> entryMap;
6548};
6549
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006550status_t ResTable::createIdmap(const ResTable& overlay,
6551 uint32_t targetCrc, uint32_t overlayCrc,
6552 const char* targetPath, const char* overlayPath,
6553 void** outData, size_t* outSize) const
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006554{
6555 // see README for details on the format of map
6556 if (mPackageGroups.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006557 ALOGW("idmap: target package has no package groups, cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006558 return UNKNOWN_ERROR;
6559 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006560
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006561 if (mPackageGroups[0]->packages.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006562 ALOGW("idmap: target package has no packages in its first package group, "
6563 "cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006564 return UNKNOWN_ERROR;
6565 }
6566
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006567 KeyedVector<uint8_t, IdmapTypeMap> map;
6568
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006569 // overlaid packages are assumed to contain only one package group
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006570 const PackageGroup* pg = mPackageGroups[0];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006571
6572 // starting size is header
6573 *outSize = ResTable::IDMAP_HEADER_SIZE_BYTES;
6574
6575 // target package id and number of types in map
6576 *outSize += 2 * sizeof(uint16_t);
6577
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006578 // overlay packages are assumed to contain only one package group
Adam Lesinski4bf58102014-11-03 11:21:19 -08006579 const ResTable_package* overlayPackageStruct = overlay.mPackageGroups[0]->packages[0]->package;
6580 char16_t tmpName[sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])];
6581 strcpy16_dtoh(tmpName, overlayPackageStruct->name, sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0]));
6582 const String16 overlayPackage(tmpName);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006583
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006584 for (size_t typeIndex = 0; typeIndex < pg->types.size(); ++typeIndex) {
6585 const TypeList& typeList = pg->types[typeIndex];
6586 if (typeList.isEmpty()) {
6587 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006588 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006589
6590 const Type* typeConfigs = typeList[0];
6591
6592 IdmapTypeMap typeMap;
6593 typeMap.overlayTypeId = -1;
6594 typeMap.entryOffset = 0;
6595
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006596 for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006597 uint32_t resID = Res_MAKEID(pg->id - 1, typeIndex, entryIndex);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006598 resource_name resName;
MÃ¥rten Kongstad65a05fd2014-01-31 14:01:52 +01006599 if (!this->getResourceName(resID, false, &resName)) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006600 if (typeMap.entryMap.isEmpty()) {
6601 typeMap.entryOffset++;
6602 }
MÃ¥rten Kongstadfcaba142011-05-19 16:02:35 +02006603 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006604 }
6605
6606 const String16 overlayType(resName.type, resName.typeLen);
6607 const String16 overlayName(resName.name, resName.nameLen);
6608 uint32_t overlayResID = overlay.identifierForName(overlayName.string(),
6609 overlayName.size(),
6610 overlayType.string(),
6611 overlayType.size(),
6612 overlayPackage.string(),
6613 overlayPackage.size());
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006614 if (overlayResID == 0) {
6615 if (typeMap.entryMap.isEmpty()) {
6616 typeMap.entryOffset++;
Jean-Baptiste Queru3e2d5912012-05-01 10:00:22 -07006617 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006618 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006619 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006620
6621 if (typeMap.overlayTypeId == -1) {
6622 typeMap.overlayTypeId = Res_GETTYPE(overlayResID) + 1;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006623 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006624
6625 if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) {
6626 ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x"
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006627 " but entries should map to resources of type %02zx",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006628 resID, overlayResID, typeMap.overlayTypeId);
6629 return BAD_TYPE;
6630 }
6631
6632 if (typeMap.entryOffset + typeMap.entryMap.size() < entryIndex) {
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006633 // pad with 0xffffffff's (indicating non-existing entries) before adding this entry
6634 size_t index = typeMap.entryMap.size();
6635 size_t numItems = entryIndex - (typeMap.entryOffset + index);
6636 if (typeMap.entryMap.insertAt(0xffffffff, index, numItems) < 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006637 return NO_MEMORY;
6638 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006639 }
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006640 typeMap.entryMap.add(Res_GETENTRY(overlayResID));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006641 }
6642
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006643 if (!typeMap.entryMap.isEmpty()) {
6644 if (map.add(static_cast<uint8_t>(typeIndex), typeMap) < 0) {
6645 return NO_MEMORY;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006646 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006647 *outSize += (4 * sizeof(uint16_t)) + (typeMap.entryMap.size() * sizeof(uint32_t));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006648 }
6649 }
6650
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006651 if (map.isEmpty()) {
6652 ALOGW("idmap: no resources in overlay package present in base package");
6653 return UNKNOWN_ERROR;
6654 }
6655
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006656 if ((*outData = malloc(*outSize)) == NULL) {
6657 return NO_MEMORY;
6658 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006659
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006660 uint32_t* data = (uint32_t*)*outData;
6661 *data++ = htodl(IDMAP_MAGIC);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006662 *data++ = htodl(IDMAP_CURRENT_VERSION);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006663 *data++ = htodl(targetCrc);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006664 *data++ = htodl(overlayCrc);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006665 const char* paths[] = { targetPath, overlayPath };
6666 for (int j = 0; j < 2; ++j) {
6667 char* p = (char*)data;
6668 const char* path = paths[j];
6669 const size_t I = strlen(path);
6670 if (I > 255) {
6671 ALOGV("path exceeds expected 255 characters: %s\n", path);
6672 return UNKNOWN_ERROR;
6673 }
6674 for (size_t i = 0; i < 256; ++i) {
6675 *p++ = i < I ? path[i] : '\0';
6676 }
6677 data += 256 / sizeof(uint32_t);
6678 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006679 const size_t mapSize = map.size();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006680 uint16_t* typeData = reinterpret_cast<uint16_t*>(data);
6681 *typeData++ = htods(pg->id);
6682 *typeData++ = htods(mapSize);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006683 for (size_t i = 0; i < mapSize; ++i) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006684 uint8_t targetTypeId = map.keyAt(i);
6685 const IdmapTypeMap& typeMap = map[i];
6686 *typeData++ = htods(targetTypeId + 1);
6687 *typeData++ = htods(typeMap.overlayTypeId);
6688 *typeData++ = htods(typeMap.entryMap.size());
6689 *typeData++ = htods(typeMap.entryOffset);
6690
6691 const size_t entryCount = typeMap.entryMap.size();
6692 uint32_t* entries = reinterpret_cast<uint32_t*>(typeData);
6693 for (size_t j = 0; j < entryCount; j++) {
6694 entries[j] = htodl(typeMap.entryMap[j]);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006695 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006696 typeData += entryCount * 2;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006697 }
6698
6699 return NO_ERROR;
6700}
6701
6702bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006703 uint32_t* pVersion,
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006704 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
6705 String8* pTargetPath, String8* pOverlayPath)
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006706{
6707 const uint32_t* map = (const uint32_t*)idmap;
6708 if (!assertIdmapHeader(map, sizeBytes)) {
6709 return false;
6710 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006711 if (pVersion) {
6712 *pVersion = dtohl(map[1]);
6713 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006714 if (pTargetCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006715 *pTargetCrc = dtohl(map[2]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006716 }
6717 if (pOverlayCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006718 *pOverlayCrc = dtohl(map[3]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006719 }
6720 if (pTargetPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006721 pTargetPath->setTo(reinterpret_cast<const char*>(map + 4));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006722 }
6723 if (pOverlayPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006724 pOverlayPath->setTo(reinterpret_cast<const char*>(map + 4 + 256 / sizeof(uint32_t)));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006725 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006726 return true;
6727}
6728
6729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730#define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string())
6731
6732#define CHAR16_ARRAY_EQ(constant, var, len) \
6733 ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len))))
6734
Jeff Brown9d3b1a42013-07-01 19:07:15 -07006735static void print_complex(uint32_t complex, bool isFraction)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736{
Dianne Hackborne17086b2009-06-19 15:13:28 -07006737 const float MANTISSA_MULT =
6738 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT);
6739 const float RADIX_MULTS[] = {
6740 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
6741 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
6742 };
6743
6744 float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK
6745 <<Res_value::COMPLEX_MANTISSA_SHIFT))
6746 * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT)
6747 & Res_value::COMPLEX_RADIX_MASK];
6748 printf("%f", value);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006749
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006750 if (!isFraction) {
Dianne Hackborne17086b2009-06-19 15:13:28 -07006751 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6752 case Res_value::COMPLEX_UNIT_PX: printf("px"); break;
6753 case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break;
6754 case Res_value::COMPLEX_UNIT_SP: printf("sp"); break;
6755 case Res_value::COMPLEX_UNIT_PT: printf("pt"); break;
6756 case Res_value::COMPLEX_UNIT_IN: printf("in"); break;
6757 case Res_value::COMPLEX_UNIT_MM: printf("mm"); break;
6758 default: printf(" (unknown unit)"); break;
6759 }
6760 } else {
6761 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6762 case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break;
6763 case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break;
6764 default: printf(" (unknown unit)"); break;
6765 }
6766 }
6767}
6768
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006769// Normalize a string for output
6770String8 ResTable::normalizeForOutput( const char *input )
6771{
6772 String8 ret;
6773 char buff[2];
6774 buff[1] = '\0';
6775
6776 while (*input != '\0') {
6777 switch (*input) {
6778 // All interesting characters are in the ASCII zone, so we are making our own lives
6779 // easier by scanning the string one byte at a time.
6780 case '\\':
6781 ret += "\\\\";
6782 break;
6783 case '\n':
6784 ret += "\\n";
6785 break;
6786 case '"':
6787 ret += "\\\"";
6788 break;
6789 default:
6790 buff[0] = *input;
6791 ret += buff;
6792 break;
6793 }
6794
6795 input++;
6796 }
6797
6798 return ret;
6799}
6800
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006801void ResTable::print_value(const Package* pkg, const Res_value& value) const
6802{
6803 if (value.dataType == Res_value::TYPE_NULL) {
Alan Viverettef2969402014-10-29 17:09:36 -07006804 if (value.data == Res_value::DATA_NULL_UNDEFINED) {
6805 printf("(null)\n");
6806 } else if (value.data == Res_value::DATA_NULL_EMPTY) {
6807 printf("(null empty)\n");
6808 } else {
6809 // This should never happen.
6810 printf("(null) 0x%08x\n", value.data);
6811 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006812 } else if (value.dataType == Res_value::TYPE_REFERENCE) {
6813 printf("(reference) 0x%08x\n", value.data);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006814 } else if (value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) {
6815 printf("(dynamic reference) 0x%08x\n", value.data);
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006816 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
6817 printf("(attribute) 0x%08x\n", value.data);
6818 } else if (value.dataType == Res_value::TYPE_STRING) {
6819 size_t len;
Kenny Root780d2a12010-02-22 22:36:26 -08006820 const char* str8 = pkg->header->values.string8At(
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006821 value.data, &len);
Kenny Root780d2a12010-02-22 22:36:26 -08006822 if (str8 != NULL) {
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006823 printf("(string8) \"%s\"\n", normalizeForOutput(str8).string());
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006824 } else {
Kenny Root780d2a12010-02-22 22:36:26 -08006825 const char16_t* str16 = pkg->header->values.stringAt(
6826 value.data, &len);
6827 if (str16 != NULL) {
6828 printf("(string16) \"%s\"\n",
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006829 normalizeForOutput(String8(str16, len).string()).string());
Kenny Root780d2a12010-02-22 22:36:26 -08006830 } else {
6831 printf("(string) null\n");
6832 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006833 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006834 } else if (value.dataType == Res_value::TYPE_FLOAT) {
6835 printf("(float) %g\n", *(const float*)&value.data);
6836 } else if (value.dataType == Res_value::TYPE_DIMENSION) {
6837 printf("(dimension) ");
6838 print_complex(value.data, false);
6839 printf("\n");
6840 } else if (value.dataType == Res_value::TYPE_FRACTION) {
6841 printf("(fraction) ");
6842 print_complex(value.data, true);
6843 printf("\n");
6844 } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT
6845 || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) {
6846 printf("(color) #%08x\n", value.data);
6847 } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) {
6848 printf("(boolean) %s\n", value.data ? "true" : "false");
6849 } else if (value.dataType >= Res_value::TYPE_FIRST_INT
6850 || value.dataType <= Res_value::TYPE_LAST_INT) {
6851 printf("(int) 0x%08x or %d\n", value.data, value.data);
6852 } else {
6853 printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n",
6854 (int)value.dataType, (int)value.data,
6855 (int)value.size, (int)value.res0);
6856 }
6857}
6858
Dianne Hackborne17086b2009-06-19 15:13:28 -07006859void ResTable::print(bool inclValues) const
6860{
6861 if (mError != 0) {
6862 printf("mError=0x%x (%s)\n", mError, strerror(mError));
6863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 size_t pgCount = mPackageGroups.size();
6865 printf("Package Groups (%d)\n", (int)pgCount);
6866 for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) {
6867 const PackageGroup* pg = mPackageGroups[pgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006868 printf("Package Group %d id=0x%02x packageCount=%d name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 (int)pgIndex, pg->id, (int)pg->packages.size(),
6870 String8(pg->name).string());
Mark Salyzyn00adb862014-03-19 11:00:06 -07006871
Adam Lesinski6022deb2014-08-20 14:59:19 -07006872 const KeyedVector<String16, uint8_t>& refEntries = pg->dynamicRefTable.entries();
6873 const size_t refEntryCount = refEntries.size();
6874 if (refEntryCount > 0) {
6875 printf(" DynamicRefTable entryCount=%d:\n", (int) refEntryCount);
6876 for (size_t refIndex = 0; refIndex < refEntryCount; refIndex++) {
6877 printf(" 0x%02x -> %s\n",
6878 refEntries.valueAt(refIndex),
6879 String8(refEntries.keyAt(refIndex)).string());
6880 }
6881 printf("\n");
6882 }
6883
6884 int packageId = pg->id;
Adam Lesinski18560882014-08-15 17:18:21 +00006885 size_t pkgCount = pg->packages.size();
6886 for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) {
6887 const Package* pkg = pg->packages[pkgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006888 // Use a package's real ID, since the ID may have been assigned
6889 // if this package is a shared library.
6890 packageId = pkg->package->id;
Adam Lesinski4bf58102014-11-03 11:21:19 -08006891 char16_t tmpName[sizeof(pkg->package->name)/sizeof(pkg->package->name[0])];
6892 strcpy16_dtoh(tmpName, pkg->package->name, sizeof(pkg->package->name)/sizeof(pkg->package->name[0]));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006893 printf(" Package %d id=0x%02x name=%s\n", (int)pkgIndex,
Adam Lesinski4bf58102014-11-03 11:21:19 -08006894 pkg->package->id, String8(tmpName).string());
Adam Lesinski18560882014-08-15 17:18:21 +00006895 }
6896
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006897 for (size_t typeIndex=0; typeIndex < pg->types.size(); typeIndex++) {
6898 const TypeList& typeList = pg->types[typeIndex];
6899 if (typeList.isEmpty()) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006900 continue;
6901 }
6902 const Type* typeConfigs = typeList[0];
6903 const size_t NTC = typeConfigs->configs.size();
6904 printf(" type %d configCount=%d entryCount=%d\n",
6905 (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount);
6906 if (typeConfigs->typeSpecFlags != NULL) {
6907 for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) {
Adam Lesinski6022deb2014-08-20 14:59:19 -07006908 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006909 | (0x00ff0000 & ((typeIndex+1)<<16))
6910 | (0x0000ffff & (entryIndex));
6911 // Since we are creating resID without actually
6912 // iterating over them, we have no idea which is a
6913 // dynamic reference. We must check.
Adam Lesinski6022deb2014-08-20 14:59:19 -07006914 if (packageId == 0) {
6915 pg->dynamicRefTable.lookupResourceId(&resID);
6916 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006917
6918 resource_name resName;
6919 if (this->getResourceName(resID, true, &resName)) {
6920 String8 type8;
6921 String8 name8;
6922 if (resName.type8 != NULL) {
6923 type8 = String8(resName.type8, resName.typeLen);
6924 } else {
6925 type8 = String8(resName.type, resName.typeLen);
6926 }
6927 if (resName.name8 != NULL) {
6928 name8 = String8(resName.name8, resName.nameLen);
6929 } else {
6930 name8 = String8(resName.name, resName.nameLen);
6931 }
6932 printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n",
6933 resID,
6934 CHAR16_TO_CSTR(resName.package, resName.packageLen),
6935 type8.string(), name8.string(),
6936 dtohl(typeConfigs->typeSpecFlags[entryIndex]));
6937 } else {
6938 printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID);
6939 }
6940 }
6941 }
6942 for (size_t configIndex=0; configIndex<NTC; configIndex++) {
6943 const ResTable_type* type = typeConfigs->configs[configIndex];
6944 if ((((uint64_t)type)&0x3) != 0) {
6945 printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 continue;
6947 }
Adam Lesinski5b0f1be2015-07-27 16:53:14 -07006948
6949 // Always copy the config, as fields get added and we need to
6950 // set the defaults.
6951 ResTable_config thisConfig;
6952 thisConfig.copyFromDtoH(type->config);
6953
6954 String8 configStr = thisConfig.toString();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006955 printf(" config %s:\n", configStr.size() > 0
6956 ? configStr.string() : "(default)");
6957 size_t entryCount = dtohl(type->entryCount);
6958 uint32_t entriesStart = dtohl(type->entriesStart);
6959 if ((entriesStart&0x3) != 0) {
6960 printf(" NON-INTEGER ResTable_type entriesStart OFFSET: 0x%x\n", entriesStart);
6961 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006963 uint32_t typeSize = dtohl(type->header.size);
6964 if ((typeSize&0x3) != 0) {
6965 printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize);
6966 continue;
6967 }
6968 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006969 const uint32_t* const eindex = (const uint32_t*)
6970 (((const uint8_t*)type) + dtohs(type->header.headerSize));
6971
6972 uint32_t thisOffset = dtohl(eindex[entryIndex]);
6973 if (thisOffset == ResTable_type::NO_ENTRY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006974 continue;
6975 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006976
Adam Lesinski6022deb2014-08-20 14:59:19 -07006977 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006978 | (0x00ff0000 & ((typeIndex+1)<<16))
6979 | (0x0000ffff & (entryIndex));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006980 if (packageId == 0) {
6981 pg->dynamicRefTable.lookupResourceId(&resID);
6982 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006983 resource_name resName;
6984 if (this->getResourceName(resID, true, &resName)) {
6985 String8 type8;
6986 String8 name8;
6987 if (resName.type8 != NULL) {
6988 type8 = String8(resName.type8, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006989 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006990 type8 = String8(resName.type, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006991 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006992 if (resName.name8 != NULL) {
6993 name8 = String8(resName.name8, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006995 name8 = String8(resName.name, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006997 printf(" resource 0x%08x %s:%s/%s: ", resID,
6998 CHAR16_TO_CSTR(resName.package, resName.packageLen),
6999 type8.string(), name8.string());
7000 } else {
7001 printf(" INVALID RESOURCE 0x%08x: ", resID);
7002 }
7003 if ((thisOffset&0x3) != 0) {
7004 printf("NON-INTEGER OFFSET: 0x%x\n", thisOffset);
7005 continue;
7006 }
7007 if ((thisOffset+sizeof(ResTable_entry)) > typeSize) {
7008 printf("OFFSET OUT OF BOUNDS: 0x%x+0x%x (size is 0x%x)\n",
7009 entriesStart, thisOffset, typeSize);
7010 continue;
7011 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07007012
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007013 const ResTable_entry* ent = (const ResTable_entry*)
7014 (((const uint8_t*)type) + entriesStart + thisOffset);
7015 if (((entriesStart + thisOffset)&0x3) != 0) {
7016 printf("NON-INTEGER ResTable_entry OFFSET: 0x%x\n",
7017 (entriesStart + thisOffset));
7018 continue;
7019 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07007020
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007021 uintptr_t esize = dtohs(ent->size);
7022 if ((esize&0x3) != 0) {
7023 printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void *)esize);
7024 continue;
7025 }
7026 if ((thisOffset+esize) > typeSize) {
7027 printf("ResTable_entry OUT OF BOUNDS: 0x%x+0x%x+%p (size is 0x%x)\n",
7028 entriesStart, thisOffset, (void *)esize, typeSize);
7029 continue;
7030 }
7031
7032 const Res_value* valuePtr = NULL;
7033 const ResTable_map_entry* bagPtr = NULL;
7034 Res_value value;
7035 if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) {
7036 printf("<bag>");
7037 bagPtr = (const ResTable_map_entry*)ent;
7038 } else {
7039 valuePtr = (const Res_value*)
7040 (((const uint8_t*)ent) + esize);
7041 value.copyFrom_dtoh(*valuePtr);
7042 printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)",
7043 (int)value.dataType, (int)value.data,
7044 (int)value.size, (int)value.res0);
7045 }
7046
7047 if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) {
7048 printf(" (PUBLIC)");
7049 }
7050 printf("\n");
7051
7052 if (inclValues) {
7053 if (valuePtr != NULL) {
7054 printf(" ");
7055 print_value(typeConfigs->package, value);
7056 } else if (bagPtr != NULL) {
7057 const int N = dtohl(bagPtr->count);
7058 const uint8_t* baseMapPtr = (const uint8_t*)ent;
7059 size_t mapOffset = esize;
7060 const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
7061 const uint32_t parent = dtohl(bagPtr->parent.ident);
7062 uint32_t resolvedParent = parent;
Adam Lesinski6022deb2014-08-20 14:59:19 -07007063 if (Res_GETPACKAGE(resolvedParent) + 1 == 0) {
7064 status_t err = pg->dynamicRefTable.lookupResourceId(&resolvedParent);
7065 if (err != NO_ERROR) {
7066 resolvedParent = 0;
7067 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007068 }
7069 printf(" Parent=0x%08x(Resolved=0x%08x), Count=%d\n",
7070 parent, resolvedParent, N);
7071 for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) {
7072 printf(" #%i (Key=0x%08x): ",
7073 i, dtohl(mapPtr->name.ident));
7074 value.copyFrom_dtoh(mapPtr->value);
7075 print_value(typeConfigs->package, value);
7076 const size_t size = dtohs(mapPtr->value.size);
7077 mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value);
7078 mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
Dianne Hackborne17086b2009-06-19 15:13:28 -07007079 }
7080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 }
7082 }
7083 }
7084 }
7085 }
7086}
7087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088} // namespace android