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,
22                              pb::StringPool* out_pb_pool) {
23   BigBuffer buffer(1024);
24   StringPool::FlattenUtf8(&buffer, pool);
25 
26   std::string* data = out_pb_pool->mutable_data();
27   data->reserve(buffer.size());
28 
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   }
35 }
36 
SerializeSourceToPb(const Source & source,StringPool * src_pool,pb::Source * out_pb_source)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   }
44 }
45 
DeserializeSourceFromPb(const pb::Source & pb_source,const android::ResStringPool & src_pool,Source * out_source)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   }
52 
53   if (pb_source.has_line_no()) {
54     out_source->line = static_cast<size_t>(pb_source.line_no());
55   }
56 }
57 
SerializeVisibilityToPb(SymbolState state)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;
68 }
69 
DeserializeVisibilityFromPb(pb::SymbolStatus_Visibility pb_visibility)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;
81 }
82 
SerializeConfig(const ConfigDescription & config,pb::ConfigDescription * out_pb_config)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));
89 }
90 
DeserializeConfigDescriptionFromPb(const pb::ConfigDescription & pb_config,ConfigDescription * out_config)91 bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
92                                         ConfigDescription* out_config) {
93   if (!pb_config.has_data()) {
94     return false;
95   }
96 
97   const android::ResTable_config* config;
98   if (pb_config.data().size() > sizeof(*config)) {
99     return false;
100   }
101 
102   config = reinterpret_cast<const android::ResTable_config*>(
103       pb_config.data().data());
104   out_config->copyFromDtoH(*config);
105   return true;
106 }
107 
SerializeReferenceTypeToPb(Reference::Type type)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;
118 }
119 
DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type)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;
130 }
131 
SerializePluralEnumToPb(size_t plural_idx)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;
148 }
149 
DeserializePluralEnumFromPb(pb::Plural_Arity arity)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;
166 }
167 
168 }  // namespace aapt
169