added generic support for listing and extracting zip files (using minizip)
[rocksndiamonds.git] / src / libgame / zip / ioapi.h
1 /* ioapi.h -- IO base function header for compress/uncompress .zip
2    part of the MiniZip project
3
4    Copyright (C) 2012-2017 Nathan Moinvaziri
5      https://github.com/nmoinvaz/minizip
6    Copyright (C) 2009-2010 Mathias Svensson
7      Modifications for Zip64 support
8      http://result42.com
9    Copyright (C) 1998-2010 Gilles Vollant
10      http://www.winimage.com/zLibDll/minizip.html
11
12    This program is distributed under the terms of the same license as zlib.
13    See the accompanying LICENSE file for the full text of the license.
14 */
15
16 #ifndef _ZLIBIOAPI64_H
17 #define _ZLIBIOAPI64_H
18
19 #if defined(__linux__)
20 // Linux needs this to support file operation on files larger then 4+GB
21 #  ifndef __USE_FILE_OFFSET64
22 #  define __USE_FILE_OFFSET64
23 #  endif
24 #  ifndef __USE_LARGEFILE64
25 #  define __USE_LARGEFILE64
26 #  endif
27 #  ifndef _LARGEFILE64_SOURCE
28 #  define _LARGEFILE64_SOURCE
29 #  endif
30 #  ifndef _FILE_OFFSET_BIT
31 #  define _FILE_OFFSET_BIT 64
32 #  endif
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38
39 #include "zlib.h"
40
41 #ifdef __GNUC__
42 #  define ZIP_UNUSED __attribute__((__unused__))
43 #else
44 #  define ZIP_UNUSED
45 #endif
46
47 #if defined(USE_FILE32API)
48 #  define fopen64 fopen
49 #  define ftello64 ftell
50 #  define fseeko64 fseek
51 #else
52 #  if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__)
53 #    define fopen64 fopen
54 #    define ftello64 ftello
55 #    define fseeko64 fseeko
56 #  endif
57 #  ifdef _MSC_VER
58 #    define fopen64 fopen
59 #    if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
60 #      define ftello64 _ftelli64
61 #      define fseeko64 _fseeki64
62 #    else /* old MSC */
63 #      define ftello64 ftell
64 #      define fseeko64 fseek
65 #    endif
66 #  endif
67 #endif
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 #define ZLIB_FILEFUNC_SEEK_CUR (1)
74 #define ZLIB_FILEFUNC_SEEK_END (2)
75 #define ZLIB_FILEFUNC_SEEK_SET (0)
76
77 #define ZLIB_FILEFUNC_MODE_READ             (1)
78 #define ZLIB_FILEFUNC_MODE_WRITE            (2)
79 #define ZLIB_FILEFUNC_MODE_READWRITEFILTER  (3)
80 #define ZLIB_FILEFUNC_MODE_EXISTING         (4)
81 #define ZLIB_FILEFUNC_MODE_CREATE           (8)
82
83 #ifndef ZCALLBACK
84 #  if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || \
85        defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK)
86 #    define ZCALLBACK CALLBACK
87 #  else
88 #    define ZCALLBACK
89 #  endif
90 #endif
91
92 typedef voidpf   (ZCALLBACK *open_file_func)     (voidpf opaque, const char *filename, int mode);
93 typedef voidpf   (ZCALLBACK *opendisk_file_func) (voidpf opaque, voidpf stream, uint32_t number_disk, int mode);
94 typedef uint32_t (ZCALLBACK *read_file_func)     (voidpf opaque, voidpf stream, void* buf, uint32_t size);
95 typedef uint32_t (ZCALLBACK *write_file_func)    (voidpf opaque, voidpf stream, const void *buf, uint32_t size);
96 typedef int      (ZCALLBACK *close_file_func)    (voidpf opaque, voidpf stream);
97 typedef int      (ZCALLBACK *error_file_func)    (voidpf opaque, voidpf stream);
98
99 typedef long     (ZCALLBACK *tell_file_func)     (voidpf opaque, voidpf stream);
100 typedef long     (ZCALLBACK *seek_file_func)     (voidpf opaque, voidpf stream, uint32_t offset, int origin);
101
102 /* here is the "old" 32 bits structure structure */
103 typedef struct zlib_filefunc_def_s
104 {
105     open_file_func      zopen_file;
106     opendisk_file_func  zopendisk_file;
107     read_file_func      zread_file;
108     write_file_func     zwrite_file;
109     tell_file_func      ztell_file;
110     seek_file_func      zseek_file;
111     close_file_func     zclose_file;
112     error_file_func     zerror_file;
113     voidpf              opaque;
114 } zlib_filefunc_def;
115
116 typedef uint64_t (ZCALLBACK *tell64_file_func)    (voidpf opaque, voidpf stream);
117 typedef long     (ZCALLBACK *seek64_file_func)    (voidpf opaque, voidpf stream, uint64_t offset, int origin);
118 typedef voidpf   (ZCALLBACK *open64_file_func)    (voidpf opaque, const void *filename, int mode);
119 typedef voidpf   (ZCALLBACK *opendisk64_file_func)(voidpf opaque, voidpf stream, uint32_t number_disk, int mode);
120
121 typedef struct zlib_filefunc64_def_s
122 {
123     open64_file_func     zopen64_file;
124     opendisk64_file_func zopendisk64_file;
125     read_file_func       zread_file;
126     write_file_func      zwrite_file;
127     tell64_file_func     ztell64_file;
128     seek64_file_func     zseek64_file;
129     close_file_func      zclose_file;
130     error_file_func      zerror_file;
131     voidpf               opaque;
132 } zlib_filefunc64_def;
133
134 void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
135 void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
136
137 /* now internal definition, only for zip.c and unzip.h */
138 typedef struct zlib_filefunc64_32_def_s
139 {
140     zlib_filefunc64_def zfile_func64;
141     open_file_func      zopen32_file;
142     opendisk_file_func  zopendisk32_file;
143     tell_file_func      ztell32_file;
144     seek_file_func      zseek32_file;
145 } zlib_filefunc64_32_def;
146
147 #define ZREAD64(filefunc,filestream,buf,size)       ((*((filefunc).zfile_func64.zread_file))        ((filefunc).zfile_func64.opaque,filestream,buf,size))
148 #define ZWRITE64(filefunc,filestream,buf,size)      ((*((filefunc).zfile_func64.zwrite_file))       ((filefunc).zfile_func64.opaque,filestream,buf,size))
149 /*#define ZTELL64(filefunc,filestream)                ((*((filefunc).ztell64_file))                   ((filefunc).opaque,filestream))*/
150 /*#define ZSEEK64(filefunc,filestream,pos,mode)       ((*((filefunc).zseek64_file))                   ((filefunc).opaque,filestream,pos,mode))*/
151 #define ZCLOSE64(filefunc,filestream)               ((*((filefunc).zfile_func64.zclose_file))       ((filefunc).zfile_func64.opaque,filestream))
152 #define ZERROR64(filefunc,filestream)               ((*((filefunc).zfile_func64.zerror_file))       ((filefunc).zfile_func64.opaque,filestream))
153
154 voidpf   call_zopen64(const zlib_filefunc64_32_def *pfilefunc,const void*filename, int mode);
155 voidpf   call_zopendisk64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, uint32_t number_disk, int mode);
156 long     call_zseek64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, uint64_t offset, int origin);
157 uint64_t call_ztell64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream);
158
159 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def *p_filefunc64_32, const zlib_filefunc_def *p_filefunc32);
160
161 #define ZOPEN64(filefunc,filename,mode)             (call_zopen64((&(filefunc)),(filename),(mode)))
162 #define ZOPENDISK64(filefunc,filestream,diskn,mode) (call_zopendisk64((&(filefunc)),(filestream),(diskn),(mode)))
163 #define ZTELL64(filefunc,filestream)                (call_ztell64((&(filefunc)),(filestream)))
164 #define ZSEEK64(filefunc,filestream,pos,mode)       (call_zseek64((&(filefunc)),(filestream),(pos),(mode)))
165
166 #ifdef __cplusplus
167 }
168 #endif
169
170 #endif