Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 22 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 24 | #include "NameMangler.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 25 | #include "SdkConstants.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 26 | #include "flatten/ResourceTypeExtensions.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 27 | #include "util/Files.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "util/Util.h" |
| 29 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | using android::StringPiece; |
| 31 | using android::StringPiece16; |
| 32 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | namespace aapt { |
| 34 | namespace ResourceUtils { |
| 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | Maybe<ResourceName> ToResourceName( |
| 37 | const android::ResTable::resource_name& name_in) { |
| 38 | ResourceName name_out; |
| 39 | if (!name_in.package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | return {}; |
| 41 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | name_out.package = |
| 44 | util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | const ResourceType* type; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | if (name_in.type) { |
| 48 | type = ParseResourceType( |
| 49 | util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen))); |
| 50 | } else if (name_in.type8) { |
| 51 | type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | } else { |
| 53 | return {}; |
| 54 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | if (!type) { |
| 57 | return {}; |
| 58 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | name_out.type = *type; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | if (name_in.name) { |
| 63 | name_out.entry = |
| 64 | util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen)); |
| 65 | } else if (name_in.name8) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 66 | name_out.entry.assign(name_in.name8, name_in.nameLen); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } else { |
| 68 | return {}; |
| 69 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | return name_out; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref, |
| 74 | bool* out_private) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | if (str.empty()) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | size_t offset = 0; |
| 80 | bool priv = false; |
| 81 | if (str.data()[0] == '*') { |
| 82 | priv = true; |
| 83 | offset = 1; |
| 84 | } |
| 85 | |
| 86 | StringPiece package; |
| 87 | StringPiece type; |
| 88 | StringPiece entry; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 89 | if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type, |
| 90 | &entry)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | const ResourceType* parsed_type = ParseResourceType(type); |
| 95 | if (!parsed_type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if (entry.empty()) { |
| 100 | return false; |
| 101 | } |
| 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | if (out_ref) { |
| 104 | out_ref->package = package; |
| 105 | out_ref->type = *parsed_type; |
| 106 | out_ref->entry = entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | if (out_private) { |
| 110 | *out_private = priv; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref, |
| 116 | bool* out_create, bool* out_private) { |
| 117 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 118 | if (trimmed_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | bool create = false; |
| 123 | bool priv = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | if (trimmed_str.data()[0] == '@') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | size_t offset = 1; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | if (trimmed_str.data()[1] == '+') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | create = true; |
| 128 | offset += 1; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | ResourceNameRef name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | if (!ParseResourceName( |
| 133 | trimmed_str.substr(offset, trimmed_str.size() - offset), &name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | &priv)) { |
| 135 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | if (create && priv) { |
| 139 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | if (create && name.type != ResourceType::kId) { |
| 143 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | if (out_ref) { |
| 147 | *out_ref = name; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | if (out_create) { |
| 151 | *out_create = create; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | if (out_private) { |
| 155 | *out_private = priv; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 156 | } |
| 157 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | } |
| 159 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | bool IsReference(const StringPiece& str) { |
| 163 | return ParseReference(str, nullptr, nullptr, nullptr); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) { |
| 167 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 168 | if (trimmed_str.empty()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | if (*trimmed_str.data() == '?') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | StringPiece package; |
| 174 | StringPiece type; |
| 175 | StringPiece entry; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 176 | if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package, |
| 177 | &type, &entry)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | if (!type.empty() && type != "attr") { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | if (entry.empty()) { |
| 186 | return false; |
| 187 | } |
| 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | if (out_ref) { |
| 190 | out_ref->package = package; |
| 191 | out_ref->type = ResourceType::kAttr; |
| 192 | out_ref->entry = entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | bool IsAttributeReference(const StringPiece& str) { |
| 200 | return ParseAttributeReference(str, nullptr); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Style parent's are a bit different. We accept the following formats: |
| 205 | * |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 206 | * @[[*]package:][style/]<entry> |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 207 | * ?[[*]package:]style/<entry> |
| 208 | * <[*]package>:[style/]<entry> |
| 209 | * [[*]package:style/]<entry> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 210 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | Maybe<Reference> ParseStyleParentReference(const StringPiece& str, |
| 212 | std::string* out_error) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | if (str.empty()) { |
| 214 | return {}; |
| 215 | } |
| 216 | |
| 217 | StringPiece name = str; |
| 218 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | bool has_leading_identifiers = false; |
| 220 | bool private_ref = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 221 | |
| 222 | // Skip over these identifiers. A style's parent is a normal reference. |
| 223 | if (name.data()[0] == '@' || name.data()[0] == '?') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | has_leading_identifiers = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | name = name.substr(1, name.size() - 1); |
| 226 | } |
| 227 | |
| 228 | if (name.data()[0] == '*') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | private_ref = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | name = name.substr(1, name.size() - 1); |
| 231 | } |
| 232 | |
| 233 | ResourceNameRef ref; |
| 234 | ref.type = ResourceType::kStyle; |
| 235 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | StringPiece type_str; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 237 | android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | if (!type_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 239 | // If we have a type, make sure it is a Style. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 241 | if (!parsed_type || *parsed_type != ResourceType::kStyle) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | std::stringstream err; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | err << "invalid resource type '" << type_str << "' for parent of style"; |
| 244 | *out_error = err.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 245 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 246 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 248 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | std::stringstream err; |
| 251 | err << "invalid parent reference '" << str << "'"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | *out_error = err.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | return {}; |
| 254 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 255 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | Reference result(ref); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | result.private_reference = private_ref; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | return result; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) { |
| 262 | StringPiece trimmed_str = util::TrimWhitespace(str); |
| 263 | const char* start = trimmed_str.data(); |
| 264 | const char* const end = start + trimmed_str.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | const char* p = start; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 266 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | Reference ref; |
| 268 | if (p != end && *p == '*') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | ref.private_reference = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | start++; |
| 271 | p++; |
| 272 | } |
| 273 | |
| 274 | StringPiece package; |
| 275 | StringPiece name; |
| 276 | while (p != end) { |
| 277 | if (*p == ':') { |
| 278 | package = StringPiece(start, p - start); |
| 279 | name = StringPiece(p + 1, end - (p + 1)); |
| 280 | break; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 281 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | p++; |
| 283 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 284 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 285 | ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | return Maybe<Reference>(std::move(ref)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 289 | std::unique_ptr<Reference> TryParseReference(const StringPiece& str, |
| 290 | bool* out_create) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | ResourceNameRef ref; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | bool private_ref = false; |
| 293 | if (ParseReference(str, &ref, out_create, &private_ref)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 294 | std::unique_ptr<Reference> value = util::make_unique<Reference>(ref); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 295 | value->private_reference = private_ref; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | return value; |
| 297 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 298 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | if (ParseAttributeReference(str, &ref)) { |
| 300 | if (out_create) { |
| 301 | *out_create = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 302 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | return util::make_unique<Reference>(ref, Reference::Type::kAttribute); |
| 304 | } |
| 305 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 308 | std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) { |
| 309 | const StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | if (trimmed_str == "@null") { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 311 | return MakeNull(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | } else if (trimmed_str == "@empty") { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 313 | return MakeEmpty(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | } |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 315 | return {}; |
| 316 | } |
| 317 | |
| 318 | std::unique_ptr<Reference> MakeNull() { |
| 319 | // TYPE_NULL with data set to 0 is interpreted by the runtime as an error. |
| 320 | // Instead we set the data type to TYPE_REFERENCE with a value of 0. |
| 321 | return util::make_unique<Reference>(); |
| 322 | } |
| 323 | |
| 324 | std::unique_ptr<BinaryPrimitive> MakeEmpty() { |
| 325 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL, |
| 326 | android::Res_value::DATA_NULL_EMPTY); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 330 | const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 332 | for (const Attribute::Symbol& symbol : enum_attr->symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | // Enum symbols are stored as @package:id/symbol resources, |
| 334 | // so we need to match against the 'entry' part of the identifier. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value(); |
| 336 | if (trimmed_str == enum_symbol_resource_name.entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | android::Res_value value = {}; |
| 338 | value.dataType = android::Res_value::TYPE_INT_DEC; |
| 339 | value.data = symbol.value; |
| 340 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 341 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | } |
| 343 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 347 | const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | android::Res_value flags = {}; |
| 349 | flags.dataType = android::Res_value::TYPE_INT_HEX; |
| 350 | flags.data = 0u; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 351 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | if (util::TrimWhitespace(str).empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | // Empty string is a valid flag (0). |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 354 | return util::make_unique<BinaryPrimitive>(flags); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | for (StringPiece part : util::Tokenize(str, '|')) { |
| 358 | StringPiece trimmed_part = util::TrimWhitespace(part); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 359 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 360 | bool flag_set = false; |
| 361 | for (const Attribute::Symbol& symbol : flag_attr->symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | // Flag symbols are stored as @package:id/symbol resources, |
| 363 | // so we need to match against the 'entry' part of the identifier. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | const ResourceName& flag_symbol_resource_name = |
| 365 | symbol.symbol.name.value(); |
| 366 | if (trimmed_part == flag_symbol_resource_name.entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | flags.data |= symbol.value; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | flag_set = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 373 | if (!flag_set) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | return {}; |
| 375 | } |
| 376 | } |
| 377 | return util::make_unique<BinaryPrimitive>(flags); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 380 | static uint32_t ParseHex(char c, bool* out_error) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 381 | if (c >= '0' && c <= '9') { |
| 382 | return c - '0'; |
| 383 | } else if (c >= 'a' && c <= 'f') { |
| 384 | return c - 'a' + 0xa; |
| 385 | } else if (c >= 'A' && c <= 'F') { |
| 386 | return c - 'A' + 0xa; |
| 387 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | *out_error = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | return 0xffffffffu; |
| 390 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) { |
| 394 | StringPiece color_str(util::TrimWhitespace(str)); |
| 395 | const char* start = color_str.data(); |
| 396 | const size_t len = color_str.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | if (len == 0 || start[0] != '#') { |
| 398 | return {}; |
| 399 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 400 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 401 | android::Res_value value = {}; |
| 402 | bool error = false; |
| 403 | if (len == 4) { |
| 404 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4; |
| 405 | value.data = 0xff000000u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | value.data |= ParseHex(start[1], &error) << 20; |
| 407 | value.data |= ParseHex(start[1], &error) << 16; |
| 408 | value.data |= ParseHex(start[2], &error) << 12; |
| 409 | value.data |= ParseHex(start[2], &error) << 8; |
| 410 | value.data |= ParseHex(start[3], &error) << 4; |
| 411 | value.data |= ParseHex(start[3], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | } else if (len == 5) { |
| 413 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 414 | value.data |= ParseHex(start[1], &error) << 28; |
| 415 | value.data |= ParseHex(start[1], &error) << 24; |
| 416 | value.data |= ParseHex(start[2], &error) << 20; |
| 417 | value.data |= ParseHex(start[2], &error) << 16; |
| 418 | value.data |= ParseHex(start[3], &error) << 12; |
| 419 | value.data |= ParseHex(start[3], &error) << 8; |
| 420 | value.data |= ParseHex(start[4], &error) << 4; |
| 421 | value.data |= ParseHex(start[4], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | } else if (len == 7) { |
| 423 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8; |
| 424 | value.data = 0xff000000u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 425 | value.data |= ParseHex(start[1], &error) << 20; |
| 426 | value.data |= ParseHex(start[2], &error) << 16; |
| 427 | value.data |= ParseHex(start[3], &error) << 12; |
| 428 | value.data |= ParseHex(start[4], &error) << 8; |
| 429 | value.data |= ParseHex(start[5], &error) << 4; |
| 430 | value.data |= ParseHex(start[6], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 431 | } else if (len == 9) { |
| 432 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 | value.data |= ParseHex(start[1], &error) << 28; |
| 434 | value.data |= ParseHex(start[2], &error) << 24; |
| 435 | value.data |= ParseHex(start[3], &error) << 20; |
| 436 | value.data |= ParseHex(start[4], &error) << 16; |
| 437 | value.data |= ParseHex(start[5], &error) << 12; |
| 438 | value.data |= ParseHex(start[6], &error) << 8; |
| 439 | value.data |= ParseHex(start[7], &error) << 4; |
| 440 | value.data |= ParseHex(start[8], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 441 | } else { |
| 442 | return {}; |
| 443 | } |
| 444 | return error ? std::unique_ptr<BinaryPrimitive>() |
| 445 | : util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 448 | Maybe<bool> ParseBool(const StringPiece& str) { |
| 449 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 450 | if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | return Maybe<bool>(true); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | } else if (trimmed_str == "false" || trimmed_str == "FALSE" || |
| 453 | trimmed_str == "False") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 454 | return Maybe<bool>(false); |
| 455 | } |
| 456 | return {}; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 459 | Maybe<uint32_t> ParseInt(const StringPiece& str) { |
| 460 | std::u16string str16 = util::Utf8ToUtf16(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 461 | android::Res_value value; |
| 462 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 463 | return value.data; |
| 464 | } |
| 465 | return {}; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 468 | Maybe<ResourceId> ParseResourceId(const StringPiece& str) { |
| 469 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 470 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | android::Res_value value; |
| 473 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 474 | if (value.dataType == android::Res_value::TYPE_INT_HEX) { |
| 475 | ResourceId id(value.data); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 476 | if (id.is_valid_dynamic()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 477 | return id; |
| 478 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 479 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | } |
| 481 | return {}; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 484 | Maybe<int> ParseSdkVersion(const StringPiece& str) { |
| 485 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 486 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 488 | android::Res_value value; |
| 489 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 490 | return static_cast<int>(value.data); |
| 491 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 492 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 493 | // Try parsing the code name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 494 | std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion(); |
| 495 | if (entry.first == trimmed_str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 496 | return entry.second; |
| 497 | } |
| 498 | return {}; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 501 | std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) { |
| 502 | if (Maybe<bool> maybe_result = ParseBool(str)) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 503 | const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u; |
| 504 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | } |
| 506 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 509 | std::unique_ptr<BinaryPrimitive> MakeBool(bool val) { |
| 510 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, |
| 511 | val ? 0xffffffffu : 0u); |
| 512 | } |
| 513 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 514 | std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) { |
| 515 | std::u16string str16 = util::Utf8ToUtf16(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 516 | android::Res_value value; |
| 517 | if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 518 | return {}; |
| 519 | } |
| 520 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) { |
| 524 | std::u16string str16 = util::Utf8ToUtf16(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | android::Res_value value; |
| 526 | if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) { |
| 527 | return {}; |
| 528 | } |
| 529 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 532 | uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 533 | switch (type) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 534 | case android::Res_value::TYPE_NULL: |
| 535 | case android::Res_value::TYPE_REFERENCE: |
| 536 | case android::Res_value::TYPE_ATTRIBUTE: |
| 537 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 538 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 539 | return android::ResTable_map::TYPE_REFERENCE; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 540 | |
| 541 | case android::Res_value::TYPE_STRING: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 542 | return android::ResTable_map::TYPE_STRING; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 543 | |
| 544 | case android::Res_value::TYPE_FLOAT: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 545 | return android::ResTable_map::TYPE_FLOAT; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 546 | |
| 547 | case android::Res_value::TYPE_DIMENSION: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | return android::ResTable_map::TYPE_DIMENSION; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 549 | |
| 550 | case android::Res_value::TYPE_FRACTION: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 551 | return android::ResTable_map::TYPE_FRACTION; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 552 | |
| 553 | case android::Res_value::TYPE_INT_DEC: |
| 554 | case android::Res_value::TYPE_INT_HEX: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 555 | return android::ResTable_map::TYPE_INTEGER | |
| 556 | android::ResTable_map::TYPE_ENUM | |
| 557 | android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 558 | |
| 559 | case android::Res_value::TYPE_INT_BOOLEAN: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 560 | return android::ResTable_map::TYPE_BOOLEAN; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 561 | |
| 562 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 563 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 564 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 565 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 566 | return android::ResTable_map::TYPE_COLOR; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 567 | |
| 568 | default: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 569 | return 0; |
| 570 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 573 | std::unique_ptr<Item> TryParseItemForAttribute( |
| 574 | const StringPiece& value, uint32_t type_mask, |
| 575 | const std::function<void(const ResourceName&)>& on_create_reference) { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 576 | using android::ResTable_map; |
| 577 | |
| 578 | auto null_or_empty = TryParseNullOrEmpty(value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | if (null_or_empty) { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 580 | return null_or_empty; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 581 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 582 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 583 | bool create = false; |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 584 | auto reference = TryParseReference(value, &create); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 585 | if (reference) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 586 | if (create && on_create_reference) { |
| 587 | on_create_reference(reference->name.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 588 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 589 | return std::move(reference); |
| 590 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 591 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 592 | if (type_mask & ResTable_map::TYPE_COLOR) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | // Try parsing this as a color. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 594 | auto color = TryParseColor(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | if (color) { |
| 596 | return std::move(color); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 597 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 598 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 599 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 600 | if (type_mask & ResTable_map::TYPE_BOOLEAN) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 601 | // Try parsing this as a boolean. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 602 | auto boolean = TryParseBool(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 603 | if (boolean) { |
| 604 | return std::move(boolean); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 605 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 606 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 607 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 608 | if (type_mask & ResTable_map::TYPE_INTEGER) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 609 | // Try parsing this as an integer. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 610 | auto integer = TryParseInt(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 611 | if (integer) { |
| 612 | return std::move(integer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 613 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 614 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 615 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 616 | const uint32_t float_mask = |
| 617 | ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 618 | if (type_mask & float_mask) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 619 | // Try parsing this as a float. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 620 | auto floating_point = TryParseFloat(value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 621 | if (floating_point) { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 622 | if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 623 | return std::move(floating_point); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 624 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 625 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 626 | } |
| 627 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * We successively try to parse the string as a resource type that the Attribute |
| 632 | * allows. |
| 633 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 634 | std::unique_ptr<Item> TryParseItemForAttribute( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 635 | const StringPiece& str, const Attribute* attr, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 636 | const std::function<void(const ResourceName&)>& on_create_reference) { |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 637 | using android::ResTable_map; |
| 638 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 639 | const uint32_t type_mask = attr->type_mask; |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 640 | auto value = TryParseItemForAttribute(str, type_mask, on_create_reference); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 641 | if (value) { |
| 642 | return value; |
| 643 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 644 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 645 | if (type_mask & ResTable_map::TYPE_ENUM) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | // Try parsing this as an enum. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 647 | auto enum_value = TryParseEnumSymbol(attr, str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | if (enum_value) { |
| 649 | return std::move(enum_value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 650 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 651 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 652 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 653 | if (type_mask & ResTable_map::TYPE_FLAGS) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 654 | // Try parsing this as a flag. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 655 | auto flag_value = TryParseFlagSymbol(attr, str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 656 | if (flag_value) { |
| 657 | return std::move(flag_value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 658 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 659 | } |
| 660 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 663 | std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 664 | std::stringstream out; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 665 | out << "res/" << res_file.name.type; |
| 666 | if (res_file.config != ConfigDescription{}) { |
| 667 | out << "-" << res_file.config; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 668 | } |
| 669 | out << "/"; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 670 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 671 | if (mangler && mangler->ShouldMangle(res_file.name.package)) { |
| 672 | out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 673 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 674 | out << res_file.name.entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 675 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 676 | out << file::GetExtension(res_file.source.path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 677 | return out.str(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 680 | std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config, |
| 681 | const android::ResStringPool& src_pool, |
| 682 | const android::Res_value& res_value, |
| 683 | StringPool* dst_pool) { |
| 684 | if (type == ResourceType::kId) { |
| 685 | return util::make_unique<Id>(); |
| 686 | } |
| 687 | |
| 688 | const uint32_t data = util::DeviceToHost32(res_value.data); |
| 689 | switch (res_value.dataType) { |
| 690 | case android::Res_value::TYPE_STRING: { |
| 691 | const std::string str = util::GetString(src_pool, data); |
| 692 | const android::ResStringPool_span* spans = src_pool.styleAt(data); |
| 693 | |
| 694 | // Check if the string has a valid style associated with it. |
| 695 | if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) { |
| 696 | StyleString style_str = {str}; |
| 697 | while (spans->name.index != android::ResStringPool_span::END) { |
| 698 | style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index), |
| 699 | spans->firstChar, spans->lastChar}); |
| 700 | spans++; |
| 701 | } |
| 702 | return util::make_unique<StyledString>(dst_pool->MakeRef( |
| 703 | style_str, StringPool::Context(StringPool::Context::kStylePriority, config))); |
| 704 | } else { |
| 705 | if (type != ResourceType::kString && util::StartsWith(str, "res/")) { |
| 706 | // This must be a FileReference. |
| 707 | return util::make_unique<FileReference>(dst_pool->MakeRef( |
| 708 | str, StringPool::Context(StringPool::Context::kHighPriority, config))); |
| 709 | } |
| 710 | |
| 711 | // There are no styles associated with this string, so treat it as a simple string. |
| 712 | return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config))); |
| 713 | } |
| 714 | } break; |
| 715 | |
| 716 | case android::Res_value::TYPE_REFERENCE: |
| 717 | case android::Res_value::TYPE_ATTRIBUTE: |
| 718 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
| 719 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: { |
| 720 | Reference::Type ref_type = Reference::Type::kResource; |
| 721 | if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE || |
| 722 | res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) { |
| 723 | ref_type = Reference::Type::kAttribute; |
| 724 | } |
| 725 | |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 726 | if (data == 0u) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 727 | // A reference of 0, must be the magic @null reference. |
Adam Lesinski | 9091997 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 728 | return util::make_unique<Reference>(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // This is a normal reference. |
| 732 | return util::make_unique<Reference>(data, ref_type); |
| 733 | } break; |
| 734 | } |
| 735 | |
| 736 | // Treat this as a raw binary primitive. |
| 737 | return util::make_unique<BinaryPrimitive>(res_value); |
| 738 | } |
| 739 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 740 | } // namespace ResourceUtils |
| 741 | } // namespace aapt |