blob: 821cc5ceb1110be0771818964fd3abe175c5d557 [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>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
22#include <string>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/random_access_file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027#include "mem_map.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070029#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070030
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070031// system/core/zip_archive definitions.
32struct ZipEntry;
33typedef void* ZipArchiveHandle;
34
Brian Carlstromb0460ea2011-07-29 10:08:05 -070035namespace art {
36
37class ZipArchive;
38class MemMap;
39
40class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070041 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070042 bool ExtractToFile(File& file, std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000043 // Extract this entry to anonymous memory (R/W).
44 // Returns null on failure and sets error_msg.
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070045 MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename,
46 std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000047 // Create a file-backed private (clean, R/W) memory mapping to this entry.
48 // 'zip_filename' is used for diagnostics only,
49 // the original file that the ZipArchive was open with is used
50 // for the mapping.
51 //
52 // Will only succeed if the entry is stored uncompressed.
53 // Returns null on failure and sets error_msg.
54 MemMap* MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg);
Mathieu Chartier661974a2014-01-09 11:23:53 -080055 virtual ~ZipEntry();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070056
Brian Carlstrom89521892011-12-07 22:05:07 -080057 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070058 uint32_t GetCrc32();
59
Igor Murashkin271a0f82017-02-14 21:14:17 +000060 bool IsUncompressed();
61 bool IsAlignedTo(size_t alignment);
62
Brian Carlstromb0460ea2011-07-29 10:08:05 -070063 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000064 ZipEntry(ZipArchiveHandle handle,
Igor Murashkin271a0f82017-02-14 21:14:17 +000065 ::ZipEntry* zip_entry,
66 const std::string& entry_name)
67 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070068
Narayan Kamath92572be2013-11-28 14:06:24 +000069 ZipArchiveHandle handle_;
70 ::ZipEntry* const zip_entry_;
Igor Murashkin271a0f82017-02-14 21:14:17 +000071 std::string const entry_name_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070072
73 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070074 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070075};
76
Brian Carlstromb0460ea2011-07-29 10:08:05 -070077class ZipArchive {
78 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 // return new ZipArchive instance on success, null on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070080 static ZipArchive* Open(const char* filename, std::string* error_msg);
81 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070082
Narayan Kamath92572be2013-11-28 14:06:24 +000083 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070084
Vladimir Marko9bdf1082016-01-21 12:15:52 +000085 ~ZipArchive();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070086
87 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000088 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070089
90 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070091
Narayan Kamath92572be2013-11-28 14:06:24 +000092 ZipArchiveHandle handle_;
93
Elliott Hughesa21039c2012-06-21 12:09:25 -070094 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070095};
96
97} // namespace art
98
Brian Carlstromfc0e3212013-07-17 14:40:12 -070099#endif // ART_RUNTIME_ZIP_ARCHIVE_H_