Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
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 | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 10 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 11 | #include <stdio.h> // printf |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 12 | #include <stdlib.h> // free |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 13 | #include <zstd.h> // presumes zstd library is installed |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 14 | #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD() |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 15 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 16 | static void decompress(const char* fname) |
| 17 | { |
| 18 | size_t cSize; |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 19 | void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 20 | /* Read the content size from the frame header. For simplicity we require |
| 21 | * that it is always present. By default, zstd will write the content size |
| 22 | * in the header when it is known. If you can't guarantee that the frame |
| 23 | * content size is always written into the header, either use streaming |
| 24 | * decompression, or ZSTD_decompressBound(). |
| 25 | */ |
| 26 | unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize); |
| 27 | CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname); |
| 28 | CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname); |
Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 29 | |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 30 | void* const rBuff = malloc_orDie((size_t)rSize); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 31 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 32 | /* Decompress. |
| 33 | * If you are doing many decompressions, you may want to reuse the context |
| 34 | * and use ZSTD_decompressDCtx(). If you want to set advanced parameters, |
| 35 | * use ZSTD_DCtx_setParameter(). |
| 36 | */ |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 37 | size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 38 | CHECK_ZSTD(dSize); |
| 39 | /* When zstd knows the content size, it will error if it doesn't match. */ |
| 40 | CHECK(dSize == rSize, "Impossible because zstd will check this condition!"); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 41 | |
| 42 | /* success */ |
| 43 | printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); |
| 44 | |
| 45 | free(rBuff); |
| 46 | free(cBuff); |
| 47 | } |
| 48 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 49 | int main(int argc, const char** argv) |
| 50 | { |
| 51 | const char* const exeName = argv[0]; |
| 52 | |
| 53 | if (argc!=2) { |
| 54 | printf("wrong arguments\n"); |
| 55 | printf("usage:\n"); |
| 56 | printf("%s FILE\n", exeName); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | decompress(argv[1]); |
| 61 | |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 62 | printf("%s correctly decoded (in memory). \n", argv[1]); |
| 63 | |
| 64 | return 0; |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 65 | } |