19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "ResourceType"
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/ResourceTypes.h>
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Atomic.h>
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/ByteOrder.h>
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Debug.h>
24b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <utils/Log.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String16.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/TextOutput.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdlib.h>
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <string.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <memory.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ctype.h>
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdint.h>
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifndef INT32_MAX
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INT32_MAX ((int32_t)(2147483647))
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define POOL_NOISY(x) //x
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define XML_NOISY(x) //x
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define TABLE_NOISY(x) //x
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define TABLE_GETENTRY(x) //x
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define TABLE_SUPER_NOISY(x) //x
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOAD_TABLE_NOISY(x) //x
45b8d81679553ee33f6ae5281310abf2effca4ffcdDianne Hackborn#define TABLE_THEME(x) //x
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef HAVE_WINSOCK
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#undef  nhtol
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#undef  htonl
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef HAVE_LITTLE_ENDIAN
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define ntohl(x)    ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define htonl(x)    ntohl(x)
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define ntohs(x)    ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define htons(x)    ntohs(x)
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define ntohl(x)    (x)
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define htonl(x)    (x)
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define ntohs(x)    (x)
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define htons(x)    (x)
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad#define IDMAP_MAGIC         0x706d6469
6757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad// size measured in sizeof(uint32_t)
6857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad#define IDMAP_HEADER_SIZE (ResTable::IDMAP_HEADER_SIZE_BYTES / sizeof(uint32_t))
6957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void printToLogFunc(void* cookie, const char* txt)
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("%s", txt);
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Standard C isspace() is only required to look at the low byte of its input, so
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// produces incorrect results for UTF-16 characters.  For safety's sake, assume that
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// any high-byte UTF-16 code point is not whitespace.
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectinline int isspace16(char16_t c) {
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return (c < 0x0080 && isspace(c));
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// range checked; guaranteed to NUL-terminate within the stated number of available slots
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// NOTE: if this truncates the dst string due to running out of space, no attempt is
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// made to avoid splitting surrogate pairs.
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void strcpy16_dtoh(uint16_t* dst, const uint16_t* src, size_t avail)
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint16_t* last = dst + avail - 1;
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (*src && (dst < last)) {
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char16_t s = dtohs(*src);
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = s;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        src++;
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    *dst = 0;
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic status_t validate_chunk(const ResChunk_header* chunk,
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               size_t minSize,
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               const uint8_t* dataEnd,
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               const char* name)
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t headerSize = dtohs(chunk->headerSize);
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint32_t size = dtohl(chunk->size);
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (headerSize >= minSize) {
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (headerSize <= size) {
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (((headerSize|size)&0x3) == 0) {
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if ((ssize_t)size <= (dataEnd-((const uint8_t*)chunk))) {
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return NO_ERROR;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1108564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("%s data size %p extends beyond resource end %p.",
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                     name, (void*)size,
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                     (void*)(dataEnd-((const uint8_t*)chunk)));
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return BAD_TYPE;
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1158564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("%s size 0x%x or headerSize 0x%x is not on an integer boundary.",
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 name, (int)size, (int)headerSize);
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return BAD_TYPE;
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1198564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("%s size %p is smaller than header size %p.",
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             name, (void*)size, (void*)(int)headerSize);
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return BAD_TYPE;
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1238564c8da817a845353d213acd8636b76f567b234Steve Block    ALOGW("%s header size %p is too small.",
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         name, (void*)(int)headerSize);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return BAD_TYPE;
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectinline void Res_value::copyFrom_dtoh(const Res_value& src)
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size = dtohs(src.size);
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    res0 = src.res0;
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    dataType = src.dataType;
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data = dtohl(src.data);
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid Res_png_9patch::deviceToFile()
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < numXDivs; i++) {
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        xDivs[i] = htonl(xDivs[i]);
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < numYDivs; i++) {
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        yDivs[i] = htonl(yDivs[i]);
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingLeft = htonl(paddingLeft);
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingRight = htonl(paddingRight);
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingTop = htonl(paddingTop);
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingBottom = htonl(paddingBottom);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i=0; i<numColors; i++) {
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        colors[i] = htonl(colors[i]);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid Res_png_9patch::fileToDevice()
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < numXDivs; i++) {
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        xDivs[i] = ntohl(xDivs[i]);
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < numYDivs; i++) {
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        yDivs[i] = ntohl(yDivs[i]);
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingLeft = ntohl(paddingLeft);
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingRight = ntohl(paddingRight);
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingTop = ntohl(paddingTop);
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    paddingBottom = ntohl(paddingBottom);
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i=0; i<numColors; i++) {
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        colors[i] = ntohl(colors[i]);
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectsize_t Res_png_9patch::serializedSize()
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The size of this struct is 32 bytes on the 32-bit target system
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // 4 * int8_t
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // 4 * int32_t
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // 3 * pointer
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 32
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            + numXDivs * sizeof(int32_t)
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            + numYDivs * sizeof(int32_t)
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            + numColors * sizeof(uint32_t);
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid* Res_png_9patch::serialize()
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1844df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    // Use calloc since we're going to leave a few holes in the data
1854df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    // and want this to run cleanly under valgrind
1864df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    void* newData = calloc(1, serializedSize());
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    serialize(newData);
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return newData;
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid Res_png_9patch::serialize(void * outData)
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char* data = (char*) outData;
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    memmove(data, &wasDeserialized, 4);     // copy  wasDeserialized, numXDivs, numYDivs, numColors
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    memmove(data + 12, &paddingLeft, 16);   // copy paddingXXXX
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data += 32;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    memmove(data, this->xDivs, numXDivs * sizeof(int32_t));
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data +=  numXDivs * sizeof(int32_t);
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    memmove(data, this->yDivs, numYDivs * sizeof(int32_t));
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data +=  numYDivs * sizeof(int32_t);
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    memmove(data, this->colors, numColors * sizeof(uint32_t));
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void deserializeInternal(const void* inData, Res_png_9patch* outData) {
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char* patch = (char*) inData;
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (inData != outData) {
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memmove(&outData->wasDeserialized, patch, 4);     // copy  wasDeserialized, numXDivs, numYDivs, numColors
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memmove(&outData->paddingLeft, patch + 12, 4);     // copy  wasDeserialized, numXDivs, numYDivs, numColors
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    outData->wasDeserialized = true;
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char* data = (char*)outData;
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data +=  sizeof(Res_png_9patch);
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    outData->xDivs = (int32_t*) data;
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data +=  outData->numXDivs * sizeof(int32_t);
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    outData->yDivs = (int32_t*) data;
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    data +=  outData->numYDivs * sizeof(int32_t);
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    outData->colors = (uint32_t*) data;
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
22157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstadstatic bool assertIdmapHeader(const uint32_t* map, size_t sizeBytes)
22257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad{
22357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (sizeBytes < ResTable::IDMAP_HEADER_SIZE_BYTES) {
2246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        ALOGW("idmap assertion failed: size=%d bytes\n", (int)sizeBytes);
22557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return false;
22657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
22757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (*map != htodl(IDMAP_MAGIC)) { // htodl: map data expected to be in correct endianess
2288564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("idmap assertion failed: invalid magic found (is 0x%08x, expected 0x%08x)\n",
22957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad             *map, htodl(IDMAP_MAGIC));
23057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return false;
23157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
23257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    return true;
23357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad}
23457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
23557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstadstatic status_t idmapLookup(const uint32_t* map, size_t sizeBytes, uint32_t key, uint32_t* outValue)
23657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad{
23757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    // see README for details on the format of map
23857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (!assertIdmapHeader(map, sizeBytes)) {
23957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return UNKNOWN_ERROR;
24057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
24157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    map = map + IDMAP_HEADER_SIZE; // skip ahead to data segment
24257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    // size of data block, in uint32_t
24357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const size_t size = (sizeBytes - ResTable::IDMAP_HEADER_SIZE_BYTES) / sizeof(uint32_t);
24457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t type = Res_GETTYPE(key) + 1; // add one, idmap stores "public" type id
24557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t entry = Res_GETENTRY(key);
24657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t typeCount = *map;
24757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
24857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (type > typeCount) {
2498564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Resource ID map: type=%d exceeds number of types=%d\n", type, typeCount);
25057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return UNKNOWN_ERROR;
25157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
25257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (typeCount > size) {
2536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        ALOGW("Resource ID map: number of types=%d exceeds size of map=%d\n", typeCount, (int)size);
25457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return UNKNOWN_ERROR;
25557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
25657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t typeOffset = map[type];
25757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (typeOffset == 0) {
25857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        *outValue = 0;
25957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return NO_ERROR;
26057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
26157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (typeOffset + 1 > size) {
2628564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Resource ID map: type offset=%d exceeds reasonable value, size of map=%d\n",
2636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn             typeOffset, (int)size);
26457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return UNKNOWN_ERROR;
26557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
26657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t entryCount = map[typeOffset];
26757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t entryOffset = map[typeOffset + 1];
26857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (entryCount == 0 || entry < entryOffset || entry - entryOffset > entryCount - 1) {
26957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        *outValue = 0;
27057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return NO_ERROR;
27157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
27257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t index = typeOffset + 2 + entry - entryOffset;
27357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (index > size) {
2746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        ALOGW("Resource ID map: entry index=%d exceeds size of map=%d\n", index, (int)size);
27557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        *outValue = 0;
27657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return NO_ERROR;
27757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
27857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    *outValue = map[index];
27957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
28057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    return NO_ERROR;
28157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad}
28257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
28357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstadstatic status_t getIdmapPackageId(const uint32_t* map, size_t mapSize, uint32_t *outId)
28457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad{
28557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    if (!assertIdmapHeader(map, mapSize)) {
28657f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        return UNKNOWN_ERROR;
28757f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
28857f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    const uint32_t* p = map + IDMAP_HEADER_SIZE + 1;
28957f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    while (*p == 0) {
29057f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad        ++p;
29157f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    }
29257f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    *outId = (map[*p + IDMAP_HEADER_SIZE + 2] >> 24) & 0x000000ff;
29357f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad    return NO_ERROR;
29457f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad}
29557f4b77c89bafedf9468f9a636561c0c193405c9Mårten Kongstad
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectRes_png_9patch* Res_png_9patch::deserialize(const void* inData)
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (sizeof(void*) != sizeof(int32_t)) {
2993762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Cannot deserialize on non 32-bit system\n");
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    deserializeInternal(inData, (Res_png_9patch*) inData);
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return (Res_png_9patch*) inData;
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResStringPool::ResStringPool()
31119138468caf7050d482dc15f35a344eab11bb756Kenny Root    : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResStringPool::ResStringPool(const void* data, size_t size, bool copyData)
31619138468caf7050d482dc15f35a344eab11bb756Kenny Root    : mError(NO_INIT), mOwnedData(NULL), mHeader(NULL), mCache(NULL)
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setTo(data, size, copyData);
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResStringPool::~ResStringPool()
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uninit();
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t ResStringPool::setTo(const void* data, size_t size, bool copyData)
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!data || !size) {
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (mError=BAD_TYPE);
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uninit();
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const bool notDeviceEndian = htods(0xf0) != 0xf0;
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (copyData || notDeviceEndian) {
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mOwnedData = malloc(size);
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mOwnedData == NULL) {
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=NO_MEMORY);
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(mOwnedData, data, size);
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        data = mOwnedData;
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mHeader = (const ResStringPool_header*)data;
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (notDeviceEndian) {
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ResStringPool_header* h = const_cast<ResStringPool_header*>(mHeader);
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->header.headerSize = dtohs(mHeader->header.headerSize);
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->header.type = dtohs(mHeader->header.type);
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->header.size = dtohl(mHeader->header.size);
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->stringCount = dtohl(mHeader->stringCount);
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->styleCount = dtohl(mHeader->styleCount);
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->flags = dtohl(mHeader->flags);
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->stringsStart = dtohl(mHeader->stringsStart);
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        h->stylesStart = dtohl(mHeader->stylesStart);
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mHeader->header.headerSize > mHeader->header.size
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            || mHeader->header.size > size) {
3618564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Bad string block: header size %d or total size %d is larger than data size %d\n",
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (int)mHeader->header.headerSize, (int)mHeader->header.size, (int)size);
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (mError=BAD_TYPE);
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mSize = mHeader->header.size;
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mEntries = (const uint32_t*)
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (((const uint8_t*)data)+mHeader->header.headerSize);
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mHeader->stringCount > 0) {
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mHeader->stringCount*sizeof(uint32_t) < mHeader->stringCount)  // uint32 overflow?
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            || (mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t)))
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                > size) {
3738564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: entry of %d items extends past data size %d\n",
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)(mHeader->header.headerSize+(mHeader->stringCount*sizeof(uint32_t))),
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)size);
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
37819138468caf7050d482dc15f35a344eab11bb756Kenny Root
37919138468caf7050d482dc15f35a344eab11bb756Kenny Root        size_t charSize;
38019138468caf7050d482dc15f35a344eab11bb756Kenny Root        if (mHeader->flags&ResStringPool_header::UTF8_FLAG) {
38119138468caf7050d482dc15f35a344eab11bb756Kenny Root            charSize = sizeof(uint8_t);
3827e1d395686a23c8638f72af190f125cf690618a3Iliyan Malchev            mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t**));
38319138468caf7050d482dc15f35a344eab11bb756Kenny Root        } else {
38419138468caf7050d482dc15f35a344eab11bb756Kenny Root            charSize = sizeof(char16_t);
38519138468caf7050d482dc15f35a344eab11bb756Kenny Root        }
38619138468caf7050d482dc15f35a344eab11bb756Kenny Root
38719138468caf7050d482dc15f35a344eab11bb756Kenny Root        mStrings = (const void*)
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (((const uint8_t*)data)+mHeader->stringsStart);
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mHeader->stringsStart >= (mHeader->header.size-sizeof(uint16_t))) {
3908564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: string pool starts at %d, after total size %d\n",
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)mHeader->stringsStart, (int)mHeader->header.size);
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mHeader->styleCount == 0) {
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mStringPoolSize =
39619138468caf7050d482dc15f35a344eab11bb756Kenny Root                (mHeader->header.size-mHeader->stringsStart)/charSize;
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
3985e4d9a04fdf9bf28650853cb19a1b06b0d543c51Kenny Root            // check invariant: styles starts before end of data
3995e4d9a04fdf9bf28650853cb19a1b06b0d543c51Kenny Root            if (mHeader->stylesStart >= (mHeader->header.size-sizeof(uint16_t))) {
4008564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("Bad style block: style block starts at %d past data size of %d\n",
4015e4d9a04fdf9bf28650853cb19a1b06b0d543c51Kenny Root                    (int)mHeader->stylesStart, (int)mHeader->header.size);
4025e4d9a04fdf9bf28650853cb19a1b06b0d543c51Kenny Root                return (mError=BAD_TYPE);
4035e4d9a04fdf9bf28650853cb19a1b06b0d543c51Kenny Root            }
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // check invariant: styles follow the strings
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (mHeader->stylesStart <= mHeader->stringsStart) {
4068564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("Bad style block: style block starts at %d, before strings at %d\n",
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)mHeader->stylesStart, (int)mHeader->stringsStart);
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return (mError=BAD_TYPE);
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mStringPoolSize =
41119138468caf7050d482dc15f35a344eab11bb756Kenny Root                (mHeader->stylesStart-mHeader->stringsStart)/charSize;
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // check invariant: stringCount > 0 requires a string pool to exist
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mStringPoolSize == 0) {
4168564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: stringCount is %d but pool size is 0\n", (int)mHeader->stringCount);
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (notDeviceEndian) {
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t i;
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            uint32_t* e = const_cast<uint32_t*>(mEntries);
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (i=0; i<mHeader->stringCount; i++) {
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                e[i] = dtohl(mEntries[i]);
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
42619138468caf7050d482dc15f35a344eab11bb756Kenny Root            if (!(mHeader->flags&ResStringPool_header::UTF8_FLAG)) {
42719138468caf7050d482dc15f35a344eab11bb756Kenny Root                const char16_t* strings = (const char16_t*)mStrings;
42819138468caf7050d482dc15f35a344eab11bb756Kenny Root                char16_t* s = const_cast<char16_t*>(strings);
42919138468caf7050d482dc15f35a344eab11bb756Kenny Root                for (i=0; i<mStringPoolSize; i++) {
43019138468caf7050d482dc15f35a344eab11bb756Kenny Root                    s[i] = dtohs(strings[i]);
43119138468caf7050d482dc15f35a344eab11bb756Kenny Root                }
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
43519138468caf7050d482dc15f35a344eab11bb756Kenny Root        if ((mHeader->flags&ResStringPool_header::UTF8_FLAG &&
43619138468caf7050d482dc15f35a344eab11bb756Kenny Root                ((uint8_t*)mStrings)[mStringPoolSize-1] != 0) ||
43719138468caf7050d482dc15f35a344eab11bb756Kenny Root                (!mHeader->flags&ResStringPool_header::UTF8_FLAG &&
43819138468caf7050d482dc15f35a344eab11bb756Kenny Root                ((char16_t*)mStrings)[mStringPoolSize-1] != 0)) {
4398564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: last string is not 0-terminated\n");
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStrings = NULL;
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStringPoolSize = 0;
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mHeader->styleCount > 0) {
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mEntryStyles = mEntries + mHeader->stringCount;
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // invariant: integer overflow in calculating mEntryStyles
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mEntryStyles < mEntries) {
4518564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: integer overflow finding styles\n");
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader) > (int)size) {
4568564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: entry of %d styles extends past data size %d\n",
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)((const uint8_t*)mEntryStyles-(const uint8_t*)mHeader),
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)size);
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStyles = (const uint32_t*)
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (((const uint8_t*)data)+mHeader->stylesStart);
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mHeader->stylesStart >= mHeader->header.size) {
4648564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: style pool starts %d, after total size %d\n",
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)mHeader->stylesStart, (int)mHeader->header.size);
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStylePoolSize =
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (mHeader->header.size-mHeader->stylesStart)/sizeof(uint32_t);
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (notDeviceEndian) {
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t i;
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            uint32_t* e = const_cast<uint32_t*>(mEntryStyles);
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (i=0; i<mHeader->styleCount; i++) {
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                e[i] = dtohl(mEntryStyles[i]);
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            uint32_t* s = const_cast<uint32_t*>(mStyles);
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (i=0; i<mStylePoolSize; i++) {
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                s[i] = dtohl(mStyles[i]);
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResStringPool_span endSpan = {
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            { htodl(ResStringPool_span::END) },
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            htodl(ResStringPool_span::END), htodl(ResStringPool_span::END)
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        };
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (memcmp(&mStyles[mStylePoolSize-(sizeof(endSpan)/sizeof(uint32_t))],
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   &endSpan, sizeof(endSpan)) != 0) {
4898564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: last style is not 0xFFFFFFFF-terminated\n");
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=BAD_TYPE);
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mEntryStyles = NULL;
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStyles = NULL;
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mStylePoolSize = 0;
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return (mError=NO_ERROR);
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t ResStringPool::getError() const
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mError;
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid ResStringPool::uninit()
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mError = NO_INIT;
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mOwnedData) {
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        free(mOwnedData);
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mOwnedData = NULL;
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
51319138468caf7050d482dc15f35a344eab11bb756Kenny Root    if (mHeader != NULL && mCache != NULL) {
51419138468caf7050d482dc15f35a344eab11bb756Kenny Root        for (size_t x = 0; x < mHeader->stringCount; x++) {
51519138468caf7050d482dc15f35a344eab11bb756Kenny Root            if (mCache[x] != NULL) {
51619138468caf7050d482dc15f35a344eab11bb756Kenny Root                free(mCache[x]);
51719138468caf7050d482dc15f35a344eab11bb756Kenny Root                mCache[x] = NULL;
51819138468caf7050d482dc15f35a344eab11bb756Kenny Root            }
51919138468caf7050d482dc15f35a344eab11bb756Kenny Root        }
52019138468caf7050d482dc15f35a344eab11bb756Kenny Root        free(mCache);
52119138468caf7050d482dc15f35a344eab11bb756Kenny Root        mCache = NULL;
52219138468caf7050d482dc15f35a344eab11bb756Kenny Root    }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
525300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root/**
526300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * Strings in UTF-16 format have length indicated by a length encoded in the
527300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * stored data. It is either 1 or 2 characters of length data. This allows a
528300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * maximum length of 0x7FFFFFF (2147483647 bytes), but if you're storing that
529300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * much data in a string, you're abusing them.
530300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root *
531300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * If the high bit is set, then there are two characters or 4 bytes of length
532300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * data encoded. In that case, drop the high bit of the first character and
533300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * add it together with the next character.
534300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root */
535300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Rootstatic inline size_t
536300ba6846949f5b21c6d93d7698dbc39716cf832Kenny RootdecodeLength(const char16_t** str)
537300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root{
538300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    size_t len = **str;
539300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    if ((len & 0x8000) != 0) {
540300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root        (*str)++;
541300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root        len = ((len & 0x7FFF) << 16) | **str;
542300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    }
543300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    (*str)++;
544300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    return len;
545300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root}
546300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
547300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root/**
548300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * Strings in UTF-8 format have length indicated by a length encoded in the
549300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * stored data. It is either 1 or 2 characters of length data. This allows a
550300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * maximum length of 0x7FFF (32767 bytes), but you should consider storing
551300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * text in another way if you're using that much data in a single string.
552300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root *
553300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * If the high bit is set, then there are two characters or 2 bytes of length
554300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * data encoded. In that case, drop the high bit of the first character and
555300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root * add it together with the next character.
556300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root */
557300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Rootstatic inline size_t
558300ba6846949f5b21c6d93d7698dbc39716cf832Kenny RootdecodeLength(const uint8_t** str)
559300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root{
560300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    size_t len = **str;
561300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    if ((len & 0x80) != 0) {
562300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root        (*str)++;
563300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root        len = ((len & 0x7F) << 8) | **str;
564300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    }
565300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    (*str)++;
566300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root    return len;
567300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root}
568300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
569300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Rootconst uint16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mError == NO_ERROR && idx < mHeader->stringCount) {
57219138468caf7050d482dc15f35a344eab11bb756Kenny Root        const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
57319138468caf7050d482dc15f35a344eab11bb756Kenny Root        const uint32_t off = mEntries[idx]/(isUTF8?sizeof(char):sizeof(char16_t));
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (off < (mStringPoolSize-1)) {
57519138468caf7050d482dc15f35a344eab11bb756Kenny Root            if (!isUTF8) {
57619138468caf7050d482dc15f35a344eab11bb756Kenny Root                const char16_t* strings = (char16_t*)mStrings;
57719138468caf7050d482dc15f35a344eab11bb756Kenny Root                const char16_t* str = strings+off;
578300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
579300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                *u16len = decodeLength(&str);
580300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                if ((uint32_t)(str+*u16len-strings) < mStringPoolSize) {
58119138468caf7050d482dc15f35a344eab11bb756Kenny Root                    return str;
58219138468caf7050d482dc15f35a344eab11bb756Kenny Root                } else {
5838564c8da817a845353d213acd8636b76f567b234Steve Block                    ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
584300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                            (int)idx, (int)(str+*u16len-strings), (int)mStringPoolSize);
58519138468caf7050d482dc15f35a344eab11bb756Kenny Root                }
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
58719138468caf7050d482dc15f35a344eab11bb756Kenny Root                const uint8_t* strings = (uint8_t*)mStrings;
588300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                const uint8_t* u8str = strings+off;
589300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
590300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                *u16len = decodeLength(&u8str);
591300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                size_t u8len = decodeLength(&u8str);
592300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
593300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                // encLen must be less than 0x7FFF due to encoding.
594300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                if ((uint32_t)(u8str+u8len-strings) < mStringPoolSize) {
59519138468caf7050d482dc15f35a344eab11bb756Kenny Root                    AutoMutex lock(mDecodeLock);
596300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
59719138468caf7050d482dc15f35a344eab11bb756Kenny Root                    if (mCache[idx] != NULL) {
59819138468caf7050d482dc15f35a344eab11bb756Kenny Root                        return mCache[idx];
59919138468caf7050d482dc15f35a344eab11bb756Kenny Root                    }
600300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
601300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                    ssize_t actualLen = utf8_to_utf16_length(u8str, u8len);
602300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                    if (actualLen < 0 || (size_t)actualLen != *u16len) {
6038564c8da817a845353d213acd8636b76f567b234Steve Block                        ALOGW("Bad string block: string #%lld decoded length is not correct "
604300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                                "%lld vs %llu\n",
605300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                                (long long)idx, (long long)actualLen, (long long)*u16len);
606300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                        return NULL;
607300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                    }
608300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
609300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                    char16_t *u16str = (char16_t *)calloc(*u16len+1, sizeof(char16_t));
61019138468caf7050d482dc15f35a344eab11bb756Kenny Root                    if (!u16str) {
6118564c8da817a845353d213acd8636b76f567b234Steve Block                        ALOGW("No memory when trying to allocate decode cache for string #%d\n",
61219138468caf7050d482dc15f35a344eab11bb756Kenny Root                                (int)idx);
61319138468caf7050d482dc15f35a344eab11bb756Kenny Root                        return NULL;
61419138468caf7050d482dc15f35a344eab11bb756Kenny Root                    }
615300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root
616300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                    utf8_to_utf16(u8str, u8len, u16str);
61719138468caf7050d482dc15f35a344eab11bb756Kenny Root                    mCache[idx] = u16str;
61819138468caf7050d482dc15f35a344eab11bb756Kenny Root                    return u16str;
61919138468caf7050d482dc15f35a344eab11bb756Kenny Root                } else {
6208564c8da817a845353d213acd8636b76f567b234Steve Block                    ALOGW("Bad string block: string #%lld extends to %lld, past end at %lld\n",
621300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                            (long long)idx, (long long)(u8str+u8len-strings),
622300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                            (long long)mStringPoolSize);
62319138468caf7050d482dc15f35a344eab11bb756Kenny Root                }
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
6268564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)idx, (int)(off*sizeof(uint16_t)),
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)(mStringPoolSize*sizeof(uint16_t)));
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
634780d2a1b714724d85227141c76b3c64f543f00b4Kenny Rootconst char* ResStringPool::string8At(size_t idx, size_t* outLen) const
635780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root{
636780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root    if (mError == NO_ERROR && idx < mHeader->stringCount) {
637780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root        const bool isUTF8 = (mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0;
638780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root        const uint32_t off = mEntries[idx]/(isUTF8?sizeof(char):sizeof(char16_t));
639780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root        if (off < (mStringPoolSize-1)) {
640780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root            if (isUTF8) {
641780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                const uint8_t* strings = (uint8_t*)mStrings;
642780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                const uint8_t* str = strings+off;
643300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                *outLen = decodeLength(&str);
644300ba6846949f5b21c6d93d7698dbc39716cf832Kenny Root                size_t encLen = decodeLength(&str);
645780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                if ((uint32_t)(str+encLen-strings) < mStringPoolSize) {
646780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                    return (const char*)str;
647780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                } else {
6488564c8da817a845353d213acd8636b76f567b234Steve Block                    ALOGW("Bad string block: string #%d extends to %d, past end at %d\n",
649780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                            (int)idx, (int)(str+encLen-strings), (int)mStringPoolSize);
650780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                }
651780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root            }
652780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root        } else {
6538564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: string #%d entry is at %d, past end at %d\n",
654780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                    (int)idx, (int)(off*sizeof(uint16_t)),
655780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root                    (int)(mStringPoolSize*sizeof(uint16_t)));
656780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root        }
657780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root    }
658780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root    return NULL;
659780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root}
660780d2a1b714724d85227141c76b3c64f543f00b4Kenny Root
6616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornconst String8 ResStringPool::string8ObjectAt(size_t idx) const
6626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn{
6636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    size_t len;
6646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    const char *str = (const char*)string8At(idx, &len);
6656c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (str != NULL) {
6666c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return String8(str);
6676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
6686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return String8(stringAt(idx, &len));
6696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
6706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst ResStringPool_span* ResStringPool::styleAt(const ResStringPool_ref& ref) const
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return styleAt(ref.index);
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst ResStringPool_span* ResStringPool::styleAt(size_t idx) const
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mError == NO_ERROR && idx < mHeader->styleCount) {
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint32_t off = (mEntryStyles[idx]/sizeof(uint32_t));
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (off < mStylePoolSize) {
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (const ResStringPool_span*)(mStyles+off);
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
6838564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad string block: style #%d entry is at %d, past end at %d\n",
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)idx, (int)(off*sizeof(uint32_t)),
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)(mStylePoolSize*sizeof(uint32_t)));
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mError != NO_ERROR) {
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mError;
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t len;
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
69919138468caf7050d482dc15f35a344eab11bb756Kenny Root    // TODO optimize searching for UTF-8 strings taking into account
70019138468caf7050d482dc15f35a344eab11bb756Kenny Root    // the cache fill to determine when to convert the searched-for
70119138468caf7050d482dc15f35a344eab11bb756Kenny Root    // string key to UTF-8.
70219138468caf7050d482dc15f35a344eab11bb756Kenny Root
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Do a binary search for the string...
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ssize_t l = 0;
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ssize_t h = mHeader->stringCount-1;
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ssize_t mid;
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (l <= h) {
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mid = l + (h - l)/2;
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char16_t* s = stringAt(mid, &len);
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int c = s ? strzcmp16(s, len, str, strLen) : -1;
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            POOL_NOISY(printf("Looking for %s, at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         String8(str).string(),
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         String8(s).string(),
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         c, (int)l, (int)mid, (int)h));
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (c == 0) {
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return mid;
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (c < 0) {
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                l = mid + 1;
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                h = mid - 1;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // It is unusual to get the ID from an unsorted string block...
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // most often this happens because we want to get IDs for style
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // span tags; since those always appear at the end of the string
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // block, start searching at the back.
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i=mHeader->stringCount-1; i>=0; i--) {
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char16_t* s = stringAt(i, &len);
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            POOL_NOISY(printf("Looking for %s, at %s, i=%d\n",
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         String8(str, strLen).string(),
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         String8(s).string(),
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         i));
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (s && strzcmp16(s, len, str, strLen) == 0) {
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return i;
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NAME_NOT_FOUND;
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectsize_t ResStringPool::size() const
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return (mError == NO_ERROR) ? mHeader->stringCount : 0;
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornsize_t ResStringPool::styleCount() const
7516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn{
7526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return (mError == NO_ERROR) ? mHeader->styleCount : 0;
7536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
7546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
7556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornsize_t ResStringPool::bytes() const
7566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn{
7576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return (mError == NO_ERROR) ? mHeader->header.size : 0;
7586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
7596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
7606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornbool ResStringPool::isSorted() const
7616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn{
7626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return (mHeader->flags&ResStringPool_header::SORTED_FLAG)!=0;
7636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
7646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
765bb79f64b89bd7be7019035a0347c681e6f39fe6bKenny Rootbool ResStringPool::isUTF8() const
766bb79f64b89bd7be7019035a0347c681e6f39fe6bKenny Root{
767bb79f64b89bd7be7019035a0347c681e6f39fe6bKenny Root    return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0;
768bb79f64b89bd7be7019035a0347c681e6f39fe6bKenny Root}
769bb79f64b89bd7be7019035a0347c681e6f39fe6bKenny Root
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLParser::ResXMLParser(const ResXMLTree& tree)
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    : mTree(tree), mEventCode(BAD_DOCUMENT)
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid ResXMLParser::restart()
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mCurNode = NULL;
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mEventCode = mTree.mError == NO_ERROR ? START_DOCUMENT : BAD_DOCUMENT;
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
784cf244ada58539ce857ec041d7288d0271204fbb6Dianne Hackbornconst ResStringPool& ResXMLParser::getStrings() const
785cf244ada58539ce857ec041d7288d0271204fbb6Dianne Hackborn{
786cf244ada58539ce857ec041d7288d0271204fbb6Dianne Hackborn    return mTree.mStrings;
787cf244ada58539ce857ec041d7288d0271204fbb6Dianne Hackborn}
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLParser::event_code_t ResXMLParser::getEventType() const
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mEventCode;
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLParser::event_code_t ResXMLParser::next()
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_DOCUMENT) {
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mCurNode = mTree.mRootNode;
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mCurExt = mTree.mRootExt;
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (mEventCode=mTree.mRootCode);
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (mEventCode >= FIRST_CHUNK_CODE) {
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return nextNode();
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mEventCode;
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8065f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getCommentID() const
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1;
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getComment(size_t* outLen) const
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getCommentID();
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8175f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianuint32_t ResXMLParser::getLineNumber() const
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1;
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8225f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getTextID() const
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == TEXT) {
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index);
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getText(size_t* outLen) const
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getTextID();
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::getTextValue(Res_value* outValue) const
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == TEXT) {
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        outValue->copyFrom_dtoh(((const ResXMLTree_cdataExt*)mCurExt)->typedData);
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return sizeof(Res_value);
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return BAD_TYPE;
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8455f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getNamespacePrefixID() const
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index);
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getNamespacePrefix(size_t* outLen) const
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getNamespacePrefixID();
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("prefix=%d  event=%p\n", id, mEventCode);
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8605f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getNamespaceUriID() const
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index);
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getNamespaceUri(size_t* outLen) const
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getNamespaceUriID();
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("uri=%d  event=%p\n", id, mEventCode);
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8755f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getElementNamespaceID() const
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index);
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == END_TAG) {
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->ns.index);
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getElementNamespace(size_t* outLen) const
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getElementNamespaceID();
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8925f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getElementNameID() const
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index);
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == END_TAG) {
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(((const ResXMLTree_endElementExt*)mCurExt)->name.index);
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getElementName(size_t* outLen) const
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getElementNameID();
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
9079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectsize_t ResXMLParser::getAttributeCount() const
9109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
9129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohs(((const ResXMLTree_attrExt*)mCurExt)->attributeCount);
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 0;
9159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9175f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
9189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
9219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return dtohl(attr->ns.index);
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -2;
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getAttributeNamespace(size_t idx, size_t* outLen) const
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getAttributeNamespaceID(idx);
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("attribute namespace=%d  idx=%d  event=%p\n", id, idx, mEventCode);
9369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id));
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9405f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getAttributeNameID(size_t idx) const
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
9469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
9479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return dtohl(attr->name.index);
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getAttributeName(size_t idx, size_t* outLen) const
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getAttributeNameID(idx);
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("attribute name=%d  idx=%d  event=%p\n", id, idx, mEventCode);
9599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id));
9609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
9619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9635f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianuint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
9649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getAttributeNameID(idx);
9669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (id >= 0 && (size_t)id < mTree.mNumResIds) {
9679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return dtohl(mTree.mResIds[id]);
9689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 0;
9709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9725f91097b26f036a45e32f4e4cae3e9e135a6042dMathias Agopianint32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
9739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
9759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
9769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
9779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
9789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
9799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
9809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
9819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return dtohl(attr->rawValue.index);
9829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
9859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst uint16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const
9889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t id = getAttributeValueStringID(idx);
9909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //XML_NOISY(printf("getAttributeValue 0x%x=0x%x\n", idx, id));
9919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
9929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint32_t ResXMLParser::getAttributeDataType(size_t idx) const
9959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
9979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
9989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
9999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
10019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
10029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
10039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return attr->typedValue.dataType;
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return Res_value::TYPE_NULL;
10079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint32_t ResXMLParser::getAttributeData(size_t idx) const
10109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
10139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
10149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
10159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
10169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
10179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
10189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return dtohl(attr->typedValue.data);
10199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 0;
10229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::getAttributeValue(size_t idx, Res_value* outValue) const
10259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
10289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx < dtohs(tag->attributeCount)) {
10299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const ResXMLTree_attribute* attr = (const ResXMLTree_attribute*)
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)tag)
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + dtohs(tag->attributeStart)
10329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 + (dtohs(tag->attributeSize)*idx));
10339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            outValue->copyFrom_dtoh(attr->typedValue);
10349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sizeof(Res_value);
10359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return BAD_TYPE;
10389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::indexOfAttribute(const char* ns, const char* attr) const
10419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String16 nsStr(ns != NULL ? ns : "");
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String16 attrStr(attr);
10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return indexOfAttribute(ns ? nsStr.string() : NULL, ns ? nsStr.size() : 0,
10459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            attrStr.string(), attrStr.size());
10469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::indexOfAttribute(const char16_t* ns, size_t nsLen,
10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       const char16_t* attr, size_t attrLen) const
10509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const size_t N = getAttributeCount();
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (size_t i=0; i<N; i++) {
10549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t curNsLen, curAttrLen;
10559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char16_t* curNs = getAttributeNamespace(i, &curNsLen);
10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char16_t* curAttr = getAttributeName(i, &curAttrLen);
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            //printf("%d: ns=%p attr=%p curNs=%p curAttr=%p\n",
10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            //       i, ns, attr, curNs, curAttr);
10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            //printf(" --> attr=%s, curAttr=%s\n",
10609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            //       String8(attr).string(), String8(curAttr).string());
10619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (attr && curAttr && (strzcmp16(attr, attrLen, curAttr, curAttrLen) == 0)) {
10629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (ns == NULL) {
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (curNs == NULL) return i;
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (curNs != NULL) {
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    //printf(" --> ns=%s, curNs=%s\n",
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    //       String8(ns).string(), String8(curNs).string());
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (strzcmp16(ns, nsLen, curNs, curNsLen) == 0) return i;
10689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NAME_NOT_FOUND;
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::indexOfID() const
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->idIndex);
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx > 0) return (idx-1);
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NAME_NOT_FOUND;
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::indexOfClass() const
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->classIndex);
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx > 0) return (idx-1);
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NAME_NOT_FOUND;
10929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectssize_t ResXMLParser::indexOfStyle() const
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode == START_TAG) {
10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ssize_t idx = dtohs(((const ResXMLTree_attrExt*)mCurExt)->styleIndex);
10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (idx > 0) return (idx-1);
10999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NAME_NOT_FOUND;
11019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
11029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLParser::event_code_t ResXMLParser::nextNode()
11049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mEventCode < 0) {
11069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mEventCode;
11079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
11109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_node* next = (const ResXMLTree_node*)
11119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size));
11128564c8da817a845353d213acd8636b76f567b234Steve Block        //ALOGW("Next node: prev=%p, next=%p\n", mCurNode, next);
11139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (((const uint8_t*)next) >= mTree.mDataEnd) {
11159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mCurNode = NULL;
11169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mEventCode=END_DOCUMENT);
11179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mTree.validateNode(next) != NO_ERROR) {
11209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mCurNode = NULL;
11219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mEventCode=BAD_DOCUMENT);
11229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mCurNode = next;
11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint16_t headerSize = dtohs(next->header.headerSize);
11269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint32_t totalSize = dtohl(next->header.size);
11279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mCurExt = ((const uint8_t*)next) + headerSize;
11289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t minExtSize = 0;
11299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        event_code_t eventCode = (event_code_t)dtohs(next->header.type);
11309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch ((mEventCode=eventCode)) {
11319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case RES_XML_START_NAMESPACE_TYPE:
11329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case RES_XML_END_NAMESPACE_TYPE:
11339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                minExtSize = sizeof(ResXMLTree_namespaceExt);
11349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
11359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case RES_XML_START_ELEMENT_TYPE:
11369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                minExtSize = sizeof(ResXMLTree_attrExt);
11379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
11389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case RES_XML_END_ELEMENT_TYPE:
11399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                minExtSize = sizeof(ResXMLTree_endElementExt);
11409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
11419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case RES_XML_CDATA_TYPE:
11429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                minExtSize = sizeof(ResXMLTree_cdataExt);
11439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
11449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            default:
11458564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("Unknown XML block: header type %d in node at %d\n",
11469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                     (int)dtohs(next->header.type),
11479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                     (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)));
11489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                continue;
11499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((totalSize-headerSize) < minExtSize) {
11528564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad XML block: header type 0x%x in node at 0x%x has size %d, need %d\n",
11539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (int)dtohs(next->header.type),
11549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (int)(((const uint8_t*)next)-((const uint8_t*)mTree.mHeader)),
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 (int)(totalSize-headerSize), (int)minExtSize);
11569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mEventCode=BAD_DOCUMENT);
11579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //printf("CurNode=%p, CurExt=%p, headerSize=%d, minExtSize=%d\n",
11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //       mCurNode, mCurExt, headerSize, minExtSize);
11619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return eventCode;
11639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (true);
11649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
11659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid ResXMLParser::getPosition(ResXMLParser::ResXMLPosition* pos) const
11679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    pos->eventCode = mEventCode;
11699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    pos->curNode = mCurNode;
11709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    pos->curExt = mCurExt;
11719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
11729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid ResXMLParser::setPosition(const ResXMLParser::ResXMLPosition& pos)
11749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mEventCode = pos.eventCode;
11769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mCurNode = pos.curNode;
11779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mCurExt = pos.curExt;
11789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
11799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
11829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic volatile int32_t gCount = 0;
11849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLTree::ResXMLTree()
11869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    : ResXMLParser(*this)
11879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    , mError(NO_INIT), mOwnedData(NULL)
11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11896215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    restart();
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLTree::ResXMLTree(const void* data, size_t size, bool copyData)
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    : ResXMLParser(*this)
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    , mError(NO_INIT), mOwnedData(NULL)
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11976215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setTo(data, size, copyData);
11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectResXMLTree::~ResXMLTree()
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
12036215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uninit();
12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t ResXMLTree::setTo(const void* data, size_t size, bool copyData)
12089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uninit();
12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mEventCode = START_DOCUMENT;
12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (copyData) {
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mOwnedData = malloc(size);
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mOwnedData == NULL) {
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (mError=NO_MEMORY);
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(mOwnedData, data, size);
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        data = mOwnedData;
12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mHeader = (const ResXMLTree_header*)data;
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mSize = dtohl(mHeader->header.size);
12239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (dtohs(mHeader->header.headerSize) > mSize || mSize > size) {
12248564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Bad XML block: header size %d or total size %d is larger than data size %d\n",
12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             (int)dtohs(mHeader->header.headerSize),
12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             (int)dtohl(mHeader->header.size), (int)size);
12279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mError = BAD_TYPE;
12289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        restart();
12299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mError;
12309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mDataEnd = ((const uint8_t*)mHeader) + mSize;
12329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mStrings.uninit();
12349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mRootNode = NULL;
12359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mResIds = NULL;
12369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mNumResIds = 0;
12379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // First look for a couple interesting chunks: the string block
12399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // and first XML node.
12409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const ResChunk_header* chunk =
12419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (const ResChunk_header*)(((const uint8_t*)mHeader) + dtohs(mHeader->header.headerSize));
12429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const ResChunk_header* lastChunk = chunk;
12439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (((const uint8_t*)chunk) < (mDataEnd-sizeof(ResChunk_header)) &&
12449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           ((const uint8_t*)chunk) < (mDataEnd-dtohl(chunk->size))) {
12459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        status_t err = validate_chunk(chunk, sizeof(ResChunk_header), mDataEnd, "XML");
12469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (err != NO_ERROR) {
12479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mError = err;
12489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto done;
12499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint16_t type = dtohs(chunk->type);
12519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const size_t size = dtohl(chunk->size);
12529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        XML_NOISY(printf("Scanning @ %p: type=0x%x, size=0x%x\n",
12539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                     (void*)(((uint32_t)chunk)-((uint32_t)mHeader)), type, size));
12549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (type == RES_STRING_POOL_TYPE) {
12559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mStrings.setTo(chunk, size);
12569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (type == RES_XML_RESOURCE_MAP_TYPE) {
12579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mResIds = (const uint32_t*)
12589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (((const uint8_t*)chunk)+dtohs(chunk->headerSize));
12599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mNumResIds = (dtohl(chunk->size)-dtohs(chunk->headerSize))/sizeof(uint32_t);
12609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (type >= RES_XML_FIRST_CHUNK_TYPE
12619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   && type <= RES_XML_LAST_CHUNK_TYPE) {
12629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (validateNode((const ResXMLTree_node*)chunk) != NO_ERROR) {
12639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mError = BAD_TYPE;
12649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto done;
12659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mCurNode = (const ResXMLTree_node*)lastChunk;
12679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (nextNode() == BAD_DOCUMENT) {
12689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mError = BAD_TYPE;
12699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto done;
12709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRootNode = mCurNode;
12729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRootExt = mCurExt;
12739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRootCode = mEventCode;
12749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
12759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
12769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            XML_NOISY(printf("Skipping unknown chunk!\n"));
12779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        lastChunk = chunk;
12799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        chunk = (const ResChunk_header*)
12809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (((const uint8_t*)chunk) + size);
12819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mRootNode == NULL) {
12848564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Bad XML block: no root element node found\n");
12859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mError = BAD_TYPE;
12869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto done;
12879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mError = mStrings.getError();
12909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectdone:
12929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    restart();
12939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mError;
12949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
12959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t ResXMLTree::getError() const
12979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
12989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mError;
12999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
13009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid ResXMLTree::uninit()
13029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
13039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mError = NO_INIT;
130419138468caf7050d482dc15f35a344eab11bb756Kenny Root    mStrings.uninit();
13059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mOwnedData) {
13069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        free(mOwnedData);
13079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mOwnedData = NULL;
13089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    restart();
13109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
13119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t ResXMLTree::validateNode(const ResXMLTree_node* node) const
13139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
13149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t eventCode = dtohs(node->header.type);
13159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t err = validate_chunk(
13179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        &node->header, sizeof(ResXMLTree_node),
13189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mDataEnd, "ResXMLTree_node");
13199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err >= NO_ERROR) {
13219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Only perform additional validation on START nodes
13229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (eventCode != RES_XML_START_ELEMENT_TYPE) {
13239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return NO_ERROR;
13249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint16_t headerSize = dtohs(node->header.headerSize);
13279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint32_t size = dtohl(node->header.size);
13289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResXMLTree_attrExt* attrExt = (const ResXMLTree_attrExt*)
13299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (((const uint8_t*)node) + headerSize);
13309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // check for sensical values pulled out of the stream so far...
13319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((size >= headerSize + sizeof(ResXMLTree_attrExt))
13329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && ((void*)attrExt > (void*)node)) {
13339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const size_t attrSize = ((size_t)dtohs(attrExt->attributeSize))
13349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                * dtohs(attrExt->attributeCount);
13359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((dtohs(attrExt->attributeStart)+attrSize) <= (size-headerSize)) {
13369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return NO_ERROR;
13379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
13388564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
13399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (unsigned int)(dtohs(attrExt->attributeStart)+attrSize),
13409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (unsigned int)(size-headerSize));
13419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
13438564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad XML start block: node header size 0x%x, size 0x%x\n",
13449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (unsigned int)headerSize, (unsigned int)size);
13459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return BAD_TYPE;
13479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return err;
13509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if 0
13529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const bool isStart = dtohs(node->header.type) == RES_XML_START_ELEMENT_TYPE;
13539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t headerSize = dtohs(node->header.headerSize);
13559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint32_t size = dtohl(node->header.size);
13569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (headerSize >= (isStart ? sizeof(ResXMLTree_attrNode) : sizeof(ResXMLTree_node))) {
13589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (size >= headerSize) {
13599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (((const uint8_t*)node) <= (mDataEnd-size)) {
13609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (!isStart) {
13619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return NO_ERROR;
13629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
13639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if ((((size_t)dtohs(node->attributeSize))*dtohs(node->attributeCount))
13649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        <= (size-headerSize)) {
13659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return NO_ERROR;
13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
13678564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("Bad XML block: node attributes use 0x%x bytes, only have 0x%x bytes\n",
13689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        ((int)dtohs(node->attributeSize))*dtohs(node->attributeCount),
13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        (int)(size-headerSize));
13709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return BAD_TYPE;
13719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
13728564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Bad XML block: node at 0x%x extends beyond data end 0x%x\n",
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)), (int)mSize);
13749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return BAD_TYPE;
13759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13768564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Bad XML block: node at 0x%x header size 0x%x smaller than total size 0x%x\n",
13779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
13789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (int)headerSize, (int)size);
13799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return BAD_TYPE;
13809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13818564c8da817a845353d213acd8636b76f567b234Steve Block    ALOGW("Bad XML block: node at 0x%x header size 0x%x too small\n",
13829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (int)(((const uint8_t*)node)-((const uint8_t*)mHeader)),
13839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (int)headerSize);
13849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return BAD_TYPE;
13859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
13869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
13879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
13899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
13909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --------------------------------------------------------------------
13919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornvoid ResTable_config::copyFromDeviceNoSwap(const ResTable_config& o) {
13936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    const size_t size = dtohl(o.size);
13946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (size >= sizeof(ResTable_config)) {
13956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        *this = o;
13966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    } else {
13976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        memcpy(this, &o, size);
13986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
13996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
14016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
14026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornvoid ResTable_config::copyFromDtoH(const ResTable_config& o) {
14036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    copyFromDeviceNoSwap(o);
14046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    size = sizeof(ResTable_config);
14056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    mcc = dtohs(mcc);
14066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    mnc = dtohs(mnc);
14076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    density = dtohs(density);
14086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenWidth = dtohs(screenWidth);
14096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenHeight = dtohs(screenHeight);
14106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    sdkVersion = dtohs(sdkVersion);
14116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    minorVersion = dtohs(minorVersion);
14126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    smallestScreenWidthDp = dtohs(smallestScreenWidthDp);
14136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenWidthDp = dtohs(screenWidthDp);
14146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenHeightDp = dtohs(screenHeightDp);
14156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
14166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
14176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornvoid ResTable_config::swapHtoD() {
14186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    size = htodl(size);
14196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    mcc = htods(mcc);
14206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    mnc = htods(mnc);
14216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    density = htods(density);
14226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenWidth = htods(screenWidth);
14236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenHeight = htods(screenHeight);
14246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    sdkVersion = htods(sdkVersion);
14256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    minorVersion = htods(minorVersion);
14266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    smallestScreenWidthDp = htods(smallestScreenWidthDp);
14276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenWidthDp = htods(screenWidthDp);
14286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    screenHeightDp = htods(screenHeightDp);
14296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
14306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
14316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornint ResTable_config::compare(const ResTable_config& o) const {
14326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    int32_t diff = (int32_t)(imsi - o.imsi);
14336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(locale - o.locale);
14356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(screenType - o.screenType);
14376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(input - o.input);
14396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(screenSize - o.screenSize);
14416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(version - o.version);
14436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(screenLayout - o.screenLayout);
14456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(uiMode - o.uiMode);
14476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(smallestScreenWidthDp - o.smallestScreenWidthDp);
14496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (diff != 0) return diff;
14506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    diff = (int32_t)(screenSizeDp - o.screenSizeDp);
14516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return (int)diff;
14526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
14536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
14546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornint ResTable_config::compareLogical(const ResTable_config& o) const {
14556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mcc != o.mcc) {
14566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return mcc < o.mcc ? -1 : 1;
14576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mnc != o.mnc) {
14596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return mnc < o.mnc ? -1 : 1;
14606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (language[0] != o.language[0]) {
14626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return language[0] < o.language[0] ? -1 : 1;
14636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (language[1] != o.language[1]) {
14656c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return language[1] < o.language[1] ? -1 : 1;
14666c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (country[0] != o.country[0]) {
14686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return country[0] < o.country[0] ? -1 : 1;
14696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (country[1] != o.country[1]) {
14716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return country[1] < o.country[1] ? -1 : 1;
14726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14735f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    if ((screenLayout & MASK_LAYOUTDIR) != (o.screenLayout & MASK_LAYOUTDIR)) {
14745f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        return (screenLayout & MASK_LAYOUTDIR) < (o.screenLayout & MASK_LAYOUTDIR) ? -1 : 1;
14755f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    }
14766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
14776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return smallestScreenWidthDp < o.smallestScreenWidthDp ? -1 : 1;
14786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenWidthDp != o.screenWidthDp) {
14806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return screenWidthDp < o.screenWidthDp ? -1 : 1;
14816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenHeightDp != o.screenHeightDp) {
14836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return screenHeightDp < o.screenHeightDp ? -1 : 1;
14846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenWidth != o.screenWidth) {
14866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return screenWidth < o.screenWidth ? -1 : 1;
14876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenHeight != o.screenHeight) {
14896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return screenHeight < o.screenHeight ? -1 : 1;
14906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (density != o.density) {
14926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return density < o.density ? -1 : 1;
14936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (orientation != o.orientation) {
14956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return orientation < o.orientation ? -1 : 1;
14966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
14976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (touchscreen != o.touchscreen) {
14986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return touchscreen < o.touchscreen ? -1 : 1;
14996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (input != o.input) {
15016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return input < o.input ? -1 : 1;
15026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenLayout != o.screenLayout) {
15046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return screenLayout < o.screenLayout ? -1 : 1;
15056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (uiMode != o.uiMode) {
15076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return uiMode < o.uiMode ? -1 : 1;
15086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (version != o.version) {
15106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return version < o.version ? -1 : 1;
15116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return 0;
15136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
15146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornint ResTable_config::diff(const ResTable_config& o) const {
15166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    int diffs = 0;
15176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mcc != o.mcc) diffs |= CONFIG_MCC;
15186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mnc != o.mnc) diffs |= CONFIG_MNC;
15196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (locale != o.locale) diffs |= CONFIG_LOCALE;
15206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
15216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (density != o.density) diffs |= CONFIG_DENSITY;
15226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
15236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
15246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            diffs |= CONFIG_KEYBOARD_HIDDEN;
15256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
15266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
15276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
15286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (version != o.version) diffs |= CONFIG_VERSION;
15296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenLayout != o.screenLayout) diffs |= CONFIG_SCREEN_LAYOUT;
15306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
15316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (smallestScreenWidthDp != o.smallestScreenWidthDp) diffs |= CONFIG_SMALLEST_SCREEN_SIZE;
15326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE;
15336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return diffs;
15346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
15356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornbool ResTable_config::isMoreSpecificThan(const ResTable_config& o) const {
15376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    // The order of the following tests defines the importance of one
15386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    // configuration parameter over another.  Those tests first are more
15396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    // important, trumping any values in those following them.
15406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (imsi || o.imsi) {
15416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (mcc != o.mcc) {
15426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!mcc) return false;
15436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.mcc) return true;
15446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (mnc != o.mnc) {
15476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!mnc) return false;
15486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.mnc) return true;
15496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (locale || o.locale) {
15536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (language[0] != o.language[0]) {
15546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!language[0]) return false;
15556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.language[0]) return true;
15566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (country[0] != o.country[0]) {
15596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!country[0]) return false;
15606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.country[0]) return true;
15616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15645f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    if (screenLayout || o.screenLayout) {
15655f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0) {
15665f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            if (!(screenLayout & MASK_LAYOUTDIR)) return false;
15675f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            if (!(o.screenLayout & MASK_LAYOUTDIR)) return true;
15685f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        }
15695f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    }
15705f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio
15716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
15726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
15736c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!smallestScreenWidthDp) return false;
15746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.smallestScreenWidthDp) return true;
15756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSizeDp || o.screenSizeDp) {
15796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenWidthDp != o.screenWidthDp) {
15806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!screenWidthDp) return false;
15816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.screenWidthDp) return true;
15826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenHeightDp != o.screenHeightDp) {
15856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!screenHeightDp) return false;
15866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.screenHeightDp) return true;
15876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
15896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
15906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenLayout || o.screenLayout) {
15916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
15926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(screenLayout & MASK_SCREENSIZE)) return false;
15936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
15946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
15966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(screenLayout & MASK_SCREENLONG)) return false;
15976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.screenLayout & MASK_SCREENLONG)) return true;
15986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
15996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (orientation != o.orientation) {
16026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (!orientation) return false;
16036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (!o.orientation) return true;
16046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (uiMode || o.uiMode) {
16076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
16086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
16096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
16106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
16126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
16136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
16146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    // density is never 'more specific'
16186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    // as the default just equals 160
16196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (touchscreen != o.touchscreen) {
16216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (!touchscreen) return false;
16226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (!o.touchscreen) return true;
16236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (input || o.input) {
16266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
16276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
16286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
16296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
16326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(inputFlags & MASK_NAVHIDDEN)) return false;
16336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
16346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (keyboard != o.keyboard) {
16376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!keyboard) return false;
16386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.keyboard) return true;
16396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (navigation != o.navigation) {
16426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!navigation) return false;
16436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.navigation) return true;
16446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSize || o.screenSize) {
16486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenWidth != o.screenWidth) {
16496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!screenWidth) return false;
16506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.screenWidth) return true;
16516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenHeight != o.screenHeight) {
16546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!screenHeight) return false;
16556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.screenHeight) return true;
16566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (version || o.version) {
16606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (sdkVersion != o.sdkVersion) {
16616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!sdkVersion) return false;
16626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.sdkVersion) return true;
16636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16656c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (minorVersion != o.minorVersion) {
16666c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!minorVersion) return false;
16676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (!o.minorVersion) return true;
16686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
16706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return false;
16716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
16726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16736c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornbool ResTable_config::isBetterThan(const ResTable_config& o,
16746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const ResTable_config* requested) const {
16756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (requested) {
16766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (imsi || o.imsi) {
16776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((mcc != o.mcc) && requested->mcc) {
16786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (mcc);
16796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
16806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((mnc != o.mnc) && requested->mnc) {
16826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (mnc);
16836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
16846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (locale || o.locale) {
16876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((language[0] != o.language[0]) && requested->language[0]) {
16886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (language[0]);
16896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
16906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((country[0] != o.country[0]) && requested->country[0]) {
16926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (country[0]);
16936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
16946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
16956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
16965f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        if (screenLayout || o.screenLayout) {
16975f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            if (((screenLayout^o.screenLayout) & MASK_LAYOUTDIR) != 0
16985f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                    && (requested->screenLayout & MASK_LAYOUTDIR)) {
16995f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                int myLayoutDir = screenLayout & MASK_LAYOUTDIR;
17005f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                int oLayoutDir = o.screenLayout & MASK_LAYOUTDIR;
17015f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                return (myLayoutDir > oLayoutDir);
17025f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            }
17035f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        }
17045f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio
17056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (smallestScreenWidthDp || o.smallestScreenWidthDp) {
17066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // The configuration closest to the actual size is best.
17076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // We assume that larger configs have already been filtered
17086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // out at this point.  That means we just want the largest one.
17095c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            if (smallestScreenWidthDp != o.smallestScreenWidthDp) {
17105c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                return smallestScreenWidthDp > o.smallestScreenWidthDp;
17115c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            }
17126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
17136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
17146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenSizeDp || o.screenSizeDp) {
17156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // "Better" is based on the sum of the difference between both
17166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // width and height from the requested dimensions.  We are
17176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // assuming the invalid configs (with smaller dimens) have
17186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // already been filtered.  Note that if a particular dimension
17196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // is unspecified, we will end up with a large value (the
17206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // difference between 0 and the requested dimension), which is
17216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // good since we will prefer a config that has specified a
17226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // dimension value.
17236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            int myDelta = 0, otherDelta = 0;
17246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (requested->screenWidthDp) {
17256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                myDelta += requested->screenWidthDp - screenWidthDp;
17266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                otherDelta += requested->screenWidthDp - o.screenWidthDp;
17276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (requested->screenHeightDp) {
17296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                myDelta += requested->screenHeightDp - screenHeightDp;
17306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                otherDelta += requested->screenHeightDp - o.screenHeightDp;
17316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
17336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //    screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
17346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //    requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
17355c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            if (myDelta != otherDelta) {
17365c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                return myDelta < otherDelta;
17375c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            }
17386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
17396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
17406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenLayout || o.screenLayout) {
17416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
17426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    && (requested->screenLayout & MASK_SCREENSIZE)) {
17436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // A little backwards compatibility here: undefined is
17446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // considered equivalent to normal.  But only if the
17456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // requested size is at least normal; otherwise, small
17466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // is better than the default.
17476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int mySL = (screenLayout & MASK_SCREENSIZE);
17486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int oSL = (o.screenLayout & MASK_SCREENSIZE);
17496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int fixedMySL = mySL;
17506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int fixedOSL = oSL;
17516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
17526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
17536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
17546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
17556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // For screen size, the best match is the one that is
17566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // closest to the requested screen size, but not over
17576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // (the not over part is dealt with in match() below).
17586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (fixedMySL == fixedOSL) {
17596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // If the two are the same, but 'this' is actually
17606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // undefined, then the other is really a better match.
17616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (mySL == 0) return false;
17626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    return true;
17636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
17645c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                if (fixedMySL != fixedOSL) {
17655c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                    return fixedMySL > fixedOSL;
17665c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                }
17676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
17696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    && (requested->screenLayout & MASK_SCREENLONG)) {
17706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (screenLayout & MASK_SCREENLONG);
17716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
17736c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
17746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if ((orientation != o.orientation) && requested->orientation) {
17756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return (orientation);
17766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
17776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
17786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (uiMode || o.uiMode) {
17796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
17806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    && (requested->uiMode & MASK_UI_MODE_TYPE)) {
17816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (uiMode & MASK_UI_MODE_TYPE);
17826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
17846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
17856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (uiMode & MASK_UI_MODE_NIGHT);
17866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
17876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
17886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
17896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenType || o.screenType) {
17906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (density != o.density) {
17916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // density is tough.  Any density is potentially useful
17926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // because the system will scale it.  Scaling down
17936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // is generally better than scaling up.
17946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // Default density counts as 160dpi (the system default)
17956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // TODO - remove 160 constants
17966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int h = (density?density:160);
17976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int l = (o.density?o.density:160);
17986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                bool bImBigger = true;
17996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (l > h) {
18006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    int t = h;
18016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    h = l;
18026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    l = t;
18036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    bImBigger = false;
18046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                int reqValue = (requested->density?requested->density:160);
18076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (reqValue >= h) {
18086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // requested value higher than both l and h, give h
18096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    return bImBigger;
18106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (l >= reqValue) {
18126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // requested value lower than both l and h, give l
18136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    return !bImBigger;
18146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                // saying that scaling down is 2x better than up
18166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (((2 * l) - reqValue) * h > reqValue * reqValue) {
18176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    return !bImBigger;
18186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                } else {
18196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    return bImBigger;
18206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((touchscreen != o.touchscreen) && requested->touchscreen) {
18246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (touchscreen);
18256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
18276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (input || o.input) {
18296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
18306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
18316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (keysHidden != oKeysHidden) {
18326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                const int reqKeysHidden =
18336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                        requested->inputFlags & MASK_KEYSHIDDEN;
18346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (reqKeysHidden) {
18356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (!keysHidden) return false;
18376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (!oKeysHidden) return true;
18386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // For compatibility, we count KEYSHIDDEN_NO as being
18396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // the same as KEYSHIDDEN_SOFT.  Here we disambiguate
18406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    // these by making an exact match more specific.
18416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (reqKeysHidden == keysHidden) return true;
18426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (reqKeysHidden == oKeysHidden) return false;
18436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            const int navHidden = inputFlags & MASK_NAVHIDDEN;
18476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
18486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (navHidden != oNavHidden) {
18496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                const int reqNavHidden =
18506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                        requested->inputFlags & MASK_NAVHIDDEN;
18516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                if (reqNavHidden) {
18526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (!navHidden) return false;
18546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    if (!oNavHidden) return true;
18556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                }
18566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((keyboard != o.keyboard) && requested->keyboard) {
18596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (keyboard);
18606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((navigation != o.navigation) && requested->navigation) {
18636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (navigation);
18646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18656c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
18666c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenSize || o.screenSize) {
18686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // "Better" is based on the sum of the difference between both
18696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // width and height from the requested dimensions.  We are
18706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // assuming the invalid configs (with smaller sizes) have
18716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // already been filtered.  Note that if a particular dimension
18726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // is unspecified, we will end up with a large value (the
18736c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // difference between 0 and the requested dimension), which is
18746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // good since we will prefer a config that has specified a
18756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // size value.
18766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            int myDelta = 0, otherDelta = 0;
18776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (requested->screenWidth) {
18786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                myDelta += requested->screenWidth - screenWidth;
18796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                otherDelta += requested->screenWidth - o.screenWidth;
18806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (requested->screenHeight) {
18826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                myDelta += requested->screenHeight - screenHeight;
18836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                otherDelta += requested->screenHeight - o.screenHeight;
18846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18855c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            if (myDelta != otherDelta) {
18865c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn                return myDelta < otherDelta;
18875c6dfeb1fb47102ffce415ea7898b33dd1290d54Dianne Hackborn            }
18886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
18896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (version || o.version) {
18916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
18926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (sdkVersion > o.sdkVersion);
18936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
18956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if ((minorVersion != o.minorVersion) &&
18966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                    requested->minorVersion) {
18976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return (minorVersion);
18986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
18996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        return false;
19026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return isMoreSpecificThan(o);
19046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
19056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornbool ResTable_config::match(const ResTable_config& settings) const {
19076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (imsi != 0) {
19086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (mcc != 0 && mcc != settings.mcc) {
19096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (mnc != 0 && mnc != settings.mnc) {
19126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (locale != 0) {
19166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (language[0] != 0
19176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            && (language[0] != settings.language[0]
19186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                || language[1] != settings.language[1])) {
19196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (country[0] != 0
19226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            && (country[0] != settings.country[0]
19236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                || country[1] != settings.country[1])) {
19246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenConfig != 0) {
19285f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        const int layoutDir = screenLayout&MASK_LAYOUTDIR;
19295f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        const int setLayoutDir = settings.screenLayout&MASK_LAYOUTDIR;
19305f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        if (layoutDir != 0 && layoutDir != setLayoutDir) {
19315f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            return false;
19325f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        }
19335f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio
19346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int screenSize = screenLayout&MASK_SCREENSIZE;
19356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
19366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        // Any screen sizes for larger screens than the setting do not
19376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        // match.
19386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenSize != 0 && screenSize > setScreenSize) {
19396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int screenLong = screenLayout&MASK_SCREENLONG;
19436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
19446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenLong != 0 && screenLong != setScreenLong) {
19456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
19496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
19506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (uiModeType != 0 && uiModeType != setUiModeType) {
19516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
19556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
19566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (uiModeNight != 0 && uiModeNight != setUiModeNight) {
19576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
19606c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (smallestScreenWidthDp != 0
19616c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                && smallestScreenWidthDp > settings.smallestScreenWidthDp) {
19626c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19636c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19646c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19656c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSizeDp != 0) {
19666c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) {
19676c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //ALOGI("Filtering out width %d in requested %d", screenWidthDp, settings.screenWidthDp);
19686c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19696c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19706c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) {
19716c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //ALOGI("Filtering out height %d in requested %d", screenHeightDp, settings.screenHeightDp);
19726c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19736c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19746c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenType != 0) {
19766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (orientation != 0 && orientation != settings.orientation) {
19776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        // density always matches - we can scale it.  See isBetterThan
19806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (touchscreen != 0 && touchscreen != settings.touchscreen) {
19816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
19826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
19846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (input != 0) {
19856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
19866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
19876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (keysHidden != 0 && keysHidden != setKeysHidden) {
19886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // For compatibility, we count a request for KEYSHIDDEN_NO as also
19896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // matching the more recent KEYSHIDDEN_SOFT.  Basically
19906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            // KEYSHIDDEN_NO means there is some kind of keyboard available.
19916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            //ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
19926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
19936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                //ALOGI("No match!");
19946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                return false;
19956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            }
19966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
19976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int navHidden = inputFlags&MASK_NAVHIDDEN;
19986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
19996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (navHidden != 0 && navHidden != setNavHidden) {
20006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (keyboard != 0 && keyboard != settings.keyboard) {
20036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (navigation != 0 && navigation != settings.navigation) {
20066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenSize != 0) {
20106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenWidth != 0 && screenWidth > settings.screenWidth) {
20116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (screenHeight != 0 && screenHeight > settings.screenHeight) {
20146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (version != 0) {
20186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (sdkVersion != 0 && sdkVersion > settings.sdkVersion) {
20196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (minorVersion != 0 && minorVersion != settings.minorVersion) {
20226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            return false;
20236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    return true;
20266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
20276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
20286c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackbornvoid ResTable_config::getLocale(char str[6]) const {
20296c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    memset(str, 0, 6);
20306c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (language[0]) {
20316c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        str[0] = language[0];
20326c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        str[1] = language[1];
20336c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (country[0]) {
20346c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            str[2] = '_';
20356c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            str[3] = country[0];
20366c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            str[4] = country[1];
20376c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
20386c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20396c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn}
20406c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
20416c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne HackbornString8 ResTable_config::toString() const {
20426c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    String8 res;
20436c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
20446c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mcc != 0) {
20456c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20466c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.appendFormat("%dmcc", dtohs(mcc));
20476c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20486c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (mnc != 0) {
20496c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20506c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.appendFormat("%dmnc", dtohs(mnc));
20516c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20526c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (language[0] != 0) {
20536c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20546c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.append(language, 2);
20556c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20566c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (country[0] != 0) {
20576c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20586c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.append(country, 2);
20596c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20605f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    if ((screenLayout&MASK_LAYOUTDIR) != 0) {
20615f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        if (res.size() > 0) res.append("-");
20625f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        switch (screenLayout&ResTable_config::MASK_LAYOUTDIR) {
20635f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            case ResTable_config::LAYOUTDIR_LTR:
20648a802dbdabdfd27692c2e38b2c3adafe95566106Fabrice Di Meglio                res.append("ldltr");
20655f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                break;
20665f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            case ResTable_config::LAYOUTDIR_RTL:
20678a802dbdabdfd27692c2e38b2c3adafe95566106Fabrice Di Meglio                res.append("ldrtl");
20685f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                break;
20695f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio            default:
20705f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                res.appendFormat("layoutDir=%d",
20715f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                        dtohs(screenLayout&ResTable_config::MASK_LAYOUTDIR));
20725f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio                break;
20735f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio        }
20745f7979993979466c79ab4f38d83c6f2aca361662Fabrice Di Meglio    }
20756c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (smallestScreenWidthDp != 0) {
20766c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20776c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.appendFormat("sw%ddp", dtohs(smallestScreenWidthDp));
20786c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20796c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenWidthDp != 0) {
20806c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20816c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.appendFormat("w%ddp", dtohs(screenWidthDp));
20826c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (screenHeightDp != 0) {
20846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        res.appendFormat("h%ddp", dtohs(screenHeightDp));
20866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
20876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if ((screenLayout&MASK_SCREENSIZE) != SCREENSIZE_ANY) {
20886c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
20896c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        switch (screenLayout&ResTable_config::MASK_SCREENSIZE) {
20906c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENSIZE_SMALL:
20916c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("small");
20926c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
20936c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENSIZE_NORMAL:
20946c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("normal");
20956c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
20966c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENSIZE_LARGE:
20976c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("large");
20986c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
20996c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENSIZE_XLARGE:
21006c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("xlarge");
21016c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
21026c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            default:
21036c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.appendFormat("screenLayoutSize=%d",
21046c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                        dtohs(screenLayout&ResTable_config::MASK_SCREENSIZE));
21056c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
21066c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
21076c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
21086c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if ((screenLayout&MASK_SCREENLONG) != 0) {
21096c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
21106c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        switch (screenLayout&ResTable_config::MASK_SCREENLONG) {
21116c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENLONG_NO:
21126c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("notlong");
21136c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
21146c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::SCREENLONG_YES:
21156c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("long");
21166c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
21176c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            default:
21186c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.appendFormat("screenLayoutLong=%d",
21196c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                        dtohs(screenLayout&ResTable_config::MASK_SCREENLONG));
21206c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                break;
21216c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        }
21226c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    }
21236c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    if (orientation != ORIENTATION_ANY) {
21246c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        if (res.size() > 0) res.append("-");
21256c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        switch (orientation) {
21266c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn            case ResTable_config::ORIENTATION_PORT:
21276c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn                res.append("port");
21286c997a9e880e08c354ffd809bd62df9e25e9c4d4