blob: 366c373223dc8212ad0d7074f14ccfc041e19144 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "flatten/XmlFlattener.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
20#include <map>
21#include <vector>
22
23#include "android-base/logging.h"
24#include "android-base/macros.h"
25#include "androidfw/ResourceTypes.h"
26#include "utils/misc.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "SdkConstants.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "flatten/ChunkWriter.h"
30#include "flatten/ResourceTypeExtensions.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033using namespace android;
34
35namespace aapt {
36
37namespace {
38
39constexpr uint32_t kLowPriority = 0xffffffffu;
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041static bool cmp_xml_attribute_by_id(const xml::Attribute* a,
42 const xml::Attribute* b) {
43 if (a->compiled_attribute && a->compiled_attribute.value().id) {
44 if (b->compiled_attribute && b->compiled_attribute.value().id) {
45 return a->compiled_attribute.value().id.value() <
46 b->compiled_attribute.value().id.value();
47 }
48 return true;
49 } else if (!b->compiled_attribute) {
50 int diff = a->namespace_uri.compare(b->namespace_uri);
51 if (diff < 0) {
52 return true;
53 } else if (diff > 0) {
54 return false;
55 }
56 return a->name < b->name;
57 }
58 return false;
59}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061class XmlFlattenerVisitor : public xml::Visitor {
62 public:
63 using xml::Visitor::Visit;
64
65 StringPool pool;
66 std::map<uint8_t, StringPool> package_pools;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 struct StringFlattenDest {
69 StringPool::Ref ref;
70 ResStringPool_ref* dest;
71 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::vector<StringFlattenDest> string_refs;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 : buffer_(buffer), options_(options) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 void Visit(xml::Namespace* node) override {
79 if (node->namespace_uri == xml::kSchemaTools) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 // Skip dedicated tools namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 xml::Visitor::Visit(node);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 WriteNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
84 xml::Visitor::Visit(node);
85 WriteNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
87 }
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 void Visit(xml::Text* node) override {
90 if (util::TrimWhitespace(node->text).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 // Skip whitespace only text nodes.
92 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 }
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 ChunkWriter writer(buffer_);
96 ResXMLTree_node* flat_node =
97 writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
98 flat_node->lineNumber = util::HostToDevice32(node->line_number);
99 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
102 AddString(node->text, kLowPriority, &flat_text->data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 void Visit(xml::Element* node) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 ChunkWriter start_writer(buffer_);
110 ResXMLTree_node* flat_node =
111 start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
112 flat_node->lineNumber = util::HostToDevice32(node->line_number);
113 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 ResXMLTree_attrExt* flat_elem =
116 start_writer.NextBlock<ResXMLTree_attrExt>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117
118 // A missing namespace must be null, not an empty string. Otherwise the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 // runtime complains.
120 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
121 true /* treat_empty_string_as_null */);
122 AddString(node->name, kLowPriority, &flat_elem->name,
123 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
126 flat_elem->attributeSize =
127 util::HostToDevice16(sizeof(ResXMLTree_attribute));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 WriteAttributes(node, flat_elem, &start_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 start_writer.Finish();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132 }
133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 xml::Visitor::Visit(node);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
136 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ChunkWriter end_writer(buffer_);
138 ResXMLTree_node* flat_end_node =
139 end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
140 flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
141 flat_end_node->comment.index = util::HostToDevice32(-1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 ResXMLTree_endElementExt* flat_end_elem =
144 end_writer.NextBlock<ResXMLTree_endElementExt>();
145 AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
146 true /* treat_empty_string_as_null */);
147 AddString(node->name, kLowPriority, &flat_end_elem->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 end_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151 }
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 private:
154 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
155
156 void AddString(const StringPiece& str, uint32_t priority,
157 android::ResStringPool_ref* dest,
158 bool treat_empty_string_as_null = false) {
159 if (str.empty() && treat_empty_string_as_null) {
160 // Some parts of the runtime treat null differently than empty string.
161 dest->index = util::DeviceToHost32(-1);
162 } else {
163 string_refs.push_back(StringFlattenDest{
164 pool.MakeRef(str, StringPool::Context(priority)), dest});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
169 string_refs.push_back(StringFlattenDest{ref, dest});
170 }
171
172 void WriteNamespace(xml::Namespace* node, uint16_t type) {
173 ChunkWriter writer(buffer_);
174
175 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
176 flatNode->lineNumber = util::HostToDevice32(node->line_number);
177 flatNode->comment.index = util::HostToDevice32(-1);
178
179 ResXMLTree_namespaceExt* flat_ns =
180 writer.NextBlock<ResXMLTree_namespaceExt>();
181 AddString(node->namespace_prefix, kLowPriority, &flat_ns->prefix);
182 AddString(node->namespace_uri, kLowPriority, &flat_ns->uri);
183
184 writer.Finish();
185 }
186
187 void WriteAttributes(xml::Element* node, ResXMLTree_attrExt* flat_elem,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 ChunkWriter* writer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 filtered_attrs_.clear();
190 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191
192 // Filter the attributes.
193 for (xml::Attribute& attr : node->attributes) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 if (options_.max_sdk_level && attr.compiled_attribute &&
195 attr.compiled_attribute.value().id) {
196 size_t sdk_level =
197 FindAttributeSdkLevel(attr.compiled_attribute.value().id.value());
198 if (sdk_level > options_.max_sdk_level.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 if (attr.namespace_uri == xml::kSchemaTools) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 continue;
204 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 filtered_attrs_.push_back(&attr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206 }
207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 return;
210 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 const ResourceId kIdAttr(0x010100d0);
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(),
215 cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 ResXMLTree_attribute* flat_attr =
220 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
221 uint16_t attribute_index = 1;
222 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 // Assign the indices for specific attributes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 if (xml_attr->compiled_attribute &&
225 xml_attr->compiled_attribute.value().id &&
226 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
227 flat_elem->idIndex = util::HostToDevice16(attribute_index);
228 } else if (xml_attr->namespace_uri.empty()) {
229 if (xml_attr->name == "class") {
230 flat_elem->classIndex = util::HostToDevice16(attribute_index);
231 } else if (xml_attr->name == "style") {
232 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 }
234 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236
237 // Add the namespaceUri to the list of StringRefs to encode. Use null if
238 // the namespace
239 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
241 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 if (!xml_attr->compiled_attribute ||
246 !xml_attr->compiled_attribute.value().id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // The attribute has no associated ResourceID, so the string order
248 // doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 } else {
251 // Attribute names are stored without packages, but we use
252 // their StringPool index to lookup their resource IDs.
253 // This will cause collisions, so we can't dedupe
254 // attribute names from different packages. We use separate
255 // pools that we later combine.
256 //
257 // Lookup the StringPool for this package and make the reference there.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 const xml::AaptAttribute& aapt_attr =
259 xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 StringPool::Ref name_ref =
262 package_pools[aapt_attr.id.value().package_id()].MakeRef(
263 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264
265 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 if (options_.keep_raw_values || !xml_attr->compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 // Keep raw values if the value is not compiled or
271 // if we're building a static library (need symbols).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 }
274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 if (xml_attr->compiled_value) {
276 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 } else {
278 // Flatten as a regular string type.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
280 AddString(xml_attr->value, kLowPriority,
281 (ResStringPool_ref*)&flat_attr->typedValue.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 }
283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 flat_attr->typedValue.size =
285 util::HostToDevice16(sizeof(flat_attr->typedValue));
286 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 }
288 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289
290 BigBuffer* buffer_;
291 XmlFlattenerOptions options_;
292
293 // Scratch vector to filter attributes. We avoid allocations
294 // making this a member.
295 std::vector<xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296};
297
298} // namespace
299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300bool XmlFlattener::Flatten(IAaptContext* context, xml::Node* node) {
301 BigBuffer node_buffer(1024);
302 XmlFlattenerVisitor visitor(&node_buffer, options_);
303 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304
305 // Merge the package pools into the main pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 for (auto& package_pool_entry : visitor.package_pools) {
307 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 }
309
310 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 visitor.pool.Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
313 return a.context.priority < b.context.priority;
314 });
315
316 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 for (const auto& ref_entry : visitor.string_refs) {
318 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 }
320
321 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 ChunkWriter xml_header_writer(buffer_);
323 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324
325 // Flatten the StringPool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 StringPool::FlattenUtf8(buffer_, visitor.pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327
328 {
329 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 ChunkWriter res_id_map_writer(buffer_);
331 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
332 for (const auto& str : visitor.pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 ResourceId id = {str->context.priority};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (id.id == kLowPriority || !id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 // When we see the first non-resource ID,
336 // we're done.
337 break;
338 }
339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 *res_id_map_writer.NextBlock<uint32_t>() = id.id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 }
344
345 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347
348 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351}
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 if (!resource->root) {
355 return false;
356 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700358}
359
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360} // namespace aapt