1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/compiler.h>
10 #include "util/compress.h"
11 #include "util/util.h"
12 #include "util/debug.h"
15 #define CHUNK_SIZE 16384
17 int gzip_decompress_to_file(const char *input, int output_fd)
19 int ret = Z_STREAM_ERROR;
24 unsigned char buf[CHUNK_SIZE];
33 input_fd = open(input, O_RDONLY);
37 if (fstat(input_fd, &stbuf) < 0)
40 ptr = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
41 if (ptr == MAP_FAILED)
44 if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK)
48 zs.avail_in = stbuf.st_size;
52 zs.avail_out = CHUNK_SIZE;
54 ret = inflate(&zs, Z_NO_FLUSH);
66 len = CHUNK_SIZE - zs.avail_out;
67 if (writen(output_fd, buf, len) != len) {
72 } while (ret != Z_STREAM_END);
77 munmap(ptr, stbuf.st_size);
81 return ret == Z_STREAM_END ? 0 : -1;
84 bool gzip_is_compressed(const char *input)
86 int fd = open(input, O_RDONLY);
87 const uint8_t magic[2] = { 0x1f, 0x8b };
94 rc = read(fd, buf, sizeof(buf));
96 return rc == sizeof(buf) ?
97 memcmp(buf, magic, sizeof(buf)) == 0 : false;