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 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 23 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 25 | #include "NameMangler.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 26 | #include "SdkConstants.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 27 | #include "format/binary/ResourceTypeExtensions.h" |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 28 | #include "text/Unicode.h" |
| 29 | #include "text/Utf8Iterator.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 30 | #include "util/Files.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | #include "util/Util.h" |
| 32 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 33 | using ::aapt::text::Utf8Iterator; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 34 | using ::android::ConfigDescription; |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 35 | using ::android::StringPiece; |
| 36 | using ::android::StringPiece16; |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 37 | using ::android::base::StringPrintf; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | namespace aapt { |
| 40 | namespace ResourceUtils { |
| 41 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 42 | constexpr int32_t kNonBreakingSpace = 0xa0; |
| 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | Maybe<ResourceName> ToResourceName( |
| 45 | const android::ResTable::resource_name& name_in) { |
| 46 | ResourceName name_out; |
| 47 | if (!name_in.package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | return {}; |
| 49 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | name_out.package = |
| 52 | util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | const ResourceType* type; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | if (name_in.type) { |
| 56 | type = ParseResourceType( |
| 57 | util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen))); |
| 58 | } else if (name_in.type8) { |
| 59 | type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | } else { |
| 61 | return {}; |
| 62 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | if (!type) { |
| 65 | return {}; |
| 66 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | name_out.type = *type; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | if (name_in.name) { |
| 71 | name_out.entry = |
| 72 | util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen)); |
| 73 | } else if (name_in.name8) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 74 | name_out.entry.assign(name_in.name8, name_in.nameLen); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | } else { |
| 76 | return {}; |
| 77 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | return name_out; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref, |
| 82 | bool* out_private) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | if (str.empty()) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | size_t offset = 0; |
| 88 | bool priv = false; |
| 89 | if (str.data()[0] == '*') { |
| 90 | priv = true; |
| 91 | offset = 1; |
| 92 | } |
| 93 | |
| 94 | StringPiece package; |
| 95 | StringPiece type; |
| 96 | StringPiece entry; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 97 | if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type, |
| 98 | &entry)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | const ResourceType* parsed_type = ParseResourceType(type); |
| 103 | if (!parsed_type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (entry.empty()) { |
| 108 | return false; |
| 109 | } |
| 110 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | if (out_ref) { |
| 112 | out_ref->package = package; |
| 113 | out_ref->type = *parsed_type; |
| 114 | out_ref->entry = entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | if (out_private) { |
| 118 | *out_private = priv; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref, |
| 124 | bool* out_create, bool* out_private) { |
| 125 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 126 | if (trimmed_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool create = false; |
| 131 | bool priv = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | if (trimmed_str.data()[0] == '@') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | size_t offset = 1; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | if (trimmed_str.data()[1] == '+') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | create = true; |
| 136 | offset += 1; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | ResourceNameRef name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | if (!ParseResourceName( |
| 141 | trimmed_str.substr(offset, trimmed_str.size() - offset), &name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | &priv)) { |
| 143 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | if (create && priv) { |
| 147 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | if (create && name.type != ResourceType::kId) { |
| 151 | return false; |
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_ref) { |
| 155 | *out_ref = name; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | if (out_create) { |
| 159 | *out_create = create; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | if (out_private) { |
| 163 | *out_private = priv; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 164 | } |
| 165 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | } |
| 167 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | bool IsReference(const StringPiece& str) { |
| 171 | return ParseReference(str, nullptr, nullptr, nullptr); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) { |
| 175 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 176 | if (trimmed_str.empty()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | if (*trimmed_str.data() == '?') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | StringPiece package; |
| 182 | StringPiece type; |
| 183 | StringPiece entry; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 184 | if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package, |
| 185 | &type, &entry)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
| 189 | if (!type.empty() && type != "attr") { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | if (entry.empty()) { |
| 194 | return false; |
| 195 | } |
| 196 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | if (out_ref) { |
| 198 | out_ref->package = package; |
| 199 | out_ref->type = ResourceType::kAttr; |
| 200 | out_ref->entry = entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | bool IsAttributeReference(const StringPiece& str) { |
| 208 | return ParseAttributeReference(str, nullptr); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Style parent's are a bit different. We accept the following formats: |
| 213 | * |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 214 | * @[[*]package:][style/]<entry> |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 215 | * ?[[*]package:]style/<entry> |
| 216 | * <[*]package>:[style/]<entry> |
| 217 | * [[*]package:style/]<entry> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | Maybe<Reference> ParseStyleParentReference(const StringPiece& str, |
| 220 | std::string* out_error) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 221 | if (str.empty()) { |
| 222 | return {}; |
| 223 | } |
| 224 | |
| 225 | StringPiece name = str; |
| 226 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | bool has_leading_identifiers = false; |
| 228 | bool private_ref = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | |
| 230 | // Skip over these identifiers. A style's parent is a normal reference. |
| 231 | if (name.data()[0] == '@' || name.data()[0] == '?') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | has_leading_identifiers = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | name = name.substr(1, name.size() - 1); |
| 234 | } |
| 235 | |
| 236 | if (name.data()[0] == '*') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | private_ref = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | name = name.substr(1, name.size() - 1); |
| 239 | } |
| 240 | |
| 241 | ResourceNameRef ref; |
| 242 | ref.type = ResourceType::kStyle; |
| 243 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | StringPiece type_str; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 245 | android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | if (!type_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | // If we have a type, make sure it is a Style. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 249 | if (!parsed_type || *parsed_type != ResourceType::kStyle) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | std::stringstream err; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | err << "invalid resource type '" << type_str << "' for parent of style"; |
| 252 | *out_error = err.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 254 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 255 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | std::stringstream err; |
| 259 | err << "invalid parent reference '" << str << "'"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | *out_error = err.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | return {}; |
| 262 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | Reference result(ref); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | result.private_reference = private_ref; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | return result; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) { |
| 270 | StringPiece trimmed_str = util::TrimWhitespace(str); |
| 271 | const char* start = trimmed_str.data(); |
| 272 | const char* const end = start + trimmed_str.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | const char* p = start; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | Reference ref; |
| 276 | if (p != end && *p == '*') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | ref.private_reference = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | start++; |
| 279 | p++; |
| 280 | } |
| 281 | |
| 282 | StringPiece package; |
| 283 | StringPiece name; |
| 284 | while (p != end) { |
| 285 | if (*p == ':') { |
| 286 | package = StringPiece(start, p - start); |
| 287 | name = StringPiece(p + 1, end - (p + 1)); |
| 288 | break; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 289 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | p++; |
| 291 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 292 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 293 | ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 294 | return Maybe<Reference>(std::move(ref)); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | std::unique_ptr<Reference> TryParseReference(const StringPiece& str, |
| 298 | bool* out_create) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | ResourceNameRef ref; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | bool private_ref = false; |
| 301 | if (ParseReference(str, &ref, out_create, &private_ref)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | std::unique_ptr<Reference> value = util::make_unique<Reference>(ref); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 303 | value->private_reference = private_ref; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | return value; |
| 305 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 306 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | if (ParseAttributeReference(str, &ref)) { |
| 308 | if (out_create) { |
| 309 | *out_create = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 310 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | return util::make_unique<Reference>(ref, Reference::Type::kAttribute); |
| 312 | } |
| 313 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 316 | std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) { |
| 317 | const StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | if (trimmed_str == "@null") { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 319 | return MakeNull(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | } else if (trimmed_str == "@empty") { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 321 | return MakeEmpty(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 | } |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 323 | return {}; |
| 324 | } |
| 325 | |
| 326 | std::unique_ptr<Reference> MakeNull() { |
| 327 | // TYPE_NULL with data set to 0 is interpreted by the runtime as an error. |
| 328 | // Instead we set the data type to TYPE_REFERENCE with a value of 0. |
| 329 | return util::make_unique<Reference>(); |
| 330 | } |
| 331 | |
| 332 | std::unique_ptr<BinaryPrimitive> MakeEmpty() { |
| 333 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL, |
| 334 | android::Res_value::DATA_NULL_EMPTY); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 338 | const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 340 | for (const Attribute::Symbol& symbol : enum_attr->symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | // Enum symbols are stored as @package:id/symbol resources, |
| 342 | // so we need to match against the 'entry' part of the identifier. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value(); |
| 344 | if (trimmed_str == enum_symbol_resource_name.entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | android::Res_value value = {}; |
| 346 | value.dataType = android::Res_value::TYPE_INT_DEC; |
| 347 | value.data = symbol.value; |
| 348 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | } |
| 351 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 355 | const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | android::Res_value flags = {}; |
| 357 | flags.dataType = android::Res_value::TYPE_INT_HEX; |
| 358 | flags.data = 0u; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 359 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 360 | if (util::TrimWhitespace(str).empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 | // Empty string is a valid flag (0). |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 362 | return util::make_unique<BinaryPrimitive>(flags); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | for (StringPiece part : util::Tokenize(str, '|')) { |
| 366 | StringPiece trimmed_part = util::TrimWhitespace(part); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | bool flag_set = false; |
| 369 | for (const Attribute::Symbol& symbol : flag_attr->symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | // Flag symbols are stored as @package:id/symbol resources, |
| 371 | // so we need to match against the 'entry' part of the identifier. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | const ResourceName& flag_symbol_resource_name = |
| 373 | symbol.symbol.name.value(); |
| 374 | if (trimmed_part == flag_symbol_resource_name.entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | flags.data |= symbol.value; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | flag_set = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | if (!flag_set) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | return {}; |
| 383 | } |
| 384 | } |
| 385 | return util::make_unique<BinaryPrimitive>(flags); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | static uint32_t ParseHex(char c, bool* out_error) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | if (c >= '0' && c <= '9') { |
| 390 | return c - '0'; |
| 391 | } else if (c >= 'a' && c <= 'f') { |
| 392 | return c - 'a' + 0xa; |
| 393 | } else if (c >= 'A' && c <= 'F') { |
| 394 | return c - 'A' + 0xa; |
| 395 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 396 | *out_error = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | return 0xffffffffu; |
| 398 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) { |
| 402 | StringPiece color_str(util::TrimWhitespace(str)); |
| 403 | const char* start = color_str.data(); |
| 404 | const size_t len = color_str.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 405 | if (len == 0 || start[0] != '#') { |
| 406 | return {}; |
| 407 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 408 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 409 | android::Res_value value = {}; |
| 410 | bool error = false; |
| 411 | if (len == 4) { |
| 412 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4; |
| 413 | value.data = 0xff000000u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 414 | value.data |= ParseHex(start[1], &error) << 20; |
| 415 | value.data |= ParseHex(start[1], &error) << 16; |
| 416 | value.data |= ParseHex(start[2], &error) << 12; |
| 417 | value.data |= ParseHex(start[2], &error) << 8; |
| 418 | value.data |= ParseHex(start[3], &error) << 4; |
| 419 | value.data |= ParseHex(start[3], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 420 | } else if (len == 5) { |
| 421 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 422 | value.data |= ParseHex(start[1], &error) << 28; |
| 423 | value.data |= ParseHex(start[1], &error) << 24; |
| 424 | value.data |= ParseHex(start[2], &error) << 20; |
| 425 | value.data |= ParseHex(start[2], &error) << 16; |
| 426 | value.data |= ParseHex(start[3], &error) << 12; |
| 427 | value.data |= ParseHex(start[3], &error) << 8; |
| 428 | value.data |= ParseHex(start[4], &error) << 4; |
| 429 | value.data |= ParseHex(start[4], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 430 | } else if (len == 7) { |
| 431 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8; |
| 432 | value.data = 0xff000000u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 | value.data |= ParseHex(start[1], &error) << 20; |
| 434 | value.data |= ParseHex(start[2], &error) << 16; |
| 435 | value.data |= ParseHex(start[3], &error) << 12; |
| 436 | value.data |= ParseHex(start[4], &error) << 8; |
| 437 | value.data |= ParseHex(start[5], &error) << 4; |
| 438 | value.data |= ParseHex(start[6], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 439 | } else if (len == 9) { |
| 440 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | value.data |= ParseHex(start[1], &error) << 28; |
| 442 | value.data |= ParseHex(start[2], &error) << 24; |
| 443 | value.data |= ParseHex(start[3], &error) << 20; |
| 444 | value.data |= ParseHex(start[4], &error) << 16; |
| 445 | value.data |= ParseHex(start[5], &error) << 12; |
| 446 | value.data |= ParseHex(start[6], &error) << 8; |
| 447 | value.data |= ParseHex(start[7], &error) << 4; |
| 448 | value.data |= ParseHex(start[8], &error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 449 | } else { |
| 450 | return {}; |
| 451 | } |
| 452 | return error ? std::unique_ptr<BinaryPrimitive>() |
| 453 | : util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 456 | Maybe<bool> ParseBool(const StringPiece& str) { |
| 457 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 458 | if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 459 | return Maybe<bool>(true); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | } else if (trimmed_str == "false" || trimmed_str == "FALSE" || |
| 461 | trimmed_str == "False") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 462 | return Maybe<bool>(false); |
| 463 | } |
| 464 | return {}; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 467 | Maybe<uint32_t> ParseInt(const StringPiece& str) { |
| 468 | std::u16string str16 = util::Utf8ToUtf16(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 469 | android::Res_value value; |
| 470 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 471 | return value.data; |
| 472 | } |
| 473 | return {}; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 476 | Maybe<ResourceId> ParseResourceId(const StringPiece& str) { |
| 477 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 478 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | android::Res_value value; |
| 481 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 482 | if (value.dataType == android::Res_value::TYPE_INT_HEX) { |
| 483 | ResourceId id(value.data); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 484 | if (id.is_valid_dynamic()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 485 | return id; |
| 486 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 487 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 488 | } |
| 489 | return {}; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | Maybe<int> ParseSdkVersion(const StringPiece& str) { |
| 493 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 494 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 495 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 496 | android::Res_value value; |
| 497 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 498 | return static_cast<int>(value.data); |
| 499 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 500 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 501 | // Try parsing the code name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 502 | std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion(); |
| 503 | if (entry.first == trimmed_str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 504 | return entry.second; |
| 505 | } |
| 506 | return {}; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) { |
| 510 | if (Maybe<bool> maybe_result = ParseBool(str)) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 511 | const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u; |
| 512 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 513 | } |
| 514 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 517 | std::unique_ptr<BinaryPrimitive> MakeBool(bool val) { |
| 518 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, |
| 519 | val ? 0xffffffffu : 0u); |
| 520 | } |
| 521 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 522 | std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) { |
Adam Lesinski | 8a3bffe | 2017-06-27 12:27:43 -0700 | [diff] [blame] | 523 | std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 524 | android::Res_value value; |
| 525 | if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 526 | return {}; |
| 527 | } |
| 528 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Shane Farmer | d05b913 | 2018-02-14 15:40:35 -0800 | [diff] [blame] | 531 | std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) { |
| 532 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val); |
| 533 | } |
| 534 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 535 | std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) { |
Adam Lesinski | 8a3bffe | 2017-06-27 12:27:43 -0700 | [diff] [blame] | 536 | std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 537 | android::Res_value value; |
| 538 | if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) { |
| 539 | return {}; |
| 540 | } |
| 541 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 544 | uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 545 | switch (type) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 546 | case android::Res_value::TYPE_NULL: |
| 547 | case android::Res_value::TYPE_REFERENCE: |
| 548 | case android::Res_value::TYPE_ATTRIBUTE: |
| 549 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 550 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 551 | return android::ResTable_map::TYPE_REFERENCE; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 552 | |
| 553 | case android::Res_value::TYPE_STRING: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 554 | return android::ResTable_map::TYPE_STRING; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 555 | |
| 556 | case android::Res_value::TYPE_FLOAT: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 557 | return android::ResTable_map::TYPE_FLOAT; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 558 | |
| 559 | case android::Res_value::TYPE_DIMENSION: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 560 | return android::ResTable_map::TYPE_DIMENSION; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 561 | |
| 562 | case android::Res_value::TYPE_FRACTION: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 563 | return android::ResTable_map::TYPE_FRACTION; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 564 | |
| 565 | case android::Res_value::TYPE_INT_DEC: |
| 566 | case android::Res_value::TYPE_INT_HEX: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 567 | return android::ResTable_map::TYPE_INTEGER | |
| 568 | android::ResTable_map::TYPE_ENUM | |
| 569 | android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 570 | |
| 571 | case android::Res_value::TYPE_INT_BOOLEAN: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 572 | return android::ResTable_map::TYPE_BOOLEAN; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 573 | |
| 574 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 575 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 576 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 577 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 578 | return android::ResTable_map::TYPE_COLOR; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 579 | |
| 580 | default: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 585 | std::unique_ptr<Item> TryParseItemForAttribute( |
| 586 | const StringPiece& value, uint32_t type_mask, |
| 587 | const std::function<void(const ResourceName&)>& on_create_reference) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 588 | using android::ResTable_map; |
| 589 | |
| 590 | auto null_or_empty = TryParseNullOrEmpty(value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 591 | if (null_or_empty) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 592 | return null_or_empty; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 594 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | bool create = false; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 596 | auto reference = TryParseReference(value, &create); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 597 | if (reference) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 598 | if (create && on_create_reference) { |
| 599 | on_create_reference(reference->name.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 600 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 601 | return std::move(reference); |
| 602 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 603 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 604 | if (type_mask & ResTable_map::TYPE_COLOR) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 605 | // Try parsing this as a color. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 606 | auto color = TryParseColor(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 607 | if (color) { |
| 608 | return std::move(color); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 609 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 610 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 611 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 612 | if (type_mask & ResTable_map::TYPE_BOOLEAN) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | // Try parsing this as a boolean. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 614 | auto boolean = TryParseBool(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 615 | if (boolean) { |
| 616 | return std::move(boolean); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 617 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 618 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 619 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 620 | if (type_mask & ResTable_map::TYPE_INTEGER) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 621 | // Try parsing this as an integer. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 622 | auto integer = TryParseInt(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 623 | if (integer) { |
| 624 | return std::move(integer); |
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 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 627 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 628 | const uint32_t float_mask = |
| 629 | ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 630 | if (type_mask & float_mask) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 631 | // Try parsing this as a float. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 632 | auto floating_point = TryParseFloat(value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 633 | if (floating_point) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 634 | if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 635 | return std::move(floating_point); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 636 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 637 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 638 | } |
| 639 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
| 643 | * We successively try to parse the string as a resource type that the Attribute |
| 644 | * allows. |
| 645 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 646 | std::unique_ptr<Item> TryParseItemForAttribute( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 647 | const StringPiece& str, const Attribute* attr, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | const std::function<void(const ResourceName&)>& on_create_reference) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 649 | using android::ResTable_map; |
| 650 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 651 | const uint32_t type_mask = attr->type_mask; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 652 | auto value = TryParseItemForAttribute(str, type_mask, on_create_reference); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 653 | if (value) { |
| 654 | return value; |
| 655 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 656 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 657 | if (type_mask & ResTable_map::TYPE_ENUM) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 658 | // Try parsing this as an enum. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 659 | auto enum_value = TryParseEnumSymbol(attr, str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 660 | if (enum_value) { |
| 661 | return std::move(enum_value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 662 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 663 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 664 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 665 | if (type_mask & ResTable_map::TYPE_FLAGS) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | // Try parsing this as a flag. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 667 | auto flag_value = TryParseFlagSymbol(attr, str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 668 | if (flag_value) { |
| 669 | return std::move(flag_value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 670 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 671 | } |
| 672 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 675 | std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 676 | std::stringstream out; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 677 | out << "res/" << res_file.name.type; |
| 678 | if (res_file.config != ConfigDescription{}) { |
| 679 | out << "-" << res_file.config; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 680 | } |
| 681 | out << "/"; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 682 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 683 | if (mangler && mangler->ShouldMangle(res_file.name.package)) { |
| 684 | out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 685 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 686 | out << res_file.name.entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 687 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 688 | out << file::GetExtension(res_file.source.path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 689 | return out.str(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 692 | std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config, |
| 693 | const android::ResStringPool& src_pool, |
| 694 | const android::Res_value& res_value, |
| 695 | StringPool* dst_pool) { |
| 696 | if (type == ResourceType::kId) { |
| 697 | return util::make_unique<Id>(); |
| 698 | } |
| 699 | |
| 700 | const uint32_t data = util::DeviceToHost32(res_value.data); |
| 701 | switch (res_value.dataType) { |
| 702 | case android::Res_value::TYPE_STRING: { |
| 703 | const std::string str = util::GetString(src_pool, data); |
| 704 | const android::ResStringPool_span* spans = src_pool.styleAt(data); |
| 705 | |
| 706 | // Check if the string has a valid style associated with it. |
| 707 | if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) { |
| 708 | StyleString style_str = {str}; |
| 709 | while (spans->name.index != android::ResStringPool_span::END) { |
| 710 | style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index), |
| 711 | spans->firstChar, spans->lastChar}); |
| 712 | spans++; |
| 713 | } |
| 714 | return util::make_unique<StyledString>(dst_pool->MakeRef( |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 715 | style_str, StringPool::Context(StringPool::Context::kNormalPriority, config))); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 716 | } else { |
| 717 | if (type != ResourceType::kString && util::StartsWith(str, "res/")) { |
| 718 | // This must be a FileReference. |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 719 | std::unique_ptr<FileReference> file_ref = |
| 720 | util::make_unique<FileReference>(dst_pool->MakeRef( |
| 721 | str, StringPool::Context(StringPool::Context::kHighPriority, config))); |
Pierre Lecesne | 70fdf76 | 2017-11-27 19:29:42 +0000 | [diff] [blame] | 722 | if (type == ResourceType::kRaw) { |
| 723 | file_ref->type = ResourceFile::Type::kUnknown; |
| 724 | } else if (util::EndsWith(*file_ref->path, ".xml")) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 725 | file_ref->type = ResourceFile::Type::kBinaryXml; |
| 726 | } else if (util::EndsWith(*file_ref->path, ".png")) { |
| 727 | file_ref->type = ResourceFile::Type::kPng; |
| 728 | } |
| 729 | return std::move(file_ref); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | // There are no styles associated with this string, so treat it as a simple string. |
| 733 | return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config))); |
| 734 | } |
| 735 | } break; |
| 736 | |
| 737 | case android::Res_value::TYPE_REFERENCE: |
| 738 | case android::Res_value::TYPE_ATTRIBUTE: |
| 739 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
| 740 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: { |
| 741 | Reference::Type ref_type = Reference::Type::kResource; |
| 742 | if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE || |
| 743 | res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) { |
| 744 | ref_type = Reference::Type::kAttribute; |
| 745 | } |
| 746 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 747 | if (data == 0u) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 748 | // A reference of 0, must be the magic @null reference. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 749 | return util::make_unique<Reference>(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // This is a normal reference. |
| 753 | return util::make_unique<Reference>(data, ref_type); |
| 754 | } break; |
| 755 | } |
| 756 | |
| 757 | // Treat this as a raw binary primitive. |
| 758 | return util::make_unique<BinaryPrimitive>(res_value); |
| 759 | } |
| 760 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 761 | // Converts the codepoint to UTF-8 and appends it to the string. |
| 762 | static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) { |
| 763 | ssize_t len = utf32_to_utf8_length(&codepoint, 1); |
| 764 | if (len < 0) { |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | const size_t start_append_pos = output->size(); |
| 769 | |
| 770 | // Make room for the next character. |
| 771 | output->resize(output->size() + len); |
| 772 | |
| 773 | char* dst = &*(output->begin() + start_append_pos); |
| 774 | utf32_to_utf8(&codepoint, 1, dst, len + 1); |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | // Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the |
| 779 | // Unicode codepoint represented by the escape sequence to the string. |
| 780 | static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) { |
| 781 | char32_t code = 0; |
| 782 | for (size_t i = 0; i < 4 && iter->HasNext(); i++) { |
| 783 | char32_t codepoint = iter->Next(); |
| 784 | char32_t a; |
| 785 | if (codepoint >= U'0' && codepoint <= U'9') { |
| 786 | a = codepoint - U'0'; |
| 787 | } else if (codepoint >= U'a' && codepoint <= U'f') { |
| 788 | a = codepoint - U'a' + 10; |
| 789 | } else if (codepoint >= U'A' && codepoint <= U'F') { |
| 790 | a = codepoint - U'A' + 10; |
| 791 | } else { |
| 792 | return {}; |
| 793 | } |
| 794 | code = (code << 4) | a; |
| 795 | } |
| 796 | return AppendCodepointToUtf8String(code, output); |
| 797 | } |
| 798 | |
| 799 | StringBuilder::StringBuilder(bool preserve_spaces) |
| 800 | : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) { |
| 801 | } |
| 802 | |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 803 | StringBuilder& StringBuilder::AppendText(const std::string& text, bool preserve_spaces) { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 804 | if (!error_.empty()) { |
| 805 | return *this; |
| 806 | } |
| 807 | |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 808 | // Enable preserving spaces if it is enabled for this append or the StringBuilder was constructed |
| 809 | // to preserve spaces |
| 810 | preserve_spaces = (preserve_spaces) ? preserve_spaces : preserve_spaces_; |
| 811 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 812 | const size_t previous_len = xml_string_.text.size(); |
| 813 | Utf8Iterator iter(text); |
| 814 | while (iter.HasNext()) { |
| 815 | char32_t codepoint = iter.Next(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 816 | if (!preserve_spaces && !quote_ && codepoint != kNonBreakingSpace && iswspace(codepoint)) { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 817 | if (!last_codepoint_was_space_) { |
| 818 | // Emit a space if it's the first. |
| 819 | xml_string_.text += ' '; |
| 820 | last_codepoint_was_space_ = true; |
| 821 | } |
| 822 | |
| 823 | // Keep eating spaces. |
| 824 | continue; |
| 825 | } |
| 826 | |
| 827 | // This is not a space. |
| 828 | last_codepoint_was_space_ = false; |
| 829 | |
| 830 | if (codepoint == U'\\') { |
| 831 | if (iter.HasNext()) { |
| 832 | codepoint = iter.Next(); |
| 833 | switch (codepoint) { |
| 834 | case U't': |
| 835 | xml_string_.text += '\t'; |
| 836 | break; |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 837 | case U'n': |
| 838 | xml_string_.text += '\n'; |
| 839 | break; |
| 840 | |
| 841 | case U'#': |
| 842 | case U'@': |
| 843 | case U'?': |
| 844 | case U'"': |
| 845 | case U'\'': |
| 846 | case U'\\': |
| 847 | xml_string_.text += static_cast<char>(codepoint); |
| 848 | break; |
| 849 | |
| 850 | case U'u': |
| 851 | if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) { |
| 852 | error_ = |
| 853 | StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str()); |
| 854 | return *this; |
| 855 | } |
| 856 | break; |
| 857 | |
| 858 | default: |
| 859 | // Ignore the escape character and just include the codepoint. |
| 860 | AppendCodepointToUtf8String(codepoint, &xml_string_.text); |
| 861 | break; |
| 862 | } |
| 863 | } |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 864 | } else if (!preserve_spaces && codepoint == U'"') { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 865 | // Only toggle the quote state when we are not preserving spaces. |
| 866 | quote_ = !quote_; |
| 867 | |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 868 | } else if (!preserve_spaces && !quote_ && codepoint == U'\'') { |
| 869 | // This should be escaped when we are not preserving spaces |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 870 | error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str()); |
| 871 | return *this; |
| 872 | |
| 873 | } else { |
| 874 | AppendCodepointToUtf8String(codepoint, &xml_string_.text); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | // Accumulate the added string's UTF-16 length. |
| 879 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str()); |
| 880 | const size_t utf8_length = xml_string_.text.size(); |
| 881 | ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len); |
| 882 | if (len < 0) { |
| 883 | error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len); |
| 884 | return *this; |
| 885 | } |
| 886 | |
| 887 | utf16_len_ += static_cast<uint32_t>(len); |
| 888 | return *this; |
| 889 | } |
| 890 | |
| 891 | StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) { |
| 892 | if (!error_.empty()) { |
| 893 | return 0u; |
| 894 | } |
| 895 | |
| 896 | // When we start a span, all state associated with whitespace truncation and quotation is ended. |
| 897 | ResetTextState(); |
| 898 | Span span; |
| 899 | span.name = name; |
| 900 | span.first_char = span.last_char = utf16_len_; |
| 901 | xml_string_.spans.push_back(std::move(span)); |
| 902 | return xml_string_.spans.size() - 1; |
| 903 | } |
| 904 | |
| 905 | void StringBuilder::EndSpan(SpanHandle handle) { |
| 906 | if (!error_.empty()) { |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | // When we end a span, all state associated with whitespace truncation and quotation is ended. |
| 911 | ResetTextState(); |
| 912 | xml_string_.spans[handle].last_char = utf16_len_ - 1u; |
| 913 | } |
| 914 | |
| 915 | StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() { |
| 916 | if (!error_.empty()) { |
| 917 | return 0u; |
| 918 | } |
| 919 | |
| 920 | UntranslatableSection section; |
| 921 | section.start = section.end = xml_string_.text.size(); |
| 922 | xml_string_.untranslatable_sections.push_back(section); |
| 923 | return xml_string_.untranslatable_sections.size() - 1; |
| 924 | } |
| 925 | |
| 926 | void StringBuilder::EndUntranslatable(UntranslatableHandle handle) { |
| 927 | if (!error_.empty()) { |
| 928 | return; |
| 929 | } |
| 930 | xml_string_.untranslatable_sections[handle].end = xml_string_.text.size(); |
| 931 | } |
| 932 | |
| 933 | FlattenedXmlString StringBuilder::GetFlattenedString() const { |
| 934 | return xml_string_; |
| 935 | } |
| 936 | |
| 937 | std::string StringBuilder::to_string() const { |
| 938 | return xml_string_.text; |
| 939 | } |
| 940 | |
| 941 | StringBuilder::operator bool() const { |
| 942 | return error_.empty(); |
| 943 | } |
| 944 | |
| 945 | std::string StringBuilder::GetError() const { |
| 946 | return error_; |
| 947 | } |
| 948 | |
| 949 | void StringBuilder::ResetTextState() { |
| 950 | quote_ = preserve_spaces_; |
| 951 | last_codepoint_was_space_ = false; |
| 952 | } |
| 953 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 954 | } // namespace ResourceUtils |
| 955 | } // namespace aapt |