blob: d326a9e9d7ecfeedafc36fd08aa7df9fd535e8ff [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
David Sehr79e26072018-04-06 17:58:50 -070017#ifndef ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_
18#define ART_LIBARTBASE_BASE_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
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
25
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026#include "globals.h"
David Sehr1979c642018-04-26 14:41:18 -070027#include "mem_map.h"
28#include "os.h"
29#include "safe_map.h"
30#include "unix_file/random_access_file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070032// system/core/zip_archive definitions.
Ryan Prichard35f31c62018-10-10 22:11:57 -070033struct ZipArchive;
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070034struct ZipEntry;
Ryan Prichard35f31c62018-10-10 22:11:57 -070035typedef ZipArchive* ZipArchiveHandle;
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070036
Brian Carlstromb0460ea2011-07-29 10:08:05 -070037namespace art {
38
39class ZipArchive;
40class MemMap;
41
42class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 bool ExtractToFile(File& file, std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000045 // Extract this entry to anonymous memory (R/W).
46 // Returns null on failure and sets error_msg.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010047 MemMap ExtractToMemMap(const char* zip_filename,
48 const char* entry_filename,
49 std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000050 // Create a file-backed private (clean, R/W) memory mapping to this entry.
51 // 'zip_filename' is used for diagnostics only,
52 // the original file that the ZipArchive was open with is used
53 // for the mapping.
54 //
55 // Will only succeed if the entry is stored uncompressed.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010056 // Returns invalid MemMap on failure and sets error_msg.
57 MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg);
Mathieu Chartier661974a2014-01-09 11:23:53 -080058 virtual ~ZipEntry();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070059
Vladimir Markoc34bebf2018-08-16 16:12:49 +010060 MemMap MapDirectlyOrExtract(const char* zip_filename,
61 const char* entry_filename,
62 std::string* error_msg);
Mathieu Chartier792111c2018-02-15 13:02:15 -080063
Brian Carlstrom89521892011-12-07 22:05:07 -080064 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070065 uint32_t GetCrc32();
66
Igor Murashkin271a0f82017-02-14 21:14:17 +000067 bool IsUncompressed();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000068 bool IsAlignedTo(size_t alignment) const;
Igor Murashkin271a0f82017-02-14 21:14:17 +000069
Brian Carlstromb0460ea2011-07-29 10:08:05 -070070 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000071 ZipEntry(ZipArchiveHandle handle,
Igor Murashkin271a0f82017-02-14 21:14:17 +000072 ::ZipEntry* zip_entry,
73 const std::string& entry_name)
74 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070075
Narayan Kamath92572be2013-11-28 14:06:24 +000076 ZipArchiveHandle handle_;
77 ::ZipEntry* const zip_entry_;
Igor Murashkin271a0f82017-02-14 21:14:17 +000078 std::string const entry_name_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070079
80 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070081 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070082};
83
Brian Carlstromb0460ea2011-07-29 10:08:05 -070084class ZipArchive {
85 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 // return new ZipArchive instance on success, null on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 static ZipArchive* Open(const char* filename, std::string* error_msg);
88 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070089
Narayan Kamath92572be2013-11-28 14:06:24 +000090 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070091
Vladimir Marko9bdf1082016-01-21 12:15:52 +000092 ~ZipArchive();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070093
94 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000095 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070096
97 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070098
Narayan Kamath92572be2013-11-28 14:06:24 +000099 ZipArchiveHandle handle_;
100
Elliott Hughesa21039c2012-06-21 12:09:25 -0700101 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700102};
103
104} // namespace art
105
David Sehr79e26072018-04-06 17:58:50 -0700106#endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_