2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "ResourceType"
18 //#define LOG_NDEBUG 0
30 #include <type_traits>
32 #include <androidfw/ByteBucketArray.h>
33 #include <androidfw/ResourceTypes.h>
34 #include <androidfw/TypeWrappers.h>
35 #include <utils/Atomic.h>
36 #include <utils/ByteOrder.h>
37 #include <utils/Debug.h>
38 #include <utils/Log.h>
39 #include <utils/String16.h>
40 #include <utils/String8.h>
43 #include <binder/TextOutput.h>
47 #define INT32_MAX ((int32_t)(2147483647))
55 #define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
56 #define htonl(x) ntohl(x)
57 #define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
58 #define htons(x) ntohs(x)
61 #define IDMAP_MAGIC 0x504D4449
62 #define IDMAP_CURRENT_VERSION 0x00000001
64 #define APP_PACKAGE_ID 0x7f
65 #define SYS_PACKAGE_ID 0x01
67 static const bool kDebugStringPoolNoisy = false;
68 static const bool kDebugXMLNoisy = false;
69 static const bool kDebugTableNoisy = false;
70 static const bool kDebugTableGetEntry = false;
71 static const bool kDebugTableSuperNoisy = false;
72 static const bool kDebugLoadTableNoisy = false;
73 static const bool kDebugLoadTableSuperNoisy = false;
74 static const bool kDebugTableTheme = false;
75 static const bool kDebugResXMLTree = false;
76 static const bool kDebugLibNoisy = false;
78 // TODO: This code uses 0xFFFFFFFF converted to bag_set* as a sentinel value. This is bad practice.
80 // Standard C isspace() is only required to look at the low byte of its input, so
81 // produces incorrect results for UTF-16 characters. For safety's sake, assume that
82 // any high-byte UTF-16 code point is not whitespace.
83 inline int isspace16(char16_t c) {
84 return (c < 0x0080 && isspace(c));
88 inline static T max(T a, T b) {
92 // range checked; guaranteed to NUL-terminate within the stated number of available slots
93 // NOTE: if this truncates the dst string due to running out of space, no attempt is
94 // made to avoid splitting surrogate pairs.
95 static void strcpy16_dtoh(char16_t* dst, const uint16_t* src, size_t avail)
97 char16_t* last = dst + avail - 1;
98 while (*src && (dst < last)) {
99 char16_t s = dtohs(static_cast<char16_t>(*src));
106 static status_t validate_chunk(const ResChunk_header* chunk,
108 const uint8_t* dataEnd,
111 const uint16_t headerSize = dtohs(chunk->headerSize);
112 const uint32_t size = dtohl(chunk->size);
114 if (headerSize >= minSize) {
115 if (headerSize <= size) {
116 if (((headerSize|size)&0x3) == 0) {
117 if ((size_t)size <= (size_t)(dataEnd-((const uint8_t*)chunk))) {
120 ALOGW("%s data size 0x%x extends beyond resource end %p.",
121 name, size, (void*)(dataEnd-((const uint8_t*)chunk)));
124 ALOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.",
125 name, (int)size, (int)headerSize);
128 ALOGW("%s size 0x%x is smaller than header size 0x%x.",
129 name, size, headerSize);
132 ALOGW("%s header size 0x%04x is too small.",
137 static void fill9patchOffsets(Res_png_9patch* patch) {
138 patch->xDivsOffset = sizeof(Res_png_9patch);
139 patch->yDivsOffset = patch->xDivsOffset + (patch->numXDivs * sizeof(int32_t));
140 patch->colorsOffset = patch->yDivsOffset + (patch->numYDivs * sizeof(int32_t));
143 void Res_value::copyFrom_dtoh(const Res_value& src)
145 size = dtohs(src.size);
147 dataType = src.dataType;
148 data = dtohl(src.data);
151 void Res_png_9patch::deviceToFile()
153 int32_t* xDivs = getXDivs();
154 for (int i = 0; i < numXDivs; i++) {
155 xDivs[i] = htonl(xDivs[i]);
157 int32_t* yDivs = getYDivs();
158 for (int i = 0; i < numYDivs; i++) {
159 yDivs[i] = htonl(yDivs[i]);
161 paddingLeft = htonl(paddingLeft);
162 paddingRight = htonl(paddingRight);
163 paddingTop = htonl(paddingTop);
164 paddingBottom = htonl(paddingBottom);
165 uint32_t* colors = getColors();
166 for (int i=0; i<numColors; i++) {
167 colors[i] = htonl(colors[i]);
171 void Res_png_9patch::fileToDevice()
173 int32_t* xDivs = getXDivs();
174 for (int i = 0; i < numXDivs; i++) {
175 xDivs[i] = ntohl(xDivs[i]);
177 int32_t* yDivs = getYDivs();
178 for (int i = 0; i < numYDivs; i++) {
179 yDivs[i] = ntohl(yDivs[i]);
181 paddingLeft = ntohl(paddingLeft);
182 paddingRight = ntohl(paddingRight);
183 paddingTop = ntohl(paddingTop);
184 paddingBottom = ntohl(paddingBottom);
185 uint32_t* colors = getColors();
186 for (int i=0; i<numColors; i++) {
187 colors[i] = ntohl(colors[i]);
191 size_t Res_png_9patch::serializedSize() const
193 // The size of this struct is 32 bytes on the 32-bit target system
198 + numXDivs * sizeof(int32_t)
199 + numYDivs * sizeof(int32_t)
200 + numColors * sizeof(uint32_t);
203 void* Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
204 const int32_t* yDivs, const uint32_t* colors)
206 // Use calloc since we're going to leave a few holes in the data
207 // and want this to run cleanly under valgrind
208 void* newData = calloc(1, patch.serializedSize());
209 serialize(patch, xDivs, yDivs, colors, newData);
213 void Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
214 const int32_t* yDivs, const uint32_t* colors, void* outData)
216 uint8_t* data = (uint8_t*) outData;
217 memcpy(data, &patch.wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors
218 memcpy(data + 12, &patch.paddingLeft, 16); // copy paddingXXXX
221 memcpy(data, xDivs, patch.numXDivs * sizeof(int32_t));
222 data += patch.numXDivs * sizeof(int32_t);
223 memcpy(data, yDivs, patch.numYDivs * sizeof(int32_t));
224 data += patch.numYDivs * sizeof(int32_t);
225 memcpy(data, colors, patch.numColors * sizeof(uint32_t));
227 fill9patchOffsets(reinterpret_cast<Res_png_9patch*>(outData));
230 static bool assertIdmapHeader(const void* idmap, size_t size) {
231 if (reinterpret_cast<uintptr_t>(idmap) & 0x03) {
232 ALOGE("idmap: header is not word aligned");
236 if (size < ResTable::IDMAP_HEADER_SIZE_BYTES) {
237 ALOGW("idmap: header too small (%d bytes)", (uint32_t) size);
241 const uint32_t magic = htodl(*reinterpret_cast<const uint32_t*>(idmap));
242 if (magic != IDMAP_MAGIC) {
243 ALOGW("idmap: no magic found in header (is 0x%08x, expected 0x%08x)",
248 const uint32_t version = htodl(*(reinterpret_cast<const uint32_t*>(idmap) + 1));
249 if (version != IDMAP_CURRENT_VERSION) {
250 // We are strict about versions because files with this format are
251 // auto-generated and don't need backwards compatibility.
252 ALOGW("idmap: version mismatch in header (is 0x%08x, expected 0x%08x)",
253 version, IDMAP_CURRENT_VERSION);
261 IdmapEntries() : mData(NULL) {}
263 bool hasEntries() const {
268 return (dtohs(*mData) > 0);
271 size_t byteSize() const {
275 uint16_t entryCount = dtohs(mData[2]);
276 return (sizeof(uint16_t) * 4) + (sizeof(uint32_t) * static_cast<size_t>(entryCount));
279 uint8_t targetTypeId() const {
283 return dtohs(mData[0]);
286 uint8_t overlayTypeId() const {
290 return dtohs(mData[1]);
293 status_t setTo(const void* entryHeader, size_t size) {
294 if (reinterpret_cast<uintptr_t>(entryHeader) & 0x03) {
295 ALOGE("idmap: entry header is not word aligned");
296 return UNKNOWN_ERROR;
299 if (size < sizeof(uint16_t) * 4) {
300 ALOGE("idmap: entry header is too small (%u bytes)", (uint32_t) size);
301 return UNKNOWN_ERROR;
304 const uint16_t* header = reinterpret_cast<const uint16_t*>(entryHeader);
305 const uint16_t targetTypeId = dtohs(header[0]);
306 const uint16_t overlayTypeId = dtohs(header[1]);
307 if (targetTypeId == 0 || overlayTypeId == 0 || targetTypeId > 255 || overlayTypeId > 255) {
308 ALOGE("idmap: invalid type map (%u -> %u)", targetTypeId, overlayTypeId);
309 return UNKNOWN_ERROR;
312 uint16_t entryCount = dtohs(header[2]);
313 if (size < sizeof(uint32_t) * (entryCount + 2)) {
314 ALOGE("idmap: too small (%u bytes) for the number of entries (%u)",
315 (uint32_t) size, (uint32_t) entryCount);
316 return UNKNOWN_ERROR;
322 status_t lookup(uint16_t entryId, uint16_t* outEntryId) const {
323 uint16_t entryCount = dtohs(mData[2]);
324 uint16_t offset = dtohs(mData[3]);
326 if (entryId < offset) {
327 // The entry is not present in this idmap
333 if (entryId >= entryCount) {
334 // The entry is not present in this idmap
338 // It is safe to access the type here without checking the size because
339 // we have checked this when it was first loaded.
340 const uint32_t* entries = reinterpret_cast<const uint32_t*>(mData) + 2;
341 uint32_t mappedEntry = dtohl(entries[entryId]);
342 if (mappedEntry == 0xffffffff) {
343 // This entry is not present in this idmap
346 *outEntryId = static_cast<uint16_t>(mappedEntry);
351 const uint16_t* mData;
354 status_t parseIdmap(const void* idmap, size_t size, uint8_t* outPackageId, KeyedVector<uint8_t, IdmapEntries>* outMap) {
355 if (!assertIdmapHeader(idmap, size)) {
356 return UNKNOWN_ERROR;
359 size -= ResTable::IDMAP_HEADER_SIZE_BYTES;
360 if (size < sizeof(uint16_t) * 2) {
361 ALOGE("idmap: too small to contain any mapping");
362 return UNKNOWN_ERROR;
365 const uint16_t* data = reinterpret_cast<const uint16_t*>(
366 reinterpret_cast<const uint8_t*>(idmap) + ResTable::IDMAP_HEADER_SIZE_BYTES);
368 uint16_t targetPackageId = dtohs(*(data++));
369 if (targetPackageId == 0 || targetPackageId > 255) {
370 ALOGE("idmap: target package ID is invalid (%02x)", targetPackageId);
371 return UNKNOWN_ERROR;
374 uint16_t mapCount = dtohs(*(data++));
376 ALOGE("idmap: no mappings");
377 return UNKNOWN_ERROR;
380 if (mapCount > 255) {
381 ALOGW("idmap: too many mappings. Only 255 are possible but %u are present", (uint32_t) mapCount);
384 while (size > sizeof(uint16_t) * 4) {
385 IdmapEntries entries;
386 status_t err = entries.setTo(data, size);
387 if (err != NO_ERROR) {
391 ssize_t index = outMap->add(entries.overlayTypeId(), entries);
396 data += entries.byteSize() / sizeof(uint16_t);
397 size -= entries.byteSize();
400 if (outPackageId != NULL) {
401 *outPackageId = static_cast<uint8_t>(targetPackageId);
406 Res_png_9patch* Res_png_9patch::deserialize(void* inData)
409 Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(inData);
410 patch->wasDeserialized = true;
411 fill9patchOffsets(patch);
416 // --------------------------------------------------------------------
417 // --------------------------------------------------------------------
418 // --------------------------------------------------------------------
420 ResStringPool::ResStringPool()
421 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
425 ResStringPool::ResStringPool(const void* data, size_t size, bool copyData)
426 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
428 setTo(data, size, copyData);
431 ResStringPool::~ResStringPool()
436 void ResStringPool::setToEmpty()
440 mOwnedData = calloc(1, sizeof(ResStringPool_header));
441 ResStringPool_header* header = (ResStringPool_header*) mOwnedData;
449 mHeader = (const ResStringPool_header*) header;
452 status_t ResStringPool::setTo(const void* data, size_t size, bool copyData)
454 if (!data || !size) {
455 return (mError=BAD_TYPE);
460 // The chunk must be at least the size of the string pool header.
461 if (size < sizeof(ResStringPool_header)) {
462 LOG_ALWAYS_FATAL("Bad string block: data size %zu is too small to be a string block", size);
463 return (mError=BAD_TYPE);
466 // The data is at least as big as a ResChunk_header, so we can safely validate the other
468 // `data + size` is safe because the source of `size` comes from the kernel/filesystem.
469 if (validate_chunk(reinterpret_cast<const ResChunk_header*>(data), sizeof(ResStringPool_header),
470 reinterpret_cast<const uint8_t*>(data) + size,
471 "ResStringPool_header") != NO_ERROR) {
472 LOG_ALWAYS_FATAL("Bad string block: malformed block dimensions");
473 return (mError=BAD_TYPE);
476 const bool notDeviceEndian = htods(0xf0) != 0xf0;
478 if (copyData || notDeviceEndian) {
479 mOwnedData = malloc(size);
480 if (mOwnedData == NULL) {
481 return (mError=NO_MEMORY);
483 memcpy(mOwnedData, data, size);
487 // The size has been checked, so it is safe to read the data in the ResStringPool_header
489 mHeader = (const ResStringPool_header*)data;
491 if (notDeviceEndian) {
492 ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader);
493 h->header.headerSize = dtohs(mHeader->header.headerSize);
494 h->header.type = dtohs(mHeader->header.type);
495 h->header.size = dtohl(mHeader->header.size);
496 h->stringCount = dtohl(mHeader->stringCount);
497 h->styleCount = dtohl(mHeader->styleCount);
498 h->flags = dtohl(mHeader->flags);
499 h->stringsStart = dtohl(mHeader->stringsStart);
500 h->stylesStart = dtohl(mHeader->stylesStart);
503 if (mHeader->header.headerSize > mHeader->header.size
504 || mHeader->header.size > size) {
505 ALOGW("Bad string block: header size %d or total size %d is larger than data size %d\n",
506 (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size);
507 return (mError=BAD_TYPE);
509 mSize = mHeader->header.size;
510 mEntries = (const uint32_t*)
511 (((const uint8_t*)data)+mHeader->header.headerSize);
513 if (mHeader->stringCount > 0) {
514 if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow?
515 || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t)))
517 ALOGW("Bad string block: entry of %d items extends past data size %d\n",
518 (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))),
520 return (mError=BAD_TYPE);
524 if (mHeader->flags&ResStringPool_header::UTF8_FLAG) {
525 charSize = sizeof(uint8_t);
527 charSize = sizeof(uint16_t);
530 // There should be at least space for the smallest string
531 // (2 bytes length, null terminator).
532 if (mHeader->stringsStart >= (mSize - sizeof(uint16_t))) {
533 ALOGW("Bad string block: string pool starts at %d, after total size %d\n",
534 (int)mHeader->stringsStart, (int)mHeader->header.size);
535 return (mError=BAD_TYPE);
538 mStrings = (const void*)
539 (((const uint8_t*)data) + mHeader->stringsStart);
541 if (mHeader->styleCount == 0) {
542 mStringPoolSize = (mSize - mHeader->stringsStart) / charSize;
544 // check invariant: styles starts before end of data
545 if (mHeader->stylesStart >= (mSize - sizeof(uint16_t))) {
546 ALOGW("Bad style block: style block starts at %d past data size of %d\n",
547 (int)mHeader->stylesStart, (int)mHeader->header.size);
548 return (mError=BAD_TYPE);
550 // check invariant: styles follow the strings
551 if (mHeader->stylesStart <= mHeader->stringsStart) {
552 ALOGW("Bad style block: style block starts at %d, before strings at %d\n",
553 (int)mHeader->stylesStart, (int)mHeader->stringsStart);
554 return (mError=BAD_TYPE);
557 (mHeader->stylesStart-mHeader->stringsStart)/charSize;
560 // check invariant: stringCount > 0 requires a string pool to exist
561 if (mStringPoolSize == 0) {
562 ALOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount);
563 return (mError=BAD_TYPE);
566 if (notDeviceEndian) {
568 uint32_t* e = const_cast<uint32_t*>(mEntries);
569 for (i=0; i<mHeader->stringCount; i++) {
570 e[i] = dtohl(mEntries[i]);
572 if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) {
573 const uint16_t* strings = (const uint16_t*)mStrings;
574 uint16_t* s = const_cast<uint16_t*>(strings);
575 for (i=0; i<mStringPoolSize; i++) {
576 s[i] = dtohs(strings[i]);
581 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG &&
582 ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) ||
583 (!(mHeader->flags&ResStringPool_header::UTF8_FLAG) &&
584 ((uint16_t*)mStrings)[mStringPoolSize-1] != 0)) {
585 ALOGW("Bad string block: last string is not 0-terminated\n");
586 return (mError=BAD_TYPE);
593 if (mHeader->styleCount > 0) {
594 mEntryStyles = mEntries + mHeader->stringCount;
595 // invariant: integer overflow in calculating mEntryStyles
596 if (mEntryStyles < mEntries) {
597 ALOGW("Bad string block: integer overflow finding styles\n");
598 return (mError=BAD_TYPE);
601 if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) {
602 ALOGW("Bad string block: entry of %d styles extends past data size %d\n",
603 (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader),
605 return (mError=BAD_TYPE);
607 mStyles = (const uint32_t*)
608 (((const uint8_t*)data)+mHeader->stylesStart);
609 if (mHeader->stylesStart >= mHeader->header.size) {
610 ALOGW("Bad string block: style pool starts %d, after total size %d\n",
611 (int)mHeader->stylesStart, (int)mHeader->header.size);
612 return (mError=BAD_TYPE);
615 (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t);
617 if (notDeviceEndian) {
619 uint32_t* e = const_cast<uint32_t*>(mEntryStyles);
620 for (i=0; i<mHeader->styleCount; i++) {
621 e[i] = dtohl(mEntryStyles[i]);
623 uint32_t* s = const_cast<uint32_t*>(mStyles);
624 for (i=0; i<mStylePoolSize; i++) {
625 s[i] = dtohl(mStyles[i]);
629 const ResStringPool_span endSpan = {
630 { htodl(ResStringPool_span::END) },
631 htodl(ResStringPool_span::END), htodl(ResStringPool_span::END)
633 if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))],
634 &endSpan, sizeof(endSpan)) != 0) {
635 ALOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n");
636 return (mError=BAD_TYPE);
644 return (mError=NO_ERROR);
647 status_t ResStringPool::getError() const
652 void ResStringPool::uninit()
655 if (mHeader != NULL && mCache != NULL) {
656 for (size_t x = 0; x < mHeader->stringCount; x++) {
657 if (mCache[x] != NULL) {
672 * Strings in UTF-16 format have length indicated by a length encoded in the
673 * stored data. It is either 1 or 2 characters of length data. This allows a
674 * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that
675 * much data in a string, you're abusing them.
677 * If the high bit is set, then there are two characters or 4 bytes of length
678 * data encoded. In that case, drop the high bit of the first character and
679 * add it together with the next character.
682 decodeLength(const uint16_t** str)
685 if ((len & 0x8000) != 0) {
687 len = ((len & 0x7FFF) << 16) | **str;
694 * Strings in UTF-8 format have length indicated by a length encoded in the
695 * stored data. It is either 1 or 2 characters of length data. This allows a
696 * maximum length of 0x7FFF (32767 bytes), but you should consider storing
697 * text in another way if you're using that much data in a single string.
699 * If the high bit is set, then there are two characters or 2 bytes of length
700 * data encoded. In that case, drop the high bit of the first character and
701 * add it together with the next character.
704 decodeLength(const uint8_t** str)
707 if ((len & 0x80) != 0) {
709 len = ((len & 0x7F) << 8) | **str;
715 const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const
717 if (mError == NO_ERROR && idx < mHeader->stringCount) {
718 const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
719 const uint32_t off = mEntries[idx]/(isUTF8?sizeof(uint8_t):sizeof(uint16_t));
720 if (off < (mStringPoolSize-1)) {
722 const uint16_t* strings = (uint16_t*)mStrings;
723 const uint16_t* str = strings+off;
725 *u16len = decodeLength(&str);
726 if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) {
727 // Reject malformed (non null-terminated) strings
728 if (str[*u16len] != 0x0000) {
729 ALOGW("Bad string block: string #%d is not null-terminated",
733 return reinterpret_cast<const char16_t*>(str);
735 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
736 (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize);
739 const uint8_t* strings = (uint8_t*)mStrings;
740 const uint8_t* u8str = strings+off;
742 *u16len = decodeLength(&u8str);
743 size_t u8len = decodeLength(&u8str);
745 // encLen must be less than 0x7FFF due to encoding.
746 if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) {
747 AutoMutex lock(mDecodeLock);
749 if (mCache == NULL) {
751 if (kDebugStringPoolNoisy) {
752 ALOGI("CREATING STRING CACHE OF %zu bytes",
753 mHeader->stringCount*sizeof(char16_t**));
756 // We do not want to be in this case when actually running Android.
757 ALOGW("CREATING STRING CACHE OF %zu bytes",
758 static_cast<size_t>(mHeader->stringCount*sizeof(char16_t**)));
760 mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t*));
761 if (mCache == NULL) {
762 ALOGW("No memory trying to allocate decode cache table of %d bytes\n",
763 (int)(mHeader->stringCount*sizeof(char16_t**)));
768 if (mCache[idx] != NULL) {
772 ssize_t actualLen = utf8_to_utf16_length(u8str, u8len);
773 if (actualLen < 0 || (size_t)actualLen != *u16len) {
774 ALOGW("Bad string block: string #%lld decoded length is not correct "
776 (long long)idx, (long long)actualLen, (long long)*u16len);
780 // Reject malformed (non null-terminated) strings
781 if (u8str[u8len] != 0x00) {
782 ALOGW("Bad string block: string #%d is not null-terminated",
787 char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t));
789 ALOGW("No memory when trying to allocate decode cache for string #%d\n",
794 if (kDebugStringPoolNoisy) {
795 ALOGI("Caching UTF8 string: %s", u8str);
797 utf8_to_utf16(u8str, u8len, u16str, *u16len + 1);
798 mCache[idx] = u16str;
801 ALOGW("Bad string block: string #%lld extends to %lld, past end at %lld\n",
802 (long long)idx, (long long)(u8str+u8len-strings),
803 (long long)mStringPoolSize);
807 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
808 (int)idx, (int)(off*sizeof(uint16_t)),
809 (int)(mStringPoolSize*sizeof(uint16_t)));
815 const char* ResStringPool::string8At(size_t idx, size_t* outLen) const
817 if (mError == NO_ERROR && idx < mHeader->stringCount) {
818 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) == 0) {
821 const uint32_t off = mEntries[idx]/sizeof(char);
822 if (off < (mStringPoolSize-1)) {
823 const uint8_t* strings = (uint8_t*)mStrings;
824 const uint8_t* str = strings+off;
826 // Decode the UTF-16 length. This is not used if we're not
827 // converting to UTF-16 from UTF-8.
830 const size_t encLen = decodeLength(&str);
833 if ((uint32_t)(str+encLen-strings) < mStringPoolSize) {
834 // Reject malformed (non null-terminated) strings
835 if (str[encLen] != 0x00) {
836 ALOGW("Bad string block: string #%d is not null-terminated",
840 return (const char*)str;
842 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
843 (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize);
846 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
847 (int)idx, (int)(off*sizeof(uint16_t)),
848 (int)(mStringPoolSize*sizeof(uint16_t)));
854 const String8 ResStringPool::string8ObjectAt(size_t idx) const
857 const char *str = string8At(idx, &len);
859 return String8(str, len);
862 const char16_t *str16 = stringAt(idx, &len);
864 return String8(str16, len);
869 const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const
871 return styleAt(ref.index);
874 const ResStringPool_span* ResStringPool::styleAt(size_t idx) const
876 if (mError == NO_ERROR && idx < mHeader->styleCount) {
877 const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t));
878 if (off < mStylePoolSize) {
879 return (const ResStringPool_span*)(mStyles+off);
881 ALOGW("Bad string block: style #%d entry is at %d, past end at %d\n",
882 (int)idx, (int)(off*sizeof(uint32_t)),
883 (int)(mStylePoolSize*sizeof(uint32_t)));
889 ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
891 if (mError != NO_ERROR) {
897 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) {
898 if (kDebugStringPoolNoisy) {
899 ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string());
902 // The string pool contains UTF 8 strings; we don't want to cause
903 // temporary UTF-16 strings to be created as we search.
904 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
905 // Do a binary search for the string... this is a little tricky,
906 // because the strings are sorted with strzcmp16(). So to match
907 // the ordering, we need to convert strings in the pool to UTF-16.
908 // But we don't want to hit the cache, so instead we will have a
909 // local temporary allocation for the conversions.
910 size_t convBufferLen = strLen + 4;
911 char16_t* convBuffer = (char16_t*)calloc(convBufferLen, sizeof(char16_t));
913 ssize_t h = mHeader->stringCount-1;
918 const uint8_t* s = (const uint8_t*)string8At(mid, &len);
921 char16_t* end = utf8_to_utf16(s, len, convBuffer, convBufferLen);
922 c = strzcmp16(convBuffer, end-convBuffer, str, strLen);
926 if (kDebugStringPoolNoisy) {
927 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
928 (const char*)s, c, (int)l, (int)mid, (int)h);
931 if (kDebugStringPoolNoisy) {
944 // It is unusual to get the ID from an unsorted string block...
945 // most often this happens because we want to get IDs for style
946 // span tags; since those always appear at the end of the string
947 // block, start searching at the back.
948 String8 str8(str, strLen);
949 const size_t str8Len = str8.size();
950 for (int i=mHeader->stringCount-1; i>=0; i--) {
951 const char* s = string8At(i, &len);
952 if (kDebugStringPoolNoisy) {
953 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
955 if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) {
956 if (kDebugStringPoolNoisy) {
965 if (kDebugStringPoolNoisy) {
966 ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string());
969 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
970 // Do a binary search for the string...
972 ssize_t h = mHeader->stringCount-1;
977 const char16_t* s = stringAt(mid, &len);
978 int c = s ? strzcmp16(s, len, str, strLen) : -1;
979 if (kDebugStringPoolNoisy) {
980 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
981 String8(s).string(), c, (int)l, (int)mid, (int)h);
984 if (kDebugStringPoolNoisy) {
995 // It is unusual to get the ID from an unsorted string block...
996 // most often this happens because we want to get IDs for style
997 // span tags; since those always appear at the end of the string
998 // block, start searching at the back.
999 for (int i=mHeader->stringCount-1; i>=0; i--) {
1000 const char16_t* s = stringAt(i, &len);
1001 if (kDebugStringPoolNoisy) {
1002 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
1004 if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) {
1005 if (kDebugStringPoolNoisy) {
1014 return NAME_NOT_FOUND;
1017 size_t ResStringPool::size() const
1019 return (mError == NO_ERROR) ? mHeader->stringCount : 0;
1022 size_t ResStringPool::styleCount() const
1024 return (mError == NO_ERROR) ? mHeader->styleCount : 0;
1027 size_t ResStringPool::bytes() const
1029 return (mError == NO_ERROR) ? mHeader->header.size : 0;
1032 bool ResStringPool::isSorted() const
1034 return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0;
1037 bool ResStringPool::isUTF8() const
1039 return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0;
1042 // --------------------------------------------------------------------
1043 // --------------------------------------------------------------------
1044 // --------------------------------------------------------------------
1046 ResXMLParser::ResXMLParser(const ResXMLTree& tree)
1047 : mTree(tree), mEventCode(BAD_DOCUMENT)
1051 void ResXMLParser::restart()
1054 mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT;
1056 const ResStringPool& ResXMLParser::getStrings() const
1058 return mTree.mStrings;
1061 ResXMLParser::event_code_t ResXMLParser::getEventType() const
1066 ResXMLParser::event_code_t ResXMLParser::next()
1068 if (mEventCode == START_DOCUMENT) {
1069 mCurNode = mTree.mRootNode;
1070 mCurExt = mTree.mRootExt;
1071 return (mEventCode=mTree.mRootCode);
1072 } else if (mEventCode >= FIRST_CHUNK_CODE) {
1078 int32_t ResXMLParser::getCommentID() const
1080 return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1;
1083 const char16_t* ResXMLParser::getComment(size_t* outLen) const
1085 int32_t id = getCommentID();
1086 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1089 uint32_t ResXMLParser::getLineNumber() const
1091 return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1;
1094 int32_t ResXMLParser::getTextID() const
1096 if (mEventCode == TEXT) {
1097 return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index);
1102 const char16_t* ResXMLParser::getText(size_t* outLen) const
1104 int32_t id = getTextID();
1105 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1108 ssize_t ResXMLParser::getTextValue(Res_value* outValue) const
1110 if (mEventCode == TEXT) {
1111 outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData);
1112 return sizeof(Res_value);
1117 int32_t ResXMLParser::getNamespacePrefixID() const
1119 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1120 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index);
1125 const char16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const
1127 int32_t id = getNamespacePrefixID();
1128 //printf("prefix=%d event=%p\n", id, mEventCode);
1129 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1132 int32_t ResXMLParser::getNamespaceUriID() const
1134 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1135 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index);
1140 const char16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const
1142 int32_t id = getNamespaceUriID();
1143 //printf("uri=%d event=%p\n", id, mEventCode);
1144 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1147 int32_t ResXMLParser::getElementNamespaceID() const
1149 if (mEventCode == START_TAG) {
1150 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index);
1152 if (mEventCode == END_TAG) {
1153 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index);
1158 const char16_t* ResXMLParser::getElementNamespace(size_t* outLen) const
1160 int32_t id = getElementNamespaceID();
1161 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1164 int32_t ResXMLParser::getElementNameID() const
1166 if (mEventCode == START_TAG) {
1167 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index);
1169 if (mEventCode == END_TAG) {
1170 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index);
1175 const char16_t* ResXMLParser::getElementName(size_t* outLen) const
1177 int32_t id = getElementNameID();
1178 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1181 size_t ResXMLParser::getAttributeCount() const
1183 if (mEventCode == START_TAG) {
1184 return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount);
1189 int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
1191 if (mEventCode == START_TAG) {
1192 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1193 if (idx < dtohs(tag->attributeCount)) {
1194 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1195 (((const uint8_t*)tag)
1196 + dtohs(tag->attributeStart)
1197 + (dtohs(tag->attributeSize)*idx));
1198 return dtohl(attr->ns.index);
1204 const char16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const
1206 int32_t id = getAttributeNamespaceID(idx);
1207 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
1208 if (kDebugXMLNoisy) {
1209 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1211 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1214 const char* ResXMLParser::getAttributeNamespace8(size_t idx, size_t* outLen) const
1216 int32_t id = getAttributeNamespaceID(idx);
1217 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
1218 if (kDebugXMLNoisy) {
1219 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1221 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1224 int32_t ResXMLParser::getAttributeNameID(size_t idx) const
1226 if (mEventCode == START_TAG) {
1227 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1228 if (idx < dtohs(tag->attributeCount)) {
1229 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1230 (((const uint8_t*)tag)
1231 + dtohs(tag->attributeStart)
1232 + (dtohs(tag->attributeSize)*idx));
1233 return dtohl(attr->name.index);
1239 const char16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const
1241 int32_t id = getAttributeNameID(idx);
1242 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
1243 if (kDebugXMLNoisy) {
1244 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1246 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1249 const char* ResXMLParser::getAttributeName8(size_t idx, size_t* outLen) const
1251 int32_t id = getAttributeNameID(idx);
1252 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
1253 if (kDebugXMLNoisy) {
1254 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1256 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1259 uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
1261 int32_t id = getAttributeNameID(idx);
1262 if (id >= 0 && (size_t)id < mTree.mNumResIds) {
1263 uint32_t resId = dtohl(mTree.mResIds[id]);
1264 if (mTree.mDynamicRefTable != NULL) {
1265 mTree.mDynamicRefTable->lookupResourceId(&resId);
1272 int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
1274 if (mEventCode == START_TAG) {
1275 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1276 if (idx < dtohs(tag->attributeCount)) {
1277 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1278 (((const uint8_t*)tag)
1279 + dtohs(tag->attributeStart)
1280 + (dtohs(tag->attributeSize)*idx));
1281 return dtohl(attr->rawValue.index);
1287 const char16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const
1289 int32_t id = getAttributeValueStringID(idx);
1290 if (kDebugXMLNoisy) {
1291 printf("getAttributeValue 0x%zx=0x%x\n", idx, id);
1293 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1296 int32_t ResXMLParser::getAttributeDataType(size_t idx) const
1298 if (mEventCode == START_TAG) {
1299 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1300 if (idx < dtohs(tag->attributeCount)) {
1301 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1302 (((const uint8_t*)tag)
1303 + dtohs(tag->attributeStart)
1304 + (dtohs(tag->attributeSize)*idx));
1305 uint8_t type = attr->typedValue.dataType;
1306 if (type != Res_value::TYPE_DYNAMIC_REFERENCE) {
1310 // This is a dynamic reference. We adjust those references
1311 // to regular references at this level, so lie to the caller.
1312 return Res_value::TYPE_REFERENCE;
1315 return Res_value::TYPE_NULL;
1318 int32_t ResXMLParser::getAttributeData(size_t idx) const
1320 if (mEventCode == START_TAG) {
1321 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1322 if (idx < dtohs(tag->attributeCount)) {
1323 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1324 (((const uint8_t*)tag)
1325 + dtohs(tag->attributeStart)
1326 + (dtohs(tag->attributeSize)*idx));
1327 if (attr->typedValue.dataType != Res_value::TYPE_DYNAMIC_REFERENCE ||
1328 mTree.mDynamicRefTable == NULL) {
1329 return dtohl(attr->typedValue.data);
1332 uint32_t data = dtohl(attr->typedValue.data);
1333 if (mTree.mDynamicRefTable->lookupResourceId(&data) == NO_ERROR) {
1341 ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const
1343 if (mEventCode == START_TAG) {
1344 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1345 if (idx < dtohs(tag->attributeCount)) {
1346 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1347 (((const uint8_t*)tag)
1348 + dtohs(tag->attributeStart)
1349 + (dtohs(tag->attributeSize)*idx));
1350 outValue->copyFrom_dtoh(attr->typedValue);
1351 if (mTree.mDynamicRefTable != NULL &&
1352 mTree.mDynamicRefTable->lookupResourceValue(outValue) != NO_ERROR) {
1355 return sizeof(Res_value);
1361 ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const
1363 String16 nsStr(ns != NULL ? ns : "");
1364 String16 attrStr(attr);
1365 return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0,
1366 attrStr.string(), attrStr.size());
1369 ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen,
1370 const char16_t* attr, size_t attrLen) const
1372 if (mEventCode == START_TAG) {
1374 return NAME_NOT_FOUND;
1376 const size_t N = getAttributeCount();
1377 if (mTree.mStrings.isUTF8()) {
1380 ns8 = String8(ns, nsLen);
1382 attr8 = String8(attr, attrLen);
1383 if (kDebugStringPoolNoisy) {
1384 ALOGI("indexOfAttribute UTF8 %s (%zu) / %s (%zu)", ns8.string(), nsLen,
1385 attr8.string(), attrLen);
1387 for (size_t i=0; i<N; i++) {
1388 size_t curNsLen = 0, curAttrLen = 0;
1389 const char* curNs = getAttributeNamespace8(i, &curNsLen);
1390 const char* curAttr = getAttributeName8(i, &curAttrLen);
1391 if (kDebugStringPoolNoisy) {
1392 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)", curNs, curNsLen, curAttr, curAttrLen);
1394 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1395 && memcmp(attr8.string(), curAttr, attrLen) == 0) {
1397 if (curNs == NULL) {
1398 if (kDebugStringPoolNoisy) {
1403 } else if (curNs != NULL) {
1404 //printf(" --> ns=%s, curNs=%s\n",
1405 // String8(ns).string(), String8(curNs).string());
1406 if (memcmp(ns8.string(), curNs, nsLen) == 0) {
1407 if (kDebugStringPoolNoisy) {
1416 if (kDebugStringPoolNoisy) {
1417 ALOGI("indexOfAttribute UTF16 %s (%zu) / %s (%zu)",
1418 String8(ns, nsLen).string(), nsLen,
1419 String8(attr, attrLen).string(), attrLen);
1421 for (size_t i=0; i<N; i++) {
1422 size_t curNsLen = 0, curAttrLen = 0;
1423 const char16_t* curNs = getAttributeNamespace(i, &curNsLen);
1424 const char16_t* curAttr = getAttributeName(i, &curAttrLen);
1425 if (kDebugStringPoolNoisy) {
1426 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)",
1427 String8(curNs, curNsLen).string(), curNsLen,
1428 String8(curAttr, curAttrLen).string(), curAttrLen);
1430 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1431 && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) {
1433 if (curNs == NULL) {
1434 if (kDebugStringPoolNoisy) {
1439 } else if (curNs != NULL) {
1440 //printf(" --> ns=%s, curNs=%s\n",
1441 // String8(ns).string(), String8(curNs).string());
1442 if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) {
1443 if (kDebugStringPoolNoisy) {
1454 return NAME_NOT_FOUND;
1457 ssize_t ResXMLParser::indexOfID() const
1459 if (mEventCode == START_TAG) {
1460 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex);
1461 if (idx > 0) return (idx-1);
1463 return NAME_NOT_FOUND;
1466 ssize_t ResXMLParser::indexOfClass() const
1468 if (mEventCode == START_TAG) {
1469 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex);
1470 if (idx > 0) return (idx-1);
1472 return NAME_NOT_FOUND;
1475 ssize_t ResXMLParser::indexOfStyle() const
1477 if (mEventCode == START_TAG) {
1478 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex);
1479 if (idx > 0) return (idx-1);
1481 return NAME_NOT_FOUND;
1484 ResXMLParser::event_code_t ResXMLParser::nextNode()
1486 if (mEventCode < 0) {
1491 const ResXMLTree_node* next = (const ResXMLTree_node*)
1492 (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size));
1493 if (kDebugXMLNoisy) {
1494 ALOGI("Next node: prev=%p, next=%p\n", mCurNode, next);
1497 if (((const uint8_t*)next) >= mTree.mDataEnd) {
1499 return (mEventCode=END_DOCUMENT);
1502 if (mTree.validateNode(next) != NO_ERROR) {
1504 return (mEventCode=BAD_DOCUMENT);
1508 const uint16_t headerSize = dtohs(next->header.headerSize);
1509 const uint32_t totalSize = dtohl(next->header.size);
1510 mCurExt = ((const uint8_t*)next) + headerSize;
1511 size_t minExtSize = 0;
1512 event_code_t eventCode = (event_code_t)dtohs(next->header.type);
1513 switch ((mEventCode=eventCode)) {
1514 case RES_XML_START_NAMESPACE_TYPE:
1515 case RES_XML_END_NAMESPACE_TYPE:
1516 minExtSize = sizeof(ResXMLTree_namespaceExt);
1518 case RES_XML_START_ELEMENT_TYPE:
1519 minExtSize = sizeof(ResXMLTree_attrExt);
1521 case RES_XML_END_ELEMENT_TYPE:
1522 minExtSize = sizeof(ResXMLTree_endElementExt);
1524 case RES_XML_CDATA_TYPE:
1525 minExtSize = sizeof(ResXMLTree_cdataExt);
1528 ALOGW("Unknown XML block: header type %d in node at %d\n",
1529 (int)dtohs(next->header.type),
1530 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)));
1534 if ((totalSize-headerSize) < minExtSize) {
1535 ALOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n",
1536 (int)dtohs(next->header.type),
1537 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)),
1538 (int)(totalSize-headerSize), (int)minExtSize);
1539 return (mEventCode=BAD_DOCUMENT);
1542 //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n",
1543 // mCurNode, mCurExt, headerSize, minExtSize);
1549 void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const
1551 pos->eventCode = mEventCode;
1552 pos->curNode = mCurNode;
1553 pos->curExt = mCurExt;
1556 void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos)
1558 mEventCode = pos.eventCode;
1559 mCurNode = pos.curNode;
1560 mCurExt = pos.curExt;
1563 // --------------------------------------------------------------------
1565 static volatile int32_t gCount = 0;
1567 ResXMLTree::ResXMLTree(const DynamicRefTable* dynamicRefTable)
1568 : ResXMLParser(*this)
1569 , mDynamicRefTable(dynamicRefTable)
1570 , mError(NO_INIT), mOwnedData(NULL)
1572 if (kDebugResXMLTree) {
1573 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1578 ResXMLTree::ResXMLTree()
1579 : ResXMLParser(*this)
1580 , mDynamicRefTable(NULL)
1581 , mError(NO_INIT), mOwnedData(NULL)
1583 if (kDebugResXMLTree) {
1584 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1589 ResXMLTree::~ResXMLTree()
1591 if (kDebugResXMLTree) {
1592 ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
1597 status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData)
1600 mEventCode = START_DOCUMENT;
1602 if (!data || !size) {
1603 return (mError=BAD_TYPE);
1607 mOwnedData = malloc(size);
1608 if (mOwnedData == NULL) {
1609 return (mError=NO_MEMORY);
1611 memcpy(mOwnedData, data, size);
1615 mHeader = (const ResXMLTree_header*)data;
1616 mSize = dtohl(mHeader->header.size);
1617 if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) {
1618 ALOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n",
1619 (int)dtohs(mHeader->header.headerSize),
1620 (int)dtohl(mHeader->header.size), (int)size);
1625 mDataEnd = ((const uint8_t*)mHeader) + mSize;
1632 // First look for a couple interesting chunks: the string block
1633 // and first XML node.
1634 const ResChunk_header* chunk =
1635 (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize));
1636 const ResChunk_header* lastChunk = chunk;
1637 while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) &&
1638 ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) {
1639 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML");
1640 if (err != NO_ERROR) {
1644 const uint16_t type = dtohs(chunk->type);
1645 const size_t size = dtohl(chunk->size);
1646 if (kDebugXMLNoisy) {
1647 printf("Scanning @ %p: type=0x%x, size=0x%zx\n",
1648 (void*)(((uintptr_t)chunk)-((uintptr_t)mHeader)), type, size);
1650 if (type == RES_STRING_POOL_TYPE) {
1651 mStrings.setTo(chunk, size);
1652 } else if (type == RES_XML_RESOURCE_MAP_TYPE) {
1653 mResIds = (const uint32_t*)
1654 (((const uint8_t*)chunk)+dtohs(chunk->headerSize));
1655 mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t);
1656 } else if (type >= RES_XML_FIRST_CHUNK_TYPE
1657 && type <= RES_XML_LAST_CHUNK_TYPE) {
1658 if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) {
1662 mCurNode = (const ResXMLTree_node*)lastChunk;
1663 if (nextNode() == BAD_DOCUMENT) {
1667 mRootNode = mCurNode;
1669 mRootCode = mEventCode;
1672 if (kDebugXMLNoisy) {
1673 printf("Skipping unknown chunk!\n");
1677 chunk = (const ResChunk_header*)
1678 (((const uint8_t*)chunk) + size);
1681 if (mRootNode == NULL) {
1682 ALOGW("Bad XML block: no root element node found\n");
1687 mError = mStrings.getError();
1694 status_t ResXMLTree::getError() const
1699 void ResXMLTree::uninit()
1710 status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const
1712 const uint16_t eventCode = dtohs(node->header.type);
1714 status_t err = validate_chunk(
1715 &node->header, sizeof(ResXMLTree_node),
1716 mDataEnd, "ResXMLTree_node");
1718 if (err >= NO_ERROR) {
1719 // Only perform additional validation on START nodes
1720 if (eventCode != RES_XML_START_ELEMENT_TYPE) {
1724 const uint16_t headerSize = dtohs(node->header.headerSize);
1725 const uint32_t size = dtohl(node->header.size);
1726 const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*)
1727 (((const uint8_t*)node) + headerSize);
1728 // check for sensical values pulled out of the stream so far...
1729 if ((size >= headerSize + sizeof(ResXMLTree_attrExt))
1730 && ((void*)attrExt > (void*)node)) {
1731 const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize))
1732 * dtohs(attrExt->attributeCount);
1733 if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) {
1736 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
1737 (unsigned int)(dtohs(attrExt->attributeStart)+attrSize),
1738 (unsigned int)(size-headerSize));
1741 ALOGW("Bad XML start block: node header size 0x%x, size 0x%x\n",
1742 (unsigned int)headerSize, (unsigned int)size);
1750 const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE;
1752 const uint16_t headerSize = dtohs(node->header.headerSize);
1753 const uint32_t size = dtohl(node->header.size);
1755 if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) {
1756 if (size >= headerSize) {
1757 if (((const uint8_t*)node) <= (mDataEnd-size)) {
1761 if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount))
1762 <= (size-headerSize)) {
1765 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
1766 ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount),
1767 (int)(size-headerSize));
1770 ALOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n",
1771 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize);
1774 ALOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n",
1775 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1776 (int)headerSize, (int)size);
1779 ALOGW("Bad XML block: node at 0x%x header size 0x%x too small\n",
1780 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1786 // --------------------------------------------------------------------
1787 // --------------------------------------------------------------------
1788 // --------------------------------------------------------------------
1790 void ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) {
1791 const size_t size = dtohl(o.size);
1792 if (size >= sizeof(ResTable_config)) {
1795 memcpy(this, &o, size);
1796 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
1800 /* static */ size_t unpackLanguageOrRegion(const char in[2], const char base,
1803 // The high bit is "1", which means this is a packed three letter
1806 // The smallest 5 bits of the second char are the first alphabet.
1807 const uint8_t first = in[1] & 0x1f;
1808 // The last three bits of the second char and the first two bits
1809 // of the first char are the second alphabet.
1810 const uint8_t second = ((in[1] & 0xe0) >> 5) + ((in[0] & 0x03) << 3);
1811 // Bits 3 to 7 (inclusive) of the first char are the third alphabet.
1812 const uint8_t third = (in[0] & 0x7c) >> 2;
1814 out[0] = first + base;
1815 out[1] = second + base;
1816 out[2] = third + base;
1824 memset(out + 2, 0, 2);
1832 /* static */ void packLanguageOrRegion(const char* in, const char base,
1834 if (in[2] == 0 || in[2] == '-') {
1838 uint8_t first = (in[0] - base) & 0x007f;
1839 uint8_t second = (in[1] - base) & 0x007f;
1840 uint8_t third = (in[2] - base) & 0x007f;
1842 out[0] = (0x80 | (third << 2) | (second >> 3));
1843 out[1] = ((second << 5) | first);
1848 void ResTable_config::packLanguage(const char* language) {
1849 packLanguageOrRegion(language, 'a', this->language);
1852 void ResTable_config::packRegion(const char* region) {
1853 packLanguageOrRegion(region, '0', this->country);
1856 size_t ResTable_config::unpackLanguage(char language[4]) const {
1857 return unpackLanguageOrRegion(this->language, 'a', language);
1860 size_t ResTable_config::unpackRegion(char region[4]) const {
1861 return unpackLanguageOrRegion(this->country, '0', region);
1865 void ResTable_config::copyFromDtoH(const ResTable_config& o) {
1866 copyFromDeviceNoSwap(o);
1867 size = sizeof(ResTable_config);
1870 density = dtohs(density);
1871 screenWidth = dtohs(screenWidth);
1872 screenHeight = dtohs(screenHeight);
1873 sdkVersion = dtohs(sdkVersion);
1874 minorVersion = dtohs(minorVersion);
1875 smallestScreenWidthDp = dtohs(smallestScreenWidthDp);
1876 screenWidthDp = dtohs(screenWidthDp);
1877 screenHeightDp = dtohs(screenHeightDp);
1880 void ResTable_config::swapHtoD() {
1884 density = htods(density);
1885 screenWidth = htods(screenWidth);
1886 screenHeight = htods(screenHeight);
1887 sdkVersion = htods(sdkVersion);
1888 minorVersion = htods(minorVersion);
1889 smallestScreenWidthDp = htods(smallestScreenWidthDp);
1890 screenWidthDp = htods(screenWidthDp);
1891 screenHeightDp = htods(screenHeightDp);
1894 /* static */ inline int compareLocales(const ResTable_config &l, const ResTable_config &r) {
1895 if (l.locale != r.locale) {
1896 // NOTE: This is the old behaviour with respect to comparison orders.
1897 // The diff value here doesn't make much sense (given our bit packing scheme)
1898 // but it's stable, and that's all we need.
1899 return l.locale - r.locale;
1902 // The language & region are equal, so compare the scripts and variants.
1903 const char emptyScript[sizeof(l.localeScript)] = {'\0', '\0', '\0', '\0'};
1904 const char *lScript = l.localeScriptWasComputed ? emptyScript : l.localeScript;
1905 const char *rScript = r.localeScriptWasComputed ? emptyScript : r.localeScript;
1906 int script = memcmp(lScript, rScript, sizeof(l.localeScript));
1911 // The language, region and script are equal, so compare variants.
1913 // This should happen very infrequently (if at all.)
1914 return memcmp(l.localeVariant, r.localeVariant, sizeof(l.localeVariant));
1917 int ResTable_config::compare(const ResTable_config& o) const {
1918 int32_t diff = (int32_t)(imsi - o.imsi);
1919 if (diff != 0) return diff;
1920 diff = compareLocales(*this, o);
1921 if (diff != 0) return diff;
1922 diff = (int32_t)(screenType - o.screenType);
1923 if (diff != 0) return diff;
1924 diff = (int32_t)(input - o.input);
1925 if (diff != 0) return diff;
1926 diff = (int32_t)(screenSize - o.screenSize);
1927 if (diff != 0) return diff;
1928 diff = (int32_t)(version - o.version);
1929 if (diff != 0) return diff;
1930 diff = (int32_t)(screenLayout - o.screenLayout);
1931 if (diff != 0) return diff;
1932 diff = (int32_t)(screenLayout2 - o.screenLayout2);
1933 if (diff != 0) return diff;
1934 diff = (int32_t)(colorMode - o.colorMode);
1935 if (diff != 0) return diff;
1936 diff = (int32_t)(uiMode - o.uiMode);
1937 if (diff != 0) return diff;
1938 diff = (int32_t)(smallestScreenWidthDp - o.smallestScreenWidthDp);
1939 if (diff != 0) return diff;
1940 diff = (int32_t)(screenSizeDp - o.screenSizeDp);
1944 int ResTable_config::compareLogical(const ResTable_config& o) const {
1946 return mcc < o.mcc ? -1 : 1;
1949 return mnc < o.mnc ? -1 : 1;
1952 int diff = compareLocales(*this, o);
1960 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) {
1961 return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1;
1963 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
1964 return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1;
1966 if (screenWidthDp != o.screenWidthDp) {
1967 return screenWidthDp < o.screenWidthDp ? -1 : 1;
1969 if (screenHeightDp != o.screenHeightDp) {
1970 return screenHeightDp < o.screenHeightDp ? -1 : 1;
1972 if (screenWidth != o.screenWidth) {
1973 return screenWidth < o.screenWidth ? -1 : 1;
1975 if (screenHeight != o.screenHeight) {
1976 return screenHeight < o.screenHeight ? -1 : 1;
1978 if (density != o.density) {
1979 return density < o.density ? -1 : 1;
1981 if (orientation != o.orientation) {
1982 return orientation < o.orientation ? -1 : 1;
1984 if (touchscreen != o.touchscreen) {
1985 return touchscreen < o.touchscreen ? -1 : 1;
1987 if (input != o.input) {
1988 return input < o.input ? -1 : 1;
1990 if (screenLayout != o.screenLayout) {
1991 return screenLayout < o.screenLayout ? -1 : 1;
1993 if (screenLayout2 != o.screenLayout2) {
1994 return screenLayout2 < o.screenLayout2 ? -1 : 1;
1996 if (colorMode != o.colorMode) {
1997 return colorMode < o.colorMode ? -1 : 1;
1999 if (uiMode != o.uiMode) {
2000 return uiMode < o.uiMode ? -1 : 1;
2002 if (version != o.version) {
2003 return version < o.version ? -1 : 1;
2008 int ResTable_config::diff(const ResTable_config& o) const {
2010 if (mcc != o.mcc) diffs |= CONFIG_MCC;
2011 if (mnc != o.mnc) diffs |= CONFIG_MNC;
2012 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
2013 if (density != o.density) diffs |= CONFIG_DENSITY;
2014 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
2015 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
2016 diffs |= CONFIG_KEYBOARD_HIDDEN;
2017 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
2018 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
2019 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
2020 if (version != o.version) diffs |= CONFIG_VERSION;
2021 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) diffs |= CONFIG_LAYOUTDIR;
2022 if ((screenLayout & ~MASK_LAYOUTDIR) != (o.screenLayout & ~MASK_LAYOUTDIR)) diffs |= CONFIG_SCREEN_LAYOUT;
2023 if ((screenLayout2 & MASK_SCREENROUND) != (o.screenLayout2 & MASK_SCREENROUND)) diffs |= CONFIG_SCREEN_ROUND;
2024 if ((colorMode & MASK_WIDE_COLOR_GAMUT) != (o.colorMode & MASK_WIDE_COLOR_GAMUT)) diffs |= CONFIG_COLOR_MODE;
2025 if ((colorMode & MASK_HDR) != (o.colorMode & MASK_HDR)) diffs |= CONFIG_COLOR_MODE;
2026 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
2027 if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE;
2028 if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE;
2030 const int diff = compareLocales(*this, o);
2031 if (diff) diffs |= CONFIG_LOCALE;
2036 int ResTable_config::isLocaleMoreSpecificThan(const ResTable_config& o) const {
2037 if (locale || o.locale) {
2038 if (language[0] != o.language[0]) {
2039 if (!language[0]) return -1;
2040 if (!o.language[0]) return 1;
2043 if (country[0] != o.country[0]) {
2044 if (!country[0]) return -1;
2045 if (!o.country[0]) return 1;
2049 // There isn't a well specified "importance" order between variants and
2050 // scripts. We can't easily tell whether, say "en-Latn-US" is more or less
2051 // specific than "en-US-POSIX".
2053 // We therefore arbitrarily decide to give priority to variants over
2054 // scripts since it seems more useful to do so. We will consider
2055 // "en-US-POSIX" to be more specific than "en-Latn-US".
2057 const int score = ((localeScript[0] != '\0' && !localeScriptWasComputed) ? 1 : 0) +
2058 ((localeVariant[0] != '\0') ? 2 : 0);
2060 const int oScore = (o.localeScript[0] != '\0' && !o.localeScriptWasComputed ? 1 : 0) +
2061 ((o.localeVariant[0] != '\0') ? 2 : 0);
2063 return score - oScore;
2066 bool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const {
2067 // The order of the following tests defines the importance of one
2068 // configuration parameter over another. Those tests first are more
2069 // important, trumping any values in those following them.
2070 if (imsi || o.imsi) {
2072 if (!mcc) return false;
2073 if (!o.mcc) return true;
2077 if (!mnc) return false;
2078 if (!o.mnc) return true;
2082 if (locale || o.locale) {
2083 const int diff = isLocaleMoreSpecificThan(o);
2093 if (screenLayout || o.screenLayout) {
2094 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) {
2095 if (!(screenLayout & MASK_LAYOUTDIR)) return false;
2096 if (!(o.screenLayout & MASK_LAYOUTDIR)) return true;
2100 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2101 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2102 if (!smallestScreenWidthDp) return false;
2103 if (!o.smallestScreenWidthDp) return true;
2107 if (screenSizeDp || o.screenSizeDp) {
2108 if (screenWidthDp != o.screenWidthDp) {
2109 if (!screenWidthDp) return false;
2110 if (!o.screenWidthDp) return true;
2113 if (screenHeightDp != o.screenHeightDp) {
2114 if (!screenHeightDp) return false;
2115 if (!o.screenHeightDp) return true;
2119 if (screenLayout || o.screenLayout) {
2120 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
2121 if (!(screenLayout & MASK_SCREENSIZE)) return false;
2122 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
2124 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
2125 if (!(screenLayout & MASK_SCREENLONG)) return false;
2126 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
2130 if (screenLayout2 || o.screenLayout2) {
2131 if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0) {
2132 if (!(screenLayout2 & MASK_SCREENROUND)) return false;
2133 if (!(o.screenLayout2 & MASK_SCREENROUND)) return true;
2137 if (colorMode || o.colorMode) {
2138 if (((colorMode^o.colorMode) & MASK_HDR) != 0) {
2139 if (!(colorMode & MASK_HDR)) return false;
2140 if (!(o.colorMode & MASK_HDR)) return true;
2142 if (((colorMode^o.colorMode) & MASK_WIDE_COLOR_GAMUT) != 0) {
2143 if (!(colorMode & MASK_WIDE_COLOR_GAMUT)) return false;
2144 if (!(o.colorMode & MASK_WIDE_COLOR_GAMUT)) return true;
2148 if (orientation != o.orientation) {
2149 if (!orientation) return false;
2150 if (!o.orientation) return true;
2153 if (uiMode || o.uiMode) {
2154 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
2155 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
2156 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
2158 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
2159 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
2160 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
2164 // density is never 'more specific'
2165 // as the default just equals 160
2167 if (touchscreen != o.touchscreen) {
2168 if (!touchscreen) return false;
2169 if (!o.touchscreen) return true;
2172 if (input || o.input) {
2173 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
2174 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
2175 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
2178 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
2179 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
2180 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
2183 if (keyboard != o.keyboard) {
2184 if (!keyboard) return false;
2185 if (!o.keyboard) return true;
2188 if (navigation != o.navigation) {
2189 if (!navigation) return false;
2190 if (!o.navigation) return true;
2194 if (screenSize || o.screenSize) {
2195 if (screenWidth != o.screenWidth) {
2196 if (!screenWidth) return false;
2197 if (!o.screenWidth) return true;
2200 if (screenHeight != o.screenHeight) {
2201 if (!screenHeight) return false;
2202 if (!o.screenHeight) return true;
2206 if (version || o.version) {
2207 if (sdkVersion != o.sdkVersion) {
2208 if (!sdkVersion) return false;
2209 if (!o.sdkVersion) return true;
2212 if (minorVersion != o.minorVersion) {
2213 if (!minorVersion) return false;
2214 if (!o.minorVersion) return true;
2220 // Codes for specially handled languages and regions
2221 static const char kEnglish[2] = {'e', 'n'}; // packed version of "en"
2222 static const char kUnitedStates[2] = {'U', 'S'}; // packed version of "US"
2223 static const char kFilipino[2] = {'\xAD', '\x05'}; // packed version of "fil"
2224 static const char kTagalog[2] = {'t', 'l'}; // packed version of "tl"
2226 // Checks if two language or region codes are identical
2227 inline bool areIdentical(const char code1[2], const char code2[2]) {
2228 return code1[0] == code2[0] && code1[1] == code2[1];
2231 inline bool langsAreEquivalent(const char lang1[2], const char lang2[2]) {
2232 return areIdentical(lang1, lang2) ||
2233 (areIdentical(lang1, kTagalog) && areIdentical(lang2, kFilipino)) ||
2234 (areIdentical(lang1, kFilipino) && areIdentical(lang2, kTagalog));
2237 bool ResTable_config::isLocaleBetterThan(const ResTable_config& o,
2238 const ResTable_config* requested) const {
2239 if (requested->locale == 0) {
2240 // The request doesn't have a locale, so no resource is better
2245 if (locale == 0 && o.locale == 0) {
2246 // The locale part of both resources is empty, so none is better
2251 // Non-matching locales have been filtered out, so both resources
2252 // match the requested locale.
2254 // Because of the locale-related checks in match() and the checks, we know
2256 // 1) The resource languages are either empty or match the request;
2258 // 2) If the request's script is known, the resource scripts are either
2259 // unknown or match the request.
2261 if (!langsAreEquivalent(language, o.language)) {
2262 // The languages of the two resources are not equivalent. If we are
2263 // here, we can only assume that the two resources matched the request
2264 // because one doesn't have a language and the other has a matching
2267 // We consider the one that has the language specified a better match.
2269 // The exception is that we consider no-language resources a better match
2270 // for US English and similar locales than locales that are a descendant
2271 // of Internatinal English (en-001), since no-language resources are
2272 // where the US English resource have traditionally lived for most apps.
2273 if (areIdentical(requested->language, kEnglish)) {
2274 if (areIdentical(requested->country, kUnitedStates)) {
2275 // For US English itself, we consider a no-locale resource a
2276 // better match if the other resource has a country other than
2278 if (language[0] != '\0') {
2279 return country[0] == '\0' || areIdentical(country, kUnitedStates);
2281 return !(o.country[0] == '\0' || areIdentical(o.country, kUnitedStates));
2283 } else if (localeDataIsCloseToUsEnglish(requested->country)) {
2284 if (language[0] != '\0') {
2285 return localeDataIsCloseToUsEnglish(country);
2287 return !localeDataIsCloseToUsEnglish(o.country);
2291 return (language[0] != '\0');
2294 // If we are here, both the resources have an equivalent non-empty language
2297 // Because the languages are equivalent, computeScript() always returns a
2298 // non-empty script for languages it knows about, and we have passed the
2299 // script checks in match(), the scripts are either all unknown or are all
2300 // the same. So we can't gain anything by checking the scripts. We need to
2301 // check the region and variant.
2303 // See if any of the regions is better than the other.
2304 const int region_comparison = localeDataCompareRegions(
2306 requested->language, requested->localeScript, requested->country);
2307 if (region_comparison != 0) {
2308 return (region_comparison > 0);
2311 // The regions are the same. Try the variant.
2312 const bool localeMatches = strncmp(
2313 localeVariant, requested->localeVariant, sizeof(localeVariant)) == 0;
2314 const bool otherMatches = strncmp(
2315 o.localeVariant, requested->localeVariant, sizeof(localeVariant)) == 0;
2316 if (localeMatches != otherMatches) {
2317 return localeMatches;
2320 // Finally, the languages, although equivalent, may still be different
2321 // (like for Tagalog and Filipino). Identical is better than just
2323 if (areIdentical(language, requested->language)
2324 && !areIdentical(o.language, requested->language)) {
2331 bool ResTable_config::isBetterThan(const ResTable_config& o,
2332 const ResTable_config* requested) const {
2334 if (imsi || o.imsi) {
2335 if ((mcc != o.mcc) && requested->mcc) {
2339 if ((mnc != o.mnc) && requested->mnc) {
2344 if (isLocaleBetterThan(o, requested)) {
2348 if (screenLayout || o.screenLayout) {
2349 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0
2350 && (requested->screenLayout & MASK_LAYOUTDIR)) {
2351 int myLayoutDir = screenLayout & MASK_LAYOUTDIR;
2352 int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR;
2353 return (myLayoutDir > oLayoutDir);
2357 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2358 // The configuration closest to the actual size is best.
2359 // We assume that larger configs have already been filtered
2360 // out at this point. That means we just want the largest one.
2361 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2362 return smallestScreenWidthDp > o.smallestScreenWidthDp;
2366 if (screenSizeDp || o.screenSizeDp) {
2367 // "Better" is based on the sum of the difference between both
2368 // width and height from the requested dimensions. We are
2369 // assuming the invalid configs (with smaller dimens) have
2370 // already been filtered. Note that if a particular dimension
2371 // is unspecified, we will end up with a large value (the
2372 // difference between 0 and the requested dimension), which is
2373 // good since we will prefer a config that has specified a
2375 int myDelta = 0, otherDelta = 0;
2376 if (requested->screenWidthDp) {
2377 myDelta += requested->screenWidthDp - screenWidthDp;
2378 otherDelta += requested->screenWidthDp - o.screenWidthDp;
2380 if (requested->screenHeightDp) {
2381 myDelta += requested->screenHeightDp - screenHeightDp;
2382 otherDelta += requested->screenHeightDp - o.screenHeightDp;
2384 if (kDebugTableSuperNoisy) {
2385 ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
2386 screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
2387 requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
2389 if (myDelta != otherDelta) {
2390 return myDelta < otherDelta;
2394 if (screenLayout || o.screenLayout) {
2395 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
2396 && (requested->screenLayout & MASK_SCREENSIZE)) {
2397 // A little backwards compatibility here: undefined is
2398 // considered equivalent to normal. But only if the
2399 // requested size is at least normal; otherwise, small
2400 // is better than the default.
2401 int mySL = (screenLayout & MASK_SCREENSIZE);
2402 int oSL = (o.screenLayout & MASK_SCREENSIZE);
2403 int fixedMySL = mySL;
2405 if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
2406 if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
2407 if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
2409 // For screen size, the best match is the one that is
2410 // closest to the requested screen size, but not over
2411 // (the not over part is dealt with in match() below).
2412 if (fixedMySL == fixedOSL) {
2413 // If the two are the same, but 'this' is actually
2414 // undefined, then the other is really a better match.
2415 if (mySL == 0) return false;
2418 if (fixedMySL != fixedOSL) {
2419 return fixedMySL > fixedOSL;
2422 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
2423 && (requested->screenLayout & MASK_SCREENLONG)) {
2424 return (screenLayout & MASK_SCREENLONG);
2428 if (screenLayout2 || o.screenLayout2) {
2429 if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0 &&
2430 (requested->screenLayout2 & MASK_SCREENROUND)) {
2431 return screenLayout2 & MASK_SCREENROUND;
2435 if (colorMode || o.colorMode) {
2436 if (((colorMode^o.colorMode) & MASK_WIDE_COLOR_GAMUT) != 0 &&
2437 (requested->colorMode & MASK_WIDE_COLOR_GAMUT)) {
2438 return colorMode & MASK_WIDE_COLOR_GAMUT;
2440 if (((colorMode^o.colorMode) & MASK_HDR) != 0 &&
2441 (requested->colorMode & MASK_HDR)) {
2442 return colorMode & MASK_HDR;
2446 if ((orientation != o.orientation) && requested->orientation) {
2447 return (orientation);
2450 if (uiMode || o.uiMode) {
2451 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
2452 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
2453 return (uiMode & MASK_UI_MODE_TYPE);
2455 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
2456 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
2457 return (uiMode & MASK_UI_MODE_NIGHT);
2461 if (screenType || o.screenType) {
2462 if (density != o.density) {
2463 // Use the system default density (DENSITY_MEDIUM, 160dpi) if none specified.
2464 const int thisDensity = density ? density : int(ResTable_config::DENSITY_MEDIUM);
2465 const int otherDensity = o.density ? o.density : int(ResTable_config::DENSITY_MEDIUM);
2467 // We always prefer DENSITY_ANY over scaling a density bucket.
2468 if (thisDensity == ResTable_config::DENSITY_ANY) {
2470 } else if (otherDensity == ResTable_config::DENSITY_ANY) {
2474 int requestedDensity = requested->density;
2475 if (requested->density == 0 ||
2476 requested->density == ResTable_config::DENSITY_ANY) {
2477 requestedDensity = ResTable_config::DENSITY_MEDIUM;
2480 // DENSITY_ANY is now dealt with. We should look to
2481 // pick a density bucket and potentially scale it.
2482 // Any density is potentially useful
2483 // because the system will scale it. Scaling down
2484 // is generally better than scaling up.
2485 int h = thisDensity;
2486 int l = otherDensity;
2487 bool bImBigger = true;
2495 if (requestedDensity >= h) {
2496 // requested value higher than both l and h, give h
2499 if (l >= requestedDensity) {
2500 // requested value lower than both l and h, give l
2503 // saying that scaling down is 2x better than up
2504 if (((2 * l) - requestedDensity) * h > requestedDensity * requestedDensity) {
2511 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
2512 return (touchscreen);
2516 if (input || o.input) {
2517 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
2518 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
2519 if (keysHidden != oKeysHidden) {
2520 const int reqKeysHidden =
2521 requested->inputFlags & MASK_KEYSHIDDEN;
2522 if (reqKeysHidden) {
2524 if (!keysHidden) return false;
2525 if (!oKeysHidden) return true;
2526 // For compatibility, we count KEYSHIDDEN_NO as being
2527 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
2528 // these by making an exact match more specific.
2529 if (reqKeysHidden == keysHidden) return true;
2530 if (reqKeysHidden == oKeysHidden) return false;
2534 const int navHidden = inputFlags & MASK_NAVHIDDEN;
2535 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
2536 if (navHidden != oNavHidden) {
2537 const int reqNavHidden =
2538 requested->inputFlags & MASK_NAVHIDDEN;
2541 if (!navHidden) return false;
2542 if (!oNavHidden) return true;
2546 if ((keyboard != o.keyboard) && requested->keyboard) {
2550 if ((navigation != o.navigation) && requested->navigation) {
2551 return (navigation);
2555 if (screenSize || o.screenSize) {
2556 // "Better" is based on the sum of the difference between both
2557 // width and height from the requested dimensions. We are
2558 // assuming the invalid configs (with smaller sizes) have
2559 // already been filtered. Note that if a particular dimension
2560 // is unspecified, we will end up with a large value (the
2561 // difference between 0 and the requested dimension), which is
2562 // good since we will prefer a config that has specified a
2564 int myDelta = 0, otherDelta = 0;
2565 if (requested->screenWidth) {
2566 myDelta += requested->screenWidth - screenWidth;
2567 otherDelta += requested->screenWidth - o.screenWidth;
2569 if (requested->screenHeight) {
2570 myDelta += requested->screenHeight - screenHeight;
2571 otherDelta += requested->screenHeight - o.screenHeight;
2573 if (myDelta != otherDelta) {
2574 return myDelta < otherDelta;
2578 if (version || o.version) {
2579 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
2580 return (sdkVersion > o.sdkVersion);
2583 if ((minorVersion != o.minorVersion) &&
2584 requested->minorVersion) {
2585 return (minorVersion);
2591 return isMoreSpecificThan(o);
2594 bool ResTable_config::match(const ResTable_config& settings) const {
2596 if (mcc != 0 && mcc != settings.mcc) {
2599 if (mnc != 0 && mnc != settings.mnc) {
2604 // Don't consider country and variants when deciding matches.
2605 // (Theoretically, the variant can also affect the script. For
2606 // example, "ar-alalc97" probably implies the Latin script, but since
2607 // CLDR doesn't support getting likely scripts for that, we'll assume
2608 // the variant doesn't change the script.)
2610 // If two configs differ only in their country and variant,
2611 // they can be weeded out in the isMoreSpecificThan test.
2612 if (!langsAreEquivalent(language, settings.language)) {
2616 // For backward compatibility and supporting private-use locales, we
2617 // fall back to old behavior if we couldn't determine the script for
2618 // either of the desired locale or the provided locale. But if we could determine
2619 // the scripts, they should be the same for the locales to match.
2620 bool countriesMustMatch = false;
2621 char computed_script[4];
2623 if (settings.localeScript[0] == '\0') { // could not determine the request's script
2624 countriesMustMatch = true;
2626 if (localeScript[0] == '\0' && !localeScriptWasComputed) {
2627 // script was not provided or computed, so we try to compute it
2628 localeDataComputeScript(computed_script, language, country);
2629 if (computed_script[0] == '\0') { // we could not compute the script
2630 countriesMustMatch = true;
2632 script = computed_script;
2634 } else { // script was provided, so just use it
2635 script = localeScript;
2639 if (countriesMustMatch) {
2640 if (country[0] != '\0' && !areIdentical(country, settings.country)) {
2644 if (memcmp(script, settings.localeScript, sizeof(settings.localeScript)) != 0) {
2650 if (screenConfig != 0) {
2651 const int layoutDir = screenLayout&MASK_LAYOUTDIR;
2652 const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR;
2653 if (layoutDir != 0 && layoutDir != setLayoutDir) {
2657 const int screenSize = screenLayout&MASK_SCREENSIZE;
2658 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
2659 // Any screen sizes for larger screens than the setting do not
2661 if (screenSize != 0 && screenSize > setScreenSize) {
2665 const int screenLong = screenLayout&MASK_SCREENLONG;
2666 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
2667 if (screenLong != 0 && screenLong != setScreenLong) {
2671 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
2672 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
2673 if (uiModeType != 0 && uiModeType != setUiModeType) {
2677 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
2678 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
2679 if (uiModeNight != 0 && uiModeNight != setUiModeNight) {
2683 if (smallestScreenWidthDp != 0
2684 && smallestScreenWidthDp > settings.smallestScreenWidthDp) {
2689 if (screenConfig2 != 0) {
2690 const int screenRound = screenLayout2 & MASK_SCREENROUND;
2691 const int setScreenRound = settings.screenLayout2 & MASK_SCREENROUND;
2692 if (screenRound != 0 && screenRound != setScreenRound) {
2696 const int hdr = colorMode & MASK_HDR;
2697 const int setHdr = settings.colorMode & MASK_HDR;
2698 if (hdr != 0 && hdr != setHdr) {
2702 const int wideColorGamut = colorMode & MASK_WIDE_COLOR_GAMUT;
2703 const int setWideColorGamut = settings.colorMode & MASK_WIDE_COLOR_GAMUT;
2704 if (wideColorGamut != 0 && wideColorGamut != setWideColorGamut) {
2709 if (screenSizeDp != 0) {
2710 if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) {
2711 if (kDebugTableSuperNoisy) {
2712 ALOGI("Filtering out width %d in requested %d", screenWidthDp,
2713 settings.screenWidthDp);
2717 if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) {
2718 if (kDebugTableSuperNoisy) {
2719 ALOGI("Filtering out height %d in requested %d", screenHeightDp,
2720 settings.screenHeightDp);
2725 if (screenType != 0) {
2726 if (orientation != 0 && orientation != settings.orientation) {
2729 // density always matches - we can scale it. See isBetterThan
2730 if (touchscreen != 0 && touchscreen != settings.touchscreen) {
2735 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
2736 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
2737 if (keysHidden != 0 && keysHidden != setKeysHidden) {
2738 // For compatibility, we count a request for KEYSHIDDEN_NO as also
2739 // matching the more recent KEYSHIDDEN_SOFT. Basically
2740 // KEYSHIDDEN_NO means there is some kind of keyboard available.
2741 if (kDebugTableSuperNoisy) {
2742 ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
2744 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
2745 if (kDebugTableSuperNoisy) {
2751 const int navHidden = inputFlags&MASK_NAVHIDDEN;
2752 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
2753 if (navHidden != 0 && navHidden != setNavHidden) {
2756 if (keyboard != 0 && keyboard != settings.keyboard) {
2759 if (navigation != 0 && navigation != settings.navigation) {
2763 if (screenSize != 0) {
2764 if (screenWidth != 0 && screenWidth > settings.screenWidth) {
2767 if (screenHeight != 0 && screenHeight > settings.screenHeight) {
2772 if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) {
2775 if (minorVersion != 0 && minorVersion != settings.minorVersion) {
2782 void ResTable_config::appendDirLocale(String8& out) const {
2786 const bool scriptWasProvided = localeScript[0] != '\0' && !localeScriptWasComputed;
2787 if (!scriptWasProvided && !localeVariant[0]) {
2789 if (out.size() > 0) {
2794 size_t len = unpackLanguage(buf);
2795 out.append(buf, len);
2799 len = unpackRegion(buf);
2800 out.append(buf, len);
2805 // We are writing the modified BCP 47 tag.
2806 // It starts with 'b+' and uses '+' as a separator.
2808 if (out.size() > 0) {
2814 size_t len = unpackLanguage(buf);
2815 out.append(buf, len);
2817 if (scriptWasProvided) {
2819 out.append(localeScript, sizeof(localeScript));
2824 len = unpackRegion(buf);
2825 out.append(buf, len);
2828 if (localeVariant[0]) {
2830 out.append(localeVariant, strnlen(localeVariant, sizeof(localeVariant)));
2834 void ResTable_config::getBcp47Locale(char str[RESTABLE_MAX_LOCALE_LEN], bool canonicalize) const {
2835 memset(str, 0, RESTABLE_MAX_LOCALE_LEN);
2837 // This represents the "any" locale value, which has traditionally been
2838 // represented by the empty string.
2839 if (language[0] == '\0' && country[0] == '\0') {
2843 size_t charsWritten = 0;
2844 if (language[0] != '\0') {
2845 if (canonicalize && areIdentical(language, kTagalog)) {
2846 // Replace Tagalog with Filipino if we are canonicalizing
2847 str[0] = 'f'; str[1] = 'i'; str[2] = 'l'; str[3] = '\0'; // 3-letter code for Filipino
2850 charsWritten += unpackLanguage(str);
2854 if (localeScript[0] != '\0' && !localeScriptWasComputed) {
2855 if (charsWritten > 0) {
2856 str[charsWritten++] = '-';
2858 memcpy(str + charsWritten, localeScript, sizeof(localeScript));
2859 charsWritten += sizeof(localeScript);
2862 if (country[0] != '\0') {
2863 if (charsWritten > 0) {
2864 str[charsWritten++] = '-';
2866 charsWritten += unpackRegion(str + charsWritten);
2869 if (localeVariant[0] != '\0') {
2870 if (charsWritten > 0) {
2871 str[charsWritten++] = '-';
2873 memcpy(str + charsWritten, localeVariant, sizeof(localeVariant));
2877 /* static */ inline bool assignLocaleComponent(ResTable_config* config,
2878 const char* start, size_t size) {
2885 config->language[0] ? config->packRegion(start) : config->packLanguage(start);
2888 if ('0' <= start[0] && start[0] <= '9') {
2889 // this is a variant, so fall through
2891 config->localeScript[0] = toupper(start[0]);
2892 for (size_t i = 1; i < 4; ++i) {
2893 config->localeScript[i] = tolower(start[i]);
2901 for (size_t i = 0; i < size; ++i) {
2902 config->localeVariant[i] = tolower(start[i]);
2912 void ResTable_config::setBcp47Locale(const char* in) {
2914 memset(localeScript, 0, sizeof(localeScript));
2915 memset(localeVariant, 0, sizeof(localeVariant));
2917 const char* separator = in;
2918 const char* start = in;
2919 while ((separator = strchr(start, '-')) != NULL) {
2920 const size_t size = separator - start;
2921 if (!assignLocaleComponent(this, start, size)) {
2922 fprintf(stderr, "Invalid BCP-47 locale string: %s", in);
2925 start = (separator + 1);
2928 const size_t size = in + strlen(in) - start;
2929 assignLocaleComponent(this, start, size);
2930 localeScriptWasComputed = (localeScript[0] == '\0');
2931 if (localeScriptWasComputed) {
2936 String8 ResTable_config::toString() const {
2940 if (res.size() > 0) res.append("-");
2941 res.appendFormat("mcc%d", dtohs(mcc));
2944 if (res.size() > 0) res.append("-");
2945 res.appendFormat("mnc%d", dtohs(mnc));
2948 appendDirLocale(res);
2950 if ((screenLayout&MASK_LAYOUTDIR) != 0) {
2951 if (res.size() > 0) res.append("-");
2952 switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) {
2953 case ResTable_config::LAYOUTDIR_LTR:
2954 res.append("ldltr");
2956 case ResTable_config::LAYOUTDIR_RTL:
2957 res.append("ldrtl");
2960 res.appendFormat("layoutDir=%d",
2961 dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR));
2965 if (smallestScreenWidthDp != 0) {
2966 if (res.size() > 0) res.append("-");
2967 res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp));
2969 if (screenWidthDp != 0) {
2970 if (res.size() > 0) res.append("-");
2971 res.appendFormat("w%ddp", dtohs(screenWidthDp));
2973 if (screenHeightDp != 0) {
2974 if (res.size() > 0) res.append("-");
2975 res.appendFormat("h%ddp", dtohs(screenHeightDp));
2977 if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) {
2978 if (res.size() > 0) res.append("-");
2979 switch (screenLayout&ResTable_config::MASK_SCREENSIZE) {
2980 case ResTable_config::SCREENSIZE_SMALL:
2981 res.append("small");
2983 case ResTable_config::SCREENSIZE_NORMAL:
2984 res.append("normal");
2986 case ResTable_config::SCREENSIZE_LARGE:
2987 res.append("large");
2989 case ResTable_config::SCREENSIZE_XLARGE:
2990 res.append("xlarge");
2993 res.appendFormat("screenLayoutSize=%d",
2994 dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE));
2998 if ((screenLayout&MASK_SCREENLONG) != 0) {
2999 if (res.size() > 0) res.append("-");
3000 switch (screenLayout&ResTable_config::MASK_SCREENLONG) {
3001 case ResTable_config::SCREENLONG_NO:
3002 res.append("notlong");
3004 case ResTable_config::SCREENLONG_YES:
3008 res.appendFormat("screenLayoutLong=%d",
3009 dtohs(screenLayout&ResTable_config::MASK_SCREENLONG));
3013 if ((screenLayout2&MASK_SCREENROUND) != 0) {
3014 if (res.size() > 0) res.append("-");
3015 switch (screenLayout2&MASK_SCREENROUND) {
3016 case SCREENROUND_NO:
3017 res.append("notround");
3019 case SCREENROUND_YES:
3020 res.append("round");
3023 res.appendFormat("screenRound=%d", dtohs(screenLayout2&MASK_SCREENROUND));
3027 if ((colorMode&MASK_HDR) != 0) {
3028 if (res.size() > 0) res.append("-");
3029 switch (colorMode&MASK_HDR) {
3030 case ResTable_config::HDR_NO:
3031 res.append("lowdr");
3033 case ResTable_config::HDR_YES:
3034 res.append("highdr");
3037 res.appendFormat("hdr=%d", dtohs(colorMode&MASK_HDR));
3041 if ((colorMode&MASK_WIDE_COLOR_GAMUT) != 0) {
3042 if (res.size() > 0) res.append("-");
3043 switch (colorMode&MASK_WIDE_COLOR_GAMUT) {
3044 case ResTable_config::WIDE_COLOR_GAMUT_NO:
3045 res.append("nowidecg");
3047 case ResTable_config::WIDE_COLOR_GAMUT_YES:
3048 res.append("widecg");
3051 res.appendFormat("wideColorGamut=%d", dtohs(colorMode&MASK_WIDE_COLOR_GAMUT));
3055 if (orientation != ORIENTATION_ANY) {
3056 if (res.size() > 0) res.append("-");
3057 switch (orientation) {
3058 case ResTable_config::ORIENTATION_PORT:
3061 case ResTable_config::ORIENTATION_LAND:
3064 case ResTable_config::ORIENTATION_SQUARE:
3065 res.append("square");
3068 res.appendFormat("orientation=%d", dtohs(orientation));
3072 if ((uiMode&MASK_UI_MODE_TYPE) != UI_MODE_TYPE_ANY) {
3073 if (res.size() > 0) res.append("-");
3074 switch (uiMode&ResTable_config::MASK_UI_MODE_TYPE) {
3075 case ResTable_config::UI_MODE_TYPE_DESK:
3078 case ResTable_config::UI_MODE_TYPE_CAR:
3081 case ResTable_config::UI_MODE_TYPE_TELEVISION:
3082 res.append("television");
3084 case ResTable_config::UI_MODE_TYPE_APPLIANCE:
3085 res.append("appliance");
3087 case ResTable_config::UI_MODE_TYPE_WATCH:
3088 res.append("watch");
3090 case ResTable_config::UI_MODE_TYPE_VR_HEADSET:
3091 res.append("vrheadset");
3094 res.appendFormat("uiModeType=%d",
3095 dtohs(screenLayout&ResTable_config::MASK_UI_MODE_TYPE));
3099 if ((uiMode&MASK_UI_MODE_NIGHT) != 0) {
3100 if (res.size() > 0) res.append("-");
3101 switch (uiMode&ResTable_config::MASK_UI_MODE_NIGHT) {
3102 case ResTable_config::UI_MODE_NIGHT_NO:
3103 res.append("notnight");
3105 case ResTable_config::UI_MODE_NIGHT_YES:
3106 res.append("night");
3109 res.appendFormat("uiModeNight=%d",
3110 dtohs(uiMode&MASK_UI_MODE_NIGHT));
3114 if (density != DENSITY_DEFAULT) {
3115 if (res.size() > 0) res.append("-");
3117 case ResTable_config::DENSITY_LOW:
3120 case ResTable_config::DENSITY_MEDIUM:
3123 case ResTable_config::DENSITY_TV:
3124 res.append("tvdpi");
3126 case ResTable_config::DENSITY_HIGH:
3129 case ResTable_config::DENSITY_XHIGH:
3130 res.append("xhdpi");
3132 case ResTable_config::DENSITY_XXHIGH:
3133 res.append("xxhdpi");
3135 case ResTable_config::DENSITY_XXXHIGH:
3136 res.append("xxxhdpi");
3138 case ResTable_config::DENSITY_NONE:
3139 res.append("nodpi");
3141 case ResTable_config::DENSITY_ANY:
3142 res.append("anydpi");
3145 res.appendFormat("%ddpi", dtohs(density));
3149 if (touchscreen != TOUCHSCREEN_ANY) {
3150 if (res.size() > 0) res.append("-");
3151 switch (touchscreen) {
3152 case ResTable_config::TOUCHSCREEN_NOTOUCH:
3153 res.append("notouch");
3155 case ResTable_config::TOUCHSCREEN_FINGER:
3156 res.append("finger");
3158 case ResTable_config::TOUCHSCREEN_STYLUS:
3159 res.append("stylus");
3162 res.appendFormat("touchscreen=%d", dtohs(touchscreen));
3166 if ((inputFlags&MASK_KEYSHIDDEN) != 0) {
3167 if (res.size() > 0) res.append("-");
3168 switch (inputFlags&MASK_KEYSHIDDEN) {
3169 case ResTable_config::KEYSHIDDEN_NO:
3170 res.append("keysexposed");
3172 case ResTable_config::KEYSHIDDEN_YES:
3173 res.append("keyshidden");
3175 case ResTable_config::KEYSHIDDEN_SOFT:
3176 res.append("keyssoft");
3180 if (keyboard != KEYBOARD_ANY) {
3181 if (res.size() > 0) res.append("-");
3183 case ResTable_config::KEYBOARD_NOKEYS:
3184 res.append("nokeys");
3186 case ResTable_config::KEYBOARD_QWERTY:
3187 res.append("qwerty");
3189 case ResTable_config::KEYBOARD_12KEY:
3190 res.append("12key");
3193 res.appendFormat("keyboard=%d", dtohs(keyboard));
3197 if ((inputFlags&MASK_NAVHIDDEN) != 0) {
3198 if (res.size() > 0) res.append("-");
3199 switch (inputFlags&MASK_NAVHIDDEN) {
3200 case ResTable_config::NAVHIDDEN_NO:
3201 res.append("navexposed");
3203 case ResTable_config::NAVHIDDEN_YES:
3204 res.append("navhidden");
3207 res.appendFormat("inputFlagsNavHidden=%d",
3208 dtohs(inputFlags&MASK_NAVHIDDEN));
3212 if (navigation != NAVIGATION_ANY) {
3213 if (res.size() > 0) res.append("-");
3214 switch (navigation) {
3215 case ResTable_config::NAVIGATION_NONAV:
3216 res.append("nonav");
3218 case ResTable_config::NAVIGATION_DPAD:
3221 case ResTable_config::NAVIGATION_TRACKBALL:
3222 res.append("trackball");
3224 case ResTable_config::NAVIGATION_WHEEL:
3225 res.append("wheel");
3228 res.appendFormat("navigation=%d", dtohs(navigation));
3232 if (screenSize != 0) {
3233 if (res.size() > 0) res.append("-");
3234 res.appendFormat("%dx%d", dtohs(screenWidth), dtohs(screenHeight));
3237 if (res.size() > 0) res.append("-");
3238 res.appendFormat("v%d", dtohs(sdkVersion));
3239 if (minorVersion != 0) {
3240 res.appendFormat(".%d", dtohs(minorVersion));
3247 // --------------------------------------------------------------------
3248 // --------------------------------------------------------------------
3249 // --------------------------------------------------------------------
3251 struct ResTable::Header
3253 explicit Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL),
3254 resourceIDMap(NULL), resourceIDMapSize(0) { }
3258 free(resourceIDMap);
3261 const ResTable* const owner;
3263 const ResTable_header* header;
3265 const uint8_t* dataEnd;
3269 ResStringPool values;
3270 uint32_t* resourceIDMap;
3271 size_t resourceIDMapSize;
3274 struct ResTable::Entry {
3275 ResTable_config config;
3276 const ResTable_entry* entry;
3277 const ResTable_type* type;
3279 const Package* package;
3281 StringPoolRef typeStr;
3282 StringPoolRef keyStr;
3285 struct ResTable::Type
3287 Type(const Header* _header, const Package* _package, size_t count)
3288 : header(_header), package(_package), entryCount(count),
3289 typeSpec(NULL), typeSpecFlags(NULL) { }
3290 const Header* const header;
3291 const Package* const package;
3292 const size_t entryCount;
3293 const ResTable_typeSpec* typeSpec;
3294 const uint32_t* typeSpecFlags;
3295 IdmapEntries idmapEntries;
3296 Vector<const ResTable_type*> configs;
3299 struct ResTable::Package
3301 Package(ResTable* _owner, const Header* _header, const ResTable_package* _package)
3302 : owner(_owner), header(_header), package(_package), typeIdOffset(0) {
3303 if (dtohs(package->header.headerSize) == sizeof(*package)) {
3304 // The package structure is the same size as the definition.
3305 // This means it contains the typeIdOffset field.
3306 typeIdOffset = package->typeIdOffset;
3310 const ResTable* const owner;
3311 const Header* const header;
3312 const ResTable_package* const package;
3314 ResStringPool typeStrings;
3315 ResStringPool keyStrings;
3317 size_t typeIdOffset;
3320 // A group of objects describing a particular resource package.
3321 // The first in 'package' is always the root object (from the resource
3322 // table that defined the package); the ones after are skins on top of it.
3323 struct ResTable::PackageGroup
3326 ResTable* _owner, const String16& _name, uint32_t _id,
3327 bool appAsLib, bool _isSystemAsset)
3332 , dynamicRefTable(static_cast<uint8_t>(_id), appAsLib)
3333 , isSystemAsset(_isSystemAsset)
3338 const size_t numTypes = types.size();
3339 for (size_t i = 0; i < numTypes; i++) {
3340 TypeList& typeList = types.editItemAt(i);
3341 const size_t numInnerTypes = typeList.size();
3342 for (size_t j = 0; j < numInnerTypes; j++) {
3343 if (typeList[j]->package->owner == owner) {
3350 const size_t N = packages.size();
3351 for (size_t i=0; i<N; i++) {
3352 Package* pkg = packages[i];
3353 if (pkg->owner == owner) {
3360 * Clear all cache related data that depends on parameters/configuration.
3361 * This includes the bag caches and filtered types.
3363 void clearBagCache() {
3364 for (size_t i = 0; i < typeCacheEntries.size(); i++) {
3365 if (kDebugTableNoisy) {
3366 printf("type=%zu\n", i);
3368 const TypeList& typeList = types[i];
3369 if (!typeList.isEmpty()) {
3370 TypeCacheEntry& cacheEntry = typeCacheEntries.editItemAt(i);
3372 // Reset the filtered configurations.
3373 cacheEntry.filteredConfigs.clear();
3375 bag_set** typeBags = cacheEntry.cachedBags;
3376 if (kDebugTableNoisy) {
3377 printf("typeBags=%p\n", typeBags);
3381 const size_t N = typeList[0]->entryCount;
3382 if (kDebugTableNoisy) {
3383 printf("type->entryCount=%zu\n", N);
3385 for (size_t j = 0; j < N; j++) {
3386 if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF) {
3391 cacheEntry.cachedBags = NULL;
3397 ssize_t findType16(const char16_t* type, size_t len) const {
3398 const size_t N = packages.size();
3399 for (size_t i = 0; i < N; i++) {
3400 ssize_t index = packages[i]->typeStrings.indexOfString(type, len);
3402 return index + packages[i]->typeIdOffset;
3408 const ResTable* const owner;
3409 String16 const name;
3412 // This is mainly used to keep track of the loaded packages
3413 // and to clean them up properly. Accessing resources happens from
3414 // the 'types' array.
3415 Vector<Package*> packages;
3417 ByteBucketArray<TypeList> types;
3419 uint8_t largestTypeId;
3421 // Cached objects dependent on the parameters/configuration of this ResTable.
3422 // Gets cleared whenever the parameters/configuration changes.
3423 // These are stored here in a parallel structure because the data in `types` may
3424 // be shared by other ResTable's (framework resources are shared this way).
3425 ByteBucketArray<TypeCacheEntry> typeCacheEntries;
3427 // The table mapping dynamic references to resolved references for
3428 // this package group.
3429 // TODO: We may be able to support dynamic references in overlays
3430 // by having these tables in a per-package scope rather than
3431 // per-package-group.
3432 DynamicRefTable dynamicRefTable;
3434 // If the package group comes from a system asset. Used in
3435 // determining non-system locales.
3436 const bool isSystemAsset;
3439 ResTable::Theme::Theme(const ResTable& table)
3443 memset(mPackages, 0, sizeof(mPackages));
3446 ResTable::Theme::~Theme()
3448 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3449 package_info* pi = mPackages[i];
3456 void ResTable::Theme::free_package(package_info* pi)
3458 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
3459 theme_entry* te = pi->types[j].entries;
3467 ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi)
3469 package_info* newpi = (package_info*)malloc(sizeof(package_info));
3470 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
3471 size_t cnt = pi->types[j].numEntries;
3472 newpi->types[j].numEntries = cnt;
3473 theme_entry* te = pi->types[j].entries;
3474 size_t cnt_max = SIZE_MAX / sizeof(theme_entry);
3475 if (te != NULL && (cnt < 0xFFFFFFFF-1) && (cnt < cnt_max)) {
3476 theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry));
3477 newpi->types[j].entries = newte;
3478 memcpy(newte, te, cnt*sizeof(theme_entry));
3480 newpi->types[j].entries = NULL;
3486 status_t ResTable::Theme::applyStyle(uint32_t resID, bool force)
3488 const bag_entry* bag;
3489 uint32_t bagTypeSpecFlags = 0;
3491 const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags);
3492 if (kDebugTableNoisy) {
3493 ALOGV("Applying style 0x%08x to theme %p, count=%zu", resID, this, N);
3500 mTypeSpecFlags |= bagTypeSpecFlags;
3502 uint32_t curPackage = 0xffffffff;
3503 ssize_t curPackageIndex = 0;
3504 package_info* curPI = NULL;
3505 uint32_t curType = 0xffffffff;
3506 size_t numEntries = 0;
3507 theme_entry* curEntries = NULL;
3509 const bag_entry* end = bag + N;
3511 const uint32_t attrRes = bag->map.name.ident;
3512 const uint32_t p = Res_GETPACKAGE(attrRes);
3513 const uint32_t t = Res_GETTYPE(attrRes);
3514 const uint32_t e = Res_GETENTRY(attrRes);
3516 if (curPackage != p) {
3517 const ssize_t pidx = mTable.getResourcePackageIndex(attrRes);
3519 ALOGE("Style contains key with bad package: 0x%08x\n", attrRes);
3524 curPackageIndex = pidx;
3525 curPI = mPackages[pidx];
3526 if (curPI == NULL) {
3527 curPI = (package_info*)malloc(sizeof(package_info));
3528 memset(curPI, 0, sizeof(*curPI));
3529 mPackages[pidx] = curPI;
3531 curType = 0xffffffff;
3534 if (t > Res_MAXTYPE) {
3535 ALOGE("Style contains key with bad type: 0x%08x\n", attrRes);
3540 curEntries = curPI->types[t].entries;
3541 if (curEntries == NULL) {
3542 PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex];
3543 const TypeList& typeList = grp->types[t];
3544 size_t cnt = typeList.isEmpty() ? 0 : typeList[0]->entryCount;
3545 size_t cnt_max = SIZE_MAX / sizeof(theme_entry);
3546 size_t buff_size = (cnt < cnt_max && cnt < 0xFFFFFFFF-1) ?
3547 cnt*sizeof(theme_entry) : 0;
3548 curEntries = (theme_entry*)malloc(buff_size);
3549 memset(curEntries, Res_value::TYPE_NULL, buff_size);
3550 curPI->types[t].numEntries = cnt;
3551 curPI->types[t].entries = curEntries;
3553 numEntries = curPI->types[t].numEntries;
3555 if (e >= numEntries) {
3556 ALOGE("Style contains key with bad entry: 0x%08x\n", attrRes);
3560 theme_entry* curEntry = curEntries + e;
3561 if (kDebugTableNoisy) {
3562 ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x",
3563 attrRes, bag->map.value.dataType, bag->map.value.data,
3564 curEntry->value.dataType);
3566 if (force || (curEntry->value.dataType == Res_value::TYPE_NULL
3567 && curEntry->value.data != Res_value::DATA_NULL_EMPTY)) {
3568 curEntry->stringBlock = bag->stringBlock;
3569 curEntry->typeSpecFlags |= bagTypeSpecFlags;
3570 curEntry->value = bag->map.value;
3578 if (kDebugTableTheme) {
3579 ALOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this);
3586 status_t ResTable::Theme::setTo(const Theme& other)
3588 if (kDebugTableTheme) {
3589 ALOGI("Setting theme %p from theme %p...\n", this, &other);
3594 if (&mTable == &other.mTable) {
3595 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3596 if (mPackages[i] != NULL) {
3597 free_package(mPackages[i]);
3599 if (other.mPackages[i] != NULL) {
3600 mPackages[i] = copy_package(other.mPackages[i]);
3602 mPackages[i] = NULL;
3606 // @todo: need to really implement this, not just copy
3607 // the system package (which is still wrong because it isn't
3608 // fixing up resource references).
3609 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3610 if (mPackages[i] != NULL) {
3611 free_package(mPackages[i]);
3613 if (i == 0 && other.mPackages[i] != NULL) {
3614 mPackages[i] = copy_package(other.mPackages[i]);
3616 mPackages[i] = NULL;
3621 mTypeSpecFlags = other.mTypeSpecFlags;
3623 if (kDebugTableTheme) {
3624 ALOGI("Final theme:");
3631 status_t ResTable::Theme::clear()
3633 if (kDebugTableTheme) {
3634 ALOGI("Clearing theme %p...\n", this);
3638 for (size_t i = 0; i < Res_MAXPACKAGE; i++) {
3639 if (mPackages[i] != NULL) {
3640 free_package(mPackages[i]);
3641 mPackages[i] = NULL;
3647 if (kDebugTableTheme) {
3648 ALOGI("Final theme:");
3655 ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue,
3656 uint32_t* outTypeSpecFlags) const
3660 if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0;
3663 const ssize_t p = mTable.getResourcePackageIndex(resID);
3664 const uint32_t t = Res_GETTYPE(resID);
3665 const uint32_t e = Res_GETENTRY(resID);
3667 if (kDebugTableTheme) {
3668 ALOGI("Looking up attr 0x%08x in theme %p", resID, this);
3672 const package_info* const pi = mPackages[p];
3673 if (kDebugTableTheme) {
3674 ALOGI("Found package: %p", pi);
3677 if (kDebugTableTheme) {
3678 ALOGI("Desired type index is %u in avail %zu", t, Res_MAXTYPE + 1);
3680 if (t <= Res_MAXTYPE) {
3681 const type_info& ti = pi->types[t];
3682 if (kDebugTableTheme) {
3683 ALOGI("Desired entry index is %u in avail %zu", e, ti.numEntries);
3685 if (e < ti.numEntries) {
3686 const theme_entry& te = ti.entries[e];
3687 if (outTypeSpecFlags != NULL) {
3688 *outTypeSpecFlags |= te.typeSpecFlags;
3690 if (kDebugTableTheme) {
3691 ALOGI("Theme value: type=0x%x, data=0x%08x",
3692 te.value.dataType, te.value.data);
3694 const uint8_t type = te.value.dataType;
3695 if (type == Res_value::TYPE_ATTRIBUTE) {
3698 resID = te.value.data;
3701 ALOGW("Too many attribute references, stopped at: 0x%08x\n", resID);
3703 } else if (type != Res_value::TYPE_NULL
3704 || te.value.data == Res_value::DATA_NULL_EMPTY) {
3705 *outValue = te.value;
3706 return te.stringBlock;
3720 ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue,
3721 ssize_t blockIndex, uint32_t* outLastRef,
3722 uint32_t* inoutTypeSpecFlags, ResTable_config* inoutConfig) const
3724 //printf("Resolving type=0x%x\n", inOutValue->dataType);
3725 if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) {
3726 uint32_t newTypeSpecFlags;
3727 blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags);
3728 if (kDebugTableTheme) {
3729 ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=0x%x\n",
3730 (int)blockIndex, (int)inOutValue->dataType, inOutValue->data);
3732 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags;
3733 //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType);
3734 if (blockIndex < 0) {
3738 return mTable.resolveReference(inOutValue, blockIndex, outLastRef,
3739 inoutTypeSpecFlags, inoutConfig);
3742 uint32_t ResTable::Theme::getChangingConfigurations() const
3744 return mTypeSpecFlags;
3747 void ResTable::Theme::dumpToLog() const
3749 ALOGI("Theme %p:\n", this);
3750 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3751 package_info* pi = mPackages[i];
3752 if (pi == NULL) continue;
3754 ALOGI(" Package #0x%02x:\n", (int)(i + 1));
3755 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
3756 type_info& ti = pi->types[j];
3757 if (ti.numEntries == 0) continue;
3758 ALOGI(" Type #0x%02x:\n", (int)(j + 1));
3759 for (size_t k = 0; k < ti.numEntries; k++) {
3760 const theme_entry& te = ti.entries[k];
3761 if (te.value.dataType == Res_value::TYPE_NULL) continue;
3762 ALOGI(" 0x%08x: t=0x%x, d=0x%08x (block=%d)\n",
3763 (int)Res_MAKEID(i, j, k),
3764 te.value.dataType, (int)te.value.data, (int)te.stringBlock);
3770 ResTable::ResTable()
3771 : mError(NO_INIT), mNextPackageId(2)
3773 memset(&mParams, 0, sizeof(mParams));
3774 memset(mPackageMap, 0, sizeof(mPackageMap));
3775 if (kDebugTableSuperNoisy) {
3776 ALOGI("Creating ResTable %p\n", this);
3780 ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData)
3781 : mError(NO_INIT), mNextPackageId(2)
3783 memset(&mParams, 0, sizeof(mParams));
3784 memset(mPackageMap, 0, sizeof(mPackageMap));
3785 addInternal(data, size, NULL, 0, false, cookie, copyData);
3786 LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table");
3787 if (kDebugTableSuperNoisy) {
3788 ALOGI("Creating ResTable %p\n", this);
3792 ResTable::~ResTable()
3794 if (kDebugTableSuperNoisy) {
3795 ALOGI("Destroying ResTable in %p\n", this);
3800 inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const
3802 return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1;
3805 status_t ResTable::add(const void* data, size_t size, const int32_t cookie, bool copyData) {
3806 return addInternal(data, size, NULL, 0, false, cookie, copyData);
3809 status_t ResTable::add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
3810 const int32_t cookie, bool copyData, bool appAsLib) {
3811 return addInternal(data, size, idmapData, idmapDataSize, appAsLib, cookie, copyData);
3814 status_t ResTable::add(Asset* asset, const int32_t cookie, bool copyData) {
3815 const void* data = asset->getBuffer(true);
3817 ALOGW("Unable to get buffer of resource asset file");
3818 return UNKNOWN_ERROR;
3821 return addInternal(data, static_cast<size_t>(asset->getLength()), NULL, false, 0, cookie,
3825 status_t ResTable::add(
3826 Asset* asset, Asset* idmapAsset, const int32_t cookie, bool copyData,
3827 bool appAsLib, bool isSystemAsset) {
3828 const void* data = asset->getBuffer(true);
3830 ALOGW("Unable to get buffer of resource asset file");
3831 return UNKNOWN_ERROR;
3834 size_t idmapSize = 0;
3835 const void* idmapData = NULL;
3836 if (idmapAsset != NULL) {
3837 idmapData = idmapAsset->getBuffer(true);
3838 if (idmapData == NULL) {
3839 ALOGW("Unable to get buffer of idmap asset file");
3840 return UNKNOWN_ERROR;
3842 idmapSize = static_cast<size_t>(idmapAsset->getLength());
3845 return addInternal(data, static_cast<size_t>(asset->getLength()),
3846 idmapData, idmapSize, appAsLib, cookie, copyData, isSystemAsset);
3849 status_t ResTable::add(ResTable* src, bool isSystemAsset)
3851 mError = src->mError;
3853 for (size_t i=0; i < src->mHeaders.size(); i++) {
3854 mHeaders.add(src->mHeaders[i]);
3857 for (size_t i=0; i < src->mPackageGroups.size(); i++) {
3858 PackageGroup* srcPg = src->mPackageGroups[i];
3859 PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id,
3860 false /* appAsLib */, isSystemAsset || srcPg->isSystemAsset);
3861 for (size_t j=0; j<srcPg->packages.size(); j++) {
3862 pg->packages.add(srcPg->packages[j]);
3865 for (size_t j = 0; j < srcPg->types.size(); j++) {
3866 if (srcPg->types[j].isEmpty()) {
3870 TypeList& typeList = pg->types.editItemAt(j);
3871 typeList.appendVector(srcPg->types[j]);
3873 pg->dynamicRefTable.addMappings(srcPg->dynamicRefTable);
3874 pg->largestTypeId = max(pg->largestTypeId, srcPg->largestTypeId);
3875 mPackageGroups.add(pg);
3878 memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap));
3883 status_t ResTable::addEmpty(const int32_t cookie) {
3884 Header* header = new Header(this);
3885 header->index = mHeaders.size();
3886 header->cookie = cookie;
3887 header->values.setToEmpty();
3888 header->ownedData = calloc(1, sizeof(ResTable_header));
3890 ResTable_header* resHeader = (ResTable_header*) header->ownedData;
3891 resHeader->header.type = RES_TABLE_TYPE;
3892 resHeader->header.headerSize = sizeof(ResTable_header);
3893 resHeader->header.size = sizeof(ResTable_header);
3895 header->header = (const ResTable_header*) resHeader;
3896 mHeaders.add(header);
3897 return (mError=NO_ERROR);
3900 status_t ResTable::addInternal(const void* data, size_t dataSize, const void* idmapData, size_t idmapDataSize,
3901 bool appAsLib, const int32_t cookie, bool copyData, bool isSystemAsset)
3907 if (dataSize < sizeof(ResTable_header)) {
3908 ALOGE("Invalid data. Size(%d) is smaller than a ResTable_header(%d).",
3909 (int) dataSize, (int) sizeof(ResTable_header));
3910 return UNKNOWN_ERROR;
3913 Header* header = new Header(this);
3914 header->index = mHeaders.size();
3915 header->cookie = cookie;
3916 if (idmapData != NULL) {
3917 header->resourceIDMap = (uint32_t*) malloc(idmapDataSize);
3918 if (header->resourceIDMap == NULL) {
3920 return (mError = NO_MEMORY);
3922 memcpy(header->resourceIDMap, idmapData, idmapDataSize);
3923 header->resourceIDMapSize = idmapDataSize;
3925 mHeaders.add(header);
3927 const bool notDeviceEndian = htods(0xf0) != 0xf0;
3929 if (kDebugLoadTableNoisy) {
3930 ALOGV("Adding resources to ResTable: data=%p, size=%zu, cookie=%d, copy=%d "
3931 "idmap=%p\n", data, dataSize, cookie, copyData, idmapData);
3934 if (copyData || notDeviceEndian) {
3935 header->ownedData = malloc(dataSize);
3936 if (header->ownedData == NULL) {
3937 return (mError=NO_MEMORY);
3939 memcpy(header->ownedData, data, dataSize);
3940 data = header->ownedData;
3943 header->header = (const ResTable_header*)data;
3944 header->size = dtohl(header->header->header.size);
3945 if (kDebugLoadTableSuperNoisy) {
3946 ALOGI("Got size %zu, again size 0x%x, raw size 0x%x\n", header->size,
3947 dtohl(header->header->header.size), header->header->header.size);
3949 if (kDebugLoadTableNoisy) {
3950 ALOGV("Loading ResTable @%p:\n", header->header);
3952 if (dtohs(header->header->header.headerSize) > header->size
3953 || header->size > dataSize) {
3954 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is larger than data size 0x%x\n",
3955 (int)dtohs(header->header->header.headerSize),
3956 (int)header->size, (int)dataSize);
3957 return (mError=BAD_TYPE);
3959 if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) {
3960 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is not on an integer boundary\n",
3961 (int)dtohs(header->header->header.headerSize),
3963 return (mError=BAD_TYPE);
3965 header->dataEnd = ((const uint8_t*)header->header) + header->size;
3967 // Iterate through all chunks.
3968 size_t curPackage = 0;
3970 const ResChunk_header* chunk =
3971 (const ResChunk_header*)(((const uint8_t*)header->header)
3972 + dtohs(header->header->header.headerSize));
3973 while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) &&
3974 ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) {
3975 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable");
3976 if (err != NO_ERROR) {
3977 return (mError=err);
3979 if (kDebugTableNoisy) {
3980 ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
3981 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
3982 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
3984 const size_t csize = dtohl(chunk->size);
3985 const uint16_t ctype = dtohs(chunk->type);
3986 if (ctype == RES_STRING_POOL_TYPE) {
3987 if (header->values.getError() != NO_ERROR) {
3988 // Only use the first string chunk; ignore any others that
3990 status_t err = header->values.setTo(chunk, csize);
3991 if (err != NO_ERROR) {
3992 return (mError=err);
3995 ALOGW("Multiple string chunks found in resource table.");
3997 } else if (ctype == RES_TABLE_PACKAGE_TYPE) {
3998 if (curPackage >= dtohl(header->header->packageCount)) {
3999 ALOGW("More package chunks were found than the %d declared in the header.",
4000 dtohl(header->header->packageCount));
4001 return (mError=BAD_TYPE);
4005 (ResTable_package*)chunk, header, appAsLib, isSystemAsset) != NO_ERROR) {
4010 ALOGW("Unknown chunk type 0x%x in table at %p.\n",
4012 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
4014 chunk = (const ResChunk_header*)
4015 (((const uint8_t*)chunk) + csize);
4018 if (curPackage < dtohl(header->header->packageCount)) {
4019 ALOGW("Fewer package chunks (%d) were found than the %d declared in the header.",
4020 (int)curPackage, dtohl(header->header->packageCount));
4021 return (mError=BAD_TYPE);
4023 mError = header->values.getError();
4024 if (mError != NO_ERROR) {
4025 ALOGW("No string values found in resource table!");
4028 if (kDebugTableNoisy) {
4029 ALOGV("Returning from add with mError=%d\n", mError);
4034 status_t ResTable::getError() const
4039 void ResTable::uninit()
4042 size_t N = mPackageGroups.size();
4043 for (size_t i=0; i<N; i++) {
4044 PackageGroup* g = mPackageGroups[i];
4047 N = mHeaders.size();
4048 for (size_t i=0; i<N; i++) {
4049 Header* header = mHeaders[i];
4050 if (header->owner == this) {
4051 if (header->ownedData) {
4052 free(header->ownedData);
4058 mPackageGroups.clear();
4062 bool ResTable::getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const
4064 if (mError != NO_ERROR) {
4068 const ssize_t p = getResourcePackageIndex(resID);
4069 const int t = Res_GETTYPE(resID);
4070 const int e = Res_GETENTRY(resID);
4073 if (Res_GETPACKAGE(resID)+1 == 0) {
4074 ALOGW("No package identifier when getting name for resource number 0x%08x", resID);
4076 #ifndef STATIC_ANDROIDFW_FOR_TOOLS
4077 ALOGW("No known package when getting name for resource number 0x%08x", resID);
4083 ALOGW("No type identifier when getting name for resource number 0x%08x", resID);
4087 const PackageGroup* const grp = mPackageGroups[p];
4089 ALOGW("Bad identifier when getting name for resource number 0x%08x", resID);
4094 status_t err = getEntry(grp, t, e, NULL, &entry);
4095 if (err != NO_ERROR) {
4099 outName->package = grp->name.string();
4100 outName->packageLen = grp->name.size();
4102 outName->type8 = entry.typeStr.string8(&outName->typeLen);
4103 outName->name8 = entry.keyStr.string8(&outName->nameLen);
4105 outName->type8 = NULL;
4106 outName->name8 = NULL;
4108 if (outName->type8 == NULL) {
4109 outName->type = entry.typeStr.string16(&outName->typeLen);
4110 // If we have a bad index for some reason, we should abort.
4111 if (outName->type == NULL) {
4115 if (outName->name8 == NULL) {
4116 outName->name = entry.keyStr.string16(&outName->nameLen);
4117 // If we have a bad index for some reason, we should abort.
4118 if (outName->name == NULL) {
4126 ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, uint16_t density,
4127 uint32_t* outSpecFlags, ResTable_config* outConfig) const
4129 if (mError != NO_ERROR) {
4133 const ssize_t p = getResourcePackageIndex(resID);
4134 const int t = Res_GETTYPE(resID);
4135 const int e = Res_GETENTRY(resID);
4138 if (Res_GETPACKAGE(resID)+1 == 0) {
4139 ALOGW("No package identifier when getting value for resource number 0x%08x", resID);
4141 ALOGW("No known package when getting value for resource number 0x%08x", resID);
4146 ALOGW("No type identifier when getting value for resource number 0x%08x", resID);
4150 const PackageGroup* const grp = mPackageGroups[p];
4152 ALOGW("Bad identifier when getting value for resource number 0x%08x", resID);
4156 // Allow overriding density
4157 ResTable_config desiredConfig = mParams;
4159 desiredConfig.density = density;
4163 status_t err = getEntry(grp, t, e, &desiredConfig, &entry);
4164 if (err != NO_ERROR) {
4165 // Only log the failure when we're not running on the host as
4166 // part of a tool. The caller will do its own logging.
4167 #ifndef STATIC_ANDROIDFW_FOR_TOOLS
4168 ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) (error %d)\n",
4174 if ((dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) != 0) {
4176 ALOGW("Requesting resource 0x%08x failed because it is complex\n", resID);
4181 const Res_value* value = reinterpret_cast<const Res_value*>(
4182 reinterpret_cast<const uint8_t*>(entry.entry) + entry.entry->size);
4184 outValue->size = dtohs(value->size);
4185 outValue->res0 = value->res0;
4186 outValue->dataType = value->dataType;
4187 outValue->data = dtohl(value->data);
4189 // The reference may be pointing to a resource in a shared library. These
4190 // references have build-time generated package IDs. These ids may not match
4191 // the actual package IDs of the corresponding packages in this ResTable.
4192 // We need to fix the package ID based on a mapping.
4193 if (grp->dynamicRefTable.lookupResourceValue(outValue) != NO_ERROR) {
4194 ALOGW("Failed to resolve referenced package: 0x%08x", outValue->data);
4198 if (kDebugTableNoisy) {
4200 printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n",
4201 entry.package->header->index,
4203 outValue->dataType == Res_value::TYPE_STRING ?
4204 String8(entry.package->header->values.stringAt(outValue->data, &len)).string() :
4209 if (outSpecFlags != NULL) {
4210 *outSpecFlags = entry.specFlags;
4213 if (outConfig != NULL) {
4214 *outConfig = entry.config;
4217 return entry.package->header->index;
4220 ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex,
4221 uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags,
4222 ResTable_config* outConfig) const
4225 while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE
4226 && value->data != 0 && count < 20) {
4227 if (outLastRef) *outLastRef = value->data;
4228 uint32_t newFlags = 0;
4229 const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags,
4231 if (newIndex == BAD_INDEX) {
4234 if (kDebugTableTheme) {
4235 ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n",
4236 value->data, (int)newIndex, (int)value->dataType, value->data);
4238 //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex);
4239 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags;
4241 // This can fail if the resource being referenced is a style...
4242 // in this case, just return the reference, and expect the
4243 // caller to deal with.
4246 blockIndex = newIndex;
4252 const char16_t* ResTable::valueToString(
4253 const Res_value* value, size_t stringBlock,
4254 char16_t /*tmpBuffer*/ [TMP_BUFFER_SIZE], size_t* outLen) const
4259 if (value->dataType == value->TYPE_STRING) {
4260 return getTableStringBlock(stringBlock)->stringAt(value->data, outLen);
4262 // XXX do int to string conversions.
4266 ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const
4269 ssize_t err = getBagLocked(resID, outBag);
4270 if (err < NO_ERROR) {
4271 //printf("*** get failed! unlocking\n");
4277 void ResTable::unlockBag(const bag_entry* /*bag*/) const
4279 //printf("<<< unlockBag %p\n", this);
4283 void ResTable::lock() const
4288 void ResTable::unlock() const
4293 ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag,
4294 uint32_t* outTypeSpecFlags) const
4296 if (mError != NO_ERROR) {
4300 const ssize_t p = getResourcePackageIndex(resID);
4301 const int t = Res_GETTYPE(resID);
4302 const int e = Res_GETENTRY(resID);
4305 ALOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID);
4309 ALOGW("No type identifier when getting bag for resource number 0x%08x", resID);
4313 //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t);
4314 PackageGroup* const grp = mPackageGroups[p];
4316 ALOGW("Bad identifier when getting bag for resource number 0x%08x", resID);
4320 const TypeList& typeConfigs = grp->types[t];
4321 if (typeConfigs.isEmpty()) {
4322 ALOGW("Type identifier 0x%x does not exist.", t+1);
4326 const size_t NENTRY = typeConfigs[0]->entryCount;
4327 if (e >= (int)NENTRY) {
4328 ALOGW("Entry identifier 0x%x is larger than entry count 0x%x",
4329 e, (int)typeConfigs[0]->entryCount);
4333 // First see if we've already computed this bag...
4334 TypeCacheEntry& cacheEntry = grp->typeCacheEntries.editItemAt(t);
4335 bag_set** typeSet = cacheEntry.cachedBags;
4337 bag_set* set = typeSet[e];
4339 if (set != (bag_set*)0xFFFFFFFF) {
4340 if (outTypeSpecFlags != NULL) {
4341 *outTypeSpecFlags = set->typeSpecFlags;
4343 *outBag = (bag_entry*)(set+1);
4344 if (kDebugTableSuperNoisy) {
4345 ALOGI("Found existing bag for: 0x%x\n", resID);
4347 return set->numAttrs;
4349 ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.",
4355 // Bag not found, we need to compute it!
4357 typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*));
4358 if (!typeSet) return NO_MEMORY;
4359 cacheEntry.cachedBags = typeSet;
4362 // Mark that we are currently working on this one.
4363 typeSet[e] = (bag_set*)0xFFFFFFFF;
4365 if (kDebugTableNoisy) {
4366 ALOGI("Building bag: %x\n", resID);
4369 // Now collect all bag attributes
4371 status_t err = getEntry(grp, t, e, &mParams, &entry);
4372 if (err != NO_ERROR) {
4376 const uint16_t entrySize = dtohs(entry.entry->size);
4377 const uint32_t parent = entrySize >= sizeof(ResTable_map_entry)
4378 ? dtohl(((const ResTable_map_entry*)entry.entry)->parent.ident) : 0;
4379 const uint32_t count = entrySize >= sizeof(ResTable_map_entry)
4380 ? dtohl(((const ResTable_map_entry*)entry.entry)->count) : 0;
4384 if (kDebugTableNoisy) {
4385 ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count);
4387 // If this map inherits from another, we need to start
4388 // with its parent's values. Otherwise start out empty.
4389 ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent);
4392 // This is what we are building.
4393 bag_set* set = NULL;
4396 uint32_t resolvedParent = parent;
4398 // Bags encode a parent reference without using the standard
4399 // Res_value structure. That means we must always try to
4400 // resolve a parent reference in case it is actually a
4401 // TYPE_DYNAMIC_REFERENCE.
4402 status_t err = grp->dynamicRefTable.lookupResourceId(&resolvedParent);
4403 if (err != NO_ERROR) {
4404 ALOGE("Failed resolving bag parent id 0x%08x", parent);
4405 return UNKNOWN_ERROR;
4408 const bag_entry* parentBag;
4409 uint32_t parentTypeSpecFlags = 0;
4410 const ssize_t NP = getBagLocked(resolvedParent, &parentBag, &parentTypeSpecFlags);
4411 const size_t NT = ((NP >= 0) ? NP : 0) + N;
4412 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT);
4417 memcpy(set+1, parentBag, NP*sizeof(bag_entry));
4419 if (kDebugTableNoisy) {
4420 ALOGI("Initialized new bag with %zd inherited attributes.\n", NP);
4423 if (kDebugTableNoisy) {
4424 ALOGI("Initialized new bag with no inherited attributes.\n");
4428 set->availAttrs = NT;
4429 set->typeSpecFlags = parentTypeSpecFlags;
4431 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N);
4436 set->availAttrs = N;
4437 set->typeSpecFlags = 0;
4440 set->typeSpecFlags |= entry.specFlags;
4442 // Now merge in the new attributes...
4443 size_t curOff = (reinterpret_cast<uintptr_t>(entry.entry) - reinterpret_cast<uintptr_t>(entry.type))
4444 + dtohs(entry.entry->size);
4445 const ResTable_map* map;
4446 bag_entry* entries = (bag_entry*)(set+1);
4447 size_t curEntry = 0;
4449 if (kDebugTableNoisy) {
4450 ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs);
4452 while (pos < count) {
4453 if (kDebugTableNoisy) {
4454 ALOGI("Now at %p\n", (void*)curOff);
4457 if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) {
4458 ALOGW("ResTable_map at %d is beyond type chunk data %d",
4459 (int)curOff, dtohl(entry.type->header.size));
4463 map = (const ResTable_map*)(((const uint8_t*)entry.type) + curOff);
4466 uint32_t newName = htodl(map->name.ident);
4467 if (!Res_INTERNALID(newName)) {
4468 // Attributes don't have a resource id as the name. They specify
4469 // other data, which would be wrong to change via a lookup.
4470 if (grp->dynamicRefTable.lookupResourceId(&newName) != NO_ERROR) {
4471 ALOGE("Failed resolving ResTable_map name at %d with ident 0x%08x",
4472 (int) curOff, (int) newName);
4474 return UNKNOWN_ERROR;
4479 uint32_t oldName = 0;
4480 while ((isInside=(curEntry < set->numAttrs))
4481 && (oldName=entries[curEntry].map.name.ident) < newName) {
4482 if (kDebugTableNoisy) {
4483 ALOGI("#%zu: Keeping existing attribute: 0x%08x\n",
4484 curEntry, entries[curEntry].map.name.ident);
4489 if ((!isInside) || oldName != newName) {
4490 // This is a new attribute... figure out what to do with it.
4491 if (set->numAttrs >= set->availAttrs) {
4492 // Need to alloc more memory...
4493 const size_t newAvail = set->availAttrs+N;
4495 set = (bag_set*)realloc(set,
4497 + sizeof(bag_entry)*newAvail);
4502 set->availAttrs = newAvail;
4503 entries = (bag_entry*)(set+1);
4504 if (kDebugTableNoisy) {
4505 ALOGI("Reallocated set %p, entries=%p, avail=%zu\n",
4506 set, entries, set->availAttrs);
4510 // Going in the middle, need to make space.
4511 memmove(entries+curEntry+1, entries+curEntry,
4512 sizeof(bag_entry)*(set->numAttrs-curEntry));
4515 if (kDebugTableNoisy) {
4516 ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName);
4519 if (kDebugTableNoisy) {
4520 ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName);
4524 bag_entry* cur = entries+curEntry;
4526 cur->stringBlock = entry.package->header->index;
4527 cur->map.name.ident = newName;
4528 cur->map.value.copyFrom_dtoh(map->value);
4529 status_t err = grp->dynamicRefTable.lookupResourceValue(&cur->map.value);
4530 if (err != NO_ERROR) {
4531 ALOGE("Reference item(0x%08x) in bag could not be resolved.", cur->map.value.data);
4532 return UNKNOWN_ERROR;
4535 if (kDebugTableNoisy) {
4536 ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n",
4537 curEntry, cur, cur->stringBlock, cur->map.name.ident,
4538 cur->map.value.dataType, cur->map.value.data);
4544 const size_t size = dtohs(map->value.size);
4545 curOff += size + sizeof(*map)-sizeof(map->value);
4548 if (curEntry > set->numAttrs) {
4549 set->numAttrs = curEntry;
4552 // And this is it...
4555 if (outTypeSpecFlags != NULL) {
4556 *outTypeSpecFlags = set->typeSpecFlags;
4558 *outBag = (bag_entry*)(set+1);
4559 if (kDebugTableNoisy) {
4560 ALOGI("Returning %zu attrs\n", set->numAttrs);
4562 return set->numAttrs;
4567 void ResTable::setParameters(const ResTable_config* params)
4569 AutoMutex _lock(mLock);
4570 AutoMutex _lock2(mFilteredConfigLock);
4572 if (kDebugTableGetEntry) {
4573 ALOGI("Setting parameters: %s\n", params->toString().string());
4576 for (size_t p = 0; p < mPackageGroups.size(); p++) {
4577 PackageGroup* packageGroup = mPackageGroups.editItemAt(p);
4578 if (kDebugTableNoisy) {
4579 ALOGI("CLEARING BAGS FOR GROUP %zu!", p);
4581 packageGroup->clearBagCache();
4583 // Find which configurations match the set of parameters. This allows for a much
4584 // faster lookup in getEntry() if the set of values is narrowed down.
4585 for (size_t t = 0; t < packageGroup->types.size(); t++) {
4586 if (packageGroup->types[t].isEmpty()) {
4590 TypeList& typeList = packageGroup->types.editItemAt(t);
4592 // Retrieve the cache entry for this type.
4593 TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries.editItemAt(t);
4595 for (size_t ts = 0; ts < typeList.size(); ts++) {
4596 Type* type = typeList.editItemAt(ts);
4598 std::shared_ptr<Vector<const ResTable_type*>> newFilteredConfigs =
4599 std::make_shared<Vector<const ResTable_type*>>();
4601 for (size_t ti = 0; ti < type->configs.size(); ti++) {
4602 ResTable_config config;
4603 config.copyFromDtoH(type->configs[ti]->config);
4605 if (config.match(mParams)) {
4606 newFilteredConfigs->add(type->configs[ti]);
4610 if (kDebugTableNoisy) {
4611 ALOGD("Updating pkg=%zu type=%zu with %zu filtered configs",
4612 p, t, newFilteredConfigs->size());
4615 cacheEntry.filteredConfigs.add(newFilteredConfigs);
4621 void ResTable::getParameters(ResTable_config* params) const
4628 struct id_name_map {
4634 const static id_name_map ID_NAMES[] = {
4635 { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } },
4636 { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } },
4637 { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } },
4638 { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } },
4639 { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } },
4640 { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } },
4641 { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } },
4642 { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } },
4643 { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } },
4644 { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } },
4647 uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen,
4648 const char16_t* type, size_t typeLen,
4649 const char16_t* package,
4651 uint32_t* outTypeSpecFlags) const
4653 if (kDebugTableSuperNoisy) {
4654 printf("Identifier for name: error=%d\n", mError);
4657 // Check for internal resource identifier as the very first thing, so
4658 // that we will always find them even when there are no resources.
4659 if (name[0] == '^') {
4660 const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0]));
4662 for (int i=0; i<N; i++) {
4663 const id_name_map* m = ID_NAMES + i;
4665 if (len != nameLen) {
4668 for (size_t j=1; j<len; j++) {
4669 if (m->name[j] != name[j]) {
4673 if (outTypeSpecFlags) {
4674 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4681 if (name[1] == 'i' && name[2] == 'n'
4682 && name[3] == 'd' && name[4] == 'e' && name[5] == 'x'
4683 && name[6] == '_') {
4684 int index = atoi(String8(name + 7, nameLen - 7).string());
4685 if (Res_CHECKID(index)) {
4686 ALOGW("Array resource index: %d is too large.",
4690 if (outTypeSpecFlags) {
4691 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4693 return Res_MAKEARRAY(index);
4699 if (mError != NO_ERROR) {
4703 bool fakePublic = false;
4705 // Figure out the package and type we are looking in...
4707 const char16_t* packageEnd = NULL;
4708 const char16_t* typeEnd = NULL;
4709 const char16_t* const nameEnd = name+nameLen;
4710 const char16_t* p = name;
4711 while (p < nameEnd) {
4712 if (*p == ':') packageEnd = p;
4713 else if (*p == '/') typeEnd = p;
4723 if (name >= nameEnd) {
4729 packageLen = packageEnd-name;
4730 name = packageEnd+1;
4731 } else if (!package) {
4737 typeLen = typeEnd-name;
4743 if (name >= nameEnd) {
4746 nameLen = nameEnd-name;
4748 if (kDebugTableNoisy) {
4749 printf("Looking for identifier: type=%s, name=%s, package=%s\n",
4750 String8(type, typeLen).string(),
4751 String8(name, nameLen).string(),
4752 String8(package, packageLen).string());
4755 const String16 attr("attr");
4756 const String16 attrPrivate("^attr-private");
4758 const size_t NG = mPackageGroups.size();
4759 for (size_t ig=0; ig<NG; ig++) {
4760 const PackageGroup* group = mPackageGroups[ig];
4762 if (strzcmp16(package, packageLen,
4763 group->name.string(), group->name.size())) {
4764 if (kDebugTableNoisy) {
4765 printf("Skipping package group: %s\n", String8(group->name).string());
4770 const size_t packageCount = group->packages.size();
4771 for (size_t pi = 0; pi < packageCount; pi++) {
4772 const char16_t* targetType = type;
4773 size_t targetTypeLen = typeLen;
4776 ssize_t ti = group->packages[pi]->typeStrings.indexOfString(
4777 targetType, targetTypeLen);
4782 ti += group->packages[pi]->typeIdOffset;
4784 const uint32_t identifier = findEntry(group, ti, name, nameLen,
4786 if (identifier != 0) {
4787 if (fakePublic && outTypeSpecFlags) {
4788 *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC;
4792 } while (strzcmp16(attr.string(), attr.size(), targetType, targetTypeLen) == 0
4793 && (targetType = attrPrivate.string())
4794 && (targetTypeLen = attrPrivate.size())
4801 uint32_t ResTable::findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name,
4802 size_t nameLen, uint32_t* outTypeSpecFlags) const {
4803 const TypeList& typeList = group->types[typeIndex];
4804 const size_t typeCount = typeList.size();
4805 for (size_t i = 0; i < typeCount; i++) {
4806 const Type* t = typeList[i];
4807 const ssize_t ei = t->package->keyStrings.indexOfString(name, nameLen);
4812 const size_t configCount = t->configs.size();
4813 for (size_t j = 0; j < configCount; j++) {
4814 const TypeVariant tv(t->configs[j]);
4815 for (TypeVariant::iterator iter = tv.beginEntries();
4816 iter != tv.endEntries();
4818 const ResTable_entry* entry = *iter;
4819 if (entry == NULL) {
4823 if (dtohl(entry->key.index) == (size_t) ei) {
4824 uint32_t resId = Res_MAKEID(group->id - 1, typeIndex, iter.index());
4825 if (outTypeSpecFlags) {
4827 if (getEntry(group, typeIndex, iter.index(), NULL, &result) != NO_ERROR) {
4828 ALOGW("Failed to find spec flags for 0x%08x", resId);
4831 *outTypeSpecFlags = result.specFlags;
4841 bool ResTable::expandResourceRef(const char16_t* refStr, size_t refLen,
4842 String16* outPackage,
4845 const String16* defType,
4846 const String16* defPackage,
4847 const char** outErrorMsg,
4848 bool* outPublicOnly)
4850 const char16_t* packageEnd = NULL;
4851 const char16_t* typeEnd = NULL;
4852 const char16_t* p = refStr;
4853 const char16_t* const end = p + refLen;
4855 if (*p == ':') packageEnd = p;
4856 else if (*p == '/') {
4865 if (outPublicOnly != NULL) {
4866 *outPublicOnly = true;
4870 if (outPublicOnly != NULL) {
4871 *outPublicOnly = false;
4876 *outPackage = String16(p, packageEnd-p);
4881 *outErrorMsg = "No resource package specified";
4885 *outPackage = *defPackage;
4888 *outType = String16(p, typeEnd-p);
4893 *outErrorMsg = "No resource type specified";
4897 *outType = *defType;
4899 *outName = String16(p, end-p);
4900 if(**outPackage == 0) {
4902 *outErrorMsg = "Resource package cannot be an empty string";
4906 if(**outType == 0) {
4908 *outErrorMsg = "Resource type cannot be an empty string";
4912 if(**outName == 0) {
4914 *outErrorMsg = "Resource id cannot be an empty string";
4921 static uint32_t get_hex(char c, bool* outError)
4923 if (c >= '0' && c <= '9') {
4925 } else if (c >= 'a' && c <= 'f') {
4926 return c - 'a' + 0xa;
4927 } else if (c >= 'A' && c <= 'F') {
4928 return c - 'A' + 0xa;
4943 static const unit_entry unitNames[] = {
4944 { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f },
4945 { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4946 { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4947 { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f },
4948 { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f },
4949 { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f },
4950 { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f },
4951 { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 },
4952 { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 },
4953 { NULL, 0, 0, 0, 0 }
4956 static bool parse_unit(const char* str, Res_value* outValue,
4957 float* outScale, const char** outEnd)
4959 const char* end = str;
4960 while (*end != 0 && !isspace((unsigned char)*end)) {
4963 const size_t len = end-str;
4965 const char* realEnd = end;
4966 while (*realEnd != 0 && isspace((unsigned char)*realEnd)) {
4969 if (*realEnd != 0) {
4973 const unit_entry* cur = unitNames;
4975 if (len == cur->len && strncmp(cur->name, str, len) == 0) {
4976 outValue->dataType = cur->type;
4977 outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT;
4978 *outScale = cur->scale;
4980 //printf("Found unit %s for %s\n", cur->name, str);
4989 bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue)
4991 while (len > 0 && isspace16(*s)) {
5009 if (s[i] < '0' || s[i] > '9') {
5013 static_assert(std::is_same<uint32_t, Res_value::data_type>::value,
5014 "Res_value::data_type has changed. The range checks in this "
5015 "function are no longer correct.");
5019 if (len > 1 && s[i] == '0' && s[i+1] == 'x') {
5033 while (i < len && !error) {
5034 val = (val*16) + get_hex(s[i], &error);
5037 if (val > std::numeric_limits<uint32_t>::max()) {
5047 if (s[i] < '0' || s[i] > '9') {
5050 val = (val*10) + s[i]-'0';
5053 if ((neg && -val < std::numeric_limits<int32_t>::min()) ||
5054 (!neg && val > std::numeric_limits<int32_t>::max())) {
5060 if (neg) val = -val;
5062 while (i < len && isspace16(s[i])) {
5071 outValue->dataType =
5072 isHex ? outValue->TYPE_INT_HEX : outValue->TYPE_INT_DEC;
5073 outValue->data = static_cast<Res_value::data_type>(val);
5078 bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue)
5080 return U16StringToInt(s, len, outValue);
5083 bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue)
5085 while (len > 0 && isspace16(*s)) {
5096 while (len > 0 && *s != 0 && i < 126) {
5107 if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.' && buf[0] != '-' && buf[0] != '+') {
5113 float f = strtof(buf, (char**)&end);
5115 if (*end != 0 && !isspace((unsigned char)*end)) {
5116 // Might be a unit...
5118 if (parse_unit(end, outValue, &scale, &end)) {
5120 const bool neg = f < 0;
5122 uint64_t bits = (uint64_t)(f*(1<<23)+.5f);
5125 if ((bits&0x7fffff) == 0) {
5126 // Always use 23p0 if there is no fraction, just to make
5127 // things easier to read.
5128 radix = Res_value::COMPLEX_RADIX_23p0;
5130 } else if ((bits&0xffffffffff800000LL) == 0) {
5131 // Magnitude is zero -- can fit in 0 bits of precision.
5132 radix = Res_value::COMPLEX_RADIX_0p23;
5134 } else if ((bits&0xffffffff80000000LL) == 0) {
5135 // Magnitude can fit in 8 bits of precision.
5136 radix = Res_value::COMPLEX_RADIX_8p15;
5138 } else if ((bits&0xffffff8000000000LL) == 0) {
5139 // Magnitude can fit in 16 bits of precision.
5140 radix = Res_value::COMPLEX_RADIX_16p7;
5143 // Magnitude needs entire range, so no fractional part.
5144 radix = Res_value::COMPLEX_RADIX_23p0;
5147 int32_t mantissa = (int32_t)(
5148 (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK);
5150 mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK;
5153 (radix<<Res_value::COMPLEX_RADIX_SHIFT)
5154 | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT);
5155 //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n",
5156 // f * (neg ? -1 : 1), bits, f*(1<<23),
5157 // radix, shift, outValue->data);
5163 while (*end != 0 && isspace((unsigned char)*end)) {
5169 outValue->dataType = outValue->TYPE_FLOAT;
5170 *(float*)(&outValue->data) = f;
5178 bool ResTable::stringToValue(Res_value* outValue, String16* outString,
5179 const char16_t* s, size_t len,
5180 bool preserveSpaces, bool coerceType,
5182 const String16* defType,
5183 const String16* defPackage,
5185 void* accessorCookie,
5187 bool enforcePrivate) const
5189 bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting();
5190 const char* errorMsg = NULL;
5192 outValue->size = sizeof(Res_value);
5195 // First strip leading/trailing whitespace. Do this before handling
5196 // escapes, so they can be used to force whitespace into the string.
5197 if (!preserveSpaces) {
5198 while (len > 0 && isspace16(*s)) {
5202 while (len > 0 && isspace16(s[len-1])) {
5205 // If the string ends with '\', then we keep the space after it.
5206 if (len > 0 && s[len-1] == '\\' && s[len] != 0) {
5211 //printf("Value for: %s\n", String8(s, len).string());
5213 uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED;
5214 uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff;
5215 bool fromAccessor = false;
5216 if (attrID != 0 && !Res_INTERNALID(attrID)) {
5217 const ssize_t p = getResourcePackageIndex(attrID);
5218 const bag_entry* bag;
5219 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5220 //printf("For attr 0x%08x got bag of %d\n", attrID, cnt);
5223 //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data);
5224 switch (bag->map.name.ident) {
5225 case ResTable_map::ATTR_TYPE:
5226 attrType = bag->map.value.data;
5228 case ResTable_map::ATTR_MIN:
5229 attrMin = bag->map.value.data;
5231 case ResTable_map::ATTR_MAX:
5232 attrMax = bag->map.value.data;
5234 case ResTable_map::ATTR_L10N:
5235 l10nReq = bag->map.value.data;
5242 } else if (accessor && accessor->getAttributeType(attrID, &attrType)) {
5243 fromAccessor = true;
5244 if (attrType == ResTable_map::TYPE_ENUM
5245 || attrType == ResTable_map::TYPE_FLAGS
5246 || attrType == ResTable_map::TYPE_INTEGER) {
5247 accessor->getAttributeMin(attrID, &attrMin);
5248 accessor->getAttributeMax(attrID, &attrMax);
5250 if (localizationSetting) {
5251 l10nReq = accessor->getAttributeL10N(attrID);
5256 const bool canStringCoerce =
5257 coerceType && (attrType&ResTable_map::TYPE_STRING) != 0;
5260 outValue->dataType = outValue->TYPE_REFERENCE;
5262 // Note: we don't check attrType here because the reference can
5263 // be to any other type; we just need to count on the client making
5264 // sure the referenced type is correct.
5266 //printf("Looking up ref: %s\n", String8(s, len).string());
5268 // It's a reference!
5269 if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') {
5270 // Special case @null as undefined. This will be converted by
5271 // AssetManager to TYPE_NULL with data DATA_NULL_UNDEFINED.
5274 } else if (len == 6 && s[1]=='e' && s[2]=='m' && s[3]=='p' && s[4]=='t' && s[5]=='y') {
5275 // Special case @empty as explicitly defined empty value.
5276 outValue->dataType = Res_value::TYPE_NULL;
5277 outValue->data = Res_value::DATA_NULL_EMPTY;
5280 bool createIfNotFound = false;
5281 const char16_t* resourceRefName;
5282 int resourceNameLen;
5283 if (len > 2 && s[1] == '+') {
5284 createIfNotFound = true;
5285 resourceRefName = s + 2;
5286 resourceNameLen = len - 2;
5287 } else if (len > 2 && s[1] == '*') {
5288 enforcePrivate = false;
5289 resourceRefName = s + 2;
5290 resourceNameLen = len - 2;
5292 createIfNotFound = false;
5293 resourceRefName = s + 1;
5294 resourceNameLen = len - 1;
5296 String16 package, type, name;
5297 if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name,
5298 defType, defPackage, &errorMsg)) {
5299 if (accessor != NULL) {
5300 accessor->reportError(accessorCookie, errorMsg);
5305 uint32_t specFlags = 0;
5306 uint32_t rid = identifierForName(name.string(), name.size(), type.string(),
5307 type.size(), package.string(), package.size(), &specFlags);
5309 if (enforcePrivate) {
5310 if (accessor == NULL || accessor->getAssetsPackage() != package) {
5311 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5312 if (accessor != NULL) {
5313 accessor->reportError(accessorCookie, "Resource is not public.");
5322 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5323 Res_GETTYPE(rid), Res_GETENTRY(rid));
5324 if (kDebugTableNoisy) {
5325 ALOGI("Incl %s:%s/%s: 0x%08x\n",
5326 String8(package).string(), String8(type).string(),
5327 String8(name).string(), rid);
5331 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5332 if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) {
5333 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5335 outValue->data = rid;
5340 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name,
5343 if (kDebugTableNoisy) {
5344 ALOGI("Pckg %s:%s/%s: 0x%08x\n",
5345 String8(package).string(), String8(type).string(),
5346 String8(name).string(), rid);
5348 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5349 if (packageId == 0x00) {
5350 outValue->data = rid;
5351 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
5353 } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) {
5354 // We accept packageId's generated as 0x01 in order to support
5355 // building the android system resources
5356 outValue->data = rid;
5363 if (accessor != NULL) {
5364 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5369 // if we got to here, and localization is required and it's not a reference,
5370 // complain and bail.
5371 if (l10nReq == ResTable_map::L10N_SUGGESTED) {
5372 if (localizationSetting) {
5373 if (accessor != NULL) {
5374 accessor->reportError(accessorCookie, "This attribute must be localized.");
5380 // It's a color! Convert to an integer of the form 0xaarrggbb.
5384 outValue->dataType = outValue->TYPE_INT_COLOR_RGB4;
5385 color |= 0xFF000000;
5386 color |= get_hex(s[1], &error) << 20;
5387 color |= get_hex(s[1], &error) << 16;
5388 color |= get_hex(s[2], &error) << 12;
5389 color |= get_hex(s[2], &error) << 8;
5390 color |= get_hex(s[3], &error) << 4;
5391 color |= get_hex(s[3], &error);
5392 } else if (len == 5) {
5393 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4;
5394 color |= get_hex(s[1], &error) << 28;
5395 color |= get_hex(s[1], &error) << 24;
5396 color |= get_hex(s[2], &error) << 20;
5397 color |= get_hex(s[2], &error) << 16;
5398 color |= get_hex(s[3], &error) << 12;
5399 color |= get_hex(s[3], &error) << 8;
5400 color |= get_hex(s[4], &error) << 4;
5401 color |= get_hex(s[4], &error);
5402 } else if (len == 7) {
5403 outValue->dataType = outValue->TYPE_INT_COLOR_RGB8;
5404 color |= 0xFF000000;
5405 color |= get_hex(s[1], &error) << 20;
5406 color |= get_hex(s[2], &error) << 16;
5407 color |= get_hex(s[3], &error) << 12;
5408 color |= get_hex(s[4], &error) << 8;
5409 color |= get_hex(s[5], &error) << 4;
5410 color |= get_hex(s[6], &error);
5411 } else if (len == 9) {
5412 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8;
5413 color |= get_hex(s[1], &error) << 28;
5414 color |= get_hex(s[2], &error) << 24;
5415 color |= get_hex(s[3], &error) << 20;
5416 color |= get_hex(s[4], &error) << 16;
5417 color |= get_hex(s[5], &error) << 12;
5418 color |= get_hex(s[6], &error) << 8;
5419 color |= get_hex(s[7], &error) << 4;
5420 color |= get_hex(s[8], &error);
5425 if ((attrType&ResTable_map::TYPE_COLOR) == 0) {
5426 if (!canStringCoerce) {
5427 if (accessor != NULL) {
5428 accessor->reportError(accessorCookie,
5429 "Color types not allowed");
5434 outValue->data = color;
5435 //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color);
5439 if ((attrType&ResTable_map::TYPE_COLOR) != 0) {
5440 if (accessor != NULL) {
5441 accessor->reportError(accessorCookie, "Color value not valid --"
5442 " must be #rgb, #argb, #rrggbb, or #aarrggbb");
5445 fprintf(stderr, "%s: Color ID %s value %s is not valid\n",
5446 "Resource File", //(const char*)in->getPrintableSource(),
5447 String8(*curTag).string(),
5448 String8(s, len).string());
5456 outValue->dataType = outValue->TYPE_ATTRIBUTE;
5458 // Note: we don't check attrType here because the reference can
5459 // be to any other type; we just need to count on the client making
5460 // sure the referenced type is correct.
5462 //printf("Looking up attr: %s\n", String8(s, len).string());
5464 static const String16 attr16("attr");
5465 String16 package, type, name;
5466 if (!expandResourceRef(s+1, len-1, &package, &type, &name,
5467 &attr16, defPackage, &errorMsg)) {
5468 if (accessor != NULL) {
5469 accessor->reportError(accessorCookie, errorMsg);
5474 //printf("Pkg: %s, Type: %s, Name: %s\n",
5475 // String8(package).string(), String8(type).string(),
5476 // String8(name).string());
5477 uint32_t specFlags = 0;
5479 identifierForName(name.string(), name.size(),
5480 type.string(), type.size(),
5481 package.string(), package.size(), &specFlags);
5483 if (enforcePrivate) {
5484 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5485 if (accessor != NULL) {
5486 accessor->reportError(accessorCookie, "Attribute is not public.");
5494 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5495 Res_GETTYPE(rid), Res_GETENTRY(rid));
5498 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5499 if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) {
5500 outValue->dataType = Res_value::TYPE_DYNAMIC_ATTRIBUTE;
5502 outValue->data = rid;
5507 uint32_t rid = accessor->getCustomResource(package, type, name);
5509 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
5510 if (packageId == 0x00) {
5511 outValue->data = rid;
5512 outValue->dataType = Res_value::TYPE_DYNAMIC_ATTRIBUTE;
5514 } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) {
5515 // We accept packageId's generated as 0x01 in order to support
5516 // building the android system resources
5517 outValue->data = rid;
5523 if (accessor != NULL) {
5524 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5529 if (stringToInt(s, len, outValue)) {
5530 if ((attrType&ResTable_map::TYPE_INTEGER) == 0) {
5531 // If this type does not allow integers, but does allow floats,
5532 // fall through on this error case because the float type should
5533 // be able to accept any integer value.
5534 if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) {
5535 if (accessor != NULL) {
5536 accessor->reportError(accessorCookie, "Integer types not allowed");
5541 if (((int32_t)outValue->data) < ((int32_t)attrMin)
5542 || ((int32_t)outValue->data) > ((int32_t)attrMax)) {
5543 if (accessor != NULL) {
5544 accessor->reportError(accessorCookie, "Integer value out of range");
5552 if (stringToFloat(s, len, outValue)) {
5553 if (outValue->dataType == Res_value::TYPE_DIMENSION) {
5554 if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) {
5557 if (!canStringCoerce) {
5558 if (accessor != NULL) {
5559 accessor->reportError(accessorCookie, "Dimension types not allowed");
5563 } else if (outValue->dataType == Res_value::TYPE_FRACTION) {
5564 if ((attrType&ResTable_map::TYPE_FRACTION) != 0) {
5567 if (!canStringCoerce) {
5568 if (accessor != NULL) {
5569 accessor->reportError(accessorCookie, "Fraction types not allowed");
5573 } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) {
5574 if (!canStringCoerce) {
5575 if (accessor != NULL) {
5576 accessor->reportError(accessorCookie, "Float types not allowed");
5586 if ((s[0] == 't' || s[0] == 'T') &&
5587 (s[1] == 'r' || s[1] == 'R') &&
5588 (s[2] == 'u' || s[2] == 'U') &&
5589 (s[3] == 'e' || s[3] == 'E')) {
5590 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5591 if (!canStringCoerce) {
5592 if (accessor != NULL) {
5593 accessor->reportError(accessorCookie, "Boolean types not allowed");
5598 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5599 outValue->data = (uint32_t)-1;
5606 if ((s[0] == 'f' || s[0] == 'F') &&
5607 (s[1] == 'a' || s[1] == 'A') &&
5608 (s[2] == 'l' || s[2] == 'L') &&
5609 (s[3] == 's' || s[3] == 'S') &&
5610 (s[4] == 'e' || s[4] == 'E')) {
5611 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5612 if (!canStringCoerce) {
5613 if (accessor != NULL) {
5614 accessor->reportError(accessorCookie, "Boolean types not allowed");
5619 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5626 if ((attrType&ResTable_map::TYPE_ENUM) != 0) {
5627 const ssize_t p = getResourcePackageIndex(attrID);
5628 const bag_entry* bag;
5629 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5630 //printf("Got %d for enum\n", cnt);
5632 resource_name rname;
5634 if (!Res_INTERNALID(bag->map.name.ident)) {
5635 //printf("Trying attr #%08x\n", bag->map.name.ident);
5636 if (getResourceName(bag->map.name.ident, false, &rname)) {
5638 printf("Matching %s against %s (0x%08x)\n",
5639 String8(s, len).string(),
5640 String8(rname.name, rname.nameLen).string(),
5641 bag->map.name.ident);
5643 if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) {
5644 outValue->dataType = bag->map.value.dataType;
5645 outValue->data = bag->map.value.data;
5659 if (accessor->getAttributeEnum(attrID, s, len, outValue)) {
5665 if ((attrType&ResTable_map::TYPE_FLAGS) != 0) {
5666 const ssize_t p = getResourcePackageIndex(attrID);
5667 const bag_entry* bag;
5668 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5669 //printf("Got %d for flags\n", cnt);
5671 bool failed = false;
5672 resource_name rname;
5673 outValue->dataType = Res_value::TYPE_INT_HEX;
5675 const char16_t* end = s + len;
5676 const char16_t* pos = s;
5677 while (pos < end && !failed) {
5678 const char16_t* start = pos;
5680 while (pos < end && *pos != '|') {
5683 //printf("Looking for: %s\n", String8(start, pos-start).string());
5684 const bag_entry* bagi = bag;
5686 for (i=0; i<cnt; i++, bagi++) {
5687 if (!Res_INTERNALID(bagi->map.name.ident)) {
5688 //printf("Trying attr #%08x\n", bagi->map.name.ident);
5689 if (getResourceName(bagi->map.name.ident, false, &rname)) {
5691 printf("Matching %s against %s (0x%08x)\n",
5692 String8(start,pos-start).string(),
5693 String8(rname.name, rname.nameLen).string(),
5694 bagi->map.name.ident);
5696 if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) {
5697 outValue->data |= bagi->map.value.data;
5704 // Didn't find this flag identifier.
5713 //printf("Final flag value: 0x%lx\n", outValue->data);
5720 if (accessor->getAttributeFlags(attrID, s, len, outValue)) {
5721 //printf("Final flag value: 0x%lx\n", outValue->data);
5727 if ((attrType&ResTable_map::TYPE_STRING) == 0) {
5728 if (accessor != NULL) {
5729 accessor->reportError(accessorCookie, "String types not allowed");
5734 // Generic string handling...
5735 outValue->dataType = outValue->TYPE_STRING;
5737 bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg);
5738 if (accessor != NULL) {
5739 accessor->reportError(accessorCookie, errorMsg);
5747 bool ResTable::collectString(String16* outString,
5748 const char16_t* s, size_t len,
5749 bool preserveSpaces,
5750 const char** outErrorMsg,
5756 const char16_t* p = s;
5757 while (p < (s+len)) {
5758 while (p < (s+len)) {
5759 const char16_t c = *p;
5763 if (!preserveSpaces) {
5764 if (quoted == 0 && isspace16(c)
5765 && (c != ' ' || isspace16(*(p+1)))) {
5768 if (c == '"' && (quoted == 0 || quoted == '"')) {
5771 if (c == '\'' && (quoted == 0 || quoted == '\'')) {
5773 * In practice, when people write ' instead of \'
5774 * in a string, they are doing it by accident
5775 * instead of really meaning to use ' as a quoting
5776 * character. Warn them so they don't lose it.
5779 *outErrorMsg = "Apostrophe not preceded by \\";
5788 tmp.append(String16(s, p-s));
5790 if (!preserveSpaces && (*p == '"' || *p == '\'')) {
5797 } else if (!preserveSpaces && isspace16(*p)) {
5798 // Space outside of a quote -- consume all spaces and
5799 // leave a single plain space char.
5800 tmp.append(String16(" "));
5802 while (p < (s+len) && isspace16(*p)) {
5805 } else if (*p == '\\') {
5810 tmp.append(String16("\t"));
5813 tmp.append(String16("\n"));
5816 tmp.append(String16("#"));
5819 tmp.append(String16("@"));
5822 tmp.append(String16("?"));
5825 tmp.append(String16("\""));
5828 tmp.append(String16("'"));
5831 tmp.append(String16("\\"));
5837 while (i < 4 && p[1] != 0) {
5841 if (*p >= '0' && *p <= '9') {
5843 } else if (*p >= 'a' && *p <= 'f') {
5845 } else if (*p >= 'A' && *p <= 'F') {
5849 *outErrorMsg = "Bad character in \\u unicode escape sequence";
5855 tmp.append(String16(&chr, 1));
5858 // ignore unknown escape chars.
5869 if (tmp.size() != 0) {
5871 tmp.append(String16(s, len));
5874 outString->append(tmp);
5876 outString->setTo(tmp);
5880 outString->append(String16(s, len));
5882 outString->setTo(s, len);
5889 size_t ResTable::getBasePackageCount() const
5891 if (mError != NO_ERROR) {
5894 return mPackageGroups.size();
5897 const String16 ResTable::getBasePackageName(size_t idx) const
5899 if (mError != NO_ERROR) {
5902 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5903 "Requested package index %d past package count %d",
5904 (int)idx, (int)mPackageGroups.size());
5905 return mPackageGroups[idx]->name;
5908 uint32_t ResTable::getBasePackageId(size_t idx) const
5910 if (mError != NO_ERROR) {
5913 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5914 "Requested package index %d past package count %d",
5915 (int)idx, (int)mPackageGroups.size());
5916 return mPackageGroups[idx]->id;
5919 uint32_t ResTable::getLastTypeIdForPackage(size_t idx) const
5921 if (mError != NO_ERROR) {
5924 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5925 "Requested package index %d past package count %d",
5926 (int)idx, (int)mPackageGroups.size());
5927 const PackageGroup* const group = mPackageGroups[idx];
5928 return group->largestTypeId;
5931 size_t ResTable::getTableCount() const
5933 return mHeaders.size();
5936 const ResStringPool* ResTable::getTableStringBlock(size_t index) const
5938 return &mHeaders[index]->values;
5941 int32_t ResTable::getTableCookie(size_t index) const
5943 return mHeaders[index]->cookie;
5946 const DynamicRefTable* ResTable::getDynamicRefTableForCookie(int32_t cookie) const
5948 const size_t N = mPackageGroups.size();
5949 for (size_t i = 0; i < N; i++) {
5950 const PackageGroup* pg = mPackageGroups[i];
5951 size_t M = pg->packages.size();
5952 for (size_t j = 0; j < M; j++) {
5953 if (pg->packages[j]->header->cookie == cookie) {
5954 return &pg->dynamicRefTable;
5961 static bool compareResTableConfig(const ResTable_config& a, const ResTable_config& b) {
5962 return a.compare(b) < 0;
5965 template <typename Func>
5966 void ResTable::forEachConfiguration(bool ignoreMipmap, bool ignoreAndroidPackage,
5967 bool includeSystemConfigs, const Func& f) const {
5968 const size_t packageCount = mPackageGroups.size();
5969 const String16 android("android");
5970 for (size_t i = 0; i < packageCount; i++) {
5971 const PackageGroup* packageGroup = mPackageGroups[i];
5972 if (ignoreAndroidPackage && android == packageGroup->name) {
5975 if (!includeSystemConfigs && packageGroup->isSystemAsset) {
5978 const size_t typeCount = packageGroup->types.size();
5979 for (size_t j = 0; j < typeCount; j++) {
5980 const TypeList& typeList = packageGroup->types[j];
5981 const size_t numTypes = typeList.size();
5982 for (size_t k = 0; k < numTypes; k++) {
5983 const Type* type = typeList[k];
5984 const ResStringPool& typeStrings = type->package->typeStrings;
5985 if (ignoreMipmap && typeStrings.string8ObjectAt(
5986 type->typeSpec->id - 1) == "mipmap") {
5990 const size_t numConfigs = type->configs.size();
5991 for (size_t m = 0; m < numConfigs; m++) {
5992 const ResTable_type* config = type->configs[m];
5993 ResTable_config cfg;
5994 memset(&cfg, 0, sizeof(ResTable_config));
5995 cfg.copyFromDtoH(config->config);
6004 void ResTable::getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap,
6005 bool ignoreAndroidPackage, bool includeSystemConfigs) const {
6006 auto func = [&](const ResTable_config& cfg) {
6007 const auto beginIter = configs->begin();
6008 const auto endIter = configs->end();
6010 auto iter = std::lower_bound(beginIter, endIter, cfg, compareResTableConfig);
6011 if (iter == endIter || iter->compare(cfg) != 0) {
6012 configs->insertAt(cfg, std::distance(beginIter, iter));
6015 forEachConfiguration(ignoreMipmap, ignoreAndroidPackage, includeSystemConfigs, func);
6018 static bool compareString8AndCString(const String8& str, const char* cStr) {
6019 return strcmp(str.string(), cStr) < 0;
6022 void ResTable::getLocales(Vector<String8>* locales, bool includeSystemLocales,
6023 bool mergeEquivalentLangs) const {
6024 char locale[RESTABLE_MAX_LOCALE_LEN];
6026 forEachConfiguration(false, false, includeSystemLocales, [&](const ResTable_config& cfg) {
6027 cfg.getBcp47Locale(locale, mergeEquivalentLangs /* canonicalize if merging */);
6029 const auto beginIter = locales->begin();
6030 const auto endIter = locales->end();
6032 auto iter = std::lower_bound(beginIter, endIter, locale, compareString8AndCString);
6033 if (iter == endIter || strcmp(iter->string(), locale) != 0) {
6034 locales->insertAt(String8(locale), std::distance(beginIter, iter));
6039 StringPoolRef::StringPoolRef(const ResStringPool* pool, uint32_t index)
6040 : mPool(pool), mIndex(index) {}
6042 StringPoolRef::StringPoolRef()
6043 : mPool(NULL), mIndex(0) {}
6045 const char* StringPoolRef::string8(size_t* outLen) const {
6046 if (mPool != NULL) {
6047 return mPool->string8At(mIndex, outLen);
6049 if (outLen != NULL) {
6055 const char16_t* StringPoolRef::string16(size_t* outLen) const {
6056 if (mPool != NULL) {
6057 return mPool->stringAt(mIndex, outLen);
6059 if (outLen != NULL) {
6065 bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const {
6066 if (mError != NO_ERROR) {
6070 const ssize_t p = getResourcePackageIndex(resID);
6071 const int t = Res_GETTYPE(resID);
6072 const int e = Res_GETENTRY(resID);
6075 if (Res_GETPACKAGE(resID)+1 == 0) {
6076 ALOGW("No package identifier when getting flags for resource number 0x%08x", resID);
6078 ALOGW("No known package when getting flags for resource number 0x%08x", resID);
6083 ALOGW("No type identifier when getting flags for resource number 0x%08x", resID);
6087 const PackageGroup* const grp = mPackageGroups[p];
6089 ALOGW("Bad identifier when getting flags for resource number 0x%08x", resID);
6094 status_t err = getEntry(grp, t, e, NULL, &entry);
6095 if (err != NO_ERROR) {
6099 *outFlags = entry.specFlags;
6103 static bool keyCompare(const ResTable_sparseTypeEntry& entry , uint16_t entryIdx) {
6104 return dtohs(entry.idx) < entryIdx;
6107 status_t ResTable::getEntry(
6108 const PackageGroup* packageGroup, int typeIndex, int entryIndex,
6109 const ResTable_config* config,
6110 Entry* outEntry) const
6112 const TypeList& typeList = packageGroup->types[typeIndex];
6113 if (typeList.isEmpty()) {
6114 ALOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex);
6118 const ResTable_type* bestType = NULL;
6119 uint32_t bestOffset = ResTable_type::NO_ENTRY;
6120 const Package* bestPackage = NULL;
6121 uint32_t specFlags = 0;
6122 uint8_t actualTypeIndex = typeIndex;
6123 ResTable_config bestConfig;
6124 memset(&bestConfig, 0, sizeof(bestConfig));
6126 // Iterate over the Types of each package.
6127 const size_t typeCount = typeList.size();
6128 for (size_t i = 0; i < typeCount; i++) {
6129 const Type* const typeSpec = typeList[i];
6131 int realEntryIndex = entryIndex;
6132 int realTypeIndex = typeIndex;
6133 bool currentTypeIsOverlay = false;
6135 // Runtime overlay packages provide a mapping of app resource
6136 // ID to package resource ID.
6137 if (typeSpec->idmapEntries.hasEntries()) {
6138 uint16_t overlayEntryIndex;
6139 if (typeSpec->idmapEntries.lookup(entryIndex, &overlayEntryIndex) != NO_ERROR) {
6140 // No such mapping exists
6143 realEntryIndex = overlayEntryIndex;
6144 realTypeIndex = typeSpec->idmapEntries.overlayTypeId() - 1;
6145 currentTypeIsOverlay = true;
6148 // Check that the entry idx is within range of the declared entry count (ResTable_typeSpec).
6149 // Particular types (ResTable_type) may be encoded with sparse entries, and so their
6150 // entryCount do not need to match.
6151 if (static_cast<size_t>(realEntryIndex) >= typeSpec->entryCount) {
6152 ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)",
6153 Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex),
6154 entryIndex, static_cast<int>(typeSpec->entryCount));
6155 // We should normally abort here, but some legacy apps declare
6156 // resources in the 'android' package (old bug in AAPT).
6160 // Aggregate all the flags for each package that defines this entry.
6161 if (typeSpec->typeSpecFlags != NULL) {
6162 specFlags |= dtohl(typeSpec->typeSpecFlags[realEntryIndex]);
6167 const Vector<const ResTable_type*>* candidateConfigs = &typeSpec->configs;
6169 std::shared_ptr<Vector<const ResTable_type*>> filteredConfigs;
6170 if (config && memcmp(&mParams, config, sizeof(mParams)) == 0) {
6171 // Grab the lock first so we can safely get the current filtered list.
6172 AutoMutex _lock(mFilteredConfigLock);
6174 // This configuration is equal to the one we have previously cached for,
6175 // so use the filtered configs.
6177 const TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries[typeIndex];
6178 if (i < cacheEntry.filteredConfigs.size()) {
6179 if (cacheEntry.filteredConfigs[i]) {
6180 // Grab a reference to the shared_ptr so it doesn't get destroyed while
6181 // going through this list.
6182 filteredConfigs = cacheEntry.filteredConfigs[i];
6184 // Use this filtered list.
6185 candidateConfigs = filteredConfigs.get();
6190 const size_t numConfigs = candidateConfigs->size();
6191 for (size_t c = 0; c < numConfigs; c++) {
6192 const ResTable_type* const thisType = candidateConfigs->itemAt(c);
6193 if (thisType == NULL) {
6197 ResTable_config thisConfig;
6198 thisConfig.copyFromDtoH(thisType->config);
6200 // Check to make sure this one is valid for the current parameters.
6201 if (config != NULL && !thisConfig.match(*config)) {
6205 const uint32_t* const eindex = reinterpret_cast<const uint32_t*>(
6206 reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize));
6208 uint32_t thisOffset;
6210 // Check if there is the desired entry in this type.
6211 if (thisType->flags & ResTable_type::FLAG_SPARSE) {
6212 // This is encoded as a sparse map, so perform a binary search.
6213 const ResTable_sparseTypeEntry* sparseIndices =
6214 reinterpret_cast<const ResTable_sparseTypeEntry*>(eindex);
6215 const ResTable_sparseTypeEntry* result = std::lower_bound(
6216 sparseIndices, sparseIndices + dtohl(thisType->entryCount), realEntryIndex,
6218 if (result == sparseIndices + dtohl(thisType->entryCount)
6219 || dtohs(result->idx) != realEntryIndex) {
6224 // Extract the offset from the entry. Each offset must be a multiple of 4
6225 // so we store it as the real offset divided by 4.
6226 thisOffset = dtohs(result->offset) * 4u;
6228 if (static_cast<uint32_t>(realEntryIndex) >= dtohl(thisType->entryCount)) {
6229 // Entry does not exist.
6233 thisOffset = dtohl(eindex[realEntryIndex]);
6236 if (thisOffset == ResTable_type::NO_ENTRY) {
6237 // There is no entry for this index and configuration.
6241 if (bestType != NULL) {
6242 // Check if this one is less specific than the last found. If so,
6243 // we will skip it. We check starting with things we most care
6244 // about to those we least care about.
6245 if (!thisConfig.isBetterThan(bestConfig, config)) {
6246 if (!currentTypeIsOverlay || thisConfig.compare(bestConfig) != 0) {
6252 bestType = thisType;
6253 bestOffset = thisOffset;
6254 bestConfig = thisConfig;
6255 bestPackage = typeSpec->package;
6256 actualTypeIndex = realTypeIndex;
6258 // If no config was specified, any type will do, so skip
6259 if (config == NULL) {
6265 if (bestType == NULL) {
6269 bestOffset += dtohl(bestType->entriesStart);
6271 if (bestOffset > (dtohl(bestType->header.size)-sizeof(ResTable_entry))) {
6272 ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x",
6273 bestOffset, dtohl(bestType->header.size));
6276 if ((bestOffset & 0x3) != 0) {
6277 ALOGW("ResTable_entry at 0x%x is not on an integer boundary", bestOffset);
6281 const ResTable_entry* const entry = reinterpret_cast<const ResTable_entry*>(
6282 reinterpret_cast<const uint8_t*>(bestType) + bestOffset);
6283 if (dtohs(entry->size) < sizeof(*entry)) {
6284 ALOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size));
6288 if (outEntry != NULL) {
6289 outEntry->entry = entry;
6290 outEntry->config = bestConfig;
6291 outEntry->type = bestType;
6292 outEntry->specFlags = specFlags;
6293 outEntry->package = bestPackage;
6294 outEntry->typeStr = StringPoolRef(&bestPackage->typeStrings, actualTypeIndex - bestPackage->typeIdOffset);
6295 outEntry->keyStr = StringPoolRef(&bestPackage->keyStrings, dtohl(entry->key.index));
6300 status_t ResTable::parsePackage(const ResTable_package* const pkg,
6301 const Header* const header, bool appAsLib, bool isSystemAsset)
6303 const uint8_t* base = (const uint8_t*)pkg;
6304 status_t err = validate_chunk(&pkg->header, sizeof(*pkg) - sizeof(pkg->typeIdOffset),
6305 header->dataEnd, "ResTable_package");
6306 if (err != NO_ERROR) {
6307 return (mError=err);
6310 const uint32_t pkgSize = dtohl(pkg->header.size);
6312 if (dtohl(pkg->typeStrings) >= pkgSize) {
6313 ALOGW("ResTable_package type strings at 0x%x are past chunk size 0x%x.",
6314 dtohl(pkg->typeStrings), pkgSize);
6315 return (mError=BAD_TYPE);
6317 if ((dtohl(pkg->typeStrings)&0x3) != 0) {
6318 ALOGW("ResTable_package type strings at 0x%x is not on an integer boundary.",
6319 dtohl(pkg->typeStrings));
6320 return (mError=BAD_TYPE);
6322 if (dtohl(pkg->keyStrings) >= pkgSize) {
6323 ALOGW("ResTable_package key strings at 0x%x are past chunk size 0x%x.",
6324 dtohl(pkg->keyStrings), pkgSize);
6325 return (mError=BAD_TYPE);
6327 if ((dtohl(pkg->keyStrings)&0x3) != 0) {
6328 ALOGW("ResTable_package key strings at 0x%x is not on an integer boundary.",
6329 dtohl(pkg->keyStrings));
6330 return (mError=BAD_TYPE);
6333 uint32_t id = dtohl(pkg->id);
6334 KeyedVector<uint8_t, IdmapEntries> idmapEntries;
6336 if (header->resourceIDMap != NULL) {
6337 uint8_t targetPackageId = 0;
6338 status_t err = parseIdmap(header->resourceIDMap, header->resourceIDMapSize, &targetPackageId, &idmapEntries);
6339 if (err != NO_ERROR) {
6340 ALOGW("Overlay is broken");
6341 return (mError=err);
6343 id = targetPackageId;
6347 LOG_ALWAYS_FATAL("Package id out of range");
6349 } else if (id == 0 || (id == 0x7f && appAsLib) || isSystemAsset) {
6350 // This is a library or a system asset, so assign an ID
6351 id = mNextPackageId++;
6354 PackageGroup* group = NULL;
6355 Package* package = new Package(this, header, pkg);
6356 if (package == NULL) {
6357 return (mError=NO_MEMORY);
6360 err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings),
6361 header->dataEnd-(base+dtohl(pkg->typeStrings)));
6362 if (err != NO_ERROR) {
6365 return (mError=err);
6368 err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings),
6369 header->dataEnd-(base+dtohl(pkg->keyStrings)));
6370 if (err != NO_ERROR) {
6373 return (mError=err);
6376 size_t idx = mPackageMap[id];
6378 idx = mPackageGroups.size() + 1;
6380 char16_t tmpName[sizeof(pkg->name)/sizeof(pkg->name[0])];
6381 strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(pkg->name[0]));
6382 group = new PackageGroup(this, String16(tmpName), id, appAsLib, isSystemAsset);
6383 if (group == NULL) {
6385 return (mError=NO_MEMORY);
6388 err = mPackageGroups.add(group);
6389 if (err < NO_ERROR) {
6390 return (mError=err);
6393 mPackageMap[id] = static_cast<uint8_t>(idx);
6395 // Find all packages that reference this package
6396 size_t N = mPackageGroups.size();
6397 for (size_t i = 0; i < N; i++) {
6398 mPackageGroups[i]->dynamicRefTable.addMapping(
6399 group->name, static_cast<uint8_t>(group->id));
6402 group = mPackageGroups.itemAt(idx - 1);
6403 if (group == NULL) {
6404 return (mError=UNKNOWN_ERROR);
6408 err = group->packages.add(package);
6409 if (err < NO_ERROR) {
6410 return (mError=err);
6413 // Iterate through all chunks.
6414 const ResChunk_header* chunk =
6415 (const ResChunk_header*)(((const uint8_t*)pkg)
6416 + dtohs(pkg->header.headerSize));
6417 const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size);
6418 while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) &&
6419 ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) {
6420 if (kDebugTableNoisy) {
6421 ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
6422 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
6423 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
6425 const size_t csize = dtohl(chunk->size);
6426 const uint16_t ctype = dtohs(chunk->type);
6427 if (ctype == RES_TABLE_TYPE_SPEC_TYPE) {
6428 const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk);
6429 err = validate_chunk(&typeSpec->header, sizeof(*typeSpec),
6430 endPos, "ResTable_typeSpec");
6431 if (err != NO_ERROR) {
6432 return (mError=err);
6435 const size_t typeSpecSize = dtohl(typeSpec->header.size);
6436 const size_t newEntryCount = dtohl(typeSpec->entryCount);
6438 if (kDebugLoadTableNoisy) {
6439 ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
6440 (void*)(base-(const uint8_t*)chunk),
6441 dtohs(typeSpec->header.type),
6442 dtohs(typeSpec->header.headerSize),
6443 (void*)typeSpecSize);
6445 // look for block overrun or int overflow when multiplying by 4
6446 if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t))
6447 || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
6449 ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.",
6450 (void*)(dtohs(typeSpec->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6451 (void*)typeSpecSize);
6452 return (mError=BAD_TYPE);
6455 if (typeSpec->id == 0) {
6456 ALOGW("ResTable_type has an id of 0.");
6457 return (mError=BAD_TYPE);
6460 if (newEntryCount > 0) {
6461 bool addToType = true;
6462 uint8_t typeIndex = typeSpec->id - 1;
6463 ssize_t idmapIndex = idmapEntries.indexOfKey(typeSpec->id);
6464 if (idmapIndex >= 0) {
6465 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6466 } else if (header->resourceIDMap != NULL) {
6467 // This is an overlay, but the types in this overlay are not
6468 // overlaying anything according to the idmap. We can skip these
6469 // as they will otherwise conflict with the other resources in the package
6470 // without a mapping.
6475 TypeList& typeList = group->types.editItemAt(typeIndex);
6476 if (!typeList.isEmpty()) {
6477 const Type* existingType = typeList[0];
6478 if (existingType->entryCount != newEntryCount && idmapIndex < 0) {
6479 ALOGW("ResTable_typeSpec entry count inconsistent: "
6480 "given %d, previously %d",
6481 (int) newEntryCount, (int) existingType->entryCount);
6482 // We should normally abort here, but some legacy apps declare
6483 // resources in the 'android' package (old bug in AAPT).
6487 Type* t = new Type(header, package, newEntryCount);
6488 t->typeSpec = typeSpec;
6489 t->typeSpecFlags = (const uint32_t*)(
6490 ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize));
6491 if (idmapIndex >= 0) {
6492 t->idmapEntries = idmapEntries[idmapIndex];
6495 group->largestTypeId = max(group->largestTypeId, typeSpec->id);
6498 ALOGV("Skipping empty ResTable_typeSpec for type %d", typeSpec->id);
6501 } else if (ctype == RES_TABLE_TYPE_TYPE) {
6502 const ResTable_type* type = (const ResTable_type*)(chunk);
6503 err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4,
6504 endPos, "ResTable_type");
6505 if (err != NO_ERROR) {
6506 return (mError=err);
6509 const uint32_t typeSize = dtohl(type->header.size);
6510 const size_t newEntryCount = dtohl(type->entryCount);
6512 if (kDebugLoadTableNoisy) {
6513 printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n",
6514 (void*)(base-(const uint8_t*)chunk),
6515 dtohs(type->header.type),
6516 dtohs(type->header.headerSize),
6519 if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) {
6520 ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.",
6521 (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
6523 return (mError=BAD_TYPE);
6526 if (newEntryCount != 0
6527 && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) {
6528 ALOGW("ResTable_type entriesStart at 0x%x extends beyond chunk end 0x%x.",
6529 dtohl(type->entriesStart), typeSize);
6530 return (mError=BAD_TYPE);
6533 if (type->id == 0) {
6534 ALOGW("ResTable_type has an id of 0.");
6535 return (mError=BAD_TYPE);
6538 if (newEntryCount > 0) {
6539 bool addToType = true;
6540 uint8_t typeIndex = type->id - 1;
6541 ssize_t idmapIndex = idmapEntries.indexOfKey(type->id);
6542 if (idmapIndex >= 0) {
6543 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
6544 } else if (header->resourceIDMap != NULL) {
6545 // This is an overlay, but the types in this overlay are not
6546 // overlaying anything according to the idmap. We can skip these
6547 // as they will otherwise conflict with the other resources in the package
6548 // without a mapping.
6553 TypeList& typeList = group->types.editItemAt(typeIndex);
6554 if (typeList.isEmpty()) {
6555 ALOGE("No TypeSpec for type %d", type->id);
6556 return (mError=BAD_TYPE);
6559 Type* t = typeList.editItemAt(typeList.size() - 1);
6560 if (t->package != package) {
6561 ALOGE("No TypeSpec for type %d", type->id);
6562 return (mError=BAD_TYPE);
6565 t->configs.add(type);
6567 if (kDebugTableGetEntry) {
6568 ResTable_config thisConfig;
6569 thisConfig.copyFromDtoH(type->config);
6570 ALOGI("Adding config to type %d: %s\n", type->id,
6571 thisConfig.toString().string());
6575 ALOGV("Skipping empty ResTable_type for type %d", type->id);
6578 } else if (ctype == RES_TABLE_LIBRARY_TYPE) {
6579 if (group->dynamicRefTable.entries().size() == 0) {
6580 status_t err = group->dynamicRefTable.load((const ResTable_lib_header*) chunk);
6581 if (err != NO_ERROR) {
6582 return (mError=err);
6585 // Fill in the reference table with the entries we already know about.
6586 size_t N = mPackageGroups.size();
6587 for (size_t i = 0; i < N; i++) {
6588 group->dynamicRefTable.addMapping(mPackageGroups[i]->name, mPackageGroups[i]->id);
6591 ALOGW("Found multiple library tables, ignoring...");
6594 status_t err = validate_chunk(chunk, sizeof(ResChunk_header),
6595 endPos, "ResTable_package:unknown");
6596 if (err != NO_ERROR) {
6597 return (mError=err);
6600 chunk = (const ResChunk_header*)
6601 (((const uint8_t*)chunk) + csize);
6607 DynamicRefTable::DynamicRefTable() : DynamicRefTable(0, false) {}
6609 DynamicRefTable::DynamicRefTable(uint8_t packageId, bool appAsLib)
6610 : mAssignedPackageId(packageId)
6611 , mAppAsLib(appAsLib)
6613 memset(mLookupTable, 0, sizeof(mLookupTable));
6615 // Reserved package ids
6616 mLookupTable[APP_PACKAGE_ID] = APP_PACKAGE_ID;
6617 mLookupTable[SYS_PACKAGE_ID] = SYS_PACKAGE_ID;
6620 status_t DynamicRefTable::load(const ResTable_lib_header* const header)
6622 const uint32_t entryCount = dtohl(header->count);
6623 const uint32_t sizeOfEntries = sizeof(ResTable_lib_entry) * entryCount;
6624 const uint32_t expectedSize = dtohl(header->header.size) - dtohl(header->header.headerSize);
6625 if (sizeOfEntries > expectedSize) {
6626 ALOGE("ResTable_lib_header size %u is too small to fit %u entries (x %u).",
6627 expectedSize, entryCount, (uint32_t)sizeof(ResTable_lib_entry));
6628 return UNKNOWN_ERROR;
6631 const ResTable_lib_entry* entry = (const ResTable_lib_entry*)(((uint8_t*) header) +
6632 dtohl(header->header.headerSize));
6633 for (uint32_t entryIndex = 0; entryIndex < entryCount; entryIndex++) {
6634 uint32_t packageId = dtohl(entry->packageId);
6635 char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)];
6636 strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t));
6637 if (kDebugLibNoisy) {
6638 ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
6639 dtohl(entry->packageId));
6641 if (packageId >= 256) {
6642 ALOGE("Bad package id 0x%08x", packageId);
6643 return UNKNOWN_ERROR;
6645 mEntries.replaceValueFor(String16(tmpName), (uint8_t) packageId);
6651 status_t DynamicRefTable::addMappings(const DynamicRefTable& other) {
6652 if (mAssignedPackageId != other.mAssignedPackageId) {
6653 return UNKNOWN_ERROR;
6656 const size_t entryCount = other.mEntries.size();
6657 for (size_t i = 0; i < entryCount; i++) {
6658 ssize_t index = mEntries.indexOfKey(other.mEntries.keyAt(i));
6660 mEntries.add(other.mEntries.keyAt(i), other.mEntries[i]);
6662 if (other.mEntries[i] != mEntries[index]) {
6663 return UNKNOWN_ERROR;
6668 // Merge the lookup table. No entry can conflict
6669 // (value of 0 means not set).
6670 for (size_t i = 0; i < 256; i++) {
6671 if (mLookupTable[i] != other.mLookupTable[i]) {
6672 if (mLookupTable[i] == 0) {
6673 mLookupTable[i] = other.mLookupTable[i];
6674 } else if (other.mLookupTable[i] != 0) {
6675 return UNKNOWN_ERROR;
6682 status_t DynamicRefTable::addMapping(const String16& packageName, uint8_t packageId)
6684 ssize_t index = mEntries.indexOfKey(packageName);
6686 return UNKNOWN_ERROR;
6688 mLookupTable[mEntries.valueAt(index)] = packageId;
6692 void DynamicRefTable::addMapping(uint8_t buildPackageId, uint8_t runtimePackageId) {
6693 mLookupTable[buildPackageId] = runtimePackageId;
6696 status_t DynamicRefTable::lookupResourceId(uint32_t* resId) const {
6697 uint32_t res = *resId;
6698 size_t packageId = Res_GETPACKAGE(res) + 1;
6700 if (packageId == APP_PACKAGE_ID && !mAppAsLib) {
6701 // No lookup needs to be done, app package IDs are absolute.
6705 if (packageId == 0 || (packageId == APP_PACKAGE_ID && mAppAsLib)) {
6706 // The package ID is 0x00. That means that a shared library is accessing
6707 // its own local resource.
6708 // Or if app resource is loaded as shared library, the resource which has
6709 // app package Id is local resources.
6710 // so we fix up those resources with the calling package ID.
6711 *resId = (0xFFFFFF & (*resId)) | (((uint32_t) mAssignedPackageId) << 24);
6715 // Do a proper lookup.
6716 uint8_t translatedId = mLookupTable[packageId];
6717 if (translatedId == 0) {
6718 ALOGW("DynamicRefTable(0x%02x): No mapping for build-time package ID 0x%02x.",
6719 (uint8_t)mAssignedPackageId, (uint8_t)packageId);
6720 for (size_t i = 0; i < 256; i++) {
6721 if (mLookupTable[i] != 0) {
6722 ALOGW("e[0x%02x] -> 0x%02x", (uint8_t)i, mLookupTable[i]);
6725 return UNKNOWN_ERROR;
6728 *resId = (res & 0x00ffffff) | (((uint32_t) translatedId) << 24);
6732 status_t DynamicRefTable::lookupResourceValue(Res_value* value) const {
6733 uint8_t resolvedType = Res_value::TYPE_REFERENCE;
6734 switch (value->dataType) {
6735 case Res_value::TYPE_ATTRIBUTE:
6736 resolvedType = Res_value::TYPE_ATTRIBUTE;
6738 case Res_value::TYPE_REFERENCE:
6743 // If the package is loaded as shared library, the resource reference
6744 // also need to be fixed.
6746 case Res_value::TYPE_DYNAMIC_ATTRIBUTE:
6747 resolvedType = Res_value::TYPE_ATTRIBUTE;
6749 case Res_value::TYPE_DYNAMIC_REFERENCE:
6755 status_t err = lookupResourceId(&value->data);
6756 if (err != NO_ERROR) {
6760 value->dataType = resolvedType;
6764 struct IdmapTypeMap {
6765 ssize_t overlayTypeId;
6767 Vector<uint32_t> entryMap;
6770 status_t ResTable::createIdmap(const ResTable& overlay,
6771 uint32_t targetCrc, uint32_t overlayCrc,
6772 const char* targetPath, const char* overlayPath,
6773 void** outData, size_t* outSize) const
6775 // see README for details on the format of map
6776 if (mPackageGroups.size() == 0) {
6777 ALOGW("idmap: target package has no package groups, cannot create idmap\n");
6778 return UNKNOWN_ERROR;
6781 if (mPackageGroups[0]->packages.size() == 0) {
6782 ALOGW("idmap: target package has no packages in its first package group, "
6783 "cannot create idmap\n");
6784 return UNKNOWN_ERROR;
6787 KeyedVector<uint8_t, IdmapTypeMap> map;
6789 // overlaid packages are assumed to contain only one package group
6790 const PackageGroup* pg = mPackageGroups[0];
6792 // starting size is header
6793 *outSize = ResTable::IDMAP_HEADER_SIZE_BYTES;
6795 // target package id and number of types in map
6796 *outSize += 2 * sizeof(uint16_t);
6798 // overlay packages are assumed to contain only one package group
6799 const ResTable_package* overlayPackageStruct = overlay.mPackageGroups[0]->packages[0]->package;
6800 char16_t tmpName[sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])];
6801 strcpy16_dtoh(tmpName, overlayPackageStruct->name, sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0]));
6802 const String16 overlayPackage(tmpName);
6804 for (size_t typeIndex = 0; typeIndex < pg->types.size(); ++typeIndex) {
6805 const TypeList& typeList = pg->types[typeIndex];
6806 if (typeList.isEmpty()) {
6810 const Type* typeConfigs = typeList[0];
6812 IdmapTypeMap typeMap;
6813 typeMap.overlayTypeId = -1;
6814 typeMap.entryOffset = 0;
6816 for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) {
6817 uint32_t resID = Res_MAKEID(pg->id - 1, typeIndex, entryIndex);
6818 resource_name resName;
6819 if (!this->getResourceName(resID, false, &resName)) {
6820 if (typeMap.entryMap.isEmpty()) {
6821 typeMap.entryOffset++;
6826 const String16 overlayType(resName.type, resName.typeLen);
6827 const String16 overlayName(resName.name, resName.nameLen);
6828 uint32_t overlayResID = overlay.identifierForName(overlayName.string(),
6830 overlayType.string(),
6832 overlayPackage.string(),
6833 overlayPackage.size());
6834 if (overlayResID == 0) {
6835 if (typeMap.entryMap.isEmpty()) {
6836 typeMap.entryOffset++;
6841 if (typeMap.overlayTypeId == -1) {
6842 typeMap.overlayTypeId = Res_GETTYPE(overlayResID) + 1;
6845 if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) {
6846 ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x"
6847 " but entries should map to resources of type %02zx",
6848 resID, overlayResID, typeMap.overlayTypeId);
6852 if (typeMap.entryOffset + typeMap.entryMap.size() < entryIndex) {
6853 // pad with 0xffffffff's (indicating non-existing entries) before adding this entry
6854 size_t index = typeMap.entryMap.size();
6855 size_t numItems = entryIndex - (typeMap.entryOffset + index);
6856 if (typeMap.entryMap.insertAt(0xffffffff, index, numItems) < 0) {
6860 typeMap.entryMap.add(Res_GETENTRY(overlayResID));
6863 if (!typeMap.entryMap.isEmpty()) {
6864 if (map.add(static_cast<uint8_t>(typeIndex), typeMap) < 0) {
6867 *outSize += (4 * sizeof(uint16_t)) + (typeMap.entryMap.size() * sizeof(uint32_t));
6871 if (map.isEmpty()) {
6872 ALOGW("idmap: no resources in overlay package present in base package");
6873 return UNKNOWN_ERROR;
6876 if ((*outData = malloc(*outSize)) == NULL) {
6880 uint32_t* data = (uint32_t*)*outData;
6881 *data++ = htodl(IDMAP_MAGIC);
6882 *data++ = htodl(IDMAP_CURRENT_VERSION);
6883 *data++ = htodl(targetCrc);
6884 *data++ = htodl(overlayCrc);
6885 const char* paths[] = { targetPath, overlayPath };
6886 for (int j = 0; j < 2; ++j) {
6887 char* p = (char*)data;
6888 const char* path = paths[j];
6889 const size_t I = strlen(path);
6891 ALOGV("path exceeds expected 255 characters: %s\n", path);
6892 return UNKNOWN_ERROR;
6894 for (size_t i = 0; i < 256; ++i) {
6895 *p++ = i < I ? path[i] : '\0';
6897 data += 256 / sizeof(uint32_t);
6899 const size_t mapSize = map.size();
6900 uint16_t* typeData = reinterpret_cast<uint16_t*>(data);
6901 *typeData++ = htods(pg->id);
6902 *typeData++ = htods(mapSize);
6903 for (size_t i = 0; i < mapSize; ++i) {
6904 uint8_t targetTypeId = map.keyAt(i);
6905 const IdmapTypeMap& typeMap = map[i];
6906 *typeData++ = htods(targetTypeId + 1);
6907 *typeData++ = htods(typeMap.overlayTypeId);
6908 *typeData++ = htods(typeMap.entryMap.size());
6909 *typeData++ = htods(typeMap.entryOffset);
6911 const size_t entryCount = typeMap.entryMap.size();
6912 uint32_t* entries = reinterpret_cast<uint32_t*>(typeData);
6913 for (size_t j = 0; j < entryCount; j++) {
6914 entries[j] = htodl(typeMap.entryMap[j]);
6916 typeData += entryCount * 2;
6922 bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes,
6924 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
6925 String8* pTargetPath, String8* pOverlayPath)
6927 const uint32_t* map = (const uint32_t*)idmap;
6928 if (!assertIdmapHeader(map, sizeBytes)) {
6932 *pVersion = dtohl(map[1]);
6935 *pTargetCrc = dtohl(map[2]);
6938 *pOverlayCrc = dtohl(map[3]);
6941 pTargetPath->setTo(reinterpret_cast<const char*>(map + 4));
6944 pOverlayPath->setTo(reinterpret_cast<const char*>(map + 4 + 256 / sizeof(uint32_t)));
6950 #define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string())
6952 #define CHAR16_ARRAY_EQ(constant, var, len) \
6953 (((len) == (sizeof(constant)/sizeof((constant)[0]))) && (0 == memcmp((var), (constant), (len))))
6955 static void print_complex(uint32_t complex, bool isFraction)
6957 const float MANTISSA_MULT =
6958 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT);
6959 const float RADIX_MULTS[] = {
6960 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
6961 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
6964 float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK
6965 <<Res_value::COMPLEX_MANTISSA_SHIFT))
6966 * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT)
6967 & Res_value::COMPLEX_RADIX_MASK];
6968 printf("%f", value);
6971 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6972 case Res_value::COMPLEX_UNIT_PX: printf("px"); break;
6973 case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break;
6974 case Res_value::COMPLEX_UNIT_SP: printf("sp"); break;
6975 case Res_value::COMPLEX_UNIT_PT: printf("pt"); break;
6976 case Res_value::COMPLEX_UNIT_IN: printf("in"); break;
6977 case Res_value::COMPLEX_UNIT_MM: printf("mm"); break;
6978 default: printf(" (unknown unit)"); break;
6981 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6982 case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break;
6983 case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break;
6984 default: printf(" (unknown unit)"); break;
6989 // Normalize a string for output
6990 String8 ResTable::normalizeForOutput( const char *input )
6996 while (*input != '\0') {
6998 // All interesting characters are in the ASCII zone, so we are making our own lives
6999 // easier by scanning the string one byte at a time.
7021 void ResTable::print_value(const Package* pkg, const Res_value& value) const
7023 if (value.dataType == Res_value::TYPE_NULL) {
7024 if (value.data == Res_value::DATA_NULL_UNDEFINED) {
7026 } else if (value.data == Res_value::DATA_NULL_EMPTY) {
7027 printf("(null empty)\n");
7029 // This should never happen.
7030 printf("(null) 0x%08x\n", value.data);
7032 } else if (value.dataType == Res_value::TYPE_REFERENCE) {
7033 printf("(reference) 0x%08x\n", value.data);
7034 } else if (value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) {
7035 printf("(dynamic reference) 0x%08x\n", value.data);
7036 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
7037 printf("(attribute) 0x%08x\n", value.data);
7038 } else if (value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
7039 printf("(dynamic attribute) 0x%08x\n", value.data);
7040 } else if (value.dataType == Res_value::TYPE_STRING) {
7042 const char* str8 = pkg->header->values.string8At(
7045 printf("(string8) \"%s\"\n", normalizeForOutput(str8).string());
7047 const char16_t* str16 = pkg->header->values.stringAt(
7049 if (str16 != NULL) {
7050 printf("(string16) \"%s\"\n",
7051 normalizeForOutput(String8(str16, len).string()).string());
7053 printf("(string) null\n");
7056 } else if (value.dataType == Res_value::TYPE_FLOAT) {
7057 printf("(float) %g\n", *(const float*)&value.data);
7058 } else if (value.dataType == Res_value::TYPE_DIMENSION) {
7059 printf("(dimension) ");
7060 print_complex(value.data, false);
7062 } else if (value.dataType == Res_value::TYPE_FRACTION) {
7063 printf("(fraction) ");
7064 print_complex(value.data, true);
7066 } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT
7067 || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) {
7068 printf("(color) #%08x\n", value.data);
7069 } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) {
7070 printf("(boolean) %s\n", value.data ? "true" : "false");
7071 } else if (value.dataType >= Res_value::TYPE_FIRST_INT
7072 || value.dataType <= Res_value::TYPE_LAST_INT) {
7073 printf("(int) 0x%08x or %d\n", value.data, value.data);
7075 printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n",
7076 (int)value.dataType, (int)value.data,
7077 (int)value.size, (int)value.res0);
7081 void ResTable::print(bool inclValues) const
7084 printf("mError=0x%x (%s)\n", mError, strerror(mError));
7086 size_t pgCount = mPackageGroups.size();
7087 printf("Package Groups (%d)\n", (int)pgCount);
7088 for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) {
7089 const PackageGroup* pg = mPackageGroups[pgIndex];
7090 printf("Package Group %d id=0x%02x packageCount=%d name=%s\n",
7091 (int)pgIndex, pg->id, (int)pg->packages.size(),
7092 String8(pg->name).string());
7094 const KeyedVector<String16, uint8_t>& refEntries = pg->dynamicRefTable.entries();
7095 const size_t refEntryCount = refEntries.size();
7096 if (refEntryCount > 0) {
7097 printf(" DynamicRefTable entryCount=%d:\n", (int) refEntryCount);
7098 for (size_t refIndex = 0; refIndex < refEntryCount; refIndex++) {
7099 printf(" 0x%02x -> %s\n",
7100 refEntries.valueAt(refIndex),
7101 String8(refEntries.keyAt(refIndex)).string());
7106 int packageId = pg->id;
7107 size_t pkgCount = pg->packages.size();
7108 for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) {
7109 const Package* pkg = pg->packages[pkgIndex];
7110 // Use a package's real ID, since the ID may have been assigned
7111 // if this package is a shared library.
7112 packageId = pkg->package->id;
7113 char16_t tmpName[sizeof(pkg->package->name)/sizeof(pkg->package->name[0])];
7114 strcpy16_dtoh(tmpName, pkg->package->name, sizeof(pkg->package->name)/sizeof(pkg->package->name[0]));
7115 printf(" Package %d id=0x%02x name=%s\n", (int)pkgIndex,
7116 pkg->package->id, String8(tmpName).string());
7119 for (size_t typeIndex=0; typeIndex < pg->types.size(); typeIndex++) {
7120 const TypeList& typeList = pg->types[typeIndex];
7121 if (typeList.isEmpty()) {
7124 const Type* typeConfigs = typeList[0];
7125 const size_t NTC = typeConfigs->configs.size();
7126 printf(" type %d configCount=%d entryCount=%d\n",
7127 (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount);
7128 if (typeConfigs->typeSpecFlags != NULL) {
7129 for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) {
7130 uint32_t resID = (0xff000000 & ((packageId)<<24))
7131 | (0x00ff0000 & ((typeIndex+1)<<16))
7132 | (0x0000ffff & (entryIndex));
7133 // Since we are creating resID without actually
7134 // iterating over them, we have no idea which is a
7135 // dynamic reference. We must check.
7136 if (packageId == 0) {
7137 pg->dynamicRefTable.lookupResourceId(&resID);
7140 resource_name resName;
7141 if (this->getResourceName(resID, true, &resName)) {
7144 if (resName.type8 != NULL) {
7145 type8 = String8(resName.type8, resName.typeLen);
7147 type8 = String8(resName.type, resName.typeLen);
7149 if (resName.name8 != NULL) {
7150 name8 = String8(resName.name8, resName.nameLen);
7152 name8 = String8(resName.name, resName.nameLen);
7154 printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n",
7156 CHAR16_TO_CSTR(resName.package, resName.packageLen),
7157 type8.string(), name8.string(),
7158 dtohl(typeConfigs->typeSpecFlags[entryIndex]));
7160 printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID);
7164 for (size_t configIndex=0; configIndex<NTC; configIndex++) {
7165 const ResTable_type* type = typeConfigs->configs[configIndex];
7166 if ((((uint64_t)type)&0x3) != 0) {
7167 printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type);
7171 // Always copy the config, as fields get added and we need to
7172 // set the defaults.
7173 ResTable_config thisConfig;
7174 thisConfig.copyFromDtoH(type->config);
7176 String8 configStr = thisConfig.toString();
7177 printf(" config %s", configStr.size() > 0
7178 ? configStr.string() : "(default)");
7179 if (type->flags != 0u) {
7180 printf(" flags=0x%02x", type->flags);
7181 if (type->flags & ResTable_type::FLAG_SPARSE) {
7182 printf(" [sparse]");
7188 size_t entryCount = dtohl(type->entryCount);
7189 uint32_t entriesStart = dtohl(type->entriesStart);
7190 if ((entriesStart&0x3) != 0) {
7191 printf(" NON-INTEGER ResTable_type entriesStart OFFSET: 0x%x\n", entriesStart);
7194 uint32_t typeSize = dtohl(type->header.size);
7195 if ((typeSize&0x3) != 0) {
7196 printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize);
7200 const uint32_t* const eindex = (const uint32_t*)
7201 (((const uint8_t*)type) + dtohs(type->header.headerSize));
7202 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) {
7204 uint32_t thisOffset;
7205 if (type->flags & ResTable_type::FLAG_SPARSE) {
7206 const ResTable_sparseTypeEntry* entry =
7207 reinterpret_cast<const ResTable_sparseTypeEntry*>(
7208 eindex + entryIndex);
7209 entryId = dtohs(entry->idx);
7210 // Offsets are encoded as divided by 4.
7211 thisOffset = static_cast<uint32_t>(dtohs(entry->offset)) * 4u;
7213 entryId = entryIndex;
7214 thisOffset = dtohl(eindex[entryIndex]);
7215 if (thisOffset == ResTable_type::NO_ENTRY) {
7220 uint32_t resID = (0xff000000 & ((packageId)<<24))
7221 | (0x00ff0000 & ((typeIndex+1)<<16))
7222 | (0x0000ffff & (entryId));
7223 if (packageId == 0) {
7224 pg->dynamicRefTable.lookupResourceId(&resID);
7226 resource_name resName;
7227 if (this->getResourceName(resID, true, &resName)) {
7230 if (resName.type8 != NULL) {
7231 type8 = String8(resName.type8, resName.typeLen);
7233 type8 = String8(resName.type, resName.typeLen);
7235 if (resName.name8 != NULL) {
7236 name8 = String8(resName.name8, resName.nameLen);
7238 name8 = String8(resName.name, resName.nameLen);
7240 printf(" resource 0x%08x %s:%s/%s: ", resID,
7241 CHAR16_TO_CSTR(resName.package, resName.packageLen),
7242 type8.string(), name8.string());
7244 printf(" INVALID RESOURCE 0x%08x: ", resID);
7246 if ((thisOffset&0x3) != 0) {
7247 printf("NON-INTEGER OFFSET: 0x%x\n", thisOffset);
7250 if ((thisOffset+sizeof(ResTable_entry)) > typeSize) {
7251 printf("OFFSET OUT OF BOUNDS: 0x%x+0x%x (size is 0x%x)\n",
7252 entriesStart, thisOffset, typeSize);
7256 const ResTable_entry* ent = (const ResTable_entry*)
7257 (((const uint8_t*)type) + entriesStart + thisOffset);
7258 if (((entriesStart + thisOffset)&0x3) != 0) {
7259 printf("NON-INTEGER ResTable_entry OFFSET: 0x%x\n",
7260 (entriesStart + thisOffset));
7264 uintptr_t esize = dtohs(ent->size);
7265 if ((esize&0x3) != 0) {
7266 printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void *)esize);
7269 if ((thisOffset+esize) > typeSize) {
7270 printf("ResTable_entry OUT OF BOUNDS: 0x%x+0x%x+%p (size is 0x%x)\n",
7271 entriesStart, thisOffset, (void *)esize, typeSize);
7275 const Res_value* valuePtr = NULL;
7276 const ResTable_map_entry* bagPtr = NULL;
7278 if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) {
7280 bagPtr = (const ResTable_map_entry*)ent;
7282 valuePtr = (const Res_value*)
7283 (((const uint8_t*)ent) + esize);
7284 value.copyFrom_dtoh(*valuePtr);
7285 printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)",
7286 (int)value.dataType, (int)value.data,
7287 (int)value.size, (int)value.res0);
7290 if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) {
7291 printf(" (PUBLIC)");
7296 if (valuePtr != NULL) {
7298 print_value(typeConfigs->package, value);
7299 } else if (bagPtr != NULL) {
7300 const int N = dtohl(bagPtr->count);
7301 const uint8_t* baseMapPtr = (const uint8_t*)ent;
7302 size_t mapOffset = esize;
7303 const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
7304 const uint32_t parent = dtohl(bagPtr->parent.ident);
7305 uint32_t resolvedParent = parent;
7306 if (Res_GETPACKAGE(resolvedParent) + 1 == 0) {
7307 status_t err = pg->dynamicRefTable.lookupResourceId(&resolvedParent);
7308 if (err != NO_ERROR) {
7312 printf(" Parent=0x%08x(Resolved=0x%08x), Count=%d\n",
7313 parent, resolvedParent, N);
7314 for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) {
7315 printf(" #%i (Key=0x%08x): ",
7316 i, dtohl(mapPtr->name.ident));
7317 value.copyFrom_dtoh(mapPtr->value);
7318 print_value(typeConfigs->package, value);
7319 const size_t size = dtohs(mapPtr->value.size);
7320 mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value);
7321 mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
7331 } // namespace android