Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "file_magic.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include "base/logging.h" |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 24 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 25 | #include "dex_file.h" |
| 26 | #include "stringprintf.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 30 | File OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) { |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 31 | CHECK(magic != nullptr); |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 32 | File fd(filename, O_RDONLY, /* check_usage */ false); |
| 33 | if (fd.Fd() == -1) { |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 34 | *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno)); |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 35 | return File(); |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 36 | } |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 37 | int n = TEMP_FAILURE_RETRY(read(fd.Fd(), magic, sizeof(*magic))); |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 38 | if (n != sizeof(*magic)) { |
| 39 | *error_msg = StringPrintf("Failed to find magic in '%s'", filename); |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 40 | return File(); |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 41 | } |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 42 | if (lseek(fd.Fd(), 0, SEEK_SET) != 0) { |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 43 | *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename, |
| 44 | strerror(errno)); |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 45 | return File(); |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 46 | } |
| 47 | return fd; |
| 48 | } |
| 49 | |
| 50 | bool IsZipMagic(uint32_t magic) { |
| 51 | return (('P' == ((magic >> 0) & 0xff)) && |
| 52 | ('K' == ((magic >> 8) & 0xff))); |
| 53 | } |
| 54 | |
| 55 | bool IsDexMagic(uint32_t magic) { |
| 56 | return DexFile::IsMagicValid(reinterpret_cast<const uint8_t*>(&magic)); |
| 57 | } |
| 58 | |
| 59 | } // namespace art |