blob: 38bf4e3bd8eb9f1262581e725834d164d452274b [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 Lesinskice5e56e2016-10-21 17:56:45 -070021void SerializeStringPoolToPb(const StringPool& pool,
22 pb::StringPool* out_pb_pool) {
23 BigBuffer buffer(1024);
24 StringPool::FlattenUtf8(&buffer, pool);
Adam Lesinski59e04c62016-02-04 15:59:23 -080025
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026 std::string* data = out_pb_pool->mutable_data();
27 data->reserve(buffer.size());
Adam Lesinski59e04c62016-02-04 15:59:23 -080028
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 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 Lesinski59e04c62016-02-04 15:59:23 -080035}
36
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037void 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 Lesinski59e04c62016-02-04 15:59:23 -080044}
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046void 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 Lesinski59e04c62016-02-04 15:59:23 -080052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 if (pb_source.has_line_no()) {
54 out_source->line = static_cast<size_t>(pb_source.line_no());
55 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080056}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058pb::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 Lesinski59e04c62016-02-04 15:59:23 -080068}
69
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070SymbolState 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 Lesinski59e04c62016-02-04 15:59:23 -080081}
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083void 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 Lesinski59e04c62016-02-04 15:59:23 -080089}
90
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
92 ConfigDescription* out_config) {
93 if (!pb_config.has_data()) {
94 return false;
95 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 const android::ResTable_config* config;
98 if (pb_config.data().size() > sizeof(*config)) {
99 return false;
100 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 config = reinterpret_cast<const android::ResTable_config*>(
103 pb_config.data().data());
104 out_config->copyFromDtoH(*config);
105 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800106}
107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108pb::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 Lesinski59e04c62016-02-04 15:59:23 -0800118}
119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120Reference::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 Lesinski59e04c62016-02-04 15:59:23 -0800130}
131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132pb::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 Lesinski59e04c62016-02-04 15:59:23 -0800148}
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150size_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 Lesinski59e04c62016-02-04 15:59:23 -0800166}
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168} // namespace aapt