mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
fix zip header error when unzipping for #103
This commit is contained in:
parent
398bf7a3bb
commit
7de0b05480
|
@ -2,27 +2,13 @@
|
|||
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#include <xlnt/xlnt.hpp>
|
||||
|
||||
#include <numeric>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
|
||||
int random(int min, int max)
|
||||
{
|
||||
static std::random_device device{};
|
||||
static std::default_random_engine engine{ device() };
|
||||
std::uniform_int_distribution<int> distribution{ min, max };
|
||||
return distribution(engine);
|
||||
}
|
||||
|
||||
class test_cell : public CxxTest::TestSuite
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -132,12 +132,12 @@ void write_header(const xlnt::detail::zip_file_header &header, std::ostream &ost
|
|||
{
|
||||
if (global)
|
||||
{
|
||||
write_int(ostream, static_cast<unsigned int>(0x02014b50)); // header sig
|
||||
write_int(ostream, static_cast<unsigned short>(0)); // version made by
|
||||
write_int(ostream, static_cast<std::uint32_t>(0x02014b50)); // header sig
|
||||
write_int(ostream, static_cast<std::uint16_t>(20)); // version made by
|
||||
}
|
||||
else
|
||||
{
|
||||
write_int(ostream, static_cast<unsigned int>(0x04034b50));
|
||||
write_int(ostream, static_cast<std::uint32_t>(0x04034b50));
|
||||
}
|
||||
|
||||
write_int(ostream, header.version);
|
||||
|
@ -148,16 +148,16 @@ void write_header(const xlnt::detail::zip_file_header &header, std::ostream &ost
|
|||
write_int(ostream, header.crc);
|
||||
write_int(ostream, header.compressed_size);
|
||||
write_int(ostream, header.uncompressed_size);
|
||||
write_int(ostream, static_cast<unsigned short>(header.filename.length()));
|
||||
write_int(ostream, static_cast<unsigned short>(0)); // extra lengthx
|
||||
write_int(ostream, static_cast<std::uint16_t>(header.filename.length()));
|
||||
write_int(ostream, static_cast<std::uint16_t>(0)); // extra lengthx
|
||||
|
||||
if (global)
|
||||
{
|
||||
write_int(ostream, static_cast<unsigned short>(0)); // filecomment
|
||||
write_int(ostream, static_cast<unsigned short>(0)); // disk# start
|
||||
write_int(ostream, static_cast<unsigned short>(0)); // internal file
|
||||
write_int(ostream, static_cast<unsigned int>(0)); // ext final
|
||||
write_int(ostream, static_cast<unsigned int>(header.header_offset)); // rel offset
|
||||
write_int(ostream, static_cast<std::uint16_t>(0)); // filecomment
|
||||
write_int(ostream, static_cast<std::uint16_t>(0)); // disk# start
|
||||
write_int(ostream, static_cast<std::uint16_t>(0)); // internal file
|
||||
write_int(ostream, static_cast<std::uint32_t>(0)); // ext final
|
||||
write_int(ostream, static_cast<std::uint32_t>(header.header_offset)); // rel offset
|
||||
}
|
||||
|
||||
for (auto c : header.filename)
|
||||
|
@ -323,8 +323,8 @@ class zip_streambuf_compress : public std::streambuf
|
|||
std::array<char, buffer_size> out;
|
||||
|
||||
zip_file_header *header;
|
||||
unsigned int uncompressed_size;
|
||||
unsigned int crc;
|
||||
std::uint32_t uncompressed_size;
|
||||
std::uint32_t crc;
|
||||
|
||||
bool valid;
|
||||
|
||||
|
@ -410,14 +410,14 @@ protected:
|
|||
|
||||
auto generated_output = static_cast<int>(strm.next_out - reinterpret_cast<std::uint8_t *>(out.data()));
|
||||
ostream.write(out.data(), generated_output);
|
||||
if (header) header->compressed_size += static_cast<unsigned int>(generated_output);
|
||||
if (header) header->compressed_size += static_cast<std::uint32_t>(generated_output);
|
||||
if (ret == Z_STREAM_END) break;
|
||||
}
|
||||
|
||||
// update counts, crc's and buffers
|
||||
auto consumed_input = static_cast<unsigned int>(pptr() - pbase());
|
||||
auto consumed_input = static_cast<std::uint32_t>(pptr() - pbase());
|
||||
uncompressed_size += consumed_input;
|
||||
crc = static_cast<unsigned int>(crc32(crc, reinterpret_cast<Bytef *>(in.data()), consumed_input));
|
||||
crc = static_cast<std::uint32_t>(crc32(crc, reinterpret_cast<Bytef *>(in.data()), consumed_input));
|
||||
setp(pbase(), pbase() + buffer_size - 4);
|
||||
|
||||
return 1;
|
||||
|
|
|
@ -54,7 +54,8 @@ struct zip_file_header
|
|||
std::uint16_t version = 20;
|
||||
std::uint16_t flags = 0;
|
||||
std::uint16_t compression_type = 8;
|
||||
std::uint16_t stamp_date, stamp_time = 0;
|
||||
std::uint16_t stamp_date = 0;
|
||||
std::uint16_t stamp_time = 0;
|
||||
std::uint32_t crc = 0;
|
||||
std::uint32_t compressed_size = 0;
|
||||
std::uint32_t uncompressed_size = 0;
|
||||
|
|
|
@ -71,25 +71,25 @@ public:
|
|||
|
||||
void test_round_trip_all_styles_rw()
|
||||
{
|
||||
auto path = path_helper::get_data_directory("13_all_styles.xlsx");
|
||||
auto path = path_helper::get_data_directory("10_all_styles.xlsx");
|
||||
TS_ASSERT(round_trip_matches_rw(path));
|
||||
}
|
||||
|
||||
void test_round_trip_headers_footers()
|
||||
{
|
||||
auto path = path_helper::get_data_directory("21_headers_and_footers.xlsx");
|
||||
auto path = path_helper::get_data_directory("18_headers_and_footers.xlsx");
|
||||
TS_ASSERT(round_trip_matches_rw(path));
|
||||
}
|
||||
|
||||
void test_round_trip_row_and_col_props()
|
||||
{
|
||||
auto path = path_helper::get_data_directory("22_row_and_col_properties.xlsx");
|
||||
auto path = path_helper::get_data_directory("19_row_and_col_properties.xlsx");
|
||||
TS_ASSERT(round_trip_matches_rw(path));
|
||||
}
|
||||
|
||||
void test_round_trip_page_breaks()
|
||||
{
|
||||
auto path = path_helper::get_data_directory("23_page_breaks.xlsx");
|
||||
auto path = path_helper::get_data_directory("20_page_breaks.xlsx");
|
||||
TS_ASSERT(round_trip_matches_rw(path));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -285,7 +285,7 @@ public:
|
|||
left_contents_stream << left_member_stream.rdbuf();
|
||||
std::string left_member_contents(left_contents_raw.begin(), left_contents_raw.end());
|
||||
|
||||
auto right_member_streambuf = right_archive.open(left_member);
|
||||
auto right_member_streambuf = left_archive.open(left_member);
|
||||
std::istream right_member_stream(right_member_streambuf.get());
|
||||
std::vector<std::uint8_t> right_contents_raw;
|
||||
xlnt::detail::vector_ostreambuf right_contents_buffer(right_contents_raw);
|
||||
|
|
Loading…
Reference in New Issue
Block a user