blob: a0ffeb3ebfbec9d8ed28d0c7868de589b2f1ff70 [file] [log] [blame]
Adam Lesinskia7d1d732014-10-01 18:24:54 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef H_ATTRIBUTE_FINDER
18#define H_ATTRIBUTE_FINDER
19
20#include <stdint.h>
21#include <utils/KeyedVector.h>
22
23namespace android {
24
25static inline uint32_t getPackage(uint32_t attr) {
26 return attr >> 24;
27}
28
29/**
30 * A helper class to search linearly for the requested
31 * attribute, maintaining it's position and optimizing for
32 * the case that subsequent searches will involve an attribute with
33 * a higher attribute ID.
34 *
35 * In the case that a subsequent attribute has a different package ID,
36 * its resource ID may not be larger than the preceding search, so
37 * back tracking is supported for this case. This
38 * back tracking requirement is mainly for shared library
39 * resources, whose package IDs get assigned at runtime
40 * and thus attributes from a shared library may
41 * be out of order.
42 *
43 * We make two assumptions about the order of attributes:
44 * 1) The input has the same sorting rules applied to it as
45 * the attribute data contained by this class.
46 * 2) Attributes are grouped by package ID.
47 * 3) Among attributes with the same package ID, the attributes are
48 * sorted by increasing resource ID.
49 *
50 * Ex: 02010000, 02010001, 010100f4, 010100f5, 0x7f010001, 07f010003
51 *
52 * The total order of attributes (including package ID) can not be linear
53 * as shared libraries get assigned dynamic package IDs at runtime, which
54 * may break the sort order established at build time.
55 */
56template <typename Derived, typename Iterator>
57class BackTrackingAttributeFinder {
58public:
59 BackTrackingAttributeFinder(const Iterator& begin, const Iterator& end);
60
61 Iterator find(uint32_t attr);
62
63private:
64 void jumpToClosestAttribute(uint32_t packageId);
65 void markCurrentPackageId(uint32_t packageId);
66
67 Iterator mBegin;
68 Iterator mEnd;
69 Iterator mCurrent;
70 Iterator mLargest;
71 uint32_t mLastPackageId;
72 uint32_t mCurrentAttr;
73
74 // Package Offsets (best-case, fast look-up).
75 Iterator mFrameworkStart;
76 Iterator mAppStart;
77
78 // Worst case, we have shared-library resources.
79 KeyedVector<uint32_t, Iterator> mPackageOffsets;
80};
81
82template <typename Derived, typename Iterator> inline
83BackTrackingAttributeFinder<Derived, Iterator>::BackTrackingAttributeFinder(const Iterator& begin, const Iterator& end)
84 : mBegin(begin)
85 , mEnd(end)
86 , mCurrent(begin)
87 , mLargest(begin)
88 , mLastPackageId(0)
89 , mCurrentAttr(0)
90 , mFrameworkStart(end)
91 , mAppStart(end) {
92}
93
94template <typename Derived, typename Iterator>
95void BackTrackingAttributeFinder<Derived, Iterator>::jumpToClosestAttribute(const uint32_t packageId) {
96 switch (packageId) {
97 case 0x01:
98 mCurrent = mFrameworkStart;
99 break;
100 case 0x7f:
101 mCurrent = mAppStart;
102 break;
103 default: {
104 ssize_t idx = mPackageOffsets.indexOfKey(packageId);
105 if (idx >= 0) {
106 // We have seen this package ID before, so jump to the first
107 // attribute with this package ID.
108 mCurrent = mPackageOffsets[idx];
109 } else {
110 mCurrent = mEnd;
111 }
112 break;
113 }
114 }
115
116 // We have never seen this package ID yet, so jump to the
117 // latest/largest index we have processed so far.
118 if (mCurrent == mEnd) {
119 mCurrent = mLargest;
120 }
121
122 if (mCurrent != mEnd) {
123 mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mCurrent);
124 }
125}
126
127template <typename Derived, typename Iterator>
128void BackTrackingAttributeFinder<Derived, Iterator>::markCurrentPackageId(const uint32_t packageId) {
129 switch (packageId) {
130 case 0x01:
131 mFrameworkStart = mCurrent;
132 break;
133 case 0x7f:
134 mAppStart = mCurrent;
135 break;
136 default:
137 mPackageOffsets.add(packageId, mCurrent);
138 break;
139 }
140}
141
142template <typename Derived, typename Iterator>
143Iterator BackTrackingAttributeFinder<Derived, Iterator>::find(uint32_t attr) {
144 if (!(mBegin < mEnd)) {
145 return mEnd;
146 }
147
148 if (mCurrentAttr == 0) {
149 // One-time initialization.
150 mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mBegin);
151 mLastPackageId = getPackage(mCurrentAttr);
152 markCurrentPackageId(mLastPackageId);
153 }
154
155 // Looking for the needle (attribute we're looking for)
156 // in the haystack (the attributes we're searching through)
157 const uint32_t needlePackageId = getPackage(attr);
158 if (mLastPackageId != needlePackageId) {
159 jumpToClosestAttribute(needlePackageId);
160 mLastPackageId = needlePackageId;
161 }
162
163 // Walk through the xml attributes looking for the requested attribute.
164 while (mCurrent != mEnd) {
165 const uint32_t haystackPackageId = getPackage(mCurrentAttr);
166 if (needlePackageId == haystackPackageId && attr < mCurrentAttr) {
167 // The attribute we are looking was not found.
168 break;
169 }
170 const uint32_t prevAttr = mCurrentAttr;
171
172 // Move to the next attribute in the XML.
173 ++mCurrent;
174 if (mCurrent != mEnd) {
175 mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mCurrent);
176 const uint32_t newHaystackPackageId = getPackage(mCurrentAttr);
177 if (haystackPackageId != newHaystackPackageId) {
178 // We've moved to the next group of attributes
179 // with a new package ID, so we should record
180 // the offset of this new package ID.
181 markCurrentPackageId(newHaystackPackageId);
182 }
183 }
184
185 if (mCurrent > mLargest) {
186 // We've moved past the latest attribute we've
187 // seen.
188 mLargest = mCurrent;
189 }
190
191 if (attr == prevAttr) {
192 // We found the attribute we were looking for.
193 return mCurrent - 1;
194 }
195 }
196 return mEnd;
197}
198
199} // namespace android
200
201#endif // H_ATTRIBUTE_FINDER