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