Debian Codegraph @

modules/file/gwyzip-minizip2.h

raw symbols SCIP
1 /*
2 * $Id: gwyzip-minizip2.h 29403 2026-01-29 17:37:40Z yeti-dn $
3 * Copyright (C) 2015-2024 David Necas (Yeti).
4 * E-mail: [email protected].
5 *
6 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
8 * later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 */
17
18 /* Do not include this file directly, let gwyzip.h include it as needed! */
19
20 #ifndef __GWY_FILE_MINIZIP2_H__
21 #define __GWY_FILE_MINIZIP2_H__
22
23 /* This either includes stdint.h or replicates its typedefs. */
24 #if defined(HAVE_MINIZIP_MZ_H)
25 #include <minizip/mz.h>
26 #include <minizip/mz_zip.h>
27 #include <minizip/mz_strm.h>
28 #include <minizip/mz_strm_mem.h>
29 #elif defined(HAVE_MZ_H)
30 #include <mz.h>
31 #include <mz_zip.h>
32 #include <mz_strm.h>
33 #include <mz_strm_mem.h>
34 #endif
35 /* If we have neither things break, but then I looked everywhere and I still have no fucking idea where minizip put
36 * its headers this time. */
37
38 #define GWYZIP_HAS_MEMORY_FILES 1
39
40 struct _GwyZipFile {
41 GMappedFile *mfile;
42 void *mem_stream;
43 void *zip_handle;
44 };
45
46 G_GNUC_UNUSED
47 static void
48 gwyzip_minizip_free(GwyZipFile zipfile)
49 {
50 if (zipfile->zip_handle) {
51 mz_zip_close(zipfile->zip_handle);
52 mz_zip_delete(&zipfile->zip_handle);
53 }
54 if (zipfile->mem_stream)
55 mz_stream_mem_delete(&zipfile->mem_stream);
56 if (zipfile->mfile)
57 g_mapped_file_free(zipfile->mfile);
58 g_free(zipfile);
59 }
60
61 G_GNUC_UNUSED
62 static void
63 err_MINIZIP(int32_t status, GError **error)
64 {
65 gchar buf[12];
66
67 g_snprintf(buf, sizeof(buf), "%d", -status);
68 g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_IO,
69 _("%s error while reading the zip file: %s."), "Minizip", buf);
70 }
71
72 G_GNUC_UNUSED
73 static GwyZipFile
74 gwyzip_open_memory(const guchar *data, gsize size, GError **error)
75 {
76 struct _GwyZipFile *zipfile;
77 GError *err = NULL;
78 int32_t status;
79
80 zipfile = g_new0(struct _GwyZipFile, 1);
81
82 mz_zip_create(&zipfile->zip_handle);
83 mz_stream_mem_create(&zipfile->mem_stream);
84 mz_stream_mem_set_buffer(zipfile->mem_stream, data, size);
85 mz_stream_open(zipfile->mem_stream, NULL, MZ_OPEN_MODE_READ);
86 status = mz_zip_open(zipfile->zip_handle, zipfile->mem_stream, MZ_OPEN_MODE_READ);
87 if (status != MZ_OK) {
88 err_MINIZIP(status, error);
89 gwyzip_minizip_free(zipfile);
90 return NULL;
91 }
92
93 return zipfile;
94 }
95
96 /* We do not want minizip doing I/O itself, in particular in MS Windows. Just mmap the input file and give the
97 * library a memory stream... */
98 G_GNUC_UNUSED
99 static GwyZipFile
100 gwyzip_open(const gchar *path, GError **error)
101 {
102 struct _GwyZipFile *zipfile;
103 GMappedFile *mfile;
104 GError *err = NULL;
105
106 if (!(mfile = g_mapped_file_new(path, FALSE, &err))) {
107 /* err_GET_FILE_CONTENTS() */
108 g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_IO,
109 _("Cannot read file contents: %s"), err->message);
110 g_clear_error(&err);
111 g_mapped_file_free(mfile);
112 return NULL;
113 }
114
115 if (!(zipfile = gwyzip_open_memory(g_mapped_file_get_contents(mfile), g_mapped_file_get_length(mfile), error))) {
116 g_mapped_file_free(mfile);
117 return NULL;
118 }
119
120 zipfile->mfile = mfile;
121 return zipfile;
122 }
123
124 G_GNUC_UNUSED
125 static void
126 gwyzip_close(GwyZipFile zipfile)
127 {
128 gwyzip_minizip_free(zipfile);
129 }
130
131 G_GNUC_UNUSED
132 static gboolean
133 gwyzip_next_file(GwyZipFile zipfile, GError **error)
134 {
135 int32_t status;
136 if ((status = mz_zip_goto_next_entry(zipfile->zip_handle)) == MZ_OK)
137 return TRUE;
138 err_MINIZIP(status, error);
139 return FALSE;
140 }
141
142 G_GNUC_UNUSED
143 static gboolean
144 gwyzip_first_file(GwyZipFile zipfile, GError **error)
145 {
146 int32_t status;
147 if ((status = mz_zip_goto_first_entry(zipfile->zip_handle)) == MZ_OK)
148 return TRUE;
149 err_MINIZIP(status, error);
150 return FALSE;
151 }
152
153 /* Pass non-NULL for the attributes you are interested in...
154 * XXX: I am not sure if file_info is guaranteed to survive closing the entry, so I am not going to try using it
155 * afterwards. */
156 G_GNUC_UNUSED
157 static gboolean
158 gwyminizip_get_common_file_info(GwyZipFile zipfile, gchar **filename, gsize *uncomp_size, GError **error)
159 {
160 mz_zip_file *file_info = NULL;
161 int32_t status;
162
163 status = mz_zip_entry_read_open(zipfile->zip_handle, 0, NULL);
164 if (status != MZ_OK) {
165 err_MINIZIP(status, error);
166 return FALSE;
167 }
168
169 status = mz_zip_entry_get_info(zipfile->zip_handle, &file_info);
170 if (status != MZ_OK) {
171 err_MINIZIP(status, error);
172 mz_zip_entry_close(zipfile->zip_handle);
173 return FALSE;
174 }
175
176 if (filename)
177 *filename = g_strdup(file_info->filename);
178 if (uncomp_size)
179 *uncomp_size = file_info->uncompressed_size;
180
181 /* No minizip code example seems to free file_info in any manner. So hopefully it points to static storage... */
182
183 return TRUE;
184 }
185
186 /* This function returns the file name as UTF-8. The others probably don't. */
187 G_GNUC_UNUSED
188 static gboolean
189 gwyzip_get_current_filename(GwyZipFile zipfile, gchar **filename, GError **error)
190 {
191 gchar *fnm;
192
193 if (gwyminizip_get_common_file_info(zipfile, &fnm, NULL, error)) {
194 *filename = fnm;
195 return TRUE;
196 }
197 *filename = NULL;
198 return FALSE;
199 }
200
201 G_GNUC_UNUSED
202 static gboolean
203 gwyzip_locate_file(GwyZipFile zipfile, const gchar *filename, gint casesens, GError **error)
204 {
205 int32_t status;
206
207 /* Negate the last argument; it is called ignore_case. */
208 status = mz_zip_locate_entry(zipfile->zip_handle, filename, !casesens);
209 if (status != MZ_OK) {
210 g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_IO,
211 _("File %s is missing in the zip file."), filename);
212 return FALSE;
213 }
214 return TRUE;
215 }
216
217 G_GNUC_UNUSED
218 static guchar*
219 gwyzip_get_file_content(GwyZipFile zipfile, gsize *contentsize, GError **error)
220 {
221 gsize uncsize, remsize;
222 guchar *buffer;
223 int32_t status;
224
225 if (!gwyminizip_get_common_file_info(zipfile, NULL, &uncsize, error))
226 return NULL;
227
228 status = mz_zip_entry_read_open(zipfile->zip_handle, 0, NULL);
229 if (status != MZ_OK) {
230 err_MINIZIP(status, error);
231 return NULL;
232 }
233
234 buffer = g_new(guchar, uncsize + 1);
235 remsize = uncsize;
236 while (remsize) {
237 int32_t bytes_to_read = (remsize > (guint)G_MAXINT32 ? G_MAXINT32 : remsize);
238 status = mz_zip_entry_read(zipfile->zip_handle, buffer + (uncsize - remsize), bytes_to_read);
239 /* When positive, the return value it is the number of bytes read. Otherwise an error code? Anyway, do not
240 * accept zero. */
241 if (status <= 0) {
242 err_MINIZIP(status, error);
243 g_free(buffer);
244 return NULL;
245 }
246 if (status > bytes_to_read) {
247 /* XXX: What to do when the library does this? */
248 err_MINIZIP(MZ_INTERNAL_ERROR, error);
249 g_free(buffer);
250 return NULL;
251 }
252 /* We read at least one byte. So proceed. */
253 remsize -= status;
254 }
255 buffer[uncsize] = '\0';
256 if (contentsize)
257 *contentsize = uncsize;
258
259 mz_zip_entry_close(zipfile->zip_handle);
260
261 return buffer;
262 }
263
264 #endif
265
266 /* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */