GzipNoCompression witout zlib calls

This commit is contained in:
orignal 2020-05-02 21:18:44 -04:00
parent e301387896
commit 1eead0e885
3 changed files with 25 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include <string.h> /* memset */
#include <iostream>
#include "Log.h"
#include "I2PEndian.h"
#include "Gzip.h"
namespace i2p
@ -111,5 +112,19 @@ namespace data
LogPrint (eLogError, "Gzip: Deflate error ", err);
return 0;
}
size_t GzipNoCompression (const uint8_t * in, uint16_t inLen, uint8_t * out, size_t outLen)
{
static const uint8_t gzipHeader[11] = { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, 0x01 };
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;
}
} // data
} // i2p

View File

@ -3,8 +3,10 @@
#include <zlib.h>
namespace i2p {
namespace data {
namespace i2p
{
namespace data
{
class GzipInflator
{
public:
@ -38,6 +40,9 @@ namespace data {
z_stream m_Deflator;
bool m_IsDirty;
};
size_t GzipNoCompression (const uint8_t * in, uint16_t inLen, uint8_t * out, size_t outLen); // for < 64K
} // data
} // i2p

View File

@ -1212,14 +1212,12 @@ namespace stream
const uint8_t * payload, size_t len, uint16_t toPort, bool checksum)
{
auto msg = m_I2NPMsgsPool.AcquireShared ();
if (!m_Gzip || len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE)
m_Deflator.SetCompressionLevel (Z_NO_COMPRESSION);
else
m_Deflator.SetCompressionLevel (Z_DEFAULT_COMPRESSION);
uint8_t * buf = msg->GetPayload ();
buf += 4; // reserve for lengthlength
msg->len += 4;
size_t size = m_Deflator.Deflate (payload, len, buf, msg->maxLen - msg->len);
size_t size = (!m_Gzip || len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE)?
i2p::data::GzipNoCompression (payload, len, buf, msg->maxLen - msg->len):
m_Deflator.Deflate (payload, len, buf, msg->maxLen - msg->len);
if (size)
{
htobe32buf (msg->GetPayload (), size); // length