diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index c723fd40..279d5ebe 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -12,14 +12,9 @@ include_directories(${LIBRARY_INCLUDE_DIR}) file(GLOB SAMPLE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -find_package(ZLIB REQUIRED) - foreach(SAMPLE_SOURCE IN ITEMS ${SAMPLE_SOURCES}) get_filename_component(SAMPLE_NAME ${SAMPLE_SOURCE} NAME_WE) set(SAMPLE_EXECUTABLE benchmark-${SAMPLE_NAME}) add_executable(${SAMPLE_EXECUTABLE} ${SAMPLE_SOURCE}) - target_link_libraries(${SAMPLE_EXECUTABLE} PRIVATE xlnt PRIVATE ${ZLIB_LIBRARIES}) + target_link_libraries(${SAMPLE_EXECUTABLE} PRIVATE xlnt) endforeach() - -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data - DESTINATION ${CMAKE_BINARY_DIR}/bin) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 5bc7b462..cceae893 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -10,14 +10,9 @@ endif() file(GLOB SAMPLE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -find_package(ZLIB REQUIRED) - foreach(SAMPLE_SOURCE IN ITEMS ${SAMPLE_SOURCES}) get_filename_component(SAMPLE_NAME ${SAMPLE_SOURCE} NAME_WE) set(SAMPLE_EXECUTABLE sample-${SAMPLE_NAME}) add_executable(${SAMPLE_EXECUTABLE} ${SAMPLE_SOURCE}) - target_link_libraries(${SAMPLE_EXECUTABLE} PRIVATE xlnt PRIVATE ${ZLIB_LIBRARIES}) + target_link_libraries(${SAMPLE_EXECUTABLE} PRIVATE xlnt) endforeach() - -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data - DESTINATION ${CMAKE_BINARY_DIR}/bin) diff --git a/source/detail/crypto/base64.cpp b/source/detail/crypto/base64.cpp new file mode 100644 index 00000000..f31ea2d4 --- /dev/null +++ b/source/detail/crypto/base64.cpp @@ -0,0 +1,169 @@ +// Copyright (C) 2017 Thomas Fussell +// Copyright (C) 2013 Tomas Kislan +// Copyright (C) 2013 Adam Rudd +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +//THE SOFTWARE. + +#include + +namespace xlnt { +namespace detail { + +std::string encode_base64(const std::vector &input) +{ + auto encoded_length = (input.size() + 2 - ((input.size() + 2) % 3)) / 3 * 4; + auto output = std::string(encoded_length, '\0'); + auto input_iterator = input.begin(); + auto output_iterator = output.begin(); + + int i = 0, j = 0; + unsigned char a3[3]; + unsigned char a4[4]; + + const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + while (input_iterator != input.end()) + { + a3[i++] = *input_iterator++; + + if (i == 3) + { + a4[0] = (a3[0] & 0xfc) >> 2; + a4[1] = static_cast(((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4)); + a4[2] = static_cast(((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6)); + a4[3] = (a3[2] & 0x3f); + + for (i = 0; i < 4; i++) + { + *output_iterator++ = kBase64Alphabet[a4[i]]; + } + + i = 0; + } + } + + if (i) + { + for (j = i; j < 3; j++) + { + a3[j] = '\0'; + } + + a4[0] = (a3[0] & 0xfc) >> 2; + a4[1] = static_cast(((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4)); + a4[2] = static_cast(((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6)); + a4[3] = (a3[2] & 0x3f); + + for (j = 0; j < i + 1; j++) + { + *output_iterator++ = kBase64Alphabet[a4[j]]; + } + + while ((i++ < 3)) + { + *output_iterator++ = '='; + } + } + + return output; +} + +std::vector decode_base64(const std::string &input) +{ + std::size_t numEq = 0; + auto in_end = input.data() + input.size(); + while (*--in_end == '=') ++numEq; + auto decoded_length = ((6 * input.size()) / 8) - numEq; + auto output = std::vector(decoded_length); + + auto input_iterator = input.begin(); + auto output_iterator = output.begin(); + + int i = 0, j = 0; + std::uint8_t a3[3]; + std::uint8_t a4[4]; + + auto b64_lookup = [](std::uint8_t c) -> std::uint8_t + { + if(c >='A' && c <='Z') return c - 'A'; + if(c >='a' && c <='z') return c - 71; + if(c >='0' && c <='9') return c + 4; + if(c == '+') return 62; + if(c == '/') return 63; + return 255; + }; + + while (input_iterator != input.end()) + { + if (*input_iterator == '=') + { + break; + } + + a4[i++] = static_cast(*(input_iterator++)); + + if (i == 4) + { + for (i = 0; i <4; i++) + { + a4[i] = b64_lookup(a4[i]); + } + + a3[0] = static_cast(a4[0] << 2) + static_cast((a4[1] & 0x30) >> 4); + a3[1] = static_cast((a4[1] & 0xf) << 4) + static_cast((a4[2] & 0x3c) >> 2); + a3[2] = static_cast((a4[2] & 0x3) << 6) + static_cast(a4[3]); + + for (i = 0; i < 3; i++) + { + *output_iterator++ = a3[i]; + } + + i = 0; + } + } + + if (i) + { + for (j = i; j < 4; j++) + { + a4[j] = '\0'; + } + + for (j = 0; j < 4; j++) + { + a4[j] = b64_lookup(a4[j]); + } + + a3[0] = static_cast(a4[0] << 2) + static_cast((a4[1] & 0x30) >> 4); + a3[1] = static_cast((a4[1] & 0xf) << 4) + static_cast((a4[2] & 0x3c) >> 2); + a3[2] = static_cast((a4[2] & 0x3) << 6) + static_cast(a4[3]); + + for (j = 0; j < i - 1; j++) + { + *output_iterator++ = a3[j]; + } + } + + return output; +} + +} // namespace detail +} // namespace xlnt diff --git a/source/detail/crypto/base64.hpp b/source/detail/crypto/base64.hpp new file mode 100644 index 00000000..cbe5ac8c --- /dev/null +++ b/source/detail/crypto/base64.hpp @@ -0,0 +1,36 @@ +// Copyright (c) 2017 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#include +#include +#include + +namespace xlnt { +namespace detail { + +std::string encode_base64(const std::vector &input); + +std::vector decode_base64(const std::string &input); + +} // namespace detail +} // namespace xlnt diff --git a/source/detail/crypto/xlsx_crypto.cpp b/source/detail/crypto/xlsx_crypto.cpp index 58a568c3..da0fbedc 100644 --- a/source/detail/crypto/xlsx_crypto.cpp +++ b/source/detail/crypto/xlsx_crypto.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -134,148 +135,6 @@ auto read_int(std::size_t &index, const std::vector &raw_data) return result; } -/* -static std::string encode_base64(const std::vector &input) -{ - auto encoded_length = (input.size() + 2 - ((input.size() + 2) % 3)) / 3 * 4; - auto output = std::string(encoded_length, '\0'); - auto input_iterator = input.begin(); - auto output_iterator = output.begin(); - - int i = 0, j = 0; - unsigned char a3[3]; - unsigned char a4[4]; - - const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - - while (input_iterator != input.end()) - { - a3[i++] = *input_iterator++; - - if (i == 3) - { - a4[0] = (a3[0] & 0xfc) >> 2; - a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); - a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); - a4[3] = (a3[2] & 0x3f); - - for (i = 0; i < 4; i++) - { - *output_iterator++ = kBase64Alphabet[a4[i]]; - } - - i = 0; - } - } - - if (i) - { - for (j = i; j < 3; j++) - { - a3[j] = '\0'; - } - - a4[0] = (a3[0] & 0xfc) >> 2; - a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); - a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); - a4[3] = (a3[2] & 0x3f); - - for (j = 0; j < i + 1; j++) - { - *output_iterator++ = kBase64Alphabet[a4[j]]; - } - - while ((i++ < 3)) - { - *output_iterator++ = '='; - } - } - - return output; -} -*/ - -static std::vector decode_base64(const std::string &input) -{ - std::size_t numEq = 0; - auto in_end = input.data() + input.size(); - while (*--in_end == '=') ++numEq; - auto decoded_length = ((6 * input.size()) / 8) - numEq; - auto output = std::vector(decoded_length); - - auto input_iterator = input.begin(); - auto output_iterator = output.begin(); - - int i = 0, j = 0; - std::uint8_t a3[3]; - std::uint8_t a4[4]; - - auto b64_lookup = [](std::uint8_t c) -> std::uint8_t - { - if(c >='A' && c <='Z') return c - 'A'; - if(c >='a' && c <='z') return c - 71; - if(c >='0' && c <='9') return c + 4; - if(c == '+') return 62; - if(c == '/') return 63; - return 255; - }; - - while (input_iterator != input.end()) - { - if (*input_iterator == '=') - { - break; - } - - a4[i++] = static_cast(*(input_iterator++)); - - if (i == 4) - { - for (i = 0; i <4; i++) - { - a4[i] = b64_lookup(a4[i]); - } - - a3[0] = static_cast(a4[0] << 2) + static_cast((a4[1] & 0x30) >> 4); - a3[1] = static_cast((a4[1] & 0xf) << 4) + static_cast((a4[2] & 0x3c) >> 2); - a3[2] = static_cast((a4[2] & 0x3) << 6) + static_cast(a4[3]); - - for (i = 0; i < 3; i++) - { - *output_iterator++ = a3[i]; - } - - i = 0; - } - } - - if (i) - { - for (j = i; j < 4; j++) - { - a4[j] = '\0'; - } - - for (j = 0; j < 4; j++) - { - a4[j] = b64_lookup(a4[j]); - } - - a3[0] = static_cast(a4[0] << 2) + static_cast((a4[1] & 0x30) >> 4); - a3[1] = static_cast((a4[1] & 0xf) << 4) + static_cast((a4[2] & 0x3c) >> 2); - a3[2] = static_cast((a4[2] & 0x3) << 6) + static_cast(a4[3]); - - for (j = 0; j < i - 1; j++) - { - *output_iterator++ = a3[j]; - } - } - - return output; -} - std::vector hash(hash_algorithm algorithm, const std::vector &input) { if (algorithm == hash_algorithm::sha512) @@ -507,6 +366,8 @@ std::vector decrypt_xlsx_agile( const std::string &password, const std::vector &encrypted_package) { + using xlnt::detail::decode_base64; + static const auto &xmlns = xlnt::constants::ns("encryption"); static const auto &xmlns_p = xlnt::constants::ns("encryption-password"); // static const auto &xmlns_c = xlnt::constants::namespace_("encryption-certificate"); diff --git a/source/detail/miniz.cpp b/source/detail/miniz.cpp index e25829ea..d79f1d8d 100755 --- a/source/detail/miniz.cpp +++ b/source/detail/miniz.cpp @@ -3170,7 +3170,7 @@ struct mz_zip_internal_state_tag #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size #if defined(DEBUG) || defined(_DEBUG) || defined(NDEBUG) -static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index) +static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array */*pArray*/, mz_uint index) { MZ_ASSERT(index < pArray->m_size); return index;