| 1 | /* zip.c -- Backwards compatible zip interface |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | Copyright (C) 1998-2010 Gilles Vollant |
| 7 | https://www.winimage.com/zLibDll/minizip.html |
| 8 | |
| 9 | This program is distributed under the terms of the same license as zlib. |
| 10 | See the accompanying LICENSE file for the full text of the license. |
| 11 | */ |
| 12 | |
| 13 | #include "mz.h" |
| 14 | #include "mz_os.h" |
| 15 | #include "mz_strm.h" |
| 16 | #include "mz_strm_os.h" |
| 17 | #include "mz_zip.h" |
| 18 | |
| 19 | #include "zip.h" |
| 20 | |
| 21 | /***************************************************************************/ |
| 22 | |
| 23 | typedef struct mz_zip_compat_s { |
| 24 | void *stream; |
| 25 | void *handle; |
| 26 | } mz_zip_compat; |
| 27 | |
| 28 | /***************************************************************************/ |
| 29 | |
| 30 | static int32_t zipConvertAppendToStreamMode(int append) { |
| 31 | int32_t mode = MZ_OPEN_MODE_WRITE; |
| 32 | switch (append) { |
| 33 | case APPEND_STATUS_CREATE: |
| 34 | mode |= MZ_OPEN_MODE_CREATE; |
| 35 | break; |
| 36 | case APPEND_STATUS_CREATEAFTER: |
| 37 | mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND; |
| 38 | break; |
| 39 | case APPEND_STATUS_ADDINZIP: |
| 40 | mode |= MZ_OPEN_MODE_READ | MZ_OPEN_MODE_APPEND; |
| 41 | break; |
| 42 | } |
| 43 | return mode; |
| 44 | } |
| 45 | |
| 46 | zipFile zipOpen(const char *path, int append) { |
| 47 | return zipOpen2(path, append, NULL, NULL); |
| 48 | } |
| 49 | |
| 50 | zipFile zipOpen64(const void *path, int append) { |
| 51 | return zipOpen2(path, append, NULL, NULL); |
| 52 | } |
| 53 | |
| 54 | zipFile zipOpen2(const char *path, int append, zipcharpc *globalcomment, zlib_filefunc_def *pzlib_filefunc_def) { |
| 55 | zipFile zip = NULL; |
| 56 | int32_t mode = zipConvertAppendToStreamMode(append); |
| 57 | void *stream = NULL; |
| 58 | |
| 59 | if (pzlib_filefunc_def) { |
| 60 | if (pzlib_filefunc_def->zopen_file) { |
| 61 | stream = mz_stream_ioapi_create(); |
| 62 | if (!stream) |
| 63 | return NULL; |
| 64 | mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def); |
| 65 | } else if (pzlib_filefunc_def->opaque) { |
| 66 | stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque); |
| 67 | if (!stream) |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (!stream) { |
| 73 | stream = mz_stream_os_create(); |
| 74 | if (!stream) |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | if (mz_stream_open(stream, path, mode) != MZ_OK) { |
| 79 | mz_stream_delete(&stream); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | zip = zipOpen_MZ(stream, append, globalcomment); |
| 84 | |
| 85 | if (!zip) { |
| 86 | mz_stream_delete(&stream); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | return zip; |
| 91 | } |
| 92 | |
| 93 | zipFile zipOpen2_64(const void *path, int append, zipcharpc *globalcomment, zlib_filefunc64_def *pzlib_filefunc_def) { |
| 94 | zipFile zip = NULL; |
| 95 | int32_t mode = zipConvertAppendToStreamMode(append); |
| 96 | void *stream = NULL; |
| 97 | |
| 98 | if (pzlib_filefunc_def) { |
| 99 | if (pzlib_filefunc_def->zopen64_file) { |
| 100 | stream = mz_stream_ioapi_create(); |
| 101 | if (!stream) |
| 102 | return NULL; |
| 103 | mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def); |
| 104 | } else if (pzlib_filefunc_def->opaque) { |
| 105 | stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque); |
| 106 | if (!stream) |
| 107 | return NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (!stream) { |
| 112 | stream = mz_stream_os_create(); |
| 113 | if (!stream) |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | if (mz_stream_open(stream, path, mode) != MZ_OK) { |
| 118 | mz_stream_delete(&stream); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | zip = zipOpen_MZ(stream, append, globalcomment); |
| 123 | |
| 124 | if (!zip) { |
| 125 | mz_stream_delete(&stream); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | return zip; |
| 130 | } |
| 131 | |
| 132 | zipFile zipOpen_MZ(void *stream, int append, zipcharpc *globalcomment) { |
| 133 | mz_zip_compat *compat = NULL; |
| 134 | int32_t err = MZ_OK; |
| 135 | int32_t mode = zipConvertAppendToStreamMode(append); |
| 136 | void *handle = NULL; |
| 137 | |
| 138 | handle = mz_zip_create(); |
| 139 | if (!handle) |
| 140 | return NULL; |
| 141 | |
| 142 | err = mz_zip_open(handle, stream, mode); |
| 143 | |
| 144 | if (err != MZ_OK) { |
| 145 | mz_zip_delete(&handle); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | if (globalcomment) |
| 150 | mz_zip_get_comment(handle, globalcomment); |
| 151 | |
| 152 | compat = (mz_zip_compat *)calloc(1, sizeof(mz_zip_compat)); |
| 153 | if (compat) { |
| 154 | compat->handle = handle; |
| 155 | compat->stream = stream; |
| 156 | } else { |
| 157 | mz_zip_delete(&handle); |
| 158 | } |
| 159 | |
| 160 | return (zipFile)compat; |
| 161 | } |
| 162 | |
| 163 | void *zipGetHandle_MZ(zipFile file) { |
| 164 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 165 | if (!compat) |
| 166 | return NULL; |
| 167 | return compat->handle; |
| 168 | } |
| 169 | |
| 170 | void *zipGetStream_MZ(zipFile file) { |
| 171 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 172 | if (!compat) |
| 173 | return NULL; |
| 174 | return (void *)compat->stream; |
| 175 | } |
| 176 | |
| 177 | static time_t zipConvertZipDateToTime(tm_zip tmz_date) { |
| 178 | struct tm tm_date; |
| 179 | memset(&tm_date, 0, sizeof(struct tm)); |
| 180 | memcpy(&tm_date, &tmz_date, sizeof(tm_zip)); |
| 181 | tm_date.tm_year -= 1900; |
| 182 | tm_date.tm_isdst = -1; |
| 183 | return mz_zip_tm_to_time_t(&tm_date); |
| 184 | } |
| 185 | |
| 186 | int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 187 | uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global, |
| 188 | const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel, |
| 189 | int strategy, const char *password, unsigned long crc_for_crypting, |
| 190 | unsigned long version_madeby, unsigned long flag_base, int zip64) { |
| 191 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 192 | mz_zip_file file_info; |
| 193 | |
| 194 | MZ_UNUSED(strategy); |
| 195 | MZ_UNUSED(memLevel); |
| 196 | MZ_UNUSED(windowBits); |
| 197 | MZ_UNUSED(size_extrafield_local); |
| 198 | MZ_UNUSED(extrafield_local); |
| 199 | MZ_UNUSED(crc_for_crypting); |
| 200 | |
| 201 | if (!compat) |
| 202 | return ZIP_PARAMERROR; |
| 203 | |
| 204 | /* The filename and comment length must fit in 16 bits. */ |
| 205 | if (filename && strlen(filename) > 0xffff) |
| 206 | return ZIP_PARAMERROR; |
| 207 | if (comment && strlen(comment) > 0xffff) |
| 208 | return ZIP_PARAMERROR; |
| 209 | |
| 210 | memset(&file_info, 0, sizeof(file_info)); |
| 211 | |
| 212 | if (zipfi) { |
| 213 | if (zipfi->mz_dos_date != 0) |
| 214 | file_info.modified_date = mz_zip_dosdate_to_time_t(zipfi->mz_dos_date); |
| 215 | else |
| 216 | file_info.modified_date = zipConvertZipDateToTime(zipfi->tmz_date); |
| 217 | |
| 218 | file_info.external_fa = (uint32_t)zipfi->external_fa; |
| 219 | file_info.internal_fa = (uint16_t)zipfi->internal_fa; |
| 220 | } |
| 221 | |
| 222 | if (!filename) |
| 223 | filename = "-"; |
| 224 | |
| 225 | file_info.compression_method = (uint16_t)compression_method; |
| 226 | file_info.filename = filename; |
| 227 | /* file_info.extrafield_local = extrafield_local; */ |
| 228 | /* file_info.extrafield_local_size = size_extrafield_local; */ |
| 229 | file_info.extrafield = extrafield_global; |
| 230 | file_info.extrafield_size = size_extrafield_global; |
| 231 | file_info.version_madeby = (uint16_t)version_madeby; |
| 232 | file_info.comment = comment; |
| 233 | if (file_info.comment) |
| 234 | file_info.comment_size = (uint16_t)strlen(file_info.comment); |
| 235 | file_info.flag = (uint16_t)flag_base; |
| 236 | if (zip64) |
| 237 | file_info.zip64 = MZ_ZIP64_FORCE; |
| 238 | else |
| 239 | file_info.zip64 = MZ_ZIP64_DISABLE; |
| 240 | #ifdef HAVE_WZAES |
| 241 | if (password || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED))) |
| 242 | file_info.aes_version = MZ_AES_VERSION; |
| 243 | #endif |
| 244 | |
| 245 | return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password); |
| 246 | } |
| 247 | |
| 248 | int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 249 | uint16_t size_extrafield_local, const void *extrafield_global, |
| 250 | uint16_t size_extrafield_global, const char *comment, int compression_method, int level, |
| 251 | int raw, int windowBits, int memLevel, int strategy, const char *password, |
| 252 | unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base, |
| 253 | int zip64) { |
| 254 | return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 255 | size_extrafield_global, comment, compression_method, level, raw, windowBits, memLevel, |
| 256 | strategy, password, crc_for_crypting, version_madeby, flag_base, zip64); |
| 257 | } |
| 258 | |
| 259 | int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 260 | uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global, |
| 261 | const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel, |
| 262 | int strategy, const char *password, unsigned long crc_for_crypting, |
| 263 | unsigned long version_madeby, unsigned long flag_base) { |
| 264 | return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 265 | size_extrafield_global, comment, compression_method, level, raw, windowBits, |
| 266 | memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0); |
| 267 | } |
| 268 | |
| 269 | int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 270 | uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global, |
| 271 | const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel, |
| 272 | int strategy, const char *password, unsigned long crc_for_crypting) { |
| 273 | return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 274 | size_extrafield_global, comment, compression_method, level, raw, windowBits, |
| 275 | memLevel, strategy, password, crc_for_crypting, 0); |
| 276 | } |
| 277 | |
| 278 | int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 279 | uint16_t size_extrafield_local, const void *extrafield_global, |
| 280 | uint16_t size_extrafield_global, const char *comment, int compression_method, int level, |
| 281 | int raw, int windowBits, int memLevel, int strategy, const char *password, |
| 282 | unsigned long crc_for_crypting, int zip64) { |
| 283 | return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 284 | size_extrafield_global, comment, compression_method, level, raw, windowBits, |
| 285 | memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64); |
| 286 | } |
| 287 | |
| 288 | int zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 289 | uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global, |
| 290 | const char *comment, int compression_method, int level, int raw) { |
| 291 | return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 292 | size_extrafield_global, comment, compression_method, level, raw, 0, 0, 0, NULL, 0, |
| 293 | 0); |
| 294 | } |
| 295 | |
| 296 | int zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 297 | uint16_t size_extrafield_local, const void *extrafield_global, |
| 298 | uint16_t size_extrafield_global, const char *comment, int compression_method, int level, |
| 299 | int raw, int zip64) { |
| 300 | return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 301 | size_extrafield_global, comment, compression_method, level, raw, 0, 0, 0, NULL, 0, |
| 302 | zip64); |
| 303 | } |
| 304 | |
| 305 | int zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 306 | uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global, |
| 307 | const char *comment, int compression_method, int level) { |
| 308 | return zipOpenNewFileInZip_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 309 | size_extrafield_global, comment, compression_method, level, 0); |
| 310 | } |
| 311 | |
| 312 | int zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 313 | uint16_t size_extrafield_local, const void *extrafield_global, |
| 314 | uint16_t size_extrafield_global, const char *comment, int compression_method, int level, |
| 315 | int zip64) { |
| 316 | return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 317 | size_extrafield_global, comment, compression_method, level, 0, zip64); |
| 318 | } |
| 319 | |
| 320 | int zipOpenNewFileInZip_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, |
| 321 | uint16_t size_extrafield_local, const void *extrafield_global, |
| 322 | uint16_t size_extrafield_global, const char *comment, int compression_method, int level, |
| 323 | int zip64) { |
| 324 | return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, |
| 325 | size_extrafield_global, comment, compression_method, level, 0, zip64); |
| 326 | } |
| 327 | |
| 328 | int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len) { |
| 329 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 330 | int32_t written = 0; |
| 331 | if (!compat || len >= INT32_MAX) |
| 332 | return ZIP_PARAMERROR; |
| 333 | written = mz_zip_entry_write(compat->handle, buf, (int32_t)len); |
| 334 | if ((written < 0) || ((uint32_t)written != len)) |
| 335 | return ZIP_ERRNO; |
| 336 | return ZIP_OK; |
| 337 | } |
| 338 | |
| 339 | int zipCloseFileInZipRaw(zipFile file, unsigned long uncompressed_size, unsigned long crc32) { |
| 340 | return zipCloseFileInZipRaw64(file, uncompressed_size, crc32); |
| 341 | } |
| 342 | |
| 343 | int zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, unsigned long crc32) { |
| 344 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 345 | if (!compat) |
| 346 | return ZIP_PARAMERROR; |
| 347 | return mz_zip_entry_close_raw(compat->handle, (int64_t)uncompressed_size, (uint32_t)crc32); |
| 348 | } |
| 349 | |
| 350 | int zipCloseFileInZip(zipFile file) { |
| 351 | return zipCloseFileInZip64(file); |
| 352 | } |
| 353 | |
| 354 | int zipCloseFileInZip64(zipFile file) { |
| 355 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 356 | if (!compat) |
| 357 | return ZIP_PARAMERROR; |
| 358 | return mz_zip_entry_close(compat->handle); |
| 359 | } |
| 360 | |
| 361 | int zipClose(zipFile file, const char *global_comment) { |
| 362 | return zipClose_64(file, global_comment); |
| 363 | } |
| 364 | |
| 365 | int zipClose_64(zipFile file, const char *global_comment) { |
| 366 | return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY); |
| 367 | } |
| 368 | |
| 369 | int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby) { |
| 370 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 371 | int32_t err = MZ_OK; |
| 372 | |
| 373 | if (compat->handle) |
| 374 | err = zipClose2_MZ(file, global_comment, version_madeby); |
| 375 | |
| 376 | if (compat->stream) { |
| 377 | mz_stream_close(compat->stream); |
| 378 | mz_stream_delete(&compat->stream); |
| 379 | } |
| 380 | |
| 381 | free(compat); |
| 382 | |
| 383 | return err; |
| 384 | } |
| 385 | |
| 386 | /* Only closes the zip handle, does not close the stream */ |
| 387 | int zipClose_MZ(zipFile file, const char *global_comment) { |
| 388 | return zipClose2_MZ(file, global_comment, MZ_VERSION_MADEBY); |
| 389 | } |
| 390 | |
| 391 | /* Only closes the zip handle, does not close the stream */ |
| 392 | int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby) { |
| 393 | mz_zip_compat *compat = (mz_zip_compat *)file; |
| 394 | int32_t err = MZ_OK; |
| 395 | |
| 396 | if (!compat) |
| 397 | return ZIP_PARAMERROR; |
| 398 | if (!compat->handle) |
| 399 | return err; |
| 400 | |
| 401 | if (global_comment) |
| 402 | mz_zip_set_comment(compat->handle, global_comment); |
| 403 | |
| 404 | mz_zip_set_version_madeby(compat->handle, version_madeby); |
| 405 | err = mz_zip_close(compat->handle); |
| 406 | mz_zip_delete(&compat->handle); |
| 407 | |
| 408 | return err; |
| 409 | } |