blob: c63dff8f9104bb847ba0347e24fed2c1f108751a [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 Mitchell741e96f2019-01-23 16:56:51 -080051bool ToResourceName(const StringPoolRef& type_string_ref,
52 const StringPoolRef& entry_string_ref,
53 const StringPiece& package_name,
Winson2f3669b2019-01-11 11:28:34 -080054 AssetManager2::ResourceName* out_name) {
Ryan Mitchell741e96f2019-01-23 16:56:51 -080055 out_name->package = package_name.data();
56 out_name->package_len = package_name.size();
Winson2f3669b2019-01-11 11:28:34 -080057
58 out_name->type = type_string_ref.string8(&out_name->type_len);
59 out_name->type16 = nullptr;
60 if (out_name->type == nullptr) {
61 out_name->type16 = type_string_ref.string16(&out_name->type_len);
62 if (out_name->type16 == nullptr) {
63 return false;
64 }
65 }
66
67 out_name->entry = entry_string_ref.string8(&out_name->entry_len);
68 out_name->entry16 = nullptr;
69 if (out_name->entry == nullptr) {
70 out_name->entry16 = entry_string_ref.string16(&out_name->entry_len);
71 if (out_name->entry16 == nullptr) {
72 return false;
73 }
74 }
75
76 return true;
77}
78
79std::string ToFormattedResourceString(AssetManager2::ResourceName* resource_name) {
80 std::string result;
81 if (resource_name->package != nullptr) {
82 result.append(resource_name->package, resource_name->package_len);
83 }
84
85 if (resource_name->type != nullptr || resource_name->type16 != nullptr) {
86 if (!result.empty()) {
87 result += ":";
88 }
89
90 if (resource_name->type != nullptr) {
91 result.append(resource_name->type, resource_name->type_len);
92 } else {
93 result += util::Utf16ToUtf8(StringPiece16(resource_name->type16, resource_name->type_len));
94 }
95 }
96
97 if (resource_name->entry != nullptr || resource_name->entry16 != nullptr) {
98 if (!result.empty()) {
99 result += "/";
100 }
101
102 if (resource_name->entry != nullptr) {
103 result.append(resource_name->entry, resource_name->entry_len);
104 } else {
105 result += util::Utf16ToUtf8(StringPiece16(resource_name->entry16, resource_name->entry_len));
106 }
107 }
108
109 return result;
110}
111
Adam Lesinski929d6512017-01-16 19:11:19 -0800112} // namespace android