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