MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / gzip_decompress

Function gzip_decompress

src/cli/cli.c:6040–6097  ·  view source on GitHub ↗

Decompress gzip data into a malloc'd buffer. Returns NULL on failure. * *out_total receives the decompressed size. Caller must free the result. */

Source from the content-addressed store, hash-verified

6038/* Decompress gzip data into a malloc'd buffer. Returns NULL on failure.
6039 * *out_total receives the decompressed size. Caller must free the result. */
6040static unsigned char *gzip_decompress(const unsigned char *data, int data_len, size_t *out_total) {
6041 z_stream strm = {0};
6042 unsigned char *mutable_data;
6043 memcpy(&mutable_data, &data, sizeof(data));
6044 strm.next_in = mutable_data;
6045 strm.avail_in = (unsigned int)data_len;
6046
6047 if (inflateInit2(&strm, 16 + MAX_WBITS) != Z_OK) {
6048 return NULL;
6049 }
6050
6051 size_t buf_cap = (size_t)data_len * DECOMP_FACTOR;
6052 if (buf_cap < CLI_BUF_4K) {
6053 buf_cap = CLI_BUF_4K;
6054 }
6055 if (buf_cap > DECOMPRESS_MAX_BYTES) {
6056 buf_cap = DECOMPRESS_MAX_BYTES;
6057 }
6058 unsigned char *decompressed = malloc(buf_cap);
6059 if (!decompressed) {
6060 inflateEnd(&strm);
6061 return NULL;
6062 }
6063
6064 size_t total = 0;
6065 int ret;
6066 do {
6067 if (total >= buf_cap) {
6068 size_t new_cap = buf_cap * GROWTH_FACTOR;
6069 if (new_cap > DECOMPRESS_MAX_BYTES) {
6070 free(decompressed);
6071 inflateEnd(&strm);
6072 return NULL;
6073 }
6074 unsigned char *nb = realloc(decompressed, new_cap);
6075 if (!nb) {
6076 free(decompressed);
6077 inflateEnd(&strm);
6078 return NULL;
6079 }
6080 decompressed = nb;
6081 buf_cap = new_cap;
6082 }
6083 strm.next_out = decompressed + total;
6084 strm.avail_out = (unsigned int)(buf_cap - total);
6085 ret = inflate(&strm, Z_NO_FLUSH);
6086 total = buf_cap - strm.avail_out;
6087 } while (ret == Z_OK);
6088
6089 inflateEnd(&strm);
6090
6091 if (ret != Z_STREAM_END) {
6092 free(decompressed);
6093 return NULL;
6094 }
6095 *out_total = total;
6096 return decompressed;
6097}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected