xref: /aosp_15_r20/frameworks/base/libs/androidfw/StringPool.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker 
17*d57664e9SAndroid Build Coastguard Worker #include <androidfw/BigBuffer.h>
18*d57664e9SAndroid Build Coastguard Worker #include <androidfw/StringPool.h>
19*d57664e9SAndroid Build Coastguard Worker 
20*d57664e9SAndroid Build Coastguard Worker #include <algorithm>
21*d57664e9SAndroid Build Coastguard Worker #include <memory>
22*d57664e9SAndroid Build Coastguard Worker #include <string>
23*d57664e9SAndroid Build Coastguard Worker 
24*d57664e9SAndroid Build Coastguard Worker #include "android-base/logging.h"
25*d57664e9SAndroid Build Coastguard Worker #include "androidfw/ResourceTypes.h"
26*d57664e9SAndroid Build Coastguard Worker #include "androidfw/StringPiece.h"
27*d57664e9SAndroid Build Coastguard Worker #include "androidfw/Util.h"
28*d57664e9SAndroid Build Coastguard Worker 
29*d57664e9SAndroid Build Coastguard Worker using ::android::StringPiece;
30*d57664e9SAndroid Build Coastguard Worker 
31*d57664e9SAndroid Build Coastguard Worker namespace android {
32*d57664e9SAndroid Build Coastguard Worker 
Ref()33*d57664e9SAndroid Build Coastguard Worker StringPool::Ref::Ref() : entry_(nullptr) {
34*d57664e9SAndroid Build Coastguard Worker }
35*d57664e9SAndroid Build Coastguard Worker 
Ref(const StringPool::Ref & rhs)36*d57664e9SAndroid Build Coastguard Worker StringPool::Ref::Ref(const StringPool::Ref& rhs) : entry_(rhs.entry_) {
37*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
38*d57664e9SAndroid Build Coastguard Worker     entry_->ref_++;
39*d57664e9SAndroid Build Coastguard Worker   }
40*d57664e9SAndroid Build Coastguard Worker }
41*d57664e9SAndroid Build Coastguard Worker 
Ref(StringPool::Entry * entry)42*d57664e9SAndroid Build Coastguard Worker StringPool::Ref::Ref(StringPool::Entry* entry) : entry_(entry) {
43*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
44*d57664e9SAndroid Build Coastguard Worker     entry_->ref_++;
45*d57664e9SAndroid Build Coastguard Worker   }
46*d57664e9SAndroid Build Coastguard Worker }
47*d57664e9SAndroid Build Coastguard Worker 
~Ref()48*d57664e9SAndroid Build Coastguard Worker StringPool::Ref::~Ref() {
49*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
50*d57664e9SAndroid Build Coastguard Worker     entry_->ref_--;
51*d57664e9SAndroid Build Coastguard Worker   }
52*d57664e9SAndroid Build Coastguard Worker }
53*d57664e9SAndroid Build Coastguard Worker 
operator =(const StringPool::Ref & rhs)54*d57664e9SAndroid Build Coastguard Worker StringPool::Ref& StringPool::Ref::operator=(const StringPool::Ref& rhs) {
55*d57664e9SAndroid Build Coastguard Worker   if (rhs.entry_ != nullptr) {
56*d57664e9SAndroid Build Coastguard Worker     rhs.entry_->ref_++;
57*d57664e9SAndroid Build Coastguard Worker   }
58*d57664e9SAndroid Build Coastguard Worker 
59*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
60*d57664e9SAndroid Build Coastguard Worker     entry_->ref_--;
61*d57664e9SAndroid Build Coastguard Worker   }
62*d57664e9SAndroid Build Coastguard Worker   entry_ = rhs.entry_;
63*d57664e9SAndroid Build Coastguard Worker   return *this;
64*d57664e9SAndroid Build Coastguard Worker }
65*d57664e9SAndroid Build Coastguard Worker 
operator ==(const Ref & rhs) const66*d57664e9SAndroid Build Coastguard Worker bool StringPool::Ref::operator==(const Ref& rhs) const {
67*d57664e9SAndroid Build Coastguard Worker   return entry_->value == rhs.entry_->value;
68*d57664e9SAndroid Build Coastguard Worker }
69*d57664e9SAndroid Build Coastguard Worker 
operator !=(const Ref & rhs) const70*d57664e9SAndroid Build Coastguard Worker bool StringPool::Ref::operator!=(const Ref& rhs) const {
71*d57664e9SAndroid Build Coastguard Worker   return entry_->value != rhs.entry_->value;
72*d57664e9SAndroid Build Coastguard Worker }
73*d57664e9SAndroid Build Coastguard Worker 
operator ->() const74*d57664e9SAndroid Build Coastguard Worker const std::string* StringPool::Ref::operator->() const {
75*d57664e9SAndroid Build Coastguard Worker   return &entry_->value;
76*d57664e9SAndroid Build Coastguard Worker }
77*d57664e9SAndroid Build Coastguard Worker 
operator *() const78*d57664e9SAndroid Build Coastguard Worker const std::string& StringPool::Ref::operator*() const {
79*d57664e9SAndroid Build Coastguard Worker   return entry_->value;
80*d57664e9SAndroid Build Coastguard Worker }
81*d57664e9SAndroid Build Coastguard Worker 
index() const82*d57664e9SAndroid Build Coastguard Worker size_t StringPool::Ref::index() const {
83*d57664e9SAndroid Build Coastguard Worker   // Account for the styles, which *always* come first.
84*d57664e9SAndroid Build Coastguard Worker   return entry_->pool_->styles_.size() + entry_->index_;
85*d57664e9SAndroid Build Coastguard Worker }
86*d57664e9SAndroid Build Coastguard Worker 
GetContext() const87*d57664e9SAndroid Build Coastguard Worker const StringPool::Context& StringPool::Ref::GetContext() const {
88*d57664e9SAndroid Build Coastguard Worker   return entry_->context;
89*d57664e9SAndroid Build Coastguard Worker }
90*d57664e9SAndroid Build Coastguard Worker 
StyleRef()91*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef::StyleRef() : entry_(nullptr) {
92*d57664e9SAndroid Build Coastguard Worker }
93*d57664e9SAndroid Build Coastguard Worker 
StyleRef(const StringPool::StyleRef & rhs)94*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs) : entry_(rhs.entry_) {
95*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
96*d57664e9SAndroid Build Coastguard Worker     entry_->ref_++;
97*d57664e9SAndroid Build Coastguard Worker   }
98*d57664e9SAndroid Build Coastguard Worker }
99*d57664e9SAndroid Build Coastguard Worker 
StyleRef(StringPool::StyleEntry * entry)100*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : entry_(entry) {
101*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
102*d57664e9SAndroid Build Coastguard Worker     entry_->ref_++;
103*d57664e9SAndroid Build Coastguard Worker   }
104*d57664e9SAndroid Build Coastguard Worker }
105*d57664e9SAndroid Build Coastguard Worker 
~StyleRef()106*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef::~StyleRef() {
107*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
108*d57664e9SAndroid Build Coastguard Worker     entry_->ref_--;
109*d57664e9SAndroid Build Coastguard Worker   }
110*d57664e9SAndroid Build Coastguard Worker }
111*d57664e9SAndroid Build Coastguard Worker 
operator =(const StringPool::StyleRef & rhs)112*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef& StringPool::StyleRef::operator=(const StringPool::StyleRef& rhs) {
113*d57664e9SAndroid Build Coastguard Worker   if (rhs.entry_ != nullptr) {
114*d57664e9SAndroid Build Coastguard Worker     rhs.entry_->ref_++;
115*d57664e9SAndroid Build Coastguard Worker   }
116*d57664e9SAndroid Build Coastguard Worker 
117*d57664e9SAndroid Build Coastguard Worker   if (entry_ != nullptr) {
118*d57664e9SAndroid Build Coastguard Worker     entry_->ref_--;
119*d57664e9SAndroid Build Coastguard Worker   }
120*d57664e9SAndroid Build Coastguard Worker   entry_ = rhs.entry_;
121*d57664e9SAndroid Build Coastguard Worker   return *this;
122*d57664e9SAndroid Build Coastguard Worker }
123*d57664e9SAndroid Build Coastguard Worker 
operator ==(const StyleRef & rhs) const124*d57664e9SAndroid Build Coastguard Worker bool StringPool::StyleRef::operator==(const StyleRef& rhs) const {
125*d57664e9SAndroid Build Coastguard Worker   if (entry_->value != rhs.entry_->value) {
126*d57664e9SAndroid Build Coastguard Worker     return false;
127*d57664e9SAndroid Build Coastguard Worker   }
128*d57664e9SAndroid Build Coastguard Worker 
129*d57664e9SAndroid Build Coastguard Worker   if (entry_->spans.size() != rhs.entry_->spans.size()) {
130*d57664e9SAndroid Build Coastguard Worker     return false;
131*d57664e9SAndroid Build Coastguard Worker   }
132*d57664e9SAndroid Build Coastguard Worker 
133*d57664e9SAndroid Build Coastguard Worker   auto rhs_iter = rhs.entry_->spans.begin();
134*d57664e9SAndroid Build Coastguard Worker   for (const Span& span : entry_->spans) {
135*d57664e9SAndroid Build Coastguard Worker     const Span& rhs_span = *rhs_iter++;
136*d57664e9SAndroid Build Coastguard Worker     if (span.first_char != rhs_span.first_char || span.last_char != rhs_span.last_char ||
137*d57664e9SAndroid Build Coastguard Worker         span.name != rhs_span.name) {
138*d57664e9SAndroid Build Coastguard Worker       return false;
139*d57664e9SAndroid Build Coastguard Worker     }
140*d57664e9SAndroid Build Coastguard Worker   }
141*d57664e9SAndroid Build Coastguard Worker   return true;
142*d57664e9SAndroid Build Coastguard Worker }
143*d57664e9SAndroid Build Coastguard Worker 
operator !=(const StyleRef & rhs) const144*d57664e9SAndroid Build Coastguard Worker bool StringPool::StyleRef::operator!=(const StyleRef& rhs) const {
145*d57664e9SAndroid Build Coastguard Worker   return !operator==(rhs);
146*d57664e9SAndroid Build Coastguard Worker }
147*d57664e9SAndroid Build Coastguard Worker 
operator ->() const148*d57664e9SAndroid Build Coastguard Worker const StringPool::StyleEntry* StringPool::StyleRef::operator->() const {
149*d57664e9SAndroid Build Coastguard Worker   return entry_;
150*d57664e9SAndroid Build Coastguard Worker }
151*d57664e9SAndroid Build Coastguard Worker 
operator *() const152*d57664e9SAndroid Build Coastguard Worker const StringPool::StyleEntry& StringPool::StyleRef::operator*() const {
153*d57664e9SAndroid Build Coastguard Worker   return *entry_;
154*d57664e9SAndroid Build Coastguard Worker }
155*d57664e9SAndroid Build Coastguard Worker 
index() const156*d57664e9SAndroid Build Coastguard Worker size_t StringPool::StyleRef::index() const {
157*d57664e9SAndroid Build Coastguard Worker   return entry_->index_;
158*d57664e9SAndroid Build Coastguard Worker }
159*d57664e9SAndroid Build Coastguard Worker 
GetContext() const160*d57664e9SAndroid Build Coastguard Worker const StringPool::Context& StringPool::StyleRef::GetContext() const {
161*d57664e9SAndroid Build Coastguard Worker   return entry_->context;
162*d57664e9SAndroid Build Coastguard Worker }
163*d57664e9SAndroid Build Coastguard Worker 
MakeRef(StringPiece str)164*d57664e9SAndroid Build Coastguard Worker StringPool::Ref StringPool::MakeRef(StringPiece str) {
165*d57664e9SAndroid Build Coastguard Worker   return MakeRefImpl(str, Context{}, true);
166*d57664e9SAndroid Build Coastguard Worker }
167*d57664e9SAndroid Build Coastguard Worker 
MakeRef(StringPiece str,const Context & context)168*d57664e9SAndroid Build Coastguard Worker StringPool::Ref StringPool::MakeRef(StringPiece str, const Context& context) {
169*d57664e9SAndroid Build Coastguard Worker   return MakeRefImpl(str, context, true);
170*d57664e9SAndroid Build Coastguard Worker }
171*d57664e9SAndroid Build Coastguard Worker 
MakeRefImpl(StringPiece str,const Context & context,bool unique)172*d57664e9SAndroid Build Coastguard Worker StringPool::Ref StringPool::MakeRefImpl(StringPiece str, const Context& context, bool unique) {
173*d57664e9SAndroid Build Coastguard Worker   if (unique) {
174*d57664e9SAndroid Build Coastguard Worker     auto range = indexed_strings_.equal_range(str);
175*d57664e9SAndroid Build Coastguard Worker     for (auto iter = range.first; iter != range.second; ++iter) {
176*d57664e9SAndroid Build Coastguard Worker       if (context.priority == iter->second->context.priority) {
177*d57664e9SAndroid Build Coastguard Worker         return Ref(iter->second);
178*d57664e9SAndroid Build Coastguard Worker       }
179*d57664e9SAndroid Build Coastguard Worker     }
180*d57664e9SAndroid Build Coastguard Worker   }
181*d57664e9SAndroid Build Coastguard Worker 
182*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<Entry> entry(new Entry());
183*d57664e9SAndroid Build Coastguard Worker   entry->value = std::string(str);
184*d57664e9SAndroid Build Coastguard Worker   entry->context = context;
185*d57664e9SAndroid Build Coastguard Worker   entry->index_ = strings_.size();
186*d57664e9SAndroid Build Coastguard Worker   entry->ref_ = 0;
187*d57664e9SAndroid Build Coastguard Worker   entry->pool_ = this;
188*d57664e9SAndroid Build Coastguard Worker 
189*d57664e9SAndroid Build Coastguard Worker   Entry* borrow = entry.get();
190*d57664e9SAndroid Build Coastguard Worker   strings_.emplace_back(std::move(entry));
191*d57664e9SAndroid Build Coastguard Worker   indexed_strings_.insert(std::make_pair(StringPiece(borrow->value), borrow));
192*d57664e9SAndroid Build Coastguard Worker   return Ref(borrow);
193*d57664e9SAndroid Build Coastguard Worker }
194*d57664e9SAndroid Build Coastguard Worker 
MakeRef(const Ref & ref)195*d57664e9SAndroid Build Coastguard Worker StringPool::Ref StringPool::MakeRef(const Ref& ref) {
196*d57664e9SAndroid Build Coastguard Worker   if (ref.entry_->pool_ == this) {
197*d57664e9SAndroid Build Coastguard Worker     return ref;
198*d57664e9SAndroid Build Coastguard Worker   }
199*d57664e9SAndroid Build Coastguard Worker   return MakeRef(ref.entry_->value, ref.entry_->context);
200*d57664e9SAndroid Build Coastguard Worker }
201*d57664e9SAndroid Build Coastguard Worker 
MakeRef(const StyleString & str)202*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef StringPool::MakeRef(const StyleString& str) {
203*d57664e9SAndroid Build Coastguard Worker   return MakeRef(str, Context{});
204*d57664e9SAndroid Build Coastguard Worker }
205*d57664e9SAndroid Build Coastguard Worker 
MakeRef(const StyleString & str,const Context & context)206*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef StringPool::MakeRef(const StyleString& str, const Context& context) {
207*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<StyleEntry> entry(new StyleEntry());
208*d57664e9SAndroid Build Coastguard Worker   entry->value = str.str;
209*d57664e9SAndroid Build Coastguard Worker   entry->context = context;
210*d57664e9SAndroid Build Coastguard Worker   entry->index_ = styles_.size();
211*d57664e9SAndroid Build Coastguard Worker   entry->ref_ = 0;
212*d57664e9SAndroid Build Coastguard Worker   for (const android::Span& span : str.spans) {
213*d57664e9SAndroid Build Coastguard Worker     entry->spans.emplace_back(Span{MakeRef(span.name), span.first_char, span.last_char});
214*d57664e9SAndroid Build Coastguard Worker   }
215*d57664e9SAndroid Build Coastguard Worker 
216*d57664e9SAndroid Build Coastguard Worker   StyleEntry* borrow = entry.get();
217*d57664e9SAndroid Build Coastguard Worker   styles_.emplace_back(std::move(entry));
218*d57664e9SAndroid Build Coastguard Worker   return StyleRef(borrow);
219*d57664e9SAndroid Build Coastguard Worker }
220*d57664e9SAndroid Build Coastguard Worker 
MakeRef(const StyleRef & ref)221*d57664e9SAndroid Build Coastguard Worker StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) {
222*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<StyleEntry> entry(new StyleEntry());
223*d57664e9SAndroid Build Coastguard Worker   entry->value = ref.entry_->value;
224*d57664e9SAndroid Build Coastguard Worker   entry->context = ref.entry_->context;
225*d57664e9SAndroid Build Coastguard Worker   entry->index_ = styles_.size();
226*d57664e9SAndroid Build Coastguard Worker   entry->ref_ = 0;
227*d57664e9SAndroid Build Coastguard Worker   for (const Span& span : ref.entry_->spans) {
228*d57664e9SAndroid Build Coastguard Worker     entry->spans.emplace_back(Span{MakeRef(*span.name), span.first_char, span.last_char});
229*d57664e9SAndroid Build Coastguard Worker   }
230*d57664e9SAndroid Build Coastguard Worker 
231*d57664e9SAndroid Build Coastguard Worker   StyleEntry* borrow = entry.get();
232*d57664e9SAndroid Build Coastguard Worker   styles_.emplace_back(std::move(entry));
233*d57664e9SAndroid Build Coastguard Worker   return StyleRef(borrow);
234*d57664e9SAndroid Build Coastguard Worker }
235*d57664e9SAndroid Build Coastguard Worker 
ReAssignIndices()236*d57664e9SAndroid Build Coastguard Worker void StringPool::ReAssignIndices() {
237*d57664e9SAndroid Build Coastguard Worker   // Assign the style indices.
238*d57664e9SAndroid Build Coastguard Worker   const size_t style_len = styles_.size();
239*d57664e9SAndroid Build Coastguard Worker   for (size_t index = 0; index < style_len; index++) {
240*d57664e9SAndroid Build Coastguard Worker     styles_[index]->index_ = index;
241*d57664e9SAndroid Build Coastguard Worker   }
242*d57664e9SAndroid Build Coastguard Worker 
243*d57664e9SAndroid Build Coastguard Worker   // Assign the string indices.
244*d57664e9SAndroid Build Coastguard Worker   const size_t string_len = strings_.size();
245*d57664e9SAndroid Build Coastguard Worker   for (size_t index = 0; index < string_len; index++) {
246*d57664e9SAndroid Build Coastguard Worker     strings_[index]->index_ = index;
247*d57664e9SAndroid Build Coastguard Worker   }
248*d57664e9SAndroid Build Coastguard Worker }
249*d57664e9SAndroid Build Coastguard Worker 
Merge(StringPool && pool)250*d57664e9SAndroid Build Coastguard Worker void StringPool::Merge(StringPool&& pool) {
251*d57664e9SAndroid Build Coastguard Worker   // First, change the owning pool for the incoming strings.
252*d57664e9SAndroid Build Coastguard Worker   for (std::unique_ptr<Entry>& entry : pool.strings_) {
253*d57664e9SAndroid Build Coastguard Worker     entry->pool_ = this;
254*d57664e9SAndroid Build Coastguard Worker   }
255*d57664e9SAndroid Build Coastguard Worker 
256*d57664e9SAndroid Build Coastguard Worker   // Now move the styles, strings, and indices over.
257*d57664e9SAndroid Build Coastguard Worker   std::move(pool.styles_.begin(), pool.styles_.end(), std::back_inserter(styles_));
258*d57664e9SAndroid Build Coastguard Worker   pool.styles_.clear();
259*d57664e9SAndroid Build Coastguard Worker   std::move(pool.strings_.begin(), pool.strings_.end(), std::back_inserter(strings_));
260*d57664e9SAndroid Build Coastguard Worker   pool.strings_.clear();
261*d57664e9SAndroid Build Coastguard Worker   indexed_strings_.insert(pool.indexed_strings_.begin(), pool.indexed_strings_.end());
262*d57664e9SAndroid Build Coastguard Worker   pool.indexed_strings_.clear();
263*d57664e9SAndroid Build Coastguard Worker 
264*d57664e9SAndroid Build Coastguard Worker   ReAssignIndices();
265*d57664e9SAndroid Build Coastguard Worker }
266*d57664e9SAndroid Build Coastguard Worker 
HintWillAdd(size_t string_count,size_t style_count)267*d57664e9SAndroid Build Coastguard Worker void StringPool::HintWillAdd(size_t string_count, size_t style_count) {
268*d57664e9SAndroid Build Coastguard Worker   strings_.reserve(strings_.size() + string_count);
269*d57664e9SAndroid Build Coastguard Worker   styles_.reserve(styles_.size() + style_count);
270*d57664e9SAndroid Build Coastguard Worker }
271*d57664e9SAndroid Build Coastguard Worker 
Prune()272*d57664e9SAndroid Build Coastguard Worker void StringPool::Prune() {
273*d57664e9SAndroid Build Coastguard Worker   const auto iter_end = indexed_strings_.end();
274*d57664e9SAndroid Build Coastguard Worker   auto index_iter = indexed_strings_.begin();
275*d57664e9SAndroid Build Coastguard Worker   while (index_iter != iter_end) {
276*d57664e9SAndroid Build Coastguard Worker     if (index_iter->second->ref_ <= 0) {
277*d57664e9SAndroid Build Coastguard Worker       index_iter = indexed_strings_.erase(index_iter);
278*d57664e9SAndroid Build Coastguard Worker     } else {
279*d57664e9SAndroid Build Coastguard Worker       ++index_iter;
280*d57664e9SAndroid Build Coastguard Worker     }
281*d57664e9SAndroid Build Coastguard Worker   }
282*d57664e9SAndroid Build Coastguard Worker 
283*d57664e9SAndroid Build Coastguard Worker   auto end_iter2 =
284*d57664e9SAndroid Build Coastguard Worker       std::remove_if(strings_.begin(), strings_.end(),
285*d57664e9SAndroid Build Coastguard Worker                      [](const std::unique_ptr<Entry>& entry) -> bool { return entry->ref_ <= 0; });
286*d57664e9SAndroid Build Coastguard Worker   auto end_iter3 = std::remove_if(
287*d57664e9SAndroid Build Coastguard Worker       styles_.begin(), styles_.end(),
288*d57664e9SAndroid Build Coastguard Worker       [](const std::unique_ptr<StyleEntry>& entry) -> bool { return entry->ref_ <= 0; });
289*d57664e9SAndroid Build Coastguard Worker 
290*d57664e9SAndroid Build Coastguard Worker   // Remove the entries at the end or else we'll be accessing a deleted string from the StyleEntry.
291*d57664e9SAndroid Build Coastguard Worker   strings_.erase(end_iter2, strings_.end());
292*d57664e9SAndroid Build Coastguard Worker   styles_.erase(end_iter3, styles_.end());
293*d57664e9SAndroid Build Coastguard Worker 
294*d57664e9SAndroid Build Coastguard Worker   ReAssignIndices();
295*d57664e9SAndroid Build Coastguard Worker }
296*d57664e9SAndroid Build Coastguard Worker 
297*d57664e9SAndroid Build Coastguard Worker template <typename E>
SortEntries(std::vector<std::unique_ptr<E>> & entries,base::function_ref<int (const StringPool::Context &,const StringPool::Context &)> cmp)298*d57664e9SAndroid Build Coastguard Worker static void SortEntries(
299*d57664e9SAndroid Build Coastguard Worker     std::vector<std::unique_ptr<E>>& entries,
300*d57664e9SAndroid Build Coastguard Worker     base::function_ref<int(const StringPool::Context&, const StringPool::Context&)> cmp) {
301*d57664e9SAndroid Build Coastguard Worker   using UEntry = std::unique_ptr<E>;
302*d57664e9SAndroid Build Coastguard Worker   std::sort(entries.begin(), entries.end(), [cmp](const UEntry& a, const UEntry& b) -> bool {
303*d57664e9SAndroid Build Coastguard Worker     int r = cmp(a->context, b->context);
304*d57664e9SAndroid Build Coastguard Worker     if (r == 0) {
305*d57664e9SAndroid Build Coastguard Worker       r = a->value.compare(b->value);
306*d57664e9SAndroid Build Coastguard Worker     }
307*d57664e9SAndroid Build Coastguard Worker     return r < 0;
308*d57664e9SAndroid Build Coastguard Worker   });
309*d57664e9SAndroid Build Coastguard Worker }
310*d57664e9SAndroid Build Coastguard Worker 
Sort()311*d57664e9SAndroid Build Coastguard Worker void StringPool::Sort() {
312*d57664e9SAndroid Build Coastguard Worker   Sort([](auto&&, auto&&) { return 0; });
313*d57664e9SAndroid Build Coastguard Worker }
314*d57664e9SAndroid Build Coastguard Worker 
Sort(base::function_ref<int (const Context &,const Context &)> cmp)315*d57664e9SAndroid Build Coastguard Worker void StringPool::Sort(base::function_ref<int(const Context&, const Context&)> cmp) {
316*d57664e9SAndroid Build Coastguard Worker   SortEntries(styles_, cmp);
317*d57664e9SAndroid Build Coastguard Worker   SortEntries(strings_, cmp);
318*d57664e9SAndroid Build Coastguard Worker   ReAssignIndices();
319*d57664e9SAndroid Build Coastguard Worker }
320*d57664e9SAndroid Build Coastguard Worker 
321*d57664e9SAndroid Build Coastguard Worker template <typename T>
EncodeLength(T * data,size_t length)322*d57664e9SAndroid Build Coastguard Worker static T* EncodeLength(T* data, size_t length) {
323*d57664e9SAndroid Build Coastguard Worker   static_assert(std::is_integral<T>::value, "wat.");
324*d57664e9SAndroid Build Coastguard Worker 
325*d57664e9SAndroid Build Coastguard Worker   constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
326*d57664e9SAndroid Build Coastguard Worker   constexpr size_t kMaxSize = kMask - 1;
327*d57664e9SAndroid Build Coastguard Worker   if (length > kMaxSize) {
328*d57664e9SAndroid Build Coastguard Worker     *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8)));
329*d57664e9SAndroid Build Coastguard Worker   }
330*d57664e9SAndroid Build Coastguard Worker   *data++ = length;
331*d57664e9SAndroid Build Coastguard Worker   return data;
332*d57664e9SAndroid Build Coastguard Worker }
333*d57664e9SAndroid Build Coastguard Worker 
334*d57664e9SAndroid Build Coastguard Worker /**
335*d57664e9SAndroid Build Coastguard Worker  * Returns the maximum possible string length that can be successfully encoded
336*d57664e9SAndroid Build Coastguard Worker  * using 2 units of the specified T.
337*d57664e9SAndroid Build Coastguard Worker  *    EncodeLengthMax<char> -> maximum unit length of 0x7FFF
338*d57664e9SAndroid Build Coastguard Worker  *    EncodeLengthMax<char16_t> -> maximum unit length of 0x7FFFFFFF
339*d57664e9SAndroid Build Coastguard Worker  **/
340*d57664e9SAndroid Build Coastguard Worker template <typename T>
EncodeLengthMax()341*d57664e9SAndroid Build Coastguard Worker static size_t EncodeLengthMax() {
342*d57664e9SAndroid Build Coastguard Worker   static_assert(std::is_integral<T>::value, "wat.");
343*d57664e9SAndroid Build Coastguard Worker 
344*d57664e9SAndroid Build Coastguard Worker   constexpr size_t kMask = 1 << ((sizeof(T) * 8 * 2) - 1);
345*d57664e9SAndroid Build Coastguard Worker   constexpr size_t max = kMask - 1;
346*d57664e9SAndroid Build Coastguard Worker   return max;
347*d57664e9SAndroid Build Coastguard Worker }
348*d57664e9SAndroid Build Coastguard Worker 
349*d57664e9SAndroid Build Coastguard Worker /**
350*d57664e9SAndroid Build Coastguard Worker  * Returns the number of units (1 or 2) needed to encode the string length
351*d57664e9SAndroid Build Coastguard Worker  * before writing the string.
352*d57664e9SAndroid Build Coastguard Worker  */
353*d57664e9SAndroid Build Coastguard Worker template <typename T>
EncodedLengthUnits(size_t length)354*d57664e9SAndroid Build Coastguard Worker static size_t EncodedLengthUnits(size_t length) {
355*d57664e9SAndroid Build Coastguard Worker   static_assert(std::is_integral<T>::value, "wat.");
356*d57664e9SAndroid Build Coastguard Worker 
357*d57664e9SAndroid Build Coastguard Worker   constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
358*d57664e9SAndroid Build Coastguard Worker   constexpr size_t kMaxSize = kMask - 1;
359*d57664e9SAndroid Build Coastguard Worker   return length > kMaxSize ? 2 : 1;
360*d57664e9SAndroid Build Coastguard Worker }
361*d57664e9SAndroid Build Coastguard Worker 
362*d57664e9SAndroid Build Coastguard Worker const std::string kStringTooLarge = "STRING_TOO_LARGE";
363*d57664e9SAndroid Build Coastguard Worker 
EncodeString(const std::string & str,const bool utf8,BigBuffer * out,IDiagnostics * diag)364*d57664e9SAndroid Build Coastguard Worker static bool EncodeString(const std::string& str, const bool utf8, BigBuffer* out,
365*d57664e9SAndroid Build Coastguard Worker                          IDiagnostics* diag) {
366*d57664e9SAndroid Build Coastguard Worker   if (utf8) {
367*d57664e9SAndroid Build Coastguard Worker     const std::string& encoded = util::Utf8ToModifiedUtf8(str);
368*d57664e9SAndroid Build Coastguard Worker     const ssize_t utf16_length =
369*d57664e9SAndroid Build Coastguard Worker         utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(encoded.data()), encoded.size());
370*d57664e9SAndroid Build Coastguard Worker     CHECK(utf16_length >= 0);
371*d57664e9SAndroid Build Coastguard Worker 
372*d57664e9SAndroid Build Coastguard Worker     // Make sure the lengths to be encoded do not exceed the maximum length that
373*d57664e9SAndroid Build Coastguard Worker     // can be encoded using chars
374*d57664e9SAndroid Build Coastguard Worker     if ((((size_t)encoded.size()) > EncodeLengthMax<char>()) ||
375*d57664e9SAndroid Build Coastguard Worker         (((size_t)utf16_length) > EncodeLengthMax<char>())) {
376*d57664e9SAndroid Build Coastguard Worker       diag->Error(DiagMessage() << "string too large to encode using UTF-8 "
377*d57664e9SAndroid Build Coastguard Worker                                 << "written instead as '" << kStringTooLarge << "'");
378*d57664e9SAndroid Build Coastguard Worker 
379*d57664e9SAndroid Build Coastguard Worker       EncodeString(kStringTooLarge, utf8, out, diag);
380*d57664e9SAndroid Build Coastguard Worker       return false;
381*d57664e9SAndroid Build Coastguard Worker     }
382*d57664e9SAndroid Build Coastguard Worker 
383*d57664e9SAndroid Build Coastguard Worker     const size_t total_size = EncodedLengthUnits<char>(utf16_length) +
384*d57664e9SAndroid Build Coastguard Worker                               EncodedLengthUnits<char>(encoded.size()) + encoded.size() + 1;
385*d57664e9SAndroid Build Coastguard Worker 
386*d57664e9SAndroid Build Coastguard Worker     char* data = out->NextBlock<char>(total_size);
387*d57664e9SAndroid Build Coastguard Worker 
388*d57664e9SAndroid Build Coastguard Worker     // First encode the UTF16 string length.
389*d57664e9SAndroid Build Coastguard Worker     data = EncodeLength(data, utf16_length);
390*d57664e9SAndroid Build Coastguard Worker 
391*d57664e9SAndroid Build Coastguard Worker     // Now encode the size of the real UTF8 string.
392*d57664e9SAndroid Build Coastguard Worker     data = EncodeLength(data, encoded.size());
393*d57664e9SAndroid Build Coastguard Worker     strncpy(data, encoded.data(), encoded.size());
394*d57664e9SAndroid Build Coastguard Worker 
395*d57664e9SAndroid Build Coastguard Worker   } else {
396*d57664e9SAndroid Build Coastguard Worker     const std::u16string encoded = util::Utf8ToUtf16(str);
397*d57664e9SAndroid Build Coastguard Worker     const ssize_t utf16_length = encoded.size();
398*d57664e9SAndroid Build Coastguard Worker 
399*d57664e9SAndroid Build Coastguard Worker     // Make sure the length to be encoded does not exceed the maximum possible
400*d57664e9SAndroid Build Coastguard Worker     // length that can be encoded
401*d57664e9SAndroid Build Coastguard Worker     if (((size_t)utf16_length) > EncodeLengthMax<char16_t>()) {
402*d57664e9SAndroid Build Coastguard Worker       diag->Error(DiagMessage() << "string too large to encode using UTF-16 "
403*d57664e9SAndroid Build Coastguard Worker                                 << "written instead as '" << kStringTooLarge << "'");
404*d57664e9SAndroid Build Coastguard Worker 
405*d57664e9SAndroid Build Coastguard Worker       EncodeString(kStringTooLarge, utf8, out, diag);
406*d57664e9SAndroid Build Coastguard Worker       return false;
407*d57664e9SAndroid Build Coastguard Worker     }
408*d57664e9SAndroid Build Coastguard Worker 
409*d57664e9SAndroid Build Coastguard Worker     // Total number of 16-bit words to write.
410*d57664e9SAndroid Build Coastguard Worker     const size_t total_size = EncodedLengthUnits<char16_t>(utf16_length) + encoded.size() + 1;
411*d57664e9SAndroid Build Coastguard Worker 
412*d57664e9SAndroid Build Coastguard Worker     char16_t* data = out->NextBlock<char16_t>(total_size);
413*d57664e9SAndroid Build Coastguard Worker 
414*d57664e9SAndroid Build Coastguard Worker     // Encode the actual UTF16 string length.
415*d57664e9SAndroid Build Coastguard Worker     data = EncodeLength(data, utf16_length);
416*d57664e9SAndroid Build Coastguard Worker     const size_t byte_length = encoded.size() * sizeof(char16_t);
417*d57664e9SAndroid Build Coastguard Worker 
418*d57664e9SAndroid Build Coastguard Worker     // NOTE: For some reason, strncpy16(data, entry->value.data(),
419*d57664e9SAndroid Build Coastguard Worker     // entry->value.size()) truncates the string.
420*d57664e9SAndroid Build Coastguard Worker     memcpy(data, encoded.data(), byte_length);
421*d57664e9SAndroid Build Coastguard Worker 
422*d57664e9SAndroid Build Coastguard Worker     // The null-terminating character is already here due to the block of data
423*d57664e9SAndroid Build Coastguard Worker     // being set to 0s on allocation.
424*d57664e9SAndroid Build Coastguard Worker   }
425*d57664e9SAndroid Build Coastguard Worker 
426*d57664e9SAndroid Build Coastguard Worker   return true;
427*d57664e9SAndroid Build Coastguard Worker }
428*d57664e9SAndroid Build Coastguard Worker 
Flatten(BigBuffer * out,const StringPool & pool,bool utf8,IDiagnostics * diag)429*d57664e9SAndroid Build Coastguard Worker bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag) {
430*d57664e9SAndroid Build Coastguard Worker   bool no_error = true;
431*d57664e9SAndroid Build Coastguard Worker   const size_t start_index = out->size();
432*d57664e9SAndroid Build Coastguard Worker   android::ResStringPool_header* header = out->NextBlock<android::ResStringPool_header>();
433*d57664e9SAndroid Build Coastguard Worker   header->header.type = util::HostToDevice16(android::RES_STRING_POOL_TYPE);
434*d57664e9SAndroid Build Coastguard Worker   header->header.headerSize = util::HostToDevice16(sizeof(*header));
435*d57664e9SAndroid Build Coastguard Worker   header->stringCount = util::HostToDevice32(pool.size());
436*d57664e9SAndroid Build Coastguard Worker   header->styleCount = util::HostToDevice32(pool.styles_.size());
437*d57664e9SAndroid Build Coastguard Worker   if (utf8) {
438*d57664e9SAndroid Build Coastguard Worker     header->flags |= android::ResStringPool_header::UTF8_FLAG;
439*d57664e9SAndroid Build Coastguard Worker   }
440*d57664e9SAndroid Build Coastguard Worker 
441*d57664e9SAndroid Build Coastguard Worker   uint32_t* indices = pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr;
442*d57664e9SAndroid Build Coastguard Worker   uint32_t* style_indices =
443*d57664e9SAndroid Build Coastguard Worker       pool.styles_.size() != 0 ? out->NextBlock<uint32_t>(pool.styles_.size()) : nullptr;
444*d57664e9SAndroid Build Coastguard Worker 
445*d57664e9SAndroid Build Coastguard Worker   const size_t before_strings_index = out->size();
446*d57664e9SAndroid Build Coastguard Worker   header->stringsStart = before_strings_index - start_index;
447*d57664e9SAndroid Build Coastguard Worker 
448*d57664e9SAndroid Build Coastguard Worker   // Styles always come first.
449*d57664e9SAndroid Build Coastguard Worker   for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
450*d57664e9SAndroid Build Coastguard Worker     *indices++ = out->size() - before_strings_index;
451*d57664e9SAndroid Build Coastguard Worker     no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
452*d57664e9SAndroid Build Coastguard Worker   }
453*d57664e9SAndroid Build Coastguard Worker 
454*d57664e9SAndroid Build Coastguard Worker   for (const std::unique_ptr<Entry>& entry : pool.strings_) {
455*d57664e9SAndroid Build Coastguard Worker     *indices++ = out->size() - before_strings_index;
456*d57664e9SAndroid Build Coastguard Worker     no_error = EncodeString(entry->value, utf8, out, diag) && no_error;
457*d57664e9SAndroid Build Coastguard Worker   }
458*d57664e9SAndroid Build Coastguard Worker 
459*d57664e9SAndroid Build Coastguard Worker   out->Align4();
460*d57664e9SAndroid Build Coastguard Worker 
461*d57664e9SAndroid Build Coastguard Worker   if (style_indices != nullptr) {
462*d57664e9SAndroid Build Coastguard Worker     const size_t before_styles_index = out->size();
463*d57664e9SAndroid Build Coastguard Worker     header->stylesStart = util::HostToDevice32(before_styles_index - start_index);
464*d57664e9SAndroid Build Coastguard Worker 
465*d57664e9SAndroid Build Coastguard Worker     for (const std::unique_ptr<StyleEntry>& entry : pool.styles_) {
466*d57664e9SAndroid Build Coastguard Worker       *style_indices++ = out->size() - before_styles_index;
467*d57664e9SAndroid Build Coastguard Worker 
468*d57664e9SAndroid Build Coastguard Worker       if (!entry->spans.empty()) {
469*d57664e9SAndroid Build Coastguard Worker         android::ResStringPool_span* span =
470*d57664e9SAndroid Build Coastguard Worker             out->NextBlock<android::ResStringPool_span>(entry->spans.size());
471*d57664e9SAndroid Build Coastguard Worker         for (const Span& s : entry->spans) {
472*d57664e9SAndroid Build Coastguard Worker           span->name.index = util::HostToDevice32(s.name.index());
473*d57664e9SAndroid Build Coastguard Worker           span->firstChar = util::HostToDevice32(s.first_char);
474*d57664e9SAndroid Build Coastguard Worker           span->lastChar = util::HostToDevice32(s.last_char);
475*d57664e9SAndroid Build Coastguard Worker           span++;
476*d57664e9SAndroid Build Coastguard Worker         }
477*d57664e9SAndroid Build Coastguard Worker       }
478*d57664e9SAndroid Build Coastguard Worker 
479*d57664e9SAndroid Build Coastguard Worker       uint32_t* spanEnd = out->NextBlock<uint32_t>();
480*d57664e9SAndroid Build Coastguard Worker       *spanEnd = android::ResStringPool_span::END;
481*d57664e9SAndroid Build Coastguard Worker     }
482*d57664e9SAndroid Build Coastguard Worker 
483*d57664e9SAndroid Build Coastguard Worker     // The error checking code in the platform looks for an entire
484*d57664e9SAndroid Build Coastguard Worker     // ResStringPool_span structure worth of 0xFFFFFFFF at the end
485*d57664e9SAndroid Build Coastguard Worker     // of the style block, so fill in the remaining 2 32bit words
486*d57664e9SAndroid Build Coastguard Worker     // with 0xFFFFFFFF.
487*d57664e9SAndroid Build Coastguard Worker     const size_t padding_length =
488*d57664e9SAndroid Build Coastguard Worker         sizeof(android::ResStringPool_span) - sizeof(android::ResStringPool_span::name);
489*d57664e9SAndroid Build Coastguard Worker     uint8_t* padding = out->NextBlock<uint8_t>(padding_length);
490*d57664e9SAndroid Build Coastguard Worker     memset(padding, 0xff, padding_length);
491*d57664e9SAndroid Build Coastguard Worker     out->Align4();
492*d57664e9SAndroid Build Coastguard Worker   }
493*d57664e9SAndroid Build Coastguard Worker   header->header.size = util::HostToDevice32(out->size() - start_index);
494*d57664e9SAndroid Build Coastguard Worker   return no_error;
495*d57664e9SAndroid Build Coastguard Worker }
496*d57664e9SAndroid Build Coastguard Worker 
FlattenUtf8(BigBuffer * out,const StringPool & pool,IDiagnostics * diag)497*d57664e9SAndroid Build Coastguard Worker bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
498*d57664e9SAndroid Build Coastguard Worker   return Flatten(out, pool, true, diag);
499*d57664e9SAndroid Build Coastguard Worker }
500*d57664e9SAndroid Build Coastguard Worker 
FlattenUtf16(BigBuffer * out,const StringPool & pool,IDiagnostics * diag)501*d57664e9SAndroid Build Coastguard Worker bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag) {
502*d57664e9SAndroid Build Coastguard Worker   return Flatten(out, pool, false, diag);
503*d57664e9SAndroid Build Coastguard Worker }
504*d57664e9SAndroid Build Coastguard Worker 
505*d57664e9SAndroid Build Coastguard Worker }  // namespace android
506