• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "standard_dex_file.h"
18 
19 #include "base/casts.h"
20 #include "base/leb128.h"
21 #include "code_item_accessors-inl.h"
22 #include "dex_file-inl.h"
23 
24 namespace art {
25 
26 const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
27 const uint8_t StandardDexFile::kDexMagicVersions[StandardDexFile::kNumDexVersions]
28                                                 [StandardDexFile::kDexVersionLen] = {
29   {'0', '3', '5', '\0'},
30   // Dex version 036 skipped because of an old dalvik bug on some versions of android where dex
31   // files with that version number would erroneously be accepted and run.
32   {'0', '3', '7', '\0'},
33   // Dex version 038: Android "O" and beyond.
34   {'0', '3', '8', '\0'},
35   // Dex verion 039: Beyond Android "O".
36   {'0', '3', '9', '\0'},
37 };
38 
WriteMagic(uint8_t * magic)39 void StandardDexFile::WriteMagic(uint8_t* magic) {
40   std::copy_n(kDexMagic, kDexMagicSize, magic);
41 }
42 
WriteCurrentVersion(uint8_t * magic)43 void StandardDexFile::WriteCurrentVersion(uint8_t* magic) {
44   std::copy_n(kDexMagicVersions[StandardDexFile::kDexVersionLen - 1],
45               kDexVersionLen,
46               magic + kDexMagicSize);
47 }
48 
IsMagicValid(const uint8_t * magic)49 bool StandardDexFile::IsMagicValid(const uint8_t* magic) {
50   return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
51 }
52 
IsVersionValid(const uint8_t * magic)53 bool StandardDexFile::IsVersionValid(const uint8_t* magic) {
54   const uint8_t* version = &magic[sizeof(kDexMagic)];
55   for (uint32_t i = 0; i < kNumDexVersions; i++) {
56     if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) {
57       return true;
58     }
59   }
60   return false;
61 }
62 
IsMagicValid() const63 bool StandardDexFile::IsMagicValid() const {
64   return IsMagicValid(header_->magic_);
65 }
66 
IsVersionValid() const67 bool StandardDexFile::IsVersionValid() const {
68   return IsVersionValid(header_->magic_);
69 }
70 
SupportsDefaultMethods() const71 bool StandardDexFile::SupportsDefaultMethods() const {
72   return GetDexVersion() >= DexFile::kDefaultMethodsVersion;
73 }
74 
GetCodeItemSize(const dex::CodeItem & item) const75 uint32_t StandardDexFile::GetCodeItemSize(const dex::CodeItem& item) const {
76   DCHECK(IsInDataSection(&item));
77   return reinterpret_cast<uintptr_t>(CodeItemDataAccessor(*this, &item).CodeItemDataEnd()) -
78       reinterpret_cast<uintptr_t>(&item);
79 }
80 
81 }  // namespace art
82