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