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 "art_dex_file_loader.h"
18 
19 #include <sys/stat.h>
20 
21 #include <memory>
22 
23 #include "android-base/stringprintf.h"
24 #include "base/file_magic.h"
25 #include "base/file_utils.h"
26 #include "base/logging.h"
27 #include "base/mem_map.h"
28 #include "base/mman.h"  // For the PROT_* and MAP_* constants.
29 #include "base/stl_util.h"
30 #include "base/systrace.h"
31 #include "base/unix_file/fd_file.h"
32 #include "base/zip_archive.h"
33 #include "dex/compact_dex_file.h"
34 #include "dex/dex_file.h"
35 #include "dex/dex_file_verifier.h"
36 #include "dex/standard_dex_file.h"
37 
38 namespace art {
39 
Open(const uint8_t * base,size_t size,const std::string & location,uint32_t location_checksum,const OatDexFile * oat_dex_file,bool verify,bool verify_checksum,std::string * error_msg,std::unique_ptr<DexFileContainer> container) const40 std::unique_ptr<const DexFile> ArtDexFileLoader::Open(
41     const uint8_t* base,
42     size_t size,
43     const std::string& location,
44     uint32_t location_checksum,
45     const OatDexFile* oat_dex_file,
46     bool verify,
47     bool verify_checksum,
48     std::string* error_msg,
49     std::unique_ptr<DexFileContainer> container) const {
50   return OpenCommon(base,
51                     size,
52                     /*data_base=*/nullptr,
53                     /*data_size=*/0,
54                     location,
55                     location_checksum,
56                     oat_dex_file,
57                     verify,
58                     verify_checksum,
59                     error_msg,
60                     std::move(container),
61                     /*verify_result=*/nullptr);
62 }
63 
Open(const std::string & location,uint32_t location_checksum,MemMap && mem_map,bool verify,bool verify_checksum,std::string * error_msg) const64 std::unique_ptr<const DexFile> ArtDexFileLoader::Open(const std::string& location,
65                                                       uint32_t location_checksum,
66                                                       MemMap&& mem_map,
67                                                       bool verify,
68                                                       bool verify_checksum,
69                                                       std::string* error_msg) const {
70   ArtDexFileLoader loader(std::move(mem_map), location);
71   return loader.Open(location_checksum, verify, verify_checksum, error_msg);
72 }
73 
Open(const char * filename,const std::string & location,bool verify,bool verify_checksum,std::string * error_msg,std::vector<std::unique_ptr<const DexFile>> * dex_files) const74 bool ArtDexFileLoader::Open(const char* filename,
75                             const std::string& location,
76                             bool verify,
77                             bool verify_checksum,
78                             std::string* error_msg,
79                             std::vector<std::unique_ptr<const DexFile>>* dex_files) const {
80   ArtDexFileLoader loader(filename, location);
81   return loader.Open(verify, verify_checksum, error_msg, dex_files);
82 }
83 
84 }  // namespace art
85