Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/ResourceUtils.h" |
| 18 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 19 | #include <memory> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 20 | #include <string> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 21 | |
| 22 | #include "androidfw/StringPiece.h" |
| 23 | #include "androidfw/Util.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 24 | #include "idmap2/Result.h" |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 25 | #include "idmap2/XmlParser.h" |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 26 | #include "idmap2/ZipFile.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 27 | |
| 28 | using android::StringPiece16; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 29 | using android::idmap2::Result; |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 30 | using android::idmap2::XmlParser; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 31 | using android::idmap2::ZipFile; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 32 | using android::util::Utf16ToUtf8; |
| 33 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 34 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 35 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 36 | StringPiece DataTypeToString(uint8_t data_type) { |
| 37 | switch (data_type) { |
| 38 | case Res_value::TYPE_NULL: |
| 39 | return "null"; |
| 40 | case Res_value::TYPE_REFERENCE: |
| 41 | return "reference"; |
| 42 | case Res_value::TYPE_ATTRIBUTE: |
| 43 | return "attribute"; |
| 44 | case Res_value::TYPE_STRING: |
| 45 | return "string"; |
| 46 | case Res_value::TYPE_FLOAT: |
| 47 | return "float"; |
| 48 | case Res_value::TYPE_DIMENSION: |
| 49 | return "dimension"; |
| 50 | case Res_value::TYPE_FRACTION: |
| 51 | return "fraction"; |
| 52 | case Res_value::TYPE_DYNAMIC_REFERENCE: |
| 53 | return "reference (dynamic)"; |
| 54 | case Res_value::TYPE_DYNAMIC_ATTRIBUTE: |
| 55 | return "attribute (dynamic)"; |
| 56 | case Res_value::TYPE_INT_DEC: |
| 57 | case Res_value::TYPE_INT_HEX: |
| 58 | return "integer"; |
| 59 | case Res_value::TYPE_INT_BOOLEAN: |
| 60 | return "boolean"; |
| 61 | case Res_value::TYPE_INT_COLOR_ARGB8: |
| 62 | case Res_value::TYPE_INT_COLOR_RGB8: |
| 63 | case Res_value::TYPE_INT_COLOR_RGB4: |
| 64 | return "color"; |
| 65 | default: |
| 66 | return "unknown"; |
| 67 | } |
| 68 | } |
| 69 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 70 | Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 71 | AssetManager2::ResourceName name; |
| 72 | if (!am.GetResourceName(resid, &name)) { |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 73 | return Error("no resource 0x%08x in asset manager", resid); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 74 | } |
| 75 | std::string out; |
| 76 | if (name.type != nullptr) { |
| 77 | out.append(name.type, name.type_len); |
| 78 | } else { |
| 79 | out += Utf16ToUtf8(StringPiece16(name.type16, name.type_len)); |
| 80 | } |
| 81 | out.append("/"); |
| 82 | if (name.entry != nullptr) { |
| 83 | out.append(name.entry, name.entry_len); |
| 84 | } else { |
| 85 | out += Utf16ToUtf8(StringPiece16(name.entry16, name.entry_len)); |
| 86 | } |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 87 | return out; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 90 | Result<OverlayManifestInfo> ExtractOverlayManifestInfo(const std::string& path, |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 91 | bool assert_overlay) { |
| 92 | std::unique_ptr<const ZipFile> zip = ZipFile::Open(path); |
| 93 | if (!zip) { |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 94 | return Error("failed to open %s as a zip file", path.c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | std::unique_ptr<const MemoryChunk> entry = zip->Uncompress("AndroidManifest.xml"); |
| 98 | if (!entry) { |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 99 | return Error("failed to uncompress AndroidManifest.xml from %s", path.c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 102 | Result<std::unique_ptr<const XmlParser>> xml = XmlParser::Create(entry->buf, entry->size); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 103 | if (!xml) { |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 104 | return Error("failed to parse AndroidManifest.xml from %s", path.c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 107 | auto manifest_it = (*xml)->tree_iterator(); |
| 108 | if (manifest_it->event() != XmlParser::Event::START_TAG || manifest_it->name() != "manifest") { |
| 109 | return Error("root element tag is not <manifest> in AndroidManifest.xml of %s", path.c_str()); |
| 110 | } |
| 111 | |
| 112 | auto overlay_it = std::find_if(manifest_it.begin(), manifest_it.end(), [](const auto& it) { |
| 113 | return it.event() == XmlParser::Event::START_TAG && it.name() == "overlay"; |
| 114 | }); |
| 115 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 116 | OverlayManifestInfo info{}; |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 117 | if (overlay_it == manifest_it.end()) { |
| 118 | if (!assert_overlay) { |
| 119 | return info; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 120 | } |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 121 | return Error("<overlay> missing from AndroidManifest.xml of %s", path.c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 124 | if (auto result_str = overlay_it->GetAttributeStringValue("targetPackage")) { |
| 125 | info.target_package = *result_str; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 126 | } else { |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 127 | return Error("android:targetPackage missing from <overlay> of %s: %s", path.c_str(), |
| 128 | result_str.GetErrorMessage().c_str()); |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 131 | if (auto result_str = overlay_it->GetAttributeStringValue("targetName")) { |
| 132 | info.target_name = *result_str; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 135 | if (auto result_value = overlay_it->GetAttributeValue("resourcesMap")) { |
| 136 | if ((*result_value).dataType == Res_value::TYPE_REFERENCE) { |
| 137 | info.resource_mapping = (*result_value).data; |
| 138 | } else { |
| 139 | return Error("android:resourcesMap is not a reference in AndroidManifest.xml of %s", |
| 140 | path.c_str()); |
| 141 | } |
| 142 | } |
| 143 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 144 | if (auto result_value = overlay_it->GetAttributeValue("isStatic")) { |
| 145 | if ((*result_value).dataType >= Res_value::TYPE_FIRST_INT && |
| 146 | (*result_value).dataType <= Res_value::TYPE_LAST_INT) { |
| 147 | info.is_static = (*result_value).data != 0U; |
| 148 | } else { |
| 149 | return Error("android:isStatic is not a boolean in AndroidManifest.xml of %s", path.c_str()); |
| 150 | } |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Ryan Mitchell | cd965a3 | 2019-09-18 14:52:45 -0700 | [diff] [blame] | 153 | if (auto result_value = overlay_it->GetAttributeValue("priority")) { |
| 154 | if ((*result_value).dataType >= Res_value::TYPE_FIRST_INT && |
| 155 | (*result_value).dataType <= Res_value::TYPE_LAST_INT) { |
| 156 | info.priority = (*result_value).data; |
| 157 | } else { |
| 158 | return Error("android:priority is not an integer in AndroidManifest.xml of %s", path.c_str()); |
| 159 | } |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 162 | if (auto result_str = overlay_it->GetAttributeStringValue("requiredSystemPropertyName")) { |
| 163 | info.requiredSystemPropertyName = *result_str; |
Gabriel Siqueira | d7fc4f7 | 2019-09-17 18:42:07 -0300 | [diff] [blame] | 164 | } |
| 165 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 166 | if (auto result_str = overlay_it->GetAttributeStringValue("requiredSystemPropertyValue")) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 167 | info.requiredSystemPropertyValue = *result_str; |
Gabriel Siqueira | d7fc4f7 | 2019-09-17 18:42:07 -0300 | [diff] [blame] | 168 | } |
| 169 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame] | 170 | return info; |
| 171 | } |
| 172 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 173 | } // namespace android::idmap2::utils |