blob: 8001fa357f4da7d92578ae6abe903cf67482ddd5 [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
Brian Carlstromdb4d5402011-08-09 12:18:28 -070025#include "file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026#include "globals.h"
27#include "logging.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "mem_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070029#include "scoped_ptr.h"
30#include "stringpiece.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070031#include "unordered_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070032
33namespace art {
34
35class ZipArchive;
36class MemMap;
37
38class ZipEntry {
39
40 public:
41 // Uncompress an entry, in its entirety, to an open file descriptor.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042 bool Extract(File& file);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043
44 uint32_t GetCrc32();
45
46 private:
47
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048 ZipEntry(ZipArchive* zip_archive, const byte* ptr) : zip_archive_(zip_archive), ptr_(ptr) {};
Brian Carlstromb0460ea2011-07-29 10:08:05 -070049
50 // Zip compression methods
51 enum {
52 kCompressStored = 0, // no compression
53 kCompressDeflated = 8, // standard deflate
54 };
55
56 // kCompressStored, kCompressDeflated, ...
57 uint16_t GetCompressionMethod();
58
59 uint32_t GetCompressedLength();
60
61 uint32_t GetUncompressedLength();
62
63 // returns -1 on error
64 off_t GetDataOffset();
65
66 ZipArchive* zip_archive_;
67
68 // pointer to zip entry within central directory
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069 const byte* ptr_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070070
71 friend class ZipArchive;
72};
73
Brian Carlstromb0460ea2011-07-29 10:08:05 -070074class ZipArchive {
75 public:
76
77 // Zip file constants.
78 static const uint32_t kEOCDSignature = 0x06054b50;
79 static const int32_t kEOCDLen = 22;
80 static const int32_t kEOCDNumEntries = 8; // offset to #of entries in file
81 static const int32_t kEOCDSize = 12; // size of the central directory
82 static const int32_t kEOCDFileOffset = 16; // offset to central directory
83
84 static const int32_t kMaxCommentLen = 65535; // longest possible in uint16_t
85 static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
86
87 static const uint32_t kLFHSignature = 0x04034b50;
88 static const int32_t kLFHLen = 30; // excluding variable-len fields
89 static const int32_t kLFHNameLen = 26; // offset to filename length
90 static const int32_t kLFHExtraLen = 28; // offset to extra length
91
92 static const uint32_t kCDESignature = 0x02014b50;
93 static const int32_t kCDELen = 46; // excluding variable-len fields
94 static const int32_t kCDEMethod = 10; // offset to compression method
95 static const int32_t kCDEModWhen = 12; // offset to modification timestamp
96 static const int32_t kCDECRC = 16; // offset to entry CRC
97 static const int32_t kCDECompLen = 20; // offset to compressed length
98 static const int32_t kCDEUncompLen = 24; // offset to uncompressed length
99 static const int32_t kCDENameLen = 28; // offset to filename length
100 static const int32_t kCDEExtraLen = 30; // offset to extra length
101 static const int32_t kCDECommentLen = 32; // offset to comment length
102 static const int32_t kCDELocalOffset = 42; // offset to local hdr
103
104 static ZipArchive* Open(const std::string& filename);
105 ZipEntry* Find(const char * name);
106
107 ~ZipArchive() {
108 Close();
109 }
110
111 private:
112 ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {}
113
114 bool MapCentralDirectory();
115 bool Parse();
116 void Close();
117
118 int fd_;
119 uint16_t num_entries_;
120 off_t dir_offset_;
121 scoped_ptr<MemMap> dir_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700122 typedef std::tr1::unordered_map<StringPiece, const byte*> DirEntries;
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700123 DirEntries dir_entries_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700124
125 friend class ZipEntry;
126};
127
128} // namespace art
129
130#endif // ART_SRC_ZIP_ARCHIVE_H_