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