Browse Source

GzipNoCompression for datagrams

pull/1514/head
orignal 5 years ago
parent
commit
648d035a0f
  1. 4
      libi2pd/Datagram.cpp
  2. 22
      libi2pd/Gzip.cpp
  3. 1
      libi2pd/Gzip.h

4
libi2pd/Datagram.cpp

@ -127,8 +127,8 @@ namespace datagram
auto msg = NewI2NPMessage (); auto msg = NewI2NPMessage ();
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
buf += 4; // reserve for length buf += 4; // reserve for length
m_Deflator.SetCompressionLevel (m_Gzip ? Z_DEFAULT_COMPRESSION : Z_NO_COMPRESSION); size_t size = m_Gzip ? m_Deflator.Deflate (payloads, buf, msg->maxLen - msg->len) :
size_t size = m_Deflator.Deflate (payloads, buf, msg->maxLen - msg->len); i2p::data::GzipNoCompression (payloads, buf, msg->maxLen - msg->len);
if (size) if (size)
{ {
htobe32buf (msg->GetPayload (), size); // length htobe32buf (msg->GetPayload (), size); // length

22
libi2pd/Gzip.cpp

@ -174,6 +174,28 @@ namespace data
htole32buf (out + inLen + 19, inLen); htole32buf (out + inLen + 19, inLen);
return inLen + 23; 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 } // data
} // i2p } // i2p

1
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 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 } // data
} // i2p } // i2p

Loading…
Cancel
Save