2016-10-31 03:48:40 +08:00
|
|
|
/*
|
|
|
|
PARTIO SOFTWARE
|
|
|
|
Copyright 2010 Disney Enterprises, Inc. All rights reserved
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
* The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
|
|
|
|
Studios" or the names of its contributors may NOT be used to
|
|
|
|
endorse or promote products derived from this software without
|
|
|
|
specific prior written permission from Walt Disney Pictures.
|
|
|
|
|
|
|
|
Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
|
|
|
|
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
|
|
|
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
|
|
|
|
CONTRIBUTORS 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 BASED 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 DAMAGES.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-11-10 08:52:18 +08:00
|
|
|
#include <array>
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <cassert>
|
2016-11-08 10:11:30 +08:00
|
|
|
#include <cstring>
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
2016-11-01 09:53:22 +08:00
|
|
|
#include <iterator> // for std::back_inserter
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/serialization/miniz.hpp>
|
|
|
|
#include <detail/serialization/vector_streambuf.hpp>
|
|
|
|
#include <detail/serialization/zstream.hpp>
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
namespace {
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
template <class T>
|
2016-12-24 23:04:57 +08:00
|
|
|
T read_int(std::istream &stream)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-01 08:48:43 +08:00
|
|
|
T value;
|
|
|
|
stream.read(reinterpret_cast<char *>(&value), sizeof(T));
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return value;
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
template <class T>
|
2016-12-24 23:04:57 +08:00
|
|
|
void write_int(std::ostream &stream, T value)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-01 08:48:43 +08:00
|
|
|
stream.write(reinterpret_cast<char *>(&value), sizeof(T));
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
xlnt::detail::zheader read_header(std::istream &istream, const bool global)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2017-01-05 08:02:31 +08:00
|
|
|
xlnt::detail::zheader header;
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
auto sig = read_int<std::uint32_t>(istream);
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
// read and check for local/global magic
|
2016-11-08 10:11:30 +08:00
|
|
|
if (global)
|
2016-11-01 10:41:20 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
if (sig != 0x02014b50)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
throw xlnt::exception("missing global header signature");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
header.version = read_int<std::uint16_t>(istream);
|
2016-11-01 10:41:20 +08:00
|
|
|
}
|
2016-11-08 10:11:30 +08:00
|
|
|
else if (sig != 0x04034b50)
|
2016-11-01 10:41:20 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
throw xlnt::exception("missing local header signature");
|
2016-11-01 10:41:20 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
// Read rest of header
|
2016-12-24 23:04:57 +08:00
|
|
|
header.version = read_int<std::uint16_t>(istream);
|
|
|
|
header.flags = read_int<std::uint16_t>(istream);
|
|
|
|
header.compression_type = read_int<std::uint16_t>(istream);
|
|
|
|
header.stamp_date = read_int<std::uint16_t>(istream);
|
|
|
|
header.stamp_time = read_int<std::uint16_t>(istream);
|
|
|
|
header.crc = read_int<std::uint32_t>(istream);
|
|
|
|
header.compressed_size = read_int<std::uint32_t>(istream);
|
|
|
|
header.uncompressed_size = read_int<std::uint32_t>(istream);
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
auto filename_length = read_int<std::uint16_t>(istream);
|
|
|
|
auto extra_length = read_int<std::uint16_t>(istream);
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
std::uint16_t comment_length = 0;
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (global)
|
2016-11-01 10:41:20 +08:00
|
|
|
{
|
|
|
|
comment_length = read_int<std::uint16_t>(istream);
|
2016-11-08 10:11:30 +08:00
|
|
|
/*std::uint16_t disk_number_start = */ read_int<std::uint16_t>(istream);
|
|
|
|
/*std::uint16_t int_file_attrib = */ read_int<std::uint16_t>(istream);
|
|
|
|
/*std::uint32_t ext_file_attrib = */ read_int<std::uint32_t>(istream);
|
2016-12-24 23:04:57 +08:00
|
|
|
header.header_offset = read_int<std::uint32_t>(istream);
|
2016-11-01 10:41:20 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
header.filename.resize(filename_length, '\0');
|
|
|
|
istream.read(&header.filename[0], filename_length);
|
2016-11-08 10:11:30 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
header.extra.resize(extra_length, 0);
|
|
|
|
istream.read(reinterpret_cast<char *>(header.extra.data()), extra_length);
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-01 10:41:20 +08:00
|
|
|
if (global)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
header.comment.resize(comment_length, '\0');
|
|
|
|
istream.read(&header.comment[0], comment_length);
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return header;
|
2016-11-01 10:41:20 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
void write_header(const xlnt::detail::zheader &header, std::ostream &ostream, const bool global)
|
2016-11-01 10:41:20 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
if (global)
|
|
|
|
{
|
2017-01-01 08:15:09 +08:00
|
|
|
write_int(ostream, static_cast<std::uint32_t>(0x02014b50)); // header sig
|
|
|
|
write_int(ostream, static_cast<std::uint16_t>(20)); // version made by
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-01 08:15:09 +08:00
|
|
|
write_int(ostream, static_cast<std::uint32_t>(0x04034b50));
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
write_int(ostream, header.version);
|
|
|
|
write_int(ostream, header.flags);
|
|
|
|
write_int(ostream, header.compression_type);
|
|
|
|
write_int(ostream, header.stamp_date);
|
|
|
|
write_int(ostream, header.stamp_time);
|
|
|
|
write_int(ostream, header.crc);
|
|
|
|
write_int(ostream, header.compressed_size);
|
|
|
|
write_int(ostream, header.uncompressed_size);
|
2017-01-01 08:15:09 +08:00
|
|
|
write_int(ostream, static_cast<std::uint16_t>(header.filename.length()));
|
|
|
|
write_int(ostream, static_cast<std::uint16_t>(0)); // extra lengthx
|
2016-11-08 10:11:30 +08:00
|
|
|
|
|
|
|
if (global)
|
|
|
|
{
|
2017-01-01 08:15:09 +08:00
|
|
|
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
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (auto c : header.filename)
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
write_int(ostream, c);
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-01 10:41:20 +08:00
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
namespace detail {
|
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
static const std::size_t buffer_size = 512;
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
class zip_streambuf_decompress : public std::streambuf
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
std::istream &istream;
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
z_stream strm;
|
2016-11-10 08:52:18 +08:00
|
|
|
std::array<char, buffer_size> in;
|
|
|
|
std::array<char, buffer_size> out;
|
2017-01-05 08:02:31 +08:00
|
|
|
zheader header;
|
2016-11-10 08:52:18 +08:00
|
|
|
std::size_t total_read;
|
|
|
|
std::size_t total_uncompressed;
|
2016-10-31 03:48:40 +08:00
|
|
|
bool valid;
|
|
|
|
bool compressed_data;
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
static const unsigned short DEFLATE = 8;
|
|
|
|
static const unsigned short UNCOMPRESSED = 0;
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
public:
|
2017-01-05 08:02:31 +08:00
|
|
|
zip_streambuf_decompress(std::istream &stream, zheader central_header)
|
2016-11-08 10:11:30 +08:00
|
|
|
: istream(stream), header(central_header), total_read(0), total_uncompressed(0), valid(true)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2017-04-30 00:29:41 +08:00
|
|
|
in.fill(0);
|
|
|
|
out.fill(0);
|
|
|
|
|
2017-09-04 22:10:51 +08:00
|
|
|
strm.zalloc = nullptr;
|
|
|
|
strm.zfree = nullptr;
|
|
|
|
strm.opaque = nullptr;
|
2016-11-08 10:11:30 +08:00
|
|
|
strm.avail_in = 0;
|
2017-09-04 22:10:51 +08:00
|
|
|
strm.next_in = nullptr;
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
setg(in.data(), in.data(), in.data());
|
2017-09-04 22:10:51 +08:00
|
|
|
setp(nullptr, nullptr);
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// skip the header
|
2016-12-24 23:04:57 +08:00
|
|
|
read_header(istream, false);
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (header.compression_type == DEFLATE)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
compressed_data = true;
|
2016-11-10 08:52:18 +08:00
|
|
|
}
|
2016-11-08 10:11:30 +08:00
|
|
|
else if (header.compression_type == UNCOMPRESSED)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
compressed_data = false;
|
2016-11-10 08:52:18 +08:00
|
|
|
}
|
2016-11-08 10:11:30 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
compressed_data = false;
|
2016-12-30 07:36:29 +08:00
|
|
|
throw xlnt::exception("unsupported compression type, should be DEFLATE or uncompressed");
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// initialize the inflate
|
2016-11-08 10:11:30 +08:00
|
|
|
if (compressed_data && valid)
|
|
|
|
{
|
2016-11-10 08:52:18 +08:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wold-style-cast"
|
2016-11-08 10:11:30 +08:00
|
|
|
int result = inflateInit2(&strm, -MAX_WBITS);
|
2016-11-10 08:52:18 +08:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (result != Z_OK)
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
throw xlnt::exception("couldn't inflate ZIP, possibly corrupted");
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
header = central_header;
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
virtual ~zip_streambuf_decompress()
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
2016-11-10 08:52:18 +08:00
|
|
|
if (compressed_data && valid)
|
|
|
|
{
|
|
|
|
inflateEnd(&strm);
|
|
|
|
}
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
int process()
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
|
|
|
if (!valid) return -1;
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (compressed_data)
|
|
|
|
{
|
|
|
|
strm.avail_out = buffer_size - 4;
|
2016-11-10 08:52:18 +08:00
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(out.data() + 4);
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
while (strm.avail_out != 0)
|
|
|
|
{
|
|
|
|
if (strm.avail_in == 0)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
|
|
|
// buffer empty, read some more from file
|
2016-12-24 23:04:57 +08:00
|
|
|
istream.read(in.data(),
|
|
|
|
static_cast<std::streamsize>(std::min(buffer_size, header.compressed_size - total_read)));
|
2016-11-10 08:52:18 +08:00
|
|
|
strm.avail_in = static_cast<unsigned int>(istream.gcount());
|
2016-11-08 10:11:30 +08:00
|
|
|
total_read += strm.avail_in;
|
2016-11-10 08:52:18 +08:00
|
|
|
strm.next_in = reinterpret_cast<Bytef *>(in.data());
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-03-21 07:22:46 +08:00
|
|
|
const auto ret = inflate(&strm, Z_NO_FLUSH); // decompress
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-03-21 07:22:46 +08:00
|
|
|
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
throw xlnt::exception("couldn't inflate ZIP, possibly corrupted");
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (ret == Z_STREAM_END) break;
|
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
|
|
|
auto unzip_count = buffer_size - strm.avail_out - 4;
|
2016-11-08 10:11:30 +08:00
|
|
|
total_uncompressed += unzip_count;
|
2016-11-10 08:52:18 +08:00
|
|
|
return static_cast<int>(unzip_count);
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
|
|
|
// uncompressed, so just read
|
2016-12-24 23:04:57 +08:00
|
|
|
istream.read(out.data() + 4,
|
|
|
|
static_cast<std::streamsize>(std::min(buffer_size - 4, header.uncompressed_size - total_read)));
|
2016-11-10 08:52:18 +08:00
|
|
|
auto count = istream.gcount();
|
|
|
|
total_read += static_cast<std::size_t>(count);
|
|
|
|
return static_cast<int>(count);
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
virtual int underflow()
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
|
|
|
if (gptr() && (gptr() < egptr()))
|
|
|
|
return traits_type::to_int_type(*gptr()); // if we already have data just use it
|
2016-11-10 08:52:18 +08:00
|
|
|
auto put_back_count = gptr() - eback();
|
2016-11-08 10:11:30 +08:00
|
|
|
if (put_back_count > 4) put_back_count = 4;
|
2016-12-24 23:04:57 +08:00
|
|
|
std::memmove(
|
|
|
|
out.data() + (4 - put_back_count), gptr() - put_back_count, static_cast<std::size_t>(put_back_count));
|
2016-11-08 10:11:30 +08:00
|
|
|
int num = process();
|
2016-11-10 08:52:18 +08:00
|
|
|
setg(out.data() + 4 - put_back_count, out.data() + 4, out.data() + 4 + num);
|
2016-11-08 10:11:30 +08:00
|
|
|
if (num <= 0) return EOF;
|
|
|
|
return traits_type::to_int_type(*gptr());
|
|
|
|
}
|
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
virtual int overflow(int c = EOF);
|
2016-10-31 03:48:40 +08:00
|
|
|
};
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
int zip_streambuf_decompress::overflow(int)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
|
|
|
throw xlnt::exception("writing to read-only buffer");
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
class zip_streambuf_compress : public std::streambuf
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
std::ostream &ostream; // owned when header==0 (when not part of zip file)
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
z_stream strm;
|
2016-11-10 08:52:18 +08:00
|
|
|
std::array<char, buffer_size> in;
|
|
|
|
std::array<char, buffer_size> out;
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
zheader *header;
|
2017-01-01 08:15:09 +08:00
|
|
|
std::uint32_t uncompressed_size;
|
|
|
|
std::uint32_t crc;
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
public:
|
2017-01-05 08:02:31 +08:00
|
|
|
zip_streambuf_compress(zheader *central_header, std::ostream &stream)
|
2016-12-14 18:39:38 +08:00
|
|
|
: ostream(stream), header(central_header), valid(true)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2017-09-04 22:10:51 +08:00
|
|
|
strm.zalloc = nullptr;
|
|
|
|
strm.zfree = nullptr;
|
|
|
|
strm.opaque = nullptr;
|
2016-11-10 08:52:18 +08:00
|
|
|
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wold-style-cast"
|
2016-11-08 10:11:30 +08:00
|
|
|
int ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
|
2016-12-24 23:04:57 +08:00
|
|
|
#pragma clang diagnostic pop
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (ret != Z_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "libz: failed to deflateInit" << std::endl;
|
|
|
|
valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-09-04 22:10:51 +08:00
|
|
|
setg(nullptr, nullptr, nullptr);
|
2016-11-10 08:52:18 +08:00
|
|
|
setp(in.data(), in.data() + buffer_size - 4); // we want to be 4 aligned
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// Write appropriate header
|
2016-11-08 10:11:30 +08:00
|
|
|
if (header)
|
|
|
|
{
|
2016-11-10 08:52:18 +08:00
|
|
|
header->header_offset = static_cast<std::uint32_t>(stream.tellp());
|
2016-12-24 23:04:57 +08:00
|
|
|
write_header(*header, ostream, false);
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
uncompressed_size = crc = 0;
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
virtual ~zip_streambuf_compress()
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
|
|
|
if (valid)
|
|
|
|
{
|
|
|
|
process(true);
|
|
|
|
deflateEnd(&strm);
|
|
|
|
if (header)
|
|
|
|
{
|
2018-07-16 14:59:00 +08:00
|
|
|
auto final_position = ostream.tellp();
|
2016-11-08 10:11:30 +08:00
|
|
|
header->uncompressed_size = uncompressed_size;
|
|
|
|
header->crc = crc;
|
|
|
|
ostream.seekp(header->header_offset);
|
2016-12-24 23:04:57 +08:00
|
|
|
write_header(*header, ostream, false);
|
2016-11-08 10:11:30 +08:00
|
|
|
ostream.seekp(final_position);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_int(ostream, crc);
|
|
|
|
write_int(ostream, uncompressed_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!header) delete &ostream;
|
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int process(bool flush)
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
|
|
|
if (!valid) return -1;
|
2016-11-10 08:52:18 +08:00
|
|
|
|
|
|
|
strm.next_in = reinterpret_cast<Bytef *>(pbase());
|
|
|
|
strm.avail_in = static_cast<unsigned int>(pptr() - pbase());
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
while (strm.avail_in != 0 || flush)
|
|
|
|
{
|
|
|
|
strm.avail_out = buffer_size;
|
2016-11-10 08:52:18 +08:00
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(out.data());
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
int ret = deflate(&strm, flush ? Z_FINISH : Z_NO_FLUSH);
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (!(ret != Z_BUF_ERROR && ret != Z_STREAM_ERROR))
|
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
std::cerr << "gzip: gzip error " << strm.msg << std::endl;
|
|
|
|
;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
|
|
|
auto generated_output = static_cast<int>(strm.next_out - reinterpret_cast<std::uint8_t *>(out.data()));
|
|
|
|
ostream.write(out.data(), generated_output);
|
2017-01-01 08:15:09 +08:00
|
|
|
if (header) header->compressed_size += static_cast<std::uint32_t>(generated_output);
|
2016-11-08 10:11:30 +08:00
|
|
|
if (ret == Z_STREAM_END) break;
|
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
// update counts, crc's and buffers
|
2017-01-01 08:15:09 +08:00
|
|
|
auto consumed_input = static_cast<std::uint32_t>(pptr() - pbase());
|
2016-11-08 10:11:30 +08:00
|
|
|
uncompressed_size += consumed_input;
|
2017-01-01 08:15:09 +08:00
|
|
|
crc = static_cast<std::uint32_t>(crc32(crc, reinterpret_cast<Bytef *>(in.data()), consumed_input));
|
2016-11-08 10:11:30 +08:00
|
|
|
setp(pbase(), pbase() + buffer_size - 4);
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
virtual int sync()
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
|
|
|
if (pptr() && pptr() > pbase()) return process(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
|
|
|
virtual int underflow()
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("Attempt to read write only ostream");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
virtual int overflow(int c = EOF);
|
|
|
|
};
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
int zip_streambuf_compress::overflow(int c)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
|
|
|
if (c != EOF)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2016-11-10 08:52:18 +08:00
|
|
|
*pptr() = static_cast<char>(c);
|
|
|
|
pbump(1);
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
if (process(false) == EOF) return EOF;
|
|
|
|
return c;
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
ozstream::ozstream(std::ostream &stream)
|
2016-12-24 23:04:57 +08:00
|
|
|
: destination_stream_(stream)
|
2016-11-08 10:11:30 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (!destination_stream_)
|
|
|
|
{
|
|
|
|
throw xlnt::exception("bad zip stream");
|
|
|
|
}
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
ozstream::~ozstream()
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
|
|
|
// Write all file headers
|
2018-07-16 14:59:00 +08:00
|
|
|
auto final_position = destination_stream_.tellp();
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &header : file_headers_)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
write_header(header, destination_stream_, true);
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2018-07-16 14:59:00 +08:00
|
|
|
auto central_end = destination_stream_.tellp();
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// Write end of central
|
2016-12-24 23:04:57 +08:00
|
|
|
write_int(destination_stream_, static_cast<std::uint32_t>(0x06054b50)); // end of central
|
|
|
|
write_int(destination_stream_, static_cast<std::uint16_t>(0)); // this disk number
|
|
|
|
write_int(destination_stream_, static_cast<std::uint16_t>(0)); // this disk number
|
|
|
|
write_int(destination_stream_, static_cast<std::uint16_t>(file_headers_.size())); // one entry in center in this disk
|
|
|
|
write_int(destination_stream_, static_cast<std::uint16_t>(file_headers_.size())); // one entry in center
|
|
|
|
write_int(destination_stream_, static_cast<std::uint32_t>(central_end - final_position)); // size of header
|
|
|
|
write_int(destination_stream_, static_cast<std::uint32_t>(final_position)); // offset to header
|
|
|
|
write_int(destination_stream_, static_cast<std::uint16_t>(0)); // zip comment
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
std::unique_ptr<std::streambuf> ozstream::open(const path &filename)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2017-01-05 08:02:31 +08:00
|
|
|
zheader header;
|
2016-12-24 23:04:57 +08:00
|
|
|
header.filename = filename.string();
|
2016-11-01 08:48:43 +08:00
|
|
|
file_headers_.push_back(header);
|
2017-07-03 10:10:22 +08:00
|
|
|
auto buffer = new zip_streambuf_compress(&file_headers_.back(), destination_stream_);
|
|
|
|
|
|
|
|
return std::unique_ptr<zip_streambuf_compress>(buffer);
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
izstream::izstream(std::istream &stream)
|
2016-12-24 23:04:57 +08:00
|
|
|
: source_stream_(stream)
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
if (!stream)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("Invalid file handle");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
read_central_header();
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
izstream::~izstream()
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
bool izstream::read_central_header()
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
|
|
|
// Find the header
|
|
|
|
// NOTE: this assumes the zip file header is the last thing written to file...
|
2016-11-08 10:11:30 +08:00
|
|
|
source_stream_.seekg(0, std::ios_base::end);
|
2018-07-16 14:59:00 +08:00
|
|
|
auto end_position = static_cast<std::size_t>(source_stream_.tellg());
|
2016-11-01 08:48:43 +08:00
|
|
|
|
|
|
|
auto max_comment_size = std::uint32_t(0xffff); // max size of header
|
|
|
|
auto read_size_before_comment = std::uint32_t(22);
|
|
|
|
|
2018-07-16 14:59:00 +08:00
|
|
|
std::uint32_t read_start = max_comment_size + read_size_before_comment;
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (read_start > end_position)
|
|
|
|
{
|
|
|
|
read_start = end_position;
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
source_stream_.seekg(end_position - read_start);
|
2017-02-18 13:11:06 +08:00
|
|
|
std::vector<std::uint8_t> buf(static_cast<std::size_t>(read_start), '\0');
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (read_start <= 0)
|
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("file is empty");
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
source_stream_.read(reinterpret_cast<char *>(buf.data()), read_start);
|
2017-07-03 10:10:22 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
if (buf[0] == 0xd0 && buf[1] == 0xcf && buf[2] == 0x11 && buf[3] == 0xe0
|
|
|
|
&& buf[4] == 0xa1 && buf[5] == 0xb1 && buf[6] == 0x1a && buf[7] == 0xe1)
|
|
|
|
{
|
|
|
|
throw xlnt::exception("encrypted xlsx, password required");
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
|
|
|
auto found_header = false;
|
|
|
|
std::size_t header_index = 0;
|
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
for (std::size_t i = 0; i < static_cast<std::size_t>(read_start - 3); ++i)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
if (buf[i] == 0x50 && buf[i + 1] == 0x4b && buf[i + 2] == 0x05 && buf[i + 3] == 0x06)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
found_header = true;
|
|
|
|
header_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (!found_header)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("failed to find zip header");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// seek to end of central header and read
|
2016-11-01 08:48:43 +08:00
|
|
|
source_stream_.seekg(end_position - (read_start - static_cast<std::ptrdiff_t>(header_index)));
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
/*auto word = */ read_int<std::uint32_t>(source_stream_);
|
2016-11-01 08:48:43 +08:00
|
|
|
auto disk_number1 = read_int<std::uint16_t>(source_stream_);
|
|
|
|
auto disk_number2 = read_int<std::uint16_t>(source_stream_);
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (disk_number1 != disk_number2 || disk_number1 != 0)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("multiple disk zip files are not supported");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto num_files = read_int<std::uint16_t>(source_stream_); // one entry in center in this disk
|
|
|
|
auto num_files_this_disk = read_int<std::uint16_t>(source_stream_); // one entry in center
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
if (num_files != num_files_this_disk)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("multi disk zip files are not supported");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
/*auto size_of_header = */ read_int<std::uint32_t>(source_stream_); // size of header
|
2016-11-01 08:48:43 +08:00
|
|
|
auto header_offset = read_int<std::uint32_t>(source_stream_); // offset to header
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
// go to header and read all file headers
|
2016-11-01 08:48:43 +08:00
|
|
|
source_stream_.seekg(header_offset);
|
|
|
|
|
|
|
|
for (std::uint16_t i = 0; i < num_files; ++i)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
auto header = read_header(source_stream_, true);
|
|
|
|
file_headers_[header.filename] = header;
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
std::unique_ptr<std::streambuf> izstream::open(const path &filename) const
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-11-01 08:48:43 +08:00
|
|
|
if (!has_file(filename))
|
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("file not found");
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
auto header = file_headers_.at(filename.string());
|
2016-11-01 08:48:43 +08:00
|
|
|
source_stream_.seekg(header.header_offset);
|
2017-07-03 10:10:22 +08:00
|
|
|
auto buffer = new zip_streambuf_decompress(source_stream_, header);
|
|
|
|
|
|
|
|
return std::unique_ptr<zip_streambuf_decompress>(buffer);
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
std::string izstream::read(const path &filename) const
|
|
|
|
{
|
|
|
|
auto buffer = open(filename);
|
|
|
|
std::istream stream(buffer.get());
|
|
|
|
auto bytes = to_vector(stream);
|
|
|
|
|
|
|
|
return std::string(bytes.begin(), bytes.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<path> izstream::files() const
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<path> filenames;
|
2016-11-01 08:48:43 +08:00
|
|
|
std::transform(file_headers_.begin(), file_headers_.end(), std::back_inserter(filenames),
|
2017-01-05 08:02:31 +08:00
|
|
|
[](const std::pair<std::string, zheader> &h) { return path(h.first); });
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return filenames;
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
bool izstream::has_file(const path &filename) const
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return file_headers_.count(filename.string()) != 0;
|
2016-10-31 03:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|