blob: 4d915d9f23c0080d1790a8a60c867765f96dcff0 [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
17#ifndef AAPT_RESOURCE_H
18#define AAPT_RESOURCE_H
19
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <iomanip>
Adam Lesinskica2fc352015-04-03 12:08:26 -070021#include <limits>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070022#include <sstream>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <string>
24#include <tuple>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include <vector>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "utils/JenkinsHash.h"
29
30#include "ConfigDescription.h"
31#include "Source.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
35/**
36 * The various types of resource types available. Corresponds
37 * to the 'type' in package:type/entry.
38 */
39enum class ResourceType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 kAnim,
41 kAnimator,
42 kArray,
43 kAttr,
44 kAttrPrivate,
45 kBool,
46 kColor,
Adam Lesinski86d67df2017-01-31 13:47:27 -080047
48 // Not really a type, but it shows up in some CTS tests and
49 // we need to continue respecting it.
50 kConfigVarying,
51
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 kDimen,
53 kDrawable,
Adam Lesinskic0c36632016-10-19 18:37:53 -070054 kFont,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 kFraction,
56 kId,
57 kInteger,
58 kInterpolator,
59 kLayout,
60 kMenu,
61 kMipmap,
62 kPlurals,
63 kRaw,
64 kString,
65 kStyle,
66 kStyleable,
67 kTransition,
68 kXml,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069};
70
Adam Lesinskid5083f62017-01-16 15:07:21 -080071android::StringPiece ToString(ResourceType type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
73/**
74 * Returns a pointer to a valid ResourceType, or nullptr if
75 * the string was invalid.
76 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080077const ResourceType* ParseResourceType(const android::StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
79/**
80 * A resource's name. This can uniquely identify
81 * a resource in the ResourceTable.
82 */
83struct ResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 std::string package;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 ResourceType type = ResourceType::kRaw;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 std::string entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ResourceName() = default;
Adam Lesinskid5083f62017-01-16 15:07:21 -080089 ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 bool is_valid() const;
94 std::string ToString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095};
96
97/**
98 * Same as ResourceName, but uses StringPieces instead.
99 * Use this if you need to avoid copying and know that
100 * the lifetime of this object is shorter than that
101 * of the original string.
102 */
103struct ResourceNameRef {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800104 android::StringPiece package;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 ResourceType type = ResourceType::kRaw;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 android::StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 ResourceNameRef() = default;
109 ResourceNameRef(const ResourceNameRef&) = default;
110 ResourceNameRef(ResourceNameRef&&) = default;
111 ResourceNameRef(const ResourceName& rhs); // NOLINT(implicit)
Adam Lesinskid5083f62017-01-16 15:07:21 -0800112 ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
114 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
115 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 ResourceName ToResourceName() const;
118 bool is_valid() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119};
120
121/**
122 * A binary identifier representing a resource. Internally it
123 * is a 32bit integer split as follows:
124 *
125 * 0xPPTTEEEE
126 *
127 * PP: 8 bit package identifier. 0x01 is reserved for system
128 * and 0x7f is reserved for the running app.
129 * TT: 8 bit type identifier. 0x00 is invalid.
130 * EEEE: 16 bit entry identifier.
131 */
132struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 ResourceId();
136 ResourceId(const ResourceId& rhs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ResourceId(uint32_t res_id); // NOLINT(implicit)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 bool is_valid() const;
141 uint8_t package_id() const;
142 uint8_t type_id() const;
143 uint16_t entry_id() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144};
145
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 ResourceName name;
148 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149};
150
151struct ResourceFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 // Name
153 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 // Configuration
156 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 // Source
159 Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 std::vector<SourcedResourceName> exported_symbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163};
164
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800165/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 * Useful struct used as a key to represent a unique resource in associative
167 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800168 */
169struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 ResourceName name;
171 ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800172};
173
174bool operator<(const ResourceKey& a, const ResourceKey& b);
175
176/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 * Useful struct used as a key to represent a unique resource in associative
178 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800179 * Holds a reference to the name, so that name better live longer than this key!
180 */
181struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 ResourceNameRef name;
183 ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 ResourceKeyRef() = default;
186 ResourceKeyRef(const ResourceNameRef& n, const ConfigDescription& c)
187 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800188
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 /**
190 * Prevent taking a reference to a temporary. This is bad.
191 */
192 ResourceKeyRef(ResourceName&& n, const ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800193};
194
195bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
196
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197//
198// ResourceId implementation.
199//
200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
208 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210inline bool ResourceId::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212}
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216}
217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220}
221
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224}
225
Adam Lesinski74605cd2016-03-03 15:39:50 -0800226inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228}
229
Adam Lesinski74605cd2016-03-03 15:39:50 -0800230inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700232}
233
Adam Lesinski74605cd2016-03-03 15:39:50 -0800234inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 return lhs.id == rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800236}
237
238inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 return lhs.id != rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800240}
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242inline ::std::ostream& operator<<(::std::ostream& out,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 const ResourceId& res_id) {
244 std::ios_base::fmtflags old_flags = out.flags();
245 char old_fill = out.fill();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 out << "0x" << std::internal << std::setfill('0') << std::setw(8) << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 << res_id.id;
248 out.flags(old_flags);
249 out.fill(old_fill);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251}
252
253//
254// ResourceType implementation.
255//
256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257inline ::std::ostream& operator<<(::std::ostream& out,
258 const ResourceType& val) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 return out << ToString(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260}
261
262//
263// ResourceName implementation.
264//
265
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266inline ResourceName::ResourceName(const android::StringPiece& p, ResourceType t,
267 const android::StringPiece& e)
268 : package(p.to_string()), type(t), entry(e.to_string()) {}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700269
Adam Lesinski8197cc462016-08-19 12:16:49 -0700270inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 int cmp = package.compare(other.package);
272 if (cmp != 0) return cmp;
273 cmp = static_cast<int>(type) - static_cast<int>(other.type);
274 if (cmp != 0) return cmp;
275 cmp = entry.compare(other.entry);
276 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700277}
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinski74605cd2016-03-03 15:39:50 -0800283inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return std::tie(lhs.package, lhs.type, lhs.entry) <
285 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286}
287
Adam Lesinski74605cd2016-03-03 15:39:50 -0800288inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return std::tie(lhs.package, lhs.type, lhs.entry) ==
290 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291}
292
Adam Lesinski74605cd2016-03-03 15:39:50 -0800293inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 return std::tie(lhs.package, lhs.type, lhs.entry) !=
295 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296}
297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298inline ::std::ostream& operator<<(::std::ostream& out,
299 const ResourceName& name) {
300 if (!name.package.empty()) {
301 out << name.package << ":";
302 }
303 return out << name.type << "/" << name.entry;
Adam Lesinski769de982015-04-10 19:43:55 -0700304}
305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306inline std::string ResourceName::ToString() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 std::stringstream stream;
308 stream << *this;
309 return stream.str();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700310}
Adam Lesinski769de982015-04-10 19:43:55 -0700311
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312//
313// ResourceNameRef implementation.
314//
315
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
317 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskid5083f62017-01-16 15:07:21 -0800319inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, ResourceType t,
320 const android::StringPiece& e)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 : package(p), type(t), entry(e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
323inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 package = rhs.package;
325 type = rhs.type;
326 entry = rhs.entry;
327 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800332}
333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336}
337
Adam Lesinski74605cd2016-03-03 15:39:50 -0800338inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 return std::tie(lhs.package, lhs.type, lhs.entry) <
340 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341}
342
Adam Lesinski74605cd2016-03-03 15:39:50 -0800343inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return std::tie(lhs.package, lhs.type, lhs.entry) ==
345 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346}
347
Adam Lesinski74605cd2016-03-03 15:39:50 -0800348inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 return std::tie(lhs.package, lhs.type, lhs.entry) !=
350 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351}
352
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353inline ::std::ostream& operator<<(::std::ostream& out,
354 const ResourceNameRef& name) {
355 if (!name.package.empty()) {
356 out << name.package << ":";
357 }
358 return out << name.type << "/" << name.entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359}
360
Adam Lesinski74605cd2016-03-03 15:39:50 -0800361inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800363}
364
365inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800367}
368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369inline bool operator==(const SourcedResourceName& lhs,
370 const SourcedResourceName& rhs) {
371 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800372}
373
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800375
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700376namespace std {
377
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378template <>
379struct hash<aapt::ResourceName> {
380 size_t operator()(const aapt::ResourceName& name) const {
381 android::hash_t h = 0;
382 h = android::JenkinsHashMix(h, hash<string>()(name.package));
383 h = android::JenkinsHashMix(h, static_cast<uint32_t>(name.type));
384 h = android::JenkinsHashMix(h, hash<string>()(name.entry));
385 return static_cast<size_t>(h);
386 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700387};
388
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700390
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391#endif // AAPT_RESOURCE_H