xref: /openbmc/linux/tools/perf/util/zlib.c (revision b746a1a2860f4a918f32d10dc569115d282aaf2f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
3e92ce12eSNamhyung Kim #include <stdio.h>
48520a98dSArnaldo Carvalho de Melo #include <string.h>
5e92ce12eSNamhyung Kim #include <unistd.h>
6e92ce12eSNamhyung Kim #include <sys/stat.h>
7e92ce12eSNamhyung Kim #include <sys/mman.h>
8e92ce12eSNamhyung Kim #include <zlib.h>
98b42b7e5SJiri Olsa #include <linux/compiler.h>
10*fb71c86cSArnaldo Carvalho de Melo #include <internal/lib.h>
11e92ce12eSNamhyung Kim 
12611f0afeSArnaldo Carvalho de Melo #include "util/compress.h"
13e92ce12eSNamhyung Kim 
14e92ce12eSNamhyung Kim #define CHUNK_SIZE  16384
15e92ce12eSNamhyung Kim 
gzip_decompress_to_file(const char * input,int output_fd)16e92ce12eSNamhyung Kim int gzip_decompress_to_file(const char *input, int output_fd)
17e92ce12eSNamhyung Kim {
18e92ce12eSNamhyung Kim 	int ret = Z_STREAM_ERROR;
19e92ce12eSNamhyung Kim 	int input_fd;
20e92ce12eSNamhyung Kim 	void *ptr;
21e92ce12eSNamhyung Kim 	int len;
22e92ce12eSNamhyung Kim 	struct stat stbuf;
23e92ce12eSNamhyung Kim 	unsigned char buf[CHUNK_SIZE];
24e92ce12eSNamhyung Kim 	z_stream zs = {
25e92ce12eSNamhyung Kim 		.zalloc		= Z_NULL,
26e92ce12eSNamhyung Kim 		.zfree		= Z_NULL,
27e92ce12eSNamhyung Kim 		.opaque		= Z_NULL,
28e92ce12eSNamhyung Kim 		.avail_in	= 0,
29e92ce12eSNamhyung Kim 		.next_in	= Z_NULL,
30e92ce12eSNamhyung Kim 	};
31e92ce12eSNamhyung Kim 
32e92ce12eSNamhyung Kim 	input_fd = open(input, O_RDONLY);
33e92ce12eSNamhyung Kim 	if (input_fd < 0)
34e92ce12eSNamhyung Kim 		return -1;
35e92ce12eSNamhyung Kim 
36e92ce12eSNamhyung Kim 	if (fstat(input_fd, &stbuf) < 0)
37e92ce12eSNamhyung Kim 		goto out_close;
38e92ce12eSNamhyung Kim 
39e92ce12eSNamhyung Kim 	ptr = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
40e92ce12eSNamhyung Kim 	if (ptr == MAP_FAILED)
41e92ce12eSNamhyung Kim 		goto out_close;
42e92ce12eSNamhyung Kim 
43e92ce12eSNamhyung Kim 	if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK)
44e92ce12eSNamhyung Kim 		goto out_unmap;
45e92ce12eSNamhyung Kim 
46e92ce12eSNamhyung Kim 	zs.next_in = ptr;
47e92ce12eSNamhyung Kim 	zs.avail_in = stbuf.st_size;
48e92ce12eSNamhyung Kim 
49e92ce12eSNamhyung Kim 	do {
50e92ce12eSNamhyung Kim 		zs.next_out = buf;
51e92ce12eSNamhyung Kim 		zs.avail_out = CHUNK_SIZE;
52e92ce12eSNamhyung Kim 
53e92ce12eSNamhyung Kim 		ret = inflate(&zs, Z_NO_FLUSH);
54e92ce12eSNamhyung Kim 		switch (ret) {
55e92ce12eSNamhyung Kim 		case Z_NEED_DICT:
56e92ce12eSNamhyung Kim 			ret = Z_DATA_ERROR;
57e92ce12eSNamhyung Kim 			/* fall through */
58e92ce12eSNamhyung Kim 		case Z_DATA_ERROR:
59e92ce12eSNamhyung Kim 		case Z_MEM_ERROR:
60e92ce12eSNamhyung Kim 			goto out;
61e92ce12eSNamhyung Kim 		default:
62e92ce12eSNamhyung Kim 			break;
63e92ce12eSNamhyung Kim 		}
64e92ce12eSNamhyung Kim 
65e92ce12eSNamhyung Kim 		len = CHUNK_SIZE - zs.avail_out;
66e92ce12eSNamhyung Kim 		if (writen(output_fd, buf, len) != len) {
67e92ce12eSNamhyung Kim 			ret = Z_DATA_ERROR;
68e92ce12eSNamhyung Kim 			goto out;
69e92ce12eSNamhyung Kim 		}
70e92ce12eSNamhyung Kim 
71e92ce12eSNamhyung Kim 	} while (ret != Z_STREAM_END);
72e92ce12eSNamhyung Kim 
73e92ce12eSNamhyung Kim out:
74e92ce12eSNamhyung Kim 	inflateEnd(&zs);
75e92ce12eSNamhyung Kim out_unmap:
76e92ce12eSNamhyung Kim 	munmap(ptr, stbuf.st_size);
77e92ce12eSNamhyung Kim out_close:
78e92ce12eSNamhyung Kim 	close(input_fd);
79e92ce12eSNamhyung Kim 
80e92ce12eSNamhyung Kim 	return ret == Z_STREAM_END ? 0 : -1;
81e92ce12eSNamhyung Kim }
828b42b7e5SJiri Olsa 
gzip_is_compressed(const char * input)8388c74dc7SJiri Olsa bool gzip_is_compressed(const char *input)
848b42b7e5SJiri Olsa {
8588c74dc7SJiri Olsa 	int fd = open(input, O_RDONLY);
8688c74dc7SJiri Olsa 	const uint8_t magic[2] = { 0x1f, 0x8b };
8788c74dc7SJiri Olsa 	char buf[2] = { 0 };
8888c74dc7SJiri Olsa 	ssize_t rc;
8988c74dc7SJiri Olsa 
9088c74dc7SJiri Olsa 	if (fd < 0)
9188c74dc7SJiri Olsa 		return -1;
9288c74dc7SJiri Olsa 
9388c74dc7SJiri Olsa 	rc = read(fd, buf, sizeof(buf));
9488c74dc7SJiri Olsa 	close(fd);
9588c74dc7SJiri Olsa 	return rc == sizeof(buf) ?
9688c74dc7SJiri Olsa 	       memcmp(buf, magic, sizeof(buf)) == 0 : false;
978b42b7e5SJiri Olsa }
98