Decompress a single zip entry (stored or deflated). Returns malloc'd buffer * or NULL on failure. *out_len receives the decompressed size. */
| 6264 | /* Decompress a single zip entry (stored or deflated). Returns malloc'd buffer |
| 6265 | * or NULL on failure. *out_len receives the decompressed size. */ |
| 6266 | static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t method, |
| 6267 | size_t comp_size, size_t uncomp_size, int *out_len) { |
| 6268 | if (method == ZIP_STORED) { |
| 6269 | if (comp_size > ZIP_MAX_UNCOMP) { |
| 6270 | return NULL; |
| 6271 | } |
| 6272 | unsigned char *out = malloc(comp_size); |
| 6273 | if (!out) { |
| 6274 | return NULL; |
| 6275 | } |
| 6276 | memcpy(out, file_data, comp_size); |
| 6277 | *out_len = (int)comp_size; |
| 6278 | return out; |
| 6279 | } |
| 6280 | if (method == ZIP_DEFLATE) { |
| 6281 | if (uncomp_size > ZIP_MAX_UNCOMP) { |
| 6282 | return NULL; |
| 6283 | } |
| 6284 | if (comp_size > UINT_MAX || uncomp_size > UINT_MAX) { |
| 6285 | return NULL; |
| 6286 | } |
| 6287 | unsigned char *out = malloc(uncomp_size); |
| 6288 | if (!out) { |
| 6289 | return NULL; |
| 6290 | } |
| 6291 | z_stream strm = {0}; |
| 6292 | strm.next_in = (unsigned char *)file_data; |
| 6293 | strm.avail_in = (uInt)comp_size; |
| 6294 | strm.next_out = out; |
| 6295 | strm.avail_out = (uInt)uncomp_size; |
| 6296 | if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) { |
| 6297 | free(out); |
| 6298 | return NULL; |
| 6299 | } |
| 6300 | int ret = inflate(&strm, Z_FINISH); |
| 6301 | inflateEnd(&strm); |
| 6302 | if (ret != Z_STREAM_END) { |
| 6303 | free(out); |
| 6304 | return NULL; |
| 6305 | } |
| 6306 | *out_len = (int)strm.total_out; |
| 6307 | return out; |
| 6308 | } |
| 6309 | return NULL; /* unknown method */ |
| 6310 | } |
| 6311 | |
| 6312 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 6313 | if (!data || data_len <= 0 || !out_len) { |
no outgoing calls
no test coverage detected