1 #ifndef _HAD_ZIP_H
2 #define _HAD_ZIP_H
3 
4 /*
5   zip.h -- exported declarations.
6   Copyright (C) 1999-2012 Dieter Baron and Thomas Klausner
7 
8   This file is part of libzip, a library to manipulate ZIP archives.
9   The authors can be contacted at <libzip@nih.at>
10 
11   Redistribution and use in source and binary forms, with or without
12   modification, are permitted provided that the following conditions
13   are met:
14   1. Redistributions of source code must retain the above copyright
15      notice, this list of conditions and the following disclaimer.
16   2. Redistributions in binary form must reproduce the above copyright
17      notice, this list of conditions and the following disclaimer in
18      the documentation and/or other materials provided with the
19      distribution.
20   3. The names of the authors may not be used to endorse or promote
21      products derived from this software without specific prior
22      written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
25   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
28   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 
38 
39 #if 0
40 # ifndef ZIP_EXTERN
41 #  ifdef __WINDOWS__
42 #   define ZIP_EXTERN __declspec(dllimport)
43 #  elif defined(__GNUC__) && __GNUC__ >= 4
44 #   define ZIP_EXTERN __attribute__ ((visibility ("default")))
45 #  else
46 #   define ZIP_EXTERN
47 #  endif
48 # endif
49 #endif
50 #ifndef ZIP_EXTERN
51 #define ZIP_EXTERN extern
52 #endif
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #include <zipconf.h>
59 
60 #include <sys/types.h>
61 #include <stdio.h>
62 #include <time.h>
63 
64 /* flags for zip_open */
65 
66 #define ZIP_CREATE           1
67 #define ZIP_EXCL             2
68 #define ZIP_CHECKCONS        4
69 #define ZIP_TRUNCATE         8
70 
71 
72 /* flags for zip_name_locate, zip_fopen, zip_stat, ... */
73 
74 #define ZIP_FL_NOCASE		1u /* ignore case on name lookup */
75 #define ZIP_FL_NODIR		2u /* ignore directory component */
76 #define ZIP_FL_COMPRESSED	4u /* read compressed data */
77 #define ZIP_FL_UNCHANGED	8u /* use original data, ignoring changes */
78 #define ZIP_FL_RECOMPRESS      16u /* force recompression of data */
79 #define ZIP_FL_ENCRYPTED       32u /* read encrypted data (implies ZIP_FL_COMPRESSED) */
80 #define ZIP_FL_ENC_GUESS        0u /* guess string encoding (is default) */
81 #define ZIP_FL_ENC_RAW         64u /* get unmodified string */
82 #define ZIP_FL_ENC_STRICT     128u /* follow specification strictly */
83 #define ZIP_FL_LOCAL	      256u /* in local header */
84 #define ZIP_FL_CENTRAL	      512u /* in central directory */
85 /*                           1024u    reserved for internal use */
86 #define ZIP_FL_ENC_UTF_8     2048u /* string is UTF-8 encoded */
87 #define ZIP_FL_ENC_CP437     4096u /* string is CP437 encoded */
88 #define ZIP_FL_OVERWRITE     8192u /* zip_file_add: if file with name exists, overwrite (replace) it */
89 
90 /* archive global flags */
91 
92 #define ZIP_AFL_TORRENT		1u /* torrent zipped */
93 #define ZIP_AFL_RDONLY		2u /* read only -- cannot be cleared */
94 
95 
96 /* create a new extra field */
97 
98 #define ZIP_EXTRA_FIELD_ALL	ZIP_UINT16_MAX
99 #define ZIP_EXTRA_FIELD_NEW	ZIP_UINT16_MAX
100 
101 /* flags for compression and encryption sources */
102 
103 #define ZIP_CODEC_DECODE	0 /* decompress/decrypt (encode flag not set) */
104 #define ZIP_CODEC_ENCODE	1 /* compress/encrypt */
105 
106 
107 /* libzip error codes */
108 
109 #define ZIP_ER_OK             0  /* N No error */
110 #define ZIP_ER_MULTIDISK      1  /* N Multi-disk zip archives not supported */
111 #define ZIP_ER_RENAME         2  /* S Renaming temporary file failed */
112 #define ZIP_ER_CLOSE          3  /* S Closing zip archive failed */
113 #define ZIP_ER_SEEK           4  /* S Seek error */
114 #define ZIP_ER_READ           5  /* S Read error */
115 #define ZIP_ER_WRITE          6  /* S Write error */
116 #define ZIP_ER_CRC            7  /* N CRC error */
117 #define ZIP_ER_ZIPCLOSED      8  /* N Containing zip archive was closed */
118 #define ZIP_ER_NOENT          9  /* N No such file */
119 #define ZIP_ER_EXISTS        10  /* N File already exists */
120 #define ZIP_ER_OPEN          11  /* S Can't open file */
121 #define ZIP_ER_TMPOPEN       12  /* S Failure to create temporary file */
122 #define ZIP_ER_ZLIB          13  /* Z Zlib error */
123 #define ZIP_ER_MEMORY        14  /* N Malloc failure */
124 #define ZIP_ER_CHANGED       15  /* N Entry has been changed */
125 #define ZIP_ER_COMPNOTSUPP   16  /* N Compression method not supported */
126 #define ZIP_ER_EOF           17  /* N Premature EOF */
127 #define ZIP_ER_INVAL         18  /* N Invalid argument */
128 #define ZIP_ER_NOZIP         19  /* N Not a zip archive */
129 #define ZIP_ER_INTERNAL      20  /* N Internal error */
130 #define ZIP_ER_INCONS        21  /* N Zip archive inconsistent */
131 #define ZIP_ER_REMOVE        22  /* S Can't remove file */
132 #define ZIP_ER_DELETED       23  /* N Entry has been deleted */
133 #define ZIP_ER_ENCRNOTSUPP   24  /* N Encryption method not supported */
134 #define ZIP_ER_RDONLY        25  /* N Read-only archive */
135 #define ZIP_ER_NOPASSWD      26  /* N No password provided */
136 #define ZIP_ER_WRONGPASSWD   27  /* N Wrong password provided */
137 
138 /* type of system error value */
139 
140 #define ZIP_ET_NONE	      0  /* sys_err unused */
141 #define ZIP_ET_SYS	      1  /* sys_err is errno */
142 #define ZIP_ET_ZLIB	      2  /* sys_err is zlib error code */
143 
144 /* compression methods */
145 
146 #define ZIP_CM_DEFAULT	      -1  /* better of deflate or store */
147 #define ZIP_CM_STORE	       0  /* stored (uncompressed) */
148 #define ZIP_CM_SHRINK	       1  /* shrunk */
149 #define ZIP_CM_REDUCE_1	       2  /* reduced with factor 1 */
150 #define ZIP_CM_REDUCE_2	       3  /* reduced with factor 2 */
151 #define ZIP_CM_REDUCE_3	       4  /* reduced with factor 3 */
152 #define ZIP_CM_REDUCE_4	       5  /* reduced with factor 4 */
153 #define ZIP_CM_IMPLODE	       6  /* imploded */
154 /* 7 - Reserved for Tokenizing compression algorithm */
155 #define ZIP_CM_DEFLATE	       8  /* deflated */
156 #define ZIP_CM_DEFLATE64       9  /* deflate64 */
157 #define ZIP_CM_PKWARE_IMPLODE 10  /* PKWARE imploding */
158 /* 11 - Reserved by PKWARE */
159 #define ZIP_CM_BZIP2          12  /* compressed using BZIP2 algorithm */
160 /* 13 - Reserved by PKWARE */
161 #define ZIP_CM_LZMA	      14  /* LZMA (EFS) */
162 /* 15-17 - Reserved by PKWARE */
163 #define ZIP_CM_TERSE	      18  /* compressed using IBM TERSE (new) */
164 #define ZIP_CM_LZ77           19  /* IBM LZ77 z Architecture (PFS) */
165 #define ZIP_CM_WAVPACK	      97  /* WavPack compressed data */
166 #define ZIP_CM_PPMD	      98  /* PPMd version I, Rev 1 */
167 
168 /* encryption methods */
169 
170 #define ZIP_EM_NONE	       0  /* not encrypted */
171 #define ZIP_EM_TRAD_PKWARE     1  /* traditional PKWARE encryption */
172 #if 0 /* Strong Encryption Header not parsed yet */
173 #define ZIP_EM_DES        0x6601  /* strong encryption: DES */
174 #define ZIP_EM_RC2_OLD    0x6602  /* strong encryption: RC2, version < 5.2 */
175 #define ZIP_EM_3DES_168   0x6603
176 #define ZIP_EM_3DES_112   0x6609
177 #define ZIP_EM_AES_128    0x660e
178 #define ZIP_EM_AES_192    0x660f
179 #define ZIP_EM_AES_256    0x6610
180 #define ZIP_EM_RC2        0x6702  /* strong encryption: RC2, version >= 5.2 */
181 #define ZIP_EM_RC4        0x6801
182 #endif
183 #define ZIP_EM_UNKNOWN    0xffff  /* unknown algorithm */
184 
185 
186 
187 enum zip_source_cmd {
188     ZIP_SOURCE_OPEN,	/* prepare for reading */
189     ZIP_SOURCE_READ, 	/* read data */
190     ZIP_SOURCE_CLOSE,	/* reading is done */
191     ZIP_SOURCE_STAT,	/* get meta information */
192     ZIP_SOURCE_ERROR,	/* get error information */
193     ZIP_SOURCE_FREE	/* cleanup and free resources */
194 };
195 
196 #define ZIP_SOURCE_ERR_LOWER	-2
197 
198 #define ZIP_STAT_NAME			0x0001u
199 #define ZIP_STAT_INDEX			0x0002u
200 #define ZIP_STAT_SIZE			0x0004u
201 #define ZIP_STAT_COMP_SIZE		0x0008u
202 #define ZIP_STAT_MTIME			0x0010u
203 #define ZIP_STAT_CRC			0x0020u
204 #define ZIP_STAT_COMP_METHOD		0x0040u
205 #define ZIP_STAT_ENCRYPTION_METHOD	0x0080u
206 #define ZIP_STAT_FLAGS			0x0100u
207 
208 struct zip_stat {
209     zip_uint64_t valid;			/* which fields have valid values */
210     const char *name;			/* name of the file */
211     zip_uint64_t index;			/* index within archive */
212     zip_uint64_t size;			/* size of file (uncompressed) */
213     zip_uint64_t comp_size;		/* size of file (compressed) */
214     time_t mtime;			/* modification time */
215     zip_uint32_t crc;			/* crc of file data */
216     zip_uint16_t comp_method;		/* compression method used */
217     zip_uint16_t encryption_method;	/* encryption method used */
218     zip_uint32_t flags;			/* reserved for future use */
219 };
220 
221 struct zip;
222 struct zip_file;
223 struct zip_source;
224 
225 typedef zip_uint32_t zip_flags_t;
226 
227 typedef zip_int64_t (*zip_source_callback)(void *, void *, zip_uint64_t,
228 					   enum zip_source_cmd);
229 
230 
231 
232 #ifndef ZIP_DISABLE_DEPRECATED
233 ZIP_EXTERN zip_int64_t zip_add(struct zip *, const char *, struct zip_source *); /* use zip_file_add */
234 ZIP_EXTERN zip_int64_t zip_add_dir(struct zip *, const char *); /* use zip_dir_add */
235 ZIP_EXTERN const char *zip_get_file_comment(struct zip *, zip_uint64_t, int *, int); /* use zip_file_get_comment */
236 ZIP_EXTERN int zip_get_num_files(struct zip *);  /* use zip_get_num_entries instead */
237 ZIP_EXTERN int zip_rename(struct zip *, zip_uint64_t, const char *); /* use zip_file_rename */
238 ZIP_EXTERN int zip_replace(struct zip *, zip_uint64_t, struct zip_source *); /* use zip_file_replace */
239 ZIP_EXTERN int zip_set_file_comment(struct zip *, zip_uint64_t, const char *, int); /* use zip_file_set_comment */
240 #endif
241 
242 ZIP_EXTERN int zip_archive_set_tempdir(struct zip *, const char *);
243 ZIP_EXTERN zip_int64_t zip_file_add(struct zip *, const char *, struct zip_source *, zip_flags_t);
244 ZIP_EXTERN zip_int64_t zip_dir_add(struct zip *, const char *, zip_flags_t);
245 ZIP_EXTERN int zip_close(struct zip *);
246 ZIP_EXTERN void zip_discard(struct zip *);
247 ZIP_EXTERN int zip_delete(struct zip *, zip_uint64_t);
248 ZIP_EXTERN int zip_file_extra_field_delete(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t);
249 ZIP_EXTERN int zip_file_extra_field_delete_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_flags_t);
250 ZIP_EXTERN void zip_error_clear(struct zip *);
251 ZIP_EXTERN void zip_error_get(struct zip *, int *, int *);
252 ZIP_EXTERN int zip_error_get_sys_type(int);
253 ZIP_EXTERN int zip_error_to_str(char *, zip_uint64_t, int, int);
254 ZIP_EXTERN int zip_fclose(struct zip_file *);
255 ZIP_EXTERN struct zip *zip_fdopen(int, int, int *);
256 ZIP_EXTERN void zip_file_error_clear(struct zip_file *);
257 ZIP_EXTERN void zip_file_error_get(struct zip_file *, int *, int *);
258 ZIP_EXTERN const char *zip_file_strerror(struct zip_file *);
259 ZIP_EXTERN struct zip_file *zip_fopen(struct zip *, const char *, zip_flags_t);
260 ZIP_EXTERN struct zip_file *zip_fopen_encrypted(struct zip *, const char *, zip_flags_t, const char *);
261 ZIP_EXTERN struct zip_file *zip_fopen_index(struct zip *, zip_uint64_t, zip_flags_t);
262 ZIP_EXTERN struct zip_file *zip_fopen_index_encrypted(struct zip *, zip_uint64_t, zip_flags_t, const char *);
263 ZIP_EXTERN zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t);
264 ZIP_EXTERN const char *zip_get_archive_comment(struct zip *, int *, zip_flags_t);
265 ZIP_EXTERN int zip_get_archive_flag(struct zip *, zip_flags_t, zip_flags_t);
266 ZIP_EXTERN const char *zip_file_get_comment(struct zip *, zip_uint64_t, zip_uint32_t *, zip_flags_t);
267 ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t *, zip_uint16_t *, zip_flags_t);
268 ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_uint16_t *, zip_flags_t);
269 ZIP_EXTERN zip_int16_t zip_file_extra_fields_count(struct zip *, zip_uint64_t, zip_flags_t);
270 ZIP_EXTERN zip_int16_t zip_file_extra_fields_count_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t);
271 ZIP_EXTERN const char *zip_get_name(struct zip *, zip_uint64_t, zip_flags_t);
272 ZIP_EXTERN zip_int64_t zip_get_num_entries(struct zip *, zip_flags_t);
273 ZIP_EXTERN zip_int64_t zip_name_locate(struct zip *, const char *, zip_flags_t);
274 ZIP_EXTERN struct zip *zip_open(const char *, int, int *);
275 ZIP_EXTERN int zip_file_rename(struct zip *, zip_uint64_t, const char *, zip_flags_t);
276 ZIP_EXTERN int zip_file_replace(struct zip *, zip_uint64_t, struct zip_source *, zip_flags_t);
277 ZIP_EXTERN int zip_set_archive_comment(struct zip *, const char *, zip_uint16_t);
278 ZIP_EXTERN int zip_set_archive_flag(struct zip *, zip_flags_t, int);
279 ZIP_EXTERN int zip_set_default_password(struct zip *, const char *);
280 ZIP_EXTERN int zip_file_set_comment(struct zip *, zip_uint64_t, const char *, zip_uint16_t, zip_flags_t);
281 ZIP_EXTERN int zip_set_file_compression(struct zip *, zip_uint64_t, zip_int32_t, zip_uint32_t);
282 ZIP_EXTERN int zip_file_extra_field_set(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, const zip_uint8_t *, zip_uint16_t, zip_flags_t);
283 ZIP_EXTERN struct zip_source *zip_source_buffer(struct zip *, const void *, zip_uint64_t, int);
284 ZIP_EXTERN struct zip_source *zip_source_file(struct zip *, const char *, zip_uint64_t, zip_int64_t);
285 ZIP_EXTERN struct zip_source *zip_source_filep(struct zip *, FILE *, zip_uint64_t, zip_int64_t);
286 ZIP_EXTERN void zip_source_free(struct zip_source *);
287 ZIP_EXTERN struct zip_source *zip_source_function(struct zip *, zip_source_callback, void *);
288 ZIP_EXTERN struct zip_source *zip_source_zip(struct zip *, struct zip *, zip_uint64_t, zip_flags_t, zip_uint64_t, zip_int64_t);
289 ZIP_EXTERN int zip_stat(struct zip *, const char *, zip_flags_t, struct zip_stat *);
290 ZIP_EXTERN int zip_stat_index(struct zip *, zip_uint64_t, zip_flags_t, struct zip_stat *);
291 ZIP_EXTERN void zip_stat_init(struct zip_stat *);
292 ZIP_EXTERN const char *zip_strerror(struct zip *);
293 ZIP_EXTERN int zip_unchange(struct zip *, zip_uint64_t);
294 ZIP_EXTERN int zip_unchange_all(struct zip *);
295 ZIP_EXTERN int zip_unchange_archive(struct zip *);
296 
297 #ifdef __cplusplus
298 }
299 #endif
300 
301 #endif /* _HAD_ZIP_H */
302