| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef __GWY_FILE_MINIZIP2_H__ |
| 21 |
#define __GWY_FILE_MINIZIP2_H__ |
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 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 |
|
| 97 |
|
| 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 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 182 |
|
| 183 |
return TRUE; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 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 |
|
| 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 |
|
| 240 |
|
| 241 |
if (status <= 0) { |
| 242 |
err_MINIZIP(status, error); |
| 243 |
g_free(buffer); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
if (status > bytes_to_read) { |
| 247 |
|
| 248 |
err_MINIZIP(MZ_INTERNAL_ERROR, error); |
| 249 |
g_free(buffer); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 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 |
|