blob: 728e35a266b7b0eb1b0ff0bff3683a32f359e34f [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceValues.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
Adam Lesinski93190b72017-11-03 15:20:17 -070020#include <cinttypes>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <limits>
22#include <set>
Adam Lesinski93190b72017-11-03 15:20:17 -070023#include <sstream>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080026#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ValueVisitor.h"
Brandon Liuf24b9a42023-03-31 22:37:42 +000028#include "android-base/stringprintf.h"
29#include "androidfw/ResourceTypes.h"
30#include "io/StringStream.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070031#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070032
Adam Lesinski93190b72017-11-03 15:20:17 -070033using ::aapt::text::Printer;
34using ::android::StringPiece;
35using ::android::base::StringPrintf;
36
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037namespace aapt {
38
Adam Lesinski93190b72017-11-03 15:20:17 -070039void Value::PrettyPrint(Printer* printer) const {
40 std::ostringstream str_stream;
41 Print(&str_stream);
42 printer->Print(str_stream.str());
43}
44
Adam Lesinski5924d8c2017-05-30 15:15:58 -070045std::ostream& operator<<(std::ostream& out, const Value& value) {
46 value.Print(&out);
47 return out;
48}
49
Ryan Mitchellefcdb952021-04-14 17:31:37 -070050std::unique_ptr<Value> Value::Transform(ValueTransformer& transformer) const {
51 return std::unique_ptr<Value>(this->TransformValueImpl(transformer));
52}
53
54std::unique_ptr<Item> Item::Transform(ValueTransformer& transformer) const {
55 return std::unique_ptr<Item>(this->TransformItemImpl(transformer));
56}
57
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070059void BaseValue<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061}
62
63template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070064void BaseValue<Derived>::Accept(ConstValueVisitor* visitor) const {
65 visitor->Visit(static_cast<const Derived*>(this));
66}
67
68template <typename Derived>
69void BaseItem<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071}
72
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070073template <typename Derived>
74void BaseItem<Derived>::Accept(ConstValueVisitor* visitor) const {
75 visitor->Visit(static_cast<const Derived*>(this));
76}
77
Jeremy Meyer56f36e82022-05-20 20:35:42 +000078RawString::RawString(const android::StringPool::Ref& ref) : value(ref) {
79}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081bool RawString::Equals(const Value* value) const {
82 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (!other) {
84 return false;
85 }
86 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070087}
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089bool RawString::Flatten(android::Res_value* out_value) const {
90 out_value->dataType = android::Res_value::TYPE_STRING;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000091 out_value->data = android::util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093}
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097}
98
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110bool Reference::Equals(const Value* value) const {
111 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 if (!other) {
113 return false;
114 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700115 return reference_type == other->reference_type && private_reference == other->private_reference &&
116 id == other->id && name == other->name && type_flags == other->type_flags;
Adam Lesinski458b8772016-04-25 14:20:21 -0700117}
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119bool Reference::Flatten(android::Res_value* out_value) const {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000120 if (name && name.value().type.type == ResourceType::kMacro) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700121 return false;
122 }
123
Ryan Mitchell4382e442021-07-14 12:53:01 -0700124 const ResourceId resid = id.value_or(ResourceId(0));
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800125 const bool dynamic = resid.is_valid() && is_dynamic;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800126
127 if (reference_type == Reference::Type::kResource) {
128 if (dynamic) {
129 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
130 } else {
131 out_value->dataType = android::Res_value::TYPE_REFERENCE;
132 }
133 } else {
134 if (dynamic) {
135 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
136 } else {
137 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
138 }
139 }
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000140 out_value->data = android::util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144void Reference::Print(std::ostream* out) const {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700145 if (reference_type == Type::kResource) {
146 *out << "(reference) @";
147 if (!name && !id) {
148 *out << "null";
149 return;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 } else {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700152 *out << "(attr-reference) ?";
153 }
154
155 if (private_reference) {
156 *out << "*";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (name) {
160 *out << name.value();
161 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800163 if (id && id.value().is_valid()) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700164 if (name) {
165 *out << " ";
166 }
167 *out << id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169}
170
Adam Lesinski93190b72017-11-03 15:20:17 -0700171static void PrettyPrintReferenceImpl(const Reference& ref, bool print_package, Printer* printer) {
172 switch (ref.reference_type) {
173 case Reference::Type::kResource:
174 printer->Print("@");
175 break;
176
177 case Reference::Type::kAttribute:
178 printer->Print("?");
179 break;
180 }
181
182 if (!ref.name && !ref.id) {
183 printer->Print("null");
184 return;
185 }
186
187 if (ref.private_reference) {
188 printer->Print("*");
189 }
190
191 if (ref.name) {
192 const ResourceName& name = ref.name.value();
193 if (print_package) {
194 printer->Print(name.to_string());
195 } else {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000196 printer->Print(name.type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700197 printer->Print("/");
198 printer->Print(name.entry);
199 }
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800200 } else if (ref.id && ref.id.value().is_valid()) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700201 printer->Print(ref.id.value().to_string());
202 }
203}
204
205void Reference::PrettyPrint(Printer* printer) const {
206 PrettyPrintReferenceImpl(*this, true /*print_package*/, printer);
207}
208
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700209void Reference::PrettyPrint(StringPiece package, Printer* printer) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700210 const bool print_package = name ? package != name.value().package : true;
211 PrettyPrintReferenceImpl(*this, print_package, printer);
212}
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214bool Id::Equals(const Value* value) const {
215 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700216}
217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000220 out->data = android::util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222}
223
Adam Lesinski93190b72017-11-03 15:20:17 -0700224void Id::Print(std::ostream* out) const {
225 *out << "(id)";
226}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000228String::String(const android::StringPool::Ref& ref) : value(ref) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700229}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231bool String::Equals(const Value* value) const {
232 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 if (!other) {
234 return false;
235 }
Adam Lesinski75421622017-01-06 15:20:04 -0800236
237 if (this->value != other->value) {
238 return false;
239 }
240
241 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
242 return false;
243 }
244
245 auto other_iter = other->untranslatable_sections.begin();
246 for (const UntranslatableSection& this_section : untranslatable_sections) {
247 if (this_section != *other_iter) {
248 return false;
249 }
250 ++other_iter;
251 }
252 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253}
254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 return false;
259 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 out_value->dataType = android::Res_value::TYPE_STRING;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000262 out_value->data = android::util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264}
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268}
269
Adam Lesinski93190b72017-11-03 15:20:17 -0700270void String::PrettyPrint(Printer* printer) const {
271 printer->Print("\"");
272 printer->Print(*value);
273 printer->Print("\"");
274}
275
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000276StyledString::StyledString(const android::StringPool::StyleRef& ref) : value(ref) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700277}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279bool StyledString::Equals(const Value* value) const {
280 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700282 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 }
284
Adam Lesinski75421622017-01-06 15:20:04 -0800285 if (this->value != other->value) {
286 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 }
Adam Lesinski75421622017-01-06 15:20:04 -0800288
289 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
290 return false;
291 }
292
293 auto other_iter = other->untranslatable_sections.begin();
294 for (const UntranslatableSection& this_section : untranslatable_sections) {
295 if (this_section != *other_iter) {
296 return false;
297 }
298 ++other_iter;
299 }
300 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301}
302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303bool StyledString::Flatten(android::Res_value* out_value) const {
304 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return false;
306 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 out_value->dataType = android::Res_value::TYPE_STRING;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000309 out_value->data = android::util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800311}
312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313void StyledString::Print(std::ostream* out) const {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700314 *out << "(styled string) \"" << value->value << "\"";
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000315 for (const android::StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700316 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318}
319
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000320FileReference::FileReference(const android::StringPool::Ref& _path) : path(_path) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700321}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323bool FileReference::Equals(const Value* value) const {
324 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 if (!other) {
326 return false;
327 }
Adam Lesinski75421622017-01-06 15:20:04 -0800328 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700329}
330
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331bool FileReference::Flatten(android::Res_value* out_value) const {
332 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 return false;
334 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 out_value->dataType = android::Res_value::TYPE_STRING;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000337 out_value->data = android::util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800339}
340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 *out << "(file) " << *path;
Adam Lesinskia65bbdf2018-02-15 12:39:44 -0800343 switch (type) {
344 case ResourceFile::Type::kBinaryXml:
345 *out << " type=XML";
346 break;
347 case ResourceFile::Type::kProtoXml:
348 *out << " type=protoXML";
349 break;
350 case ResourceFile::Type::kPng:
351 *out << " type=PNG";
352 break;
353 default:
354 break;
355 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356}
357
Adam Lesinski93190b72017-11-03 15:20:17 -0700358BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
359}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 value.dataType = dataType;
363 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364}
365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366bool BinaryPrimitive::Equals(const Value* value) const {
367 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 if (!other) {
369 return false;
370 }
371 return this->value.dataType == other->value.dataType &&
372 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700373}
374
Adam Lesinski93190b72017-11-03 15:20:17 -0700375bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 out_value->dataType = value.dataType;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000377 out_value->data = android::util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379}
380
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700382 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
383}
384
385static std::string ComplexToString(uint32_t complex_value, bool fraction) {
386 using ::android::Res_value;
387
388 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
389
390 // Determine the radix that was used.
391 const uint32_t radix =
392 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
393 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
394 Res_value::COMPLEX_MANTISSA_MASK}
395 << kRadixShifts[radix];
396 const float value = mantissa * (1.0f / (1 << 23));
397
398 std::string str = StringPrintf("%f", value);
399
400 const int unit_type =
401 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
402 if (fraction) {
403 switch (unit_type) {
404 case Res_value::COMPLEX_UNIT_FRACTION:
405 str += "%";
406 break;
407 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
408 str += "%p";
409 break;
410 default:
411 str += "???";
412 break;
413 }
414 } else {
415 switch (unit_type) {
416 case Res_value::COMPLEX_UNIT_PX:
417 str += "px";
418 break;
419 case Res_value::COMPLEX_UNIT_DIP:
420 str += "dp";
421 break;
422 case Res_value::COMPLEX_UNIT_SP:
423 str += "sp";
424 break;
425 case Res_value::COMPLEX_UNIT_PT:
426 str += "pt";
427 break;
428 case Res_value::COMPLEX_UNIT_IN:
429 str += "in";
430 break;
431 case Res_value::COMPLEX_UNIT_MM:
432 str += "mm";
433 break;
434 default:
435 str += "???";
436 break;
437 }
438 }
439 return str;
440}
441
442void BinaryPrimitive::PrettyPrint(Printer* printer) const {
443 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700445 case Res_value::TYPE_NULL:
446 if (value.data == Res_value::DATA_NULL_EMPTY) {
447 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700448 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700449 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700450 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700452
453 case Res_value::TYPE_INT_DEC:
454 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700456
457 case Res_value::TYPE_INT_HEX:
458 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700460
461 case Res_value::TYPE_INT_BOOLEAN:
462 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700464
465 case Res_value::TYPE_INT_COLOR_ARGB8:
466 case Res_value::TYPE_INT_COLOR_RGB8:
467 case Res_value::TYPE_INT_COLOR_ARGB4:
468 case Res_value::TYPE_INT_COLOR_RGB4:
469 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700471
472 case Res_value::TYPE_FLOAT:
473 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
474 break;
475
476 case Res_value::TYPE_DIMENSION:
477 printer->Print(ComplexToString(value.data, false /*fraction*/));
478 break;
479
480 case Res_value::TYPE_FRACTION:
481 printer->Print(ComplexToString(value.data, true /*fraction*/));
482 break;
483
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700485 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 break;
487 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488}
489
Brandon Liuf24b9a42023-03-31 22:37:42 +0000490std::string BinaryPrimitive::toPrettyString() const {
491 std::string str;
492 io::StringOutputStream out(&str);
493 text::Printer printer(&out);
494 this->PrettyPrint(&printer);
495 out.Flush();
496 return str;
497}
498
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800499Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 : type_mask(t),
501 min_int(std::numeric_limits<int32_t>::min()),
502 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503}
504
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700505std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
506 if (s.symbol.name) {
507 out << s.symbol.name.value().entry;
508 } else {
509 out << "???";
510 }
511 return out << "=" << s.value;
512}
513
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700514template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800515constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700517}
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519bool Attribute::Equals(const Value* value) const {
520 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 if (!other) {
522 return false;
523 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700524
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 if (symbols.size() != other->symbols.size()) {
526 return false;
527 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700528
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700529 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 return false;
531 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 std::vector<const Symbol*> sorted_a;
534 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800535 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700536 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
537 return a->symbol.name < b->symbol.name;
538 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700541 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
542 add_pointer<const Symbol>);
543 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
544 return a->symbol.name < b->symbol.name;
545 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700549 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700551}
552
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800553bool Attribute::IsCompatibleWith(const Attribute& attr) const {
554 // If the high bits are set on any of these attribute type masks, then they are incompatible.
555 // We don't check that flags and enums are identical.
556 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
557 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
558 return false;
559 }
560
561 // Every attribute accepts a reference.
562 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
563 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
564 return this_type_mask == that_type_mask;
565}
566
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700567std::string Attribute::MaskString(uint32_t type_mask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700569 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571
Adam Lesinski93190b72017-11-03 15:20:17 -0700572 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 if (!set) {
576 set = true;
577 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700578 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800579 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700580 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 if (!set) {
585 set = true;
586 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700587 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800588 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700589 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 if (!set) {
594 set = true;
595 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700596 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700598 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 if (!set) {
603 set = true;
604 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700605 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700607 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 if (!set) {
612 set = true;
613 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700614 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700616 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 if (!set) {
621 set = true;
622 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700623 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700625 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 if (!set) {
630 set = true;
631 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700632 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700634 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 if (!set) {
639 set = true;
640 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700641 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800642 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700643 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800645
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 if (!set) {
648 set = true;
649 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700650 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800651 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700652 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 if (!set) {
657 set = true;
658 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700659 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800660 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700661 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700663 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700664}
665
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700666std::string Attribute::MaskString() const {
667 return MaskString(type_mask);
668}
669
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700671 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800672
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800676
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 if (min_int != std::numeric_limits<int32_t>::min()) {
678 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 if (max_int != std::numeric_limits<int32_t>::max()) {
682 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 *out << " [weak]";
687 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800688}
689
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700690static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000691 android::DiagMessage* out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700692 *out_msg << "expected";
693 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
694 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800696
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700697 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
698 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800700
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700701 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
702 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800704
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700705 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
706 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800708
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700709 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
710 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800712
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700713 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
714 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800716
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700717 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
718 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800720
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700721 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
722 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800724
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700725 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
726 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800728
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700729 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
730 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800732
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700733 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800734}
735
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000736bool Attribute::Matches(const Item& item, android::DiagMessage* out_msg) const {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700737 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
738 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
739 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
740 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
741
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700743 item.Flatten(&val);
744
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000745 const uint32_t flattened_data = android::util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800746
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700748 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
749
750 // Only one type must match between the actual and expected.
751 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700753 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800754 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700756 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700758 // Enums and flags are encoded as integers, so check them first before doing any range checks.
759 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
760 for (const Symbol& s : symbols) {
761 if (flattened_data == s.value) {
762 return true;
763 }
764 }
765
766 // If the attribute accepts integers, we can't fail here.
767 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700769 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 }
771 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700772 }
773 }
774
775 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
776 uint32_t mask = 0u;
777 for (const Symbol& s : symbols) {
778 mask |= s.value;
779 }
780
781 // Check if the flattened data is covered by the flag bit mask.
782 // If the attribute accepts integers, we can't fail here.
783 if ((mask & flattened_data) == flattened_data) {
784 return true;
785 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700787 *out_msg << item << " is not a valid flag";
788 }
789 return false;
790 }
791 }
792
793 // Finally check the integer range of the value.
794 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
795 if (static_cast<int32_t>(flattened_data) < min_int) {
796 if (out_msg) {
797 *out_msg << item << " is less than minimum integer " << min_int;
798 }
799 return false;
800 } else if (static_cast<int32_t>(flattened_data) > max_int) {
801 if (out_msg) {
802 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 }
804 return false;
805 }
806 }
807 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800808}
809
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700810std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
811 if (entry.key.name) {
812 out << entry.key.name.value();
813 } else if (entry.key.id) {
814 out << entry.key.id.value();
815 } else {
816 out << "???";
817 }
818 out << " = " << entry.value;
819 return out;
820}
821
822template <typename T>
823std::vector<T*> ToPointerVec(std::vector<T>& src) {
824 std::vector<T*> dst;
825 dst.reserve(src.size());
826 for (T& in : src) {
827 dst.push_back(&in);
828 }
829 return dst;
830}
831
832template <typename T>
833std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
834 std::vector<const T*> dst;
835 dst.reserve(src.size());
836 for (const T& in : src) {
837 dst.push_back(&in);
838 }
839 return dst;
840}
841
842static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
843 return a->key.name < b->key.name;
844}
845
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846bool Style::Equals(const Value* value) const {
847 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 if (!other) {
849 return false;
850 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700851
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700853 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 return false;
855 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700856
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 if (entries.size() != other->entries.size()) {
858 return false;
859 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700860
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700861 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
862 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700863
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700864 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
865 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700866
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700867 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700869 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700871}
872
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 *out << "(style) ";
875 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700876 const Reference& parent_ref = parent.value();
877 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700880 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700882 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800883}
884
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000885Style::Entry CloneEntry(const Style::Entry& entry, android::StringPool* pool) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700886 Style::Entry cloned_entry{entry.key};
887 if (entry.value != nullptr) {
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700888 CloningValueTransformer cloner(pool);
889 cloned_entry.value = entry.value->Transform(cloner);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700891 return cloned_entry;
892}
893
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000894void Style::MergeWith(Style* other, android::StringPool* pool) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700895 if (other->parent) {
896 parent = other->parent;
897 }
898
899 // We can't assume that the entries are sorted alphabetically since they're supposed to be
900 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
901 // them keying off that.
902 //
903 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
904 // those.
905
906 std::vector<Entry*> this_sorted = ToPointerVec(entries);
907 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
908
909 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
910 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
911
912 auto this_iter = this_sorted.begin();
913 const auto this_end = this_sorted.end();
914
915 auto other_iter = other_sorted.begin();
916 const auto other_end = other_sorted.end();
917
918 std::vector<Entry> merged_entries;
919 while (this_iter != this_end) {
920 if (other_iter != other_end) {
921 if ((*this_iter)->key.name < (*other_iter)->key.name) {
922 merged_entries.push_back(std::move(**this_iter));
923 ++this_iter;
924 } else {
925 // The other overrides.
926 merged_entries.push_back(CloneEntry(**other_iter, pool));
927 if ((*this_iter)->key.name == (*other_iter)->key.name) {
928 ++this_iter;
929 }
930 ++other_iter;
931 }
932 } else {
933 merged_entries.push_back(std::move(**this_iter));
934 ++this_iter;
935 }
936 }
937
938 while (other_iter != other_end) {
939 merged_entries.push_back(CloneEntry(**other_iter, pool));
940 ++other_iter;
941 }
942
943 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800944}
945
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946bool Array::Equals(const Value* value) const {
947 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 if (!other) {
949 return false;
950 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700951
Adam Lesinski4ffea042017-08-10 15:37:28 -0700952 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 return false;
954 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700955
Adam Lesinski4ffea042017-08-10 15:37:28 -0700956 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
957 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700960}
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700963 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800964}
965
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966bool Plural::Equals(const Value* value) const {
967 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 if (!other) {
969 return false;
970 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700971
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800972 auto one_iter = values.begin();
973 auto one_end_iter = values.end();
974 auto two_iter = other->values.begin();
975 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
976 const std::unique_ptr<Item>& a = *one_iter;
977 const std::unique_ptr<Item>& b = *two_iter;
978 if (a != nullptr && b != nullptr) {
979 if (!a->Equals(b.get())) {
980 return false;
981 }
982 } else if (a != b) {
983 return false;
984 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800986 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700987}
988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 *out << "(plural)";
991 if (values[Zero]) {
992 *out << " zero=" << *values[Zero];
993 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700994
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 if (values[One]) {
996 *out << " one=" << *values[One];
997 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700998
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 if (values[Two]) {
1000 *out << " two=" << *values[Two];
1001 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001002
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 if (values[Few]) {
1004 *out << " few=" << *values[Few];
1005 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001006
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 if (values[Many]) {
1008 *out << " many=" << *values[Many];
1009 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001010
1011 if (values[Other]) {
1012 *out << " other=" << *values[Other];
1013 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001014}
1015
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016bool Styleable::Equals(const Value* value) const {
1017 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 if (!other) {
1019 return false;
1020 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001021
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 if (entries.size() != other->entries.size()) {
1023 return false;
1024 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001025
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1027 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001030}
1031
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001035}
1036
Ryan Mitchell326e35ff2021-04-12 07:50:42 -07001037bool Macro::Equals(const Value* value) const {
1038 const Macro* other = ValueCast<Macro>(value);
1039 if (!other) {
1040 return false;
1041 }
1042 return other->raw_value == raw_value && other->style_string.spans == style_string.spans &&
1043 other->style_string.str == style_string.str &&
1044 other->untranslatable_sections == untranslatable_sections &&
1045 other->alias_namespaces == alias_namespaces;
1046}
1047
1048void Macro::Print(std::ostream* out) const {
1049 *out << "(macro) ";
1050}
1051
Adam Lesinski8197cc462016-08-19 12:16:49 -07001052bool operator<(const Reference& a, const Reference& b) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001053 int cmp = a.name.value_or(ResourceName{}).compare(b.name.value_or(ResourceName{}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 if (cmp != 0) return cmp < 0;
1055 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001056}
1057
1058bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001060}
1061
1062bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001064}
1065
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001066struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001067 bool operator()(const Reference& a, const Reference& b) const {
1068 return a.name < b.name;
1069 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001070};
1071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001074 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 std::set<Reference, NameOnlyComparator> references;
1076 references.insert(entries.begin(), entries.end());
1077 references.insert(other->entries.begin(), other->entries.end());
1078 entries.clear();
1079 entries.reserve(references.size());
1080 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001081}
1082
Ryan Mitchellefcdb952021-04-14 17:31:37 -07001083template <typename T>
1084std::unique_ptr<T> CopyValueFields(std::unique_ptr<T> new_value, const T* value) {
1085 new_value->SetSource(value->GetSource());
1086 new_value->SetComment(value->GetComment());
1087 return new_value;
1088}
1089
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001090CloningValueTransformer::CloningValueTransformer(android::StringPool* new_pool)
Ryan Mitchellefcdb952021-04-14 17:31:37 -07001091 : ValueTransformer(new_pool) {
1092}
1093
1094std::unique_ptr<Reference> CloningValueTransformer::TransformDerived(const Reference* value) {
1095 return std::make_unique<Reference>(*value);
1096}
1097
1098std::unique_ptr<Id> CloningValueTransformer::TransformDerived(const Id* value) {
1099 return std::make_unique<Id>(*value);
1100}
1101
1102std::unique_ptr<RawString> CloningValueTransformer::TransformDerived(const RawString* value) {
1103 auto new_value = std::make_unique<RawString>(pool_->MakeRef(value->value));
1104 return CopyValueFields(std::move(new_value), value);
1105}
1106
1107std::unique_ptr<String> CloningValueTransformer::TransformDerived(const String* value) {
1108 auto new_value = std::make_unique<String>(pool_->MakeRef(value->value));
1109 new_value->untranslatable_sections = value->untranslatable_sections;
1110 return CopyValueFields(std::move(new_value), value);
1111}
1112
1113std::unique_ptr<StyledString> CloningValueTransformer::TransformDerived(const StyledString* value) {
1114 auto new_value = std::make_unique<StyledString>(pool_->MakeRef(value->value));
1115 new_value->untranslatable_sections = value->untranslatable_sections;
1116 return CopyValueFields(std::move(new_value), value);
1117}
1118
1119std::unique_ptr<FileReference> CloningValueTransformer::TransformDerived(
1120 const FileReference* value) {
1121 auto new_value = std::make_unique<FileReference>(pool_->MakeRef(value->path));
1122 new_value->file = value->file;
1123 new_value->type = value->type;
1124 return CopyValueFields(std::move(new_value), value);
1125}
1126
1127std::unique_ptr<BinaryPrimitive> CloningValueTransformer::TransformDerived(
1128 const BinaryPrimitive* value) {
1129 return std::make_unique<BinaryPrimitive>(*value);
1130}
1131
1132std::unique_ptr<Attribute> CloningValueTransformer::TransformDerived(const Attribute* value) {
1133 auto new_value = std::make_unique<Attribute>();
1134 new_value->type_mask = value->type_mask;
1135 new_value->min_int = value->min_int;
1136 new_value->max_int = value->max_int;
1137 for (const Attribute::Symbol& s : value->symbols) {
1138 new_value->symbols.emplace_back(Attribute::Symbol{
1139 .symbol = *s.symbol.Transform(*this),
1140 .value = s.value,
1141 .type = s.type,
1142 });
1143 }
1144 return CopyValueFields(std::move(new_value), value);
1145}
1146
1147std::unique_ptr<Style> CloningValueTransformer::TransformDerived(const Style* value) {
1148 auto new_value = std::make_unique<Style>();
1149 new_value->parent = value->parent;
1150 new_value->parent_inferred = value->parent_inferred;
1151 for (auto& entry : value->entries) {
1152 new_value->entries.push_back(Style::Entry{entry.key, entry.value->Transform(*this)});
1153 }
1154 return CopyValueFields(std::move(new_value), value);
1155}
1156
1157std::unique_ptr<Array> CloningValueTransformer::TransformDerived(const Array* value) {
1158 auto new_value = std::make_unique<Array>();
1159 for (auto& item : value->elements) {
1160 new_value->elements.emplace_back(item->Transform(*this));
1161 }
1162 return CopyValueFields(std::move(new_value), value);
1163}
1164
1165std::unique_ptr<Plural> CloningValueTransformer::TransformDerived(const Plural* value) {
1166 auto new_value = std::make_unique<Plural>();
1167 const size_t count = value->values.size();
1168 for (size_t i = 0; i < count; i++) {
1169 if (value->values[i]) {
1170 new_value->values[i] = value->values[i]->Transform(*this);
1171 }
1172 }
1173 return CopyValueFields(std::move(new_value), value);
1174}
1175
1176std::unique_ptr<Styleable> CloningValueTransformer::TransformDerived(const Styleable* value) {
1177 auto new_value = std::make_unique<Styleable>();
1178 for (const Reference& s : value->entries) {
1179 new_value->entries.emplace_back(*s.Transform(*this));
1180 }
1181 return CopyValueFields(std::move(new_value), value);
1182}
1183
Ryan Mitchell326e35ff2021-04-12 07:50:42 -07001184std::unique_ptr<Macro> CloningValueTransformer::TransformDerived(const Macro* value) {
1185 auto new_value = std::make_unique<Macro>(*value);
1186 return CopyValueFields(std::move(new_value), value);
1187}
1188
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189} // namespace aapt