blob: 9eb29486d60c6304c4af0c23e2adb5aac4f82f08 [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
20#include <map>
21#include <stdint.h>
22#include <sys/mman.h>
23#include <zlib.h>
24
25#include "globals.h"
26#include "logging.h"
27#include "scoped_ptr.h"
28#include "stringpiece.h"
29
30namespace art {
31
32class ZipArchive;
33class MemMap;
34
35class ZipEntry {
36
37 public:
38 // Uncompress an entry, in its entirety, to an open file descriptor.
39 bool Extract(int fd);
40
41 uint32_t GetCrc32();
42
43 private:
44
45 ZipEntry(ZipArchive* zip_archive, const uint8_t* ptr) : zip_archive_(zip_archive), ptr_(ptr) {};
46
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
58 uint32_t GetUncompressedLength();
59
60 // returns -1 on error
61 off_t GetDataOffset();
62
63 ZipArchive* zip_archive_;
64
65 // pointer to zip entry within central directory
66 const uint8_t* ptr_;
67
68 friend class ZipArchive;
69};
70
71// Used to keep track of unaligned mmap segments.
72class MemMap {
73 public:
74
75 // Map part of a file into a shared, read-only memory segment. The "start"
76 // offset is absolute, not relative.
77 //
78 // On success, returns returns a MemMap instance. On failure, returns a NULL;
79 static MemMap* Map(int fd, off_t start, size_t length) {
80 // adjust to be page-aligned
81 int page_offset = start % kPageSize;
82 off_t page_aligned_offset = start - page_offset;
83 size_t page_aligned_size = length + page_offset;
84 uint8_t* addr = reinterpret_cast<uint8_t*>(mmap(NULL,
85 page_aligned_size,
86 PROT_READ,
87 MAP_FILE | MAP_SHARED,
88 fd,
89 page_aligned_offset));
90 if (addr == MAP_FAILED) {
91 return NULL;
92 }
93 return new MemMap(addr+page_offset, length, addr, page_aligned_size);
94 }
95
96 ~MemMap() {
97 Unmap();
98 };
99
100 // Release a memory mapping, returning true on success or it was previously unmapped.
101 bool Unmap() {
102 if (base_addr_ == NULL && base_length_ == 0) {
103 return true;
104 }
105 int result = munmap(base_addr_, base_length_);
106 if (result != 0) {
107 return false;
108 }
109 base_addr_ = NULL;
110 base_length_ = 0;
111 return true;
112 }
113
114 void* GetAddress() {
115 return addr_;
116 }
117
118 size_t GetLength() {
119 return length_;
120 }
121
122 private:
123 MemMap(void* addr, size_t length, void* base_addr, size_t base_length)
124 : addr_(addr), length_(length), base_addr_(base_addr), base_length_(base_length) {
125 CHECK(addr_ != NULL);
126 CHECK(length_ != 0);
127 CHECK(base_addr_ != NULL);
128 CHECK(base_length_ != 0);
129 };
130
131 void* addr_; // start of data
132 size_t length_; // length of data
133
134 void* base_addr_; // page-aligned base address
135 size_t base_length_; // length of mapping
136};
137
138class ZipArchive {
139 public:
140
141 // Zip file constants.
142 static const uint32_t kEOCDSignature = 0x06054b50;
143 static const int32_t kEOCDLen = 22;
144 static const int32_t kEOCDNumEntries = 8; // offset to #of entries in file
145 static const int32_t kEOCDSize = 12; // size of the central directory
146 static const int32_t kEOCDFileOffset = 16; // offset to central directory
147
148 static const int32_t kMaxCommentLen = 65535; // longest possible in uint16_t
149 static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
150
151 static const uint32_t kLFHSignature = 0x04034b50;
152 static const int32_t kLFHLen = 30; // excluding variable-len fields
153 static const int32_t kLFHNameLen = 26; // offset to filename length
154 static const int32_t kLFHExtraLen = 28; // offset to extra length
155
156 static const uint32_t kCDESignature = 0x02014b50;
157 static const int32_t kCDELen = 46; // excluding variable-len fields
158 static const int32_t kCDEMethod = 10; // offset to compression method
159 static const int32_t kCDEModWhen = 12; // offset to modification timestamp
160 static const int32_t kCDECRC = 16; // offset to entry CRC
161 static const int32_t kCDECompLen = 20; // offset to compressed length
162 static const int32_t kCDEUncompLen = 24; // offset to uncompressed length
163 static const int32_t kCDENameLen = 28; // offset to filename length
164 static const int32_t kCDEExtraLen = 30; // offset to extra length
165 static const int32_t kCDECommentLen = 32; // offset to comment length
166 static const int32_t kCDELocalOffset = 42; // offset to local hdr
167
168 static ZipArchive* Open(const std::string& filename);
169 ZipEntry* Find(const char * name);
170
171 ~ZipArchive() {
172 Close();
173 }
174
175 private:
176 ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {}
177
178 bool MapCentralDirectory();
179 bool Parse();
180 void Close();
181
182 int fd_;
183 uint16_t num_entries_;
184 off_t dir_offset_;
185 scoped_ptr<MemMap> dir_map_;
186 // TODO: unordered map
187 std::map<StringPiece, const uint8_t*> dir_entries_;
188
189 friend class ZipEntry;
190};
191
192} // namespace art
193
194#endif // ART_SRC_ZIP_ARCHIVE_H_