Skip to content

Commit

Permalink
GzipNoCompression for datagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 20, 2020
1 parent 7ebf2f0 commit 648d035
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libi2pd/Datagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions libi2pd/Gzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ namespace data
htole32buf (out + inLen + 19, inLen);
return inLen + 23;
}

size_t GzipNoCompression (const std::vector<std::pair<const uint8_t *, size_t> >& 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
1 change: 1 addition & 0 deletions libi2pd/Gzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<const uint8_t *, size_t> >& bufs, uint8_t * out, size_t outLen); // for total size < 64K

} // data
} // i2p
Expand Down

0 comments on commit 648d035

Please sign in to comment.