From 13771971cfaef6356ba47668cae6ce25c5c2071f Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 29 Mar 2020 22:59:04 +0300 Subject: Drop dependency on serge-sans-paille/frozen format: - instead of frozen::unordered_map, create two std::arrays with the aliased names (entries) and respective data (values) libembed: - Resources and CompressedResources convenience classes for raw and compressed resources respectively - Resources can be constexpr in regular usage - Annotate Resources::decompress accordingly --- lib/embed_zstd.cpp | 62 ------------------------------------------------------ 1 file changed, 62 deletions(-) delete mode 100644 lib/embed_zstd.cpp (limited to 'lib/embed_zstd.cpp') diff --git a/lib/embed_zstd.cpp b/lib/embed_zstd.cpp deleted file mode 100644 index d9be35a..0000000 --- a/lib/embed_zstd.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "embed.h" -#include -#include - -using namespace embed; - -ZSTD_DDict *dictPtr = nullptr; - -template <> -Resources::Resources(const std::span &dictionary) -{ - if(!dictionary.empty()) { - dictPtr = ZSTD_createDDict(dictionary.data(), dictionary.size()); - } -} - -template <> -Resources::~Resources() -{ - if(dictPtr != nullptr) { - ZSTD_freeDDict(dictPtr); - } -} - -template <> -[[nodiscard]] std::vector Resources::decompress(const std::span &entry) -{ - /* Read the content size from the frame header. For simplicity we require - * that it is always present. By default, zstd will write the content size - * in the header when it is known. If you can't guarantee that the frame - * content size is always written into the header, either use streaming - * decompression, or ZSTD_decompressBound(). - */ - unsigned long long const rSize = ZSTD_getFrameContentSize(entry.data(), entry.size()); - assert(rSize != ZSTD_CONTENTSIZE_ERROR); //, "%s: not compressed by zstd!", fname); - assert(rSize != ZSTD_CONTENTSIZE_UNKNOWN); //, "%s: original size unknown!", fname); - //data rBuff(new unsigned char[rSize], rSize, true); - std::vector rBuff(rSize); - - /* Check that the dictionary ID matches. - * If a non-zstd dictionary is used, then both will be zero. - * By default zstd always writes the dictionary ID into the frame. - * Zstd will check if there is a dictionary ID mismatch as well. - */ - unsigned const expectedDictID = ZSTD_getDictID_fromDDict(dictPtr); - unsigned const actualDictID = ZSTD_getDictID_fromFrame(entry.data(), entry.size()); - assert(actualDictID == expectedDictID); //"DictID mismatch: expected %u got %u", - - /* Decompress using the dictionary. - * If you need to control the decompression parameters, then use the - * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and - * ZSTD_decompressDCtx(). - */ - ZSTD_DCtx *const dctx = ZSTD_createDCtx(); - assert(dctx != NULL); //, "ZSTD_createDCtx() failed!"); - size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff.data(), rSize, entry.data(), entry.size(), dictPtr); - /* When zstd knows the content size, it will error if it doesn't match. */ - assert(dSize == rSize); //, "Impossible because zstd will check this condition!"); - - ZSTD_freeDCtx(dctx); - return rBuff; -} -- cgit v1.2.1