blob: f0e4d9e7ce8c94bdf3591272e2941dea624410bc [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
Nicholas Lativy79f03962019-01-16 16:19:09 +000019#include <algorithm>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <sstream>
21
Adam Lesinski2eed52e2018-02-21 15:55:58 -080022#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "androidfw/ResourceTypes.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080024#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070027#include "SdkConstants.h"
Adam Lesinski46708052017-09-29 14:49:15 -070028#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Unicode.h"
30#include "text/Utf8Iterator.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080031#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "util/Util.h"
33
Adam Lesinski2eed52e2018-02-21 15:55:58 -080034using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020035using ::android::ConfigDescription;
Adam Lesinski46708052017-09-29 14:49:15 -070036using ::android::StringPiece;
37using ::android::StringPiece16;
Adam Lesinski2eed52e2018-02-21 15:55:58 -080038using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040namespace aapt {
41namespace ResourceUtils {
42
Ryan Mitchell0ce89732018-10-03 09:20:57 -070043constexpr int32_t kNonBreakingSpace = 0xa0;
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045Maybe<ResourceName> ToResourceName(
46 const android::ResTable::resource_name& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080047 // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 ResourceName name_out;
49 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return {};
51 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 name_out.package =
54 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 if (name_in.type) {
58 type = ParseResourceType(
59 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
60 } else if (name_in.type8) {
61 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 } else {
63 return {};
64 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 if (!type) {
67 return {};
68 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (name_in.name) {
73 name_out.entry =
74 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
75 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080076 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 } else {
78 return {};
79 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070081}
82
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080083Maybe<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in) {
84 ResourceName name_out;
85 if (!name_in.package) {
86 return {};
87 }
88
89 name_out.package = std::string(name_in.package, name_in.package_len);
90
91 const ResourceType* type;
92 if (name_in.type16) {
93 type = ParseResourceType(
94 util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len)));
95 } else if (name_in.type) {
96 type = ParseResourceType(StringPiece(name_in.type, name_in.type_len));
97 } else {
98 return {};
99 }
100
101 if (!type) {
102 return {};
103 }
104
105 name_out.type = *type;
106
107 if (name_in.entry16) {
108 name_out.entry =
109 util::Utf16ToUtf8(StringPiece16(name_in.entry16, name_in.entry_len));
110 } else if (name_in.entry) {
111 name_out.entry = std::string(name_in.entry, name_in.entry_len);
112 } else {
113 return {};
114 }
115 return name_out;
116}
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
119 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 if (str.empty()) {
121 return false;
122 }
123
124 size_t offset = 0;
125 bool priv = false;
126 if (str.data()[0] == '*') {
127 priv = true;
128 offset = 1;
129 }
130
131 StringPiece package;
132 StringPiece type;
133 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800134 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
135 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 return false;
137 }
138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 const ResourceType* parsed_type = ParseResourceType(type);
140 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 return false;
142 }
143
144 if (entry.empty()) {
145 return false;
146 }
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (out_ref) {
149 out_ref->package = package;
150 out_ref->type = *parsed_type;
151 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 }
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (out_private) {
155 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return true;
158}
159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
161 bool* out_create, bool* out_private) {
162 StringPiece trimmed_str(util::TrimWhitespace(str));
163 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 return false;
165 }
166
167 bool create = false;
168 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 create = true;
173 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800174 }
175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 if (!ParseResourceName(
178 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 &priv)) {
180 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800181 }
182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 if (create && priv) {
184 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800185 }
186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 if (create && name.type != ResourceType::kId) {
188 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800189 }
190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 if (out_ref) {
192 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 }
194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 if (out_create) {
196 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800197 }
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (out_private) {
200 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800201 }
202 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205}
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207bool IsReference(const StringPiece& str) {
208 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800209}
210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
212 StringPiece trimmed_str(util::TrimWhitespace(str));
213 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 StringPiece package;
219 StringPiece type;
220 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800221 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
222 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return false;
224 }
225
226 if (!type.empty() && type != "attr") {
227 return false;
228 }
229
230 if (entry.empty()) {
231 return false;
232 }
233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 if (out_ref) {
235 out_ref->package = package;
236 out_ref->type = ResourceType::kAttr;
237 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
239 return true;
240 }
241 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242}
243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244bool IsAttributeReference(const StringPiece& str) {
245 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800246}
247
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248/*
249 * Style parent's are a bit different. We accept the following formats:
250 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800251 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800252 * ?[[*]package:]style/<entry>
253 * <[*]package>:[style/]<entry>
254 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
257 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 if (str.empty()) {
259 return {};
260 }
261
262 StringPiece name = str;
263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 bool has_leading_identifiers = false;
265 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266
267 // Skip over these identifiers. A style's parent is a normal reference.
268 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 name = name.substr(1, name.size() - 1);
271 }
272
273 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 name = name.substr(1, name.size() - 1);
276 }
277
278 ResourceNameRef ref;
279 ref.type = ResourceType::kStyle;
280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800282 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 const ResourceType* parsed_type = ParseResourceType(type_str);
286 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 err << "invalid resource type '" << type_str << "' for parent of style";
289 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 std::stringstream err;
296 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return {};
299 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304}
305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
307 StringPiece trimmed_str = util::TrimWhitespace(str);
308 const char* start = trimmed_str.data();
309 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700311
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 Reference ref;
313 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 start++;
316 p++;
317 }
318
319 StringPiece package;
320 StringPiece name;
321 while (p != end) {
322 if (*p == ':') {
323 package = StringPiece(start, p - start);
324 name = StringPiece(p + 1, end - (p + 1));
325 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700326 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 p++;
328 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700329
Adam Lesinskid5083f62017-01-16 15:07:21 -0800330 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700332}
333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
335 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 bool private_ref = false;
338 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 return value;
342 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 if (ParseAttributeReference(str, &ref)) {
345 if (out_create) {
346 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
349 }
350 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351}
352
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700353std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
354 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700356 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700358 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700360 return {};
361}
362
363std::unique_ptr<Reference> MakeNull() {
364 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
365 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
366 return util::make_unique<Reference>();
367}
368
369std::unique_ptr<BinaryPrimitive> MakeEmpty() {
370 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
371 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700372}
373
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700375 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 StringPiece trimmed_str(util::TrimWhitespace(str));
377 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 // Enum symbols are stored as @package:id/symbol resources,
379 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
381 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 android::Res_value value = {};
383 value.dataType = android::Res_value::TYPE_INT_DEC;
384 value.data = symbol.value;
385 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700386 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 }
388 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389}
390
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700392 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 android::Res_value flags = {};
394 flags.dataType = android::Res_value::TYPE_INT_HEX;
395 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 }
401
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800402 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 bool flag_set = false;
406 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 // Flag symbols are stored as @package:id/symbol resources,
408 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 const ResourceName& flag_symbol_resource_name =
410 symbol.symbol.name.value();
411 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 break;
415 }
416 }
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 return {};
420 }
421 }
422 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700423}
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 if (c >= '0' && c <= '9') {
427 return c - '0';
428 } else if (c >= 'a' && c <= 'f') {
429 return c - 'a' + 0xa;
430 } else if (c >= 'A' && c <= 'F') {
431 return c - 'A' + 0xa;
432 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 return 0xffffffffu;
435 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436}
437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
439 StringPiece color_str(util::TrimWhitespace(str));
440 const char* start = color_str.data();
441 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 if (len == 0 || start[0] != '#') {
443 return {};
444 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700445
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 android::Res_value value = {};
447 bool error = false;
448 if (len == 4) {
449 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
450 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 value.data |= ParseHex(start[1], &error) << 20;
452 value.data |= ParseHex(start[1], &error) << 16;
453 value.data |= ParseHex(start[2], &error) << 12;
454 value.data |= ParseHex(start[2], &error) << 8;
455 value.data |= ParseHex(start[3], &error) << 4;
456 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 } else if (len == 5) {
458 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 value.data |= ParseHex(start[1], &error) << 28;
460 value.data |= ParseHex(start[1], &error) << 24;
461 value.data |= ParseHex(start[2], &error) << 20;
462 value.data |= ParseHex(start[2], &error) << 16;
463 value.data |= ParseHex(start[3], &error) << 12;
464 value.data |= ParseHex(start[3], &error) << 8;
465 value.data |= ParseHex(start[4], &error) << 4;
466 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 } else if (len == 7) {
468 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
469 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 value.data |= ParseHex(start[1], &error) << 20;
471 value.data |= ParseHex(start[2], &error) << 16;
472 value.data |= ParseHex(start[3], &error) << 12;
473 value.data |= ParseHex(start[4], &error) << 8;
474 value.data |= ParseHex(start[5], &error) << 4;
475 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 } else if (len == 9) {
477 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 value.data |= ParseHex(start[1], &error) << 28;
479 value.data |= ParseHex(start[2], &error) << 24;
480 value.data |= ParseHex(start[3], &error) << 20;
481 value.data |= ParseHex(start[4], &error) << 16;
482 value.data |= ParseHex(start[5], &error) << 12;
483 value.data |= ParseHex(start[6], &error) << 8;
484 value.data |= ParseHex(start[7], &error) << 4;
485 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 } else {
487 return {};
488 }
489 return error ? std::unique_ptr<BinaryPrimitive>()
490 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493Maybe<bool> ParseBool(const StringPiece& str) {
494 StringPiece trimmed_str(util::TrimWhitespace(str));
495 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
498 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 return Maybe<bool>(false);
500 }
501 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800502}
503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504Maybe<uint32_t> ParseInt(const StringPiece& str) {
505 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 android::Res_value value;
507 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
508 return value.data;
509 }
510 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700511}
512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
514 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 android::Res_value value;
518 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
519 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
520 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800521 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 return id;
523 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700524 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 }
526 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700527}
528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529Maybe<int> ParseSdkVersion(const StringPiece& str) {
530 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 android::Res_value value;
534 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
535 return static_cast<int>(value.data);
536 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700537
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
540 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 return entry.second;
542 }
Nicholas Lativy79f03962019-01-16 16:19:09 +0000543
544 // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value.
545 const StringPiece::const_iterator begin = std::begin(trimmed_str);
546 const StringPiece::const_iterator end = std::end(trimmed_str);
547 const StringPiece::const_iterator codename_end = std::find(begin, end, '.');
548 if (codename_end != end && entry.first == trimmed_str.substr(begin, codename_end)) {
549 return entry.second;
550 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700552}
553
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
555 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700556 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
557 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 }
559 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700560}
561
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700562std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
563 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
564 val ? 0xffffffffu : 0u);
565}
566
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700568 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 android::Res_value value;
570 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
571 return {};
572 }
573 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700574}
575
Shane Farmerd05b9132018-02-14 15:40:35 -0800576std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
577 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
578}
579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700581 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 android::Res_value value;
583 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
584 return {};
585 }
586 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587}
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591 case android::Res_value::TYPE_NULL:
592 case android::Res_value::TYPE_REFERENCE:
593 case android::Res_value::TYPE_ATTRIBUTE:
594 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800595 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597
598 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600
601 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
604 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
607 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
610 case android::Res_value::TYPE_INT_DEC:
611 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return android::ResTable_map::TYPE_INTEGER |
613 android::ResTable_map::TYPE_ENUM |
614 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700615
616 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700618
619 case android::Res_value::TYPE_INT_COLOR_ARGB8:
620 case android::Res_value::TYPE_INT_COLOR_RGB8:
621 case android::Res_value::TYPE_INT_COLOR_ARGB4:
622 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
625 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 return 0;
627 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700628}
629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630std::unique_ptr<Item> TryParseItemForAttribute(
631 const StringPiece& value, uint32_t type_mask,
632 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700633 using android::ResTable_map;
634
635 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700637 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700639
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700641 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 if (create && on_create_reference) {
644 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700645 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 return std::move(reference);
647 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700648
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700649 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700651 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 if (color) {
653 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700654 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700656
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700657 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700659 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 if (boolean) {
661 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700664
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700665 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700667 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 if (integer) {
669 return std::move(integer);
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 Lesinskibab4ef52017-06-01 15:22:57 -0700673 const uint32_t float_mask =
674 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700677 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700679 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700682 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
684 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700685}
686
687/**
688 * We successively try to parse the string as a resource type that the Attribute
689 * allows.
690 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700694 using android::ResTable_map;
695
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700697 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 if (value) {
699 return value;
700 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700701
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700702 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700704 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 if (enum_value) {
706 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700709
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700710 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700712 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713 if (flag_value) {
714 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700715 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 }
717 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700718}
719
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700720std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 out << "res/" << res_file.name.type;
723 if (res_file.config != ConfigDescription{}) {
724 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 }
726 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
729 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800735}
736
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700737std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
738 const android::ResStringPool& src_pool,
739 const android::Res_value& res_value,
740 StringPool* dst_pool) {
741 if (type == ResourceType::kId) {
742 return util::make_unique<Id>();
743 }
744
745 const uint32_t data = util::DeviceToHost32(res_value.data);
746 switch (res_value.dataType) {
747 case android::Res_value::TYPE_STRING: {
748 const std::string str = util::GetString(src_pool, data);
749 const android::ResStringPool_span* spans = src_pool.styleAt(data);
750
751 // Check if the string has a valid style associated with it.
752 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
753 StyleString style_str = {str};
754 while (spans->name.index != android::ResStringPool_span::END) {
755 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
756 spans->firstChar, spans->lastChar});
757 spans++;
758 }
759 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700760 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700761 } else {
762 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
763 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700764 std::unique_ptr<FileReference> file_ref =
765 util::make_unique<FileReference>(dst_pool->MakeRef(
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000766 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000767 if (type == ResourceType::kRaw) {
768 file_ref->type = ResourceFile::Type::kUnknown;
769 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700770 file_ref->type = ResourceFile::Type::kBinaryXml;
771 } else if (util::EndsWith(*file_ref->path, ".png")) {
772 file_ref->type = ResourceFile::Type::kPng;
773 }
774 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700775 }
776
777 // There are no styles associated with this string, so treat it as a simple string.
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000778 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700779 }
780 } break;
781
782 case android::Res_value::TYPE_REFERENCE:
783 case android::Res_value::TYPE_ATTRIBUTE:
784 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
785 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
786 Reference::Type ref_type = Reference::Type::kResource;
787 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
788 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
789 ref_type = Reference::Type::kAttribute;
790 }
791
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700792 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700793 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700794 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700795 }
796
797 // This is a normal reference.
798 return util::make_unique<Reference>(data, ref_type);
799 } break;
800 }
801
802 // Treat this as a raw binary primitive.
803 return util::make_unique<BinaryPrimitive>(res_value);
804}
805
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800806// Converts the codepoint to UTF-8 and appends it to the string.
807static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
808 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
809 if (len < 0) {
810 return false;
811 }
812
813 const size_t start_append_pos = output->size();
814
815 // Make room for the next character.
816 output->resize(output->size() + len);
817
818 char* dst = &*(output->begin() + start_append_pos);
819 utf32_to_utf8(&codepoint, 1, dst, len + 1);
820 return true;
821}
822
823// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
824// Unicode codepoint represented by the escape sequence to the string.
825static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
826 char32_t code = 0;
827 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
828 char32_t codepoint = iter->Next();
829 char32_t a;
830 if (codepoint >= U'0' && codepoint <= U'9') {
831 a = codepoint - U'0';
832 } else if (codepoint >= U'a' && codepoint <= U'f') {
833 a = codepoint - U'a' + 10;
834 } else if (codepoint >= U'A' && codepoint <= U'F') {
835 a = codepoint - U'A' + 10;
836 } else {
837 return {};
838 }
839 code = (code << 4) | a;
840 }
841 return AppendCodepointToUtf8String(code, output);
842}
843
844StringBuilder::StringBuilder(bool preserve_spaces)
845 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
846}
847
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800848StringBuilder& StringBuilder::AppendText(const std::string& text) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800849 if (!error_.empty()) {
850 return *this;
851 }
852
853 const size_t previous_len = xml_string_.text.size();
854 Utf8Iterator iter(text);
855 while (iter.HasNext()) {
856 char32_t codepoint = iter.Next();
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800857 if (!preserve_spaces_ && !quote_ && codepoint != kNonBreakingSpace && iswspace(codepoint)) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800858 if (!last_codepoint_was_space_) {
859 // Emit a space if it's the first.
860 xml_string_.text += ' ';
861 last_codepoint_was_space_ = true;
862 }
863
864 // Keep eating spaces.
865 continue;
866 }
867
868 // This is not a space.
869 last_codepoint_was_space_ = false;
870
871 if (codepoint == U'\\') {
872 if (iter.HasNext()) {
873 codepoint = iter.Next();
874 switch (codepoint) {
875 case U't':
876 xml_string_.text += '\t';
877 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800878 case U'n':
879 xml_string_.text += '\n';
880 break;
881
882 case U'#':
883 case U'@':
884 case U'?':
885 case U'"':
886 case U'\'':
887 case U'\\':
888 xml_string_.text += static_cast<char>(codepoint);
889 break;
890
891 case U'u':
892 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
893 error_ =
894 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
895 return *this;
896 }
897 break;
898
899 default:
900 // Ignore the escape character and just include the codepoint.
901 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
902 break;
903 }
904 }
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800905 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800906 // Only toggle the quote state when we are not preserving spaces.
907 quote_ = !quote_;
908
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800909 } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700910 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800911 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
912 return *this;
913
914 } else {
915 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
916 }
917 }
918
919 // Accumulate the added string's UTF-16 length.
920 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
921 const size_t utf8_length = xml_string_.text.size();
922 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
923 if (len < 0) {
924 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
925 return *this;
926 }
927
928 utf16_len_ += static_cast<uint32_t>(len);
929 return *this;
930}
931
932StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
933 if (!error_.empty()) {
934 return 0u;
935 }
936
937 // When we start a span, all state associated with whitespace truncation and quotation is ended.
938 ResetTextState();
939 Span span;
940 span.name = name;
941 span.first_char = span.last_char = utf16_len_;
942 xml_string_.spans.push_back(std::move(span));
943 return xml_string_.spans.size() - 1;
944}
945
946void StringBuilder::EndSpan(SpanHandle handle) {
947 if (!error_.empty()) {
948 return;
949 }
950
951 // When we end a span, all state associated with whitespace truncation and quotation is ended.
952 ResetTextState();
953 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
954}
955
956StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
957 if (!error_.empty()) {
958 return 0u;
959 }
960
961 UntranslatableSection section;
962 section.start = section.end = xml_string_.text.size();
963 xml_string_.untranslatable_sections.push_back(section);
964 return xml_string_.untranslatable_sections.size() - 1;
965}
966
967void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
968 if (!error_.empty()) {
969 return;
970 }
971 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
972}
973
974FlattenedXmlString StringBuilder::GetFlattenedString() const {
975 return xml_string_;
976}
977
978std::string StringBuilder::to_string() const {
979 return xml_string_.text;
980}
981
982StringBuilder::operator bool() const {
983 return error_.empty();
984}
985
986std::string StringBuilder::GetError() const {
987 return error_;
988}
989
990void StringBuilder::ResetTextState() {
991 quote_ = preserve_spaces_;
992 last_codepoint_was_space_ = false;
993}
994
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995} // namespace ResourceUtils
996} // namespace aapt