Line data Source code
1 : /* src/vm/zip.hpp - ZIP file handling for bootstrap classloader
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #ifndef ZIP_HPP_
27 : #define ZIP_HPP_ 1
28 :
29 : #include "config.h" // for ENABLE_ZLIB
30 :
31 : #ifdef ENABLE_ZLIB
32 :
33 : #include <cstddef> // for size_t
34 : #include <stdint.h> // for uint8_t
35 :
36 : #include "toolbox/hashtable.hpp" // for InsertOnlyStringEntry, etc
37 :
38 : #include "vm/types.hpp" // for u2, u4, u1
39 : #include "vm/utf8.hpp" // for Utf8String
40 :
41 :
42 : /* Local file header ***********************************************************
43 :
44 : local file header signature 4 bytes (0x04034b50)
45 : version needed to extract 2 bytes
46 : general purpose bit flag 2 bytes
47 : compression method 2 bytes
48 : last mod file time 2 bytes
49 : last mod file date 2 bytes
50 : crc-32 4 bytes
51 : compressed size 4 bytes
52 : uncompressed size 4 bytes
53 : file name length 2 bytes
54 : extra field length 2 bytes
55 :
56 : file name (variable size)
57 : extra field (variable size)
58 :
59 : *******************************************************************************/
60 :
61 : #define LFH_HEADER_SIZE 30
62 :
63 : #define LFH_SIGNATURE 0x04034b50
64 : #define LFH_FILE_NAME_LENGTH 26
65 : #define LFH_EXTRA_FIELD_LENGTH 28
66 :
67 : struct lfh {
68 : u2 compressionmethod;
69 : u4 compressedsize;
70 : u4 uncompressedsize;
71 : u2 filenamelength;
72 : u2 extrafieldlength;
73 : };
74 :
75 : /* hashtable_zipfile_entry ****************************************************/
76 :
77 6429257 : struct ZipFileEntry {
78 : Utf8String filename;
79 : u2 compressionmethod;
80 : u4 compressedsize;
81 : u4 uncompressedsize;
82 : u1 *data;
83 :
84 : /***
85 : * Load data from zipped file into memory
86 : *
87 : * `dst' must have room for `uncompressedsize' bytes.
88 : */
89 : void get(uint8_t *dst) const;
90 :
91 : /// interface to HashTable
92 1253961 : size_t hash() const { return filename.hash(); }
93 :
94 18137242 : Utf8String key() const { return filename; }
95 : void set_key(Utf8String u) { filename = u; }
96 : };
97 :
98 : class ZipFile {
99 : typedef HashTable<InsertOnlyNamedEntry<ZipFileEntry> > Table;
100 : public:
101 : typedef Table::Iterator Iterator;
102 : typedef Table::EntryRef EntryRef;
103 :
104 : /// Load zip archive
105 : static ZipFile *open(const char *path);
106 :
107 : /// Find file in zip archive
108 33506 : EntryRef find(Utf8String filename) { return table.find(filename); }
109 :
110 : /// Allows iteration over all entries in zip archive
111 0 : Iterator begin() { return table.begin(); }
112 0 : Iterator end() { return table.end(); }
113 : private:
114 164 : ZipFile(size_t capacity) : table(capacity) {}
115 :
116 : Table table;
117 : };
118 :
119 : #endif // ENABLE_ZLIB
120 :
121 : #endif // ZIP_HPP_
122 :
123 :
124 : /*
125 : * These are local overrides for various environment variables in Emacs.
126 : * Please do not remove this and leave it at the end of the file, where
127 : * Emacs will automagically detect them.
128 : * ---------------------------------------------------------------------
129 : * Local variables:
130 : * mode: c++
131 : * indent-tabs-mode: t
132 : * c-basic-offset: 4
133 : * tab-width: 4
134 : * End:
135 : * vim:noexpandtab:sw=4:ts=4:
136 : */
|