blob: 11239673272e8093ac536257e1c4326013724fae [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 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 "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <sstream>
20
21#include "androidfw/ResourceTypes.h"
22
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070024#include "SdkConstants.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "flatten/ResourceTypeExtensions.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080026#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "util/Util.h"
28
Adam Lesinskid5083f62017-01-16 15:07:21 -080029using android::StringPiece;
30using android::StringPiece16;
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032namespace aapt {
33namespace ResourceUtils {
34
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035Maybe<ResourceName> ToResourceName(
36 const android::ResTable::resource_name& name_in) {
37 ResourceName name_out;
38 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 return {};
40 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070041
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 name_out.package =
43 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 if (name_in.type) {
47 type = ParseResourceType(
48 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
49 } else if (name_in.type8) {
50 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 } else {
52 return {};
53 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 if (!type) {
56 return {};
57 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070060
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 if (name_in.name) {
62 name_out.entry =
63 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
64 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080065 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 } else {
67 return {};
68 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool ExtractResourceName(const StringPiece& str, StringPiece* out_package,
73 StringPiece* out_type, StringPiece* out_entry) {
74 bool has_package_separator = false;
75 bool has_type_separator = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 const char* start = str.data();
77 const char* end = start + str.size();
78 const char* current = start;
79 while (current != end) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 if (out_type->size() == 0 && *current == '/') {
81 has_type_separator = true;
82 out_type->assign(start, current - start);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 start = current + 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 } else if (out_package->size() == 0 && *current == ':') {
85 has_package_separator = true;
86 out_package->assign(start, current - start);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 start = current + 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 current++;
90 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 out_entry->assign(start, end - start);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 return !(has_package_separator && out_package->empty()) &&
94 !(has_type_separator && out_type->empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
98 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 if (str.empty()) {
100 return false;
101 }
102
103 size_t offset = 0;
104 bool priv = false;
105 if (str.data()[0] == '*') {
106 priv = true;
107 offset = 1;
108 }
109
110 StringPiece package;
111 StringPiece type;
112 StringPiece entry;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (!ExtractResourceName(str.substr(offset, str.size() - offset), &package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 &type, &entry)) {
115 return false;
116 }
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 const ResourceType* parsed_type = ParseResourceType(type);
119 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 return false;
121 }
122
123 if (entry.empty()) {
124 return false;
125 }
126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 if (out_ref) {
128 out_ref->package = package;
129 out_ref->type = *parsed_type;
130 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 }
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 if (out_private) {
134 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 }
136 return true;
137}
138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
140 bool* out_create, bool* out_private) {
141 StringPiece trimmed_str(util::TrimWhitespace(str));
142 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 return false;
144 }
145
146 bool create = false;
147 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 create = true;
152 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800153 }
154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 if (!ParseResourceName(
157 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 &priv)) {
159 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800160 }
161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 if (create && priv) {
163 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800164 }
165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 if (create && name.type != ResourceType::kId) {
167 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800168 }
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 if (out_ref) {
171 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 if (out_create) {
175 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800176 }
177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 if (out_private) {
179 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800180 }
181 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
183 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184}
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186bool IsReference(const StringPiece& str) {
187 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800188}
189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
191 StringPiece trimmed_str(util::TrimWhitespace(str));
192 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700193 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 }
195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 StringPiece package;
198 StringPiece type;
199 StringPiece entry;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 if (!ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 &package, &type, &entry)) {
202 return false;
203 }
204
205 if (!type.empty() && type != "attr") {
206 return false;
207 }
208
209 if (entry.empty()) {
210 return false;
211 }
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 if (out_ref) {
214 out_ref->package = package;
215 out_ref->type = ResourceType::kAttr;
216 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
218 return true;
219 }
220 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221}
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223bool IsAttributeReference(const StringPiece& str) {
224 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800225}
226
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227/*
228 * Style parent's are a bit different. We accept the following formats:
229 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800230 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800231 * ?[[*]package:]style/<entry>
232 * <[*]package>:[style/]<entry>
233 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
236 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 if (str.empty()) {
238 return {};
239 }
240
241 StringPiece name = str;
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 bool has_leading_identifiers = false;
244 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245
246 // Skip over these identifiers. A style's parent is a normal reference.
247 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 name = name.substr(1, name.size() - 1);
250 }
251
252 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 name = name.substr(1, name.size() - 1);
255 }
256
257 ResourceNameRef ref;
258 ref.type = ResourceType::kStyle;
259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 StringPiece type_str;
261 ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
262 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 const ResourceType* parsed_type = ParseResourceType(type_str);
265 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 err << "invalid resource type '" << type_str << "' for parent of style";
268 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 std::stringstream err;
275 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 return {};
278 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
286 StringPiece trimmed_str = util::TrimWhitespace(str);
287 const char* start = trimmed_str.data();
288 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700290
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 Reference ref;
292 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 start++;
295 p++;
296 }
297
298 StringPiece package;
299 StringPiece name;
300 while (p != end) {
301 if (*p == ':') {
302 package = StringPiece(start, p - start);
303 name = StringPiece(p + 1, end - (p + 1));
304 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700305 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 p++;
307 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700308
Adam Lesinskid5083f62017-01-16 15:07:21 -0800309 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700311}
312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
314 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 bool private_ref = false;
317 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return value;
321 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 if (ParseAttributeReference(str, &ref)) {
324 if (out_create) {
325 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700326 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
328 }
329 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330}
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332std::unique_ptr<BinaryPrimitive> TryParseNullOrEmpty(const StringPiece& str) {
333 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 android::Res_value value = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 if (trimmed_str == "@null") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
337 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
338 value.dataType = android::Res_value::TYPE_REFERENCE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 } else if (trimmed_str == "@empty") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime.
341 value.dataType = android::Res_value::TYPE_NULL;
342 value.data = android::Res_value::DATA_NULL_EMPTY;
343 } else {
344 return {};
345 }
346 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347}
348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700350 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 StringPiece trimmed_str(util::TrimWhitespace(str));
352 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 // Enum symbols are stored as @package:id/symbol resources,
354 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
356 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 android::Res_value value = {};
358 value.dataType = android::Res_value::TYPE_INT_DEC;
359 value.data = symbol.value;
360 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 }
363 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364}
365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700367 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 android::Res_value flags = {};
369 flags.dataType = android::Res_value::TYPE_INT_HEX;
370 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 }
376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 for (StringPiece part : util::Tokenize(str, '|')) {
378 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 bool flag_set = false;
381 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 // Flag symbols are stored as @package:id/symbol resources,
383 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 const ResourceName& flag_symbol_resource_name =
385 symbol.symbol.name.value();
386 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 break;
390 }
391 }
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 return {};
395 }
396 }
397 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398}
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 if (c >= '0' && c <= '9') {
402 return c - '0';
403 } else if (c >= 'a' && c <= 'f') {
404 return c - 'a' + 0xa;
405 } else if (c >= 'A' && c <= 'F') {
406 return c - 'A' + 0xa;
407 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return 0xffffffffu;
410 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700411}
412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
414 StringPiece color_str(util::TrimWhitespace(str));
415 const char* start = color_str.data();
416 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 if (len == 0 || start[0] != '#') {
418 return {};
419 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700420
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 android::Res_value value = {};
422 bool error = false;
423 if (len == 4) {
424 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
425 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 value.data |= ParseHex(start[1], &error) << 20;
427 value.data |= ParseHex(start[1], &error) << 16;
428 value.data |= ParseHex(start[2], &error) << 12;
429 value.data |= ParseHex(start[2], &error) << 8;
430 value.data |= ParseHex(start[3], &error) << 4;
431 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 } else if (len == 5) {
433 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 value.data |= ParseHex(start[1], &error) << 28;
435 value.data |= ParseHex(start[1], &error) << 24;
436 value.data |= ParseHex(start[2], &error) << 20;
437 value.data |= ParseHex(start[2], &error) << 16;
438 value.data |= ParseHex(start[3], &error) << 12;
439 value.data |= ParseHex(start[3], &error) << 8;
440 value.data |= ParseHex(start[4], &error) << 4;
441 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 } else if (len == 7) {
443 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
444 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 value.data |= ParseHex(start[1], &error) << 20;
446 value.data |= ParseHex(start[2], &error) << 16;
447 value.data |= ParseHex(start[3], &error) << 12;
448 value.data |= ParseHex(start[4], &error) << 8;
449 value.data |= ParseHex(start[5], &error) << 4;
450 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 } else if (len == 9) {
452 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 value.data |= ParseHex(start[1], &error) << 28;
454 value.data |= ParseHex(start[2], &error) << 24;
455 value.data |= ParseHex(start[3], &error) << 20;
456 value.data |= ParseHex(start[4], &error) << 16;
457 value.data |= ParseHex(start[5], &error) << 12;
458 value.data |= ParseHex(start[6], &error) << 8;
459 value.data |= ParseHex(start[7], &error) << 4;
460 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 } else {
462 return {};
463 }
464 return error ? std::unique_ptr<BinaryPrimitive>()
465 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700466}
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468Maybe<bool> ParseBool(const StringPiece& str) {
469 StringPiece trimmed_str(util::TrimWhitespace(str));
470 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
473 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 return Maybe<bool>(false);
475 }
476 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800477}
478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479Maybe<uint32_t> ParseInt(const StringPiece& str) {
480 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 android::Res_value value;
482 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
483 return value.data;
484 }
485 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700486}
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
489 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 android::Res_value value;
493 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
494 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
495 ResourceId id(value.data);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 return id;
498 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700499 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 }
501 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700502}
503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504Maybe<int> ParseSdkVersion(const StringPiece& str) {
505 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 android::Res_value value;
509 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
510 return static_cast<int>(value.data);
511 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700512
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
515 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 return entry.second;
517 }
518 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
522 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 android::Res_value value = {};
524 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800525
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 if (maybe_result.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 value.data = 0xffffffffu;
528 } else {
529 value.data = 0;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800530 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 return util::make_unique<BinaryPrimitive>(value);
532 }
533 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700534}
535
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
537 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 android::Res_value value;
539 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
540 return {};
541 }
542 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543}
544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
546 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 android::Res_value value;
548 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
549 return {};
550 }
551 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552}
553
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556 case android::Res_value::TYPE_NULL:
557 case android::Res_value::TYPE_REFERENCE:
558 case android::Res_value::TYPE_ATTRIBUTE:
559 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561
562 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700564
565 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700567
568 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570
571 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700573
574 case android::Res_value::TYPE_INT_DEC:
575 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 return android::ResTable_map::TYPE_INTEGER |
577 android::ResTable_map::TYPE_ENUM |
578 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700579
580 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582
583 case android::Res_value::TYPE_INT_COLOR_ARGB8:
584 case android::Res_value::TYPE_INT_COLOR_RGB8:
585 case android::Res_value::TYPE_INT_COLOR_ARGB4:
586 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700588
589 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 return 0;
591 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700592}
593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594std::unique_ptr<Item> TryParseItemForAttribute(
595 const StringPiece& value, uint32_t type_mask,
596 const std::function<void(const ResourceName&)>& on_create_reference) {
597 std::unique_ptr<BinaryPrimitive> null_or_empty = TryParseNullOrEmpty(value);
598 if (null_or_empty) {
599 return std::move(null_or_empty);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 std::unique_ptr<Reference> reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 if (create && on_create_reference) {
606 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700607 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return std::move(reference);
609 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 if (type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 // Try parsing this as a color.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 std::unique_ptr<BinaryPrimitive> color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 if (color) {
615 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700616 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if (type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 // Try parsing this as a boolean.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 std::unique_ptr<BinaryPrimitive> boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 if (boolean) {
623 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if (type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 // Try parsing this as an integer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 std::unique_ptr<BinaryPrimitive> integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 if (integer) {
631 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700632 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 const uint32_t float_mask = android::ResTable_map::TYPE_FLOAT |
636 android::ResTable_map::TYPE_DIMENSION |
637 android::ResTable_map::TYPE_FRACTION;
638 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 // Try parsing this as a float.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 std::unique_ptr<BinaryPrimitive> floating_point = TryParseFloat(value);
641 if (floating_point) {
642 if (type_mask &
643 AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
644 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700646 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 }
648 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700649}
650
651/**
652 * We successively try to parse the string as a resource type that the Attribute
653 * allows.
654 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 const std::function<void(const ResourceName&)>& on_create_reference) {
658 const uint32_t type_mask = attr->type_mask;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 std::unique_ptr<Item> value =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 if (value) {
662 return value;
663 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700664
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 // Try parsing this as an enum.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 std::unique_ptr<BinaryPrimitive> enum_value = TryParseEnumSymbol(attr, str);
668 if (enum_value) {
669 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 // Try parsing this as a flag.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 std::unique_ptr<BinaryPrimitive> flag_value = TryParseFlagSymbol(attr, str);
676 if (flag_value) {
677 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
680 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681}
682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683std::string BuildResourceFileName(const ResourceFile& res_file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 const NameMangler* mangler) {
685 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 out << "res/" << res_file.name.type;
687 if (res_file.config != ConfigDescription{}) {
688 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 }
690 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800691
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
693 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800699}
700
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701} // namespace ResourceUtils
702} // namespace aapt