2016-06-28 00:00:00 +00:00
|
|
|
/*
|
2022-12-03 19:27:52 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2016-06-28 00:00:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
#include <iostream>
|
2017-01-31 00:56:06 +00:00
|
|
|
#include "Log.h"
|
2020-05-03 01:18:44 +00:00
|
|
|
#include "I2PEndian.h"
|
2016-06-28 00:00:00 +00:00
|
|
|
#include "Gzip.h"
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
namespace i2p
|
2017-01-31 00:56:06 +00:00
|
|
|
{
|
2018-01-06 03:48:51 +00:00
|
|
|
namespace data
|
2017-01-31 00:56:06 +00:00
|
|
|
{
|
2016-06-28 00:00:00 +00:00
|
|
|
const size_t GZIP_CHUNK_SIZE = 16384;
|
|
|
|
|
|
|
|
GzipInflator::GzipInflator (): m_IsDirty (false)
|
|
|
|
{
|
|
|
|
memset (&m_Inflator, 0, sizeof (m_Inflator));
|
|
|
|
inflateInit2 (&m_Inflator, MAX_WBITS + 16); // gzip
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipInflator::~GzipInflator ()
|
|
|
|
{
|
|
|
|
inflateEnd (&m_Inflator);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GzipInflator::Inflate (const uint8_t * in, size_t inLen, uint8_t * out, size_t outLen)
|
|
|
|
{
|
2020-05-19 14:48:23 +00:00
|
|
|
if (inLen < 23) return 0;
|
|
|
|
if (in[10] == 0x01) // non compressed
|
|
|
|
{
|
|
|
|
size_t len = bufle16toh (in + 11);
|
|
|
|
if (len + 23 < inLen)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Gzip: Incorrect length");
|
|
|
|
return 0;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-05-19 14:48:23 +00:00
|
|
|
if (len > outLen) len = outLen;
|
|
|
|
memcpy (out, in + 15, len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
else
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2020-05-19 14:48:23 +00:00
|
|
|
if (m_IsDirty) inflateReset (&m_Inflator);
|
|
|
|
m_IsDirty = true;
|
|
|
|
m_Inflator.next_in = const_cast<uint8_t *>(in);
|
|
|
|
m_Inflator.avail_in = inLen;
|
|
|
|
m_Inflator.next_out = out;
|
|
|
|
m_Inflator.avail_out = outLen;
|
|
|
|
int err;
|
|
|
|
if ((err = inflate (&m_Inflator, Z_NO_FLUSH)) == Z_STREAM_END)
|
|
|
|
return outLen - m_Inflator.avail_out;
|
|
|
|
// else
|
2022-12-03 19:27:52 +00:00
|
|
|
if (err)
|
|
|
|
LogPrint (eLogError, "Gzip: Inflate error ", err);
|
2020-05-19 14:48:23 +00:00
|
|
|
return 0;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2016-06-28 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GzipInflator::Inflate (const uint8_t * in, size_t inLen, std::ostream& os)
|
|
|
|
{
|
|
|
|
m_IsDirty = true;
|
|
|
|
uint8_t * out = new uint8_t[GZIP_CHUNK_SIZE];
|
|
|
|
m_Inflator.next_in = const_cast<uint8_t *>(in);
|
|
|
|
m_Inflator.avail_in = inLen;
|
|
|
|
int ret;
|
2018-01-06 03:48:51 +00:00
|
|
|
do
|
2017-01-31 00:56:06 +00:00
|
|
|
{
|
2016-06-28 00:00:00 +00:00
|
|
|
m_Inflator.next_out = out;
|
|
|
|
m_Inflator.avail_out = GZIP_CHUNK_SIZE;
|
|
|
|
ret = inflate (&m_Inflator, Z_NO_FLUSH);
|
2018-01-06 03:48:51 +00:00
|
|
|
if (ret < 0)
|
2017-01-31 00:56:06 +00:00
|
|
|
{
|
2016-06-28 00:00:00 +00:00
|
|
|
inflateEnd (&m_Inflator);
|
|
|
|
os.setstate(std::ios_base::failbit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
os.write ((char *)out, GZIP_CHUNK_SIZE - m_Inflator.avail_out);
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2017-01-31 00:56:06 +00:00
|
|
|
while (!m_Inflator.avail_out); // more data to read
|
2016-06-28 00:00:00 +00:00
|
|
|
delete[] out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GzipInflator::Inflate (std::istream& in, std::ostream& out)
|
|
|
|
{
|
|
|
|
uint8_t * buf = new uint8_t[GZIP_CHUNK_SIZE];
|
|
|
|
while (!in.eof ())
|
|
|
|
{
|
|
|
|
in.read ((char *) buf, GZIP_CHUNK_SIZE);
|
|
|
|
Inflate (buf, in.gcount (), out);
|
|
|
|
}
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipDeflator::GzipDeflator (): m_IsDirty (false)
|
|
|
|
{
|
|
|
|
memset (&m_Deflator, 0, sizeof (m_Deflator));
|
|
|
|
deflateInit2 (&m_Deflator, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY); // 15 + 16 sets gzip
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipDeflator::~GzipDeflator ()
|
|
|
|
{
|
|
|
|
deflateEnd (&m_Deflator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GzipDeflator::SetCompressionLevel (int level)
|
|
|
|
{
|
|
|
|
deflateParams (&m_Deflator, level, Z_DEFAULT_STRATEGY);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GzipDeflator::Deflate (const uint8_t * in, size_t inLen, uint8_t * out, size_t outLen)
|
|
|
|
{
|
|
|
|
if (m_IsDirty) deflateReset (&m_Deflator);
|
|
|
|
m_IsDirty = true;
|
|
|
|
m_Deflator.next_in = const_cast<uint8_t *>(in);
|
|
|
|
m_Deflator.avail_in = inLen;
|
|
|
|
m_Deflator.next_out = out;
|
|
|
|
m_Deflator.avail_out = outLen;
|
|
|
|
int err;
|
2018-01-06 03:48:51 +00:00
|
|
|
if ((err = deflate (&m_Deflator, Z_FINISH)) == Z_STREAM_END)
|
2020-05-03 13:27:17 +00:00
|
|
|
{
|
|
|
|
out[9] = 0xff; // OS is always unknown
|
2016-06-28 00:00:00 +00:00
|
|
|
return outLen - m_Deflator.avail_out;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2017-01-31 00:56:06 +00:00
|
|
|
// else
|
2022-12-03 19:27:52 +00:00
|
|
|
if (err)
|
|
|
|
LogPrint (eLogError, "Gzip: Deflate error ", err);
|
2016-06-28 00:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-03 01:18:44 +00:00
|
|
|
|
2020-05-17 20:49:31 +00:00
|
|
|
size_t GzipDeflator::Deflate (const std::vector<std::pair<const uint8_t *, size_t> >& bufs, uint8_t * out, size_t outLen)
|
|
|
|
{
|
|
|
|
if (m_IsDirty) deflateReset (&m_Deflator);
|
|
|
|
m_IsDirty = true;
|
|
|
|
size_t offset = 0;
|
2023-03-02 10:14:49 +00:00
|
|
|
int err = 0;
|
2020-05-17 20:49:31 +00:00
|
|
|
for (const auto& it: bufs)
|
|
|
|
{
|
|
|
|
m_Deflator.next_in = const_cast<uint8_t *>(it.first);
|
|
|
|
m_Deflator.avail_in = it.second;
|
|
|
|
m_Deflator.next_out = out + offset;
|
|
|
|
m_Deflator.avail_out = outLen - offset;
|
|
|
|
auto flush = (it == bufs.back ()) ? Z_FINISH : Z_NO_FLUSH;
|
|
|
|
err = deflate (&m_Deflator, flush);
|
|
|
|
if (err)
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2020-05-17 20:49:31 +00:00
|
|
|
if (flush && err == Z_STREAM_END)
|
|
|
|
{
|
|
|
|
out[9] = 0xff; // OS is always unknown
|
|
|
|
return outLen - m_Deflator.avail_out;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-05-17 20:49:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset = outLen - m_Deflator.avail_out;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-05-17 20:49:31 +00:00
|
|
|
// else
|
2022-12-03 19:27:52 +00:00
|
|
|
if (err)
|
|
|
|
LogPrint (eLogError, "Gzip: Deflate error ", err);
|
2020-05-17 20:49:31 +00:00
|
|
|
return 0;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 01:18:44 +00:00
|
|
|
size_t GzipNoCompression (const uint8_t * in, uint16_t inLen, uint8_t * out, size_t outLen)
|
|
|
|
{
|
2020-05-19 14:48:23 +00:00
|
|
|
static const uint8_t gzipHeader[11] = { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x01 };
|
2020-05-03 01:18:44 +00:00
|
|
|
if (outLen < (size_t)inLen + 23) return 0;
|
|
|
|
memcpy (out, gzipHeader, 11);
|
|
|
|
htole16buf (out + 11, inLen);
|
|
|
|
htole16buf (out + 13, 0xffff - inLen);
|
|
|
|
memcpy (out + 15, in, inLen);
|
|
|
|
htole32buf (out + inLen + 15, crc32 (0, in, inLen));
|
|
|
|
htole32buf (out + inLen + 19, inLen);
|
|
|
|
return inLen + 23;
|
|
|
|
}
|
2020-05-20 01:02:32 +00:00
|
|
|
|
|
|
|
size_t GzipNoCompression (const std::vector<std::pair<const uint8_t *, size_t> >& bufs, uint8_t * out, size_t outLen)
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2020-05-20 01:02:32 +00:00
|
|
|
static const uint8_t gzipHeader[11] = { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x01 };
|
|
|
|
memcpy (out, gzipHeader, 11);
|
|
|
|
uint32_t crc = 0;
|
|
|
|
size_t len = 0, len1;
|
|
|
|
for (const auto& it: bufs)
|
|
|
|
{
|
|
|
|
len1 = len;
|
|
|
|
len += it.second;
|
|
|
|
if (outLen < len + 23) return 0;
|
2020-03-01 10:25:50 +00:00
|
|
|
memcpy (out + 15 + len1, it.first, it.second);
|
2020-05-20 01:02:32 +00:00
|
|
|
crc = crc32 (crc, it.first, it.second);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-05-20 01:02:32 +00:00
|
|
|
if (len > 0xffff) return 0;
|
|
|
|
htole32buf (out + len + 15, crc);
|
|
|
|
htole32buf (out + len + 19, len);
|
|
|
|
htole16buf (out + 11, len);
|
|
|
|
htole16buf (out + 13, 0xffff - len);
|
|
|
|
return len + 23;
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2016-06-28 00:00:00 +00:00
|
|
|
} // data
|
|
|
|
} // i2p
|