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 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 17 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "ResourceUtils.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 19 | #include "SdkConstants.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 20 | #include "flatten/ResourceTypeExtensions.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 21 | #include "util/Files.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "util/Util.h" |
| 23 | |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | #include <sstream> |
| 26 | |
| 27 | namespace aapt { |
| 28 | namespace ResourceUtils { |
| 29 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 30 | Maybe<ResourceName> toResourceName(const android::ResTable::resource_name& nameIn) { |
| 31 | ResourceName nameOut; |
| 32 | if (!nameIn.package) { |
| 33 | return {}; |
| 34 | } |
| 35 | |
| 36 | nameOut.package = util::utf16ToUtf8(StringPiece16(nameIn.package, nameIn.packageLen)); |
| 37 | |
| 38 | const ResourceType* type; |
| 39 | if (nameIn.type) { |
| 40 | type = parseResourceType(util::utf16ToUtf8(StringPiece16(nameIn.type, nameIn.typeLen))); |
| 41 | } else if (nameIn.type8) { |
| 42 | type = parseResourceType(StringPiece(nameIn.type8, nameIn.typeLen)); |
| 43 | } else { |
| 44 | return {}; |
| 45 | } |
| 46 | |
| 47 | if (!type) { |
| 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | nameOut.type = *type; |
| 52 | |
| 53 | if (nameIn.name) { |
| 54 | nameOut.entry = util::utf16ToUtf8(StringPiece16(nameIn.name, nameIn.nameLen)); |
| 55 | } else if (nameIn.name8) { |
| 56 | nameOut.entry = StringPiece(nameIn.name8, nameIn.nameLen).toString(); |
| 57 | } else { |
| 58 | return {}; |
| 59 | } |
| 60 | return nameOut; |
| 61 | } |
| 62 | |
| 63 | bool extractResourceName(const StringPiece& str, StringPiece* outPackage, |
| 64 | StringPiece* outType, StringPiece* outEntry) { |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 65 | bool hasPackageSeparator = false; |
| 66 | bool hasTypeSeparator = false; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 67 | const char* start = str.data(); |
| 68 | const char* end = start + str.size(); |
| 69 | const char* current = start; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | while (current != end) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 71 | if (outType->size() == 0 && *current == '/') { |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 72 | hasTypeSeparator = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | outType->assign(start, current - start); |
| 74 | start = current + 1; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 75 | } else if (outPackage->size() == 0 && *current == ':') { |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 76 | hasPackageSeparator = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | outPackage->assign(start, current - start); |
| 78 | start = current + 1; |
| 79 | } |
| 80 | current++; |
| 81 | } |
| 82 | outEntry->assign(start, end - start); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 83 | |
| 84 | return !(hasPackageSeparator && outPackage->empty()) && !(hasTypeSeparator && outType->empty()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 87 | bool parseResourceName(const StringPiece& str, ResourceNameRef* outRef, bool* outPrivate) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 88 | if (str.empty()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 92 | size_t offset = 0; |
| 93 | bool priv = false; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 94 | if (str.data()[0] == '*') { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 95 | priv = true; |
| 96 | offset = 1; |
| 97 | } |
| 98 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 99 | StringPiece package; |
| 100 | StringPiece type; |
| 101 | StringPiece entry; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 102 | if (!extractResourceName(str.substr(offset, str.size() - offset), &package, &type, &entry)) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | const ResourceType* parsedType = parseResourceType(type); |
| 107 | if (!parsedType) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (entry.empty()) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (outRef) { |
| 116 | outRef->package = package; |
| 117 | outRef->type = *parsedType; |
| 118 | outRef->entry = entry; |
| 119 | } |
| 120 | |
| 121 | if (outPrivate) { |
| 122 | *outPrivate = priv; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 127 | bool parseReference(const StringPiece& str, ResourceNameRef* outRef, bool* outCreate, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | bool* outPrivate) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 129 | StringPiece trimmedStr(util::trimWhitespace(str)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | if (trimmedStr.empty()) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool create = false; |
| 135 | bool priv = false; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 136 | if (trimmedStr.data()[0] == '@') { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | size_t offset = 1; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 138 | if (trimmedStr.data()[1] == '+') { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | create = true; |
| 140 | offset += 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 141 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 142 | |
| 143 | ResourceNameRef name; |
| 144 | if (!parseResourceName(trimmedStr.substr(offset, trimmedStr.size() - offset), |
| 145 | &name, &priv)) { |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 149 | if (create && priv) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 153 | if (create && name.type != ResourceType::kId) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 157 | if (outRef) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 158 | *outRef = name; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 159 | } |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 160 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 161 | if (outCreate) { |
| 162 | *outCreate = create; |
| 163 | } |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | if (outPrivate) { |
| 166 | *outPrivate = priv; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 173 | bool isReference(const StringPiece& str) { |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 174 | return parseReference(str, nullptr, nullptr, nullptr); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 177 | bool parseAttributeReference(const StringPiece& str, ResourceNameRef* outRef) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 178 | StringPiece trimmedStr(util::trimWhitespace(str)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 179 | if (trimmedStr.empty()) { |
| 180 | return false; |
| 181 | } |
| 182 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 183 | if (*trimmedStr.data() == '?') { |
| 184 | StringPiece package; |
| 185 | StringPiece type; |
| 186 | StringPiece entry; |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 187 | if (!extractResourceName(trimmedStr.substr(1, trimmedStr.size() - 1), |
| 188 | &package, &type, &entry)) { |
| 189 | return false; |
| 190 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 191 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 192 | if (!type.empty() && type != "attr") { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 196 | if (entry.empty()) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if (outRef) { |
| 201 | outRef->package = package; |
| 202 | outRef->type = ResourceType::kAttr; |
| 203 | outRef->entry = entry; |
| 204 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 210 | bool isAttributeReference(const StringPiece& str) { |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 211 | return parseAttributeReference(str, nullptr); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | /* |
| 215 | * Style parent's are a bit different. We accept the following formats: |
| 216 | * |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 217 | * @[[*]package:][style/]<entry> |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 218 | * ?[[*]package:]style/<entry> |
| 219 | * <[*]package>:[style/]<entry> |
| 220 | * [[*]package:style/]<entry> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 221 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 222 | Maybe<Reference> parseStyleParentReference(const StringPiece& str, std::string* outError) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 223 | if (str.empty()) { |
| 224 | return {}; |
| 225 | } |
| 226 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 227 | StringPiece name = str; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 228 | |
| 229 | bool hasLeadingIdentifiers = false; |
| 230 | bool privateRef = false; |
| 231 | |
| 232 | // Skip over these identifiers. A style's parent is a normal reference. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 233 | if (name.data()[0] == '@' || name.data()[0] == '?') { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 234 | hasLeadingIdentifiers = true; |
| 235 | name = name.substr(1, name.size() - 1); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 238 | if (name.data()[0] == '*') { |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 239 | privateRef = true; |
| 240 | name = name.substr(1, name.size() - 1); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ResourceNameRef ref; |
| 244 | ref.type = ResourceType::kStyle; |
| 245 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 246 | StringPiece typeStr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 247 | extractResourceName(name, &ref.package, &typeStr, &ref.entry); |
| 248 | if (!typeStr.empty()) { |
| 249 | // If we have a type, make sure it is a Style. |
| 250 | const ResourceType* parsedType = parseResourceType(typeStr); |
| 251 | if (!parsedType || *parsedType != ResourceType::kStyle) { |
| 252 | std::stringstream err; |
| 253 | err << "invalid resource type '" << typeStr << "' for parent of style"; |
| 254 | *outError = err.str(); |
| 255 | return {}; |
| 256 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (!hasLeadingIdentifiers && ref.package.empty() && !typeStr.empty()) { |
| 260 | std::stringstream err; |
| 261 | err << "invalid parent reference '" << str << "'"; |
| 262 | *outError = err.str(); |
| 263 | return {}; |
| 264 | } |
| 265 | |
| 266 | Reference result(ref); |
| 267 | result.privateReference = privateRef; |
| 268 | return result; |
| 269 | } |
| 270 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 271 | std::unique_ptr<Reference> tryParseReference(const StringPiece& str, bool* outCreate) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 272 | ResourceNameRef ref; |
| 273 | bool privateRef = false; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 274 | if (parseReference(str, &ref, outCreate, &privateRef)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 275 | std::unique_ptr<Reference> value = util::make_unique<Reference>(ref); |
| 276 | value->privateReference = privateRef; |
| 277 | return value; |
| 278 | } |
| 279 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 280 | if (parseAttributeReference(str, &ref)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 281 | if (outCreate) { |
| 282 | *outCreate = false; |
| 283 | } |
| 284 | return util::make_unique<Reference>(ref, Reference::Type::kAttribute); |
| 285 | } |
| 286 | return {}; |
| 287 | } |
| 288 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 289 | std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str) { |
| 290 | StringPiece trimmedStr(util::trimWhitespace(str)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 291 | android::Res_value value = { }; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 292 | if (trimmedStr == "@null") { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 293 | // TYPE_NULL with data set to 0 is interpreted by the runtime as an error. |
| 294 | // Instead we set the data type to TYPE_REFERENCE with a value of 0. |
| 295 | value.dataType = android::Res_value::TYPE_REFERENCE; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 296 | } else if (trimmedStr == "@empty") { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime. |
| 298 | value.dataType = android::Res_value::TYPE_NULL; |
| 299 | value.data = android::Res_value::DATA_NULL_EMPTY; |
| 300 | } else { |
| 301 | return {}; |
| 302 | } |
| 303 | return util::make_unique<BinaryPrimitive>(value); |
| 304 | } |
| 305 | |
| 306 | std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 307 | const StringPiece& str) { |
| 308 | StringPiece trimmedStr(util::trimWhitespace(str)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 309 | for (const Attribute::Symbol& symbol : enumAttr->symbols) { |
| 310 | // Enum symbols are stored as @package:id/symbol resources, |
| 311 | // so we need to match against the 'entry' part of the identifier. |
| 312 | const ResourceName& enumSymbolResourceName = symbol.symbol.name.value(); |
| 313 | if (trimmedStr == enumSymbolResourceName.entry) { |
| 314 | android::Res_value value = { }; |
| 315 | value.dataType = android::Res_value::TYPE_INT_DEC; |
| 316 | value.data = symbol.value; |
| 317 | return util::make_unique<BinaryPrimitive>(value); |
| 318 | } |
| 319 | } |
| 320 | return {}; |
| 321 | } |
| 322 | |
| 323 | std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* flagAttr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 324 | const StringPiece& str) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 325 | android::Res_value flags = { }; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 326 | flags.dataType = android::Res_value::TYPE_INT_HEX; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 327 | flags.data = 0u; |
| 328 | |
| 329 | if (util::trimWhitespace(str).empty()) { |
| 330 | // Empty string is a valid flag (0). |
| 331 | return util::make_unique<BinaryPrimitive>(flags); |
| 332 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 333 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 334 | for (StringPiece part : util::tokenize(str, '|')) { |
| 335 | StringPiece trimmedPart = util::trimWhitespace(part); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 336 | |
| 337 | bool flagSet = false; |
| 338 | for (const Attribute::Symbol& symbol : flagAttr->symbols) { |
| 339 | // Flag symbols are stored as @package:id/symbol resources, |
| 340 | // so we need to match against the 'entry' part of the identifier. |
| 341 | const ResourceName& flagSymbolResourceName = symbol.symbol.name.value(); |
| 342 | if (trimmedPart == flagSymbolResourceName.entry) { |
| 343 | flags.data |= symbol.value; |
| 344 | flagSet = true; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (!flagSet) { |
| 350 | return {}; |
| 351 | } |
| 352 | } |
| 353 | return util::make_unique<BinaryPrimitive>(flags); |
| 354 | } |
| 355 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 356 | static uint32_t parseHex(char c, bool* outError) { |
| 357 | if (c >= '0' && c <= '9') { |
| 358 | return c - '0'; |
| 359 | } else if (c >= 'a' && c <= 'f') { |
| 360 | return c - 'a' + 0xa; |
| 361 | } else if (c >= 'A' && c <= 'F') { |
| 362 | return c - 'A' + 0xa; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 363 | } else { |
| 364 | *outError = true; |
| 365 | return 0xffffffffu; |
| 366 | } |
| 367 | } |
| 368 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 369 | std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str) { |
| 370 | StringPiece colorStr(util::trimWhitespace(str)); |
| 371 | const char* start = colorStr.data(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 372 | const size_t len = colorStr.size(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 373 | if (len == 0 || start[0] != '#') { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 374 | return {}; |
| 375 | } |
| 376 | |
| 377 | android::Res_value value = { }; |
| 378 | bool error = false; |
| 379 | if (len == 4) { |
| 380 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4; |
| 381 | value.data = 0xff000000u; |
| 382 | value.data |= parseHex(start[1], &error) << 20; |
| 383 | value.data |= parseHex(start[1], &error) << 16; |
| 384 | value.data |= parseHex(start[2], &error) << 12; |
| 385 | value.data |= parseHex(start[2], &error) << 8; |
| 386 | value.data |= parseHex(start[3], &error) << 4; |
| 387 | value.data |= parseHex(start[3], &error); |
| 388 | } else if (len == 5) { |
| 389 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4; |
| 390 | value.data |= parseHex(start[1], &error) << 28; |
| 391 | value.data |= parseHex(start[1], &error) << 24; |
| 392 | value.data |= parseHex(start[2], &error) << 20; |
| 393 | value.data |= parseHex(start[2], &error) << 16; |
| 394 | value.data |= parseHex(start[3], &error) << 12; |
| 395 | value.data |= parseHex(start[3], &error) << 8; |
| 396 | value.data |= parseHex(start[4], &error) << 4; |
| 397 | value.data |= parseHex(start[4], &error); |
| 398 | } else if (len == 7) { |
| 399 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8; |
| 400 | value.data = 0xff000000u; |
| 401 | value.data |= parseHex(start[1], &error) << 20; |
| 402 | value.data |= parseHex(start[2], &error) << 16; |
| 403 | value.data |= parseHex(start[3], &error) << 12; |
| 404 | value.data |= parseHex(start[4], &error) << 8; |
| 405 | value.data |= parseHex(start[5], &error) << 4; |
| 406 | value.data |= parseHex(start[6], &error); |
| 407 | } else if (len == 9) { |
| 408 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8; |
| 409 | value.data |= parseHex(start[1], &error) << 28; |
| 410 | value.data |= parseHex(start[2], &error) << 24; |
| 411 | value.data |= parseHex(start[3], &error) << 20; |
| 412 | value.data |= parseHex(start[4], &error) << 16; |
| 413 | value.data |= parseHex(start[5], &error) << 12; |
| 414 | value.data |= parseHex(start[6], &error) << 8; |
| 415 | value.data |= parseHex(start[7], &error) << 4; |
| 416 | value.data |= parseHex(start[8], &error); |
| 417 | } else { |
| 418 | return {}; |
| 419 | } |
| 420 | return error ? std::unique_ptr<BinaryPrimitive>() : util::make_unique<BinaryPrimitive>(value); |
| 421 | } |
| 422 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 423 | Maybe<bool> parseBool(const StringPiece& str) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 424 | StringPiece trimmedStr(util::trimWhitespace(str)); |
| 425 | if (trimmedStr == "true" || trimmedStr == "TRUE" || trimmedStr == "True") { |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 426 | return Maybe<bool>(true); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 427 | } else if (trimmedStr == "false" || trimmedStr == "FALSE" || trimmedStr == "False") { |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 428 | return Maybe<bool>(false); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 429 | } |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 430 | return {}; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 433 | Maybe<uint32_t> parseInt(const StringPiece& str) { |
| 434 | std::u16string str16 = util::utf8ToUtf16(str); |
| 435 | android::Res_value value; |
| 436 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 437 | return value.data; |
| 438 | } |
| 439 | return {}; |
| 440 | } |
| 441 | |
| 442 | Maybe<ResourceId> parseResourceId(const StringPiece& str) { |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 443 | StringPiece trimmedStr(util::trimWhitespace(str)); |
| 444 | |
| 445 | std::u16string str16 = util::utf8ToUtf16(trimmedStr); |
| 446 | android::Res_value value; |
| 447 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 448 | if (value.dataType == android::Res_value::TYPE_INT_HEX) { |
| 449 | ResourceId id(value.data); |
| 450 | if (id.isValid()) { |
| 451 | return id; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return {}; |
| 456 | } |
| 457 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 458 | Maybe<int> parseSdkVersion(const StringPiece& str) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 459 | StringPiece trimmedStr(util::trimWhitespace(str)); |
| 460 | |
| 461 | std::u16string str16 = util::utf8ToUtf16(trimmedStr); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 462 | android::Res_value value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 463 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 464 | return static_cast<int>(value.data); |
| 465 | } |
| 466 | |
| 467 | // Try parsing the code name. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 468 | std::pair<StringPiece, int> entry = getDevelopmentSdkCodeNameAndVersion(); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 469 | if (entry.first == trimmedStr) { |
| 470 | return entry.second; |
| 471 | } |
| 472 | return {}; |
| 473 | } |
| 474 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 475 | std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str) { |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 476 | if (Maybe<bool> maybeResult = parseBool(str)) { |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 477 | android::Res_value value = {}; |
| 478 | value.dataType = android::Res_value::TYPE_INT_BOOLEAN; |
| 479 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 480 | if (maybeResult.value()) { |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 481 | value.data = 0xffffffffu; |
| 482 | } else { |
| 483 | value.data = 0; |
| 484 | } |
| 485 | return util::make_unique<BinaryPrimitive>(value); |
| 486 | } |
| 487 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 490 | std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str) { |
| 491 | std::u16string str16 = util::utf8ToUtf16(str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 492 | android::Res_value value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 493 | if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 494 | return {}; |
| 495 | } |
| 496 | return util::make_unique<BinaryPrimitive>(value); |
| 497 | } |
| 498 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 499 | std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str) { |
| 500 | std::u16string str16 = util::utf8ToUtf16(str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 501 | android::Res_value value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 502 | if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 503 | return {}; |
| 504 | } |
| 505 | return util::make_unique<BinaryPrimitive>(value); |
| 506 | } |
| 507 | |
| 508 | uint32_t androidTypeToAttributeTypeMask(uint16_t type) { |
| 509 | switch (type) { |
| 510 | case android::Res_value::TYPE_NULL: |
| 511 | case android::Res_value::TYPE_REFERENCE: |
| 512 | case android::Res_value::TYPE_ATTRIBUTE: |
| 513 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
| 514 | return android::ResTable_map::TYPE_REFERENCE; |
| 515 | |
| 516 | case android::Res_value::TYPE_STRING: |
| 517 | return android::ResTable_map::TYPE_STRING; |
| 518 | |
| 519 | case android::Res_value::TYPE_FLOAT: |
| 520 | return android::ResTable_map::TYPE_FLOAT; |
| 521 | |
| 522 | case android::Res_value::TYPE_DIMENSION: |
| 523 | return android::ResTable_map::TYPE_DIMENSION; |
| 524 | |
| 525 | case android::Res_value::TYPE_FRACTION: |
| 526 | return android::ResTable_map::TYPE_FRACTION; |
| 527 | |
| 528 | case android::Res_value::TYPE_INT_DEC: |
| 529 | case android::Res_value::TYPE_INT_HEX: |
| 530 | return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM |
| 531 | | android::ResTable_map::TYPE_FLAGS; |
| 532 | |
| 533 | case android::Res_value::TYPE_INT_BOOLEAN: |
| 534 | return android::ResTable_map::TYPE_BOOLEAN; |
| 535 | |
| 536 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 537 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 538 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 539 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
| 540 | return android::ResTable_map::TYPE_COLOR; |
| 541 | |
| 542 | default: |
| 543 | return 0; |
| 544 | }; |
| 545 | } |
| 546 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 547 | std::unique_ptr<Item> tryParseItemForAttribute( |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 548 | const StringPiece& value, |
| 549 | uint32_t typeMask, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 550 | std::function<void(const ResourceName&)> onCreateReference) { |
| 551 | std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value); |
| 552 | if (nullOrEmpty) { |
| 553 | return std::move(nullOrEmpty); |
| 554 | } |
| 555 | |
| 556 | bool create = false; |
| 557 | std::unique_ptr<Reference> reference = tryParseReference(value, &create); |
| 558 | if (reference) { |
| 559 | if (create && onCreateReference) { |
| 560 | onCreateReference(reference->name.value()); |
| 561 | } |
| 562 | return std::move(reference); |
| 563 | } |
| 564 | |
| 565 | if (typeMask & android::ResTable_map::TYPE_COLOR) { |
| 566 | // Try parsing this as a color. |
| 567 | std::unique_ptr<BinaryPrimitive> color = tryParseColor(value); |
| 568 | if (color) { |
| 569 | return std::move(color); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (typeMask & android::ResTable_map::TYPE_BOOLEAN) { |
| 574 | // Try parsing this as a boolean. |
| 575 | std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value); |
| 576 | if (boolean) { |
| 577 | return std::move(boolean); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (typeMask & android::ResTable_map::TYPE_INTEGER) { |
| 582 | // Try parsing this as an integer. |
| 583 | std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value); |
| 584 | if (integer) { |
| 585 | return std::move(integer); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT |
| 590 | | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION; |
| 591 | if (typeMask & floatMask) { |
| 592 | // Try parsing this as a float. |
| 593 | std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value); |
| 594 | if (floatingPoint) { |
| 595 | if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) { |
| 596 | return std::move(floatingPoint); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | return {}; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * We successively try to parse the string as a resource type that the Attribute |
| 605 | * allows. |
| 606 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 607 | std::unique_ptr<Item> tryParseItemForAttribute( |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 608 | const StringPiece& str, const Attribute* attr, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 609 | std::function<void(const ResourceName&)> onCreateReference) { |
| 610 | const uint32_t typeMask = attr->typeMask; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 611 | std::unique_ptr<Item> value = tryParseItemForAttribute(str, typeMask, onCreateReference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 612 | if (value) { |
| 613 | return value; |
| 614 | } |
| 615 | |
| 616 | if (typeMask & android::ResTable_map::TYPE_ENUM) { |
| 617 | // Try parsing this as an enum. |
| 618 | std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str); |
| 619 | if (enumValue) { |
| 620 | return std::move(enumValue); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if (typeMask & android::ResTable_map::TYPE_FLAGS) { |
| 625 | // Try parsing this as a flag. |
| 626 | std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str); |
| 627 | if (flagValue) { |
| 628 | return std::move(flagValue); |
| 629 | } |
| 630 | } |
| 631 | return {}; |
| 632 | } |
| 633 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 634 | std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) { |
| 635 | std::stringstream out; |
| 636 | out << "res/" << resFile.name.type; |
| 637 | if (resFile.config != ConfigDescription{}) { |
| 638 | out << "-" << resFile.config; |
| 639 | } |
| 640 | out << "/"; |
| 641 | |
| 642 | if (mangler && mangler->shouldMangle(resFile.name.package)) { |
| 643 | out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry); |
| 644 | } else { |
| 645 | out << resFile.name.entry; |
| 646 | } |
| 647 | out << file::getExtension(resFile.source.path); |
| 648 | return out.str(); |
| 649 | } |
| 650 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 651 | } // namespace ResourceUtils |
| 652 | } // namespace aapt |