blob: 6b21364b5eb27ffac7955cee292a16aa22cc1731 [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 Lesinski5b6ee112017-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 Lesinski5b6ee112017-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 Lesinski5b6ee112017-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) {
39 out_pb_source->set_line_no(static_cast<uint32_t>(source.line.value()));
40 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080041}
42
Adam Lesinski5b6ee112017-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 Lesinskice5e56e2016-10-21 17:56:45 -070049 if (pb_source.has_line_no()) {
50 out_source->line = static_cast<size_t>(pb_source.line_no());
51 }
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:
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;
Adam Lesinski59e04c62016-02-04 15:59:23 -080064}
65
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066SymbolState DeserializeVisibilityFromPb(
67 pb::SymbolStatus_Visibility pb_visibility) {
68 switch (pb_visibility) {
69 case pb::SymbolStatus_Visibility_Private:
70 return SymbolState::kPrivate;
71 case pb::SymbolStatus_Visibility_Public:
72 return SymbolState::kPublic;
73 default:
74 break;
75 }
76 return SymbolState::kUndefined;
Adam Lesinski59e04c62016-02-04 15:59:23 -080077}
78
Adam Lesinski5b6ee112017-07-28 17:10:35 -070079void SerializeConfig(const ConfigDescription& config, pb::ConfigDescription* out_pb_config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 android::ResTable_config flat_config = config;
81 flat_config.size = sizeof(flat_config);
82 flat_config.swapHtoD();
83 out_pb_config->set_data(&flat_config, sizeof(flat_config));
Adam Lesinski59e04c62016-02-04 15:59:23 -080084}
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
87 ConfigDescription* out_config) {
88 if (!pb_config.has_data()) {
89 return false;
90 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 const android::ResTable_config* config;
93 if (pb_config.data().size() > sizeof(*config)) {
94 return false;
95 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080096
Adam Lesinski5b6ee112017-07-28 17:10:35 -070097 config = reinterpret_cast<const android::ResTable_config*>(pb_config.data().data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 out_config->copyFromDtoH(*config);
99 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102pb::Reference_Type SerializeReferenceTypeToPb(Reference::Type type) {
103 switch (type) {
104 case Reference::Type::kResource:
105 return pb::Reference_Type_Ref;
106 case Reference::Type::kAttribute:
107 return pb::Reference_Type_Attr;
108 default:
109 break;
110 }
111 return pb::Reference_Type_Ref;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800112}
113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114Reference::Type DeserializeReferenceTypeFromPb(pb::Reference_Type pb_type) {
115 switch (pb_type) {
116 case pb::Reference_Type_Ref:
117 return Reference::Type::kResource;
118 case pb::Reference_Type_Attr:
119 return Reference::Type::kAttribute;
120 default:
121 break;
122 }
123 return Reference::Type::kResource;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800124}
125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126pb::Plural_Arity SerializePluralEnumToPb(size_t plural_idx) {
127 switch (plural_idx) {
128 case Plural::Zero:
129 return pb::Plural_Arity_Zero;
130 case Plural::One:
131 return pb::Plural_Arity_One;
132 case Plural::Two:
133 return pb::Plural_Arity_Two;
134 case Plural::Few:
135 return pb::Plural_Arity_Few;
136 case Plural::Many:
137 return pb::Plural_Arity_Many;
138 default:
139 break;
140 }
141 return pb::Plural_Arity_Other;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144size_t DeserializePluralEnumFromPb(pb::Plural_Arity arity) {
145 switch (arity) {
146 case pb::Plural_Arity_Zero:
147 return Plural::Zero;
148 case pb::Plural_Arity_One:
149 return Plural::One;
150 case pb::Plural_Arity_Two:
151 return Plural::Two;
152 case pb::Plural_Arity_Few:
153 return Plural::Few;
154 case pb::Plural_Arity_Many:
155 return Plural::Many;
156 default:
157 break;
158 }
159 return Plural::Other;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162} // namespace aapt