blob: ff193ffb07fce8de44d47d429d74f79ce505f9a4 [file] [log] [blame]
Mathieu Chartiercf76bf82017-09-25 16:22:36 -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_file.h"
18
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080019#include "code_item_accessors-no_art-inl.h"
Mathieu Chartier6238c832018-01-04 09:55:13 -080020#include "dex_file-inl.h"
21#include "leb128.h"
22
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070023namespace art {
24
25constexpr uint8_t CompactDexFile::kDexMagic[kDexMagicSize];
26constexpr uint8_t CompactDexFile::kDexMagicVersion[];
27
Mathieu Chartier603ccab2017-10-20 14:34:28 -070028void CompactDexFile::WriteMagic(uint8_t* magic) {
29 std::copy_n(kDexMagic, kDexMagicSize, magic);
30}
31
32void CompactDexFile::WriteCurrentVersion(uint8_t* magic) {
33 std::copy_n(kDexMagicVersion, kDexVersionLen, magic + kDexMagicSize);
34}
35
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070036bool CompactDexFile::IsMagicValid(const uint8_t* magic) {
37 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
38}
39
40bool CompactDexFile::IsVersionValid(const uint8_t* magic) {
41 const uint8_t* version = &magic[sizeof(kDexMagic)];
42 return memcmp(version, kDexMagicVersion, kDexVersionLen) == 0;
43}
44
45bool CompactDexFile::IsMagicValid() const {
46 return IsMagicValid(header_->magic_);
47}
48
49bool CompactDexFile::IsVersionValid() const {
50 return IsVersionValid(header_->magic_);
51}
52
Mathieu Chartierf6e31472017-12-28 13:32:08 -080053bool CompactDexFile::SupportsDefaultMethods() const {
54 return (GetHeader().GetFeatureFlags() &
55 static_cast<uint32_t>(FeatureFlags::kDefaultMethods)) != 0;
56}
57
Mathieu Chartier6238c832018-01-04 09:55:13 -080058uint32_t CompactDexFile::GetCodeItemSize(const DexFile::CodeItem& item) const {
59 // TODO: Clean up this temporary code duplication with StandardDexFile. Eventually the
60 // implementations will differ.
61 DCHECK(HasAddress(&item));
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080062 return reinterpret_cast<uintptr_t>(CodeItemDataAccessor(*this, &item).CodeItemDataEnd()) -
63 reinterpret_cast<uintptr_t>(&item);
Mathieu Chartier6238c832018-01-04 09:55:13 -080064}
65
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080066CompactDexFile::CompactDexFile(const uint8_t* base,
67 size_t size,
68 const std::string& location,
69 uint32_t location_checksum,
70 const OatDexFile* oat_dex_file,
71 DexFileContainer* container)
72 : DexFile(base,
73 size,
74 location,
75 location_checksum,
76 oat_dex_file,
77 container,
78 /*is_compact_dex*/ true),
79 debug_info_offsets_(Begin() + GetHeader().debug_info_offsets_pos_,
80 GetHeader().debug_info_base_,
81 GetHeader().debug_info_offsets_table_offset_) {}
82
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070083} // namespace art