| 7141 | } |
| 7142 | |
| 7143 | unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data_len, |
| 7144 | int *out_len) { |
| 7145 | if (!data || data_len <= 0 || !out_len) { |
| 7146 | return NULL; |
| 7147 | } |
| 7148 | |
| 7149 | size_t total = 0; |
| 7150 | unsigned char *decompressed = gzip_decompress(data, data_len, &total); |
| 7151 | if (!decompressed) { |
| 7152 | return NULL; |
| 7153 | } |
| 7154 | |
| 7155 | /* Parse tar: find entry starting with "codebase-memory-mcp" */ |
| 7156 | size_t pos = 0; |
| 7157 | while (pos + TAR_BLOCK_SIZE <= total) { |
| 7158 | const unsigned char *hdr = decompressed + pos; |
| 7159 | |
| 7160 | if (is_tar_end_of_archive(hdr)) { |
| 7161 | break; |
| 7162 | } |
| 7163 | |
| 7164 | char name[TAR_NAME_LEN] = {0}; |
| 7165 | memcpy(name, hdr, TAR_NAME_LEN - SKIP_ONE); |
| 7166 | char size_str[TAR_SIZE_LEN] = {0}; |
| 7167 | memcpy(size_str, hdr + TAR_SIZE_OFFSET, TAR_SIZE_LEN - SKIP_ONE); |
| 7168 | long file_size = strtol(size_str, NULL, OCTAL_BASE); |
| 7169 | char typeflag = (char)hdr[TAR_TYPE_OFFSET]; |
| 7170 | pos += TAR_BLOCK_SIZE; |
| 7171 | |
| 7172 | unsigned char *found = tar_try_extract_binary(hdr, typeflag, name, decompressed, pos, |
| 7173 | file_size, total, out_len); |
| 7174 | if (found) { |
| 7175 | free(decompressed); |
| 7176 | return found; |
| 7177 | } |
| 7178 | |
| 7179 | size_t blocks = ((size_t)file_size + TAR_BLOCK_MASK) / TAR_BLOCK_SIZE; |
| 7180 | pos += blocks * TAR_BLOCK_SIZE; |
| 7181 | } |
| 7182 | |
| 7183 | free(decompressed); |
| 7184 | return NULL; /* binary not found */ |
| 7185 | } |
| 7186 | |
| 7187 | /* ── Zip extraction (in-memory, replaces external unzip) ──────── */ |
| 7188 |
no test coverage detected