blob: 52837418e7759f20a9099d7bd9187047708a5b37 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/ResourceUtils.h"
18
Ryan Mitchella3628462019-01-14 12:19:40 -080019#include <memory>
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
22#include "androidfw/StringPiece.h"
23#include "androidfw/Util.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010024#include "idmap2/Result.h"
Ryan Mitchellcd965a32019-09-18 14:52:45 -070025#include "idmap2/XmlParser.h"
Ryan Mitchella3628462019-01-14 12:19:40 -080026#include "idmap2/ZipFile.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027
28using android::StringPiece16;
Ryan Mitchella3628462019-01-14 12:19:40 -080029using android::idmap2::Result;
Ryan Mitchellcd965a32019-09-18 14:52:45 -070030using android::idmap2::XmlParser;
Ryan Mitchella3628462019-01-14 12:19:40 -080031using android::idmap2::ZipFile;
Mårten Kongstad02751232018-04-27 13:16:32 +020032using android::util::Utf16ToUtf8;
33
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010034namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020035
Ryan Mitchell5035d662020-01-22 13:19:41 -080036bool IsReference(uint8_t data_type) {
37 return data_type == Res_value::TYPE_REFERENCE || data_type == Res_value::TYPE_DYNAMIC_REFERENCE;
38}
39
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070040StringPiece 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
Ryan Mitchellcd965a32019-09-18 14:52:45 -070074Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000075 const auto name = am.GetResourceName(resid);
76 if (!name.has_value()) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010077 return Error("no resource 0x%08x in asset manager", resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020078 }
79 std::string out;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000080 if (name->type != nullptr) {
81 out.append(name->type, name->type_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020082 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000083 out += Utf16ToUtf8(StringPiece16(name->type16, name->type_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020084 }
85 out.append("/");
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000086 if (name->entry != nullptr) {
87 out.append(name->entry, name->entry_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020088 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000089 out += Utf16ToUtf8(StringPiece16(name->entry16, name->entry_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020090 }
Mårten Kongstad49d835d2019-01-31 10:50:48 +010091 return out;
Mårten Kongstad02751232018-04-27 13:16:32 +020092}
93
Ryan Mitchella3628462019-01-14 12:19:40 -080094Result<OverlayManifestInfo> ExtractOverlayManifestInfo(const std::string& path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080095 const std::string& name) {
Ryan Mitchella3628462019-01-14 12:19:40 -080096 std::unique_ptr<const ZipFile> zip = ZipFile::Open(path);
97 if (!zip) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010098 return Error("failed to open %s as a zip file", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080099 }
100
101 std::unique_ptr<const MemoryChunk> entry = zip->Uncompress("AndroidManifest.xml");
102 if (!entry) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100103 return Error("failed to uncompress AndroidManifest.xml from %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800104 }
105
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700106 Result<std::unique_ptr<const XmlParser>> xml = XmlParser::Create(entry->buf, entry->size);
Ryan Mitchella3628462019-01-14 12:19:40 -0800107 if (!xml) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100108 return Error("failed to parse AndroidManifest.xml from %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800109 }
110
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700111 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
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800116 for (auto&& it : manifest_it) {
117 if (it.event() != XmlParser::Event::START_TAG || it.name() != "overlay") {
118 continue;
Ryan Mitchella3628462019-01-14 12:19:40 -0800119 }
Ryan Mitchella3628462019-01-14 12:19:40 -0800120
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800121 OverlayManifestInfo info{};
122 if (auto result_str = it.GetAttributeStringValue("name")) {
123 if (*result_str != name) {
124 // A value for android:name was found, but either a the name does not match the requested
125 // name, or an <overlay> tag with no name was requested.
126 continue;
127 }
128 info.name = *result_str;
129 } else if (!name.empty()) {
130 // This tag does not have a value for android:name, but an <overlay> tag with a specific name
131 // has been requested.
132 continue;
133 }
Ryan Mitchella3628462019-01-14 12:19:40 -0800134
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800135 if (auto result_str = it.GetAttributeStringValue("targetPackage")) {
136 info.target_package = *result_str;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700137 } else {
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800138 return Error("android:targetPackage missing from <overlay> of %s: %s", path.c_str(),
139 result_str.GetErrorMessage().c_str());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700140 }
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700141
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800142 if (auto result_str = it.GetAttributeStringValue("targetName")) {
143 info.target_name = *result_str;
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700144 }
Ryan Mitchella3628462019-01-14 12:19:40 -0800145
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800146 if (auto result_value = it.GetAttributeValue("resourcesMap")) {
147 if (IsReference((*result_value).dataType)) {
148 info.resource_mapping = (*result_value).data;
149 } else {
150 return Error("android:resourcesMap is not a reference in AndroidManifest.xml of %s",
151 path.c_str());
152 }
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700153 }
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800154 return info;
Ryan Mitchella3628462019-01-14 12:19:40 -0800155 }
156
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800157 return Error("<overlay> with android:name \"%s\" missing from AndroidManifest.xml of %s",
158 name.c_str(), path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800159}
160
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100161} // namespace android::idmap2::utils