Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [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 | |
| 17 | #include "BigBuffer.h" |
| 18 | #include "Logger.h" |
| 19 | #include "Maybe.h" |
| 20 | #include "Resolver.h" |
| 21 | #include "Resource.h" |
| 22 | #include "ResourceParser.h" |
| 23 | #include "ResourceValues.h" |
| 24 | #include "SdkConstants.h" |
| 25 | #include "Source.h" |
| 26 | #include "StringPool.h" |
| 27 | #include "Util.h" |
| 28 | #include "XmlFlattener.h" |
| 29 | |
| 30 | #include <androidfw/ResourceTypes.h> |
| 31 | #include <limits> |
| 32 | #include <map> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace aapt { |
| 37 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 38 | constexpr const char16_t* kSchemaAndroid = u"http://schemas.android.com/apk/res/android"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | struct AttributeValueFlattener : ValueVisitor { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 41 | AttributeValueFlattener( |
| 42 | std::shared_ptr<IResolver> resolver, SourceLogger* logger, |
| 43 | android::Res_value* outValue, std::shared_ptr<XmlPullParser> parser, bool* outError, |
| 44 | StringPool::Ref rawValue, std::u16string* defaultPackage, |
| 45 | std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>>* outStringRefs) : |
| 46 | mResolver(resolver), mLogger(logger), mOutValue(outValue), mParser(parser), |
| 47 | mError(outError), mRawValue(rawValue), mDefaultPackage(defaultPackage), |
| 48 | mStringRefs(outStringRefs) { |
| 49 | } |
| 50 | |
| 51 | void visit(Reference& reference, ValueVisitorArgs&) override { |
| 52 | // First see if we can convert the package name from a prefix to a real |
| 53 | // package name. |
| 54 | ResourceName aliasedName = reference.name; |
| 55 | |
| 56 | if (!reference.name.package.empty()) { |
| 57 | // Only if we specified a package do we look for its alias. |
| 58 | mParser->applyPackageAlias(&reference.name.package, *mDefaultPackage); |
| 59 | } else { |
| 60 | reference.name.package = *mDefaultPackage; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 63 | Maybe<ResourceId> result = mResolver->findId(reference.name); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | if (!result || !result.value().isValid()) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 65 | std::ostream& out = mLogger->error(mParser->getLineNumber()) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | << "unresolved reference '" |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 67 | << aliasedName |
| 68 | << "'"; |
| 69 | if (aliasedName != reference.name) { |
| 70 | out << " (aka '" << reference.name << "')"; |
| 71 | } |
| 72 | out << "'." << std::endl; |
| 73 | *mError = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | } else { |
| 75 | reference.id = result.value(); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 76 | reference.flatten(*mOutValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 80 | void visit(String& string, ValueVisitorArgs&) override { |
| 81 | mOutValue->dataType = android::Res_value::TYPE_STRING; |
| 82 | mStringRefs->emplace_back( |
| 83 | mRawValue, |
| 84 | reinterpret_cast<android::ResStringPool_ref*>(mOutValue->data)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 87 | void visitItem(Item& item, ValueVisitorArgs&) override { |
| 88 | item.flatten(*mOutValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | std::shared_ptr<IResolver> mResolver; |
| 93 | SourceLogger* mLogger; |
| 94 | android::Res_value* mOutValue; |
| 95 | std::shared_ptr<XmlPullParser> mParser; |
| 96 | bool* mError; |
| 97 | StringPool::Ref mRawValue; |
| 98 | std::u16string* mDefaultPackage; |
| 99 | std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>>* mStringRefs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct XmlAttribute { |
| 103 | uint32_t resourceId; |
| 104 | const XmlPullParser::Attribute* xmlAttr; |
| 105 | const Attribute* attr; |
| 106 | StringPool::Ref nameRef; |
| 107 | }; |
| 108 | |
| 109 | static bool lessAttributeId(const XmlAttribute& a, uint32_t id) { |
| 110 | return a.resourceId < id; |
| 111 | } |
| 112 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 113 | XmlFlattener::XmlFlattener(const std::shared_ptr<ResourceTable>& table, |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 114 | const std::shared_ptr<IResolver>& resolver) : |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 115 | mTable(table), mResolver(resolver) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Reads events from the parser and writes to a BigBuffer. The binary XML file |
| 120 | * expects the StringPool to appear first, but we haven't collected the strings yet. We |
| 121 | * write to a temporary BigBuffer while parsing the input, adding strings we encounter |
| 122 | * to the StringPool. At the end, we write the StringPool to the given BigBuffer and |
| 123 | * then move the data from the temporary BigBuffer into the given one. This incurs no |
| 124 | * copies as the given BigBuffer simply takes ownership of the data. |
| 125 | */ |
| 126 | Maybe<size_t> XmlFlattener::flatten(const Source& source, |
| 127 | const std::shared_ptr<XmlPullParser>& parser, |
| 128 | BigBuffer* outBuffer, Options options) { |
| 129 | SourceLogger logger(source); |
| 130 | StringPool pool; |
| 131 | bool error = false; |
| 132 | |
| 133 | size_t smallestStrippedAttributeSdk = std::numeric_limits<size_t>::max(); |
| 134 | |
| 135 | // Attribute names are stored without packages, but we use |
| 136 | // their StringPool index to lookup their resource IDs. |
| 137 | // This will cause collisions, so we can't dedupe |
| 138 | // attribute names from different packages. We use separate |
| 139 | // pools that we later combine. |
| 140 | std::map<std::u16string, StringPool> packagePools; |
| 141 | |
| 142 | // Attribute resource IDs are stored in the same order |
| 143 | // as the attribute names appear in the StringPool. |
| 144 | // Since the StringPool contains more than just attribute |
| 145 | // names, to maintain a tight packing of resource IDs, |
| 146 | // we must ensure that attribute names appear first |
| 147 | // in our StringPool. For this, we assign a low priority |
| 148 | // (0xffffffff) to non-attribute strings. Attribute |
| 149 | // names will be stored along with a priority equal |
| 150 | // to their resource ID so that they are ordered. |
| 151 | StringPool::Context lowPriority { 0xffffffffu }; |
| 152 | |
| 153 | // Once we sort the StringPool, we can assign the updated indices |
| 154 | // to the correct data locations. |
| 155 | std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>> stringRefs; |
| 156 | |
| 157 | // Since we don't know the size of the final StringPool, we write to this |
| 158 | // temporary BigBuffer, which we will append to outBuffer later. |
| 159 | BigBuffer out(1024); |
| 160 | while (XmlPullParser::isGoodEvent(parser->next())) { |
| 161 | XmlPullParser::Event event = parser->getEvent(); |
| 162 | switch (event) { |
| 163 | case XmlPullParser::Event::kStartNamespace: |
| 164 | case XmlPullParser::Event::kEndNamespace: { |
| 165 | const size_t startIndex = out.size(); |
| 166 | android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>(); |
| 167 | if (event == XmlPullParser::Event::kStartNamespace) { |
| 168 | node->header.type = android::RES_XML_START_NAMESPACE_TYPE; |
| 169 | } else { |
| 170 | node->header.type = android::RES_XML_END_NAMESPACE_TYPE; |
| 171 | } |
| 172 | |
| 173 | node->header.headerSize = sizeof(*node); |
| 174 | node->lineNumber = parser->getLineNumber(); |
| 175 | node->comment.index = -1; |
| 176 | |
| 177 | android::ResXMLTree_namespaceExt* ns = |
| 178 | out.nextBlock<android::ResXMLTree_namespaceExt>(); |
| 179 | stringRefs.emplace_back( |
| 180 | pool.makeRef(parser->getNamespacePrefix(), lowPriority), &ns->prefix); |
| 181 | stringRefs.emplace_back( |
| 182 | pool.makeRef(parser->getNamespaceUri(), lowPriority), &ns->uri); |
| 183 | |
| 184 | out.align4(); |
| 185 | node->header.size = out.size() - startIndex; |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | case XmlPullParser::Event::kStartElement: { |
| 190 | const size_t startIndex = out.size(); |
| 191 | android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>(); |
| 192 | node->header.type = android::RES_XML_START_ELEMENT_TYPE; |
| 193 | node->header.headerSize = sizeof(*node); |
| 194 | node->lineNumber = parser->getLineNumber(); |
| 195 | node->comment.index = -1; |
| 196 | |
| 197 | android::ResXMLTree_attrExt* elem = out.nextBlock<android::ResXMLTree_attrExt>(); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 198 | if (!parser->getElementNamespace().empty()) { |
| 199 | stringRefs.emplace_back( |
| 200 | pool.makeRef(parser->getElementNamespace(), lowPriority), &elem->ns); |
| 201 | } else { |
| 202 | // The device doesn't think a string of size 0 is the same as null. |
| 203 | elem->ns.index = -1; |
| 204 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 205 | stringRefs.emplace_back( |
| 206 | pool.makeRef(parser->getElementName(), lowPriority), &elem->name); |
| 207 | elem->attributeStart = sizeof(*elem); |
| 208 | elem->attributeSize = sizeof(android::ResXMLTree_attribute); |
| 209 | |
| 210 | // The resource system expects attributes to be sorted by resource ID. |
| 211 | std::vector<XmlAttribute> sortedAttributes; |
| 212 | uint32_t nextAttributeId = 0; |
| 213 | const auto endAttrIter = parser->endAttributes(); |
| 214 | for (auto attrIter = parser->beginAttributes(); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 215 | attrIter != endAttrIter; |
| 216 | ++attrIter) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 217 | uint32_t id; |
| 218 | StringPool::Ref nameRef; |
| 219 | const Attribute* attr = nullptr; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 220 | |
| 221 | if (options.maxSdkAttribute && attrIter->namespaceUri == kSchemaAndroid) { |
| 222 | size_t sdkVersion = findAttributeSdkLevel(attrIter->name); |
| 223 | if (sdkVersion > options.maxSdkAttribute.value()) { |
| 224 | // We will silently omit this attribute |
| 225 | smallestStrippedAttributeSdk = |
| 226 | std::min(smallestStrippedAttributeSdk, sdkVersion); |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | ResourceNameRef genIdName; |
| 232 | bool create = false; |
| 233 | bool privateRef = false; |
| 234 | if (mTable && ResourceParser::tryParseReference(attrIter->value, &genIdName, |
| 235 | &create, &privateRef) && create) { |
| 236 | mTable->addResource(genIdName, {}, source.line(parser->getLineNumber()), |
| 237 | util::make_unique<Id>()); |
| 238 | } |
| 239 | |
| 240 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 241 | Maybe<std::u16string> package = util::extractPackageFromNamespace( |
| 242 | attrIter->namespaceUri); |
| 243 | if (!package || !mResolver) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 244 | // Attributes that have no resource ID (because they don't belong to a |
| 245 | // package) should appear after those that do have resource IDs. Assign |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 246 | // them some integer value that will appear after. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 247 | id = 0x80000000u | nextAttributeId++; |
| 248 | nameRef = pool.makeRef(attrIter->name, StringPool::Context{ id }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 249 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 250 | } else { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 251 | // Find the Attribute object via our Resolver. |
| 252 | ResourceName attrName = { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 253 | package.value(), ResourceType::kAttr, attrIter->name }; |
| 254 | |
| 255 | if (attrName.package.empty()) { |
| 256 | attrName.package = options.defaultPackage; |
| 257 | } |
| 258 | |
| 259 | Maybe<IResolver::Entry> result = mResolver->findAttribute(attrName); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | if (!result || !result.value().id.isValid()) { |
| 261 | logger.error(parser->getLineNumber()) |
| 262 | << "unresolved attribute '" |
| 263 | << attrName |
| 264 | << "'." |
| 265 | << std::endl; |
| 266 | error = true; |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | if (!result.value().attr) { |
| 271 | logger.error(parser->getLineNumber()) |
| 272 | << "not a valid attribute '" |
| 273 | << attrName |
| 274 | << "'." |
| 275 | << std::endl; |
| 276 | error = true; |
| 277 | continue; |
| 278 | } |
| 279 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 280 | id = result.value().id.id; |
| 281 | attr = result.value().attr; |
| 282 | |
| 283 | // Put the attribute name into a package specific pool, since we don't |
| 284 | // want to collapse names from different packages. |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 285 | nameRef = packagePools[package.value()].makeRef( |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 286 | attrIter->name, StringPool::Context{ id }); |
| 287 | } |
| 288 | |
| 289 | // Insert the attribute into the sorted vector. |
| 290 | auto iter = std::lower_bound(sortedAttributes.begin(), sortedAttributes.end(), |
| 291 | id, lessAttributeId); |
| 292 | sortedAttributes.insert(iter, XmlAttribute{ id, &*attrIter, attr, nameRef }); |
| 293 | } |
| 294 | |
| 295 | if (error) { |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | // Now that we have filtered out some attributes, get the final count. |
| 300 | elem->attributeCount = sortedAttributes.size(); |
| 301 | |
| 302 | // Flatten the sorted attributes. |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 303 | uint16_t attributeIndex = 1; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 304 | for (auto entry : sortedAttributes) { |
| 305 | android::ResXMLTree_attribute* attr = |
| 306 | out.nextBlock<android::ResXMLTree_attribute>(); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 307 | if (!entry.xmlAttr->namespaceUri.empty()) { |
| 308 | stringRefs.emplace_back( |
| 309 | pool.makeRef(entry.xmlAttr->namespaceUri, lowPriority), &attr->ns); |
| 310 | } else { |
| 311 | attr->ns.index = -1; |
| 312 | } |
| 313 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 314 | StringPool::Ref rawValueRef = pool.makeRef(entry.xmlAttr->value, lowPriority); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 315 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 316 | stringRefs.emplace_back(entry.nameRef, &attr->name); |
| 317 | |
| 318 | if (options.keepRawValues) { |
| 319 | stringRefs.emplace_back(rawValueRef, &attr->rawValue); |
| 320 | } else { |
| 321 | attr->rawValue.index = -1; |
| 322 | } |
| 323 | |
| 324 | // Assign the indices for specific attributes. |
| 325 | if (entry.xmlAttr->namespaceUri == kSchemaAndroid && |
| 326 | entry.xmlAttr->name == u"id") { |
| 327 | elem->idIndex = attributeIndex; |
| 328 | } else if (entry.xmlAttr->namespaceUri.empty()) { |
| 329 | if (entry.xmlAttr->name == u"class") { |
| 330 | elem->classIndex = attributeIndex; |
| 331 | } else if (entry.xmlAttr->name == u"style") { |
| 332 | elem->styleIndex = attributeIndex; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 333 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 334 | } |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 335 | attributeIndex++; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 336 | |
| 337 | std::unique_ptr<Item> value; |
| 338 | if (entry.attr) { |
| 339 | value = ResourceParser::parseItemForAttribute(entry.xmlAttr->value, |
| 340 | *entry.attr); |
| 341 | } else { |
| 342 | bool create = false; |
| 343 | value = ResourceParser::tryParseReference(entry.xmlAttr->value, &create); |
| 344 | } |
| 345 | |
| 346 | if (mResolver && value) { |
| 347 | AttributeValueFlattener flattener( |
| 348 | mResolver, |
| 349 | &logger, |
| 350 | &attr->typedValue, |
| 351 | parser, |
| 352 | &error, |
| 353 | rawValueRef, |
| 354 | &options.defaultPackage, |
| 355 | &stringRefs); |
| 356 | value->accept(flattener, {}); |
| 357 | } else if (!value && entry.attr && |
| 358 | !(entry.attr->typeMask & android::ResTable_map::TYPE_STRING)) { |
| 359 | logger.error(parser->getLineNumber()) |
| 360 | << "'" |
| 361 | << *rawValueRef |
| 362 | << "' is not compatible with attribute " |
| 363 | << *entry.attr |
| 364 | << "." |
| 365 | << std::endl; |
| 366 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 367 | } else { |
| 368 | attr->typedValue.dataType = android::Res_value::TYPE_STRING; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 369 | if (!options.keepRawValues) { |
| 370 | // Don't set the string twice. |
| 371 | stringRefs.emplace_back(rawValueRef, &attr->rawValue); |
| 372 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 373 | stringRefs.emplace_back(rawValueRef, |
| 374 | reinterpret_cast<android::ResStringPool_ref*>( |
| 375 | &attr->typedValue.data)); |
| 376 | } |
| 377 | attr->typedValue.size = sizeof(attr->typedValue); |
| 378 | } |
| 379 | |
| 380 | out.align4(); |
| 381 | node->header.size = out.size() - startIndex; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | case XmlPullParser::Event::kEndElement: { |
| 386 | const size_t startIndex = out.size(); |
| 387 | android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>(); |
| 388 | node->header.type = android::RES_XML_END_ELEMENT_TYPE; |
| 389 | node->header.headerSize = sizeof(*node); |
| 390 | node->lineNumber = parser->getLineNumber(); |
| 391 | node->comment.index = -1; |
| 392 | |
| 393 | android::ResXMLTree_endElementExt* elem = |
| 394 | out.nextBlock<android::ResXMLTree_endElementExt>(); |
| 395 | stringRefs.emplace_back( |
| 396 | pool.makeRef(parser->getElementNamespace(), lowPriority), &elem->ns); |
| 397 | stringRefs.emplace_back( |
| 398 | pool.makeRef(parser->getElementName(), lowPriority), &elem->name); |
| 399 | |
| 400 | out.align4(); |
| 401 | node->header.size = out.size() - startIndex; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | case XmlPullParser::Event::kText: { |
| 406 | StringPiece16 text = util::trimWhitespace(parser->getText()); |
| 407 | if (text.empty()) { |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | const size_t startIndex = out.size(); |
| 412 | android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>(); |
| 413 | node->header.type = android::RES_XML_CDATA_TYPE; |
| 414 | node->header.headerSize = sizeof(*node); |
| 415 | node->lineNumber = parser->getLineNumber(); |
| 416 | node->comment.index = -1; |
| 417 | |
| 418 | android::ResXMLTree_cdataExt* elem = out.nextBlock<android::ResXMLTree_cdataExt>(); |
| 419 | stringRefs.emplace_back(pool.makeRef(text, lowPriority), &elem->data); |
| 420 | |
| 421 | out.align4(); |
| 422 | node->header.size = out.size() - startIndex; |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | default: |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | } |
| 431 | out.align4(); |
| 432 | |
| 433 | if (error) { |
| 434 | return {}; |
| 435 | } |
| 436 | |
| 437 | if (parser->getEvent() == XmlPullParser::Event::kBadDocument) { |
| 438 | logger.error(parser->getLineNumber()) |
| 439 | << parser->getLastError() |
| 440 | << std::endl; |
| 441 | return {}; |
| 442 | } |
| 443 | |
| 444 | // Merge the package pools into the main pool. |
| 445 | for (auto& packagePoolEntry : packagePools) { |
| 446 | pool.merge(std::move(packagePoolEntry.second)); |
| 447 | } |
| 448 | |
| 449 | // Sort so that attribute resource IDs show up first. |
| 450 | pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { |
| 451 | return a.context.priority < b.context.priority; |
| 452 | }); |
| 453 | |
| 454 | // Now we flatten the string pool references into the correct places. |
| 455 | for (const auto& refEntry : stringRefs) { |
| 456 | refEntry.second->index = refEntry.first.getIndex(); |
| 457 | } |
| 458 | |
| 459 | // Write the XML header. |
| 460 | const size_t beforeXmlTreeIndex = outBuffer->size(); |
| 461 | android::ResXMLTree_header* header = outBuffer->nextBlock<android::ResXMLTree_header>(); |
| 462 | header->header.type = android::RES_XML_TYPE; |
| 463 | header->header.headerSize = sizeof(*header); |
| 464 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 465 | // Flatten the StringPool. |
| 466 | StringPool::flattenUtf16(outBuffer, pool); |
| 467 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 468 | // Write the array of resource IDs, indexed by StringPool order. |
| 469 | const size_t beforeResIdMapIndex = outBuffer->size(); |
| 470 | android::ResChunk_header* resIdMapChunk = outBuffer->nextBlock<android::ResChunk_header>(); |
| 471 | resIdMapChunk->type = android::RES_XML_RESOURCE_MAP_TYPE; |
| 472 | resIdMapChunk->headerSize = sizeof(*resIdMapChunk); |
| 473 | for (const auto& str : pool) { |
| 474 | ResourceId id { str->context.priority }; |
| 475 | if (!id.isValid()) { |
| 476 | // When we see the first non-resource ID, |
| 477 | // we're done. |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | uint32_t* flatId = outBuffer->nextBlock<uint32_t>(); |
| 482 | *flatId = id.id; |
| 483 | } |
| 484 | resIdMapChunk->size = outBuffer->size() - beforeResIdMapIndex; |
| 485 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 486 | // Move the temporary BigBuffer into outBuffer. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 487 | outBuffer->appendBuffer(std::move(out)); |
| 488 | |
| 489 | header->header.size = outBuffer->size() - beforeXmlTreeIndex; |
| 490 | |
| 491 | if (smallestStrippedAttributeSdk == std::numeric_limits<size_t>::max()) { |
| 492 | // Nothing was stripped |
| 493 | return 0u; |
| 494 | } |
| 495 | return smallestStrippedAttributeSdk; |
| 496 | } |
| 497 | |
| 498 | } // namespace aapt |