blob: 1ccc59a6a348f0922c9ea94b1410ae8d6f6298a0 [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 {
Adam Lesinski1d7172e2016-03-30 16:22:33 -07003938#ifndef STATIC_ANDROIDFW_FOR_TOOLS
Steve Block8564c8d2012-01-05 23:22:43 +00003939 ALOGW("No known package when getting name for resource number 0x%08x", resID);
Adam Lesinski1d7172e2016-03-30 16:22:33 -07003940#endif
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 return false;
3943 }
3944 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003945 ALOGW("No type identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 return false;
3947 }
3948
3949 const PackageGroup* const grp = mPackageGroups[p];
3950 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003951 ALOGW("Bad identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 return false;
3953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003955 Entry entry;
3956 status_t err = getEntry(grp, t, e, NULL, &entry);
3957 if (err != NO_ERROR) {
3958 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003961 outName->package = grp->name.string();
3962 outName->packageLen = grp->name.size();
3963 if (allowUtf8) {
3964 outName->type8 = entry.typeStr.string8(&outName->typeLen);
3965 outName->name8 = entry.keyStr.string8(&outName->nameLen);
3966 } else {
3967 outName->type8 = NULL;
3968 outName->name8 = NULL;
3969 }
3970 if (outName->type8 == NULL) {
3971 outName->type = entry.typeStr.string16(&outName->typeLen);
3972 // If we have a bad index for some reason, we should abort.
3973 if (outName->type == NULL) {
3974 return false;
3975 }
3976 }
3977 if (outName->name8 == NULL) {
3978 outName->name = entry.keyStr.string16(&outName->nameLen);
3979 // If we have a bad index for some reason, we should abort.
3980 if (outName->name == NULL) {
3981 return false;
3982 }
3983 }
3984
3985 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986}
3987
Kenny Root55fc8502010-10-28 14:47:01 -07003988ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, uint16_t density,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 uint32_t* outSpecFlags, ResTable_config* outConfig) const
3990{
3991 if (mError != NO_ERROR) {
3992 return mError;
3993 }
3994
3995 const ssize_t p = getResourcePackageIndex(resID);
3996 const int t = Res_GETTYPE(resID);
3997 const int e = Res_GETENTRY(resID);
3998
3999 if (p < 0) {
Dianne Hackborn6cca1592009-09-20 12:40:03 -07004000 if (Res_GETPACKAGE(resID)+1 == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004001 ALOGW("No package identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07004002 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004003 ALOGW("No known package when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07004004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 return BAD_INDEX;
4006 }
4007 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004008 ALOGW("No type identifier when getting value for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 return BAD_INDEX;
4010 }
4011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 const PackageGroup* const grp = mPackageGroups[p];
4013 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00004014 ALOGW("Bad identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08004015 return BAD_INDEX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 }
Kenny Root55fc8502010-10-28 14:47:01 -07004017
4018 // Allow overriding density
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004019 ResTable_config desiredConfig = mParams;
Kenny Root55fc8502010-10-28 14:47:01 -07004020 if (density > 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004021 desiredConfig.density = density;
Kenny Root55fc8502010-10-28 14:47:01 -07004022 }
4023
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004024 Entry entry;
4025 status_t err = getEntry(grp, t, e, &desiredConfig, &entry);
4026 if (err != NO_ERROR) {
Adam Lesinskide7de472014-11-03 12:03:08 -08004027 // Only log the failure when we're not running on the host as
4028 // part of a tool. The caller will do its own logging.
4029#ifndef STATIC_ANDROIDFW_FOR_TOOLS
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004030 ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) (error %d)\n",
4031 resID, t, e, err);
Adam Lesinskide7de472014-11-03 12:03:08 -08004032#endif
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004033 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 }
4035
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004036 if ((dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) != 0) {
4037 if (!mayBeBag) {
4038 ALOGW("Requesting resource 0x%08x failed because it is complex\n", resID);
Adam Lesinskide898ff2014-01-29 18:20:45 -08004039 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004040 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 }
4042
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004043 const Res_value* value = reinterpret_cast<const Res_value*>(
4044 reinterpret_cast<const uint8_t*>(entry.entry) + entry.entry->size);
4045
4046 outValue->size = dtohs(value->size);
4047 outValue->res0 = value->res0;
4048 outValue->dataType = value->dataType;
4049 outValue->data = dtohl(value->data);
4050
4051 // The reference may be pointing to a resource in a shared library. These
4052 // references have build-time generated package IDs. These ids may not match
4053 // the actual package IDs of the corresponding packages in this ResTable.
4054 // We need to fix the package ID based on a mapping.
4055 if (grp->dynamicRefTable.lookupResourceValue(outValue) != NO_ERROR) {
4056 ALOGW("Failed to resolve referenced package: 0x%08x", outValue->data);
4057 return BAD_VALUE;
Kenny Root55fc8502010-10-28 14:47:01 -07004058 }
4059
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004060 if (kDebugTableNoisy) {
4061 size_t len;
4062 printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n",
4063 entry.package->header->index,
4064 outValue->dataType,
4065 outValue->dataType == Res_value::TYPE_STRING ?
4066 String8(entry.package->header->values.stringAt(outValue->data, &len)).string() :
4067 "",
4068 outValue->data);
4069 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004070
4071 if (outSpecFlags != NULL) {
4072 *outSpecFlags = entry.specFlags;
4073 }
4074
4075 if (outConfig != NULL) {
4076 *outConfig = entry.config;
4077 }
4078
4079 return entry.package->header->index;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080}
4081
4082ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex,
Dianne Hackborn0d221012009-07-29 15:41:19 -07004083 uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags,
4084 ResTable_config* outConfig) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085{
4086 int count=0;
Adam Lesinskide898ff2014-01-29 18:20:45 -08004087 while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE
4088 && value->data != 0 && count < 20) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 if (outLastRef) *outLastRef = value->data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 uint32_t newFlags = 0;
Kenny Root55fc8502010-10-28 14:47:01 -07004091 const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags,
Dianne Hackborn0d221012009-07-29 15:41:19 -07004092 outConfig);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08004093 if (newIndex == BAD_INDEX) {
4094 return BAD_INDEX;
4095 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004096 if (kDebugTableTheme) {
4097 ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n",
4098 value->data, (int)newIndex, (int)value->dataType, value->data);
4099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex);
4101 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags;
4102 if (newIndex < 0) {
4103 // This can fail if the resource being referenced is a style...
4104 // in this case, just return the reference, and expect the
4105 // caller to deal with.
4106 return blockIndex;
4107 }
4108 blockIndex = newIndex;
4109 count++;
4110 }
4111 return blockIndex;
4112}
4113
4114const char16_t* ResTable::valueToString(
4115 const Res_value* value, size_t stringBlock,
Adam Lesinskiad2d07d2014-08-27 16:21:08 -07004116 char16_t /*tmpBuffer*/ [TMP_BUFFER_SIZE], size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117{
4118 if (!value) {
4119 return NULL;
4120 }
4121 if (value->dataType == value->TYPE_STRING) {
4122 return getTableStringBlock(stringBlock)->stringAt(value->data, outLen);
4123 }
4124 // XXX do int to string conversions.
4125 return NULL;
4126}
4127
4128ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const
4129{
4130 mLock.lock();
4131 ssize_t err = getBagLocked(resID, outBag);
4132 if (err < NO_ERROR) {
4133 //printf("*** get failed! unlocking\n");
4134 mLock.unlock();
4135 }
4136 return err;
4137}
4138
Mark Salyzyn00adb862014-03-19 11:00:06 -07004139void ResTable::unlockBag(const bag_entry* /*bag*/) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140{
4141 //printf("<<< unlockBag %p\n", this);
4142 mLock.unlock();
4143}
4144
4145void ResTable::lock() const
4146{
4147 mLock.lock();
4148}
4149
4150void ResTable::unlock() const
4151{
4152 mLock.unlock();
4153}
4154
4155ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag,
4156 uint32_t* outTypeSpecFlags) const
4157{
4158 if (mError != NO_ERROR) {
4159 return mError;
4160 }
4161
4162 const ssize_t p = getResourcePackageIndex(resID);
4163 const int t = Res_GETTYPE(resID);
4164 const int e = Res_GETENTRY(resID);
4165
4166 if (p < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004167 ALOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 return BAD_INDEX;
4169 }
4170 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00004171 ALOGW("No type identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 return BAD_INDEX;
4173 }
4174
4175 //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t);
4176 PackageGroup* const grp = mPackageGroups[p];
4177 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00004178 ALOGW("Bad identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 return BAD_INDEX;
4180 }
4181
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004182 const TypeList& typeConfigs = grp->types[t];
4183 if (typeConfigs.isEmpty()) {
4184 ALOGW("Type identifier 0x%x does not exist.", t+1);
4185 return BAD_INDEX;
4186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004188 const size_t NENTRY = typeConfigs[0]->entryCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 if (e >= (int)NENTRY) {
Steve Block8564c8d2012-01-05 23:22:43 +00004190 ALOGW("Entry identifier 0x%x is larger than entry count 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004191 e, (int)typeConfigs[0]->entryCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 return BAD_INDEX;
4193 }
4194
4195 // First see if we've already computed this bag...
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004196 TypeCacheEntry& cacheEntry = grp->typeCacheEntries.editItemAt(t);
4197 bag_set** typeSet = cacheEntry.cachedBags;
4198 if (typeSet) {
4199 bag_set* set = typeSet[e];
4200 if (set) {
4201 if (set != (bag_set*)0xFFFFFFFF) {
4202 if (outTypeSpecFlags != NULL) {
4203 *outTypeSpecFlags = set->typeSpecFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004205 *outBag = (bag_entry*)(set+1);
4206 if (kDebugTableSuperNoisy) {
4207 ALOGI("Found existing bag for: 0x%x\n", resID);
4208 }
4209 return set->numAttrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004211 ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.",
4212 resID);
4213 return BAD_INDEX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
4215 }
4216
4217 // Bag not found, we need to compute it!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 if (!typeSet) {
Iliyan Malchev7e1d3952012-02-17 12:15:58 -08004219 typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 if (!typeSet) return NO_MEMORY;
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004221 cacheEntry.cachedBags = typeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
4223
4224 // Mark that we are currently working on this one.
4225 typeSet[e] = (bag_set*)0xFFFFFFFF;
4226
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004227 if (kDebugTableNoisy) {
4228 ALOGI("Building bag: %x\n", resID);
4229 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004230
4231 // Now collect all bag attributes
4232 Entry entry;
4233 status_t err = getEntry(grp, t, e, &mParams, &entry);
4234 if (err != NO_ERROR) {
4235 return err;
4236 }
4237
4238 const uint16_t entrySize = dtohs(entry.entry->size);
4239 const uint32_t parent = entrySize >= sizeof(ResTable_map_entry)
4240 ? dtohl(((const ResTable_map_entry*)entry.entry)->parent.ident) : 0;
4241 const uint32_t count = entrySize >= sizeof(ResTable_map_entry)
4242 ? dtohl(((const ResTable_map_entry*)entry.entry)->count) : 0;
4243
4244 size_t N = count;
4245
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004246 if (kDebugTableNoisy) {
4247 ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004248
4249 // If this map inherits from another, we need to start
4250 // with its parent's values. Otherwise start out empty.
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004251 ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent);
4252 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 // This is what we are building.
4255 bag_set* set = NULL;
4256
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004257 if (parent) {
4258 uint32_t resolvedParent = parent;
Mark Salyzyn00adb862014-03-19 11:00:06 -07004259
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004260 // Bags encode a parent reference without using the standard
4261 // Res_value structure. That means we must always try to
4262 // resolve a parent reference in case it is actually a
4263 // TYPE_DYNAMIC_REFERENCE.
4264 status_t err = grp->dynamicRefTable.lookupResourceId(&resolvedParent);
4265 if (err != NO_ERROR) {
4266 ALOGE("Failed resolving bag parent id 0x%08x", parent);
4267 return UNKNOWN_ERROR;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004270 const bag_entry* parentBag;
4271 uint32_t parentTypeSpecFlags = 0;
4272 const ssize_t NP = getBagLocked(resolvedParent, &parentBag, &parentTypeSpecFlags);
4273 const size_t NT = ((NP >= 0) ? NP : 0) + N;
4274 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT);
4275 if (set == NULL) {
4276 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004278 if (NP > 0) {
4279 memcpy(set+1, parentBag, NP*sizeof(bag_entry));
4280 set->numAttrs = NP;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004281 if (kDebugTableNoisy) {
4282 ALOGI("Initialized new bag with %zd inherited attributes.\n", NP);
4283 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004284 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004285 if (kDebugTableNoisy) {
4286 ALOGI("Initialized new bag with no inherited attributes.\n");
4287 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004288 set->numAttrs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004290 set->availAttrs = NT;
4291 set->typeSpecFlags = parentTypeSpecFlags;
4292 } else {
4293 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N);
4294 if (set == NULL) {
4295 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004297 set->numAttrs = 0;
4298 set->availAttrs = N;
4299 set->typeSpecFlags = 0;
4300 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004301
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004302 set->typeSpecFlags |= entry.specFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004304 // Now merge in the new attributes...
4305 size_t curOff = (reinterpret_cast<uintptr_t>(entry.entry) - reinterpret_cast<uintptr_t>(entry.type))
4306 + dtohs(entry.entry->size);
4307 const ResTable_map* map;
4308 bag_entry* entries = (bag_entry*)(set+1);
4309 size_t curEntry = 0;
4310 uint32_t pos = 0;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004311 if (kDebugTableNoisy) {
4312 ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs);
4313 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004314 while (pos < count) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004315 if (kDebugTableNoisy) {
4316 ALOGI("Now at %p\n", (void*)curOff);
4317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004319 if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) {
4320 ALOGW("ResTable_map at %d is beyond type chunk data %d",
4321 (int)curOff, dtohl(entry.type->header.size));
4322 return BAD_TYPE;
4323 }
4324 map = (const ResTable_map*)(((const uint8_t*)entry.type) + curOff);
4325 N++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -07004327 uint32_t newName = htodl(map->name.ident);
4328 if (!Res_INTERNALID(newName)) {
4329 // Attributes don't have a resource id as the name. They specify
4330 // other data, which would be wrong to change via a lookup.
4331 if (grp->dynamicRefTable.lookupResourceId(&newName) != NO_ERROR) {
4332 ALOGE("Failed resolving ResTable_map name at %d with ident 0x%08x",
4333 (int) curOff, (int) newName);
4334 return UNKNOWN_ERROR;
4335 }
4336 }
4337
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004338 bool isInside;
4339 uint32_t oldName = 0;
4340 while ((isInside=(curEntry < set->numAttrs))
4341 && (oldName=entries[curEntry].map.name.ident) < newName) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004342 if (kDebugTableNoisy) {
4343 ALOGI("#%zu: Keeping existing attribute: 0x%08x\n",
4344 curEntry, entries[curEntry].map.name.ident);
4345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 curEntry++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004348
4349 if ((!isInside) || oldName != newName) {
4350 // This is a new attribute... figure out what to do with it.
4351 if (set->numAttrs >= set->availAttrs) {
4352 // Need to alloc more memory...
4353 const size_t newAvail = set->availAttrs+N;
4354 set = (bag_set*)realloc(set,
4355 sizeof(bag_set)
4356 + sizeof(bag_entry)*newAvail);
4357 if (set == NULL) {
4358 return NO_MEMORY;
4359 }
4360 set->availAttrs = newAvail;
4361 entries = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004362 if (kDebugTableNoisy) {
4363 ALOGI("Reallocated set %p, entries=%p, avail=%zu\n",
4364 set, entries, set->availAttrs);
4365 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004366 }
4367 if (isInside) {
4368 // Going in the middle, need to make space.
4369 memmove(entries+curEntry+1, entries+curEntry,
4370 sizeof(bag_entry)*(set->numAttrs-curEntry));
4371 set->numAttrs++;
4372 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004373 if (kDebugTableNoisy) {
4374 ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName);
4375 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004376 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004377 if (kDebugTableNoisy) {
4378 ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName);
4379 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004380 }
4381
4382 bag_entry* cur = entries+curEntry;
4383
4384 cur->stringBlock = entry.package->header->index;
4385 cur->map.name.ident = newName;
4386 cur->map.value.copyFrom_dtoh(map->value);
4387 status_t err = grp->dynamicRefTable.lookupResourceValue(&cur->map.value);
4388 if (err != NO_ERROR) {
4389 ALOGE("Reference item(0x%08x) in bag could not be resolved.", cur->map.value.data);
4390 return UNKNOWN_ERROR;
4391 }
4392
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004393 if (kDebugTableNoisy) {
4394 ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n",
4395 curEntry, cur, cur->stringBlock, cur->map.name.ident,
4396 cur->map.value.dataType, cur->map.value.data);
4397 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004398
4399 // On to the next!
4400 curEntry++;
4401 pos++;
4402 const size_t size = dtohs(map->value.size);
4403 curOff += size + sizeof(*map)-sizeof(map->value);
4404 };
4405
4406 if (curEntry > set->numAttrs) {
4407 set->numAttrs = curEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409
4410 // And this is it...
4411 typeSet[e] = set;
4412 if (set) {
4413 if (outTypeSpecFlags != NULL) {
4414 *outTypeSpecFlags = set->typeSpecFlags;
4415 }
4416 *outBag = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004417 if (kDebugTableNoisy) {
4418 ALOGI("Returning %zu attrs\n", set->numAttrs);
4419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 return set->numAttrs;
4421 }
4422 return BAD_INDEX;
4423}
4424
4425void ResTable::setParameters(const ResTable_config* params)
4426{
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004427 AutoMutex _lock(mLock);
4428 AutoMutex _lock2(mFilteredConfigLock);
4429
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004430 if (kDebugTableGetEntry) {
4431 ALOGI("Setting parameters: %s\n", params->toString().string());
4432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 mParams = *params;
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004434 for (size_t p = 0; p < mPackageGroups.size(); p++) {
4435 PackageGroup* packageGroup = mPackageGroups.editItemAt(p);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004436 if (kDebugTableNoisy) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004437 ALOGI("CLEARING BAGS FOR GROUP %zu!", p);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004438 }
Adam Lesinskiff5808d2016-02-23 17:49:53 -08004439 packageGroup->clearBagCache();
4440
4441 // Find which configurations match the set of parameters. This allows for a much
4442 // faster lookup in getEntry() if the set of values is narrowed down.
4443 for (size_t t = 0; t < packageGroup->types.size(); t++) {
4444 if (packageGroup->types[t].isEmpty()) {
4445 continue;
4446 }
4447
4448 TypeList& typeList = packageGroup->types.editItemAt(t);
4449
4450 // Retrieve the cache entry for this type.
4451 TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries.editItemAt(t);
4452
4453 for (size_t ts = 0; ts < typeList.size(); ts++) {
4454 Type* type = typeList.editItemAt(ts);
4455
4456 std::shared_ptr<Vector<const ResTable_type*>> newFilteredConfigs =
4457 std::make_shared<Vector<const ResTable_type*>>();
4458
4459 for (size_t ti = 0; ti < type->configs.size(); ti++) {
4460 ResTable_config config;
4461 config.copyFromDtoH(type->configs[ti]->config);
4462
4463 if (config.match(mParams)) {
4464 newFilteredConfigs->add(type->configs[ti]);
4465 }
4466 }
4467
4468 if (kDebugTableNoisy) {
4469 ALOGD("Updating pkg=%zu type=%zu with %zu filtered configs",
4470 p, t, newFilteredConfigs->size());
4471 }
4472
4473 cacheEntry.filteredConfigs.add(newFilteredConfigs);
4474 }
4475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477}
4478
4479void ResTable::getParameters(ResTable_config* params) const
4480{
4481 mLock.lock();
4482 *params = mParams;
4483 mLock.unlock();
4484}
4485
4486struct id_name_map {
4487 uint32_t id;
4488 size_t len;
4489 char16_t name[6];
4490};
4491
4492const static id_name_map ID_NAMES[] = {
4493 { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } },
4494 { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } },
4495 { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } },
4496 { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } },
4497 { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } },
4498 { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } },
4499 { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } },
4500 { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } },
4501 { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } },
4502 { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } },
4503};
4504
4505uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen,
4506 const char16_t* type, size_t typeLen,
4507 const char16_t* package,
4508 size_t packageLen,
4509 uint32_t* outTypeSpecFlags) const
4510{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004511 if (kDebugTableSuperNoisy) {
4512 printf("Identifier for name: error=%d\n", mError);
4513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514
4515 // Check for internal resource identifier as the very first thing, so
4516 // that we will always find them even when there are no resources.
4517 if (name[0] == '^') {
4518 const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0]));
4519 size_t len;
4520 for (int i=0; i<N; i++) {
4521 const id_name_map* m = ID_NAMES + i;
4522 len = m->len;
4523 if (len != nameLen) {
4524 continue;
4525 }
4526 for (size_t j=1; j<len; j++) {
4527 if (m->name[j] != name[j]) {
4528 goto nope;
4529 }
4530 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004531 if (outTypeSpecFlags) {
4532 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 return m->id;
4535nope:
4536 ;
4537 }
4538 if (nameLen > 7) {
4539 if (name[1] == 'i' && name[2] == 'n'
4540 && name[3] == 'd' && name[4] == 'e' && name[5] == 'x'
4541 && name[6] == '_') {
4542 int index = atoi(String8(name + 7, nameLen - 7).string());
4543 if (Res_CHECKID(index)) {
Steve Block8564c8d2012-01-05 23:22:43 +00004544 ALOGW("Array resource index: %d is too large.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 index);
4546 return 0;
4547 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004548 if (outTypeSpecFlags) {
4549 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 return Res_MAKEARRAY(index);
4552 }
4553 }
4554 return 0;
4555 }
4556
4557 if (mError != NO_ERROR) {
4558 return 0;
4559 }
4560
Dianne Hackborn426431a2011-06-09 11:29:08 -07004561 bool fakePublic = false;
4562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 // Figure out the package and type we are looking in...
4564
4565 const char16_t* packageEnd = NULL;
4566 const char16_t* typeEnd = NULL;
4567 const char16_t* const nameEnd = name+nameLen;
4568 const char16_t* p = name;
4569 while (p < nameEnd) {
4570 if (*p == ':') packageEnd = p;
4571 else if (*p == '/') typeEnd = p;
4572 p++;
4573 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004574 if (*name == '@') {
4575 name++;
4576 if (*name == '*') {
4577 fakePublic = true;
4578 name++;
4579 }
4580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 if (name >= nameEnd) {
4582 return 0;
4583 }
4584
4585 if (packageEnd) {
4586 package = name;
4587 packageLen = packageEnd-name;
4588 name = packageEnd+1;
4589 } else if (!package) {
4590 return 0;
4591 }
4592
4593 if (typeEnd) {
4594 type = name;
4595 typeLen = typeEnd-name;
4596 name = typeEnd+1;
4597 } else if (!type) {
4598 return 0;
4599 }
4600
4601 if (name >= nameEnd) {
4602 return 0;
4603 }
4604 nameLen = nameEnd-name;
4605
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004606 if (kDebugTableNoisy) {
4607 printf("Looking for identifier: type=%s, name=%s, package=%s\n",
4608 String8(type, typeLen).string(),
4609 String8(name, nameLen).string(),
4610 String8(package, packageLen).string());
4611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612
Adam Lesinski9b624c12014-11-19 17:49:26 -08004613 const String16 attr("attr");
4614 const String16 attrPrivate("^attr-private");
4615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 const size_t NG = mPackageGroups.size();
4617 for (size_t ig=0; ig<NG; ig++) {
4618 const PackageGroup* group = mPackageGroups[ig];
4619
4620 if (strzcmp16(package, packageLen,
4621 group->name.string(), group->name.size())) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004622 if (kDebugTableNoisy) {
4623 printf("Skipping package group: %s\n", String8(group->name).string());
4624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 continue;
4626 }
4627
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004628 const size_t packageCount = group->packages.size();
4629 for (size_t pi = 0; pi < packageCount; pi++) {
Adam Lesinski9b624c12014-11-19 17:49:26 -08004630 const char16_t* targetType = type;
4631 size_t targetTypeLen = typeLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632
Adam Lesinski9b624c12014-11-19 17:49:26 -08004633 do {
4634 ssize_t ti = group->packages[pi]->typeStrings.indexOfString(
4635 targetType, targetTypeLen);
4636 if (ti < 0) {
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004637 continue;
4638 }
4639
Adam Lesinski9b624c12014-11-19 17:49:26 -08004640 ti += group->packages[pi]->typeIdOffset;
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004641
Adam Lesinski9b624c12014-11-19 17:49:26 -08004642 const uint32_t identifier = findEntry(group, ti, name, nameLen,
4643 outTypeSpecFlags);
4644 if (identifier != 0) {
4645 if (fakePublic && outTypeSpecFlags) {
4646 *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004647 }
Adam Lesinski9b624c12014-11-19 17:49:26 -08004648 return identifier;
4649 }
4650 } while (strzcmp16(attr.string(), attr.size(), targetType, targetTypeLen) == 0
4651 && (targetType = attrPrivate.string())
4652 && (targetTypeLen = attrPrivate.size())
4653 );
4654 }
4655 break;
4656 }
4657 return 0;
4658}
4659
4660uint32_t ResTable::findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name,
4661 size_t nameLen, uint32_t* outTypeSpecFlags) const {
4662 const TypeList& typeList = group->types[typeIndex];
4663 const size_t typeCount = typeList.size();
4664 for (size_t i = 0; i < typeCount; i++) {
4665 const Type* t = typeList[i];
4666 const ssize_t ei = t->package->keyStrings.indexOfString(name, nameLen);
4667 if (ei < 0) {
4668 continue;
4669 }
4670
4671 const size_t configCount = t->configs.size();
4672 for (size_t j = 0; j < configCount; j++) {
4673 const TypeVariant tv(t->configs[j]);
4674 for (TypeVariant::iterator iter = tv.beginEntries();
4675 iter != tv.endEntries();
4676 iter++) {
4677 const ResTable_entry* entry = *iter;
4678 if (entry == NULL) {
4679 continue;
4680 }
4681
4682 if (dtohl(entry->key.index) == (size_t) ei) {
4683 uint32_t resId = Res_MAKEID(group->id - 1, typeIndex, iter.index());
4684 if (outTypeSpecFlags) {
4685 Entry result;
4686 if (getEntry(group, typeIndex, iter.index(), NULL, &result) != NO_ERROR) {
4687 ALOGW("Failed to find spec flags for 0x%08x", resId);
4688 return 0;
4689 }
4690 *outTypeSpecFlags = result.specFlags;
4691 }
4692 return resId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 }
4694 }
4695 }
4696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 return 0;
4698}
4699
Dan Albertf348c152014-09-08 18:28:00 -07004700bool ResTable::expandResourceRef(const char16_t* refStr, size_t refLen,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 String16* outPackage,
4702 String16* outType,
4703 String16* outName,
4704 const String16* defType,
4705 const String16* defPackage,
Dianne Hackborn426431a2011-06-09 11:29:08 -07004706 const char** outErrorMsg,
4707 bool* outPublicOnly)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004708{
4709 const char16_t* packageEnd = NULL;
4710 const char16_t* typeEnd = NULL;
4711 const char16_t* p = refStr;
4712 const char16_t* const end = p + refLen;
4713 while (p < end) {
4714 if (*p == ':') packageEnd = p;
4715 else if (*p == '/') {
4716 typeEnd = p;
4717 break;
4718 }
4719 p++;
4720 }
4721 p = refStr;
4722 if (*p == '@') p++;
4723
Dianne Hackborn426431a2011-06-09 11:29:08 -07004724 if (outPublicOnly != NULL) {
4725 *outPublicOnly = true;
4726 }
4727 if (*p == '*') {
4728 p++;
4729 if (outPublicOnly != NULL) {
4730 *outPublicOnly = false;
4731 }
4732 }
4733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 if (packageEnd) {
4735 *outPackage = String16(p, packageEnd-p);
4736 p = packageEnd+1;
4737 } else {
4738 if (!defPackage) {
4739 if (outErrorMsg) {
4740 *outErrorMsg = "No resource package specified";
4741 }
4742 return false;
4743 }
4744 *outPackage = *defPackage;
4745 }
4746 if (typeEnd) {
4747 *outType = String16(p, typeEnd-p);
4748 p = typeEnd+1;
4749 } else {
4750 if (!defType) {
4751 if (outErrorMsg) {
4752 *outErrorMsg = "No resource type specified";
4753 }
4754 return false;
4755 }
4756 *outType = *defType;
4757 }
4758 *outName = String16(p, end-p);
Konstantin Lopyrevddcafcb2010-06-04 14:36:49 -07004759 if(**outPackage == 0) {
4760 if(outErrorMsg) {
4761 *outErrorMsg = "Resource package cannot be an empty string";
4762 }
4763 return false;
4764 }
4765 if(**outType == 0) {
4766 if(outErrorMsg) {
4767 *outErrorMsg = "Resource type cannot be an empty string";
4768 }
4769 return false;
4770 }
4771 if(**outName == 0) {
4772 if(outErrorMsg) {
4773 *outErrorMsg = "Resource id cannot be an empty string";
4774 }
4775 return false;
4776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 return true;
4778}
4779
4780static uint32_t get_hex(char c, bool* outError)
4781{
4782 if (c >= '0' && c <= '9') {
4783 return c - '0';
4784 } else if (c >= 'a' && c <= 'f') {
4785 return c - 'a' + 0xa;
4786 } else if (c >= 'A' && c <= 'F') {
4787 return c - 'A' + 0xa;
4788 }
4789 *outError = true;
4790 return 0;
4791}
4792
4793struct unit_entry
4794{
4795 const char* name;
4796 size_t len;
4797 uint8_t type;
4798 uint32_t unit;
4799 float scale;
4800};
4801
4802static const unit_entry unitNames[] = {
4803 { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f },
4804 { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4805 { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4806 { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f },
4807 { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f },
4808 { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f },
4809 { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f },
4810 { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 },
4811 { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 },
4812 { NULL, 0, 0, 0, 0 }
4813};
4814
4815static bool parse_unit(const char* str, Res_value* outValue,
4816 float* outScale, const char** outEnd)
4817{
4818 const char* end = str;
4819 while (*end != 0 && !isspace((unsigned char)*end)) {
4820 end++;
4821 }
4822 const size_t len = end-str;
4823
4824 const char* realEnd = end;
4825 while (*realEnd != 0 && isspace((unsigned char)*realEnd)) {
4826 realEnd++;
4827 }
4828 if (*realEnd != 0) {
4829 return false;
4830 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 const unit_entry* cur = unitNames;
4833 while (cur->name) {
4834 if (len == cur->len && strncmp(cur->name, str, len) == 0) {
4835 outValue->dataType = cur->type;
4836 outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT;
4837 *outScale = cur->scale;
4838 *outEnd = end;
4839 //printf("Found unit %s for %s\n", cur->name, str);
4840 return true;
4841 }
4842 cur++;
4843 }
4844
4845 return false;
4846}
4847
Dan Albert1b4f3162015-04-07 18:43:15 -07004848bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849{
4850 while (len > 0 && isspace16(*s)) {
4851 s++;
4852 len--;
4853 }
4854
4855 if (len <= 0) {
4856 return false;
4857 }
4858
4859 size_t i = 0;
Dan Albert1b4f3162015-04-07 18:43:15 -07004860 int64_t val = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 bool neg = false;
4862
4863 if (*s == '-') {
4864 neg = true;
4865 i++;
4866 }
4867
4868 if (s[i] < '0' || s[i] > '9') {
4869 return false;
4870 }
4871
Dan Albert1b4f3162015-04-07 18:43:15 -07004872 static_assert(std::is_same<uint32_t, Res_value::data_type>::value,
4873 "Res_value::data_type has changed. The range checks in this "
4874 "function are no longer correct.");
4875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 // Decimal or hex?
Dan Albert1b4f3162015-04-07 18:43:15 -07004877 bool isHex;
4878 if (len > 1 && s[i] == '0' && s[i+1] == 'x') {
4879 isHex = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 i += 2;
Dan Albert1b4f3162015-04-07 18:43:15 -07004881
4882 if (neg) {
4883 return false;
4884 }
4885
4886 if (i == len) {
4887 // Just u"0x"
4888 return false;
4889 }
4890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 bool error = false;
4892 while (i < len && !error) {
4893 val = (val*16) + get_hex(s[i], &error);
4894 i++;
Dan Albert1b4f3162015-04-07 18:43:15 -07004895
4896 if (val > std::numeric_limits<uint32_t>::max()) {
4897 return false;
4898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 }
4900 if (error) {
4901 return false;
4902 }
4903 } else {
Dan Albert1b4f3162015-04-07 18:43:15 -07004904 isHex = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 while (i < len) {
4906 if (s[i] < '0' || s[i] > '9') {
4907 return false;
4908 }
4909 val = (val*10) + s[i]-'0';
4910 i++;
Dan Albert1b4f3162015-04-07 18:43:15 -07004911
4912 if ((neg && -val < std::numeric_limits<int32_t>::min()) ||
4913 (!neg && val > std::numeric_limits<int32_t>::max())) {
4914 return false;
4915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 }
4917 }
4918
4919 if (neg) val = -val;
4920
4921 while (i < len && isspace16(s[i])) {
4922 i++;
4923 }
4924
Dan Albert1b4f3162015-04-07 18:43:15 -07004925 if (i != len) {
4926 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 }
4928
Dan Albert1b4f3162015-04-07 18:43:15 -07004929 if (outValue) {
4930 outValue->dataType =
4931 isHex ? outValue->TYPE_INT_HEX : outValue->TYPE_INT_DEC;
4932 outValue->data = static_cast<Res_value::data_type>(val);
4933 }
4934 return true;
4935}
4936
4937bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue)
4938{
4939 return U16StringToInt(s, len, outValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940}
4941
4942bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue)
4943{
4944 while (len > 0 && isspace16(*s)) {
4945 s++;
4946 len--;
4947 }
4948
4949 if (len <= 0) {
4950 return false;
4951 }
4952
4953 char buf[128];
4954 int i=0;
4955 while (len > 0 && *s != 0 && i < 126) {
4956 if (*s > 255) {
4957 return false;
4958 }
4959 buf[i++] = *s++;
4960 len--;
4961 }
4962
4963 if (len > 0) {
4964 return false;
4965 }
Torne (Richard Coles)46a807f2014-08-27 12:36:44 +01004966 if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.' && buf[0] != '-' && buf[0] != '+') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 return false;
4968 }
4969
4970 buf[i] = 0;
4971 const char* end;
4972 float f = strtof(buf, (char**)&end);
4973
4974 if (*end != 0 && !isspace((unsigned char)*end)) {
4975 // Might be a unit...
4976 float scale;
4977 if (parse_unit(end, outValue, &scale, &end)) {
4978 f *= scale;
4979 const bool neg = f < 0;
4980 if (neg) f = -f;
4981 uint64_t bits = (uint64_t)(f*(1<<23)+.5f);
4982 uint32_t radix;
4983 uint32_t shift;
4984 if ((bits&0x7fffff) == 0) {
4985 // Always use 23p0 if there is no fraction, just to make
4986 // things easier to read.
4987 radix = Res_value::COMPLEX_RADIX_23p0;
4988 shift = 23;
4989 } else if ((bits&0xffffffffff800000LL) == 0) {
4990 // Magnitude is zero -- can fit in 0 bits of precision.
4991 radix = Res_value::COMPLEX_RADIX_0p23;
4992 shift = 0;
4993 } else if ((bits&0xffffffff80000000LL) == 0) {
4994 // Magnitude can fit in 8 bits of precision.
4995 radix = Res_value::COMPLEX_RADIX_8p15;
4996 shift = 8;
4997 } else if ((bits&0xffffff8000000000LL) == 0) {
4998 // Magnitude can fit in 16 bits of precision.
4999 radix = Res_value::COMPLEX_RADIX_16p7;
5000 shift = 16;
5001 } else {
5002 // Magnitude needs entire range, so no fractional part.
5003 radix = Res_value::COMPLEX_RADIX_23p0;
5004 shift = 23;
5005 }
5006 int32_t mantissa = (int32_t)(
5007 (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK);
5008 if (neg) {
5009 mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK;
5010 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005011 outValue->data |=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 (radix<<Res_value::COMPLEX_RADIX_SHIFT)
5013 | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT);
5014 //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n",
5015 // f * (neg ? -1 : 1), bits, f*(1<<23),
5016 // radix, shift, outValue->data);
5017 return true;
5018 }
5019 return false;
5020 }
5021
5022 while (*end != 0 && isspace((unsigned char)*end)) {
5023 end++;
5024 }
5025
5026 if (*end == 0) {
5027 if (outValue) {
5028 outValue->dataType = outValue->TYPE_FLOAT;
5029 *(float*)(&outValue->data) = f;
5030 return true;
5031 }
5032 }
5033
5034 return false;
5035}
5036
5037bool ResTable::stringToValue(Res_value* outValue, String16* outString,
5038 const char16_t* s, size_t len,
5039 bool preserveSpaces, bool coerceType,
5040 uint32_t attrID,
5041 const String16* defType,
5042 const String16* defPackage,
5043 Accessor* accessor,
5044 void* accessorCookie,
5045 uint32_t attrType,
5046 bool enforcePrivate) const
5047{
5048 bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting();
5049 const char* errorMsg = NULL;
5050
5051 outValue->size = sizeof(Res_value);
5052 outValue->res0 = 0;
5053
5054 // First strip leading/trailing whitespace. Do this before handling
5055 // escapes, so they can be used to force whitespace into the string.
5056 if (!preserveSpaces) {
5057 while (len > 0 && isspace16(*s)) {
5058 s++;
5059 len--;
5060 }
5061 while (len > 0 && isspace16(s[len-1])) {
5062 len--;
5063 }
5064 // If the string ends with '\', then we keep the space after it.
5065 if (len > 0 && s[len-1] == '\\' && s[len] != 0) {
5066 len++;
5067 }
5068 }
5069
5070 //printf("Value for: %s\n", String8(s, len).string());
5071
5072 uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED;
5073 uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff;
5074 bool fromAccessor = false;
5075 if (attrID != 0 && !Res_INTERNALID(attrID)) {
5076 const ssize_t p = getResourcePackageIndex(attrID);
5077 const bag_entry* bag;
5078 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5079 //printf("For attr 0x%08x got bag of %d\n", attrID, cnt);
5080 if (cnt >= 0) {
5081 while (cnt > 0) {
5082 //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data);
5083 switch (bag->map.name.ident) {
5084 case ResTable_map::ATTR_TYPE:
5085 attrType = bag->map.value.data;
5086 break;
5087 case ResTable_map::ATTR_MIN:
5088 attrMin = bag->map.value.data;
5089 break;
5090 case ResTable_map::ATTR_MAX:
5091 attrMax = bag->map.value.data;
5092 break;
5093 case ResTable_map::ATTR_L10N:
5094 l10nReq = bag->map.value.data;
5095 break;
5096 }
5097 bag++;
5098 cnt--;
5099 }
5100 unlockBag(bag);
5101 } else if (accessor && accessor->getAttributeType(attrID, &attrType)) {
5102 fromAccessor = true;
5103 if (attrType == ResTable_map::TYPE_ENUM
5104 || attrType == ResTable_map::TYPE_FLAGS
5105 || attrType == ResTable_map::TYPE_INTEGER) {
5106 accessor->getAttributeMin(attrID, &attrMin);
5107 accessor->getAttributeMax(attrID, &attrMax);
5108 }
5109 if (localizationSetting) {
5110 l10nReq = accessor->getAttributeL10N(attrID);
5111 }
5112 }
5113 }
5114
5115 const bool canStringCoerce =
5116 coerceType && (attrType&ResTable_map::TYPE_STRING) != 0;
5117
5118 if (*s == '@') {
5119 outValue->dataType = outValue->TYPE_REFERENCE;
5120
5121 // Note: we don't check attrType here because the reference can
5122 // be to any other type; we just need to count on the client making
5123 // sure the referenced type is correct.
Mark Salyzyn00adb862014-03-19 11:00:06 -07005124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 //printf("Looking up ref: %s\n", String8(s, len).string());
5126
5127 // It's a reference!
5128 if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') {
Alan Viverettef2969402014-10-29 17:09:36 -07005129 // Special case @null as undefined. This will be converted by
5130 // AssetManager to TYPE_NULL with data DATA_NULL_UNDEFINED.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 outValue->data = 0;
5132 return true;
Alan Viverettef2969402014-10-29 17:09:36 -07005133 } else if (len == 6 && s[1]=='e' && s[2]=='m' && s[3]=='p' && s[4]=='t' && s[5]=='y') {
5134 // Special case @empty as explicitly defined empty value.
5135 outValue->dataType = Res_value::TYPE_NULL;
5136 outValue->data = Res_value::DATA_NULL_EMPTY;
5137 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 } else {
5139 bool createIfNotFound = false;
5140 const char16_t* resourceRefName;
5141 int resourceNameLen;
5142 if (len > 2 && s[1] == '+') {
5143 createIfNotFound = true;
5144 resourceRefName = s + 2;
5145 resourceNameLen = len - 2;
5146 } else if (len > 2 && s[1] == '*') {
5147 enforcePrivate = false;
5148 resourceRefName = s + 2;
5149 resourceNameLen = len - 2;
5150 } else {
5151 createIfNotFound = false;
5152 resourceRefName = s + 1;
5153 resourceNameLen = len - 1;
5154 }
5155 String16 package, type, name;
5156 if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name,
5157 defType, defPackage, &errorMsg)) {
5158 if (accessor != NULL) {
5159 accessor->reportError(accessorCookie, errorMsg);
5160 }
5161 return false;
5162 }
5163
5164 uint32_t specFlags = 0;
5165 uint32_t rid = identifierForName(name.string(), name.size(), type.string(),
5166 type.size(), package.string(), package.size(), &specFlags);
5167 if (rid != 0) {
5168 if (enforcePrivate) {
Adam Lesinski833f3cc2014-06-18 15:06:01 -07005169 if (accessor == NULL || accessor->getAssetsPackage() != package) {
5170 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5171 if (accessor != NULL) {
5172 accessor->reportError(accessorCookie, "Resource is not public.");
5173 }
5174 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 }
5177 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005178
5179 if (accessor) {
5180 rid = Res_MAKEID(
5181 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5182 Res_GETTYPE(rid), Res_GETENTRY(rid));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005183 if (kDebugTableNoisy) {
5184 ALOGI("Incl %s:%s/%s: 0x%08x\n",
5185 String8(package).string(), String8(type).string(),
5186 String8(name).string(), rid);
5187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005189
5190 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5191 if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) {
5192 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 outValue->data = rid;
5195 return true;
5196 }
5197
5198 if (accessor) {
5199 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name,
5200 createIfNotFound);
5201 if (rid != 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005202 if (kDebugTableNoisy) {
5203 ALOGI("Pckg %s:%s/%s: 0x%08x\n",
5204 String8(package).string(), String8(type).string(),
5205 String8(name).string(), rid);
5206 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08005207 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5208 if (packageId == 0x00) {
5209 outValue->data = rid;
5210 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5211 return true;
5212 } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) {
5213 // We accept packageId's generated as 0x01 in order to support
5214 // building the android system resources
5215 outValue->data = rid;
5216 return true;
5217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 }
5219 }
5220 }
5221
5222 if (accessor != NULL) {
5223 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5224 }
5225 return false;
5226 }
5227
5228 // if we got to here, and localization is required and it's not a reference,
5229 // complain and bail.
5230 if (l10nReq == ResTable_map::L10N_SUGGESTED) {
5231 if (localizationSetting) {
5232 if (accessor != NULL) {
5233 accessor->reportError(accessorCookie, "This attribute must be localized.");
5234 }
5235 }
5236 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 if (*s == '#') {
5239 // It's a color! Convert to an integer of the form 0xaarrggbb.
5240 uint32_t color = 0;
5241 bool error = false;
5242 if (len == 4) {
5243 outValue->dataType = outValue->TYPE_INT_COLOR_RGB4;
5244 color |= 0xFF000000;
5245 color |= get_hex(s[1], &error) << 20;
5246 color |= get_hex(s[1], &error) << 16;
5247 color |= get_hex(s[2], &error) << 12;
5248 color |= get_hex(s[2], &error) << 8;
5249 color |= get_hex(s[3], &error) << 4;
5250 color |= get_hex(s[3], &error);
5251 } else if (len == 5) {
5252 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4;
5253 color |= get_hex(s[1], &error) << 28;
5254 color |= get_hex(s[1], &error) << 24;
5255 color |= get_hex(s[2], &error) << 20;
5256 color |= get_hex(s[2], &error) << 16;
5257 color |= get_hex(s[3], &error) << 12;
5258 color |= get_hex(s[3], &error) << 8;
5259 color |= get_hex(s[4], &error) << 4;
5260 color |= get_hex(s[4], &error);
5261 } else if (len == 7) {
5262 outValue->dataType = outValue->TYPE_INT_COLOR_RGB8;
5263 color |= 0xFF000000;
5264 color |= get_hex(s[1], &error) << 20;
5265 color |= get_hex(s[2], &error) << 16;
5266 color |= get_hex(s[3], &error) << 12;
5267 color |= get_hex(s[4], &error) << 8;
5268 color |= get_hex(s[5], &error) << 4;
5269 color |= get_hex(s[6], &error);
5270 } else if (len == 9) {
5271 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8;
5272 color |= get_hex(s[1], &error) << 28;
5273 color |= get_hex(s[2], &error) << 24;
5274 color |= get_hex(s[3], &error) << 20;
5275 color |= get_hex(s[4], &error) << 16;
5276 color |= get_hex(s[5], &error) << 12;
5277 color |= get_hex(s[6], &error) << 8;
5278 color |= get_hex(s[7], &error) << 4;
5279 color |= get_hex(s[8], &error);
5280 } else {
5281 error = true;
5282 }
5283 if (!error) {
5284 if ((attrType&ResTable_map::TYPE_COLOR) == 0) {
5285 if (!canStringCoerce) {
5286 if (accessor != NULL) {
5287 accessor->reportError(accessorCookie,
5288 "Color types not allowed");
5289 }
5290 return false;
5291 }
5292 } else {
5293 outValue->data = color;
5294 //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color);
5295 return true;
5296 }
5297 } else {
5298 if ((attrType&ResTable_map::TYPE_COLOR) != 0) {
5299 if (accessor != NULL) {
5300 accessor->reportError(accessorCookie, "Color value not valid --"
5301 " must be #rgb, #argb, #rrggbb, or #aarrggbb");
5302 }
5303 #if 0
5304 fprintf(stderr, "%s: Color ID %s value %s is not valid\n",
5305 "Resource File", //(const char*)in->getPrintableSource(),
5306 String8(*curTag).string(),
5307 String8(s, len).string());
5308 #endif
5309 return false;
5310 }
5311 }
5312 }
5313
5314 if (*s == '?') {
5315 outValue->dataType = outValue->TYPE_ATTRIBUTE;
5316
5317 // Note: we don't check attrType here because the reference can
5318 // be to any other type; we just need to count on the client making
5319 // sure the referenced type is correct.
5320
5321 //printf("Looking up attr: %s\n", String8(s, len).string());
5322
5323 static const String16 attr16("attr");
5324 String16 package, type, name;
5325 if (!expandResourceRef(s+1, len-1, &package, &type, &name,
5326 &attr16, defPackage, &errorMsg)) {
5327 if (accessor != NULL) {
5328 accessor->reportError(accessorCookie, errorMsg);
5329 }
5330 return false;
5331 }
5332
5333 //printf("Pkg: %s, Type: %s, Name: %s\n",
5334 // String8(package).string(), String8(type).string(),
5335 // String8(name).string());
5336 uint32_t specFlags = 0;
Mark Salyzyn00adb862014-03-19 11:00:06 -07005337 uint32_t rid =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 identifierForName(name.string(), name.size(),
5339 type.string(), type.size(),
5340 package.string(), package.size(), &specFlags);
5341 if (rid != 0) {
5342 if (enforcePrivate) {
5343 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5344 if (accessor != NULL) {
5345 accessor->reportError(accessorCookie, "Attribute is not public.");
5346 }
5347 return false;
5348 }
5349 }
5350 if (!accessor) {
5351 outValue->data = rid;
5352 return true;
5353 }
5354 rid = Res_MAKEID(
5355 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5356 Res_GETTYPE(rid), Res_GETENTRY(rid));
5357 //printf("Incl %s:%s/%s: 0x%08x\n",
5358 // String8(package).string(), String8(type).string(),
5359 // String8(name).string(), rid);
5360 outValue->data = rid;
5361 return true;
5362 }
5363
5364 if (accessor) {
5365 uint32_t rid = accessor->getCustomResource(package, type, name);
5366 if (rid != 0) {
5367 //printf("Mine %s:%s/%s: 0x%08x\n",
5368 // String8(package).string(), String8(type).string(),
5369 // String8(name).string(), rid);
5370 outValue->data = rid;
5371 return true;
5372 }
5373 }
5374
5375 if (accessor != NULL) {
5376 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5377 }
5378 return false;
5379 }
5380
5381 if (stringToInt(s, len, outValue)) {
5382 if ((attrType&ResTable_map::TYPE_INTEGER) == 0) {
5383 // If this type does not allow integers, but does allow floats,
5384 // fall through on this error case because the float type should
5385 // be able to accept any integer value.
5386 if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) {
5387 if (accessor != NULL) {
5388 accessor->reportError(accessorCookie, "Integer types not allowed");
5389 }
5390 return false;
5391 }
5392 } else {
5393 if (((int32_t)outValue->data) < ((int32_t)attrMin)
5394 || ((int32_t)outValue->data) > ((int32_t)attrMax)) {
5395 if (accessor != NULL) {
5396 accessor->reportError(accessorCookie, "Integer value out of range");
5397 }
5398 return false;
5399 }
5400 return true;
5401 }
5402 }
5403
5404 if (stringToFloat(s, len, outValue)) {
5405 if (outValue->dataType == Res_value::TYPE_DIMENSION) {
5406 if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) {
5407 return true;
5408 }
5409 if (!canStringCoerce) {
5410 if (accessor != NULL) {
5411 accessor->reportError(accessorCookie, "Dimension types not allowed");
5412 }
5413 return false;
5414 }
5415 } else if (outValue->dataType == Res_value::TYPE_FRACTION) {
5416 if ((attrType&ResTable_map::TYPE_FRACTION) != 0) {
5417 return true;
5418 }
5419 if (!canStringCoerce) {
5420 if (accessor != NULL) {
5421 accessor->reportError(accessorCookie, "Fraction types not allowed");
5422 }
5423 return false;
5424 }
5425 } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) {
5426 if (!canStringCoerce) {
5427 if (accessor != NULL) {
5428 accessor->reportError(accessorCookie, "Float types not allowed");
5429 }
5430 return false;
5431 }
5432 } else {
5433 return true;
5434 }
5435 }
5436
5437 if (len == 4) {
5438 if ((s[0] == 't' || s[0] == 'T') &&
5439 (s[1] == 'r' || s[1] == 'R') &&
5440 (s[2] == 'u' || s[2] == 'U') &&
5441 (s[3] == 'e' || s[3] == 'E')) {
5442 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5443 if (!canStringCoerce) {
5444 if (accessor != NULL) {
5445 accessor->reportError(accessorCookie, "Boolean types not allowed");
5446 }
5447 return false;
5448 }
5449 } else {
5450 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5451 outValue->data = (uint32_t)-1;
5452 return true;
5453 }
5454 }
5455 }
5456
5457 if (len == 5) {
5458 if ((s[0] == 'f' || s[0] == 'F') &&
5459 (s[1] == 'a' || s[1] == 'A') &&
5460 (s[2] == 'l' || s[2] == 'L') &&
5461 (s[3] == 's' || s[3] == 'S') &&
5462 (s[4] == 'e' || s[4] == 'E')) {
5463 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5464 if (!canStringCoerce) {
5465 if (accessor != NULL) {
5466 accessor->reportError(accessorCookie, "Boolean types not allowed");
5467 }
5468 return false;
5469 }
5470 } else {
5471 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5472 outValue->data = 0;
5473 return true;
5474 }
5475 }
5476 }
5477
5478 if ((attrType&ResTable_map::TYPE_ENUM) != 0) {
5479 const ssize_t p = getResourcePackageIndex(attrID);
5480 const bag_entry* bag;
5481 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5482 //printf("Got %d for enum\n", cnt);
5483 if (cnt >= 0) {
5484 resource_name rname;
5485 while (cnt > 0) {
5486 if (!Res_INTERNALID(bag->map.name.ident)) {
5487 //printf("Trying attr #%08x\n", bag->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005488 if (getResourceName(bag->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 #if 0
5490 printf("Matching %s against %s (0x%08x)\n",
5491 String8(s, len).string(),
5492 String8(rname.name, rname.nameLen).string(),
5493 bag->map.name.ident);
5494 #endif
5495 if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) {
5496 outValue->dataType = bag->map.value.dataType;
5497 outValue->data = bag->map.value.data;
5498 unlockBag(bag);
5499 return true;
5500 }
5501 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 }
5504 bag++;
5505 cnt--;
5506 }
5507 unlockBag(bag);
5508 }
5509
5510 if (fromAccessor) {
5511 if (accessor->getAttributeEnum(attrID, s, len, outValue)) {
5512 return true;
5513 }
5514 }
5515 }
5516
5517 if ((attrType&ResTable_map::TYPE_FLAGS) != 0) {
5518 const ssize_t p = getResourcePackageIndex(attrID);
5519 const bag_entry* bag;
5520 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5521 //printf("Got %d for flags\n", cnt);
5522 if (cnt >= 0) {
5523 bool failed = false;
5524 resource_name rname;
5525 outValue->dataType = Res_value::TYPE_INT_HEX;
5526 outValue->data = 0;
5527 const char16_t* end = s + len;
5528 const char16_t* pos = s;
5529 while (pos < end && !failed) {
5530 const char16_t* start = pos;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005531 pos++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 while (pos < end && *pos != '|') {
5533 pos++;
5534 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08005535 //printf("Looking for: %s\n", String8(start, pos-start).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 const bag_entry* bagi = bag;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005537 ssize_t i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 for (i=0; i<cnt; i++, bagi++) {
5539 if (!Res_INTERNALID(bagi->map.name.ident)) {
5540 //printf("Trying attr #%08x\n", bagi->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005541 if (getResourceName(bagi->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 #if 0
5543 printf("Matching %s against %s (0x%08x)\n",
5544 String8(start,pos-start).string(),
5545 String8(rname.name, rname.nameLen).string(),
5546 bagi->map.name.ident);
5547 #endif
5548 if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) {
5549 outValue->data |= bagi->map.value.data;
5550 break;
5551 }
5552 }
5553 }
5554 }
5555 if (i >= cnt) {
5556 // Didn't find this flag identifier.
5557 failed = true;
5558 }
5559 if (pos < end) {
5560 pos++;
5561 }
5562 }
5563 unlockBag(bag);
5564 if (!failed) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005565 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 return true;
5567 }
5568 }
5569
5570
5571 if (fromAccessor) {
5572 if (accessor->getAttributeFlags(attrID, s, len, outValue)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005573 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 return true;
5575 }
5576 }
5577 }
5578
5579 if ((attrType&ResTable_map::TYPE_STRING) == 0) {
5580 if (accessor != NULL) {
5581 accessor->reportError(accessorCookie, "String types not allowed");
5582 }
5583 return false;
5584 }
5585
5586 // Generic string handling...
5587 outValue->dataType = outValue->TYPE_STRING;
5588 if (outString) {
5589 bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg);
5590 if (accessor != NULL) {
5591 accessor->reportError(accessorCookie, errorMsg);
5592 }
5593 return failed;
5594 }
5595
5596 return true;
5597}
5598
5599bool ResTable::collectString(String16* outString,
5600 const char16_t* s, size_t len,
5601 bool preserveSpaces,
5602 const char** outErrorMsg,
5603 bool append)
5604{
5605 String16 tmp;
5606
5607 char quoted = 0;
5608 const char16_t* p = s;
5609 while (p < (s+len)) {
5610 while (p < (s+len)) {
5611 const char16_t c = *p;
5612 if (c == '\\') {
5613 break;
5614 }
5615 if (!preserveSpaces) {
5616 if (quoted == 0 && isspace16(c)
5617 && (c != ' ' || isspace16(*(p+1)))) {
5618 break;
5619 }
5620 if (c == '"' && (quoted == 0 || quoted == '"')) {
5621 break;
5622 }
5623 if (c == '\'' && (quoted == 0 || quoted == '\'')) {
Eric Fischerc87d2522009-09-01 15:20:30 -07005624 /*
5625 * In practice, when people write ' instead of \'
5626 * in a string, they are doing it by accident
5627 * instead of really meaning to use ' as a quoting
5628 * character. Warn them so they don't lose it.
5629 */
5630 if (outErrorMsg) {
5631 *outErrorMsg = "Apostrophe not preceded by \\";
5632 }
5633 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 }
5635 }
5636 p++;
5637 }
5638 if (p < (s+len)) {
5639 if (p > s) {
5640 tmp.append(String16(s, p-s));
5641 }
5642 if (!preserveSpaces && (*p == '"' || *p == '\'')) {
5643 if (quoted == 0) {
5644 quoted = *p;
5645 } else {
5646 quoted = 0;
5647 }
5648 p++;
5649 } else if (!preserveSpaces && isspace16(*p)) {
5650 // Space outside of a quote -- consume all spaces and
5651 // leave a single plain space char.
5652 tmp.append(String16(" "));
5653 p++;
5654 while (p < (s+len) && isspace16(*p)) {
5655 p++;
5656 }
5657 } else if (*p == '\\') {
5658 p++;
5659 if (p < (s+len)) {
5660 switch (*p) {
5661 case 't':
5662 tmp.append(String16("\t"));
5663 break;
5664 case 'n':
5665 tmp.append(String16("\n"));
5666 break;
5667 case '#':
5668 tmp.append(String16("#"));
5669 break;
5670 case '@':
5671 tmp.append(String16("@"));
5672 break;
5673 case '?':
5674 tmp.append(String16("?"));
5675 break;
5676 case '"':
5677 tmp.append(String16("\""));
5678 break;
5679 case '\'':
5680 tmp.append(String16("'"));
5681 break;
5682 case '\\':
5683 tmp.append(String16("\\"));
5684 break;
5685 case 'u':
5686 {
5687 char16_t chr = 0;
5688 int i = 0;
5689 while (i < 4 && p[1] != 0) {
5690 p++;
5691 i++;
5692 int c;
5693 if (*p >= '0' && *p <= '9') {
5694 c = *p - '0';
5695 } else if (*p >= 'a' && *p <= 'f') {
5696 c = *p - 'a' + 10;
5697 } else if (*p >= 'A' && *p <= 'F') {
5698 c = *p - 'A' + 10;
5699 } else {
5700 if (outErrorMsg) {
5701 *outErrorMsg = "Bad character in \\u unicode escape sequence";
5702 }
5703 return false;
5704 }
5705 chr = (chr<<4) | c;
5706 }
5707 tmp.append(String16(&chr, 1));
5708 } break;
5709 default:
5710 // ignore unknown escape chars.
5711 break;
5712 }
5713 p++;
5714 }
5715 }
5716 len -= (p-s);
5717 s = p;
5718 }
5719 }
5720
5721 if (tmp.size() != 0) {
5722 if (len > 0) {
5723 tmp.append(String16(s, len));
5724 }
5725 if (append) {
5726 outString->append(tmp);
5727 } else {
5728 outString->setTo(tmp);
5729 }
5730 } else {
5731 if (append) {
5732 outString->append(String16(s, len));
5733 } else {
5734 outString->setTo(s, len);
5735 }
5736 }
5737
5738 return true;
5739}
5740
5741size_t ResTable::getBasePackageCount() const
5742{
5743 if (mError != NO_ERROR) {
5744 return 0;
5745 }
5746 return mPackageGroups.size();
5747}
5748
Adam Lesinskide898ff2014-01-29 18:20:45 -08005749const String16 ResTable::getBasePackageName(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750{
5751 if (mError != NO_ERROR) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08005752 return String16();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 }
5754 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5755 "Requested package index %d past package count %d",
5756 (int)idx, (int)mPackageGroups.size());
Adam Lesinskide898ff2014-01-29 18:20:45 -08005757 return mPackageGroups[idx]->name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758}
5759
5760uint32_t ResTable::getBasePackageId(size_t idx) const
5761{
5762 if (mError != NO_ERROR) {
5763 return 0;
5764 }
5765 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5766 "Requested package index %d past package count %d",
5767 (int)idx, (int)mPackageGroups.size());
5768 return mPackageGroups[idx]->id;
5769}
5770
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005771uint32_t ResTable::getLastTypeIdForPackage(size_t idx) const
5772{
5773 if (mError != NO_ERROR) {
5774 return 0;
5775 }
5776 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5777 "Requested package index %d past package count %d",
5778 (int)idx, (int)mPackageGroups.size());
5779 const PackageGroup* const group = mPackageGroups[idx];
5780 return group->largestTypeId;
5781}
5782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783size_t ResTable::getTableCount() const
5784{
5785 return mHeaders.size();
5786}
5787
5788const ResStringPool* ResTable::getTableStringBlock(size_t index) const
5789{
5790 return &mHeaders[index]->values;
5791}
5792
Narayan Kamath7c4887f2014-01-27 17:32:37 +00005793int32_t ResTable::getTableCookie(size_t index) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794{
5795 return mHeaders[index]->cookie;
5796}
5797
Adam Lesinskide898ff2014-01-29 18:20:45 -08005798const DynamicRefTable* ResTable::getDynamicRefTableForCookie(int32_t cookie) const
5799{
5800 const size_t N = mPackageGroups.size();
5801 for (size_t i = 0; i < N; i++) {
5802 const PackageGroup* pg = mPackageGroups[i];
5803 size_t M = pg->packages.size();
5804 for (size_t j = 0; j < M; j++) {
5805 if (pg->packages[j]->header->cookie == cookie) {
5806 return &pg->dynamicRefTable;
5807 }
5808 }
5809 }
5810 return NULL;
5811}
5812
Filip Gruszczynski23493322015-07-29 17:02:59 -07005813void ResTable::getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap,
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005814 bool ignoreAndroidPackage, bool includeSystemConfigs) const {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005815 const size_t packageCount = mPackageGroups.size();
Filip Gruszczynski23493322015-07-29 17:02:59 -07005816 String16 android("android");
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005817 for (size_t i = 0; i < packageCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 const PackageGroup* packageGroup = mPackageGroups[i];
Filip Gruszczynski23493322015-07-29 17:02:59 -07005819 if (ignoreAndroidPackage && android == packageGroup->name) {
5820 continue;
5821 }
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005822 if (!includeSystemConfigs && packageGroup->isSystemAsset) {
5823 continue;
5824 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005825 const size_t typeCount = packageGroup->types.size();
5826 for (size_t j = 0; j < typeCount; j++) {
5827 const TypeList& typeList = packageGroup->types[j];
5828 const size_t numTypes = typeList.size();
5829 for (size_t k = 0; k < numTypes; k++) {
5830 const Type* type = typeList[k];
Adam Lesinski42eea272015-01-15 17:01:39 -08005831 const ResStringPool& typeStrings = type->package->typeStrings;
5832 if (ignoreMipmap && typeStrings.string8ObjectAt(
5833 type->typeSpec->id - 1) == "mipmap") {
5834 continue;
5835 }
5836
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005837 const size_t numConfigs = type->configs.size();
5838 for (size_t m = 0; m < numConfigs; m++) {
5839 const ResTable_type* config = type->configs[m];
Narayan Kamath788fa412014-01-21 15:32:36 +00005840 ResTable_config cfg;
5841 memset(&cfg, 0, sizeof(ResTable_config));
5842 cfg.copyFromDtoH(config->config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 // only insert unique
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005844 const size_t N = configs->size();
5845 size_t n;
5846 for (n = 0; n < N; n++) {
5847 if (0 == (*configs)[n].compare(cfg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 break;
5849 }
5850 }
5851 // if we didn't find it
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005852 if (n == N) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005853 configs->add(cfg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
5855 }
5856 }
5857 }
5858 }
5859}
5860
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005861void ResTable::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862{
5863 Vector<ResTable_config> configs;
Steve Block71f2cf12011-10-20 11:56:00 +01005864 ALOGV("calling getConfigurations");
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08005865 getConfigurations(&configs,
5866 false /* ignoreMipmap */,
5867 false /* ignoreAndroidPackage */,
5868 includeSystemLocales /* includeSystemConfigs */);
Steve Block71f2cf12011-10-20 11:56:00 +01005869 ALOGV("called getConfigurations size=%d", (int)configs.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 const size_t I = configs.size();
Narayan Kamath48620f12014-01-20 13:57:11 +00005871
5872 char locale[RESTABLE_MAX_LOCALE_LEN];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 for (size_t i=0; i<I; i++) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005874 configs[i].getBcp47Locale(locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 const size_t J = locales->size();
5876 size_t j;
5877 for (j=0; j<J; j++) {
5878 if (0 == strcmp(locale, (*locales)[j].string())) {
5879 break;
5880 }
5881 }
5882 if (j == J) {
5883 locales->add(String8(locale));
5884 }
5885 }
5886}
5887
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005888StringPoolRef::StringPoolRef(const ResStringPool* pool, uint32_t index)
5889 : mPool(pool), mIndex(index) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005891StringPoolRef::StringPoolRef()
5892 : mPool(NULL), mIndex(0) {}
5893
5894const char* StringPoolRef::string8(size_t* outLen) const {
5895 if (mPool != NULL) {
5896 return mPool->string8At(mIndex, outLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005898 if (outLen != NULL) {
5899 *outLen = 0;
5900 }
5901 return NULL;
5902}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005904const char16_t* StringPoolRef::string16(size_t* outLen) const {
5905 if (mPool != NULL) {
5906 return mPool->stringAt(mIndex, outLen);
5907 }
5908 if (outLen != NULL) {
5909 *outLen = 0;
5910 }
5911 return NULL;
5912}
5913
Adam Lesinski82a2dd82014-09-17 18:34:15 -07005914bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const {
5915 if (mError != NO_ERROR) {
5916 return false;
5917 }
5918
5919 const ssize_t p = getResourcePackageIndex(resID);
5920 const int t = Res_GETTYPE(resID);
5921 const int e = Res_GETENTRY(resID);
5922
5923 if (p < 0) {
5924 if (Res_GETPACKAGE(resID)+1 == 0) {
5925 ALOGW("No package identifier when getting flags for resource number 0x%08x", resID);
5926 } else {
5927 ALOGW("No known package when getting flags for resource number 0x%08x", resID);
5928 }
5929 return false;
5930 }
5931 if (t < 0) {
5932 ALOGW("No type identifier when getting flags for resource number 0x%08x", resID);
5933 return false;
5934 }
5935
5936 const PackageGroup* const grp = mPackageGroups[p];
5937 if (grp == NULL) {
5938 ALOGW("Bad identifier when getting flags for resource number 0x%08x", resID);
5939 return false;
5940 }
5941
5942 Entry entry;
5943 status_t err = getEntry(grp, t, e, NULL, &entry);
5944 if (err != NO_ERROR) {
5945 return false;
5946 }
5947
5948 *outFlags = entry.specFlags;
5949 return true;
5950}
5951
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005952status_t ResTable::getEntry(
5953 const PackageGroup* packageGroup, int typeIndex, int entryIndex,
5954 const ResTable_config* config,
5955 Entry* outEntry) const
5956{
5957 const TypeList& typeList = packageGroup->types[typeIndex];
5958 if (typeList.isEmpty()) {
5959 ALOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 return BAD_TYPE;
5961 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005962
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005963 const ResTable_type* bestType = NULL;
5964 uint32_t bestOffset = ResTable_type::NO_ENTRY;
5965 const Package* bestPackage = NULL;
5966 uint32_t specFlags = 0;
5967 uint8_t actualTypeIndex = typeIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 ResTable_config bestConfig;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005969 memset(&bestConfig, 0, sizeof(bestConfig));
Mark Salyzyn00adb862014-03-19 11:00:06 -07005970
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005971 // Iterate over the Types of each package.
5972 const size_t typeCount = typeList.size();
5973 for (size_t i = 0; i < typeCount; i++) {
5974 const Type* const typeSpec = typeList[i];
Mark Salyzyn00adb862014-03-19 11:00:06 -07005975
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005976 int realEntryIndex = entryIndex;
5977 int realTypeIndex = typeIndex;
5978 bool currentTypeIsOverlay = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005980 // Runtime overlay packages provide a mapping of app resource
5981 // ID to package resource ID.
5982 if (typeSpec->idmapEntries.hasEntries()) {
5983 uint16_t overlayEntryIndex;
5984 if (typeSpec->idmapEntries.lookup(entryIndex, &overlayEntryIndex) != NO_ERROR) {
5985 // No such mapping exists
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 continue;
5987 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005988 realEntryIndex = overlayEntryIndex;
5989 realTypeIndex = typeSpec->idmapEntries.overlayTypeId() - 1;
5990 currentTypeIsOverlay = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005992
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005993 if (static_cast<size_t>(realEntryIndex) >= typeSpec->entryCount) {
5994 ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)",
5995 Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex),
5996 entryIndex, static_cast<int>(typeSpec->entryCount));
5997 // We should normally abort here, but some legacy apps declare
5998 // resources in the 'android' package (old bug in AAPT).
5999 continue;
6000 }
6001
6002 // Aggregate all the flags for each package that defines this entry.
6003 if (typeSpec->typeSpecFlags != NULL) {
6004 specFlags |= dtohl(typeSpec->typeSpecFlags[realEntryIndex]);
6005 } else {
6006 specFlags = -1;
6007 }
6008
Adam Lesinskiff5808d2016-02-23 17:49:53 -08006009 const Vector<const ResTable_type*>* candidateConfigs = &typeSpec->configs;
6010
6011 std::shared_ptr<Vector<const ResTable_type*>> filteredConfigs;
6012 if (config && memcmp(&mParams, config, sizeof(mParams)) == 0) {
6013 // Grab the lock first so we can safely get the current filtered list.
6014 AutoMutex _lock(mFilteredConfigLock);
6015
6016 // This configuration is equal to the one we have previously cached for,
6017 // so use the filtered configs.
6018
6019 const TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries[typeIndex];
6020 if (i < cacheEntry.filteredConfigs.size()) {
6021 if (cacheEntry.filteredConfigs[i]) {
6022 // Grab a reference to the shared_ptr so it doesn't get destroyed while
6023 // going through this list.
6024 filteredConfigs = cacheEntry.filteredConfigs[i];
6025
6026 // Use this filtered list.
6027 candidateConfigs = filteredConfigs.get();
6028 }
6029 }
6030 }
6031
6032 const size_t numConfigs = candidateConfigs->size();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006033 for (size_t c = 0; c < numConfigs; c++) {
Adam Lesinskiff5808d2016-02-23 17:49:53 -08006034 const ResTable_type* const thisType = candidateConfigs->itemAt(c);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006035 if (thisType == NULL) {
6036 continue;
6037 }
6038
6039 ResTable_config thisConfig;
6040 thisConfig.copyFromDtoH(thisType->config);
6041
6042 // Check to make sure this one is valid for the current parameters.
6043 if (config != NULL && !thisConfig.match(*config)) {
6044 continue;
6045 }
6046
6047 // Check if there is the desired entry in this type.
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006048 const uint32_t* const eindex = reinterpret_cast<const uint32_t*>(
6049 reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize));
6050
6051 uint32_t thisOffset = dtohl(eindex[realEntryIndex]);
6052 if (thisOffset == ResTable_type::NO_ENTRY) {
6053 // There is no entry for this index and configuration.
6054 continue;
6055 }
6056
6057 if (bestType != NULL) {
6058 // Check if this one is less specific than the last found. If so,
6059 // we will skip it. We check starting with things we most care
6060 // about to those we least care about.
6061 if (!thisConfig.isBetterThan(bestConfig, config)) {
6062 if (!currentTypeIsOverlay || thisConfig.compare(bestConfig) != 0) {
6063 continue;
6064 }
6065 }
6066 }
6067
6068 bestType = thisType;
6069 bestOffset = thisOffset;
6070 bestConfig = thisConfig;
6071 bestPackage = typeSpec->package;
6072 actualTypeIndex = realTypeIndex;
6073
6074 // If no config was specified, any type will do, so skip
6075 if (config == NULL) {
6076 break;
6077 }
6078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006080
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006081 if (bestType == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 return BAD_INDEX;
6083 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006084
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006085 bestOffset += dtohl(bestType->entriesStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006087 if (bestOffset > (dtohl(bestType->header.size)-sizeof(ResTable_entry))) {
Steve Block8564c8d2012-01-05 23:22:43 +00006088 ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006089 bestOffset, dtohl(bestType->header.size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 return BAD_TYPE;
6091 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006092 if ((bestOffset & 0x3) != 0) {
6093 ALOGW("ResTable_entry at 0x%x is not on an integer boundary", bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 return BAD_TYPE;
6095 }
6096
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006097 const ResTable_entry* const entry = reinterpret_cast<const ResTable_entry*>(
6098 reinterpret_cast<const uint8_t*>(bestType) + bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 if (dtohs(entry->size) < sizeof(*entry)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006100 ALOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 return BAD_TYPE;
6102 }
6103
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006104 if (outEntry != NULL) {
6105 outEntry->entry = entry;
6106 outEntry->config = bestConfig;
6107 outEntry->type = bestType;
6108 outEntry->specFlags = specFlags;
6109 outEntry->package = bestPackage;
6110 outEntry->typeStr = StringPoolRef(&bestPackage->typeStrings, actualTypeIndex - bestPackage->typeIdOffset);
6111 outEntry->keyStr = StringPoolRef(&bestPackage->keyStrings, dtohl(entry->key.index));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006113 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114}
6115
6116status_t ResTable::parsePackage(const ResTable_package* const pkg,
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006117 const Header* const header, bool appAsLib, bool isSystemAsset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118{
6119 const uint8_t* base = (const uint8_t*)pkg;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006120 status_t err = validate_chunk(&pkg->header, sizeof(*pkg) - sizeof(pkg->typeIdOffset),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 header->dataEnd, "ResTable_package");
6122 if (err != NO_ERROR) {
6123 return (mError=err);
6124 }
6125
Patrik Bannura443dd932014-02-12 13:38:54 +01006126 const uint32_t pkgSize = dtohl(pkg->header.size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127
6128 if (dtohl(pkg->typeStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006129 ALOGW("ResTable_package type strings at 0x%x are past chunk size 0x%x.",
6130 dtohl(pkg->typeStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 return (mError=BAD_TYPE);
6132 }
6133 if ((dtohl(pkg->typeStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006134 ALOGW("ResTable_package type strings at 0x%x is not on an integer boundary.",
6135 dtohl(pkg->typeStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 return (mError=BAD_TYPE);
6137 }
6138 if (dtohl(pkg->keyStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006139 ALOGW("ResTable_package key strings at 0x%x are past chunk size 0x%x.",
6140 dtohl(pkg->keyStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 return (mError=BAD_TYPE);
6142 }
6143 if ((dtohl(pkg->keyStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006144 ALOGW("ResTable_package key strings at 0x%x is not on an integer boundary.",
6145 dtohl(pkg->keyStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 return (mError=BAD_TYPE);
6147 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006148
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006149 uint32_t id = dtohl(pkg->id);
6150 KeyedVector<uint8_t, IdmapEntries> idmapEntries;
Mark Salyzyn00adb862014-03-19 11:00:06 -07006151
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006152 if (header->resourceIDMap != NULL) {
6153 uint8_t targetPackageId = 0;
6154 status_t err = parseIdmap(header->resourceIDMap, header->resourceIDMapSize, &targetPackageId, &idmapEntries);
6155 if (err != NO_ERROR) {
6156 ALOGW("Overlay is broken");
6157 return (mError=err);
6158 }
6159 id = targetPackageId;
6160 }
6161
6162 if (id >= 256) {
6163 LOG_ALWAYS_FATAL("Package id out of range");
6164 return NO_ERROR;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006165 } else if (id == 0 || appAsLib || isSystemAsset) {
6166 // This is a library or a system asset, so assign an ID
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006167 id = mNextPackageId++;
6168 }
6169
6170 PackageGroup* group = NULL;
6171 Package* package = new Package(this, header, pkg);
6172 if (package == NULL) {
6173 return (mError=NO_MEMORY);
6174 }
6175
6176 err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings),
6177 header->dataEnd-(base+dtohl(pkg->typeStrings)));
6178 if (err != NO_ERROR) {
6179 delete group;
6180 delete package;
6181 return (mError=err);
6182 }
6183
6184 err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings),
6185 header->dataEnd-(base+dtohl(pkg->keyStrings)));
6186 if (err != NO_ERROR) {
6187 delete group;
6188 delete package;
6189 return (mError=err);
6190 }
6191
6192 size_t idx = mPackageMap[id];
6193 if (idx == 0) {
6194 idx = mPackageGroups.size() + 1;
6195
Adam Lesinski4bf58102014-11-03 11:21:19 -08006196 char16_t tmpName[sizeof(pkg->name)/sizeof(pkg->name[0])];
6197 strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(pkg->name[0]));
Roozbeh Pournader1c686f22015-12-18 14:22:14 -08006198 group = new PackageGroup(this, String16(tmpName), id, appAsLib, isSystemAsset);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006199 if (group == NULL) {
6200 delete package;
Dianne Hackborn78c40512009-07-06 11:07:40 -07006201 return (mError=NO_MEMORY);
6202 }
Adam Lesinskifab50872014-04-16 14:40:42 -07006203
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006204 err = mPackageGroups.add(group);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 if (err < NO_ERROR) {
6206 return (mError=err);
6207 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006208
6209 mPackageMap[id] = static_cast<uint8_t>(idx);
6210
6211 // Find all packages that reference this package
6212 size_t N = mPackageGroups.size();
6213 for (size_t i = 0; i < N; i++) {
6214 mPackageGroups[i]->dynamicRefTable.addMapping(
6215 group->name, static_cast<uint8_t>(group->id));
6216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006218 group = mPackageGroups.itemAt(idx - 1);
6219 if (group == NULL) {
6220 return (mError=UNKNOWN_ERROR);
6221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 }
6223
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006224 err = group->packages.add(package);
6225 if (err < NO_ERROR) {
6226 return (mError=err);
6227 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 // Iterate through all chunks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 const ResChunk_header* chunk =
6231 (const ResChunk_header*)(((const uint8_t*)pkg)
6232 + dtohs(pkg->header.headerSize));
6233 const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size);
6234 while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) &&
6235 ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006236 if (kDebugTableNoisy) {
6237 ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
6238 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
6239 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
6240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 const size_t csize = dtohl(chunk->size);
6242 const uint16_t ctype = dtohs(chunk->type);
6243 if (ctype == RES_TABLE_TYPE_SPEC_TYPE) {
6244 const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk);
6245 err = validate_chunk(&typeSpec->header, sizeof(*typeSpec),
6246 endPos, "ResTable_typeSpec");
6247 if (err != NO_ERROR) {
6248 return (mError=err);
6249 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 const size_t typeSpecSize = dtohl(typeSpec->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006252 const size_t newEntryCount = dtohl(typeSpec->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006253
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006254 if (kDebugLoadTableNoisy) {
6255 ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
6256 (void*)(base-(const uint8_t*)chunk),
6257 dtohs(typeSpec->header.type),
6258 dtohs(typeSpec->header.headerSize),
6259 (void*)typeSpecSize);
6260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006261 // look for block overrun or int overflow when multiplying by 4
6262 if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006263 || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 > typeSpecSize)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006265 ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006266 (void*)(dtohs(typeSpec->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6267 (void*)typeSpecSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 return (mError=BAD_TYPE);
6269 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 if (typeSpec->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006272 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 return (mError=BAD_TYPE);
6274 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006275
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006276 if (newEntryCount > 0) {
6277 uint8_t typeIndex = typeSpec->id - 1;
6278 ssize_t idmapIndex = idmapEntries.indexOfKey(typeSpec->id);
6279 if (idmapIndex >= 0) {
6280 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6281 }
6282
6283 TypeList& typeList = group->types.editItemAt(typeIndex);
6284 if (!typeList.isEmpty()) {
6285 const Type* existingType = typeList[0];
6286 if (existingType->entryCount != newEntryCount && idmapIndex < 0) {
6287 ALOGW("ResTable_typeSpec entry count inconsistent: given %d, previously %d",
6288 (int) newEntryCount, (int) existingType->entryCount);
6289 // We should normally abort here, but some legacy apps declare
6290 // resources in the 'android' package (old bug in AAPT).
6291 }
6292 }
6293
6294 Type* t = new Type(header, package, newEntryCount);
6295 t->typeSpec = typeSpec;
6296 t->typeSpecFlags = (const uint32_t*)(
6297 ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize));
6298 if (idmapIndex >= 0) {
6299 t->idmapEntries = idmapEntries[idmapIndex];
6300 }
6301 typeList.add(t);
6302 group->largestTypeId = max(group->largestTypeId, typeSpec->id);
6303 } else {
6304 ALOGV("Skipping empty ResTable_typeSpec for type %d", typeSpec->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 } else if (ctype == RES_TABLE_TYPE_TYPE) {
6308 const ResTable_type* type = (const ResTable_type*)(chunk);
6309 err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4,
6310 endPos, "ResTable_type");
6311 if (err != NO_ERROR) {
6312 return (mError=err);
6313 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006314
Patrik Bannura443dd932014-02-12 13:38:54 +01006315 const uint32_t typeSize = dtohl(type->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006316 const size_t newEntryCount = dtohl(type->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006317
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006318 if (kDebugLoadTableNoisy) {
6319 printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n",
6320 (void*)(base-(const uint8_t*)chunk),
6321 dtohs(type->header.type),
6322 dtohs(type->header.headerSize),
6323 typeSize);
6324 }
6325 if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006326 ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006327 (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6328 typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 return (mError=BAD_TYPE);
6330 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006331
6332 if (newEntryCount != 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) {
Patrik Bannura443dd932014-02-12 13:38:54 +01006334 ALOGW("ResTable_type entriesStart at 0x%x extends beyond chunk end 0x%x.",
6335 dtohl(type->entriesStart), typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 return (mError=BAD_TYPE);
6337 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 if (type->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006340 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 return (mError=BAD_TYPE);
6342 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006343
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006344 if (newEntryCount > 0) {
6345 uint8_t typeIndex = type->id - 1;
6346 ssize_t idmapIndex = idmapEntries.indexOfKey(type->id);
6347 if (idmapIndex >= 0) {
6348 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6349 }
6350
6351 TypeList& typeList = group->types.editItemAt(typeIndex);
6352 if (typeList.isEmpty()) {
6353 ALOGE("No TypeSpec for type %d", type->id);
6354 return (mError=BAD_TYPE);
6355 }
6356
6357 Type* t = typeList.editItemAt(typeList.size() - 1);
6358 if (newEntryCount != t->entryCount) {
6359 ALOGE("ResTable_type entry count inconsistent: given %d, previously %d",
6360 (int)newEntryCount, (int)t->entryCount);
6361 return (mError=BAD_TYPE);
6362 }
6363
6364 if (t->package != package) {
6365 ALOGE("No TypeSpec for type %d", type->id);
6366 return (mError=BAD_TYPE);
6367 }
6368
6369 t->configs.add(type);
6370
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006371 if (kDebugTableGetEntry) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006372 ResTable_config thisConfig;
6373 thisConfig.copyFromDtoH(type->config);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006374 ALOGI("Adding config to type %d: %s\n", type->id,
6375 thisConfig.toString().string());
6376 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006377 } else {
6378 ALOGV("Skipping empty ResTable_type for type %d", type->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006380
Adam Lesinskide898ff2014-01-29 18:20:45 -08006381 } else if (ctype == RES_TABLE_LIBRARY_TYPE) {
6382 if (group->dynamicRefTable.entries().size() == 0) {
6383 status_t err = group->dynamicRefTable.load((const ResTable_lib_header*) chunk);
6384 if (err != NO_ERROR) {
6385 return (mError=err);
6386 }
6387
6388 // Fill in the reference table with the entries we already know about.
6389 size_t N = mPackageGroups.size();
6390 for (size_t i = 0; i < N; i++) {
6391 group->dynamicRefTable.addMapping(mPackageGroups[i]->name, mPackageGroups[i]->id);
6392 }
6393 } else {
6394 ALOGW("Found multiple library tables, ignoring...");
6395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 } else {
6397 status_t err = validate_chunk(chunk, sizeof(ResChunk_header),
6398 endPos, "ResTable_package:unknown");
6399 if (err != NO_ERROR) {
6400 return (mError=err);
6401 }
6402 }
6403 chunk = (const ResChunk_header*)
6404 (((const uint8_t*)chunk) + csize);
6405 }
6406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 return NO_ERROR;
6408}
6409
Tao Baia6d7e3f2015-09-01 18:49:54 -07006410DynamicRefTable::DynamicRefTable(uint8_t packageId, bool appAsLib)
Adam Lesinskide898ff2014-01-29 18:20:45 -08006411 : mAssignedPackageId(packageId)
Tao Baia6d7e3f2015-09-01 18:49:54 -07006412 , mAppAsLib(appAsLib)
Adam Lesinskide898ff2014-01-29 18:20:45 -08006413{
6414 memset(mLookupTable, 0, sizeof(mLookupTable));
6415
6416 // Reserved package ids
6417 mLookupTable[APP_PACKAGE_ID] = APP_PACKAGE_ID;
6418 mLookupTable[SYS_PACKAGE_ID] = SYS_PACKAGE_ID;
6419}
6420
6421status_t DynamicRefTable::load(const ResTable_lib_header* const header)
6422{
6423 const uint32_t entryCount = dtohl(header->count);
6424 const uint32_t sizeOfEntries = sizeof(ResTable_lib_entry) * entryCount;
6425 const uint32_t expectedSize = dtohl(header->header.size) - dtohl(header->header.headerSize);
6426 if (sizeOfEntries > expectedSize) {
6427 ALOGE("ResTable_lib_header size %u is too small to fit %u entries (x %u).",
6428 expectedSize, entryCount, (uint32_t)sizeof(ResTable_lib_entry));
6429 return UNKNOWN_ERROR;
6430 }
6431
6432 const ResTable_lib_entry* entry = (const ResTable_lib_entry*)(((uint8_t*) header) +
6433 dtohl(header->header.headerSize));
6434 for (uint32_t entryIndex = 0; entryIndex < entryCount; entryIndex++) {
6435 uint32_t packageId = dtohl(entry->packageId);
6436 char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)];
6437 strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006438 if (kDebugLibNoisy) {
6439 ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
6440 dtohl(entry->packageId));
6441 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08006442 if (packageId >= 256) {
6443 ALOGE("Bad package id 0x%08x", packageId);
6444 return UNKNOWN_ERROR;
6445 }
6446 mEntries.replaceValueFor(String16(tmpName), (uint8_t) packageId);
6447 entry = entry + 1;
6448 }
6449 return NO_ERROR;
6450}
6451
Adam Lesinski6022deb2014-08-20 14:59:19 -07006452status_t DynamicRefTable::addMappings(const DynamicRefTable& other) {
6453 if (mAssignedPackageId != other.mAssignedPackageId) {
6454 return UNKNOWN_ERROR;
6455 }
6456
6457 const size_t entryCount = other.mEntries.size();
6458 for (size_t i = 0; i < entryCount; i++) {
6459 ssize_t index = mEntries.indexOfKey(other.mEntries.keyAt(i));
6460 if (index < 0) {
6461 mEntries.add(other.mEntries.keyAt(i), other.mEntries[i]);
6462 } else {
6463 if (other.mEntries[i] != mEntries[index]) {
6464 return UNKNOWN_ERROR;
6465 }
6466 }
6467 }
6468
6469 // Merge the lookup table. No entry can conflict
6470 // (value of 0 means not set).
6471 for (size_t i = 0; i < 256; i++) {
6472 if (mLookupTable[i] != other.mLookupTable[i]) {
6473 if (mLookupTable[i] == 0) {
6474 mLookupTable[i] = other.mLookupTable[i];
6475 } else if (other.mLookupTable[i] != 0) {
6476 return UNKNOWN_ERROR;
6477 }
6478 }
6479 }
6480 return NO_ERROR;
6481}
6482
Adam Lesinskide898ff2014-01-29 18:20:45 -08006483status_t DynamicRefTable::addMapping(const String16& packageName, uint8_t packageId)
6484{
6485 ssize_t index = mEntries.indexOfKey(packageName);
6486 if (index < 0) {
6487 return UNKNOWN_ERROR;
6488 }
6489 mLookupTable[mEntries.valueAt(index)] = packageId;
6490 return NO_ERROR;
6491}
6492
6493status_t DynamicRefTable::lookupResourceId(uint32_t* resId) const {
6494 uint32_t res = *resId;
6495 size_t packageId = Res_GETPACKAGE(res) + 1;
6496
Tao Baia6d7e3f2015-09-01 18:49:54 -07006497 if (packageId == APP_PACKAGE_ID && !mAppAsLib) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08006498 // No lookup needs to be done, app package IDs are absolute.
6499 return NO_ERROR;
6500 }
6501
Tao Baia6d7e3f2015-09-01 18:49:54 -07006502 if (packageId == 0 || (packageId == APP_PACKAGE_ID && mAppAsLib)) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08006503 // The package ID is 0x00. That means that a shared library is accessing
Tao Baia6d7e3f2015-09-01 18:49:54 -07006504 // its own local resource.
6505 // Or if app resource is loaded as shared library, the resource which has
6506 // app package Id is local resources.
6507 // so we fix up those resources with the calling package ID.
6508 *resId = (0xFFFFFF & (*resId)) | (((uint32_t) mAssignedPackageId) << 24);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006509 return NO_ERROR;
6510 }
6511
6512 // Do a proper lookup.
6513 uint8_t translatedId = mLookupTable[packageId];
6514 if (translatedId == 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006515 ALOGV("DynamicRefTable(0x%02x): No mapping for build-time package ID 0x%02x.",
Adam Lesinskide898ff2014-01-29 18:20:45 -08006516 (uint8_t)mAssignedPackageId, (uint8_t)packageId);
6517 for (size_t i = 0; i < 256; i++) {
6518 if (mLookupTable[i] != 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006519 ALOGV("e[0x%02x] -> 0x%02x", (uint8_t)i, mLookupTable[i]);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006520 }
6521 }
6522 return UNKNOWN_ERROR;
6523 }
6524
6525 *resId = (res & 0x00ffffff) | (((uint32_t) translatedId) << 24);
6526 return NO_ERROR;
6527}
6528
6529status_t DynamicRefTable::lookupResourceValue(Res_value* value) const {
Tao Baia6d7e3f2015-09-01 18:49:54 -07006530 if (value->dataType != Res_value::TYPE_DYNAMIC_REFERENCE &&
6531 (value->dataType != Res_value::TYPE_REFERENCE || !mAppAsLib)) {
6532 // If the package is loaded as shared library, the resource reference
6533 // also need to be fixed.
Adam Lesinskide898ff2014-01-29 18:20:45 -08006534 return NO_ERROR;
6535 }
6536
6537 status_t err = lookupResourceId(&value->data);
6538 if (err != NO_ERROR) {
6539 return err;
6540 }
6541
6542 value->dataType = Res_value::TYPE_REFERENCE;
6543 return NO_ERROR;
6544}
6545
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006546struct IdmapTypeMap {
6547 ssize_t overlayTypeId;
6548 size_t entryOffset;
6549 Vector<uint32_t> entryMap;
6550};
6551
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006552status_t ResTable::createIdmap(const ResTable& overlay,
6553 uint32_t targetCrc, uint32_t overlayCrc,
6554 const char* targetPath, const char* overlayPath,
6555 void** outData, size_t* outSize) const
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006556{
6557 // see README for details on the format of map
6558 if (mPackageGroups.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006559 ALOGW("idmap: target package has no package groups, cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006560 return UNKNOWN_ERROR;
6561 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006562
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006563 if (mPackageGroups[0]->packages.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006564 ALOGW("idmap: target package has no packages in its first package group, "
6565 "cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006566 return UNKNOWN_ERROR;
6567 }
6568
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006569 KeyedVector<uint8_t, IdmapTypeMap> map;
6570
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006571 // overlaid packages are assumed to contain only one package group
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006572 const PackageGroup* pg = mPackageGroups[0];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006573
6574 // starting size is header
6575 *outSize = ResTable::IDMAP_HEADER_SIZE_BYTES;
6576
6577 // target package id and number of types in map
6578 *outSize += 2 * sizeof(uint16_t);
6579
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006580 // overlay packages are assumed to contain only one package group
Adam Lesinski4bf58102014-11-03 11:21:19 -08006581 const ResTable_package* overlayPackageStruct = overlay.mPackageGroups[0]->packages[0]->package;
6582 char16_t tmpName[sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])];
6583 strcpy16_dtoh(tmpName, overlayPackageStruct->name, sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0]));
6584 const String16 overlayPackage(tmpName);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006585
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006586 for (size_t typeIndex = 0; typeIndex < pg->types.size(); ++typeIndex) {
6587 const TypeList& typeList = pg->types[typeIndex];
6588 if (typeList.isEmpty()) {
6589 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006590 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006591
6592 const Type* typeConfigs = typeList[0];
6593
6594 IdmapTypeMap typeMap;
6595 typeMap.overlayTypeId = -1;
6596 typeMap.entryOffset = 0;
6597
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006598 for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006599 uint32_t resID = Res_MAKEID(pg->id - 1, typeIndex, entryIndex);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006600 resource_name resName;
MÃ¥rten Kongstad65a05fd2014-01-31 14:01:52 +01006601 if (!this->getResourceName(resID, false, &resName)) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006602 if (typeMap.entryMap.isEmpty()) {
6603 typeMap.entryOffset++;
6604 }
MÃ¥rten Kongstadfcaba142011-05-19 16:02:35 +02006605 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006606 }
6607
6608 const String16 overlayType(resName.type, resName.typeLen);
6609 const String16 overlayName(resName.name, resName.nameLen);
6610 uint32_t overlayResID = overlay.identifierForName(overlayName.string(),
6611 overlayName.size(),
6612 overlayType.string(),
6613 overlayType.size(),
6614 overlayPackage.string(),
6615 overlayPackage.size());
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006616 if (overlayResID == 0) {
6617 if (typeMap.entryMap.isEmpty()) {
6618 typeMap.entryOffset++;
Jean-Baptiste Queru3e2d5912012-05-01 10:00:22 -07006619 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006620 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006621 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006622
6623 if (typeMap.overlayTypeId == -1) {
6624 typeMap.overlayTypeId = Res_GETTYPE(overlayResID) + 1;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006625 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006626
6627 if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) {
6628 ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x"
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006629 " but entries should map to resources of type %02zx",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006630 resID, overlayResID, typeMap.overlayTypeId);
6631 return BAD_TYPE;
6632 }
6633
6634 if (typeMap.entryOffset + typeMap.entryMap.size() < entryIndex) {
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006635 // pad with 0xffffffff's (indicating non-existing entries) before adding this entry
6636 size_t index = typeMap.entryMap.size();
6637 size_t numItems = entryIndex - (typeMap.entryOffset + index);
6638 if (typeMap.entryMap.insertAt(0xffffffff, index, numItems) < 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006639 return NO_MEMORY;
6640 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006641 }
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006642 typeMap.entryMap.add(Res_GETENTRY(overlayResID));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006643 }
6644
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006645 if (!typeMap.entryMap.isEmpty()) {
6646 if (map.add(static_cast<uint8_t>(typeIndex), typeMap) < 0) {
6647 return NO_MEMORY;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006648 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006649 *outSize += (4 * sizeof(uint16_t)) + (typeMap.entryMap.size() * sizeof(uint32_t));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006650 }
6651 }
6652
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006653 if (map.isEmpty()) {
6654 ALOGW("idmap: no resources in overlay package present in base package");
6655 return UNKNOWN_ERROR;
6656 }
6657
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006658 if ((*outData = malloc(*outSize)) == NULL) {
6659 return NO_MEMORY;
6660 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006661
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006662 uint32_t* data = (uint32_t*)*outData;
6663 *data++ = htodl(IDMAP_MAGIC);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006664 *data++ = htodl(IDMAP_CURRENT_VERSION);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006665 *data++ = htodl(targetCrc);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006666 *data++ = htodl(overlayCrc);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006667 const char* paths[] = { targetPath, overlayPath };
6668 for (int j = 0; j < 2; ++j) {
6669 char* p = (char*)data;
6670 const char* path = paths[j];
6671 const size_t I = strlen(path);
6672 if (I > 255) {
6673 ALOGV("path exceeds expected 255 characters: %s\n", path);
6674 return UNKNOWN_ERROR;
6675 }
6676 for (size_t i = 0; i < 256; ++i) {
6677 *p++ = i < I ? path[i] : '\0';
6678 }
6679 data += 256 / sizeof(uint32_t);
6680 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006681 const size_t mapSize = map.size();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006682 uint16_t* typeData = reinterpret_cast<uint16_t*>(data);
6683 *typeData++ = htods(pg->id);
6684 *typeData++ = htods(mapSize);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006685 for (size_t i = 0; i < mapSize; ++i) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006686 uint8_t targetTypeId = map.keyAt(i);
6687 const IdmapTypeMap& typeMap = map[i];
6688 *typeData++ = htods(targetTypeId + 1);
6689 *typeData++ = htods(typeMap.overlayTypeId);
6690 *typeData++ = htods(typeMap.entryMap.size());
6691 *typeData++ = htods(typeMap.entryOffset);
6692
6693 const size_t entryCount = typeMap.entryMap.size();
6694 uint32_t* entries = reinterpret_cast<uint32_t*>(typeData);
6695 for (size_t j = 0; j < entryCount; j++) {
6696 entries[j] = htodl(typeMap.entryMap[j]);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006697 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006698 typeData += entryCount * 2;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006699 }
6700
6701 return NO_ERROR;
6702}
6703
6704bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006705 uint32_t* pVersion,
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006706 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
6707 String8* pTargetPath, String8* pOverlayPath)
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006708{
6709 const uint32_t* map = (const uint32_t*)idmap;
6710 if (!assertIdmapHeader(map, sizeBytes)) {
6711 return false;
6712 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006713 if (pVersion) {
6714 *pVersion = dtohl(map[1]);
6715 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006716 if (pTargetCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006717 *pTargetCrc = dtohl(map[2]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006718 }
6719 if (pOverlayCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006720 *pOverlayCrc = dtohl(map[3]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006721 }
6722 if (pTargetPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006723 pTargetPath->setTo(reinterpret_cast<const char*>(map + 4));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006724 }
6725 if (pOverlayPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006726 pOverlayPath->setTo(reinterpret_cast<const char*>(map + 4 + 256 / sizeof(uint32_t)));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006727 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006728 return true;
6729}
6730
6731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732#define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string())
6733
6734#define CHAR16_ARRAY_EQ(constant, var, len) \
6735 ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len))))
6736
Jeff Brown9d3b1a42013-07-01 19:07:15 -07006737static void print_complex(uint32_t complex, bool isFraction)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738{
Dianne Hackborne17086b2009-06-19 15:13:28 -07006739 const float MANTISSA_MULT =
6740 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT);
6741 const float RADIX_MULTS[] = {
6742 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
6743 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
6744 };
6745
6746 float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK
6747 <<Res_value::COMPLEX_MANTISSA_SHIFT))
6748 * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT)
6749 & Res_value::COMPLEX_RADIX_MASK];
6750 printf("%f", value);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006751
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006752 if (!isFraction) {
Dianne Hackborne17086b2009-06-19 15:13:28 -07006753 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6754 case Res_value::COMPLEX_UNIT_PX: printf("px"); break;
6755 case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break;
6756 case Res_value::COMPLEX_UNIT_SP: printf("sp"); break;
6757 case Res_value::COMPLEX_UNIT_PT: printf("pt"); break;
6758 case Res_value::COMPLEX_UNIT_IN: printf("in"); break;
6759 case Res_value::COMPLEX_UNIT_MM: printf("mm"); break;
6760 default: printf(" (unknown unit)"); break;
6761 }
6762 } else {
6763 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6764 case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break;
6765 case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break;
6766 default: printf(" (unknown unit)"); break;
6767 }
6768 }
6769}
6770
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006771// Normalize a string for output
6772String8 ResTable::normalizeForOutput( const char *input )
6773{
6774 String8 ret;
6775 char buff[2];
6776 buff[1] = '\0';
6777
6778 while (*input != '\0') {
6779 switch (*input) {
6780 // All interesting characters are in the ASCII zone, so we are making our own lives
6781 // easier by scanning the string one byte at a time.
6782 case '\\':
6783 ret += "\\\\";
6784 break;
6785 case '\n':
6786 ret += "\\n";
6787 break;
6788 case '"':
6789 ret += "\\\"";
6790 break;
6791 default:
6792 buff[0] = *input;
6793 ret += buff;
6794 break;
6795 }
6796
6797 input++;
6798 }
6799
6800 return ret;
6801}
6802
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006803void ResTable::print_value(const Package* pkg, const Res_value& value) const
6804{
6805 if (value.dataType == Res_value::TYPE_NULL) {
Alan Viverettef2969402014-10-29 17:09:36 -07006806 if (value.data == Res_value::DATA_NULL_UNDEFINED) {
6807 printf("(null)\n");
6808 } else if (value.data == Res_value::DATA_NULL_EMPTY) {
6809 printf("(null empty)\n");
6810 } else {
6811 // This should never happen.
6812 printf("(null) 0x%08x\n", value.data);
6813 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006814 } else if (value.dataType == Res_value::TYPE_REFERENCE) {
6815 printf("(reference) 0x%08x\n", value.data);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006816 } else if (value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) {
6817 printf("(dynamic reference) 0x%08x\n", value.data);
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006818 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
6819 printf("(attribute) 0x%08x\n", value.data);
6820 } else if (value.dataType == Res_value::TYPE_STRING) {
6821 size_t len;
Kenny Root780d2a12010-02-22 22:36:26 -08006822 const char* str8 = pkg->header->values.string8At(
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006823 value.data, &len);
Kenny Root780d2a12010-02-22 22:36:26 -08006824 if (str8 != NULL) {
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006825 printf("(string8) \"%s\"\n", normalizeForOutput(str8).string());
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006826 } else {
Kenny Root780d2a12010-02-22 22:36:26 -08006827 const char16_t* str16 = pkg->header->values.stringAt(
6828 value.data, &len);
6829 if (str16 != NULL) {
6830 printf("(string16) \"%s\"\n",
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006831 normalizeForOutput(String8(str16, len).string()).string());
Kenny Root780d2a12010-02-22 22:36:26 -08006832 } else {
6833 printf("(string) null\n");
6834 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006835 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006836 } else if (value.dataType == Res_value::TYPE_FLOAT) {
6837 printf("(float) %g\n", *(const float*)&value.data);
6838 } else if (value.dataType == Res_value::TYPE_DIMENSION) {
6839 printf("(dimension) ");
6840 print_complex(value.data, false);
6841 printf("\n");
6842 } else if (value.dataType == Res_value::TYPE_FRACTION) {
6843 printf("(fraction) ");
6844 print_complex(value.data, true);
6845 printf("\n");
6846 } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT
6847 || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) {
6848 printf("(color) #%08x\n", value.data);
6849 } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) {
6850 printf("(boolean) %s\n", value.data ? "true" : "false");
6851 } else if (value.dataType >= Res_value::TYPE_FIRST_INT
6852 || value.dataType <= Res_value::TYPE_LAST_INT) {
6853 printf("(int) 0x%08x or %d\n", value.data, value.data);
6854 } else {
6855 printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n",
6856 (int)value.dataType, (int)value.data,
6857 (int)value.size, (int)value.res0);
6858 }
6859}
6860
Dianne Hackborne17086b2009-06-19 15:13:28 -07006861void ResTable::print(bool inclValues) const
6862{
6863 if (mError != 0) {
6864 printf("mError=0x%x (%s)\n", mError, strerror(mError));
6865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 size_t pgCount = mPackageGroups.size();
6867 printf("Package Groups (%d)\n", (int)pgCount);
6868 for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) {
6869 const PackageGroup* pg = mPackageGroups[pgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006870 printf("Package Group %d id=0x%02x packageCount=%d name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 (int)pgIndex, pg->id, (int)pg->packages.size(),
6872 String8(pg->name).string());
Mark Salyzyn00adb862014-03-19 11:00:06 -07006873
Adam Lesinski6022deb2014-08-20 14:59:19 -07006874 const KeyedVector<String16, uint8_t>& refEntries = pg->dynamicRefTable.entries();
6875 const size_t refEntryCount = refEntries.size();
6876 if (refEntryCount > 0) {
6877 printf(" DynamicRefTable entryCount=%d:\n", (int) refEntryCount);
6878 for (size_t refIndex = 0; refIndex < refEntryCount; refIndex++) {
6879 printf(" 0x%02x -> %s\n",
6880 refEntries.valueAt(refIndex),
6881 String8(refEntries.keyAt(refIndex)).string());
6882 }
6883 printf("\n");
6884 }
6885
6886 int packageId = pg->id;
Adam Lesinski18560882014-08-15 17:18:21 +00006887 size_t pkgCount = pg->packages.size();
6888 for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) {
6889 const Package* pkg = pg->packages[pkgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006890 // Use a package's real ID, since the ID may have been assigned
6891 // if this package is a shared library.
6892 packageId = pkg->package->id;
Adam Lesinski4bf58102014-11-03 11:21:19 -08006893 char16_t tmpName[sizeof(pkg->package->name)/sizeof(pkg->package->name[0])];
6894 strcpy16_dtoh(tmpName, pkg->package->name, sizeof(pkg->package->name)/sizeof(pkg->package->name[0]));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006895 printf(" Package %d id=0x%02x name=%s\n", (int)pkgIndex,
Adam Lesinski4bf58102014-11-03 11:21:19 -08006896 pkg->package->id, String8(tmpName).string());
Adam Lesinski18560882014-08-15 17:18:21 +00006897 }
6898
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006899 for (size_t typeIndex=0; typeIndex < pg->types.size(); typeIndex++) {
6900 const TypeList& typeList = pg->types[typeIndex];
6901 if (typeList.isEmpty()) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006902 continue;
6903 }
6904 const Type* typeConfigs = typeList[0];
6905 const size_t NTC = typeConfigs->configs.size();
6906 printf(" type %d configCount=%d entryCount=%d\n",
6907 (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount);
6908 if (typeConfigs->typeSpecFlags != NULL) {
6909 for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) {
Adam Lesinski6022deb2014-08-20 14:59:19 -07006910 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006911 | (0x00ff0000 & ((typeIndex+1)<<16))
6912 | (0x0000ffff & (entryIndex));
6913 // Since we are creating resID without actually
6914 // iterating over them, we have no idea which is a
6915 // dynamic reference. We must check.
Adam Lesinski6022deb2014-08-20 14:59:19 -07006916 if (packageId == 0) {
6917 pg->dynamicRefTable.lookupResourceId(&resID);
6918 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006919
6920 resource_name resName;
6921 if (this->getResourceName(resID, true, &resName)) {
6922 String8 type8;
6923 String8 name8;
6924 if (resName.type8 != NULL) {
6925 type8 = String8(resName.type8, resName.typeLen);
6926 } else {
6927 type8 = String8(resName.type, resName.typeLen);
6928 }
6929 if (resName.name8 != NULL) {
6930 name8 = String8(resName.name8, resName.nameLen);
6931 } else {
6932 name8 = String8(resName.name, resName.nameLen);
6933 }
6934 printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n",
6935 resID,
6936 CHAR16_TO_CSTR(resName.package, resName.packageLen),
6937 type8.string(), name8.string(),
6938 dtohl(typeConfigs->typeSpecFlags[entryIndex]));
6939 } else {
6940 printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID);
6941 }
6942 }
6943 }
6944 for (size_t configIndex=0; configIndex<NTC; configIndex++) {
6945 const ResTable_type* type = typeConfigs->configs[configIndex];
6946 if ((((uint64_t)type)&0x3) != 0) {
6947 printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006948 continue;
6949 }
Adam Lesinski5b0f1be2015-07-27 16:53:14 -07006950
6951 // Always copy the config, as fields get added and we need to
6952 // set the defaults.
6953 ResTable_config thisConfig;
6954 thisConfig.copyFromDtoH(type->config);
6955
6956 String8 configStr = thisConfig.toString();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006957 printf(" config %s:\n", configStr.size() > 0
6958 ? configStr.string() : "(default)");
6959 size_t entryCount = dtohl(type->entryCount);
6960 uint32_t entriesStart = dtohl(type->entriesStart);
6961 if ((entriesStart&0x3) != 0) {
6962 printf(" NON-INTEGER ResTable_type entriesStart OFFSET: 0x%x\n", entriesStart);
6963 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006965 uint32_t typeSize = dtohl(type->header.size);
6966 if ((typeSize&0x3) != 0) {
6967 printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize);
6968 continue;
6969 }
6970 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006971 const uint32_t* const eindex = (const uint32_t*)
6972 (((const uint8_t*)type) + dtohs(type->header.headerSize));
6973
6974 uint32_t thisOffset = dtohl(eindex[entryIndex]);
6975 if (thisOffset == ResTable_type::NO_ENTRY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 continue;
6977 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006978
Adam Lesinski6022deb2014-08-20 14:59:19 -07006979 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006980 | (0x00ff0000 & ((typeIndex+1)<<16))
6981 | (0x0000ffff & (entryIndex));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006982 if (packageId == 0) {
6983 pg->dynamicRefTable.lookupResourceId(&resID);
6984 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006985 resource_name resName;
6986 if (this->getResourceName(resID, true, &resName)) {
6987 String8 type8;
6988 String8 name8;
6989 if (resName.type8 != NULL) {
6990 type8 = String8(resName.type8, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006991 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006992 type8 = String8(resName.type, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006993 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006994 if (resName.name8 != NULL) {
6995 name8 = String8(resName.name8, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006997 name8 = String8(resName.name, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006999 printf(" resource 0x%08x %s:%s/%s: ", resID,
7000 CHAR16_TO_CSTR(resName.package, resName.packageLen),
7001 type8.string(), name8.string());
7002 } else {
7003 printf(" INVALID RESOURCE 0x%08x: ", resID);
7004 }
7005 if ((thisOffset&0x3) != 0) {
7006 printf("NON-INTEGER OFFSET: 0x%x\n", thisOffset);
7007 continue;
7008 }
7009 if ((thisOffset+sizeof(ResTable_entry)) > typeSize) {
7010 printf("OFFSET OUT OF BOUNDS: 0x%x+0x%x (size is 0x%x)\n",
7011 entriesStart, thisOffset, typeSize);
7012 continue;
7013 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07007014
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007015 const ResTable_entry* ent = (const ResTable_entry*)
7016 (((const uint8_t*)type) + entriesStart + thisOffset);
7017 if (((entriesStart + thisOffset)&0x3) != 0) {
7018 printf("NON-INTEGER ResTable_entry OFFSET: 0x%x\n",
7019 (entriesStart + thisOffset));
7020 continue;
7021 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07007022
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007023 uintptr_t esize = dtohs(ent->size);
7024 if ((esize&0x3) != 0) {
7025 printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void *)esize);
7026 continue;
7027 }
7028 if ((thisOffset+esize) > typeSize) {
7029 printf("ResTable_entry OUT OF BOUNDS: 0x%x+0x%x+%p (size is 0x%x)\n",
7030 entriesStart, thisOffset, (void *)esize, typeSize);
7031 continue;
7032 }
7033
7034 const Res_value* valuePtr = NULL;
7035 const ResTable_map_entry* bagPtr = NULL;
7036 Res_value value;
7037 if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) {
7038 printf("<bag>");
7039 bagPtr = (const ResTable_map_entry*)ent;
7040 } else {
7041 valuePtr = (const Res_value*)
7042 (((const uint8_t*)ent) + esize);
7043 value.copyFrom_dtoh(*valuePtr);
7044 printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)",
7045 (int)value.dataType, (int)value.data,
7046 (int)value.size, (int)value.res0);
7047 }
7048
7049 if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) {
7050 printf(" (PUBLIC)");
7051 }
7052 printf("\n");
7053
7054 if (inclValues) {
7055 if (valuePtr != NULL) {
7056 printf(" ");
7057 print_value(typeConfigs->package, value);
7058 } else if (bagPtr != NULL) {
7059 const int N = dtohl(bagPtr->count);
7060 const uint8_t* baseMapPtr = (const uint8_t*)ent;
7061 size_t mapOffset = esize;
7062 const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
7063 const uint32_t parent = dtohl(bagPtr->parent.ident);
7064 uint32_t resolvedParent = parent;
Adam Lesinski6022deb2014-08-20 14:59:19 -07007065 if (Res_GETPACKAGE(resolvedParent) + 1 == 0) {
7066 status_t err = pg->dynamicRefTable.lookupResourceId(&resolvedParent);
7067 if (err != NO_ERROR) {
7068 resolvedParent = 0;
7069 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07007070 }
7071 printf(" Parent=0x%08x(Resolved=0x%08x), Count=%d\n",
7072 parent, resolvedParent, N);
7073 for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) {
7074 printf(" #%i (Key=0x%08x): ",
7075 i, dtohl(mapPtr->name.ident));
7076 value.copyFrom_dtoh(mapPtr->value);
7077 print_value(typeConfigs->package, value);
7078 const size_t size = dtohs(mapPtr->value.size);
7079 mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value);
7080 mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
Dianne Hackborne17086b2009-06-19 15:13:28 -07007081 }
7082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 }
7084 }
7085 }
7086 }
7087 }
7088}
7089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090} // namespace android