| Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 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). |
| Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 10 | |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 11 | |
| 12 | #include <stdlib.h> // malloc, exit |
| 13 | #include <stdio.h> // fprintf, perror, feof |
| 14 | #include <string.h> // strerror |
| 15 | #include <errno.h> // errno |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 16 | #include <zstd.h> // presumes zstd library is installed |
| 17 | |
| 18 | |
| 19 | static 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 | |
| 28 | static 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 | |
| 37 | static 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 Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 47 | static 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 Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 56 | static size_t fclose_orDie(FILE* file) |
| 57 | { |
| 58 | if (!fclose(file)) return 0; |
| 59 | /* error */ |
| 60 | perror("fclose"); |
| 61 | exit(6); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static void decompressFile_orDie(const char* fname) |
| 66 | { |
| 67 | FILE* const fin = fopen_orDie(fname, "rb"); |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 68 | size_t const buffInSize = ZSTD_DStreamInSize(); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 69 | void* const buffIn = malloc_orDie(buffInSize); |
| Yann Collet | b94fcc8 | 2016-09-08 19:48:04 +0200 | [diff] [blame] | 70 | FILE* const fout = stdout; |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 71 | size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */ |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 72 | void* const buffOut = malloc_orDie(buffOutSize); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 73 | |
| 74 | ZSTD_DStream* const dstream = ZSTD_createDStream(); |
| 75 | if (dstream==NULL) { fprintf(stderr, "ZSTD_createDStream() error \n"); exit(10); } |
| Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 76 | |
| 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 Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 80 | size_t const initResult = ZSTD_initDStream(dstream); |
| Yann Collet | b94fcc8 | 2016-09-08 19:48:04 +0200 | [diff] [blame] | 81 | if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initDStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); } |
| Yann Collet | b94fcc8 | 2016-09-08 19:48:04 +0200 | [diff] [blame] | 82 | size_t read, toRead = initResult; |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 83 | while ( (read = fread_orDie(buffIn, toRead, fin)) ) { |
| Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 84 | ZSTD_inBuffer input = { buffIn, read, 0 }; |
| 85 | while (input.pos < input.size) { |
| 86 | ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 87 | toRead = ZSTD_decompressStream(dstream, &output , &input); /* toRead : size of next compressed block */ |
| Yann Collet | 47f3697 | 2016-09-20 11:59:12 +0200 | [diff] [blame] | 88 | if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_decompressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); } |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 89 | fwrite_orDie(buffOut, output.pos, fout); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 93 | ZSTD_freeDStream(dstream); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 94 | fclose_orDie(fin); |
| Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 95 | fclose_orDie(fout); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 96 | free(buffIn); |
| 97 | free(buffOut); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | int main(int argc, const char** argv) |
| 102 | { |
| 103 | const char* const exeName = argv[0]; |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 104 | |
| 105 | if (argc!=2) { |
| Yann Collet | 264c733 | 2016-09-08 19:39:00 +0200 | [diff] [blame] | 106 | fprintf(stderr, "wrong arguments\n"); |
| 107 | fprintf(stderr, "usage:\n"); |
| 108 | fprintf(stderr, "%s FILE\n", exeName); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
| niXman | 65e2cda | 2017-04-26 13:04:04 +0300 | [diff] [blame] | 112 | const char* const inFilename = argv[1]; |
| 113 | |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 114 | decompressFile_orDie(inFilename); |
| Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |