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

Source from the content-addressed store, hash-verified

2563}
2564
2565unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) {
2566 if (!data || data_len <= 0 || !out_len) {
2567 return NULL;
2568 }
2569 *out_len = 0;
2570
2571 int pos = 0;
2572 while (pos + ZIP_HDR_SZ <= data_len) {
2573 if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 ||
2574 data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) {
2575 break;
2576 }
2577
2578 uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD);
2579 uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP);
2580 uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP);
2581 uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN);
2582 uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN);
2583
2584 int header_end = pos + ZIP_HDR_SZ + name_len + extra_len;
2585 if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) {
2586 break;
2587 }
2588
2589 char fname[CLI_BUF_512] = {0};
2590 int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE
2591 ? name_len
2592 : (int)sizeof(fname) - CLI_SKIP_ONE;
2593 memcpy(fname, data + pos + 30, (size_t)fn_copy);
2594 fname[fn_copy] = '\0';
2595
2596 if (strstr(fname, "..")) {
2597 pos = header_end + (int)comp_size;
2598 continue;
2599 }
2600
2601 const char *basename = strrchr(fname, '/');
2602 basename = basename ? basename + CLI_SKIP_ONE : fname;
2603
2604 if (strcmp(basename, "codebase-memory-mcp") == 0 ||
2605 strcmp(basename, "codebase-memory-mcp.exe") == 0) {
2606 return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len);
2607 }
2608
2609 pos = header_end + (int)comp_size;
2610 }
2611
2612 return NULL;
2613}
2614
2615/* ── Index management ─────────────────────────────────────────── */
2616

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