blob: a24836da7f3a3195fd0c5fec1da282dd7b8e94cf [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 Mitchella3628462019-01-14 12:19:40 -080017#include <memory>
Mårten Kongstad02751232018-04-27 13:16:32 +020018#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020019
20#include "androidfw/StringPiece.h"
21#include "androidfw/Util.h"
22
23#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010024#include "idmap2/Result.h"
Ryan Mitchella3628462019-01-14 12:19:40 -080025#include "idmap2/Xml.h"
26#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;
30using android::idmap2::Xml;
31using 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
Mårten Kongstad49d835d2019-01-31 10:50:48 +010036Result<std::string> ResToTypeEntryName(const AssetManager2& am, ResourceId 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
68 std::unique_ptr<const Xml> xml = Xml::Create(entry->buf, entry->size);
69 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
73 OverlayManifestInfo info{};
74 const auto tag = xml->FindTag("overlay");
75 if (!tag) {
76 if (assert_overlay) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010077 return Error("<overlay> missing from AndroidManifest.xml of %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080078 }
79 return info;
80 }
81
82 auto iter = tag->find("targetPackage");
83 if (iter == tag->end()) {
84 if (assert_overlay) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010085 return Error("android:targetPackage missing from <overlay> of %s", path.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -080086 }
87 } else {
88 info.target_package = iter->second;
89 }
90
91 iter = tag->find("targetName");
92 if (iter != tag->end()) {
93 info.target_name = iter->second;
94 }
95
96 iter = tag->find("isStatic");
97 if (iter != tag->end()) {
98 info.is_static = std::stoul(iter->second) != 0U;
99 }
100
101 iter = tag->find("priority");
102 if (iter != tag->end()) {
103 info.priority = std::stoi(iter->second);
104 }
105
106 return info;
107}
108
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100109} // namespace android::idmap2::utils