~ubuntu-branches/ubuntu/trusty/advancecomp/trusty

« back to all changes in this revision

Viewing changes to zip.h

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ozarowski
  • Date: 2006-05-13 21:15:49 UTC
  • Revision ID: james.westby@ubuntu.com-20060513211549-2vu7peis643ojcm5
Tags: upstream-1.15
ImportĀ upstreamĀ versionĀ 1.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Advance project.
 
3
 *
 
4
 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Andrea Mazzoleni
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details. 
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#ifndef __ZIP_H
 
22
#define __ZIP_H
 
23
 
 
24
#include "except.h"
 
25
 
 
26
#ifdef USE_COMPRESS
 
27
#include "compress.h"
 
28
#endif
 
29
 
 
30
#include <list>
 
31
#include <sstream>
 
32
 
 
33
// --------------------------------------------------------------------------
 
34
// Zip format
 
35
 
 
36
// Methods
 
37
#define ZIP_METHOD_STORE 0x00
 
38
#define ZIP_METHOD_SHRUNK 0x01
 
39
#define ZIP_METHOD_REDUCE1 0x02
 
40
#define ZIP_METHOD_REDUCE2 0x03
 
41
#define ZIP_METHOD_REDUCE3 0x04
 
42
#define ZIP_METHOD_REDUCE4 0x05
 
43
#define ZIP_METHOD_IMPLODE 0x06
 
44
#define ZIP_METHOD_TOKENIZE 0x07
 
45
#define ZIP_METHOD_DEFLATE 0x08
 
46
 
 
47
// Not standard methods
 
48
#define ZIP_METHOD_ENHDEFLATE 0x09
 
49
#define ZIP_METHOD_BZIP2 0x0C
 
50
#define ZIP_METHOD_LZMA 0x0F
 
51
 
 
52
// Generic flags
 
53
// If bit 0 is set, indicates that the file is encrypted.
 
54
#define ZIP_GEN_FLAGS_ENCRYPTED 0x01
 
55
 
 
56
// Compression method for deflate
 
57
#define ZIP_GEN_FLAGS_DEFLATE_NORMAL 0x00
 
58
#define ZIP_GEN_FLAGS_DEFLATE_MAXIMUM 0x02
 
59
#define ZIP_GEN_FLAGS_DEFLATE_FAST 0x04
 
60
#define ZIP_GEN_FLAGS_DEFLATE_SUPERFAST 0x06
 
61
#define ZIP_GEN_FLAGS_DEFLATE_MASK 0x06
 
62
 
 
63
// If bit 3 is set, the fields crc-32, compressed size
 
64
// and uncompressed size are set to zero in the local
 
65
// header. The correct values are put in the data descriptor
 
66
// immediately following the compressed data and in the central directory.
 
67
#define ZIP_GEN_FLAGS_DEFLATE_ZERO 0x08
 
68
 
 
69
// If bit 1 is set indicates an 8K sliding dictionary was used.
 
70
// If clear, then a 4K sliding dictionary was used.
 
71
// If bit 2 is set, indicates an 3 Shannon-Fano trees were used
 
72
// to encode the sliding dictionary output. If clear, then 2
 
73
// Shannon-Fano trees were used.
 
74
#define ZIP_GEN_FLAGS_IMPLODE_4KD2T 0x00
 
75
#define ZIP_GEN_FLAGS_IMPLODE_8KD2T 0x02
 
76
#define ZIP_GEN_FLAGS_IMPLODE_4KD3T 0x04
 
77
#define ZIP_GEN_FLAGS_IMPLODE_8KD3T 0x06
 
78
#define ZIP_GEN_FLAGS_IMPLODE_MASK 0x06
 
79
 
 
80
// Internal file attributes
 
81
// If set, that the file is apparently an ASCII or text file.
 
82
#define ZIP_INT_ATTR_TEXT 0x01
 
83
 
 
84
// Signature
 
85
#define ZIP_L_signature 0x04034b50
 
86
#define ZIP_C_signature 0x02014b50
 
87
#define ZIP_E_signature 0x06054b50
 
88
 
 
89
// Offsets in end of central directory structure
 
90
#define ZIP_EO_end_of_central_dir_signature 0x00
 
91
#define ZIP_EO_number_of_this_disk 0x04
 
92
#define ZIP_EO_number_of_disk_start_cent_dir 0x06
 
93
#define ZIP_EO_total_entries_cent_dir_this_disk 0x08
 
94
#define ZIP_EO_total_entries_cent_dir 0x0A
 
95
#define ZIP_EO_size_of_cent_dir 0x0C
 
96
#define ZIP_EO_offset_to_start_of_cent_dir 0x10
 
97
#define ZIP_EO_zipfile_comment_length 0x14
 
98
#define ZIP_EO_FIXED 0x16 // size of fixed data structure
 
99
#define ZIP_EO_zipfile_comment 0x16
 
100
 
 
101
// Offsets in central directory entry structure
 
