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 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame^] | 12 | #include <stdio.h> // fprintf |
| 13 | #include <stdlib.h> // free |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 14 | #include <zstd.h> // presumes zstd library is installed |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame^] | 15 | #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD() |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 16 | |
| 17 | static void decompressFile_orDie(const char* fname) |
| 18 | { |
| 19 | FILE* const fin = fopen_orDie(fname, "rb"); |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 20 | size_t const buffInSize = ZSTD_DStreamInSize(); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 21 | void* const buffIn = malloc_orDie(buffInSize); |
Yann Collet | b94fcc8 | 2016-09-08 19:48:04 +0200 | [diff] [blame] | 22 | FILE* const fout = stdout; |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 23 | 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] | 24 | void* const buffOut = malloc_orDie(buffOutSize); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 25 | |
Nick Terrell | de58910 | 2019-04-01 17:25:26 -0700 | [diff] [blame] | 26 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 27 | CHECK(dctx != NULL, "ZSTD_createDCtx() failed!"); |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 28 | |
Nick Terrell | de58910 | 2019-04-01 17:25:26 -0700 | [diff] [blame] | 29 | /* This loop assumes that the input file is one or more concatenated zstd |
| 30 | * streams. This example won't work if there is trailing non-zstd data at |
| 31 | * the end, but streaming decompression in general handles this case. |
| 32 | * ZSTD_decompressStream() returns 0 exactly when the frame is completed, |
| 33 | * and doesn't consume input after the frame. |
| 34 | */ |
| 35 | size_t const toRead = buffInSize; |
| 36 | size_t read; |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 37 | while ( (read = fread_orDie(buffIn, toRead, fin)) ) { |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 38 | ZSTD_inBuffer input = { buffIn, read, 0 }; |
Nick Terrell | de58910 | 2019-04-01 17:25:26 -0700 | [diff] [blame] | 39 | /* Given a valid frame, zstd won't consume the last byte of the frame |
| 40 | * until it has flushed all of the decompressed data of the frame. |
| 41 | * Therefore, instead of checking if the return code is 0, we can |
| 42 | * decompress just check if input.pos < input.size. |
| 43 | */ |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 44 | while (input.pos < input.size) { |
| 45 | ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
Nick Terrell | de58910 | 2019-04-01 17:25:26 -0700 | [diff] [blame] | 46 | /* The return code is zero if the frame is complete, but there may |
| 47 | * be multiple frames concatenated together. Zstd will automatically |
| 48 | * reset the context when a frame is complete. Still, calling |
| 49 | * ZSTD_DCtx_reset() can be useful to reset the context to a clean |
| 50 | * state, for instance if the last decompression call returned an |
| 51 | * error. |
| 52 | */ |
| 53 | size_t const ret = ZSTD_decompressStream(dctx, &output , &input); |
| 54 | CHECK_ZSTD(ret); |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 55 | fwrite_orDie(buffOut, output.pos, fout); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Nick Terrell | de58910 | 2019-04-01 17:25:26 -0700 | [diff] [blame] | 59 | ZSTD_freeDCtx(dctx); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 60 | fclose_orDie(fin); |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 61 | fclose_orDie(fout); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 62 | free(buffIn); |
| 63 | free(buffOut); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | int main(int argc, const char** argv) |
| 68 | { |
| 69 | const char* const exeName = argv[0]; |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 70 | |
| 71 | if (argc!=2) { |
Yann Collet | 264c733 | 2016-09-08 19:39:00 +0200 | [diff] [blame] | 72 | fprintf(stderr, "wrong arguments\n"); |
| 73 | fprintf(stderr, "usage:\n"); |
| 74 | fprintf(stderr, "%s FILE\n", exeName); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 75 | return 1; |
| 76 | } |
| 77 | |
niXman | 65e2cda | 2017-04-26 13:04:04 +0300 | [diff] [blame] | 78 | const char* const inFilename = argv[1]; |
| 79 | |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 80 | decompressFile_orDie(inFilename); |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |