blob: f76364d8410f83d9303f081e2dd71ddf3e13e9c3 [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet394bdd72017-08-29 09:24:11 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet553b2132016-08-12 18:42:25 +020010
Yann Collet553b2132016-08-12 18:42:25 +020011
Yann Collet16a57522016-12-08 17:28:26 -080012#include <stdlib.h> // malloc, free, exit
13#include <stdio.h> // fprintf, perror, feof, fopen, etc.
14#include <string.h> // strlen, memset, strcat
Yann Collet553b2132016-08-12 18:42:25 +020015#include <zstd.h> // presumes zstd library is installed
16
17
18static void* malloc_orDie(size_t size)
19{
20 void* const buff = malloc(size);
21 if (buff) return buff;
22 /* error */
Yann Collet9f9f1fc2016-08-12 18:56:27 +020023 perror("malloc:");
Yann Collet553b2132016-08-12 18:42:25 +020024 exit(1);
25}
26
27static FILE* fopen_orDie(const char *filename, const char *instruction)
28{
29 FILE* const inFile = fopen(filename, instruction);
30 if (inFile) return inFile;
31 /* error */
32 perror(filename);
33 exit(3);
34}
35
36static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
37{
38 size_t const readSize = fread(buffer, 1, sizeToRead, file);
39 if (readSize == sizeToRead) return readSize; /* good */
40 if (feof(file)) return readSize; /* good, reached end of file */
41 /* error */
42 perror("fread");
43 exit(4);
44}
45
46static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
47{
48 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
49 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
50 /* error */
51 perror("fwrite");
52 exit(5);
53}
54
55static size_t fclose_orDie(FILE* file)
56{
57 if (!fclose(file)) return 0;
58 /* error */
59 perror("fclose");
60 exit(6);
61}
62
63
64static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
65{
66 FILE* const fin = fopen_orDie(fname, "rb");
67 FILE* const fout = fopen_orDie(outName, "wb");
Yann Collet88aa1792016-09-18 11:58:30 +020068 size_t const buffInSize = ZSTD_CStreamInSize(); /* can always read one full block */
Yann Collet553b2132016-08-12 18:42:25 +020069 void* const buffIn = malloc_orDie(buffInSize);
Yann Collet88aa1792016-09-18 11:58:30 +020070 size_t const buffOutSize = ZSTD_CStreamOutSize(); /* can always flush a full block */
Yann Collet553b2132016-08-12 18:42:25 +020071 void* const buffOut = malloc_orDie(buffOutSize);
Yann Collet553b2132016-08-12 18:42:25 +020072
73 ZSTD_CStream* const cstream = ZSTD_createCStream();
74 if (cstream==NULL) { fprintf(stderr, "ZSTD_createCStream() error \n"); exit(10); }
75 size_t const initResult = ZSTD_initCStream(cstream, cLevel);
Yann Collet86bdcd82016-09-20 11:54:29 +020076 if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initCStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
Yann Collet553b2132016-08-12 18:42:25 +020077
Yann Colleta2664642016-09-09 19:33:56 +020078 size_t read, toRead = buffInSize;
Yann Collet553b2132016-08-12 18:42:25 +020079 while( (read = fread_orDie(buffIn, toRead, fin)) ) {
Yann Collet20658792016-08-17 01:48:43 +020080 ZSTD_inBuffer input = { buffIn, read, 0 };
81 while (input.pos < input.size) {
82 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Yann Collet88aa1792016-09-18 11:58:30 +020083 toRead = ZSTD_compressStream(cstream, &output , &input); /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */
Yann Collet86bdcd82016-09-20 11:54:29 +020084 if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_compressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); }
Yann Collet16a57522016-12-08 17:28:26 -080085 if (toRead > buffInSize) toRead = buffInSize; /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/
Yann Collet20658792016-08-17 01:48:43 +020086 fwrite_orDie(buffOut, output.pos, fout);
Yann Collet553b2132016-08-12 18:42:25 +020087 }
88 }
89
Yann Collet20658792016-08-17 01:48:43 +020090 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Yann Colleta2664642016-09-09 19:33:56 +020091 size_t const remainingToFlush = ZSTD_endStream(cstream, &output); /* close frame */
Yann Collet86bdcd82016-09-20 11:54:29 +020092 if (remainingToFlush) { fprintf(stderr, "not fully flushed"); exit(13); }
Yann Collet20658792016-08-17 01:48:43 +020093 fwrite_orDie(buffOut, output.pos, fout);
Yann Collet553b2132016-08-12 18:42:25 +020094
Yann Colleta2664642016-09-09 19:33:56 +020095 ZSTD_freeCStream(cstream);
Yann Collet553b2132016-08-12 18:42:25 +020096 fclose_orDie(fout);
97 fclose_orDie(fin);
98 free(buffIn);
99 free(buffOut);
100}
101
102
103static const char* createOutFilename_orDie(const char* filename)
104{
105 size_t const inL = strlen(filename);
106 size_t const outL = inL + 5;
107 void* outSpace = malloc_orDie(outL);
108 memset(outSpace, 0, outL);
109 strcat(outSpace, filename);
110 strcat(outSpace, ".zst");
111 return (const char*)outSpace;
112}
113
114int main(int argc, const char** argv)
115{
116 const char* const exeName = argv[0];
Yann Collet553b2132016-08-12 18:42:25 +0200117
118 if (argc!=2) {
119 printf("wrong arguments\n");
120 printf("usage:\n");
121 printf("%s FILE\n", exeName);
122 return 1;
123 }
124
niXman65e2cda2017-04-26 13:04:04 +0300125 const char* const inFilename = argv[1];
126
Yann Collet553b2132016-08-12 18:42:25 +0200127 const char* const outFilename = createOutFilename_orDie(inFilename);
128 compressFile_orDie(inFilename, outFilename, 1);
129
130 return 0;
131}