mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
fix some stuff
This commit is contained in:
parent
c902af7e17
commit
9fa9343d89
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
169
source/detail/crypto/base64.cpp
Normal file
169
source/detail/crypto/base64.cpp
Normal file
|
@ -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 <detail/crypto/base64.hpp>
|
||||
|
||||
namespace xlnt {
|
||||
namespace detail {
|
||||
|
||||
std::string encode_base64(const std::vector<std::uint8_t> &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<std::uint8_t>(((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4));
|
||||
a4[2] = static_cast<std::uint8_t>(((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<std::uint8_t>(((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4));
|
||||
a4[2] = static_cast<std::uint8_t>(((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<std::uint8_t> 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<std::uint8_t>(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<std::uint8_t>(*(input_iterator++));
|
||||
|
||||
if (i == 4)
|
||||
{
|
||||
for (i = 0; i <4; i++)
|
||||
{
|
||||
a4[i] = b64_lookup(a4[i]);
|
||||
}
|
||||
|
||||
a3[0] = static_cast<std::uint8_t>(a4[0] << 2) + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
||||
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4) + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6) + static_cast<std::uint8_t>(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<std::uint8_t>(a4[0] << 2) + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
||||
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4) + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6) + static_cast<std::uint8_t>(a4[3]);
|
||||
|
||||
for (j = 0; j < i - 1; j++)
|
||||
{
|
||||
*output_iterator++ = a3[j];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace xlnt
|
36
source/detail/crypto/base64.hpp
Normal file
36
source/detail/crypto/base64.hpp
Normal file
|
@ -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 <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace xlnt {
|
||||
namespace detail {
|
||||
|
||||
std::string encode_base64(const std::vector<std::uint8_t> &input);
|
||||
|
||||
std::vector<std::uint8_t> decode_base64(const std::string &input);
|
||||
|
||||
} // namespace detail
|
||||
} // namespace xlnt
|
|
@ -24,6 +24,7 @@
|
|||
#include <array>
|
||||
|
||||
#include <detail/crypto/aes.hpp>
|
||||
#include <detail/crypto/base64.hpp>
|
||||
#include <detail/crypto/sha.hpp>
|
||||
#include <detail/crypto/xlsx_crypto.hpp>
|
||||
#include <detail/pole.hpp>
|
||||
|
@ -134,148 +135,6 @@ auto read_int(std::size_t &index, const std::vector<std::uint8_t> &raw_data)
|
|||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
static std::string encode_base64(const std::vector<std::uint8_t> &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<std::uint8_t> 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<std::uint8_t>(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<std::uint8_t>(*(input_iterator++));
|
||||
|
||||
if (i == 4)
|
||||
{
|
||||
for (i = 0; i <4; i++)
|
||||
{
|
||||
a4[i] = b64_lookup(a4[i]);
|
||||
}
|
||||
|
||||
a3[0] = static_cast<std::uint8_t>(a4[0] << 2) + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
||||
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4) + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6) + static_cast<std::uint8_t>(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<std::uint8_t>(a4[0] << 2) + static_cast<std::uint8_t>((a4[1] & 0x30) >> 4);
|
||||
a3[1] = static_cast<std::uint8_t>((a4[1] & 0xf) << 4) + static_cast<std::uint8_t>((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = static_cast<std::uint8_t>((a4[2] & 0x3) << 6) + static_cast<std::uint8_t>(a4[3]);
|
||||
|
||||
for (j = 0; j < i - 1; j++)
|
||||
{
|
||||
*output_iterator++ = a3[j];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> hash(hash_algorithm algorithm, const std::vector<std::uint8_t> &input)
|
||||
{
|
||||
if (algorithm == hash_algorithm::sha512)
|
||||
|
@ -507,6 +366,8 @@ std::vector<std::uint8_t> decrypt_xlsx_agile(
|
|||
const std::string &password,
|
||||
const std::vector<std::uint8_t> &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");
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user