From d820d03bc439109fe3df4b8e172621acae295281 Mon Sep 17 00:00:00 2001 From: Kenneth Lausdahl <laudahl@eng.au.dk> Date: Fri, 24 Mar 2017 20:53:28 +0100 Subject: [PATCH] windows tweaks, had to replace minizip --- .gitignore | 59 +- .gitmodules | 12 +- src/miniunz.c | 446 ------------ thrird_party/msys-toolchain.cmake | 19 + thrird_party/projects/minizip/CMakeLists.txt | 68 ++ thrird_party/projects/minizip/miniunz.c | 662 ++++++++++++++++++ .../projects/minizip}/miniunzipz.h | 2 +- thrird_party/zlib | 1 + 8 files changed, 787 insertions(+), 482 deletions(-) delete mode 100644 src/miniunz.c create mode 100644 thrird_party/msys-toolchain.cmake create mode 100644 thrird_party/projects/minizip/CMakeLists.txt create mode 100644 thrird_party/projects/minizip/miniunz.c rename {src => thrird_party/projects/minizip}/miniunzipz.h (84%) create mode 160000 thrird_party/zlib diff --git a/.gitignore b/.gitignore index a5d6d9a..090282c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,30 @@ -.DS_Store - -# CMake -CMakeCache.txt -CMakeFiles -CMakeScripts -Makefile -cmake_install.cmake -install_manifest.txt -CTestTestfile.cmake - - - -# -*.exe -*.dll - - -#Eclipse -*.cproject -*.project -*.classpath -*.settings - -*.log - - -# executables -fmu +.DS_Store + +# CMake +CMakeCache.txt +CMakeFiles +CMakeScripts +Makefile +cmake_install.cmake +install_manifest.txt +CTestTestfile.cmake + + + +# +*.exe +*.dll +*.obj +*.a + +#Eclipse +*.cproject +*.project +*.classpath +*.settings + +*.log + + +# executables +fmu diff --git a/.gitmodules b/.gitmodules index c0bb6cc..8b37e91 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ -[submodule "include/fmi"] - path = include/fmi - url = https://github.com/into-cps/fmi.git -[submodule "thrird_party/minizip"] - path = thrird_party/minizip - url = https://github.com/nmoinvaz/minizip +[submodule "include/fmi"] + path = include/fmi + url = https://github.com/into-cps/fmi.git +[submodule "thrird_party/zlib"] + path = thrird_party/zlib + url = https://github.com/madler/zlib.git diff --git a/src/miniunz.c b/src/miniunz.c deleted file mode 100644 index 7b84671..0000000 --- a/src/miniunz.c +++ /dev/null @@ -1,446 +0,0 @@ -/* miniunz.c - Version 1.1, February 14h, 2010 - sample part of the MiniZip project - - Copyright (C) 1998-2010 Gilles Vollant - http://www.winimage.com/zLibDll/minizip.html - Modifications of Unzip for Zip64 - Copyright (C) 2007-2008 Even Rouault - Modifications for Zip64 support - Copyright (C) 2009-2010 Mathias Svensson - http://result42.com - - This program is distributed under the terms of the same license as zlib. - See the accompanying LICENSE file for the full text of the license. -*/ - -#include "miniunzipz.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> -#include <errno.h> -#include <fcntl.h> - -#ifdef _WIN32 -# include <direct.h> -# include <io.h> -#else -# include <sys/stat.h> -# include <unistd.h> -# include <utime.h> -#endif - -#include "unzip.h" - -#ifdef _WIN32 -# define USEWIN32IOAPI -# include "iowin32.h" -#endif - -#include "minishared.h" - -void display_zpos64(uint64_t n, int size_char) -{ - /* To avoid compatibility problem we do here the conversion */ - char number[21] = {0}; - int offset = 19; - int pos_string = 19; - int size_display_string = 19; - - while (1) - { - number[offset] = (char)((n%10) + '0'); - if (number[offset] != '0') - pos_string = offset; - n /= 10; - if (offset == 0) - break; - offset--; - } - - size_display_string -= pos_string; - while (size_char-- > size_display_string) - printf(" "); - printf("%s",&number[pos_string]); -} - -void do_banner() -{ - printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n"); - printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); -} - -void do_help() -{ - printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \ - " -e Extract without path (junk paths)\n" \ - " -x Extract with path\n" \ - " -v list files\n" \ - " -l list files\n" \ - " -d directory to extract into\n" \ - " -o overwrite files without prompting\n" \ - " -p extract crypted file using password\n\n"); -} - -int do_list(unzFile uf) -{ - int err = unzGoToFirstFile(uf); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGoToFirstFile\n", err); - return 1; - } - - printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); - printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); - - do - { - char filename_inzip[256] = {0}; - unz_file_info64 file_info = {0}; - uint32_t ratio = 0; - struct tm tmu_date = { 0 }; - const char *string_method = NULL; - char char_crypt = ' '; - - err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); - break; - } - - if (file_info.uncompressed_size > 0) - ratio = (uint32_t)((file_info.compressed_size * 100) / file_info.uncompressed_size); - - /* Display a '*' if the file is encrypted */ - if ((file_info.flag & 1) != 0) - char_crypt = '*'; - - if (file_info.compression_method == 0) - string_method = "Stored"; - else if (file_info.compression_method == Z_DEFLATED) - { - uint16_t level = (uint16_t)((file_info.flag & 0x6) / 2); - if (level == 0) - string_method = "Defl:N"; - else if (level == 1) - string_method = "Defl:X"; - else if ((level == 2) || (level == 3)) - string_method = "Defl:F"; /* 2:fast , 3 : extra fast*/ - else - string_method = "Unkn. "; - } - else if (file_info.compression_method == Z_BZIP2ED) - { - string_method = "BZip2 "; - } - else - string_method = "Unkn. "; - - display_zpos64(file_info.uncompressed_size, 7); - printf(" %6s%c", string_method, char_crypt); - display_zpos64(file_info.compressed_size, 7); - - dosdate_to_tm(file_info.dos_date, &tmu_date); -// printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", ratio, -// (uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday, -// (uint32_t)tmu_date.tm_year % 100, -// (uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min, -// file_info.crc, filename_inzip); - - err = unzGoToNextFile(uf); - } - while (err == UNZ_OK); - - if (err != UNZ_END_OF_LIST_OF_FILE && err != UNZ_OK) - { - printf("error %d with zipfile in unzGoToNextFile\n", err); - return err; - } - - return 0; -} - -int do_extract_currentfile(unzFile uf, int opt_extract_without_path, int *popt_overwrite, const char *password) -{ - unz_file_info64 file_info = {0}; - FILE* fout = NULL; - void* buf = NULL; - uint16_t size_buf = 8192; - int err = UNZ_OK; - int errclose = UNZ_OK; - int skip = 0; - char filename_inzip[256] = {0}; - char *filename_withoutpath = NULL; - const char *write_filename = NULL; - char *p = NULL; - - err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); - return err; - } - - p = filename_withoutpath = filename_inzip; - while (*p != 0) - { - if ((*p == '/') || (*p == '\\')) - filename_withoutpath = p+1; - p++; - } - - /* If zip entry is a directory then create it on disk */ - if (*filename_withoutpath == 0) - { - if (opt_extract_without_path == 0) - { - // printf("creating directory: %s\n", filename_inzip); - MKDIR(filename_inzip); - } - return err; - } - - buf = (void*)malloc(size_buf); - if (buf == NULL) - { - printf("Error allocating memory\n"); - return UNZ_INTERNALERROR; - } - - err = unzOpenCurrentFilePassword(uf, password); - if (err != UNZ_OK) - printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err); - - if (opt_extract_without_path) - write_filename = filename_withoutpath; - else - write_filename = filename_inzip; - - /* Determine if the file should be overwritten or not and ask the user if needed */ - if ((err == UNZ_OK) && (*popt_overwrite == 0) && (check_file_exists(write_filename))) - { - char rep = 0; - do - { - char answer[128]; - printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", write_filename); - if (scanf("%1s", answer) != 1) - exit(EXIT_FAILURE); - rep = answer[0]; - if ((rep >= 'a') && (rep <= 'z')) - rep -= 0x20; - } - while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); - - if (rep == 'N') - skip = 1; - if (rep == 'A') - *popt_overwrite = 1; - } - - /* Create the file on disk so we can unzip to it */ - if ((skip == 0) && (err == UNZ_OK)) - { - fout = FOPEN_FUNC(write_filename, "wb"); - /* Some zips don't contain directory alone before file */ - if ((fout == NULL) && (opt_extract_without_path == 0) && - (filename_withoutpath != (char*)filename_inzip)) - { - char c = *(filename_withoutpath-1); - *(filename_withoutpath-1) = 0; - makedir(write_filename); - *(filename_withoutpath-1) = c; - fout = FOPEN_FUNC(write_filename, "wb"); - } - if (fout == NULL) - printf("error opening %s\n", write_filename); - } - - /* Read from the zip, unzip to buffer, and write to disk */ - if (fout != NULL) - { - // printf(" extracting: %s\n", write_filename); - - do - { - err = unzReadCurrentFile(uf, buf, size_buf); - if (err < 0) - { - printf("error %d with zipfile in unzReadCurrentFile\n", err); - break; - } - if (err == 0) - break; - if (fwrite(buf, err, 1, fout) != 1) - { - printf("error %d in writing extracted file\n", errno); - err = UNZ_ERRNO; - break; - } - } - while (err > 0); - - if (fout) - fclose(fout); - - /* Set the time of the file that has been unzipped */ - if (err == 0) - change_file_date(write_filename, file_info.dos_date); - } - - errclose = unzCloseCurrentFile(uf); - if (errclose != UNZ_OK) - printf("error %d with zipfile in unzCloseCurrentFile\n", errclose); - - free(buf); - return err; -} - -int do_extract_all(unzFile uf, int opt_extract_without_path, int opt_overwrite, const char *password) -{ - int err = unzGoToFirstFile(uf); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGoToFirstFile\n", err); - return 1; - } - - do - { - err = do_extract_currentfile(uf, opt_extract_without_path, &opt_overwrite, password); - if (err != UNZ_OK) - break; - err = unzGoToNextFile(uf); - } - while (err == UNZ_OK); - - if (err != UNZ_END_OF_LIST_OF_FILE) - { - printf("error %d with zipfile in unzGoToNextFile\n", err); - return 1; - } - return 0; -} - -int do_extract_onefile(unzFile uf, const char *filename, int opt_extract_without_path, int opt_overwrite, - const char *password) -{ - if (unzLocateFile(uf, filename, NULL) != UNZ_OK) - { - printf("file %s not found in the zipfile\n", filename); - return 2; - } - if (do_extract_currentfile(uf, opt_extract_without_path, &opt_overwrite, password) == UNZ_OK) - return 0; - return 1; -} - -int miniunz(int argc, const char *argv[]) -{ - const char *zipfilename = NULL; - const char *filename_to_extract = NULL; - const char *password = NULL; - int i = 0; - int ret = 0; - int opt_do_list = 0; - int opt_do_extract = 1; - int opt_do_extract_withoutpath = 0; - int opt_overwrite = 0; - int opt_extractdir = 0; - const char *dirname = NULL; - unzFile uf = NULL; - - //do_banner(); - if (argc == 1) - { - do_help(); - return 0; - } - - /* Parse command line options */ - for (i = 1; i < argc; i++) - { - if ((*argv[i]) == '-') - { - const char *p = argv[i]+1; - - while (*p != 0) - { - char c = *(p++); - if ((c == 'l') || (c == 'L')) - opt_do_list = 1; - if ((c == 'v') || (c == 'V')) - opt_do_list = 1; - if ((c == 'x') || (c == 'X')) - opt_do_extract = 1; - if ((c == 'e') || (c == 'E')) - opt_do_extract = opt_do_extract_withoutpath = 1; - if ((c == 'o') || (c == 'O')) - opt_overwrite=1; - if ((c == 'd') || (c == 'D')) - { - opt_extractdir = 1; - dirname = argv[i+1]; - } - - if (((c == 'p') || (c == 'P')) && (i+1 < argc)) - { - password = argv[i+1]; - i++; - } - } - } - else - { - if (zipfilename == NULL) - zipfilename = argv[i]; - else if ((filename_to_extract == NULL) && (!opt_extractdir)) - filename_to_extract = argv[i]; - } - } - - /* Open zip file */ - if (zipfilename != NULL) - { -#ifdef USEWIN32IOAPI - zlib_filefunc64_def ffunc; - fill_win32_filefunc64A(&ffunc); - uf = unzOpen2_64(zipfilename, &ffunc); -#else - uf = unzOpen64(zipfilename); -#endif - } - - if (uf == NULL) - { - printf("Cannot open %s\n", zipfilename); - return 1; - } - - // printf("%s opened\n", zipfilename); - - /* Process command line options */ - if (opt_do_list == 1) - { - ret = do_list(uf); - } - else if (opt_do_extract == 1) - { - if (opt_extractdir && CHDIR(dirname)) - { - printf("Error changing into %s, aborting\n", dirname); - exit(-1); - } - - if (filename_to_extract == NULL) - ret = do_extract_all(uf, opt_do_extract_withoutpath, opt_overwrite, password); - else - ret = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password); - } - - unzClose(uf); - return ret; -} diff --git a/thrird_party/msys-toolchain.cmake b/thrird_party/msys-toolchain.cmake new file mode 100644 index 0000000..cb90408 --- /dev/null +++ b/thrird_party/msys-toolchain.cmake @@ -0,0 +1,19 @@ +# the name of the target operating system +SET(CMAKE_SYSTEM_NAME Windows) + +SET(WIN32 true) + +# which compilers to use for C and C++ +SET(CMAKE_C_COMPILER /mingw64/bin/gcc) +SET(CMAKE_CXX_COMPILER /mingw64/bin/g++) +#SET(CMAKE_RC_COMPILER i486-mingw32-windres) + +# here is the target environment located +#SET(CMAKE_FIND_ROOT_PATH /usr/i486-mingw32) + +# adjust the default behaviour of the FIND_XXX() commands: +# search headers and libraries in the target environment, search +# programs in the host environment +#set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +#set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +#set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) diff --git a/thrird_party/projects/minizip/CMakeLists.txt b/thrird_party/projects/minizip/CMakeLists.txt new file mode 100644 index 0000000..c1e9456 --- /dev/null +++ b/thrird_party/projects/minizip/CMakeLists.txt @@ -0,0 +1,68 @@ +#*************************************************************************** +# Copyright: Kenneth Lausdahl, +# E-Mail: lausdahl@eng.au.dk +# Year: 2017 +#*************************************************************************** +cmake_minimum_required(VERSION 2.8) + + +# Set a consistent MACOSX_RPATH default across all CMake versions. +# When CMake 2.8.12 is required, change this default to 1. +# When CMake 3.0.0 is required, remove this block (see CMP0042). +if(NOT DEFINED CMAKE_MACOSX_RPATH) + set(CMAKE_MACOSX_RPATH 0) +endif() + +project("minizip") +set(CMAKE_VERBOSE_MAKEFILE on) + +# set cmake debug postfix to d +set(CMAKE_DEBUG_POSTFIX "d") + +# Ensure correct version of zlib is referenced +set(ZLIB_ROOT ${DEF_ZLIB_ROOT} CACHE PATH "Parent directory of zlib installation") +find_package(ZLIB REQUIRED) +if(ZLIB_FOUND) + include_directories(${ZLIB_INCLUDE_DIRS}) +endif() + +include_directories(../../zlib/contrib/minizip) + + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I../../zlib/contrib/minizip") + +set(MINIZIP_SRC "../../zlib/contrib/minizip/ioapi.c" +"miniunz.c" + # "minizip.c" + "../../zlib/contrib/minizip/mztools.c" + "../../zlib/contrib/minizip/unzip.c" + "../../zlib/contrib/minizip/zip.c") + +#set(MINIZIP_PUBLIC_HEADERS "crypt.h" +# "ioapi.h" +# "ioapi_buf.h" +# "ioapi_mem.h" +# "unzip.h" +# "zip.h") + +if(WIN32) + list(APPEND MINIZIP_SRC "../../zlib/contrib/minizip/iowin32.c") + list(APPEND MINIZIP_PUBLIC_HEADERS "../../zlib/contrib/minizip/iowin32.h") + add_definitions(-D_CRT_SECURE_NO_DEPRECATE) +endif() + +if(UNIX) +# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3") +endif() + +# create minizip library +add_library(minizip ${MINIZIP_SRC} )#${MINIZIP_PUBLIC_HEADERS} +target_link_libraries(minizip ZLIB::ZLIB) +target_include_directories(minizip + PUBLIC + "." + ) + + + + diff --git a/thrird_party/projects/minizip/miniunz.c b/thrird_party/projects/minizip/miniunz.c new file mode 100644 index 0000000..a38b9e6 --- /dev/null +++ b/thrird_party/projects/minizip/miniunz.c @@ -0,0 +1,662 @@ +/* + miniunz.c + Version 1.1, February 14h, 2010 + sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) + + Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) + + Modifications of Unzip for Zip64 + Copyright (C) 2007-2008 Even Rouault + + Modifications for Zip64 support on both zip and unzip + Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) +*/ + +#include "miniunzipz.h" + +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) + #ifndef __USE_FILE_OFFSET64 + #define __USE_FILE_OFFSET64 + #endif + #ifndef __USE_LARGEFILE64 + #define __USE_LARGEFILE64 + #endif + #ifndef _LARGEFILE64_SOURCE + #define _LARGEFILE64_SOURCE + #endif + #ifndef _FILE_OFFSET_BIT + #define _FILE_OFFSET_BIT 64 + #endif +#endif + +#ifdef __APPLE__ +// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions +#define FOPEN_FUNC(filename, mode) fopen(filename, mode) +#define FTELLO_FUNC(stream) ftello(stream) +#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin) +#else +#define FOPEN_FUNC(filename, mode) fopen64(filename, mode) +#define FTELLO_FUNC(stream) ftello64(stream) +#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin) +#endif + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <errno.h> +#include <fcntl.h> + +#ifdef _WIN32 +# include <direct.h> +# include <io.h> +#else +# include <unistd.h> +# include <utime.h> +#endif + + +#include "unzip.h" + +#define CASESENSITIVITY (0) +#define WRITEBUFFERSIZE (8192) +#define MAXFILENAME (256) + +#ifdef _WIN32 +#define USEWIN32IOAPI +#include "iowin32.h" +#endif +/* + mini unzip, demo of unzip package + + usage : + Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir] + + list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT + if it exists +*/ + + +/* change_file_date : change the date/time of a file + filename : the filename of the file where date/time must be modified + dosdate : the new date at the MSDos format (4 bytes) + tmu_date : the SAME new date at the tm_unz format */ +void change_file_date(filename,dosdate,tmu_date) + const char *filename; + uLong dosdate; + tm_unz tmu_date; +{ +#ifdef _WIN32 + HANDLE hFile; + FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite; + + hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE, + 0,NULL,OPEN_EXISTING,0,NULL); + GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite); + DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal); + LocalFileTimeToFileTime(&ftLocal,&ftm); + SetFileTime(hFile,&ftm,&ftLastAcc,&ftm); + CloseHandle(hFile); +#else +#ifdef unix || __APPLE__ + struct utimbuf ut; + struct tm newdate; + newdate.tm_sec = tmu_date.tm_sec; + newdate.tm_min=tmu_date.tm_min; + newdate.tm_hour=tmu_date.tm_hour; + newdate.tm_mday=tmu_date.tm_mday; + newdate.tm_mon=tmu_date.tm_mon; + if (tmu_date.tm_year > 1900) + newdate.tm_year=tmu_date.tm_year - 1900; + else + newdate.tm_year=tmu_date.tm_year ; + newdate.tm_isdst=-1; + + ut.actime=ut.modtime=mktime(&newdate); + utime(filename,&ut); +#endif +#endif +} + + +/* mymkdir and change_file_date are not 100 % portable + As I don't know well Unix, I wait feedback for the unix portion */ + +int mymkdir(dirname) + const char* dirname; +{ + int ret=0; +#ifdef _WIN32 + ret = _mkdir(dirname); +#elif unix + ret = mkdir (dirname,0775); +#elif __APPLE__ + ret = mkdir (dirname,0775); +#endif + return ret; +} + +int makedir (newdir) + char *newdir; +{ + char *buffer ; + char *p; + int len = (int)strlen(newdir); + + if (len <= 0) + return 0; + + buffer = (char*)malloc(len+1); + if (buffer==NULL) + { + printf("Error allocating memory\n"); + return UNZ_INTERNALERROR; + } + strcpy(buffer,newdir); + + if (buffer[len-1] == '/') { + buffer[len-1] = '\0'; + } + if (mymkdir(buffer) == 0) + { + free(buffer); + return 1; + } + + p = buffer+1; + while (1) + { + char hold; + + while(*p && *p != '\\' && *p != '/') + p++; + hold = *p; + *p = 0; + if ((mymkdir(buffer) == -1) && (errno == ENOENT)) + { + printf("couldn't create directory %s\n",buffer); + free(buffer); + return 0; + } + if (hold == 0) + break; + *p++ = hold; + } + free(buffer); + return 1; +} + +void do_banner() +{ + // printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n"); + // printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); +} + +void do_help() +{ + printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \ + " -e Extract without pathname (junk paths)\n" \ + " -x Extract with pathname\n" \ + " -v list files\n" \ + " -l list files\n" \ + " -d directory to extract into\n" \ + " -o overwrite files without prompting\n" \ + " -p extract crypted file using password\n\n"); +} + +void Display64BitsSize(ZPOS64_T n, int size_char) +{ + /* to avoid compatibility problem , we do here the conversion */ + char number[21]; + int offset=19; + int pos_string = 19; + number[20]=0; + for (;;) { + number[offset]=(char)((n%10)+'0'); + if (number[offset] != '0') + pos_string=offset; + n/=10; + if (offset==0) + break; + offset--; + } + { + int size_display_string = 19-pos_string; + while (size_char > size_display_string) + { + size_char--; + printf(" "); + } + } + + printf("%s",&number[pos_string]); +} + +int do_list(uf) + unzFile uf; +{ + uLong i; + unz_global_info64 gi; + int err; + + err = unzGetGlobalInfo64(uf,&gi); + if (err!=UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n",err); + printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); + printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); + for (i=0;i<gi.number_entry;i++) + { + char filename_inzip[256]; + unz_file_info64 file_info; + uLong ratio=0; + const char *string_method; + char charCrypt=' '; + err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); + break; + } + if (file_info.uncompressed_size>0) + ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size); + + /* display a '*' if the file is crypted */ + if ((file_info.flag & 1) != 0) + charCrypt='*'; + + if (file_info.compression_method==0) + string_method="Stored"; + else + if (file_info.compression_method==Z_DEFLATED) + { + uInt iLevel=(uInt)((file_info.flag & 0x6)/2); + if (iLevel==0) + string_method="Defl:N"; + else if (iLevel==1) + string_method="Defl:X"; + else if ((iLevel==2) || (iLevel==3)) + string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ + } + else + if (file_info.compression_method==Z_BZIP2ED) + { + string_method="BZip2 "; + } + else + string_method="Unkn. "; + + Display64BitsSize(file_info.uncompressed_size,7); + printf(" %6s%c",string_method,charCrypt); + Display64BitsSize(file_info.compressed_size,7); + printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", + ratio, + (uLong)file_info.tmu_date.tm_mon + 1, + (uLong)file_info.tmu_date.tm_mday, + (uLong)file_info.tmu_date.tm_year % 100, + (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, + (uLong)file_info.crc,filename_inzip); + if ((i+1)<gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n",err); + break; + } + } + } + + return 0; +} + + +int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) + unzFile uf; + const int* popt_extract_without_path; + int* popt_overwrite; + const char* password; +{ + char filename_inzip[256]; + char* filename_withoutpath; + char* p; + int err=UNZ_OK; + FILE *fout=NULL; + void* buf; + uInt size_buf; + + unz_file_info64 file_info; + uLong ratio=0; + err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); + return err; + } + + size_buf = WRITEBUFFERSIZE; + buf = (void*)malloc(size_buf); + if (buf==NULL) + { + printf("Error allocating memory\n"); + return UNZ_INTERNALERROR; + } + + p = filename_withoutpath = filename_inzip; + while ((*p) != '\0') + { + if (((*p)=='/') || ((*p)=='\\')) + filename_withoutpath = p+1; + p++; + } + + if ((*filename_withoutpath)=='\0') + { + if ((*popt_extract_without_path)==0) + { + printf("creating directory: %s\n",filename_inzip); + mymkdir(filename_inzip); + } + } + else + { + const char* write_filename; + int skip=0; + + if ((*popt_extract_without_path)==0) + write_filename = filename_inzip; + else + write_filename = filename_withoutpath; + + err = unzOpenCurrentFilePassword(uf,password); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); + } + + if (((*popt_overwrite)==0) && (err==UNZ_OK)) + { + char rep=0; + FILE* ftestexist; + ftestexist = FOPEN_FUNC(write_filename,"rb"); + if (ftestexist!=NULL) + { + fclose(ftestexist); + do + { + char answer[128]; + int ret; + + printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); + ret = scanf("%1s",answer); + if (ret != 1) + { + exit(EXIT_FAILURE); + } + rep = answer[0] ; + if ((rep>='a') && (rep<='z')) + rep -= 0x20; + } + while ((rep!='Y') && (rep!='N') && (rep!='A')); + } + + if (rep == 'N') + skip = 1; + + if (rep == 'A') + *popt_overwrite=1; + } + + if ((skip==0) && (err==UNZ_OK)) + { + fout=FOPEN_FUNC(write_filename,"wb"); + /* some zipfile don't contain directory alone before file */ + if ((fout==NULL) && ((*popt_extract_without_path)==0) && + (filename_withoutpath!=(char*)filename_inzip)) + { + char c=*(filename_withoutpath-1); + *(filename_withoutpath-1)='\0'; + makedir(write_filename); + *(filename_withoutpath-1)=c; + fout=FOPEN_FUNC(write_filename,"wb"); + } + + if (fout==NULL) + { + printf("error opening %s\n",write_filename); + } + } + + if (fout!=NULL) + { + printf(" extracting: %s\n",write_filename); + + do + { + err = unzReadCurrentFile(uf,buf,size_buf); + if (err<0) + { + printf("error %d with zipfile in unzReadCurrentFile\n",err); + break; + } + if (err>0) + if (fwrite(buf,err,1,fout)!=1) + { + printf("error in writing extracted file\n"); + err=UNZ_ERRNO; + break; + } + } + while (err>0); + if (fout) + fclose(fout); + + if (err==0) + change_file_date(write_filename,file_info.dosDate, + file_info.tmu_date); + } + + if (err==UNZ_OK) + { + err = unzCloseCurrentFile (uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzCloseCurrentFile\n",err); + } + } + else + unzCloseCurrentFile(uf); /* don't lose the error */ + } + + free(buf); + return err; +} + + +int do_extract(uf,opt_extract_without_path,opt_overwrite,password) + unzFile uf; + int opt_extract_without_path; + int opt_overwrite; + const char* password; +{ + uLong i; + unz_global_info64 gi; + int err; + FILE* fout=NULL; + + err = unzGetGlobalInfo64(uf,&gi); + if (err!=UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n",err); + + for (i=0;i<gi.number_entry;i++) + { + if (do_extract_currentfile(uf,&opt_extract_without_path, + &opt_overwrite, + password) != UNZ_OK) + break; + + if ((i+1)<gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n",err); + break; + } + } + } + + return 0; +} + +int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password) + unzFile uf; + const char* filename; + int opt_extract_without_path; + int opt_overwrite; + const char* password; +{ + int err = UNZ_OK; + if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) + { + printf("file %s not found in the zipfile\n",filename); + return 2; + } + + if (do_extract_currentfile(uf,&opt_extract_without_path, + &opt_overwrite, + password) == UNZ_OK) + return 0; + else + return 1; +} + + +int miniunz(argc,argv) + int argc; + const char *argv[]; +{ + const char *zipfilename=NULL; + const char *filename_to_extract=NULL; + const char *password=NULL; + char filename_try[MAXFILENAME+16] = ""; + int i; + int ret_value=0; + int opt_do_list=0; + int opt_do_extract=1; + int opt_do_extract_withoutpath=0; + int opt_overwrite=0; + int opt_extractdir=0; + const char *dirname=NULL; + unzFile uf=NULL; + + do_banner(); + if (argc==1) + { + do_help(); + return 0; + } + else + { + for (i=1;i<argc;i++) + { + if ((*argv[i])=='-') + { + const char *p=argv[i]+1; + + while ((*p)!='\0') + { + char c=*(p++);; + if ((c=='l') || (c=='L')) + opt_do_list = 1; + if ((c=='v') || (c=='V')) + opt_do_list = 1; + if ((c=='x') || (c=='X')) + opt_do_extract = 1; + if ((c=='e') || (c=='E')) + opt_do_extract = opt_do_extract_withoutpath = 1; + if ((c=='o') || (c=='O')) + opt_overwrite=1; + if ((c=='d') || (c=='D')) + { + opt_extractdir=1; + dirname=argv[i+1]; + } + + if (((c=='p') || (c=='P')) && (i+1<argc)) + { + password=argv[i+1]; + i++; + } + } + } + else + { + if (zipfilename == NULL) + zipfilename = argv[i]; + else if ((filename_to_extract==NULL) && (!opt_extractdir)) + filename_to_extract = argv[i] ; + } + } + } + + if (zipfilename!=NULL) + { + +# ifdef USEWIN32IOAPI + zlib_filefunc64_def ffunc; +# endif + + strncpy(filename_try, zipfilename,MAXFILENAME-1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + filename_try[ MAXFILENAME ] = '\0'; + +# ifdef USEWIN32IOAPI + fill_win32_filefunc64A(&ffunc); + uf = unzOpen2_64(zipfilename,&ffunc); +# else + uf = unzOpen64(zipfilename); +# endif + if (uf==NULL) + { + strcat(filename_try,".zip"); +# ifdef USEWIN32IOAPI + uf = unzOpen2_64(filename_try,&ffunc); +# else + uf = unzOpen64(filename_try); +# endif + } + } + + if (uf==NULL) + { + printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); + return 1; + } + printf("%s opened\n",filename_try); + + if (opt_do_list==1) + ret_value = do_list(uf); + else if (opt_do_extract==1) + { +#ifdef _WIN32 + if (opt_extractdir && _chdir(dirname)) +#else + if (opt_extractdir && chdir(dirname)) +#endif + { + printf("Error changing into %s, aborting\n", dirname); + exit(-1); + } + + if (filename_to_extract == NULL) + ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password); + else + ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password); + } + + unzClose(uf); + + return ret_value; +} diff --git a/src/miniunzipz.h b/thrird_party/projects/minizip/miniunzipz.h similarity index 84% rename from src/miniunzipz.h rename to thrird_party/projects/minizip/miniunzipz.h index 2f9057a..7afc1e9 100644 --- a/src/miniunzipz.h +++ b/thrird_party/projects/minizip/miniunzipz.h @@ -12,7 +12,7 @@ extern "C" { #endif -int miniunz(int argc, const char *argv[]); +int miniunz(int argc,const char *argv[]); #ifdef __cplusplus } #endif diff --git a/thrird_party/zlib b/thrird_party/zlib new file mode 160000 index 0000000..cacf7f1 --- /dev/null +++ b/thrird_party/zlib @@ -0,0 +1 @@ +Subproject commit cacf7f1d4e3d44d871b605da3b647f07d718623f -- GitLab