4#include "Stroika/Foundation/StroikaPreComp.h"
8#if qStroika_HasComponent_zlib
13#ifdef HAVE_MINIZIP64_CONF_H
20#if qStroika_HasComponent_zlib
21namespace Stroika::Foundation::DataExchange::Archive::Zip::PrivateMinizip_ {
156 constexpr uint32_t MAXU32 = numeric_limits<uint32_t>::max ();
159#define CRC32(c, b) ((*(pcrc_32_tab + (((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
162 inline int decrypt_byte (
unsigned long* pkeys, [[maybe_unused]]
const z_crc_t* pcrc_32_tab)
167 temp = ((unsigned)(*(pkeys + 2)) & 0xffff) | 2;
168 return (
int)(((temp * (temp ^ 1)) >> 8) & 0xff);
172 inline int update_keys (
unsigned long* pkeys,
const z_crc_t* pcrc_32_tab,
int c)
174 (*(pkeys + 0)) = CRC32 ((*(pkeys + 0)), c);
175 (*(pkeys + 1)) += (*(pkeys + 0)) & 0xff;
176 (*(pkeys + 1)) = (*(pkeys + 1)) * 134775813L + 1;
178 int keyshift = (int)((*(pkeys + 1)) >> 24);
179 (*(pkeys + 2)) = CRC32 ((*(pkeys + 2)), keyshift);
185 inline void init_keys (
const char* passwd,
unsigned long* pkeys,
const z_crc_t* pcrc_32_tab)
187 *(pkeys + 0) = 305419896L;
188 *(pkeys + 1) = 591751049L;
189 *(pkeys + 2) = 878082192L;
190 while (*passwd !=
'\0') {
191 update_keys (pkeys, pcrc_32_tab, (
int)*passwd);
196#define zdecode(pkeys, pcrc_32_tab, c) (update_keys (pkeys, pcrc_32_tab, c ^= decrypt_byte (pkeys, pcrc_32_tab)))
198#define zencode(pkeys, pcrc_32_tab, c, t) (t = decrypt_byte (pkeys, pcrc_32_tab), update_keys (pkeys, pcrc_32_tab, c), t ^ (c))
200#define RAND_HEAD_LEN 12
203 constexpr unsigned int ZCR_SEED2 = 3141592654UL;
205 inline int crypthead (
const char* passwd,
207 int bufSize,
unsigned long* pkeys,
const z_crc_t* pcrc_32_tab,
unsigned long crcForCrypting)
212 unsigned char header[RAND_HEAD_LEN - 2];
213 static unsigned calls = 0;
215 if (bufSize < RAND_HEAD_LEN)
223 srand ((
unsigned)(time (NULL) ^ ZCR_SEED2));
225 init_keys (passwd, pkeys, pcrc_32_tab);
226 for (n = 0; n < RAND_HEAD_LEN - 2; n++) {
227 c = (rand () >> 7) & 0xff;
228 header[n] = (
unsigned char)zencode (pkeys, pcrc_32_tab, c, t);
231 init_keys (passwd, pkeys, pcrc_32_tab);
232 for (n = 0; n < RAND_HEAD_LEN - 2; n++) {
233 buf[n] = (
unsigned char)zencode (pkeys, pcrc_32_tab, header[n], t);
235 buf[n++] = (
unsigned char)zencode (pkeys, pcrc_32_tab, (
int)(crcForCrypting >> 16) & 0xff, t);
236 buf[n++] = (
unsigned char)zencode (pkeys, pcrc_32_tab, (
int)(crcForCrypting >> 24) & 0xff, t);
243 typedef uint64_t ZPOS64_T;
245#define ZLIB_FILEFUNC_SEEK_CUR (1)
246#define ZLIB_FILEFUNC_SEEK_END (2)
247#define ZLIB_FILEFUNC_SEEK_SET (0)
249#define ZLIB_FILEFUNC_MODE_READ (1)
250#define ZLIB_FILEFUNC_MODE_WRITE (2)
251#define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3)
253#define ZLIB_FILEFUNC_MODE_EXISTING (4)
254#define ZLIB_FILEFUNC_MODE_CREATE (8)
256 typedef voidpf (*open_file_func) (voidpf opaque,
const char* filename,
int mode);
257 typedef uLong (*read_file_func) (voidpf opaque, voidpf stream,
void* buf, uLong size);
258 typedef uLong (*write_file_func) (voidpf opaque, voidpf stream,
const void* buf, uLong size);
259 typedef int (*close_file_func) (voidpf opaque, voidpf stream);
260 typedef int (*testerror_file_func) (voidpf opaque, voidpf stream);
262 typedef long (*tell_file_func) (voidpf opaque, voidpf stream);
263 typedef long (*seek_file_func) (voidpf opaque, voidpf stream, uLong offset,
int origin);
266 struct zlib_filefunc_def {
267 open_file_func zopen_file;
268 read_file_func zread_file;
269 write_file_func zwrite_file;
270 tell_file_func ztell_file;
271 seek_file_func zseek_file;
272 close_file_func zclose_file;
273 testerror_file_func zerror_file;
277 typedef ZPOS64_T (*tell64_file_func) (voidpf opaque, voidpf stream);
278 typedef long (*seek64_file_func) (voidpf opaque, voidpf stream, ZPOS64_T offset,
int origin);
279 typedef voidpf (*open64_file_func) (voidpf opaque,
const void* filename,
int mode);
281 struct zlib_filefunc64_def {
282 open64_file_func zopen64_file;
283 read_file_func zread_file;
284 write_file_func zwrite_file;
285 tell64_file_func ztell64_file;
286 seek64_file_func zseek64_file;
287 close_file_func zclose_file;
288 testerror_file_func zerror_file;
293 struct zlib_filefunc64_32_def {
294 zlib_filefunc64_def zfile_func64;
295 open_file_func zopen32_file;
296 tell_file_func ztell32_file;
297 seek_file_func zseek32_file;
300#define ZREAD64(filefunc, filestream, buf, size) \
301 ((*((filefunc).zfile_func64.zread_file)) ((filefunc).zfile_func64.opaque, filestream, buf, size))
302#define ZWRITE64(filefunc, filestream, buf, size) \
303 ((*((filefunc).zfile_func64.zwrite_file)) ((filefunc).zfile_func64.opaque, filestream, buf, size))
304#define ZCLOSE64(filefunc, filestream) ((*((filefunc).zfile_func64.zclose_file)) ((filefunc).zfile_func64.opaque, filestream))
305#define ZERROR64(filefunc, filestream) ((*((filefunc).zfile_func64.zerror_file)) ((filefunc).zfile_func64.opaque, filestream))
307 voidpf call_zopen64 (
const zlib_filefunc64_32_def* pfilefunc,
const void* filename,
int mode);
308 long call_zseek64 (
const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, ZPOS64_T offset,
int origin);
309 ZPOS64_T call_ztell64 (
const zlib_filefunc64_32_def* pfilefunc, voidpf filestream);
311 void fill_zlib_filefunc64_32_def_from_filefunc32 (zlib_filefunc64_32_def* p_filefunc64_32,
const zlib_filefunc_def* p_filefunc32);
313#define ZOPEN64(filefunc, filename, mode) (call_zopen64 ((&(filefunc)), (filename), (mode)))
314#define ZTELL64(filefunc, filestream) (call_ztell64 ((&(filefunc)), (filestream)))
315#define ZSEEK64(filefunc, filestream, pos, mode) (call_zseek64 ((&(filefunc)), (filestream), (pos), (mode)))
322#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
325 typedef struct TagunzFile__ {
328 typedef unzFile__* unzFile;
330 typedef voidp unzFile;
334#define UNZ_END_OF_LIST_OF_FILE (-100)
335#define UNZ_ERRNO (Z_ERRNO)
337#define UNZ_PARAMERROR (-102)
338#define UNZ_BADZIPFILE (-103)
339#define UNZ_INTERNALERROR (-104)
340#define UNZ_CRCERROR (-105)
354 struct unz_global_info64 {
355 ZPOS64_T number_entry;
360 struct unz_global_info {
367 struct unz_file_info64 {
369 uLong version_needed;
371 uLong compression_method;
374 ZPOS64_T compressed_size;
375 ZPOS64_T uncompressed_size;
377 uLong size_file_extra;
378 uLong size_file_comment;
380 uLong disk_num_start;
387 struct unz_file_info {
389 uLong version_needed;
391 uLong compression_method;
394 uLong compressed_size;
395 uLong uncompressed_size;
397 uLong size_file_extra;
398 uLong size_file_comment;
400 uLong disk_num_start;
407 int unzStringFileNameCompare (
const char* fileName1,
const char* fileName2,
int iCaseSensitivity);
431 unzFile unzOpen (
const char* path);
432 unzFile unzOpen64 (
const void* path);
438 unzFile unzOpen2 (
const char* path, zlib_filefunc_def* pzlib_filefunc_def);
444 unzFile unzOpen2_64 (
const void* path, zlib_filefunc64_def* pzlib_filefunc_def);
446 int unzClose (unzFile file);
453 int unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info);
459 int unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info);
466 int unzGetGlobalComment (unzFile file,
char* szComment, uLong uSizeBuf);
475 int unzGoToFirstFile (unzFile file);
482 int unzGoToNextFile_ (unzFile file);
492 int unzLocateFile_ (unzFile file,
const char* szFileName,
int iCaseSensitivity);
497 struct unz_file_pos {
498 uLong pos_in_zip_directory;
502 int unzGetFilePos (unzFile file, unz_file_pos* file_pos);
504 int unzGoToFilePos (unzFile file, unz_file_pos* file_pos);
506 struct unz64_file_pos {
507 ZPOS64_T pos_in_zip_directory;
508 ZPOS64_T num_of_file;
511 int unzGetFilePos64 (unzFile file, unz64_file_pos* file_pos);
513 int unzGoToFilePos64 (unzFile file,
const unz64_file_pos* file_pos);
516 int unzGetCurrentFileInfo64 (unzFile file, unz_file_info64* pfile_info,
char* szFileName, uLong fileNameBufferSize,
void* extraField,
517 uLong extraFieldBufferSize,
char* szComment, uLong commentBufferSize);
519 int unzGetCurrentFileInfo (unzFile file, unz_file_info* pfile_info,
char* szFileName, uLong fileNameBufferSize,
void* extraField,
520 uLong extraFieldBufferSize,
char* szComment, uLong commentBufferSize);
534 ZPOS64_T unzGetCurrentFileZStreamPos64 (unzFile file);
544 int unzOpenCurrentFile (unzFile file);
551 int unzOpenCurrentFilePassword (unzFile file,
const char* password);
561 int unzOpenCurrentFile2 (unzFile file,
int* method,
int* level,
int raw);
571 int unzOpenCurrentFile3_ (unzFile file,
int* method,
int* level,
int raw,
const char* password);
577 int unzCloseCurrentFile_ (unzFile file);
589 int unzReadCurrentFile_ (unzFile file, voidp buf,
unsigned len);
591 z_off_t unztell (unzFile file);
593 ZPOS64_T unztell64 (unzFile file);
601 int unzeof (unzFile file);
615 int unzGetLocalExtrafield (unzFile file, voidp buf,
unsigned len);
620 ZPOS64_T unzGetOffset64 (unzFile file);
621 uLong unzGetOffset (unzFile file);
624 int unzSetOffset64 (unzFile file, ZPOS64_T pos);
625 int unzSetOffset (unzFile file, uLong pos);
631#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
634 typedef struct TagzipFile__ {
637 typedef zipFile__* zipFile;
639 typedef voidp zipFile;
644#define ZIP_ERRNO (Z_ERRNO)
645#define ZIP_PARAMERROR (-102)
646#define ZIP_BADZIPFILE (-103)
647#define ZIP_INTERNALERROR (-104)
650#if MAX_MEM_LEVEL >= 8
651#define DEF_MEM_LEVEL 8
653#define DEF_MEM_LEVEL MAX_MEM_LEVEL
659 typedef struct tm_zip_s {
677 typedef const char* zipcharpc;
679#define APPEND_STATUS_CREATE (0)
680#define APPEND_STATUS_CREATEAFTER (1)
681#define APPEND_STATUS_ADDINZIP (2)
683 extern zipFile ZEXPORT zipOpen (
const char* pathname,
int append);
684 extern zipFile ZEXPORT zipOpen64 (
const void* pathname,
int append);
704 extern zipFile ZEXPORT zipOpen2 (
const char* pathname,
int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc_def);
706 extern zipFile ZEXPORT zipOpen2_64 (
const void* pathname,
int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def);
708 extern zipFile ZEXPORT zipOpen3 (
const void* pathname,
int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def);
710 extern int ZEXPORT zipOpenNewFileInZip (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
711 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
712 const char* comment,
int method,
int level);
714 extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
715 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
716 const char* comment,
int method,
int level,
int zip64);
734 extern int ZEXPORT zipOpenNewFileInZip2 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
735 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
736 const char* comment,
int method,
int level,
int raw);
738 extern int ZEXPORT zipOpenNewFileInZip2_64 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
739 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
740 const char* comment,
int method,
int level,
int raw,
int zip64);
745 extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
746 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
747 const char* comment,
int method,
int level,
int raw,
int windowBits,
int memLevel,
748 int strategy,
const char* password, uLong crcForCrypting);
750 extern int ZEXPORT zipOpenNewFileInZip3_64 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
751 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
752 const char* comment,
int method,
int level,
int raw,
int windowBits,
int memLevel,
753 int strategy,
const char* password, uLong crcForCrypting,
int zip64);
762 extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
763 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
764 const char* comment,
int method,
int level,
int raw,
int windowBits,
int memLevel,
765 int strategy,
const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase);
767 extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
768 uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
769 const char* comment,
int method,
int level,
int raw,
int windowBits,
int memLevel,
int strategy,
770 const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase,
int zip64);
777 extern int ZEXPORT zipWriteInFileInZip (zipFile file,
const void* buf,
unsigned len);
782 extern int ZEXPORT zipCloseFileInZip (zipFile file);
787 extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32);
789 extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32);
797 extern int ZEXPORT zipClose (zipFile file,
const char* global_comment);
802 extern int ZEXPORT zipRemoveExtraInfoBlock (
char* pData,
int* dataLen,
short sHeader);