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 <algorithm>
18 #include <fstream>
19 #include <iterator>
20 #include <memory>
21 #include <ostream>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include "android-base/macros.h"
27 #include "android-base/stringprintf.h"
28 #include "androidfw/ApkAssets.h"
29 #include "androidfw/AssetManager2.h"
30 #include "androidfw/ConfigDescription.h"
31 #include "androidfw/ResourceUtils.h"
32 #include "androidfw/StringPiece.h"
33 #include "androidfw/Util.h"
34 #include "idmap2/CommandLineOptions.h"
35 #include "idmap2/Idmap.h"
36 #include "idmap2/ResourceUtils.h"
37 #include "idmap2/Result.h"
38 #include "idmap2/SysTrace.h"
39 #include "idmap2/XmlParser.h"
40 #include "idmap2/ZipFile.h"
41 #include "utils/String16.h"
42 #include "utils/String8.h"
43
44 using android::ApkAssets;
45 using android::ApkAssetsCookie;
46 using android::AssetManager2;
47 using android::ConfigDescription;
48 using android::is_valid_resid;
49 using android::kInvalidCookie;
50 using android::Res_value;
51 using android::ResStringPool;
52 using android::ResTable_config;
53 using android::StringPiece16;
54 using android::base::StringPrintf;
55 using android::idmap2::CommandLineOptions;
56 using android::idmap2::Error;
57 using android::idmap2::IdmapHeader;
58 using android::idmap2::ResourceId;
59 using android::idmap2::Result;
60 using android::idmap2::Unit;
61 using android::idmap2::utils::ExtractOverlayManifestInfo;
62 using android::util::Utf16ToUtf8;
63
64 namespace {
65
ParseResReference(const AssetManager2 & am,const std::string & res,const std::string & fallback_package)66 Result<ResourceId> WARN_UNUSED ParseResReference(const AssetManager2& am, const std::string& res,
67 const std::string& fallback_package) {
68 static constexpr const int kBaseHex = 16;
69
70 // first, try to parse as a hex number
71 char* endptr = nullptr;
72 ResourceId resid;
73 resid = strtol(res.c_str(), &endptr, kBaseHex);
74 if (*endptr == '\0') {
75 return resid;
76 }
77
78 // next, try to parse as a package:type/name string
79 resid = am.GetResourceId(res, "", fallback_package);
80 if (is_valid_resid(resid)) {
81 return resid;
82 }
83
84 // end of the road: res could not be parsed
85 return Error("failed to obtain resource id for %s", res.c_str());
86 }
87
PrintValue(AssetManager2 * const am,const Res_value & value,const ApkAssetsCookie & cookie,std::string * const out)88 void PrintValue(AssetManager2* const am, const Res_value& value, const ApkAssetsCookie& cookie,
89 std::string* const out) {
90 switch (value.dataType) {
91 case Res_value::TYPE_INT_DEC:
92 out->append(StringPrintf("%d", value.data));
93 break;
94 case Res_value::TYPE_INT_HEX:
95 out->append(StringPrintf("0x%08x", value.data));
96 break;
97 case Res_value::TYPE_INT_BOOLEAN:
98 out->append(value.data != 0 ? "true" : "false");
99 break;
100 case Res_value::TYPE_STRING: {
101 const ResStringPool* pool = am->GetStringPoolForCookie(cookie);
102 out->append("\"");
103 size_t len;
104 if (pool->isUTF8()) {
105 const char* str = pool->string8At(value.data, &len);
106 out->append(str, len);
107 } else {
108 const char16_t* str16 = pool->stringAt(value.data, &len);
109 out->append(Utf16ToUtf8(StringPiece16(str16, len)));
110 }
111 out->append("\"");
112 } break;
113 default:
114 out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
115 break;
116 }
117 }
118
GetValue(AssetManager2 * const am,ResourceId resid)119 Result<std::string> WARN_UNUSED GetValue(AssetManager2* const am, ResourceId resid) {
120 Res_value value;
121 ResTable_config config;
122 uint32_t flags;
123 ApkAssetsCookie cookie = am->GetResource(resid, true, 0, &value, &config, &flags);
124 if (cookie == kInvalidCookie) {
125 return Error("no resource 0x%08x in asset manager", resid);
126 }
127
128 std::string out;
129
130 // TODO(martenkongstad): use optional parameter GetResource(..., std::string*
131 // stacktrace = NULL) instead
132 out.append(StringPrintf("cookie=%d ", cookie));
133
134 out.append("config='");
135 out.append(config.toString().c_str());
136 out.append("' value=");
137
138 if (value.dataType == Res_value::TYPE_REFERENCE) {
139 const android::ResolvedBag* bag = am->GetBag(static_cast<uint32_t>(value.data));
140 if (bag == nullptr) {
141 out.append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
142 return out;
143 }
144 out.append("[");
145 Res_value bag_val;
146 ResTable_config selected_config;
147 uint32_t flags;
148 uint32_t ref;
149 ApkAssetsCookie bag_cookie;
150 for (size_t i = 0; i < bag->entry_count; ++i) {
151 const android::ResolvedBag::Entry& entry = bag->entries[i];
152 bag_val = entry.value;
153 bag_cookie = am->ResolveReference(entry.cookie, &bag_val, &selected_config, &flags, &ref);
154 if (bag_cookie == kInvalidCookie) {
155 out.append(
156 StringPrintf("Error: dataType=0x%02x data=0x%08x", bag_val.dataType, bag_val.data));
157 continue;
158 }
159 PrintValue(am, bag_val, bag_cookie, &out);
160 if (i != bag->entry_count - 1) {
161 out.append(", ");
162 }
163 }
164 out.append("]");
165 } else {
166 PrintValue(am, value, cookie, &out);
167 }
168
169 return out;
170 }
171
172 } // namespace
173
Lookup(const std::vector<std::string> & args)174 Result<Unit> Lookup(const std::vector<std::string>& args) {
175 SYSTRACE << "Lookup " << args;
176 std::vector<std::string> idmap_paths;
177 std::string config_str;
178 std::string resid_str;
179
180 const CommandLineOptions opts =
181 CommandLineOptions("idmap2 lookup")
182 .MandatoryOption("--idmap-path", "input: path to idmap file to load", &idmap_paths)
183 .MandatoryOption("--config", "configuration to use", &config_str)
184 .MandatoryOption("--resid",
185 "Resource ID (in the target package; '0xpptteeee' or "
186 "'[package:]type/name') to look up",
187 &resid_str);
188
189 const auto opts_ok = opts.Parse(args);
190 if (!opts_ok) {
191 return opts_ok.GetError();
192 }
193
194 ConfigDescription config;
195 if (!ConfigDescription::Parse(config_str, &config)) {
196 return Error("failed to parse config");
197 }
198
199 std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
200 std::string target_path;
201 std::string target_package_name;
202 for (size_t i = 0; i < idmap_paths.size(); i++) {
203 const auto& idmap_path = idmap_paths[i];
204 std::fstream fin(idmap_path);
205 auto idmap_header = IdmapHeader::FromBinaryStream(fin);
206 fin.close();
207 if (!idmap_header) {
208 return Error("failed to read idmap from %s", idmap_path.c_str());
209 }
210
211 if (i == 0) {
212 target_path = idmap_header->GetTargetPath().to_string();
213 auto target_apk = ApkAssets::Load(target_path);
214 if (!target_apk) {
215 return Error("failed to read target apk from %s", target_path.c_str());
216 }
217 apk_assets.push_back(std::move(target_apk));
218
219 auto manifest_info = ExtractOverlayManifestInfo(idmap_header->GetOverlayPath().to_string(),
220 true /* assert_overlay */);
221 if (!manifest_info) {
222 return manifest_info.GetError();
223 }
224 target_package_name = (*manifest_info).target_package;
225 } else if (target_path != idmap_header->GetTargetPath()) {
226 return Error("different target APKs (expected target APK %s but %s has target APK %s)",
227 target_path.c_str(), idmap_path.c_str(),
228 idmap_header->GetTargetPath().to_string().c_str());
229 }
230
231 auto overlay_apk = ApkAssets::LoadOverlay(idmap_path);
232 if (!overlay_apk) {
233 return Error("failed to read overlay apk from %s",
234 idmap_header->GetOverlayPath().to_string().c_str());
235 }
236 apk_assets.push_back(std::move(overlay_apk));
237 }
238
239 // AssetManager2::SetApkAssets requires raw ApkAssets pointers, not unique_ptrs
240 std::vector<const ApkAssets*> raw_pointer_apk_assets;
241 std::transform(apk_assets.cbegin(), apk_assets.cend(), std::back_inserter(raw_pointer_apk_assets),
242 [](const auto& p) -> const ApkAssets* { return p.get(); });
243 AssetManager2 am;
244 am.SetApkAssets(raw_pointer_apk_assets);
245 am.SetConfiguration(config);
246
247 const Result<ResourceId> resid = ParseResReference(am, resid_str, target_package_name);
248 if (!resid) {
249 return Error(resid.GetError(), "failed to parse resource ID");
250 }
251
252 const Result<std::string> value = GetValue(&am, *resid);
253 if (!value) {
254 return Error(value.GetError(), "resource 0x%08x not found", *resid);
255 }
256 std::cout << *value << std::endl;
257
258 return Unit{};
259 }
260