Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2008 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 #ifndef ART_RUNTIME_ZIP_ARCHIVE_H_
     18 #define ART_RUNTIME_ZIP_ARCHIVE_H_
     19 
     20 #include <stdint.h>
     21 #include <memory>
     22 #include <string>
     23 
     24 #include <android-base/logging.h>
     25 
     26 #include "base/os.h"
     27 #include "base/safe_map.h"
     28 #include "base/unix_file/random_access_file.h"
     29 #include "globals.h"
     30 #include "mem_map.h"
     31 
     32 // system/core/zip_archive definitions.
     33 struct ZipEntry;
     34 typedef void* ZipArchiveHandle;
     35 
     36 namespace art {
     37 
     38 class ZipArchive;
     39 class MemMap;
     40 
     41 class ZipEntry {
     42  public:
     43   bool ExtractToFile(File& file, std::string* error_msg);
     44   // Extract this entry to anonymous memory (R/W).
     45   // Returns null on failure and sets error_msg.
     46   MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename,
     47                           std::string* error_msg);
     48   // Create a file-backed private (clean, R/W) memory mapping to this entry.
     49   // 'zip_filename' is used for diagnostics only,
     50   //   the original file that the ZipArchive was open with is used
     51   //   for the mapping.
     52   //
     53   // Will only succeed if the entry is stored uncompressed.
     54   // Returns null on failure and sets error_msg.
     55   MemMap* MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg);
     56   virtual ~ZipEntry();
     57 
     58   MemMap* MapDirectlyOrExtract(const char* zip_filename,
     59                                const char* entry_filename,
     60                                std::string* error_msg);
     61 
     62   uint32_t GetUncompressedLength();
     63   uint32_t GetCrc32();
     64 
     65   bool IsUncompressed();
     66   bool IsAlignedTo(size_t alignment) const;
     67   bool IsAlignedToDexHeader() const;
     68 
     69  private:
     70   ZipEntry(ZipArchiveHandle handle,
     71            ::ZipEntry* zip_entry,
     72            const std::string& entry_name)
     73     : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {}
     74 
     75   ZipArchiveHandle handle_;
     76   ::ZipEntry* const zip_entry_;
     77   std::string const entry_name_;
     78 
     79   friend class ZipArchive;
     80   DISALLOW_COPY_AND_ASSIGN(ZipEntry);
     81 };
     82 
     83 class ZipArchive {
     84  public:
     85   // return new ZipArchive instance on success, null on error.
     86   static ZipArchive* Open(const char* filename, std::string* error_msg);
     87   static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
     88 
     89   ZipEntry* Find(const char* name, std::string* error_msg) const;
     90 
     91   ~ZipArchive();
     92 
     93  private:
     94   explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
     95 
     96   friend class ZipEntry;
     97 
     98   ZipArchiveHandle handle_;
     99 
    100   DISALLOW_COPY_AND_ASSIGN(ZipArchive);
    101 };
    102 
    103 }  // namespace art
    104 
    105 #endif  // ART_RUNTIME_ZIP_ARCHIVE_H_
    106