blob: fc176a3dba39e8a58e844f3641665ed307db910f [file] [log] [blame]
Yann Colletcadd7cd2016-07-15 18:52:37 +02001/*
2 Dictionary decompression
3 Educational program using zstd library
4 Copyright (C) Yann Collet 2016
5
6 GPL v2 License
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 You can contact the author at :
23 - zstd homepage : http://www.zstd.net/
24*/
25
26#include <stdlib.h> // malloc, exit
27#include <stdio.h> // printf
28#include <string.h> // strerror
29#include <errno.h> // errno
30#include <sys/stat.h> // stat
31#include <zstd.h> // presumes zstd library is installed
32
33
34static off_t fsize_X(const char *filename)
35{
36 struct stat st;
37 if (stat(filename, &st) == 0) return st.st_size;
38 /* error */
39 perror(filename);
40 exit(1);
41}
42
43static FILE* fopen_X(const char *filename, const char *instruction)
44{
45 FILE* const inFile = fopen(filename, instruction);
46 if (inFile) return inFile;
47 /* error */
48 perror(filename);
49 exit(2);
50}
51
52static void* malloc_X(size_t size)
53{
54 void* const buff = malloc(size);
55 if (buff) return buff;
56 /* error */
57 perror(NULL);
58 exit(3);
59}
60
61static void* loadFile_X(const char* fileName, size_t* size)
62{
63 off_t const buffSize = fsize_X(fileName);
64 FILE* const inFile = fopen_X(fileName, "rb");
65 void* const buffer = malloc_X(buffSize);
66 size_t const readSize = fread(buffer, 1, buffSize, inFile);
67 if (readSize != (size_t)buffSize) {
68 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
69 exit(4);
70 }
71 fclose(inFile);
72 *size = buffSize;
73 return buffer;
74}
75
76static void saveFile_X(const char* fileName, const void* buff, size_t buffSize)
77{
78 FILE* const oFile = fopen_X(fileName, "wb");
79 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
80 if (wSize != (size_t)buffSize) {
81 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
82 exit(5);
83 }
84 if (fclose(oFile)) {
85 perror(fileName);
86 exit(6);
87 }
88}
89
90/* createDict() :
91 `dictFileName` is supposed to have been created using `zstd --train` */
92static const ZSTD_CDict* createDict(const char* dictFileName)
93{
94 size_t dictSize;
95 printf("loading dictionary %s \n", dictFileName);
96 void* const dictBuffer = loadFile_X(dictFileName, &dictSize);
97 const ZSTD_CDict* const ddict = ZSTD_createCDict(dictBuffer, dictSize, 3);
98 free(dictBuffer);
99 return ddict;
100}
101
102
103static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
104{
105 size_t fSize;
106 void* const fBuff = loadFile_X(fname, &fSize);
107 size_t const cBuffSize = ZSTD_compressBound(fSize);
108 void* const cBuff = malloc_X(cBuffSize);
109
110 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
111 size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
112 if (ZSTD_isError(cSize)) {
113 fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize));
114 exit(7);
115 }
116
117 saveFile_X(oname, cBuff, cSize);
118
119 /* success */
120 printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
121
122 ZSTD_freeCCtx(cctx);
123 free(fBuff);
124 free(cBuff);
125}
126
127
128static char* createOutFilename(const char* filename)
129{
130 size_t const inL = strlen(filename);
131 size_t const outL = inL + 5;
132 void* outSpace = malloc_X(outL);
133 memset(outSpace, 0, outL);
134 strcat(outSpace, filename);
135 strcat(outSpace, ".zst");
136 return (char*)outSpace;
137}
138
139int main(int argc, const char** argv)
140{
141 const char* const exeName = argv[0];
142
143 if (argc<3) {
144 fprintf(stderr, "wrong arguments\n");
145 fprintf(stderr, "usage:\n");
146 fprintf(stderr, "%s [FILES] dictionary\n", exeName);
147 return 1;
148 }
149
150 /* load dictionary only once */
151 const char* const dictName = argv[argc-1];
152 const ZSTD_CDict* const dictPtr = createDict(dictName);
153
154 int u;
155 for (u=1; u<argc-1; u++) {
156 const char* inFilename = argv[u];
157 char* const outFilename = createOutFilename(inFilename);
158 compress(inFilename, outFilename, dictPtr);
159 free(outFilename);
160 }
161
162 printf("All %u files compressed. \n", argc-2);
163}