blob: d7a8d801843bb637ba0ba80f5d4d2048403b2bed [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
17#ifndef ART_SRC_ZIP_ARCHIVE_H_
18#define ART_SRC_ZIP_ARCHIVE_H_
19
Brian Carlstromb0460ea2011-07-29 10:08:05 -070020#include <stdint.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070021#include <zlib.h>
22
Brian Carlstromdb4d5402011-08-09 12:18:28 -070023#include "file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024#include "globals.h"
25#include "logging.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070026#include "mem_map.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070027#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070028#include "stringpiece.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080029#include "UniquePtr.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070030
31namespace art {
32
33class ZipArchive;
34class MemMap;
35
36class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070037 public:
Brian Carlstrom89521892011-12-07 22:05:07 -080038 bool ExtractToFile(File& file);
39 bool ExtractToMemory(MemMap& mem_map);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040
Brian Carlstrom89521892011-12-07 22:05:07 -080041 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042 uint32_t GetCrc32();
43
44 private:
Brian Carlstroma6cc8932012-01-04 14:44:07 -080045 ZipEntry(const ZipArchive* zip_archive, const byte* ptr) : zip_archive_(zip_archive), ptr_(ptr) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070046
47 // Zip compression methods
48 enum {
49 kCompressStored = 0, // no compression
50 kCompressDeflated = 8, // standard deflate
51 };
52
53 // kCompressStored, kCompressDeflated, ...
54 uint16_t GetCompressionMethod();
55
56 uint32_t GetCompressedLength();
57
Brian Carlstromb0460ea2011-07-29 10:08:05 -070058 // returns -1 on error
59 off_t GetDataOffset();
60
Brian Carlstroma6cc8932012-01-04 14:44:07 -080061 const ZipArchive* zip_archive_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070062
63 // pointer to zip entry within central directory
Brian Carlstromdb4d5402011-08-09 12:18:28 -070064 const byte* ptr_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070065
66 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070067 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070068};
69
Brian Carlstromb0460ea2011-07-29 10:08:05 -070070class ZipArchive {
71 public:
Brian Carlstromb0460ea2011-07-29 10:08:05 -070072 // Zip file constants.
73 static const uint32_t kEOCDSignature = 0x06054b50;
74 static const int32_t kEOCDLen = 22;
75 static const int32_t kEOCDNumEntries = 8; // offset to #of entries in file
76 static const int32_t kEOCDSize = 12; // size of the central directory
77 static const int32_t kEOCDFileOffset = 16; // offset to central directory
78
79 static const int32_t kMaxCommentLen = 65535; // longest possible in uint16_t
80 static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
81
82 static const uint32_t kLFHSignature = 0x04034b50;
83 static const int32_t kLFHLen = 30; // excluding variable-len fields
84 static const int32_t kLFHNameLen = 26; // offset to filename length
85 static const int32_t kLFHExtraLen = 28; // offset to extra length
86
87 static const uint32_t kCDESignature = 0x02014b50;
88 static const int32_t kCDELen = 46; // excluding variable-len fields
89 static const int32_t kCDEMethod = 10; // offset to compression method
90 static const int32_t kCDEModWhen = 12; // offset to modification timestamp
91 static const int32_t kCDECRC = 16; // offset to entry CRC
92 static const int32_t kCDECompLen = 20; // offset to compressed length
93 static const int32_t kCDEUncompLen = 24; // offset to uncompressed length
94 static const int32_t kCDENameLen = 28; // offset to filename length
95 static const int32_t kCDEExtraLen = 30; // offset to extra length
96 static const int32_t kCDECommentLen = 32; // offset to comment length
97 static const int32_t kCDELocalOffset = 42; // offset to local hdr
98
Brian Carlstromb7bbba42011-10-13 14:58:47 -070099 // return new ZipArchive instance on success, NULL on error.
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700100 static ZipArchive* Open(const std::string& filename);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800101 static ZipArchive* OpenFromFd(int fd);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700102
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800103 ZipEntry* Find(const char* name) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700104
105 ~ZipArchive() {
106 Close();
107 }
108
109 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700110 explicit ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700111
112 bool MapCentralDirectory();
113 bool Parse();
114 void Close();
115
116 int fd_;
117 uint16_t num_entries_;
118 off_t dir_offset_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700119 UniquePtr<MemMap> dir_map_;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700120 typedef SafeMap<StringPiece, const byte*> DirEntries;
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700121 DirEntries dir_entries_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700122
123 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700124
125 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700126};
127
128} // namespace art
129
130#endif // ART_SRC_ZIP_ARCHIVE_H_