Decompress a single zip entry (stored or deflated). Returns malloc'd buffer * or NULL on failure. *out_len receives the decompressed size. */
| 2517 | /* Decompress a single zip entry (stored or deflated). Returns malloc'd buffer |
| 2518 | * or NULL on failure. *out_len receives the decompressed size. */ |
| 2519 | static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t method, |
| 2520 | size_t comp_size, size_t uncomp_size, int *out_len) { |
| 2521 | if (method == ZIP_STORED) { |
| 2522 | if (comp_size > ZIP_MAX_UNCOMP) { |
| 2523 | return NULL; |
| 2524 | } |
| 2525 | unsigned char *out = malloc(comp_size); |
| 2526 | if (!out) { |
| 2527 | return NULL; |
| 2528 | } |
| 2529 | memcpy(out, file_data, comp_size); |
| 2530 | *out_len = (int)comp_size; |
| 2531 | return out; |
| 2532 | } |
| 2533 | if (method == ZIP_DEFLATE) { |
| 2534 | if (uncomp_size > ZIP_MAX_UNCOMP) { |
| 2535 | return NULL; |
| 2536 | } |
| 2537 | if (comp_size > UINT_MAX || uncomp_size > UINT_MAX) { |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | unsigned char *out = malloc(uncomp_size); |
| 2541 | if (!out) { |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | z_stream strm = {0}; |
| 2545 | strm.next_in = (unsigned char *)file_data; |
| 2546 | strm.avail_in = (uInt)comp_size; |
| 2547 | strm.next_out = out; |
| 2548 | strm.avail_out = (uInt)uncomp_size; |
| 2549 | if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) { |
| 2550 | free(out); |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | int ret = inflate(&strm, Z_FINISH); |
| 2554 | inflateEnd(&strm); |
| 2555 | if (ret != Z_STREAM_END) { |
| 2556 | free(out); |
| 2557 | return NULL; |
| 2558 | } |
| 2559 | *out_len = (int)strm.total_out; |
| 2560 | return out; |
| 2561 | } |
| 2562 | return NULL; /* unknown method */ |
| 2563 | } |
| 2564 | |
| 2565 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 2566 | if (!data || data_len <= 0 || !out_len) { |
no outgoing calls
no test coverage detected