1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/compiler.h>
10 #include <internal/lib.h>
12 #include "util/compress.h"
14 #define CHUNK_SIZE 16384
16 int gzip_decompress_to_file(const char *input, int output_fd)
18 int ret = Z_STREAM_ERROR;
23 unsigned char buf[CHUNK_SIZE];
32 input_fd = open(input, O_RDONLY);
36 if (fstat(input_fd, &stbuf) < 0)
39 ptr = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
40 if (ptr == MAP_FAILED)
43 if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK)
47 zs.avail_in = stbuf.st_size;
51 zs.avail_out = CHUNK_SIZE;
53 ret = inflate(&zs, Z_NO_FLUSH);
65 len = CHUNK_SIZE - zs.avail_out;
66 if (writen(output_fd, buf, len) != len) {
71 } while (ret != Z_STREAM_END);
76 munmap(ptr, stbuf.st_size);
80 return ret == Z_STREAM_END ? 0 : -1;
83 bool gzip_is_compressed(const char *input)
85 int fd = open(input, O_RDONLY);
86 const uint8_t magic[2] = { 0x1f, 0x8b };
93 rc = read(fd, buf, sizeof(buf));
95 return rc == sizeof(buf) ?
96 memcmp(buf, magic, sizeof(buf)) == 0 : false;