blob: aa99c982f6aec3043af850f3569b15a24a2ebe49 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
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
19namespace aapt {
20
Adam Lesinski060b53d2017-07-28 17:10:35 -070021void SerializeStringPoolToPb(const StringPool& pool, pb::StringPool* out_pb_pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022 BigBuffer buffer(1024);
23 StringPool::FlattenUtf8(&buffer, pool);
Adam Lesinski59e04c62016-02-04 15:59:23 -080024
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 std::string* data = out_pb_pool->mutable_data();
26 data->reserve(buffer.size());
Adam Lesinski59e04c62016-02-04 15:59:23 -080027
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 size_t offset = 0;
29 for (const BigBuffer::Block& block : buffer) {
Adam Lesinski060b53d2017-07-28 17:10:35 -070030 data->insert(data->begin() + offset, block.buffer.get(), block.buffer.get() + block.size);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 offset += block.size;
32 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080033}
34
Adam Lesinski060b53d2017-07-28 17:10:35 -070035void SerializeSourceToPb(const Source& source, StringPool* src_pool, pb::Source* out_pb_source) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 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) {
Adam Lesinski4ffea042017-08-10 15:37:28 -070039 out_pb_source->mutable_position()->set_line_number(static_cast<uint32_t>(source.line.value()));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080041}
42
Adam Lesinski060b53d2017-07-28 17:10:35 -070043void DeserializeSourceFromPb(const pb::Source& pb_source, const android::ResStringPool& src_pool,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 Source* out_source) {
45 if (pb_source.has_path_idx()) {
46 out_source->path = util::GetString(src_pool, pb_source.path_idx());
47 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080048
Adam Lesinski4ffea042017-08-10 15:37:28 -070049 if (pb_source.has_position()) {
50 out_source->line = static_cast<size_t>(pb_source.position().line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080052}
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054pb::SymbolStatus_Visibility SerializeVisibilityToPb(SymbolState state) {
55 switch (state) {
56 case SymbolState::kPrivate:
Adam Lesinski4ffea042017-08-10 15:37:28 -070057 return pb::SymbolStatus_Visibility_PRIVATE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 case SymbolState::kPublic:
Adam Lesinski4ffea042017-08-10 15:37:28 -070059 return pb::SymbolStatus_Visibility_PUBLIC;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 default:
61 break;
62 }
Adam Lesinski4ffea042017-08-10 15:37:28 -070063 return pb::SymbolStatus_Visibility_UNKNOWN;
Adam Lesinski59e04c62016-02-04 15:59:23 -080064}
65
Adam Lesinski4ffea042017-08-10 15:37:28 -070066SymbolState DeserializeVisibilityFromPb(pb::SymbolStatus_Visibility pb_visibility) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 switch (pb_visibility) {
Adam Lesinski4ffea042017-08-10 15:37:28 -070068 case pb::SymbolStatus_Visibility_PRIVATE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 return SymbolState::kPrivate;
Adam Lesinski4ffea042017-08-10 15:37:28 -070070 case pb::SymbolStatus_Visibility_PUBLIC:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 return SymbolState::kPublic;
72 default:
73 break;
74 }
75 return SymbolState::kUndefined;
Adam Lesinski59e04c62016-02-04 15:59:23 -080076}
77
Adam Lesinski060b53d2017-07-28 17:10:35 -070078void SerializeConfig(const ConfigDescription& config, pb::ConfigDescription* out_pb_config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 android::ResTable_config flat_config = config;
80 flat_config.size = sizeof(flat_config);
81 flat_config.swapHtoD();
82 out_pb_config->set_data(&flat_config, sizeof(flat_config));
Adam Lesinski59e04c62016-02-04 15:59:23 -080083}
84
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
86 ConfigDescription* out_config) {
87 if (!pb_config.has_data()) {
88 return false;
89 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 const android::ResTable_config* config;
92 if (pb_config.data().size() > sizeof(*config)) {
93 return false;
94 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080095
Adam Lesinski060b53d2017-07-28 17:10:35 -070096 config = reinterpret_cast<const android::ResTable_config*>(pb_config.data().data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 out_config->copyFromDtoH(*config);
98 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -080099}
100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101pb::Reference_Type SerializeReferenceTypeToPb(Reference::Type type) {
102 switch (type) {
103 case Reference::Type::kResource:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700104 return pb::Reference_Type_REFERENCE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 case Reference::Type::kAttribute:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700106 return pb::Reference_Type_ATTRIBUTE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 default:
108 break;
109 }
Adam Lesinski4ffea042017-08-10 15:37:28 -0700110 return pb::Reference_Type_REFERENCE;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800111}
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113Reference::Type DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type) {
114 switch (pb_type) {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700115 case pb::Reference_Type_REFERENCE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 return Reference::Type::kResource;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700117 case pb::Reference_Type_ATTRIBUTE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 return Reference::Type::kAttribute;
119 default:
120 break;
121 }
122 return Reference::Type::kResource;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800123}
124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125pb::Plural_Arity SerializePluralEnumToPb(size_t plural_idx) {
126 switch (plural_idx) {
127 case Plural::Zero:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700128 return pb::Plural_Arity_ZERO;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 case Plural::One:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700130 return pb::Plural_Arity_ONE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 case Plural::Two:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700132 return pb::Plural_Arity_TWO;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 case Plural::Few:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700134 return pb::Plural_Arity_FEW;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 case Plural::Many:
Adam Lesinski4ffea042017-08-10 15:37:28 -0700136 return pb::Plural_Arity_MANY;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 default:
138 break;
139 }
Adam Lesinski4ffea042017-08-10 15:37:28 -0700140 return pb::Plural_Arity_OTHER;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143size_t DeserializePluralEnumFromPb(pb::Plural_Arity arity) {
144 switch (arity) {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700145 case pb::Plural_Arity_ZERO:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 return Plural::Zero;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700147 case pb::Plural_Arity_ONE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 return Plural::One;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700149 case pb::Plural_Arity_TWO:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 return Plural::Two;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700151 case pb::Plural_Arity_FEW:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 return Plural::Few;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700153 case pb::Plural_Arity_MANY:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 return Plural::Many;
155 default:
156 break;
157 }
158 return Plural::Other;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800159}
160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161} // namespace aapt