• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Result.h"
37 #include "idmap2/SysTrace.h"
38 #include "idmap2/Xml.h"
39 #include "idmap2/ZipFile.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::is_valid_resid;
48 using android::kInvalidCookie;
49 using android::Res_value;
50 using android::ResStringPool;
51 using android::ResTable_config;
52 using android::StringPiece16;
53 using android::base::StringPrintf;
54 using android::idmap2::CommandLineOptions;
55 using android::idmap2::Error;
56 using android::idmap2::IdmapHeader;
57 using android::idmap2::ResourceId;
58 using android::idmap2::Result;
59 using android::idmap2::Unit;
60 using android::idmap2::Xml;
61 using android::idmap2::ZipFile;
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 
GetValue(const AssetManager2 & am,ResourceId resid)88 Result<std::string> WARN_UNUSED GetValue(const AssetManager2& am, ResourceId resid) {
89   Res_value value;
90   ResTable_config config;
91   uint32_t flags;
92   ApkAssetsCookie cookie = am.GetResource(resid, false, 0, &value, &config, &flags);
93   if (cookie == kInvalidCookie) {
94     return Error("no resource 0x%08x in asset manager", resid);
95   }
96 
97   std::string out;
98 
99   // TODO(martenkongstad): use optional parameter GetResource(..., std::string*
100   // stacktrace = NULL) instead
101   out.append(StringPrintf("cookie=%d ", cookie));
102 
103   out.append("config='");
104   out.append(config.toString().c_str());
105   out.append("' value=");
106 
107   switch (value.dataType) {
108     case Res_value::TYPE_INT_DEC:
109       out.append(StringPrintf("%d", value.data));
110       break;
111     case Res_value::TYPE_INT_HEX:
112       out.append(StringPrintf("0x%08x", value.data));
113       break;
114     case Res_value::TYPE_INT_BOOLEAN:
115       out.append(value.data != 0 ? "true" : "false");
116       break;
117     case Res_value::TYPE_STRING: {
118       const ResStringPool* pool = am.GetStringPoolForCookie(cookie);
119       size_t len;
120       if (pool->isUTF8()) {
121         const char* str = pool->string8At(value.data, &len);
122         out.append(str, len);
123       } else {
124         const char16_t* str16 = pool->stringAt(value.data, &len);
125         out += Utf16ToUtf8(StringPiece16(str16, len));
126       }
127     } break;
128     default:
129       out.append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
130       break;
131   }
132   return out;
133 }
134 
GetTargetPackageNameFromManifest(const std::string & apk_path)135 Result<std::string> GetTargetPackageNameFromManifest(const std::string& apk_path) {
136   const auto zip = ZipFile::Open(apk_path);
137   if (!zip) {
138     return Error("failed to open %s as zip", apk_path.c_str());
139   }
140   const auto entry = zip->Uncompress("AndroidManifest.xml");
141   if (!entry) {
142     return Error("failed to uncompress AndroidManifest.xml in %s", apk_path.c_str());
143   }
144   const auto xml = Xml::Create(entry->buf, entry->size);
145   if (!xml) {
146     return Error("failed to create XML buffer");
147   }
148   const auto tag = xml->FindTag("overlay");
149   if (!tag) {
150     return Error("failed to find <overlay> tag");
151   }
152   const auto iter = tag->find("targetPackage");
153   if (iter == tag->end()) {
154     return Error("failed to find targetPackage attribute");
155   }
156   return iter->second;
157 }
158 }  // namespace
159 
Lookup(const std::vector<std::string> & args)160 Result<Unit> Lookup(const std::vector<std::string>& args) {
161   SYSTRACE << "Lookup " << args;
162   std::vector<std::string> idmap_paths;
163   std::string config_str;
164   std::string resid_str;
165 
166   const CommandLineOptions opts =
167       CommandLineOptions("idmap2 lookup")
168           .MandatoryOption("--idmap-path", "input: path to idmap file to load", &idmap_paths)
169           .MandatoryOption("--config", "configuration to use", &config_str)
170           .MandatoryOption("--resid",
171                            "Resource ID (in the target package; '0xpptteeee' or "
172                            "'[package:]type/name') to look up",
173                            &resid_str);
174 
175   const auto opts_ok = opts.Parse(args);
176   if (!opts_ok) {
177     return opts_ok.GetError();
178   }
179 
180   ConfigDescription config;
181   if (!ConfigDescription::Parse(config_str, &config)) {
182     return Error("failed to parse config");
183   }
184 
185   std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
186   std::string target_path;
187   std::string target_package_name;
188   for (size_t i = 0; i < idmap_paths.size(); i++) {
189     const auto& idmap_path = idmap_paths[i];
190     std::fstream fin(idmap_path);
191     auto idmap_header = IdmapHeader::FromBinaryStream(fin);
192     fin.close();
193     if (!idmap_header) {
194       return Error("failed to read idmap from %s", idmap_path.c_str());
195     }
196 
197     if (i == 0) {
198       target_path = idmap_header->GetTargetPath().to_string();
199       auto target_apk = ApkAssets::Load(target_path);
200       if (!target_apk) {
201         return Error("failed to read target apk from %s", target_path.c_str());
202       }
203       apk_assets.push_back(std::move(target_apk));
204 
205       const Result<std::string> package_name =
206           GetTargetPackageNameFromManifest(idmap_header->GetOverlayPath().to_string());
207       if (!package_name) {
208         return Error("failed to parse android:targetPackage from overlay manifest");
209       }
210       target_package_name = *package_name;
211     } else if (target_path != idmap_header->GetTargetPath()) {
212       return Error("different target APKs (expected target APK %s but %s has target APK %s)",
213                    target_path.c_str(), idmap_path.c_str(),
214                    idmap_header->GetTargetPath().to_string().c_str());
215     }
216 
217     auto overlay_apk = ApkAssets::LoadOverlay(idmap_path);
218     if (!overlay_apk) {
219       return Error("failed to read overlay apk from %s",
220                    idmap_header->GetOverlayPath().to_string().c_str());
221     }
222     apk_assets.push_back(std::move(overlay_apk));
223   }
224 
225   // AssetManager2::SetApkAssets requires raw ApkAssets pointers, not unique_ptrs
226   std::vector<const ApkAssets*> raw_pointer_apk_assets;
227   std::transform(apk_assets.cbegin(), apk_assets.cend(), std::back_inserter(raw_pointer_apk_assets),
228                  [](const auto& p) -> const ApkAssets* { return p.get(); });
229   AssetManager2 am;
230   am.SetApkAssets(raw_pointer_apk_assets);
231   am.SetConfiguration(config);
232 
233   const Result<ResourceId> resid = ParseResReference(am, resid_str, target_package_name);
234   if (!resid) {
235     return Error(resid.GetError(), "failed to parse resource ID");
236   }
237 
238   const Result<std::string> value = GetValue(am, *resid);
239   if (!value) {
240     return Error(value.GetError(), "resource 0x%08x not found", *resid);
241   }
242   std::cout << *value << std::endl;
243 
244   return Unit{};
245 }
246