1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "format/binary/ResEntryWriter.h"
18
19 #include "ValueVisitor.h"
20 #include "androidfw/BigBuffer.h"
21 #include "androidfw/ResourceTypes.h"
22 #include "androidfw/Util.h"
23 #include "format/binary/ResourceTypeExtensions.h"
24
25 namespace aapt {
26
27 struct less_style_entries {
operator ()aapt::less_style_entries28 bool operator()(const Style::Entry* a, const Style::Entry* b) const {
29 if (a->key.id) {
30 if (b->key.id) {
31 return cmp_ids_dynamic_after_framework(a->key.id.value(), b->key.id.value());
32 }
33 return true;
34 }
35 if (!b->key.id) {
36 return a->key.name.value() < b->key.name.value();
37 }
38 return false;
39 }
40 };
41
42 class MapFlattenVisitor : public ConstValueVisitor {
43 public:
44 using ConstValueVisitor::Visit;
45
MapFlattenVisitor(ResTable_entry_ext * out_entry,BigBuffer * buffer)46 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
47 : out_entry_(out_entry), buffer_(buffer) {
48 }
49
Visit(const Attribute * attr)50 void Visit(const Attribute* attr) override {
51 {
52 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
53 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
54 FlattenEntry(&key, &val);
55 }
56
57 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
58 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
59 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->min_int));
60 FlattenEntry(&key, &val);
61 }
62
63 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
64 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
65 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->max_int));
66 FlattenEntry(&key, &val);
67 }
68
69 for (const Attribute::Symbol& s : attr->symbols) {
70 BinaryPrimitive val(s.type, s.value);
71 FlattenEntry(&s.symbol, &val);
72 }
73 }
74
Visit(const Style * style)75 void Visit(const Style* style) override {
76 if (style->parent) {
77 const Reference& parent_ref = style->parent.value();
78 CHECK(bool(parent_ref.id)) << "parent has no ID";
79 out_entry_->parent.ident = android::util::HostToDevice32(parent_ref.id.value().id);
80 }
81
82 // Sort the style.
83 std::vector<const Style::Entry*> sorted_entries;
84 for (const auto& entry : style->entries) {
85 sorted_entries.emplace_back(&entry);
86 }
87
88 std::sort(sorted_entries.begin(), sorted_entries.end(), less_style_entries());
89
90 for (const Style::Entry* entry : sorted_entries) {
91 FlattenEntry(&entry->key, entry->value.get());
92 }
93 }
94
Visit(const Styleable * styleable)95 void Visit(const Styleable* styleable) override {
96 for (auto& attr_ref : styleable->entries) {
97 BinaryPrimitive val(Res_value{});
98 FlattenEntry(&attr_ref, &val);
99 }
100 }
101
Visit(const Array * array)102 void Visit(const Array* array) override {
103 const size_t count = array->elements.size();
104 for (size_t i = 0; i < count; i++) {
105 Reference key(android::ResTable_map::ATTR_MIN + i);
106 FlattenEntry(&key, array->elements[i].get());
107 }
108 }
109
Visit(const Plural * plural)110 void Visit(const Plural* plural) override {
111 const size_t count = plural->values.size();
112 for (size_t i = 0; i < count; i++) {
113 if (!plural->values[i]) {
114 continue;
115 }
116
117 ResourceId q;
118 switch (i) {
119 case Plural::Zero:
120 q.id = android::ResTable_map::ATTR_ZERO;
121 break;
122
123 case Plural::One:
124 q.id = android::ResTable_map::ATTR_ONE;
125 break;
126
127 case Plural::Two:
128 q.id = android::ResTable_map::ATTR_TWO;
129 break;
130
131 case Plural::Few:
132 q.id = android::ResTable_map::ATTR_FEW;
133 break;
134
135 case Plural::Many:
136 q.id = android::ResTable_map::ATTR_MANY;
137 break;
138
139 case Plural::Other:
140 q.id = android::ResTable_map::ATTR_OTHER;
141 break;
142
143 default:
144 LOG(FATAL) << "unhandled plural type";
145 break;
146 }
147
148 Reference key(q);
149 FlattenEntry(&key, plural->values[i].get());
150 }
151 }
152
153 /**
154 * Call this after visiting a Value. This will finish any work that
155 * needs to be done to prepare the entry.
156 */
Finish()157 void Finish() {
158 out_entry_->count = android::util::HostToDevice32(entry_count_);
159 }
160
161 private:
162 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
163
FlattenKey(const Reference * key,ResTable_map * out_entry)164 void FlattenKey(const Reference* key, ResTable_map* out_entry) {
165 CHECK(bool(key->id)) << "key has no ID";
166 out_entry->name.ident = android::util::HostToDevice32(key->id.value().id);
167 }
168
FlattenValue(const Item * value,ResTable_map * out_entry)169 void FlattenValue(const Item* value, ResTable_map* out_entry) {
170 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
171 }
172
FlattenEntry(const Reference * key,Item * value)173 void FlattenEntry(const Reference* key, Item* value) {
174 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
175 FlattenKey(key, out_entry);
176 FlattenValue(value, out_entry);
177 out_entry->value.size = android::util::HostToDevice16(sizeof(out_entry->value));
178 entry_count_++;
179 }
180
181 ResTable_entry_ext* out_entry_;
182 BigBuffer* buffer_;
183 size_t entry_count_ = 0;
184 };
185
186 template <typename T>
WriteEntry(const FlatEntry * entry,T * out_result,bool compact=false)187 void WriteEntry(const FlatEntry* entry, T* out_result, bool compact = false) {
188 static_assert(std::is_same_v<ResTable_entry, T> || std::is_same_v<ResTable_entry_ext, T>,
189 "T must be ResTable_entry or ResTable_entry_ext");
190
191 ResTable_entry* out_entry = (ResTable_entry*)out_result;
192 uint16_t flags = 0;
193
194 if (entry->entry->visibility.level == Visibility::Level::kPublic) {
195 flags |= ResTable_entry::FLAG_PUBLIC;
196 }
197
198 if (entry->value->IsWeak()) {
199 flags |= ResTable_entry::FLAG_WEAK;
200 }
201
202 if constexpr (std::is_same_v<ResTable_entry_ext, T>) {
203 flags |= ResTable_entry::FLAG_COMPLEX;
204 }
205
206 if (!compact) {
207 out_entry->full.flags = android::util::HostToDevice16(flags);
208 out_entry->full.key.index = android::util::HostToDevice32(entry->entry_key);
209 out_entry->full.size = android::util::HostToDevice16(sizeof(T));
210 } else {
211 Res_value value;
212 CHECK(entry->entry_key < 0xffffu) << "cannot encode key in 16-bit";
213 CHECK(compact && (std::is_same_v<ResTable_entry, T>)) << "cannot encode complex entry";
214 CHECK(ValueCast<Item>(entry->value)->Flatten(&value)) << "flatten failed";
215
216 flags |= ResTable_entry::FLAG_COMPACT | (value.dataType << 8);
217 out_entry->compact.flags = android::util::HostToDevice16(flags);
218 out_entry->compact.key = android::util::HostToDevice16(entry->entry_key);
219 out_entry->compact.data = value.data;
220 }
221 }
222
WriteMapToBuffer(const FlatEntry * map_entry,BigBuffer * buffer)223 int32_t WriteMapToBuffer(const FlatEntry* map_entry, BigBuffer* buffer) {
224 int32_t offset = buffer->size();
225 ResTable_entry_ext* out_entry = buffer->NextBlock<ResTable_entry_ext>();
226 WriteEntry<ResTable_entry_ext>(map_entry, out_entry);
227
228 MapFlattenVisitor visitor(out_entry, buffer);
229 map_entry->value->Accept(&visitor);
230 visitor.Finish();
231 return offset;
232 }
233
234 template <bool compact_entry, typename T>
WriteItemToBuffer(const FlatEntry * item_entry,BigBuffer * buffer)235 std::pair<int32_t, T*> WriteItemToBuffer(const FlatEntry* item_entry, BigBuffer* buffer) {
236 int32_t offset = buffer->size();
237 T* out_entry = buffer->NextBlock<T>();
238
239 if constexpr (compact_entry) {
240 WriteEntry(item_entry, out_entry, true);
241 } else {
242 WriteEntry(item_entry, &out_entry->entry);
243 CHECK(ValueCast<Item>(item_entry->value)->Flatten(&out_entry->value)) << "flatten failed";
244 out_entry->value.size = android::util::HostToDevice16(sizeof(out_entry->value));
245 }
246 return {offset, out_entry};
247 }
248
249 // explicitly specialize both versions
250 template std::pair<int32_t, ResEntryValue<false>*> WriteItemToBuffer<false>(
251 const FlatEntry* item_entry, BigBuffer* buffer);
252
253 template std::pair<int32_t, ResEntryValue<true>*> WriteItemToBuffer<true>(
254 const FlatEntry* item_entry, BigBuffer* buffer);
255
256 } // namespace aapt
257