~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/zlib.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php