blob: a34aa72392504db807cbb25dc6a6bff9f36c0ae4 [file] [log] [blame]
Adam Lesinski929d6512017-01-16 19:11:19 -08001/*
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
19namespace android {
20
21bool 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 Ganov3f0854222018-03-22 20:58:52 -070029 if (start[0] == '@') {
30 start++;
31 }
Adam Lesinski929d6512017-01-16 19:11:19 -080032 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 Mitchellc75c2e02020-08-17 08:42:48 -070051base::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 };
Winson2f3669b2019-01-11 11:28:34 -080058
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070059 if (base::expected<StringPiece, NullOrIOError> type_str = type_string_ref.string8()) {
60 name.type = type_str->data();
61 name.type_len = type_str->size();
62 } else if (UNLIKELY(IsIOError(type_str))) {
63 return base::unexpected(type_str.error());
64 }
65
66 if (name.type == nullptr) {
67 if (base::expected<StringPiece16, NullOrIOError> type16_str = type_string_ref.string16()) {
68 name.type16 = type16_str->data();
69 name.type_len = type16_str->size();
70 } else if (!type16_str.has_value()) {
71 return base::unexpected(type16_str.error());
Winson2f3669b2019-01-11 11:28:34 -080072 }
73 }
74
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070075 if (base::expected<StringPiece, NullOrIOError> entry_str = entry_string_ref.string8()) {
76 name.entry = entry_str->data();
77 name.entry_len = entry_str->size();
78 } else if (UNLIKELY(IsIOError(entry_str))) {
79 return base::unexpected(entry_str.error());
80 }
81
82 if (name.entry == nullptr) {
83 if (base::expected<StringPiece16, NullOrIOError> entry16_str = entry_string_ref.string16()) {
84 name.entry16 = entry16_str->data();
85 name.entry_len = entry16_str->size();
86 } else if (!entry16_str.has_value()) {
87 return base::unexpected(entry16_str.error());
Winson2f3669b2019-01-11 11:28:34 -080088 }
89 }
90
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070091 return name;
Winson2f3669b2019-01-11 11:28:34 -080092}
93
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070094std::string ToFormattedResourceString(const AssetManager2::ResourceName& resource_name) {
Winson2f3669b2019-01-11 11:28:34 -080095 std::string result;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070096 if (resource_name.package != nullptr) {
97 result.append(resource_name.package, resource_name.package_len);
Winson2f3669b2019-01-11 11:28:34 -080098 }
99
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700100 if (resource_name.type != nullptr || resource_name.type16 != nullptr) {
Winson2f3669b2019-01-11 11:28:34 -0800101 if (!result.empty()) {
102 result += ":";
103 }
104
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700105 if (resource_name.type != nullptr) {
106 result.append(resource_name.type, resource_name.type_len);
Winson2f3669b2019-01-11 11:28:34 -0800107 } else {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700108 result += util::Utf16ToUtf8(StringPiece16(resource_name.type16, resource_name.type_len));
Winson2f3669b2019-01-11 11:28:34 -0800109 }
110 }
111
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700112 if (resource_name.entry != nullptr || resource_name.entry16 != nullptr) {
Winson2f3669b2019-01-11 11:28:34 -0800113 if (!result.empty()) {
114 result += "/";
115 }
116
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700117 if (resource_name.entry != nullptr) {
118 result.append(resource_name.entry, resource_name.entry_len);
Winson2f3669b2019-01-11 11:28:34 -0800119 } else {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700120 result += util::Utf16ToUtf8(StringPiece16(resource_name.entry16, resource_name.entry_len));
Winson2f3669b2019-01-11 11:28:34 -0800121 }
122 }
123
124 return result;
125}
126
Adam Lesinski929d6512017-01-16 19:11:19 -0800127} // namespace android