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
SerializeStringPoolToPb(const StringPool & pool,pb::StringPool * out_pb_pool)21 void SerializeStringPoolToPb(const StringPool& pool, pb::StringPool* out_pb_pool) {
22 BigBuffer buffer(1024);
23 StringPool::FlattenUtf8(&buffer, pool);
24
25 std::string* data = out_pb_pool->mutable_data();
26 data->reserve(buffer.size());
27
28 size_t offset = 0;
29 for (const BigBuffer::Block& block : buffer) {
30 data->insert(data->begin() + offset, block.buffer.get(), block.buffer.get() + block.size);
31 offset += block.size;
32 }
33 }
34
SerializeSourceToPb(const Source & source,StringPool * src_pool,pb::Source * out_pb_source)35 void SerializeSourceToPb(const Source& source, StringPool* src_pool, pb::Source* out_pb_source) {
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->mutable_position()->set_line_number(static_cast<uint32_t>(source.line.value()));
40 }
41 }
42
DeserializeSourceFromPb(const pb::Source & pb_source,const android::ResStringPool & src_pool,Source * out_source)43 void DeserializeSourceFromPb(const pb::Source& pb_source, const android::ResStringPool& src_pool,
44 Source* out_source) {
45 if (pb_source.has_path_idx()) {
46 out_source->path = util::GetString(src_pool, pb_source.path_idx());
47 }
48
49 if (pb_source.has_position()) {
50 out_source->line = static_cast<size_t>(pb_source.position().line_number());
51 }
52 }
53
SerializeVisibilityToPb(SymbolState state)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;
64 }
65
DeserializeVisibilityFromPb(pb::SymbolStatus_Visibility pb_visibility)66 SymbolState DeserializeVisibilityFromPb(pb::SymbolStatus_Visibility pb_visibility) {
67 switch (pb_visibility) {
68 case pb::SymbolStatus_Visibility_PRIVATE:
69 return SymbolState::kPrivate;
70 case pb::SymbolStatus_Visibility_PUBLIC:
71 return SymbolState::kPublic;
72 default:
73 break;
74 }
75 return SymbolState::kUndefined;
76 }
77
SerializeConfig(const ConfigDescription & config,pb::ConfigDescription * out_pb_config)78 void SerializeConfig(const ConfigDescription& config, pb::ConfigDescription* out_pb_config) {
79 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));
83 }
84
DeserializeConfigDescriptionFromPb(const pb::ConfigDescription & pb_config,ConfigDescription * out_config)85 bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
86 ConfigDescription* out_config) {
87 if (!pb_config.has_data()) {
88 return false;
89 }
90
91 const android::ResTable_config* config;
92 if (pb_config.data().size() > sizeof(*config)) {
93 return false;
94 }
95
96 config = reinterpret_cast<const android::ResTable_config*>(pb_config.data().data());
97 out_config->copyFromDtoH(*config);
98 return true;
99 }
100
SerializeReferenceTypeToPb(Reference::Type type)101 pb::Reference_Type SerializeReferenceTypeToPb(Reference::Type type) {
102 switch (type) {
103 case Reference::Type::kResource:
104 return pb::Reference_Type_REFERENCE;
105 case Reference::Type::kAttribute:
106 return pb::Reference_Type_ATTRIBUTE;
107 default:
108 break;
109 }
110 return pb::Reference_Type_REFERENCE;
111 }
112
DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type)113 Reference::Type DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type) {
114 switch (pb_type) {
115 case pb::Reference_Type_REFERENCE:
116 return Reference::Type::kResource;
117 case pb::Reference_Type_ATTRIBUTE:
118 return Reference::Type::kAttribute;
119 default:
120 break;
121 }
122 return Reference::Type::kResource;
123 }
124
SerializePluralEnumToPb(size_t plural_idx)125 pb::Plural_Arity SerializePluralEnumToPb(size_t plural_idx) {
126 switch (plural_idx) {
127 case Plural::Zero:
128 return pb::Plural_Arity_ZERO;
129 case Plural::One:
130 return pb::Plural_Arity_ONE;
131 case Plural::Two:
132 return pb::Plural_Arity_TWO;
133 case Plural::Few:
134 return pb::Plural_Arity_FEW;
135 case Plural::Many:
136 return pb::Plural_Arity_MANY;
137 default:
138 break;
139 }
140 return pb::Plural_Arity_OTHER;
141 }
142
DeserializePluralEnumFromPb(pb::Plural_Arity arity)143 size_t DeserializePluralEnumFromPb(pb::Plural_Arity arity) {
144 switch (arity) {
145 case pb::Plural_Arity_ZERO:
146 return Plural::Zero;
147 case pb::Plural_Arity_ONE:
148 return Plural::One;
149 case pb::Plural_Arity_TWO:
150 return Plural::Two;
151 case pb::Plural_Arity_FEW:
152 return Plural::Few;
153 case pb::Plural_Arity_MANY:
154 return Plural::Many;
155 default:
156 break;
157 }
158 return Plural::Other;
159 }
160
161 } // namespace aapt
162