blob: 1f48e0a004964cb051e29703959664b7eba02f8a [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 Rogers8d31bbd2013-10-13 10:44:14 -070021#include <string>
Narayan Kamath92572be2013-11-28 14:06:24 +000022#include <ziparchive/zip_archive.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringpiece.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"
Elliott Hughese5448b52012-01-18 16:44:06 -080031#include "UniquePtr.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070032
33namespace art {
34
35class ZipArchive;
36class MemMap;
37
38class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070039 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070040 bool ExtractToFile(File& file, std::string* error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070041 MemMap* ExtractToMemMap(const char* entry_filename, std::string* error_msg);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042
Brian Carlstrom89521892011-12-07 22:05:07 -080043 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070044 uint32_t GetCrc32();
45
46 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000047 ZipEntry(ZipArchiveHandle handle,
48 ::ZipEntry* zip_entry) : handle_(handle), zip_entry_(zip_entry) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070049
Narayan Kamath92572be2013-11-28 14:06:24 +000050 ZipArchiveHandle handle_;
51 ::ZipEntry* const zip_entry_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070052
53 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070054 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070055};
56
Brian Carlstromb0460ea2011-07-29 10:08:05 -070057class ZipArchive {
58 public:
Brian Carlstromb7bbba42011-10-13 14:58:47 -070059 // return new ZipArchive instance on success, NULL on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070060 static ZipArchive* Open(const char* filename, std::string* error_msg);
61 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070062
Narayan Kamath92572be2013-11-28 14:06:24 +000063 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070064
65 ~ZipArchive() {
Narayan Kamath92572be2013-11-28 14:06:24 +000066 CloseArchive(handle_);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070067 }
68
69 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000070 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070071
72 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070073
Narayan Kamath92572be2013-11-28 14:06:24 +000074 ZipArchiveHandle handle_;
75
Elliott Hughesa21039c2012-06-21 12:09:25 -070076 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070077};
78
79} // namespace art
80
Brian Carlstromfc0e3212013-07-17 14:40:12 -070081#endif // ART_RUNTIME_ZIP_ARCHIVE_H_