102
#define ZIP_CO_central_file_header_signature 0x00
 
103
#define ZIP_CO_version_made_by 0x04
 
104
#define ZIP_CO_host_os 0x05
 
105
#define ZIP_CO_version_needed_to_extract 0x06
 
106
#define ZIP_CO_os_needed_to_extract 0x07
 
107
#define ZIP_CO_general_purpose_bit_flag 0x08
 
108
#define ZIP_CO_compression_method 0x0A
 
109
#define ZIP_CO_last_mod_file_time 0x0C
 
110
#define ZIP_CO_last_mod_file_date 0x0E
 
111
#define ZIP_CO_crc32 0x10
 
112
#define ZIP_CO_compressed_size 0x14
 
113
#define ZIP_CO_uncompressed_size 0x18
 
114
#define ZIP_CO_filename_length 0x1C
 
115
#define ZIP_CO_extra_field_length 0x1E
 
116
#define ZIP_CO_file_comment_length 0x20
 
117
#define ZIP_CO_disk_number_start 0x22
 
118
#define ZIP_CO_internal_file_attrib 0x24
 
119
#define ZIP_CO_external_file_attrib 0x26
 
120
#define ZIP_CO_relative_offset_of_local_header 0x2A
 
121
#define ZIP_CO_FIXED 0x2E // size of fixed data structure
 
122
#define ZIP_CO_filename 0x2E
 
123
 
 
124
// Offsets in data descriptor structure
 
125
#define ZIP_DO_crc32 0x00
 
126
#define ZIP_DO_compressed_size 0x04
 
127
#define ZIP_DO_uncompressed_size 0x08
 
128
#define ZIP_DO_FIXED 0x0C // size of fixed data structure
 
129
 
 
130
// Offsets in local file header structure
 
131
#define ZIP_LO_local_file_header_signature 0x00
 
132
#define ZIP_LO_version_needed_to_extract 0x04
 
133
#define ZIP_LO_os_needed_to_extract 0x05
 
134
#define ZIP_LO_general_purpose_bit_flag 0x06
 
135
#define ZIP_LO_compression_method 0x08
 
136
#define ZIP_LO_last_mod_file_time 0x0A
 
137
#define ZIP_LO_last_mod_file_date 0x0C
 
138
#define ZIP_LO_crc32 0x0E
 
139
#define ZIP_LO_compressed_size 0x12
 
140
#define ZIP_LO_uncompressed_size 0x16
 
141
#define ZIP_LO_filename_length 0x1A
 
142
#define ZIP_LO_extra_field_length 0x1C
 
143
#define ZIP_LO_FIXED 0x1E // size of fixed data structure
 
144
#define ZIP_LO_filename 0x1E
 
145
 
 
146
void time2zip(time_t tod, unsigned& date, unsigned& time);
 
147
 
 
148
time_t zip2time(unsigned date, unsigned time);
 
149
 
 
150
class zip;
 
151
 
 
152
class zip_entry {
 
153
public:
 
154
        enum method_t {
 
155
                unknown,
 
156
                store, shrunk, reduce1, reduce2, reduce3, reduce4,
 
157
                implode_4kdict_2tree, implode_8kdict_2tree, implode_4kdict_3tree, implode_8kdict_3tree,
 
158
                deflate0, deflate1, deflate2, deflate3, deflate4, deflate5, deflate6, deflate7, deflate8, deflate9,
 
159
                bzip2, lzma
 
160
        };
 
161
private:
 
162
        struct {
 
163
                unsigned version_made_by;
 
164
                unsigned host_os;
 
165
                unsigned version_needed_to_extract;
 
166
                unsigned os_needed_to_extract;
 
167
                unsigned general_purpose_bit_flag;
 
168
                unsigned compression_method;
 
169
                unsigned last_mod_file_time;
 
170
                unsigned last_mod_file_date;
 
171
                unsigned crc32;
 
172
                unsigned compressed_size;
 
173
                unsigned uncompressed_size;
 
174
                unsigned filename_length;
 
175
                unsigned central_extra_field_length;
 
176
                unsigned local_extra_field_length;
 
177
                unsigned file_comment_length;
 
178
                unsigned internal_file_attrib;
 
179
                unsigned external_file_attrib;
 
180
                unsigned relative_offset_of_local_header;
 
181
        } info;
 
182
 
 
183
        std::string parent_name; // parent
 
184
        unsigned char* file_name;
 
185
        unsigned char* file_comment;
 
186
        unsigned char* local_extra_field;
 
187
        unsigned char* central_extra_field;
 
188
        unsigned char* data;
 
189
 
 
190
        void check_cent(const unsigned char* buf) const;
 
191
        void check_local(const unsigned char* buf) const;
 
192
        void check_descriptor(const unsigned char* buf) const;
 
193
 
 
194
        zip_entry();
 
195
        zip_entry& operator=(const zip_entry&);
 
196
        bool operator==(const zip_entry&) const;
 
197
        bool operator!=(const zip_entry&) const;
 
198
 
 
199
public:
 
200
        zip_entry(const zip& Aparent);
 
201
        zip_entry(const zip_entry& A);
 
202
        ~zip_entry();
 
203
 
 
204
        void load_local(const unsigned char* buf, FILE* f, unsigned size);
 
205
        void save_local(FILE* f);
 
206
        void load_cent(const unsigned char* buf, unsigned& skip);
 
207
        void save_cent(FILE* f);
 
208
        void unload();
 
209
 
 
210
        method_t method_get() const;
 
211
        void set(method_t method, const std::string& name, const unsigned char* compdata, unsigned compsize, unsigned size, unsigned crc, unsigned date, unsigned time, bool is_text);
 
212
 
 
213
        unsigned compressed_size_get() const { return info.compressed_size; }
 
214
        unsigned uncompressed_size_get() const { return info.uncompressed_size; }
 
215
        unsigned crc_get() const { return info.crc32; }
 
216
        bool is_text() const;
 
217
 
 
218
        void compressed_seek(FILE* f) const;
 
219
        void compressed_read(unsigned char* outdata) const;
 
220
        void uncompressed_read(unsigned char* outdata) const;
 
221
 
 
222
        const std::string& parentname_get() const { return parent_name; }
 
223
        void name_set(const std::string& Aname);
 
224
        std::string name_get() const;
 
225
        unsigned offset_get() const { return info.relative_offset_of_local_header; }
 
226
 
 
227
        unsigned zipdate_get() const { return info.last_mod_file_date; }
 
228
        unsigned ziptime_get() const { return info.last_mod_file_time; }
 
229
        time_t time_get() const;
 
230
        void time_set(time_t tod);
 
231
 
 
232
#ifdef USE_COMPRESS
 
233
        bool shrink(bool standard, shrink_t level);
 
234
#endif
 
235
 
 
236
        void test() const;
 
237
};
 
