Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "androidfw/ResourceUtils.h" |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, StringPiece* out_type, |
| 22 | StringPiece* out_entry) { |
| 23 | *out_package = ""; |
| 24 | *out_type = ""; |
| 25 | bool has_package_separator = false; |
| 26 | bool has_type_separator = false; |
| 27 | const char* start = str.data(); |
| 28 | const char* end = start + str.size(); |
Svet Ganov | 3f085422 | 2018-03-22 20:58:52 -0700 | [diff] [blame] | 29 | if (start[0] == '@') { |
| 30 | start++; |
| 31 | } |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 32 | const char* current = start; |
| 33 | while (current != end) { |
| 34 | if (out_type->size() == 0 && *current == '/') { |
| 35 | has_type_separator = true; |
| 36 | out_type->assign(start, current - start); |
| 37 | start = current + 1; |
| 38 | } else if (out_package->size() == 0 && *current == ':') { |
| 39 | has_package_separator = true; |
| 40 | out_package->assign(start, current - start); |
| 41 | start = current + 1; |
| 42 | } |
| 43 | current++; |
| 44 | } |
| 45 | out_entry->assign(start, end - start); |
| 46 | |
| 47 | return !(has_package_separator && out_package->empty()) && |
| 48 | !(has_type_separator && out_type->empty()); |
| 49 | } |
| 50 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 51 | base::expected<AssetManager2::ResourceName, NullOrIOError> ToResourceName( |
| 52 | const StringPoolRef& type_string_ref, const StringPoolRef& entry_string_ref, |
| 53 | const StringPiece& package_name) { |
| 54 | AssetManager2::ResourceName name{ |
| 55 | .package = package_name.data(), |
| 56 | .package_len = package_name.size(), |
| 57 | }; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 58 | |
Bernie Innocenti | 58cf8e3 | 2020-12-19 15:31:52 +0900 | [diff] [blame] | 59 | if (base::expected<StringPiece, NullOrIOError> type_str = type_string_ref.string8(); |
| 60 | type_str.ok()) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 61 | name.type = type_str->data(); |
| 62 | name.type_len = type_str->size(); |
| 63 | } else if (UNLIKELY(IsIOError(type_str))) { |
| 64 | return base::unexpected(type_str.error()); |
| 65 | } |
| 66 | |
| 67 | if (name.type == nullptr) { |
Bernie Innocenti | 58cf8e3 | 2020-12-19 15:31:52 +0900 | [diff] [blame] | 68 | if (base::expected<StringPiece16, NullOrIOError> type16_str = type_string_ref.string16(); |
| 69 | type16_str.ok()) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 70 | name.type16 = type16_str->data(); |
| 71 | name.type_len = type16_str->size(); |
| 72 | } else if (!type16_str.has_value()) { |
| 73 | return base::unexpected(type16_str.error()); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Bernie Innocenti | 58cf8e3 | 2020-12-19 15:31:52 +0900 | [diff] [blame] | 77 | if (base::expected<StringPiece, NullOrIOError> entry_str = entry_string_ref.string8(); |
| 78 | entry_str.ok()) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 79 | name.entry = entry_str->data(); |
| 80 | name.entry_len = entry_str->size(); |
| 81 | } else if (UNLIKELY(IsIOError(entry_str))) { |
| 82 | return base::unexpected(entry_str.error()); |
| 83 | } |
| 84 | |
| 85 | if (name.entry == nullptr) { |
Bernie Innocenti | 58cf8e3 | 2020-12-19 15:31:52 +0900 | [diff] [blame] | 86 | if (base::expected<StringPiece16, NullOrIOError> entry16_str = entry_string_ref.string16(); |
| 87 | entry16_str.ok()) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 88 | name.entry16 = entry16_str->data(); |
| 89 | name.entry_len = entry16_str->size(); |
| 90 | } else if (!entry16_str.has_value()) { |
| 91 | return base::unexpected(entry16_str.error()); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 95 | return name; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 98 | std::string ToFormattedResourceString(const AssetManager2::ResourceName& resource_name) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 99 | std::string result; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 100 | if (resource_name.package != nullptr) { |
| 101 | result.append(resource_name.package, resource_name.package_len); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 104 | if (resource_name.type != nullptr || resource_name.type16 != nullptr) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 105 | if (!result.empty()) { |
| 106 | result += ":"; |
| 107 | } |
| 108 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 109 | if (resource_name.type != nullptr) { |
| 110 | result.append(resource_name.type, resource_name.type_len); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 111 | } else { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 112 | result += util::Utf16ToUtf8(StringPiece16(resource_name.type16, resource_name.type_len)); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 116 | if (resource_name.entry != nullptr || resource_name.entry16 != nullptr) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 117 | if (!result.empty()) { |
| 118 | result += "/"; |
| 119 | } |
| 120 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 121 | if (resource_name.entry != nullptr) { |
| 122 | result.append(resource_name.entry, resource_name.entry_len); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 123 | } else { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 124 | result += util::Utf16ToUtf8(StringPiece16(resource_name.entry16, resource_name.entry_len)); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | return result; |
| 129 | } |
| 130 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 131 | } // namespace android |