| 9 | namespace Star { |
| 10 | |
| 11 | void 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 | |
| 48 | ByteArray compressData(ByteArray const& in, CompressionLevel compression) { |
| 49 | ByteArray out = ByteArray::withReserve(in.size()); |
no test coverage detected