blob: 36bb37afe98df54b5301dd7dc4ab03751f67212d [file] [log] [blame]
Mathieu Chartier7b074bf2017-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
Mathieu Chartier292567e2017-10-12 13:24:38 -070017#include "standard_dex_file.h"
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070018
19namespace art {
20
Mathieu Chartier292567e2017-10-12 13:24:38 -070021const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
22const uint8_t StandardDexFile::kDexMagicVersions[StandardDexFile::kNumDexVersions]
23 [StandardDexFile::kDexVersionLen] = {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070024 {'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 Chartier292567e2017-10-12 13:24:38 -070034bool StandardDexFile::IsMagicValid(const uint8_t* magic) {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070035 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
36}
37
Mathieu Chartier292567e2017-10-12 13:24:38 -070038bool StandardDexFile::IsVersionValid(const uint8_t* magic) {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070039 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 Chartier292567e2017-10-12 13:24:38 -070048bool StandardDexFile::IsMagicValid() const {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070049 return IsMagicValid(header_->magic_);
50}
51
Mathieu Chartier292567e2017-10-12 13:24:38 -070052bool StandardDexFile::IsVersionValid() const {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070053 return IsVersionValid(header_->magic_);
54}
55
56} // namespace art