blob: de00fcaea07bd1838d6ef0e114b701eb49d00d39 [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
Adam Lesinski2eed52e2018-02-21 15:55:58 -080021#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "androidfw/ResourceTypes.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080023#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070026#include "SdkConstants.h"
Adam Lesinski46708052017-09-29 14:49:15 -070027#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080028#include "text/Unicode.h"
29#include "text/Utf8Iterator.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080030#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "util/Util.h"
32
Adam Lesinski2eed52e2018-02-21 15:55:58 -080033using ::aapt::text::Utf8Iterator;
Adam Lesinski46708052017-09-29 14:49:15 -070034using ::android::StringPiece;
35using ::android::StringPiece16;
Adam Lesinski2eed52e2018-02-21 15:55:58 -080036using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038namespace aapt {
39namespace ResourceUtils {
40
Ryan Mitchell0ce89732018-10-03 09:20:57 -070041constexpr int32_t kNonBreakingSpace = 0xa0;
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043Maybe<ResourceName> ToResourceName(
44 const android::ResTable::resource_name& name_in) {
45 ResourceName name_out;
46 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return {};
48 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 name_out.package =
51 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 if (name_in.type) {
55 type = ParseResourceType(
56 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
57 } else if (name_in.type8) {
58 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 } else {
60 return {};
61 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 if (!type) {
64 return {};
65 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 if (name_in.name) {
70 name_out.entry =
71 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
72 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080073 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 } else {
75 return {};
76 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070078}
79
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
81 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (str.empty()) {
83 return false;
84 }
85
86 size_t offset = 0;
87 bool priv = false;
88 if (str.data()[0] == '*') {
89 priv = true;
90 offset = 1;
91 }
92
93 StringPiece package;
94 StringPiece type;
95 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -080096 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
97 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 return false;
99 }
100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 const ResourceType* parsed_type = ParseResourceType(type);
102 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 return false;
104 }
105
106 if (entry.empty()) {
107 return false;
108 }
109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 if (out_ref) {
111 out_ref->package = package;
112 out_ref->type = *parsed_type;
113 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 }
115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 if (out_private) {
117 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 }
119 return true;
120}
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
123 bool* out_create, bool* out_private) {
124 StringPiece trimmed_str(util::TrimWhitespace(str));
125 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 return false;
127 }
128
129 bool create = false;
130 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 create = true;
135 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800136 }
137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 if (!ParseResourceName(
140 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 &priv)) {
142 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800143 }
144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (create && priv) {
146 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800147 }
148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 if (create && name.type != ResourceType::kId) {
150 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800151 }
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (out_ref) {
154 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 if (out_create) {
158 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800159 }
160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 if (out_private) {
162 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800163 }
164 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 }
166 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167}
168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169bool IsReference(const StringPiece& str) {
170 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800171}
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
174 StringPiece trimmed_str(util::TrimWhitespace(str));
175 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 }
178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 StringPiece package;
181 StringPiece type;
182 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800183 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
184 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 return false;
186 }
187
188 if (!type.empty() && type != "attr") {
189 return false;
190 }
191
192 if (entry.empty()) {
193 return false;
194 }
195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 if (out_ref) {
197 out_ref->package = package;
198 out_ref->type = ResourceType::kAttr;
199 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 }
201 return true;
202 }
203 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204}
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206bool IsAttributeReference(const StringPiece& str) {
207 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800208}
209
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210/*
211 * Style parent's are a bit different. We accept the following formats:
212 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800213 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800214 * ?[[*]package:]style/<entry>
215 * <[*]package>:[style/]<entry>
216 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
219 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 if (str.empty()) {
221 return {};
222 }
223
224 StringPiece name = str;
225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 bool has_leading_identifiers = false;
227 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228
229 // Skip over these identifiers. A style's parent is a normal reference.
230 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 name = name.substr(1, name.size() - 1);
233 }
234
235 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 name = name.substr(1, name.size() - 1);
238 }
239
240 ResourceNameRef ref;
241 ref.type = ResourceType::kStyle;
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800244 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 const ResourceType* parsed_type = ParseResourceType(type_str);
248 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 err << "invalid resource type '" << type_str << "' for parent of style";
251 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 std::stringstream err;
258 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 return {};
261 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266}
267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
269 StringPiece trimmed_str = util::TrimWhitespace(str);
270 const char* start = trimmed_str.data();
271 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700273
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 Reference ref;
275 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 start++;
278 p++;
279 }
280
281 StringPiece package;
282 StringPiece name;
283 while (p != end) {
284 if (*p == ':') {
285 package = StringPiece(start, p - start);
286 name = StringPiece(p + 1, end - (p + 1));
287 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700288 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 p++;
290 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700291
Adam Lesinskid5083f62017-01-16 15:07:21 -0800292 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700294}
295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
297 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 bool private_ref = false;
300 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return value;
304 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 if (ParseAttributeReference(str, &ref)) {
307 if (out_create) {
308 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
311 }
312 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313}
314
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700315std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
316 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700318 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700320 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700322 return {};
323}
324
325std::unique_ptr<Reference> MakeNull() {
326 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
327 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
328 return util::make_unique<Reference>();
329}
330
331std::unique_ptr<BinaryPrimitive> MakeEmpty() {
332 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
333 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700334}
335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700337 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 StringPiece trimmed_str(util::TrimWhitespace(str));
339 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 // Enum symbols are stored as @package:id/symbol resources,
341 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
343 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 android::Res_value value = {};
345 value.dataType = android::Res_value::TYPE_INT_DEC;
346 value.data = symbol.value;
347 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
350 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351}
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700354 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 android::Res_value flags = {};
356 flags.dataType = android::Res_value::TYPE_INT_HEX;
357 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 }
363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 for (StringPiece part : util::Tokenize(str, '|')) {
365 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 bool flag_set = false;
368 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 // Flag symbols are stored as @package:id/symbol resources,
370 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 const ResourceName& flag_symbol_resource_name =
372 symbol.symbol.name.value();
373 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 break;
377 }
378 }
379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 return {};
382 }
383 }
384 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700385}
386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 if (c >= '0' && c <= '9') {
389 return c - '0';
390 } else if (c >= 'a' && c <= 'f') {
391 return c - 'a' + 0xa;
392 } else if (c >= 'A' && c <= 'F') {
393 return c - 'A' + 0xa;
394 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 return 0xffffffffu;
397 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398}
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
401 StringPiece color_str(util::TrimWhitespace(str));
402 const char* start = color_str.data();
403 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 if (len == 0 || start[0] != '#') {
405 return {};
406 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700407
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 android::Res_value value = {};
409 bool error = false;
410 if (len == 4) {
411 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
412 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 value.data |= ParseHex(start[1], &error) << 20;
414 value.data |= ParseHex(start[1], &error) << 16;
415 value.data |= ParseHex(start[2], &error) << 12;
416 value.data |= ParseHex(start[2], &error) << 8;
417 value.data |= ParseHex(start[3], &error) << 4;
418 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 } else if (len == 5) {
420 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 value.data |= ParseHex(start[1], &error) << 28;
422 value.data |= ParseHex(start[1], &error) << 24;
423 value.data |= ParseHex(start[2], &error) << 20;
424 value.data |= ParseHex(start[2], &error) << 16;
425 value.data |= ParseHex(start[3], &error) << 12;
426 value.data |= ParseHex(start[3], &error) << 8;
427 value.data |= ParseHex(start[4], &error) << 4;
428 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 } else if (len == 7) {
430 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
431 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 value.data |= ParseHex(start[1], &error) << 20;
433 value.data |= ParseHex(start[2], &error) << 16;
434 value.data |= ParseHex(start[3], &error) << 12;
435 value.data |= ParseHex(start[4], &error) << 8;
436 value.data |= ParseHex(start[5], &error) << 4;
437 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 } else if (len == 9) {
439 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 value.data |= ParseHex(start[1], &error) << 28;
441 value.data |= ParseHex(start[2], &error) << 24;
442 value.data |= ParseHex(start[3], &error) << 20;
443 value.data |= ParseHex(start[4], &error) << 16;
444 value.data |= ParseHex(start[5], &error) << 12;
445 value.data |= ParseHex(start[6], &error) << 8;
446 value.data |= ParseHex(start[7], &error) << 4;
447 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 } else {
449 return {};
450 }
451 return error ? std::unique_ptr<BinaryPrimitive>()
452 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453}
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455Maybe<bool> ParseBool(const StringPiece& str) {
456 StringPiece trimmed_str(util::TrimWhitespace(str));
457 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
460 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 return Maybe<bool>(false);
462 }
463 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800464}
465
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466Maybe<uint32_t> ParseInt(const StringPiece& str) {
467 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 android::Res_value value;
469 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
470 return value.data;
471 }
472 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700473}
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
476 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 android::Res_value value;
480 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
481 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
482 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800483 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 return id;
485 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700486 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 }
488 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700489}
490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491Maybe<int> ParseSdkVersion(const StringPiece& str) {
492 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 android::Res_value value;
496 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
497 return static_cast<int>(value.data);
498 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700499
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
502 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 return entry.second;
504 }
505 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700506}
507
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
509 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700510 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
511 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
513 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700514}
515
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700516std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
517 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
518 val ? 0xffffffffu : 0u);
519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700522 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 android::Res_value value;
524 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
525 return {};
526 }
527 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700528}
529
Shane Farmerd05b9132018-02-14 15:40:35 -0800530std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
531 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
532}
533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700535 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 android::Res_value value;
537 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
538 return {};
539 }
540 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541}
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700545 case android::Res_value::TYPE_NULL:
546 case android::Res_value::TYPE_REFERENCE:
547 case android::Res_value::TYPE_ATTRIBUTE:
548 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800549 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700551
552 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554
555 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700557
558 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700560
561 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700563
564 case android::Res_value::TYPE_INT_DEC:
565 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 return android::ResTable_map::TYPE_INTEGER |
567 android::ResTable_map::TYPE_ENUM |
568 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700569
570 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700572
573 case android::Res_value::TYPE_INT_COLOR_ARGB8:
574 case android::Res_value::TYPE_INT_COLOR_RGB8:
575 case android::Res_value::TYPE_INT_COLOR_ARGB4:
576 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578
579 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 return 0;
581 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582}
583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584std::unique_ptr<Item> TryParseItemForAttribute(
585 const StringPiece& value, uint32_t type_mask,
586 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700587 using android::ResTable_map;
588
589 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700591 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700593
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700595 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if (create && on_create_reference) {
598 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 return std::move(reference);
601 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700602
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700603 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700605 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 if (color) {
607 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700608 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700611 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700613 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 if (boolean) {
615 return std::move(boolean);
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 Lesinskibab4ef52017-06-01 15:22:57 -0700619 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700621 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 if (integer) {
623 return std::move(integer);
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 Lesinskibab4ef52017-06-01 15:22:57 -0700627 const uint32_t float_mask =
628 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700631 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700633 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700636 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 }
638 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700639}
640
641/**
642 * We successively try to parse the string as a resource type that the Attribute
643 * allows.
644 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700648 using android::ResTable_map;
649
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700651 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 if (value) {
653 return value;
654 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700655
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700656 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700658 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 if (enum_value) {
660 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700663
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700664 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700666 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 if (flag_value) {
668 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700669 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 }
671 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700672}
673
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700674std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 out << "res/" << res_file.name.type;
677 if (res_file.config != ConfigDescription{}) {
678 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
680 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
683 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800689}
690
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700691std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
692 const android::ResStringPool& src_pool,
693 const android::Res_value& res_value,
694 StringPool* dst_pool) {
695 if (type == ResourceType::kId) {
696 return util::make_unique<Id>();
697 }
698
699 const uint32_t data = util::DeviceToHost32(res_value.data);
700 switch (res_value.dataType) {
701 case android::Res_value::TYPE_STRING: {
702 const std::string str = util::GetString(src_pool, data);
703 const android::ResStringPool_span* spans = src_pool.styleAt(data);
704
705 // Check if the string has a valid style associated with it.
706 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
707 StyleString style_str = {str};
708 while (spans->name.index != android::ResStringPool_span::END) {
709 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
710 spans->firstChar, spans->lastChar});
711 spans++;
712 }
713 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700714 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700715 } else {
716 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
717 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700718 std::unique_ptr<FileReference> file_ref =
719 util::make_unique<FileReference>(dst_pool->MakeRef(
720 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000721 if (type == ResourceType::kRaw) {
722 file_ref->type = ResourceFile::Type::kUnknown;
723 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700724 file_ref->type = ResourceFile::Type::kBinaryXml;
725 } else if (util::EndsWith(*file_ref->path, ".png")) {
726 file_ref->type = ResourceFile::Type::kPng;
727 }
728 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700729 }
730
731 // There are no styles associated with this string, so treat it as a simple string.
732 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
733 }
734 } break;
735
736 case android::Res_value::TYPE_REFERENCE:
737 case android::Res_value::TYPE_ATTRIBUTE:
738 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
739 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
740 Reference::Type ref_type = Reference::Type::kResource;
741 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
742 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
743 ref_type = Reference::Type::kAttribute;
744 }
745
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700746 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700747 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700748 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700749 }
750
751 // This is a normal reference.
752 return util::make_unique<Reference>(data, ref_type);
753 } break;
754 }
755
756 // Treat this as a raw binary primitive.
757 return util::make_unique<BinaryPrimitive>(res_value);
758}
759
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800760// Converts the codepoint to UTF-8 and appends it to the string.
761static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
762 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
763 if (len < 0) {
764 return false;
765 }
766
767 const size_t start_append_pos = output->size();
768
769 // Make room for the next character.
770 output->resize(output->size() + len);
771
772 char* dst = &*(output->begin() + start_append_pos);
773 utf32_to_utf8(&codepoint, 1, dst, len + 1);
774 return true;
775}
776
777// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
778// Unicode codepoint represented by the escape sequence to the string.
779static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
780 char32_t code = 0;
781 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
782 char32_t codepoint = iter->Next();
783 char32_t a;
784 if (codepoint >= U'0' && codepoint <= U'9') {
785 a = codepoint - U'0';
786 } else if (codepoint >= U'a' && codepoint <= U'f') {
787 a = codepoint - U'a' + 10;
788 } else if (codepoint >= U'A' && codepoint <= U'F') {
789 a = codepoint - U'A' + 10;
790 } else {
791 return {};
792 }
793 code = (code << 4) | a;
794 }
795 return AppendCodepointToUtf8String(code, output);
796}
797
798StringBuilder::StringBuilder(bool preserve_spaces)
799 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
800}
801
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700802StringBuilder& StringBuilder::AppendText(const std::string& text, bool preserve_spaces) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800803 if (!error_.empty()) {
804 return *this;
805 }
806
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700807 // Enable preserving spaces if it is enabled for this append or the StringBuilder was constructed
808 // to preserve spaces
809 preserve_spaces = (preserve_spaces) ? preserve_spaces : preserve_spaces_;
810
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800811 const size_t previous_len = xml_string_.text.size();
812 Utf8Iterator iter(text);
813 while (iter.HasNext()) {
814 char32_t codepoint = iter.Next();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700815 if (!preserve_spaces && !quote_ && codepoint != kNonBreakingSpace && iswspace(codepoint)) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800816 if (!last_codepoint_was_space_) {
817 // Emit a space if it's the first.
818 xml_string_.text += ' ';
819 last_codepoint_was_space_ = true;
820 }
821
822 // Keep eating spaces.
823 continue;
824 }
825
826 // This is not a space.
827 last_codepoint_was_space_ = false;
828
829 if (codepoint == U'\\') {
830 if (iter.HasNext()) {
831 codepoint = iter.Next();
832 switch (codepoint) {
833 case U't':
834 xml_string_.text += '\t';
835 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800836 case U'n':
837 xml_string_.text += '\n';
838 break;
839
840 case U'#':
841 case U'@':
842 case U'?':
843 case U'"':
844 case U'\'':
845 case U'\\':
846 xml_string_.text += static_cast<char>(codepoint);
847 break;
848
849 case U'u':
850 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
851 error_ =
852 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
853 return *this;
854 }
855 break;
856
857 default:
858 // Ignore the escape character and just include the codepoint.
859 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
860 break;
861 }
862 }
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700863 } else if (!preserve_spaces && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800864 // Only toggle the quote state when we are not preserving spaces.
865 quote_ = !quote_;
866
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700867 } else if (!preserve_spaces && !quote_ && codepoint == U'\'') {
868 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800869 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
870 return *this;
871
872 } else {
873 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
874 }
875 }
876
877 // Accumulate the added string's UTF-16 length.
878 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
879 const size_t utf8_length = xml_string_.text.size();
880 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
881 if (len < 0) {
882 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
883 return *this;
884 }
885
886 utf16_len_ += static_cast<uint32_t>(len);
887 return *this;
888}
889
890StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
891 if (!error_.empty()) {
892 return 0u;
893 }
894
895 // When we start a span, all state associated with whitespace truncation and quotation is ended.
896 ResetTextState();
897 Span span;
898 span.name = name;
899 span.first_char = span.last_char = utf16_len_;
900 xml_string_.spans.push_back(std::move(span));
901 return xml_string_.spans.size() - 1;
902}
903
904void StringBuilder::EndSpan(SpanHandle handle) {
905 if (!error_.empty()) {
906 return;
907 }
908
909 // When we end a span, all state associated with whitespace truncation and quotation is ended.
910 ResetTextState();
911 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
912}
913
914StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
915 if (!error_.empty()) {
916 return 0u;
917 }
918
919 UntranslatableSection section;
920 section.start = section.end = xml_string_.text.size();
921 xml_string_.untranslatable_sections.push_back(section);
922 return xml_string_.untranslatable_sections.size() - 1;
923}
924
925void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
926 if (!error_.empty()) {
927 return;
928 }
929 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
930}
931
932FlattenedXmlString StringBuilder::GetFlattenedString() const {
933 return xml_string_;
934}
935
936std::string StringBuilder::to_string() const {
937 return xml_string_.text;
938}
939
940StringBuilder::operator bool() const {
941 return error_.empty();
942}
943
944std::string StringBuilder::GetError() const {
945 return error_;
946}
947
948void StringBuilder::ResetTextState() {
949 quote_ = preserve_spaces_;
950 last_codepoint_was_space_ = false;
951}
952
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953} // namespace ResourceUtils
954} // namespace aapt