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