blob: 18584447e872214fc109a3609d62f487ed34f47f [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ZIP_ARCHIVE_H_
18#define ART_RUNTIME_ZIP_ARCHIVE_H_
Brian Carlstromb0460ea2011-07-29 10:08:05 -070019
Brian Carlstromb0460ea2011-07-29 10:08:05 -070020#include <stdint.h>
Narayan Kamath92572be2013-11-28 14:06:24 +000021#include <ziparchive/zip_archive.h>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
23#include <string>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/random_access_file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "mem_map.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031
32namespace art {
33
34class ZipArchive;
35class MemMap;
36
37class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070038 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070039 bool ExtractToFile(File& file, std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000040 // Extract this entry to anonymous memory (R/W).
41 // Returns null on failure and sets error_msg.
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070042 MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename,
43 std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000044 // Create a file-backed private (clean, R/W) memory mapping to this entry.
45 // 'zip_filename' is used for diagnostics only,
46 // the original file that the ZipArchive was open with is used
47 // for the mapping.
48 //
49 // Will only succeed if the entry is stored uncompressed.
50 // Returns null on failure and sets error_msg.
51 MemMap* MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg);
Mathieu Chartier661974a2014-01-09 11:23:53 -080052 virtual ~ZipEntry();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070053
Brian Carlstrom89521892011-12-07 22:05:07 -080054 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070055 uint32_t GetCrc32();
56
Igor Murashkin271a0f82017-02-14 21:14:17 +000057 bool IsUncompressed();
58 bool IsAlignedTo(size_t alignment);
59
Brian Carlstromb0460ea2011-07-29 10:08:05 -070060 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000061 ZipEntry(ZipArchiveHandle handle,
Igor Murashkin271a0f82017-02-14 21:14:17 +000062 ::ZipEntry* zip_entry,
63 const std::string& entry_name)
64 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070065
Narayan Kamath92572be2013-11-28 14:06:24 +000066 ZipArchiveHandle handle_;
67 ::ZipEntry* const zip_entry_;
Igor Murashkin271a0f82017-02-14 21:14:17 +000068 std::string const entry_name_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070069
70 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070071 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070072};
73
Brian Carlstromb0460ea2011-07-29 10:08:05 -070074class ZipArchive {
75 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 // return new ZipArchive instance on success, null on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070077 static ZipArchive* Open(const char* filename, std::string* error_msg);
78 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070079
Narayan Kamath92572be2013-11-28 14:06:24 +000080 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070081
Vladimir Marko9bdf1082016-01-21 12:15:52 +000082 ~ZipArchive();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070083
84 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000085 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070086
87 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070088
Narayan Kamath92572be2013-11-28 14:06:24 +000089 ZipArchiveHandle handle_;
90
Elliott Hughesa21039c2012-06-21 12:09:25 -070091 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070092};
93
94} // namespace art
95
Brian Carlstromfc0e3212013-07-17 14:40:12 -070096#endif // ART_RUNTIME_ZIP_ARCHIVE_H_