| 28 | } |
| 29 | |
| 30 | static void decompress(const char* fname, const ZSTD_DDict* ddict) |
| 31 | { |
| 32 | size_t cSize; |
| 33 | void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize); |
| 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); |
| 43 | void* const rBuff = malloc_orDie((size_t)rSize); |
| 44 | |
| 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 | */ |
| 62 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 63 | CHECK(dctx != NULL, "ZSTD_createDCtx() failed!"); |
| 64 | size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict); |
| 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!"); |
| 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) |
no test coverage detected