David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "vdex_file.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "base/logging.h" |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | constexpr uint8_t VdexFile::Header::kVdexMagic[4]; |
| 27 | constexpr uint8_t VdexFile::Header::kVdexVersion[4]; |
| 28 | |
| 29 | bool VdexFile::Header::IsMagicValid() const { |
| 30 | return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0); |
| 31 | } |
| 32 | |
| 33 | bool VdexFile::Header::IsVersionValid() const { |
| 34 | return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0); |
| 35 | } |
| 36 | |
| 37 | VdexFile::Header::Header() { |
| 38 | memcpy(magic_, kVdexMagic, sizeof(kVdexMagic)); |
| 39 | memcpy(version_, kVdexVersion, sizeof(kVdexVersion)); |
| 40 | DCHECK(IsMagicValid()); |
| 41 | DCHECK(IsVersionValid()); |
| 42 | } |
| 43 | |
| 44 | VdexFile* VdexFile::Open(const std::string& vdex_filename, |
| 45 | bool writable, |
| 46 | bool low_4gb, |
| 47 | std::string* error_msg) { |
| 48 | if (!OS::FileExists(vdex_filename.c_str())) { |
| 49 | *error_msg = "File " + vdex_filename + " does not exist."; |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<File> vdex_file; |
| 54 | if (writable) { |
| 55 | vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str())); |
| 56 | } else { |
| 57 | vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str())); |
| 58 | } |
| 59 | if (vdex_file == nullptr) { |
| 60 | *error_msg = "Could not open file " + vdex_filename + |
| 61 | (writable ? " for read/write" : "for reading"); |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | int64_t vdex_length = vdex_file->GetLength(); |
| 66 | if (vdex_length == -1) { |
| 67 | *error_msg = "Could not read the length of file " + vdex_filename; |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length, |
| 72 | writable ? PROT_READ | PROT_WRITE : PROT_READ, |
| 73 | MAP_SHARED, |
| 74 | vdex_file->Fd(), |
| 75 | 0 /* start offset */, |
| 76 | low_4gb, |
| 77 | vdex_filename.c_str(), |
| 78 | error_msg)); |
| 79 | if (mmap == nullptr) { |
| 80 | *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg; |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | *error_msg = "Success"; |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 85 | return new VdexFile(mmap.release()); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } // namespace art |