blob: b983a53f427376f384a4c9925b9da4ad3e7d42c3 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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#include "BigBuffer.h"
18#include "StringPiece.h"
19#include "StringPool.h"
20#include "Util.h"
21
22#include <algorithm>
23#include <androidfw/ResourceTypes.h>
24#include <memory>
25#include <string>
26
27namespace aapt {
28
29StringPool::Ref::Ref() : mEntry(nullptr) {
30}
31
32StringPool::Ref::Ref(const StringPool::Ref& rhs) : mEntry(rhs.mEntry) {
33 if (mEntry != nullptr) {
34 mEntry->ref++;
35 }
36}
37
38StringPool::Ref::Ref(StringPool::Entry* entry) : mEntry(entry) {
39 if (mEntry != nullptr) {
40 mEntry->ref++;
41 }
42}
43
44StringPool::Ref::~Ref() {
45 if (mEntry != nullptr) {
46 mEntry->ref--;
47 }
48}
49
50StringPool::Ref& StringPool::Ref::operator=(const StringPool::Ref& rhs) {
51 if (rhs.mEntry != nullptr) {
52 rhs.mEntry->ref++;
53 }
54
55 if (mEntry != nullptr) {
56 mEntry->ref--;
57 }
58 mEntry = rhs.mEntry;
59 return *this;
60}
61
62const std::u16string* StringPool::Ref::operator->() const {
63 return &mEntry->value;
64}
65
66const std::u16string& StringPool::Ref::operator*() const {
67 return mEntry->value;
68}
69
70size_t StringPool::Ref::getIndex() const {
71 return mEntry->index;
72}
73
74const StringPool::Context& StringPool::Ref::getContext() const {
75 return mEntry->context;
76}
77
78StringPool::StyleRef::StyleRef() : mEntry(nullptr) {
79}
80
81StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs) : mEntry(rhs.mEntry) {
82 if (mEntry != nullptr) {
83 mEntry->ref++;
84 }
85}
86
87StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : mEntry(entry) {
88 if (mEntry != nullptr) {
89 mEntry->ref++;
90 }
91}
92
93StringPool::StyleRef::~StyleRef() {
94 if (mEntry != nullptr) {
95 mEntry->ref--;
96 }
97}
98
99StringPool::StyleRef& StringPool::StyleRef::operator=(const StringPool::StyleRef& rhs) {
100 if (rhs.mEntry != nullptr) {
101 rhs.mEntry->ref++;
102 }
103
104 if (mEntry != nullptr) {
105 mEntry->ref--;
106 }
107 mEntry = rhs.mEntry;
108 return *this;
109}
110
111const StringPool::StyleEntry* StringPool::StyleRef::operator->() const {
112 return mEntry;
113}
114
115const StringPool::StyleEntry& StringPool::StyleRef::operator*() const {
116 return *mEntry;
117}
118
119size_t StringPool::StyleRef::getIndex() const {
120 return mEntry->str.getIndex();
121}
122
123const StringPool::Context& StringPool::StyleRef::getContext() const {
124 return mEntry->str.getContext();
125}
126
127StringPool::Ref StringPool::makeRef(const StringPiece16& str) {
128 return makeRefImpl(str, Context{}, true);
129}
130
131StringPool::Ref StringPool::makeRef(const StringPiece16& str, const Context& context) {
132 return makeRefImpl(str, context, true);
133}
134
135StringPool::Ref StringPool::makeRefImpl(const StringPiece16& str, const Context& context,
136 bool unique) {
137 if (unique) {
138 auto iter = mIndexedStrings.find(str);
139 if (iter != std::end(mIndexedStrings)) {
140 return Ref(iter->second);
141 }
142 }
143
144 Entry* entry = new Entry();
145 entry->value = str.toString();
146 entry->context = context;
147 entry->index = mStrings.size();
148 entry->ref = 0;
149 mStrings.emplace_back(entry);
150 mIndexedStrings.insert(std::make_pair(StringPiece16(entry->value), entry));
151 return Ref(entry);
152}
153
154StringPool::StyleRef StringPool::makeRef(const StyleString& str) {
155 return makeRef(str, Context{});
156}
157
158StringPool::StyleRef StringPool::makeRef(const StyleString& str, const Context& context) {
159 Entry* entry = new Entry();
160 entry->value = str.str;
161 entry->context = context;
162 entry->index = mStrings.size();
163 entry->ref = 0;
164 mStrings.emplace_back(entry);
165 mIndexedStrings.insert(std::make_pair(StringPiece16(entry->value), entry));
166
167 StyleEntry* styleEntry = new StyleEntry();
168 styleEntry->str = Ref(entry);
169 for (const aapt::Span& span : str.spans) {
170 styleEntry->spans.emplace_back(Span{makeRef(span.name),
171 span.firstChar, span.lastChar});
172 }
173 styleEntry->ref = 0;
174 mStyles.emplace_back(styleEntry);
175 return StyleRef(styleEntry);
176}
177
Adam Lesinski769de982015-04-10 19:43:55 -0700178StringPool::StyleRef StringPool::makeRef(const StyleRef& ref) {
179 Entry* entry = new Entry();
180 entry->value = *ref.mEntry->str;
181 entry->context = ref.mEntry->str.mEntry->context;
182 entry->index = mStrings.size();
183 entry->ref = 0;
184 mStrings.emplace_back(entry);
185 mIndexedStrings.insert(std::make_pair(StringPiece16(entry->value), entry));
186
187 StyleEntry* styleEntry = new StyleEntry();
188 styleEntry->str = Ref(entry);
189 for (const Span& span : ref.mEntry->spans) {
190 styleEntry->spans.emplace_back(Span{ makeRef(*span.name), span.firstChar, span.lastChar });
191 }
192 styleEntry->ref = 0;
193 mStyles.emplace_back(styleEntry);
194 return StyleRef(styleEntry);
195}
196
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197void StringPool::merge(StringPool&& pool) {
198 mIndexedStrings.insert(pool.mIndexedStrings.begin(), pool.mIndexedStrings.end());
199 pool.mIndexedStrings.clear();
200 std::move(pool.mStrings.begin(), pool.mStrings.end(), std::back_inserter(mStrings));
201 pool.mStrings.clear();
202 std::move(pool.mStyles.begin(), pool.mStyles.end(), std::back_inserter(mStyles));
203 pool.mStyles.clear();
204
205 // Assign the indices.
206 const size_t len = mStrings.size();
207 for (size_t index = 0; index < len; index++) {
208 mStrings[index]->index = index;
209 }
210}
211
212void StringPool::hintWillAdd(size_t stringCount, size_t styleCount) {
213 mStrings.reserve(mStrings.size() + stringCount);
214 mStyles.reserve(mStyles.size() + styleCount);
215}
216
217void StringPool::prune() {
218 const auto iterEnd = std::end(mIndexedStrings);
219 auto indexIter = std::begin(mIndexedStrings);
220 while (indexIter != iterEnd) {
221 if (indexIter->second->ref <= 0) {
222 mIndexedStrings.erase(indexIter++);
223 } else {
224 ++indexIter;
225 }
226 }
227
228 auto endIter2 = std::remove_if(std::begin(mStrings), std::end(mStrings),
229 [](const std::unique_ptr<Entry>& entry) -> bool {
230 return entry->ref <= 0;
231 }
232 );
233
234 auto endIter3 = std::remove_if(std::begin(mStyles), std::end(mStyles),
235 [](const std::unique_ptr<StyleEntry>& entry) -> bool {
236 return entry->ref <= 0;
237 }
238 );
239
240 // Remove the entries at the end or else we'll be accessing
241 // a deleted string from the StyleEntry.
242 mStrings.erase(endIter2, std::end(mStrings));
243 mStyles.erase(endIter3, std::end(mStyles));
244}
245
246void StringPool::sort(const std::function<bool(const Entry&, const Entry&)>& cmp) {
247 std::sort(std::begin(mStrings), std::end(mStrings),
248 [&cmp](const std::unique_ptr<Entry>& a, const std::unique_ptr<Entry>& b) -> bool {
249 return cmp(*a, *b);
250 }
251 );
252
253 // Assign the indices.
254 const size_t len = mStrings.size();
255 for (size_t index = 0; index < len; index++) {
256 mStrings[index]->index = index;
257 }
258
259 // Reorder the styles.
260 std::sort(std::begin(mStyles), std::end(mStyles),
261 [](const std::unique_ptr<StyleEntry>& lhs,
262 const std::unique_ptr<StyleEntry>& rhs) -> bool {
263 return lhs->str.getIndex() < rhs->str.getIndex();
264 }
265 );
266}
267
268static uint8_t* encodeLength(uint8_t* data, size_t length) {
269 if (length > 0x7fu) {
270 *data++ = 0x80u | (0x000000ffu & (length >> 8));
271 }
272 *data++ = 0x000000ffu & length;
273 return data;
274}
275
276static size_t encodedLengthByteCount(size_t length) {
277 return length > 0x7fu ? 2 : 1;
278}
279
280bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) {
281 const size_t startIndex = out->size();
282 android::ResStringPool_header* header = out->nextBlock<android::ResStringPool_header>();
283 header->header.type = android::RES_STRING_POOL_TYPE;
284 header->header.headerSize = sizeof(*header);
285 header->stringCount = pool.size();
286 header->flags |= android::ResStringPool_header::UTF8_FLAG;
287
Adam Lesinski769de982015-04-10 19:43:55 -0700288 uint32_t* indices = pool.size() != 0 ? out->nextBlock<uint32_t>(pool.size()) : nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
290 uint32_t* styleIndices = nullptr;
291 if (!pool.mStyles.empty()) {
292 header->styleCount = pool.mStyles.back()->str.getIndex() + 1;
293 styleIndices = out->nextBlock<uint32_t>(header->styleCount);
294 }
295
296 const size_t beforeStringsIndex = out->size();
297 header->stringsStart = beforeStringsIndex - startIndex;
298
299 for (const auto& entry : pool) {
300 *indices = out->size() - beforeStringsIndex;
301 indices++;
302
303 std::string encoded = util::utf16ToUtf8(entry->value);
304
305 const size_t stringByteLength = sizeof(char) * encoded.length();
306 const size_t totalSize = encodedLengthByteCount(entry->value.size())
307 + encodedLengthByteCount(encoded.length())
308 + stringByteLength
309 + sizeof(char);
310
311 uint8_t* data = out->nextBlock<uint8_t>(totalSize);
312
313 // First encode the actual UTF16 string length.
314 data = encodeLength(data, entry->value.size());
315
316 // Now encode the size of the converted UTF8 string.
317 data = encodeLength(data, encoded.length());
318
319 memcpy(data, encoded.data(), stringByteLength);
320 data += stringByteLength;
321 *data = 0;
322 }
323
324 out->align4();
325
326 if (!pool.mStyles.empty()) {
327 const size_t beforeStylesIndex = out->size();
328 header->stylesStart = beforeStylesIndex - startIndex;
329
330 size_t currentIndex = 0;
331 for (const auto& entry : pool.mStyles) {
332 while (entry->str.getIndex() > currentIndex) {
333 styleIndices[currentIndex++] = out->size() - beforeStylesIndex;
334
335 uint32_t* spanOffset = out->nextBlock<uint32_t>();
336 *spanOffset = android::ResStringPool_span::END;
337 }
338 styleIndices[currentIndex++] = out->size() - beforeStylesIndex;
339
340 android::ResStringPool_span* span =
341 out->nextBlock<android::ResStringPool_span>(entry->spans.size());
342 for (const auto& s : entry->spans) {
343 span->name.index = s.name.getIndex();
344 span->firstChar = s.firstChar;
345 span->lastChar = s.lastChar;
346 span++;
347 }
348
349 uint32_t* spanEnd = out->nextBlock<uint32_t>();
350 *spanEnd = android::ResStringPool_span::END;
351 }
352
353 // The error checking code in the platform looks for an entire
354 // ResStringPool_span structure worth of 0xFFFFFFFF at the end
355 // of the style block, so fill in the remaining 2 32bit words
356 // with 0xFFFFFFFF.
357 const size_t paddingLength = sizeof(android::ResStringPool_span)
358 - sizeof(android::ResStringPool_span::name);
359 uint8_t* padding = out->nextBlock<uint8_t>(paddingLength);
360 memset(padding, 0xff, paddingLength);
361 out->align4();
362 }
363 header->header.size = out->size() - startIndex;
364 return true;
365}
366
367} // namespace aapt