2
* This file is part of the Advance project.
4
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Andrea Mazzoleni
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.
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.
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.
33
// --------------------------------------------------------------------------
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
47
// Not standard methods
48
#define ZIP_METHOD_ENHDEFLATE 0x09
49
#define ZIP_METHOD_BZIP2 0x0C
50
#define ZIP_METHOD_LZMA 0x0F
53
// If bit 0 is set, indicates that the file is encrypted.
54
#define ZIP_GEN_FLAGS_ENCRYPTED 0x01
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
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
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
80
// Internal file attributes
81
// If set, that the file is apparently an ASCII or text file.
82
#define ZIP_INT_ATTR_TEXT 0x01
85
#define ZIP_L_signature 0x04034b50
86
#define ZIP_C_signature 0x02014b50
87
#define ZIP_E_signature 0x06054b50
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
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
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
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
146
void time2zip(time_t tod, unsigned& date, unsigned& time);
148
time_t zip2time(unsigned date, unsigned time);
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,
163
unsigned version_made_by;
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;
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;
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;
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;
195
zip_entry& operator=(const zip_entry&);
196
bool operator==(const zip_entry&) const;
197
bool operator!=(const zip_entry&) const;
200
zip_entry(const zip& Aparent);
201
zip_entry(const zip_entry& A);
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);
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);
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;
218
void compressed_seek(FILE* f) const;
219
void compressed_read(unsigned char* outdata) const;
220
void uncompressed_read(unsigned char* outdata) const;
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; }
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);
233
bool shrink(bool standard, shrink_t level);
239
typedef std::list<zip_entry> zip_entry_list;
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)
249
unsigned offset_to_start_of_cent_dir;
250
unsigned zipfile_comment_length;
253
unsigned char* zipfile_comment;
257
zip& operator=(const zip&);
258
bool operator==(const zip&) const;
259
bool operator!=(const zip&) const;
261
void skip_local(const unsigned char* buf, FILE* f);
263
static bool pedantic;
265
friend class zip_entry;
267
static void pedantic_set(bool Apedantic) { pedantic = Apedantic; }
269
zip(const std::string& Apath);
273
typedef zip_entry_list::const_iterator const_iterator;
274
typedef zip_entry_list::iterator iterator;
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(); }
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; }
285
std::string file_get() const { return path; }
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; }
299
void erase(iterator i);
300
void rename(iterator i, const std::string& Aname);
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);
306
void shrink(bool standard, shrink_t level);