Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 17 | #include "proto/ProtoSerialize.h" |
| 18 | |
| 19 | #include "android-base/logging.h" |
| 20 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 21 | #include "Resource.h" |
| 22 | #include "ResourceTable.h" |
| 23 | #include "StringPool.h" |
| 24 | #include "ValueVisitor.h" |
| 25 | #include "proto/ProtoHelpers.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 26 | #include "util/BigBuffer.h" |
| 27 | |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 28 | using ::google::protobuf::io::CodedInputStream; |
| 29 | using ::google::protobuf::io::CodedOutputStream; |
| 30 | using ::google::protobuf::io::ZeroCopyOutputStream; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | namespace { |
| 35 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 36 | class PbSerializerVisitor : public ConstValueVisitor { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 38 | using ConstValueVisitor::Visit; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 40 | // Constructor to use when expecting to serialize any value. |
| 41 | PbSerializerVisitor(StringPool* source_pool, pb::Value* out_pb_value) |
| 42 | : source_pool_(source_pool), out_pb_value_(out_pb_value), out_pb_item_(nullptr) { |
| 43 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 45 | // Constructor to use when expecting to serialize an Item. |
| 46 | PbSerializerVisitor(StringPool* sourcePool, pb::Item* outPbItem) |
| 47 | : source_pool_(sourcePool), out_pb_value_(nullptr), out_pb_item_(outPbItem) { |
| 48 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 50 | void Visit(const Reference* ref) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | SerializeReferenceToPb(*ref, pb_item()->mutable_ref()); |
| 52 | } |
| 53 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 54 | void Visit(const String* str) override { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 55 | pb_item()->mutable_str()->set_value(*str->value); |
| 56 | } |
| 57 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 58 | void Visit(const RawString* str) override { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 59 | pb_item()->mutable_raw_str()->set_value(*str->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 62 | void Visit(const StyledString* str) override { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 63 | pb::StyledString* pb_str = pb_item()->mutable_styled_str(); |
| 64 | pb_str->set_value(str->value->value); |
| 65 | |
| 66 | for (const StringPool::Span& span : str->value->spans) { |
| 67 | pb::StyledString::Span* pb_span = pb_str->add_span(); |
| 68 | pb_span->set_tag(*span.name); |
| 69 | pb_span->set_first_char(span.first_char); |
| 70 | pb_span->set_last_char(span.last_char); |
| 71 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 74 | void Visit(const FileReference* file) override { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 75 | pb_item()->mutable_file()->set_path(*file->path); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 78 | void Visit(const Id* /*id*/) override { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 79 | pb_item()->mutable_id(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 82 | void Visit(const BinaryPrimitive* prim) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | android::Res_value val = {}; |
| 84 | prim->Flatten(&val); |
| 85 | |
| 86 | pb::Primitive* pb_prim = pb_item()->mutable_prim(); |
| 87 | pb_prim->set_type(val.dataType); |
| 88 | pb_prim->set_data(val.data); |
| 89 | } |
| 90 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 91 | void VisitItem(const Item* item) override { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 92 | LOG(FATAL) << "unimplemented item"; |
| 93 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 95 | void Visit(const Attribute* attr) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | pb::Attribute* pb_attr = pb_compound_value()->mutable_attr(); |
| 97 | pb_attr->set_format_flags(attr->type_mask); |
| 98 | pb_attr->set_min_int(attr->min_int); |
| 99 | pb_attr->set_max_int(attr->max_int); |
| 100 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 101 | for (const auto& symbol : attr->symbols) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 102 | pb::Attribute_Symbol* pb_symbol = pb_attr->add_symbol(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | SerializeItemCommonToPb(symbol.symbol, pb_symbol); |
| 104 | SerializeReferenceToPb(symbol.symbol, pb_symbol->mutable_name()); |
| 105 | pb_symbol->set_value(symbol.value); |
| 106 | } |
| 107 | } |
| 108 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 109 | void Visit(const Style* style) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | pb::Style* pb_style = pb_compound_value()->mutable_style(); |
| 111 | if (style->parent) { |
| 112 | SerializeReferenceToPb(style->parent.value(), pb_style->mutable_parent()); |
| 113 | SerializeSourceToPb(style->parent.value().GetSource(), source_pool_, |
| 114 | pb_style->mutable_parent_source()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 117 | for (const Style::Entry& entry : style->entries) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 118 | pb::Style_Entry* pb_entry = pb_style->add_entry(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | SerializeReferenceToPb(entry.key, pb_entry->mutable_key()); |
| 120 | |
| 121 | pb::Item* pb_item = pb_entry->mutable_item(); |
| 122 | SerializeItemCommonToPb(entry.key, pb_entry); |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 123 | PbSerializerVisitor sub_visitor(source_pool_, pb_item); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | entry.value->Accept(&sub_visitor); |
| 125 | } |
| 126 | } |
| 127 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 128 | void Visit(const Styleable* styleable) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | pb::Styleable* pb_styleable = pb_compound_value()->mutable_styleable(); |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 130 | for (const Reference& entry : styleable->entries) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 131 | pb::Styleable_Entry* pb_entry = pb_styleable->add_entry(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | SerializeItemCommonToPb(entry, pb_entry); |
| 133 | SerializeReferenceToPb(entry, pb_entry->mutable_attr()); |
| 134 | } |
| 135 | } |
| 136 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 137 | void Visit(const Array* array) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | pb::Array* pb_array = pb_compound_value()->mutable_array(); |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 139 | for (const auto& value : array->elements) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 140 | pb::Array_Element* pb_element = pb_array->add_element(); |
| 141 | SerializeItemCommonToPb(*value, pb_element); |
| 142 | PbSerializerVisitor sub_visitor(source_pool_, pb_element->mutable_item()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | value->Accept(&sub_visitor); |
| 144 | } |
| 145 | } |
| 146 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 147 | void Visit(const Plural* plural) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | pb::Plural* pb_plural = pb_compound_value()->mutable_plural(); |
| 149 | const size_t count = plural->values.size(); |
| 150 | for (size_t i = 0; i < count; i++) { |
| 151 | if (!plural->values[i]) { |
| 152 | // No plural value set here. |
| 153 | continue; |
| 154 | } |
| 155 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 156 | pb::Plural_Entry* pb_entry = pb_plural->add_entry(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | pb_entry->set_arity(SerializePluralEnumToPb(i)); |
| 158 | pb::Item* pb_element = pb_entry->mutable_item(); |
| 159 | SerializeItemCommonToPb(*plural->values[i], pb_entry); |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 160 | PbSerializerVisitor sub_visitor(source_pool_, pb_element); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | plural->values[i]->Accept(&sub_visitor); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | DISALLOW_COPY_AND_ASSIGN(PbSerializerVisitor); |
| 167 | |
| 168 | pb::Item* pb_item() { |
| 169 | if (out_pb_value_) { |
| 170 | return out_pb_value_->mutable_item(); |
| 171 | } |
| 172 | return out_pb_item_; |
| 173 | } |
| 174 | |
| 175 | pb::CompoundValue* pb_compound_value() { |
| 176 | CHECK(out_pb_value_ != nullptr); |
| 177 | return out_pb_value_->mutable_compound_value(); |
| 178 | } |
| 179 | |
| 180 | template <typename T> |
| 181 | void SerializeItemCommonToPb(const Item& item, T* pb_item) { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 182 | SerializeSourceToPb(item.GetSource(), source_pool_, pb_item->mutable_source()); |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 183 | pb_item->set_comment(item.GetComment()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void SerializeReferenceToPb(const Reference& ref, pb::Reference* pb_ref) { |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 187 | pb_ref->set_id(ref.id.value_or_default(ResourceId(0x0)).id); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | if (ref.name) { |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 190 | pb_ref->set_name(ref.name.value().ToString()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | pb_ref->set_private_(ref.private_reference); |
| 194 | pb_ref->set_type(SerializeReferenceTypeToPb(ref.reference_type)); |
| 195 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | StringPool* source_pool_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | pb::Value* out_pb_value_; |
| 199 | pb::Item* out_pb_item_; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | } // namespace |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 203 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 205 | // We must do this before writing the resources, since the string pool IDs may change. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | table->string_pool.Prune(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 207 | table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { |
| 208 | int diff = util::compare(a.priority, b.priority); |
| 209 | if (diff == 0) { |
| 210 | diff = a.config.compare(b.config); |
| 211 | } |
| 212 | return diff; |
| 213 | }); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | auto pb_table = util::make_unique<pb::ResourceTable>(); |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 216 | StringPool source_pool; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | for (auto& package : table->packages) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 219 | pb::Package* pb_package = pb_table->add_package(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | if (package->id) { |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 221 | pb_package->mutable_package_id()->set_id(package->id.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | } |
| 223 | pb_package->set_package_name(package->name); |
| 224 | |
| 225 | for (auto& type : package->types) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 226 | pb::Type* pb_type = pb_package->add_type(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | if (type->id) { |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 228 | pb_type->mutable_type_id()->set_id(type->id.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | } |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 230 | pb_type->set_name(ToString(type->type).to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | |
| 232 | for (auto& entry : type->entries) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 233 | pb::Entry* pb_entry = pb_type->add_entry(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | if (entry->id) { |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 235 | pb_entry->mutable_entry_id()->set_id(entry->id.value()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 236 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | pb_entry->set_name(entry->name); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 238 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | // Write the SymbolStatus struct. |
| 240 | pb::SymbolStatus* pb_status = pb_entry->mutable_symbol_status(); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 241 | pb_status->set_visibility(SerializeVisibilityToPb(entry->symbol_status.state)); |
| 242 | SerializeSourceToPb(entry->symbol_status.source, &source_pool, pb_status->mutable_source()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | pb_status->set_comment(entry->symbol_status.comment); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 244 | pb_status->set_allow_new(entry->symbol_status.allow_new); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 245 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | for (auto& config_value : entry->values) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 247 | pb::ConfigValue* pb_config_value = pb_entry->add_config_value(); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 248 | SerializeConfig(config_value->config, pb_config_value->mutable_config()); |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 249 | pb_config_value->mutable_config()->set_product(config_value->product); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 250 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | pb::Value* pb_value = pb_config_value->mutable_value(); |
| 252 | SerializeSourceToPb(config_value->value->GetSource(), &source_pool, |
| 253 | pb_value->mutable_source()); |
Adam Lesinski | 5bd4423 | 2017-09-07 09:39:07 -0700 | [diff] [blame] | 254 | pb_value->set_comment(config_value->value->GetComment()); |
| 255 | pb_value->set_weak(config_value->value->IsWeak()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | 0853500 | 2017-08-04 16:15:17 -0700 | [diff] [blame] | 257 | PbSerializerVisitor visitor(&source_pool, pb_value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | config_value->value->Accept(&visitor); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 259 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 261 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 263 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | SerializeStringPoolToPb(source_pool, pb_table->mutable_source_pool()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | return pb_table; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 268 | std::unique_ptr<pb::internal::CompiledFile> SerializeCompiledFileToPb(const ResourceFile& file) { |
| 269 | auto pb_file = util::make_unique<pb::internal::CompiledFile>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | pb_file->set_resource_name(file.name.ToString()); |
| 271 | pb_file->set_source_path(file.source.path); |
| 272 | SerializeConfig(file.config, pb_file->mutable_config()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 273 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | for (const SourcedResourceName& exported : file.exported_symbols) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 275 | pb::internal::CompiledFile_Symbol* pb_symbol = pb_file->add_exported_symbol(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | pb_symbol->set_resource_name(exported.name.ToString()); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 277 | pb_symbol->mutable_source()->set_line_number(exported.line); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | } |
| 279 | return pb_file; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 282 | CompiledFileOutputStream::CompiledFileOutputStream(ZeroCopyOutputStream* out) : out_(out) { |
| 283 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | void CompiledFileOutputStream::EnsureAlignedWrite() { |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 286 | const int overflow = out_.ByteCount() % 4; |
| 287 | if (overflow > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | uint32_t zero = 0u; |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 289 | out_.WriteRaw(&zero, 4 - overflow); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 293 | void CompiledFileOutputStream::WriteLittleEndian32(uint32_t val) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | EnsureAlignedWrite(); |
| 295 | out_.WriteLittleEndian32(val); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 298 | void CompiledFileOutputStream::WriteCompiledFile(const pb::internal::CompiledFile* compiled_file) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | EnsureAlignedWrite(); |
| 300 | out_.WriteLittleEndian64(static_cast<uint64_t>(compiled_file->ByteSize())); |
| 301 | compiled_file->SerializeWithCachedSizes(&out_); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void CompiledFileOutputStream::WriteData(const BigBuffer* buffer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | EnsureAlignedWrite(); |
| 306 | out_.WriteLittleEndian64(static_cast<uint64_t>(buffer->size())); |
| 307 | for (const BigBuffer::Block& block : *buffer) { |
| 308 | out_.WriteRaw(block.buffer.get(), block.size); |
| 309 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void CompiledFileOutputStream::WriteData(const void* data, size_t len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | EnsureAlignedWrite(); |
| 314 | out_.WriteLittleEndian64(static_cast<uint64_t>(len)); |
| 315 | out_.WriteRaw(data, len); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 318 | bool CompiledFileOutputStream::HadError() { |
| 319 | return out_.HadError(); |
| 320 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 321 | |
| 322 | CompiledFileInputStream::CompiledFileInputStream(const void* data, size_t size) |
| 323 | : in_(static_cast<const uint8_t*>(data), size) {} |
| 324 | |
| 325 | void CompiledFileInputStream::EnsureAlignedRead() { |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 326 | const int overflow = in_.CurrentPosition() % 4; |
| 327 | if (overflow > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | // Reads are always 4 byte aligned. |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 329 | in_.Skip(4 - overflow); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | bool CompiledFileInputStream::ReadLittleEndian32(uint32_t* out_val) { |
| 334 | EnsureAlignedRead(); |
| 335 | return in_.ReadLittleEndian32(out_val); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 338 | bool CompiledFileInputStream::ReadCompiledFile(pb::internal::CompiledFile* out_val) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | EnsureAlignedRead(); |
| 340 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 341 | google::protobuf::uint64 pb_size = 0u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | if (!in_.ReadLittleEndian64(&pb_size)) { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | CodedInputStream::Limit l = in_.PushLimit(static_cast<int>(pb_size)); |
| 347 | |
| 348 | // Check that we haven't tried to read past the end. |
| 349 | if (static_cast<uint64_t>(in_.BytesUntilLimit()) != pb_size) { |
| 350 | in_.PopLimit(l); |
| 351 | in_.PushLimit(0); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | if (!out_val->ParsePartialFromCodedStream(&in_)) { |
| 356 | in_.PopLimit(l); |
| 357 | in_.PushLimit(0); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | in_.PopLimit(l); |
| 362 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 365 | bool CompiledFileInputStream::ReadDataMetaData(uint64_t* out_offset, uint64_t* out_len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 366 | EnsureAlignedRead(); |
| 367 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 368 | google::protobuf::uint64 pb_size = 0u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | if (!in_.ReadLittleEndian64(&pb_size)) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // Check that we aren't trying to read past the end. |
| 374 | if (pb_size > static_cast<uint64_t>(in_.BytesUntilLimit())) { |
| 375 | in_.PushLimit(0); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | uint64_t offset = static_cast<uint64_t>(in_.CurrentPosition()); |
| 380 | if (!in_.Skip(pb_size)) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | *out_offset = offset; |
| 385 | *out_len = pb_size; |
| 386 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 389 | } // namespace aapt |