| 1 | /* |
| 2 | * Copyright (C) 2016 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 "format/proto/ProtoDeserialize.h" |
| 18 | |
| 19 | #include "android-base/logging.h" |
| 20 | #include "android-base/macros.h" |
| 21 | #include "androidfw/ResourceTypes.h" |
| 22 | #include "androidfw/Locale.h" |
| 23 | |
| 24 | #include "ResourceTable.h" |
| 25 | #include "ResourceUtils.h" |
| 26 | #include "ResourceValues.h" |
| 27 | #include "ValueVisitor.h" |
| 28 | |
| 29 | using ::android::ConfigDescription; |
| 30 | using ::android::LocaleValue; |
| 31 | using ::android::ResStringPool; |
| 32 | |
| 33 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 34 | |
| 35 | namespace aapt { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | class ReferenceIdToNameVisitor : public DescendingValueVisitor { |
| 40 | public: |
| 41 | using DescendingValueVisitor::Visit; |
| 42 | |
| 43 | explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceNameRef>* mapping) |
| 44 | : mapping_(mapping) { |
| 45 | CHECK(mapping_ != nullptr); |
| 46 | } |
| 47 | |
| 48 | void Visit(Reference* reference) override { |
| 49 | if (!reference->id || !reference->id.value().is_valid()) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | ResourceId id = reference->id.value(); |
| 54 | auto cache_iter = mapping_->find(id); |
| 55 | if (cache_iter != mapping_->end()) { |
| 56 | reference->name = cache_iter->second.ToResourceName(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor); |
| 62 | |
| 63 | const std::map<ResourceId, ResourceNameRef>* mapping_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | bool DeserializeConfigFromPb(const pb::Configuration& pb_config, ConfigDescription* out_config, |
| 69 | std::string* out_error) { |
| 70 | out_config->mcc = static_cast<uint16_t>(pb_config.mcc()); |
| 71 | out_config->mnc = static_cast<uint16_t>(pb_config.mnc()); |
| 72 | |
| 73 | if (!pb_config.locale().empty()) { |
| 74 | LocaleValue lv; |
| 75 | if (!lv.InitFromBcp47Tag(pb_config.locale())) { |
| 76 | std::ostringstream error; |
| 77 | error << "configuration has invalid locale '" << pb_config.locale() << "'"; |
| 78 | *out_error = error.str(); |
| 79 | return false; |
| 80 | } |
| 81 | lv.WriteTo(out_config); |
| 82 | } |
| 83 | |
| 84 | switch (pb_config.layout_direction()) { |
| 85 | case pb::Configuration_LayoutDirection_LAYOUT_DIRECTION_LTR: |
| 86 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_LAYOUTDIR) | |
| 87 | ConfigDescription::LAYOUTDIR_LTR; |
| 88 | break; |
| 89 | |
| 90 | case pb::Configuration_LayoutDirection_LAYOUT_DIRECTION_RTL: |
| 91 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_LAYOUTDIR) | |
| 92 | ConfigDescription::LAYOUTDIR_RTL; |
| 93 | break; |
| 94 | |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | out_config->smallestScreenWidthDp = static_cast<uint16_t>(pb_config.smallest_screen_width_dp()); |
| 100 | out_config->screenWidthDp = static_cast<uint16_t>(pb_config.screen_width_dp()); |
| 101 | out_config->screenHeightDp = static_cast<uint16_t>(pb_config.screen_height_dp()); |
| 102 | |
| 103 | switch (pb_config.screen_layout_size()) { |
| 104 | case pb::Configuration_ScreenLayoutSize_SCREEN_LAYOUT_SIZE_SMALL: |
| 105 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENSIZE) | |
| 106 | ConfigDescription::SCREENSIZE_SMALL; |
| 107 | break; |
| 108 | |
| 109 | case pb::Configuration_ScreenLayoutSize_SCREEN_LAYOUT_SIZE_NORMAL: |
| 110 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENSIZE) | |
| 111 | ConfigDescription::SCREENSIZE_NORMAL; |
| 112 | break; |
| 113 | |
| 114 | case pb::Configuration_ScreenLayoutSize_SCREEN_LAYOUT_SIZE_LARGE: |
| 115 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENSIZE) | |
| 116 | ConfigDescription::SCREENSIZE_LARGE; |
| 117 | break; |
| 118 | |
| 119 | case pb::Configuration_ScreenLayoutSize_SCREEN_LAYOUT_SIZE_XLARGE: |
| 120 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENSIZE) | |
| 121 | ConfigDescription::SCREENSIZE_XLARGE; |
| 122 | break; |
| 123 | |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | switch (pb_config.screen_layout_long()) { |
| 129 | case pb::Configuration_ScreenLayoutLong_SCREEN_LAYOUT_LONG_LONG: |
| 130 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENLONG) | |
| 131 | ConfigDescription::SCREENLONG_YES; |
| 132 | break; |
| 133 | |
| 134 | case pb::Configuration_ScreenLayoutLong_SCREEN_LAYOUT_LONG_NOTLONG: |
| 135 | out_config->screenLayout = (out_config->screenLayout & ~ConfigDescription::MASK_SCREENLONG) | |
| 136 | ConfigDescription::SCREENLONG_NO; |
| 137 | break; |
| 138 | |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | switch (pb_config.screen_round()) { |
| 144 | case pb::Configuration_ScreenRound_SCREEN_ROUND_ROUND: |
| 145 | out_config->screenLayout2 = |
| 146 | (out_config->screenLayout2 & ~ConfigDescription::MASK_SCREENROUND) | |
| 147 | ConfigDescription::SCREENROUND_YES; |
| 148 | break; |
| 149 | |
| 150 | case pb::Configuration_ScreenRound_SCREEN_ROUND_NOTROUND: |
| 151 | out_config->screenLayout2 = |
| 152 | (out_config->screenLayout2 & ~ConfigDescription::MASK_SCREENROUND) | |
| 153 | ConfigDescription::SCREENROUND_NO; |
| 154 | break; |
| 155 | |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | switch (pb_config.wide_color_gamut()) { |
| 161 | case pb::Configuration_WideColorGamut_WIDE_COLOR_GAMUT_WIDECG: |
| 162 | out_config->colorMode = (out_config->colorMode & ~ConfigDescription::MASK_WIDE_COLOR_GAMUT) | |
| 163 | ConfigDescription::WIDE_COLOR_GAMUT_YES; |
| 164 | break; |
| 165 | |
| 166 | case pb::Configuration_WideColorGamut_WIDE_COLOR_GAMUT_NOWIDECG: |
| 167 | out_config->colorMode = (out_config->colorMode & ~ConfigDescription::MASK_WIDE_COLOR_GAMUT) | |
| 168 | ConfigDescription::WIDE_COLOR_GAMUT_NO; |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | switch (pb_config.hdr()) { |
| 176 | case pb::Configuration_Hdr_HDR_HIGHDR: |
| 177 | out_config->colorMode = |
| 178 | (out_config->colorMode & ~ConfigDescription::MASK_HDR) | ConfigDescription::HDR_YES; |
| 179 | break; |
| 180 | |
| 181 | case pb::Configuration_Hdr_HDR_LOWDR: |
| 182 | out_config->colorMode = |
| 183 | (out_config->colorMode & ~ConfigDescription::MASK_HDR) | ConfigDescription::HDR_NO; |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | switch (pb_config.orientation()) { |
| 191 | case pb::Configuration_Orientation_ORIENTATION_PORT: |
| 192 | out_config->orientation = ConfigDescription::ORIENTATION_PORT; |
| 193 | break; |
| 194 | |
| 195 | case pb::Configuration_Orientation_ORIENTATION_LAND: |
| 196 | out_config->orientation = ConfigDescription::ORIENTATION_LAND; |
| 197 | break; |
| 198 | |
| 199 | case pb::Configuration_Orientation_ORIENTATION_SQUARE: |
| 200 | out_config->orientation = ConfigDescription::ORIENTATION_SQUARE; |
| 201 | break; |
| 202 | |
| 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | switch (pb_config.ui_mode_type()) { |
| 208 | case pb::Configuration_UiModeType_UI_MODE_TYPE_NORMAL: |
| 209 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 210 | ConfigDescription::UI_MODE_TYPE_NORMAL; |
| 211 | break; |
| 212 | |
| 213 | case pb::Configuration_UiModeType_UI_MODE_TYPE_DESK: |
| 214 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 215 | ConfigDescription::UI_MODE_TYPE_DESK; |
| 216 | break; |
| 217 | |
| 218 | case pb::Configuration_UiModeType_UI_MODE_TYPE_CAR: |
| 219 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 220 | ConfigDescription::UI_MODE_TYPE_CAR; |
| 221 | break; |
| 222 | |
| 223 | case pb::Configuration_UiModeType_UI_MODE_TYPE_TELEVISION: |
| 224 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 225 | ConfigDescription::UI_MODE_TYPE_TELEVISION; |
| 226 | break; |
| 227 | |
| 228 | case pb::Configuration_UiModeType_UI_MODE_TYPE_APPLIANCE: |
| 229 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 230 | ConfigDescription::UI_MODE_TYPE_APPLIANCE; |
| 231 | break; |
| 232 | |
| 233 | case pb::Configuration_UiModeType_UI_MODE_TYPE_WATCH: |
| 234 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 235 | ConfigDescription::UI_MODE_TYPE_WATCH; |
| 236 | break; |
| 237 | |
| 238 | case pb::Configuration_UiModeType_UI_MODE_TYPE_VRHEADSET: |
| 239 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_TYPE) | |
| 240 | ConfigDescription::UI_MODE_TYPE_VR_HEADSET; |
| 241 | break; |
| 242 | |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | switch (pb_config.ui_mode_night()) { |
| 248 | case pb::Configuration_UiModeNight_UI_MODE_NIGHT_NIGHT: |
| 249 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_NIGHT) | |
| 250 | ConfigDescription::UI_MODE_NIGHT_YES; |
| 251 | break; |
| 252 | |
| 253 | case pb::Configuration_UiModeNight_UI_MODE_NIGHT_NOTNIGHT: |
| 254 | out_config->uiMode = (out_config->uiMode & ~ConfigDescription::MASK_UI_MODE_NIGHT) | |
| 255 | ConfigDescription::UI_MODE_NIGHT_NO; |
| 256 | break; |
| 257 | |
| 258 | default: |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | out_config->density = static_cast<uint16_t>(pb_config.density()); |
| 263 | |
| 264 | switch (pb_config.touchscreen()) { |
| 265 | case pb::Configuration_Touchscreen_TOUCHSCREEN_NOTOUCH: |
| 266 | out_config->touchscreen = ConfigDescription::TOUCHSCREEN_NOTOUCH; |
| 267 | break; |
| 268 | |
| 269 | case pb::Configuration_Touchscreen_TOUCHSCREEN_STYLUS: |
| 270 | out_config->touchscreen = ConfigDescription::TOUCHSCREEN_STYLUS; |
| 271 | break; |
| 272 | |
| 273 | case pb::Configuration_Touchscreen_TOUCHSCREEN_FINGER: |
| 274 | out_config->touchscreen = ConfigDescription::TOUCHSCREEN_FINGER; |
| 275 | break; |
| 276 | |
| 277 | default: |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | switch (pb_config.keys_hidden()) { |
| 282 | case pb::Configuration_KeysHidden_KEYS_HIDDEN_KEYSEXPOSED: |
| 283 | out_config->inputFlags = (out_config->inputFlags & ~ConfigDescription::MASK_KEYSHIDDEN) | |
| 284 | ConfigDescription::KEYSHIDDEN_NO; |
| 285 | break; |
| 286 | |
| 287 | case pb::Configuration_KeysHidden_KEYS_HIDDEN_KEYSHIDDEN: |
| 288 | out_config->inputFlags = (out_config->inputFlags & ~ConfigDescription::MASK_KEYSHIDDEN) | |
| 289 | ConfigDescription::KEYSHIDDEN_YES; |
| 290 | break; |
| 291 | |
| 292 | case pb::Configuration_KeysHidden_KEYS_HIDDEN_KEYSSOFT: |
| 293 | out_config->inputFlags = (out_config->inputFlags & ~ConfigDescription::MASK_KEYSHIDDEN) | |
| 294 | ConfigDescription::KEYSHIDDEN_SOFT; |
| 295 | break; |
| 296 | |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | switch (pb_config.keyboard()) { |
| 302 | case pb::Configuration_Keyboard_KEYBOARD_NOKEYS: |
| 303 | out_config->keyboard = ConfigDescription::KEYBOARD_NOKEYS; |
| 304 | break; |
| 305 | |
| 306 | case pb::Configuration_Keyboard_KEYBOARD_QWERTY: |
| 307 | out_config->keyboard = ConfigDescription::KEYBOARD_QWERTY; |
| 308 | break; |
| 309 | |
| 310 | case pb::Configuration_Keyboard_KEYBOARD_TWELVEKEY: |
| 311 | out_config->keyboard = ConfigDescription::KEYBOARD_12KEY; |
| 312 | break; |
| 313 | |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | switch (pb_config.nav_hidden()) { |
| 319 | case pb::Configuration_NavHidden_NAV_HIDDEN_NAVEXPOSED: |
| 320 | out_config->inputFlags = (out_config->inputFlags & ~ConfigDescription::MASK_NAVHIDDEN) | |
| 321 | ConfigDescription::NAVHIDDEN_NO; |
| 322 | break; |
| 323 | |
| 324 | case pb::Configuration_NavHidden_NAV_HIDDEN_NAVHIDDEN: |
| 325 | out_config->inputFlags = (out_config->inputFlags & ~ConfigDescription::MASK_NAVHIDDEN) | |
| 326 | ConfigDescription::NAVHIDDEN_YES; |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | switch (pb_config.navigation()) { |
| 334 | case pb::Configuration_Navigation_NAVIGATION_NONAV: |
| 335 | out_config->navigation = ConfigDescription::NAVIGATION_NONAV; |
| 336 | break; |
| 337 | |
| 338 | case pb::Configuration_Navigation_NAVIGATION_DPAD: |
| 339 | out_config->navigation = ConfigDescription::NAVIGATION_DPAD; |
| 340 | break; |
| 341 | |
| 342 | case pb::Configuration_Navigation_NAVIGATION_TRACKBALL: |
| 343 | out_config->navigation = ConfigDescription::NAVIGATION_TRACKBALL; |
| 344 | break; |
| 345 | |
| 346 | case pb::Configuration_Navigation_NAVIGATION_WHEEL: |
| 347 | out_config->navigation = ConfigDescription::NAVIGATION_WHEEL; |
| 348 | break; |
| 349 | |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | out_config->screenWidth = static_cast<uint16_t>(pb_config.screen_width()); |
| 355 | out_config->screenHeight = static_cast<uint16_t>(pb_config.screen_height()); |
| 356 | out_config->sdkVersion = static_cast<uint16_t>(pb_config.sdk_version()); |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | static void DeserializeSourceFromPb(const pb::Source& pb_source, const ResStringPool& src_pool, |
| 361 | Source* out_source) { |
| 362 | out_source->path = util::GetString(src_pool, pb_source.path_idx()); |
| 363 | out_source->line = static_cast<size_t>(pb_source.position().line_number()); |
| 364 | } |
| 365 | |
| 366 | static Visibility::Level DeserializeVisibilityFromPb(const pb::Visibility::Level& pb_level) { |
| 367 | switch (pb_level) { |
| 368 | case pb::Visibility::PRIVATE: |
| 369 | return Visibility::Level::kPrivate; |
| 370 | case pb::Visibility::PUBLIC: |
| 371 | return Visibility::Level::kPublic; |
| 372 | default: |
| 373 | break; |
| 374 | } |
| 375 | return Visibility::Level::kUndefined; |
| 376 | } |
| 377 | |
| 378 | bool DeserializeOverlayableItemFromPb(const pb::OverlayableItem& pb_overlayable, |
| 379 | const android::ResStringPool& src_pool, |
| 380 | OverlayableItem* out_overlayable, std::string* out_error) { |
| 381 | for (const int policy : pb_overlayable.policy()) { |
| 382 | switch (policy) { |
| 383 | case pb::OverlayableItem::PUBLIC: |
| 384 | out_overlayable->policies |= PolicyFlags::PUBLIC; |
| 385 | break; |
| 386 | case pb::OverlayableItem::SYSTEM: |
| 387 | out_overlayable->policies |= PolicyFlags::SYSTEM_PARTITION; |
| 388 | break; |
| 389 | case pb::OverlayableItem::VENDOR: |
| 390 | out_overlayable->policies |= PolicyFlags::VENDOR_PARTITION; |
| 391 | break; |
| 392 | case pb::OverlayableItem::PRODUCT: |
| 393 | out_overlayable->policies |= PolicyFlags::PRODUCT_PARTITION; |
| 394 | break; |
| 395 | case pb::OverlayableItem::SIGNATURE: |
| 396 | out_overlayable->policies |= PolicyFlags::SIGNATURE; |
| 397 | break; |
| 398 | case pb::OverlayableItem::ODM: |
| 399 | out_overlayable->policies |= PolicyFlags::ODM_PARTITION; |
| 400 | break; |
| 401 | case pb::OverlayableItem::OEM: |
| 402 | out_overlayable->policies |= PolicyFlags::OEM_PARTITION; |
| 403 | break; |
| 404 | case pb::OverlayableItem::ACTOR: |
| 405 | out_overlayable->policies |= PolicyFlags::ACTOR_SIGNATURE; |
| 406 | break; |
| 407 | case pb::OverlayableItem::CONFIG_SIGNATURE: |
| 408 | out_overlayable->policies |= PolicyFlags::CONFIG_SIGNATURE; |
| 409 | break; |
| 410 | default: |
| 411 | *out_error = "unknown overlayable policy"; |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if (pb_overlayable.has_source()) { |
| 417 | DeserializeSourceFromPb(pb_overlayable.source(), src_pool, &out_overlayable->source); |
| 418 | } |
| 419 | |
| 420 | out_overlayable->comment = pb_overlayable.comment(); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | static bool DeserializePackageFromPb(const pb::Package& pb_package, const ResStringPool& src_pool, |
| 425 | io::IFileCollection* files, |
| 426 | const std::vector<std::shared_ptr<Overlayable>>& overlayables, |
| 427 | ResourceTable* out_table, std::string* out_error) { |
| 428 | std::map<ResourceId, ResourceNameRef> id_index; |
| 429 | |
| 430 | ResourceTablePackage* pkg = out_table->FindOrCreatePackage(pb_package.package_name()); |
| 431 | for (const pb::Type& pb_type : pb_package.type()) { |
| 432 | const ResourceType* res_type = ParseResourceType(pb_type.name()); |
| 433 | if (res_type == nullptr) { |
| 434 | std::ostringstream error; |
| 435 | error << "unknown type '" << pb_type.name() << "'"; |
| 436 | *out_error = error.str(); |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | ResourceTableType* type = pkg->FindOrCreateType(*res_type); |
| 441 | |
| 442 | for (const pb::Entry& pb_entry : pb_type.entry()) { |
| 443 | ResourceEntry* entry = type->CreateEntry(pb_entry.name()); |
| 444 | const ResourceId resource_id( |
| 445 | pb_package.has_package_id() ? static_cast<uint8_t>(pb_package.package_id().id()) : 0u, |
| 446 | pb_type.has_type_id() ? static_cast<uint8_t>(pb_type.type_id().id()) : 0u, |
| 447 | pb_entry.has_entry_id() ? static_cast<uint16_t>(pb_entry.entry_id().id()) : 0u); |
| 448 | if (resource_id.id != 0u) { |
| 449 | entry->id = resource_id; |
| 450 | } |
| 451 | |
| 452 | // Deserialize the symbol status (public/private with source and comments). |
| 453 | if (pb_entry.has_visibility()) { |
| 454 | const pb::Visibility& pb_visibility = pb_entry.visibility(); |
| 455 | if (pb_visibility.has_source()) { |
| 456 | DeserializeSourceFromPb(pb_visibility.source(), src_pool, &entry->visibility.source); |
| 457 | } |
| 458 | entry->visibility.comment = pb_visibility.comment(); |
| 459 | entry->visibility.staged_api = pb_visibility.staged_api(); |
| 460 | |
| 461 | const Visibility::Level level = DeserializeVisibilityFromPb(pb_visibility.level()); |
| 462 | entry->visibility.level = level; |
| 463 | if (level == Visibility::Level::kPublic) { |
| 464 | // Propagate the public visibility up to the Type. |
| 465 | type->visibility_level = Visibility::Level::kPublic; |
| 466 | } else if (level == Visibility::Level::kPrivate) { |
| 467 | // Only propagate if no previous state was assigned. |
| 468 | if (type->visibility_level == Visibility::Level::kUndefined) { |
| 469 | type->visibility_level = Visibility::Level::kPrivate; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (pb_entry.has_allow_new()) { |
| 475 | const pb::AllowNew& pb_allow_new = pb_entry.allow_new(); |
| 476 | |
| 477 | AllowNew allow_new; |
| 478 | if (pb_allow_new.has_source()) { |
| 479 | DeserializeSourceFromPb(pb_allow_new.source(), src_pool, &allow_new.source); |
| 480 | } |
| 481 | allow_new.comment = pb_allow_new.comment(); |
| 482 | entry->allow_new = std::move(allow_new); |
| 483 | } |
| 484 | |
| 485 | if (pb_entry.has_overlayable_item()) { |
| 486 | // Find the overlayable to which this item belongs |
| 487 | pb::OverlayableItem pb_overlayable_item = pb_entry.overlayable_item(); |
| 488 | if (pb_overlayable_item.overlayable_idx() >= overlayables.size()) { |
| 489 | *out_error = |
| 490 | android::base::StringPrintf("invalid overlayable_idx value %d for entry %s/%s", |
| 491 | pb_overlayable_item.overlayable_idx(), |
| 492 | pb_type.name().c_str(), pb_entry.name().c_str()); |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | OverlayableItem overlayable_item(overlayables[pb_overlayable_item.overlayable_idx()]); |
| 497 | if (!DeserializeOverlayableItemFromPb(pb_overlayable_item, src_pool, &overlayable_item, |
| 498 | out_error)) { |
| 499 | return false; |
| 500 | } |
| 501 | entry->overlayable_item = std::move(overlayable_item); |
| 502 | } |
| 503 | |
| 504 | if (pb_entry.has_staged_id()) { |
| 505 | const pb::StagedId& pb_staged_id = pb_entry.staged_id(); |
| 506 | |
| 507 | StagedId staged_id; |
| 508 | if (pb_staged_id.has_source()) { |
| 509 | DeserializeSourceFromPb(pb_staged_id.source(), src_pool, &staged_id.source); |
| 510 | } |
| 511 | staged_id.id = pb_staged_id.staged_id(); |
| 512 | entry->staged_id = std::move(staged_id); |
| 513 | } |
| 514 | |
| 515 | ResourceId resid(pb_package.package_id().id(), pb_type.type_id().id(), |
| 516 | pb_entry.entry_id().id()); |
| 517 | if (resid.is_valid()) { |
| 518 | id_index[resid] = ResourceNameRef(pkg->name, type->type, entry->name); |
| 519 | } |
| 520 | |
| 521 | for (const pb::ConfigValue& pb_config_value : pb_entry.config_value()) { |
| 522 | const pb::Configuration& pb_config = pb_config_value.config(); |
| 523 | |
| 524 | ConfigDescription config; |
| 525 | if (!DeserializeConfigFromPb(pb_config, &config, out_error)) { |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | ResourceConfigValue* config_value = entry->FindOrCreateValue(config, pb_config.product()); |
| 530 | if (config_value->value != nullptr) { |
| 531 | *out_error = "duplicate configuration in resource table"; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | config_value->value = DeserializeValueFromPb(pb_config_value.value(), src_pool, config, |
| 536 | &out_table->string_pool, files, out_error); |
| 537 | if (config_value->value == nullptr) { |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | ReferenceIdToNameVisitor visitor(&id_index); |
| 545 | VisitAllValuesInPackage(pkg, &visitor); |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | bool DeserializeTableFromPb(const pb::ResourceTable& pb_table, io::IFileCollection* files, |
| 550 | ResourceTable* out_table, std::string* out_error) { |
| 551 | // We import the android namespace because on Windows NO_ERROR is a macro, not an enum, which |
| 552 | // causes errors when qualifying it with android:: |
| 553 | using namespace android; |
| 554 | |
| 555 | ResStringPool source_pool; |
| 556 | if (pb_table.has_source_pool()) { |
| 557 | status_t result = source_pool.setTo(pb_table.source_pool().data().data(), |
| 558 | pb_table.source_pool().data().size()); |
| 559 | if (result != NO_ERROR) { |
| 560 | *out_error = "invalid source pool"; |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // Deserialize the overlayable groups of the table |
| 566 | std::vector<std::shared_ptr<Overlayable>> overlayables; |
| 567 | for (const pb::Overlayable& pb_overlayable : pb_table.overlayable()) { |
| 568 | auto group = std::make_shared<Overlayable>(pb_overlayable.name(), pb_overlayable.actor()); |
| 569 | if (pb_overlayable.has_source()) { |
| 570 | DeserializeSourceFromPb(pb_overlayable.source(), source_pool, &group->source); |
| 571 | } |
| 572 | overlayables.push_back(group); |
| 573 | } |
| 574 | |
| 575 | for (const pb::Package& pb_package : pb_table.package()) { |
| 576 | if (!DeserializePackageFromPb(pb_package, source_pool, files, overlayables, out_table, |
| 577 | out_error)) { |
| 578 | return false; |
| 579 | } |
| 580 | } |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | static ResourceFile::Type DeserializeFileReferenceTypeFromPb(const pb::FileReference::Type& type) { |
| 585 | switch (type) { |
| 586 | case pb::FileReference::BINARY_XML: |
| 587 | return ResourceFile::Type::kBinaryXml; |
| 588 | case pb::FileReference::PROTO_XML: |
| 589 | return ResourceFile::Type::kProtoXml; |
| 590 | case pb::FileReference::PNG: |
| 591 | return ResourceFile::Type::kPng; |
| 592 | default: |
| 593 | return ResourceFile::Type::kUnknown; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | bool DeserializeCompiledFileFromPb(const pb::internal::CompiledFile& pb_file, |
| 598 | ResourceFile* out_file, std::string* out_error) { |
| 599 | ResourceNameRef name_ref; |
| 600 | if (!ResourceUtils::ParseResourceName(pb_file.resource_name(), &name_ref)) { |
| 601 | std::ostringstream error; |
| 602 | error << "invalid resource name in compiled file header: " << pb_file.resource_name(); |
| 603 | *out_error = error.str(); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | out_file->name = name_ref.ToResourceName(); |
| 608 | out_file->source.path = pb_file.source_path(); |
| 609 | out_file->type = DeserializeFileReferenceTypeFromPb(pb_file.type()); |
| 610 | |
| 611 | std::string config_error; |
| 612 | if (!DeserializeConfigFromPb(pb_file.config(), &out_file->config, &config_error)) { |
| 613 | std::ostringstream error; |
| 614 | error << "invalid resource configuration in compiled file header: " << config_error; |
| 615 | *out_error = error.str(); |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | for (const pb::internal::CompiledFile_Symbol& pb_symbol : pb_file.exported_symbol()) { |
| 620 | if (!ResourceUtils::ParseResourceName(pb_symbol.resource_name(), &name_ref)) { |
| 621 | std::ostringstream error; |
| 622 | error << "invalid resource name for exported symbol in compiled file header: " |
| 623 | << pb_file.resource_name(); |
| 624 | *out_error = error.str(); |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | size_t line = 0u; |
| 629 | if (pb_symbol.has_source()) { |
| 630 | line = pb_symbol.source().line_number(); |
| 631 | } |
| 632 | out_file->exported_symbols.push_back(SourcedResourceName{name_ref.ToResourceName(), line}); |
| 633 | } |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | static Reference::Type DeserializeReferenceTypeFromPb(const pb::Reference_Type& pb_type) { |
| 638 | switch (pb_type) { |
| 639 | case pb::Reference_Type_REFERENCE: |
| 640 | return Reference::Type::kResource; |
| 641 | case pb::Reference_Type_ATTRIBUTE: |
| 642 | return Reference::Type::kAttribute; |
| 643 | default: |
| 644 | break; |
| 645 | } |
| 646 | return Reference::Type::kResource; |
| 647 | } |
| 648 | |
| 649 | static bool DeserializeReferenceFromPb(const pb::Reference& pb_ref, Reference* out_ref, |
| 650 | std::string* out_error) { |
| 651 | out_ref->reference_type = DeserializeReferenceTypeFromPb(pb_ref.type()); |
| 652 | out_ref->private_reference = pb_ref.private_(); |
| 653 | out_ref->is_dynamic = pb_ref.is_dynamic().value(); |
| 654 | |
| 655 | if (pb_ref.id() != 0) { |
| 656 | out_ref->id = ResourceId(pb_ref.id()); |
| 657 | } |
| 658 | |
| 659 | if (!pb_ref.name().empty()) { |
| 660 | ResourceNameRef name_ref; |
| 661 | if (!ResourceUtils::ParseResourceName(pb_ref.name(), &name_ref, nullptr)) { |
| 662 | std::ostringstream error; |
| 663 | error << "reference has invalid resource name '" << pb_ref.name() << "'"; |
| 664 | *out_error = error.str(); |
| 665 | return false; |
| 666 | } |
| 667 | out_ref->name = name_ref.ToResourceName(); |
| 668 | } |
| 669 | if (pb_ref.type_flags() != 0) { |
| 670 | out_ref->type_flags = pb_ref.type_flags(); |
| 671 | } |
| 672 | out_ref->allow_raw = pb_ref.allow_raw(); |
| 673 | return true; |
| 674 | } |
| 675 | |
| 676 | static bool DeserializeMacroFromPb(const pb::MacroBody& pb_ref, Macro* out_ref, |
| 677 | std::string* out_error) { |
| 678 | out_ref->raw_value = pb_ref.raw_string(); |
| 679 | |
| 680 | if (pb_ref.has_style_string()) { |
| 681 | out_ref->style_string.str = pb_ref.style_string().str(); |
| 682 | for (const auto& span : pb_ref.style_string().spans()) { |
| 683 | out_ref->style_string.spans.emplace_back(Span{ |
| 684 | .name = span.name(), .first_char = span.start_index(), .last_char = span.end_index()}); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | for (const auto& untranslatable_section : pb_ref.untranslatable_sections()) { |
| 689 | out_ref->untranslatable_sections.emplace_back( |
| 690 | UntranslatableSection{.start = static_cast<size_t>(untranslatable_section.start_index()), |
| 691 | .end = static_cast<size_t>(untranslatable_section.end_index())}); |
| 692 | } |
| 693 | |
| 694 | for (const auto& namespace_decls : pb_ref.namespace_stack()) { |
| 695 | out_ref->alias_namespaces.emplace_back( |
| 696 | Macro::Namespace{.alias = namespace_decls.prefix(), |
| 697 | .package_name = namespace_decls.package_name(), |
| 698 | .is_private = namespace_decls.is_private()}); |
| 699 | } |
| 700 | |
| 701 | return true; |
| 702 | } |
| 703 | |
| 704 | template <typename T> |
| 705 | static void DeserializeItemMetaDataFromPb(const T& pb_item, const android::ResStringPool& src_pool, |
| 706 | Value* out_value) { |
| 707 | if (pb_item.has_source()) { |
| 708 | Source source; |
| 709 | DeserializeSourceFromPb(pb_item.source(), src_pool, &source); |
| 710 | out_value->SetSource(std::move(source)); |
| 711 | } |
| 712 | out_value->SetComment(pb_item.comment()); |
| 713 | } |
| 714 | |
| 715 | static size_t DeserializePluralEnumFromPb(const pb::Plural_Arity& arity) { |
| 716 | switch (arity) { |
| 717 | case pb::Plural_Arity_ZERO: |
| 718 | return Plural::Zero; |
| 719 | case pb::Plural_Arity_ONE: |
| 720 | return Plural::One; |
| 721 | case pb::Plural_Arity_TWO: |
| 722 | return Plural::Two; |
| 723 | case pb::Plural_Arity_FEW: |
| 724 | return Plural::Few; |
| 725 | case pb::Plural_Arity_MANY: |
| 726 | return Plural::Many; |
| 727 | default: |
| 728 | break; |
| 729 | } |
| 730 | return Plural::Other; |
| 731 | } |
| 732 | |
| 733 | std::unique_ptr<Value> DeserializeValueFromPb(const pb::Value& pb_value, |
| 734 | const android::ResStringPool& src_pool, |
| 735 | const ConfigDescription& config, |
| 736 | StringPool* value_pool, io::IFileCollection* files, |
| 737 | std::string* out_error) { |
| 738 | std::unique_ptr<Value> value; |
| 739 | if (pb_value.has_item()) { |
| 740 | value = DeserializeItemFromPb(pb_value.item(), src_pool, config, value_pool, files, out_error); |
| 741 | if (value == nullptr) { |
| 742 | return {}; |
| 743 | } |
| 744 | |
| 745 | } else if (pb_value.has_compound_value()) { |
| 746 | const pb::CompoundValue& pb_compound_value = pb_value.compound_value(); |
| 747 | switch (pb_compound_value.value_case()) { |
| 748 | case pb::CompoundValue::kAttr: { |
| 749 | const pb::Attribute& pb_attr = pb_compound_value.attr(); |
| 750 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(pb_attr.format_flags()); |
| 751 | attr->min_int = pb_attr.min_int(); |
| 752 | attr->max_int = pb_attr.max_int(); |
| 753 | for (const pb::Attribute_Symbol& pb_symbol : pb_attr.symbol()) { |
| 754 | Attribute::Symbol symbol; |
| 755 | DeserializeItemMetaDataFromPb(pb_symbol, src_pool, &symbol.symbol); |
| 756 | if (!DeserializeReferenceFromPb(pb_symbol.name(), &symbol.symbol, out_error)) { |
| 757 | return {}; |
| 758 | } |
| 759 | symbol.value = pb_symbol.value(); |
| 760 | symbol.type = pb_symbol.type() != 0U ? pb_symbol.type() |
| 761 | : android::Res_value::TYPE_INT_DEC; |
| 762 | attr->symbols.push_back(std::move(symbol)); |
| 763 | } |
| 764 | value = std::move(attr); |
| 765 | } break; |
| 766 | |
| 767 | case pb::CompoundValue::kStyle: { |
| 768 | const pb::Style& pb_style = pb_compound_value.style(); |
| 769 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
| 770 | if (pb_style.has_parent()) { |
| 771 | style->parent = Reference(); |
| 772 | if (!DeserializeReferenceFromPb(pb_style.parent(), &style->parent.value(), out_error)) { |
| 773 | return {}; |
| 774 | } |
| 775 | |
| 776 | if (pb_style.has_parent_source()) { |
| 777 | Source parent_source; |
| 778 | DeserializeSourceFromPb(pb_style.parent_source(), src_pool, &parent_source); |
| 779 | style->parent.value().SetSource(std::move(parent_source)); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | for (const pb::Style_Entry& pb_entry : pb_style.entry()) { |
| 784 | Style::Entry entry; |
| 785 | if (!DeserializeReferenceFromPb(pb_entry.key(), &entry.key, out_error)) { |
| 786 | return {}; |
| 787 | } |
| 788 | DeserializeItemMetaDataFromPb(pb_entry, src_pool, &entry.key); |
| 789 | entry.value = DeserializeItemFromPb(pb_entry.item(), src_pool, config, value_pool, files, |
| 790 | out_error); |
| 791 | if (entry.value == nullptr) { |
| 792 | return {}; |
| 793 | } |
| 794 | |
| 795 | // Copy the meta-data into the value as well. |
| 796 | DeserializeItemMetaDataFromPb(pb_entry, src_pool, entry.value.get()); |
| 797 | style->entries.push_back(std::move(entry)); |
| 798 | } |
| 799 | value = std::move(style); |
| 800 | } break; |
| 801 | |
| 802 | case pb::CompoundValue::kStyleable: { |
| 803 | const pb::Styleable& pb_styleable = pb_compound_value.styleable(); |
| 804 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 805 | for (const pb::Styleable_Entry& pb_entry : pb_styleable.entry()) { |
| 806 | Reference attr_ref; |
| 807 | DeserializeItemMetaDataFromPb(pb_entry, src_pool, &attr_ref); |
| 808 | DeserializeReferenceFromPb(pb_entry.attr(), &attr_ref, out_error); |
| 809 | styleable->entries.push_back(std::move(attr_ref)); |
| 810 | } |
| 811 | value = std::move(styleable); |
| 812 | } break; |
| 813 | |
| 814 | case pb::CompoundValue::kArray: { |
| 815 | const pb::Array& pb_array = pb_compound_value.array(); |
| 816 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
| 817 | for (const pb::Array_Element& pb_entry : pb_array.element()) { |
| 818 | std::unique_ptr<Item> item = DeserializeItemFromPb(pb_entry.item(), src_pool, config, |
| 819 | value_pool, files, out_error); |
| 820 | if (item == nullptr) { |
| 821 | return {}; |
| 822 | } |
| 823 | |
| 824 | DeserializeItemMetaDataFromPb(pb_entry, src_pool, item.get()); |
| 825 | array->elements.push_back(std::move(item)); |
| 826 | } |
| 827 | value = std::move(array); |
| 828 | } break; |
| 829 | |
| 830 | case pb::CompoundValue::kPlural: { |
| 831 | const pb::Plural& pb_plural = pb_compound_value.plural(); |
| 832 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
| 833 | for (const pb::Plural_Entry& pb_entry : pb_plural.entry()) { |
| 834 | size_t plural_idx = DeserializePluralEnumFromPb(pb_entry.arity()); |
| 835 | plural->values[plural_idx] = DeserializeItemFromPb(pb_entry.item(), src_pool, config, |
| 836 | value_pool, files, out_error); |
| 837 | if (!plural->values[plural_idx]) { |
| 838 | return {}; |
| 839 | } |
| 840 | |
| 841 | DeserializeItemMetaDataFromPb(pb_entry, src_pool, plural->values[plural_idx].get()); |
| 842 | } |
| 843 | value = std::move(plural); |
| 844 | } break; |
| 845 | |
| 846 | case pb::CompoundValue::kMacro: { |
| 847 | const pb::MacroBody& pb_macro = pb_compound_value.macro(); |
| 848 | auto macro = std::make_unique<Macro>(); |
| 849 | if (!DeserializeMacroFromPb(pb_macro, macro.get(), out_error)) { |
| 850 | return {}; |
| 851 | } |
| 852 | value = std::move(macro); |
| 853 | } break; |
| 854 | |
| 855 | default: |
| 856 | LOG(FATAL) << "unknown compound value: " << (int)pb_compound_value.value_case(); |
| 857 | break; |
| 858 | } |
| 859 | } else { |
| 860 | LOG(FATAL) << "unknown value: " << (int)pb_value.value_case(); |
| 861 | return {}; |
| 862 | } |
| 863 | |
| 864 | CHECK(value) << "forgot to set value"; |
| 865 | |
| 866 | value->SetWeak(pb_value.weak()); |
| 867 | DeserializeItemMetaDataFromPb(pb_value, src_pool, value.get()); |
| 868 | return value; |
| 869 | } |
| 870 | |
| 871 | std::unique_ptr<Item> DeserializeItemFromPb(const pb::Item& pb_item, |
| 872 | const android::ResStringPool& src_pool, |
| 873 | const ConfigDescription& config, StringPool* value_pool, |
| 874 | io::IFileCollection* files, std::string* out_error) { |
| 875 | switch (pb_item.value_case()) { |
| 876 | case pb::Item::kRef: { |
| 877 | const pb::Reference& pb_ref = pb_item.ref(); |
| 878 | std::unique_ptr<Reference> ref = util::make_unique<Reference>(); |
| 879 | if (!DeserializeReferenceFromPb(pb_ref, ref.get(), out_error)) { |
| 880 | return {}; |
| 881 | } |
| 882 | return std::move(ref); |
| 883 | } break; |
| 884 | |
| 885 | case pb::Item::kPrim: { |
| 886 | const pb::Primitive& pb_prim = pb_item.prim(); |
| 887 | android::Res_value val = {}; |
| 888 | switch (pb_prim.oneof_value_case()) { |
| 889 | case pb::Primitive::kNullValue: { |
| 890 | val.dataType = android::Res_value::TYPE_NULL; |
| 891 | val.data = android::Res_value::DATA_NULL_UNDEFINED; |
| 892 | } break; |
| 893 | case pb::Primitive::kEmptyValue: { |
| 894 | val.dataType = android::Res_value::TYPE_NULL; |
| 895 | val.data = android::Res_value::DATA_NULL_EMPTY; |
| 896 | } break; |
| 897 | case pb::Primitive::kFloatValue: { |
| 898 | val.dataType = android::Res_value::TYPE_FLOAT; |
| 899 | float float_val = pb_prim.float_value(); |
| 900 | val.data = *(uint32_t*)&float_val; |
| 901 | } break; |
| 902 | case pb::Primitive::kDimensionValue: { |
| 903 | val.dataType = android::Res_value::TYPE_DIMENSION; |
| 904 | val.data = pb_prim.dimension_value(); |
| 905 | } break; |
| 906 | case pb::Primitive::kFractionValue: { |
| 907 | val.dataType = android::Res_value::TYPE_FRACTION; |
| 908 | val.data = pb_prim.fraction_value(); |
| 909 | } break; |
| 910 | case pb::Primitive::kIntDecimalValue: { |
| 911 | val.dataType = android::Res_value::TYPE_INT_DEC; |
| 912 | val.data = static_cast<uint32_t>(pb_prim.int_decimal_value()); |
| 913 | } break; |
| 914 | case pb::Primitive::kIntHexadecimalValue: { |
| 915 | val.dataType = android::Res_value::TYPE_INT_HEX; |
| 916 | val.data = pb_prim.int_hexadecimal_value(); |
| 917 | } break; |
| 918 | case pb::Primitive::kBooleanValue: { |
| 919 | val.dataType = android::Res_value::TYPE_INT_BOOLEAN; |
| 920 | val.data = pb_prim.boolean_value() ? 0xFFFFFFFF : 0x0; |
| 921 | } break; |
| 922 | case pb::Primitive::kColorArgb8Value: { |
| 923 | val.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8; |
| 924 | val.data = pb_prim.color_argb8_value(); |
| 925 | } break; |
| 926 | case pb::Primitive::kColorRgb8Value: { |
| 927 | val.dataType = android::Res_value::TYPE_INT_COLOR_RGB8; |
| 928 | val.data = pb_prim.color_rgb8_value(); |
| 929 | } break; |
| 930 | case pb::Primitive::kColorArgb4Value: { |
| 931 | val.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4; |
| 932 | val.data = pb_prim.color_argb4_value(); |
| 933 | } break; |
| 934 | case pb::Primitive::kColorRgb4Value: { |
| 935 | val.dataType = android::Res_value::TYPE_INT_COLOR_RGB4; |
| 936 | val.data = pb_prim.color_rgb4_value(); |
| 937 | } break; |
| 938 | case pb::Primitive::kDimensionValueDeprecated: { // DEPRECATED |
| 939 | val.dataType = android::Res_value::TYPE_DIMENSION; |
| 940 | float dimen_val = pb_prim.dimension_value_deprecated(); |
| 941 | val.data = *(uint32_t*)&dimen_val; |
| 942 | } break; |
| 943 | case pb::Primitive::kFractionValueDeprecated: { // DEPRECATED |
| 944 | val.dataType = android::Res_value::TYPE_FRACTION; |
| 945 | float fraction_val = pb_prim.fraction_value_deprecated(); |
| 946 | val.data = *(uint32_t*)&fraction_val; |
| 947 | } break; |
| 948 | default: { |
| 949 | LOG(FATAL) << "Unexpected Primitive type: " |
| 950 | << static_cast<uint32_t>(pb_prim.oneof_value_case()); |
| 951 | return {}; |
| 952 | } break; |
| 953 | } |
| 954 | return util::make_unique<BinaryPrimitive>(val); |
| 955 | } break; |
| 956 | |
| 957 | case pb::Item::kId: { |
| 958 | return util::make_unique<Id>(); |
| 959 | } break; |
| 960 | |
| 961 | case pb::Item::kStr: { |
| 962 | return util::make_unique<String>( |
| 963 | value_pool->MakeRef(pb_item.str().value(), StringPool::Context(config))); |
| 964 | } break; |
| 965 | |
| 966 | case pb::Item::kRawStr: { |
| 967 | return util::make_unique<RawString>( |
| 968 | value_pool->MakeRef(pb_item.raw_str().value(), StringPool::Context(config))); |
| 969 | } break; |
| 970 | |
| 971 | case pb::Item::kStyledStr: { |
| 972 | const pb::StyledString& pb_str = pb_item.styled_str(); |
| 973 | StyleString style_str{pb_str.value()}; |
| 974 | for (const pb::StyledString::Span& pb_span : pb_str.span()) { |
| 975 | style_str.spans.push_back(Span{pb_span.tag(), pb_span.first_char(), pb_span.last_char()}); |
| 976 | } |
| 977 | return util::make_unique<StyledString>(value_pool->MakeRef( |
| 978 | style_str, StringPool::Context(StringPool::Context::kNormalPriority, config))); |
| 979 | } break; |
| 980 | |
| 981 | case pb::Item::kFile: { |
| 982 | const pb::FileReference& pb_file = pb_item.file(); |
| 983 | std::unique_ptr<FileReference> file_ref = |
| 984 | util::make_unique<FileReference>(value_pool->MakeRef( |
| 985 | pb_file.path(), StringPool::Context(StringPool::Context::kHighPriority, config))); |
| 986 | file_ref->type = DeserializeFileReferenceTypeFromPb(pb_file.type()); |
| 987 | if (files != nullptr) { |
| 988 | file_ref->file = files->FindFile(*file_ref->path); |
| 989 | } |
| 990 | return std::move(file_ref); |
| 991 | } break; |
| 992 | |
| 993 | default: |
| 994 | LOG(FATAL) << "unknown item: " << (int)pb_item.value_case(); |
| 995 | break; |
| 996 | } |
| 997 | return {}; |
| 998 | } |
| 999 | |
| 1000 | std::unique_ptr<xml::XmlResource> DeserializeXmlResourceFromPb(const pb::XmlNode& pb_node, |
| 1001 | std::string* out_error) { |
| 1002 | if (!pb_node.has_element()) { |
| 1003 | return {}; |
| 1004 | } |
| 1005 | |
| 1006 | std::unique_ptr<xml::XmlResource> resource = util::make_unique<xml::XmlResource>(); |
| 1007 | resource->root = util::make_unique<xml::Element>(); |
| 1008 | if (!DeserializeXmlFromPb(pb_node, resource->root.get(), &resource->string_pool, out_error)) { |
| 1009 | return {}; |
| 1010 | } |
| 1011 | return resource; |
| 1012 | } |
| 1013 | |
| 1014 | bool DeserializeXmlFromPb(const pb::XmlNode& pb_node, xml::Element* out_el, StringPool* value_pool, |
| 1015 | std::string* out_error) { |
| 1016 | const pb::XmlElement& pb_el = pb_node.element(); |
| 1017 | out_el->name = pb_el.name(); |
| 1018 | out_el->namespace_uri = pb_el.namespace_uri(); |
| 1019 | out_el->line_number = pb_node.source().line_number(); |
| 1020 | out_el->column_number = pb_node.source().column_number(); |
| 1021 | |
| 1022 | for (const pb::XmlNamespace& pb_ns : pb_el.namespace_declaration()) { |
| 1023 | xml::NamespaceDecl decl; |
| 1024 | decl.uri = pb_ns.uri(); |
| 1025 | decl.prefix = pb_ns.prefix(); |
| 1026 | decl.line_number = pb_ns.source().line_number(); |
| 1027 | decl.column_number = pb_ns.source().column_number(); |
| 1028 | out_el->namespace_decls.push_back(std::move(decl)); |
| 1029 | } |
| 1030 | |
| 1031 | for (const pb::XmlAttribute& pb_attr : pb_el.attribute()) { |
| 1032 | xml::Attribute attr; |
| 1033 | attr.name = pb_attr.name(); |
| 1034 | attr.namespace_uri = pb_attr.namespace_uri(); |
| 1035 | attr.value = pb_attr.value(); |
| 1036 | if (pb_attr.resource_id() != 0u) { |
| 1037 | attr.compiled_attribute = xml::AaptAttribute{Attribute(), ResourceId(pb_attr.resource_id())}; |
| 1038 | } |
| 1039 | if (pb_attr.has_compiled_item()) { |
| 1040 | attr.compiled_value = |
| 1041 | DeserializeItemFromPb(pb_attr.compiled_item(), {}, {}, value_pool, nullptr, out_error); |
| 1042 | if (attr.compiled_value == nullptr) { |
| 1043 | return {}; |
| 1044 | } |
| 1045 | attr.compiled_value->SetSource(Source().WithLine(pb_attr.source().line_number())); |
| 1046 | } |
| 1047 | out_el->attributes.push_back(std::move(attr)); |
| 1048 | } |
| 1049 | |
| 1050 | // Deserialize the children. |
| 1051 | for (const pb::XmlNode& pb_child : pb_el.child()) { |
| 1052 | switch (pb_child.node_case()) { |
| 1053 | case pb::XmlNode::NodeCase::kText: { |
| 1054 | std::unique_ptr<xml::Text> text = util::make_unique<xml::Text>(); |
| 1055 | text->line_number = pb_child.source().line_number(); |
| 1056 | text->column_number = pb_child.source().column_number(); |
| 1057 | text->text = pb_child.text(); |
| 1058 | out_el->AppendChild(std::move(text)); |
| 1059 | } break; |
| 1060 | |
| 1061 | case pb::XmlNode::NodeCase::kElement: { |
| 1062 | std::unique_ptr<xml::Element> child_el = util::make_unique<xml::Element>(); |
| 1063 | if (!DeserializeXmlFromPb(pb_child, child_el.get(), value_pool, out_error)) { |
| 1064 | return false; |
| 1065 | } |
| 1066 | out_el->AppendChild(std::move(child_el)); |
| 1067 | } break; |
| 1068 | |
| 1069 | default: |
| 1070 | LOG(FATAL) << "unknown XmlNode " << (int)pb_child.node_case(); |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
| 1077 | } // namespace aapt |