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

Function cbm_extract_binary_from_zip

src/cli/cli.c:7264–7312  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7262}
7263
7264unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) {
7265 if (!data || data_len <= 0 || !out_len) {
7266 return NULL;
7267 }
7268 *out_len = 0;
7269
7270 int pos = 0;
7271 while (pos + ZIP_HDR_SZ <= data_len) {
7272 if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 ||
7273 data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) {
7274 break;
7275 }
7276
7277 uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD);
7278 uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP);
7279 uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP);
7280 uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN);
7281 uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN);
7282
7283 int header_end = pos + ZIP_HDR_SZ + name_len + extra_len;
7284 if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) {
7285 break;
7286 }
7287
7288 char fname[CLI_BUF_512] = {0};
7289 int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE
7290 ? name_len
7291 : (int)sizeof(fname) - CLI_SKIP_ONE;
7292 memcpy(fname, data + pos + 30, (size_t)fn_copy);
7293 fname[fn_copy] = '\0';
7294
7295 if (strstr(fname, "..")) {
7296 pos = header_end + (int)comp_size;
7297 continue;
7298 }
7299
7300 const char *basename = strrchr(fname, '/');
7301 basename = basename ? basename + CLI_SKIP_ONE : fname;
7302
7303 if (strcmp(basename, "codebase-memory-mcp") == 0 ||
7304 strcmp(basename, "codebase-memory-mcp.exe") == 0) {
7305 return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len);
7306 }
7307
7308 pos = header_end + (int)comp_size;
7309 }
7310
7311 return NULL;
7312}
7313
7314#endif /* CBM_CLI_ENABLE_TEST_API — tar.gz / zip extraction */
7315

Callers 2

test_cli.cFile · 0.85

Calls 3

zip_read_u16leFunction · 0.85
zip_read_u32leFunction · 0.85
zip_extract_entryFunction · 0.85

Tested by

no test coverage detected