| 6331 | } |
| 6332 | |
| 6333 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 6334 | if (!data || data_len <= 0 || !out_len) { |
| 6335 | return NULL; |
| 6336 | } |
| 6337 | *out_len = 0; |
| 6338 | |
| 6339 | int pos = 0; |
| 6340 | while (pos + ZIP_HDR_SZ <= data_len) { |
| 6341 | if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 || |
| 6342 | data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) { |
| 6343 | break; |
| 6344 | } |
| 6345 | |
| 6346 | uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD); |
| 6347 | uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP); |
| 6348 | uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP); |
| 6349 | uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN); |
| 6350 | uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN); |
| 6351 | |
| 6352 | int header_end = pos + ZIP_HDR_SZ + name_len + extra_len; |
| 6353 | if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) { |
| 6354 | break; |
| 6355 | } |
| 6356 | |
| 6357 | char fname[CLI_BUF_512] = {0}; |
| 6358 | int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE |
| 6359 | ? name_len |
| 6360 | : (int)sizeof(fname) - CLI_SKIP_ONE; |
| 6361 | memcpy(fname, data + pos + 30, (size_t)fn_copy); |
| 6362 | fname[fn_copy] = '\0'; |
| 6363 | |
| 6364 | if (strstr(fname, "..")) { |
| 6365 | pos = header_end + (int)comp_size; |
| 6366 | continue; |
| 6367 | } |
| 6368 | |
| 6369 | const char *basename = strrchr(fname, '/'); |
| 6370 | basename = basename ? basename + CLI_SKIP_ONE : fname; |
| 6371 | |
| 6372 | if (strcmp(basename, "codebase-memory-mcp") == 0 || |
| 6373 | strcmp(basename, "codebase-memory-mcp.exe") == 0) { |
| 6374 | return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len); |
| 6375 | } |
| 6376 | |
| 6377 | pos = header_end + (int)comp_size; |
| 6378 | } |
| 6379 | |
| 6380 | return NULL; |
| 6381 | } |
| 6382 | |
| 6383 | #endif /* CBM_CLI_ENABLE_TEST_API — tar.gz / zip extraction */ |
| 6384 |
no test coverage detected