MCPcopy Create free account
hub / github.com/cuberite/cuberite / CompressStringGZIP

Function CompressStringGZIP

src/StringCompression.cpp:56–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54
55
56int CompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Compressed)
57{
58 // Compress a_Data into a_Compressed using GZIP; return Z_XXX error constants same as zlib's compress2()
59
60 a_Compressed.reserve(a_Length);
61
62 char Buffer[64 KiB];
63 z_stream strm;
64 memset(&strm, 0, sizeof(strm));
65 strm.next_in = (Bytef *)a_Data;
66 strm.avail_in = a_Length;
67 strm.next_out = (Bytef *)Buffer;
68 strm.avail_out = sizeof(Buffer);
69
70 int res = deflateInit2(&strm, 9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY);
71 if (res != Z_OK)
72 {
73 LOG("%s: compression initialization failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
74 return res;
75 }
76
77 for (;;)
78 {
79 res = deflate(&strm, Z_FINISH);
80 switch (res)
81 {
82 case Z_OK:
83 {
84 // Some data has been compressed. Consume the buffer and continue compressing
85 a_Compressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
86 strm.next_out = (Bytef *)Buffer;
87 strm.avail_out = sizeof(Buffer);
88 if (strm.avail_in == 0)
89 {
90 // All data has been compressed
91 deflateEnd(&strm);
92 return Z_OK;
93 }
94 break;
95 }
96
97 case Z_STREAM_END:
98 {
99 // Finished compressing. Consume the rest of the buffer and return
100 a_Compressed.append(Buffer, sizeof(Buffer) - strm.avail_out);
101 deflateEnd(&strm);
102 return Z_OK;
103 }
104
105 default:
106 {
107 // An error has occurred, log it and return the error value
108 LOG("%s: compression failed: %d (\"%s\").", __FUNCTION__, res, strm.msg);
109 deflateEnd(&strm);
110 return res;
111 }
112 } // switch (res)
113 } // while (true)

Callers 6

WriteItemMethod · 0.85
WriteBlockEntityMethod · 0.85
WriteItemMethod · 0.85
SaveToSchematicStringMethod · 0.85
SaveMethod · 0.85
SaveMethod · 0.85

Calls 3

LOGFunction · 0.85
deflateFunction · 0.85
deflateEndFunction · 0.85

Tested by

no test coverage detected