blob: 504a5e31694770858edbe0461230e724a3e19312 [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 Collet9f9f1fc2016-08-12 18:56:27 +020010
Yann Collet9f9f1fc2016-08-12 18:56:27 +020011
12#include <stdlib.h> // malloc, exit
13#include <stdio.h> // fprintf, perror, feof
14#include <string.h> // strerror
15#include <errno.h> // errno
Yann Collet9f9f1fc2016-08-12 18:56:27 +020016#include <zstd.h> // presumes zstd library is installed
17
18
19static void* malloc_orDie(size_t size)
20{
21 void* const buff = malloc(size);
22 if (buff) return buff;
23 /* error */
24 perror("malloc:");
25 exit(1);
26}
27
28static FILE* fopen_orDie(const char *filename, const char *instruction)
29{
30 FILE* const inFile = fopen(filename, instruction);
31 if (inFile) return inFile;
32 /* error */
33 perror(filename);
34 exit(3);
35}
36
37static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
38{
39 size_t const readSize = fread(buffer, 1, sizeToRead, file);
40 if (readSize == sizeToRead) return readSize; /* good */
41 if (feof(file)) return readSize; /* good, reached end of file */
42 /* error */
43 perror("fread");
44 exit(4);
45}
46
Yann Collet01c19922016-09-08 19:29:04 +020047static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
48{
49 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
50 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
51 /* error */
52 perror("fwrite");
53 exit(5);
54}
55
Yann Collet9f9f1fc2016-08-12 18:56:27 +020056static size_t fclose_orDie(FILE* file)
57{
58 if (!fclose(file)) return 0;
59 /* error */
60 perror("fclose");
61 exit(6);
62}
63
64
65static void decompressFile_orDie(const char* fname)
66{
67 FILE* const fin = fopen_orDie(fname, "rb");
Yann Collet01c19922016-09-08 19:29:04 +020068 size_t const buffInSize = ZSTD_DStreamInSize();
Yann Collet9f9f1fc2016-08-12 18:56:27 +020069 void* const buffIn = malloc_orDie(buffInSize);
Yann Colletb94fcc82016-09-08 19:48:04 +020070 FILE* const fout = stdout;
Yann Collet01c19922016-09-08 19:29:04 +020071 size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
Yann Collet9f9f1fc2016-08-12 18:56:27 +020072 void* const buffOut = malloc_orDie(buffOutSize);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020073
74 ZSTD_DStream* const dstream = ZSTD_createDStream();
75 if (dstream==NULL) { fprintf(stderr, "ZSTD_createDStream() error \n"); exit(10); }
Yann Collet22de81e2016-10-28 13:58:31 -070076
77 /* In more complex scenarios, a file may consist of multiple appended frames (ex : pzstd).
78 * The following example decompresses only the first frame.
79 * It is compatible with other provided streaming examples */
Yann Collet9f9f1fc2016-08-12 18:56:27 +020080 size_t const initResult = ZSTD_initDStream(dstream);
Yann Colletb94fcc82016-09-08 19:48:04 +020081 if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initDStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
Yann Colletb94fcc82016-09-08 19:48:04 +020082 size_t read, toRead = initResult;
Yann Collet01c19922016-09-08 19:29:04 +020083 while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
Yann Collet20658792016-08-17 01:48:43 +020084 ZSTD_inBuffer input = { buffIn, read, 0 };
85 while (input.pos < input.size) {
86 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Yann Collet01c19922016-09-08 19:29:04 +020087 toRead = ZSTD_decompressStream(dstream, &output , &input); /* toRead : size of next compressed block */
Yann Collet47f36972016-09-20 11:59:12 +020088 if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_decompressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); }
Yann Collet01c19922016-09-08 19:29:04 +020089 fwrite_orDie(buffOut, output.pos, fout);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020090 }
91 }
92
Yann Colleta2664642016-09-09 19:33:56 +020093 ZSTD_freeDStream(dstream);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020094 fclose_orDie(fin);
Yann Collet01c19922016-09-08 19:29:04 +020095 fclose_orDie(fout);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020096 free(buffIn);
97 free(buffOut);
98}
99
100
101int main(int argc, const char** argv)
102{
103 const char* const exeName = argv[0];
Yann Collet9f9f1fc2016-08-12 18:56:27 +0200104
105 if (argc!=2) {
Yann Collet264c7332016-09-08 19:39:00 +0200106 fprintf(stderr, "wrong arguments\n");
107 fprintf(stderr, "usage:\n");
108 fprintf(stderr, "%s FILE\n", exeName);
Yann Collet9f9f1fc2016-08-12 18:56:27 +0200109 return 1;
110 }
111
niXman65e2cda2017-04-26 13:04:04 +0300112 const char* const inFilename = argv[1];
113
Yann Collet9f9f1fc2016-08-12 18:56:27 +0200114 decompressFile_orDie(inFilename);
Yann Collet9f9f1fc2016-08-12 18:56:27 +0200115 return 0;
116}