1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/compiler.h>
11 #include "util/compress.h"
12 #include "util/util.h"
13 #include "util/debug.h"
16 #define CHUNK_SIZE 16384
18 int gzip_decompress_to_file(const char *input, int output_fd)
20 int ret = Z_STREAM_ERROR;
25 unsigned char buf[CHUNK_SIZE];
34 input_fd = open(input, O_RDONLY);
38 if (fstat(input_fd, &stbuf) < 0)
41 ptr = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
42 if (ptr == MAP_FAILED)
45 if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK)
49 zs.avail_in = stbuf.st_size;
53 zs.avail_out = CHUNK_SIZE;
55 ret = inflate(&zs, Z_NO_FLUSH);
67 len = CHUNK_SIZE - zs.avail_out;
68 if (writen(output_fd, buf, len) != len) {
73 } while (ret != Z_STREAM_END);
78 munmap(ptr, stbuf.st_size);
82 return ret == Z_STREAM_END ? 0 : -1;
85 bool gzip_is_compressed(const char *input)
87 int fd = open(input, O_RDONLY);
88 const uint8_t magic[2] = { 0x1f, 0x8b };
95 rc = read(fd, buf, sizeof(buf));
97 return rc == sizeof(buf) ?
98 memcmp(buf, magic, sizeof(buf)) == 0 : false;