Decompress a single zip entry (stored or deflated). Returns malloc'd buffer * or NULL on failure. *out_len receives the decompressed size. */
| 6428 | /* Decompress a single zip entry (stored or deflated). Returns malloc'd buffer |
| 6429 | * or NULL on failure. *out_len receives the decompressed size. */ |
| 6430 | static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t method, |
| 6431 | size_t comp_size, size_t uncomp_size, int *out_len) { |
| 6432 | if (method == ZIP_STORED) { |
| 6433 | if (comp_size > ZIP_MAX_UNCOMP) { |
| 6434 | return NULL; |
| 6435 | } |
| 6436 | unsigned char *out = malloc(comp_size); |
| 6437 | if (!out) { |
| 6438 | return NULL; |
| 6439 | } |
| 6440 | memcpy(out, file_data, comp_size); |
| 6441 | *out_len = (int)comp_size; |
| 6442 | return out; |
| 6443 | } |
| 6444 | if (method == ZIP_DEFLATE) { |
| 6445 | if (uncomp_size > ZIP_MAX_UNCOMP) { |
| 6446 | return NULL; |
| 6447 | } |
| 6448 | if (comp_size > UINT_MAX || uncomp_size > UINT_MAX) { |
| 6449 | return NULL; |
| 6450 | } |
| 6451 | unsigned char *out = malloc(uncomp_size); |
| 6452 | if (!out) { |
| 6453 | return NULL; |
| 6454 | } |
| 6455 | z_stream strm = {0}; |
| 6456 | strm.next_in = (unsigned char *)file_data; |
| 6457 | strm.avail_in = (uInt)comp_size; |
| 6458 | strm.next_out = out; |
| 6459 | strm.avail_out = (uInt)uncomp_size; |
| 6460 | if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) { |
| 6461 | free(out); |
| 6462 | return NULL; |
| 6463 | } |
| 6464 | int ret = inflate(&strm, Z_FINISH); |
| 6465 | inflateEnd(&strm); |
| 6466 | if (ret != Z_STREAM_END) { |
| 6467 | free(out); |
| 6468 | return NULL; |
| 6469 | } |
| 6470 | *out_len = (int)strm.total_out; |
| 6471 | return out; |
| 6472 | } |
| 6473 | return NULL; /* unknown method */ |
| 6474 | } |
| 6475 | |
| 6476 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 6477 | if (!data || data_len <= 0 || !out_len) { |
no outgoing calls
no test coverage detected