added optional button to restart game (door, panel and touch variants)
[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         file = fopen64((const char*)filename, mode_fopen);
125         return file_build_ioposix(file, (const char*)filename);
126     }
127     return file;
128 }
129
130 static voidpf ZCALLBACK fopendisk64_file_func(voidpf opaque, voidpf stream, uint32_t number_disk, int mode)
131 {
132     FILE_IOPOSIX *ioposix = NULL;
133     char *diskFilename = NULL;
134     voidpf ret = NULL;
135     int i = 0;
136
137     if (stream == NULL)
138         return NULL;
139     ioposix = (FILE_IOPOSIX*)stream;
140     diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
141     strncpy(diskFilename, (const char*)ioposix->filename, ioposix->filenameLength);
142     for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
143     {
144         if (diskFilename[i] != '.')
145             continue;
146         snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02u", number_disk + 1);
147         break;
148     }
149     if (i >= 0)
150         ret = fopen64_file_func(opaque, diskFilename, mode);
151     free(diskFilename);
152     return ret;
153 }
154
155 static uint32_t ZCALLBACK fread_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, void* buf, uint32_t size)
156 {
157     FILE_IOPOSIX *ioposix = NULL;
158     uint32_t read = (uint32_t)-1;
159     if (stream == NULL)
160         return read;
161     ioposix = (FILE_IOPOSIX*)stream;
162     read = (uint32_t)fread(buf, 1, (size_t)size, ioposix->file);
163     return read;
164 }
165
166 static uint32_t ZCALLBACK fwrite_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, const void *buf, uint32_t size)
167 {
168     FILE_IOPOSIX *ioposix = NULL;
169     uint32_t written = (uint32_t)-1;
170     if (stream == NULL)
171         return written;
172     ioposix = (FILE_IOPOSIX*)stream;
173     written = (uint32_t)fwrite(buf, 1, (size_t)size, ioposix->file);
174     return written;
175 }
176
177 static uint64_t ZCALLBACK ftell64_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
178 {
179     FILE_IOPOSIX *ioposix = NULL;
180     uint64_t ret = (uint64_t)-1;
181     if (stream == NULL)
182         return ret;
183     ioposix = (FILE_IOPOSIX*)stream;
184     ret = ftello64(ioposix->file);
185     return ret;
186 }
187
188 static long ZCALLBACK fseek64_file_func(ZIP_UNUSED voidpf opaque, voidpf stream, uint64_t offset, int origin)
189 {
190     FILE_IOPOSIX *ioposix = NULL;
191     int fseek_origin = 0;
192     long ret = 0;
193
194     if (stream == NULL)
195         return -1;
196     ioposix = (FILE_IOPOSIX*)stream;
197
198     switch (origin)
199     {
200         case ZLIB_FILEFUNC_SEEK_CUR:
201             fseek_origin = SEEK_CUR;
202             break;
203         case ZLIB_FILEFUNC_SEEK_END:
204             fseek_origin = SEEK_END;
205             break;
206         case ZLIB_FILEFUNC_SEEK_SET:
207             fseek_origin = SEEK_SET;
208             break;
209         default:
210             return -1;
211     }
212
213     if (fseeko64(ioposix->file, offset, fseek_origin) != 0)
214         ret = -1;
215
216     return ret;
217 }
218
219 static int ZCALLBACK fclose_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
220 {
221     FILE_IOPOSIX *ioposix = NULL;
222     int ret = -1;
223     if (stream == NULL)
224         return ret;
225     ioposix = (FILE_IOPOSIX*)stream;
226     if (ioposix->filename != NULL)
227         free(ioposix->filename);
228     ret = fclose(ioposix->file);
229     free(ioposix);
230     return ret;
231 }
232
233 static int ZCALLBACK ferror_file_func(ZIP_UNUSED voidpf opaque, voidpf stream)
234 {
235     FILE_IOPOSIX *ioposix = NULL;
236     int ret = -1;
237     if (stream == NULL)
238         return ret;
239     ioposix = (FILE_IOPOSIX*)stream;
240     ret = ferror(ioposix->file);
241     return ret;
242 }
243
244 void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
245 {
246     pzlib_filefunc_def->zopen64_file = fopen64_file_func;
247     pzlib_filefunc_def->zopendisk64_file = fopendisk64_file_func;
248     pzlib_filefunc_def->zread_file = fread_file_func;
249     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
250     pzlib_filefunc_def->ztell64_file = ftell64_file_func;
251     pzlib_filefunc_def->zseek64_file = fseek64_file_func;
252     pzlib_filefunc_def->zclose_file = fclose_file_func;
253     pzlib_filefunc_def->zerror_file = ferror_file_func;
254     pzlib_filefunc_def->opaque = NULL;
255 }