| 6310 | } |
| 6311 | |
| 6312 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 6313 | if (!data || data_len <= 0 || !out_len) { |
| 6314 | return NULL; |
| 6315 | } |
| 6316 | *out_len = 0; |
| 6317 | |
| 6318 | int pos = 0; |
| 6319 | while (pos + ZIP_HDR_SZ <= data_len) { |
| 6320 | if (data[pos] != ZIP_SIG_0 || data[pos + CLI_SKIP_ONE] != ZIP_SIG_1 || |
| 6321 | data[pos + CLI_PAIR_LEN] != ZIP_SIG_2 || data[pos + CLI_JSON_INDENT] != ZIP_SIG_3) { |
| 6322 | break; |
| 6323 | } |
| 6324 | |
| 6325 | uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD); |
| 6326 | uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP); |
| 6327 | uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP); |
| 6328 | uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN); |
| 6329 | uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN); |
| 6330 | |
| 6331 | int header_end = pos + ZIP_HDR_SZ + name_len + extra_len; |
| 6332 | if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) { |
| 6333 | break; |
| 6334 | } |
| 6335 | |
| 6336 | char fname[CLI_BUF_512] = {0}; |
| 6337 | int fn_copy = name_len < (int)sizeof(fname) - CLI_SKIP_ONE |
| 6338 | ? name_len |
| 6339 | : (int)sizeof(fname) - CLI_SKIP_ONE; |
| 6340 | memcpy(fname, data + pos + 30, (size_t)fn_copy); |
| 6341 | fname[fn_copy] = '\0'; |
| 6342 | |
| 6343 | if (strstr(fname, "..")) { |
| 6344 | pos = header_end + (int)comp_size; |
| 6345 | continue; |
| 6346 | } |
| 6347 | |
| 6348 | const char *basename = strrchr(fname, '/'); |
| 6349 | basename = basename ? basename + CLI_SKIP_ONE : fname; |
| 6350 | |
| 6351 | if (strcmp(basename, "codebase-memory-mcp") == 0 || |
| 6352 | strcmp(basename, "codebase-memory-mcp.exe") == 0) { |
| 6353 | return zip_extract_entry(data + header_end, method, comp_size, uncomp_size, out_len); |
| 6354 | } |
| 6355 | |
| 6356 | pos = header_end + (int)comp_size; |
| 6357 | } |
| 6358 | |
| 6359 | return NULL; |
| 6360 | } |
| 6361 | |
| 6362 | #endif /* CBM_CLI_ENABLE_TEST_API — tar.gz / zip extraction */ |
| 6363 |
no test coverage detected