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 | |
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) { |
| 101 | pb::Attribute_Symbol* pb_symbol = pb_attr->add_symbols(); |
| 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) { |
| 117 | pb::Style_Entry* pb_entry = pb_style->add_entries(); |
| 118 | SerializeReferenceToPb(entry.key, pb_entry->mutable_key()); |
| 119 | |
| 120 | pb::Item* pb_item = pb_entry->mutable_item(); |
| 121 | SerializeItemCommonToPb(entry.key, pb_entry); |
| 122 | PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, pb_item); |
| 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) { |
| 130 | pb::Styleable_Entry* pb_entry = pb_styleable->add_entries(); |
| 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(); |
| 138 | for (auto& value : array->items) { |
| 139 | pb::Array_Entry* pb_entry = pb_array->add_entries(); |
| 140 | SerializeItemCommonToPb(*value, pb_entry); |
| 141 | PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, |
| 142 | pb_entry->mutable_item()); |
| 143 | value->Accept(&sub_visitor); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void Visit(Plural* plural) override { |
| 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 | |
| 156 | pb::Plural_Entry* pb_entry = pb_plural->add_entries(); |
| 157 | pb_entry->set_arity(SerializePluralEnumToPb(i)); |
| 158 | pb::Item* pb_element = pb_entry->mutable_item(); |
| 159 | SerializeItemCommonToPb(*plural->values[i], pb_entry); |
| 160 | PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, pb_element); |
| 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) { |
| 182 | SerializeSourceToPb(item.GetSource(), source_pool_, |
| 183 | pb_item->mutable_source()); |
| 184 | if (!item.GetComment().empty()) { |
| 185 | pb_item->set_comment(item.GetComment()); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void SerializeReferenceToPb(const Reference& ref, pb::Reference* pb_ref) { |
| 190 | if (ref.id) { |
| 191 | pb_ref->set_id(ref.id.value().id); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | if (ref.name) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 195 | StringPool::Ref symbol_ref = symbol_pool_->MakeRef(ref.name.value().ToString()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | pb_ref->set_symbol_idx(static_cast<uint32_t>(symbol_ref.index())); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | pb_ref->set_private_(ref.private_reference); |
| 200 | pb_ref->set_type(SerializeReferenceTypeToPb(ref.reference_type)); |
| 201 | } |
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 | StringPool* source_pool_; |
| 204 | StringPool* symbol_pool_; |
| 205 | pb::Value* out_pb_value_; |
| 206 | pb::Item* out_pb_item_; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | } // namespace |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 210 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 212 | // 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] | 213 | table->string_pool.Prune(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 214 | table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { |
| 215 | int diff = util::compare(a.priority, b.priority); |
| 216 | if (diff == 0) { |
| 217 | diff = a.config.compare(b.config); |
| 218 | } |
| 219 | return diff; |
| 220 | }); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | auto pb_table = util::make_unique<pb::ResourceTable>(); |
| 223 | SerializeStringPoolToPb(table->string_pool, pb_table->mutable_string_pool()); |
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 | StringPool source_pool, symbol_pool; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 226 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | for (auto& package : table->packages) { |
| 228 | pb::Package* pb_package = pb_table->add_packages(); |
| 229 | if (package->id) { |
| 230 | pb_package->set_package_id(package->id.value()); |
| 231 | } |
| 232 | pb_package->set_package_name(package->name); |
| 233 | |
| 234 | for (auto& type : package->types) { |
| 235 | pb::Type* pb_type = pb_package->add_types(); |
| 236 | if (type->id) { |
| 237 | pb_type->set_id(type->id.value()); |
| 238 | } |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 239 | pb_type->set_name(ToString(type->type).to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | |
| 241 | for (auto& entry : type->entries) { |
| 242 | pb::Entry* pb_entry = pb_type->add_entries(); |
| 243 | if (entry->id) { |
| 244 | pb_entry->set_id(entry->id.value()); |
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 | pb_entry->set_name(entry->name); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 247 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | // Write the SymbolStatus struct. |
| 249 | pb::SymbolStatus* pb_status = pb_entry->mutable_symbol_status(); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 250 | pb_status->set_visibility(SerializeVisibilityToPb(entry->symbol_status.state)); |
| 251 | SerializeSourceToPb(entry->symbol_status.source, &source_pool, pb_status->mutable_source()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | pb_status->set_comment(entry->symbol_status.comment); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 253 | pb_status->set_allow_new(entry->symbol_status.allow_new); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 254 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | for (auto& config_value : entry->values) { |
| 256 | pb::ConfigValue* pb_config_value = pb_entry->add_config_values(); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 257 | SerializeConfig(config_value->config, pb_config_value->mutable_config()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | if (!config_value->product.empty()) { |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 259 | pb_config_value->mutable_config()->set_product(config_value->product); |
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 | pb::Value* pb_value = pb_config_value->mutable_value(); |
| 263 | SerializeSourceToPb(config_value->value->GetSource(), &source_pool, |
| 264 | pb_value->mutable_source()); |
| 265 | if (!config_value->value->GetComment().empty()) { |
| 266 | pb_value->set_comment(config_value->value->GetComment()); |
| 267 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | if (config_value->value->IsWeak()) { |
| 270 | pb_value->set_weak(true); |
| 271 | } |
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 | PbSerializerVisitor visitor(&source_pool, &symbol_pool, pb_value); |
| 274 | config_value->value->Accept(&visitor); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 275 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 277 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 279 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | SerializeStringPoolToPb(source_pool, pb_table->mutable_source_pool()); |
| 281 | SerializeStringPoolToPb(symbol_pool, pb_table->mutable_symbol_pool()); |
| 282 | return pb_table; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | std::unique_ptr<pb::CompiledFile> SerializeCompiledFileToPb( |
| 286 | const ResourceFile& file) { |
| 287 | auto pb_file = util::make_unique<pb::CompiledFile>(); |
| 288 | pb_file->set_resource_name(file.name.ToString()); |
| 289 | pb_file->set_source_path(file.source.path); |
| 290 | SerializeConfig(file.config, pb_file->mutable_config()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 291 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | for (const SourcedResourceName& exported : file.exported_symbols) { |
| 293 | pb::CompiledFile_Symbol* pb_symbol = pb_file->add_exported_symbols(); |
| 294 | pb_symbol->set_resource_name(exported.name.ToString()); |
| 295 | pb_symbol->set_line_no(exported.line); |
| 296 | } |
| 297 | return pb_file; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | CompiledFileOutputStream::CompiledFileOutputStream(ZeroCopyOutputStream* out) |
| 301 | : out_(out) {} |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 302 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 303 | void CompiledFileOutputStream::EnsureAlignedWrite() { |
| 304 | const int padding = out_.ByteCount() % 4; |
| 305 | if (padding > 0) { |
| 306 | uint32_t zero = 0u; |
| 307 | out_.WriteRaw(&zero, padding); |
| 308 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 311 | void CompiledFileOutputStream::WriteLittleEndian32(uint32_t val) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | EnsureAlignedWrite(); |
| 313 | out_.WriteLittleEndian32(val); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | void CompiledFileOutputStream::WriteCompiledFile( |
| 317 | const pb::CompiledFile* compiled_file) { |
| 318 | EnsureAlignedWrite(); |
| 319 | out_.WriteLittleEndian64(static_cast<uint64_t>(compiled_file->ByteSize())); |
| 320 | compiled_file->SerializeWithCachedSizes(&out_); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void CompiledFileOutputStream::WriteData(const BigBuffer* buffer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | EnsureAlignedWrite(); |
| 325 | out_.WriteLittleEndian64(static_cast<uint64_t>(buffer->size())); |
| 326 | for (const BigBuffer::Block& block : *buffer) { |
| 327 | out_.WriteRaw(block.buffer.get(), block.size); |
| 328 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void CompiledFileOutputStream::WriteData(const void* data, size_t len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | EnsureAlignedWrite(); |
| 333 | out_.WriteLittleEndian64(static_cast<uint64_t>(len)); |
| 334 | out_.WriteRaw(data, len); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | bool CompiledFileOutputStream::HadError() { return out_.HadError(); } |
| 338 | |
| 339 | CompiledFileInputStream::CompiledFileInputStream(const void* data, size_t size) |
| 340 | : in_(static_cast<const uint8_t*>(data), size) {} |
| 341 | |
| 342 | void CompiledFileInputStream::EnsureAlignedRead() { |
| 343 | const int padding = in_.CurrentPosition() % 4; |
| 344 | if (padding > 0) { |
| 345 | // Reads are always 4 byte aligned. |
| 346 | in_.Skip(padding); |
| 347 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | bool CompiledFileInputStream::ReadLittleEndian32(uint32_t* out_val) { |
| 351 | EnsureAlignedRead(); |
| 352 | return in_.ReadLittleEndian32(out_val); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | bool CompiledFileInputStream::ReadCompiledFile(pb::CompiledFile* out_val) { |
| 356 | EnsureAlignedRead(); |
| 357 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 358 | google::protobuf::uint64 pb_size = 0u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | if (!in_.ReadLittleEndian64(&pb_size)) { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | CodedInputStream::Limit l = in_.PushLimit(static_cast<int>(pb_size)); |
| 364 | |
| 365 | // Check that we haven't tried to read past the end. |
| 366 | if (static_cast<uint64_t>(in_.BytesUntilLimit()) != pb_size) { |
| 367 | in_.PopLimit(l); |
| 368 | in_.PushLimit(0); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | if (!out_val->ParsePartialFromCodedStream(&in_)) { |
| 373 | in_.PopLimit(l); |
| 374 | in_.PushLimit(0); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | in_.PopLimit(l); |
| 379 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | bool CompiledFileInputStream::ReadDataMetaData(uint64_t* out_offset, |
| 383 | uint64_t* out_len) { |
| 384 | EnsureAlignedRead(); |
| 385 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 386 | google::protobuf::uint64 pb_size = 0u; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 387 | if (!in_.ReadLittleEndian64(&pb_size)) { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | // Check that we aren't trying to read past the end. |
| 392 | if (pb_size > static_cast<uint64_t>(in_.BytesUntilLimit())) { |
| 393 | in_.PushLimit(0); |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | uint64_t offset = static_cast<uint64_t>(in_.CurrentPosition()); |
| 398 | if (!in_.Skip(pb_size)) { |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | *out_offset = offset; |
| 403 | *out_len = pb_size; |
| 404 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 407 | } // namespace aapt |