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