blob: 084bfd0c781a4ff008939bff568e1b6ffbc7c7b9 [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;
Vladimir Marko4f990712021-07-14 12:45:13 +010035using ZipArchiveHandle = ZipArchive*;
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:
Vladimir Marko9e4b42a2020-04-28 12:28:21 +010044 // Extracts this entry to file.
45 // Returns true on success, false on failure.
46 bool ExtractToFile(File& file, /*out*/std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000047 // Extract this entry to anonymous memory (R/W).
48 // Returns null on failure and sets error_msg.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010049 MemMap ExtractToMemMap(const char* zip_filename,
50 const char* entry_filename,
Vladimir Marko9e4b42a2020-04-28 12:28:21 +010051 /*out*/std::string* error_msg);
52 // Extracts this entry to memory. Stores `GetUncompressedSize()` bytes on success.
53 // Returns true on success, false on failure.
54 bool ExtractToMemory(/*out*/uint8_t* buffer, /*out*/std::string* error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +000055 // Create a file-backed private (clean, R/W) memory mapping to this entry.
56 // 'zip_filename' is used for diagnostics only,
57 // the original file that the ZipArchive was open with is used
58 // for the mapping.
59 //
60 // Will only succeed if the entry is stored uncompressed.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010061 // Returns invalid MemMap on failure and sets error_msg.
62 MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg);
Mathieu Chartier661974a2014-01-09 11:23:53 -080063 virtual ~ZipEntry();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070064
Vladimir Markoc34bebf2018-08-16 16:12:49 +010065 MemMap MapDirectlyOrExtract(const char* zip_filename,
66 const char* entry_filename,
Colin Cross2b41cca2018-11-16 22:43:41 -080067 std::string* error_msg,
68 size_t alignment);
Mathieu Chartier792111c2018-02-15 13:02:15 -080069
Brian Carlstrom89521892011-12-07 22:05:07 -080070 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070071 uint32_t GetCrc32();
72
Igor Murashkin271a0f82017-02-14 21:14:17 +000073 bool IsUncompressed();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000074 bool IsAlignedTo(size_t alignment) const;
Igor Murashkin271a0f82017-02-14 21:14:17 +000075
Brian Carlstromb0460ea2011-07-29 10:08:05 -070076 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000077 ZipEntry(ZipArchiveHandle handle,
Igor Murashkin271a0f82017-02-14 21:14:17 +000078 ::ZipEntry* zip_entry,
79 const std::string& entry_name)
80 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070081
Narayan Kamath92572be2013-11-28 14:06:24 +000082 ZipArchiveHandle handle_;
83 ::ZipEntry* const zip_entry_;
Igor Murashkin271a0f82017-02-14 21:14:17 +000084 std::string const entry_name_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070085
86 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070087 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070088};
89
Brian Carlstromb0460ea2011-07-29 10:08:05 -070090class ZipArchive {
91 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 // return new ZipArchive instance on success, null on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070093 static ZipArchive* Open(const char* filename, std::string* error_msg);
94 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Victor Hsiehecaf7d12021-06-14 11:09:21 -070095 static ZipArchive* OpenFromOwnedFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070096
Narayan Kamath92572be2013-11-28 14:06:24 +000097 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070098
Vladimir Marko9bdf1082016-01-21 12:15:52 +000099 ~ZipArchive();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700100
101 private:
Victor Hsiehecaf7d12021-06-14 11:09:21 -0700102 static ZipArchive* OpenFromFdInternal(int fd,
103 bool assume_ownership,
104 const char* filename,
105 std::string* error_msg);
106
Narayan Kamath92572be2013-11-28 14:06:24 +0000107 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700108
109 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700110
Narayan Kamath92572be2013-11-28 14:06:24 +0000111 ZipArchiveHandle handle_;
112
Elliott Hughesa21039c2012-06-21 12:09:25 -0700113 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700114};
115
116} // namespace art
117
David Sehr79e26072018-04-06 17:58:50 -0700118#endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_