blob: 9d3269207c912338dbf347b6f1ea1e2e4b683ca0 [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 Mitchellcd965a32019-09-18 14:52:45 -070036Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
Mårten Kongstad02751232018-04-27 13:16:32 +020037 AssetManager2::ResourceName name;
38 if (!am.GetResourceName(resid, &name)) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010039 return Error("no resource 0x%08x in asset manager", resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020040 }
41 std::string out;
42 if (name.type != nullptr) {
43 out.append(name.type, name.type_len);
44 } else {
45 out += Utf16ToUtf8(StringPiece16(name.type16, name.type_len));
46 }
47 out.append("/");
48 if (name.entry != nullptr) {
49 out.append(name.entry, name.entry_len);
50 } else {
51 out += Utf16ToUtf8(StringPiece16(name.entry16, name.entry_len));
52 }
Mårten Kongstad49d835d2019-01-31 10:50:48 +010053 return out;
Mårten Kongstad02751232018-04-27 13:16:32 +020054}
55
Ryan Mitchella3628462019-01-14 12:19:40 -080056Result<OverlayManifestInfo> ExtractOverlayManifestInfo(const std::string& path,
Ryan Mitchella3628462019-01-14 12:19:40 -080057 bool assert_overlay) {
58 std::unique_ptr<const ZipFile> zip = ZipFile::Open(path);
59 if (!zip) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010060 return Error("failed to open %s as a zip file", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080061 }
62
63 std::unique_ptr<const MemoryChunk> entry = zip->Uncompress("AndroidManifest.xml");
64 if (!entry) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010065 return Error("failed to uncompress AndroidManifest.xml from %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080066 }
67
Ryan Mitchellcd965a32019-09-18 14:52:45 -070068 Result<std::unique_ptr<const XmlParser>> xml = XmlParser::Create(entry->buf, entry->size);
Ryan Mitchella3628462019-01-14 12:19:40 -080069 if (!xml) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010070 return Error("failed to parse AndroidManifest.xml from %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080071 }
72
Ryan Mitchellcd965a32019-09-18 14:52:45 -070073 auto manifest_it = (*xml)->tree_iterator();
74 if (manifest_it->event() != XmlParser::Event::START_TAG || manifest_it->name() != "manifest") {
75 return Error("root element tag is not <manifest> in AndroidManifest.xml of %s", path.c_str());
76 }
77
78 auto overlay_it = std::find_if(manifest_it.begin(), manifest_it.end(), [](const auto& it) {
79 return it.event() == XmlParser::Event::START_TAG && it.name() == "overlay";
80 });
81
Ryan Mitchella3628462019-01-14 12:19:40 -080082 OverlayManifestInfo info{};
Ryan Mitchellcd965a32019-09-18 14:52:45 -070083 if (overlay_it == manifest_it.end()) {
84 if (!assert_overlay) {
85 return info;
Ryan Mitchella3628462019-01-14 12:19:40 -080086 }
Ryan Mitchellcd965a32019-09-18 14:52:45 -070087 return Error("<overlay> missing from AndroidManifest.xml of %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080088 }
89
Ryan Mitchellcd965a32019-09-18 14:52:45 -070090 if (auto result_str = overlay_it->GetAttributeStringValue("targetPackage")) {
91 info.target_package = *result_str;
Ryan Mitchella3628462019-01-14 12:19:40 -080092 } else {
Ryan Mitchellcd965a32019-09-18 14:52:45 -070093 return Error("android:targetPackage missing from <overlay> of %s: %s", path.c_str(),
94 result_str.GetErrorMessage().c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080095 }
96
Ryan Mitchellcd965a32019-09-18 14:52:45 -070097 if (auto result_str = overlay_it->GetAttributeStringValue("targetName")) {
98 info.target_name = *result_str;
Ryan Mitchella3628462019-01-14 12:19:40 -080099 }
100
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700101 if (auto result_value = overlay_it->GetAttributeValue("resourcesMap")) {
102 if ((*result_value).dataType == Res_value::TYPE_REFERENCE) {
103 info.resource_mapping = (*result_value).data;
104 } else {
105 return Error("android:resourcesMap is not a reference in AndroidManifest.xml of %s",
106 path.c_str());
107 }
108 }
109
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700110 if (auto result_value = overlay_it->GetAttributeValue("isStatic")) {
111 if ((*result_value).dataType >= Res_value::TYPE_FIRST_INT &&
112 (*result_value).dataType <= Res_value::TYPE_LAST_INT) {
113 info.is_static = (*result_value).data != 0U;
114 } else {
115 return Error("android:isStatic is not a boolean in AndroidManifest.xml of %s", path.c_str());
116 }
Ryan Mitchella3628462019-01-14 12:19:40 -0800117 }
118
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700119 if (auto result_value = overlay_it->GetAttributeValue("priority")) {
120 if ((*result_value).dataType >= Res_value::TYPE_FIRST_INT &&
121 (*result_value).dataType <= Res_value::TYPE_LAST_INT) {
122 info.priority = (*result_value).data;
123 } else {
124 return Error("android:priority is not an integer in AndroidManifest.xml of %s", path.c_str());
125 }
Ryan Mitchella3628462019-01-14 12:19:40 -0800126 }
127
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700128 if (auto result_str = overlay_it->GetAttributeStringValue("requiredSystemPropertyName")) {
129 info.requiredSystemPropertyName = *result_str;
Gabriel Siqueirad7fc4f72019-09-17 18:42:07 -0300130 }
131
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700132 if (auto result_str = overlay_it->GetAttributeStringValue("requiredSystemPropertyValue")) {
133 info.requiredSystemPropertyValue = *result_str;
Gabriel Siqueirad7fc4f72019-09-17 18:42:07 -0300134 }
135
Ryan Mitchella3628462019-01-14 12:19:40 -0800136 return info;
137}
138
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100139} // namespace android::idmap2::utils