MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / compressData

Function compressData

source/core/StarCompression.cpp:11–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9namespace Star {
10
11void compressData(ByteArray const& in, ByteArray& out, CompressionLevel compression) {
12 out.clear();
13
14 if (in.empty())
15 return;
16
17 const size_t BUFSIZE = 32 * 1024;
18 auto tempBuffer = std::make_unique<unsigned char[]>(BUFSIZE);
19
20 z_stream strm{};
21 strm.zalloc = Z_NULL;
22 strm.zfree = Z_NULL;
23 strm.opaque = Z_NULL;
24 int deflate_res = deflateInit(&strm, compression);
25 if (deflate_res != Z_OK)
26 throw IOException(strf("Failed to initialise deflate ({})", deflate_res));
27
28 strm.next_in = (unsigned char*)in.ptr();
29 strm.avail_in = in.size();
30 strm.next_out = tempBuffer.get();
31 strm.avail_out = BUFSIZE;
32 while (deflate_res == Z_OK) {
33 deflate_res = deflate(&strm, Z_FINISH);
34 if (strm.avail_out == 0) {
35 out.append((char const*)tempBuffer.get(), BUFSIZE);
36 strm.next_out = tempBuffer.get();
37 strm.avail_out = BUFSIZE;
38 }
39 }
40 deflateEnd(&strm);
41
42 if (deflate_res != Z_STREAM_END)
43 throw IOException(strf("Internal error in uncompressData, deflate_res is {}", deflate_res));
44
45 out.append((char const*)tempBuffer.get(), BUFSIZE - strm.avail_out);
46}
47
48ByteArray compressData(ByteArray const& in, CompressionLevel compression) {
49 ByteArray out = ByteArray::withReserve(in.size());

Callers 8

sendPacketsMethod · 0.85
updateParametersMethod · 0.85
writeWorldMetadataMethod · 0.85
writeEntitySectorMethod · 0.85
writeTileSectorMethod · 0.85
writeUniqueIndexStoreMethod · 0.85

Calls 7

strfFunction · 0.85
clearMethod · 0.45
emptyMethod · 0.45
ptrMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected