blob: 87fb2c038c9fd50fc6b3d938c5d554091d88e3c9 [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 Mitchell80094e32020-11-16 23:08:18 +000051base::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
Bernie Innocenti58cf8e32020-12-19 15:31:52 +090059 if (base::expected<StringPiece, NullOrIOError> type_str = type_string_ref.string8();
60 type_str.ok()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +000061 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 Innocenti58cf8e32020-12-19 15:31:52 +090068 if (base::expected<StringPiece16, NullOrIOError> type16_str = type_string_ref.string16();
69 type16_str.ok()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +000070 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());
Winson2f3669b2019-01-11 11:28:34 -080074 }
75 }
76
Bernie Innocenti58cf8e32020-12-19 15:31:52 +090077 if (base::expected<StringPiece, NullOrIOError> entry_str = entry_string_ref.string8();
78 entry_str.ok()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +000079 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 Innocenti58cf8e32020-12-19 15:31:52 +090086 if (base::expected<StringPiece16, NullOrIOError> entry16_str = entry_string_ref.string16();
87 entry16_str.ok()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +000088 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());
Winson2f3669b2019-01-11 11:28:34 -080092 }
93 }
94
Ryan Mitchell80094e32020-11-16 23:08:18 +000095 return name;
Winson2f3669b2019-01-11 11:28:34 -080096}
97
Ryan Mitchell80094e32020-11-16 23:08:18 +000098std::string ToFormattedResourceString(const AssetManager2::ResourceName& resource_name) {
Winson2f3669b2019-01-11 11:28:34 -080099 std::string result;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000100 if (resource_name.package != nullptr) {
101 result.append(resource_name.package, resource_name.package_len);
Winson2f3669b2019-01-11 11:28:34 -0800102 }
103
Ryan Mitchell80094e32020-11-16 23:08:18 +0000104 if (resource_name.type != nullptr || resource_name.type16 != nullptr) {
Winson2f3669b2019-01-11 11:28:34 -0800105 if (!result.empty()) {
106 result += ":";
107 }
108
Ryan Mitchell80094e32020-11-16 23:08:18 +0000109 if (resource_name.type != nullptr) {
110 result.append(resource_name.type, resource_name.type_len);
Winson2f3669b2019-01-11 11:28:34 -0800111 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000112 result += util::Utf16ToUtf8(StringPiece16(resource_name.type16, resource_name.type_len));
Winson2f3669b2019-01-11 11:28:34 -0800113 }
114 }
115
Ryan Mitchell80094e32020-11-16 23:08:18 +0000116 if (resource_name.entry != nullptr || resource_name.entry16 != nullptr) {
Winson2f3669b2019-01-11 11:28:34 -0800117 if (!result.empty()) {
118 result += "/";
119 }
120
Ryan Mitchell80094e32020-11-16 23:08:18 +0000121 if (resource_name.entry != nullptr) {
122 result.append(resource_name.entry, resource_name.entry_len);
Winson2f3669b2019-01-11 11:28:34 -0800123 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000124 result += util::Utf16ToUtf8(StringPiece16(resource_name.entry16, resource_name.entry_len));
Winson2f3669b2019-01-11 11:28:34 -0800125 }
126 }
127
128 return result;
129}
130
Adam Lesinski929d6512017-01-16 19:11:19 -0800131} // namespace android