18 intptr_t* output_length) {
22 ASSERT(output_length !=
nullptr);
24 const intptr_t kChunkSize = 256 * 1024;
27 intptr_t output_capacity = input_len * 2;
28 if (output_capacity < kChunkSize) {
29 output_capacity = kChunkSize;
31 *
output =
reinterpret_cast<uint8_t*
>(
malloc(output_capacity));
33 uint8_t* chunk_out =
reinterpret_cast<uint8_t*
>(
malloc(kChunkSize));
39 strm.next_in = Z_NULL;
40 int ret = inflateInit2(&strm, 32 + MAX_WBITS);
43 intptr_t input_cursor = 0;
44 intptr_t output_cursor = 0;
47 intptr_t size_in = input_len - input_cursor;
48 if (size_in > kChunkSize) {
51 strm.avail_in = size_in;
52 strm.next_in =
const_cast<uint8_t*
>(&input[input_cursor]);
57 strm.avail_out = kChunkSize;
58 strm.next_out = &chunk_out[0];
60 ret = inflate(&strm, Z_SYNC_FLUSH);
62 ASSERT((ret == Z_STREAM_END) || (ret == Z_OK));
64 intptr_t size_out = kChunkSize - strm.avail_out;
65 if (size_out > (output_capacity - output_cursor)) {
67 ASSERT(size_out <= (output_capacity - output_cursor));
71 memmove(&((*
output)[output_cursor]), &chunk_out[0], size_out);
72 output_cursor += size_out;
73 }
while (strm.avail_out == 0);
76 input_cursor += size_in;
79 }
while (ret != Z_STREAM_END);
83 *output_length = output_cursor;
void Decompress(const uint8_t *input, intptr_t input_len, uint8_t **output, intptr_t *output_length)
void * malloc(size_t size)
void * realloc(void *ptr, size_t size)