| 6127 | } |
| 6128 | |
| 6129 | unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data_len, |
| 6130 | int *out_len) { |
| 6131 | if (!data || data_len <= 0 || !out_len) { |
| 6132 | return NULL; |
| 6133 | } |
| 6134 | |
| 6135 | size_t total = 0; |
| 6136 | unsigned char *decompressed = gzip_decompress(data, data_len, &total); |
| 6137 | if (!decompressed) { |
| 6138 | return NULL; |
| 6139 | } |
| 6140 | |
| 6141 | /* Parse tar: find entry starting with "codebase-memory-mcp" */ |
| 6142 | size_t pos = 0; |
| 6143 | while (pos + TAR_BLOCK_SIZE <= total) { |
| 6144 | const unsigned char *hdr = decompressed + pos; |
| 6145 | |
| 6146 | if (is_tar_end_of_archive(hdr)) { |
| 6147 | break; |
| 6148 | } |
| 6149 | |
| 6150 | char name[TAR_NAME_LEN] = {0}; |
| 6151 | memcpy(name, hdr, TAR_NAME_LEN - SKIP_ONE); |
| 6152 | char size_str[TAR_SIZE_LEN] = {0}; |
| 6153 | memcpy(size_str, hdr + TAR_SIZE_OFFSET, TAR_SIZE_LEN - SKIP_ONE); |
| 6154 | long file_size = strtol(size_str, NULL, OCTAL_BASE); |
| 6155 | char typeflag = (char)hdr[TAR_TYPE_OFFSET]; |
| 6156 | pos += TAR_BLOCK_SIZE; |
| 6157 | |
| 6158 | unsigned char *found = tar_try_extract_binary(hdr, typeflag, name, decompressed, pos, |
| 6159 | file_size, total, out_len); |
| 6160 | if (found) { |
| 6161 | free(decompressed); |
| 6162 | return found; |
| 6163 | } |
| 6164 | |
| 6165 | size_t blocks = ((size_t)file_size + TAR_BLOCK_MASK) / TAR_BLOCK_SIZE; |
| 6166 | pos += blocks * TAR_BLOCK_SIZE; |
| 6167 | } |
| 6168 | |
| 6169 | free(decompressed); |
| 6170 | return NULL; /* binary not found */ |
| 6171 | } |
| 6172 | |
| 6173 | /* ── Zip extraction (in-memory, replaces external unzip) ──────── */ |
| 6174 |
no test coverage detected