]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/simple_compression.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / zstd / examples / simple_compression.c
1 /*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11
12
13 #include <stdlib.h> // malloc, free, exit
14 #include <stdio.h> // fprintf, perror, fopen, etc.
15 #include <string.h> // strlen, strcat, memset, strerror
16 #include <errno.h> // errno
17 #include <sys/stat.h> // stat
18 #include <zstd.h> // presumes zstd library is installed
19
20
21 static off_t fsize_orDie(const char *filename)
22 {
23 struct stat st;
24 if (stat(filename, &st) == 0) return st.st_size;
25 /* error */
26 perror(filename);
27 exit(1);
28 }
29
30 static FILE* fopen_orDie(const char *filename, const char *instruction)
31 {
32 FILE* const inFile = fopen(filename, instruction);
33 if (inFile) return inFile;
34 /* error */
35 perror(filename);
36 exit(2);
37 }
38
39 static void* malloc_orDie(size_t size)
40 {
41 void* const buff = malloc(size);
42 if (buff) return buff;
43 /* error */
44 perror(NULL);
45 exit(3);
46 }
47
48 static void* loadFile_orDie(const char* fileName, size_t* size)
49 {
50 off_t const fileSize = fsize_orDie(fileName);
51 size_t const buffSize = (size_t)fileSize;
52 if ((off_t)buffSize < fileSize) { /* narrowcast overflow */
53 fprintf(stderr, "%s : filesize too large \n", fileName);
54 exit(4);
55 }
56 FILE* const inFile = fopen_orDie(fileName, "rb");
57 void* const buffer = malloc_orDie(buffSize);
58 size_t const readSize = fread(buffer, 1, buffSize, inFile);
59 if (readSize != (size_t)buffSize) {
60 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
61 exit(5);
62 }
63 fclose(inFile); /* can't fail, read only */
64 *size = buffSize;
65 return buffer;
66 }
67
68
69 static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
70 {
71 FILE* const oFile = fopen_orDie(fileName, "wb");
72 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
73 if (wSize != (size_t)buffSize) {
74 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
75 exit(6);
76 }
77 if (fclose(oFile)) {
78 perror(fileName);
79 exit(7);
80 }
81 }
82
83
84 static void compress_orDie(const char* fname, const char* oname)
85 {
86 size_t fSize;
87 void* const fBuff = loadFile_orDie(fname, &fSize);
88 size_t const cBuffSize = ZSTD_compressBound(fSize);
89 void* const cBuff = malloc_orDie(cBuffSize);
90
91 size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
92 if (ZSTD_isError(cSize)) {
93 fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize));
94 exit(8);
95 }
96
97 saveFile_orDie(oname, cBuff, cSize);
98
99 /* success */
100 printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
101
102 free(fBuff);
103 free(cBuff);
104 }
105
106
107 static char* createOutFilename_orDie(const char* filename)
108 {
109 size_t const inL = strlen(filename);
110 size_t const outL = inL + 5;
111 void* const outSpace = malloc_orDie(outL);
112 memset(outSpace, 0, outL);
113 strcat(outSpace, filename);
114 strcat(outSpace, ".zst");
115 return (char*)outSpace;
116 }
117
118 int main(int argc, const char** argv)
119 {
120 const char* const exeName = argv[0];
121
122 if (argc!=2) {
123 printf("wrong arguments\n");
124 printf("usage:\n");
125 printf("%s FILE\n", exeName);
126 return 1;
127 }
128
129 const char* const inFilename = argv[1];
130
131 char* const outFilename = createOutFilename_orDie(inFilename);
132 compress_orDie(inFilename, outFilename);
133 free(outFilename);
134 return 0;
135 }