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