Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -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 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 17 | #include "standard_dex_file.h" |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 18 | |
| 19 | namespace art { |
| 20 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 21 | const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
| 22 | const uint8_t StandardDexFile::kDexMagicVersions[StandardDexFile::kNumDexVersions] |
| 23 | [StandardDexFile::kDexVersionLen] = { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 24 | {'0', '3', '5', '\0'}, |
| 25 | // Dex version 036 skipped because of an old dalvik bug on some versions of android where dex |
| 26 | // files with that version number would erroneously be accepted and run. |
| 27 | {'0', '3', '7', '\0'}, |
| 28 | // Dex version 038: Android "O" and beyond. |
| 29 | {'0', '3', '8', '\0'}, |
| 30 | // Dex verion 039: Beyond Android "O". |
| 31 | {'0', '3', '9', '\0'}, |
| 32 | }; |
| 33 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 34 | bool StandardDexFile::IsMagicValid(const uint8_t* magic) { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 35 | return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0); |
| 36 | } |
| 37 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 38 | bool StandardDexFile::IsVersionValid(const uint8_t* magic) { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 39 | const uint8_t* version = &magic[sizeof(kDexMagic)]; |
| 40 | for (uint32_t i = 0; i < kNumDexVersions; i++) { |
| 41 | if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 48 | bool StandardDexFile::IsMagicValid() const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 49 | return IsMagicValid(header_->magic_); |
| 50 | } |
| 51 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame^] | 52 | bool StandardDexFile::IsVersionValid() const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 53 | return IsVersionValid(header_->magic_); |
| 54 | } |
| 55 | |
| 56 | } // namespace art |