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

Source from the content-addressed store, hash-verified

6254}
6255
6256unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) {
6257 if (!data || data_len <= 0 || !out_len) {
6258 return NULL;
6259 }
6260 *out_len = 0;
6261
6262 int pos = 0;
6263 while (pos + ZIP_HDR_SZ <= data_len) {
6264 if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 ||
6265 data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) {
6266 break;
6267 }
6268
6269 uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD);
6270 uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP);
6271 uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP);
6272 uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN);
6273 uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN);
6274
6275 int header_end = pos + ZIP_HDR_SZ + name_len + extra_len;
6276 if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) {
6277 break;
6278 }
6279
6280 char fname[CLI_BUF_512] = {0};
6281 int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE
6282 ? name_len
6283 : (int)sizeof(fname) - CLI_SKIP_ONE;
6284 memcpy(fname, data + pos + 30, (size_t)fn_copy);
6285 fname[fn_copy] = '\0';
6286
6287 if (strstr(fname, "..")) {
6288 pos = header_end + (int)comp_size;
6289 continue;
6290 }
6291
6292 const char *basename = strrchr(fname, '/');
6293 basename = basename ? basename + CLI_SKIP_ONE : fname;
6294
6295 if (strcmp(basename, "codebase-memory-mcp") == 0 ||
6296 strcmp(basename, "codebase-memory-mcp.exe") == 0) {
6297 return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len);
6298 }
6299
6300 pos = header_end + (int)comp_size;
6301 }
6302
6303 return NULL;
6304}
6305
6306#endif /* CBM_CLI_ENABLE_TEST_API — tar.gz / zip extraction */
6307

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