Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "compact_dex_writer.h" |
| 18 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 19 | #include "dex/compact_dex_file.h" |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | void CompactDexWriter::WriteHeader() { |
| 24 | CompactDexFile::Header header; |
| 25 | CompactDexFile::WriteMagic(&header.magic_[0]); |
| 26 | CompactDexFile::WriteCurrentVersion(&header.magic_[0]); |
| 27 | header.checksum_ = header_->Checksum(); |
| 28 | std::copy_n(header_->Signature(), DexFile::kSha1DigestSize, header.signature_); |
| 29 | header.file_size_ = header_->FileSize(); |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 30 | // Since we are not necessarily outputting the same format as the input, avoid using the stored |
| 31 | // header size. |
| 32 | header.header_size_ = GetHeaderSize(); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 33 | header.endian_tag_ = header_->EndianTag(); |
| 34 | header.link_size_ = header_->LinkSize(); |
| 35 | header.link_off_ = header_->LinkOffset(); |
| 36 | const dex_ir::Collections& collections = header_->GetCollections(); |
| 37 | header.map_off_ = collections.MapListOffset(); |
| 38 | header.string_ids_size_ = collections.StringIdsSize(); |
| 39 | header.string_ids_off_ = collections.StringIdsOffset(); |
| 40 | header.type_ids_size_ = collections.TypeIdsSize(); |
| 41 | header.type_ids_off_ = collections.TypeIdsOffset(); |
| 42 | header.proto_ids_size_ = collections.ProtoIdsSize(); |
| 43 | header.proto_ids_off_ = collections.ProtoIdsOffset(); |
| 44 | header.field_ids_size_ = collections.FieldIdsSize(); |
| 45 | header.field_ids_off_ = collections.FieldIdsOffset(); |
| 46 | header.method_ids_size_ = collections.MethodIdsSize(); |
| 47 | header.method_ids_off_ = collections.MethodIdsOffset(); |
| 48 | header.class_defs_size_ = collections.ClassDefsSize(); |
| 49 | header.class_defs_off_ = collections.ClassDefsOffset(); |
| 50 | header.data_size_ = header_->DataSize(); |
| 51 | header.data_off_ = header_->DataOffset(); |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 52 | header.feature_flags_ = 0u; |
| 53 | // In cases where apps are converted to cdex during install, maintain feature flags so that |
| 54 | // the verifier correctly verifies apps that aren't targetting default methods. |
| 55 | if (header_->SupportDefaultMethods()) { |
| 56 | header.feature_flags_ |= static_cast<uint32_t>(CompactDexFile::FeatureFlags::kDefaultMethods); |
| 57 | } |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 58 | UNUSED(Write(reinterpret_cast<uint8_t*>(&header), sizeof(header), 0u)); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 61 | size_t CompactDexWriter::GetHeaderSize() const { |
| 62 | return sizeof(CompactDexFile::Header); |
| 63 | } |
| 64 | |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 65 | } // namespace art |