unzip.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
  19. /* like the STRICT of WIN32, we define a pointer that cannot be converted
  20. from (void*) without cast */
  21. typedef struct TagunzFile__ { int unused; } unzFile__;
  22. typedef unzFile__ *unzFile;
  23. #else
  24. typedef void* unzFile;
  25. #endif
  26. /* tm_unz contain date/time info */
  27. typedef struct tm_unz_s
  28. {
  29. unsigned int tm_sec; /* seconds after the minute - [0,59] */
  30. unsigned int tm_min; /* minutes after the hour - [0,59] */
  31. unsigned int tm_hour; /* hours since midnight - [0,23] */
  32. unsigned int tm_mday; /* day of the month - [1,31] */
  33. unsigned int tm_mon; /* months since January - [0,11] */
  34. unsigned int tm_year; /* years - [1980..2044] */
  35. } tm_unz;
  36. /* unz_global_info structure contain global data about the ZIPfile
  37. These data comes from the end of central dir */
  38. typedef struct unz_global_info_s
  39. {
  40. unsigned long number_entry; /* total number of entries in the central dir on this disk */
  41. unsigned long size_comment; /* size of the global comment of the zipfile */
  42. } unz_global_info;
  43. /* unz_file_info contain information about a file in the zipfile */
  44. typedef struct unz_file_info_s
  45. {
  46. unsigned long version; /* version made by 2 unsigned chars */
  47. unsigned long version_needed; /* version needed to extract 2 unsigned chars */
  48. unsigned long flag; /* general purpose bit flag 2 unsigned chars */
  49. unsigned long compression_method; /* compression method 2 unsigned chars */
  50. unsigned long dosDate; /* last mod file date in Dos fmt 4 unsigned chars */
  51. unsigned long crc; /* crc-32 4 unsigned chars */
  52. unsigned long compressed_size; /* compressed size 4 unsigned chars */
  53. unsigned long uncompressed_size; /* uncompressed size 4 unsigned chars */
  54. unsigned long size_filename; /* filename length 2 unsigned chars */
  55. unsigned long size_file_extra; /* extra field length 2 unsigned chars */
  56. unsigned long size_file_comment; /* file comment length 2 unsigned chars */
  57. unsigned long disk_num_start; /* disk number start 2 unsigned chars */
  58. unsigned long internal_fa; /* internal file attributes 2 unsigned chars */
  59. unsigned long external_fa; /* external file attributes 4 unsigned chars */
  60. tm_unz tmu_date;
  61. } unz_file_info;
  62. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  63. typedef struct unz_file_info_internal_s
  64. {
  65. unsigned long offset_curfile;/* relative offset of static header 4 unsigned chars */
  66. } unz_file_info_internal;
  67. typedef void* (*alloc_func) (void* opaque, unsigned int items, unsigned int size);
  68. typedef void (*free_func) (void* opaque, void* address);
  69. struct internal_state;
  70. typedef struct z_stream_s {
  71. unsigned char *next_in; /* next input unsigned char */
  72. unsigned int avail_in; /* number of unsigned chars available at next_in */
  73. unsigned long total_in; /* total nb of input unsigned chars read so */
  74. unsigned char *next_out; /* next output unsigned char should be put there */
  75. unsigned int avail_out; /* remaining free space at next_out */
  76. unsigned long total_out; /* total nb of unsigned chars output so */
  77. char *msg; /* last error message, NULL if no error */
  78. struct internal_state *state; /* not visible by applications */
  79. alloc_func zalloc; /* used to allocate the internal state */
  80. free_func zfree; /* used to free the internal state */
  81. unsigned char* opaque; /* private data object passed to zalloc and zfree */
  82. int data_type; /* best guess about the data type: ascii or binary */
  83. unsigned long adler; /* adler32 value of the uncompressed data */
  84. unsigned long reserved; /* reserved for future use */
  85. } z_stream;
  86. typedef z_stream *z_streamp;
  87. /* file_in_zip_read_info_s contain internal information about a file in zipfile,
  88. when reading and decompress it */
  89. typedef struct
  90. {
  91. char *read_buffer; /* internal buffer for compressed data */
  92. z_stream stream; /* zLib stream structure for inflate */
  93. unsigned long pos_in_zipfile; /* position in unsigned char on the zipfile, for fseek*/
  94. unsigned long stream_initialised; /* flag set if stream structure is initialised*/
  95. unsigned long offset_local_extrafield;/* offset of the static extra field */
  96. unsigned int size_local_extrafield;/* size of the static extra field */
  97. unsigned long pos_local_extrafield; /* position in the static extra field in read*/
  98. unsigned long crc32; /* crc32 of all data uncompressed */
  99. unsigned long crc32_wait; /* crc32 we must obtain after decompress all */
  100. unsigned long rest_read_compressed; /* number of unsigned char to be decompressed */
  101. unsigned long rest_read_uncompressed;/*number of unsigned char to be obtained after decomp*/
  102. FILE* file; /* io structore of the zipfile */
  103. unsigned long compression_method; /* compression method (0==store) */
  104. unsigned long byte_before_the_zipfile;/* unsigned char before the zipfile, (>0 for sfx)*/
  105. } file_in_zip_read_info_s;
  106. /* unz_s contain internal information about the zipfile
  107. */
  108. typedef struct
  109. {
  110. FILE* file; /* io structore of the zipfile */
  111. unz_global_info gi; /* public global information */
  112. unsigned long byte_before_the_zipfile;/* unsigned char before the zipfile, (>0 for sfx)*/
  113. unsigned long num_file; /* number of the current file in the zipfile*/
  114. unsigned long pos_in_central_dir; /* pos of the current file in the central dir*/
  115. unsigned long current_file_ok; /* flag about the usability of the current file*/
  116. unsigned long central_pos; /* position of the beginning of the central dir*/
  117. unsigned long size_central_dir; /* size of the central directory */
  118. unsigned long offset_central_dir; /* offset of start of central directory with
  119. respect to the starting disk number */
  120. unz_file_info cur_file_info; /* public info about the current file in zip*/
  121. unz_file_info_internal cur_file_info_internal; /* private info about it*/
  122. file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
  123. file if we are decompressing it */
  124. unsigned char* tmpFile;
  125. int tmpPos,tmpSize;
  126. } unz_s;
  127. #define UNZ_OK (0)
  128. #define UNZ_END_OF_LIST_OF_FILE (-100)
  129. #define UNZ_ERRNO (Z_ERRNO)
  130. #define UNZ_EOF (0)
  131. #define UNZ_PARAMERROR (-102)
  132. #define UNZ_BADZIPFILE (-103)
  133. #define UNZ_INTERNALERROR (-104)
  134. #define UNZ_CRCERROR (-105)
  135. #define UNZ_CASESENSITIVE 1
  136. #define UNZ_NOTCASESENSITIVE 2
  137. #define UNZ_OSDEFAULTCASE 0
  138. extern int unzStringFileNameCompare (const char* fileName1, const char* fileName2, int iCaseSensitivity);
  139. /*
  140. Compare two filename (fileName1,fileName2).
  141. If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
  142. If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
  143. or strcasecmp)
  144. If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
  145. (like 1 on Unix, 2 on Windows)
  146. */
  147. extern unzFile unzOpen (const char *path);
  148. extern unzFile unzReOpen (const char* path, unzFile file);
  149. /*
  150. Open a Zip file. path contain the full pathname (by example,
  151. on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer
  152. "zlib/zlib111.zip".
  153. If the zipfile cannot be opened (file don't exist or in not valid), the
  154. return value is NULL.
  155. Else, the return value is a unzFile Handle, usable with other function
  156. of this unzip package.
  157. */
  158. extern int unzClose (unzFile file);
  159. /*
  160. Close a ZipFile opened with unzipOpen.
  161. If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
  162. these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  163. return UNZ_OK if there is no problem. */
  164. extern int unzGetGlobalInfo (unzFile file, unz_global_info *pglobal_info);
  165. /*
  166. Write info about the ZipFile in the *pglobal_info structure.
  167. No preparation of the structure is needed
  168. return UNZ_OK if there is no problem. */
  169. extern int unzGetGlobalComment (unzFile file, char *szComment, unsigned long uSizeBuf);
  170. /*
  171. Get the global comment string of the ZipFile, in the szComment buffer.
  172. uSizeBuf is the size of the szComment buffer.
  173. return the number of unsigned char copied or an error code <0
  174. */
  175. /***************************************************************************/
  176. /* Unzip package allow you browse the directory of the zipfile */
  177. extern int unzGoToFirstFile (unzFile file);
  178. /*
  179. Set the current file of the zipfile to the first file.
  180. return UNZ_OK if there is no problem
  181. */
  182. extern int unzGoToNextFile (unzFile file);
  183. /*
  184. Set the current file of the zipfile to the next file.
  185. return UNZ_OK if there is no problem
  186. return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  187. */
  188. extern int unzGetCurrentFileInfoPosition (unzFile file, unsigned long *pos );
  189. /*
  190. Get the position of the info of the current file in the zip.
  191. return UNZ_OK if there is no problem
  192. */
  193. extern int unzSetCurrentFileInfoPosition (unzFile file, unsigned long pos );
  194. /*
  195. Set the position of the info of the current file in the zip.
  196. return UNZ_OK if there is no problem
  197. */
  198. extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity);
  199. /*
  200. Try locate the file szFileName in the zipfile.
  201. For the iCaseSensitivity signification, see unzStringFileNameCompare
  202. return value :
  203. UNZ_OK if the file is found. It becomes the current file.
  204. UNZ_END_OF_LIST_OF_FILE if the file is not found
  205. */
  206. extern int unzGetCurrentFileInfo (unzFile file, unz_file_info *pfile_info, char *szFileName, unsigned long fileNameBufferSize, void *extraField, unsigned long extraFieldBufferSize, char *szComment, unsigned long commentBufferSize);
  207. /*
  208. Get Info about the current file
  209. if pfile_info!=NULL, the *pfile_info structure will contain somes info about
  210. the current file
  211. if szFileName!=NULL, the filemane string will be copied in szFileName
  212. (fileNameBufferSize is the size of the buffer)
  213. if extraField!=NULL, the extra field information will be copied in extraField
  214. (extraFieldBufferSize is the size of the buffer).
  215. This is the Central-header version of the extra field
  216. if szComment!=NULL, the comment string of the file will be copied in szComment
  217. (commentBufferSize is the size of the buffer)
  218. */
  219. /***************************************************************************/
  220. /* for reading the content of the current zipfile, you can open it, read data
  221. from it, and close it (you can close it before reading all the file)
  222. */
  223. extern int unzOpenCurrentFile (unzFile file);
  224. /*
  225. Open for reading data the current file in the zipfile.
  226. If there is no error, the return value is UNZ_OK.
  227. */
  228. extern int unzCloseCurrentFile (unzFile file);
  229. /*
  230. Close the file in zip opened with unzOpenCurrentFile
  231. Return UNZ_CRCERROR if all the file was read but the CRC is not good
  232. */
  233. extern int unzReadCurrentFile (unzFile file, void* buf, unsigned len);
  234. /*
  235. Read unsigned chars from the current file (opened by unzOpenCurrentFile)
  236. buf contain buffer where data must be copied
  237. len the size of buf.
  238. return the number of unsigned char copied if somes unsigned chars are copied
  239. return 0 if the end of file was reached
  240. return <0 with error code if there is an error
  241. (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  242. */
  243. extern long unztell(unzFile file);
  244. /*
  245. Give the current position in uncompressed data
  246. */
  247. extern int unzeof (unzFile file);
  248. /*
  249. return 1 if the end of file was reached, 0 elsewhere
  250. */
  251. extern int unzGetLocalExtrafield (unzFile file, void* buf, unsigned len);
  252. /*
  253. Read extra field from the current file (opened by unzOpenCurrentFile)
  254. This is the local-header version of the extra field (sometimes, there is
  255. more info in the local-header version than in the central-header)
  256. if buf==NULL, it return the size of the local extra field
  257. if buf!=NULL, len is the size of the buffer, the extra header is copied in
  258. buf.
  259. the return value is the number of unsigned chars copied in buf, or (if <0)
  260. the error code
  261. */