blob: 7241069039a970cd15cb1f4cfa47fc2ade1ebd2a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ResourceType"
18//#define LOG_NDEBUG 0
19
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070020#include <androidfw/ByteBucketArray.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080021#include <androidfw/ResourceTypes.h>
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070022#include <androidfw/TypeWrappers.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <utils/Atomic.h>
24#include <utils/ByteOrder.h>
25#include <utils/Debug.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080026#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <utils/String16.h>
28#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Andreas Gampe2204f0b2014-10-21 23:04:54 -070030#ifdef HAVE_ANDROID_OS
31#include <binder/TextOutput.h>
32#endif
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <stdlib.h>
35#include <string.h>
36#include <memory.h>
37#include <ctype.h>
38#include <stdint.h>
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070039#include <stddef.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41#ifndef INT32_MAX
42#define INT32_MAX ((int32_t)(2147483647))
43#endif
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045namespace android {
46
47#ifdef HAVE_WINSOCK
48#undef nhtol
49#undef htonl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
51#define htonl(x) ntohl(x)
52#define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
53#define htons(x) ntohs(x)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054#endif
55
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070056#define IDMAP_MAGIC 0x504D4449
57#define IDMAP_CURRENT_VERSION 0x00000001
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +010058
Adam Lesinskide898ff2014-01-29 18:20:45 -080059#define APP_PACKAGE_ID 0x7f
60#define SYS_PACKAGE_ID 0x01
61
Andreas Gampe2204f0b2014-10-21 23:04:54 -070062static const bool kDebugStringPoolNoisy = false;
63static const bool kDebugXMLNoisy = false;
64static const bool kDebugTableNoisy = false;
65static const bool kDebugTableGetEntry = false;
66static const bool kDebugTableSuperNoisy = false;
67static const bool kDebugLoadTableNoisy = false;
68static const bool kDebugLoadTableSuperNoisy = false;
69static const bool kDebugTableTheme = false;
70static const bool kDebugResXMLTree = false;
71static const bool kDebugLibNoisy = false;
72
73// TODO: This code uses 0xFFFFFFFF converted to bag_set* as a sentinel value. This is bad practice.
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075// Standard C isspace() is only required to look at the low byte of its input, so
76// produces incorrect results for UTF-16 characters. For safety's sake, assume that
77// any high-byte UTF-16 code point is not whitespace.
78inline int isspace16(char16_t c) {
79 return (c < 0x0080 && isspace(c));
80}
81
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070082template<typename T>
83inline static T max(T a, T b) {
84 return a > b ? a : b;
85}
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087// range checked; guaranteed to NUL-terminate within the stated number of available slots
88// NOTE: if this truncates the dst string due to running out of space, no attempt is
89// made to avoid splitting surrogate pairs.
Adam Lesinski4bf58102014-11-03 11:21:19 -080090static void strcpy16_dtoh(char16_t* dst, const uint16_t* src, size_t avail)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091{
Adam Lesinski4bf58102014-11-03 11:21:19 -080092 char16_t* last = dst + avail - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 while (*src && (dst < last)) {
Adam Lesinski4bf58102014-11-03 11:21:19 -080094 char16_t s = dtohs(static_cast<char16_t>(*src));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 *dst++ = s;
96 src++;
97 }
98 *dst = 0;
99}
100
101static status_t validate_chunk(const ResChunk_header* chunk,
102 size_t minSize,
103 const uint8_t* dataEnd,
104 const char* name)
105{
106 const uint16_t headerSize = dtohs(chunk->headerSize);
107 const uint32_t size = dtohl(chunk->size);
108
109 if (headerSize >= minSize) {
110 if (headerSize <= size) {
111 if (((headerSize|size)&0x3) == 0) {
Adam Lesinski7322ea72014-05-14 11:43:26 -0700112 if ((size_t)size <= (size_t)(dataEnd-((const uint8_t*)chunk))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 return NO_ERROR;
114 }
Patrik Bannura443dd932014-02-12 13:38:54 +0100115 ALOGW("%s data size 0x%x extends beyond resource end %p.",
116 name, size, (void*)(dataEnd-((const uint8_t*)chunk)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 return BAD_TYPE;
118 }
Steve Block8564c8d2012-01-05 23:22:43 +0000119 ALOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 name, (int)size, (int)headerSize);
121 return BAD_TYPE;
122 }
Patrik Bannura443dd932014-02-12 13:38:54 +0100123 ALOGW("%s size 0x%x is smaller than header size 0x%x.",
124 name, size, headerSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 return BAD_TYPE;
126 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800127 ALOGW("%s header size 0x%04x is too small.",
Patrik Bannura443dd932014-02-12 13:38:54 +0100128 name, headerSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 return BAD_TYPE;
130}
131
Narayan Kamath6381dd42014-03-03 17:12:03 +0000132static void fill9patchOffsets(Res_png_9patch* patch) {
133 patch->xDivsOffset = sizeof(Res_png_9patch);
134 patch->yDivsOffset = patch->xDivsOffset + (patch->numXDivs * sizeof(int32_t));
135 patch->colorsOffset = patch->yDivsOffset + (patch->numYDivs * sizeof(int32_t));
136}
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138inline void Res_value::copyFrom_dtoh(const Res_value& src)
139{
140 size = dtohs(src.size);
141 res0 = src.res0;
142 dataType = src.dataType;
143 data = dtohl(src.data);
144}
145
146void Res_png_9patch::deviceToFile()
147{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000148 int32_t* xDivs = getXDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 for (int i = 0; i < numXDivs; i++) {
150 xDivs[i] = htonl(xDivs[i]);
151 }
Narayan Kamath6381dd42014-03-03 17:12:03 +0000152 int32_t* yDivs = getYDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 for (int i = 0; i < numYDivs; i++) {
154 yDivs[i] = htonl(yDivs[i]);
155 }
156 paddingLeft = htonl(paddingLeft);
157 paddingRight = htonl(paddingRight);
158 paddingTop = htonl(paddingTop);
159 paddingBottom = htonl(paddingBottom);
Narayan Kamath6381dd42014-03-03 17:12:03 +0000160 uint32_t* colors = getColors();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 for (int i=0; i<numColors; i++) {
162 colors[i] = htonl(colors[i]);
163 }
164}
165
166void Res_png_9patch::fileToDevice()
167{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000168 int32_t* xDivs = getXDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 for (int i = 0; i < numXDivs; i++) {
170 xDivs[i] = ntohl(xDivs[i]);
171 }
Narayan Kamath6381dd42014-03-03 17:12:03 +0000172 int32_t* yDivs = getYDivs();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 for (int i = 0; i < numYDivs; i++) {
174 yDivs[i] = ntohl(yDivs[i]);
175 }
176 paddingLeft = ntohl(paddingLeft);
177 paddingRight = ntohl(paddingRight);
178 paddingTop = ntohl(paddingTop);
179 paddingBottom = ntohl(paddingBottom);
Narayan Kamath6381dd42014-03-03 17:12:03 +0000180 uint32_t* colors = getColors();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 for (int i=0; i<numColors; i++) {
182 colors[i] = ntohl(colors[i]);
183 }
184}
185
Narayan Kamath6381dd42014-03-03 17:12:03 +0000186size_t Res_png_9patch::serializedSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187{
188 // The size of this struct is 32 bytes on the 32-bit target system
189 // 4 * int8_t
190 // 4 * int32_t
Narayan Kamath6381dd42014-03-03 17:12:03 +0000191 // 3 * uint32_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return 32
193 + numXDivs * sizeof(int32_t)
194 + numYDivs * sizeof(int32_t)
195 + numColors * sizeof(uint32_t);
196}
197
Narayan Kamath6381dd42014-03-03 17:12:03 +0000198void* Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
199 const int32_t* yDivs, const uint32_t* colors)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200{
The Android Open Source Project4df24232009-03-05 14:34:35 -0800201 // Use calloc since we're going to leave a few holes in the data
202 // and want this to run cleanly under valgrind
Narayan Kamath6381dd42014-03-03 17:12:03 +0000203 void* newData = calloc(1, patch.serializedSize());
204 serialize(patch, xDivs, yDivs, colors, newData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 return newData;
206}
207
Narayan Kamath6381dd42014-03-03 17:12:03 +0000208void Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs,
209 const int32_t* yDivs, const uint32_t* colors, void* outData)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000211 uint8_t* data = (uint8_t*) outData;
212 memcpy(data, &patch.wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors
213 memcpy(data + 12, &patch.paddingLeft, 16); // copy paddingXXXX
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 data += 32;
215
Narayan Kamath6381dd42014-03-03 17:12:03 +0000216 memcpy(data, xDivs, patch.numXDivs * sizeof(int32_t));
217 data += patch.numXDivs * sizeof(int32_t);
218 memcpy(data, yDivs, patch.numYDivs * sizeof(int32_t));
219 data += patch.numYDivs * sizeof(int32_t);
220 memcpy(data, colors, patch.numColors * sizeof(uint32_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
Narayan Kamath6381dd42014-03-03 17:12:03 +0000222 fill9patchOffsets(reinterpret_cast<Res_png_9patch*>(outData));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223}
224
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700225static bool assertIdmapHeader(const void* idmap, size_t size) {
226 if (reinterpret_cast<uintptr_t>(idmap) & 0x03) {
227 ALOGE("idmap: header is not word aligned");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100228 return false;
229 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700230
231 if (size < ResTable::IDMAP_HEADER_SIZE_BYTES) {
232 ALOGW("idmap: header too small (%d bytes)", (uint32_t) size);
233 return false;
234 }
235
236 const uint32_t magic = htodl(*reinterpret_cast<const uint32_t*>(idmap));
237 if (magic != IDMAP_MAGIC) {
238 ALOGW("idmap: no magic found in header (is 0x%08x, expected 0x%08x)",
239 magic, IDMAP_MAGIC);
240 return false;
241 }
242
243 const uint32_t version = htodl(*(reinterpret_cast<const uint32_t*>(idmap) + 1));
244 if (version != IDMAP_CURRENT_VERSION) {
245 // We are strict about versions because files with this format are
246 // auto-generated and don't need backwards compatibility.
247 ALOGW("idmap: version mismatch in header (is 0x%08x, expected 0x%08x)",
248 version, IDMAP_CURRENT_VERSION);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100249 return false;
250 }
251 return true;
252}
253
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700254class IdmapEntries {
255public:
256 IdmapEntries() : mData(NULL) {}
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100257
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700258 bool hasEntries() const {
259 if (mData == NULL) {
260 return false;
261 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100262
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700263 return (dtohs(*mData) > 0);
264 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100265
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700266 size_t byteSize() const {
267 if (mData == NULL) {
268 return 0;
269 }
270 uint16_t entryCount = dtohs(mData[2]);
271 return (sizeof(uint16_t) * 4) + (sizeof(uint32_t) * static_cast<size_t>(entryCount));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100272 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700273
274 uint8_t targetTypeId() const {
275 if (mData == NULL) {
276 return 0;
277 }
278 return dtohs(mData[0]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100279 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700280
281 uint8_t overlayTypeId() const {
282 if (mData == NULL) {
283 return 0;
284 }
285 return dtohs(mData[1]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100286 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700287
288 status_t setTo(const void* entryHeader, size_t size) {
289 if (reinterpret_cast<uintptr_t>(entryHeader) & 0x03) {
290 ALOGE("idmap: entry header is not word aligned");
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100291 return UNKNOWN_ERROR;
292 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700293
294 if (size < sizeof(uint16_t) * 4) {
295 ALOGE("idmap: entry header is too small (%u bytes)", (uint32_t) size);
296 return UNKNOWN_ERROR;
297 }
298
299 const uint16_t* header = reinterpret_cast<const uint16_t*>(entryHeader);
300 const uint16_t targetTypeId = dtohs(header[0]);
301 const uint16_t overlayTypeId = dtohs(header[1]);
302 if (targetTypeId == 0 || overlayTypeId == 0 || targetTypeId > 255 || overlayTypeId > 255) {
303 ALOGE("idmap: invalid type map (%u -> %u)", targetTypeId, overlayTypeId);
304 return UNKNOWN_ERROR;
305 }
306
307 uint16_t entryCount = dtohs(header[2]);
308 if (size < sizeof(uint32_t) * (entryCount + 2)) {
309 ALOGE("idmap: too small (%u bytes) for the number of entries (%u)",
310 (uint32_t) size, (uint32_t) entryCount);
311 return UNKNOWN_ERROR;
312 }
313 mData = header;
314 return NO_ERROR;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100315 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100316
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700317 status_t lookup(uint16_t entryId, uint16_t* outEntryId) const {
318 uint16_t entryCount = dtohs(mData[2]);
319 uint16_t offset = dtohs(mData[3]);
320
321 if (entryId < offset) {
322 // The entry is not present in this idmap
323 return BAD_INDEX;
324 }
325
326 entryId -= offset;
327
328 if (entryId >= entryCount) {
329 // The entry is not present in this idmap
330 return BAD_INDEX;
331 }
332
333 // It is safe to access the type here without checking the size because
334 // we have checked this when it was first loaded.
335 const uint32_t* entries = reinterpret_cast<const uint32_t*>(mData) + 2;
336 uint32_t mappedEntry = dtohl(entries[entryId]);
337 if (mappedEntry == 0xffffffff) {
338 // This entry is not present in this idmap
339 return BAD_INDEX;
340 }
341 *outEntryId = static_cast<uint16_t>(mappedEntry);
342 return NO_ERROR;
343 }
344
345private:
346 const uint16_t* mData;
347};
348
349status_t parseIdmap(const void* idmap, size_t size, uint8_t* outPackageId, KeyedVector<uint8_t, IdmapEntries>* outMap) {
350 if (!assertIdmapHeader(idmap, size)) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100351 return UNKNOWN_ERROR;
352 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100353
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700354 size -= ResTable::IDMAP_HEADER_SIZE_BYTES;
355 if (size < sizeof(uint16_t) * 2) {
356 ALOGE("idmap: too small to contain any mapping");
357 return UNKNOWN_ERROR;
358 }
359
360 const uint16_t* data = reinterpret_cast<const uint16_t*>(
361 reinterpret_cast<const uint8_t*>(idmap) + ResTable::IDMAP_HEADER_SIZE_BYTES);
362
363 uint16_t targetPackageId = dtohs(*(data++));
364 if (targetPackageId == 0 || targetPackageId > 255) {
365 ALOGE("idmap: target package ID is invalid (%02x)", targetPackageId);
366 return UNKNOWN_ERROR;
367 }
368
369 uint16_t mapCount = dtohs(*(data++));
370 if (mapCount == 0) {
371 ALOGE("idmap: no mappings");
372 return UNKNOWN_ERROR;
373 }
374
375 if (mapCount > 255) {
376 ALOGW("idmap: too many mappings. Only 255 are possible but %u are present", (uint32_t) mapCount);
377 }
378
379 while (size > sizeof(uint16_t) * 4) {
380 IdmapEntries entries;
381 status_t err = entries.setTo(data, size);
382 if (err != NO_ERROR) {
383 return err;
384 }
385
386 ssize_t index = outMap->add(entries.overlayTypeId(), entries);
387 if (index < 0) {
388 return NO_MEMORY;
389 }
390
391 data += entries.byteSize() / sizeof(uint16_t);
392 size -= entries.byteSize();
393 }
394
395 if (outPackageId != NULL) {
396 *outPackageId = static_cast<uint8_t>(targetPackageId);
397 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +0100398 return NO_ERROR;
399}
400
Narayan Kamath6381dd42014-03-03 17:12:03 +0000401Res_png_9patch* Res_png_9patch::deserialize(void* inData)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000403
404 Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(inData);
405 patch->wasDeserialized = true;
406 fill9patchOffsets(patch);
407
408 return patch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409}
410
411// --------------------------------------------------------------------
412// --------------------------------------------------------------------
413// --------------------------------------------------------------------
414
415ResStringPool::ResStringPool()
Kenny Root19138462009-12-04 09:38:48 -0800416 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417{
418}
419
420ResStringPool::ResStringPool(const void* data, size_t size, bool copyData)
Kenny Root19138462009-12-04 09:38:48 -0800421 : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422{
423 setTo(data, size, copyData);
424}
425
426ResStringPool::~ResStringPool()
427{
428 uninit();
429}
430
Adam Lesinskide898ff2014-01-29 18:20:45 -0800431void ResStringPool::setToEmpty()
432{
433 uninit();
434
435 mOwnedData = calloc(1, sizeof(ResStringPool_header));
436 ResStringPool_header* header = (ResStringPool_header*) mOwnedData;
437 mSize = 0;
438 mEntries = NULL;
439 mStrings = NULL;
440 mStringPoolSize = 0;
441 mEntryStyles = NULL;
442 mStyles = NULL;
443 mStylePoolSize = 0;
444 mHeader = (const ResStringPool_header*) header;
445}
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447status_t ResStringPool::setTo(const void* data, size_t size, bool copyData)
448{
449 if (!data || !size) {
450 return (mError=BAD_TYPE);
451 }
452
453 uninit();
454
455 const bool notDeviceEndian = htods(0xf0) != 0xf0;
456
457 if (copyData || notDeviceEndian) {
458 mOwnedData = malloc(size);
459 if (mOwnedData == NULL) {
460 return (mError=NO_MEMORY);
461 }
462 memcpy(mOwnedData, data, size);
463 data = mOwnedData;
464 }
465
466 mHeader = (const ResStringPool_header*)data;
467
468 if (notDeviceEndian) {
469 ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader);
470 h->header.headerSize = dtohs(mHeader->header.headerSize);
471 h->header.type = dtohs(mHeader->header.type);
472 h->header.size = dtohl(mHeader->header.size);
473 h->stringCount = dtohl(mHeader->stringCount);
474 h->styleCount = dtohl(mHeader->styleCount);
475 h->flags = dtohl(mHeader->flags);
476 h->stringsStart = dtohl(mHeader->stringsStart);
477 h->stylesStart = dtohl(mHeader->stylesStart);
478 }
479
480 if (mHeader->header.headerSize > mHeader->header.size
481 || mHeader->header.size > size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000482 ALOGW("Bad string block: header size %d or total size %d is larger than data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size);
484 return (mError=BAD_TYPE);
485 }
486 mSize = mHeader->header.size;
487 mEntries = (const uint32_t*)
488 (((const uint8_t*)data)+mHeader->header.headerSize);
489
490 if (mHeader->stringCount > 0) {
491 if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow?
492 || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t)))
493 > size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000494 ALOGW("Bad string block: entry of %d items extends past data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))),
496 (int)size);
497 return (mError=BAD_TYPE);
498 }
Kenny Root19138462009-12-04 09:38:48 -0800499
500 size_t charSize;
501 if (mHeader->flags&ResStringPool_header::UTF8_FLAG) {
502 charSize = sizeof(uint8_t);
Kenny Root19138462009-12-04 09:38:48 -0800503 } else {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800504 charSize = sizeof(uint16_t);
Kenny Root19138462009-12-04 09:38:48 -0800505 }
506
Adam Lesinskif28d5052014-07-25 15:25:04 -0700507 // There should be at least space for the smallest string
508 // (2 bytes length, null terminator).
509 if (mHeader->stringsStart >= (mSize - sizeof(uint16_t))) {
Steve Block8564c8d2012-01-05 23:22:43 +0000510 ALOGW("Bad string block: string pool starts at %d, after total size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 (int)mHeader->stringsStart, (int)mHeader->header.size);
512 return (mError=BAD_TYPE);
513 }
Adam Lesinskif28d5052014-07-25 15:25:04 -0700514
515 mStrings = (const void*)
516 (((const uint8_t*)data) + mHeader->stringsStart);
517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 if (mHeader->styleCount == 0) {
Adam Lesinskif28d5052014-07-25 15:25:04 -0700519 mStringPoolSize = (mSize - mHeader->stringsStart) / charSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 } else {
Kenny Root5e4d9a02010-06-08 12:34:43 -0700521 // check invariant: styles starts before end of data
Adam Lesinskif28d5052014-07-25 15:25:04 -0700522 if (mHeader->stylesStart >= (mSize - sizeof(uint16_t))) {
Steve Block8564c8d2012-01-05 23:22:43 +0000523 ALOGW("Bad style block: style block starts at %d past data size of %d\n",
Kenny Root5e4d9a02010-06-08 12:34:43 -0700524 (int)mHeader->stylesStart, (int)mHeader->header.size);
525 return (mError=BAD_TYPE);
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 // check invariant: styles follow the strings
528 if (mHeader->stylesStart <= mHeader->stringsStart) {
Steve Block8564c8d2012-01-05 23:22:43 +0000529 ALOGW("Bad style block: style block starts at %d, before strings at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 (int)mHeader->stylesStart, (int)mHeader->stringsStart);
531 return (mError=BAD_TYPE);
532 }
533 mStringPoolSize =
Kenny Root19138462009-12-04 09:38:48 -0800534 (mHeader->stylesStart-mHeader->stringsStart)/charSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536
537 // check invariant: stringCount > 0 requires a string pool to exist
538 if (mStringPoolSize == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000539 ALOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 return (mError=BAD_TYPE);
541 }
542
543 if (notDeviceEndian) {
544 size_t i;
545 uint32_t* e = const_cast<uint32_t*>(mEntries);
546 for (i=0; i<mHeader->stringCount; i++) {
547 e[i] = dtohl(mEntries[i]);
548 }
Kenny Root19138462009-12-04 09:38:48 -0800549 if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800550 const uint16_t* strings = (const uint16_t*)mStrings;
551 uint16_t* s = const_cast<uint16_t*>(strings);
Kenny Root19138462009-12-04 09:38:48 -0800552 for (i=0; i<mStringPoolSize; i++) {
553 s[i] = dtohs(strings[i]);
554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557
Kenny Root19138462009-12-04 09:38:48 -0800558 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG &&
559 ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) ||
560 (!mHeader->flags&ResStringPool_header::UTF8_FLAG &&
Adam Lesinski4bf58102014-11-03 11:21:19 -0800561 ((uint16_t*)mStrings)[mStringPoolSize-1] != 0)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000562 ALOGW("Bad string block: last string is not 0-terminated\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 return (mError=BAD_TYPE);
564 }
565 } else {
566 mStrings = NULL;
567 mStringPoolSize = 0;
568 }
569
570 if (mHeader->styleCount > 0) {
571 mEntryStyles = mEntries + mHeader->stringCount;
572 // invariant: integer overflow in calculating mEntryStyles
573 if (mEntryStyles < mEntries) {
Steve Block8564c8d2012-01-05 23:22:43 +0000574 ALOGW("Bad string block: integer overflow finding styles\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 return (mError=BAD_TYPE);
576 }
577
578 if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000579 ALOGW("Bad string block: entry of %d styles extends past data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader),
581 (int)size);
582 return (mError=BAD_TYPE);
583 }
584 mStyles = (const uint32_t*)
585 (((const uint8_t*)data)+mHeader->stylesStart);
586 if (mHeader->stylesStart >= mHeader->header.size) {
Steve Block8564c8d2012-01-05 23:22:43 +0000587 ALOGW("Bad string block: style pool starts %d, after total size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 (int)mHeader->stylesStart, (int)mHeader->header.size);
589 return (mError=BAD_TYPE);
590 }
591 mStylePoolSize =
592 (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t);
593
594 if (notDeviceEndian) {
595 size_t i;
596 uint32_t* e = const_cast<uint32_t*>(mEntryStyles);
597 for (i=0; i<mHeader->styleCount; i++) {
598 e[i] = dtohl(mEntryStyles[i]);
599 }
600 uint32_t* s = const_cast<uint32_t*>(mStyles);
601 for (i=0; i<mStylePoolSize; i++) {
602 s[i] = dtohl(mStyles[i]);
603 }
604 }
605
606 const ResStringPool_span endSpan = {
607 { htodl(ResStringPool_span::END) },
608 htodl(ResStringPool_span::END), htodl(ResStringPool_span::END)
609 };
610 if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))],
611 &endSpan, sizeof(endSpan)) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000612 ALOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 return (mError=BAD_TYPE);
614 }
615 } else {
616 mEntryStyles = NULL;
617 mStyles = NULL;
618 mStylePoolSize = 0;
619 }
620
621 return (mError=NO_ERROR);
622}
623
624status_t ResStringPool::getError() const
625{
626 return mError;
627}
628
629void ResStringPool::uninit()
630{
631 mError = NO_INIT;
Kenny Root19138462009-12-04 09:38:48 -0800632 if (mHeader != NULL && mCache != NULL) {
633 for (size_t x = 0; x < mHeader->stringCount; x++) {
634 if (mCache[x] != NULL) {
635 free(mCache[x]);
636 mCache[x] = NULL;
637 }
638 }
639 free(mCache);
640 mCache = NULL;
641 }
Chris Dearmana1d82ff32012-10-08 12:22:02 -0700642 if (mOwnedData) {
643 free(mOwnedData);
644 mOwnedData = NULL;
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646}
647
Kenny Root300ba682010-11-09 14:37:23 -0800648/**
649 * Strings in UTF-16 format have length indicated by a length encoded in the
650 * stored data. It is either 1 or 2 characters of length data. This allows a
651 * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that
652 * much data in a string, you're abusing them.
653 *
654 * If the high bit is set, then there are two characters or 4 bytes of length
655 * data encoded. In that case, drop the high bit of the first character and
656 * add it together with the next character.
657 */
658static inline size_t
Adam Lesinski4bf58102014-11-03 11:21:19 -0800659decodeLength(const uint16_t** str)
Kenny Root300ba682010-11-09 14:37:23 -0800660{
661 size_t len = **str;
662 if ((len & 0x8000) != 0) {
663 (*str)++;
664 len = ((len & 0x7FFF) << 16) | **str;
665 }
666 (*str)++;
667 return len;
668}
Kenny Root19138462009-12-04 09:38:48 -0800669
Kenny Root300ba682010-11-09 14:37:23 -0800670/**
671 * Strings in UTF-8 format have length indicated by a length encoded in the
672 * stored data. It is either 1 or 2 characters of length data. This allows a
673 * maximum length of 0x7FFF (32767 bytes), but you should consider storing
674 * text in another way if you're using that much data in a single string.
675 *
676 * If the high bit is set, then there are two characters or 2 bytes of length
677 * data encoded. In that case, drop the high bit of the first character and
678 * add it together with the next character.
679 */
680static inline size_t
681decodeLength(const uint8_t** str)
682{
683 size_t len = **str;
684 if ((len & 0x80) != 0) {
685 (*str)++;
686 len = ((len & 0x7F) << 8) | **str;
687 }
688 (*str)++;
689 return len;
690}
691
Adam Lesinski4bf58102014-11-03 11:21:19 -0800692const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693{
694 if (mError == NO_ERROR && idx < mHeader->stringCount) {
Kenny Root19138462009-12-04 09:38:48 -0800695 const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800696 const uint32_t off = mEntries[idx]/(isUTF8?sizeof(uint8_t):sizeof(uint16_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 if (off < (mStringPoolSize-1)) {
Kenny Root19138462009-12-04 09:38:48 -0800698 if (!isUTF8) {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800699 const uint16_t* strings = (uint16_t*)mStrings;
700 const uint16_t* str = strings+off;
Kenny Root300ba682010-11-09 14:37:23 -0800701
702 *u16len = decodeLength(&str);
703 if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) {
Adam Lesinski4bf58102014-11-03 11:21:19 -0800704 return reinterpret_cast<const char16_t*>(str);
Kenny Root19138462009-12-04 09:38:48 -0800705 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000706 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
Kenny Root300ba682010-11-09 14:37:23 -0800707 (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize);
Kenny Root19138462009-12-04 09:38:48 -0800708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 } else {
Kenny Root19138462009-12-04 09:38:48 -0800710 const uint8_t* strings = (uint8_t*)mStrings;
Kenny Root300ba682010-11-09 14:37:23 -0800711 const uint8_t* u8str = strings+off;
712
713 *u16len = decodeLength(&u8str);
714 size_t u8len = decodeLength(&u8str);
715
716 // encLen must be less than 0x7FFF due to encoding.
717 if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) {
Kenny Root19138462009-12-04 09:38:48 -0800718 AutoMutex lock(mDecodeLock);
Kenny Root300ba682010-11-09 14:37:23 -0800719
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700720 if (mCache == NULL) {
721#ifndef HAVE_ANDROID_OS
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700722 if (kDebugStringPoolNoisy) {
723 ALOGI("CREATING STRING CACHE OF %zu bytes",
724 mHeader->stringCount*sizeof(char16_t**));
725 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700726#else
727 // We do not want to be in this case when actually running Android.
Andreas Gampe25df5fb2014-11-07 22:24:57 -0800728 ALOGW("CREATING STRING CACHE OF %zu bytes",
729 static_cast<size_t>(mHeader->stringCount*sizeof(char16_t**)));
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700730#endif
731 mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t**));
732 if (mCache == NULL) {
733 ALOGW("No memory trying to allocate decode cache table of %d bytes\n",
734 (int)(mHeader->stringCount*sizeof(char16_t**)));
735 return NULL;
736 }
737 }
738
Kenny Root19138462009-12-04 09:38:48 -0800739 if (mCache[idx] != NULL) {
740 return mCache[idx];
741 }
Kenny Root300ba682010-11-09 14:37:23 -0800742
743 ssize_t actualLen = utf8_to_utf16_length(u8str, u8len);
744 if (actualLen < 0 || (size_t)actualLen != *u16len) {
Steve Block8564c8d2012-01-05 23:22:43 +0000745 ALOGW("Bad string block: string #%lld decoded length is not correct "
Kenny Root300ba682010-11-09 14:37:23 -0800746 "%lld vs %llu\n",
747 (long long)idx, (long long)actualLen, (long long)*u16len);
748 return NULL;
749 }
750
751 char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t));
Kenny Root19138462009-12-04 09:38:48 -0800752 if (!u16str) {
Steve Block8564c8d2012-01-05 23:22:43 +0000753 ALOGW("No memory when trying to allocate decode cache for string #%d\n",
Kenny Root19138462009-12-04 09:38:48 -0800754 (int)idx);
755 return NULL;
756 }
Kenny Root300ba682010-11-09 14:37:23 -0800757
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700758 if (kDebugStringPoolNoisy) {
759 ALOGI("Caching UTF8 string: %s", u8str);
760 }
Kenny Root300ba682010-11-09 14:37:23 -0800761 utf8_to_utf16(u8str, u8len, u16str);
Kenny Root19138462009-12-04 09:38:48 -0800762 mCache[idx] = u16str;
763 return u16str;
764 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000765 ALOGW("Bad string block: string #%lld extends to %lld, past end at %lld\n",
Kenny Root300ba682010-11-09 14:37:23 -0800766 (long long)idx, (long long)(u8str+u8len-strings),
767 (long long)mStringPoolSize);
Kenny Root19138462009-12-04 09:38:48 -0800768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000771 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 (int)idx, (int)(off*sizeof(uint16_t)),
773 (int)(mStringPoolSize*sizeof(uint16_t)));
774 }
775 }
776 return NULL;
777}
778
Kenny Root780d2a12010-02-22 22:36:26 -0800779const char* ResStringPool::string8At(size_t idx, size_t* outLen) const
780{
781 if (mError == NO_ERROR && idx < mHeader->stringCount) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700782 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) == 0) {
783 return NULL;
784 }
785 const uint32_t off = mEntries[idx]/sizeof(char);
Kenny Root780d2a12010-02-22 22:36:26 -0800786 if (off < (mStringPoolSize-1)) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700787 const uint8_t* strings = (uint8_t*)mStrings;
788 const uint8_t* str = strings+off;
789 *outLen = decodeLength(&str);
790 size_t encLen = decodeLength(&str);
791 if ((uint32_t)(str+encLen-strings) < mStringPoolSize) {
792 return (const char*)str;
793 } else {
794 ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
795 (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize);
Kenny Root780d2a12010-02-22 22:36:26 -0800796 }
797 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000798 ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
Kenny Root780d2a12010-02-22 22:36:26 -0800799 (int)idx, (int)(off*sizeof(uint16_t)),
800 (int)(mStringPoolSize*sizeof(uint16_t)));
801 }
802 }
803 return NULL;
804}
805
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800806const String8 ResStringPool::string8ObjectAt(size_t idx) const
807{
808 size_t len;
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700809 const char *str = string8At(idx, &len);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800810 if (str != NULL) {
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700811 return String8(str, len);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800812 }
Adam Lesinski4b2d0f22014-08-14 17:58:37 -0700813
814 const char16_t *str16 = stringAt(idx, &len);
815 if (str16 != NULL) {
816 return String8(str16, len);
817 }
818 return String8();
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800819}
820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const
822{
823 return styleAt(ref.index);
824}
825
826const ResStringPool_span* ResStringPool::styleAt(size_t idx) const
827{
828 if (mError == NO_ERROR && idx < mHeader->styleCount) {
829 const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t));
830 if (off < mStylePoolSize) {
831 return (const ResStringPool_span*)(mStyles+off);
832 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000833 ALOGW("Bad string block: style #%d entry is at %d, past end at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 (int)idx, (int)(off*sizeof(uint32_t)),
835 (int)(mStylePoolSize*sizeof(uint32_t)));
836 }
837 }
838 return NULL;
839}
840
841ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
842{
843 if (mError != NO_ERROR) {
844 return mError;
845 }
846
847 size_t len;
848
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700849 if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700850 if (kDebugStringPoolNoisy) {
851 ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string());
852 }
Kenny Root19138462009-12-04 09:38:48 -0800853
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700854 // The string pool contains UTF 8 strings; we don't want to cause
855 // temporary UTF-16 strings to be created as we search.
856 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
857 // Do a binary search for the string... this is a little tricky,
858 // because the strings are sorted with strzcmp16(). So to match
859 // the ordering, we need to convert strings in the pool to UTF-16.
860 // But we don't want to hit the cache, so instead we will have a
861 // local temporary allocation for the conversions.
862 char16_t* convBuffer = (char16_t*)malloc(strLen+4);
863 ssize_t l = 0;
864 ssize_t h = mHeader->stringCount-1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700866 ssize_t mid;
867 while (l <= h) {
868 mid = l + (h - l)/2;
869 const uint8_t* s = (const uint8_t*)string8At(mid, &len);
870 int c;
871 if (s != NULL) {
872 char16_t* end = utf8_to_utf16_n(s, len, convBuffer, strLen+3);
873 *end = 0;
874 c = strzcmp16(convBuffer, end-convBuffer, str, strLen);
875 } else {
876 c = -1;
877 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700878 if (kDebugStringPoolNoisy) {
879 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
880 (const char*)s, c, (int)l, (int)mid, (int)h);
881 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700882 if (c == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700883 if (kDebugStringPoolNoisy) {
884 ALOGI("MATCH!");
885 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700886 free(convBuffer);
887 return mid;
888 } else if (c < 0) {
889 l = mid + 1;
890 } else {
891 h = mid - 1;
892 }
893 }
894 free(convBuffer);
895 } else {
896 // It is unusual to get the ID from an unsorted string block...
897 // most often this happens because we want to get IDs for style
898 // span tags; since those always appear at the end of the string
899 // block, start searching at the back.
900 String8 str8(str, strLen);
901 const size_t str8Len = str8.size();
902 for (int i=mHeader->stringCount-1; i>=0; i--) {
903 const char* s = string8At(i, &len);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700904 if (kDebugStringPoolNoisy) {
905 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
906 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700907 if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700908 if (kDebugStringPoolNoisy) {
909 ALOGI("MATCH!");
910 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700911 return i;
912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700917 if (kDebugStringPoolNoisy) {
918 ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string());
919 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700920
921 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
922 // Do a binary search for the string...
923 ssize_t l = 0;
924 ssize_t h = mHeader->stringCount-1;
925
926 ssize_t mid;
927 while (l <= h) {
928 mid = l + (h - l)/2;
929 const char16_t* s = stringAt(mid, &len);
930 int c = s ? strzcmp16(s, len, str, strLen) : -1;
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700931 if (kDebugStringPoolNoisy) {
932 ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
933 String8(s).string(), c, (int)l, (int)mid, (int)h);
934 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700935 if (c == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700936 if (kDebugStringPoolNoisy) {
937 ALOGI("MATCH!");
938 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700939 return mid;
940 } else if (c < 0) {
941 l = mid + 1;
942 } else {
943 h = mid - 1;
944 }
945 }
946 } else {
947 // It is unusual to get the ID from an unsorted string block...
948 // most often this happens because we want to get IDs for style
949 // span tags; since those always appear at the end of the string
950 // block, start searching at the back.
951 for (int i=mHeader->stringCount-1; i>=0; i--) {
952 const char16_t* s = stringAt(i, &len);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700953 if (kDebugStringPoolNoisy) {
954 ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
955 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700956 if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700957 if (kDebugStringPoolNoisy) {
958 ALOGI("MATCH!");
959 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700960 return i;
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 }
964 }
965
966 return NAME_NOT_FOUND;
967}
968
969size_t ResStringPool::size() const
970{
971 return (mError == NO_ERROR) ? mHeader->stringCount : 0;
972}
973
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800974size_t ResStringPool::styleCount() const
975{
976 return (mError == NO_ERROR) ? mHeader->styleCount : 0;
977}
978
979size_t ResStringPool::bytes() const
980{
981 return (mError == NO_ERROR) ? mHeader->header.size : 0;
982}
983
984bool ResStringPool::isSorted() const
985{
986 return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0;
987}
988
Kenny Rootbb79f642009-12-10 14:20:15 -0800989bool ResStringPool::isUTF8() const
990{
991 return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0;
992}
Kenny Rootbb79f642009-12-10 14:20:15 -0800993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994// --------------------------------------------------------------------
995// --------------------------------------------------------------------
996// --------------------------------------------------------------------
997
998ResXMLParser::ResXMLParser(const ResXMLTree& tree)
999 : mTree(tree), mEventCode(BAD_DOCUMENT)
1000{
1001}
1002
1003void ResXMLParser::restart()
1004{
1005 mCurNode = NULL;
1006 mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT;
1007}
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001008const ResStringPool& ResXMLParser::getStrings() const
1009{
1010 return mTree.mStrings;
1011}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
1013ResXMLParser::event_code_t ResXMLParser::getEventType() const
1014{
1015 return mEventCode;
1016}
1017
1018ResXMLParser::event_code_t ResXMLParser::next()
1019{
1020 if (mEventCode == START_DOCUMENT) {
1021 mCurNode = mTree.mRootNode;
1022 mCurExt = mTree.mRootExt;
1023 return (mEventCode=mTree.mRootCode);
1024 } else if (mEventCode >= FIRST_CHUNK_CODE) {
1025 return nextNode();
1026 }
1027 return mEventCode;
1028}
1029
Mathias Agopian5f910972009-06-22 02:35:32 -07001030int32_t ResXMLParser::getCommentID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031{
1032 return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1;
1033}
1034
Adam Lesinski4bf58102014-11-03 11:21:19 -08001035const char16_t* ResXMLParser::getComment(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036{
1037 int32_t id = getCommentID();
1038 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1039}
1040
Mathias Agopian5f910972009-06-22 02:35:32 -07001041uint32_t ResXMLParser::getLineNumber() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042{
1043 return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1;
1044}
1045
Mathias Agopian5f910972009-06-22 02:35:32 -07001046int32_t ResXMLParser::getTextID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047{
1048 if (mEventCode == TEXT) {
1049 return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index);
1050 }
1051 return -1;
1052}
1053
Adam Lesinski4bf58102014-11-03 11:21:19 -08001054const char16_t* ResXMLParser::getText(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055{
1056 int32_t id = getTextID();
1057 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1058}
1059
1060ssize_t ResXMLParser::getTextValue(Res_value* outValue) const
1061{
1062 if (mEventCode == TEXT) {
1063 outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData);
1064 return sizeof(Res_value);
1065 }
1066 return BAD_TYPE;
1067}
1068
Mathias Agopian5f910972009-06-22 02:35:32 -07001069int32_t ResXMLParser::getNamespacePrefixID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070{
1071 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1072 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index);
1073 }
1074 return -1;
1075}
1076
Adam Lesinski4bf58102014-11-03 11:21:19 -08001077const char16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078{
1079 int32_t id = getNamespacePrefixID();
1080 //printf("prefix=%d event=%p\n", id, mEventCode);
1081 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1082}
1083
Mathias Agopian5f910972009-06-22 02:35:32 -07001084int32_t ResXMLParser::getNamespaceUriID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085{
1086 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
1087 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index);
1088 }
1089 return -1;
1090}
1091
Adam Lesinski4bf58102014-11-03 11:21:19 -08001092const char16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093{
1094 int32_t id = getNamespaceUriID();
1095 //printf("uri=%d event=%p\n", id, mEventCode);
1096 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1097}
1098
Mathias Agopian5f910972009-06-22 02:35:32 -07001099int32_t ResXMLParser::getElementNamespaceID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100{
1101 if (mEventCode == START_TAG) {
1102 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index);
1103 }
1104 if (mEventCode == END_TAG) {
1105 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index);
1106 }
1107 return -1;
1108}
1109
Adam Lesinski4bf58102014-11-03 11:21:19 -08001110const char16_t* ResXMLParser::getElementNamespace(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111{
1112 int32_t id = getElementNamespaceID();
1113 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1114}
1115
Mathias Agopian5f910972009-06-22 02:35:32 -07001116int32_t ResXMLParser::getElementNameID() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117{
1118 if (mEventCode == START_TAG) {
1119 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index);
1120 }
1121 if (mEventCode == END_TAG) {
1122 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index);
1123 }
1124 return -1;
1125}
1126
Adam Lesinski4bf58102014-11-03 11:21:19 -08001127const char16_t* ResXMLParser::getElementName(size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128{
1129 int32_t id = getElementNameID();
1130 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1131}
1132
1133size_t ResXMLParser::getAttributeCount() const
1134{
1135 if (mEventCode == START_TAG) {
1136 return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount);
1137 }
1138 return 0;
1139}
1140
Mathias Agopian5f910972009-06-22 02:35:32 -07001141int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142{
1143 if (mEventCode == START_TAG) {
1144 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1145 if (idx < dtohs(tag->attributeCount)) {
1146 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1147 (((const uint8_t*)tag)
1148 + dtohs(tag->attributeStart)
1149 + (dtohs(tag->attributeSize)*idx));
1150 return dtohl(attr->ns.index);
1151 }
1152 }
1153 return -2;
1154}
1155
Adam Lesinski4bf58102014-11-03 11:21:19 -08001156const char16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157{
1158 int32_t id = getAttributeNamespaceID(idx);
1159 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001160 if (kDebugXMLNoisy) {
1161 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1164}
1165
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001166const char* ResXMLParser::getAttributeNamespace8(size_t idx, size_t* outLen) const
1167{
1168 int32_t id = getAttributeNamespaceID(idx);
1169 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001170 if (kDebugXMLNoisy) {
1171 printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
1172 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001173 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1174}
1175
Mathias Agopian5f910972009-06-22 02:35:32 -07001176int32_t ResXMLParser::getAttributeNameID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177{
1178 if (mEventCode == START_TAG) {
1179 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1180 if (idx < dtohs(tag->attributeCount)) {
1181 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1182 (((const uint8_t*)tag)
1183 + dtohs(tag->attributeStart)
1184 + (dtohs(tag->attributeSize)*idx));
1185 return dtohl(attr->name.index);
1186 }
1187 }
1188 return -1;
1189}
1190
Adam Lesinski4bf58102014-11-03 11:21:19 -08001191const char16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192{
1193 int32_t id = getAttributeNameID(idx);
1194 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001195 if (kDebugXMLNoisy) {
1196 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1199}
1200
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001201const char* ResXMLParser::getAttributeName8(size_t idx, size_t* outLen) const
1202{
1203 int32_t id = getAttributeNameID(idx);
1204 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001205 if (kDebugXMLNoisy) {
1206 printf("getAttributeName 0x%zx=0x%x\n", idx, id);
1207 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001208 return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
1209}
1210
Mathias Agopian5f910972009-06-22 02:35:32 -07001211uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212{
1213 int32_t id = getAttributeNameID(idx);
1214 if (id >= 0 && (size_t)id < mTree.mNumResIds) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07001215 uint32_t resId = dtohl(mTree.mResIds[id]);
1216 if (mTree.mDynamicRefTable != NULL) {
1217 mTree.mDynamicRefTable->lookupResourceId(&resId);
1218 }
1219 return resId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 return 0;
1222}
1223
Mathias Agopian5f910972009-06-22 02:35:32 -07001224int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225{
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->rawValue.index);
1234 }
1235 }
1236 return -1;
1237}
1238
Adam Lesinski4bf58102014-11-03 11:21:19 -08001239const char16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240{
1241 int32_t id = getAttributeValueStringID(idx);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001242 if (kDebugXMLNoisy) {
1243 printf("getAttributeValue 0x%zx=0x%x\n", idx, id);
1244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
1246}
1247
1248int32_t ResXMLParser::getAttributeDataType(size_t idx) const
1249{
1250 if (mEventCode == START_TAG) {
1251 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1252 if (idx < dtohs(tag->attributeCount)) {
1253 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1254 (((const uint8_t*)tag)
1255 + dtohs(tag->attributeStart)
1256 + (dtohs(tag->attributeSize)*idx));
Adam Lesinskide898ff2014-01-29 18:20:45 -08001257 uint8_t type = attr->typedValue.dataType;
1258 if (type != Res_value::TYPE_DYNAMIC_REFERENCE) {
1259 return type;
1260 }
1261
1262 // This is a dynamic reference. We adjust those references
1263 // to regular references at this level, so lie to the caller.
1264 return Res_value::TYPE_REFERENCE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
1266 }
1267 return Res_value::TYPE_NULL;
1268}
1269
1270int32_t ResXMLParser::getAttributeData(size_t idx) const
1271{
1272 if (mEventCode == START_TAG) {
1273 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1274 if (idx < dtohs(tag->attributeCount)) {
1275 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1276 (((const uint8_t*)tag)
1277 + dtohs(tag->attributeStart)
1278 + (dtohs(tag->attributeSize)*idx));
Adam Lesinskide898ff2014-01-29 18:20:45 -08001279 if (attr->typedValue.dataType != Res_value::TYPE_DYNAMIC_REFERENCE ||
1280 mTree.mDynamicRefTable == NULL) {
1281 return dtohl(attr->typedValue.data);
1282 }
1283
1284 uint32_t data = dtohl(attr->typedValue.data);
1285 if (mTree.mDynamicRefTable->lookupResourceId(&data) == NO_ERROR) {
1286 return data;
1287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 }
1290 return 0;
1291}
1292
1293ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const
1294{
1295 if (mEventCode == START_TAG) {
1296 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
1297 if (idx < dtohs(tag->attributeCount)) {
1298 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
1299 (((const uint8_t*)tag)
1300 + dtohs(tag->attributeStart)
1301 + (dtohs(tag->attributeSize)*idx));
1302 outValue->copyFrom_dtoh(attr->typedValue);
Adam Lesinskide898ff2014-01-29 18:20:45 -08001303 if (mTree.mDynamicRefTable != NULL &&
1304 mTree.mDynamicRefTable->lookupResourceValue(outValue) != NO_ERROR) {
1305 return BAD_TYPE;
1306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 return sizeof(Res_value);
1308 }
1309 }
1310 return BAD_TYPE;
1311}
1312
1313ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const
1314{
1315 String16 nsStr(ns != NULL ? ns : "");
1316 String16 attrStr(attr);
1317 return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0,
1318 attrStr.string(), attrStr.size());
1319}
1320
1321ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen,
1322 const char16_t* attr, size_t attrLen) const
1323{
1324 if (mEventCode == START_TAG) {
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001325 if (attr == NULL) {
1326 return NAME_NOT_FOUND;
1327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 const size_t N = getAttributeCount();
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001329 if (mTree.mStrings.isUTF8()) {
1330 String8 ns8, attr8;
1331 if (ns != NULL) {
1332 ns8 = String8(ns, nsLen);
1333 }
1334 attr8 = String8(attr, attrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001335 if (kDebugStringPoolNoisy) {
1336 ALOGI("indexOfAttribute UTF8 %s (%zu) / %s (%zu)", ns8.string(), nsLen,
1337 attr8.string(), attrLen);
1338 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001339 for (size_t i=0; i<N; i++) {
1340 size_t curNsLen = 0, curAttrLen = 0;
1341 const char* curNs = getAttributeNamespace8(i, &curNsLen);
1342 const char* curAttr = getAttributeName8(i, &curAttrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001343 if (kDebugStringPoolNoisy) {
1344 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)", curNs, curNsLen, curAttr, curAttrLen);
1345 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001346 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1347 && memcmp(attr8.string(), curAttr, attrLen) == 0) {
1348 if (ns == NULL) {
1349 if (curNs == NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001350 if (kDebugStringPoolNoisy) {
1351 ALOGI(" FOUND!");
1352 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001353 return i;
1354 }
1355 } else if (curNs != NULL) {
1356 //printf(" --> ns=%s, curNs=%s\n",
1357 // String8(ns).string(), String8(curNs).string());
1358 if (memcmp(ns8.string(), curNs, nsLen) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001359 if (kDebugStringPoolNoisy) {
1360 ALOGI(" FOUND!");
1361 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001362 return i;
1363 }
1364 }
1365 }
1366 }
1367 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001368 if (kDebugStringPoolNoisy) {
1369 ALOGI("indexOfAttribute UTF16 %s (%zu) / %s (%zu)",
1370 String8(ns, nsLen).string(), nsLen,
1371 String8(attr, attrLen).string(), attrLen);
1372 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001373 for (size_t i=0; i<N; i++) {
1374 size_t curNsLen = 0, curAttrLen = 0;
1375 const char16_t* curNs = getAttributeNamespace(i, &curNsLen);
1376 const char16_t* curAttr = getAttributeName(i, &curAttrLen);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001377 if (kDebugStringPoolNoisy) {
1378 ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)",
1379 String8(curNs, curNsLen).string(), curNsLen,
1380 String8(curAttr, curAttrLen).string(), curAttrLen);
1381 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001382 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
1383 && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) {
1384 if (ns == NULL) {
1385 if (curNs == NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001386 if (kDebugStringPoolNoisy) {
1387 ALOGI(" FOUND!");
1388 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001389 return i;
1390 }
1391 } else if (curNs != NULL) {
1392 //printf(" --> ns=%s, curNs=%s\n",
1393 // String8(ns).string(), String8(curNs).string());
1394 if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001395 if (kDebugStringPoolNoisy) {
1396 ALOGI(" FOUND!");
1397 }
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001398 return i;
1399 }
1400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 }
1403 }
1404 }
1405
1406 return NAME_NOT_FOUND;
1407}
1408
1409ssize_t ResXMLParser::indexOfID() const
1410{
1411 if (mEventCode == START_TAG) {
1412 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex);
1413 if (idx > 0) return (idx-1);
1414 }
1415 return NAME_NOT_FOUND;
1416}
1417
1418ssize_t ResXMLParser::indexOfClass() const
1419{
1420 if (mEventCode == START_TAG) {
1421 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex);
1422 if (idx > 0) return (idx-1);
1423 }
1424 return NAME_NOT_FOUND;
1425}
1426
1427ssize_t ResXMLParser::indexOfStyle() const
1428{
1429 if (mEventCode == START_TAG) {
1430 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex);
1431 if (idx > 0) return (idx-1);
1432 }
1433 return NAME_NOT_FOUND;
1434}
1435
1436ResXMLParser::event_code_t ResXMLParser::nextNode()
1437{
1438 if (mEventCode < 0) {
1439 return mEventCode;
1440 }
1441
1442 do {
1443 const ResXMLTree_node* next = (const ResXMLTree_node*)
1444 (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001445 if (kDebugXMLNoisy) {
1446 ALOGI("Next node: prev=%p, next=%p\n", mCurNode, next);
1447 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (((const uint8_t*)next) >= mTree.mDataEnd) {
1450 mCurNode = NULL;
1451 return (mEventCode=END_DOCUMENT);
1452 }
1453
1454 if (mTree.validateNode(next) != NO_ERROR) {
1455 mCurNode = NULL;
1456 return (mEventCode=BAD_DOCUMENT);
1457 }
1458
1459 mCurNode = next;
1460 const uint16_t headerSize = dtohs(next->header.headerSize);
1461 const uint32_t totalSize = dtohl(next->header.size);
1462 mCurExt = ((const uint8_t*)next) + headerSize;
1463 size_t minExtSize = 0;
1464 event_code_t eventCode = (event_code_t)dtohs(next->header.type);
1465 switch ((mEventCode=eventCode)) {
1466 case RES_XML_START_NAMESPACE_TYPE:
1467 case RES_XML_END_NAMESPACE_TYPE:
1468 minExtSize = sizeof(ResXMLTree_namespaceExt);
1469 break;
1470 case RES_XML_START_ELEMENT_TYPE:
1471 minExtSize = sizeof(ResXMLTree_attrExt);
1472 break;
1473 case RES_XML_END_ELEMENT_TYPE:
1474 minExtSize = sizeof(ResXMLTree_endElementExt);
1475 break;
1476 case RES_XML_CDATA_TYPE:
1477 minExtSize = sizeof(ResXMLTree_cdataExt);
1478 break;
1479 default:
Steve Block8564c8d2012-01-05 23:22:43 +00001480 ALOGW("Unknown XML block: header type %d in node at %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 (int)dtohs(next->header.type),
1482 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)));
1483 continue;
1484 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if ((totalSize-headerSize) < minExtSize) {
Steve Block8564c8d2012-01-05 23:22:43 +00001487 ALOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 (int)dtohs(next->header.type),
1489 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)),
1490 (int)(totalSize-headerSize), (int)minExtSize);
1491 return (mEventCode=BAD_DOCUMENT);
1492 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07001493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n",
1495 // mCurNode, mCurExt, headerSize, minExtSize);
Mark Salyzyn00adb862014-03-19 11:00:06 -07001496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 return eventCode;
1498 } while (true);
1499}
1500
1501void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const
1502{
1503 pos->eventCode = mEventCode;
1504 pos->curNode = mCurNode;
1505 pos->curExt = mCurExt;
1506}
1507
1508void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos)
1509{
1510 mEventCode = pos.eventCode;
1511 mCurNode = pos.curNode;
1512 mCurExt = pos.curExt;
1513}
1514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515// --------------------------------------------------------------------
1516
1517static volatile int32_t gCount = 0;
1518
Adam Lesinskide898ff2014-01-29 18:20:45 -08001519ResXMLTree::ResXMLTree(const DynamicRefTable* dynamicRefTable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 : ResXMLParser(*this)
Adam Lesinskide898ff2014-01-29 18:20:45 -08001521 , mDynamicRefTable(dynamicRefTable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 , mError(NO_INIT), mOwnedData(NULL)
1523{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001524 if (kDebugResXMLTree) {
1525 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 restart();
1528}
1529
Adam Lesinskide898ff2014-01-29 18:20:45 -08001530ResXMLTree::ResXMLTree()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 : ResXMLParser(*this)
Adam Lesinskide898ff2014-01-29 18:20:45 -08001532 , mDynamicRefTable(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 , mError(NO_INIT), mOwnedData(NULL)
1534{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001535 if (kDebugResXMLTree) {
1536 ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
1537 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08001538 restart();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539}
1540
1541ResXMLTree::~ResXMLTree()
1542{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001543 if (kDebugResXMLTree) {
1544 ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 uninit();
1547}
1548
1549status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData)
1550{
1551 uninit();
1552 mEventCode = START_DOCUMENT;
1553
Kenny Root32d6aef2012-10-10 10:23:47 -07001554 if (!data || !size) {
1555 return (mError=BAD_TYPE);
1556 }
1557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (copyData) {
1559 mOwnedData = malloc(size);
1560 if (mOwnedData == NULL) {
1561 return (mError=NO_MEMORY);
1562 }
1563 memcpy(mOwnedData, data, size);
1564 data = mOwnedData;
1565 }
1566
1567 mHeader = (const ResXMLTree_header*)data;
1568 mSize = dtohl(mHeader->header.size);
1569 if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) {
Steve Block8564c8d2012-01-05 23:22:43 +00001570 ALOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 (int)dtohs(mHeader->header.headerSize),
1572 (int)dtohl(mHeader->header.size), (int)size);
1573 mError = BAD_TYPE;
1574 restart();
1575 return mError;
1576 }
1577 mDataEnd = ((const uint8_t*)mHeader) + mSize;
1578
1579 mStrings.uninit();
1580 mRootNode = NULL;
1581 mResIds = NULL;
1582 mNumResIds = 0;
1583
1584 // First look for a couple interesting chunks: the string block
1585 // and first XML node.
1586 const ResChunk_header* chunk =
1587 (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize));
1588 const ResChunk_header* lastChunk = chunk;
1589 while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) &&
1590 ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) {
1591 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML");
1592 if (err != NO_ERROR) {
1593 mError = err;
1594 goto done;
1595 }
1596 const uint16_t type = dtohs(chunk->type);
1597 const size_t size = dtohl(chunk->size);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001598 if (kDebugXMLNoisy) {
1599 printf("Scanning @ %p: type=0x%x, size=0x%zx\n",
1600 (void*)(((uintptr_t)chunk)-((uintptr_t)mHeader)), type, size);
1601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (type == RES_STRING_POOL_TYPE) {
1603 mStrings.setTo(chunk, size);
1604 } else if (type == RES_XML_RESOURCE_MAP_TYPE) {
1605 mResIds = (const uint32_t*)
1606 (((const uint8_t*)chunk)+dtohs(chunk->headerSize));
1607 mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t);
1608 } else if (type >= RES_XML_FIRST_CHUNK_TYPE
1609 && type <= RES_XML_LAST_CHUNK_TYPE) {
1610 if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) {
1611 mError = BAD_TYPE;
1612 goto done;
1613 }
1614 mCurNode = (const ResXMLTree_node*)lastChunk;
1615 if (nextNode() == BAD_DOCUMENT) {
1616 mError = BAD_TYPE;
1617 goto done;
1618 }
1619 mRootNode = mCurNode;
1620 mRootExt = mCurExt;
1621 mRootCode = mEventCode;
1622 break;
1623 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001624 if (kDebugXMLNoisy) {
1625 printf("Skipping unknown chunk!\n");
1626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 }
1628 lastChunk = chunk;
1629 chunk = (const ResChunk_header*)
1630 (((const uint8_t*)chunk) + size);
1631 }
1632
1633 if (mRootNode == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001634 ALOGW("Bad XML block: no root element node found\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 mError = BAD_TYPE;
1636 goto done;
1637 }
1638
1639 mError = mStrings.getError();
1640
1641done:
1642 restart();
1643 return mError;
1644}
1645
1646status_t ResXMLTree::getError() const
1647{
1648 return mError;
1649}
1650
1651void ResXMLTree::uninit()
1652{
1653 mError = NO_INIT;
Kenny Root19138462009-12-04 09:38:48 -08001654 mStrings.uninit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 if (mOwnedData) {
1656 free(mOwnedData);
1657 mOwnedData = NULL;
1658 }
1659 restart();
1660}
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const
1663{
1664 const uint16_t eventCode = dtohs(node->header.type);
1665
1666 status_t err = validate_chunk(
1667 &node->header, sizeof(ResXMLTree_node),
1668 mDataEnd, "ResXMLTree_node");
1669
1670 if (err >= NO_ERROR) {
1671 // Only perform additional validation on START nodes
1672 if (eventCode != RES_XML_START_ELEMENT_TYPE) {
1673 return NO_ERROR;
1674 }
1675
1676 const uint16_t headerSize = dtohs(node->header.headerSize);
1677 const uint32_t size = dtohl(node->header.size);
1678 const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*)
1679 (((const uint8_t*)node) + headerSize);
1680 // check for sensical values pulled out of the stream so far...
1681 if ((size >= headerSize + sizeof(ResXMLTree_attrExt))
1682 && ((void*)attrExt > (void*)node)) {
1683 const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize))
1684 * dtohs(attrExt->attributeCount);
1685 if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) {
1686 return NO_ERROR;
1687 }
Steve Block8564c8d2012-01-05 23:22:43 +00001688 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 (unsigned int)(dtohs(attrExt->attributeStart)+attrSize),
1690 (unsigned int)(size-headerSize));
1691 }
1692 else {
Steve Block8564c8d2012-01-05 23:22:43 +00001693 ALOGW("Bad XML start block: node header size 0x%x, size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 (unsigned int)headerSize, (unsigned int)size);
1695 }
1696 return BAD_TYPE;
1697 }
1698
1699 return err;
1700
1701#if 0
1702 const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE;
1703
1704 const uint16_t headerSize = dtohs(node->header.headerSize);
1705 const uint32_t size = dtohl(node->header.size);
1706
1707 if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) {
1708 if (size >= headerSize) {
1709 if (((const uint8_t*)node) <= (mDataEnd-size)) {
1710 if (!isStart) {
1711 return NO_ERROR;
1712 }
1713 if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount))
1714 <= (size-headerSize)) {
1715 return NO_ERROR;
1716 }
Steve Block8564c8d2012-01-05 23:22:43 +00001717 ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount),
1719 (int)(size-headerSize));
1720 return BAD_TYPE;
1721 }
Steve Block8564c8d2012-01-05 23:22:43 +00001722 ALOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize);
1724 return BAD_TYPE;
1725 }
Steve Block8564c8d2012-01-05 23:22:43 +00001726 ALOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1728 (int)headerSize, (int)size);
1729 return BAD_TYPE;
1730 }
Steve Block8564c8d2012-01-05 23:22:43 +00001731 ALOGW("Bad XML block: node at 0x%x header size 0x%x too small\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
1733 (int)headerSize);
1734 return BAD_TYPE;
1735#endif
1736}
1737
1738// --------------------------------------------------------------------
1739// --------------------------------------------------------------------
1740// --------------------------------------------------------------------
1741
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001742void ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) {
1743 const size_t size = dtohl(o.size);
1744 if (size >= sizeof(ResTable_config)) {
1745 *this = o;
1746 } else {
1747 memcpy(this, &o, size);
1748 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
1749 }
1750}
1751
Narayan Kamath48620f12014-01-20 13:57:11 +00001752/* static */ size_t unpackLanguageOrRegion(const char in[2], const char base,
1753 char out[4]) {
1754 if (in[0] & 0x80) {
1755 // The high bit is "1", which means this is a packed three letter
1756 // language code.
1757
1758 // The smallest 5 bits of the second char are the first alphabet.
1759 const uint8_t first = in[1] & 0x1f;
1760 // The last three bits of the second char and the first two bits
1761 // of the first char are the second alphabet.
1762 const uint8_t second = ((in[1] & 0xe0) >> 5) + ((in[0] & 0x03) << 3);
1763 // Bits 3 to 7 (inclusive) of the first char are the third alphabet.
1764 const uint8_t third = (in[0] & 0x7c) >> 2;
1765
1766 out[0] = first + base;
1767 out[1] = second + base;
1768 out[2] = third + base;
1769 out[3] = 0;
1770
1771 return 3;
1772 }
1773
1774 if (in[0]) {
1775 memcpy(out, in, 2);
1776 memset(out + 2, 0, 2);
1777 return 2;
1778 }
1779
1780 memset(out, 0, 4);
1781 return 0;
1782}
1783
Narayan Kamath788fa412014-01-21 15:32:36 +00001784/* static */ void packLanguageOrRegion(const char* in, const char base,
Narayan Kamath48620f12014-01-20 13:57:11 +00001785 char out[2]) {
Narayan Kamath788fa412014-01-21 15:32:36 +00001786 if (in[2] == 0 || in[2] == '-') {
Narayan Kamath48620f12014-01-20 13:57:11 +00001787 out[0] = in[0];
1788 out[1] = in[1];
1789 } else {
Narayan Kamathb2975912014-06-30 15:59:39 +01001790 uint8_t first = (in[0] - base) & 0x007f;
1791 uint8_t second = (in[1] - base) & 0x007f;
1792 uint8_t third = (in[2] - base) & 0x007f;
Narayan Kamath48620f12014-01-20 13:57:11 +00001793
1794 out[0] = (0x80 | (third << 2) | (second >> 3));
1795 out[1] = ((second << 5) | first);
1796 }
1797}
1798
1799
Narayan Kamath788fa412014-01-21 15:32:36 +00001800void ResTable_config::packLanguage(const char* language) {
Narayan Kamath48620f12014-01-20 13:57:11 +00001801 packLanguageOrRegion(language, 'a', this->language);
1802}
1803
Narayan Kamath788fa412014-01-21 15:32:36 +00001804void ResTable_config::packRegion(const char* region) {
Narayan Kamath48620f12014-01-20 13:57:11 +00001805 packLanguageOrRegion(region, '0', this->country);
1806}
1807
1808size_t ResTable_config::unpackLanguage(char language[4]) const {
1809 return unpackLanguageOrRegion(this->language, 'a', language);
1810}
1811
1812size_t ResTable_config::unpackRegion(char region[4]) const {
1813 return unpackLanguageOrRegion(this->country, '0', region);
1814}
1815
1816
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001817void ResTable_config::copyFromDtoH(const ResTable_config& o) {
1818 copyFromDeviceNoSwap(o);
1819 size = sizeof(ResTable_config);
1820 mcc = dtohs(mcc);
1821 mnc = dtohs(mnc);
1822 density = dtohs(density);
1823 screenWidth = dtohs(screenWidth);
1824 screenHeight = dtohs(screenHeight);
1825 sdkVersion = dtohs(sdkVersion);
1826 minorVersion = dtohs(minorVersion);
1827 smallestScreenWidthDp = dtohs(smallestScreenWidthDp);
1828 screenWidthDp = dtohs(screenWidthDp);
1829 screenHeightDp = dtohs(screenHeightDp);
1830}
1831
1832void ResTable_config::swapHtoD() {
1833 size = htodl(size);
1834 mcc = htods(mcc);
1835 mnc = htods(mnc);
1836 density = htods(density);
1837 screenWidth = htods(screenWidth);
1838 screenHeight = htods(screenHeight);
1839 sdkVersion = htods(sdkVersion);
1840 minorVersion = htods(minorVersion);
1841 smallestScreenWidthDp = htods(smallestScreenWidthDp);
1842 screenWidthDp = htods(screenWidthDp);
1843 screenHeightDp = htods(screenHeightDp);
1844}
1845
Narayan Kamath48620f12014-01-20 13:57:11 +00001846/* static */ inline int compareLocales(const ResTable_config &l, const ResTable_config &r) {
1847 if (l.locale != r.locale) {
1848 // NOTE: This is the old behaviour with respect to comparison orders.
1849 // The diff value here doesn't make much sense (given our bit packing scheme)
1850 // but it's stable, and that's all we need.
1851 return l.locale - r.locale;
1852 }
1853
1854 // The language & region are equal, so compare the scripts and variants.
1855 int script = memcmp(l.localeScript, r.localeScript, sizeof(l.localeScript));
1856 if (script) {
1857 return script;
1858 }
1859
1860 // The language, region and script are equal, so compare variants.
1861 //
1862 // This should happen very infrequently (if at all.)
1863 return memcmp(l.localeVariant, r.localeVariant, sizeof(l.localeVariant));
1864}
1865
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001866int ResTable_config::compare(const ResTable_config& o) const {
1867 int32_t diff = (int32_t)(imsi - o.imsi);
1868 if (diff != 0) return diff;
Narayan Kamath48620f12014-01-20 13:57:11 +00001869 diff = compareLocales(*this, o);
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001870 if (diff != 0) return diff;
1871 diff = (int32_t)(screenType - o.screenType);
1872 if (diff != 0) return diff;
1873 diff = (int32_t)(input - o.input);
1874 if (diff != 0) return diff;
1875 diff = (int32_t)(screenSize - o.screenSize);
1876 if (diff != 0) return diff;
1877 diff = (int32_t)(version - o.version);
1878 if (diff != 0) return diff;
1879 diff = (int32_t)(screenLayout - o.screenLayout);
1880 if (diff != 0) return diff;
1881 diff = (int32_t)(uiMode - o.uiMode);
1882 if (diff != 0) return diff;
1883 diff = (int32_t)(smallestScreenWidthDp - o.smallestScreenWidthDp);
1884 if (diff != 0) return diff;
1885 diff = (int32_t)(screenSizeDp - o.screenSizeDp);
1886 return (int)diff;
1887}
1888
1889int ResTable_config::compareLogical(const ResTable_config& o) const {
1890 if (mcc != o.mcc) {
1891 return mcc < o.mcc ? -1 : 1;
1892 }
1893 if (mnc != o.mnc) {
1894 return mnc < o.mnc ? -1 : 1;
1895 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001896
1897 int diff = compareLocales(*this, o);
1898 if (diff < 0) {
1899 return -1;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001900 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001901 if (diff > 0) {
1902 return 1;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001903 }
Narayan Kamath48620f12014-01-20 13:57:11 +00001904
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001905 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) {
1906 return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1;
1907 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001908 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
1909 return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1;
1910 }
1911 if (screenWidthDp != o.screenWidthDp) {
1912 return screenWidthDp < o.screenWidthDp ? -1 : 1;
1913 }
1914 if (screenHeightDp != o.screenHeightDp) {
1915 return screenHeightDp < o.screenHeightDp ? -1 : 1;
1916 }
1917 if (screenWidth != o.screenWidth) {
1918 return screenWidth < o.screenWidth ? -1 : 1;
1919 }
1920 if (screenHeight != o.screenHeight) {
1921 return screenHeight < o.screenHeight ? -1 : 1;
1922 }
1923 if (density != o.density) {
1924 return density < o.density ? -1 : 1;
1925 }
1926 if (orientation != o.orientation) {
1927 return orientation < o.orientation ? -1 : 1;
1928 }
1929 if (touchscreen != o.touchscreen) {
1930 return touchscreen < o.touchscreen ? -1 : 1;
1931 }
1932 if (input != o.input) {
1933 return input < o.input ? -1 : 1;
1934 }
1935 if (screenLayout != o.screenLayout) {
1936 return screenLayout < o.screenLayout ? -1 : 1;
1937 }
1938 if (uiMode != o.uiMode) {
1939 return uiMode < o.uiMode ? -1 : 1;
1940 }
1941 if (version != o.version) {
1942 return version < o.version ? -1 : 1;
1943 }
1944 return 0;
1945}
1946
1947int ResTable_config::diff(const ResTable_config& o) const {
1948 int diffs = 0;
1949 if (mcc != o.mcc) diffs |= CONFIG_MCC;
1950 if (mnc != o.mnc) diffs |= CONFIG_MNC;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001951 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
1952 if (density != o.density) diffs |= CONFIG_DENSITY;
1953 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
1954 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
1955 diffs |= CONFIG_KEYBOARD_HIDDEN;
1956 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
1957 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
1958 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
1959 if (version != o.version) diffs |= CONFIG_VERSION;
Fabrice Di Meglio35099352012-12-12 11:52:03 -08001960 if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) diffs |= CONFIG_LAYOUTDIR;
1961 if ((screenLayout & ~MASK_LAYOUTDIR) != (o.screenLayout & ~MASK_LAYOUTDIR)) diffs |= CONFIG_SCREEN_LAYOUT;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001962 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
1963 if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE;
1964 if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE;
Narayan Kamath48620f12014-01-20 13:57:11 +00001965
1966 const int diff = compareLocales(*this, o);
1967 if (diff) diffs |= CONFIG_LOCALE;
1968
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001969 return diffs;
1970}
1971
Narayan Kamath48620f12014-01-20 13:57:11 +00001972int ResTable_config::isLocaleMoreSpecificThan(const ResTable_config& o) const {
1973 if (locale || o.locale) {
1974 if (language[0] != o.language[0]) {
1975 if (!language[0]) return -1;
1976 if (!o.language[0]) return 1;
1977 }
1978
1979 if (country[0] != o.country[0]) {
1980 if (!country[0]) return -1;
1981 if (!o.country[0]) return 1;
1982 }
1983 }
1984
1985 // There isn't a well specified "importance" order between variants and
1986 // scripts. We can't easily tell whether, say "en-Latn-US" is more or less
1987 // specific than "en-US-POSIX".
1988 //
1989 // We therefore arbitrarily decide to give priority to variants over
1990 // scripts since it seems more useful to do so. We will consider
1991 // "en-US-POSIX" to be more specific than "en-Latn-US".
1992
1993 const int score = ((localeScript[0] != 0) ? 1 : 0) +
1994 ((localeVariant[0] != 0) ? 2 : 0);
1995
1996 const int oScore = ((o.localeScript[0] != 0) ? 1 : 0) +
1997 ((o.localeVariant[0] != 0) ? 2 : 0);
1998
1999 return score - oScore;
2000
2001}
2002
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002003bool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const {
2004 // The order of the following tests defines the importance of one
2005 // configuration parameter over another. Those tests first are more
2006 // important, trumping any values in those following them.
2007 if (imsi || o.imsi) {
2008 if (mcc != o.mcc) {
2009 if (!mcc) return false;
2010 if (!o.mcc) return true;
2011 }
2012
2013 if (mnc != o.mnc) {
2014 if (!mnc) return false;
2015 if (!o.mnc) return true;
2016 }
2017 }
2018
2019 if (locale || o.locale) {
Narayan Kamath48620f12014-01-20 13:57:11 +00002020 const int diff = isLocaleMoreSpecificThan(o);
2021 if (diff < 0) {
2022 return false;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002023 }
2024
Narayan Kamath48620f12014-01-20 13:57:11 +00002025 if (diff > 0) {
2026 return true;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002027 }
2028 }
2029
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002030 if (screenLayout || o.screenLayout) {
2031 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) {
2032 if (!(screenLayout & MASK_LAYOUTDIR)) return false;
2033 if (!(o.screenLayout & MASK_LAYOUTDIR)) return true;
2034 }
2035 }
2036
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002037 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2038 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2039 if (!smallestScreenWidthDp) return false;
2040 if (!o.smallestScreenWidthDp) return true;
2041 }
2042 }
2043
2044 if (screenSizeDp || o.screenSizeDp) {
2045 if (screenWidthDp != o.screenWidthDp) {
2046 if (!screenWidthDp) return false;
2047 if (!o.screenWidthDp) return true;
2048 }
2049
2050 if (screenHeightDp != o.screenHeightDp) {
2051 if (!screenHeightDp) return false;
2052 if (!o.screenHeightDp) return true;
2053 }
2054 }
2055
2056 if (screenLayout || o.screenLayout) {
2057 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
2058 if (!(screenLayout & MASK_SCREENSIZE)) return false;
2059 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
2060 }
2061 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
2062 if (!(screenLayout & MASK_SCREENLONG)) return false;
2063 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
2064 }
2065 }
2066
2067 if (orientation != o.orientation) {
2068 if (!orientation) return false;
2069 if (!o.orientation) return true;
2070 }
2071
2072 if (uiMode || o.uiMode) {
2073 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
2074 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
2075 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
2076 }
2077 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
2078 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
2079 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
2080 }
2081 }
2082
2083 // density is never 'more specific'
2084 // as the default just equals 160
2085
2086 if (touchscreen != o.touchscreen) {
2087 if (!touchscreen) return false;
2088 if (!o.touchscreen) return true;
2089 }
2090
2091 if (input || o.input) {
2092 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
2093 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
2094 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
2095 }
2096
2097 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
2098 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
2099 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
2100 }
2101
2102 if (keyboard != o.keyboard) {
2103 if (!keyboard) return false;
2104 if (!o.keyboard) return true;
2105 }
2106
2107 if (navigation != o.navigation) {
2108 if (!navigation) return false;
2109 if (!o.navigation) return true;
2110 }
2111 }
2112
2113 if (screenSize || o.screenSize) {
2114 if (screenWidth != o.screenWidth) {
2115 if (!screenWidth) return false;
2116 if (!o.screenWidth) return true;
2117 }
2118
2119 if (screenHeight != o.screenHeight) {
2120 if (!screenHeight) return false;
2121 if (!o.screenHeight) return true;
2122 }
2123 }
2124
2125 if (version || o.version) {
2126 if (sdkVersion != o.sdkVersion) {
2127 if (!sdkVersion) return false;
2128 if (!o.sdkVersion) return true;
2129 }
2130
2131 if (minorVersion != o.minorVersion) {
2132 if (!minorVersion) return false;
2133 if (!o.minorVersion) return true;
2134 }
2135 }
2136 return false;
2137}
2138
2139bool ResTable_config::isBetterThan(const ResTable_config& o,
2140 const ResTable_config* requested) const {
2141 if (requested) {
2142 if (imsi || o.imsi) {
2143 if ((mcc != o.mcc) && requested->mcc) {
2144 return (mcc);
2145 }
2146
2147 if ((mnc != o.mnc) && requested->mnc) {
2148 return (mnc);
2149 }
2150 }
2151
2152 if (locale || o.locale) {
2153 if ((language[0] != o.language[0]) && requested->language[0]) {
2154 return (language[0]);
2155 }
2156
2157 if ((country[0] != o.country[0]) && requested->country[0]) {
2158 return (country[0]);
2159 }
2160 }
2161
Narayan Kamath48620f12014-01-20 13:57:11 +00002162 if (localeScript[0] || o.localeScript[0]) {
2163 if (localeScript[0] != o.localeScript[0] && requested->localeScript[0]) {
2164 return localeScript[0];
2165 }
2166 }
2167
2168 if (localeVariant[0] || o.localeVariant[0]) {
2169 if (localeVariant[0] != o.localeVariant[0] && requested->localeVariant[0]) {
2170 return localeVariant[0];
2171 }
2172 }
2173
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002174 if (screenLayout || o.screenLayout) {
2175 if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0
2176 && (requested->screenLayout & MASK_LAYOUTDIR)) {
2177 int myLayoutDir = screenLayout & MASK_LAYOUTDIR;
2178 int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR;
2179 return (myLayoutDir > oLayoutDir);
2180 }
2181 }
2182
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002183 if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
2184 // The configuration closest to the actual size is best.
2185 // We assume that larger configs have already been filtered
2186 // out at this point. That means we just want the largest one.
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002187 if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
2188 return smallestScreenWidthDp > o.smallestScreenWidthDp;
2189 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002190 }
2191
2192 if (screenSizeDp || o.screenSizeDp) {
2193 // "Better" is based on the sum of the difference between both
2194 // width and height from the requested dimensions. We are
2195 // assuming the invalid configs (with smaller dimens) have
2196 // already been filtered. Note that if a particular dimension
2197 // is unspecified, we will end up with a large value (the
2198 // difference between 0 and the requested dimension), which is
2199 // good since we will prefer a config that has specified a
2200 // dimension value.
2201 int myDelta = 0, otherDelta = 0;
2202 if (requested->screenWidthDp) {
2203 myDelta += requested->screenWidthDp - screenWidthDp;
2204 otherDelta += requested->screenWidthDp - o.screenWidthDp;
2205 }
2206 if (requested->screenHeightDp) {
2207 myDelta += requested->screenHeightDp - screenHeightDp;
2208 otherDelta += requested->screenHeightDp - o.screenHeightDp;
2209 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002210 if (kDebugTableSuperNoisy) {
2211 ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
2212 screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
2213 requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
2214 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002215 if (myDelta != otherDelta) {
2216 return myDelta < otherDelta;
2217 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002218 }
2219
2220 if (screenLayout || o.screenLayout) {
2221 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
2222 && (requested->screenLayout & MASK_SCREENSIZE)) {
2223 // A little backwards compatibility here: undefined is
2224 // considered equivalent to normal. But only if the
2225 // requested size is at least normal; otherwise, small
2226 // is better than the default.
2227 int mySL = (screenLayout & MASK_SCREENSIZE);
2228 int oSL = (o.screenLayout & MASK_SCREENSIZE);
2229 int fixedMySL = mySL;
2230 int fixedOSL = oSL;
2231 if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
2232 if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
2233 if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
2234 }
2235 // For screen size, the best match is the one that is
2236 // closest to the requested screen size, but not over
2237 // (the not over part is dealt with in match() below).
2238 if (fixedMySL == fixedOSL) {
2239 // If the two are the same, but 'this' is actually
2240 // undefined, then the other is really a better match.
2241 if (mySL == 0) return false;
2242 return true;
2243 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002244 if (fixedMySL != fixedOSL) {
2245 return fixedMySL > fixedOSL;
2246 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002247 }
2248 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
2249 && (requested->screenLayout & MASK_SCREENLONG)) {
2250 return (screenLayout & MASK_SCREENLONG);
2251 }
2252 }
2253
2254 if ((orientation != o.orientation) && requested->orientation) {
2255 return (orientation);
2256 }
2257
2258 if (uiMode || o.uiMode) {
2259 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
2260 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
2261 return (uiMode & MASK_UI_MODE_TYPE);
2262 }
2263 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
2264 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
2265 return (uiMode & MASK_UI_MODE_NIGHT);
2266 }
2267 }
2268
2269 if (screenType || o.screenType) {
2270 if (density != o.density) {
Adam Lesinski31245b42014-08-22 19:10:56 -07002271 // Use the system default density (DENSITY_MEDIUM, 160dpi) if none specified.
2272 const int thisDensity = density ? density : int(ResTable_config::DENSITY_MEDIUM);
2273 const int otherDensity = o.density ? o.density : int(ResTable_config::DENSITY_MEDIUM);
2274
2275 // We always prefer DENSITY_ANY over scaling a density bucket.
2276 if (thisDensity == ResTable_config::DENSITY_ANY) {
2277 return true;
2278 } else if (otherDensity == ResTable_config::DENSITY_ANY) {
2279 return false;
2280 }
2281
2282 int requestedDensity = requested->density;
2283 if (requested->density == 0 ||
2284 requested->density == ResTable_config::DENSITY_ANY) {
2285 requestedDensity = ResTable_config::DENSITY_MEDIUM;
2286 }
2287
2288 // DENSITY_ANY is now dealt with. We should look to
2289 // pick a density bucket and potentially scale it.
2290 // Any density is potentially useful
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002291 // because the system will scale it. Scaling down
2292 // is generally better than scaling up.
Adam Lesinski31245b42014-08-22 19:10:56 -07002293 int h = thisDensity;
2294 int l = otherDensity;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002295 bool bImBigger = true;
2296 if (l > h) {
2297 int t = h;
2298 h = l;
2299 l = t;
2300 bImBigger = false;
2301 }
2302
Adam Lesinski31245b42014-08-22 19:10:56 -07002303 if (requestedDensity >= h) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002304 // requested value higher than both l and h, give h
2305 return bImBigger;
2306 }
Adam Lesinski31245b42014-08-22 19:10:56 -07002307 if (l >= requestedDensity) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002308 // requested value lower than both l and h, give l
2309 return !bImBigger;
2310 }
2311 // saying that scaling down is 2x better than up
Adam Lesinski31245b42014-08-22 19:10:56 -07002312 if (((2 * l) - requestedDensity) * h > requestedDensity * requestedDensity) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002313 return !bImBigger;
2314 } else {
2315 return bImBigger;
2316 }
2317 }
2318
2319 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
2320 return (touchscreen);
2321 }
2322 }
2323
2324 if (input || o.input) {
2325 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
2326 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
2327 if (keysHidden != oKeysHidden) {
2328 const int reqKeysHidden =
2329 requested->inputFlags & MASK_KEYSHIDDEN;
2330 if (reqKeysHidden) {
2331
2332 if (!keysHidden) return false;
2333 if (!oKeysHidden) return true;
2334 // For compatibility, we count KEYSHIDDEN_NO as being
2335 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
2336 // these by making an exact match more specific.
2337 if (reqKeysHidden == keysHidden) return true;
2338 if (reqKeysHidden == oKeysHidden) return false;
2339 }
2340 }
2341
2342 const int navHidden = inputFlags & MASK_NAVHIDDEN;
2343 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
2344 if (navHidden != oNavHidden) {
2345 const int reqNavHidden =
2346 requested->inputFlags & MASK_NAVHIDDEN;
2347 if (reqNavHidden) {
2348
2349 if (!navHidden) return false;
2350 if (!oNavHidden) return true;
2351 }
2352 }
2353
2354 if ((keyboard != o.keyboard) && requested->keyboard) {
2355 return (keyboard);
2356 }
2357
2358 if ((navigation != o.navigation) && requested->navigation) {
2359 return (navigation);
2360 }
2361 }
2362
2363 if (screenSize || o.screenSize) {
2364 // "Better" is based on the sum of the difference between both
2365 // width and height from the requested dimensions. We are
2366 // assuming the invalid configs (with smaller sizes) have
2367 // already been filtered. Note that if a particular dimension
2368 // is unspecified, we will end up with a large value (the
2369 // difference between 0 and the requested dimension), which is
2370 // good since we will prefer a config that has specified a
2371 // size value.
2372 int myDelta = 0, otherDelta = 0;
2373 if (requested->screenWidth) {
2374 myDelta += requested->screenWidth - screenWidth;
2375 otherDelta += requested->screenWidth - o.screenWidth;
2376 }
2377 if (requested->screenHeight) {
2378 myDelta += requested->screenHeight - screenHeight;
2379 otherDelta += requested->screenHeight - o.screenHeight;
2380 }
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002381 if (myDelta != otherDelta) {
2382 return myDelta < otherDelta;
2383 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002384 }
2385
2386 if (version || o.version) {
2387 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
2388 return (sdkVersion > o.sdkVersion);
2389 }
2390
2391 if ((minorVersion != o.minorVersion) &&
2392 requested->minorVersion) {
2393 return (minorVersion);
2394 }
2395 }
2396
2397 return false;
2398 }
2399 return isMoreSpecificThan(o);
2400}
2401
2402bool ResTable_config::match(const ResTable_config& settings) const {
2403 if (imsi != 0) {
2404 if (mcc != 0 && mcc != settings.mcc) {
2405 return false;
2406 }
2407 if (mnc != 0 && mnc != settings.mnc) {
2408 return false;
2409 }
2410 }
2411 if (locale != 0) {
Narayan Kamath48620f12014-01-20 13:57:11 +00002412 // Don't consider the script & variants when deciding matches.
2413 //
2414 // If we two configs differ only in their script or language, they
2415 // can be weeded out in the isMoreSpecificThan test.
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002416 if (language[0] != 0
2417 && (language[0] != settings.language[0]
2418 || language[1] != settings.language[1])) {
2419 return false;
2420 }
Narayan Kamath48620f12014-01-20 13:57:11 +00002421
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002422 if (country[0] != 0
2423 && (country[0] != settings.country[0]
2424 || country[1] != settings.country[1])) {
2425 return false;
2426 }
2427 }
Narayan Kamath48620f12014-01-20 13:57:11 +00002428
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002429 if (screenConfig != 0) {
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002430 const int layoutDir = screenLayout&MASK_LAYOUTDIR;
2431 const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR;
2432 if (layoutDir != 0 && layoutDir != setLayoutDir) {
2433 return false;
2434 }
2435
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002436 const int screenSize = screenLayout&MASK_SCREENSIZE;
2437 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
2438 // Any screen sizes for larger screens than the setting do not
2439 // match.
2440 if (screenSize != 0 && screenSize > setScreenSize) {
2441 return false;
2442 }
2443
2444 const int screenLong = screenLayout&MASK_SCREENLONG;
2445 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
2446 if (screenLong != 0 && screenLong != setScreenLong) {
2447 return false;
2448 }
2449
2450 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
2451 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
2452 if (uiModeType != 0 && uiModeType != setUiModeType) {
2453 return false;
2454 }
2455
2456 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
2457 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
2458 if (uiModeNight != 0 && uiModeNight != setUiModeNight) {
2459 return false;
2460 }
2461
2462 if (smallestScreenWidthDp != 0
2463 && smallestScreenWidthDp > settings.smallestScreenWidthDp) {
2464 return false;
2465 }
2466 }
2467 if (screenSizeDp != 0) {
2468 if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002469 if (kDebugTableSuperNoisy) {
2470 ALOGI("Filtering out width %d in requested %d", screenWidthDp,
2471 settings.screenWidthDp);
2472 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002473 return false;
2474 }
2475 if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002476 if (kDebugTableSuperNoisy) {
2477 ALOGI("Filtering out height %d in requested %d", screenHeightDp,
2478 settings.screenHeightDp);
2479 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002480 return false;
2481 }
2482 }
2483 if (screenType != 0) {
2484 if (orientation != 0 && orientation != settings.orientation) {
2485 return false;
2486 }
2487 // density always matches - we can scale it. See isBetterThan
2488 if (touchscreen != 0 && touchscreen != settings.touchscreen) {
2489 return false;
2490 }
2491 }
2492 if (input != 0) {
2493 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
2494 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
2495 if (keysHidden != 0 && keysHidden != setKeysHidden) {
2496 // For compatibility, we count a request for KEYSHIDDEN_NO as also
2497 // matching the more recent KEYSHIDDEN_SOFT. Basically
2498 // KEYSHIDDEN_NO means there is some kind of keyboard available.
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002499 if (kDebugTableSuperNoisy) {
2500 ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
2501 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002502 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07002503 if (kDebugTableSuperNoisy) {
2504 ALOGI("No match!");
2505 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002506 return false;
2507 }
2508 }
2509 const int navHidden = inputFlags&MASK_NAVHIDDEN;
2510 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
2511 if (navHidden != 0 && navHidden != setNavHidden) {
2512 return false;
2513 }
2514 if (keyboard != 0 && keyboard != settings.keyboard) {
2515 return false;
2516 }
2517 if (navigation != 0 && navigation != settings.navigation) {
2518 return false;
2519 }
2520 }
2521 if (screenSize != 0) {
2522 if (screenWidth != 0 && screenWidth > settings.screenWidth) {
2523 return false;
2524 }
2525 if (screenHeight != 0 && screenHeight > settings.screenHeight) {
2526 return false;
2527 }
2528 }
2529 if (version != 0) {
2530 if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) {
2531 return false;
2532 }
2533 if (minorVersion != 0 && minorVersion != settings.minorVersion) {
2534 return false;
2535 }
2536 }
2537 return true;
2538}
2539
Narayan Kamath788fa412014-01-21 15:32:36 +00002540void ResTable_config::getBcp47Locale(char str[RESTABLE_MAX_LOCALE_LEN]) const {
Narayan Kamath48620f12014-01-20 13:57:11 +00002541 memset(str, 0, RESTABLE_MAX_LOCALE_LEN);
2542
2543 // This represents the "any" locale value, which has traditionally been
2544 // represented by the empty string.
2545 if (!language[0] && !country[0]) {
2546 return;
2547 }
2548
2549 size_t charsWritten = 0;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002550 if (language[0]) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002551 charsWritten += unpackLanguage(str);
Narayan Kamath48620f12014-01-20 13:57:11 +00002552 }
2553
2554 if (localeScript[0]) {
2555 if (charsWritten) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002556 str[charsWritten++] = '-';
Narayan Kamath48620f12014-01-20 13:57:11 +00002557 }
2558 memcpy(str + charsWritten, localeScript, sizeof(localeScript));
Narayan Kamath788fa412014-01-21 15:32:36 +00002559 charsWritten += sizeof(localeScript);
2560 }
2561
2562 if (country[0]) {
2563 if (charsWritten) {
2564 str[charsWritten++] = '-';
2565 }
2566 charsWritten += unpackRegion(str + charsWritten);
Narayan Kamath48620f12014-01-20 13:57:11 +00002567 }
2568
2569 if (localeVariant[0]) {
2570 if (charsWritten) {
Narayan Kamath788fa412014-01-21 15:32:36 +00002571 str[charsWritten++] = '-';
Narayan Kamath48620f12014-01-20 13:57:11 +00002572 }
2573 memcpy(str + charsWritten, localeVariant, sizeof(localeVariant));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002574 }
2575}
2576
Narayan Kamath788fa412014-01-21 15:32:36 +00002577/* static */ inline bool assignLocaleComponent(ResTable_config* config,
2578 const char* start, size_t size) {
2579
2580 switch (size) {
2581 case 0:
2582 return false;
2583 case 2:
2584 case 3:
2585 config->language[0] ? config->packRegion(start) : config->packLanguage(start);
2586 break;
2587 case 4:
2588 config->localeScript[0] = toupper(start[0]);
2589 for (size_t i = 1; i < 4; ++i) {
2590 config->localeScript[i] = tolower(start[i]);
2591 }
2592 break;
2593 case 5:
2594 case 6:
2595 case 7:
2596 case 8:
2597 for (size_t i = 0; i < size; ++i) {
2598 config->localeVariant[i] = tolower(start[i]);
2599 }
2600 break;
2601 default:
2602 return false;
2603 }
2604
2605 return true;
2606}
2607
2608void ResTable_config::setBcp47Locale(const char* in) {
2609 locale = 0;
2610 memset(localeScript, 0, sizeof(localeScript));
2611 memset(localeVariant, 0, sizeof(localeVariant));
2612
2613 const char* separator = in;
2614 const char* start = in;
2615 while ((separator = strchr(start, '-')) != NULL) {
2616 const size_t size = separator - start;
2617 if (!assignLocaleComponent(this, start, size)) {
2618 fprintf(stderr, "Invalid BCP-47 locale string: %s", in);
2619 }
2620
2621 start = (separator + 1);
2622 }
2623
2624 const size_t size = in + strlen(in) - start;
2625 assignLocaleComponent(this, start, size);
2626}
2627
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002628String8 ResTable_config::toString() const {
2629 String8 res;
2630
2631 if (mcc != 0) {
2632 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07002633 res.appendFormat("mcc%d", dtohs(mcc));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002634 }
2635 if (mnc != 0) {
2636 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07002637 res.appendFormat("mnc%d", dtohs(mnc));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002638 }
Adam Lesinskifab50872014-04-16 14:40:42 -07002639
Narayan Kamath48620f12014-01-20 13:57:11 +00002640 char localeStr[RESTABLE_MAX_LOCALE_LEN];
Narayan Kamath788fa412014-01-21 15:32:36 +00002641 getBcp47Locale(localeStr);
Adam Lesinskifab50872014-04-16 14:40:42 -07002642 if (strlen(localeStr) > 0) {
2643 if (res.size() > 0) res.append("-");
2644 res.append(localeStr);
2645 }
Narayan Kamath48620f12014-01-20 13:57:11 +00002646
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002647 if ((screenLayout&MASK_LAYOUTDIR) != 0) {
2648 if (res.size() > 0) res.append("-");
2649 switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) {
2650 case ResTable_config::LAYOUTDIR_LTR:
Fabrice Di Meglio8a802db2012-09-05 13:12:02 -07002651 res.append("ldltr");
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002652 break;
2653 case ResTable_config::LAYOUTDIR_RTL:
Fabrice Di Meglio8a802db2012-09-05 13:12:02 -07002654 res.append("ldrtl");
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07002655 break;
2656 default:
2657 res.appendFormat("layoutDir=%d",
2658 dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR));
2659 break;
2660 }
2661 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002662 if (smallestScreenWidthDp != 0) {
2663 if (res.size() > 0) res.append("-");
2664 res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp));
2665 }
2666 if (screenWidthDp != 0) {
2667 if (res.size() > 0) res.append("-");
2668 res.appendFormat("w%ddp", dtohs(screenWidthDp));
2669 }
2670 if (screenHeightDp != 0) {
2671 if (res.size() > 0) res.append("-");
2672 res.appendFormat("h%ddp", dtohs(screenHeightDp));
2673 }
2674 if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) {
2675 if (res.size() > 0) res.append("-");
2676 switch (screenLayout&ResTable_config::MASK_SCREENSIZE) {
2677 case ResTable_config::SCREENSIZE_SMALL:
2678 res.append("small");
2679 break;
2680 case ResTable_config::SCREENSIZE_NORMAL:
2681 res.append("normal");
2682 break;
2683 case ResTable_config::SCREENSIZE_LARGE:
2684 res.append("large");
2685 break;
2686 case ResTable_config::SCREENSIZE_XLARGE:
2687 res.append("xlarge");
2688 break;
2689 default:
2690 res.appendFormat("screenLayoutSize=%d",
2691 dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE));
2692 break;
2693 }
2694 }
2695 if ((screenLayout&MASK_SCREENLONG) != 0) {
2696 if (res.size() > 0) res.append("-");
2697 switch (screenLayout&ResTable_config::MASK_SCREENLONG) {
2698 case ResTable_config::SCREENLONG_NO:
2699 res.append("notlong");
2700 break;
2701 case ResTable_config::SCREENLONG_YES:
2702 res.append("long");
2703 break;
2704 default:
2705 res.appendFormat("screenLayoutLong=%d",
2706 dtohs(screenLayout&ResTable_config::MASK_SCREENLONG));
2707 break;
2708 }
2709 }
2710 if (orientation != ORIENTATION_ANY) {
2711 if (res.size() > 0) res.append("-");
2712 switch (orientation) {
2713 case ResTable_config::ORIENTATION_PORT:
2714 res.append("port");
2715 break;
2716 case ResTable_config::ORIENTATION_LAND:
2717 res.append("land");
2718 break;
2719 case ResTable_config::ORIENTATION_SQUARE:
2720 res.append("square");
2721 break;
2722 default:
2723 res.appendFormat("orientation=%d", dtohs(orientation));
2724 break;
2725 }
2726 }
2727 if ((uiMode&MASK_UI_MODE_TYPE) != UI_MODE_TYPE_ANY) {
2728 if (res.size() > 0) res.append("-");
2729 switch (uiMode&ResTable_config::MASK_UI_MODE_TYPE) {
2730 case ResTable_config::UI_MODE_TYPE_DESK:
2731 res.append("desk");
2732 break;
2733 case ResTable_config::UI_MODE_TYPE_CAR:
2734 res.append("car");
2735 break;
2736 case ResTable_config::UI_MODE_TYPE_TELEVISION:
2737 res.append("television");
2738 break;
2739 case ResTable_config::UI_MODE_TYPE_APPLIANCE:
2740 res.append("appliance");
2741 break;
John Spurlock6c191292014-04-03 16:37:27 -04002742 case ResTable_config::UI_MODE_TYPE_WATCH:
2743 res.append("watch");
2744 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002745 default:
2746 res.appendFormat("uiModeType=%d",
2747 dtohs(screenLayout&ResTable_config::MASK_UI_MODE_TYPE));
2748 break;
2749 }
2750 }
2751 if ((uiMode&MASK_UI_MODE_NIGHT) != 0) {
2752 if (res.size() > 0) res.append("-");
2753 switch (uiMode&ResTable_config::MASK_UI_MODE_NIGHT) {
2754 case ResTable_config::UI_MODE_NIGHT_NO:
2755 res.append("notnight");
2756 break;
2757 case ResTable_config::UI_MODE_NIGHT_YES:
2758 res.append("night");
2759 break;
2760 default:
2761 res.appendFormat("uiModeNight=%d",
2762 dtohs(uiMode&MASK_UI_MODE_NIGHT));
2763 break;
2764 }
2765 }
2766 if (density != DENSITY_DEFAULT) {
2767 if (res.size() > 0) res.append("-");
2768 switch (density) {
2769 case ResTable_config::DENSITY_LOW:
2770 res.append("ldpi");
2771 break;
2772 case ResTable_config::DENSITY_MEDIUM:
2773 res.append("mdpi");
2774 break;
2775 case ResTable_config::DENSITY_TV:
2776 res.append("tvdpi");
2777 break;
2778 case ResTable_config::DENSITY_HIGH:
2779 res.append("hdpi");
2780 break;
2781 case ResTable_config::DENSITY_XHIGH:
2782 res.append("xhdpi");
2783 break;
2784 case ResTable_config::DENSITY_XXHIGH:
2785 res.append("xxhdpi");
2786 break;
Adam Lesinski8d5667d2014-08-13 21:02:57 -07002787 case ResTable_config::DENSITY_XXXHIGH:
2788 res.append("xxxhdpi");
2789 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002790 case ResTable_config::DENSITY_NONE:
2791 res.append("nodpi");
2792 break;
Adam Lesinski31245b42014-08-22 19:10:56 -07002793 case ResTable_config::DENSITY_ANY:
2794 res.append("anydpi");
2795 break;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002796 default:
Dianne Hackborn5c6dfeb2012-03-09 13:17:17 -08002797 res.appendFormat("%ddpi", dtohs(density));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002798 break;
2799 }
2800 }
2801 if (touchscreen != TOUCHSCREEN_ANY) {
2802 if (res.size() > 0) res.append("-");
2803 switch (touchscreen) {
2804 case ResTable_config::TOUCHSCREEN_NOTOUCH:
2805 res.append("notouch");
2806 break;
2807 case ResTable_config::TOUCHSCREEN_FINGER:
2808 res.append("finger");
2809 break;
2810 case ResTable_config::TOUCHSCREEN_STYLUS:
2811 res.append("stylus");
2812 break;
2813 default:
2814 res.appendFormat("touchscreen=%d", dtohs(touchscreen));
2815 break;
2816 }
2817 }
Adam Lesinskifab50872014-04-16 14:40:42 -07002818 if ((inputFlags&MASK_KEYSHIDDEN) != 0) {
2819 if (res.size() > 0) res.append("-");
2820 switch (inputFlags&MASK_KEYSHIDDEN) {
2821 case ResTable_config::KEYSHIDDEN_NO:
2822 res.append("keysexposed");
2823 break;
2824 case ResTable_config::KEYSHIDDEN_YES:
2825 res.append("keyshidden");
2826 break;
2827 case ResTable_config::KEYSHIDDEN_SOFT:
2828 res.append("keyssoft");
2829 break;
2830 }
2831 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002832 if (keyboard != KEYBOARD_ANY) {
2833 if (res.size() > 0) res.append("-");
2834 switch (keyboard) {
2835 case ResTable_config::KEYBOARD_NOKEYS:
2836 res.append("nokeys");
2837 break;
2838 case ResTable_config::KEYBOARD_QWERTY:
2839 res.append("qwerty");
2840 break;
2841 case ResTable_config::KEYBOARD_12KEY:
2842 res.append("12key");
2843 break;
2844 default:
2845 res.appendFormat("keyboard=%d", dtohs(keyboard));
2846 break;
2847 }
2848 }
Adam Lesinskifab50872014-04-16 14:40:42 -07002849 if ((inputFlags&MASK_NAVHIDDEN) != 0) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002850 if (res.size() > 0) res.append("-");
Adam Lesinskifab50872014-04-16 14:40:42 -07002851 switch (inputFlags&MASK_NAVHIDDEN) {
2852 case ResTable_config::NAVHIDDEN_NO:
2853 res.append("navexposed");
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002854 break;
Adam Lesinskifab50872014-04-16 14:40:42 -07002855 case ResTable_config::NAVHIDDEN_YES:
2856 res.append("navhidden");
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002857 break;
Adam Lesinskifab50872014-04-16 14:40:42 -07002858 default:
2859 res.appendFormat("inputFlagsNavHidden=%d",
2860 dtohs(inputFlags&MASK_NAVHIDDEN));
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002861 break;
2862 }
2863 }
2864 if (navigation != NAVIGATION_ANY) {
2865 if (res.size() > 0) res.append("-");
2866 switch (navigation) {
2867 case ResTable_config::NAVIGATION_NONAV:
2868 res.append("nonav");
2869 break;
2870 case ResTable_config::NAVIGATION_DPAD:
2871 res.append("dpad");
2872 break;
2873 case ResTable_config::NAVIGATION_TRACKBALL:
2874 res.append("trackball");
2875 break;
2876 case ResTable_config::NAVIGATION_WHEEL:
2877 res.append("wheel");
2878 break;
2879 default:
2880 res.appendFormat("navigation=%d", dtohs(navigation));
2881 break;
2882 }
2883 }
Dianne Hackborn6c997a92012-01-31 11:27:43 -08002884 if (screenSize != 0) {
2885 if (res.size() > 0) res.append("-");
2886 res.appendFormat("%dx%d", dtohs(screenWidth), dtohs(screenHeight));
2887 }
2888 if (version != 0) {
2889 if (res.size() > 0) res.append("-");
2890 res.appendFormat("v%d", dtohs(sdkVersion));
2891 if (minorVersion != 0) {
2892 res.appendFormat(".%d", dtohs(minorVersion));
2893 }
2894 }
2895
2896 return res;
2897}
2898
2899// --------------------------------------------------------------------
2900// --------------------------------------------------------------------
2901// --------------------------------------------------------------------
2902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903struct ResTable::Header
2904{
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01002905 Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL),
2906 resourceIDMap(NULL), resourceIDMapSize(0) { }
2907
2908 ~Header()
2909 {
2910 free(resourceIDMap);
2911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002913 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 void* ownedData;
2915 const ResTable_header* header;
2916 size_t size;
2917 const uint8_t* dataEnd;
2918 size_t index;
Narayan Kamath7c4887f2014-01-27 17:32:37 +00002919 int32_t cookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920
2921 ResStringPool values;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01002922 uint32_t* resourceIDMap;
2923 size_t resourceIDMapSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924};
2925
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002926struct ResTable::Entry {
2927 ResTable_config config;
2928 const ResTable_entry* entry;
2929 const ResTable_type* type;
2930 uint32_t specFlags;
2931 const Package* package;
2932
2933 StringPoolRef typeStr;
2934 StringPoolRef keyStr;
2935};
2936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937struct ResTable::Type
2938{
2939 Type(const Header* _header, const Package* _package, size_t count)
2940 : header(_header), package(_package), entryCount(count),
2941 typeSpec(NULL), typeSpecFlags(NULL) { }
2942 const Header* const header;
2943 const Package* const package;
2944 const size_t entryCount;
2945 const ResTable_typeSpec* typeSpec;
2946 const uint32_t* typeSpecFlags;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002947 IdmapEntries idmapEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 Vector<const ResTable_type*> configs;
2949};
2950
2951struct ResTable::Package
2952{
Dianne Hackborn78c40512009-07-06 11:07:40 -07002953 Package(ResTable* _owner, const Header* _header, const ResTable_package* _package)
Adam Lesinski18560882014-08-15 17:18:21 +00002954 : owner(_owner), header(_header), package(_package), typeIdOffset(0) {
2955 if (dtohs(package->header.headerSize) == sizeof(package)) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002956 // The package structure is the same size as the definition.
2957 // This means it contains the typeIdOffset field.
Adam Lesinski18560882014-08-15 17:18:21 +00002958 typeIdOffset = package->typeIdOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 }
2960 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07002961
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002962 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 const Header* const header;
Adam Lesinski18560882014-08-15 17:18:21 +00002964 const ResTable_package* const package;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
Dianne Hackborn78c40512009-07-06 11:07:40 -07002966 ResStringPool typeStrings;
2967 ResStringPool keyStrings;
Mark Salyzyn00adb862014-03-19 11:00:06 -07002968
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002969 size_t typeIdOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970};
2971
2972// A group of objects describing a particular resource package.
2973// The first in 'package' is always the root object (from the resource
2974// table that defined the package); the ones after are skins on top of it.
2975struct ResTable::PackageGroup
2976{
Dianne Hackborn78c40512009-07-06 11:07:40 -07002977 PackageGroup(ResTable* _owner, const String16& _name, uint32_t _id)
Adam Lesinskide898ff2014-01-29 18:20:45 -08002978 : owner(_owner)
2979 , name(_name)
2980 , id(_id)
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002981 , largestTypeId(0)
Adam Lesinskide898ff2014-01-29 18:20:45 -08002982 , bags(NULL)
2983 , dynamicRefTable(static_cast<uint8_t>(_id))
2984 { }
2985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 ~PackageGroup() {
2987 clearBagCache();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07002988 const size_t numTypes = types.size();
2989 for (size_t i = 0; i < numTypes; i++) {
2990 const TypeList& typeList = types[i];
2991 const size_t numInnerTypes = typeList.size();
2992 for (size_t j = 0; j < numInnerTypes; j++) {
2993 if (typeList[j]->package->owner == owner) {
2994 delete typeList[j];
2995 }
2996 }
2997 }
2998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 const size_t N = packages.size();
3000 for (size_t i=0; i<N; i++) {
Dianne Hackborn78c40512009-07-06 11:07:40 -07003001 Package* pkg = packages[i];
3002 if (pkg->owner == owner) {
3003 delete pkg;
3004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 }
3006 }
3007
3008 void clearBagCache() {
3009 if (bags) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003010 if (kDebugTableNoisy) {
3011 printf("bags=%p\n", bags);
3012 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003013 for (size_t i = 0; i < bags->size(); i++) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003014 if (kDebugTableNoisy) {
3015 printf("type=%zu\n", i);
3016 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003017 const TypeList& typeList = types[i];
Adam Lesinski7f668d02014-08-28 18:32:32 -07003018 if (!typeList.isEmpty()) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003019 bag_set** typeBags = bags->get(i);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003020 if (kDebugTableNoisy) {
3021 printf("typeBags=%p\n", typeBags);
3022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (typeBags) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003024 const size_t N = typeList[0]->entryCount;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003025 if (kDebugTableNoisy) {
3026 printf("type->entryCount=%zu\n", N);
3027 }
3028 for (size_t j = 0; j < N; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF)
3030 free(typeBags[j]);
3031 }
3032 free(typeBags);
3033 }
3034 }
3035 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003036 delete bags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 bags = NULL;
3038 }
3039 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003040
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003041 ssize_t findType16(const char16_t* type, size_t len) const {
3042 const size_t N = packages.size();
3043 for (size_t i = 0; i < N; i++) {
3044 ssize_t index = packages[i]->typeStrings.indexOfString(type, len);
3045 if (index >= 0) {
3046 return index + packages[i]->typeIdOffset;
3047 }
3048 }
3049 return -1;
3050 }
3051
3052 const ResTable* const owner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 String16 const name;
3054 uint32_t const id;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003055
3056 // This is mainly used to keep track of the loaded packages
3057 // and to clean them up properly. Accessing resources happens from
3058 // the 'types' array.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 Vector<Package*> packages;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003060
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003061 ByteBucketArray<TypeList> types;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003063 uint8_t largestTypeId;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 // Computed attribute bags, first indexed by the type and second
3066 // by the entry in that type.
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003067 ByteBucketArray<bag_set**>* bags;
Adam Lesinskide898ff2014-01-29 18:20:45 -08003068
3069 // The table mapping dynamic references to resolved references for
3070 // this package group.
3071 // TODO: We may be able to support dynamic references in overlays
3072 // by having these tables in a per-package scope rather than
3073 // per-package-group.
3074 DynamicRefTable dynamicRefTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075};
3076
3077struct ResTable::bag_set
3078{
3079 size_t numAttrs; // number in array
3080 size_t availAttrs; // total space in array
3081 uint32_t typeSpecFlags;
3082 // Followed by 'numAttr' bag_entry structures.
3083};
3084
3085ResTable::Theme::Theme(const ResTable& table)
3086 : mTable(table)
3087{
3088 memset(mPackages, 0, sizeof(mPackages));
3089}
3090
3091ResTable::Theme::~Theme()
3092{
3093 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3094 package_info* pi = mPackages[i];
3095 if (pi != NULL) {
3096 free_package(pi);
3097 }
3098 }
3099}
3100
3101void ResTable::Theme::free_package(package_info* pi)
3102{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003103 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 theme_entry* te = pi->types[j].entries;
3105 if (te != NULL) {
3106 free(te);
3107 }
3108 }
3109 free(pi);
3110}
3111
3112ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi)
3113{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003114 package_info* newpi = (package_info*)malloc(sizeof(package_info));
3115 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 size_t cnt = pi->types[j].numEntries;
3117 newpi->types[j].numEntries = cnt;
3118 theme_entry* te = pi->types[j].entries;
3119 if (te != NULL) {
3120 theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry));
3121 newpi->types[j].entries = newte;
3122 memcpy(newte, te, cnt*sizeof(theme_entry));
3123 } else {
3124 newpi->types[j].entries = NULL;
3125 }
3126 }
3127 return newpi;
3128}
3129
3130status_t ResTable::Theme::applyStyle(uint32_t resID, bool force)
3131{
3132 const bag_entry* bag;
3133 uint32_t bagTypeSpecFlags = 0;
3134 mTable.lock();
3135 const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003136 if (kDebugTableNoisy) {
3137 ALOGV("Applying style 0x%08x to theme %p, count=%zu", resID, this, N);
3138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 if (N < 0) {
3140 mTable.unlock();
3141 return N;
3142 }
3143
3144 uint32_t curPackage = 0xffffffff;
3145 ssize_t curPackageIndex = 0;
3146 package_info* curPI = NULL;
3147 uint32_t curType = 0xffffffff;
3148 size_t numEntries = 0;
3149 theme_entry* curEntries = NULL;
3150
3151 const bag_entry* end = bag + N;
3152 while (bag < end) {
3153 const uint32_t attrRes = bag->map.name.ident;
3154 const uint32_t p = Res_GETPACKAGE(attrRes);
3155 const uint32_t t = Res_GETTYPE(attrRes);
3156 const uint32_t e = Res_GETENTRY(attrRes);
3157
3158 if (curPackage != p) {
3159 const ssize_t pidx = mTable.getResourcePackageIndex(attrRes);
3160 if (pidx < 0) {
Steve Block3762c312012-01-06 19:20:56 +00003161 ALOGE("Style contains key with bad package: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 bag++;
3163 continue;
3164 }
3165 curPackage = p;
3166 curPackageIndex = pidx;
3167 curPI = mPackages[pidx];
3168 if (curPI == NULL) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003169 curPI = (package_info*)malloc(sizeof(package_info));
3170 memset(curPI, 0, sizeof(*curPI));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 mPackages[pidx] = curPI;
3172 }
3173 curType = 0xffffffff;
3174 }
3175 if (curType != t) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003176 if (t > Res_MAXTYPE) {
Steve Block3762c312012-01-06 19:20:56 +00003177 ALOGE("Style contains key with bad type: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 bag++;
3179 continue;
3180 }
3181 curType = t;
3182 curEntries = curPI->types[t].entries;
3183 if (curEntries == NULL) {
3184 PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003185 const TypeList& typeList = grp->types[t];
3186 int cnt = typeList.isEmpty() ? 0 : typeList[0]->entryCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 curEntries = (theme_entry*)malloc(cnt*sizeof(theme_entry));
3188 memset(curEntries, Res_value::TYPE_NULL, cnt*sizeof(theme_entry));
3189 curPI->types[t].numEntries = cnt;
3190 curPI->types[t].entries = curEntries;
3191 }
3192 numEntries = curPI->types[t].numEntries;
3193 }
3194 if (e >= numEntries) {
Steve Block3762c312012-01-06 19:20:56 +00003195 ALOGE("Style contains key with bad entry: 0x%08x\n", attrRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 bag++;
3197 continue;
3198 }
3199 theme_entry* curEntry = curEntries + e;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003200 if (kDebugTableNoisy) {
3201 ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x",
3202 attrRes, bag->map.value.dataType, bag->map.value.data,
3203 curEntry->value.dataType);
3204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 if (force || curEntry->value.dataType == Res_value::TYPE_NULL) {
3206 curEntry->stringBlock = bag->stringBlock;
3207 curEntry->typeSpecFlags |= bagTypeSpecFlags;
3208 curEntry->value = bag->map.value;
3209 }
3210
3211 bag++;
3212 }
3213
3214 mTable.unlock();
3215
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003216 if (kDebugTableTheme) {
3217 ALOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this);
3218 dumpToLog();
3219 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 return NO_ERROR;
3222}
3223
3224status_t ResTable::Theme::setTo(const Theme& other)
3225{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003226 if (kDebugTableTheme) {
3227 ALOGI("Setting theme %p from theme %p...\n", this, &other);
3228 dumpToLog();
3229 other.dumpToLog();
3230 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 if (&mTable == &other.mTable) {
3233 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3234 if (mPackages[i] != NULL) {
3235 free_package(mPackages[i]);
3236 }
3237 if (other.mPackages[i] != NULL) {
3238 mPackages[i] = copy_package(other.mPackages[i]);
3239 } else {
3240 mPackages[i] = NULL;
3241 }
3242 }
3243 } else {
3244 // @todo: need to really implement this, not just copy
3245 // the system package (which is still wrong because it isn't
3246 // fixing up resource references).
3247 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3248 if (mPackages[i] != NULL) {
3249 free_package(mPackages[i]);
3250 }
3251 if (i == 0 && other.mPackages[i] != NULL) {
3252 mPackages[i] = copy_package(other.mPackages[i]);
3253 } else {
3254 mPackages[i] = NULL;
3255 }
3256 }
3257 }
3258
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003259 if (kDebugTableTheme) {
3260 ALOGI("Final theme:");
3261 dumpToLog();
3262 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 return NO_ERROR;
3265}
3266
3267ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue,
3268 uint32_t* outTypeSpecFlags) const
3269{
3270 int cnt = 20;
3271
3272 if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 do {
3275 const ssize_t p = mTable.getResourcePackageIndex(resID);
3276 const uint32_t t = Res_GETTYPE(resID);
3277 const uint32_t e = Res_GETENTRY(resID);
3278
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003279 if (kDebugTableTheme) {
3280 ALOGI("Looking up attr 0x%08x in theme %p", resID, this);
3281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282
3283 if (p >= 0) {
3284 const package_info* const pi = mPackages[p];
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003285 if (kDebugTableTheme) {
3286 ALOGI("Found package: %p", pi);
3287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 if (pi != NULL) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003289 if (kDebugTableTheme) {
3290 ALOGI("Desired type index is %zd in avail %zu", t, Res_MAXTYPE + 1);
3291 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003292 if (t <= Res_MAXTYPE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 const type_info& ti = pi->types[t];
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003294 if (kDebugTableTheme) {
3295 ALOGI("Desired entry index is %u in avail %zu", e, ti.numEntries);
3296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 if (e < ti.numEntries) {
3298 const theme_entry& te = ti.entries[e];
Dianne Hackbornb8d81672009-11-20 14:26:42 -08003299 if (outTypeSpecFlags != NULL) {
3300 *outTypeSpecFlags |= te.typeSpecFlags;
3301 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003302 if (kDebugTableTheme) {
3303 ALOGI("Theme value: type=0x%x, data=0x%08x",
3304 te.value.dataType, te.value.data);
3305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 const uint8_t type = te.value.dataType;
3307 if (type == Res_value::TYPE_ATTRIBUTE) {
3308 if (cnt > 0) {
3309 cnt--;
3310 resID = te.value.data;
3311 continue;
3312 }
Steve Block8564c8d2012-01-05 23:22:43 +00003313 ALOGW("Too many attribute references, stopped at: 0x%08x\n", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 return BAD_INDEX;
3315 } else if (type != Res_value::TYPE_NULL) {
3316 *outValue = te.value;
3317 return te.stringBlock;
3318 }
3319 return BAD_INDEX;
3320 }
3321 }
3322 }
3323 }
3324 break;
3325
3326 } while (true);
3327
3328 return BAD_INDEX;
3329}
3330
3331ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue,
3332 ssize_t blockIndex, uint32_t* outLastRef,
Dianne Hackborn0d221012009-07-29 15:41:19 -07003333 uint32_t* inoutTypeSpecFlags, ResTable_config* inoutConfig) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334{
3335 //printf("Resolving type=0x%x\n", inOutValue->dataType);
3336 if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) {
3337 uint32_t newTypeSpecFlags;
3338 blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003339 if (kDebugTableTheme) {
3340 ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=0x%x\n",
3341 (int)blockIndex, (int)inOutValue->dataType, inOutValue->data);
3342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags;
3344 //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType);
3345 if (blockIndex < 0) {
3346 return blockIndex;
3347 }
3348 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07003349 return mTable.resolveReference(inOutValue, blockIndex, outLastRef,
3350 inoutTypeSpecFlags, inoutConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351}
3352
3353void ResTable::Theme::dumpToLog() const
3354{
Steve Block6215d3f2012-01-04 20:05:49 +00003355 ALOGI("Theme %p:\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 for (size_t i=0; i<Res_MAXPACKAGE; i++) {
3357 package_info* pi = mPackages[i];
3358 if (pi == NULL) continue;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003359
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003360 ALOGI(" Package #0x%02x:\n", (int)(i + 1));
3361 for (size_t j = 0; j <= Res_MAXTYPE; j++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 type_info& ti = pi->types[j];
3363 if (ti.numEntries == 0) continue;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003364 ALOGI(" Type #0x%02x:\n", (int)(j + 1));
3365 for (size_t k = 0; k < ti.numEntries; k++) {
3366 const theme_entry& te = ti.entries[k];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 if (te.value.dataType == Res_value::TYPE_NULL) continue;
Steve Block6215d3f2012-01-04 20:05:49 +00003368 ALOGI(" 0x%08x: t=0x%x, d=0x%08x (block=%d)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 (int)Res_MAKEID(i, j, k),
3370 te.value.dataType, (int)te.value.data, (int)te.stringBlock);
3371 }
3372 }
3373 }
3374}
3375
3376ResTable::ResTable()
Adam Lesinskide898ff2014-01-29 18:20:45 -08003377 : mError(NO_INIT), mNextPackageId(2)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378{
3379 memset(&mParams, 0, sizeof(mParams));
3380 memset(mPackageMap, 0, sizeof(mPackageMap));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003381 if (kDebugTableSuperNoisy) {
3382 ALOGI("Creating ResTable %p\n", this);
3383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384}
3385
Narayan Kamath7c4887f2014-01-27 17:32:37 +00003386ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData)
Adam Lesinskide898ff2014-01-29 18:20:45 -08003387 : mError(NO_INIT), mNextPackageId(2)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388{
3389 memset(&mParams, 0, sizeof(mParams));
3390 memset(mPackageMap, 0, sizeof(mPackageMap));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003391 addInternal(data, size, NULL, 0, cookie, copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table");
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003393 if (kDebugTableSuperNoisy) {
3394 ALOGI("Creating ResTable %p\n", this);
3395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396}
3397
3398ResTable::~ResTable()
3399{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003400 if (kDebugTableSuperNoisy) {
3401 ALOGI("Destroying ResTable in %p\n", this);
3402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 uninit();
3404}
3405
3406inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const
3407{
3408 return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1;
3409}
3410
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003411status_t ResTable::add(const void* data, size_t size, const int32_t cookie, bool copyData) {
3412 return addInternal(data, size, NULL, 0, cookie, copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413}
3414
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003415status_t ResTable::add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
3416 const int32_t cookie, bool copyData) {
3417 return addInternal(data, size, idmapData, idmapDataSize, cookie, copyData);
3418}
3419
3420status_t ResTable::add(Asset* asset, const int32_t cookie, bool copyData) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 const void* data = asset->getBuffer(true);
3422 if (data == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003423 ALOGW("Unable to get buffer of resource asset file");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 return UNKNOWN_ERROR;
3425 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003426
3427 return addInternal(data, static_cast<size_t>(asset->getLength()), NULL, 0, cookie, copyData);
3428}
3429
3430status_t ResTable::add(Asset* asset, Asset* idmapAsset, const int32_t cookie, bool copyData) {
3431 const void* data = asset->getBuffer(true);
3432 if (data == NULL) {
3433 ALOGW("Unable to get buffer of resource asset file");
3434 return UNKNOWN_ERROR;
3435 }
3436
3437 size_t idmapSize = 0;
3438 const void* idmapData = NULL;
3439 if (idmapAsset != NULL) {
3440 idmapData = idmapAsset->getBuffer(true);
3441 if (idmapData == NULL) {
3442 ALOGW("Unable to get buffer of idmap asset file");
3443 return UNKNOWN_ERROR;
3444 }
3445 idmapSize = static_cast<size_t>(idmapAsset->getLength());
3446 }
3447
3448 return addInternal(data, static_cast<size_t>(asset->getLength()),
3449 idmapData, idmapSize, cookie, copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450}
3451
Dianne Hackborn78c40512009-07-06 11:07:40 -07003452status_t ResTable::add(ResTable* src)
3453{
3454 mError = src->mError;
Mark Salyzyn00adb862014-03-19 11:00:06 -07003455
Dianne Hackborn78c40512009-07-06 11:07:40 -07003456 for (size_t i=0; i<src->mHeaders.size(); i++) {
3457 mHeaders.add(src->mHeaders[i]);
3458 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003459
Dianne Hackborn78c40512009-07-06 11:07:40 -07003460 for (size_t i=0; i<src->mPackageGroups.size(); i++) {
3461 PackageGroup* srcPg = src->mPackageGroups[i];
3462 PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id);
3463 for (size_t j=0; j<srcPg->packages.size(); j++) {
3464 pg->packages.add(srcPg->packages[j]);
3465 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003466
3467 for (size_t j = 0; j < srcPg->types.size(); j++) {
3468 if (srcPg->types[j].isEmpty()) {
3469 continue;
3470 }
3471
3472 TypeList& typeList = pg->types.editItemAt(j);
3473 typeList.appendVector(srcPg->types[j]);
3474 }
Adam Lesinski6022deb2014-08-20 14:59:19 -07003475 pg->dynamicRefTable.addMappings(srcPg->dynamicRefTable);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003476 pg->largestTypeId = max(pg->largestTypeId, srcPg->largestTypeId);
Dianne Hackborn78c40512009-07-06 11:07:40 -07003477 mPackageGroups.add(pg);
3478 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003479
Dianne Hackborn78c40512009-07-06 11:07:40 -07003480 memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap));
Mark Salyzyn00adb862014-03-19 11:00:06 -07003481
Dianne Hackborn78c40512009-07-06 11:07:40 -07003482 return mError;
3483}
3484
Adam Lesinskide898ff2014-01-29 18:20:45 -08003485status_t ResTable::addEmpty(const int32_t cookie) {
3486 Header* header = new Header(this);
3487 header->index = mHeaders.size();
3488 header->cookie = cookie;
3489 header->values.setToEmpty();
3490 header->ownedData = calloc(1, sizeof(ResTable_header));
3491
3492 ResTable_header* resHeader = (ResTable_header*) header->ownedData;
3493 resHeader->header.type = RES_TABLE_TYPE;
3494 resHeader->header.headerSize = sizeof(ResTable_header);
3495 resHeader->header.size = sizeof(ResTable_header);
3496
3497 header->header = (const ResTable_header*) resHeader;
3498 mHeaders.add(header);
Adam Lesinski961dda72014-06-09 17:10:29 -07003499 return (mError=NO_ERROR);
Adam Lesinskide898ff2014-01-29 18:20:45 -08003500}
3501
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003502status_t ResTable::addInternal(const void* data, size_t dataSize, const void* idmapData, size_t idmapDataSize,
3503 const int32_t cookie, bool copyData)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003505 if (!data) {
3506 return NO_ERROR;
3507 }
3508
Adam Lesinskif28d5052014-07-25 15:25:04 -07003509 if (dataSize < sizeof(ResTable_header)) {
3510 ALOGE("Invalid data. Size(%d) is smaller than a ResTable_header(%d).",
3511 (int) dataSize, (int) sizeof(ResTable_header));
3512 return UNKNOWN_ERROR;
3513 }
3514
Dianne Hackborn78c40512009-07-06 11:07:40 -07003515 Header* header = new Header(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 header->index = mHeaders.size();
3517 header->cookie = cookie;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003518 if (idmapData != NULL) {
3519 header->resourceIDMap = (uint32_t*) malloc(idmapDataSize);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003520 if (header->resourceIDMap == NULL) {
3521 delete header;
3522 return (mError = NO_MEMORY);
3523 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003524 memcpy(header->resourceIDMap, idmapData, idmapDataSize);
3525 header->resourceIDMapSize = idmapDataSize;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 mHeaders.add(header);
3528
3529 const bool notDeviceEndian = htods(0xf0) != 0xf0;
3530
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003531 if (kDebugLoadTableNoisy) {
3532 ALOGV("Adding resources to ResTable: data=%p, size=%zu, cookie=%d, copy=%d "
3533 "idmap=%p\n", data, dataSize, cookie, copyData, idmapData);
3534 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07003535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 if (copyData || notDeviceEndian) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003537 header->ownedData = malloc(dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 if (header->ownedData == NULL) {
3539 return (mError=NO_MEMORY);
3540 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003541 memcpy(header->ownedData, data, dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 data = header->ownedData;
3543 }
3544
3545 header->header = (const ResTable_header*)data;
3546 header->size = dtohl(header->header->header.size);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003547 if (kDebugLoadTableSuperNoisy) {
3548 ALOGI("Got size %zu, again size 0x%x, raw size 0x%x\n", header->size,
3549 dtohl(header->header->header.size), header->header->header.size);
3550 }
3551 if (kDebugLoadTableNoisy) {
3552 ALOGV("Loading ResTable @%p:\n", header->header);
3553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 if (dtohs(header->header->header.headerSize) > header->size
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003555 || header->size > dataSize) {
Steve Block8564c8d2012-01-05 23:22:43 +00003556 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is larger than data size 0x%x\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 (int)dtohs(header->header->header.headerSize),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003558 (int)header->size, (int)dataSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 return (mError=BAD_TYPE);
3560 }
3561 if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003562 ALOGW("Bad resource table: header size 0x%x or total size 0x%x is not on an integer boundary\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 (int)dtohs(header->header->header.headerSize),
3564 (int)header->size);
3565 return (mError=BAD_TYPE);
3566 }
3567 header->dataEnd = ((const uint8_t*)header->header) + header->size;
3568
3569 // Iterate through all chunks.
3570 size_t curPackage = 0;
3571
3572 const ResChunk_header* chunk =
3573 (const ResChunk_header*)(((const uint8_t*)header->header)
3574 + dtohs(header->header->header.headerSize));
3575 while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) &&
3576 ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) {
3577 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable");
3578 if (err != NO_ERROR) {
3579 return (mError=err);
3580 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003581 if (kDebugTableNoisy) {
3582 ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
3583 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
3584 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
3585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 const size_t csize = dtohl(chunk->size);
3587 const uint16_t ctype = dtohs(chunk->type);
3588 if (ctype == RES_STRING_POOL_TYPE) {
3589 if (header->values.getError() != NO_ERROR) {
3590 // Only use the first string chunk; ignore any others that
3591 // may appear.
3592 status_t err = header->values.setTo(chunk, csize);
3593 if (err != NO_ERROR) {
3594 return (mError=err);
3595 }
3596 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00003597 ALOGW("Multiple string chunks found in resource table.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 }
3599 } else if (ctype == RES_TABLE_PACKAGE_TYPE) {
3600 if (curPackage >= dtohl(header->header->packageCount)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003601 ALOGW("More package chunks were found than the %d declared in the header.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 dtohl(header->header->packageCount));
3603 return (mError=BAD_TYPE);
3604 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003605
3606 if (parsePackage((ResTable_package*)chunk, header) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 return mError;
3608 }
3609 curPackage++;
3610 } else {
Patrik Bannura443dd932014-02-12 13:38:54 +01003611 ALOGW("Unknown chunk type 0x%x in table at %p.\n",
3612 ctype,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
3614 }
3615 chunk = (const ResChunk_header*)
3616 (((const uint8_t*)chunk) + csize);
3617 }
3618
3619 if (curPackage < dtohl(header->header->packageCount)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003620 ALOGW("Fewer package chunks (%d) were found than the %d declared in the header.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 (int)curPackage, dtohl(header->header->packageCount));
3622 return (mError=BAD_TYPE);
3623 }
3624 mError = header->values.getError();
3625 if (mError != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +00003626 ALOGW("No string values found in resource table!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01003628
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003629 if (kDebugTableNoisy) {
3630 ALOGV("Returning from add with mError=%d\n", mError);
3631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 return mError;
3633}
3634
3635status_t ResTable::getError() const
3636{
3637 return mError;
3638}
3639
3640void ResTable::uninit()
3641{
3642 mError = NO_INIT;
3643 size_t N = mPackageGroups.size();
3644 for (size_t i=0; i<N; i++) {
3645 PackageGroup* g = mPackageGroups[i];
3646 delete g;
3647 }
3648 N = mHeaders.size();
3649 for (size_t i=0; i<N; i++) {
3650 Header* header = mHeaders[i];
Dianne Hackborn78c40512009-07-06 11:07:40 -07003651 if (header->owner == this) {
3652 if (header->ownedData) {
3653 free(header->ownedData);
3654 }
3655 delete header;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 }
3658
3659 mPackageGroups.clear();
3660 mHeaders.clear();
3661}
3662
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07003663bool ResTable::getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664{
3665 if (mError != NO_ERROR) {
3666 return false;
3667 }
3668
3669 const ssize_t p = getResourcePackageIndex(resID);
3670 const int t = Res_GETTYPE(resID);
3671 const int e = Res_GETENTRY(resID);
3672
3673 if (p < 0) {
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003674 if (Res_GETPACKAGE(resID)+1 == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003675 ALOGW("No package identifier when getting name for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003676 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00003677 ALOGW("No known package when getting name for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 return false;
3680 }
3681 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003682 ALOGW("No type identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 return false;
3684 }
3685
3686 const PackageGroup* const grp = mPackageGroups[p];
3687 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003688 ALOGW("Bad identifier when getting name for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 return false;
3690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003692 Entry entry;
3693 status_t err = getEntry(grp, t, e, NULL, &entry);
3694 if (err != NO_ERROR) {
3695 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 }
3697
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003698 outName->package = grp->name.string();
3699 outName->packageLen = grp->name.size();
3700 if (allowUtf8) {
3701 outName->type8 = entry.typeStr.string8(&outName->typeLen);
3702 outName->name8 = entry.keyStr.string8(&outName->nameLen);
3703 } else {
3704 outName->type8 = NULL;
3705 outName->name8 = NULL;
3706 }
3707 if (outName->type8 == NULL) {
3708 outName->type = entry.typeStr.string16(&outName->typeLen);
3709 // If we have a bad index for some reason, we should abort.
3710 if (outName->type == NULL) {
3711 return false;
3712 }
3713 }
3714 if (outName->name8 == NULL) {
3715 outName->name = entry.keyStr.string16(&outName->nameLen);
3716 // If we have a bad index for some reason, we should abort.
3717 if (outName->name == NULL) {
3718 return false;
3719 }
3720 }
3721
3722 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723}
3724
Kenny Root55fc8502010-10-28 14:47:01 -07003725ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, uint16_t density,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 uint32_t* outSpecFlags, ResTable_config* outConfig) const
3727{
3728 if (mError != NO_ERROR) {
3729 return mError;
3730 }
3731
3732 const ssize_t p = getResourcePackageIndex(resID);
3733 const int t = Res_GETTYPE(resID);
3734 const int e = Res_GETENTRY(resID);
3735
3736 if (p < 0) {
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003737 if (Res_GETPACKAGE(resID)+1 == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003738 ALOGW("No package identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003739 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00003740 ALOGW("No known package when getting value for resource number 0x%08x", resID);
Dianne Hackborn6cca1592009-09-20 12:40:03 -07003741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 return BAD_INDEX;
3743 }
3744 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003745 ALOGW("No type identifier when getting value for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 return BAD_INDEX;
3747 }
3748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 const PackageGroup* const grp = mPackageGroups[p];
3750 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003751 ALOGW("Bad identifier when getting value for resource number 0x%08x", resID);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003752 return BAD_INDEX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
Kenny Root55fc8502010-10-28 14:47:01 -07003754
3755 // Allow overriding density
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003756 ResTable_config desiredConfig = mParams;
Kenny Root55fc8502010-10-28 14:47:01 -07003757 if (density > 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003758 desiredConfig.density = density;
Kenny Root55fc8502010-10-28 14:47:01 -07003759 }
3760
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003761 Entry entry;
3762 status_t err = getEntry(grp, t, e, &desiredConfig, &entry);
3763 if (err != NO_ERROR) {
Adam Lesinskide7de472014-11-03 12:03:08 -08003764 // Only log the failure when we're not running on the host as
3765 // part of a tool. The caller will do its own logging.
3766#ifndef STATIC_ANDROIDFW_FOR_TOOLS
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003767 ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) (error %d)\n",
3768 resID, t, e, err);
Adam Lesinskide7de472014-11-03 12:03:08 -08003769#endif
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003770 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 }
3772
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003773 if ((dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) != 0) {
3774 if (!mayBeBag) {
3775 ALOGW("Requesting resource 0x%08x failed because it is complex\n", resID);
Adam Lesinskide898ff2014-01-29 18:20:45 -08003776 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003777 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 }
3779
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003780 const Res_value* value = reinterpret_cast<const Res_value*>(
3781 reinterpret_cast<const uint8_t*>(entry.entry) + entry.entry->size);
3782
3783 outValue->size = dtohs(value->size);
3784 outValue->res0 = value->res0;
3785 outValue->dataType = value->dataType;
3786 outValue->data = dtohl(value->data);
3787
3788 // The reference may be pointing to a resource in a shared library. These
3789 // references have build-time generated package IDs. These ids may not match
3790 // the actual package IDs of the corresponding packages in this ResTable.
3791 // We need to fix the package ID based on a mapping.
3792 if (grp->dynamicRefTable.lookupResourceValue(outValue) != NO_ERROR) {
3793 ALOGW("Failed to resolve referenced package: 0x%08x", outValue->data);
3794 return BAD_VALUE;
Kenny Root55fc8502010-10-28 14:47:01 -07003795 }
3796
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003797 if (kDebugTableNoisy) {
3798 size_t len;
3799 printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n",
3800 entry.package->header->index,
3801 outValue->dataType,
3802 outValue->dataType == Res_value::TYPE_STRING ?
3803 String8(entry.package->header->values.stringAt(outValue->data, &len)).string() :
3804 "",
3805 outValue->data);
3806 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003807
3808 if (outSpecFlags != NULL) {
3809 *outSpecFlags = entry.specFlags;
3810 }
3811
3812 if (outConfig != NULL) {
3813 *outConfig = entry.config;
3814 }
3815
3816 return entry.package->header->index;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817}
3818
3819ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex,
Dianne Hackborn0d221012009-07-29 15:41:19 -07003820 uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags,
3821 ResTable_config* outConfig) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822{
3823 int count=0;
Adam Lesinskide898ff2014-01-29 18:20:45 -08003824 while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE
3825 && value->data != 0 && count < 20) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 if (outLastRef) *outLastRef = value->data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 uint32_t newFlags = 0;
Kenny Root55fc8502010-10-28 14:47:01 -07003828 const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags,
Dianne Hackborn0d221012009-07-29 15:41:19 -07003829 outConfig);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003830 if (newIndex == BAD_INDEX) {
3831 return BAD_INDEX;
3832 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003833 if (kDebugTableTheme) {
3834 ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n",
3835 value->data, (int)newIndex, (int)value->dataType, value->data);
3836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex);
3838 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags;
3839 if (newIndex < 0) {
3840 // This can fail if the resource being referenced is a style...
3841 // in this case, just return the reference, and expect the
3842 // caller to deal with.
3843 return blockIndex;
3844 }
3845 blockIndex = newIndex;
3846 count++;
3847 }
3848 return blockIndex;
3849}
3850
3851const char16_t* ResTable::valueToString(
3852 const Res_value* value, size_t stringBlock,
Adam Lesinskiad2d07d2014-08-27 16:21:08 -07003853 char16_t /*tmpBuffer*/ [TMP_BUFFER_SIZE], size_t* outLen) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854{
3855 if (!value) {
3856 return NULL;
3857 }
3858 if (value->dataType == value->TYPE_STRING) {
3859 return getTableStringBlock(stringBlock)->stringAt(value->data, outLen);
3860 }
3861 // XXX do int to string conversions.
3862 return NULL;
3863}
3864
3865ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const
3866{
3867 mLock.lock();
3868 ssize_t err = getBagLocked(resID, outBag);
3869 if (err < NO_ERROR) {
3870 //printf("*** get failed! unlocking\n");
3871 mLock.unlock();
3872 }
3873 return err;
3874}
3875
Mark Salyzyn00adb862014-03-19 11:00:06 -07003876void ResTable::unlockBag(const bag_entry* /*bag*/) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877{
3878 //printf("<<< unlockBag %p\n", this);
3879 mLock.unlock();
3880}
3881
3882void ResTable::lock() const
3883{
3884 mLock.lock();
3885}
3886
3887void ResTable::unlock() const
3888{
3889 mLock.unlock();
3890}
3891
3892ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag,
3893 uint32_t* outTypeSpecFlags) const
3894{
3895 if (mError != NO_ERROR) {
3896 return mError;
3897 }
3898
3899 const ssize_t p = getResourcePackageIndex(resID);
3900 const int t = Res_GETTYPE(resID);
3901 const int e = Res_GETENTRY(resID);
3902
3903 if (p < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003904 ALOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 return BAD_INDEX;
3906 }
3907 if (t < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003908 ALOGW("No type identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 return BAD_INDEX;
3910 }
3911
3912 //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t);
3913 PackageGroup* const grp = mPackageGroups[p];
3914 if (grp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00003915 ALOGW("Bad identifier when getting bag for resource number 0x%08x", resID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 return BAD_INDEX;
3917 }
3918
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003919 const TypeList& typeConfigs = grp->types[t];
3920 if (typeConfigs.isEmpty()) {
3921 ALOGW("Type identifier 0x%x does not exist.", t+1);
3922 return BAD_INDEX;
3923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003925 const size_t NENTRY = typeConfigs[0]->entryCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 if (e >= (int)NENTRY) {
Steve Block8564c8d2012-01-05 23:22:43 +00003927 ALOGW("Entry identifier 0x%x is larger than entry count 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003928 e, (int)typeConfigs[0]->entryCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 return BAD_INDEX;
3930 }
3931
3932 // First see if we've already computed this bag...
3933 if (grp->bags) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003934 bag_set** typeSet = grp->bags->get(t);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 if (typeSet) {
3936 bag_set* set = typeSet[e];
3937 if (set) {
3938 if (set != (bag_set*)0xFFFFFFFF) {
3939 if (outTypeSpecFlags != NULL) {
3940 *outTypeSpecFlags = set->typeSpecFlags;
3941 }
3942 *outBag = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003943 if (kDebugTableSuperNoisy) {
3944 ALOGI("Found existing bag for: 0x%x\n", resID);
3945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 return set->numAttrs;
3947 }
Steve Block8564c8d2012-01-05 23:22:43 +00003948 ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 resID);
3950 return BAD_INDEX;
3951 }
3952 }
3953 }
3954
3955 // Bag not found, we need to compute it!
3956 if (!grp->bags) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003957 grp->bags = new ByteBucketArray<bag_set**>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 if (!grp->bags) return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003961 bag_set** typeSet = grp->bags->get(t);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 if (!typeSet) {
Iliyan Malchev7e1d3952012-02-17 12:15:58 -08003963 typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 if (!typeSet) return NO_MEMORY;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003965 grp->bags->set(t, typeSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967
3968 // Mark that we are currently working on this one.
3969 typeSet[e] = (bag_set*)0xFFFFFFFF;
3970
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003971 if (kDebugTableNoisy) {
3972 ALOGI("Building bag: %x\n", resID);
3973 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003974
3975 // Now collect all bag attributes
3976 Entry entry;
3977 status_t err = getEntry(grp, t, e, &mParams, &entry);
3978 if (err != NO_ERROR) {
3979 return err;
3980 }
3981
3982 const uint16_t entrySize = dtohs(entry.entry->size);
3983 const uint32_t parent = entrySize >= sizeof(ResTable_map_entry)
3984 ? dtohl(((const ResTable_map_entry*)entry.entry)->parent.ident) : 0;
3985 const uint32_t count = entrySize >= sizeof(ResTable_map_entry)
3986 ? dtohl(((const ResTable_map_entry*)entry.entry)->count) : 0;
3987
3988 size_t N = count;
3989
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003990 if (kDebugTableNoisy) {
3991 ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003992
3993 // If this map inherits from another, we need to start
3994 // with its parent's values. Otherwise start out empty.
Andreas Gampe2204f0b2014-10-21 23:04:54 -07003995 ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent);
3996 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07003997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 // This is what we are building.
3999 bag_set* set = NULL;
4000
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004001 if (parent) {
4002 uint32_t resolvedParent = parent;
Mark Salyzyn00adb862014-03-19 11:00:06 -07004003
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004004 // Bags encode a parent reference without using the standard
4005 // Res_value structure. That means we must always try to
4006 // resolve a parent reference in case it is actually a
4007 // TYPE_DYNAMIC_REFERENCE.
4008 status_t err = grp->dynamicRefTable.lookupResourceId(&resolvedParent);
4009 if (err != NO_ERROR) {
4010 ALOGE("Failed resolving bag parent id 0x%08x", parent);
4011 return UNKNOWN_ERROR;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004014 const bag_entry* parentBag;
4015 uint32_t parentTypeSpecFlags = 0;
4016 const ssize_t NP = getBagLocked(resolvedParent, &parentBag, &parentTypeSpecFlags);
4017 const size_t NT = ((NP >= 0) ? NP : 0) + N;
4018 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT);
4019 if (set == NULL) {
4020 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004022 if (NP > 0) {
4023 memcpy(set+1, parentBag, NP*sizeof(bag_entry));
4024 set->numAttrs = NP;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004025 if (kDebugTableNoisy) {
4026 ALOGI("Initialized new bag with %zd inherited attributes.\n", NP);
4027 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004028 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004029 if (kDebugTableNoisy) {
4030 ALOGI("Initialized new bag with no inherited attributes.\n");
4031 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01004032 set->numAttrs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004034 set->availAttrs = NT;
4035 set->typeSpecFlags = parentTypeSpecFlags;
4036 } else {
4037 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N);
4038 if (set == NULL) {
4039 return NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004041 set->numAttrs = 0;
4042 set->availAttrs = N;
4043 set->typeSpecFlags = 0;
4044 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004045
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004046 set->typeSpecFlags |= entry.specFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004048 // Now merge in the new attributes...
4049 size_t curOff = (reinterpret_cast<uintptr_t>(entry.entry) - reinterpret_cast<uintptr_t>(entry.type))
4050 + dtohs(entry.entry->size);
4051 const ResTable_map* map;
4052 bag_entry* entries = (bag_entry*)(set+1);
4053 size_t curEntry = 0;
4054 uint32_t pos = 0;
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004055 if (kDebugTableNoisy) {
4056 ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs);
4057 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004058 while (pos < count) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004059 if (kDebugTableNoisy) {
4060 ALOGI("Now at %p\n", (void*)curOff);
4061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004063 if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) {
4064 ALOGW("ResTable_map at %d is beyond type chunk data %d",
4065 (int)curOff, dtohl(entry.type->header.size));
4066 return BAD_TYPE;
4067 }
4068 map = (const ResTable_map*)(((const uint8_t*)entry.type) + curOff);
4069 N++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -07004071 uint32_t newName = htodl(map->name.ident);
4072 if (!Res_INTERNALID(newName)) {
4073 // Attributes don't have a resource id as the name. They specify
4074 // other data, which would be wrong to change via a lookup.
4075 if (grp->dynamicRefTable.lookupResourceId(&newName) != NO_ERROR) {
4076 ALOGE("Failed resolving ResTable_map name at %d with ident 0x%08x",
4077 (int) curOff, (int) newName);
4078 return UNKNOWN_ERROR;
4079 }
4080 }
4081
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004082 bool isInside;
4083 uint32_t oldName = 0;
4084 while ((isInside=(curEntry < set->numAttrs))
4085 && (oldName=entries[curEntry].map.name.ident) < newName) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004086 if (kDebugTableNoisy) {
4087 ALOGI("#%zu: Keeping existing attribute: 0x%08x\n",
4088 curEntry, entries[curEntry].map.name.ident);
4089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 curEntry++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004092
4093 if ((!isInside) || oldName != newName) {
4094 // This is a new attribute... figure out what to do with it.
4095 if (set->numAttrs >= set->availAttrs) {
4096 // Need to alloc more memory...
4097 const size_t newAvail = set->availAttrs+N;
4098 set = (bag_set*)realloc(set,
4099 sizeof(bag_set)
4100 + sizeof(bag_entry)*newAvail);
4101 if (set == NULL) {
4102 return NO_MEMORY;
4103 }
4104 set->availAttrs = newAvail;
4105 entries = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004106 if (kDebugTableNoisy) {
4107 ALOGI("Reallocated set %p, entries=%p, avail=%zu\n",
4108 set, entries, set->availAttrs);
4109 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004110 }
4111 if (isInside) {
4112 // Going in the middle, need to make space.
4113 memmove(entries+curEntry+1, entries+curEntry,
4114 sizeof(bag_entry)*(set->numAttrs-curEntry));
4115 set->numAttrs++;
4116 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004117 if (kDebugTableNoisy) {
4118 ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName);
4119 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004120 } else {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004121 if (kDebugTableNoisy) {
4122 ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName);
4123 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004124 }
4125
4126 bag_entry* cur = entries+curEntry;
4127
4128 cur->stringBlock = entry.package->header->index;
4129 cur->map.name.ident = newName;
4130 cur->map.value.copyFrom_dtoh(map->value);
4131 status_t err = grp->dynamicRefTable.lookupResourceValue(&cur->map.value);
4132 if (err != NO_ERROR) {
4133 ALOGE("Reference item(0x%08x) in bag could not be resolved.", cur->map.value.data);
4134 return UNKNOWN_ERROR;
4135 }
4136
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004137 if (kDebugTableNoisy) {
4138 ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n",
4139 curEntry, cur, cur->stringBlock, cur->map.name.ident,
4140 cur->map.value.dataType, cur->map.value.data);
4141 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004142
4143 // On to the next!
4144 curEntry++;
4145 pos++;
4146 const size_t size = dtohs(map->value.size);
4147 curOff += size + sizeof(*map)-sizeof(map->value);
4148 };
4149
4150 if (curEntry > set->numAttrs) {
4151 set->numAttrs = curEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153
4154 // And this is it...
4155 typeSet[e] = set;
4156 if (set) {
4157 if (outTypeSpecFlags != NULL) {
4158 *outTypeSpecFlags = set->typeSpecFlags;
4159 }
4160 *outBag = (bag_entry*)(set+1);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004161 if (kDebugTableNoisy) {
4162 ALOGI("Returning %zu attrs\n", set->numAttrs);
4163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 return set->numAttrs;
4165 }
4166 return BAD_INDEX;
4167}
4168
4169void ResTable::setParameters(const ResTable_config* params)
4170{
4171 mLock.lock();
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004172 if (kDebugTableGetEntry) {
4173 ALOGI("Setting parameters: %s\n", params->toString().string());
4174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 mParams = *params;
4176 for (size_t i=0; i<mPackageGroups.size(); i++) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004177 if (kDebugTableNoisy) {
4178 ALOGI("CLEARING BAGS FOR GROUP %zu!", i);
4179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 mPackageGroups[i]->clearBagCache();
4181 }
4182 mLock.unlock();
4183}
4184
4185void ResTable::getParameters(ResTable_config* params) const
4186{
4187 mLock.lock();
4188 *params = mParams;
4189 mLock.unlock();
4190}
4191
4192struct id_name_map {
4193 uint32_t id;
4194 size_t len;
4195 char16_t name[6];
4196};
4197
4198const static id_name_map ID_NAMES[] = {
4199 { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } },
4200 { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } },
4201 { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } },
4202 { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } },
4203 { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } },
4204 { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } },
4205 { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } },
4206 { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } },
4207 { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } },
4208 { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } },
4209};
4210
4211uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen,
4212 const char16_t* type, size_t typeLen,
4213 const char16_t* package,
4214 size_t packageLen,
4215 uint32_t* outTypeSpecFlags) const
4216{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004217 if (kDebugTableSuperNoisy) {
4218 printf("Identifier for name: error=%d\n", mError);
4219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220
4221 // Check for internal resource identifier as the very first thing, so
4222 // that we will always find them even when there are no resources.
4223 if (name[0] == '^') {
4224 const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0]));
4225 size_t len;
4226 for (int i=0; i<N; i++) {
4227 const id_name_map* m = ID_NAMES + i;
4228 len = m->len;
4229 if (len != nameLen) {
4230 continue;
4231 }
4232 for (size_t j=1; j<len; j++) {
4233 if (m->name[j] != name[j]) {
4234 goto nope;
4235 }
4236 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004237 if (outTypeSpecFlags) {
4238 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 return m->id;
4241nope:
4242 ;
4243 }
4244 if (nameLen > 7) {
4245 if (name[1] == 'i' && name[2] == 'n'
4246 && name[3] == 'd' && name[4] == 'e' && name[5] == 'x'
4247 && name[6] == '_') {
4248 int index = atoi(String8(name + 7, nameLen - 7).string());
4249 if (Res_CHECKID(index)) {
Steve Block8564c8d2012-01-05 23:22:43 +00004250 ALOGW("Array resource index: %d is too large.",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 index);
4252 return 0;
4253 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004254 if (outTypeSpecFlags) {
4255 *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC;
4256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 return Res_MAKEARRAY(index);
4258 }
4259 }
4260 return 0;
4261 }
4262
4263 if (mError != NO_ERROR) {
4264 return 0;
4265 }
4266
Dianne Hackborn426431a2011-06-09 11:29:08 -07004267 bool fakePublic = false;
4268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 // Figure out the package and type we are looking in...
4270
4271 const char16_t* packageEnd = NULL;
4272 const char16_t* typeEnd = NULL;
4273 const char16_t* const nameEnd = name+nameLen;
4274 const char16_t* p = name;
4275 while (p < nameEnd) {
4276 if (*p == ':') packageEnd = p;
4277 else if (*p == '/') typeEnd = p;
4278 p++;
4279 }
Dianne Hackborn426431a2011-06-09 11:29:08 -07004280 if (*name == '@') {
4281 name++;
4282 if (*name == '*') {
4283 fakePublic = true;
4284 name++;
4285 }
4286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 if (name >= nameEnd) {
4288 return 0;
4289 }
4290
4291 if (packageEnd) {
4292 package = name;
4293 packageLen = packageEnd-name;
4294 name = packageEnd+1;
4295 } else if (!package) {
4296 return 0;
4297 }
4298
4299 if (typeEnd) {
4300 type = name;
4301 typeLen = typeEnd-name;
4302 name = typeEnd+1;
4303 } else if (!type) {
4304 return 0;
4305 }
4306
4307 if (name >= nameEnd) {
4308 return 0;
4309 }
4310 nameLen = nameEnd-name;
4311
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004312 if (kDebugTableNoisy) {
4313 printf("Looking for identifier: type=%s, name=%s, package=%s\n",
4314 String8(type, typeLen).string(),
4315 String8(name, nameLen).string(),
4316 String8(package, packageLen).string());
4317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318
Adam Lesinski9b624c12014-11-19 17:49:26 -08004319 const String16 attr("attr");
4320 const String16 attrPrivate("^attr-private");
4321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 const size_t NG = mPackageGroups.size();
4323 for (size_t ig=0; ig<NG; ig++) {
4324 const PackageGroup* group = mPackageGroups[ig];
4325
4326 if (strzcmp16(package, packageLen,
4327 group->name.string(), group->name.size())) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004328 if (kDebugTableNoisy) {
4329 printf("Skipping package group: %s\n", String8(group->name).string());
4330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 continue;
4332 }
4333
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004334 const size_t packageCount = group->packages.size();
4335 for (size_t pi = 0; pi < packageCount; pi++) {
Adam Lesinski9b624c12014-11-19 17:49:26 -08004336 const char16_t* targetType = type;
4337 size_t targetTypeLen = typeLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338
Adam Lesinski9b624c12014-11-19 17:49:26 -08004339 do {
4340 ssize_t ti = group->packages[pi]->typeStrings.indexOfString(
4341 targetType, targetTypeLen);
4342 if (ti < 0) {
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004343 continue;
4344 }
4345
Adam Lesinski9b624c12014-11-19 17:49:26 -08004346 ti += group->packages[pi]->typeIdOffset;
Adam Lesinskie60a87f2014-10-09 11:08:04 -07004347
Adam Lesinski9b624c12014-11-19 17:49:26 -08004348 const uint32_t identifier = findEntry(group, ti, name, nameLen,
4349 outTypeSpecFlags);
4350 if (identifier != 0) {
4351 if (fakePublic && outTypeSpecFlags) {
4352 *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07004353 }
Adam Lesinski9b624c12014-11-19 17:49:26 -08004354 return identifier;
4355 }
4356 } while (strzcmp16(attr.string(), attr.size(), targetType, targetTypeLen) == 0
4357 && (targetType = attrPrivate.string())
4358 && (targetTypeLen = attrPrivate.size())
4359 );
4360 }
4361 break;
4362 }
4363 return 0;
4364}
4365
4366uint32_t ResTable::findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name,
4367 size_t nameLen, uint32_t* outTypeSpecFlags) const {
4368 const TypeList& typeList = group->types[typeIndex];
4369 const size_t typeCount = typeList.size();
4370 for (size_t i = 0; i < typeCount; i++) {
4371 const Type* t = typeList[i];
4372 const ssize_t ei = t->package->keyStrings.indexOfString(name, nameLen);
4373 if (ei < 0) {
4374 continue;
4375 }
4376
4377 const size_t configCount = t->configs.size();
4378 for (size_t j = 0; j < configCount; j++) {
4379 const TypeVariant tv(t->configs[j]);
4380 for (TypeVariant::iterator iter = tv.beginEntries();
4381 iter != tv.endEntries();
4382 iter++) {
4383 const ResTable_entry* entry = *iter;
4384 if (entry == NULL) {
4385 continue;
4386 }
4387
4388 if (dtohl(entry->key.index) == (size_t) ei) {
4389 uint32_t resId = Res_MAKEID(group->id - 1, typeIndex, iter.index());
4390 if (outTypeSpecFlags) {
4391 Entry result;
4392 if (getEntry(group, typeIndex, iter.index(), NULL, &result) != NO_ERROR) {
4393 ALOGW("Failed to find spec flags for 0x%08x", resId);
4394 return 0;
4395 }
4396 *outTypeSpecFlags = result.specFlags;
4397 }
4398 return resId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 }
4400 }
4401 }
4402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 return 0;
4404}
4405
Adam Lesinski4bf58102014-11-03 11:21:19 -08004406bool ResTable::expandResourceRef(const char16_t* refStr, size_t refLen,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 String16* outPackage,
4408 String16* outType,
4409 String16* outName,
4410 const String16* defType,
4411 const String16* defPackage,
Dianne Hackborn426431a2011-06-09 11:29:08 -07004412 const char** outErrorMsg,
4413 bool* outPublicOnly)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414{
4415 const char16_t* packageEnd = NULL;
4416 const char16_t* typeEnd = NULL;
4417 const char16_t* p = refStr;
4418 const char16_t* const end = p + refLen;
4419 while (p < end) {
4420 if (*p == ':') packageEnd = p;
4421 else if (*p == '/') {
4422 typeEnd = p;
4423 break;
4424 }
4425 p++;
4426 }
4427 p = refStr;
4428 if (*p == '@') p++;
4429
Dianne Hackborn426431a2011-06-09 11:29:08 -07004430 if (outPublicOnly != NULL) {
4431 *outPublicOnly = true;
4432 }
4433 if (*p == '*') {
4434 p++;
4435 if (outPublicOnly != NULL) {
4436 *outPublicOnly = false;
4437 }
4438 }
4439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 if (packageEnd) {
4441 *outPackage = String16(p, packageEnd-p);
4442 p = packageEnd+1;
4443 } else {
4444 if (!defPackage) {
4445 if (outErrorMsg) {
4446 *outErrorMsg = "No resource package specified";
4447 }
4448 return false;
4449 }
4450 *outPackage = *defPackage;
4451 }
4452 if (typeEnd) {
4453 *outType = String16(p, typeEnd-p);
4454 p = typeEnd+1;
4455 } else {
4456 if (!defType) {
4457 if (outErrorMsg) {
4458 *outErrorMsg = "No resource type specified";
4459 }
4460 return false;
4461 }
4462 *outType = *defType;
4463 }
4464 *outName = String16(p, end-p);
Konstantin Lopyrevddcafcb2010-06-04 14:36:49 -07004465 if(**outPackage == 0) {
4466 if(outErrorMsg) {
4467 *outErrorMsg = "Resource package cannot be an empty string";
4468 }
4469 return false;
4470 }
4471 if(**outType == 0) {
4472 if(outErrorMsg) {
4473 *outErrorMsg = "Resource type cannot be an empty string";
4474 }
4475 return false;
4476 }
4477 if(**outName == 0) {
4478 if(outErrorMsg) {
4479 *outErrorMsg = "Resource id cannot be an empty string";
4480 }
4481 return false;
4482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 return true;
4484}
4485
4486static uint32_t get_hex(char c, bool* outError)
4487{
4488 if (c >= '0' && c <= '9') {
4489 return c - '0';
4490 } else if (c >= 'a' && c <= 'f') {
4491 return c - 'a' + 0xa;
4492 } else if (c >= 'A' && c <= 'F') {
4493 return c - 'A' + 0xa;
4494 }
4495 *outError = true;
4496 return 0;
4497}
4498
4499struct unit_entry
4500{
4501 const char* name;
4502 size_t len;
4503 uint8_t type;
4504 uint32_t unit;
4505 float scale;
4506};
4507
4508static const unit_entry unitNames[] = {
4509 { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f },
4510 { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4511 { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f },
4512 { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f },
4513 { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f },
4514 { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f },
4515 { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f },
4516 { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 },
4517 { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 },
4518 { NULL, 0, 0, 0, 0 }
4519};
4520
4521static bool parse_unit(const char* str, Res_value* outValue,
4522 float* outScale, const char** outEnd)
4523{
4524 const char* end = str;
4525 while (*end != 0 && !isspace((unsigned char)*end)) {
4526 end++;
4527 }
4528 const size_t len = end-str;
4529
4530 const char* realEnd = end;
4531 while (*realEnd != 0 && isspace((unsigned char)*realEnd)) {
4532 realEnd++;
4533 }
4534 if (*realEnd != 0) {
4535 return false;
4536 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 const unit_entry* cur = unitNames;
4539 while (cur->name) {
4540 if (len == cur->len && strncmp(cur->name, str, len) == 0) {
4541 outValue->dataType = cur->type;
4542 outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT;
4543 *outScale = cur->scale;
4544 *outEnd = end;
4545 //printf("Found unit %s for %s\n", cur->name, str);
4546 return true;
4547 }
4548 cur++;
4549 }
4550
4551 return false;
4552}
4553
4554
4555bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue)
4556{
4557 while (len > 0 && isspace16(*s)) {
4558 s++;
4559 len--;
4560 }
4561
4562 if (len <= 0) {
4563 return false;
4564 }
4565
4566 size_t i = 0;
4567 int32_t val = 0;
4568 bool neg = false;
4569
4570 if (*s == '-') {
4571 neg = true;
4572 i++;
4573 }
4574
4575 if (s[i] < '0' || s[i] > '9') {
4576 return false;
4577 }
4578
4579 // Decimal or hex?
4580 if (s[i] == '0' && s[i+1] == 'x') {
4581 if (outValue)
4582 outValue->dataType = outValue->TYPE_INT_HEX;
4583 i += 2;
4584 bool error = false;
4585 while (i < len && !error) {
4586 val = (val*16) + get_hex(s[i], &error);
4587 i++;
4588 }
4589 if (error) {
4590 return false;
4591 }
4592 } else {
4593 if (outValue)
4594 outValue->dataType = outValue->TYPE_INT_DEC;
4595 while (i < len) {
4596 if (s[i] < '0' || s[i] > '9') {
4597 return false;
4598 }
4599 val = (val*10) + s[i]-'0';
4600 i++;
4601 }
4602 }
4603
4604 if (neg) val = -val;
4605
4606 while (i < len && isspace16(s[i])) {
4607 i++;
4608 }
4609
4610 if (i == len) {
4611 if (outValue)
4612 outValue->data = val;
4613 return true;
4614 }
4615
4616 return false;
4617}
4618
4619bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue)
4620{
4621 while (len > 0 && isspace16(*s)) {
4622 s++;
4623 len--;
4624 }
4625
4626 if (len <= 0) {
4627 return false;
4628 }
4629
4630 char buf[128];
4631 int i=0;
4632 while (len > 0 && *s != 0 && i < 126) {
4633 if (*s > 255) {
4634 return false;
4635 }
4636 buf[i++] = *s++;
4637 len--;
4638 }
4639
4640 if (len > 0) {
4641 return false;
4642 }
4643 if (buf[0] < '0' && buf[0] > '9' && buf[0] != '.') {
4644 return false;
4645 }
4646
4647 buf[i] = 0;
4648 const char* end;
4649 float f = strtof(buf, (char**)&end);
4650
4651 if (*end != 0 && !isspace((unsigned char)*end)) {
4652 // Might be a unit...
4653 float scale;
4654 if (parse_unit(end, outValue, &scale, &end)) {
4655 f *= scale;
4656 const bool neg = f < 0;
4657 if (neg) f = -f;
4658 uint64_t bits = (uint64_t)(f*(1<<23)+.5f);
4659 uint32_t radix;
4660 uint32_t shift;
4661 if ((bits&0x7fffff) == 0) {
4662 // Always use 23p0 if there is no fraction, just to make
4663 // things easier to read.
4664 radix = Res_value::COMPLEX_RADIX_23p0;
4665 shift = 23;
4666 } else if ((bits&0xffffffffff800000LL) == 0) {
4667 // Magnitude is zero -- can fit in 0 bits of precision.
4668 radix = Res_value::COMPLEX_RADIX_0p23;
4669 shift = 0;
4670 } else if ((bits&0xffffffff80000000LL) == 0) {
4671 // Magnitude can fit in 8 bits of precision.
4672 radix = Res_value::COMPLEX_RADIX_8p15;
4673 shift = 8;
4674 } else if ((bits&0xffffff8000000000LL) == 0) {
4675 // Magnitude can fit in 16 bits of precision.
4676 radix = Res_value::COMPLEX_RADIX_16p7;
4677 shift = 16;
4678 } else {
4679 // Magnitude needs entire range, so no fractional part.
4680 radix = Res_value::COMPLEX_RADIX_23p0;
4681 shift = 23;
4682 }
4683 int32_t mantissa = (int32_t)(
4684 (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK);
4685 if (neg) {
4686 mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK;
4687 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004688 outValue->data |=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 (radix<<Res_value::COMPLEX_RADIX_SHIFT)
4690 | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT);
4691 //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n",
4692 // f * (neg ? -1 : 1), bits, f*(1<<23),
4693 // radix, shift, outValue->data);
4694 return true;
4695 }
4696 return false;
4697 }
4698
4699 while (*end != 0 && isspace((unsigned char)*end)) {
4700 end++;
4701 }
4702
4703 if (*end == 0) {
4704 if (outValue) {
4705 outValue->dataType = outValue->TYPE_FLOAT;
4706 *(float*)(&outValue->data) = f;
4707 return true;
4708 }
4709 }
4710
4711 return false;
4712}
4713
4714bool ResTable::stringToValue(Res_value* outValue, String16* outString,
4715 const char16_t* s, size_t len,
4716 bool preserveSpaces, bool coerceType,
4717 uint32_t attrID,
4718 const String16* defType,
4719 const String16* defPackage,
4720 Accessor* accessor,
4721 void* accessorCookie,
4722 uint32_t attrType,
4723 bool enforcePrivate) const
4724{
4725 bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting();
4726 const char* errorMsg = NULL;
4727
4728 outValue->size = sizeof(Res_value);
4729 outValue->res0 = 0;
4730
4731 // First strip leading/trailing whitespace. Do this before handling
4732 // escapes, so they can be used to force whitespace into the string.
4733 if (!preserveSpaces) {
4734 while (len > 0 && isspace16(*s)) {
4735 s++;
4736 len--;
4737 }
4738 while (len > 0 && isspace16(s[len-1])) {
4739 len--;
4740 }
4741 // If the string ends with '\', then we keep the space after it.
4742 if (len > 0 && s[len-1] == '\\' && s[len] != 0) {
4743 len++;
4744 }
4745 }
4746
4747 //printf("Value for: %s\n", String8(s, len).string());
4748
4749 uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED;
4750 uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff;
4751 bool fromAccessor = false;
4752 if (attrID != 0 && !Res_INTERNALID(attrID)) {
4753 const ssize_t p = getResourcePackageIndex(attrID);
4754 const bag_entry* bag;
4755 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
4756 //printf("For attr 0x%08x got bag of %d\n", attrID, cnt);
4757 if (cnt >= 0) {
4758 while (cnt > 0) {
4759 //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data);
4760 switch (bag->map.name.ident) {
4761 case ResTable_map::ATTR_TYPE:
4762 attrType = bag->map.value.data;
4763 break;
4764 case ResTable_map::ATTR_MIN:
4765 attrMin = bag->map.value.data;
4766 break;
4767 case ResTable_map::ATTR_MAX:
4768 attrMax = bag->map.value.data;
4769 break;
4770 case ResTable_map::ATTR_L10N:
4771 l10nReq = bag->map.value.data;
4772 break;
4773 }
4774 bag++;
4775 cnt--;
4776 }
4777 unlockBag(bag);
4778 } else if (accessor && accessor->getAttributeType(attrID, &attrType)) {
4779 fromAccessor = true;
4780 if (attrType == ResTable_map::TYPE_ENUM
4781 || attrType == ResTable_map::TYPE_FLAGS
4782 || attrType == ResTable_map::TYPE_INTEGER) {
4783 accessor->getAttributeMin(attrID, &attrMin);
4784 accessor->getAttributeMax(attrID, &attrMax);
4785 }
4786 if (localizationSetting) {
4787 l10nReq = accessor->getAttributeL10N(attrID);
4788 }
4789 }
4790 }
4791
4792 const bool canStringCoerce =
4793 coerceType && (attrType&ResTable_map::TYPE_STRING) != 0;
4794
4795 if (*s == '@') {
4796 outValue->dataType = outValue->TYPE_REFERENCE;
4797
4798 // Note: we don't check attrType here because the reference can
4799 // be to any other type; we just need to count on the client making
4800 // sure the referenced type is correct.
Mark Salyzyn00adb862014-03-19 11:00:06 -07004801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 //printf("Looking up ref: %s\n", String8(s, len).string());
4803
4804 // It's a reference!
4805 if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') {
Alan Viverettef2969402014-10-29 17:09:36 -07004806 // Special case @null as undefined. This will be converted by
4807 // AssetManager to TYPE_NULL with data DATA_NULL_UNDEFINED.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 outValue->data = 0;
4809 return true;
Alan Viverettef2969402014-10-29 17:09:36 -07004810 } else if (len == 6 && s[1]=='e' && s[2]=='m' && s[3]=='p' && s[4]=='t' && s[5]=='y') {
4811 // Special case @empty as explicitly defined empty value.
4812 outValue->dataType = Res_value::TYPE_NULL;
4813 outValue->data = Res_value::DATA_NULL_EMPTY;
4814 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 } else {
4816 bool createIfNotFound = false;
4817 const char16_t* resourceRefName;
4818 int resourceNameLen;
4819 if (len > 2 && s[1] == '+') {
4820 createIfNotFound = true;
4821 resourceRefName = s + 2;
4822 resourceNameLen = len - 2;
4823 } else if (len > 2 && s[1] == '*') {
4824 enforcePrivate = false;
4825 resourceRefName = s + 2;
4826 resourceNameLen = len - 2;
4827 } else {
4828 createIfNotFound = false;
4829 resourceRefName = s + 1;
4830 resourceNameLen = len - 1;
4831 }
4832 String16 package, type, name;
4833 if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name,
4834 defType, defPackage, &errorMsg)) {
4835 if (accessor != NULL) {
4836 accessor->reportError(accessorCookie, errorMsg);
4837 }
4838 return false;
4839 }
4840
4841 uint32_t specFlags = 0;
4842 uint32_t rid = identifierForName(name.string(), name.size(), type.string(),
4843 type.size(), package.string(), package.size(), &specFlags);
4844 if (rid != 0) {
4845 if (enforcePrivate) {
Adam Lesinski833f3cc2014-06-18 15:06:01 -07004846 if (accessor == NULL || accessor->getAssetsPackage() != package) {
4847 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
4848 if (accessor != NULL) {
4849 accessor->reportError(accessorCookie, "Resource is not public.");
4850 }
4851 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 }
4854 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08004855
4856 if (accessor) {
4857 rid = Res_MAKEID(
4858 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
4859 Res_GETTYPE(rid), Res_GETENTRY(rid));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004860 if (kDebugTableNoisy) {
4861 ALOGI("Incl %s:%s/%s: 0x%08x\n",
4862 String8(package).string(), String8(type).string(),
4863 String8(name).string(), rid);
4864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08004866
4867 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
4868 if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) {
4869 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
4870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 outValue->data = rid;
4872 return true;
4873 }
4874
4875 if (accessor) {
4876 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name,
4877 createIfNotFound);
4878 if (rid != 0) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07004879 if (kDebugTableNoisy) {
4880 ALOGI("Pckg %s:%s/%s: 0x%08x\n",
4881 String8(package).string(), String8(type).string(),
4882 String8(name).string(), rid);
4883 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08004884 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
4885 if (packageId == 0x00) {
4886 outValue->data = rid;
4887 outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
4888 return true;
4889 } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) {
4890 // We accept packageId's generated as 0x01 in order to support
4891 // building the android system resources
4892 outValue->data = rid;
4893 return true;
4894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 }
4896 }
4897 }
4898
4899 if (accessor != NULL) {
4900 accessor->reportError(accessorCookie, "No resource found that matches the given name");
4901 }
4902 return false;
4903 }
4904
4905 // if we got to here, and localization is required and it's not a reference,
4906 // complain and bail.
4907 if (l10nReq == ResTable_map::L10N_SUGGESTED) {
4908 if (localizationSetting) {
4909 if (accessor != NULL) {
4910 accessor->reportError(accessorCookie, "This attribute must be localized.");
4911 }
4912 }
4913 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07004914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 if (*s == '#') {
4916 // It's a color! Convert to an integer of the form 0xaarrggbb.
4917 uint32_t color = 0;
4918 bool error = false;
4919 if (len == 4) {
4920 outValue->dataType = outValue->TYPE_INT_COLOR_RGB4;
4921 color |= 0xFF000000;
4922 color |= get_hex(s[1], &error) << 20;
4923 color |= get_hex(s[1], &error) << 16;
4924 color |= get_hex(s[2], &error) << 12;
4925 color |= get_hex(s[2], &error) << 8;
4926 color |= get_hex(s[3], &error) << 4;
4927 color |= get_hex(s[3], &error);
4928 } else if (len == 5) {
4929 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4;
4930 color |= get_hex(s[1], &error) << 28;
4931 color |= get_hex(s[1], &error) << 24;
4932 color |= get_hex(s[2], &error) << 20;
4933 color |= get_hex(s[2], &error) << 16;
4934 color |= get_hex(s[3], &error) << 12;
4935 color |= get_hex(s[3], &error) << 8;
4936 color |= get_hex(s[4], &error) << 4;
4937 color |= get_hex(s[4], &error);
4938 } else if (len == 7) {
4939 outValue->dataType = outValue->TYPE_INT_COLOR_RGB8;
4940 color |= 0xFF000000;
4941 color |= get_hex(s[1], &error) << 20;
4942 color |= get_hex(s[2], &error) << 16;
4943 color |= get_hex(s[3], &error) << 12;
4944 color |= get_hex(s[4], &error) << 8;
4945 color |= get_hex(s[5], &error) << 4;
4946 color |= get_hex(s[6], &error);
4947 } else if (len == 9) {
4948 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8;
4949 color |= get_hex(s[1], &error) << 28;
4950 color |= get_hex(s[2], &error) << 24;
4951 color |= get_hex(s[3], &error) << 20;
4952 color |= get_hex(s[4], &error) << 16;
4953 color |= get_hex(s[5], &error) << 12;
4954 color |= get_hex(s[6], &error) << 8;
4955 color |= get_hex(s[7], &error) << 4;
4956 color |= get_hex(s[8], &error);
4957 } else {
4958 error = true;
4959 }
4960 if (!error) {
4961 if ((attrType&ResTable_map::TYPE_COLOR) == 0) {
4962 if (!canStringCoerce) {
4963 if (accessor != NULL) {
4964 accessor->reportError(accessorCookie,
4965 "Color types not allowed");
4966 }
4967 return false;
4968 }
4969 } else {
4970 outValue->data = color;
4971 //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color);
4972 return true;
4973 }
4974 } else {
4975 if ((attrType&ResTable_map::TYPE_COLOR) != 0) {
4976 if (accessor != NULL) {
4977 accessor->reportError(accessorCookie, "Color value not valid --"
4978 " must be #rgb, #argb, #rrggbb, or #aarrggbb");
4979 }
4980 #if 0
4981 fprintf(stderr, "%s: Color ID %s value %s is not valid\n",
4982 "Resource File", //(const char*)in->getPrintableSource(),
4983 String8(*curTag).string(),
4984 String8(s, len).string());
4985 #endif
4986 return false;
4987 }
4988 }
4989 }
4990
4991 if (*s == '?') {
4992 outValue->dataType = outValue->TYPE_ATTRIBUTE;
4993
4994 // Note: we don't check attrType here because the reference can
4995 // be to any other type; we just need to count on the client making
4996 // sure the referenced type is correct.
4997
4998 //printf("Looking up attr: %s\n", String8(s, len).string());
4999
5000 static const String16 attr16("attr");
5001 String16 package, type, name;
5002 if (!expandResourceRef(s+1, len-1, &package, &type, &name,
5003 &attr16, defPackage, &errorMsg)) {
5004 if (accessor != NULL) {
5005 accessor->reportError(accessorCookie, errorMsg);
5006 }
5007 return false;
5008 }
5009
5010 //printf("Pkg: %s, Type: %s, Name: %s\n",
5011 // String8(package).string(), String8(type).string(),
5012 // String8(name).string());
5013 uint32_t specFlags = 0;
Mark Salyzyn00adb862014-03-19 11:00:06 -07005014 uint32_t rid =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 identifierForName(name.string(), name.size(),
5016 type.string(), type.size(),
5017 package.string(), package.size(), &specFlags);
5018 if (rid != 0) {
5019 if (enforcePrivate) {
5020 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) {
5021 if (accessor != NULL) {
5022 accessor->reportError(accessorCookie, "Attribute is not public.");
5023 }
5024 return false;
5025 }
5026 }
5027 if (!accessor) {
5028 outValue->data = rid;
5029 return true;
5030 }
5031 rid = Res_MAKEID(
5032 accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
5033 Res_GETTYPE(rid), Res_GETENTRY(rid));
5034 //printf("Incl %s:%s/%s: 0x%08x\n",
5035 // String8(package).string(), String8(type).string(),
5036 // String8(name).string(), rid);
5037 outValue->data = rid;
5038 return true;
5039 }
5040
5041 if (accessor) {
5042 uint32_t rid = accessor->getCustomResource(package, type, name);
5043 if (rid != 0) {
5044 //printf("Mine %s:%s/%s: 0x%08x\n",
5045 // String8(package).string(), String8(type).string(),
5046 // String8(name).string(), rid);
5047 outValue->data = rid;
5048 return true;
5049 }
5050 }
5051
5052 if (accessor != NULL) {
5053 accessor->reportError(accessorCookie, "No resource found that matches the given name");
5054 }
5055 return false;
5056 }
5057
5058 if (stringToInt(s, len, outValue)) {
5059 if ((attrType&ResTable_map::TYPE_INTEGER) == 0) {
5060 // If this type does not allow integers, but does allow floats,
5061 // fall through on this error case because the float type should
5062 // be able to accept any integer value.
5063 if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) {
5064 if (accessor != NULL) {
5065 accessor->reportError(accessorCookie, "Integer types not allowed");
5066 }
5067 return false;
5068 }
5069 } else {
5070 if (((int32_t)outValue->data) < ((int32_t)attrMin)
5071 || ((int32_t)outValue->data) > ((int32_t)attrMax)) {
5072 if (accessor != NULL) {
5073 accessor->reportError(accessorCookie, "Integer value out of range");
5074 }
5075 return false;
5076 }
5077 return true;
5078 }
5079 }
5080
5081 if (stringToFloat(s, len, outValue)) {
5082 if (outValue->dataType == Res_value::TYPE_DIMENSION) {
5083 if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) {
5084 return true;
5085 }
5086 if (!canStringCoerce) {
5087 if (accessor != NULL) {
5088 accessor->reportError(accessorCookie, "Dimension types not allowed");
5089 }
5090 return false;
5091 }
5092 } else if (outValue->dataType == Res_value::TYPE_FRACTION) {
5093 if ((attrType&ResTable_map::TYPE_FRACTION) != 0) {
5094 return true;
5095 }
5096 if (!canStringCoerce) {
5097 if (accessor != NULL) {
5098 accessor->reportError(accessorCookie, "Fraction types not allowed");
5099 }
5100 return false;
5101 }
5102 } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) {
5103 if (!canStringCoerce) {
5104 if (accessor != NULL) {
5105 accessor->reportError(accessorCookie, "Float types not allowed");
5106 }
5107 return false;
5108 }
5109 } else {
5110 return true;
5111 }
5112 }
5113
5114 if (len == 4) {
5115 if ((s[0] == 't' || s[0] == 'T') &&
5116 (s[1] == 'r' || s[1] == 'R') &&
5117 (s[2] == 'u' || s[2] == 'U') &&
5118 (s[3] == 'e' || s[3] == 'E')) {
5119 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5120 if (!canStringCoerce) {
5121 if (accessor != NULL) {
5122 accessor->reportError(accessorCookie, "Boolean types not allowed");
5123 }
5124 return false;
5125 }
5126 } else {
5127 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5128 outValue->data = (uint32_t)-1;
5129 return true;
5130 }
5131 }
5132 }
5133
5134 if (len == 5) {
5135 if ((s[0] == 'f' || s[0] == 'F') &&
5136 (s[1] == 'a' || s[1] == 'A') &&
5137 (s[2] == 'l' || s[2] == 'L') &&
5138 (s[3] == 's' || s[3] == 'S') &&
5139 (s[4] == 'e' || s[4] == 'E')) {
5140 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) {
5141 if (!canStringCoerce) {
5142 if (accessor != NULL) {
5143 accessor->reportError(accessorCookie, "Boolean types not allowed");
5144 }
5145 return false;
5146 }
5147 } else {
5148 outValue->dataType = outValue->TYPE_INT_BOOLEAN;
5149 outValue->data = 0;
5150 return true;
5151 }
5152 }
5153 }
5154
5155 if ((attrType&ResTable_map::TYPE_ENUM) != 0) {
5156 const ssize_t p = getResourcePackageIndex(attrID);
5157 const bag_entry* bag;
5158 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5159 //printf("Got %d for enum\n", cnt);
5160 if (cnt >= 0) {
5161 resource_name rname;
5162 while (cnt > 0) {
5163 if (!Res_INTERNALID(bag->map.name.ident)) {
5164 //printf("Trying attr #%08x\n", bag->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005165 if (getResourceName(bag->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 #if 0
5167 printf("Matching %s against %s (0x%08x)\n",
5168 String8(s, len).string(),
5169 String8(rname.name, rname.nameLen).string(),
5170 bag->map.name.ident);
5171 #endif
5172 if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) {
5173 outValue->dataType = bag->map.value.dataType;
5174 outValue->data = bag->map.value.data;
5175 unlockBag(bag);
5176 return true;
5177 }
5178 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181 bag++;
5182 cnt--;
5183 }
5184 unlockBag(bag);
5185 }
5186
5187 if (fromAccessor) {
5188 if (accessor->getAttributeEnum(attrID, s, len, outValue)) {
5189 return true;
5190 }
5191 }
5192 }
5193
5194 if ((attrType&ResTable_map::TYPE_FLAGS) != 0) {
5195 const ssize_t p = getResourcePackageIndex(attrID);
5196 const bag_entry* bag;
5197 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1;
5198 //printf("Got %d for flags\n", cnt);
5199 if (cnt >= 0) {
5200 bool failed = false;
5201 resource_name rname;
5202 outValue->dataType = Res_value::TYPE_INT_HEX;
5203 outValue->data = 0;
5204 const char16_t* end = s + len;
5205 const char16_t* pos = s;
5206 while (pos < end && !failed) {
5207 const char16_t* start = pos;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005208 pos++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 while (pos < end && *pos != '|') {
5210 pos++;
5211 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08005212 //printf("Looking for: %s\n", String8(start, pos-start).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 const bag_entry* bagi = bag;
The Android Open Source Project4df24232009-03-05 14:34:35 -08005214 ssize_t i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 for (i=0; i<cnt; i++, bagi++) {
5216 if (!Res_INTERNALID(bagi->map.name.ident)) {
5217 //printf("Trying attr #%08x\n", bagi->map.name.ident);
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07005218 if (getResourceName(bagi->map.name.ident, false, &rname)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 #if 0
5220 printf("Matching %s against %s (0x%08x)\n",
5221 String8(start,pos-start).string(),
5222 String8(rname.name, rname.nameLen).string(),
5223 bagi->map.name.ident);
5224 #endif
5225 if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) {
5226 outValue->data |= bagi->map.value.data;
5227 break;
5228 }
5229 }
5230 }
5231 }
5232 if (i >= cnt) {
5233 // Didn't find this flag identifier.
5234 failed = true;
5235 }
5236 if (pos < end) {
5237 pos++;
5238 }
5239 }
5240 unlockBag(bag);
5241 if (!failed) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005242 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 return true;
5244 }
5245 }
5246
5247
5248 if (fromAccessor) {
5249 if (accessor->getAttributeFlags(attrID, s, len, outValue)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08005250 //printf("Final flag value: 0x%lx\n", outValue->data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 return true;
5252 }
5253 }
5254 }
5255
5256 if ((attrType&ResTable_map::TYPE_STRING) == 0) {
5257 if (accessor != NULL) {
5258 accessor->reportError(accessorCookie, "String types not allowed");
5259 }
5260 return false;
5261 }
5262
5263 // Generic string handling...
5264 outValue->dataType = outValue->TYPE_STRING;
5265 if (outString) {
5266 bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg);
5267 if (accessor != NULL) {
5268 accessor->reportError(accessorCookie, errorMsg);
5269 }
5270 return failed;
5271 }
5272
5273 return true;
5274}
5275
5276bool ResTable::collectString(String16* outString,
5277 const char16_t* s, size_t len,
5278 bool preserveSpaces,
5279 const char** outErrorMsg,
5280 bool append)
5281{
5282 String16 tmp;
5283
5284 char quoted = 0;
5285 const char16_t* p = s;
5286 while (p < (s+len)) {
5287 while (p < (s+len)) {
5288 const char16_t c = *p;
5289 if (c == '\\') {
5290 break;
5291 }
5292 if (!preserveSpaces) {
5293 if (quoted == 0 && isspace16(c)
5294 && (c != ' ' || isspace16(*(p+1)))) {
5295 break;
5296 }
5297 if (c == '"' && (quoted == 0 || quoted == '"')) {
5298 break;
5299 }
5300 if (c == '\'' && (quoted == 0 || quoted == '\'')) {
Eric Fischerc87d2522009-09-01 15:20:30 -07005301 /*
5302 * In practice, when people write ' instead of \'
5303 * in a string, they are doing it by accident
5304 * instead of really meaning to use ' as a quoting
5305 * character. Warn them so they don't lose it.
5306 */
5307 if (outErrorMsg) {
5308 *outErrorMsg = "Apostrophe not preceded by \\";
5309 }
5310 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 }
5312 }
5313 p++;
5314 }
5315 if (p < (s+len)) {
5316 if (p > s) {
5317 tmp.append(String16(s, p-s));
5318 }
5319 if (!preserveSpaces && (*p == '"' || *p == '\'')) {
5320 if (quoted == 0) {
5321 quoted = *p;
5322 } else {
5323 quoted = 0;
5324 }
5325 p++;
5326 } else if (!preserveSpaces && isspace16(*p)) {
5327 // Space outside of a quote -- consume all spaces and
5328 // leave a single plain space char.
5329 tmp.append(String16(" "));
5330 p++;
5331 while (p < (s+len) && isspace16(*p)) {
5332 p++;
5333 }
5334 } else if (*p == '\\') {
5335 p++;
5336 if (p < (s+len)) {
5337 switch (*p) {
5338 case 't':
5339 tmp.append(String16("\t"));
5340 break;
5341 case 'n':
5342 tmp.append(String16("\n"));
5343 break;
5344 case '#':
5345 tmp.append(String16("#"));
5346 break;
5347 case '@':
5348 tmp.append(String16("@"));
5349 break;
5350 case '?':
5351 tmp.append(String16("?"));
5352 break;
5353 case '"':
5354 tmp.append(String16("\""));
5355 break;
5356 case '\'':
5357 tmp.append(String16("'"));
5358 break;
5359 case '\\':
5360 tmp.append(String16("\\"));
5361 break;
5362 case 'u':
5363 {
5364 char16_t chr = 0;
5365 int i = 0;
5366 while (i < 4 && p[1] != 0) {
5367 p++;
5368 i++;
5369 int c;
5370 if (*p >= '0' && *p <= '9') {
5371 c = *p - '0';
5372 } else if (*p >= 'a' && *p <= 'f') {
5373 c = *p - 'a' + 10;
5374 } else if (*p >= 'A' && *p <= 'F') {
5375 c = *p - 'A' + 10;
5376 } else {
5377 if (outErrorMsg) {
5378 *outErrorMsg = "Bad character in \\u unicode escape sequence";
5379 }
5380 return false;
5381 }
5382 chr = (chr<<4) | c;
5383 }
5384 tmp.append(String16(&chr, 1));
5385 } break;
5386 default:
5387 // ignore unknown escape chars.
5388 break;
5389 }
5390 p++;
5391 }
5392 }
5393 len -= (p-s);
5394 s = p;
5395 }
5396 }
5397
5398 if (tmp.size() != 0) {
5399 if (len > 0) {
5400 tmp.append(String16(s, len));
5401 }
5402 if (append) {
5403 outString->append(tmp);
5404 } else {
5405 outString->setTo(tmp);
5406 }
5407 } else {
5408 if (append) {
5409 outString->append(String16(s, len));
5410 } else {
5411 outString->setTo(s, len);
5412 }
5413 }
5414
5415 return true;
5416}
5417
5418size_t ResTable::getBasePackageCount() const
5419{
5420 if (mError != NO_ERROR) {
5421 return 0;
5422 }
5423 return mPackageGroups.size();
5424}
5425
Adam Lesinskide898ff2014-01-29 18:20:45 -08005426const String16 ResTable::getBasePackageName(size_t idx) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427{
5428 if (mError != NO_ERROR) {
Adam Lesinskide898ff2014-01-29 18:20:45 -08005429 return String16();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 }
5431 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5432 "Requested package index %d past package count %d",
5433 (int)idx, (int)mPackageGroups.size());
Adam Lesinskide898ff2014-01-29 18:20:45 -08005434 return mPackageGroups[idx]->name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435}
5436
5437uint32_t ResTable::getBasePackageId(size_t idx) const
5438{
5439 if (mError != NO_ERROR) {
5440 return 0;
5441 }
5442 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5443 "Requested package index %d past package count %d",
5444 (int)idx, (int)mPackageGroups.size());
5445 return mPackageGroups[idx]->id;
5446}
5447
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005448uint32_t ResTable::getLastTypeIdForPackage(size_t idx) const
5449{
5450 if (mError != NO_ERROR) {
5451 return 0;
5452 }
5453 LOG_FATAL_IF(idx >= mPackageGroups.size(),
5454 "Requested package index %d past package count %d",
5455 (int)idx, (int)mPackageGroups.size());
5456 const PackageGroup* const group = mPackageGroups[idx];
5457 return group->largestTypeId;
5458}
5459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460size_t ResTable::getTableCount() const
5461{
5462 return mHeaders.size();
5463}
5464
5465const ResStringPool* ResTable::getTableStringBlock(size_t index) const
5466{
5467 return &mHeaders[index]->values;
5468}
5469
Narayan Kamath7c4887f2014-01-27 17:32:37 +00005470int32_t ResTable::getTableCookie(size_t index) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471{
5472 return mHeaders[index]->cookie;
5473}
5474
Adam Lesinskide898ff2014-01-29 18:20:45 -08005475const DynamicRefTable* ResTable::getDynamicRefTableForCookie(int32_t cookie) const
5476{
5477 const size_t N = mPackageGroups.size();
5478 for (size_t i = 0; i < N; i++) {
5479 const PackageGroup* pg = mPackageGroups[i];
5480 size_t M = pg->packages.size();
5481 for (size_t j = 0; j < M; j++) {
5482 if (pg->packages[j]->header->cookie == cookie) {
5483 return &pg->dynamicRefTable;
5484 }
5485 }
5486 }
5487 return NULL;
5488}
5489
Adam Lesinski42eea272015-01-15 17:01:39 -08005490void ResTable::getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491{
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005492 const size_t packageCount = mPackageGroups.size();
5493 for (size_t i = 0; i < packageCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 const PackageGroup* packageGroup = mPackageGroups[i];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005495 const size_t typeCount = packageGroup->types.size();
5496 for (size_t j = 0; j < typeCount; j++) {
5497 const TypeList& typeList = packageGroup->types[j];
5498 const size_t numTypes = typeList.size();
5499 for (size_t k = 0; k < numTypes; k++) {
5500 const Type* type = typeList[k];
Adam Lesinski42eea272015-01-15 17:01:39 -08005501 const ResStringPool& typeStrings = type->package->typeStrings;
5502 if (ignoreMipmap && typeStrings.string8ObjectAt(
5503 type->typeSpec->id - 1) == "mipmap") {
5504 continue;
5505 }
5506
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005507 const size_t numConfigs = type->configs.size();
5508 for (size_t m = 0; m < numConfigs; m++) {
5509 const ResTable_type* config = type->configs[m];
Narayan Kamath788fa412014-01-21 15:32:36 +00005510 ResTable_config cfg;
5511 memset(&cfg, 0, sizeof(ResTable_config));
5512 cfg.copyFromDtoH(config->config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 // only insert unique
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005514 const size_t N = configs->size();
5515 size_t n;
5516 for (n = 0; n < N; n++) {
5517 if (0 == (*configs)[n].compare(cfg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 break;
5519 }
5520 }
5521 // if we didn't find it
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005522 if (n == N) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005523 configs->add(cfg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 }
5525 }
5526 }
5527 }
5528 }
5529}
5530
5531void ResTable::getLocales(Vector<String8>* locales) const
5532{
5533 Vector<ResTable_config> configs;
Steve Block71f2cf12011-10-20 11:56:00 +01005534 ALOGV("calling getConfigurations");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 getConfigurations(&configs);
Steve Block71f2cf12011-10-20 11:56:00 +01005536 ALOGV("called getConfigurations size=%d", (int)configs.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 const size_t I = configs.size();
Narayan Kamath48620f12014-01-20 13:57:11 +00005538
5539 char locale[RESTABLE_MAX_LOCALE_LEN];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 for (size_t i=0; i<I; i++) {
Narayan Kamath788fa412014-01-21 15:32:36 +00005541 configs[i].getBcp47Locale(locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 const size_t J = locales->size();
5543 size_t j;
5544 for (j=0; j<J; j++) {
5545 if (0 == strcmp(locale, (*locales)[j].string())) {
5546 break;
5547 }
5548 }
5549 if (j == J) {
5550 locales->add(String8(locale));
5551 }
5552 }
5553}
5554
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005555StringPoolRef::StringPoolRef(const ResStringPool* pool, uint32_t index)
5556 : mPool(pool), mIndex(index) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005558StringPoolRef::StringPoolRef()
5559 : mPool(NULL), mIndex(0) {}
5560
5561const char* StringPoolRef::string8(size_t* outLen) const {
5562 if (mPool != NULL) {
5563 return mPool->string8At(mIndex, outLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005565 if (outLen != NULL) {
5566 *outLen = 0;
5567 }
5568 return NULL;
5569}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005571const char16_t* StringPoolRef::string16(size_t* outLen) const {
5572 if (mPool != NULL) {
5573 return mPool->stringAt(mIndex, outLen);
5574 }
5575 if (outLen != NULL) {
5576 *outLen = 0;
5577 }
5578 return NULL;
5579}
5580
Adam Lesinski82a2dd82014-09-17 18:34:15 -07005581bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const {
5582 if (mError != NO_ERROR) {
5583 return false;
5584 }
5585
5586 const ssize_t p = getResourcePackageIndex(resID);
5587 const int t = Res_GETTYPE(resID);
5588 const int e = Res_GETENTRY(resID);
5589
5590 if (p < 0) {
5591 if (Res_GETPACKAGE(resID)+1 == 0) {
5592 ALOGW("No package identifier when getting flags for resource number 0x%08x", resID);
5593 } else {
5594 ALOGW("No known package when getting flags for resource number 0x%08x", resID);
5595 }
5596 return false;
5597 }
5598 if (t < 0) {
5599 ALOGW("No type identifier when getting flags for resource number 0x%08x", resID);
5600 return false;
5601 }
5602
5603 const PackageGroup* const grp = mPackageGroups[p];
5604 if (grp == NULL) {
5605 ALOGW("Bad identifier when getting flags for resource number 0x%08x", resID);
5606 return false;
5607 }
5608
5609 Entry entry;
5610 status_t err = getEntry(grp, t, e, NULL, &entry);
5611 if (err != NO_ERROR) {
5612 return false;
5613 }
5614
5615 *outFlags = entry.specFlags;
5616 return true;
5617}
5618
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005619status_t ResTable::getEntry(
5620 const PackageGroup* packageGroup, int typeIndex, int entryIndex,
5621 const ResTable_config* config,
5622 Entry* outEntry) const
5623{
5624 const TypeList& typeList = packageGroup->types[typeIndex];
5625 if (typeList.isEmpty()) {
5626 ALOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 return BAD_TYPE;
5628 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005629
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005630 const ResTable_type* bestType = NULL;
5631 uint32_t bestOffset = ResTable_type::NO_ENTRY;
5632 const Package* bestPackage = NULL;
5633 uint32_t specFlags = 0;
5634 uint8_t actualTypeIndex = typeIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 ResTable_config bestConfig;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005636 memset(&bestConfig, 0, sizeof(bestConfig));
Mark Salyzyn00adb862014-03-19 11:00:06 -07005637
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005638 // Iterate over the Types of each package.
5639 const size_t typeCount = typeList.size();
5640 for (size_t i = 0; i < typeCount; i++) {
5641 const Type* const typeSpec = typeList[i];
Mark Salyzyn00adb862014-03-19 11:00:06 -07005642
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005643 int realEntryIndex = entryIndex;
5644 int realTypeIndex = typeIndex;
5645 bool currentTypeIsOverlay = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005647 // Runtime overlay packages provide a mapping of app resource
5648 // ID to package resource ID.
5649 if (typeSpec->idmapEntries.hasEntries()) {
5650 uint16_t overlayEntryIndex;
5651 if (typeSpec->idmapEntries.lookup(entryIndex, &overlayEntryIndex) != NO_ERROR) {
5652 // No such mapping exists
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 continue;
5654 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005655 realEntryIndex = overlayEntryIndex;
5656 realTypeIndex = typeSpec->idmapEntries.overlayTypeId() - 1;
5657 currentTypeIsOverlay = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005659
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005660 if (static_cast<size_t>(realEntryIndex) >= typeSpec->entryCount) {
5661 ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)",
5662 Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex),
5663 entryIndex, static_cast<int>(typeSpec->entryCount));
5664 // We should normally abort here, but some legacy apps declare
5665 // resources in the 'android' package (old bug in AAPT).
5666 continue;
5667 }
5668
5669 // Aggregate all the flags for each package that defines this entry.
5670 if (typeSpec->typeSpecFlags != NULL) {
5671 specFlags |= dtohl(typeSpec->typeSpecFlags[realEntryIndex]);
5672 } else {
5673 specFlags = -1;
5674 }
5675
5676 const size_t numConfigs = typeSpec->configs.size();
5677 for (size_t c = 0; c < numConfigs; c++) {
5678 const ResTable_type* const thisType = typeSpec->configs[c];
5679 if (thisType == NULL) {
5680 continue;
5681 }
5682
5683 ResTable_config thisConfig;
5684 thisConfig.copyFromDtoH(thisType->config);
5685
5686 // Check to make sure this one is valid for the current parameters.
5687 if (config != NULL && !thisConfig.match(*config)) {
5688 continue;
5689 }
5690
5691 // Check if there is the desired entry in this type.
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005692 const uint32_t* const eindex = reinterpret_cast<const uint32_t*>(
5693 reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize));
5694
5695 uint32_t thisOffset = dtohl(eindex[realEntryIndex]);
5696 if (thisOffset == ResTable_type::NO_ENTRY) {
5697 // There is no entry for this index and configuration.
5698 continue;
5699 }
5700
5701 if (bestType != NULL) {
5702 // Check if this one is less specific than the last found. If so,
5703 // we will skip it. We check starting with things we most care
5704 // about to those we least care about.
5705 if (!thisConfig.isBetterThan(bestConfig, config)) {
5706 if (!currentTypeIsOverlay || thisConfig.compare(bestConfig) != 0) {
5707 continue;
5708 }
5709 }
5710 }
5711
5712 bestType = thisType;
5713 bestOffset = thisOffset;
5714 bestConfig = thisConfig;
5715 bestPackage = typeSpec->package;
5716 actualTypeIndex = realTypeIndex;
5717
5718 // If no config was specified, any type will do, so skip
5719 if (config == NULL) {
5720 break;
5721 }
5722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005724
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005725 if (bestType == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 return BAD_INDEX;
5727 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005728
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005729 bestOffset += dtohl(bestType->entriesStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005731 if (bestOffset > (dtohl(bestType->header.size)-sizeof(ResTable_entry))) {
Steve Block8564c8d2012-01-05 23:22:43 +00005732 ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005733 bestOffset, dtohl(bestType->header.size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 return BAD_TYPE;
5735 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005736 if ((bestOffset & 0x3) != 0) {
5737 ALOGW("ResTable_entry at 0x%x is not on an integer boundary", bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 return BAD_TYPE;
5739 }
5740
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005741 const ResTable_entry* const entry = reinterpret_cast<const ResTable_entry*>(
5742 reinterpret_cast<const uint8_t*>(bestType) + bestOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 if (dtohs(entry->size) < sizeof(*entry)) {
Steve Block8564c8d2012-01-05 23:22:43 +00005744 ALOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 return BAD_TYPE;
5746 }
5747
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005748 if (outEntry != NULL) {
5749 outEntry->entry = entry;
5750 outEntry->config = bestConfig;
5751 outEntry->type = bestType;
5752 outEntry->specFlags = specFlags;
5753 outEntry->package = bestPackage;
5754 outEntry->typeStr = StringPoolRef(&bestPackage->typeStrings, actualTypeIndex - bestPackage->typeIdOffset);
5755 outEntry->keyStr = StringPoolRef(&bestPackage->keyStrings, dtohl(entry->key.index));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005757 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758}
5759
5760status_t ResTable::parsePackage(const ResTable_package* const pkg,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005761 const Header* const header)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762{
5763 const uint8_t* base = (const uint8_t*)pkg;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005764 status_t err = validate_chunk(&pkg->header, sizeof(*pkg) - sizeof(pkg->typeIdOffset),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 header->dataEnd, "ResTable_package");
5766 if (err != NO_ERROR) {
5767 return (mError=err);
5768 }
5769
Patrik Bannura443dd932014-02-12 13:38:54 +01005770 const uint32_t pkgSize = dtohl(pkg->header.size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771
5772 if (dtohl(pkg->typeStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005773 ALOGW("ResTable_package type strings at 0x%x are past chunk size 0x%x.",
5774 dtohl(pkg->typeStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 return (mError=BAD_TYPE);
5776 }
5777 if ((dtohl(pkg->typeStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005778 ALOGW("ResTable_package type strings at 0x%x is not on an integer boundary.",
5779 dtohl(pkg->typeStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 return (mError=BAD_TYPE);
5781 }
5782 if (dtohl(pkg->keyStrings) >= pkgSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005783 ALOGW("ResTable_package key strings at 0x%x are past chunk size 0x%x.",
5784 dtohl(pkg->keyStrings), pkgSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 return (mError=BAD_TYPE);
5786 }
5787 if ((dtohl(pkg->keyStrings)&0x3) != 0) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005788 ALOGW("ResTable_package key strings at 0x%x is not on an integer boundary.",
5789 dtohl(pkg->keyStrings));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 return (mError=BAD_TYPE);
5791 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005792
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005793 uint32_t id = dtohl(pkg->id);
5794 KeyedVector<uint8_t, IdmapEntries> idmapEntries;
Mark Salyzyn00adb862014-03-19 11:00:06 -07005795
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005796 if (header->resourceIDMap != NULL) {
5797 uint8_t targetPackageId = 0;
5798 status_t err = parseIdmap(header->resourceIDMap, header->resourceIDMapSize, &targetPackageId, &idmapEntries);
5799 if (err != NO_ERROR) {
5800 ALOGW("Overlay is broken");
5801 return (mError=err);
5802 }
5803 id = targetPackageId;
5804 }
5805
5806 if (id >= 256) {
5807 LOG_ALWAYS_FATAL("Package id out of range");
5808 return NO_ERROR;
5809 } else if (id == 0) {
5810 // This is a library so assign an ID
5811 id = mNextPackageId++;
5812 }
5813
5814 PackageGroup* group = NULL;
5815 Package* package = new Package(this, header, pkg);
5816 if (package == NULL) {
5817 return (mError=NO_MEMORY);
5818 }
5819
5820 err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings),
5821 header->dataEnd-(base+dtohl(pkg->typeStrings)));
5822 if (err != NO_ERROR) {
5823 delete group;
5824 delete package;
5825 return (mError=err);
5826 }
5827
5828 err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings),
5829 header->dataEnd-(base+dtohl(pkg->keyStrings)));
5830 if (err != NO_ERROR) {
5831 delete group;
5832 delete package;
5833 return (mError=err);
5834 }
5835
5836 size_t idx = mPackageMap[id];
5837 if (idx == 0) {
5838 idx = mPackageGroups.size() + 1;
5839
Adam Lesinski4bf58102014-11-03 11:21:19 -08005840 char16_t tmpName[sizeof(pkg->name)/sizeof(pkg->name[0])];
5841 strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(pkg->name[0]));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005842 group = new PackageGroup(this, String16(tmpName), id);
5843 if (group == NULL) {
5844 delete package;
Dianne Hackborn78c40512009-07-06 11:07:40 -07005845 return (mError=NO_MEMORY);
5846 }
Adam Lesinskifab50872014-04-16 14:40:42 -07005847
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005848 err = mPackageGroups.add(group);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 if (err < NO_ERROR) {
5850 return (mError=err);
5851 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005852
5853 mPackageMap[id] = static_cast<uint8_t>(idx);
5854
5855 // Find all packages that reference this package
5856 size_t N = mPackageGroups.size();
5857 for (size_t i = 0; i < N; i++) {
5858 mPackageGroups[i]->dynamicRefTable.addMapping(
5859 group->name, static_cast<uint8_t>(group->id));
5860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005862 group = mPackageGroups.itemAt(idx - 1);
5863 if (group == NULL) {
5864 return (mError=UNKNOWN_ERROR);
5865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 }
5867
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005868 err = group->packages.add(package);
5869 if (err < NO_ERROR) {
5870 return (mError=err);
5871 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 // Iterate through all chunks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 const ResChunk_header* chunk =
5875 (const ResChunk_header*)(((const uint8_t*)pkg)
5876 + dtohs(pkg->header.headerSize));
5877 const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size);
5878 while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) &&
5879 ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005880 if (kDebugTableNoisy) {
5881 ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
5882 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
5883 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
5884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 const size_t csize = dtohl(chunk->size);
5886 const uint16_t ctype = dtohs(chunk->type);
5887 if (ctype == RES_TABLE_TYPE_SPEC_TYPE) {
5888 const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk);
5889 err = validate_chunk(&typeSpec->header, sizeof(*typeSpec),
5890 endPos, "ResTable_typeSpec");
5891 if (err != NO_ERROR) {
5892 return (mError=err);
5893 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 const size_t typeSpecSize = dtohl(typeSpec->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005896 const size_t newEntryCount = dtohl(typeSpec->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07005897
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005898 if (kDebugLoadTableNoisy) {
5899 ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
5900 (void*)(base-(const uint8_t*)chunk),
5901 dtohs(typeSpec->header.type),
5902 dtohs(typeSpec->header.headerSize),
5903 (void*)typeSpecSize);
5904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 // look for block overrun or int overflow when multiplying by 4
5906 if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005907 || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 > typeSpecSize)) {
Steve Block8564c8d2012-01-05 23:22:43 +00005909 ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005910 (void*)(dtohs(typeSpec->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
5911 (void*)typeSpecSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 return (mError=BAD_TYPE);
5913 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 if (typeSpec->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00005916 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 return (mError=BAD_TYPE);
5918 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005919
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005920 if (newEntryCount > 0) {
5921 uint8_t typeIndex = typeSpec->id - 1;
5922 ssize_t idmapIndex = idmapEntries.indexOfKey(typeSpec->id);
5923 if (idmapIndex >= 0) {
5924 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
5925 }
5926
5927 TypeList& typeList = group->types.editItemAt(typeIndex);
5928 if (!typeList.isEmpty()) {
5929 const Type* existingType = typeList[0];
5930 if (existingType->entryCount != newEntryCount && idmapIndex < 0) {
5931 ALOGW("ResTable_typeSpec entry count inconsistent: given %d, previously %d",
5932 (int) newEntryCount, (int) existingType->entryCount);
5933 // We should normally abort here, but some legacy apps declare
5934 // resources in the 'android' package (old bug in AAPT).
5935 }
5936 }
5937
5938 Type* t = new Type(header, package, newEntryCount);
5939 t->typeSpec = typeSpec;
5940 t->typeSpecFlags = (const uint32_t*)(
5941 ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize));
5942 if (idmapIndex >= 0) {
5943 t->idmapEntries = idmapEntries[idmapIndex];
5944 }
5945 typeList.add(t);
5946 group->largestTypeId = max(group->largestTypeId, typeSpec->id);
5947 } else {
5948 ALOGV("Skipping empty ResTable_typeSpec for type %d", typeSpec->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 } else if (ctype == RES_TABLE_TYPE_TYPE) {
5952 const ResTable_type* type = (const ResTable_type*)(chunk);
5953 err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4,
5954 endPos, "ResTable_type");
5955 if (err != NO_ERROR) {
5956 return (mError=err);
5957 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005958
Patrik Bannura443dd932014-02-12 13:38:54 +01005959 const uint32_t typeSize = dtohl(type->header.size);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005960 const size_t newEntryCount = dtohl(type->entryCount);
Mark Salyzyn00adb862014-03-19 11:00:06 -07005961
Andreas Gampe2204f0b2014-10-21 23:04:54 -07005962 if (kDebugLoadTableNoisy) {
5963 printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n",
5964 (void*)(base-(const uint8_t*)chunk),
5965 dtohs(type->header.type),
5966 dtohs(type->header.headerSize),
5967 typeSize);
5968 }
5969 if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005970 ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005971 (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
5972 typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 return (mError=BAD_TYPE);
5974 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005975
5976 if (newEntryCount != 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) {
Patrik Bannura443dd932014-02-12 13:38:54 +01005978 ALOGW("ResTable_type entriesStart at 0x%x extends beyond chunk end 0x%x.",
5979 dtohl(type->entriesStart), typeSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 return (mError=BAD_TYPE);
5981 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 if (type->id == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00005984 ALOGW("ResTable_type has an id of 0.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 return (mError=BAD_TYPE);
5986 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07005987
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07005988 if (newEntryCount > 0) {
5989 uint8_t typeIndex = type->id - 1;
5990 ssize_t idmapIndex = idmapEntries.indexOfKey(type->id);
5991 if (idmapIndex >= 0) {
5992 typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1;
5993 }
5994
5995 TypeList& typeList = group->types.editItemAt(typeIndex);
5996 if (typeList.isEmpty()) {
5997 ALOGE("No TypeSpec for type %d", type->id);
5998 return (mError=BAD_TYPE);
5999 }
6000
6001 Type* t = typeList.editItemAt(typeList.size() - 1);
6002 if (newEntryCount != t->entryCount) {
6003 ALOGE("ResTable_type entry count inconsistent: given %d, previously %d",
6004 (int)newEntryCount, (int)t->entryCount);
6005 return (mError=BAD_TYPE);
6006 }
6007
6008 if (t->package != package) {
6009 ALOGE("No TypeSpec for type %d", type->id);
6010 return (mError=BAD_TYPE);
6011 }
6012
6013 t->configs.add(type);
6014
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006015 if (kDebugTableGetEntry) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006016 ResTable_config thisConfig;
6017 thisConfig.copyFromDtoH(type->config);
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006018 ALOGI("Adding config to type %d: %s\n", type->id,
6019 thisConfig.toString().string());
6020 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006021 } else {
6022 ALOGV("Skipping empty ResTable_type for type %d", type->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006024
Adam Lesinskide898ff2014-01-29 18:20:45 -08006025 } else if (ctype == RES_TABLE_LIBRARY_TYPE) {
6026 if (group->dynamicRefTable.entries().size() == 0) {
6027 status_t err = group->dynamicRefTable.load((const ResTable_lib_header*) chunk);
6028 if (err != NO_ERROR) {
6029 return (mError=err);
6030 }
6031
6032 // Fill in the reference table with the entries we already know about.
6033 size_t N = mPackageGroups.size();
6034 for (size_t i = 0; i < N; i++) {
6035 group->dynamicRefTable.addMapping(mPackageGroups[i]->name, mPackageGroups[i]->id);
6036 }
6037 } else {
6038 ALOGW("Found multiple library tables, ignoring...");
6039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 } else {
6041 status_t err = validate_chunk(chunk, sizeof(ResChunk_header),
6042 endPos, "ResTable_package:unknown");
6043 if (err != NO_ERROR) {
6044 return (mError=err);
6045 }
6046 }
6047 chunk = (const ResChunk_header*)
6048 (((const uint8_t*)chunk) + csize);
6049 }
6050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 return NO_ERROR;
6052}
6053
Adam Lesinskide898ff2014-01-29 18:20:45 -08006054DynamicRefTable::DynamicRefTable(uint8_t packageId)
6055 : mAssignedPackageId(packageId)
6056{
6057 memset(mLookupTable, 0, sizeof(mLookupTable));
6058
6059 // Reserved package ids
6060 mLookupTable[APP_PACKAGE_ID] = APP_PACKAGE_ID;
6061 mLookupTable[SYS_PACKAGE_ID] = SYS_PACKAGE_ID;
6062}
6063
6064status_t DynamicRefTable::load(const ResTable_lib_header* const header)
6065{
6066 const uint32_t entryCount = dtohl(header->count);
6067 const uint32_t sizeOfEntries = sizeof(ResTable_lib_entry) * entryCount;
6068 const uint32_t expectedSize = dtohl(header->header.size) - dtohl(header->header.headerSize);
6069 if (sizeOfEntries > expectedSize) {
6070 ALOGE("ResTable_lib_header size %u is too small to fit %u entries (x %u).",
6071 expectedSize, entryCount, (uint32_t)sizeof(ResTable_lib_entry));
6072 return UNKNOWN_ERROR;
6073 }
6074
6075 const ResTable_lib_entry* entry = (const ResTable_lib_entry*)(((uint8_t*) header) +
6076 dtohl(header->header.headerSize));
6077 for (uint32_t entryIndex = 0; entryIndex < entryCount; entryIndex++) {
6078 uint32_t packageId = dtohl(entry->packageId);
6079 char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)];
6080 strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t));
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006081 if (kDebugLibNoisy) {
6082 ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
6083 dtohl(entry->packageId));
6084 }
Adam Lesinskide898ff2014-01-29 18:20:45 -08006085 if (packageId >= 256) {
6086 ALOGE("Bad package id 0x%08x", packageId);
6087 return UNKNOWN_ERROR;
6088 }
6089 mEntries.replaceValueFor(String16(tmpName), (uint8_t) packageId);
6090 entry = entry + 1;
6091 }
6092 return NO_ERROR;
6093}
6094
Adam Lesinski6022deb2014-08-20 14:59:19 -07006095status_t DynamicRefTable::addMappings(const DynamicRefTable& other) {
6096 if (mAssignedPackageId != other.mAssignedPackageId) {
6097 return UNKNOWN_ERROR;
6098 }
6099
6100 const size_t entryCount = other.mEntries.size();
6101 for (size_t i = 0; i < entryCount; i++) {
6102 ssize_t index = mEntries.indexOfKey(other.mEntries.keyAt(i));
6103 if (index < 0) {
6104 mEntries.add(other.mEntries.keyAt(i), other.mEntries[i]);
6105 } else {
6106 if (other.mEntries[i] != mEntries[index]) {
6107 return UNKNOWN_ERROR;
6108 }
6109 }
6110 }
6111
6112 // Merge the lookup table. No entry can conflict
6113 // (value of 0 means not set).
6114 for (size_t i = 0; i < 256; i++) {
6115 if (mLookupTable[i] != other.mLookupTable[i]) {
6116 if (mLookupTable[i] == 0) {
6117 mLookupTable[i] = other.mLookupTable[i];
6118 } else if (other.mLookupTable[i] != 0) {
6119 return UNKNOWN_ERROR;
6120 }
6121 }
6122 }
6123 return NO_ERROR;
6124}
6125
Adam Lesinskide898ff2014-01-29 18:20:45 -08006126status_t DynamicRefTable::addMapping(const String16& packageName, uint8_t packageId)
6127{
6128 ssize_t index = mEntries.indexOfKey(packageName);
6129 if (index < 0) {
6130 return UNKNOWN_ERROR;
6131 }
6132 mLookupTable[mEntries.valueAt(index)] = packageId;
6133 return NO_ERROR;
6134}
6135
6136status_t DynamicRefTable::lookupResourceId(uint32_t* resId) const {
6137 uint32_t res = *resId;
6138 size_t packageId = Res_GETPACKAGE(res) + 1;
6139
6140 if (packageId == APP_PACKAGE_ID) {
6141 // No lookup needs to be done, app package IDs are absolute.
6142 return NO_ERROR;
6143 }
6144
6145 if (packageId == 0) {
6146 // The package ID is 0x00. That means that a shared library is accessing
6147 // its own local resource, so we fix up the resource with the calling
6148 // package ID.
6149 *resId |= ((uint32_t) mAssignedPackageId) << 24;
6150 return NO_ERROR;
6151 }
6152
6153 // Do a proper lookup.
6154 uint8_t translatedId = mLookupTable[packageId];
6155 if (translatedId == 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006156 ALOGV("DynamicRefTable(0x%02x): No mapping for build-time package ID 0x%02x.",
Adam Lesinskide898ff2014-01-29 18:20:45 -08006157 (uint8_t)mAssignedPackageId, (uint8_t)packageId);
6158 for (size_t i = 0; i < 256; i++) {
6159 if (mLookupTable[i] != 0) {
Adam Lesinskia7d1d732014-10-01 18:24:54 -07006160 ALOGV("e[0x%02x] -> 0x%02x", (uint8_t)i, mLookupTable[i]);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006161 }
6162 }
6163 return UNKNOWN_ERROR;
6164 }
6165
6166 *resId = (res & 0x00ffffff) | (((uint32_t) translatedId) << 24);
6167 return NO_ERROR;
6168}
6169
6170status_t DynamicRefTable::lookupResourceValue(Res_value* value) const {
6171 if (value->dataType != Res_value::TYPE_DYNAMIC_REFERENCE) {
6172 return NO_ERROR;
6173 }
6174
6175 status_t err = lookupResourceId(&value->data);
6176 if (err != NO_ERROR) {
6177 return err;
6178 }
6179
6180 value->dataType = Res_value::TYPE_REFERENCE;
6181 return NO_ERROR;
6182}
6183
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006184struct IdmapTypeMap {
6185 ssize_t overlayTypeId;
6186 size_t entryOffset;
6187 Vector<uint32_t> entryMap;
6188};
6189
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006190status_t ResTable::createIdmap(const ResTable& overlay,
6191 uint32_t targetCrc, uint32_t overlayCrc,
6192 const char* targetPath, const char* overlayPath,
6193 void** outData, size_t* outSize) const
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006194{
6195 // see README for details on the format of map
6196 if (mPackageGroups.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006197 ALOGW("idmap: target package has no package groups, cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006198 return UNKNOWN_ERROR;
6199 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006200
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006201 if (mPackageGroups[0]->packages.size() == 0) {
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006202 ALOGW("idmap: target package has no packages in its first package group, "
6203 "cannot create idmap\n");
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006204 return UNKNOWN_ERROR;
6205 }
6206
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006207 KeyedVector<uint8_t, IdmapTypeMap> map;
6208
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006209 // overlaid packages are assumed to contain only one package group
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006210 const PackageGroup* pg = mPackageGroups[0];
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006211
6212 // starting size is header
6213 *outSize = ResTable::IDMAP_HEADER_SIZE_BYTES;
6214
6215 // target package id and number of types in map
6216 *outSize += 2 * sizeof(uint16_t);
6217
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006218 // overlay packages are assumed to contain only one package group
Adam Lesinski4bf58102014-11-03 11:21:19 -08006219 const ResTable_package* overlayPackageStruct = overlay.mPackageGroups[0]->packages[0]->package;
6220 char16_t tmpName[sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])];
6221 strcpy16_dtoh(tmpName, overlayPackageStruct->name, sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0]));
6222 const String16 overlayPackage(tmpName);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006223
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006224 for (size_t typeIndex = 0; typeIndex < pg->types.size(); ++typeIndex) {
6225 const TypeList& typeList = pg->types[typeIndex];
6226 if (typeList.isEmpty()) {
6227 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006228 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006229
6230 const Type* typeConfigs = typeList[0];
6231
6232 IdmapTypeMap typeMap;
6233 typeMap.overlayTypeId = -1;
6234 typeMap.entryOffset = 0;
6235
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006236 for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006237 uint32_t resID = Res_MAKEID(pg->id - 1, typeIndex, entryIndex);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006238 resource_name resName;
MÃ¥rten Kongstad65a05fd2014-01-31 14:01:52 +01006239 if (!this->getResourceName(resID, false, &resName)) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006240 if (typeMap.entryMap.isEmpty()) {
6241 typeMap.entryOffset++;
6242 }
MÃ¥rten Kongstadfcaba142011-05-19 16:02:35 +02006243 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006244 }
6245
6246 const String16 overlayType(resName.type, resName.typeLen);
6247 const String16 overlayName(resName.name, resName.nameLen);
6248 uint32_t overlayResID = overlay.identifierForName(overlayName.string(),
6249 overlayName.size(),
6250 overlayType.string(),
6251 overlayType.size(),
6252 overlayPackage.string(),
6253 overlayPackage.size());
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006254 if (overlayResID == 0) {
6255 if (typeMap.entryMap.isEmpty()) {
6256 typeMap.entryOffset++;
Jean-Baptiste Queru3e2d5912012-05-01 10:00:22 -07006257 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006258 continue;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006259 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006260
6261 if (typeMap.overlayTypeId == -1) {
6262 typeMap.overlayTypeId = Res_GETTYPE(overlayResID) + 1;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006263 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006264
6265 if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) {
6266 ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x"
Andreas Gampe2204f0b2014-10-21 23:04:54 -07006267 " but entries should map to resources of type %02zx",
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006268 resID, overlayResID, typeMap.overlayTypeId);
6269 return BAD_TYPE;
6270 }
6271
6272 if (typeMap.entryOffset + typeMap.entryMap.size() < entryIndex) {
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006273 // pad with 0xffffffff's (indicating non-existing entries) before adding this entry
6274 size_t index = typeMap.entryMap.size();
6275 size_t numItems = entryIndex - (typeMap.entryOffset + index);
6276 if (typeMap.entryMap.insertAt(0xffffffff, index, numItems) < 0) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006277 return NO_MEMORY;
6278 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006279 }
MÃ¥rten Kongstad96198eb2014-11-07 10:56:12 +01006280 typeMap.entryMap.add(Res_GETENTRY(overlayResID));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006281 }
6282
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006283 if (!typeMap.entryMap.isEmpty()) {
6284 if (map.add(static_cast<uint8_t>(typeIndex), typeMap) < 0) {
6285 return NO_MEMORY;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006286 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006287 *outSize += (4 * sizeof(uint16_t)) + (typeMap.entryMap.size() * sizeof(uint32_t));
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006288 }
6289 }
6290
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006291 if (map.isEmpty()) {
6292 ALOGW("idmap: no resources in overlay package present in base package");
6293 return UNKNOWN_ERROR;
6294 }
6295
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006296 if ((*outData = malloc(*outSize)) == NULL) {
6297 return NO_MEMORY;
6298 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006299
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006300 uint32_t* data = (uint32_t*)*outData;
6301 *data++ = htodl(IDMAP_MAGIC);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006302 *data++ = htodl(IDMAP_CURRENT_VERSION);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006303 *data++ = htodl(targetCrc);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006304 *data++ = htodl(overlayCrc);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006305 const char* paths[] = { targetPath, overlayPath };
6306 for (int j = 0; j < 2; ++j) {
6307 char* p = (char*)data;
6308 const char* path = paths[j];
6309 const size_t I = strlen(path);
6310 if (I > 255) {
6311 ALOGV("path exceeds expected 255 characters: %s\n", path);
6312 return UNKNOWN_ERROR;
6313 }
6314 for (size_t i = 0; i < 256; ++i) {
6315 *p++ = i < I ? path[i] : '\0';
6316 }
6317 data += 256 / sizeof(uint32_t);
6318 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006319 const size_t mapSize = map.size();
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006320 uint16_t* typeData = reinterpret_cast<uint16_t*>(data);
6321 *typeData++ = htods(pg->id);
6322 *typeData++ = htods(mapSize);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006323 for (size_t i = 0; i < mapSize; ++i) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006324 uint8_t targetTypeId = map.keyAt(i);
6325 const IdmapTypeMap& typeMap = map[i];
6326 *typeData++ = htods(targetTypeId + 1);
6327 *typeData++ = htods(typeMap.overlayTypeId);
6328 *typeData++ = htods(typeMap.entryMap.size());
6329 *typeData++ = htods(typeMap.entryOffset);
6330
6331 const size_t entryCount = typeMap.entryMap.size();
6332 uint32_t* entries = reinterpret_cast<uint32_t*>(typeData);
6333 for (size_t j = 0; j < entryCount; j++) {
6334 entries[j] = htodl(typeMap.entryMap[j]);
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006335 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006336 typeData += entryCount * 2;
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006337 }
6338
6339 return NO_ERROR;
6340}
6341
6342bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006343 uint32_t* pVersion,
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006344 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
6345 String8* pTargetPath, String8* pOverlayPath)
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006346{
6347 const uint32_t* map = (const uint32_t*)idmap;
6348 if (!assertIdmapHeader(map, sizeBytes)) {
6349 return false;
6350 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006351 if (pVersion) {
6352 *pVersion = dtohl(map[1]);
6353 }
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006354 if (pTargetCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006355 *pTargetCrc = dtohl(map[2]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006356 }
6357 if (pOverlayCrc) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006358 *pOverlayCrc = dtohl(map[3]);
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006359 }
6360 if (pTargetPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006361 pTargetPath->setTo(reinterpret_cast<const char*>(map + 4));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006362 }
6363 if (pOverlayPath) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006364 pOverlayPath->setTo(reinterpret_cast<const char*>(map + 4 + 256 / sizeof(uint32_t)));
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01006365 }
MÃ¥rten Kongstad57f4b772011-03-17 14:13:41 +01006366 return true;
6367}
6368
6369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370#define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string())
6371
6372#define CHAR16_ARRAY_EQ(constant, var, len) \
6373 ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len))))
6374
Jeff Brown9d3b1a42013-07-01 19:07:15 -07006375static void print_complex(uint32_t complex, bool isFraction)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376{
Dianne Hackborne17086b2009-06-19 15:13:28 -07006377 const float MANTISSA_MULT =
6378 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT);
6379 const float RADIX_MULTS[] = {
6380 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
6381 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
6382 };
6383
6384 float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK
6385 <<Res_value::COMPLEX_MANTISSA_SHIFT))
6386 * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT)
6387 & Res_value::COMPLEX_RADIX_MASK];
6388 printf("%f", value);
Mark Salyzyn00adb862014-03-19 11:00:06 -07006389
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006390 if (!isFraction) {
Dianne Hackborne17086b2009-06-19 15:13:28 -07006391 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6392 case Res_value::COMPLEX_UNIT_PX: printf("px"); break;
6393 case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break;
6394 case Res_value::COMPLEX_UNIT_SP: printf("sp"); break;
6395 case Res_value::COMPLEX_UNIT_PT: printf("pt"); break;
6396 case Res_value::COMPLEX_UNIT_IN: printf("in"); break;
6397 case Res_value::COMPLEX_UNIT_MM: printf("mm"); break;
6398 default: printf(" (unknown unit)"); break;
6399 }
6400 } else {
6401 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
6402 case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break;
6403 case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break;
6404 default: printf(" (unknown unit)"); break;
6405 }
6406 }
6407}
6408
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006409// Normalize a string for output
6410String8 ResTable::normalizeForOutput( const char *input )
6411{
6412 String8 ret;
6413 char buff[2];
6414 buff[1] = '\0';
6415
6416 while (*input != '\0') {
6417 switch (*input) {
6418 // All interesting characters are in the ASCII zone, so we are making our own lives
6419 // easier by scanning the string one byte at a time.
6420 case '\\':
6421 ret += "\\\\";
6422 break;
6423 case '\n':
6424 ret += "\\n";
6425 break;
6426 case '"':
6427 ret += "\\\"";
6428 break;
6429 default:
6430 buff[0] = *input;
6431 ret += buff;
6432 break;
6433 }
6434
6435 input++;
6436 }
6437
6438 return ret;
6439}
6440
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006441void ResTable::print_value(const Package* pkg, const Res_value& value) const
6442{
6443 if (value.dataType == Res_value::TYPE_NULL) {
Alan Viverettef2969402014-10-29 17:09:36 -07006444 if (value.data == Res_value::DATA_NULL_UNDEFINED) {
6445 printf("(null)\n");
6446 } else if (value.data == Res_value::DATA_NULL_EMPTY) {
6447 printf("(null empty)\n");
6448 } else {
6449 // This should never happen.
6450 printf("(null) 0x%08x\n", value.data);
6451 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006452 } else if (value.dataType == Res_value::TYPE_REFERENCE) {
6453 printf("(reference) 0x%08x\n", value.data);
Adam Lesinskide898ff2014-01-29 18:20:45 -08006454 } else if (value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) {
6455 printf("(dynamic reference) 0x%08x\n", value.data);
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006456 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
6457 printf("(attribute) 0x%08x\n", value.data);
6458 } else if (value.dataType == Res_value::TYPE_STRING) {
6459 size_t len;
Kenny Root780d2a12010-02-22 22:36:26 -08006460 const char* str8 = pkg->header->values.string8At(
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006461 value.data, &len);
Kenny Root780d2a12010-02-22 22:36:26 -08006462 if (str8 != NULL) {
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006463 printf("(string8) \"%s\"\n", normalizeForOutput(str8).string());
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006464 } else {
Kenny Root780d2a12010-02-22 22:36:26 -08006465 const char16_t* str16 = pkg->header->values.stringAt(
6466 value.data, &len);
6467 if (str16 != NULL) {
6468 printf("(string16) \"%s\"\n",
Shachar Shemesh9872bf42010-12-20 17:38:33 +02006469 normalizeForOutput(String8(str16, len).string()).string());
Kenny Root780d2a12010-02-22 22:36:26 -08006470 } else {
6471 printf("(string) null\n");
6472 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006473 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -07006474 } else if (value.dataType == Res_value::TYPE_FLOAT) {
6475 printf("(float) %g\n", *(const float*)&value.data);
6476 } else if (value.dataType == Res_value::TYPE_DIMENSION) {
6477 printf("(dimension) ");
6478 print_complex(value.data, false);
6479 printf("\n");
6480 } else if (value.dataType == Res_value::TYPE_FRACTION) {
6481 printf("(fraction) ");
6482 print_complex(value.data, true);
6483 printf("\n");
6484 } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT
6485 || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) {
6486 printf("(color) #%08x\n", value.data);
6487 } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) {
6488 printf("(boolean) %s\n", value.data ? "true" : "false");
6489 } else if (value.dataType >= Res_value::TYPE_FIRST_INT
6490 || value.dataType <= Res_value::TYPE_LAST_INT) {
6491 printf("(int) 0x%08x or %d\n", value.data, value.data);
6492 } else {
6493 printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n",
6494 (int)value.dataType, (int)value.data,
6495 (int)value.size, (int)value.res0);
6496 }
6497}
6498
Dianne Hackborne17086b2009-06-19 15:13:28 -07006499void ResTable::print(bool inclValues) const
6500{
6501 if (mError != 0) {
6502 printf("mError=0x%x (%s)\n", mError, strerror(mError));
6503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 size_t pgCount = mPackageGroups.size();
6505 printf("Package Groups (%d)\n", (int)pgCount);
6506 for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) {
6507 const PackageGroup* pg = mPackageGroups[pgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006508 printf("Package Group %d id=0x%02x packageCount=%d name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 (int)pgIndex, pg->id, (int)pg->packages.size(),
6510 String8(pg->name).string());
Mark Salyzyn00adb862014-03-19 11:00:06 -07006511
Adam Lesinski6022deb2014-08-20 14:59:19 -07006512 const KeyedVector<String16, uint8_t>& refEntries = pg->dynamicRefTable.entries();
6513 const size_t refEntryCount = refEntries.size();
6514 if (refEntryCount > 0) {
6515 printf(" DynamicRefTable entryCount=%d:\n", (int) refEntryCount);
6516 for (size_t refIndex = 0; refIndex < refEntryCount; refIndex++) {
6517 printf(" 0x%02x -> %s\n",
6518 refEntries.valueAt(refIndex),
6519 String8(refEntries.keyAt(refIndex)).string());
6520 }
6521 printf("\n");
6522 }
6523
6524 int packageId = pg->id;
Adam Lesinski18560882014-08-15 17:18:21 +00006525 size_t pkgCount = pg->packages.size();
6526 for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) {
6527 const Package* pkg = pg->packages[pkgIndex];
Adam Lesinski6022deb2014-08-20 14:59:19 -07006528 // Use a package's real ID, since the ID may have been assigned
6529 // if this package is a shared library.
6530 packageId = pkg->package->id;
Adam Lesinski4bf58102014-11-03 11:21:19 -08006531 char16_t tmpName[sizeof(pkg->package->name)/sizeof(pkg->package->name[0])];
6532 strcpy16_dtoh(tmpName, pkg->package->name, sizeof(pkg->package->name)/sizeof(pkg->package->name[0]));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006533 printf(" Package %d id=0x%02x name=%s\n", (int)pkgIndex,
Adam Lesinski4bf58102014-11-03 11:21:19 -08006534 pkg->package->id, String8(tmpName).string());
Adam Lesinski18560882014-08-15 17:18:21 +00006535 }
6536
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006537 for (size_t typeIndex=0; typeIndex < pg->types.size(); typeIndex++) {
6538 const TypeList& typeList = pg->types[typeIndex];
6539 if (typeList.isEmpty()) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006540 continue;
6541 }
6542 const Type* typeConfigs = typeList[0];
6543 const size_t NTC = typeConfigs->configs.size();
6544 printf(" type %d configCount=%d entryCount=%d\n",
6545 (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount);
6546 if (typeConfigs->typeSpecFlags != NULL) {
6547 for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) {
Adam Lesinski6022deb2014-08-20 14:59:19 -07006548 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006549 | (0x00ff0000 & ((typeIndex+1)<<16))
6550 | (0x0000ffff & (entryIndex));
6551 // Since we are creating resID without actually
6552 // iterating over them, we have no idea which is a
6553 // dynamic reference. We must check.
Adam Lesinski6022deb2014-08-20 14:59:19 -07006554 if (packageId == 0) {
6555 pg->dynamicRefTable.lookupResourceId(&resID);
6556 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006557
6558 resource_name resName;
6559 if (this->getResourceName(resID, true, &resName)) {
6560 String8 type8;
6561 String8 name8;
6562 if (resName.type8 != NULL) {
6563 type8 = String8(resName.type8, resName.typeLen);
6564 } else {
6565 type8 = String8(resName.type, resName.typeLen);
6566 }
6567 if (resName.name8 != NULL) {
6568 name8 = String8(resName.name8, resName.nameLen);
6569 } else {
6570 name8 = String8(resName.name, resName.nameLen);
6571 }
6572 printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n",
6573 resID,
6574 CHAR16_TO_CSTR(resName.package, resName.packageLen),
6575 type8.string(), name8.string(),
6576 dtohl(typeConfigs->typeSpecFlags[entryIndex]));
6577 } else {
6578 printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID);
6579 }
6580 }
6581 }
6582 for (size_t configIndex=0; configIndex<NTC; configIndex++) {
6583 const ResTable_type* type = typeConfigs->configs[configIndex];
6584 if ((((uint64_t)type)&0x3) != 0) {
6585 printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 continue;
6587 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006588 String8 configStr = type->config.toString();
6589 printf(" config %s:\n", configStr.size() > 0
6590 ? configStr.string() : "(default)");
6591 size_t entryCount = dtohl(type->entryCount);
6592 uint32_t entriesStart = dtohl(type->entriesStart);
6593 if ((entriesStart&0x3) != 0) {
6594 printf(" NON-INTEGER ResTable_type entriesStart OFFSET: 0x%x\n", entriesStart);
6595 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006597 uint32_t typeSize = dtohl(type->header.size);
6598 if ((typeSize&0x3) != 0) {
6599 printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize);
6600 continue;
6601 }
6602 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006603 const uint32_t* const eindex = (const uint32_t*)
6604 (((const uint8_t*)type) + dtohs(type->header.headerSize));
6605
6606 uint32_t thisOffset = dtohl(eindex[entryIndex]);
6607 if (thisOffset == ResTable_type::NO_ENTRY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 continue;
6609 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006610
Adam Lesinski6022deb2014-08-20 14:59:19 -07006611 uint32_t resID = (0xff000000 & ((packageId)<<24))
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006612 | (0x00ff0000 & ((typeIndex+1)<<16))
6613 | (0x0000ffff & (entryIndex));
Adam Lesinski6022deb2014-08-20 14:59:19 -07006614 if (packageId == 0) {
6615 pg->dynamicRefTable.lookupResourceId(&resID);
6616 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006617 resource_name resName;
6618 if (this->getResourceName(resID, true, &resName)) {
6619 String8 type8;
6620 String8 name8;
6621 if (resName.type8 != NULL) {
6622 type8 = String8(resName.type8, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006623 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006624 type8 = String8(resName.type, resName.typeLen);
Kenny Root33791952010-06-08 10:16:48 -07006625 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006626 if (resName.name8 != NULL) {
6627 name8 = String8(resName.name8, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 } else {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006629 name8 = String8(resName.name, resName.nameLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006631 printf(" resource 0x%08x %s:%s/%s: ", resID,
6632 CHAR16_TO_CSTR(resName.package, resName.packageLen),
6633 type8.string(), name8.string());
6634 } else {
6635 printf(" INVALID RESOURCE 0x%08x: ", resID);
6636 }
6637 if ((thisOffset&0x3) != 0) {
6638 printf("NON-INTEGER OFFSET: 0x%x\n", thisOffset);
6639 continue;
6640 }
6641 if ((thisOffset+sizeof(ResTable_entry)) > typeSize) {
6642 printf("OFFSET OUT OF BOUNDS: 0x%x+0x%x (size is 0x%x)\n",
6643 entriesStart, thisOffset, typeSize);
6644 continue;
6645 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006646
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006647 const ResTable_entry* ent = (const ResTable_entry*)
6648 (((const uint8_t*)type) + entriesStart + thisOffset);
6649 if (((entriesStart + thisOffset)&0x3) != 0) {
6650 printf("NON-INTEGER ResTable_entry OFFSET: 0x%x\n",
6651 (entriesStart + thisOffset));
6652 continue;
6653 }
Mark Salyzyn00adb862014-03-19 11:00:06 -07006654
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006655 uintptr_t esize = dtohs(ent->size);
6656 if ((esize&0x3) != 0) {
6657 printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void *)esize);
6658 continue;
6659 }
6660 if ((thisOffset+esize) > typeSize) {
6661 printf("ResTable_entry OUT OF BOUNDS: 0x%x+0x%x+%p (size is 0x%x)\n",
6662 entriesStart, thisOffset, (void *)esize, typeSize);
6663 continue;
6664 }
6665
6666 const Res_value* valuePtr = NULL;
6667 const ResTable_map_entry* bagPtr = NULL;
6668 Res_value value;
6669 if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) {
6670 printf("<bag>");
6671 bagPtr = (const ResTable_map_entry*)ent;
6672 } else {
6673 valuePtr = (const Res_value*)
6674 (((const uint8_t*)ent) + esize);
6675 value.copyFrom_dtoh(*valuePtr);
6676 printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)",
6677 (int)value.dataType, (int)value.data,
6678 (int)value.size, (int)value.res0);
6679 }
6680
6681 if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) {
6682 printf(" (PUBLIC)");
6683 }
6684 printf("\n");
6685
6686 if (inclValues) {
6687 if (valuePtr != NULL) {
6688 printf(" ");
6689 print_value(typeConfigs->package, value);
6690 } else if (bagPtr != NULL) {
6691 const int N = dtohl(bagPtr->count);
6692 const uint8_t* baseMapPtr = (const uint8_t*)ent;
6693 size_t mapOffset = esize;
6694 const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
6695 const uint32_t parent = dtohl(bagPtr->parent.ident);
6696 uint32_t resolvedParent = parent;
Adam Lesinski6022deb2014-08-20 14:59:19 -07006697 if (Res_GETPACKAGE(resolvedParent) + 1 == 0) {
6698 status_t err = pg->dynamicRefTable.lookupResourceId(&resolvedParent);
6699 if (err != NO_ERROR) {
6700 resolvedParent = 0;
6701 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07006702 }
6703 printf(" Parent=0x%08x(Resolved=0x%08x), Count=%d\n",
6704 parent, resolvedParent, N);
6705 for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) {
6706 printf(" #%i (Key=0x%08x): ",
6707 i, dtohl(mapPtr->name.ident));
6708 value.copyFrom_dtoh(mapPtr->value);
6709 print_value(typeConfigs->package, value);
6710 const size_t size = dtohs(mapPtr->value.size);
6711 mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value);
6712 mapPtr = (ResTable_map*)(baseMapPtr+mapOffset);
Dianne Hackborne17086b2009-06-19 15:13:28 -07006713 }
6714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 }
6716 }
6717 }
6718 }
6719 }
6720}
6721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722} // namespace android