blob: 57da5f01dcd1924291297ac4e3d6ede745792e35 [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
Adam Lesinski52364f72016-01-11 13:10:24 -080017#include "StringPool.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <memory>
21#include <string>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "android-base/logging.h"
24#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
27#include "util/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "util/Util.h"
29
Adam Lesinskid5083f62017-01-16 15:07:21 -080030using android::StringPiece;
31
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034StringPool::Ref::Ref() : entry_(nullptr) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036StringPool::Ref::Ref(const StringPool::Ref& rhs) : entry_(rhs.entry_) {
37 if (entry_ != nullptr) {
38 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040}
41
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042StringPool::Ref::Ref(StringPool::Entry* entry) : entry_(entry) {
43 if (entry_ != nullptr) {
44 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046}
47
48StringPool::Ref::~Ref() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 if (entry_ != nullptr) {
50 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052}
53
54StringPool::Ref& StringPool::Ref::operator=(const StringPool::Ref& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 if (rhs.entry_ != nullptr) {
56 rhs.entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 if (entry_ != nullptr) {
60 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 entry_ = rhs.entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064}
65
Adam Lesinski75421622017-01-06 15:20:04 -080066bool StringPool::Ref::operator==(const Ref& rhs) const {
67 return entry_->value == rhs.entry_->value;
68}
69
70bool StringPool::Ref::operator!=(const Ref& rhs) const {
71 return entry_->value != rhs.entry_->value;
72}
73
Adam Lesinskid0f116b2016-07-08 15:00:32 -070074const std::string* StringPool::Ref::operator->() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 return &entry_->value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076}
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078const std::string& StringPool::Ref::operator*() const { return entry_->value; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080size_t StringPool::Ref::index() const { return entry_->index; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082const StringPool::Context& StringPool::Ref::GetContext() const {
83 return entry_->context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086StringPool::StyleRef::StyleRef() : entry_(nullptr) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 : entry_(rhs.entry_) {
90 if (entry_ != nullptr) {
91 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093}
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : entry_(entry) {
96 if (entry_ != nullptr) {
97 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099}
100
101StringPool::StyleRef::~StyleRef() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 if (entry_ != nullptr) {
103 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105}
106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107StringPool::StyleRef& StringPool::StyleRef::operator=(
108 const StringPool::StyleRef& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (rhs.entry_ != nullptr) {
110 rhs.entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (entry_ != nullptr) {
114 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 entry_ = rhs.entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118}
119
Adam Lesinski75421622017-01-06 15:20:04 -0800120bool StringPool::StyleRef::operator==(const StyleRef& rhs) const {
121 if (entry_->str != rhs.entry_->str) {
122 return false;
123 }
124
125 if (entry_->spans.size() != rhs.entry_->spans.size()) {
126 return false;
127 }
128
129 auto rhs_iter = rhs.entry_->spans.begin();
130 for (const Span& span : entry_->spans) {
131 const Span& rhs_span = *rhs_iter;
132 if (span.first_char != rhs_span.first_char || span.last_char != rhs_span.last_char ||
133 span.name != rhs_span.name) {
134 return false;
135 }
136 }
137 return true;
138}
139
140bool StringPool::StyleRef::operator!=(const StyleRef& rhs) const { return !operator==(rhs); }
141
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142const StringPool::StyleEntry* StringPool::StyleRef::operator->() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 return entry_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144}
145
146const StringPool::StyleEntry& StringPool::StyleRef::operator*() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 return *entry_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148}
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150size_t StringPool::StyleRef::index() const { return entry_->str.index(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152const StringPool::Context& StringPool::StyleRef::GetContext() const {
153 return entry_->str.GetContext();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154}
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156StringPool::Ref StringPool::MakeRef(const StringPiece& str) {
157 return MakeRefImpl(str, Context{}, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158}
159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160StringPool::Ref StringPool::MakeRef(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 const Context& context) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 return MakeRefImpl(str, context, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800163}
164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 const Context& context, bool unique) {
167 if (unique) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 auto iter = indexed_strings_.find(str);
169 if (iter != std::end(indexed_strings_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return Ref(iter->second);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 Entry* entry = new Entry();
Adam Lesinskid5083f62017-01-16 15:07:21 -0800175 entry->value = str.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 entry->context = context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 entry->index = strings_.size();
178 entry->ref_ = 0;
179 strings_.emplace_back(entry);
180 indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 return Ref(entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182}
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184StringPool::StyleRef StringPool::MakeRef(const StyleString& str) {
185 return MakeRef(str, Context{});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186}
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188StringPool::StyleRef StringPool::MakeRef(const StyleString& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 const Context& context) {
190 Entry* entry = new Entry();
191 entry->value = str.str;
192 entry->context = context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 entry->index = strings_.size();
194 entry->ref_ = 0;
195 strings_.emplace_back(entry);
196 indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 StyleEntry* style_entry = new StyleEntry();
199 style_entry->str = Ref(entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 for (const aapt::Span& span : str.spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 style_entry->spans.emplace_back(
202 Span{MakeRef(span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 style_entry->ref_ = 0;
205 styles_.emplace_back(style_entry);
206 return StyleRef(style_entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207}
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 Entry* entry = new Entry();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 entry->value = *ref.entry_->str;
212 entry->context = ref.entry_->str.entry_->context;
213 entry->index = strings_.size();
214 entry->ref_ = 0;
215 strings_.emplace_back(entry);
216 indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
Adam Lesinski769de982015-04-10 19:43:55 -0700217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 StyleEntry* style_entry = new StyleEntry();
219 style_entry->str = Ref(entry);
220 for (const Span& span : ref.entry_->spans) {
221 style_entry->spans.emplace_back(
222 Span{MakeRef(*span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 style_entry->ref_ = 0;
225 styles_.emplace_back(style_entry);
226 return StyleRef(style_entry);
Adam Lesinski769de982015-04-10 19:43:55 -0700227}
228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229void StringPool::Merge(StringPool&& pool) {
230 indexed_strings_.insert(pool.indexed_strings_.begin(),
231 pool.indexed_strings_.end());
232 pool.indexed_strings_.clear();
233 std::move(pool.strings_.begin(), pool.strings_.end(),
234 std::back_inserter(strings_));
235 pool.strings_.clear();
236 std::move(pool.styles_.begin(), pool.styles_.end(),
237 std::back_inserter(styles_));
238 pool.styles_.clear();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 // Assign the indices.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 const size_t len = strings_.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 for (size_t index = 0; index < len; index++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 strings_[index]->index = index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245}
246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247void StringPool::HintWillAdd(size_t stringCount, size_t styleCount) {
248 strings_.reserve(strings_.size() + stringCount);
249 styles_.reserve(styles_.size() + styleCount);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250}
251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252void StringPool::Prune() {
253 const auto iter_end = indexed_strings_.end();
254 auto index_iter = indexed_strings_.begin();
255 while (index_iter != iter_end) {
256 if (index_iter->second->ref_ <= 0) {
257 index_iter = indexed_strings_.erase(index_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 ++index_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 auto end_iter2 =
264 std::remove_if(strings_.begin(), strings_.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 [](const std::unique_ptr<Entry>& entry) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 return entry->ref_ <= 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 auto end_iter3 =
270 std::remove_if(styles_.begin(), styles_.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 [](const std::unique_ptr<StyleEntry>& entry) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 return entry->ref_ <= 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 // Remove the entries at the end or else we'll be accessing
276 // a deleted string from the StyleEntry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 strings_.erase(end_iter2, strings_.end());
278 styles_.erase(end_iter3, styles_.end());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // Reassign the indices.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 const size_t len = strings_.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 for (size_t index = 0; index < len; index++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 strings_[index]->index = index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285}
286
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287void StringPool::Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 const std::function<bool(const Entry&, const Entry&)>& cmp) {
289 std::sort(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 strings_.begin(), strings_.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 [&cmp](const std::unique_ptr<Entry>& a,
292 const std::unique_ptr<Entry>& b) -> bool { return cmp(*a, *b); });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 // Assign the indices.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 const size_t len = strings_.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 for (size_t index = 0; index < len; index++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 strings_[index]->index = index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 // Reorder the styles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 std::sort(styles_.begin(), styles_.end(),
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302 [](const std::unique_ptr<StyleEntry>& lhs,
303 const std::unique_ptr<StyleEntry>& rhs) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 return lhs->str.index() < rhs->str.index();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306}
307
Adam Lesinski24aad162015-04-24 19:19:30 -0700308template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309static T* EncodeLength(T* data, size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700311
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
313 constexpr size_t kMaxSize = kMask - 1;
314 if (length > kMaxSize) {
315 *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8)));
316 }
317 *data++ = length;
318 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319}
320
Adam Lesinski24aad162015-04-24 19:19:30 -0700321template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322static size_t EncodedLengthUnits(size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700324
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
326 constexpr size_t kMaxSize = kMask - 1;
327 return length > kMaxSize ? 2 : 1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8) {
331 const size_t start_index = out->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 android::ResStringPool_header* header =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 out->NextBlock<android::ResStringPool_header>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 header->header.type = android::RES_STRING_POOL_TYPE;
335 header->header.headerSize = sizeof(*header);
336 header->stringCount = pool.size();
337 if (utf8) {
338 header->flags |= android::ResStringPool_header::UTF8_FLAG;
339 }
340
341 uint32_t* indices =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 uint32_t* style_indices = nullptr;
345 if (!pool.styles_.empty()) {
346 header->styleCount = pool.styles_.back()->str.index() + 1;
347 style_indices = out->NextBlock<uint32_t>(header->styleCount);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 }
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 const size_t before_strings_index = out->size();
351 header->stringsStart = before_strings_index - start_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352
353 for (const auto& entry : pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 *indices = out->size() - before_strings_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 indices++;
356
Adam Lesinski24aad162015-04-24 19:19:30 -0700357 if (utf8) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 const std::string& encoded = entry->value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 const ssize_t utf16_length = utf8_to_utf16_length(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 reinterpret_cast<const uint8_t*>(entry->value.data()),
361 entry->value.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 CHECK(utf16_length >= 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 const size_t total_size = EncodedLengthUnits<char>(utf16_length) +
365 EncodedLengthUnits<char>(encoded.length()) +
366 encoded.size() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 char* data = out->NextBlock<char>(total_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369
370 // First encode the UTF16 string length.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 data = EncodeLength(data, utf16_length);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372
373 // Now encode the size of the real UTF8 string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 data = EncodeLength(data, encoded.length());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 strncpy(data, encoded.data(), encoded.size());
376
377 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 const std::u16string encoded = util::Utf8ToUtf16(entry->value);
379 const ssize_t utf16_length = encoded.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380
381 // Total number of 16-bit words to write.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 const size_t total_size =
383 EncodedLengthUnits<char16_t>(utf16_length) + encoded.size() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 char16_t* data = out->NextBlock<char16_t>(total_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386
387 // Encode the actual UTF16 string length.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 data = EncodeLength(data, utf16_length);
389 const size_t byte_length = encoded.size() * sizeof(char16_t);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390
391 // NOTE: For some reason, strncpy16(data, entry->value.data(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 // entry->value.size()) truncates the string.
393 memcpy(data, encoded.data(), byte_length);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394
395 // The null-terminating character is already here due to the block of data
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 // being set to 0s on allocation.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 }
398 }
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 if (!pool.styles_.empty()) {
403 const size_t before_styles_index = out->size();
404 header->stylesStart = before_styles_index - start_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 size_t current_index = 0;
407 for (const auto& entry : pool.styles_) {
408 while (entry->str.index() > current_index) {
409 style_indices[current_index++] = out->size() - before_styles_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 uint32_t* span_offset = out->NextBlock<uint32_t>();
412 *span_offset = android::ResStringPool_span::END;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 style_indices[current_index++] = out->size() - before_styles_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415
416 android::ResStringPool_span* span =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 out->NextBlock<android::ResStringPool_span>(entry->spans.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 for (const auto& s : entry->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 span->name.index = s.name.index();
420 span->firstChar = s.first_char;
421 span->lastChar = s.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 span++;
423 }
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 uint32_t* spanEnd = out->NextBlock<uint32_t>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 *spanEnd = android::ResStringPool_span::END;
Adam Lesinski24aad162015-04-24 19:19:30 -0700427 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 // The error checking code in the platform looks for an entire
430 // ResStringPool_span structure worth of 0xFFFFFFFF at the end
431 // of the style block, so fill in the remaining 2 32bit words
432 // with 0xFFFFFFFF.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 const size_t padding_length = sizeof(android::ResStringPool_span) -
434 sizeof(android::ResStringPool_span::name);
435 uint8_t* padding = out->NextBlock<uint8_t>(padding_length);
436 memset(padding, 0xff, padding_length);
437 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 header->header.size = out->size() - start_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441}
442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool) {
444 return Flatten(out, pool, true);
Adam Lesinski24aad162015-04-24 19:19:30 -0700445}
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool) {
448 return Flatten(out, pool, false);
Adam Lesinski24aad162015-04-24 19:19:30 -0700449}
450
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451} // namespace aapt