Decompress a single zip entry (stored or deflated). Returns malloc'd buffer * or NULL on failure. *out_len receives the decompressed size. */
| 6202 | /* Decompress a single zip entry (stored or deflated). Returns malloc'd buffer |
| 6203 | * or NULL on failure. *out_len receives the decompressed size. */ |
| 6204 | static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t method, |
| 6205 | size_t comp_size, size_t uncomp_size, int *out_len) { |
| 6206 | if (method == ZIP_STORED) { |
| 6207 | if (comp_size > ZIP_MAX_UNCOMP) { |
| 6208 | return NULL; |
| 6209 | } |
| 6210 | unsigned char *out = malloc(comp_size); |
| 6211 | if (!out) { |
| 6212 | return NULL; |
| 6213 | } |
| 6214 | memcpy(out, file_data, comp_size); |
| 6215 | *out_len = (int)comp_size; |
| 6216 | return out; |
| 6217 | } |
| 6218 | if (method == ZIP_DEFLATE) { |
| 6219 | if (uncomp_size > ZIP_MAX_UNCOMP) { |
| 6220 | return NULL; |
| 6221 | } |
| 6222 | if (comp_size > UINT_MAX || uncomp_size > UINT_MAX) { |
| 6223 | return NULL; |
| 6224 | } |
| 6225 | unsigned char *out = malloc(uncomp_size); |
| 6226 | if (!out) { |
| 6227 | return NULL; |
| 6228 | } |
| 6229 | z_stream strm = {0}; |
| 6230 | strm.next_in = (unsigned char *)file_data; |
| 6231 | strm.avail_in = (uInt)comp_size; |
| 6232 | strm.next_out = out; |
| 6233 | strm.avail_out = (uInt)uncomp_size; |
| 6234 | if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) { |
| 6235 | free(out); |
| 6236 | return NULL; |
| 6237 | } |
| 6238 | int ret = inflate(&strm, Z_FINISH); |
| 6239 | inflateEnd(&strm); |
| 6240 | if (ret != Z_STREAM_END) { |
| 6241 | free(out); |
| 6242 | return NULL; |
| 6243 | } |
| 6244 | *out_len = (int)strm.total_out; |
| 6245 | return out; |
| 6246 | } |
| 6247 | return NULL; /* unknown method */ |
| 6248 | } |
| 6249 | |
| 6250 | unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len) { |
| 6251 | if (!data || data_len <= 0 || !out_len) { |
no outgoing calls
no test coverage detected