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 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <memory.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 28 | #include <limits> |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 29 | #include <memory> |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 30 | #include <type_traits> |
| 31 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 32 | #include <androidfw/ByteBucketArray.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 33 | #include <androidfw/ResourceTypes.h> |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 34 | #include <androidfw/TypeWrappers.h> |
Steven Moreland | fb7952f | 2018-02-23 14:58:50 -0800 | [diff] [blame] | 35 | #include <cutils/atomic.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <utils/ByteOrder.h> |
| 37 | #include <utils/Debug.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 38 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <utils/String16.h> |
| 40 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 42 | #ifdef __ANDROID__ |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 43 | #include <binder/TextOutput.h> |
| 44 | #endif |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | #ifndef INT32_MAX |
| 47 | #define INT32_MAX ((int32_t)(2147483647)) |
| 48 | #endif |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | namespace android { |
| 51 | |
Elliott Hughes | 59cbe8d | 2015-07-29 17:49:27 -0700 | [diff] [blame] | 52 | #if defined(_WIN32) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | #undef nhtol |
| 54 | #undef htonl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | #define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) ) |
| 56 | #define htonl(x) ntohl(x) |
| 57 | #define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) ) |
| 58 | #define htons(x) ntohs(x) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 61 | #define IDMAP_MAGIC 0x504D4449 |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 62 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 63 | #define APP_PACKAGE_ID 0x7f |
| 64 | #define SYS_PACKAGE_ID 0x01 |
| 65 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 66 | static const bool kDebugStringPoolNoisy = false; |
| 67 | static const bool kDebugXMLNoisy = false; |
| 68 | static const bool kDebugTableNoisy = false; |
| 69 | static const bool kDebugTableGetEntry = false; |
| 70 | static const bool kDebugTableSuperNoisy = false; |
| 71 | static const bool kDebugLoadTableNoisy = false; |
| 72 | static const bool kDebugLoadTableSuperNoisy = false; |
| 73 | static const bool kDebugTableTheme = false; |
| 74 | static const bool kDebugResXMLTree = false; |
| 75 | static const bool kDebugLibNoisy = false; |
| 76 | |
| 77 | // TODO: This code uses 0xFFFFFFFF converted to bag_set* as a sentinel value. This is bad practice. |
| 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | // Standard C isspace() is only required to look at the low byte of its input, so |
| 80 | // produces incorrect results for UTF-16 characters. For safety's sake, assume that |
| 81 | // any high-byte UTF-16 code point is not whitespace. |
| 82 | inline int isspace16(char16_t c) { |
| 83 | return (c < 0x0080 && isspace(c)); |
| 84 | } |
| 85 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 86 | template<typename T> |
| 87 | inline static T max(T a, T b) { |
| 88 | return a > b ? a : b; |
| 89 | } |
| 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | // range checked; guaranteed to NUL-terminate within the stated number of available slots |
| 92 | // NOTE: if this truncates the dst string due to running out of space, no attempt is |
| 93 | // made to avoid splitting surrogate pairs. |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 94 | static void strcpy16_dtoh(char16_t* dst, const uint16_t* src, size_t avail) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | { |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 96 | char16_t* last = dst + avail - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | while (*src && (dst < last)) { |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 98 | char16_t s = dtohs(static_cast<char16_t>(*src)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | *dst++ = s; |
| 100 | src++; |
| 101 | } |
| 102 | *dst = 0; |
| 103 | } |
| 104 | |
| 105 | static status_t validate_chunk(const ResChunk_header* chunk, |
| 106 | size_t minSize, |
| 107 | const uint8_t* dataEnd, |
| 108 | const char* name) |
| 109 | { |
| 110 | const uint16_t headerSize = dtohs(chunk->headerSize); |
| 111 | const uint32_t size = dtohl(chunk->size); |
| 112 | |
| 113 | if (headerSize >= minSize) { |
| 114 | if (headerSize <= size) { |
| 115 | if (((headerSize|size)&0x3) == 0) { |
Adam Lesinski | 7322ea7 | 2014-05-14 11:43:26 -0700 | [diff] [blame] | 116 | if ((size_t)size <= (size_t)(dataEnd-((const uint8_t*)chunk))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | return NO_ERROR; |
| 118 | } |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 119 | ALOGW("%s data size 0x%x extends beyond resource end %p.", |
| 120 | name, size, (void*)(dataEnd-((const uint8_t*)chunk))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | return BAD_TYPE; |
| 122 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 123 | 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] | 124 | name, (int)size, (int)headerSize); |
| 125 | return BAD_TYPE; |
| 126 | } |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 127 | ALOGW("%s size 0x%x is smaller than header size 0x%x.", |
| 128 | name, size, headerSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | return BAD_TYPE; |
| 130 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 131 | ALOGW("%s header size 0x%04x is too small.", |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 132 | name, headerSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | return BAD_TYPE; |
| 134 | } |
| 135 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 136 | static void fill9patchOffsets(Res_png_9patch* patch) { |
| 137 | patch->xDivsOffset = sizeof(Res_png_9patch); |
| 138 | patch->yDivsOffset = patch->xDivsOffset + (patch->numXDivs * sizeof(int32_t)); |
| 139 | patch->colorsOffset = patch->yDivsOffset + (patch->numYDivs * sizeof(int32_t)); |
| 140 | } |
| 141 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 142 | void Res_value::copyFrom_dtoh(const Res_value& src) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | { |
| 144 | size = dtohs(src.size); |
| 145 | res0 = src.res0; |
| 146 | dataType = src.dataType; |
| 147 | data = dtohl(src.data); |
| 148 | } |
| 149 | |
| 150 | void Res_png_9patch::deviceToFile() |
| 151 | { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 152 | int32_t* xDivs = getXDivs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | for (int i = 0; i < numXDivs; i++) { |
| 154 | xDivs[i] = htonl(xDivs[i]); |
| 155 | } |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 156 | int32_t* yDivs = getYDivs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | for (int i = 0; i < numYDivs; i++) { |
| 158 | yDivs[i] = htonl(yDivs[i]); |
| 159 | } |
| 160 | paddingLeft = htonl(paddingLeft); |
| 161 | paddingRight = htonl(paddingRight); |
| 162 | paddingTop = htonl(paddingTop); |
| 163 | paddingBottom = htonl(paddingBottom); |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 164 | uint32_t* colors = getColors(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | for (int i=0; i<numColors; i++) { |
| 166 | colors[i] = htonl(colors[i]); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void Res_png_9patch::fileToDevice() |
| 171 | { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 172 | int32_t* xDivs = getXDivs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | for (int i = 0; i < numXDivs; i++) { |
| 174 | xDivs[i] = ntohl(xDivs[i]); |
| 175 | } |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 176 | int32_t* yDivs = getYDivs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | for (int i = 0; i < numYDivs; i++) { |
| 178 | yDivs[i] = ntohl(yDivs[i]); |
| 179 | } |
| 180 | paddingLeft = ntohl(paddingLeft); |
| 181 | paddingRight = ntohl(paddingRight); |
| 182 | paddingTop = ntohl(paddingTop); |
| 183 | paddingBottom = ntohl(paddingBottom); |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 184 | uint32_t* colors = getColors(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | for (int i=0; i<numColors; i++) { |
| 186 | colors[i] = ntohl(colors[i]); |
| 187 | } |
| 188 | } |
| 189 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 190 | size_t Res_png_9patch::serializedSize() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | { |
| 192 | // The size of this struct is 32 bytes on the 32-bit target system |
| 193 | // 4 * int8_t |
| 194 | // 4 * int32_t |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 195 | // 3 * uint32_t |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | return 32 |
| 197 | + numXDivs * sizeof(int32_t) |
| 198 | + numYDivs * sizeof(int32_t) |
| 199 | + numColors * sizeof(uint32_t); |
| 200 | } |
| 201 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 202 | void* Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs, |
| 203 | const int32_t* yDivs, const uint32_t* colors) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 205 | // Use calloc since we're going to leave a few holes in the data |
| 206 | // and want this to run cleanly under valgrind |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 207 | void* newData = calloc(1, patch.serializedSize()); |
| 208 | serialize(patch, xDivs, yDivs, colors, newData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | return newData; |
| 210 | } |
| 211 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 212 | void Res_png_9patch::serialize(const Res_png_9patch& patch, const int32_t* xDivs, |
| 213 | const int32_t* yDivs, const uint32_t* colors, void* outData) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 215 | uint8_t* data = (uint8_t*) outData; |
| 216 | memcpy(data, &patch.wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors |
| 217 | memcpy(data + 12, &patch.paddingLeft, 16); // copy paddingXXXX |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | data += 32; |
| 219 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 220 | memcpy(data, xDivs, patch.numXDivs * sizeof(int32_t)); |
| 221 | data += patch.numXDivs * sizeof(int32_t); |
| 222 | memcpy(data, yDivs, patch.numYDivs * sizeof(int32_t)); |
| 223 | data += patch.numYDivs * sizeof(int32_t); |
| 224 | memcpy(data, colors, patch.numColors * sizeof(uint32_t)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 226 | fill9patchOffsets(reinterpret_cast<Res_png_9patch*>(outData)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 229 | static bool assertIdmapHeader(const void* idmap, size_t size) { |
| 230 | if (reinterpret_cast<uintptr_t>(idmap) & 0x03) { |
| 231 | ALOGE("idmap: header is not word aligned"); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 232 | return false; |
| 233 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 234 | |
| 235 | if (size < ResTable::IDMAP_HEADER_SIZE_BYTES) { |
| 236 | ALOGW("idmap: header too small (%d bytes)", (uint32_t) size); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | const uint32_t magic = htodl(*reinterpret_cast<const uint32_t*>(idmap)); |
| 241 | if (magic != IDMAP_MAGIC) { |
| 242 | ALOGW("idmap: no magic found in header (is 0x%08x, expected 0x%08x)", |
| 243 | magic, IDMAP_MAGIC); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | const uint32_t version = htodl(*(reinterpret_cast<const uint32_t*>(idmap) + 1)); |
MÃ¥rten Kongstad | 42ebcb8 | 2017-03-28 15:30:21 +0200 | [diff] [blame] | 248 | if (version != ResTable::IDMAP_CURRENT_VERSION) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 249 | // We are strict about versions because files with this format are |
| 250 | // auto-generated and don't need backwards compatibility. |
| 251 | ALOGW("idmap: version mismatch in header (is 0x%08x, expected 0x%08x)", |
MÃ¥rten Kongstad | 42ebcb8 | 2017-03-28 15:30:21 +0200 | [diff] [blame] | 252 | version, ResTable::IDMAP_CURRENT_VERSION); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 258 | class IdmapEntries { |
| 259 | public: |
| 260 | IdmapEntries() : mData(NULL) {} |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 261 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 262 | bool hasEntries() const { |
| 263 | if (mData == NULL) { |
| 264 | return false; |
| 265 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 266 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 267 | return (dtohs(*mData) > 0); |
| 268 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 269 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 270 | size_t byteSize() const { |
| 271 | if (mData == NULL) { |
| 272 | return 0; |
| 273 | } |
| 274 | uint16_t entryCount = dtohs(mData[2]); |
| 275 | return (sizeof(uint16_t) * 4) + (sizeof(uint32_t) * static_cast<size_t>(entryCount)); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 276 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 277 | |
| 278 | uint8_t targetTypeId() const { |
| 279 | if (mData == NULL) { |
| 280 | return 0; |
| 281 | } |
| 282 | return dtohs(mData[0]); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 283 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 284 | |
| 285 | uint8_t overlayTypeId() const { |
| 286 | if (mData == NULL) { |
| 287 | return 0; |
| 288 | } |
| 289 | return dtohs(mData[1]); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 290 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 291 | |
| 292 | status_t setTo(const void* entryHeader, size_t size) { |
| 293 | if (reinterpret_cast<uintptr_t>(entryHeader) & 0x03) { |
| 294 | ALOGE("idmap: entry header is not word aligned"); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 295 | return UNKNOWN_ERROR; |
| 296 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 297 | |
| 298 | if (size < sizeof(uint16_t) * 4) { |
| 299 | ALOGE("idmap: entry header is too small (%u bytes)", (uint32_t) size); |
| 300 | return UNKNOWN_ERROR; |
| 301 | } |
| 302 | |
| 303 | const uint16_t* header = reinterpret_cast<const uint16_t*>(entryHeader); |
| 304 | const uint16_t targetTypeId = dtohs(header[0]); |
| 305 | const uint16_t overlayTypeId = dtohs(header[1]); |
| 306 | if (targetTypeId == 0 || overlayTypeId == 0 || targetTypeId > 255 || overlayTypeId > 255) { |
| 307 | ALOGE("idmap: invalid type map (%u -> %u)", targetTypeId, overlayTypeId); |
| 308 | return UNKNOWN_ERROR; |
| 309 | } |
| 310 | |
| 311 | uint16_t entryCount = dtohs(header[2]); |
| 312 | if (size < sizeof(uint32_t) * (entryCount + 2)) { |
| 313 | ALOGE("idmap: too small (%u bytes) for the number of entries (%u)", |
| 314 | (uint32_t) size, (uint32_t) entryCount); |
| 315 | return UNKNOWN_ERROR; |
| 316 | } |
| 317 | mData = header; |
| 318 | return NO_ERROR; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 319 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 320 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 321 | status_t lookup(uint16_t entryId, uint16_t* outEntryId) const { |
| 322 | uint16_t entryCount = dtohs(mData[2]); |
| 323 | uint16_t offset = dtohs(mData[3]); |
| 324 | |
| 325 | if (entryId < offset) { |
| 326 | // The entry is not present in this idmap |
| 327 | return BAD_INDEX; |
| 328 | } |
| 329 | |
| 330 | entryId -= offset; |
| 331 | |
| 332 | if (entryId >= entryCount) { |
| 333 | // The entry is not present in this idmap |
| 334 | return BAD_INDEX; |
| 335 | } |
| 336 | |
| 337 | // It is safe to access the type here without checking the size because |
| 338 | // we have checked this when it was first loaded. |
| 339 | const uint32_t* entries = reinterpret_cast<const uint32_t*>(mData) + 2; |
| 340 | uint32_t mappedEntry = dtohl(entries[entryId]); |
| 341 | if (mappedEntry == 0xffffffff) { |
| 342 | // This entry is not present in this idmap |
| 343 | return BAD_INDEX; |
| 344 | } |
| 345 | *outEntryId = static_cast<uint16_t>(mappedEntry); |
| 346 | return NO_ERROR; |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | const uint16_t* mData; |
| 351 | }; |
| 352 | |
| 353 | status_t parseIdmap(const void* idmap, size_t size, uint8_t* outPackageId, KeyedVector<uint8_t, IdmapEntries>* outMap) { |
| 354 | if (!assertIdmapHeader(idmap, size)) { |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 355 | return UNKNOWN_ERROR; |
| 356 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 357 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 358 | size -= ResTable::IDMAP_HEADER_SIZE_BYTES; |
| 359 | if (size < sizeof(uint16_t) * 2) { |
| 360 | ALOGE("idmap: too small to contain any mapping"); |
| 361 | return UNKNOWN_ERROR; |
| 362 | } |
| 363 | |
| 364 | const uint16_t* data = reinterpret_cast<const uint16_t*>( |
| 365 | reinterpret_cast<const uint8_t*>(idmap) + ResTable::IDMAP_HEADER_SIZE_BYTES); |
| 366 | |
| 367 | uint16_t targetPackageId = dtohs(*(data++)); |
| 368 | if (targetPackageId == 0 || targetPackageId > 255) { |
| 369 | ALOGE("idmap: target package ID is invalid (%02x)", targetPackageId); |
| 370 | return UNKNOWN_ERROR; |
| 371 | } |
| 372 | |
| 373 | uint16_t mapCount = dtohs(*(data++)); |
| 374 | if (mapCount == 0) { |
| 375 | ALOGE("idmap: no mappings"); |
| 376 | return UNKNOWN_ERROR; |
| 377 | } |
| 378 | |
| 379 | if (mapCount > 255) { |
| 380 | ALOGW("idmap: too many mappings. Only 255 are possible but %u are present", (uint32_t) mapCount); |
| 381 | } |
| 382 | |
| 383 | while (size > sizeof(uint16_t) * 4) { |
| 384 | IdmapEntries entries; |
| 385 | status_t err = entries.setTo(data, size); |
| 386 | if (err != NO_ERROR) { |
| 387 | return err; |
| 388 | } |
| 389 | |
| 390 | ssize_t index = outMap->add(entries.overlayTypeId(), entries); |
| 391 | if (index < 0) { |
| 392 | return NO_MEMORY; |
| 393 | } |
| 394 | |
| 395 | data += entries.byteSize() / sizeof(uint16_t); |
| 396 | size -= entries.byteSize(); |
| 397 | } |
| 398 | |
| 399 | if (outPackageId != NULL) { |
| 400 | *outPackageId = static_cast<uint8_t>(targetPackageId); |
| 401 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 402 | return NO_ERROR; |
| 403 | } |
| 404 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 405 | Res_png_9patch* Res_png_9patch::deserialize(void* inData) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 407 | |
| 408 | Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(inData); |
| 409 | patch->wasDeserialized = true; |
| 410 | fill9patchOffsets(patch); |
| 411 | |
| 412 | return patch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // -------------------------------------------------------------------- |
| 416 | // -------------------------------------------------------------------- |
| 417 | // -------------------------------------------------------------------- |
| 418 | |
| 419 | ResStringPool::ResStringPool() |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 420 | : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | { |
| 422 | } |
| 423 | |
| 424 | ResStringPool::ResStringPool(const void* data, size_t size, bool copyData) |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 425 | : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | { |
| 427 | setTo(data, size, copyData); |
| 428 | } |
| 429 | |
| 430 | ResStringPool::~ResStringPool() |
| 431 | { |
| 432 | uninit(); |
| 433 | } |
| 434 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 435 | void ResStringPool::setToEmpty() |
| 436 | { |
| 437 | uninit(); |
| 438 | |
| 439 | mOwnedData = calloc(1, sizeof(ResStringPool_header)); |
| 440 | ResStringPool_header* header = (ResStringPool_header*) mOwnedData; |
| 441 | mSize = 0; |
| 442 | mEntries = NULL; |
| 443 | mStrings = NULL; |
| 444 | mStringPoolSize = 0; |
| 445 | mEntryStyles = NULL; |
| 446 | mStyles = NULL; |
| 447 | mStylePoolSize = 0; |
| 448 | mHeader = (const ResStringPool_header*) header; |
| 449 | } |
| 450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | status_t ResStringPool::setTo(const void* data, size_t size, bool copyData) |
| 452 | { |
| 453 | if (!data || !size) { |
| 454 | return (mError=BAD_TYPE); |
| 455 | } |
| 456 | |
| 457 | uninit(); |
| 458 | |
y | e46df9d | 2018-04-05 17:57:27 -0700 | [diff] [blame] | 459 | // The chunk must be at least the size of the string pool header. |
| 460 | if (size < sizeof(ResStringPool_header)) { |
| 461 | LOG_ALWAYS_FATAL("Bad string block: data size %zu is too small to be a string block", size); |
| 462 | return (mError=BAD_TYPE); |
| 463 | } |
| 464 | |
| 465 | // The data is at least as big as a ResChunk_header, so we can safely validate the other |
| 466 | // header fields. |
| 467 | // `data + size` is safe because the source of `size` comes from the kernel/filesystem. |
| 468 | if (validate_chunk(reinterpret_cast<const ResChunk_header*>(data), sizeof(ResStringPool_header), |
| 469 | reinterpret_cast<const uint8_t*>(data) + size, |
| 470 | "ResStringPool_header") != NO_ERROR) { |
| 471 | LOG_ALWAYS_FATAL("Bad string block: malformed block dimensions"); |
| 472 | return (mError=BAD_TYPE); |
| 473 | } |
| 474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | const bool notDeviceEndian = htods(0xf0) != 0xf0; |
| 476 | |
| 477 | if (copyData || notDeviceEndian) { |
| 478 | mOwnedData = malloc(size); |
| 479 | if (mOwnedData == NULL) { |
| 480 | return (mError=NO_MEMORY); |
| 481 | } |
| 482 | memcpy(mOwnedData, data, size); |
| 483 | data = mOwnedData; |
| 484 | } |
| 485 | |
y | e46df9d | 2018-04-05 17:57:27 -0700 | [diff] [blame] | 486 | // The size has been checked, so it is safe to read the data in the ResStringPool_header |
| 487 | // data structure. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | mHeader = (const ResStringPool_header*)data; |
| 489 | |
| 490 | if (notDeviceEndian) { |
| 491 | ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader); |
| 492 | h->header.headerSize = dtohs(mHeader->header.headerSize); |
| 493 | h->header.type = dtohs(mHeader->header.type); |
| 494 | h->header.size = dtohl(mHeader->header.size); |
| 495 | h->stringCount = dtohl(mHeader->stringCount); |
| 496 | h->styleCount = dtohl(mHeader->styleCount); |
| 497 | h->flags = dtohl(mHeader->flags); |
| 498 | h->stringsStart = dtohl(mHeader->stringsStart); |
| 499 | h->stylesStart = dtohl(mHeader->stylesStart); |
| 500 | } |
| 501 | |
| 502 | if (mHeader->header.headerSize > mHeader->header.size |
| 503 | || mHeader->header.size > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 504 | 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] | 505 | (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size); |
| 506 | return (mError=BAD_TYPE); |
| 507 | } |
| 508 | mSize = mHeader->header.size; |
| 509 | mEntries = (const uint32_t*) |
| 510 | (((const uint8_t*)data)+mHeader->header.headerSize); |
| 511 | |
| 512 | if (mHeader->stringCount > 0) { |
| 513 | if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow? |
| 514 | || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))) |
| 515 | > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 516 | 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] | 517 | (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))), |
| 518 | (int)size); |
| 519 | return (mError=BAD_TYPE); |
| 520 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 521 | |
| 522 | size_t charSize; |
| 523 | if (mHeader->flags&ResStringPool_header::UTF8_FLAG) { |
| 524 | charSize = sizeof(uint8_t); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 525 | } else { |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 526 | charSize = sizeof(uint16_t); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Adam Lesinski | f28d505 | 2014-07-25 15:25:04 -0700 | [diff] [blame] | 529 | // There should be at least space for the smallest string |
| 530 | // (2 bytes length, null terminator). |
| 531 | if (mHeader->stringsStart >= (mSize - sizeof(uint16_t))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 532 | 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] | 533 | (int)mHeader->stringsStart, (int)mHeader->header.size); |
| 534 | return (mError=BAD_TYPE); |
| 535 | } |
Adam Lesinski | f28d505 | 2014-07-25 15:25:04 -0700 | [diff] [blame] | 536 | |
| 537 | mStrings = (const void*) |
| 538 | (((const uint8_t*)data) + mHeader->stringsStart); |
| 539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | if (mHeader->styleCount == 0) { |
Adam Lesinski | f28d505 | 2014-07-25 15:25:04 -0700 | [diff] [blame] | 541 | mStringPoolSize = (mSize - mHeader->stringsStart) / charSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } else { |
Kenny Root | 5e4d9a0 | 2010-06-08 12:34:43 -0700 | [diff] [blame] | 543 | // check invariant: styles starts before end of data |
Adam Lesinski | f28d505 | 2014-07-25 15:25:04 -0700 | [diff] [blame] | 544 | if (mHeader->stylesStart >= (mSize - sizeof(uint16_t))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 545 | 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] | 546 | (int)mHeader->stylesStart, (int)mHeader->header.size); |
| 547 | return (mError=BAD_TYPE); |
| 548 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | // check invariant: styles follow the strings |
| 550 | if (mHeader->stylesStart <= mHeader->stringsStart) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 551 | 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] | 552 | (int)mHeader->stylesStart, (int)mHeader->stringsStart); |
| 553 | return (mError=BAD_TYPE); |
| 554 | } |
| 555 | mStringPoolSize = |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 556 | (mHeader->stylesStart-mHeader->stringsStart)/charSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // check invariant: stringCount > 0 requires a string pool to exist |
| 560 | if (mStringPoolSize == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 561 | 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] | 562 | return (mError=BAD_TYPE); |
| 563 | } |
| 564 | |
| 565 | if (notDeviceEndian) { |
| 566 | size_t i; |
| 567 | uint32_t* e = const_cast<uint32_t*>(mEntries); |
| 568 | for (i=0; i<mHeader->stringCount; i++) { |
| 569 | e[i] = dtohl(mEntries[i]); |
| 570 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 571 | if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) { |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 572 | const uint16_t* strings = (const uint16_t*)mStrings; |
| 573 | uint16_t* s = const_cast<uint16_t*>(strings); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 574 | for (i=0; i<mStringPoolSize; i++) { |
| 575 | s[i] = dtohs(strings[i]); |
| 576 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 580 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG && |
| 581 | ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) || |
Adam Lesinski | 666b6fb | 2016-04-21 10:05:06 -0700 | [diff] [blame] | 582 | (!(mHeader->flags&ResStringPool_header::UTF8_FLAG) && |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 583 | ((uint16_t*)mStrings)[mStringPoolSize-1] != 0)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 584 | 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] | 585 | return (mError=BAD_TYPE); |
| 586 | } |
| 587 | } else { |
| 588 | mStrings = NULL; |
| 589 | mStringPoolSize = 0; |
| 590 | } |
| 591 | |
| 592 | if (mHeader->styleCount > 0) { |
| 593 | mEntryStyles = mEntries + mHeader->stringCount; |
| 594 | // invariant: integer overflow in calculating mEntryStyles |
| 595 | if (mEntryStyles < mEntries) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 596 | ALOGW("Bad string block: integer overflow finding styles\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | return (mError=BAD_TYPE); |
| 598 | } |
| 599 | |
| 600 | if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 601 | 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] | 602 | (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader), |
| 603 | (int)size); |
| 604 | return (mError=BAD_TYPE); |
| 605 | } |
| 606 | mStyles = (const uint32_t*) |
| 607 | (((const uint8_t*)data)+mHeader->stylesStart); |
| 608 | if (mHeader->stylesStart >= mHeader->header.size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 609 | 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] | 610 | (int)mHeader->stylesStart, (int)mHeader->header.size); |
| 611 | return (mError=BAD_TYPE); |
| 612 | } |
| 613 | mStylePoolSize = |
| 614 | (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t); |
| 615 | |
| 616 | if (notDeviceEndian) { |
| 617 | size_t i; |
| 618 | uint32_t* e = const_cast<uint32_t*>(mEntryStyles); |
| 619 | for (i=0; i<mHeader->styleCount; i++) { |
| 620 | e[i] = dtohl(mEntryStyles[i]); |
| 621 | } |
| 622 | uint32_t* s = const_cast<uint32_t*>(mStyles); |
| 623 | for (i=0; i<mStylePoolSize; i++) { |
| 624 | s[i] = dtohl(mStyles[i]); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | const ResStringPool_span endSpan = { |
| 629 | { htodl(ResStringPool_span::END) }, |
| 630 | htodl(ResStringPool_span::END), htodl(ResStringPool_span::END) |
| 631 | }; |
| 632 | if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))], |
| 633 | &endSpan, sizeof(endSpan)) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 634 | 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] | 635 | return (mError=BAD_TYPE); |
| 636 | } |
| 637 | } else { |
| 638 | mEntryStyles = NULL; |
| 639 | mStyles = NULL; |
| 640 | mStylePoolSize = 0; |
| 641 | } |
| 642 | |
| 643 | return (mError=NO_ERROR); |
| 644 | } |
| 645 | |
| 646 | status_t ResStringPool::getError() const |
| 647 | { |
| 648 | return mError; |
| 649 | } |
| 650 | |
| 651 | void ResStringPool::uninit() |
| 652 | { |
| 653 | mError = NO_INIT; |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 654 | if (mHeader != NULL && mCache != NULL) { |
| 655 | for (size_t x = 0; x < mHeader->stringCount; x++) { |
| 656 | if (mCache[x] != NULL) { |
| 657 | free(mCache[x]); |
| 658 | mCache[x] = NULL; |
| 659 | } |
| 660 | } |
| 661 | free(mCache); |
| 662 | mCache = NULL; |
| 663 | } |
Chris Dearman | a1d82ff3 | 2012-10-08 12:22:02 -0700 | [diff] [blame] | 664 | if (mOwnedData) { |
| 665 | free(mOwnedData); |
| 666 | mOwnedData = NULL; |
| 667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 670 | /** |
| 671 | * Strings in UTF-16 format have length indicated by a length encoded in the |
| 672 | * stored data. It is either 1 or 2 characters of length data. This allows a |
| 673 | * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that |
| 674 | * much data in a string, you're abusing them. |
| 675 | * |
| 676 | * If the high bit is set, then there are two characters or 4 bytes of length |
| 677 | * data encoded. In that case, drop the high bit of the first character and |
| 678 | * add it together with the next character. |
| 679 | */ |
| 680 | static inline size_t |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 681 | decodeLength(const uint16_t** str) |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 682 | { |
| 683 | size_t len = **str; |
| 684 | if ((len & 0x8000) != 0) { |
| 685 | (*str)++; |
| 686 | len = ((len & 0x7FFF) << 16) | **str; |
| 687 | } |
| 688 | (*str)++; |
| 689 | return len; |
| 690 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 691 | |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 692 | /** |
| 693 | * Strings in UTF-8 format have length indicated by a length encoded in the |
| 694 | * stored data. It is either 1 or 2 characters of length data. This allows a |
| 695 | * maximum length of 0x7FFF (32767 bytes), but you should consider storing |
| 696 | * text in another way if you're using that much data in a single string. |
| 697 | * |
| 698 | * If the high bit is set, then there are two characters or 2 bytes of length |
| 699 | * data encoded. In that case, drop the high bit of the first character and |
| 700 | * add it together with the next character. |
| 701 | */ |
| 702 | static inline size_t |
| 703 | decodeLength(const uint8_t** str) |
| 704 | { |
| 705 | size_t len = **str; |
| 706 | if ((len & 0x80) != 0) { |
| 707 | (*str)++; |
| 708 | len = ((len & 0x7F) << 8) | **str; |
| 709 | } |
| 710 | (*str)++; |
| 711 | return len; |
| 712 | } |
| 713 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 714 | const char16_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] | 715 | { |
| 716 | if (mError == NO_ERROR && idx < mHeader->stringCount) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 717 | const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0; |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 718 | const uint32_t off = mEntries[idx]/(isUTF8?sizeof(uint8_t):sizeof(uint16_t)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | if (off < (mStringPoolSize-1)) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 720 | if (!isUTF8) { |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 721 | const uint16_t* strings = (uint16_t*)mStrings; |
| 722 | const uint16_t* str = strings+off; |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 723 | |
| 724 | *u16len = decodeLength(&str); |
| 725 | if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) { |
Vishwath Mohan | 6521a1b | 2015-03-11 16:08:37 -0700 | [diff] [blame] | 726 | // Reject malformed (non null-terminated) strings |
| 727 | if (str[*u16len] != 0x0000) { |
| 728 | ALOGW("Bad string block: string #%d is not null-terminated", |
| 729 | (int)idx); |
| 730 | return NULL; |
| 731 | } |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 732 | return reinterpret_cast<const char16_t*>(str); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 733 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 734 | 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] | 735 | (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } else { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 738 | const uint8_t* strings = (uint8_t*)mStrings; |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 739 | const uint8_t* u8str = strings+off; |
| 740 | |
| 741 | *u16len = decodeLength(&u8str); |
| 742 | size_t u8len = decodeLength(&u8str); |
| 743 | |
| 744 | // encLen must be less than 0x7FFF due to encoding. |
| 745 | if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 746 | AutoMutex lock(mDecodeLock); |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 747 | |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 748 | if (mCache != NULL && mCache[idx] != NULL) { |
| 749 | return mCache[idx]; |
| 750 | } |
| 751 | |
| 752 | // Retrieve the actual length of the utf8 string if the |
| 753 | // encoded length was truncated |
| 754 | if (stringDecodeAt(idx, u8str, u8len, &u8len) == NULL) { |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | // Since AAPT truncated lengths longer than 0x7FFF, check |
| 759 | // that the bits that remain after truncation at least match |
| 760 | // the bits of the actual length |
| 761 | ssize_t actualLen = utf8_to_utf16_length(u8str, u8len); |
| 762 | if (actualLen < 0 || ((size_t)actualLen & 0x7FFF) != *u16len) { |
| 763 | ALOGW("Bad string block: string #%lld decoded length is not correct " |
| 764 | "%lld vs %llu\n", |
| 765 | (long long)idx, (long long)actualLen, (long long)*u16len); |
| 766 | return NULL; |
| 767 | } |
| 768 | |
| 769 | *u16len = (size_t) actualLen; |
| 770 | char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t)); |
| 771 | if (!u16str) { |
| 772 | ALOGW("No memory when trying to allocate decode cache for string #%d\n", |
| 773 | (int)idx); |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | utf8_to_utf16(u8str, u8len, u16str, *u16len + 1); |
| 778 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 779 | if (mCache == NULL) { |
Elliott Hughes | ba3fe56 | 2015-08-12 14:49:53 -0700 | [diff] [blame] | 780 | #ifndef __ANDROID__ |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 781 | if (kDebugStringPoolNoisy) { |
| 782 | ALOGI("CREATING STRING CACHE OF %zu bytes", |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 783 | mHeader->stringCount*sizeof(char16_t**)); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 784 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 785 | #else |
| 786 | // We do not want to be in this case when actually running Android. |
Andreas Gampe | 25df5fb | 2014-11-07 22:24:57 -0800 | [diff] [blame] | 787 | ALOGW("CREATING STRING CACHE OF %zu bytes", |
| 788 | static_cast<size_t>(mHeader->stringCount*sizeof(char16_t**))); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 789 | #endif |
George Burgess IV | e8efec5 | 2016-10-11 15:42:29 -0700 | [diff] [blame] | 790 | mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t*)); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 791 | if (mCache == NULL) { |
| 792 | ALOGW("No memory trying to allocate decode cache table of %d bytes\n", |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 793 | (int)(mHeader->stringCount*sizeof(char16_t**))); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 794 | return NULL; |
| 795 | } |
| 796 | } |
| 797 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 798 | if (kDebugStringPoolNoisy) { |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 799 | ALOGI("Caching UTF8 string: %s", u8str); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 800 | } |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 801 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 802 | mCache[idx] = u16str; |
| 803 | return u16str; |
| 804 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 805 | 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] | 806 | (long long)idx, (long long)(u8str+u8len-strings), |
| 807 | (long long)mStringPoolSize); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 808 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | } |
| 810 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 811 | 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] | 812 | (int)idx, (int)(off*sizeof(uint16_t)), |
| 813 | (int)(mStringPoolSize*sizeof(uint16_t))); |
| 814 | } |
| 815 | } |
| 816 | return NULL; |
| 817 | } |
| 818 | |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 819 | const char* ResStringPool::string8At(size_t idx, size_t* outLen) const |
| 820 | { |
| 821 | if (mError == NO_ERROR && idx < mHeader->stringCount) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 822 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) == 0) { |
| 823 | return NULL; |
| 824 | } |
| 825 | const uint32_t off = mEntries[idx]/sizeof(char); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 826 | if (off < (mStringPoolSize-1)) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 827 | const uint8_t* strings = (uint8_t*)mStrings; |
| 828 | const uint8_t* str = strings+off; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 829 | |
| 830 | // Decode the UTF-16 length. This is not used if we're not |
| 831 | // converting to UTF-16 from UTF-8. |
| 832 | decodeLength(&str); |
| 833 | |
| 834 | const size_t encLen = decodeLength(&str); |
| 835 | *outLen = encLen; |
| 836 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 837 | if ((uint32_t)(str+encLen-strings) < mStringPoolSize) { |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 838 | return stringDecodeAt(idx, str, encLen, outLen); |
| 839 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 840 | } else { |
| 841 | ALOGW("Bad string block: string #%d extends to %d, past end at %d\n", |
| 842 | (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 843 | } |
| 844 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 845 | 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] | 846 | (int)idx, (int)(off*sizeof(uint16_t)), |
| 847 | (int)(mStringPoolSize*sizeof(uint16_t))); |
| 848 | } |
| 849 | } |
| 850 | return NULL; |
| 851 | } |
| 852 | |
Ryan Mitchell | 2ad530d | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 853 | /** |
| 854 | * AAPT incorrectly writes a truncated string length when the string size |
| 855 | * exceeded the maximum possible encode length value (0x7FFF). To decode a |
| 856 | * truncated length, iterate through length values that end in the encode length |
| 857 | * bits. Strings that exceed the maximum encode length are not placed into |
| 858 | * StringPools in AAPT2. |
| 859 | **/ |
| 860 | const char* ResStringPool::stringDecodeAt(size_t idx, const uint8_t* str, |
| 861 | const size_t encLen, size_t* outLen) const { |
| 862 | const uint8_t* strings = (uint8_t*)mStrings; |
| 863 | |
| 864 | size_t i = 0, end = encLen; |
| 865 | while ((uint32_t)(str+end-strings) < mStringPoolSize) { |
| 866 | if (str[end] == 0x00) { |
| 867 | if (i != 0) { |
| 868 | ALOGW("Bad string block: string #%d is truncated (actual length is %d)", |
| 869 | (int)idx, (int)end); |
| 870 | } |
| 871 | |
| 872 | *outLen = end; |
| 873 | return (const char*)str; |
| 874 | } |
| 875 | |
| 876 | end = (++i << (sizeof(uint8_t) * 8 * 2 - 1)) | encLen; |
| 877 | } |
| 878 | |
| 879 | // Reject malformed (non null-terminated) strings |
| 880 | ALOGW("Bad string block: string #%d is not null-terminated", |
| 881 | (int)idx); |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 885 | const String8 ResStringPool::string8ObjectAt(size_t idx) const |
| 886 | { |
| 887 | size_t len; |
Adam Lesinski | 4b2d0f2 | 2014-08-14 17:58:37 -0700 | [diff] [blame] | 888 | const char *str = string8At(idx, &len); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 889 | if (str != NULL) { |
Adam Lesinski | 4b2d0f2 | 2014-08-14 17:58:37 -0700 | [diff] [blame] | 890 | return String8(str, len); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 891 | } |
Adam Lesinski | 4b2d0f2 | 2014-08-14 17:58:37 -0700 | [diff] [blame] | 892 | |
| 893 | const char16_t *str16 = stringAt(idx, &len); |
| 894 | if (str16 != NULL) { |
| 895 | return String8(str16, len); |
| 896 | } |
| 897 | return String8(); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 898 | } |
| 899 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const |
| 901 | { |
| 902 | return styleAt(ref.index); |
| 903 | } |
| 904 | |
| 905 | const ResStringPool_span* ResStringPool::styleAt(size_t idx) const |
| 906 | { |
| 907 | if (mError == NO_ERROR && idx < mHeader->styleCount) { |
| 908 | const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t)); |
| 909 | if (off < mStylePoolSize) { |
| 910 | return (const ResStringPool_span*)(mStyles+off); |
| 911 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 912 | 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] | 913 | (int)idx, (int)(off*sizeof(uint32_t)), |
| 914 | (int)(mStylePoolSize*sizeof(uint32_t))); |
| 915 | } |
| 916 | } |
| 917 | return NULL; |
| 918 | } |
| 919 | |
| 920 | ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const |
| 921 | { |
| 922 | if (mError != NO_ERROR) { |
| 923 | return mError; |
| 924 | } |
| 925 | |
| 926 | size_t len; |
| 927 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 928 | if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 929 | if (kDebugStringPoolNoisy) { |
| 930 | ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string()); |
| 931 | } |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 932 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 933 | // The string pool contains UTF 8 strings; we don't want to cause |
| 934 | // temporary UTF-16 strings to be created as we search. |
| 935 | if (mHeader->flags&ResStringPool_header::SORTED_FLAG) { |
| 936 | // Do a binary search for the string... this is a little tricky, |
| 937 | // because the strings are sorted with strzcmp16(). So to match |
| 938 | // the ordering, we need to convert strings in the pool to UTF-16. |
| 939 | // But we don't want to hit the cache, so instead we will have a |
| 940 | // local temporary allocation for the conversions. |
Sergio Giro | 03b95c7 | 2016-07-21 14:44:07 +0100 | [diff] [blame] | 941 | size_t convBufferLen = strLen + 4; |
| 942 | char16_t* convBuffer = (char16_t*)calloc(convBufferLen, sizeof(char16_t)); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 943 | ssize_t l = 0; |
| 944 | ssize_t h = mHeader->stringCount-1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 946 | ssize_t mid; |
| 947 | while (l <= h) { |
| 948 | mid = l + (h - l)/2; |
| 949 | const uint8_t* s = (const uint8_t*)string8At(mid, &len); |
| 950 | int c; |
| 951 | if (s != NULL) { |
Sergio Giro | 03b95c7 | 2016-07-21 14:44:07 +0100 | [diff] [blame] | 952 | char16_t* end = utf8_to_utf16(s, len, convBuffer, convBufferLen); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 953 | c = strzcmp16(convBuffer, end-convBuffer, str, strLen); |
| 954 | } else { |
| 955 | c = -1; |
| 956 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 957 | if (kDebugStringPoolNoisy) { |
| 958 | ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n", |
| 959 | (const char*)s, c, (int)l, (int)mid, (int)h); |
| 960 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 961 | if (c == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 962 | if (kDebugStringPoolNoisy) { |
| 963 | ALOGI("MATCH!"); |
| 964 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 965 | free(convBuffer); |
| 966 | return mid; |
| 967 | } else if (c < 0) { |
| 968 | l = mid + 1; |
| 969 | } else { |
| 970 | h = mid - 1; |
| 971 | } |
| 972 | } |
| 973 | free(convBuffer); |
| 974 | } else { |
| 975 | // It is unusual to get the ID from an unsorted string block... |
| 976 | // most often this happens because we want to get IDs for style |
| 977 | // span tags; since those always appear at the end of the string |
| 978 | // block, start searching at the back. |
| 979 | String8 str8(str, strLen); |
| 980 | const size_t str8Len = str8.size(); |
| 981 | for (int i=mHeader->stringCount-1; i>=0; i--) { |
| 982 | const char* s = string8At(i, &len); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 983 | if (kDebugStringPoolNoisy) { |
| 984 | ALOGI("Looking at %s, i=%d\n", String8(s).string(), i); |
| 985 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 986 | if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 987 | if (kDebugStringPoolNoisy) { |
| 988 | ALOGI("MATCH!"); |
| 989 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 990 | return i; |
| 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 994 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } else { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 996 | if (kDebugStringPoolNoisy) { |
| 997 | ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string()); |
| 998 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 999 | |
| 1000 | if (mHeader->flags&ResStringPool_header::SORTED_FLAG) { |
| 1001 | // Do a binary search for the string... |
| 1002 | ssize_t l = 0; |
| 1003 | ssize_t h = mHeader->stringCount-1; |
| 1004 | |
| 1005 | ssize_t mid; |
| 1006 | while (l <= h) { |
| 1007 | mid = l + (h - l)/2; |
| 1008 | const char16_t* s = stringAt(mid, &len); |
| 1009 | int c = s ? strzcmp16(s, len, str, strLen) : -1; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1010 | if (kDebugStringPoolNoisy) { |
| 1011 | ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n", |
| 1012 | String8(s).string(), c, (int)l, (int)mid, (int)h); |
| 1013 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1014 | if (c == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1015 | if (kDebugStringPoolNoisy) { |
| 1016 | ALOGI("MATCH!"); |
| 1017 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1018 | return mid; |
| 1019 | } else if (c < 0) { |
| 1020 | l = mid + 1; |
| 1021 | } else { |
| 1022 | h = mid - 1; |
| 1023 | } |
| 1024 | } |
| 1025 | } else { |
| 1026 | // It is unusual to get the ID from an unsorted string block... |
| 1027 | // most often this happens because we want to get IDs for style |
| 1028 | // span tags; since those always appear at the end of the string |
| 1029 | // block, start searching at the back. |
| 1030 | for (int i=mHeader->stringCount-1; i>=0; i--) { |
| 1031 | const char16_t* s = stringAt(i, &len); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1032 | if (kDebugStringPoolNoisy) { |
| 1033 | ALOGI("Looking at %s, i=%d\n", String8(s).string(), i); |
| 1034 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1035 | if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1036 | if (kDebugStringPoolNoisy) { |
| 1037 | ALOGI("MATCH!"); |
| 1038 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1039 | return i; |
| 1040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | return NAME_NOT_FOUND; |
| 1046 | } |
| 1047 | |
| 1048 | size_t ResStringPool::size() const |
| 1049 | { |
| 1050 | return (mError == NO_ERROR) ? mHeader->stringCount : 0; |
| 1051 | } |
| 1052 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1053 | size_t ResStringPool::styleCount() const |
| 1054 | { |
| 1055 | return (mError == NO_ERROR) ? mHeader->styleCount : 0; |
| 1056 | } |
| 1057 | |
| 1058 | size_t ResStringPool::bytes() const |
| 1059 | { |
| 1060 | return (mError == NO_ERROR) ? mHeader->header.size : 0; |
| 1061 | } |
| 1062 | |
| 1063 | bool ResStringPool::isSorted() const |
| 1064 | { |
| 1065 | return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0; |
| 1066 | } |
| 1067 | |
Kenny Root | bb79f64 | 2009-12-10 14:20:15 -0800 | [diff] [blame] | 1068 | bool ResStringPool::isUTF8() const |
| 1069 | { |
| 1070 | return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0; |
| 1071 | } |
Kenny Root | bb79f64 | 2009-12-10 14:20:15 -0800 | [diff] [blame] | 1072 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | // -------------------------------------------------------------------- |
| 1074 | // -------------------------------------------------------------------- |
| 1075 | // -------------------------------------------------------------------- |
| 1076 | |
| 1077 | ResXMLParser::ResXMLParser(const ResXMLTree& tree) |
| 1078 | : mTree(tree), mEventCode(BAD_DOCUMENT) |
| 1079 | { |
| 1080 | } |
| 1081 | |
| 1082 | void ResXMLParser::restart() |
| 1083 | { |
| 1084 | mCurNode = NULL; |
| 1085 | mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT; |
| 1086 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1087 | const ResStringPool& ResXMLParser::getStrings() const |
| 1088 | { |
| 1089 | return mTree.mStrings; |
| 1090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | |
| 1092 | ResXMLParser::event_code_t ResXMLParser::getEventType() const |
| 1093 | { |
| 1094 | return mEventCode; |
| 1095 | } |
| 1096 | |
| 1097 | ResXMLParser::event_code_t ResXMLParser::next() |
| 1098 | { |
| 1099 | if (mEventCode == START_DOCUMENT) { |
| 1100 | mCurNode = mTree.mRootNode; |
| 1101 | mCurExt = mTree.mRootExt; |
| 1102 | return (mEventCode=mTree.mRootCode); |
| 1103 | } else if (mEventCode >= FIRST_CHUNK_CODE) { |
| 1104 | return nextNode(); |
| 1105 | } |
| 1106 | return mEventCode; |
| 1107 | } |
| 1108 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1109 | int32_t ResXMLParser::getCommentID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | { |
| 1111 | return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1; |
| 1112 | } |
| 1113 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1114 | const char16_t* ResXMLParser::getComment(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | { |
| 1116 | int32_t id = getCommentID(); |
| 1117 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1118 | } |
| 1119 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1120 | uint32_t ResXMLParser::getLineNumber() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | { |
| 1122 | return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1; |
| 1123 | } |
| 1124 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1125 | int32_t ResXMLParser::getTextID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | { |
| 1127 | if (mEventCode == TEXT) { |
| 1128 | return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index); |
| 1129 | } |
| 1130 | return -1; |
| 1131 | } |
| 1132 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1133 | const char16_t* ResXMLParser::getText(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | { |
| 1135 | int32_t id = getTextID(); |
| 1136 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1137 | } |
| 1138 | |
| 1139 | ssize_t ResXMLParser::getTextValue(Res_value* outValue) const |
| 1140 | { |
| 1141 | if (mEventCode == TEXT) { |
| 1142 | outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData); |
| 1143 | return sizeof(Res_value); |
| 1144 | } |
| 1145 | return BAD_TYPE; |
| 1146 | } |
| 1147 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1148 | int32_t ResXMLParser::getNamespacePrefixID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | { |
| 1150 | if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { |
| 1151 | return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index); |
| 1152 | } |
| 1153 | return -1; |
| 1154 | } |
| 1155 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1156 | const char16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | { |
| 1158 | int32_t id = getNamespacePrefixID(); |
| 1159 | //printf("prefix=%d event=%p\n", id, mEventCode); |
| 1160 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1161 | } |
| 1162 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1163 | int32_t ResXMLParser::getNamespaceUriID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | { |
| 1165 | if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { |
| 1166 | return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index); |
| 1167 | } |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1171 | const char16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | { |
| 1173 | int32_t id = getNamespaceUriID(); |
| 1174 | //printf("uri=%d event=%p\n", id, mEventCode); |
| 1175 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1176 | } |
| 1177 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1178 | int32_t ResXMLParser::getElementNamespaceID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | { |
| 1180 | if (mEventCode == START_TAG) { |
| 1181 | return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index); |
| 1182 | } |
| 1183 | if (mEventCode == END_TAG) { |
| 1184 | return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index); |
| 1185 | } |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1189 | const char16_t* ResXMLParser::getElementNamespace(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | { |
| 1191 | int32_t id = getElementNamespaceID(); |
| 1192 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1193 | } |
| 1194 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1195 | int32_t ResXMLParser::getElementNameID() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | { |
| 1197 | if (mEventCode == START_TAG) { |
| 1198 | return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index); |
| 1199 | } |
| 1200 | if (mEventCode == END_TAG) { |
| 1201 | return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index); |
| 1202 | } |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1206 | const char16_t* ResXMLParser::getElementName(size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | { |
| 1208 | int32_t id = getElementNameID(); |
| 1209 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1210 | } |
| 1211 | |
| 1212 | size_t ResXMLParser::getAttributeCount() const |
| 1213 | { |
| 1214 | if (mEventCode == START_TAG) { |
| 1215 | return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount); |
| 1216 | } |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1220 | int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | { |
| 1222 | if (mEventCode == START_TAG) { |
| 1223 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1224 | if (idx < dtohs(tag->attributeCount)) { |
| 1225 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1226 | (((const uint8_t*)tag) |
| 1227 | + dtohs(tag->attributeStart) |
| 1228 | + (dtohs(tag->attributeSize)*idx)); |
| 1229 | return dtohl(attr->ns.index); |
| 1230 | } |
| 1231 | } |
| 1232 | return -2; |
| 1233 | } |
| 1234 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1235 | const char16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | { |
| 1237 | int32_t id = getAttributeNamespaceID(idx); |
| 1238 | //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1239 | if (kDebugXMLNoisy) { |
| 1240 | printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id); |
| 1241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1243 | } |
| 1244 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1245 | const char* ResXMLParser::getAttributeNamespace8(size_t idx, size_t* outLen) const |
| 1246 | { |
| 1247 | int32_t id = getAttributeNamespaceID(idx); |
| 1248 | //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1249 | if (kDebugXMLNoisy) { |
| 1250 | printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id); |
| 1251 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1252 | return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL; |
| 1253 | } |
| 1254 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1255 | int32_t ResXMLParser::getAttributeNameID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | { |
| 1257 | if (mEventCode == START_TAG) { |
| 1258 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1259 | if (idx < dtohs(tag->attributeCount)) { |
| 1260 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1261 | (((const uint8_t*)tag) |
| 1262 | + dtohs(tag->attributeStart) |
| 1263 | + (dtohs(tag->attributeSize)*idx)); |
| 1264 | return dtohl(attr->name.index); |
| 1265 | } |
| 1266 | } |
| 1267 | return -1; |
| 1268 | } |
| 1269 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1270 | const char16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | { |
| 1272 | int32_t id = getAttributeNameID(idx); |
| 1273 | //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1274 | if (kDebugXMLNoisy) { |
| 1275 | printf("getAttributeName 0x%zx=0x%x\n", idx, id); |
| 1276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1278 | } |
| 1279 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1280 | const char* ResXMLParser::getAttributeName8(size_t idx, size_t* outLen) const |
| 1281 | { |
| 1282 | int32_t id = getAttributeNameID(idx); |
| 1283 | //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1284 | if (kDebugXMLNoisy) { |
| 1285 | printf("getAttributeName 0x%zx=0x%x\n", idx, id); |
| 1286 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1287 | return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL; |
| 1288 | } |
| 1289 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1290 | uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | { |
| 1292 | int32_t id = getAttributeNameID(idx); |
| 1293 | if (id >= 0 && (size_t)id < mTree.mNumResIds) { |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 1294 | uint32_t resId = dtohl(mTree.mResIds[id]); |
| 1295 | if (mTree.mDynamicRefTable != NULL) { |
| 1296 | mTree.mDynamicRefTable->lookupResourceId(&resId); |
| 1297 | } |
| 1298 | return resId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Mathias Agopian | 5f91097 | 2009-06-22 02:35:32 -0700 | [diff] [blame] | 1303 | int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | { |
| 1305 | if (mEventCode == START_TAG) { |
| 1306 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1307 | if (idx < dtohs(tag->attributeCount)) { |
| 1308 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1309 | (((const uint8_t*)tag) |
| 1310 | + dtohs(tag->attributeStart) |
| 1311 | + (dtohs(tag->attributeSize)*idx)); |
| 1312 | return dtohl(attr->rawValue.index); |
| 1313 | } |
| 1314 | } |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1318 | const char16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | { |
| 1320 | int32_t id = getAttributeValueStringID(idx); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1321 | if (kDebugXMLNoisy) { |
| 1322 | printf("getAttributeValue 0x%zx=0x%x\n", idx, id); |
| 1323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; |
| 1325 | } |
| 1326 | |
| 1327 | int32_t ResXMLParser::getAttributeDataType(size_t idx) const |
| 1328 | { |
| 1329 | if (mEventCode == START_TAG) { |
| 1330 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1331 | if (idx < dtohs(tag->attributeCount)) { |
| 1332 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1333 | (((const uint8_t*)tag) |
| 1334 | + dtohs(tag->attributeStart) |
| 1335 | + (dtohs(tag->attributeSize)*idx)); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1336 | uint8_t type = attr->typedValue.dataType; |
| 1337 | if (type != Res_value::TYPE_DYNAMIC_REFERENCE) { |
| 1338 | return type; |
| 1339 | } |
| 1340 | |
| 1341 | // This is a dynamic reference. We adjust those references |
| 1342 | // to regular references at this level, so lie to the caller. |
| 1343 | return Res_value::TYPE_REFERENCE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | return Res_value::TYPE_NULL; |
| 1347 | } |
| 1348 | |
| 1349 | int32_t ResXMLParser::getAttributeData(size_t idx) const |
| 1350 | { |
| 1351 | if (mEventCode == START_TAG) { |
| 1352 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1353 | if (idx < dtohs(tag->attributeCount)) { |
| 1354 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1355 | (((const uint8_t*)tag) |
| 1356 | + dtohs(tag->attributeStart) |
| 1357 | + (dtohs(tag->attributeSize)*idx)); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1358 | if (attr->typedValue.dataType != Res_value::TYPE_DYNAMIC_REFERENCE || |
| 1359 | mTree.mDynamicRefTable == NULL) { |
| 1360 | return dtohl(attr->typedValue.data); |
| 1361 | } |
| 1362 | |
| 1363 | uint32_t data = dtohl(attr->typedValue.data); |
| 1364 | if (mTree.mDynamicRefTable->lookupResourceId(&data) == NO_ERROR) { |
| 1365 | return data; |
| 1366 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const |
| 1373 | { |
| 1374 | if (mEventCode == START_TAG) { |
| 1375 | const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; |
| 1376 | if (idx < dtohs(tag->attributeCount)) { |
| 1377 | const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) |
| 1378 | (((const uint8_t*)tag) |
| 1379 | + dtohs(tag->attributeStart) |
| 1380 | + (dtohs(tag->attributeSize)*idx)); |
| 1381 | outValue->copyFrom_dtoh(attr->typedValue); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1382 | if (mTree.mDynamicRefTable != NULL && |
| 1383 | mTree.mDynamicRefTable->lookupResourceValue(outValue) != NO_ERROR) { |
| 1384 | return BAD_TYPE; |
| 1385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | return sizeof(Res_value); |
| 1387 | } |
| 1388 | } |
| 1389 | return BAD_TYPE; |
| 1390 | } |
| 1391 | |
| 1392 | ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const |
| 1393 | { |
| 1394 | String16 nsStr(ns != NULL ? ns : ""); |
| 1395 | String16 attrStr(attr); |
| 1396 | return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0, |
| 1397 | attrStr.string(), attrStr.size()); |
| 1398 | } |
| 1399 | |
| 1400 | ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen, |
| 1401 | const char16_t* attr, size_t attrLen) const |
| 1402 | { |
| 1403 | if (mEventCode == START_TAG) { |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1404 | if (attr == NULL) { |
| 1405 | return NAME_NOT_FOUND; |
| 1406 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | const size_t N = getAttributeCount(); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1408 | if (mTree.mStrings.isUTF8()) { |
| 1409 | String8 ns8, attr8; |
| 1410 | if (ns != NULL) { |
| 1411 | ns8 = String8(ns, nsLen); |
| 1412 | } |
| 1413 | attr8 = String8(attr, attrLen); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1414 | if (kDebugStringPoolNoisy) { |
| 1415 | ALOGI("indexOfAttribute UTF8 %s (%zu) / %s (%zu)", ns8.string(), nsLen, |
| 1416 | attr8.string(), attrLen); |
| 1417 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1418 | for (size_t i=0; i<N; i++) { |
| 1419 | size_t curNsLen = 0, curAttrLen = 0; |
| 1420 | const char* curNs = getAttributeNamespace8(i, &curNsLen); |
| 1421 | const char* curAttr = getAttributeName8(i, &curAttrLen); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1422 | if (kDebugStringPoolNoisy) { |
| 1423 | ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)", curNs, curNsLen, curAttr, curAttrLen); |
| 1424 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1425 | if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen |
| 1426 | && memcmp(attr8.string(), curAttr, attrLen) == 0) { |
| 1427 | if (ns == NULL) { |
| 1428 | if (curNs == NULL) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1429 | if (kDebugStringPoolNoisy) { |
| 1430 | ALOGI(" FOUND!"); |
| 1431 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1432 | return i; |
| 1433 | } |
| 1434 | } else if (curNs != NULL) { |
| 1435 | //printf(" --> ns=%s, curNs=%s\n", |
| 1436 | // String8(ns).string(), String8(curNs).string()); |
| 1437 | if (memcmp(ns8.string(), curNs, nsLen) == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1438 | if (kDebugStringPoolNoisy) { |
| 1439 | ALOGI(" FOUND!"); |
| 1440 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1441 | return i; |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | } else { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1447 | if (kDebugStringPoolNoisy) { |
| 1448 | ALOGI("indexOfAttribute UTF16 %s (%zu) / %s (%zu)", |
| 1449 | String8(ns, nsLen).string(), nsLen, |
| 1450 | String8(attr, attrLen).string(), attrLen); |
| 1451 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1452 | for (size_t i=0; i<N; i++) { |
| 1453 | size_t curNsLen = 0, curAttrLen = 0; |
| 1454 | const char16_t* curNs = getAttributeNamespace(i, &curNsLen); |
| 1455 | const char16_t* curAttr = getAttributeName(i, &curAttrLen); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1456 | if (kDebugStringPoolNoisy) { |
| 1457 | ALOGI(" curNs=%s (%zu), curAttr=%s (%zu)", |
| 1458 | String8(curNs, curNsLen).string(), curNsLen, |
| 1459 | String8(curAttr, curAttrLen).string(), curAttrLen); |
| 1460 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1461 | if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen |
| 1462 | && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) { |
| 1463 | if (ns == NULL) { |
| 1464 | if (curNs == NULL) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1465 | if (kDebugStringPoolNoisy) { |
| 1466 | ALOGI(" FOUND!"); |
| 1467 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1468 | return i; |
| 1469 | } |
| 1470 | } else if (curNs != NULL) { |
| 1471 | //printf(" --> ns=%s, curNs=%s\n", |
| 1472 | // String8(ns).string(), String8(curNs).string()); |
| 1473 | if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1474 | if (kDebugStringPoolNoisy) { |
| 1475 | ALOGI(" FOUND!"); |
| 1476 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 1477 | return i; |
| 1478 | } |
| 1479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | return NAME_NOT_FOUND; |
| 1486 | } |
| 1487 | |
| 1488 | ssize_t ResXMLParser::indexOfID() const |
| 1489 | { |
| 1490 | if (mEventCode == START_TAG) { |
| 1491 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex); |
| 1492 | if (idx > 0) return (idx-1); |
| 1493 | } |
| 1494 | return NAME_NOT_FOUND; |
| 1495 | } |
| 1496 | |
| 1497 | ssize_t ResXMLParser::indexOfClass() const |
| 1498 | { |
| 1499 | if (mEventCode == START_TAG) { |
| 1500 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex); |
| 1501 | if (idx > 0) return (idx-1); |
| 1502 | } |
| 1503 | return NAME_NOT_FOUND; |
| 1504 | } |
| 1505 | |
| 1506 | ssize_t ResXMLParser::indexOfStyle() const |
| 1507 | { |
| 1508 | if (mEventCode == START_TAG) { |
| 1509 | const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex); |
| 1510 | if (idx > 0) return (idx-1); |
| 1511 | } |
| 1512 | return NAME_NOT_FOUND; |
| 1513 | } |
| 1514 | |
| 1515 | ResXMLParser::event_code_t ResXMLParser::nextNode() |
| 1516 | { |
| 1517 | if (mEventCode < 0) { |
| 1518 | return mEventCode; |
| 1519 | } |
| 1520 | |
| 1521 | do { |
| 1522 | const ResXMLTree_node* next = (const ResXMLTree_node*) |
| 1523 | (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size)); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1524 | if (kDebugXMLNoisy) { |
| 1525 | ALOGI("Next node: prev=%p, next=%p\n", mCurNode, next); |
| 1526 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 1527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | if (((const uint8_t*)next) >= mTree.mDataEnd) { |
| 1529 | mCurNode = NULL; |
| 1530 | return (mEventCode=END_DOCUMENT); |
| 1531 | } |
| 1532 | |
| 1533 | if (mTree.validateNode(next) != NO_ERROR) { |
| 1534 | mCurNode = NULL; |
| 1535 | return (mEventCode=BAD_DOCUMENT); |
| 1536 | } |
| 1537 | |
| 1538 | mCurNode = next; |
| 1539 | const uint16_t headerSize = dtohs(next->header.headerSize); |
| 1540 | const uint32_t totalSize = dtohl(next->header.size); |
| 1541 | mCurExt = ((const uint8_t*)next) + headerSize; |
| 1542 | size_t minExtSize = 0; |
| 1543 | event_code_t eventCode = (event_code_t)dtohs(next->header.type); |
| 1544 | switch ((mEventCode=eventCode)) { |
| 1545 | case RES_XML_START_NAMESPACE_TYPE: |
| 1546 | case RES_XML_END_NAMESPACE_TYPE: |
| 1547 | minExtSize = sizeof(ResXMLTree_namespaceExt); |
| 1548 | break; |
| 1549 | case RES_XML_START_ELEMENT_TYPE: |
| 1550 | minExtSize = sizeof(ResXMLTree_attrExt); |
| 1551 | break; |
| 1552 | case RES_XML_END_ELEMENT_TYPE: |
| 1553 | minExtSize = sizeof(ResXMLTree_endElementExt); |
| 1554 | break; |
| 1555 | case RES_XML_CDATA_TYPE: |
| 1556 | minExtSize = sizeof(ResXMLTree_cdataExt); |
| 1557 | break; |
| 1558 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1559 | 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] | 1560 | (int)dtohs(next->header.type), |
| 1561 | (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader))); |
| 1562 | continue; |
| 1563 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 1564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | if ((totalSize-headerSize) < minExtSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1566 | 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] | 1567 | (int)dtohs(next->header.type), |
| 1568 | (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)), |
| 1569 | (int)(totalSize-headerSize), (int)minExtSize); |
| 1570 | return (mEventCode=BAD_DOCUMENT); |
| 1571 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 1572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n", |
| 1574 | // mCurNode, mCurExt, headerSize, minExtSize); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 1575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | return eventCode; |
| 1577 | } while (true); |
| 1578 | } |
| 1579 | |
| 1580 | void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const |
| 1581 | { |
| 1582 | pos->eventCode = mEventCode; |
| 1583 | pos->curNode = mCurNode; |
| 1584 | pos->curExt = mCurExt; |
| 1585 | } |
| 1586 | |
| 1587 | void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos) |
| 1588 | { |
| 1589 | mEventCode = pos.eventCode; |
| 1590 | mCurNode = pos.curNode; |
| 1591 | mCurExt = pos.curExt; |
| 1592 | } |
| 1593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | // -------------------------------------------------------------------- |
| 1595 | |
| 1596 | static volatile int32_t gCount = 0; |
| 1597 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1598 | ResXMLTree::ResXMLTree(const DynamicRefTable* dynamicRefTable) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | : ResXMLParser(*this) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1600 | , mDynamicRefTable(dynamicRefTable) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | , mError(NO_INIT), mOwnedData(NULL) |
| 1602 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1603 | if (kDebugResXMLTree) { |
| 1604 | ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); |
| 1605 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | restart(); |
| 1607 | } |
| 1608 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1609 | ResXMLTree::ResXMLTree() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | : ResXMLParser(*this) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1611 | , mDynamicRefTable(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | , mError(NO_INIT), mOwnedData(NULL) |
| 1613 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1614 | if (kDebugResXMLTree) { |
| 1615 | ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); |
| 1616 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1617 | restart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | ResXMLTree::~ResXMLTree() |
| 1621 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1622 | if (kDebugResXMLTree) { |
| 1623 | ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1); |
| 1624 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | uninit(); |
| 1626 | } |
| 1627 | |
| 1628 | status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData) |
| 1629 | { |
| 1630 | uninit(); |
| 1631 | mEventCode = START_DOCUMENT; |
| 1632 | |
Kenny Root | 32d6aef | 2012-10-10 10:23:47 -0700 | [diff] [blame] | 1633 | if (!data || !size) { |
| 1634 | return (mError=BAD_TYPE); |
| 1635 | } |
| 1636 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | if (copyData) { |
| 1638 | mOwnedData = malloc(size); |
| 1639 | if (mOwnedData == NULL) { |
| 1640 | return (mError=NO_MEMORY); |
| 1641 | } |
| 1642 | memcpy(mOwnedData, data, size); |
| 1643 | data = mOwnedData; |
| 1644 | } |
| 1645 | |
| 1646 | mHeader = (const ResXMLTree_header*)data; |
| 1647 | mSize = dtohl(mHeader->header.size); |
| 1648 | if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1649 | 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] | 1650 | (int)dtohs(mHeader->header.headerSize), |
| 1651 | (int)dtohl(mHeader->header.size), (int)size); |
| 1652 | mError = BAD_TYPE; |
| 1653 | restart(); |
| 1654 | return mError; |
| 1655 | } |
| 1656 | mDataEnd = ((const uint8_t*)mHeader) + mSize; |
| 1657 | |
| 1658 | mStrings.uninit(); |
| 1659 | mRootNode = NULL; |
| 1660 | mResIds = NULL; |
| 1661 | mNumResIds = 0; |
| 1662 | |
| 1663 | // First look for a couple interesting chunks: the string block |
| 1664 | // and first XML node. |
| 1665 | const ResChunk_header* chunk = |
| 1666 | (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize)); |
| 1667 | const ResChunk_header* lastChunk = chunk; |
| 1668 | while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) && |
| 1669 | ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) { |
| 1670 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML"); |
| 1671 | if (err != NO_ERROR) { |
| 1672 | mError = err; |
| 1673 | goto done; |
| 1674 | } |
| 1675 | const uint16_t type = dtohs(chunk->type); |
| 1676 | const size_t size = dtohl(chunk->size); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1677 | if (kDebugXMLNoisy) { |
| 1678 | printf("Scanning @ %p: type=0x%x, size=0x%zx\n", |
| 1679 | (void*)(((uintptr_t)chunk)-((uintptr_t)mHeader)), type, size); |
| 1680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | if (type == RES_STRING_POOL_TYPE) { |
| 1682 | mStrings.setTo(chunk, size); |
| 1683 | } else if (type == RES_XML_RESOURCE_MAP_TYPE) { |
| 1684 | mResIds = (const uint32_t*) |
| 1685 | (((const uint8_t*)chunk)+dtohs(chunk->headerSize)); |
| 1686 | mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t); |
| 1687 | } else if (type >= RES_XML_FIRST_CHUNK_TYPE |
| 1688 | && type <= RES_XML_LAST_CHUNK_TYPE) { |
| 1689 | if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) { |
| 1690 | mError = BAD_TYPE; |
| 1691 | goto done; |
| 1692 | } |
| 1693 | mCurNode = (const ResXMLTree_node*)lastChunk; |
| 1694 | if (nextNode() == BAD_DOCUMENT) { |
| 1695 | mError = BAD_TYPE; |
| 1696 | goto done; |
| 1697 | } |
| 1698 | mRootNode = mCurNode; |
| 1699 | mRootExt = mCurExt; |
| 1700 | mRootCode = mEventCode; |
| 1701 | break; |
| 1702 | } else { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1703 | if (kDebugXMLNoisy) { |
| 1704 | printf("Skipping unknown chunk!\n"); |
| 1705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | } |
| 1707 | lastChunk = chunk; |
| 1708 | chunk = (const ResChunk_header*) |
| 1709 | (((const uint8_t*)chunk) + size); |
| 1710 | } |
| 1711 | |
| 1712 | if (mRootNode == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1713 | 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] | 1714 | mError = BAD_TYPE; |
| 1715 | goto done; |
| 1716 | } |
| 1717 | |
| 1718 | mError = mStrings.getError(); |
| 1719 | |
| 1720 | done: |
| 1721 | restart(); |
| 1722 | return mError; |
| 1723 | } |
| 1724 | |
| 1725 | status_t ResXMLTree::getError() const |
| 1726 | { |
| 1727 | return mError; |
| 1728 | } |
| 1729 | |
| 1730 | void ResXMLTree::uninit() |
| 1731 | { |
| 1732 | mError = NO_INIT; |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1733 | mStrings.uninit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | if (mOwnedData) { |
| 1735 | free(mOwnedData); |
| 1736 | mOwnedData = NULL; |
| 1737 | } |
| 1738 | restart(); |
| 1739 | } |
| 1740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const |
| 1742 | { |
| 1743 | const uint16_t eventCode = dtohs(node->header.type); |
| 1744 | |
| 1745 | status_t err = validate_chunk( |
| 1746 | &node->header, sizeof(ResXMLTree_node), |
| 1747 | mDataEnd, "ResXMLTree_node"); |
| 1748 | |
| 1749 | if (err >= NO_ERROR) { |
| 1750 | // Only perform additional validation on START nodes |
| 1751 | if (eventCode != RES_XML_START_ELEMENT_TYPE) { |
| 1752 | return NO_ERROR; |
| 1753 | } |
| 1754 | |
| 1755 | const uint16_t headerSize = dtohs(node->header.headerSize); |
| 1756 | const uint32_t size = dtohl(node->header.size); |
| 1757 | const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*) |
| 1758 | (((const uint8_t*)node) + headerSize); |
| 1759 | // check for sensical values pulled out of the stream so far... |
| 1760 | if ((size >= headerSize + sizeof(ResXMLTree_attrExt)) |
| 1761 | && ((void*)attrExt > (void*)node)) { |
| 1762 | const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize)) |
| 1763 | * dtohs(attrExt->attributeCount); |
| 1764 | if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) { |
| 1765 | return NO_ERROR; |
| 1766 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1767 | 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] | 1768 | (unsigned int)(dtohs(attrExt->attributeStart)+attrSize), |
| 1769 | (unsigned int)(size-headerSize)); |
| 1770 | } |
| 1771 | else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1772 | 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] | 1773 | (unsigned int)headerSize, (unsigned int)size); |
| 1774 | } |
| 1775 | return BAD_TYPE; |
| 1776 | } |
| 1777 | |
| 1778 | return err; |
| 1779 | |
| 1780 | #if 0 |
| 1781 | const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE; |
| 1782 | |
| 1783 | const uint16_t headerSize = dtohs(node->header.headerSize); |
| 1784 | const uint32_t size = dtohl(node->header.size); |
| 1785 | |
| 1786 | if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) { |
| 1787 | if (size >= headerSize) { |
| 1788 | if (((const uint8_t*)node) <= (mDataEnd-size)) { |
| 1789 | if (!isStart) { |
| 1790 | return NO_ERROR; |
| 1791 | } |
| 1792 | if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount)) |
| 1793 | <= (size-headerSize)) { |
| 1794 | return NO_ERROR; |
| 1795 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1796 | 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] | 1797 | ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount), |
| 1798 | (int)(size-headerSize)); |
| 1799 | return BAD_TYPE; |
| 1800 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1801 | 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] | 1802 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize); |
| 1803 | return BAD_TYPE; |
| 1804 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1805 | 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] | 1806 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), |
| 1807 | (int)headerSize, (int)size); |
| 1808 | return BAD_TYPE; |
| 1809 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1810 | 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] | 1811 | (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), |
| 1812 | (int)headerSize); |
| 1813 | return BAD_TYPE; |
| 1814 | #endif |
| 1815 | } |
| 1816 | |
| 1817 | // -------------------------------------------------------------------- |
| 1818 | // -------------------------------------------------------------------- |
| 1819 | // -------------------------------------------------------------------- |
| 1820 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1821 | void ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) { |
| 1822 | const size_t size = dtohl(o.size); |
| 1823 | if (size >= sizeof(ResTable_config)) { |
| 1824 | *this = o; |
| 1825 | } else { |
| 1826 | memcpy(this, &o, size); |
| 1827 | memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size); |
| 1828 | } |
| 1829 | } |
| 1830 | |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1831 | /* static */ size_t unpackLanguageOrRegion(const char in[2], const char base, |
| 1832 | char out[4]) { |
| 1833 | if (in[0] & 0x80) { |
| 1834 | // The high bit is "1", which means this is a packed three letter |
| 1835 | // language code. |
| 1836 | |
| 1837 | // The smallest 5 bits of the second char are the first alphabet. |
| 1838 | const uint8_t first = in[1] & 0x1f; |
| 1839 | // The last three bits of the second char and the first two bits |
| 1840 | // of the first char are the second alphabet. |
| 1841 | const uint8_t second = ((in[1] & 0xe0) >> 5) + ((in[0] & 0x03) << 3); |
| 1842 | // Bits 3 to 7 (inclusive) of the first char are the third alphabet. |
| 1843 | const uint8_t third = (in[0] & 0x7c) >> 2; |
| 1844 | |
| 1845 | out[0] = first + base; |
| 1846 | out[1] = second + base; |
| 1847 | out[2] = third + base; |
| 1848 | out[3] = 0; |
| 1849 | |
| 1850 | return 3; |
| 1851 | } |
| 1852 | |
| 1853 | if (in[0]) { |
| 1854 | memcpy(out, in, 2); |
| 1855 | memset(out + 2, 0, 2); |
| 1856 | return 2; |
| 1857 | } |
| 1858 | |
| 1859 | memset(out, 0, 4); |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 1863 | /* static */ void packLanguageOrRegion(const char* in, const char base, |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1864 | char out[2]) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 1865 | if (in[2] == 0 || in[2] == '-') { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1866 | out[0] = in[0]; |
| 1867 | out[1] = in[1]; |
| 1868 | } else { |
Narayan Kamath | b297591 | 2014-06-30 15:59:39 +0100 | [diff] [blame] | 1869 | uint8_t first = (in[0] - base) & 0x007f; |
| 1870 | uint8_t second = (in[1] - base) & 0x007f; |
| 1871 | uint8_t third = (in[2] - base) & 0x007f; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1872 | |
| 1873 | out[0] = (0x80 | (third << 2) | (second >> 3)); |
| 1874 | out[1] = ((second << 5) | first); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 1879 | void ResTable_config::packLanguage(const char* language) { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1880 | packLanguageOrRegion(language, 'a', this->language); |
| 1881 | } |
| 1882 | |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 1883 | void ResTable_config::packRegion(const char* region) { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1884 | packLanguageOrRegion(region, '0', this->country); |
| 1885 | } |
| 1886 | |
| 1887 | size_t ResTable_config::unpackLanguage(char language[4]) const { |
| 1888 | return unpackLanguageOrRegion(this->language, 'a', language); |
| 1889 | } |
| 1890 | |
| 1891 | size_t ResTable_config::unpackRegion(char region[4]) const { |
| 1892 | return unpackLanguageOrRegion(this->country, '0', region); |
| 1893 | } |
| 1894 | |
| 1895 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1896 | void ResTable_config::copyFromDtoH(const ResTable_config& o) { |
| 1897 | copyFromDeviceNoSwap(o); |
| 1898 | size = sizeof(ResTable_config); |
| 1899 | mcc = dtohs(mcc); |
| 1900 | mnc = dtohs(mnc); |
| 1901 | density = dtohs(density); |
| 1902 | screenWidth = dtohs(screenWidth); |
| 1903 | screenHeight = dtohs(screenHeight); |
| 1904 | sdkVersion = dtohs(sdkVersion); |
| 1905 | minorVersion = dtohs(minorVersion); |
| 1906 | smallestScreenWidthDp = dtohs(smallestScreenWidthDp); |
| 1907 | screenWidthDp = dtohs(screenWidthDp); |
| 1908 | screenHeightDp = dtohs(screenHeightDp); |
| 1909 | } |
| 1910 | |
| 1911 | void ResTable_config::swapHtoD() { |
| 1912 | size = htodl(size); |
| 1913 | mcc = htods(mcc); |
| 1914 | mnc = htods(mnc); |
| 1915 | density = htods(density); |
| 1916 | screenWidth = htods(screenWidth); |
| 1917 | screenHeight = htods(screenHeight); |
| 1918 | sdkVersion = htods(sdkVersion); |
| 1919 | minorVersion = htods(minorVersion); |
| 1920 | smallestScreenWidthDp = htods(smallestScreenWidthDp); |
| 1921 | screenWidthDp = htods(screenWidthDp); |
| 1922 | screenHeightDp = htods(screenHeightDp); |
| 1923 | } |
| 1924 | |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1925 | /* static */ inline int compareLocales(const ResTable_config &l, const ResTable_config &r) { |
| 1926 | if (l.locale != r.locale) { |
Ivan Lozano | 599fed4 | 2017-11-01 11:05:45 -0700 | [diff] [blame] | 1927 | return (l.locale > r.locale) ? 1 : -1; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 1930 | // The language & region are equal, so compare the scripts, variants and |
| 1931 | // numbering systms in this order. Comparison of variants and numbering |
| 1932 | // systems should happen very infrequently (if at all.) |
| 1933 | // The comparison code relies on memcmp low-level optimizations that make it |
| 1934 | // more efficient than strncmp. |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 1935 | const char emptyScript[sizeof(l.localeScript)] = {'\0', '\0', '\0', '\0'}; |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 1936 | const char *lScript = l.localeScriptWasComputed ? emptyScript : l.localeScript; |
| 1937 | const char *rScript = r.localeScriptWasComputed ? emptyScript : r.localeScript; |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 1938 | |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 1939 | int script = memcmp(lScript, rScript, sizeof(l.localeScript)); |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1940 | if (script) { |
| 1941 | return script; |
| 1942 | } |
| 1943 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 1944 | int variant = memcmp(l.localeVariant, r.localeVariant, sizeof(l.localeVariant)); |
| 1945 | if (variant) { |
| 1946 | return variant; |
| 1947 | } |
| 1948 | |
| 1949 | return memcmp(l.localeNumberingSystem, r.localeNumberingSystem, |
| 1950 | sizeof(l.localeNumberingSystem)); |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1953 | int ResTable_config::compare(const ResTable_config& o) const { |
Ivan Lozano | 599fed4 | 2017-11-01 11:05:45 -0700 | [diff] [blame] | 1954 | if (imsi != o.imsi) { |
| 1955 | return (imsi > o.imsi) ? 1 : -1; |
| 1956 | } |
| 1957 | |
| 1958 | int32_t diff = compareLocales(*this, o); |
| 1959 | if (diff < 0) { |
| 1960 | return -1; |
| 1961 | } |
| 1962 | if (diff > 0) { |
| 1963 | return 1; |
| 1964 | } |
| 1965 | |
| 1966 | if (screenType != o.screenType) { |
| 1967 | return (screenType > o.screenType) ? 1 : -1; |
| 1968 | } |
| 1969 | if (input != o.input) { |
| 1970 | return (input > o.input) ? 1 : -1; |
| 1971 | } |
| 1972 | if (screenSize != o.screenSize) { |
| 1973 | return (screenSize > o.screenSize) ? 1 : -1; |
| 1974 | } |
| 1975 | if (version != o.version) { |
| 1976 | return (version > o.version) ? 1 : -1; |
| 1977 | } |
| 1978 | if (screenLayout != o.screenLayout) { |
| 1979 | return (screenLayout > o.screenLayout) ? 1 : -1; |
| 1980 | } |
| 1981 | if (screenLayout2 != o.screenLayout2) { |
| 1982 | return (screenLayout2 > o.screenLayout2) ? 1 : -1; |
| 1983 | } |
| 1984 | if (colorMode != o.colorMode) { |
| 1985 | return (colorMode > o.colorMode) ? 1 : -1; |
| 1986 | } |
| 1987 | if (uiMode != o.uiMode) { |
| 1988 | return (uiMode > o.uiMode) ? 1 : -1; |
| 1989 | } |
| 1990 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 1991 | return (smallestScreenWidthDp > o.smallestScreenWidthDp) ? 1 : -1; |
| 1992 | } |
| 1993 | if (screenSizeDp != o.screenSizeDp) { |
| 1994 | return (screenSizeDp > o.screenSizeDp) ? 1 : -1; |
| 1995 | } |
| 1996 | return 0; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | int ResTable_config::compareLogical(const ResTable_config& o) const { |
| 2000 | if (mcc != o.mcc) { |
| 2001 | return mcc < o.mcc ? -1 : 1; |
| 2002 | } |
| 2003 | if (mnc != o.mnc) { |
| 2004 | return mnc < o.mnc ? -1 : 1; |
| 2005 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2006 | |
| 2007 | int diff = compareLocales(*this, o); |
| 2008 | if (diff < 0) { |
| 2009 | return -1; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2010 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2011 | if (diff > 0) { |
| 2012 | return 1; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2013 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2014 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2015 | if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) { |
| 2016 | return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1; |
| 2017 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2018 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 2019 | return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1; |
| 2020 | } |
| 2021 | if (screenWidthDp != o.screenWidthDp) { |
| 2022 | return screenWidthDp < o.screenWidthDp ? -1 : 1; |
| 2023 | } |
| 2024 | if (screenHeightDp != o.screenHeightDp) { |
| 2025 | return screenHeightDp < o.screenHeightDp ? -1 : 1; |
| 2026 | } |
| 2027 | if (screenWidth != o.screenWidth) { |
| 2028 | return screenWidth < o.screenWidth ? -1 : 1; |
| 2029 | } |
| 2030 | if (screenHeight != o.screenHeight) { |
| 2031 | return screenHeight < o.screenHeight ? -1 : 1; |
| 2032 | } |
| 2033 | if (density != o.density) { |
| 2034 | return density < o.density ? -1 : 1; |
| 2035 | } |
| 2036 | if (orientation != o.orientation) { |
| 2037 | return orientation < o.orientation ? -1 : 1; |
| 2038 | } |
| 2039 | if (touchscreen != o.touchscreen) { |
| 2040 | return touchscreen < o.touchscreen ? -1 : 1; |
| 2041 | } |
| 2042 | if (input != o.input) { |
| 2043 | return input < o.input ? -1 : 1; |
| 2044 | } |
| 2045 | if (screenLayout != o.screenLayout) { |
| 2046 | return screenLayout < o.screenLayout ? -1 : 1; |
| 2047 | } |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2048 | if (screenLayout2 != o.screenLayout2) { |
| 2049 | return screenLayout2 < o.screenLayout2 ? -1 : 1; |
| 2050 | } |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2051 | if (colorMode != o.colorMode) { |
| 2052 | return colorMode < o.colorMode ? -1 : 1; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2053 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2054 | if (uiMode != o.uiMode) { |
| 2055 | return uiMode < o.uiMode ? -1 : 1; |
| 2056 | } |
| 2057 | if (version != o.version) { |
| 2058 | return version < o.version ? -1 : 1; |
| 2059 | } |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | int ResTable_config::diff(const ResTable_config& o) const { |
| 2064 | int diffs = 0; |
| 2065 | if (mcc != o.mcc) diffs |= CONFIG_MCC; |
| 2066 | if (mnc != o.mnc) diffs |= CONFIG_MNC; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2067 | if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION; |
| 2068 | if (density != o.density) diffs |= CONFIG_DENSITY; |
| 2069 | if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN; |
| 2070 | if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0) |
| 2071 | diffs |= CONFIG_KEYBOARD_HIDDEN; |
| 2072 | if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD; |
| 2073 | if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION; |
| 2074 | if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE; |
| 2075 | if (version != o.version) diffs |= CONFIG_VERSION; |
Fabrice Di Meglio | 3509935 | 2012-12-12 11:52:03 -0800 | [diff] [blame] | 2076 | if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) diffs |= CONFIG_LAYOUTDIR; |
| 2077 | if ((screenLayout & ~MASK_LAYOUTDIR) != (o.screenLayout & ~MASK_LAYOUTDIR)) diffs |= CONFIG_SCREEN_LAYOUT; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2078 | if ((screenLayout2 & MASK_SCREENROUND) != (o.screenLayout2 & MASK_SCREENROUND)) diffs |= CONFIG_SCREEN_ROUND; |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2079 | if ((colorMode & MASK_WIDE_COLOR_GAMUT) != (o.colorMode & MASK_WIDE_COLOR_GAMUT)) diffs |= CONFIG_COLOR_MODE; |
| 2080 | if ((colorMode & MASK_HDR) != (o.colorMode & MASK_HDR)) diffs |= CONFIG_COLOR_MODE; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2081 | if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE; |
| 2082 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE; |
| 2083 | if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2084 | |
| 2085 | const int diff = compareLocales(*this, o); |
| 2086 | if (diff) diffs |= CONFIG_LOCALE; |
| 2087 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2088 | return diffs; |
| 2089 | } |
| 2090 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2091 | // There isn't a well specified "importance" order between variants and |
| 2092 | // scripts. We can't easily tell whether, say "en-Latn-US" is more or less |
| 2093 | // specific than "en-US-POSIX". |
| 2094 | // |
| 2095 | // We therefore arbitrarily decide to give priority to variants over |
| 2096 | // scripts since it seems more useful to do so. We will consider |
| 2097 | // "en-US-POSIX" to be more specific than "en-Latn-US". |
| 2098 | // |
| 2099 | // Unicode extension keywords are considered to be less important than |
| 2100 | // scripts and variants. |
| 2101 | inline int ResTable_config::getImportanceScoreOfLocale() const { |
| 2102 | return (localeVariant[0] ? 4 : 0) |
| 2103 | + (localeScript[0] && !localeScriptWasComputed ? 2: 0) |
| 2104 | + (localeNumberingSystem[0] ? 1: 0); |
| 2105 | } |
| 2106 | |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2107 | int ResTable_config::isLocaleMoreSpecificThan(const ResTable_config& o) const { |
| 2108 | if (locale || o.locale) { |
| 2109 | if (language[0] != o.language[0]) { |
| 2110 | if (!language[0]) return -1; |
| 2111 | if (!o.language[0]) return 1; |
| 2112 | } |
| 2113 | |
| 2114 | if (country[0] != o.country[0]) { |
| 2115 | if (!country[0]) return -1; |
| 2116 | if (!o.country[0]) return 1; |
| 2117 | } |
| 2118 | } |
| 2119 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2120 | return getImportanceScoreOfLocale() - o.getImportanceScoreOfLocale(); |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2123 | bool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const { |
| 2124 | // The order of the following tests defines the importance of one |
| 2125 | // configuration parameter over another. Those tests first are more |
| 2126 | // important, trumping any values in those following them. |
| 2127 | if (imsi || o.imsi) { |
| 2128 | if (mcc != o.mcc) { |
| 2129 | if (!mcc) return false; |
| 2130 | if (!o.mcc) return true; |
| 2131 | } |
| 2132 | |
| 2133 | if (mnc != o.mnc) { |
| 2134 | if (!mnc) return false; |
| 2135 | if (!o.mnc) return true; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | if (locale || o.locale) { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2140 | const int diff = isLocaleMoreSpecificThan(o); |
| 2141 | if (diff < 0) { |
| 2142 | return false; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2145 | if (diff > 0) { |
| 2146 | return true; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2150 | if (screenLayout || o.screenLayout) { |
| 2151 | if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) { |
| 2152 | if (!(screenLayout & MASK_LAYOUTDIR)) return false; |
| 2153 | if (!(o.screenLayout & MASK_LAYOUTDIR)) return true; |
| 2154 | } |
| 2155 | } |
| 2156 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2157 | if (smallestScreenWidthDp || o.smallestScreenWidthDp) { |
| 2158 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 2159 | if (!smallestScreenWidthDp) return false; |
| 2160 | if (!o.smallestScreenWidthDp) return true; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | if (screenSizeDp || o.screenSizeDp) { |
| 2165 | if (screenWidthDp != o.screenWidthDp) { |
| 2166 | if (!screenWidthDp) return false; |
| 2167 | if (!o.screenWidthDp) return true; |
| 2168 | } |
| 2169 | |
| 2170 | if (screenHeightDp != o.screenHeightDp) { |
| 2171 | if (!screenHeightDp) return false; |
| 2172 | if (!o.screenHeightDp) return true; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | if (screenLayout || o.screenLayout) { |
| 2177 | if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) { |
| 2178 | if (!(screenLayout & MASK_SCREENSIZE)) return false; |
| 2179 | if (!(o.screenLayout & MASK_SCREENSIZE)) return true; |
| 2180 | } |
| 2181 | if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) { |
| 2182 | if (!(screenLayout & MASK_SCREENLONG)) return false; |
| 2183 | if (!(o.screenLayout & MASK_SCREENLONG)) return true; |
| 2184 | } |
| 2185 | } |
| 2186 | |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2187 | if (screenLayout2 || o.screenLayout2) { |
| 2188 | if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0) { |
| 2189 | if (!(screenLayout2 & MASK_SCREENROUND)) return false; |
| 2190 | if (!(o.screenLayout2 & MASK_SCREENROUND)) return true; |
| 2191 | } |
| 2192 | } |
| 2193 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2194 | if (colorMode || o.colorMode) { |
| 2195 | if (((colorMode^o.colorMode) & MASK_HDR) != 0) { |
| 2196 | if (!(colorMode & MASK_HDR)) return false; |
| 2197 | if (!(o.colorMode & MASK_HDR)) return true; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2198 | } |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2199 | if (((colorMode^o.colorMode) & MASK_WIDE_COLOR_GAMUT) != 0) { |
| 2200 | if (!(colorMode & MASK_WIDE_COLOR_GAMUT)) return false; |
| 2201 | if (!(o.colorMode & MASK_WIDE_COLOR_GAMUT)) return true; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2205 | if (orientation != o.orientation) { |
| 2206 | if (!orientation) return false; |
| 2207 | if (!o.orientation) return true; |
| 2208 | } |
| 2209 | |
| 2210 | if (uiMode || o.uiMode) { |
| 2211 | if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) { |
| 2212 | if (!(uiMode & MASK_UI_MODE_TYPE)) return false; |
| 2213 | if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true; |
| 2214 | } |
| 2215 | if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) { |
| 2216 | if (!(uiMode & MASK_UI_MODE_NIGHT)) return false; |
| 2217 | if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | // density is never 'more specific' |
| 2222 | // as the default just equals 160 |
| 2223 | |
| 2224 | if (touchscreen != o.touchscreen) { |
| 2225 | if (!touchscreen) return false; |
| 2226 | if (!o.touchscreen) return true; |
| 2227 | } |
| 2228 | |
| 2229 | if (input || o.input) { |
| 2230 | if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) { |
| 2231 | if (!(inputFlags & MASK_KEYSHIDDEN)) return false; |
| 2232 | if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true; |
| 2233 | } |
| 2234 | |
| 2235 | if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) { |
| 2236 | if (!(inputFlags & MASK_NAVHIDDEN)) return false; |
| 2237 | if (!(o.inputFlags & MASK_NAVHIDDEN)) return true; |
| 2238 | } |
| 2239 | |
| 2240 | if (keyboard != o.keyboard) { |
| 2241 | if (!keyboard) return false; |
| 2242 | if (!o.keyboard) return true; |
| 2243 | } |
| 2244 | |
| 2245 | if (navigation != o.navigation) { |
| 2246 | if (!navigation) return false; |
| 2247 | if (!o.navigation) return true; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | if (screenSize || o.screenSize) { |
| 2252 | if (screenWidth != o.screenWidth) { |
| 2253 | if (!screenWidth) return false; |
| 2254 | if (!o.screenWidth) return true; |
| 2255 | } |
| 2256 | |
| 2257 | if (screenHeight != o.screenHeight) { |
| 2258 | if (!screenHeight) return false; |
| 2259 | if (!o.screenHeight) return true; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | if (version || o.version) { |
| 2264 | if (sdkVersion != o.sdkVersion) { |
| 2265 | if (!sdkVersion) return false; |
| 2266 | if (!o.sdkVersion) return true; |
| 2267 | } |
| 2268 | |
| 2269 | if (minorVersion != o.minorVersion) { |
| 2270 | if (!minorVersion) return false; |
| 2271 | if (!o.minorVersion) return true; |
| 2272 | } |
| 2273 | } |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2277 | // Codes for specially handled languages and regions |
| 2278 | static const char kEnglish[2] = {'e', 'n'}; // packed version of "en" |
| 2279 | static const char kUnitedStates[2] = {'U', 'S'}; // packed version of "US" |
| 2280 | static const char kFilipino[2] = {'\xAD', '\x05'}; // packed version of "fil" |
| 2281 | static const char kTagalog[2] = {'t', 'l'}; // packed version of "tl" |
| 2282 | |
| 2283 | // Checks if two language or region codes are identical |
| 2284 | inline bool areIdentical(const char code1[2], const char code2[2]) { |
| 2285 | return code1[0] == code2[0] && code1[1] == code2[1]; |
| 2286 | } |
| 2287 | |
| 2288 | inline bool langsAreEquivalent(const char lang1[2], const char lang2[2]) { |
| 2289 | return areIdentical(lang1, lang2) || |
| 2290 | (areIdentical(lang1, kTagalog) && areIdentical(lang2, kFilipino)) || |
| 2291 | (areIdentical(lang1, kFilipino) && areIdentical(lang2, kTagalog)); |
| 2292 | } |
| 2293 | |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2294 | bool ResTable_config::isLocaleBetterThan(const ResTable_config& o, |
| 2295 | const ResTable_config* requested) const { |
| 2296 | if (requested->locale == 0) { |
| 2297 | // The request doesn't have a locale, so no resource is better |
| 2298 | // than the other. |
| 2299 | return false; |
| 2300 | } |
| 2301 | |
| 2302 | if (locale == 0 && o.locale == 0) { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2303 | // The locale part of both resources is empty, so none is better |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2304 | // than the other. |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
| 2308 | // Non-matching locales have been filtered out, so both resources |
| 2309 | // match the requested locale. |
| 2310 | // |
| 2311 | // Because of the locale-related checks in match() and the checks, we know |
| 2312 | // that: |
| 2313 | // 1) The resource languages are either empty or match the request; |
| 2314 | // and |
| 2315 | // 2) If the request's script is known, the resource scripts are either |
| 2316 | // unknown or match the request. |
| 2317 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2318 | if (!langsAreEquivalent(language, o.language)) { |
| 2319 | // The languages of the two resources are not equivalent. If we are |
| 2320 | // here, we can only assume that the two resources matched the request |
| 2321 | // because one doesn't have a language and the other has a matching |
| 2322 | // language. |
Roozbeh Pournader | 27953c3 | 2016-02-01 13:49:52 -0800 | [diff] [blame] | 2323 | // |
| 2324 | // We consider the one that has the language specified a better match. |
| 2325 | // |
| 2326 | // The exception is that we consider no-language resources a better match |
| 2327 | // for US English and similar locales than locales that are a descendant |
| 2328 | // of Internatinal English (en-001), since no-language resources are |
| 2329 | // where the US English resource have traditionally lived for most apps. |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2330 | if (areIdentical(requested->language, kEnglish)) { |
| 2331 | if (areIdentical(requested->country, kUnitedStates)) { |
Roozbeh Pournader | 27953c3 | 2016-02-01 13:49:52 -0800 | [diff] [blame] | 2332 | // For US English itself, we consider a no-locale resource a |
| 2333 | // better match if the other resource has a country other than |
| 2334 | // US specified. |
| 2335 | if (language[0] != '\0') { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2336 | return country[0] == '\0' || areIdentical(country, kUnitedStates); |
Roozbeh Pournader | 27953c3 | 2016-02-01 13:49:52 -0800 | [diff] [blame] | 2337 | } else { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2338 | return !(o.country[0] == '\0' || areIdentical(o.country, kUnitedStates)); |
Roozbeh Pournader | 27953c3 | 2016-02-01 13:49:52 -0800 | [diff] [blame] | 2339 | } |
| 2340 | } else if (localeDataIsCloseToUsEnglish(requested->country)) { |
| 2341 | if (language[0] != '\0') { |
| 2342 | return localeDataIsCloseToUsEnglish(country); |
| 2343 | } else { |
| 2344 | return !localeDataIsCloseToUsEnglish(o.country); |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | return (language[0] != '\0'); |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2349 | } |
| 2350 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2351 | // If we are here, both the resources have an equivalent non-empty language |
| 2352 | // to the request. |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2353 | // |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2354 | // Because the languages are equivalent, computeScript() always returns a |
| 2355 | // non-empty script for languages it knows about, and we have passed the |
| 2356 | // script checks in match(), the scripts are either all unknown or are all |
| 2357 | // the same. So we can't gain anything by checking the scripts. We need to |
| 2358 | // check the region and variant. |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2359 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2360 | // See if any of the regions is better than the other. |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2361 | const int region_comparison = localeDataCompareRegions( |
| 2362 | country, o.country, |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2363 | requested->language, requested->localeScript, requested->country); |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2364 | if (region_comparison != 0) { |
| 2365 | return (region_comparison > 0); |
| 2366 | } |
| 2367 | |
| 2368 | // The regions are the same. Try the variant. |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2369 | const bool localeMatches = strncmp( |
| 2370 | localeVariant, requested->localeVariant, sizeof(localeVariant)) == 0; |
| 2371 | const bool otherMatches = strncmp( |
| 2372 | o.localeVariant, requested->localeVariant, sizeof(localeVariant)) == 0; |
| 2373 | if (localeMatches != otherMatches) { |
| 2374 | return localeMatches; |
| 2375 | } |
| 2376 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2377 | // The variants are the same, try numbering system. |
| 2378 | const bool localeNumsysMatches = strncmp(localeNumberingSystem, |
| 2379 | requested->localeNumberingSystem, |
| 2380 | sizeof(localeNumberingSystem)) == 0; |
| 2381 | const bool otherNumsysMatches = strncmp(o.localeNumberingSystem, |
| 2382 | requested->localeNumberingSystem, |
| 2383 | sizeof(localeNumberingSystem)) == 0; |
| 2384 | if (localeNumsysMatches != otherNumsysMatches) { |
| 2385 | return localeNumsysMatches; |
| 2386 | } |
| 2387 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2388 | // Finally, the languages, although equivalent, may still be different |
| 2389 | // (like for Tagalog and Filipino). Identical is better than just |
| 2390 | // equivalent. |
| 2391 | if (areIdentical(language, requested->language) |
| 2392 | && !areIdentical(o.language, requested->language)) { |
| 2393 | return true; |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | return false; |
| 2397 | } |
| 2398 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2399 | bool ResTable_config::isBetterThan(const ResTable_config& o, |
| 2400 | const ResTable_config* requested) const { |
| 2401 | if (requested) { |
| 2402 | if (imsi || o.imsi) { |
| 2403 | if ((mcc != o.mcc) && requested->mcc) { |
| 2404 | return (mcc); |
| 2405 | } |
| 2406 | |
| 2407 | if ((mnc != o.mnc) && requested->mnc) { |
| 2408 | return (mnc); |
| 2409 | } |
| 2410 | } |
| 2411 | |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2412 | if (isLocaleBetterThan(o, requested)) { |
| 2413 | return true; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2416 | if (screenLayout || o.screenLayout) { |
| 2417 | if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0 |
| 2418 | && (requested->screenLayout & MASK_LAYOUTDIR)) { |
| 2419 | int myLayoutDir = screenLayout & MASK_LAYOUTDIR; |
| 2420 | int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR; |
| 2421 | return (myLayoutDir > oLayoutDir); |
| 2422 | } |
| 2423 | } |
| 2424 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2425 | if (smallestScreenWidthDp || o.smallestScreenWidthDp) { |
| 2426 | // The configuration closest to the actual size is best. |
| 2427 | // We assume that larger configs have already been filtered |
| 2428 | // out at this point. That means we just want the largest one. |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2429 | if (smallestScreenWidthDp != o.smallestScreenWidthDp) { |
| 2430 | return smallestScreenWidthDp > o.smallestScreenWidthDp; |
| 2431 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | if (screenSizeDp || o.screenSizeDp) { |
| 2435 | // "Better" is based on the sum of the difference between both |
| 2436 | // width and height from the requested dimensions. We are |
| 2437 | // assuming the invalid configs (with smaller dimens) have |
| 2438 | // already been filtered. Note that if a particular dimension |
| 2439 | // is unspecified, we will end up with a large value (the |
| 2440 | // difference between 0 and the requested dimension), which is |
| 2441 | // good since we will prefer a config that has specified a |
| 2442 | // dimension value. |
| 2443 | int myDelta = 0, otherDelta = 0; |
| 2444 | if (requested->screenWidthDp) { |
| 2445 | myDelta += requested->screenWidthDp - screenWidthDp; |
| 2446 | otherDelta += requested->screenWidthDp - o.screenWidthDp; |
| 2447 | } |
| 2448 | if (requested->screenHeightDp) { |
| 2449 | myDelta += requested->screenHeightDp - screenHeightDp; |
| 2450 | otherDelta += requested->screenHeightDp - o.screenHeightDp; |
| 2451 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 2452 | if (kDebugTableSuperNoisy) { |
| 2453 | ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d", |
| 2454 | screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp, |
| 2455 | requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta); |
| 2456 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2457 | if (myDelta != otherDelta) { |
| 2458 | return myDelta < otherDelta; |
| 2459 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | if (screenLayout || o.screenLayout) { |
| 2463 | if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0 |
| 2464 | && (requested->screenLayout & MASK_SCREENSIZE)) { |
| 2465 | // A little backwards compatibility here: undefined is |
| 2466 | // considered equivalent to normal. But only if the |
| 2467 | // requested size is at least normal; otherwise, small |
| 2468 | // is better than the default. |
| 2469 | int mySL = (screenLayout & MASK_SCREENSIZE); |
| 2470 | int oSL = (o.screenLayout & MASK_SCREENSIZE); |
| 2471 | int fixedMySL = mySL; |
| 2472 | int fixedOSL = oSL; |
| 2473 | if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) { |
| 2474 | if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL; |
| 2475 | if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL; |
| 2476 | } |
| 2477 | // For screen size, the best match is the one that is |
| 2478 | // closest to the requested screen size, but not over |
| 2479 | // (the not over part is dealt with in match() below). |
| 2480 | if (fixedMySL == fixedOSL) { |
| 2481 | // If the two are the same, but 'this' is actually |
| 2482 | // undefined, then the other is really a better match. |
| 2483 | if (mySL == 0) return false; |
| 2484 | return true; |
| 2485 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2486 | if (fixedMySL != fixedOSL) { |
| 2487 | return fixedMySL > fixedOSL; |
| 2488 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2489 | } |
| 2490 | if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0 |
| 2491 | && (requested->screenLayout & MASK_SCREENLONG)) { |
| 2492 | return (screenLayout & MASK_SCREENLONG); |
| 2493 | } |
| 2494 | } |
| 2495 | |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2496 | if (screenLayout2 || o.screenLayout2) { |
| 2497 | if (((screenLayout2^o.screenLayout2) & MASK_SCREENROUND) != 0 && |
| 2498 | (requested->screenLayout2 & MASK_SCREENROUND)) { |
| 2499 | return screenLayout2 & MASK_SCREENROUND; |
| 2500 | } |
| 2501 | } |
| 2502 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2503 | if (colorMode || o.colorMode) { |
| 2504 | if (((colorMode^o.colorMode) & MASK_WIDE_COLOR_GAMUT) != 0 && |
| 2505 | (requested->colorMode & MASK_WIDE_COLOR_GAMUT)) { |
| 2506 | return colorMode & MASK_WIDE_COLOR_GAMUT; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2507 | } |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2508 | if (((colorMode^o.colorMode) & MASK_HDR) != 0 && |
| 2509 | (requested->colorMode & MASK_HDR)) { |
| 2510 | return colorMode & MASK_HDR; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2514 | if ((orientation != o.orientation) && requested->orientation) { |
| 2515 | return (orientation); |
| 2516 | } |
| 2517 | |
| 2518 | if (uiMode || o.uiMode) { |
| 2519 | if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0 |
| 2520 | && (requested->uiMode & MASK_UI_MODE_TYPE)) { |
| 2521 | return (uiMode & MASK_UI_MODE_TYPE); |
| 2522 | } |
| 2523 | if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0 |
| 2524 | && (requested->uiMode & MASK_UI_MODE_NIGHT)) { |
| 2525 | return (uiMode & MASK_UI_MODE_NIGHT); |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | if (screenType || o.screenType) { |
| 2530 | if (density != o.density) { |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 2531 | // Use the system default density (DENSITY_MEDIUM, 160dpi) if none specified. |
| 2532 | const int thisDensity = density ? density : int(ResTable_config::DENSITY_MEDIUM); |
| 2533 | const int otherDensity = o.density ? o.density : int(ResTable_config::DENSITY_MEDIUM); |
| 2534 | |
| 2535 | // We always prefer DENSITY_ANY over scaling a density bucket. |
| 2536 | if (thisDensity == ResTable_config::DENSITY_ANY) { |
| 2537 | return true; |
| 2538 | } else if (otherDensity == ResTable_config::DENSITY_ANY) { |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
| 2542 | int requestedDensity = requested->density; |
| 2543 | if (requested->density == 0 || |
| 2544 | requested->density == ResTable_config::DENSITY_ANY) { |
| 2545 | requestedDensity = ResTable_config::DENSITY_MEDIUM; |
| 2546 | } |
| 2547 | |
| 2548 | // DENSITY_ANY is now dealt with. We should look to |
| 2549 | // pick a density bucket and potentially scale it. |
| 2550 | // Any density is potentially useful |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2551 | // because the system will scale it. Scaling down |
| 2552 | // is generally better than scaling up. |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 2553 | int h = thisDensity; |
| 2554 | int l = otherDensity; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2555 | bool bImBigger = true; |
| 2556 | if (l > h) { |
| 2557 | int t = h; |
| 2558 | h = l; |
| 2559 | l = t; |
| 2560 | bImBigger = false; |
| 2561 | } |
| 2562 | |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 2563 | if (requestedDensity >= h) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2564 | // requested value higher than both l and h, give h |
| 2565 | return bImBigger; |
| 2566 | } |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 2567 | if (l >= requestedDensity) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2568 | // requested value lower than both l and h, give l |
| 2569 | return !bImBigger; |
| 2570 | } |
| 2571 | // saying that scaling down is 2x better than up |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 2572 | if (((2 * l) - requestedDensity) * h > requestedDensity * requestedDensity) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2573 | return !bImBigger; |
| 2574 | } else { |
| 2575 | return bImBigger; |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | if ((touchscreen != o.touchscreen) && requested->touchscreen) { |
| 2580 | return (touchscreen); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | if (input || o.input) { |
| 2585 | const int keysHidden = inputFlags & MASK_KEYSHIDDEN; |
| 2586 | const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN; |
| 2587 | if (keysHidden != oKeysHidden) { |
| 2588 | const int reqKeysHidden = |
| 2589 | requested->inputFlags & MASK_KEYSHIDDEN; |
| 2590 | if (reqKeysHidden) { |
| 2591 | |
| 2592 | if (!keysHidden) return false; |
| 2593 | if (!oKeysHidden) return true; |
| 2594 | // For compatibility, we count KEYSHIDDEN_NO as being |
| 2595 | // the same as KEYSHIDDEN_SOFT. Here we disambiguate |
| 2596 | // these by making an exact match more specific. |
| 2597 | if (reqKeysHidden == keysHidden) return true; |
| 2598 | if (reqKeysHidden == oKeysHidden) return false; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | const int navHidden = inputFlags & MASK_NAVHIDDEN; |
| 2603 | const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN; |
| 2604 | if (navHidden != oNavHidden) { |
| 2605 | const int reqNavHidden = |
| 2606 | requested->inputFlags & MASK_NAVHIDDEN; |
| 2607 | if (reqNavHidden) { |
| 2608 | |
| 2609 | if (!navHidden) return false; |
| 2610 | if (!oNavHidden) return true; |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if ((keyboard != o.keyboard) && requested->keyboard) { |
| 2615 | return (keyboard); |
| 2616 | } |
| 2617 | |
| 2618 | if ((navigation != o.navigation) && requested->navigation) { |
| 2619 | return (navigation); |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | if (screenSize || o.screenSize) { |
| 2624 | // "Better" is based on the sum of the difference between both |
| 2625 | // width and height from the requested dimensions. We are |
| 2626 | // assuming the invalid configs (with smaller sizes) have |
| 2627 | // already been filtered. Note that if a particular dimension |
| 2628 | // is unspecified, we will end up with a large value (the |
| 2629 | // difference between 0 and the requested dimension), which is |
| 2630 | // good since we will prefer a config that has specified a |
| 2631 | // size value. |
| 2632 | int myDelta = 0, otherDelta = 0; |
| 2633 | if (requested->screenWidth) { |
| 2634 | myDelta += requested->screenWidth - screenWidth; |
| 2635 | otherDelta += requested->screenWidth - o.screenWidth; |
| 2636 | } |
| 2637 | if (requested->screenHeight) { |
| 2638 | myDelta += requested->screenHeight - screenHeight; |
| 2639 | otherDelta += requested->screenHeight - o.screenHeight; |
| 2640 | } |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 2641 | if (myDelta != otherDelta) { |
| 2642 | return myDelta < otherDelta; |
| 2643 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | if (version || o.version) { |
| 2647 | if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) { |
| 2648 | return (sdkVersion > o.sdkVersion); |
| 2649 | } |
| 2650 | |
| 2651 | if ((minorVersion != o.minorVersion) && |
| 2652 | requested->minorVersion) { |
| 2653 | return (minorVersion); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | return false; |
| 2658 | } |
| 2659 | return isMoreSpecificThan(o); |
| 2660 | } |
| 2661 | |
| 2662 | bool ResTable_config::match(const ResTable_config& settings) const { |
| 2663 | if (imsi != 0) { |
| 2664 | if (mcc != 0 && mcc != settings.mcc) { |
| 2665 | return false; |
| 2666 | } |
| 2667 | if (mnc != 0 && mnc != settings.mnc) { |
| 2668 | return false; |
| 2669 | } |
| 2670 | } |
| 2671 | if (locale != 0) { |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2672 | // Don't consider country and variants when deciding matches. |
| 2673 | // (Theoretically, the variant can also affect the script. For |
| 2674 | // example, "ar-alalc97" probably implies the Latin script, but since |
| 2675 | // CLDR doesn't support getting likely scripts for that, we'll assume |
| 2676 | // the variant doesn't change the script.) |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2677 | // |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2678 | // If two configs differ only in their country and variant, |
| 2679 | // they can be weeded out in the isMoreSpecificThan test. |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2680 | if (!langsAreEquivalent(language, settings.language)) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2681 | return false; |
| 2682 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2683 | |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2684 | // For backward compatibility and supporting private-use locales, we |
| 2685 | // fall back to old behavior if we couldn't determine the script for |
Roozbeh Pournader | 4de4596 | 2016-02-11 17:58:24 -0800 | [diff] [blame] | 2686 | // either of the desired locale or the provided locale. But if we could determine |
| 2687 | // the scripts, they should be the same for the locales to match. |
| 2688 | bool countriesMustMatch = false; |
| 2689 | char computed_script[4]; |
| 2690 | const char* script; |
| 2691 | if (settings.localeScript[0] == '\0') { // could not determine the request's script |
| 2692 | countriesMustMatch = true; |
| 2693 | } else { |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 2694 | if (localeScript[0] == '\0' && !localeScriptWasComputed) { |
| 2695 | // script was not provided or computed, so we try to compute it |
Roozbeh Pournader | 4de4596 | 2016-02-11 17:58:24 -0800 | [diff] [blame] | 2696 | localeDataComputeScript(computed_script, language, country); |
| 2697 | if (computed_script[0] == '\0') { // we could not compute the script |
| 2698 | countriesMustMatch = true; |
| 2699 | } else { |
| 2700 | script = computed_script; |
| 2701 | } |
| 2702 | } else { // script was provided, so just use it |
| 2703 | script = localeScript; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | if (countriesMustMatch) { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2708 | if (country[0] != '\0' && !areIdentical(country, settings.country)) { |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2709 | return false; |
| 2710 | } |
| 2711 | } else { |
Roozbeh Pournader | 4de4596 | 2016-02-11 17:58:24 -0800 | [diff] [blame] | 2712 | if (memcmp(script, settings.localeScript, sizeof(settings.localeScript)) != 0) { |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2713 | return false; |
| 2714 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2715 | } |
| 2716 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2717 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2718 | if (screenConfig != 0) { |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 2719 | const int layoutDir = screenLayout&MASK_LAYOUTDIR; |
| 2720 | const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR; |
| 2721 | if (layoutDir != 0 && layoutDir != setLayoutDir) { |
| 2722 | return false; |
| 2723 | } |
| 2724 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2725 | const int screenSize = screenLayout&MASK_SCREENSIZE; |
| 2726 | const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE; |
| 2727 | // Any screen sizes for larger screens than the setting do not |
| 2728 | // match. |
| 2729 | if (screenSize != 0 && screenSize > setScreenSize) { |
| 2730 | return false; |
| 2731 | } |
| 2732 | |
| 2733 | const int screenLong = screenLayout&MASK_SCREENLONG; |
| 2734 | const int setScreenLong = settings.screenLayout&MASK_SCREENLONG; |
| 2735 | if (screenLong != 0 && screenLong != setScreenLong) { |
| 2736 | return false; |
| 2737 | } |
| 2738 | |
| 2739 | const int uiModeType = uiMode&MASK_UI_MODE_TYPE; |
| 2740 | const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE; |
| 2741 | if (uiModeType != 0 && uiModeType != setUiModeType) { |
| 2742 | return false; |
| 2743 | } |
| 2744 | |
| 2745 | const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT; |
| 2746 | const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT; |
| 2747 | if (uiModeNight != 0 && uiModeNight != setUiModeNight) { |
| 2748 | return false; |
| 2749 | } |
| 2750 | |
| 2751 | if (smallestScreenWidthDp != 0 |
| 2752 | && smallestScreenWidthDp > settings.smallestScreenWidthDp) { |
| 2753 | return false; |
| 2754 | } |
| 2755 | } |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2756 | |
| 2757 | if (screenConfig2 != 0) { |
| 2758 | const int screenRound = screenLayout2 & MASK_SCREENROUND; |
| 2759 | const int setScreenRound = settings.screenLayout2 & MASK_SCREENROUND; |
| 2760 | if (screenRound != 0 && screenRound != setScreenRound) { |
| 2761 | return false; |
| 2762 | } |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2763 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2764 | const int hdr = colorMode & MASK_HDR; |
| 2765 | const int setHdr = settings.colorMode & MASK_HDR; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2766 | if (hdr != 0 && hdr != setHdr) { |
| 2767 | return false; |
| 2768 | } |
| 2769 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 2770 | const int wideColorGamut = colorMode & MASK_WIDE_COLOR_GAMUT; |
| 2771 | const int setWideColorGamut = settings.colorMode & MASK_WIDE_COLOR_GAMUT; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 2772 | if (wideColorGamut != 0 && wideColorGamut != setWideColorGamut) { |
| 2773 | return false; |
| 2774 | } |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2777 | if (screenSizeDp != 0) { |
| 2778 | if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 2779 | if (kDebugTableSuperNoisy) { |
| 2780 | ALOGI("Filtering out width %d in requested %d", screenWidthDp, |
| 2781 | settings.screenWidthDp); |
| 2782 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2783 | return false; |
| 2784 | } |
| 2785 | if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 2786 | if (kDebugTableSuperNoisy) { |
| 2787 | ALOGI("Filtering out height %d in requested %d", screenHeightDp, |
| 2788 | settings.screenHeightDp); |
| 2789 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2790 | return false; |
| 2791 | } |
| 2792 | } |
| 2793 | if (screenType != 0) { |
| 2794 | if (orientation != 0 && orientation != settings.orientation) { |
| 2795 | return false; |
| 2796 | } |
| 2797 | // density always matches - we can scale it. See isBetterThan |
| 2798 | if (touchscreen != 0 && touchscreen != settings.touchscreen) { |
| 2799 | return false; |
| 2800 | } |
| 2801 | } |
| 2802 | if (input != 0) { |
| 2803 | const int keysHidden = inputFlags&MASK_KEYSHIDDEN; |
| 2804 | const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN; |
| 2805 | if (keysHidden != 0 && keysHidden != setKeysHidden) { |
| 2806 | // For compatibility, we count a request for KEYSHIDDEN_NO as also |
| 2807 | // matching the more recent KEYSHIDDEN_SOFT. Basically |
| 2808 | // KEYSHIDDEN_NO means there is some kind of keyboard available. |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 2809 | if (kDebugTableSuperNoisy) { |
| 2810 | ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden); |
| 2811 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2812 | if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 2813 | if (kDebugTableSuperNoisy) { |
| 2814 | ALOGI("No match!"); |
| 2815 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2816 | return false; |
| 2817 | } |
| 2818 | } |
| 2819 | const int navHidden = inputFlags&MASK_NAVHIDDEN; |
| 2820 | const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN; |
| 2821 | if (navHidden != 0 && navHidden != setNavHidden) { |
| 2822 | return false; |
| 2823 | } |
| 2824 | if (keyboard != 0 && keyboard != settings.keyboard) { |
| 2825 | return false; |
| 2826 | } |
| 2827 | if (navigation != 0 && navigation != settings.navigation) { |
| 2828 | return false; |
| 2829 | } |
| 2830 | } |
| 2831 | if (screenSize != 0) { |
| 2832 | if (screenWidth != 0 && screenWidth > settings.screenWidth) { |
| 2833 | return false; |
| 2834 | } |
| 2835 | if (screenHeight != 0 && screenHeight > settings.screenHeight) { |
| 2836 | return false; |
| 2837 | } |
| 2838 | } |
| 2839 | if (version != 0) { |
| 2840 | if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) { |
| 2841 | return false; |
| 2842 | } |
| 2843 | if (minorVersion != 0 && minorVersion != settings.minorVersion) { |
| 2844 | return false; |
| 2845 | } |
| 2846 | } |
| 2847 | return true; |
| 2848 | } |
| 2849 | |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2850 | void ResTable_config::appendDirLocale(String8& out) const { |
| 2851 | if (!language[0]) { |
| 2852 | return; |
| 2853 | } |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 2854 | const bool scriptWasProvided = localeScript[0] != '\0' && !localeScriptWasComputed; |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2855 | if (!scriptWasProvided && !localeVariant[0] && !localeNumberingSystem[0]) { |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2856 | // Legacy format. |
| 2857 | if (out.size() > 0) { |
| 2858 | out.append("-"); |
| 2859 | } |
| 2860 | |
| 2861 | char buf[4]; |
| 2862 | size_t len = unpackLanguage(buf); |
| 2863 | out.append(buf, len); |
| 2864 | |
| 2865 | if (country[0]) { |
| 2866 | out.append("-r"); |
| 2867 | len = unpackRegion(buf); |
| 2868 | out.append(buf, len); |
| 2869 | } |
| 2870 | return; |
| 2871 | } |
| 2872 | |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2873 | // We are writing the modified BCP 47 tag. |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2874 | // It starts with 'b+' and uses '+' as a separator. |
| 2875 | |
| 2876 | if (out.size() > 0) { |
| 2877 | out.append("-"); |
| 2878 | } |
| 2879 | out.append("b+"); |
| 2880 | |
| 2881 | char buf[4]; |
| 2882 | size_t len = unpackLanguage(buf); |
| 2883 | out.append(buf, len); |
| 2884 | |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 2885 | if (scriptWasProvided) { |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2886 | out.append("+"); |
| 2887 | out.append(localeScript, sizeof(localeScript)); |
| 2888 | } |
| 2889 | |
| 2890 | if (country[0]) { |
| 2891 | out.append("+"); |
| 2892 | len = unpackRegion(buf); |
| 2893 | out.append(buf, len); |
| 2894 | } |
| 2895 | |
| 2896 | if (localeVariant[0]) { |
| 2897 | out.append("+"); |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 2898 | out.append(localeVariant, strnlen(localeVariant, sizeof(localeVariant))); |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2899 | } |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2900 | |
| 2901 | if (localeNumberingSystem[0]) { |
| 2902 | out.append("+u+nu+"); |
| 2903 | out.append(localeNumberingSystem, |
| 2904 | strnlen(localeNumberingSystem, sizeof(localeNumberingSystem))); |
| 2905 | } |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 2906 | } |
| 2907 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2908 | void ResTable_config::getBcp47Locale(char str[RESTABLE_MAX_LOCALE_LEN], bool canonicalize) const { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2909 | memset(str, 0, RESTABLE_MAX_LOCALE_LEN); |
| 2910 | |
| 2911 | // This represents the "any" locale value, which has traditionally been |
| 2912 | // represented by the empty string. |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2913 | if (language[0] == '\0' && country[0] == '\0') { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2914 | return; |
| 2915 | } |
| 2916 | |
| 2917 | size_t charsWritten = 0; |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2918 | if (language[0] != '\0') { |
| 2919 | if (canonicalize && areIdentical(language, kTagalog)) { |
| 2920 | // Replace Tagalog with Filipino if we are canonicalizing |
| 2921 | str[0] = 'f'; str[1] = 'i'; str[2] = 'l'; str[3] = '\0'; // 3-letter code for Filipino |
| 2922 | charsWritten += 3; |
| 2923 | } else { |
| 2924 | charsWritten += unpackLanguage(str); |
| 2925 | } |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2928 | if (localeScript[0] != '\0' && !localeScriptWasComputed) { |
| 2929 | if (charsWritten > 0) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 2930 | str[charsWritten++] = '-'; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2931 | } |
| 2932 | memcpy(str + charsWritten, localeScript, sizeof(localeScript)); |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 2933 | charsWritten += sizeof(localeScript); |
| 2934 | } |
| 2935 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2936 | if (country[0] != '\0') { |
| 2937 | if (charsWritten > 0) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 2938 | str[charsWritten++] = '-'; |
| 2939 | } |
| 2940 | charsWritten += unpackRegion(str + charsWritten); |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 2943 | if (localeVariant[0] != '\0') { |
| 2944 | if (charsWritten > 0) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 2945 | str[charsWritten++] = '-'; |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 2946 | } |
| 2947 | memcpy(str + charsWritten, localeVariant, sizeof(localeVariant)); |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2948 | charsWritten += strnlen(str + charsWritten, sizeof(localeVariant)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2949 | } |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 2950 | |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 2951 | // Add Unicode extension only if at least one other locale component is present |
| 2952 | if (localeNumberingSystem[0] != '\0' && charsWritten > 0) { |
| 2953 | static constexpr char NU_PREFIX[] = "-u-nu-"; |
| 2954 | static constexpr size_t NU_PREFIX_LEN = sizeof(NU_PREFIX) - 1; |
| 2955 | memcpy(str + charsWritten, NU_PREFIX, NU_PREFIX_LEN); |
| 2956 | charsWritten += NU_PREFIX_LEN; |
| 2957 | memcpy(str + charsWritten, localeNumberingSystem, sizeof(localeNumberingSystem)); |
| 2958 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 2961 | struct LocaleParserState { |
| 2962 | enum State : uint8_t { |
| 2963 | BASE, UNICODE_EXTENSION, IGNORE_THE_REST |
| 2964 | } parserState; |
| 2965 | enum UnicodeState : uint8_t { |
| 2966 | /* Initial state after the Unicode singleton is detected. Either a keyword |
| 2967 | * or an attribute is expected. */ |
| 2968 | NO_KEY, |
| 2969 | /* Unicode extension key (but not attribute) is expected. Next states: |
| 2970 | * NO_KEY, IGNORE_KEY or NUMBERING_SYSTEM. */ |
| 2971 | EXPECT_KEY, |
| 2972 | /* A key is detected, however it is not supported for now. Ignore its |
| 2973 | * value. Next states: IGNORE_KEY or NUMBERING_SYSTEM. */ |
| 2974 | IGNORE_KEY, |
| 2975 | /* Numbering system key was detected. Store its value in the configuration |
| 2976 | * localeNumberingSystem field. Next state: EXPECT_KEY */ |
| 2977 | NUMBERING_SYSTEM |
| 2978 | } unicodeState; |
| 2979 | |
| 2980 | LocaleParserState(): parserState(BASE), unicodeState(NO_KEY) {} |
| 2981 | }; |
| 2982 | |
| 2983 | /* static */ inline LocaleParserState assignLocaleComponent(ResTable_config* config, |
| 2984 | const char* start, size_t size, LocaleParserState state) { |
| 2985 | |
| 2986 | /* It is assumed that this function is not invoked with state.parserState |
| 2987 | * set to IGNORE_THE_REST. The condition is checked by setBcp47Locale |
| 2988 | * function. */ |
| 2989 | |
| 2990 | if (state.parserState == LocaleParserState::UNICODE_EXTENSION) { |
| 2991 | switch (size) { |
| 2992 | case 1: |
| 2993 | /* Other BCP 47 extensions are not supported at the moment */ |
| 2994 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 2995 | break; |
| 2996 | case 2: |
| 2997 | if (state.unicodeState == LocaleParserState::NO_KEY || |
| 2998 | state.unicodeState == LocaleParserState::EXPECT_KEY) { |
| 2999 | /* Analyze Unicode extension key. Currently only 'nu' |
| 3000 | * (numbering system) is supported.*/ |
| 3001 | if ((start[0] == 'n' || start[0] == 'N') && |
| 3002 | (start[1] == 'u' || start[1] == 'U')) { |
| 3003 | state.unicodeState = LocaleParserState::NUMBERING_SYSTEM; |
| 3004 | } else { |
| 3005 | state.unicodeState = LocaleParserState::IGNORE_KEY; |
| 3006 | } |
| 3007 | } else { |
| 3008 | /* Keys are not allowed in other state allowed, ignore the rest. */ |
| 3009 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 3010 | } |
| 3011 | break; |
| 3012 | case 3: |
| 3013 | case 4: |
| 3014 | case 5: |
| 3015 | case 6: |
| 3016 | case 7: |
| 3017 | case 8: |
| 3018 | switch (state.unicodeState) { |
| 3019 | case LocaleParserState::NUMBERING_SYSTEM: |
| 3020 | /* Accept only the first occurrence of the numbering system. */ |
| 3021 | if (config->localeNumberingSystem[0] == '\0') { |
| 3022 | for (size_t i = 0; i < size; ++i) { |
| 3023 | config->localeNumberingSystem[i] = tolower(start[i]); |
| 3024 | } |
| 3025 | state.unicodeState = LocaleParserState::EXPECT_KEY; |
| 3026 | } else { |
| 3027 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 3028 | } |
| 3029 | break; |
| 3030 | case LocaleParserState::IGNORE_KEY: |
| 3031 | /* Unsupported Unicode keyword. Ignore. */ |
| 3032 | state.unicodeState = LocaleParserState::EXPECT_KEY; |
| 3033 | break; |
| 3034 | case LocaleParserState::EXPECT_KEY: |
| 3035 | /* A keyword followed by an attribute is not allowed. */ |
| 3036 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 3037 | break; |
| 3038 | case LocaleParserState::NO_KEY: |
| 3039 | /* Extension attribute. Do nothing. */ |
| 3040 | break; |
| 3041 | default: |
| 3042 | break; |
| 3043 | } |
| 3044 | break; |
| 3045 | default: |
| 3046 | /* Unexpected field length - ignore the rest and treat as an error */ |
| 3047 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 3048 | } |
| 3049 | return state; |
| 3050 | } |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3051 | |
| 3052 | switch (size) { |
| 3053 | case 0: |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3054 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
| 3055 | break; |
| 3056 | case 1: |
| 3057 | state.parserState = (start[0] == 'u' || start[0] == 'U') |
| 3058 | ? LocaleParserState::UNICODE_EXTENSION |
| 3059 | : LocaleParserState::IGNORE_THE_REST; |
| 3060 | break; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3061 | case 2: |
| 3062 | case 3: |
| 3063 | config->language[0] ? config->packRegion(start) : config->packLanguage(start); |
| 3064 | break; |
| 3065 | case 4: |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 3066 | if ('0' <= start[0] && start[0] <= '9') { |
| 3067 | // this is a variant, so fall through |
| 3068 | } else { |
| 3069 | config->localeScript[0] = toupper(start[0]); |
| 3070 | for (size_t i = 1; i < 4; ++i) { |
| 3071 | config->localeScript[i] = tolower(start[i]); |
| 3072 | } |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 3073 | break; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3074 | } |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3075 | case 5: |
| 3076 | case 6: |
| 3077 | case 7: |
| 3078 | case 8: |
| 3079 | for (size_t i = 0; i < size; ++i) { |
| 3080 | config->localeVariant[i] = tolower(start[i]); |
| 3081 | } |
| 3082 | break; |
| 3083 | default: |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3084 | state.parserState = LocaleParserState::IGNORE_THE_REST; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3087 | return state; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | void ResTable_config::setBcp47Locale(const char* in) { |
Igor Viarheichyk | 7ec28a8 | 2017-11-10 11:58:38 -0800 | [diff] [blame] | 3091 | clearLocale(); |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3092 | |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3093 | const char* start = in; |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3094 | LocaleParserState state; |
| 3095 | while (const char* separator = strchr(start, '-')) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3096 | const size_t size = separator - start; |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3097 | state = assignLocaleComponent(this, start, size, state); |
| 3098 | if (state.parserState == LocaleParserState::IGNORE_THE_REST) { |
| 3099 | fprintf(stderr, "Invalid BCP-47 locale string: %s\n", in); |
| 3100 | break; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3101 | } |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3102 | start = (separator + 1); |
| 3103 | } |
| 3104 | |
Igor Viarheichyk | e7bc60a | 2017-10-20 15:09:13 -0700 | [diff] [blame] | 3105 | if (state.parserState != LocaleParserState::IGNORE_THE_REST) { |
| 3106 | const size_t size = strlen(start); |
| 3107 | assignLocaleComponent(this, start, size, state); |
| 3108 | } |
| 3109 | |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 3110 | localeScriptWasComputed = (localeScript[0] == '\0'); |
| 3111 | if (localeScriptWasComputed) { |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 3112 | computeScript(); |
Roozbeh Pournader | 7960898 | 2016-03-03 15:06:46 -0800 | [diff] [blame] | 3113 | } |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3116 | String8 ResTable_config::toString() const { |
| 3117 | String8 res; |
| 3118 | |
| 3119 | if (mcc != 0) { |
| 3120 | if (res.size() > 0) res.append("-"); |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3121 | res.appendFormat("mcc%d", dtohs(mcc)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3122 | } |
| 3123 | if (mnc != 0) { |
| 3124 | if (res.size() > 0) res.append("-"); |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3125 | res.appendFormat("mnc%d", dtohs(mnc)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3126 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3127 | |
Adam Lesinski | 8a9355a | 2015-03-10 16:55:43 -0700 | [diff] [blame] | 3128 | appendDirLocale(res); |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 3129 | |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 3130 | if ((screenLayout&MASK_LAYOUTDIR) != 0) { |
| 3131 | if (res.size() > 0) res.append("-"); |
| 3132 | switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) { |
| 3133 | case ResTable_config::LAYOUTDIR_LTR: |
Fabrice Di Meglio | 8a802db | 2012-09-05 13:12:02 -0700 | [diff] [blame] | 3134 | res.append("ldltr"); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 3135 | break; |
| 3136 | case ResTable_config::LAYOUTDIR_RTL: |
Fabrice Di Meglio | 8a802db | 2012-09-05 13:12:02 -0700 | [diff] [blame] | 3137 | res.append("ldrtl"); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 3138 | break; |
| 3139 | default: |
| 3140 | res.appendFormat("layoutDir=%d", |
| 3141 | dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR)); |
| 3142 | break; |
| 3143 | } |
| 3144 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3145 | if (smallestScreenWidthDp != 0) { |
| 3146 | if (res.size() > 0) res.append("-"); |
| 3147 | res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp)); |
| 3148 | } |
| 3149 | if (screenWidthDp != 0) { |
| 3150 | if (res.size() > 0) res.append("-"); |
| 3151 | res.appendFormat("w%ddp", dtohs(screenWidthDp)); |
| 3152 | } |
| 3153 | if (screenHeightDp != 0) { |
| 3154 | if (res.size() > 0) res.append("-"); |
| 3155 | res.appendFormat("h%ddp", dtohs(screenHeightDp)); |
| 3156 | } |
| 3157 | if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) { |
| 3158 | if (res.size() > 0) res.append("-"); |
| 3159 | switch (screenLayout&ResTable_config::MASK_SCREENSIZE) { |
| 3160 | case ResTable_config::SCREENSIZE_SMALL: |
| 3161 | res.append("small"); |
| 3162 | break; |
| 3163 | case ResTable_config::SCREENSIZE_NORMAL: |
| 3164 | res.append("normal"); |
| 3165 | break; |
| 3166 | case ResTable_config::SCREENSIZE_LARGE: |
| 3167 | res.append("large"); |
| 3168 | break; |
| 3169 | case ResTable_config::SCREENSIZE_XLARGE: |
| 3170 | res.append("xlarge"); |
| 3171 | break; |
| 3172 | default: |
| 3173 | res.appendFormat("screenLayoutSize=%d", |
| 3174 | dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE)); |
| 3175 | break; |
| 3176 | } |
| 3177 | } |
| 3178 | if ((screenLayout&MASK_SCREENLONG) != 0) { |
| 3179 | if (res.size() > 0) res.append("-"); |
| 3180 | switch (screenLayout&ResTable_config::MASK_SCREENLONG) { |
| 3181 | case ResTable_config::SCREENLONG_NO: |
| 3182 | res.append("notlong"); |
| 3183 | break; |
| 3184 | case ResTable_config::SCREENLONG_YES: |
| 3185 | res.append("long"); |
| 3186 | break; |
| 3187 | default: |
| 3188 | res.appendFormat("screenLayoutLong=%d", |
| 3189 | dtohs(screenLayout&ResTable_config::MASK_SCREENLONG)); |
| 3190 | break; |
| 3191 | } |
| 3192 | } |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 3193 | if ((screenLayout2&MASK_SCREENROUND) != 0) { |
| 3194 | if (res.size() > 0) res.append("-"); |
| 3195 | switch (screenLayout2&MASK_SCREENROUND) { |
| 3196 | case SCREENROUND_NO: |
| 3197 | res.append("notround"); |
| 3198 | break; |
| 3199 | case SCREENROUND_YES: |
| 3200 | res.append("round"); |
| 3201 | break; |
| 3202 | default: |
| 3203 | res.appendFormat("screenRound=%d", dtohs(screenLayout2&MASK_SCREENROUND)); |
| 3204 | break; |
| 3205 | } |
| 3206 | } |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3207 | if ((colorMode&MASK_HDR) != 0) { |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3208 | if (res.size() > 0) res.append("-"); |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3209 | switch (colorMode&MASK_HDR) { |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3210 | case ResTable_config::HDR_NO: |
| 3211 | res.append("lowdr"); |
| 3212 | break; |
| 3213 | case ResTable_config::HDR_YES: |
| 3214 | res.append("highdr"); |
| 3215 | break; |
| 3216 | default: |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3217 | res.appendFormat("hdr=%d", dtohs(colorMode&MASK_HDR)); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3218 | break; |
| 3219 | } |
| 3220 | } |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3221 | if ((colorMode&MASK_WIDE_COLOR_GAMUT) != 0) { |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3222 | if (res.size() > 0) res.append("-"); |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3223 | switch (colorMode&MASK_WIDE_COLOR_GAMUT) { |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3224 | case ResTable_config::WIDE_COLOR_GAMUT_NO: |
| 3225 | res.append("nowidecg"); |
| 3226 | break; |
| 3227 | case ResTable_config::WIDE_COLOR_GAMUT_YES: |
| 3228 | res.append("widecg"); |
| 3229 | break; |
| 3230 | default: |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 3231 | res.appendFormat("wideColorGamut=%d", dtohs(colorMode&MASK_WIDE_COLOR_GAMUT)); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 3232 | break; |
| 3233 | } |
| 3234 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3235 | if (orientation != ORIENTATION_ANY) { |
| 3236 | if (res.size() > 0) res.append("-"); |
| 3237 | switch (orientation) { |
| 3238 | case ResTable_config::ORIENTATION_PORT: |
| 3239 | res.append("port"); |
| 3240 | break; |
| 3241 | case ResTable_config::ORIENTATION_LAND: |
| 3242 | res.append("land"); |
| 3243 | break; |
| 3244 | case ResTable_config::ORIENTATION_SQUARE: |
| 3245 | res.append("square"); |
| 3246 | break; |
| 3247 | default: |
| 3248 | res.appendFormat("orientation=%d", dtohs(orientation)); |
| 3249 | break; |
| 3250 | } |
| 3251 | } |
| 3252 | if ((uiMode&MASK_UI_MODE_TYPE) != UI_MODE_TYPE_ANY) { |
| 3253 | if (res.size() > 0) res.append("-"); |
| 3254 | switch (uiMode&ResTable_config::MASK_UI_MODE_TYPE) { |
| 3255 | case ResTable_config::UI_MODE_TYPE_DESK: |
| 3256 | res.append("desk"); |
| 3257 | break; |
| 3258 | case ResTable_config::UI_MODE_TYPE_CAR: |
| 3259 | res.append("car"); |
| 3260 | break; |
| 3261 | case ResTable_config::UI_MODE_TYPE_TELEVISION: |
| 3262 | res.append("television"); |
| 3263 | break; |
| 3264 | case ResTable_config::UI_MODE_TYPE_APPLIANCE: |
| 3265 | res.append("appliance"); |
| 3266 | break; |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 3267 | case ResTable_config::UI_MODE_TYPE_WATCH: |
| 3268 | res.append("watch"); |
| 3269 | break; |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame] | 3270 | case ResTable_config::UI_MODE_TYPE_VR_HEADSET: |
| 3271 | res.append("vrheadset"); |
| 3272 | break; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3273 | default: |
| 3274 | res.appendFormat("uiModeType=%d", |
| 3275 | dtohs(screenLayout&ResTable_config::MASK_UI_MODE_TYPE)); |
| 3276 | break; |
| 3277 | } |
| 3278 | } |
| 3279 | if ((uiMode&MASK_UI_MODE_NIGHT) != 0) { |
| 3280 | if (res.size() > 0) res.append("-"); |
| 3281 | switch (uiMode&ResTable_config::MASK_UI_MODE_NIGHT) { |
| 3282 | case ResTable_config::UI_MODE_NIGHT_NO: |
| 3283 | res.append("notnight"); |
| 3284 | break; |
| 3285 | case ResTable_config::UI_MODE_NIGHT_YES: |
| 3286 | res.append("night"); |
| 3287 | break; |
| 3288 | default: |
| 3289 | res.appendFormat("uiModeNight=%d", |
| 3290 | dtohs(uiMode&MASK_UI_MODE_NIGHT)); |
| 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | if (density != DENSITY_DEFAULT) { |
| 3295 | if (res.size() > 0) res.append("-"); |
| 3296 | switch (density) { |
| 3297 | case ResTable_config::DENSITY_LOW: |
| 3298 | res.append("ldpi"); |
| 3299 | break; |
| 3300 | case ResTable_config::DENSITY_MEDIUM: |
| 3301 | res.append("mdpi"); |
| 3302 | break; |
| 3303 | case ResTable_config::DENSITY_TV: |
| 3304 | res.append("tvdpi"); |
| 3305 | break; |
| 3306 | case ResTable_config::DENSITY_HIGH: |
| 3307 | res.append("hdpi"); |
| 3308 | break; |
| 3309 | case ResTable_config::DENSITY_XHIGH: |
| 3310 | res.append("xhdpi"); |
| 3311 | break; |
| 3312 | case ResTable_config::DENSITY_XXHIGH: |
| 3313 | res.append("xxhdpi"); |
| 3314 | break; |
Adam Lesinski | 8d5667d | 2014-08-13 21:02:57 -0700 | [diff] [blame] | 3315 | case ResTable_config::DENSITY_XXXHIGH: |
| 3316 | res.append("xxxhdpi"); |
| 3317 | break; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3318 | case ResTable_config::DENSITY_NONE: |
| 3319 | res.append("nodpi"); |
| 3320 | break; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 3321 | case ResTable_config::DENSITY_ANY: |
| 3322 | res.append("anydpi"); |
| 3323 | break; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3324 | default: |
Dianne Hackborn | 5c6dfeb | 2012-03-09 13:17:17 -0800 | [diff] [blame] | 3325 | res.appendFormat("%ddpi", dtohs(density)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3326 | break; |
| 3327 | } |
| 3328 | } |
| 3329 | if (touchscreen != TOUCHSCREEN_ANY) { |
| 3330 | if (res.size() > 0) res.append("-"); |
| 3331 | switch (touchscreen) { |
| 3332 | case ResTable_config::TOUCHSCREEN_NOTOUCH: |
| 3333 | res.append("notouch"); |
| 3334 | break; |
| 3335 | case ResTable_config::TOUCHSCREEN_FINGER: |
| 3336 | res.append("finger"); |
| 3337 | break; |
| 3338 | case ResTable_config::TOUCHSCREEN_STYLUS: |
| 3339 | res.append("stylus"); |
| 3340 | break; |
| 3341 | default: |
| 3342 | res.appendFormat("touchscreen=%d", dtohs(touchscreen)); |
| 3343 | break; |
| 3344 | } |
| 3345 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3346 | if ((inputFlags&MASK_KEYSHIDDEN) != 0) { |
| 3347 | if (res.size() > 0) res.append("-"); |
| 3348 | switch (inputFlags&MASK_KEYSHIDDEN) { |
| 3349 | case ResTable_config::KEYSHIDDEN_NO: |
| 3350 | res.append("keysexposed"); |
| 3351 | break; |
| 3352 | case ResTable_config::KEYSHIDDEN_YES: |
| 3353 | res.append("keyshidden"); |
| 3354 | break; |
| 3355 | case ResTable_config::KEYSHIDDEN_SOFT: |
| 3356 | res.append("keyssoft"); |
| 3357 | break; |
| 3358 | } |
| 3359 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3360 | if (keyboard != KEYBOARD_ANY) { |
| 3361 | if (res.size() > 0) res.append("-"); |
| 3362 | switch (keyboard) { |
| 3363 | case ResTable_config::KEYBOARD_NOKEYS: |
| 3364 | res.append("nokeys"); |
| 3365 | break; |
| 3366 | case ResTable_config::KEYBOARD_QWERTY: |
| 3367 | res.append("qwerty"); |
| 3368 | break; |
| 3369 | case ResTable_config::KEYBOARD_12KEY: |
| 3370 | res.append("12key"); |
| 3371 | break; |
| 3372 | default: |
| 3373 | res.appendFormat("keyboard=%d", dtohs(keyboard)); |
| 3374 | break; |
| 3375 | } |
| 3376 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3377 | if ((inputFlags&MASK_NAVHIDDEN) != 0) { |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3378 | if (res.size() > 0) res.append("-"); |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3379 | switch (inputFlags&MASK_NAVHIDDEN) { |
| 3380 | case ResTable_config::NAVHIDDEN_NO: |
| 3381 | res.append("navexposed"); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3382 | break; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3383 | case ResTable_config::NAVHIDDEN_YES: |
| 3384 | res.append("navhidden"); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3385 | break; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3386 | default: |
| 3387 | res.appendFormat("inputFlagsNavHidden=%d", |
| 3388 | dtohs(inputFlags&MASK_NAVHIDDEN)); |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3389 | break; |
| 3390 | } |
| 3391 | } |
| 3392 | if (navigation != NAVIGATION_ANY) { |
| 3393 | if (res.size() > 0) res.append("-"); |
| 3394 | switch (navigation) { |
| 3395 | case ResTable_config::NAVIGATION_NONAV: |
| 3396 | res.append("nonav"); |
| 3397 | break; |
| 3398 | case ResTable_config::NAVIGATION_DPAD: |
| 3399 | res.append("dpad"); |
| 3400 | break; |
| 3401 | case ResTable_config::NAVIGATION_TRACKBALL: |
| 3402 | res.append("trackball"); |
| 3403 | break; |
| 3404 | case ResTable_config::NAVIGATION_WHEEL: |
| 3405 | res.append("wheel"); |
| 3406 | break; |
| 3407 | default: |
| 3408 | res.appendFormat("navigation=%d", dtohs(navigation)); |
| 3409 | break; |
| 3410 | } |
| 3411 | } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 3412 | if (screenSize != 0) { |
| 3413 | if (res.size() > 0) res.append("-"); |
| 3414 | res.appendFormat("%dx%d", dtohs(screenWidth), dtohs(screenHeight)); |
| 3415 | } |
| 3416 | if (version != 0) { |
| 3417 | if (res.size() > 0) res.append("-"); |
| 3418 | res.appendFormat("v%d", dtohs(sdkVersion)); |
| 3419 | if (minorVersion != 0) { |
| 3420 | res.appendFormat(".%d", dtohs(minorVersion)); |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | return res; |
| 3425 | } |
| 3426 | |
| 3427 | // -------------------------------------------------------------------- |
| 3428 | // -------------------------------------------------------------------- |
| 3429 | // -------------------------------------------------------------------- |
| 3430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3431 | struct ResTable::Header |
| 3432 | { |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 3433 | explicit Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL), |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3434 | resourceIDMap(NULL), resourceIDMapSize(0) { } |
| 3435 | |
| 3436 | ~Header() |
| 3437 | { |
| 3438 | free(resourceIDMap); |
| 3439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3440 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3441 | const ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | void* ownedData; |
| 3443 | const ResTable_header* header; |
| 3444 | size_t size; |
| 3445 | const uint8_t* dataEnd; |
| 3446 | size_t index; |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 3447 | int32_t cookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | |
| 3449 | ResStringPool values; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 3450 | uint32_t* resourceIDMap; |
| 3451 | size_t resourceIDMapSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | }; |
| 3453 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3454 | struct ResTable::Entry { |
| 3455 | ResTable_config config; |
| 3456 | const ResTable_entry* entry; |
| 3457 | const ResTable_type* type; |
| 3458 | uint32_t specFlags; |
| 3459 | const Package* package; |
| 3460 | |
| 3461 | StringPoolRef typeStr; |
| 3462 | StringPoolRef keyStr; |
| 3463 | }; |
| 3464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3465 | struct ResTable::Type |
| 3466 | { |
| 3467 | Type(const Header* _header, const Package* _package, size_t count) |
| 3468 | : header(_header), package(_package), entryCount(count), |
| 3469 | typeSpec(NULL), typeSpecFlags(NULL) { } |
| 3470 | const Header* const header; |
| 3471 | const Package* const package; |
| 3472 | const size_t entryCount; |
| 3473 | const ResTable_typeSpec* typeSpec; |
| 3474 | const uint32_t* typeSpecFlags; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3475 | IdmapEntries idmapEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | Vector<const ResTable_type*> configs; |
| 3477 | }; |
| 3478 | |
| 3479 | struct ResTable::Package |
| 3480 | { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3481 | Package(ResTable* _owner, const Header* _header, const ResTable_package* _package) |
Adam Lesinski | 1856088 | 2014-08-15 17:18:21 +0000 | [diff] [blame] | 3482 | : owner(_owner), header(_header), package(_package), typeIdOffset(0) { |
Wan He | 3ff4226 | 2016-11-17 17:49:37 +0800 | [diff] [blame] | 3483 | if (dtohs(package->header.headerSize) == sizeof(*package)) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3484 | // The package structure is the same size as the definition. |
| 3485 | // This means it contains the typeIdOffset field. |
Adam Lesinski | 1856088 | 2014-08-15 17:18:21 +0000 | [diff] [blame] | 3486 | typeIdOffset = package->typeIdOffset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3487 | } |
| 3488 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3489 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3490 | const ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | const Header* const header; |
Adam Lesinski | 1856088 | 2014-08-15 17:18:21 +0000 | [diff] [blame] | 3492 | const ResTable_package* const package; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3493 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3494 | ResStringPool typeStrings; |
| 3495 | ResStringPool keyStrings; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3496 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3497 | size_t typeIdOffset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | }; |
| 3499 | |
| 3500 | // A group of objects describing a particular resource package. |
| 3501 | // The first in 'package' is always the root object (from the resource |
| 3502 | // table that defined the package); the ones after are skins on top of it. |
| 3503 | struct ResTable::PackageGroup |
| 3504 | { |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 3505 | PackageGroup( |
| 3506 | ResTable* _owner, const String16& _name, uint32_t _id, |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 3507 | bool appAsLib, bool _isSystemAsset, bool _isDynamic) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3508 | : owner(_owner) |
| 3509 | , name(_name) |
| 3510 | , id(_id) |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3511 | , largestTypeId(0) |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 3512 | , dynamicRefTable(static_cast<uint8_t>(_id), appAsLib) |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 3513 | , isSystemAsset(_isSystemAsset) |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 3514 | , isDynamic(_isDynamic) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3515 | { } |
| 3516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3517 | ~PackageGroup() { |
| 3518 | clearBagCache(); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3519 | const size_t numTypes = types.size(); |
| 3520 | for (size_t i = 0; i < numTypes; i++) { |
Sean Lu | 83df842 | 2017-06-26 18:19:28 +0800 | [diff] [blame] | 3521 | TypeList& typeList = types.editItemAt(i); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3522 | const size_t numInnerTypes = typeList.size(); |
| 3523 | for (size_t j = 0; j < numInnerTypes; j++) { |
| 3524 | if (typeList[j]->package->owner == owner) { |
| 3525 | delete typeList[j]; |
| 3526 | } |
| 3527 | } |
Sean Lu | 83df842 | 2017-06-26 18:19:28 +0800 | [diff] [blame] | 3528 | typeList.clear(); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | const size_t N = packages.size(); |
| 3532 | for (size_t i=0; i<N; i++) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 3533 | Package* pkg = packages[i]; |
| 3534 | if (pkg->owner == owner) { |
| 3535 | delete pkg; |
| 3536 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3537 | } |
| 3538 | } |
| 3539 | |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3540 | /** |
| 3541 | * Clear all cache related data that depends on parameters/configuration. |
| 3542 | * This includes the bag caches and filtered types. |
| 3543 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3544 | void clearBagCache() { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3545 | for (size_t i = 0; i < typeCacheEntries.size(); i++) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3546 | if (kDebugTableNoisy) { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3547 | printf("type=%zu\n", i); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3548 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3549 | const TypeList& typeList = types[i]; |
| 3550 | if (!typeList.isEmpty()) { |
| 3551 | TypeCacheEntry& cacheEntry = typeCacheEntries.editItemAt(i); |
| 3552 | |
| 3553 | // Reset the filtered configurations. |
| 3554 | cacheEntry.filteredConfigs.clear(); |
| 3555 | |
| 3556 | bag_set** typeBags = cacheEntry.cachedBags; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3557 | if (kDebugTableNoisy) { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3558 | printf("typeBags=%p\n", typeBags); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3559 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3560 | |
| 3561 | if (typeBags) { |
| 3562 | const size_t N = typeList[0]->entryCount; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3563 | if (kDebugTableNoisy) { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3564 | printf("type->entryCount=%zu\n", N); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3565 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3566 | for (size_t j = 0; j < N; j++) { |
| 3567 | if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF) { |
| 3568 | free(typeBags[j]); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3569 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3571 | free(typeBags); |
| 3572 | cacheEntry.cachedBags = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3573 | } |
| 3574 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3575 | } |
| 3576 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3577 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3578 | ssize_t findType16(const char16_t* type, size_t len) const { |
| 3579 | const size_t N = packages.size(); |
| 3580 | for (size_t i = 0; i < N; i++) { |
| 3581 | ssize_t index = packages[i]->typeStrings.indexOfString(type, len); |
| 3582 | if (index >= 0) { |
| 3583 | return index + packages[i]->typeIdOffset; |
| 3584 | } |
| 3585 | } |
| 3586 | return -1; |
| 3587 | } |
| 3588 | |
| 3589 | const ResTable* const owner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | String16 const name; |
| 3591 | uint32_t const id; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3592 | |
| 3593 | // This is mainly used to keep track of the loaded packages |
| 3594 | // and to clean them up properly. Accessing resources happens from |
| 3595 | // the 'types' array. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3596 | Vector<Package*> packages; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3597 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3598 | ByteBucketArray<TypeList> types; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3600 | uint8_t largestTypeId; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3601 | |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 3602 | // Cached objects dependent on the parameters/configuration of this ResTable. |
| 3603 | // Gets cleared whenever the parameters/configuration changes. |
| 3604 | // These are stored here in a parallel structure because the data in `types` may |
| 3605 | // be shared by other ResTable's (framework resources are shared this way). |
| 3606 | ByteBucketArray<TypeCacheEntry> typeCacheEntries; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3607 | |
| 3608 | // The table mapping dynamic references to resolved references for |
| 3609 | // this package group. |
| 3610 | // TODO: We may be able to support dynamic references in overlays |
| 3611 | // by having these tables in a per-package scope rather than |
| 3612 | // per-package-group. |
| 3613 | DynamicRefTable dynamicRefTable; |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 3614 | |
| 3615 | // If the package group comes from a system asset. Used in |
| 3616 | // determining non-system locales. |
| 3617 | const bool isSystemAsset; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 3618 | const bool isDynamic; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3619 | }; |
| 3620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3621 | ResTable::Theme::Theme(const ResTable& table) |
| 3622 | : mTable(table) |
Alan Viverette | c1d5279 | 2015-05-05 09:49:03 -0700 | [diff] [blame] | 3623 | , mTypeSpecFlags(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3624 | { |
| 3625 | memset(mPackages, 0, sizeof(mPackages)); |
| 3626 | } |
| 3627 | |
| 3628 | ResTable::Theme::~Theme() |
| 3629 | { |
| 3630 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 3631 | package_info* pi = mPackages[i]; |
| 3632 | if (pi != NULL) { |
| 3633 | free_package(pi); |
| 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | void ResTable::Theme::free_package(package_info* pi) |
| 3639 | { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3640 | for (size_t j = 0; j <= Res_MAXTYPE; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3641 | theme_entry* te = pi->types[j].entries; |
| 3642 | if (te != NULL) { |
| 3643 | free(te); |
| 3644 | } |
| 3645 | } |
| 3646 | free(pi); |
| 3647 | } |
| 3648 | |
| 3649 | ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi) |
| 3650 | { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3651 | package_info* newpi = (package_info*)malloc(sizeof(package_info)); |
| 3652 | for (size_t j = 0; j <= Res_MAXTYPE; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3653 | size_t cnt = pi->types[j].numEntries; |
| 3654 | newpi->types[j].numEntries = cnt; |
| 3655 | theme_entry* te = pi->types[j].entries; |
Vishwath Mohan | 6a2c23d | 2015-03-09 18:55:11 -0700 | [diff] [blame] | 3656 | size_t cnt_max = SIZE_MAX / sizeof(theme_entry); |
| 3657 | if (te != NULL && (cnt < 0xFFFFFFFF-1) && (cnt < cnt_max)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3658 | theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry)); |
| 3659 | newpi->types[j].entries = newte; |
| 3660 | memcpy(newte, te, cnt*sizeof(theme_entry)); |
| 3661 | } else { |
| 3662 | newpi->types[j].entries = NULL; |
| 3663 | } |
| 3664 | } |
| 3665 | return newpi; |
| 3666 | } |
| 3667 | |
| 3668 | status_t ResTable::Theme::applyStyle(uint32_t resID, bool force) |
| 3669 | { |
| 3670 | const bag_entry* bag; |
| 3671 | uint32_t bagTypeSpecFlags = 0; |
| 3672 | mTable.lock(); |
| 3673 | const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3674 | if (kDebugTableNoisy) { |
| 3675 | ALOGV("Applying style 0x%08x to theme %p, count=%zu", resID, this, N); |
| 3676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3677 | if (N < 0) { |
| 3678 | mTable.unlock(); |
| 3679 | return N; |
| 3680 | } |
| 3681 | |
Alan Viverette | c1d5279 | 2015-05-05 09:49:03 -0700 | [diff] [blame] | 3682 | mTypeSpecFlags |= bagTypeSpecFlags; |
| 3683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3684 | uint32_t curPackage = 0xffffffff; |
| 3685 | ssize_t curPackageIndex = 0; |
| 3686 | package_info* curPI = NULL; |
| 3687 | uint32_t curType = 0xffffffff; |
| 3688 | size_t numEntries = 0; |
| 3689 | theme_entry* curEntries = NULL; |
| 3690 | |
| 3691 | const bag_entry* end = bag + N; |
| 3692 | while (bag < end) { |
| 3693 | const uint32_t attrRes = bag->map.name.ident; |
| 3694 | const uint32_t p = Res_GETPACKAGE(attrRes); |
| 3695 | const uint32_t t = Res_GETTYPE(attrRes); |
| 3696 | const uint32_t e = Res_GETENTRY(attrRes); |
| 3697 | |
| 3698 | if (curPackage != p) { |
| 3699 | const ssize_t pidx = mTable.getResourcePackageIndex(attrRes); |
| 3700 | if (pidx < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3701 | 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] | 3702 | bag++; |
| 3703 | continue; |
| 3704 | } |
| 3705 | curPackage = p; |
| 3706 | curPackageIndex = pidx; |
| 3707 | curPI = mPackages[pidx]; |
| 3708 | if (curPI == NULL) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3709 | curPI = (package_info*)malloc(sizeof(package_info)); |
| 3710 | memset(curPI, 0, sizeof(*curPI)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3711 | mPackages[pidx] = curPI; |
| 3712 | } |
| 3713 | curType = 0xffffffff; |
| 3714 | } |
| 3715 | if (curType != t) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3716 | if (t > Res_MAXTYPE) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3717 | 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] | 3718 | bag++; |
| 3719 | continue; |
| 3720 | } |
| 3721 | curType = t; |
| 3722 | curEntries = curPI->types[t].entries; |
| 3723 | if (curEntries == NULL) { |
| 3724 | PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex]; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3725 | const TypeList& typeList = grp->types[t]; |
Vishwath Mohan | 6a2c23d | 2015-03-09 18:55:11 -0700 | [diff] [blame] | 3726 | size_t cnt = typeList.isEmpty() ? 0 : typeList[0]->entryCount; |
| 3727 | size_t cnt_max = SIZE_MAX / sizeof(theme_entry); |
| 3728 | size_t buff_size = (cnt < cnt_max && cnt < 0xFFFFFFFF-1) ? |
| 3729 | cnt*sizeof(theme_entry) : 0; |
| 3730 | curEntries = (theme_entry*)malloc(buff_size); |
| 3731 | memset(curEntries, Res_value::TYPE_NULL, buff_size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3732 | curPI->types[t].numEntries = cnt; |
| 3733 | curPI->types[t].entries = curEntries; |
| 3734 | } |
| 3735 | numEntries = curPI->types[t].numEntries; |
| 3736 | } |
| 3737 | if (e >= numEntries) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3738 | 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] | 3739 | bag++; |
| 3740 | continue; |
| 3741 | } |
| 3742 | theme_entry* curEntry = curEntries + e; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3743 | if (kDebugTableNoisy) { |
| 3744 | ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x", |
| 3745 | attrRes, bag->map.value.dataType, bag->map.value.data, |
| 3746 | curEntry->value.dataType); |
| 3747 | } |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 3748 | if (force || (curEntry->value.dataType == Res_value::TYPE_NULL |
| 3749 | && curEntry->value.data != Res_value::DATA_NULL_EMPTY)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3750 | curEntry->stringBlock = bag->stringBlock; |
| 3751 | curEntry->typeSpecFlags |= bagTypeSpecFlags; |
| 3752 | curEntry->value = bag->map.value; |
| 3753 | } |
| 3754 | |
| 3755 | bag++; |
| 3756 | } |
| 3757 | |
| 3758 | mTable.unlock(); |
| 3759 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3760 | if (kDebugTableTheme) { |
| 3761 | ALOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this); |
| 3762 | dumpToLog(); |
| 3763 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3765 | return NO_ERROR; |
| 3766 | } |
| 3767 | |
| 3768 | status_t ResTable::Theme::setTo(const Theme& other) |
| 3769 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3770 | if (kDebugTableTheme) { |
| 3771 | ALOGI("Setting theme %p from theme %p...\n", this, &other); |
| 3772 | dumpToLog(); |
| 3773 | other.dumpToLog(); |
| 3774 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | if (&mTable == &other.mTable) { |
| 3777 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 3778 | if (mPackages[i] != NULL) { |
| 3779 | free_package(mPackages[i]); |
| 3780 | } |
| 3781 | if (other.mPackages[i] != NULL) { |
| 3782 | mPackages[i] = copy_package(other.mPackages[i]); |
| 3783 | } else { |
| 3784 | mPackages[i] = NULL; |
| 3785 | } |
| 3786 | } |
| 3787 | } else { |
| 3788 | // @todo: need to really implement this, not just copy |
| 3789 | // the system package (which is still wrong because it isn't |
| 3790 | // fixing up resource references). |
| 3791 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 3792 | if (mPackages[i] != NULL) { |
| 3793 | free_package(mPackages[i]); |
| 3794 | } |
| 3795 | if (i == 0 && other.mPackages[i] != NULL) { |
| 3796 | mPackages[i] = copy_package(other.mPackages[i]); |
| 3797 | } else { |
| 3798 | mPackages[i] = NULL; |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | |
Alan Viverette | c1d5279 | 2015-05-05 09:49:03 -0700 | [diff] [blame] | 3803 | mTypeSpecFlags = other.mTypeSpecFlags; |
| 3804 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3805 | if (kDebugTableTheme) { |
| 3806 | ALOGI("Final theme:"); |
| 3807 | dumpToLog(); |
| 3808 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3810 | return NO_ERROR; |
| 3811 | } |
| 3812 | |
Alan Viverette | e54d245 | 2015-05-06 10:41:43 -0700 | [diff] [blame] | 3813 | status_t ResTable::Theme::clear() |
| 3814 | { |
| 3815 | if (kDebugTableTheme) { |
| 3816 | ALOGI("Clearing theme %p...\n", this); |
| 3817 | dumpToLog(); |
| 3818 | } |
| 3819 | |
| 3820 | for (size_t i = 0; i < Res_MAXPACKAGE; i++) { |
| 3821 | if (mPackages[i] != NULL) { |
| 3822 | free_package(mPackages[i]); |
| 3823 | mPackages[i] = NULL; |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | mTypeSpecFlags = 0; |
| 3828 | |
| 3829 | if (kDebugTableTheme) { |
| 3830 | ALOGI("Final theme:"); |
| 3831 | dumpToLog(); |
| 3832 | } |
| 3833 | |
| 3834 | return NO_ERROR; |
| 3835 | } |
| 3836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3837 | ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue, |
| 3838 | uint32_t* outTypeSpecFlags) const |
| 3839 | { |
| 3840 | int cnt = 20; |
| 3841 | |
| 3842 | if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3844 | do { |
| 3845 | const ssize_t p = mTable.getResourcePackageIndex(resID); |
| 3846 | const uint32_t t = Res_GETTYPE(resID); |
| 3847 | const uint32_t e = Res_GETENTRY(resID); |
| 3848 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3849 | if (kDebugTableTheme) { |
| 3850 | ALOGI("Looking up attr 0x%08x in theme %p", resID, this); |
| 3851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3852 | |
| 3853 | if (p >= 0) { |
| 3854 | const package_info* const pi = mPackages[p]; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3855 | if (kDebugTableTheme) { |
| 3856 | ALOGI("Found package: %p", pi); |
| 3857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3858 | if (pi != NULL) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3859 | if (kDebugTableTheme) { |
John Reck | f6113af | 2016-11-03 16:16:47 -0700 | [diff] [blame] | 3860 | ALOGI("Desired type index is %u in avail %zu", t, Res_MAXTYPE + 1); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3861 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3862 | if (t <= Res_MAXTYPE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | const type_info& ti = pi->types[t]; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3864 | if (kDebugTableTheme) { |
| 3865 | ALOGI("Desired entry index is %u in avail %zu", e, ti.numEntries); |
| 3866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | if (e < ti.numEntries) { |
| 3868 | const theme_entry& te = ti.entries[e]; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 3869 | if (outTypeSpecFlags != NULL) { |
| 3870 | *outTypeSpecFlags |= te.typeSpecFlags; |
| 3871 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3872 | if (kDebugTableTheme) { |
| 3873 | ALOGI("Theme value: type=0x%x, data=0x%08x", |
| 3874 | te.value.dataType, te.value.data); |
| 3875 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | const uint8_t type = te.value.dataType; |
| 3877 | if (type == Res_value::TYPE_ATTRIBUTE) { |
| 3878 | if (cnt > 0) { |
| 3879 | cnt--; |
| 3880 | resID = te.value.data; |
| 3881 | continue; |
| 3882 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3883 | 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] | 3884 | return BAD_INDEX; |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 3885 | } else if (type != Res_value::TYPE_NULL |
| 3886 | || te.value.data == Res_value::DATA_NULL_EMPTY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3887 | *outValue = te.value; |
| 3888 | return te.stringBlock; |
| 3889 | } |
| 3890 | return BAD_INDEX; |
| 3891 | } |
| 3892 | } |
| 3893 | } |
| 3894 | } |
| 3895 | break; |
| 3896 | |
| 3897 | } while (true); |
| 3898 | |
| 3899 | return BAD_INDEX; |
| 3900 | } |
| 3901 | |
| 3902 | ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue, |
| 3903 | ssize_t blockIndex, uint32_t* outLastRef, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 3904 | uint32_t* inoutTypeSpecFlags, ResTable_config* inoutConfig) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | { |
| 3906 | //printf("Resolving type=0x%x\n", inOutValue->dataType); |
| 3907 | if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 3908 | uint32_t newTypeSpecFlags; |
| 3909 | blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3910 | if (kDebugTableTheme) { |
| 3911 | ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=0x%x\n", |
| 3912 | (int)blockIndex, (int)inOutValue->dataType, inOutValue->data); |
| 3913 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3914 | if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags; |
| 3915 | //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType); |
| 3916 | if (blockIndex < 0) { |
| 3917 | return blockIndex; |
| 3918 | } |
| 3919 | } |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 3920 | return mTable.resolveReference(inOutValue, blockIndex, outLastRef, |
| 3921 | inoutTypeSpecFlags, inoutConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3922 | } |
| 3923 | |
Alan Viverette | c1d5279 | 2015-05-05 09:49:03 -0700 | [diff] [blame] | 3924 | uint32_t ResTable::Theme::getChangingConfigurations() const |
| 3925 | { |
| 3926 | return mTypeSpecFlags; |
| 3927 | } |
| 3928 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3929 | void ResTable::Theme::dumpToLog() const |
| 3930 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 3931 | ALOGI("Theme %p:\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | for (size_t i=0; i<Res_MAXPACKAGE; i++) { |
| 3933 | package_info* pi = mPackages[i]; |
| 3934 | if (pi == NULL) continue; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 3935 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3936 | ALOGI(" Package #0x%02x:\n", (int)(i + 1)); |
| 3937 | for (size_t j = 0; j <= Res_MAXTYPE; j++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3938 | type_info& ti = pi->types[j]; |
| 3939 | if (ti.numEntries == 0) continue; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3940 | ALOGI(" Type #0x%02x:\n", (int)(j + 1)); |
| 3941 | for (size_t k = 0; k < ti.numEntries; k++) { |
| 3942 | const theme_entry& te = ti.entries[k]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3943 | if (te.value.dataType == Res_value::TYPE_NULL) continue; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 3944 | 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] | 3945 | (int)Res_MAKEID(i, j, k), |
| 3946 | te.value.dataType, (int)te.value.data, (int)te.stringBlock); |
| 3947 | } |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | ResTable::ResTable() |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3953 | : mError(NO_INIT), mNextPackageId(2) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3954 | { |
| 3955 | memset(&mParams, 0, sizeof(mParams)); |
| 3956 | memset(mPackageMap, 0, sizeof(mPackageMap)); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3957 | if (kDebugTableSuperNoisy) { |
| 3958 | ALOGI("Creating ResTable %p\n", this); |
| 3959 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3960 | } |
| 3961 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 3962 | ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3963 | : mError(NO_INIT), mNextPackageId(2) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | { |
| 3965 | memset(&mParams, 0, sizeof(mParams)); |
| 3966 | memset(mPackageMap, 0, sizeof(mPackageMap)); |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 3967 | addInternal(data, size, NULL, 0, false, cookie, copyData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3968 | LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table"); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3969 | if (kDebugTableSuperNoisy) { |
| 3970 | ALOGI("Creating ResTable %p\n", this); |
| 3971 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | ResTable::~ResTable() |
| 3975 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 3976 | if (kDebugTableSuperNoisy) { |
| 3977 | ALOGI("Destroying ResTable in %p\n", this); |
| 3978 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3979 | uninit(); |
| 3980 | } |
| 3981 | |
| 3982 | inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const |
| 3983 | { |
| 3984 | return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1; |
| 3985 | } |
| 3986 | |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 3987 | inline ssize_t ResTable::getResourcePackageIndexFromPackage(uint8_t packageID) const |
| 3988 | { |
| 3989 | return ((ssize_t)mPackageMap[packageID])-1; |
| 3990 | } |
| 3991 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3992 | status_t ResTable::add(const void* data, size_t size, const int32_t cookie, bool copyData) { |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 3993 | return addInternal(data, size, NULL, 0, false, cookie, copyData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3994 | } |
| 3995 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3996 | status_t ResTable::add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize, |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 3997 | const int32_t cookie, bool copyData, bool appAsLib) { |
| 3998 | return addInternal(data, size, idmapData, idmapDataSize, appAsLib, cookie, copyData); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | status_t ResTable::add(Asset* asset, const int32_t cookie, bool copyData) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4002 | const void* data = asset->getBuffer(true); |
| 4003 | if (data == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4004 | ALOGW("Unable to get buffer of resource asset file"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4005 | return UNKNOWN_ERROR; |
| 4006 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4007 | |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 4008 | return addInternal(data, static_cast<size_t>(asset->getLength()), NULL, false, 0, cookie, |
| 4009 | copyData); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4012 | status_t ResTable::add( |
| 4013 | Asset* asset, Asset* idmapAsset, const int32_t cookie, bool copyData, |
| 4014 | bool appAsLib, bool isSystemAsset) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4015 | const void* data = asset->getBuffer(true); |
| 4016 | if (data == NULL) { |
| 4017 | ALOGW("Unable to get buffer of resource asset file"); |
| 4018 | return UNKNOWN_ERROR; |
| 4019 | } |
| 4020 | |
| 4021 | size_t idmapSize = 0; |
| 4022 | const void* idmapData = NULL; |
| 4023 | if (idmapAsset != NULL) { |
| 4024 | idmapData = idmapAsset->getBuffer(true); |
| 4025 | if (idmapData == NULL) { |
| 4026 | ALOGW("Unable to get buffer of idmap asset file"); |
| 4027 | return UNKNOWN_ERROR; |
| 4028 | } |
| 4029 | idmapSize = static_cast<size_t>(idmapAsset->getLength()); |
| 4030 | } |
| 4031 | |
| 4032 | return addInternal(data, static_cast<size_t>(asset->getLength()), |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4033 | idmapData, idmapSize, appAsLib, cookie, copyData, isSystemAsset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4034 | } |
| 4035 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4036 | status_t ResTable::add(ResTable* src, bool isSystemAsset) |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4037 | { |
| 4038 | mError = src->mError; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4039 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4040 | for (size_t i=0; i < src->mHeaders.size(); i++) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4041 | mHeaders.add(src->mHeaders[i]); |
| 4042 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4043 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4044 | for (size_t i=0; i < src->mPackageGroups.size(); i++) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4045 | PackageGroup* srcPg = src->mPackageGroups[i]; |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4046 | PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id, |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 4047 | false /* appAsLib */, isSystemAsset || srcPg->isSystemAsset, srcPg->isDynamic); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4048 | for (size_t j=0; j<srcPg->packages.size(); j++) { |
| 4049 | pg->packages.add(srcPg->packages[j]); |
| 4050 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4051 | |
| 4052 | for (size_t j = 0; j < srcPg->types.size(); j++) { |
| 4053 | if (srcPg->types[j].isEmpty()) { |
| 4054 | continue; |
| 4055 | } |
| 4056 | |
| 4057 | TypeList& typeList = pg->types.editItemAt(j); |
| 4058 | typeList.appendVector(srcPg->types[j]); |
| 4059 | } |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 4060 | pg->dynamicRefTable.addMappings(srcPg->dynamicRefTable); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4061 | pg->largestTypeId = max(pg->largestTypeId, srcPg->largestTypeId); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4062 | mPackageGroups.add(pg); |
| 4063 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4064 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4065 | memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap)); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4066 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4067 | return mError; |
| 4068 | } |
| 4069 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 4070 | status_t ResTable::addEmpty(const int32_t cookie) { |
| 4071 | Header* header = new Header(this); |
| 4072 | header->index = mHeaders.size(); |
| 4073 | header->cookie = cookie; |
| 4074 | header->values.setToEmpty(); |
| 4075 | header->ownedData = calloc(1, sizeof(ResTable_header)); |
| 4076 | |
| 4077 | ResTable_header* resHeader = (ResTable_header*) header->ownedData; |
| 4078 | resHeader->header.type = RES_TABLE_TYPE; |
| 4079 | resHeader->header.headerSize = sizeof(ResTable_header); |
| 4080 | resHeader->header.size = sizeof(ResTable_header); |
| 4081 | |
| 4082 | header->header = (const ResTable_header*) resHeader; |
| 4083 | mHeaders.add(header); |
Adam Lesinski | 961dda7 | 2014-06-09 17:10:29 -0700 | [diff] [blame] | 4084 | return (mError=NO_ERROR); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 4085 | } |
| 4086 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4087 | status_t ResTable::addInternal(const void* data, size_t dataSize, const void* idmapData, size_t idmapDataSize, |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4088 | bool appAsLib, const int32_t cookie, bool copyData, bool isSystemAsset) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4089 | { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4090 | if (!data) { |
| 4091 | return NO_ERROR; |
| 4092 | } |
| 4093 | |
Adam Lesinski | f28d505 | 2014-07-25 15:25:04 -0700 | [diff] [blame] | 4094 | if (dataSize < sizeof(ResTable_header)) { |
| 4095 | ALOGE("Invalid data. Size(%d) is smaller than a ResTable_header(%d).", |
| 4096 | (int) dataSize, (int) sizeof(ResTable_header)); |
| 4097 | return UNKNOWN_ERROR; |
| 4098 | } |
| 4099 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4100 | Header* header = new Header(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4101 | header->index = mHeaders.size(); |
| 4102 | header->cookie = cookie; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4103 | if (idmapData != NULL) { |
| 4104 | header->resourceIDMap = (uint32_t*) malloc(idmapDataSize); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4105 | if (header->resourceIDMap == NULL) { |
| 4106 | delete header; |
| 4107 | return (mError = NO_MEMORY); |
| 4108 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4109 | memcpy(header->resourceIDMap, idmapData, idmapDataSize); |
| 4110 | header->resourceIDMapSize = idmapDataSize; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4112 | mHeaders.add(header); |
| 4113 | |
| 4114 | const bool notDeviceEndian = htods(0xf0) != 0xf0; |
| 4115 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4116 | if (kDebugLoadTableNoisy) { |
| 4117 | ALOGV("Adding resources to ResTable: data=%p, size=%zu, cookie=%d, copy=%d " |
| 4118 | "idmap=%p\n", data, dataSize, cookie, copyData, idmapData); |
| 4119 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4121 | if (copyData || notDeviceEndian) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4122 | header->ownedData = malloc(dataSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4123 | if (header->ownedData == NULL) { |
| 4124 | return (mError=NO_MEMORY); |
| 4125 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4126 | memcpy(header->ownedData, data, dataSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | data = header->ownedData; |
| 4128 | } |
| 4129 | |
| 4130 | header->header = (const ResTable_header*)data; |
| 4131 | header->size = dtohl(header->header->header.size); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4132 | if (kDebugLoadTableSuperNoisy) { |
| 4133 | ALOGI("Got size %zu, again size 0x%x, raw size 0x%x\n", header->size, |
| 4134 | dtohl(header->header->header.size), header->header->header.size); |
| 4135 | } |
| 4136 | if (kDebugLoadTableNoisy) { |
| 4137 | ALOGV("Loading ResTable @%p:\n", header->header); |
| 4138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4139 | if (dtohs(header->header->header.headerSize) > header->size |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4140 | || header->size > dataSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4141 | 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] | 4142 | (int)dtohs(header->header->header.headerSize), |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4143 | (int)header->size, (int)dataSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4144 | return (mError=BAD_TYPE); |
| 4145 | } |
| 4146 | if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4147 | 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] | 4148 | (int)dtohs(header->header->header.headerSize), |
| 4149 | (int)header->size); |
| 4150 | return (mError=BAD_TYPE); |
| 4151 | } |
| 4152 | header->dataEnd = ((const uint8_t*)header->header) + header->size; |
| 4153 | |
| 4154 | // Iterate through all chunks. |
| 4155 | size_t curPackage = 0; |
| 4156 | |
| 4157 | const ResChunk_header* chunk = |
| 4158 | (const ResChunk_header*)(((const uint8_t*)header->header) |
| 4159 | + dtohs(header->header->header.headerSize)); |
| 4160 | while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) && |
| 4161 | ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) { |
| 4162 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable"); |
| 4163 | if (err != NO_ERROR) { |
| 4164 | return (mError=err); |
| 4165 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4166 | if (kDebugTableNoisy) { |
| 4167 | ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", |
| 4168 | dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), |
| 4169 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))); |
| 4170 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4171 | const size_t csize = dtohl(chunk->size); |
| 4172 | const uint16_t ctype = dtohs(chunk->type); |
| 4173 | if (ctype == RES_STRING_POOL_TYPE) { |
| 4174 | if (header->values.getError() != NO_ERROR) { |
| 4175 | // Only use the first string chunk; ignore any others that |
| 4176 | // may appear. |
| 4177 | status_t err = header->values.setTo(chunk, csize); |
| 4178 | if (err != NO_ERROR) { |
| 4179 | return (mError=err); |
| 4180 | } |
| 4181 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4182 | ALOGW("Multiple string chunks found in resource table."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4183 | } |
| 4184 | } else if (ctype == RES_TABLE_PACKAGE_TYPE) { |
| 4185 | if (curPackage >= dtohl(header->header->packageCount)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4186 | 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] | 4187 | dtohl(header->header->packageCount)); |
| 4188 | return (mError=BAD_TYPE); |
| 4189 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4190 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 4191 | if (parsePackage( |
| 4192 | (ResTable_package*)chunk, header, appAsLib, isSystemAsset) != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | return mError; |
| 4194 | } |
| 4195 | curPackage++; |
| 4196 | } else { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 4197 | ALOGW("Unknown chunk type 0x%x in table at %p.\n", |
| 4198 | ctype, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4199 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))); |
| 4200 | } |
| 4201 | chunk = (const ResChunk_header*) |
| 4202 | (((const uint8_t*)chunk) + csize); |
| 4203 | } |
| 4204 | |
| 4205 | if (curPackage < dtohl(header->header->packageCount)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4206 | 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] | 4207 | (int)curPackage, dtohl(header->header->packageCount)); |
| 4208 | return (mError=BAD_TYPE); |
| 4209 | } |
| 4210 | mError = header->values.getError(); |
| 4211 | if (mError != NO_ERROR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4212 | ALOGW("No string values found in resource table!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4213 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4214 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4215 | if (kDebugTableNoisy) { |
| 4216 | ALOGV("Returning from add with mError=%d\n", mError); |
| 4217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | return mError; |
| 4219 | } |
| 4220 | |
| 4221 | status_t ResTable::getError() const |
| 4222 | { |
| 4223 | return mError; |
| 4224 | } |
| 4225 | |
| 4226 | void ResTable::uninit() |
| 4227 | { |
| 4228 | mError = NO_INIT; |
| 4229 | size_t N = mPackageGroups.size(); |
| 4230 | for (size_t i=0; i<N; i++) { |
| 4231 | PackageGroup* g = mPackageGroups[i]; |
| 4232 | delete g; |
| 4233 | } |
| 4234 | N = mHeaders.size(); |
| 4235 | for (size_t i=0; i<N; i++) { |
| 4236 | Header* header = mHeaders[i]; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 4237 | if (header->owner == this) { |
| 4238 | if (header->ownedData) { |
| 4239 | free(header->ownedData); |
| 4240 | } |
| 4241 | delete header; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | mPackageGroups.clear(); |
| 4246 | mHeaders.clear(); |
| 4247 | } |
| 4248 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 4249 | 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] | 4250 | { |
| 4251 | if (mError != NO_ERROR) { |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
| 4255 | const ssize_t p = getResourcePackageIndex(resID); |
| 4256 | const int t = Res_GETTYPE(resID); |
| 4257 | const int e = Res_GETENTRY(resID); |
| 4258 | |
| 4259 | if (p < 0) { |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 4260 | if (Res_GETPACKAGE(resID)+1 == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4261 | 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] | 4262 | } else { |
Adam Lesinski | 1d7172e | 2016-03-30 16:22:33 -0700 | [diff] [blame] | 4263 | #ifndef STATIC_ANDROIDFW_FOR_TOOLS |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4264 | ALOGW("No known package when getting name for resource number 0x%08x", resID); |
Adam Lesinski | 1d7172e | 2016-03-30 16:22:33 -0700 | [diff] [blame] | 4265 | #endif |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 4266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | return false; |
| 4268 | } |
| 4269 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4270 | 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] | 4271 | return false; |
| 4272 | } |
| 4273 | |
| 4274 | const PackageGroup* const grp = mPackageGroups[p]; |
| 4275 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4276 | 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] | 4277 | return false; |
| 4278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4280 | Entry entry; |
| 4281 | status_t err = getEntry(grp, t, e, NULL, &entry); |
| 4282 | if (err != NO_ERROR) { |
| 4283 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4284 | } |
| 4285 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4286 | outName->package = grp->name.string(); |
| 4287 | outName->packageLen = grp->name.size(); |
| 4288 | if (allowUtf8) { |
| 4289 | outName->type8 = entry.typeStr.string8(&outName->typeLen); |
| 4290 | outName->name8 = entry.keyStr.string8(&outName->nameLen); |
| 4291 | } else { |
| 4292 | outName->type8 = NULL; |
| 4293 | outName->name8 = NULL; |
| 4294 | } |
| 4295 | if (outName->type8 == NULL) { |
| 4296 | outName->type = entry.typeStr.string16(&outName->typeLen); |
| 4297 | // If we have a bad index for some reason, we should abort. |
| 4298 | if (outName->type == NULL) { |
| 4299 | return false; |
| 4300 | } |
| 4301 | } |
| 4302 | if (outName->name8 == NULL) { |
| 4303 | outName->name = entry.keyStr.string16(&outName->nameLen); |
| 4304 | // If we have a bad index for some reason, we should abort. |
| 4305 | if (outName->name == NULL) { |
| 4306 | return false; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | } |
| 4312 | |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4313 | 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] | 4314 | uint32_t* outSpecFlags, ResTable_config* outConfig) const |
| 4315 | { |
| 4316 | if (mError != NO_ERROR) { |
| 4317 | return mError; |
| 4318 | } |
| 4319 | |
| 4320 | const ssize_t p = getResourcePackageIndex(resID); |
| 4321 | const int t = Res_GETTYPE(resID); |
| 4322 | const int e = Res_GETENTRY(resID); |
| 4323 | |
| 4324 | if (p < 0) { |
Dianne Hackborn | 6cca159 | 2009-09-20 12:40:03 -0700 | [diff] [blame] | 4325 | if (Res_GETPACKAGE(resID)+1 == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4326 | 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] | 4327 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4328 | 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] | 4329 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4330 | return BAD_INDEX; |
| 4331 | } |
| 4332 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4333 | 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] | 4334 | return BAD_INDEX; |
| 4335 | } |
| 4336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4337 | const PackageGroup* const grp = mPackageGroups[p]; |
| 4338 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4339 | ALOGW("Bad identifier when getting value for resource number 0x%08x", resID); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 4340 | return BAD_INDEX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4341 | } |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4342 | |
| 4343 | // Allow overriding density |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4344 | ResTable_config desiredConfig = mParams; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4345 | if (density > 0) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4346 | desiredConfig.density = density; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4349 | Entry entry; |
| 4350 | status_t err = getEntry(grp, t, e, &desiredConfig, &entry); |
| 4351 | if (err != NO_ERROR) { |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4352 | // Only log the failure when we're not running on the host as |
| 4353 | // part of a tool. The caller will do its own logging. |
| 4354 | #ifndef STATIC_ANDROIDFW_FOR_TOOLS |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4355 | ALOGW("Failure getting entry for 0x%08x (t=%d e=%d) (error %d)\n", |
| 4356 | resID, t, e, err); |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4357 | #endif |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4358 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | } |
| 4360 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4361 | if ((dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) != 0) { |
| 4362 | if (!mayBeBag) { |
| 4363 | ALOGW("Requesting resource 0x%08x failed because it is complex\n", resID); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 4364 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4365 | return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4368 | const Res_value* value = reinterpret_cast<const Res_value*>( |
| 4369 | reinterpret_cast<const uint8_t*>(entry.entry) + entry.entry->size); |
| 4370 | |
| 4371 | outValue->size = dtohs(value->size); |
| 4372 | outValue->res0 = value->res0; |
| 4373 | outValue->dataType = value->dataType; |
| 4374 | outValue->data = dtohl(value->data); |
| 4375 | |
| 4376 | // The reference may be pointing to a resource in a shared library. These |
| 4377 | // references have build-time generated package IDs. These ids may not match |
| 4378 | // the actual package IDs of the corresponding packages in this ResTable. |
| 4379 | // We need to fix the package ID based on a mapping. |
| 4380 | if (grp->dynamicRefTable.lookupResourceValue(outValue) != NO_ERROR) { |
| 4381 | ALOGW("Failed to resolve referenced package: 0x%08x", outValue->data); |
| 4382 | return BAD_VALUE; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4385 | if (kDebugTableNoisy) { |
| 4386 | size_t len; |
| 4387 | printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n", |
| 4388 | entry.package->header->index, |
| 4389 | outValue->dataType, |
| 4390 | outValue->dataType == Res_value::TYPE_STRING ? |
| 4391 | String8(entry.package->header->values.stringAt(outValue->data, &len)).string() : |
| 4392 | "", |
| 4393 | outValue->data); |
| 4394 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4395 | |
| 4396 | if (outSpecFlags != NULL) { |
| 4397 | *outSpecFlags = entry.specFlags; |
| 4398 | } |
| 4399 | |
| 4400 | if (outConfig != NULL) { |
| 4401 | *outConfig = entry.config; |
| 4402 | } |
| 4403 | |
| 4404 | return entry.package->header->index; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 4408 | uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags, |
| 4409 | ResTable_config* outConfig) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4410 | { |
| 4411 | int count=0; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 4412 | while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE |
| 4413 | && value->data != 0 && count < 20) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4414 | if (outLastRef) *outLastRef = value->data; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4415 | uint32_t newFlags = 0; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 4416 | const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 4417 | outConfig); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 4418 | if (newIndex == BAD_INDEX) { |
| 4419 | return BAD_INDEX; |
| 4420 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4421 | if (kDebugTableTheme) { |
| 4422 | ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n", |
| 4423 | value->data, (int)newIndex, (int)value->dataType, value->data); |
| 4424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4425 | //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex); |
| 4426 | if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags; |
| 4427 | if (newIndex < 0) { |
| 4428 | // This can fail if the resource being referenced is a style... |
| 4429 | // in this case, just return the reference, and expect the |
| 4430 | // caller to deal with. |
| 4431 | return blockIndex; |
| 4432 | } |
| 4433 | blockIndex = newIndex; |
| 4434 | count++; |
| 4435 | } |
| 4436 | return blockIndex; |
| 4437 | } |
| 4438 | |
| 4439 | const char16_t* ResTable::valueToString( |
| 4440 | const Res_value* value, size_t stringBlock, |
Adam Lesinski | ad2d07d | 2014-08-27 16:21:08 -0700 | [diff] [blame] | 4441 | char16_t /*tmpBuffer*/ [TMP_BUFFER_SIZE], size_t* outLen) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4442 | { |
| 4443 | if (!value) { |
| 4444 | return NULL; |
| 4445 | } |
| 4446 | if (value->dataType == value->TYPE_STRING) { |
| 4447 | return getTableStringBlock(stringBlock)->stringAt(value->data, outLen); |
| 4448 | } |
| 4449 | // XXX do int to string conversions. |
| 4450 | return NULL; |
| 4451 | } |
| 4452 | |
| 4453 | ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const |
| 4454 | { |
| 4455 | mLock.lock(); |
| 4456 | ssize_t err = getBagLocked(resID, outBag); |
| 4457 | if (err < NO_ERROR) { |
| 4458 | //printf("*** get failed! unlocking\n"); |
| 4459 | mLock.unlock(); |
| 4460 | } |
| 4461 | return err; |
| 4462 | } |
| 4463 | |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4464 | void ResTable::unlockBag(const bag_entry* /*bag*/) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4465 | { |
| 4466 | //printf("<<< unlockBag %p\n", this); |
| 4467 | mLock.unlock(); |
| 4468 | } |
| 4469 | |
| 4470 | void ResTable::lock() const |
| 4471 | { |
| 4472 | mLock.lock(); |
| 4473 | } |
| 4474 | |
| 4475 | void ResTable::unlock() const |
| 4476 | { |
| 4477 | mLock.unlock(); |
| 4478 | } |
| 4479 | |
| 4480 | ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag, |
| 4481 | uint32_t* outTypeSpecFlags) const |
| 4482 | { |
| 4483 | if (mError != NO_ERROR) { |
| 4484 | return mError; |
| 4485 | } |
| 4486 | |
| 4487 | const ssize_t p = getResourcePackageIndex(resID); |
| 4488 | const int t = Res_GETTYPE(resID); |
| 4489 | const int e = Res_GETENTRY(resID); |
| 4490 | |
| 4491 | if (p < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4492 | 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] | 4493 | return BAD_INDEX; |
| 4494 | } |
| 4495 | if (t < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4496 | 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] | 4497 | return BAD_INDEX; |
| 4498 | } |
| 4499 | |
| 4500 | //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t); |
| 4501 | PackageGroup* const grp = mPackageGroups[p]; |
| 4502 | if (grp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4503 | 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] | 4504 | return BAD_INDEX; |
| 4505 | } |
| 4506 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4507 | const TypeList& typeConfigs = grp->types[t]; |
| 4508 | if (typeConfigs.isEmpty()) { |
| 4509 | ALOGW("Type identifier 0x%x does not exist.", t+1); |
| 4510 | return BAD_INDEX; |
| 4511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4512 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4513 | const size_t NENTRY = typeConfigs[0]->entryCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4514 | if (e >= (int)NENTRY) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4515 | ALOGW("Entry identifier 0x%x is larger than entry count 0x%x", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4516 | e, (int)typeConfigs[0]->entryCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4517 | return BAD_INDEX; |
| 4518 | } |
| 4519 | |
| 4520 | // First see if we've already computed this bag... |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4521 | TypeCacheEntry& cacheEntry = grp->typeCacheEntries.editItemAt(t); |
| 4522 | bag_set** typeSet = cacheEntry.cachedBags; |
| 4523 | if (typeSet) { |
| 4524 | bag_set* set = typeSet[e]; |
| 4525 | if (set) { |
| 4526 | if (set != (bag_set*)0xFFFFFFFF) { |
| 4527 | if (outTypeSpecFlags != NULL) { |
| 4528 | *outTypeSpecFlags = set->typeSpecFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4529 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4530 | *outBag = (bag_entry*)(set+1); |
| 4531 | if (kDebugTableSuperNoisy) { |
| 4532 | ALOGI("Found existing bag for: 0x%x\n", resID); |
| 4533 | } |
| 4534 | return set->numAttrs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4535 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4536 | ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.", |
| 4537 | resID); |
| 4538 | return BAD_INDEX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | // Bag not found, we need to compute it! |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4543 | if (!typeSet) { |
Iliyan Malchev | 7e1d395 | 2012-02-17 12:15:58 -0800 | [diff] [blame] | 4544 | typeSet = (bag_set**)calloc(NENTRY, sizeof(bag_set*)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4545 | if (!typeSet) return NO_MEMORY; |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4546 | cacheEntry.cachedBags = typeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | // Mark that we are currently working on this one. |
| 4550 | typeSet[e] = (bag_set*)0xFFFFFFFF; |
| 4551 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4552 | if (kDebugTableNoisy) { |
| 4553 | ALOGI("Building bag: %x\n", resID); |
| 4554 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4555 | |
| 4556 | // Now collect all bag attributes |
| 4557 | Entry entry; |
| 4558 | status_t err = getEntry(grp, t, e, &mParams, &entry); |
| 4559 | if (err != NO_ERROR) { |
| 4560 | return err; |
| 4561 | } |
| 4562 | |
| 4563 | const uint16_t entrySize = dtohs(entry.entry->size); |
| 4564 | const uint32_t parent = entrySize >= sizeof(ResTable_map_entry) |
| 4565 | ? dtohl(((const ResTable_map_entry*)entry.entry)->parent.ident) : 0; |
| 4566 | const uint32_t count = entrySize >= sizeof(ResTable_map_entry) |
| 4567 | ? dtohl(((const ResTable_map_entry*)entry.entry)->count) : 0; |
| 4568 | |
| 4569 | size_t N = count; |
| 4570 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4571 | if (kDebugTableNoisy) { |
| 4572 | ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4573 | |
| 4574 | // If this map inherits from another, we need to start |
| 4575 | // with its parent's values. Otherwise start out empty. |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4576 | ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent); |
| 4577 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | // This is what we are building. |
| 4580 | bag_set* set = NULL; |
| 4581 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4582 | if (parent) { |
| 4583 | uint32_t resolvedParent = parent; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4584 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4585 | // Bags encode a parent reference without using the standard |
| 4586 | // Res_value structure. That means we must always try to |
| 4587 | // resolve a parent reference in case it is actually a |
| 4588 | // TYPE_DYNAMIC_REFERENCE. |
| 4589 | status_t err = grp->dynamicRefTable.lookupResourceId(&resolvedParent); |
| 4590 | if (err != NO_ERROR) { |
| 4591 | ALOGE("Failed resolving bag parent id 0x%08x", parent); |
| 4592 | return UNKNOWN_ERROR; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4594 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4595 | const bag_entry* parentBag; |
| 4596 | uint32_t parentTypeSpecFlags = 0; |
| 4597 | const ssize_t NP = getBagLocked(resolvedParent, &parentBag, &parentTypeSpecFlags); |
| 4598 | const size_t NT = ((NP >= 0) ? NP : 0) + N; |
| 4599 | set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT); |
| 4600 | if (set == NULL) { |
| 4601 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4603 | if (NP > 0) { |
| 4604 | memcpy(set+1, parentBag, NP*sizeof(bag_entry)); |
| 4605 | set->numAttrs = NP; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4606 | if (kDebugTableNoisy) { |
| 4607 | ALOGI("Initialized new bag with %zd inherited attributes.\n", NP); |
| 4608 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4609 | } else { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4610 | if (kDebugTableNoisy) { |
| 4611 | ALOGI("Initialized new bag with no inherited attributes.\n"); |
| 4612 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 4613 | set->numAttrs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4614 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4615 | set->availAttrs = NT; |
| 4616 | set->typeSpecFlags = parentTypeSpecFlags; |
| 4617 | } else { |
| 4618 | set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N); |
| 4619 | if (set == NULL) { |
| 4620 | return NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4621 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4622 | set->numAttrs = 0; |
| 4623 | set->availAttrs = N; |
| 4624 | set->typeSpecFlags = 0; |
| 4625 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 4626 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4627 | set->typeSpecFlags |= entry.specFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4628 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4629 | // Now merge in the new attributes... |
| 4630 | size_t curOff = (reinterpret_cast<uintptr_t>(entry.entry) - reinterpret_cast<uintptr_t>(entry.type)) |
| 4631 | + dtohs(entry.entry->size); |
| 4632 | const ResTable_map* map; |
| 4633 | bag_entry* entries = (bag_entry*)(set+1); |
| 4634 | size_t curEntry = 0; |
| 4635 | uint32_t pos = 0; |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4636 | if (kDebugTableNoisy) { |
| 4637 | ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs); |
| 4638 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4639 | while (pos < count) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4640 | if (kDebugTableNoisy) { |
| 4641 | ALOGI("Now at %p\n", (void*)curOff); |
| 4642 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4643 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4644 | if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) { |
| 4645 | ALOGW("ResTable_map at %d is beyond type chunk data %d", |
| 4646 | (int)curOff, dtohl(entry.type->header.size)); |
Yunlian Jiang | a7645bd | 2016-12-13 19:42:30 -0800 | [diff] [blame] | 4647 | free(set); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4648 | return BAD_TYPE; |
| 4649 | } |
| 4650 | map = (const ResTable_map*)(((const uint8_t*)entry.type) + curOff); |
| 4651 | N++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4652 | |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 4653 | uint32_t newName = htodl(map->name.ident); |
| 4654 | if (!Res_INTERNALID(newName)) { |
| 4655 | // Attributes don't have a resource id as the name. They specify |
| 4656 | // other data, which would be wrong to change via a lookup. |
| 4657 | if (grp->dynamicRefTable.lookupResourceId(&newName) != NO_ERROR) { |
| 4658 | ALOGE("Failed resolving ResTable_map name at %d with ident 0x%08x", |
| 4659 | (int) curOff, (int) newName); |
Yunlian Jiang | a7645bd | 2016-12-13 19:42:30 -0800 | [diff] [blame] | 4660 | free(set); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 4661 | return UNKNOWN_ERROR; |
| 4662 | } |
| 4663 | } |
| 4664 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4665 | bool isInside; |
| 4666 | uint32_t oldName = 0; |
| 4667 | while ((isInside=(curEntry < set->numAttrs)) |
| 4668 | && (oldName=entries[curEntry].map.name.ident) < newName) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4669 | if (kDebugTableNoisy) { |
| 4670 | ALOGI("#%zu: Keeping existing attribute: 0x%08x\n", |
| 4671 | curEntry, entries[curEntry].map.name.ident); |
| 4672 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | curEntry++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4674 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4675 | |
| 4676 | if ((!isInside) || oldName != newName) { |
| 4677 | // This is a new attribute... figure out what to do with it. |
| 4678 | if (set->numAttrs >= set->availAttrs) { |
| 4679 | // Need to alloc more memory... |
| 4680 | const size_t newAvail = set->availAttrs+N; |
George Burgess IV | e8efec5 | 2016-10-11 15:42:29 -0700 | [diff] [blame] | 4681 | void *oldSet = set; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4682 | set = (bag_set*)realloc(set, |
| 4683 | sizeof(bag_set) |
| 4684 | + sizeof(bag_entry)*newAvail); |
| 4685 | if (set == NULL) { |
George Burgess IV | e8efec5 | 2016-10-11 15:42:29 -0700 | [diff] [blame] | 4686 | free(oldSet); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4687 | return NO_MEMORY; |
| 4688 | } |
| 4689 | set->availAttrs = newAvail; |
| 4690 | entries = (bag_entry*)(set+1); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4691 | if (kDebugTableNoisy) { |
| 4692 | ALOGI("Reallocated set %p, entries=%p, avail=%zu\n", |
| 4693 | set, entries, set->availAttrs); |
| 4694 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4695 | } |
| 4696 | if (isInside) { |
| 4697 | // Going in the middle, need to make space. |
| 4698 | memmove(entries+curEntry+1, entries+curEntry, |
| 4699 | sizeof(bag_entry)*(set->numAttrs-curEntry)); |
| 4700 | set->numAttrs++; |
| 4701 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4702 | if (kDebugTableNoisy) { |
| 4703 | ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName); |
| 4704 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4705 | } else { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4706 | if (kDebugTableNoisy) { |
| 4707 | ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName); |
| 4708 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | bag_entry* cur = entries+curEntry; |
| 4712 | |
| 4713 | cur->stringBlock = entry.package->header->index; |
| 4714 | cur->map.name.ident = newName; |
| 4715 | cur->map.value.copyFrom_dtoh(map->value); |
| 4716 | status_t err = grp->dynamicRefTable.lookupResourceValue(&cur->map.value); |
| 4717 | if (err != NO_ERROR) { |
| 4718 | ALOGE("Reference item(0x%08x) in bag could not be resolved.", cur->map.value.data); |
| 4719 | return UNKNOWN_ERROR; |
| 4720 | } |
| 4721 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4722 | if (kDebugTableNoisy) { |
| 4723 | ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n", |
| 4724 | curEntry, cur, cur->stringBlock, cur->map.name.ident, |
| 4725 | cur->map.value.dataType, cur->map.value.data); |
| 4726 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4727 | |
| 4728 | // On to the next! |
| 4729 | curEntry++; |
| 4730 | pos++; |
| 4731 | const size_t size = dtohs(map->value.size); |
| 4732 | curOff += size + sizeof(*map)-sizeof(map->value); |
George Burgess IV | e8efec5 | 2016-10-11 15:42:29 -0700 | [diff] [blame] | 4733 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4734 | |
| 4735 | if (curEntry > set->numAttrs) { |
| 4736 | set->numAttrs = curEntry; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | // And this is it... |
| 4740 | typeSet[e] = set; |
| 4741 | if (set) { |
| 4742 | if (outTypeSpecFlags != NULL) { |
| 4743 | *outTypeSpecFlags = set->typeSpecFlags; |
| 4744 | } |
| 4745 | *outBag = (bag_entry*)(set+1); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4746 | if (kDebugTableNoisy) { |
| 4747 | ALOGI("Returning %zu attrs\n", set->numAttrs); |
| 4748 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4749 | return set->numAttrs; |
| 4750 | } |
| 4751 | return BAD_INDEX; |
| 4752 | } |
| 4753 | |
| 4754 | void ResTable::setParameters(const ResTable_config* params) |
| 4755 | { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4756 | AutoMutex _lock(mLock); |
| 4757 | AutoMutex _lock2(mFilteredConfigLock); |
| 4758 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4759 | if (kDebugTableGetEntry) { |
| 4760 | ALOGI("Setting parameters: %s\n", params->toString().string()); |
| 4761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4762 | mParams = *params; |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4763 | for (size_t p = 0; p < mPackageGroups.size(); p++) { |
| 4764 | PackageGroup* packageGroup = mPackageGroups.editItemAt(p); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4765 | if (kDebugTableNoisy) { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4766 | ALOGI("CLEARING BAGS FOR GROUP %zu!", p); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4767 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 4768 | packageGroup->clearBagCache(); |
| 4769 | |
| 4770 | // Find which configurations match the set of parameters. This allows for a much |
| 4771 | // faster lookup in getEntry() if the set of values is narrowed down. |
| 4772 | for (size_t t = 0; t < packageGroup->types.size(); t++) { |
| 4773 | if (packageGroup->types[t].isEmpty()) { |
| 4774 | continue; |
| 4775 | } |
| 4776 | |
| 4777 | TypeList& typeList = packageGroup->types.editItemAt(t); |
| 4778 | |
| 4779 | // Retrieve the cache entry for this type. |
| 4780 | TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries.editItemAt(t); |
| 4781 | |
| 4782 | for (size_t ts = 0; ts < typeList.size(); ts++) { |
| 4783 | Type* type = typeList.editItemAt(ts); |
| 4784 | |
| 4785 | std::shared_ptr<Vector<const ResTable_type*>> newFilteredConfigs = |
| 4786 | std::make_shared<Vector<const ResTable_type*>>(); |
| 4787 | |
| 4788 | for (size_t ti = 0; ti < type->configs.size(); ti++) { |
| 4789 | ResTable_config config; |
| 4790 | config.copyFromDtoH(type->configs[ti]->config); |
| 4791 | |
| 4792 | if (config.match(mParams)) { |
| 4793 | newFilteredConfigs->add(type->configs[ti]); |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | if (kDebugTableNoisy) { |
| 4798 | ALOGD("Updating pkg=%zu type=%zu with %zu filtered configs", |
| 4799 | p, t, newFilteredConfigs->size()); |
| 4800 | } |
| 4801 | |
| 4802 | cacheEntry.filteredConfigs.add(newFilteredConfigs); |
| 4803 | } |
| 4804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | void ResTable::getParameters(ResTable_config* params) const |
| 4809 | { |
| 4810 | mLock.lock(); |
| 4811 | *params = mParams; |
| 4812 | mLock.unlock(); |
| 4813 | } |
| 4814 | |
| 4815 | struct id_name_map { |
| 4816 | uint32_t id; |
| 4817 | size_t len; |
| 4818 | char16_t name[6]; |
| 4819 | }; |
| 4820 | |
| 4821 | const static id_name_map ID_NAMES[] = { |
| 4822 | { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } }, |
| 4823 | { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } }, |
| 4824 | { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } }, |
| 4825 | { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } }, |
| 4826 | { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } }, |
| 4827 | { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } }, |
| 4828 | { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } }, |
| 4829 | { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } }, |
| 4830 | { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } }, |
| 4831 | { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } }, |
| 4832 | }; |
| 4833 | |
| 4834 | uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen, |
| 4835 | const char16_t* type, size_t typeLen, |
| 4836 | const char16_t* package, |
| 4837 | size_t packageLen, |
| 4838 | uint32_t* outTypeSpecFlags) const |
| 4839 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4840 | if (kDebugTableSuperNoisy) { |
| 4841 | printf("Identifier for name: error=%d\n", mError); |
| 4842 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4843 | |
| 4844 | // Check for internal resource identifier as the very first thing, so |
| 4845 | // that we will always find them even when there are no resources. |
| 4846 | if (name[0] == '^') { |
| 4847 | const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0])); |
| 4848 | size_t len; |
| 4849 | for (int i=0; i<N; i++) { |
| 4850 | const id_name_map* m = ID_NAMES + i; |
| 4851 | len = m->len; |
| 4852 | if (len != nameLen) { |
| 4853 | continue; |
| 4854 | } |
| 4855 | for (size_t j=1; j<len; j++) { |
| 4856 | if (m->name[j] != name[j]) { |
| 4857 | goto nope; |
| 4858 | } |
| 4859 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 4860 | if (outTypeSpecFlags) { |
| 4861 | *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC; |
| 4862 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4863 | return m->id; |
| 4864 | nope: |
| 4865 | ; |
| 4866 | } |
| 4867 | if (nameLen > 7) { |
| 4868 | if (name[1] == 'i' && name[2] == 'n' |
| 4869 | && name[3] == 'd' && name[4] == 'e' && name[5] == 'x' |
| 4870 | && name[6] == '_') { |
| 4871 | int index = atoi(String8(name + 7, nameLen - 7).string()); |
| 4872 | if (Res_CHECKID(index)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4873 | ALOGW("Array resource index: %d is too large.", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4874 | index); |
| 4875 | return 0; |
| 4876 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 4877 | if (outTypeSpecFlags) { |
| 4878 | *outTypeSpecFlags = ResTable_typeSpec::SPEC_PUBLIC; |
| 4879 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4880 | return Res_MAKEARRAY(index); |
| 4881 | } |
| 4882 | } |
| 4883 | return 0; |
| 4884 | } |
| 4885 | |
| 4886 | if (mError != NO_ERROR) { |
| 4887 | return 0; |
| 4888 | } |
| 4889 | |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 4890 | bool fakePublic = false; |
| 4891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4892 | // Figure out the package and type we are looking in... |
| 4893 | |
| 4894 | const char16_t* packageEnd = NULL; |
| 4895 | const char16_t* typeEnd = NULL; |
| 4896 | const char16_t* const nameEnd = name+nameLen; |
| 4897 | const char16_t* p = name; |
| 4898 | while (p < nameEnd) { |
| 4899 | if (*p == ':') packageEnd = p; |
| 4900 | else if (*p == '/') typeEnd = p; |
| 4901 | p++; |
| 4902 | } |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 4903 | if (*name == '@') { |
| 4904 | name++; |
| 4905 | if (*name == '*') { |
| 4906 | fakePublic = true; |
| 4907 | name++; |
| 4908 | } |
| 4909 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4910 | if (name >= nameEnd) { |
| 4911 | return 0; |
| 4912 | } |
| 4913 | |
| 4914 | if (packageEnd) { |
| 4915 | package = name; |
| 4916 | packageLen = packageEnd-name; |
| 4917 | name = packageEnd+1; |
| 4918 | } else if (!package) { |
| 4919 | return 0; |
| 4920 | } |
| 4921 | |
| 4922 | if (typeEnd) { |
| 4923 | type = name; |
| 4924 | typeLen = typeEnd-name; |
| 4925 | name = typeEnd+1; |
| 4926 | } else if (!type) { |
| 4927 | return 0; |
| 4928 | } |
| 4929 | |
| 4930 | if (name >= nameEnd) { |
| 4931 | return 0; |
| 4932 | } |
| 4933 | nameLen = nameEnd-name; |
| 4934 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4935 | if (kDebugTableNoisy) { |
| 4936 | printf("Looking for identifier: type=%s, name=%s, package=%s\n", |
| 4937 | String8(type, typeLen).string(), |
| 4938 | String8(name, nameLen).string(), |
| 4939 | String8(package, packageLen).string()); |
| 4940 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4942 | const String16 attr("attr"); |
| 4943 | const String16 attrPrivate("^attr-private"); |
| 4944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4945 | const size_t NG = mPackageGroups.size(); |
| 4946 | for (size_t ig=0; ig<NG; ig++) { |
| 4947 | const PackageGroup* group = mPackageGroups[ig]; |
| 4948 | |
| 4949 | if (strzcmp16(package, packageLen, |
| 4950 | group->name.string(), group->name.size())) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 4951 | if (kDebugTableNoisy) { |
| 4952 | printf("Skipping package group: %s\n", String8(group->name).string()); |
| 4953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4954 | continue; |
| 4955 | } |
| 4956 | |
Adam Lesinski | e60a87f | 2014-10-09 11:08:04 -0700 | [diff] [blame] | 4957 | const size_t packageCount = group->packages.size(); |
| 4958 | for (size_t pi = 0; pi < packageCount; pi++) { |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4959 | const char16_t* targetType = type; |
| 4960 | size_t targetTypeLen = typeLen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4961 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4962 | do { |
| 4963 | ssize_t ti = group->packages[pi]->typeStrings.indexOfString( |
| 4964 | targetType, targetTypeLen); |
| 4965 | if (ti < 0) { |
Adam Lesinski | e60a87f | 2014-10-09 11:08:04 -0700 | [diff] [blame] | 4966 | continue; |
| 4967 | } |
| 4968 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4969 | ti += group->packages[pi]->typeIdOffset; |
Adam Lesinski | e60a87f | 2014-10-09 11:08:04 -0700 | [diff] [blame] | 4970 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4971 | const uint32_t identifier = findEntry(group, ti, name, nameLen, |
| 4972 | outTypeSpecFlags); |
| 4973 | if (identifier != 0) { |
| 4974 | if (fakePublic && outTypeSpecFlags) { |
| 4975 | *outTypeSpecFlags |= ResTable_typeSpec::SPEC_PUBLIC; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 4976 | } |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4977 | return identifier; |
| 4978 | } |
| 4979 | } while (strzcmp16(attr.string(), attr.size(), targetType, targetTypeLen) == 0 |
| 4980 | && (targetType = attrPrivate.string()) |
| 4981 | && (targetTypeLen = attrPrivate.size()) |
| 4982 | ); |
| 4983 | } |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4984 | } |
| 4985 | return 0; |
| 4986 | } |
| 4987 | |
| 4988 | uint32_t ResTable::findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name, |
| 4989 | size_t nameLen, uint32_t* outTypeSpecFlags) const { |
| 4990 | const TypeList& typeList = group->types[typeIndex]; |
| 4991 | const size_t typeCount = typeList.size(); |
| 4992 | for (size_t i = 0; i < typeCount; i++) { |
| 4993 | const Type* t = typeList[i]; |
| 4994 | const ssize_t ei = t->package->keyStrings.indexOfString(name, nameLen); |
| 4995 | if (ei < 0) { |
| 4996 | continue; |
| 4997 | } |
| 4998 | |
| 4999 | const size_t configCount = t->configs.size(); |
| 5000 | for (size_t j = 0; j < configCount; j++) { |
| 5001 | const TypeVariant tv(t->configs[j]); |
| 5002 | for (TypeVariant::iterator iter = tv.beginEntries(); |
| 5003 | iter != tv.endEntries(); |
| 5004 | iter++) { |
| 5005 | const ResTable_entry* entry = *iter; |
| 5006 | if (entry == NULL) { |
| 5007 | continue; |
| 5008 | } |
| 5009 | |
| 5010 | if (dtohl(entry->key.index) == (size_t) ei) { |
| 5011 | uint32_t resId = Res_MAKEID(group->id - 1, typeIndex, iter.index()); |
| 5012 | if (outTypeSpecFlags) { |
| 5013 | Entry result; |
| 5014 | if (getEntry(group, typeIndex, iter.index(), NULL, &result) != NO_ERROR) { |
| 5015 | ALOGW("Failed to find spec flags for 0x%08x", resId); |
| 5016 | return 0; |
| 5017 | } |
| 5018 | *outTypeSpecFlags = result.specFlags; |
| 5019 | } |
| 5020 | return resId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5025 | return 0; |
| 5026 | } |
| 5027 | |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 5028 | bool ResTable::expandResourceRef(const char16_t* refStr, size_t refLen, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5029 | String16* outPackage, |
| 5030 | String16* outType, |
| 5031 | String16* outName, |
| 5032 | const String16* defType, |
| 5033 | const String16* defPackage, |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 5034 | const char** outErrorMsg, |
| 5035 | bool* outPublicOnly) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5036 | { |
| 5037 | const char16_t* packageEnd = NULL; |
| 5038 | const char16_t* typeEnd = NULL; |
| 5039 | const char16_t* p = refStr; |
| 5040 | const char16_t* const end = p + refLen; |
| 5041 | while (p < end) { |
| 5042 | if (*p == ':') packageEnd = p; |
| 5043 | else if (*p == '/') { |
| 5044 | typeEnd = p; |
| 5045 | break; |
| 5046 | } |
| 5047 | p++; |
| 5048 | } |
| 5049 | p = refStr; |
| 5050 | if (*p == '@') p++; |
| 5051 | |
Dianne Hackborn | 426431a | 2011-06-09 11:29:08 -0700 | [diff] [blame] | 5052 | if (outPublicOnly != NULL) { |
| 5053 | *outPublicOnly = true; |
| 5054 | } |
| 5055 | if (*p == '*') { |
| 5056 | p++; |
| 5057 | if (outPublicOnly != NULL) { |
| 5058 | *outPublicOnly = false; |
| 5059 | } |
| 5060 | } |
| 5061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5062 | if (packageEnd) { |
| 5063 | *outPackage = String16(p, packageEnd-p); |
| 5064 | p = packageEnd+1; |
| 5065 | } else { |
| 5066 | if (!defPackage) { |
| 5067 | if (outErrorMsg) { |
| 5068 | *outErrorMsg = "No resource package specified"; |
| 5069 | } |
| 5070 | return false; |
| 5071 | } |
| 5072 | *outPackage = *defPackage; |
| 5073 | } |
| 5074 | if (typeEnd) { |
| 5075 | *outType = String16(p, typeEnd-p); |
| 5076 | p = typeEnd+1; |
| 5077 | } else { |
| 5078 | if (!defType) { |
| 5079 | if (outErrorMsg) { |
| 5080 | *outErrorMsg = "No resource type specified"; |
| 5081 | } |
| 5082 | return false; |
| 5083 | } |
| 5084 | *outType = *defType; |
| 5085 | } |
| 5086 | *outName = String16(p, end-p); |
Konstantin Lopyrev | ddcafcb | 2010-06-04 14:36:49 -0700 | [diff] [blame] | 5087 | if(**outPackage == 0) { |
| 5088 | if(outErrorMsg) { |
| 5089 | *outErrorMsg = "Resource package cannot be an empty string"; |
| 5090 | } |
| 5091 | return false; |
| 5092 | } |
| 5093 | if(**outType == 0) { |
| 5094 | if(outErrorMsg) { |
| 5095 | *outErrorMsg = "Resource type cannot be an empty string"; |
| 5096 | } |
| 5097 | return false; |
| 5098 | } |
| 5099 | if(**outName == 0) { |
| 5100 | if(outErrorMsg) { |
| 5101 | *outErrorMsg = "Resource id cannot be an empty string"; |
| 5102 | } |
| 5103 | return false; |
| 5104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5105 | return true; |
| 5106 | } |
| 5107 | |
| 5108 | static uint32_t get_hex(char c, bool* outError) |
| 5109 | { |
| 5110 | if (c >= '0' && c <= '9') { |
| 5111 | return c - '0'; |
| 5112 | } else if (c >= 'a' && c <= 'f') { |
| 5113 | return c - 'a' + 0xa; |
| 5114 | } else if (c >= 'A' && c <= 'F') { |
| 5115 | return c - 'A' + 0xa; |
| 5116 | } |
| 5117 | *outError = true; |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
| 5121 | struct unit_entry |
| 5122 | { |
| 5123 | const char* name; |
| 5124 | size_t len; |
| 5125 | uint8_t type; |
| 5126 | uint32_t unit; |
| 5127 | float scale; |
| 5128 | }; |
| 5129 | |
| 5130 | static const unit_entry unitNames[] = { |
| 5131 | { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f }, |
| 5132 | { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, |
| 5133 | { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, |
| 5134 | { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f }, |
| 5135 | { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f }, |
| 5136 | { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f }, |
| 5137 | { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f }, |
| 5138 | { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 }, |
| 5139 | { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 }, |
| 5140 | { NULL, 0, 0, 0, 0 } |
| 5141 | }; |
| 5142 | |
| 5143 | static bool parse_unit(const char* str, Res_value* outValue, |
| 5144 | float* outScale, const char** outEnd) |
| 5145 | { |
| 5146 | const char* end = str; |
| 5147 | while (*end != 0 && !isspace((unsigned char)*end)) { |
| 5148 | end++; |
| 5149 | } |
| 5150 | const size_t len = end-str; |
| 5151 | |
| 5152 | const char* realEnd = end; |
| 5153 | while (*realEnd != 0 && isspace((unsigned char)*realEnd)) { |
| 5154 | realEnd++; |
| 5155 | } |
| 5156 | if (*realEnd != 0) { |
| 5157 | return false; |
| 5158 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5160 | const unit_entry* cur = unitNames; |
| 5161 | while (cur->name) { |
| 5162 | if (len == cur->len && strncmp(cur->name, str, len) == 0) { |
| 5163 | outValue->dataType = cur->type; |
| 5164 | outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT; |
| 5165 | *outScale = cur->scale; |
| 5166 | *outEnd = end; |
| 5167 | //printf("Found unit %s for %s\n", cur->name, str); |
| 5168 | return true; |
| 5169 | } |
| 5170 | cur++; |
| 5171 | } |
| 5172 | |
| 5173 | return false; |
| 5174 | } |
| 5175 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5176 | bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5177 | { |
| 5178 | while (len > 0 && isspace16(*s)) { |
| 5179 | s++; |
| 5180 | len--; |
| 5181 | } |
| 5182 | |
| 5183 | if (len <= 0) { |
| 5184 | return false; |
| 5185 | } |
| 5186 | |
| 5187 | size_t i = 0; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5188 | int64_t val = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5189 | bool neg = false; |
| 5190 | |
| 5191 | if (*s == '-') { |
| 5192 | neg = true; |
| 5193 | i++; |
| 5194 | } |
| 5195 | |
| 5196 | if (s[i] < '0' || s[i] > '9') { |
| 5197 | return false; |
| 5198 | } |
| 5199 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5200 | static_assert(std::is_same<uint32_t, Res_value::data_type>::value, |
| 5201 | "Res_value::data_type has changed. The range checks in this " |
| 5202 | "function are no longer correct."); |
| 5203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5204 | // Decimal or hex? |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5205 | bool isHex; |
| 5206 | if (len > 1 && s[i] == '0' && s[i+1] == 'x') { |
| 5207 | isHex = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5208 | i += 2; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5209 | |
| 5210 | if (neg) { |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
| 5214 | if (i == len) { |
| 5215 | // Just u"0x" |
| 5216 | return false; |
| 5217 | } |
| 5218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5219 | bool error = false; |
| 5220 | while (i < len && !error) { |
| 5221 | val = (val*16) + get_hex(s[i], &error); |
| 5222 | i++; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5223 | |
| 5224 | if (val > std::numeric_limits<uint32_t>::max()) { |
| 5225 | return false; |
| 5226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | } |
| 5228 | if (error) { |
| 5229 | return false; |
| 5230 | } |
| 5231 | } else { |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5232 | isHex = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5233 | while (i < len) { |
| 5234 | if (s[i] < '0' || s[i] > '9') { |
| 5235 | return false; |
| 5236 | } |
| 5237 | val = (val*10) + s[i]-'0'; |
| 5238 | i++; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5239 | |
| 5240 | if ((neg && -val < std::numeric_limits<int32_t>::min()) || |
| 5241 | (!neg && val > std::numeric_limits<int32_t>::max())) { |
| 5242 | return false; |
| 5243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | if (neg) val = -val; |
| 5248 | |
| 5249 | while (i < len && isspace16(s[i])) { |
| 5250 | i++; |
| 5251 | } |
| 5252 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5253 | if (i != len) { |
| 5254 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5255 | } |
| 5256 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 5257 | if (outValue) { |
| 5258 | outValue->dataType = |
| 5259 | isHex ? outValue->TYPE_INT_HEX : outValue->TYPE_INT_DEC; |
| 5260 | outValue->data = static_cast<Res_value::data_type>(val); |
| 5261 | } |
| 5262 | return true; |
| 5263 | } |
| 5264 | |
| 5265 | bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue) |
| 5266 | { |
| 5267 | return U16StringToInt(s, len, outValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue) |
| 5271 | { |
| 5272 | while (len > 0 && isspace16(*s)) { |
| 5273 | s++; |
| 5274 | len--; |
| 5275 | } |
| 5276 | |
| 5277 | if (len <= 0) { |
| 5278 | return false; |
| 5279 | } |
| 5280 | |
| 5281 | char buf[128]; |
| 5282 | int i=0; |
| 5283 | while (len > 0 && *s != 0 && i < 126) { |
| 5284 | if (*s > 255) { |
| 5285 | return false; |
| 5286 | } |
| 5287 | buf[i++] = *s++; |
| 5288 | len--; |
| 5289 | } |
| 5290 | |
| 5291 | if (len > 0) { |
| 5292 | return false; |
| 5293 | } |
Torne (Richard Coles) | 46a807f | 2014-08-27 12:36:44 +0100 | [diff] [blame] | 5294 | if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.' && buf[0] != '-' && buf[0] != '+') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | return false; |
| 5296 | } |
| 5297 | |
| 5298 | buf[i] = 0; |
| 5299 | const char* end; |
| 5300 | float f = strtof(buf, (char**)&end); |
| 5301 | |
| 5302 | if (*end != 0 && !isspace((unsigned char)*end)) { |
| 5303 | // Might be a unit... |
| 5304 | float scale; |
| 5305 | if (parse_unit(end, outValue, &scale, &end)) { |
| 5306 | f *= scale; |
| 5307 | const bool neg = f < 0; |
| 5308 | if (neg) f = -f; |
| 5309 | uint64_t bits = (uint64_t)(f*(1<<23)+.5f); |
| 5310 | uint32_t radix; |
| 5311 | uint32_t shift; |
| 5312 | if ((bits&0x7fffff) == 0) { |
| 5313 | // Always use 23p0 if there is no fraction, just to make |
| 5314 | // things easier to read. |
| 5315 | radix = Res_value::COMPLEX_RADIX_23p0; |
| 5316 | shift = 23; |
| 5317 | } else if ((bits&0xffffffffff800000LL) == 0) { |
| 5318 | // Magnitude is zero -- can fit in 0 bits of precision. |
| 5319 | radix = Res_value::COMPLEX_RADIX_0p23; |
| 5320 | shift = 0; |
| 5321 | } else if ((bits&0xffffffff80000000LL) == 0) { |
| 5322 | // Magnitude can fit in 8 bits of precision. |
| 5323 | radix = Res_value::COMPLEX_RADIX_8p15; |
| 5324 | shift = 8; |
| 5325 | } else if ((bits&0xffffff8000000000LL) == 0) { |
| 5326 | // Magnitude can fit in 16 bits of precision. |
| 5327 | radix = Res_value::COMPLEX_RADIX_16p7; |
| 5328 | shift = 16; |
| 5329 | } else { |
| 5330 | // Magnitude needs entire range, so no fractional part. |
| 5331 | radix = Res_value::COMPLEX_RADIX_23p0; |
| 5332 | shift = 23; |
| 5333 | } |
| 5334 | int32_t mantissa = (int32_t)( |
| 5335 | (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK); |
| 5336 | if (neg) { |
| 5337 | mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK; |
| 5338 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5339 | outValue->data |= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5340 | (radix<<Res_value::COMPLEX_RADIX_SHIFT) |
| 5341 | | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT); |
| 5342 | //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n", |
| 5343 | // f * (neg ? -1 : 1), bits, f*(1<<23), |
| 5344 | // radix, shift, outValue->data); |
| 5345 | return true; |
| 5346 | } |
| 5347 | return false; |
| 5348 | } |
| 5349 | |
| 5350 | while (*end != 0 && isspace((unsigned char)*end)) { |
| 5351 | end++; |
| 5352 | } |
| 5353 | |
| 5354 | if (*end == 0) { |
| 5355 | if (outValue) { |
| 5356 | outValue->dataType = outValue->TYPE_FLOAT; |
| 5357 | *(float*)(&outValue->data) = f; |
| 5358 | return true; |
| 5359 | } |
| 5360 | } |
| 5361 | |
| 5362 | return false; |
| 5363 | } |
| 5364 | |
| 5365 | bool ResTable::stringToValue(Res_value* outValue, String16* outString, |
| 5366 | const char16_t* s, size_t len, |
| 5367 | bool preserveSpaces, bool coerceType, |
| 5368 | uint32_t attrID, |
| 5369 | const String16* defType, |
| 5370 | const String16* defPackage, |
| 5371 | Accessor* accessor, |
| 5372 | void* accessorCookie, |
| 5373 | uint32_t attrType, |
| 5374 | bool enforcePrivate) const |
| 5375 | { |
| 5376 | bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting(); |
| 5377 | const char* errorMsg = NULL; |
| 5378 | |
| 5379 | outValue->size = sizeof(Res_value); |
| 5380 | outValue->res0 = 0; |
| 5381 | |
| 5382 | // First strip leading/trailing whitespace. Do this before handling |
| 5383 | // escapes, so they can be used to force whitespace into the string. |
| 5384 | if (!preserveSpaces) { |
| 5385 | while (len > 0 && isspace16(*s)) { |
| 5386 | s++; |
| 5387 | len--; |
| 5388 | } |
| 5389 | while (len > 0 && isspace16(s[len-1])) { |
| 5390 | len--; |
| 5391 | } |
| 5392 | // If the string ends with '\', then we keep the space after it. |
| 5393 | if (len > 0 && s[len-1] == '\\' && s[len] != 0) { |
| 5394 | len++; |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | //printf("Value for: %s\n", String8(s, len).string()); |
| 5399 | |
| 5400 | uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED; |
| 5401 | uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff; |
| 5402 | bool fromAccessor = false; |
| 5403 | if (attrID != 0 && !Res_INTERNALID(attrID)) { |
| 5404 | const ssize_t p = getResourcePackageIndex(attrID); |
| 5405 | const bag_entry* bag; |
| 5406 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 5407 | //printf("For attr 0x%08x got bag of %d\n", attrID, cnt); |
| 5408 | if (cnt >= 0) { |
| 5409 | while (cnt > 0) { |
| 5410 | //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data); |
| 5411 | switch (bag->map.name.ident) { |
| 5412 | case ResTable_map::ATTR_TYPE: |
| 5413 | attrType = bag->map.value.data; |
| 5414 | break; |
| 5415 | case ResTable_map::ATTR_MIN: |
| 5416 | attrMin = bag->map.value.data; |
| 5417 | break; |
| 5418 | case ResTable_map::ATTR_MAX: |
| 5419 | attrMax = bag->map.value.data; |
| 5420 | break; |
| 5421 | case ResTable_map::ATTR_L10N: |
| 5422 | l10nReq = bag->map.value.data; |
| 5423 | break; |
| 5424 | } |
| 5425 | bag++; |
| 5426 | cnt--; |
| 5427 | } |
| 5428 | unlockBag(bag); |
| 5429 | } else if (accessor && accessor->getAttributeType(attrID, &attrType)) { |
| 5430 | fromAccessor = true; |
| 5431 | if (attrType == ResTable_map::TYPE_ENUM |
| 5432 | || attrType == ResTable_map::TYPE_FLAGS |
| 5433 | || attrType == ResTable_map::TYPE_INTEGER) { |
| 5434 | accessor->getAttributeMin(attrID, &attrMin); |
| 5435 | accessor->getAttributeMax(attrID, &attrMax); |
| 5436 | } |
| 5437 | if (localizationSetting) { |
| 5438 | l10nReq = accessor->getAttributeL10N(attrID); |
| 5439 | } |
| 5440 | } |
| 5441 | } |
| 5442 | |
| 5443 | const bool canStringCoerce = |
| 5444 | coerceType && (attrType&ResTable_map::TYPE_STRING) != 0; |
| 5445 | |
| 5446 | if (*s == '@') { |
| 5447 | outValue->dataType = outValue->TYPE_REFERENCE; |
| 5448 | |
| 5449 | // Note: we don't check attrType here because the reference can |
| 5450 | // be to any other type; we just need to count on the client making |
| 5451 | // sure the referenced type is correct. |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5453 | //printf("Looking up ref: %s\n", String8(s, len).string()); |
| 5454 | |
| 5455 | // It's a reference! |
| 5456 | if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') { |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 5457 | // Special case @null as undefined. This will be converted by |
| 5458 | // AssetManager to TYPE_NULL with data DATA_NULL_UNDEFINED. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5459 | outValue->data = 0; |
| 5460 | return true; |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 5461 | } else if (len == 6 && s[1]=='e' && s[2]=='m' && s[3]=='p' && s[4]=='t' && s[5]=='y') { |
| 5462 | // Special case @empty as explicitly defined empty value. |
| 5463 | outValue->dataType = Res_value::TYPE_NULL; |
| 5464 | outValue->data = Res_value::DATA_NULL_EMPTY; |
| 5465 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5466 | } else { |
| 5467 | bool createIfNotFound = false; |
| 5468 | const char16_t* resourceRefName; |
| 5469 | int resourceNameLen; |
| 5470 | if (len > 2 && s[1] == '+') { |
| 5471 | createIfNotFound = true; |
| 5472 | resourceRefName = s + 2; |
| 5473 | resourceNameLen = len - 2; |
| 5474 | } else if (len > 2 && s[1] == '*') { |
| 5475 | enforcePrivate = false; |
| 5476 | resourceRefName = s + 2; |
| 5477 | resourceNameLen = len - 2; |
| 5478 | } else { |
| 5479 | createIfNotFound = false; |
| 5480 | resourceRefName = s + 1; |
| 5481 | resourceNameLen = len - 1; |
| 5482 | } |
| 5483 | String16 package, type, name; |
| 5484 | if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name, |
| 5485 | defType, defPackage, &errorMsg)) { |
| 5486 | if (accessor != NULL) { |
| 5487 | accessor->reportError(accessorCookie, errorMsg); |
| 5488 | } |
| 5489 | return false; |
| 5490 | } |
| 5491 | |
| 5492 | uint32_t specFlags = 0; |
| 5493 | uint32_t rid = identifierForName(name.string(), name.size(), type.string(), |
| 5494 | type.size(), package.string(), package.size(), &specFlags); |
| 5495 | if (rid != 0) { |
| 5496 | if (enforcePrivate) { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 5497 | if (accessor == NULL || accessor->getAssetsPackage() != package) { |
| 5498 | if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 5499 | if (accessor != NULL) { |
| 5500 | accessor->reportError(accessorCookie, "Resource is not public."); |
| 5501 | } |
| 5502 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5503 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | } |
| 5505 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 5506 | |
| 5507 | if (accessor) { |
| 5508 | rid = Res_MAKEID( |
| 5509 | accessor->getRemappedPackage(Res_GETPACKAGE(rid)), |
| 5510 | Res_GETTYPE(rid), Res_GETENTRY(rid)); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 5511 | if (kDebugTableNoisy) { |
| 5512 | ALOGI("Incl %s:%s/%s: 0x%08x\n", |
| 5513 | String8(package).string(), String8(type).string(), |
| 5514 | String8(name).string(), rid); |
| 5515 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5516 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 5517 | |
| 5518 | uint32_t packageId = Res_GETPACKAGE(rid) + 1; |
| 5519 | if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) { |
| 5520 | outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE; |
| 5521 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5522 | outValue->data = rid; |
| 5523 | return true; |
| 5524 | } |
| 5525 | |
| 5526 | if (accessor) { |
| 5527 | uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name, |
| 5528 | createIfNotFound); |
| 5529 | if (rid != 0) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 5530 | if (kDebugTableNoisy) { |
| 5531 | ALOGI("Pckg %s:%s/%s: 0x%08x\n", |
| 5532 | String8(package).string(), String8(type).string(), |
| 5533 | String8(name).string(), rid); |
| 5534 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 5535 | uint32_t packageId = Res_GETPACKAGE(rid) + 1; |
| 5536 | if (packageId == 0x00) { |
| 5537 | outValue->data = rid; |
| 5538 | outValue->dataType = Res_value::TYPE_DYNAMIC_REFERENCE; |
| 5539 | return true; |
| 5540 | } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) { |
| 5541 | // We accept packageId's generated as 0x01 in order to support |
| 5542 | // building the android system resources |
| 5543 | outValue->data = rid; |
| 5544 | return true; |
| 5545 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5546 | } |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | if (accessor != NULL) { |
| 5551 | accessor->reportError(accessorCookie, "No resource found that matches the given name"); |
| 5552 | } |
| 5553 | return false; |
| 5554 | } |
| 5555 | |
| 5556 | // if we got to here, and localization is required and it's not a reference, |
| 5557 | // complain and bail. |
| 5558 | if (l10nReq == ResTable_map::L10N_SUGGESTED) { |
| 5559 | if (localizationSetting) { |
| 5560 | if (accessor != NULL) { |
| 5561 | accessor->reportError(accessorCookie, "This attribute must be localized."); |
| 5562 | } |
| 5563 | } |
| 5564 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5565 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5566 | if (*s == '#') { |
| 5567 | // It's a color! Convert to an integer of the form 0xaarrggbb. |
| 5568 | uint32_t color = 0; |
| 5569 | bool error = false; |
| 5570 | if (len == 4) { |
| 5571 | outValue->dataType = outValue->TYPE_INT_COLOR_RGB4; |
| 5572 | color |= 0xFF000000; |
| 5573 | color |= get_hex(s[1], &error) << 20; |
| 5574 | color |= get_hex(s[1], &error) << 16; |
| 5575 | color |= get_hex(s[2], &error) << 12; |
| 5576 | color |= get_hex(s[2], &error) << 8; |
| 5577 | color |= get_hex(s[3], &error) << 4; |
| 5578 | color |= get_hex(s[3], &error); |
| 5579 | } else if (len == 5) { |
| 5580 | outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4; |
| 5581 | color |= get_hex(s[1], &error) << 28; |
| 5582 | color |= get_hex(s[1], &error) << 24; |
| 5583 | color |= get_hex(s[2], &error) << 20; |
| 5584 | color |= get_hex(s[2], &error) << 16; |
| 5585 | color |= get_hex(s[3], &error) << 12; |
| 5586 | color |= get_hex(s[3], &error) << 8; |
| 5587 | color |= get_hex(s[4], &error) << 4; |
| 5588 | color |= get_hex(s[4], &error); |
| 5589 | } else if (len == 7) { |
| 5590 | outValue->dataType = outValue->TYPE_INT_COLOR_RGB8; |
| 5591 | color |= 0xFF000000; |
| 5592 | color |= get_hex(s[1], &error) << 20; |
| 5593 | color |= get_hex(s[2], &error) << 16; |
| 5594 | color |= get_hex(s[3], &error) << 12; |
| 5595 | color |= get_hex(s[4], &error) << 8; |
| 5596 | color |= get_hex(s[5], &error) << 4; |
| 5597 | color |= get_hex(s[6], &error); |
| 5598 | } else if (len == 9) { |
| 5599 | outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8; |
| 5600 | color |= get_hex(s[1], &error) << 28; |
| 5601 | color |= get_hex(s[2], &error) << 24; |
| 5602 | color |= get_hex(s[3], &error) << 20; |
| 5603 | color |= get_hex(s[4], &error) << 16; |
| 5604 | color |= get_hex(s[5], &error) << 12; |
| 5605 | color |= get_hex(s[6], &error) << 8; |
| 5606 | color |= get_hex(s[7], &error) << 4; |
| 5607 | color |= get_hex(s[8], &error); |
| 5608 | } else { |
| 5609 | error = true; |
| 5610 | } |
| 5611 | if (!error) { |
| 5612 | if ((attrType&ResTable_map::TYPE_COLOR) == 0) { |
| 5613 | if (!canStringCoerce) { |
| 5614 | if (accessor != NULL) { |
| 5615 | accessor->reportError(accessorCookie, |
| 5616 | "Color types not allowed"); |
| 5617 | } |
| 5618 | return false; |
| 5619 | } |
| 5620 | } else { |
| 5621 | outValue->data = color; |
| 5622 | //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color); |
| 5623 | return true; |
| 5624 | } |
| 5625 | } else { |
| 5626 | if ((attrType&ResTable_map::TYPE_COLOR) != 0) { |
| 5627 | if (accessor != NULL) { |
| 5628 | accessor->reportError(accessorCookie, "Color value not valid --" |
| 5629 | " must be #rgb, #argb, #rrggbb, or #aarrggbb"); |
| 5630 | } |
| 5631 | #if 0 |
| 5632 | fprintf(stderr, "%s: Color ID %s value %s is not valid\n", |
| 5633 | "Resource File", //(const char*)in->getPrintableSource(), |
| 5634 | String8(*curTag).string(), |
| 5635 | String8(s, len).string()); |
| 5636 | #endif |
| 5637 | return false; |
| 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | |
| 5642 | if (*s == '?') { |
| 5643 | outValue->dataType = outValue->TYPE_ATTRIBUTE; |
| 5644 | |
| 5645 | // Note: we don't check attrType here because the reference can |
| 5646 | // be to any other type; we just need to count on the client making |
| 5647 | // sure the referenced type is correct. |
| 5648 | |
| 5649 | //printf("Looking up attr: %s\n", String8(s, len).string()); |
| 5650 | |
| 5651 | static const String16 attr16("attr"); |
| 5652 | String16 package, type, name; |
| 5653 | if (!expandResourceRef(s+1, len-1, &package, &type, &name, |
| 5654 | &attr16, defPackage, &errorMsg)) { |
| 5655 | if (accessor != NULL) { |
| 5656 | accessor->reportError(accessorCookie, errorMsg); |
| 5657 | } |
| 5658 | return false; |
| 5659 | } |
| 5660 | |
| 5661 | //printf("Pkg: %s, Type: %s, Name: %s\n", |
| 5662 | // String8(package).string(), String8(type).string(), |
| 5663 | // String8(name).string()); |
| 5664 | uint32_t specFlags = 0; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5665 | uint32_t rid = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5666 | identifierForName(name.string(), name.size(), |
| 5667 | type.string(), type.size(), |
| 5668 | package.string(), package.size(), &specFlags); |
| 5669 | if (rid != 0) { |
| 5670 | if (enforcePrivate) { |
| 5671 | if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 5672 | if (accessor != NULL) { |
| 5673 | accessor->reportError(accessorCookie, "Attribute is not public."); |
| 5674 | } |
| 5675 | return false; |
| 5676 | } |
| 5677 | } |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 5678 | |
| 5679 | if (accessor) { |
| 5680 | rid = Res_MAKEID( |
| 5681 | accessor->getRemappedPackage(Res_GETPACKAGE(rid)), |
| 5682 | Res_GETTYPE(rid), Res_GETENTRY(rid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5683 | } |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 5684 | |
| 5685 | uint32_t packageId = Res_GETPACKAGE(rid) + 1; |
| 5686 | if (packageId != APP_PACKAGE_ID && packageId != SYS_PACKAGE_ID) { |
| 5687 | outValue->dataType = Res_value::TYPE_DYNAMIC_ATTRIBUTE; |
| 5688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5689 | outValue->data = rid; |
| 5690 | return true; |
| 5691 | } |
| 5692 | |
| 5693 | if (accessor) { |
| 5694 | uint32_t rid = accessor->getCustomResource(package, type, name); |
| 5695 | if (rid != 0) { |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 5696 | uint32_t packageId = Res_GETPACKAGE(rid) + 1; |
| 5697 | if (packageId == 0x00) { |
| 5698 | outValue->data = rid; |
| 5699 | outValue->dataType = Res_value::TYPE_DYNAMIC_ATTRIBUTE; |
| 5700 | return true; |
| 5701 | } else if (packageId == APP_PACKAGE_ID || packageId == SYS_PACKAGE_ID) { |
| 5702 | // We accept packageId's generated as 0x01 in order to support |
| 5703 | // building the android system resources |
| 5704 | outValue->data = rid; |
| 5705 | return true; |
| 5706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | if (accessor != NULL) { |
| 5711 | accessor->reportError(accessorCookie, "No resource found that matches the given name"); |
| 5712 | } |
| 5713 | return false; |
| 5714 | } |
| 5715 | |
| 5716 | if (stringToInt(s, len, outValue)) { |
| 5717 | if ((attrType&ResTable_map::TYPE_INTEGER) == 0) { |
| 5718 | // If this type does not allow integers, but does allow floats, |
| 5719 | // fall through on this error case because the float type should |
| 5720 | // be able to accept any integer value. |
| 5721 | if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) { |
| 5722 | if (accessor != NULL) { |
| 5723 | accessor->reportError(accessorCookie, "Integer types not allowed"); |
| 5724 | } |
| 5725 | return false; |
| 5726 | } |
| 5727 | } else { |
| 5728 | if (((int32_t)outValue->data) < ((int32_t)attrMin) |
| 5729 | || ((int32_t)outValue->data) > ((int32_t)attrMax)) { |
| 5730 | if (accessor != NULL) { |
| 5731 | accessor->reportError(accessorCookie, "Integer value out of range"); |
| 5732 | } |
| 5733 | return false; |
| 5734 | } |
| 5735 | return true; |
| 5736 | } |
| 5737 | } |
| 5738 | |
| 5739 | if (stringToFloat(s, len, outValue)) { |
| 5740 | if (outValue->dataType == Res_value::TYPE_DIMENSION) { |
| 5741 | if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) { |
| 5742 | return true; |
| 5743 | } |
| 5744 | if (!canStringCoerce) { |
| 5745 | if (accessor != NULL) { |
| 5746 | accessor->reportError(accessorCookie, "Dimension types not allowed"); |
| 5747 | } |
| 5748 | return false; |
| 5749 | } |
| 5750 | } else if (outValue->dataType == Res_value::TYPE_FRACTION) { |
| 5751 | if ((attrType&ResTable_map::TYPE_FRACTION) != 0) { |
| 5752 | return true; |
| 5753 | } |
| 5754 | if (!canStringCoerce) { |
| 5755 | if (accessor != NULL) { |
| 5756 | accessor->reportError(accessorCookie, "Fraction types not allowed"); |
| 5757 | } |
| 5758 | return false; |
| 5759 | } |
| 5760 | } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) { |
| 5761 | if (!canStringCoerce) { |
| 5762 | if (accessor != NULL) { |
| 5763 | accessor->reportError(accessorCookie, "Float types not allowed"); |
| 5764 | } |
| 5765 | return false; |
| 5766 | } |
| 5767 | } else { |
| 5768 | return true; |
| 5769 | } |
| 5770 | } |
| 5771 | |
| 5772 | if (len == 4) { |
| 5773 | if ((s[0] == 't' || s[0] == 'T') && |
| 5774 | (s[1] == 'r' || s[1] == 'R') && |
| 5775 | (s[2] == 'u' || s[2] == 'U') && |
| 5776 | (s[3] == 'e' || s[3] == 'E')) { |
| 5777 | if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { |
| 5778 | if (!canStringCoerce) { |
| 5779 | if (accessor != NULL) { |
| 5780 | accessor->reportError(accessorCookie, "Boolean types not allowed"); |
| 5781 | } |
| 5782 | return false; |
| 5783 | } |
| 5784 | } else { |
| 5785 | outValue->dataType = outValue->TYPE_INT_BOOLEAN; |
| 5786 | outValue->data = (uint32_t)-1; |
| 5787 | return true; |
| 5788 | } |
| 5789 | } |
| 5790 | } |
| 5791 | |
| 5792 | if (len == 5) { |
| 5793 | if ((s[0] == 'f' || s[0] == 'F') && |
| 5794 | (s[1] == 'a' || s[1] == 'A') && |
| 5795 | (s[2] == 'l' || s[2] == 'L') && |
| 5796 | (s[3] == 's' || s[3] == 'S') && |
| 5797 | (s[4] == 'e' || s[4] == 'E')) { |
| 5798 | if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { |
| 5799 | if (!canStringCoerce) { |
| 5800 | if (accessor != NULL) { |
| 5801 | accessor->reportError(accessorCookie, "Boolean types not allowed"); |
| 5802 | } |
| 5803 | return false; |
| 5804 | } |
| 5805 | } else { |
| 5806 | outValue->dataType = outValue->TYPE_INT_BOOLEAN; |
| 5807 | outValue->data = 0; |
| 5808 | return true; |
| 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | |
| 5813 | if ((attrType&ResTable_map::TYPE_ENUM) != 0) { |
| 5814 | const ssize_t p = getResourcePackageIndex(attrID); |
| 5815 | const bag_entry* bag; |
| 5816 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 5817 | //printf("Got %d for enum\n", cnt); |
| 5818 | if (cnt >= 0) { |
| 5819 | resource_name rname; |
| 5820 | while (cnt > 0) { |
| 5821 | if (!Res_INTERNALID(bag->map.name.ident)) { |
| 5822 | //printf("Trying attr #%08x\n", bag->map.name.ident); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5823 | if (getResourceName(bag->map.name.ident, false, &rname)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5824 | #if 0 |
| 5825 | printf("Matching %s against %s (0x%08x)\n", |
| 5826 | String8(s, len).string(), |
| 5827 | String8(rname.name, rname.nameLen).string(), |
| 5828 | bag->map.name.ident); |
| 5829 | #endif |
| 5830 | if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) { |
| 5831 | outValue->dataType = bag->map.value.dataType; |
| 5832 | outValue->data = bag->map.value.data; |
| 5833 | unlockBag(bag); |
| 5834 | return true; |
| 5835 | } |
| 5836 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 5837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5838 | } |
| 5839 | bag++; |
| 5840 | cnt--; |
| 5841 | } |
| 5842 | unlockBag(bag); |
| 5843 | } |
| 5844 | |
| 5845 | if (fromAccessor) { |
| 5846 | if (accessor->getAttributeEnum(attrID, s, len, outValue)) { |
| 5847 | return true; |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | if ((attrType&ResTable_map::TYPE_FLAGS) != 0) { |
| 5853 | const ssize_t p = getResourcePackageIndex(attrID); |
| 5854 | const bag_entry* bag; |
| 5855 | ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; |
| 5856 | //printf("Got %d for flags\n", cnt); |
| 5857 | if (cnt >= 0) { |
| 5858 | bool failed = false; |
| 5859 | resource_name rname; |
| 5860 | outValue->dataType = Res_value::TYPE_INT_HEX; |
| 5861 | outValue->data = 0; |
| 5862 | const char16_t* end = s + len; |
| 5863 | const char16_t* pos = s; |
| 5864 | while (pos < end && !failed) { |
| 5865 | const char16_t* start = pos; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5866 | pos++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5867 | while (pos < end && *pos != '|') { |
| 5868 | pos++; |
| 5869 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5870 | //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] | 5871 | const bag_entry* bagi = bag; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5872 | ssize_t i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5873 | for (i=0; i<cnt; i++, bagi++) { |
| 5874 | if (!Res_INTERNALID(bagi->map.name.ident)) { |
| 5875 | //printf("Trying attr #%08x\n", bagi->map.name.ident); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 5876 | if (getResourceName(bagi->map.name.ident, false, &rname)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5877 | #if 0 |
| 5878 | printf("Matching %s against %s (0x%08x)\n", |
| 5879 | String8(start,pos-start).string(), |
| 5880 | String8(rname.name, rname.nameLen).string(), |
| 5881 | bagi->map.name.ident); |
| 5882 | #endif |
| 5883 | if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) { |
| 5884 | outValue->data |= bagi->map.value.data; |
| 5885 | break; |
| 5886 | } |
| 5887 | } |
| 5888 | } |
| 5889 | } |
| 5890 | if (i >= cnt) { |
| 5891 | // Didn't find this flag identifier. |
| 5892 | failed = true; |
| 5893 | } |
| 5894 | if (pos < end) { |
| 5895 | pos++; |
| 5896 | } |
| 5897 | } |
| 5898 | unlockBag(bag); |
| 5899 | if (!failed) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5900 | //printf("Final flag value: 0x%lx\n", outValue->data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5901 | return true; |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | |
| 5906 | if (fromAccessor) { |
| 5907 | if (accessor->getAttributeFlags(attrID, s, len, outValue)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 5908 | //printf("Final flag value: 0x%lx\n", outValue->data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5909 | return true; |
| 5910 | } |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | if ((attrType&ResTable_map::TYPE_STRING) == 0) { |
| 5915 | if (accessor != NULL) { |
| 5916 | accessor->reportError(accessorCookie, "String types not allowed"); |
| 5917 | } |
| 5918 | return false; |
| 5919 | } |
| 5920 | |
| 5921 | // Generic string handling... |
| 5922 | outValue->dataType = outValue->TYPE_STRING; |
| 5923 | if (outString) { |
| 5924 | bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg); |
| 5925 | if (accessor != NULL) { |
| 5926 | accessor->reportError(accessorCookie, errorMsg); |
| 5927 | } |
| 5928 | return failed; |
| 5929 | } |
| 5930 | |
| 5931 | return true; |
| 5932 | } |
| 5933 | |
| 5934 | bool ResTable::collectString(String16* outString, |
| 5935 | const char16_t* s, size_t len, |
| 5936 | bool preserveSpaces, |
| 5937 | const char** outErrorMsg, |
| 5938 | bool append) |
| 5939 | { |
| 5940 | String16 tmp; |
| 5941 | |
| 5942 | char quoted = 0; |
| 5943 | const char16_t* p = s; |
| 5944 | while (p < (s+len)) { |
| 5945 | while (p < (s+len)) { |
| 5946 | const char16_t c = *p; |
| 5947 | if (c == '\\') { |
| 5948 | break; |
| 5949 | } |
| 5950 | if (!preserveSpaces) { |
| 5951 | if (quoted == 0 && isspace16(c) |
| 5952 | && (c != ' ' || isspace16(*(p+1)))) { |
| 5953 | break; |
| 5954 | } |
| 5955 | if (c == '"' && (quoted == 0 || quoted == '"')) { |
| 5956 | break; |
| 5957 | } |
| 5958 | if (c == '\'' && (quoted == 0 || quoted == '\'')) { |
Eric Fischer | c87d252 | 2009-09-01 15:20:30 -0700 | [diff] [blame] | 5959 | /* |
| 5960 | * In practice, when people write ' instead of \' |
| 5961 | * in a string, they are doing it by accident |
| 5962 | * instead of really meaning to use ' as a quoting |
| 5963 | * character. Warn them so they don't lose it. |
| 5964 | */ |
| 5965 | if (outErrorMsg) { |
| 5966 | *outErrorMsg = "Apostrophe not preceded by \\"; |
| 5967 | } |
| 5968 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5969 | } |
| 5970 | } |
| 5971 | p++; |
| 5972 | } |
| 5973 | if (p < (s+len)) { |
| 5974 | if (p > s) { |
| 5975 | tmp.append(String16(s, p-s)); |
| 5976 | } |
| 5977 | if (!preserveSpaces && (*p == '"' || *p == '\'')) { |
| 5978 | if (quoted == 0) { |
| 5979 | quoted = *p; |
| 5980 | } else { |
| 5981 | quoted = 0; |
| 5982 | } |
| 5983 | p++; |
| 5984 | } else if (!preserveSpaces && isspace16(*p)) { |
| 5985 | // Space outside of a quote -- consume all spaces and |
| 5986 | // leave a single plain space char. |
| 5987 | tmp.append(String16(" ")); |
| 5988 | p++; |
| 5989 | while (p < (s+len) && isspace16(*p)) { |
| 5990 | p++; |
| 5991 | } |
| 5992 | } else if (*p == '\\') { |
| 5993 | p++; |
| 5994 | if (p < (s+len)) { |
| 5995 | switch (*p) { |
| 5996 | case 't': |
| 5997 | tmp.append(String16("\t")); |
| 5998 | break; |
| 5999 | case 'n': |
| 6000 | tmp.append(String16("\n")); |
| 6001 | break; |
| 6002 | case '#': |
| 6003 | tmp.append(String16("#")); |
| 6004 | break; |
| 6005 | case '@': |
| 6006 | tmp.append(String16("@")); |
| 6007 | break; |
| 6008 | case '?': |
| 6009 | tmp.append(String16("?")); |
| 6010 | break; |
| 6011 | case '"': |
| 6012 | tmp.append(String16("\"")); |
| 6013 | break; |
| 6014 | case '\'': |
| 6015 | tmp.append(String16("'")); |
| 6016 | break; |
| 6017 | case '\\': |
| 6018 | tmp.append(String16("\\")); |
| 6019 | break; |
| 6020 | case 'u': |
| 6021 | { |
| 6022 | char16_t chr = 0; |
| 6023 | int i = 0; |
| 6024 | while (i < 4 && p[1] != 0) { |
| 6025 | p++; |
| 6026 | i++; |
| 6027 | int c; |
| 6028 | if (*p >= '0' && *p <= '9') { |
| 6029 | c = *p - '0'; |
| 6030 | } else if (*p >= 'a' && *p <= 'f') { |
| 6031 | c = *p - 'a' + 10; |
| 6032 | } else if (*p >= 'A' && *p <= 'F') { |
| 6033 | c = *p - 'A' + 10; |
| 6034 | } else { |
| 6035 | if (outErrorMsg) { |
| 6036 | *outErrorMsg = "Bad character in \\u unicode escape sequence"; |
| 6037 | } |
| 6038 | return false; |
| 6039 | } |
| 6040 | chr = (chr<<4) | c; |
| 6041 | } |
| 6042 | tmp.append(String16(&chr, 1)); |
| 6043 | } break; |
| 6044 | default: |
| 6045 | // ignore unknown escape chars. |
| 6046 | break; |
| 6047 | } |
| 6048 | p++; |
| 6049 | } |
| 6050 | } |
| 6051 | len -= (p-s); |
| 6052 | s = p; |
| 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | if (tmp.size() != 0) { |
| 6057 | if (len > 0) { |
| 6058 | tmp.append(String16(s, len)); |
| 6059 | } |
| 6060 | if (append) { |
| 6061 | outString->append(tmp); |
| 6062 | } else { |
| 6063 | outString->setTo(tmp); |
| 6064 | } |
| 6065 | } else { |
| 6066 | if (append) { |
| 6067 | outString->append(String16(s, len)); |
| 6068 | } else { |
| 6069 | outString->setTo(s, len); |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | return true; |
| 6074 | } |
| 6075 | |
| 6076 | size_t ResTable::getBasePackageCount() const |
| 6077 | { |
| 6078 | if (mError != NO_ERROR) { |
| 6079 | return 0; |
| 6080 | } |
| 6081 | return mPackageGroups.size(); |
| 6082 | } |
| 6083 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6084 | const String16 ResTable::getBasePackageName(size_t idx) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6085 | { |
| 6086 | if (mError != NO_ERROR) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6087 | return String16(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6088 | } |
| 6089 | LOG_FATAL_IF(idx >= mPackageGroups.size(), |
| 6090 | "Requested package index %d past package count %d", |
| 6091 | (int)idx, (int)mPackageGroups.size()); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6092 | return mPackageGroups[idx]->name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | uint32_t ResTable::getBasePackageId(size_t idx) const |
| 6096 | { |
| 6097 | if (mError != NO_ERROR) { |
| 6098 | return 0; |
| 6099 | } |
| 6100 | LOG_FATAL_IF(idx >= mPackageGroups.size(), |
| 6101 | "Requested package index %d past package count %d", |
| 6102 | (int)idx, (int)mPackageGroups.size()); |
| 6103 | return mPackageGroups[idx]->id; |
| 6104 | } |
| 6105 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6106 | uint32_t ResTable::getLastTypeIdForPackage(size_t idx) const |
| 6107 | { |
| 6108 | if (mError != NO_ERROR) { |
| 6109 | return 0; |
| 6110 | } |
| 6111 | LOG_FATAL_IF(idx >= mPackageGroups.size(), |
| 6112 | "Requested package index %d past package count %d", |
| 6113 | (int)idx, (int)mPackageGroups.size()); |
| 6114 | const PackageGroup* const group = mPackageGroups[idx]; |
| 6115 | return group->largestTypeId; |
| 6116 | } |
| 6117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6118 | size_t ResTable::getTableCount() const |
| 6119 | { |
| 6120 | return mHeaders.size(); |
| 6121 | } |
| 6122 | |
| 6123 | const ResStringPool* ResTable::getTableStringBlock(size_t index) const |
| 6124 | { |
| 6125 | return &mHeaders[index]->values; |
| 6126 | } |
| 6127 | |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 6128 | int32_t ResTable::getTableCookie(size_t index) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6129 | { |
| 6130 | return mHeaders[index]->cookie; |
| 6131 | } |
| 6132 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6133 | const DynamicRefTable* ResTable::getDynamicRefTableForCookie(int32_t cookie) const |
| 6134 | { |
| 6135 | const size_t N = mPackageGroups.size(); |
| 6136 | for (size_t i = 0; i < N; i++) { |
| 6137 | const PackageGroup* pg = mPackageGroups[i]; |
| 6138 | size_t M = pg->packages.size(); |
| 6139 | for (size_t j = 0; j < M; j++) { |
| 6140 | if (pg->packages[j]->header->cookie == cookie) { |
| 6141 | return &pg->dynamicRefTable; |
| 6142 | } |
| 6143 | } |
| 6144 | } |
| 6145 | return NULL; |
| 6146 | } |
| 6147 | |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 6148 | static bool compareResTableConfig(const ResTable_config& a, const ResTable_config& b) { |
| 6149 | return a.compare(b) < 0; |
| 6150 | } |
| 6151 | |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6152 | template <typename Func> |
| 6153 | void ResTable::forEachConfiguration(bool ignoreMipmap, bool ignoreAndroidPackage, |
| 6154 | bool includeSystemConfigs, const Func& f) const { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6155 | const size_t packageCount = mPackageGroups.size(); |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6156 | const String16 android("android"); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6157 | for (size_t i = 0; i < packageCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6158 | const PackageGroup* packageGroup = mPackageGroups[i]; |
Filip Gruszczynski | 2349332 | 2015-07-29 17:02:59 -0700 | [diff] [blame] | 6159 | if (ignoreAndroidPackage && android == packageGroup->name) { |
| 6160 | continue; |
| 6161 | } |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 6162 | if (!includeSystemConfigs && packageGroup->isSystemAsset) { |
| 6163 | continue; |
| 6164 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6165 | const size_t typeCount = packageGroup->types.size(); |
| 6166 | for (size_t j = 0; j < typeCount; j++) { |
| 6167 | const TypeList& typeList = packageGroup->types[j]; |
| 6168 | const size_t numTypes = typeList.size(); |
| 6169 | for (size_t k = 0; k < numTypes; k++) { |
| 6170 | const Type* type = typeList[k]; |
Adam Lesinski | 42eea27 | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 6171 | const ResStringPool& typeStrings = type->package->typeStrings; |
| 6172 | if (ignoreMipmap && typeStrings.string8ObjectAt( |
| 6173 | type->typeSpec->id - 1) == "mipmap") { |
| 6174 | continue; |
| 6175 | } |
| 6176 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6177 | const size_t numConfigs = type->configs.size(); |
| 6178 | for (size_t m = 0; m < numConfigs; m++) { |
| 6179 | const ResTable_type* config = type->configs[m]; |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 6180 | ResTable_config cfg; |
| 6181 | memset(&cfg, 0, sizeof(ResTable_config)); |
| 6182 | cfg.copyFromDtoH(config->config); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 6183 | |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6184 | f(cfg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6185 | } |
| 6186 | } |
| 6187 | } |
| 6188 | } |
| 6189 | } |
| 6190 | |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6191 | void ResTable::getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap, |
| 6192 | bool ignoreAndroidPackage, bool includeSystemConfigs) const { |
| 6193 | auto func = [&](const ResTable_config& cfg) { |
| 6194 | const auto beginIter = configs->begin(); |
| 6195 | const auto endIter = configs->end(); |
| 6196 | |
| 6197 | auto iter = std::lower_bound(beginIter, endIter, cfg, compareResTableConfig); |
| 6198 | if (iter == endIter || iter->compare(cfg) != 0) { |
| 6199 | configs->insertAt(cfg, std::distance(beginIter, iter)); |
| 6200 | } |
| 6201 | }; |
| 6202 | forEachConfiguration(ignoreMipmap, ignoreAndroidPackage, includeSystemConfigs, func); |
| 6203 | } |
| 6204 | |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 6205 | static bool compareString8AndCString(const String8& str, const char* cStr) { |
| 6206 | return strcmp(str.string(), cStr) < 0; |
| 6207 | } |
| 6208 | |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 6209 | void ResTable::getLocales(Vector<String8>* locales, bool includeSystemLocales, |
| 6210 | bool mergeEquivalentLangs) const { |
Narayan Kamath | 48620f1 | 2014-01-20 13:57:11 +0000 | [diff] [blame] | 6211 | char locale[RESTABLE_MAX_LOCALE_LEN]; |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 6212 | |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6213 | forEachConfiguration(false, false, includeSystemLocales, [&](const ResTable_config& cfg) { |
Adam Lesinski | fa2fc0b | 2017-05-11 12:15:26 -0700 | [diff] [blame] | 6214 | cfg.getBcp47Locale(locale, mergeEquivalentLangs /* canonicalize if merging */); |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6215 | |
Adam Lesinski | fa2fc0b | 2017-05-11 12:15:26 -0700 | [diff] [blame] | 6216 | const auto beginIter = locales->begin(); |
| 6217 | const auto endIter = locales->end(); |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6218 | |
Adam Lesinski | fa2fc0b | 2017-05-11 12:15:26 -0700 | [diff] [blame] | 6219 | auto iter = std::lower_bound(beginIter, endIter, locale, compareString8AndCString); |
| 6220 | if (iter == endIter || strcmp(iter->string(), locale) != 0) { |
| 6221 | locales->insertAt(String8(locale), std::distance(beginIter, iter)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6222 | } |
Adam Lesinski | 76da37e | 2016-05-19 18:25:28 -0700 | [diff] [blame] | 6223 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6224 | } |
| 6225 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6226 | StringPoolRef::StringPoolRef(const ResStringPool* pool, uint32_t index) |
| 6227 | : mPool(pool), mIndex(index) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6228 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6229 | const char* StringPoolRef::string8(size_t* outLen) const { |
| 6230 | if (mPool != NULL) { |
| 6231 | return mPool->string8At(mIndex, outLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6232 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6233 | if (outLen != NULL) { |
| 6234 | *outLen = 0; |
| 6235 | } |
| 6236 | return NULL; |
| 6237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6238 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6239 | const char16_t* StringPoolRef::string16(size_t* outLen) const { |
| 6240 | if (mPool != NULL) { |
| 6241 | return mPool->stringAt(mIndex, outLen); |
| 6242 | } |
| 6243 | if (outLen != NULL) { |
| 6244 | *outLen = 0; |
| 6245 | } |
| 6246 | return NULL; |
| 6247 | } |
| 6248 | |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 6249 | bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const { |
| 6250 | if (mError != NO_ERROR) { |
| 6251 | return false; |
| 6252 | } |
| 6253 | |
| 6254 | const ssize_t p = getResourcePackageIndex(resID); |
| 6255 | const int t = Res_GETTYPE(resID); |
| 6256 | const int e = Res_GETENTRY(resID); |
| 6257 | |
| 6258 | if (p < 0) { |
| 6259 | if (Res_GETPACKAGE(resID)+1 == 0) { |
| 6260 | ALOGW("No package identifier when getting flags for resource number 0x%08x", resID); |
| 6261 | } else { |
| 6262 | ALOGW("No known package when getting flags for resource number 0x%08x", resID); |
| 6263 | } |
| 6264 | return false; |
| 6265 | } |
| 6266 | if (t < 0) { |
| 6267 | ALOGW("No type identifier when getting flags for resource number 0x%08x", resID); |
| 6268 | return false; |
| 6269 | } |
| 6270 | |
| 6271 | const PackageGroup* const grp = mPackageGroups[p]; |
| 6272 | if (grp == NULL) { |
| 6273 | ALOGW("Bad identifier when getting flags for resource number 0x%08x", resID); |
| 6274 | return false; |
| 6275 | } |
| 6276 | |
| 6277 | Entry entry; |
| 6278 | status_t err = getEntry(grp, t, e, NULL, &entry); |
| 6279 | if (err != NO_ERROR) { |
| 6280 | return false; |
| 6281 | } |
| 6282 | |
| 6283 | *outFlags = entry.specFlags; |
| 6284 | return true; |
| 6285 | } |
| 6286 | |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 6287 | bool ResTable::isPackageDynamic(uint8_t packageID) const { |
| 6288 | if (mError != NO_ERROR) { |
| 6289 | return false; |
| 6290 | } |
| 6291 | if (packageID == 0) { |
| 6292 | ALOGW("Invalid package number 0x%08x", packageID); |
| 6293 | return false; |
| 6294 | } |
| 6295 | |
| 6296 | const ssize_t p = getResourcePackageIndexFromPackage(packageID); |
| 6297 | |
| 6298 | if (p < 0) { |
| 6299 | ALOGW("Unknown package number 0x%08x", packageID); |
| 6300 | return false; |
| 6301 | } |
| 6302 | |
| 6303 | const PackageGroup* const grp = mPackageGroups[p]; |
| 6304 | if (grp == NULL) { |
| 6305 | ALOGW("Bad identifier for package number 0x%08x", packageID); |
| 6306 | return false; |
| 6307 | } |
| 6308 | |
| 6309 | return grp->isDynamic; |
| 6310 | } |
| 6311 | |
| 6312 | bool ResTable::isResourceDynamic(uint32_t resID) const { |
| 6313 | if (mError != NO_ERROR) { |
| 6314 | return false; |
| 6315 | } |
| 6316 | |
| 6317 | const ssize_t p = getResourcePackageIndex(resID); |
| 6318 | const int t = Res_GETTYPE(resID); |
| 6319 | const int e = Res_GETENTRY(resID); |
| 6320 | |
| 6321 | if (p < 0) { |
| 6322 | if (Res_GETPACKAGE(resID)+1 == 0) { |
| 6323 | ALOGW("No package identifier for resource number 0x%08x", resID); |
| 6324 | } else { |
| 6325 | ALOGW("No known package for resource number 0x%08x", resID); |
| 6326 | } |
| 6327 | return false; |
| 6328 | } |
| 6329 | if (t < 0) { |
| 6330 | ALOGW("No type identifier for resource number 0x%08x", resID); |
| 6331 | return false; |
| 6332 | } |
| 6333 | |
| 6334 | const PackageGroup* const grp = mPackageGroups[p]; |
| 6335 | if (grp == NULL) { |
| 6336 | ALOGW("Bad identifier for resource number 0x%08x", resID); |
| 6337 | return false; |
| 6338 | } |
| 6339 | |
| 6340 | Entry entry; |
| 6341 | status_t err = getEntry(grp, t, e, NULL, &entry); |
| 6342 | if (err != NO_ERROR) { |
| 6343 | return false; |
| 6344 | } |
| 6345 | |
| 6346 | return grp->isDynamic; |
| 6347 | } |
| 6348 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 6349 | static bool keyCompare(const ResTable_sparseTypeEntry& entry , uint16_t entryIdx) { |
| 6350 | return dtohs(entry.idx) < entryIdx; |
| 6351 | } |
| 6352 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6353 | status_t ResTable::getEntry( |
| 6354 | const PackageGroup* packageGroup, int typeIndex, int entryIndex, |
| 6355 | const ResTable_config* config, |
| 6356 | Entry* outEntry) const |
| 6357 | { |
| 6358 | const TypeList& typeList = packageGroup->types[typeIndex]; |
| 6359 | if (typeList.isEmpty()) { |
| 6360 | 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] | 6361 | return BAD_TYPE; |
| 6362 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6363 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6364 | const ResTable_type* bestType = NULL; |
| 6365 | uint32_t bestOffset = ResTable_type::NO_ENTRY; |
| 6366 | const Package* bestPackage = NULL; |
| 6367 | uint32_t specFlags = 0; |
| 6368 | uint8_t actualTypeIndex = typeIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6369 | ResTable_config bestConfig; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6370 | memset(&bestConfig, 0, sizeof(bestConfig)); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6371 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6372 | // Iterate over the Types of each package. |
| 6373 | const size_t typeCount = typeList.size(); |
| 6374 | for (size_t i = 0; i < typeCount; i++) { |
| 6375 | const Type* const typeSpec = typeList[i]; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6376 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6377 | int realEntryIndex = entryIndex; |
| 6378 | int realTypeIndex = typeIndex; |
| 6379 | bool currentTypeIsOverlay = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6380 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6381 | // Runtime overlay packages provide a mapping of app resource |
| 6382 | // ID to package resource ID. |
| 6383 | if (typeSpec->idmapEntries.hasEntries()) { |
| 6384 | uint16_t overlayEntryIndex; |
| 6385 | if (typeSpec->idmapEntries.lookup(entryIndex, &overlayEntryIndex) != NO_ERROR) { |
| 6386 | // No such mapping exists |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6387 | continue; |
| 6388 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6389 | realEntryIndex = overlayEntryIndex; |
| 6390 | realTypeIndex = typeSpec->idmapEntries.overlayTypeId() - 1; |
| 6391 | currentTypeIsOverlay = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6392 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6393 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 6394 | // Check that the entry idx is within range of the declared entry count (ResTable_typeSpec). |
| 6395 | // Particular types (ResTable_type) may be encoded with sparse entries, and so their |
| 6396 | // entryCount do not need to match. |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6397 | if (static_cast<size_t>(realEntryIndex) >= typeSpec->entryCount) { |
| 6398 | ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)", |
| 6399 | Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex), |
| 6400 | entryIndex, static_cast<int>(typeSpec->entryCount)); |
| 6401 | // We should normally abort here, but some legacy apps declare |
| 6402 | // resources in the 'android' package (old bug in AAPT). |
| 6403 | continue; |
| 6404 | } |
| 6405 | |
| 6406 | // Aggregate all the flags for each package that defines this entry. |
| 6407 | if (typeSpec->typeSpecFlags != NULL) { |
| 6408 | specFlags |= dtohl(typeSpec->typeSpecFlags[realEntryIndex]); |
| 6409 | } else { |
| 6410 | specFlags = -1; |
| 6411 | } |
| 6412 | |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 6413 | const Vector<const ResTable_type*>* candidateConfigs = &typeSpec->configs; |
| 6414 | |
| 6415 | std::shared_ptr<Vector<const ResTable_type*>> filteredConfigs; |
| 6416 | if (config && memcmp(&mParams, config, sizeof(mParams)) == 0) { |
| 6417 | // Grab the lock first so we can safely get the current filtered list. |
| 6418 | AutoMutex _lock(mFilteredConfigLock); |
| 6419 | |
| 6420 | // This configuration is equal to the one we have previously cached for, |
| 6421 | // so use the filtered configs. |
| 6422 | |
| 6423 | const TypeCacheEntry& cacheEntry = packageGroup->typeCacheEntries[typeIndex]; |
| 6424 | if (i < cacheEntry.filteredConfigs.size()) { |
| 6425 | if (cacheEntry.filteredConfigs[i]) { |
| 6426 | // Grab a reference to the shared_ptr so it doesn't get destroyed while |
| 6427 | // going through this list. |
| 6428 | filteredConfigs = cacheEntry.filteredConfigs[i]; |
| 6429 | |
| 6430 | // Use this filtered list. |
| 6431 | candidateConfigs = filteredConfigs.get(); |
| 6432 | } |
| 6433 | } |
| 6434 | } |
| 6435 | |
| 6436 | const size_t numConfigs = candidateConfigs->size(); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6437 | for (size_t c = 0; c < numConfigs; c++) { |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 6438 | const ResTable_type* const thisType = candidateConfigs->itemAt(c); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6439 | if (thisType == NULL) { |
| 6440 | continue; |
| 6441 | } |
| 6442 | |
| 6443 | ResTable_config thisConfig; |
| 6444 | thisConfig.copyFromDtoH(thisType->config); |
| 6445 | |
| 6446 | // Check to make sure this one is valid for the current parameters. |
| 6447 | if (config != NULL && !thisConfig.match(*config)) { |
| 6448 | continue; |
| 6449 | } |
| 6450 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6451 | const uint32_t* const eindex = reinterpret_cast<const uint32_t*>( |
| 6452 | reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize)); |
| 6453 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 6454 | uint32_t thisOffset; |
| 6455 | |
| 6456 | // Check if there is the desired entry in this type. |
| 6457 | if (thisType->flags & ResTable_type::FLAG_SPARSE) { |
| 6458 | // This is encoded as a sparse map, so perform a binary search. |
| 6459 | const ResTable_sparseTypeEntry* sparseIndices = |
| 6460 | reinterpret_cast<const ResTable_sparseTypeEntry*>(eindex); |
| 6461 | const ResTable_sparseTypeEntry* result = std::lower_bound( |
| 6462 | sparseIndices, sparseIndices + dtohl(thisType->entryCount), realEntryIndex, |
| 6463 | keyCompare); |
| 6464 | if (result == sparseIndices + dtohl(thisType->entryCount) |
| 6465 | || dtohs(result->idx) != realEntryIndex) { |
| 6466 | // No entry found. |
| 6467 | continue; |
| 6468 | } |
| 6469 | |
| 6470 | // Extract the offset from the entry. Each offset must be a multiple of 4 |
| 6471 | // so we store it as the real offset divided by 4. |
| 6472 | thisOffset = dtohs(result->offset) * 4u; |
| 6473 | } else { |
| 6474 | if (static_cast<uint32_t>(realEntryIndex) >= dtohl(thisType->entryCount)) { |
| 6475 | // Entry does not exist. |
| 6476 | continue; |
| 6477 | } |
| 6478 | |
| 6479 | thisOffset = dtohl(eindex[realEntryIndex]); |
| 6480 | } |
| 6481 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6482 | if (thisOffset == ResTable_type::NO_ENTRY) { |
| 6483 | // There is no entry for this index and configuration. |
| 6484 | continue; |
| 6485 | } |
| 6486 | |
| 6487 | if (bestType != NULL) { |
| 6488 | // Check if this one is less specific than the last found. If so, |
| 6489 | // we will skip it. We check starting with things we most care |
| 6490 | // about to those we least care about. |
| 6491 | if (!thisConfig.isBetterThan(bestConfig, config)) { |
| 6492 | if (!currentTypeIsOverlay || thisConfig.compare(bestConfig) != 0) { |
| 6493 | continue; |
| 6494 | } |
| 6495 | } |
| 6496 | } |
| 6497 | |
| 6498 | bestType = thisType; |
| 6499 | bestOffset = thisOffset; |
| 6500 | bestConfig = thisConfig; |
| 6501 | bestPackage = typeSpec->package; |
| 6502 | actualTypeIndex = realTypeIndex; |
| 6503 | |
| 6504 | // If no config was specified, any type will do, so skip |
| 6505 | if (config == NULL) { |
| 6506 | break; |
| 6507 | } |
| 6508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6509 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6510 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6511 | if (bestType == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6512 | return BAD_INDEX; |
| 6513 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6514 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6515 | bestOffset += dtohl(bestType->entriesStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6516 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6517 | if (bestOffset > (dtohl(bestType->header.size)-sizeof(ResTable_entry))) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6518 | ALOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6519 | bestOffset, dtohl(bestType->header.size)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6520 | return BAD_TYPE; |
| 6521 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6522 | if ((bestOffset & 0x3) != 0) { |
| 6523 | ALOGW("ResTable_entry at 0x%x is not on an integer boundary", bestOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6524 | return BAD_TYPE; |
| 6525 | } |
| 6526 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6527 | const ResTable_entry* const entry = reinterpret_cast<const ResTable_entry*>( |
| 6528 | reinterpret_cast<const uint8_t*>(bestType) + bestOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6529 | if (dtohs(entry->size) < sizeof(*entry)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6530 | 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] | 6531 | return BAD_TYPE; |
| 6532 | } |
| 6533 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6534 | if (outEntry != NULL) { |
| 6535 | outEntry->entry = entry; |
| 6536 | outEntry->config = bestConfig; |
| 6537 | outEntry->type = bestType; |
| 6538 | outEntry->specFlags = specFlags; |
| 6539 | outEntry->package = bestPackage; |
| 6540 | outEntry->typeStr = StringPoolRef(&bestPackage->typeStrings, actualTypeIndex - bestPackage->typeIdOffset); |
| 6541 | outEntry->keyStr = StringPoolRef(&bestPackage->keyStrings, dtohl(entry->key.index)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6542 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6543 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | status_t ResTable::parsePackage(const ResTable_package* const pkg, |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 6547 | const Header* const header, bool appAsLib, bool isSystemAsset) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | { |
| 6549 | const uint8_t* base = (const uint8_t*)pkg; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6550 | status_t err = validate_chunk(&pkg->header, sizeof(*pkg) - sizeof(pkg->typeIdOffset), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6551 | header->dataEnd, "ResTable_package"); |
| 6552 | if (err != NO_ERROR) { |
| 6553 | return (mError=err); |
| 6554 | } |
| 6555 | |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6556 | const uint32_t pkgSize = dtohl(pkg->header.size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6557 | |
| 6558 | if (dtohl(pkg->typeStrings) >= pkgSize) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6559 | ALOGW("ResTable_package type strings at 0x%x are past chunk size 0x%x.", |
| 6560 | dtohl(pkg->typeStrings), pkgSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6561 | return (mError=BAD_TYPE); |
| 6562 | } |
| 6563 | if ((dtohl(pkg->typeStrings)&0x3) != 0) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6564 | ALOGW("ResTable_package type strings at 0x%x is not on an integer boundary.", |
| 6565 | dtohl(pkg->typeStrings)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6566 | return (mError=BAD_TYPE); |
| 6567 | } |
| 6568 | if (dtohl(pkg->keyStrings) >= pkgSize) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6569 | ALOGW("ResTable_package key strings at 0x%x are past chunk size 0x%x.", |
| 6570 | dtohl(pkg->keyStrings), pkgSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6571 | return (mError=BAD_TYPE); |
| 6572 | } |
| 6573 | if ((dtohl(pkg->keyStrings)&0x3) != 0) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6574 | ALOGW("ResTable_package key strings at 0x%x is not on an integer boundary.", |
| 6575 | dtohl(pkg->keyStrings)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6576 | return (mError=BAD_TYPE); |
| 6577 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6578 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6579 | uint32_t id = dtohl(pkg->id); |
| 6580 | KeyedVector<uint8_t, IdmapEntries> idmapEntries; |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6581 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6582 | if (header->resourceIDMap != NULL) { |
| 6583 | uint8_t targetPackageId = 0; |
| 6584 | status_t err = parseIdmap(header->resourceIDMap, header->resourceIDMapSize, &targetPackageId, &idmapEntries); |
| 6585 | if (err != NO_ERROR) { |
| 6586 | ALOGW("Overlay is broken"); |
| 6587 | return (mError=err); |
| 6588 | } |
| 6589 | id = targetPackageId; |
| 6590 | } |
| 6591 | |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 6592 | bool isDynamic = false; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6593 | if (id >= 256) { |
| 6594 | LOG_ALWAYS_FATAL("Package id out of range"); |
| 6595 | return NO_ERROR; |
Adam Lesinski | 25f4888 | 2016-06-14 11:05:23 -0700 | [diff] [blame] | 6596 | } else if (id == 0 || (id == 0x7f && appAsLib) || isSystemAsset) { |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 6597 | // This is a library or a system asset, so assign an ID |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6598 | id = mNextPackageId++; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 6599 | isDynamic = true; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | PackageGroup* group = NULL; |
| 6603 | Package* package = new Package(this, header, pkg); |
| 6604 | if (package == NULL) { |
| 6605 | return (mError=NO_MEMORY); |
| 6606 | } |
| 6607 | |
| 6608 | err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings), |
| 6609 | header->dataEnd-(base+dtohl(pkg->typeStrings))); |
| 6610 | if (err != NO_ERROR) { |
| 6611 | delete group; |
| 6612 | delete package; |
| 6613 | return (mError=err); |
| 6614 | } |
| 6615 | |
| 6616 | err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings), |
| 6617 | header->dataEnd-(base+dtohl(pkg->keyStrings))); |
| 6618 | if (err != NO_ERROR) { |
| 6619 | delete group; |
| 6620 | delete package; |
| 6621 | return (mError=err); |
| 6622 | } |
| 6623 | |
| 6624 | size_t idx = mPackageMap[id]; |
| 6625 | if (idx == 0) { |
| 6626 | idx = mPackageGroups.size() + 1; |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 6627 | char16_t tmpName[sizeof(pkg->name)/sizeof(pkg->name[0])]; |
| 6628 | strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(pkg->name[0])); |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 6629 | group = new PackageGroup(this, String16(tmpName), id, appAsLib, isSystemAsset, isDynamic); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6630 | if (group == NULL) { |
| 6631 | delete package; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 6632 | return (mError=NO_MEMORY); |
| 6633 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 6634 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6635 | err = mPackageGroups.add(group); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6636 | if (err < NO_ERROR) { |
| 6637 | return (mError=err); |
| 6638 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6639 | |
| 6640 | mPackageMap[id] = static_cast<uint8_t>(idx); |
| 6641 | |
| 6642 | // Find all packages that reference this package |
| 6643 | size_t N = mPackageGroups.size(); |
| 6644 | for (size_t i = 0; i < N; i++) { |
| 6645 | mPackageGroups[i]->dynamicRefTable.addMapping( |
| 6646 | group->name, static_cast<uint8_t>(group->id)); |
| 6647 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6648 | } else { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6649 | group = mPackageGroups.itemAt(idx - 1); |
| 6650 | if (group == NULL) { |
| 6651 | return (mError=UNKNOWN_ERROR); |
| 6652 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6653 | } |
| 6654 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6655 | err = group->packages.add(package); |
| 6656 | if (err < NO_ERROR) { |
| 6657 | return (mError=err); |
| 6658 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | // Iterate through all chunks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6661 | const ResChunk_header* chunk = |
| 6662 | (const ResChunk_header*)(((const uint8_t*)pkg) |
| 6663 | + dtohs(pkg->header.headerSize)); |
| 6664 | const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size); |
| 6665 | while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) && |
| 6666 | ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 6667 | if (kDebugTableNoisy) { |
| 6668 | ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", |
| 6669 | dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), |
| 6670 | (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))); |
| 6671 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6672 | const size_t csize = dtohl(chunk->size); |
| 6673 | const uint16_t ctype = dtohs(chunk->type); |
| 6674 | if (ctype == RES_TABLE_TYPE_SPEC_TYPE) { |
| 6675 | const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk); |
| 6676 | err = validate_chunk(&typeSpec->header, sizeof(*typeSpec), |
| 6677 | endPos, "ResTable_typeSpec"); |
| 6678 | if (err != NO_ERROR) { |
| 6679 | return (mError=err); |
| 6680 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6682 | const size_t typeSpecSize = dtohl(typeSpec->header.size); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6683 | const size_t newEntryCount = dtohl(typeSpec->entryCount); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6684 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 6685 | if (kDebugLoadTableNoisy) { |
| 6686 | ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n", |
| 6687 | (void*)(base-(const uint8_t*)chunk), |
| 6688 | dtohs(typeSpec->header.type), |
| 6689 | dtohs(typeSpec->header.headerSize), |
| 6690 | (void*)typeSpecSize); |
| 6691 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6692 | // look for block overrun or int overflow when multiplying by 4 |
| 6693 | if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t)) |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6694 | || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6695 | > typeSpecSize)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6696 | ALOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6697 | (void*)(dtohs(typeSpec->header.headerSize) + (sizeof(uint32_t)*newEntryCount)), |
| 6698 | (void*)typeSpecSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6699 | return (mError=BAD_TYPE); |
| 6700 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6701 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6702 | if (typeSpec->id == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6703 | ALOGW("ResTable_type has an id of 0."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6704 | return (mError=BAD_TYPE); |
| 6705 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6706 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6707 | if (newEntryCount > 0) { |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6708 | bool addToType = true; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6709 | uint8_t typeIndex = typeSpec->id - 1; |
| 6710 | ssize_t idmapIndex = idmapEntries.indexOfKey(typeSpec->id); |
| 6711 | if (idmapIndex >= 0) { |
| 6712 | typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1; |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6713 | } else if (header->resourceIDMap != NULL) { |
| 6714 | // This is an overlay, but the types in this overlay are not |
| 6715 | // overlaying anything according to the idmap. We can skip these |
| 6716 | // as they will otherwise conflict with the other resources in the package |
| 6717 | // without a mapping. |
| 6718 | addToType = false; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6719 | } |
| 6720 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6721 | if (addToType) { |
| 6722 | TypeList& typeList = group->types.editItemAt(typeIndex); |
| 6723 | if (!typeList.isEmpty()) { |
| 6724 | const Type* existingType = typeList[0]; |
| 6725 | if (existingType->entryCount != newEntryCount && idmapIndex < 0) { |
| 6726 | ALOGW("ResTable_typeSpec entry count inconsistent: " |
| 6727 | "given %d, previously %d", |
| 6728 | (int) newEntryCount, (int) existingType->entryCount); |
| 6729 | // We should normally abort here, but some legacy apps declare |
| 6730 | // resources in the 'android' package (old bug in AAPT). |
| 6731 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6732 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6733 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6734 | Type* t = new Type(header, package, newEntryCount); |
| 6735 | t->typeSpec = typeSpec; |
| 6736 | t->typeSpecFlags = (const uint32_t*)( |
| 6737 | ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize)); |
| 6738 | if (idmapIndex >= 0) { |
| 6739 | t->idmapEntries = idmapEntries[idmapIndex]; |
| 6740 | } |
| 6741 | typeList.add(t); |
| 6742 | group->largestTypeId = max(group->largestTypeId, typeSpec->id); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6743 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6744 | } else { |
| 6745 | ALOGV("Skipping empty ResTable_typeSpec for type %d", typeSpec->id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6746 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6747 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6748 | } else if (ctype == RES_TABLE_TYPE_TYPE) { |
| 6749 | const ResTable_type* type = (const ResTable_type*)(chunk); |
| 6750 | err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4, |
| 6751 | endPos, "ResTable_type"); |
| 6752 | if (err != NO_ERROR) { |
| 6753 | return (mError=err); |
| 6754 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6755 | |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6756 | const uint32_t typeSize = dtohl(type->header.size); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6757 | const size_t newEntryCount = dtohl(type->entryCount); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6758 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 6759 | if (kDebugLoadTableNoisy) { |
| 6760 | printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n", |
| 6761 | (void*)(base-(const uint8_t*)chunk), |
| 6762 | dtohs(type->header.type), |
| 6763 | dtohs(type->header.headerSize), |
| 6764 | typeSize); |
| 6765 | } |
| 6766 | if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6767 | ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6768 | (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)), |
| 6769 | typeSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6770 | return (mError=BAD_TYPE); |
| 6771 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6772 | |
| 6773 | if (newEntryCount != 0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6774 | && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) { |
Patrik Bannura | 443dd93 | 2014-02-12 13:38:54 +0100 | [diff] [blame] | 6775 | ALOGW("ResTable_type entriesStart at 0x%x extends beyond chunk end 0x%x.", |
| 6776 | dtohl(type->entriesStart), typeSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6777 | return (mError=BAD_TYPE); |
| 6778 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6779 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6780 | if (type->id == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6781 | ALOGW("ResTable_type has an id of 0."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6782 | return (mError=BAD_TYPE); |
| 6783 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6784 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6785 | if (newEntryCount > 0) { |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6786 | bool addToType = true; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6787 | uint8_t typeIndex = type->id - 1; |
| 6788 | ssize_t idmapIndex = idmapEntries.indexOfKey(type->id); |
| 6789 | if (idmapIndex >= 0) { |
| 6790 | typeIndex = idmapEntries[idmapIndex].targetTypeId() - 1; |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6791 | } else if (header->resourceIDMap != NULL) { |
| 6792 | // This is an overlay, but the types in this overlay are not |
| 6793 | // overlaying anything according to the idmap. We can skip these |
| 6794 | // as they will otherwise conflict with the other resources in the package |
| 6795 | // without a mapping. |
| 6796 | addToType = false; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6797 | } |
| 6798 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6799 | if (addToType) { |
| 6800 | TypeList& typeList = group->types.editItemAt(typeIndex); |
| 6801 | if (typeList.isEmpty()) { |
| 6802 | ALOGE("No TypeSpec for type %d", type->id); |
| 6803 | return (mError=BAD_TYPE); |
| 6804 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6805 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6806 | Type* t = typeList.editItemAt(typeList.size() - 1); |
| 6807 | if (t->package != package) { |
| 6808 | ALOGE("No TypeSpec for type %d", type->id); |
| 6809 | return (mError=BAD_TYPE); |
| 6810 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6811 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6812 | t->configs.add(type); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6813 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 6814 | if (kDebugTableGetEntry) { |
| 6815 | ResTable_config thisConfig; |
| 6816 | thisConfig.copyFromDtoH(type->config); |
| 6817 | ALOGI("Adding config to type %d: %s\n", type->id, |
| 6818 | thisConfig.toString().string()); |
| 6819 | } |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 6820 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 6821 | } else { |
| 6822 | ALOGV("Skipping empty ResTable_type for type %d", type->id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6823 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 6824 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6825 | } else if (ctype == RES_TABLE_LIBRARY_TYPE) { |
| 6826 | if (group->dynamicRefTable.entries().size() == 0) { |
| 6827 | status_t err = group->dynamicRefTable.load((const ResTable_lib_header*) chunk); |
| 6828 | if (err != NO_ERROR) { |
| 6829 | return (mError=err); |
| 6830 | } |
| 6831 | |
| 6832 | // Fill in the reference table with the entries we already know about. |
| 6833 | size_t N = mPackageGroups.size(); |
| 6834 | for (size_t i = 0; i < N; i++) { |
| 6835 | group->dynamicRefTable.addMapping(mPackageGroups[i]->name, mPackageGroups[i]->id); |
| 6836 | } |
| 6837 | } else { |
| 6838 | ALOGW("Found multiple library tables, ignoring..."); |
| 6839 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6840 | } else { |
| 6841 | status_t err = validate_chunk(chunk, sizeof(ResChunk_header), |
| 6842 | endPos, "ResTable_package:unknown"); |
| 6843 | if (err != NO_ERROR) { |
| 6844 | return (mError=err); |
| 6845 | } |
| 6846 | } |
| 6847 | chunk = (const ResChunk_header*) |
| 6848 | (((const uint8_t*)chunk) + csize); |
| 6849 | } |
| 6850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6851 | return NO_ERROR; |
| 6852 | } |
| 6853 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 6854 | DynamicRefTable::DynamicRefTable() : DynamicRefTable(0, false) {} |
| 6855 | |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6856 | DynamicRefTable::DynamicRefTable(uint8_t packageId, bool appAsLib) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6857 | : mAssignedPackageId(packageId) |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6858 | , mAppAsLib(appAsLib) |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6859 | { |
| 6860 | memset(mLookupTable, 0, sizeof(mLookupTable)); |
| 6861 | |
| 6862 | // Reserved package ids |
| 6863 | mLookupTable[APP_PACKAGE_ID] = APP_PACKAGE_ID; |
| 6864 | mLookupTable[SYS_PACKAGE_ID] = SYS_PACKAGE_ID; |
| 6865 | } |
| 6866 | |
| 6867 | status_t DynamicRefTable::load(const ResTable_lib_header* const header) |
| 6868 | { |
| 6869 | const uint32_t entryCount = dtohl(header->count); |
| 6870 | const uint32_t sizeOfEntries = sizeof(ResTable_lib_entry) * entryCount; |
| 6871 | const uint32_t expectedSize = dtohl(header->header.size) - dtohl(header->header.headerSize); |
| 6872 | if (sizeOfEntries > expectedSize) { |
| 6873 | ALOGE("ResTable_lib_header size %u is too small to fit %u entries (x %u).", |
| 6874 | expectedSize, entryCount, (uint32_t)sizeof(ResTable_lib_entry)); |
| 6875 | return UNKNOWN_ERROR; |
| 6876 | } |
| 6877 | |
| 6878 | const ResTable_lib_entry* entry = (const ResTable_lib_entry*)(((uint8_t*) header) + |
| 6879 | dtohl(header->header.headerSize)); |
| 6880 | for (uint32_t entryIndex = 0; entryIndex < entryCount; entryIndex++) { |
| 6881 | uint32_t packageId = dtohl(entry->packageId); |
| 6882 | char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)]; |
| 6883 | strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t)); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 6884 | if (kDebugLibNoisy) { |
| 6885 | ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(), |
| 6886 | dtohl(entry->packageId)); |
| 6887 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6888 | if (packageId >= 256) { |
| 6889 | ALOGE("Bad package id 0x%08x", packageId); |
| 6890 | return UNKNOWN_ERROR; |
| 6891 | } |
| 6892 | mEntries.replaceValueFor(String16(tmpName), (uint8_t) packageId); |
| 6893 | entry = entry + 1; |
| 6894 | } |
| 6895 | return NO_ERROR; |
| 6896 | } |
| 6897 | |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 6898 | status_t DynamicRefTable::addMappings(const DynamicRefTable& other) { |
| 6899 | if (mAssignedPackageId != other.mAssignedPackageId) { |
| 6900 | return UNKNOWN_ERROR; |
| 6901 | } |
| 6902 | |
| 6903 | const size_t entryCount = other.mEntries.size(); |
| 6904 | for (size_t i = 0; i < entryCount; i++) { |
| 6905 | ssize_t index = mEntries.indexOfKey(other.mEntries.keyAt(i)); |
| 6906 | if (index < 0) { |
| 6907 | mEntries.add(other.mEntries.keyAt(i), other.mEntries[i]); |
| 6908 | } else { |
| 6909 | if (other.mEntries[i] != mEntries[index]) { |
| 6910 | return UNKNOWN_ERROR; |
| 6911 | } |
| 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | // Merge the lookup table. No entry can conflict |
| 6916 | // (value of 0 means not set). |
| 6917 | for (size_t i = 0; i < 256; i++) { |
| 6918 | if (mLookupTable[i] != other.mLookupTable[i]) { |
| 6919 | if (mLookupTable[i] == 0) { |
| 6920 | mLookupTable[i] = other.mLookupTable[i]; |
| 6921 | } else if (other.mLookupTable[i] != 0) { |
| 6922 | return UNKNOWN_ERROR; |
| 6923 | } |
| 6924 | } |
| 6925 | } |
| 6926 | return NO_ERROR; |
| 6927 | } |
| 6928 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6929 | status_t DynamicRefTable::addMapping(const String16& packageName, uint8_t packageId) |
| 6930 | { |
| 6931 | ssize_t index = mEntries.indexOfKey(packageName); |
| 6932 | if (index < 0) { |
| 6933 | return UNKNOWN_ERROR; |
| 6934 | } |
| 6935 | mLookupTable[mEntries.valueAt(index)] = packageId; |
| 6936 | return NO_ERROR; |
| 6937 | } |
| 6938 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 6939 | void DynamicRefTable::addMapping(uint8_t buildPackageId, uint8_t runtimePackageId) { |
| 6940 | mLookupTable[buildPackageId] = runtimePackageId; |
| 6941 | } |
| 6942 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6943 | status_t DynamicRefTable::lookupResourceId(uint32_t* resId) const { |
| 6944 | uint32_t res = *resId; |
| 6945 | size_t packageId = Res_GETPACKAGE(res) + 1; |
| 6946 | |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6947 | if (packageId == APP_PACKAGE_ID && !mAppAsLib) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6948 | // No lookup needs to be done, app package IDs are absolute. |
| 6949 | return NO_ERROR; |
| 6950 | } |
| 6951 | |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6952 | if (packageId == 0 || (packageId == APP_PACKAGE_ID && mAppAsLib)) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6953 | // The package ID is 0x00. That means that a shared library is accessing |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6954 | // its own local resource. |
| 6955 | // Or if app resource is loaded as shared library, the resource which has |
| 6956 | // app package Id is local resources. |
| 6957 | // so we fix up those resources with the calling package ID. |
| 6958 | *resId = (0xFFFFFF & (*resId)) | (((uint32_t) mAssignedPackageId) << 24); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6959 | return NO_ERROR; |
| 6960 | } |
| 6961 | |
| 6962 | // Do a proper lookup. |
| 6963 | uint8_t translatedId = mLookupTable[packageId]; |
| 6964 | if (translatedId == 0) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 6965 | ALOGW("DynamicRefTable(0x%02x): No mapping for build-time package ID 0x%02x.", |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6966 | (uint8_t)mAssignedPackageId, (uint8_t)packageId); |
| 6967 | for (size_t i = 0; i < 256; i++) { |
| 6968 | if (mLookupTable[i] != 0) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 6969 | ALOGW("e[0x%02x] -> 0x%02x", (uint8_t)i, mLookupTable[i]); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6970 | } |
| 6971 | } |
| 6972 | return UNKNOWN_ERROR; |
| 6973 | } |
| 6974 | |
| 6975 | *resId = (res & 0x00ffffff) | (((uint32_t) translatedId) << 24); |
| 6976 | return NO_ERROR; |
| 6977 | } |
| 6978 | |
| 6979 | status_t DynamicRefTable::lookupResourceValue(Res_value* value) const { |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 6980 | uint8_t resolvedType = Res_value::TYPE_REFERENCE; |
| 6981 | switch (value->dataType) { |
| 6982 | case Res_value::TYPE_ATTRIBUTE: |
| 6983 | resolvedType = Res_value::TYPE_ATTRIBUTE; |
| 6984 | // fallthrough |
| 6985 | case Res_value::TYPE_REFERENCE: |
| 6986 | if (!mAppAsLib) { |
| 6987 | return NO_ERROR; |
| 6988 | } |
| 6989 | |
Tao Bai | a6d7e3f | 2015-09-01 18:49:54 -0700 | [diff] [blame] | 6990 | // If the package is loaded as shared library, the resource reference |
| 6991 | // also need to be fixed. |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 6992 | break; |
| 6993 | case Res_value::TYPE_DYNAMIC_ATTRIBUTE: |
| 6994 | resolvedType = Res_value::TYPE_ATTRIBUTE; |
| 6995 | // fallthrough |
| 6996 | case Res_value::TYPE_DYNAMIC_REFERENCE: |
| 6997 | break; |
| 6998 | default: |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 6999 | return NO_ERROR; |
| 7000 | } |
| 7001 | |
| 7002 | status_t err = lookupResourceId(&value->data); |
| 7003 | if (err != NO_ERROR) { |
| 7004 | return err; |
| 7005 | } |
| 7006 | |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 7007 | value->dataType = resolvedType; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 7008 | return NO_ERROR; |
| 7009 | } |
| 7010 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7011 | struct IdmapTypeMap { |
| 7012 | ssize_t overlayTypeId; |
| 7013 | size_t entryOffset; |
| 7014 | Vector<uint32_t> entryMap; |
| 7015 | }; |
| 7016 | |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7017 | status_t ResTable::createIdmap(const ResTable& overlay, |
| 7018 | uint32_t targetCrc, uint32_t overlayCrc, |
| 7019 | const char* targetPath, const char* overlayPath, |
| 7020 | void** outData, size_t* outSize) const |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7021 | { |
| 7022 | // see README for details on the format of map |
| 7023 | if (mPackageGroups.size() == 0) { |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7024 | 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] | 7025 | return UNKNOWN_ERROR; |
| 7026 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7027 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7028 | if (mPackageGroups[0]->packages.size() == 0) { |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7029 | ALOGW("idmap: target package has no packages in its first package group, " |
| 7030 | "cannot create idmap\n"); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7031 | return UNKNOWN_ERROR; |
| 7032 | } |
| 7033 | |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7034 | // The number of resources overlaid that were not explicitly marked overlayable. |
| 7035 | size_t forcedOverlayCount = 0u; |
| 7036 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7037 | KeyedVector<uint8_t, IdmapTypeMap> map; |
| 7038 | |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7039 | // overlaid packages are assumed to contain only one package group |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7040 | const PackageGroup* pg = mPackageGroups[0]; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7041 | |
| 7042 | // starting size is header |
| 7043 | *outSize = ResTable::IDMAP_HEADER_SIZE_BYTES; |
| 7044 | |
| 7045 | // target package id and number of types in map |
| 7046 | *outSize += 2 * sizeof(uint16_t); |
| 7047 | |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7048 | // overlay packages are assumed to contain only one package group |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 7049 | const ResTable_package* overlayPackageStruct = overlay.mPackageGroups[0]->packages[0]->package; |
| 7050 | char16_t tmpName[sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])]; |
| 7051 | strcpy16_dtoh(tmpName, overlayPackageStruct->name, sizeof(overlayPackageStruct->name)/sizeof(overlayPackageStruct->name[0])); |
| 7052 | const String16 overlayPackage(tmpName); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7053 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7054 | for (size_t typeIndex = 0; typeIndex < pg->types.size(); ++typeIndex) { |
| 7055 | const TypeList& typeList = pg->types[typeIndex]; |
| 7056 | if (typeList.isEmpty()) { |
| 7057 | continue; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7058 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7059 | |
| 7060 | const Type* typeConfigs = typeList[0]; |
| 7061 | |
| 7062 | IdmapTypeMap typeMap; |
| 7063 | typeMap.overlayTypeId = -1; |
| 7064 | typeMap.entryOffset = 0; |
| 7065 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7066 | for (size_t entryIndex = 0; entryIndex < typeConfigs->entryCount; ++entryIndex) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7067 | uint32_t resID = Res_MAKEID(pg->id - 1, typeIndex, entryIndex); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7068 | resource_name resName; |
MÃ¥rten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 7069 | if (!this->getResourceName(resID, false, &resName)) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7070 | if (typeMap.entryMap.isEmpty()) { |
| 7071 | typeMap.entryOffset++; |
| 7072 | } |
MÃ¥rten Kongstad | fcaba14 | 2011-05-19 16:02:35 +0200 | [diff] [blame] | 7073 | continue; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7074 | } |
| 7075 | |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7076 | uint32_t typeSpecFlags = 0u; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7077 | const String16 overlayType(resName.type, resName.typeLen); |
| 7078 | const String16 overlayName(resName.name, resName.nameLen); |
| 7079 | uint32_t overlayResID = overlay.identifierForName(overlayName.string(), |
| 7080 | overlayName.size(), |
| 7081 | overlayType.string(), |
| 7082 | overlayType.size(), |
| 7083 | overlayPackage.string(), |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7084 | overlayPackage.size(), |
| 7085 | &typeSpecFlags); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7086 | if (overlayResID == 0) { |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7087 | // No such target resource was found. |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7088 | if (typeMap.entryMap.isEmpty()) { |
| 7089 | typeMap.entryOffset++; |
Jean-Baptiste Queru | 3e2d591 | 2012-05-01 10:00:22 -0700 | [diff] [blame] | 7090 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7091 | continue; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7092 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7093 | |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7094 | // Now that we know this is being overlaid, check if it can be, and emit a warning if |
| 7095 | // it can't. |
| 7096 | if ((dtohl(typeConfigs->typeSpecFlags[entryIndex]) & |
| 7097 | ResTable_typeSpec::SPEC_OVERLAYABLE) == 0) { |
| 7098 | forcedOverlayCount++; |
| 7099 | } |
| 7100 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7101 | if (typeMap.overlayTypeId == -1) { |
| 7102 | typeMap.overlayTypeId = Res_GETTYPE(overlayResID) + 1; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7103 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7104 | |
| 7105 | if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) { |
| 7106 | ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x" |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 7107 | " but entries should map to resources of type %02zx", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7108 | resID, overlayResID, typeMap.overlayTypeId); |
| 7109 | return BAD_TYPE; |
| 7110 | } |
| 7111 | |
| 7112 | if (typeMap.entryOffset + typeMap.entryMap.size() < entryIndex) { |
MÃ¥rten Kongstad | 96198eb | 2014-11-07 10:56:12 +0100 | [diff] [blame] | 7113 | // pad with 0xffffffff's (indicating non-existing entries) before adding this entry |
| 7114 | size_t index = typeMap.entryMap.size(); |
| 7115 | size_t numItems = entryIndex - (typeMap.entryOffset + index); |
| 7116 | if (typeMap.entryMap.insertAt(0xffffffff, index, numItems) < 0) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7117 | return NO_MEMORY; |
| 7118 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7119 | } |
MÃ¥rten Kongstad | 96198eb | 2014-11-07 10:56:12 +0100 | [diff] [blame] | 7120 | typeMap.entryMap.add(Res_GETENTRY(overlayResID)); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7121 | } |
| 7122 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7123 | if (!typeMap.entryMap.isEmpty()) { |
| 7124 | if (map.add(static_cast<uint8_t>(typeIndex), typeMap) < 0) { |
| 7125 | return NO_MEMORY; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7126 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7127 | *outSize += (4 * sizeof(uint16_t)) + (typeMap.entryMap.size() * sizeof(uint32_t)); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7128 | } |
| 7129 | } |
| 7130 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7131 | if (map.isEmpty()) { |
| 7132 | ALOGW("idmap: no resources in overlay package present in base package"); |
| 7133 | return UNKNOWN_ERROR; |
| 7134 | } |
| 7135 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7136 | if ((*outData = malloc(*outSize)) == NULL) { |
| 7137 | return NO_MEMORY; |
| 7138 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7139 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7140 | uint32_t* data = (uint32_t*)*outData; |
| 7141 | *data++ = htodl(IDMAP_MAGIC); |
MÃ¥rten Kongstad | 42ebcb8 | 2017-03-28 15:30:21 +0200 | [diff] [blame] | 7142 | *data++ = htodl(ResTable::IDMAP_CURRENT_VERSION); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7143 | *data++ = htodl(targetCrc); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7144 | *data++ = htodl(overlayCrc); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7145 | const char* paths[] = { targetPath, overlayPath }; |
| 7146 | for (int j = 0; j < 2; ++j) { |
| 7147 | char* p = (char*)data; |
| 7148 | const char* path = paths[j]; |
| 7149 | const size_t I = strlen(path); |
| 7150 | if (I > 255) { |
| 7151 | ALOGV("path exceeds expected 255 characters: %s\n", path); |
| 7152 | return UNKNOWN_ERROR; |
| 7153 | } |
| 7154 | for (size_t i = 0; i < 256; ++i) { |
| 7155 | *p++ = i < I ? path[i] : '\0'; |
| 7156 | } |
| 7157 | data += 256 / sizeof(uint32_t); |
| 7158 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7159 | const size_t mapSize = map.size(); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7160 | uint16_t* typeData = reinterpret_cast<uint16_t*>(data); |
| 7161 | *typeData++ = htods(pg->id); |
| 7162 | *typeData++ = htods(mapSize); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7163 | for (size_t i = 0; i < mapSize; ++i) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7164 | uint8_t targetTypeId = map.keyAt(i); |
| 7165 | const IdmapTypeMap& typeMap = map[i]; |
| 7166 | *typeData++ = htods(targetTypeId + 1); |
| 7167 | *typeData++ = htods(typeMap.overlayTypeId); |
| 7168 | *typeData++ = htods(typeMap.entryMap.size()); |
| 7169 | *typeData++ = htods(typeMap.entryOffset); |
| 7170 | |
| 7171 | const size_t entryCount = typeMap.entryMap.size(); |
| 7172 | uint32_t* entries = reinterpret_cast<uint32_t*>(typeData); |
| 7173 | for (size_t j = 0; j < entryCount; j++) { |
| 7174 | entries[j] = htodl(typeMap.entryMap[j]); |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7175 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7176 | typeData += entryCount * 2; |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7177 | } |
| 7178 | |
Adam Lesinski | a974382 | 2017-12-18 17:20:41 -0800 | [diff] [blame] | 7179 | if (forcedOverlayCount > 0) { |
| 7180 | ALOGW("idmap: overlaid %zu resources not marked overlayable", forcedOverlayCount); |
| 7181 | } |
| 7182 | |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7183 | return NO_ERROR; |
| 7184 | } |
| 7185 | |
| 7186 | bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes, |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7187 | uint32_t* pVersion, |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7188 | uint32_t* pTargetCrc, uint32_t* pOverlayCrc, |
| 7189 | String8* pTargetPath, String8* pOverlayPath) |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7190 | { |
| 7191 | const uint32_t* map = (const uint32_t*)idmap; |
| 7192 | if (!assertIdmapHeader(map, sizeBytes)) { |
| 7193 | return false; |
| 7194 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7195 | if (pVersion) { |
| 7196 | *pVersion = dtohl(map[1]); |
| 7197 | } |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7198 | if (pTargetCrc) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7199 | *pTargetCrc = dtohl(map[2]); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7200 | } |
| 7201 | if (pOverlayCrc) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7202 | *pOverlayCrc = dtohl(map[3]); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7203 | } |
| 7204 | if (pTargetPath) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7205 | pTargetPath->setTo(reinterpret_cast<const char*>(map + 4)); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7206 | } |
| 7207 | if (pOverlayPath) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7208 | pOverlayPath->setTo(reinterpret_cast<const char*>(map + 4 + 256 / sizeof(uint32_t))); |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 7209 | } |
MÃ¥rten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 7210 | return true; |
| 7211 | } |
| 7212 | |
| 7213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7214 | #define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string()) |
| 7215 | |
| 7216 | #define CHAR16_ARRAY_EQ(constant, var, len) \ |
Chih-Hung Hsieh | e819d01 | 2016-05-19 15:19:22 -0700 | [diff] [blame] | 7217 | (((len) == (sizeof(constant)/sizeof((constant)[0]))) && (0 == memcmp((var), (constant), (len)))) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7218 | |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 7219 | static void print_complex(uint32_t complex, bool isFraction) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | { |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 7221 | const float MANTISSA_MULT = |
| 7222 | 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT); |
| 7223 | const float RADIX_MULTS[] = { |
| 7224 | 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT, |
| 7225 | 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT |
| 7226 | }; |
| 7227 | |
| 7228 | float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK |
| 7229 | <<Res_value::COMPLEX_MANTISSA_SHIFT)) |
| 7230 | * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT) |
| 7231 | & Res_value::COMPLEX_RADIX_MASK]; |
| 7232 | printf("%f", value); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7233 | |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7234 | if (!isFraction) { |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 7235 | switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { |
| 7236 | case Res_value::COMPLEX_UNIT_PX: printf("px"); break; |
| 7237 | case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break; |
| 7238 | case Res_value::COMPLEX_UNIT_SP: printf("sp"); break; |
| 7239 | case Res_value::COMPLEX_UNIT_PT: printf("pt"); break; |
| 7240 | case Res_value::COMPLEX_UNIT_IN: printf("in"); break; |
| 7241 | case Res_value::COMPLEX_UNIT_MM: printf("mm"); break; |
| 7242 | default: printf(" (unknown unit)"); break; |
| 7243 | } |
| 7244 | } else { |
| 7245 | switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { |
| 7246 | case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break; |
| 7247 | case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break; |
| 7248 | default: printf(" (unknown unit)"); break; |
| 7249 | } |
| 7250 | } |
| 7251 | } |
| 7252 | |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 7253 | // Normalize a string for output |
| 7254 | String8 ResTable::normalizeForOutput( const char *input ) |
| 7255 | { |
| 7256 | String8 ret; |
| 7257 | char buff[2]; |
| 7258 | buff[1] = '\0'; |
| 7259 | |
| 7260 | while (*input != '\0') { |
| 7261 | switch (*input) { |
| 7262 | // All interesting characters are in the ASCII zone, so we are making our own lives |
| 7263 | // easier by scanning the string one byte at a time. |
| 7264 | case '\\': |
| 7265 | ret += "\\\\"; |
| 7266 | break; |
| 7267 | case '\n': |
| 7268 | ret += "\\n"; |
| 7269 | break; |
| 7270 | case '"': |
| 7271 | ret += "\\\""; |
| 7272 | break; |
| 7273 | default: |
| 7274 | buff[0] = *input; |
| 7275 | ret += buff; |
| 7276 | break; |
| 7277 | } |
| 7278 | |
| 7279 | input++; |
| 7280 | } |
| 7281 | |
| 7282 | return ret; |
| 7283 | } |
| 7284 | |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7285 | void ResTable::print_value(const Package* pkg, const Res_value& value) const |
| 7286 | { |
| 7287 | if (value.dataType == Res_value::TYPE_NULL) { |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 7288 | if (value.data == Res_value::DATA_NULL_UNDEFINED) { |
| 7289 | printf("(null)\n"); |
| 7290 | } else if (value.data == Res_value::DATA_NULL_EMPTY) { |
| 7291 | printf("(null empty)\n"); |
| 7292 | } else { |
| 7293 | // This should never happen. |
| 7294 | printf("(null) 0x%08x\n", value.data); |
| 7295 | } |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7296 | } else if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 7297 | printf("(reference) 0x%08x\n", value.data); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 7298 | } else if (value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) { |
| 7299 | printf("(dynamic reference) 0x%08x\n", value.data); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7300 | } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 7301 | printf("(attribute) 0x%08x\n", value.data); |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 7302 | } else if (value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE) { |
| 7303 | printf("(dynamic attribute) 0x%08x\n", value.data); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7304 | } else if (value.dataType == Res_value::TYPE_STRING) { |
| 7305 | size_t len; |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 7306 | const char* str8 = pkg->header->values.string8At( |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7307 | value.data, &len); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 7308 | if (str8 != NULL) { |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 7309 | printf("(string8) \"%s\"\n", normalizeForOutput(str8).string()); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7310 | } else { |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 7311 | const char16_t* str16 = pkg->header->values.stringAt( |
| 7312 | value.data, &len); |
| 7313 | if (str16 != NULL) { |
| 7314 | printf("(string16) \"%s\"\n", |
Shachar Shemesh | 9872bf4 | 2010-12-20 17:38:33 +0200 | [diff] [blame] | 7315 | normalizeForOutput(String8(str16, len).string()).string()); |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 7316 | } else { |
| 7317 | printf("(string) null\n"); |
| 7318 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7319 | } |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 7320 | } else if (value.dataType == Res_value::TYPE_FLOAT) { |
| 7321 | printf("(float) %g\n", *(const float*)&value.data); |
| 7322 | } else if (value.dataType == Res_value::TYPE_DIMENSION) { |
| 7323 | printf("(dimension) "); |
| 7324 | print_complex(value.data, false); |
| 7325 | printf("\n"); |
| 7326 | } else if (value.dataType == Res_value::TYPE_FRACTION) { |
| 7327 | printf("(fraction) "); |
| 7328 | print_complex(value.data, true); |
| 7329 | printf("\n"); |
| 7330 | } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT |
| 7331 | || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) { |
| 7332 | printf("(color) #%08x\n", value.data); |
| 7333 | } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) { |
| 7334 | printf("(boolean) %s\n", value.data ? "true" : "false"); |
| 7335 | } else if (value.dataType >= Res_value::TYPE_FIRST_INT |
| 7336 | || value.dataType <= Res_value::TYPE_LAST_INT) { |
| 7337 | printf("(int) 0x%08x or %d\n", value.data, value.data); |
| 7338 | } else { |
| 7339 | printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n", |
| 7340 | (int)value.dataType, (int)value.data, |
| 7341 | (int)value.size, (int)value.res0); |
| 7342 | } |
| 7343 | } |
| 7344 | |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 7345 | void ResTable::print(bool inclValues) const |
| 7346 | { |
| 7347 | if (mError != 0) { |
| 7348 | printf("mError=0x%x (%s)\n", mError, strerror(mError)); |
| 7349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7350 | size_t pgCount = mPackageGroups.size(); |
| 7351 | printf("Package Groups (%d)\n", (int)pgCount); |
| 7352 | for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) { |
| 7353 | const PackageGroup* pg = mPackageGroups[pgIndex]; |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7354 | printf("Package Group %d id=0x%02x packageCount=%d name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7355 | (int)pgIndex, pg->id, (int)pg->packages.size(), |
| 7356 | String8(pg->name).string()); |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7357 | |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7358 | const KeyedVector<String16, uint8_t>& refEntries = pg->dynamicRefTable.entries(); |
| 7359 | const size_t refEntryCount = refEntries.size(); |
| 7360 | if (refEntryCount > 0) { |
| 7361 | printf(" DynamicRefTable entryCount=%d:\n", (int) refEntryCount); |
| 7362 | for (size_t refIndex = 0; refIndex < refEntryCount; refIndex++) { |
| 7363 | printf(" 0x%02x -> %s\n", |
| 7364 | refEntries.valueAt(refIndex), |
| 7365 | String8(refEntries.keyAt(refIndex)).string()); |
| 7366 | } |
| 7367 | printf("\n"); |
| 7368 | } |
| 7369 | |
| 7370 | int packageId = pg->id; |
Adam Lesinski | 1856088 | 2014-08-15 17:18:21 +0000 | [diff] [blame] | 7371 | size_t pkgCount = pg->packages.size(); |
| 7372 | for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) { |
| 7373 | const Package* pkg = pg->packages[pkgIndex]; |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7374 | // Use a package's real ID, since the ID may have been assigned |
| 7375 | // if this package is a shared library. |
| 7376 | packageId = pkg->package->id; |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 7377 | char16_t tmpName[sizeof(pkg->package->name)/sizeof(pkg->package->name[0])]; |
| 7378 | strcpy16_dtoh(tmpName, pkg->package->name, sizeof(pkg->package->name)/sizeof(pkg->package->name[0])); |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7379 | printf(" Package %d id=0x%02x name=%s\n", (int)pkgIndex, |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 7380 | pkg->package->id, String8(tmpName).string()); |
Adam Lesinski | 1856088 | 2014-08-15 17:18:21 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7383 | for (size_t typeIndex=0; typeIndex < pg->types.size(); typeIndex++) { |
| 7384 | const TypeList& typeList = pg->types[typeIndex]; |
| 7385 | if (typeList.isEmpty()) { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7386 | continue; |
| 7387 | } |
| 7388 | const Type* typeConfigs = typeList[0]; |
| 7389 | const size_t NTC = typeConfigs->configs.size(); |
| 7390 | printf(" type %d configCount=%d entryCount=%d\n", |
| 7391 | (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount); |
| 7392 | if (typeConfigs->typeSpecFlags != NULL) { |
| 7393 | for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) { |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7394 | uint32_t resID = (0xff000000 & ((packageId)<<24)) |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7395 | | (0x00ff0000 & ((typeIndex+1)<<16)) |
| 7396 | | (0x0000ffff & (entryIndex)); |
| 7397 | // Since we are creating resID without actually |
| 7398 | // iterating over them, we have no idea which is a |
| 7399 | // dynamic reference. We must check. |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7400 | if (packageId == 0) { |
| 7401 | pg->dynamicRefTable.lookupResourceId(&resID); |
| 7402 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7403 | |
| 7404 | resource_name resName; |
| 7405 | if (this->getResourceName(resID, true, &resName)) { |
| 7406 | String8 type8; |
| 7407 | String8 name8; |
| 7408 | if (resName.type8 != NULL) { |
| 7409 | type8 = String8(resName.type8, resName.typeLen); |
| 7410 | } else { |
| 7411 | type8 = String8(resName.type, resName.typeLen); |
| 7412 | } |
| 7413 | if (resName.name8 != NULL) { |
| 7414 | name8 = String8(resName.name8, resName.nameLen); |
| 7415 | } else { |
| 7416 | name8 = String8(resName.name, resName.nameLen); |
| 7417 | } |
| 7418 | printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n", |
| 7419 | resID, |
| 7420 | CHAR16_TO_CSTR(resName.package, resName.packageLen), |
| 7421 | type8.string(), name8.string(), |
| 7422 | dtohl(typeConfigs->typeSpecFlags[entryIndex])); |
| 7423 | } else { |
| 7424 | printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID); |
| 7425 | } |
| 7426 | } |
| 7427 | } |
| 7428 | for (size_t configIndex=0; configIndex<NTC; configIndex++) { |
| 7429 | const ResTable_type* type = typeConfigs->configs[configIndex]; |
| 7430 | if ((((uint64_t)type)&0x3) != 0) { |
| 7431 | printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7432 | continue; |
| 7433 | } |
Adam Lesinski | 5b0f1be | 2015-07-27 16:53:14 -0700 | [diff] [blame] | 7434 | |
| 7435 | // Always copy the config, as fields get added and we need to |
| 7436 | // set the defaults. |
| 7437 | ResTable_config thisConfig; |
| 7438 | thisConfig.copyFromDtoH(type->config); |
| 7439 | |
| 7440 | String8 configStr = thisConfig.toString(); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 7441 | printf(" config %s", configStr.size() > 0 |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7442 | ? configStr.string() : "(default)"); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 7443 | if (type->flags != 0u) { |
| 7444 | printf(" flags=0x%02x", type->flags); |
| 7445 | if (type->flags & ResTable_type::FLAG_SPARSE) { |
| 7446 | printf(" [sparse]"); |
| 7447 | } |
| 7448 | } |
| 7449 | |
| 7450 | printf(":\n"); |
| 7451 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7452 | size_t entryCount = dtohl(type->entryCount); |
| 7453 | uint32_t entriesStart = dtohl(type->entriesStart); |
| 7454 | if ((entriesStart&0x3) != 0) { |
| 7455 | printf(" NON-INTEGER ResTable_type entriesStart OFFSET: 0x%x\n", entriesStart); |
| 7456 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7457 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7458 | uint32_t typeSize = dtohl(type->header.size); |
| 7459 | if ((typeSize&0x3) != 0) { |
| 7460 | printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize); |
| 7461 | continue; |
| 7462 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7463 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 7464 | const uint32_t* const eindex = (const uint32_t*) |
| 7465 | (((const uint8_t*)type) + dtohs(type->header.headerSize)); |
| 7466 | for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) { |
| 7467 | size_t entryId; |
| 7468 | uint32_t thisOffset; |
| 7469 | if (type->flags & ResTable_type::FLAG_SPARSE) { |
| 7470 | const ResTable_sparseTypeEntry* entry = |
| 7471 | reinterpret_cast<const ResTable_sparseTypeEntry*>( |
| 7472 | eindex + entryIndex); |
| 7473 | entryId = dtohs(entry->idx); |
| 7474 | // Offsets are encoded as divided by 4. |
| 7475 | thisOffset = static_cast<uint32_t>(dtohs(entry->offset)) * 4u; |
| 7476 | } else { |
| 7477 | entryId = entryIndex; |
| 7478 | thisOffset = dtohl(eindex[entryIndex]); |
| 7479 | if (thisOffset == ResTable_type::NO_ENTRY) { |
| 7480 | continue; |
| 7481 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7482 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7483 | |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7484 | uint32_t resID = (0xff000000 & ((packageId)<<24)) |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7485 | | (0x00ff0000 & ((typeIndex+1)<<16)) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 7486 | | (0x0000ffff & (entryId)); |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7487 | if (packageId == 0) { |
| 7488 | pg->dynamicRefTable.lookupResourceId(&resID); |
| 7489 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7490 | resource_name resName; |
| 7491 | if (this->getResourceName(resID, true, &resName)) { |
| 7492 | String8 type8; |
| 7493 | String8 name8; |
| 7494 | if (resName.type8 != NULL) { |
| 7495 | type8 = String8(resName.type8, resName.typeLen); |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 7496 | } else { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7497 | type8 = String8(resName.type, resName.typeLen); |
Kenny Root | 3379195 | 2010-06-08 10:16:48 -0700 | [diff] [blame] | 7498 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7499 | if (resName.name8 != NULL) { |
| 7500 | name8 = String8(resName.name8, resName.nameLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7501 | } else { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7502 | name8 = String8(resName.name, resName.nameLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7503 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7504 | printf(" resource 0x%08x %s:%s/%s: ", resID, |
| 7505 | CHAR16_TO_CSTR(resName.package, resName.packageLen), |
| 7506 | type8.string(), name8.string()); |
| 7507 | } else { |
| 7508 | printf(" INVALID RESOURCE 0x%08x: ", resID); |
| 7509 | } |
| 7510 | if ((thisOffset&0x3) != 0) { |
| 7511 | printf("NON-INTEGER OFFSET: 0x%x\n", thisOffset); |
| 7512 | continue; |
| 7513 | } |
| 7514 | if ((thisOffset+sizeof(ResTable_entry)) > typeSize) { |
| 7515 | printf("OFFSET OUT OF BOUNDS: 0x%x+0x%x (size is 0x%x)\n", |
| 7516 | entriesStart, thisOffset, typeSize); |
| 7517 | continue; |
| 7518 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7519 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7520 | const ResTable_entry* ent = (const ResTable_entry*) |
| 7521 | (((const uint8_t*)type) + entriesStart + thisOffset); |
| 7522 | if (((entriesStart + thisOffset)&0x3) != 0) { |
| 7523 | printf("NON-INTEGER ResTable_entry OFFSET: 0x%x\n", |
| 7524 | (entriesStart + thisOffset)); |
| 7525 | continue; |
| 7526 | } |
Mark Salyzyn | 00adb86 | 2014-03-19 11:00:06 -0700 | [diff] [blame] | 7527 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7528 | uintptr_t esize = dtohs(ent->size); |
| 7529 | if ((esize&0x3) != 0) { |
| 7530 | printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void *)esize); |
| 7531 | continue; |
| 7532 | } |
| 7533 | if ((thisOffset+esize) > typeSize) { |
| 7534 | printf("ResTable_entry OUT OF BOUNDS: 0x%x+0x%x+%p (size is 0x%x)\n", |
| 7535 | entriesStart, thisOffset, (void *)esize, typeSize); |
| 7536 | continue; |
| 7537 | } |
| 7538 | |
| 7539 | const Res_value* valuePtr = NULL; |
| 7540 | const ResTable_map_entry* bagPtr = NULL; |
| 7541 | Res_value value; |
| 7542 | if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) { |
| 7543 | printf("<bag>"); |
| 7544 | bagPtr = (const ResTable_map_entry*)ent; |
| 7545 | } else { |
| 7546 | valuePtr = (const Res_value*) |
| 7547 | (((const uint8_t*)ent) + esize); |
| 7548 | value.copyFrom_dtoh(*valuePtr); |
| 7549 | printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)", |
| 7550 | (int)value.dataType, (int)value.data, |
| 7551 | (int)value.size, (int)value.res0); |
| 7552 | } |
| 7553 | |
| 7554 | if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) { |
| 7555 | printf(" (PUBLIC)"); |
| 7556 | } |
| 7557 | printf("\n"); |
| 7558 | |
| 7559 | if (inclValues) { |
| 7560 | if (valuePtr != NULL) { |
| 7561 | printf(" "); |
| 7562 | print_value(typeConfigs->package, value); |
| 7563 | } else if (bagPtr != NULL) { |
| 7564 | const int N = dtohl(bagPtr->count); |
| 7565 | const uint8_t* baseMapPtr = (const uint8_t*)ent; |
| 7566 | size_t mapOffset = esize; |
| 7567 | const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); |
| 7568 | const uint32_t parent = dtohl(bagPtr->parent.ident); |
| 7569 | uint32_t resolvedParent = parent; |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 7570 | if (Res_GETPACKAGE(resolvedParent) + 1 == 0) { |
| 7571 | status_t err = pg->dynamicRefTable.lookupResourceId(&resolvedParent); |
| 7572 | if (err != NO_ERROR) { |
| 7573 | resolvedParent = 0; |
| 7574 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 7575 | } |
| 7576 | printf(" Parent=0x%08x(Resolved=0x%08x), Count=%d\n", |
| 7577 | parent, resolvedParent, N); |
| 7578 | for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) { |
| 7579 | printf(" #%i (Key=0x%08x): ", |
| 7580 | i, dtohl(mapPtr->name.ident)); |
| 7581 | value.copyFrom_dtoh(mapPtr->value); |
| 7582 | print_value(typeConfigs->package, value); |
| 7583 | const size_t size = dtohs(mapPtr->value.size); |
| 7584 | mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value); |
| 7585 | mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); |
Dianne Hackborn | e17086b | 2009-06-19 15:13:28 -0700 | [diff] [blame] | 7586 | } |
| 7587 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7588 | } |
| 7589 | } |
| 7590 | } |
| 7591 | } |
| 7592 | } |
| 7593 | } |
| 7594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7595 | } // namespace android |