static void decompress(const char* fname) { size_t cSize; void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize); unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize); if (rSize==ZSTD_CONTENTSIZE_ERROR) { fprintf(stderr, "%s : it was not compressed by zstd.\n", fname); exit(5); } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) { fprintf(stderr, "%s : original size unknown. Use streaming decompression instead.\n", fname); exit(6); } void* const rBuff = malloc_orDie((size_t)rSize); size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize); if (dSize != rSize) { fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize)); exit(7); } /* success */ printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); free(rBuff); free(cBuff); }
static void decompress(const char* fname, const ZSTD_DDict* ddict) { size_t cSize; void* const cBuff = loadFile_orDie(fname, &cSize); unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize); if (rSize==ZSTD_CONTENTSIZE_ERROR) { fprintf(stderr, "%s : it was not compressed by zstd.\n", fname); exit(5); } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) { fprintf(stderr, "%s : original size unknown \n", fname); exit(6); } void* const rBuff = malloc_orDie((size_t)rSize); ZSTD_DCtx* const dctx = ZSTD_createDCtx(); if (dctx==NULL) { fprintf(stderr, "ZSTD_createDCtx() error \n"); exit(10); } size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict); if (dSize != rSize) { fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize)); exit(7); } /* success */ printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); ZSTD_freeDCtx(dctx); free(rBuff); free(cBuff); }
static ezResult DecompressZStd(ezArrayPtr<const ezUInt8> pCompressedData, ezDynamicArray<ezUInt8>& out_Data) { size_t uiSize = ZSTD_findDecompressedSize(pCompressedData.GetPtr(), pCompressedData.GetCount()); if (uiSize == ZSTD_CONTENTSIZE_ERROR) { ezLog::Error("Can't decompress since it wasn't compressed with ZStd"); return EZ_FAILURE; } else if (uiSize == ZSTD_CONTENTSIZE_UNKNOWN) { ezLog::Error("Can't decompress since the original size can't be determined, was the data compressed using the streaming variant?"); return EZ_FAILURE; } if (uiSize > std::numeric_limits<ezUInt32>::max()) { ezLog::Error("Can't compress since the output container can't hold enough elements ({0})", static_cast<ezUInt64>(uiSize)); return EZ_FAILURE; } out_Data.SetCountUninitialized(static_cast<ezUInt32>(uiSize)); size_t const uiActualSize = ZSTD_decompress(out_Data.GetData(), uiSize, pCompressedData.GetPtr(), pCompressedData.GetCount()); if (uiActualSize != uiSize) { ezLog::Error("Error during ZStd decompression: '{0}'.", ZSTD_getErrorName(uiActualSize)); return EZ_FAILURE; } return EZ_SUCCESS; }