The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 20 | #include <androidfw/ResourceTypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/Atomic.h> |
| 22 | #include <utils/ByteOrder.h> |
| 23 | #include <utils/Debug.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/String16.h> |
| 26 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <memory.h> |
| 31 | #include <ctype.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #ifndef INT32_MAX |
| 35 | #define INT32_MAX ((int32_t)(2147483647)) |
| 36 | #endif |
| 37 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 38 | #define STRING_POOL_NOISY(x) //x |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #define XML_NOISY(x) //x |
| 40 | #define TABLE_NOISY(x) //x |
| 41 | #define TABLE_GETENTRY(x) //x |
| 42 | #define TABLE_SUPER_NOISY(x) //x |
| 43 | #define LOAD_TABLE_NOISY(x) //x |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 44 | #define TABLE_THEME(x) //x |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | #ifdef HAVE_WINSOCK |
| 49 | #undef nhtol |
| 50 | #undef htonl |
| 51 | |
| 52 | #ifdef HAVE_LITTLE_ENDIAN |
| 53 | #define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) ) |
| 54 | #define htonl(x) ntohl(x) |
| 55 | #define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) ) |
| 56 | #define htons(x) ntohs(x) |
| 57 | #else |
| 58 | #define ntohl(x) (x) |
| 59 | #define htonl(x) (x) |
| 60 | #define ntohs(x) (x) |
| 61 | #define htons(x) (x) |
| 62 | #endif |
| 63 | #endif |
| 64 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 65 | #define IDMAP_MAGIC 0x706d6469 |
| 66 | // size measured in sizeof(uint32_t) |
| 67 | #define IDMAP_HEADER_SIZE (ResTable::IDMAP_HEADER_SIZE_BYTES / sizeof(uint32_t)) |
| 68 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 69 | static void printToLogFunc(int32_t cookie, const char* txt) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | { |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 71 | ALOGV("[cookie=%d] %s", cookie, txt); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Standard C isspace() is only required to look at the low byte of its input, so |
| 75 | // produces incorrect results for UTF-16 characters. For safety's sake, assume that |
| 76 | // any high-byte UTF-16 code point is not whitespace. |
| 77 | inline int isspace16(char16_t c) { |
| 78 | return (c < 0x0080 && isspace(c)); |
| 79 | } |
| 80 | |
| 81 | // range checked; guaranteed to NUL-terminate within the stated number of available slots |
| 82 | // NOTE: if this truncates the dst string due to running out of space, no attempt is |
| 83 | // made to avoid splitting surrogate pairs. |
| 84 | static void strcpy16_dtoh(uint16_t* dst, const uint16_t* src, size_t avail) |
| 85 | { |
| 86 | uint16_t* last = dst + avail - 1; |
| 87 | while (*src && (dst < last)) { |
| 88 | char16_t s = dtohs(*src); |
| 89 | *dst++ = s; |
| 90 | src++; |
| 91 | } |
| 92 | *dst = 0; |
| 93 | } |
| 94 | |
| 95 | static status_t validate_chunk(const ResChunk_header* chunk, |
| 96 | size_t minSize, |
| 97 | const uint8_t* dataEnd, |
| 98 | const char* name) |
| 99 | { |
| 100 | const uint16_t headerSize = dtohs(chunk->headerSize); |
| 101 | const uint32_t size = dtohl(chunk->size); |
| 102 | |
| 103 | if (headerSize >= minSize) { |
| 104 | if (headerSize <= size) { |
| 105 | if (((headerSize|size)&0x3) == 0) { |
| 106 | if ((ssize_t)size <= (dataEnd-((const uint8_t*)chunk))) { |
| 107 | return NO_ERROR; |
| 108 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 109 | ALOGW("%s data size %p extends beyond resource end %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | name, (void*)size, |
| 111 | (void*)(dataEnd-((const uint8_t*)chunk))); |
| 112 | return BAD_TYPE; |
| 113 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 114 | ALOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | name, (int)size, (int)headerSize); |
| 116 | return BAD_TYPE; |
| 117 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 118 | ALOGW("%s size %p is smaller than header size %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | name, (void*)size, (void*)(int)headerSize); |
| 120 | return BAD_TYPE; |
| 121 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 122 | ALOGW("%s header size %p is too small.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | name, (void*)(int)headerSize); |
| 124 | return BAD_TYPE; |
| 125 | } |
| 126 | |
| 127 | inline void Res_value::copyFrom_dtoh(const Res_value& src) |
| 128 | { |
| 129 | size = dtohs(src.size); |
| 130 | res0 = src.res0; |
| 131 | dataType = src.dataType; |
| 132 | data = dtohl(src.data); |
| 133 | } |
| 134 | |
| 135 | void Res_png_9patch::deviceToFile() |
| 136 | { |
| 137 | for (int i = 0; i < numXDivs; i++) { |
| 138 | xDivs[i] = htonl(xDivs[i]); |
| 139 | } |
| 140 | for (int i = 0; i < numYDivs; i++) { |
| 141 | yDivs[i] = htonl(yDivs[i]); |
| 142 | } |
| 143 | paddingLeft = htonl(paddingLeft); |
| 144 | paddingRight = htonl(paddingRight); |
| 145 | paddingTop = htonl(paddingTop); |
| 146 | paddingBottom = htonl(paddingBottom); |
| 147 | for (int i=0; i<numColors; i++) { |
| 148 | colors[i] = htonl(colors[i]); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void Res_png_9patch::fileToDevice() |
| 153 | { |
| 154 | for (int i = 0; i < numXDivs; i++) { |
| 155 | xDivs[i] = ntohl(xDivs[i]); |
| 156 | } |
| 157 | for (int i = 0; i < numYDivs; i++) { |
| 158 | yDivs[i] = ntohl(yDivs[i]); |
| 159 | } |
| 160 | paddingLeft = ntohl(paddingLeft); |
| 161 | paddingRight = ntohl(paddingRight); |
| 162 | paddingTop = ntohl(paddingTop); |
| 163 | paddingBottom = ntohl(paddingBottom); |
| 164 | for (int i=0; i<numColors; i++) { |
| 165 | colors[i] = ntohl(colors[i]); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | size_t Res_png_9patch::serializedSize() |
| 170 | { |
| 171 | // The size of this struct is 32 bytes on the 32-bit target system |
| 172 | // 4 * int8_t |
| 173 | // 4 * int32_t |
| 174 | // 3 * pointer |
| 175 | return 32 |
| 176 | + numXDivs * sizeof(int32_t) |
| 177 | + numYDivs * sizeof(int32_t) |
| 178 | + numColors * sizeof(uint32_t); |
| 179 | } |
| 180 | |
| 181 | void* Res_png_9patch::serialize() |
| 182 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 183 | // Use calloc since we're going to leave a few holes in the data |
| 184 | // and want this to run cleanly under valgrind |
| 185 | void* newData = calloc(1, serializedSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | serialize(newData); |
| 187 | return newData; |
| 188 | } |
| 189 | |
| 190 | void Res_png_9patch::serialize(void * outData) |
| 191 | { |
| 192 | char* data = (char*) outData; |
| 193 | memmove(data, &wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors |
| 194 | memmove(data + 12, &paddingLeft, 16); // copy paddingXXXX |
| 195 | data += 32; |
| 196 | |
| 197 | memmove(data, this->xDivs, numXDivs * sizeof(int32_t)); |
| 198 | data += numXDivs * sizeof(int32_t); |
| 199 | memmove(data, this->yDivs, numYDivs * sizeof(int32_t)); |
| 200 | data += numYDivs * sizeof(int32_t); |
| 201 | memmove(data, this->colors, numColors * sizeof(uint32_t)); |
| 202 | } |
| 203 | |
| 204 | static void deserializeInternal(const void* inData, Res_png_9patch* outData) { |
| 205 | char* patch = (char*) inData; |
| 206 | if (inData != outData) { |
| 207 | memmove(&outData->wasDeserialized, patch, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors |
| 208 | memmove(&outData->paddingLeft, patch + 12, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors |
| 209 | } |
| 210 | outData->wasDeserialized = true; |
| 211 | char* data = (char*)outData; |
| 212 | data += sizeof(Res_png_9patch); |
| 213 | outData->xDivs = (int32_t*) data; |
| 214 | data += outData->numXDivs * sizeof(int32_t); |
| 215 | outData->yDivs = (int32_t*) data; |
| 216 | data += outData->numYDivs * sizeof(int32_t); |
| 217 | outData->colors = (uint32_t*) data; |
| 218 | } |
| 219 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 220 | static bool assertIdmapHeader(const uint32_t* map, size_t sizeBytes) |
| 221 | { |
| 222 | if (sizeBytes < ResTable::IDMAP_HEADER_SIZE_BYTES) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 223 | ALOGW("idmap assertion failed: size=%d bytes\n", (int)sizeBytes); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | if (*map != htodl(IDMAP_MAGIC)) { // htodl: map data expected to be in correct endianess |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 227 | ALOGW("idmap assertion failed: invalid magic found (is 0x%08x, expected 0x%08x)\n", |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 228 | *map, htodl(IDMAP_MAGIC)); |
| 229 | return false; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | static status_t idmapLookup(const uint32_t* map, size_t sizeBytes, uint32_t key, uint32_t* outValue) |
| 235 | { |
| 236 | // see README for details on the format of map |
| 237 | if (!assertIdmapHeader(map, sizeBytes)) { |
| 238 | return UNKNOWN_ERROR; |
| 239 | } |
| 240 | map = map + IDMAP_HEADER_SIZE; // skip ahead to data segment |
| 241 | // size of data block, in uint32_t |
| 242 | const size_t size = (sizeBytes - ResTable::IDMAP_HEADER_SIZE_BYTES) / sizeof(uint32_t); |
| 243 | const uint32_t type = Res_GETTYPE(key) + 1; // add one, idmap stores "public" type id |
| 244 | const uint32_t entry = Res_GETENTRY(key); |
| 245 | const uint32_t typeCount = *map; |
| 246 | |
| 247 | if (type > typeCount) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 248 | ALOGW("Resource ID map: type=%d exceeds number of types=%d\n", type, typeCount); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 249 | return UNKNOWN_ERROR; |
| 250 | } |
| 251 | if (typeCount > size) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 252 | ALOGW("Resource ID map: number of types=%d exceeds size of map=%d\n", typeCount, (int)size); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 253 | return UNKNOWN_ERROR; |
| 254 | } |
| 255 | const uint32_t typeOffset = map[type]; |
| 256 | if (typeOffset == 0) { |
| 257 | *outValue = 0; |
| 258 | return NO_ERROR; |
| 259 | } |
| 260 | if (typeOffset + 1 > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 261 | ALOGW("Resource ID map: type offset=%d exceeds reasonable value, size of map=%d\n", |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 262 | typeOffset, (int)size); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 263 | return UNKNOWN_ERROR; |
| 264 | } |
| 265 | const uint32_t entryCount = map[typeOffset]; |
| 266 | const uint32_t entryOffset = map[typeOffset + 1]; |
| 267 | if (entryCount == 0 || entry < entryOffset || entry - entryOffset > entryCount - 1) { |
| 268 | *outValue = 0; |
| 269 | return NO_ERROR; |
| 270 | } |
| 271 | const uint32_t index = typeOffset + 2 + entry - entryOffset; |
| 272 | if (index > size) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 273 | ALOGW("Resource ID map: entry index=%d exceeds size of map=%d\n", index, (int)size); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 274 | *outValue = 0; |
| 275 | return NO_ERROR; |
| 276 | } |
| 277 | *outValue = map[index]; |
| 278 | |
| 279 | return NO_ERROR; |
| 280 | } |
| 281 | |
| 282 | static status_t getIdmapPackageId(const uint32_t* map, size_t mapSize, uint32_t *outId) |
| 283 | { |
| 284 | if (!assertIdmapHeader(map, mapSize)) { |
| 285 | return UNKNOWN_ERROR; |
| 286 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 287 | if (mapSize <= IDMAP_HEADER_SIZE + 1) { |
| 288 | ALOGW("corrupt idmap: map size %d too short\n", mapSize); |
| 289 | return UNKNOWN_ERROR; |
| 290 | } |
| 291 | uint32_t typeCount = *(map + IDMAP_HEADER_SIZE); |
| 292 | if (typeCount == 0) { |
| 293 | ALOGW("corrupt idmap: no types\n"); |
| 294 | return UNKNOWN_ERROR; |
| 295 | } |
| 296 | if (IDMAP_HEADER_SIZE + 1 + typeCount > mapSize) { |
| 297 | ALOGW("corrupt idmap: number of types %d extends past idmap size %d\n", typeCount, mapSize); |
| 298 | return UNKNOWN_ERROR; |
| 299 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 300 | const uint32_t* p = map + IDMAP_HEADER_SIZE + 1; |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 301 | // find first defined type |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 302 | while (*p == 0) { |
| 303 | ++p; |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 304 | if (--typeCount == 0) { |
| 305 | ALOGW("corrupt idmap: types declared, none found\n"); |
| 306 | return UNKNOWN_ERROR; |
| 307 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 308 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 309 | |
| 310 | // determine package id from first entry of first type |
| 311 | const uint32_t offset = *p + IDMAP_HEADER_SIZE + 2; |
| 312 | if (offset > mapSize) { |
| 313 | ALOGW("corrupt idmap: entry offset %d points outside map size %d\n", offset, mapSize); |
| 314 | return UNKNOWN_ERROR; |
| 315 | } |
| 316 | *outId = (map[offset] >> 24) & 0x000000ff; |
| 317 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 318 | return NO_ERROR; |
| 319 | } |
| 320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | Res_png_9patch* Res_png_9patch::deserialize(const void* inData) |
| 322 | { |
| 323 | if (sizeof(void*) != sizeof(int32_t)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 324 | ALOGE("Cannot deserialize on non 32-bit system\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
| 327 | deserializeInternal(inData, (Res_png_9patch*) inData); |
| 328 | return (Res_png_9patch*) inData; |
| 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | // -------------------------------------------------------------------- |
| 333 | // -------------------------------------------------------------------- |
| 334 | |
| 335 | ResStringPool::ResStringPool() |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 336 | : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | { |
| 338 | } |
| 339 | |
| 340 | ResStringPool::ResStringPool(const void* data, size_t size, bool copyData) |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 341 | : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | { |
| 343 | setTo(data, size, copyData); |
| 344 | } |
| 345 | |
| 346 | ResStringPool::~ResStringPool() |
| 347 | { |
| 348 | uninit(); |
| 349 | } |
| 350 | |
| 351 | status_t ResStringPool::setTo(const void* data, size_t size, bool copyData) |
| 352 | { |
| 353 | if (!data || !size) { |
| 354 | return (mError=BAD_TYPE); |
| 355 | } |
| 356 | |
| 357 | uninit(); |
| 358 | |
| 359 | const bool notDeviceEndian = htods(0xf0) != 0xf0; |
| 360 | |
| 361 | if (copyData || notDeviceEndian) { |
| 362 | mOwnedData = malloc(size); |
| 363 | if (mOwnedData == NULL) { |
| 364 | return (mError=NO_MEMORY); |
| 365 | } |
| 366 | memcpy(mOwnedData, data, size); |
| 367 | data = mOwnedData; |
| 368 | } |
| 369 | |
| 370 | mHeader = (const ResStringPool_header*)data; |
| 371 | |
| 372 | if (notDeviceEndian) { |
| 373 | ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader); |
| 374 | h->header.headerSize = dtohs(mHeader->header.headerSize); |
| 375 | h->header.type = dtohs(mHeader->header.type); |
| 376 | h->header.size = dtohl(mHeader->header.size); |
| 377 | h->stringCount = dtohl(mHeader->stringCount); |
| 378 | h->styleCount = dtohl(mHeader->styleCount); |
| 379 | h->flags = dtohl(mHeader->flags); |
| 380 | h->stringsStart = dtohl(mHeader->stringsStart); |
| 381 | h->stylesStart = dtohl(mHeader->stylesStart); |
| 382 | } |
| 383 | |
| 384 | if (mHeader->header.headerSize > mHeader->header.size |
| 385 | || mHeader->header.size > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 386 | ALOGW("Bad string block: header size %d or total size %d is larger than data size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size); |
| 388 | return (mError=BAD_TYPE); |
| 389 | } |
| 390 | mSize = mHeader->header.size; |
| 391 | mEntries = (const uint32_t*) |
| 392 | (((const uint8_t*)data)+mHeader->header.headerSize); |
| 393 | |
| 394 | if (mHeader->stringCount > 0) { |
| 395 | if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow? |
| 396 | || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))) |
| 397 | > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 398 | ALOGW("Bad string block: entry of %d items extends past data size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))), |
| 400 | (int)size); |
| 401 | return (mError=BAD_TYPE); |
| 402 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 403 | |
| 404 | size_t charSize; |
| 405 | if (mHeader->flags&ResStringPool_header::UTF8_FLAG) { |
| 406 | charSize = sizeof(uint8_t); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 407 | } else { |
| 408 | charSize = sizeof(char16_t); |
| 409 | } |
| 410 | |
| 411 | mStrings = (const void*) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | (((const uint8_t*)data)+mHeader->stringsStart); |
| 413 | if (mHeader->stringsStart >= (mHeader->header.size-sizeof(uint16_t))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 414 | ALOGW("Bad string block: string pool starts at %d, after total size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | (int)mHeader->stringsStart, (int)mHeader->header.size); |
| 416 | return (mError=BAD_TYPE); |
| 417 | } |
| 418 | if (mHeader->styleCount == 0) { |
| 419 | mStringPoolSize = |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 420 | (mHeader->header.size-mHeader->stringsStart)/charSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | } else { |
Kenny Root | 5e4d9a0 | 2010-06-08 12:34:43 -0700 | [diff] [blame] | 422 | // check invariant: styles starts before end of data |
| 423 | if (mHeader->stylesStart >= (mHeader->header.size-sizeof(uint16_t))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 424 | ALOGW("Bad style block: style block starts at %d past data size of %d\n", |
Kenny Root | 5e4d9a0 | 2010-06-08 12:34:43 -0700 | [diff] [blame] | 425 | (int)mHeader->stylesStart, (int)mHeader->header.size); |
| 426 | return (mError=BAD_TYPE); |
| 427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | // check invariant: styles follow the strings |
| 429 | if (mHeader->stylesStart <= mHeader->stringsStart) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 430 | ALOGW("Bad style block: style block starts at %d, before strings at %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | (int)mHeader->stylesStart, (int)mHeader->stringsStart); |
| 432 | return (mError=BAD_TYPE); |
| 433 | } |
| 434 | mStringPoolSize = |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 435 | (mHeader->stylesStart-mHeader->stringsStart)/charSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // check invariant: stringCount > 0 requires a string pool to exist |
| 439 | if (mStringPoolSize == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 440 | ALOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | return (mError=BAD_TYPE); |
| 442 | } |
| 443 | |
| 444 | if (notDeviceEndian) { |
| 445 | size_t i; |
| 446 | uint32_t* e = const_cast<uint32_t*>(mEntries); |
| 447 | for (i=0; i<mHeader->stringCount; i++) { |
| 448 | e[i] = dtohl(mEntries[i]); |
| 449 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 450 | if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) { |
| 451 | const char16_t* strings = (const char16_t*)mStrings; |
| 452 | char16_t* s = const_cast<char16_t*>(strings); |
| 453 | for (i=0; i<mStringPoolSize; i++) { |
| 454 | s[i] = dtohs(strings[i]); |
| 455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 459 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG && |
| 460 | ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) || |
| 461 | (!mHeader->flags&ResStringPool_header::UTF8_FLAG && |
| 462 | ((char16_t*)mStrings)[mStringPoolSize-1] != 0)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 463 | ALOGW("Bad string block: last string is not 0-terminated\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | return (mError=BAD_TYPE); |
| 465 | } |
| 466 | } else { |
| 467 | mStrings = NULL; |
| 468 | mStringPoolSize = 0; |
| 469 | } |
| 470 | |
| 471 | if (mHeader->styleCount > 0) { |
| 472 | mEntryStyles = mEntries + mHeader->stringCount; |
| 473 | // invariant: integer overflow in calculating mEntryStyles |
| 474 | if (mEntryStyles < mEntries) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 475 | ALOGW("Bad string block: integer overflow finding styles\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | return (mError=BAD_TYPE); |
| 477 | } |
| 478 | |
| 479 | if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 480 | ALOGW("Bad string block: entry of %d styles extends past data size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader), |
| 482 | (int)size); |
| 483 | return (mError=BAD_TYPE); |
| 484 | } |
| 485 | mStyles = (const uint32_t*) |
| 486 | (((const uint8_t*)data)+mHeader->stylesStart); |
| 487 | if (mHeader->stylesStart >= mHeader->header.size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 488 | ALOGW("Bad string block: style pool starts %d, after total size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | (int)mHeader->stylesStart, (int)mHeader->header.size); |
| 490 | return (mError=BAD_TYPE); |
| 491 | } |
| 492 | mStylePoolSize = |
| 493 | (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t); |
| 494 | |
| 495 | if (notDeviceEndian) { |
| 496 | size_t i; |
| 497 | uint32_t* e = const_cast<uint32_t*>(mEntryStyles); |
| 498 | for (i=0; i<mHeader->styleCount; i++) { |
| 499 | e[i] = dtohl(mEntryStyles[i]); |
| 500 | } |
| 501 | uint32_t* s = const_cast<uint32_t*>(mStyles); |
| 502 | for (i=0; i<mStylePoolSize; i++) { |
| 503 | s[i] = dtohl(mStyles[i]); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | const ResStringPool_span endSpan = { |
| 508 | { htodl(ResStringPool_span::END) }, |
| 509 | htodl(ResStringPool_span::END), htodl(ResStringPool_span::END) |
| 510 | }; |
| 511 | if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))], |
| 512 | &endSpan, sizeof(endSpan)) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 513 | ALOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | return (mError=BAD_TYPE); |
| 515 | } |
| 516 | } else { |
| 517 | mEntryStyles = NULL; |
| 518 | mStyles = NULL; |
| 519 | mStylePoolSize = 0; |
| 520 | } |
| 521 | |
| 522 | return (mError=NO_ERROR); |
| 523 | } |
| 524 | |
| 525 | status_t ResStringPool::getError() const |
| 526 | { |
| 527 | return mError; |
| 528 | } |
| 529 | |
| 530 | void ResStringPool::uninit() |
| 531 | { |
| 532 | mError = NO_INIT; |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 533 | if (mHeader != NULL && mCache != NULL) { |
| 534 | for (size_t x = 0; x < mHeader->stringCount; x++) { |
| 535 | if (mCache[x] != NULL) { |
| 536 | free(mCache[x]); |
| 537 | mCache[x] = NULL; |
| 538 | } |
| 539 | } |
| 540 | free(mCache); |
| 541 | mCache = NULL; |
| 542 | } |
Chris Dearman | a1d82ff3 | 2012-10-08 12:22:02 -0700 | [diff] [blame] | 543 | if (mOwnedData) { |
| 544 | free(mOwnedData); |
| 545 | mOwnedData = NULL; |
| 546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 549 | /** |
| 550 | * Strings in UTF-16 format have length indicated by a length encoded in the |
| 551 | * stored data. It is either 1 or 2 characters of length data. This allows a |
| 552 | * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that |
| 553 | * much data in a string, you're abusing them. |
| 554 | * |
| 555 | * If the high bit is set, then there are two characters or 4 bytes of length |
| 556 | * data encoded. In that case, drop the high bit of the first character and |
| 557 | * add it together with the next character. |
| 558 | */ |
| 559 | static inline size_t |
| 560 | decodeLength(const char16_t** str) |
| 561 | { |
| 562 | size_t len = **str; |
| 563 | if ((len & 0x8000) != 0) { |
| 564 | (*str)++; |
| 565 | len = ((len & 0x7FFF) << 16) | **str; |
| 566 | } |
| 567 | (*str)++; |
| 568 | return len; |
| 569 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 570 | |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 571 | /** |
| 572 | * Strings in UTF-8 format have length indicated by a length encoded in the |
| 573 | * stored data. It is either 1 or 2 characters of length data. This allows a |
| 574 | * maximum length of 0x7FFF (32767 bytes), but you should consider storing |
| 575 | * text in another way if you're using that much data in a single string. |
| 576 | * |
| 577 | * If the high bit is set, then there are two characters or 2 bytes of length |
| 578 | * data encoded. In that case, drop the high bit of the first character and |
| 579 | * add it together with the next character. |
| 580 | */ |
| 581 | static inline size_t |
| 582 | decodeLength(const uint8_t** str) |
| 583 | { |
| 584 | size_t len = **str; |
| 585 | if ((len & 0x80) != 0) { |
| 586 | (*str)++; |
| 587 | len = ((len & 0x7F) << 8) | **str; |
| 588 | } |
| 589 | (*str)++; |
| 590 | return len; |
| 591 | } |
| 592 | |
| 593 | const uint16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | { |
| 595 | if (mError == NO_ERROR && idx < mHeader->stringCount) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 596 | const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0; |
| 597 | const uint32_t off = mEntries[idx]/(isUTF8?sizeof(char):sizeof(char16_t)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | if (off < (mStringPoolSize-1)) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 599 | if (!isUTF8) { |
| 600 | const char16_t* strings = (char16_t*)mStrings; |
| 601 | const char16_t* str = strings+off; |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 602 | |
| 603 | *u16len = decodeLength(&str); |
| 604 | if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 605 | return str; |
| 606 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 607 | ALOGW("Bad string block: string #%d extends to %d, past end at %d\n", |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 608 | (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } else { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 611 | const uint8_t* strings = (uint8_t*)mStrings; |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 612 | const uint8_t* u8str = strings+off; |
| 613 | |
| 614 | *u16len = decodeLength(&u8str); |
| 615 | size_t u8len = decodeLength(&u8str); |
| 616 | |
| 617 | // encLen must be less than 0x7FFF due to encoding. |
| 618 | if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 619 | AutoMutex lock(mDecodeLock); |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 620 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 621 | if (mCache == NULL) { |
| 622 | #ifndef HAVE_ANDROID_OS |
| 623 | STRING_POOL_NOISY(ALOGI("CREATING STRING CACHE OF %d bytes", |
| 624 | mHeader->stringCount*sizeof(char16_t**))); |
| 625 | #else |
| 626 | // We do not want to be in this case when actually running Android. |
| 627 | ALOGW("CREATING STRING CACHE OF %d bytes", |
| 628 | mHeader->stringCount*sizeof(char16_t**)); |
| 629 | #endif |
| 630 | mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t**)); |
| 631 | if (mCache == NULL) { |
| 632 | ALOGW("No memory trying to allocate decode cache table of %d bytes\n", |
| 633 | (int)(mHeader->stringCount*sizeof(char16_t**))); |
| 634 | return NULL; |
| 635 | } |
| 636 | } |
| 637 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 638 | if (mCache[idx] != NULL) { |
| 639 | return mCache[idx]; |
| 640 | } |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 641 | |
| 642 | ssize_t actualLen = utf8_to_utf16_length(u8str, u8len); |
| 643 | if (actualLen < 0 || (size_t)actualLen != *u16len) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 644 | ALOGW("Bad string block: string #%lld decoded length is not correct " |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 645 | "%lld vs %llu\n", |
| 646 | (long long)idx, (long long)actualLen, (long long)*u16len); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t)); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 651 | if (!u16str) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 652 | ALOGW("No memory when trying to allocate decode cache for string #%d\n", |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 653 | (int)idx); |
| 654 | return NULL; |
| 655 | } |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 656 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 657 | STRING_POOL_NOISY(ALOGI("Caching UTF8 string: %s", u8str)); |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 658 | utf8_to_utf16(u8str, u8len, u16str); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 659 | mCache[idx] = u16str; |
| 660 | return u16str; |
| 661 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 662 | ALOGW("Bad string block: string #%lld extends to %lld, past end at %lld\n", |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 663 | (long long)idx, (long long)(u8str+u8len-strings), |
| 664 | (long long)mStringPoolSize); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | } |
| 667 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 668 | ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | (int)idx, (int)(off*sizeof(uint16_t)), |
| 670 | (int)(mStringPoolSize*sizeof(uint16_t))); |
| 671 | } |
| 672 | } |
| 673 | return NULL; |
| 674 | } |
| 675 | |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 676 | const char* ResStringPool::string8At(size_t idx, size_t* outLen) const |
| 677 | { |
| 678 | if (mError == NO_ERROR && idx < mHeader->stringCount) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 679 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) == 0) { |
| 680 | return NULL; |
| 681 | } |
| 682 | const uint32_t off = mEntries[idx]/sizeof(char); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 683 | if (off < (mStringPoolSize-1)) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 684 | const uint8_t* strings = (uint8_t*)mStrings; |
| 685 | const uint8_t* str = strings+off; |
| 686 | *outLen = decodeLength(&str); |
| 687 | size_t encLen = decodeLength(&str); |
| 688 | if ((uint32_t)(str+encLen-strings) < mStringPoolSize) { |
| 689 | return (const char*)str; |
| 690 | } else { |
| 691 | ALOGW("Bad string block: string #%d extends to %d, past end at %d\n", |
| 692 | (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 693 | } |
| 694 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 695 | ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n", |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 696 | (int)idx, (int)(off*sizeof(uint16_t)), |
| 697 | (int)(mStringPoolSize*sizeof(uint16_t))); |
| 698 | } |
| 699 | } |
| 700 | return NULL; |
| 701 | } |
| 702 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 703 | const String8 ResStringPool::string8ObjectAt(size_t idx) const |
| 704 | { |
| 705 | size_t len; |
| 706 | const char *str = (const char*)string8At(idx, &len); |
| 707 | if (str != NULL) { |
| 708 | return String8(str); |
| 709 | } |
| 710 | return String8(stringAt(idx, &len)); |
| 711 | } |
| 712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const |
| 714 | { |
| 715 | return styleAt(ref.index); |
| 716 | } |
| 717 | |
| 718 | const ResStringPool_span* ResStringPool::styleAt(size_t idx) const |
| 719 | { |
| 720 | if (mError == NO_ERROR && idx < mHeader->styleCount) { |
| 721 | const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t)); |
| 722 | if (off < mStylePoolSize) { |
| 723 | return (const ResStringPool_span*)(mStyles+off); |
| 724 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 725 | ALOGW("Bad string block: style #%d entry is at %d, past end at %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | (int)idx, (int)(off*sizeof(uint32_t)), |
| 727 | (int)(mStylePoolSize*sizeof(uint32_t))); |
| 728 | } |
| 729 | } |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const |
| 734 | { |
| 735 | if (mError != NO_ERROR) { |
| 736 | return mError; |
| 737 | } |
| 738 | |
| 739 | size_t len; |
| 740 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 741 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) { |
| 742 | STRING_POOL_NOISY(ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string())); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 743 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 744 | // The string pool contains UTF 8 strings; we don't want to cause |
| 745 | // temporary UTF-16 strings to be created as we search. |
| 746 | if (mHeader->flags&ResStringPool_header::SORTED_FLAG) { |
| 747 | // Do a binary search for the string... this is a little tricky, |
| 748 | // because the strings are sorted with strzcmp16(). So to match |
| 749 | // the ordering, we need to convert strings in the pool to UTF-16. |
| 750 | // But we don't want to hit the cache, so instead we will have a |
| 751 | // local temporary allocation for the conversions. |
| 752 | char16_t* convBuffer = (char16_t*)malloc(strLen+4); |
| 753 | ssize_t l = 0; |
| 754 | ssize_t h = mHeader->stringCount-1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 756 | ssize_t mid; |
| 757 | while (l <= h) { |
| 758 | mid = l + (h - l)/2; |
| 759 | const uint8_t* s = (const uint8_t*)string8At(mid, &len); |
| 760 | int c; |
| 761 | if (s != NULL) { |
| 762 | char16_t* end = utf8_to_utf16_n(s, len, convBuffer, strLen+3); |
| 763 | *end = 0; |
| 764 | c = strzcmp16(convBuffer, end-convBuffer, str, strLen); |
| 765 | } else { |
| 766 | c = -1; |
| 767 | } |
| 768 | STRING_POOL_NOISY(ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n", |
| 769 | (const char*)s, c, (int)l, (int)mid, (int)h)); |
| 770 | if (c == 0) { |
| 771 | STRING_POOL_NOISY(ALOGI("MATCH!")); |
| 772 | free(convBuffer); |
| 773 | return mid; |
| 774 | } else if (c < 0) { |
| 775 | l = mid + 1; |
| 776 | } else { |
| 777 | h = mid - 1; |
| 778 | } |
| 779 | } |
| 780 | free(convBuffer); |
| 781 | } else { |
| 782 | // It is unusual to get the ID from an unsorted string block... |
| 783 | // most often this happens because we want to get IDs for style |
| 784 | // span tags; since those always appear at the end of the string |
| 785 | // block, start searching at the back. |
| 786 | String8 str8(str, strLen); |
| 787 | const size_t str8Len = str8.size(); |
| 788 | for (int i=mHeader->stringCount-1; i>=0; i--) { |
| 789 | const char* s = string8At(i, &len); |
| 790 | STRING_POOL_NOISY(ALOGI("Looking at %s, i=%d\n", |
| 791 | String8(s).string(), |
| 792 | i)); |
| 793 | if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) { |
| 794 | STRING_POOL_NOISY(ALOGI("MATCH!")); |
| 795 | return i; |
| 796 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } else { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 801 | STRING_POOL_NOISY(ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string())); |
| 802 | |
| 803 | if (mHeader->flags&ResStringPool_header::SORTED_FLAG) { |
| 804 | // Do a binary search for the string... |
| 805 | ssize_t l = 0; |
| 806 | ssize_t h = mHeader->stringCount-1; |
| 807 | |
| 808 | ssize_t mid; |
| 809 | while (l <= h) { |
| 810 | mid = l + (h - l)/2; |
| 811 | const char16_t* s = stringAt(mid, &len); |
| 812 | int c = s ? strzcmp16(s, len, str, strLen) : -1; |
| 813 | STRING_POOL_NOISY(ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n", |
| 814 | String8(s).string(), |
| 815 | c, (int)l, (int)mid, (int)h)); |
| 816 | if (c == 0) { |
| 817 | STRING_POOL_NOISY(ALOGI("MATCH!")); |
| 818 | return mid; |
| 819 | } else if (c < 0) { |
| 820 | l = mid + 1; |
| 821 | } else { |
| 822 | h = mid - 1; |
| 823 | } |
| 824 | } |
| 825 | } else { |
| 826 | // It is unusual to get the ID from an unsorted string block... |
| 827 | // most often this happens because we want to get IDs for style |
| 828 | // span tags; since those always appear at the end of the string |
| 829 | // block, start searching at the back. |
| 830 | for (int i=mHeader->stringCount-1; i>=0; i--) { |
| 831 | const char16_t* s = stringAt(i, &len); |
| 832 | STRING_POOL_NOISY(ALOGI("Looking at %s, i=%d\n", |
| 833 | String8(s).string(), |
| 834 | i)); |
| 835 | if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) { |
| 836 | STRING_POOL_NOISY(ALOGI("MATCH!")); |
| 837 | return i; |
| 838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return NAME_NOT_FOUND; |
| 844 | } |
| 845 | |
| 846 | size_t ResStringPool::size() const |
| 847 | { |
| 848 | return (mError == NO_ERROR) ? mHeader->stringCount : 0; |
| 849 | } |
| 850 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 851 | size_t ResStringPool::styleCount() const |
| 852 | { |
| 853 | return (mError == NO_ERROR) ? mHeader->styleCount : 0; |
| 854 | } |
| 855 | |
| 856 | size_t ResStringPool::bytes() const |
| 857 | { |
| 858 | return (mError == NO_ERROR) ? mHeader->header.size : 0; |
| 859 | } |
| 860 | |
| 861 | bool ResStringPool::isSorted() const |
| 862 | { |
| 863 | return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0; |
| 864 | } |
| 865 | |
Kenny Root | bb79f64 | 2009-12-10 14:20:15 -0800 | [diff] [blame] | 866 | bool ResStringPool::isUTF8() const |
| 867 | { |
| 868 | return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0; |
| 869 | } |
Kenny Root | bb79f64 | 2009-12-10 14:20:15 -0800 | [diff] [blame] | 870 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | // -------------------------------------------------------------------- |
| 872 | // -------------------------------------------------------------------- |
| 873 | // -------------------------------------------------------------------- |
| 874 | |
| 875 | ResXMLParser::ResXMLParser(const ResXMLTree& tree) |
| 876 | : mTree(tree), mEventCode(BAD_DOCUMENT) |
| 877 | { |
| 878 | } |
| 879 | |
| 880 | void ResXMLParser::restart() |
| 881 | { |
| 882 | mCurNode = NULL; |
| 883 | mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT; |
| 884 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 885 | const ResStringPool& ResXMLParser::getStrings() const |
| 886 | { |
| 887 | return mTree.mStrings; |
| 888 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | |
| 890 | ResXMLParser::event_code_t ResXMLParser::getEventType() const |
| 891 | { |
| 892 | return mEventCode; |
| 893 | } |
| 894 | |
| 895 | ResXMLParser::event_code_t ResXMLParser::next() |
| 896 | { |
| 897 | if (mEventCode == START_DOCUMENT) { |
| 898 | mCurNode = mTree.mRootNode; |
| 899 | mCurExt = mTree.mRootExt; |
| 900 | return (mEventCode=mTree.mRootCode); |
| 901 | } else if (mEventCode >= FIRST_CHUNK_CODE) { |
| 902 | return nextNode(); |
| 903 | } |
| 904 | return mEventCode; |
| 905 | } |
| 906 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 907 | int32_t ResXMLParser::getCommentID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | { |
| 909 | return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1; |
| 910 | } |
| 911 | |
| 912 | const uint16_t* ResXMLParser::getComment(size_t* outLen) const |
| 913 | { |
| 914 | int32_t id = getCommentID(); |
| 915 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 916 | } |
| 917 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 918 | uint32_t ResXMLParser::getLineNumber() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | { |
| 920 | return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1; |
| 921 | } |
| 922 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 923 | int32_t ResXMLParser::getTextID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | { |
| 925 | if (mEventCode == TEXT) { |
| 926 | return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index); |
| 927 | } |
| 928 | return -1; |
| 929 | } |
| 930 | |
| 931 | const uint16_t* ResXMLParser::getText(size_t* outLen) const |
| 932 | { |
| 933 | int32_t id = getTextID(); |
| 934 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 935 | } |
| 936 | |
| 937 | ssize_t ResXMLParser::getTextValue(Res_value* outValue) const |
| 938 | { |
| 939 | if (mEventCode == TEXT) { |
| 940 | outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData); |
| 941 | return sizeof(Res_value); |
| 942 | } |
| 943 | return BAD_TYPE; |
| 944 | } |
| 945 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 946 | int32_t ResXMLParser::getNamespacePrefixID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | { |
| 948 | if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { |
| 949 | return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index); |
| 950 | } |
| 951 | return -1; |
| 952 | } |
| 953 | |
| 954 | const uint16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const |
| 955 | { |
| 956 | int32_t id = getNamespacePrefixID(); |
| 957 | //printf("prefix=%d event=%p\n", id, mEventCode); |
| 958 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 959 | } |
| 960 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 961 | int32_t ResXMLParser::getNamespaceUriID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | { |
| 963 | if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { |
| 964 | return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index); |
| 965 | } |
| 966 | return -1; |
| 967 | } |
| 968 | |
| 969 | const uint16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const |
| 970 | { |
| 971 | int32_t id = getNamespaceUriID(); |
| 972 | //printf("uri=%d event=%p\n", id, mEventCode); |
| 973 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 974 | } |
| 975 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 976 | int32_t ResXMLParser::getElementNamespaceID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | { |
| 978 | if (mEventCode == START_TAG) { |
| 979 | return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index); |
| 980 | } |
| 981 | if (mEventCode == END_TAG) { |
| 982 | return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index); |
| 983 | } |
| 984 | return -1; |
| 985 | } |
| 986 | |
| 987 | const uint16_t* ResXMLParser::getElementNamespace(size_t* outLen) const |
| 988 | { |
| 989 | int32_t id = getElementNamespaceID(); |
| 990 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 991 | } |
| 992 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 993 | int32_t ResXMLParser::getElementNameID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | { |
| 995 | if (mEventCode == START_TAG) { |
| 996 | return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index); |
| 997 | } |
| 998 | if (mEventCode == END_TAG) { |
| 999 | return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index); |
| 1000 | } |
| 1001 | return -1; |
| 1002 | } |
| 1003 | |
| 1004 | const uint16_t* ResXMLParser::getElementName(size_t* outLen) const |
| 1005 | { |
| 1006 | int32_t id = getElementNameID(); |
| 1007 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1008 | } |
| 1009 | |
| 1010 | size_t ResXMLParser::getAttributeCount() const |
| 1011 | { |
| 1012 | if (mEventCode == START_TAG) { |
| 1013 | return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount); |
| 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1018 | int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | { |
| 1020 | if (mEventCode == START_TAG) { |
| 1021 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1022 | if (idx < dtohs(tag->attributeCount)) { |
| 1023 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1024 | (((const uint8_t*)tag) |
| 1025 | + dtohs(tag->attributeStart) |
| 1026 | + (dtohs(tag->attributeSize)*idx)); |
| 1027 | return dtohl(attr->ns.index); |
| 1028 | } |
| 1029 | } |
| 1030 | return -2; |
| 1031 | } |
| 1032 | |
| 1033 | const uint16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const |
| 1034 | { |
| 1035 | int32_t id = getAttributeNamespaceID(idx); |
| 1036 | //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode); |
| 1037 | //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id)); |
| 1038 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1039 | } |
| 1040 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1041 | const char* ResXMLParser::getAttributeNamespace8(size_t idx, size_t* outLen) const |
| 1042 | { |
| 1043 | int32_t id = getAttributeNamespaceID(idx); |
| 1044 | //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode); |
| 1045 | //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id)); |
| 1046 | return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL; |
| 1047 | } |
| 1048 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1049 | int32_t ResXMLParser::getAttributeNameID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | { |
| 1051 | if (mEventCode == START_TAG) { |
| 1052 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1053 | if (idx < dtohs(tag->attributeCount)) { |
| 1054 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1055 | (((const uint8_t*)tag) |
| 1056 | + dtohs(tag->attributeStart) |
| 1057 | + (dtohs(tag->attributeSize)*idx)); |
| 1058 | return dtohl(attr->name.index); |
| 1059 | } |
| 1060 | } |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | const uint16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const |
| 1065 | { |
| 1066 | int32_t id = getAttributeNameID(idx); |
| 1067 | //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode); |
| 1068 | //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id)); |
| 1069 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1070 | } |
| 1071 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1072 | const char* ResXMLParser::getAttributeName8(size_t idx, size_t* outLen) const |
| 1073 | { |
| 1074 | int32_t id = getAttributeNameID(idx); |
| 1075 | //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode); |
| 1076 | //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id)); |
| 1077 | return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL; |
| 1078 | } |
| 1079 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1080 | uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | { |
| 1082 | int32_t id = getAttributeNameID(idx); |
| 1083 | if (id >= 0 && (size_t)id < mTree.mNumResIds) { |
| 1084 | return dtohl(mTree.mResIds[id]); |
| 1085 | } |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1089 | int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | { |
| 1091 | if (mEventCode == START_TAG) { |
| 1092 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1093 | if (idx < dtohs(tag->attributeCount)) { |
| 1094 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1095 | (((const uint8_t*)tag) |
| 1096 | + dtohs(tag->attributeStart) |
| 1097 | + (dtohs(tag->attributeSize)*idx)); |
| 1098 | return dtohl(attr->rawValue.index); |
| 1099 | } |
| 1100 | } |
| 1101 | return -1; |
| 1102 | } |
| 1103 | |
| 1104 | const uint16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const |
| 1105 | { |
| 1106 | int32_t id = getAttributeValueStringID(idx); |
| 1107 | //XML_NOISY(printf("getAttributeValue 0x%x=0x%x\n", idx, id)); |
| 1108 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1109 | } |
| 1110 | |
| 1111 | int32_t ResXMLParser::getAttributeDataType(size_t idx) const |
| 1112 | { |
| 1113 | if (mEventCode == START_TAG) { |
| 1114 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1115 | if (idx < dtohs(tag->attributeCount)) { |
| 1116 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1117 | (((const uint8_t*)tag) |
| 1118 | + dtohs(tag->attributeStart) |
| 1119 | + (dtohs(tag->attributeSize)*idx)); |
| 1120 | return attr->typedValue.dataType; |
| 1121 | } |
| 1122 | } |
| 1123 | return Res_value::TYPE_NULL; |
| 1124 | } |
| 1125 | |
| 1126 | int32_t ResXMLParser::getAttributeData(size_t idx) const |
| 1127 | { |
| 1128 | if (mEventCode == START_TAG) { |
| 1129 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1130 | if (idx < dtohs(tag->attributeCount)) { |
| 1131 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1132 | (((const uint8_t*)tag) |
| 1133 | + dtohs(tag->attributeStart) |
| 1134 | + (dtohs(tag->attributeSize)*idx)); |
| 1135 | return dtohl(attr->typedValue.data); |
| 1136 | } |
| 1137 | } |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const |
| 1142 | { |
| 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 | outValue->copyFrom_dtoh(attr->typedValue); |
| 1151 | return sizeof(Res_value); |
| 1152 | } |
| 1153 | } |
| 1154 | return BAD_TYPE; |
| 1155 | } |
| 1156 | |
| 1157 | ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const |
| 1158 | { |
| 1159 | String16 nsStr(ns != NULL ? ns : ""); |
| 1160 | String16 attrStr(attr); |
| 1161 | return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0, |
| 1162 | attrStr.string(), attrStr.size()); |
| 1163 | } |
| 1164 | |
| 1165 | ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen, |
| 1166 | const char16_t* attr, size_t attrLen) const |
| 1167 | { |
| 1168 | if (mEventCode == START_TAG) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1169 | if (attr == NULL) { |
| 1170 | return NAME_NOT_FOUND; |
| 1171 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | const size_t N = getAttributeCount(); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1173 | if (mTree.mStrings.isUTF8()) { |
| 1174 | String8 ns8, attr8; |
| 1175 | if (ns != NULL) { |
| 1176 | ns8 = String8(ns, nsLen); |
| 1177 | } |
| 1178 | attr8 = String8(attr, attrLen); |
| 1179 | STRING_POOL_NOISY(ALOGI("indexOfAttribute UTF8 %s (%d) / %s (%d)", ns8.string(), nsLen, |
| 1180 | attr8.string(), attrLen)); |
| 1181 | for (size_t i=0; i<N; i++) { |
| 1182 | size_t curNsLen = 0, curAttrLen = 0; |
| 1183 | const char* curNs = getAttributeNamespace8(i, &curNsLen); |
| 1184 | const char* curAttr = getAttributeName8(i, &curAttrLen); |
| 1185 | STRING_POOL_NOISY(ALOGI(" curNs=%s (%d), curAttr=%s (%d)", curNs, curNsLen, |
| 1186 | curAttr, curAttrLen)); |
| 1187 | if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen |
| 1188 | && memcmp(attr8.string(), curAttr, attrLen) == 0) { |
| 1189 | if (ns == NULL) { |
| 1190 | if (curNs == NULL) { |
| 1191 | STRING_POOL_NOISY(ALOGI(" FOUND!")); |
| 1192 | return i; |
| 1193 | } |
| 1194 | } else if (curNs != NULL) { |
| 1195 | //printf(" --> ns=%s, curNs=%s\n", |
| 1196 | // String8(ns).string(), String8(curNs).string()); |
| 1197 | if (memcmp(ns8.string(), curNs, nsLen) == 0) { |
| 1198 | STRING_POOL_NOISY(ALOGI(" FOUND!")); |
| 1199 | return i; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } else { |
| 1205 | STRING_POOL_NOISY(ALOGI("indexOfAttribute UTF16 %s (%d) / %s (%d)", |
| 1206 | String8(ns, nsLen).string(), nsLen, |
| 1207 | String8(attr, attrLen).string(), attrLen)); |
| 1208 | for (size_t i=0; i<N; i++) { |
| 1209 | size_t curNsLen = 0, curAttrLen = 0; |
| 1210 | const char16_t* curNs = getAttributeNamespace(i, &curNsLen); |
| 1211 | const char16_t* curAttr = getAttributeName(i, &curAttrLen); |
| 1212 | STRING_POOL_NOISY(ALOGI(" curNs=%s (%d), curAttr=%s (%d)", |
| 1213 | String8(curNs, curNsLen).string(), curNsLen, |
| 1214 | String8(curAttr, curAttrLen).string(), curAttrLen)); |
| 1215 | if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen |
| 1216 | && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) { |
| 1217 | if (ns == NULL) { |
| 1218 | if (curNs == NULL) { |
| 1219 | STRING_POOL_NOISY(ALOGI(" FOUND!")); |
| 1220 | return i; |
| 1221 | } |
| 1222 | } else if (curNs != NULL) { |
| 1223 | //printf(" --> ns=%s, curNs=%s\n", |
| 1224 | // String8(ns).string(), String8(curNs).string()); |
| 1225 | if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) { |
| 1226 | STRING_POOL_NOISY(ALOGI(" FOUND!")); |
| 1227 | return i; |
| 1228 | } |
| 1229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | return NAME_NOT_FOUND; |
| 1236 | } |
| 1237 | |
| 1238 | ssize_t ResXMLParser::indexOfID() const |
| 1239 | { |
| 1240 | if (mEventCode == START_TAG) { |
| 1241 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex); |
| 1242 | if (idx > 0) return (idx-1); |
| 1243 | } |
| 1244 | return NAME_NOT_FOUND; |
| 1245 | } |
| 1246 | |
| 1247 | ssize_t ResXMLParser::indexOfClass() const |
| 1248 | { |
| 1249 | if (mEventCode == START_TAG) { |
| 1250 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex); |
| 1251 | if (idx > 0) return (idx-1); |
| 1252 | } |
| 1253 | return NAME_NOT_FOUND; |
| 1254 | } |
| 1255 | |
| 1256 | ssize_t ResXMLParser::indexOfStyle() const |
| 1257 | { |
| 1258 | if (mEventCode == START_TAG) { |
| 1259 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex); |
| 1260 | if (idx > 0) return (idx-1); |
| 1261 | } |
| 1262 | return NAME_NOT_FOUND; |
| 1263 | } |
| 1264 | |
| 1265 | ResXMLParser::event_code_t ResXMLParser::nextNode() |
| 1266 | { |
| 1267 | if (mEventCode < 0) { |
| 1268 | return mEventCode; |
| 1269 | } |
| 1270 | |
| 1271 | do { |
| 1272 | const ResXMLTree_node* next = (const ResXMLTree_node*) |
| 1273 | (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size)); |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1274 | //ALOGW("Next node: prev=%p, next=%p\n", mCurNode, next); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | |
| 1276 | if (((const uint8_t*)next) >= mTree.mDataEnd) { |
| 1277 | mCurNode = NULL; |
| 1278 | return (mEventCode=END_DOCUMENT); |
| 1279 | } |
| 1280 | |
| 1281 | if (mTree.validateNode(next) != NO_ERROR) { |
| 1282 | mCurNode = NULL; |
| 1283 | return (mEventCode=BAD_DOCUMENT); |
| 1284 | } |
| 1285 | |
| 1286 | mCurNode = next; |
| 1287 | const uint16_t headerSize = dtohs(next->header.headerSize); |
| 1288 | const uint32_t totalSize = dtohl(next->header.size); |
| 1289 | mCurExt = ((const uint8_t*)next) + headerSize; |
| 1290 | size_t minExtSize = 0; |
| 1291 | event_code_t eventCode = (event_code_t)dtohs(next->header.type); |
| 1292 | switch ((mEventCode=eventCode)) { |
| 1293 | case RES_XML_START_NAMESPACE_TYPE: |
| 1294 | case RES_XML_END_NAMESPACE_TYPE: |
| 1295 | minExtSize = sizeof(ResXMLTree_namespaceExt); |
| 1296 | break; |
| 1297 | case RES_XML_START_ELEMENT_TYPE: |
| 1298 | minExtSize = sizeof(ResXMLTree_attrExt); |
| 1299 | break; |
| 1300 | case RES_XML_END_ELEMENT_TYPE: |
| 1301 | minExtSize = sizeof(ResXMLTree_endElementExt); |
| 1302 | break; |
| 1303 | case RES_XML_CDATA_TYPE: |
| 1304 | minExtSize = sizeof(ResXMLTree_cdataExt); |
| 1305 | break; |
| 1306 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1307 | ALOGW("Unknown XML block: header type %d in node at %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | (int)dtohs(next->header.type), |
| 1309 | (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader))); |
| 1310 | continue; |
| 1311 | } |
| 1312 | |
| 1313 | if ((totalSize-headerSize) < minExtSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1314 | ALOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | (int)dtohs(next->header.type), |
| 1316 | (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)), |
| 1317 | (int)(totalSize-headerSize), (int)minExtSize); |
| 1318 | return (mEventCode=BAD_DOCUMENT); |
| 1319 | } |
| 1320 | |
| 1321 | //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n", |
| 1322 | // mCurNode, mCurExt, headerSize, minExtSize); |
| 1323 | |
| 1324 | return eventCode; |
| 1325 | } while (true); |
| 1326 | } |
| 1327 | |
| 1328 | void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const |
| 1329 | { |
| 1330 | pos->eventCode = mEventCode; |
| 1331 | pos->curNode = mCurNode; |
| 1332 | pos->curExt = mCurExt; |
| 1333 | } |
| 1334 | |
| 1335 | void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos) |
| 1336 | { |
| 1337 | mEventCode = pos.eventCode; |
| 1338 | mCurNode = pos.curNode; |
| 1339 | mCurExt = pos.curExt; |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | // -------------------------------------------------------------------- |
| 1344 | |
| 1345 | static volatile int32_t gCount = 0; |
| 1346 | |
| 1347 | ResXMLTree::ResXMLTree() |
| 1348 | : ResXMLParser(*this) |
| 1349 | , mError(NO_INIT), mOwnedData(NULL) |
| 1350 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1351 | //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | restart(); |
| 1353 | } |
| 1354 | |
| 1355 | ResXMLTree::ResXMLTree(const void* data, size_t size, bool copyData) |
| 1356 | : ResXMLParser(*this) |
| 1357 | , mError(NO_INIT), mOwnedData(NULL) |
| 1358 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1359 | //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | setTo(data, size, copyData); |
| 1361 | } |
| 1362 | |
| 1363 | ResXMLTree::~ResXMLTree() |
| 1364 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1365 | //ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | uninit(); |
| 1367 | } |
| 1368 | |
| 1369 | status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData) |
| 1370 | { |
| 1371 | uninit(); |
| 1372 | mEventCode = START_DOCUMENT; |
| 1373 | |
Kenny Root | 32d6aef | 2012-10-10 10:23:47 -0700 | [diff] [blame] | 1374 | if (!data || !size) { |
| 1375 | return (mError=BAD_TYPE); |
| 1376 | } |
| 1377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | if (copyData) { |
| 1379 | mOwnedData = malloc(size); |
| 1380 | if (mOwnedData == NULL) { |
| 1381 | return (mError=NO_MEMORY); |
| 1382 | } |
| 1383 | memcpy(mOwnedData, data, size); |
| 1384 | data = mOwnedData; |
| 1385 | } |
| 1386 | |
| 1387 | mHeader = (const ResXMLTree_header*)data; |
| 1388 | mSize = dtohl(mHeader->header.size); |
| 1389 | if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1390 | ALOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | (int)dtohs(mHeader->header.headerSize), |
| 1392 | (int)dtohl(mHeader->header.size), (int)size); |
| 1393 | mError = BAD_TYPE; |
| 1394 | restart(); |
| 1395 | return mError; |
| 1396 | } |
| 1397 | mDataEnd = ((const uint8_t*)mHeader) + mSize; |
| 1398 | |
| 1399 | mStrings.uninit(); |
| 1400 | mRootNode = NULL; |
| 1401 | mResIds = NULL; |
| 1402 | mNumResIds = 0; |
| 1403 | |
| 1404 | // First look for a couple interesting chunks: the string block |
| 1405 | // and first XML node. |
| 1406 | const ResChunk_header* chunk = |
| 1407 | (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize)); |
| 1408 | const ResChunk_header* lastChunk = chunk; |
| 1409 | while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) && |
| 1410 | ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) { |
| 1411 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML"); |
| 1412 | if (err != NO_ERROR) { |
| 1413 | mError = err; |
| 1414 | goto done; |
| 1415 | } |
| 1416 | const uint16_t type = dtohs(chunk->type); |
| 1417 | const size_t size = dtohl(chunk->size); |
| 1418 | XML_NOISY(printf("Scanning @ %p: type=0x%x, size=0x%x\n", |
| 1419 | (void*)(((uint32_t)chunk)-((uint32_t)mHeader)), type, size)); |
| 1420 | if (type == RES_STRING_POOL_TYPE) { |
| 1421 | mStrings.setTo(chunk, size); |
| 1422 | } else if (type == RES_XML_RESOURCE_MAP_TYPE) { |
| 1423 | mResIds = (const uint32_t*) |
| 1424 | (((const uint8_t*)chunk)+dtohs(chunk->headerSize)); |
| 1425 | mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t); |
| 1426 | } else if (type >= RES_XML_FIRST_CHUNK_TYPE |
| 1427 | && type <= RES_XML_LAST_CHUNK_TYPE) { |
| 1428 | if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) { |
| 1429 | mError = BAD_TYPE; |
| 1430 | goto done; |
| 1431 | } |
| 1432 | mCurNode = (const ResXMLTree_node*)lastChunk; |
| 1433 | if (nextNode() == BAD_DOCUMENT) { |
| 1434 | mError = BAD_TYPE; |
| 1435 | goto done; |
| 1436 | } |
| 1437 | mRootNode = mCurNode; |
| 1438 | mRootExt = mCurExt; |
| 1439 | mRootCode = mEventCode; |
| 1440 | break; |
| 1441 | } else { |
| 1442 | XML_NOISY(printf("Skipping unknown chunk!\n")); |
| 1443 | } |
| 1444 | lastChunk = chunk; |
| 1445 | chunk = (const ResChunk_header*) |
| 1446 | (((const uint8_t*)chunk) + size); |
| 1447 | } |
| 1448 | |
| 1449 | if (mRootNode == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1450 | ALOGW("Bad XML block: no root element node found\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | mError = BAD_TYPE; |
| 1452 | goto done; |
| 1453 | } |
| 1454 | |
| 1455 | mError = mStrings.getError(); |
| 1456 | |
| 1457 | done: |
| 1458 | restart(); |
| 1459 | return mError; |
| 1460 | } |
| 1461 | |
| 1462 | status_t ResXMLTree::getError() const |
| 1463 | { |
| 1464 | return mError; |
| 1465 | } |
| 1466 | |
| 1467 | void ResXMLTree::uninit() |
| 1468 | { |
| 1469 | mError = NO_INIT; |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1470 | mStrings.uninit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | if (mOwnedData) { |
| 1472 | free(mOwnedData); |
| 1473 | mOwnedData = NULL; |
| 1474 | } |
| 1475 | restart(); |
| 1476 | } |
| 1477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const |
| 1479 | { |
| 1480 | const uint16_t eventCode = dtohs(node->header.type); |
| 1481 | |
| 1482 | status_t err = validate_chunk( |
| 1483 | &node->header, sizeof(ResXMLTree_node), |
| 1484 | mDataEnd, "ResXMLTree_node"); |
| 1485 | |
| 1486 | if (err >= NO_ERROR) { |
| 1487 | // Only perform additional validation on START nodes |
| 1488 | if (eventCode != RES_XML_START_ELEMENT_TYPE) { |
| 1489 | return NO_ERROR; |
| 1490 | } |
| 1491 | |
| 1492 | const uint16_t headerSize = dtohs(node->header.headerSize); |
| 1493 | const uint32_t size = dtohl(node->header.size); |
| 1494 | const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*) |
| 1495 | (((const uint8_t*)node) + headerSize); |
| 1496 | // check for sensical values pulled out of the stream so far... |
| 1497 | if ((size >= headerSize + sizeof(ResXMLTree_attrExt)) |
| 1498 | && ((void*)attrExt > (void*)node)) { |
| 1499 | const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize)) |
| 1500 | * dtohs(attrExt->attributeCount); |
| 1501 | if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) { |
| 1502 | return NO_ERROR; |
| 1503 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1504 | ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | (unsigned int)(dtohs(attrExt->attributeStart)+attrSize), |
| 1506 | (unsigned int)(size-headerSize)); |
| 1507 | } |
| 1508 | else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1509 | ALOGW("Bad XML start block: node header size 0x%x, size 0x%x\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | (unsigned int)headerSize, (unsigned int)size); |
| 1511 | } |
| 1512 | return BAD_TYPE; |
| 1513 | } |
| 1514 | |
| 1515 | return err; |
| 1516 | |
| 1517 | #if 0 |
| 1518 | const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE; |
| 1519 | |
| 1520 | const uint16_t headerSize = dtohs(node->header.headerSize); |
| 1521 | const uint32_t size = dtohl(node->header.size); |
| 1522 | |
| 1523 | if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) { |
| 1524 | if (size >= headerSize) { |
| 1525 | if (((const uint8_t*)node) <= (mDataEnd-size)) { |
| 1526 | if (!isStart) { |
| 1527 | return NO_ERROR; |
| 1528 | } |
| 1529 | if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount)) |
| 1530 | <= (size-headerSize)) { |
| 1531 | return NO_ERROR; |
| 1532 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1533 | ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount), |
| 1535 | (int)(size-headerSize)); |
| 1536 | return BAD_TYPE; |
| 1537 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1538 | ALOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize); |
| 1540 | return BAD_TYPE; |
| 1541 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1542 | ALOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), |
| 1544 | (int)headerSize, (int)size); |
| 1545 | return BAD_TYPE; |
| 1546 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1547 | ALOGW("Bad XML block: node at 0x%x header size 0x%x too small\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), |
| 1549 | (int)headerSize); |
| 1550 | return BAD_TYPE; |
| 1551 | #endif |
| 1552 | } |
| 1553 | |
| 1554 | // -------------------------------------------------------------------- |
| 1555 | // -------------------------------------------------------------------- |
| 1556 | // -------------------------------------------------------------------- |
| 1557 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1558 | void ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) { |
| 1559 | const size_t size = dtohl(o.size); |
| 1560 | if (size >= sizeof(ResTable_config)) { |
| 1561 | *this = o; |
| 1562 | } else { |
| 1563 | memcpy(this, &o, size); |
| 1564 | memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | void ResTable_config::copyFromDtoH(const ResTable_config& o) { |
| 1569 | copyFromDeviceNoSwap(o); |
| 1570 | size = sizeof(ResTable_config); |
| 1571 | mcc = dtohs(mcc); |
| 1572 | mnc = dtohs(mnc); |
| 1573 | density = dtohs(density); |
| 1574 | screenWidth = dtohs(screenWidth); |
| 1575 | screenHeight = dtohs(screenHeight); |
| 1576 | sdkVersion = dtohs(sdkVersion); |
| 1577 | minorVersion = dtohs(minorVersion); |
| 1578 | smallestScreenWidthDp = dtohs(smallestScreenWidthDp); |
| 1579 | screenWidthDp = dtohs(screenWidthDp); |
| 1580 | screenHeightDp = dtohs(screenHeightDp); |
| 1581 | } |
| 1582 | |
| 1583 | void ResTable_config::swapHtoD() { |
| 1584 | size = htodl(size); |
| 1585 | mcc = htods(mcc); |
| 1586 | mnc = htods(mnc); |
| 1587 | density = htods(density); |
| 1588 | screenWidth = htods(screenWidth); |
| 1589 | screenHeight = htods(screenHeight); |
| 1590 | sdkVersion = htods(sdkVersion); |
| 1591 | minorVersion = htods(minorVersion); |
| 1592 | smallestScreenWidthDp = htods(smallestScreenWidthDp); |
| 1593 | screenWidthDp = htods(screenWidthDp); |
| 1594 | screenHeightDp = htods(screenHeightDp); |
| 1595 | } |
| 1596 | |
| 1597 | int ResTable_config::compare(const ResTable_config& o) const { |
| 1598 | int32_t diff = (int32_t)(imsi - o.imsi); |
| 1599 | if (diff != 0) return diff; |
| 1600 | diff = (int32_t)(locale - o.locale); |
| 1601 | if (diff != 0) return diff; |
| 1602 | diff = (int32_t)(screenType - o.screenType); |
| 1603 | if (diff != 0) return diff; |
| 1604 | diff = (int32_t)(input - o.input); |
| 1605 | if (diff != 0) return diff; |
| 1606 | diff = (int32_t)(screenSize - o.screenSize); |
| 1607 | if (diff != 0) return diff; |
| 1608 | diff = (int32_t)(version - o.version); |
| 1609 | if (diff != 0) return diff; |
| 1610 | diff = (int32_t)(screenLayout - o.screenLayout); |
| 1611 | if (diff != 0) return diff; |
| 1612 | diff = (int32_t)(uiMode - o.uiMode); |
| 1613 | if (diff != 0) return diff; |
| 1614 | diff = (int32_t)(smallestScreenWidthDp - o.smallestScreenWidthDp); |
| 1615 | if (diff != 0) return diff; |
| 1616 | diff = (int32_t)(screenSizeDp - o.screenSizeDp); |
| 1617 | return (int)diff; |
| 1618 | } |
| 1619 | |
| 1620 | int ResTable_config::compareLogical(const ResTable_config& o) const { |
| 1621 | if (mcc != o.mcc) { |
| 1622 | return mcc < o.mcc ? -1 : 1; |
| 1623 | } |
| 1624 | if (mnc != o.mnc) { |
| 1625 | return mnc < o.mnc ? -1 : 1; |
| 1626 | } |
| 1627 | if (language[0] != o.language[0]) { |
| 1628 | return language[0] < o.language[0] ? -1 : 1; |
| 1629 | } |
| 1630 | if (language[1] != o.language[1]) { |
| 1631 | return language[1] < o.language[1] ? -1 : 1; |
| 1632 | } |
| 1633 | if (country[0] != o.country[0]) { |
| 1634 | return country[0] < o.country[0] ? -1 : 1; |
| 1635 | } |
| 1636 | if (country[1] != o.country[1]) { |
| 1637 | return country[1] < o.country[1] ? -1 : 1; |
| 1638 | } |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1639 | if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) { |
| 1640 | return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1; |
| 1641 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1642 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 1643 | return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1; |
| 1644 | } |
| 1645 | if (screenWidthDp != o.screenWidthDp) { |
| 1646 | return screenWidthDp < o.screenWidthDp ? -1 : 1; |
| 1647 | } |
| 1648 | if (screenHeightDp != o.screenHeightDp) { |
| 1649 | return screenHeightDp < o.screenHeightDp ? -1 : 1; |
| 1650 | } |
| 1651 | if (screenWidth != o.screenWidth) { |
| 1652 | return screenWidth < o.screenWidth ? -1 : 1; |
| 1653 | } |
| 1654 | if (screenHeight != o.screenHeight) { |
| 1655 | return screenHeight < o.screenHeight ? -1 : 1; |
| 1656 | } |
| 1657 | if (density != o.density) { |
| 1658 | return density < o.density ? -1 : 1; |
| 1659 | } |
| 1660 | if (orientation != o.orientation) { |
| 1661 | return orientation < o.orientation ? -1 : 1; |
| 1662 | } |
| 1663 | if (touchscreen != o.touchscreen) { |
| 1664 | return touchscreen < o.touchscreen ? -1 : 1; |
| 1665 | } |
| 1666 | if (input != o.input) { |
| 1667 | return input < o.input ? -1 : 1; |
| 1668 | } |
| 1669 | if (screenLayout != o.screenLayout) { |
| 1670 | return screenLayout < o.screenLayout ? -1 : 1; |
| 1671 | } |
| 1672 | if (uiMode != o.uiMode) { |
| 1673 | return uiMode < o.uiMode ? -1 : 1; |
| 1674 | } |
| 1675 | if (version != o.version) { |
| 1676 | return version < o.version ? -1 : 1; |
| 1677 | } |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | int ResTable_config::diff(const ResTable_config& o) const { |
| 1682 | int diffs = 0; |
| 1683 | if (mcc != o.mcc) diffs |= CONFIG_MCC; |
| 1684 | if (mnc != o.mnc) diffs |= CONFIG_MNC; |
| 1685 | if (locale != o.locale) diffs |= CONFIG_LOCALE; |
| 1686 | if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION; |
| 1687 | if (density != o.density) diffs |= CONFIG_DENSITY; |
| 1688 | if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN; |
| 1689 | if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0) |
| 1690 | diffs |= CONFIG_KEYBOARD_HIDDEN; |
| 1691 | if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD; |
| 1692 | if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION; |
| 1693 | if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE; |
| 1694 | if (version != o.version) diffs |= CONFIG_VERSION; |
Fabrice Di Meglio | 3509935 | 2012-12-12 11:52:03 -0800 | [diff] [blame] | 1695 | if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) diffs |= CONFIG_LAYOUTDIR; |
| 1696 | if ((screenLayout & ~MASK_LAYOUTDIR) != (o.screenLayout & ~MASK_LAYOUTDIR)) diffs |= CONFIG_SCREEN_LAYOUT; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1697 | if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE; |
| 1698 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE; |
| 1699 | if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE; |
| 1700 | return diffs; |
| 1701 | } |
| 1702 | |
| 1703 | bool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const { |
| 1704 | // The order of the following tests defines the importance of one |
| 1705 | // configuration parameter over another. Those tests first are more |
| 1706 | // important, trumping any values in those following them. |
| 1707 | if (imsi || o.imsi) { |
| 1708 | if (mcc != o.mcc) { |
| 1709 | if (!mcc) return false; |
| 1710 | if (!o.mcc) return true; |
| 1711 | } |
| 1712 | |
| 1713 | if (mnc != o.mnc) { |
| 1714 | if (!mnc) return false; |
| 1715 | if (!o.mnc) return true; |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | if (locale || o.locale) { |
| 1720 | if (language[0] != o.language[0]) { |
| 1721 | if (!language[0]) return false; |
| 1722 | if (!o.language[0]) return true; |
| 1723 | } |
| 1724 | |
| 1725 | if (country[0] != o.country[0]) { |
| 1726 | if (!country[0]) return false; |
| 1727 | if (!o.country[0]) return true; |
| 1728 | } |
| 1729 | } |
| 1730 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1731 | if (screenLayout || o.screenLayout) { |
| 1732 | if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) { |
| 1733 | if (!(screenLayout & MASK_LAYOUTDIR)) return false; |
| 1734 | if (!(o.screenLayout & MASK_LAYOUTDIR)) return true; |
| 1735 | } |
| 1736 | } |
| 1737 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1738 | if (smallestScreenWidthDp || o.smallestScreenWidthDp) { |
| 1739 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 1740 | if (!smallestScreenWidthDp) return false; |
| 1741 | if (!o.smallestScreenWidthDp) return true; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | if (screenSizeDp || o.screenSizeDp) { |
| 1746 | if (screenWidthDp != o.screenWidthDp) { |
| 1747 | if (!screenWidthDp) return false; |
| 1748 | if (!o.screenWidthDp) return true; |
| 1749 | } |
| 1750 | |
| 1751 | if (screenHeightDp != o.screenHeightDp) { |
| 1752 | if (!screenHeightDp) return false; |
| 1753 | if (!o.screenHeightDp) return true; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | if (screenLayout || o.screenLayout) { |
| 1758 | if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) { |
| 1759 | if (!(screenLayout & MASK_SCREENSIZE)) return false; |
| 1760 | if (!(o.screenLayout & MASK_SCREENSIZE)) return true; |
| 1761 | } |
| 1762 | if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) { |
| 1763 | if (!(screenLayout & MASK_SCREENLONG)) return false; |
| 1764 | if (!(o.screenLayout & MASK_SCREENLONG)) return true; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | if (orientation != o.orientation) { |
| 1769 | if (!orientation) return false; |
| 1770 | if (!o.orientation) return true; |
| 1771 | } |
| 1772 | |
| 1773 | if (uiMode || o.uiMode) { |
| 1774 | if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) { |
| 1775 | if (!(uiMode & MASK_UI_MODE_TYPE)) return false; |
| 1776 | if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true; |
| 1777 | } |
| 1778 | if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) { |
| 1779 | if (!(uiMode & MASK_UI_MODE_NIGHT)) return false; |
| 1780 | if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | // density is never 'more specific' |
| 1785 | // as the default just equals 160 |
| 1786 | |
| 1787 | if (touchscreen != o.touchscreen) { |
| 1788 | if (!touchscreen) return false; |
| 1789 | if (!o.touchscreen) return true; |
| 1790 | } |
| 1791 | |
| 1792 | if (input || o.input) { |
| 1793 | if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) { |
| 1794 | if (!(inputFlags & MASK_KEYSHIDDEN)) return false; |
| 1795 | if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true; |
| 1796 | } |
| 1797 | |
| 1798 | if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) { |
| 1799 | if (!(inputFlags & MASK_NAVHIDDEN)) return false; |
| 1800 | if (!(o.inputFlags & MASK_NAVHIDDEN)) return true; |
| 1801 | } |
| 1802 | |
| 1803 | if (keyboard != o.keyboard) { |
| 1804 | if (!keyboard) return false; |
| 1805 | if (!o.keyboard) return true; |
| 1806 | } |
| 1807 | |
| 1808 | if (navigation != o.navigation) { |
| 1809 | if (!navigation) return false; |
| 1810 | if (!o.navigation) return true; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | if (screenSize || o.screenSize) { |
| 1815 | if (screenWidth != o.screenWidth) { |
| 1816 | if (!screenWidth) return false; |
| 1817 | if (!o.screenWidth) return true; |
| 1818 | } |
| 1819 | |
| 1820 | if (screenHeight != o.screenHeight) { |
| 1821 | if (!screenHeight) return false; |
| 1822 | if (!o.screenHeight) return true; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | if (version || o.version) { |
| 1827 | if (sdkVersion != o.sdkVersion) { |
| 1828 | if (!sdkVersion) return false; |
| 1829 | if (!o.sdkVersion) return true; |
| 1830 | } |
| 1831 | |
| 1832 | if (minorVersion != o.minorVersion) { |
| 1833 | if (!minorVersion) return false; |
| 1834 | if (!o.minorVersion) return true; |
| 1835 | } |
| 1836 | } |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
| 1840 | bool ResTable_config::isBetterThan(const ResTable_config& o, |
| 1841 | const ResTable_config* requested) const { |
| 1842 | if (requested) { |
| 1843 | if (imsi || o.imsi) { |
| 1844 | if ((mcc != o.mcc) && requested->mcc) { |
| 1845 | return (mcc); |
| 1846 | } |
| 1847 | |
| 1848 | if ((mnc != o.mnc) && requested->mnc) { |
| 1849 | return (mnc); |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | if (locale || o.locale) { |
| 1854 | if ((language[0] != o.language[0]) && requested->language[0]) { |
| 1855 | return (language[0]); |
| 1856 | } |
| 1857 | |
| 1858 | if ((country[0] != o.country[0]) && requested->country[0]) { |
| 1859 | return (country[0]); |
| 1860 | } |
| 1861 | } |
| 1862 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1863 | if (screenLayout || o.screenLayout) { |
| 1864 | if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0 |
| 1865 | && (requested->screenLayout & MASK_LAYOUTDIR)) { |
| 1866 | int myLayoutDir = screenLayout & MASK_LAYOUTDIR; |
| 1867 | int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR; |
| 1868 | return (myLayoutDir > oLayoutDir); |
| 1869 | } |
| 1870 | } |
| 1871 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1872 | if (smallestScreenWidthDp || o.smallestScreenWidthDp) { |
| 1873 | // The configuration closest to the actual size is best. |
| 1874 | // We assume that larger configs have already been filtered |
| 1875 | // out at this point. That means we just want the largest one. |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 1876 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 1877 | return smallestScreenWidthDp > o.smallestScreenWidthDp; |
| 1878 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | if (screenSizeDp || o.screenSizeDp) { |
| 1882 | // "Better" is based on the sum of the difference between both |
| 1883 | // width and height from the requested dimensions. We are |
| 1884 | // assuming the invalid configs (with smaller dimens) have |
| 1885 | // already been filtered. Note that if a particular dimension |
| 1886 | // is unspecified, we will end up with a large value (the |
| 1887 | // difference between 0 and the requested dimension), which is |
| 1888 | // good since we will prefer a config that has specified a |
| 1889 | // dimension value. |
| 1890 | int myDelta = 0, otherDelta = 0; |
| 1891 | if (requested->screenWidthDp) { |
| 1892 | myDelta += requested->screenWidthDp - screenWidthDp; |
| 1893 | otherDelta += requested->screenWidthDp - o.screenWidthDp; |
| 1894 | } |
| 1895 | if (requested->screenHeightDp) { |
| 1896 | myDelta += requested->screenHeightDp - screenHeightDp; |
| 1897 | otherDelta += requested->screenHeightDp - o.screenHeightDp; |
| 1898 | } |
| 1899 | //ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d", |
| 1900 | // screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp, |
| 1901 | // requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 1902 | if (myDelta != otherDelta) { |
| 1903 | return myDelta < otherDelta; |
| 1904 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | if (screenLayout || o.screenLayout) { |
| 1908 | if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0 |
| 1909 | && (requested->screenLayout & MASK_SCREENSIZE)) { |
| 1910 | // A little backwards compatibility here: undefined is |
| 1911 | // considered equivalent to normal. But only if the |
| 1912 | // requested size is at least normal; otherwise, small |
| 1913 | // is better than the default. |
| 1914 | int mySL = (screenLayout & MASK_SCREENSIZE); |
| 1915 | int oSL = (o.screenLayout & MASK_SCREENSIZE); |
| 1916 | int fixedMySL = mySL; |
| 1917 | int fixedOSL = oSL; |
| 1918 | if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) { |
| 1919 | if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL; |
| 1920 | if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL; |
| 1921 | } |
| 1922 | // For screen size, the best match is the one that is |
| 1923 | // closest to the requested screen size, but not over |
| 1924 | // (the not over part is dealt with in match() below). |
| 1925 | if (fixedMySL == fixedOSL) { |
| 1926 | // If the two are the same, but 'this' is actually |
| 1927 | // undefined, then the other is really a better match. |
| 1928 | if (mySL == 0) return false; |
| 1929 | return true; |
| 1930 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 1931 | if (fixedMySL != fixedOSL) { |
| 1932 | return fixedMySL > fixedOSL; |
| 1933 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1934 | } |
| 1935 | if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0 |
| 1936 | && (requested->screenLayout & MASK_SCREENLONG)) { |
| 1937 | return (screenLayout & MASK_SCREENLONG); |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | if ((orientation != o.orientation) && requested->orientation) { |
| 1942 | return (orientation); |
| 1943 | } |
| 1944 | |
| 1945 | if (uiMode || o.uiMode) { |
| 1946 | if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0 |
| 1947 | && (requested->uiMode & MASK_UI_MODE_TYPE)) { |
| 1948 | return (uiMode & MASK_UI_MODE_TYPE); |
| 1949 | } |
| 1950 | if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0 |
| 1951 | && (requested->uiMode & MASK_UI_MODE_NIGHT)) { |
| 1952 | return (uiMode & MASK_UI_MODE_NIGHT); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | if (screenType || o.screenType) { |
| 1957 | if (density != o.density) { |
| 1958 | // density is tough. Any density is potentially useful |
| 1959 | // because the system will scale it. Scaling down |
| 1960 | // is generally better than scaling up. |
| 1961 | // Default density counts as 160dpi (the system default) |
| 1962 | // TODO - remove 160 constants |
| 1963 | int h = (density?density:160); |
| 1964 | int l = (o.density?o.density:160); |
| 1965 | bool bImBigger = true; |
| 1966 | if (l > h) { |
| 1967 | int t = h; |
| 1968 | h = l; |
| 1969 | l = t; |
| 1970 | bImBigger = false; |
| 1971 | } |
| 1972 | |
| 1973 | int reqValue = (requested->density?requested->density:160); |
| 1974 | if (reqValue >= h) { |
| 1975 | // requested value higher than both l and h, give h |
| 1976 | return bImBigger; |
| 1977 | } |
| 1978 | if (l >= reqValue) { |
| 1979 | // requested value lower than both l and h, give l |
| 1980 | return !bImBigger; |
| 1981 | } |
| 1982 | // saying that scaling down is 2x better than up |
| 1983 | if (((2 * l) - reqValue) * h > reqValue * reqValue) { |
| 1984 | return !bImBigger; |
| 1985 | } else { |
| 1986 | return bImBigger; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | if ((touchscreen != o.touchscreen) && requested->touchscreen) { |
| 1991 | return (touchscreen); |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | if (input || o.input) { |
| 1996 | const int keysHidden = inputFlags & MASK_KEYSHIDDEN; |
| 1997 | const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN; |
| 1998 | if (keysHidden != oKeysHidden) { |
| 1999 | const int reqKeysHidden = |
| 2000 | requested->inputFlags & MASK_KEYSHIDDEN; |
| 2001 | if (reqKeysHidden) { |
| 2002 | |
| 2003 | if (!keysHidden) return false; |
| 2004 | if (!oKeysHidden) return true; |
| 2005 | // For compatibility, we count KEYSHIDDEN_NO as being |
| 2006 | // the same as KEYSHIDDEN_SOFT. Here we disambiguate |
| 2007 | // these by making an exact match more specific. |
| 2008 | if (reqKeysHidden == keysHidden) return true; |
| 2009 | if (reqKeysHidden == oKeysHidden) return false; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | const int navHidden = inputFlags & MASK_NAVHIDDEN; |
| 2014 | const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN; |
| 2015 | if (navHidden != oNavHidden) { |
| 2016 | const int reqNavHidden = |
| 2017 | requested->inputFlags & MASK_NAVHIDDEN; |
| 2018 | if (reqNavHidden) { |
| 2019 | |
| 2020 | if (!navHidden) return false; |
| 2021 | if (!oNavHidden) return true; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | if ((keyboard != o.keyboard) && requested->keyboard) { |
| 2026 | return (keyboard); |
| 2027 | } |
| 2028 | |
| 2029 | if ((navigation != o.navigation) && requested->navigation) { |
| 2030 | return (navigation); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | if (screenSize || o.screenSize) { |
| 2035 | // "Better" is based on the sum of the difference between both |
| 2036 | // width and height from the requested dimensions. We are |
| 2037 | // assuming the invalid configs (with smaller sizes) have |
| 2038 | // already been filtered. Note that if a particular dimension |
| 2039 | // is unspecified, we will end up with a large value (the |
| 2040 | // difference between 0 and the requested dimension), which is |
| 2041 | // good since we will prefer a config that has specified a |
| 2042 | // size value. |
| 2043 | int myDelta = 0, otherDelta = 0; |
| 2044 | if (requested->screenWidth) { |
| 2045 | myDelta += requested->screenWidth - screenWidth; |
| 2046 | otherDelta += requested->screenWidth - o.screenWidth; |
| 2047 | } |
| 2048 | if (requested->screenHeight) { |
| 2049 | myDelta += requested->screenHeight - screenHeight; |
| 2050 | otherDelta += requested->screenHeight - o.screenHeight; |
| 2051 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2052 | if (myDelta != otherDelta) { |
| 2053 | return myDelta < otherDelta; |
| 2054 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | if (version || o.version) { |
| 2058 | if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) { |
| 2059 | return (sdkVersion > o.sdkVersion); |
| 2060 | } |
| 2061 | |
| 2062 | if ((minorVersion != o.minorVersion) && |
| 2063 | requested->minorVersion) { |
| 2064 | return (minorVersion); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | return false; |
| 2069 | } |
| 2070 | return isMoreSpecificThan(o); |
| 2071 | } |
| 2072 | |
| 2073 | bool ResTable_config::match(const ResTable_config& settings) const { |
| 2074 | if (imsi != 0) { |
| 2075 | if (mcc != 0 && mcc != settings.mcc) { |
| 2076 | return false; |
| 2077 | } |
| 2078 | if (mnc != 0 && mnc != settings.mnc) { |
| 2079 | return false; |
| 2080 | } |
| 2081 | } |
| 2082 | if (locale != 0) { |
| 2083 | if (language[0] != 0 |
| 2084 | && (language[0] != settings.language[0] |
| 2085 | || language[1] != settings.language[1])) { |
| 2086 | return false; |
| 2087 | } |
| 2088 | if (country[0] != 0 |
| 2089 | && (country[0] != settings.country[0] |
| 2090 | || country[1] != settings.country[1])) { |
| 2091 | return false; |
| 2092 | } |
| 2093 | } |
| 2094 | if (screenConfig != 0) { |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2095 | const int layoutDir = screenLayout&MASK_LAYOUTDIR; |
| 2096 | const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR; |
| 2097 | if (layoutDir != 0 && layoutDir != setLayoutDir) { |
| 2098 | return false; |
| 2099 | } |
| 2100 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2101 | const int screenSize = screenLayout&MASK_SCREENSIZE; |
| 2102 | const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE; |
| 2103 | // Any screen sizes for larger screens than the setting do not |
| 2104 | // match. |
| 2105 | if (screenSize != 0 && screenSize > setScreenSize) { |
| 2106 | return false; |
| 2107 | } |
| 2108 | |
| 2109 | const int screenLong = screenLayout&MASK_SCREENLONG; |
| 2110 | const int setScreenLong = settings.screenLayout&MASK_SCREENLONG; |
| 2111 | if (screenLong != 0 && screenLong != setScreenLong) { |
| 2112 | return false; |
| 2113 | } |
| 2114 | |
| 2115 | const int uiModeType = uiMode&MASK_UI_MODE_TYPE; |
| 2116 | const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE; |
| 2117 | if (uiModeType != 0 && uiModeType != setUiModeType) { |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
| 2121 | const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT; |
| 2122 | const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT; |
| 2123 | if (uiModeNight != 0 && uiModeNight != setUiModeNight) { |
| 2124 | return false; |
| 2125 | } |
| 2126 | |
| 2127 | if (smallestScreenWidthDp != 0 |
| 2128 | && smallestScreenWidthDp > settings.smallestScreenWidthDp) { |
| 2129 | return false; |
| 2130 | } |
| 2131 | } |
| 2132 | if (screenSizeDp != 0) { |
| 2133 | if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) { |
| 2134 | //ALOGI("Filtering out width %d in requested %d", screenWidthDp, settings.screenWidthDp); |
| 2135 | return false; |
| 2136 | } |
| 2137 | if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) { |
| 2138 | //ALOGI("Filtering out height %d in requested %d", screenHeightDp, settings.screenHeightDp); |
| 2139 | return false; |
| 2140 | } |
| 2141 | } |
| 2142 | if (screenType != 0) { |
| 2143 | if (orientation != 0 && orientation != settings.orientation) { |
| 2144 | return false; |
| 2145 | } |
| 2146 | // density always matches - we can scale it. See isBetterThan |
| 2147 | if (touchscreen != 0 && touchscreen != settings.touchscreen) { |
| 2148 | return false; |
| 2149 | } |
| 2150 | } |
| 2151 | if (input != 0) { |
| 2152 | const int keysHidden = inputFlags&MASK_KEYSHIDDEN; |
| 2153 | const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN; |
| 2154 | if (keysHidden != 0 && keysHidden != setKeysHidden) { |
| 2155 | // For compatibility, we count a request for KEYSHIDDEN_NO as also |
| 2156 | // matching the more recent KEYSHIDDEN_SOFT. Basically |
| 2157 | // KEYSHIDDEN_NO means there is some kind of keyboard available. |
| 2158 | //ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden); |
| 2159 | if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) { |
| 2160 | //ALOGI("No match!"); |
| 2161 | return false; |
| 2162 | } |
| 2163 | } |
| 2164 | const int navHidden = inputFlags&MASK_NAVHIDDEN; |
| 2165 | const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN; |
| 2166 | if (navHidden != 0 && navHidden != setNavHidden) { |
| 2167 | return false; |
| 2168 | } |
| 2169 | if (keyboard != 0 && keyboard != settings.keyboard) { |
| 2170 | return false; |
| 2171 | } |
| 2172 | if (navigation != 0 && navigation != settings.navigation) { |
| 2173 | return false; |
| 2174 | } |
| 2175 | } |
| 2176 | if (screenSize != 0) { |
| 2177 | if (screenWidth != 0 && screenWidth > settings.screenWidth) { |
| 2178 | return false; |
| 2179 | } |
| 2180 | if (screenHeight != 0 && screenHeight > settings.screenHeight) { |
| 2181 | return false; |
| 2182 | } |
| 2183 | } |
| 2184 | if (version != 0) { |
| 2185 | if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) { |
| 2186 | return false; |
| 2187 | } |
| 2188 | if (minorVersion != 0 && minorVersion != settings.minorVersion) { |
| 2189 | return false; |
| 2190 | } |
| 2191 | } |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | void ResTable_config::getLocale(char str[6]) const { |
| 2196 | memset(str, 0, 6); |
| 2197 | if (language[0]) { |
| 2198 | str[0] = language[0]; |
| 2199 | str[1] = language[1]; |
| 2200 | if (country[0]) { |
| 2201 | str[2] = '_'; |
| 2202 | str[3] = country[0]; |
| 2203 | str[4] = country[1]; |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | String8 ResTable_config::toString() const { |
| 2209 | String8 res; |
| 2210 | |
| 2211 | if (mcc != 0) { |
| 2212 | if (res.size() > 0) res.append("-"); |
| 2213 | res.appendFormat("%dmcc", dtohs(mcc)); |
| 2214 | } |
| 2215 | if (mnc != 0) { |
| 2216 | if (res.size() > 0) res.append("-"); |
| 2217 | res.appendFormat("%dmnc", dtohs(mnc)); |
| 2218 | } |
| 2219 | if (language[0] != 0) { |
| 2220 | if (res.size() > 0) res.append("-"); |
| 2221 | res.append(language, 2); |
| 2222 | } |
| 2223 | if (country[0] != 0) { |
| 2224 | if (res.size() > 0) res.append("-"); |
| 2225 | res.append(country, 2); |
| 2226 | } |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2227 | if ((screenLayout&MASK_LAYOUTDIR) != 0) { |
| 2228 | if (res.size() > 0) res.append("-"); |
| 2229 | switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) { |
| 2230 | case ResTable_config::LAYOUTDIR_LTR: |
Fabrice Di Meglio | 8a802db | 2012-09-05 13:12:02 -0700 | [diff] [blame] | 2231 | res.append("ldltr"); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2232 | break; |
| 2233 | case ResTable_config::LAYOUTDIR_RTL: |
Fabrice Di Meglio | 8a802db | 2012-09-05 13:12:02 -0700 | [diff] [blame] | 2234 | res.append("ldrtl"); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2235 | break; |
| 2236 | default: |
| 2237 | res.appendFormat("layoutDir=%d", |
| 2238 | dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR)); |
| 2239 | break; |
| 2240 | } |
| 2241 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2242 | if (smallestScreenWidthDp != 0) { |
| 2243 | if (res.size() > 0) res.append("-"); |
| 2244 | res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp)); |
| 2245 | } |
| 2246 | if (screenWidthDp != 0) { |
| 2247 | if (res.size() > 0) res.append("-"); |
| 2248 | res.appendFormat("w%ddp", dtohs(screenWidthDp)); |
| 2249 | } |
| 2250 | if (screenHeightDp != 0) { |
| 2251 | if (res.size() > 0) res.append("-"); |
| 2252 | res.appendFormat("h%ddp", dtohs(screenHeightDp)); |
| 2253 | } |
| 2254 | if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) { |
| 2255 | if (res.size() > 0) res.append("-"); |
| 2256 | switch (screenLayout&ResTable_config::MASK_SCREENSIZE) { |
| 2257 | case ResTable_config::SCREENSIZE_SMALL: |
| 2258 | res.append("small"); |
| 2259 | break; |
| 2260 | case ResTable_config::SCREENSIZE_NORMAL: |
| 2261 | res.append("normal"); |
| 2262 | break; |
| 2263 | case ResTable_config::SCREENSIZE_LARGE: |
| 2264 | res.append("large"); |
| 2265 | break; |
| 2266 | case ResTable_config::SCREENSIZE_XLARGE: |
| 2267 | res.append("xlarge"); |
| 2268 | break; |
| 2269 | default: |
| 2270 | res.appendFormat("screenLayoutSize=%d", |
| 2271 | dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE)); |
| 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | if ((screenLayout&MASK_SCREENLONG) != 0) { |
| 2276 | if (res.size() > 0) res.append("-"); |
| 2277 | switch (screenLayout&ResTable_config::MASK_SCREENLONG) { |
| 2278 | case ResTable_config::SCREENLONG_NO: |
| 2279 | res.append("notlong"); |
| 2280 | break; |
| 2281 | case ResTable_config::SCREENLONG_YES: |
| 2282 | res.append("long"); |
| 2283 | break; |
| 2284 | default: |
| 2285 | res.appendFormat("screenLayoutLong=%d", |
| 2286 | dtohs(screenLayout&ResTable_config::MASK_SCREENLONG)); |
| 2287 | break; |
| 2288 | } |
| 2289 | } |
| 2290 | if (orientation != ORIENTATION_ANY) { |
| 2291 | if (res.size() > 0) res.append("-"); |
| 2292 | switch (orientation) { |
| 2293 | case ResTable_config::ORIENTATION_PORT: |
| 2294 | res.append("port"); |
| 2295 | break; |
| 2296 | case ResTable_config::ORIENTATION_LAND: |
| 2297 | res.append("land"); |
| 2298 | break; |
| 2299 | case ResTable_config::ORIENTATION_SQUARE: |
| 2300 | res.append("square"); |
| 2301 | break; |
| 2302 | default: |
| 2303 | res.appendFormat("orientation=%d", dtohs(orientation)); |
| 2304 | break; |
| 2305 | } |
| 2306 | } |
| 2307 | if ((uiMode&MASK_UI_MODE_TYPE) != UI_MODE_TYPE_ANY) { |
| 2308 | if (res.size() > 0) res.append("-"); |
| 2309 | switch (uiMode&ResTable_config::MASK_UI_MODE_TYPE) { |
| 2310 | case ResTable_config::UI_MODE_TYPE_DESK: |
| 2311 | res.append("desk"); |
| 2312 | break; |
| 2313 | case ResTable_config::UI_MODE_TYPE_CAR: |
| 2314 | res.append("car"); |
| 2315 | break; |
| 2316 | case ResTable_config::UI_MODE_TYPE_TELEVISION: |
| 2317 | res.append("television"); |
| 2318 | break; |
| 2319 | case ResTable_config::UI_MODE_TYPE_APPLIANCE: |
| 2320 | res.append("appliance"); |
| 2321 | break; |
| 2322 | default: |
| 2323 | res.appendFormat("uiModeType=%d", |
| 2324 | dtohs(screenLayout&ResTable_config::MASK_UI_MODE_TYPE)); |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | if ((uiMode&MASK_UI_MODE_NIGHT) != 0) { |
| 2329 | if (res.size() > 0) res.append("-"); |
| 2330 | switch (uiMode&ResTable_config::MASK_UI_MODE_NIGHT) { |
| 2331 | case ResTable_config::UI_MODE_NIGHT_NO: |
| 2332 | res.append("notnight"); |
| 2333 | break; |
| 2334 | case ResTable_config::UI_MODE_NIGHT_YES: |
| 2335 | res.append("night"); |
| 2336 | break; |
| 2337 | default: |
| 2338 | res.appendFormat("uiModeNight=%d", |
| 2339 | dtohs(uiMode&MASK_UI_MODE_NIGHT)); |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | if (density != DENSITY_DEFAULT) { |
| 2344 | if (res.size() > 0) res.append("-"); |
| 2345 | switch (density) { |
| 2346 | case ResTable_config::DENSITY_LOW: |
| 2347 | res.append("ldpi"); |
| 2348 | break; |
| 2349 | case ResTable_config::DENSITY_MEDIUM: |
| 2350 | res.append("mdpi"); |
| 2351 | break; |
| 2352 | case ResTable_config::DENSITY_TV: |
| 2353 | res.append("tvdpi"); |
| 2354 | break; |
| 2355 | case ResTable_config::DENSITY_HIGH: |
| 2356 | res.append("hdpi"); |
| 2357 | break; |
| 2358 | case ResTable_config::DENSITY_XHIGH: |
| 2359 | res.append("xhdpi"); |
| 2360 | break; |
| 2361 | case ResTable_config::DENSITY_XXHIGH: |
| 2362 | res.append("xxhdpi"); |
| 2363 | break; |
| 2364 | case ResTable_config::DENSITY_NONE: |
| 2365 | res.append("nodpi"); |
| 2366 | break; |
| 2367 | default: |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2368 | res.appendFormat("%ddpi", dtohs(density)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2369 | break; |
| 2370 | } |
| 2371 | } |
| 2372 | if (touchscreen != TOUCHSCREEN_ANY) { |
| 2373 | if (res.size() > 0) res.append("-"); |
| 2374 | switch (touchscreen) { |
| 2375 | case ResTable_config::TOUCHSCREEN_NOTOUCH: |
| 2376 | res.append("notouch"); |
| 2377 | break; |
| 2378 | case ResTable_config::TOUCHSCREEN_FINGER: |
| 2379 | res.append("finger"); |
| 2380 | break; |
| 2381 | case ResTable_config::TOUCHSCREEN_STYLUS: |
| 2382 | res.append("stylus"); |
| 2383 | break; |
| 2384 | default: |
| 2385 | res.appendFormat("touchscreen=%d", dtohs(touchscreen)); |
| 2386 | break; |
| 2387 | } |
| 2388 | } |
| 2389 | if (keyboard != KEYBOARD_ANY) { |
| 2390 | if (res.size() > 0) res.append("-"); |
| 2391 | switch (keyboard) { |
| 2392 | case ResTable_config::KEYBOARD_NOKEYS: |
| 2393 | res.append("nokeys"); |
| 2394 | break; |
| 2395 | case ResTable_config::KEYBOARD_QWERTY: |
| 2396 | res.append("qwerty"); |
| 2397 | break; |
| 2398 | case ResTable_config::KEYBOARD_12KEY: |
| 2399 | res.append("12key"); |
| 2400 | break; |
| 2401 | default: |
| 2402 | res.appendFormat("keyboard=%d", dtohs(keyboard)); |
| 2403 | break; |
| 2404 | } |
| 2405 | } |
| 2406 | if ((inputFlags&MASK_KEYSHIDDEN) != 0) { |
| 2407 | if (res.size() > 0) res.append("-"); |
| 2408 | switch (inputFlags&MASK_KEYSHIDDEN) { |
| 2409 | case ResTable_config::KEYSHIDDEN_NO: |
| 2410 | res.append("keysexposed"); |
| 2411 | break; |
| 2412 | case ResTable_config::KEYSHIDDEN_YES: |
| 2413 | res.append("keyshidden"); |
| 2414 | break; |
| 2415 | case ResTable_config::KEYSHIDDEN_SOFT: |
| 2416 | res.append("keyssoft"); |
| 2417 | break; |
| 2418 | } |
| 2419 | } |
| 2420 | if (navigation != NAVIGATION_ANY) { |
| 2421 | if (res.size() > 0) res.append("-"); |
| 2422 | switch (navigation) { |
| 2423 | case ResTable_config::NAVIGATION_NONAV: |
| 2424 | res.append("nonav"); |
| 2425 | break; |
| 2426 | case ResTable_config::NAVIGATION_DPAD: |
| 2427 | res.append("dpad"); |
| 2428 | break; |
| 2429 | case ResTable_config::NAVIGATION_TRACKBALL: |
| 2430 | res.append("trackball"); |
| 2431 | break; |
| 2432 | case ResTable_config::NAVIGATION_WHEEL: |
| 2433 | res.append("wheel"); |
| 2434 | break; |
| 2435 | default: |
| 2436 | res.appendFormat("navigation=%d", dtohs(navigation)); |
| 2437 | break; |
| 2438 | } |
| 2439 | } |
| 2440 | if ((inputFlags&MASK_NAVHIDDEN) != 0) { |
| 2441 | if (res.size() > 0) res.append("-"); |
| 2442 | switch (inputFlags&MASK_NAVHIDDEN) { |
| 2443 | case ResTable_config::NAVHIDDEN_NO: |
| 2444 | res.append("navsexposed"); |
| 2445 | break; |
| 2446 | case ResTable_config::NAVHIDDEN_YES: |
| 2447 | res.append("navhidden"); |
| 2448 | break; |
| 2449 | default: |
| 2450 | res.appendFormat("inputFlagsNavHidden=%d", |
| 2451 | dtohs(inputFlags&MASK_NAVHIDDEN)); |
| 2452 | break; |
| 2453 | } |
| 2454 | } |
| 2455 | if (screenSize != 0) { |
| 2456 | if (res.size() > 0) res.append("-"); |
| 2457 | res.appendFormat("%dx%d", dtohs(screenWidth), dtohs(screenHeight)); |
| 2458 | } |
| 2459 | if (version != 0) { |
| 2460 | if (res.size() > 0) res.append("-"); |
| 2461 | res.appendFormat("v%d", dtohs(sdkVersion)); |
| 2462 | if (minorVersion != 0) { |
| 2463 | res.appendFormat(".%d", dtohs(minorVersion)); |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | return res; |
| 2468 | } |
| 2469 | |
| 2470 | // -------------------------------------------------------------------- |
| 2471 | // -------------------------------------------------------------------- |
| 2472 | // -------------------------------------------------------------------- |
| 2473 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2474 | struct ResTable::Header |
| 2475 | { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 2476 | Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL), |
| 2477 | resourceIDMap(NULL), resourceIDMapSize(0) { } |
| 2478 | |
| 2479 | ~Header() |
| 2480 | { |
| 2481 | free(resourceIDMap); |
| 2482 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2484 | ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2485 | void* ownedData; |
| 2486 | const ResTable_header* header; |
| 2487 | size_t size; |
| 2488 | const uint8_t* dataEnd; |
| 2489 | size_t index; |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2490 | int32_t cookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | |
| 2492 | ResStringPool values; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 2493 | uint32_t* resourceIDMap; |
| 2494 | size_t resourceIDMapSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2495 | }; |
| 2496 | |
| 2497 | struct ResTable::Type |
| 2498 | { |
| 2499 | Type(const Header* _header, const Package* _package, size_t count) |
| 2500 | : header(_header), package(_package), entryCount(count), |
| 2501 | typeSpec(NULL), typeSpecFlags(NULL) { } |
| 2502 | const Header* const header; |
| 2503 | const Package* const package; |
| 2504 | const size_t entryCount; |
| 2505 | const ResTable_typeSpec* typeSpec; |
| 2506 | const uint32_t* typeSpecFlags; |
| 2507 | Vector<const ResTable_type*> configs; |
| 2508 | }; |
| 2509 | |
| 2510 | struct ResTable::Package |
| 2511 | { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2512 | Package(ResTable* _owner, const Header* _header, const ResTable_package* _package) |
| 2513 | : owner(_owner), header(_header), package(_package) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2514 | ~Package() |
| 2515 | { |
| 2516 | size_t i = types.size(); |
| 2517 | while (i > 0) { |
| 2518 | i--; |
| 2519 | delete types[i]; |
| 2520 | } |
| 2521 | } |
| 2522 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2523 | ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2524 | const Header* const header; |
| 2525 | const ResTable_package* const package; |
| 2526 | Vector<Type*> types; |
| 2527 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2528 | ResStringPool typeStrings; |
| 2529 | ResStringPool keyStrings; |
| 2530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2531 | const Type* getType(size_t idx) const { |
| 2532 | return idx < types.size() ? types[idx] : NULL; |
| 2533 | } |
| 2534 | }; |
| 2535 | |
| 2536 | // A group of objects describing a particular resource package. |
| 2537 | // The first in 'package' is always the root object (from the resource |
| 2538 | // table that defined the package); the ones after are skins on top of it. |
| 2539 | struct ResTable::PackageGroup |
| 2540 | { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2541 | PackageGroup(ResTable* _owner, const String16& _name, uint32_t _id) |
| 2542 | : owner(_owner), name(_name), id(_id), typeCount(0), bags(NULL) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2543 | ~PackageGroup() { |
| 2544 | clearBagCache(); |
| 2545 | const size_t N = packages.size(); |
| 2546 | for (size_t i=0; i<N; i++) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2547 | Package* pkg = packages[i]; |
| 2548 | if (pkg->owner == owner) { |
| 2549 | delete pkg; |
| 2550 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | void clearBagCache() { |
| 2555 | if (bags) { |
| 2556 | TABLE_NOISY(printf("bags=%p\n", bags)); |
| 2557 | Package* pkg = packages[0]; |
| 2558 | TABLE_NOISY(printf("typeCount=%x\n", typeCount)); |
| 2559 | for (size_t i=0; i<typeCount; i++) { |
| 2560 | TABLE_NOISY(printf("type=%d\n", i)); |
| 2561 | const Type* type = pkg->getType(i); |
| 2562 | if (type != NULL) { |
| 2563 | bag_set** typeBags = bags[i]; |
| 2564 | TABLE_NOISY(printf("typeBags=%p\n", typeBags)); |
| 2565 | if (typeBags) { |
| 2566 | TABLE_NOISY(printf("type->entryCount=%x\n", type->entryCount)); |
| 2567 | const size_t N = type->entryCount; |
| 2568 | for (size_t j=0; j<N; j++) { |
| 2569 | if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF) |
| 2570 | free(typeBags[j]); |
| 2571 | } |
| 2572 | free(typeBags); |
| 2573 | } |
| 2574 | } |
| 2575 | } |
| 2576 | free(bags); |
| 2577 | bags = NULL; |
| 2578 | } |
| 2579 | } |
| 2580 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2581 | ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2582 | String16 const name; |
| 2583 | uint32_t const id; |
| 2584 | Vector<Package*> packages; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2585 | |
| 2586 | // This is for finding typeStrings and other common package stuff. |
| 2587 | Package* basePackage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2588 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2589 | // For quick access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2590 | size_t typeCount; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2592 | // Computed attribute bags, first indexed by the type and second |
| 2593 | // by the entry in that type. |
| 2594 | bag_set*** bags; |
| 2595 | }; |
| 2596 | |
| 2597 | struct ResTable::bag_set |
| 2598 | { |
| 2599 | size_t numAttrs; // number in array |
| 2600 | size_t availAttrs; // total space in array |
| 2601 | uint32_t typeSpecFlags; |
| 2602 | // Followed by 'numAttr' bag_entry structures. |
| 2603 | }; |
| 2604 | |
| 2605 | ResTable::Theme::Theme(const ResTable& table) |
| 2606 | : mTable(table) |
| 2607 | { |
| 2608 | memset(mPackages, 0, sizeof(mPackages)); |
| 2609 | } |
| 2610 | |
| 2611 | ResTable::Theme::~Theme() |
| 2612 | { |
| 2613 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 2614 | package_info* pi = mPackages[i]; |
| 2615 | if (pi != NULL) { |
| 2616 | free_package(pi); |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | void ResTable::Theme::free_package(package_info* pi) |
| 2622 | { |
| 2623 | for (size_t j=0; j<pi->numTypes; j++) { |
| 2624 | theme_entry* te = pi->types[j].entries; |
| 2625 | if (te != NULL) { |
| 2626 | free(te); |
| 2627 | } |
| 2628 | } |
| 2629 | free(pi); |
| 2630 | } |
| 2631 | |
| 2632 | ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi) |
| 2633 | { |
| 2634 | package_info* newpi = (package_info*)malloc( |
| 2635 | sizeof(package_info) + (pi->numTypes*sizeof(type_info))); |
| 2636 | newpi->numTypes = pi->numTypes; |
| 2637 | for (size_t j=0; j<newpi->numTypes; j++) { |
| 2638 | size_t cnt = pi->types[j].numEntries; |
| 2639 | newpi->types[j].numEntries = cnt; |
| 2640 | theme_entry* te = pi->types[j].entries; |
| 2641 | if (te != NULL) { |
| 2642 | theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry)); |
| 2643 | newpi->types[j].entries = newte; |
| 2644 | memcpy(newte, te, cnt*sizeof(theme_entry)); |
| 2645 | } else { |
| 2646 | newpi->types[j].entries = NULL; |
| 2647 | } |
| 2648 | } |
| 2649 | return newpi; |
| 2650 | } |
| 2651 | |
| 2652 | status_t ResTable::Theme::applyStyle(uint32_t resID, bool force) |
| 2653 | { |
| 2654 | const bag_entry* bag; |
| 2655 | uint32_t bagTypeSpecFlags = 0; |
| 2656 | mTable.lock(); |
| 2657 | const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2658 | TABLE_NOISY(ALOGV("Applying style 0x%08x to theme %p, count=%d", resID, this, N)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | if (N < 0) { |
| 2660 | mTable.unlock(); |
| 2661 | return N; |
| 2662 | } |
| 2663 | |
| 2664 | uint32_t curPackage = 0xffffffff; |
| 2665 | ssize_t curPackageIndex = 0; |
| 2666 | package_info* curPI = NULL; |
| 2667 | uint32_t curType = 0xffffffff; |
| 2668 | size_t numEntries = 0; |
| 2669 | theme_entry* curEntries = NULL; |
| 2670 | |
| 2671 | const bag_entry* end = bag + N; |
| 2672 | while (bag < end) { |
| 2673 | const uint32_t attrRes = bag->map.name.ident; |
| 2674 | const uint32_t p = Res_GETPACKAGE(attrRes); |
| 2675 | const uint32_t t = Res_GETTYPE(attrRes); |
| 2676 | const uint32_t e = Res_GETENTRY(attrRes); |
| 2677 | |
| 2678 | if (curPackage != p) { |
| 2679 | const ssize_t pidx = mTable.getResourcePackageIndex(attrRes); |
| 2680 | if (pidx < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2681 | ALOGE("Style contains key with bad package: 0x%08x\n", attrRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2682 | bag++; |
| 2683 | continue; |
| 2684 | } |
| 2685 | curPackage = p; |
| 2686 | curPackageIndex = pidx; |
| 2687 | curPI = mPackages[pidx]; |
| 2688 | if (curPI == NULL) { |
| 2689 | PackageGroup* const grp = mTable.mPackageGroups[pidx]; |
| 2690 | int cnt = grp->typeCount; |
| 2691 | curPI = (package_info*)malloc( |
| 2692 | sizeof(package_info) + (cnt*sizeof(type_info))); |
| 2693 | curPI->numTypes = cnt; |
| 2694 | memset(curPI->types, 0, cnt*sizeof(type_info)); |
| 2695 | mPackages[pidx] = curPI; |
| 2696 | } |
| 2697 | curType = 0xffffffff; |
| 2698 | } |
| 2699 | if (curType != t) { |
| 2700 | if (t >= curPI->numTypes) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2701 | ALOGE("Style contains key with bad type: 0x%08x\n", attrRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | bag++; |
| 2703 | continue; |
| 2704 | } |
| 2705 | curType = t; |
| 2706 | curEntries = curPI->types[t].entries; |
| 2707 | if (curEntries == NULL) { |
| 2708 | PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex]; |
| 2709 | const Type* type = grp->packages[0]->getType(t); |
| 2710 | int cnt = type != NULL ? type->entryCount : 0; |
| 2711 | curEntries = (theme_entry*)malloc(cnt*sizeof(theme_entry)); |
| 2712 | memset(curEntries, Res_value::TYPE_NULL, cnt*sizeof(theme_entry)); |
| 2713 | curPI->types[t].numEntries = cnt; |
| 2714 | curPI->types[t].entries = curEntries; |
| 2715 | } |
| 2716 | numEntries = curPI->types[t].numEntries; |
| 2717 | } |
| 2718 | if (e >= numEntries) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2719 | ALOGE("Style contains key with bad entry: 0x%08x\n", attrRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | bag++; |
| 2721 | continue; |
| 2722 | } |
| 2723 | theme_entry* curEntry = curEntries + e; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2724 | TABLE_NOISY(ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2725 | attrRes, bag->map.value.dataType, bag->map.value.data, |
| 2726 | curEntry->value.dataType)); |
| 2727 | if (force || curEntry->value.dataType == Res_value::TYPE_NULL) { |
| 2728 | curEntry->stringBlock = bag->stringBlock; |
| 2729 | curEntry->typeSpecFlags |= bagTypeSpecFlags; |
| 2730 | curEntry->value = bag->map.value; |
| 2731 | } |
| 2732 | |
| 2733 | bag++; |
| 2734 | } |
| 2735 | |
| 2736 | mTable.unlock(); |
| 2737 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2738 | //ALOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2739 | //dumpToLog(); |
| 2740 | |
| 2741 | return NO_ERROR; |
| 2742 | } |
| 2743 | |
| 2744 | status_t ResTable::Theme::setTo(const Theme& other) |
| 2745 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2746 | //ALOGI("Setting theme %p from theme %p...\n", this, &other); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2747 | //dumpToLog(); |
| 2748 | //other.dumpToLog(); |
| 2749 | |
| 2750 | if (&mTable == &other.mTable) { |
| 2751 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 2752 | if (mPackages[i] != NULL) { |
| 2753 | free_package(mPackages[i]); |
| 2754 | } |
| 2755 | if (other.mPackages[i] != NULL) { |
| 2756 | mPackages[i] = copy_package(other.mPackages[i]); |
| 2757 | } else { |
| 2758 | mPackages[i] = NULL; |
| 2759 | } |
| 2760 | } |
| 2761 | } else { |
| 2762 | // @todo: need to really implement this, not just copy |
| 2763 | // the system package (which is still wrong because it isn't |
| 2764 | // fixing up resource references). |
| 2765 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 2766 | if (mPackages[i] != NULL) { |
| 2767 | free_package(mPackages[i]); |
| 2768 | } |
| 2769 | if (i == 0 && other.mPackages[i] != NULL) { |
| 2770 | mPackages[i] = copy_package(other.mPackages[i]); |
| 2771 | } else { |
| 2772 | mPackages[i] = NULL; |
| 2773 | } |
| 2774 | } |
| 2775 | } |
| 2776 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2777 | //ALOGI("Final theme:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2778 | //dumpToLog(); |
| 2779 | |
| 2780 | return NO_ERROR; |
| 2781 | } |
| 2782 | |
| 2783 | ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue, |
| 2784 | uint32_t* outTypeSpecFlags) const |
| 2785 | { |
| 2786 | int cnt = 20; |
| 2787 | |
| 2788 | if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0; |
| 2789 | |
| 2790 | do { |
| 2791 | const ssize_t p = mTable.getResourcePackageIndex(resID); |
| 2792 | const uint32_t t = Res_GETTYPE(resID); |
| 2793 | const uint32_t e = Res_GETENTRY(resID); |
| 2794 | |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2795 | TABLE_THEME(ALOGI("Looking up attr 0x%08x in theme %p", resID, this)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | |
| 2797 | if (p >= 0) { |
| 2798 | const package_info* const pi = mPackages[p]; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2799 | TABLE_THEME(ALOGI("Found package: %p", pi)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2800 | if (pi != NULL) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2801 | TABLE_THEME(ALOGI("Desired type index is %ld in avail %d", t, pi->numTypes)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2802 | if (t < pi->numTypes) { |
| 2803 | const type_info& ti = pi->types[t]; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2804 | TABLE_THEME(ALOGI("Desired entry index is %ld in avail %d", e, ti.numEntries)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2805 | if (e < ti.numEntries) { |
| 2806 | const theme_entry& te = ti.entries[e]; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 2807 | if (outTypeSpecFlags != NULL) { |
| 2808 | *outTypeSpecFlags |= te.typeSpecFlags; |
| 2809 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2810 | TABLE_THEME(ALOGI("Theme value: type=0x%x, data=0x%08x", |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 2811 | te.value.dataType, te.value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2812 | const uint8_t type = te.value.dataType; |
| 2813 | if (type == Res_value::TYPE_ATTRIBUTE) { |
| 2814 | if (cnt > 0) { |
| 2815 | cnt--; |
| 2816 | resID = te.value.data; |
| 2817 | continue; |
| 2818 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2819 | ALOGW("Too many attribute references, stopped at: 0x%08x\n", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | return BAD_INDEX; |
| 2821 | } else if (type != Res_value::TYPE_NULL) { |
| 2822 | *outValue = te.value; |
| 2823 | return te.stringBlock; |
| 2824 | } |
| 2825 | return BAD_INDEX; |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | } |
| 2830 | break; |
| 2831 | |
| 2832 | } while (true); |
| 2833 | |
| 2834 | return BAD_INDEX; |
| 2835 | } |
| 2836 | |
| 2837 | ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue, |
| 2838 | ssize_t blockIndex, uint32_t* outLastRef, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 2839 | uint32_t* inoutTypeSpecFlags, ResTable_config* inoutConfig) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2840 | { |
| 2841 | //printf("Resolving type=0x%x\n", inOutValue->dataType); |
| 2842 | if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 2843 | uint32_t newTypeSpecFlags; |
| 2844 | blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2845 | TABLE_THEME(ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=%p\n", |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 2846 | (int)blockIndex, (int)inOutValue->dataType, (void*)inOutValue->data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2847 | if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags; |
| 2848 | //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType); |
| 2849 | if (blockIndex < 0) { |
| 2850 | return blockIndex; |
| 2851 | } |
| 2852 | } |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 2853 | return mTable.resolveReference(inOutValue, blockIndex, outLastRef, |
| 2854 | inoutTypeSpecFlags, inoutConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | void ResTable::Theme::dumpToLog() const |
| 2858 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2859 | ALOGI("Theme %p:\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 2861 | package_info* pi = mPackages[i]; |
| 2862 | if (pi == NULL) continue; |
| 2863 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2864 | ALOGI(" Package #0x%02x:\n", (int)(i+1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2865 | for (size_t j=0; j<pi->numTypes; j++) { |
| 2866 | type_info& ti = pi->types[j]; |
| 2867 | if (ti.numEntries == 0) continue; |
| 2868 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2869 | ALOGI(" Type #0x%02x:\n", (int)(j+1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2870 | for (size_t k=0; k<ti.numEntries; k++) { |
| 2871 | theme_entry& te = ti.entries[k]; |
| 2872 | if (te.value.dataType == Res_value::TYPE_NULL) continue; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2873 | ALOGI(" 0x%08x: t=0x%x, d=0x%08x (block=%d)\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | (int)Res_MAKEID(i, j, k), |
| 2875 | te.value.dataType, (int)te.value.data, (int)te.stringBlock); |
| 2876 | } |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | ResTable::ResTable() |
| 2882 | : mError(NO_INIT) |
| 2883 | { |
| 2884 | memset(&mParams, 0, sizeof(mParams)); |
| 2885 | memset(mPackageMap, 0, sizeof(mPackageMap)); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2886 | //ALOGI("Creating ResTable %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2887 | } |
| 2888 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2889 | ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2890 | : mError(NO_INIT) |
| 2891 | { |
| 2892 | memset(&mParams, 0, sizeof(mParams)); |
| 2893 | memset(mPackageMap, 0, sizeof(mPackageMap)); |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2894 | addInternal(data, size, cookie, NULL /* asset */, copyData, NULL /* idMap */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2895 | LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table"); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2896 | //ALOGI("Creating ResTable %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | ResTable::~ResTable() |
| 2900 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2901 | //ALOGI("Destroying ResTable in %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | uninit(); |
| 2903 | } |
| 2904 | |
| 2905 | inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const |
| 2906 | { |
| 2907 | return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1; |
| 2908 | } |
| 2909 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2910 | status_t ResTable::add(const void* data, size_t size) { |
| 2911 | return addInternal(data, size, 0 /* cookie */, NULL /* asset */, |
| 2912 | false /* copyData */, NULL /* idMap */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2913 | } |
| 2914 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2915 | status_t ResTable::add(Asset* asset, const int32_t cookie, bool copyData, const void* idmap) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | { |
| 2917 | const void* data = asset->getBuffer(true); |
| 2918 | if (data == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2919 | ALOGW("Unable to get buffer of resource asset file"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2920 | return UNKNOWN_ERROR; |
| 2921 | } |
| 2922 | size_t size = (size_t)asset->getLength(); |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2923 | return addInternal(data, size, cookie, asset, copyData, |
| 2924 | reinterpret_cast<const Asset*>(idmap)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2925 | } |
| 2926 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2927 | status_t ResTable::add(ResTable* src) |
| 2928 | { |
| 2929 | mError = src->mError; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2930 | |
| 2931 | for (size_t i=0; i<src->mHeaders.size(); i++) { |
| 2932 | mHeaders.add(src->mHeaders[i]); |
| 2933 | } |
| 2934 | |
| 2935 | for (size_t i=0; i<src->mPackageGroups.size(); i++) { |
| 2936 | PackageGroup* srcPg = src->mPackageGroups[i]; |
| 2937 | PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id); |
| 2938 | for (size_t j=0; j<srcPg->packages.size(); j++) { |
| 2939 | pg->packages.add(srcPg->packages[j]); |
| 2940 | } |
| 2941 | pg->basePackage = srcPg->basePackage; |
| 2942 | pg->typeCount = srcPg->typeCount; |
| 2943 | mPackageGroups.add(pg); |
| 2944 | } |
| 2945 | |
| 2946 | memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap)); |
| 2947 | |
| 2948 | return mError; |
| 2949 | } |
| 2950 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2951 | status_t ResTable::addInternal(const void* data, size_t size, const int32_t cookie, |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 2952 | Asset* asset, bool copyData, const Asset* idmap) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2953 | { |
| 2954 | if (!data) return NO_ERROR; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 2955 | Header* header = new Header(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2956 | header->index = mHeaders.size(); |
| 2957 | header->cookie = cookie; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 2958 | if (idmap != NULL) { |
| 2959 | const size_t idmap_size = idmap->getLength(); |
| 2960 | const void* idmap_data = const_cast<Asset*>(idmap)->getBuffer(true); |
| 2961 | header->resourceIDMap = (uint32_t*)malloc(idmap_size); |
| 2962 | if (header->resourceIDMap == NULL) { |
| 2963 | delete header; |
| 2964 | return (mError = NO_MEMORY); |
| 2965 | } |
| 2966 | memcpy((void*)header->resourceIDMap, idmap_data, idmap_size); |
| 2967 | header->resourceIDMapSize = idmap_size; |
| 2968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2969 | mHeaders.add(header); |
| 2970 | |
| 2971 | const bool notDeviceEndian = htods(0xf0) != 0xf0; |
| 2972 | |
| 2973 | LOAD_TABLE_NOISY( |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 2974 | ALOGV("Adding resources to ResTable: data=%p, size=0x%x, cookie=%d, asset=%p, copy=%d " |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 2975 | "idmap=%p\n", data, size, cookie, asset, copyData, idmap)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2976 | |
| 2977 | if (copyData || notDeviceEndian) { |
| 2978 | header->ownedData = malloc(size); |
| 2979 | if (header->ownedData == NULL) { |
| 2980 | return (mError=NO_MEMORY); |
| 2981 | } |
| 2982 | memcpy(header->ownedData, data, size); |
| 2983 | data = header->ownedData; |
| 2984 | } |
| 2985 | |
| 2986 | header->header = (const ResTable_header*)data; |
| 2987 | header->size = dtohl(header->header->header.size); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2988 | //ALOGI("Got size 0x%x, again size 0x%x, raw size 0x%x\n", header->size, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2989 | // dtohl(header->header->header.size), header->header->header.size); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2990 | LOAD_TABLE_NOISY(ALOGV("Loading ResTable @%p:\n", header->header)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2991 | LOAD_TABLE_NOISY(printHexData(2, header->header, header->size < 256 ? header->size : 256, |
| 2992 | 16, 16, 0, false, printToLogFunc)); |
| 2993 | if (dtohs(header->header->header.headerSize) > header->size |
| 2994 | || header->size > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2995 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | (int)dtohs(header->header->header.headerSize), |
| 2997 | (int)header->size, (int)size); |
| 2998 | return (mError=BAD_TYPE); |
| 2999 | } |
| 3000 | if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3001 | ALOGW("Bad resource table: header size 0x%x or total size 0x%x is not on an integer boundary\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3002 | (int)dtohs(header->header->header.headerSize), |
| 3003 | (int)header->size); |
| 3004 | return (mError=BAD_TYPE); |
| 3005 | } |
| 3006 | header->dataEnd = ((const uint8_t*)header->header) + header->size; |
| 3007 | |
| 3008 | // Iterate through all chunks. |
| 3009 | size_t curPackage = 0; |
| 3010 | |
| 3011 | const ResChunk_header* chunk = |
| 3012 | (const ResChunk_header*)(((const uint8_t*)header->header) |
| 3013 | + dtohs(header->header->header.headerSize)); |
| 3014 | while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) && |
| 3015 | ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) { |
| 3016 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable"); |
| 3017 | if (err != NO_ERROR) { |
| 3018 | return (mError=err); |
| 3019 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3020 | TABLE_NOISY(ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3021 | dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), |
| 3022 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)))); |
| 3023 | const size_t csize = dtohl(chunk->size); |
| 3024 | const uint16_t ctype = dtohs(chunk->type); |
| 3025 | if (ctype == RES_STRING_POOL_TYPE) { |
| 3026 | if (header->values.getError() != NO_ERROR) { |
| 3027 | // Only use the first string chunk; ignore any others that |
| 3028 | // may appear. |
| 3029 | status_t err = header->values.setTo(chunk, csize); |
| 3030 | if (err != NO_ERROR) { |
| 3031 | return (mError=err); |
| 3032 | } |
| 3033 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3034 | ALOGW("Multiple string chunks found in resource table."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3035 | } |
| 3036 | } else if (ctype == RES_TABLE_PACKAGE_TYPE) { |
| 3037 | if (curPackage >= dtohl(header->header->packageCount)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3038 | ALOGW("More package chunks were found than the %d declared in the header.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | dtohl(header->header->packageCount)); |
| 3040 | return (mError=BAD_TYPE); |
| 3041 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3042 | uint32_t idmap_id = 0; |
| 3043 | if (idmap != NULL) { |
| 3044 | uint32_t tmp; |
| 3045 | if (getIdmapPackageId(header->resourceIDMap, |
| 3046 | header->resourceIDMapSize, |
| 3047 | &tmp) == NO_ERROR) { |
| 3048 | idmap_id = tmp; |
| 3049 | } |
| 3050 | } |
| 3051 | if (parsePackage((ResTable_package*)chunk, header, idmap_id) != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3052 | return mError; |
| 3053 | } |
| 3054 | curPackage++; |
| 3055 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3056 | ALOGW("Unknown chunk type %p in table at %p.\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | (void*)(int)(ctype), |
| 3058 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))); |
| 3059 | } |
| 3060 | chunk = (const ResChunk_header*) |
| 3061 | (((const uint8_t*)chunk) + csize); |
| 3062 | } |
| 3063 | |
| 3064 | if (curPackage < dtohl(header->header->packageCount)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3065 | ALOGW("Fewer package chunks (%d) were found than the %d declared in the header.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3066 | (int)curPackage, dtohl(header->header->packageCount)); |
| 3067 | return (mError=BAD_TYPE); |
| 3068 | } |
| 3069 | mError = header->values.getError(); |
| 3070 | if (mError != NO_ERROR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3071 | ALOGW("No string values found in resource table!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3072 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3073 | |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3074 | TABLE_NOISY(ALOGV("Returning from add with mError=%d\n", mError)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3075 | return mError; |
| 3076 | } |
| 3077 | |
| 3078 | status_t ResTable::getError() const |
| 3079 | { |
| 3080 | return mError; |
| 3081 | } |
| 3082 | |
| 3083 | void ResTable::uninit() |
| 3084 | { |
| 3085 | mError = NO_INIT; |
| 3086 | size_t N = mPackageGroups.size(); |
| 3087 | for (size_t i=0; i<N; i++) { |
| 3088 | PackageGroup* g = mPackageGroups[i]; |
| 3089 | delete g; |
| 3090 | } |
| 3091 | N = mHeaders.size(); |
| 3092 | for (size_t i=0; i<N; i++) { |
| 3093 | Header* header = mHeaders[i]; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3094 | if (header->owner == this) { |
| 3095 | if (header->ownedData) { |
| 3096 | free(header->ownedData); |
| 3097 | } |
| 3098 | delete header; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | mPackageGroups.clear(); |
| 3103 | mHeaders.clear(); |
| 3104 | } |
| 3105 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 3106 | bool ResTable::getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | { |
| 3108 | if (mError != NO_ERROR) { |
| 3109 | return false; |
| 3110 | } |
| 3111 | |
| 3112 | const ssize_t p = getResourcePackageIndex(resID); |
| 3113 | const int t = Res_GETTYPE(resID); |
| 3114 | const int e = Res_GETENTRY(resID); |
| 3115 | |
| 3116 | if (p < 0) { |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3117 | if (Res_GETPACKAGE(resID)+1 == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3118 | ALOGW("No package identifier when getting name for resource number 0x%08x", resID); |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3119 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3120 | ALOGW("No known package when getting name for resource number 0x%08x", resID); |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3122 | return false; |
| 3123 | } |
| 3124 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3125 | ALOGW("No type identifier when getting name for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3126 | return false; |
| 3127 | } |
| 3128 | |
| 3129 | const PackageGroup* const grp = mPackageGroups[p]; |
| 3130 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3131 | ALOGW("Bad identifier when getting name for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3132 | return false; |
| 3133 | } |
| 3134 | if (grp->packages.size() > 0) { |
| 3135 | const Package* const package = grp->packages[0]; |
| 3136 | |
| 3137 | const ResTable_type* type; |
| 3138 | const ResTable_entry* entry; |
| 3139 | ssize_t offset = getEntry(package, t, e, NULL, &type, &entry, NULL); |
| 3140 | if (offset <= 0) { |
| 3141 | return false; |
| 3142 | } |
| 3143 | |
| 3144 | outName->package = grp->name.string(); |
| 3145 | outName->packageLen = grp->name.size(); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 3146 | if (allowUtf8) { |
| 3147 | outName->type8 = grp->basePackage->typeStrings.string8At(t, &outName->typeLen); |
| 3148 | outName->name8 = grp->basePackage->keyStrings.string8At( |
| 3149 | dtohl(entry->key.index), &outName->nameLen); |
| 3150 | } else { |
| 3151 | outName->type8 = NULL; |
| 3152 | outName->name8 = NULL; |
| 3153 | } |
| 3154 | if (outName->type8 == NULL) { |
| 3155 | outName->type = grp->basePackage->typeStrings.stringAt(t, &outName->typeLen); |
| 3156 | // If we have a bad index for some reason, we should abort. |
| 3157 | if (outName->type == NULL) { |
| 3158 | return false; |
| 3159 | } |
| 3160 | } |
| 3161 | if (outName->name8 == NULL) { |
| 3162 | outName->name = grp->basePackage->keyStrings.stringAt( |
| 3163 | dtohl(entry->key.index), &outName->nameLen); |
| 3164 | // If we have a bad index for some reason, we should abort. |
| 3165 | if (outName->name == NULL) { |
| 3166 | return false; |
| 3167 | } |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3170 | return true; |
| 3171 | } |
| 3172 | |
| 3173 | return false; |
| 3174 | } |
| 3175 | |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3176 | ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, uint16_t density, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | uint32_t* outSpecFlags, ResTable_config* outConfig) const |
| 3178 | { |
| 3179 | if (mError != NO_ERROR) { |
| 3180 | return mError; |
| 3181 | } |
| 3182 | |
| 3183 | const ssize_t p = getResourcePackageIndex(resID); |
| 3184 | const int t = Res_GETTYPE(resID); |
| 3185 | const int e = Res_GETENTRY(resID); |
| 3186 | |
| 3187 | if (p < 0) { |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3188 | if (Res_GETPACKAGE(resID)+1 == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3189 | ALOGW("No package identifier when getting value for resource number 0x%08x", resID); |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3190 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3191 | ALOGW("No known package when getting value for resource number 0x%08x", resID); |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 3192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3193 | return BAD_INDEX; |
| 3194 | } |
| 3195 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3196 | ALOGW("No type identifier when getting value for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3197 | return BAD_INDEX; |
| 3198 | } |
| 3199 | |
| 3200 | const Res_value* bestValue = NULL; |
| 3201 | const Package* bestPackage = NULL; |
| 3202 | ResTable_config bestItem; |
| 3203 | memset(&bestItem, 0, sizeof(bestItem)); // make the compiler shut up |
| 3204 | |
| 3205 | if (outSpecFlags != NULL) *outSpecFlags = 0; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3207 | // Look through all resource packages, starting with the most |
| 3208 | // recently added. |
| 3209 | const PackageGroup* const grp = mPackageGroups[p]; |
| 3210 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3211 | ALOGW("Bad identifier when getting value for resource number 0x%08x", resID); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 3212 | return BAD_INDEX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3213 | } |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3214 | |
| 3215 | // Allow overriding density |
| 3216 | const ResTable_config* desiredConfig = &mParams; |
| 3217 | ResTable_config* overrideConfig = NULL; |
| 3218 | if (density > 0) { |
| 3219 | overrideConfig = (ResTable_config*) malloc(sizeof(ResTable_config)); |
| 3220 | if (overrideConfig == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3221 | ALOGE("Couldn't malloc ResTable_config for overrides: %s", strerror(errno)); |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3222 | return BAD_INDEX; |
| 3223 | } |
| 3224 | memcpy(overrideConfig, &mParams, sizeof(ResTable_config)); |
| 3225 | overrideConfig->density = density; |
| 3226 | desiredConfig = overrideConfig; |
| 3227 | } |
| 3228 | |
Kenny Root | 5c4cf8c | 2010-11-02 11:27:21 -0700 | [diff] [blame] | 3229 | ssize_t rc = BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3230 | size_t ip = grp->packages.size(); |
| 3231 | while (ip > 0) { |
| 3232 | ip--; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3233 | int T = t; |
| 3234 | int E = e; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3235 | |
| 3236 | const Package* const package = grp->packages[ip]; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3237 | if (package->header->resourceIDMap) { |
| 3238 | uint32_t overlayResID = 0x0; |
| 3239 | status_t retval = idmapLookup(package->header->resourceIDMap, |
| 3240 | package->header->resourceIDMapSize, |
| 3241 | resID, &overlayResID); |
| 3242 | if (retval == NO_ERROR && overlayResID != 0x0) { |
| 3243 | // for this loop iteration, this is the type and entry we really want |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3244 | ALOGV("resource map 0x%08x -> 0x%08x\n", resID, overlayResID); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3245 | T = Res_GETTYPE(overlayResID); |
| 3246 | E = Res_GETENTRY(overlayResID); |
| 3247 | } else { |
| 3248 | // resource not present in overlay package, continue with the next package |
| 3249 | continue; |
| 3250 | } |
| 3251 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3252 | |
| 3253 | const ResTable_type* type; |
| 3254 | const ResTable_entry* entry; |
| 3255 | const Type* typeClass; |
Kenny Root | 18490fb | 2011-04-12 10:27:15 -0700 | [diff] [blame] | 3256 | ssize_t offset = getEntry(package, T, E, desiredConfig, &type, &entry, &typeClass); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3257 | if (offset <= 0) { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3258 | // No {entry, appropriate config} pair found in package. If this |
| 3259 | // package is an overlay package (ip != 0), this simply means the |
| 3260 | // overlay package did not specify a default. |
| 3261 | // Non-overlay packages are still required to provide a default. |
| 3262 | if (offset < 0 && ip == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3263 | ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) in package %zd (error %d)\n", |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3264 | resID, T, E, ip, (int)offset); |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3265 | rc = offset; |
| 3266 | goto out; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3267 | } |
| 3268 | continue; |
| 3269 | } |
| 3270 | |
| 3271 | if ((dtohs(entry->flags)&entry->FLAG_COMPLEX) != 0) { |
| 3272 | if (!mayBeBag) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3273 | ALOGW("Requesting resource %p failed because it is complex\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | (void*)resID); |
| 3275 | } |
| 3276 | continue; |
| 3277 | } |
| 3278 | |
| 3279 | TABLE_NOISY(aout << "Resource type data: " |
| 3280 | << HexDump(type, dtohl(type->header.size)) << endl); |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3282 | if ((size_t)offset > (dtohl(type->header.size)-sizeof(Res_value))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3283 | ALOGW("ResTable_item at %d is beyond type chunk data %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3284 | (int)offset, dtohl(type->header.size)); |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3285 | rc = BAD_TYPE; |
| 3286 | goto out; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | } |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3289 | const Res_value* item = |
| 3290 | (const Res_value*)(((const uint8_t*)type) + offset); |
| 3291 | ResTable_config thisConfig; |
| 3292 | thisConfig.copyFromDtoH(type->config); |
| 3293 | |
| 3294 | if (outSpecFlags != NULL) { |
| 3295 | if (typeClass->typeSpecFlags != NULL) { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3296 | *outSpecFlags |= dtohl(typeClass->typeSpecFlags[E]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3297 | } else { |
| 3298 | *outSpecFlags = -1; |
| 3299 | } |
| 3300 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3301 | |
| 3302 | if (bestPackage != NULL && |
| 3303 | (bestItem.isMoreSpecificThan(thisConfig) || bestItem.diff(thisConfig) == 0)) { |
| 3304 | // Discard thisConfig not only if bestItem is more specific, but also if the two configs |
| 3305 | // are identical (diff == 0), or overlay packages will not take effect. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3306 | continue; |
| 3307 | } |
| 3308 | |
| 3309 | bestItem = thisConfig; |
| 3310 | bestValue = item; |
| 3311 | bestPackage = package; |
| 3312 | } |
| 3313 | |
| 3314 | TABLE_NOISY(printf("Found result: package %p\n", bestPackage)); |
| 3315 | |
| 3316 | if (bestValue) { |
| 3317 | outValue->size = dtohs(bestValue->size); |
| 3318 | outValue->res0 = bestValue->res0; |
| 3319 | outValue->dataType = bestValue->dataType; |
| 3320 | outValue->data = dtohl(bestValue->data); |
| 3321 | if (outConfig != NULL) { |
| 3322 | *outConfig = bestItem; |
| 3323 | } |
| 3324 | TABLE_NOISY(size_t len; |
| 3325 | printf("Found value: pkg=%d, type=%d, str=%s, int=%d\n", |
| 3326 | bestPackage->header->index, |
| 3327 | outValue->dataType, |
| 3328 | outValue->dataType == bestValue->TYPE_STRING |
| 3329 | ? String8(bestPackage->header->values.stringAt( |
| 3330 | outValue->data, &len)).string() |
| 3331 | : "", |
| 3332 | outValue->data)); |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3333 | rc = bestPackage->header->index; |
| 3334 | goto out; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3337 | out: |
| 3338 | if (overrideConfig != NULL) { |
| 3339 | free(overrideConfig); |
| 3340 | } |
| 3341 | |
| 3342 | return rc; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 3346 | uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags, |
| 3347 | ResTable_config* outConfig) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3348 | { |
| 3349 | int count=0; |
| 3350 | while (blockIndex >= 0 && value->dataType == value->TYPE_REFERENCE |
| 3351 | && value->data != 0 && count < 20) { |
| 3352 | if (outLastRef) *outLastRef = value->data; |
| 3353 | uint32_t lastRef = value->data; |
| 3354 | uint32_t newFlags = 0; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 3355 | const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 3356 | outConfig); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 3357 | if (newIndex == BAD_INDEX) { |
| 3358 | return BAD_INDEX; |
| 3359 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3360 | TABLE_THEME(ALOGI("Resolving reference %p: newIndex=%d, type=0x%x, data=%p\n", |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 3361 | (void*)lastRef, (int)newIndex, (int)value->dataType, (void*)value->data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3362 | //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex); |
| 3363 | if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags; |
| 3364 | if (newIndex < 0) { |
| 3365 | // This can fail if the resource being referenced is a style... |
| 3366 | // in this case, just return the reference, and expect the |
| 3367 | // caller to deal with. |
| 3368 | return blockIndex; |
| 3369 | } |
| 3370 | blockIndex = newIndex; |
| 3371 | count++; |
| 3372 | } |
| 3373 | return blockIndex; |
| 3374 | } |
| 3375 | |
| 3376 | const char16_t* ResTable::valueToString( |
| 3377 | const Res_value* value, size_t stringBlock, |
| 3378 | char16_t tmpBuffer[TMP_BUFFER_SIZE], size_t* outLen) |
| 3379 | { |
| 3380 | if (!value) { |
| 3381 | return NULL; |
| 3382 | } |
| 3383 | if (value->dataType == value->TYPE_STRING) { |
| 3384 | return getTableStringBlock(stringBlock)->stringAt(value->data, outLen); |
| 3385 | } |
| 3386 | // XXX do int to string conversions. |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | |
| 3390 | ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const |
| 3391 | { |
| 3392 | mLock.lock(); |
| 3393 | ssize_t err = getBagLocked(resID, outBag); |
| 3394 | if (err < NO_ERROR) { |
| 3395 | //printf("*** get failed! unlocking\n"); |
| 3396 | mLock.unlock(); |
| 3397 | } |
| 3398 | return err; |
| 3399 | } |
| 3400 | |
| 3401 | void ResTable::unlockBag(const bag_entry* bag) const |
| 3402 | { |
| 3403 | //printf("<<< unlockBag %p\n", this); |
| 3404 | mLock.unlock(); |
| 3405 | } |
| 3406 | |
| 3407 | void ResTable::lock() const |
| 3408 | { |
| 3409 | mLock.lock(); |
| 3410 | } |
| 3411 | |
| 3412 | void ResTable::unlock() const |
| 3413 | { |
| 3414 | mLock.unlock(); |
| 3415 | } |
| 3416 | |
| 3417 | ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag, |
| 3418 | uint32_t* outTypeSpecFlags) const |
| 3419 | { |
| 3420 | if (mError != NO_ERROR) { |
| 3421 | return mError; |
| 3422 | } |
| 3423 | |
| 3424 | const ssize_t p = getResourcePackageIndex(resID); |
| 3425 | const int t = Res_GETTYPE(resID); |
| 3426 | const int e = Res_GETENTRY(resID); |
| 3427 | |
| 3428 | if (p < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3429 | ALOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3430 | return BAD_INDEX; |
| 3431 | } |
| 3432 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3433 | ALOGW("No type identifier when getting bag for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | return BAD_INDEX; |
| 3435 | } |
| 3436 | |
| 3437 | //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t); |
| 3438 | PackageGroup* const grp = mPackageGroups[p]; |
| 3439 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3440 | ALOGW("Bad identifier when getting bag for resource number 0x%08x", resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3441 | return false; |
| 3442 | } |
| 3443 | |
| 3444 | if (t >= (int)grp->typeCount) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3445 | ALOGW("Type identifier 0x%x is larger than type count 0x%x", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3446 | t+1, (int)grp->typeCount); |
| 3447 | return BAD_INDEX; |
| 3448 | } |
| 3449 | |
| 3450 | const Package* const basePackage = grp->packages[0]; |
| 3451 | |
| 3452 | const Type* const typeConfigs = basePackage->getType(t); |
| 3453 | |
| 3454 | const size_t NENTRY = typeConfigs->entryCount; |
| 3455 | if (e >= (int)NENTRY) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3456 | ALOGW("Entry identifier 0x%x is larger than entry count 0x%x", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3457 | e, (int)typeConfigs->entryCount); |
| 3458 | return BAD_INDEX; |
| 3459 | } |
| 3460 | |
| 3461 | // First see if we've already computed this bag... |
| 3462 | if (grp->bags) { |
| 3463 | bag_set** typeSet = grp->bags[t]; |
| 3464 | if (typeSet) { |
| 3465 | bag_set* set = typeSet[e]; |
| 3466 | if (set) { |
| 3467 | if (set != (bag_set*)0xFFFFFFFF) { |
| 3468 | if (outTypeSpecFlags != NULL) { |
| 3469 | *outTypeSpecFlags = set->typeSpecFlags; |
| 3470 | } |
| 3471 | *outBag = (bag_entry*)(set+1); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 3472 | //ALOGI("Found existing bag for: %p\n", (void*)resID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3473 | return set->numAttrs; |
| 3474 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3475 | ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | resID); |
| 3477 | return BAD_INDEX; |
| 3478 | } |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | // Bag not found, we need to compute it! |
| 3483 | if (!grp->bags) { |
Iliyan Malchev | 7e1d395 | 2012-02-17 12:15:58 -0800 | [diff] [blame] | 3484 | grp->bags = (bag_set***)calloc(grp->typeCount, sizeof(bag_set*)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3485 | if (!grp->bags) return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | bag_set** typeSet = grp->bags[t]; |
| 3489 | if (!typeSet) { |
Iliyan Malchev | 7e1d395 | 2012-02-17 12:15:58 -0800 | [diff] [blame] | 3490 | typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | if (!typeSet) return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3492 | grp->bags[t] = typeSet; |
| 3493 | } |
| 3494 | |
| 3495 | // Mark that we are currently working on this one. |
| 3496 | typeSet[e] = (bag_set*)0xFFFFFFFF; |
| 3497 | |
| 3498 | // This is what we are building. |
| 3499 | bag_set* set = NULL; |
| 3500 | |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3501 | TABLE_NOISY(ALOGI("Building bag: %p\n", (void*)resID)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3503 | ResTable_config bestConfig; |
| 3504 | memset(&bestConfig, 0, sizeof(bestConfig)); |
| 3505 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3506 | // Now collect all bag attributes from all packages. |
| 3507 | size_t ip = grp->packages.size(); |
| 3508 | while (ip > 0) { |
| 3509 | ip--; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3510 | int T = t; |
| 3511 | int E = e; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3512 | |
| 3513 | const Package* const package = grp->packages[ip]; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3514 | if (package->header->resourceIDMap) { |
| 3515 | uint32_t overlayResID = 0x0; |
| 3516 | status_t retval = idmapLookup(package->header->resourceIDMap, |
| 3517 | package->header->resourceIDMapSize, |
| 3518 | resID, &overlayResID); |
| 3519 | if (retval == NO_ERROR && overlayResID != 0x0) { |
| 3520 | // for this loop iteration, this is the type and entry we really want |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3521 | ALOGV("resource map 0x%08x -> 0x%08x\n", resID, overlayResID); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3522 | T = Res_GETTYPE(overlayResID); |
| 3523 | E = Res_GETENTRY(overlayResID); |
| 3524 | } else { |
| 3525 | // resource not present in overlay package, continue with the next package |
| 3526 | continue; |
| 3527 | } |
| 3528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3529 | |
| 3530 | const ResTable_type* type; |
| 3531 | const ResTable_entry* entry; |
| 3532 | const Type* typeClass; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3533 | ALOGV("Getting entry pkg=%p, t=%d, e=%d\n", package, T, E); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3534 | ssize_t offset = getEntry(package, T, E, &mParams, &type, &entry, &typeClass); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3535 | ALOGV("Resulting offset=%d\n", offset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3536 | if (offset <= 0) { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3537 | // No {entry, appropriate config} pair found in package. If this |
| 3538 | // package is an overlay package (ip != 0), this simply means the |
| 3539 | // overlay package did not specify a default. |
| 3540 | // Non-overlay packages are still required to provide a default. |
| 3541 | if (offset < 0 && ip == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | if (set) free(set); |
| 3543 | return offset; |
| 3544 | } |
| 3545 | continue; |
| 3546 | } |
| 3547 | |
| 3548 | if ((dtohs(entry->flags)&entry->FLAG_COMPLEX) == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3549 | ALOGW("Skipping entry %p in package table %d because it is not complex!\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | (void*)resID, (int)ip); |
| 3551 | continue; |
| 3552 | } |
| 3553 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3554 | if (set != NULL && !type->config.isBetterThan(bestConfig, NULL)) { |
| 3555 | continue; |
| 3556 | } |
| 3557 | bestConfig = type->config; |
| 3558 | if (set) { |
| 3559 | free(set); |
| 3560 | set = NULL; |
| 3561 | } |
| 3562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | const uint16_t entrySize = dtohs(entry->size); |
| 3564 | const uint32_t parent = entrySize >= sizeof(ResTable_map_entry) |
| 3565 | ? dtohl(((const ResTable_map_entry*)entry)->parent.ident) : 0; |
| 3566 | const uint32_t count = entrySize >= sizeof(ResTable_map_entry) |
| 3567 | ? dtohl(((const ResTable_map_entry*)entry)->count) : 0; |
| 3568 | |
| 3569 | size_t N = count; |
| 3570 | |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3571 | TABLE_NOISY(ALOGI("Found map: size=%p parent=%p count=%d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3572 | entrySize, parent, count)); |
| 3573 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3574 | // If this map inherits from another, we need to start |
| 3575 | // with its parent's values. Otherwise start out empty. |
| 3576 | TABLE_NOISY(printf("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", |
| 3577 | entrySize, parent)); |
| 3578 | if (parent) { |
| 3579 | const bag_entry* parentBag; |
| 3580 | uint32_t parentTypeSpecFlags = 0; |
| 3581 | const ssize_t NP = getBagLocked(parent, &parentBag, &parentTypeSpecFlags); |
| 3582 | const size_t NT = ((NP >= 0) ? NP : 0) + N; |
| 3583 | set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT); |
| 3584 | if (set == NULL) { |
| 3585 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3586 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3587 | if (NP > 0) { |
| 3588 | memcpy(set+1, parentBag, NP*sizeof(bag_entry)); |
| 3589 | set->numAttrs = NP; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3590 | TABLE_NOISY(ALOGI("Initialized new bag with %d inherited attributes.\n", NP)); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3591 | } else { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3592 | TABLE_NOISY(ALOGI("Initialized new bag with no inherited attributes.\n")); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3593 | set->numAttrs = 0; |
| 3594 | } |
| 3595 | set->availAttrs = NT; |
| 3596 | set->typeSpecFlags = parentTypeSpecFlags; |
| 3597 | } else { |
| 3598 | set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N); |
| 3599 | if (set == NULL) { |
| 3600 | return NO_MEMORY; |
| 3601 | } |
| 3602 | set->numAttrs = 0; |
| 3603 | set->availAttrs = N; |
| 3604 | set->typeSpecFlags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | if (typeClass->typeSpecFlags != NULL) { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3608 | set->typeSpecFlags |= dtohl(typeClass->typeSpecFlags[E]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3609 | } else { |
| 3610 | set->typeSpecFlags = -1; |
| 3611 | } |
| 3612 | |
| 3613 | // Now merge in the new attributes... |
| 3614 | ssize_t curOff = offset; |
| 3615 | const ResTable_map* map; |
| 3616 | bag_entry* entries = (bag_entry*)(set+1); |
| 3617 | size_t curEntry = 0; |
| 3618 | uint32_t pos = 0; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3619 | TABLE_NOISY(ALOGI("Starting with set %p, entries=%p, avail=%d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | set, entries, set->availAttrs)); |
| 3621 | while (pos < count) { |
| 3622 | TABLE_NOISY(printf("Now at %p\n", (void*)curOff)); |
| 3623 | |
| 3624 | if ((size_t)curOff > (dtohl(type->header.size)-sizeof(ResTable_map))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3625 | ALOGW("ResTable_map at %d is beyond type chunk data %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3626 | (int)curOff, dtohl(type->header.size)); |
| 3627 | return BAD_TYPE; |
| 3628 | } |
| 3629 | map = (const ResTable_map*)(((const uint8_t*)type) + curOff); |
| 3630 | N++; |
| 3631 | |
| 3632 | const uint32_t newName = htodl(map->name.ident); |
| 3633 | bool isInside; |
| 3634 | uint32_t oldName = 0; |
| 3635 | while ((isInside=(curEntry < set->numAttrs)) |
| 3636 | && (oldName=entries[curEntry].map.name.ident) < newName) { |
| 3637 | TABLE_NOISY(printf("#%d: Keeping existing attribute: 0x%08x\n", |
| 3638 | curEntry, entries[curEntry].map.name.ident)); |
| 3639 | curEntry++; |
| 3640 | } |
| 3641 | |
| 3642 | if ((!isInside) || oldName != newName) { |
| 3643 | // This is a new attribute... figure out what to do with it. |
| 3644 | if (set->numAttrs >= set->availAttrs) { |
| 3645 | // Need to alloc more memory... |
| 3646 | const size_t newAvail = set->availAttrs+N; |
| 3647 | set = (bag_set*)realloc(set, |
| 3648 | sizeof(bag_set) |
| 3649 | + sizeof(bag_entry)*newAvail); |
| 3650 | if (set == NULL) { |
| 3651 | return NO_MEMORY; |
| 3652 | } |
| 3653 | set->availAttrs = newAvail; |
| 3654 | entries = (bag_entry*)(set+1); |
| 3655 | TABLE_NOISY(printf("Reallocated set %p, entries=%p, avail=%d\n", |
| 3656 | set, entries, set->availAttrs)); |
| 3657 | } |
| 3658 | if (isInside) { |
| 3659 | // Going in the middle, need to make space. |
| 3660 | memmove(entries+curEntry+1, entries+curEntry, |
| 3661 | sizeof(bag_entry)*(set->numAttrs-curEntry)); |
| 3662 | set->numAttrs++; |
| 3663 | } |
| 3664 | TABLE_NOISY(printf("#%d: Inserting new attribute: 0x%08x\n", |
| 3665 | curEntry, newName)); |
| 3666 | } else { |
| 3667 | TABLE_NOISY(printf("#%d: Replacing existing attribute: 0x%08x\n", |
| 3668 | curEntry, oldName)); |
| 3669 | } |
| 3670 | |
| 3671 | bag_entry* cur = entries+curEntry; |
| 3672 | |
| 3673 | cur->stringBlock = package->header->index; |
| 3674 | cur->map.name.ident = newName; |
| 3675 | cur->map.value.copyFrom_dtoh(map->value); |
| 3676 | TABLE_NOISY(printf("Setting entry #%d %p: block=%d, name=0x%08x, type=%d, data=0x%08x\n", |
| 3677 | curEntry, cur, cur->stringBlock, cur->map.name.ident, |
| 3678 | cur->map.value.dataType, cur->map.value.data)); |
| 3679 | |
| 3680 | // On to the next! |
| 3681 | curEntry++; |
| 3682 | pos++; |
| 3683 | const size_t size = dtohs(map->value.size); |
| 3684 | curOff += size + sizeof(*map)-sizeof(map->value); |
| 3685 | }; |
| 3686 | if (curEntry > set->numAttrs) { |
| 3687 | set->numAttrs = curEntry; |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | // And this is it... |
| 3692 | typeSet[e] = set; |
| 3693 | if (set) { |
| 3694 | if (outTypeSpecFlags != NULL) { |
| 3695 | *outTypeSpecFlags = set->typeSpecFlags; |
| 3696 | } |
| 3697 | *outBag = (bag_entry*)(set+1); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3698 | TABLE_NOISY(ALOGI("Returning %d attrs\n", set->numAttrs)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3699 | return set->numAttrs; |
| 3700 | } |
| 3701 | return BAD_INDEX; |
| 3702 | } |
| 3703 | |
| 3704 | void ResTable::setParameters(const ResTable_config* params) |
| 3705 | { |
| 3706 | mLock.lock(); |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3707 | TABLE_GETENTRY(ALOGI("Setting parameters: %s\n", params->toString().string())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3708 | mParams = *params; |
| 3709 | for (size_t i=0; i<mPackageGroups.size(); i++) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3710 | TABLE_NOISY(ALOGI("CLEARING BAGS FOR GROUP %d!", i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3711 | mPackageGroups[i]->clearBagCache(); |
| 3712 | } |
| 3713 | mLock.unlock(); |
| 3714 | } |
| 3715 | |
| 3716 | void ResTable::getParameters(ResTable_config* params) const |
| 3717 | { |
| 3718 | mLock.lock(); |
| 3719 | *params = mParams; |
| 3720 | mLock.unlock(); |
| 3721 | } |
| 3722 | |
| 3723 | struct id_name_map { |
| 3724 | uint32_t id; |
| 3725 | size_t len; |
| 3726 | char16_t name[6]; |
| 3727 | }; |
| 3728 | |
| 3729 | const static id_name_map ID_NAMES[] = { |
| 3730 | { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } }, |
| 3731 | { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } }, |
| 3732 | { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } }, |
| 3733 | { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } }, |
| 3734 | { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } }, |
| 3735 | { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } }, |
| 3736 | { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } }, |
| 3737 | { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } }, |
| 3738 | { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } }, |
| 3739 | { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } }, |
| 3740 | }; |
| 3741 | |
| 3742 | uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen, |
| 3743 | const char16_t* type, size_t typeLen, |
| 3744 | const char16_t* package, |
| 3745 | size_t packageLen, |
| 3746 | uint32_t* outTypeSpecFlags) const |
| 3747 | { |
| 3748 | TABLE_SUPER_NOISY(printf("Identifier for name: error=%d\n", mError)); |
| 3749 | |
| 3750 | // Check for internal resource identifier as the very first thing, so |
| 3751 | // that we will always find them even when there are no resources. |
| 3752 | if (name[0] == '^') { |
| 3753 | const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0])); |
| 3754 | size_t len; |
| 3755 | for (int i=0; i<N; i++) { |
| 3756 | const id_name_map* m = ID_NAMES + i; |
| 3757 | len = m->len; |
| 3758 | if (len != nameLen) { |
| 3759 | continue; |
| 3760 | } |
| 3761 | for (size_t j=1; j<len; j++) { |
| 3762 | if (m->name[j] != name[j]) { |
| 3763 | goto nope; |
| 3764 | } |
| 3765 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3766 | if (outTypeSpecFlags) { |
| 3767 | *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC; |
| 3768 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | return m->id; |
| 3770 | nope: |
| 3771 | ; |
| 3772 | } |
| 3773 | if (nameLen > 7) { |
| 3774 | if (name[1] == 'i' && name[2] == 'n' |
| 3775 | && name[3] == 'd' && name[4] == 'e' && name[5] == 'x' |
| 3776 | && name[6] == '_') { |
| 3777 | int index = atoi(String8(name + 7, nameLen - 7).string()); |
| 3778 | if (Res_CHECKID(index)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3779 | ALOGW("Array resource index: %d is too large.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3780 | index); |
| 3781 | return 0; |
| 3782 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3783 | if (outTypeSpecFlags) { |
| 3784 | *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC; |
| 3785 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | return Res_MAKEARRAY(index); |
| 3787 | } |
| 3788 | } |
| 3789 | return 0; |
| 3790 | } |
| 3791 | |
| 3792 | if (mError != NO_ERROR) { |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3796 | bool fakePublic = false; |
| 3797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | // Figure out the package and type we are looking in... |
| 3799 | |
| 3800 | const char16_t* packageEnd = NULL; |
| 3801 | const char16_t* typeEnd = NULL; |
| 3802 | const char16_t* const nameEnd = name+nameLen; |
| 3803 | const char16_t* p = name; |
| 3804 | while (p < nameEnd) { |
| 3805 | if (*p == ':') packageEnd = p; |
| 3806 | else if (*p == '/') typeEnd = p; |
| 3807 | p++; |
| 3808 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3809 | if (*name == '@') { |
| 3810 | name++; |
| 3811 | if (*name == '*') { |
| 3812 | fakePublic = true; |
| 3813 | name++; |
| 3814 | } |
| 3815 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | if (name >= nameEnd) { |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | if (packageEnd) { |
| 3821 | package = name; |
| 3822 | packageLen = packageEnd-name; |
| 3823 | name = packageEnd+1; |
| 3824 | } else if (!package) { |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
| 3828 | if (typeEnd) { |
| 3829 | type = name; |
| 3830 | typeLen = typeEnd-name; |
| 3831 | name = typeEnd+1; |
| 3832 | } else if (!type) { |
| 3833 | return 0; |
| 3834 | } |
| 3835 | |
| 3836 | if (name >= nameEnd) { |
| 3837 | return 0; |
| 3838 | } |
| 3839 | nameLen = nameEnd-name; |
| 3840 | |
| 3841 | TABLE_NOISY(printf("Looking for identifier: type=%s, name=%s, package=%s\n", |
| 3842 | String8(type, typeLen).string(), |
| 3843 | String8(name, nameLen).string(), |
| 3844 | String8(package, packageLen).string())); |
| 3845 | |
| 3846 | const size_t NG = mPackageGroups.size(); |
| 3847 | for (size_t ig=0; ig<NG; ig++) { |
| 3848 | const PackageGroup* group = mPackageGroups[ig]; |
| 3849 | |
| 3850 | if (strzcmp16(package, packageLen, |
| 3851 | group->name.string(), group->name.size())) { |
| 3852 | TABLE_NOISY(printf("Skipping package group: %s\n", String8(group->name).string())); |
| 3853 | continue; |
| 3854 | } |
| 3855 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3856 | const ssize_t ti = group->basePackage->typeStrings.indexOfString(type, typeLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3857 | if (ti < 0) { |
| 3858 | TABLE_NOISY(printf("Type not found in package %s\n", String8(group->name).string())); |
| 3859 | continue; |
| 3860 | } |
| 3861 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3862 | const ssize_t ei = group->basePackage->keyStrings.indexOfString(name, nameLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | if (ei < 0) { |
| 3864 | TABLE_NOISY(printf("Name not found in package %s\n", String8(group->name).string())); |
| 3865 | continue; |
| 3866 | } |
| 3867 | |
| 3868 | TABLE_NOISY(printf("Search indices: type=%d, name=%d\n", ti, ei)); |
| 3869 | |
| 3870 | const Type* const typeConfigs = group->packages[0]->getType(ti); |
| 3871 | if (typeConfigs == NULL || typeConfigs->configs.size() <= 0) { |
| 3872 | TABLE_NOISY(printf("Expected type structure not found in package %s for idnex %d\n", |
| 3873 | String8(group->name).string(), ti)); |
| 3874 | } |
| 3875 | |
| 3876 | size_t NTC = typeConfigs->configs.size(); |
| 3877 | for (size_t tci=0; tci<NTC; tci++) { |
| 3878 | const ResTable_type* const ty = typeConfigs->configs[tci]; |
| 3879 | const uint32_t typeOffset = dtohl(ty->entriesStart); |
| 3880 | |
| 3881 | const uint8_t* const end = ((const uint8_t*)ty) + dtohl(ty->header.size); |
| 3882 | const uint32_t* const eindex = (const uint32_t*) |
| 3883 | (((const uint8_t*)ty) + dtohs(ty->header.headerSize)); |
| 3884 | |
| 3885 | const size_t NE = dtohl(ty->entryCount); |
| 3886 | for (size_t i=0; i<NE; i++) { |
| 3887 | uint32_t offset = dtohl(eindex[i]); |
| 3888 | if (offset == ResTable_type::NO_ENTRY) { |
| 3889 | continue; |
| 3890 | } |
| 3891 | |
| 3892 | offset += typeOffset; |
| 3893 | |
| 3894 | if (offset > (dtohl(ty->header.size)-sizeof(ResTable_entry))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3895 | ALOGW("ResTable_entry at %d is beyond type chunk data %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3896 | offset, dtohl(ty->header.size)); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | if ((offset&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3900 | ALOGW("ResTable_entry at %d (pkg=%d type=%d ent=%d) is not on an integer boundary when looking for %s:%s/%s", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3901 | (int)offset, (int)group->id, (int)ti+1, (int)i, |
| 3902 | String8(package, packageLen).string(), |
| 3903 | String8(type, typeLen).string(), |
| 3904 | String8(name, nameLen).string()); |
| 3905 | return 0; |
| 3906 | } |
| 3907 | |
| 3908 | const ResTable_entry* const entry = (const ResTable_entry*) |
| 3909 | (((const uint8_t*)ty) + offset); |
| 3910 | if (dtohs(entry->size) < sizeof(*entry)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3911 | ALOGW("ResTable_entry size %d is too small", dtohs(entry->size)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3912 | return BAD_TYPE; |
| 3913 | } |
| 3914 | |
| 3915 | TABLE_SUPER_NOISY(printf("Looking at entry #%d: want str %d, have %d\n", |
| 3916 | i, ei, dtohl(entry->key.index))); |
| 3917 | if (dtohl(entry->key.index) == (size_t)ei) { |
| 3918 | if (outTypeSpecFlags) { |
| 3919 | *outTypeSpecFlags = typeConfigs->typeSpecFlags[i]; |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3920 | if (fakePublic) { |
| 3921 | *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC; |
| 3922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3923 | } |
| 3924 | return Res_MAKEID(group->id-1, ti, i); |
| 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | } |
| 3929 | |
| 3930 | return 0; |
| 3931 | } |
| 3932 | |
| 3933 | bool ResTable::expandResourceRef(const uint16_t* refStr, size_t refLen, |
| 3934 | String16* outPackage, |
| 3935 | String16* outType, |
| 3936 | String16* outName, |
| 3937 | const String16* defType, |
| 3938 | const String16* defPackage, |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3939 | const char** outErrorMsg, |
| 3940 | bool* outPublicOnly) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3941 | { |
| 3942 | const char16_t* packageEnd = NULL; |
| 3943 | const char16_t* typeEnd = NULL; |
| 3944 | const char16_t* p = refStr; |
| 3945 | const char16_t* const end = p + refLen; |
| 3946 | while (p < end) { |
| 3947 | if (*p == ':') packageEnd = p; |
| 3948 | else if (*p == '/') { |
| 3949 | typeEnd = p; |
| 3950 | break; |
| 3951 | } |
| 3952 | p++; |
| 3953 | } |
| 3954 | p = refStr; |
| 3955 | if (*p == '@') p++; |
| 3956 | |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 3957 | if (outPublicOnly != NULL) { |
| 3958 | *outPublicOnly = true; |
| 3959 | } |
| 3960 | if (*p == '*') { |
| 3961 | p++; |
| 3962 | if (outPublicOnly != NULL) { |
| 3963 | *outPublicOnly = false; |
| 3964 | } |
| 3965 | } |
| 3966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3967 | if (packageEnd) { |
| 3968 | *outPackage = String16(p, packageEnd-p); |
| 3969 | p = packageEnd+1; |
| 3970 | } else { |
| 3971 | if (!defPackage) { |
| 3972 | if (outErrorMsg) { |
| 3973 | *outErrorMsg = "No resource package specified"; |
| 3974 | } |
| 3975 | return false; |
| 3976 | } |
| 3977 | *outPackage = *defPackage; |
| 3978 | } |
| 3979 | if (typeEnd) { |
| 3980 | *outType = String16(p, typeEnd-p); |
| 3981 | p = typeEnd+1; |
| 3982 | } else { |
| 3983 | if (!defType) { |
| 3984 | if (outErrorMsg) { |
| 3985 | *outErrorMsg = "No resource type specified"; |
| 3986 | } |
| 3987 | return false; |
| 3988 | } |
| 3989 | *outType = *defType; |
| 3990 | } |
| 3991 | *outName = String16(p, end-p); |
Konstantin Lopyrev | ddcafcb | 2010-06-04 14:36:49 -0700 | [diff] [blame] | 3992 | if(**outPackage == 0) { |
| 3993 | if(outErrorMsg) { |
| 3994 | *outErrorMsg = "Resource package cannot be an empty string"; |
| 3995 | } |
| 3996 | return false; |
| 3997 | } |
| 3998 | if(**outType == 0) { |
| 3999 | if(outErrorMsg) { |
| 4000 | *outErrorMsg = "Resource type cannot be an empty string"; |
| 4001 | } |
| 4002 | return false; |
| 4003 | } |
| 4004 | if(**outName == 0) { |
| 4005 | if(outErrorMsg) { |
| 4006 | *outErrorMsg = "Resource id cannot be an empty string"; |
| 4007 | } |
| 4008 | return false; |
| 4009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4010 | return true; |
| 4011 | } |
| 4012 | |
| 4013 | static uint32_t get_hex(char c, bool* outError) |
| 4014 | { |
| 4015 | if (c >= '0' && c <= '9') { |
| 4016 | return c - '0'; |
| 4017 | } else if (c >= 'a' && c <= 'f') { |
| 4018 | return c - 'a' + 0xa; |
| 4019 | } else if (c >= 'A' && c <= 'F') { |
| 4020 | return c - 'A' + 0xa; |
| 4021 | } |
| 4022 | *outError = true; |
| 4023 | return 0; |
| 4024 | } |
| 4025 | |
| 4026 | struct unit_entry |
| 4027 | { |
| 4028 | const char* name; |
| 4029 | size_t len; |
| 4030 | uint8_t type; |
| 4031 | uint32_t unit; |
| 4032 | float scale; |
| 4033 | }; |
| 4034 | |
| 4035 | static const unit_entry unitNames[] = { |
| 4036 | { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f }, |
| 4037 | { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, |
| 4038 | { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, |
| 4039 | { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f }, |
| 4040 | { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f }, |
| 4041 | { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f }, |
| 4042 | { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f }, |
| 4043 | { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 }, |
| 4044 | { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 }, |
| 4045 | { NULL, 0, 0, 0, 0 } |
| 4046 | }; |
| 4047 | |
| 4048 | static bool parse_unit(const char* str, Res_value* outValue, |
| 4049 | float* outScale, const char** outEnd) |
| 4050 | { |
| 4051 | const char* end = str; |
| 4052 | while (*end != 0 && !isspace((unsigned char)*end)) { |
| 4053 | end++; |
| 4054 | } |
| 4055 | const size_t len = end-str; |
| 4056 | |
| 4057 | const char* realEnd = end; |
| 4058 | while (*realEnd != 0 && isspace((unsigned char)*realEnd)) { |
| 4059 | realEnd++; |
| 4060 | } |
| 4061 | if (*realEnd != 0) { |
| 4062 | return false; |
| 4063 | } |
| 4064 | |
| 4065 | const unit_entry* cur = unitNames; |
| 4066 | while (cur->name) { |
| 4067 | if (len == cur->len && strncmp(cur->name, str, len) == 0) { |
| 4068 | outValue->dataType = cur->type; |
| 4069 | outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT; |
| 4070 | *outScale = cur->scale; |
| 4071 | *outEnd = end; |
| 4072 | //printf("Found unit %s for %s\n", cur->name, str); |
| 4073 | return true; |
| 4074 | } |
| 4075 | cur++; |
| 4076 | } |
| 4077 | |
| 4078 | return false; |
| 4079 | } |
| 4080 | |
| 4081 | |
| 4082 | bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue) |
| 4083 | { |
| 4084 | while (len > 0 && isspace16(*s)) { |
| 4085 | s++; |
| 4086 | len--; |
| 4087 | } |
| 4088 | |
| 4089 | if (len <= 0) { |
| 4090 | return false; |
| 4091 | } |
| 4092 | |
| 4093 | size_t i = 0; |
| 4094 | int32_t val = 0; |
| 4095 | bool neg = false; |
| 4096 | |
| 4097 | if (*s == '-') { |
| 4098 | neg = true; |
| 4099 | i++; |
| 4100 | } |
| 4101 | |
| 4102 | if (s[i] < '0' || s[i] > '9') { |
| 4103 | return false; |
| 4104 | } |
| 4105 | |
| 4106 | // Decimal or hex? |
| 4107 | if (s[i] == '0' && s[i+1] == 'x') { |
| 4108 | if (outValue) |
| 4109 | outValue->dataType = outValue->TYPE_INT_HEX; |
| 4110 | i += 2; |
| 4111 | bool error = false; |
| 4112 | while (i < len && !error) { |
| 4113 | val = (val*16) + get_hex(s[i], &error); |
| 4114 | i++; |
| 4115 | } |
| 4116 | if (error) { |
| 4117 | return false; |
| 4118 | } |
| 4119 | } else { |
| 4120 | if (outValue) |
| 4121 | outValue->dataType = outValue->TYPE_INT_DEC; |
| 4122 | while (i < len) { |
| 4123 | if (s[i] < '0' || s[i] > '9') { |
| 4124 | return false; |
| 4125 | } |
| 4126 | val = (val*10) + s[i]-'0'; |
| 4127 | i++; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | if (neg) val = -val; |
| 4132 | |
| 4133 | while (i < len && isspace16(s[i])) { |
| 4134 | i++; |
| 4135 | } |
| 4136 | |
| 4137 | if (i == len) { |
| 4138 | if (outValue) |
| 4139 | outValue->data = val; |
| 4140 | return true; |
| 4141 | } |
| 4142 | |
| 4143 | return false; |
| 4144 | } |
| 4145 | |
| 4146 | bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue) |
| 4147 | { |
| 4148 | while (len > 0 && isspace16(*s)) { |
| 4149 | s++; |
| 4150 | len--; |
| 4151 | } |
| 4152 | |
| 4153 | if (len <= 0) { |
| 4154 | return false; |
| 4155 | } |
| 4156 | |
| 4157 | char buf[128]; |
| 4158 | int i=0; |
| 4159 | while (len > 0 && *s != 0 && i < 126) { |
| 4160 | if (*s > 255) { |
| 4161 | return false; |
| 4162 | } |
| 4163 | buf[i++] = *s++; |
| 4164 | len--; |
| 4165 | } |
| 4166 | |
| 4167 | if (len > 0) { |
| 4168 | return false; |
| 4169 | } |
| 4170 | if (buf[0] < '0' && buf[0] > '9' && buf[0] != '.') { |
| 4171 | return false; |
| 4172 | } |
| 4173 | |
| 4174 | buf[i] = 0; |
| 4175 | const char* end; |
| 4176 | float f = strtof(buf, (char**)&end); |
| 4177 | |
| 4178 | if (*end != 0 && !isspace((unsigned char)*end)) { |
| 4179 | // Might be a unit... |
| 4180 | float scale; |
| 4181 | if (parse_unit(end, outValue, &scale, &end)) { |
| 4182 | f *= scale; |
| 4183 | const bool neg = f < 0; |
| 4184 | if (neg) f = -f; |
| 4185 | uint64_t bits = (uint64_t)(f*(1<<23)+.5f); |
| 4186 | uint32_t radix; |
| 4187 | uint32_t shift; |
| 4188 | if ((bits&0x7fffff) == 0) { |
| 4189 | // Always use 23p0 if there is no fraction, just to make |
| 4190 | // things easier to read. |
| 4191 | radix = Res_value::COMPLEX_RADIX_23p0; |
| 4192 | shift = 23; |
| 4193 | } else if ((bits&0xffffffffff800000LL) == 0) { |
| 4194 | // Magnitude is zero -- can fit in 0 bits of precision. |
| 4195 | radix = Res_value::COMPLEX_RADIX_0p23; |
| 4196 | shift = 0; |
| 4197 | } else if ((bits&0xffffffff80000000LL) == 0) { |
| 4198 | // Magnitude can fit in 8 bits of precision. |
| 4199 | radix = Res_value::COMPLEX_RADIX_8p15; |
| 4200 | shift = 8; |
| 4201 | } else if ((bits&0xffffff8000000000LL) == 0) { |
| 4202 | // Magnitude can fit in 16 bits of precision. |
| 4203 | radix = Res_value::COMPLEX_RADIX_16p7; |
| 4204 | shift = 16; |
| 4205 | } else { |
| 4206 | // Magnitude needs entire range, so no fractional part. |
| 4207 | radix = Res_value::COMPLEX_RADIX_23p0; |
| 4208 | shift = 23; |
| 4209 | } |
| 4210 | int32_t mantissa = (int32_t)( |
| 4211 | (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK); |
| 4212 | if (neg) { |
| 4213 | mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK; |
| 4214 | } |
| 4215 | outValue->data |= |
| 4216 | (radix<<Res_value::COMPLEX_RADIX_SHIFT) |
| 4217 | | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT); |
| 4218 | //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n", |
| 4219 | // f * (neg ? -1 : 1), bits, f*(1<<23), |
| 4220 | // radix, shift, outValue->data); |
| 4221 | return true; |
| 4222 | } |
| 4223 | return false; |
| 4224 | } |
| 4225 | |
| 4226 | while (*end != 0 && isspace((unsigned char)*end)) { |
| 4227 | end++; |
| 4228 | } |
| 4229 | |
| 4230 | if (*end == 0) { |
| 4231 | if (outValue) { |
| 4232 | outValue->dataType = outValue->TYPE_FLOAT; |
| 4233 | *(float*)(&outValue->data) = f; |
| 4234 | return true; |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
| 4241 | bool ResTable::stringToValue(Res_value* outValue, String16* outString, |
| 4242 | const char16_t* s, size_t len, |
| 4243 | bool preserveSpaces, bool coerceType, |
| 4244 | uint32_t attrID, |
| 4245 | const String16* defType, |
| 4246 | const String16* defPackage, |
| 4247 | Accessor* accessor, |
| 4248 | void* accessorCookie, |
| 4249 | uint32_t attrType, |
| 4250 | bool enforcePrivate) const |
| 4251 | { |
| 4252 | bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting(); |
| 4253 | const char* errorMsg = NULL; |
| 4254 | |
| 4255 | outValue->size = sizeof(Res_value); |
| 4256 | outValue->res0 = 0; |
| 4257 | |
| 4258 | // First strip leading/trailing whitespace. Do this before handling |
| 4259 | // escapes, so they can be used to force whitespace into the string. |
| 4260 | if (!preserveSpaces) { |
| 4261 | while (len > 0 && isspace16(*s)) { |
| 4262 | s++; |
| 4263 | len--; |
| 4264 | } |
| 4265 | while (len > 0 && isspace16(s[len-1])) { |
| 4266 | len--; |
| 4267 | } |
| 4268 | // If the string ends with '\', then we keep the space after it. |
| 4269 | if (len > 0 && s[len-1] == '\\' && s[len] != 0) { |
| 4270 | len++; |
| 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | //printf("Value for: %s\n", String8(s, len).string()); |
| 4275 | |
| 4276 | uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED; |
| 4277 | uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff; |
| 4278 | bool fromAccessor = false; |
| 4279 | if (attrID != 0 && !Res_INTERNALID(attrID)) { |
| 4280 | const ssize_t p = getResourcePackageIndex(attrID); |
| 4281 | const bag_entry* bag; |
| 4282 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 4283 | //printf("For attr 0x%08x got bag of %d\n", attrID, cnt); |
| 4284 | if (cnt >= 0) { |
| 4285 | while (cnt > 0) { |
| 4286 | //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data); |
| 4287 | switch (bag->map.name.ident) { |
| 4288 | case ResTable_map::ATTR_TYPE: |
| 4289 | attrType = bag->map.value.data; |
| 4290 | break; |
| 4291 | case ResTable_map::ATTR_MIN: |
| 4292 | attrMin = bag->map.value.data; |
| 4293 | break; |
| 4294 | case ResTable_map::ATTR_MAX: |
| 4295 | attrMax = bag->map.value.data; |
| 4296 | break; |
| 4297 | case ResTable_map::ATTR_L10N: |
| 4298 | l10nReq = bag->map.value.data; |
| 4299 | break; |
| 4300 | } |
| 4301 | bag++; |
| 4302 | cnt--; |
| 4303 | } |
| 4304 | unlockBag(bag); |
| 4305 | } else if (accessor && accessor->getAttributeType(attrID, &attrType)) { |
| 4306 | fromAccessor = true; |
| 4307 | if (attrType == ResTable_map::TYPE_ENUM |
| 4308 | || attrType == ResTable_map::TYPE_FLAGS |
| 4309 | || attrType == ResTable_map::TYPE_INTEGER) { |
| 4310 | accessor->getAttributeMin(attrID, &attrMin); |
| 4311 | accessor->getAttributeMax(attrID, &attrMax); |
| 4312 | } |
| 4313 | if (localizationSetting) { |
| 4314 | l10nReq = accessor->getAttributeL10N(attrID); |
| 4315 | } |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | const bool canStringCoerce = |
| 4320 | coerceType && (attrType&ResTable_map::TYPE_STRING) != 0; |
| 4321 | |
| 4322 | if (*s == '@') { |
| 4323 | outValue->dataType = outValue->TYPE_REFERENCE; |
| 4324 | |
| 4325 | // Note: we don't check attrType here because the reference can |
| 4326 | // be to any other type; we just need to count on the client making |
| 4327 | // sure the referenced type is correct. |
| 4328 | |
| 4329 | //printf("Looking up ref: %s\n", String8(s, len).string()); |
| 4330 | |
| 4331 | // It's a reference! |
| 4332 | if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') { |
| 4333 | outValue->data = 0; |
| 4334 | return true; |
| 4335 | } else { |
| 4336 | bool createIfNotFound = false; |
| 4337 | const char16_t* resourceRefName; |
| 4338 | int resourceNameLen; |
| 4339 | if (len > 2 && s[1] == '+') { |
| 4340 | createIfNotFound = true; |
| 4341 | resourceRefName = s + 2; |
| 4342 | resourceNameLen = len - 2; |
| 4343 | } else if (len > 2 && s[1] == '*') { |
| 4344 | enforcePrivate = false; |
| 4345 | resourceRefName = s + 2; |
| 4346 | resourceNameLen = len - 2; |
| 4347 | } else { |
| 4348 | createIfNotFound = false; |
| 4349 | resourceRefName = s + 1; |
| 4350 | resourceNameLen = len - 1; |
| 4351 | } |
| 4352 | String16 package, type, name; |
| 4353 | if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name, |
| 4354 | defType, defPackage, &errorMsg)) { |
| 4355 | if (accessor != NULL) { |
| 4356 | accessor->reportError(accessorCookie, errorMsg); |
| 4357 | } |
| 4358 | return false; |
| 4359 | } |
| 4360 | |
| 4361 | uint32_t specFlags = 0; |
| 4362 | uint32_t rid = identifierForName(name.string(), name.size(), type.string(), |
| 4363 | type.size(), package.string(), package.size(), &specFlags); |
| 4364 | if (rid != 0) { |
| 4365 | if (enforcePrivate) { |
| 4366 | if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 4367 | if (accessor != NULL) { |
| 4368 | accessor->reportError(accessorCookie, "Resource is not public."); |
| 4369 | } |
| 4370 | return false; |
| 4371 | } |
| 4372 | } |
| 4373 | if (!accessor) { |
| 4374 | outValue->data = rid; |
| 4375 | return true; |
| 4376 | } |
| 4377 | rid = Res_MAKEID( |
| 4378 | accessor->getRemappedPackage(Res_GETPACKAGE(rid)), |
| 4379 | Res_GETTYPE(rid), Res_GETENTRY(rid)); |
| 4380 | TABLE_NOISY(printf("Incl %s:%s/%s: 0x%08x\n", |
| 4381 | String8(package).string(), String8(type).string(), |
| 4382 | String8(name).string(), rid)); |
| 4383 | outValue->data = rid; |
| 4384 | return true; |
| 4385 | } |
| 4386 | |
| 4387 | if (accessor) { |
| 4388 | uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name, |
| 4389 | createIfNotFound); |
| 4390 | if (rid != 0) { |
| 4391 | TABLE_NOISY(printf("Pckg %s:%s/%s: 0x%08x\n", |
| 4392 | String8(package).string(), String8(type).string(), |
| 4393 | String8(name).string(), rid)); |
| 4394 | outValue->data = rid; |
| 4395 | return true; |
| 4396 | } |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | if (accessor != NULL) { |
| 4401 | accessor->reportError(accessorCookie, "No resource found that matches the given name"); |
| 4402 | } |
| 4403 | return false; |
| 4404 | } |
| 4405 | |
| 4406 | // if we got to here, and localization is required and it's not a reference, |
| 4407 | // complain and bail. |
| 4408 | if (l10nReq == ResTable_map::L10N_SUGGESTED) { |
| 4409 | if (localizationSetting) { |
| 4410 | if (accessor != NULL) { |
| 4411 | accessor->reportError(accessorCookie, "This attribute must be localized."); |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (*s == '#') { |
| 4417 | // It's a color! Convert to an integer of the form 0xaarrggbb. |
| 4418 | uint32_t color = 0; |
| 4419 | bool error = false; |
| 4420 | if (len == 4) { |
| 4421 | outValue->dataType = outValue->TYPE_INT_COLOR_RGB4; |
| 4422 | color |= 0xFF000000; |
| 4423 | color |= get_hex(s[1], &error) << 20; |
| 4424 | color |= get_hex(s[1], &error) << 16; |
| 4425 | color |= get_hex(s[2], &error) << 12; |
| 4426 | color |= get_hex(s[2], &error) << 8; |
| 4427 | color |= get_hex(s[3], &error) << 4; |
| 4428 | color |= get_hex(s[3], &error); |
| 4429 | } else if (len == 5) { |
| 4430 | outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4; |
| 4431 | color |= get_hex(s[1], &error) << 28; |
| 4432 | color |= get_hex(s[1], &error) << 24; |
| 4433 | color |= get_hex(s[2], &error) << 20; |
| 4434 | color |= get_hex(s[2], &error) << 16; |
| 4435 | color |= get_hex(s[3], &error) << 12; |
| 4436 | color |= get_hex(s[3], &error) << 8; |
| 4437 | color |= get_hex(s[4], &error) << 4; |
| 4438 | color |= get_hex(s[4], &error); |
| 4439 | } else if (len == 7) { |
| 4440 | outValue->dataType = outValue->TYPE_INT_COLOR_RGB8; |
| 4441 | color |= 0xFF000000; |
| 4442 | color |= get_hex(s[1], &error) << 20; |
| 4443 | color |= get_hex(s[2], &error) << 16; |
| 4444 | color |= get_hex(s[3], &error) << 12; |
| 4445 | color |= get_hex(s[4], &error) << 8; |
| 4446 | color |= get_hex(s[5], &error) << 4; |
| 4447 | color |= get_hex(s[6], &error); |
| 4448 | } else if (len == 9) { |
| 4449 | outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8; |
| 4450 | color |= get_hex(s[1], &error) << 28; |
| 4451 | color |= get_hex(s[2], &error) << 24; |
| 4452 | color |= get_hex(s[3], &error) << 20; |
| 4453 | color |= get_hex(s[4], &error) << 16; |
| 4454 | color |= get_hex(s[5], &error) << 12; |
| 4455 | color |= get_hex(s[6], &error) << 8; |
| 4456 | color |= get_hex(s[7], &error) << 4; |
| 4457 | color |= get_hex(s[8], &error); |
| 4458 | } else { |
| 4459 | error = true; |
| 4460 | } |
| 4461 | if (!error) { |
| 4462 | if ((attrType&ResTable_map::TYPE_COLOR) == 0) { |
| 4463 | if (!canStringCoerce) { |
| 4464 | if (accessor != NULL) { |
| 4465 | accessor->reportError(accessorCookie, |
| 4466 | "Color types not allowed"); |
| 4467 | } |
| 4468 | return false; |
| 4469 | } |
| 4470 | } else { |
| 4471 | outValue->data = color; |
| 4472 | //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color); |
| 4473 | return true; |
| 4474 | } |
| 4475 | } else { |
| 4476 | if ((attrType&ResTable_map::TYPE_COLOR) != 0) { |
| 4477 | if (accessor != NULL) { |
| 4478 | accessor->reportError(accessorCookie, "Color value not valid --" |
| 4479 | " must be #rgb, #argb, #rrggbb, or #aarrggbb"); |
| 4480 | } |
| 4481 | #if 0 |
| 4482 | fprintf(stderr, "%s: Color ID %s value %s is not valid\n", |
| 4483 | "Resource File", //(const char*)in->getPrintableSource(), |
| 4484 | String8(*curTag).string(), |
| 4485 | String8(s, len).string()); |
| 4486 | #endif |
| 4487 | return false; |
| 4488 | } |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | if (*s == '?') { |
| 4493 | outValue->dataType = outValue->TYPE_ATTRIBUTE; |
| 4494 | |
| 4495 | // Note: we don't check attrType here because the reference can |
| 4496 | // be to any other type; we just need to count on the client making |
| 4497 | // sure the referenced type is correct. |
| 4498 | |
| 4499 | //printf("Looking up attr: %s\n", String8(s, len).string()); |
| 4500 | |
| 4501 | static const String16 attr16("attr"); |
| 4502 | String16 package, type, name; |
| 4503 | if (!expandResourceRef(s+1, len-1, &package, &type, &name, |
| 4504 | &attr16, defPackage, &errorMsg)) { |
| 4505 | if (accessor != NULL) { |
| 4506 | accessor->reportError(accessorCookie, errorMsg); |
| 4507 | } |
| 4508 | return false; |
| 4509 | } |
| 4510 | |
| 4511 | //printf("Pkg: %s, Type: %s, Name: %s\n", |
| 4512 | // String8(package).string(), String8(type).string(), |
| 4513 | // String8(name).string()); |
| 4514 | uint32_t specFlags = 0; |
| 4515 | uint32_t rid = |
| 4516 | identifierForName(name.string(), name.size(), |
| 4517 | type.string(), type.size(), |
| 4518 | package.string(), package.size(), &specFlags); |
| 4519 | if (rid != 0) { |
| 4520 | if (enforcePrivate) { |
| 4521 | if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 4522 | if (accessor != NULL) { |
| 4523 | accessor->reportError(accessorCookie, "Attribute is not public."); |
| 4524 | } |
| 4525 | return false; |
| 4526 | } |
| 4527 | } |
| 4528 | if (!accessor) { |
| 4529 | outValue->data = rid; |
| 4530 | return true; |
| 4531 | } |
| 4532 | rid = Res_MAKEID( |
| 4533 | accessor->getRemappedPackage(Res_GETPACKAGE(rid)), |
| 4534 | Res_GETTYPE(rid), Res_GETENTRY(rid)); |
| 4535 | //printf("Incl %s:%s/%s: 0x%08x\n", |
| 4536 | // String8(package).string(), String8(type).string(), |
| 4537 | // String8(name).string(), rid); |
| 4538 | outValue->data = rid; |
| 4539 | return true; |
| 4540 | } |
| 4541 | |
| 4542 | if (accessor) { |
| 4543 | uint32_t rid = accessor->getCustomResource(package, type, name); |
| 4544 | if (rid != 0) { |
| 4545 | //printf("Mine %s:%s/%s: 0x%08x\n", |
| 4546 | // String8(package).string(), String8(type).string(), |
| 4547 | // String8(name).string(), rid); |
| 4548 | outValue->data = rid; |
| 4549 | return true; |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | if (accessor != NULL) { |
| 4554 | accessor->reportError(accessorCookie, "No resource found that matches the given name"); |
| 4555 | } |
| 4556 | return false; |
| 4557 | } |
| 4558 | |
| 4559 | if (stringToInt(s, len, outValue)) { |
| 4560 | if ((attrType&ResTable_map::TYPE_INTEGER) == 0) { |
| 4561 | // If this type does not allow integers, but does allow floats, |
| 4562 | // fall through on this error case because the float type should |
| 4563 | // be able to accept any integer value. |
| 4564 | if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) { |
| 4565 | if (accessor != NULL) { |
| 4566 | accessor->reportError(accessorCookie, "Integer types not allowed"); |
| 4567 | } |
| 4568 | return false; |
| 4569 | } |
| 4570 | } else { |
| 4571 | if (((int32_t)outValue->data) < ((int32_t)attrMin) |
| 4572 | || ((int32_t)outValue->data) > ((int32_t)attrMax)) { |
| 4573 | if (accessor != NULL) { |
| 4574 | accessor->reportError(accessorCookie, "Integer value out of range"); |
| 4575 | } |
| 4576 | return false; |
| 4577 | } |
| 4578 | return true; |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | if (stringToFloat(s, len, outValue)) { |
| 4583 | if (outValue->dataType == Res_value::TYPE_DIMENSION) { |
| 4584 | if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) { |
| 4585 | return true; |
| 4586 | } |
| 4587 | if (!canStringCoerce) { |
| 4588 | if (accessor != NULL) { |
| 4589 | accessor->reportError(accessorCookie, "Dimension types not allowed"); |
| 4590 | } |
| 4591 | return false; |
| 4592 | } |
| 4593 | } else if (outValue->dataType == Res_value::TYPE_FRACTION) { |
| 4594 | if ((attrType&ResTable_map::TYPE_FRACTION) != 0) { |
| 4595 | return true; |
| 4596 | } |
| 4597 | if (!canStringCoerce) { |
| 4598 | if (accessor != NULL) { |
| 4599 | accessor->reportError(accessorCookie, "Fraction types not allowed"); |
| 4600 | } |
| 4601 | return false; |
| 4602 | } |
| 4603 | } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) { |
| 4604 | if (!canStringCoerce) { |
| 4605 | if (accessor != NULL) { |
| 4606 | accessor->reportError(accessorCookie, "Float types not allowed"); |
| 4607 | } |
| 4608 | return false; |
| 4609 | } |
| 4610 | } else { |
| 4611 | return true; |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | if (len == 4) { |
| 4616 | if ((s[0] == 't' || s[0] == 'T') && |
| 4617 | (s[1] == 'r' || s[1] == 'R') && |
| 4618 | (s[2] == 'u' || s[2] == 'U') && |
| 4619 | (s[3] == 'e' || s[3] == 'E')) { |
| 4620 | if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { |
| 4621 | if (!canStringCoerce) { |
| 4622 | if (accessor != NULL) { |
| 4623 | accessor->reportError(accessorCookie, "Boolean types not allowed"); |
| 4624 | } |
| 4625 | return false; |
| 4626 | } |
| 4627 | } else { |
| 4628 | outValue->dataType = outValue->TYPE_INT_BOOLEAN; |
| 4629 | outValue->data = (uint32_t)-1; |
| 4630 | return true; |
| 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | if (len == 5) { |
| 4636 | if ((s[0] == 'f' || s[0] == 'F') && |
| 4637 | (s[1] == 'a' || s[1] == 'A') && |
| 4638 | (s[2] == 'l' || s[2] == 'L') && |
| 4639 | (s[3] == 's' || s[3] == 'S') && |
| 4640 | (s[4] == 'e' || s[4] == 'E')) { |
| 4641 | if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { |
| 4642 | if (!canStringCoerce) { |
| 4643 | if (accessor != NULL) { |
| 4644 | accessor->reportError(accessorCookie, "Boolean types not allowed"); |
| 4645 | } |
| 4646 | return false; |
| 4647 | } |
| 4648 | } else { |
| 4649 | outValue->dataType = outValue->TYPE_INT_BOOLEAN; |
| 4650 | outValue->data = 0; |
| 4651 | return true; |
| 4652 | } |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | if ((attrType&ResTable_map::TYPE_ENUM) != 0) { |
| 4657 | const ssize_t p = getResourcePackageIndex(attrID); |
| 4658 | const bag_entry* bag; |
| 4659 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 4660 | //printf("Got %d for enum\n", cnt); |
| 4661 | if (cnt >= 0) { |
| 4662 | resource_name rname; |
| 4663 | while (cnt > 0) { |
| 4664 | if (!Res_INTERNALID(bag->map.name.ident)) { |
| 4665 | //printf("Trying attr #%08x\n", bag->map.name.ident); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 4666 | if (getResourceName(bag->map.name.ident, false, &rname)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | #if 0 |
| 4668 | printf("Matching %s against %s (0x%08x)\n", |
| 4669 | String8(s, len).string(), |
| 4670 | String8(rname.name, rname.nameLen).string(), |
| 4671 | bag->map.name.ident); |
| 4672 | #endif |
| 4673 | if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) { |
| 4674 | outValue->dataType = bag->map.value.dataType; |
| 4675 | outValue->data = bag->map.value.data; |
| 4676 | unlockBag(bag); |
| 4677 | return true; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | } |
| 4682 | bag++; |
| 4683 | cnt--; |
| 4684 | } |
| 4685 | unlockBag(bag); |
| 4686 | } |
| 4687 | |
| 4688 | if (fromAccessor) { |
| 4689 | if (accessor->getAttributeEnum(attrID, s, len, outValue)) { |
| 4690 | return true; |
| 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | if ((attrType&ResTable_map::TYPE_FLAGS) != 0) { |
| 4696 | const ssize_t p = getResourcePackageIndex(attrID); |
| 4697 | const bag_entry* bag; |
| 4698 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 4699 | //printf("Got %d for flags\n", cnt); |
| 4700 | if (cnt >= 0) { |
| 4701 | bool failed = false; |
| 4702 | resource_name rname; |
| 4703 | outValue->dataType = Res_value::TYPE_INT_HEX; |
| 4704 | outValue->data = 0; |
| 4705 | const char16_t* end = s + len; |
| 4706 | const char16_t* pos = s; |
| 4707 | while (pos < end && !failed) { |
| 4708 | const char16_t* start = pos; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4709 | pos++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4710 | while (pos < end && *pos != '|') { |
| 4711 | pos++; |
| 4712 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4713 | //printf("Looking for: %s\n", String8(start, pos-start).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4714 | const bag_entry* bagi = bag; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4715 | ssize_t i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4716 | for (i=0; i<cnt; i++, bagi++) { |
| 4717 | if (!Res_INTERNALID(bagi->map.name.ident)) { |
| 4718 | //printf("Trying attr #%08x\n", bagi->map.name.ident); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 4719 | if (getResourceName(bagi->map.name.ident, false, &rname)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4720 | #if 0 |
| 4721 | printf("Matching %s against %s (0x%08x)\n", |
| 4722 | String8(start,pos-start).string(), |
| 4723 | String8(rname.name, rname.nameLen).string(), |
| 4724 | bagi->map.name.ident); |
| 4725 | #endif |
| 4726 | if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) { |
| 4727 | outValue->data |= bagi->map.value.data; |
| 4728 | break; |
| 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | if (i >= cnt) { |
| 4734 | // Didn't find this flag identifier. |
| 4735 | failed = true; |
| 4736 | } |
| 4737 | if (pos < end) { |
| 4738 | pos++; |
| 4739 | } |
| 4740 | } |
| 4741 | unlockBag(bag); |
| 4742 | if (!failed) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4743 | //printf("Final flag value: 0x%lx\n", outValue->data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4744 | return true; |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | |
| 4749 | if (fromAccessor) { |
| 4750 | if (accessor->getAttributeFlags(attrID, s, len, outValue)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4751 | //printf("Final flag value: 0x%lx\n", outValue->data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4752 | return true; |
| 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | if ((attrType&ResTable_map::TYPE_STRING) == 0) { |
| 4758 | if (accessor != NULL) { |
| 4759 | accessor->reportError(accessorCookie, "String types not allowed"); |
| 4760 | } |
| 4761 | return false; |
| 4762 | } |
| 4763 | |
| 4764 | // Generic string handling... |
| 4765 | outValue->dataType = outValue->TYPE_STRING; |
| 4766 | if (outString) { |
| 4767 | bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg); |
| 4768 | if (accessor != NULL) { |
| 4769 | accessor->reportError(accessorCookie, errorMsg); |
| 4770 | } |
| 4771 | return failed; |
| 4772 | } |
| 4773 | |
| 4774 | return true; |
| 4775 | } |
| 4776 | |
| 4777 | bool ResTable::collectString(String16* outString, |
| 4778 | const char16_t* s, size_t len, |
| 4779 | bool preserveSpaces, |
| 4780 | const char** outErrorMsg, |
| 4781 | bool append) |
| 4782 | { |
| 4783 | String16 tmp; |
| 4784 | |
| 4785 | char quoted = 0; |
| 4786 | const char16_t* p = s; |
| 4787 | while (p < (s+len)) { |
| 4788 | while (p < (s+len)) { |
| 4789 | const char16_t c = *p; |
| 4790 | if (c == '\\') { |
| 4791 | break; |
| 4792 | } |
| 4793 | if (!preserveSpaces) { |
| 4794 | if (quoted == 0 && isspace16(c) |
| 4795 | && (c != ' ' || isspace16(*(p+1)))) { |
| 4796 | break; |
| 4797 | } |
| 4798 | if (c == '"' && (quoted == 0 || quoted == '"')) { |
| 4799 | break; |
| 4800 | } |
| 4801 | if (c == '\'' && (quoted == 0 || quoted == '\'')) { |
Eric Fischer | c87d252 | 2009-09-01 15:20:30 -0700 | [diff] [blame] | 4802 | /* |
| 4803 | * In practice, when people write ' instead of \' |
| 4804 | * in a string, they are doing it by accident |
| 4805 | * instead of really meaning to use ' as a quoting |
| 4806 | * character. Warn them so they don't lose it. |
| 4807 | */ |
| 4808 | if (outErrorMsg) { |
| 4809 | *outErrorMsg = "Apostrophe not preceded by \\"; |
| 4810 | } |
| 4811 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | p++; |
| 4815 | } |
| 4816 | if (p < (s+len)) { |
| 4817 | if (p > s) { |
| 4818 | tmp.append(String16(s, p-s)); |
| 4819 | } |
| 4820 | if (!preserveSpaces && (*p == '"' || *p == '\'')) { |
| 4821 | if (quoted == 0) { |
| 4822 | quoted = *p; |
| 4823 | } else { |
| 4824 | quoted = 0; |
| 4825 | } |
| 4826 | p++; |
| 4827 | } else if (!preserveSpaces && isspace16(*p)) { |
| 4828 | // Space outside of a quote -- consume all spaces and |
| 4829 | // leave a single plain space char. |
| 4830 | tmp.append(String16(" ")); |
| 4831 | p++; |
| 4832 | while (p < (s+len) && isspace16(*p)) { |
| 4833 | p++; |
| 4834 | } |
| 4835 | } else if (*p == '\\') { |
| 4836 | p++; |
| 4837 | if (p < (s+len)) { |
| 4838 | switch (*p) { |
| 4839 | case 't': |
| 4840 | tmp.append(String16("\t")); |
| 4841 | break; |
| 4842 | case 'n': |
| 4843 | tmp.append(String16("\n")); |
| 4844 | break; |
| 4845 | case '#': |
| 4846 | tmp.append(String16("#")); |
| 4847 | break; |
| 4848 | case '@': |
| 4849 | tmp.append(String16("@")); |
| 4850 | break; |
| 4851 | case '?': |
| 4852 | tmp.append(String16("?")); |
| 4853 | break; |
| 4854 | case '"': |
| 4855 | tmp.append(String16("\"")); |
| 4856 | break; |
| 4857 | case '\'': |
| 4858 | tmp.append(String16("'")); |
| 4859 | break; |
| 4860 | case '\\': |
| 4861 | tmp.append(String16("\\")); |
| 4862 | break; |
| 4863 | case 'u': |
| 4864 | { |
| 4865 | char16_t chr = 0; |
| 4866 | int i = 0; |
| 4867 | while (i < 4 && p[1] != 0) { |
| 4868 | p++; |
| 4869 | i++; |
| 4870 | int c; |
| 4871 | if (*p >= '0' && *p <= '9') { |
| 4872 | c = *p - '0'; |
| 4873 | } else if (*p >= 'a' && *p <= 'f') { |
| 4874 | c = *p - 'a' + 10; |
| 4875 | } else if (*p >= 'A' && *p <= 'F') { |
| 4876 | c = *p - 'A' + 10; |
| 4877 | } else { |
| 4878 | if (outErrorMsg) { |
| 4879 | *outErrorMsg = "Bad character in \\u unicode escape sequence"; |
| 4880 | } |
| 4881 | return false; |
| 4882 | } |
| 4883 | chr = (chr<<4) | c; |
| 4884 | } |
| 4885 | tmp.append(String16(&chr, 1)); |
| 4886 | } break; |
| 4887 | default: |
| 4888 | // ignore unknown escape chars. |
| 4889 | break; |
| 4890 | } |
| 4891 | p++; |
| 4892 | } |
| 4893 | } |
| 4894 | len -= (p-s); |
| 4895 | s = p; |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | if (tmp.size() != 0) { |
| 4900 | if (len > 0) { |
| 4901 | tmp.append(String16(s, len)); |
| 4902 | } |
| 4903 | if (append) { |
| 4904 | outString->append(tmp); |
| 4905 | } else { |
| 4906 | outString->setTo(tmp); |
| 4907 | } |
| 4908 | } else { |
| 4909 | if (append) { |
| 4910 | outString->append(String16(s, len)); |
| 4911 | } else { |
| 4912 | outString->setTo(s, len); |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | return true; |
| 4917 | } |
| 4918 | |
| 4919 | size_t ResTable::getBasePackageCount() const |
| 4920 | { |
| 4921 | if (mError != NO_ERROR) { |
| 4922 | return 0; |
| 4923 | } |
| 4924 | return mPackageGroups.size(); |
| 4925 | } |
| 4926 | |
| 4927 | const char16_t* ResTable::getBasePackageName(size_t idx) const |
| 4928 | { |
| 4929 | if (mError != NO_ERROR) { |
| 4930 | return 0; |
| 4931 | } |
| 4932 | LOG_FATAL_IF(idx >= mPackageGroups.size(), |
| 4933 | "Requested package index %d past package count %d", |
| 4934 | (int)idx, (int)mPackageGroups.size()); |
| 4935 | return mPackageGroups[idx]->name.string(); |
| 4936 | } |
| 4937 | |
| 4938 | uint32_t ResTable::getBasePackageId(size_t idx) const |
| 4939 | { |
| 4940 | if (mError != NO_ERROR) { |
| 4941 | return 0; |
| 4942 | } |
| 4943 | LOG_FATAL_IF(idx >= mPackageGroups.size(), |
| 4944 | "Requested package index %d past package count %d", |
| 4945 | (int)idx, (int)mPackageGroups.size()); |
| 4946 | return mPackageGroups[idx]->id; |
| 4947 | } |
| 4948 | |
| 4949 | size_t ResTable::getTableCount() const |
| 4950 | { |
| 4951 | return mHeaders.size(); |
| 4952 | } |
| 4953 | |
| 4954 | const ResStringPool* ResTable::getTableStringBlock(size_t index) const |
| 4955 | { |
| 4956 | return &mHeaders[index]->values; |
| 4957 | } |
| 4958 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 4959 | int32_t ResTable::getTableCookie(size_t index) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4960 | { |
| 4961 | return mHeaders[index]->cookie; |
| 4962 | } |
| 4963 | |
| 4964 | void ResTable::getConfigurations(Vector<ResTable_config>* configs) const |
| 4965 | { |
| 4966 | const size_t I = mPackageGroups.size(); |
| 4967 | for (size_t i=0; i<I; i++) { |
| 4968 | const PackageGroup* packageGroup = mPackageGroups[i]; |
| 4969 | const size_t J = packageGroup->packages.size(); |
| 4970 | for (size_t j=0; j<J; j++) { |
| 4971 | const Package* package = packageGroup->packages[j]; |
| 4972 | const size_t K = package->types.size(); |
| 4973 | for (size_t k=0; k<K; k++) { |
| 4974 | const Type* type = package->types[k]; |
| 4975 | if (type == NULL) continue; |
| 4976 | const size_t L = type->configs.size(); |
| 4977 | for (size_t l=0; l<L; l++) { |
| 4978 | const ResTable_type* config = type->configs[l]; |
| 4979 | const ResTable_config* cfg = &config->config; |
| 4980 | // only insert unique |
| 4981 | const size_t M = configs->size(); |
| 4982 | size_t m; |
| 4983 | for (m=0; m<M; m++) { |
| 4984 | if (0 == (*configs)[m].compare(*cfg)) { |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | // if we didn't find it |
| 4989 | if (m == M) { |
| 4990 | configs->add(*cfg); |
| 4991 | } |
| 4992 | } |
| 4993 | } |
| 4994 | } |
| 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | void ResTable::getLocales(Vector<String8>* locales) const |
| 4999 | { |
| 5000 | Vector<ResTable_config> configs; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5001 | ALOGV("calling getConfigurations"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5002 | getConfigurations(&configs); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5003 | ALOGV("called getConfigurations size=%d", (int)configs.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5004 | const size_t I = configs.size(); |
| 5005 | for (size_t i=0; i<I; i++) { |
| 5006 | char locale[6]; |
| 5007 | configs[i].getLocale(locale); |
| 5008 | const size_t J = locales->size(); |
| 5009 | size_t j; |
| 5010 | for (j=0; j<J; j++) { |
| 5011 | if (0 == strcmp(locale, (*locales)[j].string())) { |
| 5012 | break; |
| 5013 | } |
| 5014 | } |
| 5015 | if (j == J) { |
| 5016 | locales->add(String8(locale)); |
| 5017 | } |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | ssize_t ResTable::getEntry( |
| 5022 | const Package* package, int typeIndex, int entryIndex, |
| 5023 | const ResTable_config* config, |
| 5024 | const ResTable_type** outType, const ResTable_entry** outEntry, |
| 5025 | const Type** outTypeClass) const |
| 5026 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5027 | ALOGV("Getting entry from package %p\n", package); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5028 | const ResTable_package* const pkg = package->package; |
| 5029 | |
| 5030 | const Type* allTypes = package->getType(typeIndex); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5031 | ALOGV("allTypes=%p\n", allTypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5032 | if (allTypes == NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5033 | ALOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5034 | return 0; |
| 5035 | } |
| 5036 | |
| 5037 | if ((size_t)entryIndex >= allTypes->entryCount) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5038 | ALOGW("getEntry failing because entryIndex %d is beyond type entryCount %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5039 | entryIndex, (int)allTypes->entryCount); |
| 5040 | return BAD_TYPE; |
| 5041 | } |
| 5042 | |
| 5043 | const ResTable_type* type = NULL; |
| 5044 | uint32_t offset = ResTable_type::NO_ENTRY; |
| 5045 | ResTable_config bestConfig; |
| 5046 | memset(&bestConfig, 0, sizeof(bestConfig)); // make the compiler shut up |
| 5047 | |
| 5048 | const size_t NT = allTypes->configs.size(); |
| 5049 | for (size_t i=0; i<NT; i++) { |
| 5050 | const ResTable_type* const thisType = allTypes->configs[i]; |
| 5051 | if (thisType == NULL) continue; |
| 5052 | |
| 5053 | ResTable_config thisConfig; |
| 5054 | thisConfig.copyFromDtoH(thisType->config); |
| 5055 | |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5056 | TABLE_GETENTRY(ALOGI("Match entry 0x%x in type 0x%x (sz 0x%x): %s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5057 | entryIndex, typeIndex+1, dtohl(thisType->config.size), |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 5058 | thisConfig.toString().string())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5059 | |
| 5060 | // Check to make sure this one is valid for the current parameters. |
| 5061 | if (config && !thisConfig.match(*config)) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5062 | TABLE_GETENTRY(ALOGI("Does not match config!\n")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5063 | continue; |
| 5064 | } |
| 5065 | |
| 5066 | // Check if there is the desired entry in this type. |
| 5067 | |
| 5068 | const uint8_t* const end = ((const uint8_t*)thisType) |
| 5069 | + dtohl(thisType->header.size); |
| 5070 | const uint32_t* const eindex = (const uint32_t*) |
| 5071 | (((const uint8_t*)thisType) + dtohs(thisType->header.headerSize)); |
| 5072 | |
| 5073 | uint32_t thisOffset = dtohl(eindex[entryIndex]); |
| 5074 | if (thisOffset == ResTable_type::NO_ENTRY) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5075 | TABLE_GETENTRY(ALOGI("Skipping because it is not defined!\n")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | if (type != NULL) { |
| 5080 | // Check if this one is less specific than the last found. If so, |
| 5081 | // we will skip it. We check starting with things we most care |
| 5082 | // about to those we least care about. |
| 5083 | if (!thisConfig.isBetterThan(bestConfig, config)) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5084 | TABLE_GETENTRY(ALOGI("This config is worse than last!\n")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5085 | continue; |
| 5086 | } |
| 5087 | } |
| 5088 | |
| 5089 | type = thisType; |
| 5090 | offset = thisOffset; |
| 5091 | bestConfig = thisConfig; |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5092 | TABLE_GETENTRY(ALOGI("Best entry so far -- using it!\n")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5093 | if (!config) break; |
| 5094 | } |
| 5095 | |
| 5096 | if (type == NULL) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5097 | TABLE_GETENTRY(ALOGI("No value found for requested entry!\n")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5098 | return BAD_INDEX; |
| 5099 | } |
| 5100 | |
| 5101 | offset += dtohl(type->entriesStart); |
| 5102 | TABLE_NOISY(aout << "Looking in resource table " << package->header->header |
| 5103 | << ", typeOff=" |
| 5104 | << (void*)(((const char*)type)-((const char*)package->header->header)) |
| 5105 | << ", offset=" << (void*)offset << endl); |
| 5106 | |
| 5107 | if (offset > (dtohl(type->header.size)-sizeof(ResTable_entry))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5108 | ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5109 | offset, dtohl(type->header.size)); |
| 5110 | return BAD_TYPE; |
| 5111 | } |
| 5112 | if ((offset&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5113 | ALOGW("ResTable_entry at 0x%x is not on an integer boundary", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5114 | offset); |
| 5115 | return BAD_TYPE; |
| 5116 | } |
| 5117 | |
| 5118 | const ResTable_entry* const entry = (const ResTable_entry*) |
| 5119 | (((const uint8_t*)type) + offset); |
| 5120 | if (dtohs(entry->size) < sizeof(*entry)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5121 | ALOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5122 | return BAD_TYPE; |
| 5123 | } |
| 5124 | |
| 5125 | *outType = type; |
| 5126 | *outEntry = entry; |
| 5127 | if (outTypeClass != NULL) { |
| 5128 | *outTypeClass = allTypes; |
| 5129 | } |
| 5130 | return offset + dtohs(entry->size); |
| 5131 | } |
| 5132 | |
| 5133 | status_t ResTable::parsePackage(const ResTable_package* const pkg, |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5134 | const Header* const header, uint32_t idmap_id) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5135 | { |
| 5136 | const uint8_t* base = (const uint8_t*)pkg; |
| 5137 | status_t err = validate_chunk(&pkg->header, sizeof(*pkg), |
| 5138 | header->dataEnd, "ResTable_package"); |
| 5139 | if (err != NO_ERROR) { |
| 5140 | return (mError=err); |
| 5141 | } |
| 5142 | |
| 5143 | const size_t pkgSize = dtohl(pkg->header.size); |
| 5144 | |
| 5145 | if (dtohl(pkg->typeStrings) >= pkgSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5146 | ALOGW("ResTable_package type strings at %p are past chunk size %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5147 | (void*)dtohl(pkg->typeStrings), (void*)pkgSize); |
| 5148 | return (mError=BAD_TYPE); |
| 5149 | } |
| 5150 | if ((dtohl(pkg->typeStrings)&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5151 | ALOGW("ResTable_package type strings at %p is not on an integer boundary.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5152 | (void*)dtohl(pkg->typeStrings)); |
| 5153 | return (mError=BAD_TYPE); |
| 5154 | } |
| 5155 | if (dtohl(pkg->keyStrings) >= pkgSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5156 | ALOGW("ResTable_package key strings at %p are past chunk size %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5157 | (void*)dtohl(pkg->keyStrings), (void*)pkgSize); |
| 5158 | return (mError=BAD_TYPE); |
| 5159 | } |
| 5160 | if ((dtohl(pkg->keyStrings)&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5161 | ALOGW("ResTable_package key strings at %p is not on an integer boundary.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5162 | (void*)dtohl(pkg->keyStrings)); |
| 5163 | return (mError=BAD_TYPE); |
| 5164 | } |
| 5165 | |
| 5166 | Package* package = NULL; |
| 5167 | PackageGroup* group = NULL; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5168 | uint32_t id = idmap_id != 0 ? idmap_id : dtohl(pkg->id); |
| 5169 | // If at this point id == 0, pkg is an overlay package without a |
| 5170 | // corresponding idmap. During regular usage, overlay packages are |
| 5171 | // always loaded alongside their idmaps, but during idmap creation |
| 5172 | // the package is temporarily loaded by itself. |
| 5173 | if (id < 256) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5174 | |
| 5175 | package = new Package(this, header, pkg); |
| 5176 | if (package == NULL) { |
| 5177 | return (mError=NO_MEMORY); |
| 5178 | } |
| 5179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5180 | size_t idx = mPackageMap[id]; |
| 5181 | if (idx == 0) { |
| 5182 | idx = mPackageGroups.size()+1; |
| 5183 | |
| 5184 | char16_t tmpName[sizeof(pkg->name)/sizeof(char16_t)]; |
| 5185 | strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(char16_t)); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5186 | group = new PackageGroup(this, String16(tmpName), id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5187 | if (group == NULL) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5188 | delete package; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5189 | return (mError=NO_MEMORY); |
| 5190 | } |
| 5191 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5192 | err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5193 | header->dataEnd-(base+dtohl(pkg->typeStrings))); |
| 5194 | if (err != NO_ERROR) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5195 | delete group; |
| 5196 | delete package; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5197 | return (mError=err); |
| 5198 | } |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5199 | err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5200 | header->dataEnd-(base+dtohl(pkg->keyStrings))); |
| 5201 | if (err != NO_ERROR) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5202 | delete group; |
| 5203 | delete package; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5204 | return (mError=err); |
| 5205 | } |
| 5206 | |
| 5207 | //printf("Adding new package id %d at index %d\n", id, idx); |
| 5208 | err = mPackageGroups.add(group); |
| 5209 | if (err < NO_ERROR) { |
| 5210 | return (mError=err); |
| 5211 | } |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 5212 | group->basePackage = package; |
| 5213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5214 | mPackageMap[id] = (uint8_t)idx; |
| 5215 | } else { |
| 5216 | group = mPackageGroups.itemAt(idx-1); |
| 5217 | if (group == NULL) { |
| 5218 | return (mError=UNKNOWN_ERROR); |
| 5219 | } |
| 5220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | err = group->packages.add(package); |
| 5222 | if (err < NO_ERROR) { |
| 5223 | return (mError=err); |
| 5224 | } |
| 5225 | } else { |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5226 | LOG_ALWAYS_FATAL("Package id out of range"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | return NO_ERROR; |
| 5228 | } |
| 5229 | |
| 5230 | |
| 5231 | // Iterate through all chunks. |
| 5232 | size_t curPackage = 0; |
| 5233 | |
| 5234 | const ResChunk_header* chunk = |
| 5235 | (const ResChunk_header*)(((const uint8_t*)pkg) |
| 5236 | + dtohs(pkg->header.headerSize)); |
| 5237 | const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size); |
| 5238 | while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) && |
| 5239 | ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) { |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 5240 | TABLE_NOISY(ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5241 | dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), |
| 5242 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)))); |
| 5243 | const size_t csize = dtohl(chunk->size); |
| 5244 | const uint16_t ctype = dtohs(chunk->type); |
| 5245 | if (ctype == RES_TABLE_TYPE_SPEC_TYPE) { |
| 5246 | const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk); |
| 5247 | err = validate_chunk(&typeSpec->header, sizeof(*typeSpec), |
| 5248 | endPos, "ResTable_typeSpec"); |
| 5249 | if (err != NO_ERROR) { |
| 5250 | return (mError=err); |
| 5251 | } |
| 5252 | |
| 5253 | const size_t typeSpecSize = dtohl(typeSpec->header.size); |
| 5254 | |
| 5255 | LOAD_TABLE_NOISY(printf("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n", |
| 5256 | (void*)(base-(const uint8_t*)chunk), |
| 5257 | dtohs(typeSpec->header.type), |
| 5258 | dtohs(typeSpec->header.headerSize), |
| 5259 | (void*)typeSize)); |
| 5260 | // look for block overrun or int overflow when multiplying by 4 |
| 5261 | if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t)) |
| 5262 | || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*dtohl(typeSpec->entryCount)) |
| 5263 | > typeSpecSize)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5264 | ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5265 | (void*)(dtohs(typeSpec->header.headerSize) |
| 5266 | +(sizeof(uint32_t)*dtohl(typeSpec->entryCount))), |
| 5267 | (void*)typeSpecSize); |
| 5268 | return (mError=BAD_TYPE); |
| 5269 | } |
| 5270 | |
| 5271 | if (typeSpec->id == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5272 | ALOGW("ResTable_type has an id of 0."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5273 | return (mError=BAD_TYPE); |
| 5274 | } |
| 5275 | |
| 5276 | while (package->types.size() < typeSpec->id) { |
| 5277 | package->types.add(NULL); |
| 5278 | } |
| 5279 | Type* t = package->types[typeSpec->id-1]; |
| 5280 | if (t == NULL) { |
| 5281 | t = new Type(header, package, dtohl(typeSpec->entryCount)); |
| 5282 | package->types.editItemAt(typeSpec->id-1) = t; |
| 5283 | } else if (dtohl(typeSpec->entryCount) != t->entryCount) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5284 | ALOGW("ResTable_typeSpec entry count inconsistent: given %d, previously %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5285 | (int)dtohl(typeSpec->entryCount), (int)t->entryCount); |
| 5286 | return (mError=BAD_TYPE); |
| 5287 | } |
| 5288 | t->typeSpecFlags = (const uint32_t*)( |
| 5289 | ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize)); |
| 5290 | t->typeSpec = typeSpec; |
| 5291 | |
| 5292 | } else if (ctype == RES_TABLE_TYPE_TYPE) { |
| 5293 | const ResTable_type* type = (const ResTable_type*)(chunk); |
| 5294 | err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4, |
| 5295 | endPos, "ResTable_type"); |
| 5296 | if (err != NO_ERROR) { |
| 5297 | return (mError=err); |
| 5298 | } |
| 5299 | |
| 5300 | const size_t typeSize = dtohl(type->header.size); |
| 5301 | |
| 5302 | LOAD_TABLE_NOISY(printf("Type off %p: type=0x%x, headerSize=0x%x, size=%p\n", |
| 5303 | (void*)(base-(const uint8_t*)chunk), |
| 5304 | dtohs(type->header.type), |
| 5305 | dtohs(type->header.headerSize), |
| 5306 | (void*)typeSize)); |
| 5307 | if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*dtohl(type->entryCount)) |
| 5308 | > typeSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5309 | ALOGW("ResTable_type entry index to %p extends beyond chunk end %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5310 | (void*)(dtohs(type->header.headerSize) |
| 5311 | +(sizeof(uint32_t)*dtohl(type->entryCount))), |
| 5312 | (void*)typeSize); |
| 5313 | return (mError=BAD_TYPE); |
| 5314 | } |
| 5315 | if (dtohl(type->entryCount) != 0 |
| 5316 | && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5317 | ALOGW("ResTable_type entriesStart at %p extends beyond chunk end %p.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5318 | (void*)dtohl(type->entriesStart), (void*)typeSize); |
| 5319 | return (mError=BAD_TYPE); |
| 5320 | } |
| 5321 | if (type->id == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5322 | ALOGW("ResTable_type has an id of 0."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5323 | return (mError=BAD_TYPE); |
| 5324 | } |
| 5325 | |
| 5326 | while (package->types.size() < type->id) { |
| 5327 | package->types.add(NULL); |
| 5328 | } |
| 5329 | Type* t = package->types[type->id-1]; |
| 5330 | if (t == NULL) { |
| 5331 | t = new Type(header, package, dtohl(type->entryCount)); |
| 5332 | package->types.editItemAt(type->id-1) = t; |
| 5333 | } else if (dtohl(type->entryCount) != t->entryCount) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5334 | ALOGW("ResTable_type entry count inconsistent: given %d, previously %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5335 | (int)dtohl(type->entryCount), (int)t->entryCount); |
| 5336 | return (mError=BAD_TYPE); |
| 5337 | } |
| 5338 | |
| 5339 | TABLE_GETENTRY( |
| 5340 | ResTable_config thisConfig; |
| 5341 | thisConfig.copyFromDtoH(type->config); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 5342 | ALOGI("Adding config to type %d: %s\n", |
| 5343 | type->id, thisConfig.toString().string())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5344 | t->configs.add(type); |
| 5345 | } else { |
| 5346 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), |
| 5347 | endPos, "ResTable_package:unknown"); |
| 5348 | if (err != NO_ERROR) { |
| 5349 | return (mError=err); |
| 5350 | } |
| 5351 | } |
| 5352 | chunk = (const ResChunk_header*) |
| 5353 | (((const uint8_t*)chunk) + csize); |
| 5354 | } |
| 5355 | |
| 5356 | if (group->typeCount == 0) { |
| 5357 | group->typeCount = package->types.size(); |
| 5358 | } |
| 5359 | |
| 5360 | return NO_ERROR; |
| 5361 | } |
| 5362 | |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5363 | status_t ResTable::createIdmap(const ResTable& overlay, |
| 5364 | uint32_t targetCrc, uint32_t overlayCrc, |
| 5365 | const char* targetPath, const char* overlayPath, |
| 5366 | void** outData, size_t* outSize) const |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5367 | { |
| 5368 | // see README for details on the format of map |
| 5369 | if (mPackageGroups.size() == 0) { |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5370 | ALOGW("idmap: target package has no package groups, cannot create idmap\n"); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5371 | return UNKNOWN_ERROR; |
| 5372 | } |
| 5373 | if (mPackageGroups[0]->packages.size() == 0) { |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5374 | ALOGW("idmap: target package has no packages in its first package group, " |
| 5375 | "cannot create idmap\n"); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5376 | return UNKNOWN_ERROR; |
| 5377 | } |
| 5378 | |
| 5379 | Vector<Vector<uint32_t> > map; |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5380 | // overlaid packages are assumed to contain only one package group |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5381 | const PackageGroup* pg = mPackageGroups[0]; |
| 5382 | const Package* pkg = pg->packages[0]; |
| 5383 | size_t typeCount = pkg->types.size(); |
| 5384 | // starting size is header + first item (number of types in map) |
| 5385 | *outSize = (IDMAP_HEADER_SIZE + 1) * sizeof(uint32_t); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5386 | // overlay packages are assumed to contain only one package group |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5387 | const String16 overlayPackage(overlay.mPackageGroups[0]->packages[0]->package->name); |
| 5388 | const uint32_t pkg_id = pkg->package->id << 24; |
| 5389 | |
| 5390 | for (size_t typeIndex = 0; typeIndex < typeCount; ++typeIndex) { |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5391 | ssize_t first = -1; |
| 5392 | ssize_t last = -1; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5393 | const Type* typeConfigs = pkg->getType(typeIndex); |
| 5394 | ssize_t mapIndex = map.add(); |
| 5395 | if (mapIndex < 0) { |
| 5396 | return NO_MEMORY; |
| 5397 | } |
| 5398 | Vector<uint32_t>& vector = map.editItemAt(mapIndex); |
| 5399 | for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) { |
Jean-Baptiste Queru | 39b58ba | 2012-05-01 09:53:48 -0700 | [diff] [blame] | 5400 | uint32_t resID = pkg_id |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5401 | | (0x00ff0000 & ((typeIndex+1)<<16)) |
| 5402 | | (0x0000ffff & (entryIndex)); |
| 5403 | resource_name resName; |
MÃ¥rten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 5404 | if (!this->getResourceName(resID, false, &resName)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5405 | ALOGW("idmap: resource 0x%08x has spec but lacks values, skipping\n", resID); |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5406 | // add dummy value, or trimming leading/trailing zeroes later will fail |
| 5407 | vector.push(0); |
MÃ¥rten Kongstad | fcaba14 | 2011-05-19 16:02:35 +0200 | [diff] [blame] | 5408 | continue; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | const String16 overlayType(resName.type, resName.typeLen); |
| 5412 | const String16 overlayName(resName.name, resName.nameLen); |
| 5413 | uint32_t overlayResID = overlay.identifierForName(overlayName.string(), |
| 5414 | overlayName.size(), |
| 5415 | overlayType.string(), |
| 5416 | overlayType.size(), |
| 5417 | overlayPackage.string(), |
| 5418 | overlayPackage.size()); |
| 5419 | if (overlayResID != 0) { |
Jean-Baptiste Queru | 39b58ba | 2012-05-01 09:53:48 -0700 | [diff] [blame] | 5420 | overlayResID = pkg_id | (0x00ffffff & overlayResID); |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5421 | last = Res_GETENTRY(resID); |
| 5422 | if (first == -1) { |
| 5423 | first = Res_GETENTRY(resID); |
| 5424 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5425 | } |
| 5426 | vector.push(overlayResID); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5427 | #if 0 |
| 5428 | if (overlayResID != 0) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5429 | ALOGD("%s/%s 0x%08x -> 0x%08x\n", |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5430 | String8(String16(resName.type)).string(), |
| 5431 | String8(String16(resName.name)).string(), |
| 5432 | resID, overlayResID); |
| 5433 | } |
| 5434 | #endif |
| 5435 | } |
| 5436 | |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5437 | if (first != -1) { |
| 5438 | // shave off trailing entries which lack overlay values |
| 5439 | const size_t last_past_one = last + 1; |
| 5440 | if (last_past_one < vector.size()) { |
| 5441 | vector.removeItemsAt(last_past_one, vector.size() - last_past_one); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5442 | } |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5443 | // shave off leading entries which lack overlay values |
| 5444 | vector.removeItemsAt(0, first); |
| 5445 | // store offset to first overlaid resource ID of this type |
| 5446 | vector.insertAt((uint32_t)first, 0, 1); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5447 | // reserve space for number and offset of entries, and the actual entries |
| 5448 | *outSize += (2 + vector.size()) * sizeof(uint32_t); |
| 5449 | } else { |
| 5450 | // no entries of current type defined in overlay package |
| 5451 | vector.clear(); |
| 5452 | // reserve space for type offset |
| 5453 | *outSize += 1 * sizeof(uint32_t); |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | if ((*outData = malloc(*outSize)) == NULL) { |
| 5458 | return NO_MEMORY; |
| 5459 | } |
| 5460 | uint32_t* data = (uint32_t*)*outData; |
| 5461 | *data++ = htodl(IDMAP_MAGIC); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5462 | *data++ = htodl(targetCrc); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5463 | *data++ = htodl(overlayCrc); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5464 | const char* paths[] = { targetPath, overlayPath }; |
| 5465 | for (int j = 0; j < 2; ++j) { |
| 5466 | char* p = (char*)data; |
| 5467 | const char* path = paths[j]; |
| 5468 | const size_t I = strlen(path); |
| 5469 | if (I > 255) { |
| 5470 | ALOGV("path exceeds expected 255 characters: %s\n", path); |
| 5471 | return UNKNOWN_ERROR; |
| 5472 | } |
| 5473 | for (size_t i = 0; i < 256; ++i) { |
| 5474 | *p++ = i < I ? path[i] : '\0'; |
| 5475 | } |
| 5476 | data += 256 / sizeof(uint32_t); |
| 5477 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5478 | const size_t mapSize = map.size(); |
| 5479 | *data++ = htodl(mapSize); |
| 5480 | size_t offset = mapSize; |
| 5481 | for (size_t i = 0; i < mapSize; ++i) { |
| 5482 | const Vector<uint32_t>& vector = map.itemAt(i); |
| 5483 | const size_t N = vector.size(); |
| 5484 | if (N == 0) { |
| 5485 | *data++ = htodl(0); |
| 5486 | } else { |
| 5487 | offset++; |
| 5488 | *data++ = htodl(offset); |
| 5489 | offset += N; |
| 5490 | } |
| 5491 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5492 | if (offset == mapSize) { |
| 5493 | ALOGW("idmap: no resources in overlay package present in base package\n"); |
| 5494 | return UNKNOWN_ERROR; |
| 5495 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5496 | for (size_t i = 0; i < mapSize; ++i) { |
| 5497 | const Vector<uint32_t>& vector = map.itemAt(i); |
| 5498 | const size_t N = vector.size(); |
| 5499 | if (N == 0) { |
| 5500 | continue; |
| 5501 | } |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 5502 | if (N == 1) { // vector expected to hold (offset) + (N > 0 entries) |
| 5503 | ALOGW("idmap: type %d supposedly has entries, but no entries found\n", i); |
| 5504 | return UNKNOWN_ERROR; |
| 5505 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5506 | *data++ = htodl(N - 1); // do not count the offset (which is vector's first element) |
| 5507 | for (size_t j = 0; j < N; ++j) { |
| 5508 | const uint32_t& overlayResID = vector.itemAt(j); |
| 5509 | *data++ = htodl(overlayResID); |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | return NO_ERROR; |
| 5514 | } |
| 5515 | |
| 5516 | bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes, |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5517 | uint32_t* pTargetCrc, uint32_t* pOverlayCrc, |
| 5518 | String8* pTargetPath, String8* pOverlayPath) |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5519 | { |
| 5520 | const uint32_t* map = (const uint32_t*)idmap; |
| 5521 | if (!assertIdmapHeader(map, sizeBytes)) { |
| 5522 | return false; |
| 5523 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 5524 | if (pTargetCrc) { |
| 5525 | *pTargetCrc = map[1]; |
| 5526 | } |
| 5527 | if (pOverlayCrc) { |
| 5528 | *pOverlayCrc = map[2]; |
| 5529 | } |
| 5530 | if (pTargetPath) { |
| 5531 | pTargetPath->setTo(reinterpret_cast<const char*>(map + 3)); |
| 5532 | } |
| 5533 | if (pOverlayPath) { |
| 5534 | pOverlayPath->setTo(reinterpret_cast<const char*>(map + 3 + 256 / sizeof(uint32_t))); |
| 5535 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 5536 | return true; |
| 5537 | } |
| 5538 | |
| 5539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5540 | #define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string()) |
| 5541 | |
| 5542 | #define CHAR16_ARRAY_EQ(constant, var, len) \ |
| 5543 | ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len)))) |
| 5544 | |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 5545 | static void print_complex(uint32_t complex, bool isFraction) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5546 | { |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5547 | const float MANTISSA_MULT = |
| 5548 | 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT); |
| 5549 | const float RADIX_MULTS[] = { |
| 5550 | 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT, |
| 5551 | 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT |
| 5552 | }; |
| 5553 | |
| 5554 | float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK |
| 5555 | <<Res_value::COMPLEX_MANTISSA_SHIFT)) |
| 5556 | * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT) |
| 5557 | & Res_value::COMPLEX_RADIX_MASK]; |
| 5558 | printf("%f", value); |
| 5559 | |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5560 | if (!isFraction) { |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5561 | switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { |
| 5562 | case Res_value::COMPLEX_UNIT_PX: printf("px"); break; |
| 5563 | case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break; |
| 5564 | case Res_value::COMPLEX_UNIT_SP: printf("sp"); break; |
| 5565 | case Res_value::COMPLEX_UNIT_PT: printf("pt"); break; |
| 5566 | case Res_value::COMPLEX_UNIT_IN: printf("in"); break; |
| 5567 | case Res_value::COMPLEX_UNIT_MM: printf("mm"); break; |
| 5568 | default: printf(" (unknown unit)"); break; |
| 5569 | } |
| 5570 | } else { |
| 5571 | switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { |
| 5572 | case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break; |
| 5573 | case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break; |
| 5574 | default: printf(" (unknown unit)"); break; |
| 5575 | } |
| 5576 | } |
| 5577 | } |
| 5578 | |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 5579 | // Normalize a string for output |
| 5580 | String8 ResTable::normalizeForOutput( const char *input ) |
| 5581 | { |
| 5582 | String8 ret; |
| 5583 | char buff[2]; |
| 5584 | buff[1] = '\0'; |
| 5585 | |
| 5586 | while (*input != '\0') { |
| 5587 | switch (*input) { |
| 5588 | // All interesting characters are in the ASCII zone, so we are making our own lives |
| 5589 | // easier by scanning the string one byte at a time. |
| 5590 | case '\\': |
| 5591 | ret += "\\\\"; |
| 5592 | break; |
| 5593 | case '\n': |
| 5594 | ret += "\\n"; |
| 5595 | break; |
| 5596 | case '"': |
| 5597 | ret += "\\\""; |
| 5598 | break; |
| 5599 | default: |
| 5600 | buff[0] = *input; |
| 5601 | ret += buff; |
| 5602 | break; |
| 5603 | } |
| 5604 | |
| 5605 | input++; |
| 5606 | } |
| 5607 | |
| 5608 | return ret; |
| 5609 | } |
| 5610 | |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5611 | void ResTable::print_value(const Package* pkg, const Res_value& value) const |
| 5612 | { |
| 5613 | if (value.dataType == Res_value::TYPE_NULL) { |
| 5614 | printf("(null)\n"); |
| 5615 | } else if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 5616 | printf("(reference) 0x%08x\n", value.data); |
| 5617 | } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 5618 | printf("(attribute) 0x%08x\n", value.data); |
| 5619 | } else if (value.dataType == Res_value::TYPE_STRING) { |
| 5620 | size_t len; |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 5621 | const char* str8 = pkg->header->values.string8At( |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5622 | value.data, &len); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 5623 | if (str8 != NULL) { |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 5624 | printf("(string8) \"%s\"\n", normalizeForOutput(str8).string()); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5625 | } else { |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 5626 | const char16_t* str16 = pkg->header->values.stringAt( |
| 5627 | value.data, &len); |
| 5628 | if (str16 != NULL) { |
| 5629 | printf("(string16) \"%s\"\n", |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 5630 | normalizeForOutput(String8(str16, len).string()).string()); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 5631 | } else { |
| 5632 | printf("(string) null\n"); |
| 5633 | } |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5634 | } |
| 5635 | } else if (value.dataType == Res_value::TYPE_FLOAT) { |
| 5636 | printf("(float) %g\n", *(const float*)&value.data); |
| 5637 | } else if (value.dataType == Res_value::TYPE_DIMENSION) { |
| 5638 | printf("(dimension) "); |
| 5639 | print_complex(value.data, false); |
| 5640 | printf("\n"); |
| 5641 | } else if (value.dataType == Res_value::TYPE_FRACTION) { |
| 5642 | printf("(fraction) "); |
| 5643 | print_complex(value.data, true); |
| 5644 | printf("\n"); |
| 5645 | } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT |
| 5646 | || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) { |
| 5647 | printf("(color) #%08x\n", value.data); |
| 5648 | } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) { |
| 5649 | printf("(boolean) %s\n", value.data ? "true" : "false"); |
| 5650 | } else if (value.dataType >= Res_value::TYPE_FIRST_INT |
| 5651 | || value.dataType <= Res_value::TYPE_LAST_INT) { |
| 5652 | printf("(int) 0x%08x or %d\n", value.data, value.data); |
| 5653 | } else { |
| 5654 | printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n", |
| 5655 | (int)value.dataType, (int)value.data, |
| 5656 | (int)value.size, (int)value.res0); |
| 5657 | } |
| 5658 | } |
| 5659 | |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5660 | void ResTable::print(bool inclValues) const |
| 5661 | { |
| 5662 | if (mError != 0) { |
| 5663 | printf("mError=0x%x (%s)\n", mError, strerror(mError)); |
| 5664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5665 | #if 0 |
| 5666 | printf("mParams=%c%c-%c%c,\n", |
| 5667 | mParams.language[0], mParams.language[1], |
| 5668 | mParams.country[0], mParams.country[1]); |
| 5669 | #endif |
| 5670 | size_t pgCount = mPackageGroups.size(); |
| 5671 | printf("Package Groups (%d)\n", (int)pgCount); |
| 5672 | for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) { |
| 5673 | const PackageGroup* pg = mPackageGroups[pgIndex]; |
| 5674 | printf("Package Group %d id=%d packageCount=%d name=%s\n", |
| 5675 | (int)pgIndex, pg->id, (int)pg->packages.size(), |
| 5676 | String8(pg->name).string()); |
| 5677 | |
| 5678 | size_t pkgCount = pg->packages.size(); |
| 5679 | for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) { |
| 5680 | const Package* pkg = pg->packages[pkgIndex]; |
| 5681 | size_t typeCount = pkg->types.size(); |
| 5682 | printf(" Package %d id=%d name=%s typeCount=%d\n", (int)pkgIndex, |
| 5683 | pkg->package->id, String8(String16(pkg->package->name)).string(), |
| 5684 | (int)typeCount); |
| 5685 | for (size_t typeIndex=0; typeIndex<typeCount; typeIndex++) { |
| 5686 | const Type* typeConfigs = pkg->getType(typeIndex); |
| 5687 | if (typeConfigs == NULL) { |
| 5688 | printf(" type %d NULL\n", (int)typeIndex); |
| 5689 | continue; |
| 5690 | } |
| 5691 | const size_t NTC = typeConfigs->configs.size(); |
| 5692 | printf(" type %d configCount=%d entryCount=%d\n", |
| 5693 | (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount); |
| 5694 | if (typeConfigs->typeSpecFlags != NULL) { |
| 5695 | for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) { |
| 5696 | uint32_t resID = (0xff000000 & ((pkg->package->id)<<24)) |
| 5697 | | (0x00ff0000 & ((typeIndex+1)<<16)) |
| 5698 | | (0x0000ffff & (entryIndex)); |
| 5699 | resource_name resName; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5700 | if (this->getResourceName(resID, true, &resName)) { |
| 5701 | String8 type8; |
| 5702 | String8 name8; |
| 5703 | if (resName.type8 != NULL) { |
| 5704 | type8 = String8(resName.type8, resName.typeLen); |
| 5705 | } else { |
| 5706 | type8 = String8(resName.type, resName.typeLen); |
| 5707 | } |
| 5708 | if (resName.name8 != NULL) { |
| 5709 | name8 = String8(resName.name8, resName.nameLen); |
| 5710 | } else { |
| 5711 | name8 = String8(resName.name, resName.nameLen); |
| 5712 | } |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 5713 | printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n", |
| 5714 | resID, |
| 5715 | CHAR16_TO_CSTR(resName.package, resName.packageLen), |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5716 | type8.string(), name8.string(), |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 5717 | dtohl(typeConfigs->typeSpecFlags[entryIndex])); |
| 5718 | } else { |
| 5719 | printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID); |
| 5720 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | } |
| 5722 | } |
| 5723 | for (size_t configIndex=0; configIndex<NTC; configIndex++) { |
| 5724 | const ResTable_type* type = typeConfigs->configs[configIndex]; |
| 5725 | if ((((uint64_t)type)&0x3) != 0) { |
| 5726 | printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type); |
| 5727 | continue; |
| 5728 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 5729 | String8 configStr = type->config.toString(); |
| 5730 | printf(" config %s:\n", configStr.size() > 0 |
| 5731 | ? configStr.string() : "(default)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5732 | size_t entryCount = dtohl(type->entryCount); |
| 5733 | uint32_t entriesStart = dtohl(type->entriesStart); |
| 5734 | if ((entriesStart&0x3) != 0) { |
| 5735 | printf(" NON-INTEGER ResTable_type entriesStart OFFSET: %p\n", (void*)entriesStart); |
| 5736 | continue; |
| 5737 | } |
| 5738 | uint32_t typeSize = dtohl(type->header.size); |
| 5739 | if ((typeSize&0x3) != 0) { |
| 5740 | printf(" NON-INTEGER ResTable_type header.size: %p\n", (void*)typeSize); |
| 5741 | continue; |
| 5742 | } |
| 5743 | for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) { |
| 5744 | |
| 5745 | const uint8_t* const end = ((const uint8_t*)type) |
| 5746 | + dtohl(type->header.size); |
| 5747 | const uint32_t* const eindex = (const uint32_t*) |
| 5748 | (((const uint8_t*)type) + dtohs(type->header.headerSize)); |
| 5749 | |
| 5750 | uint32_t thisOffset = dtohl(eindex[entryIndex]); |
| 5751 | if (thisOffset == ResTable_type::NO_ENTRY) { |
| 5752 | continue; |
| 5753 | } |
| 5754 | |
| 5755 | uint32_t resID = (0xff000000 & ((pkg->package->id)<<24)) |
| 5756 | | (0x00ff0000 & ((typeIndex+1)<<16)) |
| 5757 | | (0x0000ffff & (entryIndex)); |
| 5758 | resource_name resName; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5759 | if (this->getResourceName(resID, true, &resName)) { |
| 5760 | String8 type8; |
| 5761 | String8 name8; |
| 5762 | if (resName.type8 != NULL) { |
| 5763 | type8 = String8(resName.type8, resName.typeLen); |
| 5764 | } else { |
| 5765 | type8 = String8(resName.type, resName.typeLen); |
| 5766 | } |
| 5767 | if (resName.name8 != NULL) { |
| 5768 | name8 = String8(resName.name8, resName.nameLen); |
| 5769 | } else { |
| 5770 | name8 = String8(resName.name, resName.nameLen); |
| 5771 | } |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 5772 | printf(" resource 0x%08x %s:%s/%s: ", resID, |
| 5773 | CHAR16_TO_CSTR(resName.package, resName.packageLen), |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5774 | type8.string(), name8.string()); |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 5775 | } else { |
| 5776 | printf(" INVALID RESOURCE 0x%08x: ", resID); |
| 5777 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5778 | if ((thisOffset&0x3) != 0) { |
| 5779 | printf("NON-INTEGER OFFSET: %p\n", (void*)thisOffset); |
| 5780 | continue; |
| 5781 | } |
| 5782 | if ((thisOffset+sizeof(ResTable_entry)) > typeSize) { |
| 5783 | printf("OFFSET OUT OF BOUNDS: %p+%p (size is %p)\n", |
| 5784 | (void*)entriesStart, (void*)thisOffset, |
| 5785 | (void*)typeSize); |
| 5786 | continue; |
| 5787 | } |
| 5788 | |
| 5789 | const ResTable_entry* ent = (const ResTable_entry*) |
| 5790 | (((const uint8_t*)type) + entriesStart + thisOffset); |
| 5791 | if (((entriesStart + thisOffset)&0x3) != 0) { |
| 5792 | printf("NON-INTEGER ResTable_entry OFFSET: %p\n", |
| 5793 | (void*)(entriesStart + thisOffset)); |
| 5794 | continue; |
| 5795 | } |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5796 | |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5797 | uint16_t esize = dtohs(ent->size); |
| 5798 | if ((esize&0x3) != 0) { |
| 5799 | printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void*)esize); |
| 5800 | continue; |
| 5801 | } |
| 5802 | if ((thisOffset+esize) > typeSize) { |
| 5803 | printf("ResTable_entry OUT OF BOUNDS: %p+%p+%p (size is %p)\n", |
| 5804 | (void*)entriesStart, (void*)thisOffset, |
| 5805 | (void*)esize, (void*)typeSize); |
| 5806 | continue; |
| 5807 | } |
| 5808 | |
| 5809 | const Res_value* valuePtr = NULL; |
| 5810 | const ResTable_map_entry* bagPtr = NULL; |
| 5811 | Res_value value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5812 | if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) { |
| 5813 | printf("<bag>"); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5814 | bagPtr = (const ResTable_map_entry*)ent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5815 | } else { |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5816 | valuePtr = (const Res_value*) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5817 | (((const uint8_t*)ent) + esize); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5818 | value.copyFrom_dtoh(*valuePtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5819 | printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)", |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5820 | (int)value.dataType, (int)value.data, |
| 5821 | (int)value.size, (int)value.res0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) { |
| 5825 | printf(" (PUBLIC)"); |
| 5826 | } |
| 5827 | printf("\n"); |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5828 | |
| 5829 | if (inclValues) { |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5830 | if (valuePtr != NULL) { |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5831 | printf(" "); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5832 | print_value(pkg, value); |
| 5833 | } else if (bagPtr != NULL) { |
| 5834 | const int N = dtohl(bagPtr->count); |
Kenny Root | 06983bc | 2010-06-08 12:45:31 -0700 | [diff] [blame] | 5835 | const uint8_t* baseMapPtr = (const uint8_t*)ent; |
| 5836 | size_t mapOffset = esize; |
| 5837 | const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5838 | printf(" Parent=0x%08x, Count=%d\n", |
| 5839 | dtohl(bagPtr->parent.ident), N); |
Kenny Root | 06983bc | 2010-06-08 12:45:31 -0700 | [diff] [blame] | 5840 | for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) { |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 5841 | printf(" #%i (Key=0x%08x): ", |
| 5842 | i, dtohl(mapPtr->name.ident)); |
| 5843 | value.copyFrom_dtoh(mapPtr->value); |
| 5844 | print_value(pkg, value); |
| 5845 | const size_t size = dtohs(mapPtr->value.size); |
Kenny Root | 06983bc | 2010-06-08 12:45:31 -0700 | [diff] [blame] | 5846 | mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value); |
| 5847 | mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 5848 | } |
| 5849 | } |
| 5850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5851 | } |
| 5852 | } |
| 5853 | } |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5858 | } // namespace android |