blob: acf18b0b0eae4897e41e251685c14c99e3bdb23a [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>
21#include <sys/mman.h>
22#include <zlib.h>
23
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include <map>
25
26#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027#include "file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070028#include "globals.h"
29#include "logging.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070030#include "mem_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031#include "stringpiece.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070032#include "unordered_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070033
34namespace art {
35
36class ZipArchive;
37class MemMap;
38
39class ZipEntry {
40
41 public:
42 // Uncompress an entry, in its entirety, to an open file descriptor.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 bool Extract(File& file);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070044
45 uint32_t GetCrc32();
46
47 private:
48
Brian Carlstromdb4d5402011-08-09 12:18:28 -070049 ZipEntry(ZipArchive* zip_archive, const byte* ptr) : zip_archive_(zip_archive), ptr_(ptr) {};
Brian Carlstromb0460ea2011-07-29 10:08:05 -070050
51 // Zip compression methods
52 enum {
53 kCompressStored = 0, // no compression
54 kCompressDeflated = 8, // standard deflate
55 };
56
57 // kCompressStored, kCompressDeflated, ...
58 uint16_t GetCompressionMethod();
59
60 uint32_t GetCompressedLength();
61
62 uint32_t GetUncompressedLength();
63
64 // returns -1 on error
65 off_t GetDataOffset();
66
67 ZipArchive* zip_archive_;
68
69 // pointer to zip entry within central directory
Brian Carlstromdb4d5402011-08-09 12:18:28 -070070 const byte* ptr_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070071
72 friend class ZipArchive;
73};
74
Brian Carlstromb0460ea2011-07-29 10:08:05 -070075class ZipArchive {
76 public:
77
78 // Zip file constants.
79 static const uint32_t kEOCDSignature = 0x06054b50;
80 static const int32_t kEOCDLen = 22;
81 static const int32_t kEOCDNumEntries = 8; // offset to #of entries in file
82 static const int32_t kEOCDSize = 12; // size of the central directory
83 static const int32_t kEOCDFileOffset = 16; // offset to central directory
84
85 static const int32_t kMaxCommentLen = 65535; // longest possible in uint16_t
86 static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
87
88 static const uint32_t kLFHSignature = 0x04034b50;
89 static const int32_t kLFHLen = 30; // excluding variable-len fields
90 static const int32_t kLFHNameLen = 26; // offset to filename length
91 static const int32_t kLFHExtraLen = 28; // offset to extra length
92
93 static const uint32_t kCDESignature = 0x02014b50;
94 static const int32_t kCDELen = 46; // excluding variable-len fields
95 static const int32_t kCDEMethod = 10; // offset to compression method
96 static const int32_t kCDEModWhen = 12; // offset to modification timestamp
97 static const int32_t kCDECRC = 16; // offset to entry CRC
98 static const int32_t kCDECompLen = 20; // offset to compressed length
99 static const int32_t kCDEUncompLen = 24; // offset to uncompressed length
100 static const int32_t kCDENameLen = 28; // offset to filename length
101 static const int32_t kCDEExtraLen = 30; // offset to extra length
102 static const int32_t kCDECommentLen = 32; // offset to comment length
103 static const int32_t kCDELocalOffset = 42; // offset to local hdr
104
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700105 // return new ZipArchive instance on success, NULL on error.
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700106 static ZipArchive* Open(const std::string& filename);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700107 static ZipArchive* Open(int fd);
108
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700109 ZipEntry* Find(const char * name);
110
111 ~ZipArchive() {
112 Close();
113 }
114
115 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700116 explicit ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700117
118 bool MapCentralDirectory();
119 bool Parse();
120 void Close();
121
122 int fd_;
123 uint16_t num_entries_;
124 off_t dir_offset_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700125 UniquePtr<MemMap> dir_map_;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700126 typedef std::tr1::unordered_map<StringPiece, const byte*, StringPieceHash> DirEntries;
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700127 DirEntries dir_entries_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700128
129 friend class ZipEntry;
130};
131
132} // namespace art
133
134#endif // ART_SRC_ZIP_ARCHIVE_H_