blob: 2169fe095ce065dc1c8d6034aa3774e0dba637f2 [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 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"
Elliott Hughese5448b52012-01-18 16:44:06 -080030#include "UniquePtr.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);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070040 MemMap* ExtractToMemMap(const char* entry_filename, std::string* error_msg);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070041
Brian Carlstrom89521892011-12-07 22:05:07 -080042 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043 uint32_t GetCrc32();
44
45 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000046 ZipEntry(ZipArchiveHandle handle,
47 ::ZipEntry* zip_entry) : handle_(handle), zip_entry_(zip_entry) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070048
Narayan Kamath92572be2013-11-28 14:06:24 +000049 ZipArchiveHandle handle_;
50 ::ZipEntry* const zip_entry_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070051
52 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070053 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070054};
55
Brian Carlstromb0460ea2011-07-29 10:08:05 -070056class ZipArchive {
57 public:
Brian Carlstromb7bbba42011-10-13 14:58:47 -070058 // return new ZipArchive instance on success, NULL on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 static ZipArchive* Open(const char* filename, std::string* error_msg);
60 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070061
Narayan Kamath92572be2013-11-28 14:06:24 +000062 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070063
64 ~ZipArchive() {
Narayan Kamath92572be2013-11-28 14:06:24 +000065 CloseArchive(handle_);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070066 }
67
68 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000069 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070070
71 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070072
Narayan Kamath92572be2013-11-28 14:06:24 +000073 ZipArchiveHandle handle_;
74
Elliott Hughesa21039c2012-06-21 12:09:25 -070075 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070076};
77
78} // namespace art
79
Brian Carlstromfc0e3212013-07-17 14:40:12 -070080#endif // ART_RUNTIME_ZIP_ARCHIVE_H_