Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 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 "proto/ProtoHelpers.h" |
| 18 | |
| 19 | namespace aapt { |
| 20 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 21 | void SerializeStringPoolToPb(const StringPool& pool, pb::StringPool* out_pb_pool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | BigBuffer buffer(1024); |
| 23 | StringPool::FlattenUtf8(&buffer, pool); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 24 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | std::string* data = out_pb_pool->mutable_data(); |
| 26 | data->reserve(buffer.size()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | size_t offset = 0; |
| 29 | for (const BigBuffer::Block& block : buffer) { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 30 | data->insert(data->begin() + offset, block.buffer.get(), block.buffer.get() + block.size); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | offset += block.size; |
| 32 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 35 | void SerializeSourceToPb(const Source& source, StringPool* src_pool, pb::Source* out_pb_source) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | StringPool::Ref ref = src_pool->MakeRef(source.path); |
| 37 | out_pb_source->set_path_idx(static_cast<uint32_t>(ref.index())); |
| 38 | if (source.line) { |
| 39 | out_pb_source->set_line_no(static_cast<uint32_t>(source.line.value())); |
| 40 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 43 | void DeserializeSourceFromPb(const pb::Source& pb_source, const android::ResStringPool& src_pool, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | Source* out_source) { |
| 45 | if (pb_source.has_path_idx()) { |
| 46 | out_source->path = util::GetString(src_pool, pb_source.path_idx()); |
| 47 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | if (pb_source.has_line_no()) { |
| 50 | out_source->line = static_cast<size_t>(pb_source.line_no()); |
| 51 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | pb::SymbolStatus_Visibility SerializeVisibilityToPb(SymbolState state) { |
| 55 | switch (state) { |
| 56 | case SymbolState::kPrivate: |
| 57 | return pb::SymbolStatus_Visibility_Private; |
| 58 | case SymbolState::kPublic: |
| 59 | return pb::SymbolStatus_Visibility_Public; |
| 60 | default: |
| 61 | break; |
| 62 | } |
| 63 | return pb::SymbolStatus_Visibility_Unknown; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | SymbolState DeserializeVisibilityFromPb( |
| 67 | pb::SymbolStatus_Visibility pb_visibility) { |
| 68 | switch (pb_visibility) { |
| 69 | case pb::SymbolStatus_Visibility_Private: |
| 70 | return SymbolState::kPrivate; |
| 71 | case pb::SymbolStatus_Visibility_Public: |
| 72 | return SymbolState::kPublic; |
| 73 | default: |
| 74 | break; |
| 75 | } |
| 76 | return SymbolState::kUndefined; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 79 | void SerializeConfig(const ConfigDescription& config, pb::ConfigDescription* out_pb_config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | android::ResTable_config flat_config = config; |
| 81 | flat_config.size = sizeof(flat_config); |
| 82 | flat_config.swapHtoD(); |
| 83 | out_pb_config->set_data(&flat_config, sizeof(flat_config)); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config, |
| 87 | ConfigDescription* out_config) { |
| 88 | if (!pb_config.has_data()) { |
| 89 | return false; |
| 90 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | const android::ResTable_config* config; |
| 93 | if (pb_config.data().size() > sizeof(*config)) { |
| 94 | return false; |
| 95 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 97 | config = reinterpret_cast<const android::ResTable_config*>(pb_config.data().data()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | out_config->copyFromDtoH(*config); |
| 99 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | pb::Reference_Type SerializeReferenceTypeToPb(Reference::Type type) { |
| 103 | switch (type) { |
| 104 | case Reference::Type::kResource: |
| 105 | return pb::Reference_Type_Ref; |
| 106 | case Reference::Type::kAttribute: |
| 107 | return pb::Reference_Type_Attr; |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | return pb::Reference_Type_Ref; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | Reference::Type DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type) { |
| 115 | switch (pb_type) { |
| 116 | case pb::Reference_Type_Ref: |
| 117 | return Reference::Type::kResource; |
| 118 | case pb::Reference_Type_Attr: |
| 119 | return Reference::Type::kAttribute; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | return Reference::Type::kResource; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | pb::Plural_Arity SerializePluralEnumToPb(size_t plural_idx) { |
| 127 | switch (plural_idx) { |
| 128 | case Plural::Zero: |
| 129 | return pb::Plural_Arity_Zero; |
| 130 | case Plural::One: |
| 131 | return pb::Plural_Arity_One; |
| 132 | case Plural::Two: |
| 133 | return pb::Plural_Arity_Two; |
| 134 | case Plural::Few: |
| 135 | return pb::Plural_Arity_Few; |
| 136 | case Plural::Many: |
| 137 | return pb::Plural_Arity_Many; |
| 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | return pb::Plural_Arity_Other; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | size_t DeserializePluralEnumFromPb(pb::Plural_Arity arity) { |
| 145 | switch (arity) { |
| 146 | case pb::Plural_Arity_Zero: |
| 147 | return Plural::Zero; |
| 148 | case pb::Plural_Arity_One: |
| 149 | return Plural::One; |
| 150 | case pb::Plural_Arity_Two: |
| 151 | return Plural::Two; |
| 152 | case pb::Plural_Arity_Few: |
| 153 | return Plural::Few; |
| 154 | case pb::Plural_Arity_Many: |
| 155 | return Plural::Many; |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | return Plural::Other; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | } // namespace aapt |