added support for opening level set zip files in Android version
[rocksndiamonds.git] / src / libgame / zip / ioapi.c
1 /* ioapi.c -- 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    Modifications for Zip64 support
7      Copyright (C) 2009-2010 Mathias Svensson
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 #include <stdlib.h>
17 #include <string.h>
18
19 #if defined unix || defined __APPLE__
20 #include <sys/types.h>
21 #include <unistd.h>
22 #endif
23
24 #include "ioapi.h"
25
26 #if defined(_WIN32)
27 #  define snprintf _snprintf
28 #endif
29
30 voidpf call_zopen64(const zlib_filefunc64_32_def *pfilefunc, const void *filename, int mode)
31 {
32     if (pfilefunc->zfile_func64.zopen64_file != NULL)
33         return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque, filename, mode);
34     return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque, (const char*)filename, mode);
35 }
36
37 voidpf call_zopendisk64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, uint32_t number_disk, int mode)
38 {
39     if (pfilefunc->zfile_func64.zopendisk64_file != NULL)
40         return (*(pfilefunc->zfile_func64.zopendisk64_file)) (pfilefunc->zfile_func64.opaque, filestream, number_disk, mode);
41     return (*(pfilefunc->zopendisk32_file))(pfilefunc->zfile_func64.opaque, filestream, number_disk, mode);
42 }
43
44 long call_zseek64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, uint64_t offset, int origin)
45 {
46     uint32_t offset_truncated = 0;
47     if (pfilefunc->zfile_func64.zseek64_file != NULL)
48         return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque, filestream, offset, origin);
49     offset_truncated = (uint32_t)offset;
50     if (offset_truncated != offset)
51         return -1;
52     return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque, filestream, offset_truncated, origin);
53 }
54
55 uint64_t call_ztell64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream)
56 {
57     uint64_t position;
58     if (pfilefunc->zfile_func64.zseek64_file != NULL)
59         return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque, filestream);
60     position = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque, filestream);
61     if ((position) == UINT32_MAX)
62         return (uint64_t)-1;
63     return position;
64 }
65
66 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def *p_filefunc64_32, const zlib_filefunc_def *p_filefunc32)
67 {
68     p_filefunc64_32->zfile_func64.zopen64_file = NULL;
69     p_filefunc64_32->zfile_func64.zopendisk64_file = NULL;
70     p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
71     p_filefunc64_32->zopendisk32_file = p_filefunc32->zopendisk_file;
72     p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
73     p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
74     p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
75     p_filefunc64_32->zfile_func64.ztell64_file = NULL;
76     p_filefunc64_32->zfile_func64.zseek64_file = NULL;
77     p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
78     p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
79     p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
80     p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
81     p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
82 }
83
84 static uint32_t ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uint32_t size);
85 static uint32_t ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void *buf, uint32_t size);
86 static uint64_t ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream);
87 static long     ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, uint64_t offset, int origin);
88 static int      ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream);
89 static int      ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream);
90
91 typedef struct
92 {
93     FILE *file;
94     int filenameLength;
95     void *filename;
96 } FILE_IOPOSIX;
97
98 static voidpf file_build_ioposix(FILE *file, const char *filename)
99 {
100     FILE_IOPOSIX *ioposix = NULL;
101     if (file == NULL)
102         return NULL;
103     ioposix = (FILE_IOPOSIX*)malloc(sizeof(FILE_IOPOSIX));
104     ioposix->file = file;
105     ioposix->filenameLength = (int)strlen(filename) + 1;
106     ioposix->filename = (char*)malloc(ioposix->filenameLength * sizeof(char));
107     strncpy((char*)ioposix->filename, filename, ioposix->filenameLength);
108     return (voidpf)ioposix;
109 }
110
111 static voidpf ZCALLBACK fopen64_file_func(ZIP_UNUSED voidpf opaque, const void *filename, int mode)
112 {
113     FILE* file = NULL;
114     const char *mode_fopen = NULL;
115     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
116         mode_fopen = "rb";
117     else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
118         mode_fopen = "r+b";
119     else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
120         mode_fopen = "wb";
121
122     if ((filename != NULL) && (mode_fopen != NULL))
123     {
124         const char *fd_prefix = "fd:";
125         if (strncmp(filename, fd_prefix, strlen(fd_prefix)) == 0)
126             file = fdopen(dup(atoi(&filename[strlen(fd_prefix)])), mode_fopen);
127         else
128             file = fopen64((const char*)filename, mode_fopen);
129         return file_build_ioposix(file, (const char*)filename);
130     }
131     return file;
132 }
133
134 static voidpf ZCALLBACK fopendisk64_file_func(voidpf opaque, voidpf stream, uint32_t number_disk, int mode)
135 {
136     FILE_IOPOSIX *ioposix = NULL;
137     char *diskFilename = NULL;
138     voidpf ret = NULL;
139     int i = 0;
140
141     if (stream == NULL)
142         return NULL;
143     ioposix = (FILE_IOPOSIX*)stream;
144     diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
145     strncpy(diskFilename, (const char*)ioposix->filename, ioposix->filenameLength);
146     for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
147     {
148         if (diskFilename[i] != '.')
149             continue;
150         snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02u", number_disk + 1);
151         break;
152     }
153     if (i >= 0)
154         ret = fopen64_file_func(opaque, diskFilename, mode);
155     free(diskFilename);
156     return ret;
157 }
158
159 static uint32_t ZCALLBACK fread_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, void* buf, uint32_t size)
160 {
161     FILE_IOPOSIX *ioposix = NULL;
162     uint32_t read = (uint32_t)-1;
163     if (stream == NULL)
164         return read;
165     ioposix = (FILE_IOPOSIX*)stream;
166     read = (uint32_t)fread(buf, 1, (size_t)size, ioposix->file);
167     return read;
168 }
169
170 static uint32_t ZCALLBACK fwrite_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, const void *buf, uint32_t size)
171 {
172     FILE_IOPOSIX *ioposix = NULL;
173     uint32_t written = (uint32_t)-1;
174     if (stream == NULL)
175         return written;
176     ioposix = (FILE_IOPOSIX*)stream;
177     written = (uint32_t)fwrite(buf, 1, (size_t)size, ioposix->file);
178     return written;
179 }
180
181 static uint64_t ZCALLBACK ftell64_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
182 {
183     FILE_IOPOSIX *ioposix = NULL;
184     uint64_t ret = (uint64_t)-1;
185     if (stream == NULL)
186         return ret;
187     ioposix = (FILE_IOPOSIX*)stream;
188     ret = ftello64(ioposix->file);
189     return ret;
190 }
191
192 static long ZCALLBACK fseek64_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, uint64_t offset, int origin)
193 {
194     FILE_IOPOSIX *ioposix = NULL;
195     int fseek_origin = 0;
196     long ret = 0;
197
198     if (stream == NULL)
199         return -1;
200     ioposix = (FILE_IOPOSIX*)stream;
201
202     switch (origin)
203     {
204         case ZLIB_FILEFUNC_SEEK_CUR:
205             fseek_origin = SEEK_CUR;
206             break;
207         case ZLIB_FILEFUNC_SEEK_END:
208             fseek_origin = SEEK_END;
209             break;
210         case ZLIB_FILEFUNC_SEEK_SET:
211             fseek_origin = SEEK_SET;
212             break;
213         default:
214             return -1;
215     }
216
217     if (fseeko64(ioposix->file, offset, fseek_origin) != 0)
218         ret = -1;
219
220     return ret;
221 }
222
223 static int ZCALLBACK fclose_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
224 {
225     FILE_IOPOSIX *ioposix = NULL;
226     int ret = -1;
227     if (stream == NULL)
228         return ret;
229     ioposix = (FILE_IOPOSIX*)stream;
230     if (ioposix->filename != NULL)
231         free(ioposix->filename);
232     ret = fclose(ioposix->file);
233     free(ioposix);
234     return ret;
235 }
236
237 static int ZCALLBACK ferror_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
238 {
239     FILE_IOPOSIX *ioposix = NULL;
240     int ret = -1;
241     if (stream == NULL)
242         return ret;
243     ioposix = (FILE_IOPOSIX*)stream;
244     ret = ferror(ioposix->file);
245     return ret;
246 }
247
248 void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
249 {
250     pzlib_filefunc_def->zopen64_file = fopen64_file_func;
251     pzlib_filefunc_def->zopendisk64_file = fopendisk64_file_func;
252     pzlib_filefunc_def->zread_file = fread_file_func;
253     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
254     pzlib_filefunc_def->ztell64_file = ftell64_file_func;
255     pzlib_filefunc_def->zseek64_file = fseek64_file_func;
256     pzlib_filefunc_def->zclose_file = fclose_file_func;
257     pzlib_filefunc_def->zerror_file = ferror_file_func;
258     pzlib_filefunc_def->opaque = NULL;
259 }