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:6476–6524  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6474}
6475
6476unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) {
6477 if (!data || data_len <= 0 || !out_len) {
6478 return NULL;
6479 }
6480 *out_len = 0;
6481
6482 int pos = 0;
6483 while (pos + ZIP_HDR_SZ <= data_len) {
6484 if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 ||
6485 data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) {
6486 break;
6487 }
6488
6489 uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD);
6490 uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP);
6491 uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP);
6492 uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN);
6493 uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN);
6494
6495 int header_end = pos + ZIP_HDR_SZ + name_len + extra_len;
6496 if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) {
6497 break;
6498 }
6499
6500 char fname[CLI_BUF_512] = {0};
6501 int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE
6502 ? name_len
6503 : (int)sizeof(fname) - CLI_SKIP_ONE;
6504 memcpy(fname, data + pos + 30, (size_t)fn_copy);
6505 fname[fn_copy] = '\0';
6506
6507 if (strstr(fname, "..")) {
6508 pos = header_end + (int)comp_size;
6509 continue;
6510 }
6511
6512 const char *basename = strrchr(fname, '/');
6513 basename = basename ? basename + CLI_SKIP_ONE : fname;
6514
6515 if (strcmp(basename, "codebase-memory-mcp") == 0 ||
6516 strcmp(basename, "codebase-memory-mcp.exe") == 0) {
6517 return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len);
6518 }
6519
6520 pos = header_end + (int)comp_size;
6521 }
6522
6523 return NULL;
6524}
6525
6526#endif /* CBM_CLI_ENABLE_TEST_API — tar.gz / zip extraction */
6527

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