]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/dictionary_decompression.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / zstd / examples / dictionary_decompression.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, exit
14 #include <stdio.h> // printf
15 #include <string.h> // strerror
16 #include <errno.h> // errno
17 #include <sys/stat.h> // stat
18 #define ZSTD_STATIC_LINKING_ONLY // ZSTD_findDecompressedSize
19 #include <zstd.h> // presumes zstd library is installed
20
21
22 static off_t fsize_orDie(const char *filename)
23 {
24 struct stat st;
25 if (stat(filename, &st) == 0) return st.st_size;
26 /* error */
27 perror(filename);
28 exit(1);
29 }
30
31 static FILE* fopen_orDie(const char *filename, const char *instruction)
32 {
33 FILE* const inFile = fopen(filename, instruction);
34 if (inFile) return inFile;
35 /* error */
36 perror(filename);
37 exit(2);
38 }
39
40 static void* malloc_orDie(size_t size)
41 {
42 void* const buff = malloc(size);
43 if (buff) return buff;
44 /* error */
45 perror("malloc");
46 exit(3);
47 }
48
49 static void* loadFile_orDie(const char* fileName, size_t* size)
50 {
51 off_t const buffSize = fsize_orDie(fileName);
52 FILE* const inFile = fopen_orDie(fileName, "rb");
53 void* const buffer = malloc_orDie(buffSize);
54 size_t const readSize = fread(buffer, 1, buffSize, inFile);
55 if (readSize != (size_t)buffSize) {
56 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
57 exit(4);
58 }
59 fclose(inFile);
60 *size = buffSize;
61 return buffer;
62 }
63
64 /* createDict() :
65 `dictFileName` is supposed to have been created using `zstd --train` */
66 static ZSTD_DDict* createDict_orDie(const char* dictFileName)
67 {
68 size_t dictSize;
69 printf("loading dictionary %s \n", dictFileName);
70 void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize);
71 ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
72 if (ddict==NULL) { fprintf(stderr, "ZSTD_createDDict error \n"); exit(5); }
73 free(dictBuffer);
74 return ddict;
75 }
76
77
78 static void decompress(const char* fname, const ZSTD_DDict* ddict)
79 {
80 size_t cSize;
81 void* const cBuff = loadFile_orDie(fname, &cSize);
82 unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize);
83 if (rSize==ZSTD_CONTENTSIZE_ERROR) {
84 fprintf(stderr, "%s : it was not compressed by zstd.\n", fname);
85 exit(5);
86 } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) {
87 fprintf(stderr, "%s : original size unknown \n", fname);
88 exit(6);
89 }
90
91 void* const rBuff = malloc_orDie((size_t)rSize);
92
93 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
94 if (dctx==NULL) { fprintf(stderr, "ZSTD_createDCtx() error \n"); exit(10); }
95 size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
96 if (dSize != rSize) {
97 fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize));
98 exit(7);
99 }
100
101 /* success */
102 printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
103
104 ZSTD_freeDCtx(dctx);
105 free(rBuff);
106 free(cBuff);
107 }
108
109
110 int main(int argc, const char** argv)
111 {
112 const char* const exeName = argv[0];
113
114 if (argc<3) {
115 printf("wrong arguments\n");
116 printf("usage:\n");
117 printf("%s [FILES] dictionary\n", exeName);
118 return 1;
119 }
120
121 /* load dictionary only once */
122 const char* const dictName = argv[argc-1];
123 ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
124
125 int u;
126 for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
127
128 ZSTD_freeDDict(dictPtr);
129 printf("All %u files correctly decoded (in memory) \n", argc-2);
130 return 0;
131 }