From 8925fbbbb00d6e2476a940335d397ed0bc63cd78 Mon Sep 17 00:00:00 2001 From: DongHun Kwak Date: Tue, 24 Apr 2018 13:13:38 +0900 Subject: [PATCH] Imported Upstream version 1.5.0 Change-Id: Ib742392fdee321b69adbd57e237278df975955a1 Signed-off-by: DongHun Kwak --- .clang-format | 12 + CMakeLists.txt | 180 +++++- FindNettle.cmake | 23 + INSTALL.md | 12 + LICENSE | 35 -- NEWS.md | 8 + README.md | 3 +- THANKS | 1 + TODO.md | 103 ++- appveyor.yml | 57 ++ cmake-config.h.in | 9 + cmake-zipconf.h.in | 103 +-- examples/in-memory.c | 26 +- examples/windows-open.c | 5 +- lib/CMakeLists.txt | 42 +- lib/compat.h | 48 +- lib/gladman-fcrypt.h | 49 -- lib/gladman-fcrypt/aes.h | 128 ---- lib/gladman-fcrypt/aescrypt.c | 450 -------------- lib/gladman-fcrypt/aeskey.c | 416 ------------- lib/gladman-fcrypt/aesopt.h | 900 --------------------------- lib/gladman-fcrypt/aestab.c | 529 ---------------- lib/gladman-fcrypt/fileenc.c | 144 ----- lib/gladman-fcrypt/fileenc.h | 121 ---- lib/gladman-fcrypt/hmac.c | 150 ----- lib/gladman-fcrypt/hmac.h | 81 --- lib/gladman-fcrypt/pwd2key.c | 159 ----- lib/gladman-fcrypt/pwd2key.h | 60 -- lib/gladman-fcrypt/sha1.c | 287 --------- lib/gladman-fcrypt/sha1.h | 79 --- lib/mkstemp.c | 184 +++--- lib/zip.h | 360 +++++------ lib/zip_add.c | 11 +- lib/zip_add_dir.c | 5 +- lib/zip_add_entry.c | 9 +- lib/zip_algorithm_bzip2.c | 19 +- lib/zip_algorithm_deflate.c | 9 +- lib/zip_buffer.c | 124 ++-- lib/zip_close.c | 245 ++++---- lib/{gladman-fcrypt.c => zip_crypto.h} | 42 +- lib/zip_crypto_commoncrypto.c | 133 ++++ lib/zip_crypto_commoncrypto.h | 54 ++ lib/zip_crypto_gnutls.c | 140 +++++ lib/zip_crypto_gnutls.h | 66 ++ lib/zip_crypto_openssl.c | 137 ++++ lib/zip_crypto_openssl.h | 55 ++ lib/zip_delete.c | 8 +- lib/zip_dir_add.c | 15 +- lib/zip_dirent.c | 483 +++++++------- lib/zip_discard.c | 13 +- lib/zip_entry.c | 8 +- lib/zip_err_str.c | 70 +-- lib/zip_error.c | 49 +- lib/zip_error_clear.c | 5 +- lib/zip_error_get.c | 11 +- lib/zip_error_get_sys_type.c | 5 +- lib/zip_error_strerror.c | 17 +- lib/zip_error_to_str.c | 14 +- lib/zip_extra_field.c | 206 +++--- lib/zip_extra_field_api.c | 111 ++-- lib/zip_fclose.c | 7 +- lib/zip_fdopen.c | 15 +- lib/zip_file_add.c | 13 +- lib/zip_file_error_clear.c | 5 +- lib/zip_file_error_get.c | 5 +- lib/zip_file_get_comment.c | 9 +- lib/zip_file_get_external_attributes.c | 7 +- lib/zip_file_get_offset.c | 70 +-- lib/zip_file_rename.c | 15 +- lib/zip_file_replace.c | 41 +- lib/zip_file_set_comment.c | 28 +- lib/zip_file_set_encryption.c | 33 +- lib/zip_file_set_external_attributes.c | 25 +- lib/zip_file_set_mtime.c | 56 +- lib/zip_file_strerror.c | 5 +- lib/zip_filerange_crc.c | 8 +- lib/zip_fopen.c | 7 +- lib/zip_fopen_encrypted.c | 7 +- lib/zip_fopen_index.c | 5 +- lib/zip_fopen_index_encrypted.c | 15 +- lib/zip_fread.c | 7 +- lib/zip_fseek.c | 6 +- lib/zip_ftell.c | 3 +- lib/zip_get_archive_comment.c | 7 +- lib/zip_get_archive_flag.c | 5 +- lib/zip_get_encryption_implementation.c | 7 +- lib/zip_get_file_comment.c | 7 +- lib/zip_get_name.c | 12 +- lib/zip_get_num_entries.c | 7 +- lib/zip_get_num_files.c | 5 +- lib/zip_hash.c | 138 ++-- lib/zip_io_util.c | 50 +- lib/zip_libzip_version.c | 5 +- lib/zip_memdup.c | 5 +- lib/zip_name_locate.c | 20 +- lib/zip_new.c | 7 +- lib/zip_open.c | 421 ++++++------- lib/zip_progress.c | 41 +- lib/zip_random_unix.c | 7 +- lib/zip_random_uwp.c | 9 +- lib/zip_random_win32.c | 9 +- lib/zip_rename.c | 5 +- lib/zip_replace.c | 5 +- lib/zip_set_archive_comment.c | 12 +- lib/zip_set_archive_flag.c | 10 +- lib/zip_set_default_password.c | 9 +- lib/zip_set_file_comment.c | 9 +- lib/zip_set_file_compression.c | 33 +- lib/zip_set_name.c | 19 +- lib/zip_source_begin_write.c | 15 +- lib/zip_source_begin_write_cloning.c | 15 +- lib/zip_source_buffer.c | 474 +++++++------- lib/zip_source_call.c | 41 +- lib/zip_source_close.c | 11 +- lib/zip_source_commit_write.c | 23 +- lib/zip_source_compress.c | 108 ++-- lib/zip_source_crc.c | 232 ++++--- lib/zip_source_error.c | 8 +- lib/zip_source_file.c | 6 +- lib/zip_source_filep.c | 593 ++++++++++-------- lib/zip_source_free.c | 23 +- lib/zip_source_function.c | 30 +- lib/zip_source_get_compression_flags.c | 7 +- lib/zip_source_is_deleted.c | 5 +- lib/zip_source_layered.c | 20 +- lib/zip_source_open.c | 13 +- lib/zip_source_pkware.c | 141 ++--- lib/zip_source_read.c | 18 +- lib/zip_source_remove.c | 23 +- lib/zip_source_rollback_write.c | 7 +- lib/zip_source_seek.c | 62 +- lib/zip_source_seek_write.c | 15 +- lib/zip_source_stat.c | 19 +- lib/zip_source_supports.c | 27 +- lib/zip_source_tell.c | 13 +- lib/zip_source_tell_write.c | 11 +- lib/zip_source_win32a.c | 86 ++- lib/zip_source_win32handle.c | 104 ++-- lib/zip_source_win32utf8.c | 62 +- lib/zip_source_win32w.c | 30 +- lib/zip_source_window.c | 274 ++++---- lib/zip_source_winzip_aes_decode.c | 122 ++-- lib/zip_source_winzip_aes_encode.c | 116 ++-- lib/zip_source_write.c | 11 +- lib/zip_source_zip.c | 14 +- lib/zip_source_zip_new.c | 32 +- lib/zip_stat.c | 7 +- lib/zip_stat_index.c | 22 +- lib/zip_stat_init.c | 26 +- lib/zip_strerror.c | 5 +- lib/zip_string.c | 48 +- lib/zip_unchange.c | 18 +- lib/zip_unchange_all.c | 11 +- lib/zip_unchange_archive.c | 7 +- lib/zip_unchange_data.c | 6 +- lib/zip_utf-8.c | 101 ++- lib/zip_winzip_aes.c | 170 +++++ lib/zipint.h | 283 +++++---- lib/zipwin32.h | 24 +- libzip.pc.in | 3 +- man/zip_errors.mdoc | 2 +- regress/CMakeLists.txt | 16 +- regress/add_from_filep.c | 27 +- regress/can_clone_file.c | 61 +- regress/decrypt-correct-password-aes128.test | 1 + regress/decrypt-correct-password-aes192.test | 1 + regress/decrypt-correct-password-aes256.test | 1 + regress/decrypt-no-password-aes256.test | 1 + regress/decrypt-wrong-password-aes128.test | 1 + regress/decrypt-wrong-password-aes192.test | 1 + regress/decrypt-wrong-password-aes256.test | 1 + regress/encryption-nonrandom-aes128.test | 1 + regress/encryption-nonrandom-aes192.test | 1 + regress/encryption-nonrandom-aes256.test | 1 + regress/encryption-remove.test | 1 + regress/fopen_unchanged.c | 15 +- regress/fread.c | 105 ++-- regress/fseek.c | 21 +- regress/hole.c | 154 +++-- regress/malloc.c | 29 +- regress/nonrandomopen.c | 9 +- regress/nonrandomopentest.c | 7 +- regress/source_hole.c | 520 ++++++++-------- regress/tryopen.c | 21 +- regress/ziptool_regress.c | 304 +++++---- src/CMakeLists.txt | 6 +- src/getopt.c | 123 ++-- src/getopt.h | 2 +- src/zipcmp.c | 208 +++---- src/zipmerge.c | 98 ++- src/ziptool.c | 252 ++++---- 191 files changed, 5284 insertions(+), 8333 deletions(-) create mode 100644 .clang-format create mode 100644 FindNettle.cmake create mode 100644 appveyor.yml delete mode 100644 lib/gladman-fcrypt.h delete mode 100644 lib/gladman-fcrypt/aes.h delete mode 100644 lib/gladman-fcrypt/aescrypt.c delete mode 100644 lib/gladman-fcrypt/aeskey.c delete mode 100644 lib/gladman-fcrypt/aesopt.h delete mode 100644 lib/gladman-fcrypt/aestab.c delete mode 100644 lib/gladman-fcrypt/fileenc.c delete mode 100644 lib/gladman-fcrypt/fileenc.h delete mode 100644 lib/gladman-fcrypt/hmac.c delete mode 100644 lib/gladman-fcrypt/hmac.h delete mode 100644 lib/gladman-fcrypt/pwd2key.c delete mode 100644 lib/gladman-fcrypt/pwd2key.h delete mode 100644 lib/gladman-fcrypt/sha1.c delete mode 100644 lib/gladman-fcrypt/sha1.h rename lib/{gladman-fcrypt.c => zip_crypto.h} (61%) create mode 100644 lib/zip_crypto_commoncrypto.c create mode 100644 lib/zip_crypto_commoncrypto.h create mode 100644 lib/zip_crypto_gnutls.c create mode 100644 lib/zip_crypto_gnutls.h create mode 100644 lib/zip_crypto_openssl.c create mode 100644 lib/zip_crypto_openssl.h create mode 100644 lib/zip_winzip_aes.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..55909c0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 2000 +AlwaysBreakAfterReturnType: TopLevelDefinitions +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 2 +BreakBeforeBraces: Custom +BraceWrapping: + BeforeElse: true +AlignEscapedNewlines: Left +UseTab: ForContinuationAndIndentation +#PPDirectiveIndentStyle: AfterHash diff --git a/CMakeLists.txt b/CMakeLists.txt index 69cdb8b..5f19291 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,16 @@ # TODO: -# 1. Problems generating html pages. -# 2. create usable libtool .la file +# create usable libtool .la file CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2) +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + PROJECT(libzip C) +OPTION(ENABLE_GNUTLS "Enable use of GnuTLS" ON) +OPTION(ENABLE_OPENSSL "Enable use of OpenSSL" ON) +OPTION(ENABLE_COMMONCRYPTO "Enable use of CommonCrypto" ON) + INCLUDE(CheckFunctionExists) INCLUDE(CheckIncludeFiles) INCLUDE(CheckSymbolExists) @@ -15,13 +20,29 @@ INCLUDE(CheckCSourceCompiles) INCLUDE(CheckStructHasMember) INCLUDE(TestBigEndian) INCLUDE(GNUInstallDirs) +IF(ENABLE_COMMONCRYPTO) + CHECK_INCLUDE_FILES(CommonCrypto/CommonCrypto.h COMMONCRYPTO_FOUND) +ELSE() + SET(COMMONCRYPTO_FOUND FALSE) +ENDIF() +IF(ENABLE_GNUTLS) + INCLUDE(FindNettle) + INCLUDE(FindGnuTLS) +ELSE() + SET(GNUTLS_FOUND FALSE) +ENDIF() +IF(ENABLE_OPENSSL) + INCLUDE(FindOpenSSL) +ELSE() + SET(OPENSSL_FOUND FALSE) +ENDIF() OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON) SET(PACKAGE "libzip") SET(PACKAGE_NAME ${PACKAGE}) SET(PACKAGE_VERSION_MAJOR "1") -SET(PACKAGE_VERSION_MINOR "4") +SET(PACKAGE_VERSION_MINOR "5") SET(PACKAGE_VERSION_MICRO "0") #SET(VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}") SET(VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_MICRO}") @@ -39,6 +60,27 @@ ADD_CUSTOM_TARGET(dist COMMAND git archive --prefix=${ARCHIVE_NAME}/ -o ${ARCHIVE_NAME}.tar.xz HEAD WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) +ADD_CUSTOM_TARGET(distcheck + COMMAND chmod -R u+w ${ARCHIVE_NAME} ${ARCHIVE_NAME}-build ${ARCHIVE_NAME}-dest 2>/dev/null || true + COMMAND rm -rf ${ARCHIVE_NAME} ${ARCHIVE_NAME}-build ${ARCHIVE_NAME}-dest + COMMAND cmake -E tar xf ${ARCHIVE_NAME}.tar.gz + COMMAND chmod -R u-w ${ARCHIVE_NAME} + COMMAND mkdir ${ARCHIVE_NAME}-build + COMMAND mkdir ${ARCHIVE_NAME}-dest + COMMAND cd ${ARCHIVE_NAME}-build && cmake -DCMAKE_INSTALL_PREFIX=../${ARCHIVE_NAME}-dest ../${ARCHIVE_NAME} + COMMAND cd ${ARCHIVE_NAME}-build && make -j4 + COMMAND cd ${ARCHIVE_NAME}-build && make test + COMMAND cd ${ARCHIVE_NAME}-build && make install +# COMMAND cd ${ARCHIVE_NAME}-build && make uninstall +# COMMAND if [ `find ${ARCHIVE_NAME}-dest ! -type d | wc -l` -ne 0 ]; then echo leftover files in ${ARCHIVE_NAME}-dest; false; fi + COMMAND cd ${ARCHIVE_NAME}-build && make clean + COMMAND chmod -R u+w ${ARCHIVE_NAME} ${ARCHIVE_NAME}-build ${ARCHIVE_NAME}-dest + COMMAND rm -rf ${ARCHIVE_NAME} ${ARCHIVE_NAME}-build ${ARCHIVE_NAME}-dest + COMMAND echo "${ARCHIVE_NAME}.tar.gz is ready for distribution." + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) +ADD_DEPENDENCIES(distcheck dist) + IF(BUILD_SHARED_LIBS) SET(HAVE_SHARED TRUE) ELSE() @@ -62,7 +104,7 @@ CHECK_FUNCTION_EXISTS(_strtoui64 HAVE__STRTOUI64) CHECK_FUNCTION_EXISTS(_umask HAVE__UMASK) CHECK_FUNCTION_EXISTS(_unlink HAVE__UNLINK) CHECK_FUNCTION_EXISTS(clonefile HAVE_CLONEFILE) -CHECK_FUNCTION_EXISTS(explict_bzero HAVE_EXPLICIT_BZERO) +CHECK_FUNCTION_EXISTS(explicit_bzero HAVE_EXPLICIT_BZERO) CHECK_FUNCTION_EXISTS(explicit_memset HAVE_EXPLICIT_MEMSET) CHECK_FUNCTION_EXISTS(fileno HAVE_FILENO) CHECK_FUNCTION_EXISTS(fseeko HAVE_FSEEKO) @@ -111,6 +153,10 @@ CHECK_TYPE_SIZE("off_t" SIZEOF_OFF_T) CHECK_TYPE_SIZE("size_t" SIZE_T_LIBZIP) CHECK_TYPE_SIZE("ssize_t" SSIZE_T_LIBZIP) +CHECK_C_SOURCE_COMPILES("#include +#include +int main(int argc, char *argv[]) { unsigned long x = FICLONERANGE; }" HAVE_FICLONERANGE) + TEST_BIG_ENDIAN(WORDS_BIGENDIAN) FIND_PACKAGE(ZLIB REQUIRED) @@ -126,22 +172,47 @@ IF(BZIP2_FOUND) INCLUDE_DIRECTORIES(${BZIP2_INCLUDE_DIR}) SET (OPTIONAL_LIBRARY ${BZIP2_LIBRARY}) +ELSE() + MESSAGE(WARNING "-- bzip2 library not found; bzip2 support disabled") ENDIF(BZIP2_FOUND) +IF (COMMONCRYPTO_FOUND) + SET (HAVE_CRYPTO 1) + SET (HAVE_COMMONCRYPTO 1) +ELSEIF (OPENSSL_FOUND) + SET (HAVE_CRYPTO 1) + SET (HAVE_OPENSSL 1) + INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) + SET (OPTIONAL_LIBRARY ${OPTIONAL_LIBRARY} ${OPENSSL_LIBRARIES}) +ELSEIF (GNUTLS_FOUND AND NETTLE_FOUND) + SET (HAVE_CRYPTO 1) + SET (HAVE_GNUTLS 1) + INCLUDE_DIRECTORIES(${GNUTLS_INCLUDE_DIR} ${NETTLE_INCLUDE_DIR}) + SET (OPTIONAL_LIBRARY ${OPTIONAL_LIBRARY} ${GNUTLS_LIBRARY} ${NETTLE_LIBRARY}) +ENDIF() + +IF (NOT HAVE_CRYPTO) + MESSAGE(WARNING "-- neither openssl nor gnutls found; AES support disabled") +ENDIF() + IF(MSVC) ADD_DEFINITIONS("-D_CRT_SECURE_NO_WARNINGS") ADD_DEFINITIONS("-D_CRT_NONSTDC_NO_DEPRECATE") ENDIF(MSVC) -if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) - ADD_DEFINITIONS(-DMS_UWP) - SET (OPTIONAL_LIBRARY "${OPTIONAL_LIBRARY}" bcrypt) -endif(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) +if(WIN32) + if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) + ADD_DEFINITIONS(-DMS_UWP) + SET (OPTIONAL_LIBRARY "${OPTIONAL_LIBRARY}" bcrypt) + else(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) + SET (OPTIONAL_LIBRARY "${OPTIONAL_LIBRARY}" advapi32) + endif(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) +endif(WIN32) ADD_DEFINITIONS("-DHAVE_CONFIG_H") # rpath handling: use rpath in installed binaries -SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") +SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # Testing @@ -157,8 +228,9 @@ ADD_SUBDIRECTORY(examples) # pkgconfig file SET(prefix ${CMAKE_INSTALL_PREFIX}) SET(exec_prefix \${prefix}) -SET(libdir \${exec_prefix}/lib) -SET(includedir \${prefix}/include) +SET(bindir \${exec_prefix}/${CMAKE_INSTALL_BINDIR}) +SET(libdir \${exec_prefix}/${CMAKE_INSTALL_LIBDIR}) +SET(includedir \${prefix}/${CMAKE_INSTALL_INCLUDEDIR}) IF(BZIP2_FOUND) SET(LIBS "${LIBS} -lbz2") ENDIF() @@ -167,7 +239,89 @@ IF(CMAKE_SYSTEM_NAME MATCHES BSD) SET(PKG_CONFIG_RPATH "-Wl,-R\${libdir}") ENDIF(CMAKE_SYSTEM_NAME MATCHES BSD) CONFIGURE_FILE(libzip.pc.in libzip.pc @ONLY) -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libzip.pc DESTINATION lib/pkgconfig) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libzip.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) + +# fixed size integral types + +IF(HAVE_STDINT_H_LIBZIP) + SET(LIBZIP_TYPES_INCLUDE "#include ") +ELSEIF(HAVE_INTTYPES_H_LIBZIP) + SET(LIBZIP_TYPES_INCLUDE "#include ") +ELSEIF(HAVE_SYS_TYPES_H_LIBZIP) + SET(LIBZIP_TYPES_INCLUDE "#include ") +ENDIF() + +IF(HAVE_INT8_T_LIBZIP) + SET(ZIP_INT8_T int8_t) +ELSEIF(HAVE___INT8_LIBZIP) + SET(ZIP_INT8_T __int8) +ELSE() + SET(ZIP_INT8_T "signed char") +ENDIF() + +IF(HAVE_UINT8_T_LIBZIP) + SET(ZIP_UINT8_T uint8_t) +ELSEIF(HAVE___INT8_LIBZIP) + SET(ZIP_UINT8_T "unsigned __int8") +ELSE() + SET(ZIP_UINT8_T "unsigned char") +ENDIF() + +IF(HAVE_INT16_T_LIBZIP) + SET(ZIP_INT16_T int16_t) +ELSEIF(HAVE___INT16_LIBZIP) + SET(INT16_T_LIBZIP __int16) +ELSEIF(SHORT_LIBZIP EQUAL 2) + SET(INT16_T_LIBZIP short) +ENDIF() + +IF(HAVE_UINT16_T_LIBZIP) + SET(ZIP_UINT16_T uint16_t) +ELSEIF(HAVE___INT16_LIBZIP) + SET(UINT16_T_LIBZIP "unsigned __int16") +ELSEIF(SHORT_LIBZIP EQUAL 2) + SET(UINT16_T_LIBZIP "unsigned short") +ENDIF() + +IF(HAVE_INT32_T_LIBZIP) + SET(ZIP_INT32_T int32_t) +ELSEIF(HAVE___INT32_LIBZIP) + SET(ZIP_INT32_T __int32) +ELSEIF(INT_LIBZIP EQUAL 4) + SET(ZIP_INT32_T int) +ELSEIF(LONG_LIBZIP EQUAL 4) + SET(ZIP_INT32_T long) +ENDIF() + +IF(HAVE_UINT32_T_LIBZIP) +SET(ZIP_UINT32_T uint32_t) +ELSEIF(HAVE___INT32_LIBZIP) +SET(ZIP_UINT32_T "unsigned __int32") +ELSEIF(INT_LIBZIP EQUAL 4) +SET(ZIP_UINT32_T "unsigned int") +ELSEIF(LONG_LIBZIP EQUAL 4) +SET(ZIP_UINT32_T "unsigned long") +ENDIF() + +IF(HAVE_INT64_T_LIBZIP) + SET(ZIP_INT64_T int64_t) +ELSEIF(HAVE___INT64_LIBZIP) + SET(ZIP_INT64_T __int64) +ELSEIF(LONG_LIBZIP EQUAL 8) + SET(ZIP_INT64_T long) +ELSEIF(LONG_LONG_LIBZIP EQUAL 8) + SET(ZIP_INT64_T "long long") +ENDIF() + +IF(HAVE_UINT64_T_LIBZIP) + SET(ZIP_UINT64_T uint64_t) +ELSEIF(HAVE___INT64_LIBZIP) + SET(ZIP_UINT64_T "unsigned __int64") +ELSEIF(LONG_LIBZIP EQUAL 8) + SET(ZIP_UINT64_T "unsigned long") +ELSEIF(LONG_LONG_LIBZIP EQUAL 8) + SET(ZIP_UINT64_T "unsigned long long") +ENDIF() # write out config file CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake-config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h) @@ -188,4 +342,4 @@ FILE(COPY ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/runtest # installation -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zipconf.h DESTINATION include) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zipconf.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/FindNettle.cmake b/FindNettle.cmake new file mode 100644 index 0000000..1f66610 --- /dev/null +++ b/FindNettle.cmake @@ -0,0 +1,23 @@ +# - Find Nettle +# Find the Nettle include directory and library +# +# NETTLE_INCLUDE_DIR - where to find , etc. +# NETTLE_LIBRARIES - List of libraries when using libnettle. +# NETTLE_FOUND - True if libnettle found. + +IF (NETTLE_INCLUDE_DIR) + # Already in cache, be silent + SET(NETTLE_FIND_QUIETLY TRUE) +ENDIF (NETTLE_INCLUDE_DIR) + +FIND_PATH(NETTLE_INCLUDE_DIR nettle/md5.h nettle/ripemd160.h nettle/sha.h) +FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle) + +# handle the QUIETLY and REQUIRED arguments and set NETTLE_FOUND to TRUE if +# all listed variables are TRUE +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(NETTLE DEFAULT_MSG NETTLE_LIBRARY NETTLE_INCLUDE_DIR) + +IF(NETTLE_FOUND) + SET(NETTLE_LIBRARIES ${NETTLE_LIBRARY}) +ENDIF(NETTLE_FOUND) diff --git a/INSTALL.md b/INSTALL.md index 72079da..e3960c6 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -8,6 +8,17 @@ comes with most operating systems. For supporting bzip2-compressed zip archives, you need [bzip2](http://bzip.org/). +For AES (encryption) support, you need one of these cryptographic libraries, +listed in order of preference: + +- Apple's CommonCrypto (available on macOS and iOS) +- [OpenSSL](https://www.openssl.org/) +- [GnuTLS](https://www.gnutls.org/). + +If you don't want a library even if it is installed, you can +pass `-DENABLE_=OFF` to cmake, where `` is one of +`COMMONCRYPTO`, `OPENSSL`, or `GNUTLS`. + The basic usage is ```sh mkdir build @@ -19,6 +30,7 @@ make install ``` Some useful parameters you can pass to `cmake` with `-Dparameter=value`: + - `BUILD_SHARED_LIBS`: set to `ON` or `OFF` to enable/disable building of shared libraries, defaults to `ON` - `CMAKE_INSTALL_PREFIX`: for setting the installation path diff --git a/LICENSE b/LICENSE index cb7be68..e93454e 100644 --- a/LICENSE +++ b/LICENSE @@ -29,38 +29,3 @@ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - - -For AES encryption support, files under the following license are used: - ---------------------------------------------------------------------------- -Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. -All rights reserved. - -LICENSE TERMS - -The free distribution and use of this software in both source and binary -form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - -ALTERNATIVELY, provided that this notice is retained in full, this product -may be distributed under the terms of the GNU General Public License (GPL), -in which case the provisions of the GPL apply INSTEAD OF those given above. - -DISCLAIMER - -This software is provided 'as is' with no explicit or implied warranties -in respect of its properties, including, but not limited to, correctness -and/or fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 18th November 2008 diff --git a/NEWS.md b/NEWS.md index 7f07ea4..a4bc5f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +1.5.0 [2018-03-11] +================== + +* Use standard cryptographic library instead of custom AES implementation. + This also simplifies the license. +* Use `clang-format` to format the source code. +* More Windows improvements. + 1.4.0 [2017-12-29] ================== diff --git a/README.md b/README.md index cc9fdfb..348c453 100644 --- a/README.md +++ b/README.md @@ -30,5 +30,6 @@ to libzip-discuss at nih.at. Use this for bug reports or questions. If you want to reach the authors in private, use . -![Travis Build Status](https://api.travis-ci.org/nih-at/libzip.svg?branch=master) +[![Travis Build Status](https://api.travis-ci.org/nih-at/libzip.svg?branch=master)](https://travis-ci.org/nih-at/libzip) +[![Appveyor Build status](https://ci.appveyor.com/api/projects/status/5x1raqqjro4wny7r?svg=true)](https://ci.appveyor.com/project/0-wiz-0/libzip) ![Coverity Status](https://scan.coverity.com/projects/127/badge.svg?flat=1) diff --git a/THANKS b/THANKS index c441c0c..4c70f2c 100644 --- a/THANKS +++ b/THANKS @@ -31,6 +31,7 @@ Heiko Hund Info-ZIP group Jan Weiß Jay Freeman (saurik) +João Custódio Joel Ebrahimi Jono Spiro Julien Schueller diff --git a/TODO.md b/TODO.md index d896b33..02737bb 100644 --- a/TODO.md +++ b/TODO.md @@ -5,19 +5,24 @@ - (Readme Maturity Level)[https://github.com/LappleApple/feedmereadmes/blob/master/README-maturity-model.md] - (Github Community Profile)[https://github.com/nih-at/libzip/community] -* migration to CMake - - replace `make distcheck` - -* run Coverity tests automatically via github/travis - -* switch to newer fcrypt sources, see https://github.com/BrianGladman/AES/issues/19 +* test different crypto backends with TravisCI. * improve man page formatting of tagged lists on webpage (`
`) * test error cases with special source - tell it which command should fail - use it both as source for `zip_add` and `zip_open_from_source` - + - `ziptool_regress`: + - `-e error_spec`: source containing zip fails depending on `error_spec` + - `add_with_error name content error_spec`: add content to archive, where source fails depending on `error_spec` + - `add_file_with_error name file_to_add offset len error_spec`: add file to archive, len bytes starting from offset, where source fails depending on `error_spec` + - `error_spec`: + - source command that fails + - error code that source returns + - conditions that must be met for error to trigger + - Nth call of command + - read/write: total byte count so far + - state of source (opened, EOF reached, ...) # Later @@ -44,7 +49,6 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * rename remaining `zip_XXX_{file,archive}_*` to `zip_{file,archive}_XXX_*`? * compression/crypt implementations: how to set error code on failure * compression/crypt error messages a la `ZIP_ER_ZLIB` (no detailed info passing) -* check arguments for every entry point into libzip ## Features @@ -62,35 +66,29 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * delete all extra fields during `zip_replace()` * function to copy file from one archive to another * set `O_CLOEXEC` flag after fopen and mkstemp -* add append-only mode writing file to disk incrementally to keep memory usage low * `zip_file_set_mtime()`: support InfoZIP time stamps -* `zipcmp`: support comparing more features: - * version needed/made by - * general purpose bit flags * support streaming output (creating new archive to e.g. stdout) -* add functions to: - * read/set ASCII file flag? (more general options?) +* add function to read/set ASCII file flag * `zip_commit()` (to finish changes without closing archive) * add custom compression function support -* `zip_fseek()` * `zip_source_zip()`: allow rewinding * add `zip_abort()` to allow aborting `zip_close()` (can be called from progress callback) -* zipcmp: add option for file content comparison -* zipcmp: compare bit flags if paranoid -* zipcmp: compare external attributes/opsys if paranoid -* zipcmp: compare last_mod if paranoid (or with separate flag?) -* consistency - . for stored files, test compressed = uncompressed - . data descriptor - . local headers come before central dir - -* support for old compression methods????? +* `zipcmp`: add option for file content comparison +* `zipcmp`: add more paranoid checks: + * external attributes/opsys + * last_mod + * version needed/made by + * general purpose bit flags +* add more consistency checks: + * for stored files, test compressed = uncompressed + * data descriptor + * local headers come before central dir +* support for old compression methods? ## Bugs * support InfoZIP encryption header extension (copy data descriptor for encrypted files) * ensure that nentries is small enough not to cause overflow (size_t for entry, uint64 for CD on disk) -* fix OpenSUSE i686 regression failures * check for limits imposed by format (central dir size, file size, extra fields, ...) * `_zip_u2d_time()`: handle `localtime(3)` failure * POSIX: `zip_open()`: check whether file can be created and fail if not @@ -100,6 +98,7 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) ## Cleanup +* drop _LIBZIP suffixes in cmake defines (no longer needed since they no longer appear in zipconf.h) * go over cdir parser and rename various offset/size variables to make it clearer * use bool * use `ZIP_SOURCE_SUPPORTS_{READABLE,SEEKABLE,WRITABLE}` @@ -107,19 +106,16 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * get rid of `zip_get_{compression,encryption}_implementation()` * use `zip_*int*_t` internally -## Analysis - -* pass through coverity - ## Infrastructure -* rewrite make_zip_errors.sh in cmake -* rewrite make_zip_err_str.sh in cmake +* rewrite `make_zip_errors.sh` in cmake +* rewrite `make_zip_err_str.sh` in cmake * configure appveyor for Windows builds of libzip ## Test Case Issues -* consider testing for malloc/realloc failures (see `ckmame/regress/malloc.c`) +* add test case for clone with files > 4k +* consider testing for malloc/realloc failures * Winzip AES support * test cases decryption: <=20, >20, stat for both * test cases encryption: no password, default password, file-specific password, 128/192/256, <=20, >20 @@ -135,7 +131,6 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * delete all * modify * use gcov output to increase test coverage -* merge most tools into ziptool * add test case to change values for newly added files (name, compression method, comment, mtime, . . .) * `zip_open()` file less than `EOCDLEN` bytes long * test calls against old API @@ -144,7 +139,7 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * fix comment test to be newline insensitive * check if http://bugs.python.org/issue20078 provides ideas for new tests -* (add, replace) +* (`add`, `replace`) * add to empty zip * add to existing zip * add w/ existing file name [E] @@ -152,32 +147,32 @@ const zip_uint8_t *zip_get_archive_prefix(struct zip *za, zip_uint64_t *lengthp) * replace w/ illegal index [E] * replace w/ deleted name [E] * unchange added/replaced file -* (close) +* (`close`) * copy zip file * open copy * rename, delete, replace, add w/ new name, add w/ deleted name * close * zipcmp copy expected * remove copy -* (error_get) -* (error_get_sys_type) -* (error_to_str) -* (extra_fields) -* (file_error_get) -* (file_strerror) -* (replace) -* (source_buffer) -* (source_file) -* (source_filep) -* (source_free) -* (source_function) -* (source_zip) -* (strerror) -* (unchange) -* (unchange_all) -* open(ZIP_RDONLY) +* (`error_get) +* (`error_get_sys_type`) +* (`error_to_str`) +* (`extra_fields`) +* (`file_error_get`) +* (`file_strerror`) +* (`replace`) +* (`source_buffer`) +* (`source_file`) +* (`source_filep`) +* (`source_free`) +* (`source_function`) +* (`source_zip`) +* (`strerror`) +* (`unchange`) +* (`unchange_all`) +* `open(ZIP_RDONLY)` * I/O abstraction layer - * zip_open_from_source + * `zip_open_from_source` * read two zip entries interleaved ## Unsorted diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..2061365 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,57 @@ +os: +- Visual Studio 2017 +environment: + matrix: + - GENERATOR: "Visual Studio 15 2017 Win64" + TRIPLET: x64-windows + CMAKE_OPTS: "-DBUILD_SHARED_LIBS=off" + - GENERATOR: "Visual Studio 15 2017" + TRIPLET: x86-windows + CMAKE_OPTS: "-DBUILD_SHARED_LIBS=off" + - GENERATOR: "Visual Studio 15 2017 ARM" + TRIPLET: arm-windows + CMAKE_OPTS: "-DENABLE_OPENSSL=off" + - GENERATOR: "Visual Studio 15 2017" + TRIPLET: x86-uwp + CMAKE_OPTS: "-DCMAKE_SYSTEM_NAME=WindowsStore" + - GENERATOR: "Visual Studio 15 2017 Win64" + TRIPLET: x64-uwp + CMAKE_OPTS: "-DCMAKE_SYSTEM_NAME=WindowsStore" + - GENERATOR: "Visual Studio 15 2017 ARM" + TRIPLET: arm-uwp + CMAKE_OPTS: "-DCMAKE_SYSTEM_NAME=WindowsStore -DENABLE_OPENSSL=off" + - GENERATOR: "Visual Studio 15 2017" + TRIPLET: arm64-windows + CMAKE_OPTS: "-AARM64 -DENABLE_OPENSSL=off" + - GENERATOR: "Visual Studio 15 2017" + TRIPLET: arm64-uwp + CMAKE_OPTS: "-AARM64 -DCMAKE_SYSTEM_NAME=WindowsStore -DENABLE_OPENSSL=off" +before_build: + cmd: >- + git clone https://github.com/Microsoft/vcpkg + + cd vcpkg + + .\bootstrap-vcpkg.bat + + .\vcpkg integrate install + + .\vcpkg install zlib:%TRIPLET% bzip2:%TRIPLET% + + cd .. + + mkdir build + + cd build + + cmake -DCMAKE_TOOLCHAIN_FILE=%cd%/../vcpkg/scripts/buildsystems/vcpkg.cmake .. -G "%GENERATOR%" %CMAKE_OPTS% + + appveyor PushArtifact config.h + + appveyor PushArtifact CMakeCache.txt + +build_script: + cmd: >- + cmake --build . --config Release --target INSTALL + + cmake --build . --config Debug --target INSTALL diff --git a/cmake-config.h.in b/cmake-config.h.in index 0624a2b..7e252b9 100644 --- a/cmake-config.h.in +++ b/cmake-config.h.in @@ -20,12 +20,17 @@ #cmakedefine HAVE__UMASK #cmakedefine HAVE__UNLINK #cmakedefine HAVE_CLONEFILE +#cmakedefine HAVE_COMMONCRYPTO +#cmakedefine HAVE_CRYPTO +#cmakedefine HAVE_FICLONERANGE #cmakedefine HAVE_FILENO #cmakedefine HAVE_FSEEKO #cmakedefine HAVE_FTELLO #cmakedefine HAVE_GETPROGNAME +#cmakedefine HAVE_GNUTLS #cmakedefine HAVE_LIBBZ2 #cmakedefine HAVE_OPEN +#cmakedefine HAVE_OPENSSL #cmakedefine HAVE_MKSTEMP #cmakedefine HAVE_SETMODE #cmakedefine HAVE_SNPRINTF @@ -51,6 +56,10 @@ #cmakedefine __INT64_LIBZIP ${__INT64_LIBZIP} #cmakedefine INT64_T_LIBZIP ${INT64_T_LIBZIP} #cmakedefine UINT64_T_LIBZIP ${UINT64_T_LIBZIP} +#cmakedefine SHORT_LIBZIP ${SHORT_LIBZIP} +#cmakedefine INT_LIBZIP ${INT_LIBZIP} +#cmakedefine LONG_LIBZIP ${LONG_LIBZIP} +#cmakedefine LONG_LONG_LIBZIP ${LONG_LONG_LIBZIP} #cmakedefine SIZEOF_OFF_T ${SIZEOF_OFF_T} #cmakedefine SIZE_T_LIBZIP ${SIZE_T_LIBZIP} #cmakedefine SSIZE_T_LIBZIP ${SSIZE_T_LIBZIP} diff --git a/cmake-zipconf.h.in b/cmake-zipconf.h.in index f0e24e8..6ff4fe3 100644 --- a/cmake-zipconf.h.in +++ b/cmake-zipconf.h.in @@ -15,99 +15,16 @@ #cmakedefine ZIP_STATIC -#cmakedefine HAVE_INTTYPES_H_LIBZIP -#cmakedefine HAVE_STDINT_H_LIBZIP -#cmakedefine HAVE_SYS_TYPES_H_LIBZIP -#cmakedefine HAVE___INT8_LIBZIP -#cmakedefine HAVE_INT8_T_LIBZIP -#cmakedefine HAVE_UINT8_T_LIBZIP -#cmakedefine HAVE___INT16_LIBZIP -#cmakedefine HAVE_INT16_T_LIBZIP -#cmakedefine HAVE_UINT16_T_LIBZIP -#cmakedefine HAVE___INT32_LIBZIP -#cmakedefine HAVE_INT32_T_LIBZIP -#cmakedefine HAVE_UINT32_T_LIBZIP -#cmakedefine HAVE___INT64_LIBZIP -#cmakedefine HAVE_INT64_T_LIBZIP -#cmakedefine HAVE_UINT64_T_LIBZIP -#cmakedefine HAVE_SSIZE_T_LIBZIP -#cmakedefine SHORT_LIBZIP ${SHORT_LIBZIP} -#cmakedefine INT_LIBZIP ${INT_LIBZIP} -#cmakedefine LONG_LIBZIP ${LONG_LIBZIP} -#cmakedefine LONG_LONG_LIBZIP ${LONG_LONG_LIBZIP} - -#if defined(HAVE_STDINT_H_LIBZIP) -#include -#elif defined(HAVE_INTTYPES_H_LIBZIP) -#include -#elif defined(HAVE_SYS_TYPES_H_LIBZIP) -#include -#endif - -#if defined(HAVE_INT8_T_LIBZIP) -typedef int8_t zip_int8_t; -#elif defined(HAVE___INT8_LIBZIP) -typedef __int8 zip_int8_t; -#else -typedef signed char zip_int8_t; -#endif -#if defined(HAVE_UINT8_T_LIBZIP) -typedef uint8_t zip_uint8_t; -#elif defined(HAVE___INT8_LIBZIP) -typedef unsigned __int8 zip_uint8_t; -#else -typedef unsigned char zip_uint8_t; -#endif -#if defined(HAVE_INT16_T_LIBZIP) -typedef int16_t zip_int16_t; -#elif defined(HAVE___INT16_LIBZIP) -typedef __int16 zip_int16_t; -#elif defined(SHORT_LIBZIP) && SHORT_LIBZIP == 2 -typedef signed short zip_int16_t; -#endif -#if defined(HAVE_UINT16_T_LIBZIP) -typedef uint16_t zip_uint16_t; -#elif defined(HAVE___INT16_LIBZIP) -typedef unsigned __int16 zip_uint16_t; -#elif defined(SHORT_LIBZIP) && SHORT_LIBZIP == 2 -typedef unsigned short zip_uint16_t; -#endif -#if defined(HAVE_INT32_T_LIBZIP) -typedef int32_t zip_int32_t; -#elif defined(HAVE___INT32_LIBZIP) -typedef __int32 zip_int32_t; -#elif defined(INT_LIBZIP) && INT_LIBZIP == 4 -typedef signed int zip_int32_t; -#elif defined(LONG_LIBZIP) && LONG_LIBZIP == 4 -typedef signed long zip_int32_t; -#endif -#if defined(HAVE_UINT32_T_LIBZIP) -typedef uint32_t zip_uint32_t; -#elif defined(HAVE___INT32_LIBZIP) -typedef unsigned __int32 zip_uint32_t; -#elif defined(INT_LIBZIP) && INT_LIBZIP == 4 -typedef unsigned int zip_uint32_t; -#elif defined(LONG_LIBZIP) && LONG_LIBZIP == 4 -typedef unsigned long zip_uint32_t; -#endif -#if defined(HAVE_INT64_T_LIBZIP) -typedef int64_t zip_int64_t; -#elif defined(HAVE___INT64_LIBZIP) -typedef __int64 zip_int64_t; -#elif defined(LONG_LIBZIP) && LONG_LIBZIP == 8 -typedef signed long zip_int64_t; -#elif defined(LONG_LONG_LIBZIP) && LONG_LONG_LIBZIP == 8 -typedef signed long long zip_int64_t; -#endif -#if defined(HAVE_UINT64_T_LIBZIP) -typedef uint64_t zip_uint64_t; -#elif defined(HAVE___INT64_LIBZIP) -typedef unsigned __int64 zip_uint64_t; -#elif defined(LONG_LIBZIP) && LONG_LONG_LIBZIP == 8 -typedef unsigned long zip_uint64_t; -#elif defined(LONG_LONG_LIBZIP) && LONG_LONG_LIBZIP == 8 -typedef unsigned long long zip_uint64_t; -#endif +${LIBZIP_TYPES_INCLUDE} + +typedef ${ZIP_INT8_T} zip_int8_t; +typedef ${ZIP_UINT8_T} zip_uint8_t; +typedef ${ZIP_INT16_T} zip_int16_t; +typedef ${ZIP_UINT16_T} zip_uint16_t; +typedef ${ZIP_INT32_T} zip_int32_t; +typedef ${ZIP_UINT32_T} zip_uint32_t; +typedef ${ZIP_INT64_T} zip_int64_t; +typedef ${ZIP_UINT64_T} zip_uint64_t; #define ZIP_INT8_MIN (-ZIP_INT8_MAX-1) #define ZIP_INT8_MAX 0x7f diff --git a/examples/in-memory.c b/examples/in-memory.c index f618e21..37e84bd 100644 --- a/examples/in-memory.c +++ b/examples/in-memory.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -40,13 +40,12 @@ #include static int -get_data(void **datap, size_t *sizep, const char *archive) -{ +get_data(void **datap, size_t *sizep, const char *archive) { /* example implementation that reads data from file */ struct stat st; FILE *fp; - if ((fp=fopen(archive, "r")) == NULL) { + if ((fp = fopen(archive, "r")) == NULL) { if (errno != ENOENT) { fprintf(stderr, "can't open %s: %s\n", archive, strerror(errno)); return -1; @@ -54,14 +53,14 @@ get_data(void **datap, size_t *sizep, const char *archive) *datap = NULL; *sizep = 0; - + return 0; } if (fstat(fileno(fp), &st) < 0) { - fprintf(stderr, "can't stat %s: %s\n", archive, strerror(errno)); - fclose(fp); - return -1; + fprintf(stderr, "can't stat %s: %s\n", archive, strerror(errno)); + fclose(fp); + return -1; } if ((*datap = malloc((size_t)st.st_size)) == NULL) { @@ -72,7 +71,7 @@ get_data(void **datap, size_t *sizep, const char *archive) if (fread(*datap, 1, (size_t)st.st_size, fp) < (size_t)st.st_size) { fprintf(stderr, "can't read %s: %s\n", archive, strerror(errno)); - free(*datap); + free(*datap); fclose(fp); return -1; } @@ -84,16 +83,14 @@ get_data(void **datap, size_t *sizep, const char *archive) } static int -modify_archive(zip_t *za) -{ +modify_archive(zip_t *za) { /* modify the archive */ return 0; } static int -use_data(void *data, size_t size, const char *archive) -{ +use_data(void *data, size_t size, const char *archive) { /* example implementation that writes data to file */ FILE *fp; @@ -124,8 +121,7 @@ use_data(void *data, size_t size, const char *archive) int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { const char *archive; zip_source_t *src; zip_t *za; diff --git a/examples/windows-open.c b/examples/windows-open.c index fdd7034..1dbea01 100644 --- a/examples/windows-open.c +++ b/examples/windows-open.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,8 +34,7 @@ #include zip_t * -windows_open(const wchar_t *name, int flags) -{ +windows_open(const wchar_t *name, int flags) { zip_source_t *src; zip_t *za; zip_error_t error; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 855ccf5..be4db6d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,6 +1,6 @@ INCLUDE(CheckFunctionExists) -INSTALL(FILES zip.h DESTINATION include) +INSTALL(FILES zip.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # from http://www.cmake.org/Wiki/CMakeMacroLibtoolFile MACRO(GET_TARGET_PROPERTY_WITH_DEFAULT _variable _target _property _default_value) @@ -51,11 +51,9 @@ MACRO(CREATE_LIBTOOL_FILE _target _install_DIR) ENDMACRO() SET(LIBZIP_SOURCES - gladman-fcrypt.c zip_add.c zip_add_dir.c zip_add_entry.c - zip_algorithm_bzip2.c zip_algorithm_deflate.c zip_buffer.c zip_close.c @@ -146,8 +144,6 @@ SET(LIBZIP_SOURCES zip_source_tell.c zip_source_tell_write.c zip_source_window.c - zip_source_winzip_aes_decode.c - zip_source_winzip_aes_encode.c zip_source_write.c zip_source_zip.c zip_source_zip_new.c @@ -170,25 +166,18 @@ IF(WIN32) zip_source_win32w.c ) IF(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore) - SET(LIBZIP_OPSYS_FILES "${LIBZIP_OPSYS_FILES}" - zip_random_uwp.c - ) ELSE() SET(LIBZIP_OPSYS_FILES "${LIBZIP_OPSYS_FILES}" - zip_random_win32.c zip_source_win32a.c ) ENDIF() ELSE(WIN32) SET(LIBZIP_OPSYS_FILES - zip_random_unix.c zip_source_file.c ) ENDIF(WIN32) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/gladman-fcrypt - ${CMAKE_CURRENT_BINARY_DIR}/..) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/..) ADD_CUSTOM_TARGET(update_zip_err_str COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/make_zip_err_str.sh ${CMAKE_CURRENT_SOURCE_DIR}/zip.h ${CMAKE_CURRENT_SOURCE_DIR}/zip_err_str.c @@ -198,12 +187,31 @@ CHECK_FUNCTION_EXISTS(mkstemp HAVE_MKSTEMP) IF(NOT HAVE_MKSTEMP) SET(LIBZIP_EXTRA_FILES mkstemp.c) ENDIF(NOT HAVE_MKSTEMP) +IF(HAVE_LIBBZ2) + SET(LIBZIP_OPTIONAL_FILES zip_algorithm_bzip2.c) +ENDIF() + +IF(HAVE_COMMONCRYPTO) + SET(LIBZIP_OPTIONAL_FILES ${LIBZIP_OPTIONAL_FILES} zip_crypto_commoncrypto.c +) +ELSEIF(HAVE_GNUTLS) + SET(LIBZIP_OPTIONAL_FILES ${LIBZIP_OPTIONAL_FILES} zip_crypto_gnutls.c +) +ELSEIF(HAVE_OPENSSL) + SET(LIBZIP_OPTIONAL_FILES ${LIBZIP_OPTIONAL_FILES} zip_crypto_openssl.c +) +ENDIF() + +IF(HAVE_CRYPTO) + SET(LIBZIP_OPTIONAL_FILES ${LIBZIP_OPTIONAL_FILES} zip_winzip_aes.c zip_source_winzip_aes_decode.c zip_source_winzip_aes_encode.c +) +ENDIF() -ADD_LIBRARY(zip ${LIBZIP_SOURCES} ${LIBZIP_EXTRA_FILES} ${LIBZIP_OPSYS_FILES}) +ADD_LIBRARY(zip ${LIBZIP_SOURCES} ${LIBZIP_EXTRA_FILES} ${LIBZIP_OPTIONAL_FILES} ${LIBZIP_OPSYS_FILES}) SET_TARGET_PROPERTIES(zip PROPERTIES VERSION 5.0 SOVERSION 5) TARGET_LINK_LIBRARIES(zip ${ZLIB_LIBRARY} ${OPTIONAL_LIBRARY}) INSTALL(TARGETS zip - RUNTIME DESTINATION bin - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib) + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) #CREATE_LIBTOOL_FILE(zip lib) diff --git a/lib/compat.h b/lib/compat.h index f2a5467..0df78d1 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -20,7 +20,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,6 +34,8 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "zipconf.h" + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -42,7 +44,11 @@ #define __STDC_LIMIT_MACROS #ifdef _WIN32 +#ifndef ZIP_EXTERN +#ifndef ZIP_STATIC #define ZIP_EXTERN __declspec(dllexport) +#endif +#endif /* for dup(), close(), etc. */ #include #endif @@ -51,8 +57,8 @@ #include #else typedef char bool; -#define true 1 -#define false 0 +#define true 1 +#define false 0 #endif #include @@ -73,57 +79,57 @@ typedef char bool; #ifdef _WIN32 #if defined(HAVE__CHMOD) -#define chmod _chmod +#define chmod _chmod #endif #if defined(HAVE__CLOSE) -#define close _close +#define close _close #endif #if defined(HAVE__DUP) -#define dup _dup +#define dup _dup #endif /* crashes reported when using fdopen instead of _fdopen on Windows/Visual Studio 10/Win64 */ #if defined(HAVE__FDOPEN) -#define fdopen _fdopen +#define fdopen _fdopen #endif #if !defined(HAVE_FILENO) && defined(HAVE__FILENO) -#define fileno _fileno +#define fileno _fileno #endif /* Windows' open() doesn't understand Unix permissions */ #if defined(HAVE__OPEN) -#define open(a, b, c) _open((a), (b)) +#define open(a, b, c) _open((a), (b)) #endif #if defined(HAVE__SNPRINTF) -#define snprintf _snprintf +#define snprintf _snprintf #endif #if defined(HAVE__STRDUP) #if !defined(HAVE_STRDUP) || defined(_WIN32) #undef strdup -#define strdup _strdup +#define strdup _strdup #endif #endif #if !defined(HAVE__SETMODE) && defined(HAVE_SETMODE) -#define _setmode setmode +#define _setmode setmode #endif #if !defined(HAVE_STRTOLL) && defined(HAVE__STRTOI64) -#define strtoll _strtoi64 +#define strtoll _strtoi64 #endif #if !defined(HAVE_STRTOULL) && defined(HAVE__STRTOUI64) -#define strtoull _strtoui64 +#define strtoull _strtoui64 #endif #if defined(HAVE__UMASK) -#define umask _umask +#define umask _umask #endif #if defined(HAVE__UNLINK) -#define unlink _unlink +#define unlink _unlink #endif #endif #ifndef HAVE_FSEEKO -#define fseeko(s, o, w) (fseek((s), (long int)(o), (w))) +#define fseeko(s, o, w) (fseek((s), (long int)(o), (w))) #endif #ifndef HAVE_FTELLO -#define ftello(s) ((long)ftell((s))) +#define ftello(s) ((long)ftell((s))) #endif #ifndef HAVE_MKSTEMP @@ -133,9 +139,9 @@ int _zip_mkstemp(char *); #if !defined(HAVE_STRCASECMP) #if defined(HAVE__STRICMP) -#define strcasecmp _stricmp +#define strcasecmp _stricmp #elif defined(HAVE_STRICMP) -#define strcasecmp stricmp +#define strcasecmp stricmp #endif #endif @@ -190,7 +196,7 @@ int _zip_mkstemp(char *); #endif #ifndef S_ISDIR -#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) #endif #endif /* compat.h */ diff --git a/lib/gladman-fcrypt.h b/lib/gladman-fcrypt.h deleted file mode 100644 index fc61b67..0000000 --- a/lib/gladman-fcrypt.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - gladman-fcrypt.h -- wrapper functions for Dr Gladman's AES functions - Copyright (C) 2016 Dieter Baron and Thomas Klausner - - This file is part of libzip, a library to manipulate ZIP archives. - The authors can be contacted at - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - 3. The names of the authors may not be used to endorse or promote - products derived from this software without specific prior - written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS - OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER - IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN - IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef HAD_GLADMAN_FCRYPT_H -#define HAD_GLADMAN_FCRYPT_H - -/* implementation fetched from - * http://173.254.28.24/~brgladma//oldsite/cryptography_technology/fileencrypt/files.zip - * - * Files dated 01-27-2004 except main.c dated 11-18-2008. - */ - -#include "zipint.h" - -#define INTERNAL static - -#include "fileenc.h" - -#endif /* HAD_GLADMAN_FCRYPT_H */ diff --git a/lib/gladman-fcrypt/aes.h b/lib/gladman-fcrypt/aes.h deleted file mode 100644 index d8a5f93..0000000 --- a/lib/gladman-fcrypt/aes.h +++ /dev/null @@ -1,128 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the definitions required to use AES and Rijndael in C. -*/ - -#ifndef _AES_H -#define _AES_H - -#if defined(__cplusplus) -extern "C" -{ -#if 0 -} -#endif -#endif - -/* If a table pointer is needed in the AES context, include the define */ -/* #define AES_TABLE_PTR */ - -#include "config.h" - -typedef zip_uint8_t aes_08t; -typedef zip_uint32_t aes_32t; - -/* This BLOCK_SIZE is in BYTES. It can have the values 16, 24, 32 or */ -/* undefined for use with aescrypt.c and aeskey.c, or 16, 20, 24, 28, */ -/* 32 or undefined for use with aescrypp.c and aeskeypp.c. When the */ -/* BLOCK_SIZE is left undefined a version that provides a dynamically */ -/* variable block size is produced but this is MUCH slower. */ - -#define BLOCK_SIZE 16 - -/* key schedule length (in 32-bit words) */ - -#if !defined(BLOCK_SIZE) -#define KS_LENGTH 128 -#else -#define KS_LENGTH (4 * BLOCK_SIZE) -#endif - -typedef unsigned int aes_fret; /* type for function return value */ -#define aes_bad 0 /* bad function return value */ -#define aes_good 1 /* good function return value */ -#ifndef AES_DLL /* implement normal or DLL functions */ -#define aes_rval aes_fret -#else -#define aes_rval aes_fret __declspec(dllexport) _stdcall -#endif - -typedef struct /* the AES context for encryption */ -{ aes_32t k_sch[KS_LENGTH]; /* the encryption key schedule */ - aes_32t n_rnd; /* the number of cipher rounds */ - aes_32t n_blk; /* the number of bytes in the state */ -#if defined(AES_TABLE_PTR) /* where global variables are not */ - void *t_ptr; /* available this pointer is used */ -#endif /* to point to the fixed tables */ -} aes_ctx; - -/* The block length (blen) is input in bytes when it is in the range */ -/* 16 <= blen <= 32 or in bits when in the range 128 <= blen <= 256 */ -/* Only 16 bytes (128 bits) is legal for AES but the files aescrypt.c */ -/* and aeskey.c provide support for 16, 24 and 32 byte (128, 192 and */ -/* 256 bit) blocks while aescrypp.c and aeskeypp.c provide support for */ -/* 16, 20, 24, 28 and 32 byte (128, 160, 192, 224 and 256 bit) blocks. */ -/* The value aes_good is returned if the requested block size is legal, */ -/* otherwise aes_bad is returned. */ - -#if !defined(BLOCK_SIZE) -INTERNAL aes_rval aes_set_block_size(unsigned int blen, aes_ctx cx[1]); -#endif - -/* The key length (klen) is input in bytes when it is in the range */ -/* 16 <= klen <= 32 or in bits when in the range 128 <= klen <= 256 */ -/* The files aescrypt.c and aeskey.c provide support for 16, 24 and */ -/* 32 byte (128, 192 and 256 bit) keys while aescrypp.c and aeskeypp.c */ -/* provide support for 16, 20, 24, 28 and 32 byte (128, 160, 192, 224 */ -/* and 256 bit) keys. The value aes_good is returned if the requested */ -/* key size is legal, otherwise aes_bad is returned. */ - -#ifdef BUILDING_FCRYPT -INTERNAL aes_rval aes_set_encrypt_key(const unsigned char in_key[], - unsigned int klen, aes_ctx cx[1]); -INTERNAL aes_rval aes_encrypt_block(const unsigned char in_blk[], - unsigned char out_blk[], const aes_ctx cx[1]); -#endif - -#if 0 /* not used */ -INTERNAL aes_rval aes_set_decrypt_key(const unsigned char in_key[], - unsigned int klen, aes_ctx cx[1]); -INTERNAL aes_rval aes_decrypt_block(const unsigned char in_blk[], - unsigned char out_blk[], const aes_ctx cx[1]); -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/aescrypt.c b/lib/gladman-fcrypt/aescrypt.c deleted file mode 100644 index 923d617..0000000 --- a/lib/gladman-fcrypt/aescrypt.c +++ /dev/null @@ -1,450 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the code for implementing encryption and decryption - for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It - can optionally be replaced by code written in assembler using NASM. -*/ - -#include "aesopt.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#if defined(BLOCK_SIZE) && (BLOCK_SIZE & 7) -#error An illegal block size has been specified. -#endif - -#define libzip_fcrypt_unused 77 /* Sunset Strip */ - -#define si(y,x,k,c) (s(y,c) = word_in(x + 4 * c) ^ k[c]) -#define so(y,x,c) word_out(y + 4 * c, s(x,c)) - -#if BLOCK_SIZE == 16 - -#if defined(ARRAYS) -#define locals(y,x) x[4],y[4] -#else -#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3 - /* - the following defines prevent the compiler requiring the declaration - of generated but unused variables in the fwd_var and inv_var macros - */ -#define b04 libzip_fcrypt_unused -#define b05 libzip_fcrypt_unused -#define b06 libzip_fcrypt_unused -#define b07 libzip_fcrypt_unused -#define b14 libzip_fcrypt_unused -#define b15 libzip_fcrypt_unused -#define b16 libzip_fcrypt_unused -#define b17 libzip_fcrypt_unused -#endif -#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ - s(y,2) = s(x,2); s(y,3) = s(x,3); -#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3) -#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) -#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3) - -#elif BLOCK_SIZE == 24 - -#if defined(ARRAYS) -#define locals(y,x) x[6],y[6] -#else -#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5, \ - y##0,y##1,y##2,y##3,y##4,y##5 -#define b06 libzip_fcrypt_unused -#define b07 libzip_fcrypt_unused -#define b16 libzip_fcrypt_unused -#define b17 libzip_fcrypt_unused -#endif -#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ - s(y,2) = s(x,2); s(y,3) = s(x,3); \ - s(y,4) = s(x,4); s(y,5) = s(x,5); -#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); \ - si(y,x,k,3); si(y,x,k,4); si(y,x,k,5) -#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); \ - so(y,x,3); so(y,x,4); so(y,x,5) -#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); \ - rm(y,x,k,3); rm(y,x,k,4); rm(y,x,k,5) -#else - -#if defined(ARRAYS) -#define locals(y,x) x[8],y[8] -#else -#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5,x##6,x##7, \ - y##0,y##1,y##2,y##3,y##4,y##5,y##6,y##7 -#endif -#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ - s(y,2) = s(x,2); s(y,3) = s(x,3); \ - s(y,4) = s(x,4); s(y,5) = s(x,5); \ - s(y,6) = s(x,6); s(y,7) = s(x,7); - -#if BLOCK_SIZE == 32 - -#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3); \ - si(y,x,k,4); si(y,x,k,5); si(y,x,k,6); si(y,x,k,7) -#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3); \ - so(y,x,4); so(y,x,5); so(y,x,6); so(y,x,7) -#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3); \ - rm(y,x,k,4); rm(y,x,k,5); rm(y,x,k,6); rm(y,x,k,7) -#else - -#define state_in(y,x,k) \ -switch(nc) \ -{ case 8: si(y,x,k,7); si(y,x,k,6); \ - case 6: si(y,x,k,5); si(y,x,k,4); \ - case 4: si(y,x,k,3); si(y,x,k,2); \ - si(y,x,k,1); si(y,x,k,0); \ -} - -#define state_out(y,x) \ -switch(nc) \ -{ case 8: so(y,x,7); so(y,x,6); \ - case 6: so(y,x,5); so(y,x,4); \ - case 4: so(y,x,3); so(y,x,2); \ - so(y,x,1); so(y,x,0); \ -} - -#if defined(FAST_VARIABLE) - -#define round(rm,y,x,k) \ -switch(nc) \ -{ case 8: rm(y,x,k,7); rm(y,x,k,6); \ - rm(y,x,k,5); rm(y,x,k,4); \ - rm(y,x,k,3); rm(y,x,k,2); \ - rm(y,x,k,1); rm(y,x,k,0); \ - break; \ - case 6: rm(y,x,k,5); rm(y,x,k,4); \ - rm(y,x,k,3); rm(y,x,k,2); \ - rm(y,x,k,1); rm(y,x,k,0); \ - break; \ - case 4: rm(y,x,k,3); rm(y,x,k,2); \ - rm(y,x,k,1); rm(y,x,k,0); \ - break; \ -} -#else - -#define round(rm,y,x,k) \ -switch(nc) \ -{ case 8: rm(y,x,k,7); rm(y,x,k,6); \ - case 6: rm(y,x,k,5); rm(y,x,k,4); \ - case 4: rm(y,x,k,3); rm(y,x,k,2); \ - rm(y,x,k,1); rm(y,x,k,0); \ -} - -#endif - -#endif -#endif - -#if defined(ENCRYPTION) && !defined(AES_ASM) - -/* Given the column (c) of the output state variable, the following - macros give the input state variables which are needed in its - computation for each row (r) of the state. All the alternative - macros give the same end values but expand into different ways - of calculating these values. In particular the complex macro - used for dynamically variable block sizes is designed to expand - to a compile time constant whenever possible but will expand to - conditional clauses on some branches (I am grateful to Frank - Yellin for this construction) -*/ - -#if defined(BLOCK_SIZE) -#if BLOCK_SIZE == 16 -# define fwd_var(x,r,c) s(x,((r+c)%nc)) -#else -#define fwd_var(x,r,c) s(x,(r+c+(((r>1)&&(nc>9-r))?1:0))%nc) -#endif -#else -#define fwd_var(x,r,c)\ - ( r == 0 ? s(x,c) \ - : r == 1 ? \ - ( c == 0 ? s(x,1) \ - : c == 1 ? s(x,2) \ - : c == 2 ? s(x,3) \ - : c == 3 ? nc == 4 ? s(x,0) : s(x,4) \ - : c == 4 ? s(x,5) \ - : c == 5 ? nc == 8 ? s(x,6) : s(x,0) \ - : c == 6 ? s(x,7) : s(x,0)) \ - : r == 2 ? \ - ( c == 0 ? nc == 8 ? s(x,3) : s(x,2) \ - : c == 1 ? nc == 8 ? s(x,4) : s(x,3) \ - : c == 2 ? nc == 4 ? s(x,0) : nc == 8 ? s(x,5) : s(x,4) \ - : c == 3 ? nc == 4 ? s(x,1) : nc == 8 ? s(x,6) : s(x,5) \ - : c == 4 ? nc == 8 ? s(x,7) : s(x,0) \ - : c == 5 ? nc == 8 ? s(x,0) : s(x,1) \ - : c == 6 ? s(x,1) : s(x,2)) \ - : \ - ( c == 0 ? nc == 8 ? s(x,4) : s(x,3) \ - : c == 1 ? nc == 4 ? s(x,0) : nc == 8 ? s(x,5) : s(x,4) \ - : c == 2 ? nc == 4 ? s(x,1) : nc == 8 ? s(x,6) : s(x,5) \ - : c == 3 ? nc == 4 ? s(x,2) : nc == 8 ? s(x,7) : s(x,0) \ - : c == 4 ? nc == 8 ? s(x,0) : s(x,1) \ - : c == 5 ? nc == 8 ? s(x,1) : s(x,2) \ - : c == 6 ? s(x,2) : s(x,3))) -#endif - -#if defined(FT4_SET) -#undef dec_fmvars -#define dec_fmvars -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) -#elif defined(FT1_SET) -#undef dec_fmvars -#define dec_fmvars -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c)) -#else -#define fwd_rnd(y,x,k,c) (s(y,c) = fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)) ^ (k)[c]) -#endif - -#if defined(FL4_SET) -#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c)) -#elif defined(FL1_SET) -#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c)) -#else -#define fwd_lrnd(y,x,k,c) (s(y,c) = no_table(x,t_use(s,box),fwd_var,rf1,c) ^ (k)[c]) -#endif - -INTERNAL aes_rval aes_encrypt_block(const unsigned char in_blk[], unsigned char out_blk[], const aes_ctx cx[1]) -{ aes_32t locals(b0, b1); - const aes_32t *kp = cx->k_sch; - dec_fmvars /* declare variables for fwd_mcol() if needed */ - - if(!(cx->n_blk & 1)) return aes_bad; - - state_in(b0, in_blk, kp); - -#if (ENC_UNROLL == FULL) - - kp += (cx->n_rnd - 9) * nc; - - /*lint -e{616} control flows into case/default */ - switch(cx->n_rnd) - { - case 14: - round(fwd_rnd, b1, b0, kp - 4 * nc); - round(fwd_rnd, b0, b1, kp - 3 * nc); - /* fallthrough */ - case 12: - round(fwd_rnd, b1, b0, kp - 2 * nc); - round(fwd_rnd, b0, b1, kp - nc); - /* fallthrough */ - case 10: - round(fwd_rnd, b1, b0, kp ); - round(fwd_rnd, b0, b1, kp + nc); - round(fwd_rnd, b1, b0, kp + 2 * nc); - round(fwd_rnd, b0, b1, kp + 3 * nc); - round(fwd_rnd, b1, b0, kp + 4 * nc); - round(fwd_rnd, b0, b1, kp + 5 * nc); - round(fwd_rnd, b1, b0, kp + 6 * nc); - round(fwd_rnd, b0, b1, kp + 7 * nc); - round(fwd_rnd, b1, b0, kp + 8 * nc); - round(fwd_lrnd, b0, b1, kp + 9 * nc); - /* fallthrough */ - default: - ; - } -#else - -#if (ENC_UNROLL == PARTIAL) - { aes_32t rnd; - for(rnd = 0; rnd < (cx->n_rnd >> 1) - 1; ++rnd) - { - kp += nc; - round(fwd_rnd, b1, b0, kp); - kp += nc; - round(fwd_rnd, b0, b1, kp); - } - kp += nc; - round(fwd_rnd, b1, b0, kp); -#else - { aes_32t rnd, *p0 = b0, *p1 = b1, *pt; - for(rnd = 0; rnd < cx->n_rnd - 1; ++rnd) - { - kp += nc; - round(fwd_rnd, p1, p0, kp); - pt = p0, p0 = p1, p1 = pt; - } -#endif - kp += nc; - round(fwd_lrnd, b0, b1, kp); - } -#endif - - state_out(out_blk, b0); - return aes_good; -} - -#endif - -#if defined(DECRYPTION) && !defined(AES_ASM) - -/* Given the column (c) of the output state variable, the following - macros give the input state variables which are needed in its - computation for each row (r) of the state. All the alternative - macros give the same end values but expand into different ways - of calculating these values. In particular the complex macro - used for dynamically variable block sizes is designed to expand - to a compile time constant whenever possible but will expand to - conditional clauses on some branches (I am grateful to Frank - Yellin for this construction) -*/ - -#if defined(BLOCK_SIZE) -#if BLOCK_SIZE == 16 -#define inv_var(x,r,c) s(x,((4+c-r)%nc)) -#else -#define inv_var(x,r,c) s(x,(840+c-r-(((r>1)&&(nc>9-r))?1:0))%nc) -#endif -#else -#define inv_var(x,r,c)\ - ( r == 0 ? s(x,c) \ - : r == 1 ? \ - ( c == 0 ? nc == 4 ? s(x,3) : nc == 8 ? s(x,7) : s(x,5) \ - : c == 1 ? s(x,0) \ - : c == 2 ? s(x,1) \ - : c == 3 ? s(x,2) \ - : c == 4 ? s(x,3) \ - : c == 5 ? s(x,4) \ - : c == 6 ? s(x,5) : s(x,6)) \ - : r == 2 ? \ - ( c == 0 ? nc == 4 ? s(x,2) : nc == 8 ? s(x,5) : s(x,4) \ - : c == 1 ? nc == 4 ? s(x,3) : nc == 8 ? s(x,6) : s(x,5) \ - : c == 2 ? nc == 8 ? s(x,7) : s(x,0) \ - : c == 3 ? nc == 8 ? s(x,0) : s(x,1) \ - : c == 4 ? nc == 8 ? s(x,1) : s(x,2) \ - : c == 5 ? nc == 8 ? s(x,2) : s(x,3) \ - : c == 6 ? s(x,3) : s(x,4)) \ - : \ - ( c == 0 ? nc == 4 ? s(x,1) : nc == 8 ? s(x,4) : s(x,3) \ - : c == 1 ? nc == 4 ? s(x,2) : nc == 8 ? s(x,5) : s(x,4) \ - : c == 2 ? nc == 4 ? s(x,3) : nc == 8 ? s(x,6) : s(x,5) \ - : c == 3 ? nc == 8 ? s(x,7) : s(x,0) \ - : c == 4 ? nc == 8 ? s(x,0) : s(x,1) \ - : c == 5 ? nc == 8 ? s(x,1) : s(x,2) \ - : c == 6 ? s(x,2) : s(x,3))) -#endif - -#if defined(IT4_SET) -#undef dec_imvars -#define dec_imvars -#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c)) -#elif defined(IT1_SET) -#undef dec_imvars -#define dec_imvars -#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c)) -#else -#define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol(no_table(x,t_use(i,box),inv_var,rf1,c) ^ (k)[c])) -#endif - -#if defined(IL4_SET) -#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c)) -#elif defined(IL1_SET) -#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c)) -#else -#define inv_lrnd(y,x,k,c) (s(y,c) = no_table(x,t_use(i,box),inv_var,rf1,c) ^ (k)[c]) -#endif - -INTERNAL aes_rval aes_decrypt_block(const unsigned char in_blk[], unsigned char out_blk[], const aes_ctx cx[1]) -{ aes_32t locals(b0, b1); - const aes_32t *kp = cx->k_sch + nc * cx->n_rnd; - dec_imvars /* declare variables for inv_mcol() if needed */ - - if(!(cx->n_blk & 2)) return aes_bad; - - state_in(b0, in_blk, kp); - -#if (DEC_UNROLL == FULL) - - kp = cx->k_sch + 9 * nc; - - /*lint -e{616} control flows into case/default */ - switch(cx->n_rnd) - { - case 14: - round(inv_rnd, b1, b0, kp + 4 * nc); - round(inv_rnd, b0, b1, kp + 3 * nc); - case 12: - round(inv_rnd, b1, b0, kp + 2 * nc); - round(inv_rnd, b0, b1, kp + nc ); - case 10: - round(inv_rnd, b1, b0, kp ); - round(inv_rnd, b0, b1, kp - nc); - round(inv_rnd, b1, b0, kp - 2 * nc); - round(inv_rnd, b0, b1, kp - 3 * nc); - round(inv_rnd, b1, b0, kp - 4 * nc); - round(inv_rnd, b0, b1, kp - 5 * nc); - round(inv_rnd, b1, b0, kp - 6 * nc); - round(inv_rnd, b0, b1, kp - 7 * nc); - round(inv_rnd, b1, b0, kp - 8 * nc); - round(inv_lrnd, b0, b1, kp - 9 * nc); - default: - ; - } -#else - -#if (DEC_UNROLL == PARTIAL) - { aes_32t rnd; - for(rnd = 0; rnd < (cx->n_rnd >> 1) - 1; ++rnd) - { - kp -= nc; - round(inv_rnd, b1, b0, kp); - kp -= nc; - round(inv_rnd, b0, b1, kp); - } - kp -= nc; - round(inv_rnd, b1, b0, kp); -#else - { aes_32t rnd, *p0 = b0, *p1 = b1, *pt; - for(rnd = 0; rnd < cx->n_rnd - 1; ++rnd) - { - kp -= nc; - round(inv_rnd, p1, p0, kp); - pt = p0, p0 = p1, p1 = pt; - } -#endif - kp -= nc; - round(inv_lrnd, b0, b1, kp); - } -#endif - - state_out(out_blk, b0); - return aes_good; -} - -#endif - -#if defined(__cplusplus) -} -#endif diff --git a/lib/gladman-fcrypt/aeskey.c b/lib/gladman-fcrypt/aeskey.c deleted file mode 100644 index e03b48f..0000000 --- a/lib/gladman-fcrypt/aeskey.c +++ /dev/null @@ -1,416 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the code for implementing the key schedule for AES and - Rijndael for block and key sizes of 16, 24, and 32 bytes. -*/ - -#include "aesopt.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#if defined(BLOCK_SIZE) && (BLOCK_SIZE & 7) -#error An illegal block size has been specified. -#endif - -/* Subroutine to set the block size (if variable). The value can be - in bytes, with legal values of 16, 24 and 32, or in bits, with - legal values of 128, 192 and 256. -*/ - -#if !defined(BLOCK_SIZE) - -INTERNAL aes_rval aes_set_block_size(unsigned int blen, aes_ctx cx[1]) -{ -#if !defined(FIXED_TABLES) -#ifdef GLOBALS - if(!t_use(in,it)) gen_tabs(); -#else - if(!cx->t_ptr || !t_use(in,it)) gen_tabs(cx); -#endif -#endif - if(((blen & 7) || blen < 16 || blen > 32) && ((blen & 63) || blen < 128 || blen > 256)) - { - cx->n_blk = 0; return aes_bad; - } - else - { - cx->n_blk = blen >> (blen < 128 ? 0 : 3); return aes_good; - } -} - -#endif - -/* Initialise the key schedule from the user supplied key. The key - length can be specified in bytes, with legal values of 16, 24 - and 32, or in bits, with legal values of 128, 192 and 256. These - values correspond with Nk values of 4, 6 and 8 respectively. - - The following macros implement a single cycle in the key - schedule generation process. The number of cycles needed - for each cx->n_col and nk value is: - - nk = 4 5 6 7 8 - ------------------------------ - cx->n_col = 4 10 9 8 7 7 - cx->n_col = 5 14 11 10 9 9 - cx->n_col = 6 19 15 12 11 11 - cx->n_col = 7 21 19 16 13 14 - cx->n_col = 8 29 23 19 17 14 -*/ - -#define ke4(k,i) \ -{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \ - k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \ -} -#define kel4(k,i) \ -{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \ - k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \ -} - -#define ke6(k,i) \ -{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \ - k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \ - k[6*(i)+10] = ss[4] ^= ss[3]; k[6*(i)+11] = ss[5] ^= ss[4]; \ -} -#define kel6(k,i) \ -{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \ - k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \ -} - -#define ke8(k,i) \ -{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \ - k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \ - k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); k[8*(i)+13] = ss[5] ^= ss[4]; \ - k[8*(i)+14] = ss[6] ^= ss[5]; k[8*(i)+15] = ss[7] ^= ss[6]; \ -} -#define kel8(k,i) \ -{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \ - k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \ -} - -#if defined(ENCRYPTION_KEY_SCHEDULE) - -INTERNAL aes_rval aes_set_encrypt_key(const unsigned char in_key[], unsigned int klen, aes_ctx cx[1]) -{ aes_32t ss[8]; - -#if !defined(FIXED_TABLES) -#ifdef GLOBALS - if(!t_use(in,it)) gen_tabs(); -#else - if(!cx->t_ptr || !t_use(in,it)) gen_tabs(cx); -#endif -#endif - -#if !defined(BLOCK_SIZE) - if(!cx->n_blk) cx->n_blk = 16; -#else - cx->n_blk = BLOCK_SIZE; -#endif - - if(((klen & 7) || klen < 16 || klen > 32) && ((klen & 63) || klen < 128 || klen > 256)) - { - cx->n_rnd = 0; return aes_bad; - } - - klen >>= (klen < 128 ? 2 : 5); - cx->n_blk = (cx->n_blk & ~3U) | 1; - - cx->k_sch[0] = ss[0] = word_in(in_key ); - cx->k_sch[1] = ss[1] = word_in(in_key + 4); - cx->k_sch[2] = ss[2] = word_in(in_key + 8); - cx->k_sch[3] = ss[3] = word_in(in_key + 12); - -#if (BLOCK_SIZE == 16) && (ENC_UNROLL != NONE) - - switch(klen) - { - case 4: - ke4(cx->k_sch, 0); ke4(cx->k_sch, 1); - ke4(cx->k_sch, 2); ke4(cx->k_sch, 3); - ke4(cx->k_sch, 4); ke4(cx->k_sch, 5); - ke4(cx->k_sch, 6); ke4(cx->k_sch, 7); - ke4(cx->k_sch, 8); kel4(cx->k_sch, 9); - cx->n_rnd = 10; break; - case 6: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - ke6(cx->k_sch, 0); ke6(cx->k_sch, 1); - ke6(cx->k_sch, 2); ke6(cx->k_sch, 3); - ke6(cx->k_sch, 4); ke6(cx->k_sch, 5); - ke6(cx->k_sch, 6); kel6(cx->k_sch, 7); - cx->n_rnd = 12; break; - case 8: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - cx->k_sch[6] = ss[6] = word_in(in_key + 24); - cx->k_sch[7] = ss[7] = word_in(in_key + 28); - ke8(cx->k_sch, 0); ke8(cx->k_sch, 1); - ke8(cx->k_sch, 2); ke8(cx->k_sch, 3); - ke8(cx->k_sch, 4); ke8(cx->k_sch, 5); - kel8(cx->k_sch, 6); - cx->n_rnd = 14; break; - default: - ; - } -#else - cx->n_rnd = (klen > nc ? klen : nc) + 6; - { aes_32t i, l; - l = (nc * cx->n_rnd + nc - 1) / klen; - - switch(klen) - { - case 4: - for(i = 0; i < l; ++i) - ke4(cx->k_sch, i); - break; - case 6: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - for(i = 0; i < l; ++i) - ke6(cx->k_sch, i); - break; - case 8: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - cx->k_sch[6] = ss[6] = word_in(in_key + 24); - cx->k_sch[7] = ss[7] = word_in(in_key + 28); - for(i = 0; i < l; ++i) - ke8(cx->k_sch, i); - break; - default: - ; - } - } -#endif - - return aes_good; -} - -#endif - -#if defined(DECRYPTION_KEY_SCHEDULE) - -#if (DEC_ROUND != NO_TABLES) -#define d_vars dec_imvars -#define ff(x) inv_mcol(x) -#else -#define ff(x) (x) -#define d_vars -#endif - -#if 1 -#define kdf4(k,i) \ -{ ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; ss[1] = ss[1] ^ ss[3]; ss[2] = ss[2] ^ ss[3]; ss[3] = ss[3]; \ - ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ - ss[4] ^= k[4*(i)]; k[4*(i)+4] = ff(ss[4]); ss[4] ^= k[4*(i)+1]; k[4*(i)+5] = ff(ss[4]); \ - ss[4] ^= k[4*(i)+2]; k[4*(i)+6] = ff(ss[4]); ss[4] ^= k[4*(i)+3]; k[4*(i)+7] = ff(ss[4]); \ -} -#define kd4(k,i) \ -{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \ - k[4*(i)+4] = ss[4] ^= k[4*(i)]; k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \ - k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \ -} -#define kdl4(k,i) \ -{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ - k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; k[4*(i)+5] = ss[1] ^ ss[3]; \ - k[4*(i)+6] = ss[0]; k[4*(i)+7] = ss[1]; \ -} -#else -#define kdf4(k,i) \ -{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ff(ss[0]); ss[1] ^= ss[0]; k[4*(i)+ 5] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[4*(i)+ 6] = ff(ss[2]); ss[3] ^= ss[2]; k[4*(i)+ 7] = ff(ss[3]); \ -} -#define kd4(k,i) \ -{ ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \ - ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[4*(i)+ 4] = ss[4] ^= k[4*(i)]; \ - ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[4] ^= k[4*(i)+ 1]; \ - ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[4] ^= k[4*(i)+ 2]; \ - ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[4] ^= k[4*(i)+ 3]; \ -} -#define kdl4(k,i) \ -{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ss[0]; ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[1]; \ - ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[2]; ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[3]; \ -} -#endif - -#define kdf6(k,i) \ -{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ff(ss[0]); ss[1] ^= ss[0]; k[6*(i)+ 7] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[6*(i)+ 8] = ff(ss[2]); ss[3] ^= ss[2]; k[6*(i)+ 9] = ff(ss[3]); \ - ss[4] ^= ss[3]; k[6*(i)+10] = ff(ss[4]); ss[5] ^= ss[4]; k[6*(i)+11] = ff(ss[5]); \ -} -#define kd6(k,i) \ -{ ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \ - ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \ - ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \ - ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \ - ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \ - ss[4] ^= ss[3]; k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \ - ss[5] ^= ss[4]; k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \ -} -#define kdl6(k,i) \ -{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ss[0]; ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[1]; \ - ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[2]; ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[3]; \ -} - -#define kdf8(k,i) \ -{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ff(ss[0]); ss[1] ^= ss[0]; k[8*(i)+ 9] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[8*(i)+10] = ff(ss[2]); ss[3] ^= ss[2]; k[8*(i)+11] = ff(ss[3]); \ - ss[4] ^= ls_box(ss[3],0); k[8*(i)+12] = ff(ss[4]); ss[5] ^= ss[4]; k[8*(i)+13] = ff(ss[5]); \ - ss[6] ^= ss[5]; k[8*(i)+14] = ff(ss[6]); ss[7] ^= ss[6]; k[8*(i)+15] = ff(ss[7]); \ -} -#define kd8(k,i) \ -{ aes_32t g = ls_box(ss[7],3) ^ t_use(r,c)[i]; \ - ss[0] ^= g; g = ff(g); k[8*(i)+ 8] = g ^= k[8*(i)]; \ - ss[1] ^= ss[0]; k[8*(i)+ 9] = g ^= k[8*(i)+ 1]; \ - ss[2] ^= ss[1]; k[8*(i)+10] = g ^= k[8*(i)+ 2]; \ - ss[3] ^= ss[2]; k[8*(i)+11] = g ^= k[8*(i)+ 3]; \ - g = ls_box(ss[3],0); \ - ss[4] ^= g; g = ff(g); k[8*(i)+12] = g ^= k[8*(i)+ 4]; \ - ss[5] ^= ss[4]; k[8*(i)+13] = g ^= k[8*(i)+ 5]; \ - ss[6] ^= ss[5]; k[8*(i)+14] = g ^= k[8*(i)+ 6]; \ - ss[7] ^= ss[6]; k[8*(i)+15] = g ^= k[8*(i)+ 7]; \ -} -#define kdl8(k,i) \ -{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ss[0]; ss[1] ^= ss[0]; k[8*(i)+ 9] = ss[1]; \ - ss[2] ^= ss[1]; k[8*(i)+10] = ss[2]; ss[3] ^= ss[2]; k[8*(i)+11] = ss[3]; \ -} - -INTERNAL aes_rval aes_set_decrypt_key(const unsigned char in_key[], unsigned int klen, aes_ctx cx[1]) -{ aes_32t ss[8]; - d_vars - -#if !defined(FIXED_TABLES) -#ifdef GLOBALS - if(!t_use(in,it)) gen_tabs(); -#else - if(!cx->t_ptr || !t_use(in,it)) gen_tabs(cx); -#endif -#endif - -#if !defined(BLOCK_SIZE) - if(!cx->n_blk) cx->n_blk = 16; -#else - cx->n_blk = BLOCK_SIZE; -#endif - - if(((klen & 7) || klen < 16 || klen > 32) && ((klen & 63) || klen < 128 || klen > 256)) - { - cx->n_rnd = 0; return aes_bad; - } - - klen >>= (klen < 128 ? 2 : 5); - cx->n_blk = (cx->n_blk & ~3) | 2; - - cx->k_sch[0] = ss[0] = word_in(in_key ); - cx->k_sch[1] = ss[1] = word_in(in_key + 4); - cx->k_sch[2] = ss[2] = word_in(in_key + 8); - cx->k_sch[3] = ss[3] = word_in(in_key + 12); - -#if (BLOCK_SIZE == 16) && (DEC_UNROLL != NONE) - - switch(klen) - { - case 4: - kdf4(cx->k_sch, 0); kd4(cx->k_sch, 1); - kd4(cx->k_sch, 2); kd4(cx->k_sch, 3); - kd4(cx->k_sch, 4); kd4(cx->k_sch, 5); - kd4(cx->k_sch, 6); kd4(cx->k_sch, 7); - kd4(cx->k_sch, 8); kdl4(cx->k_sch, 9); - cx->n_rnd = 10; break; - case 6: - cx->k_sch[4] = ff(ss[4] = word_in(in_key + 16)); - cx->k_sch[5] = ff(ss[5] = word_in(in_key + 20)); - kdf6(cx->k_sch, 0); kd6(cx->k_sch, 1); - kd6(cx->k_sch, 2); kd6(cx->k_sch, 3); - kd6(cx->k_sch, 4); kd6(cx->k_sch, 5); - kd6(cx->k_sch, 6); kdl6(cx->k_sch, 7); - cx->n_rnd = 12; break; - case 8: - cx->k_sch[4] = ff(ss[4] = word_in(in_key + 16)); - cx->k_sch[5] = ff(ss[5] = word_in(in_key + 20)); - cx->k_sch[6] = ff(ss[6] = word_in(in_key + 24)); - cx->k_sch[7] = ff(ss[7] = word_in(in_key + 28)); - kdf8(cx->k_sch, 0); kd8(cx->k_sch, 1); - kd8(cx->k_sch, 2); kd8(cx->k_sch, 3); - kd8(cx->k_sch, 4); kd8(cx->k_sch, 5); - kdl8(cx->k_sch, 6); - cx->n_rnd = 14; break; - default: - ; - } -#else - cx->n_rnd = (klen > nc ? klen : nc) + 6; - { aes_32t i, l; - l = (nc * cx->n_rnd + nc - 1) / klen; - - switch(klen) - { - case 4: - for(i = 0; i < l; ++i) - ke4(cx->k_sch, i); - break; - case 6: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - for(i = 0; i < l; ++i) - ke6(cx->k_sch, i); - break; - case 8: - cx->k_sch[4] = ss[4] = word_in(in_key + 16); - cx->k_sch[5] = ss[5] = word_in(in_key + 20); - cx->k_sch[6] = ss[6] = word_in(in_key + 24); - cx->k_sch[7] = ss[7] = word_in(in_key + 28); - for(i = 0; i < l; ++i) - ke8(cx->k_sch, i); - break; - default: - ; - } -#if (DEC_ROUND != NO_TABLES) - for(i = nc; i < nc * cx->n_rnd; ++i) - cx->k_sch[i] = inv_mcol(cx->k_sch[i]); -#endif - } -#endif - - return aes_good; -} - -#endif - -#if defined(__cplusplus) -} -#endif diff --git a/lib/gladman-fcrypt/aesopt.h b/lib/gladman-fcrypt/aesopt.h deleted file mode 100644 index 12db549..0000000 --- a/lib/gladman-fcrypt/aesopt.h +++ /dev/null @@ -1,900 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the compilation options for AES (Rijndael) and code - that is common across encryption, key scheduling and table generation. - - OPERATION - - These source code files implement the AES algorithm Rijndael designed by - Joan Daemen and Vincent Rijmen. The version in aes.c is designed for - block and key sizes of 128, 192 and 256 bits (16, 24 and 32 bytes) while - that in aespp.c provides for block and keys sizes of 128, 160, 192, 224 - and 256 bits (16, 20, 24, 28 and 32 bytes). This file is a common header - file for these two implementations and for aesref.c, which is a reference - implementation. - - This version is designed for flexibility and speed using operations on - 32-bit words rather than operations on bytes. It provides aes_both fixed - and dynamic block and key lengths and can also run with either big or - little endian internal byte order (see aes.h). It inputs block and key - lengths in bytes with the legal values being 16, 24 and 32 for aes.c and - 16, 20, 24, 28 and 32 for aespp.c - - THE CIPHER INTERFACE - - aes_08t (an unsigned 8-bit type) - aes_32t (an unsigned 32-bit type) - aes_fret (a signed 16 bit type for function return values) - aes_good (value != 0, a good return) - aes_bad (value == 0, an error return) - struct aes_ctx (structure for the cipher encryption context) - struct aes_ctx (structure for the cipher decryption context) - aes_rval the function return type (aes_fret if not DLL) - - C subroutine calls: - - aes_rval aes_set_block_size(unsigned int blen, aes_ctx cx[1]); - aes_rval aes_set_encrypt_key(const unsigned char in_key[], - unsigned int klen, aes_ctx cx[1]); - aes_rval aes_encrypt_block(const unsigned char in_blk[], - unsigned char out_blk[], const aes_ctx cx[1]); - - aes_rval aes_dec_len(unsigned int blen, aes_ctx cx[1]); - aes_rval aes_set_decrypt_key(const unsigned char in_key[], - unsigned int klen, aes_ctx cx[1]); - aes_rval aes_decrypt_block(const unsigned char in_blk[], - unsigned char out_blk[], const aes_ctx cx[1]); - - IMPORTANT NOTE: If you are using this C interface and your compiler does - not set the memory used for objects to zero before use, you will need to - ensure that cx.n_blk is set to zero before using these subroutine calls. - - C++ aes class subroutines: - - class AESclass for encryption - class AESclass for decryption - - aes_rval len(unsigned int blen = 16); - aes_rval key(const unsigned char in_key[], unsigned int klen); - aes_rval blk(const unsigned char in_blk[], unsigned char out_blk[]); - - aes_rval len(unsigned int blen = 16); - aes_rval key(const unsigned char in_key[], unsigned int klen); - aes_rval blk(const unsigned char in_blk[], unsigned char out_blk[]); - - The block length inputs to set_block and set_key are in numbers of - BYTES, not bits. The calls to subroutines must be made in the above - order but multiple calls can be made without repeating earlier calls - if their parameters have not changed. If the cipher block length is - variable but set_blk has not been called before cipher operations a - value of 16 is assumed (that is, the AES block size). In contrast to - earlier versions the block and key length parameters are now checked - for correctness and the encryption and decryption routines check to - ensure that an appropriate key has been set before they are called. - - COMPILATION - - The files used to provide AES (Rijndael) are - - a. aes.h for the definitions needed for use in C. - b. aescpp.h for the definitions needed for use in C++. - c. aesopt.h for setting compilation options (also includes common - code). - d. aescrypt.c for encryption and decrytpion, or - e. aescrypt.asm for encryption and decryption using assembler code. - f. aeskey.c for key scheduling. - g. aestab.c for table loading or generation. - - The assembler code uses the NASM assembler. The above files provice - block and key lengths of 16, 24 and 32 bytes (128, 192 and 256 bits). - If aescrypp.c and aeskeypp.c are used instead of aescrypt.c and - aeskey.c respectively, the block and key lengths can then be 16, 20, - 24, 28 or 32 bytes. However this code has not been optimised to the - same extent and is hence slower (esepcially for the AES block size - of 16 bytes). - - To compile AES (Rijndael) for use in C code use aes.h and exclude - the AES_DLL define in aes.h - - To compile AES (Rijndael) for use in in C++ code use aescpp.h and - exclude the AES_DLL define in aes.h - - To compile AES (Rijndael) in C as a Dynamic Link Library DLL) use - aes.h, include the AES_DLL define and compile the DLL. If using - the test files to test the DLL, exclude aes.c from the test build - project and compile it with the same defines as used for the DLL - (ensure that the DLL path is correct) - - CONFIGURATION OPTIONS (here and in aes.h) - - a. define BLOCK_SIZE in aes.h to set the cipher block size (16, 24 - or 32 for the standard code, or 16, 20, 24, 28 or 32 for the - extended code) or leave this undefined for dynamically variable - block size (this will result in much slower code). - b. set AES_DLL in aes.h if AES (Rijndael) is to be compiled as a DLL - c. You may need to set PLATFORM_BYTE_ORDER to define the byte order. - d. If you want the code to run in a specific internal byte order, then - INTERNAL_BYTE_ORDER must be set accordingly. - e. set other configuration options decribed below. -*/ - -#ifndef _AESOPT_H -#define _AESOPT_H - -#if defined(__cplusplus) -extern "C" -{ -#if 0 -} -#endif -#endif - -/* START OF CONFIGURATION OPTIONS - - USE OF DEFINES - - Later in this section there are a number of defines that control the - operation of the code. In each section, the purpose of each define is - explained so that the relevant form can be included or excluded by - setting either 1's or 0's respectively on the branches of the related - #if clauses. -*/ - -/* DO NOT CHANGE THE FOLLOWING EIGHT DEFINES */ - -#define NO_TABLES 0 -#define ONE_TABLE 1 -#define FOUR_TABLES 4 -#define NONE 0 -#define PARTIAL 1 -#define FULL 2 -#define AES_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ -#define AES_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ - -/* 1. PLATFORM SPECIFIC INCLUDES */ - -#include "aes.h" - -#include "config.h" - -#if defined(WORDS_BIGENDIAN) -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -#else -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -#endif - -/* 2. BYTE ORDER IN 32-BIT WORDS - - To obtain the highest speed on processors with 32-bit words, this code - needs to determine the order in which bytes are packed into such words. - The following block of code is an attempt to capture the most obvious - ways in which various environemnts define byte order. It may well fail, - in which case the definitions will need to be set by editing at the - points marked **** EDIT HERE IF NECESSARY **** below. -*/ -#if !defined(PLATFORM_BYTE_ORDER) -#if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN) -# if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# if defined(BYTE_ORDER) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -# elif (BYTE_ORDER == BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -# endif -# endif -# elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -# endif -#elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN) -# if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN) -# if defined(_BYTE_ORDER) -# if (_BYTE_ORDER == _LITTLE_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -# elif (_BYTE_ORDER == _BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -# endif -# endif -# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -# endif -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -#define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -#define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -#elif (('1234' >> 24) == '1') -# define PLATFORM_BYTE_ORDER AES_LITTLE_ENDIAN -#elif (('4321' >> 24) == '1') -# define PLATFORM_BYTE_ORDER AES_BIG_ENDIAN -#endif -#endif - -#if !defined(PLATFORM_BYTE_ORDER) -# error Please set undetermined byte order (lines 241 or 243 of aesopt.h). -#endif - -/* 3. FUNCTIONS REQUIRED - - This implementation provides five main subroutines which provide for - setting block length, setting encryption and decryption keys and for - encryption and decryption. When the assembler code is not being used - the following definition blocks allow the selection of the routines - that are to be included in the compilation. -*/ -#if 1 -#define ENCRYPTION -#define ENCRYPTION_KEY_SCHEDULE -#endif - -#if 0 -#define DECRYPTION -#define DECRYPTION_KEY_SCHEDULE -#endif - -/* 4. ASSEMBLER SUPPORT - - This define (which can be on the command line) enables the use of the - assembler code routines for encryption and decryption with the C code - only providing key scheduling -*/ -#if 0 -#define AES_ASM -#endif - -/* 5. BYTE ORDER WITHIN 32 BIT WORDS - - The fundamental data processing units in Rijndael are 8-bit bytes. The - input, output and key input are all enumerated arrays of bytes in which - bytes are numbered starting at zero and increasing to one less than the - number of bytes in the array in question. This enumeration is only used - for naming bytes and does not imply any adjacency or order relationship - from one byte to another. When these inputs and outputs are considered - as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to - byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte. - In this implementation bits are numbered from 0 to 7 starting at the - numerically least significant end of each byte (bit n represents 2^n). - - However, Rijndael can be implemented more efficiently using 32-bit - words by packing bytes into words so that bytes 4*n to 4*n+3 are placed - into word[n]. While in principle these bytes can be assembled into words - in any positions, this implementation only supports the two formats in - which bytes in adjacent positions within words also have adjacent byte - numbers. This order is called big-endian if the lowest numbered bytes - in words have the highest numeric significance and little-endian if the - opposite applies. - - This code can work in either order irrespective of the order used by the - machine on which it runs. Normally the internal byte order will be set - to the order of the processor on which the code is to be run but this - define can be used to reverse this in special situations -*/ -#if 1 -#define INTERNAL_BYTE_ORDER PLATFORM_BYTE_ORDER -#elif 0 -#define INTERNAL_BYTE_ORDER AES_LITTLE_ENDIAN -#elif 0 -#define INTERNAL_BYTE_ORDER AES_BIG_ENDIAN -#else -#error The internal byte order is not defined -#endif - -/* 6. FAST INPUT/OUTPUT OPERATIONS. - - On some machines it is possible to improve speed by transferring the - bytes in the input and output arrays to and from the internal 32-bit - variables by addressing these arrays as if they are arrays of 32-bit - words. On some machines this will always be possible but there may - be a large performance penalty if the byte arrays are not aligned on - the normal word boundaries. On other machines this technique will - lead to memory access errors when such 32-bit word accesses are not - properly aligned. The option SAFE_IO avoids such problems but will - often be slower on those machines that support misaligned access - (especially so if care is taken to align the input and output byte - arrays on 32-bit word boundaries). If SAFE_IO is not defined it is - assumed that access to byte arrays as if they are arrays of 32-bit - words will not cause problems when such accesses are misaligned. -*/ -#if 1 && !defined(_MSC_VER) -#define SAFE_IO -#endif - -/* 7. LOOP UNROLLING - - The code for encryption and decrytpion cycles through a number of rounds - that can be implemented either in a loop or by expanding the code into a - long sequence of instructions, the latter producing a larger program but - one that will often be much faster. The latter is called loop unrolling. - There are also potential speed advantages in expanding two iterations in - a loop with half the number of iterations, which is called partial loop - unrolling. The following options allow partial or full loop unrolling - to be set independently for encryption and decryption -*/ -#if 1 -#define ENC_UNROLL FULL -#elif 0 -#define ENC_UNROLL PARTIAL -#else -#define ENC_UNROLL NONE -#endif - -#if 1 -#define DEC_UNROLL FULL -#elif 0 -#define DEC_UNROLL PARTIAL -#else -#define DEC_UNROLL NONE -#endif - -/* 8. FAST FINITE FIELD OPERATIONS - - If this section is included, tables are used to provide faster finite - field arithmetic (this has no effect if FIXED_TABLES is defined). -*/ -#if 1 -#define FF_TABLES -#endif - -/* 9. INTERNAL STATE VARIABLE FORMAT - - The internal state of Rijndael is stored in a number of local 32-bit - word varaibles which can be defined either as an array or as individual - names variables. Include this section if you want to store these local - varaibles in arrays. Otherwise individual local variables will be used. -*/ -#if 1 -#define ARRAYS -#endif - -/* In this implementation the columns of the state array are each held in - 32-bit words. The state array can be held in various ways: in an array - of words, in a number of individual word variables or in a number of - processor registers. The following define maps a variable name x and - a column number c to the way the state array variable is to be held. - The first define below maps the state into an array x[c] whereas the - second form maps the state into a number of individual variables x0, - x1, etc. Another form could map individual state colums to machine - register names. -*/ - -#if defined(ARRAYS) -#define s(x,c) x[c] -#else -#define s(x,c) x##c -#endif - -/* 10. VARIABLE BLOCK SIZE SPEED - - This section is only relevant if you wish to use the variable block - length feature of the code. Include this section if you place more - emphasis on speed rather than code size. -*/ -#if 1 -#define FAST_VARIABLE -#endif - -/* 11. FIXED OR DYNAMIC TABLES - - When this section is included the tables used by the code are comipled - statically into the binary file. Otherwise they are computed once when - the code is first used. -*/ -#if 1 -#define FIXED_TABLES -#endif - -/* 12. GLOBAL VARIABLES - - In some circumstances global variables to hold the fixed tables are not - possible so the tables have to be placed in a structure that is passed - around at run time through the AES API calls. In this case FIXED_TABLES - cannot be used. GLOBALS must be defined in order to use the assembler - version of this code. If GLOBALS is not defined, then AES_TABLE_PTR must - be defined either on the command line or early in aes.h in order to - provide space for the table pointer in the AES context. -*/ -#if 1 || defined(AES_ASM) -#define GLOBALS -#elif !defined(AES_TABLE_PTR) -#error no context space for table pointer (compile with AES_TABLE_PTR defined) -#elif defined(FIXED_TABLES) -#undef FIXED_TABLES -#endif - -/* 13. INTERNAL TABLE CONFIGURATION - - This cipher proceeds by repeating in a number of cycles known as 'rounds' - which are implemented by a round function which can optionally be speeded - up using tables. The basic tables are each 256 32-bit words, with either - one or four tables being required for each round function depending on - how much speed is required. The encryption and decryption round functions - are different and the last encryption and decrytpion round functions are - different again making four different round functions in all. - - This means that: - 1. Normal encryption and decryption rounds can each use either 0, 1 - or 4 tables and table spaces of 0, 1024 or 4096 bytes each. - 2. The last encryption and decryption rounds can also use either 0, 1 - or 4 tables and table spaces of 0, 1024 or 4096 bytes each. - - Include or exclude the appropriate definitions below to set the number - of tables used by this implementation. -*/ - -#if 1 /* set tables for the normal encryption round */ -#define ENC_ROUND FOUR_TABLES -#elif 0 -#define ENC_ROUND ONE_TABLE -#else -#define ENC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the last encryption round */ -#define LAST_ENC_ROUND FOUR_TABLES -#elif 0 -#define LAST_ENC_ROUND ONE_TABLE -#else -#define LAST_ENC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the normal decryption round */ -#define DEC_ROUND FOUR_TABLES -#elif 0 -#define DEC_ROUND ONE_TABLE -#else -#define DEC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the last decryption round */ -#define LAST_DEC_ROUND FOUR_TABLES -#elif 0 -#define LAST_DEC_ROUND ONE_TABLE -#else -#define LAST_DEC_ROUND NO_TABLES -#endif - -/* The decryption key schedule can be speeded up with tables in the same - way that the round functions can. Include or exclude the following - defines to set this requirement. -*/ -#if 1 -#define KEY_SCHED FOUR_TABLES -#elif 0 -#define KEY_SCHED ONE_TABLE -#else -#define KEY_SCHED NO_TABLES -#endif - -/* END OF CONFIGURATION OPTIONS */ - -#if defined(BLOCK_SIZE) && ((BLOCK_SIZE & 3) || BLOCK_SIZE < 16 || BLOCK_SIZE > 32) -#error An illegal block size has been specified. -#endif - -#if !defined(BLOCK_SIZE) -#define RC_LENGTH 29 -#else -#define RC_LENGTH (5 * BLOCK_SIZE / 4 - (BLOCK_SIZE == 16 ? 10 : 11)) -#endif - -/* Disable at least some poor combinations of options */ - -#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES -#undef LAST_ENC_ROUND -#define LAST_ENC_ROUND NO_TABLES -#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES -#undef LAST_ENC_ROUND -#define LAST_ENC_ROUND ONE_TABLE -#endif - -#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE -#undef ENC_UNROLL -#define ENC_UNROLL NONE -#endif - -#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES -#undef LAST_DEC_ROUND -#define LAST_DEC_ROUND NO_TABLES -#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES -#undef LAST_DEC_ROUND -#define LAST_DEC_ROUND ONE_TABLE -#endif - -#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE -#undef DEC_UNROLL -#define DEC_UNROLL NONE -#endif - -/* upr(x,n): rotates bytes within words by n positions, moving bytes to - higher index positions with wrap around into low positions - ups(x,n): moves bytes by n positions to higher index positions in - words but without wrap around - bval(x,n): extracts a byte from a word - - NOTE: The definitions given here are intended only for use with - unsigned variables and with shift counts that are compile - time constants -*/ - -#if (INTERNAL_BYTE_ORDER == AES_LITTLE_ENDIAN) -#define upr(x,n) (((aes_32t)(x) << (8 * (n))) | ((aes_32t)(x) >> (32 - 8 * (n)))) -#define ups(x,n) ((aes_32t) (x) << (8 * (n))) -#define bval(x,n) ((aes_08t)((x) >> (8 * (n)))) -#define bytes2word(b0, b1, b2, b3) \ - (((aes_32t)(b3) << 24) | ((aes_32t)(b2) << 16) | ((aes_32t)(b1) << 8) | (b0)) -#endif - -#if (INTERNAL_BYTE_ORDER == AES_BIG_ENDIAN) -#define upr(x,n) (((aes_32t)(x) >> (8 * (n))) | ((aes_32t)(x) << (32 - 8 * (n)))) -#define ups(x,n) ((aes_32t) (x) >> (8 * (n)))) -#define bval(x,n) ((aes_08t)((x) >> (24 - 8 * (n)))) -#define bytes2word(b0, b1, b2, b3) \ - (((aes_32t)(b0) << 24) | ((aes_32t)(b1) << 16) | ((aes_32t)(b2) << 8) | (b3)) -#endif - -#if defined(SAFE_IO) - -#define word_in(x) bytes2word((x)[0], (x)[1], (x)[2], (x)[3]) -#define word_out(x,v) { (x)[0] = bval(v,0); (x)[1] = bval(v,1); \ - (x)[2] = bval(v,2); (x)[3] = bval(v,3); } - -#elif (INTERNAL_BYTE_ORDER == PLATFORM_BYTE_ORDER) - -#define word_in(x) (*(aes_32t*)(x)) -#define word_out(x,v) (*(aes_32t*)(x) = (v)) - -#else - -#if !defined(bswap_32) -#define brot(x,n) (((aes_32t)(x) << n) | ((aes_32t)(x) >> (32 - n))) -#define bswap_32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00)) -#endif - -#define word_in(x) bswap_32(*(aes_32t*)(x)) -#define word_out(x,v) (*(aes_32t*)(x) = bswap_32(v)) - -#endif - -/* the finite field modular polynomial and elements */ - -#define WPOLY 0x011b -#define BPOLY 0x1b - -/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */ - -#define m1 0x80808080 -#define m2 0x7f7f7f7f -#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY)) - -/* The following defines provide alternative definitions of FFmulX that might - give improved performance if a fast 32-bit multiply is not available. Note - that a temporary variable u needs to be defined where FFmulX is used. - -#define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6)) -#define m4 (0x01010101 * BPOLY) -#define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4) -*/ - -/* Work out which tables are needed for the different options */ - -#ifdef AES_ASM -#ifdef ENC_ROUND -#undef ENC_ROUND -#endif -#define ENC_ROUND FOUR_TABLES -#ifdef LAST_ENC_ROUND -#undef LAST_ENC_ROUND -#endif -#define LAST_ENC_ROUND FOUR_TABLES -#ifdef DEC_ROUND -#undef DEC_ROUND -#endif -#define DEC_ROUND FOUR_TABLES -#ifdef LAST_DEC_ROUND -#undef LAST_DEC_ROUND -#endif -#define LAST_DEC_ROUND FOUR_TABLES -#ifdef KEY_SCHED -#undef KEY_SCHED -#define KEY_SCHED FOUR_TABLES -#endif -#endif - -#if defined(ENCRYPTION) || defined(AES_ASM) -#if ENC_ROUND == ONE_TABLE -#define FT1_SET -#elif ENC_ROUND == FOUR_TABLES -#define FT4_SET -#else -#define SBX_SET -#endif -#if LAST_ENC_ROUND == ONE_TABLE -#define FL1_SET -#elif LAST_ENC_ROUND == FOUR_TABLES -#define FL4_SET -#elif !defined(SBX_SET) -#define SBX_SET -#endif -#endif - -#if defined(DECRYPTION) || defined(AES_ASM) -#if DEC_ROUND == ONE_TABLE -#define IT1_SET -#elif DEC_ROUND == FOUR_TABLES -#define IT4_SET -#else -#define ISB_SET -#endif -#if LAST_DEC_ROUND == ONE_TABLE -#define IL1_SET -#elif LAST_DEC_ROUND == FOUR_TABLES -#define IL4_SET -#elif !defined(ISB_SET) -#define ISB_SET -#endif -#endif - -#if defined(ENCRYPTION_KEY_SCHEDULE) || defined(DECRYPTION_KEY_SCHEDULE) -#if KEY_SCHED == ONE_TABLE -#define LS1_SET -#define IM1_SET -#elif KEY_SCHED == FOUR_TABLES -#define LS4_SET -#define IM4_SET -#elif !defined(SBX_SET) -#define SBX_SET -#endif -#endif - -/* If there are no global variables, the AES tables are placed in - a structure and a pointer is added to the AES context. If this - facility is used, the calling program has to ensure that this - pointer is managed appropriately. In particular, the value of - the t_dec(in,it) item in the table structure must be set to zero - in order to ensure that the tables are initialised. In practice - the three code sequences in aeskey.c that control the calls to - gen_tabs() and the gen_tabs() routine itself will require some - changes for a specific implementation. If global variables are - available it will generally be preferable to use them with the - precomputed FIXED_TABLES option that uses static global tables. - - The following defines can be used to control the way the tables - are defined, initialised and used in embedded environments that - require special features for these purposes - - the 't_dec' construction is used to declare fixed table arrays - the 't_set' construction is used to set fixed table values - the 't_use' construction is used to access fixed table values - - 256 byte tables: - - t_xxx(s,box) => forward S box - t_xxx(i,box) => inverse S box - - 256 32-bit word OR 4 x 256 32-bit word tables: - - t_xxx(f,n) => forward normal round - t_xxx(f,l) => forward last round - t_xxx(i,n) => inverse normal round - t_xxx(i,l) => inverse last round - t_xxx(l,s) => key schedule table - t_xxx(i,m) => key schedule table - - Other variables and tables: - - t_xxx(in,it) => the table initialsation flag - t_xxx(r,c) => the rcon table -*/ - -#define t_dec(m,n) t_##m##n -#define t_set(m,n) t_##m##n /* may be redefined once below */ -#define t_use(m,n) t_##m##n /* may be redefined once below */ - -#ifdef FIXED_TABLES - -#define prefx INTERNAL const -#elif defined(GLOBALS) -#define prefx extern -extern aes_08t t_dec(in,it); -void gen_tabs(void); -#else -#define prefx -void gen_tabs(aes_ctx cx[1]); - -typedef struct { -#endif - -prefx aes_32t t_dec(r,c)[RC_LENGTH]; - -#ifdef SBX_SET -prefx aes_08t t_dec(s,box)[256]; -#endif - -#ifdef ISB_SET -prefx aes_08t t_dec(i,box)[256]; -#endif - -#ifdef FT1_SET -prefx aes_32t t_dec(f,n)[256]; -#endif - -#ifdef FT4_SET -prefx aes_32t t_dec(f,n)[4][256]; -#endif - -#ifdef FL1_SET -prefx aes_32t t_dec(f,l)[256]; -#endif - -#ifdef FL4_SET -prefx aes_32t t_dec(f,l)[4][256]; -#endif - -#ifdef IT1_SET -prefx aes_32t t_dec(i,n)[256]; -#endif - -#ifdef IT4_SET -prefx aes_32t t_dec(i,n)[4][256]; -#endif - -#ifdef IL1_SET -prefx aes_32t t_dec(i,l)[256]; -#endif - -#ifdef IL4_SET -prefx aes_32t t_dec(i,l)[4][256]; -#endif - -#ifdef LS1_SET -#ifdef FL1_SET -#undef LS1_SET -#else -prefx aes_32t t_dec(l,s)[256]; -#endif -#endif - -#ifdef LS4_SET -#ifdef FL4_SET -#undef LS4_SET -#else -prefx aes_32t t_dec(l,s)[4][256]; -#endif -#endif - -#ifdef IM1_SET -prefx aes_32t t_dec(i,m)[256]; -#endif - -#ifdef IM4_SET -prefx aes_32t t_dec(i,m)[4][256]; -#endif - -prefx aes_08t t_dec(in,it); - -#ifndef GLOBALS -} s_ty; - -extern s_ty s_t; - -/* modify the table set macro to cope with a static structure */ -#undef t_set -#define t_set(m,n) (s_t.t_##m##n) - -/* modify the table use macro to cope with a context pointer */ -#undef t_use -#define t_use(m,n) (((s_ty*)cx->t_ptr)->t_##m##n) - -#endif - -/* Set the number of columns in nc. Note that it is important - that nc is a constant which is known at compile time if the - highest speed version of the code is needed. -*/ - -#if defined(BLOCK_SIZE) -#define nc (BLOCK_SIZE >> 2) -#else -#define nc (cx->n_blk >> 2) -#endif - -/* generic definitions of Rijndael macros that use tables */ - -#define no_table(x,box,vf,rf,c) bytes2word( \ - box[bval(vf(x,0,c),rf(0,c))], \ - box[bval(vf(x,1,c),rf(1,c))], \ - box[bval(vf(x,2,c),rf(2,c))], \ - box[bval(vf(x,3,c),rf(3,c))]) - -#define one_table(x,op,tab,vf,rf,c) \ - ( tab[bval(vf(x,0,c),rf(0,c))] \ - ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \ - ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \ - ^ op(tab[bval(vf(x,3,c),rf(3,c))],3)) - -#define four_tables(x,tab,vf,rf,c) \ - ( tab[0][bval(vf(x,0,c),rf(0,c))] \ - ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ - ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ - ^ tab[3][bval(vf(x,3,c),rf(3,c))]) - -#define vf1(x,r,c) (x) -#define rf1(r,c) (r) -#define rf2(r,c) ((8+r-c)&3) - -/* perform forward and inverse column mix operation on four bytes in long word x in */ -/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */ - -#define dec_fmvars -#if defined(FM4_SET) /* not currently used */ -#define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0) -#elif defined(FM1_SET) /* not currently used */ -#define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0) -#else -#undef dec_fmvars -#define dec_fmvars aes_32t f1, f2; -#define fwd_mcol(x) (f1 = (x), f2 = FFmulX(f1), f2 ^ upr(f1 ^ f2, 3) ^ upr(f1, 2) ^ upr(f1, 1)) -#endif - -#define dec_imvars -#if defined(IM4_SET) -#define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0) -#elif defined(IM1_SET) -#define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0) -#else -#undef dec_imvars -#define dec_imvars aes_32t f2, f4, f8, f9; -#define inv_mcol(x) \ - (f9 = (x), f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \ - f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1)) -#endif - -#if defined(FL4_SET) -#define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c) -#elif defined(LS4_SET) -#define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c) -#elif defined(FL1_SET) -#define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c) -#elif defined(LS1_SET) -#define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c) -#else -#define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c) -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/aestab.c b/lib/gladman-fcrypt/aestab.c deleted file mode 100644 index 7b202be..0000000 --- a/lib/gladman-fcrypt/aestab.c +++ /dev/null @@ -1,529 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the code for generating the fixed tables needed for AES -*/ - -#include "aesopt.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#if defined(FIXED_TABLES) || !defined(FF_TABLES) - -/* finite field arithmetic operations */ - -#define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY)) -#define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY)) -#define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \ - ^ (((x>>5) & 4) * WPOLY)) -#define f3(x) (f2(x) ^ x) -#define f9(x) (f8(x) ^ x) -#define fb(x) (f8(x) ^ f2(x) ^ x) -#define fd(x) (f8(x) ^ f4(x) ^ x) -#define fe(x) (f8(x) ^ f4(x) ^ f2(x)) - -#endif - -#if defined(FIXED_TABLES) - -#define sb_data(w) \ - w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ - w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ - w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ - w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ - w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ - w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ - w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ - w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ - w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ - w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ - w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ - w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ - w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ - w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ - w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ - w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ - w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ - w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ - w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ - w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ - w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ - w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ - w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ - w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ - w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ - w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ - w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ - w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ - w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ - w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ - w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ - w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) - -#define isb_data(w) \ - w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\ - w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\ - w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\ - w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\ - w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\ - w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\ - w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\ - w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\ - w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\ - w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\ - w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\ - w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\ - w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\ - w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\ - w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\ - w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\ - w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\ - w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\ - w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\ - w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\ - w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\ - w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\ - w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\ - w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\ - w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\ - w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\ - w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\ - w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\ - w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\ - w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\ - w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\ - w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d), - -#define mm_data(w) \ - w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\ - w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\ - w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\ - w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\ - w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\ - w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\ - w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\ - w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\ - w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\ - w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\ - w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\ - w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\ - w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\ - w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\ - w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\ - w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\ - w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\ - w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\ - w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\ - w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\ - w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\ - w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\ - w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\ - w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\ - w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\ - w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\ - w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\ - w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\ - w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\ - w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\ - w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\ - w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) - -#define h0(x) (x) - -/* These defines are used to ensure tables are generated in the - right format depending on the internal byte order required -*/ - -#define w0(p) bytes2word(p, 0, 0, 0) -#define w1(p) bytes2word(0, p, 0, 0) -#define w2(p) bytes2word(0, 0, p, 0) -#define w3(p) bytes2word(0, 0, 0, p) - -/* Number of elements required in this table for different - block and key lengths is: - - Rcon Table key length (bytes) - Length 16 20 24 28 32 - --------------------- - block 16 | 10 9 8 7 7 - length 20 | 14 11 10 9 9 - (bytes) 24 | 19 15 12 11 11 - 28 | 24 19 16 13 13 - 32 | 29 23 19 17 14 - - this table can be a table of bytes if the key schedule - code is adjusted accordingly -*/ - -#define u0(p) bytes2word(f2(p), p, p, f3(p)) -#define u1(p) bytes2word(f3(p), f2(p), p, p) -#define u2(p) bytes2word(p, f3(p), f2(p), p) -#define u3(p) bytes2word(p, p, f3(p), f2(p)) - -#define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p)) -#define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p)) -#define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p)) -#define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p)) - -INTERNAL const aes_32t t_dec(r,c)[RC_LENGTH] = -{ - w0(0x01), w0(0x02), w0(0x04), w0(0x08), w0(0x10), - w0(0x20), w0(0x40), w0(0x80), w0(0x1b), w0(0x36), -#if RC_LENGTH > 10 - w0(0x6c), w0(0xd8), w0(0xab), w0(0x4d), -#endif -#if RC_LENGTH > 14 - w0(0x9a), w0(0x2f), w0(0x5e), w0(0xbc), w0(0x63), -#endif -#if RC_LENGTH > 19 - w0(0xc6), w0(0x97), w0(0x35), w0(0x6a), w0(0xd4), -#endif -#if RC_LENGTH > 24 - w0(0xb3), w0(0x7d), w0(0xfa), w0(0xef), w0(0xc5) -#endif -}; - -#ifdef SBX_SET -INTERNAL const aes_08t t_dec(s,box)[256] = { sb_data(h0) }; -#endif -#ifdef ISB_SET -INTERNAL const aes_08t t_dec(i,box)[256] = { isb_data(h0) }; -#endif - -#ifdef FT1_SET -INTERNAL const aes_32t t_dec(f,n)[256] = { sb_data(u0) }; -#endif -#ifdef FT4_SET -INTERNAL const aes_32t t_dec(f,n)[4][256] = - { { sb_data(u0) }, { sb_data(u1) }, { sb_data(u2) }, { sb_data(u3) } }; -#endif - -#ifdef FL1_SET -INTERNAL const aes_32t t_dec(f,l)[256] = { sb_data(w0) }; -#endif -#ifdef FL4_SET -INTERNAL const aes_32t t_dec(f,l)[4][256] = - { { sb_data(w0) }, { sb_data(w1) }, { sb_data(w2) }, { sb_data(w3) } }; -#endif - -#ifdef IT1_SET -INTERNAL const aes_32t t_dec(i,n)[256] = { isb_data(v0) }; -#endif -#ifdef IT4_SET -INTERNAL const aes_32t t_dec(i,n)[4][256] = - { { isb_data(v0) }, { isb_data(v1) }, { isb_data(v2) }, { isb_data(v3) } }; -#endif - -#ifdef IL1_SET -INTERNAL const aes_32t t_dec(i,l)[256] = { isb_data(w0) }; -#endif -#ifdef IL4_SET -INTERNAL const aes_32t t_dec(i,l)[4][256] = - { { isb_data(w0) }, { isb_data(w1) }, { isb_data(w2) }, { isb_data(w3) } }; -#endif - -#ifdef LS1_SET -INTERNAL const aes_32t t_dec(l,s)[256] = { sb_data(w0) }; -#endif -#ifdef LS4_SET -INTERNAL const aes_32t t_dec(l,s)[4][256] = - { { sb_data(w0) }, { sb_data(w1) }, { sb_data(w2) }, { sb_data(w3) } }; -#endif - -#ifdef IM1_SET -INTERNAL const aes_32t t_dec(i,m)[256] = { mm_data(v0) }; -#endif -#ifdef IM4_SET -INTERNAL const aes_32t t_dec(i,m)[4][256] = - { { mm_data(v0) }, { mm_data(v1) }, { mm_data(v2) }, { mm_data(v3) } }; -#endif - -#else /* dynamic table generation */ - -#ifdef GLOBALS - -aes_08t t_dec(in,it) = 0; - -aes_32t t_dec(r,c)[RC_LENGTH]; - -#ifdef SBX_SET -aes_08t t_dec(s,box)[256]; -#endif -#ifdef ISB_SET -aes_08t t_dec(i,box)[256]; -#endif - -#ifdef FT1_SET -aes_32t t_dec(f,n)[256]; -#endif -#ifdef FT4_SET -aes_32t t_dec(f,n)[4][256]; -#endif - -#ifdef FL1_SET -aes_32t t_dec(f,l)[256]; -#endif -#ifdef FL4_SET -aes_32t t_dec(f,l)[4][256]; -#endif - -#ifdef IT1_SET -aes_32t t_dec(i,n)[256]; -#endif -#ifdef IT4_SET -aes_32t t_dec(i,n)[4][256]; -#endif - -#ifdef IL1_SET -aes_32t t_dec(i,l)[256]; -#endif -#ifdef IL4_SET -aes_32t t_dec(i,l)[4][256]; -#endif - -#ifdef LS1_SET -aes_32t t_dec(l,s)[256]; -#endif -#ifdef LS4_SET -aes_32t t_dec(l,s)[4][256]; -#endif - -#ifdef IM1_SET -aes_32t t_dec(i,m)[256]; -#endif -#ifdef IM4_SET -aes_32t t_dec(i,m)[4][256]; -#endif - -#else -s_ty s_t; -#endif - -#if !defined(FF_TABLES) - -/* Generate the tables for the dynamic table option - - It will generally be sensible to use tables to compute finite - field multiplies and inverses but where memory is scarse this - code might sometimes be better. But it only has effect during - initialisation so its pretty unimportant in overall terms. -*/ - -/* return 2 ^ (n - 1) where n is the bit number of the highest bit - set in x with x in the range 1 < x < 0x00000200. This form is - used so that locals within fi can be bytes rather than words -*/ - -static aes_08t hibit(const aes_32t x) -{ aes_08t r = (aes_08t)((x >> 1) | (x >> 2)); - - r |= (r >> 2); - r |= (r >> 4); - return (r + 1) >> 1; -} - -/* return the inverse of the finite field element x */ - -static aes_08t fi(const aes_08t x) -{ aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0; - - if(x < 2) return x; - - for(;;) - { - if(!n1) return v1; - - while(n2 >= n1) - { - n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2); - } - - if(!n2) return v2; - - while(n1 >= n2) - { - n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1); - } - } -} - -#else - -/* define the finite field multiplies required for Rijndael */ - -#define f2(x) ((x) ? pow[log[x] + 0x19] : 0) -#define f3(x) ((x) ? pow[log[x] + 0x01] : 0) -#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0) -#define fb(x) ((x) ? pow[log[x] + 0x68] : 0) -#define fd(x) ((x) ? pow[log[x] + 0xee] : 0) -#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0) -#define fi(x) ((x) ? pow[255 - log[x]]: 0) - -#endif - -/* The forward and inverse affine transformations used in the S-box */ - -#define fwd_affine(x) \ - (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8))) - -#define inv_affine(x) \ - (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8))) - -#ifdef GLOBALS -INTERNAL void gen_tabs(void) -#else -INTERNAL void gen_tabs(aes_ctx cx[1]) -#endif -{ aes_32t i, w; - -#if defined(FF_TABLES) - - aes_08t pow[512], log[256]; - - /* log and power tables for GF(2^8) finite field with - WPOLY as modular polynomial - the simplest primitive - root is 0x03, used here to generate the tables - */ - - i = 0; w = 1; - do - { - pow[i] = (aes_08t)w; - pow[i + 255] = (aes_08t)w; - log[w] = (aes_08t)i++; - w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0); - } - while (w != 1); - -#endif - -#ifndef GLOBALS - if(!cx->t_ptr) - cx->t_ptr = &s_t; -#endif - - for(i = 0, w = 1; i < RC_LENGTH; ++i) - { - t_set(r,c)[i] = bytes2word(w, 0, 0, 0); - w = f2(w); - } - - for(i = 0; i < 256; ++i) - { aes_08t b; - - b = fwd_affine(fi((aes_08t)i)); - w = bytes2word(f2(b), b, b, f3(b)); - -#ifdef SBX_SET - t_set(s,box)[i] = b; -#endif - -#ifdef FT1_SET /* tables for a normal encryption round */ - t_set(f,n)[i] = w; -#endif -#ifdef FT4_SET - t_set(f,n)[0][i] = w; - t_set(f,n)[1][i] = upr(w,1); - t_set(f,n)[2][i] = upr(w,2); - t_set(f,n)[3][i] = upr(w,3); -#endif - w = bytes2word(b, 0, 0, 0); - -#ifdef FL1_SET /* tables for last encryption round (may also */ - t_set(f,l)[i] = w; /* be used in the key schedule) */ -#endif -#ifdef FL4_SET - t_set(f,l)[0][i] = w; - t_set(f,l)[1][i] = upr(w,1); - t_set(f,l)[2][i] = upr(w,2); - t_set(f,l)[3][i] = upr(w,3); -#endif - -#ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */ - t_set(l,s)[i] = w; /* not of the required form */ -#endif -#ifdef LS4_SET - t_set(l,s)[0][i] = w; - t_set(l,s)[1][i] = upr(w,1); - t_set(l,s)[2][i] = upr(w,2); - t_set(l,s)[3][i] = upr(w,3); -#endif - - b = fi(inv_affine((aes_08t)i)); - w = bytes2word(fe(b), f9(b), fd(b), fb(b)); - -#ifdef IM1_SET /* tables for the inverse mix column operation */ - t_set(i,m)[b] = w; -#endif -#ifdef IM4_SET - t_set(i,m)[0][b] = w; - t_set(i,m)[1][b] = upr(w,1); - t_set(i,m)[2][b] = upr(w,2); - t_set(i,m)[3][b] = upr(w,3); -#endif - -#ifdef ISB_SET - t_set(i,box)[i] = b; -#endif -#ifdef IT1_SET /* tables for a normal decryption round */ - t_set(i,n)[i] = w; -#endif -#ifdef IT4_SET - t_set(i,n)[0][i] = w; - t_set(i,n)[1][i] = upr(w,1); - t_set(i,n)[2][i] = upr(w,2); - t_set(i,n)[3][i] = upr(w,3); -#endif - w = bytes2word(b, 0, 0, 0); -#ifdef IL1_SET /* tables for last decryption round */ - t_set(i,l)[i] = w; -#endif -#ifdef IL4_SET - t_set(i,l)[0][i] = w; - t_set(i,l)[1][i] = upr(w,1); - t_set(i,l)[2][i] = upr(w,2); - t_set(i,l)[3][i] = upr(w,3); -#endif - } - - t_set(in,it) = 1; -} - -#endif - -#if defined(__cplusplus) -} -#endif - diff --git a/lib/gladman-fcrypt/fileenc.c b/lib/gladman-fcrypt/fileenc.c deleted file mode 100644 index a96083b..0000000 --- a/lib/gladman-fcrypt/fileenc.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - ------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file implements password based file encryption and authentication - using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password - based key derivation. - -*/ - -#include - -#include "fileenc.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* subroutine for data encryption/decryption */ -/* this could be speeded up a lot by aligning */ -/* buffers and using 32 bit operations */ - -static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1]) - { unsigned long i = 0; - unsigned int pos = cx->encr_pos; - - while(i < d_len) - { - if(pos == BLOCK_SIZE) - { unsigned int j = 0; - /* increment encryption nonce */ - while(j < 8 && !++cx->nonce[j]) - ++j; - /* encrypt the nonce to form next xor buffer */ - aes_encrypt_block(cx->nonce, cx->encr_bfr, cx->encr_ctx); - pos = 0; - } - - data[i++] ^= cx->encr_bfr[pos++]; - } - - cx->encr_pos = pos; -} - -int _zip_fcrypt_init( - unsigned int mode, /* the mode to be used (input) */ - const unsigned char pwd[], /* the user specified password (input) */ - unsigned int pwd_len, /* the length of the password (input) */ - const unsigned char salt[], /* the salt (input) */ -#ifdef PASSWORD_VERIFIER - unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ -#endif - fcrypt_ctx cx[1]) /* the file encryption context (output) */ -{ unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH]; - - if(pwd_len > MAX_PWD_LENGTH) - return PASSWORD_TOO_LONG; - - if(mode < 1 || mode > 3) - return BAD_MODE; - - cx->mode = mode; - cx->pwd_len = pwd_len; - - /* derive the encryption and authetication keys and the password verifier */ - derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS, - kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH); - - /* initialise the encryption nonce and buffer pos */ - cx->encr_pos = BLOCK_SIZE; - /* if we need a random component in the encryption */ - /* nonce, this is where it would have to be set */ - memset(cx->nonce, 0, BLOCK_SIZE * sizeof(unsigned char)); - - /* initialise for encryption using key 1 */ - aes_set_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx); - - /* initialise for authentication using key 2 */ - hmac_sha1_begin(cx->auth_ctx); - hmac_sha1_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx); - -#ifdef PASSWORD_VERIFIER - memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH); -#endif - - return GOOD_RETURN; -} - -/* perform 'in place' encryption and authentication */ - -void _zip_fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) -{ - encr_data(data, data_len, cx); - hmac_sha1_data(data, data_len, cx->auth_ctx); -} - -/* perform 'in place' authentication and decryption */ - -void _zip_fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) -{ - hmac_sha1_data(data, data_len, cx->auth_ctx); - encr_data(data, data_len, cx); -} - -/* close encryption/decryption and return the MAC value */ - -int _zip_fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1]) -{ - hmac_sha1_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx); - return MAC_LENGTH(cx->mode); /* return MAC length in bytes */ -} - -#if defined(__cplusplus) -} -#endif diff --git a/lib/gladman-fcrypt/fileenc.h b/lib/gladman-fcrypt/fileenc.h deleted file mode 100644 index d7b5234..0000000 --- a/lib/gladman-fcrypt/fileenc.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the header file for fileenc.c, which implements password - based file encryption and authentication using AES in CTR mode, HMAC-SHA1 - authentication and RFC2898 password based key derivation. -*/ - -#ifndef _FENC_H -#define _FENC_H - -#include "aes.h" -#include "hmac.h" -#include "pwd2key.h" - -#define PASSWORD_VERIFIER - -#define MAX_KEY_LENGTH 32 -#define MAX_PWD_LENGTH 128 -#define MAX_SALT_LENGTH 16 -#define KEYING_ITERATIONS 1000 - -#ifdef PASSWORD_VERIFIER -#define PWD_VER_LENGTH 2 -#else -#define PWD_VER_LENGTH 0 -#endif - -#define GOOD_RETURN 0 -#define PASSWORD_TOO_LONG -100 -#define BAD_MODE -101 - -/* - Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4) - - Mode Key Salt MAC Overhead - 1 16 8 10 18 - 2 24 12 10 22 - 3 32 16 10 26 - - The following macros assume that the mode value is correct. -*/ - -#define KEY_LENGTH(mode) (8 * (mode & 3) + 8) -#define SALT_LENGTH(mode) (4 * (mode & 3) + 4) -#define MAC_LENGTH(mode) (10) - -/* the context for file encryption */ - -#if defined(__cplusplus) -extern "C" -{ -#endif - -typedef struct -{ unsigned char nonce[BLOCK_SIZE]; /* the CTR nonce */ - unsigned char encr_bfr[BLOCK_SIZE]; /* encrypt buffer */ - aes_ctx encr_ctx[1]; /* encryption context */ - hmac_ctx auth_ctx[1]; /* authentication context */ - unsigned int encr_pos; /* block position (enc) */ - unsigned int pwd_len; /* password length */ - unsigned int mode; /* File encryption mode */ -} fcrypt_ctx; - -/* initialise file encryption or decryption */ - -int _zip_fcrypt_init( - unsigned int mode, /* the mode to be used (input) */ - const unsigned char pwd[], /* the user specified password (input) */ - unsigned int pwd_len, /* the length of the password (input) */ - const unsigned char salt[], /* the salt (input) */ -#ifdef PASSWORD_VERIFIER - unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ -#endif - fcrypt_ctx cx[1]); /* the file encryption context (output) */ - -/* perform 'in place' encryption or decryption and authentication */ - -void _zip_fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); -void _zip_fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); - -/* close encryption/decryption and return the MAC value */ -/* the return value is the length of the MAC */ - -int _zip_fcrypt_end(unsigned char mac[], /* the MAC value (output) */ - fcrypt_ctx cx[1]); /* the context (input) */ - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/hmac.c b/lib/gladman-fcrypt/hmac.c deleted file mode 100644 index d031a9d..0000000 --- a/lib/gladman-fcrypt/hmac.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is an implementation of HMAC, the FIPS standard keyed hash function -*/ - -#include "hmac.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* initialise the HMAC context to zero */ -INTERNAL void hmac_sha1_begin(hmac_ctx cx[1]) -{ - memset(cx, 0, sizeof(hmac_ctx)); -} - -/* input the HMAC key (can be called multiple times) */ -INTERNAL int hmac_sha1_key(const unsigned char key[], unsigned int key_len, hmac_ctx cx[1]) -{ - if(cx->klen == HMAC_IN_DATA) /* error if further key input */ - return HMAC_BAD_MODE; /* is attempted in data mode */ - - if(cx->klen + key_len > IN_BLOCK_LENGTH) /* if the key has to be hashed */ - { - if(cx->klen <= IN_BLOCK_LENGTH) /* if the hash has not yet been */ - { /* started, initialise it and */ - sha1_begin(cx->ctx); /* hash stored key characters */ - sha1_hash(cx->key, cx->klen, cx->ctx); - } - - sha1_hash(key, key_len, cx->ctx); /* hash long key data into hash */ - } - else /* otherwise store key data */ - memcpy(cx->key + cx->klen, key, key_len); - - cx->klen += key_len; /* update the key length count */ - return HMAC_OK; -} - -/* input the HMAC data (can be called multiple times) - */ -/* note that this call terminates the key input phase */ -INTERNAL void hmac_sha1_data(const unsigned char data[], unsigned int data_len, hmac_ctx cx[1]) -{ unsigned int i; - - if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ - { - if(cx->klen > IN_BLOCK_LENGTH) /* if key is being hashed */ - { /* complete the hash and */ - sha1_end(cx->key, cx->ctx); /* store the result as the */ - cx->klen = OUT_BLOCK_LENGTH; /* key and set new length */ - } - - /* pad the key if necessary */ - if (cx->klen < IN_BLOCK_LENGTH) - { - memset(cx->key + cx->klen, 0, IN_BLOCK_LENGTH - cx->klen); - } - - /* xor ipad into key value */ - for(i = 0; i < (IN_BLOCK_LENGTH >> 2); ++i) - ((sha1_32t*)cx->key)[i] ^= 0x36363636; - - /* and start hash operation */ - sha1_begin(cx->ctx); - sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx); - - /* mark as now in data mode */ - cx->klen = HMAC_IN_DATA; - } - - /* hash the data (if any) */ - if(data_len) - sha1_hash(data, data_len, cx->ctx); -} - -/* compute and output the MAC value */ -INTERNAL void hmac_sha1_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) -{ unsigned char dig[OUT_BLOCK_LENGTH]; - unsigned int i; - - /* if no data has been entered perform a null data phase */ - if(cx->klen != HMAC_IN_DATA) - hmac_sha1_data((const unsigned char*)0, 0, cx); - - sha1_end(dig, cx->ctx); /* complete the inner hash */ - - /* set outer key value using opad and removing ipad */ - for(i = 0; i < (IN_BLOCK_LENGTH >> 2); ++i) - ((sha1_32t*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c; - - /* perform the outer hash operation */ - sha1_begin(cx->ctx); - sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx); - sha1_hash(dig, OUT_BLOCK_LENGTH, cx->ctx); - sha1_end(dig, cx->ctx); - - /* output the hash value */ - for(i = 0; i < mac_len; ++i) - mac[i] = dig[i]; -} - -#if 0 /* not used */ -/* 'do it all in one go' subroutine */ -INTERNAL void hmac_sha1(const unsigned char key[], unsigned int key_len, - const unsigned char data[], unsigned int data_len, - unsigned char mac[], unsigned int mac_len) -{ hmac_ctx cx[1]; - - hmac_sha1_begin(cx); - hmac_sha1_key(key, key_len, cx); - hmac_sha1_data(data, data_len, cx); - hmac_sha1_end(mac, mac_len, cx); -} -#endif - -#if defined(__cplusplus) -} -#endif diff --git a/lib/gladman-fcrypt/hmac.h b/lib/gladman-fcrypt/hmac.h deleted file mode 100644 index f84bdcf..0000000 --- a/lib/gladman-fcrypt/hmac.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is an implementation of HMAC, the FIPS standard keyed hash function -*/ - -#ifndef _HMAC_H -#define _HMAC_H - -#include - -#include "sha1.h" - -#define IN_BLOCK_LENGTH SHA1_BLOCK_SIZE -#define OUT_BLOCK_LENGTH SHA1_DIGEST_SIZE -#define HMAC_IN_DATA 0xffffffff - -#define HMAC_OK 0 -#define HMAC_BAD_MODE -1 - -#if defined(__cplusplus) -extern "C" -{ -#endif - -typedef struct -{ unsigned char key[IN_BLOCK_LENGTH]; - sha1_ctx ctx[1]; - unsigned int klen; -} hmac_ctx; - -#ifdef BUILDING_FCRYPT -INTERNAL void hmac_sha1_begin(hmac_ctx cx[1]); - -INTERNAL int hmac_sha1_key(const unsigned char key[], unsigned int key_len, hmac_ctx cx[1]); - -INTERNAL void hmac_sha1_data(const unsigned char data[], unsigned int data_len, hmac_ctx cx[1]); - -INTERNAL void hmac_sha1_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]); -#endif - -#if 0 /* not used */ -INTERNAL void hmac_sha1(const unsigned char key[], unsigned int key_len, - const unsigned char data[], unsigned int data_len, - unsigned char mac[], unsigned int mac_len); -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/pwd2key.c b/lib/gladman-fcrypt/pwd2key.c deleted file mode 100644 index 3bda272..0000000 --- a/lib/gladman-fcrypt/pwd2key.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is an implementation of RFC2898, which specifies key derivation from - a password and a salt value. -*/ - -#include -#include "hmac.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -INTERNAL void derive_key(const unsigned char pwd[], /* the PASSWORD */ - unsigned int pwd_len, /* and its length */ - const unsigned char salt[], /* the SALT and its */ - unsigned int salt_len, /* length */ - unsigned int iter, /* the number of iterations */ - unsigned char key[], /* space for the output key */ - unsigned int key_len)/* and its required length */ -{ - unsigned int i, j, k, n_blk; - unsigned char uu[OUT_BLOCK_LENGTH], ux[OUT_BLOCK_LENGTH]; - hmac_ctx c1[1], c2[1], c3[1]; - - /* set HMAC context (c1) for password */ - hmac_sha1_begin(c1); - hmac_sha1_key(pwd, pwd_len, c1); - - /* set HMAC context (c2) for password and salt */ - memcpy(c2, c1, sizeof(hmac_ctx)); - hmac_sha1_data(salt, salt_len, c2); - - /* find the number of SHA blocks in the key */ - n_blk = 1 + (key_len - 1) / OUT_BLOCK_LENGTH; - - for(i = 0; i < n_blk; ++i) /* for each block in key */ - { - /* ux[] holds the running xor value */ - memset(ux, 0, OUT_BLOCK_LENGTH); - - /* set HMAC context (c3) for password and salt */ - memcpy(c3, c2, sizeof(hmac_ctx)); - - /* enter additional data for 1st block into uu */ - uu[0] = (unsigned char)((i + 1) >> 24); - uu[1] = (unsigned char)((i + 1) >> 16); - uu[2] = (unsigned char)((i + 1) >> 8); - uu[3] = (unsigned char)(i + 1); - - /* this is the key mixing iteration */ - for(j = 0, k = 4; j < iter; ++j) - { - /* add previous round data to HMAC */ - hmac_sha1_data(uu, k, c3); - - /* obtain HMAC for uu[] */ - hmac_sha1_end(uu, OUT_BLOCK_LENGTH, c3); - - /* xor into the running xor block */ - for(k = 0; k < OUT_BLOCK_LENGTH; ++k) - ux[k] ^= uu[k]; - - /* set HMAC context (c3) for password */ - memcpy(c3, c1, sizeof(hmac_ctx)); - } - - /* compile key blocks into the key output */ - j = 0; k = i * OUT_BLOCK_LENGTH; - while(j < OUT_BLOCK_LENGTH && k < key_len) - key[k++] = ux[j++]; - } -} - -#ifdef TEST - -#include - -struct -{ unsigned int pwd_len; - unsigned int salt_len; - unsigned int it_count; - unsigned char *pwd; - unsigned char salt[32]; - unsigned char key[32]; -} tests[] = -{ - { 8, 4, 5, (unsigned char*)"password", - { 0x12, 0x34, 0x56, 0x78 }, - { 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7, - 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5 } /* ... */ - }, - { 8, 8, 5, (unsigned char*)"password", - { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 }, - { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6, - 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49 } /* ... */ - } -}; - -int main() -{ unsigned int i, j, key_len = 256; - unsigned char key[256]; - - printf("\nTest of RFC2898 Password Based Key Derivation"); - for(i = 0; i < 2; ++i) - { - derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt, - tests[i].salt_len, tests[i].it_count, key, key_len); - - printf("\ntest %i: ", i + 1); - printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good"); - for(j = 0; j < key_len && j < 64; j += 4) - { - if(j % 16 == 0) - printf("\n"); - printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]); - } - printf(j < key_len ? " ... \n" : "\n"); - } - printf("\n"); - return 0; -} - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/pwd2key.h b/lib/gladman-fcrypt/pwd2key.h deleted file mode 100644 index b00e288..0000000 --- a/lib/gladman-fcrypt/pwd2key.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is an implementation of RFC2898, which specifies key derivation from - a password and a salt value. -*/ - -#ifndef PWD2KEY_H -#define PWD2KEY_H - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#ifdef BUILDING_FCRYPT -INTERNAL void derive_key( - const unsigned char pwd[], /* the PASSWORD, and */ - unsigned int pwd_len, /* its length */ - const unsigned char salt[], /* the SALT and its */ - unsigned int salt_len, /* length */ - unsigned int iter, /* the number of iterations */ - unsigned char key[], /* space for the output key */ - unsigned int key_len); /* and its required length */ -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/gladman-fcrypt/sha1.c b/lib/gladman-fcrypt/sha1.c deleted file mode 100644 index 4e2c196..0000000 --- a/lib/gladman-fcrypt/sha1.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is a byte oriented version of SHA1 that operates on arrays of bytes - stored in memory. -*/ - -#include /* for memcpy() etc. */ -#include /* for _lrotl with VC++ */ - -#include "sha1.h" - -#if defined(__cplusplus) -extern "C" -{ -#if 0 -} -#endif -#endif - -/* - To obtain the highest speed on processors with 32-bit words, this code - needs to determine the order in which bytes are packed into such words. - The following block of code is an attempt to capture the most obvious - ways in which various environemnts specify their endian definitions. - It may well fail, in which case the definitions will need to be set by - editing at the points marked **** EDIT HERE IF NECESSARY **** below. -*/ -#define SHA_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ -#define SHA_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ - -#include "config.h" - -#if !defined(PLATFORM_BYTE_ORDER) -# if defined(WORDS_BIGENDIAN) -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -# else -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -# endif -#endif - -#if !defined(PLATFORM_BYTE_ORDER) -#if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN) -# if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# if defined(BYTE_ORDER) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -# elif (BYTE_ORDER == BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -# endif -# endif -# elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -# endif -#elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN) -# if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN) -# if defined(_BYTE_ORDER) -# if (_BYTE_ORDER == _LITTLE_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -# elif (_BYTE_ORDER == _BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -# endif -# endif -# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN) -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -# endif -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -#define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -#define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -#elif (('1234' >> 24) == '1') -# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN -#elif (('4321' >> 24) == '1') -# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN -#endif -#endif - -#if !defined(PLATFORM_BYTE_ORDER) -# error Please set undetermined byte order (lines 87 or 89 of sha1.c). -#endif - -#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) - -#if (PLATFORM_BYTE_ORDER == SHA_BIG_ENDIAN) -#define swap_b32(x) (x) -#elif defined(bswap_32) -#define swap_b32(x) bswap_32(x) -#else -#define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00)) -#endif - -#define SHA1_MASK (SHA1_BLOCK_SIZE - 1) - -/* reverse byte order in 32-bit words */ - -#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) -#define parity(x,y,z) ((x) ^ (y) ^ (z)) -#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) - -/* A normal version as set out in the FIPS */ - -#define rnd(f,k) \ - t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \ - e = d; d = c; c = rotl32(b, 30); b = t - -INTERNAL void sha1_compile(sha1_ctx ctx[1]) -{ sha1_32t w[80], i, a, b, c, d, e, t; - - /* note that words are compiled from the buffer into 32-bit */ - /* words in big-endian order so an order reversal is needed */ - /* here on little endian machines */ - for(i = 0; i < SHA1_BLOCK_SIZE / 4; ++i) - w[i] = swap_b32(ctx->wbuf[i]); - - for(i = SHA1_BLOCK_SIZE / 4; i < 80; ++i) - w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); - - a = ctx->hash[0]; - b = ctx->hash[1]; - c = ctx->hash[2]; - d = ctx->hash[3]; - e = ctx->hash[4]; - - for(i = 0; i < 20; ++i) - { - rnd(ch, 0x5a827999); - } - - for(i = 20; i < 40; ++i) - { - rnd(parity, 0x6ed9eba1); - } - - for(i = 40; i < 60; ++i) - { - rnd(maj, 0x8f1bbcdc); - } - - for(i = 60; i < 80; ++i) - { - rnd(parity, 0xca62c1d6); - } - - ctx->hash[0] += a; - ctx->hash[1] += b; - ctx->hash[2] += c; - ctx->hash[3] += d; - ctx->hash[4] += e; -} - -INTERNAL void sha1_begin(sha1_ctx ctx[1]) -{ - ctx->count[0] = ctx->count[1] = 0; - ctx->hash[0] = 0x67452301; - ctx->hash[1] = 0xefcdab89; - ctx->hash[2] = 0x98badcfe; - ctx->hash[3] = 0x10325476; - ctx->hash[4] = 0xc3d2e1f0; -} - -/* SHA1 hash data in an array of bytes into hash buffer and */ -/* call the hash_compile function as required. */ - -INTERNAL void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx ctx[1]) -{ sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK), - space = SHA1_BLOCK_SIZE - pos; - const unsigned char *sp = data; - - if((ctx->count[0] += len) < len) - ++(ctx->count[1]); - - while(len >= space) /* tranfer whole blocks if possible */ - { - memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); - sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; - sha1_compile(ctx); - } - - /*lint -e{803} conceivable data overrun */ - /* there are two cases: the above while loop entered or not */ - /* entered. If not entered, 'space = SHA1_BLOCK_SIZE - pos' */ - /* and 'len < space' so that 'len + pos < SHA1_BLOCK_SIZE'. */ - /* If entered, 'pos = 0', 'space = SHA1_BLOCK_SIZE' and */ - /* 'len < space' so that 'pos + len < SHA1_BLOCK_SIZE'. In */ - /* both cases, therefore, the memory copy is in the buffer */ - - memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); -} - -/* SHA1 final padding and digest calculation */ - -#if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN) -static sha1_32t mask[4] = - { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff }; -static sha1_32t bits[4] = - { 0x00000080, 0x00008000, 0x00800000, 0x80000000 }; -#else -static sha1_32t mask[4] = - { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 }; -static sha1_32t bits[4] = - { 0x80000000, 0x00800000, 0x00008000, 0x00000080 }; -#endif - -INTERNAL void sha1_end(unsigned char hval[], sha1_ctx ctx[1]) -{ sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK); - - /* mask out the rest of any partial 32-bit word and then set */ - /* the next byte to 0x80. On big-endian machines any bytes in */ - /* the buffer will be at the top end of 32 bit words, on little */ - /* endian machines they will be at the bottom. Hence the AND */ - /* and OR masks above are reversed for little endian systems */ - /* Note that we can always add the first padding byte at this */ - /* point because the buffer always has at least one empty slot */ - ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & mask[i & 3]) | bits[i & 3]; - - /* we need 9 or more empty positions, one for the padding byte */ - /* (above) and eight for the length count. If there is not */ - /* enough space pad and empty the buffer */ - if(i > SHA1_BLOCK_SIZE - 9) - { - if(i < 60) ctx->wbuf[15] = 0; - sha1_compile(ctx); - i = 0; - } - else /* compute a word index for the empty buffer positions */ - i = (i >> 2) + 1; - - while(i < 14) /* and zero pad all but last two positions */ - ctx->wbuf[i++] = 0; - - /* assemble the eight byte counter in in big-endian format */ - ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29)); - ctx->wbuf[15] = swap_b32(ctx->count[0] << 3); - - sha1_compile(ctx); - - /* extract the hash value as bytes in case the hash buffer is */ - /* misaligned for 32-bit words */ - /*lint -e{504} unusual shift operation (unusually formed right argument) */ - for(i = 0; i < SHA1_DIGEST_SIZE; ++i) - hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); -} - -#if 0 /* not used */ -INTERNAL void sha1(unsigned char hval[], const unsigned char data[], unsigned int len) -{ sha1_ctx cx[1]; - - sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx); -} -#endif - -#if defined(__cplusplus) -} -#endif diff --git a/lib/gladman-fcrypt/sha1.h b/lib/gladman-fcrypt/sha1.h deleted file mode 100644 index 58529d4..0000000 --- a/lib/gladman-fcrypt/sha1.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the definitions needed for SHA1 -*/ - -#ifndef _SHA1_H -#define _SHA1_H - -#include - -#define SHA1_BLOCK_SIZE 64 -#define SHA1_DIGEST_SIZE 20 - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* define an unsigned 32-bit type */ - -#include "config.h" - -typedef zip_uint32_t sha1_32t; - -/* type to hold the SHA256 context */ - -typedef struct -{ sha1_32t count[2]; - sha1_32t hash[5]; - sha1_32t wbuf[16]; -} sha1_ctx; - -#ifdef BUILDING_FCRYPT -INTERNAL void sha1_compile(sha1_ctx ctx[1]); - -INTERNAL void sha1_begin(sha1_ctx ctx[1]); -INTERNAL void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx ctx[1]); -INTERNAL void sha1_end(unsigned char hval[], sha1_ctx ctx[1]); -#endif - -#if 0 /* not used */ -INTERNAL void sha1(unsigned char hval[], const unsigned char data[], unsigned int len); -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/lib/mkstemp.c b/lib/mkstemp.c index cdfc0e2..01a531f 100644 --- a/lib/mkstemp.c +++ b/lib/mkstemp.c @@ -31,16 +31,16 @@ * SUCH DAMAGE. */ -#include #include +#include #include #include #include #include #ifdef _WIN32 -#include #include +#include #else #include #endif @@ -53,101 +53,101 @@ int -_zip_mkstemp(char *path) -{ +_zip_mkstemp(char *path) { #ifdef _WIN32 - int ret; - ret = _creat(_mktemp(path), _S_IREAD|_S_IWRITE); - if (ret == -1) { - return 0; - } else { - return ret; - } + int ret; + ret = _creat(_mktemp(path), _S_IREAD | _S_IWRITE); + if (ret == -1) { + return 0; + } + else { + return ret; + } #else - int fd; - char *start, *trv; - struct stat sbuf; - pid_t pid; - - /* To guarantee multiple calls generate unique names even if - the file is not created. 676 different possibilities with 7 - or more X's, 26 with 6 or less. */ - static char xtra[2] = "aa"; - int xcnt = 0; - - pid = getpid(); - - /* Move to end of path and count trailing X's. */ - for (trv = path; *trv; ++trv) - if (*trv == 'X') - xcnt++; - else - xcnt = 0; - - /* Use at least one from xtra. Use 2 if more than 6 X's. */ - if (*(trv - 1) == 'X') - *--trv = xtra[0]; - if (xcnt > 6 && *(trv - 1) == 'X') - *--trv = xtra[1]; - - /* Set remaining X's to pid digits with 0's to the left. */ - while (*--trv == 'X') { - *trv = (pid % 10) + '0'; - pid /= 10; + int fd; + char *start, *trv; + struct stat sbuf; + pid_t pid; + + /* To guarantee multiple calls generate unique names even if + the file is not created. 676 different possibilities with 7 + or more X's, 26 with 6 or less. */ + static char xtra[2] = "aa"; + int xcnt = 0; + + pid = getpid(); + + /* Move to end of path and count trailing X's. */ + for (trv = path; *trv; ++trv) + if (*trv == 'X') + xcnt++; + else + xcnt = 0; + + /* Use at least one from xtra. Use 2 if more than 6 X's. */ + if (*(trv - 1) == 'X') + *--trv = xtra[0]; + if (xcnt > 6 && *(trv - 1) == 'X') + *--trv = xtra[1]; + + /* Set remaining X's to pid digits with 0's to the left. */ + while (*--trv == 'X') { + *trv = (pid % 10) + '0'; + pid /= 10; + } + + /* update xtra for next call. */ + if (xtra[0] != 'z') + xtra[0]++; + else { + xtra[0] = 'a'; + if (xtra[1] != 'z') + xtra[1]++; + else + xtra[1] = 'a'; + } + + /* + * check the target directory; if you have six X's and it + * doesn't exist this runs for a *very* long time. + */ + for (start = trv + 1;; --trv) { + if (trv <= path) + break; + if (*trv == '/') { + *trv = '\0'; + if (stat(path, &sbuf)) + return (0); + if (!S_ISDIR(sbuf.st_mode)) { + errno = ENOTDIR; + return (0); + } + *trv = '/'; + break; } - - /* update xtra for next call. */ - if (xtra[0] != 'z') - xtra[0]++; - else { - xtra[0] = 'a'; - if (xtra[1] != 'z') - xtra[1]++; + } + + for (;;) { + if ((fd = open(path, O_CREAT | O_EXCL | O_RDWR | O_BINARY, 0600)) >= 0) + return (fd); + if (errno != EEXIST) + return (0); + + /* tricky little algorithm for backward compatibility */ + for (trv = start;;) { + if (!*trv) + return (0); + if (*trv == 'z') + *trv++ = 'a'; + else { + if (isdigit((unsigned char)*trv)) + *trv = 'a'; else - xtra[1] = 'a'; - } - - /* - * check the target directory; if you have six X's and it - * doesn't exist this runs for a *very* long time. - */ - for (start = trv + 1;; --trv) { - if (trv <= path) - break; - if (*trv == '/') { - *trv = '\0'; - if (stat(path, &sbuf)) - return (0); - if (!S_ISDIR(sbuf.st_mode)) { - errno = ENOTDIR; - return (0); - } - *trv = '/'; - break; - } - } - - for (;;) { - if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0) - return (fd); - if (errno != EEXIST) - return (0); - - /* tricky little algorithm for backward compatibility */ - for (trv = start;;) { - if (!*trv) - return (0); - if (*trv == 'z') - *trv++ = 'a'; - else { - if (isdigit((unsigned char)*trv)) - *trv = 'a'; - else - ++*trv; - break; - } - } + ++*trv; + break; + } } + } /*NOTREACHED*/ #endif } diff --git a/lib/zip.h b/lib/zip.h index 3dc0695..2d83a99 100644 --- a/lib/zip.h +++ b/lib/zip.h @@ -45,195 +45,197 @@ extern "C" { #include #ifndef ZIP_EXTERN -# ifndef ZIP_STATIC -# ifdef _WIN32 -# define ZIP_EXTERN __declspec(dllimport) -# elif defined(__GNUC__) && __GNUC__ >= 4 -# define ZIP_EXTERN __attribute__ ((visibility ("default"))) -# else -# define ZIP_EXTERN -# endif -# else -# define ZIP_EXTERN -# endif +#ifndef ZIP_STATIC +#ifdef _WIN32 +#define ZIP_EXTERN __declspec(dllimport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +#define ZIP_EXTERN __attribute__((visibility("default"))) +#else +#define ZIP_EXTERN +#endif +#else +#define ZIP_EXTERN +#endif #endif -#include #include +#include #include /* flags for zip_open */ -#define ZIP_CREATE 1 -#define ZIP_EXCL 2 -#define ZIP_CHECKCONS 4 -#define ZIP_TRUNCATE 8 -#define ZIP_RDONLY 16 +#define ZIP_CREATE 1 +#define ZIP_EXCL 2 +#define ZIP_CHECKCONS 4 +#define ZIP_TRUNCATE 8 +#define ZIP_RDONLY 16 /* flags for zip_name_locate, zip_fopen, zip_stat, ... */ -#define ZIP_FL_NOCASE 1u /* ignore case on name lookup */ -#define ZIP_FL_NODIR 2u /* ignore directory component */ -#define ZIP_FL_COMPRESSED 4u /* read compressed data */ -#define ZIP_FL_UNCHANGED 8u /* use original data, ignoring changes */ -#define ZIP_FL_RECOMPRESS 16u /* force recompression of data */ -#define ZIP_FL_ENCRYPTED 32u /* read encrypted data (implies ZIP_FL_COMPRESSED) */ -#define ZIP_FL_ENC_GUESS 0u /* guess string encoding (is default) */ -#define ZIP_FL_ENC_RAW 64u /* get unmodified string */ -#define ZIP_FL_ENC_STRICT 128u /* follow specification strictly */ -#define ZIP_FL_LOCAL 256u /* in local header */ -#define ZIP_FL_CENTRAL 512u /* in central directory */ +#define ZIP_FL_NOCASE 1u /* ignore case on name lookup */ +#define ZIP_FL_NODIR 2u /* ignore directory component */ +#define ZIP_FL_COMPRESSED 4u /* read compressed data */ +#define ZIP_FL_UNCHANGED 8u /* use original data, ignoring changes */ +#define ZIP_FL_RECOMPRESS 16u /* force recompression of data */ +#define ZIP_FL_ENCRYPTED 32u /* read encrypted data (implies ZIP_FL_COMPRESSED) */ +#define ZIP_FL_ENC_GUESS 0u /* guess string encoding (is default) */ +#define ZIP_FL_ENC_RAW 64u /* get unmodified string */ +#define ZIP_FL_ENC_STRICT 128u /* follow specification strictly */ +#define ZIP_FL_LOCAL 256u /* in local header */ +#define ZIP_FL_CENTRAL 512u /* in central directory */ /* 1024u reserved for internal use */ -#define ZIP_FL_ENC_UTF_8 2048u /* string is UTF-8 encoded */ -#define ZIP_FL_ENC_CP437 4096u /* string is CP437 encoded */ -#define ZIP_FL_OVERWRITE 8192u /* zip_file_add: if file with name exists, overwrite (replace) it */ +#define ZIP_FL_ENC_UTF_8 2048u /* string is UTF-8 encoded */ +#define ZIP_FL_ENC_CP437 4096u /* string is CP437 encoded */ +#define ZIP_FL_OVERWRITE 8192u /* zip_file_add: if file with name exists, overwrite (replace) it */ /* archive global flags flags */ -#define ZIP_AFL_RDONLY 2u /* read only -- cannot be cleared */ +#define ZIP_AFL_RDONLY 2u /* read only -- cannot be cleared */ /* create a new extra field */ -#define ZIP_EXTRA_FIELD_ALL ZIP_UINT16_MAX -#define ZIP_EXTRA_FIELD_NEW ZIP_UINT16_MAX +#define ZIP_EXTRA_FIELD_ALL ZIP_UINT16_MAX +#define ZIP_EXTRA_FIELD_NEW ZIP_UINT16_MAX /* libzip error codes */ -#define ZIP_ER_OK 0 /* N No error */ -#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */ -#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */ -#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */ -#define ZIP_ER_SEEK 4 /* S Seek error */ -#define ZIP_ER_READ 5 /* S Read error */ -#define ZIP_ER_WRITE 6 /* S Write error */ -#define ZIP_ER_CRC 7 /* N CRC error */ -#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */ -#define ZIP_ER_NOENT 9 /* N No such file */ -#define ZIP_ER_EXISTS 10 /* N File already exists */ -#define ZIP_ER_OPEN 11 /* S Can't open file */ -#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */ -#define ZIP_ER_ZLIB 13 /* Z Zlib error */ -#define ZIP_ER_MEMORY 14 /* N Malloc failure */ -#define ZIP_ER_CHANGED 15 /* N Entry has been changed */ -#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */ -#define ZIP_ER_EOF 17 /* N Premature end of file */ -#define ZIP_ER_INVAL 18 /* N Invalid argument */ -#define ZIP_ER_NOZIP 19 /* N Not a zip archive */ -#define ZIP_ER_INTERNAL 20 /* N Internal error */ -#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */ -#define ZIP_ER_REMOVE 22 /* S Can't remove file */ -#define ZIP_ER_DELETED 23 /* N Entry has been deleted */ -#define ZIP_ER_ENCRNOTSUPP 24 /* N Encryption method not supported */ -#define ZIP_ER_RDONLY 25 /* N Read-only archive */ -#define ZIP_ER_NOPASSWD 26 /* N No password provided */ -#define ZIP_ER_WRONGPASSWD 27 /* N Wrong password provided */ -#define ZIP_ER_OPNOTSUPP 28 /* N Operation not supported */ -#define ZIP_ER_INUSE 29 /* N Resource still in use */ -#define ZIP_ER_TELL 30 /* S Tell error */ -#define ZIP_ER_COMPRESSED_DATA 31 /* N Compressed data invalid */ +#define ZIP_ER_OK 0 /* N No error */ +#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */ +#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */ +#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */ +#define ZIP_ER_SEEK 4 /* S Seek error */ +#define ZIP_ER_READ 5 /* S Read error */ +#define ZIP_ER_WRITE 6 /* S Write error */ +#define ZIP_ER_CRC 7 /* N CRC error */ +#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */ +#define ZIP_ER_NOENT 9 /* N No such file */ +#define ZIP_ER_EXISTS 10 /* N File already exists */ +#define ZIP_ER_OPEN 11 /* S Can't open file */ +#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */ +#define ZIP_ER_ZLIB 13 /* Z Zlib error */ +#define ZIP_ER_MEMORY 14 /* N Malloc failure */ +#define ZIP_ER_CHANGED 15 /* N Entry has been changed */ +#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */ +#define ZIP_ER_EOF 17 /* N Premature end of file */ +#define ZIP_ER_INVAL 18 /* N Invalid argument */ +#define ZIP_ER_NOZIP 19 /* N Not a zip archive */ +#define ZIP_ER_INTERNAL 20 /* N Internal error */ +#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */ +#define ZIP_ER_REMOVE 22 /* S Can't remove file */ +#define ZIP_ER_DELETED 23 /* N Entry has been deleted */ +#define ZIP_ER_ENCRNOTSUPP 24 /* N Encryption method not supported */ +#define ZIP_ER_RDONLY 25 /* N Read-only archive */ +#define ZIP_ER_NOPASSWD 26 /* N No password provided */ +#define ZIP_ER_WRONGPASSWD 27 /* N Wrong password provided */ +#define ZIP_ER_OPNOTSUPP 28 /* N Operation not supported */ +#define ZIP_ER_INUSE 29 /* N Resource still in use */ +#define ZIP_ER_TELL 30 /* S Tell error */ +#define ZIP_ER_COMPRESSED_DATA 31 /* N Compressed data invalid */ /* type of system error value */ -#define ZIP_ET_NONE 0 /* sys_err unused */ -#define ZIP_ET_SYS 1 /* sys_err is errno */ -#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */ +#define ZIP_ET_NONE 0 /* sys_err unused */ +#define ZIP_ET_SYS 1 /* sys_err is errno */ +#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */ /* compression methods */ -#define ZIP_CM_DEFAULT -1 /* better of deflate or store */ -#define ZIP_CM_STORE 0 /* stored (uncompressed) */ -#define ZIP_CM_SHRINK 1 /* shrunk */ -#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */ -#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */ -#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */ -#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */ -#define ZIP_CM_IMPLODE 6 /* imploded */ +#define ZIP_CM_DEFAULT -1 /* better of deflate or store */ +#define ZIP_CM_STORE 0 /* stored (uncompressed) */ +#define ZIP_CM_SHRINK 1 /* shrunk */ +#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */ +#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */ +#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */ +#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */ +#define ZIP_CM_IMPLODE 6 /* imploded */ /* 7 - Reserved for Tokenizing compression algorithm */ -#define ZIP_CM_DEFLATE 8 /* deflated */ -#define ZIP_CM_DEFLATE64 9 /* deflate64 */ -#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */ +#define ZIP_CM_DEFLATE 8 /* deflated */ +#define ZIP_CM_DEFLATE64 9 /* deflate64 */ +#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */ /* 11 - Reserved by PKWARE */ -#define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */ +#define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */ /* 13 - Reserved by PKWARE */ -#define ZIP_CM_LZMA 14 /* LZMA (EFS) */ +#define ZIP_CM_LZMA 14 /* LZMA (EFS) */ /* 15-17 - Reserved by PKWARE */ -#define ZIP_CM_TERSE 18 /* compressed using IBM TERSE (new) */ -#define ZIP_CM_LZ77 19 /* IBM LZ77 z Architecture (PFS) */ -#define ZIP_CM_XZ 95 /* XZ compressed data */ -#define ZIP_CM_JPEG 96 /* Compressed Jpeg data */ -#define ZIP_CM_WAVPACK 97 /* WavPack compressed data */ -#define ZIP_CM_PPMD 98 /* PPMd version I, Rev 1 */ +#define ZIP_CM_TERSE 18 /* compressed using IBM TERSE (new) */ +#define ZIP_CM_LZ77 19 /* IBM LZ77 z Architecture (PFS) */ +#define ZIP_CM_XZ 95 /* XZ compressed data */ +#define ZIP_CM_JPEG 96 /* Compressed Jpeg data */ +#define ZIP_CM_WAVPACK 97 /* WavPack compressed data */ +#define ZIP_CM_PPMD 98 /* PPMd version I, Rev 1 */ /* encryption methods */ -#define ZIP_EM_NONE 0 /* not encrypted */ -#define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */ -#if 0 /* Strong Encryption Header not parsed yet */ -#define ZIP_EM_DES 0x6601 /* strong encryption: DES */ -#define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */ -#define ZIP_EM_3DES_168 0x6603 -#define ZIP_EM_3DES_112 0x6609 -#define ZIP_EM_PKZIP_AES_128 0x660e -#define ZIP_EM_PKZIP_AES_192 0x660f -#define ZIP_EM_PKZIP_AES_256 0x6610 -#define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */ -#define ZIP_EM_RC4 0x6801 +#define ZIP_EM_NONE 0 /* not encrypted */ +#define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */ +#if 0 /* Strong Encryption Header not parsed yet */ +#define ZIP_EM_DES 0x6601 /* strong encryption: DES */ +#define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */ +#define ZIP_EM_3DES_168 0x6603 +#define ZIP_EM_3DES_112 0x6609 +#define ZIP_EM_PKZIP_AES_128 0x660e +#define ZIP_EM_PKZIP_AES_192 0x660f +#define ZIP_EM_PKZIP_AES_256 0x6610 +#define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */ +#define ZIP_EM_RC4 0x6801 #endif -#define ZIP_EM_AES_128 0x0101 /* Winzip AES encryption */ -#define ZIP_EM_AES_192 0x0102 -#define ZIP_EM_AES_256 0x0103 -#define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */ - -#define ZIP_OPSYS_DOS 0x00u -#define ZIP_OPSYS_AMIGA 0x01u -#define ZIP_OPSYS_OPENVMS 0x02u -#define ZIP_OPSYS_UNIX 0x03u -#define ZIP_OPSYS_VM_CMS 0x04u -#define ZIP_OPSYS_ATARI_ST 0x05u -#define ZIP_OPSYS_OS_2 0x06u -#define ZIP_OPSYS_MACINTOSH 0x07u -#define ZIP_OPSYS_Z_SYSTEM 0x08u -#define ZIP_OPSYS_CPM 0x09u -#define ZIP_OPSYS_WINDOWS_NTFS 0x0au -#define ZIP_OPSYS_MVS 0x0bu -#define ZIP_OPSYS_VSE 0x0cu -#define ZIP_OPSYS_ACORN_RISC 0x0du -#define ZIP_OPSYS_VFAT 0x0eu -#define ZIP_OPSYS_ALTERNATE_MVS 0x0fu -#define ZIP_OPSYS_BEOS 0x10u -#define ZIP_OPSYS_TANDEM 0x11u -#define ZIP_OPSYS_OS_400 0x12u -#define ZIP_OPSYS_OS_X 0x13u - -#define ZIP_OPSYS_DEFAULT ZIP_OPSYS_UNIX +#define ZIP_EM_AES_128 0x0101 /* Winzip AES encryption */ +#define ZIP_EM_AES_192 0x0102 +#define ZIP_EM_AES_256 0x0103 +#define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */ + +#define ZIP_OPSYS_DOS 0x00u +#define ZIP_OPSYS_AMIGA 0x01u +#define ZIP_OPSYS_OPENVMS 0x02u +#define ZIP_OPSYS_UNIX 0x03u +#define ZIP_OPSYS_VM_CMS 0x04u +#define ZIP_OPSYS_ATARI_ST 0x05u +#define ZIP_OPSYS_OS_2 0x06u +#define ZIP_OPSYS_MACINTOSH 0x07u +#define ZIP_OPSYS_Z_SYSTEM 0x08u +#define ZIP_OPSYS_CPM 0x09u +#define ZIP_OPSYS_WINDOWS_NTFS 0x0au +#define ZIP_OPSYS_MVS 0x0bu +#define ZIP_OPSYS_VSE 0x0cu +#define ZIP_OPSYS_ACORN_RISC 0x0du +#define ZIP_OPSYS_VFAT 0x0eu +#define ZIP_OPSYS_ALTERNATE_MVS 0x0fu +#define ZIP_OPSYS_BEOS 0x10u +#define ZIP_OPSYS_TANDEM 0x11u +#define ZIP_OPSYS_OS_400 0x12u +#define ZIP_OPSYS_OS_X 0x13u + +#define ZIP_OPSYS_DEFAULT ZIP_OPSYS_UNIX enum zip_source_cmd { - ZIP_SOURCE_OPEN, /* prepare for reading */ - ZIP_SOURCE_READ, /* read data */ - ZIP_SOURCE_CLOSE, /* reading is done */ - ZIP_SOURCE_STAT, /* get meta information */ - ZIP_SOURCE_ERROR, /* get error information */ - ZIP_SOURCE_FREE, /* cleanup and free resources */ - ZIP_SOURCE_SEEK, /* set position for reading */ - ZIP_SOURCE_TELL, /* get read position */ - ZIP_SOURCE_BEGIN_WRITE, /* prepare for writing */ - ZIP_SOURCE_COMMIT_WRITE, /* writing is done */ - ZIP_SOURCE_ROLLBACK_WRITE, /* discard written changes */ - ZIP_SOURCE_WRITE, /* write data */ - ZIP_SOURCE_SEEK_WRITE, /* set position for writing */ - ZIP_SOURCE_TELL_WRITE, /* get write position */ - ZIP_SOURCE_SUPPORTS, /* check whether source supports command */ - ZIP_SOURCE_REMOVE, /* remove file */ - ZIP_SOURCE_GET_COMPRESSION_FLAGS, /* get compression flags, internal only */ - ZIP_SOURCE_BEGIN_WRITE_CLONING /* like ZIP_SOURCE_BEGIN_WRITE, but keep part of original file */ + ZIP_SOURCE_OPEN, /* prepare for reading */ + ZIP_SOURCE_READ, /* read data */ + ZIP_SOURCE_CLOSE, /* reading is done */ + ZIP_SOURCE_STAT, /* get meta information */ + ZIP_SOURCE_ERROR, /* get error information */ + ZIP_SOURCE_FREE, /* cleanup and free resources */ + ZIP_SOURCE_SEEK, /* set position for reading */ + ZIP_SOURCE_TELL, /* get read position */ + ZIP_SOURCE_BEGIN_WRITE, /* prepare for writing */ + ZIP_SOURCE_COMMIT_WRITE, /* writing is done */ + ZIP_SOURCE_ROLLBACK_WRITE, /* discard written changes */ + ZIP_SOURCE_WRITE, /* write data */ + ZIP_SOURCE_SEEK_WRITE, /* set position for writing */ + ZIP_SOURCE_TELL_WRITE, /* get write position */ + ZIP_SOURCE_SUPPORTS, /* check whether source supports command */ + ZIP_SOURCE_REMOVE, /* remove file */ + ZIP_SOURCE_GET_COMPRESSION_FLAGS, /* get compression flags, internal only */ + ZIP_SOURCE_BEGIN_WRITE_CLONING /* like ZIP_SOURCE_BEGIN_WRITE, but keep part of original file */ }; typedef enum zip_source_cmd zip_source_cmd_t; -#define ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd) (((zip_int64_t)1)<<(cmd)) +#define ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd) (((zip_int64_t)1) << (cmd)) + +// clang-format off #define ZIP_SOURCE_SUPPORTS_READABLE (ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_OPEN) \ | ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_READ) \ @@ -256,6 +258,8 @@ typedef enum zip_source_cmd zip_source_cmd_t; | ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_TELL_WRITE) \ | ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_REMOVE)) +// clang-format on + /* for use by sources */ struct zip_source_args_seek { zip_int64_t offset; @@ -269,32 +273,32 @@ typedef struct zip_source_args_seek zip_source_args_seek_t; /* error information */ /* use zip_error_*() to access */ struct zip_error { - int zip_err; /* libzip error code (ZIP_ER_*) */ - int sys_err; /* copy of errno (E*) or zlib error code */ - char *str; /* string representation or NULL */ + int zip_err; /* libzip error code (ZIP_ER_*) */ + int sys_err; /* copy of errno (E*) or zlib error code */ + char *str; /* string representation or NULL */ }; -#define ZIP_STAT_NAME 0x0001u -#define ZIP_STAT_INDEX 0x0002u -#define ZIP_STAT_SIZE 0x0004u -#define ZIP_STAT_COMP_SIZE 0x0008u -#define ZIP_STAT_MTIME 0x0010u -#define ZIP_STAT_CRC 0x0020u -#define ZIP_STAT_COMP_METHOD 0x0040u -#define ZIP_STAT_ENCRYPTION_METHOD 0x0080u -#define ZIP_STAT_FLAGS 0x0100u +#define ZIP_STAT_NAME 0x0001u +#define ZIP_STAT_INDEX 0x0002u +#define ZIP_STAT_SIZE 0x0004u +#define ZIP_STAT_COMP_SIZE 0x0008u +#define ZIP_STAT_MTIME 0x0010u +#define ZIP_STAT_CRC 0x0020u +#define ZIP_STAT_COMP_METHOD 0x0040u +#define ZIP_STAT_ENCRYPTION_METHOD 0x0080u +#define ZIP_STAT_FLAGS 0x0100u struct zip_stat { - zip_uint64_t valid; /* which fields have valid values */ - const char *name; /* name of the file */ - zip_uint64_t index; /* index within archive */ - zip_uint64_t size; /* size of file (uncompressed) */ - zip_uint64_t comp_size; /* size of file (compressed) */ - time_t mtime; /* modification time */ - zip_uint32_t crc; /* crc of file data */ - zip_uint16_t comp_method; /* compression method used */ - zip_uint16_t encryption_method; /* encryption method used */ - zip_uint32_t flags; /* reserved for future use */ + zip_uint64_t valid; /* which fields have valid values */ + const char *name; /* name of the file */ + zip_uint64_t index; /* index within archive */ + zip_uint64_t size; /* size of file (uncompressed) */ + zip_uint64_t comp_size; /* size of file (compressed) */ + time_t mtime; /* modification time */ + zip_uint32_t crc; /* crc of file data */ + zip_uint16_t comp_method; /* compression method used */ + zip_uint16_t encryption_method; /* encryption method used */ + zip_uint32_t flags; /* reserved for future use */ }; struct zip_buffer_fragment { @@ -322,17 +326,17 @@ typedef void (*zip_progress_callback)(zip_t *, double, void *); typedef void (*zip_progress_callback_t)(double); ZIP_EXTERN void zip_register_progress_callback(zip_t *, zip_progress_callback_t); /* use zip_register_progress_callback_with_state */ -ZIP_EXTERN zip_int64_t zip_add(zip_t *, const char *, zip_source_t *); /* use zip_file_add */ -ZIP_EXTERN zip_int64_t zip_add_dir(zip_t *, const char *); /* use zip_dir_add */ +ZIP_EXTERN zip_int64_t zip_add(zip_t *, const char *, zip_source_t *); /* use zip_file_add */ +ZIP_EXTERN zip_int64_t zip_add_dir(zip_t *, const char *); /* use zip_dir_add */ ZIP_EXTERN const char *zip_get_file_comment(zip_t *, zip_uint64_t, int *, int); /* use zip_file_get_comment */ -ZIP_EXTERN int zip_get_num_files(zip_t *); /* use zip_get_num_entries instead */ -ZIP_EXTERN int zip_rename(zip_t *, zip_uint64_t, const char *); /* use zip_file_rename */ -ZIP_EXTERN int zip_replace(zip_t *, zip_uint64_t, zip_source_t *); /* use zip_file_replace */ -ZIP_EXTERN int zip_set_file_comment(zip_t *, zip_uint64_t, const char *, int); /* use zip_file_set_comment */ -ZIP_EXTERN int zip_error_get_sys_type(int); /* use zip_error_system_type */ -ZIP_EXTERN void zip_error_get(zip_t *, int *, int *); /* use zip_get_error, zip_error_code_zip / zip_error_code_system */ -ZIP_EXTERN int zip_error_to_str(char *, zip_uint64_t, int, int); /* use zip_error_init_with_code / zip_error_strerror */ -ZIP_EXTERN void zip_file_error_get(zip_file_t *, int *, int *); /* use zip_file_get_error, zip_error_code_zip / zip_error_code_system */ +ZIP_EXTERN int zip_get_num_files(zip_t *); /* use zip_get_num_entries instead */ +ZIP_EXTERN int zip_rename(zip_t *, zip_uint64_t, const char *); /* use zip_file_rename */ +ZIP_EXTERN int zip_replace(zip_t *, zip_uint64_t, zip_source_t *); /* use zip_file_replace */ +ZIP_EXTERN int zip_set_file_comment(zip_t *, zip_uint64_t, const char *, int); /* use zip_file_set_comment */ +ZIP_EXTERN int zip_error_get_sys_type(int); /* use zip_error_system_type */ +ZIP_EXTERN void zip_error_get(zip_t *, int *, int *); /* use zip_get_error, zip_error_code_zip / zip_error_code_system */ +ZIP_EXTERN int zip_error_to_str(char *, zip_uint64_t, int, int); /* use zip_error_init_with_code / zip_error_strerror */ +ZIP_EXTERN void zip_file_error_get(zip_file_t *, int *, int *); /* use zip_file_get_error, zip_error_code_zip / zip_error_code_system */ #endif ZIP_EXTERN int zip_close(zip_t *); diff --git a/lib/zip_add.c b/lib/zip_add.c index d1be133..69966d3 100644 --- a/lib/zip_add.c +++ b/lib/zip_add.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,13 +38,12 @@ /* NOTE: Return type is signed so we can return -1 on error. - The index can not be larger than ZIP_INT64_MAX since the size - of the central directory cannot be larger than - ZIP_UINT64_MAX, and each entry is larger than 2 bytes. + The index can not be larger than ZIP_INT64_MAX since the size + of the central directory cannot be larger than + ZIP_UINT64_MAX, and each entry is larger than 2 bytes. */ ZIP_EXTERN zip_int64_t -zip_add(zip_t *za, const char *name, zip_source_t *source) -{ +zip_add(zip_t *za, const char *name, zip_source_t *source) { return zip_file_add(za, name, source, 0); } diff --git a/lib/zip_add_dir.c b/lib/zip_add_dir.c index 14bdeda..a0026a7 100644 --- a/lib/zip_add_dir.c +++ b/lib/zip_add_dir.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,7 +39,6 @@ /* NOTE: Signed due to -1 on error. See zip_add.c for more details. */ ZIP_EXTERN zip_int64_t -zip_add_dir(zip_t *za, const char *name) -{ +zip_add_dir(zip_t *za, const char *name) { return zip_dir_add(za, name, 0); } diff --git a/lib/zip_add_entry.c b/lib/zip_add_entry.c index f6212f1..f1de445 100644 --- a/lib/zip_add_entry.c +++ b/lib/zip_add_entry.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -40,11 +40,10 @@ /* NOTE: Signed due to -1 on error. See zip_add.c for more details. */ zip_int64_t -_zip_add_entry(zip_t *za) -{ +_zip_add_entry(zip_t *za) { zip_uint64_t idx; - if (za->nentry+1 >= za->nentry_alloc) { + if (za->nentry + 1 >= za->nentry_alloc) { zip_entry_t *rentries; zip_uint64_t nalloc = za->nentry_alloc; zip_uint64_t additional_entries = 2 * nalloc; @@ -75,7 +74,7 @@ _zip_add_entry(zip_t *za) idx = za->nentry++; - _zip_entry_init(za->entry+idx); + _zip_entry_init(za->entry + idx); return (zip_int64_t)idx; } diff --git a/lib/zip_algorithm_bzip2.c b/lib/zip_algorithm_bzip2.c index 1748fd4..f2fb162 100644 --- a/lib/zip_algorithm_bzip2.c +++ b/lib/zip_algorithm_bzip2.c @@ -33,8 +33,6 @@ #include "zipint.h" -#if defined(HAVE_LIBBZ2) - #include #include #include @@ -128,7 +126,6 @@ map_error(int ret) { default: return ZIP_ER_INTERNAL; } - } static bool @@ -143,7 +140,6 @@ start(void *ud) { if (ctx->compress) { ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30); - } else { ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0); @@ -179,7 +175,8 @@ end(void *ud) { } -static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) { +static bool +input(void *ud, zip_uint8_t *data, zip_uint64_t length) { struct ctx *ctx = (struct ctx *)ud; if (length > UINT_MAX || ctx->zstr.avail_in > 0) { @@ -194,7 +191,8 @@ static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) { } -static void end_of_input(void *ud) { +static void +end_of_input(void *ud) { struct ctx *ctx = (struct ctx *)ud; ctx->end_of_input = true; @@ -228,7 +226,7 @@ process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { case BZ_FINISH_OK: /* compression */ return ZIP_COMPRESSION_OK; - case BZ_OK: /* decompression */ + case BZ_OK: /* decompression */ case BZ_RUN_OK: /* compression */ if (ctx->zstr.avail_in == 0) { return ZIP_COMPRESSION_NEED_DATA; @@ -244,6 +242,7 @@ process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { } } +// clang-format off zip_compression_algorithm_t zip_algorithm_bzip2_compress = { compress_allocate, @@ -268,8 +267,4 @@ zip_compression_algorithm_t zip_algorithm_bzip2_decompress = { process }; -#else - -static int dummy __attribute__((used)); - -#endif /* HAVE_LIBBZ2 */ +// clang-format on diff --git a/lib/zip_algorithm_deflate.c b/lib/zip_algorithm_deflate.c index 55cd928..2a1c904 100644 --- a/lib/zip_algorithm_deflate.c +++ b/lib/zip_algorithm_deflate.c @@ -157,7 +157,8 @@ end(void *ud) { } -static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) { +static bool +input(void *ud, zip_uint8_t *data, zip_uint64_t length) { struct ctx *ctx = (struct ctx *)ud; if (length > UINT_MAX || ctx->zstr.avail_in > 0) { @@ -172,7 +173,8 @@ static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) { } -static void end_of_input(void *ud) { +static void +end_of_input(void *ud) { struct ctx *ctx = (struct ctx *)ud; ctx->end_of_input = true; @@ -217,6 +219,7 @@ process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { } } +// clang-format off zip_compression_algorithm_t zip_algorithm_deflate_compress = { compress_allocate, @@ -240,3 +243,5 @@ zip_compression_algorithm_t zip_algorithm_deflate_decompress = { end_of_input, process }; + +// clang-format on diff --git a/lib/zip_buffer.c b/lib/zip_buffer.c index 7addc4b..96010b2 100644 --- a/lib/zip_buffer.c +++ b/lib/zip_buffer.c @@ -37,21 +37,19 @@ #include "zipint.h" zip_uint8_t * -_zip_buffer_data(zip_buffer_t *buffer) -{ +_zip_buffer_data(zip_buffer_t *buffer) { return buffer->data; } void -_zip_buffer_free(zip_buffer_t *buffer) -{ +_zip_buffer_free(zip_buffer_t *buffer) { if (buffer == NULL) { - return; + return; } if (buffer->free_data) { - free(buffer->data); + free(buffer->data); } free(buffer); @@ -59,15 +57,13 @@ _zip_buffer_free(zip_buffer_t *buffer) bool -_zip_buffer_eof(zip_buffer_t *buffer) -{ +_zip_buffer_eof(zip_buffer_t *buffer) { return buffer->ok && buffer->offset == buffer->size; } zip_uint8_t * -_zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) -{ +_zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) { zip_uint8_t *data; data = _zip_buffer_peek(buffer, length); @@ -81,12 +77,11 @@ _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) zip_uint16_t -_zip_buffer_get_16(zip_buffer_t *buffer) -{ +_zip_buffer_get_16(zip_buffer_t *buffer) { zip_uint8_t *data = _zip_buffer_get(buffer, 2); if (data == NULL) { - return 0; + return 0; } return (zip_uint16_t)(data[0] + (data[1] << 8)); @@ -94,12 +89,11 @@ _zip_buffer_get_16(zip_buffer_t *buffer) zip_uint32_t -_zip_buffer_get_32(zip_buffer_t *buffer) -{ +_zip_buffer_get_32(zip_buffer_t *buffer) { zip_uint8_t *data = _zip_buffer_get(buffer, 4); if (data == NULL) { - return 0; + return 0; } return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0]; @@ -107,26 +101,23 @@ _zip_buffer_get_32(zip_buffer_t *buffer) zip_uint64_t -_zip_buffer_get_64(zip_buffer_t *buffer) -{ +_zip_buffer_get_64(zip_buffer_t *buffer) { zip_uint8_t *data = _zip_buffer_get(buffer, 8); if (data == NULL) { - return 0; + return 0; } return ((zip_uint64_t)data[7] << 56) + ((zip_uint64_t)data[6] << 48) + ((zip_uint64_t)data[5] << 40) + ((zip_uint64_t)data[4] << 32) + ((zip_uint64_t)data[3] << 24) + ((zip_uint64_t)data[2] << 16) + ((zip_uint64_t)data[1] << 8) + (zip_uint64_t)data[0]; } - zip_uint8_t -_zip_buffer_get_8(zip_buffer_t *buffer) -{ +_zip_buffer_get_8(zip_buffer_t *buffer) { zip_uint8_t *data = _zip_buffer_get(buffer, 1); if (data == NULL) { - return 0; + return 0; } return data[0]; @@ -134,15 +125,13 @@ _zip_buffer_get_8(zip_buffer_t *buffer) zip_uint64_t -_zip_buffer_left(zip_buffer_t *buffer) -{ +_zip_buffer_left(zip_buffer_t *buffer) { return buffer->ok ? buffer->size - buffer->offset : 0; } zip_uint64_t -_zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) -{ +_zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { if (_zip_buffer_left(buffer) < length) { length = _zip_buffer_left(buffer); } @@ -154,22 +143,21 @@ _zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) zip_buffer_t * -_zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) -{ +_zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) { bool free_data = (data == NULL); zip_buffer_t *buffer; if (data == NULL) { - if ((data = (zip_uint8_t *)malloc(size)) == NULL) { - return NULL; - } + if ((data = (zip_uint8_t *)malloc(size)) == NULL) { + return NULL; + } } if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) { - if (free_data) { - free(data); - } - return NULL; + if (free_data) { + free(data); + } + return NULL; } buffer->ok = true; @@ -183,18 +171,17 @@ _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) zip_buffer_t * -_zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error) -{ +_zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error) { zip_buffer_t *buffer; if ((buffer = _zip_buffer_new(buf, size)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } if (_zip_read(src, buffer->data, size, error) < 0) { - _zip_buffer_free(buffer); - return NULL; + _zip_buffer_free(buffer); + return NULL; } return buffer; @@ -202,28 +189,24 @@ _zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *b zip_uint64_t -_zip_buffer_offset(zip_buffer_t *buffer) -{ +_zip_buffer_offset(zip_buffer_t *buffer) { return buffer->ok ? buffer->offset : 0; } bool -_zip_buffer_ok(zip_buffer_t *buffer) -{ +_zip_buffer_ok(zip_buffer_t *buffer) { return buffer->ok; } - zip_uint8_t * -_zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) -{ +_zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) { zip_uint8_t *data; if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) { - buffer->ok = false; - return NULL; + buffer->ok = false; + return NULL; } data = buffer->data + buffer->offset; @@ -231,12 +214,11 @@ _zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) } int -_zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) -{ +_zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) { zip_uint8_t *dst = _zip_buffer_get(buffer, length); if (dst == NULL) { - return -1; + return -1; } memcpy(dst, src, length); @@ -245,12 +227,11 @@ _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) int -_zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) -{ +_zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) { zip_uint8_t *data = _zip_buffer_get(buffer, 2); if (data == NULL) { - return -1; + return -1; } data[0] = (zip_uint8_t)(i & 0xff); @@ -261,12 +242,11 @@ _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) int -_zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) -{ +_zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) { zip_uint8_t *data = _zip_buffer_get(buffer, 4); if (data == NULL) { - return -1; + return -1; } data[0] = (zip_uint8_t)(i & 0xff); @@ -279,12 +259,11 @@ _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) int -_zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) -{ +_zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) { zip_uint8_t *data = _zip_buffer_get(buffer, 8); if (data == NULL) { - return -1; + return -1; } data[0] = (zip_uint8_t)(i & 0xff); @@ -301,12 +280,11 @@ _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) int -_zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) -{ +_zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) { zip_uint8_t *data = _zip_buffer_get(buffer, 1); if (data == NULL) { - return -1; + return -1; } data[0] = i; @@ -316,11 +294,10 @@ _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) int -_zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset) -{ +_zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset) { if (offset > buffer->size) { - buffer->ok = false; - return -1; + buffer->ok = false; + return -1; } buffer->ok = true; @@ -335,14 +312,13 @@ _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length) { zip_uint64_t offset = buffer->offset + length; if (offset < buffer->offset) { - buffer->ok = false; - return -1; + buffer->ok = false; + return -1; } return _zip_buffer_set_offset(buffer, offset); } zip_uint64_t -_zip_buffer_size(zip_buffer_t *buffer) -{ +_zip_buffer_size(zip_buffer_t *buffer) { return buffer->size; } diff --git a/lib/zip_close.c b/lib/zip_close.c index a4b1761..c46e1b3 100644 --- a/lib/zip_close.c +++ b/lib/zip_close.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -43,11 +43,11 @@ #ifdef HAVE_UNISTD_H #include #endif -#include #include +#include #ifdef _WIN32 -#include #include +#include #endif @@ -57,8 +57,7 @@ static int copy_source(zip_t *, zip_source_t *, zip_int64_t); static int write_cdir(zip_t *, const zip_filelist_t *, zip_uint64_t); ZIP_EXTERN int -zip_close(zip_t *za) -{ +zip_close(zip_t *za) { zip_uint64_t i, j, survivors, unchanged_offset; zip_int64_t off; int error; @@ -80,7 +79,7 @@ zip_close(zip_t *za) } zip_discard(za); return 0; - } + } if (!changed) { zip_discard(za); @@ -88,102 +87,102 @@ zip_close(zip_t *za) } if (survivors > za->nentry) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - return -1; + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + return -1; } - - if ((filelist=(zip_filelist_t *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL) + + if ((filelist = (zip_filelist_t *)malloc(sizeof(filelist[0]) * (size_t)survivors)) == NULL) return -1; unchanged_offset = ZIP_UINT64_MAX; /* create list of files with index into original archive */ - for (i=j=0; inentry; i++) { - if (za->entry[i].orig != NULL && ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) { - unchanged_offset = ZIP_MIN(unchanged_offset, za->entry[i].orig->offset); - } - if (za->entry[i].deleted) { + for (i = j = 0; i < za->nentry; i++) { + if (za->entry[i].orig != NULL && ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) { + unchanged_offset = ZIP_MIN(unchanged_offset, za->entry[i].orig->offset); + } + if (za->entry[i].deleted) { continue; - } - - if (j >= survivors) { - free(filelist); - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - return -1; - } - + } + + if (j >= survivors) { + free(filelist); + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + return -1; + } + filelist[j].idx = i; j++; } if (j < survivors) { - free(filelist); - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - return -1; + free(filelist); + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + return -1; } if ((zip_source_supports(za->src) & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING)) == 0) { - unchanged_offset = 0; + unchanged_offset = 0; } else { - if (unchanged_offset == ZIP_UINT64_MAX) { - /* we're keeping all file data, find the end of the last one */ - zip_uint64_t last_index = ZIP_UINT64_MAX; - unchanged_offset = 0; - - for (i = 0; i < za->nentry; i++) { - if (za->entry[i].orig != NULL) { - if (za->entry[i].orig->offset >= unchanged_offset) { - unchanged_offset = za->entry[i].orig->offset; - last_index = i; - } - } - } - if (last_index != ZIP_UINT64_MAX) { - if ((unchanged_offset = _zip_file_get_end(za, last_index, &za->error)) == 0) { - free(filelist); - return -1; - } - } - } - if (unchanged_offset > 0) { - if (zip_source_begin_write_cloning(za->src, unchanged_offset) < 0) { - /* cloning not supported, need to copy everything */ - unchanged_offset = 0; - } - } + if (unchanged_offset == ZIP_UINT64_MAX) { + /* we're keeping all file data, find the end of the last one */ + zip_uint64_t last_index = ZIP_UINT64_MAX; + unchanged_offset = 0; + + for (i = 0; i < za->nentry; i++) { + if (za->entry[i].orig != NULL) { + if (za->entry[i].orig->offset >= unchanged_offset) { + unchanged_offset = za->entry[i].orig->offset; + last_index = i; + } + } + } + if (last_index != ZIP_UINT64_MAX) { + if ((unchanged_offset = _zip_file_get_end(za, last_index, &za->error)) == 0) { + free(filelist); + return -1; + } + } + } + if (unchanged_offset > 0) { + if (zip_source_begin_write_cloning(za->src, unchanged_offset) < 0) { + /* cloning not supported, need to copy everything */ + unchanged_offset = 0; + } + } } if (unchanged_offset == 0) { - if (zip_source_begin_write(za->src) < 0) { - _zip_error_set_from_source(&za->error, za->src); - free(filelist); - return -1; - } + if (zip_source_begin_write(za->src) < 0) { + _zip_error_set_from_source(&za->error, za->src); + free(filelist); + return -1; + } } _zip_progress_start(za->progress); error = 0; - for (j=0; jprogress, (double)j / (double)survivors, (double)(j+1) / (double)survivors); + _zip_progress_subrange(za->progress, (double)j / (double)survivors, (double)(j + 1) / (double)survivors); i = filelist[j].idx; - entry = za->entry+i; + entry = za->entry + i; - if (entry->orig != NULL && entry->orig->offset < unchanged_offset) { - /* already implicitly copied by cloning */ - continue; - } + if (entry->orig != NULL && entry->orig->offset < unchanged_offset) { + /* already implicitly copied by cloning */ + continue; + } new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_ENCRYPTION_METHOD)); /* create new local directory entry */ if (entry->changes == NULL) { - if ((entry->changes=_zip_dirent_clone(entry->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - error = 1; - break; + if ((entry->changes = _zip_dirent_clone(entry->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + error = 1; + break; } } de = entry->changes; @@ -193,18 +192,18 @@ zip_close(zip_t *za) break; } - if ((off = zip_source_tell_write(za->src)) < 0) { - error = 1; - break; - } - de->offset = (zip_uint64_t)off; + if ((off = zip_source_tell_write(za->src)) < 0) { + error = 1; + break; + } + de->offset = (zip_uint64_t)off; if (new_data) { zip_source_t *zs; zs = NULL; if (!ZIP_ENTRY_DATA_CHANGED(entry)) { - if ((zs=_zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) { + if ((zs = _zip_source_zip_new(za, za, i, ZIP_FL_UNCHANGED, 0, 0, NULL)) == NULL) { error = 1; break; } @@ -229,7 +228,7 @@ zip_close(zip_t *za) error = 1; break; } - if ((offset=_zip_file_get_offset(za, i, &za->error)) == 0) { + if ((offset = _zip_file_get_offset(za, i, &za->error)) == 0) { error = 1; break; } @@ -263,7 +262,7 @@ zip_close(zip_t *za) if (error) { zip_source_rollback_write(za->src); - return -1; + return -1; } zip_discard(za); @@ -273,8 +272,7 @@ zip_close(zip_t *za) static int -add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) -{ +add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) { zip_int64_t offstart, offdata, offend, data_length; struct zip_stat st; zip_source_t *src_final, *src_tmp; @@ -320,7 +318,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) de->uncomp_size = st.size; /* this is technically incorrect (copy_source counts compressed data), but it's the best we have */ data_length = (zip_int64_t)st.size; - + if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) { zip_uint64_t max_size; @@ -354,12 +352,12 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) if ((offstart = zip_source_tell_write(za->src)) < 0) { _zip_error_set_from_source(&za->error, za->src); - return -1; + return -1; } /* as long as we don't support non-seekable output, clear data descriptor bit */ de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR; - if ((is_zip64=_zip_dirent_write(za, de, flags)) < 0) { + if ((is_zip64 = _zip_dirent_write(za, de, flags)) < 0) { return -1; } @@ -377,7 +375,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) if (needs_decrypt) { zip_encryption_implementation impl; - + if ((impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); zip_source_free(src_final); @@ -392,7 +390,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) zip_source_free(src_final); src_final = src_tmp; } - + if (needs_decompress) { if ((src_tmp = zip_source_decompress(za, src_final, st.comp_method)) == NULL) { zip_source_free(src_final); @@ -418,22 +416,23 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) zip_source_free(src_final); return -1; } - + zip_source_free(src_final); src_final = src_tmp; } - + if (needs_encrypt) { zip_encryption_implementation impl; const char *password = NULL; if (de->password) { password = de->password; - } else if (za->default_password) { + } + else if (za->default_password) { password = za->default_password; } - + if ((impl = _zip_get_encryption_implementation(de->encryption_method, ZIP_CODEC_ENCODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); zip_source_free(src_final); @@ -452,11 +451,11 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) if ((offdata = zip_source_tell_write(za->src)) < 0) { _zip_error_set_from_source(&za->error, za->src); - return -1; + return -1; } ret = copy_source(za, src_final, data_length); - + if (zip_source_stat(src_final, &st) < 0) { _zip_error_set_from_source(&za->error, src_final); ret = -1; @@ -475,7 +474,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) if ((offend = zip_source_tell_write(za->src)) < 0) { _zip_error_set_from_source(&za->error, za->src); - return -1; + return -1; } if (zip_source_seek_write(za->src, offstart, SEEK_SET) < 0) { @@ -483,16 +482,16 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) return -1; } - if ((st.valid & (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD|ZIP_STAT_CRC|ZIP_STAT_SIZE)) { + if ((st.valid & (ZIP_STAT_COMP_METHOD | ZIP_STAT_CRC | ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD | ZIP_STAT_CRC | ZIP_STAT_SIZE)) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) { - if (st.valid & ZIP_STAT_MTIME) - de->last_mod = st.mtime; - else - time(&de->last_mod); + if (st.valid & ZIP_STAT_MTIME) + de->last_mod = st.mtime; + else + time(&de->last_mod); } de->comp_method = st.comp_method; de->crc = st.crc; @@ -500,16 +499,16 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) de->comp_size = (zip_uint64_t)(offend - offdata); de->bitflags = (zip_uint16_t)((de->bitflags & (zip_uint16_t)~6) | ((zip_uint8_t)compression_flags << 1)); _zip_dirent_set_version_needed(de, (flags & ZIP_FL_FORCE_ZIP64) != 0); - - if ((ret=_zip_dirent_write(za, de, flags)) < 0) + + if ((ret = _zip_dirent_write(za, de, flags)) < 0) return -1; - + if (is_zip64 != ret) { /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */ zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } - + if (zip_source_seek_write(za->src, offend, SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; @@ -520,8 +519,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de) static int -copy_data(zip_t *za, zip_uint64_t len) -{ +copy_data(zip_t *za, zip_uint64_t len) { zip_uint8_t buf[BUFSIZE]; size_t n; double total = (double)len; @@ -535,10 +533,10 @@ copy_data(zip_t *za, zip_uint64_t len) if (_zip_write(za, buf, n) < 0) { return -1; } - + len -= n; - _zip_progress_update(za->progress, (total - (double)len) / total); + _zip_progress_update(za->progress, (total - (double)len) / total); } return 0; @@ -546,8 +544,7 @@ copy_data(zip_t *za, zip_uint64_t len) static int -copy_source(zip_t *za, zip_source_t *src, zip_int64_t data_length) -{ +copy_source(zip_t *za, zip_source_t *src, zip_int64_t data_length) { zip_uint8_t buf[BUFSIZE]; zip_int64_t n, current; int ret; @@ -559,42 +556,41 @@ copy_source(zip_t *za, zip_source_t *src, zip_int64_t data_length) ret = 0; current = 0; - while ((n=zip_source_read(src, buf, sizeof(buf))) > 0) { + while ((n = zip_source_read(src, buf, sizeof(buf))) > 0) { if (_zip_write(za, buf, (zip_uint64_t)n) < 0) { ret = -1; break; } if (n == sizeof(buf) && za->progress && data_length > 0) { current += n; - _zip_progress_update(za->progress, (double)current/(double)data_length); + _zip_progress_update(za->progress, (double)current / (double)data_length); } } - + if (n < 0) { _zip_error_set_from_source(&za->error, src); ret = -1; } zip_source_close(src); - + return ret; } static int -write_cdir(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) -{ +write_cdir(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) { zip_int64_t cd_start, end, size; - + if ((cd_start = zip_source_tell_write(za->src)) < 0) { - return -1; + return -1; } - if ((size=_zip_cdir_write(za, filelist, survivors)) < 0) { + if ((size = _zip_cdir_write(za, filelist, survivors)) < 0) { return -1; } - + if ((end = zip_source_tell_write(za->src)) < 0) { - return -1; + return -1; } return 0; @@ -602,8 +598,7 @@ write_cdir(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) int -_zip_changed(const zip_t *za, zip_uint64_t *survivorsp) -{ +_zip_changed(const zip_t *za, zip_uint64_t *survivorsp) { int changed; zip_uint64_t i, survivors; @@ -614,13 +609,13 @@ _zip_changed(const zip_t *za, zip_uint64_t *survivorsp) changed = 1; } - for (i=0; inentry; i++) { - if (ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) { + for (i = 0; i < za->nentry; i++) { + if (ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) { changed = 1; - } - if (!za->entry[i].deleted) { - survivors++; - } + } + if (!za->entry[i].deleted) { + survivors++; + } } if (survivorsp) { diff --git a/lib/gladman-fcrypt.c b/lib/zip_crypto.h similarity index 61% rename from lib/gladman-fcrypt.c rename to lib/zip_crypto.h index 44622d3..9151fb9 100644 --- a/lib/gladman-fcrypt.c +++ b/lib/zip_crypto.h @@ -1,6 +1,6 @@ /* - gladman-fcrypt.c -- wrapper functions for Dr Gladman's AES functions - Copyright (C) 2016-2017 Dieter Baron and Thomas Klausner + zip_crypto.h -- crypto definitions + Copyright (C) 2017 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at @@ -9,15 +9,15 @@ modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. 3. The names of the authors may not be used to endorse or promote - products derived from this software without specific prior - written permission. - + products derived from this software without specific prior + written permission. + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -31,14 +31,20 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#define BUILDING_FCRYPT +#ifndef HAD_ZIP_CRYPTO_H +#define HAD_ZIP_CRYPTO_H + +#define ZIP_CRYPTO_SHA1_LENGTH 20 +#define ZIP_CRYPTO_AES_BLOCK_LENGTH 16 -#include "gladman-fcrypt.h" +#if defined(HAVE_OPENSSL) +#include "zip_crypto_openssl.h" +#elif defined(HAVE_GNUTLS) +#include "zip_crypto_gnutls.h" +#elif defined(HAVE_COMMONCRYPTO) +#include "zip_crypto_commoncrypto.h" +#else +#error "no crypto backend found" +#endif -#include "gladman-fcrypt/aescrypt.c" -#include "gladman-fcrypt/aeskey.c" -#include "gladman-fcrypt/aestab.c" -#include "gladman-fcrypt/fileenc.c" -#include "gladman-fcrypt/hmac.c" -#include "gladman-fcrypt/pwd2key.c" -#include "gladman-fcrypt/sha1.c" +#endif /* HAD_ZIP_CRYPTO_H */ diff --git a/lib/zip_crypto_commoncrypto.c b/lib/zip_crypto_commoncrypto.c new file mode 100644 index 0000000..79ce7f9 --- /dev/null +++ b/lib/zip_crypto_commoncrypto.c @@ -0,0 +1,133 @@ +/* + zip_crypto_commoncrypto.c -- CommonCrypto wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include + +#include "zipint.h" + +#include "zip_crypto.h" + +#include +#include + +void +_zip_crypto_aes_free(_zip_crypto_aes_t *aes) +{ + if (aes == NULL) { + return; + } + + CCCryptorRelease(aes); +} + + +bool +_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) +{ + size_t len; + CCCryptorUpdate(aes, in, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &len); + return true; +} + + +_zip_crypto_aes_t * +_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) +{ + _zip_crypto_aes_t *aes; + CCCryptorStatus ret; + + ret = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, kCCOptionECBMode, key, key_size / 8, NULL, &aes); + + switch (ret) { + case kCCSuccess: + return aes; + + case kCCMemoryFailure: + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + + case kCCParamError: + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + + default: + zip_error_set(error, ZIP_ER_INTERNAL, 0); + return NULL; + } +} + + +void +_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) +{ + if (hmac == NULL) { + return; + } + + _zip_crypto_clear(hmac, sizeof(*hmac)); + free(hmac); +} + + +_zip_crypto_hmac_t * +_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) +{ + _zip_crypto_hmac_t *hmac; + + if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + CCHmacInit(hmac, kCCHmacAlgSHA1, secret, secret_length); + + return hmac; +} + + +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { + int fd; + + if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { + return false; + } + + if (read(fd, buffer, length) != length) { + close(fd); + return false; + } + + close(fd); + return true; +} diff --git a/lib/zip_crypto_commoncrypto.h b/lib/zip_crypto_commoncrypto.h new file mode 100644 index 0000000..778341e --- /dev/null +++ b/lib/zip_crypto_commoncrypto.h @@ -0,0 +1,54 @@ +/* + zip_crypto_commoncrypto.h -- definitions for CommonCrypto wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef HAD_ZIP_CRYPTO_COMMONCRYPTO_H +#define HAD_ZIP_CRYPTO_COMMONCRYPTO_H + +#include + +#define _zip_crypto_aes_t struct _CCCryptor +#define _zip_crypto_hmac_t CCHmacContext + +void _zip_crypto_aes_free(_zip_crypto_aes_t *aes); +bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out); +_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error); + +#define _zip_crypto_hmac(hmac, data, length) (CCHmacUpdate((hmac), (data), (length)), true) +void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac); +_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error); +#define _zip_crypto_hmac_output(hmac, data) (CCHmacFinal((hmac), (data)), true) + +#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \ + (CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)(key), (key_length), (salt), (salt_length), kCCPRFHmacAlgSHA1, (iterations), (output), (output_length)) == kCCSuccess) + +#endif /* HAD_ZIP_CRYPTO_COMMONCRYPTO_H */ diff --git a/lib/zip_crypto_gnutls.c b/lib/zip_crypto_gnutls.c new file mode 100644 index 0000000..f688eae --- /dev/null +++ b/lib/zip_crypto_gnutls.c @@ -0,0 +1,140 @@ +/* + zip_crypto_gnutls.c -- GnuTLS wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include + +#include "zipint.h" +#include "zip_crypto.h" + +_zip_crypto_aes_t * +_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) +{ + _zip_crypto_aes_t *aes; + + if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + aes->key_size = key_size; + + switch (aes->key_size) { + case 128: + nettle_aes128_set_encrypt_key(&aes->ctx.ctx_128, key); + break; + case 192: + nettle_aes192_set_encrypt_key(&aes->ctx.ctx_192, key); + break; + case 256: + nettle_aes256_set_encrypt_key(&aes->ctx.ctx_256, key); + break; + default: + zip_error_set(error, ZIP_ER_INVAL, 0); + free(aes); + return NULL; + } + + return aes; +} + +bool +_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) +{ + switch (aes->key_size) { + case 128: + nettle_aes128_encrypt(&aes->ctx.ctx_128, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in); + break; + case 192: + nettle_aes192_encrypt(&aes->ctx.ctx_192, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in); + break; + case 256: + nettle_aes256_encrypt(&aes->ctx.ctx_256, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in); + break; + } + + return true; +} + +void +_zip_crypto_aes_free(_zip_crypto_aes_t *aes) +{ + if (aes == NULL) { + return; + } + + _zip_crypto_clear(aes, sizeof(*aes)); + free(aes); +} + + +_zip_crypto_hmac_t * +_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) +{ + _zip_crypto_hmac_t *hmac; + int ret; + + if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + if ((ret = gnutls_hmac_init(hmac, GNUTLS_MAC_SHA1, secret, secret_length)) < 0) { + // TODO: set error + free(hmac); + return NULL; + } + + return hmac; +} + + +void +_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) +{ + zip_uint8_t buf[ZIP_CRYPTO_SHA1_LENGTH]; + + if (hmac == NULL) { + return; + } + + gnutls_hmac_deinit(*hmac, buf); + _zip_crypto_clear(hmac, sizeof(*hmac)); + free(hmac); +} + + +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { + return gnutls_rnd(GNUTLS_RND_KEY, buffer, length) == 0; +} + diff --git a/lib/zip_crypto_gnutls.h b/lib/zip_crypto_gnutls.h new file mode 100644 index 0000000..c3d6a0e --- /dev/null +++ b/lib/zip_crypto_gnutls.h @@ -0,0 +1,66 @@ +/* + zip_crypto_gnutls.h -- definitions for GnuTLS wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef HAD_ZIP_CRYPTO_GNUTLS_H +#define HAD_ZIP_CRYPTO_GNUTLS_H + +#include +#include + +#include +#include + +typedef struct { + union { + struct aes128_ctx ctx_128; + struct aes192_ctx ctx_192; + struct aes256_ctx ctx_256; + } ctx; + zip_uint16_t key_size; +} _zip_crypto_aes_t; + +#define _zip_crypto_hmac_t gnutls_hmac_hd_t + +void _zip_crypto_aes_free(_zip_crypto_aes_t *aes); +bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out); +_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error); + +#define _zip_crypto_hmac(hmac, data, length) (gnutls_hmac(*(hmac), (data), (length)) == 0) +void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac); +_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error); +#define _zip_crypto_hmac_output(hmac, data) (gnutls_hmac_output(*(hmac), (data)), true) + +#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \ + (pbkdf2_hmac_sha1((key_length), (key), (iterations), (salt_length), (salt), (output_length), (output)), true) + +#endif /* HAD_ZIP_CRYPTO_GNUTLS_H */ diff --git a/lib/zip_crypto_openssl.c b/lib/zip_crypto_openssl.c new file mode 100644 index 0000000..ee39db3 --- /dev/null +++ b/lib/zip_crypto_openssl.c @@ -0,0 +1,137 @@ +/* + zip_crypto_openssl.c -- OpenSSL wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include + +#include "zipint.h" +#include "zip_crypto.h" + +#include + +_zip_crypto_aes_t * +_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) +{ + _zip_crypto_aes_t *aes; + + if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + AES_set_encrypt_key(key, key_size, aes); + + return aes; +} + +void +_zip_crypto_aes_free(_zip_crypto_aes_t *aes) +{ + if (aes == NULL) { + return; + } + + _zip_crypto_clear(aes, sizeof(*aes)); + free(aes); +} + + +_zip_crypto_hmac_t * +_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) +{ + _zip_crypto_hmac_t *hmac; + + if (secret_length > INT_MAX) { + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } + +#if OPENSSL_VERSION_NUMBER < 0x1010000fL + if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + HMAC_CTX_init(hmac); +#else + if ((hmac = HMAC_CTX_new()) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } +#endif + + if (HMAC_Init_ex(hmac, secret, (int)secret_length, EVP_sha1(), NULL) != 1) { + zip_error_set(error, ZIP_ER_INTERNAL, 0); +#if OPENSSL_VERSION_NUMBER < 0x1010000fL + free(hmac); +#else + HMAC_CTX_free(hmac); +#endif + return NULL; + } + + return hmac; +} + + +void +_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) +{ + if (hmac == NULL) { + return; + } + +#if OPENSSL_VERSION_NUMBER < 0x1010000fL + HMAC_CTX_cleanup(hmac); + _zip_crypto_clear(hmac, sizeof(*hmac)); + free(hmac); +#else + HMAC_CTX_free(hmac); +#endif +} + + +bool +_zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data) +{ + unsigned int length; + + return HMAC_Final(hmac, data, &length) == 1; +} + + +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { + return RAND_bytes(buffer, length) == 1; +} + + diff --git a/lib/zip_crypto_openssl.h b/lib/zip_crypto_openssl.h new file mode 100644 index 0000000..937635c --- /dev/null +++ b/lib/zip_crypto_openssl.h @@ -0,0 +1,55 @@ +/* + zip_crypto_openssl.h -- definitions for OpenSSL wrapper. + Copyright (C) 2018 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef HAD_ZIP_CRYPTO_OPENSSL_H +#define HAD_ZIP_CRYPTO_OPENSSL_H + +#include +#include + +#define _zip_crypto_aes_t AES_KEY +#define _zip_crypto_hmac_t HMAC_CTX + +void _zip_crypto_aes_free(_zip_crypto_aes_t *aes); +#define _zip_crypto_aes_encrypt_block(aes, in, out) (AES_encrypt((in), (out), (aes)), true) +_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error); + +#define _zip_crypto_hmac(hmac, data, length) (HMAC_Update((hmac), (data), (length)) == 1) +void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac); +_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error); +bool _zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data); + +#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \ + (PKCS5_PBKDF2_HMAC_SHA1((const char *)(key), (key_length), (salt), (salt_length), (iterations), (output_length), (output))) + +#endif /* HAD_ZIP_CRYPTO_OPENSSL_H */ diff --git a/lib/zip_delete.c b/lib/zip_delete.c index e160210..ae8f5a6 100644 --- a/lib/zip_delete.c +++ b/lib/zip_delete.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_delete(zip_t *za, zip_uint64_t idx) -{ +zip_delete(zip_t *za, zip_uint64_t idx) { const char *name; if (idx >= za->nentry) { @@ -50,7 +49,7 @@ zip_delete(zip_t *za, zip_uint64_t idx) return -1; } - if ((name=_zip_get_name(za, idx, 0, &za->error)) == NULL) { + if ((name = _zip_get_name(za, idx, 0, &za->error)) == NULL) { return -1; } @@ -67,4 +66,3 @@ zip_delete(zip_t *za, zip_uint64_t idx) return 0; } - diff --git a/lib/zip_dir_add.c b/lib/zip_dir_add.c index f535b21..236b439 100644 --- a/lib/zip_dir_add.c +++ b/lib/zip_dir_add.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,8 +41,7 @@ /* NOTE: Signed due to -1 on error. See zip_add.c for more details. */ ZIP_EXTERN zip_int64_t -zip_dir_add(zip_t *za, const char *name, zip_flags_t flags) -{ +zip_dir_add(zip_t *za, const char *name, zip_flags_t flags) { size_t len; zip_int64_t idx; char *s; @@ -61,21 +60,21 @@ zip_dir_add(zip_t *za, const char *name, zip_flags_t flags) s = NULL; len = strlen(name); - if (name[len-1] != '/') { - if ((s=(char *)malloc(len+2)) == NULL) { + if (name[len - 1] != '/') { + if ((s = (char *)malloc(len + 2)) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return -1; } strcpy(s, name); s[len] = '/'; - s[len+1] = '\0'; + s[len + 1] = '\0'; } - if ((source=zip_source_buffer(za, NULL, 0, 0)) == NULL) { + if ((source = zip_source_buffer(za, NULL, 0, 0)) == NULL) { free(s); return -1; } - + idx = _zip_file_replace(za, ZIP_UINT64_MAX, s ? s : name, source, flags); free(s); diff --git a/lib/zip_dirent.c b/lib/zip_dirent.c index b9feb84..060e1e9 100644 --- a/lib/zip_dirent.c +++ b/lib/zip_dirent.c @@ -35,8 +35,8 @@ #include #include #include -#include #include +#include #include #include "zipint.h" @@ -48,15 +48,14 @@ static bool _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) void -_zip_cdir_free(zip_cdir_t *cd) -{ +_zip_cdir_free(zip_cdir_t *cd) { zip_uint64_t i; if (!cd) return; - for (i=0; inentry; i++) - _zip_entry_finalize(cd->entry+i); + for (i = 0; i < cd->nentry; i++) + _zip_entry_finalize(cd->entry + i); free(cd->entry); _zip_string_free(cd->comment); free(cd); @@ -64,11 +63,10 @@ _zip_cdir_free(zip_cdir_t *cd) zip_cdir_t * -_zip_cdir_new(zip_uint64_t nentry, zip_error_t *error) -{ +_zip_cdir_new(zip_uint64_t nentry, zip_error_t *error) { zip_cdir_t *cd; - if ((cd=(zip_cdir_t *)malloc(sizeof(*cd))) == NULL) { + if ((cd = (zip_cdir_t *)malloc(sizeof(*cd))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } @@ -89,8 +87,7 @@ _zip_cdir_new(zip_uint64_t nentry, zip_error_t *error) bool -_zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *error) -{ +_zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *error) { zip_uint64_t i, new_alloc; zip_entry_t *new_entry; @@ -100,12 +97,12 @@ _zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *err new_alloc = cd->nentry_alloc + additional_entries; - if (new_alloc < additional_entries || new_alloc > SIZE_MAX/sizeof(*(cd->entry))) { + if (new_alloc < additional_entries || new_alloc > SIZE_MAX / sizeof(*(cd->entry))) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } - if ((new_entry = (zip_entry_t *)realloc(cd->entry, sizeof(*(cd->entry))*(size_t)new_alloc)) == NULL) { + if ((new_entry = (zip_entry_t *)realloc(cd->entry, sizeof(*(cd->entry)) * (size_t)new_alloc)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } @@ -113,7 +110,7 @@ _zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *err cd->entry = new_entry; for (i = cd->nentry; i < new_alloc; i++) { - _zip_entry_init(cd->entry+i); + _zip_entry_init(cd->entry + i); } cd->nentry = cd->nentry_alloc = new_alloc; @@ -123,8 +120,7 @@ _zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *err zip_int64_t -_zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) -{ +_zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) { zip_uint64_t offset, size; zip_string_t *comment; zip_uint8_t buf[EOCDLEN + EOCD64LEN + EOCD64LOCLEN]; @@ -135,25 +131,25 @@ _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivor int ret; if ((off = zip_source_tell_write(za->src)) < 0) { - _zip_error_set_from_source(&za->error, za->src); - return -1; + _zip_error_set_from_source(&za->error, za->src); + return -1; } offset = (zip_uint64_t)off; is_zip64 = false; - for (i=0; ientry+filelist[i].idx; + for (i = 0; i < survivors; i++) { + zip_entry_t *entry = za->entry + filelist[i].idx; - if ((ret=_zip_dirent_write(za, entry->changes ? entry->changes : entry->orig, ZIP_FL_CENTRAL)) < 0) + if ((ret = _zip_dirent_write(za, entry->changes ? entry->changes : entry->orig, ZIP_FL_CENTRAL)) < 0) return -1; if (ret) is_zip64 = true; } if ((off = zip_source_tell_write(za->src)) < 0) { - _zip_error_set_from_source(&za->error, za->src); - return -1; + _zip_error_set_from_source(&za->error, za->src); + return -1; } size = (zip_uint64_t)off - offset; @@ -162,13 +158,13 @@ _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivor if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; } if (is_zip64) { _zip_buffer_put(buffer, EOCD64_MAGIC, 4); - _zip_buffer_put_64(buffer, EOCD64LEN-12); + _zip_buffer_put_64(buffer, EOCD64LEN - 12); _zip_buffer_put_16(buffer, 45); _zip_buffer_put_16(buffer, 45); _zip_buffer_put_32(buffer, 0); @@ -179,7 +175,7 @@ _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivor _zip_buffer_put_64(buffer, offset); _zip_buffer_put(buffer, EOCD64LOC_MAGIC, 4); _zip_buffer_put_32(buffer, 0); - _zip_buffer_put_64(buffer, offset+size); + _zip_buffer_put_64(buffer, offset + size); _zip_buffer_put_32(buffer, 1); } @@ -195,13 +191,13 @@ _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivor _zip_buffer_put_16(buffer, (zip_uint16_t)(comment ? comment->length : 0)); if (!_zip_buffer_ok(buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(buffer); - return -1; + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(buffer); + return -1; } if (_zip_write(za, _zip_buffer_data(buffer), _zip_buffer_offset(buffer)) < 0) { - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); return -1; } @@ -218,11 +214,10 @@ _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivor zip_dirent_t * -_zip_dirent_clone(const zip_dirent_t *sde) -{ +_zip_dirent_clone(const zip_dirent_t *sde) { zip_dirent_t *tde; - if ((tde=(zip_dirent_t *)malloc(sizeof(*tde))) == NULL) + if ((tde = (zip_dirent_t *)malloc(sizeof(*tde))) == NULL) return NULL; if (sde) @@ -238,8 +233,7 @@ _zip_dirent_clone(const zip_dirent_t *sde) void -_zip_dirent_finalize(zip_dirent_t *zde) -{ +_zip_dirent_finalize(zip_dirent_t *zde) { if (!zde->cloned || zde->changed & ZIP_DIRENT_FILENAME) { _zip_string_free(zde->filename); zde->filename = NULL; @@ -263,8 +257,7 @@ _zip_dirent_finalize(zip_dirent_t *zde) void -_zip_dirent_free(zip_dirent_t *zde) -{ +_zip_dirent_free(zip_dirent_t *zde) { if (zde == NULL) return; @@ -274,8 +267,7 @@ _zip_dirent_free(zip_dirent_t *zde) void -_zip_dirent_init(zip_dirent_t *de) -{ +_zip_dirent_init(zip_dirent_t *de) { de->changed = 0; de->local_extra_fields_read = 0; de->cloned = 0; @@ -303,10 +295,8 @@ _zip_dirent_init(zip_dirent_t *de) bool -_zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags) -{ - if (de->uncomp_size >= ZIP_UINT32_MAX || de->comp_size >= ZIP_UINT32_MAX - || ((flags & ZIP_FL_CENTRAL) && de->offset >= ZIP_UINT32_MAX)) +_zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags) { + if (de->uncomp_size >= ZIP_UINT32_MAX || de->comp_size >= ZIP_UINT32_MAX || ((flags & ZIP_FL_CENTRAL) && de->offset >= ZIP_UINT32_MAX)) return true; return false; @@ -314,11 +304,10 @@ _zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags) zip_dirent_t * -_zip_dirent_new(void) -{ +_zip_dirent_new(void) { zip_dirent_t *de; - if ((de=(zip_dirent_t *)malloc(sizeof(*de))) == NULL) + if ((de = (zip_dirent_t *)malloc(sizeof(*de))) == NULL) return NULL; _zip_dirent_init(de); @@ -337,8 +326,7 @@ _zip_dirent_new(void) */ zip_int64_t -_zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, zip_error_t *error) -{ +_zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, zip_error_t *error) { zip_uint8_t buf[CDENTRYSIZE]; zip_uint16_t dostime, dosdate; zip_uint32_t size, variable_size; @@ -349,22 +337,22 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo size = local ? LENTRYSIZE : CDENTRYSIZE; if (buffer) { - if (_zip_buffer_left(buffer) < size) { - zip_error_set(error, ZIP_ER_NOZIP, 0); - return -1; - } + if (_zip_buffer_left(buffer) < size) { + zip_error_set(error, ZIP_ER_NOZIP, 0); + return -1; + } } else { - if ((buffer = _zip_buffer_new_from_source(src, size, buf, error)) == NULL) { - return -1; - } + if ((buffer = _zip_buffer_new_from_source(src, size, buf, error)) == NULL) { + return -1; + } } if (memcmp(_zip_buffer_get(buffer, 4), (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) { zip_error_set(error, ZIP_ER_NOZIP, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; } @@ -397,7 +385,8 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo zde->int_attrib = 0; zde->ext_attrib = 0; zde->offset = 0; - } else { + } + else { comment_len = _zip_buffer_get_16(buffer); zde->disk_number = _zip_buffer_get_16(buffer); zde->int_attrib = _zip_buffer_get_16(buffer); @@ -406,11 +395,11 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo } if (!_zip_buffer_ok(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } - return -1; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + if (!from_buffer) { + _zip_buffer_free(buffer); + } + return -1; } if (zde->bitflags & ZIP_GPBF_ENCRYPTED) { @@ -430,40 +419,40 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo zde->extra_fields = NULL; zde->comment = NULL; - variable_size = (zip_uint32_t)filename_len+(zip_uint32_t)ef_len+(zip_uint32_t)comment_len; + variable_size = (zip_uint32_t)filename_len + (zip_uint32_t)ef_len + (zip_uint32_t)comment_len; if (from_buffer) { - if (_zip_buffer_left(buffer) < variable_size) { - zip_error_set(error, ZIP_ER_INCONS, 0); - return -1; - } + if (_zip_buffer_left(buffer) < variable_size) { + zip_error_set(error, ZIP_ER_INCONS, 0); + return -1; + } } else { - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); - if ((buffer = _zip_buffer_new_from_source(src, variable_size, NULL, error)) == NULL) { - return -1; - } + if ((buffer = _zip_buffer_new_from_source(src, variable_size, NULL, error)) == NULL) { + return -1; + } } if (filename_len) { zde->filename = _zip_read_string(buffer, src, filename_len, 1, error); - if (!zde->filename) { - if (zip_error_code_zip(error) == ZIP_ER_EOF) { - zip_error_set(error, ZIP_ER_INCONS, 0); - } - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!zde->filename) { + if (zip_error_code_zip(error) == ZIP_ER_EOF) { + zip_error_set(error, ZIP_ER_INCONS, 0); + } + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; - } + } if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) { if (_zip_guess_encoding(zde->filename, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) { zip_error_set(error, ZIP_ER_INCONS, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; } } @@ -472,17 +461,17 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo if (ef_len) { zip_uint8_t *ef = _zip_read_data(buffer, src, ef_len, 0, error); - if (ef == NULL) { - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (ef == NULL) { + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; - } - if (!_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, &zde->extra_fields, error)) { + } + if (!_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, &zde->extra_fields, error)) { free(ef); - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; } free(ef); @@ -492,18 +481,18 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo if (comment_len) { zde->comment = _zip_read_string(buffer, src, comment_len, 0, error); - if (!zde->comment) { - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!zde->comment) { + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; - } + } if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) { if (_zip_guess_encoding(zde->comment, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) { zip_error_set(error, ZIP_ER_INCONS, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; } } @@ -516,30 +505,30 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) { zip_uint16_t got_len; - zip_buffer_t *ef_buffer; + zip_buffer_t *ef_buffer; const zip_uint8_t *ef = _zip_ef_get_by_id(zde->extra_fields, &got_len, ZIP_EF_ZIP64, 0, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, error); /* TODO: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */ - if (ef == NULL) { - if (!from_buffer) { - _zip_buffer_free(buffer); - } + if (ef == NULL) { + if (!from_buffer) { + _zip_buffer_free(buffer); + } return -1; - } + } - if ((ef_buffer = _zip_buffer_new((zip_uint8_t *)ef, got_len)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } - return -1; - } + if ((ef_buffer = _zip_buffer_new((zip_uint8_t *)ef, got_len)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + if (!from_buffer) { + _zip_buffer_free(buffer); + } + return -1; + } if (zde->uncomp_size == ZIP_UINT32_MAX) zde->uncomp_size = _zip_buffer_get_64(ef_buffer); else if (local) { /* From appnote.txt: This entry in the Local header MUST include BOTH original and compressed file size fields. */ - (void)_zip_buffer_skip(ef_buffer, 8); /* error is caught by _zip_buffer_eof() call */ + (void)_zip_buffer_skip(ef_buffer, 8); /* error is caught by _zip_buffer_eof() call */ } if (zde->comp_size == ZIP_UINT32_MAX) zde->comp_size = _zip_buffer_get_64(ef_buffer); @@ -550,26 +539,26 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo zde->disk_number = _zip_buffer_get_32(buffer); } - if (!_zip_buffer_eof(ef_buffer)) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(ef_buffer); - if (!from_buffer) { - _zip_buffer_free(buffer); - } - return -1; - } - _zip_buffer_free(ef_buffer); + if (!_zip_buffer_eof(ef_buffer)) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_buffer_free(ef_buffer); + if (!from_buffer) { + _zip_buffer_free(buffer); + } + return -1; + } + _zip_buffer_free(ef_buffer); } if (!_zip_buffer_ok(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - if (!from_buffer) { - _zip_buffer_free(buffer); - } - return -1; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + if (!from_buffer) { + _zip_buffer_free(buffer); + } + return -1; } if (!from_buffer) { - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); } /* zip_source_seek / zip_source_tell don't support values > ZIP_INT64_MAX */ @@ -589,8 +578,7 @@ _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, boo static zip_string_t * -_zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str) -{ +_zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str) { zip_uint16_t ef_len; zip_uint32_t ef_crc; zip_buffer_t *buffer; @@ -602,15 +590,15 @@ _zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string } if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) { - return str; + return str; } _zip_buffer_get_8(buffer); ef_crc = _zip_buffer_get_32(buffer); if (_zip_string_crc32(str) == ef_crc) { - zip_uint16_t len = (zip_uint16_t)_zip_buffer_left(buffer); - zip_string_t *ef_str = _zip_string_new(_zip_buffer_get(buffer, len), len, ZIP_FL_ENC_UTF_8, NULL); + zip_uint16_t len = (zip_uint16_t)_zip_buffer_left(buffer); + zip_string_t *ef_str = _zip_string_new(_zip_buffer_get(buffer, len), len, ZIP_FL_ENC_UTF_8, NULL); if (ef_str != NULL) { _zip_string_free(str); @@ -625,8 +613,7 @@ _zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string static bool -_zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) -{ +_zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) { zip_uint16_t ef_len; zip_buffer_t *buffer; const zip_uint8_t *ef; @@ -647,7 +634,7 @@ _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) { zip_error_set(error, ZIP_ER_INTERNAL, 0); - return false; + return false; } /* version */ @@ -709,8 +696,7 @@ _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) zip_int32_t -_zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) -{ +_zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) { zip_int32_t size; bool local = (flags & ZIP_EF_LOCAL) != 0; int i; @@ -720,7 +706,7 @@ _zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) size = local ? LENTRYSIZE : CDENTRYSIZE; if (zip_source_seek(src, local ? 26 : 28, SEEK_CUR) < 0) { - _zip_error_set_from_source(error, src); + _zip_error_set_from_source(error, src); return -1; } @@ -728,14 +714,14 @@ _zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) return -1; } - for (i=0; i<(local ? 2 : 3); i++) { + for (i = 0; i < (local ? 2 : 3); i++) { size += _zip_buffer_get_16(buffer); } if (!_zip_buffer_eof(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); + zip_error_set(error, ZIP_ER_INTERNAL, 0); _zip_buffer_free(buffer); - return -1; + return -1; } _zip_buffer_free(buffer); @@ -754,8 +740,7 @@ _zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) */ int -_zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) -{ +_zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) { zip_uint16_t dostime, dosdate; zip_encoding_type_t com_enc, name_enc; zip_extra_field_t *ef; @@ -772,9 +757,7 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) name_enc = _zip_guess_encoding(de->filename, ZIP_ENCODING_UNKNOWN); com_enc = _zip_guess_encoding(de->comment, ZIP_ENCODING_UNKNOWN); - if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) || - (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) || - (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN)) + if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) || (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) || (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN)) de->bitflags |= ZIP_GPBF_ENCODING_UTF_8; else { de->bitflags &= (zip_uint16_t)~ZIP_GPBF_ENCODING_UTF_8; @@ -783,7 +766,7 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) if (ef == NULL) return -1; } - if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN){ + if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN) { zip_extra_field_t *ef2 = _zip_ef_utf8(ZIP_EF_UTF_8_COMMENT, de->comment, &za->error); if (ef2 == NULL) { _zip_ef_free(ef); @@ -802,90 +785,90 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) } is_really_zip64 = _zip_dirent_needs_zip64(de, flags); - is_zip64 = (flags & (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64) || is_really_zip64; + is_zip64 = (flags & (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64) || is_really_zip64; is_winzip_aes = de->encryption_method == ZIP_EM_AES_128 || de->encryption_method == ZIP_EM_AES_192 || de->encryption_method == ZIP_EM_AES_256; if (is_zip64) { - zip_uint8_t ef_zip64[EFZIP64SIZE]; - zip_buffer_t *ef_buffer = _zip_buffer_new(ef_zip64, sizeof(ef_zip64)); - if (ef_buffer == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + zip_uint8_t ef_zip64[EFZIP64SIZE]; + zip_buffer_t *ef_buffer = _zip_buffer_new(ef_zip64, sizeof(ef_zip64)); + if (ef_buffer == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); _zip_ef_free(ef); - return -1; - } - - if (flags & ZIP_FL_LOCAL) { - if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) { - _zip_buffer_put_64(ef_buffer, de->uncomp_size); - _zip_buffer_put_64(ef_buffer, de->comp_size); - } - } - else { - if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX || de->offset > ZIP_UINT32_MAX) { - if (de->uncomp_size >= ZIP_UINT32_MAX) { - _zip_buffer_put_64(ef_buffer, de->uncomp_size); - } - if (de->comp_size >= ZIP_UINT32_MAX) { - _zip_buffer_put_64(ef_buffer, de->comp_size); - } - if (de->offset >= ZIP_UINT32_MAX) { - _zip_buffer_put_64(ef_buffer, de->offset); - } - } - } - - if (!_zip_buffer_ok(ef_buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(ef_buffer); + return -1; + } + + if (flags & ZIP_FL_LOCAL) { + if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) { + _zip_buffer_put_64(ef_buffer, de->uncomp_size); + _zip_buffer_put_64(ef_buffer, de->comp_size); + } + } + else { + if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX || de->offset > ZIP_UINT32_MAX) { + if (de->uncomp_size >= ZIP_UINT32_MAX) { + _zip_buffer_put_64(ef_buffer, de->uncomp_size); + } + if (de->comp_size >= ZIP_UINT32_MAX) { + _zip_buffer_put_64(ef_buffer, de->comp_size); + } + if (de->offset >= ZIP_UINT32_MAX) { + _zip_buffer_put_64(ef_buffer, de->offset); + } + } + } + + if (!_zip_buffer_ok(ef_buffer)) { + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(ef_buffer); _zip_ef_free(ef); - return -1; - } + return -1; + } - ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(_zip_buffer_offset(ef_buffer)), ef_zip64, ZIP_EF_BOTH); - _zip_buffer_free(ef_buffer); - ef64->next = ef; - ef = ef64; + ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(_zip_buffer_offset(ef_buffer)), ef_zip64, ZIP_EF_BOTH); + _zip_buffer_free(ef_buffer); + ef64->next = ef; + ef = ef64; } if (is_winzip_aes) { zip_uint8_t data[EF_WINZIP_AES_SIZE]; - zip_buffer_t *ef_buffer = _zip_buffer_new(data, sizeof(data)); + zip_buffer_t *ef_buffer = _zip_buffer_new(data, sizeof(data)); zip_extra_field_t *ef_winzip; - - if (ef_buffer == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + + if (ef_buffer == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); _zip_ef_free(ef); - return -1; - } + return -1; + } _zip_buffer_put_16(ef_buffer, 2); _zip_buffer_put(ef_buffer, "AE", 2); _zip_buffer_put_8(ef_buffer, (zip_uint8_t)(de->encryption_method & 0xff)); _zip_buffer_put_16(ef_buffer, (zip_uint16_t)de->comp_method); - if (!_zip_buffer_ok(ef_buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(ef_buffer); + if (!_zip_buffer_ok(ef_buffer)) { + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(ef_buffer); _zip_ef_free(ef); - return -1; - } + return -1; + } - ef_winzip = _zip_ef_new(ZIP_EF_WINZIP_AES, EF_WINZIP_AES_SIZE, data, ZIP_EF_BOTH); - _zip_buffer_free(ef_buffer); - ef_winzip->next = ef; - ef = ef_winzip; + ef_winzip = _zip_ef_new(ZIP_EF_WINZIP_AES, EF_WINZIP_AES_SIZE, data, ZIP_EF_BOTH); + _zip_buffer_free(ef_buffer); + ef_winzip->next = ef; + ef = ef_winzip; } if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - _zip_ef_free(ef); - return -1; + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + _zip_ef_free(ef); + return -1; } _zip_buffer_put(buffer, (flags & ZIP_FL_LOCAL) ? LOCAL_MAGIC : CENTRAL_MAGIC, 4); if ((flags & ZIP_FL_LOCAL) == 0) { - _zip_buffer_put_16(buffer, (zip_uint16_t)(is_really_zip64 ? 45 : de->version_madeby)); + _zip_buffer_put_16(buffer, (zip_uint16_t)(is_really_zip64 ? 45 : de->version_madeby)); } _zip_buffer_put_16(buffer, (zip_uint16_t)(is_really_zip64 ? 45 : de->version_needed)); _zip_buffer_put_16(buffer, de->bitflags); @@ -900,7 +883,7 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) _zip_buffer_put_16(buffer, dostime); _zip_buffer_put_16(buffer, dosdate); - if (is_winzip_aes && de->uncomp_size < 20) { + if (is_winzip_aes && de->uncomp_size < 20) { _zip_buffer_put_32(buffer, 0); } else { @@ -917,18 +900,18 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) _zip_buffer_put_32(buffer, ZIP_UINT32_MAX); } else { - if (de->comp_size < ZIP_UINT32_MAX) { + if (de->comp_size < ZIP_UINT32_MAX) { _zip_buffer_put_32(buffer, (zip_uint32_t)de->comp_size); - } - else { + } + else { _zip_buffer_put_32(buffer, ZIP_UINT32_MAX); - } - if (de->uncomp_size < ZIP_UINT32_MAX) { + } + if (de->uncomp_size < ZIP_UINT32_MAX) { _zip_buffer_put_32(buffer, (zip_uint32_t)de->uncomp_size); - } - else { + } + else { _zip_buffer_put_32(buffer, ZIP_UINT32_MAX); - } + } } _zip_buffer_put_16(buffer, _zip_string_length(de->filename)); @@ -948,30 +931,30 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) } if (!_zip_buffer_ok(buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(buffer); - _zip_ef_free(ef); - return -1; + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(buffer); + _zip_ef_free(ef); + return -1; } if (_zip_write(za, buf, _zip_buffer_offset(buffer)) < 0) { - _zip_buffer_free(buffer); - _zip_ef_free(ef); - return -1; + _zip_buffer_free(buffer); + _zip_ef_free(ef); + return -1; } _zip_buffer_free(buffer); if (de->filename) { if (_zip_string_write(za, de->filename) < 0) { - _zip_ef_free(ef); + _zip_ef_free(ef); return -1; } } if (ef) { if (_zip_ef_write(za, ef, ZIP_EF_BOTH) < 0) { - _zip_ef_free(ef); + _zip_ef_free(ef); return -1; } } @@ -996,8 +979,7 @@ _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) static time_t -_zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate) -{ +_zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate) { struct tm tm; memset(&tm, 0, sizeof(tm)); @@ -1005,37 +987,36 @@ _zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate) /* let mktime decide if DST is in effect */ tm.tm_isdst = -1; - tm.tm_year = ((ddate>>9)&127) + 1980 - 1900; - tm.tm_mon = ((ddate>>5)&15) - 1; - tm.tm_mday = ddate&31; + tm.tm_year = ((ddate >> 9) & 127) + 1980 - 1900; + tm.tm_mon = ((ddate >> 5) & 15) - 1; + tm.tm_mday = ddate & 31; - tm.tm_hour = (dtime>>11)&31; - tm.tm_min = (dtime>>5)&63; - tm.tm_sec = (dtime<<1)&62; + tm.tm_hour = (dtime >> 11) & 31; + tm.tm_min = (dtime >> 5) & 63; + tm.tm_sec = (dtime << 1) & 62; return mktime(&tm); } static zip_extra_field_t * -_zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error) -{ +_zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error) { const zip_uint8_t *raw; zip_uint32_t len; zip_buffer_t *buffer; zip_extra_field_t *ef; - if ((raw=_zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL)) == NULL) { + if ((raw = _zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL)) == NULL) { /* error already set */ return NULL; } - if (len+5 > ZIP_UINT16_MAX) { - zip_error_set(error, ZIP_ER_INVAL, 0); /* TODO: better error code? */ - return NULL; + if (len + 5 > ZIP_UINT16_MAX) { + zip_error_set(error, ZIP_ER_INVAL, 0); /* TODO: better error code? */ + return NULL; } - if ((buffer = _zip_buffer_new(NULL, len+5)) == NULL) { + if ((buffer = _zip_buffer_new(NULL, len + 5)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } @@ -1045,9 +1026,9 @@ _zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error) _zip_buffer_put(buffer, raw, len); if (!_zip_buffer_ok(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(buffer); - return NULL; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(buffer); + return NULL; } ef = _zip_ef_new(id, (zip_uint16_t)(_zip_buffer_offset(buffer)), _zip_buffer_data(buffer), ZIP_EF_BOTH); @@ -1058,8 +1039,7 @@ _zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error) zip_dirent_t * -_zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error) -{ +_zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error) { if (error == NULL) error = &za->error; @@ -1084,11 +1064,8 @@ _zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *err } - - void -_zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate) -{ +_zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate) { struct tm *tm; tm = localtime(&intime); @@ -1096,8 +1073,8 @@ _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate) tm->tm_year = 80; } - *ddate = (zip_uint16_t)(((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + tm->tm_mday); - *dtime = (zip_uint16_t)(((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + ((tm->tm_sec)>>1)); + *ddate = (zip_uint16_t)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday); + *dtime = (zip_uint16_t)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1)); return; } @@ -1121,7 +1098,7 @@ _zip_dirent_set_version_needed(zip_dirent_t *de, bool force_zip64) { de->version_needed = 45; return; } - + if (de->comp_method == ZIP_CM_DEFLATE || de->encryption_method == ZIP_EM_TRAD_PKWARE) { de->version_needed = 20; return; @@ -1129,11 +1106,11 @@ _zip_dirent_set_version_needed(zip_dirent_t *de, bool force_zip64) { /* directory */ if ((length = _zip_string_length(de->filename)) > 0) { - if (de->filename->raw[length-1] == '/') { + if (de->filename->raw[length - 1] == '/') { de->version_needed = 20; return; } } - + de->version_needed = 10; } diff --git a/lib/zip_discard.c b/lib/zip_discard.c index f0188b5..5bbf8d6 100644 --- a/lib/zip_discard.c +++ b/lib/zip_discard.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -42,8 +42,7 @@ corresponding file. */ void -zip_discard(zip_t *za) -{ +zip_discard(zip_t *za) { zip_uint64_t i; if (za == NULL) @@ -61,12 +60,12 @@ zip_discard(zip_t *za) _zip_hash_free(za->names); if (za->entry) { - for (i=0; inentry; i++) - _zip_entry_finalize(za->entry+i); + for (i = 0; i < za->nentry; i++) + _zip_entry_finalize(za->entry + i); free(za->entry); } - for (i=0; inopen_source; i++) { + for (i = 0; i < za->nopen_source; i++) { _zip_source_invalidate(za->open_source[i]); } free(za->open_source); @@ -74,7 +73,7 @@ zip_discard(zip_t *za) _zip_progress_free(za->progress); zip_error_fini(&za->error); - + free(za); return; diff --git a/lib/zip_entry.c b/lib/zip_entry.c index 6f89006..f083412 100644 --- a/lib/zip_entry.c +++ b/lib/zip_entry.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,8 +35,7 @@ #include "zipint.h" void -_zip_entry_finalize(zip_entry_t *e) -{ +_zip_entry_finalize(zip_entry_t *e) { _zip_unchange_data(e); _zip_dirent_free(e->orig); _zip_dirent_free(e->changes); @@ -44,8 +43,7 @@ _zip_entry_finalize(zip_entry_t *e) void -_zip_entry_init(zip_entry_t *e) -{ +_zip_entry_init(zip_entry_t *e) { e->orig = NULL; e->changes = NULL; e->source = NULL; diff --git a/lib/zip_err_str.c b/lib/zip_err_str.c index 724849b..3d9ee54 100644 --- a/lib/zip_err_str.c +++ b/lib/zip_err_str.c @@ -5,78 +5,16 @@ #include "zipint.h" -const char * const _zip_err_str[] = { - "No error", - "Multi-disk zip archives not supported", - "Renaming temporary file failed", - "Closing zip archive failed", - "Seek error", - "Read error", - "Write error", - "CRC error", - "Containing zip archive was closed", - "No such file", - "File already exists", - "Can't open file", - "Failure to create temporary file", - "Zlib error", - "Malloc failure", - "Entry has been changed", - "Compression method not supported", - "Premature end of file", - "Invalid argument", - "Not a zip archive", - "Internal error", - "Zip archive inconsistent", - "Can't remove file", - "Entry has been deleted", - "Encryption method not supported", - "Read-only archive", - "No password provided", - "Wrong password provided", - "Operation not supported", - "Resource still in use", - "Tell error", - "Compressed data invalid", +const char *const _zip_err_str[] = { + "No error", "Multi-disk zip archives not supported", "Renaming temporary file failed", "Closing zip archive failed", "Seek error", "Read error", "Write error", "CRC error", "Containing zip archive was closed", "No such file", "File already exists", "Can't open file", "Failure to create temporary file", "Zlib error", "Malloc failure", "Entry has been changed", "Compression method not supported", "Premature end of file", "Invalid argument", "Not a zip archive", "Internal error", "Zip archive inconsistent", "Can't remove file", "Entry has been deleted", "Encryption method not supported", "Read-only archive", "No password provided", "Wrong password provided", "Operation not supported", "Resource still in use", "Tell error", "Compressed data invalid", }; -const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]); +const int _zip_nerr_str = sizeof(_zip_err_str) / sizeof(_zip_err_str[0]); #define N ZIP_ET_NONE #define S ZIP_ET_SYS #define Z ZIP_ET_ZLIB const int _zip_err_type[] = { - N, - N, - S, - S, - S, - S, - S, - N, - N, - N, - N, - S, - S, - Z, - N, - N, - N, - N, - N, - N, - N, - N, - S, - N, - N, - N, - N, - N, - N, - N, - S, - N, + N, N, S, S, S, S, S, N, N, N, N, S, S, Z, N, N, N, N, N, N, N, N, S, N, N, N, N, N, N, N, S, N, }; diff --git a/lib/zip_error.c b/lib/zip_error.c index 5f4e07d..9ccaf91 100644 --- a/lib/zip_error.c +++ b/lib/zip_error.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,24 +49,21 @@ zip_error_code_zip(const zip_error_t *error) { ZIP_EXTERN void -zip_error_fini(zip_error_t *err) -{ +zip_error_fini(zip_error_t *err) { free(err->str); err->str = NULL; } ZIP_EXTERN void -zip_error_init(zip_error_t *err) -{ +zip_error_init(zip_error_t *err) { err->zip_err = ZIP_ER_OK; err->sys_err = 0; err->str = NULL; } ZIP_EXTERN void -zip_error_init_with_code(zip_error_t *error, int ze) -{ +zip_error_init_with_code(zip_error_t *error, int ze) { zip_error_init(error); error->zip_err = ze; switch (zip_error_system_type(error)) { @@ -77,22 +74,21 @@ zip_error_init_with_code(zip_error_t *error, int ze) default: error->sys_err = 0; break; - } + } } ZIP_EXTERN int zip_error_system_type(const zip_error_t *error) { if (error->zip_err < 0 || error->zip_err >= _zip_nerr_str) - return ZIP_ET_NONE; - + return ZIP_ET_NONE; + return _zip_err_type[error->zip_err]; } void -_zip_error_clear(zip_error_t *err) -{ +_zip_error_clear(zip_error_t *err) { if (err == NULL) return; @@ -102,16 +98,18 @@ _zip_error_clear(zip_error_t *err) void -_zip_error_copy(zip_error_t *dst, const zip_error_t *src) -{ +_zip_error_copy(zip_error_t *dst, const zip_error_t *src) { + if (dst == NULL) { + return; + } + dst->zip_err = src->zip_err; dst->sys_err = src->sys_err; } void -_zip_error_get(const zip_error_t *err, int *zep, int *sep) -{ +_zip_error_get(const zip_error_t *err, int *zep, int *sep) { if (zep) *zep = err->zip_err; if (sep) { @@ -124,8 +122,7 @@ _zip_error_get(const zip_error_t *err, int *zep, int *sep) void -zip_error_set(zip_error_t *err, int ze, int se) -{ +zip_error_set(zip_error_t *err, int ze, int se) { if (err) { err->zip_err = ze; err->sys_err = se; @@ -134,22 +131,20 @@ zip_error_set(zip_error_t *err, int ze, int se) void -_zip_error_set_from_source(zip_error_t *err, zip_source_t *src) -{ +_zip_error_set_from_source(zip_error_t *err, zip_source_t *src) { _zip_error_copy(err, zip_source_error(src)); } zip_int64_t -zip_error_to_data(const zip_error_t *error, void *data, zip_uint64_t length) -{ +zip_error_to_data(const zip_error_t *error, void *data, zip_uint64_t length) { int *e = (int *)data; - - if (length < sizeof(int)*2) { - return -1; + + if (length < sizeof(int) * 2) { + return -1; } - + e[0] = zip_error_code_zip(error); e[1] = zip_error_code_system(error); - return sizeof(int)*2; + return sizeof(int) * 2; } diff --git a/lib/zip_error_clear.c b/lib/zip_error_clear.c index ec45e68..2cce450 100644 --- a/lib/zip_error_clear.c +++ b/lib/zip_error_clear.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN void -zip_error_clear(zip_t *za) -{ +zip_error_clear(zip_t *za) { if (za == NULL) return; diff --git a/lib/zip_error_get.c b/lib/zip_error_get.c index c222018..1f02ed3 100644 --- a/lib/zip_error_get.c +++ b/lib/zip_error_get.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,21 +37,18 @@ ZIP_EXTERN void -zip_error_get(zip_t *za, int *zep, int *sep) -{ +zip_error_get(zip_t *za, int *zep, int *sep) { _zip_error_get(&za->error, zep, sep); } ZIP_EXTERN zip_error_t * -zip_get_error(zip_t *za) -{ +zip_get_error(zip_t *za) { return &za->error; } ZIP_EXTERN zip_error_t * -zip_file_get_error(zip_file_t *f) -{ +zip_file_get_error(zip_file_t *f) { return &f->error; } diff --git a/lib/zip_error_get_sys_type.c b/lib/zip_error_get_sys_type.c index 7e27bbf..d7a594e 100644 --- a/lib/zip_error_get_sys_type.c +++ b/lib/zip_error_get_sys_type.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_error_get_sys_type(int ze) -{ +zip_error_get_sys_type(int ze) { if (ze < 0 || ze >= _zip_nerr_str) return 0; diff --git a/lib/zip_error_strerror.c b/lib/zip_error_strerror.c index bdc1e44..6baf900 100644 --- a/lib/zip_error_strerror.c +++ b/lib/zip_error_strerror.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -40,8 +40,7 @@ ZIP_EXTERN const char * -zip_error_strerror(zip_error_t *err) -{ +zip_error_strerror(zip_error_t *err) { const char *zs, *ss; char buf[128], *s; @@ -54,7 +53,7 @@ zip_error_strerror(zip_error_t *err) } else { zs = _zip_err_str[err->zip_err]; - + switch (_zip_err_type[err->zip_err]) { case ZIP_ET_SYS: ss = strerror(err->sys_err); @@ -72,14 +71,10 @@ zip_error_strerror(zip_error_t *err) if (ss == NULL) return zs; else { - if ((s=(char *)malloc(strlen(ss) - + (zs ? strlen(zs)+2 : 0) + 1)) == NULL) + if ((s = (char *)malloc(strlen(ss) + (zs ? strlen(zs) + 2 : 0) + 1)) == NULL) return _zip_err_str[ZIP_ER_MEMORY]; - - sprintf(s, "%s%s%s", - (zs ? zs : ""), - (zs ? ": " : ""), - ss); + + sprintf(s, "%s%s%s", (zs ? zs : ""), (zs ? ": " : ""), ss); err->str = s; return s; diff --git a/lib/zip_error_to_str.c b/lib/zip_error_to_str.c index 019feff..518c0e5 100644 --- a/lib/zip_error_to_str.c +++ b/lib/zip_error_to_str.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,28 +41,26 @@ ZIP_EXTERN int -zip_error_to_str(char *buf, zip_uint64_t len, int ze, int se) -{ +zip_error_to_str(char *buf, zip_uint64_t len, int ze, int se) { const char *zs, *ss; if (ze < 0 || ze >= _zip_nerr_str) return snprintf(buf, len, "Unknown error %d", ze); zs = _zip_err_str[ze]; - + switch (_zip_err_type[ze]) { case ZIP_ET_SYS: ss = strerror(se); break; - + case ZIP_ET_ZLIB: ss = zError(se); break; - + default: ss = NULL; } - return snprintf(buf, len, "%s%s%s", - zs, (ss ? ": " : ""), (ss ? ss : "")); + return snprintf(buf, len, "%s%s%s", zs, (ss ? ": " : ""), (ss ? ss : "")); } diff --git a/lib/zip_extra_field.c b/lib/zip_extra_field.c index 92f36de..42f97d0 100644 --- a/lib/zip_extra_field.c +++ b/lib/zip_extra_field.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,42 +38,40 @@ zip_extra_field_t * -_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) -{ +_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) { zip_extra_field_t *head, *prev, *def; - + head = prev = NULL; - + while (ef) { - if ((def=_zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_ef_free(head); - return NULL; - } - - if (head == NULL) - head = def; - if (prev) - prev->next = def; - prev = def; + if ((def = _zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + _zip_ef_free(head); + return NULL; + } + + if (head == NULL) + head = def; + if (prev) + prev->next = def; + prev = def; ef = ef->next; } - + return head; } zip_extra_field_t * -_zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags) -{ +_zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags) { zip_extra_field_t *head, *prev; int i; i = 0; head = ef; prev = NULL; - for (; ef; ef=(prev ? prev->next : head)) { + for (; ef; ef = (prev ? prev->next : head)) { if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) { if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) { ef->flags &= ~(flags & ZIP_EF_BOTH); @@ -89,7 +87,7 @@ _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx continue; } } - + i++; if (i > id_idx) break; @@ -101,10 +99,8 @@ _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx } - void -_zip_ef_free(zip_extra_field_t *ef) -{ +_zip_ef_free(zip_extra_field_t *ef) { zip_extra_field_t *ef2; while (ef) { @@ -117,14 +113,13 @@ _zip_ef_free(zip_extra_field_t *ef) const zip_uint8_t * -_zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error) -{ - static const zip_uint8_t empty[1] = { '\0' }; - +_zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error) { + static const zip_uint8_t empty[1] = {'\0'}; + int i; i = 0; - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) { if (i < id_idx) { i++; @@ -146,22 +141,21 @@ _zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t zip_extra_field_t * -_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) -{ +_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) { zip_extra_field_t *ef2, *tt, *tail; int duplicate; if (to == NULL) return from; - for (tail=to; tail->next; tail=tail->next) + for (tail = to; tail->next; tail = tail->next) ; - for (; from; from=ef2) { + for (; from; from = ef2) { ef2 = from->next; duplicate = 0; - for (tt=to; tt; tt=tt->next) { + for (tt = to; tt; tt = tt->next) { if (tt->id == from->id && tt->size == from->size && (tt->size == 0 || memcmp(tt->data, from->data, tt->size) == 0)) { tt->flags |= (from->flags & ZIP_EF_BOTH); duplicate = 1; @@ -181,11 +175,10 @@ _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) zip_extra_field_t * -_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) -{ +_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) { zip_extra_field_t *ef; - if ((ef=(zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) + if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) return NULL; ef->next = NULL; @@ -193,7 +186,7 @@ _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_fla ef->id = id; ef->size = size; if (size > 0) { - if ((ef->data=(zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) { + if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) { free(ef); return NULL; } @@ -206,36 +199,35 @@ _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_fla bool -_zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error) -{ +_zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error) { zip_buffer_t *buffer; zip_extra_field_t *ef, *ef2, *ef_head; if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return false; + zip_error_set(error, ZIP_ER_MEMORY, 0); + return false; } - + ef_head = ef = NULL; - + while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) { - zip_uint16_t fid, flen; - zip_uint8_t *ef_data; - - fid = _zip_buffer_get_16(buffer); + zip_uint16_t fid, flen; + zip_uint8_t *ef_data; + + fid = _zip_buffer_get_16(buffer); flen = _zip_buffer_get_16(buffer); - ef_data = _zip_buffer_get(buffer, flen); + ef_data = _zip_buffer_get(buffer, flen); - if (ef_data == NULL) { + if (ef_data == NULL) { zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; - } - - if ((ef2=_zip_ef_new(fid, flen, ef_data, flags)) == NULL) { + } + + if ((ef2 = _zip_ef_new(fid, flen, ef_data, flags)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; } @@ -268,52 +260,50 @@ _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_ *ef_head_p = ef_head; } else { - _zip_ef_free(ef_head); + _zip_ef_free(ef_head); } - + return true; } zip_extra_field_t * -_zip_ef_remove_internal(zip_extra_field_t *ef) -{ +_zip_ef_remove_internal(zip_extra_field_t *ef) { zip_extra_field_t *ef_head; zip_extra_field_t *prev, *next; - + ef_head = ef; prev = NULL; - + while (ef) { - if (ZIP_EF_IS_INTERNAL(ef->id)) { - next = ef->next; - if (ef_head == ef) - ef_head = next; - ef->next = NULL; - _zip_ef_free(ef); - if (prev) - prev->next = next; - ef = next; - } - else { - prev = ef; - ef = ef->next; - } + if (ZIP_EF_IS_INTERNAL(ef->id)) { + next = ef->next; + if (ef_head == ef) + ef_head = next; + ef->next = NULL; + _zip_ef_free(ef); + if (prev) + prev->next = next; + ef = next; + } + else { + prev = ef; + ef = ef->next; + } } - + return ef_head; } zip_uint16_t -_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) -{ +_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint16_t size; size = 0; - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) - size = (zip_uint16_t)(size+4+ef->size); + size = (zip_uint16_t)(size + 4 + ef->size); } return size; @@ -321,8 +311,7 @@ _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) int -_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) -{ +_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint8_t b[4]; zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b)); @@ -330,37 +319,36 @@ _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) return -1; } - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) { - _zip_buffer_set_offset(buffer, 0); - _zip_buffer_put_16(buffer, ef->id); + _zip_buffer_set_offset(buffer, 0); + _zip_buffer_put_16(buffer, ef->id); _zip_buffer_put_16(buffer, ef->size); - if (!_zip_buffer_ok(buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(buffer); - return -1; - } + if (!_zip_buffer_ok(buffer)) { + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(buffer); + return -1; + } if (_zip_write(za, b, 4) < 0) { - _zip_buffer_free(buffer); - return -1; + _zip_buffer_free(buffer); + return -1; } if (ef->size > 0) { if (_zip_write(za, ef->data, ef->size) < 0) { - _zip_buffer_free(buffer); - return -1; + _zip_buffer_free(buffer); + return -1; } } } } - + _zip_buffer_free(buffer); return 0; } int -_zip_read_local_ef(zip_t *za, zip_uint64_t idx) -{ +_zip_read_local_ef(zip_t *za, zip_uint64_t idx) { zip_entry_t *e; unsigned char b[4]; zip_buffer_t *buffer; @@ -371,7 +359,7 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) return -1; } - e = za->entry+idx; + e = za->entry + idx; if (e->orig == NULL || e->orig->local_extra_fields_read) return 0; @@ -379,28 +367,28 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) if (e->orig->offset + 26 > ZIP_INT64_MAX) { zip_error_set(&za->error, ZIP_ER_SEEK, EFBIG); return -1; - } + } if (zip_source_seek(za->src, (zip_int64_t)(e->orig->offset + 26), SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } - + if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) { - return -1; + return -1; } - + fname_len = _zip_buffer_get_16(buffer); ef_len = _zip_buffer_get_16(buffer); - + if (!_zip_buffer_eof(buffer)) { - _zip_buffer_free(buffer); - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - return -1; + _zip_buffer_free(buffer); + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + return -1; } - + _zip_buffer_free(buffer); - + if (ef_len > 0) { zip_extra_field_t *ef; zip_uint8_t *ef_raw; @@ -428,7 +416,7 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) } e->orig->local_extra_fields_read = 1; - + if (e->changes && e->changes->local_extra_fields_read == 0) { e->changes->extra_fields = e->orig->extra_fields; e->changes->local_extra_fields_read = 1; diff --git a/lib/zip_extra_field_api.c b/lib/zip_extra_field_api.c index ed93944..469abd2 100644 --- a/lib/zip_extra_field_api.c +++ b/lib/zip_extra_field_api.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags) -{ +zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags) { zip_dirent_t *de; if ((flags & ZIP_EF_BOTH) == 0) { @@ -49,28 +48,27 @@ zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zi zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } - + if (_zip_get_dirent(za, idx, 0, NULL) == NULL) return -1; - + if (ZIP_IS_RDONLY(za)) { zip_error_set(&za->error, ZIP_ER_RDONLY, 0); return -1; } if (_zip_file_extra_field_prepare_for_change(za, idx) < 0) - return -1; - + return -1; + de = za->entry[idx].changes; - + de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ZIP_EXTRA_FIELD_ALL, ef_idx, flags); return 0; } ZIP_EXTERN int -zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_flags_t flags) -{ +zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_flags_t flags) { zip_dirent_t *de; if ((flags & ZIP_EF_BOTH) == 0) { @@ -82,7 +80,7 @@ zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } - + if (_zip_get_dirent(za, idx, 0, NULL) == NULL) return -1; @@ -90,10 +88,10 @@ zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i zip_error_set(&za->error, ZIP_ER_RDONLY, 0); return -1; } - + if (_zip_file_extra_field_prepare_for_change(za, idx) < 0) - return -1; - + return -1; + de = za->entry[idx].changes; de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags); @@ -102,9 +100,8 @@ zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i ZIP_EXTERN const zip_uint8_t * -zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_uint16_t *idp, zip_uint16_t *lenp, zip_flags_t flags) -{ - static const zip_uint8_t empty[1] = { '\0' }; +zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_uint16_t *idp, zip_uint16_t *lenp, zip_flags_t flags) { + static const zip_uint8_t empty[1] = {'\0'}; zip_dirent_t *de; zip_extra_field_t *ef; @@ -115,7 +112,7 @@ zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_u return NULL; } - if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL) return NULL; if (flags & ZIP_FL_LOCAL) @@ -123,7 +120,7 @@ zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_u return NULL; i = 0; - for (ef=de->extra_fields; ef; ef=ef->next) { + for (ef = de->extra_fields; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) { if (i < ef_idx) { i++; @@ -143,13 +140,11 @@ zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_u zip_error_set(&za->error, ZIP_ER_NOENT, 0); return NULL; - } ZIP_EXTERN const zip_uint8_t * -zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_uint16_t *lenp, zip_flags_t flags) -{ +zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_uint16_t *lenp, zip_flags_t flags) { zip_dirent_t *de; if ((flags & ZIP_EF_BOTH) == 0) { @@ -157,7 +152,7 @@ zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, return NULL; } - if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL) return NULL; if (flags & ZIP_FL_LOCAL) @@ -169,8 +164,7 @@ zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, ZIP_EXTERN zip_int16_t -zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) -{ +zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) { zip_dirent_t *de; zip_extra_field_t *ef; zip_uint16_t n; @@ -180,7 +174,7 @@ zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) return -1; } - if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL) return -1; if (flags & ZIP_FL_LOCAL) @@ -188,7 +182,7 @@ zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) return -1; n = 0; - for (ef=de->extra_fields; ef; ef=ef->next) + for (ef = de->extra_fields; ef; ef = ef->next) if (ef->flags & flags & ZIP_EF_BOTH) n++; @@ -197,8 +191,7 @@ zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) ZIP_EXTERN zip_int16_t -zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags) -{ +zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags) { zip_dirent_t *de; zip_extra_field_t *ef; zip_uint16_t n; @@ -208,7 +201,7 @@ zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i return -1; } - if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL) return -1; if (flags & ZIP_FL_LOCAL) @@ -216,7 +209,7 @@ zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i return -1; n = 0; - for (ef=de->extra_fields; ef; ef=ef->next) + for (ef = de->extra_fields; ef; ef = ef->next) if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) n++; @@ -225,8 +218,7 @@ zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_i ZIP_EXTERN int -zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags) -{ +zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags) { zip_dirent_t *de; zip_uint16_t ls, cs; zip_extra_field_t *ef, *ef_prev, *ef_new; @@ -239,20 +231,20 @@ zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_ui if (_zip_get_dirent(za, idx, 0, NULL) == NULL) return -1; - + if (ZIP_IS_RDONLY(za)) { zip_error_set(&za->error, ZIP_ER_RDONLY, 0); return -1; } - + if (ZIP_EF_IS_INTERNAL(ef_id)) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } if (_zip_file_extra_field_prepare_for_change(za, idx) < 0) - return -1; - + return -1; + de = za->entry[idx].changes; ef = de->extra_fields; @@ -260,7 +252,7 @@ zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_ui i = 0; found = 0; - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) { if (i == ef_idx) { found = 1; @@ -294,8 +286,8 @@ zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_ui zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } - - if ((ef_new=_zip_ef_new(ef_id, len, data, flags)) == NULL) { + + if ((ef_new = _zip_ef_new(ef_id, len, data, flags)) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return -1; } @@ -314,7 +306,7 @@ zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_ui ef->flags &= ~(flags & ZIP_EF_BOTH); ef_new->next = ef->next; ef->next = ef_new; - } + } } else if (ef_prev) { ef_new->next = ef_prev->next; @@ -322,45 +314,42 @@ zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_ui } else de->extra_fields = ef_new; - + return 0; } - int -_zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx) -{ +_zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx) { zip_entry_t *e; - + if (idx >= za->nentry) { - zip_error_set(&za->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; } - - e = za->entry+idx; - + + e = za->entry + idx; + if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD)) - return 0; + return 0; if (e->orig) { if (_zip_read_local_ef(za, idx) < 0) return -1; } - + if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } } - + if (e->orig && e->orig->extra_fields) { - if ((e->changes->extra_fields=_zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL) + if ((e->changes->extra_fields = _zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL) return -1; } e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD; - + return 0; } - diff --git a/lib/zip_fclose.c b/lib/zip_fclose.c index 25a201b..be98cc0 100644 --- a/lib/zip_fclose.c +++ b/lib/zip_fclose.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,10 +38,9 @@ ZIP_EXTERN int -zip_fclose(zip_file_t *zf) -{ +zip_fclose(zip_file_t *zf) { int ret; - + if (zf->src) zip_source_free(zf->src); diff --git a/lib/zip_fdopen.c b/lib/zip_fdopen.c index 3fd771f..b1fc22b 100644 --- a/lib/zip_fdopen.c +++ b/lib/zip_fdopen.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,28 +39,27 @@ ZIP_EXTERN zip_t * -zip_fdopen(int fd_orig, int _flags, int *zep) -{ +zip_fdopen(int fd_orig, int _flags, int *zep) { int fd; FILE *fp; zip_t *za; zip_source_t *src; struct zip_error error; - if (_flags < 0 || (_flags & ~(ZIP_CHECKCONS|ZIP_RDONLY))) { + if (_flags < 0 || (_flags & ~(ZIP_CHECKCONS | ZIP_RDONLY))) { _zip_set_open_error(zep, NULL, ZIP_ER_INVAL); - return NULL; + return NULL; } - + /* We dup() here to avoid messing with the passed in fd. We could not restore it to the original state in case of error. */ - if ((fd=dup(fd_orig)) < 0) { + if ((fd = dup(fd_orig)) < 0) { _zip_set_open_error(zep, NULL, ZIP_ER_OPEN); return NULL; } - if ((fp=fdopen(fd, "rb")) == NULL) { + if ((fp = fdopen(fd, "rb")) == NULL) { close(fd); _zip_set_open_error(zep, NULL, ZIP_ER_OPEN); return NULL; diff --git a/lib/zip_file_add.c b/lib/zip_file_add.c index 9944c0f..ec8ef06 100644 --- a/lib/zip_file_add.c +++ b/lib/zip_file_add.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,18 +36,17 @@ /* NOTE: Return type is signed so we can return -1 on error. - The index can not be larger than ZIP_INT64_MAX since the size - of the central directory cannot be larger than - ZIP_UINT64_MAX, and each entry is larger than 2 bytes. + The index can not be larger than ZIP_INT64_MAX since the size + of the central directory cannot be larger than + ZIP_UINT64_MAX, and each entry is larger than 2 bytes. */ ZIP_EXTERN zip_int64_t -zip_file_add(zip_t *za, const char *name, zip_source_t *source, zip_flags_t flags) -{ +zip_file_add(zip_t *za, const char *name, zip_source_t *source, zip_flags_t flags) { if (name == NULL || source == NULL) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } - + return _zip_file_replace(za, ZIP_UINT64_MAX, name, source, flags); } diff --git a/lib/zip_file_error_clear.c b/lib/zip_file_error_clear.c index be45498..666beca 100644 --- a/lib/zip_file_error_clear.c +++ b/lib/zip_file_error_clear.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN void -zip_file_error_clear(zip_file_t *zf) -{ +zip_file_error_clear(zip_file_t *zf) { if (zf == NULL) return; diff --git a/lib/zip_file_error_get.c b/lib/zip_file_error_get.c index be764fd..d7e0191 100644 --- a/lib/zip_file_error_get.c +++ b/lib/zip_file_error_get.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,7 +36,6 @@ ZIP_EXTERN void -zip_file_error_get(zip_file_t *zf, int *zep, int *sep) -{ +zip_file_error_get(zip_file_t *zf, int *zep, int *sep) { _zip_error_get(&zf->error, zep, sep); } diff --git a/lib/zip_file_get_comment.c b/lib/zip_file_get_comment.c index 55e7dc2..478f3d7 100644 --- a/lib/zip_file_get_comment.c +++ b/lib/zip_file_get_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,16 +37,15 @@ /* lenp is 32 bit because converted comment can be longer than ZIP_UINT16_MAX */ ZIP_EXTERN const char * -zip_file_get_comment(zip_t *za, zip_uint64_t idx, zip_uint32_t *lenp, zip_flags_t flags) -{ +zip_file_get_comment(zip_t *za, zip_uint64_t idx, zip_uint32_t *lenp, zip_flags_t flags) { zip_dirent_t *de; zip_uint32_t len; const zip_uint8_t *str; - if ((de=_zip_get_dirent(za, idx, flags, NULL)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, NULL)) == NULL) return NULL; - if ((str=_zip_string_get(de->comment, &len, flags, &za->error)) == NULL) + if ((str = _zip_string_get(de->comment, &len, flags, &za->error)) == NULL) return NULL; if (lenp) diff --git a/lib/zip_file_get_external_attributes.c b/lib/zip_file_get_external_attributes.c index b6526cf..e3ede74 100644 --- a/lib/zip_file_get_external_attributes.c +++ b/lib/zip_file_get_external_attributes.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,11 +34,10 @@ #include "zipint.h" int -zip_file_get_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t *opsys, zip_uint32_t *attributes) -{ +zip_file_get_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t *opsys, zip_uint32_t *attributes) { zip_dirent_t *de; - if ((de=_zip_get_dirent(za, idx, flags, NULL)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, NULL)) == NULL) return -1; if (opsys) diff --git a/lib/zip_file_get_offset.c b/lib/zip_file_get_offset.c index 16becaf..877c179 100644 --- a/lib/zip_file_get_offset.c +++ b/lib/zip_file_get_offset.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,8 +35,8 @@ #include #include #include -#include #include +#include #include "zipint.h" @@ -48,14 +48,13 @@ */ zip_uint64_t -_zip_file_get_offset(const zip_t *za, zip_uint64_t idx, zip_error_t *error) -{ +_zip_file_get_offset(const zip_t *za, zip_uint64_t idx, zip_error_t *error) { zip_uint64_t offset; zip_int32_t size; if (za->entry[idx].orig == NULL) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - return 0; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + return 0; } offset = za->entry[idx].orig->offset; @@ -66,56 +65,55 @@ _zip_file_get_offset(const zip_t *za, zip_uint64_t idx, zip_error_t *error) } /* TODO: cache? */ - if ((size=_zip_dirent_size(za->src, ZIP_EF_LOCAL, error)) < 0) + if ((size = _zip_dirent_size(za->src, ZIP_EF_LOCAL, error)) < 0) return 0; - if (offset+(zip_uint32_t)size > ZIP_INT64_MAX) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return 0; + if (offset + (zip_uint32_t)size > ZIP_INT64_MAX) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return 0; } - + return offset + (zip_uint32_t)size; } zip_uint64_t -_zip_file_get_end(const zip_t *za, zip_uint64_t index, zip_error_t *error) -{ +_zip_file_get_end(const zip_t *za, zip_uint64_t index, zip_error_t *error) { zip_uint64_t offset; zip_dirent_t *entry; if ((offset = _zip_file_get_offset(za, index, error)) == 0) { - return 0; + return 0; } entry = za->entry[index].orig; if (offset + entry->comp_size < offset || offset + entry->comp_size > ZIP_INT64_MAX) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return 0; + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return 0; } offset += entry->comp_size; if (entry->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) { - zip_uint8_t buf[4]; - if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, za->src); - return 0; - } - if (zip_source_read(za->src, buf, 4) != 4) { - _zip_error_set_from_source(error, za->src); - return 0; - } - if (memcmp(buf, DATADES_MAGIC, 4) == 0) { - offset += 4; - } - offset += 12; - if (_zip_dirent_needs_zip64(entry, 0)) { - offset += 8; - } - if (offset > ZIP_INT64_MAX) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return 0; - } + zip_uint8_t buf[4]; + if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, za->src); + return 0; + } + if (zip_source_read(za->src, buf, 4) != 4) { + _zip_error_set_from_source(error, za->src); + return 0; + } + if (memcmp(buf, DATADES_MAGIC, 4) == 0) { + offset += 4; + } + offset += 12; + if (_zip_dirent_needs_zip64(entry, 0)) { + offset += 8; + } + if (offset > ZIP_INT64_MAX) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return 0; + } } return offset; diff --git a/lib/zip_file_rename.c b/lib/zip_file_rename.c index 4400938..1eb7fac 100644 --- a/lib/zip_file_rename.c +++ b/lib/zip_file_rename.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,11 +38,10 @@ ZIP_EXTERN int -zip_file_rename(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) -{ +zip_file_rename(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) { const char *old_name; int old_is_dir, new_is_dir; - + if (idx >= za->nentry || (name != NULL && strlen(name) > ZIP_UINT16_MAX)) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; @@ -53,11 +52,11 @@ zip_file_rename(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags return -1; } - if ((old_name=zip_get_name(za, idx, 0)) == NULL) + if ((old_name = zip_get_name(za, idx, 0)) == NULL) return -1; - - new_is_dir = (name != NULL && name[strlen(name)-1] == '/'); - old_is_dir = (old_name[strlen(old_name)-1] == '/'); + + new_is_dir = (name != NULL && name[strlen(name) - 1] == '/'); + old_is_dir = (old_name[strlen(old_name) - 1] == '/'); if (new_is_dir != old_is_dir) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); diff --git a/lib/zip_file_replace.c b/lib/zip_file_replace.c index e430efa..e42f5cd 100644 --- a/lib/zip_file_replace.c +++ b/lib/zip_file_replace.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_file_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source, zip_flags_t flags) -{ +zip_file_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source, zip_flags_t flags) { if (idx >= za->nentry || source == NULL) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; @@ -50,14 +49,12 @@ zip_file_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source, zip_flags_t } - /* NOTE: Signed due to -1 on error. See zip_add.c for more details. */ zip_int64_t -_zip_file_replace(zip_t *za, zip_uint64_t idx, const char *name, zip_source_t *source, zip_flags_t flags) -{ +_zip_file_replace(zip_t *za, zip_uint64_t idx, const char *name, zip_source_t *source, zip_flags_t flags) { zip_uint64_t za_nentry_prev; - + if (ZIP_IS_RDONLY(za)) { zip_error_set(&za->error, ZIP_ER_RDONLY, 0); return -1; @@ -66,21 +63,21 @@ _zip_file_replace(zip_t *za, zip_uint64_t idx, const char *name, zip_source_t *s za_nentry_prev = za->nentry; if (idx == ZIP_UINT64_MAX) { zip_int64_t i = -1; - + if (flags & ZIP_FL_OVERWRITE) i = _zip_name_locate(za, name, flags, NULL); if (i == -1) { /* create and use new entry, used by zip_add */ - if ((i=_zip_add_entry(za)) < 0) + if ((i = _zip_add_entry(za)) < 0) return -1; } idx = (zip_uint64_t)i; } - + if (name && _zip_set_name(za, idx, name, flags) != 0) { if (za->nentry != za_nentry_prev) { - _zip_entry_finalize(za->entry+idx); + _zip_entry_finalize(za->entry + idx); za->nentry = za_nentry_prev; } return -1; @@ -88,20 +85,20 @@ _zip_file_replace(zip_t *za, zip_uint64_t idx, const char *name, zip_source_t *s /* does not change any name related data, so we can do it here; * needed for a double add of the same file name */ - _zip_unchange_data(za->entry+idx); + _zip_unchange_data(za->entry + idx); if (za->entry[idx].orig != NULL && (za->entry[idx].changes == NULL || (za->entry[idx].changes->changed & ZIP_DIRENT_COMP_METHOD) == 0)) { - if (za->entry[idx].changes == NULL) { - if ((za->entry[idx].changes=_zip_dirent_clone(za->entry[idx].orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - } - - za->entry[idx].changes->comp_method = ZIP_CM_REPLACED_DEFAULT; - za->entry[idx].changes->changed |= ZIP_DIRENT_COMP_METHOD; + if (za->entry[idx].changes == NULL) { + if ((za->entry[idx].changes = _zip_dirent_clone(za->entry[idx].orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + } + + za->entry[idx].changes->comp_method = ZIP_CM_REPLACED_DEFAULT; + za->entry[idx].changes->changed |= ZIP_DIRENT_COMP_METHOD; } - + za->entry[idx].source = source; return (zip_int64_t)idx; diff --git a/lib/zip_file_set_comment.c b/lib/zip_file_set_comment.c index e455fbd..964486b 100644 --- a/lib/zip_file_set_comment.c +++ b/lib/zip_file_set_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,9 +38,7 @@ ZIP_EXTERN int -zip_file_set_comment(zip_t *za, zip_uint64_t idx, - const char *comment, zip_uint16_t len, zip_flags_t flags) -{ +zip_file_set_comment(zip_t *za, zip_uint64_t idx, const char *comment, zip_uint16_t len, zip_flags_t flags) { zip_entry_t *e; zip_string_t *cstr; int changed; @@ -59,7 +57,7 @@ zip_file_set_comment(zip_t *za, zip_uint64_t idx, } if (len > 0) { - if ((cstr=_zip_string_new((const zip_uint8_t *)comment, len, flags, &za->error)) == NULL) + if ((cstr = _zip_string_new((const zip_uint8_t *)comment, len, flags, &za->error)) == NULL) return -1; if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(cstr, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED) cstr->encoding = ZIP_ENCODING_UTF8_KNOWN; @@ -67,7 +65,7 @@ zip_file_set_comment(zip_t *za, zip_uint64_t idx, else cstr = NULL; - e = za->entry+idx; + e = za->entry + idx; if (e->changes) { _zip_string_free(e->changes->comment); @@ -79,17 +77,17 @@ zip_file_set_comment(zip_t *za, zip_uint64_t idx, changed = !_zip_string_equal(e->orig->comment, cstr); else changed = (cstr != NULL); - + if (changed) { - if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + if (e->changes == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); _zip_string_free(cstr); - return -1; - } - } - e->changes->comment = cstr; - e->changes->changed |= ZIP_DIRENT_COMMENT; + return -1; + } + } + e->changes->comment = cstr; + e->changes->changed |= ZIP_DIRENT_COMMENT; } else { _zip_string_free(cstr); diff --git a/lib/zip_file_set_encryption.c b/lib/zip_file_set_encryption.c index 6e16135..b0cbe5c 100644 --- a/lib/zip_file_set_encryption.c +++ b/lib/zip_file_set_encryption.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ #include ZIP_EXTERN int -zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const char *password) -{ +zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const char *password) { zip_entry_t *e; zip_uint16_t old_method; @@ -58,10 +57,10 @@ zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const return -1; } - e = za->entry+idx; - + e = za->entry + idx; + old_method = (e->orig == NULL ? ZIP_EM_NONE : e->orig->encryption_method); - + if (method == old_method && password == NULL) { if (e->changes) { if (e->changes->changed & ZIP_DIRENT_PASSWORD) { @@ -69,7 +68,7 @@ zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const free(e->changes->password); e->changes->password = (e->orig == NULL ? NULL : e->orig->password); } - e->changes->changed &= ~(ZIP_DIRENT_ENCRYPTION_METHOD|ZIP_DIRENT_PASSWORD); + e->changes->changed &= ~(ZIP_DIRENT_ENCRYPTION_METHOD | ZIP_DIRENT_PASSWORD); if (e->changes->changed == 0) { _zip_dirent_free(e->changes); e->changes = NULL; @@ -85,20 +84,20 @@ zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const return -1; } } - - if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { + + if (e->changes == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { if (our_password) { _zip_crypto_clear(our_password, strlen(our_password)); } free(our_password); - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - } + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + } - e->changes->encryption_method = method; - e->changes->changed |= ZIP_DIRENT_ENCRYPTION_METHOD; + e->changes->encryption_method = method; + e->changes->changed |= ZIP_DIRENT_ENCRYPTION_METHOD; if (password) { e->changes->password = our_password; e->changes->changed |= ZIP_DIRENT_PASSWORD; @@ -112,6 +111,6 @@ zip_file_set_encryption(zip_t *za, zip_uint64_t idx, zip_uint16_t method, const } } } - + return 0; } diff --git a/lib/zip_file_set_external_attributes.c b/lib/zip_file_set_external_attributes.c index b772c31..cd46e88 100644 --- a/lib/zip_file_set_external_attributes.c +++ b/lib/zip_file_set_external_attributes.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,8 +34,7 @@ #include "zipint.h" ZIP_EXTERN int -zip_file_set_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t opsys, zip_uint32_t attributes) -{ +zip_file_set_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t opsys, zip_uint32_t attributes) { zip_entry_t *e; int changed; zip_uint8_t unchanged_opsys; @@ -49,23 +48,23 @@ zip_file_set_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, return -1; } - e = za->entry+idx; + e = za->entry + idx; - unchanged_opsys = (e->orig ? (zip_uint8_t)(e->orig->version_madeby>>8) : (zip_uint8_t)ZIP_OPSYS_DEFAULT); + unchanged_opsys = (e->orig ? (zip_uint8_t)(e->orig->version_madeby >> 8) : (zip_uint8_t)ZIP_OPSYS_DEFAULT); unchanged_attributes = e->orig ? e->orig->ext_attrib : ZIP_EXT_ATTRIB_DEFAULT; changed = (opsys != unchanged_opsys || attributes != unchanged_attributes); if (changed) { - if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - } - e->changes->version_madeby = (zip_uint16_t)((opsys << 8) | (e->changes->version_madeby & 0xff)); + if (e->changes == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + } + e->changes->version_madeby = (zip_uint16_t)((opsys << 8) | (e->changes->version_madeby & 0xff)); e->changes->ext_attrib = attributes; - e->changes->changed |= ZIP_DIRENT_ATTRIBUTES; + e->changes->changed |= ZIP_DIRENT_ATTRIBUTES; } else if (e->changes) { e->changes->changed &= ~ZIP_DIRENT_ATTRIBUTES; diff --git a/lib/zip_file_set_mtime.c b/lib/zip_file_set_mtime.c index 0cdd31a..15316d3 100644 --- a/lib/zip_file_set_mtime.c +++ b/lib/zip_file_set_mtime.c @@ -1,10 +1,10 @@ /* zip_file_set_mtime.c -- set modification time of entry. Copyright (C) 2014 Dieter Baron and Thomas Klausner - + This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -33,42 +33,42 @@ #include "zipint.h" -ZIP_EXTERN int zip_file_set_mtime(zip_t *za, zip_uint64_t idx, time_t mtime, zip_flags_t flags) -{ +ZIP_EXTERN int +zip_file_set_mtime(zip_t *za, zip_uint64_t idx, time_t mtime, zip_flags_t flags) { zip_entry_t *e; int changed; - + if (_zip_get_dirent(za, idx, 0, NULL) == NULL) - return -1; - + return -1; + if (ZIP_IS_RDONLY(za)) { - zip_error_set(&za->error, ZIP_ER_RDONLY, 0); - return -1; + zip_error_set(&za->error, ZIP_ER_RDONLY, 0); + return -1; } - - e = za->entry+idx; + + e = za->entry + idx; changed = e->orig == NULL || mtime != e->orig->last_mod; - + if (changed) { - if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - } - e->changes->last_mod = mtime; - e->changes->changed |= ZIP_DIRENT_LAST_MOD; + if (e->changes == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + } + e->changes->last_mod = mtime; + e->changes->changed |= ZIP_DIRENT_LAST_MOD; } else { - if (e->changes) { - e->changes->changed &= ~ZIP_DIRENT_LAST_MOD; - if (e->changes->changed == 0) { + if (e->changes) { + e->changes->changed &= ~ZIP_DIRENT_LAST_MOD; + if (e->changes->changed == 0) { _zip_dirent_free(e->changes); - e->changes = NULL; - } - } + e->changes = NULL; + } + } } - + return 0; } diff --git a/lib/zip_file_strerror.c b/lib/zip_file_strerror.c index 8366f1e..fd4008a 100644 --- a/lib/zip_file_strerror.c +++ b/lib/zip_file_strerror.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,7 +36,6 @@ ZIP_EXTERN const char * -zip_file_strerror(zip_file_t *zf) -{ +zip_file_strerror(zip_file_t *zf) { return zip_error_strerror(&zf->error); } diff --git a/lib/zip_filerange_crc.c b/lib/zip_filerange_crc.c index 775af9a..6c4be27 100644 --- a/lib/zip_filerange_crc.c +++ b/lib/zip_filerange_crc.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,10 +37,8 @@ #include "zipint.h" - int -_zip_filerange_crc(zip_source_t *src, zip_uint64_t start, zip_uint64_t len, uLong *crcp, zip_error_t *error) -{ +_zip_filerange_crc(zip_source_t *src, zip_uint64_t start, zip_uint64_t len, uLong *crcp, zip_error_t *error) { Bytef buf[BUFSIZE]; zip_int64_t n; @@ -55,7 +53,7 @@ _zip_filerange_crc(zip_source_t *src, zip_uint64_t start, zip_uint64_t len, uLon _zip_error_set_from_source(error, src); return -1; } - + while (len > 0) { n = (zip_int64_t)(len > BUFSIZE ? BUFSIZE : len); if ((n = zip_source_read(src, buf, (zip_uint64_t)n)) < 0) { diff --git a/lib/zip_fopen.c b/lib/zip_fopen.c index 3adb5de..cb45cc2 100644 --- a/lib/zip_fopen.c +++ b/lib/zip_fopen.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,11 +36,10 @@ ZIP_EXTERN zip_file_t * -zip_fopen(zip_t *za, const char *fname, zip_flags_t flags) -{ +zip_fopen(zip_t *za, const char *fname, zip_flags_t flags) { zip_int64_t idx; - if ((idx=zip_name_locate(za, fname, flags)) < 0) + if ((idx = zip_name_locate(za, fname, flags)) < 0) return NULL; return zip_fopen_index_encrypted(za, (zip_uint64_t)idx, flags, za->default_password); diff --git a/lib/zip_fopen_encrypted.c b/lib/zip_fopen_encrypted.c index 5eaf2b0..abcf6ac 100644 --- a/lib/zip_fopen_encrypted.c +++ b/lib/zip_fopen_encrypted.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,11 +36,10 @@ ZIP_EXTERN zip_file_t * -zip_fopen_encrypted(zip_t *za, const char *fname, zip_flags_t flags, const char *password) -{ +zip_fopen_encrypted(zip_t *za, const char *fname, zip_flags_t flags, const char *password) { zip_int64_t idx; - if ((idx=zip_name_locate(za, fname, flags)) < 0) + if ((idx = zip_name_locate(za, fname, flags)) < 0) return NULL; return zip_fopen_index_encrypted(za, (zip_uint64_t)idx, flags, password); diff --git a/lib/zip_fopen_index.c b/lib/zip_fopen_index.c index 0fb150b..7dcf1f7 100644 --- a/lib/zip_fopen_index.c +++ b/lib/zip_fopen_index.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,7 +39,6 @@ ZIP_EXTERN zip_file_t * -zip_fopen_index(zip_t *za, zip_uint64_t index, zip_flags_t flags) -{ +zip_fopen_index(zip_t *za, zip_uint64_t index, zip_flags_t flags) { return zip_fopen_index_encrypted(za, index, flags, za->default_password); } diff --git a/lib/zip_fopen_index_encrypted.c b/lib/zip_fopen_index_encrypted.c index 4c32e02..6b32008 100644 --- a/lib/zip_fopen_index_encrypted.c +++ b/lib/zip_fopen_index_encrypted.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,13 +41,11 @@ static zip_file_t *_zip_file_new(zip_t *za); ZIP_EXTERN zip_file_t * -zip_fopen_index_encrypted(zip_t *za, zip_uint64_t index, zip_flags_t flags, - const char *password) -{ +zip_fopen_index_encrypted(zip_t *za, zip_uint64_t index, zip_flags_t flags, const char *password) { zip_file_t *zf; zip_source_t *src; - if ((src=_zip_source_zip_new(za, za, index, flags, 0, 0, password)) == NULL) + if ((src = _zip_source_zip_new(za, za, index, flags, 0, 0, password)) == NULL) return NULL; if (zip_source_open(src) < 0) { @@ -56,7 +54,7 @@ zip_fopen_index_encrypted(zip_t *za, zip_uint64_t index, zip_flags_t flags, return NULL; } - if ((zf=_zip_file_new(za)) == NULL) { + if ((zf = _zip_file_new(za)) == NULL) { zip_source_free(src); return NULL; } @@ -68,11 +66,10 @@ zip_fopen_index_encrypted(zip_t *za, zip_uint64_t index, zip_flags_t flags, static zip_file_t * -_zip_file_new(zip_t *za) -{ +_zip_file_new(zip_t *za) { zip_file_t *zf; - if ((zf=(zip_file_t *)malloc(sizeof(struct zip_file))) == NULL) { + if ((zf = (zip_file_t *)malloc(sizeof(struct zip_file))) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return NULL; } diff --git a/lib/zip_fread.c b/lib/zip_fread.c index 9c1cbe0..be46cf0 100644 --- a/lib/zip_fread.c +++ b/lib/zip_fread.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN zip_int64_t -zip_fread(zip_file_t *zf, void *outbuf, zip_uint64_t toread) -{ +zip_fread(zip_file_t *zf, void *outbuf, zip_uint64_t toread) { zip_int64_t n; if (!zf) @@ -54,7 +53,7 @@ zip_fread(zip_file_t *zf, void *outbuf, zip_uint64_t toread) if ((zf->eof) || (toread == 0)) return 0; - if ((n=zip_source_read(zf->src, outbuf, toread)) < 0) { + if ((n = zip_source_read(zf->src, outbuf, toread)) < 0) { _zip_error_set_from_source(&zf->error, zf->src); return -1; } diff --git a/lib/zip_fseek.c b/lib/zip_fseek.c index a16af1a..c939ebf 100644 --- a/lib/zip_fseek.c +++ b/lib/zip_fseek.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,9 +36,7 @@ #include ZIP_EXTERN zip_int8_t -zip_fseek(zip_file_t *zf, zip_int64_t offset, int whence) -{ - +zip_fseek(zip_file_t *zf, zip_int64_t offset, int whence) { if (!zf) return -1; diff --git a/lib/zip_ftell.c b/lib/zip_ftell.c index abbf296..ff062df 100644 --- a/lib/zip_ftell.c +++ b/lib/zip_ftell.c @@ -36,8 +36,7 @@ #include ZIP_EXTERN zip_int64_t -zip_ftell(zip_file_t *zf) -{ +zip_ftell(zip_file_t *zf) { zip_int64_t res; if (!zf) diff --git a/lib/zip_get_archive_comment.c b/lib/zip_get_archive_comment.c index 78f8ca0..a3d5a19 100644 --- a/lib/zip_get_archive_comment.c +++ b/lib/zip_get_archive_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ ZIP_EXTERN const char * -zip_get_archive_comment(zip_t *za, int *lenp, zip_flags_t flags) -{ +zip_get_archive_comment(zip_t *za, int *lenp, zip_flags_t flags) { zip_string_t *comment; zip_uint32_t len; const zip_uint8_t *str; @@ -49,7 +48,7 @@ zip_get_archive_comment(zip_t *za, int *lenp, zip_flags_t flags) else comment = za->comment_changes; - if ((str=_zip_string_get(comment, &len, flags, &za->error)) == NULL) + if ((str = _zip_string_get(comment, &len, flags, &za->error)) == NULL) return NULL; if (lenp) diff --git a/lib/zip_get_archive_flag.c b/lib/zip_get_archive_flag.c index bffe10c..67fe4f8 100644 --- a/lib/zip_get_archive_flag.c +++ b/lib/zip_get_archive_flag.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_get_archive_flag(zip_t *za, zip_flags_t flag, zip_flags_t flags) -{ +zip_get_archive_flag(zip_t *za, zip_flags_t flag, zip_flags_t flags) { unsigned int fl; fl = (flags & ZIP_FL_UNCHANGED) ? za->flags : za->ch_flags; diff --git a/lib/zip_get_encryption_implementation.c b/lib/zip_get_encryption_implementation.c index f8af7b0..ba459d4 100644 --- a/lib/zip_get_encryption_implementation.c +++ b/lib/zip_get_encryption_implementation.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ zip_encryption_implementation -_zip_get_encryption_implementation(zip_uint16_t em, int operation) -{ +_zip_get_encryption_implementation(zip_uint16_t em, int operation) { switch (em) { case ZIP_EM_TRAD_PKWARE: if (operation == ZIP_CODEC_ENCODE) { @@ -45,10 +44,12 @@ _zip_get_encryption_implementation(zip_uint16_t em, int operation) } return zip_source_pkware; +#if defined(HAVE_CRYPTO) case ZIP_EM_AES_128: case ZIP_EM_AES_192: case ZIP_EM_AES_256: return operation == ZIP_CODEC_DECODE ? zip_source_winzip_aes_decode : zip_source_winzip_aes_encode; +#endif default: return NULL; diff --git a/lib/zip_get_file_comment.c b/lib/zip_get_file_comment.c index d5f50bf..44e5917 100644 --- a/lib/zip_get_file_comment.c +++ b/lib/zip_get_file_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,12 +37,11 @@ ZIP_EXTERN const char * -zip_get_file_comment(zip_t *za, zip_uint64_t idx, int *lenp, int flags) -{ +zip_get_file_comment(zip_t *za, zip_uint64_t idx, int *lenp, int flags) { zip_uint32_t len; const char *s; - if ((s=zip_file_get_comment(za, idx, &len, (zip_flags_t)flags)) != NULL) { + if ((s = zip_file_get_comment(za, idx, &len, (zip_flags_t)flags)) != NULL) { if (lenp) *lenp = (int)len; } diff --git a/lib/zip_get_name.c b/lib/zip_get_name.c index d29e636..d42f14a 100644 --- a/lib/zip_get_name.c +++ b/lib/zip_get_name.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,22 +38,20 @@ ZIP_EXTERN const char * -zip_get_name(zip_t *za, zip_uint64_t idx, zip_flags_t flags) -{ +zip_get_name(zip_t *za, zip_uint64_t idx, zip_flags_t flags) { return _zip_get_name(za, idx, flags, &za->error); } const char * -_zip_get_name(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error) -{ +_zip_get_name(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error) { zip_dirent_t *de; const zip_uint8_t *str; - if ((de=_zip_get_dirent(za, idx, flags, error)) == NULL) + if ((de = _zip_get_dirent(za, idx, flags, error)) == NULL) return NULL; - if ((str=_zip_string_get(de->filename, NULL, flags, error)) == NULL) + if ((str = _zip_string_get(de->filename, NULL, flags, error)) == NULL) return NULL; return (const char *)str; diff --git a/lib/zip_get_num_entries.c b/lib/zip_get_num_entries.c index c8644a4..78f30dc 100644 --- a/lib/zip_get_num_entries.c +++ b/lib/zip_get_num_entries.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN zip_int64_t -zip_get_num_entries(zip_t *za, zip_flags_t flags) -{ +zip_get_num_entries(zip_t *za, zip_flags_t flags) { zip_uint64_t n; if (za == NULL) @@ -45,7 +44,7 @@ zip_get_num_entries(zip_t *za, zip_flags_t flags) if (flags & ZIP_FL_UNCHANGED) { n = za->nentry; - while (n>0 && za->entry[n-1].orig == NULL) + while (n > 0 && za->entry[n - 1].orig == NULL) --n; return (zip_int64_t)n; } diff --git a/lib/zip_get_num_files.c b/lib/zip_get_num_files.c index cf96353..3ccf400 100644 --- a/lib/zip_get_num_files.c +++ b/lib/zip_get_num_files.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ ZIP_EXTERN int -zip_get_num_files(zip_t *za) -{ +zip_get_num_files(zip_t *za) { if (za == NULL) return -1; diff --git a/lib/zip_hash.c b/lib/zip_hash.c index 992e9d9..0d9e996 100644 --- a/lib/zip_hash.c +++ b/lib/zip_hash.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -31,9 +31,9 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "zipint.h" #include #include -#include "zipint.h" /* parameter for the string hash function */ #define HASH_MULTIPLIER 33 @@ -65,8 +65,7 @@ struct zip_hash { /* free list of entries */ static void -free_list(zip_hash_entry_t *entry) -{ +free_list(zip_hash_entry_t *entry) { while (entry != NULL) { zip_hash_entry_t *next = entry->next; free(entry); @@ -77,8 +76,7 @@ free_list(zip_hash_entry_t *entry) /* compute hash of string, full 32 bit value */ static zip_uint32_t -hash_string(const zip_uint8_t *name) -{ +hash_string(const zip_uint8_t *name) { zip_uint64_t value = HASH_START; if (name == NULL) { @@ -96,15 +94,14 @@ hash_string(const zip_uint8_t *name) /* resize hash table; new_size must be a power of 2, can be larger or smaller than current size */ static bool -hash_resize(zip_hash_t *hash, zip_uint32_t new_size, zip_error_t *error) -{ +hash_resize(zip_hash_t *hash, zip_uint32_t new_size, zip_error_t *error) { zip_hash_entry_t **new_table; if (new_size == hash->table_size) { return true; } - if ((new_table = (zip_hash_entry_t**)calloc(new_size, sizeof(zip_hash_entry_t *))) == NULL) { + if ((new_table = (zip_hash_entry_t **)calloc(new_size, sizeof(zip_hash_entry_t *))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } @@ -167,26 +164,24 @@ size_for_capacity(zip_uint64_t capacity) { zip_hash_t * -_zip_hash_new(zip_error_t *error) -{ +_zip_hash_new(zip_error_t *error) { zip_hash_t *hash; if ((hash = (zip_hash_t *)malloc(sizeof(zip_hash_t))) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } hash->table_size = 0; hash->nentries = 0; hash->table = NULL; - + return hash; } void -_zip_hash_free(zip_hash_t *hash) -{ +_zip_hash_free(zip_hash_t *hash) { zip_uint32_t i; if (hash == NULL) { @@ -194,7 +189,7 @@ _zip_hash_free(zip_hash_t *hash) } if (hash->table != NULL) { - for (i=0; itable_size; i++) { + for (i = 0; i < hash->table_size; i++) { if (hash->table[i] != NULL) { free_list(hash->table[i]); } @@ -207,8 +202,7 @@ _zip_hash_free(zip_hash_t *hash) /* insert into hash, return error on existence or memory issues */ bool -_zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error) -{ +_zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error) { zip_uint32_t hash_value, table_index; zip_hash_entry_t *entry; @@ -227,28 +221,28 @@ _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip table_index = hash_value % hash->table_size; for (entry = hash->table[table_index]; entry != NULL; entry = entry->next) { - if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) { - if (((flags & ZIP_FL_UNCHANGED) && entry->orig_index != -1) || entry->current_index != -1) { - zip_error_set(error, ZIP_ER_EXISTS, 0); - return false; - } - else { - break; - } - } + if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) { + if (((flags & ZIP_FL_UNCHANGED) && entry->orig_index != -1) || entry->current_index != -1) { + zip_error_set(error, ZIP_ER_EXISTS, 0); + return false; + } + else { + break; + } + } } if (entry == NULL) { - if ((entry = (zip_hash_entry_t *)malloc(sizeof(zip_hash_entry_t))) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return false; - } - entry->name = name; - entry->next = hash->table[table_index]; - hash->table[table_index] = entry; - entry->hash_value = hash_value; - entry->orig_index = -1; - hash->nentries++; + if ((entry = (zip_hash_entry_t *)malloc(sizeof(zip_hash_entry_t))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return false; + } + entry->name = name; + entry->next = hash->table[table_index]; + hash->table[table_index] = entry; + entry->hash_value = hash_value; + entry->orig_index = -1; + hash->nentries++; if (hash->nentries > hash->table_size * HASH_MAX_FILL && hash->table_size < HASH_MAX_SIZE) { if (!hash_resize(hash, hash->table_size * 2, error)) { return false; @@ -257,7 +251,7 @@ _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip } if (flags & ZIP_FL_UNCHANGED) { - entry->orig_index = (zip_int64_t)index; + entry->orig_index = (zip_int64_t)index; } entry->current_index = (zip_int64_t)index; @@ -267,8 +261,7 @@ _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip /* remove entry from hash, error if not found */ bool -_zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) -{ +_zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) { zip_uint32_t hash_value, index; zip_hash_entry_t *entry, *previous; @@ -316,14 +309,13 @@ _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) /* find value for entry in hash, -1 if not found */ zip_int64_t -_zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error) -{ +_zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error) { zip_uint32_t hash_value, index; zip_hash_entry_t *entry; if (hash == NULL || name == NULL) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(error, ZIP_ER_INVAL, 0); + return -1; } if (hash->nentries > 0) { @@ -352,8 +344,7 @@ _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, z bool -_zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t *error) -{ +_zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t *error) { zip_uint32_t new_size; if (capacity == 0) { @@ -367,7 +358,7 @@ _zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t } if (!hash_resize(hash, new_size, error)) { - return false; + return false; } return true; @@ -375,35 +366,34 @@ _zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t bool -_zip_hash_revert(zip_hash_t *hash, zip_error_t *error) -{ +_zip_hash_revert(zip_hash_t *hash, zip_error_t *error) { zip_uint32_t i; zip_hash_entry_t *entry, *previous; - + for (i = 0; i < hash->table_size; i++) { - previous = NULL; - entry = hash->table[i]; - while (entry) { - if (entry->orig_index == -1) { - zip_hash_entry_t *p; - if (previous) { - previous->next = entry->next; - } - else { - hash->table[i] = entry->next; - } - p = entry; - entry = entry->next; - /* previous does not change */ - free(p); - hash->nentries--; - } - else { - entry->current_index = entry->orig_index; - previous = entry; - entry = entry->next; - } - } + previous = NULL; + entry = hash->table[i]; + while (entry) { + if (entry->orig_index == -1) { + zip_hash_entry_t *p; + if (previous) { + previous->next = entry->next; + } + else { + hash->table[i] = entry->next; + } + p = entry; + entry = entry->next; + /* previous does not change */ + free(p); + hash->nentries--; + } + else { + entry->current_index = entry->orig_index; + previous = entry; + entry = entry->next; + } + } } if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) { diff --git a/lib/zip_io_util.c b/lib/zip_io_util.c index 53a5dd2..77be8f0 100644 --- a/lib/zip_io_util.c +++ b/lib/zip_io_util.c @@ -1,10 +1,10 @@ /* zip_io_util.c -- I/O helper functions Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner - + This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,8 +37,7 @@ #include "zipint.h" int -_zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error) -{ +_zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error) { zip_int64_t n; if (length > ZIP_INT64_MAX) { @@ -61,10 +60,9 @@ _zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *e zip_uint8_t * -_zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error) -{ +_zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error) { zip_uint8_t *r; - + if (length == 0 && !nulp) { return NULL; } @@ -76,13 +74,13 @@ _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp } if (buffer) { - zip_uint8_t *data = _zip_buffer_get(buffer, length); - - if (data == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - free(r); - return NULL; - } + zip_uint8_t *data = _zip_buffer_get(buffer, length); + + if (data == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + free(r); + return NULL; + } memcpy(r, data, length); } else { @@ -96,7 +94,7 @@ _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp zip_uint8_t *o; /* replace any in-string NUL characters with spaces */ r[length] = 0; - for (o=r; osrc, data, length)) < 0) { - _zip_error_set_from_source(&za->error, za->src); - return -1; + _zip_error_set_from_source(&za->error, za->src); + return -1; } if ((zip_uint64_t)n != length) { - zip_error_set(&za->error, ZIP_ER_WRITE, EINTR); - return -1; + zip_error_set(&za->error, ZIP_ER_WRITE, EINTR); + return -1; } - + return 0; } diff --git a/lib/zip_libzip_version.c b/lib/zip_libzip_version.c index 44d5612..72f1025 100644 --- a/lib/zip_libzip_version.c +++ b/lib/zip_libzip_version.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,7 +36,6 @@ ZIP_EXTERN const char * -zip_libzip_version(void) -{ +zip_libzip_version(void) { return LIBZIP_VERSION; } diff --git a/lib/zip_memdup.c b/lib/zip_memdup.c index cc6d767..9d7949d 100644 --- a/lib/zip_memdup.c +++ b/lib/zip_memdup.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ void * -_zip_memdup(const void *mem, size_t len, zip_error_t *error) -{ +_zip_memdup(const void *mem, size_t len, zip_error_t *error) { void *ret; if (len == 0) diff --git a/lib/zip_name_locate.c b/lib/zip_name_locate.c index 706093f..6713cab 100644 --- a/lib/zip_name_locate.c +++ b/lib/zip_name_locate.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,15 +41,13 @@ ZIP_EXTERN zip_int64_t -zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags) -{ +zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags) { return _zip_name_locate(za, fname, flags, &za->error); } zip_int64_t -_zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *error) -{ +_zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *error) { int (*cmp)(const char *, const char *); const char *fn, *p; zip_uint64_t i; @@ -62,23 +60,23 @@ _zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *e return -1; } - if (flags & (ZIP_FL_NOCASE|ZIP_FL_NODIR|ZIP_FL_ENC_CP437)) { + if (flags & (ZIP_FL_NOCASE | ZIP_FL_NODIR | ZIP_FL_ENC_CP437)) { /* can't use hash table */ cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp; - for (i=0; inentry; i++) { + for (i = 0; i < za->nentry; i++) { fn = _zip_get_name(za, i, flags, error); - + /* newly added (partially filled) entry or error */ if (fn == NULL) continue; - + if (flags & ZIP_FL_NODIR) { p = strrchr(fn, '/'); if (p) - fn = p+1; + fn = p + 1; } - + if (cmp(fname, fn) == 0) { _zip_error_clear(error); return (zip_int64_t)i; diff --git a/lib/zip_new.c b/lib/zip_new.c index da841ff..19155f4 100644 --- a/lib/zip_new.c +++ b/lib/zip_new.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -42,8 +42,7 @@ the new struct. */ zip_t * -_zip_new(zip_error_t *error) -{ +_zip_new(zip_error_t *error) { zip_t *za; za = (zip_t *)malloc(sizeof(struct zip)); @@ -69,6 +68,6 @@ _zip_new(zip_error_t *error) za->nopen_source = za->nopen_source_alloc = 0; za->open_source = NULL; za->progress = NULL; - + return za; } diff --git a/lib/zip_open.c b/lib/zip_open.c index 6c62eef..1886c9d 100644 --- a/lib/zip_open.c +++ b/lib/zip_open.c @@ -32,11 +32,11 @@ */ -#include #include #include #include #include +#include #include "zipint.h" @@ -58,8 +58,7 @@ static zip_cdir_t *_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip ZIP_EXTERN zip_t * -zip_open(const char *fn, int _flags, int *zep) -{ +zip_open(const char *fn, int _flags, int *zep) { zip_t *za; zip_source_t *src; struct zip_error error; @@ -84,8 +83,7 @@ zip_open(const char *fn, int _flags, int *zep) ZIP_EXTERN zip_t * -zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) -{ +zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) { static zip_int64_t needed_support_read = -1; static zip_int64_t needed_support_write = -1; @@ -95,24 +93,24 @@ zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) if (_flags < 0 || src == NULL) { zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + return NULL; } flags = (unsigned int)_flags; supported = zip_source_supports(src); if (needed_support_read == -1) { - needed_support_read = zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_STAT, -1); - needed_support_write = zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1); + needed_support_read = zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_STAT, -1); + needed_support_write = zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1); } if ((supported & needed_support_read) != needed_support_read) { - zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); - return NULL; + zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); + return NULL; } if ((supported & needed_support_write) != needed_support_write) { - flags |= ZIP_RDONLY; + flags |= ZIP_RDONLY; } - if ((flags & (ZIP_RDONLY|ZIP_TRUNCATE)) == (ZIP_RDONLY|ZIP_TRUNCATE)) { + if ((flags & (ZIP_RDONLY | ZIP_TRUNCATE)) == (ZIP_RDONLY | ZIP_TRUNCATE)) { zip_error_set(error, ZIP_ER_RDONLY, 0); return NULL; } @@ -159,8 +157,7 @@ zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) zip_t * -_zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) -{ +_zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) { zip_t *za; zip_cdir_t *cdir; struct zip_stat st; @@ -179,7 +176,7 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) /* treat empty files as empty archives */ if (len == 0) { - if ((za=_zip_allocate_new(src, flags, error)) == NULL) { + if ((za = _zip_allocate_new(src, flags, error)) == NULL) { zip_source_free(src); return NULL; } @@ -187,12 +184,12 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) return za; } - if ((za=_zip_allocate_new(src, flags, error)) == NULL) { - return NULL; + if ((za = _zip_allocate_new(src, flags, error)) == NULL) { + return NULL; } if ((cdir = _zip_find_central_dir(za, len)) == NULL) { - _zip_error_copy(error, &za->error); + _zip_error_copy(error, &za->error); /* keep src so discard does not get rid of it */ zip_source_keep(src); zip_discard(za); @@ -207,14 +204,14 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) free(cdir); _zip_hash_reserve_capacity(za->names, za->nentry, &za->error); - + for (idx = 0; idx < za->nentry; idx++) { const zip_uint8_t *name = _zip_string_get(za->entry[idx].orig->filename, NULL, 0, error); if (name == NULL) { - /* keep src so discard does not get rid of it */ - zip_source_keep(src); - zip_discard(za); - return NULL; + /* keep src so discard does not get rid of it */ + zip_source_keep(src); + zip_discard(za); + return NULL; } if (_zip_hash_add(za->names, name, idx, ZIP_FL_UNCHANGED, &za->error) == false) { @@ -235,8 +232,7 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) void -_zip_set_open_error(int *zep, const zip_error_t *err, int ze) -{ +_zip_set_open_error(int *zep, const zip_error_t *err, int ze) { if (err) { ze = zip_error_code_zip(err); if (zip_error_system_type(err) == ZIP_ET_SYS) { @@ -256,8 +252,7 @@ _zip_set_open_error(int *zep, const zip_error_t *err, int ze) entries, or NULL if unsuccessful. */ static zip_cdir_t * -_zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error) -{ +_zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error) { zip_cdir_t *cd; zip_uint16_t comment_len; zip_uint64_t i, left; @@ -277,12 +272,12 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err } if (eocd_offset >= EOCD64LOCLEN && memcmp(_zip_buffer_data(buffer) + eocd_offset - EOCD64LOCLEN, EOCD64LOC_MAGIC, 4) == 0) { - _zip_buffer_set_offset(buffer, eocd_offset - EOCD64LOCLEN); - cd = _zip_read_eocd64(za->src, buffer, buf_offset, za->flags, error); + _zip_buffer_set_offset(buffer, eocd_offset - EOCD64LOCLEN); + cd = _zip_read_eocd64(za->src, buffer, buf_offset, za->flags, error); } else { - _zip_buffer_set_offset(buffer, eocd_offset); - cd = _zip_read_eocd(buffer, buf_offset, za->flags, error); + _zip_buffer_set_offset(buffer, eocd_offset); + cd = _zip_read_eocd(buffer, buf_offset, za->flags, error); } if (cd == NULL) @@ -299,63 +294,63 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err } if (comment_len || (za->open_flags & ZIP_CHECKCONS)) { - zip_uint64_t tail_len; + zip_uint64_t tail_len; - _zip_buffer_set_offset(buffer, eocd_offset + EOCDLEN); - tail_len = _zip_buffer_left(buffer); + _zip_buffer_set_offset(buffer, eocd_offset + EOCDLEN); + tail_len = _zip_buffer_left(buffer); - if (tail_len < comment_len || ((za->open_flags & ZIP_CHECKCONS) && tail_len != comment_len)) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_cdir_free(cd); - return NULL; - } + if (tail_len < comment_len || ((za->open_flags & ZIP_CHECKCONS) && tail_len != comment_len)) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_cdir_free(cd); + return NULL; + } - if (comment_len) { - if ((cd->comment=_zip_string_new(_zip_buffer_get(buffer, comment_len), comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) { - _zip_cdir_free(cd); - return NULL; - } - } + if (comment_len) { + if ((cd->comment = _zip_string_new(_zip_buffer_get(buffer, comment_len), comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) { + _zip_cdir_free(cd); + return NULL; + } + } } if (cd->offset >= buf_offset) { - zip_uint8_t *data; + zip_uint8_t *data; /* if buffer already read in, use it */ - _zip_buffer_set_offset(buffer, cd->offset - buf_offset); - - if ((data = _zip_buffer_get(buffer, cd->size)) == NULL) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_cdir_free(cd); - return NULL; - } - if ((cd_buffer = _zip_buffer_new(data, cd->size)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_cdir_free(cd); - return NULL; - } + _zip_buffer_set_offset(buffer, cd->offset - buf_offset); + + if ((data = _zip_buffer_get(buffer, cd->size)) == NULL) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_cdir_free(cd); + return NULL; + } + if ((cd_buffer = _zip_buffer_new(data, cd->size)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + _zip_cdir_free(cd); + return NULL; + } } else { - cd_buffer = NULL; + cd_buffer = NULL; - if (zip_source_seek(za->src, (zip_int64_t)cd->offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, za->src); - _zip_cdir_free(cd); - return NULL; - } + if (zip_source_seek(za->src, (zip_int64_t)cd->offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, za->src); + _zip_cdir_free(cd); + return NULL; + } /* possible consistency check: cd->offset = len-(cd->size+cd->comment_len+EOCDLEN) ? */ if (zip_source_tell(za->src) != (zip_int64_t)cd->offset) { - zip_error_set(error, ZIP_ER_NOZIP, 0); + zip_error_set(error, ZIP_ER_NOZIP, 0); _zip_cdir_free(cd); return NULL; } } left = (zip_uint64_t)cd->size; - i=0; + i = 0; while (left > 0) { bool grown = false; - zip_int64_t entry_size; + zip_int64_t entry_size; if (i == cd->nentry) { /* InfoZIP has a hack to avoid using Zip64: it stores nentries % 0x10000 */ @@ -373,48 +368,48 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err grown = true; } - if ((cd->entry[i].orig=_zip_dirent_new()) == NULL || (entry_size = _zip_dirent_read(cd->entry[i].orig, za->src, cd_buffer, false, error)) < 0) { + if ((cd->entry[i].orig = _zip_dirent_new()) == NULL || (entry_size = _zip_dirent_read(cd->entry[i].orig, za->src, cd_buffer, false, error)) < 0) { if (grown && zip_error_code_zip(error) == ZIP_ER_NOZIP) { zip_error_set(error, ZIP_ER_INCONS, 0); } _zip_cdir_free(cd); - _zip_buffer_free(cd_buffer); + _zip_buffer_free(cd_buffer); return NULL; } i++; - left -= (zip_uint64_t)entry_size; + left -= (zip_uint64_t)entry_size; } if (i != cd->nentry || left > 0) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(cd_buffer); - _zip_cdir_free(cd); - return NULL; + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_buffer_free(cd_buffer); + _zip_cdir_free(cd); + return NULL; } if (za->open_flags & ZIP_CHECKCONS) { - bool ok; - - if (cd_buffer) { - ok = _zip_buffer_eof(cd_buffer); - } - else { - zip_int64_t offset = zip_source_tell(za->src); - - if (offset < 0) { - _zip_error_set_from_source(error, za->src); - _zip_cdir_free(cd); - return NULL; - } - ok = ((zip_uint64_t)offset == cd->offset + cd->size); - } - - if (!ok) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(cd_buffer); - _zip_cdir_free(cd); - return NULL; - } + bool ok; + + if (cd_buffer) { + ok = _zip_buffer_eof(cd_buffer); + } + else { + zip_int64_t offset = zip_source_tell(za->src); + + if (offset < 0) { + _zip_error_set_from_source(error, za->src); + _zip_cdir_free(cd); + return NULL; + } + ok = ((zip_uint64_t)offset == cd->offset + cd->size); + } + + if (!ok) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_buffer_free(cd_buffer); + _zip_cdir_free(cd); + return NULL; + } } _zip_buffer_free(cd_buffer); @@ -429,8 +424,7 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err difference between the lowest and the highest fileposition reached */ static zip_int64_t -_zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) -{ +_zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) { zip_uint64_t i; zip_uint64_t min, max, j; struct zip_dirent temp; @@ -443,7 +437,7 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) else min = max = 0; - for (i=0; inentry; i++) { + for (i = 0; i < cd->nentry; i++) { if (cd->entry[i].orig->offset < min) min = cd->entry[i].orig->offset; if (min > (zip_uint64_t)cd->offset) { @@ -451,8 +445,7 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) return -1; } - j = cd->entry[i].orig->offset + cd->entry[i].orig->comp_size - + _zip_string_length(cd->entry[i].orig->filename) + LENTRYSIZE; + j = cd->entry[i].orig->offset + cd->entry[i].orig->comp_size + _zip_string_length(cd->entry[i].orig->filename) + LENTRYSIZE; if (j > max) max = j; if (max > (zip_uint64_t)cd->offset) { @@ -460,9 +453,9 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) return -1; } - if (zip_source_seek(za->src, (zip_int64_t)cd->entry[i].orig->offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, za->src); - return -1; + if (zip_source_seek(za->src, (zip_int64_t)cd->entry[i].orig->offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, za->src); + return -1; } if (_zip_dirent_read(&temp, za->src, NULL, true, error) == -1) { @@ -483,7 +476,7 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) _zip_dirent_finalize(&temp); } - return (max-min) < ZIP_INT64_MAX ? (zip_int64_t)(max-min) : ZIP_INT64_MAX; + return (max - min) < ZIP_INT64_MAX ? (zip_int64_t)(max - min) : ZIP_INT64_MAX; } @@ -492,25 +485,20 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) Return 0 if they are consistent, -1 if not. */ static int -_zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) -{ +_zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) { if ((central->version_needed < local->version_needed) #if 0 /* some zip-files have different values in local and global headers for the bitflags */ || (central->bitflags != local->bitflags) #endif - || (central->comp_method != local->comp_method) - || (central->last_mod != local->last_mod) - || !_zip_string_equal(central->filename, local->filename)) + || (central->comp_method != local->comp_method) || (central->last_mod != local->last_mod) || !_zip_string_equal(central->filename, local->filename)) return -1; - if ((central->crc != local->crc) || (central->comp_size != local->comp_size) - || (central->uncomp_size != local->uncomp_size)) { + if ((central->crc != local->crc) || (central->comp_size != local->comp_size) || (central->uncomp_size != local->uncomp_size)) { /* InfoZip stores valid values in local header even when data descriptor is used. This is in violation of the appnote. */ - if (((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 - || local->crc != 0 || local->comp_size != 0 || local->uncomp_size != 0)) + if (((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local->crc != 0 || local->comp_size != 0 || local->uncomp_size != 0)) return -1; } @@ -519,8 +507,7 @@ _zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) static zip_t * -_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) -{ +_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) { zip_t *za; if ((za = _zip_new(error)) == NULL) { @@ -530,8 +517,8 @@ _zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) za->src = src; za->open_flags = flags; if (flags & ZIP_RDONLY) { - za->flags |= ZIP_AFL_RDONLY; - za->ch_flags |= ZIP_AFL_RDONLY; + za->flags |= ZIP_AFL_RDONLY; + za->ch_flags |= ZIP_AFL_RDONLY; } return za; } @@ -541,14 +528,13 @@ _zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) * tests for file existence */ static exists_t -_zip_file_exists(zip_source_t *src, zip_error_t *error) -{ +_zip_file_exists(zip_source_t *src, zip_error_t *error) { struct zip_stat st; zip_stat_init(&st); if (zip_source_stat(src, &st) != 0) { - zip_error_t *src_error = zip_source_error(src); - if (zip_error_code_zip(src_error) == ZIP_ER_READ && zip_error_code_system(src_error) == ENOENT) { + zip_error_t *src_error = zip_source_error(src); + if (zip_error_code_zip(src_error) == ZIP_ER_READ && zip_error_code_system(src_error) == ENOENT) { return EXISTS_NOT; } _zip_error_copy(error, src_error); @@ -560,8 +546,7 @@ _zip_file_exists(zip_source_t *src, zip_error_t *error) static zip_cdir_t * -_zip_find_central_dir(zip_t *za, zip_uint64_t len) -{ +_zip_find_central_dir(zip_t *za, zip_uint64_t len) { zip_cdir_t *cdir, *cdirnew; zip_uint8_t *match; zip_int64_t buf_offset; @@ -573,7 +558,7 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) if (len < EOCDLEN) { zip_error_set(&za->error, ZIP_ER_NOZIP, 0); - return NULL; + return NULL; } buflen = (len < CDBUFSIZE ? len : CDBUFSIZE); @@ -586,62 +571,62 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) } } if ((buf_offset = zip_source_tell(za->src)) < 0) { - _zip_error_set_from_source(&za->error, za->src); - return NULL; + _zip_error_set_from_source(&za->error, za->src); + return NULL; } if ((buffer = _zip_buffer_new_from_source(za->src, buflen, NULL, &za->error)) == NULL) { - return NULL; + return NULL; } best = -1; cdir = NULL; if (buflen >= CDBUFSIZE) { - /* EOCD64 locator is before EOCD, so leave place for it */ - _zip_buffer_set_offset(buffer, EOCD64LOCLEN); + /* EOCD64 locator is before EOCD, so leave place for it */ + _zip_buffer_set_offset(buffer, EOCD64LOCLEN); } zip_error_set(&error, ZIP_ER_NOZIP, 0); match = _zip_buffer_get(buffer, 0); - while ((match=_zip_memmem(match, _zip_buffer_left(buffer)-(EOCDLEN-4), (const unsigned char *)EOCD_MAGIC, 4)) != NULL) { - _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); - if ((cdirnew = _zip_read_cdir(za, buffer, (zip_uint64_t)buf_offset, &error)) != NULL) { - if (cdir) { - if (best <= 0) { - best = _zip_checkcons(za, cdir, &error); - } - - a = _zip_checkcons(za, cdirnew, &error); - if (best < a) { - _zip_cdir_free(cdir); - cdir = cdirnew; - best = a; - } - else { - _zip_cdir_free(cdirnew); - } - } - else { - cdir = cdirnew; - if (za->open_flags & ZIP_CHECKCONS) - best = _zip_checkcons(za, cdir, &error); - else { - best = 0; - } - } - cdirnew = NULL; - } - - match++; - _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); + while ((match = _zip_memmem(match, _zip_buffer_left(buffer) - (EOCDLEN - 4), (const unsigned char *)EOCD_MAGIC, 4)) != NULL) { + _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); + if ((cdirnew = _zip_read_cdir(za, buffer, (zip_uint64_t)buf_offset, &error)) != NULL) { + if (cdir) { + if (best <= 0) { + best = _zip_checkcons(za, cdir, &error); + } + + a = _zip_checkcons(za, cdirnew, &error); + if (best < a) { + _zip_cdir_free(cdir); + cdir = cdirnew; + best = a; + } + else { + _zip_cdir_free(cdirnew); + } + } + else { + cdir = cdirnew; + if (za->open_flags & ZIP_CHECKCONS) + best = _zip_checkcons(za, cdir, &error); + else { + best = 0; + } + } + cdirnew = NULL; + } + + match++; + _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); } _zip_buffer_free(buffer); if (best < 0) { - _zip_error_copy(&za->error, &error); - _zip_cdir_free(cdir); - return NULL; + _zip_error_copy(&za->error, &error); + _zip_cdir_free(cdir); + return NULL; } return cdir; @@ -649,16 +634,14 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) static unsigned char * -_zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little, size_t littlelen) -{ +_zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little, size_t littlelen) { const unsigned char *p; if ((biglen < littlelen) || (littlelen == 0)) return NULL; - p = big-1; - while ((p=(const unsigned char *) - memchr(p+1, little[0], (size_t)(big-(p+1))+(size_t)(biglen-littlelen)+1)) != NULL) { - if (memcmp(p+1, little+1, littlelen-1)==0) + p = big - 1; + while ((p = (const unsigned char *)memchr(p + 1, little[0], (size_t)(big - (p + 1)) + (size_t)(biglen - littlelen) + 1)) != NULL) { + if (memcmp(p + 1, little + 1, littlelen - 1) == 0) return (unsigned char *)p; } @@ -667,8 +650,7 @@ _zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little static zip_cdir_t * -_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) -{ +_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) { zip_cdir_t *cd; zip_uint64_t i, nentry, size, offset, eocd_offset; @@ -699,23 +681,23 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags size = _zip_buffer_get_32(buffer); offset = _zip_buffer_get_32(buffer); - if (offset+size < offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + if (offset + size < offset) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; } - if (offset+size > buf_offset + eocd_offset) { + if (offset + size > buf_offset + eocd_offset) { /* cdir spans past EOCD record */ zip_error_set(error, ZIP_ER_INCONS, 0); return NULL; } - if ((flags & ZIP_CHECKCONS) && offset+size != buf_offset + eocd_offset) { + if ((flags & ZIP_CHECKCONS) && offset + size != buf_offset + eocd_offset) { zip_error_set(error, ZIP_ER_INCONS, 0); return NULL; } - if ((cd=_zip_cdir_new(nentry, error)) == NULL) + if ((cd = _zip_cdir_new(nentry, error)) == NULL) return NULL; cd->is_zip64 = false; @@ -727,8 +709,7 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags static zip_cdir_t * -_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) -{ +_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) { zip_cdir_t *cd; zip_uint64_t offset; zip_uint8_t eocd[EOCD64LEN]; @@ -746,8 +727,8 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse eocd_offset = _zip_buffer_get_64(buffer); if (eocd_offset > ZIP_INT64_MAX || eocd_offset + EOCD64LEN < eocd_offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; } if (eocd_offset + EOCD64LEN > eocdloc_offset + buf_offset) { @@ -756,25 +737,25 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse } if (eocd_offset >= buf_offset && eocd_offset + EOCD64LEN <= buf_offset + _zip_buffer_size(buffer)) { - _zip_buffer_set_offset(buffer, eocd_offset - buf_offset); - free_buffer = false; + _zip_buffer_set_offset(buffer, eocd_offset - buf_offset); + free_buffer = false; } else { - if (zip_source_seek(src, (zip_int64_t)eocd_offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, src); - return NULL; - } - if ((buffer = _zip_buffer_new_from_source(src, EOCD64LEN, eocd, error)) == NULL) { - return NULL; - } - free_buffer = true; + if (zip_source_seek(src, (zip_int64_t)eocd_offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, src); + return NULL; + } + if ((buffer = _zip_buffer_new_from_source(src, EOCD64LEN, eocd, error)) == NULL) { + return NULL; + } + free_buffer = true; } if (memcmp(_zip_buffer_get(buffer, 4), EOCD64_MAGIC, 4) != 0) { zip_error_set(error, ZIP_ER_INCONS, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; } @@ -782,10 +763,10 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if ((flags & ZIP_CHECKCONS) && size + eocd_offset + 12 != buf_offset + eocdloc_offset) { zip_error_set(error, ZIP_ER_INCONS, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } - return NULL; + if (free_buffer) { + _zip_buffer_free(buffer); + } + return NULL; } _zip_buffer_get(buffer, 4); /* skip version made by/needed */ @@ -804,16 +785,16 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse } if ((flags & ZIP_CHECKCONS) && (eocd_disk != eocd_disk64 || num_disks != num_disks64)) { zip_error_set(error, ZIP_ER_INCONS, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; } if (num_disks != 0 || eocd_disk != 0) { zip_error_set(error, ZIP_ER_MULTIDISK, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; } @@ -822,9 +803,9 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if (nentry != i) { zip_error_set(error, ZIP_ER_MULTIDISK, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; } @@ -832,32 +813,32 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse offset = _zip_buffer_get_64(buffer); if (!_zip_buffer_ok(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } - return NULL; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + if (free_buffer) { + _zip_buffer_free(buffer); + } + return NULL; } if (free_buffer) { - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); } - if (offset > ZIP_INT64_MAX || offset+size < offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + if (offset > ZIP_INT64_MAX || offset + size < offset) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; } - if (offset+size > buf_offset + eocd_offset) { + if (offset + size > buf_offset + eocd_offset) { /* cdir spans past EOCD record */ zip_error_set(error, ZIP_ER_INCONS, 0); return NULL; } - if ((flags & ZIP_CHECKCONS) && offset+size != buf_offset + eocd_offset) { + if ((flags & ZIP_CHECKCONS) && offset + size != buf_offset + eocd_offset) { zip_error_set(error, ZIP_ER_INCONS, 0); return NULL; } - if ((cd=_zip_cdir_new(nentry, error)) == NULL) + if ((cd = _zip_cdir_new(nentry, error)) == NULL) return NULL; cd->is_zip64 = true; diff --git a/lib/zip_progress.c b/lib/zip_progress.c index b1b6e58..46c8bb8 100644 --- a/lib/zip_progress.c +++ b/lib/zip_progress.c @@ -48,10 +48,10 @@ struct zip_progress { double precision; /* state */ - double last_update; /* last value callback function was called with */ + double last_update; /* last value callback function was called with */ - double start; /* start of sub-progress section */ - double end; /* end of sub-progress section */ + double start; /* start of sub-progress section */ + double end; /* end of sub-progress section */ }; @@ -64,11 +64,11 @@ _zip_progress_end(zip_progress_t *progress) { void _zip_progress_free(zip_progress_t *progress) { if (progress == NULL) { - return; + return; } if (progress->ud_free) { - progress->ud_free(progress->ud); + progress->ud_free(progress->ud); } free(progress); @@ -80,8 +80,8 @@ _zip_progress_new(zip_t *za, double precision, zip_progress_callback callback, v zip_progress_t *progress = (zip_progress_t *)malloc(sizeof(*progress)); if (progress == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return NULL; + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; } progress->za = za; @@ -97,7 +97,7 @@ _zip_progress_new(zip_t *za, double precision, zip_progress_callback callback, v void _zip_progress_start(zip_progress_t *progress) { if (progress == NULL) { - return; + return; } progress->last_update = 0.0; @@ -108,7 +108,7 @@ _zip_progress_start(zip_progress_t *progress) { void _zip_progress_subrange(zip_progress_t *progress, double start, double end) { if (progress == NULL) { - return; + return; } progress->start = start; @@ -122,14 +122,14 @@ _zip_progress_update(zip_progress_t *progress, double sub_current) { double current; if (progress == NULL) { - return; + return; } current = ZIP_MIN(ZIP_MAX(sub_current, 0.0), 1.0) * (progress->end - progress->start) + progress->start; if (current - progress->last_update > progress->precision) { - progress->callback(progress->za, current, progress->ud); - progress->last_update = current; + progress->callback(progress->za, current, progress->ud); + progress->last_update = current; } } @@ -139,9 +139,9 @@ zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progr zip_progress_t *progress = NULL; if (callback != NULL) { - if ((progress = _zip_progress_new(za, precision, callback, ud_free, ud)) == NULL) { - return -1; - } + if ((progress = _zip_progress_new(za, precision, callback, ud_free, ud)) == NULL) { + return -1; + } } _zip_progress_free(za->progress); @@ -164,21 +164,20 @@ _zip_legacy_progress_callback(zip_t *za, double progress, void *vud) { } ZIP_EXTERN void -zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback) -{ +zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback) { struct legacy_ud *ud; - + if (progress_callback == NULL) { - zip_register_progress_callback_with_state(za, 0, NULL, NULL, NULL); + zip_register_progress_callback_with_state(za, 0, NULL, NULL, NULL); } if ((ud = (struct legacy_ud *)malloc(sizeof(*ud))) == NULL) { - return; + return; } ud->callback = progress_callback; if (zip_register_progress_callback_with_state(za, 0.001, _zip_legacy_progress_callback, free, ud) < 0) { - free(ud); + free(ud); } } diff --git a/lib/zip_random_unix.c b/lib/zip_random_unix.c index 2684456..2c59847 100644 --- a/lib/zip_random_unix.c +++ b/lib/zip_random_unix.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,9 +36,8 @@ #include #include -bool -zip_random(zip_uint8_t *buffer, zip_uint16_t length) -{ +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { int fd; if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { diff --git a/lib/zip_random_uwp.c b/lib/zip_random_uwp.c index aeead0f..3d93f0b 100644 --- a/lib/zip_random_uwp.c +++ b/lib/zip_random_uwp.c @@ -31,16 +31,15 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include #include +#include +#include #include "zipint.h" #include "zipwin32.h" -bool -zip_random(zip_uint8_t *buffer, zip_uint16_t length) -{ +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { BCRYPT_ALG_HANDLE hAlg = NULL; NTSTATUS hr = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0); if (hr != STATUS_SUCCESS || hAlg == NULL) { diff --git a/lib/zip_random_win32.c b/lib/zip_random_win32.c index 967a54c..86fa68a 100644 --- a/lib/zip_random_win32.c +++ b/lib/zip_random_win32.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,9 +34,10 @@ #include "zipint.h" #include "zipwin32.h" -bool -zip_random(zip_uint8_t *buffer, zip_uint16_t length) -{ +#include + +ZIP_EXTERN bool +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { HCRYPTPROV hprov; if (!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { return false; diff --git a/lib/zip_rename.c b/lib/zip_rename.c index 14e101d..0cc81ed 100644 --- a/lib/zip_rename.c +++ b/lib/zip_rename.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,7 +39,6 @@ ZIP_EXTERN int -zip_rename(zip_t *za, zip_uint64_t idx, const char *name) -{ +zip_rename(zip_t *za, zip_uint64_t idx, const char *name) { return zip_file_rename(za, idx, name, 0); } diff --git a/lib/zip_replace.c b/lib/zip_replace.c index eed019a..34b922e 100644 --- a/lib/zip_replace.c +++ b/lib/zip_replace.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,7 +37,6 @@ ZIP_EXTERN int -zip_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source) -{ +zip_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source) { return zip_file_replace(za, idx, source, 0); } diff --git a/lib/zip_set_archive_comment.c b/lib/zip_set_archive_comment.c index 9090eec..221fde5 100644 --- a/lib/zip_set_archive_comment.c +++ b/lib/zip_set_archive_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ ZIP_EXTERN int -zip_set_archive_comment(zip_t *za, const char *comment, zip_uint16_t len) -{ +zip_set_archive_comment(zip_t *za, const char *comment, zip_uint16_t len) { zip_string_t *cstr; if (ZIP_IS_RDONLY(za)) { @@ -53,7 +52,7 @@ zip_set_archive_comment(zip_t *za, const char *comment, zip_uint16_t len) } if (len > 0) { - if ((cstr=_zip_string_new((const zip_uint8_t *)comment, len, ZIP_FL_ENC_GUESS, &za->error)) == NULL) + if ((cstr = _zip_string_new((const zip_uint8_t *)comment, len, ZIP_FL_ENC_GUESS, &za->error)) == NULL) return -1; if (_zip_guess_encoding(cstr, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_CP437) { @@ -68,8 +67,7 @@ zip_set_archive_comment(zip_t *za, const char *comment, zip_uint16_t len) _zip_string_free(za->comment_changes); za->comment_changes = NULL; - if (((za->comment_orig && _zip_string_equal(za->comment_orig, cstr)) - || (za->comment_orig == NULL && cstr == NULL))) { + if (((za->comment_orig && _zip_string_equal(za->comment_orig, cstr)) || (za->comment_orig == NULL && cstr == NULL))) { _zip_string_free(cstr); za->comment_changed = 0; } @@ -77,6 +75,6 @@ zip_set_archive_comment(zip_t *za, const char *comment, zip_uint16_t len) za->comment_changes = cstr; za->comment_changed = 1; } - + return 0; } diff --git a/lib/zip_set_archive_flag.c b/lib/zip_set_archive_flag.c index 2625b2e..6fb1131 100644 --- a/lib/zip_set_archive_flag.c +++ b/lib/zip_set_archive_flag.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,10 +36,9 @@ ZIP_EXTERN int -zip_set_archive_flag(zip_t *za, zip_flags_t flag, int value) -{ +zip_set_archive_flag(zip_t *za, zip_flags_t flag, int value) { unsigned int new_flags; - + if (value) new_flags = za->ch_flags | flag; else @@ -53,8 +52,7 @@ zip_set_archive_flag(zip_t *za, zip_flags_t flag, int value) return -1; } - if ((flag & ZIP_AFL_RDONLY) && value - && (za->ch_flags & ZIP_AFL_RDONLY) == 0) { + if ((flag & ZIP_AFL_RDONLY) && value && (za->ch_flags & ZIP_AFL_RDONLY) == 0) { if (_zip_changed(za, NULL)) { zip_error_set(&za->error, ZIP_ER_CHANGED, 0); return -1; diff --git a/lib/zip_set_default_password.c b/lib/zip_set_default_password.c index ff7e35a..33c1754 100644 --- a/lib/zip_set_default_password.c +++ b/lib/zip_set_default_password.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,15 +39,14 @@ ZIP_EXTERN int -zip_set_default_password(zip_t *za, const char *passwd) -{ +zip_set_default_password(zip_t *za, const char *passwd) { if (za == NULL) return -1; free(za->default_password); - + if (passwd) { - if ((za->default_password=strdup(passwd)) == NULL) { + if ((za->default_password = strdup(passwd)) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return -1; } diff --git a/lib/zip_set_file_comment.c b/lib/zip_set_file_comment.c index d356693..93594f0 100644 --- a/lib/zip_set_file_comment.c +++ b/lib/zip_set_file_comment.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,11 +39,10 @@ ZIP_EXTERN int -zip_set_file_comment(zip_t *za, zip_uint64_t idx, const char *comment, int len) -{ +zip_set_file_comment(zip_t *za, zip_uint64_t idx, const char *comment, int len) { if (len < 0 || len > ZIP_UINT16_MAX) { - zip_error_set(&za->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; } return zip_file_set_comment(za, idx, comment, (zip_uint16_t)len, 0); } diff --git a/lib/zip_set_file_compression.c b/lib/zip_set_file_compression.c index 5738aab..6de2d40 100644 --- a/lib/zip_set_file_compression.c +++ b/lib/zip_set_file_compression.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,8 +36,7 @@ ZIP_EXTERN int -zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_uint32_t flags) -{ +zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_uint32_t flags) { zip_entry_t *e; zip_int32_t old_method; @@ -56,15 +55,15 @@ zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_ui return -1; } - e = za->entry+idx; - + e = za->entry + idx; + old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method); - + /* TODO: do we want to recompress if level is set? Only if it's * different than what bit flags tell us, but those are not * defined for all compression methods, or not directly mappable * to levels */ - + if (method == old_method) { if (e->changes) { e->changes->changed &= ~ZIP_DIRENT_COMP_METHOD; @@ -76,17 +75,17 @@ zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_ui } } else { - if (e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - } - - e->changes->comp_method = method; + if (e->changes == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + } + + e->changes->comp_method = method; e->changes->compression_level = (zip_uint16_t)flags; - e->changes->changed |= ZIP_DIRENT_COMP_METHOD; + e->changes->changed |= ZIP_DIRENT_COMP_METHOD; } - + return 0; } diff --git a/lib/zip_set_name.c b/lib/zip_set_name.c index 34b7615..33fb4bc 100644 --- a/lib/zip_set_name.c +++ b/lib/zip_set_name.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,8 +39,7 @@ int -_zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) -{ +_zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) { zip_entry_t *e; zip_string_t *str; bool same_as_orig; @@ -59,8 +58,8 @@ _zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) } if (name && name[0] != '\0') { - /* TODO: check for string too long */ - if ((str=_zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL) + /* TODO: check for string too long */ + if ((str = _zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL) return -1; if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(str, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED) str->encoding = ZIP_ENCODING_UTF8_KNOWN; @@ -69,19 +68,19 @@ _zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) str = NULL; /* TODO: encoding flags needed for CP437? */ - if ((i=_zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) { + if ((i = _zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) { _zip_string_free(str); zip_error_set(&za->error, ZIP_ER_EXISTS, 0); return -1; } /* no effective name change */ - if (i>=0 && (zip_uint64_t)i == idx) { + if (i >= 0 && (zip_uint64_t)i == idx) { _zip_string_free(str); return 0; } - e = za->entry+idx; + e = za->entry + idx; if (e->orig) same_as_orig = _zip_string_equal(e->orig->filename, str); @@ -89,7 +88,7 @@ _zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) same_as_orig = false; if (!same_as_orig && e->changes == NULL) { - if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { + if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); _zip_string_free(str); return -1; @@ -110,7 +109,7 @@ _zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) else { old_str = NULL; } - + if (old_str) { if ((old_name = _zip_string_get(old_str, NULL, 0, &za->error)) == NULL) { _zip_string_free(str); diff --git a/lib/zip_source_begin_write.c b/lib/zip_source_begin_write.c index 0459337..0b010df 100644 --- a/lib/zip_source_begin_write.c +++ b/lib/zip_source_begin_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,18 +36,17 @@ ZIP_EXTERN int -zip_source_begin_write(zip_source_t *src) -{ +zip_source_begin_write(zip_source_t *src) { if (ZIP_SOURCE_IS_OPEN_WRITING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + if (_zip_source_call(src, NULL, 0, ZIP_SOURCE_BEGIN_WRITE) < 0) { - return -1; + return -1; } src->write_state = ZIP_SOURCE_WRITE_OPEN; - + return 0; } diff --git a/lib/zip_source_begin_write_cloning.c b/lib/zip_source_begin_write_cloning.c index ad2cfac..8c4cf71 100644 --- a/lib/zip_source_begin_write_cloning.c +++ b/lib/zip_source_begin_write_cloning.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,18 +36,17 @@ ZIP_EXTERN int -zip_source_begin_write_cloning(zip_source_t *src, zip_uint64_t offset) -{ +zip_source_begin_write_cloning(zip_source_t *src, zip_uint64_t offset) { if (ZIP_SOURCE_IS_OPEN_WRITING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + if (_zip_source_call(src, NULL, offset, ZIP_SOURCE_BEGIN_WRITE_CLONING) < 0) { - return -1; + return -1; } src->write_state = ZIP_SOURCE_WRITE_OPEN; - + return 0; } diff --git a/lib/zip_source_buffer.c b/lib/zip_source_buffer.c index 86ed750..05ade4a 100644 --- a/lib/zip_source_buffer.c +++ b/lib/zip_source_buffer.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,23 +37,23 @@ #include "zipint.h" #ifndef WRITE_FRAGMENT_SIZE -#define WRITE_FRAGMENT_SIZE (64*1024) +#define WRITE_FRAGMENT_SIZE (64 * 1024) #endif struct buffer { - zip_buffer_fragment_t *fragments; /* fragments */ - zip_uint64_t *fragment_offsets; /* offset of each fragment from start of buffer, nfragments+1 entries */ - zip_uint64_t nfragments; /* number of allocated fragments */ - zip_uint64_t fragments_capacity; /* size of fragments (number of pointers) */ + zip_buffer_fragment_t *fragments; /* fragments */ + zip_uint64_t *fragment_offsets; /* offset of each fragment from start of buffer, nfragments+1 entries */ + zip_uint64_t nfragments; /* number of allocated fragments */ + zip_uint64_t fragments_capacity; /* size of fragments (number of pointers) */ - zip_uint64_t first_owned_fragment; /* first fragment to free data from */ + zip_uint64_t first_owned_fragment; /* first fragment to free data from */ - zip_uint64_t shared_fragments; /* number of shared fragments */ - struct buffer *shared_buffer; /* buffer fragments are shared with */ - zip_uint64_t size; /* size of buffer */ + zip_uint64_t shared_fragments; /* number of shared fragments */ + struct buffer *shared_buffer; /* buffer fragments are shared with */ + zip_uint64_t size; /* size of buffer */ - zip_uint64_t offset; /* current offset in buffer */ - zip_uint64_t current_fragment; /* fragment current offset is in */ + zip_uint64_t offset; /* current offset in buffer */ + zip_uint64_t current_fragment; /* fragment current offset is in */ }; typedef struct buffer buffer_t; @@ -65,8 +65,8 @@ struct read_data { buffer_t *out; }; -#define buffer_capacity(buffer) ((buffer)->fragment_offsets[(buffer)->nfragments]) -#define buffer_size(buffer) ((buffer)->size) +#define buffer_capacity(buffer) ((buffer)->fragment_offsets[(buffer)->nfragments]) +#define buffer_size(buffer) ((buffer)->size) static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t length, zip_error_t *error); static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset); @@ -81,8 +81,7 @@ static zip_int64_t read_data(void *, void *, zip_uint64_t, zip_source_cmd_t); ZIP_EXTERN zip_source_t * -zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) -{ +zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) { if (za == NULL) return NULL; @@ -91,13 +90,12 @@ zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) ZIP_EXTERN zip_source_t * -zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) -{ +zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) { zip_buffer_fragment_t fragment; if (data == NULL && len > 0) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; } fragment.data = (zip_uint8_t *)data; @@ -108,10 +106,9 @@ zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_erro ZIP_EXTERN zip_source_t * -zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep) -{ +zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep) { if (za == NULL) { - return NULL; + return NULL; } return zip_source_buffer_fragment_create(fragments, nfragments, freep, &za->error); @@ -119,24 +116,23 @@ zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zi ZIP_EXTERN zip_source_t * -zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_error_t *error) -{ +zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_error_t *error) { struct read_data *ctx; zip_source_t *zs; buffer_t *buffer; if (fragments == NULL && nfragments > 0) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; } if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) { - return NULL; + return NULL; } - if ((ctx=(struct read_data *)malloc(sizeof(*ctx))) == NULL) { + if ((ctx = (struct read_data *)malloc(sizeof(*ctx))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); - buffer_free(buffer); + buffer_free(buffer); return NULL; } @@ -144,8 +140,8 @@ zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_ui ctx->out = NULL; ctx->mtime = time(NULL); zip_error_init(&ctx->error); - - if ((zs=zip_source_function_create(read_data, ctx, error)) == NULL) { + + if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) { buffer_free(ctx->in); free(ctx); return NULL; @@ -156,130 +152,127 @@ zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_ui static zip_int64_t -read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct read_data *ctx = (struct read_data *)state; switch (cmd) { - case ZIP_SOURCE_BEGIN_WRITE: - if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) { - return -1; - } - ctx->out->offset = 0; - ctx->out->current_fragment = 0; - return 0; - - case ZIP_SOURCE_BEGIN_WRITE_CLONING: - if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) { - return -1; - } - ctx->out->offset = len; - ctx->out->current_fragment = ctx->out->nfragments; - return 0; - - case ZIP_SOURCE_CLOSE: - return 0; - - case ZIP_SOURCE_COMMIT_WRITE: - buffer_free(ctx->in); - ctx->in = ctx->out; - ctx->out = NULL; - return 0; - - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); - - case ZIP_SOURCE_FREE: - buffer_free(ctx->in); - buffer_free(ctx->out); - free(ctx); - return 0; - - case ZIP_SOURCE_OPEN: - ctx->in->offset = 0; - ctx->in->current_fragment = 0; - return 0; - - case ZIP_SOURCE_READ: - if (len > ZIP_INT64_MAX) { - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } - return buffer_read(ctx->in, data, len); - - case ZIP_SOURCE_REMOVE: - { - buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error); - if (empty == NULL) { - return -1; - } - - buffer_free(ctx->in); - ctx->in = empty; - return 0; + case ZIP_SOURCE_BEGIN_WRITE: + if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) { + return -1; } + ctx->out->offset = 0; + ctx->out->current_fragment = 0; + return 0; - case ZIP_SOURCE_ROLLBACK_WRITE: - buffer_free(ctx->out); - ctx->out = NULL; - return 0; - - case ZIP_SOURCE_SEEK: - return buffer_seek(ctx->in, data, len, &ctx->error); - - case ZIP_SOURCE_SEEK_WRITE: - return buffer_seek(ctx->out, data, len, &ctx->error); - - case ZIP_SOURCE_STAT: - { - zip_stat_t *st; - - if (len < sizeof(*st)) { - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } + case ZIP_SOURCE_BEGIN_WRITE_CLONING: + if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) { + return -1; + } + ctx->out->offset = len; + ctx->out->current_fragment = ctx->out->nfragments; + return 0; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_COMMIT_WRITE: + buffer_free(ctx->in); + ctx->in = ctx->out; + ctx->out = NULL; + return 0; + + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, len); - st = (zip_stat_t *)data; - - zip_stat_init(st); - st->mtime = ctx->mtime; - st->size = ctx->in->size; - st->comp_size = st->size; - st->comp_method = ZIP_CM_STORE; - st->encryption_method = ZIP_EM_NONE; - st->valid = ZIP_STAT_MTIME|ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD; - - return sizeof(*st); + case ZIP_SOURCE_FREE: + buffer_free(ctx->in); + buffer_free(ctx->out); + free(ctx); + return 0; + + case ZIP_SOURCE_OPEN: + ctx->in->offset = 0; + ctx->in->current_fragment = 0; + return 0; + + case ZIP_SOURCE_READ: + if (len > ZIP_INT64_MAX) { + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; } + return buffer_read(ctx->in, data, len); - case ZIP_SOURCE_SUPPORTS: - return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_BEGIN_WRITE_CLONING, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, -1); - - case ZIP_SOURCE_TELL: - if (ctx->in->offset > ZIP_INT64_MAX) { - zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); - return -1; - } - return (zip_int64_t)ctx->in->offset; - - - case ZIP_SOURCE_TELL_WRITE: - if (ctx->out->offset > ZIP_INT64_MAX) { - zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); - return -1; - } - return (zip_int64_t)ctx->out->offset; + case ZIP_SOURCE_REMOVE: { + buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error); + if (empty == NULL) { + return -1; + } - case ZIP_SOURCE_WRITE: - if (len > ZIP_INT64_MAX) { - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } - return buffer_write(ctx->out, data, len, &ctx->error); + buffer_free(ctx->in); + ctx->in = empty; + return 0; + } + + case ZIP_SOURCE_ROLLBACK_WRITE: + buffer_free(ctx->out); + ctx->out = NULL; + return 0; + + case ZIP_SOURCE_SEEK: + return buffer_seek(ctx->in, data, len, &ctx->error); - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + case ZIP_SOURCE_SEEK_WRITE: + return buffer_seek(ctx->out, data, len, &ctx->error); + + case ZIP_SOURCE_STAT: { + zip_stat_t *st; + + if (len < sizeof(*st)) { + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; + } + + st = (zip_stat_t *)data; + + zip_stat_init(st); + st->mtime = ctx->mtime; + st->size = ctx->in->size; + st->comp_size = st->size; + st->comp_method = ZIP_CM_STORE; + st->encryption_method = ZIP_EM_NONE; + st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; + + return sizeof(*st); + } + + case ZIP_SOURCE_SUPPORTS: + return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_BEGIN_WRITE_CLONING, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, -1); + + case ZIP_SOURCE_TELL: + if (ctx->in->offset > ZIP_INT64_MAX) { + zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); + return -1; + } + return (zip_int64_t)ctx->in->offset; + + + case ZIP_SOURCE_TELL_WRITE: + if (ctx->out->offset > ZIP_INT64_MAX) { + zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); + return -1; + } + return (zip_int64_t)ctx->out->offset; + + case ZIP_SOURCE_WRITE: + if (len > ZIP_INT64_MAX) { + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; + } + return buffer_write(ctx->out, data, len, &ctx->error); + + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } @@ -290,37 +283,40 @@ buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) { buffer_t *clone; if (offset == 0) { - return buffer_new(NULL, 0, 1, error); + return buffer_new(NULL, 0, 1, error); } if (offset > buffer->size) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; } if (buffer->shared_buffer != NULL) { - zip_error_set(error, ZIP_ER_INUSE, 0); - return NULL; + zip_error_set(error, ZIP_ER_INUSE, 0); + return NULL; } fragment = buffer_find_fragment(buffer, offset); fragment_offset = offset - buffer->fragment_offsets[fragment]; if (fragment_offset == 0) { - fragment--; - fragment_offset = buffer->fragments[fragment].length; + fragment--; + fragment_offset = buffer->fragments[fragment].length; } waste = buffer->fragments[fragment].length - fragment_offset; if (waste > offset) { - zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); - return NULL; + zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); + return NULL; } if ((clone = buffer_new(buffer->fragments, fragment + 1, 0, error)) == NULL) { - return NULL; + return NULL; } +#ifndef __clang_analyzer__ + /* clone->fragments can't be null, since it was created with at least one fragment */ clone->fragments[clone->nfragments - 1].length = fragment_offset; +#endif clone->fragment_offsets[clone->nfragments] = offset; clone->size = offset; @@ -343,16 +339,16 @@ buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) { high = buffer->nfragments - 1; while (low < high) { - mid = (high - low) / 2 + low; - if (buffer->fragment_offsets[mid] > offset) { - high = mid - 1; - } - else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) { - return mid; - } - else { - low = mid + 1; - } + mid = (high - low) / 2 + low; + if (buffer->fragment_offsets[mid] > offset) { + high = mid - 1; + } + else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) { + return mid; + } + else { + low = mid + 1; + } } return low; @@ -360,47 +356,47 @@ buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) { static void -buffer_free(buffer_t *buffer) -{ +buffer_free(buffer_t *buffer) { zip_uint64_t i; if (buffer == NULL) { - return; + return; } if (buffer->shared_buffer != NULL) { - buffer->shared_buffer->shared_buffer = NULL; - buffer->shared_buffer->shared_fragments = 0; + buffer->shared_buffer->shared_buffer = NULL; + buffer->shared_buffer->shared_fragments = 0; - buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments); + buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments); } for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) { - free(buffer->fragments[i].data); + free(buffer->fragments[i].data); } free(buffer->fragments); + free(buffer->fragment_offsets); free(buffer); } static bool -buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) -{ +buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) { zip_buffer_fragment_t *fragments; zip_uint64_t *offsets; if (capacity < buffer->fragments_capacity) { - return true; + return true; } - if ((fragments = realloc(buffer->fragments, sizeof(buffer->fragments[0]) * capacity)) == NULL - || (offsets = realloc(buffer->fragment_offsets, sizeof(buffer->fragment_offsets[0]) * (capacity + 1))) == NULL) { - free(fragments); + if ((fragments = realloc(buffer->fragments, sizeof(buffer->fragments[0]) * capacity)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } - buffer->fragments = fragments; + if ((offsets = realloc(buffer->fragment_offsets, sizeof(buffer->fragment_offsets[0]) * (capacity + 1))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return false; + } buffer->fragment_offsets = offsets; buffer->fragments_capacity = capacity; @@ -409,8 +405,7 @@ buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *erro static buffer_t * -buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) -{ +buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) { buffer_t *buffer; if ((buffer = malloc(sizeof(*buffer))) == NULL) { @@ -428,50 +423,49 @@ buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int buffer->shared_fragments = 0; if (nfragments == 0) { - if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) { - free(buffer); - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; - } - buffer->fragment_offsets[0] = 0; + if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) { + free(buffer); + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + buffer->fragment_offsets[0] = 0; } else { - zip_uint64_t i, j, offset; - - if (!buffer_grow_fragments(buffer, nfragments, NULL)) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - buffer_free(buffer); - return NULL; - } - - offset = 0; - for (i = 0, j = 0; i < nfragments; i++) { - if (fragments[i].length == 0) { - continue; - } - if (fragments[i].data == NULL) { - zip_error_set(error, ZIP_ER_INVAL, 0); - buffer_free(buffer); - return NULL; - } - buffer->fragments[j].data = fragments[i].data; - buffer->fragments[j].length = fragments[i].length; - buffer->fragment_offsets[i] = offset; - offset += fragments[i].length; - j++; - } - buffer->nfragments = j; - buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments; - buffer->fragment_offsets[nfragments] = offset; - buffer->size = offset; + zip_uint64_t i, j, offset; + + if (!buffer_grow_fragments(buffer, nfragments, NULL)) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + buffer_free(buffer); + return NULL; + } + + offset = 0; + for (i = 0, j = 0; i < nfragments; i++) { + if (fragments[i].length == 0) { + continue; + } + if (fragments[i].data == NULL) { + zip_error_set(error, ZIP_ER_INVAL, 0); + buffer_free(buffer); + return NULL; + } + buffer->fragments[j].data = fragments[i].data; + buffer->fragments[j].length = fragments[i].length; + buffer->fragment_offsets[i] = offset; + offset += fragments[i].length; + j++; + } + buffer->nfragments = j; + buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments; + buffer->fragment_offsets[nfragments] = offset; + buffer->size = offset; } return buffer; } static zip_int64_t -buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) -{ +buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { zip_uint64_t n, i, fragment_offset; length = ZIP_MIN(length, buffer->size - buffer->offset); @@ -488,12 +482,12 @@ buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) n = 0; while (n < length) { zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset); - + memcpy(data + n, buffer->fragments[i].data + fragment_offset, left); - if (left == buffer->fragments[i].length - fragment_offset) { - i++; - } + if (left == buffer->fragments[i].length - fragment_offset) { + i++; + } n += left; fragment_offset = 0; } @@ -505,12 +499,11 @@ buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) static int -buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) -{ +buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) { zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error); - + if (new_offset < 0) { - return -1; + return -1; } buffer->offset = (zip_uint64_t)new_offset; @@ -520,8 +513,7 @@ buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) static zip_int64_t -buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) -{ +buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { zip_uint64_t n, i, fragment_offset, capacity; if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) { @@ -532,19 +524,19 @@ buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip /* grow buffer if needed */ capacity = buffer_capacity(buffer); if (buffer->offset + length > capacity) { - zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE; + zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE; if (needed_fragments > buffer->fragments_capacity) { zip_uint64_t new_capacity = buffer->fragments_capacity; - if (new_capacity == 0) { - new_capacity = 16; - } + if (new_capacity == 0) { + new_capacity = 16; + } while (new_capacity < needed_fragments) { new_capacity *= 2; } - if (!buffer_grow_fragments(buffer, new_capacity, error)) { + if (!buffer_grow_fragments(buffer, new_capacity, error)) { zip_error_set(error, ZIP_ER_MEMORY, 0); return -1; } @@ -555,10 +547,10 @@ buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip zip_error_set(error, ZIP_ER_MEMORY, 0); return -1; } - buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE; - buffer->nfragments++; - capacity += WRITE_FRAGMENT_SIZE; - buffer->fragment_offsets[buffer->nfragments] = capacity; + buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE; + buffer->nfragments++; + capacity += WRITE_FRAGMENT_SIZE; + buffer->fragment_offsets[buffer->nfragments] = capacity; } } @@ -567,12 +559,12 @@ buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip n = 0; while (n < length) { zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset); - + memcpy(buffer->fragments[i].data + fragment_offset, data + n, left); - if (n == buffer->fragments[i].length - fragment_offset) { - i++; - } + if (n == buffer->fragments[i].length - fragment_offset) { + i++; + } n += left; fragment_offset = 0; } diff --git a/lib/zip_source_call.c b/lib/zip_source_call.c index 21f28bc..ec54b92 100644 --- a/lib/zip_source_call.c +++ b/lib/zip_source_call.c @@ -1,10 +1,10 @@ /* zip_source_call.c -- invoke callback command on zip_source Copyright (C) 2009-2014 Dieter Baron and Thomas Klausner - + This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,33 +36,32 @@ zip_int64_t -_zip_source_call(zip_source_t *src, void *data, zip_uint64_t length, zip_source_cmd_t command) -{ +_zip_source_call(zip_source_t *src, void *data, zip_uint64_t length, zip_source_cmd_t command) { zip_int64_t ret; - + if ((src->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(command)) == 0) { - zip_error_set(&src->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } if (src->src == NULL) { - ret = src->cb.f(src->ud, data, length, command); + ret = src->cb.f(src->ud, data, length, command); } else { - ret = src->cb.l(src->src, src->ud, data, length, command); + ret = src->cb.l(src->src, src->ud, data, length, command); } - + if (ret < 0) { - if (command != ZIP_SOURCE_ERROR && command != ZIP_SOURCE_SUPPORTS) { - int e[2]; - - if (_zip_source_call(src, e, sizeof(e), ZIP_SOURCE_ERROR) < 0) { - zip_error_set(&src->error, ZIP_ER_INTERNAL, 0); - } - else { - zip_error_set(&src->error, e[0], e[1]); - } - } + if (command != ZIP_SOURCE_ERROR && command != ZIP_SOURCE_SUPPORTS) { + int e[2]; + + if (_zip_source_call(src, e, sizeof(e), ZIP_SOURCE_ERROR) < 0) { + zip_error_set(&src->error, ZIP_ER_INTERNAL, 0); + } + else { + zip_error_set(&src->error, e[0], e[1]); + } + } } return ret; diff --git a/lib/zip_source_close.c b/lib/zip_source_close.c index 36bc842..cbc3bea 100644 --- a/lib/zip_source_close.c +++ b/lib/zip_source_close.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,13 +36,12 @@ int -zip_source_close(zip_source_t *src) -{ +zip_source_close(zip_source_t *src) { if (!ZIP_SOURCE_IS_OPEN_READING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + src->open_count--; if (src->open_count == 0) { _zip_source_call(src, NULL, 0, ZIP_SOURCE_CLOSE); diff --git a/lib/zip_source_commit_write.c b/lib/zip_source_commit_write.c index ba77abc..26c3dd7 100644 --- a/lib/zip_source_commit_write.c +++ b/lib/zip_source_commit_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,29 +36,28 @@ ZIP_EXTERN int -zip_source_commit_write(zip_source_t *src) -{ +zip_source_commit_write(zip_source_t *src) { if (!ZIP_SOURCE_IS_OPEN_WRITING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + if (src->open_count > 1) { zip_error_set(&src->error, ZIP_ER_INUSE, 0); return -1; } else if (ZIP_SOURCE_IS_OPEN_READING(src)) { - if (zip_source_close(src) < 0) { + if (zip_source_close(src) < 0) { return -1; } } - + if (_zip_source_call(src, NULL, 0, ZIP_SOURCE_COMMIT_WRITE) < 0) { - src->write_state = ZIP_SOURCE_WRITE_FAILED; - return -1; + src->write_state = ZIP_SOURCE_WRITE_FAILED; + return -1; } - + src->write_state = ZIP_SOURCE_WRITE_CLOSED; - + return 0; } diff --git a/lib/zip_source_compress.c b/lib/zip_source_compress.c index 0b97f93..a1ac3e0 100644 --- a/lib/zip_source_compress.c +++ b/lib/zip_source_compress.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -31,9 +31,9 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include -#include #include "zipint.h" @@ -43,14 +43,14 @@ struct context { bool end_of_input; bool end_of_stream; bool can_store; - bool is_stored; /* only valid if end_of_stream is true */ + bool is_stored; /* only valid if end_of_stream is true */ bool compress; zip_int32_t method; - + zip_uint64_t size; zip_int64_t first_read; zip_uint8_t buffer[BUFSIZE]; - + zip_compression_algorithm_t *algorithm; void *ud; }; @@ -63,9 +63,9 @@ struct implementation { }; static struct implementation implementations[] = { - { ZIP_CM_DEFLATE, &zip_algorithm_deflate_compress, &zip_algorithm_deflate_decompress }, + {ZIP_CM_DEFLATE, &zip_algorithm_deflate_compress, &zip_algorithm_deflate_decompress}, #if defined(HAVE_LIBBZ2) - { ZIP_CM_BZIP2, &zip_algorithm_bzip2_compress, &zip_algorithm_bzip2_decompress }, + {ZIP_CM_BZIP2, &zip_algorithm_bzip2_compress, &zip_algorithm_bzip2_decompress}, #endif }; @@ -116,12 +116,11 @@ zip_source_decompress(zip_t *za, zip_source_t *src, zip_int32_t method) { static zip_source_t * -compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags) -{ +compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags) { struct context *ctx; zip_source_t *s2; zip_compression_algorithm_t *algorithm = NULL; - + if (src == NULL) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; @@ -136,7 +135,7 @@ compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool co zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return NULL; } - + if ((s2 = zip_source_layered(za, src, compress_callback, ctx)) == NULL) { context_free(ctx); return NULL; @@ -149,7 +148,7 @@ compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool co static struct context * context_new(zip_int32_t method, bool compress, int compression_flags, zip_compression_algorithm_t *algorithm) { struct context *ctx; - + if ((ctx = (struct context *)malloc(sizeof(*ctx))) == NULL) { return NULL; } @@ -161,13 +160,13 @@ context_new(zip_int32_t method, bool compress, int compression_flags, zip_compre ctx->end_of_input = false; ctx->end_of_stream = false; ctx->is_stored = false; - + if ((ctx->ud = ctx->algorithm->allocate(ZIP_CM_ACTUAL(method), compression_flags, &ctx->error)) == NULL) { zip_error_fini(&ctx->error); free(ctx); return NULL; } - + return ctx; } @@ -186,8 +185,7 @@ context_free(struct context *ctx) { static zip_int64_t -compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t len) -{ +compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t len) { zip_compression_status_t ret; bool end; zip_int64_t n; @@ -197,11 +195,11 @@ compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t l if (zip_error_code_zip(&ctx->error) != ZIP_ER_OK) { return -1; } - + if (len == 0 || ctx->end_of_stream) { return 0; } - + out_offset = 0; end = false; @@ -214,7 +212,7 @@ compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t l } switch (ret) { - case ZIP_COMPRESSION_END: + case ZIP_COMPRESSION_END: ctx->end_of_stream = true; if (!ctx->end_of_input) { @@ -227,15 +225,15 @@ compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t l end = true; break; } - if (ctx->can_store && (zip_uint64_t)ctx->first_read <= out_offset) { - ctx->is_stored = true; - ctx->size = (zip_uint64_t)ctx->first_read; - memcpy(data, ctx->buffer, ctx->size); - return (zip_int64_t)ctx->size; - } + if (ctx->can_store && (zip_uint64_t)ctx->first_read <= out_offset) { + ctx->is_stored = true; + ctx->size = (zip_uint64_t)ctx->first_read; + memcpy(data, ctx->buffer, ctx->size); + return (zip_int64_t)ctx->size; + } end = true; break; - + case ZIP_COMPRESSION_OK: break; @@ -270,7 +268,7 @@ compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t l ctx->algorithm->input(ctx->ud, ctx->buffer, (zip_uint64_t)n); } break; - + case ZIP_COMPRESSION_ERROR: /* error set by algorithm */ if (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) { @@ -292,8 +290,7 @@ compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t l static zip_int64_t -compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct context *ctx; ctx = (struct context *)ud; @@ -305,7 +302,7 @@ compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip ctx->end_of_stream = false; ctx->is_stored = false; ctx->first_read = -1; - + if (!ctx->algorithm->start(ctx->ud)) { return -1; } @@ -321,51 +318,50 @@ compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip } return 0; - case ZIP_SOURCE_STAT: - { - zip_stat_t *st; + case ZIP_SOURCE_STAT: { + zip_stat_t *st; - st = (zip_stat_t *)data; + st = (zip_stat_t *)data; - if (ctx->compress) { - if (ctx->end_of_stream) { - st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_ACTUAL(ctx->method); - st->comp_size = ctx->size; - st->valid |= ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD; - } - else { - st->valid &= ~(ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD); - } + if (ctx->compress) { + if (ctx->end_of_stream) { + st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_ACTUAL(ctx->method); + st->comp_size = ctx->size; + st->valid |= ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD; } else { - st->comp_method = ZIP_CM_STORE; - st->valid |= ZIP_STAT_COMP_METHOD; - if (ctx->end_of_stream) { - st->size = ctx->size; - st->valid |= ZIP_STAT_SIZE; - } - else { - st->valid &= ~ZIP_STAT_SIZE; - } + st->valid &= ~(ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD); } } + else { + st->comp_method = ZIP_CM_STORE; + st->valid |= ZIP_STAT_COMP_METHOD; + if (ctx->end_of_stream) { + st->size = ctx->size; + st->valid |= ZIP_STAT_SIZE; + } + else { + st->valid &= ~ZIP_STAT_SIZE; + } + } + } return 0; case ZIP_SOURCE_GET_COMPRESSION_FLAGS: return ctx->is_stored ? 0 : ctx->algorithm->compression_flags(ctx->ud); case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); + return zip_error_to_data(&ctx->error, data, len); case ZIP_SOURCE_FREE: context_free(ctx); return 0; case ZIP_SOURCE_SUPPORTS: - return ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_GET_COMPRESSION_FLAGS, -1); - + return ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_GET_COMPRESSION_FLAGS, -1); + default: - zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); + zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); return -1; } } diff --git a/lib/zip_source_crc.c b/lib/zip_source_crc.c index 3c9c105..8797dfe 100644 --- a/lib/zip_source_crc.c +++ b/lib/zip_source_crc.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -32,18 +32,18 @@ */ +#include #include #include -#include #include "zipint.h" struct crc_context { - int validate; /* whether to check CRC on EOF and return error on mismatch */ + int validate; /* whether to check CRC on EOF and return error on mismatch */ int crc_complete; /* whether CRC was computed for complete file */ zip_error_t error; zip_uint64_t size; - zip_uint64_t position; /* current reading position */ + zip_uint64_t position; /* current reading position */ zip_uint64_t crc_position; /* how far we've computed the CRC */ zip_uint32_t crc; }; @@ -52,8 +52,7 @@ static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_so zip_source_t * -zip_source_crc(zip_t *za, zip_source_t *src, int validate) -{ +zip_source_crc(zip_t *za, zip_source_t *src, int validate) { struct crc_context *ctx; if (src == NULL) { @@ -61,7 +60,7 @@ zip_source_crc(zip_t *za, zip_source_t *src, int validate) return NULL; } - if ((ctx=(struct crc_context *)malloc(sizeof(*ctx))) == NULL) { + if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return NULL; } @@ -78,125 +77,122 @@ zip_source_crc(zip_t *za, zip_source_t *src, int validate) static zip_int64_t -crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct crc_context *ctx; zip_int64_t n; ctx = (struct crc_context *)_ctx; switch (cmd) { - case ZIP_SOURCE_OPEN: - ctx->position = 0; - return 0; - - case ZIP_SOURCE_READ: - if ((n = zip_source_read(src, data, len)) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - - if (n == 0) { - if (ctx->crc_position == ctx->position) { - ctx->crc_complete = 1; - ctx->size = ctx->position; - - if (ctx->validate) { - struct zip_stat st; - - if (zip_source_stat(src, &st) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - - if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) { - zip_error_set(&ctx->error, ZIP_ER_CRC, 0); - return -1; - } - if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) { - zip_error_set(&ctx->error, ZIP_ER_INCONS, 0); - return -1; - } - } - } - } - else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) { - zip_uint64_t i, nn; - - for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) { - nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n-i); - - ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data+i, (uInt)nn); - ctx->crc_position += nn; + case ZIP_SOURCE_OPEN: + ctx->position = 0; + return 0; + + case ZIP_SOURCE_READ: + if ((n = zip_source_read(src, data, len)) < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + + if (n == 0) { + if (ctx->crc_position == ctx->position) { + ctx->crc_complete = 1; + ctx->size = ctx->position; + + if (ctx->validate) { + struct zip_stat st; + + if (zip_source_stat(src, &st) < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + + if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) { + zip_error_set(&ctx->error, ZIP_ER_CRC, 0); + return -1; + } + if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) { + zip_error_set(&ctx->error, ZIP_ER_INCONS, 0); + return -1; + } } - } - ctx->position += (zip_uint64_t)n; - return n; - - case ZIP_SOURCE_CLOSE: - return 0; - - case ZIP_SOURCE_STAT: - { - zip_stat_t *st; - - st = (zip_stat_t *)data; - - if (ctx->crc_complete) { - /* TODO: Set comp_size, comp_method, encryption_method? - After all, this only works for uncompressed data. */ - st->size = ctx->size; - st->crc = ctx->crc; - st->comp_size = ctx->size; - st->comp_method = ZIP_CM_STORE; - st->encryption_method = ZIP_EM_NONE; - st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;; } - return 0; - } - - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); - - case ZIP_SOURCE_FREE: - free(ctx); - return 0; - - case ZIP_SOURCE_SUPPORTS: - { - zip_int64_t mask = zip_source_supports(src); - - if (mask < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - - return mask & ~zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_GET_COMPRESSION_FLAGS, -1); - } - - case ZIP_SOURCE_SEEK: - { - zip_int64_t new_position; - zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); - - if (args == NULL) { - return -1; + } + else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) { + zip_uint64_t i, nn; + + for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) { + nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i); + + ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn); + ctx->crc_position += nn; } - if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - - ctx->position = (zip_uint64_t)new_position; - - return 0; - } - - case ZIP_SOURCE_TELL: - return (zip_int64_t)ctx->position; - - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + } + ctx->position += (zip_uint64_t)n; + return n; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_STAT: { + zip_stat_t *st; + + st = (zip_stat_t *)data; + + if (ctx->crc_complete) { + /* TODO: Set comp_size, comp_method, encryption_method? + After all, this only works for uncompressed data. */ + st->size = ctx->size; + st->crc = ctx->crc; + st->comp_size = ctx->size; + st->comp_method = ZIP_CM_STORE; + st->encryption_method = ZIP_EM_NONE; + st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; + ; + } + return 0; + } + + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, len); + + case ZIP_SOURCE_FREE: + free(ctx); + return 0; + + case ZIP_SOURCE_SUPPORTS: { + zip_int64_t mask = zip_source_supports(src); + + if (mask < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + + return mask & ~zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_GET_COMPRESSION_FLAGS, -1); + } + + case ZIP_SOURCE_SEEK: { + zip_int64_t new_position; + zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); + + if (args == NULL) { + return -1; + } + if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + + ctx->position = (zip_uint64_t)new_position; + + return 0; + } + + case ZIP_SOURCE_TELL: + return (zip_int64_t)ctx->position; + + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } diff --git a/lib/zip_source_error.c b/lib/zip_source_error.c index 91e2dd6..5145169 100644 --- a/lib/zip_source_error.c +++ b/lib/zip_source_error.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,13 +36,11 @@ zip_error_t * -zip_source_error(zip_source_t *src) -{ +zip_source_error(zip_source_t *src) { return &src->error; } bool -_zip_source_had_error(zip_source_t *src) -{ +_zip_source_had_error(zip_source_t *src) { return zip_source_error(src)->zip_err != ZIP_ER_OK; } diff --git a/lib/zip_source_file.c b/lib/zip_source_file.c index 6a10c01..77376f4 100644 --- a/lib/zip_source_file.c +++ b/lib/zip_source_file.c @@ -42,8 +42,7 @@ #endif ZIP_EXTERN zip_source_t * -zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) -{ +zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -52,8 +51,7 @@ zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t le ZIP_EXTERN zip_source_t * -zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (fname == NULL || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; diff --git a/lib/zip_source_filep.c b/lib/zip_source_filep.c index 32dcdd4..9fc9a01 100644 --- a/lib/zip_source_filep.c +++ b/lib/zip_source_filep.c @@ -31,10 +31,10 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include +#include #include "zipint.h" @@ -45,6 +45,12 @@ #ifdef HAVE_CLONEFILE #include #include +#define CAN_CLONE +#endif +#ifdef HAVE_FICLONERANGE +#include +#include +#define CAN_CLONE #endif #ifdef _WIN32 @@ -54,7 +60,7 @@ /* Windows sys/types.h does not provide these */ #ifndef S_ISREG -#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG) #endif #if defined(S_IXUSR) && defined(S_IRWXG) && defined(S_IRWXO) #define _SAFE_MASK (S_IXUSR | S_IRWXG | S_IRWXO) @@ -70,7 +76,7 @@ typedef int mode_t; #endif struct read_file { - zip_error_t error; /* last error information */ + zip_error_t error; /* last error information */ zip_int64_t supports; /* reading */ @@ -89,7 +95,7 @@ struct read_file { static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd); static int create_temp_output(struct read_file *ctx); -#ifdef HAVE_CLONEFILE +#ifdef CAN_CLONE static zip_int64_t create_temp_output_cloning(struct read_file *ctx, zip_uint64_t offset); #endif static int _zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error); @@ -97,8 +103,7 @@ static int _zip_fseek(FILE *f, zip_int64_t offset, int whence, zip_error_t *erro ZIP_EXTERN zip_source_t * -zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) -{ +zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -107,8 +112,7 @@ zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) ZIP_EXTERN zip_source_t * -zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (file == NULL || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; @@ -119,8 +123,7 @@ zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_ zip_source_t * -_zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_error_t *error) -{ +_zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_error_t *error) { struct read_file *ctx; zip_source_t *zs; struct stat sb; @@ -140,14 +143,14 @@ _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int return NULL; } - if ((ctx=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) { + if ((ctx = (struct read_file *)malloc(sizeof(struct read_file))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } ctx->fname = NULL; if (fname) { - if ((ctx->fname=strdup(fname)) == NULL) { + if ((ctx->fname = strdup(fname)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); free(ctx); return NULL; @@ -158,8 +161,8 @@ _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int ctx->end = (zip_uint64_t)len; if (st) { memcpy(&ctx->st, st, sizeof(ctx->st)); - ctx->st.name = NULL; - ctx->st.valid &= ~ZIP_STAT_NAME; + ctx->st.name = NULL; + ctx->st.valid &= ~ZIP_STAT_NAME; } else { zip_stat_init(&ctx->st); @@ -213,7 +216,7 @@ _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int if (ctx->end == 0) { ctx->st.size = (zip_uint64_t)sb.st_size - ctx->start; ctx->st.valid |= ZIP_STAT_SIZE; - + if (ctx->fname && start == 0) { ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE; } @@ -221,13 +224,13 @@ _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int } } -#ifdef HAVE_CLONEFILE +#ifdef CAN_CLONE if (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE)) { - ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING); + ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING); } #endif - if ((zs=zip_source_function_create(read_file, ctx, error)) == NULL) { + if ((zs = zip_source_function_create(read_file, ctx, error)) == NULL) { free(ctx->fname); free(ctx); return NULL; @@ -238,34 +241,33 @@ _zip_source_file_or_p(const char *fname, FILE *file, zip_uint64_t start, zip_int static int -create_temp_output(struct read_file *ctx) -{ +create_temp_output(struct read_file *ctx) { char *temp; int tfd; mode_t mask; FILE *tfp; - if ((temp=(char *)malloc(strlen(ctx->fname)+8)) == NULL) { + if ((temp = (char *)malloc(strlen(ctx->fname) + 8)) == NULL) { zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); return -1; } sprintf(temp, "%s.XXXXXX", ctx->fname); mask = umask(_SAFE_MASK); - if ((tfd=mkstemp(temp)) == -1) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + if ((tfd = mkstemp(temp)) == -1) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); umask(mask); - free(temp); - return -1; + free(temp); + return -1; } umask(mask); - if ((tfp=fdopen(tfd, "r+b")) == NULL) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); - close(tfd); - (void)remove(temp); - free(temp); - return -1; + if ((tfp = fdopen(tfd, "r+b")) == NULL) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + close(tfd); + (void)remove(temp); + free(temp); + return -1; } #ifdef _WIN32 @@ -273,7 +275,7 @@ create_temp_output(struct read_file *ctx) According to Pierre Joye, Windows in some environments per default creates text files, so force binary mode. */ - _setmode(_fileno(tfp), _O_BINARY ); + _setmode(_fileno(tfp), _O_BINARY); #endif ctx->fout = tfp; @@ -282,47 +284,96 @@ create_temp_output(struct read_file *ctx) return 0; } -#ifdef HAVE_CLONEFILE -zip_int64_t -static create_temp_output_cloning(struct read_file *ctx, zip_uint64_t offset) -{ +#ifdef CAN_CLONE +zip_int64_t static create_temp_output_cloning(struct read_file *ctx, zip_uint64_t offset) { char *temp; FILE *tfp; if (offset > ZIP_OFF_MAX) { - zip_error_set(&ctx->error, ZIP_ER_SEEK, E2BIG); - return -1; + zip_error_set(&ctx->error, ZIP_ER_SEEK, E2BIG); + return -1; } - if ((temp=(char *)malloc(strlen(ctx->fname)+8)) == NULL) { - zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); - return -1; + if ((temp = (char *)malloc(strlen(ctx->fname) + 8)) == NULL) { + zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); + return -1; } sprintf(temp, "%s.XXXXXX", ctx->fname); +#ifdef HAVE_CLONEFILE +#ifndef __clang_analyzer__ + /* we can't use mkstemp, since clonefile insists on creating the file */ if (mktemp(temp) == NULL) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); - free(temp); - return -1; + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + free(temp); + return -1; } +#endif if (clonefile(ctx->fname, temp, 0) < 0) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); - free(temp); - return -1; + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + free(temp); + return -1; } - if ((tfp=fopen(temp, "r+b")) == NULL) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); - (void)remove(temp); - free(temp); - return -1; + if ((tfp = fopen(temp, "r+b")) == NULL) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + (void)remove(temp); + free(temp); + return -1; } - if (ftruncate(fileno(tfp), (off_t)offset) < 0 - || fseeko(tfp, (off_t)offset, SEEK_SET) < 0) { - zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); - (void)remove(temp); - free(temp); - return -1; +#else + { + int fd; + struct file_clone_range range; + struct stat st; + + if (fstat(fileno(ctx->f), &st) < 0) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + return -1; + } + + if ((fd = mkstemp(temp)) < 0) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + free(temp); + return -1; + } + + range.src_fd = fileno(ctx->f); + range.src_offset = 0; + range.src_length = ((offset + st.st_blksize - 1) / st.st_blksize) * st.st_blksize; + if (range.src_length > st.st_size) { + range.src_length = 0; + } + range.dest_offset = 0; + if (ioctl(fd, FICLONERANGE, &range) < 0) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + (void)close(fd); + (void)remove(temp); + free(temp); + return -1; + } + + if ((tfp = fdopen(fd, "r+b")) == NULL) { + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); + (void)close(fd); + (void)remove(temp); + free(temp); + return -1; + } + } +#endif + + if (ftruncate(fileno(tfp), (off_t)offset) < 0) { + (void)fclose(tfp); + (void)remove(temp); + free(temp); + return -1; + } + if (fseeko(tfp, (off_t)offset, SEEK_SET) < 0) { + (void)fclose(tfp); + (void)remove(temp); + free(temp); + zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); } ctx->fout = tfp; @@ -334,8 +385,7 @@ static create_temp_output_cloning(struct read_file *ctx, zip_uint64_t offset) static zip_int64_t -read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct read_file *ctx; char *buf; zip_uint64_t n; @@ -345,244 +395,240 @@ read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) buf = (char *)data; switch (cmd) { - case ZIP_SOURCE_BEGIN_WRITE: - if (ctx->fname == NULL) { - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; - } - return create_temp_output(ctx); + case ZIP_SOURCE_BEGIN_WRITE: + if (ctx->fname == NULL) { + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; + } + return create_temp_output(ctx); -#ifdef HAVE_CLONEFILE - case ZIP_SOURCE_BEGIN_WRITE_CLONING: - if (ctx->fname == NULL) { - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; - } - return create_temp_output_cloning(ctx, len); +#ifdef CAN_CLONE + case ZIP_SOURCE_BEGIN_WRITE_CLONING: + if (ctx->fname == NULL) { + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; + } + return create_temp_output_cloning(ctx, len); #endif - case ZIP_SOURCE_COMMIT_WRITE: { - mode_t mask; - - if (fclose(ctx->fout) < 0) { - ctx->fout = NULL; - zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); - } - ctx->fout = NULL; - if (rename(ctx->tmpname, ctx->fname) < 0) { - zip_error_set(&ctx->error, ZIP_ER_RENAME, errno); - return -1; - } - mask = umask(022); - umask(mask); - /* not much we can do if chmod fails except make the whole commit fail */ - (void)chmod(ctx->fname, 0666&~mask); - free(ctx->tmpname); - ctx->tmpname = NULL; - return 0; + case ZIP_SOURCE_COMMIT_WRITE: { + mode_t mask; + + if (fclose(ctx->fout) < 0) { + ctx->fout = NULL; + zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); + } + ctx->fout = NULL; + if (rename(ctx->tmpname, ctx->fname) < 0) { + zip_error_set(&ctx->error, ZIP_ER_RENAME, errno); + return -1; } + mask = umask(022); + umask(mask); + /* not much we can do if chmod fails except make the whole commit fail */ + (void)chmod(ctx->fname, 0666 & ~mask); + free(ctx->tmpname); + ctx->tmpname = NULL; + return 0; + } + + case ZIP_SOURCE_CLOSE: + if (ctx->fname) { + fclose(ctx->f); + ctx->f = NULL; + } + return 0; + + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, len); - case ZIP_SOURCE_CLOSE: - if (ctx->fname) { - fclose(ctx->f); - ctx->f = NULL; - } - return 0; - - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); - - case ZIP_SOURCE_FREE: - free(ctx->fname); - free(ctx->tmpname); - if (ctx->f) - fclose(ctx->f); - free(ctx); - return 0; - - case ZIP_SOURCE_OPEN: - if (ctx->fname) { - if ((ctx->f=fopen(ctx->fname, "rb")) == NULL) { - zip_error_set(&ctx->error, ZIP_ER_OPEN, errno); - return -1; - } - } - - if (ctx->start > 0) { - if (_zip_fseek_u(ctx->f, ctx->start, SEEK_SET, &ctx->error) < 0) { - /* TODO: skip by reading */ - return -1; - } - } - ctx->current = 0; - return 0; - - case ZIP_SOURCE_READ: - if (ctx->end > 0) { - n = ctx->end - ctx->current; - if (n > len) { - n = len; - } - } - else { - n = len; - } - - if (n > SIZE_MAX) - n = SIZE_MAX; - - if ((i=fread(buf, 1, (size_t)n, ctx->f)) == 0) { - if (ferror(ctx->f)) { - zip_error_set(&ctx->error, ZIP_ER_READ, errno); - return -1; - } - } - ctx->current += i; - - return (zip_int64_t)i; - - case ZIP_SOURCE_REMOVE: - if (remove(ctx->fname) < 0) { - zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno); - return -1; - } - return 0; - - case ZIP_SOURCE_ROLLBACK_WRITE: - if (ctx->fout) { - fclose(ctx->fout); - ctx->fout = NULL; - } - (void)remove(ctx->tmpname); - free(ctx->tmpname); - ctx->tmpname = NULL; - return 0; - - case ZIP_SOURCE_SEEK: { - zip_int64_t new_current; - int need_seek; - zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); - - if (args == NULL) + case ZIP_SOURCE_FREE: + free(ctx->fname); + free(ctx->tmpname); + if (ctx->f) + fclose(ctx->f); + free(ctx); + return 0; + + case ZIP_SOURCE_OPEN: + if (ctx->fname) { + if ((ctx->f = fopen(ctx->fname, "rb")) == NULL) { + zip_error_set(&ctx->error, ZIP_ER_OPEN, errno); return -1; + } + } - need_seek = 1; - - switch (args->whence) { - case SEEK_SET: - new_current = args->offset; - break; - - case SEEK_END: - if (ctx->end == 0) { - if (_zip_fseek(ctx->f, args->offset, SEEK_END, &ctx->error) < 0) { - return -1; - } - if ((new_current = ftello(ctx->f)) < 0) { - zip_error_set(&ctx->error, ZIP_ER_SEEK, errno); - return -1; - } - new_current -= (zip_int64_t)ctx->start; - need_seek = 0; - } - else { - new_current = (zip_int64_t)ctx->end + args->offset; - } - break; - - case SEEK_CUR: - new_current = (zip_int64_t)ctx->current + args->offset; - break; - - default: - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } - - if (new_current < 0 || (ctx->end != 0 && (zip_uint64_t)new_current > ctx->end) - || (zip_uint64_t)new_current + ctx->start < ctx->start) { - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } - - ctx->current = (zip_uint64_t)new_current; - - if (need_seek) { - if (_zip_fseek_u(ctx->f, ctx->current + ctx->start, SEEK_SET, &ctx->error) < 0) { - return -1; - } - } - return 0; - } - - case ZIP_SOURCE_SEEK_WRITE: { - zip_source_args_seek_t *args; - - args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); - if (args == NULL) { - return -1; - } - - if (_zip_fseek(ctx->fout, args->offset, args->whence, &ctx->error) < 0) { - return -1; - } - return 0; - } - - case ZIP_SOURCE_STAT: { - if (len < sizeof(ctx->st)) + if (ctx->start > 0) { + if (_zip_fseek_u(ctx->f, ctx->start, SEEK_SET, &ctx->error) < 0) { + /* TODO: skip by reading */ return -1; + } + } + ctx->current = 0; + return 0; + + case ZIP_SOURCE_READ: + if (ctx->end > 0) { + n = ctx->end - ctx->current; + if (n > len) { + n = len; + } + } + else { + n = len; + } + + if (n > SIZE_MAX) + n = SIZE_MAX; + + if ((i = fread(buf, 1, (size_t)n, ctx->f)) == 0) { + if (ferror(ctx->f)) { + zip_error_set(&ctx->error, ZIP_ER_READ, errno); + return -1; + } + } + ctx->current += i; + + return (zip_int64_t)i; + + case ZIP_SOURCE_REMOVE: + if (remove(ctx->fname) < 0) { + zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno); + return -1; + } + return 0; + + case ZIP_SOURCE_ROLLBACK_WRITE: + if (ctx->fout) { + fclose(ctx->fout); + ctx->fout = NULL; + } + (void)remove(ctx->tmpname); + free(ctx->tmpname); + ctx->tmpname = NULL; + return 0; + + case ZIP_SOURCE_SEEK: { + zip_int64_t new_current; + int need_seek; + zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); + + if (args == NULL) + return -1; + + need_seek = 1; + + switch (args->whence) { + case SEEK_SET: + new_current = args->offset; + break; + + case SEEK_END: + if (ctx->end == 0) { + if (_zip_fseek(ctx->f, args->offset, SEEK_END, &ctx->error) < 0) { + return -1; + } + if ((new_current = ftello(ctx->f)) < 0) { + zip_error_set(&ctx->error, ZIP_ER_SEEK, errno); + return -1; + } + new_current -= (zip_int64_t)ctx->start; + need_seek = 0; + } + else { + new_current = (zip_int64_t)ctx->end + args->offset; + } + break; + + case SEEK_CUR: + new_current = (zip_int64_t)ctx->current + args->offset; + break; + + default: + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; + } + + if (new_current < 0 || (ctx->end != 0 && (zip_uint64_t)new_current > ctx->end) || (zip_uint64_t)new_current + ctx->start < ctx->start) { + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; + } - if (zip_error_code_zip(&ctx->stat_error) != 0) { - zip_error_set(&ctx->error, zip_error_code_zip(&ctx->stat_error), zip_error_code_system(&ctx->stat_error)); + ctx->current = (zip_uint64_t)new_current; + + if (need_seek) { + if (_zip_fseek_u(ctx->f, ctx->current + ctx->start, SEEK_SET, &ctx->error) < 0) { return -1; } + } + return 0; + } - memcpy(data, &ctx->st, sizeof(ctx->st)); - return sizeof(ctx->st); + case ZIP_SOURCE_SEEK_WRITE: { + zip_source_args_seek_t *args; + + args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); + if (args == NULL) { + return -1; } - case ZIP_SOURCE_SUPPORTS: - return ctx->supports; + if (_zip_fseek(ctx->fout, args->offset, args->whence, &ctx->error) < 0) { + return -1; + } + return 0; + } - case ZIP_SOURCE_TELL: - return (zip_int64_t)ctx->current; + case ZIP_SOURCE_STAT: { + if (len < sizeof(ctx->st)) + return -1; - case ZIP_SOURCE_TELL_WRITE: - { - off_t ret = ftello(ctx->fout); + if (zip_error_code_zip(&ctx->stat_error) != 0) { + zip_error_set(&ctx->error, zip_error_code_zip(&ctx->stat_error), zip_error_code_system(&ctx->stat_error)); + return -1; + } + + memcpy(data, &ctx->st, sizeof(ctx->st)); + return sizeof(ctx->st); + } - if (ret < 0) { - zip_error_set(&ctx->error, ZIP_ER_TELL, errno); - return -1; - } - return ret; - } + case ZIP_SOURCE_SUPPORTS: + return ctx->supports; - case ZIP_SOURCE_WRITE: - { - size_t ret; + case ZIP_SOURCE_TELL: + return (zip_int64_t)ctx->current; - clearerr(ctx->fout); - ret = fwrite(data, 1, len, ctx->fout); - if (ret != len || ferror(ctx->fout)) { - zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); - return -1; - } + case ZIP_SOURCE_TELL_WRITE: { + off_t ret = ftello(ctx->fout); + + if (ret < 0) { + zip_error_set(&ctx->error, ZIP_ER_TELL, errno); + return -1; + } + return ret; + } - return (zip_int64_t)ret; - } + case ZIP_SOURCE_WRITE: { + size_t ret; - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + clearerr(ctx->fout); + ret = fwrite(data, 1, len, ctx->fout); + if (ret != len || ferror(ctx->fout)) { + zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); + return -1; + } + + return (zip_int64_t)ret; + } + + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } static int -_zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error) -{ +_zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error) { if (offset > ZIP_INT64_MAX) { zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW); return -1; @@ -592,8 +638,7 @@ _zip_fseek_u(FILE *f, zip_uint64_t offset, int whence, zip_error_t *error) static int -_zip_fseek(FILE *f, zip_int64_t offset, int whence, zip_error_t *error) -{ +_zip_fseek(FILE *f, zip_int64_t offset, int whence, zip_error_t *error) { if (offset > ZIP_FSEEK_MAX || offset < ZIP_FSEEK_MIN) { zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW); return -1; diff --git a/lib/zip_source_free.c b/lib/zip_source_free.c index 9070469..7cb3525 100644 --- a/lib/zip_source_free.c +++ b/lib/zip_source_free.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,34 +38,33 @@ ZIP_EXTERN void -zip_source_free(zip_source_t *src) -{ +zip_source_free(zip_source_t *src) { if (src == NULL) return; if (src->refcount > 0) { - src->refcount--; + src->refcount--; } if (src->refcount > 0) { - return; + return; } - + if (ZIP_SOURCE_IS_OPEN_READING(src)) { src->open_count = 1; /* force close */ zip_source_close(src); } if (ZIP_SOURCE_IS_OPEN_WRITING(src)) { - zip_source_rollback_write(src); + zip_source_rollback_write(src); } - + if (src->source_archive && !src->source_closed) { - _zip_deregister_source(src->source_archive, src); + _zip_deregister_source(src->source_archive, src); } - + (void)_zip_source_call(src, NULL, 0, ZIP_SOURCE_FREE); - + if (src->src) { - zip_source_free(src->src); + zip_source_free(src->src); } free(src); diff --git a/lib/zip_source_function.c b/lib/zip_source_function.c index 3d4604c..56eb1c5 100644 --- a/lib/zip_source_function.c +++ b/lib/zip_source_function.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,50 +38,46 @@ ZIP_EXTERN zip_source_t * -zip_source_function(zip_t *za, zip_source_callback zcb, void *ud) -{ +zip_source_function(zip_t *za, zip_source_callback zcb, void *ud) { if (za == NULL) { - return NULL; + return NULL; } - + return zip_source_function_create(zcb, ud, &za->error); } ZIP_EXTERN zip_source_t * -zip_source_function_create(zip_source_callback zcb, void *ud, zip_error_t *error) -{ +zip_source_function_create(zip_source_callback zcb, void *ud, zip_error_t *error) { zip_source_t *zs; - if ((zs=_zip_source_new(error)) == NULL) + if ((zs = _zip_source_new(error)) == NULL) return NULL; zs->cb.f = zcb; zs->ud = ud; - + zs->supports = zcb(ud, NULL, 0, ZIP_SOURCE_SUPPORTS); if (zs->supports < 0) { - zs->supports = ZIP_SOURCE_SUPPORTS_READABLE; + zs->supports = ZIP_SOURCE_SUPPORTS_READABLE; } - + return zs; } ZIP_EXTERN void -zip_source_keep(zip_source_t *src) -{ +zip_source_keep(zip_source_t *src) { src->refcount++; } zip_source_t * -_zip_source_new(zip_error_t *error) -{ +_zip_source_new(zip_error_t *error) { zip_source_t *src; - if ((src=(zip_source_t *)malloc(sizeof(*src))) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); + if ((src = (zip_source_t *)malloc(sizeof(*src))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } diff --git a/lib/zip_source_get_compression_flags.c b/lib/zip_source_get_compression_flags.c index b5bae82..e33e43b 100644 --- a/lib/zip_source_get_compression_flags.c +++ b/lib/zip_source_get_compression_flags.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,11 +34,10 @@ #include "zipint.h" -#define ZIP_COMPRESSION_BITFLAG_MAX 3 +#define ZIP_COMPRESSION_BITFLAG_MAX 3 zip_int8_t -zip_source_get_compression_flags(zip_source_t *src) -{ +zip_source_get_compression_flags(zip_source_t *src) { while (src) { if ((src->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_GET_COMPRESSION_FLAGS))) { zip_int64_t ret = _zip_source_call(src, NULL, 0, ZIP_SOURCE_GET_COMPRESSION_FLAGS); diff --git a/lib/zip_source_is_deleted.c b/lib/zip_source_is_deleted.c index e50cdd9..1e5d343 100644 --- a/lib/zip_source_is_deleted.c +++ b/lib/zip_source_is_deleted.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,7 +36,6 @@ ZIP_EXTERN int -zip_source_is_deleted(zip_source_t *src) -{ +zip_source_is_deleted(zip_source_t *src) { return src->write_state == ZIP_SOURCE_WRITE_REMOVED; } diff --git a/lib/zip_source_layered.c b/lib/zip_source_layered.c index 94b3310..5e95bc1 100644 --- a/lib/zip_source_layered.c +++ b/lib/zip_source_layered.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,23 +38,21 @@ zip_source_t * -zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud) -{ +zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud) { if (za == NULL) - return NULL; + return NULL; return zip_source_layered_create(src, cb, ud, &za->error); } zip_source_t * -zip_source_layered_create(zip_source_t *src, zip_source_layered_callback cb, void *ud, zip_error_t *error) -{ +zip_source_layered_create(zip_source_t *src, zip_source_layered_callback cb, void *ud, zip_error_t *error) { zip_source_t *zs; - - if ((zs=_zip_source_new(error)) == NULL) - return NULL; - + + if ((zs = _zip_source_new(error)) == NULL) + return NULL; + zip_source_keep(src); zs->src = src; zs->cb.l = cb; @@ -62,7 +60,7 @@ zip_source_layered_create(zip_source_t *src, zip_source_layered_callback cb, voi zs->supports = cb(src, ud, NULL, 0, ZIP_SOURCE_SUPPORTS); if (zs->supports < 0) { - zs->supports = ZIP_SOURCE_SUPPORTS_READABLE; + zs->supports = ZIP_SOURCE_SUPPORTS_READABLE; } return zs; diff --git a/lib/zip_source_open.c b/lib/zip_source_open.c index ec01b0d..187001c 100644 --- a/lib/zip_source_open.c +++ b/lib/zip_source_open.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,13 +35,12 @@ #include "zipint.h" ZIP_EXTERN int -zip_source_open(zip_source_t *src) -{ +zip_source_open(zip_source_t *src) { if (src->source_closed) { - return -1; + return -1; } if (src->write_state == ZIP_SOURCE_WRITE_REMOVED) { - zip_error_set(&src->error, ZIP_ER_DELETED, 0); + zip_error_set(&src->error, ZIP_ER_DELETED, 0); return -1; } @@ -58,7 +57,7 @@ zip_source_open(zip_source_t *src) return -1; } } - + if (_zip_source_call(src, NULL, 0, ZIP_SOURCE_OPEN) < 0) { if (ZIP_SOURCE_IS_LAYERED(src)) { zip_source_close(src->src); @@ -71,6 +70,6 @@ zip_source_open(zip_source_t *src) src->had_read_error = false; _zip_error_clear(&src->error); src->open_count++; - + return 0; } diff --git a/lib/zip_source_pkware.c b/lib/zip_source_pkware.c index 3957d99..b466da4 100644 --- a/lib/zip_source_pkware.c +++ b/lib/zip_source_pkware.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -42,24 +42,20 @@ struct trad_pkware { zip_uint32_t key[3]; }; -#define HEADERLEN 12 -#define KEY0 305419896 -#define KEY1 591751049 -#define KEY2 878082192 +#define HEADERLEN 12 +#define KEY0 305419896 +#define KEY1 591751049 +#define KEY2 878082192 -static void decrypt(struct trad_pkware *, zip_uint8_t *, - const zip_uint8_t *, zip_uint64_t, int); +static void decrypt(struct trad_pkware *, zip_uint8_t *, const zip_uint8_t *, zip_uint64_t, int); static int decrypt_header(zip_source_t *, struct trad_pkware *); -static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *, - zip_uint64_t, zip_source_cmd_t); +static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); static void pkware_free(struct trad_pkware *); zip_source_t * -zip_source_pkware(zip_t *za, zip_source_t *src, - zip_uint16_t em, int flags, const char *password) -{ +zip_source_pkware(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) { struct trad_pkware *ctx; zip_source_t *s2; @@ -72,7 +68,7 @@ zip_source_pkware(zip_t *za, zip_source_t *src, return NULL; } - if ((ctx=(struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) { + if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) { zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return NULL; } @@ -84,7 +80,7 @@ zip_source_pkware(zip_t *za, zip_source_t *src, ctx->key[2] = KEY2; decrypt(ctx, NULL, (const zip_uint8_t *)password, strlen(password), 1); - if ((s2=zip_source_layered(za, src, pkware_decrypt, ctx)) == NULL) { + if ((s2 = zip_source_layered(za, src, pkware_decrypt, ctx)) == NULL) { pkware_free(ctx); return NULL; } @@ -94,14 +90,12 @@ zip_source_pkware(zip_t *za, zip_source_t *src, static void -decrypt(struct trad_pkware *ctx, zip_uint8_t *out, const zip_uint8_t *in, - zip_uint64_t len, int update_only) -{ +decrypt(struct trad_pkware *ctx, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len, int update_only) { zip_uint16_t tmp; zip_uint64_t i; Bytef b; - for (i=0; ierror, src); + if ((n = zip_source_read(src, header, HEADERLEN)) < 0) { + _zip_error_set_from_source(&ctx->error, src); return -1; } - + if (n != HEADERLEN) { - zip_error_set(&ctx->error, ZIP_ER_EOF, 0); + zip_error_set(&ctx->error, ZIP_ER_EOF, 0); return -1; } @@ -151,8 +144,8 @@ decrypt_header(zip_source_t *src, struct trad_pkware *ctx) _zip_u2d_time(st.mtime, &dostime, &dosdate); - if (header[HEADERLEN-1] != st.crc>>24 && header[HEADERLEN-1] != dostime>>8) { - zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0); + if (header[HEADERLEN - 1] != st.crc >> 24 && header[HEADERLEN - 1] != dostime >> 8) { + zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0); return -1; } @@ -161,66 +154,62 @@ decrypt_header(zip_source_t *src, struct trad_pkware *ctx) static zip_int64_t -pkware_decrypt(zip_source_t *src, void *ud, void *data, - zip_uint64_t len, zip_source_cmd_t cmd) -{ +pkware_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct trad_pkware *ctx; zip_int64_t n; ctx = (struct trad_pkware *)ud; switch (cmd) { - case ZIP_SOURCE_OPEN: - if (decrypt_header(src, ctx) < 0) - return -1; - return 0; - - case ZIP_SOURCE_READ: - if ((n=zip_source_read(src, data, len)) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - - decrypt((struct trad_pkware *)ud, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n, 0); - return n; - - case ZIP_SOURCE_CLOSE: - return 0; - - case ZIP_SOURCE_STAT: - { - zip_stat_t *st; - - st = (zip_stat_t *)data; - - st->encryption_method = ZIP_EM_NONE; - st->valid |= ZIP_STAT_ENCRYPTION_METHOD; - /* TODO: deduce HEADERLEN from size for uncompressed */ - if (st->valid & ZIP_STAT_COMP_SIZE) - st->comp_size -= HEADERLEN; - - return 0; - } - - case ZIP_SOURCE_SUPPORTS: - return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1); - - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); - - case ZIP_SOURCE_FREE: - pkware_free(ctx); - return 0; - - default: - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; + case ZIP_SOURCE_OPEN: + if (decrypt_header(src, ctx) < 0) + return -1; + return 0; + + case ZIP_SOURCE_READ: + if ((n = zip_source_read(src, data, len)) < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + + decrypt((struct trad_pkware *)ud, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n, 0); + return n; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_STAT: { + zip_stat_t *st; + + st = (zip_stat_t *)data; + + st->encryption_method = ZIP_EM_NONE; + st->valid |= ZIP_STAT_ENCRYPTION_METHOD; + /* TODO: deduce HEADERLEN from size for uncompressed */ + if (st->valid & ZIP_STAT_COMP_SIZE) + st->comp_size -= HEADERLEN; + + return 0; + } + + case ZIP_SOURCE_SUPPORTS: + return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1); + + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, len); + + case ZIP_SOURCE_FREE: + pkware_free(ctx); + return 0; + + default: + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; } } static void -pkware_free(struct trad_pkware *ctx) -{ +pkware_free(struct trad_pkware *ctx) { free(ctx); } diff --git a/lib/zip_source_read.c b/lib/zip_source_read.c index 5288bf2..83514c6 100644 --- a/lib/zip_source_read.c +++ b/lib/zip_source_read.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,16 +36,15 @@ zip_int64_t -zip_source_read(zip_source_t *src, void *data, zip_uint64_t len) -{ +zip_source_read(zip_source_t *src, void *data, zip_uint64_t len) { zip_uint64_t bytes_read; zip_int64_t n; if (src->source_closed) { - return -1; + return -1; } if (!ZIP_SOURCE_IS_OPEN_READING(src) || len > ZIP_INT64_MAX || (len > 0 && data == NULL)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); + zip_error_set(&src->error, ZIP_ER_INVAL, 0); return -1; } @@ -57,10 +56,14 @@ zip_source_read(zip_source_t *src, void *data, zip_uint64_t len) return 0; } + if (len == 0) { + return 0; + } + bytes_read = 0; while (bytes_read < len) { if ((n = _zip_source_call(src, (zip_uint8_t *)data + bytes_read, len - bytes_read, ZIP_SOURCE_READ)) < 0) { - src->had_read_error = true; + src->had_read_error = true; if (bytes_read == 0) { return -1; } @@ -82,7 +85,6 @@ zip_source_read(zip_source_t *src, void *data, zip_uint64_t len) bool -_zip_source_eof(zip_source_t *src) -{ +_zip_source_eof(zip_source_t *src) { return src->eof; } diff --git a/lib/zip_source_remove.c b/lib/zip_source_remove.c index 470a5ed..8e01e24 100644 --- a/lib/zip_source_remove.c +++ b/lib/zip_source_remove.c @@ -1,10 +1,10 @@ /* zip_source_remove.c -- remove empty archive Copyright (C) 2014 Dieter Baron and Thomas Klausner - + This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,26 +36,25 @@ int -zip_source_remove(zip_source_t *src) -{ +zip_source_remove(zip_source_t *src) { if (src->write_state == ZIP_SOURCE_WRITE_REMOVED) { - return 0; + return 0; } - + if (ZIP_SOURCE_IS_OPEN_READING(src)) { if (zip_source_close(src) < 0) { return -1; } } if (src->write_state != ZIP_SOURCE_WRITE_CLOSED) { - zip_source_rollback_write(src); + zip_source_rollback_write(src); } - + if (_zip_source_call(src, NULL, 0, ZIP_SOURCE_REMOVE) < 0) { - return -1; + return -1; } - + src->write_state = ZIP_SOURCE_WRITE_REMOVED; - + return 0; } diff --git a/lib/zip_source_rollback_write.c b/lib/zip_source_rollback_write.c index c35f30f..bbccaf8 100644 --- a/lib/zip_source_rollback_write.c +++ b/lib/zip_source_rollback_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,12 +36,11 @@ ZIP_EXTERN void -zip_source_rollback_write(zip_source_t *src) -{ +zip_source_rollback_write(zip_source_t *src) { if (src->write_state != ZIP_SOURCE_WRITE_OPEN && src->write_state != ZIP_SOURCE_WRITE_FAILED) { return; } - + _zip_source_call(src, NULL, 0, ZIP_SOURCE_ROLLBACK_WRITE); src->write_state = ZIP_SOURCE_WRITE_CLOSED; } diff --git a/lib/zip_source_seek.c b/lib/zip_source_seek.c index c3f4703..aed53b8 100644 --- a/lib/zip_source_seek.c +++ b/lib/zip_source_seek.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,57 +36,55 @@ ZIP_EXTERN int -zip_source_seek(zip_source_t *src, zip_int64_t offset, int whence) -{ +zip_source_seek(zip_source_t *src, zip_int64_t offset, int whence) { zip_source_args_seek_t args; - + if (src->source_closed) { - return -1; + return -1; } if (!ZIP_SOURCE_IS_OPEN_READING(src) || (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } args.offset = offset; args.whence = whence; - + return (_zip_source_call(src, &args, sizeof(args), ZIP_SOURCE_SEEK) < 0 ? -1 : 0); } zip_int64_t -zip_source_seek_compute_offset(zip_uint64_t offset, zip_uint64_t length, void *data, zip_uint64_t data_length, zip_error_t *error) -{ +zip_source_seek_compute_offset(zip_uint64_t offset, zip_uint64_t length, void *data, zip_uint64_t data_length, zip_error_t *error) { zip_int64_t new_offset; zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, data_length, error); - + if (args == NULL) { - return -1; + return -1; } - + switch (args->whence) { - case SEEK_CUR: - new_offset = (zip_int64_t)offset + args->offset; - break; - - case SEEK_END: - new_offset = (zip_int64_t)length + args->offset; - break; - - case SEEK_SET: - new_offset = args->offset; - break; - - default: - zip_error_set(error, ZIP_ER_INVAL, 0); - return -1; + case SEEK_CUR: + new_offset = (zip_int64_t)offset + args->offset; + break; + + case SEEK_END: + new_offset = (zip_int64_t)length + args->offset; + break; + + case SEEK_SET: + new_offset = args->offset; + break; + + default: + zip_error_set(error, ZIP_ER_INVAL, 0); + return -1; } - + if (new_offset < 0 || (zip_uint64_t)new_offset > length) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(error, ZIP_ER_INVAL, 0); + return -1; } - + return new_offset; } diff --git a/lib/zip_source_seek_write.c b/lib/zip_source_seek_write.c index 6660766..4ecbee1 100644 --- a/lib/zip_source_seek_write.c +++ b/lib/zip_source_seek_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,17 +36,16 @@ ZIP_EXTERN int -zip_source_seek_write(zip_source_t *src, zip_int64_t offset, int whence) -{ +zip_source_seek_write(zip_source_t *src, zip_int64_t offset, int whence) { zip_source_args_seek_t args; - + if (!ZIP_SOURCE_IS_OPEN_WRITING(src) || (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + args.offset = offset; args.whence = whence; - + return (_zip_source_call(src, &args, sizeof(args), ZIP_SOURCE_SEEK_WRITE) < 0 ? -1 : 0); } diff --git a/lib/zip_source_stat.c b/lib/zip_source_stat.c index 864507e..987c862 100644 --- a/lib/zip_source_stat.c +++ b/lib/zip_source_stat.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,23 +36,22 @@ ZIP_EXTERN int -zip_source_stat(zip_source_t *src, zip_stat_t *st) -{ +zip_source_stat(zip_source_t *src, zip_stat_t *st) { if (src->source_closed) { - return -1; + return -1; } if (st == NULL) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); + zip_error_set(&src->error, ZIP_ER_INVAL, 0); return -1; } zip_stat_init(st); - + if (ZIP_SOURCE_IS_LAYERED(src)) { - if (zip_source_stat(src->src, st) < 0) { - _zip_error_set_from_source(&src->error, src->src); - return -1; - } + if (zip_source_stat(src->src, st) < 0) { + _zip_error_set_from_source(&src->error, src->src); + return -1; + } } if (_zip_source_call(src, st, sizeof(*st), ZIP_SOURCE_STAT) < 0) { diff --git a/lib/zip_source_supports.c b/lib/zip_source_supports.c index c59aa1f..a47f293 100644 --- a/lib/zip_source_supports.c +++ b/lib/zip_source_supports.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,31 +38,28 @@ zip_int64_t -zip_source_supports(zip_source_t *src) -{ +zip_source_supports(zip_source_t *src) { return src->supports; } ZIP_EXTERN zip_int64_t -zip_source_make_command_bitmap(zip_source_cmd_t cmd0, ...) -{ +zip_source_make_command_bitmap(zip_source_cmd_t cmd0, ...) { zip_int64_t bitmap; va_list ap; - + bitmap = ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd0); - - - + + va_start(ap, cmd0); for (;;) { - int cmd = va_arg(ap, int); - if (cmd < 0) { - break; - } - bitmap |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd); + int cmd = va_arg(ap, int); + if (cmd < 0) { + break; + } + bitmap |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd); } va_end(ap); - + return bitmap; } diff --git a/lib/zip_source_tell.c b/lib/zip_source_tell.c index f1c10b5..ab418fa 100644 --- a/lib/zip_source_tell.c +++ b/lib/zip_source_tell.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,15 +36,14 @@ ZIP_EXTERN zip_int64_t -zip_source_tell(zip_source_t *src) -{ +zip_source_tell(zip_source_t *src) { if (src->source_closed) { - return -1; + return -1; } if (!ZIP_SOURCE_IS_OPEN_READING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + return _zip_source_call(src, NULL, 0, ZIP_SOURCE_TELL); } diff --git a/lib/zip_source_tell_write.c b/lib/zip_source_tell_write.c index 2fa1507..f7a8a3f 100644 --- a/lib/zip_source_tell_write.c +++ b/lib/zip_source_tell_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,12 +36,11 @@ ZIP_EXTERN zip_int64_t -zip_source_tell_write(zip_source_t *src) -{ +zip_source_tell_write(zip_source_t *src) { if (!ZIP_SOURCE_IS_OPEN_WRITING(src)) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + return _zip_source_call(src, NULL, 0, ZIP_SOURCE_TELL_WRITE); } diff --git a/lib/zip_source_win32a.c b/lib/zip_source_win32a.c index de0bfb5..064d9b2 100644 --- a/lib/zip_source_win32a.c +++ b/lib/zip_source_win32a.c @@ -1,48 +1,50 @@ /* -zip_source_win32a.c -- create data source from Windows file (ANSI) -Copyright (C) 1999-2017 Dieter Baron and Thomas Klausner - -This file is part of libzip, a library to manipulate ZIP archives. -The authors can be contacted at - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in -the documentation and/or other materials provided with the -distribution. -3. The names of the authors may not be used to endorse or promote -products derived from this software without specific prior -written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS -OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + zip_source_win32a.c -- create data source from Windows file (ANSI) + Copyright (C) 1999-2017 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include +#include #include "zipint.h" #include "zipwin32.h" -static void * _win32_strdup_a(const void *str); +static void *_win32_strdup_a(const void *str); static HANDLE _win32_open_a(_zip_source_win32_read_file_t *ctx); static HANDLE _win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa); static int _win32_rename_temp_a(_zip_source_win32_read_file_t *ctx); static int _win32_remove_a(const void *fname); +// clang-format off static _zip_source_win32_file_ops_t win32_ops_a = { _win32_strdup_a, _win32_open_a, @@ -50,10 +52,10 @@ static _zip_source_win32_file_ops_t win32_ops_a = { _win32_rename_temp_a, _win32_remove_a }; +// clang-format on ZIP_EXTERN zip_source_t * -zip_source_win32a(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) -{ +zip_source_win32a(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -62,8 +64,7 @@ zip_source_win32a(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t ZIP_EXTERN zip_source_t * -zip_source_win32a_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_win32a_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (fname == NULL || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; @@ -74,22 +75,19 @@ zip_source_win32a_create(const char *fname, zip_uint64_t start, zip_int64_t leng static void * -_win32_strdup_a(const void *str) -{ +_win32_strdup_a(const void *str) { return strdup((const char *)str); } static HANDLE -_win32_open_a(_zip_source_win32_read_file_t *ctx) -{ +_win32_open_a(_zip_source_win32_read_file_t *ctx) { return CreateFileA(ctx->fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } static HANDLE -_win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa) -{ +_win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa) { size_t len; len = strlen((const char *)ctx->fname) + 10; @@ -108,8 +106,7 @@ _win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32 static int -_win32_rename_temp_a(_zip_source_win32_read_file_t *ctx) -{ +_win32_rename_temp_a(_zip_source_win32_read_file_t *ctx) { if (!MoveFileExA(ctx->tmpname, ctx->fname, MOVEFILE_REPLACE_EXISTING)) return -1; return 0; @@ -117,8 +114,7 @@ _win32_rename_temp_a(_zip_source_win32_read_file_t *ctx) static int -_win32_remove_a(const void *fname) -{ +_win32_remove_a(const void *fname) { DeleteFileA((const char *)fname); return 0; } diff --git a/lib/zip_source_win32handle.c b/lib/zip_source_win32handle.c index 543593b..8cef919 100644 --- a/lib/zip_source_win32handle.c +++ b/lib/zip_source_win32handle.c @@ -1,41 +1,41 @@ /* -zip_source_win32file.c -- create data source from HANDLE (Win32) -Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner - -This file is part of libzip, a library to manipulate ZIP archives. -The authors can be contacted at - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in -the documentation and/or other materials provided with the -distribution. -3. The names of the authors may not be used to endorse or promote -products derived from this software without specific prior -written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS -OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + zip_source_win32file.c -- create data source from HANDLE (Win32) + Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include #include -#include +#include #include "zipint.h" #include "zipwin32.h" @@ -49,8 +49,7 @@ static int _zip_win32_error_to_errno(unsigned long win32err); static int _zip_stat_win32(void *h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx); ZIP_EXTERN zip_source_t * -zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len) -{ +zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -59,8 +58,7 @@ zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len) ZIP_EXTERN zip_source_t * -zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (h == INVALID_HANDLE_VALUE || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; @@ -71,8 +69,7 @@ zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_source_t * -_zip_source_win32_handle_or_name(const void *fname, HANDLE h, zip_uint64_t start, zip_int64_t len, int closep, const zip_stat_t *st, _zip_source_win32_file_ops_t *ops, zip_error_t *error) -{ +_zip_source_win32_handle_or_name(const void *fname, HANDLE h, zip_uint64_t start, zip_int64_t len, int closep, const zip_stat_t *st, _zip_source_win32_file_ops_t *ops, zip_error_t *error) { _zip_source_win32_read_file_t *ctx; zip_source_t *zs; @@ -141,8 +138,7 @@ _zip_source_win32_handle_or_name(const void *fname, HANDLE h, zip_uint64_t start static zip_int64_t -_win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +_win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { _zip_source_win32_read_file_t *ctx; char *buf; zip_uint64_t n; @@ -348,8 +344,8 @@ _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd if (h == INVALID_HANDLE_VALUE) { win32err = GetLastError(); if (win32err == ERROR_FILE_NOT_FOUND || win32err == ERROR_PATH_NOT_FOUND) { - zip_error_set(&ctx->error, ZIP_ER_READ, ENOENT); - return -1; + zip_error_set(&ctx->error, ZIP_ER_READ, ENOENT); + return -1; } } } @@ -377,8 +373,7 @@ _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd case ZIP_SOURCE_TELL: return (zip_int64_t)ctx->current; - case ZIP_SOURCE_TELL_WRITE: - { + case ZIP_SOURCE_TELL_WRITE: { LARGE_INTEGER zero; LARGE_INTEGER offset; @@ -391,8 +386,7 @@ _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd return offset.QuadPart; } - case ZIP_SOURCE_WRITE: - { + case ZIP_SOURCE_WRITE: { DWORD ret; if (!WriteFile(ctx->hout, data, (DWORD)len, &ret, NULL) || ret != len) { zip_error_set(&ctx->error, ZIP_ER_WRITE, _zip_win32_error_to_errno(GetLastError())); @@ -410,8 +404,7 @@ _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd static int -_win32_create_temp_file(_zip_source_win32_read_file_t *ctx) -{ +_win32_create_temp_file(_zip_source_win32_read_file_t *ctx) { zip_uint32_t value; /* Windows has GetTempFileName(), but it closes the file after @@ -473,8 +466,7 @@ _win32_create_temp_file(_zip_source_win32_read_file_t *ctx) static int -_zip_seek_win32_u(HANDLE h, zip_uint64_t offset, int whence, zip_error_t *error) -{ +_zip_seek_win32_u(HANDLE h, zip_uint64_t offset, int whence, zip_error_t *error) { if (offset > ZIP_INT64_MAX) { zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW); return -1; @@ -484,8 +476,7 @@ _zip_seek_win32_u(HANDLE h, zip_uint64_t offset, int whence, zip_error_t *error) static int -_zip_seek_win32(HANDLE h, zip_int64_t offset, int whence, zip_error_t *error) -{ +_zip_seek_win32(HANDLE h, zip_int64_t offset, int whence, zip_error_t *error) { LARGE_INTEGER li; DWORD method; @@ -515,8 +506,7 @@ _zip_seek_win32(HANDLE h, zip_int64_t offset, int whence, zip_error_t *error) static int -_zip_win32_error_to_errno(DWORD win32err) -{ +_zip_win32_error_to_errno(DWORD win32err) { /* Note: This list isn't exhaustive, but should cover common cases. */ @@ -542,8 +532,7 @@ _zip_win32_error_to_errno(DWORD win32err) static int -_zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx) -{ +_zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx) { FILETIME mtimeft; time_t mtime; LARGE_INTEGER size; @@ -585,8 +574,7 @@ _zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx) static int -_zip_filetime_to_time_t(FILETIME ft, time_t *t) -{ +_zip_filetime_to_time_t(FILETIME ft, time_t *t) { /* Inspired by http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux */ diff --git a/lib/zip_source_win32utf8.c b/lib/zip_source_win32utf8.c index 004c66a..1087f7a 100644 --- a/lib/zip_source_win32utf8.c +++ b/lib/zip_source_win32utf8.c @@ -1,46 +1,45 @@ /* -zip_source_win32utf8.c -- create data source from Windows file (UTF-8) -Copyright (C) 1999-2015 Dieter Baron and Thomas Klausner + zip_source_win32utf8.c -- create data source from Windows file (UTF-8) + Copyright (C) 1999-2015 Dieter Baron and Thomas Klausner -This file is part of libzip, a library to manipulate ZIP archives. -The authors can be contacted at + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in -the documentation and/or other materials provided with the -distribution. -3. The names of the authors may not be used to endorse or promote -products derived from this software without specific prior -written permission. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. -THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS -OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include "zipint.h" #include "zipwin32.h" ZIP_EXTERN zip_source_t * -zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) -{ +zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -49,8 +48,7 @@ zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t le ZIP_EXTERN zip_source_t * -zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { int size; wchar_t *wfname; zip_source_t *source; diff --git a/lib/zip_source_win32w.c b/lib/zip_source_win32w.c index 685b57f..dcf310c 100644 --- a/lib/zip_source_win32w.c +++ b/lib/zip_source_win32w.c @@ -33,16 +33,18 @@ #include +#include #include "zipint.h" #include "zipwin32.h" -static void * _win32_strdup_w(const void *str); +static void *_win32_strdup_w(const void *str); static HANDLE _win32_open_w(_zip_source_win32_read_file_t *ctx); static HANDLE _win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa); static int _win32_rename_temp_w(_zip_source_win32_read_file_t *ctx); static int _win32_remove_w(const void *fname); +// clang-format off static _zip_source_win32_file_ops_t win32_ops_w = { _win32_strdup_w, _win32_open_w, @@ -50,10 +52,10 @@ static _zip_source_win32_file_ops_t win32_ops_w = { _win32_rename_temp_w, _win32_remove_w }; +// clang-format on ZIP_EXTERN zip_source_t * -zip_source_win32w(zip_t *za, const wchar_t *fname, zip_uint64_t start, zip_int64_t len) -{ +zip_source_win32w(zip_t *za, const wchar_t *fname, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; @@ -62,8 +64,7 @@ zip_source_win32w(zip_t *za, const wchar_t *fname, zip_uint64_t start, zip_int64 ZIP_EXTERN zip_source_t * -zip_source_win32w_create(const wchar_t *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) -{ +zip_source_win32w_create(const wchar_t *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (fname == NULL || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; @@ -74,17 +75,15 @@ zip_source_win32w_create(const wchar_t *fname, zip_uint64_t start, zip_int64_t l static void * -_win32_strdup_w(const void *str) -{ +_win32_strdup_w(const void *str) { return _wcsdup((const wchar_t *)str); } static HANDLE -_win32_open_w(_zip_source_win32_read_file_t *ctx) -{ +_win32_open_w(_zip_source_win32_read_file_t *ctx) { #ifdef MS_UWP - CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 }; + CREATEFILE2_EXTENDED_PARAMETERS extParams = {0}; extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; extParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS; extParams.dwSecurityQosFlags = SECURITY_ANONYMOUS; @@ -100,8 +99,7 @@ _win32_open_w(_zip_source_win32_read_file_t *ctx) static HANDLE -_win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa) -{ +_win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa) { size_t len; len = wcslen((const wchar_t *)ctx->fname) + 10; @@ -116,7 +114,7 @@ _win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32 } #ifdef MS_UWP - CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 }; + CREATEFILE2_EXTENDED_PARAMETERS extParams = {0}; extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY; extParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS; extParams.dwSecurityQosFlags = SECURITY_ANONYMOUS; @@ -132,8 +130,7 @@ _win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32 static int -_win32_rename_temp_w(_zip_source_win32_read_file_t *ctx) -{ +_win32_rename_temp_w(_zip_source_win32_read_file_t *ctx) { if (!MoveFileExW(ctx->tmpname, ctx->fname, MOVEFILE_REPLACE_EXISTING)) return -1; return 0; @@ -141,8 +138,7 @@ _win32_rename_temp_w(_zip_source_win32_read_file_t *ctx) static int -_win32_remove_w(const void *fname) -{ +_win32_remove_w(const void *fname) { DeleteFileW((const wchar_t *)fname); return 0; } diff --git a/lib/zip_source_window.c b/lib/zip_source_window.c index 3980370..f4701a0 100644 --- a/lib/zip_source_window.c +++ b/lib/zip_source_window.c @@ -38,14 +38,14 @@ #include "zipint.h" struct window { - zip_uint64_t start; /* where in file we start reading */ - zip_uint64_t end; /* where in file we stop reading */ + zip_uint64_t start; /* where in file we start reading */ + zip_uint64_t end; /* where in file we stop reading */ /* if not NULL, read file data for this file */ zip_t *source_archive; zip_uint64_t source_index; - zip_uint64_t offset; /* offset in src for next read */ + zip_uint64_t offset; /* offset in src for next read */ zip_stat_t stat; zip_int8_t compression_flags; @@ -58,25 +58,23 @@ static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip zip_source_t * -zip_source_window(zip_t *za, zip_source_t *src, zip_uint64_t start, zip_uint64_t len) -{ +zip_source_window(zip_t *za, zip_source_t *src, zip_uint64_t start, zip_uint64_t len) { return _zip_source_window_new(src, start, len, NULL, 0, NULL, 0, &za->error); } zip_source_t * -_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t length, zip_stat_t *st, zip_int8_t compression_flags, zip_t *source_archive, zip_uint64_t source_index, zip_error_t *error) -{ +_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t length, zip_stat_t *st, zip_int8_t compression_flags, zip_t *source_archive, zip_uint64_t source_index, zip_error_t *error) { struct window *ctx; if (src == NULL || start + length < start || (source_archive == NULL && source_index != 0)) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; } - if ((ctx=(struct window *)malloc(sizeof(*ctx))) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + if ((ctx = (struct window *)malloc(sizeof(*ctx))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } ctx->start = start; @@ -90,10 +88,10 @@ _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t lengt ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false; if (st) { - if (_zip_stat_merge(&ctx->stat, st, error) < 0) { - free(ctx); - return NULL; - } + if (_zip_stat_merge(&ctx->stat, st, error) < 0) { + free(ctx); + return NULL; + } } return zip_source_layered_create(src, window_read, ctx, error); @@ -101,8 +99,7 @@ _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t lengt int -_zip_source_set_source_archive(zip_source_t *src, zip_t *za) -{ +_zip_source_set_source_archive(zip_source_t *src, zip_t *za) { src->source_archive = za; return _zip_register_source(za, src); } @@ -110,19 +107,17 @@ _zip_source_set_source_archive(zip_source_t *src, zip_t *za) /* called by zip_discard to avoid operating on file from closed archive */ void -_zip_source_invalidate(zip_source_t *src) -{ +_zip_source_invalidate(zip_source_t *src) { src->source_closed = 1; if (zip_error_code_zip(&src->error) == ZIP_ER_OK) { - zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0); + zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0); } } static zip_int64_t -window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct window *ctx; zip_int64_t ret; zip_uint64_t n, i; @@ -131,149 +126,144 @@ window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_sou ctx = (struct window *)_ctx; switch (cmd) { - case ZIP_SOURCE_CLOSE: - return 0; + case ZIP_SOURCE_CLOSE: + return 0; - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, len); + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, len); - case ZIP_SOURCE_FREE: - free(ctx); - return 0; + case ZIP_SOURCE_FREE: + free(ctx); + return 0; - case ZIP_SOURCE_OPEN: - if (ctx->source_archive) { - zip_uint64_t offset; + case ZIP_SOURCE_OPEN: + if (ctx->source_archive) { + zip_uint64_t offset; - if ((offset = _zip_file_get_offset(ctx->source_archive, ctx->source_index, &ctx->error)) == 0) { + if ((offset = _zip_file_get_offset(ctx->source_archive, ctx->source_index, &ctx->error)) == 0) { + return -1; + } + if (ctx->end + offset < ctx->end) { + /* zip archive data claims end of data past zip64 limits */ + zip_error_set(&ctx->error, ZIP_ER_INCONS, 0); + return -1; + } + ctx->start += offset; + ctx->end += offset; + ctx->source_archive = NULL; + } + + if (!ctx->needs_seek) { + for (n = 0; n < ctx->start; n += (zip_uint64_t)ret) { + i = (ctx->start - n > sizeof(b) ? sizeof(b) : ctx->start - n); + if ((ret = zip_source_read(src, b, i)) < 0) { + _zip_error_set_from_source(&ctx->error, src); return -1; } - if (ctx->end + offset < ctx->end) { - /* zip archive data claims end of data past zip64 limits */ - zip_error_set(&ctx->error, ZIP_ER_INCONS, 0); + if (ret == 0) { + zip_error_set(&ctx->error, ZIP_ER_EOF, 0); return -1; } - ctx->start += offset; - ctx->end += offset; - ctx->source_archive = NULL; } + } + ctx->offset = ctx->start; + return 0; + + case ZIP_SOURCE_READ: + if (len > ctx->end - ctx->offset) + len = ctx->end - ctx->offset; + + if (len == 0) + return 0; + + if (ctx->needs_seek) { + if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) { + _zip_error_set_from_source(&ctx->error, src); + return -1; + } + } + + if ((ret = zip_source_read(src, data, len)) < 0) { + zip_error_set(&ctx->error, ZIP_ER_EOF, 0); + return -1; + } + + ctx->offset += (zip_uint64_t)ret; + + if (ret == 0) { + if (ctx->offset < ctx->end) { + zip_error_set(&ctx->error, ZIP_ER_EOF, 0); + return -1; + } + } + return ret; + + case ZIP_SOURCE_SEEK: { + zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, ctx->end - ctx->start, data, len, &ctx->error); + + if (new_offset < 0) { + return -1; + } + + ctx->offset = (zip_uint64_t)new_offset + ctx->start; + return 0; + } + + case ZIP_SOURCE_STAT: { + zip_stat_t *st; + + st = (zip_stat_t *)data; + + if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) { + return -1; + } + return 0; + } + + case ZIP_SOURCE_GET_COMPRESSION_FLAGS: + return ctx->compression_flags; + + case ZIP_SOURCE_SUPPORTS: + return ctx->supports; + + case ZIP_SOURCE_TELL: + return (zip_int64_t)(ctx->offset - ctx->start); - if (!ctx->needs_seek) { - for (n=0; nstart; n+=(zip_uint64_t)ret) { - i = (ctx->start-n > sizeof(b) ? sizeof(b) : ctx->start-n); - if ((ret=zip_source_read(src, b, i)) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - if (ret==0) { - zip_error_set(&ctx->error, ZIP_ER_EOF, 0); - return -1; - } - } - - } - ctx->offset = ctx->start; - return 0; - - case ZIP_SOURCE_READ: - if (len > ctx->end - ctx->offset) - len = ctx->end - ctx->offset; - - if (len == 0) - return 0; - - if (ctx->needs_seek) { - if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) { - _zip_error_set_from_source(&ctx->error, src); - return -1; - } - } - - if ((ret=zip_source_read(src, data, len)) < 0) { - zip_error_set(&ctx->error, ZIP_ER_EOF, 0); - return -1; - } - - ctx->offset += (zip_uint64_t)ret; - - if (ret == 0) { - if (ctx->offset < ctx->end) { - zip_error_set(&ctx->error, ZIP_ER_EOF, 0); - return -1; - } - } - return ret; - - case ZIP_SOURCE_SEEK: - { - zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, ctx->end - ctx->start, data, len, &ctx->error); - - if (new_offset < 0) { - return -1; - } - - ctx->offset = (zip_uint64_t)new_offset + ctx->start; - return 0; - } - - case ZIP_SOURCE_STAT: - { - zip_stat_t *st; - - st = (zip_stat_t *)data; - - if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) { - return -1; - } - return 0; - } - - case ZIP_SOURCE_GET_COMPRESSION_FLAGS: - return ctx->compression_flags; - - case ZIP_SOURCE_SUPPORTS: - return ctx->supports; - - case ZIP_SOURCE_TELL: - return (zip_int64_t)(ctx->offset - ctx->start); - - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } void -_zip_deregister_source(zip_t *za, zip_source_t *src) -{ +_zip_deregister_source(zip_t *za, zip_source_t *src) { unsigned int i; - for (i=0; inopen_source; i++) { - if (za->open_source[i] == src) { - za->open_source[i] = za->open_source[za->nopen_source-1]; - za->nopen_source--; - break; - } + for (i = 0; i < za->nopen_source; i++) { + if (za->open_source[i] == src) { + za->open_source[i] = za->open_source[za->nopen_source - 1]; + za->nopen_source--; + break; + } } } int -_zip_register_source(zip_t *za, zip_source_t *src) -{ +_zip_register_source(zip_t *za, zip_source_t *src) { zip_source_t **open_source; - if (za->nopen_source+1 >= za->nopen_source_alloc) { - unsigned int n; - n = za->nopen_source_alloc + 10; - open_source = (zip_source_t **)realloc(za->open_source, n*sizeof(zip_source_t *)); - if (open_source == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); - return -1; - } - za->nopen_source_alloc = n; - za->open_source = open_source; + if (za->nopen_source + 1 >= za->nopen_source_alloc) { + unsigned int n; + n = za->nopen_source_alloc + 10; + open_source = (zip_source_t **)realloc(za->open_source, n * sizeof(zip_source_t *)); + if (open_source == NULL) { + zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + za->nopen_source_alloc = n; + za->open_source = open_source; } za->open_source[za->nopen_source++] = src; diff --git a/lib/zip_source_winzip_aes_decode.c b/lib/zip_source_winzip_aes_decode.c index ddee1c8..cd76593 100644 --- a/lib/zip_source_winzip_aes_decode.c +++ b/lib/zip_source_winzip_aes_decode.c @@ -1,5 +1,5 @@ /* - zip_source_winzip_aes.c -- Winzip AES de/encryption routines + zip_source_winzip_aes_decode.c -- Winzip AES decryption routines Copyright (C) 2009-2017 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,21 +37,14 @@ #include "zipint.h" -#include "gladman-fcrypt.h" - -#define MAX_HEADER_LENGTH (16+PWD_VER_LENGTH) -#define HMAC_LENGTH 10 - -static unsigned int salt_length[] = { 0, 8, 12, 16 }; - struct winzip_aes { char *password; - unsigned int mode; + zip_uint16_t encryption_method; zip_uint64_t data_length; zip_uint64_t current_position; - fcrypt_ctx fcrypt_ctx; + zip_winzip_aes_t *aes_ctx; zip_error_t error; }; @@ -59,31 +52,17 @@ struct winzip_aes { static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx); static void winzip_aes_free(struct winzip_aes *); static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd); -static struct winzip_aes * winzip_aes_new(unsigned int mode, const char *password); +static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error); zip_source_t * -zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) -{ +zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) { zip_source_t *s2; - unsigned int mode = 0; zip_stat_t st; zip_uint64_t aux_length; struct winzip_aes *ctx; - switch (em) { - case ZIP_EM_AES_128: - mode = 1; - break; - case ZIP_EM_AES_192: - mode = 2; - break; - case ZIP_EM_AES_256: - mode = 3; - break; - } - - if (password == NULL || src == NULL || mode == 0) { + if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; } @@ -92,25 +71,19 @@ zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int return NULL; } - if (strlen(password) > UINT_MAX) { - zip_error_set(&za->error, ZIP_ER_INVAL, 0); /* TODO: better error code? (password too long) */ - return NULL; - } - if (zip_source_stat(src, &st) != 0) { _zip_error_set_from_source(&za->error, src); return NULL; } - aux_length = PWD_VER_LENGTH + salt_length[mode] + HMAC_LENGTH; - + aux_length = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(encryption_method) + HMAC_LENGTH; + if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < aux_length) { zip_error_set(&za->error, ZIP_ER_OPNOTSUPP, 0); return NULL; } - if ((ctx = winzip_aes_new(mode, password)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) { return NULL; } @@ -126,29 +99,29 @@ zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int static int -decrypt_header(zip_source_t *src, struct winzip_aes *ctx) -{ - zip_uint8_t header[MAX_HEADER_LENGTH]; - zip_uint8_t password_verification[PWD_VER_LENGTH]; +decrypt_header(zip_source_t *src, struct winzip_aes *ctx) { + zip_uint8_t header[WINZIP_AES_MAX_HEADER_LENGTH]; + zip_uint8_t password_verification[WINZIP_AES_PASSWORD_VERIFY_LENGTH]; unsigned int headerlen; zip_int64_t n; - headerlen = PWD_VER_LENGTH + salt_length[ctx->mode]; - if ((n=zip_source_read(src, header, headerlen)) < 0) { + headerlen = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method); + if ((n = zip_source_read(src, header, headerlen)) < 0) { _zip_error_set_from_source(&ctx->error, src); return -1; } - + if (n != headerlen) { zip_error_set(&ctx->error, ZIP_ER_EOF, 0); return -1; } - if (_zip_fcrypt_init(ctx->mode, (unsigned char *)ctx->password, (unsigned int)strlen(ctx->password), header, password_verification, &ctx->fcrypt_ctx) != 0) { - zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); + if ((ctx->aes_ctx = _zip_winzip_aes_new((zip_uint8_t *)ctx->password, strlen(ctx->password), header, ctx->encryption_method, password_verification, &ctx->error)) == NULL) { return -1; } - if (memcmp(password_verification, header + salt_length[ctx->mode], PWD_VER_LENGTH) != 0) { + if (memcmp(password_verification, header + SALT_LENGTH(ctx->encryption_method), WINZIP_AES_PASSWORD_VERIFY_LENGTH) != 0) { + _zip_winzip_aes_free(ctx->aes_ctx); + ctx->aes_ctx = NULL; zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0); return -1; } @@ -157,16 +130,20 @@ decrypt_header(zip_source_t *src, struct winzip_aes *ctx) static bool -verify_hmac(zip_source_t *src, struct winzip_aes *ctx) -{ - unsigned char computed[HMAC_LENGTH], from_file[HMAC_LENGTH]; +verify_hmac(zip_source_t *src, struct winzip_aes *ctx) { + unsigned char computed[SHA1_LENGTH], from_file[HMAC_LENGTH]; if (zip_source_read(src, from_file, HMAC_LENGTH) < HMAC_LENGTH) { _zip_error_set_from_source(&ctx->error, src); return false; } - _zip_fcrypt_end(computed, &ctx->fcrypt_ctx); - + if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) { + zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); + return false; + } + _zip_winzip_aes_free(ctx->aes_ctx); + ctx->aes_ctx = NULL; + if (memcmp(from_file, computed, HMAC_LENGTH) != 0) { zip_error_set(&ctx->error, ZIP_ER_CRC, 0); return false; @@ -177,11 +154,9 @@ verify_hmac(zip_source_t *src, struct winzip_aes *ctx) static zip_int64_t -winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) -{ +winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { struct winzip_aes *ctx; zip_int64_t n; - zip_uint64_t total, offset; ctx = (struct winzip_aes *)ud; @@ -204,25 +179,24 @@ winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zi } return 0; } - - if ((n=zip_source_read(src, data, len)) < 0) { + + if ((n = zip_source_read(src, data, len)) < 0) { _zip_error_set_from_source(&ctx->error, src); return -1; } ctx->current_position += (zip_uint64_t)n; - total = (zip_uint64_t)n; - for (offset = 0; offset < total; offset += ZIP_MIN(total - offset, UINT_MAX)) { - _zip_fcrypt_decrypt((zip_uint8_t *)data + offset, (unsigned int)ZIP_MIN(total - offset, UINT_MAX), &ctx->fcrypt_ctx); - } + if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) { + zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); + return -1; + } return n; case ZIP_SOURCE_CLOSE: return 0; - case ZIP_SOURCE_STAT: - { + case ZIP_SOURCE_STAT: { zip_stat_t *st; st = (zip_stat_t *)data; @@ -230,12 +204,12 @@ winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zi st->encryption_method = ZIP_EM_NONE; st->valid |= ZIP_STAT_ENCRYPTION_METHOD; if (st->valid & ZIP_STAT_COMP_SIZE) { - st->comp_size -= 12 + salt_length[ctx->mode]; + st->comp_size -= 12 + SALT_LENGTH(ctx->encryption_method); } - + return 0; } - + case ZIP_SOURCE_SUPPORTS: return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1); @@ -254,34 +228,36 @@ winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zi static void -winzip_aes_free(struct winzip_aes *ctx) -{ +winzip_aes_free(struct winzip_aes *ctx) { if (ctx == NULL) { return; } - _zip_crypto_clear(&ctx->fcrypt_ctx, sizeof(ctx->fcrypt_ctx)); _zip_crypto_clear(ctx->password, strlen(ctx->password)); free(ctx->password); zip_error_fini(&ctx->error); + _zip_winzip_aes_free(ctx->aes_ctx); free(ctx); } static struct winzip_aes * -winzip_aes_new(unsigned int mode, const char *password) { +winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) { struct winzip_aes *ctx; - + if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } - + if ((ctx->password = strdup(password)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); free(ctx); return NULL; } - ctx->mode = mode; + ctx->encryption_method = encryption_method; + ctx->aes_ctx = NULL; zip_error_init(&ctx->error); diff --git a/lib/zip_source_winzip_aes_encode.c b/lib/zip_source_winzip_aes_encode.c index d2fe2c2..12ed1b2 100644 --- a/lib/zip_source_winzip_aes_encode.c +++ b/lib/zip_source_winzip_aes_encode.c @@ -1,5 +1,5 @@ /* - zip_source_winzip_aes.c -- Winzip AES de/encryption routines + zip_source_winzip_aes_encode.c -- Winzip AES encryption routines Copyright (C) 2009-2017 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,22 +37,15 @@ #include "zipint.h" -#include "gladman-fcrypt.h" - -#define MAX_HEADER_LENGTH (16+PWD_VER_LENGTH) -#define HMAC_LENGTH 10 - -static unsigned int salt_length[] = { 0, 8, 12, 16 }; struct winzip_aes { char *password; - unsigned int mode; zip_uint16_t encryption_method; - zip_uint8_t data[MAX_HEADER_LENGTH]; + zip_uint8_t data[ZIP_MAX(WINZIP_AES_MAX_HEADER_LENGTH, SHA1_LENGTH)]; zip_buffer_t *buffer; - fcrypt_ctx fcrypt_ctx; + zip_winzip_aes_t *aes_ctx; bool eof; zip_error_t error; }; @@ -61,40 +54,20 @@ struct winzip_aes { static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx); static void winzip_aes_free(struct winzip_aes *); static zip_int64_t winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd); -static struct winzip_aes * winzip_aes_new(unsigned int mode, zip_uint16_t encryption_method, const char *password); +static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error); zip_source_t * -zip_source_winzip_aes_encode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) -{ +zip_source_winzip_aes_encode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) { zip_source_t *s2; - unsigned int mode = 0; struct winzip_aes *ctx; - switch (encryption_method) { - case ZIP_EM_AES_128: - mode = 1; - break; - case ZIP_EM_AES_192: - mode = 2; - break; - case ZIP_EM_AES_256: - mode = 3; - break; - } - - if (password == NULL || src == NULL || mode == 0) { + if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; } - if (strlen(password) > UINT_MAX) { - zip_error_set(&za->error, ZIP_ER_INVAL, 0); /* TODO: better error code? (password too long) */ - return NULL; - } - - if ((ctx = winzip_aes_new(mode, encryption_method, password)) == NULL) { - zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) { return NULL; } @@ -108,33 +81,33 @@ zip_source_winzip_aes_encode(zip_t *za, zip_source_t *src, zip_uint16_t encrypti static int -encrypt_header(zip_source_t *src, struct winzip_aes *ctx) -{ - if (!zip_random(ctx->data, (zip_uint16_t)salt_length[ctx->mode])) { +encrypt_header(zip_source_t *src, struct winzip_aes *ctx) { + zip_uint16_t salt_length = SALT_LENGTH(ctx->encryption_method); + if (!zip_random(ctx->data, salt_length)) { zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); return -1; - } - - if (_zip_fcrypt_init(ctx->mode, (unsigned char *)ctx->password, (unsigned int)strlen(ctx->password), ctx->data, ctx->data+salt_length[ctx->mode], &ctx->fcrypt_ctx) != 0) { - zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); + } + + if ((ctx->aes_ctx = _zip_winzip_aes_new((zip_uint8_t *)ctx->password, strlen(ctx->password), ctx->data, ctx->encryption_method, ctx->data + salt_length, &ctx->error)) == NULL) { return -1; } - if ((ctx->buffer = _zip_buffer_new(ctx->data, salt_length[ctx->mode] + PWD_VER_LENGTH)) == NULL) { + if ((ctx->buffer = _zip_buffer_new(ctx->data, salt_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) == NULL) { + _zip_winzip_aes_free(ctx->aes_ctx); + ctx->aes_ctx = NULL; zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); return -1; } - + return 0; } static zip_int64_t -winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip_source_cmd_t cmd) -{ +winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip_source_cmd_t cmd) { struct winzip_aes *ctx; zip_int64_t ret; - zip_uint64_t buffer_n, offset, n; + zip_uint64_t buffer_n; ctx = (struct winzip_aes *)ud; @@ -148,13 +121,13 @@ winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, case ZIP_SOURCE_READ: buffer_n = 0; - + if (ctx->buffer) { buffer_n = _zip_buffer_read(ctx->buffer, data, length); data = (zip_uint8_t *)data + buffer_n; length -= buffer_n; - + if (_zip_buffer_eof(ctx->buffer)) { _zip_buffer_free(ctx->buffer); ctx->buffer = NULL; @@ -170,14 +143,21 @@ winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, return -1; } - n = (zip_uint64_t)ret; - for (offset = 0; offset < n; offset += ZIP_MIN(n - offset, UINT_MAX)) { - _zip_fcrypt_encrypt((zip_uint8_t *)data + offset, (unsigned int)ZIP_MIN(n - offset, UINT_MAX), &ctx->fcrypt_ctx); - } + if (!_zip_winzip_aes_encrypt(ctx->aes_ctx, data, (zip_uint64_t)ret)) { + zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); + /* TODO: return partial read? */ + return -1; + } - if (n < length) { + if ((zip_uint64_t)ret < length) { ctx->eof = true; - _zip_fcrypt_end(ctx->data, &ctx->fcrypt_ctx); + if (!_zip_winzip_aes_finish(ctx->aes_ctx, ctx->data)) { + zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); + /* TODO: return partial read? */ + return -1; + } + _zip_winzip_aes_free(ctx->aes_ctx); + ctx->aes_ctx = NULL; if ((ctx->buffer = _zip_buffer_new(ctx->data, HMAC_LENGTH)) == NULL) { zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); /* TODO: return partial read? */ @@ -185,25 +165,24 @@ winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, } } - return (zip_int64_t)(buffer_n + n); + return (zip_int64_t)(buffer_n + (zip_uint64_t)ret); case ZIP_SOURCE_CLOSE: return 0; - case ZIP_SOURCE_STAT: - { + case ZIP_SOURCE_STAT: { zip_stat_t *st; st = (zip_stat_t *)data; st->encryption_method = ctx->encryption_method; st->valid |= ZIP_STAT_ENCRYPTION_METHOD; if (st->valid & ZIP_STAT_COMP_SIZE) { - st->comp_size += 12 + salt_length[ctx->mode]; + st->comp_size += 12 + SALT_LENGTH(ctx->encryption_method); } - + return 0; } - + case ZIP_SOURCE_SUPPORTS: return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1); @@ -222,37 +201,38 @@ winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, static void -winzip_aes_free(struct winzip_aes *ctx) -{ +winzip_aes_free(struct winzip_aes *ctx) { if (ctx == NULL) { return; } - _zip_crypto_clear(&ctx->fcrypt_ctx, sizeof(ctx->fcrypt_ctx)); _zip_crypto_clear(ctx->password, strlen(ctx->password)); free(ctx->password); zip_error_fini(&ctx->error); _zip_buffer_free(ctx->buffer); + _zip_winzip_aes_free(ctx->aes_ctx); free(ctx); } static struct winzip_aes * -winzip_aes_new(unsigned int mode, zip_uint16_t encryption_method, const char *password) { +winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) { struct winzip_aes *ctx; - + if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } - + if ((ctx->password = strdup(password)) == NULL) { free(ctx); + zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } - ctx->mode = mode; ctx->encryption_method = encryption_method; ctx->buffer = NULL; + ctx->aes_ctx = NULL; zip_error_init(&ctx->error); diff --git a/lib/zip_source_write.c b/lib/zip_source_write.c index c98f567..1646e33 100644 --- a/lib/zip_source_write.c +++ b/lib/zip_source_write.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,12 +36,11 @@ ZIP_EXTERN zip_int64_t -zip_source_write(zip_source_t *src, const void *data, zip_uint64_t length) -{ +zip_source_write(zip_source_t *src, const void *data, zip_uint64_t length) { if (!ZIP_SOURCE_IS_OPEN_WRITING(src) || length > ZIP_INT64_MAX) { - zip_error_set(&src->error, ZIP_ER_INVAL, 0); - return -1; + zip_error_set(&src->error, ZIP_ER_INVAL, 0); + return -1; } - + return _zip_source_call(src, (void *)data, length, ZIP_SOURCE_WRITE); } diff --git a/lib/zip_source_zip.c b/lib/zip_source_zip.c index e172ca2..b3f1805 100644 --- a/lib/zip_source_zip.c +++ b/lib/zip_source_zip.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,17 +39,15 @@ ZIP_EXTERN zip_source_t * -zip_source_zip(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, - zip_flags_t flags, zip_uint64_t start, zip_int64_t len) -{ +zip_source_zip(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_int64_t len) { if (len < -1) { - zip_error_set(&za->error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; } - + if (len == -1) len = 0; - + if (start == 0 && len == 0) flags |= ZIP_FL_COMPRESSED; else diff --git a/lib/zip_source_zip_new.c b/lib/zip_source_zip_new.c index 8a080e1..a5cfee3 100644 --- a/lib/zip_source_zip_new.c +++ b/lib/zip_source_zip_new.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,8 +38,7 @@ zip_source_t * -_zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password) -{ +_zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password) { zip_source_t *src, *s2; struct zip_stat st; bool partial_data, needs_crc, needs_decrypt, needs_decompress; @@ -52,13 +51,12 @@ _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t fl return NULL; } - if ((flags & ZIP_FL_UNCHANGED) == 0 - && (ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx) || srcza->entry[srcidx].deleted)) { + if ((flags & ZIP_FL_UNCHANGED) == 0 && (ZIP_ENTRY_DATA_CHANGED(srcza->entry + srcidx) || srcza->entry[srcidx].deleted)) { zip_error_set(&za->error, ZIP_ER_CHANGED, 0); return NULL; } - if (zip_stat_index(srcza, srcidx, flags|ZIP_FL_UNCHANGED, &st) < 0) { + if (zip_stat_index(srcza, srcidx, flags | ZIP_FL_UNCHANGED, &st) < 0) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return NULL; } @@ -86,11 +84,11 @@ _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t fl needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && (st.comp_method != ZIP_CM_STORE); /* when reading the whole file, check for CRC errors */ needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || st.comp_method == ZIP_CM_STORE) && !partial_data; - + if (needs_decrypt) { - if (password == NULL) { - password = za->default_password; - } + if (password == NULL) { + password = za->default_password; + } if (password == NULL) { zip_error_set(&za->error, ZIP_ER_NOPASSWD, 0); return NULL; @@ -103,20 +101,20 @@ _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t fl if (partial_data && !needs_decrypt && !needs_decompress) { struct zip_stat st2; - + st2.size = len; st2.comp_size = len; st2.comp_method = ZIP_CM_STORE; st2.mtime = st.mtime; - st2.valid = ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_MTIME; - + st2.valid = ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_MTIME; + if ((src = _zip_source_window_new(srcza->src, start, len, &st2, 0, srcza, srcidx, &za->error)) == NULL) { return NULL; } } else { zip_dirent_t *de; - + if ((de = _zip_get_dirent(srcza, srcidx, flags, &za->error)) == NULL) { return NULL; } @@ -124,17 +122,17 @@ _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t fl return NULL; } } - + if (_zip_source_set_source_archive(src, srcza) < 0) { zip_source_free(src); return NULL; } /* creating a layered source calls zip_keep() on the lower layer, so we free it */ - + if (needs_decrypt) { zip_encryption_implementation enc_impl; - + if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); return NULL; diff --git a/lib/zip_stat.c b/lib/zip_stat.c index cf8e566..02cca37 100644 --- a/lib/zip_stat.c +++ b/lib/zip_stat.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,11 +36,10 @@ ZIP_EXTERN int -zip_stat(zip_t *za, const char *fname, zip_flags_t flags, zip_stat_t *st) -{ +zip_stat(zip_t *za, const char *fname, zip_flags_t flags, zip_stat_t *st) { zip_int64_t idx; - if ((idx=zip_name_locate(za, fname, flags)) < 0) + if ((idx = zip_name_locate(za, fname, flags)) < 0) return -1; return zip_stat_index(za, (zip_uint64_t)idx, flags, st); diff --git a/lib/zip_stat_index.c b/lib/zip_stat_index.c index a2ef59b..109c118 100644 --- a/lib/zip_stat_index.c +++ b/lib/zip_stat_index.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,21 +36,18 @@ ZIP_EXTERN int -zip_stat_index(zip_t *za, zip_uint64_t index, zip_flags_t flags, - zip_stat_t *st) -{ +zip_stat_index(zip_t *za, zip_uint64_t index, zip_flags_t flags, zip_stat_t *st) { const char *name; zip_dirent_t *de; - if ((de=_zip_get_dirent(za, index, flags, NULL)) == NULL) + if ((de = _zip_get_dirent(za, index, flags, NULL)) == NULL) return -1; - if ((name=zip_get_name(za, index, flags)) == NULL) + if ((name = zip_get_name(za, index, flags)) == NULL) return -1; - - if ((flags & ZIP_FL_UNCHANGED) == 0 - && ZIP_ENTRY_DATA_CHANGED(za->entry+index)) { + + if ((flags & ZIP_FL_UNCHANGED) == 0 && ZIP_ENTRY_DATA_CHANGED(za->entry + index)) { if (zip_source_stat(za->entry[index].source, st) < 0) { zip_error_set(&za->error, ZIP_ER_CHANGED, 0); return -1; @@ -65,13 +62,12 @@ zip_stat_index(zip_t *za, zip_uint64_t index, zip_flags_t flags, st->comp_size = de->comp_size; st->comp_method = (zip_uint16_t)de->comp_method; st->encryption_method = de->encryption_method; - st->valid = (de->crc_valid ? ZIP_STAT_CRC : 0) | ZIP_STAT_SIZE|ZIP_STAT_MTIME - |ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD; + st->valid = (de->crc_valid ? ZIP_STAT_CRC : 0) | ZIP_STAT_SIZE | ZIP_STAT_MTIME | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; } st->index = index; st->name = name; - st->valid |= ZIP_STAT_INDEX|ZIP_STAT_NAME; - + st->valid |= ZIP_STAT_INDEX | ZIP_STAT_NAME; + return 0; } diff --git a/lib/zip_stat_init.c b/lib/zip_stat_init.c index 6b7d633..0be5138 100644 --- a/lib/zip_stat_init.c +++ b/lib/zip_stat_init.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -37,8 +37,7 @@ ZIP_EXTERN void -zip_stat_init(zip_stat_t *st) -{ +zip_stat_init(zip_stat_t *st) { st->valid = 0; st->name = NULL; st->index = ZIP_UINT64_MAX; @@ -52,34 +51,33 @@ zip_stat_init(zip_stat_t *st) int -_zip_stat_merge(zip_stat_t *dst, const zip_stat_t *src, zip_error_t *error) -{ +_zip_stat_merge(zip_stat_t *dst, const zip_stat_t *src, zip_error_t *error) { /* name is not merged, since zip_stat_t doesn't own it, and src may not be valid as long as dst */ if (src->valid & ZIP_STAT_INDEX) { - dst->index = src->index; + dst->index = src->index; } if (src->valid & ZIP_STAT_SIZE) { - dst->size = src->size; + dst->size = src->size; } if (src->valid & ZIP_STAT_COMP_SIZE) { - dst->comp_size = src->comp_size; + dst->comp_size = src->comp_size; } if (src->valid & ZIP_STAT_MTIME) { - dst->mtime = src->mtime; + dst->mtime = src->mtime; } if (src->valid & ZIP_STAT_CRC) { - dst->crc = src->crc; + dst->crc = src->crc; } if (src->valid & ZIP_STAT_COMP_METHOD) { - dst->comp_method = src->comp_method; + dst->comp_method = src->comp_method; } if (src->valid & ZIP_STAT_ENCRYPTION_METHOD) { - dst->encryption_method = src->encryption_method; + dst->encryption_method = src->encryption_method; } if (src->valid & ZIP_STAT_FLAGS) { - dst->flags = src->flags; + dst->flags = src->flags; } dst->valid |= src->valid; - + return 0; } diff --git a/lib/zip_strerror.c b/lib/zip_strerror.c index 98c4f6b..9e6e86c 100644 --- a/lib/zip_strerror.c +++ b/lib/zip_strerror.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -36,7 +36,6 @@ ZIP_EXTERN const char * -zip_strerror(zip_t *za) -{ +zip_strerror(zip_t *za) { return zip_error_strerror(&za->error); } diff --git a/lib/zip_string.c b/lib/zip_string.c index 307a425..293766c 100644 --- a/lib/zip_string.c +++ b/lib/zip_string.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,13 +39,12 @@ zip_uint32_t -_zip_string_crc32(const zip_string_t *s) -{ +_zip_string_crc32(const zip_string_t *s) { zip_uint32_t crc; - + crc = (zip_uint32_t)crc32(0L, Z_NULL, 0); - if (s != NULL) + if (s != NULL) crc = (zip_uint32_t)crc32(crc, s->raw, s->length); return crc; @@ -53,8 +52,7 @@ _zip_string_crc32(const zip_string_t *s) int -_zip_string_equal(const zip_string_t *a, const zip_string_t *b) -{ +_zip_string_equal(const zip_string_t *a, const zip_string_t *b) { if (a == NULL || b == NULL) return a == b; @@ -68,8 +66,7 @@ _zip_string_equal(const zip_string_t *a, const zip_string_t *b) void -_zip_string_free(zip_string_t *s) -{ +_zip_string_free(zip_string_t *s) { if (s == NULL) return; @@ -80,8 +77,7 @@ _zip_string_free(zip_string_t *s) const zip_uint8_t * -_zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) -{ +_zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) { static const zip_uint8_t empty[1] = ""; if (string == NULL) { @@ -95,12 +91,9 @@ _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip if (string->encoding == ZIP_ENCODING_UNKNOWN) _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN); - if (((flags & ZIP_FL_ENC_STRICT) - && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) - || (string->encoding == ZIP_ENCODING_CP437)) { + if (((flags & ZIP_FL_ENC_STRICT) && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) || (string->encoding == ZIP_ENCODING_CP437)) { if (string->converted == NULL) { - if ((string->converted=_zip_cp437_to_utf8(string->raw, string->length, - &string->converted_length, error)) == NULL) + if ((string->converted = _zip_cp437_to_utf8(string->raw, string->length, &string->converted_length, error)) == NULL) return NULL; } if (lenp) @@ -108,7 +101,7 @@ _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip return string->converted; } } - + if (lenp) *lenp = string->length; return string->raw; @@ -116,8 +109,7 @@ _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip zip_uint16_t -_zip_string_length(const zip_string_t *s) -{ +_zip_string_length(const zip_string_t *s) { if (s == NULL) return 0; @@ -126,11 +118,10 @@ _zip_string_length(const zip_string_t *s) zip_string_t * -_zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) -{ +_zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) { zip_string_t *s; zip_encoding_type_t expected_encoding; - + if (length == 0) return NULL; @@ -148,13 +139,13 @@ _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; } - - if ((s=(zip_string_t *)malloc(sizeof(*s))) == NULL) { + + if ((s = (zip_string_t *)malloc(sizeof(*s))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } - if ((s->raw=(zip_uint8_t *)malloc((size_t)(length+1))) == NULL) { + if ((s->raw = (zip_uint8_t *)malloc((size_t)(length + 1))) == NULL) { free(s); return NULL; } @@ -173,16 +164,15 @@ _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, return NULL; } } - + return s; } int -_zip_string_write(zip_t *za, const zip_string_t *s) -{ +_zip_string_write(zip_t *za, const zip_string_t *s) { if (s == NULL) return 0; - + return _zip_write(za, s->raw, s->length); } diff --git a/lib/zip_unchange.c b/lib/zip_unchange.c index 4a3d642..b0bd078 100644 --- a/lib/zip_unchange.c +++ b/lib/zip_unchange.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,18 +38,16 @@ ZIP_EXTERN int -zip_unchange(zip_t *za, zip_uint64_t idx) -{ +zip_unchange(zip_t *za, zip_uint64_t idx) { return _zip_unchange(za, idx, 0); } int -_zip_unchange(zip_t *za, zip_uint64_t idx, int allow_duplicates) -{ +_zip_unchange(zip_t *za, zip_uint64_t idx, int allow_duplicates) { zip_int64_t i; const char *orig_name, *changed_name; - + if (idx >= za->nentry) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; @@ -57,7 +55,7 @@ _zip_unchange(zip_t *za, zip_uint64_t idx, int allow_duplicates) if (!allow_duplicates && za->entry[idx].changes && (za->entry[idx].changes->changed & ZIP_DIRENT_FILENAME)) { if (za->entry[idx].orig != NULL) { - if ((orig_name=_zip_get_name(za, idx, ZIP_FL_UNCHANGED, &za->error)) == NULL) { + if ((orig_name = _zip_get_name(za, idx, ZIP_FL_UNCHANGED, &za->error)) == NULL) { return -1; } @@ -70,8 +68,8 @@ _zip_unchange(zip_t *za, zip_uint64_t idx, int allow_duplicates) else { orig_name = NULL; } - - if ((changed_name=_zip_get_name(za, idx, 0, &za->error)) == NULL) { + + if ((changed_name = _zip_get_name(za, idx, 0, &za->error)) == NULL) { return -1; } @@ -89,7 +87,7 @@ _zip_unchange(zip_t *za, zip_uint64_t idx, int allow_duplicates) _zip_dirent_free(za->entry[idx].changes); za->entry[idx].changes = NULL; - _zip_unchange_data(za->entry+idx); + _zip_unchange_data(za->entry + idx); return 0; } diff --git a/lib/zip_unchange_all.c b/lib/zip_unchange_all.c index 885c77d..165064b 100644 --- a/lib/zip_unchange_all.c +++ b/lib/zip_unchange_all.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,17 +38,16 @@ ZIP_EXTERN int -zip_unchange_all(zip_t *za) -{ +zip_unchange_all(zip_t *za) { int ret; zip_uint64_t i; if (!_zip_hash_revert(za->names, &za->error)) { - return -1; + return -1; } - + ret = 0; - for (i=0; inentry; i++) + for (i = 0; i < za->nentry; i++) ret |= _zip_unchange(za, i, 1); ret |= zip_unchange_archive(za); diff --git a/lib/zip_unchange_archive.c b/lib/zip_unchange_archive.c index 920255c..f70dba9 100644 --- a/lib/zip_unchange_archive.c +++ b/lib/zip_unchange_archive.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -38,14 +38,13 @@ ZIP_EXTERN int -zip_unchange_archive(zip_t *za) -{ +zip_unchange_archive(zip_t *za) { if (za->comment_changed) { _zip_string_free(za->comment_changes); za->comment_changes = NULL; za->comment_changed = 0; } - + za->ch_flags = za->flags; return 0; diff --git a/lib/zip_unchange_data.c b/lib/zip_unchange_data.c index 839d9a3..1addaa8 100644 --- a/lib/zip_unchange_data.c +++ b/lib/zip_unchange_data.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,8 +35,7 @@ #include "zipint.h" void -_zip_unchange_data(zip_entry_t *ze) -{ +_zip_unchange_data(zip_entry_t *ze) { if (ze->source) { zip_source_free(ze->source); ze->source = NULL; @@ -52,4 +51,3 @@ _zip_unchange_data(zip_entry_t *ze) ze->deleted = 0; } - diff --git a/lib/zip_utf-8.c b/lib/zip_utf-8.c index f38eea0..8f02f88 100644 --- a/lib/zip_utf-8.c +++ b/lib/zip_utf-8.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,83 +39,65 @@ static const zip_uint16_t _cp437_to_unicode[256] = { /* 0x00 - 0x0F */ - 0x2007, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, - 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C, + 0x2007, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C, /* 0x10 - 0x1F */ - 0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, - 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC, + 0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC, /* 0x20 - 0x2F */ - 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, - 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, /* 0x30 - 0x3F */ - 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, - 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, /* 0x40 - 0x4F */ - 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, - 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, /* 0x50 - 0x5F */ - 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, - 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, /* 0x60 - 0x6F */ - 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, - 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, /* 0x70 - 0x7F */ - 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, - 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x2302, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x2302, /* 0x80 - 0x8F */ - 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, - 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, /* 0x90 - 0x9F */ - 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, - 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, /* 0xA0 - 0xAF */ - 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, - 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, /* 0xB0 - 0xBF */ - 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, - 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, /* 0xC0 - 0xCF */ - 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, - 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, /* 0xD0 - 0xDF */ - 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, - 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, /* 0xE0 - 0xEF */ - 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, - 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, /* 0xF0 - 0xFF */ - 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, - 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 -}; - -#define UTF_8_LEN_2_MASK 0xe0 -#define UTF_8_LEN_2_MATCH 0xc0 -#define UTF_8_LEN_3_MASK 0xf0 -#define UTF_8_LEN_3_MATCH 0xe0 -#define UTF_8_LEN_4_MASK 0xf8 -#define UTF_8_LEN_4_MATCH 0xf0 -#define UTF_8_CONTINUE_MASK 0xc0 + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0}; + +#define UTF_8_LEN_2_MASK 0xe0 +#define UTF_8_LEN_2_MATCH 0xc0 +#define UTF_8_LEN_3_MASK 0xf0 +#define UTF_8_LEN_3_MATCH 0xe0 +#define UTF_8_LEN_4_MASK 0xf8 +#define UTF_8_LEN_4_MATCH 0xf0 +#define UTF_8_CONTINUE_MASK 0xc0 #define UTF_8_CONTINUE_MATCH 0x80 zip_encoding_type_t -_zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding) -{ +_zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding) { zip_encoding_type_t enc; const zip_uint8_t *name; zip_uint32_t i, j, ulen; @@ -129,7 +111,7 @@ _zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding) enc = str->encoding; else { enc = ZIP_ENCODING_ASCII; - for (i=0; ilength; i++) { + for (i = 0; i < str->length; i++) { if ((name[i] > 31 && name[i] < 128) || name[i] == '\r' || name[i] == '\n' || name[i] == '\t') continue; @@ -150,8 +132,8 @@ _zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding) break; } - for (j=1; j<=ulen; j++) { - if ((name[i+j] & UTF_8_CONTINUE_MASK) != UTF_8_CONTINUE_MATCH) { + for (j = 1; j <= ulen; j++) { + if ((name[i + j] & UTF_8_CONTINUE_MASK) != UTF_8_CONTINUE_MATCH) { enc = ZIP_ENCODING_CP437; goto done; } @@ -170,14 +152,13 @@ done: if (expected_encoding != enc && enc != ZIP_ENCODING_ASCII) return ZIP_ENCODING_ERROR; } - + return enc; } static zip_uint32_t -_zip_unicode_to_utf8_len(zip_uint32_t codepoint) -{ +_zip_unicode_to_utf8_len(zip_uint32_t codepoint) { if (codepoint < 0x0080) return 1; if (codepoint < 0x0800) @@ -189,8 +170,7 @@ _zip_unicode_to_utf8_len(zip_uint32_t codepoint) static zip_uint32_t -_zip_unicode_to_utf8(zip_uint32_t codepoint, zip_uint8_t *buf) -{ +_zip_unicode_to_utf8(zip_uint32_t codepoint, zip_uint8_t *buf) { if (codepoint < 0x0080) { buf[0] = codepoint & 0xff; return 1; @@ -215,9 +195,7 @@ _zip_unicode_to_utf8(zip_uint32_t codepoint, zip_uint8_t *buf) zip_uint8_t * -_zip_cp437_to_utf8(const zip_uint8_t * const _cp437buf, zip_uint32_t len, - zip_uint32_t *utf8_lenp, zip_error_t *error) -{ +_zip_cp437_to_utf8(const zip_uint8_t *const _cp437buf, zip_uint32_t len, zip_uint32_t *utf8_lenp, zip_error_t *error) { zip_uint8_t *cp437buf = (zip_uint8_t *)_cp437buf; zip_uint8_t *utf8buf; zip_uint32_t buflen, i, offset; @@ -229,21 +207,20 @@ _zip_cp437_to_utf8(const zip_uint8_t * const _cp437buf, zip_uint32_t len, } buflen = 1; - for (i=0; i + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "zipint.h" + +#include "zip_crypto.h" + +#include +#include +#include + + + +#define MAX_KEY_LENGTH 256 +#define PBKDF2_ITERATIONS 1000 + +struct _zip_winzip_aes { + _zip_crypto_aes_t *aes; + _zip_crypto_hmac_t *hmac; + zip_uint8_t counter[ZIP_CRYPTO_AES_BLOCK_LENGTH]; + zip_uint8_t pad[ZIP_CRYPTO_AES_BLOCK_LENGTH]; + int pad_offset; +}; + +static bool +aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) +{ + zip_uint64_t i, j; + + for (i=0; i < length; i++) { + if (ctx->pad_offset == AES_BLOCK_SIZE) { + for (j = 0; j < 8; j++) { + ctx->counter[j]++; + if (ctx->counter[j] != 0) { + break; + } + } + if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) { + return false; + } + ctx->pad_offset = 0; + } + data[i] ^= ctx->pad[ctx->pad_offset++]; + } + + return true; +} + + +zip_winzip_aes_t * +_zip_winzip_aes_new(const zip_uint8_t *password, zip_uint64_t password_length, const zip_uint8_t *salt, zip_uint16_t encryption_method, zip_uint8_t *password_verify, zip_error_t *error) +{ + zip_winzip_aes_t *ctx; + zip_uint8_t buffer[2 * (MAX_KEY_LENGTH / 8) + WINZIP_AES_PASSWORD_VERIFY_LENGTH]; + zip_uint16_t key_size = 0; /* in bits */ + zip_uint16_t key_length; /* in bytes */ + + switch (encryption_method) { + case ZIP_EM_AES_128: + key_size = 128; + break; + case ZIP_EM_AES_192: + key_size = 192; + break; + case ZIP_EM_AES_256: + key_size = 256; + break; + } + + if (key_size == 0 || salt == NULL || password == NULL || password_length == 0) { + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } + + key_length = key_size / 8; + + if ((ctx = (zip_winzip_aes_t *)malloc(sizeof(*ctx))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + memset(ctx->counter, 0, sizeof(ctx->counter)); + ctx->pad_offset = ZIP_CRYPTO_AES_BLOCK_LENGTH; + + if (!_zip_crypto_pbkdf2(password, password_length, salt, key_length / 2, PBKDF2_ITERATIONS, buffer, 2 * key_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) { + free(ctx); + return NULL; + } + + if ((ctx->aes = _zip_crypto_aes_new(buffer, key_size, error)) == NULL) { + _zip_crypto_clear(ctx, sizeof(*ctx)); + free(ctx); + return NULL; + } + if ((ctx->hmac = _zip_crypto_hmac_new(buffer + key_length, key_length, error)) == NULL) { + _zip_crypto_aes_free(ctx->aes); + free(ctx); + return NULL; + } + + if (password_verify) { + memcpy(password_verify, buffer + (2 * key_size / 8), WINZIP_AES_PASSWORD_VERIFY_LENGTH); + } + + return ctx; +} + + +bool +_zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) +{ + return aes_crypt(ctx, data, length) && _zip_crypto_hmac(ctx->hmac, data, length); +} + + +bool +_zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) +{ + return _zip_crypto_hmac(ctx->hmac, data, length) && aes_crypt(ctx, data, length); +} + + +bool +_zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac) +{ + return _zip_crypto_hmac_output(ctx->hmac, hmac); +} + + +void +_zip_winzip_aes_free(zip_winzip_aes_t *ctx) +{ + if (ctx == NULL) { + return; + } + + _zip_crypto_aes_free(ctx->aes); + _zip_crypto_hmac_free(ctx->hmac); + free(ctx); +} diff --git a/lib/zipint.h b/lib/zipint.h index d912b7d..3c60ece 100644 --- a/lib/zipint.h +++ b/lib/zipint.h @@ -49,63 +49,72 @@ #include "zip.h" #define CENTRAL_MAGIC "PK\1\2" -#define LOCAL_MAGIC "PK\3\4" -#define EOCD_MAGIC "PK\5\6" +#define LOCAL_MAGIC "PK\3\4" +#define EOCD_MAGIC "PK\5\6" #define DATADES_MAGIC "PK\7\10" #define EOCD64LOC_MAGIC "PK\6\7" -#define EOCD64_MAGIC "PK\6\6" -#define CDENTRYSIZE 46u -#define LENTRYSIZE 30 -#define MAXCOMLEN 65536 -#define MAXEXTLEN 65536 -#define EOCDLEN 22 -#define EOCD64LOCLEN 20 -#define EOCD64LEN 56 -#define CDBUFSIZE (MAXCOMLEN+EOCDLEN+EOCD64LOCLEN) -#define BUFSIZE 8192 +#define EOCD64_MAGIC "PK\6\6" +#define CDENTRYSIZE 46u +#define LENTRYSIZE 30 +#define MAXCOMLEN 65536 +#define MAXEXTLEN 65536 +#define EOCDLEN 22 +#define EOCD64LOCLEN 20 +#define EOCD64LEN 56 +#define CDBUFSIZE (MAXCOMLEN + EOCDLEN + EOCD64LOCLEN) +#define BUFSIZE 8192 #define EFZIP64SIZE 28 #define EF_WINZIP_AES_SIZE 7 -#define ZIP_CM_REPLACED_DEFAULT (-2) -#define ZIP_CM_WINZIP_AES 99 /* Winzip AES encrypted */ +#define ZIP_CM_REPLACED_DEFAULT (-2) +#define ZIP_CM_WINZIP_AES 99 /* Winzip AES encrypted */ -#define ZIP_CM_IS_DEFAULT(x) ((x) == ZIP_CM_DEFAULT || (x) == ZIP_CM_REPLACED_DEFAULT) -#define ZIP_CM_ACTUAL(x) ((zip_uint16_t)(ZIP_CM_IS_DEFAULT(x) ? ZIP_CM_DEFLATE : (x))) +#define WINZIP_AES_PASSWORD_VERIFY_LENGTH 2 +#define WINZIP_AES_MAX_HEADER_LENGTH (16 + WINZIP_AES_PASSWORD_VERIFY_LENGTH) +#define AES_BLOCK_SIZE 16 +#define HMAC_LENGTH 10 +#define SHA1_LENGTH 20 +#define SALT_LENGTH(method) ((method) == ZIP_EM_AES_128 ? 8 : ((method) == ZIP_EM_AES_192 ? 12 : 16)) -#define ZIP_EF_UTF_8_COMMENT 0x6375 -#define ZIP_EF_UTF_8_NAME 0x7075 -#define ZIP_EF_WINZIP_AES 0x9901 -#define ZIP_EF_ZIP64 0x0001 +#define ZIP_CM_IS_DEFAULT(x) ((x) == ZIP_CM_DEFAULT || (x) == ZIP_CM_REPLACED_DEFAULT) +#define ZIP_CM_ACTUAL(x) ((zip_uint16_t)(ZIP_CM_IS_DEFAULT(x) ? ZIP_CM_DEFLATE : (x))) -#define ZIP_EF_IS_INTERNAL(id) ((id) == ZIP_EF_UTF_8_COMMENT || (id) == ZIP_EF_UTF_8_NAME || (id) == ZIP_EF_WINZIP_AES || (id) == ZIP_EF_ZIP64) +#define ZIP_EF_UTF_8_COMMENT 0x6375 +#define ZIP_EF_UTF_8_NAME 0x7075 +#define ZIP_EF_WINZIP_AES 0x9901 +#define ZIP_EF_ZIP64 0x0001 + +#define ZIP_EF_IS_INTERNAL(id) ((id) == ZIP_EF_UTF_8_COMMENT || (id) == ZIP_EF_UTF_8_NAME || (id) == ZIP_EF_WINZIP_AES || (id) == ZIP_EF_ZIP64) /* according to unzip-6.0's zipinfo.c, this corresponds to a regular file with rw permissions for everyone */ -#define ZIP_EXT_ATTRIB_DEFAULT (0100666u<<16) +#define ZIP_EXT_ATTRIB_DEFAULT (0100666u << 16) /* according to unzip-6.0's zipinfo.c, this corresponds to a directory with rwx permissions for everyone */ -#define ZIP_EXT_ATTRIB_DEFAULT_DIR (0040777u<<16) +#define ZIP_EXT_ATTRIB_DEFAULT_DIR (0040777u << 16) -#define ZIP_MAX(a, b) ((a) > (b) ? (a) : (b)) -#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ZIP_MAX(a, b) ((a) > (b) ? (a) : (b)) +#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) /* This section contains API that won't materialize like this. It's placed in the internal section, pending cleanup. */ /* flags for compression and encryption sources */ -#define ZIP_CODEC_DECODE 0 /* decompress/decrypt (encode flag not set) */ -#define ZIP_CODEC_ENCODE 1 /* compress/encrypt */ +#define ZIP_CODEC_DECODE 0 /* decompress/decrypt (encode flag not set) */ +#define ZIP_CODEC_ENCODE 1 /* compress/encrypt */ typedef zip_source_t *(*zip_encryption_implementation)(zip_t *, zip_source_t *, zip_uint16_t, int, const char *); zip_encryption_implementation _zip_get_encryption_implementation(zip_uint16_t method, int operation); +// clang-format off enum zip_compression_status { ZIP_COMPRESSION_OK, ZIP_COMPRESSION_END, ZIP_COMPRESSION_ERROR, ZIP_COMPRESSION_NEED_DATA }; +// clang-format on typedef enum zip_compression_status zip_compression_status_t; struct zip_compression_algorithm { @@ -168,30 +177,30 @@ enum zip_les { ZIP_LES_NONE, ZIP_LES_UPPER, ZIP_LES_LOWER, ZIP_LES_INVAL }; /* directory entry: general purpose bit flags */ -#define ZIP_GPBF_ENCRYPTED 0x0001u /* is encrypted */ -#define ZIP_GPBF_DATA_DESCRIPTOR 0x0008u /* crc/size after file data */ -#define ZIP_GPBF_STRONG_ENCRYPTION 0x0040u /* uses strong encryption */ -#define ZIP_GPBF_ENCODING_UTF_8 0x0800u /* file name encoding is UTF-8 */ +#define ZIP_GPBF_ENCRYPTED 0x0001u /* is encrypted */ +#define ZIP_GPBF_DATA_DESCRIPTOR 0x0008u /* crc/size after file data */ +#define ZIP_GPBF_STRONG_ENCRYPTION 0x0040u /* uses strong encryption */ +#define ZIP_GPBF_ENCODING_UTF_8 0x0800u /* file name encoding is UTF-8 */ /* extra fields */ -#define ZIP_EF_LOCAL ZIP_FL_LOCAL /* include in local header */ -#define ZIP_EF_CENTRAL ZIP_FL_CENTRAL /* include in central directory */ -#define ZIP_EF_BOTH (ZIP_EF_LOCAL|ZIP_EF_CENTRAL) /* include in both */ +#define ZIP_EF_LOCAL ZIP_FL_LOCAL /* include in local header */ +#define ZIP_EF_CENTRAL ZIP_FL_CENTRAL /* include in central directory */ +#define ZIP_EF_BOTH (ZIP_EF_LOCAL | ZIP_EF_CENTRAL) /* include in both */ -#define ZIP_FL_FORCE_ZIP64 1024 /* force zip64 extra field (_zip_dirent_write) */ +#define ZIP_FL_FORCE_ZIP64 1024 /* force zip64 extra field (_zip_dirent_write) */ -#define ZIP_FL_ENCODING_ALL (ZIP_FL_ENC_GUESS|ZIP_FL_ENC_CP437|ZIP_FL_ENC_UTF_8) +#define ZIP_FL_ENCODING_ALL (ZIP_FL_ENC_GUESS | ZIP_FL_ENC_CP437 | ZIP_FL_ENC_UTF_8) /* encoding type */ enum zip_encoding_type { - ZIP_ENCODING_UNKNOWN, /* not yet analyzed */ - ZIP_ENCODING_ASCII, /* plain ASCII */ - ZIP_ENCODING_UTF8_KNOWN, /* is UTF-8 */ - ZIP_ENCODING_UTF8_GUESSED, /* possibly UTF-8 */ - ZIP_ENCODING_CP437, /* Code Page 437 */ - ZIP_ENCODING_ERROR /* should be UTF-8 but isn't */ + ZIP_ENCODING_UNKNOWN, /* not yet analyzed */ + ZIP_ENCODING_ASCII, /* plain ASCII */ + ZIP_ENCODING_UTF8_KNOWN, /* is UTF-8 */ + ZIP_ENCODING_UTF8_GUESSED, /* possibly UTF-8 */ + ZIP_ENCODING_CP437, /* Code Page 437 */ + ZIP_ENCODING_ERROR /* should be UTF-8 but isn't */ }; typedef enum zip_encoding_type zip_encoding_type_t; @@ -211,107 +220,107 @@ typedef struct zip_progress zip_progress_t; /* zip archive, part of API */ struct zip { - zip_source_t *src; /* data source for archive */ - unsigned int open_flags; /* flags passed to zip_open */ - zip_error_t error; /* error information */ + zip_source_t *src; /* data source for archive */ + unsigned int open_flags; /* flags passed to zip_open */ + zip_error_t error; /* error information */ - unsigned int flags; /* archive global flags */ - unsigned int ch_flags; /* changed archive global flags */ + unsigned int flags; /* archive global flags */ + unsigned int ch_flags; /* changed archive global flags */ - char *default_password; /* password used when no other supplied */ + char *default_password; /* password used when no other supplied */ - zip_string_t *comment_orig; /* archive comment */ - zip_string_t *comment_changes; /* changed archive comment */ - bool comment_changed; /* whether archive comment was changed */ + zip_string_t *comment_orig; /* archive comment */ + zip_string_t *comment_changes; /* changed archive comment */ + bool comment_changed; /* whether archive comment was changed */ - zip_uint64_t nentry; /* number of entries */ - zip_uint64_t nentry_alloc; /* number of entries allocated */ - zip_entry_t *entry; /* entries */ + zip_uint64_t nentry; /* number of entries */ + zip_uint64_t nentry_alloc; /* number of entries allocated */ + zip_entry_t *entry; /* entries */ - unsigned int nopen_source; /* number of open sources using archive */ - unsigned int nopen_source_alloc; /* number of sources allocated */ - zip_source_t **open_source; /* open sources using archive */ + unsigned int nopen_source; /* number of open sources using archive */ + unsigned int nopen_source_alloc; /* number of sources allocated */ + zip_source_t **open_source; /* open sources using archive */ - zip_hash_t *names; /* hash table for name lookup */ + zip_hash_t *names; /* hash table for name lookup */ - zip_progress_t *progress; /* progress callback for zip_close() */ + zip_progress_t *progress; /* progress callback for zip_close() */ }; /* file in zip archive, part of API */ struct zip_file { - zip_t *za; /* zip archive containing this file */ - zip_error_t error; /* error information */ + zip_t *za; /* zip archive containing this file */ + zip_error_t error; /* error information */ bool eof; - zip_source_t *src; /* data source */ + zip_source_t *src; /* data source */ }; /* zip archive directory entry (central or local) */ -#define ZIP_DIRENT_COMP_METHOD 0x0001u -#define ZIP_DIRENT_FILENAME 0x0002u -#define ZIP_DIRENT_COMMENT 0x0004u -#define ZIP_DIRENT_EXTRA_FIELD 0x0008u -#define ZIP_DIRENT_ATTRIBUTES 0x0010u -#define ZIP_DIRENT_LAST_MOD 0x0020u -#define ZIP_DIRENT_ENCRYPTION_METHOD 0x0040u -#define ZIP_DIRENT_PASSWORD 0x0080u -#define ZIP_DIRENT_ALL ZIP_UINT32_MAX +#define ZIP_DIRENT_COMP_METHOD 0x0001u +#define ZIP_DIRENT_FILENAME 0x0002u +#define ZIP_DIRENT_COMMENT 0x0004u +#define ZIP_DIRENT_EXTRA_FIELD 0x0008u +#define ZIP_DIRENT_ATTRIBUTES 0x0010u +#define ZIP_DIRENT_LAST_MOD 0x0020u +#define ZIP_DIRENT_ENCRYPTION_METHOD 0x0040u +#define ZIP_DIRENT_PASSWORD 0x0080u +#define ZIP_DIRENT_ALL ZIP_UINT32_MAX struct zip_dirent { zip_uint32_t changed; - bool local_extra_fields_read; /* whether we already read in local header extra fields */ - bool cloned; /* whether this instance is cloned, and thus shares non-changed strings */ - - bool crc_valid; /* if CRC is valid (sometimes not for encrypted archives) */ - - zip_uint16_t version_madeby; /* (c) version of creator */ - zip_uint16_t version_needed; /* (cl) version needed to extract */ - zip_uint16_t bitflags; /* (cl) general purpose bit flag */ - zip_int32_t comp_method; /* (cl) compression method used (uint16 and ZIP_CM_DEFAULT (-1)) */ - time_t last_mod; /* (cl) time of last modification */ - zip_uint32_t crc; /* (cl) CRC-32 of uncompressed data */ - zip_uint64_t comp_size; /* (cl) size of compressed data */ - zip_uint64_t uncomp_size; /* (cl) size of uncompressed data */ - zip_string_t *filename; /* (cl) file name (NUL-terminated) */ - zip_extra_field_t *extra_fields; /* (cl) extra fields, parsed */ - zip_string_t *comment; /* (c) file comment */ - zip_uint32_t disk_number; /* (c) disk number start */ - zip_uint16_t int_attrib; /* (c) internal file attributes */ - zip_uint32_t ext_attrib; /* (c) external file attributes */ - zip_uint64_t offset; /* (c) offset of local header */ - - zip_uint16_t compression_level; /* level of compression to use (never valid in orig) */ - zip_uint16_t encryption_method; /* encryption method, computed from other fields */ - char *password; /* file specific encryption password */ + bool local_extra_fields_read; /* whether we already read in local header extra fields */ + bool cloned; /* whether this instance is cloned, and thus shares non-changed strings */ + + bool crc_valid; /* if CRC is valid (sometimes not for encrypted archives) */ + + zip_uint16_t version_madeby; /* (c) version of creator */ + zip_uint16_t version_needed; /* (cl) version needed to extract */ + zip_uint16_t bitflags; /* (cl) general purpose bit flag */ + zip_int32_t comp_method; /* (cl) compression method used (uint16 and ZIP_CM_DEFAULT (-1)) */ + time_t last_mod; /* (cl) time of last modification */ + zip_uint32_t crc; /* (cl) CRC-32 of uncompressed data */ + zip_uint64_t comp_size; /* (cl) size of compressed data */ + zip_uint64_t uncomp_size; /* (cl) size of uncompressed data */ + zip_string_t *filename; /* (cl) file name (NUL-terminated) */ + zip_extra_field_t *extra_fields; /* (cl) extra fields, parsed */ + zip_string_t *comment; /* (c) file comment */ + zip_uint32_t disk_number; /* (c) disk number start */ + zip_uint16_t int_attrib; /* (c) internal file attributes */ + zip_uint32_t ext_attrib; /* (c) external file attributes */ + zip_uint64_t offset; /* (c) offset of local header */ + + zip_uint16_t compression_level; /* level of compression to use (never valid in orig) */ + zip_uint16_t encryption_method; /* encryption method, computed from other fields */ + char *password; /* file specific encryption password */ }; /* zip archive central directory */ struct zip_cdir { - zip_entry_t *entry; /* directory entries */ - zip_uint64_t nentry; /* number of entries */ - zip_uint64_t nentry_alloc; /* number of entries allocated */ - - zip_uint64_t size; /* size of central directory */ - zip_uint64_t offset; /* offset of central directory in file */ - zip_string_t *comment; /* zip archive comment */ - bool is_zip64; /* central directory in zip64 format */ + zip_entry_t *entry; /* directory entries */ + zip_uint64_t nentry; /* number of entries */ + zip_uint64_t nentry_alloc; /* number of entries allocated */ + + zip_uint64_t size; /* size of central directory */ + zip_uint64_t offset; /* offset of central directory in file */ + zip_string_t *comment; /* zip archive comment */ + bool is_zip64; /* central directory in zip64 format */ }; struct zip_extra_field { zip_extra_field_t *next; - zip_flags_t flags; /* in local/central header */ - zip_uint16_t id; /* header id */ - zip_uint16_t size; /* data size */ + zip_flags_t flags; /* in local/central header */ + zip_uint16_t id; /* header id */ + zip_uint16_t size; /* data size */ zip_uint8_t *data; }; enum zip_source_write_state { - ZIP_SOURCE_WRITE_CLOSED, /* write is not in progress */ - ZIP_SOURCE_WRITE_OPEN, /* write is in progress */ - ZIP_SOURCE_WRITE_FAILED, /* commit failed, only rollback allowed */ - ZIP_SOURCE_WRITE_REMOVED /* file was removed */ + ZIP_SOURCE_WRITE_CLOSED, /* write is not in progress */ + ZIP_SOURCE_WRITE_OPEN, /* write is in progress */ + ZIP_SOURCE_WRITE_FAILED, /* commit failed, only rollback allowed */ + ZIP_SOURCE_WRITE_REMOVED /* file was removed */ }; typedef enum zip_source_write_state zip_source_write_state_t; @@ -323,19 +332,19 @@ struct zip_source { } cb; void *ud; zip_error_t error; - zip_int64_t supports; /* supported commands */ - unsigned int open_count; /* number of times source was opened (directly or as lower layer) */ - zip_source_write_state_t write_state; /* whether source is open for writing */ - bool source_closed; /* set if source archive is closed */ - zip_t *source_archive; /* zip archive we're reading from, NULL if not from archive */ + zip_int64_t supports; /* supported commands */ + unsigned int open_count; /* number of times source was opened (directly or as lower layer) */ + zip_source_write_state_t write_state; /* whether source is open for writing */ + bool source_closed; /* set if source archive is closed */ + zip_t *source_archive; /* zip archive we're reading from, NULL if not from archive */ unsigned int refcount; - bool eof; /* EOF reached */ - bool had_read_error; /* a previous ZIP_SOURCE_READ reported an error */ + bool eof; /* EOF reached */ + bool had_read_error; /* a previous ZIP_SOURCE_READ reported an error */ }; #define ZIP_SOURCE_IS_OPEN_READING(src) ((src)->open_count > 0) #define ZIP_SOURCE_IS_OPEN_WRITING(src) ((src)->write_state == ZIP_SOURCE_WRITE_OPEN) -#define ZIP_SOURCE_IS_LAYERED(src) ((src)->src != NULL) +#define ZIP_SOURCE_IS_LAYERED(src) ((src)->src != NULL) /* entry in zip archive directory */ @@ -350,11 +359,11 @@ struct zip_entry { /* file or archive comment, or filename */ struct zip_string { - zip_uint8_t *raw; /* raw string */ - zip_uint16_t length; /* length of raw string */ - enum zip_encoding_type encoding; /* autorecognized encoding */ - zip_uint8_t *converted; /* autoconverted string */ - zip_uint32_t converted_length; /* length of converted */ + zip_uint8_t *raw; /* raw string */ + zip_uint16_t length; /* length of raw string */ + enum zip_encoding_type encoding; /* autorecognized encoding */ + zip_uint8_t *converted; /* autoconverted string */ + zip_uint32_t converted_length; /* length of converted */ }; @@ -373,24 +382,26 @@ struct zip_buffer { struct zip_filelist { zip_uint64_t idx; -/* TODO const char *name; */ + /* TODO const char *name; */ }; typedef struct zip_filelist zip_filelist_t; +struct _zip_winzip_aes; +typedef struct _zip_winzip_aes zip_winzip_aes_t; -extern const char * const _zip_err_str[]; +extern const char *const _zip_err_str[]; extern const int _zip_nerr_str; extern const int _zip_err_type[]; -#define ZIP_MAX(a, b) ((a) > (b) ? (a) : (b)) -#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ZIP_MAX(a, b) ((a) > (b) ? (a) : (b)) +#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) -#define ZIP_ENTRY_CHANGED(e, f) ((e)->changes && ((e)->changes->changed & (f))) -#define ZIP_ENTRY_DATA_CHANGED(x) ((x)->source != NULL) -#define ZIP_ENTRY_HAS_CHANGES(e) (ZIP_ENTRY_DATA_CHANGED(e) || (e)->deleted || ZIP_ENTRY_CHANGED((e), ZIP_DIRENT_ALL)) +#define ZIP_ENTRY_CHANGED(e, f) ((e)->changes && ((e)->changes->changed & (f))) +#define ZIP_ENTRY_DATA_CHANGED(x) ((x)->source != NULL) +#define ZIP_ENTRY_HAS_CHANGES(e) (ZIP_ENTRY_DATA_CHANGED(e) || (e)->deleted || ZIP_ENTRY_CHANGED((e), ZIP_DIRENT_ALL)) -#define ZIP_IS_RDONLY(za) ((za)->ch_flags & ZIP_AFL_RDONLY) +#define ZIP_IS_RDONLY(za) ((za)->ch_flags & ZIP_AFL_RDONLY) #ifdef HAVE_EXPLICIT_MEMSET @@ -399,6 +410,7 @@ extern const int _zip_err_type[]; #ifdef HAVE_EXPLICIT_BZERO #define _zip_crypto_clear(b, l) explicit_bzero((b), (l)) #else +#include #define _zip_crypto_clear(b, l) memset((b), 0, (l)) #endif #endif @@ -480,7 +492,7 @@ int _zip_filerange_crc(zip_source_t *src, zip_uint64_t offset, zip_uint64_t leng zip_dirent_t *_zip_get_dirent(zip_t *, zip_uint64_t, zip_flags_t, zip_error_t *); enum zip_encoding_type _zip_guess_encoding(zip_string_t *, enum zip_encoding_type); -zip_uint8_t *_zip_cp437_to_utf8(const zip_uint8_t * const, zip_uint32_t, zip_uint32_t *, zip_error_t *); +zip_uint8_t *_zip_cp437_to_utf8(const zip_uint8_t *const, zip_uint32_t, zip_uint32_t *, zip_error_t *); bool _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error); bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *key, zip_error_t *error); @@ -499,7 +511,7 @@ void _zip_progress_start(zip_progress_t *progress); void _zip_progress_subrange(zip_progress_t *progress, double start, double end); void _zip_progress_update(zip_progress_t *progress, double value); -bool zip_random(zip_uint8_t *buffer, zip_uint16_t length); +ZIP_EXTERN bool zip_random(zip_uint8_t *buffer, zip_uint16_t length); int _zip_read(zip_source_t *src, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error); int _zip_read_at_offset(zip_source_t *src, zip_uint64_t offset, unsigned char *b, size_t length, zip_error_t *error); @@ -529,6 +541,11 @@ const zip_uint8_t *_zip_string_get(zip_string_t *, zip_uint32_t *, zip_flags_t, zip_uint16_t _zip_string_length(const zip_string_t *); zip_string_t *_zip_string_new(const zip_uint8_t *, zip_uint16_t, zip_flags_t, zip_error_t *); int _zip_string_write(zip_t *za, const zip_string_t *string); +bool _zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length); +bool _zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length); +bool _zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac); +void _zip_winzip_aes_free(zip_winzip_aes_t *ctx); +zip_winzip_aes_t *_zip_winzip_aes_new(const zip_uint8_t *password, zip_uint64_t password_length, const zip_uint8_t *salt, zip_uint16_t key_size, zip_uint8_t *password_verify, zip_error_t *error); int _zip_changed(const zip_t *, zip_uint64_t *); const char *_zip_get_name(zip_t *, zip_uint64_t, zip_flags_t, zip_error_t *); diff --git a/lib/zipwin32.h b/lib/zipwin32.h index db549e2..d2c5d82 100644 --- a/lib/zipwin32.h +++ b/lib/zipwin32.h @@ -20,7 +20,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,7 +35,7 @@ */ /* 0x0501 => Windows XP; needs to be at least this value because of GetFileSizeEx */ -#ifndef MS_UWP +#if !defined(MS_UWP) && !defined(_WIN32_WINNT) #define _WIN32_WINNT 0x0501 #endif @@ -46,24 +46,24 @@ struct _zip_source_win32_file_ops; struct _zip_source_win32_read_file { - zip_error_t error; /* last error information */ + zip_error_t error; /* last error information */ zip_int64_t supports; /* operations */ struct _zip_source_win32_file_ops *ops; /* reading */ - void *fname; /* name of file to read from - ANSI (char *) or Unicode (wchar_t *) */ - void *h; /* HANDLE for file to read from */ - int closep; /* whether to close f on ZIP_CMD_FREE */ - struct zip_stat st; /* stat information passed in */ - zip_uint64_t start; /* start offset of data to read */ - zip_uint64_t end; /* end offset of data to read, 0 for up to EOF */ - zip_uint64_t current; /* current offset */ + void *fname; /* name of file to read from - ANSI (char *) or Unicode (wchar_t *) */ + void *h; /* HANDLE for file to read from */ + int closep; /* whether to close f on ZIP_CMD_FREE */ + struct zip_stat st; /* stat information passed in */ + zip_uint64_t start; /* start offset of data to read */ + zip_uint64_t end; /* end offset of data to read, 0 for up to EOF */ + zip_uint64_t current; /* current offset */ /* writing */ - void *tmpname; /* name of temp file - ANSI (char *) or Unicode (wchar_t *) */ - void *hout; /* HANDLE for output file */ + void *tmpname; /* name of temp file - ANSI (char *) or Unicode (wchar_t *) */ + void *hout; /* HANDLE for output file */ }; typedef struct _zip_source_win32_read_file _zip_source_win32_read_file_t; diff --git a/libzip.pc.in b/libzip.pc.in index 53abc47..7be7f3e 100644 --- a/libzip.pc.in +++ b/libzip.pc.in @@ -1,9 +1,10 @@ prefix=@prefix@ exec_prefix=@exec_prefix@ +bindir=@bindir@ libdir=@libdir@ includedir=@includedir@ -zipcmp=@prefix@/bin/zipcmp +zipcmp=@bindir@/zipcmp Name: libzip Description: library for handling zip archives diff --git a/man/zip_errors.mdoc b/man/zip_errors.mdoc index 935d842..dc05a5c 100644 --- a/man/zip_errors.mdoc +++ b/man/zip_errors.mdoc @@ -44,7 +44,7 @@ libzip (-lzip) .In zip.h .Sh DESCRIPTION The following error codes are used by libzip: -.Bl -tag -width XZIPXERXCOMPNOTSUPPXX +.Bl -tag -width XZIP_ER_COMPRESSED_DATAX .It Bq Er ZIP_ER_CHANGED Entry has been changed. .It Bq Er ZIP_ER_CLOSE diff --git a/regress/CMakeLists.txt b/regress/CMakeLists.txt index e2ce232..aaded89 100644 --- a/regress/CMakeLists.txt +++ b/regress/CMakeLists.txt @@ -31,8 +31,8 @@ TARGET_LINK_LIBRARIES(hole zip) ADD_EXECUTABLE(ziptool_regress ziptool_regress.c ${SRC_EXTRA_FILES} source_hole.c) TARGET_LINK_LIBRARIES(ziptool_regress zip) -ADD_LIBRARY(malloc MODULE malloc.c) -TARGET_LINK_LIBRARIES(malloc ${CMAKE_DL_LIBS}) +#ADD_LIBRARY(malloc MODULE malloc.c) +#TARGET_LINK_LIBRARIES(malloc ${CMAKE_DL_LIBS}) ADD_LIBRARY(nonrandomopen MODULE nonrandomopen.c) TARGET_LINK_LIBRARIES(nonrandomopen ${CMAKE_DL_LIBS}) @@ -51,15 +51,9 @@ ADD_CUSTOM_TARGET(cleanup ADD_CUSTOM_TARGET(testinput ALL VERBATIM - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 0 > manyfiles.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 1 > manyfiles-133000.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 2 > manyfiles-65536.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 3 > manyfiles-zip64-modulo.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 4 > manyfiles-zip64.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 5 > manyfiles-fewer.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip cat 6 > manyfiles-more.zip - COMMAND ziptool ${CMAKE_CURRENT_SOURCE_DIR}/bigzero-zip.zip cat 0 > bigzero.zip - DEPENDS ziptool ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip ${CMAKE_CURRENT_SOURCE_DIR}/bigzero-zip.zip + COMMAND cmake -E tar x ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip + COMMAND cmake -E tar x ${CMAKE_CURRENT_SOURCE_DIR}/bigzero-zip.zip + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/manyfiles-zip.zip ${CMAKE_CURRENT_SOURCE_DIR}/bigzero-zip.zip ) SET_PROPERTY(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES diff --git a/regress/add_from_filep.c b/regress/add_from_filep.c index c97b99e..f45a57b 100644 --- a/regress/add_from_filep.c +++ b/regress/add_from_filep.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -42,8 +42,7 @@ static const char *prg; int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { const char *archive; const char *file; const char *name; @@ -61,8 +60,8 @@ main(int argc, char *argv[]) archive = argv[1]; file = argv[2]; - - if ((za=zip_open(archive, ZIP_CREATE, &err)) == NULL) { + + if ((za = zip_open(archive, ZIP_CREATE, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error)); @@ -70,31 +69,27 @@ main(int argc, char *argv[]) return 1; } - if ((fp=fopen(file, "r")) == NULL) { - fprintf(stderr, "%s: can't open input file '%s': %s\n", prg, - file, strerror(errno)); + if ((fp = fopen(file, "r")) == NULL) { + fprintf(stderr, "%s: can't open input file '%s': %s\n", prg, file, strerror(errno)); return 1; } - if ((zs=zip_source_filep(za, fp, 0, -1)) == NULL) { - fprintf(stderr, "%s: error creating file source for '%s': %s\n", prg, - file, zip_strerror(za)); + if ((zs = zip_source_filep(za, fp, 0, -1)) == NULL) { + fprintf(stderr, "%s: error creating file source for '%s': %s\n", prg, file, zip_strerror(za)); return 1; } - if ((name=strrchr(file, '/')) == NULL) + if ((name = strrchr(file, '/')) == NULL) name = file; if (zip_add(za, name, zs) == -1) { zip_source_free(zs); - fprintf(stderr, "%s: can't add file '%s': %s\n", prg, - file, zip_strerror(za)); + fprintf(stderr, "%s: can't add file '%s': %s\n", prg, file, zip_strerror(za)); return 1; } if (zip_close(za) == -1) { - fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, - archive, zip_strerror(za)); + fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(za)); return 1; } diff --git a/regress/can_clone_file.c b/regress/can_clone_file.c index cef16fe..327c8e5 100644 --- a/regress/can_clone_file.c +++ b/regress/can_clone_file.c @@ -40,25 +40,28 @@ #ifdef HAVE_CLONEFILE #include -#include #include +#include +#include +#elif defined(HAVE_FICLONERANGE) +#include +#include #include #endif int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { #ifdef HAVE_CLONEFILE struct statfs fs; struct attrlist attribute_list; struct { - uint32_t size; - vol_capabilities_attr_t capabilities; + uint32_t size; + vol_capabilities_attr_t capabilities; } volume_attributes; if (statfs(".", &fs) < 0) { - fprintf(stderr, "%s: can't get mount point of current directory: %s\n", argv[0], strerror(errno)); - exit(1); + fprintf(stderr, "%s: can't get mount point of current directory: %s\n", argv[0], strerror(errno)); + exit(1); } /* Not all volumes support clonefile(). A volume can be tested for @@ -68,16 +71,52 @@ main(int argc, char *argv[]) memset(&attribute_list, 0, sizeof(attribute_list)); attribute_list.bitmapcount = ATTR_BIT_MAP_COUNT; - attribute_list.volattr = ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES; + attribute_list.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES; memset(&volume_attributes, 0, sizeof(volume_attributes)); if (getattrlist(fs.f_mntonname, &attribute_list, &volume_attributes, sizeof(volume_attributes), 0) < 0) { - fprintf(stderr, "%s: can't get volume capabilities of '%s': %s\n", argv[0], fs.f_mntonname, strerror(errno)); - exit(1); + fprintf(stderr, "%s: can't get volume capabilities of '%s': %s\n", argv[0], fs.f_mntonname, strerror(errno)); + exit(1); } if (volume_attributes.capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_CLONE) { - exit(0); + exit(0); + } +#elif defined(HAVE_FICLONERANGE) + char namea[32] = "a.fioclone.XXXXXX"; + char nameb[32] = "b.fioclone.XXXXXX"; + int fda, fdb, ret; + struct file_clone_range range; + + if ((fda = mkstemp(namea)) < 0) { + fprintf(stderr, "can't create temp file a: %s\n", strerror(errno)); + exit(1); + } + if ((fdb = mkstemp(nameb)) < 0) { + fprintf(stderr, "can't create temp file b: %s\n", strerror(errno)); + (void)close(fda); + (void)remove(namea); + exit(1); + } + if (write(fda, "test\n", 5) < 0) { + fprintf(stderr, "can't write temp file a: %s\n", strerror(errno)); + (void)close(fda); + (void)remove(namea); + close(fdb); + (void)remove(nameb); + exit(1); + } + range.src_fd = fda; + range.src_offset = 0; + range.src_length = 0; + range.dest_offset = 0; + ret = ioctl(fdb, FICLONERANGE, &range); + (void)close(fda); + (void)close(fdb); + (void)remove(namea); + (void)remove(nameb); + if (ret >= 0) { + exit(0); } #endif diff --git a/regress/decrypt-correct-password-aes128.test b/regress/decrypt-correct-password-aes128.test index a0409d9..470ba30 100644 --- a/regress/decrypt-correct-password-aes128.test +++ b/regress/decrypt-correct-password-aes128.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using correct password +features CRYPTO return 0 args encrypt.zzip set_password foofoofoo cat 1 file encrypt.zzip encrypt-aes128.zip encrypt-aes128.zip diff --git a/regress/decrypt-correct-password-aes192.test b/regress/decrypt-correct-password-aes192.test index 56fc456..a66dd16 100644 --- a/regress/decrypt-correct-password-aes192.test +++ b/regress/decrypt-correct-password-aes192.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using correct password +features CRYPTO return 0 args encrypt.zzip set_password foofoofoo cat 1 file encrypt.zzip encrypt-aes192.zip encrypt-aes192.zip diff --git a/regress/decrypt-correct-password-aes256.test b/regress/decrypt-correct-password-aes256.test index e704b53..4eddea8 100644 --- a/regress/decrypt-correct-password-aes256.test +++ b/regress/decrypt-correct-password-aes256.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using correct password +features CRYPTO return 0 args encrypt.zzip set_password foofoofoo cat 1 file encrypt.zzip encrypt-aes256.zip encrypt-aes256.zip diff --git a/regress/decrypt-no-password-aes256.test b/regress/decrypt-no-password-aes256.test index f1a4ce8..6140a42 100644 --- a/regress/decrypt-no-password-aes256.test +++ b/regress/decrypt-no-password-aes256.test @@ -1,4 +1,5 @@ # test AES decryption support, no password provided +features CRYPTO return 1 args encrypt.zzip cat 1 file encrypt.zzip encrypt-aes256.zip encrypt-aes256.zip diff --git a/regress/decrypt-wrong-password-aes128.test b/regress/decrypt-wrong-password-aes128.test index 7866efa..fa4b789 100644 --- a/regress/decrypt-wrong-password-aes128.test +++ b/regress/decrypt-wrong-password-aes128.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using wrong password +features CRYPTO return 1 args encrypt.zzip set_password notfoonotfoo cat 1 file encrypt.zzip encrypt-aes128.zip encrypt-aes128.zip diff --git a/regress/decrypt-wrong-password-aes192.test b/regress/decrypt-wrong-password-aes192.test index 9a4206d..f04d0da 100644 --- a/regress/decrypt-wrong-password-aes192.test +++ b/regress/decrypt-wrong-password-aes192.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using wrong password +features CRYPTO return 1 args encrypt.zzip set_password notfoonotfoo cat 1 file encrypt.zzip encrypt-aes192.zip encrypt-aes192.zip diff --git a/regress/decrypt-wrong-password-aes256.test b/regress/decrypt-wrong-password-aes256.test index 8cf701c..836b5b1 100644 --- a/regress/decrypt-wrong-password-aes256.test +++ b/regress/decrypt-wrong-password-aes256.test @@ -1,4 +1,5 @@ # test AES decryption support, extract file using wrong password +features CRYPTO return 1 args encrypt.zzip set_password notfoonotfoo cat 1 file encrypt.zzip encrypt-aes256.zip encrypt-aes256.zip diff --git a/regress/encryption-nonrandom-aes128.test b/regress/encryption-nonrandom-aes128.test index d5f962e..fa21efe 100644 --- a/regress/encryption-nonrandom-aes128.test +++ b/regress/encryption-nonrandom-aes128.test @@ -1,3 +1,4 @@ +features CRYPTO description encrypt file without entropy, to make results repeatable return 0 preload nonrandomopen.so diff --git a/regress/encryption-nonrandom-aes192.test b/regress/encryption-nonrandom-aes192.test index 25294cb..bd0ebc4 100644 --- a/regress/encryption-nonrandom-aes192.test +++ b/regress/encryption-nonrandom-aes192.test @@ -1,3 +1,4 @@ +features CRYPTO description encrypt file without entropy, to make results repeatable return 0 preload nonrandomopen.so diff --git a/regress/encryption-nonrandom-aes256.test b/regress/encryption-nonrandom-aes256.test index d90a481..ee6be9f 100644 --- a/regress/encryption-nonrandom-aes256.test +++ b/regress/encryption-nonrandom-aes256.test @@ -1,3 +1,4 @@ +features CRYPTO description encrypt file without entropy, to make results repeatable return 0 preload nonrandomopen.so diff --git a/regress/encryption-remove.test b/regress/encryption-remove.test index f7bb236..2079100 100644 --- a/regress/encryption-remove.test +++ b/regress/encryption-remove.test @@ -1,4 +1,5 @@ # test AES decryption support, remove encryption from file that has it +features CRYPTO return 0 args encrypt.zzip set_password foofoofoo set_file_encryption 1 none "" file encrypt.zzip encrypt-aes128.zip encrypt-none.zip diff --git a/regress/fopen_unchanged.c b/regress/fopen_unchanged.c index 0256425..5634af7 100644 --- a/regress/fopen_unchanged.c +++ b/regress/fopen_unchanged.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -39,12 +39,11 @@ #include "zip.h" -const char *teststr="This is a test.\n"; -const char *file="teststring.txt"; +const char *teststr = "This is a test.\n"; +const char *file = "teststring.txt"; int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { const char *archive; zip_t *za; zip_source_t *zs; @@ -56,8 +55,8 @@ main(int argc, char *argv[]) } archive = argv[1]; - - if ((za=zip_open(archive, ZIP_CREATE, &err)) == NULL) { + + if ((za = zip_open(archive, ZIP_CREATE, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "can't open zip archive '%s': %s\n", archive, zip_error_strerror(&error)); @@ -65,7 +64,7 @@ main(int argc, char *argv[]) return 1; } - if ((zs=zip_source_buffer(za, teststr, strlen(teststr), 0)) == NULL) { + if ((zs = zip_source_buffer(za, teststr, strlen(teststr), 0)) == NULL) { fprintf(stderr, "can't create zip_source from buffer: %s\n", zip_strerror(za)); exit(1); } diff --git a/regress/fread.c b/regress/fread.c index bad5182..0a169c3 100644 --- a/regress/fread.c +++ b/regress/fread.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -46,13 +46,9 @@ #include "zip.h" -enum when { - WHEN_NEVER, WHEN_OPEN, WHEN_READ, WHEN_CLOSE -}; +enum when { WHEN_NEVER, WHEN_OPEN, WHEN_READ, WHEN_CLOSE }; -const char *when_name[] = { - "no", "zip_fopen", "zip_fread", "zip_fclose" -}; +const char *when_name[] = {"no", "zip_fopen", "zip_fread", "zip_fclose"}; static int do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex); @@ -62,8 +58,7 @@ const char *prg; #define USAGE "usage: %s [-v] archive\n" int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { int fail, ze; int c; zip_t *z; @@ -76,27 +71,27 @@ main(int argc, char *argv[]) prg = argv[0]; - while ((c=getopt(argc, argv, "v")) != -1) { + while ((c = getopt(argc, argv, "v")) != -1) { switch (c) { - case 'v': - verbose = 1; - break; + case 'v': + verbose = 1; + break; - default: - fprintf(stderr, USAGE, prg); - return 1; + default: + fprintf(stderr, USAGE, prg); + return 1; } } - - if (argc-optind != 1) { - fprintf(stderr, USAGE, prg); - return 1; + + if (argc - optind != 1) { + fprintf(stderr, USAGE, prg); + return 1; } archive = argv[optind]; - if ((z=zip_open(archive, 0, &ze)) == NULL) { + if ((z = zip_open(archive, 0, &ze)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, ze); fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error)); @@ -111,7 +106,7 @@ main(int argc, char *argv[]) fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3); fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0); fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0); - fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0,0); + fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0); fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0); fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED, WHEN_READ, ZIP_ER_CRC, 0); fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0); @@ -125,46 +120,46 @@ main(int argc, char *argv[]) zs = zip_source_buffer(z, "asdf", 4, 0); if ((idx = zip_name_locate(z, "storedok", 0)) < 0) { - fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - fail++; + fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + fail++; } else { - if (zip_replace(z, (zip_uint64_t)idx, zs) < 0) { - fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - fail++; - } - else { - fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); - fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); - } + if (zip_replace(z, (zip_uint64_t)idx, zs) < 0) { + fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + fail++; + } + else { + fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); + fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); + } } if ((idx = zip_name_locate(z, "storedok", 0)) < 0) { - fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - fail++; + fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + fail++; } else { - if (zip_delete(z, (zip_uint64_t)idx) < 0) { - fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - fail++; - } - else { - fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0); - fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); - } + if (zip_delete(z, (zip_uint64_t)idx) < 0) { + fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + fail++; + } + else { + fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0); + fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); + } } zs = zip_source_buffer(z, "asdf", 4, 0); if (zip_file_add(z, "new_file", zs, 0) < 0) { - fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - fail++; + fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + fail++; } else { - fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); + fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); } - + zip_unchange_all(z); if (zip_close(z) == -1) { - fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - return 1; + fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + return 1; } exit(fail ? 1 : 0); @@ -172,8 +167,7 @@ main(int argc, char *argv[]) static int -do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex) -{ +do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex) { zip_file_t *zf; enum when when_got; zip_error_t error_got, error_ex; @@ -186,14 +180,14 @@ do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze zip_error_init(&error_got); zip_error_init(&error_ex); zip_error_set(&error_ex, ze_ex, se_ex); - - if ((zf=zip_fopen(z, name, flags)) == NULL) { + + if ((zf = zip_fopen(z, name, flags)) == NULL) { when_got = WHEN_OPEN; zf_error = zip_get_error(z); zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error)); } else { - while ((n=zip_fread(zf, b, sizeof(b))) > 0) + while ((n = zip_fread(zf, b, sizeof(b))) > 0) ; if (n < 0) { when_got = WHEN_READ; @@ -208,10 +202,7 @@ do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze } if (when_got != when_ex || zip_error_code_zip(&error_got) != zip_error_code_zip(&error_ex) || zip_error_code_system(&error_got) != zip_error_code_system(&error_ex)) { - printf("%s: %s: got %s error (%s), expected %s error (%s)\n", - prg, name, - when_name[when_got], zip_error_strerror(&error_got), - when_name[when_ex], zip_error_strerror(&error_ex)); + printf("%s: %s: got %s error (%s), expected %s error (%s)\n", prg, name, when_name[when_got], zip_error_strerror(&error_got), when_name[when_ex], zip_error_strerror(&error_ex)); zip_error_fini(&error_got); zip_error_fini(&error_ex); return 1; diff --git a/regress/fseek.c b/regress/fseek.c index f266a60..3c0ba09 100644 --- a/regress/fseek.c +++ b/regress/fseek.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,8 +41,7 @@ const char *prg; #define USAGE "usage: %s archive index offset\n" int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { int ze; zip_t *z; zip_file_t *zf; @@ -54,15 +53,15 @@ main(int argc, char *argv[]) prg = argv[0]; if (argc != 4) { - fprintf(stderr, USAGE, prg); - return 1; + fprintf(stderr, USAGE, prg); + return 1; } archive = argv[1]; index = strtoull(argv[2], NULL, 10); offset = (zip_int64_t)strtoull(argv[3], NULL, 10); - if ((z=zip_open(archive, 0, &ze)) == NULL) { + if ((z = zip_open(archive, 0, &ze)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, ze); fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error)); @@ -70,7 +69,7 @@ main(int argc, char *argv[]) return 1; } - if ((zf=zip_fopen_index(z, index, 0)) == NULL) { + if ((zf = zip_fopen_index(z, index, 0)) == NULL) { fprintf(stderr, "%s: can't open file in archive '%s': %s\n", prg, archive, zip_error_strerror(zip_file_get_error(zf))); zip_close(z); return 1; @@ -81,8 +80,8 @@ main(int argc, char *argv[]) zip_close(z); return 1; } - - while ((n=zip_fread(zf, b, sizeof(b))) > 0) { + + while ((n = zip_fread(zf, b, sizeof(b))) > 0) { printf("%.*s", (int)n, b); } if (n < 0) { @@ -92,8 +91,8 @@ main(int argc, char *argv[]) } if (zip_close(z) == -1) { - fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); - return 1; + fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); + return 1; } return 0; diff --git a/regress/hole.c b/regress/hole.c index bc40696..722e218 100644 --- a/regress/hole.c +++ b/regress/hole.c @@ -1,10 +1,10 @@ /* hole.c -- convert huge files with mostly NULs to/from source_hole Copyright (C) 2014-2017 Dieter Baron and Thomas Klausner - + This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,8 +34,8 @@ #include "config.h" #include -#include #include +#include #include #ifdef HAVE_UNISTD_H #include @@ -55,89 +55,85 @@ const char *prg; static int -copy_source(zip_source_t *from, zip_source_t *to) -{ +copy_source(zip_source_t *from, zip_source_t *to) { zip_uint8_t buf[8192]; zip_int64_t n; - + if (zip_source_open(from) < 0) { - fprintf(stderr, "%s: can't open source for reading: %s\n", prg, zip_error_strerror(zip_source_error(from))); - return -1; + fprintf(stderr, "%s: can't open source for reading: %s\n", prg, zip_error_strerror(zip_source_error(from))); + return -1; } - + if (zip_source_begin_write(to) < 0) { - fprintf(stderr, "%s: can't open source for writing: %s\n", prg, zip_error_strerror(zip_source_error(to))); - zip_source_close(from); - return -1; + fprintf(stderr, "%s: can't open source for writing: %s\n", prg, zip_error_strerror(zip_source_error(to))); + zip_source_close(from); + return -1; } - + while ((n = zip_source_read(from, buf, sizeof(buf))) > 0) { - if (zip_source_write(to, buf, (zip_uint64_t)n) != n) { - fprintf(stderr, "%s: can't write to source: %s\n", prg, zip_error_strerror(zip_source_error(to))); - zip_source_close(from); - zip_source_rollback_write(to); - return -1; - } + if (zip_source_write(to, buf, (zip_uint64_t)n) != n) { + fprintf(stderr, "%s: can't write to source: %s\n", prg, zip_error_strerror(zip_source_error(to))); + zip_source_close(from); + zip_source_rollback_write(to); + return -1; + } } - + if (n < 0) { - fprintf(stderr, "%s: can't read from source: %s\n", prg, zip_error_strerror(zip_source_error(from))); - zip_source_close(from); - zip_source_rollback_write(to); - return -1; + fprintf(stderr, "%s: can't read from source: %s\n", prg, zip_error_strerror(zip_source_error(from))); + zip_source_close(from); + zip_source_rollback_write(to); + return -1; } zip_source_close(from); - + if (zip_source_commit_write(to) < 0) { - fprintf(stderr, "%s: can't commit source: %s\n", prg, zip_error_strerror(zip_source_error(to))); - zip_source_rollback_write(to); - return -1; + fprintf(stderr, "%s: can't commit source: %s\n", prg, zip_error_strerror(zip_source_error(to))); + zip_source_rollback_write(to); + return -1; } - + return 0; } static zip_source_t * -open_compressed(const char *fname, int flags) -{ +open_compressed(const char *fname, int flags) { zip_error_t error; zip_source_t *src; - + zip_error_init(&error); - + if ((src = source_hole_create(fname, flags, &error)) == NULL) { - fprintf(stderr, "%s: can't open compressed file %s: %s\n", prg, fname, zip_error_strerror(&error)); - zip_error_fini(&error); - exit(1); + fprintf(stderr, "%s: can't open compressed file %s: %s\n", prg, fname, zip_error_strerror(&error)); + zip_error_fini(&error); + exit(1); } - + return src; } static zip_source_t * -open_file(const char *fname) -{ +open_file(const char *fname) { zip_error_t error; zip_source_t *src; - + zip_error_init(&error); - + if ((src = zip_source_file_create(fname, 0, 0, &error)) == NULL) { - fprintf(stderr, "%s: can't open file %s: %s\n", prg, fname, zip_error_strerror(&error)); - zip_error_fini(&error); - exit(1); + fprintf(stderr, "%s: can't open file %s: %s\n", prg, fname, zip_error_strerror(&error)); + zip_error_fini(&error); + exit(1); } - + return src; } static void -usage(void) -{ +usage(void) { fprintf(stderr, "usage: %s [-du] in out\n", prg); fprintf(stderr, "\nOptions:\n -d decompress in\n -u update in\n"); exit(1); @@ -145,57 +141,55 @@ usage(void) int -main(int argc, char **argv) -{ +main(int argc, char **argv) { zip_source_t *from; zip_source_t *to; int c, err; int compress = 1; int decompress = 0; - + prg = argv[0]; - - while ((c=getopt(argc, argv, "du")) != -1) { - switch (c) { - case 'd': - compress = 0; - decompress = 1; - break; - - case 'u': - compress = 1; - decompress = 1; - break; - - default: - usage(); - break; - } + + while ((c = getopt(argc, argv, "du")) != -1) { + switch (c) { + case 'd': + compress = 0; + decompress = 1; + break; + + case 'u': + compress = 1; + decompress = 1; + break; + + default: + usage(); + break; + } } - - if (optind+2 != argc) { - usage(); + + if (optind + 2 != argc) { + usage(); } - + if (decompress) { - from = open_compressed(argv[optind], 0); + from = open_compressed(argv[optind], 0); } else { - from = open_file(argv[optind]); + from = open_file(argv[optind]); } if (compress) { - to = open_compressed(argv[optind+1], ZIP_CREATE); + to = open_compressed(argv[optind + 1], ZIP_CREATE); } else { - to = open_file(argv[optind+1]); + to = open_file(argv[optind + 1]); } - + err = copy_source(from, to); - + zip_source_free(from); zip_source_free(to); - + exit(err < 0 ? 1 : 0); } - diff --git a/regress/malloc.c b/regress/malloc.c index 8fbcdf0..e7d9b6e 100644 --- a/regress/malloc.c +++ b/regress/malloc.c @@ -42,7 +42,7 @@ #include "config.h" #if !defined(RTLD_NEXT) -#define RTLD_NEXT RTLD_DEFAULT +#define RTLD_NEXT RTLD_DEFAULT #endif #if defined(HAVE___PROGNAME) @@ -53,8 +53,7 @@ extern char *__progname; /* all fine */ #else const char * -getprogname(void) -{ +getprogname(void) { #if defined(HAVE___PROGNAME) return __progname; #else @@ -78,15 +77,14 @@ static const char *myname = NULL; /* TODO: optionally, catch malloc of particular size */ static void -init(void) -{ +init(void) { char *foo; myname = getprogname(); if (!myname) myname = "(unknown)"; - if ((foo=getenv("MALLOC_MAX_COUNT")) != NULL) + if ((foo = getenv("MALLOC_MAX_COUNT")) != NULL) max_count = strtoul(foo, NULL, 0); - if ((foo=getenv("MALLOC_MIN_SIZE")) != NULL) + if ((foo = getenv("MALLOC_MIN_SIZE")) != NULL) min_size = strtoul(foo, NULL, 0); real_calloc = dlsym(RTLD_NEXT, "calloc"); if (!real_calloc) @@ -101,30 +99,28 @@ init(void) } void * -calloc(size_t number, size_t size) -{ +calloc(size_t number, size_t size) { void *ret; if (!inited) { init(); } - if (number >= min_size/size && count >= max_count) { + if (number >= min_size / size && count >= max_count) { errno = ENOMEM; return NULL; } ret = real_calloc(number, size); if (size >= min_size) { - count++; + count++; } return ret; } void * -malloc(size_t size) -{ +malloc(size_t size) { void *ret; if (!inited) { @@ -138,15 +134,14 @@ malloc(size_t size) ret = real_malloc(size); if (size >= min_size) { - count++; + count++; } return ret; } void * -realloc(void *ptr, size_t size) -{ +realloc(void *ptr, size_t size) { void *ret; if (!inited) { @@ -160,7 +155,7 @@ realloc(void *ptr, size_t size) ret = real_realloc(ptr, size); if (size >= min_size) { - count++; + count++; } return ret; diff --git a/regress/nonrandomopen.c b/regress/nonrandomopen.c index 182d6ba..fdc67fa 100644 --- a/regress/nonrandomopen.c +++ b/regress/nonrandomopen.c @@ -33,14 +33,17 @@ */ #include -#include #include "compat.h" + +#ifdef HAVE_UNISTD_H +#include +#endif + #include "zipint.h" bool -zip_random(zip_uint8_t *buffer, zip_uint16_t length) -{ +zip_random(zip_uint8_t *buffer, zip_uint16_t length) { memset(buffer, 0, length); return true; diff --git a/regress/nonrandomopentest.c b/regress/nonrandomopentest.c index 5b7e6a7..1da7497 100644 --- a/regress/nonrandomopentest.c +++ b/regress/nonrandomopentest.c @@ -37,20 +37,21 @@ #include int -main(int argc, const char *argv[]) -{ +main(int argc, const char *argv[]) { char buf[1024]; int i; +#ifdef HAVE_CRYPTO if (!zip_random(buf, sizeof(buf))) { fprintf(stderr, "zip_random returned false\n"); exit(1); } - for (i=0; i #include -#include #include +#include #include #include "zip.h" @@ -45,17 +45,17 @@ zip_source_t *source_hole_create(const char *, int flags, zip_error_t *); #ifndef EFTYPE -#define EFTYPE EINVAL +#define EFTYPE EINVAL #endif -#define MY_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MY_MIN(a, b) ((a) < (b) ? (a) : (b)) -#define FRAGMENT_SIZE (8*1024) +#define FRAGMENT_SIZE (8 * 1024) -#define MARK_BEGIN "NiH0" -#define MARK_DATA "NiH1" -#define MARK_NUL "NiH2" +#define MARK_BEGIN "NiH0" +#define MARK_DATA "NiH1" +#define MARK_NUL "NiH2" typedef struct buffer { @@ -91,63 +91,60 @@ static hole_t *hole_new(const char *fname, int flags, zip_error_t *error); static zip_int64_t source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command); -zip_source_t *source_hole_create(const char *fname, int flags, zip_error_t *error) -{ +zip_source_t * +source_hole_create(const char *fname, int flags, zip_error_t *error) { hole_t *ud = hole_new(fname, flags, error); if (ud == NULL) { - return NULL; + return NULL; } return zip_source_function_create(source_hole_cb, ud, error); } static void -buffer_free(buffer_t *buffer) -{ +buffer_free(buffer_t *buffer) { zip_uint64_t i; if (buffer == NULL) { - return; + return; } if (buffer->fragment) { - for (i=0; infragments; i++) { - free(buffer->fragment[i]); - } - free(buffer->fragment); + for (i = 0; i < buffer->nfragments; i++) { + free(buffer->fragment[i]); + } + free(buffer->fragment); } free(buffer); } static buffer_t * -buffer_from_file(const char *fname, int flags, zip_error_t *error) -{ +buffer_from_file(const char *fname, int flags, zip_error_t *error) { buffer_t *buffer; FILE *f; if ((buffer = buffer_new()) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; - + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } if ((flags & ZIP_TRUNCATE) == 0) { - if ((f = fopen(fname, "rb")) == NULL) { - if (!(errno == ENOENT && (flags & ZIP_CREATE))) { - buffer_free(buffer); - return NULL; - } - } - else { - if (buffer_read_file(buffer, f, error) < 0) { - buffer_free(buffer); + if ((f = fopen(fname, "rb")) == NULL) { + if (!(errno == ENOENT && (flags & ZIP_CREATE))) { + buffer_free(buffer); + return NULL; + } + } + else { + if (buffer_read_file(buffer, f, error) < 0) { + buffer_free(buffer); fclose(f); - return NULL; - } + return NULL; + } fclose(f); - } + } } return buffer; @@ -155,12 +152,11 @@ buffer_from_file(const char *fname, int flags, zip_error_t *error) static buffer_t * -buffer_new(void) -{ +buffer_new(void) { buffer_t *buffer; if ((buffer = (buffer_t *)malloc(sizeof(*buffer))) == NULL) { - return NULL; + return NULL; } buffer->fragment = NULL; @@ -174,35 +170,34 @@ buffer_new(void) static zip_int64_t -buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) -{ +buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { zip_uint64_t n, i, fragment_offset; length = MY_MIN(length, buffer->size - buffer->offset); if (length == 0) { - return 0; + return 0; } if (length > ZIP_INT64_MAX) { - return -1; + return -1; } i = buffer->offset / buffer->fragment_size; fragment_offset = buffer->offset % buffer->fragment_size; n = 0; while (n < length) { - zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset); - - if (buffer->fragment[i]) { - memcpy(data + n, buffer->fragment[i] + fragment_offset, left); - } - else { - memset(data + n, 0, left); - } - - n += left; - i++; - fragment_offset = 0; + zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset); + + if (buffer->fragment[i]) { + memcpy(data + n, buffer->fragment[i] + fragment_offset, left); + } + else { + memset(data + n, 0, left); + } + + n += left; + i++; + fragment_offset = 0; } buffer->offset += n; @@ -211,84 +206,81 @@ buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length, zip_error_ static int -buffer_read_file(buffer_t *buffer, FILE *f, zip_error_t *error) -{ +buffer_read_file(buffer_t *buffer, FILE *f, zip_error_t *error) { zip_uint8_t b[20]; zip_uint64_t i; if (fread(b, 20, 1, f) != 1) { - zip_error_set(error, ZIP_ER_READ, errno); - return -1; + zip_error_set(error, ZIP_ER_READ, errno); + return -1; } if (memcmp(b, MARK_BEGIN, 4) != 0) { - zip_error_set(error, ZIP_ER_READ, EFTYPE); - return -1; + zip_error_set(error, ZIP_ER_READ, EFTYPE); + return -1; } - buffer->fragment_size = get_u64(b+4); - buffer->size = get_u64(b+12); + buffer->fragment_size = get_u64(b + 4); + buffer->size = get_u64(b + 12); if (buffer->size + buffer->fragment_size < buffer->size) { - zip_error_set(error, ZIP_ER_MEMORY, 0); + zip_error_set(error, ZIP_ER_MEMORY, 0); return -1; } buffer->nfragments = (buffer->size + buffer->fragment_size - 1) / buffer->fragment_size; - if ((buffer->nfragments > SIZE_MAX/sizeof(buffer->fragment[0])) - || ((buffer->fragment = (zip_uint8_t **)malloc(sizeof(buffer->fragment[0]) * buffer->nfragments)) == NULL)) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return -1; + if ((buffer->nfragments > SIZE_MAX / sizeof(buffer->fragment[0])) || ((buffer->fragment = (zip_uint8_t **)malloc(sizeof(buffer->fragment[0]) * buffer->nfragments)) == NULL)) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return -1; } for (i = 0; i < buffer->nfragments; i++) { - buffer->fragment[i] = NULL; + buffer->fragment[i] = NULL; } i = 0; while (i < buffer->nfragments) { - if (fread(b, 4, 1, f) != 1) { - zip_error_set(error, ZIP_ER_READ, errno); - return -1; - } - - if (memcmp(b, MARK_DATA, 4) == 0) { - if (buffer->fragment_size > SIZE_MAX) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return -1; - } - if ((buffer->fragment[i] = (zip_uint8_t *)malloc(buffer->fragment_size)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return -1; - } - if (fread(buffer->fragment[i], buffer->fragment_size, 1, f) != 1) { - zip_error_set(error, ZIP_ER_READ, errno); - return -1; - } - i++; - } - else if (memcmp(b, MARK_NUL, 4) == 0) { - if (fread(b, 8, 1, f) != 1) { - zip_error_set(error, ZIP_ER_READ, errno); - return -1; - } - i += get_u64(b); - } - else { - zip_error_set(error, ZIP_ER_READ, EFTYPE); - return -1; - } + if (fread(b, 4, 1, f) != 1) { + zip_error_set(error, ZIP_ER_READ, errno); + return -1; + } + + if (memcmp(b, MARK_DATA, 4) == 0) { + if (buffer->fragment_size > SIZE_MAX) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return -1; + } + if ((buffer->fragment[i] = (zip_uint8_t *)malloc(buffer->fragment_size)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return -1; + } + if (fread(buffer->fragment[i], buffer->fragment_size, 1, f) != 1) { + zip_error_set(error, ZIP_ER_READ, errno); + return -1; + } + i++; + } + else if (memcmp(b, MARK_NUL, 4) == 0) { + if (fread(b, 8, 1, f) != 1) { + zip_error_set(error, ZIP_ER_READ, errno); + return -1; + } + i += get_u64(b); + } + else { + zip_error_set(error, ZIP_ER_READ, EFTYPE); + return -1; + } } return 0; } static zip_int64_t -buffer_seek(buffer_t *buffer, void *data, zip_uint64_t length, zip_error_t *error) -{ +buffer_seek(buffer_t *buffer, void *data, zip_uint64_t length, zip_error_t *error) { zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, length, error); if (new_offset < 0) { - return -1; + return -1; } buffer->offset = (zip_uint64_t)new_offset; @@ -297,15 +289,14 @@ buffer_seek(buffer_t *buffer, void *data, zip_uint64_t length, zip_error_t *erro static int -buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error) -{ +buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error) { FILE *f = fopen(fname, "wb"); zip_uint64_t i; zip_uint64_t nul_run; if (f == NULL) { - zip_error_set(error, ZIP_ER_OPEN, errno); - return -1; + zip_error_set(error, ZIP_ER_OPEN, errno); + return -1; } fwrite(MARK_BEGIN, 4, 1, f); @@ -313,28 +304,28 @@ buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error) write_u64(buffer->size, f); nul_run = 0; - for (i=0; i * buffer->fragment_size size; i++) { - if (buffer->fragment[i] == NULL || only_nul(buffer->fragment[i], buffer->fragment_size)) { - nul_run++; - } - else { - if (nul_run > 0) { - write_nuls(nul_run, f); - nul_run = 0; - } - fwrite(MARK_DATA, 4, 1, f); - - fwrite(buffer->fragment[i], 1, buffer->fragment_size, f); - } + for (i = 0; i * buffer->fragment_size < buffer->size; i++) { + if (buffer->fragment[i] == NULL || only_nul(buffer->fragment[i], buffer->fragment_size)) { + nul_run++; + } + else { + if (nul_run > 0) { + write_nuls(nul_run, f); + nul_run = 0; + } + fwrite(MARK_DATA, 4, 1, f); + + fwrite(buffer->fragment[i], 1, buffer->fragment_size, f); + } } if (nul_run > 0) { - write_nuls(nul_run, f); + write_nuls(nul_run, f); } if (fclose(f) != 0) { - zip_error_set(error, ZIP_ER_WRITE, errno); - return -1; + zip_error_set(error, ZIP_ER_WRITE, errno); + return -1; } return 0; @@ -342,64 +333,63 @@ buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error) static zip_int64_t -buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) -{ +buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { zip_uint8_t **fragment; if (buffer->offset + length > buffer->nfragments * buffer->fragment_size) { - zip_uint64_t needed_fragments = (buffer->offset + length + buffer->fragment_size - 1) / buffer->fragment_size; - zip_uint64_t new_capacity = buffer->nfragments; - zip_uint64_t i; - - if (new_capacity == 0) { - new_capacity = 4; - } - while (new_capacity < needed_fragments) { - new_capacity *= 2; - } - - fragment = realloc(buffer->fragment, new_capacity * sizeof(*fragment)); - - if (fragment == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return -1; - } - - for (i = buffer->nfragments; i < new_capacity; i++) { - fragment[i] = NULL; - } - - buffer->fragment = fragment; - buffer->nfragments = new_capacity; + zip_uint64_t needed_fragments = (buffer->offset + length + buffer->fragment_size - 1) / buffer->fragment_size; + zip_uint64_t new_capacity = buffer->nfragments; + zip_uint64_t i; + + if (new_capacity == 0) { + new_capacity = 4; + } + while (new_capacity < needed_fragments) { + new_capacity *= 2; + } + + fragment = realloc(buffer->fragment, new_capacity * sizeof(*fragment)); + + if (fragment == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return -1; + } + + for (i = buffer->nfragments; i < new_capacity; i++) { + fragment[i] = NULL; + } + + buffer->fragment = fragment; + buffer->nfragments = new_capacity; } if (!only_nul(data, length)) { - zip_uint64_t idx, n, fragment_offset; - - idx = buffer->offset / buffer->fragment_size; - fragment_offset = buffer->offset % buffer->fragment_size; - n = 0; - - while (n < length) { - zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset); - - if (buffer->fragment[idx] == NULL) { - if ((buffer->fragment[idx] = (zip_uint8_t *)malloc(buffer->fragment_size)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return -1; - } - memset(buffer->fragment[idx], 0, buffer->fragment_size); - } - memcpy(buffer->fragment[idx] + fragment_offset, data + n, left); - - n += left; - idx++; - fragment_offset = 0; - } + zip_uint64_t idx, n, fragment_offset; + + idx = buffer->offset / buffer->fragment_size; + fragment_offset = buffer->offset % buffer->fragment_size; + n = 0; + + while (n < length) { + zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset); + + if (buffer->fragment[idx] == NULL) { + if ((buffer->fragment[idx] = (zip_uint8_t *)malloc(buffer->fragment_size)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return -1; + } + memset(buffer->fragment[idx], 0, buffer->fragment_size); + } + memcpy(buffer->fragment[idx] + fragment_offset, data + n, left); + + n += left; + idx++; + fragment_offset = 0; + } } buffer->offset += length; if (buffer->offset > buffer->size) { - buffer->size = buffer->offset; + buffer->size = buffer->offset; } return (zip_int64_t)length; @@ -407,8 +397,7 @@ buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip static zip_uint64_t -get_u64(const zip_uint8_t *b) -{ +get_u64(const zip_uint8_t *b) { zip_uint64_t i; i = (zip_uint64_t)b[0] << 56 | (zip_uint64_t)b[1] << 48 | (zip_uint64_t)b[2] << 40 | (zip_uint64_t)b[3] << 32 | (zip_uint64_t)b[4] << 24 | (zip_uint64_t)b[5] << 16 | (zip_uint64_t)b[6] << 8 | (zip_uint64_t)b[7]; @@ -418,14 +407,13 @@ get_u64(const zip_uint8_t *b) static int -only_nul(const zip_uint8_t *data, zip_uint64_t length) -{ +only_nul(const zip_uint8_t *data, zip_uint64_t length) { zip_uint64_t i; - for (i=0; i< length; i++) { - if (data[i] != '\0') { - return 0; - } + for (i = 0; i < length; i++) { + if (data[i] != '\0') { + return 0; + } } return 1; @@ -433,18 +421,16 @@ only_nul(const zip_uint8_t *data, zip_uint64_t length) static int -write_nuls(zip_uint64_t n, FILE *f) -{ +write_nuls(zip_uint64_t n, FILE *f) { if (fwrite(MARK_NUL, 4, 1, f) != 1) { - return -1; + return -1; } return write_u64(n, f); } static int -write_u64(zip_uint64_t u64, FILE *f) -{ +write_u64(zip_uint64_t u64, FILE *f) { zip_uint8_t b[8]; b[0] = (zip_uint8_t)((u64 >> 56) & 0xff); @@ -463,7 +449,7 @@ write_u64(zip_uint64_t u64, FILE *f) static void hole_free(hole_t *hole) { if (hole == NULL) { - return; + return; } zip_error_fini(&hole->error); buffer_free(hole->in); @@ -474,24 +460,23 @@ hole_free(hole_t *hole) { static hole_t * -hole_new(const char *fname, int flags, zip_error_t *error) -{ +hole_new(const char *fname, int flags, zip_error_t *error) { hole_t *ctx = (hole_t *)malloc(sizeof(*ctx)); if (ctx == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } if ((ctx->fname = strdup(fname)) == NULL) { - free(ctx); - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + free(ctx); + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } if ((ctx->in = buffer_from_file(fname, flags, error)) == NULL) { - free(ctx); - return NULL; + free(ctx); + return NULL; } zip_error_init(&ctx->error); @@ -502,88 +487,87 @@ hole_new(const char *fname, int flags, zip_error_t *error) static zip_int64_t -source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command) -{ +source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command) { hole_t *ctx = (hole_t *)ud; switch (command) { - case ZIP_SOURCE_BEGIN_WRITE: - ctx->out = buffer_new(); - return 0; - - case ZIP_SOURCE_CLOSE: - return 0; - - case ZIP_SOURCE_COMMIT_WRITE: - if (buffer_to_file(ctx->out, ctx->fname, &ctx->error) < 0) { - return -1; - } - buffer_free(ctx->in); - ctx->in = ctx->out; - ctx->out = NULL; - return 0; - - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, length); - - case ZIP_SOURCE_FREE: - hole_free(ctx); - return 0; - - case ZIP_SOURCE_OPEN: - ctx->in->offset = 0; - return 0; - - case ZIP_SOURCE_READ: - return buffer_read(ctx->in, data, length, &ctx->error); - - case ZIP_SOURCE_REMOVE: - buffer_free(ctx->in); - ctx->in = buffer_new(); - buffer_free(ctx->out); - ctx->out = NULL; - (void)remove(ctx->fname); - return 0; - - case ZIP_SOURCE_ROLLBACK_WRITE: - buffer_free(ctx->out); - ctx->out = NULL; - return 0; - - case ZIP_SOURCE_SEEK: - return buffer_seek(ctx->in, data, length, &ctx->error); - - case ZIP_SOURCE_SEEK_WRITE: - return buffer_seek(ctx->out, data, length, &ctx->error); - - case ZIP_SOURCE_STAT: { - zip_stat_t *st = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, length, &ctx->error); - - if (st == NULL) { - return -1; - } - - /* TODO: return ENOENT if fname doesn't exist */ - - st->valid |= ZIP_STAT_SIZE; - st->size = ctx->in->size; - return 0; - } + case ZIP_SOURCE_BEGIN_WRITE: + ctx->out = buffer_new(); + return 0; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_COMMIT_WRITE: + if (buffer_to_file(ctx->out, ctx->fname, &ctx->error) < 0) { + return -1; + } + buffer_free(ctx->in); + ctx->in = ctx->out; + ctx->out = NULL; + return 0; + + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, length); + + case ZIP_SOURCE_FREE: + hole_free(ctx); + return 0; + + case ZIP_SOURCE_OPEN: + ctx->in->offset = 0; + return 0; + + case ZIP_SOURCE_READ: + return buffer_read(ctx->in, data, length, &ctx->error); + + case ZIP_SOURCE_REMOVE: + buffer_free(ctx->in); + ctx->in = buffer_new(); + buffer_free(ctx->out); + ctx->out = NULL; + (void)remove(ctx->fname); + return 0; + + case ZIP_SOURCE_ROLLBACK_WRITE: + buffer_free(ctx->out); + ctx->out = NULL; + return 0; + + case ZIP_SOURCE_SEEK: + return buffer_seek(ctx->in, data, length, &ctx->error); + + case ZIP_SOURCE_SEEK_WRITE: + return buffer_seek(ctx->out, data, length, &ctx->error); + + case ZIP_SOURCE_STAT: { + zip_stat_t *st = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, length, &ctx->error); + + if (st == NULL) { + return -1; + } + + /* TODO: return ENOENT if fname doesn't exist */ + + st->valid |= ZIP_STAT_SIZE; + st->size = ctx->in->size; + return 0; + } - case ZIP_SOURCE_TELL: - return (zip_int64_t)ctx->in->offset; + case ZIP_SOURCE_TELL: + return (zip_int64_t)ctx->in->offset; - case ZIP_SOURCE_TELL_WRITE: - return (zip_int64_t)ctx->out->offset; + case ZIP_SOURCE_TELL_WRITE: + return (zip_int64_t)ctx->out->offset; - case ZIP_SOURCE_WRITE: - return buffer_write(ctx->out, data, length, &ctx->error); + case ZIP_SOURCE_WRITE: + return buffer_write(ctx->out, data, length, &ctx->error); - case ZIP_SOURCE_SUPPORTS: - return zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_STAT, ZIP_SOURCE_TELL, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, -1); + case ZIP_SOURCE_SUPPORTS: + return zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_STAT, ZIP_SOURCE_TELL, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, -1); - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } diff --git a/regress/tryopen.c b/regress/tryopen.c index 6f2b924..b329004 100644 --- a/regress/tryopen.c +++ b/regress/tryopen.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -48,15 +48,14 @@ #include "zip.h" const char *usage = "usage: %s [-cent] file\n\n" - "\t-c\tcheck consistency\n" - "\t-e\texclusively open archive\n" - "\t-n\tcreate new file\n" - "\t-t\ttruncate file to size 0\n"; + "\t-c\tcheck consistency\n" + "\t-e\texclusively open archive\n" + "\t-n\tcreate new file\n" + "\t-t\ttruncate file to size 0\n"; int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { const char *fname; zip_t *z; int c, flags, ze; @@ -65,7 +64,7 @@ main(int argc, char *argv[]) flags = 0; - while ((c=getopt(argc, argv, "cent")) != -1) { + while ((c = getopt(argc, argv, "cent")) != -1) { switch (c) { case 'c': flags |= ZIP_CHECKCONS; @@ -91,13 +90,13 @@ main(int argc, char *argv[]) fname = argv[optind]; errno = 0; - if ((z=zip_open(fname, flags, &ze)) != NULL) { + if ((z = zip_open(fname, flags, &ze)) != NULL) { count = zip_get_num_entries(z, 0); - printf("opening '%s' succeeded, %"PRIu64" entries\n", fname, count); + printf("opening '%s' succeeded, %" PRIu64 " entries\n", fname, count); zip_close(z); continue; } - + printf("opening '%s' returned error %d", fname, ze); if (zip_error_get_sys_type(ze) == ZIP_ET_SYS) printf("/%d", errno); diff --git a/regress/ziptool_regress.c b/regress/ziptool_regress.c index ba0bc34..daf3d62 100644 --- a/regress/ziptool_regress.c +++ b/regress/ziptool_regress.c @@ -1,14 +1,10 @@ #include "zip.h" -#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ZIP_MIN(a, b) ((a) < (b) ? (a) : (b)) #define FOR_REGRESS -typedef enum { - SOURCE_TYPE_NONE, - SOURCE_TYPE_IN_MEMORY, - SOURCE_TYPE_HOLE -} source_type_t; +typedef enum { SOURCE_TYPE_NONE, SOURCE_TYPE_IN_MEMORY, SOURCE_TYPE_HOLE } source_type_t; source_type_t source_type = SOURCE_TYPE_NONE; zip_uint64_t fragment_size = 0; @@ -21,21 +17,19 @@ static int zin_close(int argc, char *argv[]); #define USAGE_REGRESS " [-Hm] [-F fragment-size]" -#define GETOPT_REGRESS \ - case 'H': \ - source_type = SOURCE_TYPE_HOLE; \ - break; \ - case 'm': \ - source_type = SOURCE_TYPE_IN_MEMORY; \ - break; \ - case 'F': \ - fragment_size = strtoull(optarg, NULL, 10); \ - break; +#define GETOPT_REGRESS \ + case 'H': \ + source_type = SOURCE_TYPE_HOLE; \ + break; \ + case 'm': \ + source_type = SOURCE_TYPE_IN_MEMORY; \ + break; \ + case 'F': \ + fragment_size = strtoull(optarg, NULL, 10); \ + break; #define DISPATCH_REGRESS \ - { "add_nul", 2, "name length", "add NUL bytes", add_nul }, \ - { "unchange_all", 0, "", "revert all changes", unchange_all }, \ - { "zin_close", 1, "index", "close input zip_source (for internal tests)", zin_close } + {"add_nul", 2, "name length", "add NUL bytes", add_nul}, {"unchange_all", 0, "", "revert all changes", unchange_all}, { "zin_close", 1, "index", "close input zip_source (for internal tests)", zin_close } zip_t *ziptool_open(const char *archive, int flags, zip_error_t *error, zip_uint64_t offset, zip_uint64_t len); @@ -57,15 +51,15 @@ add_nul(int argc, char *argv[]) { zip_source_t *zs; zip_uint64_t length = strtoull(argv[1], NULL, 10); - if ((zs=source_nul(za, length)) == NULL) { - fprintf(stderr, "can't create zip_source for length: %s\n", zip_strerror(za)); - return -1; + if ((zs = source_nul(za, length)) == NULL) { + fprintf(stderr, "can't create zip_source for length: %s\n", zip_strerror(za)); + return -1; } if (zip_add(za, argv[0], zs) == -1) { - zip_source_free(zs); - fprintf(stderr, "can't add file '%s': %s\n", argv[0], zip_strerror(za)); - return -1; + zip_source_free(zs); + fprintf(stderr, "can't add file '%s': %s\n", argv[0], zip_strerror(za)); + return -1; } return 0; } @@ -100,8 +94,7 @@ zin_close(int argc, char *argv[]) { static zip_t * -read_hole(const char *archive, int flags, zip_error_t *error) -{ +read_hole(const char *archive, int flags, zip_error_t *error) { zip_source_t *src = NULL; zip_t *zs = NULL; @@ -110,9 +103,8 @@ read_hole(const char *archive, int flags, zip_error_t *error) return NULL; } - if ((src = source_hole_create(archive, flags, error)) == NULL - || (zs = zip_open_from_source(src, flags, error)) == NULL) { - zip_source_free(src); + if ((src = source_hole_create(archive, flags, error)) == NULL || (zs = zip_open_from_source(src, flags, error)) == NULL) { + zip_source_free(src); } return zs; @@ -120,8 +112,7 @@ read_hole(const char *archive, int flags, zip_error_t *error) static zip_t * -read_to_memory(const char *archive, int flags, zip_error_t *error, zip_source_t **srcp) -{ +read_to_memory(const char *archive, int flags, zip_error_t *error, zip_source_t **srcp) { zip_source_t *src; zip_t *zb; FILE *fp; @@ -131,7 +122,7 @@ read_to_memory(const char *archive, int flags, zip_error_t *error, zip_source_t return NULL; } - if ((fp=fopen(archive, "r")) == NULL) { + if ((fp = fopen(archive, "r")) == NULL) { if (errno == ENOENT) { src = zip_source_buffer_create(NULL, 0, 0, error); } @@ -141,74 +132,81 @@ read_to_memory(const char *archive, int flags, zip_error_t *error, zip_source_t } } else { - struct stat st; + struct stat st; - if (fstat(fileno(fp), &st) < 0) { + if (fstat(fileno(fp), &st) < 0) { fclose(fp); - zip_error_set(error, ZIP_ER_OPEN, errno); - return NULL; - } - if (fragment_size == 0) { - char *buf; - if ((buf=malloc((size_t)st.st_size)) == NULL) { - fclose(fp); - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; - } - if (fread(buf, (size_t)st.st_size, 1, fp) < 1) { - free(buf); - fclose(fp); - zip_error_set(error, ZIP_ER_READ, errno); - return NULL; - } - src = zip_source_buffer_create(buf, (zip_uint64_t)st.st_size, 1, error); - if (src == NULL) { - free(buf); - } - } - else { - zip_uint64_t nfragments, i, left; - zip_buffer_fragment_t *fragments; - - nfragments = ((size_t)st.st_size + fragment_size - 1) / fragment_size; - if ((fragments = malloc(sizeof(fragments[0]) * nfragments)) == NULL) { - fclose(fp); - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; - } - for (i = 0; i < nfragments; i++) { - left = ZIP_MIN(fragment_size, (size_t)st.st_size - i * fragment_size); - if ((fragments[i].data = malloc(left)) == NULL) { - while (--i > 0) { - free(fragments[i].data); - } - free(fragments); - fclose(fp); - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; - } - fragments[i].length = left; - if (fread(fragments[i].data, left, 1, fp) < 1) { - while (--i > 0) { - free(fragments[i].data); + zip_error_set(error, ZIP_ER_OPEN, errno); + return NULL; + } + if (fragment_size == 0) { + char *buf; + if ((buf = malloc((size_t)st.st_size)) == NULL) { + fclose(fp); + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + if (fread(buf, (size_t)st.st_size, 1, fp) < 1) { + free(buf); + fclose(fp); + zip_error_set(error, ZIP_ER_READ, errno); + return NULL; + } + src = zip_source_buffer_create(buf, (zip_uint64_t)st.st_size, 1, error); + if (src == NULL) { + free(buf); + } + } + else { + zip_uint64_t nfragments, i, left; + zip_buffer_fragment_t *fragments; + + nfragments = ((size_t)st.st_size + fragment_size - 1) / fragment_size; + if ((fragments = malloc(sizeof(fragments[0]) * nfragments)) == NULL) { + fclose(fp); + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + for (i = 0; i < nfragments; i++) { + left = ZIP_MIN(fragment_size, (size_t)st.st_size - i * fragment_size); + if ((fragments[i].data = malloc(left)) == NULL) { +#ifndef __clang_analyzer__ + /* fragments is initialized up to i - 1*/ + while (--i > 0) { + free(fragments[i].data); } - free(fragments); - fclose(fp); - zip_error_set(error, ZIP_ER_READ, errno); - return NULL; - } - } - src = zip_source_buffer_fragment_create(fragments, nfragments, 1, error); - if (src == NULL) { - for (i = 0; i < nfragments; i++) { - free(fragments[i].data); - } - free(fragments); - fclose(fp); - return NULL; - } - } - fclose(fp); +#endif + free(fragments); + fclose(fp); + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + fragments[i].length = left; + if (fread(fragments[i].data, left, 1, fp) < 1) { +#ifndef __clang_analyzer__ + /* fragments is initialized up to i - 1*/ + while (--i > 0) { + free(fragments[i].data); + } +#endif + free(fragments); + fclose(fp); + zip_error_set(error, ZIP_ER_READ, errno); + return NULL; + } + } + src = zip_source_buffer_fragment_create(fragments, nfragments, 1, error); + if (src == NULL) { + for (i = 0; i < nfragments; i++) { + free(fragments[i].data); + } + free(fragments); + fclose(fp); + return NULL; + } + free(fragments); + } + fclose(fp); } if (src == NULL) { return NULL; @@ -231,70 +229,68 @@ typedef struct source_nul { } source_nul_t; static zip_int64_t -source_nul_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command) -{ +source_nul_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command) { source_nul_t *ctx = (source_nul_t *)ud; switch (command) { - case ZIP_SOURCE_CLOSE: - return 0; + case ZIP_SOURCE_CLOSE: + return 0; - case ZIP_SOURCE_ERROR: - return zip_error_to_data(&ctx->error, data, length); + case ZIP_SOURCE_ERROR: + return zip_error_to_data(&ctx->error, data, length); - case ZIP_SOURCE_FREE: - free(ctx); - return 0; + case ZIP_SOURCE_FREE: + free(ctx); + return 0; - case ZIP_SOURCE_OPEN: - ctx->offset = 0; - return 0; + case ZIP_SOURCE_OPEN: + ctx->offset = 0; + return 0; - case ZIP_SOURCE_READ: - if (length > ZIP_INT64_MAX) { - zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); - return -1; - } + case ZIP_SOURCE_READ: + if (length > ZIP_INT64_MAX) { + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); + return -1; + } - if (length > ctx->length - ctx->offset) { - length =ctx->length - ctx->offset; - } + if (length > ctx->length - ctx->offset) { + length = ctx->length - ctx->offset; + } - memset(data, 0, length); - ctx->offset += length; - return (zip_int64_t)length; + memset(data, 0, length); + ctx->offset += length; + return (zip_int64_t)length; - case ZIP_SOURCE_STAT: { - zip_stat_t *st = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, length, &ctx->error); + case ZIP_SOURCE_STAT: { + zip_stat_t *st = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, length, &ctx->error); - if (st == NULL) { - return -1; - } + if (st == NULL) { + return -1; + } - st->valid |= ZIP_STAT_SIZE; - st->size = ctx->length; + st->valid |= ZIP_STAT_SIZE; + st->size = ctx->length; - return 0; - } + return 0; + } - case ZIP_SOURCE_SUPPORTS: - return zip_source_make_command_bitmap(ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_STAT, -1); + case ZIP_SOURCE_SUPPORTS: + return zip_source_make_command_bitmap(ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_STAT, -1); - default: - zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); - return -1; + default: + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); + return -1; } } static zip_source_t * -source_nul(zip_t *zs, zip_uint64_t length) -{ +source_nul(zip_t *zs, zip_uint64_t length) { source_nul_t *ctx; zip_source_t *src; if ((ctx = (source_nul_t *)malloc(sizeof(*ctx))) == NULL) { - zip_error_set(zip_get_error(zs), ZIP_ER_MEMORY, 0); - return NULL; + zip_error_set(zip_get_error(zs), ZIP_ER_MEMORY, 0); + return NULL; } zip_error_init(&ctx->error); @@ -302,8 +298,8 @@ source_nul(zip_t *zs, zip_uint64_t length) ctx->offset = 0; if ((src = zip_source_function(zs, source_nul_cb, ctx)) == NULL) { - free(ctx); - return NULL; + free(ctx); + return NULL; } return src; @@ -311,8 +307,7 @@ source_nul(zip_t *zs, zip_uint64_t length) static int -write_memory_src_to_file(const char *archive, zip_source_t *src) -{ +write_memory_src_to_file(const char *archive, zip_source_t *src) { zip_stat_t zst; char *buf; FILE *fp; @@ -332,7 +327,7 @@ write_memory_src_to_file(const char *archive, zip_source_t *src) fprintf(stderr, "zip_source_open on buffer failed: %s\n", zip_error_strerror(zip_source_error(src))); return -1; } - if ((buf=malloc(zst.size)) == NULL) { + if ((buf = malloc(zst.size)) == NULL) { fprintf(stderr, "malloc failed: %s\n", strerror(errno)); zip_source_close(src); return -1; @@ -344,7 +339,7 @@ write_memory_src_to_file(const char *archive, zip_source_t *src) return -1; } zip_source_close(src); - if ((fp=fopen(archive, "wb")) == NULL) { + if ((fp = fopen(archive, "wb")) == NULL) { fprintf(stderr, "fopen failed: %s\n", strerror(errno)); free(buf); return -1; @@ -367,17 +362,17 @@ write_memory_src_to_file(const char *archive, zip_source_t *src) zip_t * ziptool_open(const char *archive, int flags, zip_error_t *error, zip_uint64_t offset, zip_uint64_t len) { switch (source_type) { - case SOURCE_TYPE_NONE: - za = read_from_file(archive, flags, error, offset, len); - break; + case SOURCE_TYPE_NONE: + za = read_from_file(archive, flags, error, offset, len); + break; - case SOURCE_TYPE_IN_MEMORY: - za = read_to_memory(archive, flags, error, &memory_src); - break; + case SOURCE_TYPE_IN_MEMORY: + za = read_to_memory(archive, flags, error, &memory_src); + break; - case SOURCE_TYPE_HOLE: - za = read_hole(archive, flags, error); - break; + case SOURCE_TYPE_HOLE: + za = read_hole(archive, flags, error); + break; } return za; @@ -395,4 +390,3 @@ ziptool_post_close(const char *archive) { return 0; } - diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a3f4d2..ccc8f7e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,12 +8,12 @@ ENDIF(NOT HAVE_GETOPT) ADD_EXECUTABLE(zipcmp zipcmp.c ${SRC_EXTRA_FILES}) TARGET_LINK_LIBRARIES(zipcmp zip) -INSTALL(TARGETS zipcmp RUNTIME DESTINATION bin) +INSTALL(TARGETS zipcmp RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ADD_EXECUTABLE(zipmerge zipmerge.c ${SRC_EXTRA_FILES}) TARGET_LINK_LIBRARIES(zipmerge zip) -INSTALL(TARGETS zipmerge RUNTIME DESTINATION bin) +INSTALL(TARGETS zipmerge RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ADD_EXECUTABLE(ziptool ziptool.c ${SRC_EXTRA_FILES}) TARGET_LINK_LIBRARIES(ziptool zip) -INSTALL(TARGETS ziptool RUNTIME DESTINATION bin) +INSTALL(TARGETS ziptool RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/src/getopt.c b/src/getopt.c index aaf840f..befd672 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -41,77 +41,70 @@ #include "getopt.h" -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ -#define BADCH (int)'?' -#define BADARG (int)':' -#define EMSG "" +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" /* * getopt -- * Parse argc/argv argument vector. */ int -getopt( int nargc, - char * const *nargv, - const char *ostr) -{ - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ +getopt(int nargc, char *const *nargv, const char *ostr) { + static char *place = EMSG; /* option letter processing */ + char *oli; /* option letter list index */ - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc || *(place = nargv[optind]) != '-') { - place = EMSG; - return (EOF); - } - if (place[1] && *++place == '-') { /* found "--" */ - ++optind; - place = EMSG; - return (EOF); - } - } /* option letter okay? */ - if ((optopt = (int)*place++) == (int)':' || - !(oli = (char*) strchr(ostr, optopt))) { - /* - * if the user didn't specify '-' as an option, - * assume it means EOF. - */ - if (optopt == (int)'-') - return (EOF); - if (!*place) - ++optind; - if (opterr && *ostr != ':') - (void)fprintf(stderr, - "illegal option -- %c\n", optopt); - return (BADCH); - } - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; - } - else { /* need an argument */ - if (*place) /* no white space */ - optarg = place; - else if (nargc <= ++optind) { /* no arg */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void)fprintf(stderr, - "option requires an argument -- %c\n", - optopt); - return (BADCH); - } - else /* white space */ - optarg = nargv[optind]; - place = EMSG; - ++optind; - } - return (optopt); /* dump back option letter */ + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (EOF); + } + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (EOF); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || !(oli = (char *)strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means EOF. + */ + if (optopt == (int)'-') + return (EOF); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)fprintf(stderr, "illegal option -- %c\n", optopt); + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; + } + else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)fprintf(stderr, "option requires an argument -- %c\n", optopt); + return (BADCH); + } + else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + return (optopt); /* dump back option letter */ } diff --git a/src/getopt.h b/src/getopt.h index 5bcb06f..97b48d1 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -42,7 +42,7 @@ extern char *optarg; extern int optind; extern int opterr; -extern int getopt(int, char * const *, const char *); +extern int getopt(int, char *const *, const char *); #ifdef __cplusplus } diff --git a/src/zipcmp.c b/src/zipcmp.c index ebb23ae..b01844f 100644 --- a/src/zipcmp.c +++ b/src/zipcmp.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,11 +34,11 @@ #include "config.h" -#include #include #include #include #include +#include #ifdef HAVE_STRINGS_H #include #endif @@ -88,12 +88,11 @@ struct entry { const char *prg; -#define PROGRAM "zipcmp" +#define PROGRAM "zipcmp" #define USAGE "usage: %s [-hipqtVv] archive1 archive2\n" -char help_head[] = - PROGRAM " (" PACKAGE ") by Dieter Baron and Thomas Klausner\n\n"; +char help_head[] = PROGRAM " (" PACKAGE ") by Dieter Baron and Thomas Klausner\n\n"; char help[] = "\n\ -h display this help message\n\ @@ -113,15 +112,11 @@ Copyright (C) 2003-2017 Dieter Baron and Thomas Klausner\n\ #define OPTIONS "hVipqtv" -#define BOTH_ARE_ZIPS(a) (a[0].za && a[1].za) +#define BOTH_ARE_ZIPS(a) (a[0].za && a[1].za) static int comment_compare(const char *c1, size_t l1, const char *c2, size_t l2); -static int compare_list(char * const name[], - const void *l[], const zip_uint64_t n[], int size, - int (*cmp)(const void *, const void *), - int (*checks)(char *const name[2], const void *, const void *), - void (*print)(const void *)); -static int compare_zip(char * const zn[]); +static int compare_list(char *const name[], const void *l[], const zip_uint64_t n[], int size, int (*cmp)(const void *, const void *), int (*checks)(char *const name[2], const void *, const void *), void (*print)(const void *)); +static int compare_zip(char *const zn[]); static int ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2); static int ef_order(const void *a, const void *b); static void ef_print(const void *p); @@ -141,8 +136,7 @@ int header_done; int -main(int argc, char * const argv[]) -{ +main(int argc, char *const argv[]) { int c; prg = argv[0]; @@ -152,7 +146,7 @@ main(int argc, char * const argv[]) paranoid = 0; verbose = 1; - while ((c=getopt(argc, argv, OPTIONS)) != -1) { + while ((c = getopt(argc, argv, OPTIONS)) != -1) { switch (c) { case 'i': ignore_case = 1; @@ -185,44 +179,43 @@ main(int argc, char * const argv[]) } } - if (argc != optind+2) { + if (argc != optind + 2) { fprintf(stderr, USAGE, prg); exit(2); } - exit((compare_zip(argv+optind) == 0) ? 0 : 1); + exit((compare_zip(argv + optind) == 0) ? 0 : 1); } static int -compare_zip(char * const zn[]) -{ +compare_zip(char *const zn[]) { struct archive a[2]; struct entry *e[2]; zip_uint64_t n[2]; int i; int res; - for (i=0; i<2; i++) { + for (i = 0; i < 2; i++) { a[i].name = zn[i]; a[i].entry = NULL; a[i].nentry = 0; a[i].za = NULL; a[i].comment = NULL; - a[i].comment_length =0; + a[i].comment_length = 0; if (is_directory(zn[i])) { #ifndef HAVE_FTS_H fprintf(stderr, "%s: reading directories not supported\n", prg); exit(2); #else - if (list_directory(zn[i], a+i) < 0) + if (list_directory(zn[i], a + i) < 0) exit(2); paranoid = 0; /* paranoid checks make no sense for directories, since they compare zip metadata */ #endif } else { - if (list_zip(zn[i], a+i) < 0) + if (list_zip(zn[i], a + i) < 0) exit(2); } if (a[i].nentry > 0) @@ -247,7 +240,7 @@ compare_zip(char * const zn[]) } } - for (i=0; i<2; i++) + for (i = 0; i < 2; i++) if (a[i].za) zip_close(a[i].za); @@ -265,20 +258,19 @@ compare_zip(char * const zn[]) #ifdef HAVE_FTS_H static zip_int64_t -compute_crc(const char *fname) -{ +compute_crc(const char *fname) { FILE *f; uLong crc = crc32(0L, Z_NULL, 0); size_t n; Bytef buffer[8192]; - if ((f=fopen(fname, "r")) == NULL) { + if ((f = fopen(fname, "r")) == NULL) { fprintf(stderr, "%s: can't open %s: %s\n", prg, fname, strerror(errno)); return -1; } - while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) { + while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) { crc = crc32(crc, buffer, (unsigned int)n); } @@ -296,8 +288,7 @@ compute_crc(const char *fname) static int -is_directory(const char *name) -{ +is_directory(const char *name) { struct stat st; if (stat(name, &st) < 0) @@ -309,21 +300,20 @@ is_directory(const char *name) #ifdef HAVE_FTS_H static int -list_directory(const char *name, struct archive *a) -{ +list_directory(const char *name, struct archive *a) { FTS *fts; FTSENT *ent; zip_uint64_t nalloc; size_t prefix_length; - char * const names[2] = { (char *)name, NULL }; + char *const names[2] = {(char *)name, NULL}; - if ((fts = fts_open(names, FTS_NOCHDIR|FTS_LOGICAL, NULL)) == NULL) { + if ((fts = fts_open(names, FTS_NOCHDIR | FTS_LOGICAL, NULL)) == NULL) { fprintf(stderr, "%s: can't open directory '%s': %s\n", prg, name, strerror(errno)); return -1; } - prefix_length = strlen(name)+1; + prefix_length = strlen(name) + 1; nalloc = 0; @@ -351,24 +341,24 @@ list_directory(const char *name, struct archive *a) case FTS_F: if (a->nentry >= nalloc) { nalloc += 16; - if (nalloc > SIZE_MAX/sizeof(a->entry[0])) { + if (nalloc > SIZE_MAX / sizeof(a->entry[0])) { fprintf(stderr, "%s: malloc failure\n", prg); exit(1); } - a->entry = realloc(a->entry, sizeof(a->entry[0])*nalloc); + a->entry = realloc(a->entry, sizeof(a->entry[0]) * nalloc); if (a->entry == NULL) { fprintf(stderr, "%s: malloc failure\n", prg); exit(1); } } - - a->entry[a->nentry].name = strdup(ent->fts_path+prefix_length); + + a->entry[a->nentry].name = strdup(ent->fts_path + prefix_length); a->entry[a->nentry].size = (zip_uint64_t)ent->fts_statp->st_size; if ((crc = compute_crc(ent->fts_accpath)) < 0) { fts_close(fts); return -1; } - + a->entry[a->nentry].crc = (zip_uint32_t)crc; a->nentry++; break; @@ -386,14 +376,13 @@ list_directory(const char *name, struct archive *a) static int -list_zip(const char *name, struct archive *a) -{ +list_zip(const char *name, struct archive *a) { zip_t *za; int err; struct zip_stat st; unsigned int i; - if ((za=zip_open(name, paranoid ? ZIP_CHECKCONS : 0, &err)) == NULL) { + if ((za = zip_open(name, paranoid ? ZIP_CHECKCONS : 0, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "%s: cannot open zip archive '%s': %s\n", prg, name, zip_error_strerror(&error)); @@ -403,16 +392,16 @@ list_zip(const char *name, struct archive *a) a->za = za; a->nentry = (zip_uint64_t)zip_get_num_entries(za, 0); - + if (a->nentry == 0) a->entry = NULL; else { - if ((a->nentry > SIZE_MAX/sizeof(a->entry[0])) || (a->entry=(struct entry *)malloc(sizeof(a->entry[0]) * a->nentry)) == NULL) { + if ((a->nentry > SIZE_MAX / sizeof(a->entry[0])) || (a->entry = (struct entry *)malloc(sizeof(a->entry[0]) * a->nentry)) == NULL) { fprintf(stderr, "%s: malloc failure\n", prg); exit(1); } - for (i=0; inentry; i++) { + for (i = 0; i < a->nentry; i++) { zip_stat_index(za, i, 0, &st); a->entry[i].name = strdup(st.name); a->entry[i].size = st.size; @@ -421,24 +410,24 @@ list_zip(const char *name, struct archive *a) test_file(za, i, st.size, st.crc); if (paranoid) { a->entry[i].comp_method = st.comp_method; - ef_read(za, i, a->entry+i); + ef_read(za, i, a->entry + i); a->entry[i].comment = zip_file_get_comment(za, i, &a->entry[i].comment_length, 0); } else { a->entry[i].comp_method = 0; a->entry[i].n_extra_fields = 0; } - } + } - if (paranoid) { - int length; + if (paranoid) { + int length; a->comment = zip_get_archive_comment(za, &length, 0); - a->comment_length = (size_t)length; - } - else { - a->comment = NULL; - a->comment_length = 0; - } + a->comment_length = (size_t)length; + } + else { + a->comment = NULL; + a->comment_length = 0; + } } return 0; @@ -454,39 +443,35 @@ comment_compare(const char *c1, size_t l1, const char *c2, size_t l2) { return 0; if (c1 == NULL || c2 == NULL) - return c1 == c2; - + return c1 == c2; + return memcmp(c1, c2, (size_t)l2); } static int -compare_list(char * const name[2], - const void *l[2], const zip_uint64_t n[2], int size, - int (*cmp)(const void *, const void *), - int (*check)(char *const name[2], const void *, const void *), - void (*print)(const void *)) -{ +compare_list(char *const name[2], const void *l[2], const zip_uint64_t n[2], int size, int (*cmp)(const void *, const void *), int (*check)(char *const name[2], const void *, const void *), void (*print)(const void *)) { unsigned int i[2]; int j, c; int diff; -#define INC(k) (i[k]++, l[k]=((const char *)l[k])+size) -#define PRINT(k) do { \ - if (header_done==0 && verbose) { \ - printf("--- %s\n+++ %s\n", name[0], name[1]); \ - header_done = 1; \ - } \ - if (verbose) { \ - printf("%c ", (k)?'+':'-'); \ - print(l[k]); \ - } \ - diff = 1; \ - } while (0) +#define INC(k) (i[k]++, l[k] = ((const char *)l[k]) + size) +#define PRINT(k) \ + do { \ + if (header_done == 0 && verbose) { \ + printf("--- %s\n+++ %s\n", name[0], name[1]); \ + header_done = 1; \ + } \ + if (verbose) { \ + printf("%c ", (k) ? '+' : '-'); \ + print(l[k]); \ + } \ + diff = 1; \ + } while (0) i[0] = i[1] = 0; diff = 0; - while (i[0]n_extra_fields = (zip_uint16_t)(n_local + n_central); - - if ((e->extra_fields=(struct ef *)malloc(sizeof(e->extra_fields[0])*e->n_extra_fields)) == NULL) + + if ((e->extra_fields = (struct ef *)malloc(sizeof(e->extra_fields[0]) * e->n_extra_fields)) == NULL) return -1; - for (i=0; iextra_fields[i].name = e->name; e->extra_fields[i].data = zip_file_extra_field_get(za, idx, i, &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_LOCAL); if (e->extra_fields[i].data == NULL) return -1; e->extra_fields[i].flags = ZIP_FL_LOCAL; } - for (; in_extra_fields; i++) { + for (; i < e->n_extra_fields; i++) { e->extra_fields[i].name = e->name; - e->extra_fields[i].data=zip_file_extra_field_get(za, idx, (zip_uint16_t)(i-n_local), &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_CENTRAL); + e->extra_fields[i].data = zip_file_extra_field_get(za, idx, (zip_uint16_t)(i - n_local), &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_CENTRAL); if (e->extra_fields[i].data == NULL) return -1; e->extra_fields[i].flags = ZIP_FL_CENTRAL; @@ -554,8 +537,7 @@ ef_read(zip_t *za, zip_uint64_t idx, struct entry *e) static int -ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2) -{ +ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2) { struct ef *ef[2]; zip_uint64_t n[2]; @@ -568,7 +550,6 @@ ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2) } - static int ef_order(const void *ap, const void *bp) { const struct ef *a, *b; @@ -587,35 +568,33 @@ ef_order(const void *ap, const void *bp) { static void -ef_print(const void *p) -{ +ef_print(const void *p) { const struct ef *ef = (struct ef *)p; int i; printf(" %s ", ef->name); printf("%04x %c <", ef->id, ef->flags == ZIP_FL_LOCAL ? 'l' : 'c'); - for (i=0; isize; i++) + for (i = 0; i < ef->size; i++) printf("%s%02x", i ? " " : "", ef->data[i]); printf(">\n"); } static int -entry_cmp(const void *p1, const void *p2) -{ +entry_cmp(const void *p1, const void *p2) { const struct entry *e1, *e2; int c; e1 = (struct entry *)p1; e2 = (struct entry *)p2; - if ((c=(ignore_case ? strcasecmp : strcmp)(e1->name, e2->name)) != 0) + if ((c = (ignore_case ? strcasecmp : strcmp)(e1->name, e2->name)) != 0) return c; if (e1->size != e2->size) { - if (e1->size > e2->size) - return 1; - else - return -1; + if (e1->size > e2->size) + return 1; + else + return -1; } if (e1->crc != e2->crc) return (int)e1->crc - (int)e2->crc; @@ -639,7 +618,7 @@ entry_paranoia_checks(char *const name[2], const void *p1, const void *p2) { if (e1->comp_method != e2->comp_method) { if (verbose) { - if (header_done==0) { + if (header_done == 0) { printf("--- %s\n+++ %s\n", name[0], name[1]); header_done = 1; } @@ -648,11 +627,11 @@ entry_paranoia_checks(char *const name[2], const void *p1, const void *p2) { printf("+++ %s ", e1->name); printf("method %u\n", e2->comp_method); } - ret = 1; + ret = 1; } if (comment_compare(e1->comment, e1->comment_length, e2->comment, e2->comment_length) != 0) { if (verbose) { - if (header_done==0) { + if (header_done == 0) { printf("--- %s\n+++ %s\n", name[0], name[1]); header_done = 1; } @@ -668,10 +647,8 @@ entry_paranoia_checks(char *const name[2], const void *p1, const void *p2) { } - static void -entry_print(const void *p) -{ +entry_print(const void *p) { const struct entry *e; e = (struct entry *)p; @@ -682,29 +659,28 @@ entry_print(const void *p) static int -test_file(zip_t *za, zip_uint64_t idx, zip_uint64_t size, zip_uint32_t crc) -{ +test_file(zip_t *za, zip_uint64_t idx, zip_uint64_t size, zip_uint32_t crc) { zip_file_t *zf; char buf[8192]; zip_uint64_t nsize; zip_int64_t n; zip_uint32_t ncrc; - - if ((zf=zip_fopen_index(za, idx, 0)) == NULL) { + + if ((zf = zip_fopen_index(za, idx, 0)) == NULL) { fprintf(stderr, "%s: cannot open file %" PRIu64 " in archive: %s\n", prg, idx, zip_strerror(za)); return -1; } ncrc = (zip_uint32_t)crc32(0, NULL, 0); nsize = 0; - - while ((n=zip_fread(zf, buf, sizeof(buf))) > 0) { + + while ((n = zip_fread(zf, buf, sizeof(buf))) > 0) { nsize += (zip_uint64_t)n; ncrc = (zip_uint32_t)crc32(ncrc, (const Bytef *)buf, (unsigned int)n); } if (n < 0) { - fprintf(stderr, "%s: error reading file %" PRIu64 " in archive: %s\n", prg, idx, zip_file_strerror(zf)); + fprintf(stderr, "%s: error reading file %" PRIu64 " in archive: %s\n", prg, idx, zip_file_strerror(zf)); zip_fclose(zf); return -1; } diff --git a/src/zipmerge.c b/src/zipmerge.c index 181f947..d080cae 100644 --- a/src/zipmerge.c +++ b/src/zipmerge.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -53,12 +53,11 @@ char *prg; -#define PROGRAM "zipmerge" +#define PROGRAM "zipmerge" #define USAGE "usage: %s [-DhIiSsV] target-zip zip...\n" -char help_head[] = - PROGRAM " (" PACKAGE ") by Dieter Baron and Thomas Klausner\n\n"; +char help_head[] = PROGRAM " (" PACKAGE ") by Dieter Baron and Thomas Klausner\n\n"; char help[] = "\n\ -h display this help message\n\ @@ -77,22 +76,20 @@ Copyright (C) 2004-2017 Dieter Baron and Thomas Klausner\n\ #define OPTIONS "hVDiIsS" -#define CONFIRM_ALL_YES 0x001 -#define CONFIRM_ALL_NO 0x002 -#define CONFIRM_SAME_YES 0x010 -#define CONFIRM_SAME_NO 0x020 +#define CONFIRM_ALL_YES 0x001 +#define CONFIRM_ALL_NO 0x002 +#define CONFIRM_SAME_YES 0x010 +#define CONFIRM_SAME_NO 0x020 int confirm; zip_flags_t name_flags; -static int confirm_replace(zip_t *, const char *, zip_uint64_t, - zip_t *, const char *, zip_uint64_t); +static int confirm_replace(zip_t *, const char *, zip_uint64_t, zip_t *, const char *, zip_uint64_t); static zip_t *merge_zip(zip_t *, const char *, const char *); int -main(int argc, char *argv[]) -{ +main(int argc, char *argv[]) { zip_t *za; zip_t **zs; int c, err; @@ -104,7 +101,7 @@ main(int argc, char *argv[]) confirm = CONFIRM_ALL_YES; name_flags = 0; - while ((c=getopt(argc, argv, OPTIONS)) != -1) { + while ((c = getopt(argc, argv, OPTIONS)) != -1) { switch (c) { case 'D': name_flags |= ZIP_FL_NODIR; @@ -139,7 +136,7 @@ main(int argc, char *argv[]) } } - if (argc < optind+2) { + if (argc < optind + 2) { fprintf(stderr, USAGE, prg); exit(2); } @@ -147,13 +144,13 @@ main(int argc, char *argv[]) tname = argv[optind++]; argv += optind; - n = (unsigned int)(argc-optind); - if ((zs=(zip_t **)malloc(sizeof(zs[0])*n)) == NULL) { + n = (unsigned int)(argc - optind); + if ((zs = (zip_t **)malloc(sizeof(zs[0]) * n)) == NULL) { fprintf(stderr, "%s: out of memory\n", prg); exit(1); } - if ((za=zip_open(tname, ZIP_CREATE, &err)) == NULL) { + if ((za = zip_open(tname, ZIP_CREATE, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, tname, zip_error_strerror(&error)); @@ -161,18 +158,17 @@ main(int argc, char *argv[]) exit(1); } - for (i=0; i= 0) { + if ((idx = zip_name_locate(za, fname, name_flags)) >= 0) { switch (confirm_replace(za, tname, (zip_uint64_t)idx, zs, sname, i)) { case 0: break; - + case 1: - if ((source=zip_source_zip(za, zs, i, 0, 0, 0)) == NULL - || zip_replace(za, (zip_uint64_t)idx, source) < 0) { + if ((source = zip_source_zip(za, zs, i, 0, 0, 0)) == NULL || zip_replace(za, (zip_uint64_t)idx, source) < 0) { zip_source_free(source); - fprintf(stderr, - "%s: cannot replace '%s' in `%s': %s\n", - prg, fname, tname, zip_strerror(za)); - zip_close(zs); + fprintf(stderr, "%s: cannot replace '%s' in `%s': %s\n", prg, fname, tname, zip_strerror(za)); + zip_close(zs); return NULL; } break; @@ -274,9 +260,10 @@ merge_zip(zip_t *za, const char *tname, const char *sname) case -1: zip_close(zs); return NULL; - + default: - fprintf(stderr, "%s: internal error: " + fprintf(stderr, + "%s: internal error: " "unexpected return code from confirm (%d)\n", prg, err); zip_close(zs); @@ -284,12 +271,9 @@ merge_zip(zip_t *za, const char *tname, const char *sname) } } else { - if ((source=zip_source_zip(za, zs, i, 0, 0, 0)) == NULL - || zip_add(za, fname, source) < 0) { + if ((source = zip_source_zip(za, zs, i, 0, 0, 0)) == NULL || zip_add(za, fname, source) < 0) { zip_source_free(source); - fprintf(stderr, - "%s: cannot add '%s' to `%s': %s\n", - prg, fname, tname, zip_strerror(za)); + fprintf(stderr, "%s: cannot add '%s' to `%s': %s\n", prg, fname, tname, zip_strerror(za)); zip_close(zs); return NULL; } diff --git a/src/ziptool.c b/src/ziptool.c index ba34110..1a37e77 100644 --- a/src/ziptool.c +++ b/src/ziptool.c @@ -83,7 +83,7 @@ static int add(int argc, char *argv[]) { zip_source_t *zs; - if ((zs=zip_source_buffer(za, argv[1], strlen(argv[1]), 0)) == NULL) { + if ((zs = zip_source_buffer(za, argv[1], strlen(argv[1]), 0)) == NULL) { fprintf(stderr, "can't create zip_source from buffer: %s\n", zip_strerror(za)); return -1; } @@ -113,12 +113,13 @@ add_file(int argc, char *argv[]) { zip_int64_t len = strtoll(argv[3], NULL, 10); if (strcmp(argv[1], "/dev/stdin") == 0) { - if ((zs=zip_source_filep(za, stdin, start, len)) == NULL) { + if ((zs = zip_source_filep(za, stdin, start, len)) == NULL) { fprintf(stderr, "can't create zip_source from stdin: %s\n", zip_strerror(za)); return -1; } - } else { - if ((zs=zip_source_file(za, argv[1], start, len)) == NULL) { + } + else { + if ((zs = zip_source_file(za, argv[1], start, len)) == NULL) { fprintf(stderr, "can't create zip_source from file: %s\n", zip_strerror(za)); return -1; } @@ -142,14 +143,14 @@ add_from_zip(int argc, char *argv[]) { idx = strtoull(argv[2], NULL, 10); start = strtoull(argv[3], NULL, 10); len = strtoll(argv[4], NULL, 10); - if ((z_in[z_in_count]=zip_open(argv[1], ZIP_CHECKCONS, &err)) == NULL) { + if ((z_in[z_in_count] = zip_open(argv[1], ZIP_CHECKCONS, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "can't open zip archive '%s': %s\n", argv[1], zip_error_strerror(&error)); zip_error_fini(&error); return -1; } - if ((zs=zip_source_zip(za, z_in[z_in_count], idx, 0, start, len)) == NULL) { + if ((zs = zip_source_zip(za, z_in[z_in_count], idx, 0, start, len)) == NULL) { fprintf(stderr, "error creating file source from '%s' index '%" PRIu64 "': %s\n", argv[1], idx, zip_strerror(za)); zip_close(z_in[z_in_count]); return -1; @@ -178,11 +179,11 @@ cat(int argc, char *argv[]) { /* Need to set stdout to binary mode for Windows */ setmode(fileno(stdout), _O_BINARY); #endif - if ((zf=zip_fopen_index(za, idx, 0)) == NULL) { + if ((zf = zip_fopen_index(za, idx, 0)) == NULL) { fprintf(stderr, "can't open file at index '%" PRIu64 "': %s\n", idx, zip_strerror(za)); return -1; } - while ((n=zip_fread(zf, buf, sizeof(buf))) > 0) { + while ((n = zip_fread(zf, buf, sizeof(buf))) > 0) { if (fwrite(buf, (size_t)n, 1, stdout) != 1) { zip_fclose(zf); fprintf(stderr, "can't write file contents to stdout: %s\n", strerror(errno)); @@ -212,10 +213,11 @@ count_extra(int argc, char *argv[]) { zip_flags_t ceflags = 0; idx = strtoull(argv[0], NULL, 10); ceflags = get_flags(argv[1]); - if ((count=zip_file_extra_fields_count(za, idx, ceflags)) < 0) { + if ((count = zip_file_extra_fields_count(za, idx, ceflags)) < 0) { fprintf(stderr, "can't get extra field count for file at index '%" PRIu64 "': %s\n", idx, zip_strerror(za)); return -1; - } else { + } + else { printf("Extra field count: %d\n", count); } return 0; @@ -230,17 +232,17 @@ count_extra_by_id(int argc, char *argv[]) { idx = strtoull(argv[0], NULL, 10); eid = (zip_uint16_t)strtoull(argv[1], NULL, 10); ceflags = get_flags(argv[2]); - if ((count=zip_file_extra_fields_count_by_id(za, idx, eid, ceflags)) < 0) { + if ((count = zip_file_extra_fields_count_by_id(za, idx, eid, ceflags)) < 0) { fprintf(stderr, "can't get extra field count for file at index '%" PRIu64 "' and for id '%d': %s\n", idx, eid, zip_strerror(za)); return -1; - } else { + } + else { printf("Extra field count: %d\n", count); } return 0; } -static int -delete(int argc, char *argv[]) { +static int delete (int argc, char *argv[]) { zip_uint64_t idx; idx = strtoull(argv[0], NULL, 10); if (zip_delete(za, idx) < 0) { @@ -286,7 +288,7 @@ get_archive_comment(int argc, char *argv[]) { const char *comment; int len; /* get archive comment */ - if ((comment=zip_get_archive_comment(za, &len, 0)) == NULL) + if ((comment = zip_get_archive_comment(za, &len, 0)) == NULL) printf("No archive comment\n"); else printf("Archive comment: %.*s\n", len, comment); @@ -303,7 +305,7 @@ get_extra(int argc, char *argv[]) { idx = strtoull(argv[0], NULL, 10); eidx = (zip_uint16_t)strtoull(argv[1], NULL, 10); geflags = get_flags(argv[2]); - if ((efdata=zip_file_extra_field_get(za, idx, eidx, &id, &eflen, geflags)) == NULL) { + if ((efdata = zip_file_extra_field_get(za, idx, eidx, &id, &eflen, geflags)) == NULL) { fprintf(stderr, "can't get extra field data for file at index %" PRIu64 ", extra field %d, flags %u: %s\n", idx, eidx, geflags, zip_strerror(za)); return -1; } @@ -326,7 +328,7 @@ get_extra_by_id(int argc, char *argv[]) { eid = (zip_uint16_t)strtoull(argv[1], NULL, 10); eidx = (zip_uint16_t)strtoull(argv[2], NULL, 10); geflags = get_flags(argv[3]); - if ((efdata=zip_file_extra_field_get_by_id(za, idx, eid, eidx, &eflen, geflags)) == NULL) { + if ((efdata = zip_file_extra_field_get_by_id(za, idx, eid, eidx, &eflen, geflags)) == NULL) { fprintf(stderr, "can't get extra field data for file at index %" PRIu64 ", extra field id %d, ef index %d, flags %u: %s\n", idx, eid, eidx, geflags, zip_strerror(za)); return -1; } @@ -346,10 +348,11 @@ get_file_comment(int argc, char *argv[]) { zip_uint64_t idx; /* get file comment */ idx = strtoull(argv[0], NULL, 10); - if ((comment=zip_get_file_comment(za, idx, &len, 0)) == NULL) { + if ((comment = zip_get_file_comment(za, idx, &len, 0)) == NULL) { fprintf(stderr, "can't get comment for '%s': %s\n", zip_get_name(za, idx, 0), zip_strerror(za)); return -1; - } else if (len == 0) + } + else if (len == 0) printf("No comment for '%s'\n", zip_get_name(za, idx, 0)); else printf("File comment for '%s': %.*s\n", zip_get_name(za, idx, 0), len, comment); @@ -373,9 +376,10 @@ name_locate(int argc, char *argv[]) { zip_int64_t idx; flags = get_flags(argv[1]); - if ((idx=zip_name_locate(za, argv[0], flags)) < 0) { + if ((idx = zip_name_locate(za, argv[0], flags)) < 0) { fprintf(stderr, "can't find entry with name '%s' using flags '%s'\n", argv[0], argv[1]); - } else { + } + else { printf("name '%s' using flags '%s' found at index %" PRId64 "\n", argv[0], argv[1], idx); } @@ -384,7 +388,7 @@ name_locate(int argc, char *argv[]) { static void progress_callback(zip_t *archive, double percentage, void *ud) { - printf("%.1lf%% done\n", percentage*100); + printf("%.1lf%% done\n", percentage * 100); } static int @@ -412,8 +416,7 @@ replace_file_contents(int argc, char *argv[]) { zip_uint64_t idx; idx = strtoull(argv[0], NULL, 10); content = argv[1]; - if ((s=zip_source_buffer(za, content, strlen(content), 0)) == NULL || - zip_file_replace(za, idx, s, 0) < 0) { + if ((s = zip_source_buffer(za, content, strlen(content), 0)) == NULL || zip_file_replace(za, idx, s, 0) < 0) { zip_source_free(s); fprintf(stderr, "error replacing file data: %s\n", zip_strerror(za)); return -1; @@ -515,8 +518,8 @@ set_file_mtime_all(int argc, char *argv[]) { mtime = (time_t)strtoull(argv[0], NULL, 10); if ((num_entries = zip_get_num_entries(za, 0)) < 0) { - fprintf(stderr, "can't get number of entries: %s\n", zip_strerror(za)); - return -1; + fprintf(stderr, "can't get number of entries: %s\n", zip_strerror(za)); + return -1; } for (idx = 0; idx < (zip_uint64_t)num_entries; idx++) { if (zip_file_set_mtime(za, idx, mtime, 0) < 0) { @@ -552,11 +555,11 @@ zstat(int argc, char *argv[]) { if (sb.valid & ZIP_STAT_NAME) printf("name: '%s'\n", sb.name); if (sb.valid & ZIP_STAT_INDEX) - printf("index: '%"PRIu64"'\n", sb.index); + printf("index: '%" PRIu64 "'\n", sb.index); if (sb.valid & ZIP_STAT_SIZE) - printf("size: '%"PRIu64"'\n", sb.size); + printf("size: '%" PRIu64 "'\n", sb.size); if (sb.valid & ZIP_STAT_COMP_SIZE) - printf("compressed size: '%"PRIu64"'\n", sb.comp_size); + printf("compressed size: '%" PRIu64 "'\n", sb.comp_size); if (sb.valid & ZIP_STAT_MTIME) { struct tm *tpm; tpm = localtime(&sb.mtime); @@ -577,8 +580,7 @@ zstat(int argc, char *argv[]) { } static zip_flags_t -get_flags(const char *arg) -{ +get_flags(const char *arg) { zip_flags_t flags = 0; if (strchr(arg, 'C') != NULL) flags |= ZIP_FL_NOCASE; @@ -594,42 +596,39 @@ get_flags(const char *arg) } static zip_int32_t -get_compression_method(const char *arg) -{ +get_compression_method(const char *arg) { if (strcmp(arg, "default") == 0) - return ZIP_CM_DEFAULT; + return ZIP_CM_DEFAULT; else if (strcmp(arg, "store") == 0) - return ZIP_CM_STORE; + return ZIP_CM_STORE; else if (strcmp(arg, "deflate") == 0) - return ZIP_CM_DEFLATE; + return ZIP_CM_DEFLATE; #if defined(HAVE_LIBBZ2) else if (strcmp(arg, "bzip2") == 0) - return ZIP_CM_BZIP2; + return ZIP_CM_BZIP2; #endif else if (strcmp(arg, "unknown") == 0) - return 100; + return 100; return 0; /* TODO: error handling */ } static zip_uint16_t -get_encryption_method(const char *arg) -{ +get_encryption_method(const char *arg) { if (strcmp(arg, "none") == 0) - return ZIP_EM_NONE; + return ZIP_EM_NONE; else if (strcmp(arg, "AES-128") == 0) - return ZIP_EM_AES_128; + return ZIP_EM_AES_128; else if (strcmp(arg, "AES-192") == 0) - return ZIP_EM_AES_192; + return ZIP_EM_AES_192; else if (strcmp(arg, "AES-256") == 0) - return ZIP_EM_AES_256; + return ZIP_EM_AES_256; else if (strcmp(arg, "unknown") == 0) - return 100; + return 100; return (zip_uint16_t)-1; /* TODO: error handling */ } static void -hexdump(const zip_uint8_t *data, zip_uint16_t len) -{ +hexdump(const zip_uint8_t *data, zip_uint16_t len) { zip_uint16_t i; if (len <= 0) @@ -637,7 +636,7 @@ hexdump(const zip_uint8_t *data, zip_uint16_t len) printf("0x"); - for (i=0; i ZIP_INT64_MAX) { - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; - } - if ((source = zip_source_file_create(archive, offset, (zip_int64_t)length, error)) == NULL - || (zaa = zip_open_from_source(source, flags, error)) == NULL) { + if (length > ZIP_INT64_MAX) { + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } + if ((source = zip_source_file_create(archive, offset, (zip_int64_t)length, error)) == NULL || (zaa = zip_open_from_source(source, flags, error)) == NULL) { zip_source_free(source); return NULL; } @@ -678,45 +675,44 @@ read_from_file(const char *archive, int flags, zip_error_t *error, zip_uint64_t return zaa; } -dispatch_table_t dispatch_table[] = { - { "add", 2, "name content", "add file called name using content", add }, - { "add_dir", 1, "name", "add directory", add_dir }, - { "add_file", 4, "name file_to_add offset len", "add file to archive, len bytes starting from offset", add_file }, - { "add_from_zip", 5, "name archivename index offset len", "add file from another archive, len bytes starting from offset", add_from_zip }, - { "cat", 1, "index", "output file contents to stdout", cat }, - { "count_extra", 2, "index flags", "show number of extra fields for archive entry", count_extra }, - { "count_extra_by_id", 3, "index extra_id flags", "show number of extra fields of type extra_id for archive entry", count_extra_by_id }, - { "delete", 1, "index", "remove entry", delete }, - { "delete_extra", 3, "index extra_idx flags", "remove extra field", delete_extra }, - { "delete_extra_by_id", 4, "index extra_id extra_index flags", "remove extra field of type extra_id", delete_extra_by_id }, - { "get_archive_comment", 0, "", "show archive comment", get_archive_comment }, - { "get_extra", 3, "index extra_index flags", "show extra field", get_extra }, - { "get_extra_by_id", 4, "index extra_id extra_index flags", "show extra field of type extra_id", get_extra_by_id }, - { "get_file_comment", 1, "index", "get file comment", get_file_comment }, - { "get_num_entries", 1, "flags", "get number of entries in archive", get_num_entries }, - { "name_locate", 2, "name flags", "find entry in archive", name_locate }, - { "print_progress", 0, "", "print progress during zip_close()", print_progress }, - { "rename", 2, "index name", "rename entry", zrename }, - { "replace_file_contents", 2, "index data", "replace entry with data", replace_file_contents }, - { "set_archive_comment", 1, "comment", "set archive comment", set_archive_comment }, - { "set_extra", 5, "index extra_id extra_index flags value", "set extra field", set_extra }, - { "set_file_comment", 2, "index comment", "set file comment", set_file_comment }, - { "set_file_compression", 3, "index method compression_flags", "set file compression method", set_file_compression }, - { "set_file_encryption", 3, "index method password", "set file encryption method", set_file_encryption }, - { "set_file_mtime", 2, "index timestamp", "set file modification time", set_file_mtime }, - { "set_file_mtime_all", 1, "timestamp", "set file modification time for all files", set_file_mtime_all }, - { "set_password", 1, "password", "set default password for encryption", set_password }, - { "stat", 1, "index", "print information about entry", zstat } +dispatch_table_t dispatch_table[] = {{"add", 2, "name content", "add file called name using content", add}, + {"add_dir", 1, "name", "add directory", add_dir}, + {"add_file", 4, "name file_to_add offset len", "add file to archive, len bytes starting from offset", add_file}, + {"add_from_zip", 5, "name archivename index offset len", "add file from another archive, len bytes starting from offset", add_from_zip}, + {"cat", 1, "index", "output file contents to stdout", cat}, + {"count_extra", 2, "index flags", "show number of extra fields for archive entry", count_extra}, + {"count_extra_by_id", 3, "index extra_id flags", "show number of extra fields of type extra_id for archive entry", count_extra_by_id}, + {"delete", 1, "index", "remove entry", delete}, + {"delete_extra", 3, "index extra_idx flags", "remove extra field", delete_extra}, + {"delete_extra_by_id", 4, "index extra_id extra_index flags", "remove extra field of type extra_id", delete_extra_by_id}, + {"get_archive_comment", 0, "", "show archive comment", get_archive_comment}, + {"get_extra", 3, "index extra_index flags", "show extra field", get_extra}, + {"get_extra_by_id", 4, "index extra_id extra_index flags", "show extra field of type extra_id", get_extra_by_id}, + {"get_file_comment", 1, "index", "get file comment", get_file_comment}, + {"get_num_entries", 1, "flags", "get number of entries in archive", get_num_entries}, + {"name_locate", 2, "name flags", "find entry in archive", name_locate}, + {"print_progress", 0, "", "print progress during zip_close()", print_progress}, + {"rename", 2, "index name", "rename entry", zrename}, + {"replace_file_contents", 2, "index data", "replace entry with data", replace_file_contents}, + {"set_archive_comment", 1, "comment", "set archive comment", set_archive_comment}, + {"set_extra", 5, "index extra_id extra_index flags value", "set extra field", set_extra}, + {"set_file_comment", 2, "index comment", "set file comment", set_file_comment}, + {"set_file_compression", 3, "index method compression_flags", "set file compression method", set_file_compression}, + {"set_file_encryption", 3, "index method password", "set file encryption method", set_file_encryption}, + {"set_file_mtime", 2, "index timestamp", "set file modification time", set_file_mtime}, + {"set_file_mtime_all", 1, "timestamp", "set file modification time for all files", set_file_mtime_all}, + {"set_password", 1, "password", "set default password for encryption", set_password}, + {"stat", 1, "index", "print information about entry", zstat} #ifdef DISPATCH_REGRESS - , DISPATCH_REGRESS + , + DISPATCH_REGRESS #endif }; static int -dispatch(int argc, char *argv[]) -{ +dispatch(int argc, char *argv[]) { unsigned int i; - for (i=0; i= argc-1) + if (optind >= argc - 1) usage(prg, "too few arguments"); arg = optind; @@ -886,10 +879,11 @@ main(int argc, char *argv[]) err = 0; while (arg < argc) { int ret; - ret = dispatch(argc-arg, argv+arg); + ret = dispatch(argc - arg, argv + arg); if (ret > 0) { arg += ret; - } else { + } + else { err = 1; break; } @@ -903,7 +897,7 @@ main(int argc, char *argv[]) err = 1; } - for (i=0; i