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 | |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 12 | #include <stdio.h> // printf |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 13 | #include <stdlib.h> // free |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +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 | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 16 | |
Yann Collet | e66708d | 2016-07-09 22:56:12 +0200 | [diff] [blame] | 17 | /* createDict() : |
| 18 | `dictFileName` is supposed to have been created using `zstd --train` */ |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 19 | static ZSTD_DDict* createDict_orDie(const char* dictFileName) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 20 | { |
| 21 | size_t dictSize; |
Yann Collet | 5e80dd3 | 2016-07-13 17:38:39 +0200 | [diff] [blame] | 22 | printf("loading dictionary %s \n", dictFileName); |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 23 | void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize); |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 24 | ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 25 | CHECK(ddict != NULL, "ZSTD_createDDict() failed!"); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 26 | free(dictBuffer); |
| 27 | return ddict; |
| 28 | } |
| 29 | |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 30 | static void decompress(const char* fname, const ZSTD_DDict* ddict) |
| 31 | { |
| 32 | size_t cSize; |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 33 | void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 34 | /* Read the content size from the frame header. For simplicity we require |
| 35 | * that it is always present. By default, zstd will write the content size |
| 36 | * in the header when it is known. If you can't guarantee that the frame |
| 37 | * content size is always written into the header, either use streaming |
| 38 | * decompression, or ZSTD_decompressBound(). |
| 39 | */ |
| 40 | unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize); |
| 41 | CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname); |
| 42 | CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname); |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 43 | void* const rBuff = malloc_orDie((size_t)rSize); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 44 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 45 | /* Check that the dictionary ID matches. |
| 46 | * If a non-zstd dictionary is used, then both will be zero. |
| 47 | * By default zstd always writes the dictionary ID into the frame. |
| 48 | * Zstd will check if there is a dictionary ID mismatch as well. |
| 49 | */ |
| 50 | unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict); |
| 51 | unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize); |
| 52 | CHECK(actualDictID == expectedDictID, |
| 53 | "DictID mismatch: expected %u got %u", |
| 54 | expectedDictID, |
| 55 | actualDictID); |
| 56 | |
| 57 | /* Decompress using the dictionary. |
| 58 | * If you need to control the decompression parameters, then use the |
| 59 | * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and |
| 60 | * ZSTD_decompressDCtx(). |
| 61 | */ |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 62 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 63 | CHECK(dctx != NULL, "ZSTD_createDCtx() failed!"); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 64 | size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 65 | CHECK_ZSTD(dSize); |
| 66 | /* When zstd knows the content size, it will error if it doesn't match. */ |
| 67 | CHECK(dSize == rSize, "Impossible because zstd will check this condition!"); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 68 | |
| 69 | /* success */ |
| 70 | printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); |
| 71 | |
| 72 | ZSTD_freeDCtx(dctx); |
| 73 | free(rBuff); |
| 74 | free(cBuff); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | int main(int argc, const char** argv) |
| 79 | { |
| 80 | const char* const exeName = argv[0]; |
| 81 | |
| 82 | if (argc<3) { |
| 83 | printf("wrong arguments\n"); |
| 84 | printf("usage:\n"); |
| 85 | printf("%s [FILES] dictionary\n", exeName); |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | /* load dictionary only once */ |
| 90 | const char* const dictName = argv[argc-1]; |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 91 | ZSTD_DDict* const dictPtr = createDict_orDie(dictName); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 92 | |
| 93 | int u; |
| 94 | for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr); |
| 95 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 96 | ZSTD_freeDDict(dictPtr); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 97 | printf("All %u files correctly decoded (in memory) \n", argc-2); |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 98 | return 0; |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 99 | } |