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