zip.h 15 KB

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