GzipNoCompression witout zlib calls

pull/1515/head
orignal 4 years ago
parent e301387896
commit 1eead0e885

@ -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

@ -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

@ -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

Loading…
Cancel
Save