From 648d035a0fe6cd44fab7108f09314b9d75965231 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 19 May 2020 21:02:32 -0400 Subject: [PATCH] GzipNoCompression for datagrams --- libi2pd/Datagram.cpp | 4 ++-- libi2pd/Gzip.cpp | 22 ++++++++++++++++++++++ libi2pd/Gzip.h | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/libi2pd/Datagram.cpp b/libi2pd/Datagram.cpp index 7eb20460..08cd86bf 100644 --- a/libi2pd/Datagram.cpp +++ b/libi2pd/Datagram.cpp @@ -127,8 +127,8 @@ namespace datagram auto msg = NewI2NPMessage (); uint8_t * buf = msg->GetPayload (); buf += 4; // reserve for length - m_Deflator.SetCompressionLevel (m_Gzip ? Z_DEFAULT_COMPRESSION : Z_NO_COMPRESSION); - size_t size = m_Deflator.Deflate (payloads, buf, msg->maxLen - msg->len); + size_t size = m_Gzip ? m_Deflator.Deflate (payloads, buf, msg->maxLen - msg->len) : + i2p::data::GzipNoCompression (payloads, buf, msg->maxLen - msg->len); if (size) { htobe32buf (msg->GetPayload (), size); // length diff --git a/libi2pd/Gzip.cpp b/libi2pd/Gzip.cpp index f098987a..f36620ae 100644 --- a/libi2pd/Gzip.cpp +++ b/libi2pd/Gzip.cpp @@ -174,6 +174,28 @@ namespace data htole32buf (out + inLen + 19, inLen); return inLen + 23; } + + size_t GzipNoCompression (const std::vector >& bufs, uint8_t * out, size_t outLen) + { + 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; + memcpy (out + 15 + len1, it.first, it.second); + crc = crc32 (crc, it.first, it.second); + } + 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; + } } // data } // i2p diff --git a/libi2pd/Gzip.h b/libi2pd/Gzip.h index e5b8775e..16776d35 100644 --- a/libi2pd/Gzip.h +++ b/libi2pd/Gzip.h @@ -44,6 +44,7 @@ namespace data }; size_t GzipNoCompression (const uint8_t * in, uint16_t inLen, uint8_t * out, size_t outLen); // for < 64K + size_t GzipNoCompression (const std::vector >& bufs, uint8_t * out, size_t outLen); // for total size < 64K } // data } // i2p