| 6353 | } |
| 6354 | |
| 6355 | unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data_len, |
| 6356 | int *out_len) { |
| 6357 | if (!data || data_len <= 0 || !out_len) { |
| 6358 | return NULL; |
| 6359 | } |
| 6360 | |
| 6361 | size_t total = 0; |
| 6362 | unsigned char *decompressed = gzip_decompress(data, data_len, &total); |
| 6363 | if (!decompressed) { |
| 6364 | return NULL; |
| 6365 | } |
| 6366 | |
| 6367 | /* Parse tar: find entry starting with "codebase-memory-mcp" */ |
| 6368 | size_t pos = 0; |
| 6369 | while (pos + TAR_BLOCK_SIZE <= total) { |
| 6370 | const unsigned char *hdr = decompressed + pos; |
| 6371 | |
| 6372 | if (is_tar_end_of_archive(hdr)) { |
| 6373 | break; |
| 6374 | } |
| 6375 | |
| 6376 | char name[TAR_NAME_LEN] = {0}; |
| 6377 | memcpy(name, hdr, TAR_NAME_LEN - SKIP_ONE); |
| 6378 | char size_str[TAR_SIZE_LEN] = {0}; |
| 6379 | memcpy(size_str, hdr + TAR_SIZE_OFFSET, TAR_SIZE_LEN - SKIP_ONE); |
| 6380 | long file_size = strtol(size_str, NULL, OCTAL_BASE); |
| 6381 | char typeflag = (char)hdr[TAR_TYPE_OFFSET]; |
| 6382 | pos += TAR_BLOCK_SIZE; |
| 6383 | |
| 6384 | unsigned char *found = tar_try_extract_binary(hdr, typeflag, name, decompressed, pos, |
| 6385 | file_size, total, out_len); |
| 6386 | if (found) { |
| 6387 | free(decompressed); |
| 6388 | return found; |
| 6389 | } |
| 6390 | |
| 6391 | size_t blocks = ((size_t)file_size + TAR_BLOCK_MASK) / TAR_BLOCK_SIZE; |
| 6392 | pos += blocks * TAR_BLOCK_SIZE; |
| 6393 | } |
| 6394 | |
| 6395 | free(decompressed); |
| 6396 | return NULL; /* binary not found */ |
| 6397 | } |
| 6398 | |
| 6399 | /* ── Zip extraction (in-memory, replaces external unzip) ──────── */ |
| 6400 |
no test coverage detected