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 "utils/String16.h"
41 #include "utils/String8.h"
42 
43 using android::ApkAssets;
44 using android::ApkAssetsCookie;
45 using android::AssetManager2;
46 using android::ConfigDescription;
47 using android::Res_value;
48 using android::ResStringPool;
49 using android::StringPiece16;
50 using android::base::StringPrintf;
51 using android::idmap2::CommandLineOptions;
52 using android::idmap2::Error;
53 using android::idmap2::IdmapHeader;
54 using android::idmap2::OverlayResourceContainer;
55 using android::idmap2::ResourceId;
56 using android::idmap2::Result;
57 using android::idmap2::Unit;
58 
59 namespace {
60 
ParseResReference(const AssetManager2 & am,const std::string & res,const std::string & fallback_package)61 Result<ResourceId> WARN_UNUSED ParseResReference(const AssetManager2& am, const std::string& res,
62                                                  const std::string& fallback_package) {
63   static constexpr const int kBaseHex = 16;
64 
65   // first, try to parse as a hex number
66   char* endptr = nullptr;
67   const ResourceId parsed_resid = strtol(res.c_str(), &endptr, kBaseHex);
68   if (*endptr == '\0') {
69     return parsed_resid;
70   }
71 
72   // next, try to parse as a package:type/name string
73   if (auto resid = am.GetResourceId(res, "", fallback_package); resid.ok()) {
74     return *resid;
75   }
76 
77   // end of the road: res could not be parsed
78   return Error("failed to obtain resource id for %s", res.c_str());
79 }
80 
PrintValue(AssetManager2 * const am,const AssetManager2::SelectedValue & value,std::string * const out)81 void PrintValue(AssetManager2* const am, const AssetManager2::SelectedValue& value,
82                 std::string* const out) {
83   switch (value.type) {
84     case Res_value::TYPE_INT_DEC:
85       out->append(StringPrintf("%d", value.data));
86       break;
87     case Res_value::TYPE_INT_HEX:
88       out->append(StringPrintf("0x%08x", value.data));
89       break;
90     case Res_value::TYPE_INT_BOOLEAN:
91       out->append(value.data != 0 ? "true" : "false");
92       break;
93     case Res_value::TYPE_STRING: {
94       const ResStringPool* pool = am->GetStringPoolForCookie(value.cookie);
95       out->append("\"");
96       if (auto str = pool->string8ObjectAt(value.data); str.ok()) {
97         out->append(*str);
98       }
99     } break;
100     default:
101       out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.type, value.data));
102       break;
103   }
104 }
105 
GetValue(AssetManager2 * const am,ResourceId resid)106 Result<std::string> WARN_UNUSED GetValue(AssetManager2* const am, ResourceId resid) {
107   auto value = am->GetResource(resid);
108   if (!value.has_value()) {
109     return Error("no resource 0x%08x in asset manager", resid);
110   }
111 
112   std::string out;
113 
114   // TODO(martenkongstad): use optional parameter GetResource(..., std::string*
115   // stacktrace = NULL) instead
116   out.append(StringPrintf("cookie=%d ", value->cookie));
117 
118   out.append("config='");
119   out.append(value->config.toString().c_str());
120   out.append("' value=");
121 
122   if (value->type == Res_value::TYPE_REFERENCE) {
123     auto bag_result = am->GetBag(static_cast<uint32_t>(value->data));
124     if (!bag_result.has_value()) {
125       out.append(StringPrintf("dataType=0x%02x data=0x%08x", value->type, value->data));
126       return out;
127     }
128 
129     out.append("[");
130     const android::ResolvedBag* bag = bag_result.value();
131     for (size_t i = 0; i < bag->entry_count; ++i) {
132       AssetManager2::SelectedValue entry(bag, bag->entries[i]);
133       if (am->ResolveReference(entry).has_value()) {
134         out.append(StringPrintf("Error: dataType=0x%02x data=0x%08x", entry.type, entry.data));
135         continue;
136       }
137       PrintValue(am, entry, &out);
138       if (i != bag->entry_count - 1) {
139         out.append(", ");
140       }
141     }
142     out.append("]");
143   } else {
144     PrintValue(am, *value, &out);
145   }
146 
147   return out;
148 }
149 
150 }  // namespace
151 
Lookup(const std::vector<std::string> & args)152 Result<Unit> Lookup(const std::vector<std::string>& args) {
153   SYSTRACE << "Lookup " << args;
154   std::vector<std::string> idmap_paths;
155   std::string config_str;
156   std::string resid_str;
157 
158   const CommandLineOptions opts =
159       CommandLineOptions("idmap2 lookup")
160           .MandatoryOption("--idmap-path", "input: path to idmap file to load", &idmap_paths)
161           .MandatoryOption("--config", "configuration to use", &config_str)
162           .MandatoryOption("--resid",
163                            "Resource ID (in the target package; '0xpptteeee' or "
164                            "'[package:]type/name') to look up",
165                            &resid_str);
166 
167   const auto opts_ok = opts.Parse(args);
168   if (!opts_ok) {
169     return opts_ok.GetError();
170   }
171 
172   ConfigDescription config;
173   if (!ConfigDescription::Parse(config_str, &config)) {
174     return Error("failed to parse config");
175   }
176 
177   std::vector<AssetManager2::ApkAssetsPtr> apk_assets;
178   std::string target_path;
179   std::string target_package_name;
180   for (size_t i = 0; i < idmap_paths.size(); i++) {
181     const auto& idmap_path = idmap_paths[i];
182     std::fstream fin(idmap_path);
183     auto idmap_header = IdmapHeader::FromBinaryStream(fin);
184     fin.close();
185     if (!idmap_header) {
186       return Error("failed to read idmap from %s", idmap_path.c_str());
187     }
188 
189     if (i == 0) {
190       target_path = idmap_header->GetTargetPath();
191       auto target_apk = ApkAssets::Load(target_path);
192       if (!target_apk) {
193         return Error("failed to read target apk from %s", target_path.c_str());
194       }
195       apk_assets.push_back(std::move(target_apk));
196 
197       auto overlay = OverlayResourceContainer::FromPath(idmap_header->GetOverlayPath());
198       if (!overlay) {
199         return overlay.GetError();
200       }
201 
202       auto manifest_info = (*overlay)->FindOverlayInfo(idmap_header->GetOverlayName());
203       if (!manifest_info) {
204         return manifest_info.GetError();
205       }
206 
207       target_package_name = (*manifest_info).target_package;
208     } else if (target_path != idmap_header->GetTargetPath()) {
209       return Error("different target APKs (expected target APK %s but %s has target APK %s)",
210                    target_path.c_str(), idmap_path.c_str(), idmap_header->GetTargetPath().c_str());
211     }
212 
213     auto overlay_apk = ApkAssets::LoadOverlay(idmap_path);
214     if (!overlay_apk) {
215       return Error("failed to read overlay apk from %s", idmap_header->GetOverlayPath().c_str());
216     }
217     apk_assets.push_back(std::move(overlay_apk));
218   }
219 
220   {
221     // Make sure |apk_assets| vector outlives the asset manager as it doesn't own the assets.
222     AssetManager2 am(apk_assets, config);
223 
224     const Result<ResourceId> resid = ParseResReference(am, resid_str, target_package_name);
225     if (!resid) {
226       return Error(resid.GetError(), "failed to parse resource ID");
227     }
228 
229     const Result<std::string> value = GetValue(&am, *resid);
230     if (!value) {
231       return Error(value.GetError(), "resource 0x%08x not found", *resid);
232     }
233     std::cout << *value << std::endl;
234   }
235 
236   return Unit{};
237 }
238