]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/dictionary_decompression.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, 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
22 static off_t
fsize_orDie(const char *filename
)
25 if (stat(filename
, &st
) == 0) return st
.st_size
;
31 static FILE* fopen_orDie(const char *filename
, const char *instruction
)
33 FILE* const inFile
= fopen(filename
, instruction
);
34 if (inFile
) return inFile
;
40 static void* malloc_orDie(size_t size
)
42 void* const buff
= malloc(size
);
43 if (buff
) return buff
;
49 static void* loadFile_orDie(const char* fileName
, size_t* size
)
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
));
65 `dictFileName` is supposed to have been created using `zstd --train` */
66 static ZSTD_DDict
* createDict_orDie(const char* dictFileName
)
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); }
78 static void decompress(const char* fname
, const ZSTD_DDict
* ddict
)
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
);
86 } else if (rSize
==ZSTD_CONTENTSIZE_UNKNOWN
) {
87 fprintf(stderr
, "%s : original size unknown \n", fname
);
91 void* const rBuff
= malloc_orDie((size_t)rSize
);
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
);
97 fprintf(stderr
, "error decoding %s : %s \n", fname
, ZSTD_getErrorName(dSize
));
102 printf("%25s : %6u -> %7u \n", fname
, (unsigned)cSize
, (unsigned)rSize
);
110 int main(int argc
, const char** argv
)
112 const char* const exeName
= argv
[0];
115 printf("wrong arguments\n");
117 printf("%s [FILES] dictionary\n", exeName
);
121 /* load dictionary only once */
122 const char* const dictName
= argv
[argc
-1];
123 ZSTD_DDict
* const dictPtr
= createDict_orDie(dictName
);
126 for (u
=1; u
<argc
-1; u
++) decompress(argv
[u
], dictPtr
);
128 ZSTD_freeDDict(dictPtr
);
129 printf("All %u files correctly decoded (in memory) \n", argc
-2);