ResourceTypes.cpp revision c4db95c077f826585d20be2f3db4043c53d30cf5
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 20#include <utils/Atomic.h> 21#include <utils/ByteOrder.h> 22#include <utils/Debug.h> 23#include <utils/ResourceTypes.h> 24#include <utils/String16.h> 25#include <utils/String8.h> 26#include <utils/TextOutput.h> 27#include <utils/Log.h> 28 29#include <stdlib.h> 30#include <string.h> 31#include <memory.h> 32#include <ctype.h> 33#include <stdint.h> 34 35#ifndef INT32_MAX 36#define INT32_MAX ((int32_t)(2147483647)) 37#endif 38 39#define POOL_NOISY(x) //x 40#define XML_NOISY(x) //x 41#define TABLE_NOISY(x) //x 42#define TABLE_GETENTRY(x) //x 43#define TABLE_SUPER_NOISY(x) //x 44#define LOAD_TABLE_NOISY(x) //x 45 46namespace android { 47 48#ifdef HAVE_WINSOCK 49#undef nhtol 50#undef htonl 51 52#ifdef HAVE_LITTLE_ENDIAN 53#define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) ) 54#define htonl(x) ntohl(x) 55#define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) ) 56#define htons(x) ntohs(x) 57#else 58#define ntohl(x) (x) 59#define htonl(x) (x) 60#define ntohs(x) (x) 61#define htons(x) (x) 62#endif 63#endif 64 65static void printToLogFunc(void* cookie, const char* txt) 66{ 67 LOGV("%s", txt); 68} 69 70// Standard C isspace() is only required to look at the low byte of its input, so 71// produces incorrect results for UTF-16 characters. For safety's sake, assume that 72// any high-byte UTF-16 code point is not whitespace. 73inline int isspace16(char16_t c) { 74 return (c < 0x0080 && isspace(c)); 75} 76 77// range checked; guaranteed to NUL-terminate within the stated number of available slots 78// NOTE: if this truncates the dst string due to running out of space, no attempt is 79// made to avoid splitting surrogate pairs. 80static void strcpy16_dtoh(uint16_t* dst, const uint16_t* src, size_t avail) 81{ 82 uint16_t* last = dst + avail - 1; 83 while (*src && (dst < last)) { 84 char16_t s = dtohs(*src); 85 *dst++ = s; 86 src++; 87 } 88 *dst = 0; 89} 90 91static status_t validate_chunk(const ResChunk_header* chunk, 92 size_t minSize, 93 const uint8_t* dataEnd, 94 const char* name) 95{ 96 const uint16_t headerSize = dtohs(chunk->headerSize); 97 const uint32_t size = dtohl(chunk->size); 98 99 if (headerSize >= minSize) { 100 if (headerSize <= size) { 101 if (((headerSize|size)&0x3) == 0) { 102 if ((ssize_t)size <= (dataEnd-((const uint8_t*)chunk))) { 103 return NO_ERROR; 104 } 105 LOGW("%s data size %p extends beyond resource end %p.", 106 name, (void*)size, 107 (void*)(dataEnd-((const uint8_t*)chunk))); 108 return BAD_TYPE; 109 } 110 LOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.", 111 name, (int)size, (int)headerSize); 112 return BAD_TYPE; 113 } 114 LOGW("%s size %p is smaller than header size %p.", 115 name, (void*)size, (void*)(int)headerSize); 116 return BAD_TYPE; 117 } 118 LOGW("%s header size %p is too small.", 119 name, (void*)(int)headerSize); 120 return BAD_TYPE; 121} 122 123inline void Res_value::copyFrom_dtoh(const Res_value& src) 124{ 125 size = dtohs(src.size); 126 res0 = src.res0; 127 dataType = src.dataType; 128 data = dtohl(src.data); 129} 130 131void Res_png_9patch::deviceToFile() 132{ 133 for (int i = 0; i < numXDivs; i++) { 134 xDivs[i] = htonl(xDivs[i]); 135 } 136 for (int i = 0; i < numYDivs; i++) { 137 yDivs[i] = htonl(yDivs[i]); 138 } 139 paddingLeft = htonl(paddingLeft); 140 paddingRight = htonl(paddingRight); 141 paddingTop = htonl(paddingTop); 142 paddingBottom = htonl(paddingBottom); 143 for (int i=0; i<numColors; i++) { 144 colors[i] = htonl(colors[i]); 145 } 146} 147 148void Res_png_9patch::fileToDevice() 149{ 150 for (int i = 0; i < numXDivs; i++) { 151 xDivs[i] = ntohl(xDivs[i]); 152 } 153 for (int i = 0; i < numYDivs; i++) { 154 yDivs[i] = ntohl(yDivs[i]); 155 } 156 paddingLeft = ntohl(paddingLeft); 157 paddingRight = ntohl(paddingRight); 158 paddingTop = ntohl(paddingTop); 159 paddingBottom = ntohl(paddingBottom); 160 for (int i=0; i<numColors; i++) { 161 colors[i] = ntohl(colors[i]); 162 } 163} 164 165size_t Res_png_9patch::serializedSize() 166{ 167 // The size of this struct is 32 bytes on the 32-bit target system 168 // 4 * int8_t 169 // 4 * int32_t 170 // 3 * pointer 171 return 32 172 + numXDivs * sizeof(int32_t) 173 + numYDivs * sizeof(int32_t) 174 + numColors * sizeof(uint32_t); 175} 176 177void* Res_png_9patch::serialize() 178{ 179 // Use calloc since we're going to leave a few holes in the data 180 // and want this to run cleanly under valgrind 181 void* newData = calloc(1, serializedSize()); 182 serialize(newData); 183 return newData; 184} 185 186void Res_png_9patch::serialize(void * outData) 187{ 188 char* data = (char*) outData; 189 memmove(data, &wasDeserialized, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors 190 memmove(data + 12, &paddingLeft, 16); // copy paddingXXXX 191 data += 32; 192 193 memmove(data, this->xDivs, numXDivs * sizeof(int32_t)); 194 data += numXDivs * sizeof(int32_t); 195 memmove(data, this->yDivs, numYDivs * sizeof(int32_t)); 196 data += numYDivs * sizeof(int32_t); 197 memmove(data, this->colors, numColors * sizeof(uint32_t)); 198} 199 200static void deserializeInternal(const void* inData, Res_png_9patch* outData) { 201 char* patch = (char*) inData; 202 if (inData != outData) { 203 memmove(&outData->wasDeserialized, patch, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors 204 memmove(&outData->paddingLeft, patch + 12, 4); // copy wasDeserialized, numXDivs, numYDivs, numColors 205 } 206 outData->wasDeserialized = true; 207 char* data = (char*)outData; 208 data += sizeof(Res_png_9patch); 209 outData->xDivs = (int32_t*) data; 210 data += outData->numXDivs * sizeof(int32_t); 211 outData->yDivs = (int32_t*) data; 212 data += outData->numYDivs * sizeof(int32_t); 213 outData->colors = (uint32_t*) data; 214} 215 216Res_png_9patch* Res_png_9patch::deserialize(const void* inData) 217{ 218 if (sizeof(void*) != sizeof(int32_t)) { 219 LOGE("Cannot deserialize on non 32-bit system\n"); 220 return NULL; 221 } 222 deserializeInternal(inData, (Res_png_9patch*) inData); 223 return (Res_png_9patch*) inData; 224} 225 226// -------------------------------------------------------------------- 227// -------------------------------------------------------------------- 228// -------------------------------------------------------------------- 229 230ResStringPool::ResStringPool() 231 : mError(NO_INIT), mOwnedData(NULL) 232{ 233} 234 235ResStringPool::ResStringPool(const void* data, size_t size, bool copyData) 236 : mError(NO_INIT), mOwnedData(NULL) 237{ 238 setTo(data, size, copyData); 239} 240 241ResStringPool::~ResStringPool() 242{ 243 uninit(); 244} 245 246status_t ResStringPool::setTo(const void* data, size_t size, bool copyData) 247{ 248 if (!data || !size) { 249 return (mError=BAD_TYPE); 250 } 251 252 uninit(); 253 254 const bool notDeviceEndian = htods(0xf0) != 0xf0; 255 256 if (copyData || notDeviceEndian) { 257 mOwnedData = malloc(size); 258 if (mOwnedData == NULL) { 259 return (mError=NO_MEMORY); 260 } 261 memcpy(mOwnedData, data, size); 262 data = mOwnedData; 263 } 264 265 mHeader = (const ResStringPool_header*)data; 266 267 if (notDeviceEndian) { 268 ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader); 269 h->header.headerSize = dtohs(mHeader->header.headerSize); 270 h->header.type = dtohs(mHeader->header.type); 271 h->header.size = dtohl(mHeader->header.size); 272 h->stringCount = dtohl(mHeader->stringCount); 273 h->styleCount = dtohl(mHeader->styleCount); 274 h->flags = dtohl(mHeader->flags); 275 h->stringsStart = dtohl(mHeader->stringsStart); 276 h->stylesStart = dtohl(mHeader->stylesStart); 277 } 278 279 if (mHeader->header.headerSize > mHeader->header.size 280 || mHeader->header.size > size) { 281 LOGW("Bad string block: header size %d or total size %d is larger than data size %d\n", 282 (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size); 283 return (mError=BAD_TYPE); 284 } 285 mSize = mHeader->header.size; 286 mEntries = (const uint32_t*) 287 (((const uint8_t*)data)+mHeader->header.headerSize); 288 289 if (mHeader->stringCount > 0) { 290 if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount) // uint32 overflow? 291 || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))) 292 > size) { 293 LOGW("Bad string block: entry of %d items extends past data size %d\n", 294 (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))), 295 (int)size); 296 return (mError=BAD_TYPE); 297 } 298 mStrings = (const char16_t*) 299 (((const uint8_t*)data)+mHeader->stringsStart); 300 if (mHeader->stringsStart >= (mHeader->header.size-sizeof(uint16_t))) { 301 LOGW("Bad string block: string pool starts at %d, after total size %d\n", 302 (int)mHeader->stringsStart, (int)mHeader->header.size); 303 return (mError=BAD_TYPE); 304 } 305 if (mHeader->styleCount == 0) { 306 mStringPoolSize = 307 (mHeader->header.size-mHeader->stringsStart)/sizeof(uint16_t); 308 } else { 309 // check invariant: styles follow the strings 310 if (mHeader->stylesStart <= mHeader->stringsStart) { 311 LOGW("Bad style block: style block starts at %d, before strings at %d\n", 312 (int)mHeader->stylesStart, (int)mHeader->stringsStart); 313 return (mError=BAD_TYPE); 314 } 315 mStringPoolSize = 316 (mHeader->stylesStart-mHeader->stringsStart)/sizeof(uint16_t); 317 } 318 319 // check invariant: stringCount > 0 requires a string pool to exist 320 if (mStringPoolSize == 0) { 321 LOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount); 322 return (mError=BAD_TYPE); 323 } 324 325 if (notDeviceEndian) { 326 size_t i; 327 uint32_t* e = const_cast<uint32_t*>(mEntries); 328 for (i=0; i<mHeader->stringCount; i++) { 329 e[i] = dtohl(mEntries[i]); 330 } 331 char16_t* s = const_cast<char16_t*>(mStrings); 332 for (i=0; i<mStringPoolSize; i++) { 333 s[i] = dtohs(mStrings[i]); 334 } 335 } 336 337 if (mStrings[mStringPoolSize-1] != 0) { 338 LOGW("Bad string block: last string is not 0-terminated\n"); 339 return (mError=BAD_TYPE); 340 } 341 } else { 342 mStrings = NULL; 343 mStringPoolSize = 0; 344 } 345 346 if (mHeader->styleCount > 0) { 347 mEntryStyles = mEntries + mHeader->stringCount; 348 // invariant: integer overflow in calculating mEntryStyles 349 if (mEntryStyles < mEntries) { 350 LOGW("Bad string block: integer overflow finding styles\n"); 351 return (mError=BAD_TYPE); 352 } 353 354 if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) { 355 LOGW("Bad string block: entry of %d styles extends past data size %d\n", 356 (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader), 357 (int)size); 358 return (mError=BAD_TYPE); 359 } 360 mStyles = (const uint32_t*) 361 (((const uint8_t*)data)+mHeader->stylesStart); 362 if (mHeader->stylesStart >= mHeader->header.size) { 363 LOGW("Bad string block: style pool starts %d, after total size %d\n", 364 (int)mHeader->stylesStart, (int)mHeader->header.size); 365 return (mError=BAD_TYPE); 366 } 367 mStylePoolSize = 368 (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t); 369 370 if (notDeviceEndian) { 371 size_t i; 372 uint32_t* e = const_cast<uint32_t*>(mEntryStyles); 373 for (i=0; i<mHeader->styleCount; i++) { 374 e[i] = dtohl(mEntryStyles[i]); 375 } 376 uint32_t* s = const_cast<uint32_t*>(mStyles); 377 for (i=0; i<mStylePoolSize; i++) { 378 s[i] = dtohl(mStyles[i]); 379 } 380 } 381 382 const ResStringPool_span endSpan = { 383 { htodl(ResStringPool_span::END) }, 384 htodl(ResStringPool_span::END), htodl(ResStringPool_span::END) 385 }; 386 if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))], 387 &endSpan, sizeof(endSpan)) != 0) { 388 LOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n"); 389 return (mError=BAD_TYPE); 390 } 391 } else { 392 mEntryStyles = NULL; 393 mStyles = NULL; 394 mStylePoolSize = 0; 395 } 396 397 return (mError=NO_ERROR); 398} 399 400status_t ResStringPool::getError() const 401{ 402 return mError; 403} 404 405void ResStringPool::uninit() 406{ 407 mError = NO_INIT; 408 if (mOwnedData) { 409 free(mOwnedData); 410 mOwnedData = NULL; 411 } 412} 413 414const uint16_t* ResStringPool::stringAt(size_t idx, size_t* outLen) const 415{ 416 if (mError == NO_ERROR && idx < mHeader->stringCount) { 417 const uint32_t off = (mEntries[idx]/sizeof(uint16_t)); 418 if (off < (mStringPoolSize-1)) { 419 const char16_t* str = mStrings+off; 420 *outLen = *str; 421 if ((*str)&0x8000) { 422 str++; 423 *outLen = (((*outLen)&0x7fff)<<16) + *str; 424 } 425 if ((uint32_t)(str+1+*outLen-mStrings) < mStringPoolSize) { 426 return str+1; 427 } else { 428 LOGW("Bad string block: string #%d extends to %d, past end at %d\n", 429 (int)idx, (int)(str+1+*outLen-mStrings), (int)mStringPoolSize); 430 } 431 } else { 432 LOGW("Bad string block: string #%d entry is at %d, past end at %d\n", 433 (int)idx, (int)(off*sizeof(uint16_t)), 434 (int)(mStringPoolSize*sizeof(uint16_t))); 435 } 436 } 437 return NULL; 438} 439 440const ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const 441{ 442 return styleAt(ref.index); 443} 444 445const ResStringPool_span* ResStringPool::styleAt(size_t idx) const 446{ 447 if (mError == NO_ERROR && idx < mHeader->styleCount) { 448 const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t)); 449 if (off < mStylePoolSize) { 450 return (const ResStringPool_span*)(mStyles+off); 451 } else { 452 LOGW("Bad string block: style #%d entry is at %d, past end at %d\n", 453 (int)idx, (int)(off*sizeof(uint32_t)), 454 (int)(mStylePoolSize*sizeof(uint32_t))); 455 } 456 } 457 return NULL; 458} 459 460ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const 461{ 462 if (mError != NO_ERROR) { 463 return mError; 464 } 465 466 size_t len; 467 468 if (mHeader->flags&ResStringPool_header::SORTED_FLAG) { 469 // Do a binary search for the string... 470 ssize_t l = 0; 471 ssize_t h = mHeader->stringCount-1; 472 473 ssize_t mid; 474 while (l <= h) { 475 mid = l + (h - l)/2; 476 const char16_t* s = stringAt(mid, &len); 477 int c = s ? strzcmp16(s, len, str, strLen) : -1; 478 POOL_NOISY(printf("Looking for %s, at %s, cmp=%d, l/mid/h=%d/%d/%d\n", 479 String8(str).string(), 480 String8(s).string(), 481 c, (int)l, (int)mid, (int)h)); 482 if (c == 0) { 483 return mid; 484 } else if (c < 0) { 485 l = mid + 1; 486 } else { 487 h = mid - 1; 488 } 489 } 490 } else { 491 // It is unusual to get the ID from an unsorted string block... 492 // most often this happens because we want to get IDs for style 493 // span tags; since those always appear at the end of the string 494 // block, start searching at the back. 495 for (int i=mHeader->stringCount-1; i>=0; i--) { 496 const char16_t* s = stringAt(i, &len); 497 POOL_NOISY(printf("Looking for %s, at %s, i=%d\n", 498 String8(str, strLen).string(), 499 String8(s).string(), 500 i)); 501 if (s && strzcmp16(s, len, str, strLen) == 0) { 502 return i; 503 } 504 } 505 } 506 507 return NAME_NOT_FOUND; 508} 509 510size_t ResStringPool::size() const 511{ 512 return (mError == NO_ERROR) ? mHeader->stringCount : 0; 513} 514 515// -------------------------------------------------------------------- 516// -------------------------------------------------------------------- 517// -------------------------------------------------------------------- 518 519ResXMLParser::ResXMLParser(const ResXMLTree& tree) 520 : mTree(tree), mEventCode(BAD_DOCUMENT) 521{ 522} 523 524void ResXMLParser::restart() 525{ 526 mCurNode = NULL; 527 mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT; 528} 529 530ResXMLParser::event_code_t ResXMLParser::getEventType() const 531{ 532 return mEventCode; 533} 534 535ResXMLParser::event_code_t ResXMLParser::next() 536{ 537 if (mEventCode == START_DOCUMENT) { 538 mCurNode = mTree.mRootNode; 539 mCurExt = mTree.mRootExt; 540 return (mEventCode=mTree.mRootCode); 541 } else if (mEventCode >= FIRST_CHUNK_CODE) { 542 return nextNode(); 543 } 544 return mEventCode; 545} 546 547int32_t ResXMLParser::getCommentID() const 548{ 549 return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1; 550} 551 552const uint16_t* ResXMLParser::getComment(size_t* outLen) const 553{ 554 int32_t id = getCommentID(); 555 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 556} 557 558uint32_t ResXMLParser::getLineNumber() const 559{ 560 return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1; 561} 562 563int32_t ResXMLParser::getTextID() const 564{ 565 if (mEventCode == TEXT) { 566 return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index); 567 } 568 return -1; 569} 570 571const uint16_t* ResXMLParser::getText(size_t* outLen) const 572{ 573 int32_t id = getTextID(); 574 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 575} 576 577ssize_t ResXMLParser::getTextValue(Res_value* outValue) const 578{ 579 if (mEventCode == TEXT) { 580 outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData); 581 return sizeof(Res_value); 582 } 583 return BAD_TYPE; 584} 585 586int32_t ResXMLParser::getNamespacePrefixID() const 587{ 588 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { 589 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index); 590 } 591 return -1; 592} 593 594const uint16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const 595{ 596 int32_t id = getNamespacePrefixID(); 597 //printf("prefix=%d event=%p\n", id, mEventCode); 598 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 599} 600 601int32_t ResXMLParser::getNamespaceUriID() const 602{ 603 if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) { 604 return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index); 605 } 606 return -1; 607} 608 609const uint16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const 610{ 611 int32_t id = getNamespaceUriID(); 612 //printf("uri=%d event=%p\n", id, mEventCode); 613 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 614} 615 616int32_t ResXMLParser::getElementNamespaceID() const 617{ 618 if (mEventCode == START_TAG) { 619 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index); 620 } 621 if (mEventCode == END_TAG) { 622 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index); 623 } 624 return -1; 625} 626 627const uint16_t* ResXMLParser::getElementNamespace(size_t* outLen) const 628{ 629 int32_t id = getElementNamespaceID(); 630 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 631} 632 633int32_t ResXMLParser::getElementNameID() const 634{ 635 if (mEventCode == START_TAG) { 636 return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index); 637 } 638 if (mEventCode == END_TAG) { 639 return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index); 640 } 641 return -1; 642} 643 644const uint16_t* ResXMLParser::getElementName(size_t* outLen) const 645{ 646 int32_t id = getElementNameID(); 647 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 648} 649 650size_t ResXMLParser::getAttributeCount() const 651{ 652 if (mEventCode == START_TAG) { 653 return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount); 654 } 655 return 0; 656} 657 658int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const 659{ 660 if (mEventCode == START_TAG) { 661 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 662 if (idx < dtohs(tag->attributeCount)) { 663 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 664 (((const uint8_t*)tag) 665 + dtohs(tag->attributeStart) 666 + (dtohs(tag->attributeSize)*idx)); 667 return dtohl(attr->ns.index); 668 } 669 } 670 return -2; 671} 672 673const uint16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const 674{ 675 int32_t id = getAttributeNamespaceID(idx); 676 //printf("attribute namespace=%d idx=%d event=%p\n", id, idx, mEventCode); 677 //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id)); 678 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 679} 680 681int32_t ResXMLParser::getAttributeNameID(size_t idx) const 682{ 683 if (mEventCode == START_TAG) { 684 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 685 if (idx < dtohs(tag->attributeCount)) { 686 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 687 (((const uint8_t*)tag) 688 + dtohs(tag->attributeStart) 689 + (dtohs(tag->attributeSize)*idx)); 690 return dtohl(attr->name.index); 691 } 692 } 693 return -1; 694} 695 696const uint16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const 697{ 698 int32_t id = getAttributeNameID(idx); 699 //printf("attribute name=%d idx=%d event=%p\n", id, idx, mEventCode); 700 //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id)); 701 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 702} 703 704uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const 705{ 706 int32_t id = getAttributeNameID(idx); 707 if (id >= 0 && (size_t)id < mTree.mNumResIds) { 708 return dtohl(mTree.mResIds[id]); 709 } 710 return 0; 711} 712 713int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const 714{ 715 if (mEventCode == START_TAG) { 716 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 717 if (idx < dtohs(tag->attributeCount)) { 718 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 719 (((const uint8_t*)tag) 720 + dtohs(tag->attributeStart) 721 + (dtohs(tag->attributeSize)*idx)); 722 return dtohl(attr->rawValue.index); 723 } 724 } 725 return -1; 726} 727 728const uint16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const 729{ 730 int32_t id = getAttributeValueStringID(idx); 731 //XML_NOISY(printf("getAttributeValue 0x%x=0x%x\n", idx, id)); 732 return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL; 733} 734 735int32_t ResXMLParser::getAttributeDataType(size_t idx) const 736{ 737 if (mEventCode == START_TAG) { 738 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 739 if (idx < dtohs(tag->attributeCount)) { 740 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 741 (((const uint8_t*)tag) 742 + dtohs(tag->attributeStart) 743 + (dtohs(tag->attributeSize)*idx)); 744 return attr->typedValue.dataType; 745 } 746 } 747 return Res_value::TYPE_NULL; 748} 749 750int32_t ResXMLParser::getAttributeData(size_t idx) const 751{ 752 if (mEventCode == START_TAG) { 753 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 754 if (idx < dtohs(tag->attributeCount)) { 755 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 756 (((const uint8_t*)tag) 757 + dtohs(tag->attributeStart) 758 + (dtohs(tag->attributeSize)*idx)); 759 return dtohl(attr->typedValue.data); 760 } 761 } 762 return 0; 763} 764 765ssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const 766{ 767 if (mEventCode == START_TAG) { 768 const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt; 769 if (idx < dtohs(tag->attributeCount)) { 770 const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*) 771 (((const uint8_t*)tag) 772 + dtohs(tag->attributeStart) 773 + (dtohs(tag->attributeSize)*idx)); 774 outValue->copyFrom_dtoh(attr->typedValue); 775 return sizeof(Res_value); 776 } 777 } 778 return BAD_TYPE; 779} 780 781ssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const 782{ 783 String16 nsStr(ns != NULL ? ns : ""); 784 String16 attrStr(attr); 785 return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0, 786 attrStr.string(), attrStr.size()); 787} 788 789ssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen, 790 const char16_t* attr, size_t attrLen) const 791{ 792 if (mEventCode == START_TAG) { 793 const size_t N = getAttributeCount(); 794 for (size_t i=0; i<N; i++) { 795 size_t curNsLen, curAttrLen; 796 const char16_t* curNs = getAttributeNamespace(i, &curNsLen); 797 const char16_t* curAttr = getAttributeName(i, &curAttrLen); 798 //printf("%d: ns=%p attr=%p curNs=%p curAttr=%p\n", 799 // i, ns, attr, curNs, curAttr); 800 //printf(" --> attr=%s, curAttr=%s\n", 801 // String8(attr).string(), String8(curAttr).string()); 802 if (attr && curAttr && (strzcmp16(attr, attrLen, curAttr, curAttrLen) == 0)) { 803 if (ns == NULL) { 804 if (curNs == NULL) return i; 805 } else if (curNs != NULL) { 806 //printf(" --> ns=%s, curNs=%s\n", 807 // String8(ns).string(), String8(curNs).string()); 808 if (strzcmp16(ns, nsLen, curNs, curNsLen) == 0) return i; 809 } 810 } 811 } 812 } 813 814 return NAME_NOT_FOUND; 815} 816 817ssize_t ResXMLParser::indexOfID() const 818{ 819 if (mEventCode == START_TAG) { 820 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex); 821 if (idx > 0) return (idx-1); 822 } 823 return NAME_NOT_FOUND; 824} 825 826ssize_t ResXMLParser::indexOfClass() const 827{ 828 if (mEventCode == START_TAG) { 829 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex); 830 if (idx > 0) return (idx-1); 831 } 832 return NAME_NOT_FOUND; 833} 834 835ssize_t ResXMLParser::indexOfStyle() const 836{ 837 if (mEventCode == START_TAG) { 838 const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex); 839 if (idx > 0) return (idx-1); 840 } 841 return NAME_NOT_FOUND; 842} 843 844ResXMLParser::event_code_t ResXMLParser::nextNode() 845{ 846 if (mEventCode < 0) { 847 return mEventCode; 848 } 849 850 do { 851 const ResXMLTree_node* next = (const ResXMLTree_node*) 852 (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size)); 853 //LOGW("Next node: prev=%p, next=%p\n", mCurNode, next); 854 855 if (((const uint8_t*)next) >= mTree.mDataEnd) { 856 mCurNode = NULL; 857 return (mEventCode=END_DOCUMENT); 858 } 859 860 if (mTree.validateNode(next) != NO_ERROR) { 861 mCurNode = NULL; 862 return (mEventCode=BAD_DOCUMENT); 863 } 864 865 mCurNode = next; 866 const uint16_t headerSize = dtohs(next->header.headerSize); 867 const uint32_t totalSize = dtohl(next->header.size); 868 mCurExt = ((const uint8_t*)next) + headerSize; 869 size_t minExtSize = 0; 870 event_code_t eventCode = (event_code_t)dtohs(next->header.type); 871 switch ((mEventCode=eventCode)) { 872 case RES_XML_START_NAMESPACE_TYPE: 873 case RES_XML_END_NAMESPACE_TYPE: 874 minExtSize = sizeof(ResXMLTree_namespaceExt); 875 break; 876 case RES_XML_START_ELEMENT_TYPE: 877 minExtSize = sizeof(ResXMLTree_attrExt); 878 break; 879 case RES_XML_END_ELEMENT_TYPE: 880 minExtSize = sizeof(ResXMLTree_endElementExt); 881 break; 882 case RES_XML_CDATA_TYPE: 883 minExtSize = sizeof(ResXMLTree_cdataExt); 884 break; 885 default: 886 LOGW("Unknown XML block: header type %d in node at %d\n", 887 (int)dtohs(next->header.type), 888 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader))); 889 continue; 890 } 891 892 if ((totalSize-headerSize) < minExtSize) { 893 LOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n", 894 (int)dtohs(next->header.type), 895 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)), 896 (int)(totalSize-headerSize), (int)minExtSize); 897 return (mEventCode=BAD_DOCUMENT); 898 } 899 900 //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n", 901 // mCurNode, mCurExt, headerSize, minExtSize); 902 903 return eventCode; 904 } while (true); 905} 906 907void ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const 908{ 909 pos->eventCode = mEventCode; 910 pos->curNode = mCurNode; 911 pos->curExt = mCurExt; 912} 913 914void ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos) 915{ 916 mEventCode = pos.eventCode; 917 mCurNode = pos.curNode; 918 mCurExt = pos.curExt; 919} 920 921 922// -------------------------------------------------------------------- 923 924static volatile int32_t gCount = 0; 925 926ResXMLTree::ResXMLTree() 927 : ResXMLParser(*this) 928 , mError(NO_INIT), mOwnedData(NULL) 929{ 930 //LOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); 931 restart(); 932} 933 934ResXMLTree::ResXMLTree(const void* data, size_t size, bool copyData) 935 : ResXMLParser(*this) 936 , mError(NO_INIT), mOwnedData(NULL) 937{ 938 //LOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1); 939 setTo(data, size, copyData); 940} 941 942ResXMLTree::~ResXMLTree() 943{ 944 //LOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1); 945 uninit(); 946} 947 948status_t ResXMLTree::setTo(const void* data, size_t size, bool copyData) 949{ 950 uninit(); 951 mEventCode = START_DOCUMENT; 952 953 if (copyData) { 954 mOwnedData = malloc(size); 955 if (mOwnedData == NULL) { 956 return (mError=NO_MEMORY); 957 } 958 memcpy(mOwnedData, data, size); 959 data = mOwnedData; 960 } 961 962 mHeader = (const ResXMLTree_header*)data; 963 mSize = dtohl(mHeader->header.size); 964 if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) { 965 LOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n", 966 (int)dtohs(mHeader->header.headerSize), 967 (int)dtohl(mHeader->header.size), (int)size); 968 mError = BAD_TYPE; 969 restart(); 970 return mError; 971 } 972 mDataEnd = ((const uint8_t*)mHeader) + mSize; 973 974 mStrings.uninit(); 975 mRootNode = NULL; 976 mResIds = NULL; 977 mNumResIds = 0; 978 979 // First look for a couple interesting chunks: the string block 980 // and first XML node. 981 const ResChunk_header* chunk = 982 (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize)); 983 const ResChunk_header* lastChunk = chunk; 984 while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) && 985 ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) { 986 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML"); 987 if (err != NO_ERROR) { 988 mError = err; 989 goto done; 990 } 991 const uint16_t type = dtohs(chunk->type); 992 const size_t size = dtohl(chunk->size); 993 XML_NOISY(printf("Scanning @ %p: type=0x%x, size=0x%x\n", 994 (void*)(((uint32_t)chunk)-((uint32_t)mHeader)), type, size)); 995 if (type == RES_STRING_POOL_TYPE) { 996 mStrings.setTo(chunk, size); 997 } else if (type == RES_XML_RESOURCE_MAP_TYPE) { 998 mResIds = (const uint32_t*) 999 (((const uint8_t*)chunk)+dtohs(chunk->headerSize)); 1000 mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t); 1001 } else if (type >= RES_XML_FIRST_CHUNK_TYPE 1002 && type <= RES_XML_LAST_CHUNK_TYPE) { 1003 if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) { 1004 mError = BAD_TYPE; 1005 goto done; 1006 } 1007 mCurNode = (const ResXMLTree_node*)lastChunk; 1008 if (nextNode() == BAD_DOCUMENT) { 1009 mError = BAD_TYPE; 1010 goto done; 1011 } 1012 mRootNode = mCurNode; 1013 mRootExt = mCurExt; 1014 mRootCode = mEventCode; 1015 break; 1016 } else { 1017 XML_NOISY(printf("Skipping unknown chunk!\n")); 1018 } 1019 lastChunk = chunk; 1020 chunk = (const ResChunk_header*) 1021 (((const uint8_t*)chunk) + size); 1022 } 1023 1024 if (mRootNode == NULL) { 1025 LOGW("Bad XML block: no root element node found\n"); 1026 mError = BAD_TYPE; 1027 goto done; 1028 } 1029 1030 mError = mStrings.getError(); 1031 1032done: 1033 restart(); 1034 return mError; 1035} 1036 1037status_t ResXMLTree::getError() const 1038{ 1039 return mError; 1040} 1041 1042void ResXMLTree::uninit() 1043{ 1044 mError = NO_INIT; 1045 if (mOwnedData) { 1046 free(mOwnedData); 1047 mOwnedData = NULL; 1048 } 1049 restart(); 1050} 1051 1052const ResStringPool& ResXMLTree::getStrings() const 1053{ 1054 return mStrings; 1055} 1056 1057status_t ResXMLTree::validateNode(const ResXMLTree_node* node) const 1058{ 1059 const uint16_t eventCode = dtohs(node->header.type); 1060 1061 status_t err = validate_chunk( 1062 &node->header, sizeof(ResXMLTree_node), 1063 mDataEnd, "ResXMLTree_node"); 1064 1065 if (err >= NO_ERROR) { 1066 // Only perform additional validation on START nodes 1067 if (eventCode != RES_XML_START_ELEMENT_TYPE) { 1068 return NO_ERROR; 1069 } 1070 1071 const uint16_t headerSize = dtohs(node->header.headerSize); 1072 const uint32_t size = dtohl(node->header.size); 1073 const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*) 1074 (((const uint8_t*)node) + headerSize); 1075 // check for sensical values pulled out of the stream so far... 1076 if ((size >= headerSize + sizeof(ResXMLTree_attrExt)) 1077 && ((void*)attrExt > (void*)node)) { 1078 const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize)) 1079 * dtohs(attrExt->attributeCount); 1080 if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) { 1081 return NO_ERROR; 1082 } 1083 LOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n", 1084 (unsigned int)(dtohs(attrExt->attributeStart)+attrSize), 1085 (unsigned int)(size-headerSize)); 1086 } 1087 else { 1088 LOGW("Bad XML start block: node header size 0x%x, size 0x%x\n", 1089 (unsigned int)headerSize, (unsigned int)size); 1090 } 1091 return BAD_TYPE; 1092 } 1093 1094 return err; 1095 1096#if 0 1097 const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE; 1098 1099 const uint16_t headerSize = dtohs(node->header.headerSize); 1100 const uint32_t size = dtohl(node->header.size); 1101 1102 if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) { 1103 if (size >= headerSize) { 1104 if (((const uint8_t*)node) <= (mDataEnd-size)) { 1105 if (!isStart) { 1106 return NO_ERROR; 1107 } 1108 if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount)) 1109 <= (size-headerSize)) { 1110 return NO_ERROR; 1111 } 1112 LOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n", 1113 ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount), 1114 (int)(size-headerSize)); 1115 return BAD_TYPE; 1116 } 1117 LOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n", 1118 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize); 1119 return BAD_TYPE; 1120 } 1121 LOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n", 1122 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), 1123 (int)headerSize, (int)size); 1124 return BAD_TYPE; 1125 } 1126 LOGW("Bad XML block: node at 0x%x header size 0x%x too small\n", 1127 (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), 1128 (int)headerSize); 1129 return BAD_TYPE; 1130#endif 1131} 1132 1133// -------------------------------------------------------------------- 1134// -------------------------------------------------------------------- 1135// -------------------------------------------------------------------- 1136 1137struct ResTable::Header 1138{ 1139 Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL) { } 1140 1141 ResTable* const owner; 1142 void* ownedData; 1143 const ResTable_header* header; 1144 size_t size; 1145 const uint8_t* dataEnd; 1146 size_t index; 1147 void* cookie; 1148 1149 ResStringPool values; 1150}; 1151 1152struct ResTable::Type 1153{ 1154 Type(const Header* _header, const Package* _package, size_t count) 1155 : header(_header), package(_package), entryCount(count), 1156 typeSpec(NULL), typeSpecFlags(NULL) { } 1157 const Header* const header; 1158 const Package* const package; 1159 const size_t entryCount; 1160 const ResTable_typeSpec* typeSpec; 1161 const uint32_t* typeSpecFlags; 1162 Vector<const ResTable_type*> configs; 1163}; 1164 1165struct ResTable::Package 1166{ 1167 Package(ResTable* _owner, const Header* _header, const ResTable_package* _package) 1168 : owner(_owner), header(_header), package(_package) { } 1169 ~Package() 1170 { 1171 size_t i = types.size(); 1172 while (i > 0) { 1173 i--; 1174 delete types[i]; 1175 } 1176 } 1177 1178 ResTable* const owner; 1179 const Header* const header; 1180 const ResTable_package* const package; 1181 Vector<Type*> types; 1182 1183 ResStringPool typeStrings; 1184 ResStringPool keyStrings; 1185 1186 const Type* getType(size_t idx) const { 1187 return idx < types.size() ? types[idx] : NULL; 1188 } 1189}; 1190 1191// A group of objects describing a particular resource package. 1192// The first in 'package' is always the root object (from the resource 1193// table that defined the package); the ones after are skins on top of it. 1194struct ResTable::PackageGroup 1195{ 1196 PackageGroup(ResTable* _owner, const String16& _name, uint32_t _id) 1197 : owner(_owner), name(_name), id(_id), typeCount(0), bags(NULL) { } 1198 ~PackageGroup() { 1199 clearBagCache(); 1200 const size_t N = packages.size(); 1201 for (size_t i=0; i<N; i++) { 1202 Package* pkg = packages[i]; 1203 if (pkg->owner == owner) { 1204 delete pkg; 1205 } 1206 } 1207 } 1208 1209 void clearBagCache() { 1210 if (bags) { 1211 TABLE_NOISY(printf("bags=%p\n", bags)); 1212 Package* pkg = packages[0]; 1213 TABLE_NOISY(printf("typeCount=%x\n", typeCount)); 1214 for (size_t i=0; i<typeCount; i++) { 1215 TABLE_NOISY(printf("type=%d\n", i)); 1216 const Type* type = pkg->getType(i); 1217 if (type != NULL) { 1218 bag_set** typeBags = bags[i]; 1219 TABLE_NOISY(printf("typeBags=%p\n", typeBags)); 1220 if (typeBags) { 1221 TABLE_NOISY(printf("type->entryCount=%x\n", type->entryCount)); 1222 const size_t N = type->entryCount; 1223 for (size_t j=0; j<N; j++) { 1224 if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF) 1225 free(typeBags[j]); 1226 } 1227 free(typeBags); 1228 } 1229 } 1230 } 1231 free(bags); 1232 bags = NULL; 1233 } 1234 } 1235 1236 ResTable* const owner; 1237 String16 const name; 1238 uint32_t const id; 1239 Vector<Package*> packages; 1240 1241 // This is for finding typeStrings and other common package stuff. 1242 Package* basePackage; 1243 1244 // For quick access. 1245 size_t typeCount; 1246 1247 // Computed attribute bags, first indexed by the type and second 1248 // by the entry in that type. 1249 bag_set*** bags; 1250}; 1251 1252struct ResTable::bag_set 1253{ 1254 size_t numAttrs; // number in array 1255 size_t availAttrs; // total space in array 1256 uint32_t typeSpecFlags; 1257 // Followed by 'numAttr' bag_entry structures. 1258}; 1259 1260ResTable::Theme::Theme(const ResTable& table) 1261 : mTable(table) 1262{ 1263 memset(mPackages, 0, sizeof(mPackages)); 1264} 1265 1266ResTable::Theme::~Theme() 1267{ 1268 for (size_t i=0; i<Res_MAXPACKAGE; i++) { 1269 package_info* pi = mPackages[i]; 1270 if (pi != NULL) { 1271 free_package(pi); 1272 } 1273 } 1274} 1275 1276void ResTable::Theme::free_package(package_info* pi) 1277{ 1278 for (size_t j=0; j<pi->numTypes; j++) { 1279 theme_entry* te = pi->types[j].entries; 1280 if (te != NULL) { 1281 free(te); 1282 } 1283 } 1284 free(pi); 1285} 1286 1287ResTable::Theme::package_info* ResTable::Theme::copy_package(package_info* pi) 1288{ 1289 package_info* newpi = (package_info*)malloc( 1290 sizeof(package_info) + (pi->numTypes*sizeof(type_info))); 1291 newpi->numTypes = pi->numTypes; 1292 for (size_t j=0; j<newpi->numTypes; j++) { 1293 size_t cnt = pi->types[j].numEntries; 1294 newpi->types[j].numEntries = cnt; 1295 theme_entry* te = pi->types[j].entries; 1296 if (te != NULL) { 1297 theme_entry* newte = (theme_entry*)malloc(cnt*sizeof(theme_entry)); 1298 newpi->types[j].entries = newte; 1299 memcpy(newte, te, cnt*sizeof(theme_entry)); 1300 } else { 1301 newpi->types[j].entries = NULL; 1302 } 1303 } 1304 return newpi; 1305} 1306 1307status_t ResTable::Theme::applyStyle(uint32_t resID, bool force) 1308{ 1309 const bag_entry* bag; 1310 uint32_t bagTypeSpecFlags = 0; 1311 mTable.lock(); 1312 const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags); 1313 TABLE_NOISY(LOGV("Applying style 0x%08x to theme %p, count=%d", resID, this, N)); 1314 if (N < 0) { 1315 mTable.unlock(); 1316 return N; 1317 } 1318 1319 uint32_t curPackage = 0xffffffff; 1320 ssize_t curPackageIndex = 0; 1321 package_info* curPI = NULL; 1322 uint32_t curType = 0xffffffff; 1323 size_t numEntries = 0; 1324 theme_entry* curEntries = NULL; 1325 1326 const bag_entry* end = bag + N; 1327 while (bag < end) { 1328 const uint32_t attrRes = bag->map.name.ident; 1329 const uint32_t p = Res_GETPACKAGE(attrRes); 1330 const uint32_t t = Res_GETTYPE(attrRes); 1331 const uint32_t e = Res_GETENTRY(attrRes); 1332 1333 if (curPackage != p) { 1334 const ssize_t pidx = mTable.getResourcePackageIndex(attrRes); 1335 if (pidx < 0) { 1336 LOGE("Style contains key with bad package: 0x%08x\n", attrRes); 1337 bag++; 1338 continue; 1339 } 1340 curPackage = p; 1341 curPackageIndex = pidx; 1342 curPI = mPackages[pidx]; 1343 if (curPI == NULL) { 1344 PackageGroup* const grp = mTable.mPackageGroups[pidx]; 1345 int cnt = grp->typeCount; 1346 curPI = (package_info*)malloc( 1347 sizeof(package_info) + (cnt*sizeof(type_info))); 1348 curPI->numTypes = cnt; 1349 memset(curPI->types, 0, cnt*sizeof(type_info)); 1350 mPackages[pidx] = curPI; 1351 } 1352 curType = 0xffffffff; 1353 } 1354 if (curType != t) { 1355 if (t >= curPI->numTypes) { 1356 LOGE("Style contains key with bad type: 0x%08x\n", attrRes); 1357 bag++; 1358 continue; 1359 } 1360 curType = t; 1361 curEntries = curPI->types[t].entries; 1362 if (curEntries == NULL) { 1363 PackageGroup* const grp = mTable.mPackageGroups[curPackageIndex]; 1364 const Type* type = grp->packages[0]->getType(t); 1365 int cnt = type != NULL ? type->entryCount : 0; 1366 curEntries = (theme_entry*)malloc(cnt*sizeof(theme_entry)); 1367 memset(curEntries, Res_value::TYPE_NULL, cnt*sizeof(theme_entry)); 1368 curPI->types[t].numEntries = cnt; 1369 curPI->types[t].entries = curEntries; 1370 } 1371 numEntries = curPI->types[t].numEntries; 1372 } 1373 if (e >= numEntries) { 1374 LOGE("Style contains key with bad entry: 0x%08x\n", attrRes); 1375 bag++; 1376 continue; 1377 } 1378 theme_entry* curEntry = curEntries + e; 1379 TABLE_NOISY(LOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x", 1380 attrRes, bag->map.value.dataType, bag->map.value.data, 1381 curEntry->value.dataType)); 1382 if (force || curEntry->value.dataType == Res_value::TYPE_NULL) { 1383 curEntry->stringBlock = bag->stringBlock; 1384 curEntry->typeSpecFlags |= bagTypeSpecFlags; 1385 curEntry->value = bag->map.value; 1386 } 1387 1388 bag++; 1389 } 1390 1391 mTable.unlock(); 1392 1393 //LOGI("Applying style 0x%08x (force=%d) theme %p...\n", resID, force, this); 1394 //dumpToLog(); 1395 1396 return NO_ERROR; 1397} 1398 1399status_t ResTable::Theme::setTo(const Theme& other) 1400{ 1401 //LOGI("Setting theme %p from theme %p...\n", this, &other); 1402 //dumpToLog(); 1403 //other.dumpToLog(); 1404 1405 if (&mTable == &other.mTable) { 1406 for (size_t i=0; i<Res_MAXPACKAGE; i++) { 1407 if (mPackages[i] != NULL) { 1408 free_package(mPackages[i]); 1409 } 1410 if (other.mPackages[i] != NULL) { 1411 mPackages[i] = copy_package(other.mPackages[i]); 1412 } else { 1413 mPackages[i] = NULL; 1414 } 1415 } 1416 } else { 1417 // @todo: need to really implement this, not just copy 1418 // the system package (which is still wrong because it isn't 1419 // fixing up resource references). 1420 for (size_t i=0; i<Res_MAXPACKAGE; i++) { 1421 if (mPackages[i] != NULL) { 1422 free_package(mPackages[i]); 1423 } 1424 if (i == 0 && other.mPackages[i] != NULL) { 1425 mPackages[i] = copy_package(other.mPackages[i]); 1426 } else { 1427 mPackages[i] = NULL; 1428 } 1429 } 1430 } 1431 1432 //LOGI("Final theme:"); 1433 //dumpToLog(); 1434 1435 return NO_ERROR; 1436} 1437 1438ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue, 1439 uint32_t* outTypeSpecFlags) const 1440{ 1441 int cnt = 20; 1442 1443 if (outTypeSpecFlags != NULL) *outTypeSpecFlags = 0; 1444 1445 do { 1446 const ssize_t p = mTable.getResourcePackageIndex(resID); 1447 const uint32_t t = Res_GETTYPE(resID); 1448 const uint32_t e = Res_GETENTRY(resID); 1449 1450 TABLE_NOISY(LOGV("Looking up attr 0x%08x in theme %p", resID, this)); 1451 1452 if (p >= 0) { 1453 const package_info* const pi = mPackages[p]; 1454 if (pi != NULL) { 1455 if (t < pi->numTypes) { 1456 const type_info& ti = pi->types[t]; 1457 if (e < ti.numEntries) { 1458 const theme_entry& te = ti.entries[e]; 1459 if (outTypeSpecFlags != NULL) { 1460 *outTypeSpecFlags |= te.typeSpecFlags; 1461 } 1462 const uint8_t type = te.value.dataType; 1463 if (type == Res_value::TYPE_ATTRIBUTE) { 1464 if (cnt > 0) { 1465 cnt--; 1466 resID = te.value.data; 1467 continue; 1468 } 1469 LOGW("Too many attribute references, stopped at: 0x%08x\n", resID); 1470 return BAD_INDEX; 1471 } else if (type != Res_value::TYPE_NULL) { 1472 *outValue = te.value; 1473 return te.stringBlock; 1474 } 1475 return BAD_INDEX; 1476 } 1477 } 1478 } 1479 } 1480 break; 1481 1482 } while (true); 1483 1484 return BAD_INDEX; 1485} 1486 1487ssize_t ResTable::Theme::resolveAttributeReference(Res_value* inOutValue, 1488 ssize_t blockIndex, uint32_t* outLastRef, 1489 uint32_t* inoutTypeSpecFlags) const 1490{ 1491 //printf("Resolving type=0x%x\n", inOutValue->dataType); 1492 if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) { 1493 uint32_t newTypeSpecFlags; 1494 blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags); 1495 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags; 1496 //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType); 1497 if (blockIndex < 0) { 1498 return blockIndex; 1499 } 1500 } 1501 return mTable.resolveReference(inOutValue, blockIndex, outLastRef); 1502} 1503 1504void ResTable::Theme::dumpToLog() const 1505{ 1506 LOGI("Theme %p:\n", this); 1507 for (size_t i=0; i<Res_MAXPACKAGE; i++) { 1508 package_info* pi = mPackages[i]; 1509 if (pi == NULL) continue; 1510 1511 LOGI(" Package #0x%02x:\n", (int)(i+1)); 1512 for (size_t j=0; j<pi->numTypes; j++) { 1513 type_info& ti = pi->types[j]; 1514 if (ti.numEntries == 0) continue; 1515 1516 LOGI(" Type #0x%02x:\n", (int)(j+1)); 1517 for (size_t k=0; k<ti.numEntries; k++) { 1518 theme_entry& te = ti.entries[k]; 1519 if (te.value.dataType == Res_value::TYPE_NULL) continue; 1520 LOGI(" 0x%08x: t=0x%x, d=0x%08x (block=%d)\n", 1521 (int)Res_MAKEID(i, j, k), 1522 te.value.dataType, (int)te.value.data, (int)te.stringBlock); 1523 } 1524 } 1525 } 1526} 1527 1528ResTable::ResTable() 1529 : mError(NO_INIT) 1530{ 1531 memset(&mParams, 0, sizeof(mParams)); 1532 memset(mPackageMap, 0, sizeof(mPackageMap)); 1533 //LOGI("Creating ResTable %p\n", this); 1534} 1535 1536ResTable::ResTable(const void* data, size_t size, void* cookie, bool copyData) 1537 : mError(NO_INIT) 1538{ 1539 memset(&mParams, 0, sizeof(mParams)); 1540 memset(mPackageMap, 0, sizeof(mPackageMap)); 1541 add(data, size, cookie, copyData); 1542 LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table"); 1543 //LOGI("Creating ResTable %p\n", this); 1544} 1545 1546ResTable::~ResTable() 1547{ 1548 //LOGI("Destroying ResTable in %p\n", this); 1549 uninit(); 1550} 1551 1552inline ssize_t ResTable::getResourcePackageIndex(uint32_t resID) const 1553{ 1554 return ((ssize_t)mPackageMap[Res_GETPACKAGE(resID)+1])-1; 1555} 1556 1557status_t ResTable::add(const void* data, size_t size, void* cookie, bool copyData) 1558{ 1559 return add(data, size, cookie, NULL, copyData); 1560} 1561 1562status_t ResTable::add(Asset* asset, void* cookie, bool copyData) 1563{ 1564 const void* data = asset->getBuffer(true); 1565 if (data == NULL) { 1566 LOGW("Unable to get buffer of resource asset file"); 1567 return UNKNOWN_ERROR; 1568 } 1569 size_t size = (size_t)asset->getLength(); 1570 return add(data, size, cookie, asset, copyData); 1571} 1572 1573status_t ResTable::add(ResTable* src) 1574{ 1575 mError = src->mError; 1576 1577 for (size_t i=0; i<src->mHeaders.size(); i++) { 1578 mHeaders.add(src->mHeaders[i]); 1579 } 1580 1581 for (size_t i=0; i<src->mPackageGroups.size(); i++) { 1582 PackageGroup* srcPg = src->mPackageGroups[i]; 1583 PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id); 1584 for (size_t j=0; j<srcPg->packages.size(); j++) { 1585 pg->packages.add(srcPg->packages[j]); 1586 } 1587 pg->basePackage = srcPg->basePackage; 1588 pg->typeCount = srcPg->typeCount; 1589 mPackageGroups.add(pg); 1590 } 1591 1592 memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap)); 1593 1594 return mError; 1595} 1596 1597status_t ResTable::add(const void* data, size_t size, void* cookie, 1598 Asset* asset, bool copyData) 1599{ 1600 if (!data) return NO_ERROR; 1601 Header* header = new Header(this); 1602 header->index = mHeaders.size(); 1603 header->cookie = cookie; 1604 mHeaders.add(header); 1605 1606 const bool notDeviceEndian = htods(0xf0) != 0xf0; 1607 1608 LOAD_TABLE_NOISY( 1609 LOGV("Adding resources to ResTable: data=%p, size=0x%x, cookie=%p, asset=%p, copy=%d\n", 1610 data, size, cookie, asset, copyData)); 1611 1612 if (copyData || notDeviceEndian) { 1613 header->ownedData = malloc(size); 1614 if (header->ownedData == NULL) { 1615 return (mError=NO_MEMORY); 1616 } 1617 memcpy(header->ownedData, data, size); 1618 data = header->ownedData; 1619 } 1620 1621 header->header = (const ResTable_header*)data; 1622 header->size = dtohl(header->header->header.size); 1623 //LOGI("Got size 0x%x, again size 0x%x, raw size 0x%x\n", header->size, 1624 // dtohl(header->header->header.size), header->header->header.size); 1625 LOAD_TABLE_NOISY(LOGV("Loading ResTable @%p:\n", header->header)); 1626 LOAD_TABLE_NOISY(printHexData(2, header->header, header->size < 256 ? header->size : 256, 1627 16, 16, 0, false, printToLogFunc)); 1628 if (dtohs(header->header->header.headerSize) > header->size 1629 || header->size > size) { 1630 LOGW("Bad resource table: header size 0x%x or total size 0x%x is larger than data size 0x%x\n", 1631 (int)dtohs(header->header->header.headerSize), 1632 (int)header->size, (int)size); 1633 return (mError=BAD_TYPE); 1634 } 1635 if (((dtohs(header->header->header.headerSize)|header->size)&0x3) != 0) { 1636 LOGW("Bad resource table: header size 0x%x or total size 0x%x is not on an integer boundary\n", 1637 (int)dtohs(header->header->header.headerSize), 1638 (int)header->size); 1639 return (mError=BAD_TYPE); 1640 } 1641 header->dataEnd = ((const uint8_t*)header->header) + header->size; 1642 1643 // Iterate through all chunks. 1644 size_t curPackage = 0; 1645 1646 const ResChunk_header* chunk = 1647 (const ResChunk_header*)(((const uint8_t*)header->header) 1648 + dtohs(header->header->header.headerSize)); 1649 while (((const uint8_t*)chunk) <= (header->dataEnd-sizeof(ResChunk_header)) && 1650 ((const uint8_t*)chunk) <= (header->dataEnd-dtohl(chunk->size))) { 1651 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), header->dataEnd, "ResTable"); 1652 if (err != NO_ERROR) { 1653 return (mError=err); 1654 } 1655 TABLE_NOISY(LOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", 1656 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), 1657 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)))); 1658 const size_t csize = dtohl(chunk->size); 1659 const uint16_t ctype = dtohs(chunk->type); 1660 if (ctype == RES_STRING_POOL_TYPE) { 1661 if (header->values.getError() != NO_ERROR) { 1662 // Only use the first string chunk; ignore any others that 1663 // may appear. 1664 status_t err = header->values.setTo(chunk, csize); 1665 if (err != NO_ERROR) { 1666 return (mError=err); 1667 } 1668 } else { 1669 LOGW("Multiple string chunks found in resource table."); 1670 } 1671 } else if (ctype == RES_TABLE_PACKAGE_TYPE) { 1672 if (curPackage >= dtohl(header->header->packageCount)) { 1673 LOGW("More package chunks were found than the %d declared in the header.", 1674 dtohl(header->header->packageCount)); 1675 return (mError=BAD_TYPE); 1676 } 1677 if (parsePackage((ResTable_package*)chunk, header) != NO_ERROR) { 1678 return mError; 1679 } 1680 curPackage++; 1681 } else { 1682 LOGW("Unknown chunk type %p in table at %p.\n", 1683 (void*)(int)(ctype), 1684 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))); 1685 } 1686 chunk = (const ResChunk_header*) 1687 (((const uint8_t*)chunk) + csize); 1688 } 1689 1690 if (curPackage < dtohl(header->header->packageCount)) { 1691 LOGW("Fewer package chunks (%d) were found than the %d declared in the header.", 1692 (int)curPackage, dtohl(header->header->packageCount)); 1693 return (mError=BAD_TYPE); 1694 } 1695 mError = header->values.getError(); 1696 if (mError != NO_ERROR) { 1697 LOGW("No string values found in resource table!"); 1698 } 1699 TABLE_NOISY(LOGV("Returning from add with mError=%d\n", mError)); 1700 return mError; 1701} 1702 1703status_t ResTable::getError() const 1704{ 1705 return mError; 1706} 1707 1708void ResTable::uninit() 1709{ 1710 mError = NO_INIT; 1711 size_t N = mPackageGroups.size(); 1712 for (size_t i=0; i<N; i++) { 1713 PackageGroup* g = mPackageGroups[i]; 1714 delete g; 1715 } 1716 N = mHeaders.size(); 1717 for (size_t i=0; i<N; i++) { 1718 Header* header = mHeaders[i]; 1719 if (header->owner == this) { 1720 if (header->ownedData) { 1721 free(header->ownedData); 1722 } 1723 delete header; 1724 } 1725 } 1726 1727 mPackageGroups.clear(); 1728 mHeaders.clear(); 1729} 1730 1731bool ResTable::getResourceName(uint32_t resID, resource_name* outName) const 1732{ 1733 if (mError != NO_ERROR) { 1734 return false; 1735 } 1736 1737 const ssize_t p = getResourcePackageIndex(resID); 1738 const int t = Res_GETTYPE(resID); 1739 const int e = Res_GETENTRY(resID); 1740 1741 if (p < 0) { 1742 LOGW("No package identifier when getting name for resource number 0x%08x", resID); 1743 return false; 1744 } 1745 if (t < 0) { 1746 LOGW("No type identifier when getting name for resource number 0x%08x", resID); 1747 return false; 1748 } 1749 1750 const PackageGroup* const grp = mPackageGroups[p]; 1751 if (grp == NULL) { 1752 LOGW("Bad identifier when getting name for resource number 0x%08x", resID); 1753 return false; 1754 } 1755 if (grp->packages.size() > 0) { 1756 const Package* const package = grp->packages[0]; 1757 1758 const ResTable_type* type; 1759 const ResTable_entry* entry; 1760 ssize_t offset = getEntry(package, t, e, NULL, &type, &entry, NULL); 1761 if (offset <= 0) { 1762 return false; 1763 } 1764 1765 outName->package = grp->name.string(); 1766 outName->packageLen = grp->name.size(); 1767 outName->type = grp->basePackage->typeStrings.stringAt(t, &outName->typeLen); 1768 outName->name = grp->basePackage->keyStrings.stringAt( 1769 dtohl(entry->key.index), &outName->nameLen); 1770 return true; 1771 } 1772 1773 return false; 1774} 1775 1776ssize_t ResTable::getResource(uint32_t resID, Res_value* outValue, bool mayBeBag, 1777 uint32_t* outSpecFlags, ResTable_config* outConfig) const 1778{ 1779 if (mError != NO_ERROR) { 1780 return mError; 1781 } 1782 1783 const ssize_t p = getResourcePackageIndex(resID); 1784 const int t = Res_GETTYPE(resID); 1785 const int e = Res_GETENTRY(resID); 1786 1787 if (p < 0) { 1788 LOGW("No package identifier when getting value for resource number 0x%08x", resID); 1789 return BAD_INDEX; 1790 } 1791 if (t < 0) { 1792 LOGW("No type identifier when getting value for resource number 0x%08x", resID); 1793 return BAD_INDEX; 1794 } 1795 1796 const Res_value* bestValue = NULL; 1797 const Package* bestPackage = NULL; 1798 ResTable_config bestItem; 1799 memset(&bestItem, 0, sizeof(bestItem)); // make the compiler shut up 1800 1801 if (outSpecFlags != NULL) *outSpecFlags = 0; 1802 1803 // Look through all resource packages, starting with the most 1804 // recently added. 1805 const PackageGroup* const grp = mPackageGroups[p]; 1806 if (grp == NULL) { 1807 LOGW("Bad identifier when getting value for resource number 0x%08x", resID); 1808 return false; 1809 } 1810 size_t ip = grp->packages.size(); 1811 while (ip > 0) { 1812 ip--; 1813 1814 const Package* const package = grp->packages[ip]; 1815 1816 const ResTable_type* type; 1817 const ResTable_entry* entry; 1818 const Type* typeClass; 1819 ssize_t offset = getEntry(package, t, e, &mParams, &type, &entry, &typeClass); 1820 if (offset <= 0) { 1821 if (offset < 0) { 1822 LOGW("Failure getting entry for 0x%08x (t=%d e=%d) in package %d: 0x%08x\n", 1823 resID, t, e, (int)ip, (int)offset); 1824 return offset; 1825 } 1826 continue; 1827 } 1828 1829 if ((dtohs(entry->flags)&entry->FLAG_COMPLEX) != 0) { 1830 if (!mayBeBag) { 1831 LOGW("Requesting resource %p failed because it is complex\n", 1832 (void*)resID); 1833 } 1834 continue; 1835 } 1836 1837 TABLE_NOISY(aout << "Resource type data: " 1838 << HexDump(type, dtohl(type->header.size)) << endl); 1839 1840 if ((size_t)offset > (dtohl(type->header.size)-sizeof(Res_value))) { 1841 LOGW("ResTable_item at %d is beyond type chunk data %d", 1842 (int)offset, dtohl(type->header.size)); 1843 return BAD_TYPE; 1844 } 1845 1846 const Res_value* item = 1847 (const Res_value*)(((const uint8_t*)type) + offset); 1848 ResTable_config thisConfig; 1849 thisConfig.copyFromDtoH(type->config); 1850 1851 if (outSpecFlags != NULL) { 1852 if (typeClass->typeSpecFlags != NULL) { 1853 *outSpecFlags |= dtohl(typeClass->typeSpecFlags[e]); 1854 } else { 1855 *outSpecFlags = -1; 1856 } 1857 } 1858 1859 if (bestPackage != NULL && bestItem.isMoreSpecificThan(thisConfig)) { 1860 continue; 1861 } 1862 1863 bestItem = thisConfig; 1864 bestValue = item; 1865 bestPackage = package; 1866 } 1867 1868 TABLE_NOISY(printf("Found result: package %p\n", bestPackage)); 1869 1870 if (bestValue) { 1871 outValue->size = dtohs(bestValue->size); 1872 outValue->res0 = bestValue->res0; 1873 outValue->dataType = bestValue->dataType; 1874 outValue->data = dtohl(bestValue->data); 1875 if (outConfig != NULL) { 1876 *outConfig = bestItem; 1877 } 1878 TABLE_NOISY(size_t len; 1879 printf("Found value: pkg=%d, type=%d, str=%s, int=%d\n", 1880 bestPackage->header->index, 1881 outValue->dataType, 1882 outValue->dataType == bestValue->TYPE_STRING 1883 ? String8(bestPackage->header->values.stringAt( 1884 outValue->data, &len)).string() 1885 : "", 1886 outValue->data)); 1887 return bestPackage->header->index; 1888 } 1889 1890 return BAD_INDEX; 1891} 1892 1893ssize_t ResTable::resolveReference(Res_value* value, ssize_t blockIndex, 1894 uint32_t* outLastRef, uint32_t* inoutTypeSpecFlags) const 1895{ 1896 int count=0; 1897 while (blockIndex >= 0 && value->dataType == value->TYPE_REFERENCE 1898 && value->data != 0 && count < 20) { 1899 if (outLastRef) *outLastRef = value->data; 1900 uint32_t lastRef = value->data; 1901 uint32_t newFlags = 0; 1902 const ssize_t newIndex = getResource(value->data, value, true, &newFlags); 1903 //LOGI("Resolving reference d=%p: newIndex=%d, t=0x%02x, d=%p\n", 1904 // (void*)lastRef, (int)newIndex, (int)value->dataType, (void*)value->data); 1905 //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex); 1906 if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags; 1907 if (newIndex < 0) { 1908 // This can fail if the resource being referenced is a style... 1909 // in this case, just return the reference, and expect the 1910 // caller to deal with. 1911 return blockIndex; 1912 } 1913 blockIndex = newIndex; 1914 count++; 1915 } 1916 return blockIndex; 1917} 1918 1919const char16_t* ResTable::valueToString( 1920 const Res_value* value, size_t stringBlock, 1921 char16_t tmpBuffer[TMP_BUFFER_SIZE], size_t* outLen) 1922{ 1923 if (!value) { 1924 return NULL; 1925 } 1926 if (value->dataType == value->TYPE_STRING) { 1927 return getTableStringBlock(stringBlock)->stringAt(value->data, outLen); 1928 } 1929 // XXX do int to string conversions. 1930 return NULL; 1931} 1932 1933ssize_t ResTable::lockBag(uint32_t resID, const bag_entry** outBag) const 1934{ 1935 mLock.lock(); 1936 ssize_t err = getBagLocked(resID, outBag); 1937 if (err < NO_ERROR) { 1938 //printf("*** get failed! unlocking\n"); 1939 mLock.unlock(); 1940 } 1941 return err; 1942} 1943 1944void ResTable::unlockBag(const bag_entry* bag) const 1945{ 1946 //printf("<<< unlockBag %p\n", this); 1947 mLock.unlock(); 1948} 1949 1950void ResTable::lock() const 1951{ 1952 mLock.lock(); 1953} 1954 1955void ResTable::unlock() const 1956{ 1957 mLock.unlock(); 1958} 1959 1960ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag, 1961 uint32_t* outTypeSpecFlags) const 1962{ 1963 if (mError != NO_ERROR) { 1964 return mError; 1965 } 1966 1967 const ssize_t p = getResourcePackageIndex(resID); 1968 const int t = Res_GETTYPE(resID); 1969 const int e = Res_GETENTRY(resID); 1970 1971 if (p < 0) { 1972 LOGW("Invalid package identifier when getting bag for resource number 0x%08x", resID); 1973 return BAD_INDEX; 1974 } 1975 if (t < 0) { 1976 LOGW("No type identifier when getting bag for resource number 0x%08x", resID); 1977 return BAD_INDEX; 1978 } 1979 1980 //printf("Get bag: id=0x%08x, p=%d, t=%d\n", resID, p, t); 1981 PackageGroup* const grp = mPackageGroups[p]; 1982 if (grp == NULL) { 1983 LOGW("Bad identifier when getting bag for resource number 0x%08x", resID); 1984 return false; 1985 } 1986 1987 if (t >= (int)grp->typeCount) { 1988 LOGW("Type identifier 0x%x is larger than type count 0x%x", 1989 t+1, (int)grp->typeCount); 1990 return BAD_INDEX; 1991 } 1992 1993 const Package* const basePackage = grp->packages[0]; 1994 1995 const Type* const typeConfigs = basePackage->getType(t); 1996 1997 const size_t NENTRY = typeConfigs->entryCount; 1998 if (e >= (int)NENTRY) { 1999 LOGW("Entry identifier 0x%x is larger than entry count 0x%x", 2000 e, (int)typeConfigs->entryCount); 2001 return BAD_INDEX; 2002 } 2003 2004 // First see if we've already computed this bag... 2005 if (grp->bags) { 2006 bag_set** typeSet = grp->bags[t]; 2007 if (typeSet) { 2008 bag_set* set = typeSet[e]; 2009 if (set) { 2010 if (set != (bag_set*)0xFFFFFFFF) { 2011 if (outTypeSpecFlags != NULL) { 2012 *outTypeSpecFlags = set->typeSpecFlags; 2013 } 2014 *outBag = (bag_entry*)(set+1); 2015 //LOGI("Found existing bag for: %p\n", (void*)resID); 2016 return set->numAttrs; 2017 } 2018 LOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.", 2019 resID); 2020 return BAD_INDEX; 2021 } 2022 } 2023 } 2024 2025 // Bag not found, we need to compute it! 2026 if (!grp->bags) { 2027 grp->bags = (bag_set***)malloc(sizeof(bag_set*)*grp->typeCount); 2028 if (!grp->bags) return NO_MEMORY; 2029 memset(grp->bags, 0, sizeof(bag_set*)*grp->typeCount); 2030 } 2031 2032 bag_set** typeSet = grp->bags[t]; 2033 if (!typeSet) { 2034 typeSet = (bag_set**)malloc(sizeof(bag_set*)*NENTRY); 2035 if (!typeSet) return NO_MEMORY; 2036 memset(typeSet, 0, sizeof(bag_set*)*NENTRY); 2037 grp->bags[t] = typeSet; 2038 } 2039 2040 // Mark that we are currently working on this one. 2041 typeSet[e] = (bag_set*)0xFFFFFFFF; 2042 2043 // This is what we are building. 2044 bag_set* set = NULL; 2045 2046 TABLE_NOISY(LOGI("Building bag: %p\n", (void*)resID)); 2047 2048 // Now collect all bag attributes from all packages. 2049 size_t ip = grp->packages.size(); 2050 while (ip > 0) { 2051 ip--; 2052 2053 const Package* const package = grp->packages[ip]; 2054 2055 const ResTable_type* type; 2056 const ResTable_entry* entry; 2057 const Type* typeClass; 2058 LOGV("Getting entry pkg=%p, t=%d, e=%d\n", package, t, e); 2059 ssize_t offset = getEntry(package, t, e, &mParams, &type, &entry, &typeClass); 2060 LOGV("Resulting offset=%d\n", offset); 2061 if (offset <= 0) { 2062 if (offset < 0) { 2063 if (set) free(set); 2064 return offset; 2065 } 2066 continue; 2067 } 2068 2069 if ((dtohs(entry->flags)&entry->FLAG_COMPLEX) == 0) { 2070 LOGW("Skipping entry %p in package table %d because it is not complex!\n", 2071 (void*)resID, (int)ip); 2072 continue; 2073 } 2074 2075 const uint16_t entrySize = dtohs(entry->size); 2076 const uint32_t parent = entrySize >= sizeof(ResTable_map_entry) 2077 ? dtohl(((const ResTable_map_entry*)entry)->parent.ident) : 0; 2078 const uint32_t count = entrySize >= sizeof(ResTable_map_entry) 2079 ? dtohl(((const ResTable_map_entry*)entry)->count) : 0; 2080 2081 size_t N = count; 2082 2083 TABLE_NOISY(LOGI("Found map: size=%p parent=%p count=%d\n", 2084 entrySize, parent, count)); 2085 2086 if (set == NULL) { 2087 // If this map inherits from another, we need to start 2088 // with its parent's values. Otherwise start out empty. 2089 TABLE_NOISY(printf("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", 2090 entrySize, parent)); 2091 if (parent) { 2092 const bag_entry* parentBag; 2093 uint32_t parentTypeSpecFlags = 0; 2094 const ssize_t NP = getBagLocked(parent, &parentBag, &parentTypeSpecFlags); 2095 const size_t NT = ((NP >= 0) ? NP : 0) + N; 2096 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*NT); 2097 if (set == NULL) { 2098 return NO_MEMORY; 2099 } 2100 if (NP > 0) { 2101 memcpy(set+1, parentBag, NP*sizeof(bag_entry)); 2102 set->numAttrs = NP; 2103 TABLE_NOISY(LOGI("Initialized new bag with %d inherited attributes.\n", NP)); 2104 } else { 2105 TABLE_NOISY(LOGI("Initialized new bag with no inherited attributes.\n")); 2106 set->numAttrs = 0; 2107 } 2108 set->availAttrs = NT; 2109 set->typeSpecFlags = parentTypeSpecFlags; 2110 } else { 2111 set = (bag_set*)malloc(sizeof(bag_set)+sizeof(bag_entry)*N); 2112 if (set == NULL) { 2113 return NO_MEMORY; 2114 } 2115 set->numAttrs = 0; 2116 set->availAttrs = N; 2117 set->typeSpecFlags = 0; 2118 } 2119 } 2120 2121 if (typeClass->typeSpecFlags != NULL) { 2122 set->typeSpecFlags |= dtohl(typeClass->typeSpecFlags[e]); 2123 } else { 2124 set->typeSpecFlags = -1; 2125 } 2126 2127 // Now merge in the new attributes... 2128 ssize_t curOff = offset; 2129 const ResTable_map* map; 2130 bag_entry* entries = (bag_entry*)(set+1); 2131 size_t curEntry = 0; 2132 uint32_t pos = 0; 2133 TABLE_NOISY(LOGI("Starting with set %p, entries=%p, avail=%d\n", 2134 set, entries, set->availAttrs)); 2135 while (pos < count) { 2136 TABLE_NOISY(printf("Now at %p\n", (void*)curOff)); 2137 2138 if ((size_t)curOff > (dtohl(type->header.size)-sizeof(ResTable_map))) { 2139 LOGW("ResTable_map at %d is beyond type chunk data %d", 2140 (int)curOff, dtohl(type->header.size)); 2141 return BAD_TYPE; 2142 } 2143 map = (const ResTable_map*)(((const uint8_t*)type) + curOff); 2144 N++; 2145 2146 const uint32_t newName = htodl(map->name.ident); 2147 bool isInside; 2148 uint32_t oldName = 0; 2149 while ((isInside=(curEntry < set->numAttrs)) 2150 && (oldName=entries[curEntry].map.name.ident) < newName) { 2151 TABLE_NOISY(printf("#%d: Keeping existing attribute: 0x%08x\n", 2152 curEntry, entries[curEntry].map.name.ident)); 2153 curEntry++; 2154 } 2155 2156 if ((!isInside) || oldName != newName) { 2157 // This is a new attribute... figure out what to do with it. 2158 if (set->numAttrs >= set->availAttrs) { 2159 // Need to alloc more memory... 2160 const size_t newAvail = set->availAttrs+N; 2161 set = (bag_set*)realloc(set, 2162 sizeof(bag_set) 2163 + sizeof(bag_entry)*newAvail); 2164 if (set == NULL) { 2165 return NO_MEMORY; 2166 } 2167 set->availAttrs = newAvail; 2168 entries = (bag_entry*)(set+1); 2169 TABLE_NOISY(printf("Reallocated set %p, entries=%p, avail=%d\n", 2170 set, entries, set->availAttrs)); 2171 } 2172 if (isInside) { 2173 // Going in the middle, need to make space. 2174 memmove(entries+curEntry+1, entries+curEntry, 2175 sizeof(bag_entry)*(set->numAttrs-curEntry)); 2176 set->numAttrs++; 2177 } 2178 TABLE_NOISY(printf("#%d: Inserting new attribute: 0x%08x\n", 2179 curEntry, newName)); 2180 } else { 2181 TABLE_NOISY(printf("#%d: Replacing existing attribute: 0x%08x\n", 2182 curEntry, oldName)); 2183 } 2184 2185 bag_entry* cur = entries+curEntry; 2186 2187 cur->stringBlock = package->header->index; 2188 cur->map.name.ident = newName; 2189 cur->map.value.copyFrom_dtoh(map->value); 2190 TABLE_NOISY(printf("Setting entry #%d %p: block=%d, name=0x%08x, type=%d, data=0x%08x\n", 2191 curEntry, cur, cur->stringBlock, cur->map.name.ident, 2192 cur->map.value.dataType, cur->map.value.data)); 2193 2194 // On to the next! 2195 curEntry++; 2196 pos++; 2197 const size_t size = dtohs(map->value.size); 2198 curOff += size + sizeof(*map)-sizeof(map->value); 2199 }; 2200 if (curEntry > set->numAttrs) { 2201 set->numAttrs = curEntry; 2202 } 2203 } 2204 2205 // And this is it... 2206 typeSet[e] = set; 2207 if (set) { 2208 if (outTypeSpecFlags != NULL) { 2209 *outTypeSpecFlags = set->typeSpecFlags; 2210 } 2211 *outBag = (bag_entry*)(set+1); 2212 TABLE_NOISY(LOGI("Returning %d attrs\n", set->numAttrs)); 2213 return set->numAttrs; 2214 } 2215 return BAD_INDEX; 2216} 2217 2218void ResTable::setParameters(const ResTable_config* params) 2219{ 2220 mLock.lock(); 2221 TABLE_GETENTRY(LOGI("Setting parameters: imsi:%d/%d lang:%c%c cnt:%c%c " 2222 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", 2223 params->mcc, params->mnc, 2224 params->language[0] ? params->language[0] : '-', 2225 params->language[1] ? params->language[1] : '-', 2226 params->country[0] ? params->country[0] : '-', 2227 params->country[1] ? params->country[1] : '-', 2228 params->orientation, 2229 params->touchscreen, 2230 params->density, 2231 params->keyboard, 2232 params->inputFlags, 2233 params->navigation, 2234 params->screenWidth, 2235 params->screenHeight)); 2236 mParams = *params; 2237 for (size_t i=0; i<mPackageGroups.size(); i++) { 2238 TABLE_NOISY(LOGI("CLEARING BAGS FOR GROUP %d!", i)); 2239 mPackageGroups[i]->clearBagCache(); 2240 } 2241 mLock.unlock(); 2242} 2243 2244void ResTable::getParameters(ResTable_config* params) const 2245{ 2246 mLock.lock(); 2247 *params = mParams; 2248 mLock.unlock(); 2249} 2250 2251struct id_name_map { 2252 uint32_t id; 2253 size_t len; 2254 char16_t name[6]; 2255}; 2256 2257const static id_name_map ID_NAMES[] = { 2258 { ResTable_map::ATTR_TYPE, 5, { '^', 't', 'y', 'p', 'e' } }, 2259 { ResTable_map::ATTR_L10N, 5, { '^', 'l', '1', '0', 'n' } }, 2260 { ResTable_map::ATTR_MIN, 4, { '^', 'm', 'i', 'n' } }, 2261 { ResTable_map::ATTR_MAX, 4, { '^', 'm', 'a', 'x' } }, 2262 { ResTable_map::ATTR_OTHER, 6, { '^', 'o', 't', 'h', 'e', 'r' } }, 2263 { ResTable_map::ATTR_ZERO, 5, { '^', 'z', 'e', 'r', 'o' } }, 2264 { ResTable_map::ATTR_ONE, 4, { '^', 'o', 'n', 'e' } }, 2265 { ResTable_map::ATTR_TWO, 4, { '^', 't', 'w', 'o' } }, 2266 { ResTable_map::ATTR_FEW, 4, { '^', 'f', 'e', 'w' } }, 2267 { ResTable_map::ATTR_MANY, 5, { '^', 'm', 'a', 'n', 'y' } }, 2268}; 2269 2270uint32_t ResTable::identifierForName(const char16_t* name, size_t nameLen, 2271 const char16_t* type, size_t typeLen, 2272 const char16_t* package, 2273 size_t packageLen, 2274 uint32_t* outTypeSpecFlags) const 2275{ 2276 TABLE_SUPER_NOISY(printf("Identifier for name: error=%d\n", mError)); 2277 2278 // Check for internal resource identifier as the very first thing, so 2279 // that we will always find them even when there are no resources. 2280 if (name[0] == '^') { 2281 const int N = (sizeof(ID_NAMES)/sizeof(ID_NAMES[0])); 2282 size_t len; 2283 for (int i=0; i<N; i++) { 2284 const id_name_map* m = ID_NAMES + i; 2285 len = m->len; 2286 if (len != nameLen) { 2287 continue; 2288 } 2289 for (size_t j=1; j<len; j++) { 2290 if (m->name[j] != name[j]) { 2291 goto nope; 2292 } 2293 } 2294 return m->id; 2295nope: 2296 ; 2297 } 2298 if (nameLen > 7) { 2299 if (name[1] == 'i' && name[2] == 'n' 2300 && name[3] == 'd' && name[4] == 'e' && name[5] == 'x' 2301 && name[6] == '_') { 2302 int index = atoi(String8(name + 7, nameLen - 7).string()); 2303 if (Res_CHECKID(index)) { 2304 LOGW("Array resource index: %d is too large.", 2305 index); 2306 return 0; 2307 } 2308 return Res_MAKEARRAY(index); 2309 } 2310 } 2311 return 0; 2312 } 2313 2314 if (mError != NO_ERROR) { 2315 return 0; 2316 } 2317 2318 // Figure out the package and type we are looking in... 2319 2320 const char16_t* packageEnd = NULL; 2321 const char16_t* typeEnd = NULL; 2322 const char16_t* const nameEnd = name+nameLen; 2323 const char16_t* p = name; 2324 while (p < nameEnd) { 2325 if (*p == ':') packageEnd = p; 2326 else if (*p == '/') typeEnd = p; 2327 p++; 2328 } 2329 if (*name == '@') name++; 2330 if (name >= nameEnd) { 2331 return 0; 2332 } 2333 2334 if (packageEnd) { 2335 package = name; 2336 packageLen = packageEnd-name; 2337 name = packageEnd+1; 2338 } else if (!package) { 2339 return 0; 2340 } 2341 2342 if (typeEnd) { 2343 type = name; 2344 typeLen = typeEnd-name; 2345 name = typeEnd+1; 2346 } else if (!type) { 2347 return 0; 2348 } 2349 2350 if (name >= nameEnd) { 2351 return 0; 2352 } 2353 nameLen = nameEnd-name; 2354 2355 TABLE_NOISY(printf("Looking for identifier: type=%s, name=%s, package=%s\n", 2356 String8(type, typeLen).string(), 2357 String8(name, nameLen).string(), 2358 String8(package, packageLen).string())); 2359 2360 const size_t NG = mPackageGroups.size(); 2361 for (size_t ig=0; ig<NG; ig++) { 2362 const PackageGroup* group = mPackageGroups[ig]; 2363 2364 if (strzcmp16(package, packageLen, 2365 group->name.string(), group->name.size())) { 2366 TABLE_NOISY(printf("Skipping package group: %s\n", String8(group->name).string())); 2367 continue; 2368 } 2369 2370 const ssize_t ti = group->basePackage->typeStrings.indexOfString(type, typeLen); 2371 if (ti < 0) { 2372 TABLE_NOISY(printf("Type not found in package %s\n", String8(group->name).string())); 2373 continue; 2374 } 2375 2376 const ssize_t ei = group->basePackage->keyStrings.indexOfString(name, nameLen); 2377 if (ei < 0) { 2378 TABLE_NOISY(printf("Name not found in package %s\n", String8(group->name).string())); 2379 continue; 2380 } 2381 2382 TABLE_NOISY(printf("Search indices: type=%d, name=%d\n", ti, ei)); 2383 2384 const Type* const typeConfigs = group->packages[0]->getType(ti); 2385 if (typeConfigs == NULL || typeConfigs->configs.size() <= 0) { 2386 TABLE_NOISY(printf("Expected type structure not found in package %s for idnex %d\n", 2387 String8(group->name).string(), ti)); 2388 } 2389 2390 size_t NTC = typeConfigs->configs.size(); 2391 for (size_t tci=0; tci<NTC; tci++) { 2392 const ResTable_type* const ty = typeConfigs->configs[tci]; 2393 const uint32_t typeOffset = dtohl(ty->entriesStart); 2394 2395 const uint8_t* const end = ((const uint8_t*)ty) + dtohl(ty->header.size); 2396 const uint32_t* const eindex = (const uint32_t*) 2397 (((const uint8_t*)ty) + dtohs(ty->header.headerSize)); 2398 2399 const size_t NE = dtohl(ty->entryCount); 2400 for (size_t i=0; i<NE; i++) { 2401 uint32_t offset = dtohl(eindex[i]); 2402 if (offset == ResTable_type::NO_ENTRY) { 2403 continue; 2404 } 2405 2406 offset += typeOffset; 2407 2408 if (offset > (dtohl(ty->header.size)-sizeof(ResTable_entry))) { 2409 LOGW("ResTable_entry at %d is beyond type chunk data %d", 2410 offset, dtohl(ty->header.size)); 2411 return 0; 2412 } 2413 if ((offset&0x3) != 0) { 2414 LOGW("ResTable_entry at %d (pkg=%d type=%d ent=%d) is not on an integer boundary when looking for %s:%s/%s", 2415 (int)offset, (int)group->id, (int)ti+1, (int)i, 2416 String8(package, packageLen).string(), 2417 String8(type, typeLen).string(), 2418 String8(name, nameLen).string()); 2419 return 0; 2420 } 2421 2422 const ResTable_entry* const entry = (const ResTable_entry*) 2423 (((const uint8_t*)ty) + offset); 2424 if (dtohs(entry->size) < sizeof(*entry)) { 2425 LOGW("ResTable_entry size %d is too small", dtohs(entry->size)); 2426 return BAD_TYPE; 2427 } 2428 2429 TABLE_SUPER_NOISY(printf("Looking at entry #%d: want str %d, have %d\n", 2430 i, ei, dtohl(entry->key.index))); 2431 if (dtohl(entry->key.index) == (size_t)ei) { 2432 if (outTypeSpecFlags) { 2433 *outTypeSpecFlags = typeConfigs->typeSpecFlags[i]; 2434 } 2435 return Res_MAKEID(group->id-1, ti, i); 2436 } 2437 } 2438 } 2439 } 2440 2441 return 0; 2442} 2443 2444bool ResTable::expandResourceRef(const uint16_t* refStr, size_t refLen, 2445 String16* outPackage, 2446 String16* outType, 2447 String16* outName, 2448 const String16* defType, 2449 const String16* defPackage, 2450 const char** outErrorMsg) 2451{ 2452 const char16_t* packageEnd = NULL; 2453 const char16_t* typeEnd = NULL; 2454 const char16_t* p = refStr; 2455 const char16_t* const end = p + refLen; 2456 while (p < end) { 2457 if (*p == ':') packageEnd = p; 2458 else if (*p == '/') { 2459 typeEnd = p; 2460 break; 2461 } 2462 p++; 2463 } 2464 p = refStr; 2465 if (*p == '@') p++; 2466 2467 if (packageEnd) { 2468 *outPackage = String16(p, packageEnd-p); 2469 p = packageEnd+1; 2470 } else { 2471 if (!defPackage) { 2472 if (outErrorMsg) { 2473 *outErrorMsg = "No resource package specified"; 2474 } 2475 return false; 2476 } 2477 *outPackage = *defPackage; 2478 } 2479 if (typeEnd) { 2480 *outType = String16(p, typeEnd-p); 2481 p = typeEnd+1; 2482 } else { 2483 if (!defType) { 2484 if (outErrorMsg) { 2485 *outErrorMsg = "No resource type specified"; 2486 } 2487 return false; 2488 } 2489 *outType = *defType; 2490 } 2491 *outName = String16(p, end-p); 2492 return true; 2493} 2494 2495static uint32_t get_hex(char c, bool* outError) 2496{ 2497 if (c >= '0' && c <= '9') { 2498 return c - '0'; 2499 } else if (c >= 'a' && c <= 'f') { 2500 return c - 'a' + 0xa; 2501 } else if (c >= 'A' && c <= 'F') { 2502 return c - 'A' + 0xa; 2503 } 2504 *outError = true; 2505 return 0; 2506} 2507 2508struct unit_entry 2509{ 2510 const char* name; 2511 size_t len; 2512 uint8_t type; 2513 uint32_t unit; 2514 float scale; 2515}; 2516 2517static const unit_entry unitNames[] = { 2518 { "px", strlen("px"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PX, 1.0f }, 2519 { "dip", strlen("dip"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, 2520 { "dp", strlen("dp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_DIP, 1.0f }, 2521 { "sp", strlen("sp"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_SP, 1.0f }, 2522 { "pt", strlen("pt"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_PT, 1.0f }, 2523 { "in", strlen("in"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_IN, 1.0f }, 2524 { "mm", strlen("mm"), Res_value::TYPE_DIMENSION, Res_value::COMPLEX_UNIT_MM, 1.0f }, 2525 { "%", strlen("%"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION, 1.0f/100 }, 2526 { "%p", strlen("%p"), Res_value::TYPE_FRACTION, Res_value::COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100 }, 2527 { NULL, 0, 0, 0, 0 } 2528}; 2529 2530static bool parse_unit(const char* str, Res_value* outValue, 2531 float* outScale, const char** outEnd) 2532{ 2533 const char* end = str; 2534 while (*end != 0 && !isspace((unsigned char)*end)) { 2535 end++; 2536 } 2537 const size_t len = end-str; 2538 2539 const char* realEnd = end; 2540 while (*realEnd != 0 && isspace((unsigned char)*realEnd)) { 2541 realEnd++; 2542 } 2543 if (*realEnd != 0) { 2544 return false; 2545 } 2546 2547 const unit_entry* cur = unitNames; 2548 while (cur->name) { 2549 if (len == cur->len && strncmp(cur->name, str, len) == 0) { 2550 outValue->dataType = cur->type; 2551 outValue->data = cur->unit << Res_value::COMPLEX_UNIT_SHIFT; 2552 *outScale = cur->scale; 2553 *outEnd = end; 2554 //printf("Found unit %s for %s\n", cur->name, str); 2555 return true; 2556 } 2557 cur++; 2558 } 2559 2560 return false; 2561} 2562 2563 2564bool ResTable::stringToInt(const char16_t* s, size_t len, Res_value* outValue) 2565{ 2566 while (len > 0 && isspace16(*s)) { 2567 s++; 2568 len--; 2569 } 2570 2571 if (len <= 0) { 2572 return false; 2573 } 2574 2575 size_t i = 0; 2576 int32_t val = 0; 2577 bool neg = false; 2578 2579 if (*s == '-') { 2580 neg = true; 2581 i++; 2582 } 2583 2584 if (s[i] < '0' || s[i] > '9') { 2585 return false; 2586 } 2587 2588 // Decimal or hex? 2589 if (s[i] == '0' && s[i+1] == 'x') { 2590 if (outValue) 2591 outValue->dataType = outValue->TYPE_INT_HEX; 2592 i += 2; 2593 bool error = false; 2594 while (i < len && !error) { 2595 val = (val*16) + get_hex(s[i], &error); 2596 i++; 2597 } 2598 if (error) { 2599 return false; 2600 } 2601 } else { 2602 if (outValue) 2603 outValue->dataType = outValue->TYPE_INT_DEC; 2604 while (i < len) { 2605 if (s[i] < '0' || s[i] > '9') { 2606 return false; 2607 } 2608 val = (val*10) + s[i]-'0'; 2609 i++; 2610 } 2611 } 2612 2613 if (neg) val = -val; 2614 2615 while (i < len && isspace16(s[i])) { 2616 i++; 2617 } 2618 2619 if (i == len) { 2620 if (outValue) 2621 outValue->data = val; 2622 return true; 2623 } 2624 2625 return false; 2626} 2627 2628bool ResTable::stringToFloat(const char16_t* s, size_t len, Res_value* outValue) 2629{ 2630 while (len > 0 && isspace16(*s)) { 2631 s++; 2632 len--; 2633 } 2634 2635 if (len <= 0) { 2636 return false; 2637 } 2638 2639 char buf[128]; 2640 int i=0; 2641 while (len > 0 && *s != 0 && i < 126) { 2642 if (*s > 255) { 2643 return false; 2644 } 2645 buf[i++] = *s++; 2646 len--; 2647 } 2648 2649 if (len > 0) { 2650 return false; 2651 } 2652 if (buf[0] < '0' && buf[0] > '9' && buf[0] != '.') { 2653 return false; 2654 } 2655 2656 buf[i] = 0; 2657 const char* end; 2658 float f = strtof(buf, (char**)&end); 2659 2660 if (*end != 0 && !isspace((unsigned char)*end)) { 2661 // Might be a unit... 2662 float scale; 2663 if (parse_unit(end, outValue, &scale, &end)) { 2664 f *= scale; 2665 const bool neg = f < 0; 2666 if (neg) f = -f; 2667 uint64_t bits = (uint64_t)(f*(1<<23)+.5f); 2668 uint32_t radix; 2669 uint32_t shift; 2670 if ((bits&0x7fffff) == 0) { 2671 // Always use 23p0 if there is no fraction, just to make 2672 // things easier to read. 2673 radix = Res_value::COMPLEX_RADIX_23p0; 2674 shift = 23; 2675 } else if ((bits&0xffffffffff800000LL) == 0) { 2676 // Magnitude is zero -- can fit in 0 bits of precision. 2677 radix = Res_value::COMPLEX_RADIX_0p23; 2678 shift = 0; 2679 } else if ((bits&0xffffffff80000000LL) == 0) { 2680 // Magnitude can fit in 8 bits of precision. 2681 radix = Res_value::COMPLEX_RADIX_8p15; 2682 shift = 8; 2683 } else if ((bits&0xffffff8000000000LL) == 0) { 2684 // Magnitude can fit in 16 bits of precision. 2685 radix = Res_value::COMPLEX_RADIX_16p7; 2686 shift = 16; 2687 } else { 2688 // Magnitude needs entire range, so no fractional part. 2689 radix = Res_value::COMPLEX_RADIX_23p0; 2690 shift = 23; 2691 } 2692 int32_t mantissa = (int32_t)( 2693 (bits>>shift) & Res_value::COMPLEX_MANTISSA_MASK); 2694 if (neg) { 2695 mantissa = (-mantissa) & Res_value::COMPLEX_MANTISSA_MASK; 2696 } 2697 outValue->data |= 2698 (radix<<Res_value::COMPLEX_RADIX_SHIFT) 2699 | (mantissa<<Res_value::COMPLEX_MANTISSA_SHIFT); 2700 //printf("Input value: %f 0x%016Lx, mult: %f, radix: %d, shift: %d, final: 0x%08x\n", 2701 // f * (neg ? -1 : 1), bits, f*(1<<23), 2702 // radix, shift, outValue->data); 2703 return true; 2704 } 2705 return false; 2706 } 2707 2708 while (*end != 0 && isspace((unsigned char)*end)) { 2709 end++; 2710 } 2711 2712 if (*end == 0) { 2713 if (outValue) { 2714 outValue->dataType = outValue->TYPE_FLOAT; 2715 *(float*)(&outValue->data) = f; 2716 return true; 2717 } 2718 } 2719 2720 return false; 2721} 2722 2723bool ResTable::stringToValue(Res_value* outValue, String16* outString, 2724 const char16_t* s, size_t len, 2725 bool preserveSpaces, bool coerceType, 2726 uint32_t attrID, 2727 const String16* defType, 2728 const String16* defPackage, 2729 Accessor* accessor, 2730 void* accessorCookie, 2731 uint32_t attrType, 2732 bool enforcePrivate) const 2733{ 2734 bool localizationSetting = accessor != NULL && accessor->getLocalizationSetting(); 2735 const char* errorMsg = NULL; 2736 2737 outValue->size = sizeof(Res_value); 2738 outValue->res0 = 0; 2739 2740 // First strip leading/trailing whitespace. Do this before handling 2741 // escapes, so they can be used to force whitespace into the string. 2742 if (!preserveSpaces) { 2743 while (len > 0 && isspace16(*s)) { 2744 s++; 2745 len--; 2746 } 2747 while (len > 0 && isspace16(s[len-1])) { 2748 len--; 2749 } 2750 // If the string ends with '\', then we keep the space after it. 2751 if (len > 0 && s[len-1] == '\\' && s[len] != 0) { 2752 len++; 2753 } 2754 } 2755 2756 //printf("Value for: %s\n", String8(s, len).string()); 2757 2758 uint32_t l10nReq = ResTable_map::L10N_NOT_REQUIRED; 2759 uint32_t attrMin = 0x80000000, attrMax = 0x7fffffff; 2760 bool fromAccessor = false; 2761 if (attrID != 0 && !Res_INTERNALID(attrID)) { 2762 const ssize_t p = getResourcePackageIndex(attrID); 2763 const bag_entry* bag; 2764 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; 2765 //printf("For attr 0x%08x got bag of %d\n", attrID, cnt); 2766 if (cnt >= 0) { 2767 while (cnt > 0) { 2768 //printf("Entry 0x%08x = 0x%08x\n", bag->map.name.ident, bag->map.value.data); 2769 switch (bag->map.name.ident) { 2770 case ResTable_map::ATTR_TYPE: 2771 attrType = bag->map.value.data; 2772 break; 2773 case ResTable_map::ATTR_MIN: 2774 attrMin = bag->map.value.data; 2775 break; 2776 case ResTable_map::ATTR_MAX: 2777 attrMax = bag->map.value.data; 2778 break; 2779 case ResTable_map::ATTR_L10N: 2780 l10nReq = bag->map.value.data; 2781 break; 2782 } 2783 bag++; 2784 cnt--; 2785 } 2786 unlockBag(bag); 2787 } else if (accessor && accessor->getAttributeType(attrID, &attrType)) { 2788 fromAccessor = true; 2789 if (attrType == ResTable_map::TYPE_ENUM 2790 || attrType == ResTable_map::TYPE_FLAGS 2791 || attrType == ResTable_map::TYPE_INTEGER) { 2792 accessor->getAttributeMin(attrID, &attrMin); 2793 accessor->getAttributeMax(attrID, &attrMax); 2794 } 2795 if (localizationSetting) { 2796 l10nReq = accessor->getAttributeL10N(attrID); 2797 } 2798 } 2799 } 2800 2801 const bool canStringCoerce = 2802 coerceType && (attrType&ResTable_map::TYPE_STRING) != 0; 2803 2804 if (*s == '@') { 2805 outValue->dataType = outValue->TYPE_REFERENCE; 2806 2807 // Note: we don't check attrType here because the reference can 2808 // be to any other type; we just need to count on the client making 2809 // sure the referenced type is correct. 2810 2811 //printf("Looking up ref: %s\n", String8(s, len).string()); 2812 2813 // It's a reference! 2814 if (len == 5 && s[1]=='n' && s[2]=='u' && s[3]=='l' && s[4]=='l') { 2815 outValue->data = 0; 2816 return true; 2817 } else { 2818 bool createIfNotFound = false; 2819 const char16_t* resourceRefName; 2820 int resourceNameLen; 2821 if (len > 2 && s[1] == '+') { 2822 createIfNotFound = true; 2823 resourceRefName = s + 2; 2824 resourceNameLen = len - 2; 2825 } else if (len > 2 && s[1] == '*') { 2826 enforcePrivate = false; 2827 resourceRefName = s + 2; 2828 resourceNameLen = len - 2; 2829 } else { 2830 createIfNotFound = false; 2831 resourceRefName = s + 1; 2832 resourceNameLen = len - 1; 2833 } 2834 String16 package, type, name; 2835 if (!expandResourceRef(resourceRefName,resourceNameLen, &package, &type, &name, 2836 defType, defPackage, &errorMsg)) { 2837 if (accessor != NULL) { 2838 accessor->reportError(accessorCookie, errorMsg); 2839 } 2840 return false; 2841 } 2842 2843 uint32_t specFlags = 0; 2844 uint32_t rid = identifierForName(name.string(), name.size(), type.string(), 2845 type.size(), package.string(), package.size(), &specFlags); 2846 if (rid != 0) { 2847 if (enforcePrivate) { 2848 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { 2849 if (accessor != NULL) { 2850 accessor->reportError(accessorCookie, "Resource is not public."); 2851 } 2852 return false; 2853 } 2854 } 2855 if (!accessor) { 2856 outValue->data = rid; 2857 return true; 2858 } 2859 rid = Res_MAKEID( 2860 accessor->getRemappedPackage(Res_GETPACKAGE(rid)), 2861 Res_GETTYPE(rid), Res_GETENTRY(rid)); 2862 TABLE_NOISY(printf("Incl %s:%s/%s: 0x%08x\n", 2863 String8(package).string(), String8(type).string(), 2864 String8(name).string(), rid)); 2865 outValue->data = rid; 2866 return true; 2867 } 2868 2869 if (accessor) { 2870 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name, 2871 createIfNotFound); 2872 if (rid != 0) { 2873 TABLE_NOISY(printf("Pckg %s:%s/%s: 0x%08x\n", 2874 String8(package).string(), String8(type).string(), 2875 String8(name).string(), rid)); 2876 outValue->data = rid; 2877 return true; 2878 } 2879 } 2880 } 2881 2882 if (accessor != NULL) { 2883 accessor->reportError(accessorCookie, "No resource found that matches the given name"); 2884 } 2885 return false; 2886 } 2887 2888 // if we got to here, and localization is required and it's not a reference, 2889 // complain and bail. 2890 if (l10nReq == ResTable_map::L10N_SUGGESTED) { 2891 if (localizationSetting) { 2892 if (accessor != NULL) { 2893 accessor->reportError(accessorCookie, "This attribute must be localized."); 2894 } 2895 } 2896 } 2897 2898 if (*s == '#') { 2899 // It's a color! Convert to an integer of the form 0xaarrggbb. 2900 uint32_t color = 0; 2901 bool error = false; 2902 if (len == 4) { 2903 outValue->dataType = outValue->TYPE_INT_COLOR_RGB4; 2904 color |= 0xFF000000; 2905 color |= get_hex(s[1], &error) << 20; 2906 color |= get_hex(s[1], &error) << 16; 2907 color |= get_hex(s[2], &error) << 12; 2908 color |= get_hex(s[2], &error) << 8; 2909 color |= get_hex(s[3], &error) << 4; 2910 color |= get_hex(s[3], &error); 2911 } else if (len == 5) { 2912 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB4; 2913 color |= get_hex(s[1], &error) << 28; 2914 color |= get_hex(s[1], &error) << 24; 2915 color |= get_hex(s[2], &error) << 20; 2916 color |= get_hex(s[2], &error) << 16; 2917 color |= get_hex(s[3], &error) << 12; 2918 color |= get_hex(s[3], &error) << 8; 2919 color |= get_hex(s[4], &error) << 4; 2920 color |= get_hex(s[4], &error); 2921 } else if (len == 7) { 2922 outValue->dataType = outValue->TYPE_INT_COLOR_RGB8; 2923 color |= 0xFF000000; 2924 color |= get_hex(s[1], &error) << 20; 2925 color |= get_hex(s[2], &error) << 16; 2926 color |= get_hex(s[3], &error) << 12; 2927 color |= get_hex(s[4], &error) << 8; 2928 color |= get_hex(s[5], &error) << 4; 2929 color |= get_hex(s[6], &error); 2930 } else if (len == 9) { 2931 outValue->dataType = outValue->TYPE_INT_COLOR_ARGB8; 2932 color |= get_hex(s[1], &error) << 28; 2933 color |= get_hex(s[2], &error) << 24; 2934 color |= get_hex(s[3], &error) << 20; 2935 color |= get_hex(s[4], &error) << 16; 2936 color |= get_hex(s[5], &error) << 12; 2937 color |= get_hex(s[6], &error) << 8; 2938 color |= get_hex(s[7], &error) << 4; 2939 color |= get_hex(s[8], &error); 2940 } else { 2941 error = true; 2942 } 2943 if (!error) { 2944 if ((attrType&ResTable_map::TYPE_COLOR) == 0) { 2945 if (!canStringCoerce) { 2946 if (accessor != NULL) { 2947 accessor->reportError(accessorCookie, 2948 "Color types not allowed"); 2949 } 2950 return false; 2951 } 2952 } else { 2953 outValue->data = color; 2954 //printf("Color input=%s, output=0x%x\n", String8(s, len).string(), color); 2955 return true; 2956 } 2957 } else { 2958 if ((attrType&ResTable_map::TYPE_COLOR) != 0) { 2959 if (accessor != NULL) { 2960 accessor->reportError(accessorCookie, "Color value not valid --" 2961 " must be #rgb, #argb, #rrggbb, or #aarrggbb"); 2962 } 2963 #if 0 2964 fprintf(stderr, "%s: Color ID %s value %s is not valid\n", 2965 "Resource File", //(const char*)in->getPrintableSource(), 2966 String8(*curTag).string(), 2967 String8(s, len).string()); 2968 #endif 2969 return false; 2970 } 2971 } 2972 } 2973 2974 if (*s == '?') { 2975 outValue->dataType = outValue->TYPE_ATTRIBUTE; 2976 2977 // Note: we don't check attrType here because the reference can 2978 // be to any other type; we just need to count on the client making 2979 // sure the referenced type is correct. 2980 2981 //printf("Looking up attr: %s\n", String8(s, len).string()); 2982 2983 static const String16 attr16("attr"); 2984 String16 package, type, name; 2985 if (!expandResourceRef(s+1, len-1, &package, &type, &name, 2986 &attr16, defPackage, &errorMsg)) { 2987 if (accessor != NULL) { 2988 accessor->reportError(accessorCookie, errorMsg); 2989 } 2990 return false; 2991 } 2992 2993 //printf("Pkg: %s, Type: %s, Name: %s\n", 2994 // String8(package).string(), String8(type).string(), 2995 // String8(name).string()); 2996 uint32_t specFlags = 0; 2997 uint32_t rid = 2998 identifierForName(name.string(), name.size(), 2999 type.string(), type.size(), 3000 package.string(), package.size(), &specFlags); 3001 if (rid != 0) { 3002 if (enforcePrivate) { 3003 if ((specFlags&ResTable_typeSpec::SPEC_PUBLIC) == 0) { 3004 if (accessor != NULL) { 3005 accessor->reportError(accessorCookie, "Attribute is not public."); 3006 } 3007 return false; 3008 } 3009 } 3010 if (!accessor) { 3011 outValue->data = rid; 3012 return true; 3013 } 3014 rid = Res_MAKEID( 3015 accessor->getRemappedPackage(Res_GETPACKAGE(rid)), 3016 Res_GETTYPE(rid), Res_GETENTRY(rid)); 3017 //printf("Incl %s:%s/%s: 0x%08x\n", 3018 // String8(package).string(), String8(type).string(), 3019 // String8(name).string(), rid); 3020 outValue->data = rid; 3021 return true; 3022 } 3023 3024 if (accessor) { 3025 uint32_t rid = accessor->getCustomResource(package, type, name); 3026 if (rid != 0) { 3027 //printf("Mine %s:%s/%s: 0x%08x\n", 3028 // String8(package).string(), String8(type).string(), 3029 // String8(name).string(), rid); 3030 outValue->data = rid; 3031 return true; 3032 } 3033 } 3034 3035 if (accessor != NULL) { 3036 accessor->reportError(accessorCookie, "No resource found that matches the given name"); 3037 } 3038 return false; 3039 } 3040 3041 if (stringToInt(s, len, outValue)) { 3042 if ((attrType&ResTable_map::TYPE_INTEGER) == 0) { 3043 // If this type does not allow integers, but does allow floats, 3044 // fall through on this error case because the float type should 3045 // be able to accept any integer value. 3046 if (!canStringCoerce && (attrType&ResTable_map::TYPE_FLOAT) == 0) { 3047 if (accessor != NULL) { 3048 accessor->reportError(accessorCookie, "Integer types not allowed"); 3049 } 3050 return false; 3051 } 3052 } else { 3053 if (((int32_t)outValue->data) < ((int32_t)attrMin) 3054 || ((int32_t)outValue->data) > ((int32_t)attrMax)) { 3055 if (accessor != NULL) { 3056 accessor->reportError(accessorCookie, "Integer value out of range"); 3057 } 3058 return false; 3059 } 3060 return true; 3061 } 3062 } 3063 3064 if (stringToFloat(s, len, outValue)) { 3065 if (outValue->dataType == Res_value::TYPE_DIMENSION) { 3066 if ((attrType&ResTable_map::TYPE_DIMENSION) != 0) { 3067 return true; 3068 } 3069 if (!canStringCoerce) { 3070 if (accessor != NULL) { 3071 accessor->reportError(accessorCookie, "Dimension types not allowed"); 3072 } 3073 return false; 3074 } 3075 } else if (outValue->dataType == Res_value::TYPE_FRACTION) { 3076 if ((attrType&ResTable_map::TYPE_FRACTION) != 0) { 3077 return true; 3078 } 3079 if (!canStringCoerce) { 3080 if (accessor != NULL) { 3081 accessor->reportError(accessorCookie, "Fraction types not allowed"); 3082 } 3083 return false; 3084 } 3085 } else if ((attrType&ResTable_map::TYPE_FLOAT) == 0) { 3086 if (!canStringCoerce) { 3087 if (accessor != NULL) { 3088 accessor->reportError(accessorCookie, "Float types not allowed"); 3089 } 3090 return false; 3091 } 3092 } else { 3093 return true; 3094 } 3095 } 3096 3097 if (len == 4) { 3098 if ((s[0] == 't' || s[0] == 'T') && 3099 (s[1] == 'r' || s[1] == 'R') && 3100 (s[2] == 'u' || s[2] == 'U') && 3101 (s[3] == 'e' || s[3] == 'E')) { 3102 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { 3103 if (!canStringCoerce) { 3104 if (accessor != NULL) { 3105 accessor->reportError(accessorCookie, "Boolean types not allowed"); 3106 } 3107 return false; 3108 } 3109 } else { 3110 outValue->dataType = outValue->TYPE_INT_BOOLEAN; 3111 outValue->data = (uint32_t)-1; 3112 return true; 3113 } 3114 } 3115 } 3116 3117 if (len == 5) { 3118 if ((s[0] == 'f' || s[0] == 'F') && 3119 (s[1] == 'a' || s[1] == 'A') && 3120 (s[2] == 'l' || s[2] == 'L') && 3121 (s[3] == 's' || s[3] == 'S') && 3122 (s[4] == 'e' || s[4] == 'E')) { 3123 if ((attrType&ResTable_map::TYPE_BOOLEAN) == 0) { 3124 if (!canStringCoerce) { 3125 if (accessor != NULL) { 3126 accessor->reportError(accessorCookie, "Boolean types not allowed"); 3127 } 3128 return false; 3129 } 3130 } else { 3131 outValue->dataType = outValue->TYPE_INT_BOOLEAN; 3132 outValue->data = 0; 3133 return true; 3134 } 3135 } 3136 } 3137 3138 if ((attrType&ResTable_map::TYPE_ENUM) != 0) { 3139 const ssize_t p = getResourcePackageIndex(attrID); 3140 const bag_entry* bag; 3141 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; 3142 //printf("Got %d for enum\n", cnt); 3143 if (cnt >= 0) { 3144 resource_name rname; 3145 while (cnt > 0) { 3146 if (!Res_INTERNALID(bag->map.name.ident)) { 3147 //printf("Trying attr #%08x\n", bag->map.name.ident); 3148 if (getResourceName(bag->map.name.ident, &rname)) { 3149 #if 0 3150 printf("Matching %s against %s (0x%08x)\n", 3151 String8(s, len).string(), 3152 String8(rname.name, rname.nameLen).string(), 3153 bag->map.name.ident); 3154 #endif 3155 if (strzcmp16(s, len, rname.name, rname.nameLen) == 0) { 3156 outValue->dataType = bag->map.value.dataType; 3157 outValue->data = bag->map.value.data; 3158 unlockBag(bag); 3159 return true; 3160 } 3161 } 3162 3163 } 3164 bag++; 3165 cnt--; 3166 } 3167 unlockBag(bag); 3168 } 3169 3170 if (fromAccessor) { 3171 if (accessor->getAttributeEnum(attrID, s, len, outValue)) { 3172 return true; 3173 } 3174 } 3175 } 3176 3177 if ((attrType&ResTable_map::TYPE_FLAGS) != 0) { 3178 const ssize_t p = getResourcePackageIndex(attrID); 3179 const bag_entry* bag; 3180 ssize_t cnt = p >= 0 ? lockBag(attrID, &bag) : -1; 3181 //printf("Got %d for flags\n", cnt); 3182 if (cnt >= 0) { 3183 bool failed = false; 3184 resource_name rname; 3185 outValue->dataType = Res_value::TYPE_INT_HEX; 3186 outValue->data = 0; 3187 const char16_t* end = s + len; 3188 const char16_t* pos = s; 3189 while (pos < end && !failed) { 3190 const char16_t* start = pos; 3191 pos++; 3192 while (pos < end && *pos != '|') { 3193 pos++; 3194 } 3195 //printf("Looking for: %s\n", String8(start, pos-start).string()); 3196 const bag_entry* bagi = bag; 3197 ssize_t i; 3198 for (i=0; i<cnt; i++, bagi++) { 3199 if (!Res_INTERNALID(bagi->map.name.ident)) { 3200 //printf("Trying attr #%08x\n", bagi->map.name.ident); 3201 if (getResourceName(bagi->map.name.ident, &rname)) { 3202 #if 0 3203 printf("Matching %s against %s (0x%08x)\n", 3204 String8(start,pos-start).string(), 3205 String8(rname.name, rname.nameLen).string(), 3206 bagi->map.name.ident); 3207 #endif 3208 if (strzcmp16(start, pos-start, rname.name, rname.nameLen) == 0) { 3209 outValue->data |= bagi->map.value.data; 3210 break; 3211 } 3212 } 3213 } 3214 } 3215 if (i >= cnt) { 3216 // Didn't find this flag identifier. 3217 failed = true; 3218 } 3219 if (pos < end) { 3220 pos++; 3221 } 3222 } 3223 unlockBag(bag); 3224 if (!failed) { 3225 //printf("Final flag value: 0x%lx\n", outValue->data); 3226 return true; 3227 } 3228 } 3229 3230 3231 if (fromAccessor) { 3232 if (accessor->getAttributeFlags(attrID, s, len, outValue)) { 3233 //printf("Final flag value: 0x%lx\n", outValue->data); 3234 return true; 3235 } 3236 } 3237 } 3238 3239 if ((attrType&ResTable_map::TYPE_STRING) == 0) { 3240 if (accessor != NULL) { 3241 accessor->reportError(accessorCookie, "String types not allowed"); 3242 } 3243 return false; 3244 } 3245 3246 // Generic string handling... 3247 outValue->dataType = outValue->TYPE_STRING; 3248 if (outString) { 3249 bool failed = collectString(outString, s, len, preserveSpaces, &errorMsg); 3250 if (accessor != NULL) { 3251 accessor->reportError(accessorCookie, errorMsg); 3252 } 3253 return failed; 3254 } 3255 3256 return true; 3257} 3258 3259bool ResTable::collectString(String16* outString, 3260 const char16_t* s, size_t len, 3261 bool preserveSpaces, 3262 const char** outErrorMsg, 3263 bool append) 3264{ 3265 String16 tmp; 3266 3267 char quoted = 0; 3268 const char16_t* p = s; 3269 while (p < (s+len)) { 3270 while (p < (s+len)) { 3271 const char16_t c = *p; 3272 if (c == '\\') { 3273 break; 3274 } 3275 if (!preserveSpaces) { 3276 if (quoted == 0 && isspace16(c) 3277 && (c != ' ' || isspace16(*(p+1)))) { 3278 break; 3279 } 3280 if (c == '"' && (quoted == 0 || quoted == '"')) { 3281 break; 3282 } 3283 if (c == '\'' && (quoted == 0 || quoted == '\'')) { 3284 break; 3285 } 3286 } 3287 p++; 3288 } 3289 if (p < (s+len)) { 3290 if (p > s) { 3291 tmp.append(String16(s, p-s)); 3292 } 3293 if (!preserveSpaces && (*p == '"' || *p == '\'')) { 3294 if (quoted == 0) { 3295 quoted = *p; 3296 } else { 3297 quoted = 0; 3298 } 3299 p++; 3300 } else if (!preserveSpaces && isspace16(*p)) { 3301 // Space outside of a quote -- consume all spaces and 3302 // leave a single plain space char. 3303 tmp.append(String16(" ")); 3304 p++; 3305 while (p < (s+len) && isspace16(*p)) { 3306 p++; 3307 } 3308 } else if (*p == '\\') { 3309 p++; 3310 if (p < (s+len)) { 3311 switch (*p) { 3312 case 't': 3313 tmp.append(String16("\t")); 3314 break; 3315 case 'n': 3316 tmp.append(String16("\n")); 3317 break; 3318 case '#': 3319 tmp.append(String16("#")); 3320 break; 3321 case '@': 3322 tmp.append(String16("@")); 3323 break; 3324 case '?': 3325 tmp.append(String16("?")); 3326 break; 3327 case '"': 3328 tmp.append(String16("\"")); 3329 break; 3330 case '\'': 3331 tmp.append(String16("'")); 3332 break; 3333 case '\\': 3334 tmp.append(String16("\\")); 3335 break; 3336 case 'u': 3337 { 3338 char16_t chr = 0; 3339 int i = 0; 3340 while (i < 4 && p[1] != 0) { 3341 p++; 3342 i++; 3343 int c; 3344 if (*p >= '0' && *p <= '9') { 3345 c = *p - '0'; 3346 } else if (*p >= 'a' && *p <= 'f') { 3347 c = *p - 'a' + 10; 3348 } else if (*p >= 'A' && *p <= 'F') { 3349 c = *p - 'A' + 10; 3350 } else { 3351 if (outErrorMsg) { 3352 *outErrorMsg = "Bad character in \\u unicode escape sequence"; 3353 } 3354 return false; 3355 } 3356 chr = (chr<<4) | c; 3357 } 3358 tmp.append(String16(&chr, 1)); 3359 } break; 3360 default: 3361 // ignore unknown escape chars. 3362 break; 3363 } 3364 p++; 3365 } 3366 } 3367 len -= (p-s); 3368 s = p; 3369 } 3370 } 3371 3372 if (tmp.size() != 0) { 3373 if (len > 0) { 3374 tmp.append(String16(s, len)); 3375 } 3376 if (append) { 3377 outString->append(tmp); 3378 } else { 3379 outString->setTo(tmp); 3380 } 3381 } else { 3382 if (append) { 3383 outString->append(String16(s, len)); 3384 } else { 3385 outString->setTo(s, len); 3386 } 3387 } 3388 3389 return true; 3390} 3391 3392size_t ResTable::getBasePackageCount() const 3393{ 3394 if (mError != NO_ERROR) { 3395 return 0; 3396 } 3397 return mPackageGroups.size(); 3398} 3399 3400const char16_t* ResTable::getBasePackageName(size_t idx) const 3401{ 3402 if (mError != NO_ERROR) { 3403 return 0; 3404 } 3405 LOG_FATAL_IF(idx >= mPackageGroups.size(), 3406 "Requested package index %d past package count %d", 3407 (int)idx, (int)mPackageGroups.size()); 3408 return mPackageGroups[idx]->name.string(); 3409} 3410 3411uint32_t ResTable::getBasePackageId(size_t idx) const 3412{ 3413 if (mError != NO_ERROR) { 3414 return 0; 3415 } 3416 LOG_FATAL_IF(idx >= mPackageGroups.size(), 3417 "Requested package index %d past package count %d", 3418 (int)idx, (int)mPackageGroups.size()); 3419 return mPackageGroups[idx]->id; 3420} 3421 3422size_t ResTable::getTableCount() const 3423{ 3424 return mHeaders.size(); 3425} 3426 3427const ResStringPool* ResTable::getTableStringBlock(size_t index) const 3428{ 3429 return &mHeaders[index]->values; 3430} 3431 3432void* ResTable::getTableCookie(size_t index) const 3433{ 3434 return mHeaders[index]->cookie; 3435} 3436 3437void ResTable::getConfigurations(Vector<ResTable_config>* configs) const 3438{ 3439 const size_t I = mPackageGroups.size(); 3440 for (size_t i=0; i<I; i++) { 3441 const PackageGroup* packageGroup = mPackageGroups[i]; 3442 const size_t J = packageGroup->packages.size(); 3443 for (size_t j=0; j<J; j++) { 3444 const Package* package = packageGroup->packages[j]; 3445 const size_t K = package->types.size(); 3446 for (size_t k=0; k<K; k++) { 3447 const Type* type = package->types[k]; 3448 if (type == NULL) continue; 3449 const size_t L = type->configs.size(); 3450 for (size_t l=0; l<L; l++) { 3451 const ResTable_type* config = type->configs[l]; 3452 const ResTable_config* cfg = &config->config; 3453 // only insert unique 3454 const size_t M = configs->size(); 3455 size_t m; 3456 for (m=0; m<M; m++) { 3457 if (0 == (*configs)[m].compare(*cfg)) { 3458 break; 3459 } 3460 } 3461 // if we didn't find it 3462 if (m == M) { 3463 configs->add(*cfg); 3464 } 3465 } 3466 } 3467 } 3468 } 3469} 3470 3471void ResTable::getLocales(Vector<String8>* locales) const 3472{ 3473 Vector<ResTable_config> configs; 3474 LOGD("calling getConfigurations"); 3475 getConfigurations(&configs); 3476 LOGD("called getConfigurations size=%d", (int)configs.size()); 3477 const size_t I = configs.size(); 3478 for (size_t i=0; i<I; i++) { 3479 char locale[6]; 3480 configs[i].getLocale(locale); 3481 const size_t J = locales->size(); 3482 size_t j; 3483 for (j=0; j<J; j++) { 3484 if (0 == strcmp(locale, (*locales)[j].string())) { 3485 break; 3486 } 3487 } 3488 if (j == J) { 3489 locales->add(String8(locale)); 3490 } 3491 } 3492} 3493 3494ssize_t ResTable::getEntry( 3495 const Package* package, int typeIndex, int entryIndex, 3496 const ResTable_config* config, 3497 const ResTable_type** outType, const ResTable_entry** outEntry, 3498 const Type** outTypeClass) const 3499{ 3500 LOGV("Getting entry from package %p\n", package); 3501 const ResTable_package* const pkg = package->package; 3502 3503 const Type* allTypes = package->getType(typeIndex); 3504 LOGV("allTypes=%p\n", allTypes); 3505 if (allTypes == NULL) { 3506 LOGV("Skipping entry type index 0x%02x because type is NULL!\n", typeIndex); 3507 return 0; 3508 } 3509 3510 if ((size_t)entryIndex >= allTypes->entryCount) { 3511 LOGW("getEntry failing because entryIndex %d is beyond type entryCount %d", 3512 entryIndex, (int)allTypes->entryCount); 3513 return BAD_TYPE; 3514 } 3515 3516 const ResTable_type* type = NULL; 3517 uint32_t offset = ResTable_type::NO_ENTRY; 3518 ResTable_config bestConfig; 3519 memset(&bestConfig, 0, sizeof(bestConfig)); // make the compiler shut up 3520 3521 const size_t NT = allTypes->configs.size(); 3522 for (size_t i=0; i<NT; i++) { 3523 const ResTable_type* const thisType = allTypes->configs[i]; 3524 if (thisType == NULL) continue; 3525 3526 ResTable_config thisConfig; 3527 thisConfig.copyFromDtoH(thisType->config); 3528 3529 TABLE_GETENTRY(LOGI("Match entry 0x%x in type 0x%x (sz 0x%x): imsi:%d/%d=%d/%d lang:%c%c=%c%c cnt:%c%c=%c%c " 3530 "orien:%d=%d touch:%d=%d density:%d=%d key:%d=%d inp:%d=%d nav:%d=%d w:%d=%d h:%d=%d\n", 3531 entryIndex, typeIndex+1, dtohl(thisType->config.size), 3532 thisConfig.mcc, thisConfig.mnc, 3533 config ? config->mcc : 0, config ? config->mnc : 0, 3534 thisConfig.language[0] ? thisConfig.language[0] : '-', 3535 thisConfig.language[1] ? thisConfig.language[1] : '-', 3536 config && config->language[0] ? config->language[0] : '-', 3537 config && config->language[1] ? config->language[1] : '-', 3538 thisConfig.country[0] ? thisConfig.country[0] : '-', 3539 thisConfig.country[1] ? thisConfig.country[1] : '-', 3540 config && config->country[0] ? config->country[0] : '-', 3541 config && config->country[1] ? config->country[1] : '-', 3542 thisConfig.orientation, 3543 config ? config->orientation : 0, 3544 thisConfig.touchscreen, 3545 config ? config->touchscreen : 0, 3546 thisConfig.density, 3547 config ? config->density : 0, 3548 thisConfig.keyboard, 3549 config ? config->keyboard : 0, 3550 thisConfig.inputFlags, 3551 config ? config->inputFlags : 0, 3552 thisConfig.navigation, 3553 config ? config->navigation : 0, 3554 thisConfig.screenWidth, 3555 config ? config->screenWidth : 0, 3556 thisConfig.screenHeight, 3557 config ? config->screenHeight : 0)); 3558 3559 // Check to make sure this one is valid for the current parameters. 3560 if (config && !thisConfig.match(*config)) { 3561 TABLE_GETENTRY(LOGI("Does not match config!\n")); 3562 continue; 3563 } 3564 3565 // Check if there is the desired entry in this type. 3566 3567 const uint8_t* const end = ((const uint8_t*)thisType) 3568 + dtohl(thisType->header.size); 3569 const uint32_t* const eindex = (const uint32_t*) 3570 (((const uint8_t*)thisType) + dtohs(thisType->header.headerSize)); 3571 3572 uint32_t thisOffset = dtohl(eindex[entryIndex]); 3573 if (thisOffset == ResTable_type::NO_ENTRY) { 3574 TABLE_GETENTRY(LOGI("Skipping because it is not defined!\n")); 3575 continue; 3576 } 3577 3578 if (type != NULL) { 3579 // Check if this one is less specific than the last found. If so, 3580 // we will skip it. We check starting with things we most care 3581 // about to those we least care about. 3582 if (!thisConfig.isBetterThan(bestConfig, config)) { 3583 TABLE_GETENTRY(LOGI("This config is worse than last!\n")); 3584 continue; 3585 } 3586 } 3587 3588 type = thisType; 3589 offset = thisOffset; 3590 bestConfig = thisConfig; 3591 TABLE_GETENTRY(LOGI("Best entry so far -- using it!\n")); 3592 if (!config) break; 3593 } 3594 3595 if (type == NULL) { 3596 TABLE_GETENTRY(LOGI("No value found for requested entry!\n")); 3597 return BAD_INDEX; 3598 } 3599 3600 offset += dtohl(type->entriesStart); 3601 TABLE_NOISY(aout << "Looking in resource table " << package->header->header 3602 << ", typeOff=" 3603 << (void*)(((const char*)type)-((const char*)package->header->header)) 3604 << ", offset=" << (void*)offset << endl); 3605 3606 if (offset > (dtohl(type->header.size)-sizeof(ResTable_entry))) { 3607 LOGW("ResTable_entry at 0x%x is beyond type chunk data 0x%x", 3608 offset, dtohl(type->header.size)); 3609 return BAD_TYPE; 3610 } 3611 if ((offset&0x3) != 0) { 3612 LOGW("ResTable_entry at 0x%x is not on an integer boundary", 3613 offset); 3614 return BAD_TYPE; 3615 } 3616 3617 const ResTable_entry* const entry = (const ResTable_entry*) 3618 (((const uint8_t*)type) + offset); 3619 if (dtohs(entry->size) < sizeof(*entry)) { 3620 LOGW("ResTable_entry size 0x%x is too small", dtohs(entry->size)); 3621 return BAD_TYPE; 3622 } 3623 3624 *outType = type; 3625 *outEntry = entry; 3626 if (outTypeClass != NULL) { 3627 *outTypeClass = allTypes; 3628 } 3629 return offset + dtohs(entry->size); 3630} 3631 3632status_t ResTable::parsePackage(const ResTable_package* const pkg, 3633 const Header* const header) 3634{ 3635 const uint8_t* base = (const uint8_t*)pkg; 3636 status_t err = validate_chunk(&pkg->header, sizeof(*pkg), 3637 header->dataEnd, "ResTable_package"); 3638 if (err != NO_ERROR) { 3639 return (mError=err); 3640 } 3641 3642 const size_t pkgSize = dtohl(pkg->header.size); 3643 3644 if (dtohl(pkg->typeStrings) >= pkgSize) { 3645 LOGW("ResTable_package type strings at %p are past chunk size %p.", 3646 (void*)dtohl(pkg->typeStrings), (void*)pkgSize); 3647 return (mError=BAD_TYPE); 3648 } 3649 if ((dtohl(pkg->typeStrings)&0x3) != 0) { 3650 LOGW("ResTable_package type strings at %p is not on an integer boundary.", 3651 (void*)dtohl(pkg->typeStrings)); 3652 return (mError=BAD_TYPE); 3653 } 3654 if (dtohl(pkg->keyStrings) >= pkgSize) { 3655 LOGW("ResTable_package key strings at %p are past chunk size %p.", 3656 (void*)dtohl(pkg->keyStrings), (void*)pkgSize); 3657 return (mError=BAD_TYPE); 3658 } 3659 if ((dtohl(pkg->keyStrings)&0x3) != 0) { 3660 LOGW("ResTable_package key strings at %p is not on an integer boundary.", 3661 (void*)dtohl(pkg->keyStrings)); 3662 return (mError=BAD_TYPE); 3663 } 3664 3665 Package* package = NULL; 3666 PackageGroup* group = NULL; 3667 uint32_t id = dtohl(pkg->id); 3668 if (id != 0 && id < 256) { 3669 3670 package = new Package(this, header, pkg); 3671 if (package == NULL) { 3672 return (mError=NO_MEMORY); 3673 } 3674 3675 size_t idx = mPackageMap[id]; 3676 if (idx == 0) { 3677 idx = mPackageGroups.size()+1; 3678 3679 char16_t tmpName[sizeof(pkg->name)/sizeof(char16_t)]; 3680 strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(char16_t)); 3681 group = new PackageGroup(this, String16(tmpName), id); 3682 if (group == NULL) { 3683 delete package; 3684 return (mError=NO_MEMORY); 3685 } 3686 3687 err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings), 3688 header->dataEnd-(base+dtohl(pkg->typeStrings))); 3689 if (err != NO_ERROR) { 3690 delete group; 3691 delete package; 3692 return (mError=err); 3693 } 3694 err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings), 3695 header->dataEnd-(base+dtohl(pkg->keyStrings))); 3696 if (err != NO_ERROR) { 3697 delete group; 3698 delete package; 3699 return (mError=err); 3700 } 3701 3702 //printf("Adding new package id %d at index %d\n", id, idx); 3703 err = mPackageGroups.add(group); 3704 if (err < NO_ERROR) { 3705 return (mError=err); 3706 } 3707 group->basePackage = package; 3708 3709 mPackageMap[id] = (uint8_t)idx; 3710 } else { 3711 group = mPackageGroups.itemAt(idx-1); 3712 if (group == NULL) { 3713 return (mError=UNKNOWN_ERROR); 3714 } 3715 } 3716 err = group->packages.add(package); 3717 if (err < NO_ERROR) { 3718 return (mError=err); 3719 } 3720 } else { 3721 LOG_ALWAYS_FATAL("Skins not supported!"); 3722 return NO_ERROR; 3723 } 3724 3725 3726 // Iterate through all chunks. 3727 size_t curPackage = 0; 3728 3729 const ResChunk_header* chunk = 3730 (const ResChunk_header*)(((const uint8_t*)pkg) 3731 + dtohs(pkg->header.headerSize)); 3732 const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size); 3733 while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) && 3734 ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) { 3735 TABLE_NOISY(LOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n", 3736 dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size), 3737 (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)))); 3738 const size_t csize = dtohl(chunk->size); 3739 const uint16_t ctype = dtohs(chunk->type); 3740 if (ctype == RES_TABLE_TYPE_SPEC_TYPE) { 3741 const ResTable_typeSpec* typeSpec = (const ResTable_typeSpec*)(chunk); 3742 err = validate_chunk(&typeSpec->header, sizeof(*typeSpec), 3743 endPos, "ResTable_typeSpec"); 3744 if (err != NO_ERROR) { 3745 return (mError=err); 3746 } 3747 3748 const size_t typeSpecSize = dtohl(typeSpec->header.size); 3749 3750 LOAD_TABLE_NOISY(printf("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n", 3751 (void*)(base-(const uint8_t*)chunk), 3752 dtohs(typeSpec->header.type), 3753 dtohs(typeSpec->header.headerSize), 3754 (void*)typeSize)); 3755 // look for block overrun or int overflow when multiplying by 4 3756 if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t)) 3757 || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*dtohl(typeSpec->entryCount)) 3758 > typeSpecSize)) { 3759 LOGW("ResTable_typeSpec entry index to %p extends beyond chunk end %p.", 3760 (void*)(dtohs(typeSpec->header.headerSize) 3761 +(sizeof(uint32_t)*dtohl(typeSpec->entryCount))), 3762 (void*)typeSpecSize); 3763 return (mError=BAD_TYPE); 3764 } 3765 3766 if (typeSpec->id == 0) { 3767 LOGW("ResTable_type has an id of 0."); 3768 return (mError=BAD_TYPE); 3769 } 3770 3771 while (package->types.size() < typeSpec->id) { 3772 package->types.add(NULL); 3773 } 3774 Type* t = package->types[typeSpec->id-1]; 3775 if (t == NULL) { 3776 t = new Type(header, package, dtohl(typeSpec->entryCount)); 3777 package->types.editItemAt(typeSpec->id-1) = t; 3778 } else if (dtohl(typeSpec->entryCount) != t->entryCount) { 3779 LOGW("ResTable_typeSpec entry count inconsistent: given %d, previously %d", 3780 (int)dtohl(typeSpec->entryCount), (int)t->entryCount); 3781 return (mError=BAD_TYPE); 3782 } 3783 t->typeSpecFlags = (const uint32_t*)( 3784 ((const uint8_t*)typeSpec) + dtohs(typeSpec->header.headerSize)); 3785 t->typeSpec = typeSpec; 3786 3787 } else if (ctype == RES_TABLE_TYPE_TYPE) { 3788 const ResTable_type* type = (const ResTable_type*)(chunk); 3789 err = validate_chunk(&type->header, sizeof(*type)-sizeof(ResTable_config)+4, 3790 endPos, "ResTable_type"); 3791 if (err != NO_ERROR) { 3792 return (mError=err); 3793 } 3794 3795 const size_t typeSize = dtohl(type->header.size); 3796 3797 LOAD_TABLE_NOISY(printf("Type off %p: type=0x%x, headerSize=0x%x, size=%p\n", 3798 (void*)(base-(const uint8_t*)chunk), 3799 dtohs(type->header.type), 3800 dtohs(type->header.headerSize), 3801 (void*)typeSize)); 3802 if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*dtohl(type->entryCount)) 3803 > typeSize) { 3804 LOGW("ResTable_type entry index to %p extends beyond chunk end %p.", 3805 (void*)(dtohs(type->header.headerSize) 3806 +(sizeof(uint32_t)*dtohl(type->entryCount))), 3807 (void*)typeSize); 3808 return (mError=BAD_TYPE); 3809 } 3810 if (dtohl(type->entryCount) != 0 3811 && dtohl(type->entriesStart) > (typeSize-sizeof(ResTable_entry))) { 3812 LOGW("ResTable_type entriesStart at %p extends beyond chunk end %p.", 3813 (void*)dtohl(type->entriesStart), (void*)typeSize); 3814 return (mError=BAD_TYPE); 3815 } 3816 if (type->id == 0) { 3817 LOGW("ResTable_type has an id of 0."); 3818 return (mError=BAD_TYPE); 3819 } 3820 3821 while (package->types.size() < type->id) { 3822 package->types.add(NULL); 3823 } 3824 Type* t = package->types[type->id-1]; 3825 if (t == NULL) { 3826 t = new Type(header, package, dtohl(type->entryCount)); 3827 package->types.editItemAt(type->id-1) = t; 3828 } else if (dtohl(type->entryCount) != t->entryCount) { 3829 LOGW("ResTable_type entry count inconsistent: given %d, previously %d", 3830 (int)dtohl(type->entryCount), (int)t->entryCount); 3831 return (mError=BAD_TYPE); 3832 } 3833 3834 TABLE_GETENTRY( 3835 ResTable_config thisConfig; 3836 thisConfig.copyFromDtoH(type->config); 3837 LOGI("Adding config to type %d: imsi:%d/%d lang:%c%c cnt:%c%c " 3838 "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", 3839 type->id, 3840 thisConfig.mcc, thisConfig.mnc, 3841 thisConfig.language[0] ? thisConfig.language[0] : '-', 3842 thisConfig.language[1] ? thisConfig.language[1] : '-', 3843 thisConfig.country[0] ? thisConfig.country[0] : '-', 3844 thisConfig.country[1] ? thisConfig.country[1] : '-', 3845 thisConfig.orientation, 3846 thisConfig.touchscreen, 3847 thisConfig.density, 3848 thisConfig.keyboard, 3849 thisConfig.inputFlags, 3850 thisConfig.navigation, 3851 thisConfig.screenWidth, 3852 thisConfig.screenHeight)); 3853 t->configs.add(type); 3854 } else { 3855 status_t err = validate_chunk(chunk, sizeof(ResChunk_header), 3856 endPos, "ResTable_package:unknown"); 3857 if (err != NO_ERROR) { 3858 return (mError=err); 3859 } 3860 } 3861 chunk = (const ResChunk_header*) 3862 (((const uint8_t*)chunk) + csize); 3863 } 3864 3865 if (group->typeCount == 0) { 3866 group->typeCount = package->types.size(); 3867 } 3868 3869 return NO_ERROR; 3870} 3871 3872#ifndef HAVE_ANDROID_OS 3873#define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string()) 3874 3875#define CHAR16_ARRAY_EQ(constant, var, len) \ 3876 ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len)))) 3877 3878void print_complex(uint32_t complex, bool isFraction) 3879{ 3880 const float MANTISSA_MULT = 3881 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT); 3882 const float RADIX_MULTS[] = { 3883 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT, 3884 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT 3885 }; 3886 3887 float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK 3888 <<Res_value::COMPLEX_MANTISSA_SHIFT)) 3889 * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT) 3890 & Res_value::COMPLEX_RADIX_MASK]; 3891 printf("%f", value); 3892 3893 if (!isFraction) { 3894 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { 3895 case Res_value::COMPLEX_UNIT_PX: printf("px"); break; 3896 case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break; 3897 case Res_value::COMPLEX_UNIT_SP: printf("sp"); break; 3898 case Res_value::COMPLEX_UNIT_PT: printf("pt"); break; 3899 case Res_value::COMPLEX_UNIT_IN: printf("in"); break; 3900 case Res_value::COMPLEX_UNIT_MM: printf("mm"); break; 3901 default: printf(" (unknown unit)"); break; 3902 } 3903 } else { 3904 switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) { 3905 case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break; 3906 case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break; 3907 default: printf(" (unknown unit)"); break; 3908 } 3909 } 3910} 3911 3912void ResTable::print_value(const Package* pkg, const Res_value& value) const 3913{ 3914 if (value.dataType == Res_value::TYPE_NULL) { 3915 printf("(null)\n"); 3916 } else if (value.dataType == Res_value::TYPE_REFERENCE) { 3917 printf("(reference) 0x%08x\n", value.data); 3918 } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) { 3919 printf("(attribute) 0x%08x\n", value.data); 3920 } else if (value.dataType == Res_value::TYPE_STRING) { 3921 size_t len; 3922 const char16_t* str = pkg->header->values.stringAt( 3923 value.data, &len); 3924 if (str == NULL) { 3925 printf("(string) null\n"); 3926 } else { 3927 printf("(string) \"%s\"\n", 3928 String8(str, len).string()); 3929 } 3930 } else if (value.dataType == Res_value::TYPE_FLOAT) { 3931 printf("(float) %g\n", *(const float*)&value.data); 3932 } else if (value.dataType == Res_value::TYPE_DIMENSION) { 3933 printf("(dimension) "); 3934 print_complex(value.data, false); 3935 printf("\n"); 3936 } else if (value.dataType == Res_value::TYPE_FRACTION) { 3937 printf("(fraction) "); 3938 print_complex(value.data, true); 3939 printf("\n"); 3940 } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT 3941 || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) { 3942 printf("(color) #%08x\n", value.data); 3943 } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) { 3944 printf("(boolean) %s\n", value.data ? "true" : "false"); 3945 } else if (value.dataType >= Res_value::TYPE_FIRST_INT 3946 || value.dataType <= Res_value::TYPE_LAST_INT) { 3947 printf("(int) 0x%08x or %d\n", value.data, value.data); 3948 } else { 3949 printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n", 3950 (int)value.dataType, (int)value.data, 3951 (int)value.size, (int)value.res0); 3952 } 3953} 3954 3955void ResTable::print(bool inclValues) const 3956{ 3957 if (mError != 0) { 3958 printf("mError=0x%x (%s)\n", mError, strerror(mError)); 3959 } 3960#if 0 3961 printf("mParams=%c%c-%c%c,\n", 3962 mParams.language[0], mParams.language[1], 3963 mParams.country[0], mParams.country[1]); 3964#endif 3965 size_t pgCount = mPackageGroups.size(); 3966 printf("Package Groups (%d)\n", (int)pgCount); 3967 for (size_t pgIndex=0; pgIndex<pgCount; pgIndex++) { 3968 const PackageGroup* pg = mPackageGroups[pgIndex]; 3969 printf("Package Group %d id=%d packageCount=%d name=%s\n", 3970 (int)pgIndex, pg->id, (int)pg->packages.size(), 3971 String8(pg->name).string()); 3972 3973 size_t pkgCount = pg->packages.size(); 3974 for (size_t pkgIndex=0; pkgIndex<pkgCount; pkgIndex++) { 3975 const Package* pkg = pg->packages[pkgIndex]; 3976 size_t typeCount = pkg->types.size(); 3977 printf(" Package %d id=%d name=%s typeCount=%d\n", (int)pkgIndex, 3978 pkg->package->id, String8(String16(pkg->package->name)).string(), 3979 (int)typeCount); 3980 for (size_t typeIndex=0; typeIndex<typeCount; typeIndex++) { 3981 const Type* typeConfigs = pkg->getType(typeIndex); 3982 if (typeConfigs == NULL) { 3983 printf(" type %d NULL\n", (int)typeIndex); 3984 continue; 3985 } 3986 const size_t NTC = typeConfigs->configs.size(); 3987 printf(" type %d configCount=%d entryCount=%d\n", 3988 (int)typeIndex, (int)NTC, (int)typeConfigs->entryCount); 3989 if (typeConfigs->typeSpecFlags != NULL) { 3990 for (size_t entryIndex=0; entryIndex<typeConfigs->entryCount; entryIndex++) { 3991 uint32_t resID = (0xff000000 & ((pkg->package->id)<<24)) 3992 | (0x00ff0000 & ((typeIndex+1)<<16)) 3993 | (0x0000ffff & (entryIndex)); 3994 resource_name resName; 3995 this->getResourceName(resID, &resName); 3996 printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n", 3997 resID, 3998 CHAR16_TO_CSTR(resName.package, resName.packageLen), 3999 CHAR16_TO_CSTR(resName.type, resName.typeLen), 4000 CHAR16_TO_CSTR(resName.name, resName.nameLen), 4001 dtohl(typeConfigs->typeSpecFlags[entryIndex])); 4002 } 4003 } 4004 for (size_t configIndex=0; configIndex<NTC; configIndex++) { 4005 const ResTable_type* type = typeConfigs->configs[configIndex]; 4006 if ((((uint64_t)type)&0x3) != 0) { 4007 printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type); 4008 continue; 4009 } 4010 char density[16]; 4011 uint16_t dval = dtohs(type->config.density); 4012 if (dval == ResTable_config::DENSITY_DEFAULT) { 4013 strcpy(density, "def"); 4014 } else if (dval == ResTable_config::DENSITY_NONE) { 4015 strcpy(density, "no"); 4016 } else { 4017 sprintf(density, "%d", (int)dval); 4018 } 4019 printf(" config %d lang=%c%c cnt=%c%c orien=%d touch=%d density=%s key=%d infl=%d nav=%d w=%d h=%d sz=%d lng=%d\n", 4020 (int)configIndex, 4021 type->config.language[0] ? type->config.language[0] : '-', 4022 type->config.language[1] ? type->config.language[1] : '-', 4023 type->config.country[0] ? type->config.country[0] : '-', 4024 type->config.country[1] ? type->config.country[1] : '-', 4025 type->config.orientation, 4026 type->config.touchscreen, 4027 density, 4028 type->config.keyboard, 4029 type->config.inputFlags, 4030 type->config.navigation, 4031 dtohs(type->config.screenWidth), 4032 dtohs(type->config.screenHeight), 4033 type->config.screenLayout&ResTable_config::MASK_SCREENSIZE, 4034 type->config.screenLayout&ResTable_config::MASK_SCREENLONG); 4035 size_t entryCount = dtohl(type->entryCount); 4036 uint32_t entriesStart = dtohl(type->entriesStart); 4037 if ((entriesStart&0x3) != 0) { 4038 printf(" NON-INTEGER ResTable_type entriesStart OFFSET: %p\n", (void*)entriesStart); 4039 continue; 4040 } 4041 uint32_t typeSize = dtohl(type->header.size); 4042 if ((typeSize&0x3) != 0) { 4043 printf(" NON-INTEGER ResTable_type header.size: %p\n", (void*)typeSize); 4044 continue; 4045 } 4046 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) { 4047 4048 const uint8_t* const end = ((const uint8_t*)type) 4049 + dtohl(type->header.size); 4050 const uint32_t* const eindex = (const uint32_t*) 4051 (((const uint8_t*)type) + dtohs(type->header.headerSize)); 4052 4053 uint32_t thisOffset = dtohl(eindex[entryIndex]); 4054 if (thisOffset == ResTable_type::NO_ENTRY) { 4055 continue; 4056 } 4057 4058 uint32_t resID = (0xff000000 & ((pkg->package->id)<<24)) 4059 | (0x00ff0000 & ((typeIndex+1)<<16)) 4060 | (0x0000ffff & (entryIndex)); 4061 resource_name resName; 4062 this->getResourceName(resID, &resName); 4063 printf(" resource 0x%08x %s:%s/%s: ", resID, 4064 CHAR16_TO_CSTR(resName.package, resName.packageLen), 4065 CHAR16_TO_CSTR(resName.type, resName.typeLen), 4066 CHAR16_TO_CSTR(resName.name, resName.nameLen)); 4067 if ((thisOffset&0x3) != 0) { 4068 printf("NON-INTEGER OFFSET: %p\n", (void*)thisOffset); 4069 continue; 4070 } 4071 if ((thisOffset+sizeof(ResTable_entry)) > typeSize) { 4072 printf("OFFSET OUT OF BOUNDS: %p+%p (size is %p)\n", 4073 (void*)entriesStart, (void*)thisOffset, 4074 (void*)typeSize); 4075 continue; 4076 } 4077 4078 const ResTable_entry* ent = (const ResTable_entry*) 4079 (((const uint8_t*)type) + entriesStart + thisOffset); 4080 if (((entriesStart + thisOffset)&0x3) != 0) { 4081 printf("NON-INTEGER ResTable_entry OFFSET: %p\n", 4082 (void*)(entriesStart + thisOffset)); 4083 continue; 4084 } 4085 4086 uint16_t esize = dtohs(ent->size); 4087 if ((esize&0x3) != 0) { 4088 printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void*)esize); 4089 continue; 4090 } 4091 if ((thisOffset+esize) > typeSize) { 4092 printf("ResTable_entry OUT OF BOUNDS: %p+%p+%p (size is %p)\n", 4093 (void*)entriesStart, (void*)thisOffset, 4094 (void*)esize, (void*)typeSize); 4095 continue; 4096 } 4097 4098 const Res_value* valuePtr = NULL; 4099 const ResTable_map_entry* bagPtr = NULL; 4100 Res_value value; 4101 if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) { 4102 printf("<bag>"); 4103 bagPtr = (const ResTable_map_entry*)ent; 4104 } else { 4105 valuePtr = (const Res_value*) 4106 (((const uint8_t*)ent) + esize); 4107 value.copyFrom_dtoh(*valuePtr); 4108 printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)", 4109 (int)value.dataType, (int)value.data, 4110 (int)value.size, (int)value.res0); 4111 } 4112 4113 if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) { 4114 printf(" (PUBLIC)"); 4115 } 4116 printf("\n"); 4117 4118 if (inclValues) { 4119 if (valuePtr != NULL) { 4120 printf(" "); 4121 print_value(pkg, value); 4122 } else if (bagPtr != NULL) { 4123 const int N = dtohl(bagPtr->count); 4124 const ResTable_map* mapPtr = (const ResTable_map*) 4125 (((const uint8_t*)ent) + esize); 4126 printf(" Parent=0x%08x, Count=%d\n", 4127 dtohl(bagPtr->parent.ident), N); 4128 for (int i=0; i<N; i++) { 4129 printf(" #%i (Key=0x%08x): ", 4130 i, dtohl(mapPtr->name.ident)); 4131 value.copyFrom_dtoh(mapPtr->value); 4132 print_value(pkg, value); 4133 const size_t size = dtohs(mapPtr->value.size); 4134 mapPtr = (ResTable_map*)(((const uint8_t*)mapPtr) 4135 + size + sizeof(*mapPtr)-sizeof(mapPtr->value)); 4136 } 4137 } 4138 } 4139 } 4140 } 4141 } 4142 } 4143 } 4144} 4145 4146#endif // HAVE_ANDROID_OS 4147 4148} // namespace android 4149