238
 
 
239
typedef std::list<zip_entry> zip_entry_list;
 
240
 
 
241
class zip {
 
242
        struct {
 
243
                bool open; // zip is opened
 
244
                bool read; // zip is loaded (valid only if flag_open==true)
 
245
                bool modify; // zip is modified (valid only if flag_read==true)
 
246
        } flag;
 
247
 
 
248
        struct {
 
249
                unsigned offset_to_start_of_cent_dir;
 
250
                unsigned zipfile_comment_length;
 
251
        } info;
 
252
 
 
253
        unsigned char* zipfile_comment;
 
254
        zip_entry_list map;
 
255
        std::string path;
 
256
 
 
257
        zip& operator=(const zip&);
 
258
        bool operator==(const zip&) const;
 
259
        bool operator!=(const zip&) const;
 
260
 
 
261
        void skip_local(const unsigned char* buf, FILE* f);
 
262
 
 
263
        static bool pedantic;
 
264
 
 
265
        friend class zip_entry;
 
266
public:
 
267
        static void pedantic_set(bool Apedantic) { pedantic = Apedantic; }
 
268
 
 
269
        zip(const std::string& Apath);
 
270
        zip(const zip& A);
 
271
        ~zip();
 
272
 
 
273
        typedef zip_entry_list::const_iterator const_iterator;
 
274
        typedef zip_entry_list::iterator iterator;
 
275
 
 
276
        const_iterator begin() const { assert(flag.open); return map.begin(); }
 
277
        const_iterator end() const { assert(flag.open); return map.end(); }
 
278
        iterator begin() { assert(flag.open); return map.begin(); }
 
279
        iterator end() { assert(flag.open); return map.end(); }
 
280
 
 
281
        unsigned size() const { assert(flag.open); return map.size(); }
 
282
        unsigned size_not_zero() const;
 
283
        bool empty() const { return size_not_zero() == 0; }
 
284
 
 
285
        std::string file_get() const { return path; }
 
286
 
 
287
        void open();
 
288
        void create();
 
289
        void close();
 
290
        void reopen();
 
291
        void save();
 
292
        void load();
 
293
        void unload();
 
294
 
 
295
        bool is_open() const { return flag.open; }
 
296
        bool is_load() const { assert(flag.open); return flag.read; }
 
297
        bool is_modify() const { assert(flag.open && flag.read); return flag.modify; }
 
298
 
 
299
        void erase(iterator i);
 
300
        void rename(iterator i, const std::string& Aname);
 
301
 
 
302
        iterator insert(const zip_entry& A, const std::string& Aname);
 
303
        iterator insert_uncompressed(const std::string& Aname, const unsigned char* data, unsigned size, unsigned crc, time_t tod, bool is_text);
 
304
 
 
305
#ifdef USE_COMPRESS
 
306
        void shrink(bool standard, shrink_t level);
 
307
#endif
 
308
 
 
309
        void test() const;
 
310
};
 
311
 
 
312
#endif
 
313