]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/simple_compression.c
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
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
21 static off_t
fsize_orDie(const char *filename
)
24 if (stat(filename
, &st
) == 0) return st
.st_size
;
30 static FILE* fopen_orDie(const char *filename
, const char *instruction
)
32 FILE* const inFile
= fopen(filename
, instruction
);
33 if (inFile
) return inFile
;
39 static void* malloc_orDie(size_t size
)
41 void* const buff
= malloc(size
);
42 if (buff
) return buff
;
48 static void* loadFile_orDie(const char* fileName
, size_t* size
)
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
);
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
));
63 fclose(inFile
); /* can't fail, read only */
69 static void saveFile_orDie(const char* fileName
, const void* buff
, size_t buffSize
)
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
));
84 static void compress_orDie(const char* fname
, const char* oname
)
87 void* const fBuff
= loadFile_orDie(fname
, &fSize
);
88 size_t const cBuffSize
= ZSTD_compressBound(fSize
);
89 void* const cBuff
= malloc_orDie(cBuffSize
);
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
));
97 saveFile_orDie(oname
, cBuff
, cSize
);
100 printf("%25s : %6u -> %7u - %s \n", fname
, (unsigned)fSize
, (unsigned)cSize
, oname
);
107 static char* createOutFilename_orDie(const char* filename
)
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
;
118 int main(int argc
, const char** argv
)
120 const char* const exeName
= argv
[0];
123 printf("wrong arguments\n");
125 printf("%s FILE\n", exeName
);
129 const char* const inFilename
= argv
[1];
131 char* const outFilename
= createOutFilename_orDie(inFilename
);
132 compress_orDie(inFilename
, outFilename
);