| 52 | } |
| 53 | |
| 54 | void uncompressData(const char* in, size_t inLen, ByteArray& out, size_t limit) { |
| 55 | out.clear(); |
| 56 | |
| 57 | if (!inLen) |
| 58 | return; |
| 59 | |
| 60 | const size_t BUFSIZE = 32 * 1024; |
| 61 | auto tempBuffer = std::make_unique<unsigned char[]>(BUFSIZE); |
| 62 | |
| 63 | z_stream strm{}; |
| 64 | strm.zalloc = Z_NULL; |
| 65 | strm.zfree = Z_NULL; |
| 66 | strm.opaque = Z_NULL; |
| 67 | int inflate_res = inflateInit(&strm); |
| 68 | if (inflate_res != Z_OK) |
| 69 | throw IOException(strf("Failed to initialise inflate ({})", inflate_res)); |
| 70 | |
| 71 | strm.next_in = (unsigned char*)in; |
| 72 | strm.avail_in = inLen; |
| 73 | strm.next_out = tempBuffer.get(); |
| 74 | strm.avail_out = BUFSIZE; |
| 75 | |
| 76 | while (inflate_res == Z_OK || inflate_res == Z_BUF_ERROR) { |
| 77 | inflate_res = inflate(&strm, Z_FINISH); |
| 78 | if (strm.avail_out == 0) { |
| 79 | out.append((char const*)tempBuffer.get(), BUFSIZE); |
| 80 | strm.next_out = tempBuffer.get(); |
| 81 | strm.avail_out = BUFSIZE; |
| 82 | if (limit && out.size() >= limit) { |
| 83 | inflateEnd(&strm); |
| 84 | throw IOException(strf("hit uncompressData limit of {} bytes", limit)); |
| 85 | break; |
| 86 | } |
| 87 | } else if (inflate_res == Z_BUF_ERROR) { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | inflateEnd(&strm); |
| 92 | |
| 93 | if (inflate_res != Z_STREAM_END) |
| 94 | throw IOException(strf("Internal error in uncompressData, inflate_res is {}", inflate_res)); |
| 95 | |
| 96 | out.append((char const*)tempBuffer.get(), BUFSIZE - strm.avail_out); |
| 97 | } |
| 98 | |
| 99 | ByteArray uncompressData(const char* in, size_t inLen, size_t limit) { |
| 100 | ByteArray out = ByteArray::withReserve(inLen); |
no test coverage detected