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