Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame^] | 17 | #include <memory> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 18 | #include <string> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | |
| 20 | #include "androidfw/StringPiece.h" |
| 21 | #include "androidfw/Util.h" |
| 22 | |
| 23 | #include "idmap2/ResourceUtils.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 24 | #include "idmap2/Result.h" |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame^] | 25 | #include "idmap2/Xml.h" |
| 26 | #include "idmap2/ZipFile.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 27 | |
| 28 | using android::StringPiece16; |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame^] | 29 | using android::idmap2::Result; |
| 30 | using android::idmap2::Xml; |
| 31 | using android::idmap2::ZipFile; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 32 | using android::util::Utf16ToUtf8; |
| 33 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 34 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 35 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 36 | Result<std::string> WARN_UNUSED ResToTypeEntryName(const AssetManager2& am, ResourceId resid) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 37 | AssetManager2::ResourceName name; |
| 38 | if (!am.GetResourceName(resid, &name)) { |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 39 | return {}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 40 | } |
| 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 Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 53 | return {out}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Ryan Mitchell | a362846 | 2019-01-14 12:19:40 -0800 | [diff] [blame^] | 56 | Result<OverlayManifestInfo> ExtractOverlayManifestInfo(const std::string& path, |
| 57 | std::ostream& out_error, |
| 58 | bool assert_overlay) { |
| 59 | std::unique_ptr<const ZipFile> zip = ZipFile::Open(path); |
| 60 | if (!zip) { |
| 61 | out_error << "error: failed to open " << path << " as a zip file" << std::endl; |
| 62 | return kResultError; |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<const MemoryChunk> entry = zip->Uncompress("AndroidManifest.xml"); |
| 66 | if (!entry) { |
| 67 | out_error << "error: failed to uncompress AndroidManifest.xml from " << path << std::endl; |
| 68 | return kResultError; |
| 69 | } |
| 70 | |
| 71 | std::unique_ptr<const Xml> xml = Xml::Create(entry->buf, entry->size); |
| 72 | if (!xml) { |
| 73 | out_error << "error: failed to parse AndroidManifest.xml from " << path << std::endl; |
| 74 | return kResultError; |
| 75 | } |
| 76 | |
| 77 | OverlayManifestInfo info{}; |
| 78 | const auto tag = xml->FindTag("overlay"); |
| 79 | if (!tag) { |
| 80 | if (assert_overlay) { |
| 81 | out_error << "error: <overlay> missing from AndroidManifest.xml of " << path << std::endl; |
| 82 | return kResultError; |
| 83 | } |
| 84 | return info; |
| 85 | } |
| 86 | |
| 87 | auto iter = tag->find("targetPackage"); |
| 88 | if (iter == tag->end()) { |
| 89 | if (assert_overlay) { |
| 90 | out_error << "error: android:targetPackage missing from <overlay> of " << path << std::endl; |
| 91 | return kResultError; |
| 92 | } |
| 93 | } else { |
| 94 | info.target_package = iter->second; |
| 95 | } |
| 96 | |
| 97 | iter = tag->find("targetName"); |
| 98 | if (iter != tag->end()) { |
| 99 | info.target_name = iter->second; |
| 100 | } |
| 101 | |
| 102 | iter = tag->find("isStatic"); |
| 103 | if (iter != tag->end()) { |
| 104 | info.is_static = std::stoul(iter->second) != 0U; |
| 105 | } |
| 106 | |
| 107 | iter = tag->find("priority"); |
| 108 | if (iter != tag->end()) { |
| 109 | info.priority = std::stoi(iter->second); |
| 110 | } |
| 111 | |
| 112 | return info; |
| 113 | } |
| 114 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 115 | } // namespace android::idmap2::utils |