CACAO
zip.hpp
Go to the documentation of this file.
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 {
73 };
74 
75 /* hashtable_zipfile_entry ****************************************************/
76 
77 struct ZipFileEntry {
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  size_t hash() const { return filename.hash(); }
93 
94  Utf8String key() const { return filename; }
95  void set_key(Utf8String u) { filename = u; }
96 };
97 
98 class ZipFile {
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  EntryRef find(Utf8String filename) { return table.find(filename); }
109 
110  /// Allows iteration over all entries in zip archive
111  Iterator begin() { return table.begin(); }
112  Iterator end() { return table.end(); }
113 private:
114  ZipFile(size_t capacity) : table(capacity) {}
115 
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  */
Iterator end()
Definition: hashtable.hpp:453
u2 extrafieldlength
Definition: zip.hpp:72
ZipFile(size_t capacity)
Definition: zip.hpp:114
u4 uncompressedsize
Definition: zip.hpp:81
Utf8String filename
Definition: zip.hpp:78
uint8_t u1
Definition: types.hpp:40
size_t hash() const
Definition: utf8.hpp:137
Definition: zip.hpp:98
Table table
Definition: zip.hpp:116
Iterator begin()
Allows iteration over all entries in zip archive.
Definition: zip.hpp:111
void set_key(Utf8String u)
Definition: zip.hpp:95
u1 * data
Definition: zip.hpp:82
uint16_t u2
Definition: types.hpp:43
u2 compressionmethod
Definition: zip.hpp:79
Table::EntryRef EntryRef
Definition: zip.hpp:102
u2 compressionmethod
Definition: zip.hpp:68
Utf8String key() const
Definition: zip.hpp:94
u4 compressedsize
Definition: zip.hpp:80
HashTable< InsertOnlyNamedEntry< ZipFileEntry > > Table
Definition: zip.hpp:99
EntryRef find(Utf8String filename)
Find file in zip archive.
Definition: zip.hpp:108
Definition: zip.hpp:67
Iterator end()
Definition: zip.hpp:112
uint32_t u4
Definition: types.hpp:46
EntryRef find(const T &t)
Definition: hashtable.hpp:215
u4 uncompressedsize
Definition: zip.hpp:70
u4 compressedsize
Definition: zip.hpp:69
size_t hash() const
interface to HashTable
Definition: zip.hpp:92
Iterator begin()
Definition: hashtable.hpp:442
u2 filenamelength
Definition: zip.hpp:71
static ZipFile * open(const char *path)
Load zip archive.
Definition: zip.cpp:160
Table::Iterator Iterator
Definition: zip.hpp:101