diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-10-28 13:13:57 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-11-01 10:20:55 +0200 |
commit | 4eb685d94e89a2aef61047225c1834a89de01de2 (patch) | |
tree | 99a6cab3b8766e23aecacc1cb91b00bfcf99700f /lib | |
parent | Drop libc++ default (diff) | |
download | rcc-4eb685d94e89a2aef61047225c1834a89de01de2.tar.xz |
Simplify rccv0.1.0
- drop qrc parsing and zstd compression
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compressionctx.h | 42 | ||||
-rw-r--r-- | lib/embed.h | 88 | ||||
-rw-r--r-- | lib/zstd.cpp | 59 |
3 files changed, 0 insertions, 189 deletions
diff --git a/lib/compressionctx.h b/lib/compressionctx.h deleted file mode 100644 index a07686f..0000000 --- a/lib/compressionctx.h +++ /dev/null @@ -1,42 +0,0 @@ -#include <span> -#include <vector> -#include <cstdint> -#include <memory> - -#pragma once - -namespace embed -{ - -enum Compression { - None, - Zstd -}; - -class CompressionCtx -{ -public: - virtual ~CompressionCtx() = default; - [[nodiscard]] virtual std::vector<uint8_t> decompress(const std::span<const uint8_t> &entry) const = 0; -}; - -class ZstdCompressionCtx final : public CompressionCtx -{ -public: - ZstdCompressionCtx(const std::span<const uint8_t> &dictionary); - ~ZstdCompressionCtx(); - [[nodiscard]] std::vector<uint8_t> decompress(const std::span<const uint8_t> &entry) const override; -}; - -std::unique_ptr<CompressionCtx> make_compression_ctx(const Compression algo, const std::span<const uint8_t> &dict) -{ - switch(algo) - { - case None: - return nullptr; - case Zstd: - return std::make_unique<ZstdCompressionCtx>(dict); - } -} - -} // namespace embed diff --git a/lib/embed.h b/lib/embed.h deleted file mode 100644 index b57d830..0000000 --- a/lib/embed.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include <array> -#include <string_view> -#include "compressionctx.h" - -namespace embed -{ - -template <std::size_t N> -class Resources -{ -public: - constexpr explicit Resources( - const std::array<const char *, N> &entries, - const std::array<std::span<const uint8_t>, N> &values) - : m_entries(entries) - , m_values(values) - { - } - - [[nodiscard]] constexpr int id(const std::string_view &path) const - { - for(std::size_t i = 0; i < m_entries.size(); ++i) { - if(m_entries.at(i) == path) - return i; - } - return -1; - } - [[nodiscard]] constexpr std::span<const uint8_t> value(const int id_) const - { - if(id_ == -1) - return {}; - else - return m_values.at(id_); - } - [[nodiscard]] constexpr std::span<const uint8_t> value(const std::string_view &path) const - { - return value(id(path)); - } - - [[deprecated("With no compression, this function returns a copy; use ::value instead")]] - [[nodiscard]] std::vector<uint8_t> decompress(const int id_) const - { - const auto v = value(id_); - return { v.begin(), v.end() }; - } - [[deprecated("With no compression, this function returns a copy; use ::value instead")]] - [[nodiscard]] std::vector<uint8_t> decompress(const std::string_view &path) const - { - const auto v = value(path); - return { v.begin(), v.end() }; - } - -protected: - const std::array<const char *, N> m_entries; - const std::array<std::span<const uint8_t>, N> m_values; -}; - -template <std::size_t N> -class CompressedResources final : public Resources<N> -{ -public: - explicit CompressedResources( - const std::array<const char *, N> &entries, - const std::array<std::span<const uint8_t>, N> &values, - const Compression algo, - const std::span<const uint8_t> &dictionary = {}) - : Resources<N>(entries, values) - , m_compression(make_compression_ctx(algo, dictionary)) - { - } - ~CompressedResources() = default; - - [[nodiscard]] std::vector<uint8_t> decompress(const int id_) const - { - return m_compression->decompress(Resources<N>::value(id_)); - } - [[nodiscard]] std::vector<uint8_t> decompress(const std::string_view &path) const - { - return m_compression->decompress(Resources<N>::value(path)); - } - -protected: - const std::unique_ptr<CompressionCtx> m_compression; -}; - -} // namespace embed diff --git a/lib/zstd.cpp b/lib/zstd.cpp deleted file mode 100644 index d2c27bc..0000000 --- a/lib/zstd.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "compressionctx.h" -#include <cassert> -#include <zstd.h> - -using namespace embed; - -ZSTD_DDict *dictPtr = nullptr; - -ZstdCompressionCtx::ZstdCompressionCtx(const std::span<const uint8_t> &dictionary) -{ - if(!dictionary.empty()) { - dictPtr = ZSTD_createDDict(dictionary.data(), dictionary.size()); - } -} - -ZstdCompressionCtx::~ZstdCompressionCtx() -{ - if(dictPtr != nullptr) { - ZSTD_freeDDict(dictPtr); - } -} - -[[nodiscard]] -std::vector<uint8_t> ZstdCompressionCtx::decompress(const std::span<const uint8_t> &entry) const -{ - /* 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); - std::vector<uint8_t> 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; -} |