blob: 1cb8df311c89b1e7ff7648b4924d322118f0f302 [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000017#include <androidfw/BigBuffer.h>
18#include <androidfw/StringPool.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
Adam Lesinskicacb28f2016-10-19 12:18:14 -070020#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <memory>
22#include <string>
23
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/logging.h"
25#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000027#include "androidfw/Util.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
Adam Lesinski5b6ee112017-07-28 17:10:35 -070029using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031namespace android {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Jeremy Meyer56f36e82022-05-20 20:35:42 +000033StringPool::Ref::Ref() : entry_(nullptr) {
34}
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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000091StringPool::StyleRef::StyleRef() : entry_(nullptr) {
92}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Jeremy Meyer56f36e82022-05-20 20:35:42 +000094StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs) : entry_(rhs.entry_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700164StringPool::Ref StringPool::MakeRef(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 return MakeRefImpl(str, Context{}, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166}
167
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700168StringPool::Ref StringPool::MakeRef(StringPiece str, const Context& context) {
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000169 return MakeRefImpl(str, context, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170}
171
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700172StringPool::Ref StringPool::MakeRefImpl(StringPiece str, const Context& context, bool unique) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 if (unique) {
y46029262018-04-16 18:13:14 -0700174 auto range = indexed_strings_.equal_range(str);
175 for (auto iter = range.first; iter != range.second; ++iter) {
176 if (context.priority == iter->second->context.priority) {
177 return Ref(iter->second);
178 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700182 std::unique_ptr<Entry> entry(new Entry());
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700183 entry->value = std::string(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 entry->context = context;
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000185 entry->index_ = strings_.size();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 entry->ref_ = 0;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700187 entry->pool_ = this;
188
189 Entry* borrow = entry.get();
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000190 strings_.emplace_back(std::move(entry));
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700191 indexed_strings_.insert(std::make_pair(StringPiece(borrow->value), borrow));
192 return Ref(borrow);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193}
194
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700195StringPool::Ref StringPool::MakeRef(const Ref& ref) {
196 if (ref.entry_->pool_ == this) {
197 return ref;
198 }
199 return MakeRef(ref.entry_->value, ref.entry_->context);
200}
201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202StringPool::StyleRef StringPool::MakeRef(const StyleString& str) {
203 return MakeRef(str, Context{});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204}
205
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700206StringPool::StyleRef StringPool::MakeRef(const StyleString& str, const Context& context) {
207 std::unique_ptr<StyleEntry> entry(new StyleEntry());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 entry->value = str.str;
209 entry->context = context;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700210 entry->index_ = styles_.size();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 entry->ref_ = 0;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000212 for (const android::Span& span : str.spans) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700213 entry->spans.emplace_back(Span{MakeRef(span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700215
216 StyleEntry* borrow = entry.get();
217 styles_.emplace_back(std::move(entry));
218 return StyleRef(borrow);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219}
220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700222 std::unique_ptr<StyleEntry> entry(new StyleEntry());
223 entry->value = ref.entry_->value;
224 entry->context = ref.entry_->context;
225 entry->index_ = styles_.size();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 entry->ref_ = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 for (const Span& span : ref.entry_->spans) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700228 entry->spans.emplace_back(Span{MakeRef(*span.name), span.first_char, span.last_char});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700230
231 StyleEntry* borrow = entry.get();
232 styles_.emplace_back(std::move(entry));
233 return StyleRef(borrow);
234}
235
236void StringPool::ReAssignIndices() {
237 // Assign the style indices.
238 const size_t style_len = styles_.size();
239 for (size_t index = 0; index < style_len; index++) {
240 styles_[index]->index_ = index;
241 }
242
243 // Assign the string indices.
244 const size_t string_len = strings_.size();
245 for (size_t index = 0; index < string_len; index++) {
246 strings_[index]->index_ = index;
247 }
Adam Lesinski769de982015-04-10 19:43:55 -0700248}
249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250void StringPool::Merge(StringPool&& pool) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700251 // First, change the owning pool for the incoming strings.
252 for (std::unique_ptr<Entry>& entry : pool.strings_) {
253 entry->pool_ = this;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700255
256 // Now move the styles, strings, and indices over.
257 std::move(pool.styles_.begin(), pool.styles_.end(), std::back_inserter(styles_));
258 pool.styles_.clear();
259 std::move(pool.strings_.begin(), pool.strings_.end(), std::back_inserter(strings_));
260 pool.strings_.clear();
261 indexed_strings_.insert(pool.indexed_strings_.begin(), pool.indexed_strings_.end());
262 pool.indexed_strings_.clear();
263
264 ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265}
266
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700267void StringPool::HintWillAdd(size_t string_count, size_t style_count) {
268 strings_.reserve(strings_.size() + string_count);
269 styles_.reserve(styles_.size() + style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270}
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272void StringPool::Prune() {
273 const auto iter_end = indexed_strings_.end();
274 auto index_iter = indexed_strings_.begin();
275 while (index_iter != iter_end) {
276 if (index_iter->second->ref_ <= 0) {
277 index_iter = indexed_strings_.erase(index_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 ++index_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 auto end_iter2 =
284 std::remove_if(strings_.begin(), strings_.end(),
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700285 [](const std::unique_ptr<Entry>& entry) -> bool { return entry->ref_ <= 0; });
286 auto end_iter3 = std::remove_if(
287 styles_.begin(), styles_.end(),
288 [](const std::unique_ptr<StyleEntry>& entry) -> bool { return entry->ref_ <= 0; });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700290 // 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 -0700291 strings_.erase(end_iter2, strings_.end());
292 styles_.erase(end_iter3, styles_.end());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700294 ReAssignIndices();
295}
296
297template <typename E>
298static void SortEntries(
299 std::vector<std::unique_ptr<E>>& entries,
300 const std::function<int(const StringPool::Context&, const StringPool::Context&)>& cmp) {
301 using UEntry = std::unique_ptr<E>;
302
303 if (cmp != nullptr) {
304 std::sort(entries.begin(), entries.end(), [&cmp](const UEntry& a, const UEntry& b) -> bool {
305 int r = cmp(a->context, b->context);
306 if (r == 0) {
307 r = a->value.compare(b->value);
308 }
309 return r < 0;
310 });
311 } else {
312 std::sort(entries.begin(), entries.end(),
313 [](const UEntry& a, const UEntry& b) -> bool { return a->value < b->value; });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315}
316
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700317void StringPool::Sort(const std::function<int(const Context&, const Context&)>& cmp) {
318 SortEntries(styles_, cmp);
319 SortEntries(strings_, cmp);
320 ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321}
322
Adam Lesinski24aad162015-04-24 19:19:30 -0700323template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324static T* EncodeLength(T* data, size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700326
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
328 constexpr size_t kMaxSize = kMask - 1;
329 if (length > kMaxSize) {
330 *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8)));
331 }
332 *data++ = length;
333 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334}
335
Ryan Mitchell70414f22018-03-26 11:05:31 -0700336/**
337 * Returns the maximum possible string length that can be successfully encoded
338 * using 2 units of the specified T.
339 * EncodeLengthMax<char> -> maximum unit length of 0x7FFF
340 * EncodeLengthMax<char16_t> -> maximum unit length of 0x7FFFFFFF
341 **/
342template <typename T>
343static size_t EncodeLengthMax() {
344 static_assert(std::is_integral<T>::value, "wat.");
345
346 constexpr size_t kMask = 1 << ((sizeof(T) * 8 * 2) - 1);
347 constexpr size_t max = kMask - 1;
348 return max;
349}
350
351/**
352 * Returns the number of units (1 or 2) needed to encode the string length
353 * before writing the string.
354 */
Adam Lesinski24aad162015-04-24 19:19:30 -0700355template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356static size_t EncodedLengthUnits(size_t length) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 static_assert(std::is_integral<T>::value, "wat.");
Adam Lesinski24aad162015-04-24 19:19:30 -0700358
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
360 constexpr size_t kMaxSize = kMask - 1;
361 return length > kMaxSize ? 2 : 1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362}
363
Ryan Mitchell70414f22018-03-26 11:05:31 -0700364const std::string kStringTooLarge = "STRING_TOO_LARGE";
365
366static bool EncodeString(const std::string& str, const bool utf8, BigBuffer* out,
367 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 if (utf8) {
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700369 const std::string& encoded = util::Utf8ToModifiedUtf8(str);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000370 const ssize_t utf16_length =
371 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(encoded.data()), encoded.size());
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700372 CHECK(utf16_length >= 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373
Ryan Mitchell70414f22018-03-26 11:05:31 -0700374 // Make sure the lengths to be encoded do not exceed the maximum length that
375 // can be encoded using chars
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000376 if ((((size_t)encoded.size()) > EncodeLengthMax<char>()) ||
377 (((size_t)utf16_length) > EncodeLengthMax<char>())) {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700378 diag->Error(DiagMessage() << "string too large to encode using UTF-8 "
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000379 << "written instead as '" << kStringTooLarge << "'");
Ryan Mitchell70414f22018-03-26 11:05:31 -0700380
381 EncodeString(kStringTooLarge, utf8, out, diag);
382 return false;
383 }
384
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000385 const size_t total_size = EncodedLengthUnits<char>(utf16_length) +
386 EncodedLengthUnits<char>(encoded.size()) + encoded.size() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700388 char* data = out->NextBlock<char>(total_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700390 // First encode the UTF16 string length.
391 data = EncodeLength(data, utf16_length);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700393 // Now encode the size of the real UTF8 string.
Ryan Mitchell70414f22018-03-26 11:05:31 -0700394 data = EncodeLength(data, encoded.size());
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700395 strncpy(data, encoded.data(), encoded.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396
Ryan Mitchell70414f22018-03-26 11:05:31 -0700397 } else {
398 const std::u16string encoded = util::Utf8ToUtf16(str);
399 const ssize_t utf16_length = encoded.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400
Ryan Mitchell70414f22018-03-26 11:05:31 -0700401 // Make sure the length to be encoded does not exceed the maximum possible
402 // length that can be encoded
403 if (((size_t)utf16_length) > EncodeLengthMax<char16_t>()) {
404 diag->Error(DiagMessage() << "string too large to encode using UTF-16 "
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000405 << "written instead as '" << kStringTooLarge << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406
Ryan Mitchell70414f22018-03-26 11:05:31 -0700407 EncodeString(kStringTooLarge, utf8, out, diag);
408 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 }
Ryan Mitchell70414f22018-03-26 11:05:31 -0700410
411 // Total number of 16-bit words to write.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000412 const size_t total_size = EncodedLengthUnits<char16_t>(utf16_length) + encoded.size() + 1;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700413
414 char16_t* data = out->NextBlock<char16_t>(total_size);
415
416 // Encode the actual UTF16 string length.
417 data = EncodeLength(data, utf16_length);
418 const size_t byte_length = encoded.size() * sizeof(char16_t);
419
420 // NOTE: For some reason, strncpy16(data, entry->value.data(),
421 // entry->value.size()) truncates the string.
422 memcpy(data, encoded.data(), byte_length);
423
424 // The null-terminating character is already here due to the block of data
425 // being set to 0s on allocation.
426 }
427
428 return true;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700429}
430
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000431bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag) {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700432 bool no_error = true;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700433 const size_t start_index = out->size();
434 android::ResStringPool_header* header = out->NextBlock<android::ResStringPool_header>();
435 header->header.type = util::HostToDevice16(android::RES_STRING_POOL_TYPE);
436 header->header.headerSize = util::HostToDevice16(sizeof(*header));
437 header->stringCount = util::HostToDevice32(pool.size());
438 header->styleCount = util::HostToDevice32(pool.styles_.size());
439 if (utf8) {
440 header->flags |= android::ResStringPool_header::UTF8_FLAG;
441 }
442
443 uint32_t* indices = pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr;
444 uint32_t* style_indices =
445 pool.styles_.size() != 0 ? out->NextBlock<uint32_t>(pool.styles_.size()) : nullptr;
446
447 const size_t before_strings_index = out->size();
448 header->stringsStart = before_strings_index - start_index;
449
450 // Styles always come first.
451 for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
452 *indices++ = out->size() - before_strings_index;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700453 no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700454 }
455
456 for (const std::unique_ptr<Entry>& entry : pool.strings_) {
457 *indices++ = out->size() - before_strings_index;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700458 no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 }
460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700463 if (style_indices != nullptr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 const size_t before_styles_index = out->size();
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700465 header->stylesStart = util::HostToDevice32(before_styles_index - start_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700467 for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
468 *style_indices++ = out->size() - before_styles_index;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700470 if (!entry->spans.empty()) {
471 android::ResStringPool_span* span =
472 out->NextBlock<android::ResStringPool_span>(entry->spans.size());
473 for (const Span& s : entry->spans) {
474 span->name.index = util::HostToDevice32(s.name.index());
475 span->firstChar = util::HostToDevice32(s.first_char);
476 span->lastChar = util::HostToDevice32(s.last_char);
477 span++;
478 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 }
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 uint32_t* spanEnd = out->NextBlock<uint32_t>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 *spanEnd = android::ResStringPool_span::END;
Adam Lesinski24aad162015-04-24 19:19:30 -0700483 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 // The error checking code in the platform looks for an entire
486 // ResStringPool_span structure worth of 0xFFFFFFFF at the end
487 // of the style block, so fill in the remaining 2 32bit words
488 // with 0xFFFFFFFF.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000489 const size_t padding_length =
490 sizeof(android::ResStringPool_span) - sizeof(android::ResStringPool_span::name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 uint8_t* padding = out->NextBlock<uint8_t>(padding_length);
492 memset(padding, 0xff, padding_length);
493 out->Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700495 header->header.size = util::HostToDevice32(out->size() - start_index);
Ryan Mitchell70414f22018-03-26 11:05:31 -0700496 return no_error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497}
498
Ryan Mitchell70414f22018-03-26 11:05:31 -0700499bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
500 return Flatten(out, pool, true, diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700501}
502
Ryan Mitchell70414f22018-03-26 11:05:31 -0700503bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
504 return Flatten(out, pool, false, diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700505}
506
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000507} // namespace android