| 6189 | } |
| 6190 | |
| 6191 | unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data_len, |
| 6192 | int *out_len) { |
| 6193 | if (!data || data_len <= 0 || !out_len) { |
| 6194 | return NULL; |
| 6195 | } |
| 6196 | |
| 6197 | size_t total = 0; |
| 6198 | unsigned char *decompressed = gzip_decompress(data, data_len, &total); |
| 6199 | if (!decompressed) { |
| 6200 | return NULL; |
| 6201 | } |
| 6202 | |
| 6203 | /* Parse tar: find entry starting with "codebase-memory-mcp" */ |
| 6204 | size_t pos = 0; |
| 6205 | while (pos + TAR_BLOCK_SIZE <= total) { |
| 6206 | const unsigned char *hdr = decompressed + pos; |
| 6207 | |
| 6208 | if (is_tar_end_of_archive(hdr)) { |
| 6209 | break; |
| 6210 | } |
| 6211 | |
| 6212 | char name[TAR_NAME_LEN] = {0}; |
| 6213 | memcpy(name, hdr, TAR_NAME_LEN - SKIP_ONE); |
| 6214 | char size_str[TAR_SIZE_LEN] = {0}; |
| 6215 | memcpy(size_str, hdr + TAR_SIZE_OFFSET, TAR_SIZE_LEN - SKIP_ONE); |
| 6216 | long file_size = strtol(size_str, NULL, OCTAL_BASE); |
| 6217 | char typeflag = (char)hdr[TAR_TYPE_OFFSET]; |
| 6218 | pos += TAR_BLOCK_SIZE; |
| 6219 | |
| 6220 | unsigned char *found = tar_try_extract_binary(hdr, typeflag, name, decompressed, pos, |
| 6221 | file_size, total, out_len); |
| 6222 | if (found) { |
| 6223 | free(decompressed); |
| 6224 | return found; |
| 6225 | } |
| 6226 | |
| 6227 | size_t blocks = ((size_t)file_size + TAR_BLOCK_MASK) / TAR_BLOCK_SIZE; |
| 6228 | pos += blocks * TAR_BLOCK_SIZE; |
| 6229 | } |
| 6230 | |
| 6231 | free(decompressed); |
| 6232 | return NULL; /* binary not found */ |
| 6233 | } |
| 6234 | |
| 6235 | /* ── Zip extraction (in-memory, replaces external unzip) ──────── */ |
| 6236 |
no test coverage detected