^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <zlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <internal/lib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "util/compress.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define CHUNK_SIZE 16384
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int gzip_decompress_to_file(const char *input, int output_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int ret = Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int input_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct stat stbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned char buf[CHUNK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) z_stream zs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .zalloc = Z_NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .zfree = Z_NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .opaque = Z_NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .avail_in = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .next_in = Z_NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) input_fd = open(input, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (input_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (fstat(input_fd, &stbuf) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ptr = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (ptr == MAP_FAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto out_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) zs.next_in = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) zs.avail_in = stbuf.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) zs.next_out = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) zs.avail_out = CHUNK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret = inflate(&zs, Z_NO_FLUSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) case Z_NEED_DICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = Z_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) case Z_DATA_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) case Z_MEM_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) len = CHUNK_SIZE - zs.avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (writen(output_fd, buf, len) != len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ret = Z_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } while (ret != Z_STREAM_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) inflateEnd(&zs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) out_unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) munmap(ptr, stbuf.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) close(input_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret == Z_STREAM_END ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bool gzip_is_compressed(const char *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int fd = open(input, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const uint8_t magic[2] = { 0x1f, 0x8b };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) char buf[2] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) rc = read(fd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return rc == sizeof(buf) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) memcmp(buf, magic, sizeof(buf)) == 0 : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }