blob: 1c5b16d84bf408919c8ae64ac86412f928940d43 [file] [log] [blame]
Mathieu Chartierf95a75e2017-11-03 15:25:52 -07001/*
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 Sehr9e734c72018-01-04 17:56:19 -080019#include "dex/compact_dex_file.h"
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070020
21namespace art {
22
23void 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 Chartierf6e31472017-12-28 13:32:08 -080030 // 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 Chartierf95a75e2017-11-03 15:25:52 -070033 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 Chartierf6e31472017-12-28 13:32:08 -080052 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 Chartier3e0c5172017-11-12 12:58:40 -080058 UNUSED(Write(reinterpret_cast<uint8_t*>(&header), sizeof(header), 0u));
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070059}
60
Mathieu Chartierf6e31472017-12-28 13:32:08 -080061size_t CompactDexWriter::GetHeaderSize() const {
62 return sizeof(CompactDexFile::Header);
63}
64
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070065} // namespace art