Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 17 | #include "format/binary/XmlFlattener.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 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 Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 28 | #include "ResourceUtils.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "SdkConstants.h" |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 30 | #include "ValueVisitor.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 31 | #include "format/binary/ChunkWriter.h" |
| 32 | #include "format/binary/ResourceTypeExtensions.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 33 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | using namespace android; |
| 36 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 37 | using ::aapt::ResourceUtils::StringBuilder; |
| 38 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | namespace aapt { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | constexpr uint32_t kLowPriority = 0xffffffffu; |
| 44 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 45 | static bool cmp_xml_attribute_by_id(const xml::Attribute* a, const xml::Attribute* b) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | if (a->compiled_attribute && a->compiled_attribute.value().id) { |
| 47 | if (b->compiled_attribute && b->compiled_attribute.value().id) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 48 | return a->compiled_attribute.value().id.value() < b->compiled_attribute.value().id.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | } |
| 50 | return true; |
| 51 | } else if (!b->compiled_attribute) { |
| 52 | int diff = a->namespace_uri.compare(b->namespace_uri); |
| 53 | if (diff < 0) { |
| 54 | return true; |
| 55 | } else if (diff > 0) { |
| 56 | return false; |
| 57 | } |
| 58 | return a->name < b->name; |
| 59 | } |
| 60 | return false; |
| 61 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 63 | class XmlFlattenerVisitor : public xml::ConstVisitor { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 65 | using xml::ConstVisitor::Visit; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | |
| 67 | StringPool pool; |
| 68 | std::map<uint8_t, StringPool> package_pools; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | struct StringFlattenDest { |
| 71 | StringPool::Ref ref; |
| 72 | ResStringPool_ref* dest; |
| 73 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | std::vector<StringFlattenDest> string_refs; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options) |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 78 | : buffer_(buffer), options_(options) { |
| 79 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 81 | void Visit(const xml::Text* node) override { |
Ryan Mitchell | 0dcb20c | 2018-04-27 14:53:04 -0700 | [diff] [blame] | 82 | std::string text = util::TrimWhitespace(node->text).to_string(); |
| 83 | |
| 84 | // Skip whitespace only text nodes. |
| 85 | if (text.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | return; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Ryan Mitchell | 0dcb20c | 2018-04-27 14:53:04 -0700 | [diff] [blame] | 89 | // Compact leading and trailing whitespace into a single space |
| 90 | if (isspace(node->text[0])) { |
| 91 | text = ' ' + text; |
| 92 | } |
| 93 | if (isspace(node->text[node->text.length() - 1])) { |
| 94 | text = text + ' '; |
| 95 | } |
| 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | ChunkWriter writer(buffer_); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 98 | ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | flat_node->lineNumber = util::HostToDevice32(node->line_number); |
| 100 | flat_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 102 | // Process plain strings to make sure they get properly escaped. |
Ryan Mitchell | 0dcb20c | 2018-04-27 14:53:04 -0700 | [diff] [blame] | 103 | text = StringBuilder(true /*preserve_spaces*/).AppendText(text).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | |
Ryan Mitchell | 0dcb20c | 2018-04-27 14:53:04 -0700 | [diff] [blame] | 105 | ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>(); |
| 106 | AddString(text, kLowPriority, &flat_text->data); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 110 | void Visit(const xml::Element* node) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 111 | for (const xml::NamespaceDecl& decl : node->namespace_decls) { |
| 112 | // Skip dedicated tools namespace. |
| 113 | if (decl.uri != xml::kSchemaTools) { |
| 114 | WriteNamespace(decl, android::RES_XML_START_NAMESPACE_TYPE); |
| 115 | } |
| 116 | } |
| 117 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | ChunkWriter start_writer(buffer_); |
| 120 | ResXMLTree_node* flat_node = |
| 121 | start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE); |
| 122 | flat_node->lineNumber = util::HostToDevice32(node->line_number); |
| 123 | flat_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 125 | ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 127 | // A missing namespace must be null, not an empty string. Otherwise the runtime complains. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | AddString(node->namespace_uri, kLowPriority, &flat_elem->ns, |
| 129 | true /* treat_empty_string_as_null */); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 130 | AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem)); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 133 | flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | WriteAttributes(node, flat_elem, &start_writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | start_writer.Finish(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 140 | xml::ConstVisitor::Visit(node); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 141 | |
| 142 | { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | ChunkWriter end_writer(buffer_); |
| 144 | ResXMLTree_node* flat_end_node = |
| 145 | end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE); |
| 146 | flat_end_node->lineNumber = util::HostToDevice32(node->line_number); |
| 147 | flat_end_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 149 | ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns, |
| 151 | true /* treat_empty_string_as_null */); |
| 152 | AddString(node->name, kLowPriority, &flat_end_elem->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | end_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 156 | |
| 157 | for (auto iter = node->namespace_decls.rbegin(); iter != node->namespace_decls.rend(); ++iter) { |
| 158 | // Skip dedicated tools namespace. |
| 159 | if (iter->uri != xml::kSchemaTools) { |
| 160 | WriteNamespace(*iter, android::RES_XML_END_NAMESPACE_TYPE); |
| 161 | } |
| 162 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | private: |
| 166 | DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor); |
| 167 | |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 168 | // We are adding strings to a StringPool whose strings will be sorted and merged with other |
| 169 | // string pools. That means we can't encode the ID of a string directly. Instead, we defer the |
| 170 | // writing of the ID here, until after the StringPool is merged and sorted. |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 171 | void AddString(const StringPiece& str, uint32_t priority, android::ResStringPool_ref* dest, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | bool treat_empty_string_as_null = false) { |
| 173 | if (str.empty() && treat_empty_string_as_null) { |
| 174 | // Some parts of the runtime treat null differently than empty string. |
| 175 | dest->index = util::DeviceToHost32(-1); |
| 176 | } else { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 177 | string_refs.push_back( |
| 178 | StringFlattenDest{pool.MakeRef(str, StringPool::Context(priority)), dest}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 182 | // We are adding strings to a StringPool whose strings will be sorted and merged with other |
| 183 | // string pools. That means we can't encode the ID of a string directly. Instead, we defer the |
| 184 | // writing of the ID here, until after the StringPool is merged and sorted. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) { |
| 186 | string_refs.push_back(StringFlattenDest{ref, dest}); |
| 187 | } |
| 188 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 189 | void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | ChunkWriter writer(buffer_); |
| 191 | |
| 192 | ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 193 | flatNode->lineNumber = util::HostToDevice32(decl.line_number); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | flatNode->comment.index = util::HostToDevice32(-1); |
| 195 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 196 | ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 197 | AddString(decl.prefix, kLowPriority, &flat_ns->prefix); |
| 198 | AddString(decl.uri, kLowPriority, &flat_ns->uri); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | |
| 200 | writer.Finish(); |
| 201 | } |
| 202 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 203 | void WriteAttributes(const xml::Element* node, ResXMLTree_attrExt* flat_elem, |
| 204 | ChunkWriter* writer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | filtered_attrs_.clear(); |
| 206 | filtered_attrs_.reserve(node->attributes.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | |
| 208 | // Filter the attributes. |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 209 | for (const xml::Attribute& attr : node->attributes) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 210 | if (attr.namespace_uri != xml::kSchemaTools) { |
| 211 | filtered_attrs_.push_back(&attr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | if (filtered_attrs_.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | const ResourceId kIdAttr(0x010100d0); |
| 220 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 221 | std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | ResXMLTree_attribute* flat_attr = |
| 226 | writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size()); |
| 227 | uint16_t attribute_index = 1; |
| 228 | for (const xml::Attribute* xml_attr : filtered_attrs_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | // Assign the indices for specific attributes. |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 230 | if (xml_attr->compiled_attribute && xml_attr->compiled_attribute.value().id && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | xml_attr->compiled_attribute.value().id.value() == kIdAttr) { |
| 232 | flat_elem->idIndex = util::HostToDevice16(attribute_index); |
| 233 | } else if (xml_attr->namespace_uri.empty()) { |
| 234 | if (xml_attr->name == "class") { |
| 235 | flat_elem->classIndex = util::HostToDevice16(attribute_index); |
| 236 | } else if (xml_attr->name == "style") { |
| 237 | flat_elem->styleIndex = util::HostToDevice16(attribute_index); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | attribute_index++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 242 | // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | // is empty (doesn't exist). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns, |
| 245 | true /* treat_empty_string_as_null */); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | flat_attr->rawValue.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 249 | if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) { |
| 250 | // The attribute has no associated ResourceID, so the string order doesn't matter. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | AddString(xml_attr->name, kLowPriority, &flat_attr->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | } else { |
| 253 | // Attribute names are stored without packages, but we use |
| 254 | // their StringPool index to lookup their resource IDs. |
| 255 | // This will cause collisions, so we can't dedupe |
| 256 | // attribute names from different packages. We use separate |
| 257 | // pools that we later combine. |
| 258 | // |
| 259 | // Lookup the StringPool for this package and make the reference there. |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 260 | const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 262 | StringPool::Ref name_ref = package_pools[aapt_attr.id.value().package_id()].MakeRef( |
| 263 | xml_attr->name, StringPool::Context(aapt_attr.id.value().id)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | |
| 265 | // Add it to the list of strings to flatten. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | AddString(name_ref, &flat_attr->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 269 | std::string processed_str; |
| 270 | Maybe<StringPiece> compiled_text; |
| 271 | if (xml_attr->compiled_value != nullptr) { |
| 272 | // Make sure we're not flattening a String. A String can be referencing a string from |
| 273 | // a different StringPool than we're using here to build the binary XML. |
| 274 | String* string_value = ValueCast<String>(xml_attr->compiled_value.get()); |
| 275 | if (string_value != nullptr) { |
| 276 | // Mark the String's text as needing to be serialized. |
| 277 | compiled_text = StringPiece(*string_value->value); |
| 278 | } else { |
| 279 | // Serialize this compiled value safely. |
| 280 | CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue)); |
| 281 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | } else { |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 283 | // There is no compiled value, so treat the raw string as compiled, once it is processed to |
| 284 | // make sure escape sequences are properly interpreted. |
| 285 | processed_str = |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 286 | StringBuilder(true /*preserve_spaces*/).AppendText(xml_attr->value).to_string(); |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 287 | compiled_text = StringPiece(processed_str); |
| 288 | } |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 289 | |
Adam Lesinski | e1094a2 | 2018-02-22 17:27:17 -0800 | [diff] [blame] | 290 | if (compiled_text) { |
| 291 | // Write out the compiled text and raw_text. |
| 292 | flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING; |
| 293 | AddString(compiled_text.value(), kLowPriority, |
| 294 | reinterpret_cast<ResStringPool_ref*>(&flat_attr->typedValue.data)); |
| 295 | if (options_.keep_raw_values) { |
| 296 | AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue); |
| 297 | } else { |
| 298 | AddString(compiled_text.value(), kLowPriority, &flat_attr->rawValue); |
| 299 | } |
| 300 | } else if (options_.keep_raw_values && !xml_attr->value.empty()) { |
| 301 | AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 304 | flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | flat_attr++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | |
| 309 | BigBuffer* buffer_; |
| 310 | XmlFlattenerOptions options_; |
| 311 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 312 | // Scratch vector to filter attributes. We avoid allocations making this a member. |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 313 | std::vector<const xml::Attribute*> filtered_attrs_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | } // namespace |
| 317 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 318 | bool XmlFlattener::Flatten(IAaptContext* context, const xml::Node* node) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | BigBuffer node_buffer(1024); |
| 320 | XmlFlattenerVisitor visitor(&node_buffer, options_); |
| 321 | node->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 | |
| 323 | // Merge the package pools into the main pool. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | for (auto& package_pool_entry : visitor.package_pools) { |
| 325 | visitor.pool.Merge(std::move(package_pool_entry.second)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Sort the string pool so that attribute resource IDs show up first. |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 329 | visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { |
| 330 | return util::compare(a.priority, b.priority); |
| 331 | }); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 332 | |
| 333 | // Now we flatten the string pool references into the correct places. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | for (const auto& ref_entry : visitor.string_refs) { |
| 335 | ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Write the XML header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | ChunkWriter xml_header_writer(buffer_); |
| 340 | xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | |
| 342 | // Flatten the StringPool. |
Adam Lesinski | ea13c1a | 2017-10-13 12:40:37 -0700 | [diff] [blame] | 343 | if (options_.use_utf16) { |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 344 | StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics()); |
Adam Lesinski | ea13c1a | 2017-10-13 12:40:37 -0700 | [diff] [blame] | 345 | } else { |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 346 | StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics()); |
Adam Lesinski | ea13c1a | 2017-10-13 12:40:37 -0700 | [diff] [blame] | 347 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | |
| 349 | { |
| 350 | // Write the array of resource IDs, indexed by StringPool order. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | ChunkWriter res_id_map_writer(buffer_); |
| 352 | res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 353 | for (const auto& str : visitor.pool.strings()) { |
| 354 | ResourceId id(str->context.priority); |
| 355 | if (str->context.priority == kLowPriority || !id.is_valid()) { |
| 356 | // When we see the first non-resource ID, we're done. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 357 | break; |
| 358 | } |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 359 | *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | res_id_map_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Move the nodeBuffer and append it to the out buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | buffer_->AppendBuffer(std::move(node_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 | |
| 367 | // Finish the xml header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | xml_header_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 372 | bool XmlFlattener::Consume(IAaptContext* context, const xml::XmlResource* resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 373 | if (!resource->root) { |
| 374 | return false; |
| 375 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | return Flatten(context, resource->root.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 379 | } // namespace aapt |