blob: a8c26668b3a5c10d9cbe8236cd027cfb375b30ed [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 Lesinski5b6ee112017-07-28 17:10:35 -070030using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080031
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 Lesinski5b6ee112017-07-28 17:10:35 -070078const std::string& StringPool::Ref::operator*() const {
79 return entry_->value;
80}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinski5b6ee112017-07-28 17:10:35 -070082size_t StringPool::Ref::index() const {
83 // Account for the styles, which *always* come first.
84 return entry_->pool_->styles_.size() + entry_->index_;
85}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087const StringPool::Context& StringPool::Ref::GetContext() const {
88 return entry_->context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089}
90
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091StringPool::StyleRef::StyleRef() : entry_(nullptr) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 : entry_(rhs.entry_) {
95 if (entry_ != nullptr) {
96 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098}
99
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : entry_(entry) {
101 if (entry_ != nullptr) {
102 entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104}
105
106StringPool::StyleRef::~StyleRef() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 if (entry_ != nullptr) {
108 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110}
111
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700112StringPool::StyleRef& StringPool::StyleRef::operator=(const StringPool::StyleRef& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (rhs.entry_ != nullptr) {
114 rhs.entry_->ref_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 if (entry_ != nullptr) {
118 entry_->ref_--;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 entry_ = rhs.entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski75421622017-01-06 15:20:04 -0800124bool StringPool::StyleRef::operator==(const StyleRef& rhs) const {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700125 if (entry_->value != rhs.entry_->value) {
Adam Lesinski75421622017-01-06 15:20:04 -0800126 return false;
127 }
128
129 if (entry_->spans.size() != rhs.entry_->spans.size()) {
130 return false;
131 }
132
133 auto rhs_iter = rhs.entry_->spans.begin();
134 for (const Span& span : entry_->spans) {
135 const Span& rhs_span = *rhs_iter;
136 if (span.first_char != rhs_span.first_char || span.last_char != rhs_span.last_char ||
137 span.name != rhs_span.name) {
138 return false;
139 }
140 }
141 return true;
142}
143
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700144bool StringPool::StyleRef::operator!=(const StyleRef& rhs) const {
145 return !operator==(rhs);
146}
Adam Lesinski75421622017-01-06 15:20:04 -0800147
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148const StringPool::StyleEntry* StringPool::StyleRef::operator->() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 return entry_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150}
151
152const StringPool::StyleEntry& StringPool::StyleRef::operator*() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 return *entry_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154}
155
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700156size_t StringPool::StyleRef::index() const {
157 return entry_->index_;
158}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160const StringPool::Context& StringPool::StyleRef::GetContext() const {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700161 return entry_->context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162}
163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164StringPool::Ref StringPool::MakeRef(const StringPiece& str) {
165 return MakeRefImpl(str, Context{}, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166}
167
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800168StringPool::Ref StringPool::MakeRef(const StringPiece& str, const Context& context,
169 Maybe<size_t> index) {
170 return MakeRefImpl(str, context, true, index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171}
172
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700173StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str, const Context& context,
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800174 bool unique, Maybe<size_t> index) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 if (unique) {
y46029262018-04-16 18:13:14 -0700176 auto range = indexed_strings_.equal_range(str);
177 for (auto iter = range.first; iter != range.second; ++iter) {
178 if (context.priority == iter->second->context.priority) {
179 return Ref(iter->second);
180 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800184 const size_t size = strings_.size();
185 // Insert the string at the end of the string vector if no index is specified
186 const size_t insertion_index = index ? index.value() : size;
187
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700188 std::unique_ptr<Entry> entry(new Entry());
Adam Lesinskid5083f62017-01-16 15:07:21 -0800189 entry->value = str.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 entry->context = context;
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800191 entry->index_ = insertion_index;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 entry->ref_ = 0;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700193 entry->pool_ = this;
194
195 Entry* borrow = entry.get();
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800196 if (insertion_index == size) {
197 strings_.emplace_back(std::move(entry));
198 } else {
199 // Allocate enough space for the string at the index
200 strings_.resize(std::max(insertion_index + 1, size));
201 strings_[insertion_index] = std::move(entry);
202 }
203
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700204 indexed_strings_.insert(std::make_pair(StringPiece(borrow->value), borrow));
205 return Ref(borrow);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206}
207
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700208StringPool::Ref StringPool::MakeRef(const Ref& ref) {
209 if (ref.entry_->pool_ == this) {
210 return ref;
211 }
212 return MakeRef(ref.entry_->value, ref.entry_->context);
213}
214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215StringPool::StyleRef StringPool::MakeRef(const StyleString& str) {
216 return MakeRef(str, Context{});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217}
218
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700219StringPool::StyleRef StringPool::MakeRef(const StyleString& str, const Context& context) {
220 std::unique_ptr<StyleEntry> entry(new StyleEntry());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 entry->value = str.str;
222 entry->context = context;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700223 entry->index_ = styles_.size();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 entry->ref_ = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 for (const aapt::Span& span : str.spans) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700226 entry->spans.emplace_back(Span{MakeRef(span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700228
229 StyleEntry* borrow = entry.get();
230 styles_.emplace_back(std::move(entry));
231 return StyleRef(borrow);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232}
233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700235 std::unique_ptr<StyleEntry> entry(new StyleEntry());
236 entry->value = ref.entry_->value;
237 entry->context = ref.entry_->context;
238 entry->index_ = styles_.size();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 entry->ref_ = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 for (const Span& span : ref.entry_->spans) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700241 entry->spans.emplace_back(Span{MakeRef(*span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700243
244 StyleEntry* borrow = entry.get();
245 styles_.emplace_back(std::move(entry));
246 return StyleRef(borrow);
247}
248
249void StringPool::ReAssignIndices() {
250 // Assign the style indices.
251 const size_t style_len = styles_.size();
252 for (size_t index = 0; index < style_len; index++) {
253 styles_[index]->index_ = index;
254 }
255
256 // Assign the string indices.
257 const size_t string_len = strings_.size();
258 for (size_t index = 0; index < string_len; index++) {
259 strings_[index]->index_ = index;
260 }
Adam Lesinski769de982015-04-10 19:43:55 -0700261}
262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263void StringPool::Merge(StringPool&& pool) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700264 // First, change the owning pool for the incoming strings.
265 for (std::unique_ptr<Entry>& entry : pool.strings_) {
266 entry->pool_ = this;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700268
269 // Now move the styles, strings, and indices over.
270 std::move(pool.styles_.begin(), pool.styles_.end(), std::back_inserter(styles_));
271 pool.styles_.clear();
272 std::move(pool.strings_.begin(), pool.strings_.end(), std::back_inserter(strings_));
273 pool.strings_.clear();
274 indexed_strings_.insert(pool.indexed_strings_.begin(), pool.indexed_strings_.end());
275 pool.indexed_strings_.clear();
276
277 ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278}
279
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700280void StringPool::HintWillAdd(size_t string_count, size_t style_count) {
281 strings_.reserve(strings_.size() + string_count);
282 styles_.reserve(styles_.size() + style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285void StringPool::Prune() {
286 const auto iter_end = indexed_strings_.end();
287 auto index_iter = indexed_strings_.begin();
288 while (index_iter != iter_end) {
289 if (index_iter->second->ref_ <= 0) {
290 index_iter = indexed_strings_.erase(index_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 ++index_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 auto end_iter2 =
297 std::remove_if(strings_.begin(), strings_.end(),
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700298 [](const std::unique_ptr<Entry>& entry) -> bool { return entry->ref_ <= 0; });
299 auto end_iter3 = std::remove_if(
300 styles_.begin(), styles_.end(),
301 [](const std::unique_ptr<StyleEntry>& entry) -> bool { return entry->ref_ <= 0; });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700303 // Remove the entries at the end or else we'll be accessing a deleted string from the StyleEntry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 strings_.erase(end_iter2, strings_.end());
305 styles_.erase(end_iter3, styles_.end());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700307 ReAssignIndices();
308}
309
310template <typename E>
311static void SortEntries(
312 std::vector<std::unique_ptr<E>>& entries,
313 const std::function<int(const StringPool::Context&, const StringPool::Context&)>& cmp) {
314 using UEntry = std::unique_ptr<E>;
315
316 if (cmp != nullptr) {
317 std::sort(entries.begin(), entries.end(), [&cmp](const UEntry& a, const UEntry& b) -> bool {
318 int r = cmp(a->context, b->context);
319 if (r == 0) {
320 r = a->value.compare(b->value);
321 }
322 return r < 0;
323 });
324 } else {
325 std::sort(entries.begin(), entries.end(),
326 [](const UEntry& a, const UEntry& b) -> bool { return a->value < b->value; });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700330void StringPool::Sort(const std::function<int(const Context&, const Context&)>& cmp) {
331 SortEntries(styles_, cmp);
332 SortEntries(strings_, cmp);
333 ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334}
335
Adam Lesinski24aad162015-04-24 19:19:30 -0700336template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337static T* EncodeLength(T* data, size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700339
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
341 constexpr size_t kMaxSize = kMask - 1;
342 if (length > kMaxSize) {
343 *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8)));
344 }
345 *data++ = length;
346 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347}
348
Ryan Mitchell70414f22018-03-26 11:05:31 -0700349/**
350 * Returns the maximum possible string length that can be successfully encoded
351 * using 2 units of the specified T.
352 * EncodeLengthMax<char> -> maximum unit length of 0x7FFF
353 * EncodeLengthMax<char16_t> -> maximum unit length of 0x7FFFFFFF
354 **/
355template <typename T>
356static size_t EncodeLengthMax() {
357 static_assert(std::is_integral<T>::value, "wat.");
358
359 constexpr size_t kMask = 1 << ((sizeof(T) * 8 * 2) - 1);
360 constexpr size_t max = kMask - 1;
361 return max;
362}
363
364/**
365 * Returns the number of units (1 or 2) needed to encode the string length
366 * before writing the string.
367 */
Adam Lesinski24aad162015-04-24 19:19:30 -0700368template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369static size_t EncodedLengthUnits(size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700371
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
373 constexpr size_t kMaxSize = kMask - 1;
374 return length > kMaxSize ? 2 : 1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800375}
376
Ryan Mitchell70414f22018-03-26 11:05:31 -0700377const std::string kStringTooLarge = "STRING_TOO_LARGE";
378
379static bool EncodeString(const std::string& str, const bool utf8, BigBuffer* out,
380 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 if (utf8) {
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700382 const std::string& encoded = util::Utf8ToModifiedUtf8(str);
Ryan Mitchell70414f22018-03-26 11:05:31 -0700383 const ssize_t utf16_length = utf8_to_utf16_length(
384 reinterpret_cast<const uint8_t*>(encoded.data()), encoded.size());
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700385 CHECK(utf16_length >= 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386
Ryan Mitchell70414f22018-03-26 11:05:31 -0700387 // Make sure the lengths to be encoded do not exceed the maximum length that
388 // can be encoded using chars
389 if ((((size_t)encoded.size()) > EncodeLengthMax<char>())
390 || (((size_t)utf16_length) > EncodeLengthMax<char>())) {
391
392 diag->Error(DiagMessage() << "string too large to encode using UTF-8 "
393 << "written instead as '" << kStringTooLarge << "'");
394
395 EncodeString(kStringTooLarge, utf8, out, diag);
396 return false;
397 }
398
399 const size_t total_size = EncodedLengthUnits<char>(utf16_length)
400 + EncodedLengthUnits<char>(encoded.size()) + encoded.size() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700402 char* data = out->NextBlock<char>(total_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700404 // First encode the UTF16 string length.
405 data = EncodeLength(data, utf16_length);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700407 // Now encode the size of the real UTF8 string.
Ryan Mitchell70414f22018-03-26 11:05:31 -0700408 data = EncodeLength(data, encoded.size());
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700409 strncpy(data, encoded.data(), encoded.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410
Ryan Mitchell70414f22018-03-26 11:05:31 -0700411 } else {
412 const std::u16string encoded = util::Utf8ToUtf16(str);
413 const ssize_t utf16_length = encoded.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414
Ryan Mitchell70414f22018-03-26 11:05:31 -0700415 // Make sure the length to be encoded does not exceed the maximum possible
416 // length that can be encoded
417 if (((size_t)utf16_length) > EncodeLengthMax<char16_t>()) {
418 diag->Error(DiagMessage() << "string too large to encode using UTF-16 "
419 << "written instead as '" << kStringTooLarge << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420
Ryan Mitchell70414f22018-03-26 11:05:31 -0700421 EncodeString(kStringTooLarge, utf8, out, diag);
422 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 }
Ryan Mitchell70414f22018-03-26 11:05:31 -0700424
425 // Total number of 16-bit words to write.
426 const size_t total_size = EncodedLengthUnits<char16_t>(utf16_length)
427 + encoded.size() + 1;
428
429 char16_t* data = out->NextBlock<char16_t>(total_size);
430
431 // Encode the actual UTF16 string length.
432 data = EncodeLength(data, utf16_length);
433 const size_t byte_length = encoded.size() * sizeof(char16_t);
434
435 // NOTE: For some reason, strncpy16(data, entry->value.data(),
436 // entry->value.size()) truncates the string.
437 memcpy(data, encoded.data(), byte_length);
438
439 // The null-terminating character is already here due to the block of data
440 // being set to 0s on allocation.
441 }
442
443 return true;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700444}
445
Ryan Mitchell70414f22018-03-26 11:05:31 -0700446bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8,
447 IDiagnostics* diag) {
448 bool no_error = true;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700449 const size_t start_index = out->size();
450 android::ResStringPool_header* header = out->NextBlock<android::ResStringPool_header>();
451 header->header.type = util::HostToDevice16(android::RES_STRING_POOL_TYPE);
452 header->header.headerSize = util::HostToDevice16(sizeof(*header));
453 header->stringCount = util::HostToDevice32(pool.size());
454 header->styleCount = util::HostToDevice32(pool.styles_.size());
455 if (utf8) {
456 header->flags |= android::ResStringPool_header::UTF8_FLAG;
457 }
458
459 uint32_t* indices = pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr;
460 uint32_t* style_indices =
461 pool.styles_.size() != 0 ? out->NextBlock<uint32_t>(pool.styles_.size()) : nullptr;
462
463 const size_t before_strings_index = out->size();
464 header->stringsStart = before_strings_index - start_index;
465
466 // Styles always come first.
467 for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
468 *indices++ = out->size() - before_strings_index;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700469 no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700470 }
471
472 for (const std::unique_ptr<Entry>& entry : pool.strings_) {
473 *indices++ = out->size() - before_strings_index;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700474 no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 }
476
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700479 if (style_indices != nullptr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 const size_t before_styles_index = out->size();
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700481 header->stylesStart = util::HostToDevice32(before_styles_index - start_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700483 for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
484 *style_indices++ = out->size() - before_styles_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700486 if (!entry->spans.empty()) {
487 android::ResStringPool_span* span =
488 out->NextBlock<android::ResStringPool_span>(entry->spans.size());
489 for (const Span& s : entry->spans) {
490 span->name.index = util::HostToDevice32(s.name.index());
491 span->firstChar = util::HostToDevice32(s.first_char);
492 span->lastChar = util::HostToDevice32(s.last_char);
493 span++;
494 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 }
496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 uint32_t* spanEnd = out->NextBlock<uint32_t>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 *spanEnd = android::ResStringPool_span::END;
Adam Lesinski24aad162015-04-24 19:19:30 -0700499 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 // The error checking code in the platform looks for an entire
502 // ResStringPool_span structure worth of 0xFFFFFFFF at the end
503 // of the style block, so fill in the remaining 2 32bit words
504 // with 0xFFFFFFFF.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 const size_t padding_length = sizeof(android::ResStringPool_span) -
506 sizeof(android::ResStringPool_span::name);
507 uint8_t* padding = out->NextBlock<uint8_t>(padding_length);
508 memset(padding, 0xff, padding_length);
509 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700511 header->header.size = util::HostToDevice32(out->size() - start_index);
Ryan Mitchell70414f22018-03-26 11:05:31 -0700512 return no_error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513}
514
Ryan Mitchell70414f22018-03-26 11:05:31 -0700515bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
516 return Flatten(out, pool, true, diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700517}
518
Ryan Mitchell70414f22018-03-26 11:05:31 -0700519bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
520 return Flatten(out, pool, false, diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700521}
522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523} // namespace aapt